xref: /sqlite-3.40.0/src/shell.c.in (revision 65022716)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Optionally #include a user-defined header, whereby compilation options
22** may be set prior to where they take effect, but after platform setup.
23** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
24** file. Note that this macro has a like effect on sqlite3.c compilation.
25*/
26# define SHELL_STRINGIFY_(f) #f
27# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
28#ifdef SQLITE_CUSTOM_INCLUDE
29# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
30#endif
31
32/*
33** Determine if we are dealing with WinRT, which provides only a subset of
34** the full Win32 API.
35*/
36#if !defined(SQLITE_OS_WINRT)
37# define SQLITE_OS_WINRT 0
38#endif
39
40/*
41** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
42** somewhat for use as a WASM module in a web browser. This flag
43** should only be used when building the "fiddle" web application, as
44** the browser-mode build has much different user input requirements
45** and this build mode rewires the user input subsystem to account for
46** that.
47*/
48
49/*
50** Warning pragmas copied from msvc.h in the core.
51*/
52#if defined(_MSC_VER)
53#pragma warning(disable : 4054)
54#pragma warning(disable : 4055)
55#pragma warning(disable : 4100)
56#pragma warning(disable : 4127)
57#pragma warning(disable : 4130)
58#pragma warning(disable : 4152)
59#pragma warning(disable : 4189)
60#pragma warning(disable : 4206)
61#pragma warning(disable : 4210)
62#pragma warning(disable : 4232)
63#pragma warning(disable : 4244)
64#pragma warning(disable : 4305)
65#pragma warning(disable : 4306)
66#pragma warning(disable : 4702)
67#pragma warning(disable : 4706)
68#endif /* defined(_MSC_VER) */
69
70/*
71** No support for loadable extensions in VxWorks.
72*/
73#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
74# define SQLITE_OMIT_LOAD_EXTENSION 1
75#endif
76
77/*
78** Enable large-file support for fopen() and friends on unix.
79*/
80#ifndef SQLITE_DISABLE_LFS
81# define _LARGE_FILE       1
82# ifndef _FILE_OFFSET_BITS
83#   define _FILE_OFFSET_BITS 64
84# endif
85# define _LARGEFILE_SOURCE 1
86#endif
87
88#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
89/*
90** emcc requires _POSIX_SOURCE (or one of several similar defines)
91** to expose strdup().
92*/
93# define _POSIX_SOURCE
94#endif
95
96#include <stdlib.h>
97#include <string.h>
98#include <stdio.h>
99#include <assert.h>
100#include "sqlite3.h"
101typedef sqlite3_int64 i64;
102typedef sqlite3_uint64 u64;
103typedef unsigned char u8;
104#if SQLITE_USER_AUTHENTICATION
105# include "sqlite3userauth.h"
106#endif
107#include <ctype.h>
108#include <stdarg.h>
109
110#if !defined(_WIN32) && !defined(WIN32)
111# include <signal.h>
112# if !defined(__RTP__) && !defined(_WRS_KERNEL)
113#  include <pwd.h>
114# endif
115#endif
116#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
117# include <unistd.h>
118# include <dirent.h>
119# define GETPID getpid
120# if defined(__MINGW32__)
121#  define DIRENT dirent
122#  ifndef S_ISLNK
123#   define S_ISLNK(mode) (0)
124#  endif
125# endif
126#else
127# define GETPID (int)GetCurrentProcessId
128#endif
129#include <sys/types.h>
130#include <sys/stat.h>
131
132#if HAVE_READLINE
133# include <readline/readline.h>
134# include <readline/history.h>
135#endif
136
137#if HAVE_EDITLINE
138# include <editline/readline.h>
139#endif
140
141#if HAVE_EDITLINE || HAVE_READLINE
142
143# define shell_add_history(X) add_history(X)
144# define shell_read_history(X) read_history(X)
145# define shell_write_history(X) write_history(X)
146# define shell_stifle_history(X) stifle_history(X)
147# define shell_readline(X) readline(X)
148
149#elif HAVE_LINENOISE
150
151# include "linenoise.h"
152# define shell_add_history(X) linenoiseHistoryAdd(X)
153# define shell_read_history(X) linenoiseHistoryLoad(X)
154# define shell_write_history(X) linenoiseHistorySave(X)
155# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
156# define shell_readline(X) linenoise(X)
157
158#else
159
160# define shell_read_history(X)
161# define shell_write_history(X)
162# define shell_stifle_history(X)
163
164# define SHELL_USE_LOCAL_GETLINE 1
165#endif
166
167
168#if defined(_WIN32) || defined(WIN32)
169# if SQLITE_OS_WINRT
170#  define SQLITE_OMIT_POPEN 1
171# else
172#  include <io.h>
173#  include <fcntl.h>
174#  define isatty(h) _isatty(h)
175#  ifndef access
176#   define access(f,m) _access((f),(m))
177#  endif
178#  ifndef unlink
179#   define unlink _unlink
180#  endif
181#  ifndef strdup
182#   define strdup _strdup
183#  endif
184#  undef popen
185#  define popen _popen
186#  undef pclose
187#  define pclose _pclose
188# endif
189#else
190 /* Make sure isatty() has a prototype. */
191 extern int isatty(int);
192
193# if !defined(__RTP__) && !defined(_WRS_KERNEL)
194  /* popen and pclose are not C89 functions and so are
195  ** sometimes omitted from the <stdio.h> header */
196   extern FILE *popen(const char*,const char*);
197   extern int pclose(FILE*);
198# else
199#  define SQLITE_OMIT_POPEN 1
200# endif
201#endif
202
203#if defined(_WIN32_WCE)
204/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
205 * thus we always assume that we have a console. That can be
206 * overridden with the -batch command line option.
207 */
208#define isatty(x) 1
209#endif
210
211/* ctype macros that work with signed characters */
212#define IsSpace(X)  isspace((unsigned char)X)
213#define IsDigit(X)  isdigit((unsigned char)X)
214#define ToLower(X)  (char)tolower((unsigned char)X)
215
216#if defined(_WIN32) || defined(WIN32)
217#if SQLITE_OS_WINRT
218#include <intrin.h>
219#endif
220#include <windows.h>
221
222/* string conversion routines only needed on Win32 */
223extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
224extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
225extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
226extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
227#endif
228
229/* On Windows, we normally run with output mode of TEXT so that \n characters
230** are automatically translated into \r\n.  However, this behavior needs
231** to be disabled in some cases (ex: when generating CSV output and when
232** rendering quoted strings that contain \n characters).  The following
233** routines take care of that.
234*/
235#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
236static void setBinaryMode(FILE *file, int isOutput){
237  if( isOutput ) fflush(file);
238  _setmode(_fileno(file), _O_BINARY);
239}
240static void setTextMode(FILE *file, int isOutput){
241  if( isOutput ) fflush(file);
242  _setmode(_fileno(file), _O_TEXT);
243}
244#else
245# define setBinaryMode(X,Y)
246# define setTextMode(X,Y)
247#endif
248
249/* True if the timer is enabled */
250static int enableTimer = 0;
251
252/* Return the current wall-clock time */
253static sqlite3_int64 timeOfDay(void){
254  static sqlite3_vfs *clockVfs = 0;
255  sqlite3_int64 t;
256  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
257  if( clockVfs==0 ) return 0;  /* Never actually happens */
258  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
259    clockVfs->xCurrentTimeInt64(clockVfs, &t);
260  }else{
261    double r;
262    clockVfs->xCurrentTime(clockVfs, &r);
263    t = (sqlite3_int64)(r*86400000.0);
264  }
265  return t;
266}
267
268#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
269#include <sys/time.h>
270#include <sys/resource.h>
271
272/* VxWorks does not support getrusage() as far as we can determine */
273#if defined(_WRS_KERNEL) || defined(__RTP__)
274struct rusage {
275  struct timeval ru_utime; /* user CPU time used */
276  struct timeval ru_stime; /* system CPU time used */
277};
278#define getrusage(A,B) memset(B,0,sizeof(*B))
279#endif
280
281/* Saved resource information for the beginning of an operation */
282static struct rusage sBegin;  /* CPU time at start */
283static sqlite3_int64 iBegin;  /* Wall-clock time at start */
284
285/*
286** Begin timing an operation
287*/
288static void beginTimer(void){
289  if( enableTimer ){
290    getrusage(RUSAGE_SELF, &sBegin);
291    iBegin = timeOfDay();
292  }
293}
294
295/* Return the difference of two time_structs in seconds */
296static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
297  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
298         (double)(pEnd->tv_sec - pStart->tv_sec);
299}
300
301/*
302** Print the timing results.
303*/
304static void endTimer(void){
305  if( enableTimer ){
306    sqlite3_int64 iEnd = timeOfDay();
307    struct rusage sEnd;
308    getrusage(RUSAGE_SELF, &sEnd);
309    printf("Run Time: real %.3f user %f sys %f\n",
310       (iEnd - iBegin)*0.001,
311       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
312       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
313  }
314}
315
316#define BEGIN_TIMER beginTimer()
317#define END_TIMER endTimer()
318#define HAS_TIMER 1
319
320#elif (defined(_WIN32) || defined(WIN32))
321
322/* Saved resource information for the beginning of an operation */
323static HANDLE hProcess;
324static FILETIME ftKernelBegin;
325static FILETIME ftUserBegin;
326static sqlite3_int64 ftWallBegin;
327typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
328                                    LPFILETIME, LPFILETIME);
329static GETPROCTIMES getProcessTimesAddr = NULL;
330
331/*
332** Check to see if we have timer support.  Return 1 if necessary
333** support found (or found previously).
334*/
335static int hasTimer(void){
336  if( getProcessTimesAddr ){
337    return 1;
338  } else {
339#if !SQLITE_OS_WINRT
340    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
341    ** versions. See if the version we are running on has it, and if it
342    ** does, save off a pointer to it and the current process handle.
343    */
344    hProcess = GetCurrentProcess();
345    if( hProcess ){
346      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
347      if( NULL != hinstLib ){
348        getProcessTimesAddr =
349            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
350        if( NULL != getProcessTimesAddr ){
351          return 1;
352        }
353        FreeLibrary(hinstLib);
354      }
355    }
356#endif
357  }
358  return 0;
359}
360
361/*
362** Begin timing an operation
363*/
364static void beginTimer(void){
365  if( enableTimer && getProcessTimesAddr ){
366    FILETIME ftCreation, ftExit;
367    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
368                        &ftKernelBegin,&ftUserBegin);
369    ftWallBegin = timeOfDay();
370  }
371}
372
373/* Return the difference of two FILETIME structs in seconds */
374static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
375  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
376  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
377  return (double) ((i64End - i64Start) / 10000000.0);
378}
379
380/*
381** Print the timing results.
382*/
383static void endTimer(void){
384  if( enableTimer && getProcessTimesAddr){
385    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
386    sqlite3_int64 ftWallEnd = timeOfDay();
387    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
388    printf("Run Time: real %.3f user %f sys %f\n",
389       (ftWallEnd - ftWallBegin)*0.001,
390       timeDiff(&ftUserBegin, &ftUserEnd),
391       timeDiff(&ftKernelBegin, &ftKernelEnd));
392  }
393}
394
395#define BEGIN_TIMER beginTimer()
396#define END_TIMER endTimer()
397#define HAS_TIMER hasTimer()
398
399#else
400#define BEGIN_TIMER
401#define END_TIMER
402#define HAS_TIMER 0
403#endif
404
405/*
406** Used to prevent warnings about unused parameters
407*/
408#define UNUSED_PARAMETER(x) (void)(x)
409
410/*
411** Number of elements in an array
412*/
413#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
414
415/*
416** If the following flag is set, then command execution stops
417** at an error if we are not interactive.
418*/
419static int bail_on_error = 0;
420
421/*
422** Threat stdin as an interactive input if the following variable
423** is true.  Otherwise, assume stdin is connected to a file or pipe.
424*/
425static int stdin_is_interactive = 1;
426
427/*
428** On Windows systems we have to know if standard output is a console
429** in order to translate UTF-8 into MBCS.  The following variable is
430** true if translation is required.
431*/
432static int stdout_is_console = 1;
433
434/*
435** The following is the open SQLite database.  We make a pointer
436** to this database a static variable so that it can be accessed
437** by the SIGINT handler to interrupt database processing.
438*/
439static sqlite3 *globalDb = 0;
440
441/*
442** True if an interrupt (Control-C) has been received.
443*/
444static volatile int seenInterrupt = 0;
445
446/*
447** This is the name of our program. It is set in main(), used
448** in a number of other places, mostly for error messages.
449*/
450static char *Argv0;
451
452/*
453** Prompt strings. Initialized in main. Settable with
454**   .prompt main continue
455*/
456static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
457static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
458
459/*
460** Render output like fprintf().  Except, if the output is going to the
461** console and if this is running on a Windows machine, translate the
462** output from UTF-8 into MBCS.
463*/
464#if defined(_WIN32) || defined(WIN32)
465void utf8_printf(FILE *out, const char *zFormat, ...){
466  va_list ap;
467  va_start(ap, zFormat);
468  if( stdout_is_console && (out==stdout || out==stderr) ){
469    char *z1 = sqlite3_vmprintf(zFormat, ap);
470    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
471    sqlite3_free(z1);
472    fputs(z2, out);
473    sqlite3_free(z2);
474  }else{
475    vfprintf(out, zFormat, ap);
476  }
477  va_end(ap);
478}
479#elif !defined(utf8_printf)
480# define utf8_printf fprintf
481#endif
482
483/*
484** Render output like fprintf().  This should not be used on anything that
485** includes string formatting (e.g. "%s").
486*/
487#if !defined(raw_printf)
488# define raw_printf fprintf
489#endif
490
491/* Indicate out-of-memory and exit. */
492static void shell_out_of_memory(void){
493  raw_printf(stderr,"Error: out of memory\n");
494  exit(1);
495}
496
497/* Check a pointer to see if it is NULL.  If it is NULL, exit with an
498** out-of-memory error.
499*/
500static void shell_check_oom(void *p){
501  if( p==0 ) shell_out_of_memory();
502}
503
504/*
505** Write I/O traces to the following stream.
506*/
507#ifdef SQLITE_ENABLE_IOTRACE
508static FILE *iotrace = 0;
509#endif
510
511/*
512** This routine works like printf in that its first argument is a
513** format string and subsequent arguments are values to be substituted
514** in place of % fields.  The result of formatting this string
515** is written to iotrace.
516*/
517#ifdef SQLITE_ENABLE_IOTRACE
518static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
519  va_list ap;
520  char *z;
521  if( iotrace==0 ) return;
522  va_start(ap, zFormat);
523  z = sqlite3_vmprintf(zFormat, ap);
524  va_end(ap);
525  utf8_printf(iotrace, "%s", z);
526  sqlite3_free(z);
527}
528#endif
529
530/*
531** Output string zUtf to stream pOut as w characters.  If w is negative,
532** then right-justify the text.  W is the width in UTF-8 characters, not
533** in bytes.  This is different from the %*.*s specification in printf
534** since with %*.*s the width is measured in bytes, not characters.
535*/
536static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
537  int i;
538  int n;
539  int aw = w<0 ? -w : w;
540  for(i=n=0; zUtf[i]; i++){
541    if( (zUtf[i]&0xc0)!=0x80 ){
542      n++;
543      if( n==aw ){
544        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
545        break;
546      }
547    }
548  }
549  if( n>=aw ){
550    utf8_printf(pOut, "%.*s", i, zUtf);
551  }else if( w<0 ){
552    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
553  }else{
554    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
555  }
556}
557
558
559/*
560** Determines if a string is a number of not.
561*/
562static int isNumber(const char *z, int *realnum){
563  if( *z=='-' || *z=='+' ) z++;
564  if( !IsDigit(*z) ){
565    return 0;
566  }
567  z++;
568  if( realnum ) *realnum = 0;
569  while( IsDigit(*z) ){ z++; }
570  if( *z=='.' ){
571    z++;
572    if( !IsDigit(*z) ) return 0;
573    while( IsDigit(*z) ){ z++; }
574    if( realnum ) *realnum = 1;
575  }
576  if( *z=='e' || *z=='E' ){
577    z++;
578    if( *z=='+' || *z=='-' ) z++;
579    if( !IsDigit(*z) ) return 0;
580    while( IsDigit(*z) ){ z++; }
581    if( realnum ) *realnum = 1;
582  }
583  return *z==0;
584}
585
586/*
587** Compute a string length that is limited to what can be stored in
588** lower 30 bits of a 32-bit signed integer.
589*/
590static int strlen30(const char *z){
591  const char *z2 = z;
592  while( *z2 ){ z2++; }
593  return 0x3fffffff & (int)(z2 - z);
594}
595
596/*
597** Return the length of a string in characters.  Multibyte UTF8 characters
598** count as a single character.
599*/
600static int strlenChar(const char *z){
601  int n = 0;
602  while( *z ){
603    if( (0xc0&*(z++))!=0x80 ) n++;
604  }
605  return n;
606}
607
608/*
609** Return open FILE * if zFile exists, can be opened for read
610** and is an ordinary file or a character stream source.
611** Otherwise return 0.
612*/
613static FILE * openChrSource(const char *zFile){
614#ifdef _WIN32
615  struct _stat x = {0};
616# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
617  /* On Windows, open first, then check the stream nature. This order
618  ** is necessary because _stat() and sibs, when checking a named pipe,
619  ** effectively break the pipe as its supplier sees it. */
620  FILE *rv = fopen(zFile, "rb");
621  if( rv==0 ) return 0;
622  if( _fstat(_fileno(rv), &x) != 0
623      || !STAT_CHR_SRC(x.st_mode)){
624    fclose(rv);
625    rv = 0;
626  }
627  return rv;
628#else
629  struct stat x = {0};
630  int rc = stat(zFile, &x);
631# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
632  if( rc!=0 ) return 0;
633  if( STAT_CHR_SRC(x.st_mode) ){
634    return fopen(zFile, "rb");
635  }else{
636    return 0;
637  }
638#endif
639#undef STAT_CHR_SRC
640}
641
642/*
643** This routine reads a line of text from FILE in, stores
644** the text in memory obtained from malloc() and returns a pointer
645** to the text.  NULL is returned at end of file, or if malloc()
646** fails.
647**
648** If zLine is not NULL then it is a malloced buffer returned from
649** a previous call to this routine that may be reused.
650*/
651static char *local_getline(char *zLine, FILE *in){
652  int nLine = zLine==0 ? 0 : 100;
653  int n = 0;
654
655  while( 1 ){
656    if( n+100>nLine ){
657      nLine = nLine*2 + 100;
658      zLine = realloc(zLine, nLine);
659      shell_check_oom(zLine);
660    }
661    if( fgets(&zLine[n], nLine - n, in)==0 ){
662      if( n==0 ){
663        free(zLine);
664        return 0;
665      }
666      zLine[n] = 0;
667      break;
668    }
669    while( zLine[n] ) n++;
670    if( n>0 && zLine[n-1]=='\n' ){
671      n--;
672      if( n>0 && zLine[n-1]=='\r' ) n--;
673      zLine[n] = 0;
674      break;
675    }
676  }
677#if defined(_WIN32) || defined(WIN32)
678  /* For interactive input on Windows systems, translate the
679  ** multi-byte characterset characters into UTF-8. */
680  if( stdin_is_interactive && in==stdin ){
681    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
682    if( zTrans ){
683      int nTrans = strlen30(zTrans)+1;
684      if( nTrans>nLine ){
685        zLine = realloc(zLine, nTrans);
686        shell_check_oom(zLine);
687      }
688      memcpy(zLine, zTrans, nTrans);
689      sqlite3_free(zTrans);
690    }
691  }
692#endif /* defined(_WIN32) || defined(WIN32) */
693  return zLine;
694}
695
696/*
697** Retrieve a single line of input text.
698**
699** If in==0 then read from standard input and prompt before each line.
700** If isContinuation is true, then a continuation prompt is appropriate.
701** If isContinuation is zero, then the main prompt should be used.
702**
703** If zPrior is not NULL then it is a buffer from a prior call to this
704** routine that can be reused.
705**
706** The result is stored in space obtained from malloc() and must either
707** be freed by the caller or else passed back into this routine via the
708** zPrior argument for reuse.
709*/
710#ifndef SQLITE_SHELL_FIDDLE
711static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
712  char *zPrompt;
713  char *zResult;
714  if( in!=0 ){
715    zResult = local_getline(zPrior, in);
716  }else{
717    zPrompt = isContinuation ? continuePrompt : mainPrompt;
718#if SHELL_USE_LOCAL_GETLINE
719    printf("%s", zPrompt);
720    fflush(stdout);
721    zResult = local_getline(zPrior, stdin);
722#else
723    free(zPrior);
724    zResult = shell_readline(zPrompt);
725    if( zResult && *zResult ) shell_add_history(zResult);
726#endif
727  }
728  return zResult;
729}
730#endif /* !SQLITE_SHELL_FIDDLE */
731
732/*
733** Return the value of a hexadecimal digit.  Return -1 if the input
734** is not a hex digit.
735*/
736static int hexDigitValue(char c){
737  if( c>='0' && c<='9' ) return c - '0';
738  if( c>='a' && c<='f' ) return c - 'a' + 10;
739  if( c>='A' && c<='F' ) return c - 'A' + 10;
740  return -1;
741}
742
743/*
744** Interpret zArg as an integer value, possibly with suffixes.
745*/
746static sqlite3_int64 integerValue(const char *zArg){
747  sqlite3_int64 v = 0;
748  static const struct { char *zSuffix; int iMult; } aMult[] = {
749    { "KiB", 1024 },
750    { "MiB", 1024*1024 },
751    { "GiB", 1024*1024*1024 },
752    { "KB",  1000 },
753    { "MB",  1000000 },
754    { "GB",  1000000000 },
755    { "K",   1000 },
756    { "M",   1000000 },
757    { "G",   1000000000 },
758  };
759  int i;
760  int isNeg = 0;
761  if( zArg[0]=='-' ){
762    isNeg = 1;
763    zArg++;
764  }else if( zArg[0]=='+' ){
765    zArg++;
766  }
767  if( zArg[0]=='0' && zArg[1]=='x' ){
768    int x;
769    zArg += 2;
770    while( (x = hexDigitValue(zArg[0]))>=0 ){
771      v = (v<<4) + x;
772      zArg++;
773    }
774  }else{
775    while( IsDigit(zArg[0]) ){
776      v = v*10 + zArg[0] - '0';
777      zArg++;
778    }
779  }
780  for(i=0; i<ArraySize(aMult); i++){
781    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
782      v *= aMult[i].iMult;
783      break;
784    }
785  }
786  return isNeg? -v : v;
787}
788
789/*
790** A variable length string to which one can append text.
791*/
792typedef struct ShellText ShellText;
793struct ShellText {
794  char *z;
795  int n;
796  int nAlloc;
797};
798
799/*
800** Initialize and destroy a ShellText object
801*/
802static void initText(ShellText *p){
803  memset(p, 0, sizeof(*p));
804}
805static void freeText(ShellText *p){
806  free(p->z);
807  initText(p);
808}
809
810/* zIn is either a pointer to a NULL-terminated string in memory obtained
811** from malloc(), or a NULL pointer. The string pointed to by zAppend is
812** added to zIn, and the result returned in memory obtained from malloc().
813** zIn, if it was not NULL, is freed.
814**
815** If the third argument, quote, is not '\0', then it is used as a
816** quote character for zAppend.
817*/
818static void appendText(ShellText *p, const char *zAppend, char quote){
819  int len;
820  int i;
821  int nAppend = strlen30(zAppend);
822
823  len = nAppend+p->n+1;
824  if( quote ){
825    len += 2;
826    for(i=0; i<nAppend; i++){
827      if( zAppend[i]==quote ) len++;
828    }
829  }
830
831  if( p->z==0 || p->n+len>=p->nAlloc ){
832    p->nAlloc = p->nAlloc*2 + len + 20;
833    p->z = realloc(p->z, p->nAlloc);
834    shell_check_oom(p->z);
835  }
836
837  if( quote ){
838    char *zCsr = p->z+p->n;
839    *zCsr++ = quote;
840    for(i=0; i<nAppend; i++){
841      *zCsr++ = zAppend[i];
842      if( zAppend[i]==quote ) *zCsr++ = quote;
843    }
844    *zCsr++ = quote;
845    p->n = (int)(zCsr - p->z);
846    *zCsr = '\0';
847  }else{
848    memcpy(p->z+p->n, zAppend, nAppend);
849    p->n += nAppend;
850    p->z[p->n] = '\0';
851  }
852}
853
854/*
855** Attempt to determine if identifier zName needs to be quoted, either
856** because it contains non-alphanumeric characters, or because it is an
857** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
858** that quoting is required.
859**
860** Return '"' if quoting is required.  Return 0 if no quoting is required.
861*/
862static char quoteChar(const char *zName){
863  int i;
864  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
865  for(i=0; zName[i]; i++){
866    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
867  }
868  return sqlite3_keyword_check(zName, i) ? '"' : 0;
869}
870
871/*
872** Construct a fake object name and column list to describe the structure
873** of the view, virtual table, or table valued function zSchema.zName.
874*/
875static char *shellFakeSchema(
876  sqlite3 *db,            /* The database connection containing the vtab */
877  const char *zSchema,    /* Schema of the database holding the vtab */
878  const char *zName       /* The name of the virtual table */
879){
880  sqlite3_stmt *pStmt = 0;
881  char *zSql;
882  ShellText s;
883  char cQuote;
884  char *zDiv = "(";
885  int nRow = 0;
886
887  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
888                         zSchema ? zSchema : "main", zName);
889  shell_check_oom(zSql);
890  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
891  sqlite3_free(zSql);
892  initText(&s);
893  if( zSchema ){
894    cQuote = quoteChar(zSchema);
895    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
896    appendText(&s, zSchema, cQuote);
897    appendText(&s, ".", 0);
898  }
899  cQuote = quoteChar(zName);
900  appendText(&s, zName, cQuote);
901  while( sqlite3_step(pStmt)==SQLITE_ROW ){
902    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
903    nRow++;
904    appendText(&s, zDiv, 0);
905    zDiv = ",";
906    if( zCol==0 ) zCol = "";
907    cQuote = quoteChar(zCol);
908    appendText(&s, zCol, cQuote);
909  }
910  appendText(&s, ")", 0);
911  sqlite3_finalize(pStmt);
912  if( nRow==0 ){
913    freeText(&s);
914    s.z = 0;
915  }
916  return s.z;
917}
918
919/*
920** SQL function:  shell_module_schema(X)
921**
922** Return a fake schema for the table-valued function or eponymous virtual
923** table X.
924*/
925static void shellModuleSchema(
926  sqlite3_context *pCtx,
927  int nVal,
928  sqlite3_value **apVal
929){
930  const char *zName;
931  char *zFake;
932  UNUSED_PARAMETER(nVal);
933  zName = (const char*)sqlite3_value_text(apVal[0]);
934  zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
935  if( zFake ){
936    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
937                        -1, sqlite3_free);
938    free(zFake);
939  }
940}
941
942/*
943** SQL function:  shell_add_schema(S,X)
944**
945** Add the schema name X to the CREATE statement in S and return the result.
946** Examples:
947**
948**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
949**
950** Also works on
951**
952**    CREATE INDEX
953**    CREATE UNIQUE INDEX
954**    CREATE VIEW
955**    CREATE TRIGGER
956**    CREATE VIRTUAL TABLE
957**
958** This UDF is used by the .schema command to insert the schema name of
959** attached databases into the middle of the sqlite_schema.sql field.
960*/
961static void shellAddSchemaName(
962  sqlite3_context *pCtx,
963  int nVal,
964  sqlite3_value **apVal
965){
966  static const char *aPrefix[] = {
967     "TABLE",
968     "INDEX",
969     "UNIQUE INDEX",
970     "VIEW",
971     "TRIGGER",
972     "VIRTUAL TABLE"
973  };
974  int i = 0;
975  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
976  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
977  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
978  sqlite3 *db = sqlite3_context_db_handle(pCtx);
979  UNUSED_PARAMETER(nVal);
980  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
981    for(i=0; i<ArraySize(aPrefix); i++){
982      int n = strlen30(aPrefix[i]);
983      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
984        char *z = 0;
985        char *zFake = 0;
986        if( zSchema ){
987          char cQuote = quoteChar(zSchema);
988          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
989            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
990          }else{
991            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
992          }
993        }
994        if( zName
995         && aPrefix[i][0]=='V'
996         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
997        ){
998          if( z==0 ){
999            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1000          }else{
1001            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1002          }
1003          free(zFake);
1004        }
1005        if( z ){
1006          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1007          return;
1008        }
1009      }
1010    }
1011  }
1012  sqlite3_result_value(pCtx, apVal[0]);
1013}
1014
1015/*
1016** The source code for several run-time loadable extensions is inserted
1017** below by the ../tool/mkshellc.tcl script.  Before processing that included
1018** code, we need to override some macros to make the included program code
1019** work here in the middle of this regular program.
1020*/
1021#define SQLITE_EXTENSION_INIT1
1022#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1023
1024#if defined(_WIN32) && defined(_MSC_VER)
1025INCLUDE test_windirent.h
1026INCLUDE test_windirent.c
1027#define dirent DIRENT
1028#endif
1029INCLUDE ../ext/misc/memtrace.c
1030INCLUDE ../ext/misc/shathree.c
1031INCLUDE ../ext/misc/uint.c
1032INCLUDE ../ext/misc/decimal.c
1033INCLUDE ../ext/misc/ieee754.c
1034INCLUDE ../ext/misc/series.c
1035INCLUDE ../ext/misc/regexp.c
1036#ifndef SQLITE_SHELL_FIDDLE
1037INCLUDE ../ext/misc/fileio.c
1038INCLUDE ../ext/misc/completion.c
1039INCLUDE ../ext/misc/appendvfs.c
1040#endif
1041#ifdef SQLITE_HAVE_ZLIB
1042INCLUDE ../ext/misc/zipfile.c
1043INCLUDE ../ext/misc/sqlar.c
1044#endif
1045INCLUDE ../ext/expert/sqlite3expert.h
1046INCLUDE ../ext/expert/sqlite3expert.c
1047
1048#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1049INCLUDE ../ext/misc/dbdata.c
1050#endif
1051
1052#if defined(SQLITE_ENABLE_SESSION)
1053/*
1054** State information for a single open session
1055*/
1056typedef struct OpenSession OpenSession;
1057struct OpenSession {
1058  char *zName;             /* Symbolic name for this session */
1059  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1060  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1061  sqlite3_session *p;      /* The open session */
1062};
1063#endif
1064
1065typedef struct ExpertInfo ExpertInfo;
1066struct ExpertInfo {
1067  sqlite3expert *pExpert;
1068  int bVerbose;
1069};
1070
1071/* A single line in the EQP output */
1072typedef struct EQPGraphRow EQPGraphRow;
1073struct EQPGraphRow {
1074  int iEqpId;           /* ID for this row */
1075  int iParentId;        /* ID of the parent row */
1076  EQPGraphRow *pNext;   /* Next row in sequence */
1077  char zText[1];        /* Text to display for this row */
1078};
1079
1080/* All EQP output is collected into an instance of the following */
1081typedef struct EQPGraph EQPGraph;
1082struct EQPGraph {
1083  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1084  EQPGraphRow *pLast;   /* Last element of the pRow list */
1085  char zPrefix[100];    /* Graph prefix */
1086};
1087
1088/* Parameters affecting columnar mode result display (defaulting together) */
1089typedef struct ColModeOpts {
1090  int iWrap;            /* In columnar modes, wrap lines reaching this limit */
1091  u8 bQuote;            /* Quote results for .mode box and table */
1092  u8 bWordWrap;         /* In columnar modes, wrap at word boundaries  */
1093} ColModeOpts;
1094#define ColModeOpts_default { 60, 0, 0 }
1095#define ColModeOpts_default_qbox { 60, 1, 0 }
1096
1097/*
1098** State information about the database connection is contained in an
1099** instance of the following structure.
1100*/
1101typedef struct ShellState ShellState;
1102struct ShellState {
1103  sqlite3 *db;           /* The database */
1104  u8 autoExplain;        /* Automatically turn on .explain mode */
1105  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1106  u8 autoEQPtest;        /* autoEQP is in test mode */
1107  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1108  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1109  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1110  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1111  u8 nEqpLevel;          /* Depth of the EQP output graph */
1112  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1113  u8 bSafeMode;          /* True to prohibit unsafe operations */
1114  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
1115  ColModeOpts cmOpts;    /* Option values affecting columnar mode output */
1116  unsigned statsOn;      /* True to display memory stats before each finalize */
1117  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1118  int inputNesting;      /* Track nesting level of .read and other redirects */
1119  int outCount;          /* Revert to stdout when reaching zero */
1120  int cnt;               /* Number of records displayed so far */
1121  int lineno;            /* Line number of last line read from in */
1122  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1123  FILE *in;              /* Read commands from this stream */
1124  FILE *out;             /* Write results here */
1125  FILE *traceOut;        /* Output for sqlite3_trace() */
1126  int nErr;              /* Number of errors seen */
1127  int mode;              /* An output mode setting */
1128  int modePrior;         /* Saved mode */
1129  int cMode;             /* temporary output mode for the current query */
1130  int normalMode;        /* Output mode before ".explain on" */
1131  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1132  int showHeader;        /* True to show column names in List or Column mode */
1133  int nCheck;            /* Number of ".check" commands run */
1134  unsigned nProgress;    /* Number of progress callbacks encountered */
1135  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1136  unsigned flgProgress;  /* Flags for the progress callback */
1137  unsigned shellFlgs;    /* Various flags */
1138  unsigned priorShFlgs;  /* Saved copy of flags */
1139  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1140  char *zDestTable;      /* Name of destination table when MODE_Insert */
1141  char *zTempFile;       /* Temporary file that might need deleting */
1142  char zTestcase[30];    /* Name of current test case */
1143  char colSeparator[20]; /* Column separator character for several modes */
1144  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1145  char colSepPrior[20];  /* Saved column separator */
1146  char rowSepPrior[20];  /* Saved row separator */
1147  int *colWidth;         /* Requested width of each column in columnar modes */
1148  int *actualWidth;      /* Actual width of each column */
1149  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1150  char nullValue[20];    /* The text to print when a NULL comes back from
1151                         ** the database */
1152  char outfile[FILENAME_MAX]; /* Filename for *out */
1153  sqlite3_stmt *pStmt;   /* Current statement if any. */
1154  FILE *pLog;            /* Write log output here */
1155  struct AuxDb {         /* Storage space for auxiliary database connections */
1156    sqlite3 *db;               /* Connection pointer */
1157    const char *zDbFilename;   /* Filename used to open the connection */
1158    char *zFreeOnClose;        /* Free this memory allocation on close */
1159#if defined(SQLITE_ENABLE_SESSION)
1160    int nSession;              /* Number of active sessions */
1161    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
1162#endif
1163  } aAuxDb[5],           /* Array of all database connections */
1164    *pAuxDb;             /* Currently active database connection */
1165  int *aiIndent;         /* Array of indents used in MODE_Explain */
1166  int nIndent;           /* Size of array aiIndent[] */
1167  int iIndent;           /* Index of current op in aiIndent[] */
1168  char *zNonce;          /* Nonce for temporary safe-mode excapes */
1169  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1170  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
1171#ifdef SQLITE_SHELL_FIDDLE
1172  struct {
1173    const char * zInput; /* Input string from wasm/JS proxy */
1174    const char * zPos;   /* Cursor pos into zInput */
1175  } wasm;
1176#endif
1177};
1178
1179#ifdef SQLITE_SHELL_FIDDLE
1180static ShellState shellState;
1181#endif
1182
1183
1184/* Allowed values for ShellState.autoEQP
1185*/
1186#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1187#define AUTOEQP_on       1           /* Automatic EQP is on */
1188#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1189#define AUTOEQP_full     3           /* Show full EXPLAIN */
1190
1191/* Allowed values for ShellState.openMode
1192*/
1193#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1194#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1195#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1196#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1197#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1198#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1199#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1200
1201/* Allowed values for ShellState.eTraceType
1202*/
1203#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1204#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1205#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1206
1207/* Bits in the ShellState.flgProgress variable */
1208#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1209#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1210                                   ** callback limit is reached, and for each
1211                                   ** top-level SQL statement */
1212#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1213
1214/*
1215** These are the allowed shellFlgs values
1216*/
1217#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1218#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1219#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1220#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1221#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1222#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1223#define SHFLG_Echo           0x00000040 /* .echo on/off, or --echo setting */
1224#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1225#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1226#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1227
1228/*
1229** Macros for testing and setting shellFlgs
1230*/
1231#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1232#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1233#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1234
1235/*
1236** These are the allowed modes.
1237*/
1238#define MODE_Line     0  /* One column per line.  Blank line between records */
1239#define MODE_Column   1  /* One record per line in neat columns */
1240#define MODE_List     2  /* One record per line with a separator */
1241#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1242#define MODE_Html     4  /* Generate an XHTML table */
1243#define MODE_Insert   5  /* Generate SQL "insert" statements */
1244#define MODE_Quote    6  /* Quote values as for SQL */
1245#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1246#define MODE_Csv      8  /* Quote strings, numbers are plain */
1247#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1248#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1249#define MODE_Pretty  11  /* Pretty-print schemas */
1250#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1251#define MODE_Json    13  /* Output JSON */
1252#define MODE_Markdown 14 /* Markdown formatting */
1253#define MODE_Table   15  /* MySQL-style table formatting */
1254#define MODE_Box     16  /* Unicode box-drawing characters */
1255#define MODE_Count   17  /* Output only a count of the rows of output */
1256#define MODE_Off     18  /* No query output shown */
1257
1258static const char *modeDescr[] = {
1259  "line",
1260  "column",
1261  "list",
1262  "semi",
1263  "html",
1264  "insert",
1265  "quote",
1266  "tcl",
1267  "csv",
1268  "explain",
1269  "ascii",
1270  "prettyprint",
1271  "eqp",
1272  "json",
1273  "markdown",
1274  "table",
1275  "box",
1276  "count",
1277  "off"
1278};
1279
1280/*
1281** These are the column/row/line separators used by the various
1282** import/export modes.
1283*/
1284#define SEP_Column    "|"
1285#define SEP_Row       "\n"
1286#define SEP_Tab       "\t"
1287#define SEP_Space     " "
1288#define SEP_Comma     ","
1289#define SEP_CrLf      "\r\n"
1290#define SEP_Unit      "\x1F"
1291#define SEP_Record    "\x1E"
1292
1293/*
1294** Limit input nesting via .read or any other input redirect.
1295** It's not too expensive, so a generous allowance can be made.
1296*/
1297#define MAX_INPUT_NESTING 25
1298
1299/*
1300** A callback for the sqlite3_log() interface.
1301*/
1302static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1303  ShellState *p = (ShellState*)pArg;
1304  if( p->pLog==0 ) return;
1305  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1306  fflush(p->pLog);
1307}
1308
1309/*
1310** SQL function:  shell_putsnl(X)
1311**
1312** Write the text X to the screen (or whatever output is being directed)
1313** adding a newline at the end, and then return X.
1314*/
1315static void shellPutsFunc(
1316  sqlite3_context *pCtx,
1317  int nVal,
1318  sqlite3_value **apVal
1319){
1320  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1321  (void)nVal;
1322  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1323  sqlite3_result_value(pCtx, apVal[0]);
1324}
1325
1326/*
1327** If in safe mode, print an error message described by the arguments
1328** and exit immediately.
1329*/
1330static void failIfSafeMode(
1331  ShellState *p,
1332  const char *zErrMsg,
1333  ...
1334){
1335  if( p->bSafeMode ){
1336    va_list ap;
1337    char *zMsg;
1338    va_start(ap, zErrMsg);
1339    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1340    va_end(ap);
1341    raw_printf(stderr, "line %d: ", p->lineno);
1342    utf8_printf(stderr, "%s\n", zMsg);
1343    exit(1);
1344  }
1345}
1346
1347/*
1348** SQL function:   edit(VALUE)
1349**                 edit(VALUE,EDITOR)
1350**
1351** These steps:
1352**
1353**     (1) Write VALUE into a temporary file.
1354**     (2) Run program EDITOR on that temporary file.
1355**     (3) Read the temporary file back and return its content as the result.
1356**     (4) Delete the temporary file
1357**
1358** If the EDITOR argument is omitted, use the value in the VISUAL
1359** environment variable.  If still there is no EDITOR, through an error.
1360**
1361** Also throw an error if the EDITOR program returns a non-zero exit code.
1362*/
1363#ifndef SQLITE_NOHAVE_SYSTEM
1364static void editFunc(
1365  sqlite3_context *context,
1366  int argc,
1367  sqlite3_value **argv
1368){
1369  const char *zEditor;
1370  char *zTempFile = 0;
1371  sqlite3 *db;
1372  char *zCmd = 0;
1373  int bBin;
1374  int rc;
1375  int hasCRNL = 0;
1376  FILE *f = 0;
1377  sqlite3_int64 sz;
1378  sqlite3_int64 x;
1379  unsigned char *p = 0;
1380
1381  if( argc==2 ){
1382    zEditor = (const char*)sqlite3_value_text(argv[1]);
1383  }else{
1384    zEditor = getenv("VISUAL");
1385  }
1386  if( zEditor==0 ){
1387    sqlite3_result_error(context, "no editor for edit()", -1);
1388    return;
1389  }
1390  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1391    sqlite3_result_error(context, "NULL input to edit()", -1);
1392    return;
1393  }
1394  db = sqlite3_context_db_handle(context);
1395  zTempFile = 0;
1396  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1397  if( zTempFile==0 ){
1398    sqlite3_uint64 r = 0;
1399    sqlite3_randomness(sizeof(r), &r);
1400    zTempFile = sqlite3_mprintf("temp%llx", r);
1401    if( zTempFile==0 ){
1402      sqlite3_result_error_nomem(context);
1403      return;
1404    }
1405  }
1406  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1407  /* When writing the file to be edited, do \n to \r\n conversions on systems
1408  ** that want \r\n line endings */
1409  f = fopen(zTempFile, bBin ? "wb" : "w");
1410  if( f==0 ){
1411    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1412    goto edit_func_end;
1413  }
1414  sz = sqlite3_value_bytes(argv[0]);
1415  if( bBin ){
1416    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1417  }else{
1418    const char *z = (const char*)sqlite3_value_text(argv[0]);
1419    /* Remember whether or not the value originally contained \r\n */
1420    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1421    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1422  }
1423  fclose(f);
1424  f = 0;
1425  if( x!=sz ){
1426    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1427    goto edit_func_end;
1428  }
1429  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1430  if( zCmd==0 ){
1431    sqlite3_result_error_nomem(context);
1432    goto edit_func_end;
1433  }
1434  rc = system(zCmd);
1435  sqlite3_free(zCmd);
1436  if( rc ){
1437    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1438    goto edit_func_end;
1439  }
1440  f = fopen(zTempFile, "rb");
1441  if( f==0 ){
1442    sqlite3_result_error(context,
1443      "edit() cannot reopen temp file after edit", -1);
1444    goto edit_func_end;
1445  }
1446  fseek(f, 0, SEEK_END);
1447  sz = ftell(f);
1448  rewind(f);
1449  p = sqlite3_malloc64( sz+1 );
1450  if( p==0 ){
1451    sqlite3_result_error_nomem(context);
1452    goto edit_func_end;
1453  }
1454  x = fread(p, 1, (size_t)sz, f);
1455  fclose(f);
1456  f = 0;
1457  if( x!=sz ){
1458    sqlite3_result_error(context, "could not read back the whole file", -1);
1459    goto edit_func_end;
1460  }
1461  if( bBin ){
1462    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1463  }else{
1464    sqlite3_int64 i, j;
1465    if( hasCRNL ){
1466      /* If the original contains \r\n then do no conversions back to \n */
1467    }else{
1468      /* If the file did not originally contain \r\n then convert any new
1469      ** \r\n back into \n */
1470      for(i=j=0; i<sz; i++){
1471        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1472        p[j++] = p[i];
1473      }
1474      sz = j;
1475      p[sz] = 0;
1476    }
1477    sqlite3_result_text64(context, (const char*)p, sz,
1478                          sqlite3_free, SQLITE_UTF8);
1479  }
1480  p = 0;
1481
1482edit_func_end:
1483  if( f ) fclose(f);
1484  unlink(zTempFile);
1485  sqlite3_free(zTempFile);
1486  sqlite3_free(p);
1487}
1488#endif /* SQLITE_NOHAVE_SYSTEM */
1489
1490/*
1491** Save or restore the current output mode
1492*/
1493static void outputModePush(ShellState *p){
1494  p->modePrior = p->mode;
1495  p->priorShFlgs = p->shellFlgs;
1496  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1497  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1498}
1499static void outputModePop(ShellState *p){
1500  p->mode = p->modePrior;
1501  p->shellFlgs = p->priorShFlgs;
1502  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1503  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1504}
1505
1506/*
1507** Output the given string as a hex-encoded blob (eg. X'1234' )
1508*/
1509static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1510  int i;
1511  unsigned char *aBlob = (unsigned char*)pBlob;
1512
1513  char *zStr = sqlite3_malloc(nBlob*2 + 1);
1514  shell_check_oom(zStr);
1515
1516  for(i=0; i<nBlob; i++){
1517    static const char aHex[] = {
1518        '0', '1', '2', '3', '4', '5', '6', '7',
1519        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1520    };
1521    zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1522    zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1523  }
1524  zStr[i*2] = '\0';
1525
1526  raw_printf(out,"X'%s'", zStr);
1527  sqlite3_free(zStr);
1528}
1529
1530/*
1531** Find a string that is not found anywhere in z[].  Return a pointer
1532** to that string.
1533**
1534** Try to use zA and zB first.  If both of those are already found in z[]
1535** then make up some string and store it in the buffer zBuf.
1536*/
1537static const char *unused_string(
1538  const char *z,                    /* Result must not appear anywhere in z */
1539  const char *zA, const char *zB,   /* Try these first */
1540  char *zBuf                        /* Space to store a generated string */
1541){
1542  unsigned i = 0;
1543  if( strstr(z, zA)==0 ) return zA;
1544  if( strstr(z, zB)==0 ) return zB;
1545  do{
1546    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1547  }while( strstr(z,zBuf)!=0 );
1548  return zBuf;
1549}
1550
1551/*
1552** Output the given string as a quoted string using SQL quoting conventions.
1553**
1554** See also: output_quoted_escaped_string()
1555*/
1556static void output_quoted_string(FILE *out, const char *z){
1557  int i;
1558  char c;
1559  setBinaryMode(out, 1);
1560  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1561  if( c==0 ){
1562    utf8_printf(out,"'%s'",z);
1563  }else{
1564    raw_printf(out, "'");
1565    while( *z ){
1566      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1567      if( c=='\'' ) i++;
1568      if( i ){
1569        utf8_printf(out, "%.*s", i, z);
1570        z += i;
1571      }
1572      if( c=='\'' ){
1573        raw_printf(out, "'");
1574        continue;
1575      }
1576      if( c==0 ){
1577        break;
1578      }
1579      z++;
1580    }
1581    raw_printf(out, "'");
1582  }
1583  setTextMode(out, 1);
1584}
1585
1586/*
1587** Output the given string as a quoted string using SQL quoting conventions.
1588** Additionallly , escape the "\n" and "\r" characters so that they do not
1589** get corrupted by end-of-line translation facilities in some operating
1590** systems.
1591**
1592** This is like output_quoted_string() but with the addition of the \r\n
1593** escape mechanism.
1594*/
1595static void output_quoted_escaped_string(FILE *out, const char *z){
1596  int i;
1597  char c;
1598  setBinaryMode(out, 1);
1599  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1600  if( c==0 ){
1601    utf8_printf(out,"'%s'",z);
1602  }else{
1603    const char *zNL = 0;
1604    const char *zCR = 0;
1605    int nNL = 0;
1606    int nCR = 0;
1607    char zBuf1[20], zBuf2[20];
1608    for(i=0; z[i]; i++){
1609      if( z[i]=='\n' ) nNL++;
1610      if( z[i]=='\r' ) nCR++;
1611    }
1612    if( nNL ){
1613      raw_printf(out, "replace(");
1614      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1615    }
1616    if( nCR ){
1617      raw_printf(out, "replace(");
1618      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1619    }
1620    raw_printf(out, "'");
1621    while( *z ){
1622      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1623      if( c=='\'' ) i++;
1624      if( i ){
1625        utf8_printf(out, "%.*s", i, z);
1626        z += i;
1627      }
1628      if( c=='\'' ){
1629        raw_printf(out, "'");
1630        continue;
1631      }
1632      if( c==0 ){
1633        break;
1634      }
1635      z++;
1636      if( c=='\n' ){
1637        raw_printf(out, "%s", zNL);
1638        continue;
1639      }
1640      raw_printf(out, "%s", zCR);
1641    }
1642    raw_printf(out, "'");
1643    if( nCR ){
1644      raw_printf(out, ",'%s',char(13))", zCR);
1645    }
1646    if( nNL ){
1647      raw_printf(out, ",'%s',char(10))", zNL);
1648    }
1649  }
1650  setTextMode(out, 1);
1651}
1652
1653/*
1654** Output the given string as a quoted according to C or TCL quoting rules.
1655*/
1656static void output_c_string(FILE *out, const char *z){
1657  unsigned int c;
1658  fputc('"', out);
1659  while( (c = *(z++))!=0 ){
1660    if( c=='\\' ){
1661      fputc(c, out);
1662      fputc(c, out);
1663    }else if( c=='"' ){
1664      fputc('\\', out);
1665      fputc('"', out);
1666    }else if( c=='\t' ){
1667      fputc('\\', out);
1668      fputc('t', out);
1669    }else if( c=='\n' ){
1670      fputc('\\', out);
1671      fputc('n', out);
1672    }else if( c=='\r' ){
1673      fputc('\\', out);
1674      fputc('r', out);
1675    }else if( !isprint(c&0xff) ){
1676      raw_printf(out, "\\%03o", c&0xff);
1677    }else{
1678      fputc(c, out);
1679    }
1680  }
1681  fputc('"', out);
1682}
1683
1684/*
1685** Output the given string as a quoted according to JSON quoting rules.
1686*/
1687static void output_json_string(FILE *out, const char *z, int n){
1688  unsigned int c;
1689  if( n<0 ) n = (int)strlen(z);
1690  fputc('"', out);
1691  while( n-- ){
1692    c = *(z++);
1693    if( c=='\\' || c=='"' ){
1694      fputc('\\', out);
1695      fputc(c, out);
1696    }else if( c<=0x1f ){
1697      fputc('\\', out);
1698      if( c=='\b' ){
1699        fputc('b', out);
1700      }else if( c=='\f' ){
1701        fputc('f', out);
1702      }else if( c=='\n' ){
1703        fputc('n', out);
1704      }else if( c=='\r' ){
1705        fputc('r', out);
1706      }else if( c=='\t' ){
1707        fputc('t', out);
1708      }else{
1709         raw_printf(out, "u%04x",c);
1710      }
1711    }else{
1712      fputc(c, out);
1713    }
1714  }
1715  fputc('"', out);
1716}
1717
1718/*
1719** Output the given string with characters that are special to
1720** HTML escaped.
1721*/
1722static void output_html_string(FILE *out, const char *z){
1723  int i;
1724  if( z==0 ) z = "";
1725  while( *z ){
1726    for(i=0;   z[i]
1727            && z[i]!='<'
1728            && z[i]!='&'
1729            && z[i]!='>'
1730            && z[i]!='\"'
1731            && z[i]!='\'';
1732        i++){}
1733    if( i>0 ){
1734      utf8_printf(out,"%.*s",i,z);
1735    }
1736    if( z[i]=='<' ){
1737      raw_printf(out,"&lt;");
1738    }else if( z[i]=='&' ){
1739      raw_printf(out,"&amp;");
1740    }else if( z[i]=='>' ){
1741      raw_printf(out,"&gt;");
1742    }else if( z[i]=='\"' ){
1743      raw_printf(out,"&quot;");
1744    }else if( z[i]=='\'' ){
1745      raw_printf(out,"&#39;");
1746    }else{
1747      break;
1748    }
1749    z += i + 1;
1750  }
1751}
1752
1753/*
1754** If a field contains any character identified by a 1 in the following
1755** array, then the string must be quoted for CSV.
1756*/
1757static const char needCsvQuote[] = {
1758  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1759  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1760  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1761  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1762  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1763  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1764  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1765  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1766  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1767  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1768  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1769  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1770  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1771  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1772  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1773  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1774};
1775
1776/*
1777** Output a single term of CSV.  Actually, p->colSeparator is used for
1778** the separator, which may or may not be a comma.  p->nullValue is
1779** the null value.  Strings are quoted if necessary.  The separator
1780** is only issued if bSep is true.
1781*/
1782static void output_csv(ShellState *p, const char *z, int bSep){
1783  FILE *out = p->out;
1784  if( z==0 ){
1785    utf8_printf(out,"%s",p->nullValue);
1786  }else{
1787    unsigned i;
1788    for(i=0; z[i]; i++){
1789      if( needCsvQuote[((unsigned char*)z)[i]] ){
1790        i = 0;
1791        break;
1792      }
1793    }
1794    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1795      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1796      shell_check_oom(zQuoted);
1797      utf8_printf(out, "%s", zQuoted);
1798      sqlite3_free(zQuoted);
1799    }else{
1800      utf8_printf(out, "%s", z);
1801    }
1802  }
1803  if( bSep ){
1804    utf8_printf(p->out, "%s", p->colSeparator);
1805  }
1806}
1807
1808/*
1809** This routine runs when the user presses Ctrl-C
1810*/
1811static void interrupt_handler(int NotUsed){
1812  UNUSED_PARAMETER(NotUsed);
1813  seenInterrupt++;
1814  if( seenInterrupt>2 ) exit(1);
1815  if( globalDb ) sqlite3_interrupt(globalDb);
1816}
1817
1818#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1819/*
1820** This routine runs for console events (e.g. Ctrl-C) on Win32
1821*/
1822static BOOL WINAPI ConsoleCtrlHandler(
1823  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1824){
1825  if( dwCtrlType==CTRL_C_EVENT ){
1826    interrupt_handler(0);
1827    return TRUE;
1828  }
1829  return FALSE;
1830}
1831#endif
1832
1833#ifndef SQLITE_OMIT_AUTHORIZATION
1834/*
1835** This authorizer runs in safe mode.
1836*/
1837static int safeModeAuth(
1838  void *pClientData,
1839  int op,
1840  const char *zA1,
1841  const char *zA2,
1842  const char *zA3,
1843  const char *zA4
1844){
1845  ShellState *p = (ShellState*)pClientData;
1846  static const char *azProhibitedFunctions[] = {
1847    "edit",
1848    "fts3_tokenizer",
1849    "load_extension",
1850    "readfile",
1851    "writefile",
1852    "zipfile",
1853    "zipfile_cds",
1854  };
1855  UNUSED_PARAMETER(zA2);
1856  UNUSED_PARAMETER(zA3);
1857  UNUSED_PARAMETER(zA4);
1858  switch( op ){
1859    case SQLITE_ATTACH: {
1860#ifndef SQLITE_SHELL_FIDDLE
1861      /* In WASM builds the filesystem is a virtual sandbox, so
1862      ** there's no harm in using ATTACH. */
1863      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1864#endif
1865      break;
1866    }
1867    case SQLITE_FUNCTION: {
1868      int i;
1869      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1870        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1871          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1872                         azProhibitedFunctions[i]);
1873        }
1874      }
1875      break;
1876    }
1877  }
1878  return SQLITE_OK;
1879}
1880
1881/*
1882** When the ".auth ON" is set, the following authorizer callback is
1883** invoked.  It always returns SQLITE_OK.
1884*/
1885static int shellAuth(
1886  void *pClientData,
1887  int op,
1888  const char *zA1,
1889  const char *zA2,
1890  const char *zA3,
1891  const char *zA4
1892){
1893  ShellState *p = (ShellState*)pClientData;
1894  static const char *azAction[] = { 0,
1895     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1896     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1897     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1898     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1899     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1900     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1901     "PRAGMA",               "READ",                 "SELECT",
1902     "TRANSACTION",          "UPDATE",               "ATTACH",
1903     "DETACH",               "ALTER_TABLE",          "REINDEX",
1904     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1905     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1906  };
1907  int i;
1908  const char *az[4];
1909  az[0] = zA1;
1910  az[1] = zA2;
1911  az[2] = zA3;
1912  az[3] = zA4;
1913  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1914  for(i=0; i<4; i++){
1915    raw_printf(p->out, " ");
1916    if( az[i] ){
1917      output_c_string(p->out, az[i]);
1918    }else{
1919      raw_printf(p->out, "NULL");
1920    }
1921  }
1922  raw_printf(p->out, "\n");
1923  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1924  return SQLITE_OK;
1925}
1926#endif
1927
1928/*
1929** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1930**
1931** This routine converts some CREATE TABLE statements for shadow tables
1932** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1933**
1934** If the schema statement in z[] contains a start-of-comment and if
1935** sqlite3_complete() returns false, try to terminate the comment before
1936** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
1937*/
1938static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1939  char *zToFree = 0;
1940  if( z==0 ) return;
1941  if( zTail==0 ) return;
1942  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
1943    const char *zOrig = z;
1944    static const char *azTerm[] = { "", "*/", "\n" };
1945    int i;
1946    for(i=0; i<ArraySize(azTerm); i++){
1947      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
1948      if( sqlite3_complete(zNew) ){
1949        size_t n = strlen(zNew);
1950        zNew[n-1] = 0;
1951        zToFree = zNew;
1952        z = zNew;
1953        break;
1954      }
1955      sqlite3_free(zNew);
1956    }
1957  }
1958  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1959    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1960  }else{
1961    utf8_printf(out, "%s%s", z, zTail);
1962  }
1963  sqlite3_free(zToFree);
1964}
1965static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1966  char c = z[n];
1967  z[n] = 0;
1968  printSchemaLine(out, z, zTail);
1969  z[n] = c;
1970}
1971
1972/*
1973** Return true if string z[] has nothing but whitespace and comments to the
1974** end of the first line.
1975*/
1976static int wsToEol(const char *z){
1977  int i;
1978  for(i=0; z[i]; i++){
1979    if( z[i]=='\n' ) return 1;
1980    if( IsSpace(z[i]) ) continue;
1981    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1982    return 0;
1983  }
1984  return 1;
1985}
1986
1987/*
1988** Add a new entry to the EXPLAIN QUERY PLAN data
1989*/
1990static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1991  EQPGraphRow *pNew;
1992  int nText = strlen30(zText);
1993  if( p->autoEQPtest ){
1994    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1995  }
1996  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1997  shell_check_oom(pNew);
1998  pNew->iEqpId = iEqpId;
1999  pNew->iParentId = p2;
2000  memcpy(pNew->zText, zText, nText+1);
2001  pNew->pNext = 0;
2002  if( p->sGraph.pLast ){
2003    p->sGraph.pLast->pNext = pNew;
2004  }else{
2005    p->sGraph.pRow = pNew;
2006  }
2007  p->sGraph.pLast = pNew;
2008}
2009
2010/*
2011** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2012** in p->sGraph.
2013*/
2014static void eqp_reset(ShellState *p){
2015  EQPGraphRow *pRow, *pNext;
2016  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2017    pNext = pRow->pNext;
2018    sqlite3_free(pRow);
2019  }
2020  memset(&p->sGraph, 0, sizeof(p->sGraph));
2021}
2022
2023/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2024** pOld, or return the first such line if pOld is NULL
2025*/
2026static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2027  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2028  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2029  return pRow;
2030}
2031
2032/* Render a single level of the graph that has iEqpId as its parent.  Called
2033** recursively to render sublevels.
2034*/
2035static void eqp_render_level(ShellState *p, int iEqpId){
2036  EQPGraphRow *pRow, *pNext;
2037  int n = strlen30(p->sGraph.zPrefix);
2038  char *z;
2039  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2040    pNext = eqp_next_row(p, iEqpId, pRow);
2041    z = pRow->zText;
2042    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2043                pNext ? "|--" : "`--", z);
2044    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
2045      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
2046      eqp_render_level(p, pRow->iEqpId);
2047      p->sGraph.zPrefix[n] = 0;
2048    }
2049  }
2050}
2051
2052/*
2053** Display and reset the EXPLAIN QUERY PLAN data
2054*/
2055static void eqp_render(ShellState *p){
2056  EQPGraphRow *pRow = p->sGraph.pRow;
2057  if( pRow ){
2058    if( pRow->zText[0]=='-' ){
2059      if( pRow->pNext==0 ){
2060        eqp_reset(p);
2061        return;
2062      }
2063      utf8_printf(p->out, "%s\n", pRow->zText+3);
2064      p->sGraph.pRow = pRow->pNext;
2065      sqlite3_free(pRow);
2066    }else{
2067      utf8_printf(p->out, "QUERY PLAN\n");
2068    }
2069    p->sGraph.zPrefix[0] = 0;
2070    eqp_render_level(p, 0);
2071    eqp_reset(p);
2072  }
2073}
2074
2075#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2076/*
2077** Progress handler callback.
2078*/
2079static int progress_handler(void *pClientData) {
2080  ShellState *p = (ShellState*)pClientData;
2081  p->nProgress++;
2082  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2083    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2084    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2085    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2086    return 1;
2087  }
2088  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2089    raw_printf(p->out, "Progress %u\n", p->nProgress);
2090  }
2091  return 0;
2092}
2093#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2094
2095/*
2096** Print N dashes
2097*/
2098static void print_dashes(FILE *out, int N){
2099  const char zDash[] = "--------------------------------------------------";
2100  const int nDash = sizeof(zDash) - 1;
2101  while( N>nDash ){
2102    fputs(zDash, out);
2103    N -= nDash;
2104  }
2105  raw_printf(out, "%.*s", N, zDash);
2106}
2107
2108/*
2109** Print a markdown or table-style row separator using ascii-art
2110*/
2111static void print_row_separator(
2112  ShellState *p,
2113  int nArg,
2114  const char *zSep
2115){
2116  int i;
2117  if( nArg>0 ){
2118    fputs(zSep, p->out);
2119    print_dashes(p->out, p->actualWidth[0]+2);
2120    for(i=1; i<nArg; i++){
2121      fputs(zSep, p->out);
2122      print_dashes(p->out, p->actualWidth[i]+2);
2123    }
2124    fputs(zSep, p->out);
2125  }
2126  fputs("\n", p->out);
2127}
2128
2129/*
2130** This is the callback routine that the shell
2131** invokes for each row of a query result.
2132*/
2133static int shell_callback(
2134  void *pArg,
2135  int nArg,        /* Number of result columns */
2136  char **azArg,    /* Text of each result column */
2137  char **azCol,    /* Column names */
2138  int *aiType      /* Column types.  Might be NULL */
2139){
2140  int i;
2141  ShellState *p = (ShellState*)pArg;
2142
2143  if( azArg==0 ) return 0;
2144  switch( p->cMode ){
2145    case MODE_Count:
2146    case MODE_Off: {
2147      break;
2148    }
2149    case MODE_Line: {
2150      int w = 5;
2151      if( azArg==0 ) break;
2152      for(i=0; i<nArg; i++){
2153        int len = strlen30(azCol[i] ? azCol[i] : "");
2154        if( len>w ) w = len;
2155      }
2156      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2157      for(i=0; i<nArg; i++){
2158        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2159                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2160      }
2161      break;
2162    }
2163    case MODE_Explain: {
2164      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2165      if( nArg>ArraySize(aExplainWidth) ){
2166        nArg = ArraySize(aExplainWidth);
2167      }
2168      if( p->cnt++==0 ){
2169        for(i=0; i<nArg; i++){
2170          int w = aExplainWidth[i];
2171          utf8_width_print(p->out, w, azCol[i]);
2172          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2173        }
2174        for(i=0; i<nArg; i++){
2175          int w = aExplainWidth[i];
2176          print_dashes(p->out, w);
2177          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2178        }
2179      }
2180      if( azArg==0 ) break;
2181      for(i=0; i<nArg; i++){
2182        int w = aExplainWidth[i];
2183        if( i==nArg-1 ) w = 0;
2184        if( azArg[i] && strlenChar(azArg[i])>w ){
2185          w = strlenChar(azArg[i]);
2186        }
2187        if( i==1 && p->aiIndent && p->pStmt ){
2188          if( p->iIndent<p->nIndent ){
2189            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2190          }
2191          p->iIndent++;
2192        }
2193        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2194        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2195      }
2196      break;
2197    }
2198    case MODE_Semi: {   /* .schema and .fullschema output */
2199      printSchemaLine(p->out, azArg[0], ";\n");
2200      break;
2201    }
2202    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2203      char *z;
2204      int j;
2205      int nParen = 0;
2206      char cEnd = 0;
2207      char c;
2208      int nLine = 0;
2209      assert( nArg==1 );
2210      if( azArg[0]==0 ) break;
2211      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2212       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2213      ){
2214        utf8_printf(p->out, "%s;\n", azArg[0]);
2215        break;
2216      }
2217      z = sqlite3_mprintf("%s", azArg[0]);
2218      shell_check_oom(z);
2219      j = 0;
2220      for(i=0; IsSpace(z[i]); i++){}
2221      for(; (c = z[i])!=0; i++){
2222        if( IsSpace(c) ){
2223          if( z[j-1]=='\r' ) z[j-1] = '\n';
2224          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2225        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2226          j--;
2227        }
2228        z[j++] = c;
2229      }
2230      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2231      z[j] = 0;
2232      if( strlen30(z)>=79 ){
2233        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2234          if( c==cEnd ){
2235            cEnd = 0;
2236          }else if( c=='"' || c=='\'' || c=='`' ){
2237            cEnd = c;
2238          }else if( c=='[' ){
2239            cEnd = ']';
2240          }else if( c=='-' && z[i+1]=='-' ){
2241            cEnd = '\n';
2242          }else if( c=='(' ){
2243            nParen++;
2244          }else if( c==')' ){
2245            nParen--;
2246            if( nLine>0 && nParen==0 && j>0 ){
2247              printSchemaLineN(p->out, z, j, "\n");
2248              j = 0;
2249            }
2250          }
2251          z[j++] = c;
2252          if( nParen==1 && cEnd==0
2253           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2254          ){
2255            if( c=='\n' ) j--;
2256            printSchemaLineN(p->out, z, j, "\n  ");
2257            j = 0;
2258            nLine++;
2259            while( IsSpace(z[i+1]) ){ i++; }
2260          }
2261        }
2262        z[j] = 0;
2263      }
2264      printSchemaLine(p->out, z, ";\n");
2265      sqlite3_free(z);
2266      break;
2267    }
2268    case MODE_List: {
2269      if( p->cnt++==0 && p->showHeader ){
2270        for(i=0; i<nArg; i++){
2271          utf8_printf(p->out,"%s%s",azCol[i],
2272                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2273        }
2274      }
2275      if( azArg==0 ) break;
2276      for(i=0; i<nArg; i++){
2277        char *z = azArg[i];
2278        if( z==0 ) z = p->nullValue;
2279        utf8_printf(p->out, "%s", z);
2280        if( i<nArg-1 ){
2281          utf8_printf(p->out, "%s", p->colSeparator);
2282        }else{
2283          utf8_printf(p->out, "%s", p->rowSeparator);
2284        }
2285      }
2286      break;
2287    }
2288    case MODE_Html: {
2289      if( p->cnt++==0 && p->showHeader ){
2290        raw_printf(p->out,"<TR>");
2291        for(i=0; i<nArg; i++){
2292          raw_printf(p->out,"<TH>");
2293          output_html_string(p->out, azCol[i]);
2294          raw_printf(p->out,"</TH>\n");
2295        }
2296        raw_printf(p->out,"</TR>\n");
2297      }
2298      if( azArg==0 ) break;
2299      raw_printf(p->out,"<TR>");
2300      for(i=0; i<nArg; i++){
2301        raw_printf(p->out,"<TD>");
2302        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2303        raw_printf(p->out,"</TD>\n");
2304      }
2305      raw_printf(p->out,"</TR>\n");
2306      break;
2307    }
2308    case MODE_Tcl: {
2309      if( p->cnt++==0 && p->showHeader ){
2310        for(i=0; i<nArg; i++){
2311          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2312          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2313        }
2314        utf8_printf(p->out, "%s", p->rowSeparator);
2315      }
2316      if( azArg==0 ) break;
2317      for(i=0; i<nArg; i++){
2318        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2319        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2320      }
2321      utf8_printf(p->out, "%s", p->rowSeparator);
2322      break;
2323    }
2324    case MODE_Csv: {
2325      setBinaryMode(p->out, 1);
2326      if( p->cnt++==0 && p->showHeader ){
2327        for(i=0; i<nArg; i++){
2328          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2329        }
2330        utf8_printf(p->out, "%s", p->rowSeparator);
2331      }
2332      if( nArg>0 ){
2333        for(i=0; i<nArg; i++){
2334          output_csv(p, azArg[i], i<nArg-1);
2335        }
2336        utf8_printf(p->out, "%s", p->rowSeparator);
2337      }
2338      setTextMode(p->out, 1);
2339      break;
2340    }
2341    case MODE_Insert: {
2342      if( azArg==0 ) break;
2343      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2344      if( p->showHeader ){
2345        raw_printf(p->out,"(");
2346        for(i=0; i<nArg; i++){
2347          if( i>0 ) raw_printf(p->out, ",");
2348          if( quoteChar(azCol[i]) ){
2349            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2350            shell_check_oom(z);
2351            utf8_printf(p->out, "%s", z);
2352            sqlite3_free(z);
2353          }else{
2354            raw_printf(p->out, "%s", azCol[i]);
2355          }
2356        }
2357        raw_printf(p->out,")");
2358      }
2359      p->cnt++;
2360      for(i=0; i<nArg; i++){
2361        raw_printf(p->out, i>0 ? "," : " VALUES(");
2362        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2363          utf8_printf(p->out,"NULL");
2364        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2365          if( ShellHasFlag(p, SHFLG_Newlines) ){
2366            output_quoted_string(p->out, azArg[i]);
2367          }else{
2368            output_quoted_escaped_string(p->out, azArg[i]);
2369          }
2370        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2371          utf8_printf(p->out,"%s", azArg[i]);
2372        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2373          char z[50];
2374          double r = sqlite3_column_double(p->pStmt, i);
2375          sqlite3_uint64 ur;
2376          memcpy(&ur,&r,sizeof(r));
2377          if( ur==0x7ff0000000000000LL ){
2378            raw_printf(p->out, "1e999");
2379          }else if( ur==0xfff0000000000000LL ){
2380            raw_printf(p->out, "-1e999");
2381          }else{
2382            sqlite3_int64 ir = (sqlite3_int64)r;
2383            if( r==(double)ir ){
2384              sqlite3_snprintf(50,z,"%lld.0", ir);
2385            }else{
2386              sqlite3_snprintf(50,z,"%!.20g", r);
2387            }
2388            raw_printf(p->out, "%s", z);
2389          }
2390        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2391          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2392          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2393          output_hex_blob(p->out, pBlob, nBlob);
2394        }else if( isNumber(azArg[i], 0) ){
2395          utf8_printf(p->out,"%s", azArg[i]);
2396        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2397          output_quoted_string(p->out, azArg[i]);
2398        }else{
2399          output_quoted_escaped_string(p->out, azArg[i]);
2400        }
2401      }
2402      raw_printf(p->out,");\n");
2403      break;
2404    }
2405    case MODE_Json: {
2406      if( azArg==0 ) break;
2407      if( p->cnt==0 ){
2408        fputs("[{", p->out);
2409      }else{
2410        fputs(",\n{", p->out);
2411      }
2412      p->cnt++;
2413      for(i=0; i<nArg; i++){
2414        output_json_string(p->out, azCol[i], -1);
2415        putc(':', p->out);
2416        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2417          fputs("null",p->out);
2418        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2419          char z[50];
2420          double r = sqlite3_column_double(p->pStmt, i);
2421          sqlite3_uint64 ur;
2422          memcpy(&ur,&r,sizeof(r));
2423          if( ur==0x7ff0000000000000LL ){
2424            raw_printf(p->out, "1e999");
2425          }else if( ur==0xfff0000000000000LL ){
2426            raw_printf(p->out, "-1e999");
2427          }else{
2428            sqlite3_snprintf(50,z,"%!.20g", r);
2429            raw_printf(p->out, "%s", z);
2430          }
2431        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2432          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2433          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2434          output_json_string(p->out, pBlob, nBlob);
2435        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2436          output_json_string(p->out, azArg[i], -1);
2437        }else{
2438          utf8_printf(p->out,"%s", azArg[i]);
2439        }
2440        if( i<nArg-1 ){
2441          putc(',', p->out);
2442        }
2443      }
2444      putc('}', p->out);
2445      break;
2446    }
2447    case MODE_Quote: {
2448      if( azArg==0 ) break;
2449      if( p->cnt==0 && p->showHeader ){
2450        for(i=0; i<nArg; i++){
2451          if( i>0 ) fputs(p->colSeparator, p->out);
2452          output_quoted_string(p->out, azCol[i]);
2453        }
2454        fputs(p->rowSeparator, p->out);
2455      }
2456      p->cnt++;
2457      for(i=0; i<nArg; i++){
2458        if( i>0 ) fputs(p->colSeparator, p->out);
2459        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2460          utf8_printf(p->out,"NULL");
2461        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2462          output_quoted_string(p->out, azArg[i]);
2463        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2464          utf8_printf(p->out,"%s", azArg[i]);
2465        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2466          char z[50];
2467          double r = sqlite3_column_double(p->pStmt, i);
2468          sqlite3_snprintf(50,z,"%!.20g", r);
2469          raw_printf(p->out, "%s", z);
2470        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2471          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2472          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2473          output_hex_blob(p->out, pBlob, nBlob);
2474        }else if( isNumber(azArg[i], 0) ){
2475          utf8_printf(p->out,"%s", azArg[i]);
2476        }else{
2477          output_quoted_string(p->out, azArg[i]);
2478        }
2479      }
2480      fputs(p->rowSeparator, p->out);
2481      break;
2482    }
2483    case MODE_Ascii: {
2484      if( p->cnt++==0 && p->showHeader ){
2485        for(i=0; i<nArg; i++){
2486          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2487          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2488        }
2489        utf8_printf(p->out, "%s", p->rowSeparator);
2490      }
2491      if( azArg==0 ) break;
2492      for(i=0; i<nArg; i++){
2493        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2494        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2495      }
2496      utf8_printf(p->out, "%s", p->rowSeparator);
2497      break;
2498    }
2499    case MODE_EQP: {
2500      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2501      break;
2502    }
2503  }
2504  return 0;
2505}
2506
2507/*
2508** This is the callback routine that the SQLite library
2509** invokes for each row of a query result.
2510*/
2511static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2512  /* since we don't have type info, call the shell_callback with a NULL value */
2513  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2514}
2515
2516/*
2517** This is the callback routine from sqlite3_exec() that appends all
2518** output onto the end of a ShellText object.
2519*/
2520static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2521  ShellText *p = (ShellText*)pArg;
2522  int i;
2523  UNUSED_PARAMETER(az);
2524  if( azArg==0 ) return 0;
2525  if( p->n ) appendText(p, "|", 0);
2526  for(i=0; i<nArg; i++){
2527    if( i ) appendText(p, ",", 0);
2528    if( azArg[i] ) appendText(p, azArg[i], 0);
2529  }
2530  return 0;
2531}
2532
2533/*
2534** Generate an appropriate SELFTEST table in the main database.
2535*/
2536static void createSelftestTable(ShellState *p){
2537  char *zErrMsg = 0;
2538  sqlite3_exec(p->db,
2539    "SAVEPOINT selftest_init;\n"
2540    "CREATE TABLE IF NOT EXISTS selftest(\n"
2541    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2542    "  op TEXT,\n"                   /* Operator:  memo run */
2543    "  cmd TEXT,\n"                  /* Command text */
2544    "  ans TEXT\n"                   /* Desired answer */
2545    ");"
2546    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2547    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2548    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2549    "         'memo','Tests generated by --init');\n"
2550    "INSERT INTO [_shell$self]\n"
2551    "  SELECT 'run',\n"
2552    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2553                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2554    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2555                          "FROM sqlite_schema ORDER BY 2',224));\n"
2556    "INSERT INTO [_shell$self]\n"
2557    "  SELECT 'run',"
2558    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2559    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2560    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2561    "  FROM (\n"
2562    "    SELECT name FROM sqlite_schema\n"
2563    "     WHERE type='table'\n"
2564    "       AND name<>'selftest'\n"
2565    "       AND coalesce(rootpage,0)>0\n"
2566    "  )\n"
2567    " ORDER BY name;\n"
2568    "INSERT INTO [_shell$self]\n"
2569    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2570    "INSERT INTO selftest(tno,op,cmd,ans)"
2571    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2572    "DROP TABLE [_shell$self];"
2573    ,0,0,&zErrMsg);
2574  if( zErrMsg ){
2575    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2576    sqlite3_free(zErrMsg);
2577  }
2578  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2579}
2580
2581
2582/*
2583** Set the destination table field of the ShellState structure to
2584** the name of the table given.  Escape any quote characters in the
2585** table name.
2586*/
2587static void set_table_name(ShellState *p, const char *zName){
2588  int i, n;
2589  char cQuote;
2590  char *z;
2591
2592  if( p->zDestTable ){
2593    free(p->zDestTable);
2594    p->zDestTable = 0;
2595  }
2596  if( zName==0 ) return;
2597  cQuote = quoteChar(zName);
2598  n = strlen30(zName);
2599  if( cQuote ) n += n+2;
2600  z = p->zDestTable = malloc( n+1 );
2601  shell_check_oom(z);
2602  n = 0;
2603  if( cQuote ) z[n++] = cQuote;
2604  for(i=0; zName[i]; i++){
2605    z[n++] = zName[i];
2606    if( zName[i]==cQuote ) z[n++] = cQuote;
2607  }
2608  if( cQuote ) z[n++] = cQuote;
2609  z[n] = 0;
2610}
2611
2612/*
2613** Maybe construct two lines of text that point out the position of a
2614** syntax error.  Return a pointer to the text, in memory obtained from
2615** sqlite3_malloc().  Or, if the most recent error does not involve a
2616** specific token that we can point to, return an empty string.
2617**
2618** In all cases, the memory returned is obtained from sqlite3_malloc64()
2619** and should be released by the caller invoking sqlite3_free().
2620*/
2621static char *shell_error_context(const char *zSql, sqlite3 *db){
2622  int iOffset;
2623  size_t len;
2624  char *zCode;
2625  char *zMsg;
2626  int i;
2627  if( db==0
2628   || zSql==0
2629   || (iOffset = sqlite3_error_offset(db))<0
2630  ){
2631    return sqlite3_mprintf("");
2632  }
2633  while( iOffset>50 ){
2634    iOffset--;
2635    zSql++;
2636    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
2637  }
2638  len = strlen(zSql);
2639  if( len>78 ){
2640    len = 78;
2641    while( (zSql[len]&0xc0)==0x80 ) len--;
2642  }
2643  zCode = sqlite3_mprintf("%.*s", len, zSql);
2644  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
2645  if( iOffset<25 ){
2646    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");
2647  }else{
2648    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");
2649  }
2650  return zMsg;
2651}
2652
2653
2654/*
2655** Execute a query statement that will generate SQL output.  Print
2656** the result columns, comma-separated, on a line and then add a
2657** semicolon terminator to the end of that line.
2658**
2659** If the number of columns is 1 and that column contains text "--"
2660** then write the semicolon on a separate line.  That way, if a
2661** "--" comment occurs at the end of the statement, the comment
2662** won't consume the semicolon terminator.
2663*/
2664static int run_table_dump_query(
2665  ShellState *p,           /* Query context */
2666  const char *zSelect      /* SELECT statement to extract content */
2667){
2668  sqlite3_stmt *pSelect;
2669  int rc;
2670  int nResult;
2671  int i;
2672  const char *z;
2673  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2674  if( rc!=SQLITE_OK || !pSelect ){
2675    char *zContext = shell_error_context(zSelect, p->db);
2676    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
2677                sqlite3_errmsg(p->db), zContext);
2678    sqlite3_free(zContext);
2679    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2680    return rc;
2681  }
2682  rc = sqlite3_step(pSelect);
2683  nResult = sqlite3_column_count(pSelect);
2684  while( rc==SQLITE_ROW ){
2685    z = (const char*)sqlite3_column_text(pSelect, 0);
2686    utf8_printf(p->out, "%s", z);
2687    for(i=1; i<nResult; i++){
2688      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2689    }
2690    if( z==0 ) z = "";
2691    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2692    if( z[0] ){
2693      raw_printf(p->out, "\n;\n");
2694    }else{
2695      raw_printf(p->out, ";\n");
2696    }
2697    rc = sqlite3_step(pSelect);
2698  }
2699  rc = sqlite3_finalize(pSelect);
2700  if( rc!=SQLITE_OK ){
2701    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2702                sqlite3_errmsg(p->db));
2703    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2704  }
2705  return rc;
2706}
2707
2708/*
2709** Allocate space and save off string indicating current error.
2710*/
2711static char *save_err_msg(
2712  sqlite3 *db,           /* Database to query */
2713  const char *zPhase,    /* When the error occcurs */
2714  int rc,                /* Error code returned from API */
2715  const char *zSql       /* SQL string, or NULL */
2716){
2717  char *zErr;
2718  char *zContext;
2719  sqlite3_str *pStr = sqlite3_str_new(0);
2720  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
2721  if( rc>1 ){
2722    sqlite3_str_appendf(pStr, " (%d)", rc);
2723  }
2724  zContext = shell_error_context(zSql, db);
2725  if( zContext ){
2726    sqlite3_str_appendall(pStr, zContext);
2727    sqlite3_free(zContext);
2728  }
2729  zErr = sqlite3_str_finish(pStr);
2730  shell_check_oom(zErr);
2731  return zErr;
2732}
2733
2734#ifdef __linux__
2735/*
2736** Attempt to display I/O stats on Linux using /proc/PID/io
2737*/
2738static void displayLinuxIoStats(FILE *out){
2739  FILE *in;
2740  char z[200];
2741  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2742  in = fopen(z, "rb");
2743  if( in==0 ) return;
2744  while( fgets(z, sizeof(z), in)!=0 ){
2745    static const struct {
2746      const char *zPattern;
2747      const char *zDesc;
2748    } aTrans[] = {
2749      { "rchar: ",                  "Bytes received by read():" },
2750      { "wchar: ",                  "Bytes sent to write():"    },
2751      { "syscr: ",                  "Read() system calls:"      },
2752      { "syscw: ",                  "Write() system calls:"     },
2753      { "read_bytes: ",             "Bytes read from storage:"  },
2754      { "write_bytes: ",            "Bytes written to storage:" },
2755      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2756    };
2757    int i;
2758    for(i=0; i<ArraySize(aTrans); i++){
2759      int n = strlen30(aTrans[i].zPattern);
2760      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2761        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2762        break;
2763      }
2764    }
2765  }
2766  fclose(in);
2767}
2768#endif
2769
2770/*
2771** Display a single line of status using 64-bit values.
2772*/
2773static void displayStatLine(
2774  ShellState *p,            /* The shell context */
2775  char *zLabel,             /* Label for this one line */
2776  char *zFormat,            /* Format for the result */
2777  int iStatusCtrl,          /* Which status to display */
2778  int bReset                /* True to reset the stats */
2779){
2780  sqlite3_int64 iCur = -1;
2781  sqlite3_int64 iHiwtr = -1;
2782  int i, nPercent;
2783  char zLine[200];
2784  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2785  for(i=0, nPercent=0; zFormat[i]; i++){
2786    if( zFormat[i]=='%' ) nPercent++;
2787  }
2788  if( nPercent>1 ){
2789    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2790  }else{
2791    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2792  }
2793  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2794}
2795
2796/*
2797** Display memory stats.
2798*/
2799static int display_stats(
2800  sqlite3 *db,                /* Database to query */
2801  ShellState *pArg,           /* Pointer to ShellState */
2802  int bReset                  /* True to reset the stats */
2803){
2804  int iCur;
2805  int iHiwtr;
2806  FILE *out;
2807  if( pArg==0 || pArg->out==0 ) return 0;
2808  out = pArg->out;
2809
2810  if( pArg->pStmt && pArg->statsOn==2 ){
2811    int nCol, i, x;
2812    sqlite3_stmt *pStmt = pArg->pStmt;
2813    char z[100];
2814    nCol = sqlite3_column_count(pStmt);
2815    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2816    for(i=0; i<nCol; i++){
2817      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2818      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2819#ifndef SQLITE_OMIT_DECLTYPE
2820      sqlite3_snprintf(30, z+x, "declared type:");
2821      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2822#endif
2823#ifdef SQLITE_ENABLE_COLUMN_METADATA
2824      sqlite3_snprintf(30, z+x, "database name:");
2825      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2826      sqlite3_snprintf(30, z+x, "table name:");
2827      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2828      sqlite3_snprintf(30, z+x, "origin name:");
2829      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2830#endif
2831    }
2832  }
2833
2834  if( pArg->statsOn==3 ){
2835    if( pArg->pStmt ){
2836      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2837      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2838    }
2839    return 0;
2840  }
2841
2842  displayStatLine(pArg, "Memory Used:",
2843     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2844  displayStatLine(pArg, "Number of Outstanding Allocations:",
2845     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2846  if( pArg->shellFlgs & SHFLG_Pagecache ){
2847    displayStatLine(pArg, "Number of Pcache Pages Used:",
2848       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2849  }
2850  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2851     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2852  displayStatLine(pArg, "Largest Allocation:",
2853     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2854  displayStatLine(pArg, "Largest Pcache Allocation:",
2855     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2856#ifdef YYTRACKMAXSTACKDEPTH
2857  displayStatLine(pArg, "Deepest Parser Stack:",
2858     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2859#endif
2860
2861  if( db ){
2862    if( pArg->shellFlgs & SHFLG_Lookaside ){
2863      iHiwtr = iCur = -1;
2864      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2865                        &iCur, &iHiwtr, bReset);
2866      raw_printf(pArg->out,
2867              "Lookaside Slots Used:                %d (max %d)\n",
2868              iCur, iHiwtr);
2869      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2870                        &iCur, &iHiwtr, bReset);
2871      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2872              iHiwtr);
2873      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2874                        &iCur, &iHiwtr, bReset);
2875      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2876              iHiwtr);
2877      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2878                        &iCur, &iHiwtr, bReset);
2879      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2880              iHiwtr);
2881    }
2882    iHiwtr = iCur = -1;
2883    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2884    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2885            iCur);
2886    iHiwtr = iCur = -1;
2887    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2888    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2889    iHiwtr = iCur = -1;
2890    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2891    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2892    iHiwtr = iCur = -1;
2893    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2894    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2895    iHiwtr = iCur = -1;
2896    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2897    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2898    iHiwtr = iCur = -1;
2899    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2900    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2901            iCur);
2902    iHiwtr = iCur = -1;
2903    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2904    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2905            iCur);
2906  }
2907
2908  if( pArg->pStmt ){
2909    int iHit, iMiss;
2910    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2911                               bReset);
2912    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2913    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2914    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2915    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2916    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2917    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);
2918    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);
2919    if( iHit || iMiss ){
2920      raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",
2921            iHit, iHit+iMiss);
2922    }
2923    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2924    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2925    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2926    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2927    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2928    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2929    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2930    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2931  }
2932
2933#ifdef __linux__
2934  displayLinuxIoStats(pArg->out);
2935#endif
2936
2937  /* Do not remove this machine readable comment: extra-stats-output-here */
2938
2939  return 0;
2940}
2941
2942/*
2943** Display scan stats.
2944*/
2945static void display_scanstats(
2946  sqlite3 *db,                    /* Database to query */
2947  ShellState *pArg                /* Pointer to ShellState */
2948){
2949#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2950  UNUSED_PARAMETER(db);
2951  UNUSED_PARAMETER(pArg);
2952#else
2953  int i, k, n, mx;
2954  raw_printf(pArg->out, "-------- scanstats --------\n");
2955  mx = 0;
2956  for(k=0; k<=mx; k++){
2957    double rEstLoop = 1.0;
2958    for(i=n=0; 1; i++){
2959      sqlite3_stmt *p = pArg->pStmt;
2960      sqlite3_int64 nLoop, nVisit;
2961      double rEst;
2962      int iSid;
2963      const char *zExplain;
2964      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2965        break;
2966      }
2967      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2968      if( iSid>mx ) mx = iSid;
2969      if( iSid!=k ) continue;
2970      if( n==0 ){
2971        rEstLoop = (double)nLoop;
2972        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2973      }
2974      n++;
2975      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2976      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2977      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2978      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2979      rEstLoop *= rEst;
2980      raw_printf(pArg->out,
2981          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2982          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2983      );
2984    }
2985  }
2986  raw_printf(pArg->out, "---------------------------\n");
2987#endif
2988}
2989
2990/*
2991** Parameter azArray points to a zero-terminated array of strings. zStr
2992** points to a single nul-terminated string. Return non-zero if zStr
2993** is equal, according to strcmp(), to any of the strings in the array.
2994** Otherwise, return zero.
2995*/
2996static int str_in_array(const char *zStr, const char **azArray){
2997  int i;
2998  for(i=0; azArray[i]; i++){
2999    if( 0==strcmp(zStr, azArray[i]) ) return 1;
3000  }
3001  return 0;
3002}
3003
3004/*
3005** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3006** and populate the ShellState.aiIndent[] array with the number of
3007** spaces each opcode should be indented before it is output.
3008**
3009** The indenting rules are:
3010**
3011**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3012**       all opcodes that occur between the p2 jump destination and the opcode
3013**       itself by 2 spaces.
3014**
3015**     * Do the previous for "Return" instructions for when P2 is positive.
3016**       See tag-20220407a in wherecode.c and vdbe.c.
3017**
3018**     * For each "Goto", if the jump destination is earlier in the program
3019**       and ends on one of:
3020**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
3021**       or if the P1 parameter is one instead of zero,
3022**       then indent all opcodes between the earlier instruction
3023**       and "Goto" by 2 spaces.
3024*/
3025static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3026  const char *zSql;               /* The text of the SQL statement */
3027  const char *z;                  /* Used to check if this is an EXPLAIN */
3028  int *abYield = 0;               /* True if op is an OP_Yield */
3029  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
3030  int iOp;                        /* Index of operation in p->aiIndent[] */
3031
3032  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3033                           "Return", 0 };
3034  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3035                            "Rewind", 0 };
3036  const char *azGoto[] = { "Goto", 0 };
3037
3038  /* Try to figure out if this is really an EXPLAIN statement. If this
3039  ** cannot be verified, return early.  */
3040  if( sqlite3_column_count(pSql)!=8 ){
3041    p->cMode = p->mode;
3042    return;
3043  }
3044  zSql = sqlite3_sql(pSql);
3045  if( zSql==0 ) return;
3046  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3047  if( sqlite3_strnicmp(z, "explain", 7) ){
3048    p->cMode = p->mode;
3049    return;
3050  }
3051
3052  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3053    int i;
3054    int iAddr = sqlite3_column_int(pSql, 0);
3055    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3056
3057    /* Set p2 to the P2 field of the current opcode. Then, assuming that
3058    ** p2 is an instruction address, set variable p2op to the index of that
3059    ** instruction in the aiIndent[] array. p2 and p2op may be different if
3060    ** the current instruction is part of a sub-program generated by an
3061    ** SQL trigger or foreign key.  */
3062    int p2 = sqlite3_column_int(pSql, 3);
3063    int p2op = (p2 + (iOp-iAddr));
3064
3065    /* Grow the p->aiIndent array as required */
3066    if( iOp>=nAlloc ){
3067      if( iOp==0 ){
3068        /* Do further verfication that this is explain output.  Abort if
3069        ** it is not */
3070        static const char *explainCols[] = {
3071           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3072        int jj;
3073        for(jj=0; jj<ArraySize(explainCols); jj++){
3074          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3075            p->cMode = p->mode;
3076            sqlite3_reset(pSql);
3077            return;
3078          }
3079        }
3080      }
3081      nAlloc += 100;
3082      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3083      shell_check_oom(p->aiIndent);
3084      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3085      shell_check_oom(abYield);
3086    }
3087    abYield[iOp] = str_in_array(zOp, azYield);
3088    p->aiIndent[iOp] = 0;
3089    p->nIndent = iOp+1;
3090
3091    if( str_in_array(zOp, azNext) && p2op>0 ){
3092      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3093    }
3094    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3095     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3096    ){
3097      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3098    }
3099  }
3100
3101  p->iIndent = 0;
3102  sqlite3_free(abYield);
3103  sqlite3_reset(pSql);
3104}
3105
3106/*
3107** Free the array allocated by explain_data_prepare().
3108*/
3109static void explain_data_delete(ShellState *p){
3110  sqlite3_free(p->aiIndent);
3111  p->aiIndent = 0;
3112  p->nIndent = 0;
3113  p->iIndent = 0;
3114}
3115
3116/*
3117** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3118*/
3119static unsigned int savedSelectTrace;
3120static unsigned int savedWhereTrace;
3121static void disable_debug_trace_modes(void){
3122  unsigned int zero = 0;
3123  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3124  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3125  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3126  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3127}
3128static void restore_debug_trace_modes(void){
3129  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3130  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3131}
3132
3133/* Create the TEMP table used to store parameter bindings */
3134static void bind_table_init(ShellState *p){
3135  int wrSchema = 0;
3136  int defensiveMode = 0;
3137  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3138  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3139  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3140  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3141  sqlite3_exec(p->db,
3142    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3143    "  key TEXT PRIMARY KEY,\n"
3144    "  value\n"
3145    ") WITHOUT ROWID;",
3146    0, 0, 0);
3147  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3148  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3149}
3150
3151/*
3152** Bind parameters on a prepared statement.
3153**
3154** Parameter bindings are taken from a TEMP table of the form:
3155**
3156**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3157**    WITHOUT ROWID;
3158**
3159** No bindings occur if this table does not exist.  The name of the table
3160** begins with "sqlite_" so that it will not collide with ordinary application
3161** tables.  The table must be in the TEMP schema.
3162*/
3163static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3164  int nVar;
3165  int i;
3166  int rc;
3167  sqlite3_stmt *pQ = 0;
3168
3169  nVar = sqlite3_bind_parameter_count(pStmt);
3170  if( nVar==0 ) return;  /* Nothing to do */
3171  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3172                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3173    return; /* Parameter table does not exist */
3174  }
3175  rc = sqlite3_prepare_v2(pArg->db,
3176          "SELECT value FROM temp.sqlite_parameters"
3177          " WHERE key=?1", -1, &pQ, 0);
3178  if( rc || pQ==0 ) return;
3179  for(i=1; i<=nVar; i++){
3180    char zNum[30];
3181    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3182    if( zVar==0 ){
3183      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3184      zVar = zNum;
3185    }
3186    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3187    if( sqlite3_step(pQ)==SQLITE_ROW ){
3188      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3189    }else{
3190      sqlite3_bind_null(pStmt, i);
3191    }
3192    sqlite3_reset(pQ);
3193  }
3194  sqlite3_finalize(pQ);
3195}
3196
3197/*
3198** UTF8 box-drawing characters.  Imagine box lines like this:
3199**
3200**           1
3201**           |
3202**       4 --+-- 2
3203**           |
3204**           3
3205**
3206** Each box characters has between 2 and 4 of the lines leading from
3207** the center.  The characters are here identified by the numbers of
3208** their corresponding lines.
3209*/
3210#define BOX_24   "\342\224\200"  /* U+2500 --- */
3211#define BOX_13   "\342\224\202"  /* U+2502  |  */
3212#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3213#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3214#define BOX_12   "\342\224\224"  /* U+2514  '- */
3215#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3216#define BOX_123  "\342\224\234"  /* U+251c  |- */
3217#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3218#define BOX_234  "\342\224\254"  /* U+252c -,- */
3219#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3220#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3221
3222/* Draw horizontal line N characters long using unicode box
3223** characters
3224*/
3225static void print_box_line(FILE *out, int N){
3226  const char zDash[] =
3227      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3228      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3229  const int nDash = sizeof(zDash) - 1;
3230  N *= 3;
3231  while( N>nDash ){
3232    utf8_printf(out, zDash);
3233    N -= nDash;
3234  }
3235  utf8_printf(out, "%.*s", N, zDash);
3236}
3237
3238/*
3239** Draw a horizontal separator for a MODE_Box table.
3240*/
3241static void print_box_row_separator(
3242  ShellState *p,
3243  int nArg,
3244  const char *zSep1,
3245  const char *zSep2,
3246  const char *zSep3
3247){
3248  int i;
3249  if( nArg>0 ){
3250    utf8_printf(p->out, "%s", zSep1);
3251    print_box_line(p->out, p->actualWidth[0]+2);
3252    for(i=1; i<nArg; i++){
3253      utf8_printf(p->out, "%s", zSep2);
3254      print_box_line(p->out, p->actualWidth[i]+2);
3255    }
3256    utf8_printf(p->out, "%s", zSep3);
3257  }
3258  fputs("\n", p->out);
3259}
3260
3261/*
3262** z[] is a line of text that is to be displayed the .mode box or table or
3263** similar tabular formats.  z[] might contain control characters such
3264** as \n, \t, \f, or \r.
3265**
3266** Compute characters to display on the first line of z[].  Stop at the
3267** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
3268** from malloc()) of that first line, which caller should free sometime.
3269** Write anything to display on the next line into *pzTail.  If this is
3270** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3271*/
3272static char *translateForDisplayAndDup(
3273  const unsigned char *z,            /* Input text to be transformed */
3274  const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
3275  int mxWidth,                       /* Max width.  0 means no limit */
3276  u8 bWordWrap                       /* If true, avoid breaking mid-word */
3277){
3278  int i;                 /* Input bytes consumed */
3279  int j;                 /* Output bytes generated */
3280  int k;                 /* Input bytes to be displayed */
3281  int n;                 /* Output column number */
3282  unsigned char *zOut;   /* Output text */
3283
3284  if( z==0 ){
3285    *pzTail = 0;
3286    return 0;
3287  }
3288  if( mxWidth<0 ) mxWidth = -mxWidth;
3289  if( mxWidth==0 ) mxWidth = 1000000;
3290  i = j = n = 0;
3291  while( n<mxWidth ){
3292    if( z[i]>=' ' ){
3293      n++;
3294      do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
3295      continue;
3296    }
3297    if( z[i]=='\t' ){
3298      do{
3299        n++;
3300        j++;
3301      }while( (n&7)!=0 && n<mxWidth );
3302      i++;
3303      continue;
3304    }
3305    break;
3306  }
3307  if( n>=mxWidth && bWordWrap  ){
3308    /* Perhaps try to back up to a better place to break the line */
3309    for(k=i; k>i/2; k--){
3310      if( isspace(z[k-1]) ) break;
3311    }
3312    if( k<=i/2 ){
3313      for(k=i; k>i/2; k--){
3314        if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3315      }
3316    }
3317    if( k<=i/2 ){
3318      k = i;
3319    }else{
3320      i = k;
3321      while( z[i]==' ' ) i++;
3322    }
3323  }else{
3324    k = i;
3325  }
3326  if( n>=mxWidth && z[i]>=' ' ){
3327   *pzTail = &z[i];
3328  }else if( z[i]=='\r' && z[i+1]=='\n' ){
3329    *pzTail = z[i+2] ? &z[i+2] : 0;
3330  }else if( z[i]==0 || z[i+1]==0 ){
3331    *pzTail = 0;
3332  }else{
3333    *pzTail = &z[i+1];
3334  }
3335  zOut = malloc( j+1 );
3336  shell_check_oom(zOut);
3337  i = j = n = 0;
3338  while( i<k ){
3339    if( z[i]>=' ' ){
3340      n++;
3341      do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
3342      continue;
3343    }
3344    if( z[i]=='\t' ){
3345      do{
3346        n++;
3347        zOut[j++] = ' ';
3348      }while( (n&7)!=0 && n<mxWidth );
3349      i++;
3350      continue;
3351    }
3352    break;
3353  }
3354  zOut[j] = 0;
3355  return (char*)zOut;
3356}
3357
3358/* Extract the value of the i-th current column for pStmt as an SQL literal
3359** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
3360** the caller.
3361*/
3362static char *quoted_column(sqlite3_stmt *pStmt, int i){
3363  switch( sqlite3_column_type(pStmt, i) ){
3364    case SQLITE_NULL: {
3365      return sqlite3_mprintf("NULL");
3366    }
3367    case SQLITE_INTEGER:
3368    case SQLITE_FLOAT: {
3369      return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3370    }
3371    case SQLITE_TEXT: {
3372      return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3373    }
3374    case SQLITE_BLOB: {
3375      int j;
3376      sqlite3_str *pStr = sqlite3_str_new(0);
3377      const unsigned char *a = sqlite3_column_blob(pStmt,i);
3378      int n = sqlite3_column_bytes(pStmt,i);
3379      sqlite3_str_append(pStr, "x'", 2);
3380      for(j=0; j<n; j++){
3381        sqlite3_str_appendf(pStr, "%02x", a[j]);
3382      }
3383      sqlite3_str_append(pStr, "'", 1);
3384      return sqlite3_str_finish(pStr);
3385    }
3386  }
3387  return 0; /* Not reached */
3388}
3389
3390/*
3391** Run a prepared statement and output the result in one of the
3392** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3393** or MODE_Box.
3394**
3395** This is different from ordinary exec_prepared_stmt() in that
3396** it has to run the entire query and gather the results into memory
3397** first, in order to determine column widths, before providing
3398** any output.
3399*/
3400static void exec_prepared_stmt_columnar(
3401  ShellState *p,                        /* Pointer to ShellState */
3402  sqlite3_stmt *pStmt                   /* Statment to run */
3403){
3404  sqlite3_int64 nRow = 0;
3405  int nColumn = 0;
3406  char **azData = 0;
3407  sqlite3_int64 nAlloc = 0;
3408  char *abRowDiv = 0;
3409  const unsigned char *uz;
3410  const char *z;
3411  char **azQuoted = 0;
3412  int rc;
3413  sqlite3_int64 i, nData;
3414  int j, nTotal, w, n;
3415  const char *colSep = 0;
3416  const char *rowSep = 0;
3417  const unsigned char **azNextLine = 0;
3418  int bNextLine = 0;
3419  int bMultiLineRowExists = 0;
3420  int bw = p->cmOpts.bWordWrap;
3421  const char *zEmpty = "";
3422  const char *zShowNull = p->nullValue;
3423
3424  rc = sqlite3_step(pStmt);
3425  if( rc!=SQLITE_ROW ) return;
3426  nColumn = sqlite3_column_count(pStmt);
3427  nAlloc = nColumn*4;
3428  if( nAlloc<=0 ) nAlloc = 1;
3429  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3430  shell_check_oom(azData);
3431  azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
3432  shell_check_oom((void*)azNextLine);
3433  memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
3434  if( p->cmOpts.bQuote ){
3435    azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
3436    shell_check_oom(azQuoted);
3437    memset(azQuoted, 0, nColumn*sizeof(char*) );
3438  }
3439  abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
3440  shell_check_oom(abRowDiv);
3441  if( nColumn>p->nWidth ){
3442    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3443    shell_check_oom(p->colWidth);
3444    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3445    p->nWidth = nColumn;
3446    p->actualWidth = &p->colWidth[nColumn];
3447  }
3448  memset(p->actualWidth, 0, nColumn*sizeof(int));
3449  for(i=0; i<nColumn; i++){
3450    w = p->colWidth[i];
3451    if( w<0 ) w = -w;
3452    p->actualWidth[i] = w;
3453  }
3454  for(i=0; i<nColumn; i++){
3455    const unsigned char *zNotUsed;
3456    int wx = p->colWidth[i];
3457    if( wx==0 ){
3458      wx = p->cmOpts.iWrap;
3459    }
3460    if( wx<0 ) wx = -wx;
3461    uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
3462    azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
3463  }
3464  do{
3465    int useNextLine = bNextLine;
3466    bNextLine = 0;
3467    if( (nRow+2)*nColumn >= nAlloc ){
3468      nAlloc *= 2;
3469      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3470      shell_check_oom(azData);
3471      abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
3472      shell_check_oom(abRowDiv);
3473    }
3474    abRowDiv[nRow] = 1;
3475    nRow++;
3476    for(i=0; i<nColumn; i++){
3477      int wx = p->colWidth[i];
3478      if( wx==0 ){
3479        wx = p->cmOpts.iWrap;
3480      }
3481      if( wx<0 ) wx = -wx;
3482      if( useNextLine ){
3483        uz = azNextLine[i];
3484        if( uz==0 ) uz = (u8*)zEmpty;
3485      }else if( p->cmOpts.bQuote ){
3486        sqlite3_free(azQuoted[i]);
3487        azQuoted[i] = quoted_column(pStmt,i);
3488        uz = (const unsigned char*)azQuoted[i];
3489      }else{
3490        uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
3491        if( uz==0 ) uz = (u8*)zShowNull;
3492      }
3493      azData[nRow*nColumn + i]
3494        = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
3495      if( azNextLine[i] ){
3496        bNextLine = 1;
3497        abRowDiv[nRow-1] = 0;
3498        bMultiLineRowExists = 1;
3499      }
3500    }
3501  }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
3502  nTotal = nColumn*(nRow+1);
3503  for(i=0; i<nTotal; i++){
3504    z = azData[i];
3505    if( z==0 ) z = (char*)zEmpty;
3506    n = strlenChar(z);
3507    j = i%nColumn;
3508    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3509  }
3510  if( seenInterrupt ) goto columnar_end;
3511  if( nColumn==0 ) goto columnar_end;
3512  switch( p->cMode ){
3513    case MODE_Column: {
3514      colSep = "  ";
3515      rowSep = "\n";
3516      if( p->showHeader ){
3517        for(i=0; i<nColumn; i++){
3518          w = p->actualWidth[i];
3519          if( p->colWidth[i]<0 ) w = -w;
3520          utf8_width_print(p->out, w, azData[i]);
3521          fputs(i==nColumn-1?"\n":"  ", p->out);
3522        }
3523        for(i=0; i<nColumn; i++){
3524          print_dashes(p->out, p->actualWidth[i]);
3525          fputs(i==nColumn-1?"\n":"  ", p->out);
3526        }
3527      }
3528      break;
3529    }
3530    case MODE_Table: {
3531      colSep = " | ";
3532      rowSep = " |\n";
3533      print_row_separator(p, nColumn, "+");
3534      fputs("| ", p->out);
3535      for(i=0; i<nColumn; i++){
3536        w = p->actualWidth[i];
3537        n = strlenChar(azData[i]);
3538        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3539        fputs(i==nColumn-1?" |\n":" | ", p->out);
3540      }
3541      print_row_separator(p, nColumn, "+");
3542      break;
3543    }
3544    case MODE_Markdown: {
3545      colSep = " | ";
3546      rowSep = " |\n";
3547      fputs("| ", p->out);
3548      for(i=0; i<nColumn; i++){
3549        w = p->actualWidth[i];
3550        n = strlenChar(azData[i]);
3551        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3552        fputs(i==nColumn-1?" |\n":" | ", p->out);
3553      }
3554      print_row_separator(p, nColumn, "|");
3555      break;
3556    }
3557    case MODE_Box: {
3558      colSep = " " BOX_13 " ";
3559      rowSep = " " BOX_13 "\n";
3560      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3561      utf8_printf(p->out, BOX_13 " ");
3562      for(i=0; i<nColumn; i++){
3563        w = p->actualWidth[i];
3564        n = strlenChar(azData[i]);
3565        utf8_printf(p->out, "%*s%s%*s%s",
3566            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3567            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3568      }
3569      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3570      break;
3571    }
3572  }
3573  for(i=nColumn, j=0; i<nTotal; i++, j++){
3574    if( j==0 && p->cMode!=MODE_Column ){
3575      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3576    }
3577    z = azData[i];
3578    if( z==0 ) z = p->nullValue;
3579    w = p->actualWidth[j];
3580    if( p->colWidth[j]<0 ) w = -w;
3581    utf8_width_print(p->out, w, z);
3582    if( j==nColumn-1 ){
3583      utf8_printf(p->out, "%s", rowSep);
3584      if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
3585        if( p->cMode==MODE_Table ){
3586          print_row_separator(p, nColumn, "+");
3587        }else if( p->cMode==MODE_Box ){
3588          print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3589        }else if( p->cMode==MODE_Column ){
3590          raw_printf(p->out, "\n");
3591        }
3592      }
3593      j = -1;
3594      if( seenInterrupt ) goto columnar_end;
3595    }else{
3596      utf8_printf(p->out, "%s", colSep);
3597    }
3598  }
3599  if( p->cMode==MODE_Table ){
3600    print_row_separator(p, nColumn, "+");
3601  }else if( p->cMode==MODE_Box ){
3602    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3603  }
3604columnar_end:
3605  if( seenInterrupt ){
3606    utf8_printf(p->out, "Interrupt\n");
3607  }
3608  nData = (nRow+1)*nColumn;
3609  for(i=0; i<nData; i++){
3610    z = azData[i];
3611    if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
3612  }
3613  sqlite3_free(azData);
3614  sqlite3_free((void*)azNextLine);
3615  sqlite3_free(abRowDiv);
3616  if( azQuoted ){
3617    for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
3618    sqlite3_free(azQuoted);
3619  }
3620}
3621
3622/*
3623** Run a prepared statement
3624*/
3625static void exec_prepared_stmt(
3626  ShellState *pArg,                                /* Pointer to ShellState */
3627  sqlite3_stmt *pStmt                              /* Statment to run */
3628){
3629  int rc;
3630  sqlite3_uint64 nRow = 0;
3631
3632  if( pArg->cMode==MODE_Column
3633   || pArg->cMode==MODE_Table
3634   || pArg->cMode==MODE_Box
3635   || pArg->cMode==MODE_Markdown
3636  ){
3637    exec_prepared_stmt_columnar(pArg, pStmt);
3638    return;
3639  }
3640
3641  /* perform the first step.  this will tell us if we
3642  ** have a result set or not and how wide it is.
3643  */
3644  rc = sqlite3_step(pStmt);
3645  /* if we have a result set... */
3646  if( SQLITE_ROW == rc ){
3647    /* allocate space for col name ptr, value ptr, and type */
3648    int nCol = sqlite3_column_count(pStmt);
3649    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3650    if( !pData ){
3651      shell_out_of_memory();
3652    }else{
3653      char **azCols = (char **)pData;      /* Names of result columns */
3654      char **azVals = &azCols[nCol];       /* Results */
3655      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3656      int i, x;
3657      assert(sizeof(int) <= sizeof(char *));
3658      /* save off ptrs to column names */
3659      for(i=0; i<nCol; i++){
3660        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3661      }
3662      do{
3663        nRow++;
3664        /* extract the data and data types */
3665        for(i=0; i<nCol; i++){
3666          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3667          if( x==SQLITE_BLOB
3668           && pArg
3669           && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
3670          ){
3671            azVals[i] = "";
3672          }else{
3673            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3674          }
3675          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3676            rc = SQLITE_NOMEM;
3677            break; /* from for */
3678          }
3679        } /* end for */
3680
3681        /* if data and types extracted successfully... */
3682        if( SQLITE_ROW == rc ){
3683          /* call the supplied callback with the result row data */
3684          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3685            rc = SQLITE_ABORT;
3686          }else{
3687            rc = sqlite3_step(pStmt);
3688          }
3689        }
3690      } while( SQLITE_ROW == rc );
3691      sqlite3_free(pData);
3692      if( pArg->cMode==MODE_Json ){
3693        fputs("]\n", pArg->out);
3694      }else if( pArg->cMode==MODE_Count ){
3695        char zBuf[200];
3696        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
3697                         nRow, nRow!=1 ? "s" : "");
3698        printf("%s", zBuf);
3699      }
3700    }
3701  }
3702}
3703
3704#ifndef SQLITE_OMIT_VIRTUALTABLE
3705/*
3706** This function is called to process SQL if the previous shell command
3707** was ".expert". It passes the SQL in the second argument directly to
3708** the sqlite3expert object.
3709**
3710** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3711** code. In this case, (*pzErr) may be set to point to a buffer containing
3712** an English language error message. It is the responsibility of the
3713** caller to eventually free this buffer using sqlite3_free().
3714*/
3715static int expertHandleSQL(
3716  ShellState *pState,
3717  const char *zSql,
3718  char **pzErr
3719){
3720  assert( pState->expert.pExpert );
3721  assert( pzErr==0 || *pzErr==0 );
3722  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3723}
3724
3725/*
3726** This function is called either to silently clean up the object
3727** created by the ".expert" command (if bCancel==1), or to generate a
3728** report from it and then clean it up (if bCancel==0).
3729**
3730** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3731** code. In this case, (*pzErr) may be set to point to a buffer containing
3732** an English language error message. It is the responsibility of the
3733** caller to eventually free this buffer using sqlite3_free().
3734*/
3735static int expertFinish(
3736  ShellState *pState,
3737  int bCancel,
3738  char **pzErr
3739){
3740  int rc = SQLITE_OK;
3741  sqlite3expert *p = pState->expert.pExpert;
3742  assert( p );
3743  assert( bCancel || pzErr==0 || *pzErr==0 );
3744  if( bCancel==0 ){
3745    FILE *out = pState->out;
3746    int bVerbose = pState->expert.bVerbose;
3747
3748    rc = sqlite3_expert_analyze(p, pzErr);
3749    if( rc==SQLITE_OK ){
3750      int nQuery = sqlite3_expert_count(p);
3751      int i;
3752
3753      if( bVerbose ){
3754        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3755        raw_printf(out, "-- Candidates -----------------------------\n");
3756        raw_printf(out, "%s\n", zCand);
3757      }
3758      for(i=0; i<nQuery; i++){
3759        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3760        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3761        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3762        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3763        if( bVerbose ){
3764          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3765          raw_printf(out, "%s\n\n", zSql);
3766        }
3767        raw_printf(out, "%s\n", zIdx);
3768        raw_printf(out, "%s\n", zEQP);
3769      }
3770    }
3771  }
3772  sqlite3_expert_destroy(p);
3773  pState->expert.pExpert = 0;
3774  return rc;
3775}
3776
3777/*
3778** Implementation of ".expert" dot command.
3779*/
3780static int expertDotCommand(
3781  ShellState *pState,             /* Current shell tool state */
3782  char **azArg,                   /* Array of arguments passed to dot command */
3783  int nArg                        /* Number of entries in azArg[] */
3784){
3785  int rc = SQLITE_OK;
3786  char *zErr = 0;
3787  int i;
3788  int iSample = 0;
3789
3790  assert( pState->expert.pExpert==0 );
3791  memset(&pState->expert, 0, sizeof(ExpertInfo));
3792
3793  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3794    char *z = azArg[i];
3795    int n;
3796    if( z[0]=='-' && z[1]=='-' ) z++;
3797    n = strlen30(z);
3798    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3799      pState->expert.bVerbose = 1;
3800    }
3801    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3802      if( i==(nArg-1) ){
3803        raw_printf(stderr, "option requires an argument: %s\n", z);
3804        rc = SQLITE_ERROR;
3805      }else{
3806        iSample = (int)integerValue(azArg[++i]);
3807        if( iSample<0 || iSample>100 ){
3808          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3809          rc = SQLITE_ERROR;
3810        }
3811      }
3812    }
3813    else{
3814      raw_printf(stderr, "unknown option: %s\n", z);
3815      rc = SQLITE_ERROR;
3816    }
3817  }
3818
3819  if( rc==SQLITE_OK ){
3820    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3821    if( pState->expert.pExpert==0 ){
3822      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
3823      rc = SQLITE_ERROR;
3824    }else{
3825      sqlite3_expert_config(
3826          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3827      );
3828    }
3829  }
3830  sqlite3_free(zErr);
3831
3832  return rc;
3833}
3834#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3835
3836/*
3837** Execute a statement or set of statements.  Print
3838** any result rows/columns depending on the current mode
3839** set via the supplied callback.
3840**
3841** This is very similar to SQLite's built-in sqlite3_exec()
3842** function except it takes a slightly different callback
3843** and callback data argument.
3844*/
3845static int shell_exec(
3846  ShellState *pArg,                         /* Pointer to ShellState */
3847  const char *zSql,                         /* SQL to be evaluated */
3848  char **pzErrMsg                           /* Error msg written here */
3849){
3850  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3851  int rc = SQLITE_OK;             /* Return Code */
3852  int rc2;
3853  const char *zLeftover;          /* Tail of unprocessed SQL */
3854  sqlite3 *db = pArg->db;
3855
3856  if( pzErrMsg ){
3857    *pzErrMsg = NULL;
3858  }
3859
3860#ifndef SQLITE_OMIT_VIRTUALTABLE
3861  if( pArg->expert.pExpert ){
3862    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3863    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3864  }
3865#endif
3866
3867  while( zSql[0] && (SQLITE_OK == rc) ){
3868    static const char *zStmtSql;
3869    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3870    if( SQLITE_OK != rc ){
3871      if( pzErrMsg ){
3872        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
3873      }
3874    }else{
3875      if( !pStmt ){
3876        /* this happens for a comment or white-space */
3877        zSql = zLeftover;
3878        while( IsSpace(zSql[0]) ) zSql++;
3879        continue;
3880      }
3881      zStmtSql = sqlite3_sql(pStmt);
3882      if( zStmtSql==0 ) zStmtSql = "";
3883      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3884
3885      /* save off the prepared statment handle and reset row count */
3886      if( pArg ){
3887        pArg->pStmt = pStmt;
3888        pArg->cnt = 0;
3889      }
3890
3891      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3892      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3893        sqlite3_stmt *pExplain;
3894        char *zEQP;
3895        int triggerEQP = 0;
3896        disable_debug_trace_modes();
3897        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3898        if( pArg->autoEQP>=AUTOEQP_trigger ){
3899          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3900        }
3901        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3902        shell_check_oom(zEQP);
3903        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3904        if( rc==SQLITE_OK ){
3905          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3906            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3907            int iEqpId = sqlite3_column_int(pExplain, 0);
3908            int iParentId = sqlite3_column_int(pExplain, 1);
3909            if( zEQPLine==0 ) zEQPLine = "";
3910            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3911            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3912          }
3913          eqp_render(pArg);
3914        }
3915        sqlite3_finalize(pExplain);
3916        sqlite3_free(zEQP);
3917        if( pArg->autoEQP>=AUTOEQP_full ){
3918          /* Also do an EXPLAIN for ".eqp full" mode */
3919          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3920          shell_check_oom(zEQP);
3921          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3922          if( rc==SQLITE_OK ){
3923            pArg->cMode = MODE_Explain;
3924            explain_data_prepare(pArg, pExplain);
3925            exec_prepared_stmt(pArg, pExplain);
3926            explain_data_delete(pArg);
3927          }
3928          sqlite3_finalize(pExplain);
3929          sqlite3_free(zEQP);
3930        }
3931        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3932          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3933          /* Reprepare pStmt before reactiving trace modes */
3934          sqlite3_finalize(pStmt);
3935          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3936          if( pArg ) pArg->pStmt = pStmt;
3937        }
3938        restore_debug_trace_modes();
3939      }
3940
3941      if( pArg ){
3942        pArg->cMode = pArg->mode;
3943        if( pArg->autoExplain ){
3944          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3945            pArg->cMode = MODE_Explain;
3946          }
3947          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3948            pArg->cMode = MODE_EQP;
3949          }
3950        }
3951
3952        /* If the shell is currently in ".explain" mode, gather the extra
3953        ** data required to add indents to the output.*/
3954        if( pArg->cMode==MODE_Explain ){
3955          explain_data_prepare(pArg, pStmt);
3956        }
3957      }
3958
3959      bind_prepared_stmt(pArg, pStmt);
3960      exec_prepared_stmt(pArg, pStmt);
3961      explain_data_delete(pArg);
3962      eqp_render(pArg);
3963
3964      /* print usage stats if stats on */
3965      if( pArg && pArg->statsOn ){
3966        display_stats(db, pArg, 0);
3967      }
3968
3969      /* print loop-counters if required */
3970      if( pArg && pArg->scanstatsOn ){
3971        display_scanstats(db, pArg);
3972      }
3973
3974      /* Finalize the statement just executed. If this fails, save a
3975      ** copy of the error message. Otherwise, set zSql to point to the
3976      ** next statement to execute. */
3977      rc2 = sqlite3_finalize(pStmt);
3978      if( rc!=SQLITE_NOMEM ) rc = rc2;
3979      if( rc==SQLITE_OK ){
3980        zSql = zLeftover;
3981        while( IsSpace(zSql[0]) ) zSql++;
3982      }else if( pzErrMsg ){
3983        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
3984      }
3985
3986      /* clear saved stmt handle */
3987      if( pArg ){
3988        pArg->pStmt = NULL;
3989      }
3990    }
3991  } /* end while */
3992
3993  return rc;
3994}
3995
3996/*
3997** Release memory previously allocated by tableColumnList().
3998*/
3999static void freeColumnList(char **azCol){
4000  int i;
4001  for(i=1; azCol[i]; i++){
4002    sqlite3_free(azCol[i]);
4003  }
4004  /* azCol[0] is a static string */
4005  sqlite3_free(azCol);
4006}
4007
4008/*
4009** Return a list of pointers to strings which are the names of all
4010** columns in table zTab.   The memory to hold the names is dynamically
4011** allocated and must be released by the caller using a subsequent call
4012** to freeColumnList().
4013**
4014** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
4015** value that needs to be preserved, then azCol[0] is filled in with the
4016** name of the rowid column.
4017**
4018** The first regular column in the table is azCol[1].  The list is terminated
4019** by an entry with azCol[i]==0.
4020*/
4021static char **tableColumnList(ShellState *p, const char *zTab){
4022  char **azCol = 0;
4023  sqlite3_stmt *pStmt;
4024  char *zSql;
4025  int nCol = 0;
4026  int nAlloc = 0;
4027  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
4028  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
4029  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4030  int rc;
4031
4032  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4033  shell_check_oom(zSql);
4034  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4035  sqlite3_free(zSql);
4036  if( rc ) return 0;
4037  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4038    if( nCol>=nAlloc-2 ){
4039      nAlloc = nAlloc*2 + nCol + 10;
4040      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4041      shell_check_oom(azCol);
4042    }
4043    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4044    shell_check_oom(azCol[nCol]);
4045    if( sqlite3_column_int(pStmt, 5) ){
4046      nPK++;
4047      if( nPK==1
4048       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4049                          "INTEGER")==0
4050      ){
4051        isIPK = 1;
4052      }else{
4053        isIPK = 0;
4054      }
4055    }
4056  }
4057  sqlite3_finalize(pStmt);
4058  if( azCol==0 ) return 0;
4059  azCol[0] = 0;
4060  azCol[nCol+1] = 0;
4061
4062  /* The decision of whether or not a rowid really needs to be preserved
4063  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
4064  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
4065  ** rowids on tables where the rowid is inaccessible because there are other
4066  ** columns in the table named "rowid", "_rowid_", and "oid".
4067  */
4068  if( preserveRowid && isIPK ){
4069    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4070    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
4071    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4072    ** ROWID aliases.  To distinguish these cases, check to see if
4073    ** there is a "pk" entry in "PRAGMA index_list".  There will be
4074    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4075    */
4076    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4077                           " WHERE origin='pk'", zTab);
4078    shell_check_oom(zSql);
4079    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4080    sqlite3_free(zSql);
4081    if( rc ){
4082      freeColumnList(azCol);
4083      return 0;
4084    }
4085    rc = sqlite3_step(pStmt);
4086    sqlite3_finalize(pStmt);
4087    preserveRowid = rc==SQLITE_ROW;
4088  }
4089  if( preserveRowid ){
4090    /* Only preserve the rowid if we can find a name to use for the
4091    ** rowid */
4092    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4093    int i, j;
4094    for(j=0; j<3; j++){
4095      for(i=1; i<=nCol; i++){
4096        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4097      }
4098      if( i>nCol ){
4099        /* At this point, we know that azRowid[j] is not the name of any
4100        ** ordinary column in the table.  Verify that azRowid[j] is a valid
4101        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
4102        ** tables will fail this last check */
4103        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4104        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4105        break;
4106      }
4107    }
4108  }
4109  return azCol;
4110}
4111
4112/*
4113** Toggle the reverse_unordered_selects setting.
4114*/
4115static void toggleSelectOrder(sqlite3 *db){
4116  sqlite3_stmt *pStmt = 0;
4117  int iSetting = 0;
4118  char zStmt[100];
4119  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4120  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4121    iSetting = sqlite3_column_int(pStmt, 0);
4122  }
4123  sqlite3_finalize(pStmt);
4124  sqlite3_snprintf(sizeof(zStmt), zStmt,
4125       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4126  sqlite3_exec(db, zStmt, 0, 0, 0);
4127}
4128
4129/*
4130** This is a different callback routine used for dumping the database.
4131** Each row received by this callback consists of a table name,
4132** the table type ("index" or "table") and SQL to create the table.
4133** This routine should print text sufficient to recreate the table.
4134*/
4135static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4136  int rc;
4137  const char *zTable;
4138  const char *zType;
4139  const char *zSql;
4140  ShellState *p = (ShellState *)pArg;
4141  int dataOnly;
4142  int noSys;
4143
4144  UNUSED_PARAMETER(azNotUsed);
4145  if( nArg!=3 || azArg==0 ) return 0;
4146  zTable = azArg[0];
4147  zType = azArg[1];
4148  zSql = azArg[2];
4149  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4150  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4151
4152  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4153    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4154  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4155    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4156  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
4157    return 0;
4158  }else if( dataOnly ){
4159    /* no-op */
4160  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4161    char *zIns;
4162    if( !p->writableSchema ){
4163      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4164      p->writableSchema = 1;
4165    }
4166    zIns = sqlite3_mprintf(
4167       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4168       "VALUES('table','%q','%q',0,'%q');",
4169       zTable, zTable, zSql);
4170    shell_check_oom(zIns);
4171    utf8_printf(p->out, "%s\n", zIns);
4172    sqlite3_free(zIns);
4173    return 0;
4174  }else{
4175    printSchemaLine(p->out, zSql, ";\n");
4176  }
4177
4178  if( strcmp(zType, "table")==0 ){
4179    ShellText sSelect;
4180    ShellText sTable;
4181    char **azCol;
4182    int i;
4183    char *savedDestTable;
4184    int savedMode;
4185
4186    azCol = tableColumnList(p, zTable);
4187    if( azCol==0 ){
4188      p->nErr++;
4189      return 0;
4190    }
4191
4192    /* Always quote the table name, even if it appears to be pure ascii,
4193    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
4194    initText(&sTable);
4195    appendText(&sTable, zTable, quoteChar(zTable));
4196    /* If preserving the rowid, add a column list after the table name.
4197    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4198    ** instead of the usual "INSERT INTO tab VALUES(...)".
4199    */
4200    if( azCol[0] ){
4201      appendText(&sTable, "(", 0);
4202      appendText(&sTable, azCol[0], 0);
4203      for(i=1; azCol[i]; i++){
4204        appendText(&sTable, ",", 0);
4205        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4206      }
4207      appendText(&sTable, ")", 0);
4208    }
4209
4210    /* Build an appropriate SELECT statement */
4211    initText(&sSelect);
4212    appendText(&sSelect, "SELECT ", 0);
4213    if( azCol[0] ){
4214      appendText(&sSelect, azCol[0], 0);
4215      appendText(&sSelect, ",", 0);
4216    }
4217    for(i=1; azCol[i]; i++){
4218      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4219      if( azCol[i+1] ){
4220        appendText(&sSelect, ",", 0);
4221      }
4222    }
4223    freeColumnList(azCol);
4224    appendText(&sSelect, " FROM ", 0);
4225    appendText(&sSelect, zTable, quoteChar(zTable));
4226
4227    savedDestTable = p->zDestTable;
4228    savedMode = p->mode;
4229    p->zDestTable = sTable.z;
4230    p->mode = p->cMode = MODE_Insert;
4231    rc = shell_exec(p, sSelect.z, 0);
4232    if( (rc&0xff)==SQLITE_CORRUPT ){
4233      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4234      toggleSelectOrder(p->db);
4235      shell_exec(p, sSelect.z, 0);
4236      toggleSelectOrder(p->db);
4237    }
4238    p->zDestTable = savedDestTable;
4239    p->mode = savedMode;
4240    freeText(&sTable);
4241    freeText(&sSelect);
4242    if( rc ) p->nErr++;
4243  }
4244  return 0;
4245}
4246
4247/*
4248** Run zQuery.  Use dump_callback() as the callback routine so that
4249** the contents of the query are output as SQL statements.
4250**
4251** If we get a SQLITE_CORRUPT error, rerun the query after appending
4252** "ORDER BY rowid DESC" to the end.
4253*/
4254static int run_schema_dump_query(
4255  ShellState *p,
4256  const char *zQuery
4257){
4258  int rc;
4259  char *zErr = 0;
4260  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4261  if( rc==SQLITE_CORRUPT ){
4262    char *zQ2;
4263    int len = strlen30(zQuery);
4264    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4265    if( zErr ){
4266      utf8_printf(p->out, "/****** %s ******/\n", zErr);
4267      sqlite3_free(zErr);
4268      zErr = 0;
4269    }
4270    zQ2 = malloc( len+100 );
4271    if( zQ2==0 ) return rc;
4272    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4273    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4274    if( rc ){
4275      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4276    }else{
4277      rc = SQLITE_CORRUPT;
4278    }
4279    sqlite3_free(zErr);
4280    free(zQ2);
4281  }
4282  return rc;
4283}
4284
4285/*
4286** Text of help messages.
4287**
4288** The help text for each individual command begins with a line that starts
4289** with ".".  Subsequent lines are supplemental information.
4290**
4291** There must be two or more spaces between the end of the command and the
4292** start of the description of what that command does.
4293*/
4294static const char *(azHelp[]) = {
4295#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4296  && !defined(SQLITE_SHELL_FIDDLE)
4297  ".archive ...             Manage SQL archives",
4298  "   Each command must have exactly one of the following options:",
4299  "     -c, --create               Create a new archive",
4300  "     -u, --update               Add or update files with changed mtime",
4301  "     -i, --insert               Like -u but always add even if unchanged",
4302  "     -r, --remove               Remove files from archive",
4303  "     -t, --list                 List contents of archive",
4304  "     -x, --extract              Extract files from archive",
4305  "   Optional arguments:",
4306  "     -v, --verbose              Print each filename as it is processed",
4307  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
4308  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
4309  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
4310  "     -g, --glob                 Use glob matching for names in archive",
4311  "     -n, --dryrun               Show the SQL that would have occurred",
4312  "   Examples:",
4313  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
4314  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
4315  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
4316  "   See also:",
4317  "      http://sqlite.org/cli.html#sqlite_archive_support",
4318#endif
4319#ifndef SQLITE_OMIT_AUTHORIZATION
4320  ".auth ON|OFF             Show authorizer callbacks",
4321#endif
4322#ifndef SQLITE_SHELL_FIDDLE
4323  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
4324  "   Options:",
4325  "       --append            Use the appendvfs",
4326  "       --async             Write to FILE without journal and fsync()",
4327#endif
4328  ".bail on|off             Stop after hitting an error.  Default OFF",
4329  ".binary on|off           Turn binary output on or off.  Default OFF",
4330#ifndef SQLITE_SHELL_FIDDLE
4331  ".cd DIRECTORY            Change the working directory to DIRECTORY",
4332#endif
4333  ".changes on|off          Show number of rows changed by SQL",
4334#ifndef SQLITE_SHELL_FIDDLE
4335  ".check GLOB              Fail if output since .testcase does not match",
4336  ".clone NEWDB             Clone data into NEWDB from the existing database",
4337#endif
4338  ".connection [close] [#]  Open or close an auxiliary database connection",
4339  ".databases               List names and files of attached databases",
4340  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4341#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4342  ".dbinfo ?DB?             Show status information about the database",
4343#endif
4344  ".dump ?OBJECTS?          Render database content as SQL",
4345  "   Options:",
4346  "     --data-only            Output only INSERT statements",
4347  "     --newlines             Allow unescaped newline characters in output",
4348  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4349  "     --preserve-rowids      Include ROWID values in the output",
4350  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4351  "   Additional LIKE patterns can be given in subsequent arguments",
4352  ".echo on|off             Turn command echo on or off",
4353  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4354  "   Other Modes:",
4355#ifdef SQLITE_DEBUG
4356  "      test                  Show raw EXPLAIN QUERY PLAN output",
4357  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4358#endif
4359  "      trigger               Like \"full\" but also show trigger bytecode",
4360#ifndef SQLITE_SHELL_FIDDLE
4361  ".excel                   Display the output of next command in spreadsheet",
4362  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4363#endif
4364#ifndef SQLITE_SHELL_FIDDLE
4365  ".exit ?CODE?             Exit this program with return-code CODE",
4366#endif
4367  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4368  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4369  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4370  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4371  "   --help                  Show CMD details",
4372  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4373  ".headers on|off          Turn display of headers on or off",
4374  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4375#ifndef SQLITE_SHELL_FIDDLE
4376  ".import FILE TABLE       Import data from FILE into TABLE",
4377  "   Options:",
4378  "     --ascii               Use \\037 and \\036 as column and row separators",
4379  "     --csv                 Use , and \\n as column and row separators",
4380  "     --skip N              Skip the first N rows of input",
4381  "     --schema S            Target table to be S.TABLE",
4382  "     -v                    \"Verbose\" - increase auxiliary output",
4383  "   Notes:",
4384  "     *  If TABLE does not exist, it is created.  The first row of input",
4385  "        determines the column names.",
4386  "     *  If neither --csv or --ascii are used, the input mode is derived",
4387  "        from the \".mode\" output mode",
4388  "     *  If FILE begins with \"|\" then it is a command that generates the",
4389  "        input text.",
4390#endif
4391#ifndef SQLITE_OMIT_TEST_CONTROL
4392  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4393#endif
4394  ".indexes ?TABLE?         Show names of indexes",
4395  "                           If TABLE is specified, only show indexes for",
4396  "                           tables matching TABLE using the LIKE operator.",
4397#ifdef SQLITE_ENABLE_IOTRACE
4398  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4399#endif
4400  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4401  ".lint OPTIONS            Report potential schema issues.",
4402  "     Options:",
4403  "        fkey-indexes     Find missing foreign key indexes",
4404#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4405  ".load FILE ?ENTRY?       Load an extension library",
4406#endif
4407#ifndef SQLITE_SHELL_FIDDLE
4408  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4409#endif
4410  ".mode MODE ?OPTIONS?     Set output mode",
4411  "   MODE is one of:",
4412  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
4413  "     box         Tables using unicode box-drawing characters",
4414  "     csv         Comma-separated values",
4415  "     column      Output in columns.  (See .width)",
4416  "     html        HTML <table> code",
4417  "     insert      SQL insert statements for TABLE",
4418  "     json        Results in a JSON array",
4419  "     line        One value per line",
4420  "     list        Values delimited by \"|\"",
4421  "     markdown    Markdown table format",
4422  "     qbox        Shorthand for \"box --width 60 --quote\"",
4423  "     quote       Escape answers as for SQL",
4424  "     table       ASCII-art table",
4425  "     tabs        Tab-separated values",
4426  "     tcl         TCL list elements",
4427  "   OPTIONS: (for columnar modes or insert mode):",
4428  "     --wrap N       Wrap output lines to no longer than N characters",
4429  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
4430  "     --ww           Shorthand for \"--wordwrap 1\"",
4431  "     --quote        Quote output text as SQL literals",
4432  "     --noquote      Do not quote output text",
4433  "     TABLE          The name of SQL table used for \"insert\" mode",
4434#ifndef SQLITE_SHELL_FIDDLE
4435  ".nonce STRING            Suspend safe mode for one command if nonce matches",
4436#endif
4437  ".nullvalue STRING        Use STRING in place of NULL values",
4438#ifndef SQLITE_SHELL_FIDDLE
4439  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4440  "     If FILE begins with '|' then open as a pipe",
4441  "       --bom  Put a UTF8 byte-order mark at the beginning",
4442  "       -e     Send output to the system text editor",
4443  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4444  /* Note that .open is (partially) available in WASM builds but is
4445  ** currently only intended to be used by the fiddle tool, not
4446  ** end users, so is "undocumented." */
4447  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4448  "     Options:",
4449  "        --append        Use appendvfs to append database to the end of FILE",
4450#endif
4451#ifndef SQLITE_OMIT_DESERIALIZE
4452  "        --deserialize   Load into memory using sqlite3_deserialize()",
4453  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4454  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4455#endif
4456  "        --new           Initialize FILE to an empty database",
4457  "        --nofollow      Do not follow symbolic links",
4458  "        --readonly      Open FILE readonly",
4459  "        --zip           FILE is a ZIP archive",
4460#ifndef SQLITE_SHELL_FIDDLE
4461  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4462  "   If FILE begins with '|' then open it as a pipe.",
4463  "   Options:",
4464  "     --bom                 Prefix output with a UTF8 byte-order mark",
4465  "     -e                    Send output to the system text editor",
4466  "     -x                    Send output as CSV to a spreadsheet",
4467#endif
4468  ".parameter CMD ...       Manage SQL parameter bindings",
4469  "   clear                   Erase all bindings",
4470  "   init                    Initialize the TEMP table that holds bindings",
4471  "   list                    List the current parameter bindings",
4472  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4473  "                           PARAMETER should start with one of: $ : @ ?",
4474  "   unset PARAMETER         Remove PARAMETER from the binding table",
4475  ".print STRING...         Print literal STRING",
4476#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4477  ".progress N              Invoke progress handler after every N opcodes",
4478  "   --limit N                 Interrupt after N progress callbacks",
4479  "   --once                    Do no more than one progress interrupt",
4480  "   --quiet|-q                No output except at interrupts",
4481  "   --reset                   Reset the count for each input and interrupt",
4482#endif
4483  ".prompt MAIN CONTINUE    Replace the standard prompts",
4484#ifndef SQLITE_SHELL_FIDDLE
4485  ".quit                    Exit this program",
4486  ".read FILE               Read input from FILE or command output",
4487  "    If FILE begins with \"|\", it is a command that generates the input.",
4488#endif
4489#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4490  ".recover                 Recover as much data as possible from corrupt db.",
4491  "   --freelist-corrupt       Assume the freelist is corrupt",
4492  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4493  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4494  "   --no-rowids              Do not attempt to recover rowid values",
4495  "                            that are not also INTEGER PRIMARY KEYs",
4496#endif
4497#ifndef SQLITE_SHELL_FIDDLE
4498  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4499  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
4500#endif
4501  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4502  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4503  "   Options:",
4504  "      --indent             Try to pretty-print the schema",
4505  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4506  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4507  "    Options:",
4508  "       --init               Create a new SELFTEST table",
4509  "       -v                   Verbose output",
4510  ".separator COL ?ROW?     Change the column and row separators",
4511#if defined(SQLITE_ENABLE_SESSION)
4512  ".session ?NAME? CMD ...  Create or control sessions",
4513  "   Subcommands:",
4514  "     attach TABLE             Attach TABLE",
4515  "     changeset FILE           Write a changeset into FILE",
4516  "     close                    Close one session",
4517  "     enable ?BOOLEAN?         Set or query the enable bit",
4518  "     filter GLOB...           Reject tables matching GLOBs",
4519  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4520  "     isempty                  Query whether the session is empty",
4521  "     list                     List currently open session names",
4522  "     open DB NAME             Open a new session on DB",
4523  "     patchset FILE            Write a patchset into FILE",
4524  "   If ?NAME? is omitted, the first defined session is used.",
4525#endif
4526  ".sha3sum ...             Compute a SHA3 hash of database content",
4527  "    Options:",
4528  "      --schema              Also hash the sqlite_schema table",
4529  "      --sha3-224            Use the sha3-224 algorithm",
4530  "      --sha3-256            Use the sha3-256 algorithm (default)",
4531  "      --sha3-384            Use the sha3-384 algorithm",
4532  "      --sha3-512            Use the sha3-512 algorithm",
4533  "    Any other argument is a LIKE pattern for tables to hash",
4534#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4535  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4536#endif
4537  ".show                    Show the current values for various settings",
4538  ".stats ?ARG?             Show stats or turn stats on or off",
4539  "   off                      Turn off automatic stat display",
4540  "   on                       Turn on automatic stat display",
4541  "   stmt                     Show statement stats",
4542  "   vmstep                   Show the virtual machine step count only",
4543#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4544  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4545#endif
4546  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4547#ifndef SQLITE_SHELL_FIDDLE
4548  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4549#endif
4550  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4551  "                           Run \".testctrl\" with no arguments for details",
4552  ".timeout MS              Try opening locked tables for MS milliseconds",
4553  ".timer on|off            Turn SQL timer on or off",
4554#ifndef SQLITE_OMIT_TRACE
4555  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4556  "    FILE                    Send output to FILE",
4557  "    stdout                  Send output to stdout",
4558  "    stderr                  Send output to stderr",
4559  "    off                     Disable tracing",
4560  "    --expanded              Expand query parameters",
4561#ifdef SQLITE_ENABLE_NORMALIZE
4562  "    --normalized            Normal the SQL statements",
4563#endif
4564  "    --plain                 Show SQL as it is input",
4565  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4566  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4567  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4568  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4569#endif /* SQLITE_OMIT_TRACE */
4570#ifdef SQLITE_DEBUG
4571  ".unmodule NAME ...       Unregister virtual table modules",
4572  "    --allexcept             Unregister everything except those named",
4573#endif
4574  ".vfsinfo ?AUX?           Information about the top-level VFS",
4575  ".vfslist                 List all available VFSes",
4576  ".vfsname ?AUX?           Print the name of the VFS stack",
4577  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4578  "     Negative values right-justify",
4579};
4580
4581/*
4582** Output help text.
4583**
4584** zPattern describes the set of commands for which help text is provided.
4585** If zPattern is NULL, then show all commands, but only give a one-line
4586** description of each.
4587**
4588** Return the number of matches.
4589*/
4590static int showHelp(FILE *out, const char *zPattern){
4591  int i = 0;
4592  int j = 0;
4593  int n = 0;
4594  char *zPat;
4595  if( zPattern==0
4596   || zPattern[0]=='0'
4597   || strcmp(zPattern,"-a")==0
4598   || strcmp(zPattern,"-all")==0
4599   || strcmp(zPattern,"--all")==0
4600  ){
4601    /* Show all commands, but only one line per command */
4602    if( zPattern==0 ) zPattern = "";
4603    for(i=0; i<ArraySize(azHelp); i++){
4604      if( azHelp[i][0]=='.' || zPattern[0] ){
4605        utf8_printf(out, "%s\n", azHelp[i]);
4606        n++;
4607      }
4608    }
4609  }else{
4610    /* Look for commands that for which zPattern is an exact prefix */
4611    zPat = sqlite3_mprintf(".%s*", zPattern);
4612    shell_check_oom(zPat);
4613    for(i=0; i<ArraySize(azHelp); i++){
4614      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4615        utf8_printf(out, "%s\n", azHelp[i]);
4616        j = i+1;
4617        n++;
4618      }
4619    }
4620    sqlite3_free(zPat);
4621    if( n ){
4622      if( n==1 ){
4623        /* when zPattern is a prefix of exactly one command, then include the
4624        ** details of that command, which should begin at offset j */
4625        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4626          utf8_printf(out, "%s\n", azHelp[j]);
4627          j++;
4628        }
4629      }
4630      return n;
4631    }
4632    /* Look for commands that contain zPattern anywhere.  Show the complete
4633    ** text of all commands that match. */
4634    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4635    shell_check_oom(zPat);
4636    for(i=0; i<ArraySize(azHelp); i++){
4637      if( azHelp[i][0]=='.' ) j = i;
4638      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4639        utf8_printf(out, "%s\n", azHelp[j]);
4640        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4641          j++;
4642          utf8_printf(out, "%s\n", azHelp[j]);
4643        }
4644        i = j;
4645        n++;
4646      }
4647    }
4648    sqlite3_free(zPat);
4649  }
4650  return n;
4651}
4652
4653/* Forward reference */
4654static int process_input(ShellState *p);
4655
4656/*
4657** Read the content of file zName into memory obtained from sqlite3_malloc64()
4658** and return a pointer to the buffer. The caller is responsible for freeing
4659** the memory.
4660**
4661** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4662** read.
4663**
4664** For convenience, a nul-terminator byte is always appended to the data read
4665** from the file before the buffer is returned. This byte is not included in
4666** the final value of (*pnByte), if applicable.
4667**
4668** NULL is returned if any error is encountered. The final value of *pnByte
4669** is undefined in this case.
4670*/
4671static char *readFile(const char *zName, int *pnByte){
4672  FILE *in = fopen(zName, "rb");
4673  long nIn;
4674  size_t nRead;
4675  char *pBuf;
4676  if( in==0 ) return 0;
4677  fseek(in, 0, SEEK_END);
4678  nIn = ftell(in);
4679  rewind(in);
4680  pBuf = sqlite3_malloc64( nIn+1 );
4681  if( pBuf==0 ){ fclose(in); return 0; }
4682  nRead = fread(pBuf, nIn, 1, in);
4683  fclose(in);
4684  if( nRead!=1 ){
4685    sqlite3_free(pBuf);
4686    return 0;
4687  }
4688  pBuf[nIn] = 0;
4689  if( pnByte ) *pnByte = nIn;
4690  return pBuf;
4691}
4692
4693#if defined(SQLITE_ENABLE_SESSION)
4694/*
4695** Close a single OpenSession object and release all of its associated
4696** resources.
4697*/
4698static void session_close(OpenSession *pSession){
4699  int i;
4700  sqlite3session_delete(pSession->p);
4701  sqlite3_free(pSession->zName);
4702  for(i=0; i<pSession->nFilter; i++){
4703    sqlite3_free(pSession->azFilter[i]);
4704  }
4705  sqlite3_free(pSession->azFilter);
4706  memset(pSession, 0, sizeof(OpenSession));
4707}
4708#endif
4709
4710/*
4711** Close all OpenSession objects and release all associated resources.
4712*/
4713#if defined(SQLITE_ENABLE_SESSION)
4714static void session_close_all(ShellState *p, int i){
4715  int j;
4716  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4717  for(j=0; j<pAuxDb->nSession; j++){
4718    session_close(&pAuxDb->aSession[j]);
4719  }
4720  pAuxDb->nSession = 0;
4721}
4722#else
4723# define session_close_all(X,Y)
4724#endif
4725
4726/*
4727** Implementation of the xFilter function for an open session.  Omit
4728** any tables named by ".session filter" but let all other table through.
4729*/
4730#if defined(SQLITE_ENABLE_SESSION)
4731static int session_filter(void *pCtx, const char *zTab){
4732  OpenSession *pSession = (OpenSession*)pCtx;
4733  int i;
4734  for(i=0; i<pSession->nFilter; i++){
4735    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4736  }
4737  return 1;
4738}
4739#endif
4740
4741/*
4742** Try to deduce the type of file for zName based on its content.  Return
4743** one of the SHELL_OPEN_* constants.
4744**
4745** If the file does not exist or is empty but its name looks like a ZIP
4746** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4747** Otherwise, assume an ordinary database regardless of the filename if
4748** the type cannot be determined from content.
4749*/
4750int deduceDatabaseType(const char *zName, int dfltZip){
4751  FILE *f = fopen(zName, "rb");
4752  size_t n;
4753  int rc = SHELL_OPEN_UNSPEC;
4754  char zBuf[100];
4755  if( f==0 ){
4756    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4757       return SHELL_OPEN_ZIPFILE;
4758    }else{
4759       return SHELL_OPEN_NORMAL;
4760    }
4761  }
4762  n = fread(zBuf, 16, 1, f);
4763  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4764    fclose(f);
4765    return SHELL_OPEN_NORMAL;
4766  }
4767  fseek(f, -25, SEEK_END);
4768  n = fread(zBuf, 25, 1, f);
4769  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4770    rc = SHELL_OPEN_APPENDVFS;
4771  }else{
4772    fseek(f, -22, SEEK_END);
4773    n = fread(zBuf, 22, 1, f);
4774    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4775       && zBuf[3]==0x06 ){
4776      rc = SHELL_OPEN_ZIPFILE;
4777    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4778      rc = SHELL_OPEN_ZIPFILE;
4779    }
4780  }
4781  fclose(f);
4782  return rc;
4783}
4784
4785#ifndef SQLITE_OMIT_DESERIALIZE
4786/*
4787** Reconstruct an in-memory database using the output from the "dbtotxt"
4788** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4789** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4790*/
4791static unsigned char *readHexDb(ShellState *p, int *pnData){
4792  unsigned char *a = 0;
4793  int nLine;
4794  int n = 0;
4795  int pgsz = 0;
4796  int iOffset = 0;
4797  int j, k;
4798  int rc;
4799  FILE *in;
4800  const char *zDbFilename = p->pAuxDb->zDbFilename;
4801  unsigned int x[16];
4802  char zLine[1000];
4803  if( zDbFilename ){
4804    in = fopen(zDbFilename, "r");
4805    if( in==0 ){
4806      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4807      return 0;
4808    }
4809    nLine = 0;
4810  }else{
4811    in = p->in;
4812    nLine = p->lineno;
4813    if( in==0 ) in = stdin;
4814  }
4815  *pnData = 0;
4816  nLine++;
4817  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4818  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4819  if( rc!=2 ) goto readHexDb_error;
4820  if( n<0 ) goto readHexDb_error;
4821  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4822  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4823  a = sqlite3_malloc( n ? n : 1 );
4824  shell_check_oom(a);
4825  memset(a, 0, n);
4826  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4827    utf8_printf(stderr, "invalid pagesize\n");
4828    goto readHexDb_error;
4829  }
4830  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4831    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4832    if( rc==2 ){
4833      iOffset = k;
4834      continue;
4835    }
4836    if( strncmp(zLine, "| end ", 6)==0 ){
4837      break;
4838    }
4839    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4840                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4841                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4842    if( rc==17 ){
4843      k = iOffset+j;
4844      if( k+16<=n && k>=0 ){
4845        int ii;
4846        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4847      }
4848    }
4849  }
4850  *pnData = n;
4851  if( in!=p->in ){
4852    fclose(in);
4853  }else{
4854    p->lineno = nLine;
4855  }
4856  return a;
4857
4858readHexDb_error:
4859  if( in!=p->in ){
4860    fclose(in);
4861  }else{
4862    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4863      nLine++;
4864      if(strncmp(zLine, "| end ", 6)==0 ) break;
4865    }
4866    p->lineno = nLine;
4867  }
4868  sqlite3_free(a);
4869  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4870  return 0;
4871}
4872#endif /* SQLITE_OMIT_DESERIALIZE */
4873
4874/*
4875** Scalar function "shell_int32". The first argument to this function
4876** must be a blob. The second a non-negative integer. This function
4877** reads and returns a 32-bit big-endian integer from byte
4878** offset (4*<arg2>) of the blob.
4879*/
4880static void shellInt32(
4881  sqlite3_context *context,
4882  int argc,
4883  sqlite3_value **argv
4884){
4885  const unsigned char *pBlob;
4886  int nBlob;
4887  int iInt;
4888
4889  UNUSED_PARAMETER(argc);
4890  nBlob = sqlite3_value_bytes(argv[0]);
4891  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4892  iInt = sqlite3_value_int(argv[1]);
4893
4894  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4895    const unsigned char *a = &pBlob[iInt*4];
4896    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4897                       + ((sqlite3_int64)a[1]<<16)
4898                       + ((sqlite3_int64)a[2]<< 8)
4899                       + ((sqlite3_int64)a[3]<< 0);
4900    sqlite3_result_int64(context, iVal);
4901  }
4902}
4903
4904/*
4905** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4906** using "..." with internal double-quote characters doubled.
4907*/
4908static void shellIdQuote(
4909  sqlite3_context *context,
4910  int argc,
4911  sqlite3_value **argv
4912){
4913  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4914  UNUSED_PARAMETER(argc);
4915  if( zName ){
4916    char *z = sqlite3_mprintf("\"%w\"", zName);
4917    sqlite3_result_text(context, z, -1, sqlite3_free);
4918  }
4919}
4920
4921/*
4922** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4923*/
4924static void shellUSleepFunc(
4925  sqlite3_context *context,
4926  int argcUnused,
4927  sqlite3_value **argv
4928){
4929  int sleep = sqlite3_value_int(argv[0]);
4930  (void)argcUnused;
4931  sqlite3_sleep(sleep/1000);
4932  sqlite3_result_int(context, sleep);
4933}
4934
4935/*
4936** Scalar function "shell_escape_crnl" used by the .recover command.
4937** The argument passed to this function is the output of built-in
4938** function quote(). If the first character of the input is "'",
4939** indicating that the value passed to quote() was a text value,
4940** then this function searches the input for "\n" and "\r" characters
4941** and adds a wrapper similar to the following:
4942**
4943**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4944**
4945** Or, if the first character of the input is not "'", then a copy
4946** of the input is returned.
4947*/
4948static void shellEscapeCrnl(
4949  sqlite3_context *context,
4950  int argc,
4951  sqlite3_value **argv
4952){
4953  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4954  UNUSED_PARAMETER(argc);
4955  if( zText && zText[0]=='\'' ){
4956    int nText = sqlite3_value_bytes(argv[0]);
4957    int i;
4958    char zBuf1[20];
4959    char zBuf2[20];
4960    const char *zNL = 0;
4961    const char *zCR = 0;
4962    int nCR = 0;
4963    int nNL = 0;
4964
4965    for(i=0; zText[i]; i++){
4966      if( zNL==0 && zText[i]=='\n' ){
4967        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4968        nNL = (int)strlen(zNL);
4969      }
4970      if( zCR==0 && zText[i]=='\r' ){
4971        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4972        nCR = (int)strlen(zCR);
4973      }
4974    }
4975
4976    if( zNL || zCR ){
4977      int iOut = 0;
4978      i64 nMax = (nNL > nCR) ? nNL : nCR;
4979      i64 nAlloc = nMax * nText + (nMax+64)*2;
4980      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4981      if( zOut==0 ){
4982        sqlite3_result_error_nomem(context);
4983        return;
4984      }
4985
4986      if( zNL && zCR ){
4987        memcpy(&zOut[iOut], "replace(replace(", 16);
4988        iOut += 16;
4989      }else{
4990        memcpy(&zOut[iOut], "replace(", 8);
4991        iOut += 8;
4992      }
4993      for(i=0; zText[i]; i++){
4994        if( zText[i]=='\n' ){
4995          memcpy(&zOut[iOut], zNL, nNL);
4996          iOut += nNL;
4997        }else if( zText[i]=='\r' ){
4998          memcpy(&zOut[iOut], zCR, nCR);
4999          iOut += nCR;
5000        }else{
5001          zOut[iOut] = zText[i];
5002          iOut++;
5003        }
5004      }
5005
5006      if( zNL ){
5007        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5008        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
5009        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
5010      }
5011      if( zCR ){
5012        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5013        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
5014        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
5015      }
5016
5017      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
5018      sqlite3_free(zOut);
5019      return;
5020    }
5021  }
5022
5023  sqlite3_result_value(context, argv[0]);
5024}
5025
5026/* Flags for open_db().
5027**
5028** The default behavior of open_db() is to exit(1) if the database fails to
5029** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5030** but still returns without calling exit.
5031**
5032** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5033** ZIP archive if the file does not exist or is empty and its name matches
5034** the *.zip pattern.
5035*/
5036#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
5037#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
5038
5039/*
5040** Make sure the database is open.  If it is not, then open it.  If
5041** the database fails to open, print an error message and exit.
5042*/
5043static void open_db(ShellState *p, int openFlags){
5044  if( p->db==0 ){
5045    const char *zDbFilename = p->pAuxDb->zDbFilename;
5046    if( p->openMode==SHELL_OPEN_UNSPEC ){
5047      if( zDbFilename==0 || zDbFilename[0]==0 ){
5048        p->openMode = SHELL_OPEN_NORMAL;
5049      }else{
5050        p->openMode = (u8)deduceDatabaseType(zDbFilename,
5051                             (openFlags & OPEN_DB_ZIPFILE)!=0);
5052      }
5053    }
5054    switch( p->openMode ){
5055      case SHELL_OPEN_APPENDVFS: {
5056        sqlite3_open_v2(zDbFilename, &p->db,
5057           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5058        break;
5059      }
5060      case SHELL_OPEN_HEXDB:
5061      case SHELL_OPEN_DESERIALIZE: {
5062        sqlite3_open(0, &p->db);
5063        break;
5064      }
5065      case SHELL_OPEN_ZIPFILE: {
5066        sqlite3_open(":memory:", &p->db);
5067        break;
5068      }
5069      case SHELL_OPEN_READONLY: {
5070        sqlite3_open_v2(zDbFilename, &p->db,
5071            SQLITE_OPEN_READONLY|p->openFlags, 0);
5072        break;
5073      }
5074      case SHELL_OPEN_UNSPEC:
5075      case SHELL_OPEN_NORMAL: {
5076        sqlite3_open_v2(zDbFilename, &p->db,
5077           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5078        break;
5079      }
5080    }
5081    globalDb = p->db;
5082    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5083      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5084          zDbFilename, sqlite3_errmsg(p->db));
5085      if( openFlags & OPEN_DB_KEEPALIVE ){
5086        sqlite3_open(":memory:", &p->db);
5087        return;
5088      }
5089      exit(1);
5090    }
5091#ifndef SQLITE_OMIT_LOAD_EXTENSION
5092    sqlite3_enable_load_extension(p->db, 1);
5093#endif
5094    sqlite3_shathree_init(p->db, 0, 0);
5095    sqlite3_uint_init(p->db, 0, 0);
5096    sqlite3_decimal_init(p->db, 0, 0);
5097    sqlite3_regexp_init(p->db, 0, 0);
5098    sqlite3_ieee_init(p->db, 0, 0);
5099    sqlite3_series_init(p->db, 0, 0);
5100#ifndef SQLITE_SHELL_FIDDLE
5101    sqlite3_fileio_init(p->db, 0, 0);
5102    sqlite3_completion_init(p->db, 0, 0);
5103#endif
5104#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5105    sqlite3_dbdata_init(p->db, 0, 0);
5106#endif
5107#ifdef SQLITE_HAVE_ZLIB
5108    if( !p->bSafeModePersist ){
5109      sqlite3_zipfile_init(p->db, 0, 0);
5110      sqlite3_sqlar_init(p->db, 0, 0);
5111    }
5112#endif
5113    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5114                            shellAddSchemaName, 0, 0);
5115    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5116                            shellModuleSchema, 0, 0);
5117    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5118                            shellPutsFunc, 0, 0);
5119    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
5120                            shellEscapeCrnl, 0, 0);
5121    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
5122                            shellInt32, 0, 0);
5123    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
5124                            shellIdQuote, 0, 0);
5125    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5126                            shellUSleepFunc, 0, 0);
5127#ifndef SQLITE_NOHAVE_SYSTEM
5128    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5129                            editFunc, 0, 0);
5130    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5131                            editFunc, 0, 0);
5132#endif
5133    if( p->openMode==SHELL_OPEN_ZIPFILE ){
5134      char *zSql = sqlite3_mprintf(
5135         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5136      shell_check_oom(zSql);
5137      sqlite3_exec(p->db, zSql, 0, 0, 0);
5138      sqlite3_free(zSql);
5139    }
5140#ifndef SQLITE_OMIT_DESERIALIZE
5141    else
5142    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5143      int rc;
5144      int nData = 0;
5145      unsigned char *aData;
5146      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5147        aData = (unsigned char*)readFile(zDbFilename, &nData);
5148      }else{
5149        aData = readHexDb(p, &nData);
5150        if( aData==0 ){
5151          return;
5152        }
5153      }
5154      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5155                   SQLITE_DESERIALIZE_RESIZEABLE |
5156                   SQLITE_DESERIALIZE_FREEONCLOSE);
5157      if( rc ){
5158        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5159      }
5160      if( p->szMax>0 ){
5161        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5162      }
5163    }
5164#endif
5165  }
5166  if( p->bSafeModePersist && p->db!=0 ){
5167    sqlite3_set_authorizer(p->db, safeModeAuth, p);
5168  }
5169}
5170
5171/*
5172** Attempt to close the databaes connection.  Report errors.
5173*/
5174void close_db(sqlite3 *db){
5175  int rc = sqlite3_close(db);
5176  if( rc ){
5177    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5178        rc, sqlite3_errmsg(db));
5179  }
5180}
5181
5182#if HAVE_READLINE || HAVE_EDITLINE
5183/*
5184** Readline completion callbacks
5185*/
5186static char *readline_completion_generator(const char *text, int state){
5187  static sqlite3_stmt *pStmt = 0;
5188  char *zRet;
5189  if( state==0 ){
5190    char *zSql;
5191    sqlite3_finalize(pStmt);
5192    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5193                           "  FROM completion(%Q) ORDER BY 1", text);
5194    shell_check_oom(zSql);
5195    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5196    sqlite3_free(zSql);
5197  }
5198  if( sqlite3_step(pStmt)==SQLITE_ROW ){
5199    const char *z = (const char*)sqlite3_column_text(pStmt,0);
5200    zRet = z ? strdup(z) : 0;
5201  }else{
5202    sqlite3_finalize(pStmt);
5203    pStmt = 0;
5204    zRet = 0;
5205  }
5206  return zRet;
5207}
5208static char **readline_completion(const char *zText, int iStart, int iEnd){
5209  rl_attempted_completion_over = 1;
5210  return rl_completion_matches(zText, readline_completion_generator);
5211}
5212
5213#elif HAVE_LINENOISE
5214/*
5215** Linenoise completion callback
5216*/
5217static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5218  int nLine = strlen30(zLine);
5219  int i, iStart;
5220  sqlite3_stmt *pStmt = 0;
5221  char *zSql;
5222  char zBuf[1000];
5223
5224  if( nLine>sizeof(zBuf)-30 ) return;
5225  if( zLine[0]=='.' || zLine[0]=='#') return;
5226  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5227  if( i==nLine-1 ) return;
5228  iStart = i+1;
5229  memcpy(zBuf, zLine, iStart);
5230  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5231                         "  FROM completion(%Q,%Q) ORDER BY 1",
5232                         &zLine[iStart], zLine);
5233  shell_check_oom(zSql);
5234  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5235  sqlite3_free(zSql);
5236  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5237  while( sqlite3_step(pStmt)==SQLITE_ROW ){
5238    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5239    int nCompletion = sqlite3_column_bytes(pStmt, 0);
5240    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
5241      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5242      linenoiseAddCompletion(lc, zBuf);
5243    }
5244  }
5245  sqlite3_finalize(pStmt);
5246}
5247#endif
5248
5249/*
5250** Do C-language style dequoting.
5251**
5252**    \a    -> alarm
5253**    \b    -> backspace
5254**    \t    -> tab
5255**    \n    -> newline
5256**    \v    -> vertical tab
5257**    \f    -> form feed
5258**    \r    -> carriage return
5259**    \s    -> space
5260**    \"    -> "
5261**    \'    -> '
5262**    \\    -> backslash
5263**    \NNN  -> ascii character NNN in octal
5264*/
5265static void resolve_backslashes(char *z){
5266  int i, j;
5267  char c;
5268  while( *z && *z!='\\' ) z++;
5269  for(i=j=0; (c = z[i])!=0; i++, j++){
5270    if( c=='\\' && z[i+1]!=0 ){
5271      c = z[++i];
5272      if( c=='a' ){
5273        c = '\a';
5274      }else if( c=='b' ){
5275        c = '\b';
5276      }else if( c=='t' ){
5277        c = '\t';
5278      }else if( c=='n' ){
5279        c = '\n';
5280      }else if( c=='v' ){
5281        c = '\v';
5282      }else if( c=='f' ){
5283        c = '\f';
5284      }else if( c=='r' ){
5285        c = '\r';
5286      }else if( c=='"' ){
5287        c = '"';
5288      }else if( c=='\'' ){
5289        c = '\'';
5290      }else if( c=='\\' ){
5291        c = '\\';
5292      }else if( c>='0' && c<='7' ){
5293        c -= '0';
5294        if( z[i+1]>='0' && z[i+1]<='7' ){
5295          i++;
5296          c = (c<<3) + z[i] - '0';
5297          if( z[i+1]>='0' && z[i+1]<='7' ){
5298            i++;
5299            c = (c<<3) + z[i] - '0';
5300          }
5301        }
5302      }
5303    }
5304    z[j] = c;
5305  }
5306  if( j<i ) z[j] = 0;
5307}
5308
5309/*
5310** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
5311** for TRUE and FALSE.  Return the integer value if appropriate.
5312*/
5313static int booleanValue(const char *zArg){
5314  int i;
5315  if( zArg[0]=='0' && zArg[1]=='x' ){
5316    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5317  }else{
5318    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5319  }
5320  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5321  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5322    return 1;
5323  }
5324  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5325    return 0;
5326  }
5327  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5328          zArg);
5329  return 0;
5330}
5331
5332/*
5333** Set or clear a shell flag according to a boolean value.
5334*/
5335static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5336  if( booleanValue(zArg) ){
5337    ShellSetFlag(p, mFlag);
5338  }else{
5339    ShellClearFlag(p, mFlag);
5340  }
5341}
5342
5343/*
5344** Close an output file, assuming it is not stderr or stdout
5345*/
5346static void output_file_close(FILE *f){
5347  if( f && f!=stdout && f!=stderr ) fclose(f);
5348}
5349
5350/*
5351** Try to open an output file.   The names "stdout" and "stderr" are
5352** recognized and do the right thing.  NULL is returned if the output
5353** filename is "off".
5354*/
5355static FILE *output_file_open(const char *zFile, int bTextMode){
5356  FILE *f;
5357  if( strcmp(zFile,"stdout")==0 ){
5358    f = stdout;
5359  }else if( strcmp(zFile, "stderr")==0 ){
5360    f = stderr;
5361  }else if( strcmp(zFile, "off")==0 ){
5362    f = 0;
5363  }else{
5364    f = fopen(zFile, bTextMode ? "w" : "wb");
5365    if( f==0 ){
5366      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5367    }
5368  }
5369  return f;
5370}
5371
5372#ifndef SQLITE_OMIT_TRACE
5373/*
5374** A routine for handling output from sqlite3_trace().
5375*/
5376static int sql_trace_callback(
5377  unsigned mType,         /* The trace type */
5378  void *pArg,             /* The ShellState pointer */
5379  void *pP,               /* Usually a pointer to sqlite_stmt */
5380  void *pX                /* Auxiliary output */
5381){
5382  ShellState *p = (ShellState*)pArg;
5383  sqlite3_stmt *pStmt;
5384  const char *zSql;
5385  int nSql;
5386  if( p->traceOut==0 ) return 0;
5387  if( mType==SQLITE_TRACE_CLOSE ){
5388    utf8_printf(p->traceOut, "-- closing database connection\n");
5389    return 0;
5390  }
5391  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5392    zSql = (const char*)pX;
5393  }else{
5394    pStmt = (sqlite3_stmt*)pP;
5395    switch( p->eTraceType ){
5396      case SHELL_TRACE_EXPANDED: {
5397        zSql = sqlite3_expanded_sql(pStmt);
5398        break;
5399      }
5400#ifdef SQLITE_ENABLE_NORMALIZE
5401      case SHELL_TRACE_NORMALIZED: {
5402        zSql = sqlite3_normalized_sql(pStmt);
5403        break;
5404      }
5405#endif
5406      default: {
5407        zSql = sqlite3_sql(pStmt);
5408        break;
5409      }
5410    }
5411  }
5412  if( zSql==0 ) return 0;
5413  nSql = strlen30(zSql);
5414  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5415  switch( mType ){
5416    case SQLITE_TRACE_ROW:
5417    case SQLITE_TRACE_STMT: {
5418      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
5419      break;
5420    }
5421    case SQLITE_TRACE_PROFILE: {
5422      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5423      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
5424      break;
5425    }
5426  }
5427  return 0;
5428}
5429#endif
5430
5431/*
5432** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5433** a useful spot to set a debugger breakpoint.
5434*/
5435static void test_breakpoint(void){
5436  static int nCall = 0;
5437  nCall++;
5438}
5439
5440/*
5441** An object used to read a CSV and other files for import.
5442*/
5443typedef struct ImportCtx ImportCtx;
5444struct ImportCtx {
5445  const char *zFile;  /* Name of the input file */
5446  FILE *in;           /* Read the CSV text from this input stream */
5447  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5448  char *z;            /* Accumulated text for a field */
5449  int n;              /* Number of bytes in z */
5450  int nAlloc;         /* Space allocated for z[] */
5451  int nLine;          /* Current line number */
5452  int nRow;           /* Number of rows imported */
5453  int nErr;           /* Number of errors encountered */
5454  int bNotFirst;      /* True if one or more bytes already read */
5455  int cTerm;          /* Character that terminated the most recent field */
5456  int cColSep;        /* The column separator character.  (Usually ",") */
5457  int cRowSep;        /* The row separator character.  (Usually "\n") */
5458};
5459
5460/* Clean up resourced used by an ImportCtx */
5461static void import_cleanup(ImportCtx *p){
5462  if( p->in!=0 && p->xCloser!=0 ){
5463    p->xCloser(p->in);
5464    p->in = 0;
5465  }
5466  sqlite3_free(p->z);
5467  p->z = 0;
5468}
5469
5470/* Append a single byte to z[] */
5471static void import_append_char(ImportCtx *p, int c){
5472  if( p->n+1>=p->nAlloc ){
5473    p->nAlloc += p->nAlloc + 100;
5474    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5475    shell_check_oom(p->z);
5476  }
5477  p->z[p->n++] = (char)c;
5478}
5479
5480/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5481** with the option of having a separator other than ",".
5482**
5483**   +  Input comes from p->in.
5484**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5485**      from sqlite3_malloc64().
5486**   +  Use p->cSep as the column separator.  The default is ",".
5487**   +  Use p->rSep as the row separator.  The default is "\n".
5488**   +  Keep track of the line number in p->nLine.
5489**   +  Store the character that terminates the field in p->cTerm.  Store
5490**      EOF on end-of-file.
5491**   +  Report syntax errors on stderr
5492*/
5493static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5494  int c;
5495  int cSep = p->cColSep;
5496  int rSep = p->cRowSep;
5497  p->n = 0;
5498  c = fgetc(p->in);
5499  if( c==EOF || seenInterrupt ){
5500    p->cTerm = EOF;
5501    return 0;
5502  }
5503  if( c=='"' ){
5504    int pc, ppc;
5505    int startLine = p->nLine;
5506    int cQuote = c;
5507    pc = ppc = 0;
5508    while( 1 ){
5509      c = fgetc(p->in);
5510      if( c==rSep ) p->nLine++;
5511      if( c==cQuote ){
5512        if( pc==cQuote ){
5513          pc = 0;
5514          continue;
5515        }
5516      }
5517      if( (c==cSep && pc==cQuote)
5518       || (c==rSep && pc==cQuote)
5519       || (c==rSep && pc=='\r' && ppc==cQuote)
5520       || (c==EOF && pc==cQuote)
5521      ){
5522        do{ p->n--; }while( p->z[p->n]!=cQuote );
5523        p->cTerm = c;
5524        break;
5525      }
5526      if( pc==cQuote && c!='\r' ){
5527        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5528                p->zFile, p->nLine, cQuote);
5529      }
5530      if( c==EOF ){
5531        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5532                p->zFile, startLine, cQuote);
5533        p->cTerm = c;
5534        break;
5535      }
5536      import_append_char(p, c);
5537      ppc = pc;
5538      pc = c;
5539    }
5540  }else{
5541    /* If this is the first field being parsed and it begins with the
5542    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5543    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5544      import_append_char(p, c);
5545      c = fgetc(p->in);
5546      if( (c&0xff)==0xbb ){
5547        import_append_char(p, c);
5548        c = fgetc(p->in);
5549        if( (c&0xff)==0xbf ){
5550          p->bNotFirst = 1;
5551          p->n = 0;
5552          return csv_read_one_field(p);
5553        }
5554      }
5555    }
5556    while( c!=EOF && c!=cSep && c!=rSep ){
5557      import_append_char(p, c);
5558      c = fgetc(p->in);
5559    }
5560    if( c==rSep ){
5561      p->nLine++;
5562      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5563    }
5564    p->cTerm = c;
5565  }
5566  if( p->z ) p->z[p->n] = 0;
5567  p->bNotFirst = 1;
5568  return p->z;
5569}
5570
5571/* Read a single field of ASCII delimited text.
5572**
5573**   +  Input comes from p->in.
5574**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5575**      from sqlite3_malloc64().
5576**   +  Use p->cSep as the column separator.  The default is "\x1F".
5577**   +  Use p->rSep as the row separator.  The default is "\x1E".
5578**   +  Keep track of the row number in p->nLine.
5579**   +  Store the character that terminates the field in p->cTerm.  Store
5580**      EOF on end-of-file.
5581**   +  Report syntax errors on stderr
5582*/
5583static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5584  int c;
5585  int cSep = p->cColSep;
5586  int rSep = p->cRowSep;
5587  p->n = 0;
5588  c = fgetc(p->in);
5589  if( c==EOF || seenInterrupt ){
5590    p->cTerm = EOF;
5591    return 0;
5592  }
5593  while( c!=EOF && c!=cSep && c!=rSep ){
5594    import_append_char(p, c);
5595    c = fgetc(p->in);
5596  }
5597  if( c==rSep ){
5598    p->nLine++;
5599  }
5600  p->cTerm = c;
5601  if( p->z ) p->z[p->n] = 0;
5602  return p->z;
5603}
5604
5605/*
5606** Try to transfer data for table zTable.  If an error is seen while
5607** moving forward, try to go backwards.  The backwards movement won't
5608** work for WITHOUT ROWID tables.
5609*/
5610static void tryToCloneData(
5611  ShellState *p,
5612  sqlite3 *newDb,
5613  const char *zTable
5614){
5615  sqlite3_stmt *pQuery = 0;
5616  sqlite3_stmt *pInsert = 0;
5617  char *zQuery = 0;
5618  char *zInsert = 0;
5619  int rc;
5620  int i, j, n;
5621  int nTable = strlen30(zTable);
5622  int k = 0;
5623  int cnt = 0;
5624  const int spinRate = 10000;
5625
5626  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5627  shell_check_oom(zQuery);
5628  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5629  if( rc ){
5630    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5631            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5632            zQuery);
5633    goto end_data_xfer;
5634  }
5635  n = sqlite3_column_count(pQuery);
5636  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5637  shell_check_oom(zInsert);
5638  sqlite3_snprintf(200+nTable,zInsert,
5639                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5640  i = strlen30(zInsert);
5641  for(j=1; j<n; j++){
5642    memcpy(zInsert+i, ",?", 2);
5643    i += 2;
5644  }
5645  memcpy(zInsert+i, ");", 3);
5646  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5647  if( rc ){
5648    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5649            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5650            zQuery);
5651    goto end_data_xfer;
5652  }
5653  for(k=0; k<2; k++){
5654    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5655      for(i=0; i<n; i++){
5656        switch( sqlite3_column_type(pQuery, i) ){
5657          case SQLITE_NULL: {
5658            sqlite3_bind_null(pInsert, i+1);
5659            break;
5660          }
5661          case SQLITE_INTEGER: {
5662            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5663            break;
5664          }
5665          case SQLITE_FLOAT: {
5666            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5667            break;
5668          }
5669          case SQLITE_TEXT: {
5670            sqlite3_bind_text(pInsert, i+1,
5671                             (const char*)sqlite3_column_text(pQuery,i),
5672                             -1, SQLITE_STATIC);
5673            break;
5674          }
5675          case SQLITE_BLOB: {
5676            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5677                                            sqlite3_column_bytes(pQuery,i),
5678                                            SQLITE_STATIC);
5679            break;
5680          }
5681        }
5682      } /* End for */
5683      rc = sqlite3_step(pInsert);
5684      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5685        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5686                        sqlite3_errmsg(newDb));
5687      }
5688      sqlite3_reset(pInsert);
5689      cnt++;
5690      if( (cnt%spinRate)==0 ){
5691        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5692        fflush(stdout);
5693      }
5694    } /* End while */
5695    if( rc==SQLITE_DONE ) break;
5696    sqlite3_finalize(pQuery);
5697    sqlite3_free(zQuery);
5698    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5699                             zTable);
5700    shell_check_oom(zQuery);
5701    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5702    if( rc ){
5703      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5704      break;
5705    }
5706  } /* End for(k=0...) */
5707
5708end_data_xfer:
5709  sqlite3_finalize(pQuery);
5710  sqlite3_finalize(pInsert);
5711  sqlite3_free(zQuery);
5712  sqlite3_free(zInsert);
5713}
5714
5715
5716/*
5717** Try to transfer all rows of the schema that match zWhere.  For
5718** each row, invoke xForEach() on the object defined by that row.
5719** If an error is encountered while moving forward through the
5720** sqlite_schema table, try again moving backwards.
5721*/
5722static void tryToCloneSchema(
5723  ShellState *p,
5724  sqlite3 *newDb,
5725  const char *zWhere,
5726  void (*xForEach)(ShellState*,sqlite3*,const char*)
5727){
5728  sqlite3_stmt *pQuery = 0;
5729  char *zQuery = 0;
5730  int rc;
5731  const unsigned char *zName;
5732  const unsigned char *zSql;
5733  char *zErrMsg = 0;
5734
5735  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5736                           " WHERE %s", zWhere);
5737  shell_check_oom(zQuery);
5738  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5739  if( rc ){
5740    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5741                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5742                    zQuery);
5743    goto end_schema_xfer;
5744  }
5745  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5746    zName = sqlite3_column_text(pQuery, 0);
5747    zSql = sqlite3_column_text(pQuery, 1);
5748    if( zName==0 || zSql==0 ) continue;
5749    printf("%s... ", zName); fflush(stdout);
5750    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5751    if( zErrMsg ){
5752      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5753      sqlite3_free(zErrMsg);
5754      zErrMsg = 0;
5755    }
5756    if( xForEach ){
5757      xForEach(p, newDb, (const char*)zName);
5758    }
5759    printf("done\n");
5760  }
5761  if( rc!=SQLITE_DONE ){
5762    sqlite3_finalize(pQuery);
5763    sqlite3_free(zQuery);
5764    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5765                             " WHERE %s ORDER BY rowid DESC", zWhere);
5766    shell_check_oom(zQuery);
5767    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5768    if( rc ){
5769      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5770                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5771                      zQuery);
5772      goto end_schema_xfer;
5773    }
5774    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5775      zName = sqlite3_column_text(pQuery, 0);
5776      zSql = sqlite3_column_text(pQuery, 1);
5777      if( zName==0 || zSql==0 ) continue;
5778      printf("%s... ", zName); fflush(stdout);
5779      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5780      if( zErrMsg ){
5781        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5782        sqlite3_free(zErrMsg);
5783        zErrMsg = 0;
5784      }
5785      if( xForEach ){
5786        xForEach(p, newDb, (const char*)zName);
5787      }
5788      printf("done\n");
5789    }
5790  }
5791end_schema_xfer:
5792  sqlite3_finalize(pQuery);
5793  sqlite3_free(zQuery);
5794}
5795
5796/*
5797** Open a new database file named "zNewDb".  Try to recover as much information
5798** as possible out of the main database (which might be corrupt) and write it
5799** into zNewDb.
5800*/
5801static void tryToClone(ShellState *p, const char *zNewDb){
5802  int rc;
5803  sqlite3 *newDb = 0;
5804  if( access(zNewDb,0)==0 ){
5805    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5806    return;
5807  }
5808  rc = sqlite3_open(zNewDb, &newDb);
5809  if( rc ){
5810    utf8_printf(stderr, "Cannot create output database: %s\n",
5811            sqlite3_errmsg(newDb));
5812  }else{
5813    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5814    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5815    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5816    tryToCloneSchema(p, newDb, "type!='table'", 0);
5817    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5818    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5819  }
5820  close_db(newDb);
5821}
5822
5823/*
5824** Change the output file back to stdout.
5825**
5826** If the p->doXdgOpen flag is set, that means the output was being
5827** redirected to a temporary file named by p->zTempFile.  In that case,
5828** launch start/open/xdg-open on that temporary file.
5829*/
5830static void output_reset(ShellState *p){
5831  if( p->outfile[0]=='|' ){
5832#ifndef SQLITE_OMIT_POPEN
5833    pclose(p->out);
5834#endif
5835  }else{
5836    output_file_close(p->out);
5837#ifndef SQLITE_NOHAVE_SYSTEM
5838    if( p->doXdgOpen ){
5839      const char *zXdgOpenCmd =
5840#if defined(_WIN32)
5841      "start";
5842#elif defined(__APPLE__)
5843      "open";
5844#else
5845      "xdg-open";
5846#endif
5847      char *zCmd;
5848      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5849      if( system(zCmd) ){
5850        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5851      }else{
5852        /* Give the start/open/xdg-open command some time to get
5853        ** going before we continue, and potential delete the
5854        ** p->zTempFile data file out from under it */
5855        sqlite3_sleep(2000);
5856      }
5857      sqlite3_free(zCmd);
5858      outputModePop(p);
5859      p->doXdgOpen = 0;
5860    }
5861#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5862  }
5863  p->outfile[0] = 0;
5864  p->out = stdout;
5865}
5866
5867/*
5868** Run an SQL command and return the single integer result.
5869*/
5870static int db_int(sqlite3 *db, const char *zSql){
5871  sqlite3_stmt *pStmt;
5872  int res = 0;
5873  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
5874  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5875    res = sqlite3_column_int(pStmt,0);
5876  }
5877  sqlite3_finalize(pStmt);
5878  return res;
5879}
5880
5881#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5882/*
5883** Convert a 2-byte or 4-byte big-endian integer into a native integer
5884*/
5885static unsigned int get2byteInt(unsigned char *a){
5886  return (a[0]<<8) + a[1];
5887}
5888static unsigned int get4byteInt(unsigned char *a){
5889  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5890}
5891
5892/*
5893** Implementation of the ".dbinfo" command.
5894**
5895** Return 1 on error, 2 to exit, and 0 otherwise.
5896*/
5897static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5898  static const struct { const char *zName; int ofst; } aField[] = {
5899     { "file change counter:",  24  },
5900     { "database page count:",  28  },
5901     { "freelist page count:",  36  },
5902     { "schema cookie:",        40  },
5903     { "schema format:",        44  },
5904     { "default cache size:",   48  },
5905     { "autovacuum top root:",  52  },
5906     { "incremental vacuum:",   64  },
5907     { "text encoding:",        56  },
5908     { "user version:",         60  },
5909     { "application id:",       68  },
5910     { "software version:",     96  },
5911  };
5912  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5913     { "number of tables:",
5914       "SELECT count(*) FROM %s WHERE type='table'" },
5915     { "number of indexes:",
5916       "SELECT count(*) FROM %s WHERE type='index'" },
5917     { "number of triggers:",
5918       "SELECT count(*) FROM %s WHERE type='trigger'" },
5919     { "number of views:",
5920       "SELECT count(*) FROM %s WHERE type='view'" },
5921     { "schema size:",
5922       "SELECT total(length(sql)) FROM %s" },
5923  };
5924  int i, rc;
5925  unsigned iDataVersion;
5926  char *zSchemaTab;
5927  char *zDb = nArg>=2 ? azArg[1] : "main";
5928  sqlite3_stmt *pStmt = 0;
5929  unsigned char aHdr[100];
5930  open_db(p, 0);
5931  if( p->db==0 ) return 1;
5932  rc = sqlite3_prepare_v2(p->db,
5933             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5934             -1, &pStmt, 0);
5935  if( rc ){
5936    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5937    sqlite3_finalize(pStmt);
5938    return 1;
5939  }
5940  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5941  if( sqlite3_step(pStmt)==SQLITE_ROW
5942   && sqlite3_column_bytes(pStmt,0)>100
5943  ){
5944    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5945    sqlite3_finalize(pStmt);
5946  }else{
5947    raw_printf(stderr, "unable to read database header\n");
5948    sqlite3_finalize(pStmt);
5949    return 1;
5950  }
5951  i = get2byteInt(aHdr+16);
5952  if( i==1 ) i = 65536;
5953  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5954  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5955  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5956  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5957  for(i=0; i<ArraySize(aField); i++){
5958    int ofst = aField[i].ofst;
5959    unsigned int val = get4byteInt(aHdr + ofst);
5960    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5961    switch( ofst ){
5962      case 56: {
5963        if( val==1 ) raw_printf(p->out, " (utf8)");
5964        if( val==2 ) raw_printf(p->out, " (utf16le)");
5965        if( val==3 ) raw_printf(p->out, " (utf16be)");
5966      }
5967    }
5968    raw_printf(p->out, "\n");
5969  }
5970  if( zDb==0 ){
5971    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5972  }else if( strcmp(zDb,"temp")==0 ){
5973    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5974  }else{
5975    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5976  }
5977  for(i=0; i<ArraySize(aQuery); i++){
5978    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5979    int val = db_int(p->db, zSql);
5980    sqlite3_free(zSql);
5981    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5982  }
5983  sqlite3_free(zSchemaTab);
5984  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5985  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5986  return 0;
5987}
5988#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
5989          && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
5990
5991/*
5992** Print the current sqlite3_errmsg() value to stderr and return 1.
5993*/
5994static int shellDatabaseError(sqlite3 *db){
5995  const char *zErr = sqlite3_errmsg(db);
5996  utf8_printf(stderr, "Error: %s\n", zErr);
5997  return 1;
5998}
5999
6000/*
6001** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
6002** if they match and FALSE (0) if they do not match.
6003**
6004** Globbing rules:
6005**
6006**      '*'       Matches any sequence of zero or more characters.
6007**
6008**      '?'       Matches exactly one character.
6009**
6010**     [...]      Matches one character from the enclosed list of
6011**                characters.
6012**
6013**     [^...]     Matches one character not in the enclosed list.
6014**
6015**      '#'       Matches any sequence of one or more digits with an
6016**                optional + or - sign in front
6017**
6018**      ' '       Any span of whitespace matches any other span of
6019**                whitespace.
6020**
6021** Extra whitespace at the end of z[] is ignored.
6022*/
6023static int testcase_glob(const char *zGlob, const char *z){
6024  int c, c2;
6025  int invert;
6026  int seen;
6027
6028  while( (c = (*(zGlob++)))!=0 ){
6029    if( IsSpace(c) ){
6030      if( !IsSpace(*z) ) return 0;
6031      while( IsSpace(*zGlob) ) zGlob++;
6032      while( IsSpace(*z) ) z++;
6033    }else if( c=='*' ){
6034      while( (c=(*(zGlob++))) == '*' || c=='?' ){
6035        if( c=='?' && (*(z++))==0 ) return 0;
6036      }
6037      if( c==0 ){
6038        return 1;
6039      }else if( c=='[' ){
6040        while( *z && testcase_glob(zGlob-1,z)==0 ){
6041          z++;
6042        }
6043        return (*z)!=0;
6044      }
6045      while( (c2 = (*(z++)))!=0 ){
6046        while( c2!=c ){
6047          c2 = *(z++);
6048          if( c2==0 ) return 0;
6049        }
6050        if( testcase_glob(zGlob,z) ) return 1;
6051      }
6052      return 0;
6053    }else if( c=='?' ){
6054      if( (*(z++))==0 ) return 0;
6055    }else if( c=='[' ){
6056      int prior_c = 0;
6057      seen = 0;
6058      invert = 0;
6059      c = *(z++);
6060      if( c==0 ) return 0;
6061      c2 = *(zGlob++);
6062      if( c2=='^' ){
6063        invert = 1;
6064        c2 = *(zGlob++);
6065      }
6066      if( c2==']' ){
6067        if( c==']' ) seen = 1;
6068        c2 = *(zGlob++);
6069      }
6070      while( c2 && c2!=']' ){
6071        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6072          c2 = *(zGlob++);
6073          if( c>=prior_c && c<=c2 ) seen = 1;
6074          prior_c = 0;
6075        }else{
6076          if( c==c2 ){
6077            seen = 1;
6078          }
6079          prior_c = c2;
6080        }
6081        c2 = *(zGlob++);
6082      }
6083      if( c2==0 || (seen ^ invert)==0 ) return 0;
6084    }else if( c=='#' ){
6085      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6086      if( !IsDigit(z[0]) ) return 0;
6087      z++;
6088      while( IsDigit(z[0]) ){ z++; }
6089    }else{
6090      if( c!=(*(z++)) ) return 0;
6091    }
6092  }
6093  while( IsSpace(*z) ){ z++; }
6094  return *z==0;
6095}
6096
6097
6098/*
6099** Compare the string as a command-line option with either one or two
6100** initial "-" characters.
6101*/
6102static int optionMatch(const char *zStr, const char *zOpt){
6103  if( zStr[0]!='-' ) return 0;
6104  zStr++;
6105  if( zStr[0]=='-' ) zStr++;
6106  return strcmp(zStr, zOpt)==0;
6107}
6108
6109/*
6110** Delete a file.
6111*/
6112int shellDeleteFile(const char *zFilename){
6113  int rc;
6114#ifdef _WIN32
6115  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6116  rc = _wunlink(z);
6117  sqlite3_free(z);
6118#else
6119  rc = unlink(zFilename);
6120#endif
6121  return rc;
6122}
6123
6124/*
6125** Try to delete the temporary file (if there is one) and free the
6126** memory used to hold the name of the temp file.
6127*/
6128static void clearTempFile(ShellState *p){
6129  if( p->zTempFile==0 ) return;
6130  if( p->doXdgOpen ) return;
6131  if( shellDeleteFile(p->zTempFile) ) return;
6132  sqlite3_free(p->zTempFile);
6133  p->zTempFile = 0;
6134}
6135
6136/*
6137** Create a new temp file name with the given suffix.
6138*/
6139static void newTempFile(ShellState *p, const char *zSuffix){
6140  clearTempFile(p);
6141  sqlite3_free(p->zTempFile);
6142  p->zTempFile = 0;
6143  if( p->db ){
6144    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6145  }
6146  if( p->zTempFile==0 ){
6147    /* If p->db is an in-memory database then the TEMPFILENAME file-control
6148    ** will not work and we will need to fallback to guessing */
6149    char *zTemp;
6150    sqlite3_uint64 r;
6151    sqlite3_randomness(sizeof(r), &r);
6152    zTemp = getenv("TEMP");
6153    if( zTemp==0 ) zTemp = getenv("TMP");
6154    if( zTemp==0 ){
6155#ifdef _WIN32
6156      zTemp = "\\tmp";
6157#else
6158      zTemp = "/tmp";
6159#endif
6160    }
6161    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6162  }else{
6163    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6164  }
6165  shell_check_oom(p->zTempFile);
6166}
6167
6168
6169/*
6170** The implementation of SQL scalar function fkey_collate_clause(), used
6171** by the ".lint fkey-indexes" command. This scalar function is always
6172** called with four arguments - the parent table name, the parent column name,
6173** the child table name and the child column name.
6174**
6175**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6176**
6177** If either of the named tables or columns do not exist, this function
6178** returns an empty string. An empty string is also returned if both tables
6179** and columns exist but have the same default collation sequence. Or,
6180** if both exist but the default collation sequences are different, this
6181** function returns the string " COLLATE <parent-collation>", where
6182** <parent-collation> is the default collation sequence of the parent column.
6183*/
6184static void shellFkeyCollateClause(
6185  sqlite3_context *pCtx,
6186  int nVal,
6187  sqlite3_value **apVal
6188){
6189  sqlite3 *db = sqlite3_context_db_handle(pCtx);
6190  const char *zParent;
6191  const char *zParentCol;
6192  const char *zParentSeq;
6193  const char *zChild;
6194  const char *zChildCol;
6195  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
6196  int rc;
6197
6198  assert( nVal==4 );
6199  zParent = (const char*)sqlite3_value_text(apVal[0]);
6200  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6201  zChild = (const char*)sqlite3_value_text(apVal[2]);
6202  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6203
6204  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6205  rc = sqlite3_table_column_metadata(
6206      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6207  );
6208  if( rc==SQLITE_OK ){
6209    rc = sqlite3_table_column_metadata(
6210        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6211    );
6212  }
6213
6214  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6215    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6216    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6217    sqlite3_free(z);
6218  }
6219}
6220
6221
6222/*
6223** The implementation of dot-command ".lint fkey-indexes".
6224*/
6225static int lintFkeyIndexes(
6226  ShellState *pState,             /* Current shell tool state */
6227  char **azArg,                   /* Array of arguments passed to dot command */
6228  int nArg                        /* Number of entries in azArg[] */
6229){
6230  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
6231  FILE *out = pState->out;        /* Stream to write non-error output to */
6232  int bVerbose = 0;               /* If -verbose is present */
6233  int bGroupByParent = 0;         /* If -groupbyparent is present */
6234  int i;                          /* To iterate through azArg[] */
6235  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
6236  int rc;                         /* Return code */
6237  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
6238
6239  /*
6240  ** This SELECT statement returns one row for each foreign key constraint
6241  ** in the schema of the main database. The column values are:
6242  **
6243  ** 0. The text of an SQL statement similar to:
6244  **
6245  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6246  **
6247  **    This SELECT is similar to the one that the foreign keys implementation
6248  **    needs to run internally on child tables. If there is an index that can
6249  **    be used to optimize this query, then it can also be used by the FK
6250  **    implementation to optimize DELETE or UPDATE statements on the parent
6251  **    table.
6252  **
6253  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6254  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6255  **    contains an index that can be used to optimize the query.
6256  **
6257  ** 2. Human readable text that describes the child table and columns. e.g.
6258  **
6259  **       "child_table(child_key1, child_key2)"
6260  **
6261  ** 3. Human readable text that describes the parent table and columns. e.g.
6262  **
6263  **       "parent_table(parent_key1, parent_key2)"
6264  **
6265  ** 4. A full CREATE INDEX statement for an index that could be used to
6266  **    optimize DELETE or UPDATE statements on the parent table. e.g.
6267  **
6268  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
6269  **
6270  ** 5. The name of the parent table.
6271  **
6272  ** These six values are used by the C logic below to generate the report.
6273  */
6274  const char *zSql =
6275  "SELECT "
6276    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6277    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6278    "  || fkey_collate_clause("
6279    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6280    ", "
6281    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6282    "  || group_concat('*=?', ' AND ') || ')'"
6283    ", "
6284    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
6285    ", "
6286    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6287    ", "
6288    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6289    "  || ' ON ' || quote(s.name) || '('"
6290    "  || group_concat(quote(f.[from]) ||"
6291    "        fkey_collate_clause("
6292    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6293    "  || ');'"
6294    ", "
6295    "     f.[table] "
6296    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6297    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6298    "GROUP BY s.name, f.id "
6299    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6300  ;
6301  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6302
6303  for(i=2; i<nArg; i++){
6304    int n = strlen30(azArg[i]);
6305    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6306      bVerbose = 1;
6307    }
6308    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6309      bGroupByParent = 1;
6310      zIndent = "    ";
6311    }
6312    else{
6313      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6314          azArg[0], azArg[1]
6315      );
6316      return SQLITE_ERROR;
6317    }
6318  }
6319
6320  /* Register the fkey_collate_clause() SQL function */
6321  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6322      0, shellFkeyCollateClause, 0, 0
6323  );
6324
6325
6326  if( rc==SQLITE_OK ){
6327    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6328  }
6329  if( rc==SQLITE_OK ){
6330    sqlite3_bind_int(pSql, 1, bGroupByParent);
6331  }
6332
6333  if( rc==SQLITE_OK ){
6334    int rc2;
6335    char *zPrev = 0;
6336    while( SQLITE_ROW==sqlite3_step(pSql) ){
6337      int res = -1;
6338      sqlite3_stmt *pExplain = 0;
6339      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6340      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6341      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6342      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6343      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6344      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6345
6346      if( zEQP==0 ) continue;
6347      if( zGlob==0 ) continue;
6348      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6349      if( rc!=SQLITE_OK ) break;
6350      if( SQLITE_ROW==sqlite3_step(pExplain) ){
6351        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6352        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
6353                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
6354      }
6355      rc = sqlite3_finalize(pExplain);
6356      if( rc!=SQLITE_OK ) break;
6357
6358      if( res<0 ){
6359        raw_printf(stderr, "Error: internal error");
6360        break;
6361      }else{
6362        if( bGroupByParent
6363        && (bVerbose || res==0)
6364        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6365        ){
6366          raw_printf(out, "-- Parent table %s\n", zParent);
6367          sqlite3_free(zPrev);
6368          zPrev = sqlite3_mprintf("%s", zParent);
6369        }
6370
6371        if( res==0 ){
6372          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6373        }else if( bVerbose ){
6374          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6375              zIndent, zFrom, zTarget
6376          );
6377        }
6378      }
6379    }
6380    sqlite3_free(zPrev);
6381
6382    if( rc!=SQLITE_OK ){
6383      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6384    }
6385
6386    rc2 = sqlite3_finalize(pSql);
6387    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6388      rc = rc2;
6389      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6390    }
6391  }else{
6392    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6393  }
6394
6395  return rc;
6396}
6397
6398/*
6399** Implementation of ".lint" dot command.
6400*/
6401static int lintDotCommand(
6402  ShellState *pState,             /* Current shell tool state */
6403  char **azArg,                   /* Array of arguments passed to dot command */
6404  int nArg                        /* Number of entries in azArg[] */
6405){
6406  int n;
6407  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6408  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6409  return lintFkeyIndexes(pState, azArg, nArg);
6410
6411 usage:
6412  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6413  raw_printf(stderr, "Where sub-commands are:\n");
6414  raw_printf(stderr, "    fkey-indexes\n");
6415  return SQLITE_ERROR;
6416}
6417
6418#if !defined SQLITE_OMIT_VIRTUALTABLE
6419static void shellPrepare(
6420  sqlite3 *db,
6421  int *pRc,
6422  const char *zSql,
6423  sqlite3_stmt **ppStmt
6424){
6425  *ppStmt = 0;
6426  if( *pRc==SQLITE_OK ){
6427    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6428    if( rc!=SQLITE_OK ){
6429      raw_printf(stderr, "sql error: %s (%d)\n",
6430          sqlite3_errmsg(db), sqlite3_errcode(db)
6431      );
6432      *pRc = rc;
6433    }
6434  }
6435}
6436
6437/*
6438** Create a prepared statement using printf-style arguments for the SQL.
6439**
6440** This routine is could be marked "static".  But it is not always used,
6441** depending on compile-time options.  By omitting the "static", we avoid
6442** nuisance compiler warnings about "defined but not used".
6443*/
6444void shellPreparePrintf(
6445  sqlite3 *db,
6446  int *pRc,
6447  sqlite3_stmt **ppStmt,
6448  const char *zFmt,
6449  ...
6450){
6451  *ppStmt = 0;
6452  if( *pRc==SQLITE_OK ){
6453    va_list ap;
6454    char *z;
6455    va_start(ap, zFmt);
6456    z = sqlite3_vmprintf(zFmt, ap);
6457    va_end(ap);
6458    if( z==0 ){
6459      *pRc = SQLITE_NOMEM;
6460    }else{
6461      shellPrepare(db, pRc, z, ppStmt);
6462      sqlite3_free(z);
6463    }
6464  }
6465}
6466
6467/* Finalize the prepared statement created using shellPreparePrintf().
6468**
6469** This routine is could be marked "static".  But it is not always used,
6470** depending on compile-time options.  By omitting the "static", we avoid
6471** nuisance compiler warnings about "defined but not used".
6472*/
6473void shellFinalize(
6474  int *pRc,
6475  sqlite3_stmt *pStmt
6476){
6477  if( pStmt ){
6478    sqlite3 *db = sqlite3_db_handle(pStmt);
6479    int rc = sqlite3_finalize(pStmt);
6480    if( *pRc==SQLITE_OK ){
6481      if( rc!=SQLITE_OK ){
6482        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6483      }
6484      *pRc = rc;
6485    }
6486  }
6487}
6488
6489/* Reset the prepared statement created using shellPreparePrintf().
6490**
6491** This routine is could be marked "static".  But it is not always used,
6492** depending on compile-time options.  By omitting the "static", we avoid
6493** nuisance compiler warnings about "defined but not used".
6494*/
6495void shellReset(
6496  int *pRc,
6497  sqlite3_stmt *pStmt
6498){
6499  int rc = sqlite3_reset(pStmt);
6500  if( *pRc==SQLITE_OK ){
6501    if( rc!=SQLITE_OK ){
6502      sqlite3 *db = sqlite3_db_handle(pStmt);
6503      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6504    }
6505    *pRc = rc;
6506  }
6507}
6508#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6509
6510#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6511/******************************************************************************
6512** The ".archive" or ".ar" command.
6513*/
6514/*
6515** Structure representing a single ".ar" command.
6516*/
6517typedef struct ArCommand ArCommand;
6518struct ArCommand {
6519  u8 eCmd;                        /* An AR_CMD_* value */
6520  u8 bVerbose;                    /* True if --verbose */
6521  u8 bZip;                        /* True if the archive is a ZIP */
6522  u8 bDryRun;                     /* True if --dry-run */
6523  u8 bAppend;                     /* True if --append */
6524  u8 bGlob;                       /* True if --glob */
6525  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6526  int nArg;                       /* Number of command arguments */
6527  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6528  const char *zFile;              /* --file argument, or NULL */
6529  const char *zDir;               /* --directory argument, or NULL */
6530  char **azArg;                   /* Array of command arguments */
6531  ShellState *p;                  /* Shell state */
6532  sqlite3 *db;                    /* Database containing the archive */
6533};
6534
6535/*
6536** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6537*/
6538static int arUsage(FILE *f){
6539  showHelp(f,"archive");
6540  return SQLITE_ERROR;
6541}
6542
6543/*
6544** Print an error message for the .ar command to stderr and return
6545** SQLITE_ERROR.
6546*/
6547static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6548  va_list ap;
6549  char *z;
6550  va_start(ap, zFmt);
6551  z = sqlite3_vmprintf(zFmt, ap);
6552  va_end(ap);
6553  utf8_printf(stderr, "Error: %s\n", z);
6554  if( pAr->fromCmdLine ){
6555    utf8_printf(stderr, "Use \"-A\" for more help\n");
6556  }else{
6557    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6558  }
6559  sqlite3_free(z);
6560  return SQLITE_ERROR;
6561}
6562
6563/*
6564** Values for ArCommand.eCmd.
6565*/
6566#define AR_CMD_CREATE       1
6567#define AR_CMD_UPDATE       2
6568#define AR_CMD_INSERT       3
6569#define AR_CMD_EXTRACT      4
6570#define AR_CMD_LIST         5
6571#define AR_CMD_HELP         6
6572#define AR_CMD_REMOVE       7
6573
6574/*
6575** Other (non-command) switches.
6576*/
6577#define AR_SWITCH_VERBOSE     8
6578#define AR_SWITCH_FILE        9
6579#define AR_SWITCH_DIRECTORY  10
6580#define AR_SWITCH_APPEND     11
6581#define AR_SWITCH_DRYRUN     12
6582#define AR_SWITCH_GLOB       13
6583
6584static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6585  switch( eSwitch ){
6586    case AR_CMD_CREATE:
6587    case AR_CMD_EXTRACT:
6588    case AR_CMD_LIST:
6589    case AR_CMD_REMOVE:
6590    case AR_CMD_UPDATE:
6591    case AR_CMD_INSERT:
6592    case AR_CMD_HELP:
6593      if( pAr->eCmd ){
6594        return arErrorMsg(pAr, "multiple command options");
6595      }
6596      pAr->eCmd = eSwitch;
6597      break;
6598
6599    case AR_SWITCH_DRYRUN:
6600      pAr->bDryRun = 1;
6601      break;
6602    case AR_SWITCH_GLOB:
6603      pAr->bGlob = 1;
6604      break;
6605    case AR_SWITCH_VERBOSE:
6606      pAr->bVerbose = 1;
6607      break;
6608    case AR_SWITCH_APPEND:
6609      pAr->bAppend = 1;
6610      /* Fall thru into --file */
6611    case AR_SWITCH_FILE:
6612      pAr->zFile = zArg;
6613      break;
6614    case AR_SWITCH_DIRECTORY:
6615      pAr->zDir = zArg;
6616      break;
6617  }
6618
6619  return SQLITE_OK;
6620}
6621
6622/*
6623** Parse the command line for an ".ar" command. The results are written into
6624** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6625** successfully, otherwise an error message is written to stderr and
6626** SQLITE_ERROR returned.
6627*/
6628static int arParseCommand(
6629  char **azArg,                   /* Array of arguments passed to dot command */
6630  int nArg,                       /* Number of entries in azArg[] */
6631  ArCommand *pAr                  /* Populate this object */
6632){
6633  struct ArSwitch {
6634    const char *zLong;
6635    char cShort;
6636    u8 eSwitch;
6637    u8 bArg;
6638  } aSwitch[] = {
6639    { "create",    'c', AR_CMD_CREATE,       0 },
6640    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6641    { "insert",    'i', AR_CMD_INSERT,       0 },
6642    { "list",      't', AR_CMD_LIST,         0 },
6643    { "remove",    'r', AR_CMD_REMOVE,       0 },
6644    { "update",    'u', AR_CMD_UPDATE,       0 },
6645    { "help",      'h', AR_CMD_HELP,         0 },
6646    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6647    { "file",      'f', AR_SWITCH_FILE,      1 },
6648    { "append",    'a', AR_SWITCH_APPEND,    1 },
6649    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6650    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6651    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6652  };
6653  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6654  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6655
6656  if( nArg<=1 ){
6657    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6658    return arUsage(stderr);
6659  }else{
6660    char *z = azArg[1];
6661    if( z[0]!='-' ){
6662      /* Traditional style [tar] invocation */
6663      int i;
6664      int iArg = 2;
6665      for(i=0; z[i]; i++){
6666        const char *zArg = 0;
6667        struct ArSwitch *pOpt;
6668        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6669          if( z[i]==pOpt->cShort ) break;
6670        }
6671        if( pOpt==pEnd ){
6672          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6673        }
6674        if( pOpt->bArg ){
6675          if( iArg>=nArg ){
6676            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6677          }
6678          zArg = azArg[iArg++];
6679        }
6680        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6681      }
6682      pAr->nArg = nArg-iArg;
6683      if( pAr->nArg>0 ){
6684        pAr->azArg = &azArg[iArg];
6685      }
6686    }else{
6687      /* Non-traditional invocation */
6688      int iArg;
6689      for(iArg=1; iArg<nArg; iArg++){
6690        int n;
6691        z = azArg[iArg];
6692        if( z[0]!='-' ){
6693          /* All remaining command line words are command arguments. */
6694          pAr->azArg = &azArg[iArg];
6695          pAr->nArg = nArg-iArg;
6696          break;
6697        }
6698        n = strlen30(z);
6699
6700        if( z[1]!='-' ){
6701          int i;
6702          /* One or more short options */
6703          for(i=1; i<n; i++){
6704            const char *zArg = 0;
6705            struct ArSwitch *pOpt;
6706            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6707              if( z[i]==pOpt->cShort ) break;
6708            }
6709            if( pOpt==pEnd ){
6710              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6711            }
6712            if( pOpt->bArg ){
6713              if( i<(n-1) ){
6714                zArg = &z[i+1];
6715                i = n;
6716              }else{
6717                if( iArg>=(nArg-1) ){
6718                  return arErrorMsg(pAr, "option requires an argument: %c",
6719                                    z[i]);
6720                }
6721                zArg = azArg[++iArg];
6722              }
6723            }
6724            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6725          }
6726        }else if( z[2]=='\0' ){
6727          /* A -- option, indicating that all remaining command line words
6728          ** are command arguments.  */
6729          pAr->azArg = &azArg[iArg+1];
6730          pAr->nArg = nArg-iArg-1;
6731          break;
6732        }else{
6733          /* A long option */
6734          const char *zArg = 0;             /* Argument for option, if any */
6735          struct ArSwitch *pMatch = 0;      /* Matching option */
6736          struct ArSwitch *pOpt;            /* Iterator */
6737          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6738            const char *zLong = pOpt->zLong;
6739            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6740              if( pMatch ){
6741                return arErrorMsg(pAr, "ambiguous option: %s",z);
6742              }else{
6743                pMatch = pOpt;
6744              }
6745            }
6746          }
6747
6748          if( pMatch==0 ){
6749            return arErrorMsg(pAr, "unrecognized option: %s", z);
6750          }
6751          if( pMatch->bArg ){
6752            if( iArg>=(nArg-1) ){
6753              return arErrorMsg(pAr, "option requires an argument: %s", z);
6754            }
6755            zArg = azArg[++iArg];
6756          }
6757          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6758        }
6759      }
6760    }
6761  }
6762
6763  return SQLITE_OK;
6764}
6765
6766/*
6767** This function assumes that all arguments within the ArCommand.azArg[]
6768** array refer to archive members, as for the --extract, --list or --remove
6769** commands. It checks that each of them are "present". If any specified
6770** file is not present in the archive, an error is printed to stderr and an
6771** error code returned. Otherwise, if all specified arguments are present
6772** in the archive, SQLITE_OK is returned. Here, "present" means either an
6773** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6774** when pAr->bGlob is true.
6775**
6776** This function strips any trailing '/' characters from each argument.
6777** This is consistent with the way the [tar] command seems to work on
6778** Linux.
6779*/
6780static int arCheckEntries(ArCommand *pAr){
6781  int rc = SQLITE_OK;
6782  if( pAr->nArg ){
6783    int i, j;
6784    sqlite3_stmt *pTest = 0;
6785    const char *zSel = (pAr->bGlob)
6786      ? "SELECT name FROM %s WHERE glob($name,name)"
6787      : "SELECT name FROM %s WHERE name=$name";
6788
6789    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6790    j = sqlite3_bind_parameter_index(pTest, "$name");
6791    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6792      char *z = pAr->azArg[i];
6793      int n = strlen30(z);
6794      int bOk = 0;
6795      while( n>0 && z[n-1]=='/' ) n--;
6796      z[n] = '\0';
6797      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6798      if( SQLITE_ROW==sqlite3_step(pTest) ){
6799        bOk = 1;
6800      }
6801      shellReset(&rc, pTest);
6802      if( rc==SQLITE_OK && bOk==0 ){
6803        utf8_printf(stderr, "not found in archive: %s\n", z);
6804        rc = SQLITE_ERROR;
6805      }
6806    }
6807    shellFinalize(&rc, pTest);
6808  }
6809  return rc;
6810}
6811
6812/*
6813** Format a WHERE clause that can be used against the "sqlar" table to
6814** identify all archive members that match the command arguments held
6815** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6816** The caller is responsible for eventually calling sqlite3_free() on
6817** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6818** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6819*/
6820static void arWhereClause(
6821  int *pRc,
6822  ArCommand *pAr,
6823  char **pzWhere                  /* OUT: New WHERE clause */
6824){
6825  char *zWhere = 0;
6826  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6827  if( *pRc==SQLITE_OK ){
6828    if( pAr->nArg==0 ){
6829      zWhere = sqlite3_mprintf("1");
6830    }else{
6831      int i;
6832      const char *zSep = "";
6833      for(i=0; i<pAr->nArg; i++){
6834        const char *z = pAr->azArg[i];
6835        zWhere = sqlite3_mprintf(
6836          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6837          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6838        );
6839        if( zWhere==0 ){
6840          *pRc = SQLITE_NOMEM;
6841          break;
6842        }
6843        zSep = " OR ";
6844      }
6845    }
6846  }
6847  *pzWhere = zWhere;
6848}
6849
6850/*
6851** Implementation of .ar "lisT" command.
6852*/
6853static int arListCommand(ArCommand *pAr){
6854  const char *zSql = "SELECT %s FROM %s WHERE %s";
6855  const char *azCols[] = {
6856    "name",
6857    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6858  };
6859
6860  char *zWhere = 0;
6861  sqlite3_stmt *pSql = 0;
6862  int rc;
6863
6864  rc = arCheckEntries(pAr);
6865  arWhereClause(&rc, pAr, &zWhere);
6866
6867  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6868                     pAr->zSrcTable, zWhere);
6869  if( pAr->bDryRun ){
6870    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6871  }else{
6872    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6873      if( pAr->bVerbose ){
6874        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6875            sqlite3_column_text(pSql, 0),
6876            sqlite3_column_int(pSql, 1),
6877            sqlite3_column_text(pSql, 2),
6878            sqlite3_column_text(pSql, 3)
6879        );
6880      }else{
6881        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6882      }
6883    }
6884  }
6885  shellFinalize(&rc, pSql);
6886  sqlite3_free(zWhere);
6887  return rc;
6888}
6889
6890
6891/*
6892** Implementation of .ar "Remove" command.
6893*/
6894static int arRemoveCommand(ArCommand *pAr){
6895  int rc = 0;
6896  char *zSql = 0;
6897  char *zWhere = 0;
6898
6899  if( pAr->nArg ){
6900    /* Verify that args actually exist within the archive before proceeding.
6901    ** And formulate a WHERE clause to match them.  */
6902    rc = arCheckEntries(pAr);
6903    arWhereClause(&rc, pAr, &zWhere);
6904  }
6905  if( rc==SQLITE_OK ){
6906    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6907                           pAr->zSrcTable, zWhere);
6908    if( pAr->bDryRun ){
6909      utf8_printf(pAr->p->out, "%s\n", zSql);
6910    }else{
6911      char *zErr = 0;
6912      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6913      if( rc==SQLITE_OK ){
6914        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6915        if( rc!=SQLITE_OK ){
6916          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6917        }else{
6918          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6919        }
6920      }
6921      if( zErr ){
6922        utf8_printf(stdout, "ERROR: %s\n", zErr);
6923        sqlite3_free(zErr);
6924      }
6925    }
6926  }
6927  sqlite3_free(zWhere);
6928  sqlite3_free(zSql);
6929  return rc;
6930}
6931
6932/*
6933** Implementation of .ar "eXtract" command.
6934*/
6935static int arExtractCommand(ArCommand *pAr){
6936  const char *zSql1 =
6937    "SELECT "
6938    " ($dir || name),"
6939    " writefile(($dir || name), %s, mode, mtime) "
6940    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6941    " AND name NOT GLOB '*..[/\\]*'";
6942
6943  const char *azExtraArg[] = {
6944    "sqlar_uncompress(data, sz)",
6945    "data"
6946  };
6947
6948  sqlite3_stmt *pSql = 0;
6949  int rc = SQLITE_OK;
6950  char *zDir = 0;
6951  char *zWhere = 0;
6952  int i, j;
6953
6954  /* If arguments are specified, check that they actually exist within
6955  ** the archive before proceeding. And formulate a WHERE clause to
6956  ** match them.  */
6957  rc = arCheckEntries(pAr);
6958  arWhereClause(&rc, pAr, &zWhere);
6959
6960  if( rc==SQLITE_OK ){
6961    if( pAr->zDir ){
6962      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6963    }else{
6964      zDir = sqlite3_mprintf("");
6965    }
6966    if( zDir==0 ) rc = SQLITE_NOMEM;
6967  }
6968
6969  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6970      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6971  );
6972
6973  if( rc==SQLITE_OK ){
6974    j = sqlite3_bind_parameter_index(pSql, "$dir");
6975    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6976
6977    /* Run the SELECT statement twice. The first time, writefile() is called
6978    ** for all archive members that should be extracted. The second time,
6979    ** only for the directories. This is because the timestamps for
6980    ** extracted directories must be reset after they are populated (as
6981    ** populating them changes the timestamp).  */
6982    for(i=0; i<2; i++){
6983      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6984      sqlite3_bind_int(pSql, j, i);
6985      if( pAr->bDryRun ){
6986        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6987      }else{
6988        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6989          if( i==0 && pAr->bVerbose ){
6990            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6991          }
6992        }
6993      }
6994      shellReset(&rc, pSql);
6995    }
6996    shellFinalize(&rc, pSql);
6997  }
6998
6999  sqlite3_free(zDir);
7000  sqlite3_free(zWhere);
7001  return rc;
7002}
7003
7004/*
7005** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
7006*/
7007static int arExecSql(ArCommand *pAr, const char *zSql){
7008  int rc;
7009  if( pAr->bDryRun ){
7010    utf8_printf(pAr->p->out, "%s\n", zSql);
7011    rc = SQLITE_OK;
7012  }else{
7013    char *zErr = 0;
7014    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7015    if( zErr ){
7016      utf8_printf(stdout, "ERROR: %s\n", zErr);
7017      sqlite3_free(zErr);
7018    }
7019  }
7020  return rc;
7021}
7022
7023
7024/*
7025** Implementation of .ar "create", "insert", and "update" commands.
7026**
7027**     create    ->     Create a new SQL archive
7028**     insert    ->     Insert or reinsert all files listed
7029**     update    ->     Insert files that have changed or that were not
7030**                      previously in the archive
7031**
7032** Create the "sqlar" table in the database if it does not already exist.
7033** Then add each file in the azFile[] array to the archive. Directories
7034** are added recursively. If argument bVerbose is non-zero, a message is
7035** printed on stdout for each file archived.
7036**
7037** The create command is the same as update, except that it drops
7038** any existing "sqlar" table before beginning.  The "insert" command
7039** always overwrites every file named on the command-line, where as
7040** "update" only overwrites if the size or mtime or mode has changed.
7041*/
7042static int arCreateOrUpdateCommand(
7043  ArCommand *pAr,                 /* Command arguments and options */
7044  int bUpdate,                    /* true for a --create. */
7045  int bOnlyIfChanged              /* Only update if file has changed */
7046){
7047  const char *zCreate =
7048      "CREATE TABLE IF NOT EXISTS sqlar(\n"
7049      "  name TEXT PRIMARY KEY,  -- name of the file\n"
7050      "  mode INT,               -- access permissions\n"
7051      "  mtime INT,              -- last modification time\n"
7052      "  sz INT,                 -- original file size\n"
7053      "  data BLOB               -- compressed content\n"
7054      ")";
7055  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7056  const char *zInsertFmt[2] = {
7057     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7058     "  SELECT\n"
7059     "    %s,\n"
7060     "    mode,\n"
7061     "    mtime,\n"
7062     "    CASE substr(lsmode(mode),1,1)\n"
7063     "      WHEN '-' THEN length(data)\n"
7064     "      WHEN 'd' THEN 0\n"
7065     "      ELSE -1 END,\n"
7066     "    sqlar_compress(data)\n"
7067     "  FROM fsdir(%Q,%Q) AS disk\n"
7068     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7069     ,
7070     "REPLACE INTO %s(name,mode,mtime,data)\n"
7071     "  SELECT\n"
7072     "    %s,\n"
7073     "    mode,\n"
7074     "    mtime,\n"
7075     "    data\n"
7076     "  FROM fsdir(%Q,%Q) AS disk\n"
7077     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7078  };
7079  int i;                          /* For iterating through azFile[] */
7080  int rc;                         /* Return code */
7081  const char *zTab = 0;           /* SQL table into which to insert */
7082  char *zSql;
7083  char zTemp[50];
7084  char *zExists = 0;
7085
7086  arExecSql(pAr, "PRAGMA page_size=512");
7087  rc = arExecSql(pAr, "SAVEPOINT ar;");
7088  if( rc!=SQLITE_OK ) return rc;
7089  zTemp[0] = 0;
7090  if( pAr->bZip ){
7091    /* Initialize the zipfile virtual table, if necessary */
7092    if( pAr->zFile ){
7093      sqlite3_uint64 r;
7094      sqlite3_randomness(sizeof(r),&r);
7095      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7096      zTab = zTemp;
7097      zSql = sqlite3_mprintf(
7098         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7099         zTab, pAr->zFile
7100      );
7101      rc = arExecSql(pAr, zSql);
7102      sqlite3_free(zSql);
7103    }else{
7104      zTab = "zip";
7105    }
7106  }else{
7107    /* Initialize the table for an SQLAR */
7108    zTab = "sqlar";
7109    if( bUpdate==0 ){
7110      rc = arExecSql(pAr, zDrop);
7111      if( rc!=SQLITE_OK ) goto end_ar_transaction;
7112    }
7113    rc = arExecSql(pAr, zCreate);
7114  }
7115  if( bOnlyIfChanged ){
7116    zExists = sqlite3_mprintf(
7117      " AND NOT EXISTS("
7118          "SELECT 1 FROM %s AS mem"
7119          " WHERE mem.name=disk.name"
7120          " AND mem.mtime=disk.mtime"
7121          " AND mem.mode=disk.mode)", zTab);
7122  }else{
7123    zExists = sqlite3_mprintf("");
7124  }
7125  if( zExists==0 ) rc = SQLITE_NOMEM;
7126  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7127    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7128        pAr->bVerbose ? "shell_putsnl(name)" : "name",
7129        pAr->azArg[i], pAr->zDir, zExists);
7130    rc = arExecSql(pAr, zSql2);
7131    sqlite3_free(zSql2);
7132  }
7133end_ar_transaction:
7134  if( rc!=SQLITE_OK ){
7135    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7136  }else{
7137    rc = arExecSql(pAr, "RELEASE ar;");
7138    if( pAr->bZip && pAr->zFile ){
7139      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7140      arExecSql(pAr, zSql);
7141      sqlite3_free(zSql);
7142    }
7143  }
7144  sqlite3_free(zExists);
7145  return rc;
7146}
7147
7148/*
7149** Implementation of ".ar" dot command.
7150*/
7151static int arDotCommand(
7152  ShellState *pState,          /* Current shell tool state */
7153  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
7154  char **azArg,                /* Array of arguments passed to dot command */
7155  int nArg                     /* Number of entries in azArg[] */
7156){
7157  ArCommand cmd;
7158  int rc;
7159  memset(&cmd, 0, sizeof(cmd));
7160  cmd.fromCmdLine = fromCmdLine;
7161  rc = arParseCommand(azArg, nArg, &cmd);
7162  if( rc==SQLITE_OK ){
7163    int eDbType = SHELL_OPEN_UNSPEC;
7164    cmd.p = pState;
7165    cmd.db = pState->db;
7166    if( cmd.zFile ){
7167      eDbType = deduceDatabaseType(cmd.zFile, 1);
7168    }else{
7169      eDbType = pState->openMode;
7170    }
7171    if( eDbType==SHELL_OPEN_ZIPFILE ){
7172      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7173        if( cmd.zFile==0 ){
7174          cmd.zSrcTable = sqlite3_mprintf("zip");
7175        }else{
7176          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7177        }
7178      }
7179      cmd.bZip = 1;
7180    }else if( cmd.zFile ){
7181      int flags;
7182      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7183      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7184           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7185        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7186      }else{
7187        flags = SQLITE_OPEN_READONLY;
7188      }
7189      cmd.db = 0;
7190      if( cmd.bDryRun ){
7191        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7192             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7193      }
7194      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7195             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7196      if( rc!=SQLITE_OK ){
7197        utf8_printf(stderr, "cannot open file: %s (%s)\n",
7198            cmd.zFile, sqlite3_errmsg(cmd.db)
7199        );
7200        goto end_ar_command;
7201      }
7202      sqlite3_fileio_init(cmd.db, 0, 0);
7203      sqlite3_sqlar_init(cmd.db, 0, 0);
7204      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7205                              shellPutsFunc, 0, 0);
7206
7207    }
7208    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7209      if( cmd.eCmd!=AR_CMD_CREATE
7210       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7211      ){
7212        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7213        rc = SQLITE_ERROR;
7214        goto end_ar_command;
7215      }
7216      cmd.zSrcTable = sqlite3_mprintf("sqlar");
7217    }
7218
7219    switch( cmd.eCmd ){
7220      case AR_CMD_CREATE:
7221        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7222        break;
7223
7224      case AR_CMD_EXTRACT:
7225        rc = arExtractCommand(&cmd);
7226        break;
7227
7228      case AR_CMD_LIST:
7229        rc = arListCommand(&cmd);
7230        break;
7231
7232      case AR_CMD_HELP:
7233        arUsage(pState->out);
7234        break;
7235
7236      case AR_CMD_INSERT:
7237        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7238        break;
7239
7240      case AR_CMD_REMOVE:
7241        rc = arRemoveCommand(&cmd);
7242        break;
7243
7244      default:
7245        assert( cmd.eCmd==AR_CMD_UPDATE );
7246        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7247        break;
7248    }
7249  }
7250end_ar_command:
7251  if( cmd.db!=pState->db ){
7252    close_db(cmd.db);
7253  }
7254  sqlite3_free(cmd.zSrcTable);
7255
7256  return rc;
7257}
7258/* End of the ".archive" or ".ar" command logic
7259*******************************************************************************/
7260#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7261
7262#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7263/*
7264** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
7265** Otherwise, the SQL statement or statements in zSql are executed using
7266** database connection db and the error code written to *pRc before
7267** this function returns.
7268*/
7269static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
7270  int rc = *pRc;
7271  if( rc==SQLITE_OK ){
7272    char *zErr = 0;
7273    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
7274    if( rc!=SQLITE_OK ){
7275      raw_printf(stderr, "SQL error: %s\n", zErr);
7276    }
7277    sqlite3_free(zErr);
7278    *pRc = rc;
7279  }
7280}
7281
7282/*
7283** Like shellExec(), except that zFmt is a printf() style format string.
7284*/
7285static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
7286  char *z = 0;
7287  if( *pRc==SQLITE_OK ){
7288    va_list ap;
7289    va_start(ap, zFmt);
7290    z = sqlite3_vmprintf(zFmt, ap);
7291    va_end(ap);
7292    if( z==0 ){
7293      *pRc = SQLITE_NOMEM;
7294    }else{
7295      shellExec(db, pRc, z);
7296    }
7297    sqlite3_free(z);
7298  }
7299}
7300
7301/*
7302** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7303** Otherwise, an attempt is made to allocate, zero and return a pointer
7304** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
7305** to SQLITE_NOMEM and NULL returned.
7306*/
7307static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
7308  void *pRet = 0;
7309  if( *pRc==SQLITE_OK ){
7310    pRet = sqlite3_malloc64(nByte);
7311    if( pRet==0 ){
7312      *pRc = SQLITE_NOMEM;
7313    }else{
7314      memset(pRet, 0, nByte);
7315    }
7316  }
7317  return pRet;
7318}
7319
7320/*
7321** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7322** Otherwise, zFmt is treated as a printf() style string. The result of
7323** formatting it along with any trailing arguments is written into a
7324** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
7325** It is the responsibility of the caller to eventually free this buffer
7326** using a call to sqlite3_free().
7327**
7328** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
7329** pointer returned.
7330*/
7331static char *shellMPrintf(int *pRc, const char *zFmt, ...){
7332  char *z = 0;
7333  if( *pRc==SQLITE_OK ){
7334    va_list ap;
7335    va_start(ap, zFmt);
7336    z = sqlite3_vmprintf(zFmt, ap);
7337    va_end(ap);
7338    if( z==0 ){
7339      *pRc = SQLITE_NOMEM;
7340    }
7341  }
7342  return z;
7343}
7344
7345
7346/*
7347** When running the ".recover" command, each output table, and the special
7348** orphaned row table if it is required, is represented by an instance
7349** of the following struct.
7350*/
7351typedef struct RecoverTable RecoverTable;
7352struct RecoverTable {
7353  char *zQuoted;                  /* Quoted version of table name */
7354  int nCol;                       /* Number of columns in table */
7355  char **azlCol;                  /* Array of column lists */
7356  int iPk;                        /* Index of IPK column */
7357};
7358
7359/*
7360** Free a RecoverTable object allocated by recoverFindTable() or
7361** recoverOrphanTable().
7362*/
7363static void recoverFreeTable(RecoverTable *pTab){
7364  if( pTab ){
7365    sqlite3_free(pTab->zQuoted);
7366    if( pTab->azlCol ){
7367      int i;
7368      for(i=0; i<=pTab->nCol; i++){
7369        sqlite3_free(pTab->azlCol[i]);
7370      }
7371      sqlite3_free(pTab->azlCol);
7372    }
7373    sqlite3_free(pTab);
7374  }
7375}
7376
7377/*
7378** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
7379** Otherwise, it allocates and returns a RecoverTable object based on the
7380** final four arguments passed to this function. It is the responsibility
7381** of the caller to eventually free the returned object using
7382** recoverFreeTable().
7383*/
7384static RecoverTable *recoverNewTable(
7385  int *pRc,                       /* IN/OUT: Error code */
7386  const char *zName,              /* Name of table */
7387  const char *zSql,               /* CREATE TABLE statement */
7388  int bIntkey,
7389  int nCol
7390){
7391  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
7392  int rc = *pRc;
7393  RecoverTable *pTab = 0;
7394
7395  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
7396  if( rc==SQLITE_OK ){
7397    int nSqlCol = 0;
7398    int bSqlIntkey = 0;
7399    sqlite3_stmt *pStmt = 0;
7400
7401    rc = sqlite3_open("", &dbtmp);
7402    if( rc==SQLITE_OK ){
7403      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
7404                              shellIdQuote, 0, 0);
7405    }
7406    if( rc==SQLITE_OK ){
7407      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
7408    }
7409    if( rc==SQLITE_OK ){
7410      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
7411      if( rc==SQLITE_ERROR ){
7412        rc = SQLITE_OK;
7413        goto finished;
7414      }
7415    }
7416    shellPreparePrintf(dbtmp, &rc, &pStmt,
7417        "SELECT count(*) FROM pragma_table_info(%Q)", zName
7418    );
7419    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7420      nSqlCol = sqlite3_column_int(pStmt, 0);
7421    }
7422    shellFinalize(&rc, pStmt);
7423
7424    if( rc!=SQLITE_OK || nSqlCol<nCol ){
7425      goto finished;
7426    }
7427
7428    shellPreparePrintf(dbtmp, &rc, &pStmt,
7429      "SELECT ("
7430      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
7431      ") FROM sqlite_schema WHERE name = %Q", zName
7432    );
7433    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7434      bSqlIntkey = sqlite3_column_int(pStmt, 0);
7435    }
7436    shellFinalize(&rc, pStmt);
7437
7438    if( bIntkey==bSqlIntkey ){
7439      int i;
7440      const char *zPk = "_rowid_";
7441      sqlite3_stmt *pPkFinder = 0;
7442
7443      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
7444      ** set zPk to the name of the PK column, and pTab->iPk to the index
7445      ** of the column, where columns are 0-numbered from left to right.
7446      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7447      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7448      pTab->iPk = -2;
7449      if( bIntkey ){
7450        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7451          "SELECT cid, name FROM pragma_table_info(%Q) "
7452          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7453          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7454          , zName, zName
7455        );
7456        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7457          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7458          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7459          if( zPk==0 ){ zPk = "_";  /* Defensive.  Should never happen */ }
7460        }
7461      }
7462
7463      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7464      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7465      pTab->nCol = nSqlCol;
7466
7467      if( bIntkey ){
7468        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7469      }else{
7470        pTab->azlCol[0] = shellMPrintf(&rc, "");
7471      }
7472      i = 1;
7473      shellPreparePrintf(dbtmp, &rc, &pStmt,
7474          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7475          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7476          "FROM pragma_table_info(%Q)",
7477          bIntkey ? ", " : "", pTab->iPk,
7478          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7479          zName
7480      );
7481      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7482        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7483        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7484        i++;
7485      }
7486      shellFinalize(&rc, pStmt);
7487
7488      shellFinalize(&rc, pPkFinder);
7489    }
7490  }
7491
7492 finished:
7493  sqlite3_close(dbtmp);
7494  *pRc = rc;
7495  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7496    recoverFreeTable(pTab);
7497    pTab = 0;
7498  }
7499  return pTab;
7500}
7501
7502/*
7503** This function is called to search the schema recovered from the
7504** sqlite_schema table of the (possibly) corrupt database as part
7505** of a ".recover" command. Specifically, for a table with root page
7506** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7507** table must be a WITHOUT ROWID table, or if non-zero, not one of
7508** those.
7509**
7510** If a table is found, a (RecoverTable*) object is returned. Or, if
7511** no such table is found, but bIntkey is false and iRoot is the
7512** root page of an index in the recovered schema, then (*pbNoop) is
7513** set to true and NULL returned. Or, if there is no such table or
7514** index, NULL is returned and (*pbNoop) set to 0, indicating that
7515** the caller should write data to the orphans table.
7516*/
7517static RecoverTable *recoverFindTable(
7518  ShellState *pState,             /* Shell state object */
7519  int *pRc,                       /* IN/OUT: Error code */
7520  int iRoot,                      /* Root page of table */
7521  int bIntkey,                    /* True for an intkey table */
7522  int nCol,                       /* Number of columns in table */
7523  int *pbNoop                     /* OUT: True if iRoot is root of index */
7524){
7525  sqlite3_stmt *pStmt = 0;
7526  RecoverTable *pRet = 0;
7527  int bNoop = 0;
7528  const char *zSql = 0;
7529  const char *zName = 0;
7530
7531  /* Search the recovered schema for an object with root page iRoot. */
7532  shellPreparePrintf(pState->db, pRc, &pStmt,
7533      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7534  );
7535  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7536    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7537    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7538      bNoop = 1;
7539      break;
7540    }
7541    if( sqlite3_stricmp(zType, "table")==0 ){
7542      zName = (const char*)sqlite3_column_text(pStmt, 1);
7543      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7544      if( zName!=0 && zSql!=0 ){
7545        pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7546        break;
7547      }
7548    }
7549  }
7550
7551  shellFinalize(pRc, pStmt);
7552  *pbNoop = bNoop;
7553  return pRet;
7554}
7555
7556/*
7557** Return a RecoverTable object representing the orphans table.
7558*/
7559static RecoverTable *recoverOrphanTable(
7560  ShellState *pState,             /* Shell state object */
7561  int *pRc,                       /* IN/OUT: Error code */
7562  const char *zLostAndFound,      /* Base name for orphans table */
7563  int nCol                        /* Number of user data columns */
7564){
7565  RecoverTable *pTab = 0;
7566  if( nCol>=0 && *pRc==SQLITE_OK ){
7567    int i;
7568
7569    /* This block determines the name of the orphan table. The prefered
7570    ** name is zLostAndFound. But if that clashes with another name
7571    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7572    ** and so on until a non-clashing name is found.  */
7573    int iTab = 0;
7574    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7575    sqlite3_stmt *pTest = 0;
7576    shellPrepare(pState->db, pRc,
7577        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7578    );
7579    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7580    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7581      shellReset(pRc, pTest);
7582      sqlite3_free(zTab);
7583      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7584      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7585    }
7586    shellFinalize(pRc, pTest);
7587
7588    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7589    if( pTab ){
7590      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7591      pTab->nCol = nCol;
7592      pTab->iPk = -2;
7593      if( nCol>0 ){
7594        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7595        if( pTab->azlCol ){
7596          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7597          for(i=nCol-1; i>=0; i--){
7598            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7599          }
7600        }
7601      }
7602
7603      if( *pRc!=SQLITE_OK ){
7604        recoverFreeTable(pTab);
7605        pTab = 0;
7606      }else{
7607        raw_printf(pState->out,
7608            "CREATE TABLE %s(rootpgno INTEGER, "
7609            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7610        );
7611        for(i=0; i<nCol; i++){
7612          raw_printf(pState->out, ", c%d", i);
7613        }
7614        raw_printf(pState->out, ");\n");
7615      }
7616    }
7617    sqlite3_free(zTab);
7618  }
7619  return pTab;
7620}
7621
7622/*
7623** This function is called to recover data from the database. A script
7624** to construct a new database containing all recovered data is output
7625** on stream pState->out.
7626*/
7627static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7628  int rc = SQLITE_OK;
7629  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7630  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7631  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7632  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7633  const char *zLostAndFound = "lost_and_found";
7634  int i;
7635  int nOrphan = -1;
7636  RecoverTable *pOrphan = 0;
7637
7638  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7639  int bRowids = 1;                /* 0 if --no-rowids */
7640  for(i=1; i<nArg; i++){
7641    char *z = azArg[i];
7642    int n;
7643    if( z[0]=='-' && z[1]=='-' ) z++;
7644    n = strlen30(z);
7645    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7646      bFreelist = 0;
7647    }else
7648    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7649      i++;
7650      zRecoveryDb = azArg[i];
7651    }else
7652    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7653      i++;
7654      zLostAndFound = azArg[i];
7655    }else
7656    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7657      bRowids = 0;
7658    }
7659    else{
7660      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7661      showHelp(pState->out, azArg[0]);
7662      return 1;
7663    }
7664  }
7665
7666  shellExecPrintf(pState->db, &rc,
7667    /* Attach an in-memory database named 'recovery'. Create an indexed
7668    ** cache of the sqlite_dbptr virtual table. */
7669    "PRAGMA writable_schema = on;"
7670    "ATTACH %Q AS recovery;"
7671    "DROP TABLE IF EXISTS recovery.dbptr;"
7672    "DROP TABLE IF EXISTS recovery.freelist;"
7673    "DROP TABLE IF EXISTS recovery.map;"
7674    "DROP TABLE IF EXISTS recovery.schema;"
7675    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7676  );
7677
7678  if( bFreelist ){
7679    shellExec(pState->db, &rc,
7680      "WITH trunk(pgno) AS ("
7681      "  SELECT shell_int32("
7682      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7683      "      WHERE x>0"
7684      "    UNION"
7685      "  SELECT shell_int32("
7686      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7687      "      FROM trunk WHERE x>0"
7688      "),"
7689      "freelist(data, n, freepgno) AS ("
7690      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7691      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7692      "    UNION ALL"
7693      "  SELECT data, n-1, shell_int32(data, 2+n) "
7694      "      FROM freelist WHERE n>=0"
7695      ")"
7696      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7697    );
7698  }
7699
7700  /* If this is an auto-vacuum database, add all pointer-map pages to
7701  ** the freelist table. Do this regardless of whether or not
7702  ** --freelist-corrupt was specified.  */
7703  shellExec(pState->db, &rc,
7704    "WITH ptrmap(pgno) AS ("
7705    "  SELECT 2 WHERE shell_int32("
7706    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7707    "  )"
7708    "    UNION ALL "
7709    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7710    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7711    ")"
7712    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7713  );
7714
7715  shellExec(pState->db, &rc,
7716    "CREATE TABLE recovery.dbptr("
7717    "      pgno, child, PRIMARY KEY(child, pgno)"
7718    ") WITHOUT ROWID;"
7719    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7720    "    SELECT * FROM sqlite_dbptr"
7721    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7722
7723    /* Delete any pointer to page 1. This ensures that page 1 is considered
7724    ** a root page, regardless of how corrupt the db is. */
7725    "DELETE FROM recovery.dbptr WHERE child = 1;"
7726
7727    /* Delete all pointers to any pages that have more than one pointer
7728    ** to them. Such pages will be treated as root pages when recovering
7729    ** data.  */
7730    "DELETE FROM recovery.dbptr WHERE child IN ("
7731    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7732    ");"
7733
7734    /* Create the "map" table that will (eventually) contain instructions
7735    ** for dealing with each page in the db that contains one or more
7736    ** records. */
7737    "CREATE TABLE recovery.map("
7738      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7739    ");"
7740
7741    /* Populate table [map]. If there are circular loops of pages in the
7742    ** database, the following adds all pages in such a loop to the map
7743    ** as individual root pages. This could be handled better.  */
7744    "WITH pages(i, maxlen) AS ("
7745    "  SELECT page_count, ("
7746    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7747    "  ) FROM pragma_page_count WHERE page_count>0"
7748    "    UNION ALL"
7749    "  SELECT i-1, ("
7750    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7751    "  ) FROM pages WHERE i>=2"
7752    ")"
7753    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7754    "  SELECT i, maxlen, NULL, ("
7755    "    WITH p(orig, pgno, parent) AS ("
7756    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7757    "        UNION "
7758    "      SELECT i, p.parent, "
7759    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7760    "    )"
7761    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7762    ") "
7763    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7764    "UPDATE recovery.map AS o SET intkey = ("
7765    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7766    ");"
7767
7768    /* Extract data from page 1 and any linked pages into table
7769    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7770    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7771    "INSERT INTO recovery.schema SELECT "
7772    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7773    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7774    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7775    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7776    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7777    "FROM sqlite_dbdata WHERE pgno IN ("
7778    "  SELECT pgno FROM recovery.map WHERE root=1"
7779    ")"
7780    "GROUP BY pgno, cell;"
7781    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7782  );
7783
7784  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7785  ** CREATE TABLE statements that extracted from the existing schema.  */
7786  if( rc==SQLITE_OK ){
7787    sqlite3_stmt *pStmt = 0;
7788    /* ".recover" might output content in an order which causes immediate
7789    ** foreign key constraints to be violated. So disable foreign-key
7790    ** constraint enforcement to prevent problems when running the output
7791    ** script. */
7792    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7793    raw_printf(pState->out, "BEGIN;\n");
7794    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7795    shellPrepare(pState->db, &rc,
7796        "SELECT sql FROM recovery.schema "
7797        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7798    );
7799    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7800      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7801      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7802          &zCreateTable[12]
7803      );
7804    }
7805    shellFinalize(&rc, pStmt);
7806  }
7807
7808  /* Figure out if an orphan table will be required. And if so, how many
7809  ** user columns it should contain */
7810  shellPrepare(pState->db, &rc,
7811      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7812      , &pLoop
7813  );
7814  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7815    nOrphan = sqlite3_column_int(pLoop, 0);
7816  }
7817  shellFinalize(&rc, pLoop);
7818  pLoop = 0;
7819
7820  shellPrepare(pState->db, &rc,
7821      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7822  );
7823
7824  shellPrepare(pState->db, &rc,
7825      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7826      "(case when (? AND field<0) then NULL else value end)"
7827      "), ', ')"
7828      ", min(field) "
7829      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7830      "GROUP BY cell", &pCells
7831  );
7832
7833  /* Loop through each root page. */
7834  shellPrepare(pState->db, &rc,
7835      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7836      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7837      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7838      ")", &pLoop
7839  );
7840  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7841    int iRoot = sqlite3_column_int(pLoop, 0);
7842    int bIntkey = sqlite3_column_int(pLoop, 1);
7843    int nCol = sqlite3_column_int(pLoop, 2);
7844    int bNoop = 0;
7845    RecoverTable *pTab;
7846
7847    assert( bIntkey==0 || bIntkey==1 );
7848    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7849    if( bNoop || rc ) continue;
7850    if( pTab==0 ){
7851      if( pOrphan==0 ){
7852        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7853      }
7854      pTab = pOrphan;
7855      if( pTab==0 ) break;
7856    }
7857
7858    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7859      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7860    }
7861    sqlite3_bind_int(pPages, 1, iRoot);
7862    if( bRowids==0 && pTab->iPk<0 ){
7863      sqlite3_bind_int(pCells, 1, 1);
7864    }else{
7865      sqlite3_bind_int(pCells, 1, 0);
7866    }
7867    sqlite3_bind_int(pCells, 3, pTab->iPk);
7868
7869    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7870      int iPgno = sqlite3_column_int(pPages, 0);
7871      sqlite3_bind_int(pCells, 2, iPgno);
7872      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7873        int nField = sqlite3_column_int(pCells, 0);
7874        int iMin = sqlite3_column_int(pCells, 2);
7875        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7876
7877        RecoverTable *pTab2 = pTab;
7878        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7879          if( pOrphan==0 ){
7880            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7881          }
7882          pTab2 = pOrphan;
7883          if( pTab2==0 ) break;
7884        }
7885
7886        nField = nField+1;
7887        if( pTab2==pOrphan ){
7888          raw_printf(pState->out,
7889              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7890              pTab2->zQuoted, iRoot, iPgno, nField,
7891              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7892          );
7893        }else{
7894          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7895              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7896          );
7897        }
7898      }
7899      shellReset(&rc, pCells);
7900    }
7901    shellReset(&rc, pPages);
7902    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7903  }
7904  shellFinalize(&rc, pLoop);
7905  shellFinalize(&rc, pPages);
7906  shellFinalize(&rc, pCells);
7907  recoverFreeTable(pOrphan);
7908
7909  /* The rest of the schema */
7910  if( rc==SQLITE_OK ){
7911    sqlite3_stmt *pStmt = 0;
7912    shellPrepare(pState->db, &rc,
7913        "SELECT sql, name FROM recovery.schema "
7914        "WHERE sql NOT LIKE 'create table%'", &pStmt
7915    );
7916    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7917      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7918      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7919        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7920        char *zPrint = shellMPrintf(&rc,
7921          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7922          zName, zName, zSql
7923        );
7924        raw_printf(pState->out, "%s;\n", zPrint);
7925        sqlite3_free(zPrint);
7926      }else{
7927        raw_printf(pState->out, "%s;\n", zSql);
7928      }
7929    }
7930    shellFinalize(&rc, pStmt);
7931  }
7932
7933  if( rc==SQLITE_OK ){
7934    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7935    raw_printf(pState->out, "COMMIT;\n");
7936  }
7937  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7938  return rc;
7939}
7940#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7941
7942
7943/*
7944 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7945 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7946 *   close db and set it to 0, and return the columns spec, to later
7947 *   be sqlite3_free()'ed by the caller.
7948 * The return is 0 when either:
7949 *   (a) The db was not initialized and zCol==0 (There are no columns.)
7950 *   (b) zCol!=0  (Column was added, db initialized as needed.)
7951 * The 3rd argument, pRenamed, references an out parameter. If the
7952 * pointer is non-zero, its referent will be set to a summary of renames
7953 * done if renaming was necessary, or set to 0 if none was done. The out
7954 * string (if any) must be sqlite3_free()'ed by the caller.
7955 */
7956#ifdef SHELL_DEBUG
7957#define rc_err_oom_die(rc) \
7958  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7959  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7960    fprintf(stderr,"E:%d\n",rc), assert(0)
7961#else
7962static void rc_err_oom_die(int rc){
7963  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7964  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7965}
7966#endif
7967
7968#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7969static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7970#else  /* Otherwise, memory is faster/better for the transient DB. */
7971static const char *zCOL_DB = ":memory:";
7972#endif
7973
7974/* Define character (as C string) to separate generated column ordinal
7975 * from protected part of incoming column names. This defaults to "_"
7976 * so that incoming column identifiers that did not need not be quoted
7977 * remain usable without being quoted. It must be one character.
7978 */
7979#ifndef SHELL_AUTOCOLUMN_SEP
7980# define AUTOCOLUMN_SEP "_"
7981#else
7982# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7983#endif
7984
7985static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
7986  /* Queries and D{D,M}L used here */
7987  static const char * const zTabMake = "\
7988CREATE TABLE ColNames(\
7989 cpos INTEGER PRIMARY KEY,\
7990 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7991CREATE VIEW RepeatedNames AS \
7992SELECT DISTINCT t.name FROM ColNames t \
7993WHERE t.name COLLATE NOCASE IN (\
7994 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7995);\
7996";
7997  static const char * const zTabFill = "\
7998INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7999 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
8000";
8001  static const char * const zHasDupes = "\
8002SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
8003 <count(name) FROM ColNames\
8004";
8005#ifdef SHELL_COLUMN_RENAME_CLEAN
8006  static const char * const zDedoctor = "\
8007UPDATE ColNames SET chop=iif(\
8008  (substring(name,nlen,1) BETWEEN '0' AND '9')\
8009  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
8010 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
8011 0\
8012)\
8013";
8014#endif
8015  static const char * const zSetReps = "\
8016UPDATE ColNames AS t SET reps=\
8017(SELECT count(*) FROM ColNames d \
8018 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
8019 COLLATE NOCASE\
8020)\
8021";
8022#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8023  static const char * const zColDigits = "\
8024SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8025";
8026#else
8027  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8028  static const char * const zColDigits = "\
8029SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8030 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8031 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8032";
8033#endif
8034  static const char * const zRenameRank =
8035#ifdef SHELL_COLUMN_RENAME_CLEAN
8036    "UPDATE ColNames AS t SET suff="
8037    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
8038#else /* ...RENAME_MINIMAL_ONE_PASS */
8039"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8040"  SELECT 0 AS nlz"
8041"  UNION"
8042"  SELECT nlz+1 AS nlz FROM Lzn"
8043"  WHERE EXISTS("
8044"   SELECT 1"
8045"   FROM ColNames t, ColNames o"
8046"   WHERE"
8047"    iif(t.name IN (SELECT * FROM RepeatedNames),"
8048"     printf('%s"AUTOCOLUMN_SEP"%s',"
8049"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8050"     t.name"
8051"    )"
8052"    ="
8053"    iif(o.name IN (SELECT * FROM RepeatedNames),"
8054"     printf('%s"AUTOCOLUMN_SEP"%s',"
8055"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8056"     o.name"
8057"    )"
8058"    COLLATE NOCASE"
8059"    AND o.cpos<>t.cpos"
8060"   GROUP BY t.cpos"
8061"  )"
8062") UPDATE Colnames AS t SET"
8063" chop = 0," /* No chopping, never touch incoming names. */
8064" suff = iif(name IN (SELECT * FROM RepeatedNames),"
8065"  printf('"AUTOCOLUMN_SEP"%s', substring("
8066"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8067"  ''"
8068" )"
8069#endif
8070    ;
8071  static const char * const zCollectVar = "\
8072SELECT\
8073 '('||x'0a'\
8074 || group_concat(\
8075  cname||' TEXT',\
8076  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8077 ||')' AS ColsSpec \
8078FROM (\
8079 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8080 FROM ColNames ORDER BY cpos\
8081)";
8082  static const char * const zRenamesDone =
8083    "SELECT group_concat("
8084    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8085    " ','||x'0a')"
8086    "FROM ColNames WHERE suff<>'' OR chop!=0"
8087    ;
8088  int rc;
8089  sqlite3_stmt *pStmt = 0;
8090  assert(pDb!=0);
8091  if( zColNew ){
8092    /* Add initial or additional column. Init db if necessary. */
8093    if( *pDb==0 ){
8094      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
8095#ifdef SHELL_COLFIX_DB
8096      if(*zCOL_DB!=':')
8097        sqlite3_exec(*pDb,"drop table if exists ColNames;"
8098                     "drop view if exists RepeatedNames;",0,0,0);
8099#endif
8100      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
8101      rc_err_oom_die(rc);
8102    }
8103    assert(*pDb!=0);
8104    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
8105    rc_err_oom_die(rc);
8106    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
8107    rc_err_oom_die(rc);
8108    rc = sqlite3_step(pStmt);
8109    rc_err_oom_die(rc);
8110    sqlite3_finalize(pStmt);
8111    return 0;
8112  }else if( *pDb==0 ){
8113    return 0;
8114  }else{
8115    /* Formulate the columns spec, close the DB, zero *pDb. */
8116    char *zColsSpec = 0;
8117    int hasDupes = db_int(*pDb, zHasDupes);
8118    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
8119    if( hasDupes ){
8120#ifdef SHELL_COLUMN_RENAME_CLEAN
8121      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
8122      rc_err_oom_die(rc);
8123#endif
8124      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
8125      rc_err_oom_die(rc);
8126      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
8127      rc_err_oom_die(rc);
8128      sqlite3_bind_int(pStmt, 1, nDigits);
8129      rc = sqlite3_step(pStmt);
8130      sqlite3_finalize(pStmt);
8131      assert(rc==SQLITE_DONE);
8132    }
8133    assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
8134    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
8135    rc_err_oom_die(rc);
8136    rc = sqlite3_step(pStmt);
8137    if( rc==SQLITE_ROW ){
8138      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8139    }else{
8140      zColsSpec = 0;
8141    }
8142    if( pzRenamed!=0 ){
8143      if( !hasDupes ) *pzRenamed = 0;
8144      else{
8145        sqlite3_finalize(pStmt);
8146        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
8147            && SQLITE_ROW==sqlite3_step(pStmt) ){
8148          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8149        }else
8150          *pzRenamed = 0;
8151      }
8152    }
8153    sqlite3_finalize(pStmt);
8154    sqlite3_close(*pDb);
8155    *pDb = 0;
8156    return zColsSpec;
8157  }
8158}
8159
8160/*
8161** If an input line begins with "." then invoke this routine to
8162** process that line.
8163**
8164** Return 1 on error, 2 to exit, and 0 otherwise.
8165*/
8166static int do_meta_command(char *zLine, ShellState *p){
8167  int h = 1;
8168  int nArg = 0;
8169  int n, c;
8170  int rc = 0;
8171  char *azArg[52];
8172
8173#ifndef SQLITE_OMIT_VIRTUALTABLE
8174  if( p->expert.pExpert ){
8175    expertFinish(p, 1, 0);
8176  }
8177#endif
8178
8179  /* Parse the input line into tokens.
8180  */
8181  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
8182    while( IsSpace(zLine[h]) ){ h++; }
8183    if( zLine[h]==0 ) break;
8184    if( zLine[h]=='\'' || zLine[h]=='"' ){
8185      int delim = zLine[h++];
8186      azArg[nArg++] = &zLine[h];
8187      while( zLine[h] && zLine[h]!=delim ){
8188        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
8189        h++;
8190      }
8191      if( zLine[h]==delim ){
8192        zLine[h++] = 0;
8193      }
8194      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
8195    }else{
8196      azArg[nArg++] = &zLine[h];
8197      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
8198      if( zLine[h] ) zLine[h++] = 0;
8199      resolve_backslashes(azArg[nArg-1]);
8200    }
8201  }
8202  azArg[nArg] = 0;
8203
8204  /* Process the input line.
8205  */
8206  if( nArg==0 ) return 0; /* no tokens, no error */
8207  n = strlen30(azArg[0]);
8208  c = azArg[0][0];
8209  clearTempFile(p);
8210
8211#ifndef SQLITE_OMIT_AUTHORIZATION
8212  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
8213    if( nArg!=2 ){
8214      raw_printf(stderr, "Usage: .auth ON|OFF\n");
8215      rc = 1;
8216      goto meta_command_exit;
8217    }
8218    open_db(p, 0);
8219    if( booleanValue(azArg[1]) ){
8220      sqlite3_set_authorizer(p->db, shellAuth, p);
8221    }else if( p->bSafeModePersist ){
8222      sqlite3_set_authorizer(p->db, safeModeAuth, p);
8223    }else{
8224      sqlite3_set_authorizer(p->db, 0, 0);
8225    }
8226  }else
8227#endif
8228
8229#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8230  && !defined(SQLITE_SHELL_FIDDLE)
8231  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
8232    open_db(p, 0);
8233    failIfSafeMode(p, "cannot run .archive in safe mode");
8234    rc = arDotCommand(p, 0, azArg, nArg);
8235  }else
8236#endif
8237
8238#ifndef SQLITE_SHELL_FIDDLE
8239  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
8240   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
8241  ){
8242    const char *zDestFile = 0;
8243    const char *zDb = 0;
8244    sqlite3 *pDest;
8245    sqlite3_backup *pBackup;
8246    int j;
8247    int bAsync = 0;
8248    const char *zVfs = 0;
8249    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8250    for(j=1; j<nArg; j++){
8251      const char *z = azArg[j];
8252      if( z[0]=='-' ){
8253        if( z[1]=='-' ) z++;
8254        if( strcmp(z, "-append")==0 ){
8255          zVfs = "apndvfs";
8256        }else
8257        if( strcmp(z, "-async")==0 ){
8258          bAsync = 1;
8259        }else
8260        {
8261          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
8262          return 1;
8263        }
8264      }else if( zDestFile==0 ){
8265        zDestFile = azArg[j];
8266      }else if( zDb==0 ){
8267        zDb = zDestFile;
8268        zDestFile = azArg[j];
8269      }else{
8270        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8271        return 1;
8272      }
8273    }
8274    if( zDestFile==0 ){
8275      raw_printf(stderr, "missing FILENAME argument on .backup\n");
8276      return 1;
8277    }
8278    if( zDb==0 ) zDb = "main";
8279    rc = sqlite3_open_v2(zDestFile, &pDest,
8280                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8281    if( rc!=SQLITE_OK ){
8282      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
8283      close_db(pDest);
8284      return 1;
8285    }
8286    if( bAsync ){
8287      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8288                   0, 0, 0);
8289    }
8290    open_db(p, 0);
8291    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8292    if( pBackup==0 ){
8293      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8294      close_db(pDest);
8295      return 1;
8296    }
8297    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8298    sqlite3_backup_finish(pBackup);
8299    if( rc==SQLITE_DONE ){
8300      rc = 0;
8301    }else{
8302      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8303      rc = 1;
8304    }
8305    close_db(pDest);
8306  }else
8307#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8308
8309  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
8310    if( nArg==2 ){
8311      bail_on_error = booleanValue(azArg[1]);
8312    }else{
8313      raw_printf(stderr, "Usage: .bail on|off\n");
8314      rc = 1;
8315    }
8316  }else
8317
8318  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
8319    if( nArg==2 ){
8320      if( booleanValue(azArg[1]) ){
8321        setBinaryMode(p->out, 1);
8322      }else{
8323        setTextMode(p->out, 1);
8324      }
8325    }else{
8326      raw_printf(stderr, "Usage: .binary on|off\n");
8327      rc = 1;
8328    }
8329  }else
8330
8331  /* The undocumented ".breakpoint" command causes a call to the no-op
8332  ** routine named test_breakpoint().
8333  */
8334  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
8335    test_breakpoint();
8336  }else
8337
8338#ifndef SQLITE_SHELL_FIDDLE
8339  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
8340    failIfSafeMode(p, "cannot run .cd in safe mode");
8341    if( nArg==2 ){
8342#if defined(_WIN32) || defined(WIN32)
8343      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8344      rc = !SetCurrentDirectoryW(z);
8345      sqlite3_free(z);
8346#else
8347      rc = chdir(azArg[1]);
8348#endif
8349      if( rc ){
8350        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
8351        rc = 1;
8352      }
8353    }else{
8354      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
8355      rc = 1;
8356    }
8357  }else
8358#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8359
8360  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
8361    if( nArg==2 ){
8362      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8363    }else{
8364      raw_printf(stderr, "Usage: .changes on|off\n");
8365      rc = 1;
8366    }
8367  }else
8368
8369#ifndef SQLITE_SHELL_FIDDLE
8370  /* Cancel output redirection, if it is currently set (by .testcase)
8371  ** Then read the content of the testcase-out.txt file and compare against
8372  ** azArg[1].  If there are differences, report an error and exit.
8373  */
8374  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
8375    char *zRes = 0;
8376    output_reset(p);
8377    if( nArg!=2 ){
8378      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
8379      rc = 2;
8380    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8381      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
8382      rc = 2;
8383    }else if( testcase_glob(azArg[1],zRes)==0 ){
8384      utf8_printf(stderr,
8385                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
8386                 p->zTestcase, azArg[1], zRes);
8387      rc = 1;
8388    }else{
8389      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
8390      p->nCheck++;
8391    }
8392    sqlite3_free(zRes);
8393  }else
8394#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8395
8396#ifndef SQLITE_SHELL_FIDDLE
8397  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
8398    failIfSafeMode(p, "cannot run .clone in safe mode");
8399    if( nArg==2 ){
8400      tryToClone(p, azArg[1]);
8401    }else{
8402      raw_printf(stderr, "Usage: .clone FILENAME\n");
8403      rc = 1;
8404    }
8405  }else
8406#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8407
8408  if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
8409    if( nArg==1 ){
8410      /* List available connections */
8411      int i;
8412      for(i=0; i<ArraySize(p->aAuxDb); i++){
8413        const char *zFile = p->aAuxDb[i].zDbFilename;
8414        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8415          zFile = "(not open)";
8416        }else if( zFile==0 ){
8417          zFile = "(memory)";
8418        }else if( zFile[0]==0 ){
8419          zFile = "(temporary-file)";
8420        }
8421        if( p->pAuxDb == &p->aAuxDb[i] ){
8422          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
8423        }else if( p->aAuxDb[i].db!=0 ){
8424          utf8_printf(stdout, "       %d: %s\n", i, zFile);
8425        }
8426      }
8427    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8428      int i = azArg[1][0] - '0';
8429      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8430        p->pAuxDb->db = p->db;
8431        p->pAuxDb = &p->aAuxDb[i];
8432        globalDb = p->db = p->pAuxDb->db;
8433        p->pAuxDb->db = 0;
8434      }
8435    }else if( nArg==3 && strcmp(azArg[1], "close")==0
8436           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8437      int i = azArg[2][0] - '0';
8438      if( i<0 || i>=ArraySize(p->aAuxDb) ){
8439        /* No-op */
8440      }else if( p->pAuxDb == &p->aAuxDb[i] ){
8441        raw_printf(stderr, "cannot close the active database connection\n");
8442        rc = 1;
8443      }else if( p->aAuxDb[i].db ){
8444        session_close_all(p, i);
8445        close_db(p->aAuxDb[i].db);
8446        p->aAuxDb[i].db = 0;
8447      }
8448    }else{
8449      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
8450      rc = 1;
8451    }
8452  }else
8453
8454  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
8455    char **azName = 0;
8456    int nName = 0;
8457    sqlite3_stmt *pStmt;
8458    int i;
8459    open_db(p, 0);
8460    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8461    if( rc ){
8462      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8463      rc = 1;
8464    }else{
8465      while( sqlite3_step(pStmt)==SQLITE_ROW ){
8466        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8467        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8468        if( zSchema==0 || zFile==0 ) continue;
8469        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8470        shell_check_oom(azName);
8471        azName[nName*2] = strdup(zSchema);
8472        azName[nName*2+1] = strdup(zFile);
8473        nName++;
8474      }
8475    }
8476    sqlite3_finalize(pStmt);
8477    for(i=0; i<nName; i++){
8478      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8479      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8480      const char *z = azName[i*2+1];
8481      utf8_printf(p->out, "%s: %s %s%s\n",
8482         azName[i*2],
8483         z && z[0] ? z : "\"\"",
8484         bRdonly ? "r/o" : "r/w",
8485         eTxn==SQLITE_TXN_NONE ? "" :
8486            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8487      free(azName[i*2]);
8488      free(azName[i*2+1]);
8489    }
8490    sqlite3_free(azName);
8491  }else
8492
8493  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
8494    static const struct DbConfigChoices {
8495      const char *zName;
8496      int op;
8497    } aDbConfig[] = {
8498        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
8499        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
8500        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
8501        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
8502        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
8503        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
8504        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
8505        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8506        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
8507        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
8508        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8509        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
8510        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
8511        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
8512        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
8513        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
8514    };
8515    int ii, v;
8516    open_db(p, 0);
8517    for(ii=0; ii<ArraySize(aDbConfig); ii++){
8518      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8519      if( nArg>=3 ){
8520        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8521      }
8522      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8523      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
8524      if( nArg>1 ) break;
8525    }
8526    if( nArg>1 && ii==ArraySize(aDbConfig) ){
8527      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
8528      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
8529    }
8530  }else
8531
8532#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8533  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
8534    rc = shell_dbinfo_command(p, nArg, azArg);
8535  }else
8536
8537  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
8538    open_db(p, 0);
8539    rc = recoverDatabaseCmd(p, nArg, azArg);
8540  }else
8541#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
8542
8543  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
8544    char *zLike = 0;
8545    char *zSql;
8546    int i;
8547    int savedShowHeader = p->showHeader;
8548    int savedShellFlags = p->shellFlgs;
8549    ShellClearFlag(p,
8550       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8551       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8552    for(i=1; i<nArg; i++){
8553      if( azArg[i][0]=='-' ){
8554        const char *z = azArg[i]+1;
8555        if( z[0]=='-' ) z++;
8556        if( strcmp(z,"preserve-rowids")==0 ){
8557#ifdef SQLITE_OMIT_VIRTUALTABLE
8558          raw_printf(stderr, "The --preserve-rowids option is not compatible"
8559                             " with SQLITE_OMIT_VIRTUALTABLE\n");
8560          rc = 1;
8561          sqlite3_free(zLike);
8562          goto meta_command_exit;
8563#else
8564          ShellSetFlag(p, SHFLG_PreserveRowid);
8565#endif
8566        }else
8567        if( strcmp(z,"newlines")==0 ){
8568          ShellSetFlag(p, SHFLG_Newlines);
8569        }else
8570        if( strcmp(z,"data-only")==0 ){
8571          ShellSetFlag(p, SHFLG_DumpDataOnly);
8572        }else
8573        if( strcmp(z,"nosys")==0 ){
8574          ShellSetFlag(p, SHFLG_DumpNoSys);
8575        }else
8576        {
8577          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8578          rc = 1;
8579          sqlite3_free(zLike);
8580          goto meta_command_exit;
8581        }
8582      }else{
8583        /* azArg[i] contains a LIKE pattern. This ".dump" request should
8584        ** only dump data for tables for which either the table name matches
8585        ** the LIKE pattern, or the table appears to be a shadow table of
8586        ** a virtual table for which the name matches the LIKE pattern.
8587        */
8588        char *zExpr = sqlite3_mprintf(
8589            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8590            "  SELECT 1 FROM sqlite_schema WHERE "
8591            "    name LIKE %Q ESCAPE '\\' AND"
8592            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8593            "    substr(o.name, 1, length(name)+1) == (name||'_')"
8594            ")", azArg[i], azArg[i]
8595        );
8596
8597        if( zLike ){
8598          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8599        }else{
8600          zLike = zExpr;
8601        }
8602      }
8603    }
8604
8605    open_db(p, 0);
8606
8607    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8608      /* When playing back a "dump", the content might appear in an order
8609      ** which causes immediate foreign key constraints to be violated.
8610      ** So disable foreign-key constraint enforcement to prevent problems. */
8611      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8612      raw_printf(p->out, "BEGIN TRANSACTION;\n");
8613    }
8614    p->writableSchema = 0;
8615    p->showHeader = 0;
8616    /* Set writable_schema=ON since doing so forces SQLite to initialize
8617    ** as much of the schema as it can even if the sqlite_schema table is
8618    ** corrupt. */
8619    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8620    p->nErr = 0;
8621    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8622    zSql = sqlite3_mprintf(
8623      "SELECT name, type, sql FROM sqlite_schema AS o "
8624      "WHERE (%s) AND type=='table'"
8625      "  AND sql NOT NULL"
8626      " ORDER BY tbl_name='sqlite_sequence', rowid",
8627      zLike
8628    );
8629    run_schema_dump_query(p,zSql);
8630    sqlite3_free(zSql);
8631    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8632      zSql = sqlite3_mprintf(
8633        "SELECT sql FROM sqlite_schema AS o "
8634        "WHERE (%s) AND sql NOT NULL"
8635        "  AND type IN ('index','trigger','view')",
8636        zLike
8637      );
8638      run_table_dump_query(p, zSql);
8639      sqlite3_free(zSql);
8640    }
8641    sqlite3_free(zLike);
8642    if( p->writableSchema ){
8643      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8644      p->writableSchema = 0;
8645    }
8646    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8647    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8648    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8649      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8650    }
8651    p->showHeader = savedShowHeader;
8652    p->shellFlgs = savedShellFlags;
8653  }else
8654
8655  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
8656    if( nArg==2 ){
8657      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8658    }else{
8659      raw_printf(stderr, "Usage: .echo on|off\n");
8660      rc = 1;
8661    }
8662  }else
8663
8664  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
8665    if( nArg==2 ){
8666      p->autoEQPtest = 0;
8667      if( p->autoEQPtrace ){
8668        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8669        p->autoEQPtrace = 0;
8670      }
8671      if( strcmp(azArg[1],"full")==0 ){
8672        p->autoEQP = AUTOEQP_full;
8673      }else if( strcmp(azArg[1],"trigger")==0 ){
8674        p->autoEQP = AUTOEQP_trigger;
8675#ifdef SQLITE_DEBUG
8676      }else if( strcmp(azArg[1],"test")==0 ){
8677        p->autoEQP = AUTOEQP_on;
8678        p->autoEQPtest = 1;
8679      }else if( strcmp(azArg[1],"trace")==0 ){
8680        p->autoEQP = AUTOEQP_full;
8681        p->autoEQPtrace = 1;
8682        open_db(p, 0);
8683        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8684        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8685#endif
8686      }else{
8687        p->autoEQP = (u8)booleanValue(azArg[1]);
8688      }
8689    }else{
8690      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8691      rc = 1;
8692    }
8693  }else
8694
8695#ifndef SQLITE_SHELL_FIDDLE
8696  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
8697    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8698    rc = 2;
8699  }else
8700#endif
8701
8702  /* The ".explain" command is automatic now.  It is largely pointless.  It
8703  ** retained purely for backwards compatibility */
8704  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
8705    int val = 1;
8706    if( nArg>=2 ){
8707      if( strcmp(azArg[1],"auto")==0 ){
8708        val = 99;
8709      }else{
8710        val =  booleanValue(azArg[1]);
8711      }
8712    }
8713    if( val==1 && p->mode!=MODE_Explain ){
8714      p->normalMode = p->mode;
8715      p->mode = MODE_Explain;
8716      p->autoExplain = 0;
8717    }else if( val==0 ){
8718      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8719      p->autoExplain = 0;
8720    }else if( val==99 ){
8721      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8722      p->autoExplain = 1;
8723    }
8724  }else
8725
8726#ifndef SQLITE_OMIT_VIRTUALTABLE
8727  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
8728    if( p->bSafeMode ){
8729      raw_printf(stderr,
8730        "Cannot run experimental commands such as \"%s\" in safe mode\n",
8731        azArg[0]);
8732      rc = 1;
8733    }else{
8734      open_db(p, 0);
8735      expertDotCommand(p, azArg, nArg);
8736    }
8737  }else
8738#endif
8739
8740  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
8741    static const struct {
8742       const char *zCtrlName;   /* Name of a test-control option */
8743       int ctrlCode;            /* Integer code for that option */
8744       const char *zUsage;      /* Usage notes */
8745    } aCtrl[] = {
8746      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8747      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8748      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8749      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8750      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8751   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8752      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8753      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8754      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8755      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8756   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8757    };
8758    int filectrl = -1;
8759    int iCtrl = -1;
8760    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8761    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8762    int n2, i;
8763    const char *zCmd = 0;
8764    const char *zSchema = 0;
8765
8766    open_db(p, 0);
8767    zCmd = nArg>=2 ? azArg[1] : "help";
8768
8769    if( zCmd[0]=='-'
8770     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
8771     && nArg>=4
8772    ){
8773      zSchema = azArg[2];
8774      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8775      nArg -= 2;
8776      zCmd = azArg[1];
8777    }
8778
8779    /* The argument can optionally begin with "-" or "--" */
8780    if( zCmd[0]=='-' && zCmd[1] ){
8781      zCmd++;
8782      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8783    }
8784
8785    /* --help lists all file-controls */
8786    if( strcmp(zCmd,"help")==0 ){
8787      utf8_printf(p->out, "Available file-controls:\n");
8788      for(i=0; i<ArraySize(aCtrl); i++){
8789        utf8_printf(p->out, "  .filectrl %s %s\n",
8790                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8791      }
8792      rc = 1;
8793      goto meta_command_exit;
8794    }
8795
8796    /* convert filectrl text option to value. allow any unique prefix
8797    ** of the option name, or a numerical value. */
8798    n2 = strlen30(zCmd);
8799    for(i=0; i<ArraySize(aCtrl); i++){
8800      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8801        if( filectrl<0 ){
8802          filectrl = aCtrl[i].ctrlCode;
8803          iCtrl = i;
8804        }else{
8805          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8806                              "Use \".filectrl --help\" for help\n", zCmd);
8807          rc = 1;
8808          goto meta_command_exit;
8809        }
8810      }
8811    }
8812    if( filectrl<0 ){
8813      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8814                         "Use \".filectrl --help\" for help\n", zCmd);
8815    }else{
8816      switch(filectrl){
8817        case SQLITE_FCNTL_SIZE_LIMIT: {
8818          if( nArg!=2 && nArg!=3 ) break;
8819          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8820          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8821          isOk = 1;
8822          break;
8823        }
8824        case SQLITE_FCNTL_LOCK_TIMEOUT:
8825        case SQLITE_FCNTL_CHUNK_SIZE: {
8826          int x;
8827          if( nArg!=3 ) break;
8828          x = (int)integerValue(azArg[2]);
8829          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8830          isOk = 2;
8831          break;
8832        }
8833        case SQLITE_FCNTL_PERSIST_WAL:
8834        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8835          int x;
8836          if( nArg!=2 && nArg!=3 ) break;
8837          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8838          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8839          iRes = x;
8840          isOk = 1;
8841          break;
8842        }
8843        case SQLITE_FCNTL_DATA_VERSION:
8844        case SQLITE_FCNTL_HAS_MOVED: {
8845          int x;
8846          if( nArg!=2 ) break;
8847          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8848          iRes = x;
8849          isOk = 1;
8850          break;
8851        }
8852        case SQLITE_FCNTL_TEMPFILENAME: {
8853          char *z = 0;
8854          if( nArg!=2 ) break;
8855          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8856          if( z ){
8857            utf8_printf(p->out, "%s\n", z);
8858            sqlite3_free(z);
8859          }
8860          isOk = 2;
8861          break;
8862        }
8863        case SQLITE_FCNTL_RESERVE_BYTES: {
8864          int x;
8865          if( nArg>=3 ){
8866            x = atoi(azArg[2]);
8867            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8868          }
8869          x = -1;
8870          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8871          utf8_printf(p->out,"%d\n", x);
8872          isOk = 2;
8873          break;
8874        }
8875      }
8876    }
8877    if( isOk==0 && iCtrl>=0 ){
8878      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8879      rc = 1;
8880    }else if( isOk==1 ){
8881      char zBuf[100];
8882      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8883      raw_printf(p->out, "%s\n", zBuf);
8884    }
8885  }else
8886
8887  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8888    ShellState data;
8889    int doStats = 0;
8890    memcpy(&data, p, sizeof(data));
8891    data.showHeader = 0;
8892    data.cMode = data.mode = MODE_Semi;
8893    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8894      data.cMode = data.mode = MODE_Pretty;
8895      nArg = 1;
8896    }
8897    if( nArg!=1 ){
8898      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8899      rc = 1;
8900      goto meta_command_exit;
8901    }
8902    open_db(p, 0);
8903    rc = sqlite3_exec(p->db,
8904       "SELECT sql FROM"
8905       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8906       "     FROM sqlite_schema UNION ALL"
8907       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8908       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8909       "ORDER BY x",
8910       callback, &data, 0
8911    );
8912    if( rc==SQLITE_OK ){
8913      sqlite3_stmt *pStmt;
8914      rc = sqlite3_prepare_v2(p->db,
8915               "SELECT rowid FROM sqlite_schema"
8916               " WHERE name GLOB 'sqlite_stat[134]'",
8917               -1, &pStmt, 0);
8918      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8919      sqlite3_finalize(pStmt);
8920    }
8921    if( doStats==0 ){
8922      raw_printf(p->out, "/* No STAT tables available */\n");
8923    }else{
8924      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8925      data.cMode = data.mode = MODE_Insert;
8926      data.zDestTable = "sqlite_stat1";
8927      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8928      data.zDestTable = "sqlite_stat4";
8929      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8930      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8931    }
8932  }else
8933
8934  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8935    if( nArg==2 ){
8936      p->showHeader = booleanValue(azArg[1]);
8937      p->shellFlgs |= SHFLG_HeaderSet;
8938    }else{
8939      raw_printf(stderr, "Usage: .headers on|off\n");
8940      rc = 1;
8941    }
8942  }else
8943
8944  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8945    if( nArg>=2 ){
8946      n = showHelp(p->out, azArg[1]);
8947      if( n==0 ){
8948        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8949      }
8950    }else{
8951      showHelp(p->out, 0);
8952    }
8953  }else
8954
8955#ifndef SQLITE_SHELL_FIDDLE
8956  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8957    char *zTable = 0;           /* Insert data into this table */
8958    char *zSchema = 0;          /* within this schema (may default to "main") */
8959    char *zFile = 0;            /* Name of file to extra content from */
8960    sqlite3_stmt *pStmt = NULL; /* A statement */
8961    int nCol;                   /* Number of columns in the table */
8962    int nByte;                  /* Number of bytes in an SQL string */
8963    int i, j;                   /* Loop counters */
8964    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8965    int nSep;                   /* Number of bytes in p->colSeparator[] */
8966    char *zSql;                 /* An SQL statement */
8967    char *zFullTabName;         /* Table name with schema if applicable */
8968    ImportCtx sCtx;             /* Reader context */
8969    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8970    int eVerbose = 0;           /* Larger for more console output */
8971    int nSkip = 0;              /* Initial lines to skip */
8972    int useOutputMode = 1;      /* Use output mode to determine separators */
8973    char *zCreate = 0;          /* CREATE TABLE statement text */
8974
8975    failIfSafeMode(p, "cannot run .import in safe mode");
8976    memset(&sCtx, 0, sizeof(sCtx));
8977    if( p->mode==MODE_Ascii ){
8978      xRead = ascii_read_one_field;
8979    }else{
8980      xRead = csv_read_one_field;
8981    }
8982    rc = 1;
8983    for(i=1; i<nArg; i++){
8984      char *z = azArg[i];
8985      if( z[0]=='-' && z[1]=='-' ) z++;
8986      if( z[0]!='-' ){
8987        if( zFile==0 ){
8988          zFile = z;
8989        }else if( zTable==0 ){
8990          zTable = z;
8991        }else{
8992          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8993          showHelp(p->out, "import");
8994          goto meta_command_exit;
8995        }
8996      }else if( strcmp(z,"-v")==0 ){
8997        eVerbose++;
8998      }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
8999        zSchema = azArg[++i];
9000      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
9001        nSkip = integerValue(azArg[++i]);
9002      }else if( strcmp(z,"-ascii")==0 ){
9003        sCtx.cColSep = SEP_Unit[0];
9004        sCtx.cRowSep = SEP_Record[0];
9005        xRead = ascii_read_one_field;
9006        useOutputMode = 0;
9007      }else if( strcmp(z,"-csv")==0 ){
9008        sCtx.cColSep = ',';
9009        sCtx.cRowSep = '\n';
9010        xRead = csv_read_one_field;
9011        useOutputMode = 0;
9012      }else{
9013        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
9014        showHelp(p->out, "import");
9015        goto meta_command_exit;
9016      }
9017    }
9018    if( zTable==0 ){
9019      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
9020                  zFile==0 ? "FILE" : "TABLE");
9021      showHelp(p->out, "import");
9022      goto meta_command_exit;
9023    }
9024    seenInterrupt = 0;
9025    open_db(p, 0);
9026    if( useOutputMode ){
9027      /* If neither the --csv or --ascii options are specified, then set
9028      ** the column and row separator characters from the output mode. */
9029      nSep = strlen30(p->colSeparator);
9030      if( nSep==0 ){
9031        raw_printf(stderr,
9032                   "Error: non-null column separator required for import\n");
9033        goto meta_command_exit;
9034      }
9035      if( nSep>1 ){
9036        raw_printf(stderr,
9037              "Error: multi-character column separators not allowed"
9038              " for import\n");
9039        goto meta_command_exit;
9040      }
9041      nSep = strlen30(p->rowSeparator);
9042      if( nSep==0 ){
9043        raw_printf(stderr,
9044            "Error: non-null row separator required for import\n");
9045        goto meta_command_exit;
9046      }
9047      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
9048        /* When importing CSV (only), if the row separator is set to the
9049        ** default output row separator, change it to the default input
9050        ** row separator.  This avoids having to maintain different input
9051        ** and output row separators. */
9052        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9053        nSep = strlen30(p->rowSeparator);
9054      }
9055      if( nSep>1 ){
9056        raw_printf(stderr, "Error: multi-character row separators not allowed"
9057                           " for import\n");
9058        goto meta_command_exit;
9059      }
9060      sCtx.cColSep = p->colSeparator[0];
9061      sCtx.cRowSep = p->rowSeparator[0];
9062    }
9063    sCtx.zFile = zFile;
9064    sCtx.nLine = 1;
9065    if( sCtx.zFile[0]=='|' ){
9066#ifdef SQLITE_OMIT_POPEN
9067      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9068      goto meta_command_exit;
9069#else
9070      sCtx.in = popen(sCtx.zFile+1, "r");
9071      sCtx.zFile = "<pipe>";
9072      sCtx.xCloser = pclose;
9073#endif
9074    }else{
9075      sCtx.in = fopen(sCtx.zFile, "rb");
9076      sCtx.xCloser = fclose;
9077    }
9078    if( sCtx.in==0 ){
9079      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
9080      goto meta_command_exit;
9081    }
9082    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
9083      char zSep[2];
9084      zSep[1] = 0;
9085      zSep[0] = sCtx.cColSep;
9086      utf8_printf(p->out, "Column separator ");
9087      output_c_string(p->out, zSep);
9088      utf8_printf(p->out, ", row separator ");
9089      zSep[0] = sCtx.cRowSep;
9090      output_c_string(p->out, zSep);
9091      utf8_printf(p->out, "\n");
9092    }
9093    sCtx.z = sqlite3_malloc64(120);
9094    if( sCtx.z==0 ){
9095      import_cleanup(&sCtx);
9096      shell_out_of_memory();
9097    }
9098    /* Below, resources must be freed before exit. */
9099    while( (nSkip--)>0 ){
9100      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
9101    }
9102    if( zSchema!=0 ){
9103      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
9104    }else{
9105      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
9106    }
9107    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
9108    if( zSql==0 || zFullTabName==0 ){
9109      import_cleanup(&sCtx);
9110      shell_out_of_memory();
9111    }
9112    nByte = strlen30(zSql);
9113    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9114    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
9115    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
9116      sqlite3 *dbCols = 0;
9117      char *zRenames = 0;
9118      char *zColDefs;
9119      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
9120      while( xRead(&sCtx) ){
9121        zAutoColumn(sCtx.z, &dbCols, 0);
9122        if( sCtx.cTerm!=sCtx.cColSep ) break;
9123      }
9124      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
9125      if( zRenames!=0 ){
9126        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
9127                    "Columns renamed during .import %s due to duplicates:\n"
9128                    "%s\n", sCtx.zFile, zRenames);
9129        sqlite3_free(zRenames);
9130      }
9131      assert(dbCols==0);
9132      if( zColDefs==0 ){
9133        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
9134      import_fail:
9135        sqlite3_free(zCreate);
9136        sqlite3_free(zSql);
9137        sqlite3_free(zFullTabName);
9138        import_cleanup(&sCtx);
9139        rc = 1;
9140        goto meta_command_exit;
9141      }
9142      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
9143      if( eVerbose>=1 ){
9144        utf8_printf(p->out, "%s\n", zCreate);
9145      }
9146      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
9147      if( rc ){
9148        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
9149        goto import_fail;
9150      }
9151      sqlite3_free(zCreate);
9152      zCreate = 0;
9153      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9154    }
9155    if( rc ){
9156      if (pStmt) sqlite3_finalize(pStmt);
9157      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
9158      goto import_fail;
9159    }
9160    sqlite3_free(zSql);
9161    nCol = sqlite3_column_count(pStmt);
9162    sqlite3_finalize(pStmt);
9163    pStmt = 0;
9164    if( nCol==0 ) return 0; /* no columns, no error */
9165    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
9166    if( zSql==0 ){
9167      import_cleanup(&sCtx);
9168      shell_out_of_memory();
9169    }
9170    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
9171    j = strlen30(zSql);
9172    for(i=1; i<nCol; i++){
9173      zSql[j++] = ',';
9174      zSql[j++] = '?';
9175    }
9176    zSql[j++] = ')';
9177    zSql[j] = 0;
9178    if( eVerbose>=2 ){
9179      utf8_printf(p->out, "Insert using: %s\n", zSql);
9180    }
9181    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9182    if( rc ){
9183      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9184      if (pStmt) sqlite3_finalize(pStmt);
9185      goto import_fail;
9186    }
9187    sqlite3_free(zSql);
9188    sqlite3_free(zFullTabName);
9189    needCommit = sqlite3_get_autocommit(p->db);
9190    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
9191    do{
9192      int startLine = sCtx.nLine;
9193      for(i=0; i<nCol; i++){
9194        char *z = xRead(&sCtx);
9195        /*
9196        ** Did we reach end-of-file before finding any columns?
9197        ** If so, stop instead of NULL filling the remaining columns.
9198        */
9199        if( z==0 && i==0 ) break;
9200        /*
9201        ** Did we reach end-of-file OR end-of-line before finding any
9202        ** columns in ASCII mode?  If so, stop instead of NULL filling
9203        ** the remaining columns.
9204        */
9205        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
9206        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
9207        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
9208          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9209                          "filling the rest with NULL\n",
9210                          sCtx.zFile, startLine, nCol, i+1);
9211          i += 2;
9212          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9213        }
9214      }
9215      if( sCtx.cTerm==sCtx.cColSep ){
9216        do{
9217          xRead(&sCtx);
9218          i++;
9219        }while( sCtx.cTerm==sCtx.cColSep );
9220        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9221                        "extras ignored\n",
9222                        sCtx.zFile, startLine, nCol, i);
9223      }
9224      if( i>=nCol ){
9225        sqlite3_step(pStmt);
9226        rc = sqlite3_reset(pStmt);
9227        if( rc!=SQLITE_OK ){
9228          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
9229                      startLine, sqlite3_errmsg(p->db));
9230          sCtx.nErr++;
9231        }else{
9232          sCtx.nRow++;
9233        }
9234      }
9235    }while( sCtx.cTerm!=EOF );
9236
9237    import_cleanup(&sCtx);
9238    sqlite3_finalize(pStmt);
9239    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9240    if( eVerbose>0 ){
9241      utf8_printf(p->out,
9242          "Added %d rows with %d errors using %d lines of input\n",
9243          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9244    }
9245  }else
9246#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9247
9248#ifndef SQLITE_UNTESTABLE
9249  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
9250    char *zSql;
9251    char *zCollist = 0;
9252    sqlite3_stmt *pStmt;
9253    int tnum = 0;
9254    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
9255    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9256    int i;
9257    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9258      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
9259                          "       .imposter off\n");
9260      /* Also allowed, but not documented:
9261      **
9262      **    .imposter TABLE IMPOSTER
9263      **
9264      ** where TABLE is a WITHOUT ROWID table.  In that case, the
9265      ** imposter is another WITHOUT ROWID table with the columns in
9266      ** storage order. */
9267      rc = 1;
9268      goto meta_command_exit;
9269    }
9270    open_db(p, 0);
9271    if( nArg==2 ){
9272      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9273      goto meta_command_exit;
9274    }
9275    zSql = sqlite3_mprintf(
9276      "SELECT rootpage, 0 FROM sqlite_schema"
9277      " WHERE name='%q' AND type='index'"
9278      "UNION ALL "
9279      "SELECT rootpage, 1 FROM sqlite_schema"
9280      " WHERE name='%q' AND type='table'"
9281      "   AND sql LIKE '%%without%%rowid%%'",
9282      azArg[1], azArg[1]
9283    );
9284    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9285    sqlite3_free(zSql);
9286    if( sqlite3_step(pStmt)==SQLITE_ROW ){
9287      tnum = sqlite3_column_int(pStmt, 0);
9288      isWO = sqlite3_column_int(pStmt, 1);
9289    }
9290    sqlite3_finalize(pStmt);
9291    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9292    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9293    sqlite3_free(zSql);
9294    i = 0;
9295    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9296      char zLabel[20];
9297      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9298      i++;
9299      if( zCol==0 ){
9300        if( sqlite3_column_int(pStmt,1)==-1 ){
9301          zCol = "_ROWID_";
9302        }else{
9303          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9304          zCol = zLabel;
9305        }
9306      }
9307      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9308        lenPK = (int)strlen(zCollist);
9309      }
9310      if( zCollist==0 ){
9311        zCollist = sqlite3_mprintf("\"%w\"", zCol);
9312      }else{
9313        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9314      }
9315    }
9316    sqlite3_finalize(pStmt);
9317    if( i==0 || tnum==0 ){
9318      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
9319      rc = 1;
9320      sqlite3_free(zCollist);
9321      goto meta_command_exit;
9322    }
9323    if( lenPK==0 ) lenPK = 100000;
9324    zSql = sqlite3_mprintf(
9325          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9326          azArg[2], zCollist, lenPK, zCollist);
9327    sqlite3_free(zCollist);
9328    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9329    if( rc==SQLITE_OK ){
9330      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9331      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9332      if( rc ){
9333        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9334      }else{
9335        utf8_printf(stdout, "%s;\n", zSql);
9336        raw_printf(stdout,
9337          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
9338          azArg[1], isWO ? "table" : "index"
9339        );
9340      }
9341    }else{
9342      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9343      rc = 1;
9344    }
9345    sqlite3_free(zSql);
9346  }else
9347#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9348
9349#ifdef SQLITE_ENABLE_IOTRACE
9350  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
9351    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9352    if( iotrace && iotrace!=stdout ) fclose(iotrace);
9353    iotrace = 0;
9354    if( nArg<2 ){
9355      sqlite3IoTrace = 0;
9356    }else if( strcmp(azArg[1], "-")==0 ){
9357      sqlite3IoTrace = iotracePrintf;
9358      iotrace = stdout;
9359    }else{
9360      iotrace = fopen(azArg[1], "w");
9361      if( iotrace==0 ){
9362        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9363        sqlite3IoTrace = 0;
9364        rc = 1;
9365      }else{
9366        sqlite3IoTrace = iotracePrintf;
9367      }
9368    }
9369  }else
9370#endif
9371
9372  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
9373    static const struct {
9374       const char *zLimitName;   /* Name of a limit */
9375       int limitCode;            /* Integer code for that limit */
9376    } aLimit[] = {
9377      { "length",                SQLITE_LIMIT_LENGTH                    },
9378      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
9379      { "column",                SQLITE_LIMIT_COLUMN                    },
9380      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
9381      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
9382      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
9383      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
9384      { "attached",              SQLITE_LIMIT_ATTACHED                  },
9385      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
9386      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
9387      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
9388      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
9389    };
9390    int i, n2;
9391    open_db(p, 0);
9392    if( nArg==1 ){
9393      for(i=0; i<ArraySize(aLimit); i++){
9394        printf("%20s %d\n", aLimit[i].zLimitName,
9395               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9396      }
9397    }else if( nArg>3 ){
9398      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
9399      rc = 1;
9400      goto meta_command_exit;
9401    }else{
9402      int iLimit = -1;
9403      n2 = strlen30(azArg[1]);
9404      for(i=0; i<ArraySize(aLimit); i++){
9405        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9406          if( iLimit<0 ){
9407            iLimit = i;
9408          }else{
9409            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
9410            rc = 1;
9411            goto meta_command_exit;
9412          }
9413        }
9414      }
9415      if( iLimit<0 ){
9416        utf8_printf(stderr, "unknown limit: \"%s\"\n"
9417                        "enter \".limits\" with no arguments for a list.\n",
9418                         azArg[1]);
9419        rc = 1;
9420        goto meta_command_exit;
9421      }
9422      if( nArg==3 ){
9423        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9424                      (int)integerValue(azArg[2]));
9425      }
9426      printf("%20s %d\n", aLimit[iLimit].zLimitName,
9427             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9428    }
9429  }else
9430
9431  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
9432    open_db(p, 0);
9433    lintDotCommand(p, azArg, nArg);
9434  }else
9435
9436#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9437  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
9438    const char *zFile, *zProc;
9439    char *zErrMsg = 0;
9440    failIfSafeMode(p, "cannot run .load in safe mode");
9441    if( nArg<2 ){
9442      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
9443      rc = 1;
9444      goto meta_command_exit;
9445    }
9446    zFile = azArg[1];
9447    zProc = nArg>=3 ? azArg[2] : 0;
9448    open_db(p, 0);
9449    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9450    if( rc!=SQLITE_OK ){
9451      utf8_printf(stderr, "Error: %s\n", zErrMsg);
9452      sqlite3_free(zErrMsg);
9453      rc = 1;
9454    }
9455  }else
9456#endif
9457
9458#ifndef SQLITE_SHELL_FIDDLE
9459  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
9460    failIfSafeMode(p, "cannot run .log in safe mode");
9461    if( nArg!=2 ){
9462      raw_printf(stderr, "Usage: .log FILENAME\n");
9463      rc = 1;
9464    }else{
9465      const char *zFile = azArg[1];
9466      output_file_close(p->pLog);
9467      p->pLog = output_file_open(zFile, 0);
9468    }
9469  }else
9470#endif
9471
9472  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
9473    const char *zMode = 0;
9474    const char *zTabname = 0;
9475    int i, n2;
9476    ColModeOpts cmOpts = ColModeOpts_default;
9477    for(i=1; i<nArg; i++){
9478      const char *z = azArg[i];
9479      if( optionMatch(z,"wrap") && i+1<nArg ){
9480        cmOpts.iWrap = integerValue(azArg[++i]);
9481      }else if( optionMatch(z,"ww") ){
9482        cmOpts.bWordWrap = 1;
9483      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9484        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9485      }else if( optionMatch(z,"quote") ){
9486        cmOpts.bQuote = 1;
9487      }else if( optionMatch(z,"noquote") ){
9488        cmOpts.bQuote = 0;
9489      }else if( zMode==0 ){
9490        zMode = z;
9491        /* Apply defaults for qbox pseudo-mods. If that
9492         * overwrites already-set values, user was informed of this.
9493         */
9494        if( strcmp(z, "qbox")==0 ){
9495          ColModeOpts cmo = ColModeOpts_default_qbox;
9496          zMode = "box";
9497          cmOpts = cmo;
9498        }
9499      }else if( zTabname==0 ){
9500        zTabname = z;
9501      }else if( z[0]=='-' ){
9502        utf8_printf(stderr, "unknown option: %s\n", z);
9503        utf8_printf(stderr, "options:\n"
9504                            "  --noquote\n"
9505                            "  --quote\n"
9506                            "  --wordwrap on/off\n"
9507                            "  --wrap N\n"
9508                            "  --ww\n");
9509        rc = 1;
9510        goto meta_command_exit;
9511      }else{
9512        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9513        rc = 1;
9514        goto meta_command_exit;
9515      }
9516    }
9517    if( zMode==0 ){
9518      if( p->mode==MODE_Column
9519       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9520      ){
9521        raw_printf
9522          (p->out,
9523           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9524           modeDescr[p->mode], p->cmOpts.iWrap,
9525           p->cmOpts.bWordWrap ? "on" : "off",
9526           p->cmOpts.bQuote ? "" : "no");
9527      }else{
9528        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
9529      }
9530      zMode = modeDescr[p->mode];
9531    }
9532    n2 = strlen30(zMode);
9533    if( strncmp(zMode,"lines",n2)==0 ){
9534      p->mode = MODE_Line;
9535      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9536    }else if( strncmp(zMode,"columns",n2)==0 ){
9537      p->mode = MODE_Column;
9538      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9539        p->showHeader = 1;
9540      }
9541      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9542      p->cmOpts = cmOpts;
9543    }else if( strncmp(zMode,"list",n2)==0 ){
9544      p->mode = MODE_List;
9545      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9546      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9547    }else if( strncmp(zMode,"html",n2)==0 ){
9548      p->mode = MODE_Html;
9549    }else if( strncmp(zMode,"tcl",n2)==0 ){
9550      p->mode = MODE_Tcl;
9551      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9552      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9553    }else if( strncmp(zMode,"csv",n2)==0 ){
9554      p->mode = MODE_Csv;
9555      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9556      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9557    }else if( strncmp(zMode,"tabs",n2)==0 ){
9558      p->mode = MODE_List;
9559      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9560    }else if( strncmp(zMode,"insert",n2)==0 ){
9561      p->mode = MODE_Insert;
9562      set_table_name(p, zTabname ? zTabname : "table");
9563    }else if( strncmp(zMode,"quote",n2)==0 ){
9564      p->mode = MODE_Quote;
9565      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9566      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9567    }else if( strncmp(zMode,"ascii",n2)==0 ){
9568      p->mode = MODE_Ascii;
9569      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9570      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9571    }else if( strncmp(zMode,"markdown",n2)==0 ){
9572      p->mode = MODE_Markdown;
9573      p->cmOpts = cmOpts;
9574    }else if( strncmp(zMode,"table",n2)==0 ){
9575      p->mode = MODE_Table;
9576      p->cmOpts = cmOpts;
9577    }else if( strncmp(zMode,"box",n2)==0 ){
9578      p->mode = MODE_Box;
9579      p->cmOpts = cmOpts;
9580    }else if( strncmp(zMode,"count",n2)==0 ){
9581      p->mode = MODE_Count;
9582    }else if( strncmp(zMode,"off",n2)==0 ){
9583      p->mode = MODE_Off;
9584    }else if( strncmp(zMode,"json",n2)==0 ){
9585      p->mode = MODE_Json;
9586    }else{
9587      raw_printf(stderr, "Error: mode should be one of: "
9588         "ascii box column csv html insert json line list markdown "
9589         "qbox quote table tabs tcl\n");
9590      rc = 1;
9591    }
9592    p->cMode = p->mode;
9593  }else
9594
9595#ifndef SQLITE_SHELL_FIDDLE
9596  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
9597    if( nArg!=2 ){
9598      raw_printf(stderr, "Usage: .nonce NONCE\n");
9599      rc = 1;
9600    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
9601      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9602                 p->lineno, azArg[1]);
9603      exit(1);
9604    }else{
9605      p->bSafeMode = 0;
9606      return 0;  /* Return immediately to bypass the safe mode reset
9607                 ** at the end of this procedure */
9608    }
9609  }else
9610#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9611
9612  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
9613    if( nArg==2 ){
9614      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9615                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9616    }else{
9617      raw_printf(stderr, "Usage: .nullvalue STRING\n");
9618      rc = 1;
9619    }
9620  }else
9621
9622  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
9623    const char *zFN = 0;     /* Pointer to constant filename */
9624    char *zNewFilename = 0;  /* Name of the database file to open */
9625    int iName = 1;           /* Index in azArg[] of the filename */
9626    int newFlag = 0;         /* True to delete file before opening */
9627    int openMode = SHELL_OPEN_UNSPEC;
9628
9629    /* Check for command-line arguments */
9630    for(iName=1; iName<nArg; iName++){
9631      const char *z = azArg[iName];
9632#ifndef SQLITE_SHELL_FIDDLE
9633      if( optionMatch(z,"new") ){
9634        newFlag = 1;
9635#ifdef SQLITE_HAVE_ZLIB
9636      }else if( optionMatch(z, "zip") ){
9637        openMode = SHELL_OPEN_ZIPFILE;
9638#endif
9639      }else if( optionMatch(z, "append") ){
9640        openMode = SHELL_OPEN_APPENDVFS;
9641      }else if( optionMatch(z, "readonly") ){
9642        openMode = SHELL_OPEN_READONLY;
9643      }else if( optionMatch(z, "nofollow") ){
9644        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9645#ifndef SQLITE_OMIT_DESERIALIZE
9646      }else if( optionMatch(z, "deserialize") ){
9647        openMode = SHELL_OPEN_DESERIALIZE;
9648      }else if( optionMatch(z, "hexdb") ){
9649        openMode = SHELL_OPEN_HEXDB;
9650      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9651        p->szMax = integerValue(azArg[++iName]);
9652#endif /* SQLITE_OMIT_DESERIALIZE */
9653      }else
9654#endif /* !SQLITE_SHELL_FIDDLE */
9655      if( z[0]=='-' ){
9656        utf8_printf(stderr, "unknown option: %s\n", z);
9657        rc = 1;
9658        goto meta_command_exit;
9659      }else if( zFN ){
9660        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9661        rc = 1;
9662        goto meta_command_exit;
9663      }else{
9664        zFN = z;
9665      }
9666    }
9667
9668    /* Close the existing database */
9669    session_close_all(p, -1);
9670    close_db(p->db);
9671    p->db = 0;
9672    p->pAuxDb->zDbFilename = 0;
9673    sqlite3_free(p->pAuxDb->zFreeOnClose);
9674    p->pAuxDb->zFreeOnClose = 0;
9675    p->openMode = openMode;
9676    p->openFlags = 0;
9677    p->szMax = 0;
9678
9679    /* If a filename is specified, try to open it first */
9680    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9681      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9682#ifndef SQLITE_SHELL_FIDDLE
9683      if( p->bSafeMode
9684       && p->openMode!=SHELL_OPEN_HEXDB
9685       && zFN
9686       && strcmp(zFN,":memory:")!=0
9687      ){
9688        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9689      }
9690#else
9691      /* WASM mode has its own sandboxed pseudo-filesystem. */
9692#endif
9693      if( zFN ){
9694        zNewFilename = sqlite3_mprintf("%s", zFN);
9695        shell_check_oom(zNewFilename);
9696      }else{
9697        zNewFilename = 0;
9698      }
9699      p->pAuxDb->zDbFilename = zNewFilename;
9700      open_db(p, OPEN_DB_KEEPALIVE);
9701      if( p->db==0 ){
9702        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9703        sqlite3_free(zNewFilename);
9704      }else{
9705        p->pAuxDb->zFreeOnClose = zNewFilename;
9706      }
9707    }
9708    if( p->db==0 ){
9709      /* As a fall-back open a TEMP database */
9710      p->pAuxDb->zDbFilename = 0;
9711      open_db(p, 0);
9712    }
9713  }else
9714
9715#ifndef SQLITE_SHELL_FIDDLE
9716  if( (c=='o'
9717        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
9718   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
9719  ){
9720    char *zFile = 0;
9721    int bTxtMode = 0;
9722    int i;
9723    int eMode = 0;
9724    int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
9725    unsigned char zBOM[4];    /* Byte-order mark to using if --bom is present */
9726
9727    zBOM[0] = 0;
9728    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9729    if( c=='e' ){
9730      eMode = 'x';
9731      bOnce = 2;
9732    }else if( strncmp(azArg[0],"once",n)==0 ){
9733      bOnce = 1;
9734    }
9735    for(i=1; i<nArg; i++){
9736      char *z = azArg[i];
9737      if( z[0]=='-' ){
9738        if( z[1]=='-' ) z++;
9739        if( strcmp(z,"-bom")==0 ){
9740          zBOM[0] = 0xef;
9741          zBOM[1] = 0xbb;
9742          zBOM[2] = 0xbf;
9743          zBOM[3] = 0;
9744        }else if( c!='e' && strcmp(z,"-x")==0 ){
9745          eMode = 'x';  /* spreadsheet */
9746        }else if( c!='e' && strcmp(z,"-e")==0 ){
9747          eMode = 'e';  /* text editor */
9748        }else{
9749          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9750                      azArg[i]);
9751          showHelp(p->out, azArg[0]);
9752          rc = 1;
9753          goto meta_command_exit;
9754        }
9755      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9756        zFile = sqlite3_mprintf("%s", z);
9757        if( zFile && zFile[0]=='|' ){
9758          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9759          break;
9760        }
9761      }else{
9762        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9763                    azArg[i]);
9764        showHelp(p->out, azArg[0]);
9765        rc = 1;
9766        sqlite3_free(zFile);
9767        goto meta_command_exit;
9768      }
9769    }
9770    if( zFile==0 ){
9771      zFile = sqlite3_mprintf("stdout");
9772    }
9773    if( bOnce ){
9774      p->outCount = 2;
9775    }else{
9776      p->outCount = 0;
9777    }
9778    output_reset(p);
9779#ifndef SQLITE_NOHAVE_SYSTEM
9780    if( eMode=='e' || eMode=='x' ){
9781      p->doXdgOpen = 1;
9782      outputModePush(p);
9783      if( eMode=='x' ){
9784        /* spreadsheet mode.  Output as CSV. */
9785        newTempFile(p, "csv");
9786        ShellClearFlag(p, SHFLG_Echo);
9787        p->mode = MODE_Csv;
9788        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9789        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9790      }else{
9791        /* text editor mode */
9792        newTempFile(p, "txt");
9793        bTxtMode = 1;
9794      }
9795      sqlite3_free(zFile);
9796      zFile = sqlite3_mprintf("%s", p->zTempFile);
9797    }
9798#endif /* SQLITE_NOHAVE_SYSTEM */
9799    shell_check_oom(zFile);
9800    if( zFile[0]=='|' ){
9801#ifdef SQLITE_OMIT_POPEN
9802      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9803      rc = 1;
9804      p->out = stdout;
9805#else
9806      p->out = popen(zFile + 1, "w");
9807      if( p->out==0 ){
9808        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9809        p->out = stdout;
9810        rc = 1;
9811      }else{
9812        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9813        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9814      }
9815#endif
9816    }else{
9817      p->out = output_file_open(zFile, bTxtMode);
9818      if( p->out==0 ){
9819        if( strcmp(zFile,"off")!=0 ){
9820          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9821        }
9822        p->out = stdout;
9823        rc = 1;
9824      } else {
9825        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9826        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9827      }
9828    }
9829    sqlite3_free(zFile);
9830  }else
9831#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9832
9833  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
9834    open_db(p,0);
9835    if( nArg<=1 ) goto parameter_syntax_error;
9836
9837    /* .parameter clear
9838    ** Clear all bind parameters by dropping the TEMP table that holds them.
9839    */
9840    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
9841      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9842                   0, 0, 0);
9843    }else
9844
9845    /* .parameter list
9846    ** List all bind parameters.
9847    */
9848    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
9849      sqlite3_stmt *pStmt = 0;
9850      int rx;
9851      int len = 0;
9852      rx = sqlite3_prepare_v2(p->db,
9853             "SELECT max(length(key)) "
9854             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9855      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9856        len = sqlite3_column_int(pStmt, 0);
9857        if( len>40 ) len = 40;
9858      }
9859      sqlite3_finalize(pStmt);
9860      pStmt = 0;
9861      if( len ){
9862        rx = sqlite3_prepare_v2(p->db,
9863             "SELECT key, quote(value) "
9864             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9865        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9866          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9867                      sqlite3_column_text(pStmt,1));
9868        }
9869        sqlite3_finalize(pStmt);
9870      }
9871    }else
9872
9873    /* .parameter init
9874    ** Make sure the TEMP table used to hold bind parameters exists.
9875    ** Create it if necessary.
9876    */
9877    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
9878      bind_table_init(p);
9879    }else
9880
9881    /* .parameter set NAME VALUE
9882    ** Set or reset a bind parameter.  NAME should be the full parameter
9883    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9884    ** VALUE can be in either SQL literal notation, or if not it will be
9885    ** understood to be a text string.
9886    */
9887    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
9888      int rx;
9889      char *zSql;
9890      sqlite3_stmt *pStmt;
9891      const char *zKey = azArg[2];
9892      const char *zValue = azArg[3];
9893      bind_table_init(p);
9894      zSql = sqlite3_mprintf(
9895                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9896                  "VALUES(%Q,%s);", zKey, zValue);
9897      shell_check_oom(zSql);
9898      pStmt = 0;
9899      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9900      sqlite3_free(zSql);
9901      if( rx!=SQLITE_OK ){
9902        sqlite3_finalize(pStmt);
9903        pStmt = 0;
9904        zSql = sqlite3_mprintf(
9905                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9906                   "VALUES(%Q,%Q);", zKey, zValue);
9907        shell_check_oom(zSql);
9908        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9909        sqlite3_free(zSql);
9910        if( rx!=SQLITE_OK ){
9911          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9912          sqlite3_finalize(pStmt);
9913          pStmt = 0;
9914          rc = 1;
9915        }
9916      }
9917      sqlite3_step(pStmt);
9918      sqlite3_finalize(pStmt);
9919    }else
9920
9921    /* .parameter unset NAME
9922    ** Remove the NAME binding from the parameter binding table, if it
9923    ** exists.
9924    */
9925    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
9926      char *zSql = sqlite3_mprintf(
9927          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9928      shell_check_oom(zSql);
9929      sqlite3_exec(p->db, zSql, 0, 0, 0);
9930      sqlite3_free(zSql);
9931    }else
9932    /* If no command name matches, show a syntax error */
9933    parameter_syntax_error:
9934    showHelp(p->out, "parameter");
9935  }else
9936
9937  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
9938    int i;
9939    for(i=1; i<nArg; i++){
9940      if( i>1 ) raw_printf(p->out, " ");
9941      utf8_printf(p->out, "%s", azArg[i]);
9942    }
9943    raw_printf(p->out, "\n");
9944  }else
9945
9946#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9947  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
9948    int i;
9949    int nn = 0;
9950    p->flgProgress = 0;
9951    p->mxProgress = 0;
9952    p->nProgress = 0;
9953    for(i=1; i<nArg; i++){
9954      const char *z = azArg[i];
9955      if( z[0]=='-' ){
9956        z++;
9957        if( z[0]=='-' ) z++;
9958        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9959          p->flgProgress |= SHELL_PROGRESS_QUIET;
9960          continue;
9961        }
9962        if( strcmp(z,"reset")==0 ){
9963          p->flgProgress |= SHELL_PROGRESS_RESET;
9964          continue;
9965        }
9966        if( strcmp(z,"once")==0 ){
9967          p->flgProgress |= SHELL_PROGRESS_ONCE;
9968          continue;
9969        }
9970        if( strcmp(z,"limit")==0 ){
9971          if( i+1>=nArg ){
9972            utf8_printf(stderr, "Error: missing argument on --limit\n");
9973            rc = 1;
9974            goto meta_command_exit;
9975          }else{
9976            p->mxProgress = (int)integerValue(azArg[++i]);
9977          }
9978          continue;
9979        }
9980        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9981        rc = 1;
9982        goto meta_command_exit;
9983      }else{
9984        nn = (int)integerValue(z);
9985      }
9986    }
9987    open_db(p, 0);
9988    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9989  }else
9990#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9991
9992  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9993    if( nArg >= 2) {
9994      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9995    }
9996    if( nArg >= 3) {
9997      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9998    }
9999  }else
10000
10001#ifndef SQLITE_SHELL_FIDDLE
10002  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
10003    rc = 2;
10004  }else
10005#endif
10006
10007#ifndef SQLITE_SHELL_FIDDLE
10008  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
10009    FILE *inSaved = p->in;
10010    int savedLineno = p->lineno;
10011    failIfSafeMode(p, "cannot run .read in safe mode");
10012    if( nArg!=2 ){
10013      raw_printf(stderr, "Usage: .read FILE\n");
10014      rc = 1;
10015      goto meta_command_exit;
10016    }
10017    if( azArg[1][0]=='|' ){
10018#ifdef SQLITE_OMIT_POPEN
10019      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
10020      rc = 1;
10021      p->out = stdout;
10022#else
10023      p->in = popen(azArg[1]+1, "r");
10024      if( p->in==0 ){
10025        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
10026        rc = 1;
10027      }else{
10028        rc = process_input(p);
10029        pclose(p->in);
10030      }
10031#endif
10032    }else if( (p->in = openChrSource(azArg[1]))==0 ){
10033      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
10034      rc = 1;
10035    }else{
10036      rc = process_input(p);
10037      fclose(p->in);
10038    }
10039    p->in = inSaved;
10040    p->lineno = savedLineno;
10041  }else
10042#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10043
10044#ifndef SQLITE_SHELL_FIDDLE
10045  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
10046    const char *zSrcFile;
10047    const char *zDb;
10048    sqlite3 *pSrc;
10049    sqlite3_backup *pBackup;
10050    int nTimeout = 0;
10051
10052    failIfSafeMode(p, "cannot run .restore in safe mode");
10053    if( nArg==2 ){
10054      zSrcFile = azArg[1];
10055      zDb = "main";
10056    }else if( nArg==3 ){
10057      zSrcFile = azArg[2];
10058      zDb = azArg[1];
10059    }else{
10060      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
10061      rc = 1;
10062      goto meta_command_exit;
10063    }
10064    rc = sqlite3_open(zSrcFile, &pSrc);
10065    if( rc!=SQLITE_OK ){
10066      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
10067      close_db(pSrc);
10068      return 1;
10069    }
10070    open_db(p, 0);
10071    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
10072    if( pBackup==0 ){
10073      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10074      close_db(pSrc);
10075      return 1;
10076    }
10077    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
10078          || rc==SQLITE_BUSY  ){
10079      if( rc==SQLITE_BUSY ){
10080        if( nTimeout++ >= 3 ) break;
10081        sqlite3_sleep(100);
10082      }
10083    }
10084    sqlite3_backup_finish(pBackup);
10085    if( rc==SQLITE_DONE ){
10086      rc = 0;
10087    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
10088      raw_printf(stderr, "Error: source database is busy\n");
10089      rc = 1;
10090    }else{
10091      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10092      rc = 1;
10093    }
10094    close_db(pSrc);
10095  }else
10096#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10097
10098  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
10099    if( nArg==2 ){
10100      p->scanstatsOn = (u8)booleanValue(azArg[1]);
10101#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10102      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
10103#endif
10104    }else{
10105      raw_printf(stderr, "Usage: .scanstats on|off\n");
10106      rc = 1;
10107    }
10108  }else
10109
10110  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
10111    ShellText sSelect;
10112    ShellState data;
10113    char *zErrMsg = 0;
10114    const char *zDiv = "(";
10115    const char *zName = 0;
10116    int iSchema = 0;
10117    int bDebug = 0;
10118    int bNoSystemTabs = 0;
10119    int ii;
10120
10121    open_db(p, 0);
10122    memcpy(&data, p, sizeof(data));
10123    data.showHeader = 0;
10124    data.cMode = data.mode = MODE_Semi;
10125    initText(&sSelect);
10126    for(ii=1; ii<nArg; ii++){
10127      if( optionMatch(azArg[ii],"indent") ){
10128        data.cMode = data.mode = MODE_Pretty;
10129      }else if( optionMatch(azArg[ii],"debug") ){
10130        bDebug = 1;
10131      }else if( optionMatch(azArg[ii],"nosys") ){
10132        bNoSystemTabs = 1;
10133      }else if( azArg[ii][0]=='-' ){
10134        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
10135        rc = 1;
10136        goto meta_command_exit;
10137      }else if( zName==0 ){
10138        zName = azArg[ii];
10139      }else{
10140        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10141        rc = 1;
10142        goto meta_command_exit;
10143      }
10144    }
10145    if( zName!=0 ){
10146      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
10147                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
10148                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
10149                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
10150      if( isSchema ){
10151        char *new_argv[2], *new_colv[2];
10152        new_argv[0] = sqlite3_mprintf(
10153                      "CREATE TABLE %s (\n"
10154                      "  type text,\n"
10155                      "  name text,\n"
10156                      "  tbl_name text,\n"
10157                      "  rootpage integer,\n"
10158                      "  sql text\n"
10159                      ")", zName);
10160        shell_check_oom(new_argv[0]);
10161        new_argv[1] = 0;
10162        new_colv[0] = "sql";
10163        new_colv[1] = 0;
10164        callback(&data, 1, new_argv, new_colv);
10165        sqlite3_free(new_argv[0]);
10166      }
10167    }
10168    if( zDiv ){
10169      sqlite3_stmt *pStmt = 0;
10170      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
10171                              -1, &pStmt, 0);
10172      if( rc ){
10173        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10174        sqlite3_finalize(pStmt);
10175        rc = 1;
10176        goto meta_command_exit;
10177      }
10178      appendText(&sSelect, "SELECT sql FROM", 0);
10179      iSchema = 0;
10180      while( sqlite3_step(pStmt)==SQLITE_ROW ){
10181        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
10182        char zScNum[30];
10183        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
10184        appendText(&sSelect, zDiv, 0);
10185        zDiv = " UNION ALL ";
10186        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
10187        if( sqlite3_stricmp(zDb, "main")!=0 ){
10188          appendText(&sSelect, zDb, '\'');
10189        }else{
10190          appendText(&sSelect, "NULL", 0);
10191        }
10192        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10193        appendText(&sSelect, zScNum, 0);
10194        appendText(&sSelect, " AS snum, ", 0);
10195        appendText(&sSelect, zDb, '\'');
10196        appendText(&sSelect, " AS sname FROM ", 0);
10197        appendText(&sSelect, zDb, quoteChar(zDb));
10198        appendText(&sSelect, ".sqlite_schema", 0);
10199      }
10200      sqlite3_finalize(pStmt);
10201#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10202      if( zName ){
10203        appendText(&sSelect,
10204           " UNION ALL SELECT shell_module_schema(name),"
10205           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10206        0);
10207      }
10208#endif
10209      appendText(&sSelect, ") WHERE ", 0);
10210      if( zName ){
10211        char *zQarg = sqlite3_mprintf("%Q", zName);
10212        int bGlob;
10213        shell_check_oom(zQarg);
10214        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10215                strchr(zName, '[') != 0;
10216        if( strchr(zName, '.') ){
10217          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10218        }else{
10219          appendText(&sSelect, "lower(tbl_name)", 0);
10220        }
10221        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10222        appendText(&sSelect, zQarg, 0);
10223        if( !bGlob ){
10224          appendText(&sSelect, " ESCAPE '\\' ", 0);
10225        }
10226        appendText(&sSelect, " AND ", 0);
10227        sqlite3_free(zQarg);
10228      }
10229      if( bNoSystemTabs ){
10230        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10231      }
10232      appendText(&sSelect, "sql IS NOT NULL"
10233                           " ORDER BY snum, rowid", 0);
10234      if( bDebug ){
10235        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
10236      }else{
10237        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10238      }
10239      freeText(&sSelect);
10240    }
10241    if( zErrMsg ){
10242      utf8_printf(stderr,"Error: %s\n", zErrMsg);
10243      sqlite3_free(zErrMsg);
10244      rc = 1;
10245    }else if( rc != SQLITE_OK ){
10246      raw_printf(stderr,"Error: querying schema information\n");
10247      rc = 1;
10248    }else{
10249      rc = 0;
10250    }
10251  }else
10252
10253  if( (c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0)
10254   || (c=='t' && n==9  && strncmp(azArg[0], "treetrace", n)==0)
10255  ){
10256    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10257    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10258  }else
10259
10260#if defined(SQLITE_ENABLE_SESSION)
10261  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
10262    struct AuxDb *pAuxDb = p->pAuxDb;
10263    OpenSession *pSession = &pAuxDb->aSession[0];
10264    char **azCmd = &azArg[1];
10265    int iSes = 0;
10266    int nCmd = nArg - 1;
10267    int i;
10268    if( nArg<=1 ) goto session_syntax_error;
10269    open_db(p, 0);
10270    if( nArg>=3 ){
10271      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10272        if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10273      }
10274      if( iSes<pAuxDb->nSession ){
10275        pSession = &pAuxDb->aSession[iSes];
10276        azCmd++;
10277        nCmd--;
10278      }else{
10279        pSession = &pAuxDb->aSession[0];
10280        iSes = 0;
10281      }
10282    }
10283
10284    /* .session attach TABLE
10285    ** Invoke the sqlite3session_attach() interface to attach a particular
10286    ** table so that it is never filtered.
10287    */
10288    if( strcmp(azCmd[0],"attach")==0 ){
10289      if( nCmd!=2 ) goto session_syntax_error;
10290      if( pSession->p==0 ){
10291        session_not_open:
10292        raw_printf(stderr, "ERROR: No sessions are open\n");
10293      }else{
10294        rc = sqlite3session_attach(pSession->p, azCmd[1]);
10295        if( rc ){
10296          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
10297          rc = 0;
10298        }
10299      }
10300    }else
10301
10302    /* .session changeset FILE
10303    ** .session patchset FILE
10304    ** Write a changeset or patchset into a file.  The file is overwritten.
10305    */
10306    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
10307      FILE *out = 0;
10308      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10309      if( nCmd!=2 ) goto session_syntax_error;
10310      if( pSession->p==0 ) goto session_not_open;
10311      out = fopen(azCmd[1], "wb");
10312      if( out==0 ){
10313        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
10314                    azCmd[1]);
10315      }else{
10316        int szChng;
10317        void *pChng;
10318        if( azCmd[0][0]=='c' ){
10319          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10320        }else{
10321          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10322        }
10323        if( rc ){
10324          printf("Error: error code %d\n", rc);
10325          rc = 0;
10326        }
10327        if( pChng
10328          && fwrite(pChng, szChng, 1, out)!=1 ){
10329          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
10330                  szChng);
10331        }
10332        sqlite3_free(pChng);
10333        fclose(out);
10334      }
10335    }else
10336
10337    /* .session close
10338    ** Close the identified session
10339    */
10340    if( strcmp(azCmd[0], "close")==0 ){
10341      if( nCmd!=1 ) goto session_syntax_error;
10342      if( pAuxDb->nSession ){
10343        session_close(pSession);
10344        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10345      }
10346    }else
10347
10348    /* .session enable ?BOOLEAN?
10349    ** Query or set the enable flag
10350    */
10351    if( strcmp(azCmd[0], "enable")==0 ){
10352      int ii;
10353      if( nCmd>2 ) goto session_syntax_error;
10354      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10355      if( pAuxDb->nSession ){
10356        ii = sqlite3session_enable(pSession->p, ii);
10357        utf8_printf(p->out, "session %s enable flag = %d\n",
10358                    pSession->zName, ii);
10359      }
10360    }else
10361
10362    /* .session filter GLOB ....
10363    ** Set a list of GLOB patterns of table names to be excluded.
10364    */
10365    if( strcmp(azCmd[0], "filter")==0 ){
10366      int ii, nByte;
10367      if( nCmd<2 ) goto session_syntax_error;
10368      if( pAuxDb->nSession ){
10369        for(ii=0; ii<pSession->nFilter; ii++){
10370          sqlite3_free(pSession->azFilter[ii]);
10371        }
10372        sqlite3_free(pSession->azFilter);
10373        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10374        pSession->azFilter = sqlite3_malloc( nByte );
10375        if( pSession->azFilter==0 ){
10376          raw_printf(stderr, "Error: out or memory\n");
10377          exit(1);
10378        }
10379        for(ii=1; ii<nCmd; ii++){
10380          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10381          shell_check_oom(x);
10382        }
10383        pSession->nFilter = ii-1;
10384      }
10385    }else
10386
10387    /* .session indirect ?BOOLEAN?
10388    ** Query or set the indirect flag
10389    */
10390    if( strcmp(azCmd[0], "indirect")==0 ){
10391      int ii;
10392      if( nCmd>2 ) goto session_syntax_error;
10393      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10394      if( pAuxDb->nSession ){
10395        ii = sqlite3session_indirect(pSession->p, ii);
10396        utf8_printf(p->out, "session %s indirect flag = %d\n",
10397                    pSession->zName, ii);
10398      }
10399    }else
10400
10401    /* .session isempty
10402    ** Determine if the session is empty
10403    */
10404    if( strcmp(azCmd[0], "isempty")==0 ){
10405      int ii;
10406      if( nCmd!=1 ) goto session_syntax_error;
10407      if( pAuxDb->nSession ){
10408        ii = sqlite3session_isempty(pSession->p);
10409        utf8_printf(p->out, "session %s isempty flag = %d\n",
10410                    pSession->zName, ii);
10411      }
10412    }else
10413
10414    /* .session list
10415    ** List all currently open sessions
10416    */
10417    if( strcmp(azCmd[0],"list")==0 ){
10418      for(i=0; i<pAuxDb->nSession; i++){
10419        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
10420      }
10421    }else
10422
10423    /* .session open DB NAME
10424    ** Open a new session called NAME on the attached database DB.
10425    ** DB is normally "main".
10426    */
10427    if( strcmp(azCmd[0],"open")==0 ){
10428      char *zName;
10429      if( nCmd!=3 ) goto session_syntax_error;
10430      zName = azCmd[2];
10431      if( zName[0]==0 ) goto session_syntax_error;
10432      for(i=0; i<pAuxDb->nSession; i++){
10433        if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10434          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
10435          goto meta_command_exit;
10436        }
10437      }
10438      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10439        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10440        goto meta_command_exit;
10441      }
10442      pSession = &pAuxDb->aSession[pAuxDb->nSession];
10443      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10444      if( rc ){
10445        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
10446        rc = 0;
10447        goto meta_command_exit;
10448      }
10449      pSession->nFilter = 0;
10450      sqlite3session_table_filter(pSession->p, session_filter, pSession);
10451      pAuxDb->nSession++;
10452      pSession->zName = sqlite3_mprintf("%s", zName);
10453      shell_check_oom(pSession->zName);
10454    }else
10455    /* If no command name matches, show a syntax error */
10456    session_syntax_error:
10457    showHelp(p->out, "session");
10458  }else
10459#endif
10460
10461#ifdef SQLITE_DEBUG
10462  /* Undocumented commands for internal testing.  Subject to change
10463  ** without notice. */
10464  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
10465    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10466      int i, v;
10467      for(i=1; i<nArg; i++){
10468        v = booleanValue(azArg[i]);
10469        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
10470      }
10471    }
10472    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
10473      int i; sqlite3_int64 v;
10474      for(i=1; i<nArg; i++){
10475        char zBuf[200];
10476        v = integerValue(azArg[i]);
10477        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10478        utf8_printf(p->out, "%s", zBuf);
10479      }
10480    }
10481  }else
10482#endif
10483
10484  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
10485    int bIsInit = 0;         /* True to initialize the SELFTEST table */
10486    int bVerbose = 0;        /* Verbose output */
10487    int bSelftestExists;     /* True if SELFTEST already exists */
10488    int i, k;                /* Loop counters */
10489    int nTest = 0;           /* Number of tests runs */
10490    int nErr = 0;            /* Number of errors seen */
10491    ShellText str;           /* Answer for a query */
10492    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10493
10494    open_db(p,0);
10495    for(i=1; i<nArg; i++){
10496      const char *z = azArg[i];
10497      if( z[0]=='-' && z[1]=='-' ) z++;
10498      if( strcmp(z,"-init")==0 ){
10499        bIsInit = 1;
10500      }else
10501      if( strcmp(z,"-v")==0 ){
10502        bVerbose++;
10503      }else
10504      {
10505        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10506                    azArg[i], azArg[0]);
10507        raw_printf(stderr, "Should be one of: --init -v\n");
10508        rc = 1;
10509        goto meta_command_exit;
10510      }
10511    }
10512    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10513           != SQLITE_OK ){
10514      bSelftestExists = 0;
10515    }else{
10516      bSelftestExists = 1;
10517    }
10518    if( bIsInit ){
10519      createSelftestTable(p);
10520      bSelftestExists = 1;
10521    }
10522    initText(&str);
10523    appendText(&str, "x", 0);
10524    for(k=bSelftestExists; k>=0; k--){
10525      if( k==1 ){
10526        rc = sqlite3_prepare_v2(p->db,
10527            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10528            -1, &pStmt, 0);
10529      }else{
10530        rc = sqlite3_prepare_v2(p->db,
10531          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10532          "      (1,'run','PRAGMA integrity_check','ok')",
10533          -1, &pStmt, 0);
10534      }
10535      if( rc ){
10536        raw_printf(stderr, "Error querying the selftest table\n");
10537        rc = 1;
10538        sqlite3_finalize(pStmt);
10539        goto meta_command_exit;
10540      }
10541      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10542        int tno = sqlite3_column_int(pStmt, 0);
10543        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10544        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10545        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10546
10547        if( zOp==0 ) continue;
10548        if( zSql==0 ) continue;
10549        if( zAns==0 ) continue;
10550        k = 0;
10551        if( bVerbose>0 ){
10552          printf("%d: %s %s\n", tno, zOp, zSql);
10553        }
10554        if( strcmp(zOp,"memo")==0 ){
10555          utf8_printf(p->out, "%s\n", zSql);
10556        }else
10557        if( strcmp(zOp,"run")==0 ){
10558          char *zErrMsg = 0;
10559          str.n = 0;
10560          str.z[0] = 0;
10561          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10562          nTest++;
10563          if( bVerbose ){
10564            utf8_printf(p->out, "Result: %s\n", str.z);
10565          }
10566          if( rc || zErrMsg ){
10567            nErr++;
10568            rc = 1;
10569            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
10570            sqlite3_free(zErrMsg);
10571          }else if( strcmp(zAns,str.z)!=0 ){
10572            nErr++;
10573            rc = 1;
10574            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10575            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
10576          }
10577        }else
10578        {
10579          utf8_printf(stderr,
10580            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10581          rc = 1;
10582          break;
10583        }
10584      } /* End loop over rows of content from SELFTEST */
10585      sqlite3_finalize(pStmt);
10586    } /* End loop over k */
10587    freeText(&str);
10588    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10589  }else
10590
10591  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
10592    if( nArg<2 || nArg>3 ){
10593      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10594      rc = 1;
10595    }
10596    if( nArg>=2 ){
10597      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10598                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10599    }
10600    if( nArg>=3 ){
10601      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10602                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10603    }
10604  }else
10605
10606  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
10607    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
10608    int i;                   /* Loop counter */
10609    int bSchema = 0;         /* Also hash the schema */
10610    int bSeparate = 0;       /* Hash each table separately */
10611    int iSize = 224;         /* Hash algorithm to use */
10612    int bDebug = 0;          /* Only show the query that would have run */
10613    sqlite3_stmt *pStmt;     /* For querying tables names */
10614    char *zSql;              /* SQL to be run */
10615    char *zSep;              /* Separator */
10616    ShellText sSql;          /* Complete SQL for the query to run the hash */
10617    ShellText sQuery;        /* Set of queries used to read all content */
10618    open_db(p, 0);
10619    for(i=1; i<nArg; i++){
10620      const char *z = azArg[i];
10621      if( z[0]=='-' ){
10622        z++;
10623        if( z[0]=='-' ) z++;
10624        if( strcmp(z,"schema")==0 ){
10625          bSchema = 1;
10626        }else
10627        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
10628         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
10629        ){
10630          iSize = atoi(&z[5]);
10631        }else
10632        if( strcmp(z,"debug")==0 ){
10633          bDebug = 1;
10634        }else
10635        {
10636          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10637                      azArg[i], azArg[0]);
10638          showHelp(p->out, azArg[0]);
10639          rc = 1;
10640          goto meta_command_exit;
10641        }
10642      }else if( zLike ){
10643        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10644        rc = 1;
10645        goto meta_command_exit;
10646      }else{
10647        zLike = z;
10648        bSeparate = 1;
10649        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10650      }
10651    }
10652    if( bSchema ){
10653      zSql = "SELECT lower(name) FROM sqlite_schema"
10654             " WHERE type='table' AND coalesce(rootpage,0)>1"
10655             " UNION ALL SELECT 'sqlite_schema'"
10656             " ORDER BY 1 collate nocase";
10657    }else{
10658      zSql = "SELECT lower(name) FROM sqlite_schema"
10659             " WHERE type='table' AND coalesce(rootpage,0)>1"
10660             " AND name NOT LIKE 'sqlite_%'"
10661             " ORDER BY 1 collate nocase";
10662    }
10663    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10664    initText(&sQuery);
10665    initText(&sSql);
10666    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10667    zSep = "VALUES(";
10668    while( SQLITE_ROW==sqlite3_step(pStmt) ){
10669      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10670      if( zTab==0 ) continue;
10671      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10672      if( strncmp(zTab, "sqlite_",7)!=0 ){
10673        appendText(&sQuery,"SELECT * FROM ", 0);
10674        appendText(&sQuery,zTab,'"');
10675        appendText(&sQuery," NOT INDEXED;", 0);
10676      }else if( strcmp(zTab, "sqlite_schema")==0 ){
10677        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10678                           " ORDER BY name;", 0);
10679      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
10680        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10681                           " ORDER BY name;", 0);
10682      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
10683        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10684                           " ORDER BY tbl,idx;", 0);
10685      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
10686        appendText(&sQuery, "SELECT * FROM ", 0);
10687        appendText(&sQuery, zTab, 0);
10688        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10689      }
10690      appendText(&sSql, zSep, 0);
10691      appendText(&sSql, sQuery.z, '\'');
10692      sQuery.n = 0;
10693      appendText(&sSql, ",", 0);
10694      appendText(&sSql, zTab, '\'');
10695      zSep = "),(";
10696    }
10697    sqlite3_finalize(pStmt);
10698    if( bSeparate ){
10699      zSql = sqlite3_mprintf(
10700          "%s))"
10701          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10702          "   FROM [sha3sum$query]",
10703          sSql.z, iSize);
10704    }else{
10705      zSql = sqlite3_mprintf(
10706          "%s))"
10707          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10708          "   FROM [sha3sum$query]",
10709          sSql.z, iSize);
10710    }
10711    shell_check_oom(zSql);
10712    freeText(&sQuery);
10713    freeText(&sSql);
10714    if( bDebug ){
10715      utf8_printf(p->out, "%s\n", zSql);
10716    }else{
10717      shell_exec(p, zSql, 0);
10718    }
10719    sqlite3_free(zSql);
10720  }else
10721
10722#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10723  if( c=='s'
10724   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
10725  ){
10726    char *zCmd;
10727    int i, x;
10728    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10729    if( nArg<2 ){
10730      raw_printf(stderr, "Usage: .system COMMAND\n");
10731      rc = 1;
10732      goto meta_command_exit;
10733    }
10734    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10735    for(i=2; i<nArg && zCmd!=0; i++){
10736      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10737                             zCmd, azArg[i]);
10738    }
10739    x = zCmd!=0 ? system(zCmd) : 1;
10740    sqlite3_free(zCmd);
10741    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10742  }else
10743#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10744
10745  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
10746    static const char *azBool[] = { "off", "on", "trigger", "full"};
10747    const char *zOut;
10748    int i;
10749    if( nArg!=1 ){
10750      raw_printf(stderr, "Usage: .show\n");
10751      rc = 1;
10752      goto meta_command_exit;
10753    }
10754    utf8_printf(p->out, "%12.12s: %s\n","echo",
10755                azBool[ShellHasFlag(p, SHFLG_Echo)]);
10756    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10757    utf8_printf(p->out, "%12.12s: %s\n","explain",
10758         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10759    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10760    if( p->mode==MODE_Column
10761     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10762    ){
10763      utf8_printf
10764        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10765         modeDescr[p->mode], p->cmOpts.iWrap,
10766         p->cmOpts.bWordWrap ? "on" : "off",
10767         p->cmOpts.bQuote ? "" : "no");
10768    }else{
10769      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10770    }
10771    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10772      output_c_string(p->out, p->nullValue);
10773      raw_printf(p->out, "\n");
10774    utf8_printf(p->out,"%12.12s: %s\n","output",
10775            strlen30(p->outfile) ? p->outfile : "stdout");
10776    utf8_printf(p->out,"%12.12s: ", "colseparator");
10777      output_c_string(p->out, p->colSeparator);
10778      raw_printf(p->out, "\n");
10779    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10780      output_c_string(p->out, p->rowSeparator);
10781      raw_printf(p->out, "\n");
10782    switch( p->statsOn ){
10783      case 0:  zOut = "off";     break;
10784      default: zOut = "on";      break;
10785      case 2:  zOut = "stmt";    break;
10786      case 3:  zOut = "vmstep";  break;
10787    }
10788    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10789    utf8_printf(p->out, "%12.12s: ", "width");
10790    for (i=0;i<p->nWidth;i++) {
10791      raw_printf(p->out, "%d ", p->colWidth[i]);
10792    }
10793    raw_printf(p->out, "\n");
10794    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10795                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10796  }else
10797
10798  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
10799    if( nArg==2 ){
10800      if( strcmp(azArg[1],"stmt")==0 ){
10801        p->statsOn = 2;
10802      }else if( strcmp(azArg[1],"vmstep")==0 ){
10803        p->statsOn = 3;
10804      }else{
10805        p->statsOn = (u8)booleanValue(azArg[1]);
10806      }
10807    }else if( nArg==1 ){
10808      display_stats(p->db, p, 0);
10809    }else{
10810      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10811      rc = 1;
10812    }
10813  }else
10814
10815  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
10816   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
10817                 || strncmp(azArg[0], "indexes", n)==0) )
10818  ){
10819    sqlite3_stmt *pStmt;
10820    char **azResult;
10821    int nRow, nAlloc;
10822    int ii;
10823    ShellText s;
10824    initText(&s);
10825    open_db(p, 0);
10826    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10827    if( rc ){
10828      sqlite3_finalize(pStmt);
10829      return shellDatabaseError(p->db);
10830    }
10831
10832    if( nArg>2 && c=='i' ){
10833      /* It is an historical accident that the .indexes command shows an error
10834      ** when called with the wrong number of arguments whereas the .tables
10835      ** command does not. */
10836      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10837      rc = 1;
10838      sqlite3_finalize(pStmt);
10839      goto meta_command_exit;
10840    }
10841    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10842      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10843      if( zDbName==0 ) continue;
10844      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10845      if( sqlite3_stricmp(zDbName, "main")==0 ){
10846        appendText(&s, "SELECT name FROM ", 0);
10847      }else{
10848        appendText(&s, "SELECT ", 0);
10849        appendText(&s, zDbName, '\'');
10850        appendText(&s, "||'.'||name FROM ", 0);
10851      }
10852      appendText(&s, zDbName, '"');
10853      appendText(&s, ".sqlite_schema ", 0);
10854      if( c=='t' ){
10855        appendText(&s," WHERE type IN ('table','view')"
10856                      "   AND name NOT LIKE 'sqlite_%'"
10857                      "   AND name LIKE ?1", 0);
10858      }else{
10859        appendText(&s," WHERE type='index'"
10860                      "   AND tbl_name LIKE ?1", 0);
10861      }
10862    }
10863    rc = sqlite3_finalize(pStmt);
10864    if( rc==SQLITE_OK ){
10865      appendText(&s, " ORDER BY 1", 0);
10866      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10867    }
10868    freeText(&s);
10869    if( rc ) return shellDatabaseError(p->db);
10870
10871    /* Run the SQL statement prepared by the above block. Store the results
10872    ** as an array of nul-terminated strings in azResult[].  */
10873    nRow = nAlloc = 0;
10874    azResult = 0;
10875    if( nArg>1 ){
10876      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10877    }else{
10878      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10879    }
10880    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10881      if( nRow>=nAlloc ){
10882        char **azNew;
10883        int n2 = nAlloc*2 + 10;
10884        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10885        shell_check_oom(azNew);
10886        nAlloc = n2;
10887        azResult = azNew;
10888      }
10889      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10890      shell_check_oom(azResult[nRow]);
10891      nRow++;
10892    }
10893    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10894      rc = shellDatabaseError(p->db);
10895    }
10896
10897    /* Pretty-print the contents of array azResult[] to the output */
10898    if( rc==0 && nRow>0 ){
10899      int len, maxlen = 0;
10900      int i, j;
10901      int nPrintCol, nPrintRow;
10902      for(i=0; i<nRow; i++){
10903        len = strlen30(azResult[i]);
10904        if( len>maxlen ) maxlen = len;
10905      }
10906      nPrintCol = 80/(maxlen+2);
10907      if( nPrintCol<1 ) nPrintCol = 1;
10908      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10909      for(i=0; i<nPrintRow; i++){
10910        for(j=i; j<nRow; j+=nPrintRow){
10911          char *zSp = j<nPrintRow ? "" : "  ";
10912          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10913                      azResult[j] ? azResult[j]:"");
10914        }
10915        raw_printf(p->out, "\n");
10916      }
10917    }
10918
10919    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10920    sqlite3_free(azResult);
10921  }else
10922
10923#ifndef SQLITE_SHELL_FIDDLE
10924  /* Begin redirecting output to the file "testcase-out.txt" */
10925  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
10926    output_reset(p);
10927    p->out = output_file_open("testcase-out.txt", 0);
10928    if( p->out==0 ){
10929      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10930    }
10931    if( nArg>=2 ){
10932      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10933    }else{
10934      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10935    }
10936  }else
10937#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10938
10939#ifndef SQLITE_UNTESTABLE
10940  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
10941    static const struct {
10942       const char *zCtrlName;   /* Name of a test-control option */
10943       int ctrlCode;            /* Integer code for that option */
10944       int unSafe;              /* Not valid for --safe mode */
10945       const char *zUsage;      /* Usage notes */
10946    } aCtrl[] = {
10947      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10948      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10949    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10950    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10951      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10952      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10953    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10954      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10955      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10956      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10957      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10958      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10959#ifdef YYCOVERAGE
10960      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10961#endif
10962      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10963      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10964      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10965      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10966      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10967      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10968      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10969    };
10970    int testctrl = -1;
10971    int iCtrl = -1;
10972    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10973    int isOk = 0;
10974    int i, n2;
10975    const char *zCmd = 0;
10976
10977    open_db(p, 0);
10978    zCmd = nArg>=2 ? azArg[1] : "help";
10979
10980    /* The argument can optionally begin with "-" or "--" */
10981    if( zCmd[0]=='-' && zCmd[1] ){
10982      zCmd++;
10983      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10984    }
10985
10986    /* --help lists all test-controls */
10987    if( strcmp(zCmd,"help")==0 ){
10988      utf8_printf(p->out, "Available test-controls:\n");
10989      for(i=0; i<ArraySize(aCtrl); i++){
10990        utf8_printf(p->out, "  .testctrl %s %s\n",
10991                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10992      }
10993      rc = 1;
10994      goto meta_command_exit;
10995    }
10996
10997    /* convert testctrl text option to value. allow any unique prefix
10998    ** of the option name, or a numerical value. */
10999    n2 = strlen30(zCmd);
11000    for(i=0; i<ArraySize(aCtrl); i++){
11001      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
11002        if( testctrl<0 ){
11003          testctrl = aCtrl[i].ctrlCode;
11004          iCtrl = i;
11005        }else{
11006          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
11007                              "Use \".testctrl --help\" for help\n", zCmd);
11008          rc = 1;
11009          goto meta_command_exit;
11010        }
11011      }
11012    }
11013    if( testctrl<0 ){
11014      utf8_printf(stderr,"Error: unknown test-control: %s\n"
11015                         "Use \".testctrl --help\" for help\n", zCmd);
11016    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
11017      utf8_printf(stderr,
11018         "line %d: \".testctrl %s\" may not be used in safe mode\n",
11019         p->lineno, aCtrl[iCtrl].zCtrlName);
11020      exit(1);
11021    }else{
11022      switch(testctrl){
11023
11024        /* sqlite3_test_control(int, db, int) */
11025        case SQLITE_TESTCTRL_OPTIMIZATIONS:
11026          if( nArg==3 ){
11027            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
11028            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11029            isOk = 3;
11030          }
11031          break;
11032
11033        /* sqlite3_test_control(int) */
11034        case SQLITE_TESTCTRL_PRNG_SAVE:
11035        case SQLITE_TESTCTRL_PRNG_RESTORE:
11036        case SQLITE_TESTCTRL_BYTEORDER:
11037          if( nArg==2 ){
11038            rc2 = sqlite3_test_control(testctrl);
11039            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
11040          }
11041          break;
11042
11043        /* sqlite3_test_control(int, uint) */
11044        case SQLITE_TESTCTRL_PENDING_BYTE:
11045          if( nArg==3 ){
11046            unsigned int opt = (unsigned int)integerValue(azArg[2]);
11047            rc2 = sqlite3_test_control(testctrl, opt);
11048            isOk = 3;
11049          }
11050          break;
11051
11052        /* sqlite3_test_control(int, int, sqlite3*) */
11053        case SQLITE_TESTCTRL_PRNG_SEED:
11054          if( nArg==3 || nArg==4 ){
11055            int ii = (int)integerValue(azArg[2]);
11056            sqlite3 *db;
11057            if( ii==0 && strcmp(azArg[2],"random")==0 ){
11058              sqlite3_randomness(sizeof(ii),&ii);
11059              printf("-- random seed: %d\n", ii);
11060            }
11061            if( nArg==3 ){
11062              db = 0;
11063            }else{
11064              db = p->db;
11065              /* Make sure the schema has been loaded */
11066              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
11067            }
11068            rc2 = sqlite3_test_control(testctrl, ii, db);
11069            isOk = 3;
11070          }
11071          break;
11072
11073        /* sqlite3_test_control(int, int) */
11074        case SQLITE_TESTCTRL_ASSERT:
11075        case SQLITE_TESTCTRL_ALWAYS:
11076          if( nArg==3 ){
11077            int opt = booleanValue(azArg[2]);
11078            rc2 = sqlite3_test_control(testctrl, opt);
11079            isOk = 1;
11080          }
11081          break;
11082
11083        /* sqlite3_test_control(int, int) */
11084        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
11085        case SQLITE_TESTCTRL_NEVER_CORRUPT:
11086          if( nArg==3 ){
11087            int opt = booleanValue(azArg[2]);
11088            rc2 = sqlite3_test_control(testctrl, opt);
11089            isOk = 3;
11090          }
11091          break;
11092
11093        /* sqlite3_test_control(sqlite3*) */
11094        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
11095          rc2 = sqlite3_test_control(testctrl, p->db);
11096          isOk = 3;
11097          break;
11098
11099        case SQLITE_TESTCTRL_IMPOSTER:
11100          if( nArg==5 ){
11101            rc2 = sqlite3_test_control(testctrl, p->db,
11102                          azArg[2],
11103                          integerValue(azArg[3]),
11104                          integerValue(azArg[4]));
11105            isOk = 3;
11106          }
11107          break;
11108
11109        case SQLITE_TESTCTRL_SEEK_COUNT: {
11110          u64 x = 0;
11111          rc2 = sqlite3_test_control(testctrl, p->db, &x);
11112          utf8_printf(p->out, "%llu\n", x);
11113          isOk = 3;
11114          break;
11115        }
11116
11117#ifdef YYCOVERAGE
11118        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
11119          if( nArg==2 ){
11120            sqlite3_test_control(testctrl, p->out);
11121            isOk = 3;
11122          }
11123          break;
11124        }
11125#endif
11126#ifdef SQLITE_DEBUG
11127        case SQLITE_TESTCTRL_TUNE: {
11128          if( nArg==4 ){
11129            int id = (int)integerValue(azArg[2]);
11130            int val = (int)integerValue(azArg[3]);
11131            sqlite3_test_control(testctrl, id, &val);
11132            isOk = 3;
11133          }else if( nArg==3 ){
11134            int id = (int)integerValue(azArg[2]);
11135            sqlite3_test_control(testctrl, -id, &rc2);
11136            isOk = 1;
11137          }else if( nArg==2 ){
11138            int id = 1;
11139            while(1){
11140              int val = 0;
11141              rc2 = sqlite3_test_control(testctrl, -id, &val);
11142              if( rc2!=SQLITE_OK ) break;
11143              if( id>1 ) utf8_printf(p->out, "  ");
11144              utf8_printf(p->out, "%d: %d", id, val);
11145              id++;
11146            }
11147            if( id>1 ) utf8_printf(p->out, "\n");
11148            isOk = 3;
11149          }
11150          break;
11151        }
11152#endif
11153        case SQLITE_TESTCTRL_SORTER_MMAP:
11154          if( nArg==3 ){
11155            int opt = (unsigned int)integerValue(azArg[2]);
11156            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11157            isOk = 3;
11158          }
11159          break;
11160      }
11161    }
11162    if( isOk==0 && iCtrl>=0 ){
11163      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11164      rc = 1;
11165    }else if( isOk==1 ){
11166      raw_printf(p->out, "%d\n", rc2);
11167    }else if( isOk==2 ){
11168      raw_printf(p->out, "0x%08x\n", rc2);
11169    }
11170  }else
11171#endif /* !defined(SQLITE_UNTESTABLE) */
11172
11173  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
11174    open_db(p, 0);
11175    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11176  }else
11177
11178  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
11179    if( nArg==2 ){
11180      enableTimer = booleanValue(azArg[1]);
11181      if( enableTimer && !HAS_TIMER ){
11182        raw_printf(stderr, "Error: timer not available on this system.\n");
11183        enableTimer = 0;
11184      }
11185    }else{
11186      raw_printf(stderr, "Usage: .timer on|off\n");
11187      rc = 1;
11188    }
11189  }else
11190
11191#ifndef SQLITE_OMIT_TRACE
11192  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
11193    int mType = 0;
11194    int jj;
11195    open_db(p, 0);
11196    for(jj=1; jj<nArg; jj++){
11197      const char *z = azArg[jj];
11198      if( z[0]=='-' ){
11199        if( optionMatch(z, "expanded") ){
11200          p->eTraceType = SHELL_TRACE_EXPANDED;
11201        }
11202#ifdef SQLITE_ENABLE_NORMALIZE
11203        else if( optionMatch(z, "normalized") ){
11204          p->eTraceType = SHELL_TRACE_NORMALIZED;
11205        }
11206#endif
11207        else if( optionMatch(z, "plain") ){
11208          p->eTraceType = SHELL_TRACE_PLAIN;
11209        }
11210        else if( optionMatch(z, "profile") ){
11211          mType |= SQLITE_TRACE_PROFILE;
11212        }
11213        else if( optionMatch(z, "row") ){
11214          mType |= SQLITE_TRACE_ROW;
11215        }
11216        else if( optionMatch(z, "stmt") ){
11217          mType |= SQLITE_TRACE_STMT;
11218        }
11219        else if( optionMatch(z, "close") ){
11220          mType |= SQLITE_TRACE_CLOSE;
11221        }
11222        else {
11223          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
11224          rc = 1;
11225          goto meta_command_exit;
11226        }
11227      }else{
11228        output_file_close(p->traceOut);
11229        p->traceOut = output_file_open(azArg[1], 0);
11230      }
11231    }
11232    if( p->traceOut==0 ){
11233      sqlite3_trace_v2(p->db, 0, 0, 0);
11234    }else{
11235      if( mType==0 ) mType = SQLITE_TRACE_STMT;
11236      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11237    }
11238  }else
11239#endif /* !defined(SQLITE_OMIT_TRACE) */
11240
11241#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11242  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
11243    int ii;
11244    int lenOpt;
11245    char *zOpt;
11246    if( nArg<2 ){
11247      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
11248      rc = 1;
11249      goto meta_command_exit;
11250    }
11251    open_db(p, 0);
11252    zOpt = azArg[1];
11253    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11254    lenOpt = (int)strlen(zOpt);
11255    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11256      assert( azArg[nArg]==0 );
11257      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11258    }else{
11259      for(ii=1; ii<nArg; ii++){
11260        sqlite3_create_module(p->db, azArg[ii], 0, 0);
11261      }
11262    }
11263  }else
11264#endif
11265
11266#if SQLITE_USER_AUTHENTICATION
11267  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
11268    if( nArg<2 ){
11269      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
11270      rc = 1;
11271      goto meta_command_exit;
11272    }
11273    open_db(p, 0);
11274    if( strcmp(azArg[1],"login")==0 ){
11275      if( nArg!=4 ){
11276        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
11277        rc = 1;
11278        goto meta_command_exit;
11279      }
11280      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11281                                     strlen30(azArg[3]));
11282      if( rc ){
11283        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
11284        rc = 1;
11285      }
11286    }else if( strcmp(azArg[1],"add")==0 ){
11287      if( nArg!=5 ){
11288        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
11289        rc = 1;
11290        goto meta_command_exit;
11291      }
11292      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11293                            booleanValue(azArg[4]));
11294      if( rc ){
11295        raw_printf(stderr, "User-Add failed: %d\n", rc);
11296        rc = 1;
11297      }
11298    }else if( strcmp(azArg[1],"edit")==0 ){
11299      if( nArg!=5 ){
11300        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
11301        rc = 1;
11302        goto meta_command_exit;
11303      }
11304      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11305                              booleanValue(azArg[4]));
11306      if( rc ){
11307        raw_printf(stderr, "User-Edit failed: %d\n", rc);
11308        rc = 1;
11309      }
11310    }else if( strcmp(azArg[1],"delete")==0 ){
11311      if( nArg!=3 ){
11312        raw_printf(stderr, "Usage: .user delete USER\n");
11313        rc = 1;
11314        goto meta_command_exit;
11315      }
11316      rc = sqlite3_user_delete(p->db, azArg[2]);
11317      if( rc ){
11318        raw_printf(stderr, "User-Delete failed: %d\n", rc);
11319        rc = 1;
11320      }
11321    }else{
11322      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
11323      rc = 1;
11324      goto meta_command_exit;
11325    }
11326  }else
11327#endif /* SQLITE_USER_AUTHENTICATION */
11328
11329  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
11330    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
11331        sqlite3_libversion(), sqlite3_sourceid());
11332#if SQLITE_HAVE_ZLIB
11333    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
11334#endif
11335#define CTIMEOPT_VAL_(opt) #opt
11336#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11337#if defined(__clang__) && defined(__clang_major__)
11338    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
11339                    CTIMEOPT_VAL(__clang_minor__) "."
11340                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
11341#elif defined(_MSC_VER)
11342    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
11343#elif defined(__GNUC__) && defined(__VERSION__)
11344    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
11345#endif
11346  }else
11347
11348  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
11349    const char *zDbName = nArg==2 ? azArg[1] : "main";
11350    sqlite3_vfs *pVfs = 0;
11351    if( p->db ){
11352      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11353      if( pVfs ){
11354        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
11355        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11356        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11357        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11358      }
11359    }
11360  }else
11361
11362  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
11363    sqlite3_vfs *pVfs;
11364    sqlite3_vfs *pCurrent = 0;
11365    if( p->db ){
11366      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11367    }
11368    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11369      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
11370           pVfs==pCurrent ? "  <--- CURRENT" : "");
11371      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11372      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11373      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11374      if( pVfs->pNext ){
11375        raw_printf(p->out, "-----------------------------------\n");
11376      }
11377    }
11378  }else
11379
11380  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
11381    const char *zDbName = nArg==2 ? azArg[1] : "main";
11382    char *zVfsName = 0;
11383    if( p->db ){
11384      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11385      if( zVfsName ){
11386        utf8_printf(p->out, "%s\n", zVfsName);
11387        sqlite3_free(zVfsName);
11388      }
11389    }
11390  }else
11391
11392  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
11393    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11394    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11395  }else
11396
11397  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
11398    int j;
11399    assert( nArg<=ArraySize(azArg) );
11400    p->nWidth = nArg-1;
11401    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11402    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11403    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11404    for(j=1; j<nArg; j++){
11405      p->colWidth[j-1] = (int)integerValue(azArg[j]);
11406    }
11407  }else
11408
11409  {
11410    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
11411      " \"%s\". Enter \".help\" for help\n", azArg[0]);
11412    rc = 1;
11413  }
11414
11415meta_command_exit:
11416  if( p->outCount ){
11417    p->outCount--;
11418    if( p->outCount==0 ) output_reset(p);
11419  }
11420  p->bSafeMode = p->bSafeModePersist;
11421  return rc;
11422}
11423
11424/* Line scan result and intermediate states (supporting scan resumption)
11425*/
11426#ifndef CHAR_BIT
11427# define CHAR_BIT 8
11428#endif
11429typedef enum {
11430  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11431  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11432  QSS_Start = 0
11433} QuickScanState;
11434#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11435#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11436#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11437#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11438#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11439
11440/*
11441** Scan line for classification to guide shell's handling.
11442** The scan is resumable for subsequent lines when prior
11443** return values are passed as the 2nd argument.
11444*/
11445static QuickScanState quickscan(char *zLine, QuickScanState qss){
11446  char cin;
11447  char cWait = (char)qss; /* intentional narrowing loss */
11448  if( cWait==0 ){
11449  PlainScan:
11450    assert( cWait==0 );
11451    while( (cin = *zLine++)!=0 ){
11452      if( IsSpace(cin) )
11453        continue;
11454      switch (cin){
11455      case '-':
11456        if( *zLine!='-' )
11457          break;
11458        while((cin = *++zLine)!=0 )
11459          if( cin=='\n')
11460            goto PlainScan;
11461        return qss;
11462      case ';':
11463        qss |= QSS_EndingSemi;
11464        continue;
11465      case '/':
11466        if( *zLine=='*' ){
11467          ++zLine;
11468          cWait = '*';
11469          qss = QSS_SETV(qss, cWait);
11470          goto TermScan;
11471        }
11472        break;
11473      case '[':
11474        cin = ']';
11475        /* fall thru */
11476      case '`': case '\'': case '"':
11477        cWait = cin;
11478        qss = QSS_HasDark | cWait;
11479        goto TermScan;
11480      default:
11481        break;
11482      }
11483      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11484    }
11485  }else{
11486  TermScan:
11487    while( (cin = *zLine++)!=0 ){
11488      if( cin==cWait ){
11489        switch( cWait ){
11490        case '*':
11491          if( *zLine != '/' )
11492            continue;
11493          ++zLine;
11494          cWait = 0;
11495          qss = QSS_SETV(qss, 0);
11496          goto PlainScan;
11497        case '`': case '\'': case '"':
11498          if(*zLine==cWait){
11499            ++zLine;
11500            continue;
11501          }
11502          /* fall thru */
11503        case ']':
11504          cWait = 0;
11505          qss = QSS_SETV(qss, 0);
11506          goto PlainScan;
11507        default: assert(0);
11508        }
11509      }
11510    }
11511  }
11512  return qss;
11513}
11514
11515/*
11516** Return TRUE if the line typed in is an SQL command terminator other
11517** than a semi-colon.  The SQL Server style "go" command is understood
11518** as is the Oracle "/".
11519*/
11520static int line_is_command_terminator(char *zLine){
11521  while( IsSpace(zLine[0]) ){ zLine++; };
11522  if( zLine[0]=='/' )
11523    zLine += 1; /* Oracle */
11524  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
11525    zLine += 2; /* SQL Server */
11526  else
11527    return 0;
11528  return quickscan(zLine, QSS_Start)==QSS_Start;
11529}
11530
11531/*
11532** We need a default sqlite3_complete() implementation to use in case
11533** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
11534** any arbitrary text is a complete SQL statement.  This is not very
11535** user-friendly, but it does seem to work.
11536*/
11537#ifdef SQLITE_OMIT_COMPLETE
11538#define sqlite3_complete(x) 1
11539#endif
11540
11541/*
11542** Return true if zSql is a complete SQL statement.  Return false if it
11543** ends in the middle of a string literal or C-style comment.
11544*/
11545static int line_is_complete(char *zSql, int nSql){
11546  int rc;
11547  if( zSql==0 ) return 1;
11548  zSql[nSql] = ';';
11549  zSql[nSql+1] = 0;
11550  rc = sqlite3_complete(zSql);
11551  zSql[nSql] = 0;
11552  return rc;
11553}
11554
11555/*
11556** Run a single line of SQL.  Return the number of errors.
11557*/
11558static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
11559  int rc;
11560  char *zErrMsg = 0;
11561
11562  open_db(p, 0);
11563  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
11564  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11565  BEGIN_TIMER;
11566  rc = shell_exec(p, zSql, &zErrMsg);
11567  END_TIMER;
11568  if( rc || zErrMsg ){
11569    char zPrefix[100];
11570    const char *zErrorTail;
11571    const char *zErrorType;
11572    if( zErrMsg==0 ){
11573      zErrorType = "Error";
11574      zErrorTail = sqlite3_errmsg(p->db);
11575    }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){
11576      zErrorType = "Parse error";
11577      zErrorTail = &zErrMsg[12];
11578    }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){
11579      zErrorType = "Runtime error";
11580      zErrorTail = &zErrMsg[10];
11581    }else{
11582      zErrorType = "Error";
11583      zErrorTail = zErrMsg;
11584    }
11585    if( in!=0 || !stdin_is_interactive ){
11586      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11587                       "%s near line %d:", zErrorType, startline);
11588    }else{
11589      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11590    }
11591    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11592    sqlite3_free(zErrMsg);
11593    zErrMsg = 0;
11594    return 1;
11595  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11596    char zLineBuf[2000];
11597    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11598            "changes: %lld   total_changes: %lld",
11599            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11600    raw_printf(p->out, "%s\n", zLineBuf);
11601  }
11602  return 0;
11603}
11604
11605static void echo_group_input(ShellState *p, const char *zDo){
11606  if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11607}
11608
11609#ifdef SQLITE_SHELL_FIDDLE
11610/*
11611** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
11612** because we need the global shellState and cannot access it from that function
11613** without moving lots of code around (creating a larger/messier diff).
11614*/
11615static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11616  /* Parse the next line from shellState.wasm.zInput. */
11617  const char *zBegin = shellState.wasm.zPos;
11618  const char *z = zBegin;
11619  char *zLine = 0;
11620  int nZ = 0;
11621
11622  UNUSED_PARAMETER(in);
11623  UNUSED_PARAMETER(isContinuation);
11624  if(!z || !*z){
11625    return 0;
11626  }
11627  while(*z && isspace(*z)) ++z;
11628  zBegin = z;
11629  for(; *z && '\n'!=*z; ++nZ, ++z){}
11630  if(nZ>0 && '\r'==zBegin[nZ-1]){
11631    --nZ;
11632  }
11633  shellState.wasm.zPos = z;
11634  zLine = realloc(zPrior, nZ+1);
11635  shell_check_oom(zLine);
11636  memcpy(zLine, zBegin, (size_t)nZ);
11637  zLine[nZ] = 0;
11638  return zLine;
11639}
11640#endif /* SQLITE_SHELL_FIDDLE */
11641
11642/*
11643** Read input from *in and process it.  If *in==0 then input
11644** is interactive - the user is typing it it.  Otherwise, input
11645** is coming from a file or device.  A prompt is issued and history
11646** is saved only if input is interactive.  An interrupt signal will
11647** cause this routine to exit immediately, unless input is interactive.
11648**
11649** Return the number of errors.
11650*/
11651static int process_input(ShellState *p){
11652  char *zLine = 0;          /* A single input line */
11653  char *zSql = 0;           /* Accumulated SQL text */
11654  int nLine;                /* Length of current line */
11655  int nSql = 0;             /* Bytes of zSql[] used */
11656  int nAlloc = 0;           /* Allocated zSql[] space */
11657  int rc;                   /* Error code */
11658  int errCnt = 0;           /* Number of errors seen */
11659  int startline = 0;        /* Line number for start of current input */
11660  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11661
11662  if( p->inputNesting==MAX_INPUT_NESTING ){
11663    /* This will be more informative in a later version. */
11664    utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11665                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11666    return 1;
11667  }
11668  ++p->inputNesting;
11669  p->lineno = 0;
11670  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11671    fflush(p->out);
11672    zLine = one_input_line(p->in, zLine, nSql>0);
11673    if( zLine==0 ){
11674      /* End of input */
11675      if( p->in==0 && stdin_is_interactive ) printf("\n");
11676      break;
11677    }
11678    if( seenInterrupt ){
11679      if( p->in!=0 ) break;
11680      seenInterrupt = 0;
11681    }
11682    p->lineno++;
11683    if( QSS_INPLAIN(qss)
11684        && line_is_command_terminator(zLine)
11685        && line_is_complete(zSql, nSql) ){
11686      memcpy(zLine,";",2);
11687    }
11688    qss = quickscan(zLine, qss);
11689    if( QSS_PLAINWHITE(qss) && nSql==0 ){
11690      /* Just swallow single-line whitespace */
11691      echo_group_input(p, zLine);
11692      qss = QSS_Start;
11693      continue;
11694    }
11695    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11696      echo_group_input(p, zLine);
11697      if( zLine[0]=='.' ){
11698        rc = do_meta_command(zLine, p);
11699        if( rc==2 ){ /* exit requested */
11700          break;
11701        }else if( rc ){
11702          errCnt++;
11703        }
11704      }
11705      qss = QSS_Start;
11706      continue;
11707    }
11708    /* No single-line dispositions remain; accumulate line(s). */
11709    nLine = strlen30(zLine);
11710    if( nSql+nLine+2>=nAlloc ){
11711      /* Grow buffer by half-again increments when big. */
11712      nAlloc = nSql+(nSql>>1)+nLine+100;
11713      zSql = realloc(zSql, nAlloc);
11714      shell_check_oom(zSql);
11715    }
11716    if( nSql==0 ){
11717      int i;
11718      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11719      assert( nAlloc>0 && zSql!=0 );
11720      memcpy(zSql, zLine+i, nLine+1-i);
11721      startline = p->lineno;
11722      nSql = nLine-i;
11723    }else{
11724      zSql[nSql++] = '\n';
11725      memcpy(zSql+nSql, zLine, nLine+1);
11726      nSql += nLine;
11727    }
11728    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11729      echo_group_input(p, zSql);
11730      errCnt += runOneSqlLine(p, zSql, p->in, startline);
11731      nSql = 0;
11732      if( p->outCount ){
11733        output_reset(p);
11734        p->outCount = 0;
11735      }else{
11736        clearTempFile(p);
11737      }
11738      p->bSafeMode = p->bSafeModePersist;
11739      qss = QSS_Start;
11740    }else if( nSql && QSS_PLAINWHITE(qss) ){
11741      echo_group_input(p, zSql);
11742      nSql = 0;
11743      qss = QSS_Start;
11744    }
11745  }
11746  if( nSql ){
11747    /* This may be incomplete. Let the SQL parser deal with that. */
11748    echo_group_input(p, zSql);
11749    errCnt += runOneSqlLine(p, zSql, p->in, startline);
11750  }
11751  free(zSql);
11752  free(zLine);
11753  --p->inputNesting;
11754  return errCnt>0;
11755}
11756
11757/*
11758** Return a pathname which is the user's home directory.  A
11759** 0 return indicates an error of some kind.
11760*/
11761static char *find_home_dir(int clearFlag){
11762  static char *home_dir = NULL;
11763  if( clearFlag ){
11764    free(home_dir);
11765    home_dir = 0;
11766    return 0;
11767  }
11768  if( home_dir ) return home_dir;
11769
11770#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11771     && !defined(__RTP__) && !defined(_WRS_KERNEL)
11772  {
11773    struct passwd *pwent;
11774    uid_t uid = getuid();
11775    if( (pwent=getpwuid(uid)) != NULL) {
11776      home_dir = pwent->pw_dir;
11777    }
11778  }
11779#endif
11780
11781#if defined(_WIN32_WCE)
11782  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11783   */
11784  home_dir = "/";
11785#else
11786
11787#if defined(_WIN32) || defined(WIN32)
11788  if (!home_dir) {
11789    home_dir = getenv("USERPROFILE");
11790  }
11791#endif
11792
11793  if (!home_dir) {
11794    home_dir = getenv("HOME");
11795  }
11796
11797#if defined(_WIN32) || defined(WIN32)
11798  if (!home_dir) {
11799    char *zDrive, *zPath;
11800    int n;
11801    zDrive = getenv("HOMEDRIVE");
11802    zPath = getenv("HOMEPATH");
11803    if( zDrive && zPath ){
11804      n = strlen30(zDrive) + strlen30(zPath) + 1;
11805      home_dir = malloc( n );
11806      if( home_dir==0 ) return 0;
11807      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11808      return home_dir;
11809    }
11810    home_dir = "c:\\";
11811  }
11812#endif
11813
11814#endif /* !_WIN32_WCE */
11815
11816  if( home_dir ){
11817    int n = strlen30(home_dir) + 1;
11818    char *z = malloc( n );
11819    if( z ) memcpy(z, home_dir, n);
11820    home_dir = z;
11821  }
11822
11823  return home_dir;
11824}
11825
11826/*
11827** Read input from the file given by sqliterc_override.  Or if that
11828** parameter is NULL, take input from ~/.sqliterc
11829**
11830** Returns the number of errors.
11831*/
11832static void process_sqliterc(
11833  ShellState *p,                  /* Configuration data */
11834  const char *sqliterc_override   /* Name of config file. NULL to use default */
11835){
11836  char *home_dir = NULL;
11837  const char *sqliterc = sqliterc_override;
11838  char *zBuf = 0;
11839  FILE *inSaved = p->in;
11840  int savedLineno = p->lineno;
11841
11842  if (sqliterc == NULL) {
11843    home_dir = find_home_dir(0);
11844    if( home_dir==0 ){
11845      raw_printf(stderr, "-- warning: cannot find home directory;"
11846                      " cannot read ~/.sqliterc\n");
11847      return;
11848    }
11849    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11850    shell_check_oom(zBuf);
11851    sqliterc = zBuf;
11852  }
11853  p->in = fopen(sqliterc,"rb");
11854  if( p->in ){
11855    if( stdin_is_interactive ){
11856      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11857    }
11858    if( process_input(p) && bail_on_error ) exit(1);
11859    fclose(p->in);
11860  }else if( sqliterc_override!=0 ){
11861    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11862    if( bail_on_error ) exit(1);
11863  }
11864  p->in = inSaved;
11865  p->lineno = savedLineno;
11866  sqlite3_free(zBuf);
11867}
11868
11869/*
11870** Show available command line options
11871*/
11872static const char zOptions[] =
11873#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11874  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11875#endif
11876  "   -append              append the database to the end of the file\n"
11877  "   -ascii               set output mode to 'ascii'\n"
11878  "   -bail                stop after hitting an error\n"
11879  "   -batch               force batch I/O\n"
11880  "   -box                 set output mode to 'box'\n"
11881  "   -column              set output mode to 'column'\n"
11882  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11883  "   -csv                 set output mode to 'csv'\n"
11884#if !defined(SQLITE_OMIT_DESERIALIZE)
11885  "   -deserialize         open the database using sqlite3_deserialize()\n"
11886#endif
11887  "   -echo                print inputs before execution\n"
11888  "   -init FILENAME       read/process named file\n"
11889  "   -[no]header          turn headers on or off\n"
11890#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11891  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11892#endif
11893  "   -help                show this message\n"
11894  "   -html                set output mode to HTML\n"
11895  "   -interactive         force interactive I/O\n"
11896  "   -json                set output mode to 'json'\n"
11897  "   -line                set output mode to 'line'\n"
11898  "   -list                set output mode to 'list'\n"
11899  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11900  "   -markdown            set output mode to 'markdown'\n"
11901#if !defined(SQLITE_OMIT_DESERIALIZE)
11902  "   -maxsize N           maximum size for a --deserialize database\n"
11903#endif
11904  "   -memtrace            trace all memory allocations and deallocations\n"
11905  "   -mmap N              default mmap size set to N\n"
11906#ifdef SQLITE_ENABLE_MULTIPLEX
11907  "   -multiplex           enable the multiplexor VFS\n"
11908#endif
11909  "   -newline SEP         set output row separator. Default: '\\n'\n"
11910  "   -nofollow            refuse to open symbolic links to database files\n"
11911  "   -nonce STRING        set the safe-mode escape nonce\n"
11912  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11913  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11914  "   -quote               set output mode to 'quote'\n"
11915  "   -readonly            open the database read-only\n"
11916  "   -safe                enable safe-mode\n"
11917  "   -separator SEP       set output column separator. Default: '|'\n"
11918#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11919  "   -sorterref SIZE      sorter references threshold size\n"
11920#endif
11921  "   -stats               print memory stats before each finalize\n"
11922  "   -table               set output mode to 'table'\n"
11923  "   -tabs                set output mode to 'tabs'\n"
11924  "   -version             show SQLite version\n"
11925  "   -vfs NAME            use NAME as the default VFS\n"
11926#ifdef SQLITE_ENABLE_VFSTRACE
11927  "   -vfstrace            enable tracing of all VFS calls\n"
11928#endif
11929#ifdef SQLITE_HAVE_ZLIB
11930  "   -zip                 open the file as a ZIP Archive\n"
11931#endif
11932;
11933static void usage(int showDetail){
11934  utf8_printf(stderr,
11935      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11936      "FILENAME is the name of an SQLite database. A new database is created\n"
11937      "if the file does not previously exist.\n", Argv0);
11938  if( showDetail ){
11939    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11940  }else{
11941    raw_printf(stderr, "Use the -help option for additional information\n");
11942  }
11943  exit(1);
11944}
11945
11946/*
11947** Internal check:  Verify that the SQLite is uninitialized.  Print a
11948** error message if it is initialized.
11949*/
11950static void verify_uninitialized(void){
11951  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11952    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11953                        " initialization.\n");
11954  }
11955}
11956
11957/*
11958** Initialize the state information in data
11959*/
11960static void main_init(ShellState *data) {
11961  memset(data, 0, sizeof(*data));
11962  data->normalMode = data->cMode = data->mode = MODE_List;
11963  data->autoExplain = 1;
11964  data->pAuxDb = &data->aAuxDb[0];
11965  memcpy(data->colSeparator,SEP_Column, 2);
11966  memcpy(data->rowSeparator,SEP_Row, 2);
11967  data->showHeader = 0;
11968  data->shellFlgs = SHFLG_Lookaside;
11969  verify_uninitialized();
11970  sqlite3_config(SQLITE_CONFIG_URI, 1);
11971  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11972  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11973  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11974  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11975}
11976
11977/*
11978** Output text to the console in a font that attracts extra attention.
11979*/
11980#ifdef _WIN32
11981static void printBold(const char *zText){
11982#if !SQLITE_OS_WINRT
11983  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11984  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11985  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11986  SetConsoleTextAttribute(out,
11987         FOREGROUND_RED|FOREGROUND_INTENSITY
11988  );
11989#endif
11990  printf("%s", zText);
11991#if !SQLITE_OS_WINRT
11992  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11993#endif
11994}
11995#else
11996static void printBold(const char *zText){
11997  printf("\033[1m%s\033[0m", zText);
11998}
11999#endif
12000
12001/*
12002** Get the argument to an --option.  Throw an error and die if no argument
12003** is available.
12004*/
12005static char *cmdline_option_value(int argc, char **argv, int i){
12006  if( i==argc ){
12007    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
12008            argv[0], argv[argc-1]);
12009    exit(1);
12010  }
12011  return argv[i];
12012}
12013
12014#ifndef SQLITE_SHELL_IS_UTF8
12015#  if (defined(_WIN32) || defined(WIN32)) \
12016   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12017#    define SQLITE_SHELL_IS_UTF8          (0)
12018#  else
12019#    define SQLITE_SHELL_IS_UTF8          (1)
12020#  endif
12021#endif
12022
12023#ifdef SQLITE_SHELL_FIDDLE
12024#  define main fiddle_main
12025#endif
12026
12027#if SQLITE_SHELL_IS_UTF8
12028int SQLITE_CDECL main(int argc, char **argv){
12029#else
12030int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
12031  char **argv;
12032#endif
12033#ifdef SQLITE_DEBUG
12034  sqlite3_int64 mem_main_enter = sqlite3_memory_used();
12035#endif
12036  char *zErrMsg = 0;
12037#ifdef SQLITE_SHELL_FIDDLE
12038#  define data shellState
12039#else
12040  ShellState data;
12041#endif
12042  const char *zInitFile = 0;
12043  int i;
12044  int rc = 0;
12045  int warnInmemoryDb = 0;
12046  int readStdin = 1;
12047  int nCmd = 0;
12048  char **azCmd = 0;
12049  const char *zVfs = 0;           /* Value of -vfs command-line option */
12050#if !SQLITE_SHELL_IS_UTF8
12051  char **argvToFree = 0;
12052  int argcToFree = 0;
12053#endif
12054
12055  setBinaryMode(stdin, 0);
12056  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12057#ifdef SQLITE_SHELL_FIDDLE
12058  stdin_is_interactive = 0;
12059  stdout_is_console = 1;
12060#else
12061  stdin_is_interactive = isatty(0);
12062  stdout_is_console = isatty(1);
12063#endif
12064
12065#if !defined(_WIN32_WCE)
12066  if( getenv("SQLITE_DEBUG_BREAK") ){
12067    if( isatty(0) && isatty(2) ){
12068      fprintf(stderr,
12069          "attach debugger to process %d and press any key to continue.\n",
12070          GETPID());
12071      fgetc(stdin);
12072    }else{
12073#if defined(_WIN32) || defined(WIN32)
12074#if SQLITE_OS_WINRT
12075      __debugbreak();
12076#else
12077      DebugBreak();
12078#endif
12079#elif defined(SIGTRAP)
12080      raise(SIGTRAP);
12081#endif
12082    }
12083  }
12084#endif
12085
12086#if USE_SYSTEM_SQLITE+0!=1
12087  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12088    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
12089            sqlite3_sourceid(), SQLITE_SOURCE_ID);
12090    exit(1);
12091  }
12092#endif
12093  main_init(&data);
12094
12095  /* On Windows, we must translate command-line arguments into UTF-8.
12096  ** The SQLite memory allocator subsystem has to be enabled in order to
12097  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
12098  ** subsequent sqlite3_config() calls will work.  So copy all results into
12099  ** memory that does not come from the SQLite memory allocator.
12100  */
12101#if !SQLITE_SHELL_IS_UTF8
12102  sqlite3_initialize();
12103  argvToFree = malloc(sizeof(argv[0])*argc*2);
12104  shell_check_oom(argvToFree);
12105  argcToFree = argc;
12106  argv = argvToFree + argc;
12107  for(i=0; i<argc; i++){
12108    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12109    int n;
12110    shell_check_oom(z);
12111    n = (int)strlen(z);
12112    argv[i] = malloc( n+1 );
12113    shell_check_oom(argv[i]);
12114    memcpy(argv[i], z, n+1);
12115    argvToFree[i] = argv[i];
12116    sqlite3_free(z);
12117  }
12118  sqlite3_shutdown();
12119#endif
12120
12121  assert( argc>=1 && argv && argv[0] );
12122  Argv0 = argv[0];
12123
12124  /* Make sure we have a valid signal handler early, before anything
12125  ** else is done.
12126  */
12127#ifdef SIGINT
12128  signal(SIGINT, interrupt_handler);
12129#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12130  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
12131#endif
12132
12133#ifdef SQLITE_SHELL_DBNAME_PROC
12134  {
12135    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12136    ** of a C-function that will provide the name of the database file.  Use
12137    ** this compile-time option to embed this shell program in larger
12138    ** applications. */
12139    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12140    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12141    warnInmemoryDb = 0;
12142  }
12143#endif
12144
12145  /* Do an initial pass through the command-line argument to locate
12146  ** the name of the database file, the name of the initialization file,
12147  ** the size of the alternative malloc heap,
12148  ** and the first command to execute.
12149  */
12150  verify_uninitialized();
12151  for(i=1; i<argc; i++){
12152    char *z;
12153    z = argv[i];
12154    if( z[0]!='-' ){
12155      if( data.aAuxDb->zDbFilename==0 ){
12156        data.aAuxDb->zDbFilename = z;
12157      }else{
12158        /* Excesss arguments are interpreted as SQL (or dot-commands) and
12159        ** mean that nothing is read from stdin */
12160        readStdin = 0;
12161        nCmd++;
12162        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12163        shell_check_oom(azCmd);
12164        azCmd[nCmd-1] = z;
12165      }
12166    }
12167    if( z[1]=='-' ) z++;
12168    if( strcmp(z,"-separator")==0
12169     || strcmp(z,"-nullvalue")==0
12170     || strcmp(z,"-newline")==0
12171     || strcmp(z,"-cmd")==0
12172    ){
12173      (void)cmdline_option_value(argc, argv, ++i);
12174    }else if( strcmp(z,"-init")==0 ){
12175      zInitFile = cmdline_option_value(argc, argv, ++i);
12176    }else if( strcmp(z,"-batch")==0 ){
12177      /* Need to check for batch mode here to so we can avoid printing
12178      ** informational messages (like from process_sqliterc) before
12179      ** we do the actual processing of arguments later in a second pass.
12180      */
12181      stdin_is_interactive = 0;
12182    }else if( strcmp(z,"-heap")==0 ){
12183#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12184      const char *zSize;
12185      sqlite3_int64 szHeap;
12186
12187      zSize = cmdline_option_value(argc, argv, ++i);
12188      szHeap = integerValue(zSize);
12189      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12190      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12191#else
12192      (void)cmdline_option_value(argc, argv, ++i);
12193#endif
12194    }else if( strcmp(z,"-pagecache")==0 ){
12195      sqlite3_int64 n, sz;
12196      sz = integerValue(cmdline_option_value(argc,argv,++i));
12197      if( sz>70000 ) sz = 70000;
12198      if( sz<0 ) sz = 0;
12199      n = integerValue(cmdline_option_value(argc,argv,++i));
12200      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12201        n = 0xffffffffffffLL/sz;
12202      }
12203      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12204                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12205      data.shellFlgs |= SHFLG_Pagecache;
12206    }else if( strcmp(z,"-lookaside")==0 ){
12207      int n, sz;
12208      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12209      if( sz<0 ) sz = 0;
12210      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12211      if( n<0 ) n = 0;
12212      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12213      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12214    }else if( strcmp(z,"-threadsafe")==0 ){
12215      int n;
12216      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12217      switch( n ){
12218         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
12219         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
12220         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
12221      }
12222#ifdef SQLITE_ENABLE_VFSTRACE
12223    }else if( strcmp(z,"-vfstrace")==0 ){
12224      extern int vfstrace_register(
12225         const char *zTraceName,
12226         const char *zOldVfsName,
12227         int (*xOut)(const char*,void*),
12228         void *pOutArg,
12229         int makeDefault
12230      );
12231      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
12232#endif
12233#ifdef SQLITE_ENABLE_MULTIPLEX
12234    }else if( strcmp(z,"-multiplex")==0 ){
12235      extern int sqlite3_multiple_initialize(const char*,int);
12236      sqlite3_multiplex_initialize(0, 1);
12237#endif
12238    }else if( strcmp(z,"-mmap")==0 ){
12239      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12240      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12241#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12242    }else if( strcmp(z,"-sorterref")==0 ){
12243      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12244      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12245#endif
12246    }else if( strcmp(z,"-vfs")==0 ){
12247      zVfs = cmdline_option_value(argc, argv, ++i);
12248#ifdef SQLITE_HAVE_ZLIB
12249    }else if( strcmp(z,"-zip")==0 ){
12250      data.openMode = SHELL_OPEN_ZIPFILE;
12251#endif
12252    }else if( strcmp(z,"-append")==0 ){
12253      data.openMode = SHELL_OPEN_APPENDVFS;
12254#ifndef SQLITE_OMIT_DESERIALIZE
12255    }else if( strcmp(z,"-deserialize")==0 ){
12256      data.openMode = SHELL_OPEN_DESERIALIZE;
12257    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
12258      data.szMax = integerValue(argv[++i]);
12259#endif
12260    }else if( strcmp(z,"-readonly")==0 ){
12261      data.openMode = SHELL_OPEN_READONLY;
12262    }else if( strcmp(z,"-nofollow")==0 ){
12263      data.openFlags = SQLITE_OPEN_NOFOLLOW;
12264#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12265    }else if( strncmp(z, "-A",2)==0 ){
12266      /* All remaining command-line arguments are passed to the ".archive"
12267      ** command, so ignore them */
12268      break;
12269#endif
12270    }else if( strcmp(z, "-memtrace")==0 ){
12271      sqlite3MemTraceActivate(stderr);
12272    }else if( strcmp(z,"-bail")==0 ){
12273      bail_on_error = 1;
12274    }else if( strcmp(z,"-nonce")==0 ){
12275      free(data.zNonce);
12276      data.zNonce = strdup(argv[++i]);
12277    }else if( strcmp(z,"-safe")==0 ){
12278      /* no-op - catch this on the second pass */
12279    }
12280  }
12281  verify_uninitialized();
12282
12283
12284#ifdef SQLITE_SHELL_INIT_PROC
12285  {
12286    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12287    ** of a C-function that will perform initialization actions on SQLite that
12288    ** occur just before or after sqlite3_initialize(). Use this compile-time
12289    ** option to embed this shell program in larger applications. */
12290    extern void SQLITE_SHELL_INIT_PROC(void);
12291    SQLITE_SHELL_INIT_PROC();
12292  }
12293#else
12294  /* All the sqlite3_config() calls have now been made. So it is safe
12295  ** to call sqlite3_initialize() and process any command line -vfs option. */
12296  sqlite3_initialize();
12297#endif
12298
12299  if( zVfs ){
12300    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12301    if( pVfs ){
12302      sqlite3_vfs_register(pVfs, 1);
12303    }else{
12304      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
12305      exit(1);
12306    }
12307  }
12308
12309  if( data.pAuxDb->zDbFilename==0 ){
12310#ifndef SQLITE_OMIT_MEMORYDB
12311    data.pAuxDb->zDbFilename = ":memory:";
12312    warnInmemoryDb = argc==1;
12313#else
12314    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
12315    return 1;
12316#endif
12317  }
12318  data.out = stdout;
12319#ifndef SQLITE_SHELL_FIDDLE
12320  sqlite3_appendvfs_init(0,0,0);
12321#endif
12322
12323  /* Go ahead and open the database file if it already exists.  If the
12324  ** file does not exist, delay opening it.  This prevents empty database
12325  ** files from being created if a user mistypes the database name argument
12326  ** to the sqlite command-line tool.
12327  */
12328  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
12329    open_db(&data, 0);
12330  }
12331
12332  /* Process the initialization file if there is one.  If no -init option
12333  ** is given on the command line, look for a file named ~/.sqliterc and
12334  ** try to process it.
12335  */
12336  process_sqliterc(&data,zInitFile);
12337
12338  /* Make a second pass through the command-line argument and set
12339  ** options.  This second pass is delayed until after the initialization
12340  ** file is processed so that the command-line arguments will override
12341  ** settings in the initialization file.
12342  */
12343  for(i=1; i<argc; i++){
12344    char *z = argv[i];
12345    if( z[0]!='-' ) continue;
12346    if( z[1]=='-' ){ z++; }
12347    if( strcmp(z,"-init")==0 ){
12348      i++;
12349    }else if( strcmp(z,"-html")==0 ){
12350      data.mode = MODE_Html;
12351    }else if( strcmp(z,"-list")==0 ){
12352      data.mode = MODE_List;
12353    }else if( strcmp(z,"-quote")==0 ){
12354      data.mode = MODE_Quote;
12355      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
12356      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12357    }else if( strcmp(z,"-line")==0 ){
12358      data.mode = MODE_Line;
12359    }else if( strcmp(z,"-column")==0 ){
12360      data.mode = MODE_Column;
12361    }else if( strcmp(z,"-json")==0 ){
12362      data.mode = MODE_Json;
12363    }else if( strcmp(z,"-markdown")==0 ){
12364      data.mode = MODE_Markdown;
12365    }else if( strcmp(z,"-table")==0 ){
12366      data.mode = MODE_Table;
12367    }else if( strcmp(z,"-box")==0 ){
12368      data.mode = MODE_Box;
12369    }else if( strcmp(z,"-csv")==0 ){
12370      data.mode = MODE_Csv;
12371      memcpy(data.colSeparator,",",2);
12372#ifdef SQLITE_HAVE_ZLIB
12373    }else if( strcmp(z,"-zip")==0 ){
12374      data.openMode = SHELL_OPEN_ZIPFILE;
12375#endif
12376    }else if( strcmp(z,"-append")==0 ){
12377      data.openMode = SHELL_OPEN_APPENDVFS;
12378#ifndef SQLITE_OMIT_DESERIALIZE
12379    }else if( strcmp(z,"-deserialize")==0 ){
12380      data.openMode = SHELL_OPEN_DESERIALIZE;
12381    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
12382      data.szMax = integerValue(argv[++i]);
12383#endif
12384    }else if( strcmp(z,"-readonly")==0 ){
12385      data.openMode = SHELL_OPEN_READONLY;
12386    }else if( strcmp(z,"-nofollow")==0 ){
12387      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
12388    }else if( strcmp(z,"-ascii")==0 ){
12389      data.mode = MODE_Ascii;
12390      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
12391      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
12392    }else if( strcmp(z,"-tabs")==0 ){
12393      data.mode = MODE_List;
12394      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
12395      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12396    }else if( strcmp(z,"-separator")==0 ){
12397      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
12398                       "%s",cmdline_option_value(argc,argv,++i));
12399    }else if( strcmp(z,"-newline")==0 ){
12400      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
12401                       "%s",cmdline_option_value(argc,argv,++i));
12402    }else if( strcmp(z,"-nullvalue")==0 ){
12403      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
12404                       "%s",cmdline_option_value(argc,argv,++i));
12405    }else if( strcmp(z,"-header")==0 ){
12406      data.showHeader = 1;
12407      ShellSetFlag(&data, SHFLG_HeaderSet);
12408     }else if( strcmp(z,"-noheader")==0 ){
12409      data.showHeader = 0;
12410      ShellSetFlag(&data, SHFLG_HeaderSet);
12411    }else if( strcmp(z,"-echo")==0 ){
12412      ShellSetFlag(&data, SHFLG_Echo);
12413    }else if( strcmp(z,"-eqp")==0 ){
12414      data.autoEQP = AUTOEQP_on;
12415    }else if( strcmp(z,"-eqpfull")==0 ){
12416      data.autoEQP = AUTOEQP_full;
12417    }else if( strcmp(z,"-stats")==0 ){
12418      data.statsOn = 1;
12419    }else if( strcmp(z,"-scanstats")==0 ){
12420      data.scanstatsOn = 1;
12421    }else if( strcmp(z,"-backslash")==0 ){
12422      /* Undocumented command-line option: -backslash
12423      ** Causes C-style backslash escapes to be evaluated in SQL statements
12424      ** prior to sending the SQL into SQLite.  Useful for injecting
12425      ** crazy bytes in the middle of SQL statements for testing and debugging.
12426      */
12427      ShellSetFlag(&data, SHFLG_Backslash);
12428    }else if( strcmp(z,"-bail")==0 ){
12429      /* No-op.  The bail_on_error flag should already be set. */
12430    }else if( strcmp(z,"-version")==0 ){
12431      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
12432      return 0;
12433    }else if( strcmp(z,"-interactive")==0 ){
12434      stdin_is_interactive = 1;
12435    }else if( strcmp(z,"-batch")==0 ){
12436      stdin_is_interactive = 0;
12437    }else if( strcmp(z,"-heap")==0 ){
12438      i++;
12439    }else if( strcmp(z,"-pagecache")==0 ){
12440      i+=2;
12441    }else if( strcmp(z,"-lookaside")==0 ){
12442      i+=2;
12443    }else if( strcmp(z,"-threadsafe")==0 ){
12444      i+=2;
12445    }else if( strcmp(z,"-nonce")==0 ){
12446      i += 2;
12447    }else if( strcmp(z,"-mmap")==0 ){
12448      i++;
12449    }else if( strcmp(z,"-memtrace")==0 ){
12450      i++;
12451#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12452    }else if( strcmp(z,"-sorterref")==0 ){
12453      i++;
12454#endif
12455    }else if( strcmp(z,"-vfs")==0 ){
12456      i++;
12457#ifdef SQLITE_ENABLE_VFSTRACE
12458    }else if( strcmp(z,"-vfstrace")==0 ){
12459      i++;
12460#endif
12461#ifdef SQLITE_ENABLE_MULTIPLEX
12462    }else if( strcmp(z,"-multiplex")==0 ){
12463      i++;
12464#endif
12465    }else if( strcmp(z,"-help")==0 ){
12466      usage(1);
12467    }else if( strcmp(z,"-cmd")==0 ){
12468      /* Run commands that follow -cmd first and separately from commands
12469      ** that simply appear on the command-line.  This seems goofy.  It would
12470      ** be better if all commands ran in the order that they appear.  But
12471      ** we retain the goofy behavior for historical compatibility. */
12472      if( i==argc-1 ) break;
12473      z = cmdline_option_value(argc,argv,++i);
12474      if( z[0]=='.' ){
12475        rc = do_meta_command(z, &data);
12476        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
12477      }else{
12478        open_db(&data, 0);
12479        rc = shell_exec(&data, z, &zErrMsg);
12480        if( zErrMsg!=0 ){
12481          utf8_printf(stderr,"Error: %s\n", zErrMsg);
12482          if( bail_on_error ) return rc!=0 ? rc : 1;
12483        }else if( rc!=0 ){
12484          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
12485          if( bail_on_error ) return rc;
12486        }
12487      }
12488#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12489    }else if( strncmp(z, "-A", 2)==0 ){
12490      if( nCmd>0 ){
12491        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
12492                            " with \"%s\"\n", z);
12493        return 1;
12494      }
12495      open_db(&data, OPEN_DB_ZIPFILE);
12496      if( z[2] ){
12497        argv[i] = &z[2];
12498        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
12499      }else{
12500        arDotCommand(&data, 1, argv+i, argc-i);
12501      }
12502      readStdin = 0;
12503      break;
12504#endif
12505    }else if( strcmp(z,"-safe")==0 ){
12506      data.bSafeMode = data.bSafeModePersist = 1;
12507    }else{
12508      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
12509      raw_printf(stderr,"Use -help for a list of options.\n");
12510      return 1;
12511    }
12512    data.cMode = data.mode;
12513  }
12514
12515  if( !readStdin ){
12516    /* Run all arguments that do not begin with '-' as if they were separate
12517    ** command-line inputs, except for the argToSkip argument which contains
12518    ** the database filename.
12519    */
12520    for(i=0; i<nCmd; i++){
12521      if( azCmd[i][0]=='.' ){
12522        rc = do_meta_command(azCmd[i], &data);
12523        if( rc ){
12524          free(azCmd);
12525          return rc==2 ? 0 : rc;
12526        }
12527      }else{
12528        open_db(&data, 0);
12529        rc = shell_exec(&data, azCmd[i], &zErrMsg);
12530        if( zErrMsg || rc ){
12531          if( zErrMsg!=0 ){
12532            utf8_printf(stderr,"Error: %s\n", zErrMsg);
12533          }else{
12534            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
12535          }
12536          sqlite3_free(zErrMsg);
12537          free(azCmd);
12538          return rc!=0 ? rc : 1;
12539        }
12540      }
12541    }
12542  }else{
12543    /* Run commands received from standard input
12544    */
12545    if( stdin_is_interactive ){
12546      char *zHome;
12547      char *zHistory;
12548      int nHistory;
12549      printf(
12550        "SQLite version %s %.19s\n" /*extra-version-info*/
12551        "Enter \".help\" for usage hints.\n",
12552        sqlite3_libversion(), sqlite3_sourceid()
12553      );
12554      if( warnInmemoryDb ){
12555        printf("Connected to a ");
12556        printBold("transient in-memory database");
12557        printf(".\nUse \".open FILENAME\" to reopen on a "
12558               "persistent database.\n");
12559      }
12560      zHistory = getenv("SQLITE_HISTORY");
12561      if( zHistory ){
12562        zHistory = strdup(zHistory);
12563      }else if( (zHome = find_home_dir(0))!=0 ){
12564        nHistory = strlen30(zHome) + 20;
12565        if( (zHistory = malloc(nHistory))!=0 ){
12566          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
12567        }
12568      }
12569      if( zHistory ){ shell_read_history(zHistory); }
12570#if HAVE_READLINE || HAVE_EDITLINE
12571      rl_attempted_completion_function = readline_completion;
12572#elif HAVE_LINENOISE
12573      linenoiseSetCompletionCallback(linenoise_completion);
12574#endif
12575      data.in = 0;
12576      rc = process_input(&data);
12577      if( zHistory ){
12578        shell_stifle_history(2000);
12579        shell_write_history(zHistory);
12580        free(zHistory);
12581      }
12582    }else{
12583      data.in = stdin;
12584      rc = process_input(&data);
12585    }
12586  }
12587#ifndef SQLITE_SHELL_FIDDLE
12588  /* In WASM mode we have to leave the db state in place so that
12589  ** client code can "push" SQL into it after this call returns. */
12590  free(azCmd);
12591  set_table_name(&data, 0);
12592  if( data.db ){
12593    session_close_all(&data, -1);
12594    close_db(data.db);
12595  }
12596  for(i=0; i<ArraySize(data.aAuxDb); i++){
12597    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12598    if( data.aAuxDb[i].db ){
12599      session_close_all(&data, i);
12600      close_db(data.aAuxDb[i].db);
12601    }
12602  }
12603  find_home_dir(1);
12604  output_reset(&data);
12605  data.doXdgOpen = 0;
12606  clearTempFile(&data);
12607#if !SQLITE_SHELL_IS_UTF8
12608  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12609  free(argvToFree);
12610#endif
12611  free(data.colWidth);
12612  free(data.zNonce);
12613  /* Clear the global data structure so that valgrind will detect memory
12614  ** leaks */
12615  memset(&data, 0, sizeof(data));
12616#ifdef SQLITE_DEBUG
12617  if( sqlite3_memory_used()>mem_main_enter ){
12618    utf8_printf(stderr, "Memory leaked: %u bytes\n",
12619                (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12620  }
12621#endif
12622#endif /* !SQLITE_SHELL_FIDDLE */
12623  return rc;
12624}
12625
12626
12627#ifdef SQLITE_SHELL_FIDDLE
12628/* Only for emcc experimentation purposes. */
12629int fiddle_experiment(int a,int b){
12630   return a + b;
12631}
12632
12633/* Only for emcc experimentation purposes.
12634
12635  Define this function in JS using:
12636
12637  emcc ... --js-library somefile.js
12638
12639  containing:
12640
12641mergeInto(LibraryManager.library, {
12642    my_foo: function(){
12643        console.debug("my_foo()",arguments);
12644    }
12645});
12646*/
12647/*extern void my_foo(sqlite3 *);*/
12648/* Only for emcc experimentation purposes. */
12649sqlite3 * fiddle_the_db(){
12650    printf("fiddle_the_db(%p)\n", (const void*)globalDb);
12651    /*my_foo(globalDb);*/
12652    return globalDb;
12653}
12654/* Only for emcc experimentation purposes. */
12655sqlite3 * fiddle_db_arg(sqlite3 *arg){
12656    printf("fiddle_db_arg(%p)\n", (const void*)arg);
12657    return arg;
12658}
12659
12660/*
12661** Intended to be called via a SharedWorker() while a separate
12662** SharedWorker() (which manages the wasm module) is performing work
12663** which should be interrupted. Unfortunately, SharedWorker is not
12664** portable enough to make real use of.
12665*/
12666void fiddle_interrupt(void){
12667  if(globalDb) sqlite3_interrupt(globalDb);
12668}
12669
12670/*
12671** Returns the filename of the given db name, assuming "main" if
12672** zDbName is NULL. Returns NULL if globalDb is not opened.
12673*/
12674const char * fiddle_db_filename(const char * zDbName){
12675    return globalDb
12676      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12677      : NULL;
12678}
12679
12680/*
12681** Closes, unlinks, and reopens the db using its current filename (or
12682** the default if the db is currently closed). It is assumed, for
12683** purposes of the fiddle build, that the file is in a transient
12684** virtual filesystem within the browser.
12685*/
12686void fiddle_reset_db(void){
12687  char *zFilename = 0;
12688  if(0==globalDb){
12689    shellState.pAuxDb->zDbFilename = "/fiddle.sqlite3";
12690  }else{
12691    zFilename =
12692      sqlite3_mprintf("%s", sqlite3_db_filename(globalDb, "main"));
12693    shell_check_oom(zFilename);
12694    close_db(globalDb);
12695    shellDeleteFile(zFilename);
12696    shellState.db = 0;
12697    shellState.pAuxDb->zDbFilename = zFilename;
12698  }
12699  open_db(&shellState, 0);
12700  sqlite3_free(zFilename);
12701}
12702
12703/*
12704** Trivial exportable function for emscripten. Needs to be exported using:
12705**
12706** emcc ..flags... -sEXPORTED_FUNCTIONS=_fiddle_exec -sEXPORTED_RUNTIME_METHODS=ccall,cwrap
12707**
12708** (Note the underscore before the function name.) It processes zSql
12709** as if it were input to the sqlite3 shell and redirects all output
12710** to the wasm binding.
12711*/
12712void fiddle_exec(const char * zSql){
12713  static int once = 0;
12714  int rc = 0;
12715  if(!once){
12716    /* Simulate an argv array for main() */
12717    static char * argv[] = {"fiddle",
12718                            "-bail",
12719                            "-safe"};
12720    rc = fiddle_main((int)(sizeof(argv)/sizeof(argv[0])), argv);
12721    once = rc ? -1 : 1;
12722    memset(&shellState.wasm, 0, sizeof(shellState.wasm));
12723    printf(
12724        "SQLite version %s %.19s\n" /*extra-version-info*/,
12725        sqlite3_libversion(), sqlite3_sourceid()
12726    );
12727    puts("WASM shell");
12728    puts("Enter \".help\" for usage hints.");
12729    if(once>0){
12730      fiddle_reset_db();
12731    }
12732    if(shellState.db){
12733      printf("Connected to %s.\n", fiddle_db_filename(NULL));
12734    }else{
12735      fprintf(stderr,"ERROR initializing db!\n");
12736      return;
12737    }
12738  }
12739  if(once<0){
12740    puts("DB init failed. Not executing SQL.");
12741  }else if(zSql && *zSql){
12742    shellState.wasm.zInput = zSql;
12743    shellState.wasm.zPos = zSql;
12744    process_input(&shellState);
12745    memset(&shellState.wasm, 0, sizeof(shellState.wasm));
12746  }
12747}
12748#endif /* SQLITE_SHELL_FIDDLE */
12749