xref: /sqlite-3.40.0/src/shell.c.in (revision b5e2e6fc)
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      i64 nTrans = strlen(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  i64 len;
820  i64 i;
821  i64 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    const char * zDefaultDbName; /* Default name for db file */
1176  } wasm;
1177#endif
1178};
1179
1180#ifdef SQLITE_SHELL_FIDDLE
1181static ShellState shellState;
1182#endif
1183
1184
1185/* Allowed values for ShellState.autoEQP
1186*/
1187#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1188#define AUTOEQP_on       1           /* Automatic EQP is on */
1189#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1190#define AUTOEQP_full     3           /* Show full EXPLAIN */
1191
1192/* Allowed values for ShellState.openMode
1193*/
1194#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1195#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1196#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1197#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1198#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1199#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1200#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1201
1202/* Allowed values for ShellState.eTraceType
1203*/
1204#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1205#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1206#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1207
1208/* Bits in the ShellState.flgProgress variable */
1209#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1210#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1211                                   ** callback limit is reached, and for each
1212                                   ** top-level SQL statement */
1213#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1214
1215/*
1216** These are the allowed shellFlgs values
1217*/
1218#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1219#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1220#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1221#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1222#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1223#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1224#define SHFLG_Echo           0x00000040 /* .echo on/off, or --echo setting */
1225#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1226#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1227#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1228
1229/*
1230** Macros for testing and setting shellFlgs
1231*/
1232#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1233#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1234#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1235
1236/*
1237** These are the allowed modes.
1238*/
1239#define MODE_Line     0  /* One column per line.  Blank line between records */
1240#define MODE_Column   1  /* One record per line in neat columns */
1241#define MODE_List     2  /* One record per line with a separator */
1242#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1243#define MODE_Html     4  /* Generate an XHTML table */
1244#define MODE_Insert   5  /* Generate SQL "insert" statements */
1245#define MODE_Quote    6  /* Quote values as for SQL */
1246#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1247#define MODE_Csv      8  /* Quote strings, numbers are plain */
1248#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1249#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1250#define MODE_Pretty  11  /* Pretty-print schemas */
1251#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1252#define MODE_Json    13  /* Output JSON */
1253#define MODE_Markdown 14 /* Markdown formatting */
1254#define MODE_Table   15  /* MySQL-style table formatting */
1255#define MODE_Box     16  /* Unicode box-drawing characters */
1256#define MODE_Count   17  /* Output only a count of the rows of output */
1257#define MODE_Off     18  /* No query output shown */
1258
1259static const char *modeDescr[] = {
1260  "line",
1261  "column",
1262  "list",
1263  "semi",
1264  "html",
1265  "insert",
1266  "quote",
1267  "tcl",
1268  "csv",
1269  "explain",
1270  "ascii",
1271  "prettyprint",
1272  "eqp",
1273  "json",
1274  "markdown",
1275  "table",
1276  "box",
1277  "count",
1278  "off"
1279};
1280
1281/*
1282** These are the column/row/line separators used by the various
1283** import/export modes.
1284*/
1285#define SEP_Column    "|"
1286#define SEP_Row       "\n"
1287#define SEP_Tab       "\t"
1288#define SEP_Space     " "
1289#define SEP_Comma     ","
1290#define SEP_CrLf      "\r\n"
1291#define SEP_Unit      "\x1F"
1292#define SEP_Record    "\x1E"
1293
1294/*
1295** Limit input nesting via .read or any other input redirect.
1296** It's not too expensive, so a generous allowance can be made.
1297*/
1298#define MAX_INPUT_NESTING 25
1299
1300/*
1301** A callback for the sqlite3_log() interface.
1302*/
1303static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1304  ShellState *p = (ShellState*)pArg;
1305  if( p->pLog==0 ) return;
1306  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1307  fflush(p->pLog);
1308}
1309
1310/*
1311** SQL function:  shell_putsnl(X)
1312**
1313** Write the text X to the screen (or whatever output is being directed)
1314** adding a newline at the end, and then return X.
1315*/
1316static void shellPutsFunc(
1317  sqlite3_context *pCtx,
1318  int nVal,
1319  sqlite3_value **apVal
1320){
1321  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1322  (void)nVal;
1323  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1324  sqlite3_result_value(pCtx, apVal[0]);
1325}
1326
1327/*
1328** If in safe mode, print an error message described by the arguments
1329** and exit immediately.
1330*/
1331static void failIfSafeMode(
1332  ShellState *p,
1333  const char *zErrMsg,
1334  ...
1335){
1336  if( p->bSafeMode ){
1337    va_list ap;
1338    char *zMsg;
1339    va_start(ap, zErrMsg);
1340    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1341    va_end(ap);
1342    raw_printf(stderr, "line %d: ", p->lineno);
1343    utf8_printf(stderr, "%s\n", zMsg);
1344    exit(1);
1345  }
1346}
1347
1348/*
1349** SQL function:   edit(VALUE)
1350**                 edit(VALUE,EDITOR)
1351**
1352** These steps:
1353**
1354**     (1) Write VALUE into a temporary file.
1355**     (2) Run program EDITOR on that temporary file.
1356**     (3) Read the temporary file back and return its content as the result.
1357**     (4) Delete the temporary file
1358**
1359** If the EDITOR argument is omitted, use the value in the VISUAL
1360** environment variable.  If still there is no EDITOR, through an error.
1361**
1362** Also throw an error if the EDITOR program returns a non-zero exit code.
1363*/
1364#ifndef SQLITE_NOHAVE_SYSTEM
1365static void editFunc(
1366  sqlite3_context *context,
1367  int argc,
1368  sqlite3_value **argv
1369){
1370  const char *zEditor;
1371  char *zTempFile = 0;
1372  sqlite3 *db;
1373  char *zCmd = 0;
1374  int bBin;
1375  int rc;
1376  int hasCRNL = 0;
1377  FILE *f = 0;
1378  sqlite3_int64 sz;
1379  sqlite3_int64 x;
1380  unsigned char *p = 0;
1381
1382  if( argc==2 ){
1383    zEditor = (const char*)sqlite3_value_text(argv[1]);
1384  }else{
1385    zEditor = getenv("VISUAL");
1386  }
1387  if( zEditor==0 ){
1388    sqlite3_result_error(context, "no editor for edit()", -1);
1389    return;
1390  }
1391  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1392    sqlite3_result_error(context, "NULL input to edit()", -1);
1393    return;
1394  }
1395  db = sqlite3_context_db_handle(context);
1396  zTempFile = 0;
1397  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1398  if( zTempFile==0 ){
1399    sqlite3_uint64 r = 0;
1400    sqlite3_randomness(sizeof(r), &r);
1401    zTempFile = sqlite3_mprintf("temp%llx", r);
1402    if( zTempFile==0 ){
1403      sqlite3_result_error_nomem(context);
1404      return;
1405    }
1406  }
1407  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1408  /* When writing the file to be edited, do \n to \r\n conversions on systems
1409  ** that want \r\n line endings */
1410  f = fopen(zTempFile, bBin ? "wb" : "w");
1411  if( f==0 ){
1412    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1413    goto edit_func_end;
1414  }
1415  sz = sqlite3_value_bytes(argv[0]);
1416  if( bBin ){
1417    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1418  }else{
1419    const char *z = (const char*)sqlite3_value_text(argv[0]);
1420    /* Remember whether or not the value originally contained \r\n */
1421    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1422    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1423  }
1424  fclose(f);
1425  f = 0;
1426  if( x!=sz ){
1427    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1428    goto edit_func_end;
1429  }
1430  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1431  if( zCmd==0 ){
1432    sqlite3_result_error_nomem(context);
1433    goto edit_func_end;
1434  }
1435  rc = system(zCmd);
1436  sqlite3_free(zCmd);
1437  if( rc ){
1438    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1439    goto edit_func_end;
1440  }
1441  f = fopen(zTempFile, "rb");
1442  if( f==0 ){
1443    sqlite3_result_error(context,
1444      "edit() cannot reopen temp file after edit", -1);
1445    goto edit_func_end;
1446  }
1447  fseek(f, 0, SEEK_END);
1448  sz = ftell(f);
1449  rewind(f);
1450  p = sqlite3_malloc64( sz+1 );
1451  if( p==0 ){
1452    sqlite3_result_error_nomem(context);
1453    goto edit_func_end;
1454  }
1455  x = fread(p, 1, (size_t)sz, f);
1456  fclose(f);
1457  f = 0;
1458  if( x!=sz ){
1459    sqlite3_result_error(context, "could not read back the whole file", -1);
1460    goto edit_func_end;
1461  }
1462  if( bBin ){
1463    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1464  }else{
1465    sqlite3_int64 i, j;
1466    if( hasCRNL ){
1467      /* If the original contains \r\n then do no conversions back to \n */
1468    }else{
1469      /* If the file did not originally contain \r\n then convert any new
1470      ** \r\n back into \n */
1471      for(i=j=0; i<sz; i++){
1472        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1473        p[j++] = p[i];
1474      }
1475      sz = j;
1476      p[sz] = 0;
1477    }
1478    sqlite3_result_text64(context, (const char*)p, sz,
1479                          sqlite3_free, SQLITE_UTF8);
1480  }
1481  p = 0;
1482
1483edit_func_end:
1484  if( f ) fclose(f);
1485  unlink(zTempFile);
1486  sqlite3_free(zTempFile);
1487  sqlite3_free(p);
1488}
1489#endif /* SQLITE_NOHAVE_SYSTEM */
1490
1491/*
1492** Save or restore the current output mode
1493*/
1494static void outputModePush(ShellState *p){
1495  p->modePrior = p->mode;
1496  p->priorShFlgs = p->shellFlgs;
1497  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1498  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1499}
1500static void outputModePop(ShellState *p){
1501  p->mode = p->modePrior;
1502  p->shellFlgs = p->priorShFlgs;
1503  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1504  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1505}
1506
1507/*
1508** Output the given string as a hex-encoded blob (eg. X'1234' )
1509*/
1510static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1511  int i;
1512  unsigned char *aBlob = (unsigned char*)pBlob;
1513
1514  char *zStr = sqlite3_malloc(nBlob*2 + 1);
1515  shell_check_oom(zStr);
1516
1517  for(i=0; i<nBlob; i++){
1518    static const char aHex[] = {
1519        '0', '1', '2', '3', '4', '5', '6', '7',
1520        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1521    };
1522    zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1523    zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1524  }
1525  zStr[i*2] = '\0';
1526
1527  raw_printf(out,"X'%s'", zStr);
1528  sqlite3_free(zStr);
1529}
1530
1531/*
1532** Find a string that is not found anywhere in z[].  Return a pointer
1533** to that string.
1534**
1535** Try to use zA and zB first.  If both of those are already found in z[]
1536** then make up some string and store it in the buffer zBuf.
1537*/
1538static const char *unused_string(
1539  const char *z,                    /* Result must not appear anywhere in z */
1540  const char *zA, const char *zB,   /* Try these first */
1541  char *zBuf                        /* Space to store a generated string */
1542){
1543  unsigned i = 0;
1544  if( strstr(z, zA)==0 ) return zA;
1545  if( strstr(z, zB)==0 ) return zB;
1546  do{
1547    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1548  }while( strstr(z,zBuf)!=0 );
1549  return zBuf;
1550}
1551
1552/*
1553** Output the given string as a quoted string using SQL quoting conventions.
1554**
1555** See also: output_quoted_escaped_string()
1556*/
1557static void output_quoted_string(FILE *out, const char *z){
1558  int i;
1559  char c;
1560  setBinaryMode(out, 1);
1561  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1562  if( c==0 ){
1563    utf8_printf(out,"'%s'",z);
1564  }else{
1565    raw_printf(out, "'");
1566    while( *z ){
1567      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1568      if( c=='\'' ) i++;
1569      if( i ){
1570        utf8_printf(out, "%.*s", i, z);
1571        z += i;
1572      }
1573      if( c=='\'' ){
1574        raw_printf(out, "'");
1575        continue;
1576      }
1577      if( c==0 ){
1578        break;
1579      }
1580      z++;
1581    }
1582    raw_printf(out, "'");
1583  }
1584  setTextMode(out, 1);
1585}
1586
1587/*
1588** Output the given string as a quoted string using SQL quoting conventions.
1589** Additionallly , escape the "\n" and "\r" characters so that they do not
1590** get corrupted by end-of-line translation facilities in some operating
1591** systems.
1592**
1593** This is like output_quoted_string() but with the addition of the \r\n
1594** escape mechanism.
1595*/
1596static void output_quoted_escaped_string(FILE *out, const char *z){
1597  int i;
1598  char c;
1599  setBinaryMode(out, 1);
1600  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1601  if( c==0 ){
1602    utf8_printf(out,"'%s'",z);
1603  }else{
1604    const char *zNL = 0;
1605    const char *zCR = 0;
1606    int nNL = 0;
1607    int nCR = 0;
1608    char zBuf1[20], zBuf2[20];
1609    for(i=0; z[i]; i++){
1610      if( z[i]=='\n' ) nNL++;
1611      if( z[i]=='\r' ) nCR++;
1612    }
1613    if( nNL ){
1614      raw_printf(out, "replace(");
1615      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1616    }
1617    if( nCR ){
1618      raw_printf(out, "replace(");
1619      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1620    }
1621    raw_printf(out, "'");
1622    while( *z ){
1623      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1624      if( c=='\'' ) i++;
1625      if( i ){
1626        utf8_printf(out, "%.*s", i, z);
1627        z += i;
1628      }
1629      if( c=='\'' ){
1630        raw_printf(out, "'");
1631        continue;
1632      }
1633      if( c==0 ){
1634        break;
1635      }
1636      z++;
1637      if( c=='\n' ){
1638        raw_printf(out, "%s", zNL);
1639        continue;
1640      }
1641      raw_printf(out, "%s", zCR);
1642    }
1643    raw_printf(out, "'");
1644    if( nCR ){
1645      raw_printf(out, ",'%s',char(13))", zCR);
1646    }
1647    if( nNL ){
1648      raw_printf(out, ",'%s',char(10))", zNL);
1649    }
1650  }
1651  setTextMode(out, 1);
1652}
1653
1654/*
1655** Output the given string as a quoted according to C or TCL quoting rules.
1656*/
1657static void output_c_string(FILE *out, const char *z){
1658  unsigned int c;
1659  fputc('"', out);
1660  while( (c = *(z++))!=0 ){
1661    if( c=='\\' ){
1662      fputc(c, out);
1663      fputc(c, out);
1664    }else if( c=='"' ){
1665      fputc('\\', out);
1666      fputc('"', out);
1667    }else if( c=='\t' ){
1668      fputc('\\', out);
1669      fputc('t', out);
1670    }else if( c=='\n' ){
1671      fputc('\\', out);
1672      fputc('n', out);
1673    }else if( c=='\r' ){
1674      fputc('\\', out);
1675      fputc('r', out);
1676    }else if( !isprint(c&0xff) ){
1677      raw_printf(out, "\\%03o", c&0xff);
1678    }else{
1679      fputc(c, out);
1680    }
1681  }
1682  fputc('"', out);
1683}
1684
1685/*
1686** Output the given string as a quoted according to JSON quoting rules.
1687*/
1688static void output_json_string(FILE *out, const char *z, i64 n){
1689  unsigned int c;
1690  if( n<0 ) n = strlen(z);
1691  fputc('"', out);
1692  while( n-- ){
1693    c = *(z++);
1694    if( c=='\\' || c=='"' ){
1695      fputc('\\', out);
1696      fputc(c, out);
1697    }else if( c<=0x1f ){
1698      fputc('\\', out);
1699      if( c=='\b' ){
1700        fputc('b', out);
1701      }else if( c=='\f' ){
1702        fputc('f', out);
1703      }else if( c=='\n' ){
1704        fputc('n', out);
1705      }else if( c=='\r' ){
1706        fputc('r', out);
1707      }else if( c=='\t' ){
1708        fputc('t', out);
1709      }else{
1710         raw_printf(out, "u%04x",c);
1711      }
1712    }else{
1713      fputc(c, out);
1714    }
1715  }
1716  fputc('"', out);
1717}
1718
1719/*
1720** Output the given string with characters that are special to
1721** HTML escaped.
1722*/
1723static void output_html_string(FILE *out, const char *z){
1724  int i;
1725  if( z==0 ) z = "";
1726  while( *z ){
1727    for(i=0;   z[i]
1728            && z[i]!='<'
1729            && z[i]!='&'
1730            && z[i]!='>'
1731            && z[i]!='\"'
1732            && z[i]!='\'';
1733        i++){}
1734    if( i>0 ){
1735      utf8_printf(out,"%.*s",i,z);
1736    }
1737    if( z[i]=='<' ){
1738      raw_printf(out,"&lt;");
1739    }else if( z[i]=='&' ){
1740      raw_printf(out,"&amp;");
1741    }else if( z[i]=='>' ){
1742      raw_printf(out,"&gt;");
1743    }else if( z[i]=='\"' ){
1744      raw_printf(out,"&quot;");
1745    }else if( z[i]=='\'' ){
1746      raw_printf(out,"&#39;");
1747    }else{
1748      break;
1749    }
1750    z += i + 1;
1751  }
1752}
1753
1754/*
1755** If a field contains any character identified by a 1 in the following
1756** array, then the string must be quoted for CSV.
1757*/
1758static const char needCsvQuote[] = {
1759  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1760  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1761  1, 0, 1, 0, 0, 0, 0, 1,   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, 0,
1766  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 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  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1775};
1776
1777/*
1778** Output a single term of CSV.  Actually, p->colSeparator is used for
1779** the separator, which may or may not be a comma.  p->nullValue is
1780** the null value.  Strings are quoted if necessary.  The separator
1781** is only issued if bSep is true.
1782*/
1783static void output_csv(ShellState *p, const char *z, int bSep){
1784  FILE *out = p->out;
1785  if( z==0 ){
1786    utf8_printf(out,"%s",p->nullValue);
1787  }else{
1788    unsigned i;
1789    for(i=0; z[i]; i++){
1790      if( needCsvQuote[((unsigned char*)z)[i]] ){
1791        i = 0;
1792        break;
1793      }
1794    }
1795    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1796      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1797      shell_check_oom(zQuoted);
1798      utf8_printf(out, "%s", zQuoted);
1799      sqlite3_free(zQuoted);
1800    }else{
1801      utf8_printf(out, "%s", z);
1802    }
1803  }
1804  if( bSep ){
1805    utf8_printf(p->out, "%s", p->colSeparator);
1806  }
1807}
1808
1809/*
1810** This routine runs when the user presses Ctrl-C
1811*/
1812static void interrupt_handler(int NotUsed){
1813  UNUSED_PARAMETER(NotUsed);
1814  seenInterrupt++;
1815  if( seenInterrupt>2 ) exit(1);
1816  if( globalDb ) sqlite3_interrupt(globalDb);
1817}
1818
1819#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1820/*
1821** This routine runs for console events (e.g. Ctrl-C) on Win32
1822*/
1823static BOOL WINAPI ConsoleCtrlHandler(
1824  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1825){
1826  if( dwCtrlType==CTRL_C_EVENT ){
1827    interrupt_handler(0);
1828    return TRUE;
1829  }
1830  return FALSE;
1831}
1832#endif
1833
1834#ifndef SQLITE_OMIT_AUTHORIZATION
1835/*
1836** This authorizer runs in safe mode.
1837*/
1838static int safeModeAuth(
1839  void *pClientData,
1840  int op,
1841  const char *zA1,
1842  const char *zA2,
1843  const char *zA3,
1844  const char *zA4
1845){
1846  ShellState *p = (ShellState*)pClientData;
1847  static const char *azProhibitedFunctions[] = {
1848    "edit",
1849    "fts3_tokenizer",
1850    "load_extension",
1851    "readfile",
1852    "writefile",
1853    "zipfile",
1854    "zipfile_cds",
1855  };
1856  UNUSED_PARAMETER(zA2);
1857  UNUSED_PARAMETER(zA3);
1858  UNUSED_PARAMETER(zA4);
1859  switch( op ){
1860    case SQLITE_ATTACH: {
1861#ifndef SQLITE_SHELL_FIDDLE
1862      /* In WASM builds the filesystem is a virtual sandbox, so
1863      ** there's no harm in using ATTACH. */
1864      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1865#endif
1866      break;
1867    }
1868    case SQLITE_FUNCTION: {
1869      int i;
1870      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1871        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1872          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1873                         azProhibitedFunctions[i]);
1874        }
1875      }
1876      break;
1877    }
1878  }
1879  return SQLITE_OK;
1880}
1881
1882/*
1883** When the ".auth ON" is set, the following authorizer callback is
1884** invoked.  It always returns SQLITE_OK.
1885*/
1886static int shellAuth(
1887  void *pClientData,
1888  int op,
1889  const char *zA1,
1890  const char *zA2,
1891  const char *zA3,
1892  const char *zA4
1893){
1894  ShellState *p = (ShellState*)pClientData;
1895  static const char *azAction[] = { 0,
1896     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1897     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1898     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1899     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1900     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1901     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1902     "PRAGMA",               "READ",                 "SELECT",
1903     "TRANSACTION",          "UPDATE",               "ATTACH",
1904     "DETACH",               "ALTER_TABLE",          "REINDEX",
1905     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1906     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1907  };
1908  int i;
1909  const char *az[4];
1910  az[0] = zA1;
1911  az[1] = zA2;
1912  az[2] = zA3;
1913  az[3] = zA4;
1914  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1915  for(i=0; i<4; i++){
1916    raw_printf(p->out, " ");
1917    if( az[i] ){
1918      output_c_string(p->out, az[i]);
1919    }else{
1920      raw_printf(p->out, "NULL");
1921    }
1922  }
1923  raw_printf(p->out, "\n");
1924  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1925  return SQLITE_OK;
1926}
1927#endif
1928
1929/*
1930** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1931**
1932** This routine converts some CREATE TABLE statements for shadow tables
1933** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1934**
1935** If the schema statement in z[] contains a start-of-comment and if
1936** sqlite3_complete() returns false, try to terminate the comment before
1937** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
1938*/
1939static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1940  char *zToFree = 0;
1941  if( z==0 ) return;
1942  if( zTail==0 ) return;
1943  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
1944    const char *zOrig = z;
1945    static const char *azTerm[] = { "", "*/", "\n" };
1946    int i;
1947    for(i=0; i<ArraySize(azTerm); i++){
1948      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
1949      if( sqlite3_complete(zNew) ){
1950        size_t n = strlen(zNew);
1951        zNew[n-1] = 0;
1952        zToFree = zNew;
1953        z = zNew;
1954        break;
1955      }
1956      sqlite3_free(zNew);
1957    }
1958  }
1959  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1960    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1961  }else{
1962    utf8_printf(out, "%s%s", z, zTail);
1963  }
1964  sqlite3_free(zToFree);
1965}
1966static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1967  char c = z[n];
1968  z[n] = 0;
1969  printSchemaLine(out, z, zTail);
1970  z[n] = c;
1971}
1972
1973/*
1974** Return true if string z[] has nothing but whitespace and comments to the
1975** end of the first line.
1976*/
1977static int wsToEol(const char *z){
1978  int i;
1979  for(i=0; z[i]; i++){
1980    if( z[i]=='\n' ) return 1;
1981    if( IsSpace(z[i]) ) continue;
1982    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1983    return 0;
1984  }
1985  return 1;
1986}
1987
1988/*
1989** Add a new entry to the EXPLAIN QUERY PLAN data
1990*/
1991static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1992  EQPGraphRow *pNew;
1993  i64 nText;
1994  if( zText==0 ) return;
1995  nText = strlen(zText);
1996  if( p->autoEQPtest ){
1997    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1998  }
1999  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2000  shell_check_oom(pNew);
2001  pNew->iEqpId = iEqpId;
2002  pNew->iParentId = p2;
2003  memcpy(pNew->zText, zText, nText+1);
2004  pNew->pNext = 0;
2005  if( p->sGraph.pLast ){
2006    p->sGraph.pLast->pNext = pNew;
2007  }else{
2008    p->sGraph.pRow = pNew;
2009  }
2010  p->sGraph.pLast = pNew;
2011}
2012
2013/*
2014** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2015** in p->sGraph.
2016*/
2017static void eqp_reset(ShellState *p){
2018  EQPGraphRow *pRow, *pNext;
2019  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2020    pNext = pRow->pNext;
2021    sqlite3_free(pRow);
2022  }
2023  memset(&p->sGraph, 0, sizeof(p->sGraph));
2024}
2025
2026/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2027** pOld, or return the first such line if pOld is NULL
2028*/
2029static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2030  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2031  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2032  return pRow;
2033}
2034
2035/* Render a single level of the graph that has iEqpId as its parent.  Called
2036** recursively to render sublevels.
2037*/
2038static void eqp_render_level(ShellState *p, int iEqpId){
2039  EQPGraphRow *pRow, *pNext;
2040  i64 n = strlen(p->sGraph.zPrefix);
2041  char *z;
2042  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2043    pNext = eqp_next_row(p, iEqpId, pRow);
2044    z = pRow->zText;
2045    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2046                pNext ? "|--" : "`--", z);
2047    if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2048      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
2049      eqp_render_level(p, pRow->iEqpId);
2050      p->sGraph.zPrefix[n] = 0;
2051    }
2052  }
2053}
2054
2055/*
2056** Display and reset the EXPLAIN QUERY PLAN data
2057*/
2058static void eqp_render(ShellState *p){
2059  EQPGraphRow *pRow = p->sGraph.pRow;
2060  if( pRow ){
2061    if( pRow->zText[0]=='-' ){
2062      if( pRow->pNext==0 ){
2063        eqp_reset(p);
2064        return;
2065      }
2066      utf8_printf(p->out, "%s\n", pRow->zText+3);
2067      p->sGraph.pRow = pRow->pNext;
2068      sqlite3_free(pRow);
2069    }else{
2070      utf8_printf(p->out, "QUERY PLAN\n");
2071    }
2072    p->sGraph.zPrefix[0] = 0;
2073    eqp_render_level(p, 0);
2074    eqp_reset(p);
2075  }
2076}
2077
2078#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2079/*
2080** Progress handler callback.
2081*/
2082static int progress_handler(void *pClientData) {
2083  ShellState *p = (ShellState*)pClientData;
2084  p->nProgress++;
2085  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2086    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2087    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2088    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2089    return 1;
2090  }
2091  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2092    raw_printf(p->out, "Progress %u\n", p->nProgress);
2093  }
2094  return 0;
2095}
2096#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2097
2098/*
2099** Print N dashes
2100*/
2101static void print_dashes(FILE *out, int N){
2102  const char zDash[] = "--------------------------------------------------";
2103  const int nDash = sizeof(zDash) - 1;
2104  while( N>nDash ){
2105    fputs(zDash, out);
2106    N -= nDash;
2107  }
2108  raw_printf(out, "%.*s", N, zDash);
2109}
2110
2111/*
2112** Print a markdown or table-style row separator using ascii-art
2113*/
2114static void print_row_separator(
2115  ShellState *p,
2116  int nArg,
2117  const char *zSep
2118){
2119  int i;
2120  if( nArg>0 ){
2121    fputs(zSep, p->out);
2122    print_dashes(p->out, p->actualWidth[0]+2);
2123    for(i=1; i<nArg; i++){
2124      fputs(zSep, p->out);
2125      print_dashes(p->out, p->actualWidth[i]+2);
2126    }
2127    fputs(zSep, p->out);
2128  }
2129  fputs("\n", p->out);
2130}
2131
2132/*
2133** This is the callback routine that the shell
2134** invokes for each row of a query result.
2135*/
2136static int shell_callback(
2137  void *pArg,
2138  int nArg,        /* Number of result columns */
2139  char **azArg,    /* Text of each result column */
2140  char **azCol,    /* Column names */
2141  int *aiType      /* Column types.  Might be NULL */
2142){
2143  int i;
2144  ShellState *p = (ShellState*)pArg;
2145
2146  if( azArg==0 ) return 0;
2147  switch( p->cMode ){
2148    case MODE_Count:
2149    case MODE_Off: {
2150      break;
2151    }
2152    case MODE_Line: {
2153      int w = 5;
2154      if( azArg==0 ) break;
2155      for(i=0; i<nArg; i++){
2156        int len = strlen30(azCol[i] ? azCol[i] : "");
2157        if( len>w ) w = len;
2158      }
2159      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2160      for(i=0; i<nArg; i++){
2161        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2162                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2163      }
2164      break;
2165    }
2166    case MODE_Explain: {
2167      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2168      if( nArg>ArraySize(aExplainWidth) ){
2169        nArg = ArraySize(aExplainWidth);
2170      }
2171      if( p->cnt++==0 ){
2172        for(i=0; i<nArg; i++){
2173          int w = aExplainWidth[i];
2174          utf8_width_print(p->out, w, azCol[i]);
2175          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2176        }
2177        for(i=0; i<nArg; i++){
2178          int w = aExplainWidth[i];
2179          print_dashes(p->out, w);
2180          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2181        }
2182      }
2183      if( azArg==0 ) break;
2184      for(i=0; i<nArg; i++){
2185        int w = aExplainWidth[i];
2186        if( i==nArg-1 ) w = 0;
2187        if( azArg[i] && strlenChar(azArg[i])>w ){
2188          w = strlenChar(azArg[i]);
2189        }
2190        if( i==1 && p->aiIndent && p->pStmt ){
2191          if( p->iIndent<p->nIndent ){
2192            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2193          }
2194          p->iIndent++;
2195        }
2196        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2197        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2198      }
2199      break;
2200    }
2201    case MODE_Semi: {   /* .schema and .fullschema output */
2202      printSchemaLine(p->out, azArg[0], ";\n");
2203      break;
2204    }
2205    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2206      char *z;
2207      int j;
2208      int nParen = 0;
2209      char cEnd = 0;
2210      char c;
2211      int nLine = 0;
2212      assert( nArg==1 );
2213      if( azArg[0]==0 ) break;
2214      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2215       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2216      ){
2217        utf8_printf(p->out, "%s;\n", azArg[0]);
2218        break;
2219      }
2220      z = sqlite3_mprintf("%s", azArg[0]);
2221      shell_check_oom(z);
2222      j = 0;
2223      for(i=0; IsSpace(z[i]); i++){}
2224      for(; (c = z[i])!=0; i++){
2225        if( IsSpace(c) ){
2226          if( z[j-1]=='\r' ) z[j-1] = '\n';
2227          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2228        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2229          j--;
2230        }
2231        z[j++] = c;
2232      }
2233      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2234      z[j] = 0;
2235      if( strlen30(z)>=79 ){
2236        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2237          if( c==cEnd ){
2238            cEnd = 0;
2239          }else if( c=='"' || c=='\'' || c=='`' ){
2240            cEnd = c;
2241          }else if( c=='[' ){
2242            cEnd = ']';
2243          }else if( c=='-' && z[i+1]=='-' ){
2244            cEnd = '\n';
2245          }else if( c=='(' ){
2246            nParen++;
2247          }else if( c==')' ){
2248            nParen--;
2249            if( nLine>0 && nParen==0 && j>0 ){
2250              printSchemaLineN(p->out, z, j, "\n");
2251              j = 0;
2252            }
2253          }
2254          z[j++] = c;
2255          if( nParen==1 && cEnd==0
2256           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2257          ){
2258            if( c=='\n' ) j--;
2259            printSchemaLineN(p->out, z, j, "\n  ");
2260            j = 0;
2261            nLine++;
2262            while( IsSpace(z[i+1]) ){ i++; }
2263          }
2264        }
2265        z[j] = 0;
2266      }
2267      printSchemaLine(p->out, z, ";\n");
2268      sqlite3_free(z);
2269      break;
2270    }
2271    case MODE_List: {
2272      if( p->cnt++==0 && p->showHeader ){
2273        for(i=0; i<nArg; i++){
2274          utf8_printf(p->out,"%s%s",azCol[i],
2275                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2276        }
2277      }
2278      if( azArg==0 ) break;
2279      for(i=0; i<nArg; i++){
2280        char *z = azArg[i];
2281        if( z==0 ) z = p->nullValue;
2282        utf8_printf(p->out, "%s", z);
2283        if( i<nArg-1 ){
2284          utf8_printf(p->out, "%s", p->colSeparator);
2285        }else{
2286          utf8_printf(p->out, "%s", p->rowSeparator);
2287        }
2288      }
2289      break;
2290    }
2291    case MODE_Html: {
2292      if( p->cnt++==0 && p->showHeader ){
2293        raw_printf(p->out,"<TR>");
2294        for(i=0; i<nArg; i++){
2295          raw_printf(p->out,"<TH>");
2296          output_html_string(p->out, azCol[i]);
2297          raw_printf(p->out,"</TH>\n");
2298        }
2299        raw_printf(p->out,"</TR>\n");
2300      }
2301      if( azArg==0 ) break;
2302      raw_printf(p->out,"<TR>");
2303      for(i=0; i<nArg; i++){
2304        raw_printf(p->out,"<TD>");
2305        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2306        raw_printf(p->out,"</TD>\n");
2307      }
2308      raw_printf(p->out,"</TR>\n");
2309      break;
2310    }
2311    case MODE_Tcl: {
2312      if( p->cnt++==0 && p->showHeader ){
2313        for(i=0; i<nArg; i++){
2314          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2315          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2316        }
2317        utf8_printf(p->out, "%s", p->rowSeparator);
2318      }
2319      if( azArg==0 ) break;
2320      for(i=0; i<nArg; i++){
2321        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2322        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2323      }
2324      utf8_printf(p->out, "%s", p->rowSeparator);
2325      break;
2326    }
2327    case MODE_Csv: {
2328      setBinaryMode(p->out, 1);
2329      if( p->cnt++==0 && p->showHeader ){
2330        for(i=0; i<nArg; i++){
2331          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2332        }
2333        utf8_printf(p->out, "%s", p->rowSeparator);
2334      }
2335      if( nArg>0 ){
2336        for(i=0; i<nArg; i++){
2337          output_csv(p, azArg[i], i<nArg-1);
2338        }
2339        utf8_printf(p->out, "%s", p->rowSeparator);
2340      }
2341      setTextMode(p->out, 1);
2342      break;
2343    }
2344    case MODE_Insert: {
2345      if( azArg==0 ) break;
2346      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2347      if( p->showHeader ){
2348        raw_printf(p->out,"(");
2349        for(i=0; i<nArg; i++){
2350          if( i>0 ) raw_printf(p->out, ",");
2351          if( quoteChar(azCol[i]) ){
2352            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2353            shell_check_oom(z);
2354            utf8_printf(p->out, "%s", z);
2355            sqlite3_free(z);
2356          }else{
2357            raw_printf(p->out, "%s", azCol[i]);
2358          }
2359        }
2360        raw_printf(p->out,")");
2361      }
2362      p->cnt++;
2363      for(i=0; i<nArg; i++){
2364        raw_printf(p->out, i>0 ? "," : " VALUES(");
2365        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2366          utf8_printf(p->out,"NULL");
2367        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2368          if( ShellHasFlag(p, SHFLG_Newlines) ){
2369            output_quoted_string(p->out, azArg[i]);
2370          }else{
2371            output_quoted_escaped_string(p->out, azArg[i]);
2372          }
2373        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2374          utf8_printf(p->out,"%s", azArg[i]);
2375        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2376          char z[50];
2377          double r = sqlite3_column_double(p->pStmt, i);
2378          sqlite3_uint64 ur;
2379          memcpy(&ur,&r,sizeof(r));
2380          if( ur==0x7ff0000000000000LL ){
2381            raw_printf(p->out, "1e999");
2382          }else if( ur==0xfff0000000000000LL ){
2383            raw_printf(p->out, "-1e999");
2384          }else{
2385            sqlite3_int64 ir = (sqlite3_int64)r;
2386            if( r==(double)ir ){
2387              sqlite3_snprintf(50,z,"%lld.0", ir);
2388            }else{
2389              sqlite3_snprintf(50,z,"%!.20g", r);
2390            }
2391            raw_printf(p->out, "%s", z);
2392          }
2393        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2394          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2395          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2396          output_hex_blob(p->out, pBlob, nBlob);
2397        }else if( isNumber(azArg[i], 0) ){
2398          utf8_printf(p->out,"%s", azArg[i]);
2399        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2400          output_quoted_string(p->out, azArg[i]);
2401        }else{
2402          output_quoted_escaped_string(p->out, azArg[i]);
2403        }
2404      }
2405      raw_printf(p->out,");\n");
2406      break;
2407    }
2408    case MODE_Json: {
2409      if( azArg==0 ) break;
2410      if( p->cnt==0 ){
2411        fputs("[{", p->out);
2412      }else{
2413        fputs(",\n{", p->out);
2414      }
2415      p->cnt++;
2416      for(i=0; i<nArg; i++){
2417        output_json_string(p->out, azCol[i], -1);
2418        putc(':', p->out);
2419        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2420          fputs("null",p->out);
2421        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2422          char z[50];
2423          double r = sqlite3_column_double(p->pStmt, i);
2424          sqlite3_uint64 ur;
2425          memcpy(&ur,&r,sizeof(r));
2426          if( ur==0x7ff0000000000000LL ){
2427            raw_printf(p->out, "1e999");
2428          }else if( ur==0xfff0000000000000LL ){
2429            raw_printf(p->out, "-1e999");
2430          }else{
2431            sqlite3_snprintf(50,z,"%!.20g", r);
2432            raw_printf(p->out, "%s", z);
2433          }
2434        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2435          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2436          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2437          output_json_string(p->out, pBlob, nBlob);
2438        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2439          output_json_string(p->out, azArg[i], -1);
2440        }else{
2441          utf8_printf(p->out,"%s", azArg[i]);
2442        }
2443        if( i<nArg-1 ){
2444          putc(',', p->out);
2445        }
2446      }
2447      putc('}', p->out);
2448      break;
2449    }
2450    case MODE_Quote: {
2451      if( azArg==0 ) break;
2452      if( p->cnt==0 && p->showHeader ){
2453        for(i=0; i<nArg; i++){
2454          if( i>0 ) fputs(p->colSeparator, p->out);
2455          output_quoted_string(p->out, azCol[i]);
2456        }
2457        fputs(p->rowSeparator, p->out);
2458      }
2459      p->cnt++;
2460      for(i=0; i<nArg; i++){
2461        if( i>0 ) fputs(p->colSeparator, p->out);
2462        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2463          utf8_printf(p->out,"NULL");
2464        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2465          output_quoted_string(p->out, azArg[i]);
2466        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2467          utf8_printf(p->out,"%s", azArg[i]);
2468        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2469          char z[50];
2470          double r = sqlite3_column_double(p->pStmt, i);
2471          sqlite3_snprintf(50,z,"%!.20g", r);
2472          raw_printf(p->out, "%s", z);
2473        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2474          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2475          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2476          output_hex_blob(p->out, pBlob, nBlob);
2477        }else if( isNumber(azArg[i], 0) ){
2478          utf8_printf(p->out,"%s", azArg[i]);
2479        }else{
2480          output_quoted_string(p->out, azArg[i]);
2481        }
2482      }
2483      fputs(p->rowSeparator, p->out);
2484      break;
2485    }
2486    case MODE_Ascii: {
2487      if( p->cnt++==0 && p->showHeader ){
2488        for(i=0; i<nArg; i++){
2489          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2490          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2491        }
2492        utf8_printf(p->out, "%s", p->rowSeparator);
2493      }
2494      if( azArg==0 ) break;
2495      for(i=0; i<nArg; i++){
2496        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2497        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2498      }
2499      utf8_printf(p->out, "%s", p->rowSeparator);
2500      break;
2501    }
2502    case MODE_EQP: {
2503      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2504      break;
2505    }
2506  }
2507  return 0;
2508}
2509
2510/*
2511** This is the callback routine that the SQLite library
2512** invokes for each row of a query result.
2513*/
2514static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2515  /* since we don't have type info, call the shell_callback with a NULL value */
2516  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2517}
2518
2519/*
2520** This is the callback routine from sqlite3_exec() that appends all
2521** output onto the end of a ShellText object.
2522*/
2523static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2524  ShellText *p = (ShellText*)pArg;
2525  int i;
2526  UNUSED_PARAMETER(az);
2527  if( azArg==0 ) return 0;
2528  if( p->n ) appendText(p, "|", 0);
2529  for(i=0; i<nArg; i++){
2530    if( i ) appendText(p, ",", 0);
2531    if( azArg[i] ) appendText(p, azArg[i], 0);
2532  }
2533  return 0;
2534}
2535
2536/*
2537** Generate an appropriate SELFTEST table in the main database.
2538*/
2539static void createSelftestTable(ShellState *p){
2540  char *zErrMsg = 0;
2541  sqlite3_exec(p->db,
2542    "SAVEPOINT selftest_init;\n"
2543    "CREATE TABLE IF NOT EXISTS selftest(\n"
2544    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2545    "  op TEXT,\n"                   /* Operator:  memo run */
2546    "  cmd TEXT,\n"                  /* Command text */
2547    "  ans TEXT\n"                   /* Desired answer */
2548    ");"
2549    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2550    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2551    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2552    "         'memo','Tests generated by --init');\n"
2553    "INSERT INTO [_shell$self]\n"
2554    "  SELECT 'run',\n"
2555    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2556                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2557    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2558                          "FROM sqlite_schema ORDER BY 2',224));\n"
2559    "INSERT INTO [_shell$self]\n"
2560    "  SELECT 'run',"
2561    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2562    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2563    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2564    "  FROM (\n"
2565    "    SELECT name FROM sqlite_schema\n"
2566    "     WHERE type='table'\n"
2567    "       AND name<>'selftest'\n"
2568    "       AND coalesce(rootpage,0)>0\n"
2569    "  )\n"
2570    " ORDER BY name;\n"
2571    "INSERT INTO [_shell$self]\n"
2572    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2573    "INSERT INTO selftest(tno,op,cmd,ans)"
2574    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2575    "DROP TABLE [_shell$self];"
2576    ,0,0,&zErrMsg);
2577  if( zErrMsg ){
2578    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2579    sqlite3_free(zErrMsg);
2580  }
2581  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2582}
2583
2584
2585/*
2586** Set the destination table field of the ShellState structure to
2587** the name of the table given.  Escape any quote characters in the
2588** table name.
2589*/
2590static void set_table_name(ShellState *p, const char *zName){
2591  int i, n;
2592  char cQuote;
2593  char *z;
2594
2595  if( p->zDestTable ){
2596    free(p->zDestTable);
2597    p->zDestTable = 0;
2598  }
2599  if( zName==0 ) return;
2600  cQuote = quoteChar(zName);
2601  n = strlen30(zName);
2602  if( cQuote ) n += n+2;
2603  z = p->zDestTable = malloc( n+1 );
2604  shell_check_oom(z);
2605  n = 0;
2606  if( cQuote ) z[n++] = cQuote;
2607  for(i=0; zName[i]; i++){
2608    z[n++] = zName[i];
2609    if( zName[i]==cQuote ) z[n++] = cQuote;
2610  }
2611  if( cQuote ) z[n++] = cQuote;
2612  z[n] = 0;
2613}
2614
2615/*
2616** Maybe construct two lines of text that point out the position of a
2617** syntax error.  Return a pointer to the text, in memory obtained from
2618** sqlite3_malloc().  Or, if the most recent error does not involve a
2619** specific token that we can point to, return an empty string.
2620**
2621** In all cases, the memory returned is obtained from sqlite3_malloc64()
2622** and should be released by the caller invoking sqlite3_free().
2623*/
2624static char *shell_error_context(const char *zSql, sqlite3 *db){
2625  int iOffset;
2626  size_t len;
2627  char *zCode;
2628  char *zMsg;
2629  int i;
2630  if( db==0
2631   || zSql==0
2632   || (iOffset = sqlite3_error_offset(db))<0
2633  ){
2634    return sqlite3_mprintf("");
2635  }
2636  while( iOffset>50 ){
2637    iOffset--;
2638    zSql++;
2639    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
2640  }
2641  len = strlen(zSql);
2642  if( len>78 ){
2643    len = 78;
2644    while( (zSql[len]&0xc0)==0x80 ) len--;
2645  }
2646  zCode = sqlite3_mprintf("%.*s", len, zSql);
2647  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
2648  if( iOffset<25 ){
2649    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");
2650  }else{
2651    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");
2652  }
2653  return zMsg;
2654}
2655
2656
2657/*
2658** Execute a query statement that will generate SQL output.  Print
2659** the result columns, comma-separated, on a line and then add a
2660** semicolon terminator to the end of that line.
2661**
2662** If the number of columns is 1 and that column contains text "--"
2663** then write the semicolon on a separate line.  That way, if a
2664** "--" comment occurs at the end of the statement, the comment
2665** won't consume the semicolon terminator.
2666*/
2667static int run_table_dump_query(
2668  ShellState *p,           /* Query context */
2669  const char *zSelect      /* SELECT statement to extract content */
2670){
2671  sqlite3_stmt *pSelect;
2672  int rc;
2673  int nResult;
2674  int i;
2675  const char *z;
2676  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2677  if( rc!=SQLITE_OK || !pSelect ){
2678    char *zContext = shell_error_context(zSelect, p->db);
2679    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
2680                sqlite3_errmsg(p->db), zContext);
2681    sqlite3_free(zContext);
2682    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2683    return rc;
2684  }
2685  rc = sqlite3_step(pSelect);
2686  nResult = sqlite3_column_count(pSelect);
2687  while( rc==SQLITE_ROW ){
2688    z = (const char*)sqlite3_column_text(pSelect, 0);
2689    utf8_printf(p->out, "%s", z);
2690    for(i=1; i<nResult; i++){
2691      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2692    }
2693    if( z==0 ) z = "";
2694    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2695    if( z[0] ){
2696      raw_printf(p->out, "\n;\n");
2697    }else{
2698      raw_printf(p->out, ";\n");
2699    }
2700    rc = sqlite3_step(pSelect);
2701  }
2702  rc = sqlite3_finalize(pSelect);
2703  if( rc!=SQLITE_OK ){
2704    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2705                sqlite3_errmsg(p->db));
2706    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2707  }
2708  return rc;
2709}
2710
2711/*
2712** Allocate space and save off string indicating current error.
2713*/
2714static char *save_err_msg(
2715  sqlite3 *db,           /* Database to query */
2716  const char *zPhase,    /* When the error occcurs */
2717  int rc,                /* Error code returned from API */
2718  const char *zSql       /* SQL string, or NULL */
2719){
2720  char *zErr;
2721  char *zContext;
2722  sqlite3_str *pStr = sqlite3_str_new(0);
2723  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
2724  if( rc>1 ){
2725    sqlite3_str_appendf(pStr, " (%d)", rc);
2726  }
2727  zContext = shell_error_context(zSql, db);
2728  if( zContext ){
2729    sqlite3_str_appendall(pStr, zContext);
2730    sqlite3_free(zContext);
2731  }
2732  zErr = sqlite3_str_finish(pStr);
2733  shell_check_oom(zErr);
2734  return zErr;
2735}
2736
2737#ifdef __linux__
2738/*
2739** Attempt to display I/O stats on Linux using /proc/PID/io
2740*/
2741static void displayLinuxIoStats(FILE *out){
2742  FILE *in;
2743  char z[200];
2744  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2745  in = fopen(z, "rb");
2746  if( in==0 ) return;
2747  while( fgets(z, sizeof(z), in)!=0 ){
2748    static const struct {
2749      const char *zPattern;
2750      const char *zDesc;
2751    } aTrans[] = {
2752      { "rchar: ",                  "Bytes received by read():" },
2753      { "wchar: ",                  "Bytes sent to write():"    },
2754      { "syscr: ",                  "Read() system calls:"      },
2755      { "syscw: ",                  "Write() system calls:"     },
2756      { "read_bytes: ",             "Bytes read from storage:"  },
2757      { "write_bytes: ",            "Bytes written to storage:" },
2758      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2759    };
2760    int i;
2761    for(i=0; i<ArraySize(aTrans); i++){
2762      int n = strlen30(aTrans[i].zPattern);
2763      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2764        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2765        break;
2766      }
2767    }
2768  }
2769  fclose(in);
2770}
2771#endif
2772
2773/*
2774** Display a single line of status using 64-bit values.
2775*/
2776static void displayStatLine(
2777  ShellState *p,            /* The shell context */
2778  char *zLabel,             /* Label for this one line */
2779  char *zFormat,            /* Format for the result */
2780  int iStatusCtrl,          /* Which status to display */
2781  int bReset                /* True to reset the stats */
2782){
2783  sqlite3_int64 iCur = -1;
2784  sqlite3_int64 iHiwtr = -1;
2785  int i, nPercent;
2786  char zLine[200];
2787  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2788  for(i=0, nPercent=0; zFormat[i]; i++){
2789    if( zFormat[i]=='%' ) nPercent++;
2790  }
2791  if( nPercent>1 ){
2792    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2793  }else{
2794    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2795  }
2796  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2797}
2798
2799/*
2800** Display memory stats.
2801*/
2802static int display_stats(
2803  sqlite3 *db,                /* Database to query */
2804  ShellState *pArg,           /* Pointer to ShellState */
2805  int bReset                  /* True to reset the stats */
2806){
2807  int iCur;
2808  int iHiwtr;
2809  FILE *out;
2810  if( pArg==0 || pArg->out==0 ) return 0;
2811  out = pArg->out;
2812
2813  if( pArg->pStmt && pArg->statsOn==2 ){
2814    int nCol, i, x;
2815    sqlite3_stmt *pStmt = pArg->pStmt;
2816    char z[100];
2817    nCol = sqlite3_column_count(pStmt);
2818    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2819    for(i=0; i<nCol; i++){
2820      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2821      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2822#ifndef SQLITE_OMIT_DECLTYPE
2823      sqlite3_snprintf(30, z+x, "declared type:");
2824      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2825#endif
2826#ifdef SQLITE_ENABLE_COLUMN_METADATA
2827      sqlite3_snprintf(30, z+x, "database name:");
2828      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2829      sqlite3_snprintf(30, z+x, "table name:");
2830      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2831      sqlite3_snprintf(30, z+x, "origin name:");
2832      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2833#endif
2834    }
2835  }
2836
2837  if( pArg->statsOn==3 ){
2838    if( pArg->pStmt ){
2839      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2840      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2841    }
2842    return 0;
2843  }
2844
2845  displayStatLine(pArg, "Memory Used:",
2846     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2847  displayStatLine(pArg, "Number of Outstanding Allocations:",
2848     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2849  if( pArg->shellFlgs & SHFLG_Pagecache ){
2850    displayStatLine(pArg, "Number of Pcache Pages Used:",
2851       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2852  }
2853  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2854     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2855  displayStatLine(pArg, "Largest Allocation:",
2856     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2857  displayStatLine(pArg, "Largest Pcache Allocation:",
2858     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2859#ifdef YYTRACKMAXSTACKDEPTH
2860  displayStatLine(pArg, "Deepest Parser Stack:",
2861     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2862#endif
2863
2864  if( db ){
2865    if( pArg->shellFlgs & SHFLG_Lookaside ){
2866      iHiwtr = iCur = -1;
2867      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2868                        &iCur, &iHiwtr, bReset);
2869      raw_printf(pArg->out,
2870              "Lookaside Slots Used:                %d (max %d)\n",
2871              iCur, iHiwtr);
2872      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2873                        &iCur, &iHiwtr, bReset);
2874      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2875              iHiwtr);
2876      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2877                        &iCur, &iHiwtr, bReset);
2878      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2879              iHiwtr);
2880      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2881                        &iCur, &iHiwtr, bReset);
2882      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2883              iHiwtr);
2884    }
2885    iHiwtr = iCur = -1;
2886    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2887    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2888            iCur);
2889    iHiwtr = iCur = -1;
2890    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2891    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2892    iHiwtr = iCur = -1;
2893    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2894    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2895    iHiwtr = iCur = -1;
2896    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2897    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2898    iHiwtr = iCur = -1;
2899    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2900    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2901    iHiwtr = iCur = -1;
2902    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2903    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2904            iCur);
2905    iHiwtr = iCur = -1;
2906    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2907    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2908            iCur);
2909  }
2910
2911  if( pArg->pStmt ){
2912    int iHit, iMiss;
2913    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2914                               bReset);
2915    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2916    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2917    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2918    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2919    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2920    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);
2921    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);
2922    if( iHit || iMiss ){
2923      raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",
2924            iHit, iHit+iMiss);
2925    }
2926    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2927    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2928    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2929    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2930    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2931    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2932    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2933    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2934  }
2935
2936#ifdef __linux__
2937  displayLinuxIoStats(pArg->out);
2938#endif
2939
2940  /* Do not remove this machine readable comment: extra-stats-output-here */
2941
2942  return 0;
2943}
2944
2945/*
2946** Display scan stats.
2947*/
2948static void display_scanstats(
2949  sqlite3 *db,                    /* Database to query */
2950  ShellState *pArg                /* Pointer to ShellState */
2951){
2952#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2953  UNUSED_PARAMETER(db);
2954  UNUSED_PARAMETER(pArg);
2955#else
2956  int i, k, n, mx;
2957  raw_printf(pArg->out, "-------- scanstats --------\n");
2958  mx = 0;
2959  for(k=0; k<=mx; k++){
2960    double rEstLoop = 1.0;
2961    for(i=n=0; 1; i++){
2962      sqlite3_stmt *p = pArg->pStmt;
2963      sqlite3_int64 nLoop, nVisit;
2964      double rEst;
2965      int iSid;
2966      const char *zExplain;
2967      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2968        break;
2969      }
2970      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2971      if( iSid>mx ) mx = iSid;
2972      if( iSid!=k ) continue;
2973      if( n==0 ){
2974        rEstLoop = (double)nLoop;
2975        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2976      }
2977      n++;
2978      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2979      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2980      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2981      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2982      rEstLoop *= rEst;
2983      raw_printf(pArg->out,
2984          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2985          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2986      );
2987    }
2988  }
2989  raw_printf(pArg->out, "---------------------------\n");
2990#endif
2991}
2992
2993/*
2994** Parameter azArray points to a zero-terminated array of strings. zStr
2995** points to a single nul-terminated string. Return non-zero if zStr
2996** is equal, according to strcmp(), to any of the strings in the array.
2997** Otherwise, return zero.
2998*/
2999static int str_in_array(const char *zStr, const char **azArray){
3000  int i;
3001  for(i=0; azArray[i]; i++){
3002    if( 0==strcmp(zStr, azArray[i]) ) return 1;
3003  }
3004  return 0;
3005}
3006
3007/*
3008** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3009** and populate the ShellState.aiIndent[] array with the number of
3010** spaces each opcode should be indented before it is output.
3011**
3012** The indenting rules are:
3013**
3014**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3015**       all opcodes that occur between the p2 jump destination and the opcode
3016**       itself by 2 spaces.
3017**
3018**     * Do the previous for "Return" instructions for when P2 is positive.
3019**       See tag-20220407a in wherecode.c and vdbe.c.
3020**
3021**     * For each "Goto", if the jump destination is earlier in the program
3022**       and ends on one of:
3023**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
3024**       or if the P1 parameter is one instead of zero,
3025**       then indent all opcodes between the earlier instruction
3026**       and "Goto" by 2 spaces.
3027*/
3028static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3029  const char *zSql;               /* The text of the SQL statement */
3030  const char *z;                  /* Used to check if this is an EXPLAIN */
3031  int *abYield = 0;               /* True if op is an OP_Yield */
3032  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
3033  int iOp;                        /* Index of operation in p->aiIndent[] */
3034
3035  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3036                           "Return", 0 };
3037  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3038                            "Rewind", 0 };
3039  const char *azGoto[] = { "Goto", 0 };
3040
3041  /* Try to figure out if this is really an EXPLAIN statement. If this
3042  ** cannot be verified, return early.  */
3043  if( sqlite3_column_count(pSql)!=8 ){
3044    p->cMode = p->mode;
3045    return;
3046  }
3047  zSql = sqlite3_sql(pSql);
3048  if( zSql==0 ) return;
3049  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3050  if( sqlite3_strnicmp(z, "explain", 7) ){
3051    p->cMode = p->mode;
3052    return;
3053  }
3054
3055  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3056    int i;
3057    int iAddr = sqlite3_column_int(pSql, 0);
3058    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3059
3060    /* Set p2 to the P2 field of the current opcode. Then, assuming that
3061    ** p2 is an instruction address, set variable p2op to the index of that
3062    ** instruction in the aiIndent[] array. p2 and p2op may be different if
3063    ** the current instruction is part of a sub-program generated by an
3064    ** SQL trigger or foreign key.  */
3065    int p2 = sqlite3_column_int(pSql, 3);
3066    int p2op = (p2 + (iOp-iAddr));
3067
3068    /* Grow the p->aiIndent array as required */
3069    if( iOp>=nAlloc ){
3070      if( iOp==0 ){
3071        /* Do further verfication that this is explain output.  Abort if
3072        ** it is not */
3073        static const char *explainCols[] = {
3074           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3075        int jj;
3076        for(jj=0; jj<ArraySize(explainCols); jj++){
3077          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3078            p->cMode = p->mode;
3079            sqlite3_reset(pSql);
3080            return;
3081          }
3082        }
3083      }
3084      nAlloc += 100;
3085      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3086      shell_check_oom(p->aiIndent);
3087      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3088      shell_check_oom(abYield);
3089    }
3090    abYield[iOp] = str_in_array(zOp, azYield);
3091    p->aiIndent[iOp] = 0;
3092    p->nIndent = iOp+1;
3093
3094    if( str_in_array(zOp, azNext) && p2op>0 ){
3095      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3096    }
3097    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3098     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3099    ){
3100      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3101    }
3102  }
3103
3104  p->iIndent = 0;
3105  sqlite3_free(abYield);
3106  sqlite3_reset(pSql);
3107}
3108
3109/*
3110** Free the array allocated by explain_data_prepare().
3111*/
3112static void explain_data_delete(ShellState *p){
3113  sqlite3_free(p->aiIndent);
3114  p->aiIndent = 0;
3115  p->nIndent = 0;
3116  p->iIndent = 0;
3117}
3118
3119/*
3120** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3121*/
3122static unsigned int savedSelectTrace;
3123static unsigned int savedWhereTrace;
3124static void disable_debug_trace_modes(void){
3125  unsigned int zero = 0;
3126  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3127  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3128  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3129  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3130}
3131static void restore_debug_trace_modes(void){
3132  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3133  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3134}
3135
3136/* Create the TEMP table used to store parameter bindings */
3137static void bind_table_init(ShellState *p){
3138  int wrSchema = 0;
3139  int defensiveMode = 0;
3140  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3141  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3142  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3143  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3144  sqlite3_exec(p->db,
3145    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3146    "  key TEXT PRIMARY KEY,\n"
3147    "  value\n"
3148    ") WITHOUT ROWID;",
3149    0, 0, 0);
3150  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3151  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3152}
3153
3154/*
3155** Bind parameters on a prepared statement.
3156**
3157** Parameter bindings are taken from a TEMP table of the form:
3158**
3159**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3160**    WITHOUT ROWID;
3161**
3162** No bindings occur if this table does not exist.  The name of the table
3163** begins with "sqlite_" so that it will not collide with ordinary application
3164** tables.  The table must be in the TEMP schema.
3165*/
3166static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3167  int nVar;
3168  int i;
3169  int rc;
3170  sqlite3_stmt *pQ = 0;
3171
3172  nVar = sqlite3_bind_parameter_count(pStmt);
3173  if( nVar==0 ) return;  /* Nothing to do */
3174  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3175                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3176    return; /* Parameter table does not exist */
3177  }
3178  rc = sqlite3_prepare_v2(pArg->db,
3179          "SELECT value FROM temp.sqlite_parameters"
3180          " WHERE key=?1", -1, &pQ, 0);
3181  if( rc || pQ==0 ) return;
3182  for(i=1; i<=nVar; i++){
3183    char zNum[30];
3184    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3185    if( zVar==0 ){
3186      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3187      zVar = zNum;
3188    }
3189    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3190    if( sqlite3_step(pQ)==SQLITE_ROW ){
3191      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3192    }else{
3193      sqlite3_bind_null(pStmt, i);
3194    }
3195    sqlite3_reset(pQ);
3196  }
3197  sqlite3_finalize(pQ);
3198}
3199
3200/*
3201** UTF8 box-drawing characters.  Imagine box lines like this:
3202**
3203**           1
3204**           |
3205**       4 --+-- 2
3206**           |
3207**           3
3208**
3209** Each box characters has between 2 and 4 of the lines leading from
3210** the center.  The characters are here identified by the numbers of
3211** their corresponding lines.
3212*/
3213#define BOX_24   "\342\224\200"  /* U+2500 --- */
3214#define BOX_13   "\342\224\202"  /* U+2502  |  */
3215#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3216#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3217#define BOX_12   "\342\224\224"  /* U+2514  '- */
3218#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3219#define BOX_123  "\342\224\234"  /* U+251c  |- */
3220#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3221#define BOX_234  "\342\224\254"  /* U+252c -,- */
3222#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3223#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3224
3225/* Draw horizontal line N characters long using unicode box
3226** characters
3227*/
3228static void print_box_line(FILE *out, int N){
3229  const char zDash[] =
3230      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3231      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3232  const int nDash = sizeof(zDash) - 1;
3233  N *= 3;
3234  while( N>nDash ){
3235    utf8_printf(out, zDash);
3236    N -= nDash;
3237  }
3238  utf8_printf(out, "%.*s", N, zDash);
3239}
3240
3241/*
3242** Draw a horizontal separator for a MODE_Box table.
3243*/
3244static void print_box_row_separator(
3245  ShellState *p,
3246  int nArg,
3247  const char *zSep1,
3248  const char *zSep2,
3249  const char *zSep3
3250){
3251  int i;
3252  if( nArg>0 ){
3253    utf8_printf(p->out, "%s", zSep1);
3254    print_box_line(p->out, p->actualWidth[0]+2);
3255    for(i=1; i<nArg; i++){
3256      utf8_printf(p->out, "%s", zSep2);
3257      print_box_line(p->out, p->actualWidth[i]+2);
3258    }
3259    utf8_printf(p->out, "%s", zSep3);
3260  }
3261  fputs("\n", p->out);
3262}
3263
3264/*
3265** z[] is a line of text that is to be displayed the .mode box or table or
3266** similar tabular formats.  z[] might contain control characters such
3267** as \n, \t, \f, or \r.
3268**
3269** Compute characters to display on the first line of z[].  Stop at the
3270** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
3271** from malloc()) of that first line, which caller should free sometime.
3272** Write anything to display on the next line into *pzTail.  If this is
3273** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3274*/
3275static char *translateForDisplayAndDup(
3276  const unsigned char *z,            /* Input text to be transformed */
3277  const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
3278  int mxWidth,                       /* Max width.  0 means no limit */
3279  u8 bWordWrap                       /* If true, avoid breaking mid-word */
3280){
3281  int i;                 /* Input bytes consumed */
3282  int j;                 /* Output bytes generated */
3283  int k;                 /* Input bytes to be displayed */
3284  int n;                 /* Output column number */
3285  unsigned char *zOut;   /* Output text */
3286
3287  if( z==0 ){
3288    *pzTail = 0;
3289    return 0;
3290  }
3291  if( mxWidth<0 ) mxWidth = -mxWidth;
3292  if( mxWidth==0 ) mxWidth = 1000000;
3293  i = j = n = 0;
3294  while( n<mxWidth ){
3295    if( z[i]>=' ' ){
3296      n++;
3297      do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
3298      continue;
3299    }
3300    if( z[i]=='\t' ){
3301      do{
3302        n++;
3303        j++;
3304      }while( (n&7)!=0 && n<mxWidth );
3305      i++;
3306      continue;
3307    }
3308    break;
3309  }
3310  if( n>=mxWidth && bWordWrap  ){
3311    /* Perhaps try to back up to a better place to break the line */
3312    for(k=i; k>i/2; k--){
3313      if( isspace(z[k-1]) ) break;
3314    }
3315    if( k<=i/2 ){
3316      for(k=i; k>i/2; k--){
3317        if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3318      }
3319    }
3320    if( k<=i/2 ){
3321      k = i;
3322    }else{
3323      i = k;
3324      while( z[i]==' ' ) i++;
3325    }
3326  }else{
3327    k = i;
3328  }
3329  if( n>=mxWidth && z[i]>=' ' ){
3330   *pzTail = &z[i];
3331  }else if( z[i]=='\r' && z[i+1]=='\n' ){
3332    *pzTail = z[i+2] ? &z[i+2] : 0;
3333  }else if( z[i]==0 || z[i+1]==0 ){
3334    *pzTail = 0;
3335  }else{
3336    *pzTail = &z[i+1];
3337  }
3338  zOut = malloc( j+1 );
3339  shell_check_oom(zOut);
3340  i = j = n = 0;
3341  while( i<k ){
3342    if( z[i]>=' ' ){
3343      n++;
3344      do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
3345      continue;
3346    }
3347    if( z[i]=='\t' ){
3348      do{
3349        n++;
3350        zOut[j++] = ' ';
3351      }while( (n&7)!=0 && n<mxWidth );
3352      i++;
3353      continue;
3354    }
3355    break;
3356  }
3357  zOut[j] = 0;
3358  return (char*)zOut;
3359}
3360
3361/* Extract the value of the i-th current column for pStmt as an SQL literal
3362** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
3363** the caller.
3364*/
3365static char *quoted_column(sqlite3_stmt *pStmt, int i){
3366  switch( sqlite3_column_type(pStmt, i) ){
3367    case SQLITE_NULL: {
3368      return sqlite3_mprintf("NULL");
3369    }
3370    case SQLITE_INTEGER:
3371    case SQLITE_FLOAT: {
3372      return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3373    }
3374    case SQLITE_TEXT: {
3375      return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3376    }
3377    case SQLITE_BLOB: {
3378      int j;
3379      sqlite3_str *pStr = sqlite3_str_new(0);
3380      const unsigned char *a = sqlite3_column_blob(pStmt,i);
3381      int n = sqlite3_column_bytes(pStmt,i);
3382      sqlite3_str_append(pStr, "x'", 2);
3383      for(j=0; j<n; j++){
3384        sqlite3_str_appendf(pStr, "%02x", a[j]);
3385      }
3386      sqlite3_str_append(pStr, "'", 1);
3387      return sqlite3_str_finish(pStr);
3388    }
3389  }
3390  return 0; /* Not reached */
3391}
3392
3393/*
3394** Run a prepared statement and output the result in one of the
3395** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3396** or MODE_Box.
3397**
3398** This is different from ordinary exec_prepared_stmt() in that
3399** it has to run the entire query and gather the results into memory
3400** first, in order to determine column widths, before providing
3401** any output.
3402*/
3403static void exec_prepared_stmt_columnar(
3404  ShellState *p,                        /* Pointer to ShellState */
3405  sqlite3_stmt *pStmt                   /* Statment to run */
3406){
3407  sqlite3_int64 nRow = 0;
3408  int nColumn = 0;
3409  char **azData = 0;
3410  sqlite3_int64 nAlloc = 0;
3411  char *abRowDiv = 0;
3412  const unsigned char *uz;
3413  const char *z;
3414  char **azQuoted = 0;
3415  int rc;
3416  sqlite3_int64 i, nData;
3417  int j, nTotal, w, n;
3418  const char *colSep = 0;
3419  const char *rowSep = 0;
3420  const unsigned char **azNextLine = 0;
3421  int bNextLine = 0;
3422  int bMultiLineRowExists = 0;
3423  int bw = p->cmOpts.bWordWrap;
3424  const char *zEmpty = "";
3425  const char *zShowNull = p->nullValue;
3426
3427  rc = sqlite3_step(pStmt);
3428  if( rc!=SQLITE_ROW ) return;
3429  nColumn = sqlite3_column_count(pStmt);
3430  nAlloc = nColumn*4;
3431  if( nAlloc<=0 ) nAlloc = 1;
3432  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3433  shell_check_oom(azData);
3434  azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
3435  shell_check_oom((void*)azNextLine);
3436  memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
3437  if( p->cmOpts.bQuote ){
3438    azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
3439    shell_check_oom(azQuoted);
3440    memset(azQuoted, 0, nColumn*sizeof(char*) );
3441  }
3442  abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
3443  shell_check_oom(abRowDiv);
3444  if( nColumn>p->nWidth ){
3445    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3446    shell_check_oom(p->colWidth);
3447    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3448    p->nWidth = nColumn;
3449    p->actualWidth = &p->colWidth[nColumn];
3450  }
3451  memset(p->actualWidth, 0, nColumn*sizeof(int));
3452  for(i=0; i<nColumn; i++){
3453    w = p->colWidth[i];
3454    if( w<0 ) w = -w;
3455    p->actualWidth[i] = w;
3456  }
3457  for(i=0; i<nColumn; i++){
3458    const unsigned char *zNotUsed;
3459    int wx = p->colWidth[i];
3460    if( wx==0 ){
3461      wx = p->cmOpts.iWrap;
3462    }
3463    if( wx<0 ) wx = -wx;
3464    uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
3465    azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
3466  }
3467  do{
3468    int useNextLine = bNextLine;
3469    bNextLine = 0;
3470    if( (nRow+2)*nColumn >= nAlloc ){
3471      nAlloc *= 2;
3472      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3473      shell_check_oom(azData);
3474      abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
3475      shell_check_oom(abRowDiv);
3476    }
3477    abRowDiv[nRow] = 1;
3478    nRow++;
3479    for(i=0; i<nColumn; i++){
3480      int wx = p->colWidth[i];
3481      if( wx==0 ){
3482        wx = p->cmOpts.iWrap;
3483      }
3484      if( wx<0 ) wx = -wx;
3485      if( useNextLine ){
3486        uz = azNextLine[i];
3487        if( uz==0 ) uz = (u8*)zEmpty;
3488      }else if( p->cmOpts.bQuote ){
3489        sqlite3_free(azQuoted[i]);
3490        azQuoted[i] = quoted_column(pStmt,i);
3491        uz = (const unsigned char*)azQuoted[i];
3492      }else{
3493        uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
3494        if( uz==0 ) uz = (u8*)zShowNull;
3495      }
3496      azData[nRow*nColumn + i]
3497        = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
3498      if( azNextLine[i] ){
3499        bNextLine = 1;
3500        abRowDiv[nRow-1] = 0;
3501        bMultiLineRowExists = 1;
3502      }
3503    }
3504  }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
3505  nTotal = nColumn*(nRow+1);
3506  for(i=0; i<nTotal; i++){
3507    z = azData[i];
3508    if( z==0 ) z = (char*)zEmpty;
3509    n = strlenChar(z);
3510    j = i%nColumn;
3511    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3512  }
3513  if( seenInterrupt ) goto columnar_end;
3514  if( nColumn==0 ) goto columnar_end;
3515  switch( p->cMode ){
3516    case MODE_Column: {
3517      colSep = "  ";
3518      rowSep = "\n";
3519      if( p->showHeader ){
3520        for(i=0; i<nColumn; i++){
3521          w = p->actualWidth[i];
3522          if( p->colWidth[i]<0 ) w = -w;
3523          utf8_width_print(p->out, w, azData[i]);
3524          fputs(i==nColumn-1?"\n":"  ", p->out);
3525        }
3526        for(i=0; i<nColumn; i++){
3527          print_dashes(p->out, p->actualWidth[i]);
3528          fputs(i==nColumn-1?"\n":"  ", p->out);
3529        }
3530      }
3531      break;
3532    }
3533    case MODE_Table: {
3534      colSep = " | ";
3535      rowSep = " |\n";
3536      print_row_separator(p, nColumn, "+");
3537      fputs("| ", p->out);
3538      for(i=0; i<nColumn; i++){
3539        w = p->actualWidth[i];
3540        n = strlenChar(azData[i]);
3541        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3542        fputs(i==nColumn-1?" |\n":" | ", p->out);
3543      }
3544      print_row_separator(p, nColumn, "+");
3545      break;
3546    }
3547    case MODE_Markdown: {
3548      colSep = " | ";
3549      rowSep = " |\n";
3550      fputs("| ", p->out);
3551      for(i=0; i<nColumn; i++){
3552        w = p->actualWidth[i];
3553        n = strlenChar(azData[i]);
3554        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3555        fputs(i==nColumn-1?" |\n":" | ", p->out);
3556      }
3557      print_row_separator(p, nColumn, "|");
3558      break;
3559    }
3560    case MODE_Box: {
3561      colSep = " " BOX_13 " ";
3562      rowSep = " " BOX_13 "\n";
3563      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3564      utf8_printf(p->out, BOX_13 " ");
3565      for(i=0; i<nColumn; i++){
3566        w = p->actualWidth[i];
3567        n = strlenChar(azData[i]);
3568        utf8_printf(p->out, "%*s%s%*s%s",
3569            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3570            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3571      }
3572      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3573      break;
3574    }
3575  }
3576  for(i=nColumn, j=0; i<nTotal; i++, j++){
3577    if( j==0 && p->cMode!=MODE_Column ){
3578      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3579    }
3580    z = azData[i];
3581    if( z==0 ) z = p->nullValue;
3582    w = p->actualWidth[j];
3583    if( p->colWidth[j]<0 ) w = -w;
3584    utf8_width_print(p->out, w, z);
3585    if( j==nColumn-1 ){
3586      utf8_printf(p->out, "%s", rowSep);
3587      if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
3588        if( p->cMode==MODE_Table ){
3589          print_row_separator(p, nColumn, "+");
3590        }else if( p->cMode==MODE_Box ){
3591          print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3592        }else if( p->cMode==MODE_Column ){
3593          raw_printf(p->out, "\n");
3594        }
3595      }
3596      j = -1;
3597      if( seenInterrupt ) goto columnar_end;
3598    }else{
3599      utf8_printf(p->out, "%s", colSep);
3600    }
3601  }
3602  if( p->cMode==MODE_Table ){
3603    print_row_separator(p, nColumn, "+");
3604  }else if( p->cMode==MODE_Box ){
3605    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3606  }
3607columnar_end:
3608  if( seenInterrupt ){
3609    utf8_printf(p->out, "Interrupt\n");
3610  }
3611  nData = (nRow+1)*nColumn;
3612  for(i=0; i<nData; i++){
3613    z = azData[i];
3614    if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
3615  }
3616  sqlite3_free(azData);
3617  sqlite3_free((void*)azNextLine);
3618  sqlite3_free(abRowDiv);
3619  if( azQuoted ){
3620    for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
3621    sqlite3_free(azQuoted);
3622  }
3623}
3624
3625/*
3626** Run a prepared statement
3627*/
3628static void exec_prepared_stmt(
3629  ShellState *pArg,                                /* Pointer to ShellState */
3630  sqlite3_stmt *pStmt                              /* Statment to run */
3631){
3632  int rc;
3633  sqlite3_uint64 nRow = 0;
3634
3635  if( pArg->cMode==MODE_Column
3636   || pArg->cMode==MODE_Table
3637   || pArg->cMode==MODE_Box
3638   || pArg->cMode==MODE_Markdown
3639  ){
3640    exec_prepared_stmt_columnar(pArg, pStmt);
3641    return;
3642  }
3643
3644  /* perform the first step.  this will tell us if we
3645  ** have a result set or not and how wide it is.
3646  */
3647  rc = sqlite3_step(pStmt);
3648  /* if we have a result set... */
3649  if( SQLITE_ROW == rc ){
3650    /* allocate space for col name ptr, value ptr, and type */
3651    int nCol = sqlite3_column_count(pStmt);
3652    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3653    if( !pData ){
3654      shell_out_of_memory();
3655    }else{
3656      char **azCols = (char **)pData;      /* Names of result columns */
3657      char **azVals = &azCols[nCol];       /* Results */
3658      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3659      int i, x;
3660      assert(sizeof(int) <= sizeof(char *));
3661      /* save off ptrs to column names */
3662      for(i=0; i<nCol; i++){
3663        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3664      }
3665      do{
3666        nRow++;
3667        /* extract the data and data types */
3668        for(i=0; i<nCol; i++){
3669          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3670          if( x==SQLITE_BLOB
3671           && pArg
3672           && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
3673          ){
3674            azVals[i] = "";
3675          }else{
3676            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3677          }
3678          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3679            rc = SQLITE_NOMEM;
3680            break; /* from for */
3681          }
3682        } /* end for */
3683
3684        /* if data and types extracted successfully... */
3685        if( SQLITE_ROW == rc ){
3686          /* call the supplied callback with the result row data */
3687          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3688            rc = SQLITE_ABORT;
3689          }else{
3690            rc = sqlite3_step(pStmt);
3691          }
3692        }
3693      } while( SQLITE_ROW == rc );
3694      sqlite3_free(pData);
3695      if( pArg->cMode==MODE_Json ){
3696        fputs("]\n", pArg->out);
3697      }else if( pArg->cMode==MODE_Count ){
3698        char zBuf[200];
3699        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
3700                         nRow, nRow!=1 ? "s" : "");
3701        printf("%s", zBuf);
3702      }
3703    }
3704  }
3705}
3706
3707#ifndef SQLITE_OMIT_VIRTUALTABLE
3708/*
3709** This function is called to process SQL if the previous shell command
3710** was ".expert". It passes the SQL in the second argument directly to
3711** the sqlite3expert object.
3712**
3713** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3714** code. In this case, (*pzErr) may be set to point to a buffer containing
3715** an English language error message. It is the responsibility of the
3716** caller to eventually free this buffer using sqlite3_free().
3717*/
3718static int expertHandleSQL(
3719  ShellState *pState,
3720  const char *zSql,
3721  char **pzErr
3722){
3723  assert( pState->expert.pExpert );
3724  assert( pzErr==0 || *pzErr==0 );
3725  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3726}
3727
3728/*
3729** This function is called either to silently clean up the object
3730** created by the ".expert" command (if bCancel==1), or to generate a
3731** report from it and then clean it up (if bCancel==0).
3732**
3733** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3734** code. In this case, (*pzErr) may be set to point to a buffer containing
3735** an English language error message. It is the responsibility of the
3736** caller to eventually free this buffer using sqlite3_free().
3737*/
3738static int expertFinish(
3739  ShellState *pState,
3740  int bCancel,
3741  char **pzErr
3742){
3743  int rc = SQLITE_OK;
3744  sqlite3expert *p = pState->expert.pExpert;
3745  assert( p );
3746  assert( bCancel || pzErr==0 || *pzErr==0 );
3747  if( bCancel==0 ){
3748    FILE *out = pState->out;
3749    int bVerbose = pState->expert.bVerbose;
3750
3751    rc = sqlite3_expert_analyze(p, pzErr);
3752    if( rc==SQLITE_OK ){
3753      int nQuery = sqlite3_expert_count(p);
3754      int i;
3755
3756      if( bVerbose ){
3757        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3758        raw_printf(out, "-- Candidates -----------------------------\n");
3759        raw_printf(out, "%s\n", zCand);
3760      }
3761      for(i=0; i<nQuery; i++){
3762        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3763        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3764        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3765        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3766        if( bVerbose ){
3767          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3768          raw_printf(out, "%s\n\n", zSql);
3769        }
3770        raw_printf(out, "%s\n", zIdx);
3771        raw_printf(out, "%s\n", zEQP);
3772      }
3773    }
3774  }
3775  sqlite3_expert_destroy(p);
3776  pState->expert.pExpert = 0;
3777  return rc;
3778}
3779
3780/*
3781** Implementation of ".expert" dot command.
3782*/
3783static int expertDotCommand(
3784  ShellState *pState,             /* Current shell tool state */
3785  char **azArg,                   /* Array of arguments passed to dot command */
3786  int nArg                        /* Number of entries in azArg[] */
3787){
3788  int rc = SQLITE_OK;
3789  char *zErr = 0;
3790  int i;
3791  int iSample = 0;
3792
3793  assert( pState->expert.pExpert==0 );
3794  memset(&pState->expert, 0, sizeof(ExpertInfo));
3795
3796  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3797    char *z = azArg[i];
3798    int n;
3799    if( z[0]=='-' && z[1]=='-' ) z++;
3800    n = strlen30(z);
3801    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3802      pState->expert.bVerbose = 1;
3803    }
3804    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3805      if( i==(nArg-1) ){
3806        raw_printf(stderr, "option requires an argument: %s\n", z);
3807        rc = SQLITE_ERROR;
3808      }else{
3809        iSample = (int)integerValue(azArg[++i]);
3810        if( iSample<0 || iSample>100 ){
3811          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3812          rc = SQLITE_ERROR;
3813        }
3814      }
3815    }
3816    else{
3817      raw_printf(stderr, "unknown option: %s\n", z);
3818      rc = SQLITE_ERROR;
3819    }
3820  }
3821
3822  if( rc==SQLITE_OK ){
3823    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3824    if( pState->expert.pExpert==0 ){
3825      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
3826      rc = SQLITE_ERROR;
3827    }else{
3828      sqlite3_expert_config(
3829          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3830      );
3831    }
3832  }
3833  sqlite3_free(zErr);
3834
3835  return rc;
3836}
3837#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3838
3839/*
3840** Execute a statement or set of statements.  Print
3841** any result rows/columns depending on the current mode
3842** set via the supplied callback.
3843**
3844** This is very similar to SQLite's built-in sqlite3_exec()
3845** function except it takes a slightly different callback
3846** and callback data argument.
3847*/
3848static int shell_exec(
3849  ShellState *pArg,                         /* Pointer to ShellState */
3850  const char *zSql,                         /* SQL to be evaluated */
3851  char **pzErrMsg                           /* Error msg written here */
3852){
3853  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3854  int rc = SQLITE_OK;             /* Return Code */
3855  int rc2;
3856  const char *zLeftover;          /* Tail of unprocessed SQL */
3857  sqlite3 *db = pArg->db;
3858
3859  if( pzErrMsg ){
3860    *pzErrMsg = NULL;
3861  }
3862
3863#ifndef SQLITE_OMIT_VIRTUALTABLE
3864  if( pArg->expert.pExpert ){
3865    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3866    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3867  }
3868#endif
3869
3870  while( zSql[0] && (SQLITE_OK == rc) ){
3871    static const char *zStmtSql;
3872    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3873    if( SQLITE_OK != rc ){
3874      if( pzErrMsg ){
3875        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
3876      }
3877    }else{
3878      if( !pStmt ){
3879        /* this happens for a comment or white-space */
3880        zSql = zLeftover;
3881        while( IsSpace(zSql[0]) ) zSql++;
3882        continue;
3883      }
3884      zStmtSql = sqlite3_sql(pStmt);
3885      if( zStmtSql==0 ) zStmtSql = "";
3886      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3887
3888      /* save off the prepared statment handle and reset row count */
3889      if( pArg ){
3890        pArg->pStmt = pStmt;
3891        pArg->cnt = 0;
3892      }
3893
3894      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3895      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3896        sqlite3_stmt *pExplain;
3897        char *zEQP;
3898        int triggerEQP = 0;
3899        disable_debug_trace_modes();
3900        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3901        if( pArg->autoEQP>=AUTOEQP_trigger ){
3902          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3903        }
3904        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3905        shell_check_oom(zEQP);
3906        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3907        if( rc==SQLITE_OK ){
3908          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3909            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3910            int iEqpId = sqlite3_column_int(pExplain, 0);
3911            int iParentId = sqlite3_column_int(pExplain, 1);
3912            if( zEQPLine==0 ) zEQPLine = "";
3913            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3914            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3915          }
3916          eqp_render(pArg);
3917        }
3918        sqlite3_finalize(pExplain);
3919        sqlite3_free(zEQP);
3920        if( pArg->autoEQP>=AUTOEQP_full ){
3921          /* Also do an EXPLAIN for ".eqp full" mode */
3922          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3923          shell_check_oom(zEQP);
3924          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3925          if( rc==SQLITE_OK ){
3926            pArg->cMode = MODE_Explain;
3927            explain_data_prepare(pArg, pExplain);
3928            exec_prepared_stmt(pArg, pExplain);
3929            explain_data_delete(pArg);
3930          }
3931          sqlite3_finalize(pExplain);
3932          sqlite3_free(zEQP);
3933        }
3934        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3935          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3936          /* Reprepare pStmt before reactiving trace modes */
3937          sqlite3_finalize(pStmt);
3938          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3939          if( pArg ) pArg->pStmt = pStmt;
3940        }
3941        restore_debug_trace_modes();
3942      }
3943
3944      if( pArg ){
3945        pArg->cMode = pArg->mode;
3946        if( pArg->autoExplain ){
3947          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3948            pArg->cMode = MODE_Explain;
3949          }
3950          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3951            pArg->cMode = MODE_EQP;
3952          }
3953        }
3954
3955        /* If the shell is currently in ".explain" mode, gather the extra
3956        ** data required to add indents to the output.*/
3957        if( pArg->cMode==MODE_Explain ){
3958          explain_data_prepare(pArg, pStmt);
3959        }
3960      }
3961
3962      bind_prepared_stmt(pArg, pStmt);
3963      exec_prepared_stmt(pArg, pStmt);
3964      explain_data_delete(pArg);
3965      eqp_render(pArg);
3966
3967      /* print usage stats if stats on */
3968      if( pArg && pArg->statsOn ){
3969        display_stats(db, pArg, 0);
3970      }
3971
3972      /* print loop-counters if required */
3973      if( pArg && pArg->scanstatsOn ){
3974        display_scanstats(db, pArg);
3975      }
3976
3977      /* Finalize the statement just executed. If this fails, save a
3978      ** copy of the error message. Otherwise, set zSql to point to the
3979      ** next statement to execute. */
3980      rc2 = sqlite3_finalize(pStmt);
3981      if( rc!=SQLITE_NOMEM ) rc = rc2;
3982      if( rc==SQLITE_OK ){
3983        zSql = zLeftover;
3984        while( IsSpace(zSql[0]) ) zSql++;
3985      }else if( pzErrMsg ){
3986        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
3987      }
3988
3989      /* clear saved stmt handle */
3990      if( pArg ){
3991        pArg->pStmt = NULL;
3992      }
3993    }
3994  } /* end while */
3995
3996  return rc;
3997}
3998
3999/*
4000** Release memory previously allocated by tableColumnList().
4001*/
4002static void freeColumnList(char **azCol){
4003  int i;
4004  for(i=1; azCol[i]; i++){
4005    sqlite3_free(azCol[i]);
4006  }
4007  /* azCol[0] is a static string */
4008  sqlite3_free(azCol);
4009}
4010
4011/*
4012** Return a list of pointers to strings which are the names of all
4013** columns in table zTab.   The memory to hold the names is dynamically
4014** allocated and must be released by the caller using a subsequent call
4015** to freeColumnList().
4016**
4017** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
4018** value that needs to be preserved, then azCol[0] is filled in with the
4019** name of the rowid column.
4020**
4021** The first regular column in the table is azCol[1].  The list is terminated
4022** by an entry with azCol[i]==0.
4023*/
4024static char **tableColumnList(ShellState *p, const char *zTab){
4025  char **azCol = 0;
4026  sqlite3_stmt *pStmt;
4027  char *zSql;
4028  int nCol = 0;
4029  int nAlloc = 0;
4030  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
4031  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
4032  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4033  int rc;
4034
4035  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4036  shell_check_oom(zSql);
4037  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4038  sqlite3_free(zSql);
4039  if( rc ) return 0;
4040  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4041    if( nCol>=nAlloc-2 ){
4042      nAlloc = nAlloc*2 + nCol + 10;
4043      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4044      shell_check_oom(azCol);
4045    }
4046    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4047    shell_check_oom(azCol[nCol]);
4048    if( sqlite3_column_int(pStmt, 5) ){
4049      nPK++;
4050      if( nPK==1
4051       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4052                          "INTEGER")==0
4053      ){
4054        isIPK = 1;
4055      }else{
4056        isIPK = 0;
4057      }
4058    }
4059  }
4060  sqlite3_finalize(pStmt);
4061  if( azCol==0 ) return 0;
4062  azCol[0] = 0;
4063  azCol[nCol+1] = 0;
4064
4065  /* The decision of whether or not a rowid really needs to be preserved
4066  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
4067  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
4068  ** rowids on tables where the rowid is inaccessible because there are other
4069  ** columns in the table named "rowid", "_rowid_", and "oid".
4070  */
4071  if( preserveRowid && isIPK ){
4072    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4073    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
4074    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4075    ** ROWID aliases.  To distinguish these cases, check to see if
4076    ** there is a "pk" entry in "PRAGMA index_list".  There will be
4077    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4078    */
4079    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4080                           " WHERE origin='pk'", zTab);
4081    shell_check_oom(zSql);
4082    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4083    sqlite3_free(zSql);
4084    if( rc ){
4085      freeColumnList(azCol);
4086      return 0;
4087    }
4088    rc = sqlite3_step(pStmt);
4089    sqlite3_finalize(pStmt);
4090    preserveRowid = rc==SQLITE_ROW;
4091  }
4092  if( preserveRowid ){
4093    /* Only preserve the rowid if we can find a name to use for the
4094    ** rowid */
4095    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4096    int i, j;
4097    for(j=0; j<3; j++){
4098      for(i=1; i<=nCol; i++){
4099        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4100      }
4101      if( i>nCol ){
4102        /* At this point, we know that azRowid[j] is not the name of any
4103        ** ordinary column in the table.  Verify that azRowid[j] is a valid
4104        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
4105        ** tables will fail this last check */
4106        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4107        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4108        break;
4109      }
4110    }
4111  }
4112  return azCol;
4113}
4114
4115/*
4116** Toggle the reverse_unordered_selects setting.
4117*/
4118static void toggleSelectOrder(sqlite3 *db){
4119  sqlite3_stmt *pStmt = 0;
4120  int iSetting = 0;
4121  char zStmt[100];
4122  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4123  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4124    iSetting = sqlite3_column_int(pStmt, 0);
4125  }
4126  sqlite3_finalize(pStmt);
4127  sqlite3_snprintf(sizeof(zStmt), zStmt,
4128       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4129  sqlite3_exec(db, zStmt, 0, 0, 0);
4130}
4131
4132/*
4133** This is a different callback routine used for dumping the database.
4134** Each row received by this callback consists of a table name,
4135** the table type ("index" or "table") and SQL to create the table.
4136** This routine should print text sufficient to recreate the table.
4137*/
4138static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4139  int rc;
4140  const char *zTable;
4141  const char *zType;
4142  const char *zSql;
4143  ShellState *p = (ShellState *)pArg;
4144  int dataOnly;
4145  int noSys;
4146
4147  UNUSED_PARAMETER(azNotUsed);
4148  if( nArg!=3 || azArg==0 ) return 0;
4149  zTable = azArg[0];
4150  zType = azArg[1];
4151  zSql = azArg[2];
4152  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4153  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4154
4155  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4156    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4157  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4158    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4159  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
4160    return 0;
4161  }else if( dataOnly ){
4162    /* no-op */
4163  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4164    char *zIns;
4165    if( !p->writableSchema ){
4166      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4167      p->writableSchema = 1;
4168    }
4169    zIns = sqlite3_mprintf(
4170       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4171       "VALUES('table','%q','%q',0,'%q');",
4172       zTable, zTable, zSql);
4173    shell_check_oom(zIns);
4174    utf8_printf(p->out, "%s\n", zIns);
4175    sqlite3_free(zIns);
4176    return 0;
4177  }else{
4178    printSchemaLine(p->out, zSql, ";\n");
4179  }
4180
4181  if( strcmp(zType, "table")==0 ){
4182    ShellText sSelect;
4183    ShellText sTable;
4184    char **azCol;
4185    int i;
4186    char *savedDestTable;
4187    int savedMode;
4188
4189    azCol = tableColumnList(p, zTable);
4190    if( azCol==0 ){
4191      p->nErr++;
4192      return 0;
4193    }
4194
4195    /* Always quote the table name, even if it appears to be pure ascii,
4196    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
4197    initText(&sTable);
4198    appendText(&sTable, zTable, quoteChar(zTable));
4199    /* If preserving the rowid, add a column list after the table name.
4200    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4201    ** instead of the usual "INSERT INTO tab VALUES(...)".
4202    */
4203    if( azCol[0] ){
4204      appendText(&sTable, "(", 0);
4205      appendText(&sTable, azCol[0], 0);
4206      for(i=1; azCol[i]; i++){
4207        appendText(&sTable, ",", 0);
4208        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4209      }
4210      appendText(&sTable, ")", 0);
4211    }
4212
4213    /* Build an appropriate SELECT statement */
4214    initText(&sSelect);
4215    appendText(&sSelect, "SELECT ", 0);
4216    if( azCol[0] ){
4217      appendText(&sSelect, azCol[0], 0);
4218      appendText(&sSelect, ",", 0);
4219    }
4220    for(i=1; azCol[i]; i++){
4221      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4222      if( azCol[i+1] ){
4223        appendText(&sSelect, ",", 0);
4224      }
4225    }
4226    freeColumnList(azCol);
4227    appendText(&sSelect, " FROM ", 0);
4228    appendText(&sSelect, zTable, quoteChar(zTable));
4229
4230    savedDestTable = p->zDestTable;
4231    savedMode = p->mode;
4232    p->zDestTable = sTable.z;
4233    p->mode = p->cMode = MODE_Insert;
4234    rc = shell_exec(p, sSelect.z, 0);
4235    if( (rc&0xff)==SQLITE_CORRUPT ){
4236      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4237      toggleSelectOrder(p->db);
4238      shell_exec(p, sSelect.z, 0);
4239      toggleSelectOrder(p->db);
4240    }
4241    p->zDestTable = savedDestTable;
4242    p->mode = savedMode;
4243    freeText(&sTable);
4244    freeText(&sSelect);
4245    if( rc ) p->nErr++;
4246  }
4247  return 0;
4248}
4249
4250/*
4251** Run zQuery.  Use dump_callback() as the callback routine so that
4252** the contents of the query are output as SQL statements.
4253**
4254** If we get a SQLITE_CORRUPT error, rerun the query after appending
4255** "ORDER BY rowid DESC" to the end.
4256*/
4257static int run_schema_dump_query(
4258  ShellState *p,
4259  const char *zQuery
4260){
4261  int rc;
4262  char *zErr = 0;
4263  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4264  if( rc==SQLITE_CORRUPT ){
4265    char *zQ2;
4266    int len = strlen30(zQuery);
4267    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4268    if( zErr ){
4269      utf8_printf(p->out, "/****** %s ******/\n", zErr);
4270      sqlite3_free(zErr);
4271      zErr = 0;
4272    }
4273    zQ2 = malloc( len+100 );
4274    if( zQ2==0 ) return rc;
4275    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4276    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4277    if( rc ){
4278      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4279    }else{
4280      rc = SQLITE_CORRUPT;
4281    }
4282    sqlite3_free(zErr);
4283    free(zQ2);
4284  }
4285  return rc;
4286}
4287
4288/*
4289** Text of help messages.
4290**
4291** The help text for each individual command begins with a line that starts
4292** with ".".  Subsequent lines are supplemental information.
4293**
4294** There must be two or more spaces between the end of the command and the
4295** start of the description of what that command does.
4296*/
4297static const char *(azHelp[]) = {
4298#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4299  && !defined(SQLITE_SHELL_FIDDLE)
4300  ".archive ...             Manage SQL archives",
4301  "   Each command must have exactly one of the following options:",
4302  "     -c, --create               Create a new archive",
4303  "     -u, --update               Add or update files with changed mtime",
4304  "     -i, --insert               Like -u but always add even if unchanged",
4305  "     -r, --remove               Remove files from archive",
4306  "     -t, --list                 List contents of archive",
4307  "     -x, --extract              Extract files from archive",
4308  "   Optional arguments:",
4309  "     -v, --verbose              Print each filename as it is processed",
4310  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
4311  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
4312  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
4313  "     -g, --glob                 Use glob matching for names in archive",
4314  "     -n, --dryrun               Show the SQL that would have occurred",
4315  "   Examples:",
4316  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
4317  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
4318  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
4319  "   See also:",
4320  "      http://sqlite.org/cli.html#sqlite_archive_support",
4321#endif
4322#ifndef SQLITE_OMIT_AUTHORIZATION
4323  ".auth ON|OFF             Show authorizer callbacks",
4324#endif
4325#ifndef SQLITE_SHELL_FIDDLE
4326  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
4327  "   Options:",
4328  "       --append            Use the appendvfs",
4329  "       --async             Write to FILE without journal and fsync()",
4330#endif
4331  ".bail on|off             Stop after hitting an error.  Default OFF",
4332  ".binary on|off           Turn binary output on or off.  Default OFF",
4333#ifndef SQLITE_SHELL_FIDDLE
4334  ".cd DIRECTORY            Change the working directory to DIRECTORY",
4335#endif
4336  ".changes on|off          Show number of rows changed by SQL",
4337#ifndef SQLITE_SHELL_FIDDLE
4338  ".check GLOB              Fail if output since .testcase does not match",
4339  ".clone NEWDB             Clone data into NEWDB from the existing database",
4340#endif
4341  ".connection [close] [#]  Open or close an auxiliary database connection",
4342  ".databases               List names and files of attached databases",
4343  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4344#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4345  ".dbinfo ?DB?             Show status information about the database",
4346#endif
4347  ".dump ?OBJECTS?          Render database content as SQL",
4348  "   Options:",
4349  "     --data-only            Output only INSERT statements",
4350  "     --newlines             Allow unescaped newline characters in output",
4351  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4352  "     --preserve-rowids      Include ROWID values in the output",
4353  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4354  "   Additional LIKE patterns can be given in subsequent arguments",
4355  ".echo on|off             Turn command echo on or off",
4356  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4357  "   Other Modes:",
4358#ifdef SQLITE_DEBUG
4359  "      test                  Show raw EXPLAIN QUERY PLAN output",
4360  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4361#endif
4362  "      trigger               Like \"full\" but also show trigger bytecode",
4363#ifndef SQLITE_SHELL_FIDDLE
4364  ".excel                   Display the output of next command in spreadsheet",
4365  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4366#endif
4367#ifndef SQLITE_SHELL_FIDDLE
4368  ".exit ?CODE?             Exit this program with return-code CODE",
4369#endif
4370  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4371  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4372  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4373  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4374  "   --help                  Show CMD details",
4375  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4376  ".headers on|off          Turn display of headers on or off",
4377  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4378#ifndef SQLITE_SHELL_FIDDLE
4379  ".import FILE TABLE       Import data from FILE into TABLE",
4380  "   Options:",
4381  "     --ascii               Use \\037 and \\036 as column and row separators",
4382  "     --csv                 Use , and \\n as column and row separators",
4383  "     --skip N              Skip the first N rows of input",
4384  "     --schema S            Target table to be S.TABLE",
4385  "     -v                    \"Verbose\" - increase auxiliary output",
4386  "   Notes:",
4387  "     *  If TABLE does not exist, it is created.  The first row of input",
4388  "        determines the column names.",
4389  "     *  If neither --csv or --ascii are used, the input mode is derived",
4390  "        from the \".mode\" output mode",
4391  "     *  If FILE begins with \"|\" then it is a command that generates the",
4392  "        input text.",
4393#endif
4394#ifndef SQLITE_OMIT_TEST_CONTROL
4395  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4396#endif
4397  ".indexes ?TABLE?         Show names of indexes",
4398  "                           If TABLE is specified, only show indexes for",
4399  "                           tables matching TABLE using the LIKE operator.",
4400#ifdef SQLITE_ENABLE_IOTRACE
4401  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4402#endif
4403  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4404  ".lint OPTIONS            Report potential schema issues.",
4405  "     Options:",
4406  "        fkey-indexes     Find missing foreign key indexes",
4407#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4408  ".load FILE ?ENTRY?       Load an extension library",
4409#endif
4410#ifndef SQLITE_SHELL_FIDDLE
4411  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4412#endif
4413  ".mode MODE ?OPTIONS?     Set output mode",
4414  "   MODE is one of:",
4415  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
4416  "     box         Tables using unicode box-drawing characters",
4417  "     csv         Comma-separated values",
4418  "     column      Output in columns.  (See .width)",
4419  "     html        HTML <table> code",
4420  "     insert      SQL insert statements for TABLE",
4421  "     json        Results in a JSON array",
4422  "     line        One value per line",
4423  "     list        Values delimited by \"|\"",
4424  "     markdown    Markdown table format",
4425  "     qbox        Shorthand for \"box --width 60 --quote\"",
4426  "     quote       Escape answers as for SQL",
4427  "     table       ASCII-art table",
4428  "     tabs        Tab-separated values",
4429  "     tcl         TCL list elements",
4430  "   OPTIONS: (for columnar modes or insert mode):",
4431  "     --wrap N       Wrap output lines to no longer than N characters",
4432  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
4433  "     --ww           Shorthand for \"--wordwrap 1\"",
4434  "     --quote        Quote output text as SQL literals",
4435  "     --noquote      Do not quote output text",
4436  "     TABLE          The name of SQL table used for \"insert\" mode",
4437#ifndef SQLITE_SHELL_FIDDLE
4438  ".nonce STRING            Suspend safe mode for one command if nonce matches",
4439#endif
4440  ".nullvalue STRING        Use STRING in place of NULL values",
4441#ifndef SQLITE_SHELL_FIDDLE
4442  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4443  "     If FILE begins with '|' then open as a pipe",
4444  "       --bom  Put a UTF8 byte-order mark at the beginning",
4445  "       -e     Send output to the system text editor",
4446  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4447  /* Note that .open is (partially) available in WASM builds but is
4448  ** currently only intended to be used by the fiddle tool, not
4449  ** end users, so is "undocumented." */
4450  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4451  "     Options:",
4452  "        --append        Use appendvfs to append database to the end of FILE",
4453#endif
4454#ifndef SQLITE_OMIT_DESERIALIZE
4455  "        --deserialize   Load into memory using sqlite3_deserialize()",
4456  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4457  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4458#endif
4459  "        --new           Initialize FILE to an empty database",
4460  "        --nofollow      Do not follow symbolic links",
4461  "        --readonly      Open FILE readonly",
4462  "        --zip           FILE is a ZIP archive",
4463#ifndef SQLITE_SHELL_FIDDLE
4464  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4465  "   If FILE begins with '|' then open it as a pipe.",
4466  "   Options:",
4467  "     --bom                 Prefix output with a UTF8 byte-order mark",
4468  "     -e                    Send output to the system text editor",
4469  "     -x                    Send output as CSV to a spreadsheet",
4470#endif
4471  ".parameter CMD ...       Manage SQL parameter bindings",
4472  "   clear                   Erase all bindings",
4473  "   init                    Initialize the TEMP table that holds bindings",
4474  "   list                    List the current parameter bindings",
4475  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4476  "                           PARAMETER should start with one of: $ : @ ?",
4477  "   unset PARAMETER         Remove PARAMETER from the binding table",
4478  ".print STRING...         Print literal STRING",
4479#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4480  ".progress N              Invoke progress handler after every N opcodes",
4481  "   --limit N                 Interrupt after N progress callbacks",
4482  "   --once                    Do no more than one progress interrupt",
4483  "   --quiet|-q                No output except at interrupts",
4484  "   --reset                   Reset the count for each input and interrupt",
4485#endif
4486  ".prompt MAIN CONTINUE    Replace the standard prompts",
4487#ifndef SQLITE_SHELL_FIDDLE
4488  ".quit                    Exit this program",
4489  ".read FILE               Read input from FILE or command output",
4490  "    If FILE begins with \"|\", it is a command that generates the input.",
4491#endif
4492#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4493  ".recover                 Recover as much data as possible from corrupt db.",
4494  "   --freelist-corrupt       Assume the freelist is corrupt",
4495  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4496  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4497  "   --no-rowids              Do not attempt to recover rowid values",
4498  "                            that are not also INTEGER PRIMARY KEYs",
4499#endif
4500#ifndef SQLITE_SHELL_FIDDLE
4501  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4502  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
4503#endif
4504  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4505  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4506  "   Options:",
4507  "      --indent             Try to pretty-print the schema",
4508  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4509  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4510  "    Options:",
4511  "       --init               Create a new SELFTEST table",
4512  "       -v                   Verbose output",
4513  ".separator COL ?ROW?     Change the column and row separators",
4514#if defined(SQLITE_ENABLE_SESSION)
4515  ".session ?NAME? CMD ...  Create or control sessions",
4516  "   Subcommands:",
4517  "     attach TABLE             Attach TABLE",
4518  "     changeset FILE           Write a changeset into FILE",
4519  "     close                    Close one session",
4520  "     enable ?BOOLEAN?         Set or query the enable bit",
4521  "     filter GLOB...           Reject tables matching GLOBs",
4522  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4523  "     isempty                  Query whether the session is empty",
4524  "     list                     List currently open session names",
4525  "     open DB NAME             Open a new session on DB",
4526  "     patchset FILE            Write a patchset into FILE",
4527  "   If ?NAME? is omitted, the first defined session is used.",
4528#endif
4529  ".sha3sum ...             Compute a SHA3 hash of database content",
4530  "    Options:",
4531  "      --schema              Also hash the sqlite_schema table",
4532  "      --sha3-224            Use the sha3-224 algorithm",
4533  "      --sha3-256            Use the sha3-256 algorithm (default)",
4534  "      --sha3-384            Use the sha3-384 algorithm",
4535  "      --sha3-512            Use the sha3-512 algorithm",
4536  "    Any other argument is a LIKE pattern for tables to hash",
4537#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4538  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4539#endif
4540  ".show                    Show the current values for various settings",
4541  ".stats ?ARG?             Show stats or turn stats on or off",
4542  "   off                      Turn off automatic stat display",
4543  "   on                       Turn on automatic stat display",
4544  "   stmt                     Show statement stats",
4545  "   vmstep                   Show the virtual machine step count only",
4546#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4547  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4548#endif
4549  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4550#ifndef SQLITE_SHELL_FIDDLE
4551  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4552#endif
4553  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4554  "                           Run \".testctrl\" with no arguments for details",
4555  ".timeout MS              Try opening locked tables for MS milliseconds",
4556  ".timer on|off            Turn SQL timer on or off",
4557#ifndef SQLITE_OMIT_TRACE
4558  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4559  "    FILE                    Send output to FILE",
4560  "    stdout                  Send output to stdout",
4561  "    stderr                  Send output to stderr",
4562  "    off                     Disable tracing",
4563  "    --expanded              Expand query parameters",
4564#ifdef SQLITE_ENABLE_NORMALIZE
4565  "    --normalized            Normal the SQL statements",
4566#endif
4567  "    --plain                 Show SQL as it is input",
4568  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4569  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4570  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4571  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4572#endif /* SQLITE_OMIT_TRACE */
4573#ifdef SQLITE_DEBUG
4574  ".unmodule NAME ...       Unregister virtual table modules",
4575  "    --allexcept             Unregister everything except those named",
4576#endif
4577  ".vfsinfo ?AUX?           Information about the top-level VFS",
4578  ".vfslist                 List all available VFSes",
4579  ".vfsname ?AUX?           Print the name of the VFS stack",
4580  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4581  "     Negative values right-justify",
4582};
4583
4584/*
4585** Output help text.
4586**
4587** zPattern describes the set of commands for which help text is provided.
4588** If zPattern is NULL, then show all commands, but only give a one-line
4589** description of each.
4590**
4591** Return the number of matches.
4592*/
4593static int showHelp(FILE *out, const char *zPattern){
4594  int i = 0;
4595  int j = 0;
4596  int n = 0;
4597  char *zPat;
4598  if( zPattern==0
4599   || zPattern[0]=='0'
4600   || strcmp(zPattern,"-a")==0
4601   || strcmp(zPattern,"-all")==0
4602   || strcmp(zPattern,"--all")==0
4603  ){
4604    /* Show all commands, but only one line per command */
4605    if( zPattern==0 ) zPattern = "";
4606    for(i=0; i<ArraySize(azHelp); i++){
4607      if( azHelp[i][0]=='.' || zPattern[0] ){
4608        utf8_printf(out, "%s\n", azHelp[i]);
4609        n++;
4610      }
4611    }
4612  }else{
4613    /* Look for commands that for which zPattern is an exact prefix */
4614    zPat = sqlite3_mprintf(".%s*", zPattern);
4615    shell_check_oom(zPat);
4616    for(i=0; i<ArraySize(azHelp); i++){
4617      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4618        utf8_printf(out, "%s\n", azHelp[i]);
4619        j = i+1;
4620        n++;
4621      }
4622    }
4623    sqlite3_free(zPat);
4624    if( n ){
4625      if( n==1 ){
4626        /* when zPattern is a prefix of exactly one command, then include the
4627        ** details of that command, which should begin at offset j */
4628        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4629          utf8_printf(out, "%s\n", azHelp[j]);
4630          j++;
4631        }
4632      }
4633      return n;
4634    }
4635    /* Look for commands that contain zPattern anywhere.  Show the complete
4636    ** text of all commands that match. */
4637    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4638    shell_check_oom(zPat);
4639    for(i=0; i<ArraySize(azHelp); i++){
4640      if( azHelp[i][0]=='.' ) j = i;
4641      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4642        utf8_printf(out, "%s\n", azHelp[j]);
4643        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4644          j++;
4645          utf8_printf(out, "%s\n", azHelp[j]);
4646        }
4647        i = j;
4648        n++;
4649      }
4650    }
4651    sqlite3_free(zPat);
4652  }
4653  return n;
4654}
4655
4656/* Forward reference */
4657static int process_input(ShellState *p);
4658
4659/*
4660** Read the content of file zName into memory obtained from sqlite3_malloc64()
4661** and return a pointer to the buffer. The caller is responsible for freeing
4662** the memory.
4663**
4664** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4665** read.
4666**
4667** For convenience, a nul-terminator byte is always appended to the data read
4668** from the file before the buffer is returned. This byte is not included in
4669** the final value of (*pnByte), if applicable.
4670**
4671** NULL is returned if any error is encountered. The final value of *pnByte
4672** is undefined in this case.
4673*/
4674static char *readFile(const char *zName, int *pnByte){
4675  FILE *in = fopen(zName, "rb");
4676  long nIn;
4677  size_t nRead;
4678  char *pBuf;
4679  if( in==0 ) return 0;
4680  fseek(in, 0, SEEK_END);
4681  nIn = ftell(in);
4682  rewind(in);
4683  pBuf = sqlite3_malloc64( nIn+1 );
4684  if( pBuf==0 ){ fclose(in); return 0; }
4685  nRead = fread(pBuf, nIn, 1, in);
4686  fclose(in);
4687  if( nRead!=1 ){
4688    sqlite3_free(pBuf);
4689    return 0;
4690  }
4691  pBuf[nIn] = 0;
4692  if( pnByte ) *pnByte = nIn;
4693  return pBuf;
4694}
4695
4696#if defined(SQLITE_ENABLE_SESSION)
4697/*
4698** Close a single OpenSession object and release all of its associated
4699** resources.
4700*/
4701static void session_close(OpenSession *pSession){
4702  int i;
4703  sqlite3session_delete(pSession->p);
4704  sqlite3_free(pSession->zName);
4705  for(i=0; i<pSession->nFilter; i++){
4706    sqlite3_free(pSession->azFilter[i]);
4707  }
4708  sqlite3_free(pSession->azFilter);
4709  memset(pSession, 0, sizeof(OpenSession));
4710}
4711#endif
4712
4713/*
4714** Close all OpenSession objects and release all associated resources.
4715*/
4716#if defined(SQLITE_ENABLE_SESSION)
4717static void session_close_all(ShellState *p, int i){
4718  int j;
4719  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4720  for(j=0; j<pAuxDb->nSession; j++){
4721    session_close(&pAuxDb->aSession[j]);
4722  }
4723  pAuxDb->nSession = 0;
4724}
4725#else
4726# define session_close_all(X,Y)
4727#endif
4728
4729/*
4730** Implementation of the xFilter function for an open session.  Omit
4731** any tables named by ".session filter" but let all other table through.
4732*/
4733#if defined(SQLITE_ENABLE_SESSION)
4734static int session_filter(void *pCtx, const char *zTab){
4735  OpenSession *pSession = (OpenSession*)pCtx;
4736  int i;
4737  for(i=0; i<pSession->nFilter; i++){
4738    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4739  }
4740  return 1;
4741}
4742#endif
4743
4744/*
4745** Try to deduce the type of file for zName based on its content.  Return
4746** one of the SHELL_OPEN_* constants.
4747**
4748** If the file does not exist or is empty but its name looks like a ZIP
4749** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4750** Otherwise, assume an ordinary database regardless of the filename if
4751** the type cannot be determined from content.
4752*/
4753int deduceDatabaseType(const char *zName, int dfltZip){
4754  FILE *f = fopen(zName, "rb");
4755  size_t n;
4756  int rc = SHELL_OPEN_UNSPEC;
4757  char zBuf[100];
4758  if( f==0 ){
4759    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4760       return SHELL_OPEN_ZIPFILE;
4761    }else{
4762       return SHELL_OPEN_NORMAL;
4763    }
4764  }
4765  n = fread(zBuf, 16, 1, f);
4766  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4767    fclose(f);
4768    return SHELL_OPEN_NORMAL;
4769  }
4770  fseek(f, -25, SEEK_END);
4771  n = fread(zBuf, 25, 1, f);
4772  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4773    rc = SHELL_OPEN_APPENDVFS;
4774  }else{
4775    fseek(f, -22, SEEK_END);
4776    n = fread(zBuf, 22, 1, f);
4777    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4778       && zBuf[3]==0x06 ){
4779      rc = SHELL_OPEN_ZIPFILE;
4780    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4781      rc = SHELL_OPEN_ZIPFILE;
4782    }
4783  }
4784  fclose(f);
4785  return rc;
4786}
4787
4788#ifndef SQLITE_OMIT_DESERIALIZE
4789/*
4790** Reconstruct an in-memory database using the output from the "dbtotxt"
4791** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4792** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4793*/
4794static unsigned char *readHexDb(ShellState *p, int *pnData){
4795  unsigned char *a = 0;
4796  int nLine;
4797  int n = 0;
4798  int pgsz = 0;
4799  int iOffset = 0;
4800  int j, k;
4801  int rc;
4802  FILE *in;
4803  const char *zDbFilename = p->pAuxDb->zDbFilename;
4804  unsigned int x[16];
4805  char zLine[1000];
4806  if( zDbFilename ){
4807    in = fopen(zDbFilename, "r");
4808    if( in==0 ){
4809      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4810      return 0;
4811    }
4812    nLine = 0;
4813  }else{
4814    in = p->in;
4815    nLine = p->lineno;
4816    if( in==0 ) in = stdin;
4817  }
4818  *pnData = 0;
4819  nLine++;
4820  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4821  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4822  if( rc!=2 ) goto readHexDb_error;
4823  if( n<0 ) goto readHexDb_error;
4824  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4825  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4826  a = sqlite3_malloc( n ? n : 1 );
4827  shell_check_oom(a);
4828  memset(a, 0, n);
4829  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4830    utf8_printf(stderr, "invalid pagesize\n");
4831    goto readHexDb_error;
4832  }
4833  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4834    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4835    if( rc==2 ){
4836      iOffset = k;
4837      continue;
4838    }
4839    if( strncmp(zLine, "| end ", 6)==0 ){
4840      break;
4841    }
4842    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4843                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4844                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4845    if( rc==17 ){
4846      k = iOffset+j;
4847      if( k+16<=n && k>=0 ){
4848        int ii;
4849        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4850      }
4851    }
4852  }
4853  *pnData = n;
4854  if( in!=p->in ){
4855    fclose(in);
4856  }else{
4857    p->lineno = nLine;
4858  }
4859  return a;
4860
4861readHexDb_error:
4862  if( in!=p->in ){
4863    fclose(in);
4864  }else{
4865    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4866      nLine++;
4867      if(strncmp(zLine, "| end ", 6)==0 ) break;
4868    }
4869    p->lineno = nLine;
4870  }
4871  sqlite3_free(a);
4872  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4873  return 0;
4874}
4875#endif /* SQLITE_OMIT_DESERIALIZE */
4876
4877/*
4878** Scalar function "shell_int32". The first argument to this function
4879** must be a blob. The second a non-negative integer. This function
4880** reads and returns a 32-bit big-endian integer from byte
4881** offset (4*<arg2>) of the blob.
4882*/
4883static void shellInt32(
4884  sqlite3_context *context,
4885  int argc,
4886  sqlite3_value **argv
4887){
4888  const unsigned char *pBlob;
4889  int nBlob;
4890  int iInt;
4891
4892  UNUSED_PARAMETER(argc);
4893  nBlob = sqlite3_value_bytes(argv[0]);
4894  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4895  iInt = sqlite3_value_int(argv[1]);
4896
4897  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4898    const unsigned char *a = &pBlob[iInt*4];
4899    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4900                       + ((sqlite3_int64)a[1]<<16)
4901                       + ((sqlite3_int64)a[2]<< 8)
4902                       + ((sqlite3_int64)a[3]<< 0);
4903    sqlite3_result_int64(context, iVal);
4904  }
4905}
4906
4907/*
4908** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4909** using "..." with internal double-quote characters doubled.
4910*/
4911static void shellIdQuote(
4912  sqlite3_context *context,
4913  int argc,
4914  sqlite3_value **argv
4915){
4916  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4917  UNUSED_PARAMETER(argc);
4918  if( zName ){
4919    char *z = sqlite3_mprintf("\"%w\"", zName);
4920    sqlite3_result_text(context, z, -1, sqlite3_free);
4921  }
4922}
4923
4924/*
4925** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4926*/
4927static void shellUSleepFunc(
4928  sqlite3_context *context,
4929  int argcUnused,
4930  sqlite3_value **argv
4931){
4932  int sleep = sqlite3_value_int(argv[0]);
4933  (void)argcUnused;
4934  sqlite3_sleep(sleep/1000);
4935  sqlite3_result_int(context, sleep);
4936}
4937
4938/*
4939** Scalar function "shell_escape_crnl" used by the .recover command.
4940** The argument passed to this function is the output of built-in
4941** function quote(). If the first character of the input is "'",
4942** indicating that the value passed to quote() was a text value,
4943** then this function searches the input for "\n" and "\r" characters
4944** and adds a wrapper similar to the following:
4945**
4946**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4947**
4948** Or, if the first character of the input is not "'", then a copy
4949** of the input is returned.
4950*/
4951static void shellEscapeCrnl(
4952  sqlite3_context *context,
4953  int argc,
4954  sqlite3_value **argv
4955){
4956  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4957  UNUSED_PARAMETER(argc);
4958  if( zText && zText[0]=='\'' ){
4959    i64 nText = sqlite3_value_bytes(argv[0]);
4960    i64 i;
4961    char zBuf1[20];
4962    char zBuf2[20];
4963    const char *zNL = 0;
4964    const char *zCR = 0;
4965    i64 nCR = 0;
4966    i64 nNL = 0;
4967
4968    for(i=0; zText[i]; i++){
4969      if( zNL==0 && zText[i]=='\n' ){
4970        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4971        nNL = strlen(zNL);
4972      }
4973      if( zCR==0 && zText[i]=='\r' ){
4974        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4975        nCR = strlen(zCR);
4976      }
4977    }
4978
4979    if( zNL || zCR ){
4980      i64 iOut = 0;
4981      i64 nMax = (nNL > nCR) ? nNL : nCR;
4982      i64 nAlloc = nMax * nText + (nMax+64)*2;
4983      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4984      if( zOut==0 ){
4985        sqlite3_result_error_nomem(context);
4986        return;
4987      }
4988
4989      if( zNL && zCR ){
4990        memcpy(&zOut[iOut], "replace(replace(", 16);
4991        iOut += 16;
4992      }else{
4993        memcpy(&zOut[iOut], "replace(", 8);
4994        iOut += 8;
4995      }
4996      for(i=0; zText[i]; i++){
4997        if( zText[i]=='\n' ){
4998          memcpy(&zOut[iOut], zNL, nNL);
4999          iOut += nNL;
5000        }else if( zText[i]=='\r' ){
5001          memcpy(&zOut[iOut], zCR, nCR);
5002          iOut += nCR;
5003        }else{
5004          zOut[iOut] = zText[i];
5005          iOut++;
5006        }
5007      }
5008
5009      if( zNL ){
5010        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5011        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
5012        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
5013      }
5014      if( zCR ){
5015        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5016        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
5017        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
5018      }
5019
5020      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
5021      sqlite3_free(zOut);
5022      return;
5023    }
5024  }
5025
5026  sqlite3_result_value(context, argv[0]);
5027}
5028
5029/* Flags for open_db().
5030**
5031** The default behavior of open_db() is to exit(1) if the database fails to
5032** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5033** but still returns without calling exit.
5034**
5035** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5036** ZIP archive if the file does not exist or is empty and its name matches
5037** the *.zip pattern.
5038*/
5039#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
5040#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
5041
5042/*
5043** Make sure the database is open.  If it is not, then open it.  If
5044** the database fails to open, print an error message and exit.
5045*/
5046static void open_db(ShellState *p, int openFlags){
5047  if( p->db==0 ){
5048    const char *zDbFilename = p->pAuxDb->zDbFilename;
5049    if( p->openMode==SHELL_OPEN_UNSPEC ){
5050      if( zDbFilename==0 || zDbFilename[0]==0 ){
5051        p->openMode = SHELL_OPEN_NORMAL;
5052      }else{
5053        p->openMode = (u8)deduceDatabaseType(zDbFilename,
5054                             (openFlags & OPEN_DB_ZIPFILE)!=0);
5055      }
5056    }
5057    switch( p->openMode ){
5058      case SHELL_OPEN_APPENDVFS: {
5059        sqlite3_open_v2(zDbFilename, &p->db,
5060           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5061        break;
5062      }
5063      case SHELL_OPEN_HEXDB:
5064      case SHELL_OPEN_DESERIALIZE: {
5065        sqlite3_open(0, &p->db);
5066        break;
5067      }
5068      case SHELL_OPEN_ZIPFILE: {
5069        sqlite3_open(":memory:", &p->db);
5070        break;
5071      }
5072      case SHELL_OPEN_READONLY: {
5073        sqlite3_open_v2(zDbFilename, &p->db,
5074            SQLITE_OPEN_READONLY|p->openFlags, 0);
5075        break;
5076      }
5077      case SHELL_OPEN_UNSPEC:
5078      case SHELL_OPEN_NORMAL: {
5079        sqlite3_open_v2(zDbFilename, &p->db,
5080           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5081        break;
5082      }
5083    }
5084    globalDb = p->db;
5085    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5086      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5087          zDbFilename, sqlite3_errmsg(p->db));
5088      if( openFlags & OPEN_DB_KEEPALIVE ){
5089        sqlite3_open(":memory:", &p->db);
5090        return;
5091      }
5092      exit(1);
5093    }
5094#ifndef SQLITE_OMIT_LOAD_EXTENSION
5095    sqlite3_enable_load_extension(p->db, 1);
5096#endif
5097    sqlite3_shathree_init(p->db, 0, 0);
5098    sqlite3_uint_init(p->db, 0, 0);
5099    sqlite3_decimal_init(p->db, 0, 0);
5100    sqlite3_regexp_init(p->db, 0, 0);
5101    sqlite3_ieee_init(p->db, 0, 0);
5102    sqlite3_series_init(p->db, 0, 0);
5103#ifndef SQLITE_SHELL_FIDDLE
5104    sqlite3_fileio_init(p->db, 0, 0);
5105    sqlite3_completion_init(p->db, 0, 0);
5106#endif
5107#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5108    sqlite3_dbdata_init(p->db, 0, 0);
5109#endif
5110#ifdef SQLITE_HAVE_ZLIB
5111    if( !p->bSafeModePersist ){
5112      sqlite3_zipfile_init(p->db, 0, 0);
5113      sqlite3_sqlar_init(p->db, 0, 0);
5114    }
5115#endif
5116    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5117                            shellAddSchemaName, 0, 0);
5118    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5119                            shellModuleSchema, 0, 0);
5120    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5121                            shellPutsFunc, 0, 0);
5122    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
5123                            shellEscapeCrnl, 0, 0);
5124    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
5125                            shellInt32, 0, 0);
5126    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
5127                            shellIdQuote, 0, 0);
5128    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5129                            shellUSleepFunc, 0, 0);
5130#ifndef SQLITE_NOHAVE_SYSTEM
5131    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5132                            editFunc, 0, 0);
5133    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5134                            editFunc, 0, 0);
5135#endif
5136    if( p->openMode==SHELL_OPEN_ZIPFILE ){
5137      char *zSql = sqlite3_mprintf(
5138         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5139      shell_check_oom(zSql);
5140      sqlite3_exec(p->db, zSql, 0, 0, 0);
5141      sqlite3_free(zSql);
5142    }
5143#ifndef SQLITE_OMIT_DESERIALIZE
5144    else
5145    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5146      int rc;
5147      int nData = 0;
5148      unsigned char *aData;
5149      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5150        aData = (unsigned char*)readFile(zDbFilename, &nData);
5151      }else{
5152        aData = readHexDb(p, &nData);
5153        if( aData==0 ){
5154          return;
5155        }
5156      }
5157      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5158                   SQLITE_DESERIALIZE_RESIZEABLE |
5159                   SQLITE_DESERIALIZE_FREEONCLOSE);
5160      if( rc ){
5161        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5162      }
5163      if( p->szMax>0 ){
5164        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5165      }
5166    }
5167#endif
5168  }
5169  if( p->bSafeModePersist && p->db!=0 ){
5170    sqlite3_set_authorizer(p->db, safeModeAuth, p);
5171  }
5172}
5173
5174/*
5175** Attempt to close the databaes connection.  Report errors.
5176*/
5177void close_db(sqlite3 *db){
5178  int rc = sqlite3_close(db);
5179  if( rc ){
5180    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5181        rc, sqlite3_errmsg(db));
5182  }
5183}
5184
5185#if HAVE_READLINE || HAVE_EDITLINE
5186/*
5187** Readline completion callbacks
5188*/
5189static char *readline_completion_generator(const char *text, int state){
5190  static sqlite3_stmt *pStmt = 0;
5191  char *zRet;
5192  if( state==0 ){
5193    char *zSql;
5194    sqlite3_finalize(pStmt);
5195    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5196                           "  FROM completion(%Q) ORDER BY 1", text);
5197    shell_check_oom(zSql);
5198    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5199    sqlite3_free(zSql);
5200  }
5201  if( sqlite3_step(pStmt)==SQLITE_ROW ){
5202    const char *z = (const char*)sqlite3_column_text(pStmt,0);
5203    zRet = z ? strdup(z) : 0;
5204  }else{
5205    sqlite3_finalize(pStmt);
5206    pStmt = 0;
5207    zRet = 0;
5208  }
5209  return zRet;
5210}
5211static char **readline_completion(const char *zText, int iStart, int iEnd){
5212  rl_attempted_completion_over = 1;
5213  return rl_completion_matches(zText, readline_completion_generator);
5214}
5215
5216#elif HAVE_LINENOISE
5217/*
5218** Linenoise completion callback
5219*/
5220static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5221  i64 nLine = strlen(zLine);
5222  i64 i, iStart;
5223  sqlite3_stmt *pStmt = 0;
5224  char *zSql;
5225  char zBuf[1000];
5226
5227  if( nLine>sizeof(zBuf)-30 ) return;
5228  if( zLine[0]=='.' || zLine[0]=='#') return;
5229  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5230  if( i==nLine-1 ) return;
5231  iStart = i+1;
5232  memcpy(zBuf, zLine, iStart);
5233  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5234                         "  FROM completion(%Q,%Q) ORDER BY 1",
5235                         &zLine[iStart], zLine);
5236  shell_check_oom(zSql);
5237  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5238  sqlite3_free(zSql);
5239  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5240  while( sqlite3_step(pStmt)==SQLITE_ROW ){
5241    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5242    int nCompletion = sqlite3_column_bytes(pStmt, 0);
5243    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
5244      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5245      linenoiseAddCompletion(lc, zBuf);
5246    }
5247  }
5248  sqlite3_finalize(pStmt);
5249}
5250#endif
5251
5252/*
5253** Do C-language style dequoting.
5254**
5255**    \a    -> alarm
5256**    \b    -> backspace
5257**    \t    -> tab
5258**    \n    -> newline
5259**    \v    -> vertical tab
5260**    \f    -> form feed
5261**    \r    -> carriage return
5262**    \s    -> space
5263**    \"    -> "
5264**    \'    -> '
5265**    \\    -> backslash
5266**    \NNN  -> ascii character NNN in octal
5267*/
5268static void resolve_backslashes(char *z){
5269  int i, j;
5270  char c;
5271  while( *z && *z!='\\' ) z++;
5272  for(i=j=0; (c = z[i])!=0; i++, j++){
5273    if( c=='\\' && z[i+1]!=0 ){
5274      c = z[++i];
5275      if( c=='a' ){
5276        c = '\a';
5277      }else if( c=='b' ){
5278        c = '\b';
5279      }else if( c=='t' ){
5280        c = '\t';
5281      }else if( c=='n' ){
5282        c = '\n';
5283      }else if( c=='v' ){
5284        c = '\v';
5285      }else if( c=='f' ){
5286        c = '\f';
5287      }else if( c=='r' ){
5288        c = '\r';
5289      }else if( c=='"' ){
5290        c = '"';
5291      }else if( c=='\'' ){
5292        c = '\'';
5293      }else if( c=='\\' ){
5294        c = '\\';
5295      }else if( c>='0' && c<='7' ){
5296        c -= '0';
5297        if( z[i+1]>='0' && z[i+1]<='7' ){
5298          i++;
5299          c = (c<<3) + z[i] - '0';
5300          if( z[i+1]>='0' && z[i+1]<='7' ){
5301            i++;
5302            c = (c<<3) + z[i] - '0';
5303          }
5304        }
5305      }
5306    }
5307    z[j] = c;
5308  }
5309  if( j<i ) z[j] = 0;
5310}
5311
5312/*
5313** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
5314** for TRUE and FALSE.  Return the integer value if appropriate.
5315*/
5316static int booleanValue(const char *zArg){
5317  int i;
5318  if( zArg[0]=='0' && zArg[1]=='x' ){
5319    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5320  }else{
5321    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5322  }
5323  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5324  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5325    return 1;
5326  }
5327  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5328    return 0;
5329  }
5330  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5331          zArg);
5332  return 0;
5333}
5334
5335/*
5336** Set or clear a shell flag according to a boolean value.
5337*/
5338static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5339  if( booleanValue(zArg) ){
5340    ShellSetFlag(p, mFlag);
5341  }else{
5342    ShellClearFlag(p, mFlag);
5343  }
5344}
5345
5346/*
5347** Close an output file, assuming it is not stderr or stdout
5348*/
5349static void output_file_close(FILE *f){
5350  if( f && f!=stdout && f!=stderr ) fclose(f);
5351}
5352
5353/*
5354** Try to open an output file.   The names "stdout" and "stderr" are
5355** recognized and do the right thing.  NULL is returned if the output
5356** filename is "off".
5357*/
5358static FILE *output_file_open(const char *zFile, int bTextMode){
5359  FILE *f;
5360  if( strcmp(zFile,"stdout")==0 ){
5361    f = stdout;
5362  }else if( strcmp(zFile, "stderr")==0 ){
5363    f = stderr;
5364  }else if( strcmp(zFile, "off")==0 ){
5365    f = 0;
5366  }else{
5367    f = fopen(zFile, bTextMode ? "w" : "wb");
5368    if( f==0 ){
5369      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5370    }
5371  }
5372  return f;
5373}
5374
5375#ifndef SQLITE_OMIT_TRACE
5376/*
5377** A routine for handling output from sqlite3_trace().
5378*/
5379static int sql_trace_callback(
5380  unsigned mType,         /* The trace type */
5381  void *pArg,             /* The ShellState pointer */
5382  void *pP,               /* Usually a pointer to sqlite_stmt */
5383  void *pX                /* Auxiliary output */
5384){
5385  ShellState *p = (ShellState*)pArg;
5386  sqlite3_stmt *pStmt;
5387  const char *zSql;
5388  i64 nSql;
5389  if( p->traceOut==0 ) return 0;
5390  if( mType==SQLITE_TRACE_CLOSE ){
5391    utf8_printf(p->traceOut, "-- closing database connection\n");
5392    return 0;
5393  }
5394  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5395    zSql = (const char*)pX;
5396  }else{
5397    pStmt = (sqlite3_stmt*)pP;
5398    switch( p->eTraceType ){
5399      case SHELL_TRACE_EXPANDED: {
5400        zSql = sqlite3_expanded_sql(pStmt);
5401        break;
5402      }
5403#ifdef SQLITE_ENABLE_NORMALIZE
5404      case SHELL_TRACE_NORMALIZED: {
5405        zSql = sqlite3_normalized_sql(pStmt);
5406        break;
5407      }
5408#endif
5409      default: {
5410        zSql = sqlite3_sql(pStmt);
5411        break;
5412      }
5413    }
5414  }
5415  if( zSql==0 ) return 0;
5416  nSql = strlen(zSql);
5417  if( nSql>1000000000 ) nSql = 1000000000;
5418  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5419  switch( mType ){
5420    case SQLITE_TRACE_ROW:
5421    case SQLITE_TRACE_STMT: {
5422      utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5423      break;
5424    }
5425    case SQLITE_TRACE_PROFILE: {
5426      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5427      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5428      break;
5429    }
5430  }
5431  return 0;
5432}
5433#endif
5434
5435/*
5436** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5437** a useful spot to set a debugger breakpoint.
5438*/
5439static void test_breakpoint(void){
5440  static int nCall = 0;
5441  nCall++;
5442}
5443
5444/*
5445** An object used to read a CSV and other files for import.
5446*/
5447typedef struct ImportCtx ImportCtx;
5448struct ImportCtx {
5449  const char *zFile;  /* Name of the input file */
5450  FILE *in;           /* Read the CSV text from this input stream */
5451  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5452  char *z;            /* Accumulated text for a field */
5453  int n;              /* Number of bytes in z */
5454  int nAlloc;         /* Space allocated for z[] */
5455  int nLine;          /* Current line number */
5456  int nRow;           /* Number of rows imported */
5457  int nErr;           /* Number of errors encountered */
5458  int bNotFirst;      /* True if one or more bytes already read */
5459  int cTerm;          /* Character that terminated the most recent field */
5460  int cColSep;        /* The column separator character.  (Usually ",") */
5461  int cRowSep;        /* The row separator character.  (Usually "\n") */
5462};
5463
5464/* Clean up resourced used by an ImportCtx */
5465static void import_cleanup(ImportCtx *p){
5466  if( p->in!=0 && p->xCloser!=0 ){
5467    p->xCloser(p->in);
5468    p->in = 0;
5469  }
5470  sqlite3_free(p->z);
5471  p->z = 0;
5472}
5473
5474/* Append a single byte to z[] */
5475static void import_append_char(ImportCtx *p, int c){
5476  if( p->n+1>=p->nAlloc ){
5477    p->nAlloc += p->nAlloc + 100;
5478    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5479    shell_check_oom(p->z);
5480  }
5481  p->z[p->n++] = (char)c;
5482}
5483
5484/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5485** with the option of having a separator other than ",".
5486**
5487**   +  Input comes from p->in.
5488**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5489**      from sqlite3_malloc64().
5490**   +  Use p->cSep as the column separator.  The default is ",".
5491**   +  Use p->rSep as the row separator.  The default is "\n".
5492**   +  Keep track of the line number in p->nLine.
5493**   +  Store the character that terminates the field in p->cTerm.  Store
5494**      EOF on end-of-file.
5495**   +  Report syntax errors on stderr
5496*/
5497static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5498  int c;
5499  int cSep = p->cColSep;
5500  int rSep = p->cRowSep;
5501  p->n = 0;
5502  c = fgetc(p->in);
5503  if( c==EOF || seenInterrupt ){
5504    p->cTerm = EOF;
5505    return 0;
5506  }
5507  if( c=='"' ){
5508    int pc, ppc;
5509    int startLine = p->nLine;
5510    int cQuote = c;
5511    pc = ppc = 0;
5512    while( 1 ){
5513      c = fgetc(p->in);
5514      if( c==rSep ) p->nLine++;
5515      if( c==cQuote ){
5516        if( pc==cQuote ){
5517          pc = 0;
5518          continue;
5519        }
5520      }
5521      if( (c==cSep && pc==cQuote)
5522       || (c==rSep && pc==cQuote)
5523       || (c==rSep && pc=='\r' && ppc==cQuote)
5524       || (c==EOF && pc==cQuote)
5525      ){
5526        do{ p->n--; }while( p->z[p->n]!=cQuote );
5527        p->cTerm = c;
5528        break;
5529      }
5530      if( pc==cQuote && c!='\r' ){
5531        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5532                p->zFile, p->nLine, cQuote);
5533      }
5534      if( c==EOF ){
5535        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5536                p->zFile, startLine, cQuote);
5537        p->cTerm = c;
5538        break;
5539      }
5540      import_append_char(p, c);
5541      ppc = pc;
5542      pc = c;
5543    }
5544  }else{
5545    /* If this is the first field being parsed and it begins with the
5546    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5547    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5548      import_append_char(p, c);
5549      c = fgetc(p->in);
5550      if( (c&0xff)==0xbb ){
5551        import_append_char(p, c);
5552        c = fgetc(p->in);
5553        if( (c&0xff)==0xbf ){
5554          p->bNotFirst = 1;
5555          p->n = 0;
5556          return csv_read_one_field(p);
5557        }
5558      }
5559    }
5560    while( c!=EOF && c!=cSep && c!=rSep ){
5561      import_append_char(p, c);
5562      c = fgetc(p->in);
5563    }
5564    if( c==rSep ){
5565      p->nLine++;
5566      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5567    }
5568    p->cTerm = c;
5569  }
5570  if( p->z ) p->z[p->n] = 0;
5571  p->bNotFirst = 1;
5572  return p->z;
5573}
5574
5575/* Read a single field of ASCII delimited text.
5576**
5577**   +  Input comes from p->in.
5578**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5579**      from sqlite3_malloc64().
5580**   +  Use p->cSep as the column separator.  The default is "\x1F".
5581**   +  Use p->rSep as the row separator.  The default is "\x1E".
5582**   +  Keep track of the row number in p->nLine.
5583**   +  Store the character that terminates the field in p->cTerm.  Store
5584**      EOF on end-of-file.
5585**   +  Report syntax errors on stderr
5586*/
5587static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5588  int c;
5589  int cSep = p->cColSep;
5590  int rSep = p->cRowSep;
5591  p->n = 0;
5592  c = fgetc(p->in);
5593  if( c==EOF || seenInterrupt ){
5594    p->cTerm = EOF;
5595    return 0;
5596  }
5597  while( c!=EOF && c!=cSep && c!=rSep ){
5598    import_append_char(p, c);
5599    c = fgetc(p->in);
5600  }
5601  if( c==rSep ){
5602    p->nLine++;
5603  }
5604  p->cTerm = c;
5605  if( p->z ) p->z[p->n] = 0;
5606  return p->z;
5607}
5608
5609/*
5610** Try to transfer data for table zTable.  If an error is seen while
5611** moving forward, try to go backwards.  The backwards movement won't
5612** work for WITHOUT ROWID tables.
5613*/
5614static void tryToCloneData(
5615  ShellState *p,
5616  sqlite3 *newDb,
5617  const char *zTable
5618){
5619  sqlite3_stmt *pQuery = 0;
5620  sqlite3_stmt *pInsert = 0;
5621  char *zQuery = 0;
5622  char *zInsert = 0;
5623  int rc;
5624  int i, j, n;
5625  int nTable = strlen30(zTable);
5626  int k = 0;
5627  int cnt = 0;
5628  const int spinRate = 10000;
5629
5630  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5631  shell_check_oom(zQuery);
5632  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5633  if( rc ){
5634    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5635            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5636            zQuery);
5637    goto end_data_xfer;
5638  }
5639  n = sqlite3_column_count(pQuery);
5640  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5641  shell_check_oom(zInsert);
5642  sqlite3_snprintf(200+nTable,zInsert,
5643                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5644  i = strlen30(zInsert);
5645  for(j=1; j<n; j++){
5646    memcpy(zInsert+i, ",?", 2);
5647    i += 2;
5648  }
5649  memcpy(zInsert+i, ");", 3);
5650  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5651  if( rc ){
5652    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5653            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5654            zQuery);
5655    goto end_data_xfer;
5656  }
5657  for(k=0; k<2; k++){
5658    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5659      for(i=0; i<n; i++){
5660        switch( sqlite3_column_type(pQuery, i) ){
5661          case SQLITE_NULL: {
5662            sqlite3_bind_null(pInsert, i+1);
5663            break;
5664          }
5665          case SQLITE_INTEGER: {
5666            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5667            break;
5668          }
5669          case SQLITE_FLOAT: {
5670            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5671            break;
5672          }
5673          case SQLITE_TEXT: {
5674            sqlite3_bind_text(pInsert, i+1,
5675                             (const char*)sqlite3_column_text(pQuery,i),
5676                             -1, SQLITE_STATIC);
5677            break;
5678          }
5679          case SQLITE_BLOB: {
5680            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5681                                            sqlite3_column_bytes(pQuery,i),
5682                                            SQLITE_STATIC);
5683            break;
5684          }
5685        }
5686      } /* End for */
5687      rc = sqlite3_step(pInsert);
5688      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5689        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5690                        sqlite3_errmsg(newDb));
5691      }
5692      sqlite3_reset(pInsert);
5693      cnt++;
5694      if( (cnt%spinRate)==0 ){
5695        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5696        fflush(stdout);
5697      }
5698    } /* End while */
5699    if( rc==SQLITE_DONE ) break;
5700    sqlite3_finalize(pQuery);
5701    sqlite3_free(zQuery);
5702    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5703                             zTable);
5704    shell_check_oom(zQuery);
5705    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5706    if( rc ){
5707      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5708      break;
5709    }
5710  } /* End for(k=0...) */
5711
5712end_data_xfer:
5713  sqlite3_finalize(pQuery);
5714  sqlite3_finalize(pInsert);
5715  sqlite3_free(zQuery);
5716  sqlite3_free(zInsert);
5717}
5718
5719
5720/*
5721** Try to transfer all rows of the schema that match zWhere.  For
5722** each row, invoke xForEach() on the object defined by that row.
5723** If an error is encountered while moving forward through the
5724** sqlite_schema table, try again moving backwards.
5725*/
5726static void tryToCloneSchema(
5727  ShellState *p,
5728  sqlite3 *newDb,
5729  const char *zWhere,
5730  void (*xForEach)(ShellState*,sqlite3*,const char*)
5731){
5732  sqlite3_stmt *pQuery = 0;
5733  char *zQuery = 0;
5734  int rc;
5735  const unsigned char *zName;
5736  const unsigned char *zSql;
5737  char *zErrMsg = 0;
5738
5739  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5740                           " WHERE %s", zWhere);
5741  shell_check_oom(zQuery);
5742  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5743  if( rc ){
5744    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5745                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5746                    zQuery);
5747    goto end_schema_xfer;
5748  }
5749  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5750    zName = sqlite3_column_text(pQuery, 0);
5751    zSql = sqlite3_column_text(pQuery, 1);
5752    if( zName==0 || zSql==0 ) continue;
5753    printf("%s... ", zName); fflush(stdout);
5754    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5755    if( zErrMsg ){
5756      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5757      sqlite3_free(zErrMsg);
5758      zErrMsg = 0;
5759    }
5760    if( xForEach ){
5761      xForEach(p, newDb, (const char*)zName);
5762    }
5763    printf("done\n");
5764  }
5765  if( rc!=SQLITE_DONE ){
5766    sqlite3_finalize(pQuery);
5767    sqlite3_free(zQuery);
5768    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5769                             " WHERE %s ORDER BY rowid DESC", zWhere);
5770    shell_check_oom(zQuery);
5771    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5772    if( rc ){
5773      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5774                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5775                      zQuery);
5776      goto end_schema_xfer;
5777    }
5778    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5779      zName = sqlite3_column_text(pQuery, 0);
5780      zSql = sqlite3_column_text(pQuery, 1);
5781      if( zName==0 || zSql==0 ) continue;
5782      printf("%s... ", zName); fflush(stdout);
5783      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5784      if( zErrMsg ){
5785        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5786        sqlite3_free(zErrMsg);
5787        zErrMsg = 0;
5788      }
5789      if( xForEach ){
5790        xForEach(p, newDb, (const char*)zName);
5791      }
5792      printf("done\n");
5793    }
5794  }
5795end_schema_xfer:
5796  sqlite3_finalize(pQuery);
5797  sqlite3_free(zQuery);
5798}
5799
5800/*
5801** Open a new database file named "zNewDb".  Try to recover as much information
5802** as possible out of the main database (which might be corrupt) and write it
5803** into zNewDb.
5804*/
5805static void tryToClone(ShellState *p, const char *zNewDb){
5806  int rc;
5807  sqlite3 *newDb = 0;
5808  if( access(zNewDb,0)==0 ){
5809    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5810    return;
5811  }
5812  rc = sqlite3_open(zNewDb, &newDb);
5813  if( rc ){
5814    utf8_printf(stderr, "Cannot create output database: %s\n",
5815            sqlite3_errmsg(newDb));
5816  }else{
5817    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5818    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5819    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5820    tryToCloneSchema(p, newDb, "type!='table'", 0);
5821    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5822    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5823  }
5824  close_db(newDb);
5825}
5826
5827/*
5828** Change the output file back to stdout.
5829**
5830** If the p->doXdgOpen flag is set, that means the output was being
5831** redirected to a temporary file named by p->zTempFile.  In that case,
5832** launch start/open/xdg-open on that temporary file.
5833*/
5834static void output_reset(ShellState *p){
5835  if( p->outfile[0]=='|' ){
5836#ifndef SQLITE_OMIT_POPEN
5837    pclose(p->out);
5838#endif
5839  }else{
5840    output_file_close(p->out);
5841#ifndef SQLITE_NOHAVE_SYSTEM
5842    if( p->doXdgOpen ){
5843      const char *zXdgOpenCmd =
5844#if defined(_WIN32)
5845      "start";
5846#elif defined(__APPLE__)
5847      "open";
5848#else
5849      "xdg-open";
5850#endif
5851      char *zCmd;
5852      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5853      if( system(zCmd) ){
5854        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5855      }else{
5856        /* Give the start/open/xdg-open command some time to get
5857        ** going before we continue, and potential delete the
5858        ** p->zTempFile data file out from under it */
5859        sqlite3_sleep(2000);
5860      }
5861      sqlite3_free(zCmd);
5862      outputModePop(p);
5863      p->doXdgOpen = 0;
5864    }
5865#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5866  }
5867  p->outfile[0] = 0;
5868  p->out = stdout;
5869}
5870
5871/*
5872** Run an SQL command and return the single integer result.
5873*/
5874static int db_int(sqlite3 *db, const char *zSql){
5875  sqlite3_stmt *pStmt;
5876  int res = 0;
5877  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
5878  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5879    res = sqlite3_column_int(pStmt,0);
5880  }
5881  sqlite3_finalize(pStmt);
5882  return res;
5883}
5884
5885#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5886/*
5887** Convert a 2-byte or 4-byte big-endian integer into a native integer
5888*/
5889static unsigned int get2byteInt(unsigned char *a){
5890  return (a[0]<<8) + a[1];
5891}
5892static unsigned int get4byteInt(unsigned char *a){
5893  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5894}
5895
5896/*
5897** Implementation of the ".dbinfo" command.
5898**
5899** Return 1 on error, 2 to exit, and 0 otherwise.
5900*/
5901static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5902  static const struct { const char *zName; int ofst; } aField[] = {
5903     { "file change counter:",  24  },
5904     { "database page count:",  28  },
5905     { "freelist page count:",  36  },
5906     { "schema cookie:",        40  },
5907     { "schema format:",        44  },
5908     { "default cache size:",   48  },
5909     { "autovacuum top root:",  52  },
5910     { "incremental vacuum:",   64  },
5911     { "text encoding:",        56  },
5912     { "user version:",         60  },
5913     { "application id:",       68  },
5914     { "software version:",     96  },
5915  };
5916  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5917     { "number of tables:",
5918       "SELECT count(*) FROM %s WHERE type='table'" },
5919     { "number of indexes:",
5920       "SELECT count(*) FROM %s WHERE type='index'" },
5921     { "number of triggers:",
5922       "SELECT count(*) FROM %s WHERE type='trigger'" },
5923     { "number of views:",
5924       "SELECT count(*) FROM %s WHERE type='view'" },
5925     { "schema size:",
5926       "SELECT total(length(sql)) FROM %s" },
5927  };
5928  int i, rc;
5929  unsigned iDataVersion;
5930  char *zSchemaTab;
5931  char *zDb = nArg>=2 ? azArg[1] : "main";
5932  sqlite3_stmt *pStmt = 0;
5933  unsigned char aHdr[100];
5934  open_db(p, 0);
5935  if( p->db==0 ) return 1;
5936  rc = sqlite3_prepare_v2(p->db,
5937             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5938             -1, &pStmt, 0);
5939  if( rc ){
5940    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5941    sqlite3_finalize(pStmt);
5942    return 1;
5943  }
5944  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5945  if( sqlite3_step(pStmt)==SQLITE_ROW
5946   && sqlite3_column_bytes(pStmt,0)>100
5947  ){
5948    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5949    sqlite3_finalize(pStmt);
5950  }else{
5951    raw_printf(stderr, "unable to read database header\n");
5952    sqlite3_finalize(pStmt);
5953    return 1;
5954  }
5955  i = get2byteInt(aHdr+16);
5956  if( i==1 ) i = 65536;
5957  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5958  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5959  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5960  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5961  for(i=0; i<ArraySize(aField); i++){
5962    int ofst = aField[i].ofst;
5963    unsigned int val = get4byteInt(aHdr + ofst);
5964    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5965    switch( ofst ){
5966      case 56: {
5967        if( val==1 ) raw_printf(p->out, " (utf8)");
5968        if( val==2 ) raw_printf(p->out, " (utf16le)");
5969        if( val==3 ) raw_printf(p->out, " (utf16be)");
5970      }
5971    }
5972    raw_printf(p->out, "\n");
5973  }
5974  if( zDb==0 ){
5975    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5976  }else if( strcmp(zDb,"temp")==0 ){
5977    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5978  }else{
5979    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5980  }
5981  for(i=0; i<ArraySize(aQuery); i++){
5982    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5983    int val = db_int(p->db, zSql);
5984    sqlite3_free(zSql);
5985    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5986  }
5987  sqlite3_free(zSchemaTab);
5988  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5989  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5990  return 0;
5991}
5992#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
5993          && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
5994
5995/*
5996** Print the current sqlite3_errmsg() value to stderr and return 1.
5997*/
5998static int shellDatabaseError(sqlite3 *db){
5999  const char *zErr = sqlite3_errmsg(db);
6000  utf8_printf(stderr, "Error: %s\n", zErr);
6001  return 1;
6002}
6003
6004/*
6005** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
6006** if they match and FALSE (0) if they do not match.
6007**
6008** Globbing rules:
6009**
6010**      '*'       Matches any sequence of zero or more characters.
6011**
6012**      '?'       Matches exactly one character.
6013**
6014**     [...]      Matches one character from the enclosed list of
6015**                characters.
6016**
6017**     [^...]     Matches one character not in the enclosed list.
6018**
6019**      '#'       Matches any sequence of one or more digits with an
6020**                optional + or - sign in front
6021**
6022**      ' '       Any span of whitespace matches any other span of
6023**                whitespace.
6024**
6025** Extra whitespace at the end of z[] is ignored.
6026*/
6027static int testcase_glob(const char *zGlob, const char *z){
6028  int c, c2;
6029  int invert;
6030  int seen;
6031
6032  while( (c = (*(zGlob++)))!=0 ){
6033    if( IsSpace(c) ){
6034      if( !IsSpace(*z) ) return 0;
6035      while( IsSpace(*zGlob) ) zGlob++;
6036      while( IsSpace(*z) ) z++;
6037    }else if( c=='*' ){
6038      while( (c=(*(zGlob++))) == '*' || c=='?' ){
6039        if( c=='?' && (*(z++))==0 ) return 0;
6040      }
6041      if( c==0 ){
6042        return 1;
6043      }else if( c=='[' ){
6044        while( *z && testcase_glob(zGlob-1,z)==0 ){
6045          z++;
6046        }
6047        return (*z)!=0;
6048      }
6049      while( (c2 = (*(z++)))!=0 ){
6050        while( c2!=c ){
6051          c2 = *(z++);
6052          if( c2==0 ) return 0;
6053        }
6054        if( testcase_glob(zGlob,z) ) return 1;
6055      }
6056      return 0;
6057    }else if( c=='?' ){
6058      if( (*(z++))==0 ) return 0;
6059    }else if( c=='[' ){
6060      int prior_c = 0;
6061      seen = 0;
6062      invert = 0;
6063      c = *(z++);
6064      if( c==0 ) return 0;
6065      c2 = *(zGlob++);
6066      if( c2=='^' ){
6067        invert = 1;
6068        c2 = *(zGlob++);
6069      }
6070      if( c2==']' ){
6071        if( c==']' ) seen = 1;
6072        c2 = *(zGlob++);
6073      }
6074      while( c2 && c2!=']' ){
6075        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6076          c2 = *(zGlob++);
6077          if( c>=prior_c && c<=c2 ) seen = 1;
6078          prior_c = 0;
6079        }else{
6080          if( c==c2 ){
6081            seen = 1;
6082          }
6083          prior_c = c2;
6084        }
6085        c2 = *(zGlob++);
6086      }
6087      if( c2==0 || (seen ^ invert)==0 ) return 0;
6088    }else if( c=='#' ){
6089      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6090      if( !IsDigit(z[0]) ) return 0;
6091      z++;
6092      while( IsDigit(z[0]) ){ z++; }
6093    }else{
6094      if( c!=(*(z++)) ) return 0;
6095    }
6096  }
6097  while( IsSpace(*z) ){ z++; }
6098  return *z==0;
6099}
6100
6101
6102/*
6103** Compare the string as a command-line option with either one or two
6104** initial "-" characters.
6105*/
6106static int optionMatch(const char *zStr, const char *zOpt){
6107  if( zStr[0]!='-' ) return 0;
6108  zStr++;
6109  if( zStr[0]=='-' ) zStr++;
6110  return strcmp(zStr, zOpt)==0;
6111}
6112
6113/*
6114** Delete a file.
6115*/
6116int shellDeleteFile(const char *zFilename){
6117  int rc;
6118#ifdef _WIN32
6119  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6120  rc = _wunlink(z);
6121  sqlite3_free(z);
6122#else
6123  rc = unlink(zFilename);
6124#endif
6125  return rc;
6126}
6127
6128/*
6129** Try to delete the temporary file (if there is one) and free the
6130** memory used to hold the name of the temp file.
6131*/
6132static void clearTempFile(ShellState *p){
6133  if( p->zTempFile==0 ) return;
6134  if( p->doXdgOpen ) return;
6135  if( shellDeleteFile(p->zTempFile) ) return;
6136  sqlite3_free(p->zTempFile);
6137  p->zTempFile = 0;
6138}
6139
6140/*
6141** Create a new temp file name with the given suffix.
6142*/
6143static void newTempFile(ShellState *p, const char *zSuffix){
6144  clearTempFile(p);
6145  sqlite3_free(p->zTempFile);
6146  p->zTempFile = 0;
6147  if( p->db ){
6148    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6149  }
6150  if( p->zTempFile==0 ){
6151    /* If p->db is an in-memory database then the TEMPFILENAME file-control
6152    ** will not work and we will need to fallback to guessing */
6153    char *zTemp;
6154    sqlite3_uint64 r;
6155    sqlite3_randomness(sizeof(r), &r);
6156    zTemp = getenv("TEMP");
6157    if( zTemp==0 ) zTemp = getenv("TMP");
6158    if( zTemp==0 ){
6159#ifdef _WIN32
6160      zTemp = "\\tmp";
6161#else
6162      zTemp = "/tmp";
6163#endif
6164    }
6165    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6166  }else{
6167    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6168  }
6169  shell_check_oom(p->zTempFile);
6170}
6171
6172
6173/*
6174** The implementation of SQL scalar function fkey_collate_clause(), used
6175** by the ".lint fkey-indexes" command. This scalar function is always
6176** called with four arguments - the parent table name, the parent column name,
6177** the child table name and the child column name.
6178**
6179**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6180**
6181** If either of the named tables or columns do not exist, this function
6182** returns an empty string. An empty string is also returned if both tables
6183** and columns exist but have the same default collation sequence. Or,
6184** if both exist but the default collation sequences are different, this
6185** function returns the string " COLLATE <parent-collation>", where
6186** <parent-collation> is the default collation sequence of the parent column.
6187*/
6188static void shellFkeyCollateClause(
6189  sqlite3_context *pCtx,
6190  int nVal,
6191  sqlite3_value **apVal
6192){
6193  sqlite3 *db = sqlite3_context_db_handle(pCtx);
6194  const char *zParent;
6195  const char *zParentCol;
6196  const char *zParentSeq;
6197  const char *zChild;
6198  const char *zChildCol;
6199  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
6200  int rc;
6201
6202  assert( nVal==4 );
6203  zParent = (const char*)sqlite3_value_text(apVal[0]);
6204  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6205  zChild = (const char*)sqlite3_value_text(apVal[2]);
6206  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6207
6208  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6209  rc = sqlite3_table_column_metadata(
6210      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6211  );
6212  if( rc==SQLITE_OK ){
6213    rc = sqlite3_table_column_metadata(
6214        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6215    );
6216  }
6217
6218  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6219    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6220    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6221    sqlite3_free(z);
6222  }
6223}
6224
6225
6226/*
6227** The implementation of dot-command ".lint fkey-indexes".
6228*/
6229static int lintFkeyIndexes(
6230  ShellState *pState,             /* Current shell tool state */
6231  char **azArg,                   /* Array of arguments passed to dot command */
6232  int nArg                        /* Number of entries in azArg[] */
6233){
6234  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
6235  FILE *out = pState->out;        /* Stream to write non-error output to */
6236  int bVerbose = 0;               /* If -verbose is present */
6237  int bGroupByParent = 0;         /* If -groupbyparent is present */
6238  int i;                          /* To iterate through azArg[] */
6239  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
6240  int rc;                         /* Return code */
6241  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
6242
6243  /*
6244  ** This SELECT statement returns one row for each foreign key constraint
6245  ** in the schema of the main database. The column values are:
6246  **
6247  ** 0. The text of an SQL statement similar to:
6248  **
6249  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6250  **
6251  **    This SELECT is similar to the one that the foreign keys implementation
6252  **    needs to run internally on child tables. If there is an index that can
6253  **    be used to optimize this query, then it can also be used by the FK
6254  **    implementation to optimize DELETE or UPDATE statements on the parent
6255  **    table.
6256  **
6257  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6258  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6259  **    contains an index that can be used to optimize the query.
6260  **
6261  ** 2. Human readable text that describes the child table and columns. e.g.
6262  **
6263  **       "child_table(child_key1, child_key2)"
6264  **
6265  ** 3. Human readable text that describes the parent table and columns. e.g.
6266  **
6267  **       "parent_table(parent_key1, parent_key2)"
6268  **
6269  ** 4. A full CREATE INDEX statement for an index that could be used to
6270  **    optimize DELETE or UPDATE statements on the parent table. e.g.
6271  **
6272  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
6273  **
6274  ** 5. The name of the parent table.
6275  **
6276  ** These six values are used by the C logic below to generate the report.
6277  */
6278  const char *zSql =
6279  "SELECT "
6280    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6281    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6282    "  || fkey_collate_clause("
6283    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6284    ", "
6285    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6286    "  || group_concat('*=?', ' AND ') || ')'"
6287    ", "
6288    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
6289    ", "
6290    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6291    ", "
6292    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6293    "  || ' ON ' || quote(s.name) || '('"
6294    "  || group_concat(quote(f.[from]) ||"
6295    "        fkey_collate_clause("
6296    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6297    "  || ');'"
6298    ", "
6299    "     f.[table] "
6300    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6301    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6302    "GROUP BY s.name, f.id "
6303    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6304  ;
6305  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6306
6307  for(i=2; i<nArg; i++){
6308    int n = strlen30(azArg[i]);
6309    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6310      bVerbose = 1;
6311    }
6312    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6313      bGroupByParent = 1;
6314      zIndent = "    ";
6315    }
6316    else{
6317      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6318          azArg[0], azArg[1]
6319      );
6320      return SQLITE_ERROR;
6321    }
6322  }
6323
6324  /* Register the fkey_collate_clause() SQL function */
6325  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6326      0, shellFkeyCollateClause, 0, 0
6327  );
6328
6329
6330  if( rc==SQLITE_OK ){
6331    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6332  }
6333  if( rc==SQLITE_OK ){
6334    sqlite3_bind_int(pSql, 1, bGroupByParent);
6335  }
6336
6337  if( rc==SQLITE_OK ){
6338    int rc2;
6339    char *zPrev = 0;
6340    while( SQLITE_ROW==sqlite3_step(pSql) ){
6341      int res = -1;
6342      sqlite3_stmt *pExplain = 0;
6343      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6344      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6345      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6346      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6347      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6348      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6349
6350      if( zEQP==0 ) continue;
6351      if( zGlob==0 ) continue;
6352      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6353      if( rc!=SQLITE_OK ) break;
6354      if( SQLITE_ROW==sqlite3_step(pExplain) ){
6355        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6356        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
6357                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
6358      }
6359      rc = sqlite3_finalize(pExplain);
6360      if( rc!=SQLITE_OK ) break;
6361
6362      if( res<0 ){
6363        raw_printf(stderr, "Error: internal error");
6364        break;
6365      }else{
6366        if( bGroupByParent
6367        && (bVerbose || res==0)
6368        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6369        ){
6370          raw_printf(out, "-- Parent table %s\n", zParent);
6371          sqlite3_free(zPrev);
6372          zPrev = sqlite3_mprintf("%s", zParent);
6373        }
6374
6375        if( res==0 ){
6376          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6377        }else if( bVerbose ){
6378          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6379              zIndent, zFrom, zTarget
6380          );
6381        }
6382      }
6383    }
6384    sqlite3_free(zPrev);
6385
6386    if( rc!=SQLITE_OK ){
6387      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6388    }
6389
6390    rc2 = sqlite3_finalize(pSql);
6391    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6392      rc = rc2;
6393      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6394    }
6395  }else{
6396    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6397  }
6398
6399  return rc;
6400}
6401
6402/*
6403** Implementation of ".lint" dot command.
6404*/
6405static int lintDotCommand(
6406  ShellState *pState,             /* Current shell tool state */
6407  char **azArg,                   /* Array of arguments passed to dot command */
6408  int nArg                        /* Number of entries in azArg[] */
6409){
6410  int n;
6411  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6412  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6413  return lintFkeyIndexes(pState, azArg, nArg);
6414
6415 usage:
6416  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6417  raw_printf(stderr, "Where sub-commands are:\n");
6418  raw_printf(stderr, "    fkey-indexes\n");
6419  return SQLITE_ERROR;
6420}
6421
6422#if !defined SQLITE_OMIT_VIRTUALTABLE
6423static void shellPrepare(
6424  sqlite3 *db,
6425  int *pRc,
6426  const char *zSql,
6427  sqlite3_stmt **ppStmt
6428){
6429  *ppStmt = 0;
6430  if( *pRc==SQLITE_OK ){
6431    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6432    if( rc!=SQLITE_OK ){
6433      raw_printf(stderr, "sql error: %s (%d)\n",
6434          sqlite3_errmsg(db), sqlite3_errcode(db)
6435      );
6436      *pRc = rc;
6437    }
6438  }
6439}
6440
6441/*
6442** Create a prepared statement using printf-style arguments for the SQL.
6443**
6444** This routine is could be marked "static".  But it is not always used,
6445** depending on compile-time options.  By omitting the "static", we avoid
6446** nuisance compiler warnings about "defined but not used".
6447*/
6448void shellPreparePrintf(
6449  sqlite3 *db,
6450  int *pRc,
6451  sqlite3_stmt **ppStmt,
6452  const char *zFmt,
6453  ...
6454){
6455  *ppStmt = 0;
6456  if( *pRc==SQLITE_OK ){
6457    va_list ap;
6458    char *z;
6459    va_start(ap, zFmt);
6460    z = sqlite3_vmprintf(zFmt, ap);
6461    va_end(ap);
6462    if( z==0 ){
6463      *pRc = SQLITE_NOMEM;
6464    }else{
6465      shellPrepare(db, pRc, z, ppStmt);
6466      sqlite3_free(z);
6467    }
6468  }
6469}
6470
6471/* Finalize the prepared statement created using shellPreparePrintf().
6472**
6473** This routine is could be marked "static".  But it is not always used,
6474** depending on compile-time options.  By omitting the "static", we avoid
6475** nuisance compiler warnings about "defined but not used".
6476*/
6477void shellFinalize(
6478  int *pRc,
6479  sqlite3_stmt *pStmt
6480){
6481  if( pStmt ){
6482    sqlite3 *db = sqlite3_db_handle(pStmt);
6483    int rc = sqlite3_finalize(pStmt);
6484    if( *pRc==SQLITE_OK ){
6485      if( rc!=SQLITE_OK ){
6486        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6487      }
6488      *pRc = rc;
6489    }
6490  }
6491}
6492
6493/* Reset the prepared statement created using shellPreparePrintf().
6494**
6495** This routine is could be marked "static".  But it is not always used,
6496** depending on compile-time options.  By omitting the "static", we avoid
6497** nuisance compiler warnings about "defined but not used".
6498*/
6499void shellReset(
6500  int *pRc,
6501  sqlite3_stmt *pStmt
6502){
6503  int rc = sqlite3_reset(pStmt);
6504  if( *pRc==SQLITE_OK ){
6505    if( rc!=SQLITE_OK ){
6506      sqlite3 *db = sqlite3_db_handle(pStmt);
6507      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6508    }
6509    *pRc = rc;
6510  }
6511}
6512#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6513
6514#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6515/******************************************************************************
6516** The ".archive" or ".ar" command.
6517*/
6518/*
6519** Structure representing a single ".ar" command.
6520*/
6521typedef struct ArCommand ArCommand;
6522struct ArCommand {
6523  u8 eCmd;                        /* An AR_CMD_* value */
6524  u8 bVerbose;                    /* True if --verbose */
6525  u8 bZip;                        /* True if the archive is a ZIP */
6526  u8 bDryRun;                     /* True if --dry-run */
6527  u8 bAppend;                     /* True if --append */
6528  u8 bGlob;                       /* True if --glob */
6529  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6530  int nArg;                       /* Number of command arguments */
6531  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6532  const char *zFile;              /* --file argument, or NULL */
6533  const char *zDir;               /* --directory argument, or NULL */
6534  char **azArg;                   /* Array of command arguments */
6535  ShellState *p;                  /* Shell state */
6536  sqlite3 *db;                    /* Database containing the archive */
6537};
6538
6539/*
6540** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6541*/
6542static int arUsage(FILE *f){
6543  showHelp(f,"archive");
6544  return SQLITE_ERROR;
6545}
6546
6547/*
6548** Print an error message for the .ar command to stderr and return
6549** SQLITE_ERROR.
6550*/
6551static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6552  va_list ap;
6553  char *z;
6554  va_start(ap, zFmt);
6555  z = sqlite3_vmprintf(zFmt, ap);
6556  va_end(ap);
6557  utf8_printf(stderr, "Error: %s\n", z);
6558  if( pAr->fromCmdLine ){
6559    utf8_printf(stderr, "Use \"-A\" for more help\n");
6560  }else{
6561    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6562  }
6563  sqlite3_free(z);
6564  return SQLITE_ERROR;
6565}
6566
6567/*
6568** Values for ArCommand.eCmd.
6569*/
6570#define AR_CMD_CREATE       1
6571#define AR_CMD_UPDATE       2
6572#define AR_CMD_INSERT       3
6573#define AR_CMD_EXTRACT      4
6574#define AR_CMD_LIST         5
6575#define AR_CMD_HELP         6
6576#define AR_CMD_REMOVE       7
6577
6578/*
6579** Other (non-command) switches.
6580*/
6581#define AR_SWITCH_VERBOSE     8
6582#define AR_SWITCH_FILE        9
6583#define AR_SWITCH_DIRECTORY  10
6584#define AR_SWITCH_APPEND     11
6585#define AR_SWITCH_DRYRUN     12
6586#define AR_SWITCH_GLOB       13
6587
6588static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6589  switch( eSwitch ){
6590    case AR_CMD_CREATE:
6591    case AR_CMD_EXTRACT:
6592    case AR_CMD_LIST:
6593    case AR_CMD_REMOVE:
6594    case AR_CMD_UPDATE:
6595    case AR_CMD_INSERT:
6596    case AR_CMD_HELP:
6597      if( pAr->eCmd ){
6598        return arErrorMsg(pAr, "multiple command options");
6599      }
6600      pAr->eCmd = eSwitch;
6601      break;
6602
6603    case AR_SWITCH_DRYRUN:
6604      pAr->bDryRun = 1;
6605      break;
6606    case AR_SWITCH_GLOB:
6607      pAr->bGlob = 1;
6608      break;
6609    case AR_SWITCH_VERBOSE:
6610      pAr->bVerbose = 1;
6611      break;
6612    case AR_SWITCH_APPEND:
6613      pAr->bAppend = 1;
6614      /* Fall thru into --file */
6615    case AR_SWITCH_FILE:
6616      pAr->zFile = zArg;
6617      break;
6618    case AR_SWITCH_DIRECTORY:
6619      pAr->zDir = zArg;
6620      break;
6621  }
6622
6623  return SQLITE_OK;
6624}
6625
6626/*
6627** Parse the command line for an ".ar" command. The results are written into
6628** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6629** successfully, otherwise an error message is written to stderr and
6630** SQLITE_ERROR returned.
6631*/
6632static int arParseCommand(
6633  char **azArg,                   /* Array of arguments passed to dot command */
6634  int nArg,                       /* Number of entries in azArg[] */
6635  ArCommand *pAr                  /* Populate this object */
6636){
6637  struct ArSwitch {
6638    const char *zLong;
6639    char cShort;
6640    u8 eSwitch;
6641    u8 bArg;
6642  } aSwitch[] = {
6643    { "create",    'c', AR_CMD_CREATE,       0 },
6644    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6645    { "insert",    'i', AR_CMD_INSERT,       0 },
6646    { "list",      't', AR_CMD_LIST,         0 },
6647    { "remove",    'r', AR_CMD_REMOVE,       0 },
6648    { "update",    'u', AR_CMD_UPDATE,       0 },
6649    { "help",      'h', AR_CMD_HELP,         0 },
6650    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6651    { "file",      'f', AR_SWITCH_FILE,      1 },
6652    { "append",    'a', AR_SWITCH_APPEND,    1 },
6653    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6654    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6655    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6656  };
6657  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6658  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6659
6660  if( nArg<=1 ){
6661    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6662    return arUsage(stderr);
6663  }else{
6664    char *z = azArg[1];
6665    if( z[0]!='-' ){
6666      /* Traditional style [tar] invocation */
6667      int i;
6668      int iArg = 2;
6669      for(i=0; z[i]; i++){
6670        const char *zArg = 0;
6671        struct ArSwitch *pOpt;
6672        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6673          if( z[i]==pOpt->cShort ) break;
6674        }
6675        if( pOpt==pEnd ){
6676          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6677        }
6678        if( pOpt->bArg ){
6679          if( iArg>=nArg ){
6680            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6681          }
6682          zArg = azArg[iArg++];
6683        }
6684        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6685      }
6686      pAr->nArg = nArg-iArg;
6687      if( pAr->nArg>0 ){
6688        pAr->azArg = &azArg[iArg];
6689      }
6690    }else{
6691      /* Non-traditional invocation */
6692      int iArg;
6693      for(iArg=1; iArg<nArg; iArg++){
6694        int n;
6695        z = azArg[iArg];
6696        if( z[0]!='-' ){
6697          /* All remaining command line words are command arguments. */
6698          pAr->azArg = &azArg[iArg];
6699          pAr->nArg = nArg-iArg;
6700          break;
6701        }
6702        n = strlen30(z);
6703
6704        if( z[1]!='-' ){
6705          int i;
6706          /* One or more short options */
6707          for(i=1; i<n; i++){
6708            const char *zArg = 0;
6709            struct ArSwitch *pOpt;
6710            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6711              if( z[i]==pOpt->cShort ) break;
6712            }
6713            if( pOpt==pEnd ){
6714              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6715            }
6716            if( pOpt->bArg ){
6717              if( i<(n-1) ){
6718                zArg = &z[i+1];
6719                i = n;
6720              }else{
6721                if( iArg>=(nArg-1) ){
6722                  return arErrorMsg(pAr, "option requires an argument: %c",
6723                                    z[i]);
6724                }
6725                zArg = azArg[++iArg];
6726              }
6727            }
6728            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6729          }
6730        }else if( z[2]=='\0' ){
6731          /* A -- option, indicating that all remaining command line words
6732          ** are command arguments.  */
6733          pAr->azArg = &azArg[iArg+1];
6734          pAr->nArg = nArg-iArg-1;
6735          break;
6736        }else{
6737          /* A long option */
6738          const char *zArg = 0;             /* Argument for option, if any */
6739          struct ArSwitch *pMatch = 0;      /* Matching option */
6740          struct ArSwitch *pOpt;            /* Iterator */
6741          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6742            const char *zLong = pOpt->zLong;
6743            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6744              if( pMatch ){
6745                return arErrorMsg(pAr, "ambiguous option: %s",z);
6746              }else{
6747                pMatch = pOpt;
6748              }
6749            }
6750          }
6751
6752          if( pMatch==0 ){
6753            return arErrorMsg(pAr, "unrecognized option: %s", z);
6754          }
6755          if( pMatch->bArg ){
6756            if( iArg>=(nArg-1) ){
6757              return arErrorMsg(pAr, "option requires an argument: %s", z);
6758            }
6759            zArg = azArg[++iArg];
6760          }
6761          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6762        }
6763      }
6764    }
6765  }
6766
6767  return SQLITE_OK;
6768}
6769
6770/*
6771** This function assumes that all arguments within the ArCommand.azArg[]
6772** array refer to archive members, as for the --extract, --list or --remove
6773** commands. It checks that each of them are "present". If any specified
6774** file is not present in the archive, an error is printed to stderr and an
6775** error code returned. Otherwise, if all specified arguments are present
6776** in the archive, SQLITE_OK is returned. Here, "present" means either an
6777** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6778** when pAr->bGlob is true.
6779**
6780** This function strips any trailing '/' characters from each argument.
6781** This is consistent with the way the [tar] command seems to work on
6782** Linux.
6783*/
6784static int arCheckEntries(ArCommand *pAr){
6785  int rc = SQLITE_OK;
6786  if( pAr->nArg ){
6787    int i, j;
6788    sqlite3_stmt *pTest = 0;
6789    const char *zSel = (pAr->bGlob)
6790      ? "SELECT name FROM %s WHERE glob($name,name)"
6791      : "SELECT name FROM %s WHERE name=$name";
6792
6793    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6794    j = sqlite3_bind_parameter_index(pTest, "$name");
6795    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6796      char *z = pAr->azArg[i];
6797      int n = strlen30(z);
6798      int bOk = 0;
6799      while( n>0 && z[n-1]=='/' ) n--;
6800      z[n] = '\0';
6801      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6802      if( SQLITE_ROW==sqlite3_step(pTest) ){
6803        bOk = 1;
6804      }
6805      shellReset(&rc, pTest);
6806      if( rc==SQLITE_OK && bOk==0 ){
6807        utf8_printf(stderr, "not found in archive: %s\n", z);
6808        rc = SQLITE_ERROR;
6809      }
6810    }
6811    shellFinalize(&rc, pTest);
6812  }
6813  return rc;
6814}
6815
6816/*
6817** Format a WHERE clause that can be used against the "sqlar" table to
6818** identify all archive members that match the command arguments held
6819** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6820** The caller is responsible for eventually calling sqlite3_free() on
6821** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6822** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6823*/
6824static void arWhereClause(
6825  int *pRc,
6826  ArCommand *pAr,
6827  char **pzWhere                  /* OUT: New WHERE clause */
6828){
6829  char *zWhere = 0;
6830  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6831  if( *pRc==SQLITE_OK ){
6832    if( pAr->nArg==0 ){
6833      zWhere = sqlite3_mprintf("1");
6834    }else{
6835      int i;
6836      const char *zSep = "";
6837      for(i=0; i<pAr->nArg; i++){
6838        const char *z = pAr->azArg[i];
6839        zWhere = sqlite3_mprintf(
6840          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6841          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6842        );
6843        if( zWhere==0 ){
6844          *pRc = SQLITE_NOMEM;
6845          break;
6846        }
6847        zSep = " OR ";
6848      }
6849    }
6850  }
6851  *pzWhere = zWhere;
6852}
6853
6854/*
6855** Implementation of .ar "lisT" command.
6856*/
6857static int arListCommand(ArCommand *pAr){
6858  const char *zSql = "SELECT %s FROM %s WHERE %s";
6859  const char *azCols[] = {
6860    "name",
6861    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6862  };
6863
6864  char *zWhere = 0;
6865  sqlite3_stmt *pSql = 0;
6866  int rc;
6867
6868  rc = arCheckEntries(pAr);
6869  arWhereClause(&rc, pAr, &zWhere);
6870
6871  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6872                     pAr->zSrcTable, zWhere);
6873  if( pAr->bDryRun ){
6874    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6875  }else{
6876    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6877      if( pAr->bVerbose ){
6878        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6879            sqlite3_column_text(pSql, 0),
6880            sqlite3_column_int(pSql, 1),
6881            sqlite3_column_text(pSql, 2),
6882            sqlite3_column_text(pSql, 3)
6883        );
6884      }else{
6885        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6886      }
6887    }
6888  }
6889  shellFinalize(&rc, pSql);
6890  sqlite3_free(zWhere);
6891  return rc;
6892}
6893
6894
6895/*
6896** Implementation of .ar "Remove" command.
6897*/
6898static int arRemoveCommand(ArCommand *pAr){
6899  int rc = 0;
6900  char *zSql = 0;
6901  char *zWhere = 0;
6902
6903  if( pAr->nArg ){
6904    /* Verify that args actually exist within the archive before proceeding.
6905    ** And formulate a WHERE clause to match them.  */
6906    rc = arCheckEntries(pAr);
6907    arWhereClause(&rc, pAr, &zWhere);
6908  }
6909  if( rc==SQLITE_OK ){
6910    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6911                           pAr->zSrcTable, zWhere);
6912    if( pAr->bDryRun ){
6913      utf8_printf(pAr->p->out, "%s\n", zSql);
6914    }else{
6915      char *zErr = 0;
6916      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6917      if( rc==SQLITE_OK ){
6918        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6919        if( rc!=SQLITE_OK ){
6920          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6921        }else{
6922          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6923        }
6924      }
6925      if( zErr ){
6926        utf8_printf(stdout, "ERROR: %s\n", zErr);
6927        sqlite3_free(zErr);
6928      }
6929    }
6930  }
6931  sqlite3_free(zWhere);
6932  sqlite3_free(zSql);
6933  return rc;
6934}
6935
6936/*
6937** Implementation of .ar "eXtract" command.
6938*/
6939static int arExtractCommand(ArCommand *pAr){
6940  const char *zSql1 =
6941    "SELECT "
6942    " ($dir || name),"
6943    " writefile(($dir || name), %s, mode, mtime) "
6944    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6945    " AND name NOT GLOB '*..[/\\]*'";
6946
6947  const char *azExtraArg[] = {
6948    "sqlar_uncompress(data, sz)",
6949    "data"
6950  };
6951
6952  sqlite3_stmt *pSql = 0;
6953  int rc = SQLITE_OK;
6954  char *zDir = 0;
6955  char *zWhere = 0;
6956  int i, j;
6957
6958  /* If arguments are specified, check that they actually exist within
6959  ** the archive before proceeding. And formulate a WHERE clause to
6960  ** match them.  */
6961  rc = arCheckEntries(pAr);
6962  arWhereClause(&rc, pAr, &zWhere);
6963
6964  if( rc==SQLITE_OK ){
6965    if( pAr->zDir ){
6966      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6967    }else{
6968      zDir = sqlite3_mprintf("");
6969    }
6970    if( zDir==0 ) rc = SQLITE_NOMEM;
6971  }
6972
6973  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6974      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6975  );
6976
6977  if( rc==SQLITE_OK ){
6978    j = sqlite3_bind_parameter_index(pSql, "$dir");
6979    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6980
6981    /* Run the SELECT statement twice. The first time, writefile() is called
6982    ** for all archive members that should be extracted. The second time,
6983    ** only for the directories. This is because the timestamps for
6984    ** extracted directories must be reset after they are populated (as
6985    ** populating them changes the timestamp).  */
6986    for(i=0; i<2; i++){
6987      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6988      sqlite3_bind_int(pSql, j, i);
6989      if( pAr->bDryRun ){
6990        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6991      }else{
6992        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6993          if( i==0 && pAr->bVerbose ){
6994            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6995          }
6996        }
6997      }
6998      shellReset(&rc, pSql);
6999    }
7000    shellFinalize(&rc, pSql);
7001  }
7002
7003  sqlite3_free(zDir);
7004  sqlite3_free(zWhere);
7005  return rc;
7006}
7007
7008/*
7009** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
7010*/
7011static int arExecSql(ArCommand *pAr, const char *zSql){
7012  int rc;
7013  if( pAr->bDryRun ){
7014    utf8_printf(pAr->p->out, "%s\n", zSql);
7015    rc = SQLITE_OK;
7016  }else{
7017    char *zErr = 0;
7018    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7019    if( zErr ){
7020      utf8_printf(stdout, "ERROR: %s\n", zErr);
7021      sqlite3_free(zErr);
7022    }
7023  }
7024  return rc;
7025}
7026
7027
7028/*
7029** Implementation of .ar "create", "insert", and "update" commands.
7030**
7031**     create    ->     Create a new SQL archive
7032**     insert    ->     Insert or reinsert all files listed
7033**     update    ->     Insert files that have changed or that were not
7034**                      previously in the archive
7035**
7036** Create the "sqlar" table in the database if it does not already exist.
7037** Then add each file in the azFile[] array to the archive. Directories
7038** are added recursively. If argument bVerbose is non-zero, a message is
7039** printed on stdout for each file archived.
7040**
7041** The create command is the same as update, except that it drops
7042** any existing "sqlar" table before beginning.  The "insert" command
7043** always overwrites every file named on the command-line, where as
7044** "update" only overwrites if the size or mtime or mode has changed.
7045*/
7046static int arCreateOrUpdateCommand(
7047  ArCommand *pAr,                 /* Command arguments and options */
7048  int bUpdate,                    /* true for a --create. */
7049  int bOnlyIfChanged              /* Only update if file has changed */
7050){
7051  const char *zCreate =
7052      "CREATE TABLE IF NOT EXISTS sqlar(\n"
7053      "  name TEXT PRIMARY KEY,  -- name of the file\n"
7054      "  mode INT,               -- access permissions\n"
7055      "  mtime INT,              -- last modification time\n"
7056      "  sz INT,                 -- original file size\n"
7057      "  data BLOB               -- compressed content\n"
7058      ")";
7059  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7060  const char *zInsertFmt[2] = {
7061     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7062     "  SELECT\n"
7063     "    %s,\n"
7064     "    mode,\n"
7065     "    mtime,\n"
7066     "    CASE substr(lsmode(mode),1,1)\n"
7067     "      WHEN '-' THEN length(data)\n"
7068     "      WHEN 'd' THEN 0\n"
7069     "      ELSE -1 END,\n"
7070     "    sqlar_compress(data)\n"
7071     "  FROM fsdir(%Q,%Q) AS disk\n"
7072     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7073     ,
7074     "REPLACE INTO %s(name,mode,mtime,data)\n"
7075     "  SELECT\n"
7076     "    %s,\n"
7077     "    mode,\n"
7078     "    mtime,\n"
7079     "    data\n"
7080     "  FROM fsdir(%Q,%Q) AS disk\n"
7081     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7082  };
7083  int i;                          /* For iterating through azFile[] */
7084  int rc;                         /* Return code */
7085  const char *zTab = 0;           /* SQL table into which to insert */
7086  char *zSql;
7087  char zTemp[50];
7088  char *zExists = 0;
7089
7090  arExecSql(pAr, "PRAGMA page_size=512");
7091  rc = arExecSql(pAr, "SAVEPOINT ar;");
7092  if( rc!=SQLITE_OK ) return rc;
7093  zTemp[0] = 0;
7094  if( pAr->bZip ){
7095    /* Initialize the zipfile virtual table, if necessary */
7096    if( pAr->zFile ){
7097      sqlite3_uint64 r;
7098      sqlite3_randomness(sizeof(r),&r);
7099      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7100      zTab = zTemp;
7101      zSql = sqlite3_mprintf(
7102         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7103         zTab, pAr->zFile
7104      );
7105      rc = arExecSql(pAr, zSql);
7106      sqlite3_free(zSql);
7107    }else{
7108      zTab = "zip";
7109    }
7110  }else{
7111    /* Initialize the table for an SQLAR */
7112    zTab = "sqlar";
7113    if( bUpdate==0 ){
7114      rc = arExecSql(pAr, zDrop);
7115      if( rc!=SQLITE_OK ) goto end_ar_transaction;
7116    }
7117    rc = arExecSql(pAr, zCreate);
7118  }
7119  if( bOnlyIfChanged ){
7120    zExists = sqlite3_mprintf(
7121      " AND NOT EXISTS("
7122          "SELECT 1 FROM %s AS mem"
7123          " WHERE mem.name=disk.name"
7124          " AND mem.mtime=disk.mtime"
7125          " AND mem.mode=disk.mode)", zTab);
7126  }else{
7127    zExists = sqlite3_mprintf("");
7128  }
7129  if( zExists==0 ) rc = SQLITE_NOMEM;
7130  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7131    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7132        pAr->bVerbose ? "shell_putsnl(name)" : "name",
7133        pAr->azArg[i], pAr->zDir, zExists);
7134    rc = arExecSql(pAr, zSql2);
7135    sqlite3_free(zSql2);
7136  }
7137end_ar_transaction:
7138  if( rc!=SQLITE_OK ){
7139    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7140  }else{
7141    rc = arExecSql(pAr, "RELEASE ar;");
7142    if( pAr->bZip && pAr->zFile ){
7143      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7144      arExecSql(pAr, zSql);
7145      sqlite3_free(zSql);
7146    }
7147  }
7148  sqlite3_free(zExists);
7149  return rc;
7150}
7151
7152/*
7153** Implementation of ".ar" dot command.
7154*/
7155static int arDotCommand(
7156  ShellState *pState,          /* Current shell tool state */
7157  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
7158  char **azArg,                /* Array of arguments passed to dot command */
7159  int nArg                     /* Number of entries in azArg[] */
7160){
7161  ArCommand cmd;
7162  int rc;
7163  memset(&cmd, 0, sizeof(cmd));
7164  cmd.fromCmdLine = fromCmdLine;
7165  rc = arParseCommand(azArg, nArg, &cmd);
7166  if( rc==SQLITE_OK ){
7167    int eDbType = SHELL_OPEN_UNSPEC;
7168    cmd.p = pState;
7169    cmd.db = pState->db;
7170    if( cmd.zFile ){
7171      eDbType = deduceDatabaseType(cmd.zFile, 1);
7172    }else{
7173      eDbType = pState->openMode;
7174    }
7175    if( eDbType==SHELL_OPEN_ZIPFILE ){
7176      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7177        if( cmd.zFile==0 ){
7178          cmd.zSrcTable = sqlite3_mprintf("zip");
7179        }else{
7180          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7181        }
7182      }
7183      cmd.bZip = 1;
7184    }else if( cmd.zFile ){
7185      int flags;
7186      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7187      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7188           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7189        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7190      }else{
7191        flags = SQLITE_OPEN_READONLY;
7192      }
7193      cmd.db = 0;
7194      if( cmd.bDryRun ){
7195        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7196             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7197      }
7198      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7199             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7200      if( rc!=SQLITE_OK ){
7201        utf8_printf(stderr, "cannot open file: %s (%s)\n",
7202            cmd.zFile, sqlite3_errmsg(cmd.db)
7203        );
7204        goto end_ar_command;
7205      }
7206      sqlite3_fileio_init(cmd.db, 0, 0);
7207      sqlite3_sqlar_init(cmd.db, 0, 0);
7208      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7209                              shellPutsFunc, 0, 0);
7210
7211    }
7212    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7213      if( cmd.eCmd!=AR_CMD_CREATE
7214       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7215      ){
7216        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7217        rc = SQLITE_ERROR;
7218        goto end_ar_command;
7219      }
7220      cmd.zSrcTable = sqlite3_mprintf("sqlar");
7221    }
7222
7223    switch( cmd.eCmd ){
7224      case AR_CMD_CREATE:
7225        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7226        break;
7227
7228      case AR_CMD_EXTRACT:
7229        rc = arExtractCommand(&cmd);
7230        break;
7231
7232      case AR_CMD_LIST:
7233        rc = arListCommand(&cmd);
7234        break;
7235
7236      case AR_CMD_HELP:
7237        arUsage(pState->out);
7238        break;
7239
7240      case AR_CMD_INSERT:
7241        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7242        break;
7243
7244      case AR_CMD_REMOVE:
7245        rc = arRemoveCommand(&cmd);
7246        break;
7247
7248      default:
7249        assert( cmd.eCmd==AR_CMD_UPDATE );
7250        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7251        break;
7252    }
7253  }
7254end_ar_command:
7255  if( cmd.db!=pState->db ){
7256    close_db(cmd.db);
7257  }
7258  sqlite3_free(cmd.zSrcTable);
7259
7260  return rc;
7261}
7262/* End of the ".archive" or ".ar" command logic
7263*******************************************************************************/
7264#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7265
7266#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7267/*
7268** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
7269** Otherwise, the SQL statement or statements in zSql are executed using
7270** database connection db and the error code written to *pRc before
7271** this function returns.
7272*/
7273static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
7274  int rc = *pRc;
7275  if( rc==SQLITE_OK ){
7276    char *zErr = 0;
7277    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
7278    if( rc!=SQLITE_OK ){
7279      raw_printf(stderr, "SQL error: %s\n", zErr);
7280    }
7281    sqlite3_free(zErr);
7282    *pRc = rc;
7283  }
7284}
7285
7286/*
7287** Like shellExec(), except that zFmt is a printf() style format string.
7288*/
7289static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
7290  char *z = 0;
7291  if( *pRc==SQLITE_OK ){
7292    va_list ap;
7293    va_start(ap, zFmt);
7294    z = sqlite3_vmprintf(zFmt, ap);
7295    va_end(ap);
7296    if( z==0 ){
7297      *pRc = SQLITE_NOMEM;
7298    }else{
7299      shellExec(db, pRc, z);
7300    }
7301    sqlite3_free(z);
7302  }
7303}
7304
7305/*
7306** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7307** Otherwise, an attempt is made to allocate, zero and return a pointer
7308** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
7309** to SQLITE_NOMEM and NULL returned.
7310*/
7311static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
7312  void *pRet = 0;
7313  if( *pRc==SQLITE_OK ){
7314    pRet = sqlite3_malloc64(nByte);
7315    if( pRet==0 ){
7316      *pRc = SQLITE_NOMEM;
7317    }else{
7318      memset(pRet, 0, nByte);
7319    }
7320  }
7321  return pRet;
7322}
7323
7324/*
7325** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7326** Otherwise, zFmt is treated as a printf() style string. The result of
7327** formatting it along with any trailing arguments is written into a
7328** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
7329** It is the responsibility of the caller to eventually free this buffer
7330** using a call to sqlite3_free().
7331**
7332** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
7333** pointer returned.
7334*/
7335static char *shellMPrintf(int *pRc, const char *zFmt, ...){
7336  char *z = 0;
7337  if( *pRc==SQLITE_OK ){
7338    va_list ap;
7339    va_start(ap, zFmt);
7340    z = sqlite3_vmprintf(zFmt, ap);
7341    va_end(ap);
7342    if( z==0 ){
7343      *pRc = SQLITE_NOMEM;
7344    }
7345  }
7346  return z;
7347}
7348
7349
7350/*
7351** When running the ".recover" command, each output table, and the special
7352** orphaned row table if it is required, is represented by an instance
7353** of the following struct.
7354*/
7355typedef struct RecoverTable RecoverTable;
7356struct RecoverTable {
7357  char *zQuoted;                  /* Quoted version of table name */
7358  int nCol;                       /* Number of columns in table */
7359  char **azlCol;                  /* Array of column lists */
7360  int iPk;                        /* Index of IPK column */
7361};
7362
7363/*
7364** Free a RecoverTable object allocated by recoverFindTable() or
7365** recoverOrphanTable().
7366*/
7367static void recoverFreeTable(RecoverTable *pTab){
7368  if( pTab ){
7369    sqlite3_free(pTab->zQuoted);
7370    if( pTab->azlCol ){
7371      int i;
7372      for(i=0; i<=pTab->nCol; i++){
7373        sqlite3_free(pTab->azlCol[i]);
7374      }
7375      sqlite3_free(pTab->azlCol);
7376    }
7377    sqlite3_free(pTab);
7378  }
7379}
7380
7381/*
7382** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
7383** Otherwise, it allocates and returns a RecoverTable object based on the
7384** final four arguments passed to this function. It is the responsibility
7385** of the caller to eventually free the returned object using
7386** recoverFreeTable().
7387*/
7388static RecoverTable *recoverNewTable(
7389  int *pRc,                       /* IN/OUT: Error code */
7390  const char *zName,              /* Name of table */
7391  const char *zSql,               /* CREATE TABLE statement */
7392  int bIntkey,
7393  int nCol
7394){
7395  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
7396  int rc = *pRc;
7397  RecoverTable *pTab = 0;
7398
7399  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
7400  if( rc==SQLITE_OK ){
7401    int nSqlCol = 0;
7402    int bSqlIntkey = 0;
7403    sqlite3_stmt *pStmt = 0;
7404
7405    rc = sqlite3_open("", &dbtmp);
7406    if( rc==SQLITE_OK ){
7407      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
7408                              shellIdQuote, 0, 0);
7409    }
7410    if( rc==SQLITE_OK ){
7411      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
7412    }
7413    if( rc==SQLITE_OK ){
7414      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
7415      if( rc==SQLITE_ERROR ){
7416        rc = SQLITE_OK;
7417        goto finished;
7418      }
7419    }
7420    shellPreparePrintf(dbtmp, &rc, &pStmt,
7421        "SELECT count(*) FROM pragma_table_info(%Q)", zName
7422    );
7423    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7424      nSqlCol = sqlite3_column_int(pStmt, 0);
7425    }
7426    shellFinalize(&rc, pStmt);
7427
7428    if( rc!=SQLITE_OK || nSqlCol<nCol ){
7429      goto finished;
7430    }
7431
7432    shellPreparePrintf(dbtmp, &rc, &pStmt,
7433      "SELECT ("
7434      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
7435      ") FROM sqlite_schema WHERE name = %Q", zName
7436    );
7437    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7438      bSqlIntkey = sqlite3_column_int(pStmt, 0);
7439    }
7440    shellFinalize(&rc, pStmt);
7441
7442    if( bIntkey==bSqlIntkey ){
7443      int i;
7444      const char *zPk = "_rowid_";
7445      sqlite3_stmt *pPkFinder = 0;
7446
7447      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
7448      ** set zPk to the name of the PK column, and pTab->iPk to the index
7449      ** of the column, where columns are 0-numbered from left to right.
7450      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7451      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7452      pTab->iPk = -2;
7453      if( bIntkey ){
7454        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7455          "SELECT cid, name FROM pragma_table_info(%Q) "
7456          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7457          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7458          , zName, zName
7459        );
7460        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7461          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7462          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7463          if( zPk==0 ){ zPk = "_";  /* Defensive.  Should never happen */ }
7464        }
7465      }
7466
7467      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7468      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7469      pTab->nCol = nSqlCol;
7470
7471      if( bIntkey ){
7472        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7473      }else{
7474        pTab->azlCol[0] = shellMPrintf(&rc, "");
7475      }
7476      i = 1;
7477      shellPreparePrintf(dbtmp, &rc, &pStmt,
7478          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7479          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7480          "FROM pragma_table_info(%Q)",
7481          bIntkey ? ", " : "", pTab->iPk,
7482          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7483          zName
7484      );
7485      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7486        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7487        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7488        i++;
7489      }
7490      shellFinalize(&rc, pStmt);
7491
7492      shellFinalize(&rc, pPkFinder);
7493    }
7494  }
7495
7496 finished:
7497  sqlite3_close(dbtmp);
7498  *pRc = rc;
7499  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7500    recoverFreeTable(pTab);
7501    pTab = 0;
7502  }
7503  return pTab;
7504}
7505
7506/*
7507** This function is called to search the schema recovered from the
7508** sqlite_schema table of the (possibly) corrupt database as part
7509** of a ".recover" command. Specifically, for a table with root page
7510** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7511** table must be a WITHOUT ROWID table, or if non-zero, not one of
7512** those.
7513**
7514** If a table is found, a (RecoverTable*) object is returned. Or, if
7515** no such table is found, but bIntkey is false and iRoot is the
7516** root page of an index in the recovered schema, then (*pbNoop) is
7517** set to true and NULL returned. Or, if there is no such table or
7518** index, NULL is returned and (*pbNoop) set to 0, indicating that
7519** the caller should write data to the orphans table.
7520*/
7521static RecoverTable *recoverFindTable(
7522  ShellState *pState,             /* Shell state object */
7523  int *pRc,                       /* IN/OUT: Error code */
7524  int iRoot,                      /* Root page of table */
7525  int bIntkey,                    /* True for an intkey table */
7526  int nCol,                       /* Number of columns in table */
7527  int *pbNoop                     /* OUT: True if iRoot is root of index */
7528){
7529  sqlite3_stmt *pStmt = 0;
7530  RecoverTable *pRet = 0;
7531  int bNoop = 0;
7532  const char *zSql = 0;
7533  const char *zName = 0;
7534
7535  /* Search the recovered schema for an object with root page iRoot. */
7536  shellPreparePrintf(pState->db, pRc, &pStmt,
7537      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7538  );
7539  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7540    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7541    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7542      bNoop = 1;
7543      break;
7544    }
7545    if( sqlite3_stricmp(zType, "table")==0 ){
7546      zName = (const char*)sqlite3_column_text(pStmt, 1);
7547      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7548      if( zName!=0 && zSql!=0 ){
7549        pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7550        break;
7551      }
7552    }
7553  }
7554
7555  shellFinalize(pRc, pStmt);
7556  *pbNoop = bNoop;
7557  return pRet;
7558}
7559
7560/*
7561** Return a RecoverTable object representing the orphans table.
7562*/
7563static RecoverTable *recoverOrphanTable(
7564  ShellState *pState,             /* Shell state object */
7565  int *pRc,                       /* IN/OUT: Error code */
7566  const char *zLostAndFound,      /* Base name for orphans table */
7567  int nCol                        /* Number of user data columns */
7568){
7569  RecoverTable *pTab = 0;
7570  if( nCol>=0 && *pRc==SQLITE_OK ){
7571    int i;
7572
7573    /* This block determines the name of the orphan table. The prefered
7574    ** name is zLostAndFound. But if that clashes with another name
7575    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7576    ** and so on until a non-clashing name is found.  */
7577    int iTab = 0;
7578    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7579    sqlite3_stmt *pTest = 0;
7580    shellPrepare(pState->db, pRc,
7581        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7582    );
7583    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7584    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7585      shellReset(pRc, pTest);
7586      sqlite3_free(zTab);
7587      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7588      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7589    }
7590    shellFinalize(pRc, pTest);
7591
7592    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7593    if( pTab ){
7594      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7595      pTab->nCol = nCol;
7596      pTab->iPk = -2;
7597      if( nCol>0 ){
7598        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7599        if( pTab->azlCol ){
7600          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7601          for(i=nCol-1; i>=0; i--){
7602            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7603          }
7604        }
7605      }
7606
7607      if( *pRc!=SQLITE_OK ){
7608        recoverFreeTable(pTab);
7609        pTab = 0;
7610      }else{
7611        raw_printf(pState->out,
7612            "CREATE TABLE %s(rootpgno INTEGER, "
7613            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7614        );
7615        for(i=0; i<nCol; i++){
7616          raw_printf(pState->out, ", c%d", i);
7617        }
7618        raw_printf(pState->out, ");\n");
7619      }
7620    }
7621    sqlite3_free(zTab);
7622  }
7623  return pTab;
7624}
7625
7626/*
7627** This function is called to recover data from the database. A script
7628** to construct a new database containing all recovered data is output
7629** on stream pState->out.
7630*/
7631static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7632  int rc = SQLITE_OK;
7633  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7634  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7635  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7636  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7637  const char *zLostAndFound = "lost_and_found";
7638  int i;
7639  int nOrphan = -1;
7640  RecoverTable *pOrphan = 0;
7641
7642  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7643  int bRowids = 1;                /* 0 if --no-rowids */
7644  for(i=1; i<nArg; i++){
7645    char *z = azArg[i];
7646    int n;
7647    if( z[0]=='-' && z[1]=='-' ) z++;
7648    n = strlen30(z);
7649    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7650      bFreelist = 0;
7651    }else
7652    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7653      i++;
7654      zRecoveryDb = azArg[i];
7655    }else
7656    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7657      i++;
7658      zLostAndFound = azArg[i];
7659    }else
7660    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7661      bRowids = 0;
7662    }
7663    else{
7664      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7665      showHelp(pState->out, azArg[0]);
7666      return 1;
7667    }
7668  }
7669
7670  shellExecPrintf(pState->db, &rc,
7671    /* Attach an in-memory database named 'recovery'. Create an indexed
7672    ** cache of the sqlite_dbptr virtual table. */
7673    "PRAGMA writable_schema = on;"
7674    "ATTACH %Q AS recovery;"
7675    "DROP TABLE IF EXISTS recovery.dbptr;"
7676    "DROP TABLE IF EXISTS recovery.freelist;"
7677    "DROP TABLE IF EXISTS recovery.map;"
7678    "DROP TABLE IF EXISTS recovery.schema;"
7679    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7680  );
7681
7682  if( bFreelist ){
7683    shellExec(pState->db, &rc,
7684      "WITH trunk(pgno) AS ("
7685      "  SELECT shell_int32("
7686      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7687      "      WHERE x>0"
7688      "    UNION"
7689      "  SELECT shell_int32("
7690      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7691      "      FROM trunk WHERE x>0"
7692      "),"
7693      "freelist(data, n, freepgno) AS ("
7694      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7695      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7696      "    UNION ALL"
7697      "  SELECT data, n-1, shell_int32(data, 2+n) "
7698      "      FROM freelist WHERE n>=0"
7699      ")"
7700      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7701    );
7702  }
7703
7704  /* If this is an auto-vacuum database, add all pointer-map pages to
7705  ** the freelist table. Do this regardless of whether or not
7706  ** --freelist-corrupt was specified.  */
7707  shellExec(pState->db, &rc,
7708    "WITH ptrmap(pgno) AS ("
7709    "  SELECT 2 WHERE shell_int32("
7710    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7711    "  )"
7712    "    UNION ALL "
7713    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7714    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7715    ")"
7716    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7717  );
7718
7719  shellExec(pState->db, &rc,
7720    "CREATE TABLE recovery.dbptr("
7721    "      pgno, child, PRIMARY KEY(child, pgno)"
7722    ") WITHOUT ROWID;"
7723    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7724    "    SELECT * FROM sqlite_dbptr"
7725    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7726
7727    /* Delete any pointer to page 1. This ensures that page 1 is considered
7728    ** a root page, regardless of how corrupt the db is. */
7729    "DELETE FROM recovery.dbptr WHERE child = 1;"
7730
7731    /* Delete all pointers to any pages that have more than one pointer
7732    ** to them. Such pages will be treated as root pages when recovering
7733    ** data.  */
7734    "DELETE FROM recovery.dbptr WHERE child IN ("
7735    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7736    ");"
7737
7738    /* Create the "map" table that will (eventually) contain instructions
7739    ** for dealing with each page in the db that contains one or more
7740    ** records. */
7741    "CREATE TABLE recovery.map("
7742      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7743    ");"
7744
7745    /* Populate table [map]. If there are circular loops of pages in the
7746    ** database, the following adds all pages in such a loop to the map
7747    ** as individual root pages. This could be handled better.  */
7748    "WITH pages(i, maxlen) AS ("
7749    "  SELECT page_count, ("
7750    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7751    "  ) FROM pragma_page_count WHERE page_count>0"
7752    "    UNION ALL"
7753    "  SELECT i-1, ("
7754    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7755    "  ) FROM pages WHERE i>=2"
7756    ")"
7757    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7758    "  SELECT i, maxlen, NULL, ("
7759    "    WITH p(orig, pgno, parent) AS ("
7760    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7761    "        UNION "
7762    "      SELECT i, p.parent, "
7763    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7764    "    )"
7765    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7766    ") "
7767    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7768    "UPDATE recovery.map AS o SET intkey = ("
7769    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7770    ");"
7771
7772    /* Extract data from page 1 and any linked pages into table
7773    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7774    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7775    "INSERT INTO recovery.schema SELECT "
7776    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7777    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7778    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7779    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7780    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7781    "FROM sqlite_dbdata WHERE pgno IN ("
7782    "  SELECT pgno FROM recovery.map WHERE root=1"
7783    ")"
7784    "GROUP BY pgno, cell;"
7785    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7786  );
7787
7788  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7789  ** CREATE TABLE statements that extracted from the existing schema.  */
7790  if( rc==SQLITE_OK ){
7791    sqlite3_stmt *pStmt = 0;
7792    /* ".recover" might output content in an order which causes immediate
7793    ** foreign key constraints to be violated. So disable foreign-key
7794    ** constraint enforcement to prevent problems when running the output
7795    ** script. */
7796    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7797    raw_printf(pState->out, "BEGIN;\n");
7798    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7799    shellPrepare(pState->db, &rc,
7800        "SELECT sql FROM recovery.schema "
7801        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7802    );
7803    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7804      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7805      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7806          &zCreateTable[12]
7807      );
7808    }
7809    shellFinalize(&rc, pStmt);
7810  }
7811
7812  /* Figure out if an orphan table will be required. And if so, how many
7813  ** user columns it should contain */
7814  shellPrepare(pState->db, &rc,
7815      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7816      , &pLoop
7817  );
7818  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7819    nOrphan = sqlite3_column_int(pLoop, 0);
7820  }
7821  shellFinalize(&rc, pLoop);
7822  pLoop = 0;
7823
7824  shellPrepare(pState->db, &rc,
7825      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7826  );
7827
7828  shellPrepare(pState->db, &rc,
7829      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7830      "(case when (? AND field<0) then NULL else value end)"
7831      "), ', ')"
7832      ", min(field) "
7833      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7834      "GROUP BY cell", &pCells
7835  );
7836
7837  /* Loop through each root page. */
7838  shellPrepare(pState->db, &rc,
7839      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7840      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7841      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7842      ")", &pLoop
7843  );
7844  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7845    int iRoot = sqlite3_column_int(pLoop, 0);
7846    int bIntkey = sqlite3_column_int(pLoop, 1);
7847    int nCol = sqlite3_column_int(pLoop, 2);
7848    int bNoop = 0;
7849    RecoverTable *pTab;
7850
7851    assert( bIntkey==0 || bIntkey==1 );
7852    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7853    if( bNoop || rc ) continue;
7854    if( pTab==0 ){
7855      if( pOrphan==0 ){
7856        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7857      }
7858      pTab = pOrphan;
7859      if( pTab==0 ) break;
7860    }
7861
7862    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7863      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7864    }
7865    sqlite3_bind_int(pPages, 1, iRoot);
7866    if( bRowids==0 && pTab->iPk<0 ){
7867      sqlite3_bind_int(pCells, 1, 1);
7868    }else{
7869      sqlite3_bind_int(pCells, 1, 0);
7870    }
7871    sqlite3_bind_int(pCells, 3, pTab->iPk);
7872
7873    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7874      int iPgno = sqlite3_column_int(pPages, 0);
7875      sqlite3_bind_int(pCells, 2, iPgno);
7876      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7877        int nField = sqlite3_column_int(pCells, 0);
7878        int iMin = sqlite3_column_int(pCells, 2);
7879        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7880
7881        RecoverTable *pTab2 = pTab;
7882        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7883          if( pOrphan==0 ){
7884            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7885          }
7886          pTab2 = pOrphan;
7887          if( pTab2==0 ) break;
7888        }
7889
7890        nField = nField+1;
7891        if( pTab2==pOrphan ){
7892          raw_printf(pState->out,
7893              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7894              pTab2->zQuoted, iRoot, iPgno, nField,
7895              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7896          );
7897        }else{
7898          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7899              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7900          );
7901        }
7902      }
7903      shellReset(&rc, pCells);
7904    }
7905    shellReset(&rc, pPages);
7906    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7907  }
7908  shellFinalize(&rc, pLoop);
7909  shellFinalize(&rc, pPages);
7910  shellFinalize(&rc, pCells);
7911  recoverFreeTable(pOrphan);
7912
7913  /* The rest of the schema */
7914  if( rc==SQLITE_OK ){
7915    sqlite3_stmt *pStmt = 0;
7916    shellPrepare(pState->db, &rc,
7917        "SELECT sql, name FROM recovery.schema "
7918        "WHERE sql NOT LIKE 'create table%'", &pStmt
7919    );
7920    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7921      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7922      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7923        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7924        char *zPrint = shellMPrintf(&rc,
7925          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7926          zName, zName, zSql
7927        );
7928        raw_printf(pState->out, "%s;\n", zPrint);
7929        sqlite3_free(zPrint);
7930      }else{
7931        raw_printf(pState->out, "%s;\n", zSql);
7932      }
7933    }
7934    shellFinalize(&rc, pStmt);
7935  }
7936
7937  if( rc==SQLITE_OK ){
7938    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7939    raw_printf(pState->out, "COMMIT;\n");
7940  }
7941  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7942  return rc;
7943}
7944#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7945
7946
7947/*
7948 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7949 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7950 *   close db and set it to 0, and return the columns spec, to later
7951 *   be sqlite3_free()'ed by the caller.
7952 * The return is 0 when either:
7953 *   (a) The db was not initialized and zCol==0 (There are no columns.)
7954 *   (b) zCol!=0  (Column was added, db initialized as needed.)
7955 * The 3rd argument, pRenamed, references an out parameter. If the
7956 * pointer is non-zero, its referent will be set to a summary of renames
7957 * done if renaming was necessary, or set to 0 if none was done. The out
7958 * string (if any) must be sqlite3_free()'ed by the caller.
7959 */
7960#ifdef SHELL_DEBUG
7961#define rc_err_oom_die(rc) \
7962  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7963  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7964    fprintf(stderr,"E:%d\n",rc), assert(0)
7965#else
7966static void rc_err_oom_die(int rc){
7967  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7968  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7969}
7970#endif
7971
7972#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7973static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7974#else  /* Otherwise, memory is faster/better for the transient DB. */
7975static const char *zCOL_DB = ":memory:";
7976#endif
7977
7978/* Define character (as C string) to separate generated column ordinal
7979 * from protected part of incoming column names. This defaults to "_"
7980 * so that incoming column identifiers that did not need not be quoted
7981 * remain usable without being quoted. It must be one character.
7982 */
7983#ifndef SHELL_AUTOCOLUMN_SEP
7984# define AUTOCOLUMN_SEP "_"
7985#else
7986# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7987#endif
7988
7989static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
7990  /* Queries and D{D,M}L used here */
7991  static const char * const zTabMake = "\
7992CREATE TABLE ColNames(\
7993 cpos INTEGER PRIMARY KEY,\
7994 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7995CREATE VIEW RepeatedNames AS \
7996SELECT DISTINCT t.name FROM ColNames t \
7997WHERE t.name COLLATE NOCASE IN (\
7998 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7999);\
8000";
8001  static const char * const zTabFill = "\
8002INSERT INTO ColNames(name,nlen,chop,reps,suff)\
8003 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
8004";
8005  static const char * const zHasDupes = "\
8006SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
8007 <count(name) FROM ColNames\
8008";
8009#ifdef SHELL_COLUMN_RENAME_CLEAN
8010  static const char * const zDedoctor = "\
8011UPDATE ColNames SET chop=iif(\
8012  (substring(name,nlen,1) BETWEEN '0' AND '9')\
8013  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
8014 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
8015 0\
8016)\
8017";
8018#endif
8019  static const char * const zSetReps = "\
8020UPDATE ColNames AS t SET reps=\
8021(SELECT count(*) FROM ColNames d \
8022 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
8023 COLLATE NOCASE\
8024)\
8025";
8026#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8027  static const char * const zColDigits = "\
8028SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8029";
8030#else
8031  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8032  static const char * const zColDigits = "\
8033SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8034 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8035 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8036";
8037#endif
8038  static const char * const zRenameRank =
8039#ifdef SHELL_COLUMN_RENAME_CLEAN
8040    "UPDATE ColNames AS t SET suff="
8041    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
8042#else /* ...RENAME_MINIMAL_ONE_PASS */
8043"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8044"  SELECT 0 AS nlz"
8045"  UNION"
8046"  SELECT nlz+1 AS nlz FROM Lzn"
8047"  WHERE EXISTS("
8048"   SELECT 1"
8049"   FROM ColNames t, ColNames o"
8050"   WHERE"
8051"    iif(t.name IN (SELECT * FROM RepeatedNames),"
8052"     printf('%s"AUTOCOLUMN_SEP"%s',"
8053"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8054"     t.name"
8055"    )"
8056"    ="
8057"    iif(o.name IN (SELECT * FROM RepeatedNames),"
8058"     printf('%s"AUTOCOLUMN_SEP"%s',"
8059"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8060"     o.name"
8061"    )"
8062"    COLLATE NOCASE"
8063"    AND o.cpos<>t.cpos"
8064"   GROUP BY t.cpos"
8065"  )"
8066") UPDATE Colnames AS t SET"
8067" chop = 0," /* No chopping, never touch incoming names. */
8068" suff = iif(name IN (SELECT * FROM RepeatedNames),"
8069"  printf('"AUTOCOLUMN_SEP"%s', substring("
8070"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8071"  ''"
8072" )"
8073#endif
8074    ;
8075  static const char * const zCollectVar = "\
8076SELECT\
8077 '('||x'0a'\
8078 || group_concat(\
8079  cname||' TEXT',\
8080  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8081 ||')' AS ColsSpec \
8082FROM (\
8083 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8084 FROM ColNames ORDER BY cpos\
8085)";
8086  static const char * const zRenamesDone =
8087    "SELECT group_concat("
8088    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8089    " ','||x'0a')"
8090    "FROM ColNames WHERE suff<>'' OR chop!=0"
8091    ;
8092  int rc;
8093  sqlite3_stmt *pStmt = 0;
8094  assert(pDb!=0);
8095  if( zColNew ){
8096    /* Add initial or additional column. Init db if necessary. */
8097    if( *pDb==0 ){
8098      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
8099#ifdef SHELL_COLFIX_DB
8100      if(*zCOL_DB!=':')
8101        sqlite3_exec(*pDb,"drop table if exists ColNames;"
8102                     "drop view if exists RepeatedNames;",0,0,0);
8103#endif
8104      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
8105      rc_err_oom_die(rc);
8106    }
8107    assert(*pDb!=0);
8108    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
8109    rc_err_oom_die(rc);
8110    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
8111    rc_err_oom_die(rc);
8112    rc = sqlite3_step(pStmt);
8113    rc_err_oom_die(rc);
8114    sqlite3_finalize(pStmt);
8115    return 0;
8116  }else if( *pDb==0 ){
8117    return 0;
8118  }else{
8119    /* Formulate the columns spec, close the DB, zero *pDb. */
8120    char *zColsSpec = 0;
8121    int hasDupes = db_int(*pDb, zHasDupes);
8122    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
8123    if( hasDupes ){
8124#ifdef SHELL_COLUMN_RENAME_CLEAN
8125      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
8126      rc_err_oom_die(rc);
8127#endif
8128      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
8129      rc_err_oom_die(rc);
8130      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
8131      rc_err_oom_die(rc);
8132      sqlite3_bind_int(pStmt, 1, nDigits);
8133      rc = sqlite3_step(pStmt);
8134      sqlite3_finalize(pStmt);
8135      assert(rc==SQLITE_DONE);
8136    }
8137    assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
8138    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
8139    rc_err_oom_die(rc);
8140    rc = sqlite3_step(pStmt);
8141    if( rc==SQLITE_ROW ){
8142      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8143    }else{
8144      zColsSpec = 0;
8145    }
8146    if( pzRenamed!=0 ){
8147      if( !hasDupes ) *pzRenamed = 0;
8148      else{
8149        sqlite3_finalize(pStmt);
8150        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
8151            && SQLITE_ROW==sqlite3_step(pStmt) ){
8152          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8153        }else
8154          *pzRenamed = 0;
8155      }
8156    }
8157    sqlite3_finalize(pStmt);
8158    sqlite3_close(*pDb);
8159    *pDb = 0;
8160    return zColsSpec;
8161  }
8162}
8163
8164/*
8165** If an input line begins with "." then invoke this routine to
8166** process that line.
8167**
8168** Return 1 on error, 2 to exit, and 0 otherwise.
8169*/
8170static int do_meta_command(char *zLine, ShellState *p){
8171  int h = 1;
8172  int nArg = 0;
8173  int n, c;
8174  int rc = 0;
8175  char *azArg[52];
8176
8177#ifndef SQLITE_OMIT_VIRTUALTABLE
8178  if( p->expert.pExpert ){
8179    expertFinish(p, 1, 0);
8180  }
8181#endif
8182
8183  /* Parse the input line into tokens.
8184  */
8185  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
8186    while( IsSpace(zLine[h]) ){ h++; }
8187    if( zLine[h]==0 ) break;
8188    if( zLine[h]=='\'' || zLine[h]=='"' ){
8189      int delim = zLine[h++];
8190      azArg[nArg++] = &zLine[h];
8191      while( zLine[h] && zLine[h]!=delim ){
8192        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
8193        h++;
8194      }
8195      if( zLine[h]==delim ){
8196        zLine[h++] = 0;
8197      }
8198      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
8199    }else{
8200      azArg[nArg++] = &zLine[h];
8201      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
8202      if( zLine[h] ) zLine[h++] = 0;
8203      resolve_backslashes(azArg[nArg-1]);
8204    }
8205  }
8206  azArg[nArg] = 0;
8207
8208  /* Process the input line.
8209  */
8210  if( nArg==0 ) return 0; /* no tokens, no error */
8211  n = strlen30(azArg[0]);
8212  c = azArg[0][0];
8213  clearTempFile(p);
8214
8215#ifndef SQLITE_OMIT_AUTHORIZATION
8216  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
8217    if( nArg!=2 ){
8218      raw_printf(stderr, "Usage: .auth ON|OFF\n");
8219      rc = 1;
8220      goto meta_command_exit;
8221    }
8222    open_db(p, 0);
8223    if( booleanValue(azArg[1]) ){
8224      sqlite3_set_authorizer(p->db, shellAuth, p);
8225    }else if( p->bSafeModePersist ){
8226      sqlite3_set_authorizer(p->db, safeModeAuth, p);
8227    }else{
8228      sqlite3_set_authorizer(p->db, 0, 0);
8229    }
8230  }else
8231#endif
8232
8233#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8234  && !defined(SQLITE_SHELL_FIDDLE)
8235  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
8236    open_db(p, 0);
8237    failIfSafeMode(p, "cannot run .archive in safe mode");
8238    rc = arDotCommand(p, 0, azArg, nArg);
8239  }else
8240#endif
8241
8242#ifndef SQLITE_SHELL_FIDDLE
8243  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
8244   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
8245  ){
8246    const char *zDestFile = 0;
8247    const char *zDb = 0;
8248    sqlite3 *pDest;
8249    sqlite3_backup *pBackup;
8250    int j;
8251    int bAsync = 0;
8252    const char *zVfs = 0;
8253    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8254    for(j=1; j<nArg; j++){
8255      const char *z = azArg[j];
8256      if( z[0]=='-' ){
8257        if( z[1]=='-' ) z++;
8258        if( strcmp(z, "-append")==0 ){
8259          zVfs = "apndvfs";
8260        }else
8261        if( strcmp(z, "-async")==0 ){
8262          bAsync = 1;
8263        }else
8264        {
8265          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
8266          return 1;
8267        }
8268      }else if( zDestFile==0 ){
8269        zDestFile = azArg[j];
8270      }else if( zDb==0 ){
8271        zDb = zDestFile;
8272        zDestFile = azArg[j];
8273      }else{
8274        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8275        return 1;
8276      }
8277    }
8278    if( zDestFile==0 ){
8279      raw_printf(stderr, "missing FILENAME argument on .backup\n");
8280      return 1;
8281    }
8282    if( zDb==0 ) zDb = "main";
8283    rc = sqlite3_open_v2(zDestFile, &pDest,
8284                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8285    if( rc!=SQLITE_OK ){
8286      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
8287      close_db(pDest);
8288      return 1;
8289    }
8290    if( bAsync ){
8291      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8292                   0, 0, 0);
8293    }
8294    open_db(p, 0);
8295    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8296    if( pBackup==0 ){
8297      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8298      close_db(pDest);
8299      return 1;
8300    }
8301    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8302    sqlite3_backup_finish(pBackup);
8303    if( rc==SQLITE_DONE ){
8304      rc = 0;
8305    }else{
8306      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8307      rc = 1;
8308    }
8309    close_db(pDest);
8310  }else
8311#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8312
8313  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
8314    if( nArg==2 ){
8315      bail_on_error = booleanValue(azArg[1]);
8316    }else{
8317      raw_printf(stderr, "Usage: .bail on|off\n");
8318      rc = 1;
8319    }
8320  }else
8321
8322  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
8323    if( nArg==2 ){
8324      if( booleanValue(azArg[1]) ){
8325        setBinaryMode(p->out, 1);
8326      }else{
8327        setTextMode(p->out, 1);
8328      }
8329    }else{
8330      raw_printf(stderr, "Usage: .binary on|off\n");
8331      rc = 1;
8332    }
8333  }else
8334
8335  /* The undocumented ".breakpoint" command causes a call to the no-op
8336  ** routine named test_breakpoint().
8337  */
8338  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
8339    test_breakpoint();
8340  }else
8341
8342#ifndef SQLITE_SHELL_FIDDLE
8343  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
8344    failIfSafeMode(p, "cannot run .cd in safe mode");
8345    if( nArg==2 ){
8346#if defined(_WIN32) || defined(WIN32)
8347      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8348      rc = !SetCurrentDirectoryW(z);
8349      sqlite3_free(z);
8350#else
8351      rc = chdir(azArg[1]);
8352#endif
8353      if( rc ){
8354        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
8355        rc = 1;
8356      }
8357    }else{
8358      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
8359      rc = 1;
8360    }
8361  }else
8362#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8363
8364  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
8365    if( nArg==2 ){
8366      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8367    }else{
8368      raw_printf(stderr, "Usage: .changes on|off\n");
8369      rc = 1;
8370    }
8371  }else
8372
8373#ifndef SQLITE_SHELL_FIDDLE
8374  /* Cancel output redirection, if it is currently set (by .testcase)
8375  ** Then read the content of the testcase-out.txt file and compare against
8376  ** azArg[1].  If there are differences, report an error and exit.
8377  */
8378  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
8379    char *zRes = 0;
8380    output_reset(p);
8381    if( nArg!=2 ){
8382      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
8383      rc = 2;
8384    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8385      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
8386      rc = 2;
8387    }else if( testcase_glob(azArg[1],zRes)==0 ){
8388      utf8_printf(stderr,
8389                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
8390                 p->zTestcase, azArg[1], zRes);
8391      rc = 1;
8392    }else{
8393      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
8394      p->nCheck++;
8395    }
8396    sqlite3_free(zRes);
8397  }else
8398#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8399
8400#ifndef SQLITE_SHELL_FIDDLE
8401  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
8402    failIfSafeMode(p, "cannot run .clone in safe mode");
8403    if( nArg==2 ){
8404      tryToClone(p, azArg[1]);
8405    }else{
8406      raw_printf(stderr, "Usage: .clone FILENAME\n");
8407      rc = 1;
8408    }
8409  }else
8410#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8411
8412  if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
8413    if( nArg==1 ){
8414      /* List available connections */
8415      int i;
8416      for(i=0; i<ArraySize(p->aAuxDb); i++){
8417        const char *zFile = p->aAuxDb[i].zDbFilename;
8418        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8419          zFile = "(not open)";
8420        }else if( zFile==0 ){
8421          zFile = "(memory)";
8422        }else if( zFile[0]==0 ){
8423          zFile = "(temporary-file)";
8424        }
8425        if( p->pAuxDb == &p->aAuxDb[i] ){
8426          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
8427        }else if( p->aAuxDb[i].db!=0 ){
8428          utf8_printf(stdout, "       %d: %s\n", i, zFile);
8429        }
8430      }
8431    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8432      int i = azArg[1][0] - '0';
8433      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8434        p->pAuxDb->db = p->db;
8435        p->pAuxDb = &p->aAuxDb[i];
8436        globalDb = p->db = p->pAuxDb->db;
8437        p->pAuxDb->db = 0;
8438      }
8439    }else if( nArg==3 && strcmp(azArg[1], "close")==0
8440           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8441      int i = azArg[2][0] - '0';
8442      if( i<0 || i>=ArraySize(p->aAuxDb) ){
8443        /* No-op */
8444      }else if( p->pAuxDb == &p->aAuxDb[i] ){
8445        raw_printf(stderr, "cannot close the active database connection\n");
8446        rc = 1;
8447      }else if( p->aAuxDb[i].db ){
8448        session_close_all(p, i);
8449        close_db(p->aAuxDb[i].db);
8450        p->aAuxDb[i].db = 0;
8451      }
8452    }else{
8453      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
8454      rc = 1;
8455    }
8456  }else
8457
8458  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
8459    char **azName = 0;
8460    int nName = 0;
8461    sqlite3_stmt *pStmt;
8462    int i;
8463    open_db(p, 0);
8464    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8465    if( rc ){
8466      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8467      rc = 1;
8468    }else{
8469      while( sqlite3_step(pStmt)==SQLITE_ROW ){
8470        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8471        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8472        if( zSchema==0 || zFile==0 ) continue;
8473        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8474        shell_check_oom(azName);
8475        azName[nName*2] = strdup(zSchema);
8476        azName[nName*2+1] = strdup(zFile);
8477        nName++;
8478      }
8479    }
8480    sqlite3_finalize(pStmt);
8481    for(i=0; i<nName; i++){
8482      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8483      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8484      const char *z = azName[i*2+1];
8485      utf8_printf(p->out, "%s: %s %s%s\n",
8486         azName[i*2],
8487         z && z[0] ? z : "\"\"",
8488         bRdonly ? "r/o" : "r/w",
8489         eTxn==SQLITE_TXN_NONE ? "" :
8490            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8491      free(azName[i*2]);
8492      free(azName[i*2+1]);
8493    }
8494    sqlite3_free(azName);
8495  }else
8496
8497  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
8498    static const struct DbConfigChoices {
8499      const char *zName;
8500      int op;
8501    } aDbConfig[] = {
8502        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
8503        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
8504        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
8505        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
8506        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
8507        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
8508        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
8509        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8510        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
8511        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
8512        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8513        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
8514        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
8515        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
8516        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
8517        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
8518    };
8519    int ii, v;
8520    open_db(p, 0);
8521    for(ii=0; ii<ArraySize(aDbConfig); ii++){
8522      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8523      if( nArg>=3 ){
8524        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8525      }
8526      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8527      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
8528      if( nArg>1 ) break;
8529    }
8530    if( nArg>1 && ii==ArraySize(aDbConfig) ){
8531      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
8532      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
8533    }
8534  }else
8535
8536#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8537  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
8538    rc = shell_dbinfo_command(p, nArg, azArg);
8539  }else
8540
8541  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
8542    open_db(p, 0);
8543    rc = recoverDatabaseCmd(p, nArg, azArg);
8544  }else
8545#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
8546
8547  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
8548    char *zLike = 0;
8549    char *zSql;
8550    int i;
8551    int savedShowHeader = p->showHeader;
8552    int savedShellFlags = p->shellFlgs;
8553    ShellClearFlag(p,
8554       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8555       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8556    for(i=1; i<nArg; i++){
8557      if( azArg[i][0]=='-' ){
8558        const char *z = azArg[i]+1;
8559        if( z[0]=='-' ) z++;
8560        if( strcmp(z,"preserve-rowids")==0 ){
8561#ifdef SQLITE_OMIT_VIRTUALTABLE
8562          raw_printf(stderr, "The --preserve-rowids option is not compatible"
8563                             " with SQLITE_OMIT_VIRTUALTABLE\n");
8564          rc = 1;
8565          sqlite3_free(zLike);
8566          goto meta_command_exit;
8567#else
8568          ShellSetFlag(p, SHFLG_PreserveRowid);
8569#endif
8570        }else
8571        if( strcmp(z,"newlines")==0 ){
8572          ShellSetFlag(p, SHFLG_Newlines);
8573        }else
8574        if( strcmp(z,"data-only")==0 ){
8575          ShellSetFlag(p, SHFLG_DumpDataOnly);
8576        }else
8577        if( strcmp(z,"nosys")==0 ){
8578          ShellSetFlag(p, SHFLG_DumpNoSys);
8579        }else
8580        {
8581          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8582          rc = 1;
8583          sqlite3_free(zLike);
8584          goto meta_command_exit;
8585        }
8586      }else{
8587        /* azArg[i] contains a LIKE pattern. This ".dump" request should
8588        ** only dump data for tables for which either the table name matches
8589        ** the LIKE pattern, or the table appears to be a shadow table of
8590        ** a virtual table for which the name matches the LIKE pattern.
8591        */
8592        char *zExpr = sqlite3_mprintf(
8593            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8594            "  SELECT 1 FROM sqlite_schema WHERE "
8595            "    name LIKE %Q ESCAPE '\\' AND"
8596            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8597            "    substr(o.name, 1, length(name)+1) == (name||'_')"
8598            ")", azArg[i], azArg[i]
8599        );
8600
8601        if( zLike ){
8602          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8603        }else{
8604          zLike = zExpr;
8605        }
8606      }
8607    }
8608
8609    open_db(p, 0);
8610
8611    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8612      /* When playing back a "dump", the content might appear in an order
8613      ** which causes immediate foreign key constraints to be violated.
8614      ** So disable foreign-key constraint enforcement to prevent problems. */
8615      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8616      raw_printf(p->out, "BEGIN TRANSACTION;\n");
8617    }
8618    p->writableSchema = 0;
8619    p->showHeader = 0;
8620    /* Set writable_schema=ON since doing so forces SQLite to initialize
8621    ** as much of the schema as it can even if the sqlite_schema table is
8622    ** corrupt. */
8623    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8624    p->nErr = 0;
8625    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8626    zSql = sqlite3_mprintf(
8627      "SELECT name, type, sql FROM sqlite_schema AS o "
8628      "WHERE (%s) AND type=='table'"
8629      "  AND sql NOT NULL"
8630      " ORDER BY tbl_name='sqlite_sequence', rowid",
8631      zLike
8632    );
8633    run_schema_dump_query(p,zSql);
8634    sqlite3_free(zSql);
8635    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8636      zSql = sqlite3_mprintf(
8637        "SELECT sql FROM sqlite_schema AS o "
8638        "WHERE (%s) AND sql NOT NULL"
8639        "  AND type IN ('index','trigger','view')",
8640        zLike
8641      );
8642      run_table_dump_query(p, zSql);
8643      sqlite3_free(zSql);
8644    }
8645    sqlite3_free(zLike);
8646    if( p->writableSchema ){
8647      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8648      p->writableSchema = 0;
8649    }
8650    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8651    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8652    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8653      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8654    }
8655    p->showHeader = savedShowHeader;
8656    p->shellFlgs = savedShellFlags;
8657  }else
8658
8659  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
8660    if( nArg==2 ){
8661      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8662    }else{
8663      raw_printf(stderr, "Usage: .echo on|off\n");
8664      rc = 1;
8665    }
8666  }else
8667
8668  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
8669    if( nArg==2 ){
8670      p->autoEQPtest = 0;
8671      if( p->autoEQPtrace ){
8672        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8673        p->autoEQPtrace = 0;
8674      }
8675      if( strcmp(azArg[1],"full")==0 ){
8676        p->autoEQP = AUTOEQP_full;
8677      }else if( strcmp(azArg[1],"trigger")==0 ){
8678        p->autoEQP = AUTOEQP_trigger;
8679#ifdef SQLITE_DEBUG
8680      }else if( strcmp(azArg[1],"test")==0 ){
8681        p->autoEQP = AUTOEQP_on;
8682        p->autoEQPtest = 1;
8683      }else if( strcmp(azArg[1],"trace")==0 ){
8684        p->autoEQP = AUTOEQP_full;
8685        p->autoEQPtrace = 1;
8686        open_db(p, 0);
8687        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8688        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8689#endif
8690      }else{
8691        p->autoEQP = (u8)booleanValue(azArg[1]);
8692      }
8693    }else{
8694      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8695      rc = 1;
8696    }
8697  }else
8698
8699#ifndef SQLITE_SHELL_FIDDLE
8700  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
8701    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8702    rc = 2;
8703  }else
8704#endif
8705
8706  /* The ".explain" command is automatic now.  It is largely pointless.  It
8707  ** retained purely for backwards compatibility */
8708  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
8709    int val = 1;
8710    if( nArg>=2 ){
8711      if( strcmp(azArg[1],"auto")==0 ){
8712        val = 99;
8713      }else{
8714        val =  booleanValue(azArg[1]);
8715      }
8716    }
8717    if( val==1 && p->mode!=MODE_Explain ){
8718      p->normalMode = p->mode;
8719      p->mode = MODE_Explain;
8720      p->autoExplain = 0;
8721    }else if( val==0 ){
8722      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8723      p->autoExplain = 0;
8724    }else if( val==99 ){
8725      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8726      p->autoExplain = 1;
8727    }
8728  }else
8729
8730#ifndef SQLITE_OMIT_VIRTUALTABLE
8731  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
8732    if( p->bSafeMode ){
8733      raw_printf(stderr,
8734        "Cannot run experimental commands such as \"%s\" in safe mode\n",
8735        azArg[0]);
8736      rc = 1;
8737    }else{
8738      open_db(p, 0);
8739      expertDotCommand(p, azArg, nArg);
8740    }
8741  }else
8742#endif
8743
8744  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
8745    static const struct {
8746       const char *zCtrlName;   /* Name of a test-control option */
8747       int ctrlCode;            /* Integer code for that option */
8748       const char *zUsage;      /* Usage notes */
8749    } aCtrl[] = {
8750      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8751      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8752      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8753      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8754      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8755   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8756      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8757      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8758      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8759      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8760   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8761    };
8762    int filectrl = -1;
8763    int iCtrl = -1;
8764    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8765    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8766    int n2, i;
8767    const char *zCmd = 0;
8768    const char *zSchema = 0;
8769
8770    open_db(p, 0);
8771    zCmd = nArg>=2 ? azArg[1] : "help";
8772
8773    if( zCmd[0]=='-'
8774     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
8775     && nArg>=4
8776    ){
8777      zSchema = azArg[2];
8778      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8779      nArg -= 2;
8780      zCmd = azArg[1];
8781    }
8782
8783    /* The argument can optionally begin with "-" or "--" */
8784    if( zCmd[0]=='-' && zCmd[1] ){
8785      zCmd++;
8786      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8787    }
8788
8789    /* --help lists all file-controls */
8790    if( strcmp(zCmd,"help")==0 ){
8791      utf8_printf(p->out, "Available file-controls:\n");
8792      for(i=0; i<ArraySize(aCtrl); i++){
8793        utf8_printf(p->out, "  .filectrl %s %s\n",
8794                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8795      }
8796      rc = 1;
8797      goto meta_command_exit;
8798    }
8799
8800    /* convert filectrl text option to value. allow any unique prefix
8801    ** of the option name, or a numerical value. */
8802    n2 = strlen30(zCmd);
8803    for(i=0; i<ArraySize(aCtrl); i++){
8804      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8805        if( filectrl<0 ){
8806          filectrl = aCtrl[i].ctrlCode;
8807          iCtrl = i;
8808        }else{
8809          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8810                              "Use \".filectrl --help\" for help\n", zCmd);
8811          rc = 1;
8812          goto meta_command_exit;
8813        }
8814      }
8815    }
8816    if( filectrl<0 ){
8817      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8818                         "Use \".filectrl --help\" for help\n", zCmd);
8819    }else{
8820      switch(filectrl){
8821        case SQLITE_FCNTL_SIZE_LIMIT: {
8822          if( nArg!=2 && nArg!=3 ) break;
8823          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8824          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8825          isOk = 1;
8826          break;
8827        }
8828        case SQLITE_FCNTL_LOCK_TIMEOUT:
8829        case SQLITE_FCNTL_CHUNK_SIZE: {
8830          int x;
8831          if( nArg!=3 ) break;
8832          x = (int)integerValue(azArg[2]);
8833          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8834          isOk = 2;
8835          break;
8836        }
8837        case SQLITE_FCNTL_PERSIST_WAL:
8838        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8839          int x;
8840          if( nArg!=2 && nArg!=3 ) break;
8841          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8842          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8843          iRes = x;
8844          isOk = 1;
8845          break;
8846        }
8847        case SQLITE_FCNTL_DATA_VERSION:
8848        case SQLITE_FCNTL_HAS_MOVED: {
8849          int x;
8850          if( nArg!=2 ) break;
8851          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8852          iRes = x;
8853          isOk = 1;
8854          break;
8855        }
8856        case SQLITE_FCNTL_TEMPFILENAME: {
8857          char *z = 0;
8858          if( nArg!=2 ) break;
8859          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8860          if( z ){
8861            utf8_printf(p->out, "%s\n", z);
8862            sqlite3_free(z);
8863          }
8864          isOk = 2;
8865          break;
8866        }
8867        case SQLITE_FCNTL_RESERVE_BYTES: {
8868          int x;
8869          if( nArg>=3 ){
8870            x = atoi(azArg[2]);
8871            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8872          }
8873          x = -1;
8874          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8875          utf8_printf(p->out,"%d\n", x);
8876          isOk = 2;
8877          break;
8878        }
8879      }
8880    }
8881    if( isOk==0 && iCtrl>=0 ){
8882      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8883      rc = 1;
8884    }else if( isOk==1 ){
8885      char zBuf[100];
8886      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8887      raw_printf(p->out, "%s\n", zBuf);
8888    }
8889  }else
8890
8891  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8892    ShellState data;
8893    int doStats = 0;
8894    memcpy(&data, p, sizeof(data));
8895    data.showHeader = 0;
8896    data.cMode = data.mode = MODE_Semi;
8897    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8898      data.cMode = data.mode = MODE_Pretty;
8899      nArg = 1;
8900    }
8901    if( nArg!=1 ){
8902      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8903      rc = 1;
8904      goto meta_command_exit;
8905    }
8906    open_db(p, 0);
8907    rc = sqlite3_exec(p->db,
8908       "SELECT sql FROM"
8909       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8910       "     FROM sqlite_schema UNION ALL"
8911       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8912       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8913       "ORDER BY x",
8914       callback, &data, 0
8915    );
8916    if( rc==SQLITE_OK ){
8917      sqlite3_stmt *pStmt;
8918      rc = sqlite3_prepare_v2(p->db,
8919               "SELECT rowid FROM sqlite_schema"
8920               " WHERE name GLOB 'sqlite_stat[134]'",
8921               -1, &pStmt, 0);
8922      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8923      sqlite3_finalize(pStmt);
8924    }
8925    if( doStats==0 ){
8926      raw_printf(p->out, "/* No STAT tables available */\n");
8927    }else{
8928      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8929      data.cMode = data.mode = MODE_Insert;
8930      data.zDestTable = "sqlite_stat1";
8931      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8932      data.zDestTable = "sqlite_stat4";
8933      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8934      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8935    }
8936  }else
8937
8938  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8939    if( nArg==2 ){
8940      p->showHeader = booleanValue(azArg[1]);
8941      p->shellFlgs |= SHFLG_HeaderSet;
8942    }else{
8943      raw_printf(stderr, "Usage: .headers on|off\n");
8944      rc = 1;
8945    }
8946  }else
8947
8948  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8949    if( nArg>=2 ){
8950      n = showHelp(p->out, azArg[1]);
8951      if( n==0 ){
8952        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8953      }
8954    }else{
8955      showHelp(p->out, 0);
8956    }
8957  }else
8958
8959#ifndef SQLITE_SHELL_FIDDLE
8960  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8961    char *zTable = 0;           /* Insert data into this table */
8962    char *zSchema = 0;          /* within this schema (may default to "main") */
8963    char *zFile = 0;            /* Name of file to extra content from */
8964    sqlite3_stmt *pStmt = NULL; /* A statement */
8965    int nCol;                   /* Number of columns in the table */
8966    int nByte;                  /* Number of bytes in an SQL string */
8967    int i, j;                   /* Loop counters */
8968    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8969    int nSep;                   /* Number of bytes in p->colSeparator[] */
8970    char *zSql;                 /* An SQL statement */
8971    char *zFullTabName;         /* Table name with schema if applicable */
8972    ImportCtx sCtx;             /* Reader context */
8973    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8974    int eVerbose = 0;           /* Larger for more console output */
8975    int nSkip = 0;              /* Initial lines to skip */
8976    int useOutputMode = 1;      /* Use output mode to determine separators */
8977    char *zCreate = 0;          /* CREATE TABLE statement text */
8978
8979    failIfSafeMode(p, "cannot run .import in safe mode");
8980    memset(&sCtx, 0, sizeof(sCtx));
8981    if( p->mode==MODE_Ascii ){
8982      xRead = ascii_read_one_field;
8983    }else{
8984      xRead = csv_read_one_field;
8985    }
8986    rc = 1;
8987    for(i=1; i<nArg; i++){
8988      char *z = azArg[i];
8989      if( z[0]=='-' && z[1]=='-' ) z++;
8990      if( z[0]!='-' ){
8991        if( zFile==0 ){
8992          zFile = z;
8993        }else if( zTable==0 ){
8994          zTable = z;
8995        }else{
8996          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8997          showHelp(p->out, "import");
8998          goto meta_command_exit;
8999        }
9000      }else if( strcmp(z,"-v")==0 ){
9001        eVerbose++;
9002      }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
9003        zSchema = azArg[++i];
9004      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
9005        nSkip = integerValue(azArg[++i]);
9006      }else if( strcmp(z,"-ascii")==0 ){
9007        sCtx.cColSep = SEP_Unit[0];
9008        sCtx.cRowSep = SEP_Record[0];
9009        xRead = ascii_read_one_field;
9010        useOutputMode = 0;
9011      }else if( strcmp(z,"-csv")==0 ){
9012        sCtx.cColSep = ',';
9013        sCtx.cRowSep = '\n';
9014        xRead = csv_read_one_field;
9015        useOutputMode = 0;
9016      }else{
9017        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
9018        showHelp(p->out, "import");
9019        goto meta_command_exit;
9020      }
9021    }
9022    if( zTable==0 ){
9023      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
9024                  zFile==0 ? "FILE" : "TABLE");
9025      showHelp(p->out, "import");
9026      goto meta_command_exit;
9027    }
9028    seenInterrupt = 0;
9029    open_db(p, 0);
9030    if( useOutputMode ){
9031      /* If neither the --csv or --ascii options are specified, then set
9032      ** the column and row separator characters from the output mode. */
9033      nSep = strlen30(p->colSeparator);
9034      if( nSep==0 ){
9035        raw_printf(stderr,
9036                   "Error: non-null column separator required for import\n");
9037        goto meta_command_exit;
9038      }
9039      if( nSep>1 ){
9040        raw_printf(stderr,
9041              "Error: multi-character column separators not allowed"
9042              " for import\n");
9043        goto meta_command_exit;
9044      }
9045      nSep = strlen30(p->rowSeparator);
9046      if( nSep==0 ){
9047        raw_printf(stderr,
9048            "Error: non-null row separator required for import\n");
9049        goto meta_command_exit;
9050      }
9051      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
9052        /* When importing CSV (only), if the row separator is set to the
9053        ** default output row separator, change it to the default input
9054        ** row separator.  This avoids having to maintain different input
9055        ** and output row separators. */
9056        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9057        nSep = strlen30(p->rowSeparator);
9058      }
9059      if( nSep>1 ){
9060        raw_printf(stderr, "Error: multi-character row separators not allowed"
9061                           " for import\n");
9062        goto meta_command_exit;
9063      }
9064      sCtx.cColSep = p->colSeparator[0];
9065      sCtx.cRowSep = p->rowSeparator[0];
9066    }
9067    sCtx.zFile = zFile;
9068    sCtx.nLine = 1;
9069    if( sCtx.zFile[0]=='|' ){
9070#ifdef SQLITE_OMIT_POPEN
9071      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9072      goto meta_command_exit;
9073#else
9074      sCtx.in = popen(sCtx.zFile+1, "r");
9075      sCtx.zFile = "<pipe>";
9076      sCtx.xCloser = pclose;
9077#endif
9078    }else{
9079      sCtx.in = fopen(sCtx.zFile, "rb");
9080      sCtx.xCloser = fclose;
9081    }
9082    if( sCtx.in==0 ){
9083      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
9084      goto meta_command_exit;
9085    }
9086    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
9087      char zSep[2];
9088      zSep[1] = 0;
9089      zSep[0] = sCtx.cColSep;
9090      utf8_printf(p->out, "Column separator ");
9091      output_c_string(p->out, zSep);
9092      utf8_printf(p->out, ", row separator ");
9093      zSep[0] = sCtx.cRowSep;
9094      output_c_string(p->out, zSep);
9095      utf8_printf(p->out, "\n");
9096    }
9097    sCtx.z = sqlite3_malloc64(120);
9098    if( sCtx.z==0 ){
9099      import_cleanup(&sCtx);
9100      shell_out_of_memory();
9101    }
9102    /* Below, resources must be freed before exit. */
9103    while( (nSkip--)>0 ){
9104      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
9105    }
9106    if( zSchema!=0 ){
9107      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
9108    }else{
9109      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
9110    }
9111    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
9112    if( zSql==0 || zFullTabName==0 ){
9113      import_cleanup(&sCtx);
9114      shell_out_of_memory();
9115    }
9116    nByte = strlen30(zSql);
9117    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9118    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
9119    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
9120      sqlite3 *dbCols = 0;
9121      char *zRenames = 0;
9122      char *zColDefs;
9123      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
9124      while( xRead(&sCtx) ){
9125        zAutoColumn(sCtx.z, &dbCols, 0);
9126        if( sCtx.cTerm!=sCtx.cColSep ) break;
9127      }
9128      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
9129      if( zRenames!=0 ){
9130        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
9131                    "Columns renamed during .import %s due to duplicates:\n"
9132                    "%s\n", sCtx.zFile, zRenames);
9133        sqlite3_free(zRenames);
9134      }
9135      assert(dbCols==0);
9136      if( zColDefs==0 ){
9137        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
9138      import_fail:
9139        sqlite3_free(zCreate);
9140        sqlite3_free(zSql);
9141        sqlite3_free(zFullTabName);
9142        import_cleanup(&sCtx);
9143        rc = 1;
9144        goto meta_command_exit;
9145      }
9146      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
9147      if( eVerbose>=1 ){
9148        utf8_printf(p->out, "%s\n", zCreate);
9149      }
9150      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
9151      if( rc ){
9152        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
9153        goto import_fail;
9154      }
9155      sqlite3_free(zCreate);
9156      zCreate = 0;
9157      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9158    }
9159    if( rc ){
9160      if (pStmt) sqlite3_finalize(pStmt);
9161      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
9162      goto import_fail;
9163    }
9164    sqlite3_free(zSql);
9165    nCol = sqlite3_column_count(pStmt);
9166    sqlite3_finalize(pStmt);
9167    pStmt = 0;
9168    if( nCol==0 ) return 0; /* no columns, no error */
9169    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
9170    if( zSql==0 ){
9171      import_cleanup(&sCtx);
9172      shell_out_of_memory();
9173    }
9174    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
9175    j = strlen30(zSql);
9176    for(i=1; i<nCol; i++){
9177      zSql[j++] = ',';
9178      zSql[j++] = '?';
9179    }
9180    zSql[j++] = ')';
9181    zSql[j] = 0;
9182    if( eVerbose>=2 ){
9183      utf8_printf(p->out, "Insert using: %s\n", zSql);
9184    }
9185    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9186    if( rc ){
9187      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9188      if (pStmt) sqlite3_finalize(pStmt);
9189      goto import_fail;
9190    }
9191    sqlite3_free(zSql);
9192    sqlite3_free(zFullTabName);
9193    needCommit = sqlite3_get_autocommit(p->db);
9194    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
9195    do{
9196      int startLine = sCtx.nLine;
9197      for(i=0; i<nCol; i++){
9198        char *z = xRead(&sCtx);
9199        /*
9200        ** Did we reach end-of-file before finding any columns?
9201        ** If so, stop instead of NULL filling the remaining columns.
9202        */
9203        if( z==0 && i==0 ) break;
9204        /*
9205        ** Did we reach end-of-file OR end-of-line before finding any
9206        ** columns in ASCII mode?  If so, stop instead of NULL filling
9207        ** the remaining columns.
9208        */
9209        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
9210        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
9211        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
9212          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9213                          "filling the rest with NULL\n",
9214                          sCtx.zFile, startLine, nCol, i+1);
9215          i += 2;
9216          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9217        }
9218      }
9219      if( sCtx.cTerm==sCtx.cColSep ){
9220        do{
9221          xRead(&sCtx);
9222          i++;
9223        }while( sCtx.cTerm==sCtx.cColSep );
9224        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9225                        "extras ignored\n",
9226                        sCtx.zFile, startLine, nCol, i);
9227      }
9228      if( i>=nCol ){
9229        sqlite3_step(pStmt);
9230        rc = sqlite3_reset(pStmt);
9231        if( rc!=SQLITE_OK ){
9232          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
9233                      startLine, sqlite3_errmsg(p->db));
9234          sCtx.nErr++;
9235        }else{
9236          sCtx.nRow++;
9237        }
9238      }
9239    }while( sCtx.cTerm!=EOF );
9240
9241    import_cleanup(&sCtx);
9242    sqlite3_finalize(pStmt);
9243    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9244    if( eVerbose>0 ){
9245      utf8_printf(p->out,
9246          "Added %d rows with %d errors using %d lines of input\n",
9247          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9248    }
9249  }else
9250#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9251
9252#ifndef SQLITE_UNTESTABLE
9253  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
9254    char *zSql;
9255    char *zCollist = 0;
9256    sqlite3_stmt *pStmt;
9257    int tnum = 0;
9258    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
9259    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9260    int i;
9261    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9262      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
9263                          "       .imposter off\n");
9264      /* Also allowed, but not documented:
9265      **
9266      **    .imposter TABLE IMPOSTER
9267      **
9268      ** where TABLE is a WITHOUT ROWID table.  In that case, the
9269      ** imposter is another WITHOUT ROWID table with the columns in
9270      ** storage order. */
9271      rc = 1;
9272      goto meta_command_exit;
9273    }
9274    open_db(p, 0);
9275    if( nArg==2 ){
9276      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9277      goto meta_command_exit;
9278    }
9279    zSql = sqlite3_mprintf(
9280      "SELECT rootpage, 0 FROM sqlite_schema"
9281      " WHERE name='%q' AND type='index'"
9282      "UNION ALL "
9283      "SELECT rootpage, 1 FROM sqlite_schema"
9284      " WHERE name='%q' AND type='table'"
9285      "   AND sql LIKE '%%without%%rowid%%'",
9286      azArg[1], azArg[1]
9287    );
9288    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9289    sqlite3_free(zSql);
9290    if( sqlite3_step(pStmt)==SQLITE_ROW ){
9291      tnum = sqlite3_column_int(pStmt, 0);
9292      isWO = sqlite3_column_int(pStmt, 1);
9293    }
9294    sqlite3_finalize(pStmt);
9295    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9296    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9297    sqlite3_free(zSql);
9298    i = 0;
9299    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9300      char zLabel[20];
9301      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9302      i++;
9303      if( zCol==0 ){
9304        if( sqlite3_column_int(pStmt,1)==-1 ){
9305          zCol = "_ROWID_";
9306        }else{
9307          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9308          zCol = zLabel;
9309        }
9310      }
9311      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9312        lenPK = (int)strlen(zCollist);
9313      }
9314      if( zCollist==0 ){
9315        zCollist = sqlite3_mprintf("\"%w\"", zCol);
9316      }else{
9317        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9318      }
9319    }
9320    sqlite3_finalize(pStmt);
9321    if( i==0 || tnum==0 ){
9322      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
9323      rc = 1;
9324      sqlite3_free(zCollist);
9325      goto meta_command_exit;
9326    }
9327    if( lenPK==0 ) lenPK = 100000;
9328    zSql = sqlite3_mprintf(
9329          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9330          azArg[2], zCollist, lenPK, zCollist);
9331    sqlite3_free(zCollist);
9332    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9333    if( rc==SQLITE_OK ){
9334      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9335      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9336      if( rc ){
9337        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9338      }else{
9339        utf8_printf(stdout, "%s;\n", zSql);
9340        raw_printf(stdout,
9341          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
9342          azArg[1], isWO ? "table" : "index"
9343        );
9344      }
9345    }else{
9346      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9347      rc = 1;
9348    }
9349    sqlite3_free(zSql);
9350  }else
9351#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9352
9353#ifdef SQLITE_ENABLE_IOTRACE
9354  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
9355    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9356    if( iotrace && iotrace!=stdout ) fclose(iotrace);
9357    iotrace = 0;
9358    if( nArg<2 ){
9359      sqlite3IoTrace = 0;
9360    }else if( strcmp(azArg[1], "-")==0 ){
9361      sqlite3IoTrace = iotracePrintf;
9362      iotrace = stdout;
9363    }else{
9364      iotrace = fopen(azArg[1], "w");
9365      if( iotrace==0 ){
9366        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9367        sqlite3IoTrace = 0;
9368        rc = 1;
9369      }else{
9370        sqlite3IoTrace = iotracePrintf;
9371      }
9372    }
9373  }else
9374#endif
9375
9376  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
9377    static const struct {
9378       const char *zLimitName;   /* Name of a limit */
9379       int limitCode;            /* Integer code for that limit */
9380    } aLimit[] = {
9381      { "length",                SQLITE_LIMIT_LENGTH                    },
9382      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
9383      { "column",                SQLITE_LIMIT_COLUMN                    },
9384      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
9385      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
9386      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
9387      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
9388      { "attached",              SQLITE_LIMIT_ATTACHED                  },
9389      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
9390      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
9391      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
9392      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
9393    };
9394    int i, n2;
9395    open_db(p, 0);
9396    if( nArg==1 ){
9397      for(i=0; i<ArraySize(aLimit); i++){
9398        printf("%20s %d\n", aLimit[i].zLimitName,
9399               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9400      }
9401    }else if( nArg>3 ){
9402      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
9403      rc = 1;
9404      goto meta_command_exit;
9405    }else{
9406      int iLimit = -1;
9407      n2 = strlen30(azArg[1]);
9408      for(i=0; i<ArraySize(aLimit); i++){
9409        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9410          if( iLimit<0 ){
9411            iLimit = i;
9412          }else{
9413            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
9414            rc = 1;
9415            goto meta_command_exit;
9416          }
9417        }
9418      }
9419      if( iLimit<0 ){
9420        utf8_printf(stderr, "unknown limit: \"%s\"\n"
9421                        "enter \".limits\" with no arguments for a list.\n",
9422                         azArg[1]);
9423        rc = 1;
9424        goto meta_command_exit;
9425      }
9426      if( nArg==3 ){
9427        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9428                      (int)integerValue(azArg[2]));
9429      }
9430      printf("%20s %d\n", aLimit[iLimit].zLimitName,
9431             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9432    }
9433  }else
9434
9435  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
9436    open_db(p, 0);
9437    lintDotCommand(p, azArg, nArg);
9438  }else
9439
9440#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9441  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
9442    const char *zFile, *zProc;
9443    char *zErrMsg = 0;
9444    failIfSafeMode(p, "cannot run .load in safe mode");
9445    if( nArg<2 ){
9446      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
9447      rc = 1;
9448      goto meta_command_exit;
9449    }
9450    zFile = azArg[1];
9451    zProc = nArg>=3 ? azArg[2] : 0;
9452    open_db(p, 0);
9453    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9454    if( rc!=SQLITE_OK ){
9455      utf8_printf(stderr, "Error: %s\n", zErrMsg);
9456      sqlite3_free(zErrMsg);
9457      rc = 1;
9458    }
9459  }else
9460#endif
9461
9462#ifndef SQLITE_SHELL_FIDDLE
9463  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
9464    failIfSafeMode(p, "cannot run .log in safe mode");
9465    if( nArg!=2 ){
9466      raw_printf(stderr, "Usage: .log FILENAME\n");
9467      rc = 1;
9468    }else{
9469      const char *zFile = azArg[1];
9470      output_file_close(p->pLog);
9471      p->pLog = output_file_open(zFile, 0);
9472    }
9473  }else
9474#endif
9475
9476  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
9477    const char *zMode = 0;
9478    const char *zTabname = 0;
9479    int i, n2;
9480    ColModeOpts cmOpts = ColModeOpts_default;
9481    for(i=1; i<nArg; i++){
9482      const char *z = azArg[i];
9483      if( optionMatch(z,"wrap") && i+1<nArg ){
9484        cmOpts.iWrap = integerValue(azArg[++i]);
9485      }else if( optionMatch(z,"ww") ){
9486        cmOpts.bWordWrap = 1;
9487      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9488        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9489      }else if( optionMatch(z,"quote") ){
9490        cmOpts.bQuote = 1;
9491      }else if( optionMatch(z,"noquote") ){
9492        cmOpts.bQuote = 0;
9493      }else if( zMode==0 ){
9494        zMode = z;
9495        /* Apply defaults for qbox pseudo-mods. If that
9496         * overwrites already-set values, user was informed of this.
9497         */
9498        if( strcmp(z, "qbox")==0 ){
9499          ColModeOpts cmo = ColModeOpts_default_qbox;
9500          zMode = "box";
9501          cmOpts = cmo;
9502        }
9503      }else if( zTabname==0 ){
9504        zTabname = z;
9505      }else if( z[0]=='-' ){
9506        utf8_printf(stderr, "unknown option: %s\n", z);
9507        utf8_printf(stderr, "options:\n"
9508                            "  --noquote\n"
9509                            "  --quote\n"
9510                            "  --wordwrap on/off\n"
9511                            "  --wrap N\n"
9512                            "  --ww\n");
9513        rc = 1;
9514        goto meta_command_exit;
9515      }else{
9516        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9517        rc = 1;
9518        goto meta_command_exit;
9519      }
9520    }
9521    if( zMode==0 ){
9522      if( p->mode==MODE_Column
9523       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9524      ){
9525        raw_printf
9526          (p->out,
9527           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9528           modeDescr[p->mode], p->cmOpts.iWrap,
9529           p->cmOpts.bWordWrap ? "on" : "off",
9530           p->cmOpts.bQuote ? "" : "no");
9531      }else{
9532        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
9533      }
9534      zMode = modeDescr[p->mode];
9535    }
9536    n2 = strlen30(zMode);
9537    if( strncmp(zMode,"lines",n2)==0 ){
9538      p->mode = MODE_Line;
9539      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9540    }else if( strncmp(zMode,"columns",n2)==0 ){
9541      p->mode = MODE_Column;
9542      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9543        p->showHeader = 1;
9544      }
9545      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9546      p->cmOpts = cmOpts;
9547    }else if( strncmp(zMode,"list",n2)==0 ){
9548      p->mode = MODE_List;
9549      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9550      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9551    }else if( strncmp(zMode,"html",n2)==0 ){
9552      p->mode = MODE_Html;
9553    }else if( strncmp(zMode,"tcl",n2)==0 ){
9554      p->mode = MODE_Tcl;
9555      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9556      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9557    }else if( strncmp(zMode,"csv",n2)==0 ){
9558      p->mode = MODE_Csv;
9559      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9560      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9561    }else if( strncmp(zMode,"tabs",n2)==0 ){
9562      p->mode = MODE_List;
9563      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9564    }else if( strncmp(zMode,"insert",n2)==0 ){
9565      p->mode = MODE_Insert;
9566      set_table_name(p, zTabname ? zTabname : "table");
9567    }else if( strncmp(zMode,"quote",n2)==0 ){
9568      p->mode = MODE_Quote;
9569      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9570      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9571    }else if( strncmp(zMode,"ascii",n2)==0 ){
9572      p->mode = MODE_Ascii;
9573      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9574      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9575    }else if( strncmp(zMode,"markdown",n2)==0 ){
9576      p->mode = MODE_Markdown;
9577      p->cmOpts = cmOpts;
9578    }else if( strncmp(zMode,"table",n2)==0 ){
9579      p->mode = MODE_Table;
9580      p->cmOpts = cmOpts;
9581    }else if( strncmp(zMode,"box",n2)==0 ){
9582      p->mode = MODE_Box;
9583      p->cmOpts = cmOpts;
9584    }else if( strncmp(zMode,"count",n2)==0 ){
9585      p->mode = MODE_Count;
9586    }else if( strncmp(zMode,"off",n2)==0 ){
9587      p->mode = MODE_Off;
9588    }else if( strncmp(zMode,"json",n2)==0 ){
9589      p->mode = MODE_Json;
9590    }else{
9591      raw_printf(stderr, "Error: mode should be one of: "
9592         "ascii box column csv html insert json line list markdown "
9593         "qbox quote table tabs tcl\n");
9594      rc = 1;
9595    }
9596    p->cMode = p->mode;
9597  }else
9598
9599#ifndef SQLITE_SHELL_FIDDLE
9600  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
9601    if( nArg!=2 ){
9602      raw_printf(stderr, "Usage: .nonce NONCE\n");
9603      rc = 1;
9604    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
9605      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9606                 p->lineno, azArg[1]);
9607      exit(1);
9608    }else{
9609      p->bSafeMode = 0;
9610      return 0;  /* Return immediately to bypass the safe mode reset
9611                 ** at the end of this procedure */
9612    }
9613  }else
9614#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9615
9616  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
9617    if( nArg==2 ){
9618      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9619                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9620    }else{
9621      raw_printf(stderr, "Usage: .nullvalue STRING\n");
9622      rc = 1;
9623    }
9624  }else
9625
9626  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
9627    const char *zFN = 0;     /* Pointer to constant filename */
9628    char *zNewFilename = 0;  /* Name of the database file to open */
9629    int iName = 1;           /* Index in azArg[] of the filename */
9630    int newFlag = 0;         /* True to delete file before opening */
9631    int openMode = SHELL_OPEN_UNSPEC;
9632
9633    /* Check for command-line arguments */
9634    for(iName=1; iName<nArg; iName++){
9635      const char *z = azArg[iName];
9636#ifndef SQLITE_SHELL_FIDDLE
9637      if( optionMatch(z,"new") ){
9638        newFlag = 1;
9639#ifdef SQLITE_HAVE_ZLIB
9640      }else if( optionMatch(z, "zip") ){
9641        openMode = SHELL_OPEN_ZIPFILE;
9642#endif
9643      }else if( optionMatch(z, "append") ){
9644        openMode = SHELL_OPEN_APPENDVFS;
9645      }else if( optionMatch(z, "readonly") ){
9646        openMode = SHELL_OPEN_READONLY;
9647      }else if( optionMatch(z, "nofollow") ){
9648        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9649#ifndef SQLITE_OMIT_DESERIALIZE
9650      }else if( optionMatch(z, "deserialize") ){
9651        openMode = SHELL_OPEN_DESERIALIZE;
9652      }else if( optionMatch(z, "hexdb") ){
9653        openMode = SHELL_OPEN_HEXDB;
9654      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9655        p->szMax = integerValue(azArg[++iName]);
9656#endif /* SQLITE_OMIT_DESERIALIZE */
9657      }else
9658#endif /* !SQLITE_SHELL_FIDDLE */
9659      if( z[0]=='-' ){
9660        utf8_printf(stderr, "unknown option: %s\n", z);
9661        rc = 1;
9662        goto meta_command_exit;
9663      }else if( zFN ){
9664        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9665        rc = 1;
9666        goto meta_command_exit;
9667      }else{
9668        zFN = z;
9669      }
9670    }
9671
9672    /* Close the existing database */
9673    session_close_all(p, -1);
9674    close_db(p->db);
9675    p->db = 0;
9676    p->pAuxDb->zDbFilename = 0;
9677    sqlite3_free(p->pAuxDb->zFreeOnClose);
9678    p->pAuxDb->zFreeOnClose = 0;
9679    p->openMode = openMode;
9680    p->openFlags = 0;
9681    p->szMax = 0;
9682
9683    /* If a filename is specified, try to open it first */
9684    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9685      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9686#ifndef SQLITE_SHELL_FIDDLE
9687      if( p->bSafeMode
9688       && p->openMode!=SHELL_OPEN_HEXDB
9689       && zFN
9690       && strcmp(zFN,":memory:")!=0
9691      ){
9692        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9693      }
9694#else
9695      /* WASM mode has its own sandboxed pseudo-filesystem. */
9696#endif
9697      if( zFN ){
9698        zNewFilename = sqlite3_mprintf("%s", zFN);
9699        shell_check_oom(zNewFilename);
9700      }else{
9701        zNewFilename = 0;
9702      }
9703      p->pAuxDb->zDbFilename = zNewFilename;
9704      open_db(p, OPEN_DB_KEEPALIVE);
9705      if( p->db==0 ){
9706        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9707        sqlite3_free(zNewFilename);
9708      }else{
9709        p->pAuxDb->zFreeOnClose = zNewFilename;
9710      }
9711    }
9712    if( p->db==0 ){
9713      /* As a fall-back open a TEMP database */
9714      p->pAuxDb->zDbFilename = 0;
9715      open_db(p, 0);
9716    }
9717  }else
9718
9719#ifndef SQLITE_SHELL_FIDDLE
9720  if( (c=='o'
9721        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
9722   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
9723  ){
9724    char *zFile = 0;
9725    int bTxtMode = 0;
9726    int i;
9727    int eMode = 0;
9728    int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
9729    unsigned char zBOM[4];    /* Byte-order mark to using if --bom is present */
9730
9731    zBOM[0] = 0;
9732    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9733    if( c=='e' ){
9734      eMode = 'x';
9735      bOnce = 2;
9736    }else if( strncmp(azArg[0],"once",n)==0 ){
9737      bOnce = 1;
9738    }
9739    for(i=1; i<nArg; i++){
9740      char *z = azArg[i];
9741      if( z[0]=='-' ){
9742        if( z[1]=='-' ) z++;
9743        if( strcmp(z,"-bom")==0 ){
9744          zBOM[0] = 0xef;
9745          zBOM[1] = 0xbb;
9746          zBOM[2] = 0xbf;
9747          zBOM[3] = 0;
9748        }else if( c!='e' && strcmp(z,"-x")==0 ){
9749          eMode = 'x';  /* spreadsheet */
9750        }else if( c!='e' && strcmp(z,"-e")==0 ){
9751          eMode = 'e';  /* text editor */
9752        }else{
9753          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9754                      azArg[i]);
9755          showHelp(p->out, azArg[0]);
9756          rc = 1;
9757          goto meta_command_exit;
9758        }
9759      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9760        zFile = sqlite3_mprintf("%s", z);
9761        if( zFile && zFile[0]=='|' ){
9762          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9763          break;
9764        }
9765      }else{
9766        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9767                    azArg[i]);
9768        showHelp(p->out, azArg[0]);
9769        rc = 1;
9770        sqlite3_free(zFile);
9771        goto meta_command_exit;
9772      }
9773    }
9774    if( zFile==0 ){
9775      zFile = sqlite3_mprintf("stdout");
9776    }
9777    if( bOnce ){
9778      p->outCount = 2;
9779    }else{
9780      p->outCount = 0;
9781    }
9782    output_reset(p);
9783#ifndef SQLITE_NOHAVE_SYSTEM
9784    if( eMode=='e' || eMode=='x' ){
9785      p->doXdgOpen = 1;
9786      outputModePush(p);
9787      if( eMode=='x' ){
9788        /* spreadsheet mode.  Output as CSV. */
9789        newTempFile(p, "csv");
9790        ShellClearFlag(p, SHFLG_Echo);
9791        p->mode = MODE_Csv;
9792        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9793        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9794      }else{
9795        /* text editor mode */
9796        newTempFile(p, "txt");
9797        bTxtMode = 1;
9798      }
9799      sqlite3_free(zFile);
9800      zFile = sqlite3_mprintf("%s", p->zTempFile);
9801    }
9802#endif /* SQLITE_NOHAVE_SYSTEM */
9803    shell_check_oom(zFile);
9804    if( zFile[0]=='|' ){
9805#ifdef SQLITE_OMIT_POPEN
9806      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9807      rc = 1;
9808      p->out = stdout;
9809#else
9810      p->out = popen(zFile + 1, "w");
9811      if( p->out==0 ){
9812        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9813        p->out = stdout;
9814        rc = 1;
9815      }else{
9816        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9817        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9818      }
9819#endif
9820    }else{
9821      p->out = output_file_open(zFile, bTxtMode);
9822      if( p->out==0 ){
9823        if( strcmp(zFile,"off")!=0 ){
9824          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9825        }
9826        p->out = stdout;
9827        rc = 1;
9828      } else {
9829        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9830        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9831      }
9832    }
9833    sqlite3_free(zFile);
9834  }else
9835#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9836
9837  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
9838    open_db(p,0);
9839    if( nArg<=1 ) goto parameter_syntax_error;
9840
9841    /* .parameter clear
9842    ** Clear all bind parameters by dropping the TEMP table that holds them.
9843    */
9844    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
9845      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9846                   0, 0, 0);
9847    }else
9848
9849    /* .parameter list
9850    ** List all bind parameters.
9851    */
9852    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
9853      sqlite3_stmt *pStmt = 0;
9854      int rx;
9855      int len = 0;
9856      rx = sqlite3_prepare_v2(p->db,
9857             "SELECT max(length(key)) "
9858             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9859      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9860        len = sqlite3_column_int(pStmt, 0);
9861        if( len>40 ) len = 40;
9862      }
9863      sqlite3_finalize(pStmt);
9864      pStmt = 0;
9865      if( len ){
9866        rx = sqlite3_prepare_v2(p->db,
9867             "SELECT key, quote(value) "
9868             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9869        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9870          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9871                      sqlite3_column_text(pStmt,1));
9872        }
9873        sqlite3_finalize(pStmt);
9874      }
9875    }else
9876
9877    /* .parameter init
9878    ** Make sure the TEMP table used to hold bind parameters exists.
9879    ** Create it if necessary.
9880    */
9881    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
9882      bind_table_init(p);
9883    }else
9884
9885    /* .parameter set NAME VALUE
9886    ** Set or reset a bind parameter.  NAME should be the full parameter
9887    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9888    ** VALUE can be in either SQL literal notation, or if not it will be
9889    ** understood to be a text string.
9890    */
9891    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
9892      int rx;
9893      char *zSql;
9894      sqlite3_stmt *pStmt;
9895      const char *zKey = azArg[2];
9896      const char *zValue = azArg[3];
9897      bind_table_init(p);
9898      zSql = sqlite3_mprintf(
9899                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9900                  "VALUES(%Q,%s);", zKey, zValue);
9901      shell_check_oom(zSql);
9902      pStmt = 0;
9903      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9904      sqlite3_free(zSql);
9905      if( rx!=SQLITE_OK ){
9906        sqlite3_finalize(pStmt);
9907        pStmt = 0;
9908        zSql = sqlite3_mprintf(
9909                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9910                   "VALUES(%Q,%Q);", zKey, zValue);
9911        shell_check_oom(zSql);
9912        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9913        sqlite3_free(zSql);
9914        if( rx!=SQLITE_OK ){
9915          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9916          sqlite3_finalize(pStmt);
9917          pStmt = 0;
9918          rc = 1;
9919        }
9920      }
9921      sqlite3_step(pStmt);
9922      sqlite3_finalize(pStmt);
9923    }else
9924
9925    /* .parameter unset NAME
9926    ** Remove the NAME binding from the parameter binding table, if it
9927    ** exists.
9928    */
9929    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
9930      char *zSql = sqlite3_mprintf(
9931          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9932      shell_check_oom(zSql);
9933      sqlite3_exec(p->db, zSql, 0, 0, 0);
9934      sqlite3_free(zSql);
9935    }else
9936    /* If no command name matches, show a syntax error */
9937    parameter_syntax_error:
9938    showHelp(p->out, "parameter");
9939  }else
9940
9941  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
9942    int i;
9943    for(i=1; i<nArg; i++){
9944      if( i>1 ) raw_printf(p->out, " ");
9945      utf8_printf(p->out, "%s", azArg[i]);
9946    }
9947    raw_printf(p->out, "\n");
9948  }else
9949
9950#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9951  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
9952    int i;
9953    int nn = 0;
9954    p->flgProgress = 0;
9955    p->mxProgress = 0;
9956    p->nProgress = 0;
9957    for(i=1; i<nArg; i++){
9958      const char *z = azArg[i];
9959      if( z[0]=='-' ){
9960        z++;
9961        if( z[0]=='-' ) z++;
9962        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9963          p->flgProgress |= SHELL_PROGRESS_QUIET;
9964          continue;
9965        }
9966        if( strcmp(z,"reset")==0 ){
9967          p->flgProgress |= SHELL_PROGRESS_RESET;
9968          continue;
9969        }
9970        if( strcmp(z,"once")==0 ){
9971          p->flgProgress |= SHELL_PROGRESS_ONCE;
9972          continue;
9973        }
9974        if( strcmp(z,"limit")==0 ){
9975          if( i+1>=nArg ){
9976            utf8_printf(stderr, "Error: missing argument on --limit\n");
9977            rc = 1;
9978            goto meta_command_exit;
9979          }else{
9980            p->mxProgress = (int)integerValue(azArg[++i]);
9981          }
9982          continue;
9983        }
9984        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9985        rc = 1;
9986        goto meta_command_exit;
9987      }else{
9988        nn = (int)integerValue(z);
9989      }
9990    }
9991    open_db(p, 0);
9992    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9993  }else
9994#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9995
9996  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9997    if( nArg >= 2) {
9998      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9999    }
10000    if( nArg >= 3) {
10001      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
10002    }
10003  }else
10004
10005#ifndef SQLITE_SHELL_FIDDLE
10006  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
10007    rc = 2;
10008  }else
10009#endif
10010
10011#ifndef SQLITE_SHELL_FIDDLE
10012  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
10013    FILE *inSaved = p->in;
10014    int savedLineno = p->lineno;
10015    failIfSafeMode(p, "cannot run .read in safe mode");
10016    if( nArg!=2 ){
10017      raw_printf(stderr, "Usage: .read FILE\n");
10018      rc = 1;
10019      goto meta_command_exit;
10020    }
10021    if( azArg[1][0]=='|' ){
10022#ifdef SQLITE_OMIT_POPEN
10023      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
10024      rc = 1;
10025      p->out = stdout;
10026#else
10027      p->in = popen(azArg[1]+1, "r");
10028      if( p->in==0 ){
10029        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
10030        rc = 1;
10031      }else{
10032        rc = process_input(p);
10033        pclose(p->in);
10034      }
10035#endif
10036    }else if( (p->in = openChrSource(azArg[1]))==0 ){
10037      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
10038      rc = 1;
10039    }else{
10040      rc = process_input(p);
10041      fclose(p->in);
10042    }
10043    p->in = inSaved;
10044    p->lineno = savedLineno;
10045  }else
10046#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10047
10048#ifndef SQLITE_SHELL_FIDDLE
10049  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
10050    const char *zSrcFile;
10051    const char *zDb;
10052    sqlite3 *pSrc;
10053    sqlite3_backup *pBackup;
10054    int nTimeout = 0;
10055
10056    failIfSafeMode(p, "cannot run .restore in safe mode");
10057    if( nArg==2 ){
10058      zSrcFile = azArg[1];
10059      zDb = "main";
10060    }else if( nArg==3 ){
10061      zSrcFile = azArg[2];
10062      zDb = azArg[1];
10063    }else{
10064      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
10065      rc = 1;
10066      goto meta_command_exit;
10067    }
10068    rc = sqlite3_open(zSrcFile, &pSrc);
10069    if( rc!=SQLITE_OK ){
10070      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
10071      close_db(pSrc);
10072      return 1;
10073    }
10074    open_db(p, 0);
10075    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
10076    if( pBackup==0 ){
10077      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10078      close_db(pSrc);
10079      return 1;
10080    }
10081    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
10082          || rc==SQLITE_BUSY  ){
10083      if( rc==SQLITE_BUSY ){
10084        if( nTimeout++ >= 3 ) break;
10085        sqlite3_sleep(100);
10086      }
10087    }
10088    sqlite3_backup_finish(pBackup);
10089    if( rc==SQLITE_DONE ){
10090      rc = 0;
10091    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
10092      raw_printf(stderr, "Error: source database is busy\n");
10093      rc = 1;
10094    }else{
10095      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10096      rc = 1;
10097    }
10098    close_db(pSrc);
10099  }else
10100#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10101
10102  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
10103    if( nArg==2 ){
10104      p->scanstatsOn = (u8)booleanValue(azArg[1]);
10105#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10106      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
10107#endif
10108    }else{
10109      raw_printf(stderr, "Usage: .scanstats on|off\n");
10110      rc = 1;
10111    }
10112  }else
10113
10114  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
10115    ShellText sSelect;
10116    ShellState data;
10117    char *zErrMsg = 0;
10118    const char *zDiv = "(";
10119    const char *zName = 0;
10120    int iSchema = 0;
10121    int bDebug = 0;
10122    int bNoSystemTabs = 0;
10123    int ii;
10124
10125    open_db(p, 0);
10126    memcpy(&data, p, sizeof(data));
10127    data.showHeader = 0;
10128    data.cMode = data.mode = MODE_Semi;
10129    initText(&sSelect);
10130    for(ii=1; ii<nArg; ii++){
10131      if( optionMatch(azArg[ii],"indent") ){
10132        data.cMode = data.mode = MODE_Pretty;
10133      }else if( optionMatch(azArg[ii],"debug") ){
10134        bDebug = 1;
10135      }else if( optionMatch(azArg[ii],"nosys") ){
10136        bNoSystemTabs = 1;
10137      }else if( azArg[ii][0]=='-' ){
10138        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
10139        rc = 1;
10140        goto meta_command_exit;
10141      }else if( zName==0 ){
10142        zName = azArg[ii];
10143      }else{
10144        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10145        rc = 1;
10146        goto meta_command_exit;
10147      }
10148    }
10149    if( zName!=0 ){
10150      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
10151                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
10152                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
10153                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
10154      if( isSchema ){
10155        char *new_argv[2], *new_colv[2];
10156        new_argv[0] = sqlite3_mprintf(
10157                      "CREATE TABLE %s (\n"
10158                      "  type text,\n"
10159                      "  name text,\n"
10160                      "  tbl_name text,\n"
10161                      "  rootpage integer,\n"
10162                      "  sql text\n"
10163                      ")", zName);
10164        shell_check_oom(new_argv[0]);
10165        new_argv[1] = 0;
10166        new_colv[0] = "sql";
10167        new_colv[1] = 0;
10168        callback(&data, 1, new_argv, new_colv);
10169        sqlite3_free(new_argv[0]);
10170      }
10171    }
10172    if( zDiv ){
10173      sqlite3_stmt *pStmt = 0;
10174      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
10175                              -1, &pStmt, 0);
10176      if( rc ){
10177        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10178        sqlite3_finalize(pStmt);
10179        rc = 1;
10180        goto meta_command_exit;
10181      }
10182      appendText(&sSelect, "SELECT sql FROM", 0);
10183      iSchema = 0;
10184      while( sqlite3_step(pStmt)==SQLITE_ROW ){
10185        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
10186        char zScNum[30];
10187        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
10188        appendText(&sSelect, zDiv, 0);
10189        zDiv = " UNION ALL ";
10190        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
10191        if( sqlite3_stricmp(zDb, "main")!=0 ){
10192          appendText(&sSelect, zDb, '\'');
10193        }else{
10194          appendText(&sSelect, "NULL", 0);
10195        }
10196        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10197        appendText(&sSelect, zScNum, 0);
10198        appendText(&sSelect, " AS snum, ", 0);
10199        appendText(&sSelect, zDb, '\'');
10200        appendText(&sSelect, " AS sname FROM ", 0);
10201        appendText(&sSelect, zDb, quoteChar(zDb));
10202        appendText(&sSelect, ".sqlite_schema", 0);
10203      }
10204      sqlite3_finalize(pStmt);
10205#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10206      if( zName ){
10207        appendText(&sSelect,
10208           " UNION ALL SELECT shell_module_schema(name),"
10209           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10210        0);
10211      }
10212#endif
10213      appendText(&sSelect, ") WHERE ", 0);
10214      if( zName ){
10215        char *zQarg = sqlite3_mprintf("%Q", zName);
10216        int bGlob;
10217        shell_check_oom(zQarg);
10218        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10219                strchr(zName, '[') != 0;
10220        if( strchr(zName, '.') ){
10221          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10222        }else{
10223          appendText(&sSelect, "lower(tbl_name)", 0);
10224        }
10225        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10226        appendText(&sSelect, zQarg, 0);
10227        if( !bGlob ){
10228          appendText(&sSelect, " ESCAPE '\\' ", 0);
10229        }
10230        appendText(&sSelect, " AND ", 0);
10231        sqlite3_free(zQarg);
10232      }
10233      if( bNoSystemTabs ){
10234        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10235      }
10236      appendText(&sSelect, "sql IS NOT NULL"
10237                           " ORDER BY snum, rowid", 0);
10238      if( bDebug ){
10239        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
10240      }else{
10241        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10242      }
10243      freeText(&sSelect);
10244    }
10245    if( zErrMsg ){
10246      utf8_printf(stderr,"Error: %s\n", zErrMsg);
10247      sqlite3_free(zErrMsg);
10248      rc = 1;
10249    }else if( rc != SQLITE_OK ){
10250      raw_printf(stderr,"Error: querying schema information\n");
10251      rc = 1;
10252    }else{
10253      rc = 0;
10254    }
10255  }else
10256
10257  if( (c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0)
10258   || (c=='t' && n==9  && strncmp(azArg[0], "treetrace", n)==0)
10259  ){
10260    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10261    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10262  }else
10263
10264#if defined(SQLITE_ENABLE_SESSION)
10265  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
10266    struct AuxDb *pAuxDb = p->pAuxDb;
10267    OpenSession *pSession = &pAuxDb->aSession[0];
10268    char **azCmd = &azArg[1];
10269    int iSes = 0;
10270    int nCmd = nArg - 1;
10271    int i;
10272    if( nArg<=1 ) goto session_syntax_error;
10273    open_db(p, 0);
10274    if( nArg>=3 ){
10275      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10276        if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10277      }
10278      if( iSes<pAuxDb->nSession ){
10279        pSession = &pAuxDb->aSession[iSes];
10280        azCmd++;
10281        nCmd--;
10282      }else{
10283        pSession = &pAuxDb->aSession[0];
10284        iSes = 0;
10285      }
10286    }
10287
10288    /* .session attach TABLE
10289    ** Invoke the sqlite3session_attach() interface to attach a particular
10290    ** table so that it is never filtered.
10291    */
10292    if( strcmp(azCmd[0],"attach")==0 ){
10293      if( nCmd!=2 ) goto session_syntax_error;
10294      if( pSession->p==0 ){
10295        session_not_open:
10296        raw_printf(stderr, "ERROR: No sessions are open\n");
10297      }else{
10298        rc = sqlite3session_attach(pSession->p, azCmd[1]);
10299        if( rc ){
10300          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
10301          rc = 0;
10302        }
10303      }
10304    }else
10305
10306    /* .session changeset FILE
10307    ** .session patchset FILE
10308    ** Write a changeset or patchset into a file.  The file is overwritten.
10309    */
10310    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
10311      FILE *out = 0;
10312      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10313      if( nCmd!=2 ) goto session_syntax_error;
10314      if( pSession->p==0 ) goto session_not_open;
10315      out = fopen(azCmd[1], "wb");
10316      if( out==0 ){
10317        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
10318                    azCmd[1]);
10319      }else{
10320        int szChng;
10321        void *pChng;
10322        if( azCmd[0][0]=='c' ){
10323          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10324        }else{
10325          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10326        }
10327        if( rc ){
10328          printf("Error: error code %d\n", rc);
10329          rc = 0;
10330        }
10331        if( pChng
10332          && fwrite(pChng, szChng, 1, out)!=1 ){
10333          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
10334                  szChng);
10335        }
10336        sqlite3_free(pChng);
10337        fclose(out);
10338      }
10339    }else
10340
10341    /* .session close
10342    ** Close the identified session
10343    */
10344    if( strcmp(azCmd[0], "close")==0 ){
10345      if( nCmd!=1 ) goto session_syntax_error;
10346      if( pAuxDb->nSession ){
10347        session_close(pSession);
10348        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10349      }
10350    }else
10351
10352    /* .session enable ?BOOLEAN?
10353    ** Query or set the enable flag
10354    */
10355    if( strcmp(azCmd[0], "enable")==0 ){
10356      int ii;
10357      if( nCmd>2 ) goto session_syntax_error;
10358      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10359      if( pAuxDb->nSession ){
10360        ii = sqlite3session_enable(pSession->p, ii);
10361        utf8_printf(p->out, "session %s enable flag = %d\n",
10362                    pSession->zName, ii);
10363      }
10364    }else
10365
10366    /* .session filter GLOB ....
10367    ** Set a list of GLOB patterns of table names to be excluded.
10368    */
10369    if( strcmp(azCmd[0], "filter")==0 ){
10370      int ii, nByte;
10371      if( nCmd<2 ) goto session_syntax_error;
10372      if( pAuxDb->nSession ){
10373        for(ii=0; ii<pSession->nFilter; ii++){
10374          sqlite3_free(pSession->azFilter[ii]);
10375        }
10376        sqlite3_free(pSession->azFilter);
10377        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10378        pSession->azFilter = sqlite3_malloc( nByte );
10379        if( pSession->azFilter==0 ){
10380          raw_printf(stderr, "Error: out or memory\n");
10381          exit(1);
10382        }
10383        for(ii=1; ii<nCmd; ii++){
10384          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10385          shell_check_oom(x);
10386        }
10387        pSession->nFilter = ii-1;
10388      }
10389    }else
10390
10391    /* .session indirect ?BOOLEAN?
10392    ** Query or set the indirect flag
10393    */
10394    if( strcmp(azCmd[0], "indirect")==0 ){
10395      int ii;
10396      if( nCmd>2 ) goto session_syntax_error;
10397      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10398      if( pAuxDb->nSession ){
10399        ii = sqlite3session_indirect(pSession->p, ii);
10400        utf8_printf(p->out, "session %s indirect flag = %d\n",
10401                    pSession->zName, ii);
10402      }
10403    }else
10404
10405    /* .session isempty
10406    ** Determine if the session is empty
10407    */
10408    if( strcmp(azCmd[0], "isempty")==0 ){
10409      int ii;
10410      if( nCmd!=1 ) goto session_syntax_error;
10411      if( pAuxDb->nSession ){
10412        ii = sqlite3session_isempty(pSession->p);
10413        utf8_printf(p->out, "session %s isempty flag = %d\n",
10414                    pSession->zName, ii);
10415      }
10416    }else
10417
10418    /* .session list
10419    ** List all currently open sessions
10420    */
10421    if( strcmp(azCmd[0],"list")==0 ){
10422      for(i=0; i<pAuxDb->nSession; i++){
10423        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
10424      }
10425    }else
10426
10427    /* .session open DB NAME
10428    ** Open a new session called NAME on the attached database DB.
10429    ** DB is normally "main".
10430    */
10431    if( strcmp(azCmd[0],"open")==0 ){
10432      char *zName;
10433      if( nCmd!=3 ) goto session_syntax_error;
10434      zName = azCmd[2];
10435      if( zName[0]==0 ) goto session_syntax_error;
10436      for(i=0; i<pAuxDb->nSession; i++){
10437        if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10438          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
10439          goto meta_command_exit;
10440        }
10441      }
10442      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10443        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10444        goto meta_command_exit;
10445      }
10446      pSession = &pAuxDb->aSession[pAuxDb->nSession];
10447      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10448      if( rc ){
10449        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
10450        rc = 0;
10451        goto meta_command_exit;
10452      }
10453      pSession->nFilter = 0;
10454      sqlite3session_table_filter(pSession->p, session_filter, pSession);
10455      pAuxDb->nSession++;
10456      pSession->zName = sqlite3_mprintf("%s", zName);
10457      shell_check_oom(pSession->zName);
10458    }else
10459    /* If no command name matches, show a syntax error */
10460    session_syntax_error:
10461    showHelp(p->out, "session");
10462  }else
10463#endif
10464
10465#ifdef SQLITE_DEBUG
10466  /* Undocumented commands for internal testing.  Subject to change
10467  ** without notice. */
10468  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
10469    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10470      int i, v;
10471      for(i=1; i<nArg; i++){
10472        v = booleanValue(azArg[i]);
10473        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
10474      }
10475    }
10476    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
10477      int i; sqlite3_int64 v;
10478      for(i=1; i<nArg; i++){
10479        char zBuf[200];
10480        v = integerValue(azArg[i]);
10481        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10482        utf8_printf(p->out, "%s", zBuf);
10483      }
10484    }
10485  }else
10486#endif
10487
10488  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
10489    int bIsInit = 0;         /* True to initialize the SELFTEST table */
10490    int bVerbose = 0;        /* Verbose output */
10491    int bSelftestExists;     /* True if SELFTEST already exists */
10492    int i, k;                /* Loop counters */
10493    int nTest = 0;           /* Number of tests runs */
10494    int nErr = 0;            /* Number of errors seen */
10495    ShellText str;           /* Answer for a query */
10496    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10497
10498    open_db(p,0);
10499    for(i=1; i<nArg; i++){
10500      const char *z = azArg[i];
10501      if( z[0]=='-' && z[1]=='-' ) z++;
10502      if( strcmp(z,"-init")==0 ){
10503        bIsInit = 1;
10504      }else
10505      if( strcmp(z,"-v")==0 ){
10506        bVerbose++;
10507      }else
10508      {
10509        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10510                    azArg[i], azArg[0]);
10511        raw_printf(stderr, "Should be one of: --init -v\n");
10512        rc = 1;
10513        goto meta_command_exit;
10514      }
10515    }
10516    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10517           != SQLITE_OK ){
10518      bSelftestExists = 0;
10519    }else{
10520      bSelftestExists = 1;
10521    }
10522    if( bIsInit ){
10523      createSelftestTable(p);
10524      bSelftestExists = 1;
10525    }
10526    initText(&str);
10527    appendText(&str, "x", 0);
10528    for(k=bSelftestExists; k>=0; k--){
10529      if( k==1 ){
10530        rc = sqlite3_prepare_v2(p->db,
10531            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10532            -1, &pStmt, 0);
10533      }else{
10534        rc = sqlite3_prepare_v2(p->db,
10535          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10536          "      (1,'run','PRAGMA integrity_check','ok')",
10537          -1, &pStmt, 0);
10538      }
10539      if( rc ){
10540        raw_printf(stderr, "Error querying the selftest table\n");
10541        rc = 1;
10542        sqlite3_finalize(pStmt);
10543        goto meta_command_exit;
10544      }
10545      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10546        int tno = sqlite3_column_int(pStmt, 0);
10547        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10548        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10549        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10550
10551        if( zOp==0 ) continue;
10552        if( zSql==0 ) continue;
10553        if( zAns==0 ) continue;
10554        k = 0;
10555        if( bVerbose>0 ){
10556          printf("%d: %s %s\n", tno, zOp, zSql);
10557        }
10558        if( strcmp(zOp,"memo")==0 ){
10559          utf8_printf(p->out, "%s\n", zSql);
10560        }else
10561        if( strcmp(zOp,"run")==0 ){
10562          char *zErrMsg = 0;
10563          str.n = 0;
10564          str.z[0] = 0;
10565          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10566          nTest++;
10567          if( bVerbose ){
10568            utf8_printf(p->out, "Result: %s\n", str.z);
10569          }
10570          if( rc || zErrMsg ){
10571            nErr++;
10572            rc = 1;
10573            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
10574            sqlite3_free(zErrMsg);
10575          }else if( strcmp(zAns,str.z)!=0 ){
10576            nErr++;
10577            rc = 1;
10578            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10579            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
10580          }
10581        }else
10582        {
10583          utf8_printf(stderr,
10584            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10585          rc = 1;
10586          break;
10587        }
10588      } /* End loop over rows of content from SELFTEST */
10589      sqlite3_finalize(pStmt);
10590    } /* End loop over k */
10591    freeText(&str);
10592    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10593  }else
10594
10595  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
10596    if( nArg<2 || nArg>3 ){
10597      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10598      rc = 1;
10599    }
10600    if( nArg>=2 ){
10601      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10602                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10603    }
10604    if( nArg>=3 ){
10605      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10606                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10607    }
10608  }else
10609
10610  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
10611    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
10612    int i;                   /* Loop counter */
10613    int bSchema = 0;         /* Also hash the schema */
10614    int bSeparate = 0;       /* Hash each table separately */
10615    int iSize = 224;         /* Hash algorithm to use */
10616    int bDebug = 0;          /* Only show the query that would have run */
10617    sqlite3_stmt *pStmt;     /* For querying tables names */
10618    char *zSql;              /* SQL to be run */
10619    char *zSep;              /* Separator */
10620    ShellText sSql;          /* Complete SQL for the query to run the hash */
10621    ShellText sQuery;        /* Set of queries used to read all content */
10622    open_db(p, 0);
10623    for(i=1; i<nArg; i++){
10624      const char *z = azArg[i];
10625      if( z[0]=='-' ){
10626        z++;
10627        if( z[0]=='-' ) z++;
10628        if( strcmp(z,"schema")==0 ){
10629          bSchema = 1;
10630        }else
10631        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
10632         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
10633        ){
10634          iSize = atoi(&z[5]);
10635        }else
10636        if( strcmp(z,"debug")==0 ){
10637          bDebug = 1;
10638        }else
10639        {
10640          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10641                      azArg[i], azArg[0]);
10642          showHelp(p->out, azArg[0]);
10643          rc = 1;
10644          goto meta_command_exit;
10645        }
10646      }else if( zLike ){
10647        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10648        rc = 1;
10649        goto meta_command_exit;
10650      }else{
10651        zLike = z;
10652        bSeparate = 1;
10653        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10654      }
10655    }
10656    if( bSchema ){
10657      zSql = "SELECT lower(name) FROM sqlite_schema"
10658             " WHERE type='table' AND coalesce(rootpage,0)>1"
10659             " UNION ALL SELECT 'sqlite_schema'"
10660             " ORDER BY 1 collate nocase";
10661    }else{
10662      zSql = "SELECT lower(name) FROM sqlite_schema"
10663             " WHERE type='table' AND coalesce(rootpage,0)>1"
10664             " AND name NOT LIKE 'sqlite_%'"
10665             " ORDER BY 1 collate nocase";
10666    }
10667    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10668    initText(&sQuery);
10669    initText(&sSql);
10670    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10671    zSep = "VALUES(";
10672    while( SQLITE_ROW==sqlite3_step(pStmt) ){
10673      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10674      if( zTab==0 ) continue;
10675      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10676      if( strncmp(zTab, "sqlite_",7)!=0 ){
10677        appendText(&sQuery,"SELECT * FROM ", 0);
10678        appendText(&sQuery,zTab,'"');
10679        appendText(&sQuery," NOT INDEXED;", 0);
10680      }else if( strcmp(zTab, "sqlite_schema")==0 ){
10681        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10682                           " ORDER BY name;", 0);
10683      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
10684        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10685                           " ORDER BY name;", 0);
10686      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
10687        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10688                           " ORDER BY tbl,idx;", 0);
10689      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
10690        appendText(&sQuery, "SELECT * FROM ", 0);
10691        appendText(&sQuery, zTab, 0);
10692        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10693      }
10694      appendText(&sSql, zSep, 0);
10695      appendText(&sSql, sQuery.z, '\'');
10696      sQuery.n = 0;
10697      appendText(&sSql, ",", 0);
10698      appendText(&sSql, zTab, '\'');
10699      zSep = "),(";
10700    }
10701    sqlite3_finalize(pStmt);
10702    if( bSeparate ){
10703      zSql = sqlite3_mprintf(
10704          "%s))"
10705          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10706          "   FROM [sha3sum$query]",
10707          sSql.z, iSize);
10708    }else{
10709      zSql = sqlite3_mprintf(
10710          "%s))"
10711          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10712          "   FROM [sha3sum$query]",
10713          sSql.z, iSize);
10714    }
10715    shell_check_oom(zSql);
10716    freeText(&sQuery);
10717    freeText(&sSql);
10718    if( bDebug ){
10719      utf8_printf(p->out, "%s\n", zSql);
10720    }else{
10721      shell_exec(p, zSql, 0);
10722    }
10723    sqlite3_free(zSql);
10724  }else
10725
10726#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10727  if( c=='s'
10728   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
10729  ){
10730    char *zCmd;
10731    int i, x;
10732    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10733    if( nArg<2 ){
10734      raw_printf(stderr, "Usage: .system COMMAND\n");
10735      rc = 1;
10736      goto meta_command_exit;
10737    }
10738    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10739    for(i=2; i<nArg && zCmd!=0; i++){
10740      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10741                             zCmd, azArg[i]);
10742    }
10743    x = zCmd!=0 ? system(zCmd) : 1;
10744    sqlite3_free(zCmd);
10745    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10746  }else
10747#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10748
10749  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
10750    static const char *azBool[] = { "off", "on", "trigger", "full"};
10751    const char *zOut;
10752    int i;
10753    if( nArg!=1 ){
10754      raw_printf(stderr, "Usage: .show\n");
10755      rc = 1;
10756      goto meta_command_exit;
10757    }
10758    utf8_printf(p->out, "%12.12s: %s\n","echo",
10759                azBool[ShellHasFlag(p, SHFLG_Echo)]);
10760    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10761    utf8_printf(p->out, "%12.12s: %s\n","explain",
10762         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10763    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10764    if( p->mode==MODE_Column
10765     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10766    ){
10767      utf8_printf
10768        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10769         modeDescr[p->mode], p->cmOpts.iWrap,
10770         p->cmOpts.bWordWrap ? "on" : "off",
10771         p->cmOpts.bQuote ? "" : "no");
10772    }else{
10773      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10774    }
10775    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10776      output_c_string(p->out, p->nullValue);
10777      raw_printf(p->out, "\n");
10778    utf8_printf(p->out,"%12.12s: %s\n","output",
10779            strlen30(p->outfile) ? p->outfile : "stdout");
10780    utf8_printf(p->out,"%12.12s: ", "colseparator");
10781      output_c_string(p->out, p->colSeparator);
10782      raw_printf(p->out, "\n");
10783    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10784      output_c_string(p->out, p->rowSeparator);
10785      raw_printf(p->out, "\n");
10786    switch( p->statsOn ){
10787      case 0:  zOut = "off";     break;
10788      default: zOut = "on";      break;
10789      case 2:  zOut = "stmt";    break;
10790      case 3:  zOut = "vmstep";  break;
10791    }
10792    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10793    utf8_printf(p->out, "%12.12s: ", "width");
10794    for (i=0;i<p->nWidth;i++) {
10795      raw_printf(p->out, "%d ", p->colWidth[i]);
10796    }
10797    raw_printf(p->out, "\n");
10798    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10799                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10800  }else
10801
10802  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
10803    if( nArg==2 ){
10804      if( strcmp(azArg[1],"stmt")==0 ){
10805        p->statsOn = 2;
10806      }else if( strcmp(azArg[1],"vmstep")==0 ){
10807        p->statsOn = 3;
10808      }else{
10809        p->statsOn = (u8)booleanValue(azArg[1]);
10810      }
10811    }else if( nArg==1 ){
10812      display_stats(p->db, p, 0);
10813    }else{
10814      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10815      rc = 1;
10816    }
10817  }else
10818
10819  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
10820   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
10821                 || strncmp(azArg[0], "indexes", n)==0) )
10822  ){
10823    sqlite3_stmt *pStmt;
10824    char **azResult;
10825    int nRow, nAlloc;
10826    int ii;
10827    ShellText s;
10828    initText(&s);
10829    open_db(p, 0);
10830    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10831    if( rc ){
10832      sqlite3_finalize(pStmt);
10833      return shellDatabaseError(p->db);
10834    }
10835
10836    if( nArg>2 && c=='i' ){
10837      /* It is an historical accident that the .indexes command shows an error
10838      ** when called with the wrong number of arguments whereas the .tables
10839      ** command does not. */
10840      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10841      rc = 1;
10842      sqlite3_finalize(pStmt);
10843      goto meta_command_exit;
10844    }
10845    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10846      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10847      if( zDbName==0 ) continue;
10848      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10849      if( sqlite3_stricmp(zDbName, "main")==0 ){
10850        appendText(&s, "SELECT name FROM ", 0);
10851      }else{
10852        appendText(&s, "SELECT ", 0);
10853        appendText(&s, zDbName, '\'');
10854        appendText(&s, "||'.'||name FROM ", 0);
10855      }
10856      appendText(&s, zDbName, '"');
10857      appendText(&s, ".sqlite_schema ", 0);
10858      if( c=='t' ){
10859        appendText(&s," WHERE type IN ('table','view')"
10860                      "   AND name NOT LIKE 'sqlite_%'"
10861                      "   AND name LIKE ?1", 0);
10862      }else{
10863        appendText(&s," WHERE type='index'"
10864                      "   AND tbl_name LIKE ?1", 0);
10865      }
10866    }
10867    rc = sqlite3_finalize(pStmt);
10868    if( rc==SQLITE_OK ){
10869      appendText(&s, " ORDER BY 1", 0);
10870      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10871    }
10872    freeText(&s);
10873    if( rc ) return shellDatabaseError(p->db);
10874
10875    /* Run the SQL statement prepared by the above block. Store the results
10876    ** as an array of nul-terminated strings in azResult[].  */
10877    nRow = nAlloc = 0;
10878    azResult = 0;
10879    if( nArg>1 ){
10880      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10881    }else{
10882      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10883    }
10884    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10885      if( nRow>=nAlloc ){
10886        char **azNew;
10887        int n2 = nAlloc*2 + 10;
10888        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10889        shell_check_oom(azNew);
10890        nAlloc = n2;
10891        azResult = azNew;
10892      }
10893      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10894      shell_check_oom(azResult[nRow]);
10895      nRow++;
10896    }
10897    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10898      rc = shellDatabaseError(p->db);
10899    }
10900
10901    /* Pretty-print the contents of array azResult[] to the output */
10902    if( rc==0 && nRow>0 ){
10903      int len, maxlen = 0;
10904      int i, j;
10905      int nPrintCol, nPrintRow;
10906      for(i=0; i<nRow; i++){
10907        len = strlen30(azResult[i]);
10908        if( len>maxlen ) maxlen = len;
10909      }
10910      nPrintCol = 80/(maxlen+2);
10911      if( nPrintCol<1 ) nPrintCol = 1;
10912      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10913      for(i=0; i<nPrintRow; i++){
10914        for(j=i; j<nRow; j+=nPrintRow){
10915          char *zSp = j<nPrintRow ? "" : "  ";
10916          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10917                      azResult[j] ? azResult[j]:"");
10918        }
10919        raw_printf(p->out, "\n");
10920      }
10921    }
10922
10923    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10924    sqlite3_free(azResult);
10925  }else
10926
10927#ifndef SQLITE_SHELL_FIDDLE
10928  /* Begin redirecting output to the file "testcase-out.txt" */
10929  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
10930    output_reset(p);
10931    p->out = output_file_open("testcase-out.txt", 0);
10932    if( p->out==0 ){
10933      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10934    }
10935    if( nArg>=2 ){
10936      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10937    }else{
10938      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10939    }
10940  }else
10941#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10942
10943#ifndef SQLITE_UNTESTABLE
10944  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
10945    static const struct {
10946       const char *zCtrlName;   /* Name of a test-control option */
10947       int ctrlCode;            /* Integer code for that option */
10948       int unSafe;              /* Not valid for --safe mode */
10949       const char *zUsage;      /* Usage notes */
10950    } aCtrl[] = {
10951      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10952      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10953    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10954    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10955      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10956      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10957    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10958      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10959      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10960      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10961      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10962      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10963#ifdef YYCOVERAGE
10964      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10965#endif
10966      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10967      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10968      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10969      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10970      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10971      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10972      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10973    };
10974    int testctrl = -1;
10975    int iCtrl = -1;
10976    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10977    int isOk = 0;
10978    int i, n2;
10979    const char *zCmd = 0;
10980
10981    open_db(p, 0);
10982    zCmd = nArg>=2 ? azArg[1] : "help";
10983
10984    /* The argument can optionally begin with "-" or "--" */
10985    if( zCmd[0]=='-' && zCmd[1] ){
10986      zCmd++;
10987      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10988    }
10989
10990    /* --help lists all test-controls */
10991    if( strcmp(zCmd,"help")==0 ){
10992      utf8_printf(p->out, "Available test-controls:\n");
10993      for(i=0; i<ArraySize(aCtrl); i++){
10994        utf8_printf(p->out, "  .testctrl %s %s\n",
10995                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10996      }
10997      rc = 1;
10998      goto meta_command_exit;
10999    }
11000
11001    /* convert testctrl text option to value. allow any unique prefix
11002    ** of the option name, or a numerical value. */
11003    n2 = strlen30(zCmd);
11004    for(i=0; i<ArraySize(aCtrl); i++){
11005      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
11006        if( testctrl<0 ){
11007          testctrl = aCtrl[i].ctrlCode;
11008          iCtrl = i;
11009        }else{
11010          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
11011                              "Use \".testctrl --help\" for help\n", zCmd);
11012          rc = 1;
11013          goto meta_command_exit;
11014        }
11015      }
11016    }
11017    if( testctrl<0 ){
11018      utf8_printf(stderr,"Error: unknown test-control: %s\n"
11019                         "Use \".testctrl --help\" for help\n", zCmd);
11020    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
11021      utf8_printf(stderr,
11022         "line %d: \".testctrl %s\" may not be used in safe mode\n",
11023         p->lineno, aCtrl[iCtrl].zCtrlName);
11024      exit(1);
11025    }else{
11026      switch(testctrl){
11027
11028        /* sqlite3_test_control(int, db, int) */
11029        case SQLITE_TESTCTRL_OPTIMIZATIONS:
11030          if( nArg==3 ){
11031            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
11032            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11033            isOk = 3;
11034          }
11035          break;
11036
11037        /* sqlite3_test_control(int) */
11038        case SQLITE_TESTCTRL_PRNG_SAVE:
11039        case SQLITE_TESTCTRL_PRNG_RESTORE:
11040        case SQLITE_TESTCTRL_BYTEORDER:
11041          if( nArg==2 ){
11042            rc2 = sqlite3_test_control(testctrl);
11043            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
11044          }
11045          break;
11046
11047        /* sqlite3_test_control(int, uint) */
11048        case SQLITE_TESTCTRL_PENDING_BYTE:
11049          if( nArg==3 ){
11050            unsigned int opt = (unsigned int)integerValue(azArg[2]);
11051            rc2 = sqlite3_test_control(testctrl, opt);
11052            isOk = 3;
11053          }
11054          break;
11055
11056        /* sqlite3_test_control(int, int, sqlite3*) */
11057        case SQLITE_TESTCTRL_PRNG_SEED:
11058          if( nArg==3 || nArg==4 ){
11059            int ii = (int)integerValue(azArg[2]);
11060            sqlite3 *db;
11061            if( ii==0 && strcmp(azArg[2],"random")==0 ){
11062              sqlite3_randomness(sizeof(ii),&ii);
11063              printf("-- random seed: %d\n", ii);
11064            }
11065            if( nArg==3 ){
11066              db = 0;
11067            }else{
11068              db = p->db;
11069              /* Make sure the schema has been loaded */
11070              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
11071            }
11072            rc2 = sqlite3_test_control(testctrl, ii, db);
11073            isOk = 3;
11074          }
11075          break;
11076
11077        /* sqlite3_test_control(int, int) */
11078        case SQLITE_TESTCTRL_ASSERT:
11079        case SQLITE_TESTCTRL_ALWAYS:
11080          if( nArg==3 ){
11081            int opt = booleanValue(azArg[2]);
11082            rc2 = sqlite3_test_control(testctrl, opt);
11083            isOk = 1;
11084          }
11085          break;
11086
11087        /* sqlite3_test_control(int, int) */
11088        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
11089        case SQLITE_TESTCTRL_NEVER_CORRUPT:
11090          if( nArg==3 ){
11091            int opt = booleanValue(azArg[2]);
11092            rc2 = sqlite3_test_control(testctrl, opt);
11093            isOk = 3;
11094          }
11095          break;
11096
11097        /* sqlite3_test_control(sqlite3*) */
11098        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
11099          rc2 = sqlite3_test_control(testctrl, p->db);
11100          isOk = 3;
11101          break;
11102
11103        case SQLITE_TESTCTRL_IMPOSTER:
11104          if( nArg==5 ){
11105            rc2 = sqlite3_test_control(testctrl, p->db,
11106                          azArg[2],
11107                          integerValue(azArg[3]),
11108                          integerValue(azArg[4]));
11109            isOk = 3;
11110          }
11111          break;
11112
11113        case SQLITE_TESTCTRL_SEEK_COUNT: {
11114          u64 x = 0;
11115          rc2 = sqlite3_test_control(testctrl, p->db, &x);
11116          utf8_printf(p->out, "%llu\n", x);
11117          isOk = 3;
11118          break;
11119        }
11120
11121#ifdef YYCOVERAGE
11122        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
11123          if( nArg==2 ){
11124            sqlite3_test_control(testctrl, p->out);
11125            isOk = 3;
11126          }
11127          break;
11128        }
11129#endif
11130#ifdef SQLITE_DEBUG
11131        case SQLITE_TESTCTRL_TUNE: {
11132          if( nArg==4 ){
11133            int id = (int)integerValue(azArg[2]);
11134            int val = (int)integerValue(azArg[3]);
11135            sqlite3_test_control(testctrl, id, &val);
11136            isOk = 3;
11137          }else if( nArg==3 ){
11138            int id = (int)integerValue(azArg[2]);
11139            sqlite3_test_control(testctrl, -id, &rc2);
11140            isOk = 1;
11141          }else if( nArg==2 ){
11142            int id = 1;
11143            while(1){
11144              int val = 0;
11145              rc2 = sqlite3_test_control(testctrl, -id, &val);
11146              if( rc2!=SQLITE_OK ) break;
11147              if( id>1 ) utf8_printf(p->out, "  ");
11148              utf8_printf(p->out, "%d: %d", id, val);
11149              id++;
11150            }
11151            if( id>1 ) utf8_printf(p->out, "\n");
11152            isOk = 3;
11153          }
11154          break;
11155        }
11156#endif
11157        case SQLITE_TESTCTRL_SORTER_MMAP:
11158          if( nArg==3 ){
11159            int opt = (unsigned int)integerValue(azArg[2]);
11160            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11161            isOk = 3;
11162          }
11163          break;
11164      }
11165    }
11166    if( isOk==0 && iCtrl>=0 ){
11167      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11168      rc = 1;
11169    }else if( isOk==1 ){
11170      raw_printf(p->out, "%d\n", rc2);
11171    }else if( isOk==2 ){
11172      raw_printf(p->out, "0x%08x\n", rc2);
11173    }
11174  }else
11175#endif /* !defined(SQLITE_UNTESTABLE) */
11176
11177  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
11178    open_db(p, 0);
11179    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11180  }else
11181
11182  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
11183    if( nArg==2 ){
11184      enableTimer = booleanValue(azArg[1]);
11185      if( enableTimer && !HAS_TIMER ){
11186        raw_printf(stderr, "Error: timer not available on this system.\n");
11187        enableTimer = 0;
11188      }
11189    }else{
11190      raw_printf(stderr, "Usage: .timer on|off\n");
11191      rc = 1;
11192    }
11193  }else
11194
11195#ifndef SQLITE_OMIT_TRACE
11196  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
11197    int mType = 0;
11198    int jj;
11199    open_db(p, 0);
11200    for(jj=1; jj<nArg; jj++){
11201      const char *z = azArg[jj];
11202      if( z[0]=='-' ){
11203        if( optionMatch(z, "expanded") ){
11204          p->eTraceType = SHELL_TRACE_EXPANDED;
11205        }
11206#ifdef SQLITE_ENABLE_NORMALIZE
11207        else if( optionMatch(z, "normalized") ){
11208          p->eTraceType = SHELL_TRACE_NORMALIZED;
11209        }
11210#endif
11211        else if( optionMatch(z, "plain") ){
11212          p->eTraceType = SHELL_TRACE_PLAIN;
11213        }
11214        else if( optionMatch(z, "profile") ){
11215          mType |= SQLITE_TRACE_PROFILE;
11216        }
11217        else if( optionMatch(z, "row") ){
11218          mType |= SQLITE_TRACE_ROW;
11219        }
11220        else if( optionMatch(z, "stmt") ){
11221          mType |= SQLITE_TRACE_STMT;
11222        }
11223        else if( optionMatch(z, "close") ){
11224          mType |= SQLITE_TRACE_CLOSE;
11225        }
11226        else {
11227          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
11228          rc = 1;
11229          goto meta_command_exit;
11230        }
11231      }else{
11232        output_file_close(p->traceOut);
11233        p->traceOut = output_file_open(azArg[1], 0);
11234      }
11235    }
11236    if( p->traceOut==0 ){
11237      sqlite3_trace_v2(p->db, 0, 0, 0);
11238    }else{
11239      if( mType==0 ) mType = SQLITE_TRACE_STMT;
11240      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11241    }
11242  }else
11243#endif /* !defined(SQLITE_OMIT_TRACE) */
11244
11245#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11246  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
11247    int ii;
11248    int lenOpt;
11249    char *zOpt;
11250    if( nArg<2 ){
11251      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
11252      rc = 1;
11253      goto meta_command_exit;
11254    }
11255    open_db(p, 0);
11256    zOpt = azArg[1];
11257    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11258    lenOpt = (int)strlen(zOpt);
11259    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11260      assert( azArg[nArg]==0 );
11261      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11262    }else{
11263      for(ii=1; ii<nArg; ii++){
11264        sqlite3_create_module(p->db, azArg[ii], 0, 0);
11265      }
11266    }
11267  }else
11268#endif
11269
11270#if SQLITE_USER_AUTHENTICATION
11271  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
11272    if( nArg<2 ){
11273      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
11274      rc = 1;
11275      goto meta_command_exit;
11276    }
11277    open_db(p, 0);
11278    if( strcmp(azArg[1],"login")==0 ){
11279      if( nArg!=4 ){
11280        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
11281        rc = 1;
11282        goto meta_command_exit;
11283      }
11284      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11285                                     strlen30(azArg[3]));
11286      if( rc ){
11287        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
11288        rc = 1;
11289      }
11290    }else if( strcmp(azArg[1],"add")==0 ){
11291      if( nArg!=5 ){
11292        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
11293        rc = 1;
11294        goto meta_command_exit;
11295      }
11296      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11297                            booleanValue(azArg[4]));
11298      if( rc ){
11299        raw_printf(stderr, "User-Add failed: %d\n", rc);
11300        rc = 1;
11301      }
11302    }else if( strcmp(azArg[1],"edit")==0 ){
11303      if( nArg!=5 ){
11304        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
11305        rc = 1;
11306        goto meta_command_exit;
11307      }
11308      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11309                              booleanValue(azArg[4]));
11310      if( rc ){
11311        raw_printf(stderr, "User-Edit failed: %d\n", rc);
11312        rc = 1;
11313      }
11314    }else if( strcmp(azArg[1],"delete")==0 ){
11315      if( nArg!=3 ){
11316        raw_printf(stderr, "Usage: .user delete USER\n");
11317        rc = 1;
11318        goto meta_command_exit;
11319      }
11320      rc = sqlite3_user_delete(p->db, azArg[2]);
11321      if( rc ){
11322        raw_printf(stderr, "User-Delete failed: %d\n", rc);
11323        rc = 1;
11324      }
11325    }else{
11326      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
11327      rc = 1;
11328      goto meta_command_exit;
11329    }
11330  }else
11331#endif /* SQLITE_USER_AUTHENTICATION */
11332
11333  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
11334    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
11335        sqlite3_libversion(), sqlite3_sourceid());
11336#if SQLITE_HAVE_ZLIB
11337    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
11338#endif
11339#define CTIMEOPT_VAL_(opt) #opt
11340#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11341#if defined(__clang__) && defined(__clang_major__)
11342    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
11343                    CTIMEOPT_VAL(__clang_minor__) "."
11344                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
11345#elif defined(_MSC_VER)
11346    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
11347#elif defined(__GNUC__) && defined(__VERSION__)
11348    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
11349#endif
11350  }else
11351
11352  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
11353    const char *zDbName = nArg==2 ? azArg[1] : "main";
11354    sqlite3_vfs *pVfs = 0;
11355    if( p->db ){
11356      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11357      if( pVfs ){
11358        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
11359        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11360        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11361        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11362      }
11363    }
11364  }else
11365
11366  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
11367    sqlite3_vfs *pVfs;
11368    sqlite3_vfs *pCurrent = 0;
11369    if( p->db ){
11370      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11371    }
11372    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11373      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
11374           pVfs==pCurrent ? "  <--- CURRENT" : "");
11375      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11376      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11377      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11378      if( pVfs->pNext ){
11379        raw_printf(p->out, "-----------------------------------\n");
11380      }
11381    }
11382  }else
11383
11384  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
11385    const char *zDbName = nArg==2 ? azArg[1] : "main";
11386    char *zVfsName = 0;
11387    if( p->db ){
11388      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11389      if( zVfsName ){
11390        utf8_printf(p->out, "%s\n", zVfsName);
11391        sqlite3_free(zVfsName);
11392      }
11393    }
11394  }else
11395
11396  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
11397    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11398    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11399  }else
11400
11401  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
11402    int j;
11403    assert( nArg<=ArraySize(azArg) );
11404    p->nWidth = nArg-1;
11405    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11406    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11407    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11408    for(j=1; j<nArg; j++){
11409      p->colWidth[j-1] = (int)integerValue(azArg[j]);
11410    }
11411  }else
11412
11413  {
11414    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
11415      " \"%s\". Enter \".help\" for help\n", azArg[0]);
11416    rc = 1;
11417  }
11418
11419meta_command_exit:
11420  if( p->outCount ){
11421    p->outCount--;
11422    if( p->outCount==0 ) output_reset(p);
11423  }
11424  p->bSafeMode = p->bSafeModePersist;
11425  return rc;
11426}
11427
11428/* Line scan result and intermediate states (supporting scan resumption)
11429*/
11430#ifndef CHAR_BIT
11431# define CHAR_BIT 8
11432#endif
11433typedef enum {
11434  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11435  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11436  QSS_Start = 0
11437} QuickScanState;
11438#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11439#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11440#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11441#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11442#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11443
11444/*
11445** Scan line for classification to guide shell's handling.
11446** The scan is resumable for subsequent lines when prior
11447** return values are passed as the 2nd argument.
11448*/
11449static QuickScanState quickscan(char *zLine, QuickScanState qss){
11450  char cin;
11451  char cWait = (char)qss; /* intentional narrowing loss */
11452  if( cWait==0 ){
11453  PlainScan:
11454    assert( cWait==0 );
11455    while( (cin = *zLine++)!=0 ){
11456      if( IsSpace(cin) )
11457        continue;
11458      switch (cin){
11459      case '-':
11460        if( *zLine!='-' )
11461          break;
11462        while((cin = *++zLine)!=0 )
11463          if( cin=='\n')
11464            goto PlainScan;
11465        return qss;
11466      case ';':
11467        qss |= QSS_EndingSemi;
11468        continue;
11469      case '/':
11470        if( *zLine=='*' ){
11471          ++zLine;
11472          cWait = '*';
11473          qss = QSS_SETV(qss, cWait);
11474          goto TermScan;
11475        }
11476        break;
11477      case '[':
11478        cin = ']';
11479        /* fall thru */
11480      case '`': case '\'': case '"':
11481        cWait = cin;
11482        qss = QSS_HasDark | cWait;
11483        goto TermScan;
11484      default:
11485        break;
11486      }
11487      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11488    }
11489  }else{
11490  TermScan:
11491    while( (cin = *zLine++)!=0 ){
11492      if( cin==cWait ){
11493        switch( cWait ){
11494        case '*':
11495          if( *zLine != '/' )
11496            continue;
11497          ++zLine;
11498          cWait = 0;
11499          qss = QSS_SETV(qss, 0);
11500          goto PlainScan;
11501        case '`': case '\'': case '"':
11502          if(*zLine==cWait){
11503            ++zLine;
11504            continue;
11505          }
11506          /* fall thru */
11507        case ']':
11508          cWait = 0;
11509          qss = QSS_SETV(qss, 0);
11510          goto PlainScan;
11511        default: assert(0);
11512        }
11513      }
11514    }
11515  }
11516  return qss;
11517}
11518
11519/*
11520** Return TRUE if the line typed in is an SQL command terminator other
11521** than a semi-colon.  The SQL Server style "go" command is understood
11522** as is the Oracle "/".
11523*/
11524static int line_is_command_terminator(char *zLine){
11525  while( IsSpace(zLine[0]) ){ zLine++; };
11526  if( zLine[0]=='/' )
11527    zLine += 1; /* Oracle */
11528  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
11529    zLine += 2; /* SQL Server */
11530  else
11531    return 0;
11532  return quickscan(zLine, QSS_Start)==QSS_Start;
11533}
11534
11535/*
11536** We need a default sqlite3_complete() implementation to use in case
11537** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
11538** any arbitrary text is a complete SQL statement.  This is not very
11539** user-friendly, but it does seem to work.
11540*/
11541#ifdef SQLITE_OMIT_COMPLETE
11542#define sqlite3_complete(x) 1
11543#endif
11544
11545/*
11546** Return true if zSql is a complete SQL statement.  Return false if it
11547** ends in the middle of a string literal or C-style comment.
11548*/
11549static int line_is_complete(char *zSql, int nSql){
11550  int rc;
11551  if( zSql==0 ) return 1;
11552  zSql[nSql] = ';';
11553  zSql[nSql+1] = 0;
11554  rc = sqlite3_complete(zSql);
11555  zSql[nSql] = 0;
11556  return rc;
11557}
11558
11559/*
11560** Run a single line of SQL.  Return the number of errors.
11561*/
11562static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
11563  int rc;
11564  char *zErrMsg = 0;
11565
11566  open_db(p, 0);
11567  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
11568  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11569  BEGIN_TIMER;
11570  rc = shell_exec(p, zSql, &zErrMsg);
11571  END_TIMER;
11572  if( rc || zErrMsg ){
11573    char zPrefix[100];
11574    const char *zErrorTail;
11575    const char *zErrorType;
11576    if( zErrMsg==0 ){
11577      zErrorType = "Error";
11578      zErrorTail = sqlite3_errmsg(p->db);
11579    }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){
11580      zErrorType = "Parse error";
11581      zErrorTail = &zErrMsg[12];
11582    }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){
11583      zErrorType = "Runtime error";
11584      zErrorTail = &zErrMsg[10];
11585    }else{
11586      zErrorType = "Error";
11587      zErrorTail = zErrMsg;
11588    }
11589    if( in!=0 || !stdin_is_interactive ){
11590      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11591                       "%s near line %d:", zErrorType, startline);
11592    }else{
11593      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11594    }
11595    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11596    sqlite3_free(zErrMsg);
11597    zErrMsg = 0;
11598    return 1;
11599  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11600    char zLineBuf[2000];
11601    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11602            "changes: %lld   total_changes: %lld",
11603            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11604    raw_printf(p->out, "%s\n", zLineBuf);
11605  }
11606  return 0;
11607}
11608
11609static void echo_group_input(ShellState *p, const char *zDo){
11610  if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11611}
11612
11613#ifdef SQLITE_SHELL_FIDDLE
11614/*
11615** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
11616** because we need the global shellState and cannot access it from that function
11617** without moving lots of code around (creating a larger/messier diff).
11618*/
11619static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11620  /* Parse the next line from shellState.wasm.zInput. */
11621  const char *zBegin = shellState.wasm.zPos;
11622  const char *z = zBegin;
11623  char *zLine = 0;
11624  i64 nZ = 0;
11625
11626  UNUSED_PARAMETER(in);
11627  UNUSED_PARAMETER(isContinuation);
11628  if(!z || !*z){
11629    return 0;
11630  }
11631  while(*z && isspace(*z)) ++z;
11632  zBegin = z;
11633  for(; *z && '\n'!=*z; ++nZ, ++z){}
11634  if(nZ>0 && '\r'==zBegin[nZ-1]){
11635    --nZ;
11636  }
11637  shellState.wasm.zPos = z;
11638  zLine = realloc(zPrior, nZ+1);
11639  shell_check_oom(zLine);
11640  memcpy(zLine, zBegin, nZ);
11641  zLine[nZ] = 0;
11642  return zLine;
11643}
11644#endif /* SQLITE_SHELL_FIDDLE */
11645
11646/*
11647** Read input from *in and process it.  If *in==0 then input
11648** is interactive - the user is typing it it.  Otherwise, input
11649** is coming from a file or device.  A prompt is issued and history
11650** is saved only if input is interactive.  An interrupt signal will
11651** cause this routine to exit immediately, unless input is interactive.
11652**
11653** Return the number of errors.
11654*/
11655static int process_input(ShellState *p){
11656  char *zLine = 0;          /* A single input line */
11657  char *zSql = 0;           /* Accumulated SQL text */
11658  i64 nLine;                /* Length of current line */
11659  i64 nSql = 0;             /* Bytes of zSql[] used */
11660  i64 nAlloc = 0;           /* Allocated zSql[] space */
11661  int rc;                   /* Error code */
11662  int errCnt = 0;           /* Number of errors seen */
11663  i64 startline = 0;        /* Line number for start of current input */
11664  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11665
11666  if( p->inputNesting==MAX_INPUT_NESTING ){
11667    /* This will be more informative in a later version. */
11668    utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11669                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11670    return 1;
11671  }
11672  ++p->inputNesting;
11673  p->lineno = 0;
11674  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11675    fflush(p->out);
11676    zLine = one_input_line(p->in, zLine, nSql>0);
11677    if( zLine==0 ){
11678      /* End of input */
11679      if( p->in==0 && stdin_is_interactive ) printf("\n");
11680      break;
11681    }
11682    if( seenInterrupt ){
11683      if( p->in!=0 ) break;
11684      seenInterrupt = 0;
11685    }
11686    p->lineno++;
11687    if( QSS_INPLAIN(qss)
11688        && line_is_command_terminator(zLine)
11689        && line_is_complete(zSql, nSql) ){
11690      memcpy(zLine,";",2);
11691    }
11692    qss = quickscan(zLine, qss);
11693    if( QSS_PLAINWHITE(qss) && nSql==0 ){
11694      /* Just swallow single-line whitespace */
11695      echo_group_input(p, zLine);
11696      qss = QSS_Start;
11697      continue;
11698    }
11699    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11700      echo_group_input(p, zLine);
11701      if( zLine[0]=='.' ){
11702        rc = do_meta_command(zLine, p);
11703        if( rc==2 ){ /* exit requested */
11704          break;
11705        }else if( rc ){
11706          errCnt++;
11707        }
11708      }
11709      qss = QSS_Start;
11710      continue;
11711    }
11712    /* No single-line dispositions remain; accumulate line(s). */
11713    nLine = strlen(zLine);
11714    if( nSql+nLine+2>=nAlloc ){
11715      /* Grow buffer by half-again increments when big. */
11716      nAlloc = nSql+(nSql>>1)+nLine+100;
11717      zSql = realloc(zSql, nAlloc);
11718      shell_check_oom(zSql);
11719    }
11720    if( nSql==0 ){
11721      i64 i;
11722      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11723      assert( nAlloc>0 && zSql!=0 );
11724      memcpy(zSql, zLine+i, nLine+1-i);
11725      startline = p->lineno;
11726      nSql = nLine-i;
11727    }else{
11728      zSql[nSql++] = '\n';
11729      memcpy(zSql+nSql, zLine, nLine+1);
11730      nSql += nLine;
11731    }
11732    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11733      echo_group_input(p, zSql);
11734      errCnt += runOneSqlLine(p, zSql, p->in, startline);
11735      nSql = 0;
11736      if( p->outCount ){
11737        output_reset(p);
11738        p->outCount = 0;
11739      }else{
11740        clearTempFile(p);
11741      }
11742      p->bSafeMode = p->bSafeModePersist;
11743      qss = QSS_Start;
11744    }else if( nSql && QSS_PLAINWHITE(qss) ){
11745      echo_group_input(p, zSql);
11746      nSql = 0;
11747      qss = QSS_Start;
11748    }
11749  }
11750  if( nSql ){
11751    /* This may be incomplete. Let the SQL parser deal with that. */
11752    echo_group_input(p, zSql);
11753    errCnt += runOneSqlLine(p, zSql, p->in, startline);
11754  }
11755  free(zSql);
11756  free(zLine);
11757  --p->inputNesting;
11758  return errCnt>0;
11759}
11760
11761/*
11762** Return a pathname which is the user's home directory.  A
11763** 0 return indicates an error of some kind.
11764*/
11765static char *find_home_dir(int clearFlag){
11766  static char *home_dir = NULL;
11767  if( clearFlag ){
11768    free(home_dir);
11769    home_dir = 0;
11770    return 0;
11771  }
11772  if( home_dir ) return home_dir;
11773
11774#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11775     && !defined(__RTP__) && !defined(_WRS_KERNEL)
11776  {
11777    struct passwd *pwent;
11778    uid_t uid = getuid();
11779    if( (pwent=getpwuid(uid)) != NULL) {
11780      home_dir = pwent->pw_dir;
11781    }
11782  }
11783#endif
11784
11785#if defined(_WIN32_WCE)
11786  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11787   */
11788  home_dir = "/";
11789#else
11790
11791#if defined(_WIN32) || defined(WIN32)
11792  if (!home_dir) {
11793    home_dir = getenv("USERPROFILE");
11794  }
11795#endif
11796
11797  if (!home_dir) {
11798    home_dir = getenv("HOME");
11799  }
11800
11801#if defined(_WIN32) || defined(WIN32)
11802  if (!home_dir) {
11803    char *zDrive, *zPath;
11804    int n;
11805    zDrive = getenv("HOMEDRIVE");
11806    zPath = getenv("HOMEPATH");
11807    if( zDrive && zPath ){
11808      n = strlen30(zDrive) + strlen30(zPath) + 1;
11809      home_dir = malloc( n );
11810      if( home_dir==0 ) return 0;
11811      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11812      return home_dir;
11813    }
11814    home_dir = "c:\\";
11815  }
11816#endif
11817
11818#endif /* !_WIN32_WCE */
11819
11820  if( home_dir ){
11821    i64 n = strlen(home_dir) + 1;
11822    char *z = malloc( n );
11823    if( z ) memcpy(z, home_dir, n);
11824    home_dir = z;
11825  }
11826
11827  return home_dir;
11828}
11829
11830/*
11831** Read input from the file given by sqliterc_override.  Or if that
11832** parameter is NULL, take input from ~/.sqliterc
11833**
11834** Returns the number of errors.
11835*/
11836static void process_sqliterc(
11837  ShellState *p,                  /* Configuration data */
11838  const char *sqliterc_override   /* Name of config file. NULL to use default */
11839){
11840  char *home_dir = NULL;
11841  const char *sqliterc = sqliterc_override;
11842  char *zBuf = 0;
11843  FILE *inSaved = p->in;
11844  int savedLineno = p->lineno;
11845
11846  if (sqliterc == NULL) {
11847    home_dir = find_home_dir(0);
11848    if( home_dir==0 ){
11849      raw_printf(stderr, "-- warning: cannot find home directory;"
11850                      " cannot read ~/.sqliterc\n");
11851      return;
11852    }
11853    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11854    shell_check_oom(zBuf);
11855    sqliterc = zBuf;
11856  }
11857  p->in = fopen(sqliterc,"rb");
11858  if( p->in ){
11859    if( stdin_is_interactive ){
11860      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11861    }
11862    if( process_input(p) && bail_on_error ) exit(1);
11863    fclose(p->in);
11864  }else if( sqliterc_override!=0 ){
11865    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11866    if( bail_on_error ) exit(1);
11867  }
11868  p->in = inSaved;
11869  p->lineno = savedLineno;
11870  sqlite3_free(zBuf);
11871}
11872
11873/*
11874** Show available command line options
11875*/
11876static const char zOptions[] =
11877#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11878  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11879#endif
11880  "   -append              append the database to the end of the file\n"
11881  "   -ascii               set output mode to 'ascii'\n"
11882  "   -bail                stop after hitting an error\n"
11883  "   -batch               force batch I/O\n"
11884  "   -box                 set output mode to 'box'\n"
11885  "   -column              set output mode to 'column'\n"
11886  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11887  "   -csv                 set output mode to 'csv'\n"
11888#if !defined(SQLITE_OMIT_DESERIALIZE)
11889  "   -deserialize         open the database using sqlite3_deserialize()\n"
11890#endif
11891  "   -echo                print inputs before execution\n"
11892  "   -init FILENAME       read/process named file\n"
11893  "   -[no]header          turn headers on or off\n"
11894#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11895  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11896#endif
11897  "   -help                show this message\n"
11898  "   -html                set output mode to HTML\n"
11899  "   -interactive         force interactive I/O\n"
11900  "   -json                set output mode to 'json'\n"
11901  "   -line                set output mode to 'line'\n"
11902  "   -list                set output mode to 'list'\n"
11903  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11904  "   -markdown            set output mode to 'markdown'\n"
11905#if !defined(SQLITE_OMIT_DESERIALIZE)
11906  "   -maxsize N           maximum size for a --deserialize database\n"
11907#endif
11908  "   -memtrace            trace all memory allocations and deallocations\n"
11909  "   -mmap N              default mmap size set to N\n"
11910#ifdef SQLITE_ENABLE_MULTIPLEX
11911  "   -multiplex           enable the multiplexor VFS\n"
11912#endif
11913  "   -newline SEP         set output row separator. Default: '\\n'\n"
11914  "   -nofollow            refuse to open symbolic links to database files\n"
11915  "   -nonce STRING        set the safe-mode escape nonce\n"
11916  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11917  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11918  "   -quote               set output mode to 'quote'\n"
11919  "   -readonly            open the database read-only\n"
11920  "   -safe                enable safe-mode\n"
11921  "   -separator SEP       set output column separator. Default: '|'\n"
11922#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11923  "   -sorterref SIZE      sorter references threshold size\n"
11924#endif
11925  "   -stats               print memory stats before each finalize\n"
11926  "   -table               set output mode to 'table'\n"
11927  "   -tabs                set output mode to 'tabs'\n"
11928  "   -version             show SQLite version\n"
11929  "   -vfs NAME            use NAME as the default VFS\n"
11930#ifdef SQLITE_ENABLE_VFSTRACE
11931  "   -vfstrace            enable tracing of all VFS calls\n"
11932#endif
11933#ifdef SQLITE_HAVE_ZLIB
11934  "   -zip                 open the file as a ZIP Archive\n"
11935#endif
11936;
11937static void usage(int showDetail){
11938  utf8_printf(stderr,
11939      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11940      "FILENAME is the name of an SQLite database. A new database is created\n"
11941      "if the file does not previously exist.\n", Argv0);
11942  if( showDetail ){
11943    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11944  }else{
11945    raw_printf(stderr, "Use the -help option for additional information\n");
11946  }
11947  exit(1);
11948}
11949
11950/*
11951** Internal check:  Verify that the SQLite is uninitialized.  Print a
11952** error message if it is initialized.
11953*/
11954static void verify_uninitialized(void){
11955  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11956    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11957                        " initialization.\n");
11958  }
11959}
11960
11961/*
11962** Initialize the state information in data
11963*/
11964static void main_init(ShellState *data) {
11965  memset(data, 0, sizeof(*data));
11966  data->normalMode = data->cMode = data->mode = MODE_List;
11967  data->autoExplain = 1;
11968  data->pAuxDb = &data->aAuxDb[0];
11969  memcpy(data->colSeparator,SEP_Column, 2);
11970  memcpy(data->rowSeparator,SEP_Row, 2);
11971  data->showHeader = 0;
11972  data->shellFlgs = SHFLG_Lookaside;
11973  verify_uninitialized();
11974  sqlite3_config(SQLITE_CONFIG_URI, 1);
11975  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11976  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11977  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11978  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11979}
11980
11981/*
11982** Output text to the console in a font that attracts extra attention.
11983*/
11984#ifdef _WIN32
11985static void printBold(const char *zText){
11986#if !SQLITE_OS_WINRT
11987  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11988  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11989  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11990  SetConsoleTextAttribute(out,
11991         FOREGROUND_RED|FOREGROUND_INTENSITY
11992  );
11993#endif
11994  printf("%s", zText);
11995#if !SQLITE_OS_WINRT
11996  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11997#endif
11998}
11999#else
12000static void printBold(const char *zText){
12001  printf("\033[1m%s\033[0m", zText);
12002}
12003#endif
12004
12005/*
12006** Get the argument to an --option.  Throw an error and die if no argument
12007** is available.
12008*/
12009static char *cmdline_option_value(int argc, char **argv, int i){
12010  if( i==argc ){
12011    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
12012            argv[0], argv[argc-1]);
12013    exit(1);
12014  }
12015  return argv[i];
12016}
12017
12018#ifndef SQLITE_SHELL_IS_UTF8
12019#  if (defined(_WIN32) || defined(WIN32)) \
12020   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12021#    define SQLITE_SHELL_IS_UTF8          (0)
12022#  else
12023#    define SQLITE_SHELL_IS_UTF8          (1)
12024#  endif
12025#endif
12026
12027#ifdef SQLITE_SHELL_FIDDLE
12028#  define main fiddle_main
12029#endif
12030
12031#if SQLITE_SHELL_IS_UTF8
12032int SQLITE_CDECL main(int argc, char **argv){
12033#else
12034int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
12035  char **argv;
12036#endif
12037#ifdef SQLITE_DEBUG
12038  sqlite3_int64 mem_main_enter = sqlite3_memory_used();
12039#endif
12040  char *zErrMsg = 0;
12041#ifdef SQLITE_SHELL_FIDDLE
12042#  define data shellState
12043#else
12044  ShellState data;
12045#endif
12046  const char *zInitFile = 0;
12047  int i;
12048  int rc = 0;
12049  int warnInmemoryDb = 0;
12050  int readStdin = 1;
12051  int nCmd = 0;
12052  char **azCmd = 0;
12053  const char *zVfs = 0;           /* Value of -vfs command-line option */
12054#if !SQLITE_SHELL_IS_UTF8
12055  char **argvToFree = 0;
12056  int argcToFree = 0;
12057#endif
12058
12059  setBinaryMode(stdin, 0);
12060  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12061#ifdef SQLITE_SHELL_FIDDLE
12062  stdin_is_interactive = 0;
12063  stdout_is_console = 1;
12064  data.wasm.zDefaultDbName = "/fiddle.sqlite3";
12065#else
12066  stdin_is_interactive = isatty(0);
12067  stdout_is_console = isatty(1);
12068#endif
12069
12070#if !defined(_WIN32_WCE)
12071  if( getenv("SQLITE_DEBUG_BREAK") ){
12072    if( isatty(0) && isatty(2) ){
12073      fprintf(stderr,
12074          "attach debugger to process %d and press any key to continue.\n",
12075          GETPID());
12076      fgetc(stdin);
12077    }else{
12078#if defined(_WIN32) || defined(WIN32)
12079#if SQLITE_OS_WINRT
12080      __debugbreak();
12081#else
12082      DebugBreak();
12083#endif
12084#elif defined(SIGTRAP)
12085      raise(SIGTRAP);
12086#endif
12087    }
12088  }
12089#endif
12090
12091#if USE_SYSTEM_SQLITE+0!=1
12092  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12093    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
12094            sqlite3_sourceid(), SQLITE_SOURCE_ID);
12095    exit(1);
12096  }
12097#endif
12098  main_init(&data);
12099
12100  /* On Windows, we must translate command-line arguments into UTF-8.
12101  ** The SQLite memory allocator subsystem has to be enabled in order to
12102  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
12103  ** subsequent sqlite3_config() calls will work.  So copy all results into
12104  ** memory that does not come from the SQLite memory allocator.
12105  */
12106#if !SQLITE_SHELL_IS_UTF8
12107  sqlite3_initialize();
12108  argvToFree = malloc(sizeof(argv[0])*argc*2);
12109  shell_check_oom(argvToFree);
12110  argcToFree = argc;
12111  argv = argvToFree + argc;
12112  for(i=0; i<argc; i++){
12113    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12114    i64 n;
12115    shell_check_oom(z);
12116    n = strlen(z);
12117    argv[i] = malloc( n+1 );
12118    shell_check_oom(argv[i]);
12119    memcpy(argv[i], z, n+1);
12120    argvToFree[i] = argv[i];
12121    sqlite3_free(z);
12122  }
12123  sqlite3_shutdown();
12124#endif
12125
12126  assert( argc>=1 && argv && argv[0] );
12127  Argv0 = argv[0];
12128
12129  /* Make sure we have a valid signal handler early, before anything
12130  ** else is done.
12131  */
12132#ifdef SIGINT
12133  signal(SIGINT, interrupt_handler);
12134#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12135  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
12136#endif
12137
12138#ifdef SQLITE_SHELL_DBNAME_PROC
12139  {
12140    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12141    ** of a C-function that will provide the name of the database file.  Use
12142    ** this compile-time option to embed this shell program in larger
12143    ** applications. */
12144    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12145    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12146    warnInmemoryDb = 0;
12147  }
12148#endif
12149
12150  /* Do an initial pass through the command-line argument to locate
12151  ** the name of the database file, the name of the initialization file,
12152  ** the size of the alternative malloc heap,
12153  ** and the first command to execute.
12154  */
12155  verify_uninitialized();
12156  for(i=1; i<argc; i++){
12157    char *z;
12158    z = argv[i];
12159    if( z[0]!='-' ){
12160      if( data.aAuxDb->zDbFilename==0 ){
12161        data.aAuxDb->zDbFilename = z;
12162      }else{
12163        /* Excesss arguments are interpreted as SQL (or dot-commands) and
12164        ** mean that nothing is read from stdin */
12165        readStdin = 0;
12166        nCmd++;
12167        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12168        shell_check_oom(azCmd);
12169        azCmd[nCmd-1] = z;
12170      }
12171    }
12172    if( z[1]=='-' ) z++;
12173    if( strcmp(z,"-separator")==0
12174     || strcmp(z,"-nullvalue")==0
12175     || strcmp(z,"-newline")==0
12176     || strcmp(z,"-cmd")==0
12177    ){
12178      (void)cmdline_option_value(argc, argv, ++i);
12179    }else if( strcmp(z,"-init")==0 ){
12180      zInitFile = cmdline_option_value(argc, argv, ++i);
12181    }else if( strcmp(z,"-batch")==0 ){
12182      /* Need to check for batch mode here to so we can avoid printing
12183      ** informational messages (like from process_sqliterc) before
12184      ** we do the actual processing of arguments later in a second pass.
12185      */
12186      stdin_is_interactive = 0;
12187    }else if( strcmp(z,"-heap")==0 ){
12188#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12189      const char *zSize;
12190      sqlite3_int64 szHeap;
12191
12192      zSize = cmdline_option_value(argc, argv, ++i);
12193      szHeap = integerValue(zSize);
12194      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12195      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12196#else
12197      (void)cmdline_option_value(argc, argv, ++i);
12198#endif
12199    }else if( strcmp(z,"-pagecache")==0 ){
12200      sqlite3_int64 n, sz;
12201      sz = integerValue(cmdline_option_value(argc,argv,++i));
12202      if( sz>70000 ) sz = 70000;
12203      if( sz<0 ) sz = 0;
12204      n = integerValue(cmdline_option_value(argc,argv,++i));
12205      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12206        n = 0xffffffffffffLL/sz;
12207      }
12208      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12209                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12210      data.shellFlgs |= SHFLG_Pagecache;
12211    }else if( strcmp(z,"-lookaside")==0 ){
12212      int n, sz;
12213      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12214      if( sz<0 ) sz = 0;
12215      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12216      if( n<0 ) n = 0;
12217      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12218      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12219    }else if( strcmp(z,"-threadsafe")==0 ){
12220      int n;
12221      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12222      switch( n ){
12223         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
12224         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
12225         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
12226      }
12227#ifdef SQLITE_ENABLE_VFSTRACE
12228    }else if( strcmp(z,"-vfstrace")==0 ){
12229      extern int vfstrace_register(
12230         const char *zTraceName,
12231         const char *zOldVfsName,
12232         int (*xOut)(const char*,void*),
12233         void *pOutArg,
12234         int makeDefault
12235      );
12236      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
12237#endif
12238#ifdef SQLITE_ENABLE_MULTIPLEX
12239    }else if( strcmp(z,"-multiplex")==0 ){
12240      extern int sqlite3_multiple_initialize(const char*,int);
12241      sqlite3_multiplex_initialize(0, 1);
12242#endif
12243    }else if( strcmp(z,"-mmap")==0 ){
12244      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12245      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12246#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12247    }else if( strcmp(z,"-sorterref")==0 ){
12248      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12249      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12250#endif
12251    }else if( strcmp(z,"-vfs")==0 ){
12252      zVfs = cmdline_option_value(argc, argv, ++i);
12253#ifdef SQLITE_HAVE_ZLIB
12254    }else if( strcmp(z,"-zip")==0 ){
12255      data.openMode = SHELL_OPEN_ZIPFILE;
12256#endif
12257    }else if( strcmp(z,"-append")==0 ){
12258      data.openMode = SHELL_OPEN_APPENDVFS;
12259#ifndef SQLITE_OMIT_DESERIALIZE
12260    }else if( strcmp(z,"-deserialize")==0 ){
12261      data.openMode = SHELL_OPEN_DESERIALIZE;
12262    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
12263      data.szMax = integerValue(argv[++i]);
12264#endif
12265    }else if( strcmp(z,"-readonly")==0 ){
12266      data.openMode = SHELL_OPEN_READONLY;
12267    }else if( strcmp(z,"-nofollow")==0 ){
12268      data.openFlags = SQLITE_OPEN_NOFOLLOW;
12269#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12270    }else if( strncmp(z, "-A",2)==0 ){
12271      /* All remaining command-line arguments are passed to the ".archive"
12272      ** command, so ignore them */
12273      break;
12274#endif
12275    }else if( strcmp(z, "-memtrace")==0 ){
12276      sqlite3MemTraceActivate(stderr);
12277    }else if( strcmp(z,"-bail")==0 ){
12278      bail_on_error = 1;
12279    }else if( strcmp(z,"-nonce")==0 ){
12280      free(data.zNonce);
12281      data.zNonce = strdup(argv[++i]);
12282    }else if( strcmp(z,"-safe")==0 ){
12283      /* no-op - catch this on the second pass */
12284    }
12285  }
12286  verify_uninitialized();
12287
12288
12289#ifdef SQLITE_SHELL_INIT_PROC
12290  {
12291    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12292    ** of a C-function that will perform initialization actions on SQLite that
12293    ** occur just before or after sqlite3_initialize(). Use this compile-time
12294    ** option to embed this shell program in larger applications. */
12295    extern void SQLITE_SHELL_INIT_PROC(void);
12296    SQLITE_SHELL_INIT_PROC();
12297  }
12298#else
12299  /* All the sqlite3_config() calls have now been made. So it is safe
12300  ** to call sqlite3_initialize() and process any command line -vfs option. */
12301  sqlite3_initialize();
12302#endif
12303
12304  if( zVfs ){
12305    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12306    if( pVfs ){
12307      sqlite3_vfs_register(pVfs, 1);
12308    }else{
12309      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
12310      exit(1);
12311    }
12312  }
12313
12314  if( data.pAuxDb->zDbFilename==0 ){
12315#ifndef SQLITE_OMIT_MEMORYDB
12316    data.pAuxDb->zDbFilename = ":memory:";
12317    warnInmemoryDb = argc==1;
12318#else
12319    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
12320    return 1;
12321#endif
12322  }
12323  data.out = stdout;
12324#ifndef SQLITE_SHELL_FIDDLE
12325  sqlite3_appendvfs_init(0,0,0);
12326#endif
12327
12328  /* Go ahead and open the database file if it already exists.  If the
12329  ** file does not exist, delay opening it.  This prevents empty database
12330  ** files from being created if a user mistypes the database name argument
12331  ** to the sqlite command-line tool.
12332  */
12333  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
12334    open_db(&data, 0);
12335  }
12336
12337  /* Process the initialization file if there is one.  If no -init option
12338  ** is given on the command line, look for a file named ~/.sqliterc and
12339  ** try to process it.
12340  */
12341  process_sqliterc(&data,zInitFile);
12342
12343  /* Make a second pass through the command-line argument and set
12344  ** options.  This second pass is delayed until after the initialization
12345  ** file is processed so that the command-line arguments will override
12346  ** settings in the initialization file.
12347  */
12348  for(i=1; i<argc; i++){
12349    char *z = argv[i];
12350    if( z[0]!='-' ) continue;
12351    if( z[1]=='-' ){ z++; }
12352    if( strcmp(z,"-init")==0 ){
12353      i++;
12354    }else if( strcmp(z,"-html")==0 ){
12355      data.mode = MODE_Html;
12356    }else if( strcmp(z,"-list")==0 ){
12357      data.mode = MODE_List;
12358    }else if( strcmp(z,"-quote")==0 ){
12359      data.mode = MODE_Quote;
12360      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
12361      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12362    }else if( strcmp(z,"-line")==0 ){
12363      data.mode = MODE_Line;
12364    }else if( strcmp(z,"-column")==0 ){
12365      data.mode = MODE_Column;
12366    }else if( strcmp(z,"-json")==0 ){
12367      data.mode = MODE_Json;
12368    }else if( strcmp(z,"-markdown")==0 ){
12369      data.mode = MODE_Markdown;
12370    }else if( strcmp(z,"-table")==0 ){
12371      data.mode = MODE_Table;
12372    }else if( strcmp(z,"-box")==0 ){
12373      data.mode = MODE_Box;
12374    }else if( strcmp(z,"-csv")==0 ){
12375      data.mode = MODE_Csv;
12376      memcpy(data.colSeparator,",",2);
12377#ifdef SQLITE_HAVE_ZLIB
12378    }else if( strcmp(z,"-zip")==0 ){
12379      data.openMode = SHELL_OPEN_ZIPFILE;
12380#endif
12381    }else if( strcmp(z,"-append")==0 ){
12382      data.openMode = SHELL_OPEN_APPENDVFS;
12383#ifndef SQLITE_OMIT_DESERIALIZE
12384    }else if( strcmp(z,"-deserialize")==0 ){
12385      data.openMode = SHELL_OPEN_DESERIALIZE;
12386    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
12387      data.szMax = integerValue(argv[++i]);
12388#endif
12389    }else if( strcmp(z,"-readonly")==0 ){
12390      data.openMode = SHELL_OPEN_READONLY;
12391    }else if( strcmp(z,"-nofollow")==0 ){
12392      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
12393    }else if( strcmp(z,"-ascii")==0 ){
12394      data.mode = MODE_Ascii;
12395      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
12396      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
12397    }else if( strcmp(z,"-tabs")==0 ){
12398      data.mode = MODE_List;
12399      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
12400      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12401    }else if( strcmp(z,"-separator")==0 ){
12402      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
12403                       "%s",cmdline_option_value(argc,argv,++i));
12404    }else if( strcmp(z,"-newline")==0 ){
12405      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
12406                       "%s",cmdline_option_value(argc,argv,++i));
12407    }else if( strcmp(z,"-nullvalue")==0 ){
12408      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
12409                       "%s",cmdline_option_value(argc,argv,++i));
12410    }else if( strcmp(z,"-header")==0 ){
12411      data.showHeader = 1;
12412      ShellSetFlag(&data, SHFLG_HeaderSet);
12413     }else if( strcmp(z,"-noheader")==0 ){
12414      data.showHeader = 0;
12415      ShellSetFlag(&data, SHFLG_HeaderSet);
12416    }else if( strcmp(z,"-echo")==0 ){
12417      ShellSetFlag(&data, SHFLG_Echo);
12418    }else if( strcmp(z,"-eqp")==0 ){
12419      data.autoEQP = AUTOEQP_on;
12420    }else if( strcmp(z,"-eqpfull")==0 ){
12421      data.autoEQP = AUTOEQP_full;
12422    }else if( strcmp(z,"-stats")==0 ){
12423      data.statsOn = 1;
12424    }else if( strcmp(z,"-scanstats")==0 ){
12425      data.scanstatsOn = 1;
12426    }else if( strcmp(z,"-backslash")==0 ){
12427      /* Undocumented command-line option: -backslash
12428      ** Causes C-style backslash escapes to be evaluated in SQL statements
12429      ** prior to sending the SQL into SQLite.  Useful for injecting
12430      ** crazy bytes in the middle of SQL statements for testing and debugging.
12431      */
12432      ShellSetFlag(&data, SHFLG_Backslash);
12433    }else if( strcmp(z,"-bail")==0 ){
12434      /* No-op.  The bail_on_error flag should already be set. */
12435    }else if( strcmp(z,"-version")==0 ){
12436      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
12437      return 0;
12438    }else if( strcmp(z,"-interactive")==0 ){
12439      stdin_is_interactive = 1;
12440    }else if( strcmp(z,"-batch")==0 ){
12441      stdin_is_interactive = 0;
12442    }else if( strcmp(z,"-heap")==0 ){
12443      i++;
12444    }else if( strcmp(z,"-pagecache")==0 ){
12445      i+=2;
12446    }else if( strcmp(z,"-lookaside")==0 ){
12447      i+=2;
12448    }else if( strcmp(z,"-threadsafe")==0 ){
12449      i+=2;
12450    }else if( strcmp(z,"-nonce")==0 ){
12451      i += 2;
12452    }else if( strcmp(z,"-mmap")==0 ){
12453      i++;
12454    }else if( strcmp(z,"-memtrace")==0 ){
12455      i++;
12456#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12457    }else if( strcmp(z,"-sorterref")==0 ){
12458      i++;
12459#endif
12460    }else if( strcmp(z,"-vfs")==0 ){
12461      i++;
12462#ifdef SQLITE_ENABLE_VFSTRACE
12463    }else if( strcmp(z,"-vfstrace")==0 ){
12464      i++;
12465#endif
12466#ifdef SQLITE_ENABLE_MULTIPLEX
12467    }else if( strcmp(z,"-multiplex")==0 ){
12468      i++;
12469#endif
12470    }else if( strcmp(z,"-help")==0 ){
12471      usage(1);
12472    }else if( strcmp(z,"-cmd")==0 ){
12473      /* Run commands that follow -cmd first and separately from commands
12474      ** that simply appear on the command-line.  This seems goofy.  It would
12475      ** be better if all commands ran in the order that they appear.  But
12476      ** we retain the goofy behavior for historical compatibility. */
12477      if( i==argc-1 ) break;
12478      z = cmdline_option_value(argc,argv,++i);
12479      if( z[0]=='.' ){
12480        rc = do_meta_command(z, &data);
12481        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
12482      }else{
12483        open_db(&data, 0);
12484        rc = shell_exec(&data, z, &zErrMsg);
12485        if( zErrMsg!=0 ){
12486          utf8_printf(stderr,"Error: %s\n", zErrMsg);
12487          if( bail_on_error ) return rc!=0 ? rc : 1;
12488        }else if( rc!=0 ){
12489          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
12490          if( bail_on_error ) return rc;
12491        }
12492      }
12493#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12494    }else if( strncmp(z, "-A", 2)==0 ){
12495      if( nCmd>0 ){
12496        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
12497                            " with \"%s\"\n", z);
12498        return 1;
12499      }
12500      open_db(&data, OPEN_DB_ZIPFILE);
12501      if( z[2] ){
12502        argv[i] = &z[2];
12503        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
12504      }else{
12505        arDotCommand(&data, 1, argv+i, argc-i);
12506      }
12507      readStdin = 0;
12508      break;
12509#endif
12510    }else if( strcmp(z,"-safe")==0 ){
12511      data.bSafeMode = data.bSafeModePersist = 1;
12512    }else{
12513      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
12514      raw_printf(stderr,"Use -help for a list of options.\n");
12515      return 1;
12516    }
12517    data.cMode = data.mode;
12518  }
12519
12520  if( !readStdin ){
12521    /* Run all arguments that do not begin with '-' as if they were separate
12522    ** command-line inputs, except for the argToSkip argument which contains
12523    ** the database filename.
12524    */
12525    for(i=0; i<nCmd; i++){
12526      if( azCmd[i][0]=='.' ){
12527        rc = do_meta_command(azCmd[i], &data);
12528        if( rc ){
12529          free(azCmd);
12530          return rc==2 ? 0 : rc;
12531        }
12532      }else{
12533        open_db(&data, 0);
12534        rc = shell_exec(&data, azCmd[i], &zErrMsg);
12535        if( zErrMsg || rc ){
12536          if( zErrMsg!=0 ){
12537            utf8_printf(stderr,"Error: %s\n", zErrMsg);
12538          }else{
12539            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
12540          }
12541          sqlite3_free(zErrMsg);
12542          free(azCmd);
12543          return rc!=0 ? rc : 1;
12544        }
12545      }
12546    }
12547  }else{
12548    /* Run commands received from standard input
12549    */
12550    if( stdin_is_interactive ){
12551      char *zHome;
12552      char *zHistory;
12553      int nHistory;
12554      printf(
12555        "SQLite version %s %.19s\n" /*extra-version-info*/
12556        "Enter \".help\" for usage hints.\n",
12557        sqlite3_libversion(), sqlite3_sourceid()
12558      );
12559      if( warnInmemoryDb ){
12560        printf("Connected to a ");
12561        printBold("transient in-memory database");
12562        printf(".\nUse \".open FILENAME\" to reopen on a "
12563               "persistent database.\n");
12564      }
12565      zHistory = getenv("SQLITE_HISTORY");
12566      if( zHistory ){
12567        zHistory = strdup(zHistory);
12568      }else if( (zHome = find_home_dir(0))!=0 ){
12569        nHistory = strlen30(zHome) + 20;
12570        if( (zHistory = malloc(nHistory))!=0 ){
12571          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
12572        }
12573      }
12574      if( zHistory ){ shell_read_history(zHistory); }
12575#if HAVE_READLINE || HAVE_EDITLINE
12576      rl_attempted_completion_function = readline_completion;
12577#elif HAVE_LINENOISE
12578      linenoiseSetCompletionCallback(linenoise_completion);
12579#endif
12580      data.in = 0;
12581      rc = process_input(&data);
12582      if( zHistory ){
12583        shell_stifle_history(2000);
12584        shell_write_history(zHistory);
12585        free(zHistory);
12586      }
12587    }else{
12588      data.in = stdin;
12589      rc = process_input(&data);
12590    }
12591  }
12592#ifndef SQLITE_SHELL_FIDDLE
12593  /* In WASM mode we have to leave the db state in place so that
12594  ** client code can "push" SQL into it after this call returns. */
12595  free(azCmd);
12596  set_table_name(&data, 0);
12597  if( data.db ){
12598    session_close_all(&data, -1);
12599    close_db(data.db);
12600  }
12601  for(i=0; i<ArraySize(data.aAuxDb); i++){
12602    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12603    if( data.aAuxDb[i].db ){
12604      session_close_all(&data, i);
12605      close_db(data.aAuxDb[i].db);
12606    }
12607  }
12608  find_home_dir(1);
12609  output_reset(&data);
12610  data.doXdgOpen = 0;
12611  clearTempFile(&data);
12612#if !SQLITE_SHELL_IS_UTF8
12613  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12614  free(argvToFree);
12615#endif
12616  free(data.colWidth);
12617  free(data.zNonce);
12618  /* Clear the global data structure so that valgrind will detect memory
12619  ** leaks */
12620  memset(&data, 0, sizeof(data));
12621#ifdef SQLITE_DEBUG
12622  if( sqlite3_memory_used()>mem_main_enter ){
12623    utf8_printf(stderr, "Memory leaked: %u bytes\n",
12624                (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12625  }
12626#endif
12627#endif /* !SQLITE_SHELL_FIDDLE */
12628  return rc;
12629}
12630
12631
12632#ifdef SQLITE_SHELL_FIDDLE
12633/* Only for emcc experimentation purposes. */
12634int fiddle_experiment(int a,int b){
12635  return a + b;
12636}
12637
12638/*
12639** Returns a pointer to the current DB handle.
12640*/
12641sqlite3 * fiddle_db_handle(){
12642  return globalDb;
12643}
12644
12645/*
12646** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
12647** "main" is assumed. Returns 0 if no db with the given name is
12648** open.
12649*/
12650sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
12651  sqlite3_vfs * pVfs = 0;
12652  if(globalDb){
12653    sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
12654                         SQLITE_FCNTL_VFS_POINTER, &pVfs);
12655  }
12656  return pVfs;
12657}
12658
12659/* Only for emcc experimentation purposes. */
12660sqlite3 * fiddle_db_arg(sqlite3 *arg){
12661    printf("fiddle_db_arg(%p)\n", (const void*)arg);
12662    return arg;
12663}
12664
12665/*
12666** Intended to be called via a SharedWorker() while a separate
12667** SharedWorker() (which manages the wasm module) is performing work
12668** which should be interrupted. Unfortunately, SharedWorker is not
12669** portable enough to make real use of.
12670*/
12671void fiddle_interrupt(void){
12672  if( globalDb ) sqlite3_interrupt(globalDb);
12673}
12674
12675/*
12676** Returns the filename of the given db name, assuming "main" if
12677** zDbName is NULL. Returns NULL if globalDb is not opened.
12678*/
12679const char * fiddle_db_filename(const char * zDbName){
12680    return globalDb
12681      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12682      : NULL;
12683}
12684
12685/*
12686** Completely wipes out the contents of the currently-opened database
12687** but leaves its storage intact for reuse.
12688*/
12689void fiddle_reset_db(void){
12690  if( globalDb ){
12691    int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
12692    if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
12693    sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
12694  }
12695}
12696
12697/*
12698** Uses the current database's VFS xRead to stream the db file's
12699** contents out to the given callback. The callback gets a single
12700** chunk of size n (its 2nd argument) on each call and must return 0
12701** on success, non-0 on error. This function returns 0 on success,
12702** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
12703** code from the callback. Note that this is not thread-friendly: it
12704** expects that it will be the only thread reading the db file and
12705** takes no measures to ensure that is the case.
12706*/
12707int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
12708  sqlite3_int64 nSize = 0;
12709  sqlite3_int64 nPos = 0;
12710  sqlite3_file * pFile = 0;
12711  unsigned char buf[1024 * 8];
12712  int nBuf = (int)sizeof(buf);
12713  int rc = shellState.db
12714    ? sqlite3_file_control(shellState.db, "main",
12715                           SQLITE_FCNTL_FILE_POINTER, &pFile)
12716    : SQLITE_NOTFOUND;
12717  if( rc ) return rc;
12718  rc = pFile->pMethods->xFileSize(pFile, &nSize);
12719  if( rc ) return rc;
12720  if(nSize % nBuf){
12721    /* DB size is not an even multiple of the buffer size. Reduce
12722    ** buffer size so that we do not unduly inflate the db size when
12723    ** exporting. */
12724    if(0 == nSize % 4096) nBuf = 4096;
12725    else if(0 == nSize % 2048) nBuf = 2048;
12726    else if(0 == nSize % 1024) nBuf = 1024;
12727    else nBuf = 512;
12728  }
12729  for( ; 0==rc && nPos<nSize; nPos += nBuf ){
12730    rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
12731    if(SQLITE_IOERR_SHORT_READ == rc){
12732      rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
12733    }
12734    if( 0==rc ) rc = xCallback(buf, nBuf);
12735  }
12736  return rc;
12737}
12738
12739/*
12740** Trivial exportable function for emscripten. It processes zSql as if
12741** it were input to the sqlite3 shell and redirects all output to the
12742** wasm binding. If fiddle_main() has not been called by the time this
12743** is called, this function calls it with a conservative set of
12744** flags.
12745*/
12746void fiddle_exec(const char * zSql){
12747  if(zSql && *zSql){
12748    if('.'==*zSql) puts(zSql);
12749    shellState.wasm.zInput = zSql;
12750    shellState.wasm.zPos = zSql;
12751    process_input(&shellState);
12752    shellState.wasm.zInput = shellState.wasm.zPos = 0;
12753  }
12754}
12755#endif /* SQLITE_SHELL_FIDDLE */
12756