xref: /sqlite-3.40.0/src/shell.c.in (revision c08716a3)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Determine if we are dealing with WinRT, which provides only a subset of
22** the full Win32 API.
23*/
24#if !defined(SQLITE_OS_WINRT)
25# define SQLITE_OS_WINRT 0
26#endif
27
28/*
29** Warning pragmas copied from msvc.h in the core.
30*/
31#if defined(_MSC_VER)
32#pragma warning(disable : 4054)
33#pragma warning(disable : 4055)
34#pragma warning(disable : 4100)
35#pragma warning(disable : 4127)
36#pragma warning(disable : 4130)
37#pragma warning(disable : 4152)
38#pragma warning(disable : 4189)
39#pragma warning(disable : 4206)
40#pragma warning(disable : 4210)
41#pragma warning(disable : 4232)
42#pragma warning(disable : 4244)
43#pragma warning(disable : 4305)
44#pragma warning(disable : 4306)
45#pragma warning(disable : 4702)
46#pragma warning(disable : 4706)
47#endif /* defined(_MSC_VER) */
48
49/*
50** No support for loadable extensions in VxWorks.
51*/
52#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
53# define SQLITE_OMIT_LOAD_EXTENSION 1
54#endif
55
56/*
57** Enable large-file support for fopen() and friends on unix.
58*/
59#ifndef SQLITE_DISABLE_LFS
60# define _LARGE_FILE       1
61# ifndef _FILE_OFFSET_BITS
62#   define _FILE_OFFSET_BITS 64
63# endif
64# define _LARGEFILE_SOURCE 1
65#endif
66
67#include <stdlib.h>
68#include <string.h>
69#include <stdio.h>
70#include <assert.h>
71#include "sqlite3.h"
72typedef sqlite3_int64 i64;
73typedef sqlite3_uint64 u64;
74typedef unsigned char u8;
75#if SQLITE_USER_AUTHENTICATION
76# include "sqlite3userauth.h"
77#endif
78#include <ctype.h>
79#include <stdarg.h>
80
81#if !defined(_WIN32) && !defined(WIN32)
82# include <signal.h>
83# if !defined(__RTP__) && !defined(_WRS_KERNEL)
84#  include <pwd.h>
85# endif
86#endif
87#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
88# include <unistd.h>
89# include <dirent.h>
90# define GETPID getpid
91# if defined(__MINGW32__)
92#  define DIRENT dirent
93#  ifndef S_ISLNK
94#   define S_ISLNK(mode) (0)
95#  endif
96# endif
97#else
98# define GETPID (int)GetCurrentProcessId
99#endif
100#include <sys/types.h>
101#include <sys/stat.h>
102
103#if HAVE_READLINE
104# include <readline/readline.h>
105# include <readline/history.h>
106#endif
107
108#if HAVE_EDITLINE
109# include <editline/readline.h>
110#endif
111
112#if HAVE_EDITLINE || HAVE_READLINE
113
114# define shell_add_history(X) add_history(X)
115# define shell_read_history(X) read_history(X)
116# define shell_write_history(X) write_history(X)
117# define shell_stifle_history(X) stifle_history(X)
118# define shell_readline(X) readline(X)
119
120#elif HAVE_LINENOISE
121
122# include "linenoise.h"
123# define shell_add_history(X) linenoiseHistoryAdd(X)
124# define shell_read_history(X) linenoiseHistoryLoad(X)
125# define shell_write_history(X) linenoiseHistorySave(X)
126# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
127# define shell_readline(X) linenoise(X)
128
129#else
130
131# define shell_read_history(X)
132# define shell_write_history(X)
133# define shell_stifle_history(X)
134
135# define SHELL_USE_LOCAL_GETLINE 1
136#endif
137
138
139#if defined(_WIN32) || defined(WIN32)
140# if SQLITE_OS_WINRT
141#  define SQLITE_OMIT_POPEN 1
142# else
143#  include <io.h>
144#  include <fcntl.h>
145#  define isatty(h) _isatty(h)
146#  ifndef access
147#   define access(f,m) _access((f),(m))
148#  endif
149#  ifndef unlink
150#   define unlink _unlink
151#  endif
152#  ifndef strdup
153#   define strdup _strdup
154#  endif
155#  undef popen
156#  define popen _popen
157#  undef pclose
158#  define pclose _pclose
159# endif
160#else
161 /* Make sure isatty() has a prototype. */
162 extern int isatty(int);
163
164# if !defined(__RTP__) && !defined(_WRS_KERNEL)
165  /* popen and pclose are not C89 functions and so are
166  ** sometimes omitted from the <stdio.h> header */
167   extern FILE *popen(const char*,const char*);
168   extern int pclose(FILE*);
169# else
170#  define SQLITE_OMIT_POPEN 1
171# endif
172#endif
173
174#if defined(_WIN32_WCE)
175/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176 * thus we always assume that we have a console. That can be
177 * overridden with the -batch command line option.
178 */
179#define isatty(x) 1
180#endif
181
182/* ctype macros that work with signed characters */
183#define IsSpace(X)  isspace((unsigned char)X)
184#define IsDigit(X)  isdigit((unsigned char)X)
185#define ToLower(X)  (char)tolower((unsigned char)X)
186
187#if defined(_WIN32) || defined(WIN32)
188#if SQLITE_OS_WINRT
189#include <intrin.h>
190#endif
191#include <windows.h>
192
193/* string conversion routines only needed on Win32 */
194extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
195extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
196extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
197extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
198#endif
199
200/* On Windows, we normally run with output mode of TEXT so that \n characters
201** are automatically translated into \r\n.  However, this behavior needs
202** to be disabled in some cases (ex: when generating CSV output and when
203** rendering quoted strings that contain \n characters).  The following
204** routines take care of that.
205*/
206#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
207static void setBinaryMode(FILE *file, int isOutput){
208  if( isOutput ) fflush(file);
209  _setmode(_fileno(file), _O_BINARY);
210}
211static void setTextMode(FILE *file, int isOutput){
212  if( isOutput ) fflush(file);
213  _setmode(_fileno(file), _O_TEXT);
214}
215#else
216# define setBinaryMode(X,Y)
217# define setTextMode(X,Y)
218#endif
219
220
221/* True if the timer is enabled */
222static int enableTimer = 0;
223
224/* Return the current wall-clock time */
225static sqlite3_int64 timeOfDay(void){
226  static sqlite3_vfs *clockVfs = 0;
227  sqlite3_int64 t;
228  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
229  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
230    clockVfs->xCurrentTimeInt64(clockVfs, &t);
231  }else{
232    double r;
233    clockVfs->xCurrentTime(clockVfs, &r);
234    t = (sqlite3_int64)(r*86400000.0);
235  }
236  return t;
237}
238
239#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
240#include <sys/time.h>
241#include <sys/resource.h>
242
243/* VxWorks does not support getrusage() as far as we can determine */
244#if defined(_WRS_KERNEL) || defined(__RTP__)
245struct rusage {
246  struct timeval ru_utime; /* user CPU time used */
247  struct timeval ru_stime; /* system CPU time used */
248};
249#define getrusage(A,B) memset(B,0,sizeof(*B))
250#endif
251
252/* Saved resource information for the beginning of an operation */
253static struct rusage sBegin;  /* CPU time at start */
254static sqlite3_int64 iBegin;  /* Wall-clock time at start */
255
256/*
257** Begin timing an operation
258*/
259static void beginTimer(void){
260  if( enableTimer ){
261    getrusage(RUSAGE_SELF, &sBegin);
262    iBegin = timeOfDay();
263  }
264}
265
266/* Return the difference of two time_structs in seconds */
267static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
268  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
269         (double)(pEnd->tv_sec - pStart->tv_sec);
270}
271
272/*
273** Print the timing results.
274*/
275static void endTimer(void){
276  if( enableTimer ){
277    sqlite3_int64 iEnd = timeOfDay();
278    struct rusage sEnd;
279    getrusage(RUSAGE_SELF, &sEnd);
280    printf("Run Time: real %.3f user %f sys %f\n",
281       (iEnd - iBegin)*0.001,
282       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
283       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
284  }
285}
286
287#define BEGIN_TIMER beginTimer()
288#define END_TIMER endTimer()
289#define HAS_TIMER 1
290
291#elif (defined(_WIN32) || defined(WIN32))
292
293/* Saved resource information for the beginning of an operation */
294static HANDLE hProcess;
295static FILETIME ftKernelBegin;
296static FILETIME ftUserBegin;
297static sqlite3_int64 ftWallBegin;
298typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
299                                    LPFILETIME, LPFILETIME);
300static GETPROCTIMES getProcessTimesAddr = NULL;
301
302/*
303** Check to see if we have timer support.  Return 1 if necessary
304** support found (or found previously).
305*/
306static int hasTimer(void){
307  if( getProcessTimesAddr ){
308    return 1;
309  } else {
310#if !SQLITE_OS_WINRT
311    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
312    ** versions. See if the version we are running on has it, and if it
313    ** does, save off a pointer to it and the current process handle.
314    */
315    hProcess = GetCurrentProcess();
316    if( hProcess ){
317      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
318      if( NULL != hinstLib ){
319        getProcessTimesAddr =
320            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
321        if( NULL != getProcessTimesAddr ){
322          return 1;
323        }
324        FreeLibrary(hinstLib);
325      }
326    }
327#endif
328  }
329  return 0;
330}
331
332/*
333** Begin timing an operation
334*/
335static void beginTimer(void){
336  if( enableTimer && getProcessTimesAddr ){
337    FILETIME ftCreation, ftExit;
338    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
339                        &ftKernelBegin,&ftUserBegin);
340    ftWallBegin = timeOfDay();
341  }
342}
343
344/* Return the difference of two FILETIME structs in seconds */
345static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
346  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
347  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
348  return (double) ((i64End - i64Start) / 10000000.0);
349}
350
351/*
352** Print the timing results.
353*/
354static void endTimer(void){
355  if( enableTimer && getProcessTimesAddr){
356    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
357    sqlite3_int64 ftWallEnd = timeOfDay();
358    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
359    printf("Run Time: real %.3f user %f sys %f\n",
360       (ftWallEnd - ftWallBegin)*0.001,
361       timeDiff(&ftUserBegin, &ftUserEnd),
362       timeDiff(&ftKernelBegin, &ftKernelEnd));
363  }
364}
365
366#define BEGIN_TIMER beginTimer()
367#define END_TIMER endTimer()
368#define HAS_TIMER hasTimer()
369
370#else
371#define BEGIN_TIMER
372#define END_TIMER
373#define HAS_TIMER 0
374#endif
375
376/*
377** Used to prevent warnings about unused parameters
378*/
379#define UNUSED_PARAMETER(x) (void)(x)
380
381/*
382** Number of elements in an array
383*/
384#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
385
386/*
387** If the following flag is set, then command execution stops
388** at an error if we are not interactive.
389*/
390static int bail_on_error = 0;
391
392/*
393** Threat stdin as an interactive input if the following variable
394** is true.  Otherwise, assume stdin is connected to a file or pipe.
395*/
396static int stdin_is_interactive = 1;
397
398/*
399** On Windows systems we have to know if standard output is a console
400** in order to translate UTF-8 into MBCS.  The following variable is
401** true if translation is required.
402*/
403static int stdout_is_console = 1;
404
405/*
406** The following is the open SQLite database.  We make a pointer
407** to this database a static variable so that it can be accessed
408** by the SIGINT handler to interrupt database processing.
409*/
410static sqlite3 *globalDb = 0;
411
412/*
413** True if an interrupt (Control-C) has been received.
414*/
415static volatile int seenInterrupt = 0;
416
417#ifdef SQLITE_DEBUG
418/*
419** Out-of-memory simulator variables
420*/
421static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
422static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
423static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
424#endif /* SQLITE_DEBUG */
425
426/*
427** This is the name of our program. It is set in main(), used
428** in a number of other places, mostly for error messages.
429*/
430static char *Argv0;
431
432/*
433** Prompt strings. Initialized in main. Settable with
434**   .prompt main continue
435*/
436static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
437static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
438
439/*
440** Render output like fprintf().  Except, if the output is going to the
441** console and if this is running on a Windows machine, translate the
442** output from UTF-8 into MBCS.
443*/
444#if defined(_WIN32) || defined(WIN32)
445void utf8_printf(FILE *out, const char *zFormat, ...){
446  va_list ap;
447  va_start(ap, zFormat);
448  if( stdout_is_console && (out==stdout || out==stderr) ){
449    char *z1 = sqlite3_vmprintf(zFormat, ap);
450    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
451    sqlite3_free(z1);
452    fputs(z2, out);
453    sqlite3_free(z2);
454  }else{
455    vfprintf(out, zFormat, ap);
456  }
457  va_end(ap);
458}
459#elif !defined(utf8_printf)
460# define utf8_printf fprintf
461#endif
462
463/*
464** Render output like fprintf().  This should not be used on anything that
465** includes string formatting (e.g. "%s").
466*/
467#if !defined(raw_printf)
468# define raw_printf fprintf
469#endif
470
471/* Indicate out-of-memory and exit. */
472static void shell_out_of_memory(void){
473  raw_printf(stderr,"Error: out of memory\n");
474  exit(1);
475}
476
477#ifdef SQLITE_DEBUG
478/* This routine is called when a simulated OOM occurs.  It is broken
479** out as a separate routine to make it easy to set a breakpoint on
480** the OOM
481*/
482void shellOomFault(void){
483  if( oomRepeat>0 ){
484    oomRepeat--;
485  }else{
486    oomCounter--;
487  }
488}
489#endif /* SQLITE_DEBUG */
490
491#ifdef SQLITE_DEBUG
492/* This routine is a replacement malloc() that is used to simulate
493** Out-Of-Memory (OOM) errors for testing purposes.
494*/
495static void *oomMalloc(int nByte){
496  if( oomCounter ){
497    if( oomCounter==1 ){
498      shellOomFault();
499      return 0;
500    }else{
501      oomCounter--;
502    }
503  }
504  return defaultMalloc(nByte);
505}
506#endif /* SQLITE_DEBUG */
507
508#ifdef SQLITE_DEBUG
509/* Register the OOM simulator.  This must occur before any memory
510** allocations */
511static void registerOomSimulator(void){
512  sqlite3_mem_methods mem;
513  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
514  defaultMalloc = mem.xMalloc;
515  mem.xMalloc = oomMalloc;
516  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
517}
518#endif
519
520/*
521** Write I/O traces to the following stream.
522*/
523#ifdef SQLITE_ENABLE_IOTRACE
524static FILE *iotrace = 0;
525#endif
526
527/*
528** This routine works like printf in that its first argument is a
529** format string and subsequent arguments are values to be substituted
530** in place of % fields.  The result of formatting this string
531** is written to iotrace.
532*/
533#ifdef SQLITE_ENABLE_IOTRACE
534static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
535  va_list ap;
536  char *z;
537  if( iotrace==0 ) return;
538  va_start(ap, zFormat);
539  z = sqlite3_vmprintf(zFormat, ap);
540  va_end(ap);
541  utf8_printf(iotrace, "%s", z);
542  sqlite3_free(z);
543}
544#endif
545
546/*
547** Output string zUtf to stream pOut as w characters.  If w is negative,
548** then right-justify the text.  W is the width in UTF-8 characters, not
549** in bytes.  This is different from the %*.*s specification in printf
550** since with %*.*s the width is measured in bytes, not characters.
551*/
552static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
553  int i;
554  int n;
555  int aw = w<0 ? -w : w;
556  char zBuf[1000];
557  if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
558  for(i=n=0; zUtf[i]; i++){
559    if( (zUtf[i]&0xc0)!=0x80 ){
560      n++;
561      if( n==aw ){
562        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
563        break;
564      }
565    }
566  }
567  if( n>=aw ){
568    utf8_printf(pOut, "%.*s", i, zUtf);
569  }else if( w<0 ){
570    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
571  }else{
572    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
573  }
574}
575
576
577/*
578** Determines if a string is a number of not.
579*/
580static int isNumber(const char *z, int *realnum){
581  if( *z=='-' || *z=='+' ) z++;
582  if( !IsDigit(*z) ){
583    return 0;
584  }
585  z++;
586  if( realnum ) *realnum = 0;
587  while( IsDigit(*z) ){ z++; }
588  if( *z=='.' ){
589    z++;
590    if( !IsDigit(*z) ) return 0;
591    while( IsDigit(*z) ){ z++; }
592    if( realnum ) *realnum = 1;
593  }
594  if( *z=='e' || *z=='E' ){
595    z++;
596    if( *z=='+' || *z=='-' ) z++;
597    if( !IsDigit(*z) ) return 0;
598    while( IsDigit(*z) ){ z++; }
599    if( realnum ) *realnum = 1;
600  }
601  return *z==0;
602}
603
604/*
605** Compute a string length that is limited to what can be stored in
606** lower 30 bits of a 32-bit signed integer.
607*/
608static int strlen30(const char *z){
609  const char *z2 = z;
610  while( *z2 ){ z2++; }
611  return 0x3fffffff & (int)(z2 - z);
612}
613
614/*
615** Return the length of a string in characters.  Multibyte UTF8 characters
616** count as a single character.
617*/
618static int strlenChar(const char *z){
619  int n = 0;
620  while( *z ){
621    if( (0xc0&*(z++))!=0x80 ) n++;
622  }
623  return n;
624}
625
626/*
627** Return true if zFile does not exist or if it is not an ordinary file.
628*/
629#ifdef _WIN32
630# define notNormalFile(X) 0
631#else
632static int notNormalFile(const char *zFile){
633  struct stat x;
634  int rc;
635  memset(&x, 0, sizeof(x));
636  rc = stat(zFile, &x);
637  return rc || !S_ISREG(x.st_mode);
638}
639#endif
640
641/*
642** This routine reads a line of text from FILE in, stores
643** the text in memory obtained from malloc() and returns a pointer
644** to the text.  NULL is returned at end of file, or if malloc()
645** fails.
646**
647** If zLine is not NULL then it is a malloced buffer returned from
648** a previous call to this routine that may be reused.
649*/
650static char *local_getline(char *zLine, FILE *in){
651  int nLine = zLine==0 ? 0 : 100;
652  int n = 0;
653
654  while( 1 ){
655    if( n+100>nLine ){
656      nLine = nLine*2 + 100;
657      zLine = realloc(zLine, nLine);
658      if( zLine==0 ) shell_out_of_memory();
659    }
660    if( fgets(&zLine[n], nLine - n, in)==0 ){
661      if( n==0 ){
662        free(zLine);
663        return 0;
664      }
665      zLine[n] = 0;
666      break;
667    }
668    while( zLine[n] ) n++;
669    if( n>0 && zLine[n-1]=='\n' ){
670      n--;
671      if( n>0 && zLine[n-1]=='\r' ) n--;
672      zLine[n] = 0;
673      break;
674    }
675  }
676#if defined(_WIN32) || defined(WIN32)
677  /* For interactive input on Windows systems, translate the
678  ** multi-byte characterset characters into UTF-8. */
679  if( stdin_is_interactive && in==stdin ){
680    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
681    if( zTrans ){
682      int nTrans = strlen30(zTrans)+1;
683      if( nTrans>nLine ){
684        zLine = realloc(zLine, nTrans);
685        if( zLine==0 ) shell_out_of_memory();
686      }
687      memcpy(zLine, zTrans, nTrans);
688      sqlite3_free(zTrans);
689    }
690  }
691#endif /* defined(_WIN32) || defined(WIN32) */
692  return zLine;
693}
694
695/*
696** Retrieve a single line of input text.
697**
698** If in==0 then read from standard input and prompt before each line.
699** If isContinuation is true, then a continuation prompt is appropriate.
700** If isContinuation is zero, then the main prompt should be used.
701**
702** If zPrior is not NULL then it is a buffer from a prior call to this
703** routine that can be reused.
704**
705** The result is stored in space obtained from malloc() and must either
706** be freed by the caller or else passed back into this routine via the
707** zPrior argument for reuse.
708*/
709static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
710  char *zPrompt;
711  char *zResult;
712  if( in!=0 ){
713    zResult = local_getline(zPrior, in);
714  }else{
715    zPrompt = isContinuation ? continuePrompt : mainPrompt;
716#if SHELL_USE_LOCAL_GETLINE
717    printf("%s", zPrompt);
718    fflush(stdout);
719    zResult = local_getline(zPrior, stdin);
720#else
721    free(zPrior);
722    zResult = shell_readline(zPrompt);
723    if( zResult && *zResult ) shell_add_history(zResult);
724#endif
725  }
726  return zResult;
727}
728
729
730/*
731** Return the value of a hexadecimal digit.  Return -1 if the input
732** is not a hex digit.
733*/
734static int hexDigitValue(char c){
735  if( c>='0' && c<='9' ) return c - '0';
736  if( c>='a' && c<='f' ) return c - 'a' + 10;
737  if( c>='A' && c<='F' ) return c - 'A' + 10;
738  return -1;
739}
740
741/*
742** Interpret zArg as an integer value, possibly with suffixes.
743*/
744static sqlite3_int64 integerValue(const char *zArg){
745  sqlite3_int64 v = 0;
746  static const struct { char *zSuffix; int iMult; } aMult[] = {
747    { "KiB", 1024 },
748    { "MiB", 1024*1024 },
749    { "GiB", 1024*1024*1024 },
750    { "KB",  1000 },
751    { "MB",  1000000 },
752    { "GB",  1000000000 },
753    { "K",   1000 },
754    { "M",   1000000 },
755    { "G",   1000000000 },
756  };
757  int i;
758  int isNeg = 0;
759  if( zArg[0]=='-' ){
760    isNeg = 1;
761    zArg++;
762  }else if( zArg[0]=='+' ){
763    zArg++;
764  }
765  if( zArg[0]=='0' && zArg[1]=='x' ){
766    int x;
767    zArg += 2;
768    while( (x = hexDigitValue(zArg[0]))>=0 ){
769      v = (v<<4) + x;
770      zArg++;
771    }
772  }else{
773    while( IsDigit(zArg[0]) ){
774      v = v*10 + zArg[0] - '0';
775      zArg++;
776    }
777  }
778  for(i=0; i<ArraySize(aMult); i++){
779    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
780      v *= aMult[i].iMult;
781      break;
782    }
783  }
784  return isNeg? -v : v;
785}
786
787/*
788** A variable length string to which one can append text.
789*/
790typedef struct ShellText ShellText;
791struct ShellText {
792  char *z;
793  int n;
794  int nAlloc;
795};
796
797/*
798** Initialize and destroy a ShellText object
799*/
800static void initText(ShellText *p){
801  memset(p, 0, sizeof(*p));
802}
803static void freeText(ShellText *p){
804  free(p->z);
805  initText(p);
806}
807
808/* zIn is either a pointer to a NULL-terminated string in memory obtained
809** from malloc(), or a NULL pointer. The string pointed to by zAppend is
810** added to zIn, and the result returned in memory obtained from malloc().
811** zIn, if it was not NULL, is freed.
812**
813** If the third argument, quote, is not '\0', then it is used as a
814** quote character for zAppend.
815*/
816static void appendText(ShellText *p, char const *zAppend, char quote){
817  int len;
818  int i;
819  int nAppend = strlen30(zAppend);
820
821  len = nAppend+p->n+1;
822  if( quote ){
823    len += 2;
824    for(i=0; i<nAppend; i++){
825      if( zAppend[i]==quote ) len++;
826    }
827  }
828
829  if( p->n+len>=p->nAlloc ){
830    p->nAlloc = p->nAlloc*2 + len + 20;
831    p->z = realloc(p->z, p->nAlloc);
832    if( p->z==0 ) shell_out_of_memory();
833  }
834
835  if( quote ){
836    char *zCsr = p->z+p->n;
837    *zCsr++ = quote;
838    for(i=0; i<nAppend; i++){
839      *zCsr++ = zAppend[i];
840      if( zAppend[i]==quote ) *zCsr++ = quote;
841    }
842    *zCsr++ = quote;
843    p->n = (int)(zCsr - p->z);
844    *zCsr = '\0';
845  }else{
846    memcpy(p->z+p->n, zAppend, nAppend);
847    p->n += nAppend;
848    p->z[p->n] = '\0';
849  }
850}
851
852/*
853** Attempt to determine if identifier zName needs to be quoted, either
854** because it contains non-alphanumeric characters, or because it is an
855** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
856** that quoting is required.
857**
858** Return '"' if quoting is required.  Return 0 if no quoting is required.
859*/
860static char quoteChar(const char *zName){
861  int i;
862  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
863  for(i=0; zName[i]; i++){
864    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
865  }
866  return sqlite3_keyword_check(zName, i) ? '"' : 0;
867}
868
869/*
870** Construct a fake object name and column list to describe the structure
871** of the view, virtual table, or table valued function zSchema.zName.
872*/
873static char *shellFakeSchema(
874  sqlite3 *db,            /* The database connection containing the vtab */
875  const char *zSchema,    /* Schema of the database holding the vtab */
876  const char *zName       /* The name of the virtual table */
877){
878  sqlite3_stmt *pStmt = 0;
879  char *zSql;
880  ShellText s;
881  char cQuote;
882  char *zDiv = "(";
883  int nRow = 0;
884
885  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
886                         zSchema ? zSchema : "main", zName);
887  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
888  sqlite3_free(zSql);
889  initText(&s);
890  if( zSchema ){
891    cQuote = quoteChar(zSchema);
892    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
893    appendText(&s, zSchema, cQuote);
894    appendText(&s, ".", 0);
895  }
896  cQuote = quoteChar(zName);
897  appendText(&s, zName, cQuote);
898  while( sqlite3_step(pStmt)==SQLITE_ROW ){
899    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
900    nRow++;
901    appendText(&s, zDiv, 0);
902    zDiv = ",";
903    cQuote = quoteChar(zCol);
904    appendText(&s, zCol, cQuote);
905  }
906  appendText(&s, ")", 0);
907  sqlite3_finalize(pStmt);
908  if( nRow==0 ){
909    freeText(&s);
910    s.z = 0;
911  }
912  return s.z;
913}
914
915/*
916** SQL function:  shell_module_schema(X)
917**
918** Return a fake schema for the table-valued function or eponymous virtual
919** table X.
920*/
921static void shellModuleSchema(
922  sqlite3_context *pCtx,
923  int nVal,
924  sqlite3_value **apVal
925){
926  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
927  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
928  UNUSED_PARAMETER(nVal);
929  if( zFake ){
930    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
931                        -1, sqlite3_free);
932    free(zFake);
933  }
934}
935
936/*
937** SQL function:  shell_add_schema(S,X)
938**
939** Add the schema name X to the CREATE statement in S and return the result.
940** Examples:
941**
942**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
943**
944** Also works on
945**
946**    CREATE INDEX
947**    CREATE UNIQUE INDEX
948**    CREATE VIEW
949**    CREATE TRIGGER
950**    CREATE VIRTUAL TABLE
951**
952** This UDF is used by the .schema command to insert the schema name of
953** attached databases into the middle of the sqlite_schema.sql field.
954*/
955static void shellAddSchemaName(
956  sqlite3_context *pCtx,
957  int nVal,
958  sqlite3_value **apVal
959){
960  static const char *aPrefix[] = {
961     "TABLE",
962     "INDEX",
963     "UNIQUE INDEX",
964     "VIEW",
965     "TRIGGER",
966     "VIRTUAL TABLE"
967  };
968  int i = 0;
969  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
970  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
971  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
972  sqlite3 *db = sqlite3_context_db_handle(pCtx);
973  UNUSED_PARAMETER(nVal);
974  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
975    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
976      int n = strlen30(aPrefix[i]);
977      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
978        char *z = 0;
979        char *zFake = 0;
980        if( zSchema ){
981          char cQuote = quoteChar(zSchema);
982          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
983            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
984          }else{
985            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
986          }
987        }
988        if( zName
989         && aPrefix[i][0]=='V'
990         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
991        ){
992          if( z==0 ){
993            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
994          }else{
995            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
996          }
997          free(zFake);
998        }
999        if( z ){
1000          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1001          return;
1002        }
1003      }
1004    }
1005  }
1006  sqlite3_result_value(pCtx, apVal[0]);
1007}
1008
1009/*
1010** The source code for several run-time loadable extensions is inserted
1011** below by the ../tool/mkshellc.tcl script.  Before processing that included
1012** code, we need to override some macros to make the included program code
1013** work here in the middle of this regular program.
1014*/
1015#define SQLITE_EXTENSION_INIT1
1016#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1017
1018#if defined(_WIN32) && defined(_MSC_VER)
1019INCLUDE test_windirent.h
1020INCLUDE test_windirent.c
1021#define dirent DIRENT
1022#endif
1023INCLUDE ../ext/misc/shathree.c
1024INCLUDE ../ext/misc/fileio.c
1025INCLUDE ../ext/misc/completion.c
1026INCLUDE ../ext/misc/appendvfs.c
1027INCLUDE ../ext/misc/memtrace.c
1028INCLUDE ../ext/misc/uint.c
1029INCLUDE ../ext/misc/decimal.c
1030INCLUDE ../ext/misc/ieee754.c
1031#ifdef SQLITE_HAVE_ZLIB
1032INCLUDE ../ext/misc/zipfile.c
1033INCLUDE ../ext/misc/sqlar.c
1034#endif
1035INCLUDE ../ext/expert/sqlite3expert.h
1036INCLUDE ../ext/expert/sqlite3expert.c
1037
1038#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1039INCLUDE ../ext/misc/dbdata.c
1040#endif
1041
1042#if defined(SQLITE_ENABLE_SESSION)
1043/*
1044** State information for a single open session
1045*/
1046typedef struct OpenSession OpenSession;
1047struct OpenSession {
1048  char *zName;             /* Symbolic name for this session */
1049  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1050  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1051  sqlite3_session *p;      /* The open session */
1052};
1053#endif
1054
1055typedef struct ExpertInfo ExpertInfo;
1056struct ExpertInfo {
1057  sqlite3expert *pExpert;
1058  int bVerbose;
1059};
1060
1061/* A single line in the EQP output */
1062typedef struct EQPGraphRow EQPGraphRow;
1063struct EQPGraphRow {
1064  int iEqpId;           /* ID for this row */
1065  int iParentId;        /* ID of the parent row */
1066  EQPGraphRow *pNext;   /* Next row in sequence */
1067  char zText[1];        /* Text to display for this row */
1068};
1069
1070/* All EQP output is collected into an instance of the following */
1071typedef struct EQPGraph EQPGraph;
1072struct EQPGraph {
1073  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1074  EQPGraphRow *pLast;   /* Last element of the pRow list */
1075  char zPrefix[100];    /* Graph prefix */
1076};
1077
1078/*
1079** State information about the database connection is contained in an
1080** instance of the following structure.
1081*/
1082typedef struct ShellState ShellState;
1083struct ShellState {
1084  sqlite3 *db;           /* The database */
1085  u8 autoExplain;        /* Automatically turn on .explain mode */
1086  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1087  u8 autoEQPtest;        /* autoEQP is in test mode */
1088  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1089  u8 statsOn;            /* True to display memory stats before each finalize */
1090  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1091  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1092  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1093  u8 nEqpLevel;          /* Depth of the EQP output graph */
1094  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1095  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1096  int outCount;          /* Revert to stdout when reaching zero */
1097  int cnt;               /* Number of records displayed so far */
1098  int lineno;            /* Line number of last line read from in */
1099  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1100  FILE *in;              /* Read commands from this stream */
1101  FILE *out;             /* Write results here */
1102  FILE *traceOut;        /* Output for sqlite3_trace() */
1103  int nErr;              /* Number of errors seen */
1104  int mode;              /* An output mode setting */
1105  int modePrior;         /* Saved mode */
1106  int cMode;             /* temporary output mode for the current query */
1107  int normalMode;        /* Output mode before ".explain on" */
1108  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1109  int showHeader;        /* True to show column names in List or Column mode */
1110  int nCheck;            /* Number of ".check" commands run */
1111  unsigned nProgress;    /* Number of progress callbacks encountered */
1112  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1113  unsigned flgProgress;  /* Flags for the progress callback */
1114  unsigned shellFlgs;    /* Various flags */
1115  unsigned priorShFlgs;  /* Saved copy of flags */
1116  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1117  char *zDestTable;      /* Name of destination table when MODE_Insert */
1118  char *zTempFile;       /* Temporary file that might need deleting */
1119  char zTestcase[30];    /* Name of current test case */
1120  char colSeparator[20]; /* Column separator character for several modes */
1121  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1122  char colSepPrior[20];  /* Saved column separator */
1123  char rowSepPrior[20];  /* Saved row separator */
1124  int *colWidth;         /* Requested width of each column in columnar modes */
1125  int *actualWidth;      /* Actual width of each column */
1126  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1127  char nullValue[20];    /* The text to print when a NULL comes back from
1128                         ** the database */
1129  char outfile[FILENAME_MAX]; /* Filename for *out */
1130  const char *zDbFilename;    /* name of the database file */
1131  char *zFreeOnClose;         /* Filename to free when closing */
1132  const char *zVfs;           /* Name of VFS to use */
1133  sqlite3_stmt *pStmt;   /* Current statement if any. */
1134  FILE *pLog;            /* Write log output here */
1135  int *aiIndent;         /* Array of indents used in MODE_Explain */
1136  int nIndent;           /* Size of array aiIndent[] */
1137  int iIndent;           /* Index of current op in aiIndent[] */
1138  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1139#if defined(SQLITE_ENABLE_SESSION)
1140  int nSession;             /* Number of active sessions */
1141  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1142#endif
1143  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1144};
1145
1146
1147/* Allowed values for ShellState.autoEQP
1148*/
1149#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1150#define AUTOEQP_on       1           /* Automatic EQP is on */
1151#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1152#define AUTOEQP_full     3           /* Show full EXPLAIN */
1153
1154/* Allowed values for ShellState.openMode
1155*/
1156#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1157#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1158#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1159#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1160#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1161#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1162#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1163
1164/* Allowed values for ShellState.eTraceType
1165*/
1166#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1167#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1168#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1169
1170/* Bits in the ShellState.flgProgress variable */
1171#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1172#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1173                                   ** callback limit is reached, and for each
1174                                   ** top-level SQL statement */
1175#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1176
1177/*
1178** These are the allowed shellFlgs values
1179*/
1180#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1181#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1182#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1183#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1184#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1185#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1186#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1187#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1188
1189/*
1190** Macros for testing and setting shellFlgs
1191*/
1192#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1193#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1194#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1195
1196/*
1197** These are the allowed modes.
1198*/
1199#define MODE_Line     0  /* One column per line.  Blank line between records */
1200#define MODE_Column   1  /* One record per line in neat columns */
1201#define MODE_List     2  /* One record per line with a separator */
1202#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1203#define MODE_Html     4  /* Generate an XHTML table */
1204#define MODE_Insert   5  /* Generate SQL "insert" statements */
1205#define MODE_Quote    6  /* Quote values as for SQL */
1206#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1207#define MODE_Csv      8  /* Quote strings, numbers are plain */
1208#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1209#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1210#define MODE_Pretty  11  /* Pretty-print schemas */
1211#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1212#define MODE_Json    13  /* Output JSON */
1213#define MODE_Markdown 14 /* Markdown formatting */
1214#define MODE_Table   15  /* MySQL-style table formatting */
1215#define MODE_Box     16  /* Unicode box-drawing characters */
1216
1217static const char *modeDescr[] = {
1218  "line",
1219  "column",
1220  "list",
1221  "semi",
1222  "html",
1223  "insert",
1224  "quote",
1225  "tcl",
1226  "csv",
1227  "explain",
1228  "ascii",
1229  "prettyprint",
1230  "eqp",
1231  "json",
1232  "markdown",
1233  "table",
1234  "box"
1235};
1236
1237/*
1238** These are the column/row/line separators used by the various
1239** import/export modes.
1240*/
1241#define SEP_Column    "|"
1242#define SEP_Row       "\n"
1243#define SEP_Tab       "\t"
1244#define SEP_Space     " "
1245#define SEP_Comma     ","
1246#define SEP_CrLf      "\r\n"
1247#define SEP_Unit      "\x1F"
1248#define SEP_Record    "\x1E"
1249
1250/*
1251** A callback for the sqlite3_log() interface.
1252*/
1253static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1254  ShellState *p = (ShellState*)pArg;
1255  if( p->pLog==0 ) return;
1256  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1257  fflush(p->pLog);
1258}
1259
1260/*
1261** SQL function:  shell_putsnl(X)
1262**
1263** Write the text X to the screen (or whatever output is being directed)
1264** adding a newline at the end, and then return X.
1265*/
1266static void shellPutsFunc(
1267  sqlite3_context *pCtx,
1268  int nVal,
1269  sqlite3_value **apVal
1270){
1271  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1272  (void)nVal;
1273  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1274  sqlite3_result_value(pCtx, apVal[0]);
1275}
1276
1277/*
1278** SQL function:   edit(VALUE)
1279**                 edit(VALUE,EDITOR)
1280**
1281** These steps:
1282**
1283**     (1) Write VALUE into a temporary file.
1284**     (2) Run program EDITOR on that temporary file.
1285**     (3) Read the temporary file back and return its content as the result.
1286**     (4) Delete the temporary file
1287**
1288** If the EDITOR argument is omitted, use the value in the VISUAL
1289** environment variable.  If still there is no EDITOR, through an error.
1290**
1291** Also throw an error if the EDITOR program returns a non-zero exit code.
1292*/
1293#ifndef SQLITE_NOHAVE_SYSTEM
1294static void editFunc(
1295  sqlite3_context *context,
1296  int argc,
1297  sqlite3_value **argv
1298){
1299  const char *zEditor;
1300  char *zTempFile = 0;
1301  sqlite3 *db;
1302  char *zCmd = 0;
1303  int bBin;
1304  int rc;
1305  int hasCRNL = 0;
1306  FILE *f = 0;
1307  sqlite3_int64 sz;
1308  sqlite3_int64 x;
1309  unsigned char *p = 0;
1310
1311  if( argc==2 ){
1312    zEditor = (const char*)sqlite3_value_text(argv[1]);
1313  }else{
1314    zEditor = getenv("VISUAL");
1315  }
1316  if( zEditor==0 ){
1317    sqlite3_result_error(context, "no editor for edit()", -1);
1318    return;
1319  }
1320  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1321    sqlite3_result_error(context, "NULL input to edit()", -1);
1322    return;
1323  }
1324  db = sqlite3_context_db_handle(context);
1325  zTempFile = 0;
1326  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1327  if( zTempFile==0 ){
1328    sqlite3_uint64 r = 0;
1329    sqlite3_randomness(sizeof(r), &r);
1330    zTempFile = sqlite3_mprintf("temp%llx", r);
1331    if( zTempFile==0 ){
1332      sqlite3_result_error_nomem(context);
1333      return;
1334    }
1335  }
1336  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1337  /* When writing the file to be edited, do \n to \r\n conversions on systems
1338  ** that want \r\n line endings */
1339  f = fopen(zTempFile, bBin ? "wb" : "w");
1340  if( f==0 ){
1341    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1342    goto edit_func_end;
1343  }
1344  sz = sqlite3_value_bytes(argv[0]);
1345  if( bBin ){
1346    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1347  }else{
1348    const char *z = (const char*)sqlite3_value_text(argv[0]);
1349    /* Remember whether or not the value originally contained \r\n */
1350    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1351    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1352  }
1353  fclose(f);
1354  f = 0;
1355  if( x!=sz ){
1356    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1357    goto edit_func_end;
1358  }
1359  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1360  if( zCmd==0 ){
1361    sqlite3_result_error_nomem(context);
1362    goto edit_func_end;
1363  }
1364  rc = system(zCmd);
1365  sqlite3_free(zCmd);
1366  if( rc ){
1367    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1368    goto edit_func_end;
1369  }
1370  f = fopen(zTempFile, "rb");
1371  if( f==0 ){
1372    sqlite3_result_error(context,
1373      "edit() cannot reopen temp file after edit", -1);
1374    goto edit_func_end;
1375  }
1376  fseek(f, 0, SEEK_END);
1377  sz = ftell(f);
1378  rewind(f);
1379  p = sqlite3_malloc64( sz+1 );
1380  if( p==0 ){
1381    sqlite3_result_error_nomem(context);
1382    goto edit_func_end;
1383  }
1384  x = fread(p, 1, (size_t)sz, f);
1385  fclose(f);
1386  f = 0;
1387  if( x!=sz ){
1388    sqlite3_result_error(context, "could not read back the whole file", -1);
1389    goto edit_func_end;
1390  }
1391  if( bBin ){
1392    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1393  }else{
1394    sqlite3_int64 i, j;
1395    if( hasCRNL ){
1396      /* If the original contains \r\n then do no conversions back to \n */
1397      j = sz;
1398    }else{
1399      /* If the file did not originally contain \r\n then convert any new
1400      ** \r\n back into \n */
1401      for(i=j=0; i<sz; i++){
1402        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1403        p[j++] = p[i];
1404      }
1405      sz = j;
1406      p[sz] = 0;
1407    }
1408    sqlite3_result_text64(context, (const char*)p, sz,
1409                          sqlite3_free, SQLITE_UTF8);
1410  }
1411  p = 0;
1412
1413edit_func_end:
1414  if( f ) fclose(f);
1415  unlink(zTempFile);
1416  sqlite3_free(zTempFile);
1417  sqlite3_free(p);
1418}
1419#endif /* SQLITE_NOHAVE_SYSTEM */
1420
1421/*
1422** Save or restore the current output mode
1423*/
1424static void outputModePush(ShellState *p){
1425  p->modePrior = p->mode;
1426  p->priorShFlgs = p->shellFlgs;
1427  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1428  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1429}
1430static void outputModePop(ShellState *p){
1431  p->mode = p->modePrior;
1432  p->shellFlgs = p->priorShFlgs;
1433  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1434  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1435}
1436
1437/*
1438** Output the given string as a hex-encoded blob (eg. X'1234' )
1439*/
1440static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1441  int i;
1442  char *zBlob = (char *)pBlob;
1443  raw_printf(out,"X'");
1444  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1445  raw_printf(out,"'");
1446}
1447
1448/*
1449** Find a string that is not found anywhere in z[].  Return a pointer
1450** to that string.
1451**
1452** Try to use zA and zB first.  If both of those are already found in z[]
1453** then make up some string and store it in the buffer zBuf.
1454*/
1455static const char *unused_string(
1456  const char *z,                    /* Result must not appear anywhere in z */
1457  const char *zA, const char *zB,   /* Try these first */
1458  char *zBuf                        /* Space to store a generated string */
1459){
1460  unsigned i = 0;
1461  if( strstr(z, zA)==0 ) return zA;
1462  if( strstr(z, zB)==0 ) return zB;
1463  do{
1464    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1465  }while( strstr(z,zBuf)!=0 );
1466  return zBuf;
1467}
1468
1469/*
1470** Output the given string as a quoted string using SQL quoting conventions.
1471**
1472** See also: output_quoted_escaped_string()
1473*/
1474static void output_quoted_string(FILE *out, const char *z){
1475  int i;
1476  char c;
1477  setBinaryMode(out, 1);
1478  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1479  if( c==0 ){
1480    utf8_printf(out,"'%s'",z);
1481  }else{
1482    raw_printf(out, "'");
1483    while( *z ){
1484      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1485      if( c=='\'' ) i++;
1486      if( i ){
1487        utf8_printf(out, "%.*s", i, z);
1488        z += i;
1489      }
1490      if( c=='\'' ){
1491        raw_printf(out, "'");
1492        continue;
1493      }
1494      if( c==0 ){
1495        break;
1496      }
1497      z++;
1498    }
1499    raw_printf(out, "'");
1500  }
1501  setTextMode(out, 1);
1502}
1503
1504/*
1505** Output the given string as a quoted string using SQL quoting conventions.
1506** Additionallly , escape the "\n" and "\r" characters so that they do not
1507** get corrupted by end-of-line translation facilities in some operating
1508** systems.
1509**
1510** This is like output_quoted_string() but with the addition of the \r\n
1511** escape mechanism.
1512*/
1513static void output_quoted_escaped_string(FILE *out, const char *z){
1514  int i;
1515  char c;
1516  setBinaryMode(out, 1);
1517  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1518  if( c==0 ){
1519    utf8_printf(out,"'%s'",z);
1520  }else{
1521    const char *zNL = 0;
1522    const char *zCR = 0;
1523    int nNL = 0;
1524    int nCR = 0;
1525    char zBuf1[20], zBuf2[20];
1526    for(i=0; z[i]; i++){
1527      if( z[i]=='\n' ) nNL++;
1528      if( z[i]=='\r' ) nCR++;
1529    }
1530    if( nNL ){
1531      raw_printf(out, "replace(");
1532      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1533    }
1534    if( nCR ){
1535      raw_printf(out, "replace(");
1536      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1537    }
1538    raw_printf(out, "'");
1539    while( *z ){
1540      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1541      if( c=='\'' ) i++;
1542      if( i ){
1543        utf8_printf(out, "%.*s", i, z);
1544        z += i;
1545      }
1546      if( c=='\'' ){
1547        raw_printf(out, "'");
1548        continue;
1549      }
1550      if( c==0 ){
1551        break;
1552      }
1553      z++;
1554      if( c=='\n' ){
1555        raw_printf(out, "%s", zNL);
1556        continue;
1557      }
1558      raw_printf(out, "%s", zCR);
1559    }
1560    raw_printf(out, "'");
1561    if( nCR ){
1562      raw_printf(out, ",'%s',char(13))", zCR);
1563    }
1564    if( nNL ){
1565      raw_printf(out, ",'%s',char(10))", zNL);
1566    }
1567  }
1568  setTextMode(out, 1);
1569}
1570
1571/*
1572** Output the given string as a quoted according to C or TCL quoting rules.
1573*/
1574static void output_c_string(FILE *out, const char *z){
1575  unsigned int c;
1576  fputc('"', out);
1577  while( (c = *(z++))!=0 ){
1578    if( c=='\\' ){
1579      fputc(c, out);
1580      fputc(c, out);
1581    }else if( c=='"' ){
1582      fputc('\\', out);
1583      fputc('"', out);
1584    }else if( c=='\t' ){
1585      fputc('\\', out);
1586      fputc('t', out);
1587    }else if( c=='\n' ){
1588      fputc('\\', out);
1589      fputc('n', out);
1590    }else if( c=='\r' ){
1591      fputc('\\', out);
1592      fputc('r', out);
1593    }else if( !isprint(c&0xff) ){
1594      raw_printf(out, "\\%03o", c&0xff);
1595    }else{
1596      fputc(c, out);
1597    }
1598  }
1599  fputc('"', out);
1600}
1601
1602/*
1603** Output the given string as a quoted according to JSON quoting rules.
1604*/
1605static void output_json_string(FILE *out, const char *z, int n){
1606  unsigned int c;
1607  if( n<0 ) n = (int)strlen(z);
1608  fputc('"', out);
1609  while( n-- ){
1610    c = *(z++);
1611    if( c=='\\' || c=='"' ){
1612      fputc('\\', out);
1613      fputc(c, out);
1614    }else if( c<=0x1f ){
1615      fputc('\\', out);
1616      if( c=='\b' ){
1617        fputc('b', out);
1618      }else if( c=='\f' ){
1619        fputc('f', out);
1620      }else if( c=='\n' ){
1621        fputc('n', out);
1622      }else if( c=='\r' ){
1623        fputc('r', out);
1624      }else if( c=='\t' ){
1625        fputc('t', out);
1626      }else{
1627         raw_printf(out, "u%04x",c);
1628      }
1629    }else{
1630      fputc(c, out);
1631    }
1632  }
1633  fputc('"', out);
1634}
1635
1636/*
1637** Output the given string with characters that are special to
1638** HTML escaped.
1639*/
1640static void output_html_string(FILE *out, const char *z){
1641  int i;
1642  if( z==0 ) z = "";
1643  while( *z ){
1644    for(i=0;   z[i]
1645            && z[i]!='<'
1646            && z[i]!='&'
1647            && z[i]!='>'
1648            && z[i]!='\"'
1649            && z[i]!='\'';
1650        i++){}
1651    if( i>0 ){
1652      utf8_printf(out,"%.*s",i,z);
1653    }
1654    if( z[i]=='<' ){
1655      raw_printf(out,"&lt;");
1656    }else if( z[i]=='&' ){
1657      raw_printf(out,"&amp;");
1658    }else if( z[i]=='>' ){
1659      raw_printf(out,"&gt;");
1660    }else if( z[i]=='\"' ){
1661      raw_printf(out,"&quot;");
1662    }else if( z[i]=='\'' ){
1663      raw_printf(out,"&#39;");
1664    }else{
1665      break;
1666    }
1667    z += i + 1;
1668  }
1669}
1670
1671/*
1672** If a field contains any character identified by a 1 in the following
1673** array, then the string must be quoted for CSV.
1674*/
1675static const char needCsvQuote[] = {
1676  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1677  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1678  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1679  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1680  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1681  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1682  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1683  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1684  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1685  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1686  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1687  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1688  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1689  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1690  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1691  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1692};
1693
1694/*
1695** Output a single term of CSV.  Actually, p->colSeparator is used for
1696** the separator, which may or may not be a comma.  p->nullValue is
1697** the null value.  Strings are quoted if necessary.  The separator
1698** is only issued if bSep is true.
1699*/
1700static void output_csv(ShellState *p, const char *z, int bSep){
1701  FILE *out = p->out;
1702  if( z==0 ){
1703    utf8_printf(out,"%s",p->nullValue);
1704  }else{
1705    int i;
1706    int nSep = strlen30(p->colSeparator);
1707    for(i=0; z[i]; i++){
1708      if( needCsvQuote[((unsigned char*)z)[i]]
1709         || (z[i]==p->colSeparator[0] &&
1710             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1711        i = 0;
1712        break;
1713      }
1714    }
1715    if( i==0 ){
1716      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1717      utf8_printf(out, "%s", zQuoted);
1718      sqlite3_free(zQuoted);
1719    }else{
1720      utf8_printf(out, "%s", z);
1721    }
1722  }
1723  if( bSep ){
1724    utf8_printf(p->out, "%s", p->colSeparator);
1725  }
1726}
1727
1728/*
1729** This routine runs when the user presses Ctrl-C
1730*/
1731static void interrupt_handler(int NotUsed){
1732  UNUSED_PARAMETER(NotUsed);
1733  seenInterrupt++;
1734  if( seenInterrupt>2 ) exit(1);
1735  if( globalDb ) sqlite3_interrupt(globalDb);
1736}
1737
1738#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1739/*
1740** This routine runs for console events (e.g. Ctrl-C) on Win32
1741*/
1742static BOOL WINAPI ConsoleCtrlHandler(
1743  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1744){
1745  if( dwCtrlType==CTRL_C_EVENT ){
1746    interrupt_handler(0);
1747    return TRUE;
1748  }
1749  return FALSE;
1750}
1751#endif
1752
1753#ifndef SQLITE_OMIT_AUTHORIZATION
1754/*
1755** When the ".auth ON" is set, the following authorizer callback is
1756** invoked.  It always returns SQLITE_OK.
1757*/
1758static int shellAuth(
1759  void *pClientData,
1760  int op,
1761  const char *zA1,
1762  const char *zA2,
1763  const char *zA3,
1764  const char *zA4
1765){
1766  ShellState *p = (ShellState*)pClientData;
1767  static const char *azAction[] = { 0,
1768     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1769     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1770     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1771     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1772     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1773     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1774     "PRAGMA",               "READ",                 "SELECT",
1775     "TRANSACTION",          "UPDATE",               "ATTACH",
1776     "DETACH",               "ALTER_TABLE",          "REINDEX",
1777     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1778     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1779  };
1780  int i;
1781  const char *az[4];
1782  az[0] = zA1;
1783  az[1] = zA2;
1784  az[2] = zA3;
1785  az[3] = zA4;
1786  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1787  for(i=0; i<4; i++){
1788    raw_printf(p->out, " ");
1789    if( az[i] ){
1790      output_c_string(p->out, az[i]);
1791    }else{
1792      raw_printf(p->out, "NULL");
1793    }
1794  }
1795  raw_printf(p->out, "\n");
1796  return SQLITE_OK;
1797}
1798#endif
1799
1800/*
1801** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1802**
1803** This routine converts some CREATE TABLE statements for shadow tables
1804** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1805*/
1806static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1807  if( z==0 ) return;
1808  if( zTail==0 ) return;
1809  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1810    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1811  }else{
1812    utf8_printf(out, "%s%s", z, zTail);
1813  }
1814}
1815static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1816  char c = z[n];
1817  z[n] = 0;
1818  printSchemaLine(out, z, zTail);
1819  z[n] = c;
1820}
1821
1822/*
1823** Return true if string z[] has nothing but whitespace and comments to the
1824** end of the first line.
1825*/
1826static int wsToEol(const char *z){
1827  int i;
1828  for(i=0; z[i]; i++){
1829    if( z[i]=='\n' ) return 1;
1830    if( IsSpace(z[i]) ) continue;
1831    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1832    return 0;
1833  }
1834  return 1;
1835}
1836
1837/*
1838** Add a new entry to the EXPLAIN QUERY PLAN data
1839*/
1840static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1841  EQPGraphRow *pNew;
1842  int nText = strlen30(zText);
1843  if( p->autoEQPtest ){
1844    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1845  }
1846  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1847  if( pNew==0 ) shell_out_of_memory();
1848  pNew->iEqpId = iEqpId;
1849  pNew->iParentId = p2;
1850  memcpy(pNew->zText, zText, nText+1);
1851  pNew->pNext = 0;
1852  if( p->sGraph.pLast ){
1853    p->sGraph.pLast->pNext = pNew;
1854  }else{
1855    p->sGraph.pRow = pNew;
1856  }
1857  p->sGraph.pLast = pNew;
1858}
1859
1860/*
1861** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1862** in p->sGraph.
1863*/
1864static void eqp_reset(ShellState *p){
1865  EQPGraphRow *pRow, *pNext;
1866  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1867    pNext = pRow->pNext;
1868    sqlite3_free(pRow);
1869  }
1870  memset(&p->sGraph, 0, sizeof(p->sGraph));
1871}
1872
1873/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1874** pOld, or return the first such line if pOld is NULL
1875*/
1876static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1877  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1878  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1879  return pRow;
1880}
1881
1882/* Render a single level of the graph that has iEqpId as its parent.  Called
1883** recursively to render sublevels.
1884*/
1885static void eqp_render_level(ShellState *p, int iEqpId){
1886  EQPGraphRow *pRow, *pNext;
1887  int n = strlen30(p->sGraph.zPrefix);
1888  char *z;
1889  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1890    pNext = eqp_next_row(p, iEqpId, pRow);
1891    z = pRow->zText;
1892    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1893                pNext ? "|--" : "`--", z);
1894    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1895      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1896      eqp_render_level(p, pRow->iEqpId);
1897      p->sGraph.zPrefix[n] = 0;
1898    }
1899  }
1900}
1901
1902/*
1903** Display and reset the EXPLAIN QUERY PLAN data
1904*/
1905static void eqp_render(ShellState *p){
1906  EQPGraphRow *pRow = p->sGraph.pRow;
1907  if( pRow ){
1908    if( pRow->zText[0]=='-' ){
1909      if( pRow->pNext==0 ){
1910        eqp_reset(p);
1911        return;
1912      }
1913      utf8_printf(p->out, "%s\n", pRow->zText+3);
1914      p->sGraph.pRow = pRow->pNext;
1915      sqlite3_free(pRow);
1916    }else{
1917      utf8_printf(p->out, "QUERY PLAN\n");
1918    }
1919    p->sGraph.zPrefix[0] = 0;
1920    eqp_render_level(p, 0);
1921    eqp_reset(p);
1922  }
1923}
1924
1925#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1926/*
1927** Progress handler callback.
1928*/
1929static int progress_handler(void *pClientData) {
1930  ShellState *p = (ShellState*)pClientData;
1931  p->nProgress++;
1932  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1933    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1934    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1935    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1936    return 1;
1937  }
1938  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1939    raw_printf(p->out, "Progress %u\n", p->nProgress);
1940  }
1941  return 0;
1942}
1943#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1944
1945/*
1946** Print N dashes
1947*/
1948static void print_dashes(FILE *out, int N){
1949  const char zDash[] = "--------------------------------------------------";
1950  const int nDash = sizeof(zDash) - 1;
1951  while( N>nDash ){
1952    fputs(zDash, out);
1953    N -= nDash;
1954  }
1955  raw_printf(out, "%.*s", N, zDash);
1956}
1957
1958/*
1959** Print a markdown or table-style row separator using ascii-art
1960*/
1961static void print_row_separator(
1962  ShellState *p,
1963  int nArg,
1964  const char *zSep
1965){
1966  int i;
1967  if( nArg>0 ){
1968    fputs(zSep, p->out);
1969    print_dashes(p->out, p->actualWidth[0]+2);
1970    for(i=1; i<nArg; i++){
1971      fputs(zSep, p->out);
1972      print_dashes(p->out, p->actualWidth[i]+2);
1973    }
1974    fputs(zSep, p->out);
1975  }
1976  fputs("\n", p->out);
1977}
1978
1979/*
1980** This is the callback routine that the shell
1981** invokes for each row of a query result.
1982*/
1983static int shell_callback(
1984  void *pArg,
1985  int nArg,        /* Number of result columns */
1986  char **azArg,    /* Text of each result column */
1987  char **azCol,    /* Column names */
1988  int *aiType      /* Column types.  Might be NULL */
1989){
1990  int i;
1991  ShellState *p = (ShellState*)pArg;
1992
1993  if( azArg==0 ) return 0;
1994  switch( p->cMode ){
1995    case MODE_Line: {
1996      int w = 5;
1997      if( azArg==0 ) break;
1998      for(i=0; i<nArg; i++){
1999        int len = strlen30(azCol[i] ? azCol[i] : "");
2000        if( len>w ) w = len;
2001      }
2002      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2003      for(i=0; i<nArg; i++){
2004        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2005                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2006      }
2007      break;
2008    }
2009    case MODE_Explain: {
2010      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2011      if( nArg>ArraySize(aExplainWidth) ){
2012        nArg = ArraySize(aExplainWidth);
2013      }
2014      if( p->cnt++==0 ){
2015        for(i=0; i<nArg; i++){
2016          int w = aExplainWidth[i];
2017          utf8_width_print(p->out, w, azCol[i]);
2018          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2019        }
2020        for(i=0; i<nArg; i++){
2021          int w = aExplainWidth[i];
2022          print_dashes(p->out, w);
2023          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2024        }
2025      }
2026      if( azArg==0 ) break;
2027      for(i=0; i<nArg; i++){
2028        int w = aExplainWidth[i];
2029        if( azArg[i] && strlenChar(azArg[i])>w ){
2030          w = strlenChar(azArg[i]);
2031        }
2032        if( i==1 && p->aiIndent && p->pStmt ){
2033          if( p->iIndent<p->nIndent ){
2034            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2035          }
2036          p->iIndent++;
2037        }
2038        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2039        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2040      }
2041      break;
2042    }
2043    case MODE_Semi: {   /* .schema and .fullschema output */
2044      printSchemaLine(p->out, azArg[0], ";\n");
2045      break;
2046    }
2047    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2048      char *z;
2049      int j;
2050      int nParen = 0;
2051      char cEnd = 0;
2052      char c;
2053      int nLine = 0;
2054      assert( nArg==1 );
2055      if( azArg[0]==0 ) break;
2056      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2057       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2058      ){
2059        utf8_printf(p->out, "%s;\n", azArg[0]);
2060        break;
2061      }
2062      z = sqlite3_mprintf("%s", azArg[0]);
2063      j = 0;
2064      for(i=0; IsSpace(z[i]); i++){}
2065      for(; (c = z[i])!=0; i++){
2066        if( IsSpace(c) ){
2067          if( z[j-1]=='\r' ) z[j-1] = '\n';
2068          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2069        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2070          j--;
2071        }
2072        z[j++] = c;
2073      }
2074      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2075      z[j] = 0;
2076      if( strlen30(z)>=79 ){
2077        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2078          if( c==cEnd ){
2079            cEnd = 0;
2080          }else if( c=='"' || c=='\'' || c=='`' ){
2081            cEnd = c;
2082          }else if( c=='[' ){
2083            cEnd = ']';
2084          }else if( c=='-' && z[i+1]=='-' ){
2085            cEnd = '\n';
2086          }else if( c=='(' ){
2087            nParen++;
2088          }else if( c==')' ){
2089            nParen--;
2090            if( nLine>0 && nParen==0 && j>0 ){
2091              printSchemaLineN(p->out, z, j, "\n");
2092              j = 0;
2093            }
2094          }
2095          z[j++] = c;
2096          if( nParen==1 && cEnd==0
2097           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2098          ){
2099            if( c=='\n' ) j--;
2100            printSchemaLineN(p->out, z, j, "\n  ");
2101            j = 0;
2102            nLine++;
2103            while( IsSpace(z[i+1]) ){ i++; }
2104          }
2105        }
2106        z[j] = 0;
2107      }
2108      printSchemaLine(p->out, z, ";\n");
2109      sqlite3_free(z);
2110      break;
2111    }
2112    case MODE_List: {
2113      if( p->cnt++==0 && p->showHeader ){
2114        for(i=0; i<nArg; i++){
2115          utf8_printf(p->out,"%s%s",azCol[i],
2116                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2117        }
2118      }
2119      if( azArg==0 ) break;
2120      for(i=0; i<nArg; i++){
2121        char *z = azArg[i];
2122        if( z==0 ) z = p->nullValue;
2123        utf8_printf(p->out, "%s", z);
2124        if( i<nArg-1 ){
2125          utf8_printf(p->out, "%s", p->colSeparator);
2126        }else{
2127          utf8_printf(p->out, "%s", p->rowSeparator);
2128        }
2129      }
2130      break;
2131    }
2132    case MODE_Html: {
2133      if( p->cnt++==0 && p->showHeader ){
2134        raw_printf(p->out,"<TR>");
2135        for(i=0; i<nArg; i++){
2136          raw_printf(p->out,"<TH>");
2137          output_html_string(p->out, azCol[i]);
2138          raw_printf(p->out,"</TH>\n");
2139        }
2140        raw_printf(p->out,"</TR>\n");
2141      }
2142      if( azArg==0 ) break;
2143      raw_printf(p->out,"<TR>");
2144      for(i=0; i<nArg; i++){
2145        raw_printf(p->out,"<TD>");
2146        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2147        raw_printf(p->out,"</TD>\n");
2148      }
2149      raw_printf(p->out,"</TR>\n");
2150      break;
2151    }
2152    case MODE_Tcl: {
2153      if( p->cnt++==0 && p->showHeader ){
2154        for(i=0; i<nArg; i++){
2155          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2156          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2157        }
2158        utf8_printf(p->out, "%s", p->rowSeparator);
2159      }
2160      if( azArg==0 ) break;
2161      for(i=0; i<nArg; i++){
2162        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2163        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2164      }
2165      utf8_printf(p->out, "%s", p->rowSeparator);
2166      break;
2167    }
2168    case MODE_Csv: {
2169      setBinaryMode(p->out, 1);
2170      if( p->cnt++==0 && p->showHeader ){
2171        for(i=0; i<nArg; i++){
2172          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2173        }
2174        utf8_printf(p->out, "%s", p->rowSeparator);
2175      }
2176      if( nArg>0 ){
2177        for(i=0; i<nArg; i++){
2178          output_csv(p, azArg[i], i<nArg-1);
2179        }
2180        utf8_printf(p->out, "%s", p->rowSeparator);
2181      }
2182      setTextMode(p->out, 1);
2183      break;
2184    }
2185    case MODE_Insert: {
2186      if( azArg==0 ) break;
2187      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2188      if( p->showHeader ){
2189        raw_printf(p->out,"(");
2190        for(i=0; i<nArg; i++){
2191          if( i>0 ) raw_printf(p->out, ",");
2192          if( quoteChar(azCol[i]) ){
2193            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2194            utf8_printf(p->out, "%s", z);
2195            sqlite3_free(z);
2196          }else{
2197            raw_printf(p->out, "%s", azCol[i]);
2198          }
2199        }
2200        raw_printf(p->out,")");
2201      }
2202      p->cnt++;
2203      for(i=0; i<nArg; i++){
2204        raw_printf(p->out, i>0 ? "," : " VALUES(");
2205        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2206          utf8_printf(p->out,"NULL");
2207        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2208          if( ShellHasFlag(p, SHFLG_Newlines) ){
2209            output_quoted_string(p->out, azArg[i]);
2210          }else{
2211            output_quoted_escaped_string(p->out, azArg[i]);
2212          }
2213        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2214          utf8_printf(p->out,"%s", azArg[i]);
2215        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2216          char z[50];
2217          double r = sqlite3_column_double(p->pStmt, i);
2218          sqlite3_uint64 ur;
2219          memcpy(&ur,&r,sizeof(r));
2220          if( ur==0x7ff0000000000000LL ){
2221            raw_printf(p->out, "1e999");
2222          }else if( ur==0xfff0000000000000LL ){
2223            raw_printf(p->out, "-1e999");
2224          }else{
2225            sqlite3_snprintf(50,z,"%!.20g", r);
2226            raw_printf(p->out, "%s", z);
2227          }
2228        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2229          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2230          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2231          output_hex_blob(p->out, pBlob, nBlob);
2232        }else if( isNumber(azArg[i], 0) ){
2233          utf8_printf(p->out,"%s", azArg[i]);
2234        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2235          output_quoted_string(p->out, azArg[i]);
2236        }else{
2237          output_quoted_escaped_string(p->out, azArg[i]);
2238        }
2239      }
2240      raw_printf(p->out,");\n");
2241      break;
2242    }
2243    case MODE_Json: {
2244      if( azArg==0 ) break;
2245      if( p->cnt==0 ){
2246        fputs("[{", p->out);
2247      }else{
2248        fputs(",\n{", p->out);
2249      }
2250      p->cnt++;
2251      for(i=0; i<nArg; i++){
2252        output_json_string(p->out, azCol[i], -1);
2253        putc(':', p->out);
2254        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2255          fputs("null",p->out);
2256        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2257          char z[50];
2258          double r = sqlite3_column_double(p->pStmt, i);
2259          sqlite3_uint64 ur;
2260          memcpy(&ur,&r,sizeof(r));
2261          if( ur==0x7ff0000000000000LL ){
2262            raw_printf(p->out, "1e999");
2263          }else if( ur==0xfff0000000000000LL ){
2264            raw_printf(p->out, "-1e999");
2265          }else{
2266            sqlite3_snprintf(50,z,"%!.20g", r);
2267            raw_printf(p->out, "%s", z);
2268          }
2269        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2270          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2271          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2272          output_json_string(p->out, pBlob, nBlob);
2273        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2274          output_json_string(p->out, azArg[i], -1);
2275        }else{
2276          utf8_printf(p->out,"%s", azArg[i]);
2277        }
2278        if( i<nArg-1 ){
2279          putc(',', p->out);
2280        }
2281      }
2282      putc('}', p->out);
2283      break;
2284    }
2285    case MODE_Quote: {
2286      if( azArg==0 ) break;
2287      if( p->cnt==0 && p->showHeader ){
2288        for(i=0; i<nArg; i++){
2289          if( i>0 ) fputs(p->colSeparator, p->out);
2290          output_quoted_string(p->out, azCol[i]);
2291        }
2292        fputs(p->rowSeparator, p->out);
2293      }
2294      p->cnt++;
2295      for(i=0; i<nArg; i++){
2296        if( i>0 ) fputs(p->colSeparator, p->out);
2297        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2298          utf8_printf(p->out,"NULL");
2299        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2300          output_quoted_string(p->out, azArg[i]);
2301        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2302          utf8_printf(p->out,"%s", azArg[i]);
2303        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2304          char z[50];
2305          double r = sqlite3_column_double(p->pStmt, i);
2306          sqlite3_snprintf(50,z,"%!.20g", r);
2307          raw_printf(p->out, "%s", z);
2308        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2309          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2310          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2311          output_hex_blob(p->out, pBlob, nBlob);
2312        }else if( isNumber(azArg[i], 0) ){
2313          utf8_printf(p->out,"%s", azArg[i]);
2314        }else{
2315          output_quoted_string(p->out, azArg[i]);
2316        }
2317      }
2318      fputs(p->rowSeparator, p->out);
2319      break;
2320    }
2321    case MODE_Ascii: {
2322      if( p->cnt++==0 && p->showHeader ){
2323        for(i=0; i<nArg; i++){
2324          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2325          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2326        }
2327        utf8_printf(p->out, "%s", p->rowSeparator);
2328      }
2329      if( azArg==0 ) break;
2330      for(i=0; i<nArg; i++){
2331        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2332        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2333      }
2334      utf8_printf(p->out, "%s", p->rowSeparator);
2335      break;
2336    }
2337    case MODE_EQP: {
2338      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2339      break;
2340    }
2341  }
2342  return 0;
2343}
2344
2345/*
2346** This is the callback routine that the SQLite library
2347** invokes for each row of a query result.
2348*/
2349static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2350  /* since we don't have type info, call the shell_callback with a NULL value */
2351  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2352}
2353
2354/*
2355** This is the callback routine from sqlite3_exec() that appends all
2356** output onto the end of a ShellText object.
2357*/
2358static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2359  ShellText *p = (ShellText*)pArg;
2360  int i;
2361  UNUSED_PARAMETER(az);
2362  if( azArg==0 ) return 0;
2363  if( p->n ) appendText(p, "|", 0);
2364  for(i=0; i<nArg; i++){
2365    if( i ) appendText(p, ",", 0);
2366    if( azArg[i] ) appendText(p, azArg[i], 0);
2367  }
2368  return 0;
2369}
2370
2371/*
2372** Generate an appropriate SELFTEST table in the main database.
2373*/
2374static void createSelftestTable(ShellState *p){
2375  char *zErrMsg = 0;
2376  sqlite3_exec(p->db,
2377    "SAVEPOINT selftest_init;\n"
2378    "CREATE TABLE IF NOT EXISTS selftest(\n"
2379    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2380    "  op TEXT,\n"                   /* Operator:  memo run */
2381    "  cmd TEXT,\n"                  /* Command text */
2382    "  ans TEXT\n"                   /* Desired answer */
2383    ");"
2384    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2385    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2386    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2387    "         'memo','Tests generated by --init');\n"
2388    "INSERT INTO [_shell$self]\n"
2389    "  SELECT 'run',\n"
2390    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2391                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2392    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2393                          "FROM sqlite_schema ORDER BY 2',224));\n"
2394    "INSERT INTO [_shell$self]\n"
2395    "  SELECT 'run',"
2396    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2397    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2398    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2399    "  FROM (\n"
2400    "    SELECT name FROM sqlite_schema\n"
2401    "     WHERE type='table'\n"
2402    "       AND name<>'selftest'\n"
2403    "       AND coalesce(rootpage,0)>0\n"
2404    "  )\n"
2405    " ORDER BY name;\n"
2406    "INSERT INTO [_shell$self]\n"
2407    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2408    "INSERT INTO selftest(tno,op,cmd,ans)"
2409    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2410    "DROP TABLE [_shell$self];"
2411    ,0,0,&zErrMsg);
2412  if( zErrMsg ){
2413    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2414    sqlite3_free(zErrMsg);
2415  }
2416  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2417}
2418
2419
2420/*
2421** Set the destination table field of the ShellState structure to
2422** the name of the table given.  Escape any quote characters in the
2423** table name.
2424*/
2425static void set_table_name(ShellState *p, const char *zName){
2426  int i, n;
2427  char cQuote;
2428  char *z;
2429
2430  if( p->zDestTable ){
2431    free(p->zDestTable);
2432    p->zDestTable = 0;
2433  }
2434  if( zName==0 ) return;
2435  cQuote = quoteChar(zName);
2436  n = strlen30(zName);
2437  if( cQuote ) n += n+2;
2438  z = p->zDestTable = malloc( n+1 );
2439  if( z==0 ) shell_out_of_memory();
2440  n = 0;
2441  if( cQuote ) z[n++] = cQuote;
2442  for(i=0; zName[i]; i++){
2443    z[n++] = zName[i];
2444    if( zName[i]==cQuote ) z[n++] = cQuote;
2445  }
2446  if( cQuote ) z[n++] = cQuote;
2447  z[n] = 0;
2448}
2449
2450
2451/*
2452** Execute a query statement that will generate SQL output.  Print
2453** the result columns, comma-separated, on a line and then add a
2454** semicolon terminator to the end of that line.
2455**
2456** If the number of columns is 1 and that column contains text "--"
2457** then write the semicolon on a separate line.  That way, if a
2458** "--" comment occurs at the end of the statement, the comment
2459** won't consume the semicolon terminator.
2460*/
2461static int run_table_dump_query(
2462  ShellState *p,           /* Query context */
2463  const char *zSelect      /* SELECT statement to extract content */
2464){
2465  sqlite3_stmt *pSelect;
2466  int rc;
2467  int nResult;
2468  int i;
2469  const char *z;
2470  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2471  if( rc!=SQLITE_OK || !pSelect ){
2472    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2473                sqlite3_errmsg(p->db));
2474    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2475    return rc;
2476  }
2477  rc = sqlite3_step(pSelect);
2478  nResult = sqlite3_column_count(pSelect);
2479  while( rc==SQLITE_ROW ){
2480    z = (const char*)sqlite3_column_text(pSelect, 0);
2481    utf8_printf(p->out, "%s", z);
2482    for(i=1; i<nResult; i++){
2483      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2484    }
2485    if( z==0 ) z = "";
2486    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2487    if( z[0] ){
2488      raw_printf(p->out, "\n;\n");
2489    }else{
2490      raw_printf(p->out, ";\n");
2491    }
2492    rc = sqlite3_step(pSelect);
2493  }
2494  rc = sqlite3_finalize(pSelect);
2495  if( rc!=SQLITE_OK ){
2496    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2497                sqlite3_errmsg(p->db));
2498    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2499  }
2500  return rc;
2501}
2502
2503/*
2504** Allocate space and save off current error string.
2505*/
2506static char *save_err_msg(
2507  sqlite3 *db            /* Database to query */
2508){
2509  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2510  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2511  if( zErrMsg ){
2512    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2513  }
2514  return zErrMsg;
2515}
2516
2517#ifdef __linux__
2518/*
2519** Attempt to display I/O stats on Linux using /proc/PID/io
2520*/
2521static void displayLinuxIoStats(FILE *out){
2522  FILE *in;
2523  char z[200];
2524  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2525  in = fopen(z, "rb");
2526  if( in==0 ) return;
2527  while( fgets(z, sizeof(z), in)!=0 ){
2528    static const struct {
2529      const char *zPattern;
2530      const char *zDesc;
2531    } aTrans[] = {
2532      { "rchar: ",                  "Bytes received by read():" },
2533      { "wchar: ",                  "Bytes sent to write():"    },
2534      { "syscr: ",                  "Read() system calls:"      },
2535      { "syscw: ",                  "Write() system calls:"     },
2536      { "read_bytes: ",             "Bytes read from storage:"  },
2537      { "write_bytes: ",            "Bytes written to storage:" },
2538      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2539    };
2540    int i;
2541    for(i=0; i<ArraySize(aTrans); i++){
2542      int n = strlen30(aTrans[i].zPattern);
2543      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2544        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2545        break;
2546      }
2547    }
2548  }
2549  fclose(in);
2550}
2551#endif
2552
2553/*
2554** Display a single line of status using 64-bit values.
2555*/
2556static void displayStatLine(
2557  ShellState *p,            /* The shell context */
2558  char *zLabel,             /* Label for this one line */
2559  char *zFormat,            /* Format for the result */
2560  int iStatusCtrl,          /* Which status to display */
2561  int bReset                /* True to reset the stats */
2562){
2563  sqlite3_int64 iCur = -1;
2564  sqlite3_int64 iHiwtr = -1;
2565  int i, nPercent;
2566  char zLine[200];
2567  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2568  for(i=0, nPercent=0; zFormat[i]; i++){
2569    if( zFormat[i]=='%' ) nPercent++;
2570  }
2571  if( nPercent>1 ){
2572    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2573  }else{
2574    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2575  }
2576  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2577}
2578
2579/*
2580** Display memory stats.
2581*/
2582static int display_stats(
2583  sqlite3 *db,                /* Database to query */
2584  ShellState *pArg,           /* Pointer to ShellState */
2585  int bReset                  /* True to reset the stats */
2586){
2587  int iCur;
2588  int iHiwtr;
2589  FILE *out;
2590  if( pArg==0 || pArg->out==0 ) return 0;
2591  out = pArg->out;
2592
2593  if( pArg->pStmt && (pArg->statsOn & 2) ){
2594    int nCol, i, x;
2595    sqlite3_stmt *pStmt = pArg->pStmt;
2596    char z[100];
2597    nCol = sqlite3_column_count(pStmt);
2598    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2599    for(i=0; i<nCol; i++){
2600      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2601      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2602#ifndef SQLITE_OMIT_DECLTYPE
2603      sqlite3_snprintf(30, z+x, "declared type:");
2604      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2605#endif
2606#ifdef SQLITE_ENABLE_COLUMN_METADATA
2607      sqlite3_snprintf(30, z+x, "database name:");
2608      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2609      sqlite3_snprintf(30, z+x, "table name:");
2610      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2611      sqlite3_snprintf(30, z+x, "origin name:");
2612      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2613#endif
2614    }
2615  }
2616
2617  displayStatLine(pArg, "Memory Used:",
2618     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2619  displayStatLine(pArg, "Number of Outstanding Allocations:",
2620     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2621  if( pArg->shellFlgs & SHFLG_Pagecache ){
2622    displayStatLine(pArg, "Number of Pcache Pages Used:",
2623       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2624  }
2625  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2626     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2627  displayStatLine(pArg, "Largest Allocation:",
2628     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2629  displayStatLine(pArg, "Largest Pcache Allocation:",
2630     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2631#ifdef YYTRACKMAXSTACKDEPTH
2632  displayStatLine(pArg, "Deepest Parser Stack:",
2633     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2634#endif
2635
2636  if( db ){
2637    if( pArg->shellFlgs & SHFLG_Lookaside ){
2638      iHiwtr = iCur = -1;
2639      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2640                        &iCur, &iHiwtr, bReset);
2641      raw_printf(pArg->out,
2642              "Lookaside Slots Used:                %d (max %d)\n",
2643              iCur, iHiwtr);
2644      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2645                        &iCur, &iHiwtr, bReset);
2646      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2647              iHiwtr);
2648      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2649                        &iCur, &iHiwtr, bReset);
2650      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2651              iHiwtr);
2652      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2653                        &iCur, &iHiwtr, bReset);
2654      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2655              iHiwtr);
2656    }
2657    iHiwtr = iCur = -1;
2658    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2659    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2660            iCur);
2661    iHiwtr = iCur = -1;
2662    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2663    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2664    iHiwtr = iCur = -1;
2665    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2666    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2667    iHiwtr = iCur = -1;
2668    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2669    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2670    iHiwtr = iCur = -1;
2671    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2672    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2673    iHiwtr = iCur = -1;
2674    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2675    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2676            iCur);
2677    iHiwtr = iCur = -1;
2678    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2679    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2680            iCur);
2681  }
2682
2683  if( pArg->pStmt ){
2684    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2685                               bReset);
2686    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2687    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2688    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2689    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2690    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2691    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2692    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2693    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2694    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2695    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2696    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2697    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2698    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2699  }
2700
2701#ifdef __linux__
2702  displayLinuxIoStats(pArg->out);
2703#endif
2704
2705  /* Do not remove this machine readable comment: extra-stats-output-here */
2706
2707  return 0;
2708}
2709
2710/*
2711** Display scan stats.
2712*/
2713static void display_scanstats(
2714  sqlite3 *db,                    /* Database to query */
2715  ShellState *pArg                /* Pointer to ShellState */
2716){
2717#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2718  UNUSED_PARAMETER(db);
2719  UNUSED_PARAMETER(pArg);
2720#else
2721  int i, k, n, mx;
2722  raw_printf(pArg->out, "-------- scanstats --------\n");
2723  mx = 0;
2724  for(k=0; k<=mx; k++){
2725    double rEstLoop = 1.0;
2726    for(i=n=0; 1; i++){
2727      sqlite3_stmt *p = pArg->pStmt;
2728      sqlite3_int64 nLoop, nVisit;
2729      double rEst;
2730      int iSid;
2731      const char *zExplain;
2732      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2733        break;
2734      }
2735      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2736      if( iSid>mx ) mx = iSid;
2737      if( iSid!=k ) continue;
2738      if( n==0 ){
2739        rEstLoop = (double)nLoop;
2740        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2741      }
2742      n++;
2743      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2744      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2745      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2746      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2747      rEstLoop *= rEst;
2748      raw_printf(pArg->out,
2749          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2750          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2751      );
2752    }
2753  }
2754  raw_printf(pArg->out, "---------------------------\n");
2755#endif
2756}
2757
2758/*
2759** Parameter azArray points to a zero-terminated array of strings. zStr
2760** points to a single nul-terminated string. Return non-zero if zStr
2761** is equal, according to strcmp(), to any of the strings in the array.
2762** Otherwise, return zero.
2763*/
2764static int str_in_array(const char *zStr, const char **azArray){
2765  int i;
2766  for(i=0; azArray[i]; i++){
2767    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2768  }
2769  return 0;
2770}
2771
2772/*
2773** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2774** and populate the ShellState.aiIndent[] array with the number of
2775** spaces each opcode should be indented before it is output.
2776**
2777** The indenting rules are:
2778**
2779**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2780**       all opcodes that occur between the p2 jump destination and the opcode
2781**       itself by 2 spaces.
2782**
2783**     * For each "Goto", if the jump destination is earlier in the program
2784**       and ends on one of:
2785**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2786**       or if the P1 parameter is one instead of zero,
2787**       then indent all opcodes between the earlier instruction
2788**       and "Goto" by 2 spaces.
2789*/
2790static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2791  const char *zSql;               /* The text of the SQL statement */
2792  const char *z;                  /* Used to check if this is an EXPLAIN */
2793  int *abYield = 0;               /* True if op is an OP_Yield */
2794  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2795  int iOp;                        /* Index of operation in p->aiIndent[] */
2796
2797  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2798  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2799                            "Rewind", 0 };
2800  const char *azGoto[] = { "Goto", 0 };
2801
2802  /* Try to figure out if this is really an EXPLAIN statement. If this
2803  ** cannot be verified, return early.  */
2804  if( sqlite3_column_count(pSql)!=8 ){
2805    p->cMode = p->mode;
2806    return;
2807  }
2808  zSql = sqlite3_sql(pSql);
2809  if( zSql==0 ) return;
2810  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2811  if( sqlite3_strnicmp(z, "explain", 7) ){
2812    p->cMode = p->mode;
2813    return;
2814  }
2815
2816  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2817    int i;
2818    int iAddr = sqlite3_column_int(pSql, 0);
2819    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2820
2821    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2822    ** p2 is an instruction address, set variable p2op to the index of that
2823    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2824    ** the current instruction is part of a sub-program generated by an
2825    ** SQL trigger or foreign key.  */
2826    int p2 = sqlite3_column_int(pSql, 3);
2827    int p2op = (p2 + (iOp-iAddr));
2828
2829    /* Grow the p->aiIndent array as required */
2830    if( iOp>=nAlloc ){
2831      if( iOp==0 ){
2832        /* Do further verfication that this is explain output.  Abort if
2833        ** it is not */
2834        static const char *explainCols[] = {
2835           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2836        int jj;
2837        for(jj=0; jj<ArraySize(explainCols); jj++){
2838          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2839            p->cMode = p->mode;
2840            sqlite3_reset(pSql);
2841            return;
2842          }
2843        }
2844      }
2845      nAlloc += 100;
2846      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2847      if( p->aiIndent==0 ) shell_out_of_memory();
2848      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2849      if( abYield==0 ) shell_out_of_memory();
2850    }
2851    abYield[iOp] = str_in_array(zOp, azYield);
2852    p->aiIndent[iOp] = 0;
2853    p->nIndent = iOp+1;
2854
2855    if( str_in_array(zOp, azNext) ){
2856      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2857    }
2858    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2859     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2860    ){
2861      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2862    }
2863  }
2864
2865  p->iIndent = 0;
2866  sqlite3_free(abYield);
2867  sqlite3_reset(pSql);
2868}
2869
2870/*
2871** Free the array allocated by explain_data_prepare().
2872*/
2873static void explain_data_delete(ShellState *p){
2874  sqlite3_free(p->aiIndent);
2875  p->aiIndent = 0;
2876  p->nIndent = 0;
2877  p->iIndent = 0;
2878}
2879
2880/*
2881** Disable and restore .wheretrace and .selecttrace settings.
2882*/
2883#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2884extern unsigned int sqlite3_unsupported_selecttrace;
2885static int savedSelectTrace;
2886#endif
2887#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2888extern int sqlite3WhereTrace;
2889static int savedWhereTrace;
2890#endif
2891static void disable_debug_trace_modes(void){
2892#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2893  savedSelectTrace = sqlite3_unsupported_selecttrace;
2894  sqlite3_unsupported_selecttrace = 0;
2895#endif
2896#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2897  savedWhereTrace = sqlite3WhereTrace;
2898  sqlite3WhereTrace = 0;
2899#endif
2900}
2901static void restore_debug_trace_modes(void){
2902#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2903  sqlite3_unsupported_selecttrace = savedSelectTrace;
2904#endif
2905#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2906  sqlite3WhereTrace = savedWhereTrace;
2907#endif
2908}
2909
2910/* Create the TEMP table used to store parameter bindings */
2911static void bind_table_init(ShellState *p){
2912  int wrSchema = 0;
2913  int defensiveMode = 0;
2914  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2915  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2916  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2917  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2918  sqlite3_exec(p->db,
2919    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2920    "  key TEXT PRIMARY KEY,\n"
2921    "  value ANY\n"
2922    ") WITHOUT ROWID;",
2923    0, 0, 0);
2924  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2925  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2926}
2927
2928/*
2929** Bind parameters on a prepared statement.
2930**
2931** Parameter bindings are taken from a TEMP table of the form:
2932**
2933**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2934**    WITHOUT ROWID;
2935**
2936** No bindings occur if this table does not exist.  The name of the table
2937** begins with "sqlite_" so that it will not collide with ordinary application
2938** tables.  The table must be in the TEMP schema.
2939*/
2940static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2941  int nVar;
2942  int i;
2943  int rc;
2944  sqlite3_stmt *pQ = 0;
2945
2946  nVar = sqlite3_bind_parameter_count(pStmt);
2947  if( nVar==0 ) return;  /* Nothing to do */
2948  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2949                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2950    return; /* Parameter table does not exist */
2951  }
2952  rc = sqlite3_prepare_v2(pArg->db,
2953          "SELECT value FROM temp.sqlite_parameters"
2954          " WHERE key=?1", -1, &pQ, 0);
2955  if( rc || pQ==0 ) return;
2956  for(i=1; i<=nVar; i++){
2957    char zNum[30];
2958    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2959    if( zVar==0 ){
2960      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2961      zVar = zNum;
2962    }
2963    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2964    if( sqlite3_step(pQ)==SQLITE_ROW ){
2965      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2966    }else{
2967      sqlite3_bind_null(pStmt, i);
2968    }
2969    sqlite3_reset(pQ);
2970  }
2971  sqlite3_finalize(pQ);
2972}
2973
2974/*
2975** UTF8 box-drawing characters.  Imagine box lines like this:
2976**
2977**           1
2978**           |
2979**       4 --+-- 2
2980**           |
2981**           3
2982**
2983** Each box characters has between 2 and 4 of the lines leading from
2984** the center.  The characters are here identified by the numbers of
2985** their corresponding lines.
2986*/
2987#define BOX_24   "\342\224\200"  /* U+2500 --- */
2988#define BOX_13   "\342\224\202"  /* U+2502  |  */
2989#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2990#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2991#define BOX_12   "\342\224\224"  /* U+2514  '- */
2992#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2993#define BOX_123  "\342\224\234"  /* U+251c  |- */
2994#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2995#define BOX_234  "\342\224\254"  /* U+252c -,- */
2996#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2997#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2998
2999/* Draw horizontal line N characters long using unicode box
3000** characters
3001*/
3002static void print_box_line(FILE *out, int N){
3003  const char zDash[] =
3004      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3005      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3006  const int nDash = sizeof(zDash) - 1;
3007  N *= 3;
3008  while( N>nDash ){
3009    utf8_printf(out, zDash);
3010    N -= nDash;
3011  }
3012  utf8_printf(out, "%.*s", N, zDash);
3013}
3014
3015/*
3016** Draw a horizontal separator for a MODE_Box table.
3017*/
3018static void print_box_row_separator(
3019  ShellState *p,
3020  int nArg,
3021  const char *zSep1,
3022  const char *zSep2,
3023  const char *zSep3
3024){
3025  int i;
3026  if( nArg>0 ){
3027    utf8_printf(p->out, "%s", zSep1);
3028    print_box_line(p->out, p->actualWidth[0]+2);
3029    for(i=1; i<nArg; i++){
3030      utf8_printf(p->out, "%s", zSep2);
3031      print_box_line(p->out, p->actualWidth[i]+2);
3032    }
3033    utf8_printf(p->out, "%s", zSep3);
3034  }
3035  fputs("\n", p->out);
3036}
3037
3038
3039
3040/*
3041** Run a prepared statement and output the result in one of the
3042** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3043** or MODE_Box.
3044**
3045** This is different from ordinary exec_prepared_stmt() in that
3046** it has to run the entire query and gather the results into memory
3047** first, in order to determine column widths, before providing
3048** any output.
3049*/
3050static void exec_prepared_stmt_columnar(
3051  ShellState *p,                        /* Pointer to ShellState */
3052  sqlite3_stmt *pStmt                   /* Statment to run */
3053){
3054  sqlite3_int64 nRow = 0;
3055  int nColumn = 0;
3056  char **azData = 0;
3057  sqlite3_int64 nAlloc = 0;
3058  const char *z;
3059  int rc;
3060  sqlite3_int64 i, nData;
3061  int j, nTotal, w, n;
3062  const char *colSep = 0;
3063  const char *rowSep = 0;
3064
3065  rc = sqlite3_step(pStmt);
3066  if( rc!=SQLITE_ROW ) return;
3067  nColumn = sqlite3_column_count(pStmt);
3068  nAlloc = nColumn*4;
3069  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3070  if( azData==0 ) shell_out_of_memory();
3071  for(i=0; i<nColumn; i++){
3072    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3073  }
3074  do{
3075    if( (nRow+2)*nColumn >= nAlloc ){
3076      nAlloc *= 2;
3077      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3078      if( azData==0 ) shell_out_of_memory();
3079    }
3080    nRow++;
3081    for(i=0; i<nColumn; i++){
3082      z = (const char*)sqlite3_column_text(pStmt,i);
3083      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3084    }
3085  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3086  if( nColumn>p->nWidth ){
3087    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3088    if( p->colWidth==0 ) shell_out_of_memory();
3089    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3090    p->nWidth = nColumn;
3091    p->actualWidth = &p->colWidth[nColumn];
3092  }
3093  memset(p->actualWidth, 0, nColumn*sizeof(int));
3094  for(i=0; i<nColumn; i++){
3095    w = p->colWidth[i];
3096    if( w<0 ) w = -w;
3097    p->actualWidth[i] = w;
3098  }
3099  nTotal = nColumn*(nRow+1);
3100  for(i=0; i<nTotal; i++){
3101    z = azData[i];
3102    if( z==0 ) z = p->nullValue;
3103    n = strlenChar(z);
3104    j = i%nColumn;
3105    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3106  }
3107  if( seenInterrupt ) goto columnar_end;
3108  switch( p->cMode ){
3109    case MODE_Column: {
3110      colSep = "  ";
3111      rowSep = "\n";
3112      if( p->showHeader ){
3113        for(i=0; i<nColumn; i++){
3114          w = p->actualWidth[i];
3115          if( p->colWidth[i]<0 ) w = -w;
3116          utf8_width_print(p->out, w, azData[i]);
3117          fputs(i==nColumn-1?"\n":"  ", p->out);
3118        }
3119        for(i=0; i<nColumn; i++){
3120          print_dashes(p->out, p->actualWidth[i]);
3121          fputs(i==nColumn-1?"\n":"  ", p->out);
3122        }
3123      }
3124      break;
3125    }
3126    case MODE_Table: {
3127      colSep = " | ";
3128      rowSep = " |\n";
3129      print_row_separator(p, nColumn, "+");
3130      fputs("| ", p->out);
3131      for(i=0; i<nColumn; i++){
3132        w = p->actualWidth[i];
3133        n = strlenChar(azData[i]);
3134        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3135        fputs(i==nColumn-1?" |\n":" | ", p->out);
3136      }
3137      print_row_separator(p, nColumn, "+");
3138      break;
3139    }
3140    case MODE_Markdown: {
3141      colSep = " | ";
3142      rowSep = " |\n";
3143      fputs("| ", p->out);
3144      for(i=0; i<nColumn; i++){
3145        w = p->actualWidth[i];
3146        n = strlenChar(azData[i]);
3147        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3148        fputs(i==nColumn-1?" |\n":" | ", p->out);
3149      }
3150      print_row_separator(p, nColumn, "|");
3151      break;
3152    }
3153    case MODE_Box: {
3154      colSep = " " BOX_13 " ";
3155      rowSep = " " BOX_13 "\n";
3156      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3157      utf8_printf(p->out, BOX_13 " ");
3158      for(i=0; i<nColumn; i++){
3159        w = p->actualWidth[i];
3160        n = strlenChar(azData[i]);
3161        utf8_printf(p->out, "%*s%s%*s%s",
3162            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3163            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3164      }
3165      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3166      break;
3167    }
3168  }
3169  for(i=nColumn, j=0; i<nTotal; i++, j++){
3170    if( j==0 && p->cMode!=MODE_Column ){
3171      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3172    }
3173    z = azData[i];
3174    if( z==0 ) z = p->nullValue;
3175    w = p->actualWidth[j];
3176    if( p->colWidth[j]<0 ) w = -w;
3177    utf8_width_print(p->out, w, z);
3178    if( j==nColumn-1 ){
3179      utf8_printf(p->out, "%s", rowSep);
3180      j = -1;
3181      if( seenInterrupt ) goto columnar_end;
3182    }else{
3183      utf8_printf(p->out, "%s", colSep);
3184    }
3185  }
3186  if( p->cMode==MODE_Table ){
3187    print_row_separator(p, nColumn, "+");
3188  }else if( p->cMode==MODE_Box ){
3189    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3190  }
3191columnar_end:
3192  if( seenInterrupt ){
3193    utf8_printf(p->out, "Interrupt\n");
3194  }
3195  nData = (nRow+1)*nColumn;
3196  for(i=0; i<nData; i++) free(azData[i]);
3197  sqlite3_free(azData);
3198}
3199
3200/*
3201** Run a prepared statement
3202*/
3203static void exec_prepared_stmt(
3204  ShellState *pArg,                                /* Pointer to ShellState */
3205  sqlite3_stmt *pStmt                              /* Statment to run */
3206){
3207  int rc;
3208
3209  if( pArg->cMode==MODE_Column
3210   || pArg->cMode==MODE_Table
3211   || pArg->cMode==MODE_Box
3212   || pArg->cMode==MODE_Markdown
3213  ){
3214    exec_prepared_stmt_columnar(pArg, pStmt);
3215    return;
3216  }
3217
3218  /* perform the first step.  this will tell us if we
3219  ** have a result set or not and how wide it is.
3220  */
3221  rc = sqlite3_step(pStmt);
3222  /* if we have a result set... */
3223  if( SQLITE_ROW == rc ){
3224    /* allocate space for col name ptr, value ptr, and type */
3225    int nCol = sqlite3_column_count(pStmt);
3226    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3227    if( !pData ){
3228      rc = SQLITE_NOMEM;
3229    }else{
3230      char **azCols = (char **)pData;      /* Names of result columns */
3231      char **azVals = &azCols[nCol];       /* Results */
3232      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3233      int i, x;
3234      assert(sizeof(int) <= sizeof(char *));
3235      /* save off ptrs to column names */
3236      for(i=0; i<nCol; i++){
3237        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3238      }
3239      do{
3240        /* extract the data and data types */
3241        for(i=0; i<nCol; i++){
3242          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3243          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3244            azVals[i] = "";
3245          }else{
3246            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3247          }
3248          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3249            rc = SQLITE_NOMEM;
3250            break; /* from for */
3251          }
3252        } /* end for */
3253
3254        /* if data and types extracted successfully... */
3255        if( SQLITE_ROW == rc ){
3256          /* call the supplied callback with the result row data */
3257          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3258            rc = SQLITE_ABORT;
3259          }else{
3260            rc = sqlite3_step(pStmt);
3261          }
3262        }
3263      } while( SQLITE_ROW == rc );
3264      sqlite3_free(pData);
3265      if( pArg->cMode==MODE_Json ){
3266        fputs("]\n", pArg->out);
3267      }
3268    }
3269  }
3270}
3271
3272#ifndef SQLITE_OMIT_VIRTUALTABLE
3273/*
3274** This function is called to process SQL if the previous shell command
3275** was ".expert". It passes the SQL in the second argument directly to
3276** the sqlite3expert object.
3277**
3278** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3279** code. In this case, (*pzErr) may be set to point to a buffer containing
3280** an English language error message. It is the responsibility of the
3281** caller to eventually free this buffer using sqlite3_free().
3282*/
3283static int expertHandleSQL(
3284  ShellState *pState,
3285  const char *zSql,
3286  char **pzErr
3287){
3288  assert( pState->expert.pExpert );
3289  assert( pzErr==0 || *pzErr==0 );
3290  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3291}
3292
3293/*
3294** This function is called either to silently clean up the object
3295** created by the ".expert" command (if bCancel==1), or to generate a
3296** report from it and then clean it up (if bCancel==0).
3297**
3298** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3299** code. In this case, (*pzErr) may be set to point to a buffer containing
3300** an English language error message. It is the responsibility of the
3301** caller to eventually free this buffer using sqlite3_free().
3302*/
3303static int expertFinish(
3304  ShellState *pState,
3305  int bCancel,
3306  char **pzErr
3307){
3308  int rc = SQLITE_OK;
3309  sqlite3expert *p = pState->expert.pExpert;
3310  assert( p );
3311  assert( bCancel || pzErr==0 || *pzErr==0 );
3312  if( bCancel==0 ){
3313    FILE *out = pState->out;
3314    int bVerbose = pState->expert.bVerbose;
3315
3316    rc = sqlite3_expert_analyze(p, pzErr);
3317    if( rc==SQLITE_OK ){
3318      int nQuery = sqlite3_expert_count(p);
3319      int i;
3320
3321      if( bVerbose ){
3322        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3323        raw_printf(out, "-- Candidates -----------------------------\n");
3324        raw_printf(out, "%s\n", zCand);
3325      }
3326      for(i=0; i<nQuery; i++){
3327        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3328        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3329        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3330        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3331        if( bVerbose ){
3332          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3333          raw_printf(out, "%s\n\n", zSql);
3334        }
3335        raw_printf(out, "%s\n", zIdx);
3336        raw_printf(out, "%s\n", zEQP);
3337      }
3338    }
3339  }
3340  sqlite3_expert_destroy(p);
3341  pState->expert.pExpert = 0;
3342  return rc;
3343}
3344
3345/*
3346** Implementation of ".expert" dot command.
3347*/
3348static int expertDotCommand(
3349  ShellState *pState,             /* Current shell tool state */
3350  char **azArg,                   /* Array of arguments passed to dot command */
3351  int nArg                        /* Number of entries in azArg[] */
3352){
3353  int rc = SQLITE_OK;
3354  char *zErr = 0;
3355  int i;
3356  int iSample = 0;
3357
3358  assert( pState->expert.pExpert==0 );
3359  memset(&pState->expert, 0, sizeof(ExpertInfo));
3360
3361  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3362    char *z = azArg[i];
3363    int n;
3364    if( z[0]=='-' && z[1]=='-' ) z++;
3365    n = strlen30(z);
3366    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3367      pState->expert.bVerbose = 1;
3368    }
3369    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3370      if( i==(nArg-1) ){
3371        raw_printf(stderr, "option requires an argument: %s\n", z);
3372        rc = SQLITE_ERROR;
3373      }else{
3374        iSample = (int)integerValue(azArg[++i]);
3375        if( iSample<0 || iSample>100 ){
3376          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3377          rc = SQLITE_ERROR;
3378        }
3379      }
3380    }
3381    else{
3382      raw_printf(stderr, "unknown option: %s\n", z);
3383      rc = SQLITE_ERROR;
3384    }
3385  }
3386
3387  if( rc==SQLITE_OK ){
3388    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3389    if( pState->expert.pExpert==0 ){
3390      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3391      rc = SQLITE_ERROR;
3392    }else{
3393      sqlite3_expert_config(
3394          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3395      );
3396    }
3397  }
3398
3399  return rc;
3400}
3401#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3402
3403/*
3404** Execute a statement or set of statements.  Print
3405** any result rows/columns depending on the current mode
3406** set via the supplied callback.
3407**
3408** This is very similar to SQLite's built-in sqlite3_exec()
3409** function except it takes a slightly different callback
3410** and callback data argument.
3411*/
3412static int shell_exec(
3413  ShellState *pArg,                         /* Pointer to ShellState */
3414  const char *zSql,                         /* SQL to be evaluated */
3415  char **pzErrMsg                           /* Error msg written here */
3416){
3417  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3418  int rc = SQLITE_OK;             /* Return Code */
3419  int rc2;
3420  const char *zLeftover;          /* Tail of unprocessed SQL */
3421  sqlite3 *db = pArg->db;
3422
3423  if( pzErrMsg ){
3424    *pzErrMsg = NULL;
3425  }
3426
3427#ifndef SQLITE_OMIT_VIRTUALTABLE
3428  if( pArg->expert.pExpert ){
3429    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3430    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3431  }
3432#endif
3433
3434  while( zSql[0] && (SQLITE_OK == rc) ){
3435    static const char *zStmtSql;
3436    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3437    if( SQLITE_OK != rc ){
3438      if( pzErrMsg ){
3439        *pzErrMsg = save_err_msg(db);
3440      }
3441    }else{
3442      if( !pStmt ){
3443        /* this happens for a comment or white-space */
3444        zSql = zLeftover;
3445        while( IsSpace(zSql[0]) ) zSql++;
3446        continue;
3447      }
3448      zStmtSql = sqlite3_sql(pStmt);
3449      if( zStmtSql==0 ) zStmtSql = "";
3450      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3451
3452      /* save off the prepared statment handle and reset row count */
3453      if( pArg ){
3454        pArg->pStmt = pStmt;
3455        pArg->cnt = 0;
3456      }
3457
3458      /* echo the sql statement if echo on */
3459      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3460        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3461      }
3462
3463      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3464      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3465        sqlite3_stmt *pExplain;
3466        char *zEQP;
3467        int triggerEQP = 0;
3468        disable_debug_trace_modes();
3469        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3470        if( pArg->autoEQP>=AUTOEQP_trigger ){
3471          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3472        }
3473        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3474        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3475        if( rc==SQLITE_OK ){
3476          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3477            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3478            int iEqpId = sqlite3_column_int(pExplain, 0);
3479            int iParentId = sqlite3_column_int(pExplain, 1);
3480            if( zEQPLine==0 ) zEQPLine = "";
3481            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3482            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3483          }
3484          eqp_render(pArg);
3485        }
3486        sqlite3_finalize(pExplain);
3487        sqlite3_free(zEQP);
3488        if( pArg->autoEQP>=AUTOEQP_full ){
3489          /* Also do an EXPLAIN for ".eqp full" mode */
3490          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3491          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3492          if( rc==SQLITE_OK ){
3493            pArg->cMode = MODE_Explain;
3494            explain_data_prepare(pArg, pExplain);
3495            exec_prepared_stmt(pArg, pExplain);
3496            explain_data_delete(pArg);
3497          }
3498          sqlite3_finalize(pExplain);
3499          sqlite3_free(zEQP);
3500        }
3501        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3502          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3503          /* Reprepare pStmt before reactiving trace modes */
3504          sqlite3_finalize(pStmt);
3505          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3506          if( pArg ) pArg->pStmt = pStmt;
3507        }
3508        restore_debug_trace_modes();
3509      }
3510
3511      if( pArg ){
3512        pArg->cMode = pArg->mode;
3513        if( pArg->autoExplain ){
3514          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3515            pArg->cMode = MODE_Explain;
3516          }
3517          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3518            pArg->cMode = MODE_EQP;
3519          }
3520        }
3521
3522        /* If the shell is currently in ".explain" mode, gather the extra
3523        ** data required to add indents to the output.*/
3524        if( pArg->cMode==MODE_Explain ){
3525          explain_data_prepare(pArg, pStmt);
3526        }
3527      }
3528
3529      bind_prepared_stmt(pArg, pStmt);
3530      exec_prepared_stmt(pArg, pStmt);
3531      explain_data_delete(pArg);
3532      eqp_render(pArg);
3533
3534      /* print usage stats if stats on */
3535      if( pArg && pArg->statsOn ){
3536        display_stats(db, pArg, 0);
3537      }
3538
3539      /* print loop-counters if required */
3540      if( pArg && pArg->scanstatsOn ){
3541        display_scanstats(db, pArg);
3542      }
3543
3544      /* Finalize the statement just executed. If this fails, save a
3545      ** copy of the error message. Otherwise, set zSql to point to the
3546      ** next statement to execute. */
3547      rc2 = sqlite3_finalize(pStmt);
3548      if( rc!=SQLITE_NOMEM ) rc = rc2;
3549      if( rc==SQLITE_OK ){
3550        zSql = zLeftover;
3551        while( IsSpace(zSql[0]) ) zSql++;
3552      }else if( pzErrMsg ){
3553        *pzErrMsg = save_err_msg(db);
3554      }
3555
3556      /* clear saved stmt handle */
3557      if( pArg ){
3558        pArg->pStmt = NULL;
3559      }
3560    }
3561  } /* end while */
3562
3563  return rc;
3564}
3565
3566/*
3567** Release memory previously allocated by tableColumnList().
3568*/
3569static void freeColumnList(char **azCol){
3570  int i;
3571  for(i=1; azCol[i]; i++){
3572    sqlite3_free(azCol[i]);
3573  }
3574  /* azCol[0] is a static string */
3575  sqlite3_free(azCol);
3576}
3577
3578/*
3579** Return a list of pointers to strings which are the names of all
3580** columns in table zTab.   The memory to hold the names is dynamically
3581** allocated and must be released by the caller using a subsequent call
3582** to freeColumnList().
3583**
3584** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3585** value that needs to be preserved, then azCol[0] is filled in with the
3586** name of the rowid column.
3587**
3588** The first regular column in the table is azCol[1].  The list is terminated
3589** by an entry with azCol[i]==0.
3590*/
3591static char **tableColumnList(ShellState *p, const char *zTab){
3592  char **azCol = 0;
3593  sqlite3_stmt *pStmt;
3594  char *zSql;
3595  int nCol = 0;
3596  int nAlloc = 0;
3597  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3598  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3599  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3600  int rc;
3601
3602  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3603  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3604  sqlite3_free(zSql);
3605  if( rc ) return 0;
3606  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3607    if( nCol>=nAlloc-2 ){
3608      nAlloc = nAlloc*2 + nCol + 10;
3609      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3610      if( azCol==0 ) shell_out_of_memory();
3611    }
3612    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3613    if( sqlite3_column_int(pStmt, 5) ){
3614      nPK++;
3615      if( nPK==1
3616       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3617                          "INTEGER")==0
3618      ){
3619        isIPK = 1;
3620      }else{
3621        isIPK = 0;
3622      }
3623    }
3624  }
3625  sqlite3_finalize(pStmt);
3626  if( azCol==0 ) return 0;
3627  azCol[0] = 0;
3628  azCol[nCol+1] = 0;
3629
3630  /* The decision of whether or not a rowid really needs to be preserved
3631  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3632  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3633  ** rowids on tables where the rowid is inaccessible because there are other
3634  ** columns in the table named "rowid", "_rowid_", and "oid".
3635  */
3636  if( preserveRowid && isIPK ){
3637    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3638    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3639    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3640    ** ROWID aliases.  To distinguish these cases, check to see if
3641    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3642    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3643    */
3644    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3645                           " WHERE origin='pk'", zTab);
3646    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3647    sqlite3_free(zSql);
3648    if( rc ){
3649      freeColumnList(azCol);
3650      return 0;
3651    }
3652    rc = sqlite3_step(pStmt);
3653    sqlite3_finalize(pStmt);
3654    preserveRowid = rc==SQLITE_ROW;
3655  }
3656  if( preserveRowid ){
3657    /* Only preserve the rowid if we can find a name to use for the
3658    ** rowid */
3659    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3660    int i, j;
3661    for(j=0; j<3; j++){
3662      for(i=1; i<=nCol; i++){
3663        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3664      }
3665      if( i>nCol ){
3666        /* At this point, we know that azRowid[j] is not the name of any
3667        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3668        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3669        ** tables will fail this last check */
3670        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3671        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3672        break;
3673      }
3674    }
3675  }
3676  return azCol;
3677}
3678
3679/*
3680** Toggle the reverse_unordered_selects setting.
3681*/
3682static void toggleSelectOrder(sqlite3 *db){
3683  sqlite3_stmt *pStmt = 0;
3684  int iSetting = 0;
3685  char zStmt[100];
3686  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3687  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3688    iSetting = sqlite3_column_int(pStmt, 0);
3689  }
3690  sqlite3_finalize(pStmt);
3691  sqlite3_snprintf(sizeof(zStmt), zStmt,
3692       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3693  sqlite3_exec(db, zStmt, 0, 0, 0);
3694}
3695
3696/*
3697** This is a different callback routine used for dumping the database.
3698** Each row received by this callback consists of a table name,
3699** the table type ("index" or "table") and SQL to create the table.
3700** This routine should print text sufficient to recreate the table.
3701*/
3702static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3703  int rc;
3704  const char *zTable;
3705  const char *zType;
3706  const char *zSql;
3707  ShellState *p = (ShellState *)pArg;
3708
3709  UNUSED_PARAMETER(azNotUsed);
3710  if( nArg!=3 || azArg==0 ) return 0;
3711  zTable = azArg[0];
3712  zType = azArg[1];
3713  zSql = azArg[2];
3714
3715  if( strcmp(zTable, "sqlite_sequence")==0 ){
3716    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3717  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3718    raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3719  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3720    return 0;
3721  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3722    char *zIns;
3723    if( !p->writableSchema ){
3724      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3725      p->writableSchema = 1;
3726    }
3727    zIns = sqlite3_mprintf(
3728       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3729       "VALUES('table','%q','%q',0,'%q');",
3730       zTable, zTable, zSql);
3731    utf8_printf(p->out, "%s\n", zIns);
3732    sqlite3_free(zIns);
3733    return 0;
3734  }else{
3735    printSchemaLine(p->out, zSql, ";\n");
3736  }
3737
3738  if( strcmp(zType, "table")==0 ){
3739    ShellText sSelect;
3740    ShellText sTable;
3741    char **azCol;
3742    int i;
3743    char *savedDestTable;
3744    int savedMode;
3745
3746    azCol = tableColumnList(p, zTable);
3747    if( azCol==0 ){
3748      p->nErr++;
3749      return 0;
3750    }
3751
3752    /* Always quote the table name, even if it appears to be pure ascii,
3753    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3754    initText(&sTable);
3755    appendText(&sTable, zTable, quoteChar(zTable));
3756    /* If preserving the rowid, add a column list after the table name.
3757    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3758    ** instead of the usual "INSERT INTO tab VALUES(...)".
3759    */
3760    if( azCol[0] ){
3761      appendText(&sTable, "(", 0);
3762      appendText(&sTable, azCol[0], 0);
3763      for(i=1; azCol[i]; i++){
3764        appendText(&sTable, ",", 0);
3765        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3766      }
3767      appendText(&sTable, ")", 0);
3768    }
3769
3770    /* Build an appropriate SELECT statement */
3771    initText(&sSelect);
3772    appendText(&sSelect, "SELECT ", 0);
3773    if( azCol[0] ){
3774      appendText(&sSelect, azCol[0], 0);
3775      appendText(&sSelect, ",", 0);
3776    }
3777    for(i=1; azCol[i]; i++){
3778      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3779      if( azCol[i+1] ){
3780        appendText(&sSelect, ",", 0);
3781      }
3782    }
3783    freeColumnList(azCol);
3784    appendText(&sSelect, " FROM ", 0);
3785    appendText(&sSelect, zTable, quoteChar(zTable));
3786
3787    savedDestTable = p->zDestTable;
3788    savedMode = p->mode;
3789    p->zDestTable = sTable.z;
3790    p->mode = p->cMode = MODE_Insert;
3791    rc = shell_exec(p, sSelect.z, 0);
3792    if( (rc&0xff)==SQLITE_CORRUPT ){
3793      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3794      toggleSelectOrder(p->db);
3795      shell_exec(p, sSelect.z, 0);
3796      toggleSelectOrder(p->db);
3797    }
3798    p->zDestTable = savedDestTable;
3799    p->mode = savedMode;
3800    freeText(&sTable);
3801    freeText(&sSelect);
3802    if( rc ) p->nErr++;
3803  }
3804  return 0;
3805}
3806
3807/*
3808** Run zQuery.  Use dump_callback() as the callback routine so that
3809** the contents of the query are output as SQL statements.
3810**
3811** If we get a SQLITE_CORRUPT error, rerun the query after appending
3812** "ORDER BY rowid DESC" to the end.
3813*/
3814static int run_schema_dump_query(
3815  ShellState *p,
3816  const char *zQuery
3817){
3818  int rc;
3819  char *zErr = 0;
3820  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3821  if( rc==SQLITE_CORRUPT ){
3822    char *zQ2;
3823    int len = strlen30(zQuery);
3824    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3825    if( zErr ){
3826      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3827      sqlite3_free(zErr);
3828      zErr = 0;
3829    }
3830    zQ2 = malloc( len+100 );
3831    if( zQ2==0 ) return rc;
3832    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3833    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3834    if( rc ){
3835      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3836    }else{
3837      rc = SQLITE_CORRUPT;
3838    }
3839    sqlite3_free(zErr);
3840    free(zQ2);
3841  }
3842  return rc;
3843}
3844
3845/*
3846** Text of help messages.
3847**
3848** The help text for each individual command begins with a line that starts
3849** with ".".  Subsequent lines are supplimental information.
3850**
3851** There must be two or more spaces between the end of the command and the
3852** start of the description of what that command does.
3853*/
3854static const char *(azHelp[]) = {
3855#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3856  ".archive ...             Manage SQL archives",
3857  "   Each command must have exactly one of the following options:",
3858  "     -c, --create               Create a new archive",
3859  "     -u, --update               Add or update files with changed mtime",
3860  "     -i, --insert               Like -u but always add even if unchanged",
3861  "     -t, --list                 List contents of archive",
3862  "     -x, --extract              Extract files from archive",
3863  "   Optional arguments:",
3864  "     -v, --verbose              Print each filename as it is processed",
3865  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3866  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3867  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3868  "     -n, --dryrun               Show the SQL that would have occurred",
3869  "   Examples:",
3870  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3871  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3872  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3873  "   See also:",
3874  "      http://sqlite.org/cli.html#sqlar_archive_support",
3875#endif
3876#ifndef SQLITE_OMIT_AUTHORIZATION
3877  ".auth ON|OFF             Show authorizer callbacks",
3878#endif
3879  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3880  "       --append            Use the appendvfs",
3881  "       --async             Write to FILE without journal and fsync()",
3882  ".bail on|off             Stop after hitting an error.  Default OFF",
3883  ".binary on|off           Turn binary output on or off.  Default OFF",
3884  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3885  ".changes on|off          Show number of rows changed by SQL",
3886  ".check GLOB              Fail if output since .testcase does not match",
3887  ".clone NEWDB             Clone data into NEWDB from the existing database",
3888  ".databases               List names and files of attached databases",
3889  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3890  ".dbinfo ?DB?             Show status information about the database",
3891  ".dump ?TABLE?            Render database content as SQL",
3892  "   Options:",
3893  "     --preserve-rowids      Include ROWID values in the output",
3894  "     --newlines             Allow unescaped newline characters in output",
3895  "   TABLE is a LIKE pattern for the tables to dump",
3896  "   Additional LIKE patterns can be given in subsequent arguments",
3897  ".echo on|off             Turn command echo on or off",
3898  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3899  "   Other Modes:",
3900#ifdef SQLITE_DEBUG
3901  "      test                  Show raw EXPLAIN QUERY PLAN output",
3902  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3903#endif
3904  "      trigger               Like \"full\" but also show trigger bytecode",
3905  ".excel                   Display the output of next command in spreadsheet",
3906  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3907  ".exit ?CODE?             Exit this program with return-code CODE",
3908  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3909  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3910  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3911  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3912  "   --help                  Show CMD details",
3913  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3914  ".headers on|off          Turn display of headers on or off",
3915  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3916  ".import FILE TABLE       Import data from FILE into TABLE",
3917  "   Options:",
3918  "     --ascii               Use \\037 and \\036 as column and row separators",
3919  "     --csv                 Use , and \\n as column and row separators",
3920  "     --skip N              Skip the first N rows of input",
3921  "     -v                    \"Verbose\" - increase auxiliary output",
3922  "   Notes:",
3923  "     *  If TABLE does not exist, it is created.  The first row of input",
3924  "        determines the column names.",
3925  "     *  If neither --csv or --ascii are used, the input mode is derived",
3926  "        from the \".mode\" output mode",
3927  "     *  If FILE begins with \"|\" then it is a command that generates the",
3928  "        input text.",
3929#ifndef SQLITE_OMIT_TEST_CONTROL
3930  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3931#endif
3932  ".indexes ?TABLE?         Show names of indexes",
3933  "                           If TABLE is specified, only show indexes for",
3934  "                           tables matching TABLE using the LIKE operator.",
3935#ifdef SQLITE_ENABLE_IOTRACE
3936  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3937#endif
3938  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3939  ".lint OPTIONS            Report potential schema issues.",
3940  "     Options:",
3941  "        fkey-indexes     Find missing foreign key indexes",
3942#ifndef SQLITE_OMIT_LOAD_EXTENSION
3943  ".load FILE ?ENTRY?       Load an extension library",
3944#endif
3945  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3946  ".mode MODE ?TABLE?       Set output mode",
3947  "   MODE is one of:",
3948  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3949  "     box       Tables using unicode box-drawing characters",
3950  "     csv       Comma-separated values",
3951  "     column    Output in columns.  (See .width)",
3952  "     html      HTML <table> code",
3953  "     insert    SQL insert statements for TABLE",
3954  "     json      Results in a JSON array",
3955  "     line      One value per line",
3956  "     list      Values delimited by \"|\"",
3957  "     markdown  Markdown table format",
3958  "     quote     Escape answers as for SQL",
3959  "     table     ASCII-art table",
3960  "     tabs      Tab-separated values",
3961  "     tcl       TCL list elements",
3962  ".nullvalue STRING        Use STRING in place of NULL values",
3963  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3964  "     If FILE begins with '|' then open as a pipe",
3965  "       --bom  Put a UTF8 byte-order mark at the beginning",
3966  "       -e     Send output to the system text editor",
3967  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3968#ifdef SQLITE_DEBUG
3969  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3970#endif
3971  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3972  "     Options:",
3973  "        --append        Use appendvfs to append database to the end of FILE",
3974#ifdef SQLITE_ENABLE_DESERIALIZE
3975  "        --deserialize   Load into memory useing sqlite3_deserialize()",
3976  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3977  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3978#endif
3979  "        --new           Initialize FILE to an empty database",
3980  "        --nofollow      Do not follow symbolic links",
3981  "        --readonly      Open FILE readonly",
3982  "        --zip           FILE is a ZIP archive",
3983  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3984  "   If FILE begins with '|' then open it as a pipe.",
3985  "   Options:",
3986  "     --bom                 Prefix output with a UTF8 byte-order mark",
3987  "     -e                    Send output to the system text editor",
3988  "     -x                    Send output as CSV to a spreadsheet",
3989  ".parameter CMD ...       Manage SQL parameter bindings",
3990  "   clear                   Erase all bindings",
3991  "   init                    Initialize the TEMP table that holds bindings",
3992  "   list                    List the current parameter bindings",
3993  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
3994  "                           PARAMETER should start with one of: $ : @ ?",
3995  "   unset PARAMETER         Remove PARAMETER from the binding table",
3996  ".print STRING...         Print literal STRING",
3997#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3998  ".progress N              Invoke progress handler after every N opcodes",
3999  "   --limit N                 Interrupt after N progress callbacks",
4000  "   --once                    Do no more than one progress interrupt",
4001  "   --quiet|-q                No output except at interrupts",
4002  "   --reset                   Reset the count for each input and interrupt",
4003#endif
4004  ".prompt MAIN CONTINUE    Replace the standard prompts",
4005  ".quit                    Exit this program",
4006  ".read FILE               Read input from FILE",
4007#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4008  ".recover                 Recover as much data as possible from corrupt db.",
4009  "   --freelist-corrupt       Assume the freelist is corrupt",
4010  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4011  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4012  "   --no-rowids              Do not attempt to recover rowid values",
4013  "                            that are not also INTEGER PRIMARY KEYs",
4014#endif
4015  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4016  ".save FILE               Write in-memory database into FILE",
4017  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4018  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4019  "     Options:",
4020  "         --indent            Try to pretty-print the schema",
4021  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4022  "    Options:",
4023  "       --init               Create a new SELFTEST table",
4024  "       -v                   Verbose output",
4025  ".separator COL ?ROW?     Change the column and row separators",
4026#if defined(SQLITE_ENABLE_SESSION)
4027  ".session ?NAME? CMD ...  Create or control sessions",
4028  "   Subcommands:",
4029  "     attach TABLE             Attach TABLE",
4030  "     changeset FILE           Write a changeset into FILE",
4031  "     close                    Close one session",
4032  "     enable ?BOOLEAN?         Set or query the enable bit",
4033  "     filter GLOB...           Reject tables matching GLOBs",
4034  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4035  "     isempty                  Query whether the session is empty",
4036  "     list                     List currently open session names",
4037  "     open DB NAME             Open a new session on DB",
4038  "     patchset FILE            Write a patchset into FILE",
4039  "   If ?NAME? is omitted, the first defined session is used.",
4040#endif
4041  ".sha3sum ...             Compute a SHA3 hash of database content",
4042  "    Options:",
4043  "      --schema              Also hash the sqlite_schema table",
4044  "      --sha3-224            Use the sha3-224 algorithm",
4045  "      --sha3-256            Use the sha3-256 algorithm (default)",
4046  "      --sha3-384            Use the sha3-384 algorithm",
4047  "      --sha3-512            Use the sha3-512 algorithm",
4048  "    Any other argument is a LIKE pattern for tables to hash",
4049#ifndef SQLITE_NOHAVE_SYSTEM
4050  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4051#endif
4052  ".show                    Show the current values for various settings",
4053  ".stats ?on|off?          Show stats or turn stats on or off",
4054#ifndef SQLITE_NOHAVE_SYSTEM
4055  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4056#endif
4057  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4058  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4059  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4060  "                           Run \".testctrl\" with no arguments for details",
4061  ".timeout MS              Try opening locked tables for MS milliseconds",
4062  ".timer on|off            Turn SQL timer on or off",
4063#ifndef SQLITE_OMIT_TRACE
4064  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4065  "    FILE                    Send output to FILE",
4066  "    stdout                  Send output to stdout",
4067  "    stderr                  Send output to stderr",
4068  "    off                     Disable tracing",
4069  "    --expanded              Expand query parameters",
4070#ifdef SQLITE_ENABLE_NORMALIZE
4071  "    --normalized            Normal the SQL statements",
4072#endif
4073  "    --plain                 Show SQL as it is input",
4074  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4075  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4076  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4077  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4078#endif /* SQLITE_OMIT_TRACE */
4079#ifdef SQLITE_DEBUG
4080  ".unmodule NAME ...       Unregister virtual table modules",
4081  "    --allexcept             Unregister everything except those named",
4082#endif
4083  ".vfsinfo ?AUX?           Information about the top-level VFS",
4084  ".vfslist                 List all available VFSes",
4085  ".vfsname ?AUX?           Print the name of the VFS stack",
4086  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4087  "     Negative values right-justify",
4088};
4089
4090/*
4091** Output help text.
4092**
4093** zPattern describes the set of commands for which help text is provided.
4094** If zPattern is NULL, then show all commands, but only give a one-line
4095** description of each.
4096**
4097** Return the number of matches.
4098*/
4099static int showHelp(FILE *out, const char *zPattern){
4100  int i = 0;
4101  int j = 0;
4102  int n = 0;
4103  char *zPat;
4104  if( zPattern==0
4105   || zPattern[0]=='0'
4106   || strcmp(zPattern,"-a")==0
4107   || strcmp(zPattern,"-all")==0
4108   || strcmp(zPattern,"--all")==0
4109  ){
4110    /* Show all commands, but only one line per command */
4111    if( zPattern==0 ) zPattern = "";
4112    for(i=0; i<ArraySize(azHelp); i++){
4113      if( azHelp[i][0]=='.' || zPattern[0] ){
4114        utf8_printf(out, "%s\n", azHelp[i]);
4115        n++;
4116      }
4117    }
4118  }else{
4119    /* Look for commands that for which zPattern is an exact prefix */
4120    zPat = sqlite3_mprintf(".%s*", zPattern);
4121    for(i=0; i<ArraySize(azHelp); i++){
4122      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4123        utf8_printf(out, "%s\n", azHelp[i]);
4124        j = i+1;
4125        n++;
4126      }
4127    }
4128    sqlite3_free(zPat);
4129    if( n ){
4130      if( n==1 ){
4131        /* when zPattern is a prefix of exactly one command, then include the
4132        ** details of that command, which should begin at offset j */
4133        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4134          utf8_printf(out, "%s\n", azHelp[j]);
4135          j++;
4136        }
4137      }
4138      return n;
4139    }
4140    /* Look for commands that contain zPattern anywhere.  Show the complete
4141    ** text of all commands that match. */
4142    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4143    for(i=0; i<ArraySize(azHelp); i++){
4144      if( azHelp[i][0]=='.' ) j = i;
4145      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4146        utf8_printf(out, "%s\n", azHelp[j]);
4147        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4148          j++;
4149          utf8_printf(out, "%s\n", azHelp[j]);
4150        }
4151        i = j;
4152        n++;
4153      }
4154    }
4155    sqlite3_free(zPat);
4156  }
4157  return n;
4158}
4159
4160/* Forward reference */
4161static int process_input(ShellState *p);
4162
4163/*
4164** Read the content of file zName into memory obtained from sqlite3_malloc64()
4165** and return a pointer to the buffer. The caller is responsible for freeing
4166** the memory.
4167**
4168** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4169** read.
4170**
4171** For convenience, a nul-terminator byte is always appended to the data read
4172** from the file before the buffer is returned. This byte is not included in
4173** the final value of (*pnByte), if applicable.
4174**
4175** NULL is returned if any error is encountered. The final value of *pnByte
4176** is undefined in this case.
4177*/
4178static char *readFile(const char *zName, int *pnByte){
4179  FILE *in = fopen(zName, "rb");
4180  long nIn;
4181  size_t nRead;
4182  char *pBuf;
4183  if( in==0 ) return 0;
4184  fseek(in, 0, SEEK_END);
4185  nIn = ftell(in);
4186  rewind(in);
4187  pBuf = sqlite3_malloc64( nIn+1 );
4188  if( pBuf==0 ){ fclose(in); return 0; }
4189  nRead = fread(pBuf, nIn, 1, in);
4190  fclose(in);
4191  if( nRead!=1 ){
4192    sqlite3_free(pBuf);
4193    return 0;
4194  }
4195  pBuf[nIn] = 0;
4196  if( pnByte ) *pnByte = nIn;
4197  return pBuf;
4198}
4199
4200#if defined(SQLITE_ENABLE_SESSION)
4201/*
4202** Close a single OpenSession object and release all of its associated
4203** resources.
4204*/
4205static void session_close(OpenSession *pSession){
4206  int i;
4207  sqlite3session_delete(pSession->p);
4208  sqlite3_free(pSession->zName);
4209  for(i=0; i<pSession->nFilter; i++){
4210    sqlite3_free(pSession->azFilter[i]);
4211  }
4212  sqlite3_free(pSession->azFilter);
4213  memset(pSession, 0, sizeof(OpenSession));
4214}
4215#endif
4216
4217/*
4218** Close all OpenSession objects and release all associated resources.
4219*/
4220#if defined(SQLITE_ENABLE_SESSION)
4221static void session_close_all(ShellState *p){
4222  int i;
4223  for(i=0; i<p->nSession; i++){
4224    session_close(&p->aSession[i]);
4225  }
4226  p->nSession = 0;
4227}
4228#else
4229# define session_close_all(X)
4230#endif
4231
4232/*
4233** Implementation of the xFilter function for an open session.  Omit
4234** any tables named by ".session filter" but let all other table through.
4235*/
4236#if defined(SQLITE_ENABLE_SESSION)
4237static int session_filter(void *pCtx, const char *zTab){
4238  OpenSession *pSession = (OpenSession*)pCtx;
4239  int i;
4240  for(i=0; i<pSession->nFilter; i++){
4241    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4242  }
4243  return 1;
4244}
4245#endif
4246
4247/*
4248** Try to deduce the type of file for zName based on its content.  Return
4249** one of the SHELL_OPEN_* constants.
4250**
4251** If the file does not exist or is empty but its name looks like a ZIP
4252** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4253** Otherwise, assume an ordinary database regardless of the filename if
4254** the type cannot be determined from content.
4255*/
4256int deduceDatabaseType(const char *zName, int dfltZip){
4257  FILE *f = fopen(zName, "rb");
4258  size_t n;
4259  int rc = SHELL_OPEN_UNSPEC;
4260  char zBuf[100];
4261  if( f==0 ){
4262    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4263       return SHELL_OPEN_ZIPFILE;
4264    }else{
4265       return SHELL_OPEN_NORMAL;
4266    }
4267  }
4268  n = fread(zBuf, 16, 1, f);
4269  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4270    fclose(f);
4271    return SHELL_OPEN_NORMAL;
4272  }
4273  fseek(f, -25, SEEK_END);
4274  n = fread(zBuf, 25, 1, f);
4275  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4276    rc = SHELL_OPEN_APPENDVFS;
4277  }else{
4278    fseek(f, -22, SEEK_END);
4279    n = fread(zBuf, 22, 1, f);
4280    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4281       && zBuf[3]==0x06 ){
4282      rc = SHELL_OPEN_ZIPFILE;
4283    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4284      rc = SHELL_OPEN_ZIPFILE;
4285    }
4286  }
4287  fclose(f);
4288  return rc;
4289}
4290
4291#ifdef SQLITE_ENABLE_DESERIALIZE
4292/*
4293** Reconstruct an in-memory database using the output from the "dbtotxt"
4294** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4295** is 0, then read from standard input.
4296*/
4297static unsigned char *readHexDb(ShellState *p, int *pnData){
4298  unsigned char *a = 0;
4299  int nLine;
4300  int n = 0;
4301  int pgsz = 0;
4302  int iOffset = 0;
4303  int j, k;
4304  int rc;
4305  FILE *in;
4306  unsigned int x[16];
4307  char zLine[1000];
4308  if( p->zDbFilename ){
4309    in = fopen(p->zDbFilename, "r");
4310    if( in==0 ){
4311      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4312      return 0;
4313    }
4314    nLine = 0;
4315  }else{
4316    in = p->in;
4317    nLine = p->lineno;
4318    if( in==0 ) in = stdin;
4319  }
4320  *pnData = 0;
4321  nLine++;
4322  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4323  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4324  if( rc!=2 ) goto readHexDb_error;
4325  if( n<0 ) goto readHexDb_error;
4326  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4327  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4328  a = sqlite3_malloc( n ? n : 1 );
4329  if( a==0 ){
4330    utf8_printf(stderr, "Out of memory!\n");
4331    goto readHexDb_error;
4332  }
4333  memset(a, 0, n);
4334  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4335    utf8_printf(stderr, "invalid pagesize\n");
4336    goto readHexDb_error;
4337  }
4338  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4339    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4340    if( rc==2 ){
4341      iOffset = k;
4342      continue;
4343    }
4344    if( strncmp(zLine, "| end ", 6)==0 ){
4345      break;
4346    }
4347    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4348                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4349                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4350    if( rc==17 ){
4351      k = iOffset+j;
4352      if( k+16<=n ){
4353        int ii;
4354        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4355      }
4356    }
4357  }
4358  *pnData = n;
4359  if( in!=p->in ){
4360    fclose(in);
4361  }else{
4362    p->lineno = nLine;
4363  }
4364  return a;
4365
4366readHexDb_error:
4367  if( in!=p->in ){
4368    fclose(in);
4369  }else{
4370    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4371      nLine++;
4372      if(strncmp(zLine, "| end ", 6)==0 ) break;
4373    }
4374    p->lineno = nLine;
4375  }
4376  sqlite3_free(a);
4377  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4378  return 0;
4379}
4380#endif /* SQLITE_ENABLE_DESERIALIZE */
4381
4382/*
4383** Scalar function "shell_int32". The first argument to this function
4384** must be a blob. The second a non-negative integer. This function
4385** reads and returns a 32-bit big-endian integer from byte
4386** offset (4*<arg2>) of the blob.
4387*/
4388static void shellInt32(
4389  sqlite3_context *context,
4390  int argc,
4391  sqlite3_value **argv
4392){
4393  const unsigned char *pBlob;
4394  int nBlob;
4395  int iInt;
4396
4397  UNUSED_PARAMETER(argc);
4398  nBlob = sqlite3_value_bytes(argv[0]);
4399  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4400  iInt = sqlite3_value_int(argv[1]);
4401
4402  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4403    const unsigned char *a = &pBlob[iInt*4];
4404    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4405                       + ((sqlite3_int64)a[1]<<16)
4406                       + ((sqlite3_int64)a[2]<< 8)
4407                       + ((sqlite3_int64)a[3]<< 0);
4408    sqlite3_result_int64(context, iVal);
4409  }
4410}
4411
4412/*
4413** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4414** using "..." with internal double-quote characters doubled.
4415*/
4416static void shellIdQuote(
4417  sqlite3_context *context,
4418  int argc,
4419  sqlite3_value **argv
4420){
4421  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4422  UNUSED_PARAMETER(argc);
4423  if( zName ){
4424    char *z = sqlite3_mprintf("\"%w\"", zName);
4425    sqlite3_result_text(context, z, -1, sqlite3_free);
4426  }
4427}
4428
4429/*
4430** Scalar function "shell_escape_crnl" used by the .recover command.
4431** The argument passed to this function is the output of built-in
4432** function quote(). If the first character of the input is "'",
4433** indicating that the value passed to quote() was a text value,
4434** then this function searches the input for "\n" and "\r" characters
4435** and adds a wrapper similar to the following:
4436**
4437**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4438**
4439** Or, if the first character of the input is not "'", then a copy
4440** of the input is returned.
4441*/
4442static void shellEscapeCrnl(
4443  sqlite3_context *context,
4444  int argc,
4445  sqlite3_value **argv
4446){
4447  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4448  UNUSED_PARAMETER(argc);
4449  if( zText[0]=='\'' ){
4450    int nText = sqlite3_value_bytes(argv[0]);
4451    int i;
4452    char zBuf1[20];
4453    char zBuf2[20];
4454    const char *zNL = 0;
4455    const char *zCR = 0;
4456    int nCR = 0;
4457    int nNL = 0;
4458
4459    for(i=0; zText[i]; i++){
4460      if( zNL==0 && zText[i]=='\n' ){
4461        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4462        nNL = (int)strlen(zNL);
4463      }
4464      if( zCR==0 && zText[i]=='\r' ){
4465        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4466        nCR = (int)strlen(zCR);
4467      }
4468    }
4469
4470    if( zNL || zCR ){
4471      int iOut = 0;
4472      i64 nMax = (nNL > nCR) ? nNL : nCR;
4473      i64 nAlloc = nMax * nText + (nMax+64)*2;
4474      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4475      if( zOut==0 ){
4476        sqlite3_result_error_nomem(context);
4477        return;
4478      }
4479
4480      if( zNL && zCR ){
4481        memcpy(&zOut[iOut], "replace(replace(", 16);
4482        iOut += 16;
4483      }else{
4484        memcpy(&zOut[iOut], "replace(", 8);
4485        iOut += 8;
4486      }
4487      for(i=0; zText[i]; i++){
4488        if( zText[i]=='\n' ){
4489          memcpy(&zOut[iOut], zNL, nNL);
4490          iOut += nNL;
4491        }else if( zText[i]=='\r' ){
4492          memcpy(&zOut[iOut], zCR, nCR);
4493          iOut += nCR;
4494        }else{
4495          zOut[iOut] = zText[i];
4496          iOut++;
4497        }
4498      }
4499
4500      if( zNL ){
4501        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4502        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4503        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4504      }
4505      if( zCR ){
4506        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4507        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4508        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4509      }
4510
4511      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4512      sqlite3_free(zOut);
4513      return;
4514    }
4515  }
4516
4517  sqlite3_result_value(context, argv[0]);
4518}
4519
4520/* Flags for open_db().
4521**
4522** The default behavior of open_db() is to exit(1) if the database fails to
4523** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4524** but still returns without calling exit.
4525**
4526** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4527** ZIP archive if the file does not exist or is empty and its name matches
4528** the *.zip pattern.
4529*/
4530#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4531#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4532
4533/*
4534** Make sure the database is open.  If it is not, then open it.  If
4535** the database fails to open, print an error message and exit.
4536*/
4537static void open_db(ShellState *p, int openFlags){
4538  if( p->db==0 ){
4539    if( p->openMode==SHELL_OPEN_UNSPEC ){
4540      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4541        p->openMode = SHELL_OPEN_NORMAL;
4542      }else{
4543        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4544                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4545      }
4546    }
4547    switch( p->openMode ){
4548      case SHELL_OPEN_APPENDVFS: {
4549        sqlite3_open_v2(p->zDbFilename, &p->db,
4550           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4551        break;
4552      }
4553      case SHELL_OPEN_HEXDB:
4554      case SHELL_OPEN_DESERIALIZE: {
4555        sqlite3_open(0, &p->db);
4556        break;
4557      }
4558      case SHELL_OPEN_ZIPFILE: {
4559        sqlite3_open(":memory:", &p->db);
4560        break;
4561      }
4562      case SHELL_OPEN_READONLY: {
4563        sqlite3_open_v2(p->zDbFilename, &p->db,
4564            SQLITE_OPEN_READONLY|p->openFlags, 0);
4565        break;
4566      }
4567      case SHELL_OPEN_UNSPEC:
4568      case SHELL_OPEN_NORMAL: {
4569        sqlite3_open_v2(p->zDbFilename, &p->db,
4570           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4571        break;
4572      }
4573    }
4574    globalDb = p->db;
4575    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4576      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4577          p->zDbFilename, sqlite3_errmsg(p->db));
4578      if( openFlags & OPEN_DB_KEEPALIVE ){
4579        sqlite3_open(":memory:", &p->db);
4580        return;
4581      }
4582      exit(1);
4583    }
4584#ifndef SQLITE_OMIT_LOAD_EXTENSION
4585    sqlite3_enable_load_extension(p->db, 1);
4586#endif
4587    sqlite3_fileio_init(p->db, 0, 0);
4588    sqlite3_shathree_init(p->db, 0, 0);
4589    sqlite3_completion_init(p->db, 0, 0);
4590    sqlite3_uint_init(p->db, 0, 0);
4591    sqlite3_decimal_init(p->db, 0, 0);
4592    sqlite3_ieee_init(p->db, 0, 0);
4593#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4594    sqlite3_dbdata_init(p->db, 0, 0);
4595#endif
4596#ifdef SQLITE_HAVE_ZLIB
4597    sqlite3_zipfile_init(p->db, 0, 0);
4598    sqlite3_sqlar_init(p->db, 0, 0);
4599#endif
4600    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4601                            shellAddSchemaName, 0, 0);
4602    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4603                            shellModuleSchema, 0, 0);
4604    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4605                            shellPutsFunc, 0, 0);
4606    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4607                            shellEscapeCrnl, 0, 0);
4608    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4609                            shellInt32, 0, 0);
4610    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4611                            shellIdQuote, 0, 0);
4612#ifndef SQLITE_NOHAVE_SYSTEM
4613    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4614                            editFunc, 0, 0);
4615    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4616                            editFunc, 0, 0);
4617#endif
4618    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4619      char *zSql = sqlite3_mprintf(
4620         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4621      sqlite3_exec(p->db, zSql, 0, 0, 0);
4622      sqlite3_free(zSql);
4623    }
4624#ifdef SQLITE_ENABLE_DESERIALIZE
4625    else
4626    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4627      int rc;
4628      int nData = 0;
4629      unsigned char *aData;
4630      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4631        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4632      }else{
4633        aData = readHexDb(p, &nData);
4634        if( aData==0 ){
4635          return;
4636        }
4637      }
4638      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4639                   SQLITE_DESERIALIZE_RESIZEABLE |
4640                   SQLITE_DESERIALIZE_FREEONCLOSE);
4641      if( rc ){
4642        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4643      }
4644      if( p->szMax>0 ){
4645        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4646      }
4647    }
4648#endif
4649  }
4650}
4651
4652/*
4653** Attempt to close the databaes connection.  Report errors.
4654*/
4655void close_db(sqlite3 *db){
4656  int rc = sqlite3_close(db);
4657  if( rc ){
4658    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4659        rc, sqlite3_errmsg(db));
4660  }
4661}
4662
4663#if HAVE_READLINE || HAVE_EDITLINE
4664/*
4665** Readline completion callbacks
4666*/
4667static char *readline_completion_generator(const char *text, int state){
4668  static sqlite3_stmt *pStmt = 0;
4669  char *zRet;
4670  if( state==0 ){
4671    char *zSql;
4672    sqlite3_finalize(pStmt);
4673    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4674                           "  FROM completion(%Q) ORDER BY 1", text);
4675    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4676    sqlite3_free(zSql);
4677  }
4678  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4679    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4680  }else{
4681    sqlite3_finalize(pStmt);
4682    pStmt = 0;
4683    zRet = 0;
4684  }
4685  return zRet;
4686}
4687static char **readline_completion(const char *zText, int iStart, int iEnd){
4688  rl_attempted_completion_over = 1;
4689  return rl_completion_matches(zText, readline_completion_generator);
4690}
4691
4692#elif HAVE_LINENOISE
4693/*
4694** Linenoise completion callback
4695*/
4696static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4697  int nLine = strlen30(zLine);
4698  int i, iStart;
4699  sqlite3_stmt *pStmt = 0;
4700  char *zSql;
4701  char zBuf[1000];
4702
4703  if( nLine>sizeof(zBuf)-30 ) return;
4704  if( zLine[0]=='.' || zLine[0]=='#') return;
4705  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4706  if( i==nLine-1 ) return;
4707  iStart = i+1;
4708  memcpy(zBuf, zLine, iStart);
4709  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4710                         "  FROM completion(%Q,%Q) ORDER BY 1",
4711                         &zLine[iStart], zLine);
4712  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4713  sqlite3_free(zSql);
4714  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4715  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4716    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4717    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4718    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4719      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4720      linenoiseAddCompletion(lc, zBuf);
4721    }
4722  }
4723  sqlite3_finalize(pStmt);
4724}
4725#endif
4726
4727/*
4728** Do C-language style dequoting.
4729**
4730**    \a    -> alarm
4731**    \b    -> backspace
4732**    \t    -> tab
4733**    \n    -> newline
4734**    \v    -> vertical tab
4735**    \f    -> form feed
4736**    \r    -> carriage return
4737**    \s    -> space
4738**    \"    -> "
4739**    \'    -> '
4740**    \\    -> backslash
4741**    \NNN  -> ascii character NNN in octal
4742*/
4743static void resolve_backslashes(char *z){
4744  int i, j;
4745  char c;
4746  while( *z && *z!='\\' ) z++;
4747  for(i=j=0; (c = z[i])!=0; i++, j++){
4748    if( c=='\\' && z[i+1]!=0 ){
4749      c = z[++i];
4750      if( c=='a' ){
4751        c = '\a';
4752      }else if( c=='b' ){
4753        c = '\b';
4754      }else if( c=='t' ){
4755        c = '\t';
4756      }else if( c=='n' ){
4757        c = '\n';
4758      }else if( c=='v' ){
4759        c = '\v';
4760      }else if( c=='f' ){
4761        c = '\f';
4762      }else if( c=='r' ){
4763        c = '\r';
4764      }else if( c=='"' ){
4765        c = '"';
4766      }else if( c=='\'' ){
4767        c = '\'';
4768      }else if( c=='\\' ){
4769        c = '\\';
4770      }else if( c>='0' && c<='7' ){
4771        c -= '0';
4772        if( z[i+1]>='0' && z[i+1]<='7' ){
4773          i++;
4774          c = (c<<3) + z[i] - '0';
4775          if( z[i+1]>='0' && z[i+1]<='7' ){
4776            i++;
4777            c = (c<<3) + z[i] - '0';
4778          }
4779        }
4780      }
4781    }
4782    z[j] = c;
4783  }
4784  if( j<i ) z[j] = 0;
4785}
4786
4787/*
4788** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4789** for TRUE and FALSE.  Return the integer value if appropriate.
4790*/
4791static int booleanValue(const char *zArg){
4792  int i;
4793  if( zArg[0]=='0' && zArg[1]=='x' ){
4794    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4795  }else{
4796    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4797  }
4798  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4799  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4800    return 1;
4801  }
4802  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4803    return 0;
4804  }
4805  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4806          zArg);
4807  return 0;
4808}
4809
4810/*
4811** Set or clear a shell flag according to a boolean value.
4812*/
4813static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4814  if( booleanValue(zArg) ){
4815    ShellSetFlag(p, mFlag);
4816  }else{
4817    ShellClearFlag(p, mFlag);
4818  }
4819}
4820
4821/*
4822** Close an output file, assuming it is not stderr or stdout
4823*/
4824static void output_file_close(FILE *f){
4825  if( f && f!=stdout && f!=stderr ) fclose(f);
4826}
4827
4828/*
4829** Try to open an output file.   The names "stdout" and "stderr" are
4830** recognized and do the right thing.  NULL is returned if the output
4831** filename is "off".
4832*/
4833static FILE *output_file_open(const char *zFile, int bTextMode){
4834  FILE *f;
4835  if( strcmp(zFile,"stdout")==0 ){
4836    f = stdout;
4837  }else if( strcmp(zFile, "stderr")==0 ){
4838    f = stderr;
4839  }else if( strcmp(zFile, "off")==0 ){
4840    f = 0;
4841  }else{
4842    f = fopen(zFile, bTextMode ? "w" : "wb");
4843    if( f==0 ){
4844      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4845    }
4846  }
4847  return f;
4848}
4849
4850#ifndef SQLITE_OMIT_TRACE
4851/*
4852** A routine for handling output from sqlite3_trace().
4853*/
4854static int sql_trace_callback(
4855  unsigned mType,         /* The trace type */
4856  void *pArg,             /* The ShellState pointer */
4857  void *pP,               /* Usually a pointer to sqlite_stmt */
4858  void *pX                /* Auxiliary output */
4859){
4860  ShellState *p = (ShellState*)pArg;
4861  sqlite3_stmt *pStmt;
4862  const char *zSql;
4863  int nSql;
4864  if( p->traceOut==0 ) return 0;
4865  if( mType==SQLITE_TRACE_CLOSE ){
4866    utf8_printf(p->traceOut, "-- closing database connection\n");
4867    return 0;
4868  }
4869  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4870    zSql = (const char*)pX;
4871  }else{
4872    pStmt = (sqlite3_stmt*)pP;
4873    switch( p->eTraceType ){
4874      case SHELL_TRACE_EXPANDED: {
4875        zSql = sqlite3_expanded_sql(pStmt);
4876        break;
4877      }
4878#ifdef SQLITE_ENABLE_NORMALIZE
4879      case SHELL_TRACE_NORMALIZED: {
4880        zSql = sqlite3_normalized_sql(pStmt);
4881        break;
4882      }
4883#endif
4884      default: {
4885        zSql = sqlite3_sql(pStmt);
4886        break;
4887      }
4888    }
4889  }
4890  if( zSql==0 ) return 0;
4891  nSql = strlen30(zSql);
4892  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4893  switch( mType ){
4894    case SQLITE_TRACE_ROW:
4895    case SQLITE_TRACE_STMT: {
4896      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4897      break;
4898    }
4899    case SQLITE_TRACE_PROFILE: {
4900      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4901      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4902      break;
4903    }
4904  }
4905  return 0;
4906}
4907#endif
4908
4909/*
4910** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4911** a useful spot to set a debugger breakpoint.
4912*/
4913static void test_breakpoint(void){
4914  static int nCall = 0;
4915  nCall++;
4916}
4917
4918/*
4919** An object used to read a CSV and other files for import.
4920*/
4921typedef struct ImportCtx ImportCtx;
4922struct ImportCtx {
4923  const char *zFile;  /* Name of the input file */
4924  FILE *in;           /* Read the CSV text from this input stream */
4925  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4926  char *z;            /* Accumulated text for a field */
4927  int n;              /* Number of bytes in z */
4928  int nAlloc;         /* Space allocated for z[] */
4929  int nLine;          /* Current line number */
4930  int nRow;           /* Number of rows imported */
4931  int nErr;           /* Number of errors encountered */
4932  int bNotFirst;      /* True if one or more bytes already read */
4933  int cTerm;          /* Character that terminated the most recent field */
4934  int cColSep;        /* The column separator character.  (Usually ",") */
4935  int cRowSep;        /* The row separator character.  (Usually "\n") */
4936};
4937
4938/* Clean up resourced used by an ImportCtx */
4939static void import_cleanup(ImportCtx *p){
4940  if( p->in!=0 && p->xCloser!=0 ){
4941    p->xCloser(p->in);
4942    p->in = 0;
4943  }
4944  sqlite3_free(p->z);
4945  p->z = 0;
4946}
4947
4948/* Append a single byte to z[] */
4949static void import_append_char(ImportCtx *p, int c){
4950  if( p->n+1>=p->nAlloc ){
4951    p->nAlloc += p->nAlloc + 100;
4952    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4953    if( p->z==0 ) shell_out_of_memory();
4954  }
4955  p->z[p->n++] = (char)c;
4956}
4957
4958/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4959** with the option of having a separator other than ",".
4960**
4961**   +  Input comes from p->in.
4962**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4963**      from sqlite3_malloc64().
4964**   +  Use p->cSep as the column separator.  The default is ",".
4965**   +  Use p->rSep as the row separator.  The default is "\n".
4966**   +  Keep track of the line number in p->nLine.
4967**   +  Store the character that terminates the field in p->cTerm.  Store
4968**      EOF on end-of-file.
4969**   +  Report syntax errors on stderr
4970*/
4971static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4972  int c;
4973  int cSep = p->cColSep;
4974  int rSep = p->cRowSep;
4975  p->n = 0;
4976  c = fgetc(p->in);
4977  if( c==EOF || seenInterrupt ){
4978    p->cTerm = EOF;
4979    return 0;
4980  }
4981  if( c=='"' ){
4982    int pc, ppc;
4983    int startLine = p->nLine;
4984    int cQuote = c;
4985    pc = ppc = 0;
4986    while( 1 ){
4987      c = fgetc(p->in);
4988      if( c==rSep ) p->nLine++;
4989      if( c==cQuote ){
4990        if( pc==cQuote ){
4991          pc = 0;
4992          continue;
4993        }
4994      }
4995      if( (c==cSep && pc==cQuote)
4996       || (c==rSep && pc==cQuote)
4997       || (c==rSep && pc=='\r' && ppc==cQuote)
4998       || (c==EOF && pc==cQuote)
4999      ){
5000        do{ p->n--; }while( p->z[p->n]!=cQuote );
5001        p->cTerm = c;
5002        break;
5003      }
5004      if( pc==cQuote && c!='\r' ){
5005        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5006                p->zFile, p->nLine, cQuote);
5007      }
5008      if( c==EOF ){
5009        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5010                p->zFile, startLine, cQuote);
5011        p->cTerm = c;
5012        break;
5013      }
5014      import_append_char(p, c);
5015      ppc = pc;
5016      pc = c;
5017    }
5018  }else{
5019    /* If this is the first field being parsed and it begins with the
5020    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5021    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5022      import_append_char(p, c);
5023      c = fgetc(p->in);
5024      if( (c&0xff)==0xbb ){
5025        import_append_char(p, c);
5026        c = fgetc(p->in);
5027        if( (c&0xff)==0xbf ){
5028          p->bNotFirst = 1;
5029          p->n = 0;
5030          return csv_read_one_field(p);
5031        }
5032      }
5033    }
5034    while( c!=EOF && c!=cSep && c!=rSep ){
5035      import_append_char(p, c);
5036      c = fgetc(p->in);
5037    }
5038    if( c==rSep ){
5039      p->nLine++;
5040      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5041    }
5042    p->cTerm = c;
5043  }
5044  if( p->z ) p->z[p->n] = 0;
5045  p->bNotFirst = 1;
5046  return p->z;
5047}
5048
5049/* Read a single field of ASCII delimited text.
5050**
5051**   +  Input comes from p->in.
5052**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5053**      from sqlite3_malloc64().
5054**   +  Use p->cSep as the column separator.  The default is "\x1F".
5055**   +  Use p->rSep as the row separator.  The default is "\x1E".
5056**   +  Keep track of the row number in p->nLine.
5057**   +  Store the character that terminates the field in p->cTerm.  Store
5058**      EOF on end-of-file.
5059**   +  Report syntax errors on stderr
5060*/
5061static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5062  int c;
5063  int cSep = p->cColSep;
5064  int rSep = p->cRowSep;
5065  p->n = 0;
5066  c = fgetc(p->in);
5067  if( c==EOF || seenInterrupt ){
5068    p->cTerm = EOF;
5069    return 0;
5070  }
5071  while( c!=EOF && c!=cSep && c!=rSep ){
5072    import_append_char(p, c);
5073    c = fgetc(p->in);
5074  }
5075  if( c==rSep ){
5076    p->nLine++;
5077  }
5078  p->cTerm = c;
5079  if( p->z ) p->z[p->n] = 0;
5080  return p->z;
5081}
5082
5083/*
5084** Try to transfer data for table zTable.  If an error is seen while
5085** moving forward, try to go backwards.  The backwards movement won't
5086** work for WITHOUT ROWID tables.
5087*/
5088static void tryToCloneData(
5089  ShellState *p,
5090  sqlite3 *newDb,
5091  const char *zTable
5092){
5093  sqlite3_stmt *pQuery = 0;
5094  sqlite3_stmt *pInsert = 0;
5095  char *zQuery = 0;
5096  char *zInsert = 0;
5097  int rc;
5098  int i, j, n;
5099  int nTable = strlen30(zTable);
5100  int k = 0;
5101  int cnt = 0;
5102  const int spinRate = 10000;
5103
5104  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5105  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5106  if( rc ){
5107    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5108            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5109            zQuery);
5110    goto end_data_xfer;
5111  }
5112  n = sqlite3_column_count(pQuery);
5113  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5114  if( zInsert==0 ) shell_out_of_memory();
5115  sqlite3_snprintf(200+nTable,zInsert,
5116                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5117  i = strlen30(zInsert);
5118  for(j=1; j<n; j++){
5119    memcpy(zInsert+i, ",?", 2);
5120    i += 2;
5121  }
5122  memcpy(zInsert+i, ");", 3);
5123  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5124  if( rc ){
5125    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5126            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5127            zQuery);
5128    goto end_data_xfer;
5129  }
5130  for(k=0; k<2; k++){
5131    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5132      for(i=0; i<n; i++){
5133        switch( sqlite3_column_type(pQuery, i) ){
5134          case SQLITE_NULL: {
5135            sqlite3_bind_null(pInsert, i+1);
5136            break;
5137          }
5138          case SQLITE_INTEGER: {
5139            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5140            break;
5141          }
5142          case SQLITE_FLOAT: {
5143            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5144            break;
5145          }
5146          case SQLITE_TEXT: {
5147            sqlite3_bind_text(pInsert, i+1,
5148                             (const char*)sqlite3_column_text(pQuery,i),
5149                             -1, SQLITE_STATIC);
5150            break;
5151          }
5152          case SQLITE_BLOB: {
5153            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5154                                            sqlite3_column_bytes(pQuery,i),
5155                                            SQLITE_STATIC);
5156            break;
5157          }
5158        }
5159      } /* End for */
5160      rc = sqlite3_step(pInsert);
5161      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5162        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5163                        sqlite3_errmsg(newDb));
5164      }
5165      sqlite3_reset(pInsert);
5166      cnt++;
5167      if( (cnt%spinRate)==0 ){
5168        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5169        fflush(stdout);
5170      }
5171    } /* End while */
5172    if( rc==SQLITE_DONE ) break;
5173    sqlite3_finalize(pQuery);
5174    sqlite3_free(zQuery);
5175    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5176                             zTable);
5177    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5178    if( rc ){
5179      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5180      break;
5181    }
5182  } /* End for(k=0...) */
5183
5184end_data_xfer:
5185  sqlite3_finalize(pQuery);
5186  sqlite3_finalize(pInsert);
5187  sqlite3_free(zQuery);
5188  sqlite3_free(zInsert);
5189}
5190
5191
5192/*
5193** Try to transfer all rows of the schema that match zWhere.  For
5194** each row, invoke xForEach() on the object defined by that row.
5195** If an error is encountered while moving forward through the
5196** sqlite_schema table, try again moving backwards.
5197*/
5198static void tryToCloneSchema(
5199  ShellState *p,
5200  sqlite3 *newDb,
5201  const char *zWhere,
5202  void (*xForEach)(ShellState*,sqlite3*,const char*)
5203){
5204  sqlite3_stmt *pQuery = 0;
5205  char *zQuery = 0;
5206  int rc;
5207  const unsigned char *zName;
5208  const unsigned char *zSql;
5209  char *zErrMsg = 0;
5210
5211  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5212                           " WHERE %s", zWhere);
5213  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5214  if( rc ){
5215    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5216                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5217                    zQuery);
5218    goto end_schema_xfer;
5219  }
5220  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5221    zName = sqlite3_column_text(pQuery, 0);
5222    zSql = sqlite3_column_text(pQuery, 1);
5223    printf("%s... ", zName); fflush(stdout);
5224    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5225    if( zErrMsg ){
5226      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5227      sqlite3_free(zErrMsg);
5228      zErrMsg = 0;
5229    }
5230    if( xForEach ){
5231      xForEach(p, newDb, (const char*)zName);
5232    }
5233    printf("done\n");
5234  }
5235  if( rc!=SQLITE_DONE ){
5236    sqlite3_finalize(pQuery);
5237    sqlite3_free(zQuery);
5238    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5239                             " WHERE %s ORDER BY rowid DESC", zWhere);
5240    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5241    if( rc ){
5242      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5243                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5244                      zQuery);
5245      goto end_schema_xfer;
5246    }
5247    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5248      zName = sqlite3_column_text(pQuery, 0);
5249      zSql = sqlite3_column_text(pQuery, 1);
5250      printf("%s... ", zName); fflush(stdout);
5251      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5252      if( zErrMsg ){
5253        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5254        sqlite3_free(zErrMsg);
5255        zErrMsg = 0;
5256      }
5257      if( xForEach ){
5258        xForEach(p, newDb, (const char*)zName);
5259      }
5260      printf("done\n");
5261    }
5262  }
5263end_schema_xfer:
5264  sqlite3_finalize(pQuery);
5265  sqlite3_free(zQuery);
5266}
5267
5268/*
5269** Open a new database file named "zNewDb".  Try to recover as much information
5270** as possible out of the main database (which might be corrupt) and write it
5271** into zNewDb.
5272*/
5273static void tryToClone(ShellState *p, const char *zNewDb){
5274  int rc;
5275  sqlite3 *newDb = 0;
5276  if( access(zNewDb,0)==0 ){
5277    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5278    return;
5279  }
5280  rc = sqlite3_open(zNewDb, &newDb);
5281  if( rc ){
5282    utf8_printf(stderr, "Cannot create output database: %s\n",
5283            sqlite3_errmsg(newDb));
5284  }else{
5285    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5286    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5287    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5288    tryToCloneSchema(p, newDb, "type!='table'", 0);
5289    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5290    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5291  }
5292  close_db(newDb);
5293}
5294
5295/*
5296** Change the output file back to stdout.
5297**
5298** If the p->doXdgOpen flag is set, that means the output was being
5299** redirected to a temporary file named by p->zTempFile.  In that case,
5300** launch start/open/xdg-open on that temporary file.
5301*/
5302static void output_reset(ShellState *p){
5303  if( p->outfile[0]=='|' ){
5304#ifndef SQLITE_OMIT_POPEN
5305    pclose(p->out);
5306#endif
5307  }else{
5308    output_file_close(p->out);
5309#ifndef SQLITE_NOHAVE_SYSTEM
5310    if( p->doXdgOpen ){
5311      const char *zXdgOpenCmd =
5312#if defined(_WIN32)
5313      "start";
5314#elif defined(__APPLE__)
5315      "open";
5316#else
5317      "xdg-open";
5318#endif
5319      char *zCmd;
5320      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5321      if( system(zCmd) ){
5322        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5323      }else{
5324        /* Give the start/open/xdg-open command some time to get
5325        ** going before we continue, and potential delete the
5326        ** p->zTempFile data file out from under it */
5327        sqlite3_sleep(2000);
5328      }
5329      sqlite3_free(zCmd);
5330      outputModePop(p);
5331      p->doXdgOpen = 0;
5332    }
5333#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5334  }
5335  p->outfile[0] = 0;
5336  p->out = stdout;
5337}
5338
5339/*
5340** Run an SQL command and return the single integer result.
5341*/
5342static int db_int(ShellState *p, const char *zSql){
5343  sqlite3_stmt *pStmt;
5344  int res = 0;
5345  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5346  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5347    res = sqlite3_column_int(pStmt,0);
5348  }
5349  sqlite3_finalize(pStmt);
5350  return res;
5351}
5352
5353/*
5354** Convert a 2-byte or 4-byte big-endian integer into a native integer
5355*/
5356static unsigned int get2byteInt(unsigned char *a){
5357  return (a[0]<<8) + a[1];
5358}
5359static unsigned int get4byteInt(unsigned char *a){
5360  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5361}
5362
5363/*
5364** Implementation of the ".dbinfo" command.
5365**
5366** Return 1 on error, 2 to exit, and 0 otherwise.
5367*/
5368static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5369  static const struct { const char *zName; int ofst; } aField[] = {
5370     { "file change counter:",  24  },
5371     { "database page count:",  28  },
5372     { "freelist page count:",  36  },
5373     { "schema cookie:",        40  },
5374     { "schema format:",        44  },
5375     { "default cache size:",   48  },
5376     { "autovacuum top root:",  52  },
5377     { "incremental vacuum:",   64  },
5378     { "text encoding:",        56  },
5379     { "user version:",         60  },
5380     { "application id:",       68  },
5381     { "software version:",     96  },
5382  };
5383  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5384     { "number of tables:",
5385       "SELECT count(*) FROM %s WHERE type='table'" },
5386     { "number of indexes:",
5387       "SELECT count(*) FROM %s WHERE type='index'" },
5388     { "number of triggers:",
5389       "SELECT count(*) FROM %s WHERE type='trigger'" },
5390     { "number of views:",
5391       "SELECT count(*) FROM %s WHERE type='view'" },
5392     { "schema size:",
5393       "SELECT total(length(sql)) FROM %s" },
5394  };
5395  int i, rc;
5396  unsigned iDataVersion;
5397  char *zSchemaTab;
5398  char *zDb = nArg>=2 ? azArg[1] : "main";
5399  sqlite3_stmt *pStmt = 0;
5400  unsigned char aHdr[100];
5401  open_db(p, 0);
5402  if( p->db==0 ) return 1;
5403  rc = sqlite3_prepare_v2(p->db,
5404             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5405             -1, &pStmt, 0);
5406  if( rc ){
5407    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5408    sqlite3_finalize(pStmt);
5409    return 1;
5410  }
5411  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5412  if( sqlite3_step(pStmt)==SQLITE_ROW
5413   && sqlite3_column_bytes(pStmt,0)>100
5414  ){
5415    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5416    sqlite3_finalize(pStmt);
5417  }else{
5418    raw_printf(stderr, "unable to read database header\n");
5419    sqlite3_finalize(pStmt);
5420    return 1;
5421  }
5422  i = get2byteInt(aHdr+16);
5423  if( i==1 ) i = 65536;
5424  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5425  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5426  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5427  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5428  for(i=0; i<ArraySize(aField); i++){
5429    int ofst = aField[i].ofst;
5430    unsigned int val = get4byteInt(aHdr + ofst);
5431    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5432    switch( ofst ){
5433      case 56: {
5434        if( val==1 ) raw_printf(p->out, " (utf8)");
5435        if( val==2 ) raw_printf(p->out, " (utf16le)");
5436        if( val==3 ) raw_printf(p->out, " (utf16be)");
5437      }
5438    }
5439    raw_printf(p->out, "\n");
5440  }
5441  if( zDb==0 ){
5442    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5443  }else if( strcmp(zDb,"temp")==0 ){
5444    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5445  }else{
5446    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5447  }
5448  for(i=0; i<ArraySize(aQuery); i++){
5449    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5450    int val = db_int(p, zSql);
5451    sqlite3_free(zSql);
5452    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5453  }
5454  sqlite3_free(zSchemaTab);
5455  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5456  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5457  return 0;
5458}
5459
5460/*
5461** Print the current sqlite3_errmsg() value to stderr and return 1.
5462*/
5463static int shellDatabaseError(sqlite3 *db){
5464  const char *zErr = sqlite3_errmsg(db);
5465  utf8_printf(stderr, "Error: %s\n", zErr);
5466  return 1;
5467}
5468
5469/*
5470** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5471** if they match and FALSE (0) if they do not match.
5472**
5473** Globbing rules:
5474**
5475**      '*'       Matches any sequence of zero or more characters.
5476**
5477**      '?'       Matches exactly one character.
5478**
5479**     [...]      Matches one character from the enclosed list of
5480**                characters.
5481**
5482**     [^...]     Matches one character not in the enclosed list.
5483**
5484**      '#'       Matches any sequence of one or more digits with an
5485**                optional + or - sign in front
5486**
5487**      ' '       Any span of whitespace matches any other span of
5488**                whitespace.
5489**
5490** Extra whitespace at the end of z[] is ignored.
5491*/
5492static int testcase_glob(const char *zGlob, const char *z){
5493  int c, c2;
5494  int invert;
5495  int seen;
5496
5497  while( (c = (*(zGlob++)))!=0 ){
5498    if( IsSpace(c) ){
5499      if( !IsSpace(*z) ) return 0;
5500      while( IsSpace(*zGlob) ) zGlob++;
5501      while( IsSpace(*z) ) z++;
5502    }else if( c=='*' ){
5503      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5504        if( c=='?' && (*(z++))==0 ) return 0;
5505      }
5506      if( c==0 ){
5507        return 1;
5508      }else if( c=='[' ){
5509        while( *z && testcase_glob(zGlob-1,z)==0 ){
5510          z++;
5511        }
5512        return (*z)!=0;
5513      }
5514      while( (c2 = (*(z++)))!=0 ){
5515        while( c2!=c ){
5516          c2 = *(z++);
5517          if( c2==0 ) return 0;
5518        }
5519        if( testcase_glob(zGlob,z) ) return 1;
5520      }
5521      return 0;
5522    }else if( c=='?' ){
5523      if( (*(z++))==0 ) return 0;
5524    }else if( c=='[' ){
5525      int prior_c = 0;
5526      seen = 0;
5527      invert = 0;
5528      c = *(z++);
5529      if( c==0 ) return 0;
5530      c2 = *(zGlob++);
5531      if( c2=='^' ){
5532        invert = 1;
5533        c2 = *(zGlob++);
5534      }
5535      if( c2==']' ){
5536        if( c==']' ) seen = 1;
5537        c2 = *(zGlob++);
5538      }
5539      while( c2 && c2!=']' ){
5540        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5541          c2 = *(zGlob++);
5542          if( c>=prior_c && c<=c2 ) seen = 1;
5543          prior_c = 0;
5544        }else{
5545          if( c==c2 ){
5546            seen = 1;
5547          }
5548          prior_c = c2;
5549        }
5550        c2 = *(zGlob++);
5551      }
5552      if( c2==0 || (seen ^ invert)==0 ) return 0;
5553    }else if( c=='#' ){
5554      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5555      if( !IsDigit(z[0]) ) return 0;
5556      z++;
5557      while( IsDigit(z[0]) ){ z++; }
5558    }else{
5559      if( c!=(*(z++)) ) return 0;
5560    }
5561  }
5562  while( IsSpace(*z) ){ z++; }
5563  return *z==0;
5564}
5565
5566
5567/*
5568** Compare the string as a command-line option with either one or two
5569** initial "-" characters.
5570*/
5571static int optionMatch(const char *zStr, const char *zOpt){
5572  if( zStr[0]!='-' ) return 0;
5573  zStr++;
5574  if( zStr[0]=='-' ) zStr++;
5575  return strcmp(zStr, zOpt)==0;
5576}
5577
5578/*
5579** Delete a file.
5580*/
5581int shellDeleteFile(const char *zFilename){
5582  int rc;
5583#ifdef _WIN32
5584  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5585  rc = _wunlink(z);
5586  sqlite3_free(z);
5587#else
5588  rc = unlink(zFilename);
5589#endif
5590  return rc;
5591}
5592
5593/*
5594** Try to delete the temporary file (if there is one) and free the
5595** memory used to hold the name of the temp file.
5596*/
5597static void clearTempFile(ShellState *p){
5598  if( p->zTempFile==0 ) return;
5599  if( p->doXdgOpen ) return;
5600  if( shellDeleteFile(p->zTempFile) ) return;
5601  sqlite3_free(p->zTempFile);
5602  p->zTempFile = 0;
5603}
5604
5605/*
5606** Create a new temp file name with the given suffix.
5607*/
5608static void newTempFile(ShellState *p, const char *zSuffix){
5609  clearTempFile(p);
5610  sqlite3_free(p->zTempFile);
5611  p->zTempFile = 0;
5612  if( p->db ){
5613    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5614  }
5615  if( p->zTempFile==0 ){
5616    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5617    ** will not work and we will need to fallback to guessing */
5618    char *zTemp;
5619    sqlite3_uint64 r;
5620    sqlite3_randomness(sizeof(r), &r);
5621    zTemp = getenv("TEMP");
5622    if( zTemp==0 ) zTemp = getenv("TMP");
5623    if( zTemp==0 ){
5624#ifdef _WIN32
5625      zTemp = "\\tmp";
5626#else
5627      zTemp = "/tmp";
5628#endif
5629    }
5630    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5631  }else{
5632    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5633  }
5634  if( p->zTempFile==0 ){
5635    raw_printf(stderr, "out of memory\n");
5636    exit(1);
5637  }
5638}
5639
5640
5641/*
5642** The implementation of SQL scalar function fkey_collate_clause(), used
5643** by the ".lint fkey-indexes" command. This scalar function is always
5644** called with four arguments - the parent table name, the parent column name,
5645** the child table name and the child column name.
5646**
5647**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5648**
5649** If either of the named tables or columns do not exist, this function
5650** returns an empty string. An empty string is also returned if both tables
5651** and columns exist but have the same default collation sequence. Or,
5652** if both exist but the default collation sequences are different, this
5653** function returns the string " COLLATE <parent-collation>", where
5654** <parent-collation> is the default collation sequence of the parent column.
5655*/
5656static void shellFkeyCollateClause(
5657  sqlite3_context *pCtx,
5658  int nVal,
5659  sqlite3_value **apVal
5660){
5661  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5662  const char *zParent;
5663  const char *zParentCol;
5664  const char *zParentSeq;
5665  const char *zChild;
5666  const char *zChildCol;
5667  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5668  int rc;
5669
5670  assert( nVal==4 );
5671  zParent = (const char*)sqlite3_value_text(apVal[0]);
5672  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5673  zChild = (const char*)sqlite3_value_text(apVal[2]);
5674  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5675
5676  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5677  rc = sqlite3_table_column_metadata(
5678      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5679  );
5680  if( rc==SQLITE_OK ){
5681    rc = sqlite3_table_column_metadata(
5682        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5683    );
5684  }
5685
5686  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5687    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5688    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5689    sqlite3_free(z);
5690  }
5691}
5692
5693
5694/*
5695** The implementation of dot-command ".lint fkey-indexes".
5696*/
5697static int lintFkeyIndexes(
5698  ShellState *pState,             /* Current shell tool state */
5699  char **azArg,                   /* Array of arguments passed to dot command */
5700  int nArg                        /* Number of entries in azArg[] */
5701){
5702  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5703  FILE *out = pState->out;        /* Stream to write non-error output to */
5704  int bVerbose = 0;               /* If -verbose is present */
5705  int bGroupByParent = 0;         /* If -groupbyparent is present */
5706  int i;                          /* To iterate through azArg[] */
5707  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5708  int rc;                         /* Return code */
5709  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5710
5711  /*
5712  ** This SELECT statement returns one row for each foreign key constraint
5713  ** in the schema of the main database. The column values are:
5714  **
5715  ** 0. The text of an SQL statement similar to:
5716  **
5717  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5718  **
5719  **    This SELECT is similar to the one that the foreign keys implementation
5720  **    needs to run internally on child tables. If there is an index that can
5721  **    be used to optimize this query, then it can also be used by the FK
5722  **    implementation to optimize DELETE or UPDATE statements on the parent
5723  **    table.
5724  **
5725  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5726  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5727  **    contains an index that can be used to optimize the query.
5728  **
5729  ** 2. Human readable text that describes the child table and columns. e.g.
5730  **
5731  **       "child_table(child_key1, child_key2)"
5732  **
5733  ** 3. Human readable text that describes the parent table and columns. e.g.
5734  **
5735  **       "parent_table(parent_key1, parent_key2)"
5736  **
5737  ** 4. A full CREATE INDEX statement for an index that could be used to
5738  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5739  **
5740  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5741  **
5742  ** 5. The name of the parent table.
5743  **
5744  ** These six values are used by the C logic below to generate the report.
5745  */
5746  const char *zSql =
5747  "SELECT "
5748    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5749    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5750    "  || fkey_collate_clause("
5751    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5752    ", "
5753    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5754    "  || group_concat('*=?', ' AND ') || ')'"
5755    ", "
5756    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5757    ", "
5758    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5759    ", "
5760    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5761    "  || ' ON ' || quote(s.name) || '('"
5762    "  || group_concat(quote(f.[from]) ||"
5763    "        fkey_collate_clause("
5764    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5765    "  || ');'"
5766    ", "
5767    "     f.[table] "
5768    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5769    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5770    "GROUP BY s.name, f.id "
5771    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5772  ;
5773  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5774
5775  for(i=2; i<nArg; i++){
5776    int n = strlen30(azArg[i]);
5777    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5778      bVerbose = 1;
5779    }
5780    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5781      bGroupByParent = 1;
5782      zIndent = "    ";
5783    }
5784    else{
5785      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5786          azArg[0], azArg[1]
5787      );
5788      return SQLITE_ERROR;
5789    }
5790  }
5791
5792  /* Register the fkey_collate_clause() SQL function */
5793  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5794      0, shellFkeyCollateClause, 0, 0
5795  );
5796
5797
5798  if( rc==SQLITE_OK ){
5799    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5800  }
5801  if( rc==SQLITE_OK ){
5802    sqlite3_bind_int(pSql, 1, bGroupByParent);
5803  }
5804
5805  if( rc==SQLITE_OK ){
5806    int rc2;
5807    char *zPrev = 0;
5808    while( SQLITE_ROW==sqlite3_step(pSql) ){
5809      int res = -1;
5810      sqlite3_stmt *pExplain = 0;
5811      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5812      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5813      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5814      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5815      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5816      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5817
5818      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5819      if( rc!=SQLITE_OK ) break;
5820      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5821        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5822        res = (
5823              0==sqlite3_strglob(zGlob, zPlan)
5824           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5825        );
5826      }
5827      rc = sqlite3_finalize(pExplain);
5828      if( rc!=SQLITE_OK ) break;
5829
5830      if( res<0 ){
5831        raw_printf(stderr, "Error: internal error");
5832        break;
5833      }else{
5834        if( bGroupByParent
5835        && (bVerbose || res==0)
5836        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5837        ){
5838          raw_printf(out, "-- Parent table %s\n", zParent);
5839          sqlite3_free(zPrev);
5840          zPrev = sqlite3_mprintf("%s", zParent);
5841        }
5842
5843        if( res==0 ){
5844          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5845        }else if( bVerbose ){
5846          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5847              zIndent, zFrom, zTarget
5848          );
5849        }
5850      }
5851    }
5852    sqlite3_free(zPrev);
5853
5854    if( rc!=SQLITE_OK ){
5855      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5856    }
5857
5858    rc2 = sqlite3_finalize(pSql);
5859    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5860      rc = rc2;
5861      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5862    }
5863  }else{
5864    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5865  }
5866
5867  return rc;
5868}
5869
5870/*
5871** Implementation of ".lint" dot command.
5872*/
5873static int lintDotCommand(
5874  ShellState *pState,             /* Current shell tool state */
5875  char **azArg,                   /* Array of arguments passed to dot command */
5876  int nArg                        /* Number of entries in azArg[] */
5877){
5878  int n;
5879  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5880  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5881  return lintFkeyIndexes(pState, azArg, nArg);
5882
5883 usage:
5884  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5885  raw_printf(stderr, "Where sub-commands are:\n");
5886  raw_printf(stderr, "    fkey-indexes\n");
5887  return SQLITE_ERROR;
5888}
5889
5890#if !defined SQLITE_OMIT_VIRTUALTABLE
5891static void shellPrepare(
5892  sqlite3 *db,
5893  int *pRc,
5894  const char *zSql,
5895  sqlite3_stmt **ppStmt
5896){
5897  *ppStmt = 0;
5898  if( *pRc==SQLITE_OK ){
5899    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5900    if( rc!=SQLITE_OK ){
5901      raw_printf(stderr, "sql error: %s (%d)\n",
5902          sqlite3_errmsg(db), sqlite3_errcode(db)
5903      );
5904      *pRc = rc;
5905    }
5906  }
5907}
5908
5909/*
5910** Create a prepared statement using printf-style arguments for the SQL.
5911**
5912** This routine is could be marked "static".  But it is not always used,
5913** depending on compile-time options.  By omitting the "static", we avoid
5914** nuisance compiler warnings about "defined but not used".
5915*/
5916void shellPreparePrintf(
5917  sqlite3 *db,
5918  int *pRc,
5919  sqlite3_stmt **ppStmt,
5920  const char *zFmt,
5921  ...
5922){
5923  *ppStmt = 0;
5924  if( *pRc==SQLITE_OK ){
5925    va_list ap;
5926    char *z;
5927    va_start(ap, zFmt);
5928    z = sqlite3_vmprintf(zFmt, ap);
5929    va_end(ap);
5930    if( z==0 ){
5931      *pRc = SQLITE_NOMEM;
5932    }else{
5933      shellPrepare(db, pRc, z, ppStmt);
5934      sqlite3_free(z);
5935    }
5936  }
5937}
5938
5939/* Finalize the prepared statement created using shellPreparePrintf().
5940**
5941** This routine is could be marked "static".  But it is not always used,
5942** depending on compile-time options.  By omitting the "static", we avoid
5943** nuisance compiler warnings about "defined but not used".
5944*/
5945void shellFinalize(
5946  int *pRc,
5947  sqlite3_stmt *pStmt
5948){
5949  if( pStmt ){
5950    sqlite3 *db = sqlite3_db_handle(pStmt);
5951    int rc = sqlite3_finalize(pStmt);
5952    if( *pRc==SQLITE_OK ){
5953      if( rc!=SQLITE_OK ){
5954        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5955      }
5956      *pRc = rc;
5957    }
5958  }
5959}
5960
5961/* Reset the prepared statement created using shellPreparePrintf().
5962**
5963** This routine is could be marked "static".  But it is not always used,
5964** depending on compile-time options.  By omitting the "static", we avoid
5965** nuisance compiler warnings about "defined but not used".
5966*/
5967void shellReset(
5968  int *pRc,
5969  sqlite3_stmt *pStmt
5970){
5971  int rc = sqlite3_reset(pStmt);
5972  if( *pRc==SQLITE_OK ){
5973    if( rc!=SQLITE_OK ){
5974      sqlite3 *db = sqlite3_db_handle(pStmt);
5975      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5976    }
5977    *pRc = rc;
5978  }
5979}
5980#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5981
5982#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5983/******************************************************************************
5984** The ".archive" or ".ar" command.
5985*/
5986/*
5987** Structure representing a single ".ar" command.
5988*/
5989typedef struct ArCommand ArCommand;
5990struct ArCommand {
5991  u8 eCmd;                        /* An AR_CMD_* value */
5992  u8 bVerbose;                    /* True if --verbose */
5993  u8 bZip;                        /* True if the archive is a ZIP */
5994  u8 bDryRun;                     /* True if --dry-run */
5995  u8 bAppend;                     /* True if --append */
5996  u8 fromCmdLine;                 /* Run from -A instead of .archive */
5997  int nArg;                       /* Number of command arguments */
5998  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
5999  const char *zFile;              /* --file argument, or NULL */
6000  const char *zDir;               /* --directory argument, or NULL */
6001  char **azArg;                   /* Array of command arguments */
6002  ShellState *p;                  /* Shell state */
6003  sqlite3 *db;                    /* Database containing the archive */
6004};
6005
6006/*
6007** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6008*/
6009static int arUsage(FILE *f){
6010  showHelp(f,"archive");
6011  return SQLITE_ERROR;
6012}
6013
6014/*
6015** Print an error message for the .ar command to stderr and return
6016** SQLITE_ERROR.
6017*/
6018static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6019  va_list ap;
6020  char *z;
6021  va_start(ap, zFmt);
6022  z = sqlite3_vmprintf(zFmt, ap);
6023  va_end(ap);
6024  utf8_printf(stderr, "Error: %s\n", z);
6025  if( pAr->fromCmdLine ){
6026    utf8_printf(stderr, "Use \"-A\" for more help\n");
6027  }else{
6028    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6029  }
6030  sqlite3_free(z);
6031  return SQLITE_ERROR;
6032}
6033
6034/*
6035** Values for ArCommand.eCmd.
6036*/
6037#define AR_CMD_CREATE       1
6038#define AR_CMD_UPDATE       2
6039#define AR_CMD_INSERT       3
6040#define AR_CMD_EXTRACT      4
6041#define AR_CMD_LIST         5
6042#define AR_CMD_HELP         6
6043
6044/*
6045** Other (non-command) switches.
6046*/
6047#define AR_SWITCH_VERBOSE     7
6048#define AR_SWITCH_FILE        8
6049#define AR_SWITCH_DIRECTORY   9
6050#define AR_SWITCH_APPEND     10
6051#define AR_SWITCH_DRYRUN     11
6052
6053static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6054  switch( eSwitch ){
6055    case AR_CMD_CREATE:
6056    case AR_CMD_EXTRACT:
6057    case AR_CMD_LIST:
6058    case AR_CMD_UPDATE:
6059    case AR_CMD_INSERT:
6060    case AR_CMD_HELP:
6061      if( pAr->eCmd ){
6062        return arErrorMsg(pAr, "multiple command options");
6063      }
6064      pAr->eCmd = eSwitch;
6065      break;
6066
6067    case AR_SWITCH_DRYRUN:
6068      pAr->bDryRun = 1;
6069      break;
6070    case AR_SWITCH_VERBOSE:
6071      pAr->bVerbose = 1;
6072      break;
6073    case AR_SWITCH_APPEND:
6074      pAr->bAppend = 1;
6075      /* Fall thru into --file */
6076    case AR_SWITCH_FILE:
6077      pAr->zFile = zArg;
6078      break;
6079    case AR_SWITCH_DIRECTORY:
6080      pAr->zDir = zArg;
6081      break;
6082  }
6083
6084  return SQLITE_OK;
6085}
6086
6087/*
6088** Parse the command line for an ".ar" command. The results are written into
6089** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6090** successfully, otherwise an error message is written to stderr and
6091** SQLITE_ERROR returned.
6092*/
6093static int arParseCommand(
6094  char **azArg,                   /* Array of arguments passed to dot command */
6095  int nArg,                       /* Number of entries in azArg[] */
6096  ArCommand *pAr                  /* Populate this object */
6097){
6098  struct ArSwitch {
6099    const char *zLong;
6100    char cShort;
6101    u8 eSwitch;
6102    u8 bArg;
6103  } aSwitch[] = {
6104    { "create",    'c', AR_CMD_CREATE,       0 },
6105    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6106    { "insert",    'i', AR_CMD_INSERT,       0 },
6107    { "list",      't', AR_CMD_LIST,         0 },
6108    { "update",    'u', AR_CMD_UPDATE,       0 },
6109    { "help",      'h', AR_CMD_HELP,         0 },
6110    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6111    { "file",      'f', AR_SWITCH_FILE,      1 },
6112    { "append",    'a', AR_SWITCH_APPEND,    1 },
6113    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6114    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6115  };
6116  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6117  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6118
6119  if( nArg<=1 ){
6120    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6121    return arUsage(stderr);
6122  }else{
6123    char *z = azArg[1];
6124    if( z[0]!='-' ){
6125      /* Traditional style [tar] invocation */
6126      int i;
6127      int iArg = 2;
6128      for(i=0; z[i]; i++){
6129        const char *zArg = 0;
6130        struct ArSwitch *pOpt;
6131        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6132          if( z[i]==pOpt->cShort ) break;
6133        }
6134        if( pOpt==pEnd ){
6135          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6136        }
6137        if( pOpt->bArg ){
6138          if( iArg>=nArg ){
6139            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6140          }
6141          zArg = azArg[iArg++];
6142        }
6143        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6144      }
6145      pAr->nArg = nArg-iArg;
6146      if( pAr->nArg>0 ){
6147        pAr->azArg = &azArg[iArg];
6148      }
6149    }else{
6150      /* Non-traditional invocation */
6151      int iArg;
6152      for(iArg=1; iArg<nArg; iArg++){
6153        int n;
6154        z = azArg[iArg];
6155        if( z[0]!='-' ){
6156          /* All remaining command line words are command arguments. */
6157          pAr->azArg = &azArg[iArg];
6158          pAr->nArg = nArg-iArg;
6159          break;
6160        }
6161        n = strlen30(z);
6162
6163        if( z[1]!='-' ){
6164          int i;
6165          /* One or more short options */
6166          for(i=1; i<n; i++){
6167            const char *zArg = 0;
6168            struct ArSwitch *pOpt;
6169            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6170              if( z[i]==pOpt->cShort ) break;
6171            }
6172            if( pOpt==pEnd ){
6173              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6174            }
6175            if( pOpt->bArg ){
6176              if( i<(n-1) ){
6177                zArg = &z[i+1];
6178                i = n;
6179              }else{
6180                if( iArg>=(nArg-1) ){
6181                  return arErrorMsg(pAr, "option requires an argument: %c",
6182                                    z[i]);
6183                }
6184                zArg = azArg[++iArg];
6185              }
6186            }
6187            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6188          }
6189        }else if( z[2]=='\0' ){
6190          /* A -- option, indicating that all remaining command line words
6191          ** are command arguments.  */
6192          pAr->azArg = &azArg[iArg+1];
6193          pAr->nArg = nArg-iArg-1;
6194          break;
6195        }else{
6196          /* A long option */
6197          const char *zArg = 0;             /* Argument for option, if any */
6198          struct ArSwitch *pMatch = 0;      /* Matching option */
6199          struct ArSwitch *pOpt;            /* Iterator */
6200          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6201            const char *zLong = pOpt->zLong;
6202            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6203              if( pMatch ){
6204                return arErrorMsg(pAr, "ambiguous option: %s",z);
6205              }else{
6206                pMatch = pOpt;
6207              }
6208            }
6209          }
6210
6211          if( pMatch==0 ){
6212            return arErrorMsg(pAr, "unrecognized option: %s", z);
6213          }
6214          if( pMatch->bArg ){
6215            if( iArg>=(nArg-1) ){
6216              return arErrorMsg(pAr, "option requires an argument: %s", z);
6217            }
6218            zArg = azArg[++iArg];
6219          }
6220          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6221        }
6222      }
6223    }
6224  }
6225
6226  return SQLITE_OK;
6227}
6228
6229/*
6230** This function assumes that all arguments within the ArCommand.azArg[]
6231** array refer to archive members, as for the --extract or --list commands.
6232** It checks that each of them are present. If any specified file is not
6233** present in the archive, an error is printed to stderr and an error
6234** code returned. Otherwise, if all specified arguments are present in
6235** the archive, SQLITE_OK is returned.
6236**
6237** This function strips any trailing '/' characters from each argument.
6238** This is consistent with the way the [tar] command seems to work on
6239** Linux.
6240*/
6241static int arCheckEntries(ArCommand *pAr){
6242  int rc = SQLITE_OK;
6243  if( pAr->nArg ){
6244    int i, j;
6245    sqlite3_stmt *pTest = 0;
6246
6247    shellPreparePrintf(pAr->db, &rc, &pTest,
6248        "SELECT name FROM %s WHERE name=$name",
6249        pAr->zSrcTable
6250    );
6251    j = sqlite3_bind_parameter_index(pTest, "$name");
6252    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6253      char *z = pAr->azArg[i];
6254      int n = strlen30(z);
6255      int bOk = 0;
6256      while( n>0 && z[n-1]=='/' ) n--;
6257      z[n] = '\0';
6258      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6259      if( SQLITE_ROW==sqlite3_step(pTest) ){
6260        bOk = 1;
6261      }
6262      shellReset(&rc, pTest);
6263      if( rc==SQLITE_OK && bOk==0 ){
6264        utf8_printf(stderr, "not found in archive: %s\n", z);
6265        rc = SQLITE_ERROR;
6266      }
6267    }
6268    shellFinalize(&rc, pTest);
6269  }
6270  return rc;
6271}
6272
6273/*
6274** Format a WHERE clause that can be used against the "sqlar" table to
6275** identify all archive members that match the command arguments held
6276** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6277** The caller is responsible for eventually calling sqlite3_free() on
6278** any non-NULL (*pzWhere) value.
6279*/
6280static void arWhereClause(
6281  int *pRc,
6282  ArCommand *pAr,
6283  char **pzWhere                  /* OUT: New WHERE clause */
6284){
6285  char *zWhere = 0;
6286  if( *pRc==SQLITE_OK ){
6287    if( pAr->nArg==0 ){
6288      zWhere = sqlite3_mprintf("1");
6289    }else{
6290      int i;
6291      const char *zSep = "";
6292      for(i=0; i<pAr->nArg; i++){
6293        const char *z = pAr->azArg[i];
6294        zWhere = sqlite3_mprintf(
6295          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6296          zWhere, zSep, z, strlen30(z)+1, z
6297        );
6298        if( zWhere==0 ){
6299          *pRc = SQLITE_NOMEM;
6300          break;
6301        }
6302        zSep = " OR ";
6303      }
6304    }
6305  }
6306  *pzWhere = zWhere;
6307}
6308
6309/*
6310** Implementation of .ar "lisT" command.
6311*/
6312static int arListCommand(ArCommand *pAr){
6313  const char *zSql = "SELECT %s FROM %s WHERE %s";
6314  const char *azCols[] = {
6315    "name",
6316    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6317  };
6318
6319  char *zWhere = 0;
6320  sqlite3_stmt *pSql = 0;
6321  int rc;
6322
6323  rc = arCheckEntries(pAr);
6324  arWhereClause(&rc, pAr, &zWhere);
6325
6326  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6327                     pAr->zSrcTable, zWhere);
6328  if( pAr->bDryRun ){
6329    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6330  }else{
6331    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6332      if( pAr->bVerbose ){
6333        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6334            sqlite3_column_text(pSql, 0),
6335            sqlite3_column_int(pSql, 1),
6336            sqlite3_column_text(pSql, 2),
6337            sqlite3_column_text(pSql, 3)
6338        );
6339      }else{
6340        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6341      }
6342    }
6343  }
6344  shellFinalize(&rc, pSql);
6345  sqlite3_free(zWhere);
6346  return rc;
6347}
6348
6349
6350/*
6351** Implementation of .ar "eXtract" command.
6352*/
6353static int arExtractCommand(ArCommand *pAr){
6354  const char *zSql1 =
6355    "SELECT "
6356    " ($dir || name),"
6357    " writefile(($dir || name), %s, mode, mtime) "
6358    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6359    " AND name NOT GLOB '*..[/\\]*'";
6360
6361  const char *azExtraArg[] = {
6362    "sqlar_uncompress(data, sz)",
6363    "data"
6364  };
6365
6366  sqlite3_stmt *pSql = 0;
6367  int rc = SQLITE_OK;
6368  char *zDir = 0;
6369  char *zWhere = 0;
6370  int i, j;
6371
6372  /* If arguments are specified, check that they actually exist within
6373  ** the archive before proceeding. And formulate a WHERE clause to
6374  ** match them.  */
6375  rc = arCheckEntries(pAr);
6376  arWhereClause(&rc, pAr, &zWhere);
6377
6378  if( rc==SQLITE_OK ){
6379    if( pAr->zDir ){
6380      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6381    }else{
6382      zDir = sqlite3_mprintf("");
6383    }
6384    if( zDir==0 ) rc = SQLITE_NOMEM;
6385  }
6386
6387  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6388      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6389  );
6390
6391  if( rc==SQLITE_OK ){
6392    j = sqlite3_bind_parameter_index(pSql, "$dir");
6393    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6394
6395    /* Run the SELECT statement twice. The first time, writefile() is called
6396    ** for all archive members that should be extracted. The second time,
6397    ** only for the directories. This is because the timestamps for
6398    ** extracted directories must be reset after they are populated (as
6399    ** populating them changes the timestamp).  */
6400    for(i=0; i<2; i++){
6401      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6402      sqlite3_bind_int(pSql, j, i);
6403      if( pAr->bDryRun ){
6404        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6405      }else{
6406        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6407          if( i==0 && pAr->bVerbose ){
6408            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6409          }
6410        }
6411      }
6412      shellReset(&rc, pSql);
6413    }
6414    shellFinalize(&rc, pSql);
6415  }
6416
6417  sqlite3_free(zDir);
6418  sqlite3_free(zWhere);
6419  return rc;
6420}
6421
6422/*
6423** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6424*/
6425static int arExecSql(ArCommand *pAr, const char *zSql){
6426  int rc;
6427  if( pAr->bDryRun ){
6428    utf8_printf(pAr->p->out, "%s\n", zSql);
6429    rc = SQLITE_OK;
6430  }else{
6431    char *zErr = 0;
6432    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6433    if( zErr ){
6434      utf8_printf(stdout, "ERROR: %s\n", zErr);
6435      sqlite3_free(zErr);
6436    }
6437  }
6438  return rc;
6439}
6440
6441
6442/*
6443** Implementation of .ar "create", "insert", and "update" commands.
6444**
6445**     create    ->     Create a new SQL archive
6446**     insert    ->     Insert or reinsert all files listed
6447**     update    ->     Insert files that have changed or that were not
6448**                      previously in the archive
6449**
6450** Create the "sqlar" table in the database if it does not already exist.
6451** Then add each file in the azFile[] array to the archive. Directories
6452** are added recursively. If argument bVerbose is non-zero, a message is
6453** printed on stdout for each file archived.
6454**
6455** The create command is the same as update, except that it drops
6456** any existing "sqlar" table before beginning.  The "insert" command
6457** always overwrites every file named on the command-line, where as
6458** "update" only overwrites if the size or mtime or mode has changed.
6459*/
6460static int arCreateOrUpdateCommand(
6461  ArCommand *pAr,                 /* Command arguments and options */
6462  int bUpdate,                    /* true for a --create. */
6463  int bOnlyIfChanged              /* Only update if file has changed */
6464){
6465  const char *zCreate =
6466      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6467      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6468      "  mode INT,               -- access permissions\n"
6469      "  mtime INT,              -- last modification time\n"
6470      "  sz INT,                 -- original file size\n"
6471      "  data BLOB               -- compressed content\n"
6472      ")";
6473  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6474  const char *zInsertFmt[2] = {
6475     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6476     "  SELECT\n"
6477     "    %s,\n"
6478     "    mode,\n"
6479     "    mtime,\n"
6480     "    CASE substr(lsmode(mode),1,1)\n"
6481     "      WHEN '-' THEN length(data)\n"
6482     "      WHEN 'd' THEN 0\n"
6483     "      ELSE -1 END,\n"
6484     "    sqlar_compress(data)\n"
6485     "  FROM fsdir(%Q,%Q) AS disk\n"
6486     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6487     ,
6488     "REPLACE INTO %s(name,mode,mtime,data)\n"
6489     "  SELECT\n"
6490     "    %s,\n"
6491     "    mode,\n"
6492     "    mtime,\n"
6493     "    data\n"
6494     "  FROM fsdir(%Q,%Q) AS disk\n"
6495     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6496  };
6497  int i;                          /* For iterating through azFile[] */
6498  int rc;                         /* Return code */
6499  const char *zTab = 0;           /* SQL table into which to insert */
6500  char *zSql;
6501  char zTemp[50];
6502  char *zExists = 0;
6503
6504  arExecSql(pAr, "PRAGMA page_size=512");
6505  rc = arExecSql(pAr, "SAVEPOINT ar;");
6506  if( rc!=SQLITE_OK ) return rc;
6507  zTemp[0] = 0;
6508  if( pAr->bZip ){
6509    /* Initialize the zipfile virtual table, if necessary */
6510    if( pAr->zFile ){
6511      sqlite3_uint64 r;
6512      sqlite3_randomness(sizeof(r),&r);
6513      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6514      zTab = zTemp;
6515      zSql = sqlite3_mprintf(
6516         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6517         zTab, pAr->zFile
6518      );
6519      rc = arExecSql(pAr, zSql);
6520      sqlite3_free(zSql);
6521    }else{
6522      zTab = "zip";
6523    }
6524  }else{
6525    /* Initialize the table for an SQLAR */
6526    zTab = "sqlar";
6527    if( bUpdate==0 ){
6528      rc = arExecSql(pAr, zDrop);
6529      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6530    }
6531    rc = arExecSql(pAr, zCreate);
6532  }
6533  if( bOnlyIfChanged ){
6534    zExists = sqlite3_mprintf(
6535      " AND NOT EXISTS("
6536          "SELECT 1 FROM %s AS mem"
6537          " WHERE mem.name=disk.name"
6538          " AND mem.mtime=disk.mtime"
6539          " AND mem.mode=disk.mode)", zTab);
6540  }else{
6541    zExists = sqlite3_mprintf("");
6542  }
6543  if( zExists==0 ) rc = SQLITE_NOMEM;
6544  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6545    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6546        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6547        pAr->azArg[i], pAr->zDir, zExists);
6548    rc = arExecSql(pAr, zSql2);
6549    sqlite3_free(zSql2);
6550  }
6551end_ar_transaction:
6552  if( rc!=SQLITE_OK ){
6553    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6554  }else{
6555    rc = arExecSql(pAr, "RELEASE ar;");
6556    if( pAr->bZip && pAr->zFile ){
6557      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6558      arExecSql(pAr, zSql);
6559      sqlite3_free(zSql);
6560    }
6561  }
6562  sqlite3_free(zExists);
6563  return rc;
6564}
6565
6566/*
6567** Implementation of ".ar" dot command.
6568*/
6569static int arDotCommand(
6570  ShellState *pState,          /* Current shell tool state */
6571  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6572  char **azArg,                /* Array of arguments passed to dot command */
6573  int nArg                     /* Number of entries in azArg[] */
6574){
6575  ArCommand cmd;
6576  int rc;
6577  memset(&cmd, 0, sizeof(cmd));
6578  cmd.fromCmdLine = fromCmdLine;
6579  rc = arParseCommand(azArg, nArg, &cmd);
6580  if( rc==SQLITE_OK ){
6581    int eDbType = SHELL_OPEN_UNSPEC;
6582    cmd.p = pState;
6583    cmd.db = pState->db;
6584    if( cmd.zFile ){
6585      eDbType = deduceDatabaseType(cmd.zFile, 1);
6586    }else{
6587      eDbType = pState->openMode;
6588    }
6589    if( eDbType==SHELL_OPEN_ZIPFILE ){
6590      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6591        if( cmd.zFile==0 ){
6592          cmd.zSrcTable = sqlite3_mprintf("zip");
6593        }else{
6594          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6595        }
6596      }
6597      cmd.bZip = 1;
6598    }else if( cmd.zFile ){
6599      int flags;
6600      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6601      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6602           || cmd.eCmd==AR_CMD_UPDATE ){
6603        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6604      }else{
6605        flags = SQLITE_OPEN_READONLY;
6606      }
6607      cmd.db = 0;
6608      if( cmd.bDryRun ){
6609        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6610             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6611      }
6612      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6613             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6614      if( rc!=SQLITE_OK ){
6615        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6616            cmd.zFile, sqlite3_errmsg(cmd.db)
6617        );
6618        goto end_ar_command;
6619      }
6620      sqlite3_fileio_init(cmd.db, 0, 0);
6621      sqlite3_sqlar_init(cmd.db, 0, 0);
6622      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6623                              shellPutsFunc, 0, 0);
6624
6625    }
6626    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6627      if( cmd.eCmd!=AR_CMD_CREATE
6628       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6629      ){
6630        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6631        rc = SQLITE_ERROR;
6632        goto end_ar_command;
6633      }
6634      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6635    }
6636
6637    switch( cmd.eCmd ){
6638      case AR_CMD_CREATE:
6639        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6640        break;
6641
6642      case AR_CMD_EXTRACT:
6643        rc = arExtractCommand(&cmd);
6644        break;
6645
6646      case AR_CMD_LIST:
6647        rc = arListCommand(&cmd);
6648        break;
6649
6650      case AR_CMD_HELP:
6651        arUsage(pState->out);
6652        break;
6653
6654      case AR_CMD_INSERT:
6655        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6656        break;
6657
6658      default:
6659        assert( cmd.eCmd==AR_CMD_UPDATE );
6660        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6661        break;
6662    }
6663  }
6664end_ar_command:
6665  if( cmd.db!=pState->db ){
6666    close_db(cmd.db);
6667  }
6668  sqlite3_free(cmd.zSrcTable);
6669
6670  return rc;
6671}
6672/* End of the ".archive" or ".ar" command logic
6673*******************************************************************************/
6674#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6675
6676#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6677/*
6678** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6679** Otherwise, the SQL statement or statements in zSql are executed using
6680** database connection db and the error code written to *pRc before
6681** this function returns.
6682*/
6683static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6684  int rc = *pRc;
6685  if( rc==SQLITE_OK ){
6686    char *zErr = 0;
6687    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6688    if( rc!=SQLITE_OK ){
6689      raw_printf(stderr, "SQL error: %s\n", zErr);
6690    }
6691    *pRc = rc;
6692  }
6693}
6694
6695/*
6696** Like shellExec(), except that zFmt is a printf() style format string.
6697*/
6698static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6699  char *z = 0;
6700  if( *pRc==SQLITE_OK ){
6701    va_list ap;
6702    va_start(ap, zFmt);
6703    z = sqlite3_vmprintf(zFmt, ap);
6704    va_end(ap);
6705    if( z==0 ){
6706      *pRc = SQLITE_NOMEM;
6707    }else{
6708      shellExec(db, pRc, z);
6709    }
6710    sqlite3_free(z);
6711  }
6712}
6713
6714/*
6715** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6716** Otherwise, an attempt is made to allocate, zero and return a pointer
6717** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6718** to SQLITE_NOMEM and NULL returned.
6719*/
6720static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6721  void *pRet = 0;
6722  if( *pRc==SQLITE_OK ){
6723    pRet = sqlite3_malloc64(nByte);
6724    if( pRet==0 ){
6725      *pRc = SQLITE_NOMEM;
6726    }else{
6727      memset(pRet, 0, nByte);
6728    }
6729  }
6730  return pRet;
6731}
6732
6733/*
6734** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6735** Otherwise, zFmt is treated as a printf() style string. The result of
6736** formatting it along with any trailing arguments is written into a
6737** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6738** It is the responsibility of the caller to eventually free this buffer
6739** using a call to sqlite3_free().
6740**
6741** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6742** pointer returned.
6743*/
6744static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6745  char *z = 0;
6746  if( *pRc==SQLITE_OK ){
6747    va_list ap;
6748    va_start(ap, zFmt);
6749    z = sqlite3_vmprintf(zFmt, ap);
6750    va_end(ap);
6751    if( z==0 ){
6752      *pRc = SQLITE_NOMEM;
6753    }
6754  }
6755  return z;
6756}
6757
6758/*
6759** When running the ".recover" command, each output table, and the special
6760** orphaned row table if it is required, is represented by an instance
6761** of the following struct.
6762*/
6763typedef struct RecoverTable RecoverTable;
6764struct RecoverTable {
6765  char *zQuoted;                  /* Quoted version of table name */
6766  int nCol;                       /* Number of columns in table */
6767  char **azlCol;                  /* Array of column lists */
6768  int iPk;                        /* Index of IPK column */
6769};
6770
6771/*
6772** Free a RecoverTable object allocated by recoverFindTable() or
6773** recoverOrphanTable().
6774*/
6775static void recoverFreeTable(RecoverTable *pTab){
6776  if( pTab ){
6777    sqlite3_free(pTab->zQuoted);
6778    if( pTab->azlCol ){
6779      int i;
6780      for(i=0; i<=pTab->nCol; i++){
6781        sqlite3_free(pTab->azlCol[i]);
6782      }
6783      sqlite3_free(pTab->azlCol);
6784    }
6785    sqlite3_free(pTab);
6786  }
6787}
6788
6789/*
6790** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6791** Otherwise, it allocates and returns a RecoverTable object based on the
6792** final four arguments passed to this function. It is the responsibility
6793** of the caller to eventually free the returned object using
6794** recoverFreeTable().
6795*/
6796static RecoverTable *recoverNewTable(
6797  int *pRc,                       /* IN/OUT: Error code */
6798  const char *zName,              /* Name of table */
6799  const char *zSql,               /* CREATE TABLE statement */
6800  int bIntkey,
6801  int nCol
6802){
6803  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6804  int rc = *pRc;
6805  RecoverTable *pTab = 0;
6806
6807  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6808  if( rc==SQLITE_OK ){
6809    int nSqlCol = 0;
6810    int bSqlIntkey = 0;
6811    sqlite3_stmt *pStmt = 0;
6812
6813    rc = sqlite3_open("", &dbtmp);
6814    if( rc==SQLITE_OK ){
6815      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6816                              shellIdQuote, 0, 0);
6817    }
6818    if( rc==SQLITE_OK ){
6819      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6820    }
6821    if( rc==SQLITE_OK ){
6822      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6823      if( rc==SQLITE_ERROR ){
6824        rc = SQLITE_OK;
6825        goto finished;
6826      }
6827    }
6828    shellPreparePrintf(dbtmp, &rc, &pStmt,
6829        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6830    );
6831    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6832      nSqlCol = sqlite3_column_int(pStmt, 0);
6833    }
6834    shellFinalize(&rc, pStmt);
6835
6836    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6837      goto finished;
6838    }
6839
6840    shellPreparePrintf(dbtmp, &rc, &pStmt,
6841      "SELECT ("
6842      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6843      ") FROM sqlite_schema WHERE name = %Q", zName
6844    );
6845    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6846      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6847    }
6848    shellFinalize(&rc, pStmt);
6849
6850    if( bIntkey==bSqlIntkey ){
6851      int i;
6852      const char *zPk = "_rowid_";
6853      sqlite3_stmt *pPkFinder = 0;
6854
6855      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6856      ** set zPk to the name of the PK column, and pTab->iPk to the index
6857      ** of the column, where columns are 0-numbered from left to right.
6858      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6859      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6860      pTab->iPk = -2;
6861      if( bIntkey ){
6862        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6863          "SELECT cid, name FROM pragma_table_info(%Q) "
6864          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6865          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6866          , zName, zName
6867        );
6868        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6869          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6870          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6871        }
6872      }
6873
6874      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6875      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6876      pTab->nCol = nSqlCol;
6877
6878      if( bIntkey ){
6879        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6880      }else{
6881        pTab->azlCol[0] = shellMPrintf(&rc, "");
6882      }
6883      i = 1;
6884      shellPreparePrintf(dbtmp, &rc, &pStmt,
6885          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6886          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6887          "FROM pragma_table_info(%Q)",
6888          bIntkey ? ", " : "", pTab->iPk,
6889          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6890          zName
6891      );
6892      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6893        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6894        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6895        i++;
6896      }
6897      shellFinalize(&rc, pStmt);
6898
6899      shellFinalize(&rc, pPkFinder);
6900    }
6901  }
6902
6903 finished:
6904  sqlite3_close(dbtmp);
6905  *pRc = rc;
6906  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6907    recoverFreeTable(pTab);
6908    pTab = 0;
6909  }
6910  return pTab;
6911}
6912
6913/*
6914** This function is called to search the schema recovered from the
6915** sqlite_schema table of the (possibly) corrupt database as part
6916** of a ".recover" command. Specifically, for a table with root page
6917** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6918** table must be a WITHOUT ROWID table, or if non-zero, not one of
6919** those.
6920**
6921** If a table is found, a (RecoverTable*) object is returned. Or, if
6922** no such table is found, but bIntkey is false and iRoot is the
6923** root page of an index in the recovered schema, then (*pbNoop) is
6924** set to true and NULL returned. Or, if there is no such table or
6925** index, NULL is returned and (*pbNoop) set to 0, indicating that
6926** the caller should write data to the orphans table.
6927*/
6928static RecoverTable *recoverFindTable(
6929  ShellState *pState,             /* Shell state object */
6930  int *pRc,                       /* IN/OUT: Error code */
6931  int iRoot,                      /* Root page of table */
6932  int bIntkey,                    /* True for an intkey table */
6933  int nCol,                       /* Number of columns in table */
6934  int *pbNoop                     /* OUT: True if iRoot is root of index */
6935){
6936  sqlite3_stmt *pStmt = 0;
6937  RecoverTable *pRet = 0;
6938  int bNoop = 0;
6939  const char *zSql = 0;
6940  const char *zName = 0;
6941
6942  /* Search the recovered schema for an object with root page iRoot. */
6943  shellPreparePrintf(pState->db, pRc, &pStmt,
6944      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6945  );
6946  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6947    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6948    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6949      bNoop = 1;
6950      break;
6951    }
6952    if( sqlite3_stricmp(zType, "table")==0 ){
6953      zName = (const char*)sqlite3_column_text(pStmt, 1);
6954      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6955      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6956      break;
6957    }
6958  }
6959
6960  shellFinalize(pRc, pStmt);
6961  *pbNoop = bNoop;
6962  return pRet;
6963}
6964
6965/*
6966** Return a RecoverTable object representing the orphans table.
6967*/
6968static RecoverTable *recoverOrphanTable(
6969  ShellState *pState,             /* Shell state object */
6970  int *pRc,                       /* IN/OUT: Error code */
6971  const char *zLostAndFound,      /* Base name for orphans table */
6972  int nCol                        /* Number of user data columns */
6973){
6974  RecoverTable *pTab = 0;
6975  if( nCol>=0 && *pRc==SQLITE_OK ){
6976    int i;
6977
6978    /* This block determines the name of the orphan table. The prefered
6979    ** name is zLostAndFound. But if that clashes with another name
6980    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6981    ** and so on until a non-clashing name is found.  */
6982    int iTab = 0;
6983    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6984    sqlite3_stmt *pTest = 0;
6985    shellPrepare(pState->db, pRc,
6986        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
6987    );
6988    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6989    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
6990      shellReset(pRc, pTest);
6991      sqlite3_free(zTab);
6992      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
6993      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6994    }
6995    shellFinalize(pRc, pTest);
6996
6997    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
6998    if( pTab ){
6999      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7000      pTab->nCol = nCol;
7001      pTab->iPk = -2;
7002      if( nCol>0 ){
7003        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7004        if( pTab->azlCol ){
7005          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7006          for(i=nCol-1; i>=0; i--){
7007            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7008          }
7009        }
7010      }
7011
7012      if( *pRc!=SQLITE_OK ){
7013        recoverFreeTable(pTab);
7014        pTab = 0;
7015      }else{
7016        raw_printf(pState->out,
7017            "CREATE TABLE %s(rootpgno INTEGER, "
7018            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7019        );
7020        for(i=0; i<nCol; i++){
7021          raw_printf(pState->out, ", c%d", i);
7022        }
7023        raw_printf(pState->out, ");\n");
7024      }
7025    }
7026    sqlite3_free(zTab);
7027  }
7028  return pTab;
7029}
7030
7031/*
7032** This function is called to recover data from the database. A script
7033** to construct a new database containing all recovered data is output
7034** on stream pState->out.
7035*/
7036static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7037  int rc = SQLITE_OK;
7038  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7039  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7040  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7041  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7042  const char *zLostAndFound = "lost_and_found";
7043  int i;
7044  int nOrphan = -1;
7045  RecoverTable *pOrphan = 0;
7046
7047  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7048  int bRowids = 1;                /* 0 if --no-rowids */
7049  for(i=1; i<nArg; i++){
7050    char *z = azArg[i];
7051    int n;
7052    if( z[0]=='-' && z[1]=='-' ) z++;
7053    n = strlen30(z);
7054    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7055      bFreelist = 0;
7056    }else
7057    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7058      i++;
7059      zRecoveryDb = azArg[i];
7060    }else
7061    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7062      i++;
7063      zLostAndFound = azArg[i];
7064    }else
7065    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7066      bRowids = 0;
7067    }
7068    else{
7069      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7070      showHelp(pState->out, azArg[0]);
7071      return 1;
7072    }
7073  }
7074
7075  shellExecPrintf(pState->db, &rc,
7076    /* Attach an in-memory database named 'recovery'. Create an indexed
7077    ** cache of the sqlite_dbptr virtual table. */
7078    "PRAGMA writable_schema = on;"
7079    "ATTACH %Q AS recovery;"
7080    "DROP TABLE IF EXISTS recovery.dbptr;"
7081    "DROP TABLE IF EXISTS recovery.freelist;"
7082    "DROP TABLE IF EXISTS recovery.map;"
7083    "DROP TABLE IF EXISTS recovery.schema;"
7084    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7085  );
7086
7087  if( bFreelist ){
7088    shellExec(pState->db, &rc,
7089      "WITH trunk(pgno) AS ("
7090      "  SELECT shell_int32("
7091      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7092      "      WHERE x>0"
7093      "    UNION"
7094      "  SELECT shell_int32("
7095      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7096      "      FROM trunk WHERE x>0"
7097      "),"
7098      "freelist(data, n, freepgno) AS ("
7099      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7100      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7101      "    UNION ALL"
7102      "  SELECT data, n-1, shell_int32(data, 2+n) "
7103      "      FROM freelist WHERE n>=0"
7104      ")"
7105      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7106    );
7107  }
7108
7109  /* If this is an auto-vacuum database, add all pointer-map pages to
7110  ** the freelist table. Do this regardless of whether or not
7111  ** --freelist-corrupt was specified.  */
7112  shellExec(pState->db, &rc,
7113    "WITH ptrmap(pgno) AS ("
7114    "  SELECT 2 WHERE shell_int32("
7115    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7116    "  )"
7117    "    UNION ALL "
7118    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7119    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7120    ")"
7121    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7122  );
7123
7124  shellExec(pState->db, &rc,
7125    "CREATE TABLE recovery.dbptr("
7126    "      pgno, child, PRIMARY KEY(child, pgno)"
7127    ") WITHOUT ROWID;"
7128    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7129    "    SELECT * FROM sqlite_dbptr"
7130    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7131
7132    /* Delete any pointer to page 1. This ensures that page 1 is considered
7133    ** a root page, regardless of how corrupt the db is. */
7134    "DELETE FROM recovery.dbptr WHERE child = 1;"
7135
7136    /* Delete all pointers to any pages that have more than one pointer
7137    ** to them. Such pages will be treated as root pages when recovering
7138    ** data.  */
7139    "DELETE FROM recovery.dbptr WHERE child IN ("
7140    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7141    ");"
7142
7143    /* Create the "map" table that will (eventually) contain instructions
7144    ** for dealing with each page in the db that contains one or more
7145    ** records. */
7146    "CREATE TABLE recovery.map("
7147      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7148    ");"
7149
7150    /* Populate table [map]. If there are circular loops of pages in the
7151    ** database, the following adds all pages in such a loop to the map
7152    ** as individual root pages. This could be handled better.  */
7153    "WITH pages(i, maxlen) AS ("
7154    "  SELECT page_count, ("
7155    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7156    "  ) FROM pragma_page_count WHERE page_count>0"
7157    "    UNION ALL"
7158    "  SELECT i-1, ("
7159    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7160    "  ) FROM pages WHERE i>=2"
7161    ")"
7162    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7163    "  SELECT i, maxlen, NULL, ("
7164    "    WITH p(orig, pgno, parent) AS ("
7165    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7166    "        UNION "
7167    "      SELECT i, p.parent, "
7168    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7169    "    )"
7170    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7171    ") "
7172    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7173    "UPDATE recovery.map AS o SET intkey = ("
7174    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7175    ");"
7176
7177    /* Extract data from page 1 and any linked pages into table
7178    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7179    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7180    "INSERT INTO recovery.schema SELECT "
7181    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7182    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7183    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7184    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7185    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7186    "FROM sqlite_dbdata WHERE pgno IN ("
7187    "  SELECT pgno FROM recovery.map WHERE root=1"
7188    ")"
7189    "GROUP BY pgno, cell;"
7190    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7191  );
7192
7193  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7194  ** CREATE TABLE statements that extracted from the existing schema.  */
7195  if( rc==SQLITE_OK ){
7196    sqlite3_stmt *pStmt = 0;
7197    /* ".recover" might output content in an order which causes immediate
7198    ** foreign key constraints to be violated. So disable foreign-key
7199    ** constraint enforcement to prevent problems when running the output
7200    ** script. */
7201    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7202    raw_printf(pState->out, "BEGIN;\n");
7203    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7204    shellPrepare(pState->db, &rc,
7205        "SELECT sql FROM recovery.schema "
7206        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7207    );
7208    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7209      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7210      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7211          &zCreateTable[12]
7212      );
7213    }
7214    shellFinalize(&rc, pStmt);
7215  }
7216
7217  /* Figure out if an orphan table will be required. And if so, how many
7218  ** user columns it should contain */
7219  shellPrepare(pState->db, &rc,
7220      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7221      , &pLoop
7222  );
7223  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7224    nOrphan = sqlite3_column_int(pLoop, 0);
7225  }
7226  shellFinalize(&rc, pLoop);
7227  pLoop = 0;
7228
7229  shellPrepare(pState->db, &rc,
7230      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7231  );
7232
7233  shellPrepare(pState->db, &rc,
7234      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7235      "(case when (? AND field<0) then NULL else value end)"
7236      "), ', ')"
7237      ", min(field) "
7238      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7239      "GROUP BY cell", &pCells
7240  );
7241
7242  /* Loop through each root page. */
7243  shellPrepare(pState->db, &rc,
7244      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7245      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7246      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7247      ")", &pLoop
7248  );
7249  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7250    int iRoot = sqlite3_column_int(pLoop, 0);
7251    int bIntkey = sqlite3_column_int(pLoop, 1);
7252    int nCol = sqlite3_column_int(pLoop, 2);
7253    int bNoop = 0;
7254    RecoverTable *pTab;
7255
7256    assert( bIntkey==0 || bIntkey==1 );
7257    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7258    if( bNoop || rc ) continue;
7259    if( pTab==0 ){
7260      if( pOrphan==0 ){
7261        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7262      }
7263      pTab = pOrphan;
7264      if( pTab==0 ) break;
7265    }
7266
7267    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7268      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7269    }
7270    sqlite3_bind_int(pPages, 1, iRoot);
7271    if( bRowids==0 && pTab->iPk<0 ){
7272      sqlite3_bind_int(pCells, 1, 1);
7273    }else{
7274      sqlite3_bind_int(pCells, 1, 0);
7275    }
7276    sqlite3_bind_int(pCells, 3, pTab->iPk);
7277
7278    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7279      int iPgno = sqlite3_column_int(pPages, 0);
7280      sqlite3_bind_int(pCells, 2, iPgno);
7281      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7282        int nField = sqlite3_column_int(pCells, 0);
7283        int iMin = sqlite3_column_int(pCells, 2);
7284        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7285
7286        RecoverTable *pTab2 = pTab;
7287        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7288          if( pOrphan==0 ){
7289            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7290          }
7291          pTab2 = pOrphan;
7292          if( pTab2==0 ) break;
7293        }
7294
7295        nField = nField+1;
7296        if( pTab2==pOrphan ){
7297          raw_printf(pState->out,
7298              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7299              pTab2->zQuoted, iRoot, iPgno, nField,
7300              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7301          );
7302        }else{
7303          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7304              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7305          );
7306        }
7307      }
7308      shellReset(&rc, pCells);
7309    }
7310    shellReset(&rc, pPages);
7311    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7312  }
7313  shellFinalize(&rc, pLoop);
7314  shellFinalize(&rc, pPages);
7315  shellFinalize(&rc, pCells);
7316  recoverFreeTable(pOrphan);
7317
7318  /* The rest of the schema */
7319  if( rc==SQLITE_OK ){
7320    sqlite3_stmt *pStmt = 0;
7321    shellPrepare(pState->db, &rc,
7322        "SELECT sql, name FROM recovery.schema "
7323        "WHERE sql NOT LIKE 'create table%'", &pStmt
7324    );
7325    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7326      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7327      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7328        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7329        char *zPrint = shellMPrintf(&rc,
7330          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7331          zName, zName, zSql
7332        );
7333        raw_printf(pState->out, "%s;\n", zPrint);
7334        sqlite3_free(zPrint);
7335      }else{
7336        raw_printf(pState->out, "%s;\n", zSql);
7337      }
7338    }
7339    shellFinalize(&rc, pStmt);
7340  }
7341
7342  if( rc==SQLITE_OK ){
7343    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7344    raw_printf(pState->out, "COMMIT;\n");
7345  }
7346  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7347  return rc;
7348}
7349#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7350
7351
7352/*
7353** If an input line begins with "." then invoke this routine to
7354** process that line.
7355**
7356** Return 1 on error, 2 to exit, and 0 otherwise.
7357*/
7358static int do_meta_command(char *zLine, ShellState *p){
7359  int h = 1;
7360  int nArg = 0;
7361  int n, c;
7362  int rc = 0;
7363  char *azArg[52];
7364
7365#ifndef SQLITE_OMIT_VIRTUALTABLE
7366  if( p->expert.pExpert ){
7367    expertFinish(p, 1, 0);
7368  }
7369#endif
7370
7371  /* Parse the input line into tokens.
7372  */
7373  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7374    while( IsSpace(zLine[h]) ){ h++; }
7375    if( zLine[h]==0 ) break;
7376    if( zLine[h]=='\'' || zLine[h]=='"' ){
7377      int delim = zLine[h++];
7378      azArg[nArg++] = &zLine[h];
7379      while( zLine[h] && zLine[h]!=delim ){
7380        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7381        h++;
7382      }
7383      if( zLine[h]==delim ){
7384        zLine[h++] = 0;
7385      }
7386      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7387    }else{
7388      azArg[nArg++] = &zLine[h];
7389      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7390      if( zLine[h] ) zLine[h++] = 0;
7391      resolve_backslashes(azArg[nArg-1]);
7392    }
7393  }
7394  azArg[nArg] = 0;
7395
7396  /* Process the input line.
7397  */
7398  if( nArg==0 ) return 0; /* no tokens, no error */
7399  n = strlen30(azArg[0]);
7400  c = azArg[0][0];
7401  clearTempFile(p);
7402
7403#ifndef SQLITE_OMIT_AUTHORIZATION
7404  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7405    if( nArg!=2 ){
7406      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7407      rc = 1;
7408      goto meta_command_exit;
7409    }
7410    open_db(p, 0);
7411    if( booleanValue(azArg[1]) ){
7412      sqlite3_set_authorizer(p->db, shellAuth, p);
7413    }else{
7414      sqlite3_set_authorizer(p->db, 0, 0);
7415    }
7416  }else
7417#endif
7418
7419#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7420  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7421    open_db(p, 0);
7422    rc = arDotCommand(p, 0, azArg, nArg);
7423  }else
7424#endif
7425
7426  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7427   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7428  ){
7429    const char *zDestFile = 0;
7430    const char *zDb = 0;
7431    sqlite3 *pDest;
7432    sqlite3_backup *pBackup;
7433    int j;
7434    int bAsync = 0;
7435    const char *zVfs = 0;
7436    for(j=1; j<nArg; j++){
7437      const char *z = azArg[j];
7438      if( z[0]=='-' ){
7439        if( z[1]=='-' ) z++;
7440        if( strcmp(z, "-append")==0 ){
7441          zVfs = "apndvfs";
7442        }else
7443        if( strcmp(z, "-async")==0 ){
7444          bAsync = 1;
7445        }else
7446        {
7447          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7448          return 1;
7449        }
7450      }else if( zDestFile==0 ){
7451        zDestFile = azArg[j];
7452      }else if( zDb==0 ){
7453        zDb = zDestFile;
7454        zDestFile = azArg[j];
7455      }else{
7456        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7457        return 1;
7458      }
7459    }
7460    if( zDestFile==0 ){
7461      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7462      return 1;
7463    }
7464    if( zDb==0 ) zDb = "main";
7465    rc = sqlite3_open_v2(zDestFile, &pDest,
7466                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7467    if( rc!=SQLITE_OK ){
7468      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7469      close_db(pDest);
7470      return 1;
7471    }
7472    if( bAsync ){
7473      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7474                   0, 0, 0);
7475    }
7476    open_db(p, 0);
7477    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7478    if( pBackup==0 ){
7479      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7480      close_db(pDest);
7481      return 1;
7482    }
7483    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7484    sqlite3_backup_finish(pBackup);
7485    if( rc==SQLITE_DONE ){
7486      rc = 0;
7487    }else{
7488      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7489      rc = 1;
7490    }
7491    close_db(pDest);
7492  }else
7493
7494  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7495    if( nArg==2 ){
7496      bail_on_error = booleanValue(azArg[1]);
7497    }else{
7498      raw_printf(stderr, "Usage: .bail on|off\n");
7499      rc = 1;
7500    }
7501  }else
7502
7503  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7504    if( nArg==2 ){
7505      if( booleanValue(azArg[1]) ){
7506        setBinaryMode(p->out, 1);
7507      }else{
7508        setTextMode(p->out, 1);
7509      }
7510    }else{
7511      raw_printf(stderr, "Usage: .binary on|off\n");
7512      rc = 1;
7513    }
7514  }else
7515
7516  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7517    if( nArg==2 ){
7518#if defined(_WIN32) || defined(WIN32)
7519      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7520      rc = !SetCurrentDirectoryW(z);
7521      sqlite3_free(z);
7522#else
7523      rc = chdir(azArg[1]);
7524#endif
7525      if( rc ){
7526        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7527        rc = 1;
7528      }
7529    }else{
7530      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7531      rc = 1;
7532    }
7533  }else
7534
7535  /* The undocumented ".breakpoint" command causes a call to the no-op
7536  ** routine named test_breakpoint().
7537  */
7538  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7539    test_breakpoint();
7540  }else
7541
7542  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7543    if( nArg==2 ){
7544      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7545    }else{
7546      raw_printf(stderr, "Usage: .changes on|off\n");
7547      rc = 1;
7548    }
7549  }else
7550
7551  /* Cancel output redirection, if it is currently set (by .testcase)
7552  ** Then read the content of the testcase-out.txt file and compare against
7553  ** azArg[1].  If there are differences, report an error and exit.
7554  */
7555  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7556    char *zRes = 0;
7557    output_reset(p);
7558    if( nArg!=2 ){
7559      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7560      rc = 2;
7561    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7562      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7563      rc = 2;
7564    }else if( testcase_glob(azArg[1],zRes)==0 ){
7565      utf8_printf(stderr,
7566                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7567                 p->zTestcase, azArg[1], zRes);
7568      rc = 1;
7569    }else{
7570      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7571      p->nCheck++;
7572    }
7573    sqlite3_free(zRes);
7574  }else
7575
7576  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7577    if( nArg==2 ){
7578      tryToClone(p, azArg[1]);
7579    }else{
7580      raw_printf(stderr, "Usage: .clone FILENAME\n");
7581      rc = 1;
7582    }
7583  }else
7584
7585  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7586    ShellState data;
7587    char *zErrMsg = 0;
7588    open_db(p, 0);
7589    memcpy(&data, p, sizeof(data));
7590    data.showHeader = 0;
7591    data.cMode = data.mode = MODE_List;
7592    sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
7593    data.cnt = 0;
7594    sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
7595                 callback, &data, &zErrMsg);
7596    if( zErrMsg ){
7597      utf8_printf(stderr,"Error: %s\n", zErrMsg);
7598      sqlite3_free(zErrMsg);
7599      rc = 1;
7600    }
7601  }else
7602
7603  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7604    static const struct DbConfigChoices {
7605      const char *zName;
7606      int op;
7607    } aDbConfig[] = {
7608        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7609        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7610        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7611        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7612        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7613        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7614        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7615        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7616        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7617        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7618        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7619        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7620        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7621        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7622        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7623        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7624    };
7625    int ii, v;
7626    open_db(p, 0);
7627    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7628      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7629      if( nArg>=3 ){
7630        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7631      }
7632      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7633      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7634      if( nArg>1 ) break;
7635    }
7636    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7637      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7638      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7639    }
7640  }else
7641
7642  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7643    rc = shell_dbinfo_command(p, nArg, azArg);
7644  }else
7645
7646#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7647  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7648    open_db(p, 0);
7649    rc = recoverDatabaseCmd(p, nArg, azArg);
7650  }else
7651#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7652
7653  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7654    char *zLike = 0;
7655    char *zSql;
7656    int i;
7657    int savedShowHeader = p->showHeader;
7658    int savedShellFlags = p->shellFlgs;
7659    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
7660    for(i=1; i<nArg; i++){
7661      if( azArg[i][0]=='-' ){
7662        const char *z = azArg[i]+1;
7663        if( z[0]=='-' ) z++;
7664        if( strcmp(z,"preserve-rowids")==0 ){
7665#ifdef SQLITE_OMIT_VIRTUALTABLE
7666          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7667                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7668          rc = 1;
7669          sqlite3_free(zLike);
7670          goto meta_command_exit;
7671#else
7672          ShellSetFlag(p, SHFLG_PreserveRowid);
7673#endif
7674        }else
7675        if( strcmp(z,"newlines")==0 ){
7676          ShellSetFlag(p, SHFLG_Newlines);
7677        }else
7678        {
7679          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7680          rc = 1;
7681          sqlite3_free(zLike);
7682          goto meta_command_exit;
7683        }
7684      }else if( zLike ){
7685        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
7686                zLike, azArg[i]);
7687      }else{
7688        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
7689      }
7690    }
7691
7692    open_db(p, 0);
7693
7694    /* When playing back a "dump", the content might appear in an order
7695    ** which causes immediate foreign key constraints to be violated.
7696    ** So disable foreign-key constraint enforcement to prevent problems. */
7697    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7698    raw_printf(p->out, "BEGIN TRANSACTION;\n");
7699    p->writableSchema = 0;
7700    p->showHeader = 0;
7701    /* Set writable_schema=ON since doing so forces SQLite to initialize
7702    ** as much of the schema as it can even if the sqlite_schema table is
7703    ** corrupt. */
7704    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7705    p->nErr = 0;
7706    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7707    zSql = sqlite3_mprintf(
7708      "SELECT name, type, sql FROM sqlite_schema "
7709      "WHERE (%s) AND type=='table'"
7710      "  AND sql NOT NULL"
7711      " ORDER BY tbl_name='sqlite_sequence', rowid",
7712      zLike
7713    );
7714    run_schema_dump_query(p,zSql);
7715    sqlite3_free(zSql);
7716    zSql = sqlite3_mprintf(
7717      "SELECT sql FROM sqlite_schema "
7718      "WHERE (%s) AND sql NOT NULL"
7719      "  AND type IN ('index','trigger','view')",
7720      zLike
7721    );
7722    run_table_dump_query(p, zSql);
7723    sqlite3_free(zSql);
7724    sqlite3_free(zLike);
7725    if( p->writableSchema ){
7726      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7727      p->writableSchema = 0;
7728    }
7729    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7730    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7731    raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7732    p->showHeader = savedShowHeader;
7733    p->shellFlgs = savedShellFlags;
7734  }else
7735
7736  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7737    if( nArg==2 ){
7738      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7739    }else{
7740      raw_printf(stderr, "Usage: .echo on|off\n");
7741      rc = 1;
7742    }
7743  }else
7744
7745  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7746    if( nArg==2 ){
7747      p->autoEQPtest = 0;
7748      if( p->autoEQPtrace ){
7749        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7750        p->autoEQPtrace = 0;
7751      }
7752      if( strcmp(azArg[1],"full")==0 ){
7753        p->autoEQP = AUTOEQP_full;
7754      }else if( strcmp(azArg[1],"trigger")==0 ){
7755        p->autoEQP = AUTOEQP_trigger;
7756#ifdef SQLITE_DEBUG
7757      }else if( strcmp(azArg[1],"test")==0 ){
7758        p->autoEQP = AUTOEQP_on;
7759        p->autoEQPtest = 1;
7760      }else if( strcmp(azArg[1],"trace")==0 ){
7761        p->autoEQP = AUTOEQP_full;
7762        p->autoEQPtrace = 1;
7763        open_db(p, 0);
7764        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7765        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7766#endif
7767      }else{
7768        p->autoEQP = (u8)booleanValue(azArg[1]);
7769      }
7770    }else{
7771      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7772      rc = 1;
7773    }
7774  }else
7775
7776  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7777    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7778    rc = 2;
7779  }else
7780
7781  /* The ".explain" command is automatic now.  It is largely pointless.  It
7782  ** retained purely for backwards compatibility */
7783  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7784    int val = 1;
7785    if( nArg>=2 ){
7786      if( strcmp(azArg[1],"auto")==0 ){
7787        val = 99;
7788      }else{
7789        val =  booleanValue(azArg[1]);
7790      }
7791    }
7792    if( val==1 && p->mode!=MODE_Explain ){
7793      p->normalMode = p->mode;
7794      p->mode = MODE_Explain;
7795      p->autoExplain = 0;
7796    }else if( val==0 ){
7797      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7798      p->autoExplain = 0;
7799    }else if( val==99 ){
7800      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7801      p->autoExplain = 1;
7802    }
7803  }else
7804
7805#ifndef SQLITE_OMIT_VIRTUALTABLE
7806  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7807    open_db(p, 0);
7808    expertDotCommand(p, azArg, nArg);
7809  }else
7810#endif
7811
7812  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7813    static const struct {
7814       const char *zCtrlName;   /* Name of a test-control option */
7815       int ctrlCode;            /* Integer code for that option */
7816       const char *zUsage;      /* Usage notes */
7817    } aCtrl[] = {
7818      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7819      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7820   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7821      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7822      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7823   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7824      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7825      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7826      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7827      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7828    };
7829    int filectrl = -1;
7830    int iCtrl = -1;
7831    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7832    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7833    int n2, i;
7834    const char *zCmd = 0;
7835    const char *zSchema = 0;
7836
7837    open_db(p, 0);
7838    zCmd = nArg>=2 ? azArg[1] : "help";
7839
7840    if( zCmd[0]=='-'
7841     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7842     && nArg>=4
7843    ){
7844      zSchema = azArg[2];
7845      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7846      nArg -= 2;
7847      zCmd = azArg[1];
7848    }
7849
7850    /* The argument can optionally begin with "-" or "--" */
7851    if( zCmd[0]=='-' && zCmd[1] ){
7852      zCmd++;
7853      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7854    }
7855
7856    /* --help lists all file-controls */
7857    if( strcmp(zCmd,"help")==0 ){
7858      utf8_printf(p->out, "Available file-controls:\n");
7859      for(i=0; i<ArraySize(aCtrl); i++){
7860        utf8_printf(p->out, "  .filectrl %s %s\n",
7861                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7862      }
7863      rc = 1;
7864      goto meta_command_exit;
7865    }
7866
7867    /* convert filectrl text option to value. allow any unique prefix
7868    ** of the option name, or a numerical value. */
7869    n2 = strlen30(zCmd);
7870    for(i=0; i<ArraySize(aCtrl); i++){
7871      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7872        if( filectrl<0 ){
7873          filectrl = aCtrl[i].ctrlCode;
7874          iCtrl = i;
7875        }else{
7876          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7877                              "Use \".filectrl --help\" for help\n", zCmd);
7878          rc = 1;
7879          goto meta_command_exit;
7880        }
7881      }
7882    }
7883    if( filectrl<0 ){
7884      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7885                         "Use \".filectrl --help\" for help\n", zCmd);
7886    }else{
7887      switch(filectrl){
7888        case SQLITE_FCNTL_SIZE_LIMIT: {
7889          if( nArg!=2 && nArg!=3 ) break;
7890          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7891          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7892          isOk = 1;
7893          break;
7894        }
7895        case SQLITE_FCNTL_LOCK_TIMEOUT:
7896        case SQLITE_FCNTL_CHUNK_SIZE: {
7897          int x;
7898          if( nArg!=3 ) break;
7899          x = (int)integerValue(azArg[2]);
7900          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7901          isOk = 2;
7902          break;
7903        }
7904        case SQLITE_FCNTL_PERSIST_WAL:
7905        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7906          int x;
7907          if( nArg!=2 && nArg!=3 ) break;
7908          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7909          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7910          iRes = x;
7911          isOk = 1;
7912          break;
7913        }
7914        case SQLITE_FCNTL_HAS_MOVED: {
7915          int x;
7916          if( nArg!=2 ) break;
7917          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7918          iRes = x;
7919          isOk = 1;
7920          break;
7921        }
7922        case SQLITE_FCNTL_TEMPFILENAME: {
7923          char *z = 0;
7924          if( nArg!=2 ) break;
7925          sqlite3_file_control(p->db, zSchema, filectrl, &z);
7926          if( z ){
7927            utf8_printf(p->out, "%s\n", z);
7928            sqlite3_free(z);
7929          }
7930          isOk = 2;
7931          break;
7932        }
7933        case SQLITE_FCNTL_RESERVE_BYTES: {
7934          int x;
7935          if( nArg>=3 ){
7936            x = atoi(azArg[2]);
7937            sqlite3_file_control(p->db, zSchema, filectrl, &x);
7938          }
7939          x = -1;
7940          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7941          utf8_printf(p->out,"%d\n", x);
7942          isOk = 2;
7943          break;
7944        }
7945      }
7946    }
7947    if( isOk==0 && iCtrl>=0 ){
7948      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7949      rc = 1;
7950    }else if( isOk==1 ){
7951      char zBuf[100];
7952      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
7953      raw_printf(p->out, "%s\n", zBuf);
7954    }
7955  }else
7956
7957  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
7958    ShellState data;
7959    char *zErrMsg = 0;
7960    int doStats = 0;
7961    memcpy(&data, p, sizeof(data));
7962    data.showHeader = 0;
7963    data.cMode = data.mode = MODE_Semi;
7964    if( nArg==2 && optionMatch(azArg[1], "indent") ){
7965      data.cMode = data.mode = MODE_Pretty;
7966      nArg = 1;
7967    }
7968    if( nArg!=1 ){
7969      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
7970      rc = 1;
7971      goto meta_command_exit;
7972    }
7973    open_db(p, 0);
7974    rc = sqlite3_exec(p->db,
7975       "SELECT sql FROM"
7976       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
7977       "     FROM sqlite_schema UNION ALL"
7978       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
7979       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
7980       "ORDER BY rowid",
7981       callback, &data, &zErrMsg
7982    );
7983    if( rc==SQLITE_OK ){
7984      sqlite3_stmt *pStmt;
7985      rc = sqlite3_prepare_v2(p->db,
7986               "SELECT rowid FROM sqlite_schema"
7987               " WHERE name GLOB 'sqlite_stat[134]'",
7988               -1, &pStmt, 0);
7989      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
7990      sqlite3_finalize(pStmt);
7991    }
7992    if( doStats==0 ){
7993      raw_printf(p->out, "/* No STAT tables available */\n");
7994    }else{
7995      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
7996      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
7997                   callback, &data, &zErrMsg);
7998      data.cMode = data.mode = MODE_Insert;
7999      data.zDestTable = "sqlite_stat1";
8000      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
8001      data.zDestTable = "sqlite_stat4";
8002      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
8003      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8004    }
8005  }else
8006
8007  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8008    if( nArg==2 ){
8009      p->showHeader = booleanValue(azArg[1]);
8010      p->shellFlgs |= SHFLG_HeaderSet;
8011    }else{
8012      raw_printf(stderr, "Usage: .headers on|off\n");
8013      rc = 1;
8014    }
8015  }else
8016
8017  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8018    if( nArg>=2 ){
8019      n = showHelp(p->out, azArg[1]);
8020      if( n==0 ){
8021        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8022      }
8023    }else{
8024      showHelp(p->out, 0);
8025    }
8026  }else
8027
8028  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8029    char *zTable = 0;           /* Insert data into this table */
8030    char *zFile = 0;            /* Name of file to extra content from */
8031    sqlite3_stmt *pStmt = NULL; /* A statement */
8032    int nCol;                   /* Number of columns in the table */
8033    int nByte;                  /* Number of bytes in an SQL string */
8034    int i, j;                   /* Loop counters */
8035    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8036    int nSep;                   /* Number of bytes in p->colSeparator[] */
8037    char *zSql;                 /* An SQL statement */
8038    ImportCtx sCtx;             /* Reader context */
8039    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8040    int eVerbose = 0;           /* Larger for more console output */
8041    int nSkip = 0;              /* Initial lines to skip */
8042    int useOutputMode = 1;      /* Use output mode to determine separators */
8043
8044    memset(&sCtx, 0, sizeof(sCtx));
8045    if( p->mode==MODE_Ascii ){
8046      xRead = ascii_read_one_field;
8047    }else{
8048      xRead = csv_read_one_field;
8049    }
8050    for(i=1; i<nArg; i++){
8051      char *z = azArg[i];
8052      if( z[0]=='-' && z[1]=='-' ) z++;
8053      if( z[0]!='-' ){
8054        if( zFile==0 ){
8055          zFile = z;
8056        }else if( zTable==0 ){
8057          zTable = z;
8058        }else{
8059          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8060          showHelp(p->out, "import");
8061          rc = 1;
8062          goto meta_command_exit;
8063        }
8064      }else if( strcmp(z,"-v")==0 ){
8065        eVerbose++;
8066      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8067        nSkip = integerValue(azArg[++i]);
8068      }else if( strcmp(z,"-ascii")==0 ){
8069        sCtx.cColSep = SEP_Unit[0];
8070        sCtx.cRowSep = SEP_Record[0];
8071        xRead = ascii_read_one_field;
8072        useOutputMode = 0;
8073      }else if( strcmp(z,"-csv")==0 ){
8074        sCtx.cColSep = ',';
8075        sCtx.cRowSep = '\n';
8076        xRead = csv_read_one_field;
8077        useOutputMode = 0;
8078      }else{
8079        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8080        showHelp(p->out, "import");
8081        rc = 1;
8082        goto meta_command_exit;
8083      }
8084    }
8085    if( zTable==0 ){
8086      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8087                  zFile==0 ? "FILE" : "TABLE");
8088      showHelp(p->out, "import");
8089      rc = 1;
8090      goto meta_command_exit;
8091    }
8092    seenInterrupt = 0;
8093    open_db(p, 0);
8094    if( useOutputMode ){
8095      /* If neither the --csv or --ascii options are specified, then set
8096      ** the column and row separator characters from the output mode. */
8097      nSep = strlen30(p->colSeparator);
8098      if( nSep==0 ){
8099        raw_printf(stderr,
8100                   "Error: non-null column separator required for import\n");
8101        rc = 1;
8102        goto meta_command_exit;
8103      }
8104      if( nSep>1 ){
8105        raw_printf(stderr,
8106              "Error: multi-character column separators not allowed"
8107              " for import\n");
8108        rc = 1;
8109        goto meta_command_exit;
8110      }
8111      nSep = strlen30(p->rowSeparator);
8112      if( nSep==0 ){
8113        raw_printf(stderr,
8114            "Error: non-null row separator required for import\n");
8115        rc = 1;
8116        goto meta_command_exit;
8117      }
8118      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8119        /* When importing CSV (only), if the row separator is set to the
8120        ** default output row separator, change it to the default input
8121        ** row separator.  This avoids having to maintain different input
8122        ** and output row separators. */
8123        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8124        nSep = strlen30(p->rowSeparator);
8125      }
8126      if( nSep>1 ){
8127        raw_printf(stderr, "Error: multi-character row separators not allowed"
8128                           " for import\n");
8129        rc = 1;
8130        goto meta_command_exit;
8131      }
8132      sCtx.cColSep = p->colSeparator[0];
8133      sCtx.cRowSep = p->rowSeparator[0];
8134    }
8135    sCtx.zFile = zFile;
8136    sCtx.nLine = 1;
8137    if( sCtx.zFile[0]=='|' ){
8138#ifdef SQLITE_OMIT_POPEN
8139      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8140      rc = 1;
8141      goto meta_command_exit;
8142#else
8143      sCtx.in = popen(sCtx.zFile+1, "r");
8144      sCtx.zFile = "<pipe>";
8145      sCtx.xCloser = pclose;
8146#endif
8147    }else{
8148      sCtx.in = fopen(sCtx.zFile, "rb");
8149      sCtx.xCloser = fclose;
8150    }
8151    if( sCtx.in==0 ){
8152      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8153      rc = 1;
8154      goto meta_command_exit;
8155    }
8156    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8157      char zSep[2];
8158      zSep[1] = 0;
8159      zSep[0] = sCtx.cColSep;
8160      utf8_printf(p->out, "Column separator ");
8161      output_c_string(p->out, zSep);
8162      utf8_printf(p->out, ", row separator ");
8163      zSep[0] = sCtx.cRowSep;
8164      output_c_string(p->out, zSep);
8165      utf8_printf(p->out, "\n");
8166    }
8167    while( (nSkip--)>0 ){
8168      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8169    }
8170    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
8171    if( zSql==0 ){
8172      import_cleanup(&sCtx);
8173      shell_out_of_memory();
8174    }
8175    nByte = strlen30(zSql);
8176    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8177    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8178    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8179      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
8180      char cSep = '(';
8181      while( xRead(&sCtx) ){
8182        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8183        cSep = ',';
8184        if( sCtx.cTerm!=sCtx.cColSep ) break;
8185      }
8186      if( cSep=='(' ){
8187        sqlite3_free(zCreate);
8188        import_cleanup(&sCtx);
8189        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8190        rc = 1;
8191        goto meta_command_exit;
8192      }
8193      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8194      if( eVerbose>=1 ){
8195        utf8_printf(p->out, "%s\n", zCreate);
8196      }
8197      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8198      sqlite3_free(zCreate);
8199      if( rc ){
8200        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
8201                sqlite3_errmsg(p->db));
8202        import_cleanup(&sCtx);
8203        rc = 1;
8204        goto meta_command_exit;
8205      }
8206      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8207    }
8208    sqlite3_free(zSql);
8209    if( rc ){
8210      if (pStmt) sqlite3_finalize(pStmt);
8211      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8212      import_cleanup(&sCtx);
8213      rc = 1;
8214      goto meta_command_exit;
8215    }
8216    nCol = sqlite3_column_count(pStmt);
8217    sqlite3_finalize(pStmt);
8218    pStmt = 0;
8219    if( nCol==0 ) return 0; /* no columns, no error */
8220    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8221    if( zSql==0 ){
8222      import_cleanup(&sCtx);
8223      shell_out_of_memory();
8224    }
8225    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8226    j = strlen30(zSql);
8227    for(i=1; i<nCol; i++){
8228      zSql[j++] = ',';
8229      zSql[j++] = '?';
8230    }
8231    zSql[j++] = ')';
8232    zSql[j] = 0;
8233    if( eVerbose>=2 ){
8234      utf8_printf(p->out, "Insert using: %s\n", zSql);
8235    }
8236    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8237    sqlite3_free(zSql);
8238    if( rc ){
8239      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8240      if (pStmt) sqlite3_finalize(pStmt);
8241      import_cleanup(&sCtx);
8242      rc = 1;
8243      goto meta_command_exit;
8244    }
8245    needCommit = sqlite3_get_autocommit(p->db);
8246    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8247    do{
8248      int startLine = sCtx.nLine;
8249      for(i=0; i<nCol; i++){
8250        char *z = xRead(&sCtx);
8251        /*
8252        ** Did we reach end-of-file before finding any columns?
8253        ** If so, stop instead of NULL filling the remaining columns.
8254        */
8255        if( z==0 && i==0 ) break;
8256        /*
8257        ** Did we reach end-of-file OR end-of-line before finding any
8258        ** columns in ASCII mode?  If so, stop instead of NULL filling
8259        ** the remaining columns.
8260        */
8261        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8262        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8263        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8264          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8265                          "filling the rest with NULL\n",
8266                          sCtx.zFile, startLine, nCol, i+1);
8267          i += 2;
8268          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8269        }
8270      }
8271      if( sCtx.cTerm==sCtx.cColSep ){
8272        do{
8273          xRead(&sCtx);
8274          i++;
8275        }while( sCtx.cTerm==sCtx.cColSep );
8276        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8277                        "extras ignored\n",
8278                        sCtx.zFile, startLine, nCol, i);
8279      }
8280      if( i>=nCol ){
8281        sqlite3_step(pStmt);
8282        rc = sqlite3_reset(pStmt);
8283        if( rc!=SQLITE_OK ){
8284          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8285                      startLine, sqlite3_errmsg(p->db));
8286          sCtx.nErr++;
8287        }else{
8288          sCtx.nRow++;
8289        }
8290      }
8291    }while( sCtx.cTerm!=EOF );
8292
8293    import_cleanup(&sCtx);
8294    sqlite3_finalize(pStmt);
8295    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8296    if( eVerbose>0 ){
8297      utf8_printf(p->out,
8298          "Added %d rows with %d errors using %d lines of input\n",
8299          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8300    }
8301  }else
8302
8303#ifndef SQLITE_UNTESTABLE
8304  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8305    char *zSql;
8306    char *zCollist = 0;
8307    sqlite3_stmt *pStmt;
8308    int tnum = 0;
8309    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8310    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8311    int i;
8312    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8313      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8314                          "       .imposter off\n");
8315      /* Also allowed, but not documented:
8316      **
8317      **    .imposter TABLE IMPOSTER
8318      **
8319      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8320      ** imposter is another WITHOUT ROWID table with the columns in
8321      ** storage order. */
8322      rc = 1;
8323      goto meta_command_exit;
8324    }
8325    open_db(p, 0);
8326    if( nArg==2 ){
8327      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8328      goto meta_command_exit;
8329    }
8330    zSql = sqlite3_mprintf(
8331      "SELECT rootpage, 0 FROM sqlite_schema"
8332      " WHERE name='%q' AND type='index'"
8333      "UNION ALL "
8334      "SELECT rootpage, 1 FROM sqlite_schema"
8335      " WHERE name='%q' AND type='table'"
8336      "   AND sql LIKE '%%without%%rowid%%'",
8337      azArg[1], azArg[1]
8338    );
8339    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8340    sqlite3_free(zSql);
8341    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8342      tnum = sqlite3_column_int(pStmt, 0);
8343      isWO = sqlite3_column_int(pStmt, 1);
8344    }
8345    sqlite3_finalize(pStmt);
8346    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8347    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8348    sqlite3_free(zSql);
8349    i = 0;
8350    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8351      char zLabel[20];
8352      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8353      i++;
8354      if( zCol==0 ){
8355        if( sqlite3_column_int(pStmt,1)==-1 ){
8356          zCol = "_ROWID_";
8357        }else{
8358          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8359          zCol = zLabel;
8360        }
8361      }
8362      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8363        lenPK = (int)strlen(zCollist);
8364      }
8365      if( zCollist==0 ){
8366        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8367      }else{
8368        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8369      }
8370    }
8371    sqlite3_finalize(pStmt);
8372    if( i==0 || tnum==0 ){
8373      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8374      rc = 1;
8375      sqlite3_free(zCollist);
8376      goto meta_command_exit;
8377    }
8378    if( lenPK==0 ) lenPK = 100000;
8379    zSql = sqlite3_mprintf(
8380          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8381          azArg[2], zCollist, lenPK, zCollist);
8382    sqlite3_free(zCollist);
8383    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8384    if( rc==SQLITE_OK ){
8385      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8386      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8387      if( rc ){
8388        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8389      }else{
8390        utf8_printf(stdout, "%s;\n", zSql);
8391        raw_printf(stdout,
8392          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8393          azArg[1], isWO ? "table" : "index"
8394        );
8395      }
8396    }else{
8397      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8398      rc = 1;
8399    }
8400    sqlite3_free(zSql);
8401  }else
8402#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8403
8404#ifdef SQLITE_ENABLE_IOTRACE
8405  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8406    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8407    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8408    iotrace = 0;
8409    if( nArg<2 ){
8410      sqlite3IoTrace = 0;
8411    }else if( strcmp(azArg[1], "-")==0 ){
8412      sqlite3IoTrace = iotracePrintf;
8413      iotrace = stdout;
8414    }else{
8415      iotrace = fopen(azArg[1], "w");
8416      if( iotrace==0 ){
8417        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8418        sqlite3IoTrace = 0;
8419        rc = 1;
8420      }else{
8421        sqlite3IoTrace = iotracePrintf;
8422      }
8423    }
8424  }else
8425#endif
8426
8427  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8428    static const struct {
8429       const char *zLimitName;   /* Name of a limit */
8430       int limitCode;            /* Integer code for that limit */
8431    } aLimit[] = {
8432      { "length",                SQLITE_LIMIT_LENGTH                    },
8433      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8434      { "column",                SQLITE_LIMIT_COLUMN                    },
8435      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8436      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8437      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8438      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8439      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8440      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8441      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8442      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8443      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8444    };
8445    int i, n2;
8446    open_db(p, 0);
8447    if( nArg==1 ){
8448      for(i=0; i<ArraySize(aLimit); i++){
8449        printf("%20s %d\n", aLimit[i].zLimitName,
8450               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8451      }
8452    }else if( nArg>3 ){
8453      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8454      rc = 1;
8455      goto meta_command_exit;
8456    }else{
8457      int iLimit = -1;
8458      n2 = strlen30(azArg[1]);
8459      for(i=0; i<ArraySize(aLimit); i++){
8460        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8461          if( iLimit<0 ){
8462            iLimit = i;
8463          }else{
8464            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8465            rc = 1;
8466            goto meta_command_exit;
8467          }
8468        }
8469      }
8470      if( iLimit<0 ){
8471        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8472                        "enter \".limits\" with no arguments for a list.\n",
8473                         azArg[1]);
8474        rc = 1;
8475        goto meta_command_exit;
8476      }
8477      if( nArg==3 ){
8478        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8479                      (int)integerValue(azArg[2]));
8480      }
8481      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8482             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8483    }
8484  }else
8485
8486  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8487    open_db(p, 0);
8488    lintDotCommand(p, azArg, nArg);
8489  }else
8490
8491#ifndef SQLITE_OMIT_LOAD_EXTENSION
8492  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8493    const char *zFile, *zProc;
8494    char *zErrMsg = 0;
8495    if( nArg<2 ){
8496      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8497      rc = 1;
8498      goto meta_command_exit;
8499    }
8500    zFile = azArg[1];
8501    zProc = nArg>=3 ? azArg[2] : 0;
8502    open_db(p, 0);
8503    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8504    if( rc!=SQLITE_OK ){
8505      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8506      sqlite3_free(zErrMsg);
8507      rc = 1;
8508    }
8509  }else
8510#endif
8511
8512  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8513    if( nArg!=2 ){
8514      raw_printf(stderr, "Usage: .log FILENAME\n");
8515      rc = 1;
8516    }else{
8517      const char *zFile = azArg[1];
8518      output_file_close(p->pLog);
8519      p->pLog = output_file_open(zFile, 0);
8520    }
8521  }else
8522
8523  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8524    const char *zMode = nArg>=2 ? azArg[1] : "";
8525    int n2 = strlen30(zMode);
8526    int c2 = zMode[0];
8527    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8528      p->mode = MODE_Line;
8529      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8530    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8531      p->mode = MODE_Column;
8532      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8533        p->showHeader = 1;
8534      }
8535      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8536    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8537      p->mode = MODE_List;
8538      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8539      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8540    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8541      p->mode = MODE_Html;
8542    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8543      p->mode = MODE_Tcl;
8544      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8545      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8546    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8547      p->mode = MODE_Csv;
8548      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8549      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8550    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8551      p->mode = MODE_List;
8552      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8553    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8554      p->mode = MODE_Insert;
8555      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8556    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8557      p->mode = MODE_Quote;
8558      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8559      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8560    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8561      p->mode = MODE_Ascii;
8562      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8563      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8564    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8565      p->mode = MODE_Markdown;
8566    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8567      p->mode = MODE_Table;
8568    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8569      p->mode = MODE_Box;
8570    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8571      p->mode = MODE_Json;
8572    }else if( nArg==1 ){
8573      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8574    }else{
8575      raw_printf(stderr, "Error: mode should be one of: "
8576         "ascii box column csv html insert json line list markdown "
8577         "quote table tabs tcl\n");
8578      rc = 1;
8579    }
8580    p->cMode = p->mode;
8581  }else
8582
8583  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8584    if( nArg==2 ){
8585      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8586                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8587    }else{
8588      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8589      rc = 1;
8590    }
8591  }else
8592
8593#ifdef SQLITE_DEBUG
8594  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8595    int i;
8596    for(i=1; i<nArg; i++){
8597      const char *z = azArg[i];
8598      if( z[0]=='-' && z[1]=='-' ) z++;
8599      if( strcmp(z,"-repeat")==0 ){
8600        if( i==nArg-1 ){
8601          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8602          rc = 1;
8603        }else{
8604          oomRepeat = (int)integerValue(azArg[++i]);
8605        }
8606      }else if( IsDigit(z[0]) ){
8607        oomCounter = (int)integerValue(azArg[i]);
8608      }else{
8609        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8610        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8611        rc = 1;
8612      }
8613    }
8614    if( rc==0 ){
8615      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8616      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8617    }
8618  }else
8619#endif /* SQLITE_DEBUG */
8620
8621  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8622    char *zNewFilename;  /* Name of the database file to open */
8623    int iName = 1;       /* Index in azArg[] of the filename */
8624    int newFlag = 0;     /* True to delete file before opening */
8625    /* Close the existing database */
8626    session_close_all(p);
8627    close_db(p->db);
8628    p->db = 0;
8629    p->zDbFilename = 0;
8630    sqlite3_free(p->zFreeOnClose);
8631    p->zFreeOnClose = 0;
8632    p->openMode = SHELL_OPEN_UNSPEC;
8633    p->openFlags = 0;
8634    p->szMax = 0;
8635    /* Check for command-line arguments */
8636    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8637      const char *z = azArg[iName];
8638      if( optionMatch(z,"new") ){
8639        newFlag = 1;
8640#ifdef SQLITE_HAVE_ZLIB
8641      }else if( optionMatch(z, "zip") ){
8642        p->openMode = SHELL_OPEN_ZIPFILE;
8643#endif
8644      }else if( optionMatch(z, "append") ){
8645        p->openMode = SHELL_OPEN_APPENDVFS;
8646      }else if( optionMatch(z, "readonly") ){
8647        p->openMode = SHELL_OPEN_READONLY;
8648      }else if( optionMatch(z, "nofollow") ){
8649        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8650#ifdef SQLITE_ENABLE_DESERIALIZE
8651      }else if( optionMatch(z, "deserialize") ){
8652        p->openMode = SHELL_OPEN_DESERIALIZE;
8653      }else if( optionMatch(z, "hexdb") ){
8654        p->openMode = SHELL_OPEN_HEXDB;
8655      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8656        p->szMax = integerValue(azArg[++iName]);
8657#endif /* SQLITE_ENABLE_DESERIALIZE */
8658      }else if( z[0]=='-' ){
8659        utf8_printf(stderr, "unknown option: %s\n", z);
8660        rc = 1;
8661        goto meta_command_exit;
8662      }
8663    }
8664    /* If a filename is specified, try to open it first */
8665    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
8666    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8667      if( newFlag ) shellDeleteFile(zNewFilename);
8668      p->zDbFilename = zNewFilename;
8669      open_db(p, OPEN_DB_KEEPALIVE);
8670      if( p->db==0 ){
8671        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8672        sqlite3_free(zNewFilename);
8673      }else{
8674        p->zFreeOnClose = zNewFilename;
8675      }
8676    }
8677    if( p->db==0 ){
8678      /* As a fall-back open a TEMP database */
8679      p->zDbFilename = 0;
8680      open_db(p, 0);
8681    }
8682  }else
8683
8684  if( (c=='o'
8685        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8686   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8687  ){
8688    const char *zFile = 0;
8689    int bTxtMode = 0;
8690    int i;
8691    int eMode = 0;
8692    int bBOM = 0;
8693    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8694
8695    if( c=='e' ){
8696      eMode = 'x';
8697      bOnce = 2;
8698    }else if( strncmp(azArg[0],"once",n)==0 ){
8699      bOnce = 1;
8700    }
8701    for(i=1; i<nArg; i++){
8702      char *z = azArg[i];
8703      if( z[0]=='-' ){
8704        if( z[1]=='-' ) z++;
8705        if( strcmp(z,"-bom")==0 ){
8706          bBOM = 1;
8707        }else if( c!='e' && strcmp(z,"-x")==0 ){
8708          eMode = 'x';  /* spreadsheet */
8709        }else if( c!='e' && strcmp(z,"-e")==0 ){
8710          eMode = 'e';  /* text editor */
8711        }else{
8712          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8713                      azArg[i]);
8714          showHelp(p->out, azArg[0]);
8715          rc = 1;
8716          goto meta_command_exit;
8717        }
8718      }else if( zFile==0 ){
8719        zFile = z;
8720      }else{
8721        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8722                    azArg[i]);
8723        showHelp(p->out, azArg[0]);
8724        rc = 1;
8725        goto meta_command_exit;
8726      }
8727    }
8728    if( zFile==0 ) zFile = "stdout";
8729    if( bOnce ){
8730      p->outCount = 2;
8731    }else{
8732      p->outCount = 0;
8733    }
8734    output_reset(p);
8735#ifndef SQLITE_NOHAVE_SYSTEM
8736    if( eMode=='e' || eMode=='x' ){
8737      p->doXdgOpen = 1;
8738      outputModePush(p);
8739      if( eMode=='x' ){
8740        /* spreadsheet mode.  Output as CSV. */
8741        newTempFile(p, "csv");
8742        ShellClearFlag(p, SHFLG_Echo);
8743        p->mode = MODE_Csv;
8744        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8745        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8746      }else{
8747        /* text editor mode */
8748        newTempFile(p, "txt");
8749        bTxtMode = 1;
8750      }
8751      zFile = p->zTempFile;
8752    }
8753#endif /* SQLITE_NOHAVE_SYSTEM */
8754    if( zFile[0]=='|' ){
8755#ifdef SQLITE_OMIT_POPEN
8756      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8757      rc = 1;
8758      p->out = stdout;
8759#else
8760      p->out = popen(zFile + 1, "w");
8761      if( p->out==0 ){
8762        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8763        p->out = stdout;
8764        rc = 1;
8765      }else{
8766        if( bBOM ) fprintf(p->out,"\357\273\277");
8767        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8768      }
8769#endif
8770    }else{
8771      p->out = output_file_open(zFile, bTxtMode);
8772      if( p->out==0 ){
8773        if( strcmp(zFile,"off")!=0 ){
8774          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8775        }
8776        p->out = stdout;
8777        rc = 1;
8778      } else {
8779        if( bBOM ) fprintf(p->out,"\357\273\277");
8780        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8781      }
8782    }
8783  }else
8784
8785  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8786    open_db(p,0);
8787    if( nArg<=1 ) goto parameter_syntax_error;
8788
8789    /* .parameter clear
8790    ** Clear all bind parameters by dropping the TEMP table that holds them.
8791    */
8792    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8793      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8794                   0, 0, 0);
8795    }else
8796
8797    /* .parameter list
8798    ** List all bind parameters.
8799    */
8800    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8801      sqlite3_stmt *pStmt = 0;
8802      int rx;
8803      int len = 0;
8804      rx = sqlite3_prepare_v2(p->db,
8805             "SELECT max(length(key)) "
8806             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8807      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8808        len = sqlite3_column_int(pStmt, 0);
8809        if( len>40 ) len = 40;
8810      }
8811      sqlite3_finalize(pStmt);
8812      pStmt = 0;
8813      if( len ){
8814        rx = sqlite3_prepare_v2(p->db,
8815             "SELECT key, quote(value) "
8816             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8817        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8818          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8819                      sqlite3_column_text(pStmt,1));
8820        }
8821        sqlite3_finalize(pStmt);
8822      }
8823    }else
8824
8825    /* .parameter init
8826    ** Make sure the TEMP table used to hold bind parameters exists.
8827    ** Create it if necessary.
8828    */
8829    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8830      bind_table_init(p);
8831    }else
8832
8833    /* .parameter set NAME VALUE
8834    ** Set or reset a bind parameter.  NAME should be the full parameter
8835    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8836    ** VALUE can be in either SQL literal notation, or if not it will be
8837    ** understood to be a text string.
8838    */
8839    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8840      int rx;
8841      char *zSql;
8842      sqlite3_stmt *pStmt;
8843      const char *zKey = azArg[2];
8844      const char *zValue = azArg[3];
8845      bind_table_init(p);
8846      zSql = sqlite3_mprintf(
8847                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8848                  "VALUES(%Q,%s);", zKey, zValue);
8849      if( zSql==0 ) shell_out_of_memory();
8850      pStmt = 0;
8851      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8852      sqlite3_free(zSql);
8853      if( rx!=SQLITE_OK ){
8854        sqlite3_finalize(pStmt);
8855        pStmt = 0;
8856        zSql = sqlite3_mprintf(
8857                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8858                   "VALUES(%Q,%Q);", zKey, zValue);
8859        if( zSql==0 ) shell_out_of_memory();
8860        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8861        sqlite3_free(zSql);
8862        if( rx!=SQLITE_OK ){
8863          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8864          sqlite3_finalize(pStmt);
8865          pStmt = 0;
8866          rc = 1;
8867        }
8868      }
8869      sqlite3_step(pStmt);
8870      sqlite3_finalize(pStmt);
8871    }else
8872
8873    /* .parameter unset NAME
8874    ** Remove the NAME binding from the parameter binding table, if it
8875    ** exists.
8876    */
8877    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8878      char *zSql = sqlite3_mprintf(
8879          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8880      if( zSql==0 ) shell_out_of_memory();
8881      sqlite3_exec(p->db, zSql, 0, 0, 0);
8882      sqlite3_free(zSql);
8883    }else
8884    /* If no command name matches, show a syntax error */
8885    parameter_syntax_error:
8886    showHelp(p->out, "parameter");
8887  }else
8888
8889  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8890    int i;
8891    for(i=1; i<nArg; i++){
8892      if( i>1 ) raw_printf(p->out, " ");
8893      utf8_printf(p->out, "%s", azArg[i]);
8894    }
8895    raw_printf(p->out, "\n");
8896  }else
8897
8898#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8899  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8900    int i;
8901    int nn = 0;
8902    p->flgProgress = 0;
8903    p->mxProgress = 0;
8904    p->nProgress = 0;
8905    for(i=1; i<nArg; i++){
8906      const char *z = azArg[i];
8907      if( z[0]=='-' ){
8908        z++;
8909        if( z[0]=='-' ) z++;
8910        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8911          p->flgProgress |= SHELL_PROGRESS_QUIET;
8912          continue;
8913        }
8914        if( strcmp(z,"reset")==0 ){
8915          p->flgProgress |= SHELL_PROGRESS_RESET;
8916          continue;
8917        }
8918        if( strcmp(z,"once")==0 ){
8919          p->flgProgress |= SHELL_PROGRESS_ONCE;
8920          continue;
8921        }
8922        if( strcmp(z,"limit")==0 ){
8923          if( i+1>=nArg ){
8924            utf8_printf(stderr, "Error: missing argument on --limit\n");
8925            rc = 1;
8926            goto meta_command_exit;
8927          }else{
8928            p->mxProgress = (int)integerValue(azArg[++i]);
8929          }
8930          continue;
8931        }
8932        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8933        rc = 1;
8934        goto meta_command_exit;
8935      }else{
8936        nn = (int)integerValue(z);
8937      }
8938    }
8939    open_db(p, 0);
8940    sqlite3_progress_handler(p->db, nn, progress_handler, p);
8941  }else
8942#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
8943
8944  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8945    if( nArg >= 2) {
8946      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
8947    }
8948    if( nArg >= 3) {
8949      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
8950    }
8951  }else
8952
8953  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
8954    rc = 2;
8955  }else
8956
8957  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
8958    FILE *inSaved = p->in;
8959    int savedLineno = p->lineno;
8960    if( nArg!=2 ){
8961      raw_printf(stderr, "Usage: .read FILE\n");
8962      rc = 1;
8963      goto meta_command_exit;
8964    }
8965    if( notNormalFile(azArg[1])
8966     || (p->in = fopen(azArg[1], "rb"))==0
8967    ){
8968      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
8969      rc = 1;
8970    }else{
8971      rc = process_input(p);
8972      fclose(p->in);
8973    }
8974    p->in = inSaved;
8975    p->lineno = savedLineno;
8976  }else
8977
8978  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
8979    const char *zSrcFile;
8980    const char *zDb;
8981    sqlite3 *pSrc;
8982    sqlite3_backup *pBackup;
8983    int nTimeout = 0;
8984
8985    if( nArg==2 ){
8986      zSrcFile = azArg[1];
8987      zDb = "main";
8988    }else if( nArg==3 ){
8989      zSrcFile = azArg[2];
8990      zDb = azArg[1];
8991    }else{
8992      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
8993      rc = 1;
8994      goto meta_command_exit;
8995    }
8996    rc = sqlite3_open(zSrcFile, &pSrc);
8997    if( rc!=SQLITE_OK ){
8998      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
8999      close_db(pSrc);
9000      return 1;
9001    }
9002    open_db(p, 0);
9003    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9004    if( pBackup==0 ){
9005      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9006      close_db(pSrc);
9007      return 1;
9008    }
9009    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9010          || rc==SQLITE_BUSY  ){
9011      if( rc==SQLITE_BUSY ){
9012        if( nTimeout++ >= 3 ) break;
9013        sqlite3_sleep(100);
9014      }
9015    }
9016    sqlite3_backup_finish(pBackup);
9017    if( rc==SQLITE_DONE ){
9018      rc = 0;
9019    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9020      raw_printf(stderr, "Error: source database is busy\n");
9021      rc = 1;
9022    }else{
9023      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9024      rc = 1;
9025    }
9026    close_db(pSrc);
9027  }else
9028
9029  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9030    if( nArg==2 ){
9031      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9032#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9033      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9034#endif
9035    }else{
9036      raw_printf(stderr, "Usage: .scanstats on|off\n");
9037      rc = 1;
9038    }
9039  }else
9040
9041  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9042    ShellText sSelect;
9043    ShellState data;
9044    char *zErrMsg = 0;
9045    const char *zDiv = "(";
9046    const char *zName = 0;
9047    int iSchema = 0;
9048    int bDebug = 0;
9049    int ii;
9050
9051    open_db(p, 0);
9052    memcpy(&data, p, sizeof(data));
9053    data.showHeader = 0;
9054    data.cMode = data.mode = MODE_Semi;
9055    initText(&sSelect);
9056    for(ii=1; ii<nArg; ii++){
9057      if( optionMatch(azArg[ii],"indent") ){
9058        data.cMode = data.mode = MODE_Pretty;
9059      }else if( optionMatch(azArg[ii],"debug") ){
9060        bDebug = 1;
9061      }else if( zName==0 ){
9062        zName = azArg[ii];
9063      }else{
9064        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
9065        rc = 1;
9066        goto meta_command_exit;
9067      }
9068    }
9069    if( zName!=0 ){
9070      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9071                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9072                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9073                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9074      if( isSchema ){
9075        char *new_argv[2], *new_colv[2];
9076        new_argv[0] = sqlite3_mprintf(
9077                      "CREATE TABLE %s (\n"
9078                      "  type text,\n"
9079                      "  name text,\n"
9080                      "  tbl_name text,\n"
9081                      "  rootpage integer,\n"
9082                      "  sql text\n"
9083                      ")", zName);
9084        new_argv[1] = 0;
9085        new_colv[0] = "sql";
9086        new_colv[1] = 0;
9087        callback(&data, 1, new_argv, new_colv);
9088        sqlite3_free(new_argv[0]);
9089      }
9090    }
9091    if( zDiv ){
9092      sqlite3_stmt *pStmt = 0;
9093      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9094                              -1, &pStmt, 0);
9095      if( rc ){
9096        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9097        sqlite3_finalize(pStmt);
9098        rc = 1;
9099        goto meta_command_exit;
9100      }
9101      appendText(&sSelect, "SELECT sql FROM", 0);
9102      iSchema = 0;
9103      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9104        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9105        char zScNum[30];
9106        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9107        appendText(&sSelect, zDiv, 0);
9108        zDiv = " UNION ALL ";
9109        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9110        if( sqlite3_stricmp(zDb, "main")!=0 ){
9111          appendText(&sSelect, zDb, '\'');
9112        }else{
9113          appendText(&sSelect, "NULL", 0);
9114        }
9115        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9116        appendText(&sSelect, zScNum, 0);
9117        appendText(&sSelect, " AS snum, ", 0);
9118        appendText(&sSelect, zDb, '\'');
9119        appendText(&sSelect, " AS sname FROM ", 0);
9120        appendText(&sSelect, zDb, quoteChar(zDb));
9121        appendText(&sSelect, ".sqlite_schema", 0);
9122      }
9123      sqlite3_finalize(pStmt);
9124#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9125      if( zName ){
9126        appendText(&sSelect,
9127           " UNION ALL SELECT shell_module_schema(name),"
9128           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9129        0);
9130      }
9131#endif
9132      appendText(&sSelect, ") WHERE ", 0);
9133      if( zName ){
9134        char *zQarg = sqlite3_mprintf("%Q", zName);
9135        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9136                    strchr(zName, '[') != 0;
9137        if( strchr(zName, '.') ){
9138          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9139        }else{
9140          appendText(&sSelect, "lower(tbl_name)", 0);
9141        }
9142        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9143        appendText(&sSelect, zQarg, 0);
9144        if( !bGlob ){
9145          appendText(&sSelect, " ESCAPE '\\' ", 0);
9146        }
9147        appendText(&sSelect, " AND ", 0);
9148        sqlite3_free(zQarg);
9149      }
9150      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
9151                           " ORDER BY snum, rowid", 0);
9152      if( bDebug ){
9153        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9154      }else{
9155        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9156      }
9157      freeText(&sSelect);
9158    }
9159    if( zErrMsg ){
9160      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9161      sqlite3_free(zErrMsg);
9162      rc = 1;
9163    }else if( rc != SQLITE_OK ){
9164      raw_printf(stderr,"Error: querying schema information\n");
9165      rc = 1;
9166    }else{
9167      rc = 0;
9168    }
9169  }else
9170
9171#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9172  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9173    sqlite3_unsupported_selecttrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
9174  }else
9175#endif
9176
9177#if defined(SQLITE_ENABLE_SESSION)
9178  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9179    OpenSession *pSession = &p->aSession[0];
9180    char **azCmd = &azArg[1];
9181    int iSes = 0;
9182    int nCmd = nArg - 1;
9183    int i;
9184    if( nArg<=1 ) goto session_syntax_error;
9185    open_db(p, 0);
9186    if( nArg>=3 ){
9187      for(iSes=0; iSes<p->nSession; iSes++){
9188        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9189      }
9190      if( iSes<p->nSession ){
9191        pSession = &p->aSession[iSes];
9192        azCmd++;
9193        nCmd--;
9194      }else{
9195        pSession = &p->aSession[0];
9196        iSes = 0;
9197      }
9198    }
9199
9200    /* .session attach TABLE
9201    ** Invoke the sqlite3session_attach() interface to attach a particular
9202    ** table so that it is never filtered.
9203    */
9204    if( strcmp(azCmd[0],"attach")==0 ){
9205      if( nCmd!=2 ) goto session_syntax_error;
9206      if( pSession->p==0 ){
9207        session_not_open:
9208        raw_printf(stderr, "ERROR: No sessions are open\n");
9209      }else{
9210        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9211        if( rc ){
9212          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9213          rc = 0;
9214        }
9215      }
9216    }else
9217
9218    /* .session changeset FILE
9219    ** .session patchset FILE
9220    ** Write a changeset or patchset into a file.  The file is overwritten.
9221    */
9222    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9223      FILE *out = 0;
9224      if( nCmd!=2 ) goto session_syntax_error;
9225      if( pSession->p==0 ) goto session_not_open;
9226      out = fopen(azCmd[1], "wb");
9227      if( out==0 ){
9228        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9229                    azCmd[1]);
9230      }else{
9231        int szChng;
9232        void *pChng;
9233        if( azCmd[0][0]=='c' ){
9234          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9235        }else{
9236          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9237        }
9238        if( rc ){
9239          printf("Error: error code %d\n", rc);
9240          rc = 0;
9241        }
9242        if( pChng
9243          && fwrite(pChng, szChng, 1, out)!=1 ){
9244          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9245                  szChng);
9246        }
9247        sqlite3_free(pChng);
9248        fclose(out);
9249      }
9250    }else
9251
9252    /* .session close
9253    ** Close the identified session
9254    */
9255    if( strcmp(azCmd[0], "close")==0 ){
9256      if( nCmd!=1 ) goto session_syntax_error;
9257      if( p->nSession ){
9258        session_close(pSession);
9259        p->aSession[iSes] = p->aSession[--p->nSession];
9260      }
9261    }else
9262
9263    /* .session enable ?BOOLEAN?
9264    ** Query or set the enable flag
9265    */
9266    if( strcmp(azCmd[0], "enable")==0 ){
9267      int ii;
9268      if( nCmd>2 ) goto session_syntax_error;
9269      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9270      if( p->nSession ){
9271        ii = sqlite3session_enable(pSession->p, ii);
9272        utf8_printf(p->out, "session %s enable flag = %d\n",
9273                    pSession->zName, ii);
9274      }
9275    }else
9276
9277    /* .session filter GLOB ....
9278    ** Set a list of GLOB patterns of table names to be excluded.
9279    */
9280    if( strcmp(azCmd[0], "filter")==0 ){
9281      int ii, nByte;
9282      if( nCmd<2 ) goto session_syntax_error;
9283      if( p->nSession ){
9284        for(ii=0; ii<pSession->nFilter; ii++){
9285          sqlite3_free(pSession->azFilter[ii]);
9286        }
9287        sqlite3_free(pSession->azFilter);
9288        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9289        pSession->azFilter = sqlite3_malloc( nByte );
9290        if( pSession->azFilter==0 ){
9291          raw_printf(stderr, "Error: out or memory\n");
9292          exit(1);
9293        }
9294        for(ii=1; ii<nCmd; ii++){
9295          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9296        }
9297        pSession->nFilter = ii-1;
9298      }
9299    }else
9300
9301    /* .session indirect ?BOOLEAN?
9302    ** Query or set the indirect flag
9303    */
9304    if( strcmp(azCmd[0], "indirect")==0 ){
9305      int ii;
9306      if( nCmd>2 ) goto session_syntax_error;
9307      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9308      if( p->nSession ){
9309        ii = sqlite3session_indirect(pSession->p, ii);
9310        utf8_printf(p->out, "session %s indirect flag = %d\n",
9311                    pSession->zName, ii);
9312      }
9313    }else
9314
9315    /* .session isempty
9316    ** Determine if the session is empty
9317    */
9318    if( strcmp(azCmd[0], "isempty")==0 ){
9319      int ii;
9320      if( nCmd!=1 ) goto session_syntax_error;
9321      if( p->nSession ){
9322        ii = sqlite3session_isempty(pSession->p);
9323        utf8_printf(p->out, "session %s isempty flag = %d\n",
9324                    pSession->zName, ii);
9325      }
9326    }else
9327
9328    /* .session list
9329    ** List all currently open sessions
9330    */
9331    if( strcmp(azCmd[0],"list")==0 ){
9332      for(i=0; i<p->nSession; i++){
9333        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9334      }
9335    }else
9336
9337    /* .session open DB NAME
9338    ** Open a new session called NAME on the attached database DB.
9339    ** DB is normally "main".
9340    */
9341    if( strcmp(azCmd[0],"open")==0 ){
9342      char *zName;
9343      if( nCmd!=3 ) goto session_syntax_error;
9344      zName = azCmd[2];
9345      if( zName[0]==0 ) goto session_syntax_error;
9346      for(i=0; i<p->nSession; i++){
9347        if( strcmp(p->aSession[i].zName,zName)==0 ){
9348          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9349          goto meta_command_exit;
9350        }
9351      }
9352      if( p->nSession>=ArraySize(p->aSession) ){
9353        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9354        goto meta_command_exit;
9355      }
9356      pSession = &p->aSession[p->nSession];
9357      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9358      if( rc ){
9359        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9360        rc = 0;
9361        goto meta_command_exit;
9362      }
9363      pSession->nFilter = 0;
9364      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9365      p->nSession++;
9366      pSession->zName = sqlite3_mprintf("%s", zName);
9367    }else
9368    /* If no command name matches, show a syntax error */
9369    session_syntax_error:
9370    showHelp(p->out, "session");
9371  }else
9372#endif
9373
9374#ifdef SQLITE_DEBUG
9375  /* Undocumented commands for internal testing.  Subject to change
9376  ** without notice. */
9377  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9378    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9379      int i, v;
9380      for(i=1; i<nArg; i++){
9381        v = booleanValue(azArg[i]);
9382        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9383      }
9384    }
9385    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9386      int i; sqlite3_int64 v;
9387      for(i=1; i<nArg; i++){
9388        char zBuf[200];
9389        v = integerValue(azArg[i]);
9390        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9391        utf8_printf(p->out, "%s", zBuf);
9392      }
9393    }
9394  }else
9395#endif
9396
9397  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9398    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9399    int bVerbose = 0;        /* Verbose output */
9400    int bSelftestExists;     /* True if SELFTEST already exists */
9401    int i, k;                /* Loop counters */
9402    int nTest = 0;           /* Number of tests runs */
9403    int nErr = 0;            /* Number of errors seen */
9404    ShellText str;           /* Answer for a query */
9405    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9406
9407    open_db(p,0);
9408    for(i=1; i<nArg; i++){
9409      const char *z = azArg[i];
9410      if( z[0]=='-' && z[1]=='-' ) z++;
9411      if( strcmp(z,"-init")==0 ){
9412        bIsInit = 1;
9413      }else
9414      if( strcmp(z,"-v")==0 ){
9415        bVerbose++;
9416      }else
9417      {
9418        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9419                    azArg[i], azArg[0]);
9420        raw_printf(stderr, "Should be one of: --init -v\n");
9421        rc = 1;
9422        goto meta_command_exit;
9423      }
9424    }
9425    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9426           != SQLITE_OK ){
9427      bSelftestExists = 0;
9428    }else{
9429      bSelftestExists = 1;
9430    }
9431    if( bIsInit ){
9432      createSelftestTable(p);
9433      bSelftestExists = 1;
9434    }
9435    initText(&str);
9436    appendText(&str, "x", 0);
9437    for(k=bSelftestExists; k>=0; k--){
9438      if( k==1 ){
9439        rc = sqlite3_prepare_v2(p->db,
9440            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9441            -1, &pStmt, 0);
9442      }else{
9443        rc = sqlite3_prepare_v2(p->db,
9444          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9445          "      (1,'run','PRAGMA integrity_check','ok')",
9446          -1, &pStmt, 0);
9447      }
9448      if( rc ){
9449        raw_printf(stderr, "Error querying the selftest table\n");
9450        rc = 1;
9451        sqlite3_finalize(pStmt);
9452        goto meta_command_exit;
9453      }
9454      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9455        int tno = sqlite3_column_int(pStmt, 0);
9456        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9457        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9458        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9459
9460        k = 0;
9461        if( bVerbose>0 ){
9462          char *zQuote = sqlite3_mprintf("%q", zSql);
9463          printf("%d: %s %s\n", tno, zOp, zSql);
9464          sqlite3_free(zQuote);
9465        }
9466        if( strcmp(zOp,"memo")==0 ){
9467          utf8_printf(p->out, "%s\n", zSql);
9468        }else
9469        if( strcmp(zOp,"run")==0 ){
9470          char *zErrMsg = 0;
9471          str.n = 0;
9472          str.z[0] = 0;
9473          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9474          nTest++;
9475          if( bVerbose ){
9476            utf8_printf(p->out, "Result: %s\n", str.z);
9477          }
9478          if( rc || zErrMsg ){
9479            nErr++;
9480            rc = 1;
9481            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9482            sqlite3_free(zErrMsg);
9483          }else if( strcmp(zAns,str.z)!=0 ){
9484            nErr++;
9485            rc = 1;
9486            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9487            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9488          }
9489        }else
9490        {
9491          utf8_printf(stderr,
9492            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9493          rc = 1;
9494          break;
9495        }
9496      } /* End loop over rows of content from SELFTEST */
9497      sqlite3_finalize(pStmt);
9498    } /* End loop over k */
9499    freeText(&str);
9500    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9501  }else
9502
9503  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9504    if( nArg<2 || nArg>3 ){
9505      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9506      rc = 1;
9507    }
9508    if( nArg>=2 ){
9509      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9510                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9511    }
9512    if( nArg>=3 ){
9513      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9514                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9515    }
9516  }else
9517
9518  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9519    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9520    int i;                   /* Loop counter */
9521    int bSchema = 0;         /* Also hash the schema */
9522    int bSeparate = 0;       /* Hash each table separately */
9523    int iSize = 224;         /* Hash algorithm to use */
9524    int bDebug = 0;          /* Only show the query that would have run */
9525    sqlite3_stmt *pStmt;     /* For querying tables names */
9526    char *zSql;              /* SQL to be run */
9527    char *zSep;              /* Separator */
9528    ShellText sSql;          /* Complete SQL for the query to run the hash */
9529    ShellText sQuery;        /* Set of queries used to read all content */
9530    open_db(p, 0);
9531    for(i=1; i<nArg; i++){
9532      const char *z = azArg[i];
9533      if( z[0]=='-' ){
9534        z++;
9535        if( z[0]=='-' ) z++;
9536        if( strcmp(z,"schema")==0 ){
9537          bSchema = 1;
9538        }else
9539        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9540         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9541        ){
9542          iSize = atoi(&z[5]);
9543        }else
9544        if( strcmp(z,"debug")==0 ){
9545          bDebug = 1;
9546        }else
9547        {
9548          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9549                      azArg[i], azArg[0]);
9550          showHelp(p->out, azArg[0]);
9551          rc = 1;
9552          goto meta_command_exit;
9553        }
9554      }else if( zLike ){
9555        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9556        rc = 1;
9557        goto meta_command_exit;
9558      }else{
9559        zLike = z;
9560        bSeparate = 1;
9561        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9562      }
9563    }
9564    if( bSchema ){
9565      zSql = "SELECT lower(name) FROM sqlite_schema"
9566             " WHERE type='table' AND coalesce(rootpage,0)>1"
9567             " UNION ALL SELECT 'sqlite_schema'"
9568             " ORDER BY 1 collate nocase";
9569    }else{
9570      zSql = "SELECT lower(name) FROM sqlite_schema"
9571             " WHERE type='table' AND coalesce(rootpage,0)>1"
9572             " AND name NOT LIKE 'sqlite_%'"
9573             " ORDER BY 1 collate nocase";
9574    }
9575    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9576    initText(&sQuery);
9577    initText(&sSql);
9578    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9579    zSep = "VALUES(";
9580    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9581      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9582      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9583      if( strncmp(zTab, "sqlite_",7)!=0 ){
9584        appendText(&sQuery,"SELECT * FROM ", 0);
9585        appendText(&sQuery,zTab,'"');
9586        appendText(&sQuery," NOT INDEXED;", 0);
9587      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9588        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9589                           " ORDER BY name;", 0);
9590      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9591        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9592                           " ORDER BY name;", 0);
9593      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9594        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9595                           " ORDER BY tbl,idx;", 0);
9596      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9597        appendText(&sQuery, "SELECT * FROM ", 0);
9598        appendText(&sQuery, zTab, 0);
9599        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9600      }
9601      appendText(&sSql, zSep, 0);
9602      appendText(&sSql, sQuery.z, '\'');
9603      sQuery.n = 0;
9604      appendText(&sSql, ",", 0);
9605      appendText(&sSql, zTab, '\'');
9606      zSep = "),(";
9607    }
9608    sqlite3_finalize(pStmt);
9609    if( bSeparate ){
9610      zSql = sqlite3_mprintf(
9611          "%s))"
9612          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9613          "   FROM [sha3sum$query]",
9614          sSql.z, iSize);
9615    }else{
9616      zSql = sqlite3_mprintf(
9617          "%s))"
9618          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9619          "   FROM [sha3sum$query]",
9620          sSql.z, iSize);
9621    }
9622    freeText(&sQuery);
9623    freeText(&sSql);
9624    if( bDebug ){
9625      utf8_printf(p->out, "%s\n", zSql);
9626    }else{
9627      shell_exec(p, zSql, 0);
9628    }
9629    sqlite3_free(zSql);
9630  }else
9631
9632#ifndef SQLITE_NOHAVE_SYSTEM
9633  if( c=='s'
9634   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9635  ){
9636    char *zCmd;
9637    int i, x;
9638    if( nArg<2 ){
9639      raw_printf(stderr, "Usage: .system COMMAND\n");
9640      rc = 1;
9641      goto meta_command_exit;
9642    }
9643    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9644    for(i=2; i<nArg; i++){
9645      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9646                             zCmd, azArg[i]);
9647    }
9648    x = system(zCmd);
9649    sqlite3_free(zCmd);
9650    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9651  }else
9652#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9653
9654  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9655    static const char *azBool[] = { "off", "on", "trigger", "full"};
9656    int i;
9657    if( nArg!=1 ){
9658      raw_printf(stderr, "Usage: .show\n");
9659      rc = 1;
9660      goto meta_command_exit;
9661    }
9662    utf8_printf(p->out, "%12.12s: %s\n","echo",
9663                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9664    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9665    utf8_printf(p->out, "%12.12s: %s\n","explain",
9666         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9667    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9668    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9669    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9670      output_c_string(p->out, p->nullValue);
9671      raw_printf(p->out, "\n");
9672    utf8_printf(p->out,"%12.12s: %s\n","output",
9673            strlen30(p->outfile) ? p->outfile : "stdout");
9674    utf8_printf(p->out,"%12.12s: ", "colseparator");
9675      output_c_string(p->out, p->colSeparator);
9676      raw_printf(p->out, "\n");
9677    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9678      output_c_string(p->out, p->rowSeparator);
9679      raw_printf(p->out, "\n");
9680    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9681    utf8_printf(p->out, "%12.12s: ", "width");
9682    for (i=0;i<p->nWidth;i++) {
9683      raw_printf(p->out, "%d ", p->colWidth[i]);
9684    }
9685    raw_printf(p->out, "\n");
9686    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9687                p->zDbFilename ? p->zDbFilename : "");
9688  }else
9689
9690  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9691    if( nArg==2 ){
9692      p->statsOn = (u8)booleanValue(azArg[1]);
9693    }else if( nArg==1 ){
9694      display_stats(p->db, p, 0);
9695    }else{
9696      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9697      rc = 1;
9698    }
9699  }else
9700
9701  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9702   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9703                 || strncmp(azArg[0], "indexes", n)==0) )
9704  ){
9705    sqlite3_stmt *pStmt;
9706    char **azResult;
9707    int nRow, nAlloc;
9708    int ii;
9709    ShellText s;
9710    initText(&s);
9711    open_db(p, 0);
9712    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9713    if( rc ){
9714      sqlite3_finalize(pStmt);
9715      return shellDatabaseError(p->db);
9716    }
9717
9718    if( nArg>2 && c=='i' ){
9719      /* It is an historical accident that the .indexes command shows an error
9720      ** when called with the wrong number of arguments whereas the .tables
9721      ** command does not. */
9722      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9723      rc = 1;
9724      sqlite3_finalize(pStmt);
9725      goto meta_command_exit;
9726    }
9727    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9728      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9729      if( zDbName==0 ) continue;
9730      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9731      if( sqlite3_stricmp(zDbName, "main")==0 ){
9732        appendText(&s, "SELECT name FROM ", 0);
9733      }else{
9734        appendText(&s, "SELECT ", 0);
9735        appendText(&s, zDbName, '\'');
9736        appendText(&s, "||'.'||name FROM ", 0);
9737      }
9738      appendText(&s, zDbName, '"');
9739      appendText(&s, ".sqlite_schema ", 0);
9740      if( c=='t' ){
9741        appendText(&s," WHERE type IN ('table','view')"
9742                      "   AND name NOT LIKE 'sqlite_%'"
9743                      "   AND name LIKE ?1", 0);
9744      }else{
9745        appendText(&s," WHERE type='index'"
9746                      "   AND tbl_name LIKE ?1", 0);
9747      }
9748    }
9749    rc = sqlite3_finalize(pStmt);
9750    appendText(&s, " ORDER BY 1", 0);
9751    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9752    freeText(&s);
9753    if( rc ) return shellDatabaseError(p->db);
9754
9755    /* Run the SQL statement prepared by the above block. Store the results
9756    ** as an array of nul-terminated strings in azResult[].  */
9757    nRow = nAlloc = 0;
9758    azResult = 0;
9759    if( nArg>1 ){
9760      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9761    }else{
9762      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9763    }
9764    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9765      if( nRow>=nAlloc ){
9766        char **azNew;
9767        int n2 = nAlloc*2 + 10;
9768        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9769        if( azNew==0 ) shell_out_of_memory();
9770        nAlloc = n2;
9771        azResult = azNew;
9772      }
9773      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9774      if( 0==azResult[nRow] ) shell_out_of_memory();
9775      nRow++;
9776    }
9777    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9778      rc = shellDatabaseError(p->db);
9779    }
9780
9781    /* Pretty-print the contents of array azResult[] to the output */
9782    if( rc==0 && nRow>0 ){
9783      int len, maxlen = 0;
9784      int i, j;
9785      int nPrintCol, nPrintRow;
9786      for(i=0; i<nRow; i++){
9787        len = strlen30(azResult[i]);
9788        if( len>maxlen ) maxlen = len;
9789      }
9790      nPrintCol = 80/(maxlen+2);
9791      if( nPrintCol<1 ) nPrintCol = 1;
9792      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9793      for(i=0; i<nPrintRow; i++){
9794        for(j=i; j<nRow; j+=nPrintRow){
9795          char *zSp = j<nPrintRow ? "" : "  ";
9796          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9797                      azResult[j] ? azResult[j]:"");
9798        }
9799        raw_printf(p->out, "\n");
9800      }
9801    }
9802
9803    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9804    sqlite3_free(azResult);
9805  }else
9806
9807  /* Begin redirecting output to the file "testcase-out.txt" */
9808  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9809    output_reset(p);
9810    p->out = output_file_open("testcase-out.txt", 0);
9811    if( p->out==0 ){
9812      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9813    }
9814    if( nArg>=2 ){
9815      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9816    }else{
9817      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9818    }
9819  }else
9820
9821#ifndef SQLITE_UNTESTABLE
9822  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9823    static const struct {
9824       const char *zCtrlName;   /* Name of a test-control option */
9825       int ctrlCode;            /* Integer code for that option */
9826       const char *zUsage;      /* Usage notes */
9827    } aCtrl[] = {
9828      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9829      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9830    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9831    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9832      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9833      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9834    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9835      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9836      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9837      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9838      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9839      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9840#ifdef YYCOVERAGE
9841      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9842#endif
9843      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9844      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9845      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9846      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9847    };
9848    int testctrl = -1;
9849    int iCtrl = -1;
9850    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9851    int isOk = 0;
9852    int i, n2;
9853    const char *zCmd = 0;
9854
9855    open_db(p, 0);
9856    zCmd = nArg>=2 ? azArg[1] : "help";
9857
9858    /* The argument can optionally begin with "-" or "--" */
9859    if( zCmd[0]=='-' && zCmd[1] ){
9860      zCmd++;
9861      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9862    }
9863
9864    /* --help lists all test-controls */
9865    if( strcmp(zCmd,"help")==0 ){
9866      utf8_printf(p->out, "Available test-controls:\n");
9867      for(i=0; i<ArraySize(aCtrl); i++){
9868        utf8_printf(p->out, "  .testctrl %s %s\n",
9869                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9870      }
9871      rc = 1;
9872      goto meta_command_exit;
9873    }
9874
9875    /* convert testctrl text option to value. allow any unique prefix
9876    ** of the option name, or a numerical value. */
9877    n2 = strlen30(zCmd);
9878    for(i=0; i<ArraySize(aCtrl); i++){
9879      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9880        if( testctrl<0 ){
9881          testctrl = aCtrl[i].ctrlCode;
9882          iCtrl = i;
9883        }else{
9884          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9885                              "Use \".testctrl --help\" for help\n", zCmd);
9886          rc = 1;
9887          goto meta_command_exit;
9888        }
9889      }
9890    }
9891    if( testctrl<0 ){
9892      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9893                         "Use \".testctrl --help\" for help\n", zCmd);
9894    }else{
9895      switch(testctrl){
9896
9897        /* sqlite3_test_control(int, db, int) */
9898        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9899          if( nArg==3 ){
9900            int opt = (int)strtol(azArg[2], 0, 0);
9901            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9902            isOk = 3;
9903          }
9904          break;
9905
9906        /* sqlite3_test_control(int) */
9907        case SQLITE_TESTCTRL_PRNG_SAVE:
9908        case SQLITE_TESTCTRL_PRNG_RESTORE:
9909        case SQLITE_TESTCTRL_PRNG_RESET:
9910        case SQLITE_TESTCTRL_BYTEORDER:
9911          if( nArg==2 ){
9912            rc2 = sqlite3_test_control(testctrl);
9913            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9914          }
9915          break;
9916
9917        /* sqlite3_test_control(int, uint) */
9918        case SQLITE_TESTCTRL_PENDING_BYTE:
9919          if( nArg==3 ){
9920            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9921            rc2 = sqlite3_test_control(testctrl, opt);
9922            isOk = 3;
9923          }
9924          break;
9925
9926        /* sqlite3_test_control(int, int, sqlite3*) */
9927        case SQLITE_TESTCTRL_PRNG_SEED:
9928          if( nArg==3 || nArg==4 ){
9929            int ii = (int)integerValue(azArg[2]);
9930            sqlite3 *db;
9931            if( ii==0 && strcmp(azArg[2],"random")==0 ){
9932              sqlite3_randomness(sizeof(ii),&ii);
9933              printf("-- random seed: %d\n", ii);
9934            }
9935            if( nArg==3 ){
9936              db = 0;
9937            }else{
9938              db = p->db;
9939              /* Make sure the schema has been loaded */
9940              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
9941            }
9942            rc2 = sqlite3_test_control(testctrl, ii, db);
9943            isOk = 3;
9944          }
9945          break;
9946
9947        /* sqlite3_test_control(int, int) */
9948        case SQLITE_TESTCTRL_ASSERT:
9949        case SQLITE_TESTCTRL_ALWAYS:
9950          if( nArg==3 ){
9951            int opt = booleanValue(azArg[2]);
9952            rc2 = sqlite3_test_control(testctrl, opt);
9953            isOk = 1;
9954          }
9955          break;
9956
9957        /* sqlite3_test_control(int, int) */
9958        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
9959        case SQLITE_TESTCTRL_NEVER_CORRUPT:
9960          if( nArg==3 ){
9961            int opt = booleanValue(azArg[2]);
9962            rc2 = sqlite3_test_control(testctrl, opt);
9963            isOk = 3;
9964          }
9965          break;
9966
9967        /* sqlite3_test_control(sqlite3*) */
9968        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
9969          rc2 = sqlite3_test_control(testctrl, p->db);
9970          isOk = 3;
9971          break;
9972
9973        case SQLITE_TESTCTRL_IMPOSTER:
9974          if( nArg==5 ){
9975            rc2 = sqlite3_test_control(testctrl, p->db,
9976                          azArg[2],
9977                          integerValue(azArg[3]),
9978                          integerValue(azArg[4]));
9979            isOk = 3;
9980          }
9981          break;
9982
9983#ifdef YYCOVERAGE
9984        case SQLITE_TESTCTRL_PARSER_COVERAGE:
9985          if( nArg==2 ){
9986            sqlite3_test_control(testctrl, p->out);
9987            isOk = 3;
9988          }
9989#endif
9990      }
9991    }
9992    if( isOk==0 && iCtrl>=0 ){
9993      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
9994      rc = 1;
9995    }else if( isOk==1 ){
9996      raw_printf(p->out, "%d\n", rc2);
9997    }else if( isOk==2 ){
9998      raw_printf(p->out, "0x%08x\n", rc2);
9999    }
10000  }else
10001#endif /* !defined(SQLITE_UNTESTABLE) */
10002
10003  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10004    open_db(p, 0);
10005    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10006  }else
10007
10008  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10009    if( nArg==2 ){
10010      enableTimer = booleanValue(azArg[1]);
10011      if( enableTimer && !HAS_TIMER ){
10012        raw_printf(stderr, "Error: timer not available on this system.\n");
10013        enableTimer = 0;
10014      }
10015    }else{
10016      raw_printf(stderr, "Usage: .timer on|off\n");
10017      rc = 1;
10018    }
10019  }else
10020
10021#ifndef SQLITE_OMIT_TRACE
10022  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10023    int mType = 0;
10024    int jj;
10025    open_db(p, 0);
10026    for(jj=1; jj<nArg; jj++){
10027      const char *z = azArg[jj];
10028      if( z[0]=='-' ){
10029        if( optionMatch(z, "expanded") ){
10030          p->eTraceType = SHELL_TRACE_EXPANDED;
10031        }
10032#ifdef SQLITE_ENABLE_NORMALIZE
10033        else if( optionMatch(z, "normalized") ){
10034          p->eTraceType = SHELL_TRACE_NORMALIZED;
10035        }
10036#endif
10037        else if( optionMatch(z, "plain") ){
10038          p->eTraceType = SHELL_TRACE_PLAIN;
10039        }
10040        else if( optionMatch(z, "profile") ){
10041          mType |= SQLITE_TRACE_PROFILE;
10042        }
10043        else if( optionMatch(z, "row") ){
10044          mType |= SQLITE_TRACE_ROW;
10045        }
10046        else if( optionMatch(z, "stmt") ){
10047          mType |= SQLITE_TRACE_STMT;
10048        }
10049        else if( optionMatch(z, "close") ){
10050          mType |= SQLITE_TRACE_CLOSE;
10051        }
10052        else {
10053          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10054          rc = 1;
10055          goto meta_command_exit;
10056        }
10057      }else{
10058        output_file_close(p->traceOut);
10059        p->traceOut = output_file_open(azArg[1], 0);
10060      }
10061    }
10062    if( p->traceOut==0 ){
10063      sqlite3_trace_v2(p->db, 0, 0, 0);
10064    }else{
10065      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10066      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10067    }
10068  }else
10069#endif /* !defined(SQLITE_OMIT_TRACE) */
10070
10071#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10072  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10073    int ii;
10074    int lenOpt;
10075    char *zOpt;
10076    if( nArg<2 ){
10077      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10078      rc = 1;
10079      goto meta_command_exit;
10080    }
10081    open_db(p, 0);
10082    zOpt = azArg[1];
10083    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10084    lenOpt = (int)strlen(zOpt);
10085    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10086      assert( azArg[nArg]==0 );
10087      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10088    }else{
10089      for(ii=1; ii<nArg; ii++){
10090        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10091      }
10092    }
10093  }else
10094#endif
10095
10096#if SQLITE_USER_AUTHENTICATION
10097  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10098    if( nArg<2 ){
10099      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10100      rc = 1;
10101      goto meta_command_exit;
10102    }
10103    open_db(p, 0);
10104    if( strcmp(azArg[1],"login")==0 ){
10105      if( nArg!=4 ){
10106        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10107        rc = 1;
10108        goto meta_command_exit;
10109      }
10110      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10111                                     strlen30(azArg[3]));
10112      if( rc ){
10113        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10114        rc = 1;
10115      }
10116    }else if( strcmp(azArg[1],"add")==0 ){
10117      if( nArg!=5 ){
10118        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10119        rc = 1;
10120        goto meta_command_exit;
10121      }
10122      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10123                            booleanValue(azArg[4]));
10124      if( rc ){
10125        raw_printf(stderr, "User-Add failed: %d\n", rc);
10126        rc = 1;
10127      }
10128    }else if( strcmp(azArg[1],"edit")==0 ){
10129      if( nArg!=5 ){
10130        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10131        rc = 1;
10132        goto meta_command_exit;
10133      }
10134      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10135                              booleanValue(azArg[4]));
10136      if( rc ){
10137        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10138        rc = 1;
10139      }
10140    }else if( strcmp(azArg[1],"delete")==0 ){
10141      if( nArg!=3 ){
10142        raw_printf(stderr, "Usage: .user delete USER\n");
10143        rc = 1;
10144        goto meta_command_exit;
10145      }
10146      rc = sqlite3_user_delete(p->db, azArg[2]);
10147      if( rc ){
10148        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10149        rc = 1;
10150      }
10151    }else{
10152      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10153      rc = 1;
10154      goto meta_command_exit;
10155    }
10156  }else
10157#endif /* SQLITE_USER_AUTHENTICATION */
10158
10159  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10160    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10161        sqlite3_libversion(), sqlite3_sourceid());
10162#if SQLITE_HAVE_ZLIB
10163    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10164#endif
10165#define CTIMEOPT_VAL_(opt) #opt
10166#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10167#if defined(__clang__) && defined(__clang_major__)
10168    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10169                    CTIMEOPT_VAL(__clang_minor__) "."
10170                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10171#elif defined(_MSC_VER)
10172    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10173#elif defined(__GNUC__) && defined(__VERSION__)
10174    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10175#endif
10176  }else
10177
10178  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10179    const char *zDbName = nArg==2 ? azArg[1] : "main";
10180    sqlite3_vfs *pVfs = 0;
10181    if( p->db ){
10182      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10183      if( pVfs ){
10184        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10185        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10186        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10187        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10188      }
10189    }
10190  }else
10191
10192  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10193    sqlite3_vfs *pVfs;
10194    sqlite3_vfs *pCurrent = 0;
10195    if( p->db ){
10196      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10197    }
10198    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10199      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10200           pVfs==pCurrent ? "  <--- CURRENT" : "");
10201      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10202      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10203      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10204      if( pVfs->pNext ){
10205        raw_printf(p->out, "-----------------------------------\n");
10206      }
10207    }
10208  }else
10209
10210  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10211    const char *zDbName = nArg==2 ? azArg[1] : "main";
10212    char *zVfsName = 0;
10213    if( p->db ){
10214      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10215      if( zVfsName ){
10216        utf8_printf(p->out, "%s\n", zVfsName);
10217        sqlite3_free(zVfsName);
10218      }
10219    }
10220  }else
10221
10222#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10223  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10224    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
10225  }else
10226#endif
10227
10228  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10229    int j;
10230    assert( nArg<=ArraySize(azArg) );
10231    p->nWidth = nArg-1;
10232    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10233    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10234    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10235    for(j=1; j<nArg; j++){
10236      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10237    }
10238  }else
10239
10240  {
10241    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10242      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10243    rc = 1;
10244  }
10245
10246meta_command_exit:
10247  if( p->outCount ){
10248    p->outCount--;
10249    if( p->outCount==0 ) output_reset(p);
10250  }
10251  return rc;
10252}
10253
10254/*
10255** Return TRUE if a semicolon occurs anywhere in the first N characters
10256** of string z[].
10257*/
10258static int line_contains_semicolon(const char *z, int N){
10259  int i;
10260  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10261  return 0;
10262}
10263
10264/*
10265** Test to see if a line consists entirely of whitespace.
10266*/
10267static int _all_whitespace(const char *z){
10268  for(; *z; z++){
10269    if( IsSpace(z[0]) ) continue;
10270    if( *z=='/' && z[1]=='*' ){
10271      z += 2;
10272      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10273      if( *z==0 ) return 0;
10274      z++;
10275      continue;
10276    }
10277    if( *z=='-' && z[1]=='-' ){
10278      z += 2;
10279      while( *z && *z!='\n' ){ z++; }
10280      if( *z==0 ) return 1;
10281      continue;
10282    }
10283    return 0;
10284  }
10285  return 1;
10286}
10287
10288/*
10289** Return TRUE if the line typed in is an SQL command terminator other
10290** than a semi-colon.  The SQL Server style "go" command is understood
10291** as is the Oracle "/".
10292*/
10293static int line_is_command_terminator(const char *zLine){
10294  while( IsSpace(zLine[0]) ){ zLine++; };
10295  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10296    return 1;  /* Oracle */
10297  }
10298  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10299         && _all_whitespace(&zLine[2]) ){
10300    return 1;  /* SQL Server */
10301  }
10302  return 0;
10303}
10304
10305/*
10306** We need a default sqlite3_complete() implementation to use in case
10307** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10308** any arbitrary text is a complete SQL statement.  This is not very
10309** user-friendly, but it does seem to work.
10310*/
10311#ifdef SQLITE_OMIT_COMPLETE
10312#define sqlite3_complete(x) 1
10313#endif
10314
10315/*
10316** Return true if zSql is a complete SQL statement.  Return false if it
10317** ends in the middle of a string literal or C-style comment.
10318*/
10319static int line_is_complete(char *zSql, int nSql){
10320  int rc;
10321  if( zSql==0 ) return 1;
10322  zSql[nSql] = ';';
10323  zSql[nSql+1] = 0;
10324  rc = sqlite3_complete(zSql);
10325  zSql[nSql] = 0;
10326  return rc;
10327}
10328
10329/*
10330** Run a single line of SQL.  Return the number of errors.
10331*/
10332static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10333  int rc;
10334  char *zErrMsg = 0;
10335
10336  open_db(p, 0);
10337  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10338  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10339  BEGIN_TIMER;
10340  rc = shell_exec(p, zSql, &zErrMsg);
10341  END_TIMER;
10342  if( rc || zErrMsg ){
10343    char zPrefix[100];
10344    if( in!=0 || !stdin_is_interactive ){
10345      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10346                       "Error: near line %d:", startline);
10347    }else{
10348      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10349    }
10350    if( zErrMsg!=0 ){
10351      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10352      sqlite3_free(zErrMsg);
10353      zErrMsg = 0;
10354    }else{
10355      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10356    }
10357    return 1;
10358  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10359    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
10360            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10361  }
10362  return 0;
10363}
10364
10365
10366/*
10367** Read input from *in and process it.  If *in==0 then input
10368** is interactive - the user is typing it it.  Otherwise, input
10369** is coming from a file or device.  A prompt is issued and history
10370** is saved only if input is interactive.  An interrupt signal will
10371** cause this routine to exit immediately, unless input is interactive.
10372**
10373** Return the number of errors.
10374*/
10375static int process_input(ShellState *p){
10376  char *zLine = 0;          /* A single input line */
10377  char *zSql = 0;           /* Accumulated SQL text */
10378  int nLine;                /* Length of current line */
10379  int nSql = 0;             /* Bytes of zSql[] used */
10380  int nAlloc = 0;           /* Allocated zSql[] space */
10381  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10382  int rc;                   /* Error code */
10383  int errCnt = 0;           /* Number of errors seen */
10384  int startline = 0;        /* Line number for start of current input */
10385
10386  p->lineno = 0;
10387  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10388    fflush(p->out);
10389    zLine = one_input_line(p->in, zLine, nSql>0);
10390    if( zLine==0 ){
10391      /* End of input */
10392      if( p->in==0 && stdin_is_interactive ) printf("\n");
10393      break;
10394    }
10395    if( seenInterrupt ){
10396      if( p->in!=0 ) break;
10397      seenInterrupt = 0;
10398    }
10399    p->lineno++;
10400    if( nSql==0 && _all_whitespace(zLine) ){
10401      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10402      continue;
10403    }
10404    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10405      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10406      if( zLine[0]=='.' ){
10407        rc = do_meta_command(zLine, p);
10408        if( rc==2 ){ /* exit requested */
10409          break;
10410        }else if( rc ){
10411          errCnt++;
10412        }
10413      }
10414      continue;
10415    }
10416    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10417      memcpy(zLine,";",2);
10418    }
10419    nLine = strlen30(zLine);
10420    if( nSql+nLine+2>=nAlloc ){
10421      nAlloc = nSql+nLine+100;
10422      zSql = realloc(zSql, nAlloc);
10423      if( zSql==0 ) shell_out_of_memory();
10424    }
10425    nSqlPrior = nSql;
10426    if( nSql==0 ){
10427      int i;
10428      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10429      assert( nAlloc>0 && zSql!=0 );
10430      memcpy(zSql, zLine+i, nLine+1-i);
10431      startline = p->lineno;
10432      nSql = nLine-i;
10433    }else{
10434      zSql[nSql++] = '\n';
10435      memcpy(zSql+nSql, zLine, nLine+1);
10436      nSql += nLine;
10437    }
10438    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10439                && sqlite3_complete(zSql) ){
10440      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10441      nSql = 0;
10442      if( p->outCount ){
10443        output_reset(p);
10444        p->outCount = 0;
10445      }else{
10446        clearTempFile(p);
10447      }
10448    }else if( nSql && _all_whitespace(zSql) ){
10449      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10450      nSql = 0;
10451    }
10452  }
10453  if( nSql && !_all_whitespace(zSql) ){
10454    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10455  }
10456  free(zSql);
10457  free(zLine);
10458  return errCnt>0;
10459}
10460
10461/*
10462** Return a pathname which is the user's home directory.  A
10463** 0 return indicates an error of some kind.
10464*/
10465static char *find_home_dir(int clearFlag){
10466  static char *home_dir = NULL;
10467  if( clearFlag ){
10468    free(home_dir);
10469    home_dir = 0;
10470    return 0;
10471  }
10472  if( home_dir ) return home_dir;
10473
10474#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10475     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10476  {
10477    struct passwd *pwent;
10478    uid_t uid = getuid();
10479    if( (pwent=getpwuid(uid)) != NULL) {
10480      home_dir = pwent->pw_dir;
10481    }
10482  }
10483#endif
10484
10485#if defined(_WIN32_WCE)
10486  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10487   */
10488  home_dir = "/";
10489#else
10490
10491#if defined(_WIN32) || defined(WIN32)
10492  if (!home_dir) {
10493    home_dir = getenv("USERPROFILE");
10494  }
10495#endif
10496
10497  if (!home_dir) {
10498    home_dir = getenv("HOME");
10499  }
10500
10501#if defined(_WIN32) || defined(WIN32)
10502  if (!home_dir) {
10503    char *zDrive, *zPath;
10504    int n;
10505    zDrive = getenv("HOMEDRIVE");
10506    zPath = getenv("HOMEPATH");
10507    if( zDrive && zPath ){
10508      n = strlen30(zDrive) + strlen30(zPath) + 1;
10509      home_dir = malloc( n );
10510      if( home_dir==0 ) return 0;
10511      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10512      return home_dir;
10513    }
10514    home_dir = "c:\\";
10515  }
10516#endif
10517
10518#endif /* !_WIN32_WCE */
10519
10520  if( home_dir ){
10521    int n = strlen30(home_dir) + 1;
10522    char *z = malloc( n );
10523    if( z ) memcpy(z, home_dir, n);
10524    home_dir = z;
10525  }
10526
10527  return home_dir;
10528}
10529
10530/*
10531** Read input from the file given by sqliterc_override.  Or if that
10532** parameter is NULL, take input from ~/.sqliterc
10533**
10534** Returns the number of errors.
10535*/
10536static void process_sqliterc(
10537  ShellState *p,                  /* Configuration data */
10538  const char *sqliterc_override   /* Name of config file. NULL to use default */
10539){
10540  char *home_dir = NULL;
10541  const char *sqliterc = sqliterc_override;
10542  char *zBuf = 0;
10543  FILE *inSaved = p->in;
10544  int savedLineno = p->lineno;
10545
10546  if (sqliterc == NULL) {
10547    home_dir = find_home_dir(0);
10548    if( home_dir==0 ){
10549      raw_printf(stderr, "-- warning: cannot find home directory;"
10550                      " cannot read ~/.sqliterc\n");
10551      return;
10552    }
10553    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10554    sqliterc = zBuf;
10555  }
10556  p->in = fopen(sqliterc,"rb");
10557  if( p->in ){
10558    if( stdin_is_interactive ){
10559      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10560    }
10561    process_input(p);
10562    fclose(p->in);
10563  }
10564  p->in = inSaved;
10565  p->lineno = savedLineno;
10566  sqlite3_free(zBuf);
10567}
10568
10569/*
10570** Show available command line options
10571*/
10572static const char zOptions[] =
10573#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10574  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10575#endif
10576  "   -append              append the database to the end of the file\n"
10577  "   -ascii               set output mode to 'ascii'\n"
10578  "   -bail                stop after hitting an error\n"
10579  "   -batch               force batch I/O\n"
10580  "   -box                 set output mode to 'box'\n"
10581  "   -column              set output mode to 'column'\n"
10582  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10583  "   -csv                 set output mode to 'csv'\n"
10584#if defined(SQLITE_ENABLE_DESERIALIZE)
10585  "   -deserialize         open the database using sqlite3_deserialize()\n"
10586#endif
10587  "   -echo                print commands before execution\n"
10588  "   -init FILENAME       read/process named file\n"
10589  "   -[no]header          turn headers on or off\n"
10590#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10591  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10592#endif
10593  "   -help                show this message\n"
10594  "   -html                set output mode to HTML\n"
10595  "   -interactive         force interactive I/O\n"
10596  "   -json                set output mode to 'json'\n"
10597  "   -line                set output mode to 'line'\n"
10598  "   -list                set output mode to 'list'\n"
10599  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10600  "   -markdown            set output mode to 'markdown'\n"
10601#if defined(SQLITE_ENABLE_DESERIALIZE)
10602  "   -maxsize N           maximum size for a --deserialize database\n"
10603#endif
10604  "   -memtrace            trace all memory allocations and deallocations\n"
10605  "   -mmap N              default mmap size set to N\n"
10606#ifdef SQLITE_ENABLE_MULTIPLEX
10607  "   -multiplex           enable the multiplexor VFS\n"
10608#endif
10609  "   -newline SEP         set output row separator. Default: '\\n'\n"
10610  "   -nofollow            refuse to open symbolic links to database files\n"
10611  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10612  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10613  "   -quote               set output mode to 'quote'\n"
10614  "   -readonly            open the database read-only\n"
10615  "   -separator SEP       set output column separator. Default: '|'\n"
10616#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10617  "   -sorterref SIZE      sorter references threshold size\n"
10618#endif
10619  "   -stats               print memory stats before each finalize\n"
10620  "   -table               set output mode to 'table'\n"
10621  "   -version             show SQLite version\n"
10622  "   -vfs NAME            use NAME as the default VFS\n"
10623#ifdef SQLITE_ENABLE_VFSTRACE
10624  "   -vfstrace            enable tracing of all VFS calls\n"
10625#endif
10626#ifdef SQLITE_HAVE_ZLIB
10627  "   -zip                 open the file as a ZIP Archive\n"
10628#endif
10629;
10630static void usage(int showDetail){
10631  utf8_printf(stderr,
10632      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10633      "FILENAME is the name of an SQLite database. A new database is created\n"
10634      "if the file does not previously exist.\n", Argv0);
10635  if( showDetail ){
10636    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10637  }else{
10638    raw_printf(stderr, "Use the -help option for additional information\n");
10639  }
10640  exit(1);
10641}
10642
10643/*
10644** Internal check:  Verify that the SQLite is uninitialized.  Print a
10645** error message if it is initialized.
10646*/
10647static void verify_uninitialized(void){
10648  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10649    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10650                        " initialization.\n");
10651  }
10652}
10653
10654/*
10655** Initialize the state information in data
10656*/
10657static void main_init(ShellState *data) {
10658  memset(data, 0, sizeof(*data));
10659  data->normalMode = data->cMode = data->mode = MODE_List;
10660  data->autoExplain = 1;
10661  memcpy(data->colSeparator,SEP_Column, 2);
10662  memcpy(data->rowSeparator,SEP_Row, 2);
10663  data->showHeader = 0;
10664  data->shellFlgs = SHFLG_Lookaside;
10665  verify_uninitialized();
10666  sqlite3_config(SQLITE_CONFIG_URI, 1);
10667  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10668  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10669  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10670  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10671}
10672
10673/*
10674** Output text to the console in a font that attracts extra attention.
10675*/
10676#ifdef _WIN32
10677static void printBold(const char *zText){
10678#if !SQLITE_OS_WINRT
10679  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10680  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10681  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10682  SetConsoleTextAttribute(out,
10683         FOREGROUND_RED|FOREGROUND_INTENSITY
10684  );
10685#endif
10686  printf("%s", zText);
10687#if !SQLITE_OS_WINRT
10688  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10689#endif
10690}
10691#else
10692static void printBold(const char *zText){
10693  printf("\033[1m%s\033[0m", zText);
10694}
10695#endif
10696
10697/*
10698** Get the argument to an --option.  Throw an error and die if no argument
10699** is available.
10700*/
10701static char *cmdline_option_value(int argc, char **argv, int i){
10702  if( i==argc ){
10703    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10704            argv[0], argv[argc-1]);
10705    exit(1);
10706  }
10707  return argv[i];
10708}
10709
10710#ifndef SQLITE_SHELL_IS_UTF8
10711#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10712#    define SQLITE_SHELL_IS_UTF8          (0)
10713#  else
10714#    define SQLITE_SHELL_IS_UTF8          (1)
10715#  endif
10716#endif
10717
10718#if SQLITE_SHELL_IS_UTF8
10719int SQLITE_CDECL main(int argc, char **argv){
10720#else
10721int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10722  char **argv;
10723#endif
10724  char *zErrMsg = 0;
10725  ShellState data;
10726  const char *zInitFile = 0;
10727  int i;
10728  int rc = 0;
10729  int warnInmemoryDb = 0;
10730  int readStdin = 1;
10731  int nCmd = 0;
10732  char **azCmd = 0;
10733  const char *zVfs = 0;           /* Value of -vfs command-line option */
10734#if !SQLITE_SHELL_IS_UTF8
10735  char **argvToFree = 0;
10736  int argcToFree = 0;
10737#endif
10738
10739  setBinaryMode(stdin, 0);
10740  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10741  stdin_is_interactive = isatty(0);
10742  stdout_is_console = isatty(1);
10743
10744#ifdef SQLITE_DEBUG
10745  registerOomSimulator();
10746#endif
10747
10748#if !defined(_WIN32_WCE)
10749  if( getenv("SQLITE_DEBUG_BREAK") ){
10750    if( isatty(0) && isatty(2) ){
10751      fprintf(stderr,
10752          "attach debugger to process %d and press any key to continue.\n",
10753          GETPID());
10754      fgetc(stdin);
10755    }else{
10756#if defined(_WIN32) || defined(WIN32)
10757#if SQLITE_OS_WINRT
10758      __debugbreak();
10759#else
10760      DebugBreak();
10761#endif
10762#elif defined(SIGTRAP)
10763      raise(SIGTRAP);
10764#endif
10765    }
10766  }
10767#endif
10768
10769#if USE_SYSTEM_SQLITE+0!=1
10770  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10771    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10772            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10773    exit(1);
10774  }
10775#endif
10776  main_init(&data);
10777
10778  /* On Windows, we must translate command-line arguments into UTF-8.
10779  ** The SQLite memory allocator subsystem has to be enabled in order to
10780  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10781  ** subsequent sqlite3_config() calls will work.  So copy all results into
10782  ** memory that does not come from the SQLite memory allocator.
10783  */
10784#if !SQLITE_SHELL_IS_UTF8
10785  sqlite3_initialize();
10786  argvToFree = malloc(sizeof(argv[0])*argc*2);
10787  argcToFree = argc;
10788  argv = argvToFree + argc;
10789  if( argv==0 ) shell_out_of_memory();
10790  for(i=0; i<argc; i++){
10791    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10792    int n;
10793    if( z==0 ) shell_out_of_memory();
10794    n = (int)strlen(z);
10795    argv[i] = malloc( n+1 );
10796    if( argv[i]==0 ) shell_out_of_memory();
10797    memcpy(argv[i], z, n+1);
10798    argvToFree[i] = argv[i];
10799    sqlite3_free(z);
10800  }
10801  sqlite3_shutdown();
10802#endif
10803
10804  assert( argc>=1 && argv && argv[0] );
10805  Argv0 = argv[0];
10806
10807  /* Make sure we have a valid signal handler early, before anything
10808  ** else is done.
10809  */
10810#ifdef SIGINT
10811  signal(SIGINT, interrupt_handler);
10812#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10813  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10814#endif
10815
10816#ifdef SQLITE_SHELL_DBNAME_PROC
10817  {
10818    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10819    ** of a C-function that will provide the name of the database file.  Use
10820    ** this compile-time option to embed this shell program in larger
10821    ** applications. */
10822    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10823    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10824    warnInmemoryDb = 0;
10825  }
10826#endif
10827
10828  /* Do an initial pass through the command-line argument to locate
10829  ** the name of the database file, the name of the initialization file,
10830  ** the size of the alternative malloc heap,
10831  ** and the first command to execute.
10832  */
10833  verify_uninitialized();
10834  for(i=1; i<argc; i++){
10835    char *z;
10836    z = argv[i];
10837    if( z[0]!='-' ){
10838      if( data.zDbFilename==0 ){
10839        data.zDbFilename = z;
10840      }else{
10841        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10842        ** mean that nothing is read from stdin */
10843        readStdin = 0;
10844        nCmd++;
10845        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10846        if( azCmd==0 ) shell_out_of_memory();
10847        azCmd[nCmd-1] = z;
10848      }
10849    }
10850    if( z[1]=='-' ) z++;
10851    if( strcmp(z,"-separator")==0
10852     || strcmp(z,"-nullvalue")==0
10853     || strcmp(z,"-newline")==0
10854     || strcmp(z,"-cmd")==0
10855    ){
10856      (void)cmdline_option_value(argc, argv, ++i);
10857    }else if( strcmp(z,"-init")==0 ){
10858      zInitFile = cmdline_option_value(argc, argv, ++i);
10859    }else if( strcmp(z,"-batch")==0 ){
10860      /* Need to check for batch mode here to so we can avoid printing
10861      ** informational messages (like from process_sqliterc) before
10862      ** we do the actual processing of arguments later in a second pass.
10863      */
10864      stdin_is_interactive = 0;
10865    }else if( strcmp(z,"-heap")==0 ){
10866#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10867      const char *zSize;
10868      sqlite3_int64 szHeap;
10869
10870      zSize = cmdline_option_value(argc, argv, ++i);
10871      szHeap = integerValue(zSize);
10872      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10873      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10874#else
10875      (void)cmdline_option_value(argc, argv, ++i);
10876#endif
10877    }else if( strcmp(z,"-pagecache")==0 ){
10878      int n, sz;
10879      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10880      if( sz>70000 ) sz = 70000;
10881      if( sz<0 ) sz = 0;
10882      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10883      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10884                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10885      data.shellFlgs |= SHFLG_Pagecache;
10886    }else if( strcmp(z,"-lookaside")==0 ){
10887      int n, sz;
10888      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10889      if( sz<0 ) sz = 0;
10890      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10891      if( n<0 ) n = 0;
10892      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10893      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10894#ifdef SQLITE_ENABLE_VFSTRACE
10895    }else if( strcmp(z,"-vfstrace")==0 ){
10896      extern int vfstrace_register(
10897         const char *zTraceName,
10898         const char *zOldVfsName,
10899         int (*xOut)(const char*,void*),
10900         void *pOutArg,
10901         int makeDefault
10902      );
10903      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10904#endif
10905#ifdef SQLITE_ENABLE_MULTIPLEX
10906    }else if( strcmp(z,"-multiplex")==0 ){
10907      extern int sqlite3_multiple_initialize(const char*,int);
10908      sqlite3_multiplex_initialize(0, 1);
10909#endif
10910    }else if( strcmp(z,"-mmap")==0 ){
10911      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10912      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
10913#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10914    }else if( strcmp(z,"-sorterref")==0 ){
10915      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10916      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
10917#endif
10918    }else if( strcmp(z,"-vfs")==0 ){
10919      zVfs = cmdline_option_value(argc, argv, ++i);
10920#ifdef SQLITE_HAVE_ZLIB
10921    }else if( strcmp(z,"-zip")==0 ){
10922      data.openMode = SHELL_OPEN_ZIPFILE;
10923#endif
10924    }else if( strcmp(z,"-append")==0 ){
10925      data.openMode = SHELL_OPEN_APPENDVFS;
10926#ifdef SQLITE_ENABLE_DESERIALIZE
10927    }else if( strcmp(z,"-deserialize")==0 ){
10928      data.openMode = SHELL_OPEN_DESERIALIZE;
10929    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10930      data.szMax = integerValue(argv[++i]);
10931#endif
10932    }else if( strcmp(z,"-readonly")==0 ){
10933      data.openMode = SHELL_OPEN_READONLY;
10934    }else if( strcmp(z,"-nofollow")==0 ){
10935      data.openFlags = SQLITE_OPEN_NOFOLLOW;
10936#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10937    }else if( strncmp(z, "-A",2)==0 ){
10938      /* All remaining command-line arguments are passed to the ".archive"
10939      ** command, so ignore them */
10940      break;
10941#endif
10942    }else if( strcmp(z, "-memtrace")==0 ){
10943      sqlite3MemTraceActivate(stderr);
10944    }
10945  }
10946  verify_uninitialized();
10947
10948
10949#ifdef SQLITE_SHELL_INIT_PROC
10950  {
10951    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
10952    ** of a C-function that will perform initialization actions on SQLite that
10953    ** occur just before or after sqlite3_initialize(). Use this compile-time
10954    ** option to embed this shell program in larger applications. */
10955    extern void SQLITE_SHELL_INIT_PROC(void);
10956    SQLITE_SHELL_INIT_PROC();
10957  }
10958#else
10959  /* All the sqlite3_config() calls have now been made. So it is safe
10960  ** to call sqlite3_initialize() and process any command line -vfs option. */
10961  sqlite3_initialize();
10962#endif
10963
10964  if( zVfs ){
10965    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
10966    if( pVfs ){
10967      sqlite3_vfs_register(pVfs, 1);
10968    }else{
10969      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
10970      exit(1);
10971    }
10972  }
10973
10974  if( data.zDbFilename==0 ){
10975#ifndef SQLITE_OMIT_MEMORYDB
10976    data.zDbFilename = ":memory:";
10977    warnInmemoryDb = argc==1;
10978#else
10979    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
10980    return 1;
10981#endif
10982  }
10983  data.out = stdout;
10984  sqlite3_appendvfs_init(0,0,0);
10985
10986  /* Go ahead and open the database file if it already exists.  If the
10987  ** file does not exist, delay opening it.  This prevents empty database
10988  ** files from being created if a user mistypes the database name argument
10989  ** to the sqlite command-line tool.
10990  */
10991  if( access(data.zDbFilename, 0)==0 ){
10992    open_db(&data, 0);
10993  }
10994
10995  /* Process the initialization file if there is one.  If no -init option
10996  ** is given on the command line, look for a file named ~/.sqliterc and
10997  ** try to process it.
10998  */
10999  process_sqliterc(&data,zInitFile);
11000
11001  /* Make a second pass through the command-line argument and set
11002  ** options.  This second pass is delayed until after the initialization
11003  ** file is processed so that the command-line arguments will override
11004  ** settings in the initialization file.
11005  */
11006  for(i=1; i<argc; i++){
11007    char *z = argv[i];
11008    if( z[0]!='-' ) continue;
11009    if( z[1]=='-' ){ z++; }
11010    if( strcmp(z,"-init")==0 ){
11011      i++;
11012    }else if( strcmp(z,"-html")==0 ){
11013      data.mode = MODE_Html;
11014    }else if( strcmp(z,"-list")==0 ){
11015      data.mode = MODE_List;
11016    }else if( strcmp(z,"-quote")==0 ){
11017      data.mode = MODE_Quote;
11018    }else if( strcmp(z,"-line")==0 ){
11019      data.mode = MODE_Line;
11020    }else if( strcmp(z,"-column")==0 ){
11021      data.mode = MODE_Column;
11022    }else if( strcmp(z,"-json")==0 ){
11023      data.mode = MODE_Json;
11024    }else if( strcmp(z,"-markdown")==0 ){
11025      data.mode = MODE_Markdown;
11026    }else if( strcmp(z,"-table")==0 ){
11027      data.mode = MODE_Table;
11028    }else if( strcmp(z,"-box")==0 ){
11029      data.mode = MODE_Box;
11030    }else if( strcmp(z,"-csv")==0 ){
11031      data.mode = MODE_Csv;
11032      memcpy(data.colSeparator,",",2);
11033#ifdef SQLITE_HAVE_ZLIB
11034    }else if( strcmp(z,"-zip")==0 ){
11035      data.openMode = SHELL_OPEN_ZIPFILE;
11036#endif
11037    }else if( strcmp(z,"-append")==0 ){
11038      data.openMode = SHELL_OPEN_APPENDVFS;
11039#ifdef SQLITE_ENABLE_DESERIALIZE
11040    }else if( strcmp(z,"-deserialize")==0 ){
11041      data.openMode = SHELL_OPEN_DESERIALIZE;
11042    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11043      data.szMax = integerValue(argv[++i]);
11044#endif
11045    }else if( strcmp(z,"-readonly")==0 ){
11046      data.openMode = SHELL_OPEN_READONLY;
11047    }else if( strcmp(z,"-nofollow")==0 ){
11048      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11049    }else if( strcmp(z,"-ascii")==0 ){
11050      data.mode = MODE_Ascii;
11051      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11052                       SEP_Unit);
11053      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11054                       SEP_Record);
11055    }else if( strcmp(z,"-separator")==0 ){
11056      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11057                       "%s",cmdline_option_value(argc,argv,++i));
11058    }else if( strcmp(z,"-newline")==0 ){
11059      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11060                       "%s",cmdline_option_value(argc,argv,++i));
11061    }else if( strcmp(z,"-nullvalue")==0 ){
11062      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11063                       "%s",cmdline_option_value(argc,argv,++i));
11064    }else if( strcmp(z,"-header")==0 ){
11065      data.showHeader = 1;
11066    }else if( strcmp(z,"-noheader")==0 ){
11067      data.showHeader = 0;
11068    }else if( strcmp(z,"-echo")==0 ){
11069      ShellSetFlag(&data, SHFLG_Echo);
11070    }else if( strcmp(z,"-eqp")==0 ){
11071      data.autoEQP = AUTOEQP_on;
11072    }else if( strcmp(z,"-eqpfull")==0 ){
11073      data.autoEQP = AUTOEQP_full;
11074    }else if( strcmp(z,"-stats")==0 ){
11075      data.statsOn = 1;
11076    }else if( strcmp(z,"-scanstats")==0 ){
11077      data.scanstatsOn = 1;
11078    }else if( strcmp(z,"-backslash")==0 ){
11079      /* Undocumented command-line option: -backslash
11080      ** Causes C-style backslash escapes to be evaluated in SQL statements
11081      ** prior to sending the SQL into SQLite.  Useful for injecting
11082      ** crazy bytes in the middle of SQL statements for testing and debugging.
11083      */
11084      ShellSetFlag(&data, SHFLG_Backslash);
11085    }else if( strcmp(z,"-bail")==0 ){
11086      bail_on_error = 1;
11087    }else if( strcmp(z,"-version")==0 ){
11088      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11089      return 0;
11090    }else if( strcmp(z,"-interactive")==0 ){
11091      stdin_is_interactive = 1;
11092    }else if( strcmp(z,"-batch")==0 ){
11093      stdin_is_interactive = 0;
11094    }else if( strcmp(z,"-heap")==0 ){
11095      i++;
11096    }else if( strcmp(z,"-pagecache")==0 ){
11097      i+=2;
11098    }else if( strcmp(z,"-lookaside")==0 ){
11099      i+=2;
11100    }else if( strcmp(z,"-mmap")==0 ){
11101      i++;
11102    }else if( strcmp(z,"-memtrace")==0 ){
11103      i++;
11104#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11105    }else if( strcmp(z,"-sorterref")==0 ){
11106      i++;
11107#endif
11108    }else if( strcmp(z,"-vfs")==0 ){
11109      i++;
11110#ifdef SQLITE_ENABLE_VFSTRACE
11111    }else if( strcmp(z,"-vfstrace")==0 ){
11112      i++;
11113#endif
11114#ifdef SQLITE_ENABLE_MULTIPLEX
11115    }else if( strcmp(z,"-multiplex")==0 ){
11116      i++;
11117#endif
11118    }else if( strcmp(z,"-help")==0 ){
11119      usage(1);
11120    }else if( strcmp(z,"-cmd")==0 ){
11121      /* Run commands that follow -cmd first and separately from commands
11122      ** that simply appear on the command-line.  This seems goofy.  It would
11123      ** be better if all commands ran in the order that they appear.  But
11124      ** we retain the goofy behavior for historical compatibility. */
11125      if( i==argc-1 ) break;
11126      z = cmdline_option_value(argc,argv,++i);
11127      if( z[0]=='.' ){
11128        rc = do_meta_command(z, &data);
11129        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11130      }else{
11131        open_db(&data, 0);
11132        rc = shell_exec(&data, z, &zErrMsg);
11133        if( zErrMsg!=0 ){
11134          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11135          if( bail_on_error ) return rc!=0 ? rc : 1;
11136        }else if( rc!=0 ){
11137          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11138          if( bail_on_error ) return rc;
11139        }
11140      }
11141#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11142    }else if( strncmp(z, "-A", 2)==0 ){
11143      if( nCmd>0 ){
11144        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11145                            " with \"%s\"\n", z);
11146        return 1;
11147      }
11148      open_db(&data, OPEN_DB_ZIPFILE);
11149      if( z[2] ){
11150        argv[i] = &z[2];
11151        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11152      }else{
11153        arDotCommand(&data, 1, argv+i, argc-i);
11154      }
11155      readStdin = 0;
11156      break;
11157#endif
11158    }else{
11159      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11160      raw_printf(stderr,"Use -help for a list of options.\n");
11161      return 1;
11162    }
11163    data.cMode = data.mode;
11164  }
11165
11166  if( !readStdin ){
11167    /* Run all arguments that do not begin with '-' as if they were separate
11168    ** command-line inputs, except for the argToSkip argument which contains
11169    ** the database filename.
11170    */
11171    for(i=0; i<nCmd; i++){
11172      if( azCmd[i][0]=='.' ){
11173        rc = do_meta_command(azCmd[i], &data);
11174        if( rc ) return rc==2 ? 0 : rc;
11175      }else{
11176        open_db(&data, 0);
11177        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11178        if( zErrMsg!=0 ){
11179          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11180          return rc!=0 ? rc : 1;
11181        }else if( rc!=0 ){
11182          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11183          return rc;
11184        }
11185      }
11186    }
11187    free(azCmd);
11188  }else{
11189    /* Run commands received from standard input
11190    */
11191    if( stdin_is_interactive ){
11192      char *zHome;
11193      char *zHistory;
11194      int nHistory;
11195      printf(
11196        "SQLite version %s %.19s\n" /*extra-version-info*/
11197        "Enter \".help\" for usage hints.\n",
11198        sqlite3_libversion(), sqlite3_sourceid()
11199      );
11200      if( warnInmemoryDb ){
11201        printf("Connected to a ");
11202        printBold("transient in-memory database");
11203        printf(".\nUse \".open FILENAME\" to reopen on a "
11204               "persistent database.\n");
11205      }
11206      zHistory = getenv("SQLITE_HISTORY");
11207      if( zHistory ){
11208        zHistory = strdup(zHistory);
11209      }else if( (zHome = find_home_dir(0))!=0 ){
11210        nHistory = strlen30(zHome) + 20;
11211        if( (zHistory = malloc(nHistory))!=0 ){
11212          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11213        }
11214      }
11215      if( zHistory ){ shell_read_history(zHistory); }
11216#if HAVE_READLINE || HAVE_EDITLINE
11217      rl_attempted_completion_function = readline_completion;
11218#elif HAVE_LINENOISE
11219      linenoiseSetCompletionCallback(linenoise_completion);
11220#endif
11221      data.in = 0;
11222      rc = process_input(&data);
11223      if( zHistory ){
11224        shell_stifle_history(2000);
11225        shell_write_history(zHistory);
11226        free(zHistory);
11227      }
11228    }else{
11229      data.in = stdin;
11230      rc = process_input(&data);
11231    }
11232  }
11233  set_table_name(&data, 0);
11234  if( data.db ){
11235    session_close_all(&data);
11236    close_db(data.db);
11237  }
11238  sqlite3_free(data.zFreeOnClose);
11239  find_home_dir(1);
11240  output_reset(&data);
11241  data.doXdgOpen = 0;
11242  clearTempFile(&data);
11243#if !SQLITE_SHELL_IS_UTF8
11244  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11245  free(argvToFree);
11246#endif
11247  free(data.colWidth);
11248  /* Clear the global data structure so that valgrind will detect memory
11249  ** leaks */
11250  memset(&data, 0, sizeof(data));
11251  return rc;
11252}
11253