xref: /sqlite-3.40.0/src/shell.c.in (revision 5f0d37b5)
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** This routine reads a line of text from FILE in, stores
628** the text in memory obtained from malloc() and returns a pointer
629** to the text.  NULL is returned at end of file, or if malloc()
630** fails.
631**
632** If zLine is not NULL then it is a malloced buffer returned from
633** a previous call to this routine that may be reused.
634*/
635static char *local_getline(char *zLine, FILE *in){
636  int nLine = zLine==0 ? 0 : 100;
637  int n = 0;
638
639  while( 1 ){
640    if( n+100>nLine ){
641      nLine = nLine*2 + 100;
642      zLine = realloc(zLine, nLine);
643      if( zLine==0 ) shell_out_of_memory();
644    }
645    if( fgets(&zLine[n], nLine - n, in)==0 ){
646      if( n==0 ){
647        free(zLine);
648        return 0;
649      }
650      zLine[n] = 0;
651      break;
652    }
653    while( zLine[n] ) n++;
654    if( n>0 && zLine[n-1]=='\n' ){
655      n--;
656      if( n>0 && zLine[n-1]=='\r' ) n--;
657      zLine[n] = 0;
658      break;
659    }
660  }
661#if defined(_WIN32) || defined(WIN32)
662  /* For interactive input on Windows systems, translate the
663  ** multi-byte characterset characters into UTF-8. */
664  if( stdin_is_interactive && in==stdin ){
665    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
666    if( zTrans ){
667      int nTrans = strlen30(zTrans)+1;
668      if( nTrans>nLine ){
669        zLine = realloc(zLine, nTrans);
670        if( zLine==0 ) shell_out_of_memory();
671      }
672      memcpy(zLine, zTrans, nTrans);
673      sqlite3_free(zTrans);
674    }
675  }
676#endif /* defined(_WIN32) || defined(WIN32) */
677  return zLine;
678}
679
680/*
681** Retrieve a single line of input text.
682**
683** If in==0 then read from standard input and prompt before each line.
684** If isContinuation is true, then a continuation prompt is appropriate.
685** If isContinuation is zero, then the main prompt should be used.
686**
687** If zPrior is not NULL then it is a buffer from a prior call to this
688** routine that can be reused.
689**
690** The result is stored in space obtained from malloc() and must either
691** be freed by the caller or else passed back into this routine via the
692** zPrior argument for reuse.
693*/
694static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
695  char *zPrompt;
696  char *zResult;
697  if( in!=0 ){
698    zResult = local_getline(zPrior, in);
699  }else{
700    zPrompt = isContinuation ? continuePrompt : mainPrompt;
701#if SHELL_USE_LOCAL_GETLINE
702    printf("%s", zPrompt);
703    fflush(stdout);
704    zResult = local_getline(zPrior, stdin);
705#else
706    free(zPrior);
707    zResult = shell_readline(zPrompt);
708    if( zResult && *zResult ) shell_add_history(zResult);
709#endif
710  }
711  return zResult;
712}
713
714
715/*
716** Return the value of a hexadecimal digit.  Return -1 if the input
717** is not a hex digit.
718*/
719static int hexDigitValue(char c){
720  if( c>='0' && c<='9' ) return c - '0';
721  if( c>='a' && c<='f' ) return c - 'a' + 10;
722  if( c>='A' && c<='F' ) return c - 'A' + 10;
723  return -1;
724}
725
726/*
727** Interpret zArg as an integer value, possibly with suffixes.
728*/
729static sqlite3_int64 integerValue(const char *zArg){
730  sqlite3_int64 v = 0;
731  static const struct { char *zSuffix; int iMult; } aMult[] = {
732    { "KiB", 1024 },
733    { "MiB", 1024*1024 },
734    { "GiB", 1024*1024*1024 },
735    { "KB",  1000 },
736    { "MB",  1000000 },
737    { "GB",  1000000000 },
738    { "K",   1000 },
739    { "M",   1000000 },
740    { "G",   1000000000 },
741  };
742  int i;
743  int isNeg = 0;
744  if( zArg[0]=='-' ){
745    isNeg = 1;
746    zArg++;
747  }else if( zArg[0]=='+' ){
748    zArg++;
749  }
750  if( zArg[0]=='0' && zArg[1]=='x' ){
751    int x;
752    zArg += 2;
753    while( (x = hexDigitValue(zArg[0]))>=0 ){
754      v = (v<<4) + x;
755      zArg++;
756    }
757  }else{
758    while( IsDigit(zArg[0]) ){
759      v = v*10 + zArg[0] - '0';
760      zArg++;
761    }
762  }
763  for(i=0; i<ArraySize(aMult); i++){
764    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
765      v *= aMult[i].iMult;
766      break;
767    }
768  }
769  return isNeg? -v : v;
770}
771
772/*
773** A variable length string to which one can append text.
774*/
775typedef struct ShellText ShellText;
776struct ShellText {
777  char *z;
778  int n;
779  int nAlloc;
780};
781
782/*
783** Initialize and destroy a ShellText object
784*/
785static void initText(ShellText *p){
786  memset(p, 0, sizeof(*p));
787}
788static void freeText(ShellText *p){
789  free(p->z);
790  initText(p);
791}
792
793/* zIn is either a pointer to a NULL-terminated string in memory obtained
794** from malloc(), or a NULL pointer. The string pointed to by zAppend is
795** added to zIn, and the result returned in memory obtained from malloc().
796** zIn, if it was not NULL, is freed.
797**
798** If the third argument, quote, is not '\0', then it is used as a
799** quote character for zAppend.
800*/
801static void appendText(ShellText *p, char const *zAppend, char quote){
802  int len;
803  int i;
804  int nAppend = strlen30(zAppend);
805
806  len = nAppend+p->n+1;
807  if( quote ){
808    len += 2;
809    for(i=0; i<nAppend; i++){
810      if( zAppend[i]==quote ) len++;
811    }
812  }
813
814  if( p->n+len>=p->nAlloc ){
815    p->nAlloc = p->nAlloc*2 + len + 20;
816    p->z = realloc(p->z, p->nAlloc);
817    if( p->z==0 ) shell_out_of_memory();
818  }
819
820  if( quote ){
821    char *zCsr = p->z+p->n;
822    *zCsr++ = quote;
823    for(i=0; i<nAppend; i++){
824      *zCsr++ = zAppend[i];
825      if( zAppend[i]==quote ) *zCsr++ = quote;
826    }
827    *zCsr++ = quote;
828    p->n = (int)(zCsr - p->z);
829    *zCsr = '\0';
830  }else{
831    memcpy(p->z+p->n, zAppend, nAppend);
832    p->n += nAppend;
833    p->z[p->n] = '\0';
834  }
835}
836
837/*
838** Attempt to determine if identifier zName needs to be quoted, either
839** because it contains non-alphanumeric characters, or because it is an
840** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
841** that quoting is required.
842**
843** Return '"' if quoting is required.  Return 0 if no quoting is required.
844*/
845static char quoteChar(const char *zName){
846  int i;
847  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
848  for(i=0; zName[i]; i++){
849    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
850  }
851  return sqlite3_keyword_check(zName, i) ? '"' : 0;
852}
853
854/*
855** Construct a fake object name and column list to describe the structure
856** of the view, virtual table, or table valued function zSchema.zName.
857*/
858static char *shellFakeSchema(
859  sqlite3 *db,            /* The database connection containing the vtab */
860  const char *zSchema,    /* Schema of the database holding the vtab */
861  const char *zName       /* The name of the virtual table */
862){
863  sqlite3_stmt *pStmt = 0;
864  char *zSql;
865  ShellText s;
866  char cQuote;
867  char *zDiv = "(";
868  int nRow = 0;
869
870  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
871                         zSchema ? zSchema : "main", zName);
872  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
873  sqlite3_free(zSql);
874  initText(&s);
875  if( zSchema ){
876    cQuote = quoteChar(zSchema);
877    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
878    appendText(&s, zSchema, cQuote);
879    appendText(&s, ".", 0);
880  }
881  cQuote = quoteChar(zName);
882  appendText(&s, zName, cQuote);
883  while( sqlite3_step(pStmt)==SQLITE_ROW ){
884    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
885    nRow++;
886    appendText(&s, zDiv, 0);
887    zDiv = ",";
888    cQuote = quoteChar(zCol);
889    appendText(&s, zCol, cQuote);
890  }
891  appendText(&s, ")", 0);
892  sqlite3_finalize(pStmt);
893  if( nRow==0 ){
894    freeText(&s);
895    s.z = 0;
896  }
897  return s.z;
898}
899
900/*
901** SQL function:  shell_module_schema(X)
902**
903** Return a fake schema for the table-valued function or eponymous virtual
904** table X.
905*/
906static void shellModuleSchema(
907  sqlite3_context *pCtx,
908  int nVal,
909  sqlite3_value **apVal
910){
911  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
912  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
913  UNUSED_PARAMETER(nVal);
914  if( zFake ){
915    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
916                        -1, sqlite3_free);
917    free(zFake);
918  }
919}
920
921/*
922** SQL function:  shell_add_schema(S,X)
923**
924** Add the schema name X to the CREATE statement in S and return the result.
925** Examples:
926**
927**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
928**
929** Also works on
930**
931**    CREATE INDEX
932**    CREATE UNIQUE INDEX
933**    CREATE VIEW
934**    CREATE TRIGGER
935**    CREATE VIRTUAL TABLE
936**
937** This UDF is used by the .schema command to insert the schema name of
938** attached databases into the middle of the sqlite_schema.sql field.
939*/
940static void shellAddSchemaName(
941  sqlite3_context *pCtx,
942  int nVal,
943  sqlite3_value **apVal
944){
945  static const char *aPrefix[] = {
946     "TABLE",
947     "INDEX",
948     "UNIQUE INDEX",
949     "VIEW",
950     "TRIGGER",
951     "VIRTUAL TABLE"
952  };
953  int i = 0;
954  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
955  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
956  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
957  sqlite3 *db = sqlite3_context_db_handle(pCtx);
958  UNUSED_PARAMETER(nVal);
959  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
960    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
961      int n = strlen30(aPrefix[i]);
962      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
963        char *z = 0;
964        char *zFake = 0;
965        if( zSchema ){
966          char cQuote = quoteChar(zSchema);
967          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
968            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
969          }else{
970            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
971          }
972        }
973        if( zName
974         && aPrefix[i][0]=='V'
975         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
976        ){
977          if( z==0 ){
978            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
979          }else{
980            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
981          }
982          free(zFake);
983        }
984        if( z ){
985          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
986          return;
987        }
988      }
989    }
990  }
991  sqlite3_result_value(pCtx, apVal[0]);
992}
993
994/*
995** The source code for several run-time loadable extensions is inserted
996** below by the ../tool/mkshellc.tcl script.  Before processing that included
997** code, we need to override some macros to make the included program code
998** work here in the middle of this regular program.
999*/
1000#define SQLITE_EXTENSION_INIT1
1001#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1002
1003#if defined(_WIN32) && defined(_MSC_VER)
1004INCLUDE test_windirent.h
1005INCLUDE test_windirent.c
1006#define dirent DIRENT
1007#endif
1008INCLUDE ../ext/misc/shathree.c
1009INCLUDE ../ext/misc/fileio.c
1010INCLUDE ../ext/misc/completion.c
1011INCLUDE ../ext/misc/appendvfs.c
1012INCLUDE ../ext/misc/memtrace.c
1013INCLUDE ../ext/misc/uint.c
1014INCLUDE ../ext/misc/decimal.c
1015INCLUDE ../ext/misc/ieee754.c
1016#ifdef SQLITE_HAVE_ZLIB
1017INCLUDE ../ext/misc/zipfile.c
1018INCLUDE ../ext/misc/sqlar.c
1019#endif
1020INCLUDE ../ext/expert/sqlite3expert.h
1021INCLUDE ../ext/expert/sqlite3expert.c
1022
1023#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1024INCLUDE ../ext/misc/dbdata.c
1025#endif
1026
1027#if defined(SQLITE_ENABLE_SESSION)
1028/*
1029** State information for a single open session
1030*/
1031typedef struct OpenSession OpenSession;
1032struct OpenSession {
1033  char *zName;             /* Symbolic name for this session */
1034  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1035  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1036  sqlite3_session *p;      /* The open session */
1037};
1038#endif
1039
1040typedef struct ExpertInfo ExpertInfo;
1041struct ExpertInfo {
1042  sqlite3expert *pExpert;
1043  int bVerbose;
1044};
1045
1046/* A single line in the EQP output */
1047typedef struct EQPGraphRow EQPGraphRow;
1048struct EQPGraphRow {
1049  int iEqpId;           /* ID for this row */
1050  int iParentId;        /* ID of the parent row */
1051  EQPGraphRow *pNext;   /* Next row in sequence */
1052  char zText[1];        /* Text to display for this row */
1053};
1054
1055/* All EQP output is collected into an instance of the following */
1056typedef struct EQPGraph EQPGraph;
1057struct EQPGraph {
1058  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1059  EQPGraphRow *pLast;   /* Last element of the pRow list */
1060  char zPrefix[100];    /* Graph prefix */
1061};
1062
1063/*
1064** State information about the database connection is contained in an
1065** instance of the following structure.
1066*/
1067typedef struct ShellState ShellState;
1068struct ShellState {
1069  sqlite3 *db;           /* The database */
1070  u8 autoExplain;        /* Automatically turn on .explain mode */
1071  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1072  u8 autoEQPtest;        /* autoEQP is in test mode */
1073  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1074  u8 statsOn;            /* True to display memory stats before each finalize */
1075  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1076  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1077  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1078  u8 nEqpLevel;          /* Depth of the EQP output graph */
1079  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1080  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1081  int outCount;          /* Revert to stdout when reaching zero */
1082  int cnt;               /* Number of records displayed so far */
1083  int lineno;            /* Line number of last line read from in */
1084  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1085  FILE *in;              /* Read commands from this stream */
1086  FILE *out;             /* Write results here */
1087  FILE *traceOut;        /* Output for sqlite3_trace() */
1088  int nErr;              /* Number of errors seen */
1089  int mode;              /* An output mode setting */
1090  int modePrior;         /* Saved mode */
1091  int cMode;             /* temporary output mode for the current query */
1092  int normalMode;        /* Output mode before ".explain on" */
1093  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1094  int showHeader;        /* True to show column names in List or Column mode */
1095  int nCheck;            /* Number of ".check" commands run */
1096  unsigned nProgress;    /* Number of progress callbacks encountered */
1097  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1098  unsigned flgProgress;  /* Flags for the progress callback */
1099  unsigned shellFlgs;    /* Various flags */
1100  unsigned priorShFlgs;  /* Saved copy of flags */
1101  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1102  char *zDestTable;      /* Name of destination table when MODE_Insert */
1103  char *zTempFile;       /* Temporary file that might need deleting */
1104  char zTestcase[30];    /* Name of current test case */
1105  char colSeparator[20]; /* Column separator character for several modes */
1106  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1107  char colSepPrior[20];  /* Saved column separator */
1108  char rowSepPrior[20];  /* Saved row separator */
1109  int *colWidth;         /* Requested width of each column in columnar modes */
1110  int *actualWidth;      /* Actual width of each column */
1111  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1112  char nullValue[20];    /* The text to print when a NULL comes back from
1113                         ** the database */
1114  char outfile[FILENAME_MAX]; /* Filename for *out */
1115  const char *zDbFilename;    /* name of the database file */
1116  char *zFreeOnClose;         /* Filename to free when closing */
1117  const char *zVfs;           /* Name of VFS to use */
1118  sqlite3_stmt *pStmt;   /* Current statement if any. */
1119  FILE *pLog;            /* Write log output here */
1120  int *aiIndent;         /* Array of indents used in MODE_Explain */
1121  int nIndent;           /* Size of array aiIndent[] */
1122  int iIndent;           /* Index of current op in aiIndent[] */
1123  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1124#if defined(SQLITE_ENABLE_SESSION)
1125  int nSession;             /* Number of active sessions */
1126  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1127#endif
1128  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1129};
1130
1131
1132/* Allowed values for ShellState.autoEQP
1133*/
1134#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1135#define AUTOEQP_on       1           /* Automatic EQP is on */
1136#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1137#define AUTOEQP_full     3           /* Show full EXPLAIN */
1138
1139/* Allowed values for ShellState.openMode
1140*/
1141#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1142#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1143#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1144#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1145#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1146#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1147#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1148
1149/* Allowed values for ShellState.eTraceType
1150*/
1151#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1152#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1153#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1154
1155/* Bits in the ShellState.flgProgress variable */
1156#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1157#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1158                                   ** callback limit is reached, and for each
1159                                   ** top-level SQL statement */
1160#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1161
1162/*
1163** These are the allowed shellFlgs values
1164*/
1165#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1166#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1167#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1168#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1169#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1170#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1171#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1172#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1173
1174/*
1175** Macros for testing and setting shellFlgs
1176*/
1177#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1178#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1179#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1180
1181/*
1182** These are the allowed modes.
1183*/
1184#define MODE_Line     0  /* One column per line.  Blank line between records */
1185#define MODE_Column   1  /* One record per line in neat columns */
1186#define MODE_List     2  /* One record per line with a separator */
1187#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1188#define MODE_Html     4  /* Generate an XHTML table */
1189#define MODE_Insert   5  /* Generate SQL "insert" statements */
1190#define MODE_Quote    6  /* Quote values as for SQL */
1191#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1192#define MODE_Csv      8  /* Quote strings, numbers are plain */
1193#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1194#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1195#define MODE_Pretty  11  /* Pretty-print schemas */
1196#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1197#define MODE_Json    13  /* Output JSON */
1198#define MODE_Markdown 14 /* Markdown formatting */
1199#define MODE_Table   15  /* MySQL-style table formatting */
1200#define MODE_Box     16  /* Unicode box-drawing characters */
1201
1202static const char *modeDescr[] = {
1203  "line",
1204  "column",
1205  "list",
1206  "semi",
1207  "html",
1208  "insert",
1209  "quote",
1210  "tcl",
1211  "csv",
1212  "explain",
1213  "ascii",
1214  "prettyprint",
1215  "eqp",
1216  "json",
1217  "markdown",
1218  "table",
1219  "box"
1220};
1221
1222/*
1223** These are the column/row/line separators used by the various
1224** import/export modes.
1225*/
1226#define SEP_Column    "|"
1227#define SEP_Row       "\n"
1228#define SEP_Tab       "\t"
1229#define SEP_Space     " "
1230#define SEP_Comma     ","
1231#define SEP_CrLf      "\r\n"
1232#define SEP_Unit      "\x1F"
1233#define SEP_Record    "\x1E"
1234
1235/*
1236** A callback for the sqlite3_log() interface.
1237*/
1238static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1239  ShellState *p = (ShellState*)pArg;
1240  if( p->pLog==0 ) return;
1241  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1242  fflush(p->pLog);
1243}
1244
1245/*
1246** SQL function:  shell_putsnl(X)
1247**
1248** Write the text X to the screen (or whatever output is being directed)
1249** adding a newline at the end, and then return X.
1250*/
1251static void shellPutsFunc(
1252  sqlite3_context *pCtx,
1253  int nVal,
1254  sqlite3_value **apVal
1255){
1256  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1257  (void)nVal;
1258  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1259  sqlite3_result_value(pCtx, apVal[0]);
1260}
1261
1262/*
1263** SQL function:   edit(VALUE)
1264**                 edit(VALUE,EDITOR)
1265**
1266** These steps:
1267**
1268**     (1) Write VALUE into a temporary file.
1269**     (2) Run program EDITOR on that temporary file.
1270**     (3) Read the temporary file back and return its content as the result.
1271**     (4) Delete the temporary file
1272**
1273** If the EDITOR argument is omitted, use the value in the VISUAL
1274** environment variable.  If still there is no EDITOR, through an error.
1275**
1276** Also throw an error if the EDITOR program returns a non-zero exit code.
1277*/
1278#ifndef SQLITE_NOHAVE_SYSTEM
1279static void editFunc(
1280  sqlite3_context *context,
1281  int argc,
1282  sqlite3_value **argv
1283){
1284  const char *zEditor;
1285  char *zTempFile = 0;
1286  sqlite3 *db;
1287  char *zCmd = 0;
1288  int bBin;
1289  int rc;
1290  int hasCRNL = 0;
1291  FILE *f = 0;
1292  sqlite3_int64 sz;
1293  sqlite3_int64 x;
1294  unsigned char *p = 0;
1295
1296  if( argc==2 ){
1297    zEditor = (const char*)sqlite3_value_text(argv[1]);
1298  }else{
1299    zEditor = getenv("VISUAL");
1300  }
1301  if( zEditor==0 ){
1302    sqlite3_result_error(context, "no editor for edit()", -1);
1303    return;
1304  }
1305  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1306    sqlite3_result_error(context, "NULL input to edit()", -1);
1307    return;
1308  }
1309  db = sqlite3_context_db_handle(context);
1310  zTempFile = 0;
1311  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1312  if( zTempFile==0 ){
1313    sqlite3_uint64 r = 0;
1314    sqlite3_randomness(sizeof(r), &r);
1315    zTempFile = sqlite3_mprintf("temp%llx", r);
1316    if( zTempFile==0 ){
1317      sqlite3_result_error_nomem(context);
1318      return;
1319    }
1320  }
1321  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1322  /* When writing the file to be edited, do \n to \r\n conversions on systems
1323  ** that want \r\n line endings */
1324  f = fopen(zTempFile, bBin ? "wb" : "w");
1325  if( f==0 ){
1326    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1327    goto edit_func_end;
1328  }
1329  sz = sqlite3_value_bytes(argv[0]);
1330  if( bBin ){
1331    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1332  }else{
1333    const char *z = (const char*)sqlite3_value_text(argv[0]);
1334    /* Remember whether or not the value originally contained \r\n */
1335    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1336    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1337  }
1338  fclose(f);
1339  f = 0;
1340  if( x!=sz ){
1341    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1342    goto edit_func_end;
1343  }
1344  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1345  if( zCmd==0 ){
1346    sqlite3_result_error_nomem(context);
1347    goto edit_func_end;
1348  }
1349  rc = system(zCmd);
1350  sqlite3_free(zCmd);
1351  if( rc ){
1352    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1353    goto edit_func_end;
1354  }
1355  f = fopen(zTempFile, "rb");
1356  if( f==0 ){
1357    sqlite3_result_error(context,
1358      "edit() cannot reopen temp file after edit", -1);
1359    goto edit_func_end;
1360  }
1361  fseek(f, 0, SEEK_END);
1362  sz = ftell(f);
1363  rewind(f);
1364  p = sqlite3_malloc64( sz+1 );
1365  if( p==0 ){
1366    sqlite3_result_error_nomem(context);
1367    goto edit_func_end;
1368  }
1369  x = fread(p, 1, (size_t)sz, f);
1370  fclose(f);
1371  f = 0;
1372  if( x!=sz ){
1373    sqlite3_result_error(context, "could not read back the whole file", -1);
1374    goto edit_func_end;
1375  }
1376  if( bBin ){
1377    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1378  }else{
1379    sqlite3_int64 i, j;
1380    if( hasCRNL ){
1381      /* If the original contains \r\n then do no conversions back to \n */
1382      j = sz;
1383    }else{
1384      /* If the file did not originally contain \r\n then convert any new
1385      ** \r\n back into \n */
1386      for(i=j=0; i<sz; i++){
1387        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1388        p[j++] = p[i];
1389      }
1390      sz = j;
1391      p[sz] = 0;
1392    }
1393    sqlite3_result_text64(context, (const char*)p, sz,
1394                          sqlite3_free, SQLITE_UTF8);
1395  }
1396  p = 0;
1397
1398edit_func_end:
1399  if( f ) fclose(f);
1400  unlink(zTempFile);
1401  sqlite3_free(zTempFile);
1402  sqlite3_free(p);
1403}
1404#endif /* SQLITE_NOHAVE_SYSTEM */
1405
1406/*
1407** Save or restore the current output mode
1408*/
1409static void outputModePush(ShellState *p){
1410  p->modePrior = p->mode;
1411  p->priorShFlgs = p->shellFlgs;
1412  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1413  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1414}
1415static void outputModePop(ShellState *p){
1416  p->mode = p->modePrior;
1417  p->shellFlgs = p->priorShFlgs;
1418  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1419  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1420}
1421
1422/*
1423** Output the given string as a hex-encoded blob (eg. X'1234' )
1424*/
1425static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1426  int i;
1427  char *zBlob = (char *)pBlob;
1428  raw_printf(out,"X'");
1429  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1430  raw_printf(out,"'");
1431}
1432
1433/*
1434** Find a string that is not found anywhere in z[].  Return a pointer
1435** to that string.
1436**
1437** Try to use zA and zB first.  If both of those are already found in z[]
1438** then make up some string and store it in the buffer zBuf.
1439*/
1440static const char *unused_string(
1441  const char *z,                    /* Result must not appear anywhere in z */
1442  const char *zA, const char *zB,   /* Try these first */
1443  char *zBuf                        /* Space to store a generated string */
1444){
1445  unsigned i = 0;
1446  if( strstr(z, zA)==0 ) return zA;
1447  if( strstr(z, zB)==0 ) return zB;
1448  do{
1449    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1450  }while( strstr(z,zBuf)!=0 );
1451  return zBuf;
1452}
1453
1454/*
1455** Output the given string as a quoted string using SQL quoting conventions.
1456**
1457** See also: output_quoted_escaped_string()
1458*/
1459static void output_quoted_string(FILE *out, const char *z){
1460  int i;
1461  char c;
1462  setBinaryMode(out, 1);
1463  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1464  if( c==0 ){
1465    utf8_printf(out,"'%s'",z);
1466  }else{
1467    raw_printf(out, "'");
1468    while( *z ){
1469      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1470      if( c=='\'' ) i++;
1471      if( i ){
1472        utf8_printf(out, "%.*s", i, z);
1473        z += i;
1474      }
1475      if( c=='\'' ){
1476        raw_printf(out, "'");
1477        continue;
1478      }
1479      if( c==0 ){
1480        break;
1481      }
1482      z++;
1483    }
1484    raw_printf(out, "'");
1485  }
1486  setTextMode(out, 1);
1487}
1488
1489/*
1490** Output the given string as a quoted string using SQL quoting conventions.
1491** Additionallly , escape the "\n" and "\r" characters so that they do not
1492** get corrupted by end-of-line translation facilities in some operating
1493** systems.
1494**
1495** This is like output_quoted_string() but with the addition of the \r\n
1496** escape mechanism.
1497*/
1498static void output_quoted_escaped_string(FILE *out, const char *z){
1499  int i;
1500  char c;
1501  setBinaryMode(out, 1);
1502  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1503  if( c==0 ){
1504    utf8_printf(out,"'%s'",z);
1505  }else{
1506    const char *zNL = 0;
1507    const char *zCR = 0;
1508    int nNL = 0;
1509    int nCR = 0;
1510    char zBuf1[20], zBuf2[20];
1511    for(i=0; z[i]; i++){
1512      if( z[i]=='\n' ) nNL++;
1513      if( z[i]=='\r' ) nCR++;
1514    }
1515    if( nNL ){
1516      raw_printf(out, "replace(");
1517      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1518    }
1519    if( nCR ){
1520      raw_printf(out, "replace(");
1521      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1522    }
1523    raw_printf(out, "'");
1524    while( *z ){
1525      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1526      if( c=='\'' ) i++;
1527      if( i ){
1528        utf8_printf(out, "%.*s", i, z);
1529        z += i;
1530      }
1531      if( c=='\'' ){
1532        raw_printf(out, "'");
1533        continue;
1534      }
1535      if( c==0 ){
1536        break;
1537      }
1538      z++;
1539      if( c=='\n' ){
1540        raw_printf(out, "%s", zNL);
1541        continue;
1542      }
1543      raw_printf(out, "%s", zCR);
1544    }
1545    raw_printf(out, "'");
1546    if( nCR ){
1547      raw_printf(out, ",'%s',char(13))", zCR);
1548    }
1549    if( nNL ){
1550      raw_printf(out, ",'%s',char(10))", zNL);
1551    }
1552  }
1553  setTextMode(out, 1);
1554}
1555
1556/*
1557** Output the given string as a quoted according to C or TCL quoting rules.
1558*/
1559static void output_c_string(FILE *out, const char *z){
1560  unsigned int c;
1561  fputc('"', out);
1562  while( (c = *(z++))!=0 ){
1563    if( c=='\\' ){
1564      fputc(c, out);
1565      fputc(c, out);
1566    }else if( c=='"' ){
1567      fputc('\\', out);
1568      fputc('"', out);
1569    }else if( c=='\t' ){
1570      fputc('\\', out);
1571      fputc('t', out);
1572    }else if( c=='\n' ){
1573      fputc('\\', out);
1574      fputc('n', out);
1575    }else if( c=='\r' ){
1576      fputc('\\', out);
1577      fputc('r', out);
1578    }else if( !isprint(c&0xff) ){
1579      raw_printf(out, "\\%03o", c&0xff);
1580    }else{
1581      fputc(c, out);
1582    }
1583  }
1584  fputc('"', out);
1585}
1586
1587/*
1588** Output the given string as a quoted according to JSON quoting rules.
1589*/
1590static void output_json_string(FILE *out, const char *z, int n){
1591  unsigned int c;
1592  if( n<0 ) n = (int)strlen(z);
1593  fputc('"', out);
1594  while( n-- ){
1595    c = *(z++);
1596    if( c=='\\' || c=='"' ){
1597      fputc('\\', out);
1598      fputc(c, out);
1599    }else if( c<=0x1f ){
1600      fputc('\\', out);
1601      if( c=='\b' ){
1602        fputc('b', out);
1603      }else if( c=='\f' ){
1604        fputc('f', out);
1605      }else if( c=='\n' ){
1606        fputc('n', out);
1607      }else if( c=='\r' ){
1608        fputc('r', out);
1609      }else if( c=='\t' ){
1610        fputc('t', out);
1611      }else{
1612         raw_printf(out, "u%04x",c);
1613      }
1614    }else{
1615      fputc(c, out);
1616    }
1617  }
1618  fputc('"', out);
1619}
1620
1621/*
1622** Output the given string with characters that are special to
1623** HTML escaped.
1624*/
1625static void output_html_string(FILE *out, const char *z){
1626  int i;
1627  if( z==0 ) z = "";
1628  while( *z ){
1629    for(i=0;   z[i]
1630            && z[i]!='<'
1631            && z[i]!='&'
1632            && z[i]!='>'
1633            && z[i]!='\"'
1634            && z[i]!='\'';
1635        i++){}
1636    if( i>0 ){
1637      utf8_printf(out,"%.*s",i,z);
1638    }
1639    if( z[i]=='<' ){
1640      raw_printf(out,"&lt;");
1641    }else if( z[i]=='&' ){
1642      raw_printf(out,"&amp;");
1643    }else if( z[i]=='>' ){
1644      raw_printf(out,"&gt;");
1645    }else if( z[i]=='\"' ){
1646      raw_printf(out,"&quot;");
1647    }else if( z[i]=='\'' ){
1648      raw_printf(out,"&#39;");
1649    }else{
1650      break;
1651    }
1652    z += i + 1;
1653  }
1654}
1655
1656/*
1657** If a field contains any character identified by a 1 in the following
1658** array, then the string must be quoted for CSV.
1659*/
1660static const char needCsvQuote[] = {
1661  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1662  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1663  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1664  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1665  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1666  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1667  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1668  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1669  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1670  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1671  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1672  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1673  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1674  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1675  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1676  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1677};
1678
1679/*
1680** Output a single term of CSV.  Actually, p->colSeparator is used for
1681** the separator, which may or may not be a comma.  p->nullValue is
1682** the null value.  Strings are quoted if necessary.  The separator
1683** is only issued if bSep is true.
1684*/
1685static void output_csv(ShellState *p, const char *z, int bSep){
1686  FILE *out = p->out;
1687  if( z==0 ){
1688    utf8_printf(out,"%s",p->nullValue);
1689  }else{
1690    int i;
1691    int nSep = strlen30(p->colSeparator);
1692    for(i=0; z[i]; i++){
1693      if( needCsvQuote[((unsigned char*)z)[i]]
1694         || (z[i]==p->colSeparator[0] &&
1695             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1696        i = 0;
1697        break;
1698      }
1699    }
1700    if( i==0 ){
1701      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1702      utf8_printf(out, "%s", zQuoted);
1703      sqlite3_free(zQuoted);
1704    }else{
1705      utf8_printf(out, "%s", z);
1706    }
1707  }
1708  if( bSep ){
1709    utf8_printf(p->out, "%s", p->colSeparator);
1710  }
1711}
1712
1713/*
1714** This routine runs when the user presses Ctrl-C
1715*/
1716static void interrupt_handler(int NotUsed){
1717  UNUSED_PARAMETER(NotUsed);
1718  seenInterrupt++;
1719  if( seenInterrupt>2 ) exit(1);
1720  if( globalDb ) sqlite3_interrupt(globalDb);
1721}
1722
1723#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1724/*
1725** This routine runs for console events (e.g. Ctrl-C) on Win32
1726*/
1727static BOOL WINAPI ConsoleCtrlHandler(
1728  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1729){
1730  if( dwCtrlType==CTRL_C_EVENT ){
1731    interrupt_handler(0);
1732    return TRUE;
1733  }
1734  return FALSE;
1735}
1736#endif
1737
1738#ifndef SQLITE_OMIT_AUTHORIZATION
1739/*
1740** When the ".auth ON" is set, the following authorizer callback is
1741** invoked.  It always returns SQLITE_OK.
1742*/
1743static int shellAuth(
1744  void *pClientData,
1745  int op,
1746  const char *zA1,
1747  const char *zA2,
1748  const char *zA3,
1749  const char *zA4
1750){
1751  ShellState *p = (ShellState*)pClientData;
1752  static const char *azAction[] = { 0,
1753     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1754     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1755     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1756     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1757     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1758     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1759     "PRAGMA",               "READ",                 "SELECT",
1760     "TRANSACTION",          "UPDATE",               "ATTACH",
1761     "DETACH",               "ALTER_TABLE",          "REINDEX",
1762     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1763     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1764  };
1765  int i;
1766  const char *az[4];
1767  az[0] = zA1;
1768  az[1] = zA2;
1769  az[2] = zA3;
1770  az[3] = zA4;
1771  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1772  for(i=0; i<4; i++){
1773    raw_printf(p->out, " ");
1774    if( az[i] ){
1775      output_c_string(p->out, az[i]);
1776    }else{
1777      raw_printf(p->out, "NULL");
1778    }
1779  }
1780  raw_printf(p->out, "\n");
1781  return SQLITE_OK;
1782}
1783#endif
1784
1785/*
1786** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1787**
1788** This routine converts some CREATE TABLE statements for shadow tables
1789** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1790*/
1791static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1792  if( z==0 ) return;
1793  if( zTail==0 ) return;
1794  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1795    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1796  }else{
1797    utf8_printf(out, "%s%s", z, zTail);
1798  }
1799}
1800static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1801  char c = z[n];
1802  z[n] = 0;
1803  printSchemaLine(out, z, zTail);
1804  z[n] = c;
1805}
1806
1807/*
1808** Return true if string z[] has nothing but whitespace and comments to the
1809** end of the first line.
1810*/
1811static int wsToEol(const char *z){
1812  int i;
1813  for(i=0; z[i]; i++){
1814    if( z[i]=='\n' ) return 1;
1815    if( IsSpace(z[i]) ) continue;
1816    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1817    return 0;
1818  }
1819  return 1;
1820}
1821
1822/*
1823** Add a new entry to the EXPLAIN QUERY PLAN data
1824*/
1825static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1826  EQPGraphRow *pNew;
1827  int nText = strlen30(zText);
1828  if( p->autoEQPtest ){
1829    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1830  }
1831  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1832  if( pNew==0 ) shell_out_of_memory();
1833  pNew->iEqpId = iEqpId;
1834  pNew->iParentId = p2;
1835  memcpy(pNew->zText, zText, nText+1);
1836  pNew->pNext = 0;
1837  if( p->sGraph.pLast ){
1838    p->sGraph.pLast->pNext = pNew;
1839  }else{
1840    p->sGraph.pRow = pNew;
1841  }
1842  p->sGraph.pLast = pNew;
1843}
1844
1845/*
1846** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1847** in p->sGraph.
1848*/
1849static void eqp_reset(ShellState *p){
1850  EQPGraphRow *pRow, *pNext;
1851  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1852    pNext = pRow->pNext;
1853    sqlite3_free(pRow);
1854  }
1855  memset(&p->sGraph, 0, sizeof(p->sGraph));
1856}
1857
1858/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1859** pOld, or return the first such line if pOld is NULL
1860*/
1861static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1862  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1863  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1864  return pRow;
1865}
1866
1867/* Render a single level of the graph that has iEqpId as its parent.  Called
1868** recursively to render sublevels.
1869*/
1870static void eqp_render_level(ShellState *p, int iEqpId){
1871  EQPGraphRow *pRow, *pNext;
1872  int n = strlen30(p->sGraph.zPrefix);
1873  char *z;
1874  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1875    pNext = eqp_next_row(p, iEqpId, pRow);
1876    z = pRow->zText;
1877    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1878                pNext ? "|--" : "`--", z);
1879    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1880      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1881      eqp_render_level(p, pRow->iEqpId);
1882      p->sGraph.zPrefix[n] = 0;
1883    }
1884  }
1885}
1886
1887/*
1888** Display and reset the EXPLAIN QUERY PLAN data
1889*/
1890static void eqp_render(ShellState *p){
1891  EQPGraphRow *pRow = p->sGraph.pRow;
1892  if( pRow ){
1893    if( pRow->zText[0]=='-' ){
1894      if( pRow->pNext==0 ){
1895        eqp_reset(p);
1896        return;
1897      }
1898      utf8_printf(p->out, "%s\n", pRow->zText+3);
1899      p->sGraph.pRow = pRow->pNext;
1900      sqlite3_free(pRow);
1901    }else{
1902      utf8_printf(p->out, "QUERY PLAN\n");
1903    }
1904    p->sGraph.zPrefix[0] = 0;
1905    eqp_render_level(p, 0);
1906    eqp_reset(p);
1907  }
1908}
1909
1910#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1911/*
1912** Progress handler callback.
1913*/
1914static int progress_handler(void *pClientData) {
1915  ShellState *p = (ShellState*)pClientData;
1916  p->nProgress++;
1917  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1918    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1919    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1920    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1921    return 1;
1922  }
1923  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1924    raw_printf(p->out, "Progress %u\n", p->nProgress);
1925  }
1926  return 0;
1927}
1928#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1929
1930/*
1931** Print N dashes
1932*/
1933static void print_dashes(FILE *out, int N){
1934  const char zDash[] = "--------------------------------------------------";
1935  const int nDash = sizeof(zDash) - 1;
1936  while( N>nDash ){
1937    fputs(zDash, out);
1938    N -= nDash;
1939  }
1940  raw_printf(out, "%.*s", N, zDash);
1941}
1942
1943/*
1944** Print a markdown or table-style row separator using ascii-art
1945*/
1946static void print_row_separator(
1947  ShellState *p,
1948  int nArg,
1949  const char *zSep
1950){
1951  int i;
1952  if( nArg>0 ){
1953    fputs(zSep, p->out);
1954    print_dashes(p->out, p->actualWidth[0]+2);
1955    for(i=1; i<nArg; i++){
1956      fputs(zSep, p->out);
1957      print_dashes(p->out, p->actualWidth[i]+2);
1958    }
1959    fputs(zSep, p->out);
1960  }
1961  fputs("\n", p->out);
1962}
1963
1964/*
1965** This is the callback routine that the shell
1966** invokes for each row of a query result.
1967*/
1968static int shell_callback(
1969  void *pArg,
1970  int nArg,        /* Number of result columns */
1971  char **azArg,    /* Text of each result column */
1972  char **azCol,    /* Column names */
1973  int *aiType      /* Column types.  Might be NULL */
1974){
1975  int i;
1976  ShellState *p = (ShellState*)pArg;
1977
1978  if( azArg==0 ) return 0;
1979  switch( p->cMode ){
1980    case MODE_Line: {
1981      int w = 5;
1982      if( azArg==0 ) break;
1983      for(i=0; i<nArg; i++){
1984        int len = strlen30(azCol[i] ? azCol[i] : "");
1985        if( len>w ) w = len;
1986      }
1987      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
1988      for(i=0; i<nArg; i++){
1989        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
1990                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1991      }
1992      break;
1993    }
1994    case MODE_Explain: {
1995      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
1996      if( nArg>ArraySize(aExplainWidth) ){
1997        nArg = ArraySize(aExplainWidth);
1998      }
1999      if( p->cnt++==0 ){
2000        for(i=0; i<nArg; i++){
2001          int w = aExplainWidth[i];
2002          utf8_width_print(p->out, w, azCol[i]);
2003          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2004        }
2005        for(i=0; i<nArg; i++){
2006          int w = aExplainWidth[i];
2007          print_dashes(p->out, w);
2008          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2009        }
2010      }
2011      if( azArg==0 ) break;
2012      for(i=0; i<nArg; i++){
2013        int w = aExplainWidth[i];
2014        if( azArg[i] && strlenChar(azArg[i])>w ){
2015          w = strlenChar(azArg[i]);
2016        }
2017        if( i==1 && p->aiIndent && p->pStmt ){
2018          if( p->iIndent<p->nIndent ){
2019            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2020          }
2021          p->iIndent++;
2022        }
2023        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2024        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2025      }
2026      break;
2027    }
2028    case MODE_Semi: {   /* .schema and .fullschema output */
2029      printSchemaLine(p->out, azArg[0], ";\n");
2030      break;
2031    }
2032    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2033      char *z;
2034      int j;
2035      int nParen = 0;
2036      char cEnd = 0;
2037      char c;
2038      int nLine = 0;
2039      assert( nArg==1 );
2040      if( azArg[0]==0 ) break;
2041      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2042       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2043      ){
2044        utf8_printf(p->out, "%s;\n", azArg[0]);
2045        break;
2046      }
2047      z = sqlite3_mprintf("%s", azArg[0]);
2048      j = 0;
2049      for(i=0; IsSpace(z[i]); i++){}
2050      for(; (c = z[i])!=0; i++){
2051        if( IsSpace(c) ){
2052          if( z[j-1]=='\r' ) z[j-1] = '\n';
2053          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2054        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2055          j--;
2056        }
2057        z[j++] = c;
2058      }
2059      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2060      z[j] = 0;
2061      if( strlen30(z)>=79 ){
2062        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2063          if( c==cEnd ){
2064            cEnd = 0;
2065          }else if( c=='"' || c=='\'' || c=='`' ){
2066            cEnd = c;
2067          }else if( c=='[' ){
2068            cEnd = ']';
2069          }else if( c=='-' && z[i+1]=='-' ){
2070            cEnd = '\n';
2071          }else if( c=='(' ){
2072            nParen++;
2073          }else if( c==')' ){
2074            nParen--;
2075            if( nLine>0 && nParen==0 && j>0 ){
2076              printSchemaLineN(p->out, z, j, "\n");
2077              j = 0;
2078            }
2079          }
2080          z[j++] = c;
2081          if( nParen==1 && cEnd==0
2082           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2083          ){
2084            if( c=='\n' ) j--;
2085            printSchemaLineN(p->out, z, j, "\n  ");
2086            j = 0;
2087            nLine++;
2088            while( IsSpace(z[i+1]) ){ i++; }
2089          }
2090        }
2091        z[j] = 0;
2092      }
2093      printSchemaLine(p->out, z, ";\n");
2094      sqlite3_free(z);
2095      break;
2096    }
2097    case MODE_List: {
2098      if( p->cnt++==0 && p->showHeader ){
2099        for(i=0; i<nArg; i++){
2100          utf8_printf(p->out,"%s%s",azCol[i],
2101                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2102        }
2103      }
2104      if( azArg==0 ) break;
2105      for(i=0; i<nArg; i++){
2106        char *z = azArg[i];
2107        if( z==0 ) z = p->nullValue;
2108        utf8_printf(p->out, "%s", z);
2109        if( i<nArg-1 ){
2110          utf8_printf(p->out, "%s", p->colSeparator);
2111        }else{
2112          utf8_printf(p->out, "%s", p->rowSeparator);
2113        }
2114      }
2115      break;
2116    }
2117    case MODE_Html: {
2118      if( p->cnt++==0 && p->showHeader ){
2119        raw_printf(p->out,"<TR>");
2120        for(i=0; i<nArg; i++){
2121          raw_printf(p->out,"<TH>");
2122          output_html_string(p->out, azCol[i]);
2123          raw_printf(p->out,"</TH>\n");
2124        }
2125        raw_printf(p->out,"</TR>\n");
2126      }
2127      if( azArg==0 ) break;
2128      raw_printf(p->out,"<TR>");
2129      for(i=0; i<nArg; i++){
2130        raw_printf(p->out,"<TD>");
2131        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2132        raw_printf(p->out,"</TD>\n");
2133      }
2134      raw_printf(p->out,"</TR>\n");
2135      break;
2136    }
2137    case MODE_Tcl: {
2138      if( p->cnt++==0 && p->showHeader ){
2139        for(i=0; i<nArg; i++){
2140          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2141          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2142        }
2143        utf8_printf(p->out, "%s", p->rowSeparator);
2144      }
2145      if( azArg==0 ) break;
2146      for(i=0; i<nArg; i++){
2147        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2148        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2149      }
2150      utf8_printf(p->out, "%s", p->rowSeparator);
2151      break;
2152    }
2153    case MODE_Csv: {
2154      setBinaryMode(p->out, 1);
2155      if( p->cnt++==0 && p->showHeader ){
2156        for(i=0; i<nArg; i++){
2157          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2158        }
2159        utf8_printf(p->out, "%s", p->rowSeparator);
2160      }
2161      if( nArg>0 ){
2162        for(i=0; i<nArg; i++){
2163          output_csv(p, azArg[i], i<nArg-1);
2164        }
2165        utf8_printf(p->out, "%s", p->rowSeparator);
2166      }
2167      setTextMode(p->out, 1);
2168      break;
2169    }
2170    case MODE_Insert: {
2171      if( azArg==0 ) break;
2172      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2173      if( p->showHeader ){
2174        raw_printf(p->out,"(");
2175        for(i=0; i<nArg; i++){
2176          if( i>0 ) raw_printf(p->out, ",");
2177          if( quoteChar(azCol[i]) ){
2178            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2179            utf8_printf(p->out, "%s", z);
2180            sqlite3_free(z);
2181          }else{
2182            raw_printf(p->out, "%s", azCol[i]);
2183          }
2184        }
2185        raw_printf(p->out,")");
2186      }
2187      p->cnt++;
2188      for(i=0; i<nArg; i++){
2189        raw_printf(p->out, i>0 ? "," : " VALUES(");
2190        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2191          utf8_printf(p->out,"NULL");
2192        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2193          if( ShellHasFlag(p, SHFLG_Newlines) ){
2194            output_quoted_string(p->out, azArg[i]);
2195          }else{
2196            output_quoted_escaped_string(p->out, azArg[i]);
2197          }
2198        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2199          utf8_printf(p->out,"%s", azArg[i]);
2200        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2201          char z[50];
2202          double r = sqlite3_column_double(p->pStmt, i);
2203          sqlite3_uint64 ur;
2204          memcpy(&ur,&r,sizeof(r));
2205          if( ur==0x7ff0000000000000LL ){
2206            raw_printf(p->out, "1e999");
2207          }else if( ur==0xfff0000000000000LL ){
2208            raw_printf(p->out, "-1e999");
2209          }else{
2210            sqlite3_snprintf(50,z,"%!.20g", r);
2211            raw_printf(p->out, "%s", z);
2212          }
2213        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2214          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2215          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2216          output_hex_blob(p->out, pBlob, nBlob);
2217        }else if( isNumber(azArg[i], 0) ){
2218          utf8_printf(p->out,"%s", azArg[i]);
2219        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2220          output_quoted_string(p->out, azArg[i]);
2221        }else{
2222          output_quoted_escaped_string(p->out, azArg[i]);
2223        }
2224      }
2225      raw_printf(p->out,");\n");
2226      break;
2227    }
2228    case MODE_Json: {
2229      if( azArg==0 ) break;
2230      if( p->cnt==0 ){
2231        fputs("[{", p->out);
2232      }else{
2233        fputs(",\n{", p->out);
2234      }
2235      p->cnt++;
2236      for(i=0; i<nArg; i++){
2237        output_json_string(p->out, azCol[i], -1);
2238        putc(':', p->out);
2239        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2240          fputs("null",p->out);
2241        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2242          char z[50];
2243          double r = sqlite3_column_double(p->pStmt, i);
2244          sqlite3_uint64 ur;
2245          memcpy(&ur,&r,sizeof(r));
2246          if( ur==0x7ff0000000000000LL ){
2247            raw_printf(p->out, "1e999");
2248          }else if( ur==0xfff0000000000000LL ){
2249            raw_printf(p->out, "-1e999");
2250          }else{
2251            sqlite3_snprintf(50,z,"%!.20g", r);
2252            raw_printf(p->out, "%s", z);
2253          }
2254        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2255          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2256          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2257          output_json_string(p->out, pBlob, nBlob);
2258        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2259          output_json_string(p->out, azArg[i], -1);
2260        }else{
2261          utf8_printf(p->out,"%s", azArg[i]);
2262        }
2263        if( i<nArg-1 ){
2264          putc(',', p->out);
2265        }
2266      }
2267      putc('}', p->out);
2268      break;
2269    }
2270    case MODE_Quote: {
2271      if( azArg==0 ) break;
2272      if( p->cnt==0 && p->showHeader ){
2273        for(i=0; i<nArg; i++){
2274          if( i>0 ) fputs(p->colSeparator, p->out);
2275          output_quoted_string(p->out, azCol[i]);
2276        }
2277        fputs(p->rowSeparator, p->out);
2278      }
2279      p->cnt++;
2280      for(i=0; i<nArg; i++){
2281        if( i>0 ) fputs(p->colSeparator, p->out);
2282        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2283          utf8_printf(p->out,"NULL");
2284        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2285          output_quoted_string(p->out, azArg[i]);
2286        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2287          utf8_printf(p->out,"%s", azArg[i]);
2288        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2289          char z[50];
2290          double r = sqlite3_column_double(p->pStmt, i);
2291          sqlite3_snprintf(50,z,"%!.20g", r);
2292          raw_printf(p->out, "%s", z);
2293        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2294          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2295          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2296          output_hex_blob(p->out, pBlob, nBlob);
2297        }else if( isNumber(azArg[i], 0) ){
2298          utf8_printf(p->out,"%s", azArg[i]);
2299        }else{
2300          output_quoted_string(p->out, azArg[i]);
2301        }
2302      }
2303      fputs(p->rowSeparator, p->out);
2304      break;
2305    }
2306    case MODE_Ascii: {
2307      if( p->cnt++==0 && p->showHeader ){
2308        for(i=0; i<nArg; i++){
2309          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2310          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2311        }
2312        utf8_printf(p->out, "%s", p->rowSeparator);
2313      }
2314      if( azArg==0 ) break;
2315      for(i=0; i<nArg; i++){
2316        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2317        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2318      }
2319      utf8_printf(p->out, "%s", p->rowSeparator);
2320      break;
2321    }
2322    case MODE_EQP: {
2323      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2324      break;
2325    }
2326  }
2327  return 0;
2328}
2329
2330/*
2331** This is the callback routine that the SQLite library
2332** invokes for each row of a query result.
2333*/
2334static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2335  /* since we don't have type info, call the shell_callback with a NULL value */
2336  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2337}
2338
2339/*
2340** This is the callback routine from sqlite3_exec() that appends all
2341** output onto the end of a ShellText object.
2342*/
2343static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2344  ShellText *p = (ShellText*)pArg;
2345  int i;
2346  UNUSED_PARAMETER(az);
2347  if( azArg==0 ) return 0;
2348  if( p->n ) appendText(p, "|", 0);
2349  for(i=0; i<nArg; i++){
2350    if( i ) appendText(p, ",", 0);
2351    if( azArg[i] ) appendText(p, azArg[i], 0);
2352  }
2353  return 0;
2354}
2355
2356/*
2357** Generate an appropriate SELFTEST table in the main database.
2358*/
2359static void createSelftestTable(ShellState *p){
2360  char *zErrMsg = 0;
2361  sqlite3_exec(p->db,
2362    "SAVEPOINT selftest_init;\n"
2363    "CREATE TABLE IF NOT EXISTS selftest(\n"
2364    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2365    "  op TEXT,\n"                   /* Operator:  memo run */
2366    "  cmd TEXT,\n"                  /* Command text */
2367    "  ans TEXT\n"                   /* Desired answer */
2368    ");"
2369    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2370    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2371    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2372    "         'memo','Tests generated by --init');\n"
2373    "INSERT INTO [_shell$self]\n"
2374    "  SELECT 'run',\n"
2375    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2376                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2377    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2378                          "FROM sqlite_schema ORDER BY 2',224));\n"
2379    "INSERT INTO [_shell$self]\n"
2380    "  SELECT 'run',"
2381    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2382    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2383    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2384    "  FROM (\n"
2385    "    SELECT name FROM sqlite_schema\n"
2386    "     WHERE type='table'\n"
2387    "       AND name<>'selftest'\n"
2388    "       AND coalesce(rootpage,0)>0\n"
2389    "  )\n"
2390    " ORDER BY name;\n"
2391    "INSERT INTO [_shell$self]\n"
2392    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2393    "INSERT INTO selftest(tno,op,cmd,ans)"
2394    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2395    "DROP TABLE [_shell$self];"
2396    ,0,0,&zErrMsg);
2397  if( zErrMsg ){
2398    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2399    sqlite3_free(zErrMsg);
2400  }
2401  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2402}
2403
2404
2405/*
2406** Set the destination table field of the ShellState structure to
2407** the name of the table given.  Escape any quote characters in the
2408** table name.
2409*/
2410static void set_table_name(ShellState *p, const char *zName){
2411  int i, n;
2412  char cQuote;
2413  char *z;
2414
2415  if( p->zDestTable ){
2416    free(p->zDestTable);
2417    p->zDestTable = 0;
2418  }
2419  if( zName==0 ) return;
2420  cQuote = quoteChar(zName);
2421  n = strlen30(zName);
2422  if( cQuote ) n += n+2;
2423  z = p->zDestTable = malloc( n+1 );
2424  if( z==0 ) shell_out_of_memory();
2425  n = 0;
2426  if( cQuote ) z[n++] = cQuote;
2427  for(i=0; zName[i]; i++){
2428    z[n++] = zName[i];
2429    if( zName[i]==cQuote ) z[n++] = cQuote;
2430  }
2431  if( cQuote ) z[n++] = cQuote;
2432  z[n] = 0;
2433}
2434
2435
2436/*
2437** Execute a query statement that will generate SQL output.  Print
2438** the result columns, comma-separated, on a line and then add a
2439** semicolon terminator to the end of that line.
2440**
2441** If the number of columns is 1 and that column contains text "--"
2442** then write the semicolon on a separate line.  That way, if a
2443** "--" comment occurs at the end of the statement, the comment
2444** won't consume the semicolon terminator.
2445*/
2446static int run_table_dump_query(
2447  ShellState *p,           /* Query context */
2448  const char *zSelect      /* SELECT statement to extract content */
2449){
2450  sqlite3_stmt *pSelect;
2451  int rc;
2452  int nResult;
2453  int i;
2454  const char *z;
2455  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2456  if( rc!=SQLITE_OK || !pSelect ){
2457    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2458                sqlite3_errmsg(p->db));
2459    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2460    return rc;
2461  }
2462  rc = sqlite3_step(pSelect);
2463  nResult = sqlite3_column_count(pSelect);
2464  while( rc==SQLITE_ROW ){
2465    z = (const char*)sqlite3_column_text(pSelect, 0);
2466    utf8_printf(p->out, "%s", z);
2467    for(i=1; i<nResult; i++){
2468      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2469    }
2470    if( z==0 ) z = "";
2471    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2472    if( z[0] ){
2473      raw_printf(p->out, "\n;\n");
2474    }else{
2475      raw_printf(p->out, ";\n");
2476    }
2477    rc = sqlite3_step(pSelect);
2478  }
2479  rc = sqlite3_finalize(pSelect);
2480  if( rc!=SQLITE_OK ){
2481    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2482                sqlite3_errmsg(p->db));
2483    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2484  }
2485  return rc;
2486}
2487
2488/*
2489** Allocate space and save off current error string.
2490*/
2491static char *save_err_msg(
2492  sqlite3 *db            /* Database to query */
2493){
2494  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2495  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2496  if( zErrMsg ){
2497    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2498  }
2499  return zErrMsg;
2500}
2501
2502#ifdef __linux__
2503/*
2504** Attempt to display I/O stats on Linux using /proc/PID/io
2505*/
2506static void displayLinuxIoStats(FILE *out){
2507  FILE *in;
2508  char z[200];
2509  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2510  in = fopen(z, "rb");
2511  if( in==0 ) return;
2512  while( fgets(z, sizeof(z), in)!=0 ){
2513    static const struct {
2514      const char *zPattern;
2515      const char *zDesc;
2516    } aTrans[] = {
2517      { "rchar: ",                  "Bytes received by read():" },
2518      { "wchar: ",                  "Bytes sent to write():"    },
2519      { "syscr: ",                  "Read() system calls:"      },
2520      { "syscw: ",                  "Write() system calls:"     },
2521      { "read_bytes: ",             "Bytes read from storage:"  },
2522      { "write_bytes: ",            "Bytes written to storage:" },
2523      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2524    };
2525    int i;
2526    for(i=0; i<ArraySize(aTrans); i++){
2527      int n = strlen30(aTrans[i].zPattern);
2528      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2529        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2530        break;
2531      }
2532    }
2533  }
2534  fclose(in);
2535}
2536#endif
2537
2538/*
2539** Display a single line of status using 64-bit values.
2540*/
2541static void displayStatLine(
2542  ShellState *p,            /* The shell context */
2543  char *zLabel,             /* Label for this one line */
2544  char *zFormat,            /* Format for the result */
2545  int iStatusCtrl,          /* Which status to display */
2546  int bReset                /* True to reset the stats */
2547){
2548  sqlite3_int64 iCur = -1;
2549  sqlite3_int64 iHiwtr = -1;
2550  int i, nPercent;
2551  char zLine[200];
2552  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2553  for(i=0, nPercent=0; zFormat[i]; i++){
2554    if( zFormat[i]=='%' ) nPercent++;
2555  }
2556  if( nPercent>1 ){
2557    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2558  }else{
2559    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2560  }
2561  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2562}
2563
2564/*
2565** Display memory stats.
2566*/
2567static int display_stats(
2568  sqlite3 *db,                /* Database to query */
2569  ShellState *pArg,           /* Pointer to ShellState */
2570  int bReset                  /* True to reset the stats */
2571){
2572  int iCur;
2573  int iHiwtr;
2574  FILE *out;
2575  if( pArg==0 || pArg->out==0 ) return 0;
2576  out = pArg->out;
2577
2578  if( pArg->pStmt && (pArg->statsOn & 2) ){
2579    int nCol, i, x;
2580    sqlite3_stmt *pStmt = pArg->pStmt;
2581    char z[100];
2582    nCol = sqlite3_column_count(pStmt);
2583    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2584    for(i=0; i<nCol; i++){
2585      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2586      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2587#ifndef SQLITE_OMIT_DECLTYPE
2588      sqlite3_snprintf(30, z+x, "declared type:");
2589      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2590#endif
2591#ifdef SQLITE_ENABLE_COLUMN_METADATA
2592      sqlite3_snprintf(30, z+x, "database name:");
2593      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2594      sqlite3_snprintf(30, z+x, "table name:");
2595      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2596      sqlite3_snprintf(30, z+x, "origin name:");
2597      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2598#endif
2599    }
2600  }
2601
2602  displayStatLine(pArg, "Memory Used:",
2603     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2604  displayStatLine(pArg, "Number of Outstanding Allocations:",
2605     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2606  if( pArg->shellFlgs & SHFLG_Pagecache ){
2607    displayStatLine(pArg, "Number of Pcache Pages Used:",
2608       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2609  }
2610  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2611     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2612  displayStatLine(pArg, "Largest Allocation:",
2613     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2614  displayStatLine(pArg, "Largest Pcache Allocation:",
2615     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2616#ifdef YYTRACKMAXSTACKDEPTH
2617  displayStatLine(pArg, "Deepest Parser Stack:",
2618     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2619#endif
2620
2621  if( db ){
2622    if( pArg->shellFlgs & SHFLG_Lookaside ){
2623      iHiwtr = iCur = -1;
2624      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2625                        &iCur, &iHiwtr, bReset);
2626      raw_printf(pArg->out,
2627              "Lookaside Slots Used:                %d (max %d)\n",
2628              iCur, iHiwtr);
2629      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2630                        &iCur, &iHiwtr, bReset);
2631      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2632              iHiwtr);
2633      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2634                        &iCur, &iHiwtr, bReset);
2635      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2636              iHiwtr);
2637      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2638                        &iCur, &iHiwtr, bReset);
2639      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2640              iHiwtr);
2641    }
2642    iHiwtr = iCur = -1;
2643    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2644    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2645            iCur);
2646    iHiwtr = iCur = -1;
2647    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2648    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2649    iHiwtr = iCur = -1;
2650    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2651    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2652    iHiwtr = iCur = -1;
2653    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2654    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2655    iHiwtr = iCur = -1;
2656    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2657    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2658    iHiwtr = iCur = -1;
2659    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2660    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2661            iCur);
2662    iHiwtr = iCur = -1;
2663    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2664    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2665            iCur);
2666  }
2667
2668  if( pArg->pStmt ){
2669    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2670                               bReset);
2671    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2672    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2673    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2674    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2675    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2676    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2677    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2678    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2679    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2680    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2681    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2682    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2683    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2684  }
2685
2686#ifdef __linux__
2687  displayLinuxIoStats(pArg->out);
2688#endif
2689
2690  /* Do not remove this machine readable comment: extra-stats-output-here */
2691
2692  return 0;
2693}
2694
2695/*
2696** Display scan stats.
2697*/
2698static void display_scanstats(
2699  sqlite3 *db,                    /* Database to query */
2700  ShellState *pArg                /* Pointer to ShellState */
2701){
2702#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2703  UNUSED_PARAMETER(db);
2704  UNUSED_PARAMETER(pArg);
2705#else
2706  int i, k, n, mx;
2707  raw_printf(pArg->out, "-------- scanstats --------\n");
2708  mx = 0;
2709  for(k=0; k<=mx; k++){
2710    double rEstLoop = 1.0;
2711    for(i=n=0; 1; i++){
2712      sqlite3_stmt *p = pArg->pStmt;
2713      sqlite3_int64 nLoop, nVisit;
2714      double rEst;
2715      int iSid;
2716      const char *zExplain;
2717      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2718        break;
2719      }
2720      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2721      if( iSid>mx ) mx = iSid;
2722      if( iSid!=k ) continue;
2723      if( n==0 ){
2724        rEstLoop = (double)nLoop;
2725        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2726      }
2727      n++;
2728      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2729      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2730      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2731      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2732      rEstLoop *= rEst;
2733      raw_printf(pArg->out,
2734          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2735          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2736      );
2737    }
2738  }
2739  raw_printf(pArg->out, "---------------------------\n");
2740#endif
2741}
2742
2743/*
2744** Parameter azArray points to a zero-terminated array of strings. zStr
2745** points to a single nul-terminated string. Return non-zero if zStr
2746** is equal, according to strcmp(), to any of the strings in the array.
2747** Otherwise, return zero.
2748*/
2749static int str_in_array(const char *zStr, const char **azArray){
2750  int i;
2751  for(i=0; azArray[i]; i++){
2752    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2753  }
2754  return 0;
2755}
2756
2757/*
2758** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2759** and populate the ShellState.aiIndent[] array with the number of
2760** spaces each opcode should be indented before it is output.
2761**
2762** The indenting rules are:
2763**
2764**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2765**       all opcodes that occur between the p2 jump destination and the opcode
2766**       itself by 2 spaces.
2767**
2768**     * For each "Goto", if the jump destination is earlier in the program
2769**       and ends on one of:
2770**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2771**       or if the P1 parameter is one instead of zero,
2772**       then indent all opcodes between the earlier instruction
2773**       and "Goto" by 2 spaces.
2774*/
2775static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2776  const char *zSql;               /* The text of the SQL statement */
2777  const char *z;                  /* Used to check if this is an EXPLAIN */
2778  int *abYield = 0;               /* True if op is an OP_Yield */
2779  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2780  int iOp;                        /* Index of operation in p->aiIndent[] */
2781
2782  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2783  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2784                            "Rewind", 0 };
2785  const char *azGoto[] = { "Goto", 0 };
2786
2787  /* Try to figure out if this is really an EXPLAIN statement. If this
2788  ** cannot be verified, return early.  */
2789  if( sqlite3_column_count(pSql)!=8 ){
2790    p->cMode = p->mode;
2791    return;
2792  }
2793  zSql = sqlite3_sql(pSql);
2794  if( zSql==0 ) return;
2795  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2796  if( sqlite3_strnicmp(z, "explain", 7) ){
2797    p->cMode = p->mode;
2798    return;
2799  }
2800
2801  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2802    int i;
2803    int iAddr = sqlite3_column_int(pSql, 0);
2804    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2805
2806    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2807    ** p2 is an instruction address, set variable p2op to the index of that
2808    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2809    ** the current instruction is part of a sub-program generated by an
2810    ** SQL trigger or foreign key.  */
2811    int p2 = sqlite3_column_int(pSql, 3);
2812    int p2op = (p2 + (iOp-iAddr));
2813
2814    /* Grow the p->aiIndent array as required */
2815    if( iOp>=nAlloc ){
2816      if( iOp==0 ){
2817        /* Do further verfication that this is explain output.  Abort if
2818        ** it is not */
2819        static const char *explainCols[] = {
2820           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2821        int jj;
2822        for(jj=0; jj<ArraySize(explainCols); jj++){
2823          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2824            p->cMode = p->mode;
2825            sqlite3_reset(pSql);
2826            return;
2827          }
2828        }
2829      }
2830      nAlloc += 100;
2831      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2832      if( p->aiIndent==0 ) shell_out_of_memory();
2833      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2834      if( abYield==0 ) shell_out_of_memory();
2835    }
2836    abYield[iOp] = str_in_array(zOp, azYield);
2837    p->aiIndent[iOp] = 0;
2838    p->nIndent = iOp+1;
2839
2840    if( str_in_array(zOp, azNext) ){
2841      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2842    }
2843    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2844     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2845    ){
2846      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2847    }
2848  }
2849
2850  p->iIndent = 0;
2851  sqlite3_free(abYield);
2852  sqlite3_reset(pSql);
2853}
2854
2855/*
2856** Free the array allocated by explain_data_prepare().
2857*/
2858static void explain_data_delete(ShellState *p){
2859  sqlite3_free(p->aiIndent);
2860  p->aiIndent = 0;
2861  p->nIndent = 0;
2862  p->iIndent = 0;
2863}
2864
2865/*
2866** Disable and restore .wheretrace and .selecttrace settings.
2867*/
2868#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2869extern int sqlite3SelectTrace;
2870static int savedSelectTrace;
2871#endif
2872#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2873extern int sqlite3WhereTrace;
2874static int savedWhereTrace;
2875#endif
2876static void disable_debug_trace_modes(void){
2877#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2878  savedSelectTrace = sqlite3SelectTrace;
2879  sqlite3SelectTrace = 0;
2880#endif
2881#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2882  savedWhereTrace = sqlite3WhereTrace;
2883  sqlite3WhereTrace = 0;
2884#endif
2885}
2886static void restore_debug_trace_modes(void){
2887#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2888  sqlite3SelectTrace = savedSelectTrace;
2889#endif
2890#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2891  sqlite3WhereTrace = savedWhereTrace;
2892#endif
2893}
2894
2895/* Create the TEMP table used to store parameter bindings */
2896static void bind_table_init(ShellState *p){
2897  int wrSchema = 0;
2898  int defensiveMode = 0;
2899  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2900  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2901  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2902  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2903  sqlite3_exec(p->db,
2904    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2905    "  key TEXT PRIMARY KEY,\n"
2906    "  value ANY\n"
2907    ") WITHOUT ROWID;",
2908    0, 0, 0);
2909  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2910  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2911}
2912
2913/*
2914** Bind parameters on a prepared statement.
2915**
2916** Parameter bindings are taken from a TEMP table of the form:
2917**
2918**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2919**    WITHOUT ROWID;
2920**
2921** No bindings occur if this table does not exist.  The name of the table
2922** begins with "sqlite_" so that it will not collide with ordinary application
2923** tables.  The table must be in the TEMP schema.
2924*/
2925static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2926  int nVar;
2927  int i;
2928  int rc;
2929  sqlite3_stmt *pQ = 0;
2930
2931  nVar = sqlite3_bind_parameter_count(pStmt);
2932  if( nVar==0 ) return;  /* Nothing to do */
2933  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2934                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2935    return; /* Parameter table does not exist */
2936  }
2937  rc = sqlite3_prepare_v2(pArg->db,
2938          "SELECT value FROM temp.sqlite_parameters"
2939          " WHERE key=?1", -1, &pQ, 0);
2940  if( rc || pQ==0 ) return;
2941  for(i=1; i<=nVar; i++){
2942    char zNum[30];
2943    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2944    if( zVar==0 ){
2945      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2946      zVar = zNum;
2947    }
2948    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2949    if( sqlite3_step(pQ)==SQLITE_ROW ){
2950      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2951    }else{
2952      sqlite3_bind_null(pStmt, i);
2953    }
2954    sqlite3_reset(pQ);
2955  }
2956  sqlite3_finalize(pQ);
2957}
2958
2959/*
2960** UTF8 box-drawing characters.  Imagine box lines like this:
2961**
2962**           1
2963**           |
2964**       4 --+-- 2
2965**           |
2966**           3
2967**
2968** Each box characters has between 2 and 4 of the lines leading from
2969** the center.  The characters are here identified by the numbers of
2970** their corresponding lines.
2971*/
2972#define BOX_24   "\342\224\200"  /* U+2500 --- */
2973#define BOX_13   "\342\224\202"  /* U+2502  |  */
2974#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2975#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2976#define BOX_12   "\342\224\224"  /* U+2514  '- */
2977#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2978#define BOX_123  "\342\224\234"  /* U+251c  |- */
2979#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2980#define BOX_234  "\342\224\254"  /* U+252c -,- */
2981#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2982#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2983
2984/* Draw horizontal line N characters long using unicode box
2985** characters
2986*/
2987static void print_box_line(FILE *out, int N){
2988  const char zDash[] =
2989      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
2990      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
2991  const int nDash = sizeof(zDash) - 1;
2992  N *= 3;
2993  while( N>nDash ){
2994    utf8_printf(out, zDash);
2995    N -= nDash;
2996  }
2997  utf8_printf(out, "%.*s", N, zDash);
2998}
2999
3000/*
3001** Draw a horizontal separator for a MODE_Box table.
3002*/
3003static void print_box_row_separator(
3004  ShellState *p,
3005  int nArg,
3006  const char *zSep1,
3007  const char *zSep2,
3008  const char *zSep3
3009){
3010  int i;
3011  if( nArg>0 ){
3012    utf8_printf(p->out, "%s", zSep1);
3013    print_box_line(p->out, p->actualWidth[0]+2);
3014    for(i=1; i<nArg; i++){
3015      utf8_printf(p->out, "%s", zSep2);
3016      print_box_line(p->out, p->actualWidth[i]+2);
3017    }
3018    utf8_printf(p->out, "%s", zSep3);
3019  }
3020  fputs("\n", p->out);
3021}
3022
3023
3024
3025/*
3026** Run a prepared statement and output the result in one of the
3027** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3028** or MODE_Box.
3029**
3030** This is different from ordinary exec_prepared_stmt() in that
3031** it has to run the entire query and gather the results into memory
3032** first, in order to determine column widths, before providing
3033** any output.
3034*/
3035static void exec_prepared_stmt_columnar(
3036  ShellState *p,                        /* Pointer to ShellState */
3037  sqlite3_stmt *pStmt                   /* Statment to run */
3038){
3039  int nRow = 0;
3040  int nColumn = 0;
3041  char **azData = 0;
3042  char *zMsg = 0;
3043  const char *z;
3044  int rc;
3045  int i, j, nTotal, w, n;
3046  const char *colSep = 0;
3047  const char *rowSep = 0;
3048
3049  rc = sqlite3_get_table(p->db, sqlite3_sql(pStmt),
3050                         &azData, &nRow, &nColumn, &zMsg);
3051  if( rc ){
3052    utf8_printf(p->out, "ERROR: %s\n", zMsg);
3053    sqlite3_free(zMsg);
3054    sqlite3_free_table(azData);
3055    return;
3056  }
3057  if( nRow==0 || nColumn==0 ) goto columnar_end;
3058  if( nColumn>p->nWidth ){
3059    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3060    if( p->colWidth==0 ) shell_out_of_memory();
3061    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3062    p->nWidth = nColumn;
3063    p->actualWidth = &p->colWidth[nColumn];
3064  }
3065  memset(p->actualWidth, 0, nColumn*sizeof(int));
3066  for(i=0; i<nColumn; i++){
3067    w = p->colWidth[i];
3068    if( w<0 ) w = -w;
3069    p->actualWidth[i] = w;
3070  }
3071  nTotal = nColumn*(nRow+1);
3072  for(i=0; i<nTotal; i++){
3073    z = azData[i];
3074    if( z==0 ) z = p->nullValue;
3075    n = strlenChar(z);
3076    j = i%nColumn;
3077    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3078  }
3079  if( seenInterrupt ) goto columnar_end;
3080  switch( p->cMode ){
3081    case MODE_Column: {
3082      colSep = "  ";
3083      rowSep = "\n";
3084      if( p->showHeader ){
3085        for(i=0; i<nColumn; i++){
3086          w = p->actualWidth[i];
3087          if( p->colWidth[i]<0 ) w = -w;
3088          utf8_width_print(p->out, w, azData[i]);
3089          fputs(i==nColumn-1?"\n":"  ", p->out);
3090        }
3091        for(i=0; i<nColumn; i++){
3092          print_dashes(p->out, p->actualWidth[i]);
3093          fputs(i==nColumn-1?"\n":"  ", p->out);
3094        }
3095      }
3096      break;
3097    }
3098    case MODE_Table: {
3099      colSep = " | ";
3100      rowSep = " |\n";
3101      print_row_separator(p, nColumn, "+");
3102      fputs("| ", p->out);
3103      for(i=0; i<nColumn; i++){
3104        w = p->actualWidth[i];
3105        n = strlenChar(azData[i]);
3106        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3107        fputs(i==nColumn-1?" |\n":" | ", p->out);
3108      }
3109      print_row_separator(p, nColumn, "+");
3110      break;
3111    }
3112    case MODE_Markdown: {
3113      colSep = " | ";
3114      rowSep = " |\n";
3115      fputs("| ", p->out);
3116      for(i=0; i<nColumn; i++){
3117        w = p->actualWidth[i];
3118        n = strlenChar(azData[i]);
3119        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3120        fputs(i==nColumn-1?" |\n":" | ", p->out);
3121      }
3122      print_row_separator(p, nColumn, "|");
3123      break;
3124    }
3125    case MODE_Box: {
3126      colSep = " " BOX_13 " ";
3127      rowSep = " " BOX_13 "\n";
3128      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3129      utf8_printf(p->out, BOX_13 " ");
3130      for(i=0; i<nColumn; i++){
3131        w = p->actualWidth[i];
3132        n = strlenChar(azData[i]);
3133        utf8_printf(p->out, "%*s%s%*s%s",
3134            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3135            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3136      }
3137      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3138      break;
3139    }
3140  }
3141  for(i=nColumn, j=0; i<nTotal; i++, j++){
3142    if( j==0 && p->cMode!=MODE_Column ){
3143      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3144    }
3145    z = azData[i];
3146    if( z==0 ) z = p->nullValue;
3147    w = p->actualWidth[j];
3148    if( p->colWidth[j]<0 ) w = -w;
3149    utf8_width_print(p->out, w, z);
3150    if( j==nColumn-1 ){
3151      utf8_printf(p->out, "%s", rowSep);
3152      j = -1;
3153      if( seenInterrupt ) goto columnar_end;
3154    }else{
3155      utf8_printf(p->out, "%s", colSep);
3156    }
3157  }
3158  if( p->cMode==MODE_Table ){
3159    print_row_separator(p, nColumn, "+");
3160  }else if( p->cMode==MODE_Box ){
3161    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3162  }
3163columnar_end:
3164  if( seenInterrupt ){
3165    utf8_printf(p->out, "Interrupt\n");
3166  }
3167  sqlite3_free_table(azData);
3168}
3169
3170/*
3171** Run a prepared statement
3172*/
3173static void exec_prepared_stmt(
3174  ShellState *pArg,                                /* Pointer to ShellState */
3175  sqlite3_stmt *pStmt                              /* Statment to run */
3176){
3177  int rc;
3178
3179  if( pArg->cMode==MODE_Column
3180   || pArg->cMode==MODE_Table
3181   || pArg->cMode==MODE_Box
3182   || pArg->cMode==MODE_Markdown
3183  ){
3184    exec_prepared_stmt_columnar(pArg, pStmt);
3185    return;
3186  }
3187
3188  /* perform the first step.  this will tell us if we
3189  ** have a result set or not and how wide it is.
3190  */
3191  rc = sqlite3_step(pStmt);
3192  /* if we have a result set... */
3193  if( SQLITE_ROW == rc ){
3194    /* allocate space for col name ptr, value ptr, and type */
3195    int nCol = sqlite3_column_count(pStmt);
3196    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3197    if( !pData ){
3198      rc = SQLITE_NOMEM;
3199    }else{
3200      char **azCols = (char **)pData;      /* Names of result columns */
3201      char **azVals = &azCols[nCol];       /* Results */
3202      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3203      int i, x;
3204      assert(sizeof(int) <= sizeof(char *));
3205      /* save off ptrs to column names */
3206      for(i=0; i<nCol; i++){
3207        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3208      }
3209      do{
3210        /* extract the data and data types */
3211        for(i=0; i<nCol; i++){
3212          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3213          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3214            azVals[i] = "";
3215          }else{
3216            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3217          }
3218          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3219            rc = SQLITE_NOMEM;
3220            break; /* from for */
3221          }
3222        } /* end for */
3223
3224        /* if data and types extracted successfully... */
3225        if( SQLITE_ROW == rc ){
3226          /* call the supplied callback with the result row data */
3227          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3228            rc = SQLITE_ABORT;
3229          }else{
3230            rc = sqlite3_step(pStmt);
3231          }
3232        }
3233      } while( SQLITE_ROW == rc );
3234      sqlite3_free(pData);
3235      if( pArg->cMode==MODE_Json ){
3236        fputs("]\n", pArg->out);
3237      }
3238    }
3239  }
3240}
3241
3242#ifndef SQLITE_OMIT_VIRTUALTABLE
3243/*
3244** This function is called to process SQL if the previous shell command
3245** was ".expert". It passes the SQL in the second argument directly to
3246** the sqlite3expert object.
3247**
3248** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3249** code. In this case, (*pzErr) may be set to point to a buffer containing
3250** an English language error message. It is the responsibility of the
3251** caller to eventually free this buffer using sqlite3_free().
3252*/
3253static int expertHandleSQL(
3254  ShellState *pState,
3255  const char *zSql,
3256  char **pzErr
3257){
3258  assert( pState->expert.pExpert );
3259  assert( pzErr==0 || *pzErr==0 );
3260  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3261}
3262
3263/*
3264** This function is called either to silently clean up the object
3265** created by the ".expert" command (if bCancel==1), or to generate a
3266** report from it and then clean it up (if bCancel==0).
3267**
3268** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3269** code. In this case, (*pzErr) may be set to point to a buffer containing
3270** an English language error message. It is the responsibility of the
3271** caller to eventually free this buffer using sqlite3_free().
3272*/
3273static int expertFinish(
3274  ShellState *pState,
3275  int bCancel,
3276  char **pzErr
3277){
3278  int rc = SQLITE_OK;
3279  sqlite3expert *p = pState->expert.pExpert;
3280  assert( p );
3281  assert( bCancel || pzErr==0 || *pzErr==0 );
3282  if( bCancel==0 ){
3283    FILE *out = pState->out;
3284    int bVerbose = pState->expert.bVerbose;
3285
3286    rc = sqlite3_expert_analyze(p, pzErr);
3287    if( rc==SQLITE_OK ){
3288      int nQuery = sqlite3_expert_count(p);
3289      int i;
3290
3291      if( bVerbose ){
3292        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3293        raw_printf(out, "-- Candidates -----------------------------\n");
3294        raw_printf(out, "%s\n", zCand);
3295      }
3296      for(i=0; i<nQuery; i++){
3297        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3298        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3299        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3300        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3301        if( bVerbose ){
3302          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3303          raw_printf(out, "%s\n\n", zSql);
3304        }
3305        raw_printf(out, "%s\n", zIdx);
3306        raw_printf(out, "%s\n", zEQP);
3307      }
3308    }
3309  }
3310  sqlite3_expert_destroy(p);
3311  pState->expert.pExpert = 0;
3312  return rc;
3313}
3314
3315/*
3316** Implementation of ".expert" dot command.
3317*/
3318static int expertDotCommand(
3319  ShellState *pState,             /* Current shell tool state */
3320  char **azArg,                   /* Array of arguments passed to dot command */
3321  int nArg                        /* Number of entries in azArg[] */
3322){
3323  int rc = SQLITE_OK;
3324  char *zErr = 0;
3325  int i;
3326  int iSample = 0;
3327
3328  assert( pState->expert.pExpert==0 );
3329  memset(&pState->expert, 0, sizeof(ExpertInfo));
3330
3331  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3332    char *z = azArg[i];
3333    int n;
3334    if( z[0]=='-' && z[1]=='-' ) z++;
3335    n = strlen30(z);
3336    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3337      pState->expert.bVerbose = 1;
3338    }
3339    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3340      if( i==(nArg-1) ){
3341        raw_printf(stderr, "option requires an argument: %s\n", z);
3342        rc = SQLITE_ERROR;
3343      }else{
3344        iSample = (int)integerValue(azArg[++i]);
3345        if( iSample<0 || iSample>100 ){
3346          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3347          rc = SQLITE_ERROR;
3348        }
3349      }
3350    }
3351    else{
3352      raw_printf(stderr, "unknown option: %s\n", z);
3353      rc = SQLITE_ERROR;
3354    }
3355  }
3356
3357  if( rc==SQLITE_OK ){
3358    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3359    if( pState->expert.pExpert==0 ){
3360      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3361      rc = SQLITE_ERROR;
3362    }else{
3363      sqlite3_expert_config(
3364          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3365      );
3366    }
3367  }
3368
3369  return rc;
3370}
3371#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3372
3373/*
3374** Execute a statement or set of statements.  Print
3375** any result rows/columns depending on the current mode
3376** set via the supplied callback.
3377**
3378** This is very similar to SQLite's built-in sqlite3_exec()
3379** function except it takes a slightly different callback
3380** and callback data argument.
3381*/
3382static int shell_exec(
3383  ShellState *pArg,                         /* Pointer to ShellState */
3384  const char *zSql,                         /* SQL to be evaluated */
3385  char **pzErrMsg                           /* Error msg written here */
3386){
3387  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3388  int rc = SQLITE_OK;             /* Return Code */
3389  int rc2;
3390  const char *zLeftover;          /* Tail of unprocessed SQL */
3391  sqlite3 *db = pArg->db;
3392
3393  if( pzErrMsg ){
3394    *pzErrMsg = NULL;
3395  }
3396
3397#ifndef SQLITE_OMIT_VIRTUALTABLE
3398  if( pArg->expert.pExpert ){
3399    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3400    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3401  }
3402#endif
3403
3404  while( zSql[0] && (SQLITE_OK == rc) ){
3405    static const char *zStmtSql;
3406    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3407    if( SQLITE_OK != rc ){
3408      if( pzErrMsg ){
3409        *pzErrMsg = save_err_msg(db);
3410      }
3411    }else{
3412      if( !pStmt ){
3413        /* this happens for a comment or white-space */
3414        zSql = zLeftover;
3415        while( IsSpace(zSql[0]) ) zSql++;
3416        continue;
3417      }
3418      zStmtSql = sqlite3_sql(pStmt);
3419      if( zStmtSql==0 ) zStmtSql = "";
3420      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3421
3422      /* save off the prepared statment handle and reset row count */
3423      if( pArg ){
3424        pArg->pStmt = pStmt;
3425        pArg->cnt = 0;
3426      }
3427
3428      /* echo the sql statement if echo on */
3429      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3430        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3431      }
3432
3433      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3434      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3435        sqlite3_stmt *pExplain;
3436        char *zEQP;
3437        int triggerEQP = 0;
3438        disable_debug_trace_modes();
3439        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3440        if( pArg->autoEQP>=AUTOEQP_trigger ){
3441          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3442        }
3443        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3444        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3445        if( rc==SQLITE_OK ){
3446          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3447            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3448            int iEqpId = sqlite3_column_int(pExplain, 0);
3449            int iParentId = sqlite3_column_int(pExplain, 1);
3450            if( zEQPLine==0 ) zEQPLine = "";
3451            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3452            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3453          }
3454          eqp_render(pArg);
3455        }
3456        sqlite3_finalize(pExplain);
3457        sqlite3_free(zEQP);
3458        if( pArg->autoEQP>=AUTOEQP_full ){
3459          /* Also do an EXPLAIN for ".eqp full" mode */
3460          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3461          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3462          if( rc==SQLITE_OK ){
3463            pArg->cMode = MODE_Explain;
3464            explain_data_prepare(pArg, pExplain);
3465            exec_prepared_stmt(pArg, pExplain);
3466            explain_data_delete(pArg);
3467          }
3468          sqlite3_finalize(pExplain);
3469          sqlite3_free(zEQP);
3470        }
3471        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3472          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3473          /* Reprepare pStmt before reactiving trace modes */
3474          sqlite3_finalize(pStmt);
3475          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3476          if( pArg ) pArg->pStmt = pStmt;
3477        }
3478        restore_debug_trace_modes();
3479      }
3480
3481      if( pArg ){
3482        pArg->cMode = pArg->mode;
3483        if( pArg->autoExplain ){
3484          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3485            pArg->cMode = MODE_Explain;
3486          }
3487          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3488            pArg->cMode = MODE_EQP;
3489          }
3490        }
3491
3492        /* If the shell is currently in ".explain" mode, gather the extra
3493        ** data required to add indents to the output.*/
3494        if( pArg->cMode==MODE_Explain ){
3495          explain_data_prepare(pArg, pStmt);
3496        }
3497      }
3498
3499      bind_prepared_stmt(pArg, pStmt);
3500      exec_prepared_stmt(pArg, pStmt);
3501      explain_data_delete(pArg);
3502      eqp_render(pArg);
3503
3504      /* print usage stats if stats on */
3505      if( pArg && pArg->statsOn ){
3506        display_stats(db, pArg, 0);
3507      }
3508
3509      /* print loop-counters if required */
3510      if( pArg && pArg->scanstatsOn ){
3511        display_scanstats(db, pArg);
3512      }
3513
3514      /* Finalize the statement just executed. If this fails, save a
3515      ** copy of the error message. Otherwise, set zSql to point to the
3516      ** next statement to execute. */
3517      rc2 = sqlite3_finalize(pStmt);
3518      if( rc!=SQLITE_NOMEM ) rc = rc2;
3519      if( rc==SQLITE_OK ){
3520        zSql = zLeftover;
3521        while( IsSpace(zSql[0]) ) zSql++;
3522      }else if( pzErrMsg ){
3523        *pzErrMsg = save_err_msg(db);
3524      }
3525
3526      /* clear saved stmt handle */
3527      if( pArg ){
3528        pArg->pStmt = NULL;
3529      }
3530    }
3531  } /* end while */
3532
3533  return rc;
3534}
3535
3536/*
3537** Release memory previously allocated by tableColumnList().
3538*/
3539static void freeColumnList(char **azCol){
3540  int i;
3541  for(i=1; azCol[i]; i++){
3542    sqlite3_free(azCol[i]);
3543  }
3544  /* azCol[0] is a static string */
3545  sqlite3_free(azCol);
3546}
3547
3548/*
3549** Return a list of pointers to strings which are the names of all
3550** columns in table zTab.   The memory to hold the names is dynamically
3551** allocated and must be released by the caller using a subsequent call
3552** to freeColumnList().
3553**
3554** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3555** value that needs to be preserved, then azCol[0] is filled in with the
3556** name of the rowid column.
3557**
3558** The first regular column in the table is azCol[1].  The list is terminated
3559** by an entry with azCol[i]==0.
3560*/
3561static char **tableColumnList(ShellState *p, const char *zTab){
3562  char **azCol = 0;
3563  sqlite3_stmt *pStmt;
3564  char *zSql;
3565  int nCol = 0;
3566  int nAlloc = 0;
3567  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3568  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3569  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3570  int rc;
3571
3572  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3573  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3574  sqlite3_free(zSql);
3575  if( rc ) return 0;
3576  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3577    if( nCol>=nAlloc-2 ){
3578      nAlloc = nAlloc*2 + nCol + 10;
3579      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3580      if( azCol==0 ) shell_out_of_memory();
3581    }
3582    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3583    if( sqlite3_column_int(pStmt, 5) ){
3584      nPK++;
3585      if( nPK==1
3586       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3587                          "INTEGER")==0
3588      ){
3589        isIPK = 1;
3590      }else{
3591        isIPK = 0;
3592      }
3593    }
3594  }
3595  sqlite3_finalize(pStmt);
3596  if( azCol==0 ) return 0;
3597  azCol[0] = 0;
3598  azCol[nCol+1] = 0;
3599
3600  /* The decision of whether or not a rowid really needs to be preserved
3601  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3602  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3603  ** rowids on tables where the rowid is inaccessible because there are other
3604  ** columns in the table named "rowid", "_rowid_", and "oid".
3605  */
3606  if( preserveRowid && isIPK ){
3607    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3608    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3609    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3610    ** ROWID aliases.  To distinguish these cases, check to see if
3611    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3612    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3613    */
3614    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3615                           " WHERE origin='pk'", zTab);
3616    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3617    sqlite3_free(zSql);
3618    if( rc ){
3619      freeColumnList(azCol);
3620      return 0;
3621    }
3622    rc = sqlite3_step(pStmt);
3623    sqlite3_finalize(pStmt);
3624    preserveRowid = rc==SQLITE_ROW;
3625  }
3626  if( preserveRowid ){
3627    /* Only preserve the rowid if we can find a name to use for the
3628    ** rowid */
3629    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3630    int i, j;
3631    for(j=0; j<3; j++){
3632      for(i=1; i<=nCol; i++){
3633        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3634      }
3635      if( i>nCol ){
3636        /* At this point, we know that azRowid[j] is not the name of any
3637        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3638        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3639        ** tables will fail this last check */
3640        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3641        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3642        break;
3643      }
3644    }
3645  }
3646  return azCol;
3647}
3648
3649/*
3650** Toggle the reverse_unordered_selects setting.
3651*/
3652static void toggleSelectOrder(sqlite3 *db){
3653  sqlite3_stmt *pStmt = 0;
3654  int iSetting = 0;
3655  char zStmt[100];
3656  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3657  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3658    iSetting = sqlite3_column_int(pStmt, 0);
3659  }
3660  sqlite3_finalize(pStmt);
3661  sqlite3_snprintf(sizeof(zStmt), zStmt,
3662       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3663  sqlite3_exec(db, zStmt, 0, 0, 0);
3664}
3665
3666/*
3667** This is a different callback routine used for dumping the database.
3668** Each row received by this callback consists of a table name,
3669** the table type ("index" or "table") and SQL to create the table.
3670** This routine should print text sufficient to recreate the table.
3671*/
3672static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3673  int rc;
3674  const char *zTable;
3675  const char *zType;
3676  const char *zSql;
3677  ShellState *p = (ShellState *)pArg;
3678
3679  UNUSED_PARAMETER(azNotUsed);
3680  if( nArg!=3 || azArg==0 ) return 0;
3681  zTable = azArg[0];
3682  zType = azArg[1];
3683  zSql = azArg[2];
3684
3685  if( strcmp(zTable, "sqlite_sequence")==0 ){
3686    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3687  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3688    raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3689  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3690    return 0;
3691  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3692    char *zIns;
3693    if( !p->writableSchema ){
3694      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3695      p->writableSchema = 1;
3696    }
3697    zIns = sqlite3_mprintf(
3698       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3699       "VALUES('table','%q','%q',0,'%q');",
3700       zTable, zTable, zSql);
3701    utf8_printf(p->out, "%s\n", zIns);
3702    sqlite3_free(zIns);
3703    return 0;
3704  }else{
3705    printSchemaLine(p->out, zSql, ";\n");
3706  }
3707
3708  if( strcmp(zType, "table")==0 ){
3709    ShellText sSelect;
3710    ShellText sTable;
3711    char **azCol;
3712    int i;
3713    char *savedDestTable;
3714    int savedMode;
3715
3716    azCol = tableColumnList(p, zTable);
3717    if( azCol==0 ){
3718      p->nErr++;
3719      return 0;
3720    }
3721
3722    /* Always quote the table name, even if it appears to be pure ascii,
3723    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3724    initText(&sTable);
3725    appendText(&sTable, zTable, quoteChar(zTable));
3726    /* If preserving the rowid, add a column list after the table name.
3727    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3728    ** instead of the usual "INSERT INTO tab VALUES(...)".
3729    */
3730    if( azCol[0] ){
3731      appendText(&sTable, "(", 0);
3732      appendText(&sTable, azCol[0], 0);
3733      for(i=1; azCol[i]; i++){
3734        appendText(&sTable, ",", 0);
3735        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3736      }
3737      appendText(&sTable, ")", 0);
3738    }
3739
3740    /* Build an appropriate SELECT statement */
3741    initText(&sSelect);
3742    appendText(&sSelect, "SELECT ", 0);
3743    if( azCol[0] ){
3744      appendText(&sSelect, azCol[0], 0);
3745      appendText(&sSelect, ",", 0);
3746    }
3747    for(i=1; azCol[i]; i++){
3748      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3749      if( azCol[i+1] ){
3750        appendText(&sSelect, ",", 0);
3751      }
3752    }
3753    freeColumnList(azCol);
3754    appendText(&sSelect, " FROM ", 0);
3755    appendText(&sSelect, zTable, quoteChar(zTable));
3756
3757    savedDestTable = p->zDestTable;
3758    savedMode = p->mode;
3759    p->zDestTable = sTable.z;
3760    p->mode = p->cMode = MODE_Insert;
3761    rc = shell_exec(p, sSelect.z, 0);
3762    if( (rc&0xff)==SQLITE_CORRUPT ){
3763      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3764      toggleSelectOrder(p->db);
3765      shell_exec(p, sSelect.z, 0);
3766      toggleSelectOrder(p->db);
3767    }
3768    p->zDestTable = savedDestTable;
3769    p->mode = savedMode;
3770    freeText(&sTable);
3771    freeText(&sSelect);
3772    if( rc ) p->nErr++;
3773  }
3774  return 0;
3775}
3776
3777/*
3778** Run zQuery.  Use dump_callback() as the callback routine so that
3779** the contents of the query are output as SQL statements.
3780**
3781** If we get a SQLITE_CORRUPT error, rerun the query after appending
3782** "ORDER BY rowid DESC" to the end.
3783*/
3784static int run_schema_dump_query(
3785  ShellState *p,
3786  const char *zQuery
3787){
3788  int rc;
3789  char *zErr = 0;
3790  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3791  if( rc==SQLITE_CORRUPT ){
3792    char *zQ2;
3793    int len = strlen30(zQuery);
3794    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3795    if( zErr ){
3796      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3797      sqlite3_free(zErr);
3798      zErr = 0;
3799    }
3800    zQ2 = malloc( len+100 );
3801    if( zQ2==0 ) return rc;
3802    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3803    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3804    if( rc ){
3805      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3806    }else{
3807      rc = SQLITE_CORRUPT;
3808    }
3809    sqlite3_free(zErr);
3810    free(zQ2);
3811  }
3812  return rc;
3813}
3814
3815/*
3816** Text of help messages.
3817**
3818** The help text for each individual command begins with a line that starts
3819** with ".".  Subsequent lines are supplimental information.
3820**
3821** There must be two or more spaces between the end of the command and the
3822** start of the description of what that command does.
3823*/
3824static const char *(azHelp[]) = {
3825#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3826  ".archive ...             Manage SQL archives",
3827  "   Each command must have exactly one of the following options:",
3828  "     -c, --create               Create a new archive",
3829  "     -u, --update               Add or update files with changed mtime",
3830  "     -i, --insert               Like -u but always add even if unchanged",
3831  "     -t, --list                 List contents of archive",
3832  "     -x, --extract              Extract files from archive",
3833  "   Optional arguments:",
3834  "     -v, --verbose              Print each filename as it is processed",
3835  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3836  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3837  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3838  "     -n, --dryrun               Show the SQL that would have occurred",
3839  "   Examples:",
3840  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3841  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3842  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3843  "   See also:",
3844  "      http://sqlite.org/cli.html#sqlar_archive_support",
3845#endif
3846#ifndef SQLITE_OMIT_AUTHORIZATION
3847  ".auth ON|OFF             Show authorizer callbacks",
3848#endif
3849  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3850  "       --append            Use the appendvfs",
3851  "       --async             Write to FILE without journal and fsync()",
3852  ".bail on|off             Stop after hitting an error.  Default OFF",
3853  ".binary on|off           Turn binary output on or off.  Default OFF",
3854  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3855  ".changes on|off          Show number of rows changed by SQL",
3856  ".check GLOB              Fail if output since .testcase does not match",
3857  ".clone NEWDB             Clone data into NEWDB from the existing database",
3858  ".databases               List names and files of attached databases",
3859  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3860  ".dbinfo ?DB?             Show status information about the database",
3861  ".dump ?TABLE?            Render database content as SQL",
3862  "   Options:",
3863  "     --preserve-rowids      Include ROWID values in the output",
3864  "     --newlines             Allow unescaped newline characters in output",
3865  "   TABLE is a LIKE pattern for the tables to dump",
3866  "   Additional LIKE patterns can be given in subsequent arguments",
3867  ".echo on|off             Turn command echo on or off",
3868  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3869  "   Other Modes:",
3870#ifdef SQLITE_DEBUG
3871  "      test                  Show raw EXPLAIN QUERY PLAN output",
3872  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3873#endif
3874  "      trigger               Like \"full\" but also show trigger bytecode",
3875  ".excel                   Display the output of next command in spreadsheet",
3876  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3877  ".exit ?CODE?             Exit this program with return-code CODE",
3878  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3879  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3880  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3881  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3882  "   --help                  Show CMD details",
3883  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3884  ".headers on|off          Turn display of headers on or off",
3885  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3886  ".import FILE TABLE       Import data from FILE into TABLE",
3887  "   Options:",
3888  "     --ascii               Use \\037 and \\036 as column and row separators",
3889  "     --csv                 Use , and \\n as column and row separators",
3890  "     --skip N              Skip the first N rows of input",
3891  "     -v                    \"Verbose\" - increase auxiliary output",
3892  "   Notes:",
3893  "     *  If TABLE does not exist, it is created.  The first row of input",
3894  "        determines the column names.",
3895  "     *  If neither --csv or --ascii are used, the input mode is derived",
3896  "        from the \".mode\" output mode",
3897  "     *  If FILE begins with \"|\" then it is a command that generates the",
3898  "        input text.",
3899#ifndef SQLITE_OMIT_TEST_CONTROL
3900  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3901#endif
3902  ".indexes ?TABLE?         Show names of indexes",
3903  "                           If TABLE is specified, only show indexes for",
3904  "                           tables matching TABLE using the LIKE operator.",
3905#ifdef SQLITE_ENABLE_IOTRACE
3906  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3907#endif
3908  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3909  ".lint OPTIONS            Report potential schema issues.",
3910  "     Options:",
3911  "        fkey-indexes     Find missing foreign key indexes",
3912#ifndef SQLITE_OMIT_LOAD_EXTENSION
3913  ".load FILE ?ENTRY?       Load an extension library",
3914#endif
3915  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3916  ".mode MODE ?TABLE?       Set output mode",
3917  "   MODE is one of:",
3918  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3919  "     box       Tables using unicode box-drawing characters",
3920  "     csv       Comma-separated values",
3921  "     column    Output in columns.  (See .width)",
3922  "     html      HTML <table> code",
3923  "     insert    SQL insert statements for TABLE",
3924  "     json      Results in a JSON array",
3925  "     line      One value per line",
3926  "     list      Values delimited by \"|\"",
3927  "     markdown  Markdown table format",
3928  "     quote     Escape answers as for SQL",
3929  "     table     ASCII-art table",
3930  "     tabs      Tab-separated values",
3931  "     tcl       TCL list elements",
3932  ".nullvalue STRING        Use STRING in place of NULL values",
3933  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3934  "     If FILE begins with '|' then open as a pipe",
3935  "       --bom  Put a UTF8 byte-order mark at the beginning",
3936  "       -e     Send output to the system text editor",
3937  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3938#ifdef SQLITE_DEBUG
3939  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3940#endif
3941  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3942  "     Options:",
3943  "        --append        Use appendvfs to append database to the end of FILE",
3944#ifdef SQLITE_ENABLE_DESERIALIZE
3945  "        --deserialize   Load into memory useing sqlite3_deserialize()",
3946  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3947  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3948#endif
3949  "        --new           Initialize FILE to an empty database",
3950  "        --nofollow      Do not follow symbolic links",
3951  "        --readonly      Open FILE readonly",
3952  "        --zip           FILE is a ZIP archive",
3953  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3954  "   If FILE begins with '|' then open it as a pipe.",
3955  "   Options:",
3956  "     --bom                 Prefix output with a UTF8 byte-order mark",
3957  "     -e                    Send output to the system text editor",
3958  "     -x                    Send output as CSV to a spreadsheet",
3959  ".parameter CMD ...       Manage SQL parameter bindings",
3960  "   clear                   Erase all bindings",
3961  "   init                    Initialize the TEMP table that holds bindings",
3962  "   list                    List the current parameter bindings",
3963  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
3964  "                           PARAMETER should start with one of: $ : @ ?",
3965  "   unset PARAMETER         Remove PARAMETER from the binding table",
3966  ".print STRING...         Print literal STRING",
3967#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3968  ".progress N              Invoke progress handler after every N opcodes",
3969  "   --limit N                 Interrupt after N progress callbacks",
3970  "   --once                    Do no more than one progress interrupt",
3971  "   --quiet|-q                No output except at interrupts",
3972  "   --reset                   Reset the count for each input and interrupt",
3973#endif
3974  ".prompt MAIN CONTINUE    Replace the standard prompts",
3975  ".quit                    Exit this program",
3976  ".read FILE               Read input from FILE",
3977#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
3978  ".recover                 Recover as much data as possible from corrupt db.",
3979  "   --freelist-corrupt       Assume the freelist is corrupt",
3980  "   --recovery-db NAME       Store recovery metadata in database file NAME",
3981  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
3982  "   --no-rowids              Do not attempt to recover rowid values",
3983  "                            that are not also INTEGER PRIMARY KEYs",
3984#endif
3985  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
3986  ".save FILE               Write in-memory database into FILE",
3987  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
3988  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
3989  "     Options:",
3990  "         --indent            Try to pretty-print the schema",
3991  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
3992  "    Options:",
3993  "       --init               Create a new SELFTEST table",
3994  "       -v                   Verbose output",
3995  ".separator COL ?ROW?     Change the column and row separators",
3996#if defined(SQLITE_ENABLE_SESSION)
3997  ".session ?NAME? CMD ...  Create or control sessions",
3998  "   Subcommands:",
3999  "     attach TABLE             Attach TABLE",
4000  "     changeset FILE           Write a changeset into FILE",
4001  "     close                    Close one session",
4002  "     enable ?BOOLEAN?         Set or query the enable bit",
4003  "     filter GLOB...           Reject tables matching GLOBs",
4004  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4005  "     isempty                  Query whether the session is empty",
4006  "     list                     List currently open session names",
4007  "     open DB NAME             Open a new session on DB",
4008  "     patchset FILE            Write a patchset into FILE",
4009  "   If ?NAME? is omitted, the first defined session is used.",
4010#endif
4011  ".sha3sum ...             Compute a SHA3 hash of database content",
4012  "    Options:",
4013  "      --schema              Also hash the sqlite_schema table",
4014  "      --sha3-224            Use the sha3-224 algorithm",
4015  "      --sha3-256            Use the sha3-256 algorithm (default)",
4016  "      --sha3-384            Use the sha3-384 algorithm",
4017  "      --sha3-512            Use the sha3-512 algorithm",
4018  "    Any other argument is a LIKE pattern for tables to hash",
4019#ifndef SQLITE_NOHAVE_SYSTEM
4020  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4021#endif
4022  ".show                    Show the current values for various settings",
4023  ".stats ?on|off?          Show stats or turn stats on or off",
4024#ifndef SQLITE_NOHAVE_SYSTEM
4025  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4026#endif
4027  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4028  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4029  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4030  "                           Run \".testctrl\" with no arguments for details",
4031  ".timeout MS              Try opening locked tables for MS milliseconds",
4032  ".timer on|off            Turn SQL timer on or off",
4033#ifndef SQLITE_OMIT_TRACE
4034  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4035  "    FILE                    Send output to FILE",
4036  "    stdout                  Send output to stdout",
4037  "    stderr                  Send output to stderr",
4038  "    off                     Disable tracing",
4039  "    --expanded              Expand query parameters",
4040#ifdef SQLITE_ENABLE_NORMALIZE
4041  "    --normalized            Normal the SQL statements",
4042#endif
4043  "    --plain                 Show SQL as it is input",
4044  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4045  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4046  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4047  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4048#endif /* SQLITE_OMIT_TRACE */
4049#ifdef SQLITE_DEBUG
4050  ".unmodule NAME ...       Unregister virtual table modules",
4051  "    --allexcept             Unregister everything except those named",
4052#endif
4053  ".vfsinfo ?AUX?           Information about the top-level VFS",
4054  ".vfslist                 List all available VFSes",
4055  ".vfsname ?AUX?           Print the name of the VFS stack",
4056  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4057  "     Negative values right-justify",
4058};
4059
4060/*
4061** Output help text.
4062**
4063** zPattern describes the set of commands for which help text is provided.
4064** If zPattern is NULL, then show all commands, but only give a one-line
4065** description of each.
4066**
4067** Return the number of matches.
4068*/
4069static int showHelp(FILE *out, const char *zPattern){
4070  int i = 0;
4071  int j = 0;
4072  int n = 0;
4073  char *zPat;
4074  if( zPattern==0
4075   || zPattern[0]=='0'
4076   || strcmp(zPattern,"-a")==0
4077   || strcmp(zPattern,"-all")==0
4078   || strcmp(zPattern,"--all")==0
4079  ){
4080    /* Show all commands, but only one line per command */
4081    if( zPattern==0 ) zPattern = "";
4082    for(i=0; i<ArraySize(azHelp); i++){
4083      if( azHelp[i][0]=='.' || zPattern[0] ){
4084        utf8_printf(out, "%s\n", azHelp[i]);
4085        n++;
4086      }
4087    }
4088  }else{
4089    /* Look for commands that for which zPattern is an exact prefix */
4090    zPat = sqlite3_mprintf(".%s*", zPattern);
4091    for(i=0; i<ArraySize(azHelp); i++){
4092      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4093        utf8_printf(out, "%s\n", azHelp[i]);
4094        j = i+1;
4095        n++;
4096      }
4097    }
4098    sqlite3_free(zPat);
4099    if( n ){
4100      if( n==1 ){
4101        /* when zPattern is a prefix of exactly one command, then include the
4102        ** details of that command, which should begin at offset j */
4103        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4104          utf8_printf(out, "%s\n", azHelp[j]);
4105          j++;
4106        }
4107      }
4108      return n;
4109    }
4110    /* Look for commands that contain zPattern anywhere.  Show the complete
4111    ** text of all commands that match. */
4112    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4113    for(i=0; i<ArraySize(azHelp); i++){
4114      if( azHelp[i][0]=='.' ) j = i;
4115      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4116        utf8_printf(out, "%s\n", azHelp[j]);
4117        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4118          j++;
4119          utf8_printf(out, "%s\n", azHelp[j]);
4120        }
4121        i = j;
4122        n++;
4123      }
4124    }
4125    sqlite3_free(zPat);
4126  }
4127  return n;
4128}
4129
4130/* Forward reference */
4131static int process_input(ShellState *p);
4132
4133/*
4134** Read the content of file zName into memory obtained from sqlite3_malloc64()
4135** and return a pointer to the buffer. The caller is responsible for freeing
4136** the memory.
4137**
4138** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4139** read.
4140**
4141** For convenience, a nul-terminator byte is always appended to the data read
4142** from the file before the buffer is returned. This byte is not included in
4143** the final value of (*pnByte), if applicable.
4144**
4145** NULL is returned if any error is encountered. The final value of *pnByte
4146** is undefined in this case.
4147*/
4148static char *readFile(const char *zName, int *pnByte){
4149  FILE *in = fopen(zName, "rb");
4150  long nIn;
4151  size_t nRead;
4152  char *pBuf;
4153  if( in==0 ) return 0;
4154  fseek(in, 0, SEEK_END);
4155  nIn = ftell(in);
4156  rewind(in);
4157  pBuf = sqlite3_malloc64( nIn+1 );
4158  if( pBuf==0 ){ fclose(in); return 0; }
4159  nRead = fread(pBuf, nIn, 1, in);
4160  fclose(in);
4161  if( nRead!=1 ){
4162    sqlite3_free(pBuf);
4163    return 0;
4164  }
4165  pBuf[nIn] = 0;
4166  if( pnByte ) *pnByte = nIn;
4167  return pBuf;
4168}
4169
4170#if defined(SQLITE_ENABLE_SESSION)
4171/*
4172** Close a single OpenSession object and release all of its associated
4173** resources.
4174*/
4175static void session_close(OpenSession *pSession){
4176  int i;
4177  sqlite3session_delete(pSession->p);
4178  sqlite3_free(pSession->zName);
4179  for(i=0; i<pSession->nFilter; i++){
4180    sqlite3_free(pSession->azFilter[i]);
4181  }
4182  sqlite3_free(pSession->azFilter);
4183  memset(pSession, 0, sizeof(OpenSession));
4184}
4185#endif
4186
4187/*
4188** Close all OpenSession objects and release all associated resources.
4189*/
4190#if defined(SQLITE_ENABLE_SESSION)
4191static void session_close_all(ShellState *p){
4192  int i;
4193  for(i=0; i<p->nSession; i++){
4194    session_close(&p->aSession[i]);
4195  }
4196  p->nSession = 0;
4197}
4198#else
4199# define session_close_all(X)
4200#endif
4201
4202/*
4203** Implementation of the xFilter function for an open session.  Omit
4204** any tables named by ".session filter" but let all other table through.
4205*/
4206#if defined(SQLITE_ENABLE_SESSION)
4207static int session_filter(void *pCtx, const char *zTab){
4208  OpenSession *pSession = (OpenSession*)pCtx;
4209  int i;
4210  for(i=0; i<pSession->nFilter; i++){
4211    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4212  }
4213  return 1;
4214}
4215#endif
4216
4217/*
4218** Try to deduce the type of file for zName based on its content.  Return
4219** one of the SHELL_OPEN_* constants.
4220**
4221** If the file does not exist or is empty but its name looks like a ZIP
4222** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4223** Otherwise, assume an ordinary database regardless of the filename if
4224** the type cannot be determined from content.
4225*/
4226int deduceDatabaseType(const char *zName, int dfltZip){
4227  FILE *f = fopen(zName, "rb");
4228  size_t n;
4229  int rc = SHELL_OPEN_UNSPEC;
4230  char zBuf[100];
4231  if( f==0 ){
4232    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4233       return SHELL_OPEN_ZIPFILE;
4234    }else{
4235       return SHELL_OPEN_NORMAL;
4236    }
4237  }
4238  n = fread(zBuf, 16, 1, f);
4239  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4240    fclose(f);
4241    return SHELL_OPEN_NORMAL;
4242  }
4243  fseek(f, -25, SEEK_END);
4244  n = fread(zBuf, 25, 1, f);
4245  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4246    rc = SHELL_OPEN_APPENDVFS;
4247  }else{
4248    fseek(f, -22, SEEK_END);
4249    n = fread(zBuf, 22, 1, f);
4250    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4251       && zBuf[3]==0x06 ){
4252      rc = SHELL_OPEN_ZIPFILE;
4253    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4254      rc = SHELL_OPEN_ZIPFILE;
4255    }
4256  }
4257  fclose(f);
4258  return rc;
4259}
4260
4261#ifdef SQLITE_ENABLE_DESERIALIZE
4262/*
4263** Reconstruct an in-memory database using the output from the "dbtotxt"
4264** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4265** is 0, then read from standard input.
4266*/
4267static unsigned char *readHexDb(ShellState *p, int *pnData){
4268  unsigned char *a = 0;
4269  int nLine;
4270  int n = 0;
4271  int pgsz = 0;
4272  int iOffset = 0;
4273  int j, k;
4274  int rc;
4275  FILE *in;
4276  unsigned int x[16];
4277  char zLine[1000];
4278  if( p->zDbFilename ){
4279    in = fopen(p->zDbFilename, "r");
4280    if( in==0 ){
4281      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4282      return 0;
4283    }
4284    nLine = 0;
4285  }else{
4286    in = p->in;
4287    nLine = p->lineno;
4288    if( in==0 ) in = stdin;
4289  }
4290  *pnData = 0;
4291  nLine++;
4292  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4293  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4294  if( rc!=2 ) goto readHexDb_error;
4295  if( n<0 ) goto readHexDb_error;
4296  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4297  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4298  a = sqlite3_malloc( n ? n : 1 );
4299  if( a==0 ){
4300    utf8_printf(stderr, "Out of memory!\n");
4301    goto readHexDb_error;
4302  }
4303  memset(a, 0, n);
4304  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4305    utf8_printf(stderr, "invalid pagesize\n");
4306    goto readHexDb_error;
4307  }
4308  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4309    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4310    if( rc==2 ){
4311      iOffset = k;
4312      continue;
4313    }
4314    if( strncmp(zLine, "| end ", 6)==0 ){
4315      break;
4316    }
4317    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4318                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4319                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4320    if( rc==17 ){
4321      k = iOffset+j;
4322      if( k+16<=n ){
4323        int ii;
4324        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4325      }
4326    }
4327  }
4328  *pnData = n;
4329  if( in!=p->in ){
4330    fclose(in);
4331  }else{
4332    p->lineno = nLine;
4333  }
4334  return a;
4335
4336readHexDb_error:
4337  if( in!=p->in ){
4338    fclose(in);
4339  }else{
4340    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4341      nLine++;
4342      if(strncmp(zLine, "| end ", 6)==0 ) break;
4343    }
4344    p->lineno = nLine;
4345  }
4346  sqlite3_free(a);
4347  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4348  return 0;
4349}
4350#endif /* SQLITE_ENABLE_DESERIALIZE */
4351
4352/*
4353** Scalar function "shell_int32". The first argument to this function
4354** must be a blob. The second a non-negative integer. This function
4355** reads and returns a 32-bit big-endian integer from byte
4356** offset (4*<arg2>) of the blob.
4357*/
4358static void shellInt32(
4359  sqlite3_context *context,
4360  int argc,
4361  sqlite3_value **argv
4362){
4363  const unsigned char *pBlob;
4364  int nBlob;
4365  int iInt;
4366
4367  UNUSED_PARAMETER(argc);
4368  nBlob = sqlite3_value_bytes(argv[0]);
4369  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4370  iInt = sqlite3_value_int(argv[1]);
4371
4372  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4373    const unsigned char *a = &pBlob[iInt*4];
4374    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4375                       + ((sqlite3_int64)a[1]<<16)
4376                       + ((sqlite3_int64)a[2]<< 8)
4377                       + ((sqlite3_int64)a[3]<< 0);
4378    sqlite3_result_int64(context, iVal);
4379  }
4380}
4381
4382/*
4383** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4384** using "..." with internal double-quote characters doubled.
4385*/
4386static void shellIdQuote(
4387  sqlite3_context *context,
4388  int argc,
4389  sqlite3_value **argv
4390){
4391  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4392  UNUSED_PARAMETER(argc);
4393  if( zName ){
4394    char *z = sqlite3_mprintf("\"%w\"", zName);
4395    sqlite3_result_text(context, z, -1, sqlite3_free);
4396  }
4397}
4398
4399/*
4400** Scalar function "shell_escape_crnl" used by the .recover command.
4401** The argument passed to this function is the output of built-in
4402** function quote(). If the first character of the input is "'",
4403** indicating that the value passed to quote() was a text value,
4404** then this function searches the input for "\n" and "\r" characters
4405** and adds a wrapper similar to the following:
4406**
4407**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4408**
4409** Or, if the first character of the input is not "'", then a copy
4410** of the input is returned.
4411*/
4412static void shellEscapeCrnl(
4413  sqlite3_context *context,
4414  int argc,
4415  sqlite3_value **argv
4416){
4417  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4418  UNUSED_PARAMETER(argc);
4419  if( zText[0]=='\'' ){
4420    int nText = sqlite3_value_bytes(argv[0]);
4421    int i;
4422    char zBuf1[20];
4423    char zBuf2[20];
4424    const char *zNL = 0;
4425    const char *zCR = 0;
4426    int nCR = 0;
4427    int nNL = 0;
4428
4429    for(i=0; zText[i]; i++){
4430      if( zNL==0 && zText[i]=='\n' ){
4431        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4432        nNL = (int)strlen(zNL);
4433      }
4434      if( zCR==0 && zText[i]=='\r' ){
4435        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4436        nCR = (int)strlen(zCR);
4437      }
4438    }
4439
4440    if( zNL || zCR ){
4441      int iOut = 0;
4442      i64 nMax = (nNL > nCR) ? nNL : nCR;
4443      i64 nAlloc = nMax * nText + (nMax+64)*2;
4444      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4445      if( zOut==0 ){
4446        sqlite3_result_error_nomem(context);
4447        return;
4448      }
4449
4450      if( zNL && zCR ){
4451        memcpy(&zOut[iOut], "replace(replace(", 16);
4452        iOut += 16;
4453      }else{
4454        memcpy(&zOut[iOut], "replace(", 8);
4455        iOut += 8;
4456      }
4457      for(i=0; zText[i]; i++){
4458        if( zText[i]=='\n' ){
4459          memcpy(&zOut[iOut], zNL, nNL);
4460          iOut += nNL;
4461        }else if( zText[i]=='\r' ){
4462          memcpy(&zOut[iOut], zCR, nCR);
4463          iOut += nCR;
4464        }else{
4465          zOut[iOut] = zText[i];
4466          iOut++;
4467        }
4468      }
4469
4470      if( zNL ){
4471        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4472        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4473        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4474      }
4475      if( zCR ){
4476        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4477        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4478        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4479      }
4480
4481      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4482      sqlite3_free(zOut);
4483      return;
4484    }
4485  }
4486
4487  sqlite3_result_value(context, argv[0]);
4488}
4489
4490/* Flags for open_db().
4491**
4492** The default behavior of open_db() is to exit(1) if the database fails to
4493** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4494** but still returns without calling exit.
4495**
4496** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4497** ZIP archive if the file does not exist or is empty and its name matches
4498** the *.zip pattern.
4499*/
4500#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4501#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4502
4503/*
4504** Make sure the database is open.  If it is not, then open it.  If
4505** the database fails to open, print an error message and exit.
4506*/
4507static void open_db(ShellState *p, int openFlags){
4508  if( p->db==0 ){
4509    if( p->openMode==SHELL_OPEN_UNSPEC ){
4510      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4511        p->openMode = SHELL_OPEN_NORMAL;
4512      }else{
4513        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4514                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4515      }
4516    }
4517    switch( p->openMode ){
4518      case SHELL_OPEN_APPENDVFS: {
4519        sqlite3_open_v2(p->zDbFilename, &p->db,
4520           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4521        break;
4522      }
4523      case SHELL_OPEN_HEXDB:
4524      case SHELL_OPEN_DESERIALIZE: {
4525        sqlite3_open(0, &p->db);
4526        break;
4527      }
4528      case SHELL_OPEN_ZIPFILE: {
4529        sqlite3_open(":memory:", &p->db);
4530        break;
4531      }
4532      case SHELL_OPEN_READONLY: {
4533        sqlite3_open_v2(p->zDbFilename, &p->db,
4534            SQLITE_OPEN_READONLY|p->openFlags, 0);
4535        break;
4536      }
4537      case SHELL_OPEN_UNSPEC:
4538      case SHELL_OPEN_NORMAL: {
4539        sqlite3_open_v2(p->zDbFilename, &p->db,
4540           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4541        break;
4542      }
4543    }
4544    globalDb = p->db;
4545    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4546      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4547          p->zDbFilename, sqlite3_errmsg(p->db));
4548      if( openFlags & OPEN_DB_KEEPALIVE ){
4549        sqlite3_open(":memory:", &p->db);
4550        return;
4551      }
4552      exit(1);
4553    }
4554#ifndef SQLITE_OMIT_LOAD_EXTENSION
4555    sqlite3_enable_load_extension(p->db, 1);
4556#endif
4557    sqlite3_fileio_init(p->db, 0, 0);
4558    sqlite3_shathree_init(p->db, 0, 0);
4559    sqlite3_completion_init(p->db, 0, 0);
4560    sqlite3_uint_init(p->db, 0, 0);
4561    sqlite3_decimal_init(p->db, 0, 0);
4562    sqlite3_ieee_init(p->db, 0, 0);
4563#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4564    sqlite3_dbdata_init(p->db, 0, 0);
4565#endif
4566#ifdef SQLITE_HAVE_ZLIB
4567    sqlite3_zipfile_init(p->db, 0, 0);
4568    sqlite3_sqlar_init(p->db, 0, 0);
4569#endif
4570    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4571                            shellAddSchemaName, 0, 0);
4572    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4573                            shellModuleSchema, 0, 0);
4574    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4575                            shellPutsFunc, 0, 0);
4576    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4577                            shellEscapeCrnl, 0, 0);
4578    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4579                            shellInt32, 0, 0);
4580    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4581                            shellIdQuote, 0, 0);
4582#ifndef SQLITE_NOHAVE_SYSTEM
4583    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4584                            editFunc, 0, 0);
4585    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4586                            editFunc, 0, 0);
4587#endif
4588    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4589      char *zSql = sqlite3_mprintf(
4590         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4591      sqlite3_exec(p->db, zSql, 0, 0, 0);
4592      sqlite3_free(zSql);
4593    }
4594#ifdef SQLITE_ENABLE_DESERIALIZE
4595    else
4596    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4597      int rc;
4598      int nData = 0;
4599      unsigned char *aData;
4600      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4601        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4602      }else{
4603        aData = readHexDb(p, &nData);
4604        if( aData==0 ){
4605          return;
4606        }
4607      }
4608      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4609                   SQLITE_DESERIALIZE_RESIZEABLE |
4610                   SQLITE_DESERIALIZE_FREEONCLOSE);
4611      if( rc ){
4612        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4613      }
4614      if( p->szMax>0 ){
4615        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4616      }
4617    }
4618#endif
4619  }
4620}
4621
4622/*
4623** Attempt to close the databaes connection.  Report errors.
4624*/
4625void close_db(sqlite3 *db){
4626  int rc = sqlite3_close(db);
4627  if( rc ){
4628    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4629        rc, sqlite3_errmsg(db));
4630  }
4631}
4632
4633#if HAVE_READLINE || HAVE_EDITLINE
4634/*
4635** Readline completion callbacks
4636*/
4637static char *readline_completion_generator(const char *text, int state){
4638  static sqlite3_stmt *pStmt = 0;
4639  char *zRet;
4640  if( state==0 ){
4641    char *zSql;
4642    sqlite3_finalize(pStmt);
4643    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4644                           "  FROM completion(%Q) ORDER BY 1", text);
4645    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4646    sqlite3_free(zSql);
4647  }
4648  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4649    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4650  }else{
4651    sqlite3_finalize(pStmt);
4652    pStmt = 0;
4653    zRet = 0;
4654  }
4655  return zRet;
4656}
4657static char **readline_completion(const char *zText, int iStart, int iEnd){
4658  rl_attempted_completion_over = 1;
4659  return rl_completion_matches(zText, readline_completion_generator);
4660}
4661
4662#elif HAVE_LINENOISE
4663/*
4664** Linenoise completion callback
4665*/
4666static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4667  int nLine = strlen30(zLine);
4668  int i, iStart;
4669  sqlite3_stmt *pStmt = 0;
4670  char *zSql;
4671  char zBuf[1000];
4672
4673  if( nLine>sizeof(zBuf)-30 ) return;
4674  if( zLine[0]=='.' || zLine[0]=='#') return;
4675  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4676  if( i==nLine-1 ) return;
4677  iStart = i+1;
4678  memcpy(zBuf, zLine, iStart);
4679  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4680                         "  FROM completion(%Q,%Q) ORDER BY 1",
4681                         &zLine[iStart], zLine);
4682  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4683  sqlite3_free(zSql);
4684  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4685  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4686    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4687    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4688    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4689      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4690      linenoiseAddCompletion(lc, zBuf);
4691    }
4692  }
4693  sqlite3_finalize(pStmt);
4694}
4695#endif
4696
4697/*
4698** Do C-language style dequoting.
4699**
4700**    \a    -> alarm
4701**    \b    -> backspace
4702**    \t    -> tab
4703**    \n    -> newline
4704**    \v    -> vertical tab
4705**    \f    -> form feed
4706**    \r    -> carriage return
4707**    \s    -> space
4708**    \"    -> "
4709**    \'    -> '
4710**    \\    -> backslash
4711**    \NNN  -> ascii character NNN in octal
4712*/
4713static void resolve_backslashes(char *z){
4714  int i, j;
4715  char c;
4716  while( *z && *z!='\\' ) z++;
4717  for(i=j=0; (c = z[i])!=0; i++, j++){
4718    if( c=='\\' && z[i+1]!=0 ){
4719      c = z[++i];
4720      if( c=='a' ){
4721        c = '\a';
4722      }else if( c=='b' ){
4723        c = '\b';
4724      }else if( c=='t' ){
4725        c = '\t';
4726      }else if( c=='n' ){
4727        c = '\n';
4728      }else if( c=='v' ){
4729        c = '\v';
4730      }else if( c=='f' ){
4731        c = '\f';
4732      }else if( c=='r' ){
4733        c = '\r';
4734      }else if( c=='"' ){
4735        c = '"';
4736      }else if( c=='\'' ){
4737        c = '\'';
4738      }else if( c=='\\' ){
4739        c = '\\';
4740      }else if( c>='0' && c<='7' ){
4741        c -= '0';
4742        if( z[i+1]>='0' && z[i+1]<='7' ){
4743          i++;
4744          c = (c<<3) + z[i] - '0';
4745          if( z[i+1]>='0' && z[i+1]<='7' ){
4746            i++;
4747            c = (c<<3) + z[i] - '0';
4748          }
4749        }
4750      }
4751    }
4752    z[j] = c;
4753  }
4754  if( j<i ) z[j] = 0;
4755}
4756
4757/*
4758** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4759** for TRUE and FALSE.  Return the integer value if appropriate.
4760*/
4761static int booleanValue(const char *zArg){
4762  int i;
4763  if( zArg[0]=='0' && zArg[1]=='x' ){
4764    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4765  }else{
4766    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4767  }
4768  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4769  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4770    return 1;
4771  }
4772  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4773    return 0;
4774  }
4775  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4776          zArg);
4777  return 0;
4778}
4779
4780/*
4781** Set or clear a shell flag according to a boolean value.
4782*/
4783static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4784  if( booleanValue(zArg) ){
4785    ShellSetFlag(p, mFlag);
4786  }else{
4787    ShellClearFlag(p, mFlag);
4788  }
4789}
4790
4791/*
4792** Close an output file, assuming it is not stderr or stdout
4793*/
4794static void output_file_close(FILE *f){
4795  if( f && f!=stdout && f!=stderr ) fclose(f);
4796}
4797
4798/*
4799** Try to open an output file.   The names "stdout" and "stderr" are
4800** recognized and do the right thing.  NULL is returned if the output
4801** filename is "off".
4802*/
4803static FILE *output_file_open(const char *zFile, int bTextMode){
4804  FILE *f;
4805  if( strcmp(zFile,"stdout")==0 ){
4806    f = stdout;
4807  }else if( strcmp(zFile, "stderr")==0 ){
4808    f = stderr;
4809  }else if( strcmp(zFile, "off")==0 ){
4810    f = 0;
4811  }else{
4812    f = fopen(zFile, bTextMode ? "w" : "wb");
4813    if( f==0 ){
4814      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4815    }
4816  }
4817  return f;
4818}
4819
4820#ifndef SQLITE_OMIT_TRACE
4821/*
4822** A routine for handling output from sqlite3_trace().
4823*/
4824static int sql_trace_callback(
4825  unsigned mType,         /* The trace type */
4826  void *pArg,             /* The ShellState pointer */
4827  void *pP,               /* Usually a pointer to sqlite_stmt */
4828  void *pX                /* Auxiliary output */
4829){
4830  ShellState *p = (ShellState*)pArg;
4831  sqlite3_stmt *pStmt;
4832  const char *zSql;
4833  int nSql;
4834  if( p->traceOut==0 ) return 0;
4835  if( mType==SQLITE_TRACE_CLOSE ){
4836    utf8_printf(p->traceOut, "-- closing database connection\n");
4837    return 0;
4838  }
4839  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4840    zSql = (const char*)pX;
4841  }else{
4842    pStmt = (sqlite3_stmt*)pP;
4843    switch( p->eTraceType ){
4844      case SHELL_TRACE_EXPANDED: {
4845        zSql = sqlite3_expanded_sql(pStmt);
4846        break;
4847      }
4848#ifdef SQLITE_ENABLE_NORMALIZE
4849      case SHELL_TRACE_NORMALIZED: {
4850        zSql = sqlite3_normalized_sql(pStmt);
4851        break;
4852      }
4853#endif
4854      default: {
4855        zSql = sqlite3_sql(pStmt);
4856        break;
4857      }
4858    }
4859  }
4860  if( zSql==0 ) return 0;
4861  nSql = strlen30(zSql);
4862  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4863  switch( mType ){
4864    case SQLITE_TRACE_ROW:
4865    case SQLITE_TRACE_STMT: {
4866      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4867      break;
4868    }
4869    case SQLITE_TRACE_PROFILE: {
4870      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4871      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4872      break;
4873    }
4874  }
4875  return 0;
4876}
4877#endif
4878
4879/*
4880** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4881** a useful spot to set a debugger breakpoint.
4882*/
4883static void test_breakpoint(void){
4884  static int nCall = 0;
4885  nCall++;
4886}
4887
4888/*
4889** An object used to read a CSV and other files for import.
4890*/
4891typedef struct ImportCtx ImportCtx;
4892struct ImportCtx {
4893  const char *zFile;  /* Name of the input file */
4894  FILE *in;           /* Read the CSV text from this input stream */
4895  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4896  char *z;            /* Accumulated text for a field */
4897  int n;              /* Number of bytes in z */
4898  int nAlloc;         /* Space allocated for z[] */
4899  int nLine;          /* Current line number */
4900  int nRow;           /* Number of rows imported */
4901  int nErr;           /* Number of errors encountered */
4902  int bNotFirst;      /* True if one or more bytes already read */
4903  int cTerm;          /* Character that terminated the most recent field */
4904  int cColSep;        /* The column separator character.  (Usually ",") */
4905  int cRowSep;        /* The row separator character.  (Usually "\n") */
4906};
4907
4908/* Clean up resourced used by an ImportCtx */
4909static void import_cleanup(ImportCtx *p){
4910  if( p->in!=0 && p->xCloser!=0 ){
4911    p->xCloser(p->in);
4912    p->in = 0;
4913  }
4914  sqlite3_free(p->z);
4915  p->z = 0;
4916}
4917
4918/* Append a single byte to z[] */
4919static void import_append_char(ImportCtx *p, int c){
4920  if( p->n+1>=p->nAlloc ){
4921    p->nAlloc += p->nAlloc + 100;
4922    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4923    if( p->z==0 ) shell_out_of_memory();
4924  }
4925  p->z[p->n++] = (char)c;
4926}
4927
4928/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4929** with the option of having a separator other than ",".
4930**
4931**   +  Input comes from p->in.
4932**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4933**      from sqlite3_malloc64().
4934**   +  Use p->cSep as the column separator.  The default is ",".
4935**   +  Use p->rSep as the row separator.  The default is "\n".
4936**   +  Keep track of the line number in p->nLine.
4937**   +  Store the character that terminates the field in p->cTerm.  Store
4938**      EOF on end-of-file.
4939**   +  Report syntax errors on stderr
4940*/
4941static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4942  int c;
4943  int cSep = p->cColSep;
4944  int rSep = p->cRowSep;
4945  p->n = 0;
4946  c = fgetc(p->in);
4947  if( c==EOF || seenInterrupt ){
4948    p->cTerm = EOF;
4949    return 0;
4950  }
4951  if( c=='"' ){
4952    int pc, ppc;
4953    int startLine = p->nLine;
4954    int cQuote = c;
4955    pc = ppc = 0;
4956    while( 1 ){
4957      c = fgetc(p->in);
4958      if( c==rSep ) p->nLine++;
4959      if( c==cQuote ){
4960        if( pc==cQuote ){
4961          pc = 0;
4962          continue;
4963        }
4964      }
4965      if( (c==cSep && pc==cQuote)
4966       || (c==rSep && pc==cQuote)
4967       || (c==rSep && pc=='\r' && ppc==cQuote)
4968       || (c==EOF && pc==cQuote)
4969      ){
4970        do{ p->n--; }while( p->z[p->n]!=cQuote );
4971        p->cTerm = c;
4972        break;
4973      }
4974      if( pc==cQuote && c!='\r' ){
4975        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
4976                p->zFile, p->nLine, cQuote);
4977      }
4978      if( c==EOF ){
4979        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
4980                p->zFile, startLine, cQuote);
4981        p->cTerm = c;
4982        break;
4983      }
4984      import_append_char(p, c);
4985      ppc = pc;
4986      pc = c;
4987    }
4988  }else{
4989    /* If this is the first field being parsed and it begins with the
4990    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
4991    if( (c&0xff)==0xef && p->bNotFirst==0 ){
4992      import_append_char(p, c);
4993      c = fgetc(p->in);
4994      if( (c&0xff)==0xbb ){
4995        import_append_char(p, c);
4996        c = fgetc(p->in);
4997        if( (c&0xff)==0xbf ){
4998          p->bNotFirst = 1;
4999          p->n = 0;
5000          return csv_read_one_field(p);
5001        }
5002      }
5003    }
5004    while( c!=EOF && c!=cSep && c!=rSep ){
5005      import_append_char(p, c);
5006      c = fgetc(p->in);
5007    }
5008    if( c==rSep ){
5009      p->nLine++;
5010      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5011    }
5012    p->cTerm = c;
5013  }
5014  if( p->z ) p->z[p->n] = 0;
5015  p->bNotFirst = 1;
5016  return p->z;
5017}
5018
5019/* Read a single field of ASCII delimited text.
5020**
5021**   +  Input comes from p->in.
5022**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5023**      from sqlite3_malloc64().
5024**   +  Use p->cSep as the column separator.  The default is "\x1F".
5025**   +  Use p->rSep as the row separator.  The default is "\x1E".
5026**   +  Keep track of the row number in p->nLine.
5027**   +  Store the character that terminates the field in p->cTerm.  Store
5028**      EOF on end-of-file.
5029**   +  Report syntax errors on stderr
5030*/
5031static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5032  int c;
5033  int cSep = p->cColSep;
5034  int rSep = p->cRowSep;
5035  p->n = 0;
5036  c = fgetc(p->in);
5037  if( c==EOF || seenInterrupt ){
5038    p->cTerm = EOF;
5039    return 0;
5040  }
5041  while( c!=EOF && c!=cSep && c!=rSep ){
5042    import_append_char(p, c);
5043    c = fgetc(p->in);
5044  }
5045  if( c==rSep ){
5046    p->nLine++;
5047  }
5048  p->cTerm = c;
5049  if( p->z ) p->z[p->n] = 0;
5050  return p->z;
5051}
5052
5053/*
5054** Try to transfer data for table zTable.  If an error is seen while
5055** moving forward, try to go backwards.  The backwards movement won't
5056** work for WITHOUT ROWID tables.
5057*/
5058static void tryToCloneData(
5059  ShellState *p,
5060  sqlite3 *newDb,
5061  const char *zTable
5062){
5063  sqlite3_stmt *pQuery = 0;
5064  sqlite3_stmt *pInsert = 0;
5065  char *zQuery = 0;
5066  char *zInsert = 0;
5067  int rc;
5068  int i, j, n;
5069  int nTable = strlen30(zTable);
5070  int k = 0;
5071  int cnt = 0;
5072  const int spinRate = 10000;
5073
5074  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5075  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5076  if( rc ){
5077    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5078            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5079            zQuery);
5080    goto end_data_xfer;
5081  }
5082  n = sqlite3_column_count(pQuery);
5083  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5084  if( zInsert==0 ) shell_out_of_memory();
5085  sqlite3_snprintf(200+nTable,zInsert,
5086                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5087  i = strlen30(zInsert);
5088  for(j=1; j<n; j++){
5089    memcpy(zInsert+i, ",?", 2);
5090    i += 2;
5091  }
5092  memcpy(zInsert+i, ");", 3);
5093  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5094  if( rc ){
5095    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5096            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5097            zQuery);
5098    goto end_data_xfer;
5099  }
5100  for(k=0; k<2; k++){
5101    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5102      for(i=0; i<n; i++){
5103        switch( sqlite3_column_type(pQuery, i) ){
5104          case SQLITE_NULL: {
5105            sqlite3_bind_null(pInsert, i+1);
5106            break;
5107          }
5108          case SQLITE_INTEGER: {
5109            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5110            break;
5111          }
5112          case SQLITE_FLOAT: {
5113            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5114            break;
5115          }
5116          case SQLITE_TEXT: {
5117            sqlite3_bind_text(pInsert, i+1,
5118                             (const char*)sqlite3_column_text(pQuery,i),
5119                             -1, SQLITE_STATIC);
5120            break;
5121          }
5122          case SQLITE_BLOB: {
5123            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5124                                            sqlite3_column_bytes(pQuery,i),
5125                                            SQLITE_STATIC);
5126            break;
5127          }
5128        }
5129      } /* End for */
5130      rc = sqlite3_step(pInsert);
5131      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5132        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5133                        sqlite3_errmsg(newDb));
5134      }
5135      sqlite3_reset(pInsert);
5136      cnt++;
5137      if( (cnt%spinRate)==0 ){
5138        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5139        fflush(stdout);
5140      }
5141    } /* End while */
5142    if( rc==SQLITE_DONE ) break;
5143    sqlite3_finalize(pQuery);
5144    sqlite3_free(zQuery);
5145    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5146                             zTable);
5147    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5148    if( rc ){
5149      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5150      break;
5151    }
5152  } /* End for(k=0...) */
5153
5154end_data_xfer:
5155  sqlite3_finalize(pQuery);
5156  sqlite3_finalize(pInsert);
5157  sqlite3_free(zQuery);
5158  sqlite3_free(zInsert);
5159}
5160
5161
5162/*
5163** Try to transfer all rows of the schema that match zWhere.  For
5164** each row, invoke xForEach() on the object defined by that row.
5165** If an error is encountered while moving forward through the
5166** sqlite_schema table, try again moving backwards.
5167*/
5168static void tryToCloneSchema(
5169  ShellState *p,
5170  sqlite3 *newDb,
5171  const char *zWhere,
5172  void (*xForEach)(ShellState*,sqlite3*,const char*)
5173){
5174  sqlite3_stmt *pQuery = 0;
5175  char *zQuery = 0;
5176  int rc;
5177  const unsigned char *zName;
5178  const unsigned char *zSql;
5179  char *zErrMsg = 0;
5180
5181  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5182                           " WHERE %s", zWhere);
5183  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5184  if( rc ){
5185    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5186                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5187                    zQuery);
5188    goto end_schema_xfer;
5189  }
5190  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5191    zName = sqlite3_column_text(pQuery, 0);
5192    zSql = sqlite3_column_text(pQuery, 1);
5193    printf("%s... ", zName); fflush(stdout);
5194    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5195    if( zErrMsg ){
5196      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5197      sqlite3_free(zErrMsg);
5198      zErrMsg = 0;
5199    }
5200    if( xForEach ){
5201      xForEach(p, newDb, (const char*)zName);
5202    }
5203    printf("done\n");
5204  }
5205  if( rc!=SQLITE_DONE ){
5206    sqlite3_finalize(pQuery);
5207    sqlite3_free(zQuery);
5208    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5209                             " WHERE %s ORDER BY rowid DESC", zWhere);
5210    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5211    if( rc ){
5212      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5213                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5214                      zQuery);
5215      goto end_schema_xfer;
5216    }
5217    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5218      zName = sqlite3_column_text(pQuery, 0);
5219      zSql = sqlite3_column_text(pQuery, 1);
5220      printf("%s... ", zName); fflush(stdout);
5221      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5222      if( zErrMsg ){
5223        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5224        sqlite3_free(zErrMsg);
5225        zErrMsg = 0;
5226      }
5227      if( xForEach ){
5228        xForEach(p, newDb, (const char*)zName);
5229      }
5230      printf("done\n");
5231    }
5232  }
5233end_schema_xfer:
5234  sqlite3_finalize(pQuery);
5235  sqlite3_free(zQuery);
5236}
5237
5238/*
5239** Open a new database file named "zNewDb".  Try to recover as much information
5240** as possible out of the main database (which might be corrupt) and write it
5241** into zNewDb.
5242*/
5243static void tryToClone(ShellState *p, const char *zNewDb){
5244  int rc;
5245  sqlite3 *newDb = 0;
5246  if( access(zNewDb,0)==0 ){
5247    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5248    return;
5249  }
5250  rc = sqlite3_open(zNewDb, &newDb);
5251  if( rc ){
5252    utf8_printf(stderr, "Cannot create output database: %s\n",
5253            sqlite3_errmsg(newDb));
5254  }else{
5255    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5256    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5257    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5258    tryToCloneSchema(p, newDb, "type!='table'", 0);
5259    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5260    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5261  }
5262  close_db(newDb);
5263}
5264
5265/*
5266** Change the output file back to stdout.
5267**
5268** If the p->doXdgOpen flag is set, that means the output was being
5269** redirected to a temporary file named by p->zTempFile.  In that case,
5270** launch start/open/xdg-open on that temporary file.
5271*/
5272static void output_reset(ShellState *p){
5273  if( p->outfile[0]=='|' ){
5274#ifndef SQLITE_OMIT_POPEN
5275    pclose(p->out);
5276#endif
5277  }else{
5278    output_file_close(p->out);
5279#ifndef SQLITE_NOHAVE_SYSTEM
5280    if( p->doXdgOpen ){
5281      const char *zXdgOpenCmd =
5282#if defined(_WIN32)
5283      "start";
5284#elif defined(__APPLE__)
5285      "open";
5286#else
5287      "xdg-open";
5288#endif
5289      char *zCmd;
5290      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5291      if( system(zCmd) ){
5292        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5293      }else{
5294        /* Give the start/open/xdg-open command some time to get
5295        ** going before we continue, and potential delete the
5296        ** p->zTempFile data file out from under it */
5297        sqlite3_sleep(2000);
5298      }
5299      sqlite3_free(zCmd);
5300      outputModePop(p);
5301      p->doXdgOpen = 0;
5302    }
5303#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5304  }
5305  p->outfile[0] = 0;
5306  p->out = stdout;
5307}
5308
5309/*
5310** Run an SQL command and return the single integer result.
5311*/
5312static int db_int(ShellState *p, const char *zSql){
5313  sqlite3_stmt *pStmt;
5314  int res = 0;
5315  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5316  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5317    res = sqlite3_column_int(pStmt,0);
5318  }
5319  sqlite3_finalize(pStmt);
5320  return res;
5321}
5322
5323/*
5324** Convert a 2-byte or 4-byte big-endian integer into a native integer
5325*/
5326static unsigned int get2byteInt(unsigned char *a){
5327  return (a[0]<<8) + a[1];
5328}
5329static unsigned int get4byteInt(unsigned char *a){
5330  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5331}
5332
5333/*
5334** Implementation of the ".dbinfo" command.
5335**
5336** Return 1 on error, 2 to exit, and 0 otherwise.
5337*/
5338static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5339  static const struct { const char *zName; int ofst; } aField[] = {
5340     { "file change counter:",  24  },
5341     { "database page count:",  28  },
5342     { "freelist page count:",  36  },
5343     { "schema cookie:",        40  },
5344     { "schema format:",        44  },
5345     { "default cache size:",   48  },
5346     { "autovacuum top root:",  52  },
5347     { "incremental vacuum:",   64  },
5348     { "text encoding:",        56  },
5349     { "user version:",         60  },
5350     { "application id:",       68  },
5351     { "software version:",     96  },
5352  };
5353  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5354     { "number of tables:",
5355       "SELECT count(*) FROM %s WHERE type='table'" },
5356     { "number of indexes:",
5357       "SELECT count(*) FROM %s WHERE type='index'" },
5358     { "number of triggers:",
5359       "SELECT count(*) FROM %s WHERE type='trigger'" },
5360     { "number of views:",
5361       "SELECT count(*) FROM %s WHERE type='view'" },
5362     { "schema size:",
5363       "SELECT total(length(sql)) FROM %s" },
5364  };
5365  int i, rc;
5366  unsigned iDataVersion;
5367  char *zSchemaTab;
5368  char *zDb = nArg>=2 ? azArg[1] : "main";
5369  sqlite3_stmt *pStmt = 0;
5370  unsigned char aHdr[100];
5371  open_db(p, 0);
5372  if( p->db==0 ) return 1;
5373  rc = sqlite3_prepare_v2(p->db,
5374             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5375             -1, &pStmt, 0);
5376  if( rc ){
5377    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5378    sqlite3_finalize(pStmt);
5379    return 1;
5380  }
5381  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5382  if( sqlite3_step(pStmt)==SQLITE_ROW
5383   && sqlite3_column_bytes(pStmt,0)>100
5384  ){
5385    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5386    sqlite3_finalize(pStmt);
5387  }else{
5388    raw_printf(stderr, "unable to read database header\n");
5389    sqlite3_finalize(pStmt);
5390    return 1;
5391  }
5392  i = get2byteInt(aHdr+16);
5393  if( i==1 ) i = 65536;
5394  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5395  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5396  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5397  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5398  for(i=0; i<ArraySize(aField); i++){
5399    int ofst = aField[i].ofst;
5400    unsigned int val = get4byteInt(aHdr + ofst);
5401    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5402    switch( ofst ){
5403      case 56: {
5404        if( val==1 ) raw_printf(p->out, " (utf8)");
5405        if( val==2 ) raw_printf(p->out, " (utf16le)");
5406        if( val==3 ) raw_printf(p->out, " (utf16be)");
5407      }
5408    }
5409    raw_printf(p->out, "\n");
5410  }
5411  if( zDb==0 ){
5412    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5413  }else if( strcmp(zDb,"temp")==0 ){
5414    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5415  }else{
5416    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5417  }
5418  for(i=0; i<ArraySize(aQuery); i++){
5419    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5420    int val = db_int(p, zSql);
5421    sqlite3_free(zSql);
5422    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5423  }
5424  sqlite3_free(zSchemaTab);
5425  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5426  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5427  return 0;
5428}
5429
5430/*
5431** Print the current sqlite3_errmsg() value to stderr and return 1.
5432*/
5433static int shellDatabaseError(sqlite3 *db){
5434  const char *zErr = sqlite3_errmsg(db);
5435  utf8_printf(stderr, "Error: %s\n", zErr);
5436  return 1;
5437}
5438
5439/*
5440** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5441** if they match and FALSE (0) if they do not match.
5442**
5443** Globbing rules:
5444**
5445**      '*'       Matches any sequence of zero or more characters.
5446**
5447**      '?'       Matches exactly one character.
5448**
5449**     [...]      Matches one character from the enclosed list of
5450**                characters.
5451**
5452**     [^...]     Matches one character not in the enclosed list.
5453**
5454**      '#'       Matches any sequence of one or more digits with an
5455**                optional + or - sign in front
5456**
5457**      ' '       Any span of whitespace matches any other span of
5458**                whitespace.
5459**
5460** Extra whitespace at the end of z[] is ignored.
5461*/
5462static int testcase_glob(const char *zGlob, const char *z){
5463  int c, c2;
5464  int invert;
5465  int seen;
5466
5467  while( (c = (*(zGlob++)))!=0 ){
5468    if( IsSpace(c) ){
5469      if( !IsSpace(*z) ) return 0;
5470      while( IsSpace(*zGlob) ) zGlob++;
5471      while( IsSpace(*z) ) z++;
5472    }else if( c=='*' ){
5473      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5474        if( c=='?' && (*(z++))==0 ) return 0;
5475      }
5476      if( c==0 ){
5477        return 1;
5478      }else if( c=='[' ){
5479        while( *z && testcase_glob(zGlob-1,z)==0 ){
5480          z++;
5481        }
5482        return (*z)!=0;
5483      }
5484      while( (c2 = (*(z++)))!=0 ){
5485        while( c2!=c ){
5486          c2 = *(z++);
5487          if( c2==0 ) return 0;
5488        }
5489        if( testcase_glob(zGlob,z) ) return 1;
5490      }
5491      return 0;
5492    }else if( c=='?' ){
5493      if( (*(z++))==0 ) return 0;
5494    }else if( c=='[' ){
5495      int prior_c = 0;
5496      seen = 0;
5497      invert = 0;
5498      c = *(z++);
5499      if( c==0 ) return 0;
5500      c2 = *(zGlob++);
5501      if( c2=='^' ){
5502        invert = 1;
5503        c2 = *(zGlob++);
5504      }
5505      if( c2==']' ){
5506        if( c==']' ) seen = 1;
5507        c2 = *(zGlob++);
5508      }
5509      while( c2 && c2!=']' ){
5510        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5511          c2 = *(zGlob++);
5512          if( c>=prior_c && c<=c2 ) seen = 1;
5513          prior_c = 0;
5514        }else{
5515          if( c==c2 ){
5516            seen = 1;
5517          }
5518          prior_c = c2;
5519        }
5520        c2 = *(zGlob++);
5521      }
5522      if( c2==0 || (seen ^ invert)==0 ) return 0;
5523    }else if( c=='#' ){
5524      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5525      if( !IsDigit(z[0]) ) return 0;
5526      z++;
5527      while( IsDigit(z[0]) ){ z++; }
5528    }else{
5529      if( c!=(*(z++)) ) return 0;
5530    }
5531  }
5532  while( IsSpace(*z) ){ z++; }
5533  return *z==0;
5534}
5535
5536
5537/*
5538** Compare the string as a command-line option with either one or two
5539** initial "-" characters.
5540*/
5541static int optionMatch(const char *zStr, const char *zOpt){
5542  if( zStr[0]!='-' ) return 0;
5543  zStr++;
5544  if( zStr[0]=='-' ) zStr++;
5545  return strcmp(zStr, zOpt)==0;
5546}
5547
5548/*
5549** Delete a file.
5550*/
5551int shellDeleteFile(const char *zFilename){
5552  int rc;
5553#ifdef _WIN32
5554  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5555  rc = _wunlink(z);
5556  sqlite3_free(z);
5557#else
5558  rc = unlink(zFilename);
5559#endif
5560  return rc;
5561}
5562
5563/*
5564** Try to delete the temporary file (if there is one) and free the
5565** memory used to hold the name of the temp file.
5566*/
5567static void clearTempFile(ShellState *p){
5568  if( p->zTempFile==0 ) return;
5569  if( p->doXdgOpen ) return;
5570  if( shellDeleteFile(p->zTempFile) ) return;
5571  sqlite3_free(p->zTempFile);
5572  p->zTempFile = 0;
5573}
5574
5575/*
5576** Create a new temp file name with the given suffix.
5577*/
5578static void newTempFile(ShellState *p, const char *zSuffix){
5579  clearTempFile(p);
5580  sqlite3_free(p->zTempFile);
5581  p->zTempFile = 0;
5582  if( p->db ){
5583    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5584  }
5585  if( p->zTempFile==0 ){
5586    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5587    ** will not work and we will need to fallback to guessing */
5588    char *zTemp;
5589    sqlite3_uint64 r;
5590    sqlite3_randomness(sizeof(r), &r);
5591    zTemp = getenv("TEMP");
5592    if( zTemp==0 ) zTemp = getenv("TMP");
5593    if( zTemp==0 ){
5594#ifdef _WIN32
5595      zTemp = "\\tmp";
5596#else
5597      zTemp = "/tmp";
5598#endif
5599    }
5600    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5601  }else{
5602    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5603  }
5604  if( p->zTempFile==0 ){
5605    raw_printf(stderr, "out of memory\n");
5606    exit(1);
5607  }
5608}
5609
5610
5611/*
5612** The implementation of SQL scalar function fkey_collate_clause(), used
5613** by the ".lint fkey-indexes" command. This scalar function is always
5614** called with four arguments - the parent table name, the parent column name,
5615** the child table name and the child column name.
5616**
5617**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5618**
5619** If either of the named tables or columns do not exist, this function
5620** returns an empty string. An empty string is also returned if both tables
5621** and columns exist but have the same default collation sequence. Or,
5622** if both exist but the default collation sequences are different, this
5623** function returns the string " COLLATE <parent-collation>", where
5624** <parent-collation> is the default collation sequence of the parent column.
5625*/
5626static void shellFkeyCollateClause(
5627  sqlite3_context *pCtx,
5628  int nVal,
5629  sqlite3_value **apVal
5630){
5631  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5632  const char *zParent;
5633  const char *zParentCol;
5634  const char *zParentSeq;
5635  const char *zChild;
5636  const char *zChildCol;
5637  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5638  int rc;
5639
5640  assert( nVal==4 );
5641  zParent = (const char*)sqlite3_value_text(apVal[0]);
5642  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5643  zChild = (const char*)sqlite3_value_text(apVal[2]);
5644  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5645
5646  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5647  rc = sqlite3_table_column_metadata(
5648      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5649  );
5650  if( rc==SQLITE_OK ){
5651    rc = sqlite3_table_column_metadata(
5652        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5653    );
5654  }
5655
5656  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5657    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5658    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5659    sqlite3_free(z);
5660  }
5661}
5662
5663
5664/*
5665** The implementation of dot-command ".lint fkey-indexes".
5666*/
5667static int lintFkeyIndexes(
5668  ShellState *pState,             /* Current shell tool state */
5669  char **azArg,                   /* Array of arguments passed to dot command */
5670  int nArg                        /* Number of entries in azArg[] */
5671){
5672  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5673  FILE *out = pState->out;        /* Stream to write non-error output to */
5674  int bVerbose = 0;               /* If -verbose is present */
5675  int bGroupByParent = 0;         /* If -groupbyparent is present */
5676  int i;                          /* To iterate through azArg[] */
5677  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5678  int rc;                         /* Return code */
5679  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5680
5681  /*
5682  ** This SELECT statement returns one row for each foreign key constraint
5683  ** in the schema of the main database. The column values are:
5684  **
5685  ** 0. The text of an SQL statement similar to:
5686  **
5687  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5688  **
5689  **    This SELECT is similar to the one that the foreign keys implementation
5690  **    needs to run internally on child tables. If there is an index that can
5691  **    be used to optimize this query, then it can also be used by the FK
5692  **    implementation to optimize DELETE or UPDATE statements on the parent
5693  **    table.
5694  **
5695  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5696  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5697  **    contains an index that can be used to optimize the query.
5698  **
5699  ** 2. Human readable text that describes the child table and columns. e.g.
5700  **
5701  **       "child_table(child_key1, child_key2)"
5702  **
5703  ** 3. Human readable text that describes the parent table and columns. e.g.
5704  **
5705  **       "parent_table(parent_key1, parent_key2)"
5706  **
5707  ** 4. A full CREATE INDEX statement for an index that could be used to
5708  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5709  **
5710  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5711  **
5712  ** 5. The name of the parent table.
5713  **
5714  ** These six values are used by the C logic below to generate the report.
5715  */
5716  const char *zSql =
5717  "SELECT "
5718    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5719    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5720    "  || fkey_collate_clause("
5721    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5722    ", "
5723    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5724    "  || group_concat('*=?', ' AND ') || ')'"
5725    ", "
5726    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5727    ", "
5728    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5729    ", "
5730    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5731    "  || ' ON ' || quote(s.name) || '('"
5732    "  || group_concat(quote(f.[from]) ||"
5733    "        fkey_collate_clause("
5734    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5735    "  || ');'"
5736    ", "
5737    "     f.[table] "
5738    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5739    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5740    "GROUP BY s.name, f.id "
5741    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5742  ;
5743  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5744
5745  for(i=2; i<nArg; i++){
5746    int n = strlen30(azArg[i]);
5747    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5748      bVerbose = 1;
5749    }
5750    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5751      bGroupByParent = 1;
5752      zIndent = "    ";
5753    }
5754    else{
5755      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5756          azArg[0], azArg[1]
5757      );
5758      return SQLITE_ERROR;
5759    }
5760  }
5761
5762  /* Register the fkey_collate_clause() SQL function */
5763  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5764      0, shellFkeyCollateClause, 0, 0
5765  );
5766
5767
5768  if( rc==SQLITE_OK ){
5769    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5770  }
5771  if( rc==SQLITE_OK ){
5772    sqlite3_bind_int(pSql, 1, bGroupByParent);
5773  }
5774
5775  if( rc==SQLITE_OK ){
5776    int rc2;
5777    char *zPrev = 0;
5778    while( SQLITE_ROW==sqlite3_step(pSql) ){
5779      int res = -1;
5780      sqlite3_stmt *pExplain = 0;
5781      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5782      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5783      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5784      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5785      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5786      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5787
5788      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5789      if( rc!=SQLITE_OK ) break;
5790      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5791        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5792        res = (
5793              0==sqlite3_strglob(zGlob, zPlan)
5794           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5795        );
5796      }
5797      rc = sqlite3_finalize(pExplain);
5798      if( rc!=SQLITE_OK ) break;
5799
5800      if( res<0 ){
5801        raw_printf(stderr, "Error: internal error");
5802        break;
5803      }else{
5804        if( bGroupByParent
5805        && (bVerbose || res==0)
5806        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5807        ){
5808          raw_printf(out, "-- Parent table %s\n", zParent);
5809          sqlite3_free(zPrev);
5810          zPrev = sqlite3_mprintf("%s", zParent);
5811        }
5812
5813        if( res==0 ){
5814          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5815        }else if( bVerbose ){
5816          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5817              zIndent, zFrom, zTarget
5818          );
5819        }
5820      }
5821    }
5822    sqlite3_free(zPrev);
5823
5824    if( rc!=SQLITE_OK ){
5825      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5826    }
5827
5828    rc2 = sqlite3_finalize(pSql);
5829    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5830      rc = rc2;
5831      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5832    }
5833  }else{
5834    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5835  }
5836
5837  return rc;
5838}
5839
5840/*
5841** Implementation of ".lint" dot command.
5842*/
5843static int lintDotCommand(
5844  ShellState *pState,             /* Current shell tool state */
5845  char **azArg,                   /* Array of arguments passed to dot command */
5846  int nArg                        /* Number of entries in azArg[] */
5847){
5848  int n;
5849  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5850  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5851  return lintFkeyIndexes(pState, azArg, nArg);
5852
5853 usage:
5854  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5855  raw_printf(stderr, "Where sub-commands are:\n");
5856  raw_printf(stderr, "    fkey-indexes\n");
5857  return SQLITE_ERROR;
5858}
5859
5860#if !defined SQLITE_OMIT_VIRTUALTABLE
5861static void shellPrepare(
5862  sqlite3 *db,
5863  int *pRc,
5864  const char *zSql,
5865  sqlite3_stmt **ppStmt
5866){
5867  *ppStmt = 0;
5868  if( *pRc==SQLITE_OK ){
5869    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5870    if( rc!=SQLITE_OK ){
5871      raw_printf(stderr, "sql error: %s (%d)\n",
5872          sqlite3_errmsg(db), sqlite3_errcode(db)
5873      );
5874      *pRc = rc;
5875    }
5876  }
5877}
5878
5879/*
5880** Create a prepared statement using printf-style arguments for the SQL.
5881**
5882** This routine is could be marked "static".  But it is not always used,
5883** depending on compile-time options.  By omitting the "static", we avoid
5884** nuisance compiler warnings about "defined but not used".
5885*/
5886void shellPreparePrintf(
5887  sqlite3 *db,
5888  int *pRc,
5889  sqlite3_stmt **ppStmt,
5890  const char *zFmt,
5891  ...
5892){
5893  *ppStmt = 0;
5894  if( *pRc==SQLITE_OK ){
5895    va_list ap;
5896    char *z;
5897    va_start(ap, zFmt);
5898    z = sqlite3_vmprintf(zFmt, ap);
5899    va_end(ap);
5900    if( z==0 ){
5901      *pRc = SQLITE_NOMEM;
5902    }else{
5903      shellPrepare(db, pRc, z, ppStmt);
5904      sqlite3_free(z);
5905    }
5906  }
5907}
5908
5909/* Finalize the prepared statement created using shellPreparePrintf().
5910**
5911** This routine is could be marked "static".  But it is not always used,
5912** depending on compile-time options.  By omitting the "static", we avoid
5913** nuisance compiler warnings about "defined but not used".
5914*/
5915void shellFinalize(
5916  int *pRc,
5917  sqlite3_stmt *pStmt
5918){
5919  if( pStmt ){
5920    sqlite3 *db = sqlite3_db_handle(pStmt);
5921    int rc = sqlite3_finalize(pStmt);
5922    if( *pRc==SQLITE_OK ){
5923      if( rc!=SQLITE_OK ){
5924        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5925      }
5926      *pRc = rc;
5927    }
5928  }
5929}
5930
5931/* Reset the prepared statement created using shellPreparePrintf().
5932**
5933** This routine is could be marked "static".  But it is not always used,
5934** depending on compile-time options.  By omitting the "static", we avoid
5935** nuisance compiler warnings about "defined but not used".
5936*/
5937void shellReset(
5938  int *pRc,
5939  sqlite3_stmt *pStmt
5940){
5941  int rc = sqlite3_reset(pStmt);
5942  if( *pRc==SQLITE_OK ){
5943    if( rc!=SQLITE_OK ){
5944      sqlite3 *db = sqlite3_db_handle(pStmt);
5945      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5946    }
5947    *pRc = rc;
5948  }
5949}
5950#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5951
5952#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5953/******************************************************************************
5954** The ".archive" or ".ar" command.
5955*/
5956/*
5957** Structure representing a single ".ar" command.
5958*/
5959typedef struct ArCommand ArCommand;
5960struct ArCommand {
5961  u8 eCmd;                        /* An AR_CMD_* value */
5962  u8 bVerbose;                    /* True if --verbose */
5963  u8 bZip;                        /* True if the archive is a ZIP */
5964  u8 bDryRun;                     /* True if --dry-run */
5965  u8 bAppend;                     /* True if --append */
5966  u8 fromCmdLine;                 /* Run from -A instead of .archive */
5967  int nArg;                       /* Number of command arguments */
5968  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
5969  const char *zFile;              /* --file argument, or NULL */
5970  const char *zDir;               /* --directory argument, or NULL */
5971  char **azArg;                   /* Array of command arguments */
5972  ShellState *p;                  /* Shell state */
5973  sqlite3 *db;                    /* Database containing the archive */
5974};
5975
5976/*
5977** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
5978*/
5979static int arUsage(FILE *f){
5980  showHelp(f,"archive");
5981  return SQLITE_ERROR;
5982}
5983
5984/*
5985** Print an error message for the .ar command to stderr and return
5986** SQLITE_ERROR.
5987*/
5988static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
5989  va_list ap;
5990  char *z;
5991  va_start(ap, zFmt);
5992  z = sqlite3_vmprintf(zFmt, ap);
5993  va_end(ap);
5994  utf8_printf(stderr, "Error: %s\n", z);
5995  if( pAr->fromCmdLine ){
5996    utf8_printf(stderr, "Use \"-A\" for more help\n");
5997  }else{
5998    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
5999  }
6000  sqlite3_free(z);
6001  return SQLITE_ERROR;
6002}
6003
6004/*
6005** Values for ArCommand.eCmd.
6006*/
6007#define AR_CMD_CREATE       1
6008#define AR_CMD_UPDATE       2
6009#define AR_CMD_INSERT       3
6010#define AR_CMD_EXTRACT      4
6011#define AR_CMD_LIST         5
6012#define AR_CMD_HELP         6
6013
6014/*
6015** Other (non-command) switches.
6016*/
6017#define AR_SWITCH_VERBOSE     7
6018#define AR_SWITCH_FILE        8
6019#define AR_SWITCH_DIRECTORY   9
6020#define AR_SWITCH_APPEND     10
6021#define AR_SWITCH_DRYRUN     11
6022
6023static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6024  switch( eSwitch ){
6025    case AR_CMD_CREATE:
6026    case AR_CMD_EXTRACT:
6027    case AR_CMD_LIST:
6028    case AR_CMD_UPDATE:
6029    case AR_CMD_INSERT:
6030    case AR_CMD_HELP:
6031      if( pAr->eCmd ){
6032        return arErrorMsg(pAr, "multiple command options");
6033      }
6034      pAr->eCmd = eSwitch;
6035      break;
6036
6037    case AR_SWITCH_DRYRUN:
6038      pAr->bDryRun = 1;
6039      break;
6040    case AR_SWITCH_VERBOSE:
6041      pAr->bVerbose = 1;
6042      break;
6043    case AR_SWITCH_APPEND:
6044      pAr->bAppend = 1;
6045      /* Fall thru into --file */
6046    case AR_SWITCH_FILE:
6047      pAr->zFile = zArg;
6048      break;
6049    case AR_SWITCH_DIRECTORY:
6050      pAr->zDir = zArg;
6051      break;
6052  }
6053
6054  return SQLITE_OK;
6055}
6056
6057/*
6058** Parse the command line for an ".ar" command. The results are written into
6059** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6060** successfully, otherwise an error message is written to stderr and
6061** SQLITE_ERROR returned.
6062*/
6063static int arParseCommand(
6064  char **azArg,                   /* Array of arguments passed to dot command */
6065  int nArg,                       /* Number of entries in azArg[] */
6066  ArCommand *pAr                  /* Populate this object */
6067){
6068  struct ArSwitch {
6069    const char *zLong;
6070    char cShort;
6071    u8 eSwitch;
6072    u8 bArg;
6073  } aSwitch[] = {
6074    { "create",    'c', AR_CMD_CREATE,       0 },
6075    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6076    { "insert",    'i', AR_CMD_INSERT,       0 },
6077    { "list",      't', AR_CMD_LIST,         0 },
6078    { "update",    'u', AR_CMD_UPDATE,       0 },
6079    { "help",      'h', AR_CMD_HELP,         0 },
6080    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6081    { "file",      'f', AR_SWITCH_FILE,      1 },
6082    { "append",    'a', AR_SWITCH_APPEND,    1 },
6083    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6084    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6085  };
6086  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6087  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6088
6089  if( nArg<=1 ){
6090    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6091    return arUsage(stderr);
6092  }else{
6093    char *z = azArg[1];
6094    if( z[0]!='-' ){
6095      /* Traditional style [tar] invocation */
6096      int i;
6097      int iArg = 2;
6098      for(i=0; z[i]; i++){
6099        const char *zArg = 0;
6100        struct ArSwitch *pOpt;
6101        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6102          if( z[i]==pOpt->cShort ) break;
6103        }
6104        if( pOpt==pEnd ){
6105          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6106        }
6107        if( pOpt->bArg ){
6108          if( iArg>=nArg ){
6109            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6110          }
6111          zArg = azArg[iArg++];
6112        }
6113        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6114      }
6115      pAr->nArg = nArg-iArg;
6116      if( pAr->nArg>0 ){
6117        pAr->azArg = &azArg[iArg];
6118      }
6119    }else{
6120      /* Non-traditional invocation */
6121      int iArg;
6122      for(iArg=1; iArg<nArg; iArg++){
6123        int n;
6124        z = azArg[iArg];
6125        if( z[0]!='-' ){
6126          /* All remaining command line words are command arguments. */
6127          pAr->azArg = &azArg[iArg];
6128          pAr->nArg = nArg-iArg;
6129          break;
6130        }
6131        n = strlen30(z);
6132
6133        if( z[1]!='-' ){
6134          int i;
6135          /* One or more short options */
6136          for(i=1; i<n; i++){
6137            const char *zArg = 0;
6138            struct ArSwitch *pOpt;
6139            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6140              if( z[i]==pOpt->cShort ) break;
6141            }
6142            if( pOpt==pEnd ){
6143              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6144            }
6145            if( pOpt->bArg ){
6146              if( i<(n-1) ){
6147                zArg = &z[i+1];
6148                i = n;
6149              }else{
6150                if( iArg>=(nArg-1) ){
6151                  return arErrorMsg(pAr, "option requires an argument: %c",
6152                                    z[i]);
6153                }
6154                zArg = azArg[++iArg];
6155              }
6156            }
6157            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6158          }
6159        }else if( z[2]=='\0' ){
6160          /* A -- option, indicating that all remaining command line words
6161          ** are command arguments.  */
6162          pAr->azArg = &azArg[iArg+1];
6163          pAr->nArg = nArg-iArg-1;
6164          break;
6165        }else{
6166          /* A long option */
6167          const char *zArg = 0;             /* Argument for option, if any */
6168          struct ArSwitch *pMatch = 0;      /* Matching option */
6169          struct ArSwitch *pOpt;            /* Iterator */
6170          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6171            const char *zLong = pOpt->zLong;
6172            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6173              if( pMatch ){
6174                return arErrorMsg(pAr, "ambiguous option: %s",z);
6175              }else{
6176                pMatch = pOpt;
6177              }
6178            }
6179          }
6180
6181          if( pMatch==0 ){
6182            return arErrorMsg(pAr, "unrecognized option: %s", z);
6183          }
6184          if( pMatch->bArg ){
6185            if( iArg>=(nArg-1) ){
6186              return arErrorMsg(pAr, "option requires an argument: %s", z);
6187            }
6188            zArg = azArg[++iArg];
6189          }
6190          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6191        }
6192      }
6193    }
6194  }
6195
6196  return SQLITE_OK;
6197}
6198
6199/*
6200** This function assumes that all arguments within the ArCommand.azArg[]
6201** array refer to archive members, as for the --extract or --list commands.
6202** It checks that each of them are present. If any specified file is not
6203** present in the archive, an error is printed to stderr and an error
6204** code returned. Otherwise, if all specified arguments are present in
6205** the archive, SQLITE_OK is returned.
6206**
6207** This function strips any trailing '/' characters from each argument.
6208** This is consistent with the way the [tar] command seems to work on
6209** Linux.
6210*/
6211static int arCheckEntries(ArCommand *pAr){
6212  int rc = SQLITE_OK;
6213  if( pAr->nArg ){
6214    int i, j;
6215    sqlite3_stmt *pTest = 0;
6216
6217    shellPreparePrintf(pAr->db, &rc, &pTest,
6218        "SELECT name FROM %s WHERE name=$name",
6219        pAr->zSrcTable
6220    );
6221    j = sqlite3_bind_parameter_index(pTest, "$name");
6222    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6223      char *z = pAr->azArg[i];
6224      int n = strlen30(z);
6225      int bOk = 0;
6226      while( n>0 && z[n-1]=='/' ) n--;
6227      z[n] = '\0';
6228      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6229      if( SQLITE_ROW==sqlite3_step(pTest) ){
6230        bOk = 1;
6231      }
6232      shellReset(&rc, pTest);
6233      if( rc==SQLITE_OK && bOk==0 ){
6234        utf8_printf(stderr, "not found in archive: %s\n", z);
6235        rc = SQLITE_ERROR;
6236      }
6237    }
6238    shellFinalize(&rc, pTest);
6239  }
6240  return rc;
6241}
6242
6243/*
6244** Format a WHERE clause that can be used against the "sqlar" table to
6245** identify all archive members that match the command arguments held
6246** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6247** The caller is responsible for eventually calling sqlite3_free() on
6248** any non-NULL (*pzWhere) value.
6249*/
6250static void arWhereClause(
6251  int *pRc,
6252  ArCommand *pAr,
6253  char **pzWhere                  /* OUT: New WHERE clause */
6254){
6255  char *zWhere = 0;
6256  if( *pRc==SQLITE_OK ){
6257    if( pAr->nArg==0 ){
6258      zWhere = sqlite3_mprintf("1");
6259    }else{
6260      int i;
6261      const char *zSep = "";
6262      for(i=0; i<pAr->nArg; i++){
6263        const char *z = pAr->azArg[i];
6264        zWhere = sqlite3_mprintf(
6265          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6266          zWhere, zSep, z, strlen30(z)+1, z
6267        );
6268        if( zWhere==0 ){
6269          *pRc = SQLITE_NOMEM;
6270          break;
6271        }
6272        zSep = " OR ";
6273      }
6274    }
6275  }
6276  *pzWhere = zWhere;
6277}
6278
6279/*
6280** Implementation of .ar "lisT" command.
6281*/
6282static int arListCommand(ArCommand *pAr){
6283  const char *zSql = "SELECT %s FROM %s WHERE %s";
6284  const char *azCols[] = {
6285    "name",
6286    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6287  };
6288
6289  char *zWhere = 0;
6290  sqlite3_stmt *pSql = 0;
6291  int rc;
6292
6293  rc = arCheckEntries(pAr);
6294  arWhereClause(&rc, pAr, &zWhere);
6295
6296  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6297                     pAr->zSrcTable, zWhere);
6298  if( pAr->bDryRun ){
6299    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6300  }else{
6301    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6302      if( pAr->bVerbose ){
6303        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6304            sqlite3_column_text(pSql, 0),
6305            sqlite3_column_int(pSql, 1),
6306            sqlite3_column_text(pSql, 2),
6307            sqlite3_column_text(pSql, 3)
6308        );
6309      }else{
6310        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6311      }
6312    }
6313  }
6314  shellFinalize(&rc, pSql);
6315  sqlite3_free(zWhere);
6316  return rc;
6317}
6318
6319
6320/*
6321** Implementation of .ar "eXtract" command.
6322*/
6323static int arExtractCommand(ArCommand *pAr){
6324  const char *zSql1 =
6325    "SELECT "
6326    " ($dir || name),"
6327    " writefile(($dir || name), %s, mode, mtime) "
6328    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6329    " AND name NOT GLOB '*..[/\\]*'";
6330
6331  const char *azExtraArg[] = {
6332    "sqlar_uncompress(data, sz)",
6333    "data"
6334  };
6335
6336  sqlite3_stmt *pSql = 0;
6337  int rc = SQLITE_OK;
6338  char *zDir = 0;
6339  char *zWhere = 0;
6340  int i, j;
6341
6342  /* If arguments are specified, check that they actually exist within
6343  ** the archive before proceeding. And formulate a WHERE clause to
6344  ** match them.  */
6345  rc = arCheckEntries(pAr);
6346  arWhereClause(&rc, pAr, &zWhere);
6347
6348  if( rc==SQLITE_OK ){
6349    if( pAr->zDir ){
6350      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6351    }else{
6352      zDir = sqlite3_mprintf("");
6353    }
6354    if( zDir==0 ) rc = SQLITE_NOMEM;
6355  }
6356
6357  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6358      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6359  );
6360
6361  if( rc==SQLITE_OK ){
6362    j = sqlite3_bind_parameter_index(pSql, "$dir");
6363    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6364
6365    /* Run the SELECT statement twice. The first time, writefile() is called
6366    ** for all archive members that should be extracted. The second time,
6367    ** only for the directories. This is because the timestamps for
6368    ** extracted directories must be reset after they are populated (as
6369    ** populating them changes the timestamp).  */
6370    for(i=0; i<2; i++){
6371      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6372      sqlite3_bind_int(pSql, j, i);
6373      if( pAr->bDryRun ){
6374        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6375      }else{
6376        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6377          if( i==0 && pAr->bVerbose ){
6378            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6379          }
6380        }
6381      }
6382      shellReset(&rc, pSql);
6383    }
6384    shellFinalize(&rc, pSql);
6385  }
6386
6387  sqlite3_free(zDir);
6388  sqlite3_free(zWhere);
6389  return rc;
6390}
6391
6392/*
6393** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6394*/
6395static int arExecSql(ArCommand *pAr, const char *zSql){
6396  int rc;
6397  if( pAr->bDryRun ){
6398    utf8_printf(pAr->p->out, "%s\n", zSql);
6399    rc = SQLITE_OK;
6400  }else{
6401    char *zErr = 0;
6402    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6403    if( zErr ){
6404      utf8_printf(stdout, "ERROR: %s\n", zErr);
6405      sqlite3_free(zErr);
6406    }
6407  }
6408  return rc;
6409}
6410
6411
6412/*
6413** Implementation of .ar "create", "insert", and "update" commands.
6414**
6415**     create    ->     Create a new SQL archive
6416**     insert    ->     Insert or reinsert all files listed
6417**     update    ->     Insert files that have changed or that were not
6418**                      previously in the archive
6419**
6420** Create the "sqlar" table in the database if it does not already exist.
6421** Then add each file in the azFile[] array to the archive. Directories
6422** are added recursively. If argument bVerbose is non-zero, a message is
6423** printed on stdout for each file archived.
6424**
6425** The create command is the same as update, except that it drops
6426** any existing "sqlar" table before beginning.  The "insert" command
6427** always overwrites every file named on the command-line, where as
6428** "update" only overwrites if the size or mtime or mode has changed.
6429*/
6430static int arCreateOrUpdateCommand(
6431  ArCommand *pAr,                 /* Command arguments and options */
6432  int bUpdate,                    /* true for a --create. */
6433  int bOnlyIfChanged              /* Only update if file has changed */
6434){
6435  const char *zCreate =
6436      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6437      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6438      "  mode INT,               -- access permissions\n"
6439      "  mtime INT,              -- last modification time\n"
6440      "  sz INT,                 -- original file size\n"
6441      "  data BLOB               -- compressed content\n"
6442      ")";
6443  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6444  const char *zInsertFmt[2] = {
6445     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6446     "  SELECT\n"
6447     "    %s,\n"
6448     "    mode,\n"
6449     "    mtime,\n"
6450     "    CASE substr(lsmode(mode),1,1)\n"
6451     "      WHEN '-' THEN length(data)\n"
6452     "      WHEN 'd' THEN 0\n"
6453     "      ELSE -1 END,\n"
6454     "    sqlar_compress(data)\n"
6455     "  FROM fsdir(%Q,%Q) AS disk\n"
6456     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6457     ,
6458     "REPLACE INTO %s(name,mode,mtime,data)\n"
6459     "  SELECT\n"
6460     "    %s,\n"
6461     "    mode,\n"
6462     "    mtime,\n"
6463     "    data\n"
6464     "  FROM fsdir(%Q,%Q) AS disk\n"
6465     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6466  };
6467  int i;                          /* For iterating through azFile[] */
6468  int rc;                         /* Return code */
6469  const char *zTab = 0;           /* SQL table into which to insert */
6470  char *zSql;
6471  char zTemp[50];
6472  char *zExists = 0;
6473
6474  arExecSql(pAr, "PRAGMA page_size=512");
6475  rc = arExecSql(pAr, "SAVEPOINT ar;");
6476  if( rc!=SQLITE_OK ) return rc;
6477  zTemp[0] = 0;
6478  if( pAr->bZip ){
6479    /* Initialize the zipfile virtual table, if necessary */
6480    if( pAr->zFile ){
6481      sqlite3_uint64 r;
6482      sqlite3_randomness(sizeof(r),&r);
6483      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6484      zTab = zTemp;
6485      zSql = sqlite3_mprintf(
6486         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6487         zTab, pAr->zFile
6488      );
6489      rc = arExecSql(pAr, zSql);
6490      sqlite3_free(zSql);
6491    }else{
6492      zTab = "zip";
6493    }
6494  }else{
6495    /* Initialize the table for an SQLAR */
6496    zTab = "sqlar";
6497    if( bUpdate==0 ){
6498      rc = arExecSql(pAr, zDrop);
6499      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6500    }
6501    rc = arExecSql(pAr, zCreate);
6502  }
6503  if( bOnlyIfChanged ){
6504    zExists = sqlite3_mprintf(
6505      " AND NOT EXISTS("
6506          "SELECT 1 FROM %s AS mem"
6507          " WHERE mem.name=disk.name"
6508          " AND mem.mtime=disk.mtime"
6509          " AND mem.mode=disk.mode)", zTab);
6510  }else{
6511    zExists = sqlite3_mprintf("");
6512  }
6513  if( zExists==0 ) rc = SQLITE_NOMEM;
6514  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6515    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6516        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6517        pAr->azArg[i], pAr->zDir, zExists);
6518    rc = arExecSql(pAr, zSql2);
6519    sqlite3_free(zSql2);
6520  }
6521end_ar_transaction:
6522  if( rc!=SQLITE_OK ){
6523    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6524  }else{
6525    rc = arExecSql(pAr, "RELEASE ar;");
6526    if( pAr->bZip && pAr->zFile ){
6527      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6528      arExecSql(pAr, zSql);
6529      sqlite3_free(zSql);
6530    }
6531  }
6532  sqlite3_free(zExists);
6533  return rc;
6534}
6535
6536/*
6537** Implementation of ".ar" dot command.
6538*/
6539static int arDotCommand(
6540  ShellState *pState,          /* Current shell tool state */
6541  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6542  char **azArg,                /* Array of arguments passed to dot command */
6543  int nArg                     /* Number of entries in azArg[] */
6544){
6545  ArCommand cmd;
6546  int rc;
6547  memset(&cmd, 0, sizeof(cmd));
6548  cmd.fromCmdLine = fromCmdLine;
6549  rc = arParseCommand(azArg, nArg, &cmd);
6550  if( rc==SQLITE_OK ){
6551    int eDbType = SHELL_OPEN_UNSPEC;
6552    cmd.p = pState;
6553    cmd.db = pState->db;
6554    if( cmd.zFile ){
6555      eDbType = deduceDatabaseType(cmd.zFile, 1);
6556    }else{
6557      eDbType = pState->openMode;
6558    }
6559    if( eDbType==SHELL_OPEN_ZIPFILE ){
6560      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6561        if( cmd.zFile==0 ){
6562          cmd.zSrcTable = sqlite3_mprintf("zip");
6563        }else{
6564          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6565        }
6566      }
6567      cmd.bZip = 1;
6568    }else if( cmd.zFile ){
6569      int flags;
6570      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6571      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6572           || cmd.eCmd==AR_CMD_UPDATE ){
6573        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6574      }else{
6575        flags = SQLITE_OPEN_READONLY;
6576      }
6577      cmd.db = 0;
6578      if( cmd.bDryRun ){
6579        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6580             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6581      }
6582      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6583             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6584      if( rc!=SQLITE_OK ){
6585        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6586            cmd.zFile, sqlite3_errmsg(cmd.db)
6587        );
6588        goto end_ar_command;
6589      }
6590      sqlite3_fileio_init(cmd.db, 0, 0);
6591      sqlite3_sqlar_init(cmd.db, 0, 0);
6592      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6593                              shellPutsFunc, 0, 0);
6594
6595    }
6596    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6597      if( cmd.eCmd!=AR_CMD_CREATE
6598       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6599      ){
6600        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6601        rc = SQLITE_ERROR;
6602        goto end_ar_command;
6603      }
6604      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6605    }
6606
6607    switch( cmd.eCmd ){
6608      case AR_CMD_CREATE:
6609        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6610        break;
6611
6612      case AR_CMD_EXTRACT:
6613        rc = arExtractCommand(&cmd);
6614        break;
6615
6616      case AR_CMD_LIST:
6617        rc = arListCommand(&cmd);
6618        break;
6619
6620      case AR_CMD_HELP:
6621        arUsage(pState->out);
6622        break;
6623
6624      case AR_CMD_INSERT:
6625        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6626        break;
6627
6628      default:
6629        assert( cmd.eCmd==AR_CMD_UPDATE );
6630        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6631        break;
6632    }
6633  }
6634end_ar_command:
6635  if( cmd.db!=pState->db ){
6636    close_db(cmd.db);
6637  }
6638  sqlite3_free(cmd.zSrcTable);
6639
6640  return rc;
6641}
6642/* End of the ".archive" or ".ar" command logic
6643*******************************************************************************/
6644#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6645
6646#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6647/*
6648** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6649** Otherwise, the SQL statement or statements in zSql are executed using
6650** database connection db and the error code written to *pRc before
6651** this function returns.
6652*/
6653static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6654  int rc = *pRc;
6655  if( rc==SQLITE_OK ){
6656    char *zErr = 0;
6657    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6658    if( rc!=SQLITE_OK ){
6659      raw_printf(stderr, "SQL error: %s\n", zErr);
6660    }
6661    *pRc = rc;
6662  }
6663}
6664
6665/*
6666** Like shellExec(), except that zFmt is a printf() style format string.
6667*/
6668static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6669  char *z = 0;
6670  if( *pRc==SQLITE_OK ){
6671    va_list ap;
6672    va_start(ap, zFmt);
6673    z = sqlite3_vmprintf(zFmt, ap);
6674    va_end(ap);
6675    if( z==0 ){
6676      *pRc = SQLITE_NOMEM;
6677    }else{
6678      shellExec(db, pRc, z);
6679    }
6680    sqlite3_free(z);
6681  }
6682}
6683
6684/*
6685** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6686** Otherwise, an attempt is made to allocate, zero and return a pointer
6687** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6688** to SQLITE_NOMEM and NULL returned.
6689*/
6690static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6691  void *pRet = 0;
6692  if( *pRc==SQLITE_OK ){
6693    pRet = sqlite3_malloc64(nByte);
6694    if( pRet==0 ){
6695      *pRc = SQLITE_NOMEM;
6696    }else{
6697      memset(pRet, 0, nByte);
6698    }
6699  }
6700  return pRet;
6701}
6702
6703/*
6704** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6705** Otherwise, zFmt is treated as a printf() style string. The result of
6706** formatting it along with any trailing arguments is written into a
6707** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6708** It is the responsibility of the caller to eventually free this buffer
6709** using a call to sqlite3_free().
6710**
6711** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6712** pointer returned.
6713*/
6714static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6715  char *z = 0;
6716  if( *pRc==SQLITE_OK ){
6717    va_list ap;
6718    va_start(ap, zFmt);
6719    z = sqlite3_vmprintf(zFmt, ap);
6720    va_end(ap);
6721    if( z==0 ){
6722      *pRc = SQLITE_NOMEM;
6723    }
6724  }
6725  return z;
6726}
6727
6728/*
6729** When running the ".recover" command, each output table, and the special
6730** orphaned row table if it is required, is represented by an instance
6731** of the following struct.
6732*/
6733typedef struct RecoverTable RecoverTable;
6734struct RecoverTable {
6735  char *zQuoted;                  /* Quoted version of table name */
6736  int nCol;                       /* Number of columns in table */
6737  char **azlCol;                  /* Array of column lists */
6738  int iPk;                        /* Index of IPK column */
6739};
6740
6741/*
6742** Free a RecoverTable object allocated by recoverFindTable() or
6743** recoverOrphanTable().
6744*/
6745static void recoverFreeTable(RecoverTable *pTab){
6746  if( pTab ){
6747    sqlite3_free(pTab->zQuoted);
6748    if( pTab->azlCol ){
6749      int i;
6750      for(i=0; i<=pTab->nCol; i++){
6751        sqlite3_free(pTab->azlCol[i]);
6752      }
6753      sqlite3_free(pTab->azlCol);
6754    }
6755    sqlite3_free(pTab);
6756  }
6757}
6758
6759/*
6760** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6761** Otherwise, it allocates and returns a RecoverTable object based on the
6762** final four arguments passed to this function. It is the responsibility
6763** of the caller to eventually free the returned object using
6764** recoverFreeTable().
6765*/
6766static RecoverTable *recoverNewTable(
6767  int *pRc,                       /* IN/OUT: Error code */
6768  const char *zName,              /* Name of table */
6769  const char *zSql,               /* CREATE TABLE statement */
6770  int bIntkey,
6771  int nCol
6772){
6773  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6774  int rc = *pRc;
6775  RecoverTable *pTab = 0;
6776
6777  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6778  if( rc==SQLITE_OK ){
6779    int nSqlCol = 0;
6780    int bSqlIntkey = 0;
6781    sqlite3_stmt *pStmt = 0;
6782
6783    rc = sqlite3_open("", &dbtmp);
6784    if( rc==SQLITE_OK ){
6785      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6786                              shellIdQuote, 0, 0);
6787    }
6788    if( rc==SQLITE_OK ){
6789      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6790    }
6791    if( rc==SQLITE_OK ){
6792      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6793      if( rc==SQLITE_ERROR ){
6794        rc = SQLITE_OK;
6795        goto finished;
6796      }
6797    }
6798    shellPreparePrintf(dbtmp, &rc, &pStmt,
6799        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6800    );
6801    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6802      nSqlCol = sqlite3_column_int(pStmt, 0);
6803    }
6804    shellFinalize(&rc, pStmt);
6805
6806    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6807      goto finished;
6808    }
6809
6810    shellPreparePrintf(dbtmp, &rc, &pStmt,
6811      "SELECT ("
6812      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6813      ") FROM sqlite_schema WHERE name = %Q", zName
6814    );
6815    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6816      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6817    }
6818    shellFinalize(&rc, pStmt);
6819
6820    if( bIntkey==bSqlIntkey ){
6821      int i;
6822      const char *zPk = "_rowid_";
6823      sqlite3_stmt *pPkFinder = 0;
6824
6825      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6826      ** set zPk to the name of the PK column, and pTab->iPk to the index
6827      ** of the column, where columns are 0-numbered from left to right.
6828      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6829      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6830      pTab->iPk = -2;
6831      if( bIntkey ){
6832        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6833          "SELECT cid, name FROM pragma_table_info(%Q) "
6834          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6835          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6836          , zName, zName
6837        );
6838        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6839          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6840          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6841        }
6842      }
6843
6844      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6845      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6846      pTab->nCol = nSqlCol;
6847
6848      if( bIntkey ){
6849        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6850      }else{
6851        pTab->azlCol[0] = shellMPrintf(&rc, "");
6852      }
6853      i = 1;
6854      shellPreparePrintf(dbtmp, &rc, &pStmt,
6855          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6856          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6857          "FROM pragma_table_info(%Q)",
6858          bIntkey ? ", " : "", pTab->iPk,
6859          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6860          zName
6861      );
6862      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6863        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6864        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6865        i++;
6866      }
6867      shellFinalize(&rc, pStmt);
6868
6869      shellFinalize(&rc, pPkFinder);
6870    }
6871  }
6872
6873 finished:
6874  sqlite3_close(dbtmp);
6875  *pRc = rc;
6876  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6877    recoverFreeTable(pTab);
6878    pTab = 0;
6879  }
6880  return pTab;
6881}
6882
6883/*
6884** This function is called to search the schema recovered from the
6885** sqlite_schema table of the (possibly) corrupt database as part
6886** of a ".recover" command. Specifically, for a table with root page
6887** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6888** table must be a WITHOUT ROWID table, or if non-zero, not one of
6889** those.
6890**
6891** If a table is found, a (RecoverTable*) object is returned. Or, if
6892** no such table is found, but bIntkey is false and iRoot is the
6893** root page of an index in the recovered schema, then (*pbNoop) is
6894** set to true and NULL returned. Or, if there is no such table or
6895** index, NULL is returned and (*pbNoop) set to 0, indicating that
6896** the caller should write data to the orphans table.
6897*/
6898static RecoverTable *recoverFindTable(
6899  ShellState *pState,             /* Shell state object */
6900  int *pRc,                       /* IN/OUT: Error code */
6901  int iRoot,                      /* Root page of table */
6902  int bIntkey,                    /* True for an intkey table */
6903  int nCol,                       /* Number of columns in table */
6904  int *pbNoop                     /* OUT: True if iRoot is root of index */
6905){
6906  sqlite3_stmt *pStmt = 0;
6907  RecoverTable *pRet = 0;
6908  int bNoop = 0;
6909  const char *zSql = 0;
6910  const char *zName = 0;
6911
6912  /* Search the recovered schema for an object with root page iRoot. */
6913  shellPreparePrintf(pState->db, pRc, &pStmt,
6914      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6915  );
6916  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6917    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6918    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6919      bNoop = 1;
6920      break;
6921    }
6922    if( sqlite3_stricmp(zType, "table")==0 ){
6923      zName = (const char*)sqlite3_column_text(pStmt, 1);
6924      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6925      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6926      break;
6927    }
6928  }
6929
6930  shellFinalize(pRc, pStmt);
6931  *pbNoop = bNoop;
6932  return pRet;
6933}
6934
6935/*
6936** Return a RecoverTable object representing the orphans table.
6937*/
6938static RecoverTable *recoverOrphanTable(
6939  ShellState *pState,             /* Shell state object */
6940  int *pRc,                       /* IN/OUT: Error code */
6941  const char *zLostAndFound,      /* Base name for orphans table */
6942  int nCol                        /* Number of user data columns */
6943){
6944  RecoverTable *pTab = 0;
6945  if( nCol>=0 && *pRc==SQLITE_OK ){
6946    int i;
6947
6948    /* This block determines the name of the orphan table. The prefered
6949    ** name is zLostAndFound. But if that clashes with another name
6950    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6951    ** and so on until a non-clashing name is found.  */
6952    int iTab = 0;
6953    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6954    sqlite3_stmt *pTest = 0;
6955    shellPrepare(pState->db, pRc,
6956        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
6957    );
6958    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6959    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
6960      shellReset(pRc, pTest);
6961      sqlite3_free(zTab);
6962      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
6963      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6964    }
6965    shellFinalize(pRc, pTest);
6966
6967    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
6968    if( pTab ){
6969      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
6970      pTab->nCol = nCol;
6971      pTab->iPk = -2;
6972      if( nCol>0 ){
6973        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
6974        if( pTab->azlCol ){
6975          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
6976          for(i=nCol-1; i>=0; i--){
6977            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
6978          }
6979        }
6980      }
6981
6982      if( *pRc!=SQLITE_OK ){
6983        recoverFreeTable(pTab);
6984        pTab = 0;
6985      }else{
6986        raw_printf(pState->out,
6987            "CREATE TABLE %s(rootpgno INTEGER, "
6988            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
6989        );
6990        for(i=0; i<nCol; i++){
6991          raw_printf(pState->out, ", c%d", i);
6992        }
6993        raw_printf(pState->out, ");\n");
6994      }
6995    }
6996    sqlite3_free(zTab);
6997  }
6998  return pTab;
6999}
7000
7001/*
7002** This function is called to recover data from the database. A script
7003** to construct a new database containing all recovered data is output
7004** on stream pState->out.
7005*/
7006static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7007  int rc = SQLITE_OK;
7008  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7009  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7010  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7011  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7012  const char *zLostAndFound = "lost_and_found";
7013  int i;
7014  int nOrphan = -1;
7015  RecoverTable *pOrphan = 0;
7016
7017  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7018  int bRowids = 1;                /* 0 if --no-rowids */
7019  for(i=1; i<nArg; i++){
7020    char *z = azArg[i];
7021    int n;
7022    if( z[0]=='-' && z[1]=='-' ) z++;
7023    n = strlen30(z);
7024    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7025      bFreelist = 0;
7026    }else
7027    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7028      i++;
7029      zRecoveryDb = azArg[i];
7030    }else
7031    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7032      i++;
7033      zLostAndFound = azArg[i];
7034    }else
7035    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7036      bRowids = 0;
7037    }
7038    else{
7039      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7040      showHelp(pState->out, azArg[0]);
7041      return 1;
7042    }
7043  }
7044
7045  shellExecPrintf(pState->db, &rc,
7046    /* Attach an in-memory database named 'recovery'. Create an indexed
7047    ** cache of the sqlite_dbptr virtual table. */
7048    "PRAGMA writable_schema = on;"
7049    "ATTACH %Q AS recovery;"
7050    "DROP TABLE IF EXISTS recovery.dbptr;"
7051    "DROP TABLE IF EXISTS recovery.freelist;"
7052    "DROP TABLE IF EXISTS recovery.map;"
7053    "DROP TABLE IF EXISTS recovery.schema;"
7054    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7055  );
7056
7057  if( bFreelist ){
7058    shellExec(pState->db, &rc,
7059      "WITH trunk(pgno) AS ("
7060      "  SELECT shell_int32("
7061      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7062      "      WHERE x>0"
7063      "    UNION"
7064      "  SELECT shell_int32("
7065      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7066      "      FROM trunk WHERE x>0"
7067      "),"
7068      "freelist(data, n, freepgno) AS ("
7069      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7070      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7071      "    UNION ALL"
7072      "  SELECT data, n-1, shell_int32(data, 2+n) "
7073      "      FROM freelist WHERE n>=0"
7074      ")"
7075      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7076    );
7077  }
7078
7079  /* If this is an auto-vacuum database, add all pointer-map pages to
7080  ** the freelist table. Do this regardless of whether or not
7081  ** --freelist-corrupt was specified.  */
7082  shellExec(pState->db, &rc,
7083    "WITH ptrmap(pgno) AS ("
7084    "  SELECT 2 WHERE shell_int32("
7085    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7086    "  )"
7087    "    UNION ALL "
7088    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7089    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7090    ")"
7091    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7092  );
7093
7094  shellExec(pState->db, &rc,
7095    "CREATE TABLE recovery.dbptr("
7096    "      pgno, child, PRIMARY KEY(child, pgno)"
7097    ") WITHOUT ROWID;"
7098    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7099    "    SELECT * FROM sqlite_dbptr"
7100    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7101
7102    /* Delete any pointer to page 1. This ensures that page 1 is considered
7103    ** a root page, regardless of how corrupt the db is. */
7104    "DELETE FROM recovery.dbptr WHERE child = 1;"
7105
7106    /* Delete all pointers to any pages that have more than one pointer
7107    ** to them. Such pages will be treated as root pages when recovering
7108    ** data.  */
7109    "DELETE FROM recovery.dbptr WHERE child IN ("
7110    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7111    ");"
7112
7113    /* Create the "map" table that will (eventually) contain instructions
7114    ** for dealing with each page in the db that contains one or more
7115    ** records. */
7116    "CREATE TABLE recovery.map("
7117      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7118    ");"
7119
7120    /* Populate table [map]. If there are circular loops of pages in the
7121    ** database, the following adds all pages in such a loop to the map
7122    ** as individual root pages. This could be handled better.  */
7123    "WITH pages(i, maxlen) AS ("
7124    "  SELECT page_count, ("
7125    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7126    "  ) FROM pragma_page_count WHERE page_count>0"
7127    "    UNION ALL"
7128    "  SELECT i-1, ("
7129    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7130    "  ) FROM pages WHERE i>=2"
7131    ")"
7132    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7133    "  SELECT i, maxlen, NULL, ("
7134    "    WITH p(orig, pgno, parent) AS ("
7135    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7136    "        UNION "
7137    "      SELECT i, p.parent, "
7138    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7139    "    )"
7140    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7141    ") "
7142    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7143    "UPDATE recovery.map AS o SET intkey = ("
7144    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7145    ");"
7146
7147    /* Extract data from page 1 and any linked pages into table
7148    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7149    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7150    "INSERT INTO recovery.schema SELECT "
7151    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7152    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7153    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7154    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7155    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7156    "FROM sqlite_dbdata WHERE pgno IN ("
7157    "  SELECT pgno FROM recovery.map WHERE root=1"
7158    ")"
7159    "GROUP BY pgno, cell;"
7160    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7161  );
7162
7163  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7164  ** CREATE TABLE statements that extracted from the existing schema.  */
7165  if( rc==SQLITE_OK ){
7166    sqlite3_stmt *pStmt = 0;
7167    /* ".recover" might output content in an order which causes immediate
7168    ** foreign key constraints to be violated. So disable foreign-key
7169    ** constraint enforcement to prevent problems when running the output
7170    ** script. */
7171    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7172    raw_printf(pState->out, "BEGIN;\n");
7173    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7174    shellPrepare(pState->db, &rc,
7175        "SELECT sql FROM recovery.schema "
7176        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7177    );
7178    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7179      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7180      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7181          &zCreateTable[12]
7182      );
7183    }
7184    shellFinalize(&rc, pStmt);
7185  }
7186
7187  /* Figure out if an orphan table will be required. And if so, how many
7188  ** user columns it should contain */
7189  shellPrepare(pState->db, &rc,
7190      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7191      , &pLoop
7192  );
7193  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7194    nOrphan = sqlite3_column_int(pLoop, 0);
7195  }
7196  shellFinalize(&rc, pLoop);
7197  pLoop = 0;
7198
7199  shellPrepare(pState->db, &rc,
7200      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7201  );
7202
7203  shellPrepare(pState->db, &rc,
7204      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7205      "(case when (? AND field<0) then NULL else value end)"
7206      "), ', ')"
7207      ", min(field) "
7208      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7209      "GROUP BY cell", &pCells
7210  );
7211
7212  /* Loop through each root page. */
7213  shellPrepare(pState->db, &rc,
7214      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7215      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7216      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7217      ")", &pLoop
7218  );
7219  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7220    int iRoot = sqlite3_column_int(pLoop, 0);
7221    int bIntkey = sqlite3_column_int(pLoop, 1);
7222    int nCol = sqlite3_column_int(pLoop, 2);
7223    int bNoop = 0;
7224    RecoverTable *pTab;
7225
7226    assert( bIntkey==0 || bIntkey==1 );
7227    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7228    if( bNoop || rc ) continue;
7229    if( pTab==0 ){
7230      if( pOrphan==0 ){
7231        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7232      }
7233      pTab = pOrphan;
7234      if( pTab==0 ) break;
7235    }
7236
7237    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7238      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7239    }
7240    sqlite3_bind_int(pPages, 1, iRoot);
7241    if( bRowids==0 && pTab->iPk<0 ){
7242      sqlite3_bind_int(pCells, 1, 1);
7243    }else{
7244      sqlite3_bind_int(pCells, 1, 0);
7245    }
7246    sqlite3_bind_int(pCells, 3, pTab->iPk);
7247
7248    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7249      int iPgno = sqlite3_column_int(pPages, 0);
7250      sqlite3_bind_int(pCells, 2, iPgno);
7251      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7252        int nField = sqlite3_column_int(pCells, 0);
7253        int iMin = sqlite3_column_int(pCells, 2);
7254        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7255
7256        RecoverTable *pTab2 = pTab;
7257        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7258          if( pOrphan==0 ){
7259            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7260          }
7261          pTab2 = pOrphan;
7262          if( pTab2==0 ) break;
7263        }
7264
7265        nField = nField+1;
7266        if( pTab2==pOrphan ){
7267          raw_printf(pState->out,
7268              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7269              pTab2->zQuoted, iRoot, iPgno, nField,
7270              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7271          );
7272        }else{
7273          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7274              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7275          );
7276        }
7277      }
7278      shellReset(&rc, pCells);
7279    }
7280    shellReset(&rc, pPages);
7281    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7282  }
7283  shellFinalize(&rc, pLoop);
7284  shellFinalize(&rc, pPages);
7285  shellFinalize(&rc, pCells);
7286  recoverFreeTable(pOrphan);
7287
7288  /* The rest of the schema */
7289  if( rc==SQLITE_OK ){
7290    sqlite3_stmt *pStmt = 0;
7291    shellPrepare(pState->db, &rc,
7292        "SELECT sql, name FROM recovery.schema "
7293        "WHERE sql NOT LIKE 'create table%'", &pStmt
7294    );
7295    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7296      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7297      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7298        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7299        char *zPrint = shellMPrintf(&rc,
7300          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7301          zName, zName, zSql
7302        );
7303        raw_printf(pState->out, "%s;\n", zPrint);
7304        sqlite3_free(zPrint);
7305      }else{
7306        raw_printf(pState->out, "%s;\n", zSql);
7307      }
7308    }
7309    shellFinalize(&rc, pStmt);
7310  }
7311
7312  if( rc==SQLITE_OK ){
7313    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7314    raw_printf(pState->out, "COMMIT;\n");
7315  }
7316  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7317  return rc;
7318}
7319#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7320
7321
7322/*
7323** If an input line begins with "." then invoke this routine to
7324** process that line.
7325**
7326** Return 1 on error, 2 to exit, and 0 otherwise.
7327*/
7328static int do_meta_command(char *zLine, ShellState *p){
7329  int h = 1;
7330  int nArg = 0;
7331  int n, c;
7332  int rc = 0;
7333  char *azArg[52];
7334
7335#ifndef SQLITE_OMIT_VIRTUALTABLE
7336  if( p->expert.pExpert ){
7337    expertFinish(p, 1, 0);
7338  }
7339#endif
7340
7341  /* Parse the input line into tokens.
7342  */
7343  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7344    while( IsSpace(zLine[h]) ){ h++; }
7345    if( zLine[h]==0 ) break;
7346    if( zLine[h]=='\'' || zLine[h]=='"' ){
7347      int delim = zLine[h++];
7348      azArg[nArg++] = &zLine[h];
7349      while( zLine[h] && zLine[h]!=delim ){
7350        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7351        h++;
7352      }
7353      if( zLine[h]==delim ){
7354        zLine[h++] = 0;
7355      }
7356      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7357    }else{
7358      azArg[nArg++] = &zLine[h];
7359      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7360      if( zLine[h] ) zLine[h++] = 0;
7361      resolve_backslashes(azArg[nArg-1]);
7362    }
7363  }
7364  azArg[nArg] = 0;
7365
7366  /* Process the input line.
7367  */
7368  if( nArg==0 ) return 0; /* no tokens, no error */
7369  n = strlen30(azArg[0]);
7370  c = azArg[0][0];
7371  clearTempFile(p);
7372
7373#ifndef SQLITE_OMIT_AUTHORIZATION
7374  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7375    if( nArg!=2 ){
7376      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7377      rc = 1;
7378      goto meta_command_exit;
7379    }
7380    open_db(p, 0);
7381    if( booleanValue(azArg[1]) ){
7382      sqlite3_set_authorizer(p->db, shellAuth, p);
7383    }else{
7384      sqlite3_set_authorizer(p->db, 0, 0);
7385    }
7386  }else
7387#endif
7388
7389#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7390  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7391    open_db(p, 0);
7392    rc = arDotCommand(p, 0, azArg, nArg);
7393  }else
7394#endif
7395
7396  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7397   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7398  ){
7399    const char *zDestFile = 0;
7400    const char *zDb = 0;
7401    sqlite3 *pDest;
7402    sqlite3_backup *pBackup;
7403    int j;
7404    int bAsync = 0;
7405    const char *zVfs = 0;
7406    for(j=1; j<nArg; j++){
7407      const char *z = azArg[j];
7408      if( z[0]=='-' ){
7409        if( z[1]=='-' ) z++;
7410        if( strcmp(z, "-append")==0 ){
7411          zVfs = "apndvfs";
7412        }else
7413        if( strcmp(z, "-async")==0 ){
7414          bAsync = 1;
7415        }else
7416        {
7417          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7418          return 1;
7419        }
7420      }else if( zDestFile==0 ){
7421        zDestFile = azArg[j];
7422      }else if( zDb==0 ){
7423        zDb = zDestFile;
7424        zDestFile = azArg[j];
7425      }else{
7426        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7427        return 1;
7428      }
7429    }
7430    if( zDestFile==0 ){
7431      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7432      return 1;
7433    }
7434    if( zDb==0 ) zDb = "main";
7435    rc = sqlite3_open_v2(zDestFile, &pDest,
7436                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7437    if( rc!=SQLITE_OK ){
7438      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7439      close_db(pDest);
7440      return 1;
7441    }
7442    if( bAsync ){
7443      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7444                   0, 0, 0);
7445    }
7446    open_db(p, 0);
7447    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7448    if( pBackup==0 ){
7449      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7450      close_db(pDest);
7451      return 1;
7452    }
7453    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7454    sqlite3_backup_finish(pBackup);
7455    if( rc==SQLITE_DONE ){
7456      rc = 0;
7457    }else{
7458      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7459      rc = 1;
7460    }
7461    close_db(pDest);
7462  }else
7463
7464  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7465    if( nArg==2 ){
7466      bail_on_error = booleanValue(azArg[1]);
7467    }else{
7468      raw_printf(stderr, "Usage: .bail on|off\n");
7469      rc = 1;
7470    }
7471  }else
7472
7473  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7474    if( nArg==2 ){
7475      if( booleanValue(azArg[1]) ){
7476        setBinaryMode(p->out, 1);
7477      }else{
7478        setTextMode(p->out, 1);
7479      }
7480    }else{
7481      raw_printf(stderr, "Usage: .binary on|off\n");
7482      rc = 1;
7483    }
7484  }else
7485
7486  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7487    if( nArg==2 ){
7488#if defined(_WIN32) || defined(WIN32)
7489      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7490      rc = !SetCurrentDirectoryW(z);
7491      sqlite3_free(z);
7492#else
7493      rc = chdir(azArg[1]);
7494#endif
7495      if( rc ){
7496        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7497        rc = 1;
7498      }
7499    }else{
7500      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7501      rc = 1;
7502    }
7503  }else
7504
7505  /* The undocumented ".breakpoint" command causes a call to the no-op
7506  ** routine named test_breakpoint().
7507  */
7508  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7509    test_breakpoint();
7510  }else
7511
7512  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7513    if( nArg==2 ){
7514      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7515    }else{
7516      raw_printf(stderr, "Usage: .changes on|off\n");
7517      rc = 1;
7518    }
7519  }else
7520
7521  /* Cancel output redirection, if it is currently set (by .testcase)
7522  ** Then read the content of the testcase-out.txt file and compare against
7523  ** azArg[1].  If there are differences, report an error and exit.
7524  */
7525  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7526    char *zRes = 0;
7527    output_reset(p);
7528    if( nArg!=2 ){
7529      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7530      rc = 2;
7531    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7532      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7533      rc = 2;
7534    }else if( testcase_glob(azArg[1],zRes)==0 ){
7535      utf8_printf(stderr,
7536                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7537                 p->zTestcase, azArg[1], zRes);
7538      rc = 1;
7539    }else{
7540      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7541      p->nCheck++;
7542    }
7543    sqlite3_free(zRes);
7544  }else
7545
7546  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7547    if( nArg==2 ){
7548      tryToClone(p, azArg[1]);
7549    }else{
7550      raw_printf(stderr, "Usage: .clone FILENAME\n");
7551      rc = 1;
7552    }
7553  }else
7554
7555  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7556    ShellState data;
7557    char *zErrMsg = 0;
7558    open_db(p, 0);
7559    memcpy(&data, p, sizeof(data));
7560    data.showHeader = 0;
7561    data.cMode = data.mode = MODE_List;
7562    sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
7563    data.cnt = 0;
7564    sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
7565                 callback, &data, &zErrMsg);
7566    if( zErrMsg ){
7567      utf8_printf(stderr,"Error: %s\n", zErrMsg);
7568      sqlite3_free(zErrMsg);
7569      rc = 1;
7570    }
7571  }else
7572
7573  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7574    static const struct DbConfigChoices {
7575      const char *zName;
7576      int op;
7577    } aDbConfig[] = {
7578        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7579        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7580        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7581        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7582        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7583        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7584        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7585        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7586        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7587        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7588        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7589        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7590        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7591        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7592        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7593        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7594    };
7595    int ii, v;
7596    open_db(p, 0);
7597    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7598      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7599      if( nArg>=3 ){
7600        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7601      }
7602      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7603      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7604      if( nArg>1 ) break;
7605    }
7606    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7607      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7608      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7609    }
7610  }else
7611
7612  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7613    rc = shell_dbinfo_command(p, nArg, azArg);
7614  }else
7615
7616#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7617  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7618    open_db(p, 0);
7619    rc = recoverDatabaseCmd(p, nArg, azArg);
7620  }else
7621#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7622
7623  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7624    char *zLike = 0;
7625    char *zSql;
7626    int i;
7627    int savedShowHeader = p->showHeader;
7628    int savedShellFlags = p->shellFlgs;
7629    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
7630    for(i=1; i<nArg; i++){
7631      if( azArg[i][0]=='-' ){
7632        const char *z = azArg[i]+1;
7633        if( z[0]=='-' ) z++;
7634        if( strcmp(z,"preserve-rowids")==0 ){
7635#ifdef SQLITE_OMIT_VIRTUALTABLE
7636          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7637                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7638          rc = 1;
7639          sqlite3_free(zLike);
7640          goto meta_command_exit;
7641#else
7642          ShellSetFlag(p, SHFLG_PreserveRowid);
7643#endif
7644        }else
7645        if( strcmp(z,"newlines")==0 ){
7646          ShellSetFlag(p, SHFLG_Newlines);
7647        }else
7648        {
7649          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7650          rc = 1;
7651          sqlite3_free(zLike);
7652          goto meta_command_exit;
7653        }
7654      }else if( zLike ){
7655        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
7656                zLike, azArg[i]);
7657      }else{
7658        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
7659      }
7660    }
7661
7662    open_db(p, 0);
7663
7664    /* When playing back a "dump", the content might appear in an order
7665    ** which causes immediate foreign key constraints to be violated.
7666    ** So disable foreign-key constraint enforcement to prevent problems. */
7667    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7668    raw_printf(p->out, "BEGIN TRANSACTION;\n");
7669    p->writableSchema = 0;
7670    p->showHeader = 0;
7671    /* Set writable_schema=ON since doing so forces SQLite to initialize
7672    ** as much of the schema as it can even if the sqlite_schema table is
7673    ** corrupt. */
7674    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7675    p->nErr = 0;
7676    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7677    zSql = sqlite3_mprintf(
7678      "SELECT name, type, sql FROM sqlite_schema "
7679      "WHERE (%s) AND type=='table'"
7680      "  AND sql NOT NULL"
7681      " ORDER BY tbl_name='sqlite_sequence', rowid",
7682      zLike
7683    );
7684    run_schema_dump_query(p,zSql);
7685    sqlite3_free(zSql);
7686    zSql = sqlite3_mprintf(
7687      "SELECT sql FROM sqlite_schema "
7688      "WHERE (%s) AND sql NOT NULL"
7689      "  AND type IN ('index','trigger','view')",
7690      zLike
7691    );
7692    run_table_dump_query(p, zSql);
7693    sqlite3_free(zSql);
7694    sqlite3_free(zLike);
7695    if( p->writableSchema ){
7696      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7697      p->writableSchema = 0;
7698    }
7699    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7700    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7701    raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7702    p->showHeader = savedShowHeader;
7703    p->shellFlgs = savedShellFlags;
7704  }else
7705
7706  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7707    if( nArg==2 ){
7708      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7709    }else{
7710      raw_printf(stderr, "Usage: .echo on|off\n");
7711      rc = 1;
7712    }
7713  }else
7714
7715  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7716    if( nArg==2 ){
7717      p->autoEQPtest = 0;
7718      if( p->autoEQPtrace ){
7719        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7720        p->autoEQPtrace = 0;
7721      }
7722      if( strcmp(azArg[1],"full")==0 ){
7723        p->autoEQP = AUTOEQP_full;
7724      }else if( strcmp(azArg[1],"trigger")==0 ){
7725        p->autoEQP = AUTOEQP_trigger;
7726#ifdef SQLITE_DEBUG
7727      }else if( strcmp(azArg[1],"test")==0 ){
7728        p->autoEQP = AUTOEQP_on;
7729        p->autoEQPtest = 1;
7730      }else if( strcmp(azArg[1],"trace")==0 ){
7731        p->autoEQP = AUTOEQP_full;
7732        p->autoEQPtrace = 1;
7733        open_db(p, 0);
7734        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7735        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7736#endif
7737      }else{
7738        p->autoEQP = (u8)booleanValue(azArg[1]);
7739      }
7740    }else{
7741      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7742      rc = 1;
7743    }
7744  }else
7745
7746  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7747    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7748    rc = 2;
7749  }else
7750
7751  /* The ".explain" command is automatic now.  It is largely pointless.  It
7752  ** retained purely for backwards compatibility */
7753  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7754    int val = 1;
7755    if( nArg>=2 ){
7756      if( strcmp(azArg[1],"auto")==0 ){
7757        val = 99;
7758      }else{
7759        val =  booleanValue(azArg[1]);
7760      }
7761    }
7762    if( val==1 && p->mode!=MODE_Explain ){
7763      p->normalMode = p->mode;
7764      p->mode = MODE_Explain;
7765      p->autoExplain = 0;
7766    }else if( val==0 ){
7767      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7768      p->autoExplain = 0;
7769    }else if( val==99 ){
7770      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7771      p->autoExplain = 1;
7772    }
7773  }else
7774
7775#ifndef SQLITE_OMIT_VIRTUALTABLE
7776  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7777    open_db(p, 0);
7778    expertDotCommand(p, azArg, nArg);
7779  }else
7780#endif
7781
7782  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7783    static const struct {
7784       const char *zCtrlName;   /* Name of a test-control option */
7785       int ctrlCode;            /* Integer code for that option */
7786       const char *zUsage;      /* Usage notes */
7787    } aCtrl[] = {
7788      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7789      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7790   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7791      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7792      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7793   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7794      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7795      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7796      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7797      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7798    };
7799    int filectrl = -1;
7800    int iCtrl = -1;
7801    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7802    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7803    int n2, i;
7804    const char *zCmd = 0;
7805    const char *zSchema = 0;
7806
7807    open_db(p, 0);
7808    zCmd = nArg>=2 ? azArg[1] : "help";
7809
7810    if( zCmd[0]=='-'
7811     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7812     && nArg>=4
7813    ){
7814      zSchema = azArg[2];
7815      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7816      nArg -= 2;
7817      zCmd = azArg[1];
7818    }
7819
7820    /* The argument can optionally begin with "-" or "--" */
7821    if( zCmd[0]=='-' && zCmd[1] ){
7822      zCmd++;
7823      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7824    }
7825
7826    /* --help lists all file-controls */
7827    if( strcmp(zCmd,"help")==0 ){
7828      utf8_printf(p->out, "Available file-controls:\n");
7829      for(i=0; i<ArraySize(aCtrl); i++){
7830        utf8_printf(p->out, "  .filectrl %s %s\n",
7831                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7832      }
7833      rc = 1;
7834      goto meta_command_exit;
7835    }
7836
7837    /* convert filectrl text option to value. allow any unique prefix
7838    ** of the option name, or a numerical value. */
7839    n2 = strlen30(zCmd);
7840    for(i=0; i<ArraySize(aCtrl); i++){
7841      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7842        if( filectrl<0 ){
7843          filectrl = aCtrl[i].ctrlCode;
7844          iCtrl = i;
7845        }else{
7846          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7847                              "Use \".filectrl --help\" for help\n", zCmd);
7848          rc = 1;
7849          goto meta_command_exit;
7850        }
7851      }
7852    }
7853    if( filectrl<0 ){
7854      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7855                         "Use \".filectrl --help\" for help\n", zCmd);
7856    }else{
7857      switch(filectrl){
7858        case SQLITE_FCNTL_SIZE_LIMIT: {
7859          if( nArg!=2 && nArg!=3 ) break;
7860          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7861          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7862          isOk = 1;
7863          break;
7864        }
7865        case SQLITE_FCNTL_LOCK_TIMEOUT:
7866        case SQLITE_FCNTL_CHUNK_SIZE: {
7867          int x;
7868          if( nArg!=3 ) break;
7869          x = (int)integerValue(azArg[2]);
7870          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7871          isOk = 2;
7872          break;
7873        }
7874        case SQLITE_FCNTL_PERSIST_WAL:
7875        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7876          int x;
7877          if( nArg!=2 && nArg!=3 ) break;
7878          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7879          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7880          iRes = x;
7881          isOk = 1;
7882          break;
7883        }
7884        case SQLITE_FCNTL_HAS_MOVED: {
7885          int x;
7886          if( nArg!=2 ) break;
7887          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7888          iRes = x;
7889          isOk = 1;
7890          break;
7891        }
7892        case SQLITE_FCNTL_TEMPFILENAME: {
7893          char *z = 0;
7894          if( nArg!=2 ) break;
7895          sqlite3_file_control(p->db, zSchema, filectrl, &z);
7896          if( z ){
7897            utf8_printf(p->out, "%s\n", z);
7898            sqlite3_free(z);
7899          }
7900          isOk = 2;
7901          break;
7902        }
7903        case SQLITE_FCNTL_RESERVE_BYTES: {
7904          int x;
7905          if( nArg>=3 ){
7906            x = atoi(azArg[2]);
7907            sqlite3_file_control(p->db, zSchema, filectrl, &x);
7908          }
7909          x = -1;
7910          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7911          utf8_printf(p->out,"%d\n", x);
7912          isOk = 2;
7913          break;
7914        }
7915      }
7916    }
7917    if( isOk==0 && iCtrl>=0 ){
7918      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7919      rc = 1;
7920    }else if( isOk==1 ){
7921      char zBuf[100];
7922      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
7923      raw_printf(p->out, "%s\n", zBuf);
7924    }
7925  }else
7926
7927  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
7928    ShellState data;
7929    char *zErrMsg = 0;
7930    int doStats = 0;
7931    memcpy(&data, p, sizeof(data));
7932    data.showHeader = 0;
7933    data.cMode = data.mode = MODE_Semi;
7934    if( nArg==2 && optionMatch(azArg[1], "indent") ){
7935      data.cMode = data.mode = MODE_Pretty;
7936      nArg = 1;
7937    }
7938    if( nArg!=1 ){
7939      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
7940      rc = 1;
7941      goto meta_command_exit;
7942    }
7943    open_db(p, 0);
7944    rc = sqlite3_exec(p->db,
7945       "SELECT sql FROM"
7946       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
7947       "     FROM sqlite_schema UNION ALL"
7948       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
7949       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
7950       "ORDER BY rowid",
7951       callback, &data, &zErrMsg
7952    );
7953    if( rc==SQLITE_OK ){
7954      sqlite3_stmt *pStmt;
7955      rc = sqlite3_prepare_v2(p->db,
7956               "SELECT rowid FROM sqlite_schema"
7957               " WHERE name GLOB 'sqlite_stat[134]'",
7958               -1, &pStmt, 0);
7959      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
7960      sqlite3_finalize(pStmt);
7961    }
7962    if( doStats==0 ){
7963      raw_printf(p->out, "/* No STAT tables available */\n");
7964    }else{
7965      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
7966      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
7967                   callback, &data, &zErrMsg);
7968      data.cMode = data.mode = MODE_Insert;
7969      data.zDestTable = "sqlite_stat1";
7970      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
7971      data.zDestTable = "sqlite_stat4";
7972      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
7973      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
7974    }
7975  }else
7976
7977  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
7978    if( nArg==2 ){
7979      p->showHeader = booleanValue(azArg[1]);
7980      p->shellFlgs |= SHFLG_HeaderSet;
7981    }else{
7982      raw_printf(stderr, "Usage: .headers on|off\n");
7983      rc = 1;
7984    }
7985  }else
7986
7987  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
7988    if( nArg>=2 ){
7989      n = showHelp(p->out, azArg[1]);
7990      if( n==0 ){
7991        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
7992      }
7993    }else{
7994      showHelp(p->out, 0);
7995    }
7996  }else
7997
7998  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
7999    char *zTable = 0;           /* Insert data into this table */
8000    char *zFile = 0;            /* Name of file to extra content from */
8001    sqlite3_stmt *pStmt = NULL; /* A statement */
8002    int nCol;                   /* Number of columns in the table */
8003    int nByte;                  /* Number of bytes in an SQL string */
8004    int i, j;                   /* Loop counters */
8005    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8006    int nSep;                   /* Number of bytes in p->colSeparator[] */
8007    char *zSql;                 /* An SQL statement */
8008    ImportCtx sCtx;             /* Reader context */
8009    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8010    int eVerbose = 0;           /* Larger for more console output */
8011    int nSkip = 0;              /* Initial lines to skip */
8012    int useOutputMode = 1;      /* Use output mode to determine separators */
8013
8014    memset(&sCtx, 0, sizeof(sCtx));
8015    if( p->mode==MODE_Ascii ){
8016      xRead = ascii_read_one_field;
8017    }else{
8018      xRead = csv_read_one_field;
8019    }
8020    for(i=1; i<nArg; i++){
8021      char *z = azArg[i];
8022      if( z[0]=='-' && z[1]=='-' ) z++;
8023      if( z[0]!='-' ){
8024        if( zFile==0 ){
8025          zFile = z;
8026        }else if( zTable==0 ){
8027          zTable = z;
8028        }else{
8029          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8030          showHelp(p->out, "import");
8031          rc = 1;
8032          goto meta_command_exit;
8033        }
8034      }else if( strcmp(z,"-v")==0 ){
8035        eVerbose++;
8036      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8037        nSkip = integerValue(azArg[++i]);
8038      }else if( strcmp(z,"-ascii")==0 ){
8039        sCtx.cColSep = SEP_Unit[0];
8040        sCtx.cRowSep = SEP_Record[0];
8041        xRead = ascii_read_one_field;
8042        useOutputMode = 0;
8043      }else if( strcmp(z,"-csv")==0 ){
8044        sCtx.cColSep = ',';
8045        sCtx.cRowSep = '\n';
8046        xRead = csv_read_one_field;
8047        useOutputMode = 0;
8048      }else{
8049        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8050        showHelp(p->out, "import");
8051        rc = 1;
8052        goto meta_command_exit;
8053      }
8054    }
8055    if( zTable==0 ){
8056      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8057                  zFile==0 ? "FILE" : "TABLE");
8058      showHelp(p->out, "import");
8059      rc = 1;
8060      goto meta_command_exit;
8061    }
8062    seenInterrupt = 0;
8063    open_db(p, 0);
8064    if( useOutputMode ){
8065      /* If neither the --csv or --ascii options are specified, then set
8066      ** the column and row separator characters from the output mode. */
8067      nSep = strlen30(p->colSeparator);
8068      if( nSep==0 ){
8069        raw_printf(stderr,
8070                   "Error: non-null column separator required for import\n");
8071        rc = 1;
8072        goto meta_command_exit;
8073      }
8074      if( nSep>1 ){
8075        raw_printf(stderr,
8076              "Error: multi-character column separators not allowed"
8077              " for import\n");
8078        rc = 1;
8079        goto meta_command_exit;
8080      }
8081      nSep = strlen30(p->rowSeparator);
8082      if( nSep==0 ){
8083        raw_printf(stderr,
8084            "Error: non-null row separator required for import\n");
8085        rc = 1;
8086        goto meta_command_exit;
8087      }
8088      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8089        /* When importing CSV (only), if the row separator is set to the
8090        ** default output row separator, change it to the default input
8091        ** row separator.  This avoids having to maintain different input
8092        ** and output row separators. */
8093        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8094        nSep = strlen30(p->rowSeparator);
8095      }
8096      if( nSep>1 ){
8097        raw_printf(stderr, "Error: multi-character row separators not allowed"
8098                           " for import\n");
8099        rc = 1;
8100        goto meta_command_exit;
8101      }
8102      sCtx.cColSep = p->colSeparator[0];
8103      sCtx.cRowSep = p->rowSeparator[0];
8104    }
8105    sCtx.zFile = zFile;
8106    sCtx.nLine = 1;
8107    if( sCtx.zFile[0]=='|' ){
8108#ifdef SQLITE_OMIT_POPEN
8109      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8110      rc = 1;
8111      goto meta_command_exit;
8112#else
8113      sCtx.in = popen(sCtx.zFile+1, "r");
8114      sCtx.zFile = "<pipe>";
8115      sCtx.xCloser = pclose;
8116#endif
8117    }else{
8118      sCtx.in = fopen(sCtx.zFile, "rb");
8119      sCtx.xCloser = fclose;
8120    }
8121    if( sCtx.in==0 ){
8122      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8123      rc = 1;
8124      goto meta_command_exit;
8125    }
8126    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8127      char zSep[2];
8128      zSep[1] = 0;
8129      zSep[0] = sCtx.cColSep;
8130      utf8_printf(p->out, "Column separator ");
8131      output_c_string(p->out, zSep);
8132      utf8_printf(p->out, ", row separator ");
8133      zSep[0] = sCtx.cRowSep;
8134      output_c_string(p->out, zSep);
8135      utf8_printf(p->out, "\n");
8136    }
8137    while( (nSkip--)>0 ){
8138      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8139      sCtx.nLine++;
8140    }
8141    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
8142    if( zSql==0 ){
8143      import_cleanup(&sCtx);
8144      shell_out_of_memory();
8145    }
8146    nByte = strlen30(zSql);
8147    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8148    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8149    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8150      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
8151      char cSep = '(';
8152      while( xRead(&sCtx) ){
8153        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8154        cSep = ',';
8155        if( sCtx.cTerm!=sCtx.cColSep ) break;
8156      }
8157      if( cSep=='(' ){
8158        sqlite3_free(zCreate);
8159        import_cleanup(&sCtx);
8160        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8161        rc = 1;
8162        goto meta_command_exit;
8163      }
8164      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8165      if( eVerbose>=1 ){
8166        utf8_printf(p->out, "%s\n", zCreate);
8167      }
8168      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8169      sqlite3_free(zCreate);
8170      if( rc ){
8171        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
8172                sqlite3_errmsg(p->db));
8173        import_cleanup(&sCtx);
8174        rc = 1;
8175        goto meta_command_exit;
8176      }
8177      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8178    }
8179    sqlite3_free(zSql);
8180    if( rc ){
8181      if (pStmt) sqlite3_finalize(pStmt);
8182      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8183      import_cleanup(&sCtx);
8184      rc = 1;
8185      goto meta_command_exit;
8186    }
8187    nCol = sqlite3_column_count(pStmt);
8188    sqlite3_finalize(pStmt);
8189    pStmt = 0;
8190    if( nCol==0 ) return 0; /* no columns, no error */
8191    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8192    if( zSql==0 ){
8193      import_cleanup(&sCtx);
8194      shell_out_of_memory();
8195    }
8196    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8197    j = strlen30(zSql);
8198    for(i=1; i<nCol; i++){
8199      zSql[j++] = ',';
8200      zSql[j++] = '?';
8201    }
8202    zSql[j++] = ')';
8203    zSql[j] = 0;
8204    if( eVerbose>=2 ){
8205      utf8_printf(p->out, "Insert using: %s\n", zSql);
8206    }
8207    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8208    sqlite3_free(zSql);
8209    if( rc ){
8210      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8211      if (pStmt) sqlite3_finalize(pStmt);
8212      import_cleanup(&sCtx);
8213      rc = 1;
8214      goto meta_command_exit;
8215    }
8216    needCommit = sqlite3_get_autocommit(p->db);
8217    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8218    do{
8219      int startLine = sCtx.nLine;
8220      for(i=0; i<nCol; i++){
8221        char *z = xRead(&sCtx);
8222        /*
8223        ** Did we reach end-of-file before finding any columns?
8224        ** If so, stop instead of NULL filling the remaining columns.
8225        */
8226        if( z==0 && i==0 ) break;
8227        /*
8228        ** Did we reach end-of-file OR end-of-line before finding any
8229        ** columns in ASCII mode?  If so, stop instead of NULL filling
8230        ** the remaining columns.
8231        */
8232        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8233        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8234        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8235          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8236                          "filling the rest with NULL\n",
8237                          sCtx.zFile, startLine, nCol, i+1);
8238          i += 2;
8239          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8240        }
8241      }
8242      if( sCtx.cTerm==sCtx.cColSep ){
8243        do{
8244          xRead(&sCtx);
8245          i++;
8246        }while( sCtx.cTerm==sCtx.cColSep );
8247        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8248                        "extras ignored\n",
8249                        sCtx.zFile, startLine, nCol, i);
8250      }
8251      if( i>=nCol ){
8252        sqlite3_step(pStmt);
8253        rc = sqlite3_reset(pStmt);
8254        if( rc!=SQLITE_OK ){
8255          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8256                      startLine, sqlite3_errmsg(p->db));
8257          sCtx.nErr++;
8258        }else{
8259          sCtx.nRow++;
8260        }
8261      }
8262    }while( sCtx.cTerm!=EOF );
8263
8264    import_cleanup(&sCtx);
8265    sqlite3_finalize(pStmt);
8266    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8267    if( eVerbose>0 ){
8268      utf8_printf(p->out,
8269          "Added %d rows with %d errors using %d lines of input\n",
8270          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8271    }
8272  }else
8273
8274#ifndef SQLITE_UNTESTABLE
8275  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8276    char *zSql;
8277    char *zCollist = 0;
8278    sqlite3_stmt *pStmt;
8279    int tnum = 0;
8280    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8281    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8282    int i;
8283    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8284      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8285                          "       .imposter off\n");
8286      /* Also allowed, but not documented:
8287      **
8288      **    .imposter TABLE IMPOSTER
8289      **
8290      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8291      ** imposter is another WITHOUT ROWID table with the columns in
8292      ** storage order. */
8293      rc = 1;
8294      goto meta_command_exit;
8295    }
8296    open_db(p, 0);
8297    if( nArg==2 ){
8298      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8299      goto meta_command_exit;
8300    }
8301    zSql = sqlite3_mprintf(
8302      "SELECT rootpage, 0 FROM sqlite_schema"
8303      " WHERE name='%q' AND type='index'"
8304      "UNION ALL "
8305      "SELECT rootpage, 1 FROM sqlite_schema"
8306      " WHERE name='%q' AND type='table'"
8307      "   AND sql LIKE '%%without%%rowid%%'",
8308      azArg[1], azArg[1]
8309    );
8310    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8311    sqlite3_free(zSql);
8312    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8313      tnum = sqlite3_column_int(pStmt, 0);
8314      isWO = sqlite3_column_int(pStmt, 1);
8315    }
8316    sqlite3_finalize(pStmt);
8317    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8318    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8319    sqlite3_free(zSql);
8320    i = 0;
8321    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8322      char zLabel[20];
8323      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8324      i++;
8325      if( zCol==0 ){
8326        if( sqlite3_column_int(pStmt,1)==-1 ){
8327          zCol = "_ROWID_";
8328        }else{
8329          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8330          zCol = zLabel;
8331        }
8332      }
8333      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8334        lenPK = (int)strlen(zCollist);
8335      }
8336      if( zCollist==0 ){
8337        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8338      }else{
8339        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8340      }
8341    }
8342    sqlite3_finalize(pStmt);
8343    if( i==0 || tnum==0 ){
8344      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8345      rc = 1;
8346      sqlite3_free(zCollist);
8347      goto meta_command_exit;
8348    }
8349    if( lenPK==0 ) lenPK = 100000;
8350    zSql = sqlite3_mprintf(
8351          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8352          azArg[2], zCollist, lenPK, zCollist);
8353    sqlite3_free(zCollist);
8354    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8355    if( rc==SQLITE_OK ){
8356      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8357      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8358      if( rc ){
8359        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8360      }else{
8361        utf8_printf(stdout, "%s;\n", zSql);
8362        raw_printf(stdout,
8363          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8364          azArg[1], isWO ? "table" : "index"
8365        );
8366      }
8367    }else{
8368      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8369      rc = 1;
8370    }
8371    sqlite3_free(zSql);
8372  }else
8373#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8374
8375#ifdef SQLITE_ENABLE_IOTRACE
8376  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8377    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8378    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8379    iotrace = 0;
8380    if( nArg<2 ){
8381      sqlite3IoTrace = 0;
8382    }else if( strcmp(azArg[1], "-")==0 ){
8383      sqlite3IoTrace = iotracePrintf;
8384      iotrace = stdout;
8385    }else{
8386      iotrace = fopen(azArg[1], "w");
8387      if( iotrace==0 ){
8388        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8389        sqlite3IoTrace = 0;
8390        rc = 1;
8391      }else{
8392        sqlite3IoTrace = iotracePrintf;
8393      }
8394    }
8395  }else
8396#endif
8397
8398  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8399    static const struct {
8400       const char *zLimitName;   /* Name of a limit */
8401       int limitCode;            /* Integer code for that limit */
8402    } aLimit[] = {
8403      { "length",                SQLITE_LIMIT_LENGTH                    },
8404      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8405      { "column",                SQLITE_LIMIT_COLUMN                    },
8406      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8407      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8408      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8409      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8410      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8411      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8412      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8413      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8414      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8415    };
8416    int i, n2;
8417    open_db(p, 0);
8418    if( nArg==1 ){
8419      for(i=0; i<ArraySize(aLimit); i++){
8420        printf("%20s %d\n", aLimit[i].zLimitName,
8421               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8422      }
8423    }else if( nArg>3 ){
8424      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8425      rc = 1;
8426      goto meta_command_exit;
8427    }else{
8428      int iLimit = -1;
8429      n2 = strlen30(azArg[1]);
8430      for(i=0; i<ArraySize(aLimit); i++){
8431        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8432          if( iLimit<0 ){
8433            iLimit = i;
8434          }else{
8435            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8436            rc = 1;
8437            goto meta_command_exit;
8438          }
8439        }
8440      }
8441      if( iLimit<0 ){
8442        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8443                        "enter \".limits\" with no arguments for a list.\n",
8444                         azArg[1]);
8445        rc = 1;
8446        goto meta_command_exit;
8447      }
8448      if( nArg==3 ){
8449        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8450                      (int)integerValue(azArg[2]));
8451      }
8452      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8453             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8454    }
8455  }else
8456
8457  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8458    open_db(p, 0);
8459    lintDotCommand(p, azArg, nArg);
8460  }else
8461
8462#ifndef SQLITE_OMIT_LOAD_EXTENSION
8463  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8464    const char *zFile, *zProc;
8465    char *zErrMsg = 0;
8466    if( nArg<2 ){
8467      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8468      rc = 1;
8469      goto meta_command_exit;
8470    }
8471    zFile = azArg[1];
8472    zProc = nArg>=3 ? azArg[2] : 0;
8473    open_db(p, 0);
8474    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8475    if( rc!=SQLITE_OK ){
8476      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8477      sqlite3_free(zErrMsg);
8478      rc = 1;
8479    }
8480  }else
8481#endif
8482
8483  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8484    if( nArg!=2 ){
8485      raw_printf(stderr, "Usage: .log FILENAME\n");
8486      rc = 1;
8487    }else{
8488      const char *zFile = azArg[1];
8489      output_file_close(p->pLog);
8490      p->pLog = output_file_open(zFile, 0);
8491    }
8492  }else
8493
8494  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8495    const char *zMode = nArg>=2 ? azArg[1] : "";
8496    int n2 = strlen30(zMode);
8497    int c2 = zMode[0];
8498    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8499      p->mode = MODE_Line;
8500      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8501    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8502      p->mode = MODE_Column;
8503      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8504        p->showHeader = 1;
8505      }
8506      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8507    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8508      p->mode = MODE_List;
8509      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8510      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8511    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8512      p->mode = MODE_Html;
8513    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8514      p->mode = MODE_Tcl;
8515      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8516      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8517    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8518      p->mode = MODE_Csv;
8519      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8520      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8521    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8522      p->mode = MODE_List;
8523      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8524    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8525      p->mode = MODE_Insert;
8526      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8527    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8528      p->mode = MODE_Quote;
8529      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8530      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8531    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8532      p->mode = MODE_Ascii;
8533      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8534      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8535    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8536      p->mode = MODE_Markdown;
8537    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8538      p->mode = MODE_Table;
8539    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8540      p->mode = MODE_Box;
8541    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8542      p->mode = MODE_Json;
8543    }else if( nArg==1 ){
8544      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8545    }else{
8546      raw_printf(stderr, "Error: mode should be one of: "
8547         "ascii box column csv html insert json line list markdown "
8548         "quote table tabs tcl\n");
8549      rc = 1;
8550    }
8551    p->cMode = p->mode;
8552  }else
8553
8554  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8555    if( nArg==2 ){
8556      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8557                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8558    }else{
8559      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8560      rc = 1;
8561    }
8562  }else
8563
8564#ifdef SQLITE_DEBUG
8565  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8566    int i;
8567    for(i=1; i<nArg; i++){
8568      const char *z = azArg[i];
8569      if( z[0]=='-' && z[1]=='-' ) z++;
8570      if( strcmp(z,"-repeat")==0 ){
8571        if( i==nArg-1 ){
8572          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8573          rc = 1;
8574        }else{
8575          oomRepeat = (int)integerValue(azArg[++i]);
8576        }
8577      }else if( IsDigit(z[0]) ){
8578        oomCounter = (int)integerValue(azArg[i]);
8579      }else{
8580        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8581        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8582        rc = 1;
8583      }
8584    }
8585    if( rc==0 ){
8586      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8587      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8588    }
8589  }else
8590#endif /* SQLITE_DEBUG */
8591
8592  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8593    char *zNewFilename;  /* Name of the database file to open */
8594    int iName = 1;       /* Index in azArg[] of the filename */
8595    int newFlag = 0;     /* True to delete file before opening */
8596    /* Close the existing database */
8597    session_close_all(p);
8598    close_db(p->db);
8599    p->db = 0;
8600    p->zDbFilename = 0;
8601    sqlite3_free(p->zFreeOnClose);
8602    p->zFreeOnClose = 0;
8603    p->openMode = SHELL_OPEN_UNSPEC;
8604    p->openFlags = 0;
8605    p->szMax = 0;
8606    /* Check for command-line arguments */
8607    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8608      const char *z = azArg[iName];
8609      if( optionMatch(z,"new") ){
8610        newFlag = 1;
8611#ifdef SQLITE_HAVE_ZLIB
8612      }else if( optionMatch(z, "zip") ){
8613        p->openMode = SHELL_OPEN_ZIPFILE;
8614#endif
8615      }else if( optionMatch(z, "append") ){
8616        p->openMode = SHELL_OPEN_APPENDVFS;
8617      }else if( optionMatch(z, "readonly") ){
8618        p->openMode = SHELL_OPEN_READONLY;
8619      }else if( optionMatch(z, "nofollow") ){
8620        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8621#ifdef SQLITE_ENABLE_DESERIALIZE
8622      }else if( optionMatch(z, "deserialize") ){
8623        p->openMode = SHELL_OPEN_DESERIALIZE;
8624      }else if( optionMatch(z, "hexdb") ){
8625        p->openMode = SHELL_OPEN_HEXDB;
8626      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8627        p->szMax = integerValue(azArg[++iName]);
8628#endif /* SQLITE_ENABLE_DESERIALIZE */
8629      }else if( z[0]=='-' ){
8630        utf8_printf(stderr, "unknown option: %s\n", z);
8631        rc = 1;
8632        goto meta_command_exit;
8633      }
8634    }
8635    /* If a filename is specified, try to open it first */
8636    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
8637    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8638      if( newFlag ) shellDeleteFile(zNewFilename);
8639      p->zDbFilename = zNewFilename;
8640      open_db(p, OPEN_DB_KEEPALIVE);
8641      if( p->db==0 ){
8642        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8643        sqlite3_free(zNewFilename);
8644      }else{
8645        p->zFreeOnClose = zNewFilename;
8646      }
8647    }
8648    if( p->db==0 ){
8649      /* As a fall-back open a TEMP database */
8650      p->zDbFilename = 0;
8651      open_db(p, 0);
8652    }
8653  }else
8654
8655  if( (c=='o'
8656        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8657   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8658  ){
8659    const char *zFile = 0;
8660    int bTxtMode = 0;
8661    int i;
8662    int eMode = 0;
8663    int bBOM = 0;
8664    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8665
8666    if( c=='e' ){
8667      eMode = 'x';
8668      bOnce = 2;
8669    }else if( strncmp(azArg[0],"once",n)==0 ){
8670      bOnce = 1;
8671    }
8672    for(i=1; i<nArg; i++){
8673      char *z = azArg[i];
8674      if( z[0]=='-' ){
8675        if( z[1]=='-' ) z++;
8676        if( strcmp(z,"-bom")==0 ){
8677          bBOM = 1;
8678        }else if( c!='e' && strcmp(z,"-x")==0 ){
8679          eMode = 'x';  /* spreadsheet */
8680        }else if( c!='e' && strcmp(z,"-e")==0 ){
8681          eMode = 'e';  /* text editor */
8682        }else{
8683          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8684                      azArg[i]);
8685          showHelp(p->out, azArg[0]);
8686          rc = 1;
8687          goto meta_command_exit;
8688        }
8689      }else if( zFile==0 ){
8690        zFile = z;
8691      }else{
8692        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8693                    azArg[i]);
8694        showHelp(p->out, azArg[0]);
8695        rc = 1;
8696        goto meta_command_exit;
8697      }
8698    }
8699    if( zFile==0 ) zFile = "stdout";
8700    if( bOnce ){
8701      p->outCount = 2;
8702    }else{
8703      p->outCount = 0;
8704    }
8705    output_reset(p);
8706#ifndef SQLITE_NOHAVE_SYSTEM
8707    if( eMode=='e' || eMode=='x' ){
8708      p->doXdgOpen = 1;
8709      outputModePush(p);
8710      if( eMode=='x' ){
8711        /* spreadsheet mode.  Output as CSV. */
8712        newTempFile(p, "csv");
8713        ShellClearFlag(p, SHFLG_Echo);
8714        p->mode = MODE_Csv;
8715        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8716        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8717      }else{
8718        /* text editor mode */
8719        newTempFile(p, "txt");
8720        bTxtMode = 1;
8721      }
8722      zFile = p->zTempFile;
8723    }
8724#endif /* SQLITE_NOHAVE_SYSTEM */
8725    if( zFile[0]=='|' ){
8726#ifdef SQLITE_OMIT_POPEN
8727      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8728      rc = 1;
8729      p->out = stdout;
8730#else
8731      p->out = popen(zFile + 1, "w");
8732      if( p->out==0 ){
8733        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8734        p->out = stdout;
8735        rc = 1;
8736      }else{
8737        if( bBOM ) fprintf(p->out,"\357\273\277");
8738        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8739      }
8740#endif
8741    }else{
8742      p->out = output_file_open(zFile, bTxtMode);
8743      if( p->out==0 ){
8744        if( strcmp(zFile,"off")!=0 ){
8745          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8746        }
8747        p->out = stdout;
8748        rc = 1;
8749      } else {
8750        if( bBOM ) fprintf(p->out,"\357\273\277");
8751        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8752      }
8753    }
8754  }else
8755
8756  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8757    open_db(p,0);
8758    if( nArg<=1 ) goto parameter_syntax_error;
8759
8760    /* .parameter clear
8761    ** Clear all bind parameters by dropping the TEMP table that holds them.
8762    */
8763    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8764      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8765                   0, 0, 0);
8766    }else
8767
8768    /* .parameter list
8769    ** List all bind parameters.
8770    */
8771    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8772      sqlite3_stmt *pStmt = 0;
8773      int rx;
8774      int len = 0;
8775      rx = sqlite3_prepare_v2(p->db,
8776             "SELECT max(length(key)) "
8777             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8778      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8779        len = sqlite3_column_int(pStmt, 0);
8780        if( len>40 ) len = 40;
8781      }
8782      sqlite3_finalize(pStmt);
8783      pStmt = 0;
8784      if( len ){
8785        rx = sqlite3_prepare_v2(p->db,
8786             "SELECT key, quote(value) "
8787             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8788        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8789          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8790                      sqlite3_column_text(pStmt,1));
8791        }
8792        sqlite3_finalize(pStmt);
8793      }
8794    }else
8795
8796    /* .parameter init
8797    ** Make sure the TEMP table used to hold bind parameters exists.
8798    ** Create it if necessary.
8799    */
8800    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8801      bind_table_init(p);
8802    }else
8803
8804    /* .parameter set NAME VALUE
8805    ** Set or reset a bind parameter.  NAME should be the full parameter
8806    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8807    ** VALUE can be in either SQL literal notation, or if not it will be
8808    ** understood to be a text string.
8809    */
8810    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8811      int rx;
8812      char *zSql;
8813      sqlite3_stmt *pStmt;
8814      const char *zKey = azArg[2];
8815      const char *zValue = azArg[3];
8816      bind_table_init(p);
8817      zSql = sqlite3_mprintf(
8818                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8819                  "VALUES(%Q,%s);", zKey, zValue);
8820      if( zSql==0 ) shell_out_of_memory();
8821      pStmt = 0;
8822      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8823      sqlite3_free(zSql);
8824      if( rx!=SQLITE_OK ){
8825        sqlite3_finalize(pStmt);
8826        pStmt = 0;
8827        zSql = sqlite3_mprintf(
8828                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8829                   "VALUES(%Q,%Q);", zKey, zValue);
8830        if( zSql==0 ) shell_out_of_memory();
8831        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8832        sqlite3_free(zSql);
8833        if( rx!=SQLITE_OK ){
8834          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8835          sqlite3_finalize(pStmt);
8836          pStmt = 0;
8837          rc = 1;
8838        }
8839      }
8840      sqlite3_step(pStmt);
8841      sqlite3_finalize(pStmt);
8842    }else
8843
8844    /* .parameter unset NAME
8845    ** Remove the NAME binding from the parameter binding table, if it
8846    ** exists.
8847    */
8848    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8849      char *zSql = sqlite3_mprintf(
8850          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8851      if( zSql==0 ) shell_out_of_memory();
8852      sqlite3_exec(p->db, zSql, 0, 0, 0);
8853      sqlite3_free(zSql);
8854    }else
8855    /* If no command name matches, show a syntax error */
8856    parameter_syntax_error:
8857    showHelp(p->out, "parameter");
8858  }else
8859
8860  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8861    int i;
8862    for(i=1; i<nArg; i++){
8863      if( i>1 ) raw_printf(p->out, " ");
8864      utf8_printf(p->out, "%s", azArg[i]);
8865    }
8866    raw_printf(p->out, "\n");
8867  }else
8868
8869#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8870  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8871    int i;
8872    int nn = 0;
8873    p->flgProgress = 0;
8874    p->mxProgress = 0;
8875    p->nProgress = 0;
8876    for(i=1; i<nArg; i++){
8877      const char *z = azArg[i];
8878      if( z[0]=='-' ){
8879        z++;
8880        if( z[0]=='-' ) z++;
8881        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8882          p->flgProgress |= SHELL_PROGRESS_QUIET;
8883          continue;
8884        }
8885        if( strcmp(z,"reset")==0 ){
8886          p->flgProgress |= SHELL_PROGRESS_RESET;
8887          continue;
8888        }
8889        if( strcmp(z,"once")==0 ){
8890          p->flgProgress |= SHELL_PROGRESS_ONCE;
8891          continue;
8892        }
8893        if( strcmp(z,"limit")==0 ){
8894          if( i+1>=nArg ){
8895            utf8_printf(stderr, "Error: missing argument on --limit\n");
8896            rc = 1;
8897            goto meta_command_exit;
8898          }else{
8899            p->mxProgress = (int)integerValue(azArg[++i]);
8900          }
8901          continue;
8902        }
8903        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8904        rc = 1;
8905        goto meta_command_exit;
8906      }else{
8907        nn = (int)integerValue(z);
8908      }
8909    }
8910    open_db(p, 0);
8911    sqlite3_progress_handler(p->db, nn, progress_handler, p);
8912  }else
8913#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
8914
8915  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8916    if( nArg >= 2) {
8917      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
8918    }
8919    if( nArg >= 3) {
8920      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
8921    }
8922  }else
8923
8924  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
8925    rc = 2;
8926  }else
8927
8928  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
8929    FILE *inSaved = p->in;
8930    int savedLineno = p->lineno;
8931    if( nArg!=2 ){
8932      raw_printf(stderr, "Usage: .read FILE\n");
8933      rc = 1;
8934      goto meta_command_exit;
8935    }
8936    p->in = fopen(azArg[1], "rb");
8937    if( p->in==0 ){
8938      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
8939      rc = 1;
8940    }else{
8941      rc = process_input(p);
8942      fclose(p->in);
8943    }
8944    p->in = inSaved;
8945    p->lineno = savedLineno;
8946  }else
8947
8948  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
8949    const char *zSrcFile;
8950    const char *zDb;
8951    sqlite3 *pSrc;
8952    sqlite3_backup *pBackup;
8953    int nTimeout = 0;
8954
8955    if( nArg==2 ){
8956      zSrcFile = azArg[1];
8957      zDb = "main";
8958    }else if( nArg==3 ){
8959      zSrcFile = azArg[2];
8960      zDb = azArg[1];
8961    }else{
8962      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
8963      rc = 1;
8964      goto meta_command_exit;
8965    }
8966    rc = sqlite3_open(zSrcFile, &pSrc);
8967    if( rc!=SQLITE_OK ){
8968      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
8969      close_db(pSrc);
8970      return 1;
8971    }
8972    open_db(p, 0);
8973    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
8974    if( pBackup==0 ){
8975      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8976      close_db(pSrc);
8977      return 1;
8978    }
8979    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
8980          || rc==SQLITE_BUSY  ){
8981      if( rc==SQLITE_BUSY ){
8982        if( nTimeout++ >= 3 ) break;
8983        sqlite3_sleep(100);
8984      }
8985    }
8986    sqlite3_backup_finish(pBackup);
8987    if( rc==SQLITE_DONE ){
8988      rc = 0;
8989    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
8990      raw_printf(stderr, "Error: source database is busy\n");
8991      rc = 1;
8992    }else{
8993      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8994      rc = 1;
8995    }
8996    close_db(pSrc);
8997  }else
8998
8999  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9000    if( nArg==2 ){
9001      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9002#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9003      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9004#endif
9005    }else{
9006      raw_printf(stderr, "Usage: .scanstats on|off\n");
9007      rc = 1;
9008    }
9009  }else
9010
9011  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9012    ShellText sSelect;
9013    ShellState data;
9014    char *zErrMsg = 0;
9015    const char *zDiv = "(";
9016    const char *zName = 0;
9017    int iSchema = 0;
9018    int bDebug = 0;
9019    int ii;
9020
9021    open_db(p, 0);
9022    memcpy(&data, p, sizeof(data));
9023    data.showHeader = 0;
9024    data.cMode = data.mode = MODE_Semi;
9025    initText(&sSelect);
9026    for(ii=1; ii<nArg; ii++){
9027      if( optionMatch(azArg[ii],"indent") ){
9028        data.cMode = data.mode = MODE_Pretty;
9029      }else if( optionMatch(azArg[ii],"debug") ){
9030        bDebug = 1;
9031      }else if( zName==0 ){
9032        zName = azArg[ii];
9033      }else{
9034        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
9035        rc = 1;
9036        goto meta_command_exit;
9037      }
9038    }
9039    if( zName!=0 ){
9040      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9041                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9042                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9043                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9044      if( isSchema ){
9045        char *new_argv[2], *new_colv[2];
9046        new_argv[0] = sqlite3_mprintf(
9047                      "CREATE TABLE %s (\n"
9048                      "  type text,\n"
9049                      "  name text,\n"
9050                      "  tbl_name text,\n"
9051                      "  rootpage integer,\n"
9052                      "  sql text\n"
9053                      ")", zName);
9054        new_argv[1] = 0;
9055        new_colv[0] = "sql";
9056        new_colv[1] = 0;
9057        callback(&data, 1, new_argv, new_colv);
9058        sqlite3_free(new_argv[0]);
9059      }
9060    }
9061    if( zDiv ){
9062      sqlite3_stmt *pStmt = 0;
9063      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9064                              -1, &pStmt, 0);
9065      if( rc ){
9066        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9067        sqlite3_finalize(pStmt);
9068        rc = 1;
9069        goto meta_command_exit;
9070      }
9071      appendText(&sSelect, "SELECT sql FROM", 0);
9072      iSchema = 0;
9073      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9074        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9075        char zScNum[30];
9076        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9077        appendText(&sSelect, zDiv, 0);
9078        zDiv = " UNION ALL ";
9079        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9080        if( sqlite3_stricmp(zDb, "main")!=0 ){
9081          appendText(&sSelect, zDb, '\'');
9082        }else{
9083          appendText(&sSelect, "NULL", 0);
9084        }
9085        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9086        appendText(&sSelect, zScNum, 0);
9087        appendText(&sSelect, " AS snum, ", 0);
9088        appendText(&sSelect, zDb, '\'');
9089        appendText(&sSelect, " AS sname FROM ", 0);
9090        appendText(&sSelect, zDb, quoteChar(zDb));
9091        appendText(&sSelect, ".sqlite_schema", 0);
9092      }
9093      sqlite3_finalize(pStmt);
9094#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9095      if( zName ){
9096        appendText(&sSelect,
9097           " UNION ALL SELECT shell_module_schema(name),"
9098           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9099        0);
9100      }
9101#endif
9102      appendText(&sSelect, ") WHERE ", 0);
9103      if( zName ){
9104        char *zQarg = sqlite3_mprintf("%Q", zName);
9105        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9106                    strchr(zName, '[') != 0;
9107        if( strchr(zName, '.') ){
9108          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9109        }else{
9110          appendText(&sSelect, "lower(tbl_name)", 0);
9111        }
9112        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9113        appendText(&sSelect, zQarg, 0);
9114        if( !bGlob ){
9115          appendText(&sSelect, " ESCAPE '\\' ", 0);
9116        }
9117        appendText(&sSelect, " AND ", 0);
9118        sqlite3_free(zQarg);
9119      }
9120      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
9121                           " ORDER BY snum, rowid", 0);
9122      if( bDebug ){
9123        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9124      }else{
9125        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9126      }
9127      freeText(&sSelect);
9128    }
9129    if( zErrMsg ){
9130      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9131      sqlite3_free(zErrMsg);
9132      rc = 1;
9133    }else if( rc != SQLITE_OK ){
9134      raw_printf(stderr,"Error: querying schema information\n");
9135      rc = 1;
9136    }else{
9137      rc = 0;
9138    }
9139  }else
9140
9141#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9142  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9143    sqlite3SelectTrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
9144  }else
9145#endif
9146
9147#if defined(SQLITE_ENABLE_SESSION)
9148  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9149    OpenSession *pSession = &p->aSession[0];
9150    char **azCmd = &azArg[1];
9151    int iSes = 0;
9152    int nCmd = nArg - 1;
9153    int i;
9154    if( nArg<=1 ) goto session_syntax_error;
9155    open_db(p, 0);
9156    if( nArg>=3 ){
9157      for(iSes=0; iSes<p->nSession; iSes++){
9158        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9159      }
9160      if( iSes<p->nSession ){
9161        pSession = &p->aSession[iSes];
9162        azCmd++;
9163        nCmd--;
9164      }else{
9165        pSession = &p->aSession[0];
9166        iSes = 0;
9167      }
9168    }
9169
9170    /* .session attach TABLE
9171    ** Invoke the sqlite3session_attach() interface to attach a particular
9172    ** table so that it is never filtered.
9173    */
9174    if( strcmp(azCmd[0],"attach")==0 ){
9175      if( nCmd!=2 ) goto session_syntax_error;
9176      if( pSession->p==0 ){
9177        session_not_open:
9178        raw_printf(stderr, "ERROR: No sessions are open\n");
9179      }else{
9180        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9181        if( rc ){
9182          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9183          rc = 0;
9184        }
9185      }
9186    }else
9187
9188    /* .session changeset FILE
9189    ** .session patchset FILE
9190    ** Write a changeset or patchset into a file.  The file is overwritten.
9191    */
9192    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9193      FILE *out = 0;
9194      if( nCmd!=2 ) goto session_syntax_error;
9195      if( pSession->p==0 ) goto session_not_open;
9196      out = fopen(azCmd[1], "wb");
9197      if( out==0 ){
9198        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9199                    azCmd[1]);
9200      }else{
9201        int szChng;
9202        void *pChng;
9203        if( azCmd[0][0]=='c' ){
9204          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9205        }else{
9206          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9207        }
9208        if( rc ){
9209          printf("Error: error code %d\n", rc);
9210          rc = 0;
9211        }
9212        if( pChng
9213          && fwrite(pChng, szChng, 1, out)!=1 ){
9214          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9215                  szChng);
9216        }
9217        sqlite3_free(pChng);
9218        fclose(out);
9219      }
9220    }else
9221
9222    /* .session close
9223    ** Close the identified session
9224    */
9225    if( strcmp(azCmd[0], "close")==0 ){
9226      if( nCmd!=1 ) goto session_syntax_error;
9227      if( p->nSession ){
9228        session_close(pSession);
9229        p->aSession[iSes] = p->aSession[--p->nSession];
9230      }
9231    }else
9232
9233    /* .session enable ?BOOLEAN?
9234    ** Query or set the enable flag
9235    */
9236    if( strcmp(azCmd[0], "enable")==0 ){
9237      int ii;
9238      if( nCmd>2 ) goto session_syntax_error;
9239      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9240      if( p->nSession ){
9241        ii = sqlite3session_enable(pSession->p, ii);
9242        utf8_printf(p->out, "session %s enable flag = %d\n",
9243                    pSession->zName, ii);
9244      }
9245    }else
9246
9247    /* .session filter GLOB ....
9248    ** Set a list of GLOB patterns of table names to be excluded.
9249    */
9250    if( strcmp(azCmd[0], "filter")==0 ){
9251      int ii, nByte;
9252      if( nCmd<2 ) goto session_syntax_error;
9253      if( p->nSession ){
9254        for(ii=0; ii<pSession->nFilter; ii++){
9255          sqlite3_free(pSession->azFilter[ii]);
9256        }
9257        sqlite3_free(pSession->azFilter);
9258        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9259        pSession->azFilter = sqlite3_malloc( nByte );
9260        if( pSession->azFilter==0 ){
9261          raw_printf(stderr, "Error: out or memory\n");
9262          exit(1);
9263        }
9264        for(ii=1; ii<nCmd; ii++){
9265          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9266        }
9267        pSession->nFilter = ii-1;
9268      }
9269    }else
9270
9271    /* .session indirect ?BOOLEAN?
9272    ** Query or set the indirect flag
9273    */
9274    if( strcmp(azCmd[0], "indirect")==0 ){
9275      int ii;
9276      if( nCmd>2 ) goto session_syntax_error;
9277      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9278      if( p->nSession ){
9279        ii = sqlite3session_indirect(pSession->p, ii);
9280        utf8_printf(p->out, "session %s indirect flag = %d\n",
9281                    pSession->zName, ii);
9282      }
9283    }else
9284
9285    /* .session isempty
9286    ** Determine if the session is empty
9287    */
9288    if( strcmp(azCmd[0], "isempty")==0 ){
9289      int ii;
9290      if( nCmd!=1 ) goto session_syntax_error;
9291      if( p->nSession ){
9292        ii = sqlite3session_isempty(pSession->p);
9293        utf8_printf(p->out, "session %s isempty flag = %d\n",
9294                    pSession->zName, ii);
9295      }
9296    }else
9297
9298    /* .session list
9299    ** List all currently open sessions
9300    */
9301    if( strcmp(azCmd[0],"list")==0 ){
9302      for(i=0; i<p->nSession; i++){
9303        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9304      }
9305    }else
9306
9307    /* .session open DB NAME
9308    ** Open a new session called NAME on the attached database DB.
9309    ** DB is normally "main".
9310    */
9311    if( strcmp(azCmd[0],"open")==0 ){
9312      char *zName;
9313      if( nCmd!=3 ) goto session_syntax_error;
9314      zName = azCmd[2];
9315      if( zName[0]==0 ) goto session_syntax_error;
9316      for(i=0; i<p->nSession; i++){
9317        if( strcmp(p->aSession[i].zName,zName)==0 ){
9318          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9319          goto meta_command_exit;
9320        }
9321      }
9322      if( p->nSession>=ArraySize(p->aSession) ){
9323        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9324        goto meta_command_exit;
9325      }
9326      pSession = &p->aSession[p->nSession];
9327      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9328      if( rc ){
9329        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9330        rc = 0;
9331        goto meta_command_exit;
9332      }
9333      pSession->nFilter = 0;
9334      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9335      p->nSession++;
9336      pSession->zName = sqlite3_mprintf("%s", zName);
9337    }else
9338    /* If no command name matches, show a syntax error */
9339    session_syntax_error:
9340    showHelp(p->out, "session");
9341  }else
9342#endif
9343
9344#ifdef SQLITE_DEBUG
9345  /* Undocumented commands for internal testing.  Subject to change
9346  ** without notice. */
9347  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9348    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9349      int i, v;
9350      for(i=1; i<nArg; i++){
9351        v = booleanValue(azArg[i]);
9352        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9353      }
9354    }
9355    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9356      int i; sqlite3_int64 v;
9357      for(i=1; i<nArg; i++){
9358        char zBuf[200];
9359        v = integerValue(azArg[i]);
9360        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9361        utf8_printf(p->out, "%s", zBuf);
9362      }
9363    }
9364  }else
9365#endif
9366
9367  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9368    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9369    int bVerbose = 0;        /* Verbose output */
9370    int bSelftestExists;     /* True if SELFTEST already exists */
9371    int i, k;                /* Loop counters */
9372    int nTest = 0;           /* Number of tests runs */
9373    int nErr = 0;            /* Number of errors seen */
9374    ShellText str;           /* Answer for a query */
9375    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9376
9377    open_db(p,0);
9378    for(i=1; i<nArg; i++){
9379      const char *z = azArg[i];
9380      if( z[0]=='-' && z[1]=='-' ) z++;
9381      if( strcmp(z,"-init")==0 ){
9382        bIsInit = 1;
9383      }else
9384      if( strcmp(z,"-v")==0 ){
9385        bVerbose++;
9386      }else
9387      {
9388        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9389                    azArg[i], azArg[0]);
9390        raw_printf(stderr, "Should be one of: --init -v\n");
9391        rc = 1;
9392        goto meta_command_exit;
9393      }
9394    }
9395    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9396           != SQLITE_OK ){
9397      bSelftestExists = 0;
9398    }else{
9399      bSelftestExists = 1;
9400    }
9401    if( bIsInit ){
9402      createSelftestTable(p);
9403      bSelftestExists = 1;
9404    }
9405    initText(&str);
9406    appendText(&str, "x", 0);
9407    for(k=bSelftestExists; k>=0; k--){
9408      if( k==1 ){
9409        rc = sqlite3_prepare_v2(p->db,
9410            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9411            -1, &pStmt, 0);
9412      }else{
9413        rc = sqlite3_prepare_v2(p->db,
9414          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9415          "      (1,'run','PRAGMA integrity_check','ok')",
9416          -1, &pStmt, 0);
9417      }
9418      if( rc ){
9419        raw_printf(stderr, "Error querying the selftest table\n");
9420        rc = 1;
9421        sqlite3_finalize(pStmt);
9422        goto meta_command_exit;
9423      }
9424      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9425        int tno = sqlite3_column_int(pStmt, 0);
9426        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9427        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9428        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9429
9430        k = 0;
9431        if( bVerbose>0 ){
9432          char *zQuote = sqlite3_mprintf("%q", zSql);
9433          printf("%d: %s %s\n", tno, zOp, zSql);
9434          sqlite3_free(zQuote);
9435        }
9436        if( strcmp(zOp,"memo")==0 ){
9437          utf8_printf(p->out, "%s\n", zSql);
9438        }else
9439        if( strcmp(zOp,"run")==0 ){
9440          char *zErrMsg = 0;
9441          str.n = 0;
9442          str.z[0] = 0;
9443          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9444          nTest++;
9445          if( bVerbose ){
9446            utf8_printf(p->out, "Result: %s\n", str.z);
9447          }
9448          if( rc || zErrMsg ){
9449            nErr++;
9450            rc = 1;
9451            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9452            sqlite3_free(zErrMsg);
9453          }else if( strcmp(zAns,str.z)!=0 ){
9454            nErr++;
9455            rc = 1;
9456            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9457            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9458          }
9459        }else
9460        {
9461          utf8_printf(stderr,
9462            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9463          rc = 1;
9464          break;
9465        }
9466      } /* End loop over rows of content from SELFTEST */
9467      sqlite3_finalize(pStmt);
9468    } /* End loop over k */
9469    freeText(&str);
9470    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9471  }else
9472
9473  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9474    if( nArg<2 || nArg>3 ){
9475      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9476      rc = 1;
9477    }
9478    if( nArg>=2 ){
9479      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9480                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9481    }
9482    if( nArg>=3 ){
9483      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9484                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9485    }
9486  }else
9487
9488  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9489    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9490    int i;                   /* Loop counter */
9491    int bSchema = 0;         /* Also hash the schema */
9492    int bSeparate = 0;       /* Hash each table separately */
9493    int iSize = 224;         /* Hash algorithm to use */
9494    int bDebug = 0;          /* Only show the query that would have run */
9495    sqlite3_stmt *pStmt;     /* For querying tables names */
9496    char *zSql;              /* SQL to be run */
9497    char *zSep;              /* Separator */
9498    ShellText sSql;          /* Complete SQL for the query to run the hash */
9499    ShellText sQuery;        /* Set of queries used to read all content */
9500    open_db(p, 0);
9501    for(i=1; i<nArg; i++){
9502      const char *z = azArg[i];
9503      if( z[0]=='-' ){
9504        z++;
9505        if( z[0]=='-' ) z++;
9506        if( strcmp(z,"schema")==0 ){
9507          bSchema = 1;
9508        }else
9509        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9510         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9511        ){
9512          iSize = atoi(&z[5]);
9513        }else
9514        if( strcmp(z,"debug")==0 ){
9515          bDebug = 1;
9516        }else
9517        {
9518          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9519                      azArg[i], azArg[0]);
9520          showHelp(p->out, azArg[0]);
9521          rc = 1;
9522          goto meta_command_exit;
9523        }
9524      }else if( zLike ){
9525        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9526        rc = 1;
9527        goto meta_command_exit;
9528      }else{
9529        zLike = z;
9530        bSeparate = 1;
9531        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9532      }
9533    }
9534    if( bSchema ){
9535      zSql = "SELECT lower(name) FROM sqlite_schema"
9536             " WHERE type='table' AND coalesce(rootpage,0)>1"
9537             " UNION ALL SELECT 'sqlite_schema'"
9538             " ORDER BY 1 collate nocase";
9539    }else{
9540      zSql = "SELECT lower(name) FROM sqlite_schema"
9541             " WHERE type='table' AND coalesce(rootpage,0)>1"
9542             " AND name NOT LIKE 'sqlite_%'"
9543             " ORDER BY 1 collate nocase";
9544    }
9545    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9546    initText(&sQuery);
9547    initText(&sSql);
9548    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9549    zSep = "VALUES(";
9550    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9551      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9552      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9553      if( strncmp(zTab, "sqlite_",7)!=0 ){
9554        appendText(&sQuery,"SELECT * FROM ", 0);
9555        appendText(&sQuery,zTab,'"');
9556        appendText(&sQuery," NOT INDEXED;", 0);
9557      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9558        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9559                           " ORDER BY name;", 0);
9560      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9561        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9562                           " ORDER BY name;", 0);
9563      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9564        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9565                           " ORDER BY tbl,idx;", 0);
9566      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9567        appendText(&sQuery, "SELECT * FROM ", 0);
9568        appendText(&sQuery, zTab, 0);
9569        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9570      }
9571      appendText(&sSql, zSep, 0);
9572      appendText(&sSql, sQuery.z, '\'');
9573      sQuery.n = 0;
9574      appendText(&sSql, ",", 0);
9575      appendText(&sSql, zTab, '\'');
9576      zSep = "),(";
9577    }
9578    sqlite3_finalize(pStmt);
9579    if( bSeparate ){
9580      zSql = sqlite3_mprintf(
9581          "%s))"
9582          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9583          "   FROM [sha3sum$query]",
9584          sSql.z, iSize);
9585    }else{
9586      zSql = sqlite3_mprintf(
9587          "%s))"
9588          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9589          "   FROM [sha3sum$query]",
9590          sSql.z, iSize);
9591    }
9592    freeText(&sQuery);
9593    freeText(&sSql);
9594    if( bDebug ){
9595      utf8_printf(p->out, "%s\n", zSql);
9596    }else{
9597      shell_exec(p, zSql, 0);
9598    }
9599    sqlite3_free(zSql);
9600  }else
9601
9602#ifndef SQLITE_NOHAVE_SYSTEM
9603  if( c=='s'
9604   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9605  ){
9606    char *zCmd;
9607    int i, x;
9608    if( nArg<2 ){
9609      raw_printf(stderr, "Usage: .system COMMAND\n");
9610      rc = 1;
9611      goto meta_command_exit;
9612    }
9613    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9614    for(i=2; i<nArg; i++){
9615      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9616                             zCmd, azArg[i]);
9617    }
9618    x = system(zCmd);
9619    sqlite3_free(zCmd);
9620    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9621  }else
9622#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9623
9624  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9625    static const char *azBool[] = { "off", "on", "trigger", "full"};
9626    int i;
9627    if( nArg!=1 ){
9628      raw_printf(stderr, "Usage: .show\n");
9629      rc = 1;
9630      goto meta_command_exit;
9631    }
9632    utf8_printf(p->out, "%12.12s: %s\n","echo",
9633                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9634    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9635    utf8_printf(p->out, "%12.12s: %s\n","explain",
9636         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9637    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9638    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9639    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9640      output_c_string(p->out, p->nullValue);
9641      raw_printf(p->out, "\n");
9642    utf8_printf(p->out,"%12.12s: %s\n","output",
9643            strlen30(p->outfile) ? p->outfile : "stdout");
9644    utf8_printf(p->out,"%12.12s: ", "colseparator");
9645      output_c_string(p->out, p->colSeparator);
9646      raw_printf(p->out, "\n");
9647    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9648      output_c_string(p->out, p->rowSeparator);
9649      raw_printf(p->out, "\n");
9650    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9651    utf8_printf(p->out, "%12.12s: ", "width");
9652    for (i=0;i<p->nWidth;i++) {
9653      raw_printf(p->out, "%d ", p->colWidth[i]);
9654    }
9655    raw_printf(p->out, "\n");
9656    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9657                p->zDbFilename ? p->zDbFilename : "");
9658  }else
9659
9660  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9661    if( nArg==2 ){
9662      p->statsOn = (u8)booleanValue(azArg[1]);
9663    }else if( nArg==1 ){
9664      display_stats(p->db, p, 0);
9665    }else{
9666      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9667      rc = 1;
9668    }
9669  }else
9670
9671  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9672   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9673                 || strncmp(azArg[0], "indexes", n)==0) )
9674  ){
9675    sqlite3_stmt *pStmt;
9676    char **azResult;
9677    int nRow, nAlloc;
9678    int ii;
9679    ShellText s;
9680    initText(&s);
9681    open_db(p, 0);
9682    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9683    if( rc ){
9684      sqlite3_finalize(pStmt);
9685      return shellDatabaseError(p->db);
9686    }
9687
9688    if( nArg>2 && c=='i' ){
9689      /* It is an historical accident that the .indexes command shows an error
9690      ** when called with the wrong number of arguments whereas the .tables
9691      ** command does not. */
9692      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9693      rc = 1;
9694      sqlite3_finalize(pStmt);
9695      goto meta_command_exit;
9696    }
9697    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9698      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9699      if( zDbName==0 ) continue;
9700      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9701      if( sqlite3_stricmp(zDbName, "main")==0 ){
9702        appendText(&s, "SELECT name FROM ", 0);
9703      }else{
9704        appendText(&s, "SELECT ", 0);
9705        appendText(&s, zDbName, '\'');
9706        appendText(&s, "||'.'||name FROM ", 0);
9707      }
9708      appendText(&s, zDbName, '"');
9709      appendText(&s, ".sqlite_schema ", 0);
9710      if( c=='t' ){
9711        appendText(&s," WHERE type IN ('table','view')"
9712                      "   AND name NOT LIKE 'sqlite_%'"
9713                      "   AND name LIKE ?1", 0);
9714      }else{
9715        appendText(&s," WHERE type='index'"
9716                      "   AND tbl_name LIKE ?1", 0);
9717      }
9718    }
9719    rc = sqlite3_finalize(pStmt);
9720    appendText(&s, " ORDER BY 1", 0);
9721    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9722    freeText(&s);
9723    if( rc ) return shellDatabaseError(p->db);
9724
9725    /* Run the SQL statement prepared by the above block. Store the results
9726    ** as an array of nul-terminated strings in azResult[].  */
9727    nRow = nAlloc = 0;
9728    azResult = 0;
9729    if( nArg>1 ){
9730      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9731    }else{
9732      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9733    }
9734    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9735      if( nRow>=nAlloc ){
9736        char **azNew;
9737        int n2 = nAlloc*2 + 10;
9738        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9739        if( azNew==0 ) shell_out_of_memory();
9740        nAlloc = n2;
9741        azResult = azNew;
9742      }
9743      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9744      if( 0==azResult[nRow] ) shell_out_of_memory();
9745      nRow++;
9746    }
9747    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9748      rc = shellDatabaseError(p->db);
9749    }
9750
9751    /* Pretty-print the contents of array azResult[] to the output */
9752    if( rc==0 && nRow>0 ){
9753      int len, maxlen = 0;
9754      int i, j;
9755      int nPrintCol, nPrintRow;
9756      for(i=0; i<nRow; i++){
9757        len = strlen30(azResult[i]);
9758        if( len>maxlen ) maxlen = len;
9759      }
9760      nPrintCol = 80/(maxlen+2);
9761      if( nPrintCol<1 ) nPrintCol = 1;
9762      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9763      for(i=0; i<nPrintRow; i++){
9764        for(j=i; j<nRow; j+=nPrintRow){
9765          char *zSp = j<nPrintRow ? "" : "  ";
9766          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9767                      azResult[j] ? azResult[j]:"");
9768        }
9769        raw_printf(p->out, "\n");
9770      }
9771    }
9772
9773    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9774    sqlite3_free(azResult);
9775  }else
9776
9777  /* Begin redirecting output to the file "testcase-out.txt" */
9778  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9779    output_reset(p);
9780    p->out = output_file_open("testcase-out.txt", 0);
9781    if( p->out==0 ){
9782      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9783    }
9784    if( nArg>=2 ){
9785      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9786    }else{
9787      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9788    }
9789  }else
9790
9791#ifndef SQLITE_UNTESTABLE
9792  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9793    static const struct {
9794       const char *zCtrlName;   /* Name of a test-control option */
9795       int ctrlCode;            /* Integer code for that option */
9796       const char *zUsage;      /* Usage notes */
9797    } aCtrl[] = {
9798      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9799      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9800    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9801    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9802      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9803      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9804    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9805      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9806      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9807      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9808      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9809      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9810#ifdef YYCOVERAGE
9811      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9812#endif
9813      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9814      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9815      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9816      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9817    };
9818    int testctrl = -1;
9819    int iCtrl = -1;
9820    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9821    int isOk = 0;
9822    int i, n2;
9823    const char *zCmd = 0;
9824
9825    open_db(p, 0);
9826    zCmd = nArg>=2 ? azArg[1] : "help";
9827
9828    /* The argument can optionally begin with "-" or "--" */
9829    if( zCmd[0]=='-' && zCmd[1] ){
9830      zCmd++;
9831      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9832    }
9833
9834    /* --help lists all test-controls */
9835    if( strcmp(zCmd,"help")==0 ){
9836      utf8_printf(p->out, "Available test-controls:\n");
9837      for(i=0; i<ArraySize(aCtrl); i++){
9838        utf8_printf(p->out, "  .testctrl %s %s\n",
9839                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9840      }
9841      rc = 1;
9842      goto meta_command_exit;
9843    }
9844
9845    /* convert testctrl text option to value. allow any unique prefix
9846    ** of the option name, or a numerical value. */
9847    n2 = strlen30(zCmd);
9848    for(i=0; i<ArraySize(aCtrl); i++){
9849      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9850        if( testctrl<0 ){
9851          testctrl = aCtrl[i].ctrlCode;
9852          iCtrl = i;
9853        }else{
9854          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9855                              "Use \".testctrl --help\" for help\n", zCmd);
9856          rc = 1;
9857          goto meta_command_exit;
9858        }
9859      }
9860    }
9861    if( testctrl<0 ){
9862      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9863                         "Use \".testctrl --help\" for help\n", zCmd);
9864    }else{
9865      switch(testctrl){
9866
9867        /* sqlite3_test_control(int, db, int) */
9868        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9869          if( nArg==3 ){
9870            int opt = (int)strtol(azArg[2], 0, 0);
9871            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9872            isOk = 3;
9873          }
9874          break;
9875
9876        /* sqlite3_test_control(int) */
9877        case SQLITE_TESTCTRL_PRNG_SAVE:
9878        case SQLITE_TESTCTRL_PRNG_RESTORE:
9879        case SQLITE_TESTCTRL_PRNG_RESET:
9880        case SQLITE_TESTCTRL_BYTEORDER:
9881          if( nArg==2 ){
9882            rc2 = sqlite3_test_control(testctrl);
9883            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9884          }
9885          break;
9886
9887        /* sqlite3_test_control(int, uint) */
9888        case SQLITE_TESTCTRL_PENDING_BYTE:
9889          if( nArg==3 ){
9890            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9891            rc2 = sqlite3_test_control(testctrl, opt);
9892            isOk = 3;
9893          }
9894          break;
9895
9896        /* sqlite3_test_control(int, int, sqlite3*) */
9897        case SQLITE_TESTCTRL_PRNG_SEED:
9898          if( nArg==3 || nArg==4 ){
9899            int ii = (int)integerValue(azArg[2]);
9900            sqlite3 *db;
9901            if( ii==0 && strcmp(azArg[2],"random")==0 ){
9902              sqlite3_randomness(sizeof(ii),&ii);
9903              printf("-- random seed: %d\n", ii);
9904            }
9905            if( nArg==3 ){
9906              db = 0;
9907            }else{
9908              db = p->db;
9909              /* Make sure the schema has been loaded */
9910              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
9911            }
9912            rc2 = sqlite3_test_control(testctrl, ii, db);
9913            isOk = 3;
9914          }
9915          break;
9916
9917        /* sqlite3_test_control(int, int) */
9918        case SQLITE_TESTCTRL_ASSERT:
9919        case SQLITE_TESTCTRL_ALWAYS:
9920          if( nArg==3 ){
9921            int opt = booleanValue(azArg[2]);
9922            rc2 = sqlite3_test_control(testctrl, opt);
9923            isOk = 1;
9924          }
9925          break;
9926
9927        /* sqlite3_test_control(int, int) */
9928        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
9929        case SQLITE_TESTCTRL_NEVER_CORRUPT:
9930          if( nArg==3 ){
9931            int opt = booleanValue(azArg[2]);
9932            rc2 = sqlite3_test_control(testctrl, opt);
9933            isOk = 3;
9934          }
9935          break;
9936
9937        /* sqlite3_test_control(sqlite3*) */
9938        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
9939          rc2 = sqlite3_test_control(testctrl, p->db);
9940          isOk = 3;
9941          break;
9942
9943        case SQLITE_TESTCTRL_IMPOSTER:
9944          if( nArg==5 ){
9945            rc2 = sqlite3_test_control(testctrl, p->db,
9946                          azArg[2],
9947                          integerValue(azArg[3]),
9948                          integerValue(azArg[4]));
9949            isOk = 3;
9950          }
9951          break;
9952
9953#ifdef YYCOVERAGE
9954        case SQLITE_TESTCTRL_PARSER_COVERAGE:
9955          if( nArg==2 ){
9956            sqlite3_test_control(testctrl, p->out);
9957            isOk = 3;
9958          }
9959#endif
9960      }
9961    }
9962    if( isOk==0 && iCtrl>=0 ){
9963      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
9964      rc = 1;
9965    }else if( isOk==1 ){
9966      raw_printf(p->out, "%d\n", rc2);
9967    }else if( isOk==2 ){
9968      raw_printf(p->out, "0x%08x\n", rc2);
9969    }
9970  }else
9971#endif /* !defined(SQLITE_UNTESTABLE) */
9972
9973  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
9974    open_db(p, 0);
9975    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
9976  }else
9977
9978  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
9979    if( nArg==2 ){
9980      enableTimer = booleanValue(azArg[1]);
9981      if( enableTimer && !HAS_TIMER ){
9982        raw_printf(stderr, "Error: timer not available on this system.\n");
9983        enableTimer = 0;
9984      }
9985    }else{
9986      raw_printf(stderr, "Usage: .timer on|off\n");
9987      rc = 1;
9988    }
9989  }else
9990
9991#ifndef SQLITE_OMIT_TRACE
9992  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
9993    int mType = 0;
9994    int jj;
9995    open_db(p, 0);
9996    for(jj=1; jj<nArg; jj++){
9997      const char *z = azArg[jj];
9998      if( z[0]=='-' ){
9999        if( optionMatch(z, "expanded") ){
10000          p->eTraceType = SHELL_TRACE_EXPANDED;
10001        }
10002#ifdef SQLITE_ENABLE_NORMALIZE
10003        else if( optionMatch(z, "normalized") ){
10004          p->eTraceType = SHELL_TRACE_NORMALIZED;
10005        }
10006#endif
10007        else if( optionMatch(z, "plain") ){
10008          p->eTraceType = SHELL_TRACE_PLAIN;
10009        }
10010        else if( optionMatch(z, "profile") ){
10011          mType |= SQLITE_TRACE_PROFILE;
10012        }
10013        else if( optionMatch(z, "row") ){
10014          mType |= SQLITE_TRACE_ROW;
10015        }
10016        else if( optionMatch(z, "stmt") ){
10017          mType |= SQLITE_TRACE_STMT;
10018        }
10019        else if( optionMatch(z, "close") ){
10020          mType |= SQLITE_TRACE_CLOSE;
10021        }
10022        else {
10023          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10024          rc = 1;
10025          goto meta_command_exit;
10026        }
10027      }else{
10028        output_file_close(p->traceOut);
10029        p->traceOut = output_file_open(azArg[1], 0);
10030      }
10031    }
10032    if( p->traceOut==0 ){
10033      sqlite3_trace_v2(p->db, 0, 0, 0);
10034    }else{
10035      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10036      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10037    }
10038  }else
10039#endif /* !defined(SQLITE_OMIT_TRACE) */
10040
10041#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10042  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10043    int ii;
10044    int lenOpt;
10045    char *zOpt;
10046    if( nArg<2 ){
10047      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10048      rc = 1;
10049      goto meta_command_exit;
10050    }
10051    open_db(p, 0);
10052    zOpt = azArg[1];
10053    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10054    lenOpt = (int)strlen(zOpt);
10055    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10056      assert( azArg[nArg]==0 );
10057      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10058    }else{
10059      for(ii=1; ii<nArg; ii++){
10060        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10061      }
10062    }
10063  }else
10064#endif
10065
10066#if SQLITE_USER_AUTHENTICATION
10067  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10068    if( nArg<2 ){
10069      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10070      rc = 1;
10071      goto meta_command_exit;
10072    }
10073    open_db(p, 0);
10074    if( strcmp(azArg[1],"login")==0 ){
10075      if( nArg!=4 ){
10076        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10077        rc = 1;
10078        goto meta_command_exit;
10079      }
10080      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10081                                     strlen30(azArg[3]));
10082      if( rc ){
10083        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10084        rc = 1;
10085      }
10086    }else if( strcmp(azArg[1],"add")==0 ){
10087      if( nArg!=5 ){
10088        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10089        rc = 1;
10090        goto meta_command_exit;
10091      }
10092      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10093                            booleanValue(azArg[4]));
10094      if( rc ){
10095        raw_printf(stderr, "User-Add failed: %d\n", rc);
10096        rc = 1;
10097      }
10098    }else if( strcmp(azArg[1],"edit")==0 ){
10099      if( nArg!=5 ){
10100        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10101        rc = 1;
10102        goto meta_command_exit;
10103      }
10104      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10105                              booleanValue(azArg[4]));
10106      if( rc ){
10107        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10108        rc = 1;
10109      }
10110    }else if( strcmp(azArg[1],"delete")==0 ){
10111      if( nArg!=3 ){
10112        raw_printf(stderr, "Usage: .user delete USER\n");
10113        rc = 1;
10114        goto meta_command_exit;
10115      }
10116      rc = sqlite3_user_delete(p->db, azArg[2]);
10117      if( rc ){
10118        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10119        rc = 1;
10120      }
10121    }else{
10122      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10123      rc = 1;
10124      goto meta_command_exit;
10125    }
10126  }else
10127#endif /* SQLITE_USER_AUTHENTICATION */
10128
10129  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10130    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10131        sqlite3_libversion(), sqlite3_sourceid());
10132#if SQLITE_HAVE_ZLIB
10133    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10134#endif
10135#define CTIMEOPT_VAL_(opt) #opt
10136#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10137#if defined(__clang__) && defined(__clang_major__)
10138    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10139                    CTIMEOPT_VAL(__clang_minor__) "."
10140                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10141#elif defined(_MSC_VER)
10142    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10143#elif defined(__GNUC__) && defined(__VERSION__)
10144    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10145#endif
10146  }else
10147
10148  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10149    const char *zDbName = nArg==2 ? azArg[1] : "main";
10150    sqlite3_vfs *pVfs = 0;
10151    if( p->db ){
10152      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10153      if( pVfs ){
10154        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10155        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10156        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10157        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10158      }
10159    }
10160  }else
10161
10162  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10163    sqlite3_vfs *pVfs;
10164    sqlite3_vfs *pCurrent = 0;
10165    if( p->db ){
10166      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10167    }
10168    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10169      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10170           pVfs==pCurrent ? "  <--- CURRENT" : "");
10171      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10172      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10173      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10174      if( pVfs->pNext ){
10175        raw_printf(p->out, "-----------------------------------\n");
10176      }
10177    }
10178  }else
10179
10180  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10181    const char *zDbName = nArg==2 ? azArg[1] : "main";
10182    char *zVfsName = 0;
10183    if( p->db ){
10184      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10185      if( zVfsName ){
10186        utf8_printf(p->out, "%s\n", zVfsName);
10187        sqlite3_free(zVfsName);
10188      }
10189    }
10190  }else
10191
10192#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10193  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10194    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
10195  }else
10196#endif
10197
10198  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10199    int j;
10200    assert( nArg<=ArraySize(azArg) );
10201    p->nWidth = nArg-1;
10202    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10203    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10204    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10205    for(j=1; j<nArg; j++){
10206      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10207    }
10208  }else
10209
10210  {
10211    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10212      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10213    rc = 1;
10214  }
10215
10216meta_command_exit:
10217  if( p->outCount ){
10218    p->outCount--;
10219    if( p->outCount==0 ) output_reset(p);
10220  }
10221  return rc;
10222}
10223
10224/*
10225** Return TRUE if a semicolon occurs anywhere in the first N characters
10226** of string z[].
10227*/
10228static int line_contains_semicolon(const char *z, int N){
10229  int i;
10230  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10231  return 0;
10232}
10233
10234/*
10235** Test to see if a line consists entirely of whitespace.
10236*/
10237static int _all_whitespace(const char *z){
10238  for(; *z; z++){
10239    if( IsSpace(z[0]) ) continue;
10240    if( *z=='/' && z[1]=='*' ){
10241      z += 2;
10242      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10243      if( *z==0 ) return 0;
10244      z++;
10245      continue;
10246    }
10247    if( *z=='-' && z[1]=='-' ){
10248      z += 2;
10249      while( *z && *z!='\n' ){ z++; }
10250      if( *z==0 ) return 1;
10251      continue;
10252    }
10253    return 0;
10254  }
10255  return 1;
10256}
10257
10258/*
10259** Return TRUE if the line typed in is an SQL command terminator other
10260** than a semi-colon.  The SQL Server style "go" command is understood
10261** as is the Oracle "/".
10262*/
10263static int line_is_command_terminator(const char *zLine){
10264  while( IsSpace(zLine[0]) ){ zLine++; };
10265  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10266    return 1;  /* Oracle */
10267  }
10268  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10269         && _all_whitespace(&zLine[2]) ){
10270    return 1;  /* SQL Server */
10271  }
10272  return 0;
10273}
10274
10275/*
10276** We need a default sqlite3_complete() implementation to use in case
10277** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10278** any arbitrary text is a complete SQL statement.  This is not very
10279** user-friendly, but it does seem to work.
10280*/
10281#ifdef SQLITE_OMIT_COMPLETE
10282#define sqlite3_complete(x) 1
10283#endif
10284
10285/*
10286** Return true if zSql is a complete SQL statement.  Return false if it
10287** ends in the middle of a string literal or C-style comment.
10288*/
10289static int line_is_complete(char *zSql, int nSql){
10290  int rc;
10291  if( zSql==0 ) return 1;
10292  zSql[nSql] = ';';
10293  zSql[nSql+1] = 0;
10294  rc = sqlite3_complete(zSql);
10295  zSql[nSql] = 0;
10296  return rc;
10297}
10298
10299/*
10300** Run a single line of SQL.  Return the number of errors.
10301*/
10302static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10303  int rc;
10304  char *zErrMsg = 0;
10305
10306  open_db(p, 0);
10307  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10308  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10309  BEGIN_TIMER;
10310  rc = shell_exec(p, zSql, &zErrMsg);
10311  END_TIMER;
10312  if( rc || zErrMsg ){
10313    char zPrefix[100];
10314    if( in!=0 || !stdin_is_interactive ){
10315      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10316                       "Error: near line %d:", startline);
10317    }else{
10318      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10319    }
10320    if( zErrMsg!=0 ){
10321      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10322      sqlite3_free(zErrMsg);
10323      zErrMsg = 0;
10324    }else{
10325      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10326    }
10327    return 1;
10328  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10329    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
10330            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10331  }
10332  return 0;
10333}
10334
10335
10336/*
10337** Read input from *in and process it.  If *in==0 then input
10338** is interactive - the user is typing it it.  Otherwise, input
10339** is coming from a file or device.  A prompt is issued and history
10340** is saved only if input is interactive.  An interrupt signal will
10341** cause this routine to exit immediately, unless input is interactive.
10342**
10343** Return the number of errors.
10344*/
10345static int process_input(ShellState *p){
10346  char *zLine = 0;          /* A single input line */
10347  char *zSql = 0;           /* Accumulated SQL text */
10348  int nLine;                /* Length of current line */
10349  int nSql = 0;             /* Bytes of zSql[] used */
10350  int nAlloc = 0;           /* Allocated zSql[] space */
10351  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10352  int rc;                   /* Error code */
10353  int errCnt = 0;           /* Number of errors seen */
10354  int startline = 0;        /* Line number for start of current input */
10355
10356  p->lineno = 0;
10357  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10358    fflush(p->out);
10359    zLine = one_input_line(p->in, zLine, nSql>0);
10360    if( zLine==0 ){
10361      /* End of input */
10362      if( p->in==0 && stdin_is_interactive ) printf("\n");
10363      break;
10364    }
10365    if( seenInterrupt ){
10366      if( p->in!=0 ) break;
10367      seenInterrupt = 0;
10368    }
10369    p->lineno++;
10370    if( nSql==0 && _all_whitespace(zLine) ){
10371      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10372      continue;
10373    }
10374    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10375      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10376      if( zLine[0]=='.' ){
10377        rc = do_meta_command(zLine, p);
10378        if( rc==2 ){ /* exit requested */
10379          break;
10380        }else if( rc ){
10381          errCnt++;
10382        }
10383      }
10384      continue;
10385    }
10386    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10387      memcpy(zLine,";",2);
10388    }
10389    nLine = strlen30(zLine);
10390    if( nSql+nLine+2>=nAlloc ){
10391      nAlloc = nSql+nLine+100;
10392      zSql = realloc(zSql, nAlloc);
10393      if( zSql==0 ) shell_out_of_memory();
10394    }
10395    nSqlPrior = nSql;
10396    if( nSql==0 ){
10397      int i;
10398      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10399      assert( nAlloc>0 && zSql!=0 );
10400      memcpy(zSql, zLine+i, nLine+1-i);
10401      startline = p->lineno;
10402      nSql = nLine-i;
10403    }else{
10404      zSql[nSql++] = '\n';
10405      memcpy(zSql+nSql, zLine, nLine+1);
10406      nSql += nLine;
10407    }
10408    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10409                && sqlite3_complete(zSql) ){
10410      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10411      nSql = 0;
10412      if( p->outCount ){
10413        output_reset(p);
10414        p->outCount = 0;
10415      }else{
10416        clearTempFile(p);
10417      }
10418    }else if( nSql && _all_whitespace(zSql) ){
10419      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10420      nSql = 0;
10421    }
10422  }
10423  if( nSql && !_all_whitespace(zSql) ){
10424    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10425  }
10426  free(zSql);
10427  free(zLine);
10428  return errCnt>0;
10429}
10430
10431/*
10432** Return a pathname which is the user's home directory.  A
10433** 0 return indicates an error of some kind.
10434*/
10435static char *find_home_dir(int clearFlag){
10436  static char *home_dir = NULL;
10437  if( clearFlag ){
10438    free(home_dir);
10439    home_dir = 0;
10440    return 0;
10441  }
10442  if( home_dir ) return home_dir;
10443
10444#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10445     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10446  {
10447    struct passwd *pwent;
10448    uid_t uid = getuid();
10449    if( (pwent=getpwuid(uid)) != NULL) {
10450      home_dir = pwent->pw_dir;
10451    }
10452  }
10453#endif
10454
10455#if defined(_WIN32_WCE)
10456  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10457   */
10458  home_dir = "/";
10459#else
10460
10461#if defined(_WIN32) || defined(WIN32)
10462  if (!home_dir) {
10463    home_dir = getenv("USERPROFILE");
10464  }
10465#endif
10466
10467  if (!home_dir) {
10468    home_dir = getenv("HOME");
10469  }
10470
10471#if defined(_WIN32) || defined(WIN32)
10472  if (!home_dir) {
10473    char *zDrive, *zPath;
10474    int n;
10475    zDrive = getenv("HOMEDRIVE");
10476    zPath = getenv("HOMEPATH");
10477    if( zDrive && zPath ){
10478      n = strlen30(zDrive) + strlen30(zPath) + 1;
10479      home_dir = malloc( n );
10480      if( home_dir==0 ) return 0;
10481      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10482      return home_dir;
10483    }
10484    home_dir = "c:\\";
10485  }
10486#endif
10487
10488#endif /* !_WIN32_WCE */
10489
10490  if( home_dir ){
10491    int n = strlen30(home_dir) + 1;
10492    char *z = malloc( n );
10493    if( z ) memcpy(z, home_dir, n);
10494    home_dir = z;
10495  }
10496
10497  return home_dir;
10498}
10499
10500/*
10501** Read input from the file given by sqliterc_override.  Or if that
10502** parameter is NULL, take input from ~/.sqliterc
10503**
10504** Returns the number of errors.
10505*/
10506static void process_sqliterc(
10507  ShellState *p,                  /* Configuration data */
10508  const char *sqliterc_override   /* Name of config file. NULL to use default */
10509){
10510  char *home_dir = NULL;
10511  const char *sqliterc = sqliterc_override;
10512  char *zBuf = 0;
10513  FILE *inSaved = p->in;
10514  int savedLineno = p->lineno;
10515
10516  if (sqliterc == NULL) {
10517    home_dir = find_home_dir(0);
10518    if( home_dir==0 ){
10519      raw_printf(stderr, "-- warning: cannot find home directory;"
10520                      " cannot read ~/.sqliterc\n");
10521      return;
10522    }
10523    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10524    sqliterc = zBuf;
10525  }
10526  p->in = fopen(sqliterc,"rb");
10527  if( p->in ){
10528    if( stdin_is_interactive ){
10529      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10530    }
10531    process_input(p);
10532    fclose(p->in);
10533  }
10534  p->in = inSaved;
10535  p->lineno = savedLineno;
10536  sqlite3_free(zBuf);
10537}
10538
10539/*
10540** Show available command line options
10541*/
10542static const char zOptions[] =
10543#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10544  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10545#endif
10546  "   -append              append the database to the end of the file\n"
10547  "   -ascii               set output mode to 'ascii'\n"
10548  "   -bail                stop after hitting an error\n"
10549  "   -batch               force batch I/O\n"
10550  "   -box                 set output mode to 'box'\n"
10551  "   -column              set output mode to 'column'\n"
10552  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10553  "   -csv                 set output mode to 'csv'\n"
10554#if defined(SQLITE_ENABLE_DESERIALIZE)
10555  "   -deserialize         open the database using sqlite3_deserialize()\n"
10556#endif
10557  "   -echo                print commands before execution\n"
10558  "   -init FILENAME       read/process named file\n"
10559  "   -[no]header          turn headers on or off\n"
10560#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10561  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10562#endif
10563  "   -help                show this message\n"
10564  "   -html                set output mode to HTML\n"
10565  "   -interactive         force interactive I/O\n"
10566  "   -json                set output mode to 'json'\n"
10567  "   -line                set output mode to 'line'\n"
10568  "   -list                set output mode to 'list'\n"
10569  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10570  "   -markdown            set output mode to 'markdown'\n"
10571#if defined(SQLITE_ENABLE_DESERIALIZE)
10572  "   -maxsize N           maximum size for a --deserialize database\n"
10573#endif
10574  "   -memtrace            trace all memory allocations and deallocations\n"
10575  "   -mmap N              default mmap size set to N\n"
10576#ifdef SQLITE_ENABLE_MULTIPLEX
10577  "   -multiplex           enable the multiplexor VFS\n"
10578#endif
10579  "   -newline SEP         set output row separator. Default: '\\n'\n"
10580  "   -nofollow            refuse to open symbolic links to database files\n"
10581  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10582  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10583  "   -quote               set output mode to 'quote'\n"
10584  "   -readonly            open the database read-only\n"
10585  "   -separator SEP       set output column separator. Default: '|'\n"
10586#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10587  "   -sorterref SIZE      sorter references threshold size\n"
10588#endif
10589  "   -stats               print memory stats before each finalize\n"
10590  "   -table               set output mode to 'table'\n"
10591  "   -version             show SQLite version\n"
10592  "   -vfs NAME            use NAME as the default VFS\n"
10593#ifdef SQLITE_ENABLE_VFSTRACE
10594  "   -vfstrace            enable tracing of all VFS calls\n"
10595#endif
10596#ifdef SQLITE_HAVE_ZLIB
10597  "   -zip                 open the file as a ZIP Archive\n"
10598#endif
10599;
10600static void usage(int showDetail){
10601  utf8_printf(stderr,
10602      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10603      "FILENAME is the name of an SQLite database. A new database is created\n"
10604      "if the file does not previously exist.\n", Argv0);
10605  if( showDetail ){
10606    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10607  }else{
10608    raw_printf(stderr, "Use the -help option for additional information\n");
10609  }
10610  exit(1);
10611}
10612
10613/*
10614** Internal check:  Verify that the SQLite is uninitialized.  Print a
10615** error message if it is initialized.
10616*/
10617static void verify_uninitialized(void){
10618  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10619    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10620                        " initialization.\n");
10621  }
10622}
10623
10624/*
10625** Initialize the state information in data
10626*/
10627static void main_init(ShellState *data) {
10628  memset(data, 0, sizeof(*data));
10629  data->normalMode = data->cMode = data->mode = MODE_List;
10630  data->autoExplain = 1;
10631  memcpy(data->colSeparator,SEP_Column, 2);
10632  memcpy(data->rowSeparator,SEP_Row, 2);
10633  data->showHeader = 0;
10634  data->shellFlgs = SHFLG_Lookaside;
10635  verify_uninitialized();
10636  sqlite3_config(SQLITE_CONFIG_URI, 1);
10637  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10638  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10639  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10640  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10641}
10642
10643/*
10644** Output text to the console in a font that attracts extra attention.
10645*/
10646#ifdef _WIN32
10647static void printBold(const char *zText){
10648#if !SQLITE_OS_WINRT
10649  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10650  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10651  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10652  SetConsoleTextAttribute(out,
10653         FOREGROUND_RED|FOREGROUND_INTENSITY
10654  );
10655#endif
10656  printf("%s", zText);
10657#if !SQLITE_OS_WINRT
10658  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10659#endif
10660}
10661#else
10662static void printBold(const char *zText){
10663  printf("\033[1m%s\033[0m", zText);
10664}
10665#endif
10666
10667/*
10668** Get the argument to an --option.  Throw an error and die if no argument
10669** is available.
10670*/
10671static char *cmdline_option_value(int argc, char **argv, int i){
10672  if( i==argc ){
10673    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10674            argv[0], argv[argc-1]);
10675    exit(1);
10676  }
10677  return argv[i];
10678}
10679
10680#ifndef SQLITE_SHELL_IS_UTF8
10681#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10682#    define SQLITE_SHELL_IS_UTF8          (0)
10683#  else
10684#    define SQLITE_SHELL_IS_UTF8          (1)
10685#  endif
10686#endif
10687
10688#if SQLITE_SHELL_IS_UTF8
10689int SQLITE_CDECL main(int argc, char **argv){
10690#else
10691int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10692  char **argv;
10693#endif
10694  char *zErrMsg = 0;
10695  ShellState data;
10696  const char *zInitFile = 0;
10697  int i;
10698  int rc = 0;
10699  int warnInmemoryDb = 0;
10700  int readStdin = 1;
10701  int nCmd = 0;
10702  char **azCmd = 0;
10703  const char *zVfs = 0;           /* Value of -vfs command-line option */
10704#if !SQLITE_SHELL_IS_UTF8
10705  char **argvToFree = 0;
10706  int argcToFree = 0;
10707#endif
10708
10709  setBinaryMode(stdin, 0);
10710  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10711  stdin_is_interactive = isatty(0);
10712  stdout_is_console = isatty(1);
10713
10714#ifdef SQLITE_DEBUG
10715  registerOomSimulator();
10716#endif
10717
10718#if !defined(_WIN32_WCE)
10719  if( getenv("SQLITE_DEBUG_BREAK") ){
10720    if( isatty(0) && isatty(2) ){
10721      fprintf(stderr,
10722          "attach debugger to process %d and press any key to continue.\n",
10723          GETPID());
10724      fgetc(stdin);
10725    }else{
10726#if defined(_WIN32) || defined(WIN32)
10727#if SQLITE_OS_WINRT
10728      __debugbreak();
10729#else
10730      DebugBreak();
10731#endif
10732#elif defined(SIGTRAP)
10733      raise(SIGTRAP);
10734#endif
10735    }
10736  }
10737#endif
10738
10739#if USE_SYSTEM_SQLITE+0!=1
10740  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10741    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10742            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10743    exit(1);
10744  }
10745#endif
10746  main_init(&data);
10747
10748  /* On Windows, we must translate command-line arguments into UTF-8.
10749  ** The SQLite memory allocator subsystem has to be enabled in order to
10750  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10751  ** subsequent sqlite3_config() calls will work.  So copy all results into
10752  ** memory that does not come from the SQLite memory allocator.
10753  */
10754#if !SQLITE_SHELL_IS_UTF8
10755  sqlite3_initialize();
10756  argvToFree = malloc(sizeof(argv[0])*argc*2);
10757  argcToFree = argc;
10758  argv = argvToFree + argc;
10759  if( argv==0 ) shell_out_of_memory();
10760  for(i=0; i<argc; i++){
10761    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10762    int n;
10763    if( z==0 ) shell_out_of_memory();
10764    n = (int)strlen(z);
10765    argv[i] = malloc( n+1 );
10766    if( argv[i]==0 ) shell_out_of_memory();
10767    memcpy(argv[i], z, n+1);
10768    argvToFree[i] = argv[i];
10769    sqlite3_free(z);
10770  }
10771  sqlite3_shutdown();
10772#endif
10773
10774  assert( argc>=1 && argv && argv[0] );
10775  Argv0 = argv[0];
10776
10777  /* Make sure we have a valid signal handler early, before anything
10778  ** else is done.
10779  */
10780#ifdef SIGINT
10781  signal(SIGINT, interrupt_handler);
10782#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10783  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10784#endif
10785
10786#ifdef SQLITE_SHELL_DBNAME_PROC
10787  {
10788    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10789    ** of a C-function that will provide the name of the database file.  Use
10790    ** this compile-time option to embed this shell program in larger
10791    ** applications. */
10792    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10793    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10794    warnInmemoryDb = 0;
10795  }
10796#endif
10797
10798  /* Do an initial pass through the command-line argument to locate
10799  ** the name of the database file, the name of the initialization file,
10800  ** the size of the alternative malloc heap,
10801  ** and the first command to execute.
10802  */
10803  verify_uninitialized();
10804  for(i=1; i<argc; i++){
10805    char *z;
10806    z = argv[i];
10807    if( z[0]!='-' ){
10808      if( data.zDbFilename==0 ){
10809        data.zDbFilename = z;
10810      }else{
10811        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10812        ** mean that nothing is read from stdin */
10813        readStdin = 0;
10814        nCmd++;
10815        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10816        if( azCmd==0 ) shell_out_of_memory();
10817        azCmd[nCmd-1] = z;
10818      }
10819    }
10820    if( z[1]=='-' ) z++;
10821    if( strcmp(z,"-separator")==0
10822     || strcmp(z,"-nullvalue")==0
10823     || strcmp(z,"-newline")==0
10824     || strcmp(z,"-cmd")==0
10825    ){
10826      (void)cmdline_option_value(argc, argv, ++i);
10827    }else if( strcmp(z,"-init")==0 ){
10828      zInitFile = cmdline_option_value(argc, argv, ++i);
10829    }else if( strcmp(z,"-batch")==0 ){
10830      /* Need to check for batch mode here to so we can avoid printing
10831      ** informational messages (like from process_sqliterc) before
10832      ** we do the actual processing of arguments later in a second pass.
10833      */
10834      stdin_is_interactive = 0;
10835    }else if( strcmp(z,"-heap")==0 ){
10836#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10837      const char *zSize;
10838      sqlite3_int64 szHeap;
10839
10840      zSize = cmdline_option_value(argc, argv, ++i);
10841      szHeap = integerValue(zSize);
10842      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10843      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10844#else
10845      (void)cmdline_option_value(argc, argv, ++i);
10846#endif
10847    }else if( strcmp(z,"-pagecache")==0 ){
10848      int n, sz;
10849      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10850      if( sz>70000 ) sz = 70000;
10851      if( sz<0 ) sz = 0;
10852      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10853      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10854                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10855      data.shellFlgs |= SHFLG_Pagecache;
10856    }else if( strcmp(z,"-lookaside")==0 ){
10857      int n, sz;
10858      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10859      if( sz<0 ) sz = 0;
10860      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10861      if( n<0 ) n = 0;
10862      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10863      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10864#ifdef SQLITE_ENABLE_VFSTRACE
10865    }else if( strcmp(z,"-vfstrace")==0 ){
10866      extern int vfstrace_register(
10867         const char *zTraceName,
10868         const char *zOldVfsName,
10869         int (*xOut)(const char*,void*),
10870         void *pOutArg,
10871         int makeDefault
10872      );
10873      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10874#endif
10875#ifdef SQLITE_ENABLE_MULTIPLEX
10876    }else if( strcmp(z,"-multiplex")==0 ){
10877      extern int sqlite3_multiple_initialize(const char*,int);
10878      sqlite3_multiplex_initialize(0, 1);
10879#endif
10880    }else if( strcmp(z,"-mmap")==0 ){
10881      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10882      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
10883#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10884    }else if( strcmp(z,"-sorterref")==0 ){
10885      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10886      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
10887#endif
10888    }else if( strcmp(z,"-vfs")==0 ){
10889      zVfs = cmdline_option_value(argc, argv, ++i);
10890#ifdef SQLITE_HAVE_ZLIB
10891    }else if( strcmp(z,"-zip")==0 ){
10892      data.openMode = SHELL_OPEN_ZIPFILE;
10893#endif
10894    }else if( strcmp(z,"-append")==0 ){
10895      data.openMode = SHELL_OPEN_APPENDVFS;
10896#ifdef SQLITE_ENABLE_DESERIALIZE
10897    }else if( strcmp(z,"-deserialize")==0 ){
10898      data.openMode = SHELL_OPEN_DESERIALIZE;
10899    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10900      data.szMax = integerValue(argv[++i]);
10901#endif
10902    }else if( strcmp(z,"-readonly")==0 ){
10903      data.openMode = SHELL_OPEN_READONLY;
10904    }else if( strcmp(z,"-nofollow")==0 ){
10905      data.openFlags = SQLITE_OPEN_NOFOLLOW;
10906#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10907    }else if( strncmp(z, "-A",2)==0 ){
10908      /* All remaining command-line arguments are passed to the ".archive"
10909      ** command, so ignore them */
10910      break;
10911#endif
10912    }else if( strcmp(z, "-memtrace")==0 ){
10913      sqlite3MemTraceActivate(stderr);
10914    }
10915  }
10916  verify_uninitialized();
10917
10918
10919#ifdef SQLITE_SHELL_INIT_PROC
10920  {
10921    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
10922    ** of a C-function that will perform initialization actions on SQLite that
10923    ** occur just before or after sqlite3_initialize(). Use this compile-time
10924    ** option to embed this shell program in larger applications. */
10925    extern void SQLITE_SHELL_INIT_PROC(void);
10926    SQLITE_SHELL_INIT_PROC();
10927  }
10928#else
10929  /* All the sqlite3_config() calls have now been made. So it is safe
10930  ** to call sqlite3_initialize() and process any command line -vfs option. */
10931  sqlite3_initialize();
10932#endif
10933
10934  if( zVfs ){
10935    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
10936    if( pVfs ){
10937      sqlite3_vfs_register(pVfs, 1);
10938    }else{
10939      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
10940      exit(1);
10941    }
10942  }
10943
10944  if( data.zDbFilename==0 ){
10945#ifndef SQLITE_OMIT_MEMORYDB
10946    data.zDbFilename = ":memory:";
10947    warnInmemoryDb = argc==1;
10948#else
10949    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
10950    return 1;
10951#endif
10952  }
10953  data.out = stdout;
10954  sqlite3_appendvfs_init(0,0,0);
10955
10956  /* Go ahead and open the database file if it already exists.  If the
10957  ** file does not exist, delay opening it.  This prevents empty database
10958  ** files from being created if a user mistypes the database name argument
10959  ** to the sqlite command-line tool.
10960  */
10961  if( access(data.zDbFilename, 0)==0 ){
10962    open_db(&data, 0);
10963  }
10964
10965  /* Process the initialization file if there is one.  If no -init option
10966  ** is given on the command line, look for a file named ~/.sqliterc and
10967  ** try to process it.
10968  */
10969  process_sqliterc(&data,zInitFile);
10970
10971  /* Make a second pass through the command-line argument and set
10972  ** options.  This second pass is delayed until after the initialization
10973  ** file is processed so that the command-line arguments will override
10974  ** settings in the initialization file.
10975  */
10976  for(i=1; i<argc; i++){
10977    char *z = argv[i];
10978    if( z[0]!='-' ) continue;
10979    if( z[1]=='-' ){ z++; }
10980    if( strcmp(z,"-init")==0 ){
10981      i++;
10982    }else if( strcmp(z,"-html")==0 ){
10983      data.mode = MODE_Html;
10984    }else if( strcmp(z,"-list")==0 ){
10985      data.mode = MODE_List;
10986    }else if( strcmp(z,"-quote")==0 ){
10987      data.mode = MODE_Quote;
10988    }else if( strcmp(z,"-line")==0 ){
10989      data.mode = MODE_Line;
10990    }else if( strcmp(z,"-column")==0 ){
10991      data.mode = MODE_Column;
10992    }else if( strcmp(z,"-json")==0 ){
10993      data.mode = MODE_Json;
10994    }else if( strcmp(z,"-markdown")==0 ){
10995      data.mode = MODE_Markdown;
10996    }else if( strcmp(z,"-table")==0 ){
10997      data.mode = MODE_Table;
10998    }else if( strcmp(z,"-box")==0 ){
10999      data.mode = MODE_Box;
11000    }else if( strcmp(z,"-csv")==0 ){
11001      data.mode = MODE_Csv;
11002      memcpy(data.colSeparator,",",2);
11003#ifdef SQLITE_HAVE_ZLIB
11004    }else if( strcmp(z,"-zip")==0 ){
11005      data.openMode = SHELL_OPEN_ZIPFILE;
11006#endif
11007    }else if( strcmp(z,"-append")==0 ){
11008      data.openMode = SHELL_OPEN_APPENDVFS;
11009#ifdef SQLITE_ENABLE_DESERIALIZE
11010    }else if( strcmp(z,"-deserialize")==0 ){
11011      data.openMode = SHELL_OPEN_DESERIALIZE;
11012    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11013      data.szMax = integerValue(argv[++i]);
11014#endif
11015    }else if( strcmp(z,"-readonly")==0 ){
11016      data.openMode = SHELL_OPEN_READONLY;
11017    }else if( strcmp(z,"-nofollow")==0 ){
11018      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11019    }else if( strcmp(z,"-ascii")==0 ){
11020      data.mode = MODE_Ascii;
11021      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11022                       SEP_Unit);
11023      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11024                       SEP_Record);
11025    }else if( strcmp(z,"-separator")==0 ){
11026      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11027                       "%s",cmdline_option_value(argc,argv,++i));
11028    }else if( strcmp(z,"-newline")==0 ){
11029      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11030                       "%s",cmdline_option_value(argc,argv,++i));
11031    }else if( strcmp(z,"-nullvalue")==0 ){
11032      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11033                       "%s",cmdline_option_value(argc,argv,++i));
11034    }else if( strcmp(z,"-header")==0 ){
11035      data.showHeader = 1;
11036    }else if( strcmp(z,"-noheader")==0 ){
11037      data.showHeader = 0;
11038    }else if( strcmp(z,"-echo")==0 ){
11039      ShellSetFlag(&data, SHFLG_Echo);
11040    }else if( strcmp(z,"-eqp")==0 ){
11041      data.autoEQP = AUTOEQP_on;
11042    }else if( strcmp(z,"-eqpfull")==0 ){
11043      data.autoEQP = AUTOEQP_full;
11044    }else if( strcmp(z,"-stats")==0 ){
11045      data.statsOn = 1;
11046    }else if( strcmp(z,"-scanstats")==0 ){
11047      data.scanstatsOn = 1;
11048    }else if( strcmp(z,"-backslash")==0 ){
11049      /* Undocumented command-line option: -backslash
11050      ** Causes C-style backslash escapes to be evaluated in SQL statements
11051      ** prior to sending the SQL into SQLite.  Useful for injecting
11052      ** crazy bytes in the middle of SQL statements for testing and debugging.
11053      */
11054      ShellSetFlag(&data, SHFLG_Backslash);
11055    }else if( strcmp(z,"-bail")==0 ){
11056      bail_on_error = 1;
11057    }else if( strcmp(z,"-version")==0 ){
11058      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11059      return 0;
11060    }else if( strcmp(z,"-interactive")==0 ){
11061      stdin_is_interactive = 1;
11062    }else if( strcmp(z,"-batch")==0 ){
11063      stdin_is_interactive = 0;
11064    }else if( strcmp(z,"-heap")==0 ){
11065      i++;
11066    }else if( strcmp(z,"-pagecache")==0 ){
11067      i+=2;
11068    }else if( strcmp(z,"-lookaside")==0 ){
11069      i+=2;
11070    }else if( strcmp(z,"-mmap")==0 ){
11071      i++;
11072    }else if( strcmp(z,"-memtrace")==0 ){
11073      i++;
11074#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11075    }else if( strcmp(z,"-sorterref")==0 ){
11076      i++;
11077#endif
11078    }else if( strcmp(z,"-vfs")==0 ){
11079      i++;
11080#ifdef SQLITE_ENABLE_VFSTRACE
11081    }else if( strcmp(z,"-vfstrace")==0 ){
11082      i++;
11083#endif
11084#ifdef SQLITE_ENABLE_MULTIPLEX
11085    }else if( strcmp(z,"-multiplex")==0 ){
11086      i++;
11087#endif
11088    }else if( strcmp(z,"-help")==0 ){
11089      usage(1);
11090    }else if( strcmp(z,"-cmd")==0 ){
11091      /* Run commands that follow -cmd first and separately from commands
11092      ** that simply appear on the command-line.  This seems goofy.  It would
11093      ** be better if all commands ran in the order that they appear.  But
11094      ** we retain the goofy behavior for historical compatibility. */
11095      if( i==argc-1 ) break;
11096      z = cmdline_option_value(argc,argv,++i);
11097      if( z[0]=='.' ){
11098        rc = do_meta_command(z, &data);
11099        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11100      }else{
11101        open_db(&data, 0);
11102        rc = shell_exec(&data, z, &zErrMsg);
11103        if( zErrMsg!=0 ){
11104          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11105          if( bail_on_error ) return rc!=0 ? rc : 1;
11106        }else if( rc!=0 ){
11107          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11108          if( bail_on_error ) return rc;
11109        }
11110      }
11111#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11112    }else if( strncmp(z, "-A", 2)==0 ){
11113      if( nCmd>0 ){
11114        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11115                            " with \"%s\"\n", z);
11116        return 1;
11117      }
11118      open_db(&data, OPEN_DB_ZIPFILE);
11119      if( z[2] ){
11120        argv[i] = &z[2];
11121        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11122      }else{
11123        arDotCommand(&data, 1, argv+i, argc-i);
11124      }
11125      readStdin = 0;
11126      break;
11127#endif
11128    }else{
11129      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11130      raw_printf(stderr,"Use -help for a list of options.\n");
11131      return 1;
11132    }
11133    data.cMode = data.mode;
11134  }
11135
11136  if( !readStdin ){
11137    /* Run all arguments that do not begin with '-' as if they were separate
11138    ** command-line inputs, except for the argToSkip argument which contains
11139    ** the database filename.
11140    */
11141    for(i=0; i<nCmd; i++){
11142      if( azCmd[i][0]=='.' ){
11143        rc = do_meta_command(azCmd[i], &data);
11144        if( rc ) return rc==2 ? 0 : rc;
11145      }else{
11146        open_db(&data, 0);
11147        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11148        if( zErrMsg!=0 ){
11149          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11150          return rc!=0 ? rc : 1;
11151        }else if( rc!=0 ){
11152          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11153          return rc;
11154        }
11155      }
11156    }
11157    free(azCmd);
11158  }else{
11159    /* Run commands received from standard input
11160    */
11161    if( stdin_is_interactive ){
11162      char *zHome;
11163      char *zHistory;
11164      int nHistory;
11165      printf(
11166        "SQLite version %s %.19s\n" /*extra-version-info*/
11167        "Enter \".help\" for usage hints.\n",
11168        sqlite3_libversion(), sqlite3_sourceid()
11169      );
11170      if( warnInmemoryDb ){
11171        printf("Connected to a ");
11172        printBold("transient in-memory database");
11173        printf(".\nUse \".open FILENAME\" to reopen on a "
11174               "persistent database.\n");
11175      }
11176      zHistory = getenv("SQLITE_HISTORY");
11177      if( zHistory ){
11178        zHistory = strdup(zHistory);
11179      }else if( (zHome = find_home_dir(0))!=0 ){
11180        nHistory = strlen30(zHome) + 20;
11181        if( (zHistory = malloc(nHistory))!=0 ){
11182          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11183        }
11184      }
11185      if( zHistory ){ shell_read_history(zHistory); }
11186#if HAVE_READLINE || HAVE_EDITLINE
11187      rl_attempted_completion_function = readline_completion;
11188#elif HAVE_LINENOISE
11189      linenoiseSetCompletionCallback(linenoise_completion);
11190#endif
11191      data.in = 0;
11192      rc = process_input(&data);
11193      if( zHistory ){
11194        shell_stifle_history(2000);
11195        shell_write_history(zHistory);
11196        free(zHistory);
11197      }
11198    }else{
11199      data.in = stdin;
11200      rc = process_input(&data);
11201    }
11202  }
11203  set_table_name(&data, 0);
11204  if( data.db ){
11205    session_close_all(&data);
11206    close_db(data.db);
11207  }
11208  sqlite3_free(data.zFreeOnClose);
11209  find_home_dir(1);
11210  output_reset(&data);
11211  data.doXdgOpen = 0;
11212  clearTempFile(&data);
11213#if !SQLITE_SHELL_IS_UTF8
11214  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11215  free(argvToFree);
11216#endif
11217  free(data.colWidth);
11218  /* Clear the global data structure so that valgrind will detect memory
11219  ** leaks */
11220  memset(&data, 0, sizeof(data));
11221  return rc;
11222}
11223