xref: /sqlite-3.40.0/src/shell.c.in (revision de033d07)
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/* A version of strcmp() that works with NULL values */
253static int cli_strcmp(const char *a, const char *b){
254  if( a==0 ) a = "";
255  if( b==0 ) b = "";
256  return strcmp(a,b);
257}
258static int cli_strncmp(const char *a, const char *b, size_t n){
259  if( a==0 ) a = "";
260  if( b==0 ) b = "";
261  return strncmp(a,b,n);
262}
263
264/* Return the current wall-clock time */
265static sqlite3_int64 timeOfDay(void){
266  static sqlite3_vfs *clockVfs = 0;
267  sqlite3_int64 t;
268  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
269  if( clockVfs==0 ) return 0;  /* Never actually happens */
270  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
271    clockVfs->xCurrentTimeInt64(clockVfs, &t);
272  }else{
273    double r;
274    clockVfs->xCurrentTime(clockVfs, &r);
275    t = (sqlite3_int64)(r*86400000.0);
276  }
277  return t;
278}
279
280#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
281#include <sys/time.h>
282#include <sys/resource.h>
283
284/* VxWorks does not support getrusage() as far as we can determine */
285#if defined(_WRS_KERNEL) || defined(__RTP__)
286struct rusage {
287  struct timeval ru_utime; /* user CPU time used */
288  struct timeval ru_stime; /* system CPU time used */
289};
290#define getrusage(A,B) memset(B,0,sizeof(*B))
291#endif
292
293/* Saved resource information for the beginning of an operation */
294static struct rusage sBegin;  /* CPU time at start */
295static sqlite3_int64 iBegin;  /* Wall-clock time at start */
296
297/*
298** Begin timing an operation
299*/
300static void beginTimer(void){
301  if( enableTimer ){
302    getrusage(RUSAGE_SELF, &sBegin);
303    iBegin = timeOfDay();
304  }
305}
306
307/* Return the difference of two time_structs in seconds */
308static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
309  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
310         (double)(pEnd->tv_sec - pStart->tv_sec);
311}
312
313/*
314** Print the timing results.
315*/
316static void endTimer(void){
317  if( enableTimer ){
318    sqlite3_int64 iEnd = timeOfDay();
319    struct rusage sEnd;
320    getrusage(RUSAGE_SELF, &sEnd);
321    printf("Run Time: real %.3f user %f sys %f\n",
322       (iEnd - iBegin)*0.001,
323       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
324       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
325  }
326}
327
328#define BEGIN_TIMER beginTimer()
329#define END_TIMER endTimer()
330#define HAS_TIMER 1
331
332#elif (defined(_WIN32) || defined(WIN32))
333
334/* Saved resource information for the beginning of an operation */
335static HANDLE hProcess;
336static FILETIME ftKernelBegin;
337static FILETIME ftUserBegin;
338static sqlite3_int64 ftWallBegin;
339typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
340                                    LPFILETIME, LPFILETIME);
341static GETPROCTIMES getProcessTimesAddr = NULL;
342
343/*
344** Check to see if we have timer support.  Return 1 if necessary
345** support found (or found previously).
346*/
347static int hasTimer(void){
348  if( getProcessTimesAddr ){
349    return 1;
350  } else {
351#if !SQLITE_OS_WINRT
352    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
353    ** versions. See if the version we are running on has it, and if it
354    ** does, save off a pointer to it and the current process handle.
355    */
356    hProcess = GetCurrentProcess();
357    if( hProcess ){
358      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
359      if( NULL != hinstLib ){
360        getProcessTimesAddr =
361            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
362        if( NULL != getProcessTimesAddr ){
363          return 1;
364        }
365        FreeLibrary(hinstLib);
366      }
367    }
368#endif
369  }
370  return 0;
371}
372
373/*
374** Begin timing an operation
375*/
376static void beginTimer(void){
377  if( enableTimer && getProcessTimesAddr ){
378    FILETIME ftCreation, ftExit;
379    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
380                        &ftKernelBegin,&ftUserBegin);
381    ftWallBegin = timeOfDay();
382  }
383}
384
385/* Return the difference of two FILETIME structs in seconds */
386static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
387  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
388  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
389  return (double) ((i64End - i64Start) / 10000000.0);
390}
391
392/*
393** Print the timing results.
394*/
395static void endTimer(void){
396  if( enableTimer && getProcessTimesAddr){
397    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
398    sqlite3_int64 ftWallEnd = timeOfDay();
399    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
400    printf("Run Time: real %.3f user %f sys %f\n",
401       (ftWallEnd - ftWallBegin)*0.001,
402       timeDiff(&ftUserBegin, &ftUserEnd),
403       timeDiff(&ftKernelBegin, &ftKernelEnd));
404  }
405}
406
407#define BEGIN_TIMER beginTimer()
408#define END_TIMER endTimer()
409#define HAS_TIMER hasTimer()
410
411#else
412#define BEGIN_TIMER
413#define END_TIMER
414#define HAS_TIMER 0
415#endif
416
417/*
418** Used to prevent warnings about unused parameters
419*/
420#define UNUSED_PARAMETER(x) (void)(x)
421
422/*
423** Number of elements in an array
424*/
425#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
426
427/*
428** If the following flag is set, then command execution stops
429** at an error if we are not interactive.
430*/
431static int bail_on_error = 0;
432
433/*
434** Threat stdin as an interactive input if the following variable
435** is true.  Otherwise, assume stdin is connected to a file or pipe.
436*/
437static int stdin_is_interactive = 1;
438
439/*
440** On Windows systems we have to know if standard output is a console
441** in order to translate UTF-8 into MBCS.  The following variable is
442** true if translation is required.
443*/
444static int stdout_is_console = 1;
445
446/*
447** The following is the open SQLite database.  We make a pointer
448** to this database a static variable so that it can be accessed
449** by the SIGINT handler to interrupt database processing.
450*/
451static sqlite3 *globalDb = 0;
452
453/*
454** True if an interrupt (Control-C) has been received.
455*/
456static volatile int seenInterrupt = 0;
457
458/*
459** This is the name of our program. It is set in main(), used
460** in a number of other places, mostly for error messages.
461*/
462static char *Argv0;
463
464/*
465** Prompt strings. Initialized in main. Settable with
466**   .prompt main continue
467*/
468static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
469static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
470
471/*
472** Render output like fprintf().  Except, if the output is going to the
473** console and if this is running on a Windows machine, translate the
474** output from UTF-8 into MBCS.
475*/
476#if defined(_WIN32) || defined(WIN32)
477void utf8_printf(FILE *out, const char *zFormat, ...){
478  va_list ap;
479  va_start(ap, zFormat);
480  if( stdout_is_console && (out==stdout || out==stderr) ){
481    char *z1 = sqlite3_vmprintf(zFormat, ap);
482    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
483    sqlite3_free(z1);
484    fputs(z2, out);
485    sqlite3_free(z2);
486  }else{
487    vfprintf(out, zFormat, ap);
488  }
489  va_end(ap);
490}
491#elif !defined(utf8_printf)
492# define utf8_printf fprintf
493#endif
494
495/*
496** Render output like fprintf().  This should not be used on anything that
497** includes string formatting (e.g. "%s").
498*/
499#if !defined(raw_printf)
500# define raw_printf fprintf
501#endif
502
503/* Indicate out-of-memory and exit. */
504static void shell_out_of_memory(void){
505  raw_printf(stderr,"Error: out of memory\n");
506  exit(1);
507}
508
509/* Check a pointer to see if it is NULL.  If it is NULL, exit with an
510** out-of-memory error.
511*/
512static void shell_check_oom(void *p){
513  if( p==0 ) shell_out_of_memory();
514}
515
516/*
517** Write I/O traces to the following stream.
518*/
519#ifdef SQLITE_ENABLE_IOTRACE
520static FILE *iotrace = 0;
521#endif
522
523/*
524** This routine works like printf in that its first argument is a
525** format string and subsequent arguments are values to be substituted
526** in place of % fields.  The result of formatting this string
527** is written to iotrace.
528*/
529#ifdef SQLITE_ENABLE_IOTRACE
530static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
531  va_list ap;
532  char *z;
533  if( iotrace==0 ) return;
534  va_start(ap, zFormat);
535  z = sqlite3_vmprintf(zFormat, ap);
536  va_end(ap);
537  utf8_printf(iotrace, "%s", z);
538  sqlite3_free(z);
539}
540#endif
541
542/*
543** Output string zUtf to stream pOut as w characters.  If w is negative,
544** then right-justify the text.  W is the width in UTF-8 characters, not
545** in bytes.  This is different from the %*.*s specification in printf
546** since with %*.*s the width is measured in bytes, not characters.
547*/
548static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
549  int i;
550  int n;
551  int aw = w<0 ? -w : w;
552  for(i=n=0; zUtf[i]; i++){
553    if( (zUtf[i]&0xc0)!=0x80 ){
554      n++;
555      if( n==aw ){
556        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
557        break;
558      }
559    }
560  }
561  if( n>=aw ){
562    utf8_printf(pOut, "%.*s", i, zUtf);
563  }else if( w<0 ){
564    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
565  }else{
566    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
567  }
568}
569
570
571/*
572** Determines if a string is a number of not.
573*/
574static int isNumber(const char *z, int *realnum){
575  if( *z=='-' || *z=='+' ) z++;
576  if( !IsDigit(*z) ){
577    return 0;
578  }
579  z++;
580  if( realnum ) *realnum = 0;
581  while( IsDigit(*z) ){ z++; }
582  if( *z=='.' ){
583    z++;
584    if( !IsDigit(*z) ) return 0;
585    while( IsDigit(*z) ){ z++; }
586    if( realnum ) *realnum = 1;
587  }
588  if( *z=='e' || *z=='E' ){
589    z++;
590    if( *z=='+' || *z=='-' ) z++;
591    if( !IsDigit(*z) ) return 0;
592    while( IsDigit(*z) ){ z++; }
593    if( realnum ) *realnum = 1;
594  }
595  return *z==0;
596}
597
598/*
599** Compute a string length that is limited to what can be stored in
600** lower 30 bits of a 32-bit signed integer.
601*/
602static int strlen30(const char *z){
603  const char *z2 = z;
604  while( *z2 ){ z2++; }
605  return 0x3fffffff & (int)(z2 - z);
606}
607
608/*
609** Return the length of a string in characters.  Multibyte UTF8 characters
610** count as a single character.
611*/
612static int strlenChar(const char *z){
613  int n = 0;
614  while( *z ){
615    if( (0xc0&*(z++))!=0x80 ) n++;
616  }
617  return n;
618}
619
620/*
621** Return open FILE * if zFile exists, can be opened for read
622** and is an ordinary file or a character stream source.
623** Otherwise return 0.
624*/
625static FILE * openChrSource(const char *zFile){
626#ifdef _WIN32
627  struct _stat x = {0};
628# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
629  /* On Windows, open first, then check the stream nature. This order
630  ** is necessary because _stat() and sibs, when checking a named pipe,
631  ** effectively break the pipe as its supplier sees it. */
632  FILE *rv = fopen(zFile, "rb");
633  if( rv==0 ) return 0;
634  if( _fstat(_fileno(rv), &x) != 0
635      || !STAT_CHR_SRC(x.st_mode)){
636    fclose(rv);
637    rv = 0;
638  }
639  return rv;
640#else
641  struct stat x = {0};
642  int rc = stat(zFile, &x);
643# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
644  if( rc!=0 ) return 0;
645  if( STAT_CHR_SRC(x.st_mode) ){
646    return fopen(zFile, "rb");
647  }else{
648    return 0;
649  }
650#endif
651#undef STAT_CHR_SRC
652}
653
654/*
655** This routine reads a line of text from FILE in, stores
656** the text in memory obtained from malloc() and returns a pointer
657** to the text.  NULL is returned at end of file, or if malloc()
658** fails.
659**
660** If zLine is not NULL then it is a malloced buffer returned from
661** a previous call to this routine that may be reused.
662*/
663static char *local_getline(char *zLine, FILE *in){
664  int nLine = zLine==0 ? 0 : 100;
665  int n = 0;
666
667  while( 1 ){
668    if( n+100>nLine ){
669      nLine = nLine*2 + 100;
670      zLine = realloc(zLine, nLine);
671      shell_check_oom(zLine);
672    }
673    if( fgets(&zLine[n], nLine - n, in)==0 ){
674      if( n==0 ){
675        free(zLine);
676        return 0;
677      }
678      zLine[n] = 0;
679      break;
680    }
681    while( zLine[n] ) n++;
682    if( n>0 && zLine[n-1]=='\n' ){
683      n--;
684      if( n>0 && zLine[n-1]=='\r' ) n--;
685      zLine[n] = 0;
686      break;
687    }
688  }
689#if defined(_WIN32) || defined(WIN32)
690  /* For interactive input on Windows systems, translate the
691  ** multi-byte characterset characters into UTF-8. */
692  if( stdin_is_interactive && in==stdin ){
693    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
694    if( zTrans ){
695      i64 nTrans = strlen(zTrans)+1;
696      if( nTrans>nLine ){
697        zLine = realloc(zLine, nTrans);
698        shell_check_oom(zLine);
699      }
700      memcpy(zLine, zTrans, nTrans);
701      sqlite3_free(zTrans);
702    }
703  }
704#endif /* defined(_WIN32) || defined(WIN32) */
705  return zLine;
706}
707
708/*
709** Retrieve a single line of input text.
710**
711** If in==0 then read from standard input and prompt before each line.
712** If isContinuation is true, then a continuation prompt is appropriate.
713** If isContinuation is zero, then the main prompt should be used.
714**
715** If zPrior is not NULL then it is a buffer from a prior call to this
716** routine that can be reused.
717**
718** The result is stored in space obtained from malloc() and must either
719** be freed by the caller or else passed back into this routine via the
720** zPrior argument for reuse.
721*/
722#ifndef SQLITE_SHELL_FIDDLE
723static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
724  char *zPrompt;
725  char *zResult;
726  if( in!=0 ){
727    zResult = local_getline(zPrior, in);
728  }else{
729    zPrompt = isContinuation ? continuePrompt : mainPrompt;
730#if SHELL_USE_LOCAL_GETLINE
731    printf("%s", zPrompt);
732    fflush(stdout);
733    zResult = local_getline(zPrior, stdin);
734#else
735    free(zPrior);
736    zResult = shell_readline(zPrompt);
737    if( zResult && *zResult ) shell_add_history(zResult);
738#endif
739  }
740  return zResult;
741}
742#endif /* !SQLITE_SHELL_FIDDLE */
743
744/*
745** Return the value of a hexadecimal digit.  Return -1 if the input
746** is not a hex digit.
747*/
748static int hexDigitValue(char c){
749  if( c>='0' && c<='9' ) return c - '0';
750  if( c>='a' && c<='f' ) return c - 'a' + 10;
751  if( c>='A' && c<='F' ) return c - 'A' + 10;
752  return -1;
753}
754
755/*
756** Interpret zArg as an integer value, possibly with suffixes.
757*/
758static sqlite3_int64 integerValue(const char *zArg){
759  sqlite3_int64 v = 0;
760  static const struct { char *zSuffix; int iMult; } aMult[] = {
761    { "KiB", 1024 },
762    { "MiB", 1024*1024 },
763    { "GiB", 1024*1024*1024 },
764    { "KB",  1000 },
765    { "MB",  1000000 },
766    { "GB",  1000000000 },
767    { "K",   1000 },
768    { "M",   1000000 },
769    { "G",   1000000000 },
770  };
771  int i;
772  int isNeg = 0;
773  if( zArg[0]=='-' ){
774    isNeg = 1;
775    zArg++;
776  }else if( zArg[0]=='+' ){
777    zArg++;
778  }
779  if( zArg[0]=='0' && zArg[1]=='x' ){
780    int x;
781    zArg += 2;
782    while( (x = hexDigitValue(zArg[0]))>=0 ){
783      v = (v<<4) + x;
784      zArg++;
785    }
786  }else{
787    while( IsDigit(zArg[0]) ){
788      v = v*10 + zArg[0] - '0';
789      zArg++;
790    }
791  }
792  for(i=0; i<ArraySize(aMult); i++){
793    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
794      v *= aMult[i].iMult;
795      break;
796    }
797  }
798  return isNeg? -v : v;
799}
800
801/*
802** A variable length string to which one can append text.
803*/
804typedef struct ShellText ShellText;
805struct ShellText {
806  char *z;
807  int n;
808  int nAlloc;
809};
810
811/*
812** Initialize and destroy a ShellText object
813*/
814static void initText(ShellText *p){
815  memset(p, 0, sizeof(*p));
816}
817static void freeText(ShellText *p){
818  free(p->z);
819  initText(p);
820}
821
822/* zIn is either a pointer to a NULL-terminated string in memory obtained
823** from malloc(), or a NULL pointer. The string pointed to by zAppend is
824** added to zIn, and the result returned in memory obtained from malloc().
825** zIn, if it was not NULL, is freed.
826**
827** If the third argument, quote, is not '\0', then it is used as a
828** quote character for zAppend.
829*/
830static void appendText(ShellText *p, const char *zAppend, char quote){
831  i64 len;
832  i64 i;
833  i64 nAppend = strlen30(zAppend);
834
835  len = nAppend+p->n+1;
836  if( quote ){
837    len += 2;
838    for(i=0; i<nAppend; i++){
839      if( zAppend[i]==quote ) len++;
840    }
841  }
842
843  if( p->z==0 || p->n+len>=p->nAlloc ){
844    p->nAlloc = p->nAlloc*2 + len + 20;
845    p->z = realloc(p->z, p->nAlloc);
846    shell_check_oom(p->z);
847  }
848
849  if( quote ){
850    char *zCsr = p->z+p->n;
851    *zCsr++ = quote;
852    for(i=0; i<nAppend; i++){
853      *zCsr++ = zAppend[i];
854      if( zAppend[i]==quote ) *zCsr++ = quote;
855    }
856    *zCsr++ = quote;
857    p->n = (int)(zCsr - p->z);
858    *zCsr = '\0';
859  }else{
860    memcpy(p->z+p->n, zAppend, nAppend);
861    p->n += nAppend;
862    p->z[p->n] = '\0';
863  }
864}
865
866/*
867** Attempt to determine if identifier zName needs to be quoted, either
868** because it contains non-alphanumeric characters, or because it is an
869** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
870** that quoting is required.
871**
872** Return '"' if quoting is required.  Return 0 if no quoting is required.
873*/
874static char quoteChar(const char *zName){
875  int i;
876  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
877  for(i=0; zName[i]; i++){
878    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
879  }
880  return sqlite3_keyword_check(zName, i) ? '"' : 0;
881}
882
883/*
884** Construct a fake object name and column list to describe the structure
885** of the view, virtual table, or table valued function zSchema.zName.
886*/
887static char *shellFakeSchema(
888  sqlite3 *db,            /* The database connection containing the vtab */
889  const char *zSchema,    /* Schema of the database holding the vtab */
890  const char *zName       /* The name of the virtual table */
891){
892  sqlite3_stmt *pStmt = 0;
893  char *zSql;
894  ShellText s;
895  char cQuote;
896  char *zDiv = "(";
897  int nRow = 0;
898
899  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
900                         zSchema ? zSchema : "main", zName);
901  shell_check_oom(zSql);
902  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
903  sqlite3_free(zSql);
904  initText(&s);
905  if( zSchema ){
906    cQuote = quoteChar(zSchema);
907    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
908    appendText(&s, zSchema, cQuote);
909    appendText(&s, ".", 0);
910  }
911  cQuote = quoteChar(zName);
912  appendText(&s, zName, cQuote);
913  while( sqlite3_step(pStmt)==SQLITE_ROW ){
914    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
915    nRow++;
916    appendText(&s, zDiv, 0);
917    zDiv = ",";
918    if( zCol==0 ) zCol = "";
919    cQuote = quoteChar(zCol);
920    appendText(&s, zCol, cQuote);
921  }
922  appendText(&s, ")", 0);
923  sqlite3_finalize(pStmt);
924  if( nRow==0 ){
925    freeText(&s);
926    s.z = 0;
927  }
928  return s.z;
929}
930
931/*
932** SQL function:  shell_module_schema(X)
933**
934** Return a fake schema for the table-valued function or eponymous virtual
935** table X.
936*/
937static void shellModuleSchema(
938  sqlite3_context *pCtx,
939  int nVal,
940  sqlite3_value **apVal
941){
942  const char *zName;
943  char *zFake;
944  UNUSED_PARAMETER(nVal);
945  zName = (const char*)sqlite3_value_text(apVal[0]);
946  zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
947  if( zFake ){
948    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
949                        -1, sqlite3_free);
950    free(zFake);
951  }
952}
953
954/*
955** SQL function:  shell_add_schema(S,X)
956**
957** Add the schema name X to the CREATE statement in S and return the result.
958** Examples:
959**
960**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
961**
962** Also works on
963**
964**    CREATE INDEX
965**    CREATE UNIQUE INDEX
966**    CREATE VIEW
967**    CREATE TRIGGER
968**    CREATE VIRTUAL TABLE
969**
970** This UDF is used by the .schema command to insert the schema name of
971** attached databases into the middle of the sqlite_schema.sql field.
972*/
973static void shellAddSchemaName(
974  sqlite3_context *pCtx,
975  int nVal,
976  sqlite3_value **apVal
977){
978  static const char *aPrefix[] = {
979     "TABLE",
980     "INDEX",
981     "UNIQUE INDEX",
982     "VIEW",
983     "TRIGGER",
984     "VIRTUAL TABLE"
985  };
986  int i = 0;
987  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
988  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
989  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
990  sqlite3 *db = sqlite3_context_db_handle(pCtx);
991  UNUSED_PARAMETER(nVal);
992  if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
993    for(i=0; i<ArraySize(aPrefix); i++){
994      int n = strlen30(aPrefix[i]);
995      if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
996        char *z = 0;
997        char *zFake = 0;
998        if( zSchema ){
999          char cQuote = quoteChar(zSchema);
1000          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1001            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1002          }else{
1003            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1004          }
1005        }
1006        if( zName
1007         && aPrefix[i][0]=='V'
1008         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1009        ){
1010          if( z==0 ){
1011            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1012          }else{
1013            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1014          }
1015          free(zFake);
1016        }
1017        if( z ){
1018          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1019          return;
1020        }
1021      }
1022    }
1023  }
1024  sqlite3_result_value(pCtx, apVal[0]);
1025}
1026
1027/*
1028** The source code for several run-time loadable extensions is inserted
1029** below by the ../tool/mkshellc.tcl script.  Before processing that included
1030** code, we need to override some macros to make the included program code
1031** work here in the middle of this regular program.
1032*/
1033#define SQLITE_EXTENSION_INIT1
1034#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1035
1036#if defined(_WIN32) && defined(_MSC_VER)
1037INCLUDE test_windirent.h
1038INCLUDE test_windirent.c
1039#define dirent DIRENT
1040#endif
1041INCLUDE ../ext/misc/memtrace.c
1042INCLUDE ../ext/misc/shathree.c
1043INCLUDE ../ext/misc/uint.c
1044INCLUDE ../ext/misc/decimal.c
1045INCLUDE ../ext/misc/ieee754.c
1046INCLUDE ../ext/misc/series.c
1047INCLUDE ../ext/misc/regexp.c
1048#ifndef SQLITE_SHELL_FIDDLE
1049INCLUDE ../ext/misc/fileio.c
1050INCLUDE ../ext/misc/completion.c
1051INCLUDE ../ext/misc/appendvfs.c
1052#endif
1053#ifdef SQLITE_HAVE_ZLIB
1054INCLUDE ../ext/misc/zipfile.c
1055INCLUDE ../ext/misc/sqlar.c
1056#endif
1057INCLUDE ../ext/expert/sqlite3expert.h
1058INCLUDE ../ext/expert/sqlite3expert.c
1059
1060#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1061INCLUDE ../ext/misc/dbdata.c
1062#endif
1063
1064#if defined(SQLITE_ENABLE_SESSION)
1065/*
1066** State information for a single open session
1067*/
1068typedef struct OpenSession OpenSession;
1069struct OpenSession {
1070  char *zName;             /* Symbolic name for this session */
1071  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1072  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1073  sqlite3_session *p;      /* The open session */
1074};
1075#endif
1076
1077typedef struct ExpertInfo ExpertInfo;
1078struct ExpertInfo {
1079  sqlite3expert *pExpert;
1080  int bVerbose;
1081};
1082
1083/* A single line in the EQP output */
1084typedef struct EQPGraphRow EQPGraphRow;
1085struct EQPGraphRow {
1086  int iEqpId;           /* ID for this row */
1087  int iParentId;        /* ID of the parent row */
1088  EQPGraphRow *pNext;   /* Next row in sequence */
1089  char zText[1];        /* Text to display for this row */
1090};
1091
1092/* All EQP output is collected into an instance of the following */
1093typedef struct EQPGraph EQPGraph;
1094struct EQPGraph {
1095  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1096  EQPGraphRow *pLast;   /* Last element of the pRow list */
1097  char zPrefix[100];    /* Graph prefix */
1098};
1099
1100/* Parameters affecting columnar mode result display (defaulting together) */
1101typedef struct ColModeOpts {
1102  int iWrap;            /* In columnar modes, wrap lines reaching this limit */
1103  u8 bQuote;            /* Quote results for .mode box and table */
1104  u8 bWordWrap;         /* In columnar modes, wrap at word boundaries  */
1105} ColModeOpts;
1106#define ColModeOpts_default { 60, 0, 0 }
1107#define ColModeOpts_default_qbox { 60, 1, 0 }
1108
1109/*
1110** State information about the database connection is contained in an
1111** instance of the following structure.
1112*/
1113typedef struct ShellState ShellState;
1114struct ShellState {
1115  sqlite3 *db;           /* The database */
1116  u8 autoExplain;        /* Automatically turn on .explain mode */
1117  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1118  u8 autoEQPtest;        /* autoEQP is in test mode */
1119  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1120  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1121  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1122  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1123  u8 nEqpLevel;          /* Depth of the EQP output graph */
1124  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1125  u8 bSafeMode;          /* True to prohibit unsafe operations */
1126  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
1127  ColModeOpts cmOpts;    /* Option values affecting columnar mode output */
1128  unsigned statsOn;      /* True to display memory stats before each finalize */
1129  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1130  int inputNesting;      /* Track nesting level of .read and other redirects */
1131  int outCount;          /* Revert to stdout when reaching zero */
1132  int cnt;               /* Number of records displayed so far */
1133  int lineno;            /* Line number of last line read from in */
1134  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1135  FILE *in;              /* Read commands from this stream */
1136  FILE *out;             /* Write results here */
1137  FILE *traceOut;        /* Output for sqlite3_trace() */
1138  int nErr;              /* Number of errors seen */
1139  int mode;              /* An output mode setting */
1140  int modePrior;         /* Saved mode */
1141  int cMode;             /* temporary output mode for the current query */
1142  int normalMode;        /* Output mode before ".explain on" */
1143  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1144  int showHeader;        /* True to show column names in List or Column mode */
1145  int nCheck;            /* Number of ".check" commands run */
1146  unsigned nProgress;    /* Number of progress callbacks encountered */
1147  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1148  unsigned flgProgress;  /* Flags for the progress callback */
1149  unsigned shellFlgs;    /* Various flags */
1150  unsigned priorShFlgs;  /* Saved copy of flags */
1151  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1152  char *zDestTable;      /* Name of destination table when MODE_Insert */
1153  char *zTempFile;       /* Temporary file that might need deleting */
1154  char zTestcase[30];    /* Name of current test case */
1155  char colSeparator[20]; /* Column separator character for several modes */
1156  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1157  char colSepPrior[20];  /* Saved column separator */
1158  char rowSepPrior[20];  /* Saved row separator */
1159  int *colWidth;         /* Requested width of each column in columnar modes */
1160  int *actualWidth;      /* Actual width of each column */
1161  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1162  char nullValue[20];    /* The text to print when a NULL comes back from
1163                         ** the database */
1164  char outfile[FILENAME_MAX]; /* Filename for *out */
1165  sqlite3_stmt *pStmt;   /* Current statement if any. */
1166  FILE *pLog;            /* Write log output here */
1167  struct AuxDb {         /* Storage space for auxiliary database connections */
1168    sqlite3 *db;               /* Connection pointer */
1169    const char *zDbFilename;   /* Filename used to open the connection */
1170    char *zFreeOnClose;        /* Free this memory allocation on close */
1171#if defined(SQLITE_ENABLE_SESSION)
1172    int nSession;              /* Number of active sessions */
1173    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
1174#endif
1175  } aAuxDb[5],           /* Array of all database connections */
1176    *pAuxDb;             /* Currently active database connection */
1177  int *aiIndent;         /* Array of indents used in MODE_Explain */
1178  int nIndent;           /* Size of array aiIndent[] */
1179  int iIndent;           /* Index of current op in aiIndent[] */
1180  char *zNonce;          /* Nonce for temporary safe-mode excapes */
1181  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1182  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
1183#ifdef SQLITE_SHELL_FIDDLE
1184  struct {
1185    const char * zInput; /* Input string from wasm/JS proxy */
1186    const char * zPos;   /* Cursor pos into zInput */
1187    const char * zDefaultDbName; /* Default name for db file */
1188  } wasm;
1189#endif
1190};
1191
1192#ifdef SQLITE_SHELL_FIDDLE
1193static ShellState shellState;
1194#endif
1195
1196
1197/* Allowed values for ShellState.autoEQP
1198*/
1199#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1200#define AUTOEQP_on       1           /* Automatic EQP is on */
1201#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1202#define AUTOEQP_full     3           /* Show full EXPLAIN */
1203
1204/* Allowed values for ShellState.openMode
1205*/
1206#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1207#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1208#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1209#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1210#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1211#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1212#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1213
1214/* Allowed values for ShellState.eTraceType
1215*/
1216#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1217#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1218#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1219
1220/* Bits in the ShellState.flgProgress variable */
1221#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1222#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1223                                   ** callback limit is reached, and for each
1224                                   ** top-level SQL statement */
1225#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1226
1227/*
1228** These are the allowed shellFlgs values
1229*/
1230#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1231#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1232#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1233#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1234#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1235#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1236#define SHFLG_Echo           0x00000040 /* .echo on/off, or --echo setting */
1237#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1238#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1239#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1240
1241/*
1242** Macros for testing and setting shellFlgs
1243*/
1244#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1245#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1246#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1247
1248/*
1249** These are the allowed modes.
1250*/
1251#define MODE_Line     0  /* One column per line.  Blank line between records */
1252#define MODE_Column   1  /* One record per line in neat columns */
1253#define MODE_List     2  /* One record per line with a separator */
1254#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1255#define MODE_Html     4  /* Generate an XHTML table */
1256#define MODE_Insert   5  /* Generate SQL "insert" statements */
1257#define MODE_Quote    6  /* Quote values as for SQL */
1258#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1259#define MODE_Csv      8  /* Quote strings, numbers are plain */
1260#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1261#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1262#define MODE_Pretty  11  /* Pretty-print schemas */
1263#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1264#define MODE_Json    13  /* Output JSON */
1265#define MODE_Markdown 14 /* Markdown formatting */
1266#define MODE_Table   15  /* MySQL-style table formatting */
1267#define MODE_Box     16  /* Unicode box-drawing characters */
1268#define MODE_Count   17  /* Output only a count of the rows of output */
1269#define MODE_Off     18  /* No query output shown */
1270
1271static const char *modeDescr[] = {
1272  "line",
1273  "column",
1274  "list",
1275  "semi",
1276  "html",
1277  "insert",
1278  "quote",
1279  "tcl",
1280  "csv",
1281  "explain",
1282  "ascii",
1283  "prettyprint",
1284  "eqp",
1285  "json",
1286  "markdown",
1287  "table",
1288  "box",
1289  "count",
1290  "off"
1291};
1292
1293/*
1294** These are the column/row/line separators used by the various
1295** import/export modes.
1296*/
1297#define SEP_Column    "|"
1298#define SEP_Row       "\n"
1299#define SEP_Tab       "\t"
1300#define SEP_Space     " "
1301#define SEP_Comma     ","
1302#define SEP_CrLf      "\r\n"
1303#define SEP_Unit      "\x1F"
1304#define SEP_Record    "\x1E"
1305
1306/*
1307** Limit input nesting via .read or any other input redirect.
1308** It's not too expensive, so a generous allowance can be made.
1309*/
1310#define MAX_INPUT_NESTING 25
1311
1312/*
1313** A callback for the sqlite3_log() interface.
1314*/
1315static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1316  ShellState *p = (ShellState*)pArg;
1317  if( p->pLog==0 ) return;
1318  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1319  fflush(p->pLog);
1320}
1321
1322/*
1323** SQL function:  shell_putsnl(X)
1324**
1325** Write the text X to the screen (or whatever output is being directed)
1326** adding a newline at the end, and then return X.
1327*/
1328static void shellPutsFunc(
1329  sqlite3_context *pCtx,
1330  int nVal,
1331  sqlite3_value **apVal
1332){
1333  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1334  (void)nVal;
1335  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1336  sqlite3_result_value(pCtx, apVal[0]);
1337}
1338
1339/*
1340** If in safe mode, print an error message described by the arguments
1341** and exit immediately.
1342*/
1343static void failIfSafeMode(
1344  ShellState *p,
1345  const char *zErrMsg,
1346  ...
1347){
1348  if( p->bSafeMode ){
1349    va_list ap;
1350    char *zMsg;
1351    va_start(ap, zErrMsg);
1352    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1353    va_end(ap);
1354    raw_printf(stderr, "line %d: ", p->lineno);
1355    utf8_printf(stderr, "%s\n", zMsg);
1356    exit(1);
1357  }
1358}
1359
1360/*
1361** SQL function:   edit(VALUE)
1362**                 edit(VALUE,EDITOR)
1363**
1364** These steps:
1365**
1366**     (1) Write VALUE into a temporary file.
1367**     (2) Run program EDITOR on that temporary file.
1368**     (3) Read the temporary file back and return its content as the result.
1369**     (4) Delete the temporary file
1370**
1371** If the EDITOR argument is omitted, use the value in the VISUAL
1372** environment variable.  If still there is no EDITOR, through an error.
1373**
1374** Also throw an error if the EDITOR program returns a non-zero exit code.
1375*/
1376#ifndef SQLITE_NOHAVE_SYSTEM
1377static void editFunc(
1378  sqlite3_context *context,
1379  int argc,
1380  sqlite3_value **argv
1381){
1382  const char *zEditor;
1383  char *zTempFile = 0;
1384  sqlite3 *db;
1385  char *zCmd = 0;
1386  int bBin;
1387  int rc;
1388  int hasCRNL = 0;
1389  FILE *f = 0;
1390  sqlite3_int64 sz;
1391  sqlite3_int64 x;
1392  unsigned char *p = 0;
1393
1394  if( argc==2 ){
1395    zEditor = (const char*)sqlite3_value_text(argv[1]);
1396  }else{
1397    zEditor = getenv("VISUAL");
1398  }
1399  if( zEditor==0 ){
1400    sqlite3_result_error(context, "no editor for edit()", -1);
1401    return;
1402  }
1403  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1404    sqlite3_result_error(context, "NULL input to edit()", -1);
1405    return;
1406  }
1407  db = sqlite3_context_db_handle(context);
1408  zTempFile = 0;
1409  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1410  if( zTempFile==0 ){
1411    sqlite3_uint64 r = 0;
1412    sqlite3_randomness(sizeof(r), &r);
1413    zTempFile = sqlite3_mprintf("temp%llx", r);
1414    if( zTempFile==0 ){
1415      sqlite3_result_error_nomem(context);
1416      return;
1417    }
1418  }
1419  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1420  /* When writing the file to be edited, do \n to \r\n conversions on systems
1421  ** that want \r\n line endings */
1422  f = fopen(zTempFile, bBin ? "wb" : "w");
1423  if( f==0 ){
1424    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1425    goto edit_func_end;
1426  }
1427  sz = sqlite3_value_bytes(argv[0]);
1428  if( bBin ){
1429    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1430  }else{
1431    const char *z = (const char*)sqlite3_value_text(argv[0]);
1432    /* Remember whether or not the value originally contained \r\n */
1433    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1434    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1435  }
1436  fclose(f);
1437  f = 0;
1438  if( x!=sz ){
1439    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1440    goto edit_func_end;
1441  }
1442  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1443  if( zCmd==0 ){
1444    sqlite3_result_error_nomem(context);
1445    goto edit_func_end;
1446  }
1447  rc = system(zCmd);
1448  sqlite3_free(zCmd);
1449  if( rc ){
1450    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1451    goto edit_func_end;
1452  }
1453  f = fopen(zTempFile, "rb");
1454  if( f==0 ){
1455    sqlite3_result_error(context,
1456      "edit() cannot reopen temp file after edit", -1);
1457    goto edit_func_end;
1458  }
1459  fseek(f, 0, SEEK_END);
1460  sz = ftell(f);
1461  rewind(f);
1462  p = sqlite3_malloc64( sz+1 );
1463  if( p==0 ){
1464    sqlite3_result_error_nomem(context);
1465    goto edit_func_end;
1466  }
1467  x = fread(p, 1, (size_t)sz, f);
1468  fclose(f);
1469  f = 0;
1470  if( x!=sz ){
1471    sqlite3_result_error(context, "could not read back the whole file", -1);
1472    goto edit_func_end;
1473  }
1474  if( bBin ){
1475    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1476  }else{
1477    sqlite3_int64 i, j;
1478    if( hasCRNL ){
1479      /* If the original contains \r\n then do no conversions back to \n */
1480    }else{
1481      /* If the file did not originally contain \r\n then convert any new
1482      ** \r\n back into \n */
1483      for(i=j=0; i<sz; i++){
1484        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1485        p[j++] = p[i];
1486      }
1487      sz = j;
1488      p[sz] = 0;
1489    }
1490    sqlite3_result_text64(context, (const char*)p, sz,
1491                          sqlite3_free, SQLITE_UTF8);
1492  }
1493  p = 0;
1494
1495edit_func_end:
1496  if( f ) fclose(f);
1497  unlink(zTempFile);
1498  sqlite3_free(zTempFile);
1499  sqlite3_free(p);
1500}
1501#endif /* SQLITE_NOHAVE_SYSTEM */
1502
1503/*
1504** Save or restore the current output mode
1505*/
1506static void outputModePush(ShellState *p){
1507  p->modePrior = p->mode;
1508  p->priorShFlgs = p->shellFlgs;
1509  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1510  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1511}
1512static void outputModePop(ShellState *p){
1513  p->mode = p->modePrior;
1514  p->shellFlgs = p->priorShFlgs;
1515  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1516  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1517}
1518
1519/*
1520** Output the given string as a hex-encoded blob (eg. X'1234' )
1521*/
1522static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1523  int i;
1524  unsigned char *aBlob = (unsigned char*)pBlob;
1525
1526  char *zStr = sqlite3_malloc(nBlob*2 + 1);
1527  shell_check_oom(zStr);
1528
1529  for(i=0; i<nBlob; i++){
1530    static const char aHex[] = {
1531        '0', '1', '2', '3', '4', '5', '6', '7',
1532        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1533    };
1534    zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1535    zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1536  }
1537  zStr[i*2] = '\0';
1538
1539  raw_printf(out,"X'%s'", zStr);
1540  sqlite3_free(zStr);
1541}
1542
1543/*
1544** Find a string that is not found anywhere in z[].  Return a pointer
1545** to that string.
1546**
1547** Try to use zA and zB first.  If both of those are already found in z[]
1548** then make up some string and store it in the buffer zBuf.
1549*/
1550static const char *unused_string(
1551  const char *z,                    /* Result must not appear anywhere in z */
1552  const char *zA, const char *zB,   /* Try these first */
1553  char *zBuf                        /* Space to store a generated string */
1554){
1555  unsigned i = 0;
1556  if( strstr(z, zA)==0 ) return zA;
1557  if( strstr(z, zB)==0 ) return zB;
1558  do{
1559    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1560  }while( strstr(z,zBuf)!=0 );
1561  return zBuf;
1562}
1563
1564/*
1565** Output the given string as a quoted string using SQL quoting conventions.
1566**
1567** See also: output_quoted_escaped_string()
1568*/
1569static void output_quoted_string(FILE *out, const char *z){
1570  int i;
1571  char c;
1572  setBinaryMode(out, 1);
1573  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1574  if( c==0 ){
1575    utf8_printf(out,"'%s'",z);
1576  }else{
1577    raw_printf(out, "'");
1578    while( *z ){
1579      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1580      if( c=='\'' ) i++;
1581      if( i ){
1582        utf8_printf(out, "%.*s", i, z);
1583        z += i;
1584      }
1585      if( c=='\'' ){
1586        raw_printf(out, "'");
1587        continue;
1588      }
1589      if( c==0 ){
1590        break;
1591      }
1592      z++;
1593    }
1594    raw_printf(out, "'");
1595  }
1596  setTextMode(out, 1);
1597}
1598
1599/*
1600** Output the given string as a quoted string using SQL quoting conventions.
1601** Additionallly , escape the "\n" and "\r" characters so that they do not
1602** get corrupted by end-of-line translation facilities in some operating
1603** systems.
1604**
1605** This is like output_quoted_string() but with the addition of the \r\n
1606** escape mechanism.
1607*/
1608static void output_quoted_escaped_string(FILE *out, const char *z){
1609  int i;
1610  char c;
1611  setBinaryMode(out, 1);
1612  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1613  if( c==0 ){
1614    utf8_printf(out,"'%s'",z);
1615  }else{
1616    const char *zNL = 0;
1617    const char *zCR = 0;
1618    int nNL = 0;
1619    int nCR = 0;
1620    char zBuf1[20], zBuf2[20];
1621    for(i=0; z[i]; i++){
1622      if( z[i]=='\n' ) nNL++;
1623      if( z[i]=='\r' ) nCR++;
1624    }
1625    if( nNL ){
1626      raw_printf(out, "replace(");
1627      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1628    }
1629    if( nCR ){
1630      raw_printf(out, "replace(");
1631      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1632    }
1633    raw_printf(out, "'");
1634    while( *z ){
1635      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1636      if( c=='\'' ) i++;
1637      if( i ){
1638        utf8_printf(out, "%.*s", i, z);
1639        z += i;
1640      }
1641      if( c=='\'' ){
1642        raw_printf(out, "'");
1643        continue;
1644      }
1645      if( c==0 ){
1646        break;
1647      }
1648      z++;
1649      if( c=='\n' ){
1650        raw_printf(out, "%s", zNL);
1651        continue;
1652      }
1653      raw_printf(out, "%s", zCR);
1654    }
1655    raw_printf(out, "'");
1656    if( nCR ){
1657      raw_printf(out, ",'%s',char(13))", zCR);
1658    }
1659    if( nNL ){
1660      raw_printf(out, ",'%s',char(10))", zNL);
1661    }
1662  }
1663  setTextMode(out, 1);
1664}
1665
1666/*
1667** Output the given string as a quoted according to C or TCL quoting rules.
1668*/
1669static void output_c_string(FILE *out, const char *z){
1670  unsigned int c;
1671  fputc('"', out);
1672  while( (c = *(z++))!=0 ){
1673    if( c=='\\' ){
1674      fputc(c, out);
1675      fputc(c, out);
1676    }else if( c=='"' ){
1677      fputc('\\', out);
1678      fputc('"', out);
1679    }else if( c=='\t' ){
1680      fputc('\\', out);
1681      fputc('t', out);
1682    }else if( c=='\n' ){
1683      fputc('\\', out);
1684      fputc('n', out);
1685    }else if( c=='\r' ){
1686      fputc('\\', out);
1687      fputc('r', out);
1688    }else if( !isprint(c&0xff) ){
1689      raw_printf(out, "\\%03o", c&0xff);
1690    }else{
1691      fputc(c, out);
1692    }
1693  }
1694  fputc('"', out);
1695}
1696
1697/*
1698** Output the given string as a quoted according to JSON quoting rules.
1699*/
1700static void output_json_string(FILE *out, const char *z, i64 n){
1701  unsigned int c;
1702  if( n<0 ) n = strlen(z);
1703  fputc('"', out);
1704  while( n-- ){
1705    c = *(z++);
1706    if( c=='\\' || c=='"' ){
1707      fputc('\\', out);
1708      fputc(c, out);
1709    }else if( c<=0x1f ){
1710      fputc('\\', out);
1711      if( c=='\b' ){
1712        fputc('b', out);
1713      }else if( c=='\f' ){
1714        fputc('f', out);
1715      }else if( c=='\n' ){
1716        fputc('n', out);
1717      }else if( c=='\r' ){
1718        fputc('r', out);
1719      }else if( c=='\t' ){
1720        fputc('t', out);
1721      }else{
1722         raw_printf(out, "u%04x",c);
1723      }
1724    }else{
1725      fputc(c, out);
1726    }
1727  }
1728  fputc('"', out);
1729}
1730
1731/*
1732** Output the given string with characters that are special to
1733** HTML escaped.
1734*/
1735static void output_html_string(FILE *out, const char *z){
1736  int i;
1737  if( z==0 ) z = "";
1738  while( *z ){
1739    for(i=0;   z[i]
1740            && z[i]!='<'
1741            && z[i]!='&'
1742            && z[i]!='>'
1743            && z[i]!='\"'
1744            && z[i]!='\'';
1745        i++){}
1746    if( i>0 ){
1747      utf8_printf(out,"%.*s",i,z);
1748    }
1749    if( z[i]=='<' ){
1750      raw_printf(out,"&lt;");
1751    }else if( z[i]=='&' ){
1752      raw_printf(out,"&amp;");
1753    }else if( z[i]=='>' ){
1754      raw_printf(out,"&gt;");
1755    }else if( z[i]=='\"' ){
1756      raw_printf(out,"&quot;");
1757    }else if( z[i]=='\'' ){
1758      raw_printf(out,"&#39;");
1759    }else{
1760      break;
1761    }
1762    z += i + 1;
1763  }
1764}
1765
1766/*
1767** If a field contains any character identified by a 1 in the following
1768** array, then the string must be quoted for CSV.
1769*/
1770static const char needCsvQuote[] = {
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, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1774  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1775  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1776  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1777  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1778  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1779  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1780  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1781  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1782  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1783  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1784  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1785  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1786  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1787};
1788
1789/*
1790** Output a single term of CSV.  Actually, p->colSeparator is used for
1791** the separator, which may or may not be a comma.  p->nullValue is
1792** the null value.  Strings are quoted if necessary.  The separator
1793** is only issued if bSep is true.
1794*/
1795static void output_csv(ShellState *p, const char *z, int bSep){
1796  FILE *out = p->out;
1797  if( z==0 ){
1798    utf8_printf(out,"%s",p->nullValue);
1799  }else{
1800    unsigned i;
1801    for(i=0; z[i]; i++){
1802      if( needCsvQuote[((unsigned char*)z)[i]] ){
1803        i = 0;
1804        break;
1805      }
1806    }
1807    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1808      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1809      shell_check_oom(zQuoted);
1810      utf8_printf(out, "%s", zQuoted);
1811      sqlite3_free(zQuoted);
1812    }else{
1813      utf8_printf(out, "%s", z);
1814    }
1815  }
1816  if( bSep ){
1817    utf8_printf(p->out, "%s", p->colSeparator);
1818  }
1819}
1820
1821/*
1822** This routine runs when the user presses Ctrl-C
1823*/
1824static void interrupt_handler(int NotUsed){
1825  UNUSED_PARAMETER(NotUsed);
1826  seenInterrupt++;
1827  if( seenInterrupt>2 ) exit(1);
1828  if( globalDb ) sqlite3_interrupt(globalDb);
1829}
1830
1831#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1832/*
1833** This routine runs for console events (e.g. Ctrl-C) on Win32
1834*/
1835static BOOL WINAPI ConsoleCtrlHandler(
1836  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1837){
1838  if( dwCtrlType==CTRL_C_EVENT ){
1839    interrupt_handler(0);
1840    return TRUE;
1841  }
1842  return FALSE;
1843}
1844#endif
1845
1846#ifndef SQLITE_OMIT_AUTHORIZATION
1847/*
1848** This authorizer runs in safe mode.
1849*/
1850static int safeModeAuth(
1851  void *pClientData,
1852  int op,
1853  const char *zA1,
1854  const char *zA2,
1855  const char *zA3,
1856  const char *zA4
1857){
1858  ShellState *p = (ShellState*)pClientData;
1859  static const char *azProhibitedFunctions[] = {
1860    "edit",
1861    "fts3_tokenizer",
1862    "load_extension",
1863    "readfile",
1864    "writefile",
1865    "zipfile",
1866    "zipfile_cds",
1867  };
1868  UNUSED_PARAMETER(zA2);
1869  UNUSED_PARAMETER(zA3);
1870  UNUSED_PARAMETER(zA4);
1871  switch( op ){
1872    case SQLITE_ATTACH: {
1873#ifndef SQLITE_SHELL_FIDDLE
1874      /* In WASM builds the filesystem is a virtual sandbox, so
1875      ** there's no harm in using ATTACH. */
1876      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1877#endif
1878      break;
1879    }
1880    case SQLITE_FUNCTION: {
1881      int i;
1882      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1883        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1884          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1885                         azProhibitedFunctions[i]);
1886        }
1887      }
1888      break;
1889    }
1890  }
1891  return SQLITE_OK;
1892}
1893
1894/*
1895** When the ".auth ON" is set, the following authorizer callback is
1896** invoked.  It always returns SQLITE_OK.
1897*/
1898static int shellAuth(
1899  void *pClientData,
1900  int op,
1901  const char *zA1,
1902  const char *zA2,
1903  const char *zA3,
1904  const char *zA4
1905){
1906  ShellState *p = (ShellState*)pClientData;
1907  static const char *azAction[] = { 0,
1908     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1909     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1910     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1911     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1912     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1913     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1914     "PRAGMA",               "READ",                 "SELECT",
1915     "TRANSACTION",          "UPDATE",               "ATTACH",
1916     "DETACH",               "ALTER_TABLE",          "REINDEX",
1917     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1918     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1919  };
1920  int i;
1921  const char *az[4];
1922  az[0] = zA1;
1923  az[1] = zA2;
1924  az[2] = zA3;
1925  az[3] = zA4;
1926  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1927  for(i=0; i<4; i++){
1928    raw_printf(p->out, " ");
1929    if( az[i] ){
1930      output_c_string(p->out, az[i]);
1931    }else{
1932      raw_printf(p->out, "NULL");
1933    }
1934  }
1935  raw_printf(p->out, "\n");
1936  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1937  return SQLITE_OK;
1938}
1939#endif
1940
1941/*
1942** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1943**
1944** This routine converts some CREATE TABLE statements for shadow tables
1945** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1946**
1947** If the schema statement in z[] contains a start-of-comment and if
1948** sqlite3_complete() returns false, try to terminate the comment before
1949** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
1950*/
1951static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1952  char *zToFree = 0;
1953  if( z==0 ) return;
1954  if( zTail==0 ) return;
1955  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
1956    const char *zOrig = z;
1957    static const char *azTerm[] = { "", "*/", "\n" };
1958    int i;
1959    for(i=0; i<ArraySize(azTerm); i++){
1960      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
1961      if( sqlite3_complete(zNew) ){
1962        size_t n = strlen(zNew);
1963        zNew[n-1] = 0;
1964        zToFree = zNew;
1965        z = zNew;
1966        break;
1967      }
1968      sqlite3_free(zNew);
1969    }
1970  }
1971  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1972    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1973  }else{
1974    utf8_printf(out, "%s%s", z, zTail);
1975  }
1976  sqlite3_free(zToFree);
1977}
1978static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1979  char c = z[n];
1980  z[n] = 0;
1981  printSchemaLine(out, z, zTail);
1982  z[n] = c;
1983}
1984
1985/*
1986** Return true if string z[] has nothing but whitespace and comments to the
1987** end of the first line.
1988*/
1989static int wsToEol(const char *z){
1990  int i;
1991  for(i=0; z[i]; i++){
1992    if( z[i]=='\n' ) return 1;
1993    if( IsSpace(z[i]) ) continue;
1994    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1995    return 0;
1996  }
1997  return 1;
1998}
1999
2000/*
2001** Add a new entry to the EXPLAIN QUERY PLAN data
2002*/
2003static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
2004  EQPGraphRow *pNew;
2005  i64 nText;
2006  if( zText==0 ) return;
2007  nText = strlen(zText);
2008  if( p->autoEQPtest ){
2009    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
2010  }
2011  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2012  shell_check_oom(pNew);
2013  pNew->iEqpId = iEqpId;
2014  pNew->iParentId = p2;
2015  memcpy(pNew->zText, zText, nText+1);
2016  pNew->pNext = 0;
2017  if( p->sGraph.pLast ){
2018    p->sGraph.pLast->pNext = pNew;
2019  }else{
2020    p->sGraph.pRow = pNew;
2021  }
2022  p->sGraph.pLast = pNew;
2023}
2024
2025/*
2026** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2027** in p->sGraph.
2028*/
2029static void eqp_reset(ShellState *p){
2030  EQPGraphRow *pRow, *pNext;
2031  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2032    pNext = pRow->pNext;
2033    sqlite3_free(pRow);
2034  }
2035  memset(&p->sGraph, 0, sizeof(p->sGraph));
2036}
2037
2038/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2039** pOld, or return the first such line if pOld is NULL
2040*/
2041static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2042  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2043  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2044  return pRow;
2045}
2046
2047/* Render a single level of the graph that has iEqpId as its parent.  Called
2048** recursively to render sublevels.
2049*/
2050static void eqp_render_level(ShellState *p, int iEqpId){
2051  EQPGraphRow *pRow, *pNext;
2052  i64 n = strlen(p->sGraph.zPrefix);
2053  char *z;
2054  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2055    pNext = eqp_next_row(p, iEqpId, pRow);
2056    z = pRow->zText;
2057    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2058                pNext ? "|--" : "`--", z);
2059    if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2060      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
2061      eqp_render_level(p, pRow->iEqpId);
2062      p->sGraph.zPrefix[n] = 0;
2063    }
2064  }
2065}
2066
2067/*
2068** Display and reset the EXPLAIN QUERY PLAN data
2069*/
2070static void eqp_render(ShellState *p){
2071  EQPGraphRow *pRow = p->sGraph.pRow;
2072  if( pRow ){
2073    if( pRow->zText[0]=='-' ){
2074      if( pRow->pNext==0 ){
2075        eqp_reset(p);
2076        return;
2077      }
2078      utf8_printf(p->out, "%s\n", pRow->zText+3);
2079      p->sGraph.pRow = pRow->pNext;
2080      sqlite3_free(pRow);
2081    }else{
2082      utf8_printf(p->out, "QUERY PLAN\n");
2083    }
2084    p->sGraph.zPrefix[0] = 0;
2085    eqp_render_level(p, 0);
2086    eqp_reset(p);
2087  }
2088}
2089
2090#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2091/*
2092** Progress handler callback.
2093*/
2094static int progress_handler(void *pClientData) {
2095  ShellState *p = (ShellState*)pClientData;
2096  p->nProgress++;
2097  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2098    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2099    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2100    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2101    return 1;
2102  }
2103  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2104    raw_printf(p->out, "Progress %u\n", p->nProgress);
2105  }
2106  return 0;
2107}
2108#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2109
2110/*
2111** Print N dashes
2112*/
2113static void print_dashes(FILE *out, int N){
2114  const char zDash[] = "--------------------------------------------------";
2115  const int nDash = sizeof(zDash) - 1;
2116  while( N>nDash ){
2117    fputs(zDash, out);
2118    N -= nDash;
2119  }
2120  raw_printf(out, "%.*s", N, zDash);
2121}
2122
2123/*
2124** Print a markdown or table-style row separator using ascii-art
2125*/
2126static void print_row_separator(
2127  ShellState *p,
2128  int nArg,
2129  const char *zSep
2130){
2131  int i;
2132  if( nArg>0 ){
2133    fputs(zSep, p->out);
2134    print_dashes(p->out, p->actualWidth[0]+2);
2135    for(i=1; i<nArg; i++){
2136      fputs(zSep, p->out);
2137      print_dashes(p->out, p->actualWidth[i]+2);
2138    }
2139    fputs(zSep, p->out);
2140  }
2141  fputs("\n", p->out);
2142}
2143
2144/*
2145** This is the callback routine that the shell
2146** invokes for each row of a query result.
2147*/
2148static int shell_callback(
2149  void *pArg,
2150  int nArg,        /* Number of result columns */
2151  char **azArg,    /* Text of each result column */
2152  char **azCol,    /* Column names */
2153  int *aiType      /* Column types.  Might be NULL */
2154){
2155  int i;
2156  ShellState *p = (ShellState*)pArg;
2157
2158  if( azArg==0 ) return 0;
2159  switch( p->cMode ){
2160    case MODE_Count:
2161    case MODE_Off: {
2162      break;
2163    }
2164    case MODE_Line: {
2165      int w = 5;
2166      if( azArg==0 ) break;
2167      for(i=0; i<nArg; i++){
2168        int len = strlen30(azCol[i] ? azCol[i] : "");
2169        if( len>w ) w = len;
2170      }
2171      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2172      for(i=0; i<nArg; i++){
2173        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2174                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2175      }
2176      break;
2177    }
2178    case MODE_Explain: {
2179      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2180      if( nArg>ArraySize(aExplainWidth) ){
2181        nArg = ArraySize(aExplainWidth);
2182      }
2183      if( p->cnt++==0 ){
2184        for(i=0; i<nArg; i++){
2185          int w = aExplainWidth[i];
2186          utf8_width_print(p->out, w, azCol[i]);
2187          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2188        }
2189        for(i=0; i<nArg; i++){
2190          int w = aExplainWidth[i];
2191          print_dashes(p->out, w);
2192          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2193        }
2194      }
2195      if( azArg==0 ) break;
2196      for(i=0; i<nArg; i++){
2197        int w = aExplainWidth[i];
2198        if( i==nArg-1 ) w = 0;
2199        if( azArg[i] && strlenChar(azArg[i])>w ){
2200          w = strlenChar(azArg[i]);
2201        }
2202        if( i==1 && p->aiIndent && p->pStmt ){
2203          if( p->iIndent<p->nIndent ){
2204            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2205          }
2206          p->iIndent++;
2207        }
2208        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2209        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2210      }
2211      break;
2212    }
2213    case MODE_Semi: {   /* .schema and .fullschema output */
2214      printSchemaLine(p->out, azArg[0], ";\n");
2215      break;
2216    }
2217    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2218      char *z;
2219      int j;
2220      int nParen = 0;
2221      char cEnd = 0;
2222      char c;
2223      int nLine = 0;
2224      assert( nArg==1 );
2225      if( azArg[0]==0 ) break;
2226      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2227       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2228      ){
2229        utf8_printf(p->out, "%s;\n", azArg[0]);
2230        break;
2231      }
2232      z = sqlite3_mprintf("%s", azArg[0]);
2233      shell_check_oom(z);
2234      j = 0;
2235      for(i=0; IsSpace(z[i]); i++){}
2236      for(; (c = z[i])!=0; i++){
2237        if( IsSpace(c) ){
2238          if( z[j-1]=='\r' ) z[j-1] = '\n';
2239          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2240        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2241          j--;
2242        }
2243        z[j++] = c;
2244      }
2245      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2246      z[j] = 0;
2247      if( strlen30(z)>=79 ){
2248        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2249          if( c==cEnd ){
2250            cEnd = 0;
2251          }else if( c=='"' || c=='\'' || c=='`' ){
2252            cEnd = c;
2253          }else if( c=='[' ){
2254            cEnd = ']';
2255          }else if( c=='-' && z[i+1]=='-' ){
2256            cEnd = '\n';
2257          }else if( c=='(' ){
2258            nParen++;
2259          }else if( c==')' ){
2260            nParen--;
2261            if( nLine>0 && nParen==0 && j>0 ){
2262              printSchemaLineN(p->out, z, j, "\n");
2263              j = 0;
2264            }
2265          }
2266          z[j++] = c;
2267          if( nParen==1 && cEnd==0
2268           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2269          ){
2270            if( c=='\n' ) j--;
2271            printSchemaLineN(p->out, z, j, "\n  ");
2272            j = 0;
2273            nLine++;
2274            while( IsSpace(z[i+1]) ){ i++; }
2275          }
2276        }
2277        z[j] = 0;
2278      }
2279      printSchemaLine(p->out, z, ";\n");
2280      sqlite3_free(z);
2281      break;
2282    }
2283    case MODE_List: {
2284      if( p->cnt++==0 && p->showHeader ){
2285        for(i=0; i<nArg; i++){
2286          utf8_printf(p->out,"%s%s",azCol[i],
2287                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2288        }
2289      }
2290      if( azArg==0 ) break;
2291      for(i=0; i<nArg; i++){
2292        char *z = azArg[i];
2293        if( z==0 ) z = p->nullValue;
2294        utf8_printf(p->out, "%s", z);
2295        if( i<nArg-1 ){
2296          utf8_printf(p->out, "%s", p->colSeparator);
2297        }else{
2298          utf8_printf(p->out, "%s", p->rowSeparator);
2299        }
2300      }
2301      break;
2302    }
2303    case MODE_Html: {
2304      if( p->cnt++==0 && p->showHeader ){
2305        raw_printf(p->out,"<TR>");
2306        for(i=0; i<nArg; i++){
2307          raw_printf(p->out,"<TH>");
2308          output_html_string(p->out, azCol[i]);
2309          raw_printf(p->out,"</TH>\n");
2310        }
2311        raw_printf(p->out,"</TR>\n");
2312      }
2313      if( azArg==0 ) break;
2314      raw_printf(p->out,"<TR>");
2315      for(i=0; i<nArg; i++){
2316        raw_printf(p->out,"<TD>");
2317        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2318        raw_printf(p->out,"</TD>\n");
2319      }
2320      raw_printf(p->out,"</TR>\n");
2321      break;
2322    }
2323    case MODE_Tcl: {
2324      if( p->cnt++==0 && p->showHeader ){
2325        for(i=0; i<nArg; i++){
2326          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2327          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2328        }
2329        utf8_printf(p->out, "%s", p->rowSeparator);
2330      }
2331      if( azArg==0 ) break;
2332      for(i=0; i<nArg; i++){
2333        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2334        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2335      }
2336      utf8_printf(p->out, "%s", p->rowSeparator);
2337      break;
2338    }
2339    case MODE_Csv: {
2340      setBinaryMode(p->out, 1);
2341      if( p->cnt++==0 && p->showHeader ){
2342        for(i=0; i<nArg; i++){
2343          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2344        }
2345        utf8_printf(p->out, "%s", p->rowSeparator);
2346      }
2347      if( nArg>0 ){
2348        for(i=0; i<nArg; i++){
2349          output_csv(p, azArg[i], i<nArg-1);
2350        }
2351        utf8_printf(p->out, "%s", p->rowSeparator);
2352      }
2353      setTextMode(p->out, 1);
2354      break;
2355    }
2356    case MODE_Insert: {
2357      if( azArg==0 ) break;
2358      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2359      if( p->showHeader ){
2360        raw_printf(p->out,"(");
2361        for(i=0; i<nArg; i++){
2362          if( i>0 ) raw_printf(p->out, ",");
2363          if( quoteChar(azCol[i]) ){
2364            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2365            shell_check_oom(z);
2366            utf8_printf(p->out, "%s", z);
2367            sqlite3_free(z);
2368          }else{
2369            raw_printf(p->out, "%s", azCol[i]);
2370          }
2371        }
2372        raw_printf(p->out,")");
2373      }
2374      p->cnt++;
2375      for(i=0; i<nArg; i++){
2376        raw_printf(p->out, i>0 ? "," : " VALUES(");
2377        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2378          utf8_printf(p->out,"NULL");
2379        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2380          if( ShellHasFlag(p, SHFLG_Newlines) ){
2381            output_quoted_string(p->out, azArg[i]);
2382          }else{
2383            output_quoted_escaped_string(p->out, azArg[i]);
2384          }
2385        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2386          utf8_printf(p->out,"%s", azArg[i]);
2387        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2388          char z[50];
2389          double r = sqlite3_column_double(p->pStmt, i);
2390          sqlite3_uint64 ur;
2391          memcpy(&ur,&r,sizeof(r));
2392          if( ur==0x7ff0000000000000LL ){
2393            raw_printf(p->out, "1e999");
2394          }else if( ur==0xfff0000000000000LL ){
2395            raw_printf(p->out, "-1e999");
2396          }else{
2397            sqlite3_int64 ir = (sqlite3_int64)r;
2398            if( r==(double)ir ){
2399              sqlite3_snprintf(50,z,"%lld.0", ir);
2400            }else{
2401              sqlite3_snprintf(50,z,"%!.20g", r);
2402            }
2403            raw_printf(p->out, "%s", z);
2404          }
2405        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2406          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2407          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2408          output_hex_blob(p->out, pBlob, nBlob);
2409        }else if( isNumber(azArg[i], 0) ){
2410          utf8_printf(p->out,"%s", azArg[i]);
2411        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2412          output_quoted_string(p->out, azArg[i]);
2413        }else{
2414          output_quoted_escaped_string(p->out, azArg[i]);
2415        }
2416      }
2417      raw_printf(p->out,");\n");
2418      break;
2419    }
2420    case MODE_Json: {
2421      if( azArg==0 ) break;
2422      if( p->cnt==0 ){
2423        fputs("[{", p->out);
2424      }else{
2425        fputs(",\n{", p->out);
2426      }
2427      p->cnt++;
2428      for(i=0; i<nArg; i++){
2429        output_json_string(p->out, azCol[i], -1);
2430        putc(':', p->out);
2431        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2432          fputs("null",p->out);
2433        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2434          char z[50];
2435          double r = sqlite3_column_double(p->pStmt, i);
2436          sqlite3_uint64 ur;
2437          memcpy(&ur,&r,sizeof(r));
2438          if( ur==0x7ff0000000000000LL ){
2439            raw_printf(p->out, "1e999");
2440          }else if( ur==0xfff0000000000000LL ){
2441            raw_printf(p->out, "-1e999");
2442          }else{
2443            sqlite3_snprintf(50,z,"%!.20g", r);
2444            raw_printf(p->out, "%s", z);
2445          }
2446        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2447          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2448          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2449          output_json_string(p->out, pBlob, nBlob);
2450        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2451          output_json_string(p->out, azArg[i], -1);
2452        }else{
2453          utf8_printf(p->out,"%s", azArg[i]);
2454        }
2455        if( i<nArg-1 ){
2456          putc(',', p->out);
2457        }
2458      }
2459      putc('}', p->out);
2460      break;
2461    }
2462    case MODE_Quote: {
2463      if( azArg==0 ) break;
2464      if( p->cnt==0 && p->showHeader ){
2465        for(i=0; i<nArg; i++){
2466          if( i>0 ) fputs(p->colSeparator, p->out);
2467          output_quoted_string(p->out, azCol[i]);
2468        }
2469        fputs(p->rowSeparator, p->out);
2470      }
2471      p->cnt++;
2472      for(i=0; i<nArg; i++){
2473        if( i>0 ) fputs(p->colSeparator, p->out);
2474        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2475          utf8_printf(p->out,"NULL");
2476        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2477          output_quoted_string(p->out, azArg[i]);
2478        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2479          utf8_printf(p->out,"%s", azArg[i]);
2480        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2481          char z[50];
2482          double r = sqlite3_column_double(p->pStmt, i);
2483          sqlite3_snprintf(50,z,"%!.20g", r);
2484          raw_printf(p->out, "%s", z);
2485        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2486          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2487          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2488          output_hex_blob(p->out, pBlob, nBlob);
2489        }else if( isNumber(azArg[i], 0) ){
2490          utf8_printf(p->out,"%s", azArg[i]);
2491        }else{
2492          output_quoted_string(p->out, azArg[i]);
2493        }
2494      }
2495      fputs(p->rowSeparator, p->out);
2496      break;
2497    }
2498    case MODE_Ascii: {
2499      if( p->cnt++==0 && p->showHeader ){
2500        for(i=0; i<nArg; i++){
2501          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2502          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2503        }
2504        utf8_printf(p->out, "%s", p->rowSeparator);
2505      }
2506      if( azArg==0 ) break;
2507      for(i=0; i<nArg; i++){
2508        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2509        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2510      }
2511      utf8_printf(p->out, "%s", p->rowSeparator);
2512      break;
2513    }
2514    case MODE_EQP: {
2515      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2516      break;
2517    }
2518  }
2519  return 0;
2520}
2521
2522/*
2523** This is the callback routine that the SQLite library
2524** invokes for each row of a query result.
2525*/
2526static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2527  /* since we don't have type info, call the shell_callback with a NULL value */
2528  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2529}
2530
2531/*
2532** This is the callback routine from sqlite3_exec() that appends all
2533** output onto the end of a ShellText object.
2534*/
2535static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2536  ShellText *p = (ShellText*)pArg;
2537  int i;
2538  UNUSED_PARAMETER(az);
2539  if( azArg==0 ) return 0;
2540  if( p->n ) appendText(p, "|", 0);
2541  for(i=0; i<nArg; i++){
2542    if( i ) appendText(p, ",", 0);
2543    if( azArg[i] ) appendText(p, azArg[i], 0);
2544  }
2545  return 0;
2546}
2547
2548/*
2549** Generate an appropriate SELFTEST table in the main database.
2550*/
2551static void createSelftestTable(ShellState *p){
2552  char *zErrMsg = 0;
2553  sqlite3_exec(p->db,
2554    "SAVEPOINT selftest_init;\n"
2555    "CREATE TABLE IF NOT EXISTS selftest(\n"
2556    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2557    "  op TEXT,\n"                   /* Operator:  memo run */
2558    "  cmd TEXT,\n"                  /* Command text */
2559    "  ans TEXT\n"                   /* Desired answer */
2560    ");"
2561    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2562    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2563    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2564    "         'memo','Tests generated by --init');\n"
2565    "INSERT INTO [_shell$self]\n"
2566    "  SELECT 'run',\n"
2567    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2568                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2569    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2570                          "FROM sqlite_schema ORDER BY 2',224));\n"
2571    "INSERT INTO [_shell$self]\n"
2572    "  SELECT 'run',"
2573    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2574    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2575    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2576    "  FROM (\n"
2577    "    SELECT name FROM sqlite_schema\n"
2578    "     WHERE type='table'\n"
2579    "       AND name<>'selftest'\n"
2580    "       AND coalesce(rootpage,0)>0\n"
2581    "  )\n"
2582    " ORDER BY name;\n"
2583    "INSERT INTO [_shell$self]\n"
2584    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2585    "INSERT INTO selftest(tno,op,cmd,ans)"
2586    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2587    "DROP TABLE [_shell$self];"
2588    ,0,0,&zErrMsg);
2589  if( zErrMsg ){
2590    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2591    sqlite3_free(zErrMsg);
2592  }
2593  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2594}
2595
2596
2597/*
2598** Set the destination table field of the ShellState structure to
2599** the name of the table given.  Escape any quote characters in the
2600** table name.
2601*/
2602static void set_table_name(ShellState *p, const char *zName){
2603  int i, n;
2604  char cQuote;
2605  char *z;
2606
2607  if( p->zDestTable ){
2608    free(p->zDestTable);
2609    p->zDestTable = 0;
2610  }
2611  if( zName==0 ) return;
2612  cQuote = quoteChar(zName);
2613  n = strlen30(zName);
2614  if( cQuote ) n += n+2;
2615  z = p->zDestTable = malloc( n+1 );
2616  shell_check_oom(z);
2617  n = 0;
2618  if( cQuote ) z[n++] = cQuote;
2619  for(i=0; zName[i]; i++){
2620    z[n++] = zName[i];
2621    if( zName[i]==cQuote ) z[n++] = cQuote;
2622  }
2623  if( cQuote ) z[n++] = cQuote;
2624  z[n] = 0;
2625}
2626
2627/*
2628** Maybe construct two lines of text that point out the position of a
2629** syntax error.  Return a pointer to the text, in memory obtained from
2630** sqlite3_malloc().  Or, if the most recent error does not involve a
2631** specific token that we can point to, return an empty string.
2632**
2633** In all cases, the memory returned is obtained from sqlite3_malloc64()
2634** and should be released by the caller invoking sqlite3_free().
2635*/
2636static char *shell_error_context(const char *zSql, sqlite3 *db){
2637  int iOffset;
2638  size_t len;
2639  char *zCode;
2640  char *zMsg;
2641  int i;
2642  if( db==0
2643   || zSql==0
2644   || (iOffset = sqlite3_error_offset(db))<0
2645  ){
2646    return sqlite3_mprintf("");
2647  }
2648  while( iOffset>50 ){
2649    iOffset--;
2650    zSql++;
2651    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
2652  }
2653  len = strlen(zSql);
2654  if( len>78 ){
2655    len = 78;
2656    while( (zSql[len]&0xc0)==0x80 ) len--;
2657  }
2658  zCode = sqlite3_mprintf("%.*s", len, zSql);
2659  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
2660  if( iOffset<25 ){
2661    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");
2662  }else{
2663    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");
2664  }
2665  return zMsg;
2666}
2667
2668
2669/*
2670** Execute a query statement that will generate SQL output.  Print
2671** the result columns, comma-separated, on a line and then add a
2672** semicolon terminator to the end of that line.
2673**
2674** If the number of columns is 1 and that column contains text "--"
2675** then write the semicolon on a separate line.  That way, if a
2676** "--" comment occurs at the end of the statement, the comment
2677** won't consume the semicolon terminator.
2678*/
2679static int run_table_dump_query(
2680  ShellState *p,           /* Query context */
2681  const char *zSelect      /* SELECT statement to extract content */
2682){
2683  sqlite3_stmt *pSelect;
2684  int rc;
2685  int nResult;
2686  int i;
2687  const char *z;
2688  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2689  if( rc!=SQLITE_OK || !pSelect ){
2690    char *zContext = shell_error_context(zSelect, p->db);
2691    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
2692                sqlite3_errmsg(p->db), zContext);
2693    sqlite3_free(zContext);
2694    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2695    return rc;
2696  }
2697  rc = sqlite3_step(pSelect);
2698  nResult = sqlite3_column_count(pSelect);
2699  while( rc==SQLITE_ROW ){
2700    z = (const char*)sqlite3_column_text(pSelect, 0);
2701    utf8_printf(p->out, "%s", z);
2702    for(i=1; i<nResult; i++){
2703      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2704    }
2705    if( z==0 ) z = "";
2706    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2707    if( z[0] ){
2708      raw_printf(p->out, "\n;\n");
2709    }else{
2710      raw_printf(p->out, ";\n");
2711    }
2712    rc = sqlite3_step(pSelect);
2713  }
2714  rc = sqlite3_finalize(pSelect);
2715  if( rc!=SQLITE_OK ){
2716    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2717                sqlite3_errmsg(p->db));
2718    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2719  }
2720  return rc;
2721}
2722
2723/*
2724** Allocate space and save off string indicating current error.
2725*/
2726static char *save_err_msg(
2727  sqlite3 *db,           /* Database to query */
2728  const char *zPhase,    /* When the error occcurs */
2729  int rc,                /* Error code returned from API */
2730  const char *zSql       /* SQL string, or NULL */
2731){
2732  char *zErr;
2733  char *zContext;
2734  sqlite3_str *pStr = sqlite3_str_new(0);
2735  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
2736  if( rc>1 ){
2737    sqlite3_str_appendf(pStr, " (%d)", rc);
2738  }
2739  zContext = shell_error_context(zSql, db);
2740  if( zContext ){
2741    sqlite3_str_appendall(pStr, zContext);
2742    sqlite3_free(zContext);
2743  }
2744  zErr = sqlite3_str_finish(pStr);
2745  shell_check_oom(zErr);
2746  return zErr;
2747}
2748
2749#ifdef __linux__
2750/*
2751** Attempt to display I/O stats on Linux using /proc/PID/io
2752*/
2753static void displayLinuxIoStats(FILE *out){
2754  FILE *in;
2755  char z[200];
2756  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2757  in = fopen(z, "rb");
2758  if( in==0 ) return;
2759  while( fgets(z, sizeof(z), in)!=0 ){
2760    static const struct {
2761      const char *zPattern;
2762      const char *zDesc;
2763    } aTrans[] = {
2764      { "rchar: ",                  "Bytes received by read():" },
2765      { "wchar: ",                  "Bytes sent to write():"    },
2766      { "syscr: ",                  "Read() system calls:"      },
2767      { "syscw: ",                  "Write() system calls:"     },
2768      { "read_bytes: ",             "Bytes read from storage:"  },
2769      { "write_bytes: ",            "Bytes written to storage:" },
2770      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2771    };
2772    int i;
2773    for(i=0; i<ArraySize(aTrans); i++){
2774      int n = strlen30(aTrans[i].zPattern);
2775      if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
2776        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2777        break;
2778      }
2779    }
2780  }
2781  fclose(in);
2782}
2783#endif
2784
2785/*
2786** Display a single line of status using 64-bit values.
2787*/
2788static void displayStatLine(
2789  ShellState *p,            /* The shell context */
2790  char *zLabel,             /* Label for this one line */
2791  char *zFormat,            /* Format for the result */
2792  int iStatusCtrl,          /* Which status to display */
2793  int bReset                /* True to reset the stats */
2794){
2795  sqlite3_int64 iCur = -1;
2796  sqlite3_int64 iHiwtr = -1;
2797  int i, nPercent;
2798  char zLine[200];
2799  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2800  for(i=0, nPercent=0; zFormat[i]; i++){
2801    if( zFormat[i]=='%' ) nPercent++;
2802  }
2803  if( nPercent>1 ){
2804    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2805  }else{
2806    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2807  }
2808  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2809}
2810
2811/*
2812** Display memory stats.
2813*/
2814static int display_stats(
2815  sqlite3 *db,                /* Database to query */
2816  ShellState *pArg,           /* Pointer to ShellState */
2817  int bReset                  /* True to reset the stats */
2818){
2819  int iCur;
2820  int iHiwtr;
2821  FILE *out;
2822  if( pArg==0 || pArg->out==0 ) return 0;
2823  out = pArg->out;
2824
2825  if( pArg->pStmt && pArg->statsOn==2 ){
2826    int nCol, i, x;
2827    sqlite3_stmt *pStmt = pArg->pStmt;
2828    char z[100];
2829    nCol = sqlite3_column_count(pStmt);
2830    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2831    for(i=0; i<nCol; i++){
2832      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2833      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2834#ifndef SQLITE_OMIT_DECLTYPE
2835      sqlite3_snprintf(30, z+x, "declared type:");
2836      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2837#endif
2838#ifdef SQLITE_ENABLE_COLUMN_METADATA
2839      sqlite3_snprintf(30, z+x, "database name:");
2840      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2841      sqlite3_snprintf(30, z+x, "table name:");
2842      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2843      sqlite3_snprintf(30, z+x, "origin name:");
2844      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2845#endif
2846    }
2847  }
2848
2849  if( pArg->statsOn==3 ){
2850    if( pArg->pStmt ){
2851      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2852      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2853    }
2854    return 0;
2855  }
2856
2857  displayStatLine(pArg, "Memory Used:",
2858     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2859  displayStatLine(pArg, "Number of Outstanding Allocations:",
2860     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2861  if( pArg->shellFlgs & SHFLG_Pagecache ){
2862    displayStatLine(pArg, "Number of Pcache Pages Used:",
2863       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2864  }
2865  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2866     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2867  displayStatLine(pArg, "Largest Allocation:",
2868     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2869  displayStatLine(pArg, "Largest Pcache Allocation:",
2870     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2871#ifdef YYTRACKMAXSTACKDEPTH
2872  displayStatLine(pArg, "Deepest Parser Stack:",
2873     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2874#endif
2875
2876  if( db ){
2877    if( pArg->shellFlgs & SHFLG_Lookaside ){
2878      iHiwtr = iCur = -1;
2879      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2880                        &iCur, &iHiwtr, bReset);
2881      raw_printf(pArg->out,
2882              "Lookaside Slots Used:                %d (max %d)\n",
2883              iCur, iHiwtr);
2884      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2885                        &iCur, &iHiwtr, bReset);
2886      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2887              iHiwtr);
2888      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2889                        &iCur, &iHiwtr, bReset);
2890      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2891              iHiwtr);
2892      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2893                        &iCur, &iHiwtr, bReset);
2894      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2895              iHiwtr);
2896    }
2897    iHiwtr = iCur = -1;
2898    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2899    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2900            iCur);
2901    iHiwtr = iCur = -1;
2902    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2903    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2904    iHiwtr = iCur = -1;
2905    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2906    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2907    iHiwtr = iCur = -1;
2908    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2909    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2910    iHiwtr = iCur = -1;
2911    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2912    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2913    iHiwtr = iCur = -1;
2914    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2915    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2916            iCur);
2917    iHiwtr = iCur = -1;
2918    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2919    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2920            iCur);
2921  }
2922
2923  if( pArg->pStmt ){
2924    int iHit, iMiss;
2925    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2926                               bReset);
2927    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2928    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2929    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2930    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2931    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2932    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);
2933    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);
2934    if( iHit || iMiss ){
2935      raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",
2936            iHit, iHit+iMiss);
2937    }
2938    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2939    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2940    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2941    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2942    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2943    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2944    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2945    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2946  }
2947
2948#ifdef __linux__
2949  displayLinuxIoStats(pArg->out);
2950#endif
2951
2952  /* Do not remove this machine readable comment: extra-stats-output-here */
2953
2954  return 0;
2955}
2956
2957/*
2958** Display scan stats.
2959*/
2960static void display_scanstats(
2961  sqlite3 *db,                    /* Database to query */
2962  ShellState *pArg                /* Pointer to ShellState */
2963){
2964#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2965  UNUSED_PARAMETER(db);
2966  UNUSED_PARAMETER(pArg);
2967#else
2968  int i, k, n, mx;
2969  raw_printf(pArg->out, "-------- scanstats --------\n");
2970  mx = 0;
2971  for(k=0; k<=mx; k++){
2972    double rEstLoop = 1.0;
2973    for(i=n=0; 1; i++){
2974      sqlite3_stmt *p = pArg->pStmt;
2975      sqlite3_int64 nLoop, nVisit;
2976      double rEst;
2977      int iSid;
2978      const char *zExplain;
2979      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2980        break;
2981      }
2982      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2983      if( iSid>mx ) mx = iSid;
2984      if( iSid!=k ) continue;
2985      if( n==0 ){
2986        rEstLoop = (double)nLoop;
2987        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2988      }
2989      n++;
2990      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2991      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2992      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2993      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2994      rEstLoop *= rEst;
2995      raw_printf(pArg->out,
2996          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2997          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2998      );
2999    }
3000  }
3001  raw_printf(pArg->out, "---------------------------\n");
3002#endif
3003}
3004
3005/*
3006** Parameter azArray points to a zero-terminated array of strings. zStr
3007** points to a single nul-terminated string. Return non-zero if zStr
3008** is equal, according to strcmp(), to any of the strings in the array.
3009** Otherwise, return zero.
3010*/
3011static int str_in_array(const char *zStr, const char **azArray){
3012  int i;
3013  for(i=0; azArray[i]; i++){
3014    if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
3015  }
3016  return 0;
3017}
3018
3019/*
3020** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3021** and populate the ShellState.aiIndent[] array with the number of
3022** spaces each opcode should be indented before it is output.
3023**
3024** The indenting rules are:
3025**
3026**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3027**       all opcodes that occur between the p2 jump destination and the opcode
3028**       itself by 2 spaces.
3029**
3030**     * Do the previous for "Return" instructions for when P2 is positive.
3031**       See tag-20220407a in wherecode.c and vdbe.c.
3032**
3033**     * For each "Goto", if the jump destination is earlier in the program
3034**       and ends on one of:
3035**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
3036**       or if the P1 parameter is one instead of zero,
3037**       then indent all opcodes between the earlier instruction
3038**       and "Goto" by 2 spaces.
3039*/
3040static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3041  const char *zSql;               /* The text of the SQL statement */
3042  const char *z;                  /* Used to check if this is an EXPLAIN */
3043  int *abYield = 0;               /* True if op is an OP_Yield */
3044  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
3045  int iOp;                        /* Index of operation in p->aiIndent[] */
3046
3047  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3048                           "Return", 0 };
3049  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3050                            "Rewind", 0 };
3051  const char *azGoto[] = { "Goto", 0 };
3052
3053  /* Try to figure out if this is really an EXPLAIN statement. If this
3054  ** cannot be verified, return early.  */
3055  if( sqlite3_column_count(pSql)!=8 ){
3056    p->cMode = p->mode;
3057    return;
3058  }
3059  zSql = sqlite3_sql(pSql);
3060  if( zSql==0 ) return;
3061  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3062  if( sqlite3_strnicmp(z, "explain", 7) ){
3063    p->cMode = p->mode;
3064    return;
3065  }
3066
3067  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3068    int i;
3069    int iAddr = sqlite3_column_int(pSql, 0);
3070    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3071
3072    /* Set p2 to the P2 field of the current opcode. Then, assuming that
3073    ** p2 is an instruction address, set variable p2op to the index of that
3074    ** instruction in the aiIndent[] array. p2 and p2op may be different if
3075    ** the current instruction is part of a sub-program generated by an
3076    ** SQL trigger or foreign key.  */
3077    int p2 = sqlite3_column_int(pSql, 3);
3078    int p2op = (p2 + (iOp-iAddr));
3079
3080    /* Grow the p->aiIndent array as required */
3081    if( iOp>=nAlloc ){
3082      if( iOp==0 ){
3083        /* Do further verfication that this is explain output.  Abort if
3084        ** it is not */
3085        static const char *explainCols[] = {
3086           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3087        int jj;
3088        for(jj=0; jj<ArraySize(explainCols); jj++){
3089          if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3090            p->cMode = p->mode;
3091            sqlite3_reset(pSql);
3092            return;
3093          }
3094        }
3095      }
3096      nAlloc += 100;
3097      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3098      shell_check_oom(p->aiIndent);
3099      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3100      shell_check_oom(abYield);
3101    }
3102    abYield[iOp] = str_in_array(zOp, azYield);
3103    p->aiIndent[iOp] = 0;
3104    p->nIndent = iOp+1;
3105
3106    if( str_in_array(zOp, azNext) && p2op>0 ){
3107      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3108    }
3109    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3110     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3111    ){
3112      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3113    }
3114  }
3115
3116  p->iIndent = 0;
3117  sqlite3_free(abYield);
3118  sqlite3_reset(pSql);
3119}
3120
3121/*
3122** Free the array allocated by explain_data_prepare().
3123*/
3124static void explain_data_delete(ShellState *p){
3125  sqlite3_free(p->aiIndent);
3126  p->aiIndent = 0;
3127  p->nIndent = 0;
3128  p->iIndent = 0;
3129}
3130
3131/*
3132** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3133*/
3134static unsigned int savedSelectTrace;
3135static unsigned int savedWhereTrace;
3136static void disable_debug_trace_modes(void){
3137  unsigned int zero = 0;
3138  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3139  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3140  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3141  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3142}
3143static void restore_debug_trace_modes(void){
3144  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3145  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3146}
3147
3148/* Create the TEMP table used to store parameter bindings */
3149static void bind_table_init(ShellState *p){
3150  int wrSchema = 0;
3151  int defensiveMode = 0;
3152  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3153  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3154  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3155  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3156  sqlite3_exec(p->db,
3157    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3158    "  key TEXT PRIMARY KEY,\n"
3159    "  value\n"
3160    ") WITHOUT ROWID;",
3161    0, 0, 0);
3162  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3163  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3164}
3165
3166/*
3167** Bind parameters on a prepared statement.
3168**
3169** Parameter bindings are taken from a TEMP table of the form:
3170**
3171**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3172**    WITHOUT ROWID;
3173**
3174** No bindings occur if this table does not exist.  The name of the table
3175** begins with "sqlite_" so that it will not collide with ordinary application
3176** tables.  The table must be in the TEMP schema.
3177*/
3178static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3179  int nVar;
3180  int i;
3181  int rc;
3182  sqlite3_stmt *pQ = 0;
3183
3184  nVar = sqlite3_bind_parameter_count(pStmt);
3185  if( nVar==0 ) return;  /* Nothing to do */
3186  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3187                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3188    return; /* Parameter table does not exist */
3189  }
3190  rc = sqlite3_prepare_v2(pArg->db,
3191          "SELECT value FROM temp.sqlite_parameters"
3192          " WHERE key=?1", -1, &pQ, 0);
3193  if( rc || pQ==0 ) return;
3194  for(i=1; i<=nVar; i++){
3195    char zNum[30];
3196    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3197    if( zVar==0 ){
3198      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3199      zVar = zNum;
3200    }
3201    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3202    if( sqlite3_step(pQ)==SQLITE_ROW ){
3203      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3204    }else{
3205      sqlite3_bind_null(pStmt, i);
3206    }
3207    sqlite3_reset(pQ);
3208  }
3209  sqlite3_finalize(pQ);
3210}
3211
3212/*
3213** UTF8 box-drawing characters.  Imagine box lines like this:
3214**
3215**           1
3216**           |
3217**       4 --+-- 2
3218**           |
3219**           3
3220**
3221** Each box characters has between 2 and 4 of the lines leading from
3222** the center.  The characters are here identified by the numbers of
3223** their corresponding lines.
3224*/
3225#define BOX_24   "\342\224\200"  /* U+2500 --- */
3226#define BOX_13   "\342\224\202"  /* U+2502  |  */
3227#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3228#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3229#define BOX_12   "\342\224\224"  /* U+2514  '- */
3230#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3231#define BOX_123  "\342\224\234"  /* U+251c  |- */
3232#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3233#define BOX_234  "\342\224\254"  /* U+252c -,- */
3234#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3235#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3236
3237/* Draw horizontal line N characters long using unicode box
3238** characters
3239*/
3240static void print_box_line(FILE *out, int N){
3241  const char zDash[] =
3242      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3243      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3244  const int nDash = sizeof(zDash) - 1;
3245  N *= 3;
3246  while( N>nDash ){
3247    utf8_printf(out, zDash);
3248    N -= nDash;
3249  }
3250  utf8_printf(out, "%.*s", N, zDash);
3251}
3252
3253/*
3254** Draw a horizontal separator for a MODE_Box table.
3255*/
3256static void print_box_row_separator(
3257  ShellState *p,
3258  int nArg,
3259  const char *zSep1,
3260  const char *zSep2,
3261  const char *zSep3
3262){
3263  int i;
3264  if( nArg>0 ){
3265    utf8_printf(p->out, "%s", zSep1);
3266    print_box_line(p->out, p->actualWidth[0]+2);
3267    for(i=1; i<nArg; i++){
3268      utf8_printf(p->out, "%s", zSep2);
3269      print_box_line(p->out, p->actualWidth[i]+2);
3270    }
3271    utf8_printf(p->out, "%s", zSep3);
3272  }
3273  fputs("\n", p->out);
3274}
3275
3276/*
3277** z[] is a line of text that is to be displayed the .mode box or table or
3278** similar tabular formats.  z[] might contain control characters such
3279** as \n, \t, \f, or \r.
3280**
3281** Compute characters to display on the first line of z[].  Stop at the
3282** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
3283** from malloc()) of that first line, which caller should free sometime.
3284** Write anything to display on the next line into *pzTail.  If this is
3285** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3286*/
3287static char *translateForDisplayAndDup(
3288  const unsigned char *z,            /* Input text to be transformed */
3289  const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
3290  int mxWidth,                       /* Max width.  0 means no limit */
3291  u8 bWordWrap                       /* If true, avoid breaking mid-word */
3292){
3293  int i;                 /* Input bytes consumed */
3294  int j;                 /* Output bytes generated */
3295  int k;                 /* Input bytes to be displayed */
3296  int n;                 /* Output column number */
3297  unsigned char *zOut;   /* Output text */
3298
3299  if( z==0 ){
3300    *pzTail = 0;
3301    return 0;
3302  }
3303  if( mxWidth<0 ) mxWidth = -mxWidth;
3304  if( mxWidth==0 ) mxWidth = 1000000;
3305  i = j = n = 0;
3306  while( n<mxWidth ){
3307    if( z[i]>=' ' ){
3308      n++;
3309      do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
3310      continue;
3311    }
3312    if( z[i]=='\t' ){
3313      do{
3314        n++;
3315        j++;
3316      }while( (n&7)!=0 && n<mxWidth );
3317      i++;
3318      continue;
3319    }
3320    break;
3321  }
3322  if( n>=mxWidth && bWordWrap  ){
3323    /* Perhaps try to back up to a better place to break the line */
3324    for(k=i; k>i/2; k--){
3325      if( isspace(z[k-1]) ) break;
3326    }
3327    if( k<=i/2 ){
3328      for(k=i; k>i/2; k--){
3329        if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3330      }
3331    }
3332    if( k<=i/2 ){
3333      k = i;
3334    }else{
3335      i = k;
3336      while( z[i]==' ' ) i++;
3337    }
3338  }else{
3339    k = i;
3340  }
3341  if( n>=mxWidth && z[i]>=' ' ){
3342   *pzTail = &z[i];
3343  }else if( z[i]=='\r' && z[i+1]=='\n' ){
3344    *pzTail = z[i+2] ? &z[i+2] : 0;
3345  }else if( z[i]==0 || z[i+1]==0 ){
3346    *pzTail = 0;
3347  }else{
3348    *pzTail = &z[i+1];
3349  }
3350  zOut = malloc( j+1 );
3351  shell_check_oom(zOut);
3352  i = j = n = 0;
3353  while( i<k ){
3354    if( z[i]>=' ' ){
3355      n++;
3356      do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
3357      continue;
3358    }
3359    if( z[i]=='\t' ){
3360      do{
3361        n++;
3362        zOut[j++] = ' ';
3363      }while( (n&7)!=0 && n<mxWidth );
3364      i++;
3365      continue;
3366    }
3367    break;
3368  }
3369  zOut[j] = 0;
3370  return (char*)zOut;
3371}
3372
3373/* Extract the value of the i-th current column for pStmt as an SQL literal
3374** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
3375** the caller.
3376*/
3377static char *quoted_column(sqlite3_stmt *pStmt, int i){
3378  switch( sqlite3_column_type(pStmt, i) ){
3379    case SQLITE_NULL: {
3380      return sqlite3_mprintf("NULL");
3381    }
3382    case SQLITE_INTEGER:
3383    case SQLITE_FLOAT: {
3384      return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3385    }
3386    case SQLITE_TEXT: {
3387      return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3388    }
3389    case SQLITE_BLOB: {
3390      int j;
3391      sqlite3_str *pStr = sqlite3_str_new(0);
3392      const unsigned char *a = sqlite3_column_blob(pStmt,i);
3393      int n = sqlite3_column_bytes(pStmt,i);
3394      sqlite3_str_append(pStr, "x'", 2);
3395      for(j=0; j<n; j++){
3396        sqlite3_str_appendf(pStr, "%02x", a[j]);
3397      }
3398      sqlite3_str_append(pStr, "'", 1);
3399      return sqlite3_str_finish(pStr);
3400    }
3401  }
3402  return 0; /* Not reached */
3403}
3404
3405/*
3406** Run a prepared statement and output the result in one of the
3407** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3408** or MODE_Box.
3409**
3410** This is different from ordinary exec_prepared_stmt() in that
3411** it has to run the entire query and gather the results into memory
3412** first, in order to determine column widths, before providing
3413** any output.
3414*/
3415static void exec_prepared_stmt_columnar(
3416  ShellState *p,                        /* Pointer to ShellState */
3417  sqlite3_stmt *pStmt                   /* Statment to run */
3418){
3419  sqlite3_int64 nRow = 0;
3420  int nColumn = 0;
3421  char **azData = 0;
3422  sqlite3_int64 nAlloc = 0;
3423  char *abRowDiv = 0;
3424  const unsigned char *uz;
3425  const char *z;
3426  char **azQuoted = 0;
3427  int rc;
3428  sqlite3_int64 i, nData;
3429  int j, nTotal, w, n;
3430  const char *colSep = 0;
3431  const char *rowSep = 0;
3432  const unsigned char **azNextLine = 0;
3433  int bNextLine = 0;
3434  int bMultiLineRowExists = 0;
3435  int bw = p->cmOpts.bWordWrap;
3436  const char *zEmpty = "";
3437  const char *zShowNull = p->nullValue;
3438
3439  rc = sqlite3_step(pStmt);
3440  if( rc!=SQLITE_ROW ) return;
3441  nColumn = sqlite3_column_count(pStmt);
3442  nAlloc = nColumn*4;
3443  if( nAlloc<=0 ) nAlloc = 1;
3444  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3445  shell_check_oom(azData);
3446  azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
3447  shell_check_oom((void*)azNextLine);
3448  memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
3449  if( p->cmOpts.bQuote ){
3450    azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
3451    shell_check_oom(azQuoted);
3452    memset(azQuoted, 0, nColumn*sizeof(char*) );
3453  }
3454  abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
3455  shell_check_oom(abRowDiv);
3456  if( nColumn>p->nWidth ){
3457    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3458    shell_check_oom(p->colWidth);
3459    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3460    p->nWidth = nColumn;
3461    p->actualWidth = &p->colWidth[nColumn];
3462  }
3463  memset(p->actualWidth, 0, nColumn*sizeof(int));
3464  for(i=0; i<nColumn; i++){
3465    w = p->colWidth[i];
3466    if( w<0 ) w = -w;
3467    p->actualWidth[i] = w;
3468  }
3469  for(i=0; i<nColumn; i++){
3470    const unsigned char *zNotUsed;
3471    int wx = p->colWidth[i];
3472    if( wx==0 ){
3473      wx = p->cmOpts.iWrap;
3474    }
3475    if( wx<0 ) wx = -wx;
3476    uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
3477    azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
3478  }
3479  do{
3480    int useNextLine = bNextLine;
3481    bNextLine = 0;
3482    if( (nRow+2)*nColumn >= nAlloc ){
3483      nAlloc *= 2;
3484      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3485      shell_check_oom(azData);
3486      abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
3487      shell_check_oom(abRowDiv);
3488    }
3489    abRowDiv[nRow] = 1;
3490    nRow++;
3491    for(i=0; i<nColumn; i++){
3492      int wx = p->colWidth[i];
3493      if( wx==0 ){
3494        wx = p->cmOpts.iWrap;
3495      }
3496      if( wx<0 ) wx = -wx;
3497      if( useNextLine ){
3498        uz = azNextLine[i];
3499        if( uz==0 ) uz = (u8*)zEmpty;
3500      }else if( p->cmOpts.bQuote ){
3501        sqlite3_free(azQuoted[i]);
3502        azQuoted[i] = quoted_column(pStmt,i);
3503        uz = (const unsigned char*)azQuoted[i];
3504      }else{
3505        uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
3506        if( uz==0 ) uz = (u8*)zShowNull;
3507      }
3508      azData[nRow*nColumn + i]
3509        = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
3510      if( azNextLine[i] ){
3511        bNextLine = 1;
3512        abRowDiv[nRow-1] = 0;
3513        bMultiLineRowExists = 1;
3514      }
3515    }
3516  }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
3517  nTotal = nColumn*(nRow+1);
3518  for(i=0; i<nTotal; i++){
3519    z = azData[i];
3520    if( z==0 ) z = (char*)zEmpty;
3521    n = strlenChar(z);
3522    j = i%nColumn;
3523    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3524  }
3525  if( seenInterrupt ) goto columnar_end;
3526  if( nColumn==0 ) goto columnar_end;
3527  switch( p->cMode ){
3528    case MODE_Column: {
3529      colSep = "  ";
3530      rowSep = "\n";
3531      if( p->showHeader ){
3532        for(i=0; i<nColumn; i++){
3533          w = p->actualWidth[i];
3534          if( p->colWidth[i]<0 ) w = -w;
3535          utf8_width_print(p->out, w, azData[i]);
3536          fputs(i==nColumn-1?"\n":"  ", p->out);
3537        }
3538        for(i=0; i<nColumn; i++){
3539          print_dashes(p->out, p->actualWidth[i]);
3540          fputs(i==nColumn-1?"\n":"  ", p->out);
3541        }
3542      }
3543      break;
3544    }
3545    case MODE_Table: {
3546      colSep = " | ";
3547      rowSep = " |\n";
3548      print_row_separator(p, nColumn, "+");
3549      fputs("| ", p->out);
3550      for(i=0; i<nColumn; i++){
3551        w = p->actualWidth[i];
3552        n = strlenChar(azData[i]);
3553        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3554        fputs(i==nColumn-1?" |\n":" | ", p->out);
3555      }
3556      print_row_separator(p, nColumn, "+");
3557      break;
3558    }
3559    case MODE_Markdown: {
3560      colSep = " | ";
3561      rowSep = " |\n";
3562      fputs("| ", p->out);
3563      for(i=0; i<nColumn; i++){
3564        w = p->actualWidth[i];
3565        n = strlenChar(azData[i]);
3566        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3567        fputs(i==nColumn-1?" |\n":" | ", p->out);
3568      }
3569      print_row_separator(p, nColumn, "|");
3570      break;
3571    }
3572    case MODE_Box: {
3573      colSep = " " BOX_13 " ";
3574      rowSep = " " BOX_13 "\n";
3575      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3576      utf8_printf(p->out, BOX_13 " ");
3577      for(i=0; i<nColumn; i++){
3578        w = p->actualWidth[i];
3579        n = strlenChar(azData[i]);
3580        utf8_printf(p->out, "%*s%s%*s%s",
3581            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3582            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3583      }
3584      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3585      break;
3586    }
3587  }
3588  for(i=nColumn, j=0; i<nTotal; i++, j++){
3589    if( j==0 && p->cMode!=MODE_Column ){
3590      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3591    }
3592    z = azData[i];
3593    if( z==0 ) z = p->nullValue;
3594    w = p->actualWidth[j];
3595    if( p->colWidth[j]<0 ) w = -w;
3596    utf8_width_print(p->out, w, z);
3597    if( j==nColumn-1 ){
3598      utf8_printf(p->out, "%s", rowSep);
3599      if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
3600        if( p->cMode==MODE_Table ){
3601          print_row_separator(p, nColumn, "+");
3602        }else if( p->cMode==MODE_Box ){
3603          print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3604        }else if( p->cMode==MODE_Column ){
3605          raw_printf(p->out, "\n");
3606        }
3607      }
3608      j = -1;
3609      if( seenInterrupt ) goto columnar_end;
3610    }else{
3611      utf8_printf(p->out, "%s", colSep);
3612    }
3613  }
3614  if( p->cMode==MODE_Table ){
3615    print_row_separator(p, nColumn, "+");
3616  }else if( p->cMode==MODE_Box ){
3617    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3618  }
3619columnar_end:
3620  if( seenInterrupt ){
3621    utf8_printf(p->out, "Interrupt\n");
3622  }
3623  nData = (nRow+1)*nColumn;
3624  for(i=0; i<nData; i++){
3625    z = azData[i];
3626    if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
3627  }
3628  sqlite3_free(azData);
3629  sqlite3_free((void*)azNextLine);
3630  sqlite3_free(abRowDiv);
3631  if( azQuoted ){
3632    for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
3633    sqlite3_free(azQuoted);
3634  }
3635}
3636
3637/*
3638** Run a prepared statement
3639*/
3640static void exec_prepared_stmt(
3641  ShellState *pArg,                                /* Pointer to ShellState */
3642  sqlite3_stmt *pStmt                              /* Statment to run */
3643){
3644  int rc;
3645  sqlite3_uint64 nRow = 0;
3646
3647  if( pArg->cMode==MODE_Column
3648   || pArg->cMode==MODE_Table
3649   || pArg->cMode==MODE_Box
3650   || pArg->cMode==MODE_Markdown
3651  ){
3652    exec_prepared_stmt_columnar(pArg, pStmt);
3653    return;
3654  }
3655
3656  /* perform the first step.  this will tell us if we
3657  ** have a result set or not and how wide it is.
3658  */
3659  rc = sqlite3_step(pStmt);
3660  /* if we have a result set... */
3661  if( SQLITE_ROW == rc ){
3662    /* allocate space for col name ptr, value ptr, and type */
3663    int nCol = sqlite3_column_count(pStmt);
3664    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3665    if( !pData ){
3666      shell_out_of_memory();
3667    }else{
3668      char **azCols = (char **)pData;      /* Names of result columns */
3669      char **azVals = &azCols[nCol];       /* Results */
3670      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3671      int i, x;
3672      assert(sizeof(int) <= sizeof(char *));
3673      /* save off ptrs to column names */
3674      for(i=0; i<nCol; i++){
3675        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3676      }
3677      do{
3678        nRow++;
3679        /* extract the data and data types */
3680        for(i=0; i<nCol; i++){
3681          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3682          if( x==SQLITE_BLOB
3683           && pArg
3684           && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
3685          ){
3686            azVals[i] = "";
3687          }else{
3688            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3689          }
3690          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3691            rc = SQLITE_NOMEM;
3692            break; /* from for */
3693          }
3694        } /* end for */
3695
3696        /* if data and types extracted successfully... */
3697        if( SQLITE_ROW == rc ){
3698          /* call the supplied callback with the result row data */
3699          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3700            rc = SQLITE_ABORT;
3701          }else{
3702            rc = sqlite3_step(pStmt);
3703          }
3704        }
3705      } while( SQLITE_ROW == rc );
3706      sqlite3_free(pData);
3707      if( pArg->cMode==MODE_Json ){
3708        fputs("]\n", pArg->out);
3709      }else if( pArg->cMode==MODE_Count ){
3710        char zBuf[200];
3711        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
3712                         nRow, nRow!=1 ? "s" : "");
3713        printf("%s", zBuf);
3714      }
3715    }
3716  }
3717}
3718
3719#ifndef SQLITE_OMIT_VIRTUALTABLE
3720/*
3721** This function is called to process SQL if the previous shell command
3722** was ".expert". It passes the SQL in the second argument directly to
3723** the sqlite3expert object.
3724**
3725** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3726** code. In this case, (*pzErr) may be set to point to a buffer containing
3727** an English language error message. It is the responsibility of the
3728** caller to eventually free this buffer using sqlite3_free().
3729*/
3730static int expertHandleSQL(
3731  ShellState *pState,
3732  const char *zSql,
3733  char **pzErr
3734){
3735  assert( pState->expert.pExpert );
3736  assert( pzErr==0 || *pzErr==0 );
3737  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3738}
3739
3740/*
3741** This function is called either to silently clean up the object
3742** created by the ".expert" command (if bCancel==1), or to generate a
3743** report from it and then clean it up (if bCancel==0).
3744**
3745** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3746** code. In this case, (*pzErr) may be set to point to a buffer containing
3747** an English language error message. It is the responsibility of the
3748** caller to eventually free this buffer using sqlite3_free().
3749*/
3750static int expertFinish(
3751  ShellState *pState,
3752  int bCancel,
3753  char **pzErr
3754){
3755  int rc = SQLITE_OK;
3756  sqlite3expert *p = pState->expert.pExpert;
3757  assert( p );
3758  assert( bCancel || pzErr==0 || *pzErr==0 );
3759  if( bCancel==0 ){
3760    FILE *out = pState->out;
3761    int bVerbose = pState->expert.bVerbose;
3762
3763    rc = sqlite3_expert_analyze(p, pzErr);
3764    if( rc==SQLITE_OK ){
3765      int nQuery = sqlite3_expert_count(p);
3766      int i;
3767
3768      if( bVerbose ){
3769        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3770        raw_printf(out, "-- Candidates -----------------------------\n");
3771        raw_printf(out, "%s\n", zCand);
3772      }
3773      for(i=0; i<nQuery; i++){
3774        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3775        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3776        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3777        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3778        if( bVerbose ){
3779          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3780          raw_printf(out, "%s\n\n", zSql);
3781        }
3782        raw_printf(out, "%s\n", zIdx);
3783        raw_printf(out, "%s\n", zEQP);
3784      }
3785    }
3786  }
3787  sqlite3_expert_destroy(p);
3788  pState->expert.pExpert = 0;
3789  return rc;
3790}
3791
3792/*
3793** Implementation of ".expert" dot command.
3794*/
3795static int expertDotCommand(
3796  ShellState *pState,             /* Current shell tool state */
3797  char **azArg,                   /* Array of arguments passed to dot command */
3798  int nArg                        /* Number of entries in azArg[] */
3799){
3800  int rc = SQLITE_OK;
3801  char *zErr = 0;
3802  int i;
3803  int iSample = 0;
3804
3805  assert( pState->expert.pExpert==0 );
3806  memset(&pState->expert, 0, sizeof(ExpertInfo));
3807
3808  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3809    char *z = azArg[i];
3810    int n;
3811    if( z[0]=='-' && z[1]=='-' ) z++;
3812    n = strlen30(z);
3813    if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
3814      pState->expert.bVerbose = 1;
3815    }
3816    else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
3817      if( i==(nArg-1) ){
3818        raw_printf(stderr, "option requires an argument: %s\n", z);
3819        rc = SQLITE_ERROR;
3820      }else{
3821        iSample = (int)integerValue(azArg[++i]);
3822        if( iSample<0 || iSample>100 ){
3823          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3824          rc = SQLITE_ERROR;
3825        }
3826      }
3827    }
3828    else{
3829      raw_printf(stderr, "unknown option: %s\n", z);
3830      rc = SQLITE_ERROR;
3831    }
3832  }
3833
3834  if( rc==SQLITE_OK ){
3835    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3836    if( pState->expert.pExpert==0 ){
3837      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
3838      rc = SQLITE_ERROR;
3839    }else{
3840      sqlite3_expert_config(
3841          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3842      );
3843    }
3844  }
3845  sqlite3_free(zErr);
3846
3847  return rc;
3848}
3849#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3850
3851/*
3852** Execute a statement or set of statements.  Print
3853** any result rows/columns depending on the current mode
3854** set via the supplied callback.
3855**
3856** This is very similar to SQLite's built-in sqlite3_exec()
3857** function except it takes a slightly different callback
3858** and callback data argument.
3859*/
3860static int shell_exec(
3861  ShellState *pArg,                         /* Pointer to ShellState */
3862  const char *zSql,                         /* SQL to be evaluated */
3863  char **pzErrMsg                           /* Error msg written here */
3864){
3865  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3866  int rc = SQLITE_OK;             /* Return Code */
3867  int rc2;
3868  const char *zLeftover;          /* Tail of unprocessed SQL */
3869  sqlite3 *db = pArg->db;
3870
3871  if( pzErrMsg ){
3872    *pzErrMsg = NULL;
3873  }
3874
3875#ifndef SQLITE_OMIT_VIRTUALTABLE
3876  if( pArg->expert.pExpert ){
3877    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3878    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3879  }
3880#endif
3881
3882  while( zSql[0] && (SQLITE_OK == rc) ){
3883    static const char *zStmtSql;
3884    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3885    if( SQLITE_OK != rc ){
3886      if( pzErrMsg ){
3887        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
3888      }
3889    }else{
3890      if( !pStmt ){
3891        /* this happens for a comment or white-space */
3892        zSql = zLeftover;
3893        while( IsSpace(zSql[0]) ) zSql++;
3894        continue;
3895      }
3896      zStmtSql = sqlite3_sql(pStmt);
3897      if( zStmtSql==0 ) zStmtSql = "";
3898      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3899
3900      /* save off the prepared statment handle and reset row count */
3901      if( pArg ){
3902        pArg->pStmt = pStmt;
3903        pArg->cnt = 0;
3904      }
3905
3906      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3907      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3908        sqlite3_stmt *pExplain;
3909        char *zEQP;
3910        int triggerEQP = 0;
3911        disable_debug_trace_modes();
3912        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3913        if( pArg->autoEQP>=AUTOEQP_trigger ){
3914          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3915        }
3916        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3917        shell_check_oom(zEQP);
3918        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3919        if( rc==SQLITE_OK ){
3920          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3921            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3922            int iEqpId = sqlite3_column_int(pExplain, 0);
3923            int iParentId = sqlite3_column_int(pExplain, 1);
3924            if( zEQPLine==0 ) zEQPLine = "";
3925            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3926            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3927          }
3928          eqp_render(pArg);
3929        }
3930        sqlite3_finalize(pExplain);
3931        sqlite3_free(zEQP);
3932        if( pArg->autoEQP>=AUTOEQP_full ){
3933          /* Also do an EXPLAIN for ".eqp full" mode */
3934          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3935          shell_check_oom(zEQP);
3936          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3937          if( rc==SQLITE_OK ){
3938            pArg->cMode = MODE_Explain;
3939            explain_data_prepare(pArg, pExplain);
3940            exec_prepared_stmt(pArg, pExplain);
3941            explain_data_delete(pArg);
3942          }
3943          sqlite3_finalize(pExplain);
3944          sqlite3_free(zEQP);
3945        }
3946        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3947          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3948          /* Reprepare pStmt before reactiving trace modes */
3949          sqlite3_finalize(pStmt);
3950          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3951          if( pArg ) pArg->pStmt = pStmt;
3952        }
3953        restore_debug_trace_modes();
3954      }
3955
3956      if( pArg ){
3957        pArg->cMode = pArg->mode;
3958        if( pArg->autoExplain ){
3959          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3960            pArg->cMode = MODE_Explain;
3961          }
3962          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3963            pArg->cMode = MODE_EQP;
3964          }
3965        }
3966
3967        /* If the shell is currently in ".explain" mode, gather the extra
3968        ** data required to add indents to the output.*/
3969        if( pArg->cMode==MODE_Explain ){
3970          explain_data_prepare(pArg, pStmt);
3971        }
3972      }
3973
3974      bind_prepared_stmt(pArg, pStmt);
3975      exec_prepared_stmt(pArg, pStmt);
3976      explain_data_delete(pArg);
3977      eqp_render(pArg);
3978
3979      /* print usage stats if stats on */
3980      if( pArg && pArg->statsOn ){
3981        display_stats(db, pArg, 0);
3982      }
3983
3984      /* print loop-counters if required */
3985      if( pArg && pArg->scanstatsOn ){
3986        display_scanstats(db, pArg);
3987      }
3988
3989      /* Finalize the statement just executed. If this fails, save a
3990      ** copy of the error message. Otherwise, set zSql to point to the
3991      ** next statement to execute. */
3992      rc2 = sqlite3_finalize(pStmt);
3993      if( rc!=SQLITE_NOMEM ) rc = rc2;
3994      if( rc==SQLITE_OK ){
3995        zSql = zLeftover;
3996        while( IsSpace(zSql[0]) ) zSql++;
3997      }else if( pzErrMsg ){
3998        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
3999      }
4000
4001      /* clear saved stmt handle */
4002      if( pArg ){
4003        pArg->pStmt = NULL;
4004      }
4005    }
4006  } /* end while */
4007
4008  return rc;
4009}
4010
4011/*
4012** Release memory previously allocated by tableColumnList().
4013*/
4014static void freeColumnList(char **azCol){
4015  int i;
4016  for(i=1; azCol[i]; i++){
4017    sqlite3_free(azCol[i]);
4018  }
4019  /* azCol[0] is a static string */
4020  sqlite3_free(azCol);
4021}
4022
4023/*
4024** Return a list of pointers to strings which are the names of all
4025** columns in table zTab.   The memory to hold the names is dynamically
4026** allocated and must be released by the caller using a subsequent call
4027** to freeColumnList().
4028**
4029** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
4030** value that needs to be preserved, then azCol[0] is filled in with the
4031** name of the rowid column.
4032**
4033** The first regular column in the table is azCol[1].  The list is terminated
4034** by an entry with azCol[i]==0.
4035*/
4036static char **tableColumnList(ShellState *p, const char *zTab){
4037  char **azCol = 0;
4038  sqlite3_stmt *pStmt;
4039  char *zSql;
4040  int nCol = 0;
4041  int nAlloc = 0;
4042  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
4043  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
4044  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4045  int rc;
4046
4047  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4048  shell_check_oom(zSql);
4049  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4050  sqlite3_free(zSql);
4051  if( rc ) return 0;
4052  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4053    if( nCol>=nAlloc-2 ){
4054      nAlloc = nAlloc*2 + nCol + 10;
4055      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4056      shell_check_oom(azCol);
4057    }
4058    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4059    shell_check_oom(azCol[nCol]);
4060    if( sqlite3_column_int(pStmt, 5) ){
4061      nPK++;
4062      if( nPK==1
4063       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4064                          "INTEGER")==0
4065      ){
4066        isIPK = 1;
4067      }else{
4068        isIPK = 0;
4069      }
4070    }
4071  }
4072  sqlite3_finalize(pStmt);
4073  if( azCol==0 ) return 0;
4074  azCol[0] = 0;
4075  azCol[nCol+1] = 0;
4076
4077  /* The decision of whether or not a rowid really needs to be preserved
4078  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
4079  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
4080  ** rowids on tables where the rowid is inaccessible because there are other
4081  ** columns in the table named "rowid", "_rowid_", and "oid".
4082  */
4083  if( preserveRowid && isIPK ){
4084    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4085    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
4086    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4087    ** ROWID aliases.  To distinguish these cases, check to see if
4088    ** there is a "pk" entry in "PRAGMA index_list".  There will be
4089    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4090    */
4091    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4092                           " WHERE origin='pk'", zTab);
4093    shell_check_oom(zSql);
4094    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4095    sqlite3_free(zSql);
4096    if( rc ){
4097      freeColumnList(azCol);
4098      return 0;
4099    }
4100    rc = sqlite3_step(pStmt);
4101    sqlite3_finalize(pStmt);
4102    preserveRowid = rc==SQLITE_ROW;
4103  }
4104  if( preserveRowid ){
4105    /* Only preserve the rowid if we can find a name to use for the
4106    ** rowid */
4107    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4108    int i, j;
4109    for(j=0; j<3; j++){
4110      for(i=1; i<=nCol; i++){
4111        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4112      }
4113      if( i>nCol ){
4114        /* At this point, we know that azRowid[j] is not the name of any
4115        ** ordinary column in the table.  Verify that azRowid[j] is a valid
4116        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
4117        ** tables will fail this last check */
4118        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4119        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4120        break;
4121      }
4122    }
4123  }
4124  return azCol;
4125}
4126
4127/*
4128** Toggle the reverse_unordered_selects setting.
4129*/
4130static void toggleSelectOrder(sqlite3 *db){
4131  sqlite3_stmt *pStmt = 0;
4132  int iSetting = 0;
4133  char zStmt[100];
4134  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4135  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4136    iSetting = sqlite3_column_int(pStmt, 0);
4137  }
4138  sqlite3_finalize(pStmt);
4139  sqlite3_snprintf(sizeof(zStmt), zStmt,
4140       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4141  sqlite3_exec(db, zStmt, 0, 0, 0);
4142}
4143
4144/*
4145** This is a different callback routine used for dumping the database.
4146** Each row received by this callback consists of a table name,
4147** the table type ("index" or "table") and SQL to create the table.
4148** This routine should print text sufficient to recreate the table.
4149*/
4150static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4151  int rc;
4152  const char *zTable;
4153  const char *zType;
4154  const char *zSql;
4155  ShellState *p = (ShellState *)pArg;
4156  int dataOnly;
4157  int noSys;
4158
4159  UNUSED_PARAMETER(azNotUsed);
4160  if( nArg!=3 || azArg==0 ) return 0;
4161  zTable = azArg[0];
4162  zType = azArg[1];
4163  zSql = azArg[2];
4164  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4165  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4166
4167  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4168    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4169  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4170    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4171  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
4172    return 0;
4173  }else if( dataOnly ){
4174    /* no-op */
4175  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4176    char *zIns;
4177    if( !p->writableSchema ){
4178      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4179      p->writableSchema = 1;
4180    }
4181    zIns = sqlite3_mprintf(
4182       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4183       "VALUES('table','%q','%q',0,'%q');",
4184       zTable, zTable, zSql);
4185    shell_check_oom(zIns);
4186    utf8_printf(p->out, "%s\n", zIns);
4187    sqlite3_free(zIns);
4188    return 0;
4189  }else{
4190    printSchemaLine(p->out, zSql, ";\n");
4191  }
4192
4193  if( cli_strcmp(zType, "table")==0 ){
4194    ShellText sSelect;
4195    ShellText sTable;
4196    char **azCol;
4197    int i;
4198    char *savedDestTable;
4199    int savedMode;
4200
4201    azCol = tableColumnList(p, zTable);
4202    if( azCol==0 ){
4203      p->nErr++;
4204      return 0;
4205    }
4206
4207    /* Always quote the table name, even if it appears to be pure ascii,
4208    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
4209    initText(&sTable);
4210    appendText(&sTable, zTable, quoteChar(zTable));
4211    /* If preserving the rowid, add a column list after the table name.
4212    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4213    ** instead of the usual "INSERT INTO tab VALUES(...)".
4214    */
4215    if( azCol[0] ){
4216      appendText(&sTable, "(", 0);
4217      appendText(&sTable, azCol[0], 0);
4218      for(i=1; azCol[i]; i++){
4219        appendText(&sTable, ",", 0);
4220        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4221      }
4222      appendText(&sTable, ")", 0);
4223    }
4224
4225    /* Build an appropriate SELECT statement */
4226    initText(&sSelect);
4227    appendText(&sSelect, "SELECT ", 0);
4228    if( azCol[0] ){
4229      appendText(&sSelect, azCol[0], 0);
4230      appendText(&sSelect, ",", 0);
4231    }
4232    for(i=1; azCol[i]; i++){
4233      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4234      if( azCol[i+1] ){
4235        appendText(&sSelect, ",", 0);
4236      }
4237    }
4238    freeColumnList(azCol);
4239    appendText(&sSelect, " FROM ", 0);
4240    appendText(&sSelect, zTable, quoteChar(zTable));
4241
4242    savedDestTable = p->zDestTable;
4243    savedMode = p->mode;
4244    p->zDestTable = sTable.z;
4245    p->mode = p->cMode = MODE_Insert;
4246    rc = shell_exec(p, sSelect.z, 0);
4247    if( (rc&0xff)==SQLITE_CORRUPT ){
4248      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4249      toggleSelectOrder(p->db);
4250      shell_exec(p, sSelect.z, 0);
4251      toggleSelectOrder(p->db);
4252    }
4253    p->zDestTable = savedDestTable;
4254    p->mode = savedMode;
4255    freeText(&sTable);
4256    freeText(&sSelect);
4257    if( rc ) p->nErr++;
4258  }
4259  return 0;
4260}
4261
4262/*
4263** Run zQuery.  Use dump_callback() as the callback routine so that
4264** the contents of the query are output as SQL statements.
4265**
4266** If we get a SQLITE_CORRUPT error, rerun the query after appending
4267** "ORDER BY rowid DESC" to the end.
4268*/
4269static int run_schema_dump_query(
4270  ShellState *p,
4271  const char *zQuery
4272){
4273  int rc;
4274  char *zErr = 0;
4275  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4276  if( rc==SQLITE_CORRUPT ){
4277    char *zQ2;
4278    int len = strlen30(zQuery);
4279    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4280    if( zErr ){
4281      utf8_printf(p->out, "/****** %s ******/\n", zErr);
4282      sqlite3_free(zErr);
4283      zErr = 0;
4284    }
4285    zQ2 = malloc( len+100 );
4286    if( zQ2==0 ) return rc;
4287    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4288    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4289    if( rc ){
4290      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4291    }else{
4292      rc = SQLITE_CORRUPT;
4293    }
4294    sqlite3_free(zErr);
4295    free(zQ2);
4296  }
4297  return rc;
4298}
4299
4300/*
4301** Text of help messages.
4302**
4303** The help text for each individual command begins with a line that starts
4304** with ".".  Subsequent lines are supplemental information.
4305**
4306** There must be two or more spaces between the end of the command and the
4307** start of the description of what that command does.
4308*/
4309static const char *(azHelp[]) = {
4310#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4311  && !defined(SQLITE_SHELL_FIDDLE)
4312  ".archive ...             Manage SQL archives",
4313  "   Each command must have exactly one of the following options:",
4314  "     -c, --create               Create a new archive",
4315  "     -u, --update               Add or update files with changed mtime",
4316  "     -i, --insert               Like -u but always add even if unchanged",
4317  "     -r, --remove               Remove files from archive",
4318  "     -t, --list                 List contents of archive",
4319  "     -x, --extract              Extract files from archive",
4320  "   Optional arguments:",
4321  "     -v, --verbose              Print each filename as it is processed",
4322  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
4323  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
4324  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
4325  "     -g, --glob                 Use glob matching for names in archive",
4326  "     -n, --dryrun               Show the SQL that would have occurred",
4327  "   Examples:",
4328  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
4329  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
4330  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
4331  "   See also:",
4332  "      http://sqlite.org/cli.html#sqlite_archive_support",
4333#endif
4334#ifndef SQLITE_OMIT_AUTHORIZATION
4335  ".auth ON|OFF             Show authorizer callbacks",
4336#endif
4337#ifndef SQLITE_SHELL_FIDDLE
4338  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
4339  "   Options:",
4340  "       --append            Use the appendvfs",
4341  "       --async             Write to FILE without journal and fsync()",
4342#endif
4343  ".bail on|off             Stop after hitting an error.  Default OFF",
4344  ".binary on|off           Turn binary output on or off.  Default OFF",
4345#ifndef SQLITE_SHELL_FIDDLE
4346  ".cd DIRECTORY            Change the working directory to DIRECTORY",
4347#endif
4348  ".changes on|off          Show number of rows changed by SQL",
4349#ifndef SQLITE_SHELL_FIDDLE
4350  ".check GLOB              Fail if output since .testcase does not match",
4351  ".clone NEWDB             Clone data into NEWDB from the existing database",
4352#endif
4353  ".connection [close] [#]  Open or close an auxiliary database connection",
4354  ".databases               List names and files of attached databases",
4355  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4356#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4357  ".dbinfo ?DB?             Show status information about the database",
4358#endif
4359  ".dump ?OBJECTS?          Render database content as SQL",
4360  "   Options:",
4361  "     --data-only            Output only INSERT statements",
4362  "     --newlines             Allow unescaped newline characters in output",
4363  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4364  "     --preserve-rowids      Include ROWID values in the output",
4365  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4366  "   Additional LIKE patterns can be given in subsequent arguments",
4367  ".echo on|off             Turn command echo on or off",
4368  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4369  "   Other Modes:",
4370#ifdef SQLITE_DEBUG
4371  "      test                  Show raw EXPLAIN QUERY PLAN output",
4372  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4373#endif
4374  "      trigger               Like \"full\" but also show trigger bytecode",
4375#ifndef SQLITE_SHELL_FIDDLE
4376  ".excel                   Display the output of next command in spreadsheet",
4377  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4378#endif
4379#ifndef SQLITE_SHELL_FIDDLE
4380  ".exit ?CODE?             Exit this program with return-code CODE",
4381#endif
4382  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4383  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4384  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4385  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4386  "   --help                  Show CMD details",
4387  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4388  ".headers on|off          Turn display of headers on or off",
4389  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4390#ifndef SQLITE_SHELL_FIDDLE
4391  ".import FILE TABLE       Import data from FILE into TABLE",
4392  "   Options:",
4393  "     --ascii               Use \\037 and \\036 as column and row separators",
4394  "     --csv                 Use , and \\n as column and row separators",
4395  "     --skip N              Skip the first N rows of input",
4396  "     --schema S            Target table to be S.TABLE",
4397  "     -v                    \"Verbose\" - increase auxiliary output",
4398  "   Notes:",
4399  "     *  If TABLE does not exist, it is created.  The first row of input",
4400  "        determines the column names.",
4401  "     *  If neither --csv or --ascii are used, the input mode is derived",
4402  "        from the \".mode\" output mode",
4403  "     *  If FILE begins with \"|\" then it is a command that generates the",
4404  "        input text.",
4405#endif
4406#ifndef SQLITE_OMIT_TEST_CONTROL
4407  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4408#endif
4409  ".indexes ?TABLE?         Show names of indexes",
4410  "                           If TABLE is specified, only show indexes for",
4411  "                           tables matching TABLE using the LIKE operator.",
4412#ifdef SQLITE_ENABLE_IOTRACE
4413  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4414#endif
4415  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4416  ".lint OPTIONS            Report potential schema issues.",
4417  "     Options:",
4418  "        fkey-indexes     Find missing foreign key indexes",
4419#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4420  ".load FILE ?ENTRY?       Load an extension library",
4421#endif
4422#ifndef SQLITE_SHELL_FIDDLE
4423  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4424#endif
4425  ".mode MODE ?OPTIONS?     Set output mode",
4426  "   MODE is one of:",
4427  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
4428  "     box         Tables using unicode box-drawing characters",
4429  "     csv         Comma-separated values",
4430  "     column      Output in columns.  (See .width)",
4431  "     html        HTML <table> code",
4432  "     insert      SQL insert statements for TABLE",
4433  "     json        Results in a JSON array",
4434  "     line        One value per line",
4435  "     list        Values delimited by \"|\"",
4436  "     markdown    Markdown table format",
4437  "     qbox        Shorthand for \"box --width 60 --quote\"",
4438  "     quote       Escape answers as for SQL",
4439  "     table       ASCII-art table",
4440  "     tabs        Tab-separated values",
4441  "     tcl         TCL list elements",
4442  "   OPTIONS: (for columnar modes or insert mode):",
4443  "     --wrap N       Wrap output lines to no longer than N characters",
4444  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
4445  "     --ww           Shorthand for \"--wordwrap 1\"",
4446  "     --quote        Quote output text as SQL literals",
4447  "     --noquote      Do not quote output text",
4448  "     TABLE          The name of SQL table used for \"insert\" mode",
4449#ifndef SQLITE_SHELL_FIDDLE
4450  ".nonce STRING            Suspend safe mode for one command if nonce matches",
4451#endif
4452  ".nullvalue STRING        Use STRING in place of NULL values",
4453#ifndef SQLITE_SHELL_FIDDLE
4454  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4455  "     If FILE begins with '|' then open as a pipe",
4456  "       --bom  Put a UTF8 byte-order mark at the beginning",
4457  "       -e     Send output to the system text editor",
4458  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4459  /* Note that .open is (partially) available in WASM builds but is
4460  ** currently only intended to be used by the fiddle tool, not
4461  ** end users, so is "undocumented." */
4462  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4463  "     Options:",
4464  "        --append        Use appendvfs to append database to the end of FILE",
4465#endif
4466#ifndef SQLITE_OMIT_DESERIALIZE
4467  "        --deserialize   Load into memory using sqlite3_deserialize()",
4468  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4469  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4470#endif
4471  "        --new           Initialize FILE to an empty database",
4472  "        --nofollow      Do not follow symbolic links",
4473  "        --readonly      Open FILE readonly",
4474  "        --zip           FILE is a ZIP archive",
4475#ifndef SQLITE_SHELL_FIDDLE
4476  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4477  "   If FILE begins with '|' then open it as a pipe.",
4478  "   Options:",
4479  "     --bom                 Prefix output with a UTF8 byte-order mark",
4480  "     -e                    Send output to the system text editor",
4481  "     -x                    Send output as CSV to a spreadsheet",
4482#endif
4483  ".parameter CMD ...       Manage SQL parameter bindings",
4484  "   clear                   Erase all bindings",
4485  "   init                    Initialize the TEMP table that holds bindings",
4486  "   list                    List the current parameter bindings",
4487  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4488  "                           PARAMETER should start with one of: $ : @ ?",
4489  "   unset PARAMETER         Remove PARAMETER from the binding table",
4490  ".print STRING...         Print literal STRING",
4491#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4492  ".progress N              Invoke progress handler after every N opcodes",
4493  "   --limit N                 Interrupt after N progress callbacks",
4494  "   --once                    Do no more than one progress interrupt",
4495  "   --quiet|-q                No output except at interrupts",
4496  "   --reset                   Reset the count for each input and interrupt",
4497#endif
4498  ".prompt MAIN CONTINUE    Replace the standard prompts",
4499#ifndef SQLITE_SHELL_FIDDLE
4500  ".quit                    Exit this program",
4501  ".read FILE               Read input from FILE or command output",
4502  "    If FILE begins with \"|\", it is a command that generates the input.",
4503#endif
4504#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4505  ".recover                 Recover as much data as possible from corrupt db.",
4506  "   --freelist-corrupt       Assume the freelist is corrupt",
4507  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4508  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4509  "   --no-rowids              Do not attempt to recover rowid values",
4510  "                            that are not also INTEGER PRIMARY KEYs",
4511#endif
4512#ifndef SQLITE_SHELL_FIDDLE
4513  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4514  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
4515#endif
4516  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4517  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4518  "   Options:",
4519  "      --indent             Try to pretty-print the schema",
4520  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4521  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4522  "    Options:",
4523  "       --init               Create a new SELFTEST table",
4524  "       -v                   Verbose output",
4525  ".separator COL ?ROW?     Change the column and row separators",
4526#if defined(SQLITE_ENABLE_SESSION)
4527  ".session ?NAME? CMD ...  Create or control sessions",
4528  "   Subcommands:",
4529  "     attach TABLE             Attach TABLE",
4530  "     changeset FILE           Write a changeset into FILE",
4531  "     close                    Close one session",
4532  "     enable ?BOOLEAN?         Set or query the enable bit",
4533  "     filter GLOB...           Reject tables matching GLOBs",
4534  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4535  "     isempty                  Query whether the session is empty",
4536  "     list                     List currently open session names",
4537  "     open DB NAME             Open a new session on DB",
4538  "     patchset FILE            Write a patchset into FILE",
4539  "   If ?NAME? is omitted, the first defined session is used.",
4540#endif
4541  ".sha3sum ...             Compute a SHA3 hash of database content",
4542  "    Options:",
4543  "      --schema              Also hash the sqlite_schema table",
4544  "      --sha3-224            Use the sha3-224 algorithm",
4545  "      --sha3-256            Use the sha3-256 algorithm (default)",
4546  "      --sha3-384            Use the sha3-384 algorithm",
4547  "      --sha3-512            Use the sha3-512 algorithm",
4548  "    Any other argument is a LIKE pattern for tables to hash",
4549#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4550  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4551#endif
4552  ".show                    Show the current values for various settings",
4553  ".stats ?ARG?             Show stats or turn stats on or off",
4554  "   off                      Turn off automatic stat display",
4555  "   on                       Turn on automatic stat display",
4556  "   stmt                     Show statement stats",
4557  "   vmstep                   Show the virtual machine step count only",
4558#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4559  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4560#endif
4561  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4562#ifndef SQLITE_SHELL_FIDDLE
4563  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4564#endif
4565  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4566  "                           Run \".testctrl\" with no arguments for details",
4567  ".timeout MS              Try opening locked tables for MS milliseconds",
4568  ".timer on|off            Turn SQL timer on or off",
4569#ifndef SQLITE_OMIT_TRACE
4570  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4571  "    FILE                    Send output to FILE",
4572  "    stdout                  Send output to stdout",
4573  "    stderr                  Send output to stderr",
4574  "    off                     Disable tracing",
4575  "    --expanded              Expand query parameters",
4576#ifdef SQLITE_ENABLE_NORMALIZE
4577  "    --normalized            Normal the SQL statements",
4578#endif
4579  "    --plain                 Show SQL as it is input",
4580  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4581  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4582  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4583  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4584#endif /* SQLITE_OMIT_TRACE */
4585#ifdef SQLITE_DEBUG
4586  ".unmodule NAME ...       Unregister virtual table modules",
4587  "    --allexcept             Unregister everything except those named",
4588#endif
4589  ".vfsinfo ?AUX?           Information about the top-level VFS",
4590  ".vfslist                 List all available VFSes",
4591  ".vfsname ?AUX?           Print the name of the VFS stack",
4592  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4593  "     Negative values right-justify",
4594};
4595
4596/*
4597** Output help text.
4598**
4599** zPattern describes the set of commands for which help text is provided.
4600** If zPattern is NULL, then show all commands, but only give a one-line
4601** description of each.
4602**
4603** Return the number of matches.
4604*/
4605static int showHelp(FILE *out, const char *zPattern){
4606  int i = 0;
4607  int j = 0;
4608  int n = 0;
4609  char *zPat;
4610  if( zPattern==0
4611   || zPattern[0]=='0'
4612   || cli_strcmp(zPattern,"-a")==0
4613   || cli_strcmp(zPattern,"-all")==0
4614   || cli_strcmp(zPattern,"--all")==0
4615  ){
4616    /* Show all commands, but only one line per command */
4617    if( zPattern==0 ) zPattern = "";
4618    for(i=0; i<ArraySize(azHelp); i++){
4619      if( azHelp[i][0]=='.' || zPattern[0] ){
4620        utf8_printf(out, "%s\n", azHelp[i]);
4621        n++;
4622      }
4623    }
4624  }else{
4625    /* Look for commands that for which zPattern is an exact prefix */
4626    zPat = sqlite3_mprintf(".%s*", zPattern);
4627    shell_check_oom(zPat);
4628    for(i=0; i<ArraySize(azHelp); i++){
4629      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4630        utf8_printf(out, "%s\n", azHelp[i]);
4631        j = i+1;
4632        n++;
4633      }
4634    }
4635    sqlite3_free(zPat);
4636    if( n ){
4637      if( n==1 ){
4638        /* when zPattern is a prefix of exactly one command, then include the
4639        ** details of that command, which should begin at offset j */
4640        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4641          utf8_printf(out, "%s\n", azHelp[j]);
4642          j++;
4643        }
4644      }
4645      return n;
4646    }
4647    /* Look for commands that contain zPattern anywhere.  Show the complete
4648    ** text of all commands that match. */
4649    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4650    shell_check_oom(zPat);
4651    for(i=0; i<ArraySize(azHelp); i++){
4652      if( azHelp[i][0]=='.' ) j = i;
4653      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4654        utf8_printf(out, "%s\n", azHelp[j]);
4655        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4656          j++;
4657          utf8_printf(out, "%s\n", azHelp[j]);
4658        }
4659        i = j;
4660        n++;
4661      }
4662    }
4663    sqlite3_free(zPat);
4664  }
4665  return n;
4666}
4667
4668/* Forward reference */
4669static int process_input(ShellState *p);
4670
4671/*
4672** Read the content of file zName into memory obtained from sqlite3_malloc64()
4673** and return a pointer to the buffer. The caller is responsible for freeing
4674** the memory.
4675**
4676** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4677** read.
4678**
4679** For convenience, a nul-terminator byte is always appended to the data read
4680** from the file before the buffer is returned. This byte is not included in
4681** the final value of (*pnByte), if applicable.
4682**
4683** NULL is returned if any error is encountered. The final value of *pnByte
4684** is undefined in this case.
4685*/
4686static char *readFile(const char *zName, int *pnByte){
4687  FILE *in = fopen(zName, "rb");
4688  long nIn;
4689  size_t nRead;
4690  char *pBuf;
4691  if( in==0 ) return 0;
4692  fseek(in, 0, SEEK_END);
4693  nIn = ftell(in);
4694  rewind(in);
4695  pBuf = sqlite3_malloc64( nIn+1 );
4696  if( pBuf==0 ){ fclose(in); return 0; }
4697  nRead = fread(pBuf, nIn, 1, in);
4698  fclose(in);
4699  if( nRead!=1 ){
4700    sqlite3_free(pBuf);
4701    return 0;
4702  }
4703  pBuf[nIn] = 0;
4704  if( pnByte ) *pnByte = nIn;
4705  return pBuf;
4706}
4707
4708#if defined(SQLITE_ENABLE_SESSION)
4709/*
4710** Close a single OpenSession object and release all of its associated
4711** resources.
4712*/
4713static void session_close(OpenSession *pSession){
4714  int i;
4715  sqlite3session_delete(pSession->p);
4716  sqlite3_free(pSession->zName);
4717  for(i=0; i<pSession->nFilter; i++){
4718    sqlite3_free(pSession->azFilter[i]);
4719  }
4720  sqlite3_free(pSession->azFilter);
4721  memset(pSession, 0, sizeof(OpenSession));
4722}
4723#endif
4724
4725/*
4726** Close all OpenSession objects and release all associated resources.
4727*/
4728#if defined(SQLITE_ENABLE_SESSION)
4729static void session_close_all(ShellState *p, int i){
4730  int j;
4731  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4732  for(j=0; j<pAuxDb->nSession; j++){
4733    session_close(&pAuxDb->aSession[j]);
4734  }
4735  pAuxDb->nSession = 0;
4736}
4737#else
4738# define session_close_all(X,Y)
4739#endif
4740
4741/*
4742** Implementation of the xFilter function for an open session.  Omit
4743** any tables named by ".session filter" but let all other table through.
4744*/
4745#if defined(SQLITE_ENABLE_SESSION)
4746static int session_filter(void *pCtx, const char *zTab){
4747  OpenSession *pSession = (OpenSession*)pCtx;
4748  int i;
4749  for(i=0; i<pSession->nFilter; i++){
4750    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4751  }
4752  return 1;
4753}
4754#endif
4755
4756/*
4757** Try to deduce the type of file for zName based on its content.  Return
4758** one of the SHELL_OPEN_* constants.
4759**
4760** If the file does not exist or is empty but its name looks like a ZIP
4761** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4762** Otherwise, assume an ordinary database regardless of the filename if
4763** the type cannot be determined from content.
4764*/
4765int deduceDatabaseType(const char *zName, int dfltZip){
4766  FILE *f = fopen(zName, "rb");
4767  size_t n;
4768  int rc = SHELL_OPEN_UNSPEC;
4769  char zBuf[100];
4770  if( f==0 ){
4771    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4772       return SHELL_OPEN_ZIPFILE;
4773    }else{
4774       return SHELL_OPEN_NORMAL;
4775    }
4776  }
4777  n = fread(zBuf, 16, 1, f);
4778  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4779    fclose(f);
4780    return SHELL_OPEN_NORMAL;
4781  }
4782  fseek(f, -25, SEEK_END);
4783  n = fread(zBuf, 25, 1, f);
4784  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4785    rc = SHELL_OPEN_APPENDVFS;
4786  }else{
4787    fseek(f, -22, SEEK_END);
4788    n = fread(zBuf, 22, 1, f);
4789    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4790       && zBuf[3]==0x06 ){
4791      rc = SHELL_OPEN_ZIPFILE;
4792    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4793      rc = SHELL_OPEN_ZIPFILE;
4794    }
4795  }
4796  fclose(f);
4797  return rc;
4798}
4799
4800#ifndef SQLITE_OMIT_DESERIALIZE
4801/*
4802** Reconstruct an in-memory database using the output from the "dbtotxt"
4803** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4804** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4805*/
4806static unsigned char *readHexDb(ShellState *p, int *pnData){
4807  unsigned char *a = 0;
4808  int nLine;
4809  int n = 0;
4810  int pgsz = 0;
4811  int iOffset = 0;
4812  int j, k;
4813  int rc;
4814  FILE *in;
4815  const char *zDbFilename = p->pAuxDb->zDbFilename;
4816  unsigned int x[16];
4817  char zLine[1000];
4818  if( zDbFilename ){
4819    in = fopen(zDbFilename, "r");
4820    if( in==0 ){
4821      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4822      return 0;
4823    }
4824    nLine = 0;
4825  }else{
4826    in = p->in;
4827    nLine = p->lineno;
4828    if( in==0 ) in = stdin;
4829  }
4830  *pnData = 0;
4831  nLine++;
4832  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4833  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4834  if( rc!=2 ) goto readHexDb_error;
4835  if( n<0 ) goto readHexDb_error;
4836  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4837  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4838  a = sqlite3_malloc( n ? n : 1 );
4839  shell_check_oom(a);
4840  memset(a, 0, n);
4841  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4842    utf8_printf(stderr, "invalid pagesize\n");
4843    goto readHexDb_error;
4844  }
4845  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4846    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4847    if( rc==2 ){
4848      iOffset = k;
4849      continue;
4850    }
4851    if( cli_strncmp(zLine, "| end ", 6)==0 ){
4852      break;
4853    }
4854    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4855                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4856                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4857    if( rc==17 ){
4858      k = iOffset+j;
4859      if( k+16<=n && k>=0 ){
4860        int ii;
4861        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4862      }
4863    }
4864  }
4865  *pnData = n;
4866  if( in!=p->in ){
4867    fclose(in);
4868  }else{
4869    p->lineno = nLine;
4870  }
4871  return a;
4872
4873readHexDb_error:
4874  if( in!=p->in ){
4875    fclose(in);
4876  }else{
4877    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4878      nLine++;
4879      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
4880    }
4881    p->lineno = nLine;
4882  }
4883  sqlite3_free(a);
4884  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4885  return 0;
4886}
4887#endif /* SQLITE_OMIT_DESERIALIZE */
4888
4889/*
4890** Scalar function "shell_int32". The first argument to this function
4891** must be a blob. The second a non-negative integer. This function
4892** reads and returns a 32-bit big-endian integer from byte
4893** offset (4*<arg2>) of the blob.
4894*/
4895static void shellInt32(
4896  sqlite3_context *context,
4897  int argc,
4898  sqlite3_value **argv
4899){
4900  const unsigned char *pBlob;
4901  int nBlob;
4902  int iInt;
4903
4904  UNUSED_PARAMETER(argc);
4905  nBlob = sqlite3_value_bytes(argv[0]);
4906  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4907  iInt = sqlite3_value_int(argv[1]);
4908
4909  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4910    const unsigned char *a = &pBlob[iInt*4];
4911    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4912                       + ((sqlite3_int64)a[1]<<16)
4913                       + ((sqlite3_int64)a[2]<< 8)
4914                       + ((sqlite3_int64)a[3]<< 0);
4915    sqlite3_result_int64(context, iVal);
4916  }
4917}
4918
4919/*
4920** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4921** using "..." with internal double-quote characters doubled.
4922*/
4923static void shellIdQuote(
4924  sqlite3_context *context,
4925  int argc,
4926  sqlite3_value **argv
4927){
4928  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4929  UNUSED_PARAMETER(argc);
4930  if( zName ){
4931    char *z = sqlite3_mprintf("\"%w\"", zName);
4932    sqlite3_result_text(context, z, -1, sqlite3_free);
4933  }
4934}
4935
4936/*
4937** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4938*/
4939static void shellUSleepFunc(
4940  sqlite3_context *context,
4941  int argcUnused,
4942  sqlite3_value **argv
4943){
4944  int sleep = sqlite3_value_int(argv[0]);
4945  (void)argcUnused;
4946  sqlite3_sleep(sleep/1000);
4947  sqlite3_result_int(context, sleep);
4948}
4949
4950/*
4951** Scalar function "shell_escape_crnl" used by the .recover command.
4952** The argument passed to this function is the output of built-in
4953** function quote(). If the first character of the input is "'",
4954** indicating that the value passed to quote() was a text value,
4955** then this function searches the input for "\n" and "\r" characters
4956** and adds a wrapper similar to the following:
4957**
4958**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4959**
4960** Or, if the first character of the input is not "'", then a copy
4961** of the input is returned.
4962*/
4963static void shellEscapeCrnl(
4964  sqlite3_context *context,
4965  int argc,
4966  sqlite3_value **argv
4967){
4968  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4969  UNUSED_PARAMETER(argc);
4970  if( zText && zText[0]=='\'' ){
4971    i64 nText = sqlite3_value_bytes(argv[0]);
4972    i64 i;
4973    char zBuf1[20];
4974    char zBuf2[20];
4975    const char *zNL = 0;
4976    const char *zCR = 0;
4977    i64 nCR = 0;
4978    i64 nNL = 0;
4979
4980    for(i=0; zText[i]; i++){
4981      if( zNL==0 && zText[i]=='\n' ){
4982        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4983        nNL = strlen(zNL);
4984      }
4985      if( zCR==0 && zText[i]=='\r' ){
4986        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4987        nCR = strlen(zCR);
4988      }
4989    }
4990
4991    if( zNL || zCR ){
4992      i64 iOut = 0;
4993      i64 nMax = (nNL > nCR) ? nNL : nCR;
4994      i64 nAlloc = nMax * nText + (nMax+64)*2;
4995      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4996      if( zOut==0 ){
4997        sqlite3_result_error_nomem(context);
4998        return;
4999      }
5000
5001      if( zNL && zCR ){
5002        memcpy(&zOut[iOut], "replace(replace(", 16);
5003        iOut += 16;
5004      }else{
5005        memcpy(&zOut[iOut], "replace(", 8);
5006        iOut += 8;
5007      }
5008      for(i=0; zText[i]; i++){
5009        if( zText[i]=='\n' ){
5010          memcpy(&zOut[iOut], zNL, nNL);
5011          iOut += nNL;
5012        }else if( zText[i]=='\r' ){
5013          memcpy(&zOut[iOut], zCR, nCR);
5014          iOut += nCR;
5015        }else{
5016          zOut[iOut] = zText[i];
5017          iOut++;
5018        }
5019      }
5020
5021      if( zNL ){
5022        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5023        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
5024        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
5025      }
5026      if( zCR ){
5027        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5028        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
5029        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
5030      }
5031
5032      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
5033      sqlite3_free(zOut);
5034      return;
5035    }
5036  }
5037
5038  sqlite3_result_value(context, argv[0]);
5039}
5040
5041/* Flags for open_db().
5042**
5043** The default behavior of open_db() is to exit(1) if the database fails to
5044** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5045** but still returns without calling exit.
5046**
5047** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5048** ZIP archive if the file does not exist or is empty and its name matches
5049** the *.zip pattern.
5050*/
5051#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
5052#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
5053
5054/*
5055** Make sure the database is open.  If it is not, then open it.  If
5056** the database fails to open, print an error message and exit.
5057*/
5058static void open_db(ShellState *p, int openFlags){
5059  if( p->db==0 ){
5060    const char *zDbFilename = p->pAuxDb->zDbFilename;
5061    if( p->openMode==SHELL_OPEN_UNSPEC ){
5062      if( zDbFilename==0 || zDbFilename[0]==0 ){
5063        p->openMode = SHELL_OPEN_NORMAL;
5064      }else{
5065        p->openMode = (u8)deduceDatabaseType(zDbFilename,
5066                             (openFlags & OPEN_DB_ZIPFILE)!=0);
5067      }
5068    }
5069    switch( p->openMode ){
5070      case SHELL_OPEN_APPENDVFS: {
5071        sqlite3_open_v2(zDbFilename, &p->db,
5072           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5073        break;
5074      }
5075      case SHELL_OPEN_HEXDB:
5076      case SHELL_OPEN_DESERIALIZE: {
5077        sqlite3_open(0, &p->db);
5078        break;
5079      }
5080      case SHELL_OPEN_ZIPFILE: {
5081        sqlite3_open(":memory:", &p->db);
5082        break;
5083      }
5084      case SHELL_OPEN_READONLY: {
5085        sqlite3_open_v2(zDbFilename, &p->db,
5086            SQLITE_OPEN_READONLY|p->openFlags, 0);
5087        break;
5088      }
5089      case SHELL_OPEN_UNSPEC:
5090      case SHELL_OPEN_NORMAL: {
5091        sqlite3_open_v2(zDbFilename, &p->db,
5092           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5093        break;
5094      }
5095    }
5096    globalDb = p->db;
5097    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5098      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5099          zDbFilename, sqlite3_errmsg(p->db));
5100      if( openFlags & OPEN_DB_KEEPALIVE ){
5101        sqlite3_open(":memory:", &p->db);
5102        return;
5103      }
5104      exit(1);
5105    }
5106#ifndef SQLITE_OMIT_LOAD_EXTENSION
5107    sqlite3_enable_load_extension(p->db, 1);
5108#endif
5109    sqlite3_shathree_init(p->db, 0, 0);
5110    sqlite3_uint_init(p->db, 0, 0);
5111    sqlite3_decimal_init(p->db, 0, 0);
5112    sqlite3_regexp_init(p->db, 0, 0);
5113    sqlite3_ieee_init(p->db, 0, 0);
5114    sqlite3_series_init(p->db, 0, 0);
5115#ifndef SQLITE_SHELL_FIDDLE
5116    sqlite3_fileio_init(p->db, 0, 0);
5117    sqlite3_completion_init(p->db, 0, 0);
5118#endif
5119#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5120    sqlite3_dbdata_init(p->db, 0, 0);
5121#endif
5122#ifdef SQLITE_HAVE_ZLIB
5123    if( !p->bSafeModePersist ){
5124      sqlite3_zipfile_init(p->db, 0, 0);
5125      sqlite3_sqlar_init(p->db, 0, 0);
5126    }
5127#endif
5128    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5129                            shellAddSchemaName, 0, 0);
5130    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5131                            shellModuleSchema, 0, 0);
5132    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5133                            shellPutsFunc, 0, 0);
5134    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
5135                            shellEscapeCrnl, 0, 0);
5136    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
5137                            shellInt32, 0, 0);
5138    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
5139                            shellIdQuote, 0, 0);
5140    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5141                            shellUSleepFunc, 0, 0);
5142#ifndef SQLITE_NOHAVE_SYSTEM
5143    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5144                            editFunc, 0, 0);
5145    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5146                            editFunc, 0, 0);
5147#endif
5148    if( p->openMode==SHELL_OPEN_ZIPFILE ){
5149      char *zSql = sqlite3_mprintf(
5150         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5151      shell_check_oom(zSql);
5152      sqlite3_exec(p->db, zSql, 0, 0, 0);
5153      sqlite3_free(zSql);
5154    }
5155#ifndef SQLITE_OMIT_DESERIALIZE
5156    else
5157    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5158      int rc;
5159      int nData = 0;
5160      unsigned char *aData;
5161      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5162        aData = (unsigned char*)readFile(zDbFilename, &nData);
5163      }else{
5164        aData = readHexDb(p, &nData);
5165        if( aData==0 ){
5166          return;
5167        }
5168      }
5169      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5170                   SQLITE_DESERIALIZE_RESIZEABLE |
5171                   SQLITE_DESERIALIZE_FREEONCLOSE);
5172      if( rc ){
5173        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5174      }
5175      if( p->szMax>0 ){
5176        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5177      }
5178    }
5179#endif
5180  }
5181  if( p->bSafeModePersist && p->db!=0 ){
5182    sqlite3_set_authorizer(p->db, safeModeAuth, p);
5183  }
5184}
5185
5186/*
5187** Attempt to close the databaes connection.  Report errors.
5188*/
5189void close_db(sqlite3 *db){
5190  int rc = sqlite3_close(db);
5191  if( rc ){
5192    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5193        rc, sqlite3_errmsg(db));
5194  }
5195}
5196
5197#if HAVE_READLINE || HAVE_EDITLINE
5198/*
5199** Readline completion callbacks
5200*/
5201static char *readline_completion_generator(const char *text, int state){
5202  static sqlite3_stmt *pStmt = 0;
5203  char *zRet;
5204  if( state==0 ){
5205    char *zSql;
5206    sqlite3_finalize(pStmt);
5207    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5208                           "  FROM completion(%Q) ORDER BY 1", text);
5209    shell_check_oom(zSql);
5210    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5211    sqlite3_free(zSql);
5212  }
5213  if( sqlite3_step(pStmt)==SQLITE_ROW ){
5214    const char *z = (const char*)sqlite3_column_text(pStmt,0);
5215    zRet = z ? strdup(z) : 0;
5216  }else{
5217    sqlite3_finalize(pStmt);
5218    pStmt = 0;
5219    zRet = 0;
5220  }
5221  return zRet;
5222}
5223static char **readline_completion(const char *zText, int iStart, int iEnd){
5224  rl_attempted_completion_over = 1;
5225  return rl_completion_matches(zText, readline_completion_generator);
5226}
5227
5228#elif HAVE_LINENOISE
5229/*
5230** Linenoise completion callback
5231*/
5232static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5233  i64 nLine = strlen(zLine);
5234  i64 i, iStart;
5235  sqlite3_stmt *pStmt = 0;
5236  char *zSql;
5237  char zBuf[1000];
5238
5239  if( nLine>sizeof(zBuf)-30 ) return;
5240  if( zLine[0]=='.' || zLine[0]=='#') return;
5241  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5242  if( i==nLine-1 ) return;
5243  iStart = i+1;
5244  memcpy(zBuf, zLine, iStart);
5245  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5246                         "  FROM completion(%Q,%Q) ORDER BY 1",
5247                         &zLine[iStart], zLine);
5248  shell_check_oom(zSql);
5249  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5250  sqlite3_free(zSql);
5251  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5252  while( sqlite3_step(pStmt)==SQLITE_ROW ){
5253    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5254    int nCompletion = sqlite3_column_bytes(pStmt, 0);
5255    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
5256      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5257      linenoiseAddCompletion(lc, zBuf);
5258    }
5259  }
5260  sqlite3_finalize(pStmt);
5261}
5262#endif
5263
5264/*
5265** Do C-language style dequoting.
5266**
5267**    \a    -> alarm
5268**    \b    -> backspace
5269**    \t    -> tab
5270**    \n    -> newline
5271**    \v    -> vertical tab
5272**    \f    -> form feed
5273**    \r    -> carriage return
5274**    \s    -> space
5275**    \"    -> "
5276**    \'    -> '
5277**    \\    -> backslash
5278**    \NNN  -> ascii character NNN in octal
5279*/
5280static void resolve_backslashes(char *z){
5281  int i, j;
5282  char c;
5283  while( *z && *z!='\\' ) z++;
5284  for(i=j=0; (c = z[i])!=0; i++, j++){
5285    if( c=='\\' && z[i+1]!=0 ){
5286      c = z[++i];
5287      if( c=='a' ){
5288        c = '\a';
5289      }else if( c=='b' ){
5290        c = '\b';
5291      }else if( c=='t' ){
5292        c = '\t';
5293      }else if( c=='n' ){
5294        c = '\n';
5295      }else if( c=='v' ){
5296        c = '\v';
5297      }else if( c=='f' ){
5298        c = '\f';
5299      }else if( c=='r' ){
5300        c = '\r';
5301      }else if( c=='"' ){
5302        c = '"';
5303      }else if( c=='\'' ){
5304        c = '\'';
5305      }else if( c=='\\' ){
5306        c = '\\';
5307      }else if( c>='0' && c<='7' ){
5308        c -= '0';
5309        if( z[i+1]>='0' && z[i+1]<='7' ){
5310          i++;
5311          c = (c<<3) + z[i] - '0';
5312          if( z[i+1]>='0' && z[i+1]<='7' ){
5313            i++;
5314            c = (c<<3) + z[i] - '0';
5315          }
5316        }
5317      }
5318    }
5319    z[j] = c;
5320  }
5321  if( j<i ) z[j] = 0;
5322}
5323
5324/*
5325** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
5326** for TRUE and FALSE.  Return the integer value if appropriate.
5327*/
5328static int booleanValue(const char *zArg){
5329  int i;
5330  if( zArg[0]=='0' && zArg[1]=='x' ){
5331    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5332  }else{
5333    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5334  }
5335  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5336  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5337    return 1;
5338  }
5339  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5340    return 0;
5341  }
5342  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5343          zArg);
5344  return 0;
5345}
5346
5347/*
5348** Set or clear a shell flag according to a boolean value.
5349*/
5350static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5351  if( booleanValue(zArg) ){
5352    ShellSetFlag(p, mFlag);
5353  }else{
5354    ShellClearFlag(p, mFlag);
5355  }
5356}
5357
5358/*
5359** Close an output file, assuming it is not stderr or stdout
5360*/
5361static void output_file_close(FILE *f){
5362  if( f && f!=stdout && f!=stderr ) fclose(f);
5363}
5364
5365/*
5366** Try to open an output file.   The names "stdout" and "stderr" are
5367** recognized and do the right thing.  NULL is returned if the output
5368** filename is "off".
5369*/
5370static FILE *output_file_open(const char *zFile, int bTextMode){
5371  FILE *f;
5372  if( cli_strcmp(zFile,"stdout")==0 ){
5373    f = stdout;
5374  }else if( cli_strcmp(zFile, "stderr")==0 ){
5375    f = stderr;
5376  }else if( cli_strcmp(zFile, "off")==0 ){
5377    f = 0;
5378  }else{
5379    f = fopen(zFile, bTextMode ? "w" : "wb");
5380    if( f==0 ){
5381      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5382    }
5383  }
5384  return f;
5385}
5386
5387#ifndef SQLITE_OMIT_TRACE
5388/*
5389** A routine for handling output from sqlite3_trace().
5390*/
5391static int sql_trace_callback(
5392  unsigned mType,         /* The trace type */
5393  void *pArg,             /* The ShellState pointer */
5394  void *pP,               /* Usually a pointer to sqlite_stmt */
5395  void *pX                /* Auxiliary output */
5396){
5397  ShellState *p = (ShellState*)pArg;
5398  sqlite3_stmt *pStmt;
5399  const char *zSql;
5400  i64 nSql;
5401  if( p->traceOut==0 ) return 0;
5402  if( mType==SQLITE_TRACE_CLOSE ){
5403    utf8_printf(p->traceOut, "-- closing database connection\n");
5404    return 0;
5405  }
5406  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5407    zSql = (const char*)pX;
5408  }else{
5409    pStmt = (sqlite3_stmt*)pP;
5410    switch( p->eTraceType ){
5411      case SHELL_TRACE_EXPANDED: {
5412        zSql = sqlite3_expanded_sql(pStmt);
5413        break;
5414      }
5415#ifdef SQLITE_ENABLE_NORMALIZE
5416      case SHELL_TRACE_NORMALIZED: {
5417        zSql = sqlite3_normalized_sql(pStmt);
5418        break;
5419      }
5420#endif
5421      default: {
5422        zSql = sqlite3_sql(pStmt);
5423        break;
5424      }
5425    }
5426  }
5427  if( zSql==0 ) return 0;
5428  nSql = strlen(zSql);
5429  if( nSql>1000000000 ) nSql = 1000000000;
5430  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5431  switch( mType ){
5432    case SQLITE_TRACE_ROW:
5433    case SQLITE_TRACE_STMT: {
5434      utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5435      break;
5436    }
5437    case SQLITE_TRACE_PROFILE: {
5438      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5439      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5440      break;
5441    }
5442  }
5443  return 0;
5444}
5445#endif
5446
5447/*
5448** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5449** a useful spot to set a debugger breakpoint.
5450*/
5451static void test_breakpoint(void){
5452  static int nCall = 0;
5453  nCall++;
5454}
5455
5456/*
5457** An object used to read a CSV and other files for import.
5458*/
5459typedef struct ImportCtx ImportCtx;
5460struct ImportCtx {
5461  const char *zFile;  /* Name of the input file */
5462  FILE *in;           /* Read the CSV text from this input stream */
5463  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5464  char *z;            /* Accumulated text for a field */
5465  int n;              /* Number of bytes in z */
5466  int nAlloc;         /* Space allocated for z[] */
5467  int nLine;          /* Current line number */
5468  int nRow;           /* Number of rows imported */
5469  int nErr;           /* Number of errors encountered */
5470  int bNotFirst;      /* True if one or more bytes already read */
5471  int cTerm;          /* Character that terminated the most recent field */
5472  int cColSep;        /* The column separator character.  (Usually ",") */
5473  int cRowSep;        /* The row separator character.  (Usually "\n") */
5474};
5475
5476/* Clean up resourced used by an ImportCtx */
5477static void import_cleanup(ImportCtx *p){
5478  if( p->in!=0 && p->xCloser!=0 ){
5479    p->xCloser(p->in);
5480    p->in = 0;
5481  }
5482  sqlite3_free(p->z);
5483  p->z = 0;
5484}
5485
5486/* Append a single byte to z[] */
5487static void import_append_char(ImportCtx *p, int c){
5488  if( p->n+1>=p->nAlloc ){
5489    p->nAlloc += p->nAlloc + 100;
5490    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5491    shell_check_oom(p->z);
5492  }
5493  p->z[p->n++] = (char)c;
5494}
5495
5496/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5497** with the option of having a separator other than ",".
5498**
5499**   +  Input comes from p->in.
5500**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5501**      from sqlite3_malloc64().
5502**   +  Use p->cSep as the column separator.  The default is ",".
5503**   +  Use p->rSep as the row separator.  The default is "\n".
5504**   +  Keep track of the line number in p->nLine.
5505**   +  Store the character that terminates the field in p->cTerm.  Store
5506**      EOF on end-of-file.
5507**   +  Report syntax errors on stderr
5508*/
5509static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5510  int c;
5511  int cSep = p->cColSep;
5512  int rSep = p->cRowSep;
5513  p->n = 0;
5514  c = fgetc(p->in);
5515  if( c==EOF || seenInterrupt ){
5516    p->cTerm = EOF;
5517    return 0;
5518  }
5519  if( c=='"' ){
5520    int pc, ppc;
5521    int startLine = p->nLine;
5522    int cQuote = c;
5523    pc = ppc = 0;
5524    while( 1 ){
5525      c = fgetc(p->in);
5526      if( c==rSep ) p->nLine++;
5527      if( c==cQuote ){
5528        if( pc==cQuote ){
5529          pc = 0;
5530          continue;
5531        }
5532      }
5533      if( (c==cSep && pc==cQuote)
5534       || (c==rSep && pc==cQuote)
5535       || (c==rSep && pc=='\r' && ppc==cQuote)
5536       || (c==EOF && pc==cQuote)
5537      ){
5538        do{ p->n--; }while( p->z[p->n]!=cQuote );
5539        p->cTerm = c;
5540        break;
5541      }
5542      if( pc==cQuote && c!='\r' ){
5543        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5544                p->zFile, p->nLine, cQuote);
5545      }
5546      if( c==EOF ){
5547        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5548                p->zFile, startLine, cQuote);
5549        p->cTerm = c;
5550        break;
5551      }
5552      import_append_char(p, c);
5553      ppc = pc;
5554      pc = c;
5555    }
5556  }else{
5557    /* If this is the first field being parsed and it begins with the
5558    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5559    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5560      import_append_char(p, c);
5561      c = fgetc(p->in);
5562      if( (c&0xff)==0xbb ){
5563        import_append_char(p, c);
5564        c = fgetc(p->in);
5565        if( (c&0xff)==0xbf ){
5566          p->bNotFirst = 1;
5567          p->n = 0;
5568          return csv_read_one_field(p);
5569        }
5570      }
5571    }
5572    while( c!=EOF && c!=cSep && c!=rSep ){
5573      import_append_char(p, c);
5574      c = fgetc(p->in);
5575    }
5576    if( c==rSep ){
5577      p->nLine++;
5578      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5579    }
5580    p->cTerm = c;
5581  }
5582  if( p->z ) p->z[p->n] = 0;
5583  p->bNotFirst = 1;
5584  return p->z;
5585}
5586
5587/* Read a single field of ASCII delimited text.
5588**
5589**   +  Input comes from p->in.
5590**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5591**      from sqlite3_malloc64().
5592**   +  Use p->cSep as the column separator.  The default is "\x1F".
5593**   +  Use p->rSep as the row separator.  The default is "\x1E".
5594**   +  Keep track of the row number in p->nLine.
5595**   +  Store the character that terminates the field in p->cTerm.  Store
5596**      EOF on end-of-file.
5597**   +  Report syntax errors on stderr
5598*/
5599static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5600  int c;
5601  int cSep = p->cColSep;
5602  int rSep = p->cRowSep;
5603  p->n = 0;
5604  c = fgetc(p->in);
5605  if( c==EOF || seenInterrupt ){
5606    p->cTerm = EOF;
5607    return 0;
5608  }
5609  while( c!=EOF && c!=cSep && c!=rSep ){
5610    import_append_char(p, c);
5611    c = fgetc(p->in);
5612  }
5613  if( c==rSep ){
5614    p->nLine++;
5615  }
5616  p->cTerm = c;
5617  if( p->z ) p->z[p->n] = 0;
5618  return p->z;
5619}
5620
5621/*
5622** Try to transfer data for table zTable.  If an error is seen while
5623** moving forward, try to go backwards.  The backwards movement won't
5624** work for WITHOUT ROWID tables.
5625*/
5626static void tryToCloneData(
5627  ShellState *p,
5628  sqlite3 *newDb,
5629  const char *zTable
5630){
5631  sqlite3_stmt *pQuery = 0;
5632  sqlite3_stmt *pInsert = 0;
5633  char *zQuery = 0;
5634  char *zInsert = 0;
5635  int rc;
5636  int i, j, n;
5637  int nTable = strlen30(zTable);
5638  int k = 0;
5639  int cnt = 0;
5640  const int spinRate = 10000;
5641
5642  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5643  shell_check_oom(zQuery);
5644  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5645  if( rc ){
5646    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5647            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5648            zQuery);
5649    goto end_data_xfer;
5650  }
5651  n = sqlite3_column_count(pQuery);
5652  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5653  shell_check_oom(zInsert);
5654  sqlite3_snprintf(200+nTable,zInsert,
5655                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5656  i = strlen30(zInsert);
5657  for(j=1; j<n; j++){
5658    memcpy(zInsert+i, ",?", 2);
5659    i += 2;
5660  }
5661  memcpy(zInsert+i, ");", 3);
5662  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5663  if( rc ){
5664    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5665            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5666            zQuery);
5667    goto end_data_xfer;
5668  }
5669  for(k=0; k<2; k++){
5670    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5671      for(i=0; i<n; i++){
5672        switch( sqlite3_column_type(pQuery, i) ){
5673          case SQLITE_NULL: {
5674            sqlite3_bind_null(pInsert, i+1);
5675            break;
5676          }
5677          case SQLITE_INTEGER: {
5678            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5679            break;
5680          }
5681          case SQLITE_FLOAT: {
5682            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5683            break;
5684          }
5685          case SQLITE_TEXT: {
5686            sqlite3_bind_text(pInsert, i+1,
5687                             (const char*)sqlite3_column_text(pQuery,i),
5688                             -1, SQLITE_STATIC);
5689            break;
5690          }
5691          case SQLITE_BLOB: {
5692            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5693                                            sqlite3_column_bytes(pQuery,i),
5694                                            SQLITE_STATIC);
5695            break;
5696          }
5697        }
5698      } /* End for */
5699      rc = sqlite3_step(pInsert);
5700      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5701        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5702                        sqlite3_errmsg(newDb));
5703      }
5704      sqlite3_reset(pInsert);
5705      cnt++;
5706      if( (cnt%spinRate)==0 ){
5707        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5708        fflush(stdout);
5709      }
5710    } /* End while */
5711    if( rc==SQLITE_DONE ) break;
5712    sqlite3_finalize(pQuery);
5713    sqlite3_free(zQuery);
5714    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5715                             zTable);
5716    shell_check_oom(zQuery);
5717    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5718    if( rc ){
5719      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5720      break;
5721    }
5722  } /* End for(k=0...) */
5723
5724end_data_xfer:
5725  sqlite3_finalize(pQuery);
5726  sqlite3_finalize(pInsert);
5727  sqlite3_free(zQuery);
5728  sqlite3_free(zInsert);
5729}
5730
5731
5732/*
5733** Try to transfer all rows of the schema that match zWhere.  For
5734** each row, invoke xForEach() on the object defined by that row.
5735** If an error is encountered while moving forward through the
5736** sqlite_schema table, try again moving backwards.
5737*/
5738static void tryToCloneSchema(
5739  ShellState *p,
5740  sqlite3 *newDb,
5741  const char *zWhere,
5742  void (*xForEach)(ShellState*,sqlite3*,const char*)
5743){
5744  sqlite3_stmt *pQuery = 0;
5745  char *zQuery = 0;
5746  int rc;
5747  const unsigned char *zName;
5748  const unsigned char *zSql;
5749  char *zErrMsg = 0;
5750
5751  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5752                           " WHERE %s", zWhere);
5753  shell_check_oom(zQuery);
5754  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5755  if( rc ){
5756    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5757                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5758                    zQuery);
5759    goto end_schema_xfer;
5760  }
5761  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5762    zName = sqlite3_column_text(pQuery, 0);
5763    zSql = sqlite3_column_text(pQuery, 1);
5764    if( zName==0 || zSql==0 ) continue;
5765    printf("%s... ", zName); fflush(stdout);
5766    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5767    if( zErrMsg ){
5768      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5769      sqlite3_free(zErrMsg);
5770      zErrMsg = 0;
5771    }
5772    if( xForEach ){
5773      xForEach(p, newDb, (const char*)zName);
5774    }
5775    printf("done\n");
5776  }
5777  if( rc!=SQLITE_DONE ){
5778    sqlite3_finalize(pQuery);
5779    sqlite3_free(zQuery);
5780    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5781                             " WHERE %s ORDER BY rowid DESC", zWhere);
5782    shell_check_oom(zQuery);
5783    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5784    if( rc ){
5785      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5786                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5787                      zQuery);
5788      goto end_schema_xfer;
5789    }
5790    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5791      zName = sqlite3_column_text(pQuery, 0);
5792      zSql = sqlite3_column_text(pQuery, 1);
5793      if( zName==0 || zSql==0 ) continue;
5794      printf("%s... ", zName); fflush(stdout);
5795      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5796      if( zErrMsg ){
5797        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5798        sqlite3_free(zErrMsg);
5799        zErrMsg = 0;
5800      }
5801      if( xForEach ){
5802        xForEach(p, newDb, (const char*)zName);
5803      }
5804      printf("done\n");
5805    }
5806  }
5807end_schema_xfer:
5808  sqlite3_finalize(pQuery);
5809  sqlite3_free(zQuery);
5810}
5811
5812/*
5813** Open a new database file named "zNewDb".  Try to recover as much information
5814** as possible out of the main database (which might be corrupt) and write it
5815** into zNewDb.
5816*/
5817static void tryToClone(ShellState *p, const char *zNewDb){
5818  int rc;
5819  sqlite3 *newDb = 0;
5820  if( access(zNewDb,0)==0 ){
5821    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5822    return;
5823  }
5824  rc = sqlite3_open(zNewDb, &newDb);
5825  if( rc ){
5826    utf8_printf(stderr, "Cannot create output database: %s\n",
5827            sqlite3_errmsg(newDb));
5828  }else{
5829    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5830    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5831    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5832    tryToCloneSchema(p, newDb, "type!='table'", 0);
5833    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5834    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5835  }
5836  close_db(newDb);
5837}
5838
5839/*
5840** Change the output file back to stdout.
5841**
5842** If the p->doXdgOpen flag is set, that means the output was being
5843** redirected to a temporary file named by p->zTempFile.  In that case,
5844** launch start/open/xdg-open on that temporary file.
5845*/
5846static void output_reset(ShellState *p){
5847  if( p->outfile[0]=='|' ){
5848#ifndef SQLITE_OMIT_POPEN
5849    pclose(p->out);
5850#endif
5851  }else{
5852    output_file_close(p->out);
5853#ifndef SQLITE_NOHAVE_SYSTEM
5854    if( p->doXdgOpen ){
5855      const char *zXdgOpenCmd =
5856#if defined(_WIN32)
5857      "start";
5858#elif defined(__APPLE__)
5859      "open";
5860#else
5861      "xdg-open";
5862#endif
5863      char *zCmd;
5864      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5865      if( system(zCmd) ){
5866        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5867      }else{
5868        /* Give the start/open/xdg-open command some time to get
5869        ** going before we continue, and potential delete the
5870        ** p->zTempFile data file out from under it */
5871        sqlite3_sleep(2000);
5872      }
5873      sqlite3_free(zCmd);
5874      outputModePop(p);
5875      p->doXdgOpen = 0;
5876    }
5877#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5878  }
5879  p->outfile[0] = 0;
5880  p->out = stdout;
5881}
5882
5883/*
5884** Run an SQL command and return the single integer result.
5885*/
5886static int db_int(sqlite3 *db, const char *zSql){
5887  sqlite3_stmt *pStmt;
5888  int res = 0;
5889  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
5890  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5891    res = sqlite3_column_int(pStmt,0);
5892  }
5893  sqlite3_finalize(pStmt);
5894  return res;
5895}
5896
5897#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5898/*
5899** Convert a 2-byte or 4-byte big-endian integer into a native integer
5900*/
5901static unsigned int get2byteInt(unsigned char *a){
5902  return (a[0]<<8) + a[1];
5903}
5904static unsigned int get4byteInt(unsigned char *a){
5905  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5906}
5907
5908/*
5909** Implementation of the ".dbinfo" command.
5910**
5911** Return 1 on error, 2 to exit, and 0 otherwise.
5912*/
5913static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5914  static const struct { const char *zName; int ofst; } aField[] = {
5915     { "file change counter:",  24  },
5916     { "database page count:",  28  },
5917     { "freelist page count:",  36  },
5918     { "schema cookie:",        40  },
5919     { "schema format:",        44  },
5920     { "default cache size:",   48  },
5921     { "autovacuum top root:",  52  },
5922     { "incremental vacuum:",   64  },
5923     { "text encoding:",        56  },
5924     { "user version:",         60  },
5925     { "application id:",       68  },
5926     { "software version:",     96  },
5927  };
5928  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5929     { "number of tables:",
5930       "SELECT count(*) FROM %s WHERE type='table'" },
5931     { "number of indexes:",
5932       "SELECT count(*) FROM %s WHERE type='index'" },
5933     { "number of triggers:",
5934       "SELECT count(*) FROM %s WHERE type='trigger'" },
5935     { "number of views:",
5936       "SELECT count(*) FROM %s WHERE type='view'" },
5937     { "schema size:",
5938       "SELECT total(length(sql)) FROM %s" },
5939  };
5940  int i, rc;
5941  unsigned iDataVersion;
5942  char *zSchemaTab;
5943  char *zDb = nArg>=2 ? azArg[1] : "main";
5944  sqlite3_stmt *pStmt = 0;
5945  unsigned char aHdr[100];
5946  open_db(p, 0);
5947  if( p->db==0 ) return 1;
5948  rc = sqlite3_prepare_v2(p->db,
5949             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5950             -1, &pStmt, 0);
5951  if( rc ){
5952    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5953    sqlite3_finalize(pStmt);
5954    return 1;
5955  }
5956  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5957  if( sqlite3_step(pStmt)==SQLITE_ROW
5958   && sqlite3_column_bytes(pStmt,0)>100
5959  ){
5960    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5961    sqlite3_finalize(pStmt);
5962  }else{
5963    raw_printf(stderr, "unable to read database header\n");
5964    sqlite3_finalize(pStmt);
5965    return 1;
5966  }
5967  i = get2byteInt(aHdr+16);
5968  if( i==1 ) i = 65536;
5969  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5970  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5971  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5972  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5973  for(i=0; i<ArraySize(aField); i++){
5974    int ofst = aField[i].ofst;
5975    unsigned int val = get4byteInt(aHdr + ofst);
5976    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5977    switch( ofst ){
5978      case 56: {
5979        if( val==1 ) raw_printf(p->out, " (utf8)");
5980        if( val==2 ) raw_printf(p->out, " (utf16le)");
5981        if( val==3 ) raw_printf(p->out, " (utf16be)");
5982      }
5983    }
5984    raw_printf(p->out, "\n");
5985  }
5986  if( zDb==0 ){
5987    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5988  }else if( cli_strcmp(zDb,"temp")==0 ){
5989    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5990  }else{
5991    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5992  }
5993  for(i=0; i<ArraySize(aQuery); i++){
5994    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5995    int val = db_int(p->db, zSql);
5996    sqlite3_free(zSql);
5997    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5998  }
5999  sqlite3_free(zSchemaTab);
6000  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
6001  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
6002  return 0;
6003}
6004#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
6005          && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
6006
6007/*
6008** Print the current sqlite3_errmsg() value to stderr and return 1.
6009*/
6010static int shellDatabaseError(sqlite3 *db){
6011  const char *zErr = sqlite3_errmsg(db);
6012  utf8_printf(stderr, "Error: %s\n", zErr);
6013  return 1;
6014}
6015
6016/*
6017** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
6018** if they match and FALSE (0) if they do not match.
6019**
6020** Globbing rules:
6021**
6022**      '*'       Matches any sequence of zero or more characters.
6023**
6024**      '?'       Matches exactly one character.
6025**
6026**     [...]      Matches one character from the enclosed list of
6027**                characters.
6028**
6029**     [^...]     Matches one character not in the enclosed list.
6030**
6031**      '#'       Matches any sequence of one or more digits with an
6032**                optional + or - sign in front
6033**
6034**      ' '       Any span of whitespace matches any other span of
6035**                whitespace.
6036**
6037** Extra whitespace at the end of z[] is ignored.
6038*/
6039static int testcase_glob(const char *zGlob, const char *z){
6040  int c, c2;
6041  int invert;
6042  int seen;
6043
6044  while( (c = (*(zGlob++)))!=0 ){
6045    if( IsSpace(c) ){
6046      if( !IsSpace(*z) ) return 0;
6047      while( IsSpace(*zGlob) ) zGlob++;
6048      while( IsSpace(*z) ) z++;
6049    }else if( c=='*' ){
6050      while( (c=(*(zGlob++))) == '*' || c=='?' ){
6051        if( c=='?' && (*(z++))==0 ) return 0;
6052      }
6053      if( c==0 ){
6054        return 1;
6055      }else if( c=='[' ){
6056        while( *z && testcase_glob(zGlob-1,z)==0 ){
6057          z++;
6058        }
6059        return (*z)!=0;
6060      }
6061      while( (c2 = (*(z++)))!=0 ){
6062        while( c2!=c ){
6063          c2 = *(z++);
6064          if( c2==0 ) return 0;
6065        }
6066        if( testcase_glob(zGlob,z) ) return 1;
6067      }
6068      return 0;
6069    }else if( c=='?' ){
6070      if( (*(z++))==0 ) return 0;
6071    }else if( c=='[' ){
6072      int prior_c = 0;
6073      seen = 0;
6074      invert = 0;
6075      c = *(z++);
6076      if( c==0 ) return 0;
6077      c2 = *(zGlob++);
6078      if( c2=='^' ){
6079        invert = 1;
6080        c2 = *(zGlob++);
6081      }
6082      if( c2==']' ){
6083        if( c==']' ) seen = 1;
6084        c2 = *(zGlob++);
6085      }
6086      while( c2 && c2!=']' ){
6087        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6088          c2 = *(zGlob++);
6089          if( c>=prior_c && c<=c2 ) seen = 1;
6090          prior_c = 0;
6091        }else{
6092          if( c==c2 ){
6093            seen = 1;
6094          }
6095          prior_c = c2;
6096        }
6097        c2 = *(zGlob++);
6098      }
6099      if( c2==0 || (seen ^ invert)==0 ) return 0;
6100    }else if( c=='#' ){
6101      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6102      if( !IsDigit(z[0]) ) return 0;
6103      z++;
6104      while( IsDigit(z[0]) ){ z++; }
6105    }else{
6106      if( c!=(*(z++)) ) return 0;
6107    }
6108  }
6109  while( IsSpace(*z) ){ z++; }
6110  return *z==0;
6111}
6112
6113
6114/*
6115** Compare the string as a command-line option with either one or two
6116** initial "-" characters.
6117*/
6118static int optionMatch(const char *zStr, const char *zOpt){
6119  if( zStr[0]!='-' ) return 0;
6120  zStr++;
6121  if( zStr[0]=='-' ) zStr++;
6122  return cli_strcmp(zStr, zOpt)==0;
6123}
6124
6125/*
6126** Delete a file.
6127*/
6128int shellDeleteFile(const char *zFilename){
6129  int rc;
6130#ifdef _WIN32
6131  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6132  rc = _wunlink(z);
6133  sqlite3_free(z);
6134#else
6135  rc = unlink(zFilename);
6136#endif
6137  return rc;
6138}
6139
6140/*
6141** Try to delete the temporary file (if there is one) and free the
6142** memory used to hold the name of the temp file.
6143*/
6144static void clearTempFile(ShellState *p){
6145  if( p->zTempFile==0 ) return;
6146  if( p->doXdgOpen ) return;
6147  if( shellDeleteFile(p->zTempFile) ) return;
6148  sqlite3_free(p->zTempFile);
6149  p->zTempFile = 0;
6150}
6151
6152/*
6153** Create a new temp file name with the given suffix.
6154*/
6155static void newTempFile(ShellState *p, const char *zSuffix){
6156  clearTempFile(p);
6157  sqlite3_free(p->zTempFile);
6158  p->zTempFile = 0;
6159  if( p->db ){
6160    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6161  }
6162  if( p->zTempFile==0 ){
6163    /* If p->db is an in-memory database then the TEMPFILENAME file-control
6164    ** will not work and we will need to fallback to guessing */
6165    char *zTemp;
6166    sqlite3_uint64 r;
6167    sqlite3_randomness(sizeof(r), &r);
6168    zTemp = getenv("TEMP");
6169    if( zTemp==0 ) zTemp = getenv("TMP");
6170    if( zTemp==0 ){
6171#ifdef _WIN32
6172      zTemp = "\\tmp";
6173#else
6174      zTemp = "/tmp";
6175#endif
6176    }
6177    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6178  }else{
6179    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6180  }
6181  shell_check_oom(p->zTempFile);
6182}
6183
6184
6185/*
6186** The implementation of SQL scalar function fkey_collate_clause(), used
6187** by the ".lint fkey-indexes" command. This scalar function is always
6188** called with four arguments - the parent table name, the parent column name,
6189** the child table name and the child column name.
6190**
6191**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6192**
6193** If either of the named tables or columns do not exist, this function
6194** returns an empty string. An empty string is also returned if both tables
6195** and columns exist but have the same default collation sequence. Or,
6196** if both exist but the default collation sequences are different, this
6197** function returns the string " COLLATE <parent-collation>", where
6198** <parent-collation> is the default collation sequence of the parent column.
6199*/
6200static void shellFkeyCollateClause(
6201  sqlite3_context *pCtx,
6202  int nVal,
6203  sqlite3_value **apVal
6204){
6205  sqlite3 *db = sqlite3_context_db_handle(pCtx);
6206  const char *zParent;
6207  const char *zParentCol;
6208  const char *zParentSeq;
6209  const char *zChild;
6210  const char *zChildCol;
6211  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
6212  int rc;
6213
6214  assert( nVal==4 );
6215  zParent = (const char*)sqlite3_value_text(apVal[0]);
6216  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6217  zChild = (const char*)sqlite3_value_text(apVal[2]);
6218  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6219
6220  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6221  rc = sqlite3_table_column_metadata(
6222      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6223  );
6224  if( rc==SQLITE_OK ){
6225    rc = sqlite3_table_column_metadata(
6226        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6227    );
6228  }
6229
6230  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6231    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6232    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6233    sqlite3_free(z);
6234  }
6235}
6236
6237
6238/*
6239** The implementation of dot-command ".lint fkey-indexes".
6240*/
6241static int lintFkeyIndexes(
6242  ShellState *pState,             /* Current shell tool state */
6243  char **azArg,                   /* Array of arguments passed to dot command */
6244  int nArg                        /* Number of entries in azArg[] */
6245){
6246  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
6247  FILE *out = pState->out;        /* Stream to write non-error output to */
6248  int bVerbose = 0;               /* If -verbose is present */
6249  int bGroupByParent = 0;         /* If -groupbyparent is present */
6250  int i;                          /* To iterate through azArg[] */
6251  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
6252  int rc;                         /* Return code */
6253  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
6254
6255  /*
6256  ** This SELECT statement returns one row for each foreign key constraint
6257  ** in the schema of the main database. The column values are:
6258  **
6259  ** 0. The text of an SQL statement similar to:
6260  **
6261  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6262  **
6263  **    This SELECT is similar to the one that the foreign keys implementation
6264  **    needs to run internally on child tables. If there is an index that can
6265  **    be used to optimize this query, then it can also be used by the FK
6266  **    implementation to optimize DELETE or UPDATE statements on the parent
6267  **    table.
6268  **
6269  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6270  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6271  **    contains an index that can be used to optimize the query.
6272  **
6273  ** 2. Human readable text that describes the child table and columns. e.g.
6274  **
6275  **       "child_table(child_key1, child_key2)"
6276  **
6277  ** 3. Human readable text that describes the parent table and columns. e.g.
6278  **
6279  **       "parent_table(parent_key1, parent_key2)"
6280  **
6281  ** 4. A full CREATE INDEX statement for an index that could be used to
6282  **    optimize DELETE or UPDATE statements on the parent table. e.g.
6283  **
6284  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
6285  **
6286  ** 5. The name of the parent table.
6287  **
6288  ** These six values are used by the C logic below to generate the report.
6289  */
6290  const char *zSql =
6291  "SELECT "
6292    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6293    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6294    "  || fkey_collate_clause("
6295    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6296    ", "
6297    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6298    "  || group_concat('*=?', ' AND ') || ')'"
6299    ", "
6300    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
6301    ", "
6302    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6303    ", "
6304    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6305    "  || ' ON ' || quote(s.name) || '('"
6306    "  || group_concat(quote(f.[from]) ||"
6307    "        fkey_collate_clause("
6308    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6309    "  || ');'"
6310    ", "
6311    "     f.[table] "
6312    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6313    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6314    "GROUP BY s.name, f.id "
6315    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6316  ;
6317  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6318
6319  for(i=2; i<nArg; i++){
6320    int n = strlen30(azArg[i]);
6321    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6322      bVerbose = 1;
6323    }
6324    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6325      bGroupByParent = 1;
6326      zIndent = "    ";
6327    }
6328    else{
6329      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6330          azArg[0], azArg[1]
6331      );
6332      return SQLITE_ERROR;
6333    }
6334  }
6335
6336  /* Register the fkey_collate_clause() SQL function */
6337  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6338      0, shellFkeyCollateClause, 0, 0
6339  );
6340
6341
6342  if( rc==SQLITE_OK ){
6343    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6344  }
6345  if( rc==SQLITE_OK ){
6346    sqlite3_bind_int(pSql, 1, bGroupByParent);
6347  }
6348
6349  if( rc==SQLITE_OK ){
6350    int rc2;
6351    char *zPrev = 0;
6352    while( SQLITE_ROW==sqlite3_step(pSql) ){
6353      int res = -1;
6354      sqlite3_stmt *pExplain = 0;
6355      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6356      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6357      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6358      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6359      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6360      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6361
6362      if( zEQP==0 ) continue;
6363      if( zGlob==0 ) continue;
6364      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6365      if( rc!=SQLITE_OK ) break;
6366      if( SQLITE_ROW==sqlite3_step(pExplain) ){
6367        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6368        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
6369                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
6370      }
6371      rc = sqlite3_finalize(pExplain);
6372      if( rc!=SQLITE_OK ) break;
6373
6374      if( res<0 ){
6375        raw_printf(stderr, "Error: internal error");
6376        break;
6377      }else{
6378        if( bGroupByParent
6379        && (bVerbose || res==0)
6380        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6381        ){
6382          raw_printf(out, "-- Parent table %s\n", zParent);
6383          sqlite3_free(zPrev);
6384          zPrev = sqlite3_mprintf("%s", zParent);
6385        }
6386
6387        if( res==0 ){
6388          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6389        }else if( bVerbose ){
6390          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6391              zIndent, zFrom, zTarget
6392          );
6393        }
6394      }
6395    }
6396    sqlite3_free(zPrev);
6397
6398    if( rc!=SQLITE_OK ){
6399      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6400    }
6401
6402    rc2 = sqlite3_finalize(pSql);
6403    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6404      rc = rc2;
6405      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6406    }
6407  }else{
6408    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6409  }
6410
6411  return rc;
6412}
6413
6414/*
6415** Implementation of ".lint" dot command.
6416*/
6417static int lintDotCommand(
6418  ShellState *pState,             /* Current shell tool state */
6419  char **azArg,                   /* Array of arguments passed to dot command */
6420  int nArg                        /* Number of entries in azArg[] */
6421){
6422  int n;
6423  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6424  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6425  return lintFkeyIndexes(pState, azArg, nArg);
6426
6427 usage:
6428  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6429  raw_printf(stderr, "Where sub-commands are:\n");
6430  raw_printf(stderr, "    fkey-indexes\n");
6431  return SQLITE_ERROR;
6432}
6433
6434#if !defined SQLITE_OMIT_VIRTUALTABLE
6435static void shellPrepare(
6436  sqlite3 *db,
6437  int *pRc,
6438  const char *zSql,
6439  sqlite3_stmt **ppStmt
6440){
6441  *ppStmt = 0;
6442  if( *pRc==SQLITE_OK ){
6443    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6444    if( rc!=SQLITE_OK ){
6445      raw_printf(stderr, "sql error: %s (%d)\n",
6446          sqlite3_errmsg(db), sqlite3_errcode(db)
6447      );
6448      *pRc = rc;
6449    }
6450  }
6451}
6452
6453/*
6454** Create a prepared statement using printf-style arguments for the SQL.
6455**
6456** This routine is could be marked "static".  But it is not always used,
6457** depending on compile-time options.  By omitting the "static", we avoid
6458** nuisance compiler warnings about "defined but not used".
6459*/
6460void shellPreparePrintf(
6461  sqlite3 *db,
6462  int *pRc,
6463  sqlite3_stmt **ppStmt,
6464  const char *zFmt,
6465  ...
6466){
6467  *ppStmt = 0;
6468  if( *pRc==SQLITE_OK ){
6469    va_list ap;
6470    char *z;
6471    va_start(ap, zFmt);
6472    z = sqlite3_vmprintf(zFmt, ap);
6473    va_end(ap);
6474    if( z==0 ){
6475      *pRc = SQLITE_NOMEM;
6476    }else{
6477      shellPrepare(db, pRc, z, ppStmt);
6478      sqlite3_free(z);
6479    }
6480  }
6481}
6482
6483/* Finalize the prepared statement created using shellPreparePrintf().
6484**
6485** This routine is could be marked "static".  But it is not always used,
6486** depending on compile-time options.  By omitting the "static", we avoid
6487** nuisance compiler warnings about "defined but not used".
6488*/
6489void shellFinalize(
6490  int *pRc,
6491  sqlite3_stmt *pStmt
6492){
6493  if( pStmt ){
6494    sqlite3 *db = sqlite3_db_handle(pStmt);
6495    int rc = sqlite3_finalize(pStmt);
6496    if( *pRc==SQLITE_OK ){
6497      if( rc!=SQLITE_OK ){
6498        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6499      }
6500      *pRc = rc;
6501    }
6502  }
6503}
6504
6505/* Reset the prepared statement created using shellPreparePrintf().
6506**
6507** This routine is could be marked "static".  But it is not always used,
6508** depending on compile-time options.  By omitting the "static", we avoid
6509** nuisance compiler warnings about "defined but not used".
6510*/
6511void shellReset(
6512  int *pRc,
6513  sqlite3_stmt *pStmt
6514){
6515  int rc = sqlite3_reset(pStmt);
6516  if( *pRc==SQLITE_OK ){
6517    if( rc!=SQLITE_OK ){
6518      sqlite3 *db = sqlite3_db_handle(pStmt);
6519      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6520    }
6521    *pRc = rc;
6522  }
6523}
6524#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6525
6526#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6527/******************************************************************************
6528** The ".archive" or ".ar" command.
6529*/
6530/*
6531** Structure representing a single ".ar" command.
6532*/
6533typedef struct ArCommand ArCommand;
6534struct ArCommand {
6535  u8 eCmd;                        /* An AR_CMD_* value */
6536  u8 bVerbose;                    /* True if --verbose */
6537  u8 bZip;                        /* True if the archive is a ZIP */
6538  u8 bDryRun;                     /* True if --dry-run */
6539  u8 bAppend;                     /* True if --append */
6540  u8 bGlob;                       /* True if --glob */
6541  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6542  int nArg;                       /* Number of command arguments */
6543  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6544  const char *zFile;              /* --file argument, or NULL */
6545  const char *zDir;               /* --directory argument, or NULL */
6546  char **azArg;                   /* Array of command arguments */
6547  ShellState *p;                  /* Shell state */
6548  sqlite3 *db;                    /* Database containing the archive */
6549};
6550
6551/*
6552** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6553*/
6554static int arUsage(FILE *f){
6555  showHelp(f,"archive");
6556  return SQLITE_ERROR;
6557}
6558
6559/*
6560** Print an error message for the .ar command to stderr and return
6561** SQLITE_ERROR.
6562*/
6563static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6564  va_list ap;
6565  char *z;
6566  va_start(ap, zFmt);
6567  z = sqlite3_vmprintf(zFmt, ap);
6568  va_end(ap);
6569  utf8_printf(stderr, "Error: %s\n", z);
6570  if( pAr->fromCmdLine ){
6571    utf8_printf(stderr, "Use \"-A\" for more help\n");
6572  }else{
6573    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6574  }
6575  sqlite3_free(z);
6576  return SQLITE_ERROR;
6577}
6578
6579/*
6580** Values for ArCommand.eCmd.
6581*/
6582#define AR_CMD_CREATE       1
6583#define AR_CMD_UPDATE       2
6584#define AR_CMD_INSERT       3
6585#define AR_CMD_EXTRACT      4
6586#define AR_CMD_LIST         5
6587#define AR_CMD_HELP         6
6588#define AR_CMD_REMOVE       7
6589
6590/*
6591** Other (non-command) switches.
6592*/
6593#define AR_SWITCH_VERBOSE     8
6594#define AR_SWITCH_FILE        9
6595#define AR_SWITCH_DIRECTORY  10
6596#define AR_SWITCH_APPEND     11
6597#define AR_SWITCH_DRYRUN     12
6598#define AR_SWITCH_GLOB       13
6599
6600static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6601  switch( eSwitch ){
6602    case AR_CMD_CREATE:
6603    case AR_CMD_EXTRACT:
6604    case AR_CMD_LIST:
6605    case AR_CMD_REMOVE:
6606    case AR_CMD_UPDATE:
6607    case AR_CMD_INSERT:
6608    case AR_CMD_HELP:
6609      if( pAr->eCmd ){
6610        return arErrorMsg(pAr, "multiple command options");
6611      }
6612      pAr->eCmd = eSwitch;
6613      break;
6614
6615    case AR_SWITCH_DRYRUN:
6616      pAr->bDryRun = 1;
6617      break;
6618    case AR_SWITCH_GLOB:
6619      pAr->bGlob = 1;
6620      break;
6621    case AR_SWITCH_VERBOSE:
6622      pAr->bVerbose = 1;
6623      break;
6624    case AR_SWITCH_APPEND:
6625      pAr->bAppend = 1;
6626      /* Fall thru into --file */
6627    case AR_SWITCH_FILE:
6628      pAr->zFile = zArg;
6629      break;
6630    case AR_SWITCH_DIRECTORY:
6631      pAr->zDir = zArg;
6632      break;
6633  }
6634
6635  return SQLITE_OK;
6636}
6637
6638/*
6639** Parse the command line for an ".ar" command. The results are written into
6640** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6641** successfully, otherwise an error message is written to stderr and
6642** SQLITE_ERROR returned.
6643*/
6644static int arParseCommand(
6645  char **azArg,                   /* Array of arguments passed to dot command */
6646  int nArg,                       /* Number of entries in azArg[] */
6647  ArCommand *pAr                  /* Populate this object */
6648){
6649  struct ArSwitch {
6650    const char *zLong;
6651    char cShort;
6652    u8 eSwitch;
6653    u8 bArg;
6654  } aSwitch[] = {
6655    { "create",    'c', AR_CMD_CREATE,       0 },
6656    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6657    { "insert",    'i', AR_CMD_INSERT,       0 },
6658    { "list",      't', AR_CMD_LIST,         0 },
6659    { "remove",    'r', AR_CMD_REMOVE,       0 },
6660    { "update",    'u', AR_CMD_UPDATE,       0 },
6661    { "help",      'h', AR_CMD_HELP,         0 },
6662    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6663    { "file",      'f', AR_SWITCH_FILE,      1 },
6664    { "append",    'a', AR_SWITCH_APPEND,    1 },
6665    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6666    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6667    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6668  };
6669  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6670  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6671
6672  if( nArg<=1 ){
6673    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6674    return arUsage(stderr);
6675  }else{
6676    char *z = azArg[1];
6677    if( z[0]!='-' ){
6678      /* Traditional style [tar] invocation */
6679      int i;
6680      int iArg = 2;
6681      for(i=0; z[i]; i++){
6682        const char *zArg = 0;
6683        struct ArSwitch *pOpt;
6684        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6685          if( z[i]==pOpt->cShort ) break;
6686        }
6687        if( pOpt==pEnd ){
6688          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6689        }
6690        if( pOpt->bArg ){
6691          if( iArg>=nArg ){
6692            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6693          }
6694          zArg = azArg[iArg++];
6695        }
6696        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6697      }
6698      pAr->nArg = nArg-iArg;
6699      if( pAr->nArg>0 ){
6700        pAr->azArg = &azArg[iArg];
6701      }
6702    }else{
6703      /* Non-traditional invocation */
6704      int iArg;
6705      for(iArg=1; iArg<nArg; iArg++){
6706        int n;
6707        z = azArg[iArg];
6708        if( z[0]!='-' ){
6709          /* All remaining command line words are command arguments. */
6710          pAr->azArg = &azArg[iArg];
6711          pAr->nArg = nArg-iArg;
6712          break;
6713        }
6714        n = strlen30(z);
6715
6716        if( z[1]!='-' ){
6717          int i;
6718          /* One or more short options */
6719          for(i=1; i<n; i++){
6720            const char *zArg = 0;
6721            struct ArSwitch *pOpt;
6722            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6723              if( z[i]==pOpt->cShort ) break;
6724            }
6725            if( pOpt==pEnd ){
6726              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6727            }
6728            if( pOpt->bArg ){
6729              if( i<(n-1) ){
6730                zArg = &z[i+1];
6731                i = n;
6732              }else{
6733                if( iArg>=(nArg-1) ){
6734                  return arErrorMsg(pAr, "option requires an argument: %c",
6735                                    z[i]);
6736                }
6737                zArg = azArg[++iArg];
6738              }
6739            }
6740            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6741          }
6742        }else if( z[2]=='\0' ){
6743          /* A -- option, indicating that all remaining command line words
6744          ** are command arguments.  */
6745          pAr->azArg = &azArg[iArg+1];
6746          pAr->nArg = nArg-iArg-1;
6747          break;
6748        }else{
6749          /* A long option */
6750          const char *zArg = 0;             /* Argument for option, if any */
6751          struct ArSwitch *pMatch = 0;      /* Matching option */
6752          struct ArSwitch *pOpt;            /* Iterator */
6753          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6754            const char *zLong = pOpt->zLong;
6755            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6756              if( pMatch ){
6757                return arErrorMsg(pAr, "ambiguous option: %s",z);
6758              }else{
6759                pMatch = pOpt;
6760              }
6761            }
6762          }
6763
6764          if( pMatch==0 ){
6765            return arErrorMsg(pAr, "unrecognized option: %s", z);
6766          }
6767          if( pMatch->bArg ){
6768            if( iArg>=(nArg-1) ){
6769              return arErrorMsg(pAr, "option requires an argument: %s", z);
6770            }
6771            zArg = azArg[++iArg];
6772          }
6773          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6774        }
6775      }
6776    }
6777  }
6778
6779  return SQLITE_OK;
6780}
6781
6782/*
6783** This function assumes that all arguments within the ArCommand.azArg[]
6784** array refer to archive members, as for the --extract, --list or --remove
6785** commands. It checks that each of them are "present". If any specified
6786** file is not present in the archive, an error is printed to stderr and an
6787** error code returned. Otherwise, if all specified arguments are present
6788** in the archive, SQLITE_OK is returned. Here, "present" means either an
6789** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6790** when pAr->bGlob is true.
6791**
6792** This function strips any trailing '/' characters from each argument.
6793** This is consistent with the way the [tar] command seems to work on
6794** Linux.
6795*/
6796static int arCheckEntries(ArCommand *pAr){
6797  int rc = SQLITE_OK;
6798  if( pAr->nArg ){
6799    int i, j;
6800    sqlite3_stmt *pTest = 0;
6801    const char *zSel = (pAr->bGlob)
6802      ? "SELECT name FROM %s WHERE glob($name,name)"
6803      : "SELECT name FROM %s WHERE name=$name";
6804
6805    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6806    j = sqlite3_bind_parameter_index(pTest, "$name");
6807    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6808      char *z = pAr->azArg[i];
6809      int n = strlen30(z);
6810      int bOk = 0;
6811      while( n>0 && z[n-1]=='/' ) n--;
6812      z[n] = '\0';
6813      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6814      if( SQLITE_ROW==sqlite3_step(pTest) ){
6815        bOk = 1;
6816      }
6817      shellReset(&rc, pTest);
6818      if( rc==SQLITE_OK && bOk==0 ){
6819        utf8_printf(stderr, "not found in archive: %s\n", z);
6820        rc = SQLITE_ERROR;
6821      }
6822    }
6823    shellFinalize(&rc, pTest);
6824  }
6825  return rc;
6826}
6827
6828/*
6829** Format a WHERE clause that can be used against the "sqlar" table to
6830** identify all archive members that match the command arguments held
6831** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6832** The caller is responsible for eventually calling sqlite3_free() on
6833** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6834** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6835*/
6836static void arWhereClause(
6837  int *pRc,
6838  ArCommand *pAr,
6839  char **pzWhere                  /* OUT: New WHERE clause */
6840){
6841  char *zWhere = 0;
6842  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6843  if( *pRc==SQLITE_OK ){
6844    if( pAr->nArg==0 ){
6845      zWhere = sqlite3_mprintf("1");
6846    }else{
6847      int i;
6848      const char *zSep = "";
6849      for(i=0; i<pAr->nArg; i++){
6850        const char *z = pAr->azArg[i];
6851        zWhere = sqlite3_mprintf(
6852          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6853          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6854        );
6855        if( zWhere==0 ){
6856          *pRc = SQLITE_NOMEM;
6857          break;
6858        }
6859        zSep = " OR ";
6860      }
6861    }
6862  }
6863  *pzWhere = zWhere;
6864}
6865
6866/*
6867** Implementation of .ar "lisT" command.
6868*/
6869static int arListCommand(ArCommand *pAr){
6870  const char *zSql = "SELECT %s FROM %s WHERE %s";
6871  const char *azCols[] = {
6872    "name",
6873    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6874  };
6875
6876  char *zWhere = 0;
6877  sqlite3_stmt *pSql = 0;
6878  int rc;
6879
6880  rc = arCheckEntries(pAr);
6881  arWhereClause(&rc, pAr, &zWhere);
6882
6883  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6884                     pAr->zSrcTable, zWhere);
6885  if( pAr->bDryRun ){
6886    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6887  }else{
6888    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6889      if( pAr->bVerbose ){
6890        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6891            sqlite3_column_text(pSql, 0),
6892            sqlite3_column_int(pSql, 1),
6893            sqlite3_column_text(pSql, 2),
6894            sqlite3_column_text(pSql, 3)
6895        );
6896      }else{
6897        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6898      }
6899    }
6900  }
6901  shellFinalize(&rc, pSql);
6902  sqlite3_free(zWhere);
6903  return rc;
6904}
6905
6906
6907/*
6908** Implementation of .ar "Remove" command.
6909*/
6910static int arRemoveCommand(ArCommand *pAr){
6911  int rc = 0;
6912  char *zSql = 0;
6913  char *zWhere = 0;
6914
6915  if( pAr->nArg ){
6916    /* Verify that args actually exist within the archive before proceeding.
6917    ** And formulate a WHERE clause to match them.  */
6918    rc = arCheckEntries(pAr);
6919    arWhereClause(&rc, pAr, &zWhere);
6920  }
6921  if( rc==SQLITE_OK ){
6922    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6923                           pAr->zSrcTable, zWhere);
6924    if( pAr->bDryRun ){
6925      utf8_printf(pAr->p->out, "%s\n", zSql);
6926    }else{
6927      char *zErr = 0;
6928      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6929      if( rc==SQLITE_OK ){
6930        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6931        if( rc!=SQLITE_OK ){
6932          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6933        }else{
6934          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6935        }
6936      }
6937      if( zErr ){
6938        utf8_printf(stdout, "ERROR: %s\n", zErr);
6939        sqlite3_free(zErr);
6940      }
6941    }
6942  }
6943  sqlite3_free(zWhere);
6944  sqlite3_free(zSql);
6945  return rc;
6946}
6947
6948/*
6949** Implementation of .ar "eXtract" command.
6950*/
6951static int arExtractCommand(ArCommand *pAr){
6952  const char *zSql1 =
6953    "SELECT "
6954    " ($dir || name),"
6955    " writefile(($dir || name), %s, mode, mtime) "
6956    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6957    " AND name NOT GLOB '*..[/\\]*'";
6958
6959  const char *azExtraArg[] = {
6960    "sqlar_uncompress(data, sz)",
6961    "data"
6962  };
6963
6964  sqlite3_stmt *pSql = 0;
6965  int rc = SQLITE_OK;
6966  char *zDir = 0;
6967  char *zWhere = 0;
6968  int i, j;
6969
6970  /* If arguments are specified, check that they actually exist within
6971  ** the archive before proceeding. And formulate a WHERE clause to
6972  ** match them.  */
6973  rc = arCheckEntries(pAr);
6974  arWhereClause(&rc, pAr, &zWhere);
6975
6976  if( rc==SQLITE_OK ){
6977    if( pAr->zDir ){
6978      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6979    }else{
6980      zDir = sqlite3_mprintf("");
6981    }
6982    if( zDir==0 ) rc = SQLITE_NOMEM;
6983  }
6984
6985  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6986      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6987  );
6988
6989  if( rc==SQLITE_OK ){
6990    j = sqlite3_bind_parameter_index(pSql, "$dir");
6991    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6992
6993    /* Run the SELECT statement twice. The first time, writefile() is called
6994    ** for all archive members that should be extracted. The second time,
6995    ** only for the directories. This is because the timestamps for
6996    ** extracted directories must be reset after they are populated (as
6997    ** populating them changes the timestamp).  */
6998    for(i=0; i<2; i++){
6999      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
7000      sqlite3_bind_int(pSql, j, i);
7001      if( pAr->bDryRun ){
7002        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
7003      }else{
7004        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7005          if( i==0 && pAr->bVerbose ){
7006            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
7007          }
7008        }
7009      }
7010      shellReset(&rc, pSql);
7011    }
7012    shellFinalize(&rc, pSql);
7013  }
7014
7015  sqlite3_free(zDir);
7016  sqlite3_free(zWhere);
7017  return rc;
7018}
7019
7020/*
7021** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
7022*/
7023static int arExecSql(ArCommand *pAr, const char *zSql){
7024  int rc;
7025  if( pAr->bDryRun ){
7026    utf8_printf(pAr->p->out, "%s\n", zSql);
7027    rc = SQLITE_OK;
7028  }else{
7029    char *zErr = 0;
7030    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7031    if( zErr ){
7032      utf8_printf(stdout, "ERROR: %s\n", zErr);
7033      sqlite3_free(zErr);
7034    }
7035  }
7036  return rc;
7037}
7038
7039
7040/*
7041** Implementation of .ar "create", "insert", and "update" commands.
7042**
7043**     create    ->     Create a new SQL archive
7044**     insert    ->     Insert or reinsert all files listed
7045**     update    ->     Insert files that have changed or that were not
7046**                      previously in the archive
7047**
7048** Create the "sqlar" table in the database if it does not already exist.
7049** Then add each file in the azFile[] array to the archive. Directories
7050** are added recursively. If argument bVerbose is non-zero, a message is
7051** printed on stdout for each file archived.
7052**
7053** The create command is the same as update, except that it drops
7054** any existing "sqlar" table before beginning.  The "insert" command
7055** always overwrites every file named on the command-line, where as
7056** "update" only overwrites if the size or mtime or mode has changed.
7057*/
7058static int arCreateOrUpdateCommand(
7059  ArCommand *pAr,                 /* Command arguments and options */
7060  int bUpdate,                    /* true for a --create. */
7061  int bOnlyIfChanged              /* Only update if file has changed */
7062){
7063  const char *zCreate =
7064      "CREATE TABLE IF NOT EXISTS sqlar(\n"
7065      "  name TEXT PRIMARY KEY,  -- name of the file\n"
7066      "  mode INT,               -- access permissions\n"
7067      "  mtime INT,              -- last modification time\n"
7068      "  sz INT,                 -- original file size\n"
7069      "  data BLOB               -- compressed content\n"
7070      ")";
7071  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7072  const char *zInsertFmt[2] = {
7073     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7074     "  SELECT\n"
7075     "    %s,\n"
7076     "    mode,\n"
7077     "    mtime,\n"
7078     "    CASE substr(lsmode(mode),1,1)\n"
7079     "      WHEN '-' THEN length(data)\n"
7080     "      WHEN 'd' THEN 0\n"
7081     "      ELSE -1 END,\n"
7082     "    sqlar_compress(data)\n"
7083     "  FROM fsdir(%Q,%Q) AS disk\n"
7084     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7085     ,
7086     "REPLACE INTO %s(name,mode,mtime,data)\n"
7087     "  SELECT\n"
7088     "    %s,\n"
7089     "    mode,\n"
7090     "    mtime,\n"
7091     "    data\n"
7092     "  FROM fsdir(%Q,%Q) AS disk\n"
7093     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7094  };
7095  int i;                          /* For iterating through azFile[] */
7096  int rc;                         /* Return code */
7097  const char *zTab = 0;           /* SQL table into which to insert */
7098  char *zSql;
7099  char zTemp[50];
7100  char *zExists = 0;
7101
7102  arExecSql(pAr, "PRAGMA page_size=512");
7103  rc = arExecSql(pAr, "SAVEPOINT ar;");
7104  if( rc!=SQLITE_OK ) return rc;
7105  zTemp[0] = 0;
7106  if( pAr->bZip ){
7107    /* Initialize the zipfile virtual table, if necessary */
7108    if( pAr->zFile ){
7109      sqlite3_uint64 r;
7110      sqlite3_randomness(sizeof(r),&r);
7111      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7112      zTab = zTemp;
7113      zSql = sqlite3_mprintf(
7114         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7115         zTab, pAr->zFile
7116      );
7117      rc = arExecSql(pAr, zSql);
7118      sqlite3_free(zSql);
7119    }else{
7120      zTab = "zip";
7121    }
7122  }else{
7123    /* Initialize the table for an SQLAR */
7124    zTab = "sqlar";
7125    if( bUpdate==0 ){
7126      rc = arExecSql(pAr, zDrop);
7127      if( rc!=SQLITE_OK ) goto end_ar_transaction;
7128    }
7129    rc = arExecSql(pAr, zCreate);
7130  }
7131  if( bOnlyIfChanged ){
7132    zExists = sqlite3_mprintf(
7133      " AND NOT EXISTS("
7134          "SELECT 1 FROM %s AS mem"
7135          " WHERE mem.name=disk.name"
7136          " AND mem.mtime=disk.mtime"
7137          " AND mem.mode=disk.mode)", zTab);
7138  }else{
7139    zExists = sqlite3_mprintf("");
7140  }
7141  if( zExists==0 ) rc = SQLITE_NOMEM;
7142  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7143    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7144        pAr->bVerbose ? "shell_putsnl(name)" : "name",
7145        pAr->azArg[i], pAr->zDir, zExists);
7146    rc = arExecSql(pAr, zSql2);
7147    sqlite3_free(zSql2);
7148  }
7149end_ar_transaction:
7150  if( rc!=SQLITE_OK ){
7151    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7152  }else{
7153    rc = arExecSql(pAr, "RELEASE ar;");
7154    if( pAr->bZip && pAr->zFile ){
7155      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7156      arExecSql(pAr, zSql);
7157      sqlite3_free(zSql);
7158    }
7159  }
7160  sqlite3_free(zExists);
7161  return rc;
7162}
7163
7164/*
7165** Implementation of ".ar" dot command.
7166*/
7167static int arDotCommand(
7168  ShellState *pState,          /* Current shell tool state */
7169  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
7170  char **azArg,                /* Array of arguments passed to dot command */
7171  int nArg                     /* Number of entries in azArg[] */
7172){
7173  ArCommand cmd;
7174  int rc;
7175  memset(&cmd, 0, sizeof(cmd));
7176  cmd.fromCmdLine = fromCmdLine;
7177  rc = arParseCommand(azArg, nArg, &cmd);
7178  if( rc==SQLITE_OK ){
7179    int eDbType = SHELL_OPEN_UNSPEC;
7180    cmd.p = pState;
7181    cmd.db = pState->db;
7182    if( cmd.zFile ){
7183      eDbType = deduceDatabaseType(cmd.zFile, 1);
7184    }else{
7185      eDbType = pState->openMode;
7186    }
7187    if( eDbType==SHELL_OPEN_ZIPFILE ){
7188      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7189        if( cmd.zFile==0 ){
7190          cmd.zSrcTable = sqlite3_mprintf("zip");
7191        }else{
7192          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7193        }
7194      }
7195      cmd.bZip = 1;
7196    }else if( cmd.zFile ){
7197      int flags;
7198      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7199      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7200           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7201        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7202      }else{
7203        flags = SQLITE_OPEN_READONLY;
7204      }
7205      cmd.db = 0;
7206      if( cmd.bDryRun ){
7207        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7208             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7209      }
7210      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7211             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7212      if( rc!=SQLITE_OK ){
7213        utf8_printf(stderr, "cannot open file: %s (%s)\n",
7214            cmd.zFile, sqlite3_errmsg(cmd.db)
7215        );
7216        goto end_ar_command;
7217      }
7218      sqlite3_fileio_init(cmd.db, 0, 0);
7219      sqlite3_sqlar_init(cmd.db, 0, 0);
7220      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7221                              shellPutsFunc, 0, 0);
7222
7223    }
7224    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7225      if( cmd.eCmd!=AR_CMD_CREATE
7226       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7227      ){
7228        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7229        rc = SQLITE_ERROR;
7230        goto end_ar_command;
7231      }
7232      cmd.zSrcTable = sqlite3_mprintf("sqlar");
7233    }
7234
7235    switch( cmd.eCmd ){
7236      case AR_CMD_CREATE:
7237        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7238        break;
7239
7240      case AR_CMD_EXTRACT:
7241        rc = arExtractCommand(&cmd);
7242        break;
7243
7244      case AR_CMD_LIST:
7245        rc = arListCommand(&cmd);
7246        break;
7247
7248      case AR_CMD_HELP:
7249        arUsage(pState->out);
7250        break;
7251
7252      case AR_CMD_INSERT:
7253        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7254        break;
7255
7256      case AR_CMD_REMOVE:
7257        rc = arRemoveCommand(&cmd);
7258        break;
7259
7260      default:
7261        assert( cmd.eCmd==AR_CMD_UPDATE );
7262        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7263        break;
7264    }
7265  }
7266end_ar_command:
7267  if( cmd.db!=pState->db ){
7268    close_db(cmd.db);
7269  }
7270  sqlite3_free(cmd.zSrcTable);
7271
7272  return rc;
7273}
7274/* End of the ".archive" or ".ar" command logic
7275*******************************************************************************/
7276#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7277
7278#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7279/*
7280** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
7281** Otherwise, the SQL statement or statements in zSql are executed using
7282** database connection db and the error code written to *pRc before
7283** this function returns.
7284*/
7285static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
7286  int rc = *pRc;
7287  if( rc==SQLITE_OK ){
7288    char *zErr = 0;
7289    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
7290    if( rc!=SQLITE_OK ){
7291      raw_printf(stderr, "SQL error: %s\n", zErr);
7292    }
7293    sqlite3_free(zErr);
7294    *pRc = rc;
7295  }
7296}
7297
7298/*
7299** Like shellExec(), except that zFmt is a printf() style format string.
7300*/
7301static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
7302  char *z = 0;
7303  if( *pRc==SQLITE_OK ){
7304    va_list ap;
7305    va_start(ap, zFmt);
7306    z = sqlite3_vmprintf(zFmt, ap);
7307    va_end(ap);
7308    if( z==0 ){
7309      *pRc = SQLITE_NOMEM;
7310    }else{
7311      shellExec(db, pRc, z);
7312    }
7313    sqlite3_free(z);
7314  }
7315}
7316
7317/*
7318** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7319** Otherwise, an attempt is made to allocate, zero and return a pointer
7320** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
7321** to SQLITE_NOMEM and NULL returned.
7322*/
7323static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
7324  void *pRet = 0;
7325  if( *pRc==SQLITE_OK ){
7326    pRet = sqlite3_malloc64(nByte);
7327    if( pRet==0 ){
7328      *pRc = SQLITE_NOMEM;
7329    }else{
7330      memset(pRet, 0, nByte);
7331    }
7332  }
7333  return pRet;
7334}
7335
7336/*
7337** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7338** Otherwise, zFmt is treated as a printf() style string. The result of
7339** formatting it along with any trailing arguments is written into a
7340** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
7341** It is the responsibility of the caller to eventually free this buffer
7342** using a call to sqlite3_free().
7343**
7344** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
7345** pointer returned.
7346*/
7347static char *shellMPrintf(int *pRc, const char *zFmt, ...){
7348  char *z = 0;
7349  if( *pRc==SQLITE_OK ){
7350    va_list ap;
7351    va_start(ap, zFmt);
7352    z = sqlite3_vmprintf(zFmt, ap);
7353    va_end(ap);
7354    if( z==0 ){
7355      *pRc = SQLITE_NOMEM;
7356    }
7357  }
7358  return z;
7359}
7360
7361
7362/*
7363** When running the ".recover" command, each output table, and the special
7364** orphaned row table if it is required, is represented by an instance
7365** of the following struct.
7366*/
7367typedef struct RecoverTable RecoverTable;
7368struct RecoverTable {
7369  char *zQuoted;                  /* Quoted version of table name */
7370  int nCol;                       /* Number of columns in table */
7371  char **azlCol;                  /* Array of column lists */
7372  int iPk;                        /* Index of IPK column */
7373};
7374
7375/*
7376** Free a RecoverTable object allocated by recoverFindTable() or
7377** recoverOrphanTable().
7378*/
7379static void recoverFreeTable(RecoverTable *pTab){
7380  if( pTab ){
7381    sqlite3_free(pTab->zQuoted);
7382    if( pTab->azlCol ){
7383      int i;
7384      for(i=0; i<=pTab->nCol; i++){
7385        sqlite3_free(pTab->azlCol[i]);
7386      }
7387      sqlite3_free(pTab->azlCol);
7388    }
7389    sqlite3_free(pTab);
7390  }
7391}
7392
7393/*
7394** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
7395** Otherwise, it allocates and returns a RecoverTable object based on the
7396** final four arguments passed to this function. It is the responsibility
7397** of the caller to eventually free the returned object using
7398** recoverFreeTable().
7399*/
7400static RecoverTable *recoverNewTable(
7401  int *pRc,                       /* IN/OUT: Error code */
7402  const char *zName,              /* Name of table */
7403  const char *zSql,               /* CREATE TABLE statement */
7404  int bIntkey,
7405  int nCol
7406){
7407  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
7408  int rc = *pRc;
7409  RecoverTable *pTab = 0;
7410
7411  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
7412  if( rc==SQLITE_OK ){
7413    int nSqlCol = 0;
7414    int bSqlIntkey = 0;
7415    sqlite3_stmt *pStmt = 0;
7416
7417    rc = sqlite3_open("", &dbtmp);
7418    if( rc==SQLITE_OK ){
7419      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
7420                              shellIdQuote, 0, 0);
7421    }
7422    if( rc==SQLITE_OK ){
7423      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
7424    }
7425    if( rc==SQLITE_OK ){
7426      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
7427      if( rc==SQLITE_ERROR ){
7428        rc = SQLITE_OK;
7429        goto finished;
7430      }
7431    }
7432    shellPreparePrintf(dbtmp, &rc, &pStmt,
7433        "SELECT count(*) FROM pragma_table_info(%Q)", zName
7434    );
7435    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7436      nSqlCol = sqlite3_column_int(pStmt, 0);
7437    }
7438    shellFinalize(&rc, pStmt);
7439
7440    if( rc!=SQLITE_OK || nSqlCol<nCol ){
7441      goto finished;
7442    }
7443
7444    shellPreparePrintf(dbtmp, &rc, &pStmt,
7445      "SELECT ("
7446      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
7447      ") FROM sqlite_schema WHERE name = %Q", zName
7448    );
7449    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7450      bSqlIntkey = sqlite3_column_int(pStmt, 0);
7451    }
7452    shellFinalize(&rc, pStmt);
7453
7454    if( bIntkey==bSqlIntkey ){
7455      int i;
7456      const char *zPk = "_rowid_";
7457      sqlite3_stmt *pPkFinder = 0;
7458
7459      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
7460      ** set zPk to the name of the PK column, and pTab->iPk to the index
7461      ** of the column, where columns are 0-numbered from left to right.
7462      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7463      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7464      pTab->iPk = -2;
7465      if( bIntkey ){
7466        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7467          "SELECT cid, name FROM pragma_table_info(%Q) "
7468          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7469          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7470          , zName, zName
7471        );
7472        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7473          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7474          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7475          if( zPk==0 ){ zPk = "_";  /* Defensive.  Should never happen */ }
7476        }
7477      }
7478
7479      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7480      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7481      pTab->nCol = nSqlCol;
7482
7483      if( bIntkey ){
7484        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7485      }else{
7486        pTab->azlCol[0] = shellMPrintf(&rc, "");
7487      }
7488      i = 1;
7489      shellPreparePrintf(dbtmp, &rc, &pStmt,
7490          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7491          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7492          "FROM pragma_table_info(%Q)",
7493          bIntkey ? ", " : "", pTab->iPk,
7494          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7495          zName
7496      );
7497      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7498        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7499        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7500        i++;
7501      }
7502      shellFinalize(&rc, pStmt);
7503
7504      shellFinalize(&rc, pPkFinder);
7505    }
7506  }
7507
7508 finished:
7509  sqlite3_close(dbtmp);
7510  *pRc = rc;
7511  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7512    recoverFreeTable(pTab);
7513    pTab = 0;
7514  }
7515  return pTab;
7516}
7517
7518/*
7519** This function is called to search the schema recovered from the
7520** sqlite_schema table of the (possibly) corrupt database as part
7521** of a ".recover" command. Specifically, for a table with root page
7522** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7523** table must be a WITHOUT ROWID table, or if non-zero, not one of
7524** those.
7525**
7526** If a table is found, a (RecoverTable*) object is returned. Or, if
7527** no such table is found, but bIntkey is false and iRoot is the
7528** root page of an index in the recovered schema, then (*pbNoop) is
7529** set to true and NULL returned. Or, if there is no such table or
7530** index, NULL is returned and (*pbNoop) set to 0, indicating that
7531** the caller should write data to the orphans table.
7532*/
7533static RecoverTable *recoverFindTable(
7534  ShellState *pState,             /* Shell state object */
7535  int *pRc,                       /* IN/OUT: Error code */
7536  int iRoot,                      /* Root page of table */
7537  int bIntkey,                    /* True for an intkey table */
7538  int nCol,                       /* Number of columns in table */
7539  int *pbNoop                     /* OUT: True if iRoot is root of index */
7540){
7541  sqlite3_stmt *pStmt = 0;
7542  RecoverTable *pRet = 0;
7543  int bNoop = 0;
7544  const char *zSql = 0;
7545  const char *zName = 0;
7546
7547  /* Search the recovered schema for an object with root page iRoot. */
7548  shellPreparePrintf(pState->db, pRc, &pStmt,
7549      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7550  );
7551  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7552    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7553    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7554      bNoop = 1;
7555      break;
7556    }
7557    if( sqlite3_stricmp(zType, "table")==0 ){
7558      zName = (const char*)sqlite3_column_text(pStmt, 1);
7559      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7560      if( zName!=0 && zSql!=0 ){
7561        pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7562        break;
7563      }
7564    }
7565  }
7566
7567  shellFinalize(pRc, pStmt);
7568  *pbNoop = bNoop;
7569  return pRet;
7570}
7571
7572/*
7573** Return a RecoverTable object representing the orphans table.
7574*/
7575static RecoverTable *recoverOrphanTable(
7576  ShellState *pState,             /* Shell state object */
7577  int *pRc,                       /* IN/OUT: Error code */
7578  const char *zLostAndFound,      /* Base name for orphans table */
7579  int nCol                        /* Number of user data columns */
7580){
7581  RecoverTable *pTab = 0;
7582  if( nCol>=0 && *pRc==SQLITE_OK ){
7583    int i;
7584
7585    /* This block determines the name of the orphan table. The prefered
7586    ** name is zLostAndFound. But if that clashes with another name
7587    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7588    ** and so on until a non-clashing name is found.  */
7589    int iTab = 0;
7590    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7591    sqlite3_stmt *pTest = 0;
7592    shellPrepare(pState->db, pRc,
7593        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7594    );
7595    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7596    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7597      shellReset(pRc, pTest);
7598      sqlite3_free(zTab);
7599      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7600      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7601    }
7602    shellFinalize(pRc, pTest);
7603
7604    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7605    if( pTab ){
7606      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7607      pTab->nCol = nCol;
7608      pTab->iPk = -2;
7609      if( nCol>0 ){
7610        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7611        if( pTab->azlCol ){
7612          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7613          for(i=nCol-1; i>=0; i--){
7614            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7615          }
7616        }
7617      }
7618
7619      if( *pRc!=SQLITE_OK ){
7620        recoverFreeTable(pTab);
7621        pTab = 0;
7622      }else{
7623        raw_printf(pState->out,
7624            "CREATE TABLE %s(rootpgno INTEGER, "
7625            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7626        );
7627        for(i=0; i<nCol; i++){
7628          raw_printf(pState->out, ", c%d", i);
7629        }
7630        raw_printf(pState->out, ");\n");
7631      }
7632    }
7633    sqlite3_free(zTab);
7634  }
7635  return pTab;
7636}
7637
7638/*
7639** This function is called to recover data from the database. A script
7640** to construct a new database containing all recovered data is output
7641** on stream pState->out.
7642*/
7643static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7644  int rc = SQLITE_OK;
7645  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7646  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7647  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7648  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7649  const char *zLostAndFound = "lost_and_found";
7650  int i;
7651  int nOrphan = -1;
7652  RecoverTable *pOrphan = 0;
7653
7654  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7655  int bRowids = 1;                /* 0 if --no-rowids */
7656  for(i=1; i<nArg; i++){
7657    char *z = azArg[i];
7658    int n;
7659    if( z[0]=='-' && z[1]=='-' ) z++;
7660    n = strlen30(z);
7661    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7662      bFreelist = 0;
7663    }else
7664    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7665      i++;
7666      zRecoveryDb = azArg[i];
7667    }else
7668    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7669      i++;
7670      zLostAndFound = azArg[i];
7671    }else
7672    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7673      bRowids = 0;
7674    }
7675    else{
7676      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7677      showHelp(pState->out, azArg[0]);
7678      return 1;
7679    }
7680  }
7681
7682  shellExecPrintf(pState->db, &rc,
7683    /* Attach an in-memory database named 'recovery'. Create an indexed
7684    ** cache of the sqlite_dbptr virtual table. */
7685    "PRAGMA writable_schema = on;"
7686    "ATTACH %Q AS recovery;"
7687    "DROP TABLE IF EXISTS recovery.dbptr;"
7688    "DROP TABLE IF EXISTS recovery.freelist;"
7689    "DROP TABLE IF EXISTS recovery.map;"
7690    "DROP TABLE IF EXISTS recovery.schema;"
7691    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7692  );
7693
7694  if( bFreelist ){
7695    shellExec(pState->db, &rc,
7696      "WITH trunk(pgno) AS ("
7697      "  SELECT shell_int32("
7698      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7699      "      WHERE x>0"
7700      "    UNION"
7701      "  SELECT shell_int32("
7702      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7703      "      FROM trunk WHERE x>0"
7704      "),"
7705      "freelist(data, n, freepgno) AS ("
7706      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7707      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7708      "    UNION ALL"
7709      "  SELECT data, n-1, shell_int32(data, 2+n) "
7710      "      FROM freelist WHERE n>=0"
7711      ")"
7712      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7713    );
7714  }
7715
7716  /* If this is an auto-vacuum database, add all pointer-map pages to
7717  ** the freelist table. Do this regardless of whether or not
7718  ** --freelist-corrupt was specified.  */
7719  shellExec(pState->db, &rc,
7720    "WITH ptrmap(pgno) AS ("
7721    "  SELECT 2 WHERE shell_int32("
7722    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7723    "  )"
7724    "    UNION ALL "
7725    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7726    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7727    ")"
7728    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7729  );
7730
7731  shellExec(pState->db, &rc,
7732    "CREATE TABLE recovery.dbptr("
7733    "      pgno, child, PRIMARY KEY(child, pgno)"
7734    ") WITHOUT ROWID;"
7735    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7736    "    SELECT * FROM sqlite_dbptr"
7737    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7738
7739    /* Delete any pointer to page 1. This ensures that page 1 is considered
7740    ** a root page, regardless of how corrupt the db is. */
7741    "DELETE FROM recovery.dbptr WHERE child = 1;"
7742
7743    /* Delete all pointers to any pages that have more than one pointer
7744    ** to them. Such pages will be treated as root pages when recovering
7745    ** data.  */
7746    "DELETE FROM recovery.dbptr WHERE child IN ("
7747    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7748    ");"
7749
7750    /* Create the "map" table that will (eventually) contain instructions
7751    ** for dealing with each page in the db that contains one or more
7752    ** records. */
7753    "CREATE TABLE recovery.map("
7754      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7755    ");"
7756
7757    /* Populate table [map]. If there are circular loops of pages in the
7758    ** database, the following adds all pages in such a loop to the map
7759    ** as individual root pages. This could be handled better.  */
7760    "WITH pages(i, maxlen) AS ("
7761    "  SELECT page_count, ("
7762    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7763    "  ) FROM pragma_page_count WHERE page_count>0"
7764    "    UNION ALL"
7765    "  SELECT i-1, ("
7766    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7767    "  ) FROM pages WHERE i>=2"
7768    ")"
7769    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7770    "  SELECT i, maxlen, NULL, ("
7771    "    WITH p(orig, pgno, parent) AS ("
7772    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7773    "        UNION "
7774    "      SELECT i, p.parent, "
7775    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7776    "    )"
7777    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7778    ") "
7779    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7780    "UPDATE recovery.map AS o SET intkey = ("
7781    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7782    ");"
7783
7784    /* Extract data from page 1 and any linked pages into table
7785    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7786    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7787    "INSERT INTO recovery.schema SELECT "
7788    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7789    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7790    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7791    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7792    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7793    "FROM sqlite_dbdata WHERE pgno IN ("
7794    "  SELECT pgno FROM recovery.map WHERE root=1"
7795    ")"
7796    "GROUP BY pgno, cell;"
7797    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7798  );
7799
7800  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7801  ** CREATE TABLE statements that extracted from the existing schema.  */
7802  if( rc==SQLITE_OK ){
7803    sqlite3_stmt *pStmt = 0;
7804    /* ".recover" might output content in an order which causes immediate
7805    ** foreign key constraints to be violated. So disable foreign-key
7806    ** constraint enforcement to prevent problems when running the output
7807    ** script. */
7808    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7809    raw_printf(pState->out, "BEGIN;\n");
7810    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7811    shellPrepare(pState->db, &rc,
7812        "SELECT sql FROM recovery.schema "
7813        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7814    );
7815    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7816      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7817      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7818          &zCreateTable[12]
7819      );
7820    }
7821    shellFinalize(&rc, pStmt);
7822  }
7823
7824  /* Figure out if an orphan table will be required. And if so, how many
7825  ** user columns it should contain */
7826  shellPrepare(pState->db, &rc,
7827      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7828      , &pLoop
7829  );
7830  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7831    nOrphan = sqlite3_column_int(pLoop, 0);
7832  }
7833  shellFinalize(&rc, pLoop);
7834  pLoop = 0;
7835
7836  shellPrepare(pState->db, &rc,
7837      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7838  );
7839
7840  shellPrepare(pState->db, &rc,
7841      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7842      "(case when (? AND field<0) then NULL else value end)"
7843      "), ', ')"
7844      ", min(field) "
7845      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7846      "GROUP BY cell", &pCells
7847  );
7848
7849  /* Loop through each root page. */
7850  shellPrepare(pState->db, &rc,
7851      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7852      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7853      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7854      ")", &pLoop
7855  );
7856  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7857    int iRoot = sqlite3_column_int(pLoop, 0);
7858    int bIntkey = sqlite3_column_int(pLoop, 1);
7859    int nCol = sqlite3_column_int(pLoop, 2);
7860    int bNoop = 0;
7861    RecoverTable *pTab;
7862
7863    assert( bIntkey==0 || bIntkey==1 );
7864    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7865    if( bNoop || rc ) continue;
7866    if( pTab==0 ){
7867      if( pOrphan==0 ){
7868        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7869      }
7870      pTab = pOrphan;
7871      if( pTab==0 ) break;
7872    }
7873
7874    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7875      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7876    }
7877    sqlite3_bind_int(pPages, 1, iRoot);
7878    if( bRowids==0 && pTab->iPk<0 ){
7879      sqlite3_bind_int(pCells, 1, 1);
7880    }else{
7881      sqlite3_bind_int(pCells, 1, 0);
7882    }
7883    sqlite3_bind_int(pCells, 3, pTab->iPk);
7884
7885    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7886      int iPgno = sqlite3_column_int(pPages, 0);
7887      sqlite3_bind_int(pCells, 2, iPgno);
7888      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7889        int nField = sqlite3_column_int(pCells, 0);
7890        int iMin = sqlite3_column_int(pCells, 2);
7891        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7892
7893        RecoverTable *pTab2 = pTab;
7894        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7895          if( pOrphan==0 ){
7896            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7897          }
7898          pTab2 = pOrphan;
7899          if( pTab2==0 ) break;
7900        }
7901
7902        nField = nField+1;
7903        if( pTab2==pOrphan ){
7904          raw_printf(pState->out,
7905              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7906              pTab2->zQuoted, iRoot, iPgno, nField,
7907              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7908          );
7909        }else{
7910          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7911              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7912          );
7913        }
7914      }
7915      shellReset(&rc, pCells);
7916    }
7917    shellReset(&rc, pPages);
7918    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7919  }
7920  shellFinalize(&rc, pLoop);
7921  shellFinalize(&rc, pPages);
7922  shellFinalize(&rc, pCells);
7923  recoverFreeTable(pOrphan);
7924
7925  /* The rest of the schema */
7926  if( rc==SQLITE_OK ){
7927    sqlite3_stmt *pStmt = 0;
7928    shellPrepare(pState->db, &rc,
7929        "SELECT sql, name FROM recovery.schema "
7930        "WHERE sql NOT LIKE 'create table%'", &pStmt
7931    );
7932    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7933      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7934      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7935        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7936        char *zPrint = shellMPrintf(&rc,
7937          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7938          zName, zName, zSql
7939        );
7940        raw_printf(pState->out, "%s;\n", zPrint);
7941        sqlite3_free(zPrint);
7942      }else{
7943        raw_printf(pState->out, "%s;\n", zSql);
7944      }
7945    }
7946    shellFinalize(&rc, pStmt);
7947  }
7948
7949  if( rc==SQLITE_OK ){
7950    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7951    raw_printf(pState->out, "COMMIT;\n");
7952  }
7953  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7954  return rc;
7955}
7956#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7957
7958
7959/*
7960 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7961 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7962 *   close db and set it to 0, and return the columns spec, to later
7963 *   be sqlite3_free()'ed by the caller.
7964 * The return is 0 when either:
7965 *   (a) The db was not initialized and zCol==0 (There are no columns.)
7966 *   (b) zCol!=0  (Column was added, db initialized as needed.)
7967 * The 3rd argument, pRenamed, references an out parameter. If the
7968 * pointer is non-zero, its referent will be set to a summary of renames
7969 * done if renaming was necessary, or set to 0 if none was done. The out
7970 * string (if any) must be sqlite3_free()'ed by the caller.
7971 */
7972#ifdef SHELL_DEBUG
7973#define rc_err_oom_die(rc) \
7974  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7975  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7976    fprintf(stderr,"E:%d\n",rc), assert(0)
7977#else
7978static void rc_err_oom_die(int rc){
7979  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7980  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7981}
7982#endif
7983
7984#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7985static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7986#else  /* Otherwise, memory is faster/better for the transient DB. */
7987static const char *zCOL_DB = ":memory:";
7988#endif
7989
7990/* Define character (as C string) to separate generated column ordinal
7991 * from protected part of incoming column names. This defaults to "_"
7992 * so that incoming column identifiers that did not need not be quoted
7993 * remain usable without being quoted. It must be one character.
7994 */
7995#ifndef SHELL_AUTOCOLUMN_SEP
7996# define AUTOCOLUMN_SEP "_"
7997#else
7998# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7999#endif
8000
8001static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
8002  /* Queries and D{D,M}L used here */
8003  static const char * const zTabMake = "\
8004CREATE TABLE ColNames(\
8005 cpos INTEGER PRIMARY KEY,\
8006 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
8007CREATE VIEW RepeatedNames AS \
8008SELECT DISTINCT t.name FROM ColNames t \
8009WHERE t.name COLLATE NOCASE IN (\
8010 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
8011);\
8012";
8013  static const char * const zTabFill = "\
8014INSERT INTO ColNames(name,nlen,chop,reps,suff)\
8015 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
8016";
8017  static const char * const zHasDupes = "\
8018SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
8019 <count(name) FROM ColNames\
8020";
8021#ifdef SHELL_COLUMN_RENAME_CLEAN
8022  static const char * const zDedoctor = "\
8023UPDATE ColNames SET chop=iif(\
8024  (substring(name,nlen,1) BETWEEN '0' AND '9')\
8025  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
8026 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
8027 0\
8028)\
8029";
8030#endif
8031  static const char * const zSetReps = "\
8032UPDATE ColNames AS t SET reps=\
8033(SELECT count(*) FROM ColNames d \
8034 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
8035 COLLATE NOCASE\
8036)\
8037";
8038#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8039  static const char * const zColDigits = "\
8040SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8041";
8042#else
8043  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8044  static const char * const zColDigits = "\
8045SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8046 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8047 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8048";
8049#endif
8050  static const char * const zRenameRank =
8051#ifdef SHELL_COLUMN_RENAME_CLEAN
8052    "UPDATE ColNames AS t SET suff="
8053    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
8054#else /* ...RENAME_MINIMAL_ONE_PASS */
8055"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8056"  SELECT 0 AS nlz"
8057"  UNION"
8058"  SELECT nlz+1 AS nlz FROM Lzn"
8059"  WHERE EXISTS("
8060"   SELECT 1"
8061"   FROM ColNames t, ColNames o"
8062"   WHERE"
8063"    iif(t.name IN (SELECT * FROM RepeatedNames),"
8064"     printf('%s"AUTOCOLUMN_SEP"%s',"
8065"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8066"     t.name"
8067"    )"
8068"    ="
8069"    iif(o.name IN (SELECT * FROM RepeatedNames),"
8070"     printf('%s"AUTOCOLUMN_SEP"%s',"
8071"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8072"     o.name"
8073"    )"
8074"    COLLATE NOCASE"
8075"    AND o.cpos<>t.cpos"
8076"   GROUP BY t.cpos"
8077"  )"
8078") UPDATE Colnames AS t SET"
8079" chop = 0," /* No chopping, never touch incoming names. */
8080" suff = iif(name IN (SELECT * FROM RepeatedNames),"
8081"  printf('"AUTOCOLUMN_SEP"%s', substring("
8082"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8083"  ''"
8084" )"
8085#endif
8086    ;
8087  static const char * const zCollectVar = "\
8088SELECT\
8089 '('||x'0a'\
8090 || group_concat(\
8091  cname||' TEXT',\
8092  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8093 ||')' AS ColsSpec \
8094FROM (\
8095 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8096 FROM ColNames ORDER BY cpos\
8097)";
8098  static const char * const zRenamesDone =
8099    "SELECT group_concat("
8100    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8101    " ','||x'0a')"
8102    "FROM ColNames WHERE suff<>'' OR chop!=0"
8103    ;
8104  int rc;
8105  sqlite3_stmt *pStmt = 0;
8106  assert(pDb!=0);
8107  if( zColNew ){
8108    /* Add initial or additional column. Init db if necessary. */
8109    if( *pDb==0 ){
8110      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
8111#ifdef SHELL_COLFIX_DB
8112      if(*zCOL_DB!=':')
8113        sqlite3_exec(*pDb,"drop table if exists ColNames;"
8114                     "drop view if exists RepeatedNames;",0,0,0);
8115#endif
8116      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
8117      rc_err_oom_die(rc);
8118    }
8119    assert(*pDb!=0);
8120    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
8121    rc_err_oom_die(rc);
8122    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
8123    rc_err_oom_die(rc);
8124    rc = sqlite3_step(pStmt);
8125    rc_err_oom_die(rc);
8126    sqlite3_finalize(pStmt);
8127    return 0;
8128  }else if( *pDb==0 ){
8129    return 0;
8130  }else{
8131    /* Formulate the columns spec, close the DB, zero *pDb. */
8132    char *zColsSpec = 0;
8133    int hasDupes = db_int(*pDb, zHasDupes);
8134    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
8135    if( hasDupes ){
8136#ifdef SHELL_COLUMN_RENAME_CLEAN
8137      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
8138      rc_err_oom_die(rc);
8139#endif
8140      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
8141      rc_err_oom_die(rc);
8142      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
8143      rc_err_oom_die(rc);
8144      sqlite3_bind_int(pStmt, 1, nDigits);
8145      rc = sqlite3_step(pStmt);
8146      sqlite3_finalize(pStmt);
8147      assert(rc==SQLITE_DONE);
8148    }
8149    assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
8150    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
8151    rc_err_oom_die(rc);
8152    rc = sqlite3_step(pStmt);
8153    if( rc==SQLITE_ROW ){
8154      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8155    }else{
8156      zColsSpec = 0;
8157    }
8158    if( pzRenamed!=0 ){
8159      if( !hasDupes ) *pzRenamed = 0;
8160      else{
8161        sqlite3_finalize(pStmt);
8162        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
8163            && SQLITE_ROW==sqlite3_step(pStmt) ){
8164          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8165        }else
8166          *pzRenamed = 0;
8167      }
8168    }
8169    sqlite3_finalize(pStmt);
8170    sqlite3_close(*pDb);
8171    *pDb = 0;
8172    return zColsSpec;
8173  }
8174}
8175
8176/*
8177** If an input line begins with "." then invoke this routine to
8178** process that line.
8179**
8180** Return 1 on error, 2 to exit, and 0 otherwise.
8181*/
8182static int do_meta_command(char *zLine, ShellState *p){
8183  int h = 1;
8184  int nArg = 0;
8185  int n, c;
8186  int rc = 0;
8187  char *azArg[52];
8188
8189#ifndef SQLITE_OMIT_VIRTUALTABLE
8190  if( p->expert.pExpert ){
8191    expertFinish(p, 1, 0);
8192  }
8193#endif
8194
8195  /* Parse the input line into tokens.
8196  */
8197  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
8198    while( IsSpace(zLine[h]) ){ h++; }
8199    if( zLine[h]==0 ) break;
8200    if( zLine[h]=='\'' || zLine[h]=='"' ){
8201      int delim = zLine[h++];
8202      azArg[nArg++] = &zLine[h];
8203      while( zLine[h] && zLine[h]!=delim ){
8204        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
8205        h++;
8206      }
8207      if( zLine[h]==delim ){
8208        zLine[h++] = 0;
8209      }
8210      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
8211    }else{
8212      azArg[nArg++] = &zLine[h];
8213      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
8214      if( zLine[h] ) zLine[h++] = 0;
8215      resolve_backslashes(azArg[nArg-1]);
8216    }
8217  }
8218  azArg[nArg] = 0;
8219
8220  /* Process the input line.
8221  */
8222  if( nArg==0 ) return 0; /* no tokens, no error */
8223  n = strlen30(azArg[0]);
8224  c = azArg[0][0];
8225  clearTempFile(p);
8226
8227#ifndef SQLITE_OMIT_AUTHORIZATION
8228  if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
8229    if( nArg!=2 ){
8230      raw_printf(stderr, "Usage: .auth ON|OFF\n");
8231      rc = 1;
8232      goto meta_command_exit;
8233    }
8234    open_db(p, 0);
8235    if( booleanValue(azArg[1]) ){
8236      sqlite3_set_authorizer(p->db, shellAuth, p);
8237    }else if( p->bSafeModePersist ){
8238      sqlite3_set_authorizer(p->db, safeModeAuth, p);
8239    }else{
8240      sqlite3_set_authorizer(p->db, 0, 0);
8241    }
8242  }else
8243#endif
8244
8245#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8246  && !defined(SQLITE_SHELL_FIDDLE)
8247  if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
8248    open_db(p, 0);
8249    failIfSafeMode(p, "cannot run .archive in safe mode");
8250    rc = arDotCommand(p, 0, azArg, nArg);
8251  }else
8252#endif
8253
8254#ifndef SQLITE_SHELL_FIDDLE
8255  if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
8256   || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
8257  ){
8258    const char *zDestFile = 0;
8259    const char *zDb = 0;
8260    sqlite3 *pDest;
8261    sqlite3_backup *pBackup;
8262    int j;
8263    int bAsync = 0;
8264    const char *zVfs = 0;
8265    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8266    for(j=1; j<nArg; j++){
8267      const char *z = azArg[j];
8268      if( z[0]=='-' ){
8269        if( z[1]=='-' ) z++;
8270        if( cli_strcmp(z, "-append")==0 ){
8271          zVfs = "apndvfs";
8272        }else
8273        if( cli_strcmp(z, "-async")==0 ){
8274          bAsync = 1;
8275        }else
8276        {
8277          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
8278          return 1;
8279        }
8280      }else if( zDestFile==0 ){
8281        zDestFile = azArg[j];
8282      }else if( zDb==0 ){
8283        zDb = zDestFile;
8284        zDestFile = azArg[j];
8285      }else{
8286        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8287        return 1;
8288      }
8289    }
8290    if( zDestFile==0 ){
8291      raw_printf(stderr, "missing FILENAME argument on .backup\n");
8292      return 1;
8293    }
8294    if( zDb==0 ) zDb = "main";
8295    rc = sqlite3_open_v2(zDestFile, &pDest,
8296                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8297    if( rc!=SQLITE_OK ){
8298      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
8299      close_db(pDest);
8300      return 1;
8301    }
8302    if( bAsync ){
8303      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8304                   0, 0, 0);
8305    }
8306    open_db(p, 0);
8307    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8308    if( pBackup==0 ){
8309      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8310      close_db(pDest);
8311      return 1;
8312    }
8313    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8314    sqlite3_backup_finish(pBackup);
8315    if( rc==SQLITE_DONE ){
8316      rc = 0;
8317    }else{
8318      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8319      rc = 1;
8320    }
8321    close_db(pDest);
8322  }else
8323#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8324
8325  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
8326    if( nArg==2 ){
8327      bail_on_error = booleanValue(azArg[1]);
8328    }else{
8329      raw_printf(stderr, "Usage: .bail on|off\n");
8330      rc = 1;
8331    }
8332  }else
8333
8334  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
8335    if( nArg==2 ){
8336      if( booleanValue(azArg[1]) ){
8337        setBinaryMode(p->out, 1);
8338      }else{
8339        setTextMode(p->out, 1);
8340      }
8341    }else{
8342      raw_printf(stderr, "Usage: .binary on|off\n");
8343      rc = 1;
8344    }
8345  }else
8346
8347  /* The undocumented ".breakpoint" command causes a call to the no-op
8348  ** routine named test_breakpoint().
8349  */
8350  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
8351    test_breakpoint();
8352  }else
8353
8354#ifndef SQLITE_SHELL_FIDDLE
8355  if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
8356    failIfSafeMode(p, "cannot run .cd in safe mode");
8357    if( nArg==2 ){
8358#if defined(_WIN32) || defined(WIN32)
8359      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8360      rc = !SetCurrentDirectoryW(z);
8361      sqlite3_free(z);
8362#else
8363      rc = chdir(azArg[1]);
8364#endif
8365      if( rc ){
8366        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
8367        rc = 1;
8368      }
8369    }else{
8370      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
8371      rc = 1;
8372    }
8373  }else
8374#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8375
8376  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
8377    if( nArg==2 ){
8378      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8379    }else{
8380      raw_printf(stderr, "Usage: .changes on|off\n");
8381      rc = 1;
8382    }
8383  }else
8384
8385#ifndef SQLITE_SHELL_FIDDLE
8386  /* Cancel output redirection, if it is currently set (by .testcase)
8387  ** Then read the content of the testcase-out.txt file and compare against
8388  ** azArg[1].  If there are differences, report an error and exit.
8389  */
8390  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
8391    char *zRes = 0;
8392    output_reset(p);
8393    if( nArg!=2 ){
8394      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
8395      rc = 2;
8396    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8397      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
8398      rc = 2;
8399    }else if( testcase_glob(azArg[1],zRes)==0 ){
8400      utf8_printf(stderr,
8401                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
8402                 p->zTestcase, azArg[1], zRes);
8403      rc = 1;
8404    }else{
8405      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
8406      p->nCheck++;
8407    }
8408    sqlite3_free(zRes);
8409  }else
8410#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8411
8412#ifndef SQLITE_SHELL_FIDDLE
8413  if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
8414    failIfSafeMode(p, "cannot run .clone in safe mode");
8415    if( nArg==2 ){
8416      tryToClone(p, azArg[1]);
8417    }else{
8418      raw_printf(stderr, "Usage: .clone FILENAME\n");
8419      rc = 1;
8420    }
8421  }else
8422#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8423
8424  if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
8425    if( nArg==1 ){
8426      /* List available connections */
8427      int i;
8428      for(i=0; i<ArraySize(p->aAuxDb); i++){
8429        const char *zFile = p->aAuxDb[i].zDbFilename;
8430        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8431          zFile = "(not open)";
8432        }else if( zFile==0 ){
8433          zFile = "(memory)";
8434        }else if( zFile[0]==0 ){
8435          zFile = "(temporary-file)";
8436        }
8437        if( p->pAuxDb == &p->aAuxDb[i] ){
8438          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
8439        }else if( p->aAuxDb[i].db!=0 ){
8440          utf8_printf(stdout, "       %d: %s\n", i, zFile);
8441        }
8442      }
8443    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8444      int i = azArg[1][0] - '0';
8445      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8446        p->pAuxDb->db = p->db;
8447        p->pAuxDb = &p->aAuxDb[i];
8448        globalDb = p->db = p->pAuxDb->db;
8449        p->pAuxDb->db = 0;
8450      }
8451    }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
8452           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8453      int i = azArg[2][0] - '0';
8454      if( i<0 || i>=ArraySize(p->aAuxDb) ){
8455        /* No-op */
8456      }else if( p->pAuxDb == &p->aAuxDb[i] ){
8457        raw_printf(stderr, "cannot close the active database connection\n");
8458        rc = 1;
8459      }else if( p->aAuxDb[i].db ){
8460        session_close_all(p, i);
8461        close_db(p->aAuxDb[i].db);
8462        p->aAuxDb[i].db = 0;
8463      }
8464    }else{
8465      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
8466      rc = 1;
8467    }
8468  }else
8469
8470  if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
8471    char **azName = 0;
8472    int nName = 0;
8473    sqlite3_stmt *pStmt;
8474    int i;
8475    open_db(p, 0);
8476    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8477    if( rc ){
8478      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8479      rc = 1;
8480    }else{
8481      while( sqlite3_step(pStmt)==SQLITE_ROW ){
8482        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8483        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8484        if( zSchema==0 || zFile==0 ) continue;
8485        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8486        shell_check_oom(azName);
8487        azName[nName*2] = strdup(zSchema);
8488        azName[nName*2+1] = strdup(zFile);
8489        nName++;
8490      }
8491    }
8492    sqlite3_finalize(pStmt);
8493    for(i=0; i<nName; i++){
8494      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8495      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8496      const char *z = azName[i*2+1];
8497      utf8_printf(p->out, "%s: %s %s%s\n",
8498         azName[i*2],
8499         z && z[0] ? z : "\"\"",
8500         bRdonly ? "r/o" : "r/w",
8501         eTxn==SQLITE_TXN_NONE ? "" :
8502            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8503      free(azName[i*2]);
8504      free(azName[i*2+1]);
8505    }
8506    sqlite3_free(azName);
8507  }else
8508
8509  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
8510    static const struct DbConfigChoices {
8511      const char *zName;
8512      int op;
8513    } aDbConfig[] = {
8514        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
8515        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
8516        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
8517        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
8518        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
8519        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
8520        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
8521        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8522        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
8523        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
8524        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8525        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
8526        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
8527        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
8528        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
8529        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
8530    };
8531    int ii, v;
8532    open_db(p, 0);
8533    for(ii=0; ii<ArraySize(aDbConfig); ii++){
8534      if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8535      if( nArg>=3 ){
8536        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8537      }
8538      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8539      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
8540      if( nArg>1 ) break;
8541    }
8542    if( nArg>1 && ii==ArraySize(aDbConfig) ){
8543      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
8544      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
8545    }
8546  }else
8547
8548#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8549  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
8550    rc = shell_dbinfo_command(p, nArg, azArg);
8551  }else
8552
8553  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
8554    open_db(p, 0);
8555    rc = recoverDatabaseCmd(p, nArg, azArg);
8556  }else
8557#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
8558
8559  if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
8560    char *zLike = 0;
8561    char *zSql;
8562    int i;
8563    int savedShowHeader = p->showHeader;
8564    int savedShellFlags = p->shellFlgs;
8565    ShellClearFlag(p,
8566       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8567       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8568    for(i=1; i<nArg; i++){
8569      if( azArg[i][0]=='-' ){
8570        const char *z = azArg[i]+1;
8571        if( z[0]=='-' ) z++;
8572        if( cli_strcmp(z,"preserve-rowids")==0 ){
8573#ifdef SQLITE_OMIT_VIRTUALTABLE
8574          raw_printf(stderr, "The --preserve-rowids option is not compatible"
8575                             " with SQLITE_OMIT_VIRTUALTABLE\n");
8576          rc = 1;
8577          sqlite3_free(zLike);
8578          goto meta_command_exit;
8579#else
8580          ShellSetFlag(p, SHFLG_PreserveRowid);
8581#endif
8582        }else
8583        if( cli_strcmp(z,"newlines")==0 ){
8584          ShellSetFlag(p, SHFLG_Newlines);
8585        }else
8586        if( cli_strcmp(z,"data-only")==0 ){
8587          ShellSetFlag(p, SHFLG_DumpDataOnly);
8588        }else
8589        if( cli_strcmp(z,"nosys")==0 ){
8590          ShellSetFlag(p, SHFLG_DumpNoSys);
8591        }else
8592        {
8593          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8594          rc = 1;
8595          sqlite3_free(zLike);
8596          goto meta_command_exit;
8597        }
8598      }else{
8599        /* azArg[i] contains a LIKE pattern. This ".dump" request should
8600        ** only dump data for tables for which either the table name matches
8601        ** the LIKE pattern, or the table appears to be a shadow table of
8602        ** a virtual table for which the name matches the LIKE pattern.
8603        */
8604        char *zExpr = sqlite3_mprintf(
8605            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8606            "  SELECT 1 FROM sqlite_schema WHERE "
8607            "    name LIKE %Q ESCAPE '\\' AND"
8608            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8609            "    substr(o.name, 1, length(name)+1) == (name||'_')"
8610            ")", azArg[i], azArg[i]
8611        );
8612
8613        if( zLike ){
8614          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8615        }else{
8616          zLike = zExpr;
8617        }
8618      }
8619    }
8620
8621    open_db(p, 0);
8622
8623    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8624      /* When playing back a "dump", the content might appear in an order
8625      ** which causes immediate foreign key constraints to be violated.
8626      ** So disable foreign-key constraint enforcement to prevent problems. */
8627      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8628      raw_printf(p->out, "BEGIN TRANSACTION;\n");
8629    }
8630    p->writableSchema = 0;
8631    p->showHeader = 0;
8632    /* Set writable_schema=ON since doing so forces SQLite to initialize
8633    ** as much of the schema as it can even if the sqlite_schema table is
8634    ** corrupt. */
8635    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8636    p->nErr = 0;
8637    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8638    zSql = sqlite3_mprintf(
8639      "SELECT name, type, sql FROM sqlite_schema AS o "
8640      "WHERE (%s) AND type=='table'"
8641      "  AND sql NOT NULL"
8642      " ORDER BY tbl_name='sqlite_sequence', rowid",
8643      zLike
8644    );
8645    run_schema_dump_query(p,zSql);
8646    sqlite3_free(zSql);
8647    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8648      zSql = sqlite3_mprintf(
8649        "SELECT sql FROM sqlite_schema AS o "
8650        "WHERE (%s) AND sql NOT NULL"
8651        "  AND type IN ('index','trigger','view')",
8652        zLike
8653      );
8654      run_table_dump_query(p, zSql);
8655      sqlite3_free(zSql);
8656    }
8657    sqlite3_free(zLike);
8658    if( p->writableSchema ){
8659      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8660      p->writableSchema = 0;
8661    }
8662    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8663    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8664    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8665      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8666    }
8667    p->showHeader = savedShowHeader;
8668    p->shellFlgs = savedShellFlags;
8669  }else
8670
8671  if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
8672    if( nArg==2 ){
8673      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8674    }else{
8675      raw_printf(stderr, "Usage: .echo on|off\n");
8676      rc = 1;
8677    }
8678  }else
8679
8680  if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
8681    if( nArg==2 ){
8682      p->autoEQPtest = 0;
8683      if( p->autoEQPtrace ){
8684        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8685        p->autoEQPtrace = 0;
8686      }
8687      if( cli_strcmp(azArg[1],"full")==0 ){
8688        p->autoEQP = AUTOEQP_full;
8689      }else if( cli_strcmp(azArg[1],"trigger")==0 ){
8690        p->autoEQP = AUTOEQP_trigger;
8691#ifdef SQLITE_DEBUG
8692      }else if( cli_strcmp(azArg[1],"test")==0 ){
8693        p->autoEQP = AUTOEQP_on;
8694        p->autoEQPtest = 1;
8695      }else if( cli_strcmp(azArg[1],"trace")==0 ){
8696        p->autoEQP = AUTOEQP_full;
8697        p->autoEQPtrace = 1;
8698        open_db(p, 0);
8699        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8700        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8701#endif
8702      }else{
8703        p->autoEQP = (u8)booleanValue(azArg[1]);
8704      }
8705    }else{
8706      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8707      rc = 1;
8708    }
8709  }else
8710
8711#ifndef SQLITE_SHELL_FIDDLE
8712  if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
8713    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8714    rc = 2;
8715  }else
8716#endif
8717
8718  /* The ".explain" command is automatic now.  It is largely pointless.  It
8719  ** retained purely for backwards compatibility */
8720  if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
8721    int val = 1;
8722    if( nArg>=2 ){
8723      if( cli_strcmp(azArg[1],"auto")==0 ){
8724        val = 99;
8725      }else{
8726        val =  booleanValue(azArg[1]);
8727      }
8728    }
8729    if( val==1 && p->mode!=MODE_Explain ){
8730      p->normalMode = p->mode;
8731      p->mode = MODE_Explain;
8732      p->autoExplain = 0;
8733    }else if( val==0 ){
8734      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8735      p->autoExplain = 0;
8736    }else if( val==99 ){
8737      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8738      p->autoExplain = 1;
8739    }
8740  }else
8741
8742#ifndef SQLITE_OMIT_VIRTUALTABLE
8743  if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
8744    if( p->bSafeMode ){
8745      raw_printf(stderr,
8746        "Cannot run experimental commands such as \"%s\" in safe mode\n",
8747        azArg[0]);
8748      rc = 1;
8749    }else{
8750      open_db(p, 0);
8751      expertDotCommand(p, azArg, nArg);
8752    }
8753  }else
8754#endif
8755
8756  if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
8757    static const struct {
8758       const char *zCtrlName;   /* Name of a test-control option */
8759       int ctrlCode;            /* Integer code for that option */
8760       const char *zUsage;      /* Usage notes */
8761    } aCtrl[] = {
8762      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8763      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8764      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8765      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8766      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8767   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8768      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8769      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8770      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8771      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8772   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8773    };
8774    int filectrl = -1;
8775    int iCtrl = -1;
8776    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8777    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8778    int n2, i;
8779    const char *zCmd = 0;
8780    const char *zSchema = 0;
8781
8782    open_db(p, 0);
8783    zCmd = nArg>=2 ? azArg[1] : "help";
8784
8785    if( zCmd[0]=='-'
8786     && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
8787     && nArg>=4
8788    ){
8789      zSchema = azArg[2];
8790      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8791      nArg -= 2;
8792      zCmd = azArg[1];
8793    }
8794
8795    /* The argument can optionally begin with "-" or "--" */
8796    if( zCmd[0]=='-' && zCmd[1] ){
8797      zCmd++;
8798      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8799    }
8800
8801    /* --help lists all file-controls */
8802    if( cli_strcmp(zCmd,"help")==0 ){
8803      utf8_printf(p->out, "Available file-controls:\n");
8804      for(i=0; i<ArraySize(aCtrl); i++){
8805        utf8_printf(p->out, "  .filectrl %s %s\n",
8806                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8807      }
8808      rc = 1;
8809      goto meta_command_exit;
8810    }
8811
8812    /* convert filectrl text option to value. allow any unique prefix
8813    ** of the option name, or a numerical value. */
8814    n2 = strlen30(zCmd);
8815    for(i=0; i<ArraySize(aCtrl); i++){
8816      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8817        if( filectrl<0 ){
8818          filectrl = aCtrl[i].ctrlCode;
8819          iCtrl = i;
8820        }else{
8821          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8822                              "Use \".filectrl --help\" for help\n", zCmd);
8823          rc = 1;
8824          goto meta_command_exit;
8825        }
8826      }
8827    }
8828    if( filectrl<0 ){
8829      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8830                         "Use \".filectrl --help\" for help\n", zCmd);
8831    }else{
8832      switch(filectrl){
8833        case SQLITE_FCNTL_SIZE_LIMIT: {
8834          if( nArg!=2 && nArg!=3 ) break;
8835          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8836          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8837          isOk = 1;
8838          break;
8839        }
8840        case SQLITE_FCNTL_LOCK_TIMEOUT:
8841        case SQLITE_FCNTL_CHUNK_SIZE: {
8842          int x;
8843          if( nArg!=3 ) break;
8844          x = (int)integerValue(azArg[2]);
8845          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8846          isOk = 2;
8847          break;
8848        }
8849        case SQLITE_FCNTL_PERSIST_WAL:
8850        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8851          int x;
8852          if( nArg!=2 && nArg!=3 ) break;
8853          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8854          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8855          iRes = x;
8856          isOk = 1;
8857          break;
8858        }
8859        case SQLITE_FCNTL_DATA_VERSION:
8860        case SQLITE_FCNTL_HAS_MOVED: {
8861          int x;
8862          if( nArg!=2 ) break;
8863          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8864          iRes = x;
8865          isOk = 1;
8866          break;
8867        }
8868        case SQLITE_FCNTL_TEMPFILENAME: {
8869          char *z = 0;
8870          if( nArg!=2 ) break;
8871          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8872          if( z ){
8873            utf8_printf(p->out, "%s\n", z);
8874            sqlite3_free(z);
8875          }
8876          isOk = 2;
8877          break;
8878        }
8879        case SQLITE_FCNTL_RESERVE_BYTES: {
8880          int x;
8881          if( nArg>=3 ){
8882            x = atoi(azArg[2]);
8883            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8884          }
8885          x = -1;
8886          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8887          utf8_printf(p->out,"%d\n", x);
8888          isOk = 2;
8889          break;
8890        }
8891      }
8892    }
8893    if( isOk==0 && iCtrl>=0 ){
8894      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8895      rc = 1;
8896    }else if( isOk==1 ){
8897      char zBuf[100];
8898      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8899      raw_printf(p->out, "%s\n", zBuf);
8900    }
8901  }else
8902
8903  if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
8904    ShellState data;
8905    int doStats = 0;
8906    memcpy(&data, p, sizeof(data));
8907    data.showHeader = 0;
8908    data.cMode = data.mode = MODE_Semi;
8909    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8910      data.cMode = data.mode = MODE_Pretty;
8911      nArg = 1;
8912    }
8913    if( nArg!=1 ){
8914      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8915      rc = 1;
8916      goto meta_command_exit;
8917    }
8918    open_db(p, 0);
8919    rc = sqlite3_exec(p->db,
8920       "SELECT sql FROM"
8921       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8922       "     FROM sqlite_schema UNION ALL"
8923       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8924       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8925       "ORDER BY x",
8926       callback, &data, 0
8927    );
8928    if( rc==SQLITE_OK ){
8929      sqlite3_stmt *pStmt;
8930      rc = sqlite3_prepare_v2(p->db,
8931               "SELECT rowid FROM sqlite_schema"
8932               " WHERE name GLOB 'sqlite_stat[134]'",
8933               -1, &pStmt, 0);
8934      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8935      sqlite3_finalize(pStmt);
8936    }
8937    if( doStats==0 ){
8938      raw_printf(p->out, "/* No STAT tables available */\n");
8939    }else{
8940      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8941      data.cMode = data.mode = MODE_Insert;
8942      data.zDestTable = "sqlite_stat1";
8943      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8944      data.zDestTable = "sqlite_stat4";
8945      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8946      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8947    }
8948  }else
8949
8950  if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
8951    if( nArg==2 ){
8952      p->showHeader = booleanValue(azArg[1]);
8953      p->shellFlgs |= SHFLG_HeaderSet;
8954    }else{
8955      raw_printf(stderr, "Usage: .headers on|off\n");
8956      rc = 1;
8957    }
8958  }else
8959
8960  if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
8961    if( nArg>=2 ){
8962      n = showHelp(p->out, azArg[1]);
8963      if( n==0 ){
8964        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8965      }
8966    }else{
8967      showHelp(p->out, 0);
8968    }
8969  }else
8970
8971#ifndef SQLITE_SHELL_FIDDLE
8972  if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
8973    char *zTable = 0;           /* Insert data into this table */
8974    char *zSchema = 0;          /* within this schema (may default to "main") */
8975    char *zFile = 0;            /* Name of file to extra content from */
8976    sqlite3_stmt *pStmt = NULL; /* A statement */
8977    int nCol;                   /* Number of columns in the table */
8978    int nByte;                  /* Number of bytes in an SQL string */
8979    int i, j;                   /* Loop counters */
8980    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8981    int nSep;                   /* Number of bytes in p->colSeparator[] */
8982    char *zSql;                 /* An SQL statement */
8983    char *zFullTabName;         /* Table name with schema if applicable */
8984    ImportCtx sCtx;             /* Reader context */
8985    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8986    int eVerbose = 0;           /* Larger for more console output */
8987    int nSkip = 0;              /* Initial lines to skip */
8988    int useOutputMode = 1;      /* Use output mode to determine separators */
8989    char *zCreate = 0;          /* CREATE TABLE statement text */
8990
8991    failIfSafeMode(p, "cannot run .import in safe mode");
8992    memset(&sCtx, 0, sizeof(sCtx));
8993    if( p->mode==MODE_Ascii ){
8994      xRead = ascii_read_one_field;
8995    }else{
8996      xRead = csv_read_one_field;
8997    }
8998    rc = 1;
8999    for(i=1; i<nArg; i++){
9000      char *z = azArg[i];
9001      if( z[0]=='-' && z[1]=='-' ) z++;
9002      if( z[0]!='-' ){
9003        if( zFile==0 ){
9004          zFile = z;
9005        }else if( zTable==0 ){
9006          zTable = z;
9007        }else{
9008          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
9009          showHelp(p->out, "import");
9010          goto meta_command_exit;
9011        }
9012      }else if( cli_strcmp(z,"-v")==0 ){
9013        eVerbose++;
9014      }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
9015        zSchema = azArg[++i];
9016      }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
9017        nSkip = integerValue(azArg[++i]);
9018      }else if( cli_strcmp(z,"-ascii")==0 ){
9019        sCtx.cColSep = SEP_Unit[0];
9020        sCtx.cRowSep = SEP_Record[0];
9021        xRead = ascii_read_one_field;
9022        useOutputMode = 0;
9023      }else if( cli_strcmp(z,"-csv")==0 ){
9024        sCtx.cColSep = ',';
9025        sCtx.cRowSep = '\n';
9026        xRead = csv_read_one_field;
9027        useOutputMode = 0;
9028      }else{
9029        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
9030        showHelp(p->out, "import");
9031        goto meta_command_exit;
9032      }
9033    }
9034    if( zTable==0 ){
9035      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
9036                  zFile==0 ? "FILE" : "TABLE");
9037      showHelp(p->out, "import");
9038      goto meta_command_exit;
9039    }
9040    seenInterrupt = 0;
9041    open_db(p, 0);
9042    if( useOutputMode ){
9043      /* If neither the --csv or --ascii options are specified, then set
9044      ** the column and row separator characters from the output mode. */
9045      nSep = strlen30(p->colSeparator);
9046      if( nSep==0 ){
9047        raw_printf(stderr,
9048                   "Error: non-null column separator required for import\n");
9049        goto meta_command_exit;
9050      }
9051      if( nSep>1 ){
9052        raw_printf(stderr,
9053              "Error: multi-character column separators not allowed"
9054              " for import\n");
9055        goto meta_command_exit;
9056      }
9057      nSep = strlen30(p->rowSeparator);
9058      if( nSep==0 ){
9059        raw_printf(stderr,
9060            "Error: non-null row separator required for import\n");
9061        goto meta_command_exit;
9062      }
9063      if( nSep==2 && p->mode==MODE_Csv
9064       && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
9065      ){
9066        /* When importing CSV (only), if the row separator is set to the
9067        ** default output row separator, change it to the default input
9068        ** row separator.  This avoids having to maintain different input
9069        ** and output row separators. */
9070        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9071        nSep = strlen30(p->rowSeparator);
9072      }
9073      if( nSep>1 ){
9074        raw_printf(stderr, "Error: multi-character row separators not allowed"
9075                           " for import\n");
9076        goto meta_command_exit;
9077      }
9078      sCtx.cColSep = p->colSeparator[0];
9079      sCtx.cRowSep = p->rowSeparator[0];
9080    }
9081    sCtx.zFile = zFile;
9082    sCtx.nLine = 1;
9083    if( sCtx.zFile[0]=='|' ){
9084#ifdef SQLITE_OMIT_POPEN
9085      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9086      goto meta_command_exit;
9087#else
9088      sCtx.in = popen(sCtx.zFile+1, "r");
9089      sCtx.zFile = "<pipe>";
9090      sCtx.xCloser = pclose;
9091#endif
9092    }else{
9093      sCtx.in = fopen(sCtx.zFile, "rb");
9094      sCtx.xCloser = fclose;
9095    }
9096    if( sCtx.in==0 ){
9097      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
9098      goto meta_command_exit;
9099    }
9100    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
9101      char zSep[2];
9102      zSep[1] = 0;
9103      zSep[0] = sCtx.cColSep;
9104      utf8_printf(p->out, "Column separator ");
9105      output_c_string(p->out, zSep);
9106      utf8_printf(p->out, ", row separator ");
9107      zSep[0] = sCtx.cRowSep;
9108      output_c_string(p->out, zSep);
9109      utf8_printf(p->out, "\n");
9110    }
9111    sCtx.z = sqlite3_malloc64(120);
9112    if( sCtx.z==0 ){
9113      import_cleanup(&sCtx);
9114      shell_out_of_memory();
9115    }
9116    /* Below, resources must be freed before exit. */
9117    while( (nSkip--)>0 ){
9118      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
9119    }
9120    if( zSchema!=0 ){
9121      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
9122    }else{
9123      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
9124    }
9125    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
9126    if( zSql==0 || zFullTabName==0 ){
9127      import_cleanup(&sCtx);
9128      shell_out_of_memory();
9129    }
9130    nByte = strlen30(zSql);
9131    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9132    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
9133    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
9134      sqlite3 *dbCols = 0;
9135      char *zRenames = 0;
9136      char *zColDefs;
9137      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
9138      while( xRead(&sCtx) ){
9139        zAutoColumn(sCtx.z, &dbCols, 0);
9140        if( sCtx.cTerm!=sCtx.cColSep ) break;
9141      }
9142      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
9143      if( zRenames!=0 ){
9144        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
9145                    "Columns renamed during .import %s due to duplicates:\n"
9146                    "%s\n", sCtx.zFile, zRenames);
9147        sqlite3_free(zRenames);
9148      }
9149      assert(dbCols==0);
9150      if( zColDefs==0 ){
9151        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
9152      import_fail:
9153        sqlite3_free(zCreate);
9154        sqlite3_free(zSql);
9155        sqlite3_free(zFullTabName);
9156        import_cleanup(&sCtx);
9157        rc = 1;
9158        goto meta_command_exit;
9159      }
9160      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
9161      if( eVerbose>=1 ){
9162        utf8_printf(p->out, "%s\n", zCreate);
9163      }
9164      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
9165      if( rc ){
9166        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
9167        goto import_fail;
9168      }
9169      sqlite3_free(zCreate);
9170      zCreate = 0;
9171      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9172    }
9173    if( rc ){
9174      if (pStmt) sqlite3_finalize(pStmt);
9175      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
9176      goto import_fail;
9177    }
9178    sqlite3_free(zSql);
9179    nCol = sqlite3_column_count(pStmt);
9180    sqlite3_finalize(pStmt);
9181    pStmt = 0;
9182    if( nCol==0 ) return 0; /* no columns, no error */
9183    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
9184    if( zSql==0 ){
9185      import_cleanup(&sCtx);
9186      shell_out_of_memory();
9187    }
9188    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
9189    j = strlen30(zSql);
9190    for(i=1; i<nCol; i++){
9191      zSql[j++] = ',';
9192      zSql[j++] = '?';
9193    }
9194    zSql[j++] = ')';
9195    zSql[j] = 0;
9196    if( eVerbose>=2 ){
9197      utf8_printf(p->out, "Insert using: %s\n", zSql);
9198    }
9199    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9200    if( rc ){
9201      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9202      if (pStmt) sqlite3_finalize(pStmt);
9203      goto import_fail;
9204    }
9205    sqlite3_free(zSql);
9206    sqlite3_free(zFullTabName);
9207    needCommit = sqlite3_get_autocommit(p->db);
9208    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
9209    do{
9210      int startLine = sCtx.nLine;
9211      for(i=0; i<nCol; i++){
9212        char *z = xRead(&sCtx);
9213        /*
9214        ** Did we reach end-of-file before finding any columns?
9215        ** If so, stop instead of NULL filling the remaining columns.
9216        */
9217        if( z==0 && i==0 ) break;
9218        /*
9219        ** Did we reach end-of-file OR end-of-line before finding any
9220        ** columns in ASCII mode?  If so, stop instead of NULL filling
9221        ** the remaining columns.
9222        */
9223        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
9224        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
9225        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
9226          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9227                          "filling the rest with NULL\n",
9228                          sCtx.zFile, startLine, nCol, i+1);
9229          i += 2;
9230          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9231        }
9232      }
9233      if( sCtx.cTerm==sCtx.cColSep ){
9234        do{
9235          xRead(&sCtx);
9236          i++;
9237        }while( sCtx.cTerm==sCtx.cColSep );
9238        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9239                        "extras ignored\n",
9240                        sCtx.zFile, startLine, nCol, i);
9241      }
9242      if( i>=nCol ){
9243        sqlite3_step(pStmt);
9244        rc = sqlite3_reset(pStmt);
9245        if( rc!=SQLITE_OK ){
9246          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
9247                      startLine, sqlite3_errmsg(p->db));
9248          sCtx.nErr++;
9249        }else{
9250          sCtx.nRow++;
9251        }
9252      }
9253    }while( sCtx.cTerm!=EOF );
9254
9255    import_cleanup(&sCtx);
9256    sqlite3_finalize(pStmt);
9257    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9258    if( eVerbose>0 ){
9259      utf8_printf(p->out,
9260          "Added %d rows with %d errors using %d lines of input\n",
9261          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9262    }
9263  }else
9264#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9265
9266#ifndef SQLITE_UNTESTABLE
9267  if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
9268    char *zSql;
9269    char *zCollist = 0;
9270    sqlite3_stmt *pStmt;
9271    int tnum = 0;
9272    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
9273    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9274    int i;
9275    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9276      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
9277                          "       .imposter off\n");
9278      /* Also allowed, but not documented:
9279      **
9280      **    .imposter TABLE IMPOSTER
9281      **
9282      ** where TABLE is a WITHOUT ROWID table.  In that case, the
9283      ** imposter is another WITHOUT ROWID table with the columns in
9284      ** storage order. */
9285      rc = 1;
9286      goto meta_command_exit;
9287    }
9288    open_db(p, 0);
9289    if( nArg==2 ){
9290      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9291      goto meta_command_exit;
9292    }
9293    zSql = sqlite3_mprintf(
9294      "SELECT rootpage, 0 FROM sqlite_schema"
9295      " WHERE name='%q' AND type='index'"
9296      "UNION ALL "
9297      "SELECT rootpage, 1 FROM sqlite_schema"
9298      " WHERE name='%q' AND type='table'"
9299      "   AND sql LIKE '%%without%%rowid%%'",
9300      azArg[1], azArg[1]
9301    );
9302    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9303    sqlite3_free(zSql);
9304    if( sqlite3_step(pStmt)==SQLITE_ROW ){
9305      tnum = sqlite3_column_int(pStmt, 0);
9306      isWO = sqlite3_column_int(pStmt, 1);
9307    }
9308    sqlite3_finalize(pStmt);
9309    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9310    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9311    sqlite3_free(zSql);
9312    i = 0;
9313    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9314      char zLabel[20];
9315      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9316      i++;
9317      if( zCol==0 ){
9318        if( sqlite3_column_int(pStmt,1)==-1 ){
9319          zCol = "_ROWID_";
9320        }else{
9321          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9322          zCol = zLabel;
9323        }
9324      }
9325      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9326        lenPK = (int)strlen(zCollist);
9327      }
9328      if( zCollist==0 ){
9329        zCollist = sqlite3_mprintf("\"%w\"", zCol);
9330      }else{
9331        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9332      }
9333    }
9334    sqlite3_finalize(pStmt);
9335    if( i==0 || tnum==0 ){
9336      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
9337      rc = 1;
9338      sqlite3_free(zCollist);
9339      goto meta_command_exit;
9340    }
9341    if( lenPK==0 ) lenPK = 100000;
9342    zSql = sqlite3_mprintf(
9343          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9344          azArg[2], zCollist, lenPK, zCollist);
9345    sqlite3_free(zCollist);
9346    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9347    if( rc==SQLITE_OK ){
9348      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9349      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9350      if( rc ){
9351        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9352      }else{
9353        utf8_printf(stdout, "%s;\n", zSql);
9354        raw_printf(stdout,
9355          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
9356          azArg[1], isWO ? "table" : "index"
9357        );
9358      }
9359    }else{
9360      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9361      rc = 1;
9362    }
9363    sqlite3_free(zSql);
9364  }else
9365#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9366
9367#ifdef SQLITE_ENABLE_IOTRACE
9368  if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
9369    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9370    if( iotrace && iotrace!=stdout ) fclose(iotrace);
9371    iotrace = 0;
9372    if( nArg<2 ){
9373      sqlite3IoTrace = 0;
9374    }else if( cli_strcmp(azArg[1], "-")==0 ){
9375      sqlite3IoTrace = iotracePrintf;
9376      iotrace = stdout;
9377    }else{
9378      iotrace = fopen(azArg[1], "w");
9379      if( iotrace==0 ){
9380        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9381        sqlite3IoTrace = 0;
9382        rc = 1;
9383      }else{
9384        sqlite3IoTrace = iotracePrintf;
9385      }
9386    }
9387  }else
9388#endif
9389
9390  if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
9391    static const struct {
9392       const char *zLimitName;   /* Name of a limit */
9393       int limitCode;            /* Integer code for that limit */
9394    } aLimit[] = {
9395      { "length",                SQLITE_LIMIT_LENGTH                    },
9396      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
9397      { "column",                SQLITE_LIMIT_COLUMN                    },
9398      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
9399      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
9400      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
9401      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
9402      { "attached",              SQLITE_LIMIT_ATTACHED                  },
9403      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
9404      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
9405      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
9406      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
9407    };
9408    int i, n2;
9409    open_db(p, 0);
9410    if( nArg==1 ){
9411      for(i=0; i<ArraySize(aLimit); i++){
9412        printf("%20s %d\n", aLimit[i].zLimitName,
9413               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9414      }
9415    }else if( nArg>3 ){
9416      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
9417      rc = 1;
9418      goto meta_command_exit;
9419    }else{
9420      int iLimit = -1;
9421      n2 = strlen30(azArg[1]);
9422      for(i=0; i<ArraySize(aLimit); i++){
9423        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9424          if( iLimit<0 ){
9425            iLimit = i;
9426          }else{
9427            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
9428            rc = 1;
9429            goto meta_command_exit;
9430          }
9431        }
9432      }
9433      if( iLimit<0 ){
9434        utf8_printf(stderr, "unknown limit: \"%s\"\n"
9435                        "enter \".limits\" with no arguments for a list.\n",
9436                         azArg[1]);
9437        rc = 1;
9438        goto meta_command_exit;
9439      }
9440      if( nArg==3 ){
9441        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9442                      (int)integerValue(azArg[2]));
9443      }
9444      printf("%20s %d\n", aLimit[iLimit].zLimitName,
9445             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9446    }
9447  }else
9448
9449  if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
9450    open_db(p, 0);
9451    lintDotCommand(p, azArg, nArg);
9452  }else
9453
9454#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9455  if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
9456    const char *zFile, *zProc;
9457    char *zErrMsg = 0;
9458    failIfSafeMode(p, "cannot run .load in safe mode");
9459    if( nArg<2 ){
9460      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
9461      rc = 1;
9462      goto meta_command_exit;
9463    }
9464    zFile = azArg[1];
9465    zProc = nArg>=3 ? azArg[2] : 0;
9466    open_db(p, 0);
9467    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9468    if( rc!=SQLITE_OK ){
9469      utf8_printf(stderr, "Error: %s\n", zErrMsg);
9470      sqlite3_free(zErrMsg);
9471      rc = 1;
9472    }
9473  }else
9474#endif
9475
9476#ifndef SQLITE_SHELL_FIDDLE
9477  if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
9478    failIfSafeMode(p, "cannot run .log in safe mode");
9479    if( nArg!=2 ){
9480      raw_printf(stderr, "Usage: .log FILENAME\n");
9481      rc = 1;
9482    }else{
9483      const char *zFile = azArg[1];
9484      output_file_close(p->pLog);
9485      p->pLog = output_file_open(zFile, 0);
9486    }
9487  }else
9488#endif
9489
9490  if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
9491    const char *zMode = 0;
9492    const char *zTabname = 0;
9493    int i, n2;
9494    ColModeOpts cmOpts = ColModeOpts_default;
9495    for(i=1; i<nArg; i++){
9496      const char *z = azArg[i];
9497      if( optionMatch(z,"wrap") && i+1<nArg ){
9498        cmOpts.iWrap = integerValue(azArg[++i]);
9499      }else if( optionMatch(z,"ww") ){
9500        cmOpts.bWordWrap = 1;
9501      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9502        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9503      }else if( optionMatch(z,"quote") ){
9504        cmOpts.bQuote = 1;
9505      }else if( optionMatch(z,"noquote") ){
9506        cmOpts.bQuote = 0;
9507      }else if( zMode==0 ){
9508        zMode = z;
9509        /* Apply defaults for qbox pseudo-mods. If that
9510         * overwrites already-set values, user was informed of this.
9511         */
9512        if( cli_strcmp(z, "qbox")==0 ){
9513          ColModeOpts cmo = ColModeOpts_default_qbox;
9514          zMode = "box";
9515          cmOpts = cmo;
9516        }
9517      }else if( zTabname==0 ){
9518        zTabname = z;
9519      }else if( z[0]=='-' ){
9520        utf8_printf(stderr, "unknown option: %s\n", z);
9521        utf8_printf(stderr, "options:\n"
9522                            "  --noquote\n"
9523                            "  --quote\n"
9524                            "  --wordwrap on/off\n"
9525                            "  --wrap N\n"
9526                            "  --ww\n");
9527        rc = 1;
9528        goto meta_command_exit;
9529      }else{
9530        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9531        rc = 1;
9532        goto meta_command_exit;
9533      }
9534    }
9535    if( zMode==0 ){
9536      if( p->mode==MODE_Column
9537       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9538      ){
9539        raw_printf
9540          (p->out,
9541           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9542           modeDescr[p->mode], p->cmOpts.iWrap,
9543           p->cmOpts.bWordWrap ? "on" : "off",
9544           p->cmOpts.bQuote ? "" : "no");
9545      }else{
9546        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
9547      }
9548      zMode = modeDescr[p->mode];
9549    }
9550    n2 = strlen30(zMode);
9551    if( cli_strncmp(zMode,"lines",n2)==0 ){
9552      p->mode = MODE_Line;
9553      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9554    }else if( cli_strncmp(zMode,"columns",n2)==0 ){
9555      p->mode = MODE_Column;
9556      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9557        p->showHeader = 1;
9558      }
9559      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9560      p->cmOpts = cmOpts;
9561    }else if( cli_strncmp(zMode,"list",n2)==0 ){
9562      p->mode = MODE_List;
9563      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9564      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9565    }else if( cli_strncmp(zMode,"html",n2)==0 ){
9566      p->mode = MODE_Html;
9567    }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
9568      p->mode = MODE_Tcl;
9569      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9570      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9571    }else if( cli_strncmp(zMode,"csv",n2)==0 ){
9572      p->mode = MODE_Csv;
9573      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9574      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9575    }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
9576      p->mode = MODE_List;
9577      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9578    }else if( cli_strncmp(zMode,"insert",n2)==0 ){
9579      p->mode = MODE_Insert;
9580      set_table_name(p, zTabname ? zTabname : "table");
9581    }else if( cli_strncmp(zMode,"quote",n2)==0 ){
9582      p->mode = MODE_Quote;
9583      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9584      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9585    }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
9586      p->mode = MODE_Ascii;
9587      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9588      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9589    }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
9590      p->mode = MODE_Markdown;
9591      p->cmOpts = cmOpts;
9592    }else if( cli_strncmp(zMode,"table",n2)==0 ){
9593      p->mode = MODE_Table;
9594      p->cmOpts = cmOpts;
9595    }else if( cli_strncmp(zMode,"box",n2)==0 ){
9596      p->mode = MODE_Box;
9597      p->cmOpts = cmOpts;
9598    }else if( cli_strncmp(zMode,"count",n2)==0 ){
9599      p->mode = MODE_Count;
9600    }else if( cli_strncmp(zMode,"off",n2)==0 ){
9601      p->mode = MODE_Off;
9602    }else if( cli_strncmp(zMode,"json",n2)==0 ){
9603      p->mode = MODE_Json;
9604    }else{
9605      raw_printf(stderr, "Error: mode should be one of: "
9606         "ascii box column csv html insert json line list markdown "
9607         "qbox quote table tabs tcl\n");
9608      rc = 1;
9609    }
9610    p->cMode = p->mode;
9611  }else
9612
9613#ifndef SQLITE_SHELL_FIDDLE
9614  if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
9615    if( nArg!=2 ){
9616      raw_printf(stderr, "Usage: .nonce NONCE\n");
9617      rc = 1;
9618    }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
9619      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9620                 p->lineno, azArg[1]);
9621      exit(1);
9622    }else{
9623      p->bSafeMode = 0;
9624      return 0;  /* Return immediately to bypass the safe mode reset
9625                 ** at the end of this procedure */
9626    }
9627  }else
9628#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9629
9630  if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
9631    if( nArg==2 ){
9632      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9633                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9634    }else{
9635      raw_printf(stderr, "Usage: .nullvalue STRING\n");
9636      rc = 1;
9637    }
9638  }else
9639
9640  if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
9641    const char *zFN = 0;     /* Pointer to constant filename */
9642    char *zNewFilename = 0;  /* Name of the database file to open */
9643    int iName = 1;           /* Index in azArg[] of the filename */
9644    int newFlag = 0;         /* True to delete file before opening */
9645    int openMode = SHELL_OPEN_UNSPEC;
9646
9647    /* Check for command-line arguments */
9648    for(iName=1; iName<nArg; iName++){
9649      const char *z = azArg[iName];
9650#ifndef SQLITE_SHELL_FIDDLE
9651      if( optionMatch(z,"new") ){
9652        newFlag = 1;
9653#ifdef SQLITE_HAVE_ZLIB
9654      }else if( optionMatch(z, "zip") ){
9655        openMode = SHELL_OPEN_ZIPFILE;
9656#endif
9657      }else if( optionMatch(z, "append") ){
9658        openMode = SHELL_OPEN_APPENDVFS;
9659      }else if( optionMatch(z, "readonly") ){
9660        openMode = SHELL_OPEN_READONLY;
9661      }else if( optionMatch(z, "nofollow") ){
9662        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9663#ifndef SQLITE_OMIT_DESERIALIZE
9664      }else if( optionMatch(z, "deserialize") ){
9665        openMode = SHELL_OPEN_DESERIALIZE;
9666      }else if( optionMatch(z, "hexdb") ){
9667        openMode = SHELL_OPEN_HEXDB;
9668      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9669        p->szMax = integerValue(azArg[++iName]);
9670#endif /* SQLITE_OMIT_DESERIALIZE */
9671      }else
9672#endif /* !SQLITE_SHELL_FIDDLE */
9673      if( z[0]=='-' ){
9674        utf8_printf(stderr, "unknown option: %s\n", z);
9675        rc = 1;
9676        goto meta_command_exit;
9677      }else if( zFN ){
9678        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9679        rc = 1;
9680        goto meta_command_exit;
9681      }else{
9682        zFN = z;
9683      }
9684    }
9685
9686    /* Close the existing database */
9687    session_close_all(p, -1);
9688    close_db(p->db);
9689    p->db = 0;
9690    p->pAuxDb->zDbFilename = 0;
9691    sqlite3_free(p->pAuxDb->zFreeOnClose);
9692    p->pAuxDb->zFreeOnClose = 0;
9693    p->openMode = openMode;
9694    p->openFlags = 0;
9695    p->szMax = 0;
9696
9697    /* If a filename is specified, try to open it first */
9698    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9699      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9700#ifndef SQLITE_SHELL_FIDDLE
9701      if( p->bSafeMode
9702       && p->openMode!=SHELL_OPEN_HEXDB
9703       && zFN
9704       && cli_strcmp(zFN,":memory:")!=0
9705      ){
9706        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9707      }
9708#else
9709      /* WASM mode has its own sandboxed pseudo-filesystem. */
9710#endif
9711      if( zFN ){
9712        zNewFilename = sqlite3_mprintf("%s", zFN);
9713        shell_check_oom(zNewFilename);
9714      }else{
9715        zNewFilename = 0;
9716      }
9717      p->pAuxDb->zDbFilename = zNewFilename;
9718      open_db(p, OPEN_DB_KEEPALIVE);
9719      if( p->db==0 ){
9720        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9721        sqlite3_free(zNewFilename);
9722      }else{
9723        p->pAuxDb->zFreeOnClose = zNewFilename;
9724      }
9725    }
9726    if( p->db==0 ){
9727      /* As a fall-back open a TEMP database */
9728      p->pAuxDb->zDbFilename = 0;
9729      open_db(p, 0);
9730    }
9731  }else
9732
9733#ifndef SQLITE_SHELL_FIDDLE
9734  if( (c=='o'
9735        && (cli_strncmp(azArg[0], "output", n)==0
9736            || cli_strncmp(azArg[0], "once", n)==0))
9737   || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
9738  ){
9739    char *zFile = 0;
9740    int bTxtMode = 0;
9741    int i;
9742    int eMode = 0;
9743    int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
9744    unsigned char zBOM[4];    /* Byte-order mark to using if --bom is present */
9745
9746    zBOM[0] = 0;
9747    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9748    if( c=='e' ){
9749      eMode = 'x';
9750      bOnce = 2;
9751    }else if( cli_strncmp(azArg[0],"once",n)==0 ){
9752      bOnce = 1;
9753    }
9754    for(i=1; i<nArg; i++){
9755      char *z = azArg[i];
9756      if( z[0]=='-' ){
9757        if( z[1]=='-' ) z++;
9758        if( cli_strcmp(z,"-bom")==0 ){
9759          zBOM[0] = 0xef;
9760          zBOM[1] = 0xbb;
9761          zBOM[2] = 0xbf;
9762          zBOM[3] = 0;
9763        }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
9764          eMode = 'x';  /* spreadsheet */
9765        }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
9766          eMode = 'e';  /* text editor */
9767        }else{
9768          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9769                      azArg[i]);
9770          showHelp(p->out, azArg[0]);
9771          rc = 1;
9772          goto meta_command_exit;
9773        }
9774      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9775        zFile = sqlite3_mprintf("%s", z);
9776        if( zFile && zFile[0]=='|' ){
9777          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9778          break;
9779        }
9780      }else{
9781        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9782                    azArg[i]);
9783        showHelp(p->out, azArg[0]);
9784        rc = 1;
9785        sqlite3_free(zFile);
9786        goto meta_command_exit;
9787      }
9788    }
9789    if( zFile==0 ){
9790      zFile = sqlite3_mprintf("stdout");
9791    }
9792    if( bOnce ){
9793      p->outCount = 2;
9794    }else{
9795      p->outCount = 0;
9796    }
9797    output_reset(p);
9798#ifndef SQLITE_NOHAVE_SYSTEM
9799    if( eMode=='e' || eMode=='x' ){
9800      p->doXdgOpen = 1;
9801      outputModePush(p);
9802      if( eMode=='x' ){
9803        /* spreadsheet mode.  Output as CSV. */
9804        newTempFile(p, "csv");
9805        ShellClearFlag(p, SHFLG_Echo);
9806        p->mode = MODE_Csv;
9807        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9808        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9809      }else{
9810        /* text editor mode */
9811        newTempFile(p, "txt");
9812        bTxtMode = 1;
9813      }
9814      sqlite3_free(zFile);
9815      zFile = sqlite3_mprintf("%s", p->zTempFile);
9816    }
9817#endif /* SQLITE_NOHAVE_SYSTEM */
9818    shell_check_oom(zFile);
9819    if( zFile[0]=='|' ){
9820#ifdef SQLITE_OMIT_POPEN
9821      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9822      rc = 1;
9823      p->out = stdout;
9824#else
9825      p->out = popen(zFile + 1, "w");
9826      if( p->out==0 ){
9827        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9828        p->out = stdout;
9829        rc = 1;
9830      }else{
9831        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9832        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9833      }
9834#endif
9835    }else{
9836      p->out = output_file_open(zFile, bTxtMode);
9837      if( p->out==0 ){
9838        if( cli_strcmp(zFile,"off")!=0 ){
9839          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9840        }
9841        p->out = stdout;
9842        rc = 1;
9843      } else {
9844        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9845        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9846      }
9847    }
9848    sqlite3_free(zFile);
9849  }else
9850#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9851
9852  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
9853    open_db(p,0);
9854    if( nArg<=1 ) goto parameter_syntax_error;
9855
9856    /* .parameter clear
9857    ** Clear all bind parameters by dropping the TEMP table that holds them.
9858    */
9859    if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
9860      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9861                   0, 0, 0);
9862    }else
9863
9864    /* .parameter list
9865    ** List all bind parameters.
9866    */
9867    if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
9868      sqlite3_stmt *pStmt = 0;
9869      int rx;
9870      int len = 0;
9871      rx = sqlite3_prepare_v2(p->db,
9872             "SELECT max(length(key)) "
9873             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9874      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9875        len = sqlite3_column_int(pStmt, 0);
9876        if( len>40 ) len = 40;
9877      }
9878      sqlite3_finalize(pStmt);
9879      pStmt = 0;
9880      if( len ){
9881        rx = sqlite3_prepare_v2(p->db,
9882             "SELECT key, quote(value) "
9883             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9884        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9885          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9886                      sqlite3_column_text(pStmt,1));
9887        }
9888        sqlite3_finalize(pStmt);
9889      }
9890    }else
9891
9892    /* .parameter init
9893    ** Make sure the TEMP table used to hold bind parameters exists.
9894    ** Create it if necessary.
9895    */
9896    if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
9897      bind_table_init(p);
9898    }else
9899
9900    /* .parameter set NAME VALUE
9901    ** Set or reset a bind parameter.  NAME should be the full parameter
9902    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9903    ** VALUE can be in either SQL literal notation, or if not it will be
9904    ** understood to be a text string.
9905    */
9906    if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
9907      int rx;
9908      char *zSql;
9909      sqlite3_stmt *pStmt;
9910      const char *zKey = azArg[2];
9911      const char *zValue = azArg[3];
9912      bind_table_init(p);
9913      zSql = sqlite3_mprintf(
9914                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9915                  "VALUES(%Q,%s);", zKey, zValue);
9916      shell_check_oom(zSql);
9917      pStmt = 0;
9918      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9919      sqlite3_free(zSql);
9920      if( rx!=SQLITE_OK ){
9921        sqlite3_finalize(pStmt);
9922        pStmt = 0;
9923        zSql = sqlite3_mprintf(
9924                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9925                   "VALUES(%Q,%Q);", zKey, zValue);
9926        shell_check_oom(zSql);
9927        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9928        sqlite3_free(zSql);
9929        if( rx!=SQLITE_OK ){
9930          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9931          sqlite3_finalize(pStmt);
9932          pStmt = 0;
9933          rc = 1;
9934        }
9935      }
9936      sqlite3_step(pStmt);
9937      sqlite3_finalize(pStmt);
9938    }else
9939
9940    /* .parameter unset NAME
9941    ** Remove the NAME binding from the parameter binding table, if it
9942    ** exists.
9943    */
9944    if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
9945      char *zSql = sqlite3_mprintf(
9946          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9947      shell_check_oom(zSql);
9948      sqlite3_exec(p->db, zSql, 0, 0, 0);
9949      sqlite3_free(zSql);
9950    }else
9951    /* If no command name matches, show a syntax error */
9952    parameter_syntax_error:
9953    showHelp(p->out, "parameter");
9954  }else
9955
9956  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
9957    int i;
9958    for(i=1; i<nArg; i++){
9959      if( i>1 ) raw_printf(p->out, " ");
9960      utf8_printf(p->out, "%s", azArg[i]);
9961    }
9962    raw_printf(p->out, "\n");
9963  }else
9964
9965#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9966  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
9967    int i;
9968    int nn = 0;
9969    p->flgProgress = 0;
9970    p->mxProgress = 0;
9971    p->nProgress = 0;
9972    for(i=1; i<nArg; i++){
9973      const char *z = azArg[i];
9974      if( z[0]=='-' ){
9975        z++;
9976        if( z[0]=='-' ) z++;
9977        if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
9978          p->flgProgress |= SHELL_PROGRESS_QUIET;
9979          continue;
9980        }
9981        if( cli_strcmp(z,"reset")==0 ){
9982          p->flgProgress |= SHELL_PROGRESS_RESET;
9983          continue;
9984        }
9985        if( cli_strcmp(z,"once")==0 ){
9986          p->flgProgress |= SHELL_PROGRESS_ONCE;
9987          continue;
9988        }
9989        if( cli_strcmp(z,"limit")==0 ){
9990          if( i+1>=nArg ){
9991            utf8_printf(stderr, "Error: missing argument on --limit\n");
9992            rc = 1;
9993            goto meta_command_exit;
9994          }else{
9995            p->mxProgress = (int)integerValue(azArg[++i]);
9996          }
9997          continue;
9998        }
9999        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
10000        rc = 1;
10001        goto meta_command_exit;
10002      }else{
10003        nn = (int)integerValue(z);
10004      }
10005    }
10006    open_db(p, 0);
10007    sqlite3_progress_handler(p->db, nn, progress_handler, p);
10008  }else
10009#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10010
10011  if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
10012    if( nArg >= 2) {
10013      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
10014    }
10015    if( nArg >= 3) {
10016      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
10017    }
10018  }else
10019
10020#ifndef SQLITE_SHELL_FIDDLE
10021  if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
10022    rc = 2;
10023  }else
10024#endif
10025
10026#ifndef SQLITE_SHELL_FIDDLE
10027  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
10028    FILE *inSaved = p->in;
10029    int savedLineno = p->lineno;
10030    failIfSafeMode(p, "cannot run .read in safe mode");
10031    if( nArg!=2 ){
10032      raw_printf(stderr, "Usage: .read FILE\n");
10033      rc = 1;
10034      goto meta_command_exit;
10035    }
10036    if( azArg[1][0]=='|' ){
10037#ifdef SQLITE_OMIT_POPEN
10038      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
10039      rc = 1;
10040      p->out = stdout;
10041#else
10042      p->in = popen(azArg[1]+1, "r");
10043      if( p->in==0 ){
10044        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
10045        rc = 1;
10046      }else{
10047        rc = process_input(p);
10048        pclose(p->in);
10049      }
10050#endif
10051    }else if( (p->in = openChrSource(azArg[1]))==0 ){
10052      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
10053      rc = 1;
10054    }else{
10055      rc = process_input(p);
10056      fclose(p->in);
10057    }
10058    p->in = inSaved;
10059    p->lineno = savedLineno;
10060  }else
10061#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10062
10063#ifndef SQLITE_SHELL_FIDDLE
10064  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
10065    const char *zSrcFile;
10066    const char *zDb;
10067    sqlite3 *pSrc;
10068    sqlite3_backup *pBackup;
10069    int nTimeout = 0;
10070
10071    failIfSafeMode(p, "cannot run .restore in safe mode");
10072    if( nArg==2 ){
10073      zSrcFile = azArg[1];
10074      zDb = "main";
10075    }else if( nArg==3 ){
10076      zSrcFile = azArg[2];
10077      zDb = azArg[1];
10078    }else{
10079      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
10080      rc = 1;
10081      goto meta_command_exit;
10082    }
10083    rc = sqlite3_open(zSrcFile, &pSrc);
10084    if( rc!=SQLITE_OK ){
10085      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
10086      close_db(pSrc);
10087      return 1;
10088    }
10089    open_db(p, 0);
10090    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
10091    if( pBackup==0 ){
10092      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10093      close_db(pSrc);
10094      return 1;
10095    }
10096    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
10097          || rc==SQLITE_BUSY  ){
10098      if( rc==SQLITE_BUSY ){
10099        if( nTimeout++ >= 3 ) break;
10100        sqlite3_sleep(100);
10101      }
10102    }
10103    sqlite3_backup_finish(pBackup);
10104    if( rc==SQLITE_DONE ){
10105      rc = 0;
10106    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
10107      raw_printf(stderr, "Error: source database is busy\n");
10108      rc = 1;
10109    }else{
10110      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10111      rc = 1;
10112    }
10113    close_db(pSrc);
10114  }else
10115#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10116
10117  if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
10118    if( nArg==2 ){
10119      p->scanstatsOn = (u8)booleanValue(azArg[1]);
10120#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10121      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
10122#endif
10123    }else{
10124      raw_printf(stderr, "Usage: .scanstats on|off\n");
10125      rc = 1;
10126    }
10127  }else
10128
10129  if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
10130    ShellText sSelect;
10131    ShellState data;
10132    char *zErrMsg = 0;
10133    const char *zDiv = "(";
10134    const char *zName = 0;
10135    int iSchema = 0;
10136    int bDebug = 0;
10137    int bNoSystemTabs = 0;
10138    int ii;
10139
10140    open_db(p, 0);
10141    memcpy(&data, p, sizeof(data));
10142    data.showHeader = 0;
10143    data.cMode = data.mode = MODE_Semi;
10144    initText(&sSelect);
10145    for(ii=1; ii<nArg; ii++){
10146      if( optionMatch(azArg[ii],"indent") ){
10147        data.cMode = data.mode = MODE_Pretty;
10148      }else if( optionMatch(azArg[ii],"debug") ){
10149        bDebug = 1;
10150      }else if( optionMatch(azArg[ii],"nosys") ){
10151        bNoSystemTabs = 1;
10152      }else if( azArg[ii][0]=='-' ){
10153        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
10154        rc = 1;
10155        goto meta_command_exit;
10156      }else if( zName==0 ){
10157        zName = azArg[ii];
10158      }else{
10159        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10160        rc = 1;
10161        goto meta_command_exit;
10162      }
10163    }
10164    if( zName!=0 ){
10165      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
10166                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
10167                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
10168                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
10169      if( isSchema ){
10170        char *new_argv[2], *new_colv[2];
10171        new_argv[0] = sqlite3_mprintf(
10172                      "CREATE TABLE %s (\n"
10173                      "  type text,\n"
10174                      "  name text,\n"
10175                      "  tbl_name text,\n"
10176                      "  rootpage integer,\n"
10177                      "  sql text\n"
10178                      ")", zName);
10179        shell_check_oom(new_argv[0]);
10180        new_argv[1] = 0;
10181        new_colv[0] = "sql";
10182        new_colv[1] = 0;
10183        callback(&data, 1, new_argv, new_colv);
10184        sqlite3_free(new_argv[0]);
10185      }
10186    }
10187    if( zDiv ){
10188      sqlite3_stmt *pStmt = 0;
10189      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
10190                              -1, &pStmt, 0);
10191      if( rc ){
10192        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10193        sqlite3_finalize(pStmt);
10194        rc = 1;
10195        goto meta_command_exit;
10196      }
10197      appendText(&sSelect, "SELECT sql FROM", 0);
10198      iSchema = 0;
10199      while( sqlite3_step(pStmt)==SQLITE_ROW ){
10200        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
10201        char zScNum[30];
10202        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
10203        appendText(&sSelect, zDiv, 0);
10204        zDiv = " UNION ALL ";
10205        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
10206        if( sqlite3_stricmp(zDb, "main")!=0 ){
10207          appendText(&sSelect, zDb, '\'');
10208        }else{
10209          appendText(&sSelect, "NULL", 0);
10210        }
10211        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10212        appendText(&sSelect, zScNum, 0);
10213        appendText(&sSelect, " AS snum, ", 0);
10214        appendText(&sSelect, zDb, '\'');
10215        appendText(&sSelect, " AS sname FROM ", 0);
10216        appendText(&sSelect, zDb, quoteChar(zDb));
10217        appendText(&sSelect, ".sqlite_schema", 0);
10218      }
10219      sqlite3_finalize(pStmt);
10220#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10221      if( zName ){
10222        appendText(&sSelect,
10223           " UNION ALL SELECT shell_module_schema(name),"
10224           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10225        0);
10226      }
10227#endif
10228      appendText(&sSelect, ") WHERE ", 0);
10229      if( zName ){
10230        char *zQarg = sqlite3_mprintf("%Q", zName);
10231        int bGlob;
10232        shell_check_oom(zQarg);
10233        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10234                strchr(zName, '[') != 0;
10235        if( strchr(zName, '.') ){
10236          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10237        }else{
10238          appendText(&sSelect, "lower(tbl_name)", 0);
10239        }
10240        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10241        appendText(&sSelect, zQarg, 0);
10242        if( !bGlob ){
10243          appendText(&sSelect, " ESCAPE '\\' ", 0);
10244        }
10245        appendText(&sSelect, " AND ", 0);
10246        sqlite3_free(zQarg);
10247      }
10248      if( bNoSystemTabs ){
10249        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10250      }
10251      appendText(&sSelect, "sql IS NOT NULL"
10252                           " ORDER BY snum, rowid", 0);
10253      if( bDebug ){
10254        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
10255      }else{
10256        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10257      }
10258      freeText(&sSelect);
10259    }
10260    if( zErrMsg ){
10261      utf8_printf(stderr,"Error: %s\n", zErrMsg);
10262      sqlite3_free(zErrMsg);
10263      rc = 1;
10264    }else if( rc != SQLITE_OK ){
10265      raw_printf(stderr,"Error: querying schema information\n");
10266      rc = 1;
10267    }else{
10268      rc = 0;
10269    }
10270  }else
10271
10272  if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
10273   || (c=='t' && n==9  && cli_strncmp(azArg[0], "treetrace", n)==0)
10274  ){
10275    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10276    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10277  }else
10278
10279#if defined(SQLITE_ENABLE_SESSION)
10280  if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
10281    struct AuxDb *pAuxDb = p->pAuxDb;
10282    OpenSession *pSession = &pAuxDb->aSession[0];
10283    char **azCmd = &azArg[1];
10284    int iSes = 0;
10285    int nCmd = nArg - 1;
10286    int i;
10287    if( nArg<=1 ) goto session_syntax_error;
10288    open_db(p, 0);
10289    if( nArg>=3 ){
10290      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10291        if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10292      }
10293      if( iSes<pAuxDb->nSession ){
10294        pSession = &pAuxDb->aSession[iSes];
10295        azCmd++;
10296        nCmd--;
10297      }else{
10298        pSession = &pAuxDb->aSession[0];
10299        iSes = 0;
10300      }
10301    }
10302
10303    /* .session attach TABLE
10304    ** Invoke the sqlite3session_attach() interface to attach a particular
10305    ** table so that it is never filtered.
10306    */
10307    if( cli_strcmp(azCmd[0],"attach")==0 ){
10308      if( nCmd!=2 ) goto session_syntax_error;
10309      if( pSession->p==0 ){
10310        session_not_open:
10311        raw_printf(stderr, "ERROR: No sessions are open\n");
10312      }else{
10313        rc = sqlite3session_attach(pSession->p, azCmd[1]);
10314        if( rc ){
10315          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
10316          rc = 0;
10317        }
10318      }
10319    }else
10320
10321    /* .session changeset FILE
10322    ** .session patchset FILE
10323    ** Write a changeset or patchset into a file.  The file is overwritten.
10324    */
10325    if( cli_strcmp(azCmd[0],"changeset")==0
10326     || cli_strcmp(azCmd[0],"patchset")==0
10327    ){
10328      FILE *out = 0;
10329      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10330      if( nCmd!=2 ) goto session_syntax_error;
10331      if( pSession->p==0 ) goto session_not_open;
10332      out = fopen(azCmd[1], "wb");
10333      if( out==0 ){
10334        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
10335                    azCmd[1]);
10336      }else{
10337        int szChng;
10338        void *pChng;
10339        if( azCmd[0][0]=='c' ){
10340          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10341        }else{
10342          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10343        }
10344        if( rc ){
10345          printf("Error: error code %d\n", rc);
10346          rc = 0;
10347        }
10348        if( pChng
10349          && fwrite(pChng, szChng, 1, out)!=1 ){
10350          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
10351                  szChng);
10352        }
10353        sqlite3_free(pChng);
10354        fclose(out);
10355      }
10356    }else
10357
10358    /* .session close
10359    ** Close the identified session
10360    */
10361    if( cli_strcmp(azCmd[0], "close")==0 ){
10362      if( nCmd!=1 ) goto session_syntax_error;
10363      if( pAuxDb->nSession ){
10364        session_close(pSession);
10365        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10366      }
10367    }else
10368
10369    /* .session enable ?BOOLEAN?
10370    ** Query or set the enable flag
10371    */
10372    if( cli_strcmp(azCmd[0], "enable")==0 ){
10373      int ii;
10374      if( nCmd>2 ) goto session_syntax_error;
10375      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10376      if( pAuxDb->nSession ){
10377        ii = sqlite3session_enable(pSession->p, ii);
10378        utf8_printf(p->out, "session %s enable flag = %d\n",
10379                    pSession->zName, ii);
10380      }
10381    }else
10382
10383    /* .session filter GLOB ....
10384    ** Set a list of GLOB patterns of table names to be excluded.
10385    */
10386    if( cli_strcmp(azCmd[0], "filter")==0 ){
10387      int ii, nByte;
10388      if( nCmd<2 ) goto session_syntax_error;
10389      if( pAuxDb->nSession ){
10390        for(ii=0; ii<pSession->nFilter; ii++){
10391          sqlite3_free(pSession->azFilter[ii]);
10392        }
10393        sqlite3_free(pSession->azFilter);
10394        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10395        pSession->azFilter = sqlite3_malloc( nByte );
10396        if( pSession->azFilter==0 ){
10397          raw_printf(stderr, "Error: out or memory\n");
10398          exit(1);
10399        }
10400        for(ii=1; ii<nCmd; ii++){
10401          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10402          shell_check_oom(x);
10403        }
10404        pSession->nFilter = ii-1;
10405      }
10406    }else
10407
10408    /* .session indirect ?BOOLEAN?
10409    ** Query or set the indirect flag
10410    */
10411    if( cli_strcmp(azCmd[0], "indirect")==0 ){
10412      int ii;
10413      if( nCmd>2 ) goto session_syntax_error;
10414      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10415      if( pAuxDb->nSession ){
10416        ii = sqlite3session_indirect(pSession->p, ii);
10417        utf8_printf(p->out, "session %s indirect flag = %d\n",
10418                    pSession->zName, ii);
10419      }
10420    }else
10421
10422    /* .session isempty
10423    ** Determine if the session is empty
10424    */
10425    if( cli_strcmp(azCmd[0], "isempty")==0 ){
10426      int ii;
10427      if( nCmd!=1 ) goto session_syntax_error;
10428      if( pAuxDb->nSession ){
10429        ii = sqlite3session_isempty(pSession->p);
10430        utf8_printf(p->out, "session %s isempty flag = %d\n",
10431                    pSession->zName, ii);
10432      }
10433    }else
10434
10435    /* .session list
10436    ** List all currently open sessions
10437    */
10438    if( cli_strcmp(azCmd[0],"list")==0 ){
10439      for(i=0; i<pAuxDb->nSession; i++){
10440        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
10441      }
10442    }else
10443
10444    /* .session open DB NAME
10445    ** Open a new session called NAME on the attached database DB.
10446    ** DB is normally "main".
10447    */
10448    if( cli_strcmp(azCmd[0],"open")==0 ){
10449      char *zName;
10450      if( nCmd!=3 ) goto session_syntax_error;
10451      zName = azCmd[2];
10452      if( zName[0]==0 ) goto session_syntax_error;
10453      for(i=0; i<pAuxDb->nSession; i++){
10454        if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10455          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
10456          goto meta_command_exit;
10457        }
10458      }
10459      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10460        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10461        goto meta_command_exit;
10462      }
10463      pSession = &pAuxDb->aSession[pAuxDb->nSession];
10464      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10465      if( rc ){
10466        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
10467        rc = 0;
10468        goto meta_command_exit;
10469      }
10470      pSession->nFilter = 0;
10471      sqlite3session_table_filter(pSession->p, session_filter, pSession);
10472      pAuxDb->nSession++;
10473      pSession->zName = sqlite3_mprintf("%s", zName);
10474      shell_check_oom(pSession->zName);
10475    }else
10476    /* If no command name matches, show a syntax error */
10477    session_syntax_error:
10478    showHelp(p->out, "session");
10479  }else
10480#endif
10481
10482#ifdef SQLITE_DEBUG
10483  /* Undocumented commands for internal testing.  Subject to change
10484  ** without notice. */
10485  if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
10486    if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10487      int i, v;
10488      for(i=1; i<nArg; i++){
10489        v = booleanValue(azArg[i]);
10490        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
10491      }
10492    }
10493    if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
10494      int i; sqlite3_int64 v;
10495      for(i=1; i<nArg; i++){
10496        char zBuf[200];
10497        v = integerValue(azArg[i]);
10498        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10499        utf8_printf(p->out, "%s", zBuf);
10500      }
10501    }
10502  }else
10503#endif
10504
10505  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
10506    int bIsInit = 0;         /* True to initialize the SELFTEST table */
10507    int bVerbose = 0;        /* Verbose output */
10508    int bSelftestExists;     /* True if SELFTEST already exists */
10509    int i, k;                /* Loop counters */
10510    int nTest = 0;           /* Number of tests runs */
10511    int nErr = 0;            /* Number of errors seen */
10512    ShellText str;           /* Answer for a query */
10513    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10514
10515    open_db(p,0);
10516    for(i=1; i<nArg; i++){
10517      const char *z = azArg[i];
10518      if( z[0]=='-' && z[1]=='-' ) z++;
10519      if( cli_strcmp(z,"-init")==0 ){
10520        bIsInit = 1;
10521      }else
10522      if( cli_strcmp(z,"-v")==0 ){
10523        bVerbose++;
10524      }else
10525      {
10526        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10527                    azArg[i], azArg[0]);
10528        raw_printf(stderr, "Should be one of: --init -v\n");
10529        rc = 1;
10530        goto meta_command_exit;
10531      }
10532    }
10533    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10534           != SQLITE_OK ){
10535      bSelftestExists = 0;
10536    }else{
10537      bSelftestExists = 1;
10538    }
10539    if( bIsInit ){
10540      createSelftestTable(p);
10541      bSelftestExists = 1;
10542    }
10543    initText(&str);
10544    appendText(&str, "x", 0);
10545    for(k=bSelftestExists; k>=0; k--){
10546      if( k==1 ){
10547        rc = sqlite3_prepare_v2(p->db,
10548            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10549            -1, &pStmt, 0);
10550      }else{
10551        rc = sqlite3_prepare_v2(p->db,
10552          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10553          "      (1,'run','PRAGMA integrity_check','ok')",
10554          -1, &pStmt, 0);
10555      }
10556      if( rc ){
10557        raw_printf(stderr, "Error querying the selftest table\n");
10558        rc = 1;
10559        sqlite3_finalize(pStmt);
10560        goto meta_command_exit;
10561      }
10562      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10563        int tno = sqlite3_column_int(pStmt, 0);
10564        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10565        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10566        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10567
10568        if( zOp==0 ) continue;
10569        if( zSql==0 ) continue;
10570        if( zAns==0 ) continue;
10571        k = 0;
10572        if( bVerbose>0 ){
10573          printf("%d: %s %s\n", tno, zOp, zSql);
10574        }
10575        if( cli_strcmp(zOp,"memo")==0 ){
10576          utf8_printf(p->out, "%s\n", zSql);
10577        }else
10578        if( cli_strcmp(zOp,"run")==0 ){
10579          char *zErrMsg = 0;
10580          str.n = 0;
10581          str.z[0] = 0;
10582          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10583          nTest++;
10584          if( bVerbose ){
10585            utf8_printf(p->out, "Result: %s\n", str.z);
10586          }
10587          if( rc || zErrMsg ){
10588            nErr++;
10589            rc = 1;
10590            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
10591            sqlite3_free(zErrMsg);
10592          }else if( cli_strcmp(zAns,str.z)!=0 ){
10593            nErr++;
10594            rc = 1;
10595            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10596            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
10597          }
10598        }else
10599        {
10600          utf8_printf(stderr,
10601            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10602          rc = 1;
10603          break;
10604        }
10605      } /* End loop over rows of content from SELFTEST */
10606      sqlite3_finalize(pStmt);
10607    } /* End loop over k */
10608    freeText(&str);
10609    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10610  }else
10611
10612  if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
10613    if( nArg<2 || nArg>3 ){
10614      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10615      rc = 1;
10616    }
10617    if( nArg>=2 ){
10618      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10619                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10620    }
10621    if( nArg>=3 ){
10622      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10623                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10624    }
10625  }else
10626
10627  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
10628    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
10629    int i;                   /* Loop counter */
10630    int bSchema = 0;         /* Also hash the schema */
10631    int bSeparate = 0;       /* Hash each table separately */
10632    int iSize = 224;         /* Hash algorithm to use */
10633    int bDebug = 0;          /* Only show the query that would have run */
10634    sqlite3_stmt *pStmt;     /* For querying tables names */
10635    char *zSql;              /* SQL to be run */
10636    char *zSep;              /* Separator */
10637    ShellText sSql;          /* Complete SQL for the query to run the hash */
10638    ShellText sQuery;        /* Set of queries used to read all content */
10639    open_db(p, 0);
10640    for(i=1; i<nArg; i++){
10641      const char *z = azArg[i];
10642      if( z[0]=='-' ){
10643        z++;
10644        if( z[0]=='-' ) z++;
10645        if( cli_strcmp(z,"schema")==0 ){
10646          bSchema = 1;
10647        }else
10648        if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
10649         || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
10650        ){
10651          iSize = atoi(&z[5]);
10652        }else
10653        if( cli_strcmp(z,"debug")==0 ){
10654          bDebug = 1;
10655        }else
10656        {
10657          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10658                      azArg[i], azArg[0]);
10659          showHelp(p->out, azArg[0]);
10660          rc = 1;
10661          goto meta_command_exit;
10662        }
10663      }else if( zLike ){
10664        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10665        rc = 1;
10666        goto meta_command_exit;
10667      }else{
10668        zLike = z;
10669        bSeparate = 1;
10670        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10671      }
10672    }
10673    if( bSchema ){
10674      zSql = "SELECT lower(name) FROM sqlite_schema"
10675             " WHERE type='table' AND coalesce(rootpage,0)>1"
10676             " UNION ALL SELECT 'sqlite_schema'"
10677             " ORDER BY 1 collate nocase";
10678    }else{
10679      zSql = "SELECT lower(name) FROM sqlite_schema"
10680             " WHERE type='table' AND coalesce(rootpage,0)>1"
10681             " AND name NOT LIKE 'sqlite_%'"
10682             " ORDER BY 1 collate nocase";
10683    }
10684    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10685    initText(&sQuery);
10686    initText(&sSql);
10687    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10688    zSep = "VALUES(";
10689    while( SQLITE_ROW==sqlite3_step(pStmt) ){
10690      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10691      if( zTab==0 ) continue;
10692      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10693      if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
10694        appendText(&sQuery,"SELECT * FROM ", 0);
10695        appendText(&sQuery,zTab,'"');
10696        appendText(&sQuery," NOT INDEXED;", 0);
10697      }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
10698        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10699                           " ORDER BY name;", 0);
10700      }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
10701        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10702                           " ORDER BY name;", 0);
10703      }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
10704        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10705                           " ORDER BY tbl,idx;", 0);
10706      }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
10707        appendText(&sQuery, "SELECT * FROM ", 0);
10708        appendText(&sQuery, zTab, 0);
10709        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10710      }
10711      appendText(&sSql, zSep, 0);
10712      appendText(&sSql, sQuery.z, '\'');
10713      sQuery.n = 0;
10714      appendText(&sSql, ",", 0);
10715      appendText(&sSql, zTab, '\'');
10716      zSep = "),(";
10717    }
10718    sqlite3_finalize(pStmt);
10719    if( bSeparate ){
10720      zSql = sqlite3_mprintf(
10721          "%s))"
10722          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10723          "   FROM [sha3sum$query]",
10724          sSql.z, iSize);
10725    }else{
10726      zSql = sqlite3_mprintf(
10727          "%s))"
10728          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10729          "   FROM [sha3sum$query]",
10730          sSql.z, iSize);
10731    }
10732    shell_check_oom(zSql);
10733    freeText(&sQuery);
10734    freeText(&sSql);
10735    if( bDebug ){
10736      utf8_printf(p->out, "%s\n", zSql);
10737    }else{
10738      shell_exec(p, zSql, 0);
10739    }
10740    sqlite3_free(zSql);
10741  }else
10742
10743#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10744  if( c=='s'
10745   && (cli_strncmp(azArg[0], "shell", n)==0
10746       || cli_strncmp(azArg[0],"system",n)==0)
10747  ){
10748    char *zCmd;
10749    int i, x;
10750    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10751    if( nArg<2 ){
10752      raw_printf(stderr, "Usage: .system COMMAND\n");
10753      rc = 1;
10754      goto meta_command_exit;
10755    }
10756    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10757    for(i=2; i<nArg && zCmd!=0; i++){
10758      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10759                             zCmd, azArg[i]);
10760    }
10761    x = zCmd!=0 ? system(zCmd) : 1;
10762    sqlite3_free(zCmd);
10763    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10764  }else
10765#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10766
10767  if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
10768    static const char *azBool[] = { "off", "on", "trigger", "full"};
10769    const char *zOut;
10770    int i;
10771    if( nArg!=1 ){
10772      raw_printf(stderr, "Usage: .show\n");
10773      rc = 1;
10774      goto meta_command_exit;
10775    }
10776    utf8_printf(p->out, "%12.12s: %s\n","echo",
10777                azBool[ShellHasFlag(p, SHFLG_Echo)]);
10778    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10779    utf8_printf(p->out, "%12.12s: %s\n","explain",
10780         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10781    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10782    if( p->mode==MODE_Column
10783     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10784    ){
10785      utf8_printf
10786        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10787         modeDescr[p->mode], p->cmOpts.iWrap,
10788         p->cmOpts.bWordWrap ? "on" : "off",
10789         p->cmOpts.bQuote ? "" : "no");
10790    }else{
10791      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10792    }
10793    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10794      output_c_string(p->out, p->nullValue);
10795      raw_printf(p->out, "\n");
10796    utf8_printf(p->out,"%12.12s: %s\n","output",
10797            strlen30(p->outfile) ? p->outfile : "stdout");
10798    utf8_printf(p->out,"%12.12s: ", "colseparator");
10799      output_c_string(p->out, p->colSeparator);
10800      raw_printf(p->out, "\n");
10801    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10802      output_c_string(p->out, p->rowSeparator);
10803      raw_printf(p->out, "\n");
10804    switch( p->statsOn ){
10805      case 0:  zOut = "off";     break;
10806      default: zOut = "on";      break;
10807      case 2:  zOut = "stmt";    break;
10808      case 3:  zOut = "vmstep";  break;
10809    }
10810    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10811    utf8_printf(p->out, "%12.12s: ", "width");
10812    for (i=0;i<p->nWidth;i++) {
10813      raw_printf(p->out, "%d ", p->colWidth[i]);
10814    }
10815    raw_printf(p->out, "\n");
10816    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10817                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10818  }else
10819
10820  if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
10821    if( nArg==2 ){
10822      if( cli_strcmp(azArg[1],"stmt")==0 ){
10823        p->statsOn = 2;
10824      }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
10825        p->statsOn = 3;
10826      }else{
10827        p->statsOn = (u8)booleanValue(azArg[1]);
10828      }
10829    }else if( nArg==1 ){
10830      display_stats(p->db, p, 0);
10831    }else{
10832      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10833      rc = 1;
10834    }
10835  }else
10836
10837  if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
10838   || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
10839                 || cli_strncmp(azArg[0], "indexes", n)==0) )
10840  ){
10841    sqlite3_stmt *pStmt;
10842    char **azResult;
10843    int nRow, nAlloc;
10844    int ii;
10845    ShellText s;
10846    initText(&s);
10847    open_db(p, 0);
10848    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10849    if( rc ){
10850      sqlite3_finalize(pStmt);
10851      return shellDatabaseError(p->db);
10852    }
10853
10854    if( nArg>2 && c=='i' ){
10855      /* It is an historical accident that the .indexes command shows an error
10856      ** when called with the wrong number of arguments whereas the .tables
10857      ** command does not. */
10858      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10859      rc = 1;
10860      sqlite3_finalize(pStmt);
10861      goto meta_command_exit;
10862    }
10863    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10864      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10865      if( zDbName==0 ) continue;
10866      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10867      if( sqlite3_stricmp(zDbName, "main")==0 ){
10868        appendText(&s, "SELECT name FROM ", 0);
10869      }else{
10870        appendText(&s, "SELECT ", 0);
10871        appendText(&s, zDbName, '\'');
10872        appendText(&s, "||'.'||name FROM ", 0);
10873      }
10874      appendText(&s, zDbName, '"');
10875      appendText(&s, ".sqlite_schema ", 0);
10876      if( c=='t' ){
10877        appendText(&s," WHERE type IN ('table','view')"
10878                      "   AND name NOT LIKE 'sqlite_%'"
10879                      "   AND name LIKE ?1", 0);
10880      }else{
10881        appendText(&s," WHERE type='index'"
10882                      "   AND tbl_name LIKE ?1", 0);
10883      }
10884    }
10885    rc = sqlite3_finalize(pStmt);
10886    if( rc==SQLITE_OK ){
10887      appendText(&s, " ORDER BY 1", 0);
10888      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10889    }
10890    freeText(&s);
10891    if( rc ) return shellDatabaseError(p->db);
10892
10893    /* Run the SQL statement prepared by the above block. Store the results
10894    ** as an array of nul-terminated strings in azResult[].  */
10895    nRow = nAlloc = 0;
10896    azResult = 0;
10897    if( nArg>1 ){
10898      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10899    }else{
10900      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10901    }
10902    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10903      if( nRow>=nAlloc ){
10904        char **azNew;
10905        int n2 = nAlloc*2 + 10;
10906        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10907        shell_check_oom(azNew);
10908        nAlloc = n2;
10909        azResult = azNew;
10910      }
10911      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10912      shell_check_oom(azResult[nRow]);
10913      nRow++;
10914    }
10915    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10916      rc = shellDatabaseError(p->db);
10917    }
10918
10919    /* Pretty-print the contents of array azResult[] to the output */
10920    if( rc==0 && nRow>0 ){
10921      int len, maxlen = 0;
10922      int i, j;
10923      int nPrintCol, nPrintRow;
10924      for(i=0; i<nRow; i++){
10925        len = strlen30(azResult[i]);
10926        if( len>maxlen ) maxlen = len;
10927      }
10928      nPrintCol = 80/(maxlen+2);
10929      if( nPrintCol<1 ) nPrintCol = 1;
10930      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10931      for(i=0; i<nPrintRow; i++){
10932        for(j=i; j<nRow; j+=nPrintRow){
10933          char *zSp = j<nPrintRow ? "" : "  ";
10934          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10935                      azResult[j] ? azResult[j]:"");
10936        }
10937        raw_printf(p->out, "\n");
10938      }
10939    }
10940
10941    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10942    sqlite3_free(azResult);
10943  }else
10944
10945#ifndef SQLITE_SHELL_FIDDLE
10946  /* Begin redirecting output to the file "testcase-out.txt" */
10947  if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
10948    output_reset(p);
10949    p->out = output_file_open("testcase-out.txt", 0);
10950    if( p->out==0 ){
10951      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10952    }
10953    if( nArg>=2 ){
10954      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10955    }else{
10956      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10957    }
10958  }else
10959#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10960
10961#ifndef SQLITE_UNTESTABLE
10962  if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
10963    static const struct {
10964       const char *zCtrlName;   /* Name of a test-control option */
10965       int ctrlCode;            /* Integer code for that option */
10966       int unSafe;              /* Not valid for --safe mode */
10967       const char *zUsage;      /* Usage notes */
10968    } aCtrl[] = {
10969      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10970      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10971    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10972    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10973      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10974      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10975    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10976      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10977      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10978      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10979      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10980      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10981#ifdef YYCOVERAGE
10982      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10983#endif
10984      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10985      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10986      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10987      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10988      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10989      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10990      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10991    };
10992    int testctrl = -1;
10993    int iCtrl = -1;
10994    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10995    int isOk = 0;
10996    int i, n2;
10997    const char *zCmd = 0;
10998
10999    open_db(p, 0);
11000    zCmd = nArg>=2 ? azArg[1] : "help";
11001
11002    /* The argument can optionally begin with "-" or "--" */
11003    if( zCmd[0]=='-' && zCmd[1] ){
11004      zCmd++;
11005      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
11006    }
11007
11008    /* --help lists all test-controls */
11009    if( cli_strcmp(zCmd,"help")==0 ){
11010      utf8_printf(p->out, "Available test-controls:\n");
11011      for(i=0; i<ArraySize(aCtrl); i++){
11012        utf8_printf(p->out, "  .testctrl %s %s\n",
11013                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
11014      }
11015      rc = 1;
11016      goto meta_command_exit;
11017    }
11018
11019    /* convert testctrl text option to value. allow any unique prefix
11020    ** of the option name, or a numerical value. */
11021    n2 = strlen30(zCmd);
11022    for(i=0; i<ArraySize(aCtrl); i++){
11023      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
11024        if( testctrl<0 ){
11025          testctrl = aCtrl[i].ctrlCode;
11026          iCtrl = i;
11027        }else{
11028          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
11029                              "Use \".testctrl --help\" for help\n", zCmd);
11030          rc = 1;
11031          goto meta_command_exit;
11032        }
11033      }
11034    }
11035    if( testctrl<0 ){
11036      utf8_printf(stderr,"Error: unknown test-control: %s\n"
11037                         "Use \".testctrl --help\" for help\n", zCmd);
11038    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
11039      utf8_printf(stderr,
11040         "line %d: \".testctrl %s\" may not be used in safe mode\n",
11041         p->lineno, aCtrl[iCtrl].zCtrlName);
11042      exit(1);
11043    }else{
11044      switch(testctrl){
11045
11046        /* sqlite3_test_control(int, db, int) */
11047        case SQLITE_TESTCTRL_OPTIMIZATIONS:
11048          if( nArg==3 ){
11049            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
11050            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11051            isOk = 3;
11052          }
11053          break;
11054
11055        /* sqlite3_test_control(int) */
11056        case SQLITE_TESTCTRL_PRNG_SAVE:
11057        case SQLITE_TESTCTRL_PRNG_RESTORE:
11058        case SQLITE_TESTCTRL_BYTEORDER:
11059          if( nArg==2 ){
11060            rc2 = sqlite3_test_control(testctrl);
11061            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
11062          }
11063          break;
11064
11065        /* sqlite3_test_control(int, uint) */
11066        case SQLITE_TESTCTRL_PENDING_BYTE:
11067          if( nArg==3 ){
11068            unsigned int opt = (unsigned int)integerValue(azArg[2]);
11069            rc2 = sqlite3_test_control(testctrl, opt);
11070            isOk = 3;
11071          }
11072          break;
11073
11074        /* sqlite3_test_control(int, int, sqlite3*) */
11075        case SQLITE_TESTCTRL_PRNG_SEED:
11076          if( nArg==3 || nArg==4 ){
11077            int ii = (int)integerValue(azArg[2]);
11078            sqlite3 *db;
11079            if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
11080              sqlite3_randomness(sizeof(ii),&ii);
11081              printf("-- random seed: %d\n", ii);
11082            }
11083            if( nArg==3 ){
11084              db = 0;
11085            }else{
11086              db = p->db;
11087              /* Make sure the schema has been loaded */
11088              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
11089            }
11090            rc2 = sqlite3_test_control(testctrl, ii, db);
11091            isOk = 3;
11092          }
11093          break;
11094
11095        /* sqlite3_test_control(int, int) */
11096        case SQLITE_TESTCTRL_ASSERT:
11097        case SQLITE_TESTCTRL_ALWAYS:
11098          if( nArg==3 ){
11099            int opt = booleanValue(azArg[2]);
11100            rc2 = sqlite3_test_control(testctrl, opt);
11101            isOk = 1;
11102          }
11103          break;
11104
11105        /* sqlite3_test_control(int, int) */
11106        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
11107        case SQLITE_TESTCTRL_NEVER_CORRUPT:
11108          if( nArg==3 ){
11109            int opt = booleanValue(azArg[2]);
11110            rc2 = sqlite3_test_control(testctrl, opt);
11111            isOk = 3;
11112          }
11113          break;
11114
11115        /* sqlite3_test_control(sqlite3*) */
11116        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
11117          rc2 = sqlite3_test_control(testctrl, p->db);
11118          isOk = 3;
11119          break;
11120
11121        case SQLITE_TESTCTRL_IMPOSTER:
11122          if( nArg==5 ){
11123            rc2 = sqlite3_test_control(testctrl, p->db,
11124                          azArg[2],
11125                          integerValue(azArg[3]),
11126                          integerValue(azArg[4]));
11127            isOk = 3;
11128          }
11129          break;
11130
11131        case SQLITE_TESTCTRL_SEEK_COUNT: {
11132          u64 x = 0;
11133          rc2 = sqlite3_test_control(testctrl, p->db, &x);
11134          utf8_printf(p->out, "%llu\n", x);
11135          isOk = 3;
11136          break;
11137        }
11138
11139#ifdef YYCOVERAGE
11140        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
11141          if( nArg==2 ){
11142            sqlite3_test_control(testctrl, p->out);
11143            isOk = 3;
11144          }
11145          break;
11146        }
11147#endif
11148#ifdef SQLITE_DEBUG
11149        case SQLITE_TESTCTRL_TUNE: {
11150          if( nArg==4 ){
11151            int id = (int)integerValue(azArg[2]);
11152            int val = (int)integerValue(azArg[3]);
11153            sqlite3_test_control(testctrl, id, &val);
11154            isOk = 3;
11155          }else if( nArg==3 ){
11156            int id = (int)integerValue(azArg[2]);
11157            sqlite3_test_control(testctrl, -id, &rc2);
11158            isOk = 1;
11159          }else if( nArg==2 ){
11160            int id = 1;
11161            while(1){
11162              int val = 0;
11163              rc2 = sqlite3_test_control(testctrl, -id, &val);
11164              if( rc2!=SQLITE_OK ) break;
11165              if( id>1 ) utf8_printf(p->out, "  ");
11166              utf8_printf(p->out, "%d: %d", id, val);
11167              id++;
11168            }
11169            if( id>1 ) utf8_printf(p->out, "\n");
11170            isOk = 3;
11171          }
11172          break;
11173        }
11174#endif
11175        case SQLITE_TESTCTRL_SORTER_MMAP:
11176          if( nArg==3 ){
11177            int opt = (unsigned int)integerValue(azArg[2]);
11178            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11179            isOk = 3;
11180          }
11181          break;
11182      }
11183    }
11184    if( isOk==0 && iCtrl>=0 ){
11185      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11186      rc = 1;
11187    }else if( isOk==1 ){
11188      raw_printf(p->out, "%d\n", rc2);
11189    }else if( isOk==2 ){
11190      raw_printf(p->out, "0x%08x\n", rc2);
11191    }
11192  }else
11193#endif /* !defined(SQLITE_UNTESTABLE) */
11194
11195  if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
11196    open_db(p, 0);
11197    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11198  }else
11199
11200  if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
11201    if( nArg==2 ){
11202      enableTimer = booleanValue(azArg[1]);
11203      if( enableTimer && !HAS_TIMER ){
11204        raw_printf(stderr, "Error: timer not available on this system.\n");
11205        enableTimer = 0;
11206      }
11207    }else{
11208      raw_printf(stderr, "Usage: .timer on|off\n");
11209      rc = 1;
11210    }
11211  }else
11212
11213#ifndef SQLITE_OMIT_TRACE
11214  if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
11215    int mType = 0;
11216    int jj;
11217    open_db(p, 0);
11218    for(jj=1; jj<nArg; jj++){
11219      const char *z = azArg[jj];
11220      if( z[0]=='-' ){
11221        if( optionMatch(z, "expanded") ){
11222          p->eTraceType = SHELL_TRACE_EXPANDED;
11223        }
11224#ifdef SQLITE_ENABLE_NORMALIZE
11225        else if( optionMatch(z, "normalized") ){
11226          p->eTraceType = SHELL_TRACE_NORMALIZED;
11227        }
11228#endif
11229        else if( optionMatch(z, "plain") ){
11230          p->eTraceType = SHELL_TRACE_PLAIN;
11231        }
11232        else if( optionMatch(z, "profile") ){
11233          mType |= SQLITE_TRACE_PROFILE;
11234        }
11235        else if( optionMatch(z, "row") ){
11236          mType |= SQLITE_TRACE_ROW;
11237        }
11238        else if( optionMatch(z, "stmt") ){
11239          mType |= SQLITE_TRACE_STMT;
11240        }
11241        else if( optionMatch(z, "close") ){
11242          mType |= SQLITE_TRACE_CLOSE;
11243        }
11244        else {
11245          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
11246          rc = 1;
11247          goto meta_command_exit;
11248        }
11249      }else{
11250        output_file_close(p->traceOut);
11251        p->traceOut = output_file_open(azArg[1], 0);
11252      }
11253    }
11254    if( p->traceOut==0 ){
11255      sqlite3_trace_v2(p->db, 0, 0, 0);
11256    }else{
11257      if( mType==0 ) mType = SQLITE_TRACE_STMT;
11258      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11259    }
11260  }else
11261#endif /* !defined(SQLITE_OMIT_TRACE) */
11262
11263#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11264  if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
11265    int ii;
11266    int lenOpt;
11267    char *zOpt;
11268    if( nArg<2 ){
11269      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
11270      rc = 1;
11271      goto meta_command_exit;
11272    }
11273    open_db(p, 0);
11274    zOpt = azArg[1];
11275    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11276    lenOpt = (int)strlen(zOpt);
11277    if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11278      assert( azArg[nArg]==0 );
11279      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11280    }else{
11281      for(ii=1; ii<nArg; ii++){
11282        sqlite3_create_module(p->db, azArg[ii], 0, 0);
11283      }
11284    }
11285  }else
11286#endif
11287
11288#if SQLITE_USER_AUTHENTICATION
11289  if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
11290    if( nArg<2 ){
11291      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
11292      rc = 1;
11293      goto meta_command_exit;
11294    }
11295    open_db(p, 0);
11296    if( cli_strcmp(azArg[1],"login")==0 ){
11297      if( nArg!=4 ){
11298        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
11299        rc = 1;
11300        goto meta_command_exit;
11301      }
11302      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11303                                     strlen30(azArg[3]));
11304      if( rc ){
11305        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
11306        rc = 1;
11307      }
11308    }else if( cli_strcmp(azArg[1],"add")==0 ){
11309      if( nArg!=5 ){
11310        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
11311        rc = 1;
11312        goto meta_command_exit;
11313      }
11314      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11315                            booleanValue(azArg[4]));
11316      if( rc ){
11317        raw_printf(stderr, "User-Add failed: %d\n", rc);
11318        rc = 1;
11319      }
11320    }else if( cli_strcmp(azArg[1],"edit")==0 ){
11321      if( nArg!=5 ){
11322        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
11323        rc = 1;
11324        goto meta_command_exit;
11325      }
11326      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11327                              booleanValue(azArg[4]));
11328      if( rc ){
11329        raw_printf(stderr, "User-Edit failed: %d\n", rc);
11330        rc = 1;
11331      }
11332    }else if( cli_strcmp(azArg[1],"delete")==0 ){
11333      if( nArg!=3 ){
11334        raw_printf(stderr, "Usage: .user delete USER\n");
11335        rc = 1;
11336        goto meta_command_exit;
11337      }
11338      rc = sqlite3_user_delete(p->db, azArg[2]);
11339      if( rc ){
11340        raw_printf(stderr, "User-Delete failed: %d\n", rc);
11341        rc = 1;
11342      }
11343    }else{
11344      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
11345      rc = 1;
11346      goto meta_command_exit;
11347    }
11348  }else
11349#endif /* SQLITE_USER_AUTHENTICATION */
11350
11351  if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
11352    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
11353        sqlite3_libversion(), sqlite3_sourceid());
11354#if SQLITE_HAVE_ZLIB
11355    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
11356#endif
11357#define CTIMEOPT_VAL_(opt) #opt
11358#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11359#if defined(__clang__) && defined(__clang_major__)
11360    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
11361                    CTIMEOPT_VAL(__clang_minor__) "."
11362                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
11363#elif defined(_MSC_VER)
11364    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
11365#elif defined(__GNUC__) && defined(__VERSION__)
11366    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
11367#endif
11368  }else
11369
11370  if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
11371    const char *zDbName = nArg==2 ? azArg[1] : "main";
11372    sqlite3_vfs *pVfs = 0;
11373    if( p->db ){
11374      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11375      if( pVfs ){
11376        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
11377        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11378        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11379        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11380      }
11381    }
11382  }else
11383
11384  if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
11385    sqlite3_vfs *pVfs;
11386    sqlite3_vfs *pCurrent = 0;
11387    if( p->db ){
11388      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11389    }
11390    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11391      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
11392           pVfs==pCurrent ? "  <--- CURRENT" : "");
11393      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11394      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11395      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11396      if( pVfs->pNext ){
11397        raw_printf(p->out, "-----------------------------------\n");
11398      }
11399    }
11400  }else
11401
11402  if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
11403    const char *zDbName = nArg==2 ? azArg[1] : "main";
11404    char *zVfsName = 0;
11405    if( p->db ){
11406      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11407      if( zVfsName ){
11408        utf8_printf(p->out, "%s\n", zVfsName);
11409        sqlite3_free(zVfsName);
11410      }
11411    }
11412  }else
11413
11414  if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
11415    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11416    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11417  }else
11418
11419  if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
11420    int j;
11421    assert( nArg<=ArraySize(azArg) );
11422    p->nWidth = nArg-1;
11423    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11424    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11425    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11426    for(j=1; j<nArg; j++){
11427      p->colWidth[j-1] = (int)integerValue(azArg[j]);
11428    }
11429  }else
11430
11431  {
11432    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
11433      " \"%s\". Enter \".help\" for help\n", azArg[0]);
11434    rc = 1;
11435  }
11436
11437meta_command_exit:
11438  if( p->outCount ){
11439    p->outCount--;
11440    if( p->outCount==0 ) output_reset(p);
11441  }
11442  p->bSafeMode = p->bSafeModePersist;
11443  return rc;
11444}
11445
11446/* Line scan result and intermediate states (supporting scan resumption)
11447*/
11448#ifndef CHAR_BIT
11449# define CHAR_BIT 8
11450#endif
11451typedef enum {
11452  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11453  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11454  QSS_Start = 0
11455} QuickScanState;
11456#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11457#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11458#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11459#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11460#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11461
11462/*
11463** Scan line for classification to guide shell's handling.
11464** The scan is resumable for subsequent lines when prior
11465** return values are passed as the 2nd argument.
11466*/
11467static QuickScanState quickscan(char *zLine, QuickScanState qss){
11468  char cin;
11469  char cWait = (char)qss; /* intentional narrowing loss */
11470  if( cWait==0 ){
11471  PlainScan:
11472    assert( cWait==0 );
11473    while( (cin = *zLine++)!=0 ){
11474      if( IsSpace(cin) )
11475        continue;
11476      switch (cin){
11477      case '-':
11478        if( *zLine!='-' )
11479          break;
11480        while((cin = *++zLine)!=0 )
11481          if( cin=='\n')
11482            goto PlainScan;
11483        return qss;
11484      case ';':
11485        qss |= QSS_EndingSemi;
11486        continue;
11487      case '/':
11488        if( *zLine=='*' ){
11489          ++zLine;
11490          cWait = '*';
11491          qss = QSS_SETV(qss, cWait);
11492          goto TermScan;
11493        }
11494        break;
11495      case '[':
11496        cin = ']';
11497        /* fall thru */
11498      case '`': case '\'': case '"':
11499        cWait = cin;
11500        qss = QSS_HasDark | cWait;
11501        goto TermScan;
11502      default:
11503        break;
11504      }
11505      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11506    }
11507  }else{
11508  TermScan:
11509    while( (cin = *zLine++)!=0 ){
11510      if( cin==cWait ){
11511        switch( cWait ){
11512        case '*':
11513          if( *zLine != '/' )
11514            continue;
11515          ++zLine;
11516          cWait = 0;
11517          qss = QSS_SETV(qss, 0);
11518          goto PlainScan;
11519        case '`': case '\'': case '"':
11520          if(*zLine==cWait){
11521            ++zLine;
11522            continue;
11523          }
11524          /* fall thru */
11525        case ']':
11526          cWait = 0;
11527          qss = QSS_SETV(qss, 0);
11528          goto PlainScan;
11529        default: assert(0);
11530        }
11531      }
11532    }
11533  }
11534  return qss;
11535}
11536
11537/*
11538** Return TRUE if the line typed in is an SQL command terminator other
11539** than a semi-colon.  The SQL Server style "go" command is understood
11540** as is the Oracle "/".
11541*/
11542static int line_is_command_terminator(char *zLine){
11543  while( IsSpace(zLine[0]) ){ zLine++; };
11544  if( zLine[0]=='/' )
11545    zLine += 1; /* Oracle */
11546  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
11547    zLine += 2; /* SQL Server */
11548  else
11549    return 0;
11550  return quickscan(zLine, QSS_Start)==QSS_Start;
11551}
11552
11553/*
11554** We need a default sqlite3_complete() implementation to use in case
11555** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
11556** any arbitrary text is a complete SQL statement.  This is not very
11557** user-friendly, but it does seem to work.
11558*/
11559#ifdef SQLITE_OMIT_COMPLETE
11560#define sqlite3_complete(x) 1
11561#endif
11562
11563/*
11564** Return true if zSql is a complete SQL statement.  Return false if it
11565** ends in the middle of a string literal or C-style comment.
11566*/
11567static int line_is_complete(char *zSql, int nSql){
11568  int rc;
11569  if( zSql==0 ) return 1;
11570  zSql[nSql] = ';';
11571  zSql[nSql+1] = 0;
11572  rc = sqlite3_complete(zSql);
11573  zSql[nSql] = 0;
11574  return rc;
11575}
11576
11577/*
11578** Run a single line of SQL.  Return the number of errors.
11579*/
11580static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
11581  int rc;
11582  char *zErrMsg = 0;
11583
11584  open_db(p, 0);
11585  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
11586  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11587  BEGIN_TIMER;
11588  rc = shell_exec(p, zSql, &zErrMsg);
11589  END_TIMER;
11590  if( rc || zErrMsg ){
11591    char zPrefix[100];
11592    const char *zErrorTail;
11593    const char *zErrorType;
11594    if( zErrMsg==0 ){
11595      zErrorType = "Error";
11596      zErrorTail = sqlite3_errmsg(p->db);
11597    }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
11598      zErrorType = "Parse error";
11599      zErrorTail = &zErrMsg[12];
11600    }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
11601      zErrorType = "Runtime error";
11602      zErrorTail = &zErrMsg[10];
11603    }else{
11604      zErrorType = "Error";
11605      zErrorTail = zErrMsg;
11606    }
11607    if( in!=0 || !stdin_is_interactive ){
11608      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11609                       "%s near line %d:", zErrorType, startline);
11610    }else{
11611      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11612    }
11613    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11614    sqlite3_free(zErrMsg);
11615    zErrMsg = 0;
11616    return 1;
11617  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11618    char zLineBuf[2000];
11619    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11620            "changes: %lld   total_changes: %lld",
11621            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11622    raw_printf(p->out, "%s\n", zLineBuf);
11623  }
11624  return 0;
11625}
11626
11627static void echo_group_input(ShellState *p, const char *zDo){
11628  if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11629}
11630
11631#ifdef SQLITE_SHELL_FIDDLE
11632/*
11633** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
11634** because we need the global shellState and cannot access it from that function
11635** without moving lots of code around (creating a larger/messier diff).
11636*/
11637static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11638  /* Parse the next line from shellState.wasm.zInput. */
11639  const char *zBegin = shellState.wasm.zPos;
11640  const char *z = zBegin;
11641  char *zLine = 0;
11642  i64 nZ = 0;
11643
11644  UNUSED_PARAMETER(in);
11645  UNUSED_PARAMETER(isContinuation);
11646  if(!z || !*z){
11647    return 0;
11648  }
11649  while(*z && isspace(*z)) ++z;
11650  zBegin = z;
11651  for(; *z && '\n'!=*z; ++nZ, ++z){}
11652  if(nZ>0 && '\r'==zBegin[nZ-1]){
11653    --nZ;
11654  }
11655  shellState.wasm.zPos = z;
11656  zLine = realloc(zPrior, nZ+1);
11657  shell_check_oom(zLine);
11658  memcpy(zLine, zBegin, nZ);
11659  zLine[nZ] = 0;
11660  return zLine;
11661}
11662#endif /* SQLITE_SHELL_FIDDLE */
11663
11664/*
11665** Read input from *in and process it.  If *in==0 then input
11666** is interactive - the user is typing it it.  Otherwise, input
11667** is coming from a file or device.  A prompt is issued and history
11668** is saved only if input is interactive.  An interrupt signal will
11669** cause this routine to exit immediately, unless input is interactive.
11670**
11671** Return the number of errors.
11672*/
11673static int process_input(ShellState *p){
11674  char *zLine = 0;          /* A single input line */
11675  char *zSql = 0;           /* Accumulated SQL text */
11676  i64 nLine;                /* Length of current line */
11677  i64 nSql = 0;             /* Bytes of zSql[] used */
11678  i64 nAlloc = 0;           /* Allocated zSql[] space */
11679  int rc;                   /* Error code */
11680  int errCnt = 0;           /* Number of errors seen */
11681  i64 startline = 0;        /* Line number for start of current input */
11682  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11683
11684  if( p->inputNesting==MAX_INPUT_NESTING ){
11685    /* This will be more informative in a later version. */
11686    utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11687                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11688    return 1;
11689  }
11690  ++p->inputNesting;
11691  p->lineno = 0;
11692  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11693    fflush(p->out);
11694    zLine = one_input_line(p->in, zLine, nSql>0);
11695    if( zLine==0 ){
11696      /* End of input */
11697      if( p->in==0 && stdin_is_interactive ) printf("\n");
11698      break;
11699    }
11700    if( seenInterrupt ){
11701      if( p->in!=0 ) break;
11702      seenInterrupt = 0;
11703    }
11704    p->lineno++;
11705    if( QSS_INPLAIN(qss)
11706        && line_is_command_terminator(zLine)
11707        && line_is_complete(zSql, nSql) ){
11708      memcpy(zLine,";",2);
11709    }
11710    qss = quickscan(zLine, qss);
11711    if( QSS_PLAINWHITE(qss) && nSql==0 ){
11712      /* Just swallow single-line whitespace */
11713      echo_group_input(p, zLine);
11714      qss = QSS_Start;
11715      continue;
11716    }
11717    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11718      echo_group_input(p, zLine);
11719      if( zLine[0]=='.' ){
11720        rc = do_meta_command(zLine, p);
11721        if( rc==2 ){ /* exit requested */
11722          break;
11723        }else if( rc ){
11724          errCnt++;
11725        }
11726      }
11727      qss = QSS_Start;
11728      continue;
11729    }
11730    /* No single-line dispositions remain; accumulate line(s). */
11731    nLine = strlen(zLine);
11732    if( nSql+nLine+2>=nAlloc ){
11733      /* Grow buffer by half-again increments when big. */
11734      nAlloc = nSql+(nSql>>1)+nLine+100;
11735      zSql = realloc(zSql, nAlloc);
11736      shell_check_oom(zSql);
11737    }
11738    if( nSql==0 ){
11739      i64 i;
11740      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11741      assert( nAlloc>0 && zSql!=0 );
11742      memcpy(zSql, zLine+i, nLine+1-i);
11743      startline = p->lineno;
11744      nSql = nLine-i;
11745    }else{
11746      zSql[nSql++] = '\n';
11747      memcpy(zSql+nSql, zLine, nLine+1);
11748      nSql += nLine;
11749    }
11750    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11751      echo_group_input(p, zSql);
11752      errCnt += runOneSqlLine(p, zSql, p->in, startline);
11753      nSql = 0;
11754      if( p->outCount ){
11755        output_reset(p);
11756        p->outCount = 0;
11757      }else{
11758        clearTempFile(p);
11759      }
11760      p->bSafeMode = p->bSafeModePersist;
11761      qss = QSS_Start;
11762    }else if( nSql && QSS_PLAINWHITE(qss) ){
11763      echo_group_input(p, zSql);
11764      nSql = 0;
11765      qss = QSS_Start;
11766    }
11767  }
11768  if( nSql ){
11769    /* This may be incomplete. Let the SQL parser deal with that. */
11770    echo_group_input(p, zSql);
11771    errCnt += runOneSqlLine(p, zSql, p->in, startline);
11772  }
11773  free(zSql);
11774  free(zLine);
11775  --p->inputNesting;
11776  return errCnt>0;
11777}
11778
11779/*
11780** Return a pathname which is the user's home directory.  A
11781** 0 return indicates an error of some kind.
11782*/
11783static char *find_home_dir(int clearFlag){
11784  static char *home_dir = NULL;
11785  if( clearFlag ){
11786    free(home_dir);
11787    home_dir = 0;
11788    return 0;
11789  }
11790  if( home_dir ) return home_dir;
11791
11792#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11793     && !defined(__RTP__) && !defined(_WRS_KERNEL)
11794  {
11795    struct passwd *pwent;
11796    uid_t uid = getuid();
11797    if( (pwent=getpwuid(uid)) != NULL) {
11798      home_dir = pwent->pw_dir;
11799    }
11800  }
11801#endif
11802
11803#if defined(_WIN32_WCE)
11804  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11805   */
11806  home_dir = "/";
11807#else
11808
11809#if defined(_WIN32) || defined(WIN32)
11810  if (!home_dir) {
11811    home_dir = getenv("USERPROFILE");
11812  }
11813#endif
11814
11815  if (!home_dir) {
11816    home_dir = getenv("HOME");
11817  }
11818
11819#if defined(_WIN32) || defined(WIN32)
11820  if (!home_dir) {
11821    char *zDrive, *zPath;
11822    int n;
11823    zDrive = getenv("HOMEDRIVE");
11824    zPath = getenv("HOMEPATH");
11825    if( zDrive && zPath ){
11826      n = strlen30(zDrive) + strlen30(zPath) + 1;
11827      home_dir = malloc( n );
11828      if( home_dir==0 ) return 0;
11829      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11830      return home_dir;
11831    }
11832    home_dir = "c:\\";
11833  }
11834#endif
11835
11836#endif /* !_WIN32_WCE */
11837
11838  if( home_dir ){
11839    i64 n = strlen(home_dir) + 1;
11840    char *z = malloc( n );
11841    if( z ) memcpy(z, home_dir, n);
11842    home_dir = z;
11843  }
11844
11845  return home_dir;
11846}
11847
11848/*
11849** Read input from the file given by sqliterc_override.  Or if that
11850** parameter is NULL, take input from ~/.sqliterc
11851**
11852** Returns the number of errors.
11853*/
11854static void process_sqliterc(
11855  ShellState *p,                  /* Configuration data */
11856  const char *sqliterc_override   /* Name of config file. NULL to use default */
11857){
11858  char *home_dir = NULL;
11859  const char *sqliterc = sqliterc_override;
11860  char *zBuf = 0;
11861  FILE *inSaved = p->in;
11862  int savedLineno = p->lineno;
11863
11864  if (sqliterc == NULL) {
11865    home_dir = find_home_dir(0);
11866    if( home_dir==0 ){
11867      raw_printf(stderr, "-- warning: cannot find home directory;"
11868                      " cannot read ~/.sqliterc\n");
11869      return;
11870    }
11871    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11872    shell_check_oom(zBuf);
11873    sqliterc = zBuf;
11874  }
11875  p->in = fopen(sqliterc,"rb");
11876  if( p->in ){
11877    if( stdin_is_interactive ){
11878      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11879    }
11880    if( process_input(p) && bail_on_error ) exit(1);
11881    fclose(p->in);
11882  }else if( sqliterc_override!=0 ){
11883    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11884    if( bail_on_error ) exit(1);
11885  }
11886  p->in = inSaved;
11887  p->lineno = savedLineno;
11888  sqlite3_free(zBuf);
11889}
11890
11891/*
11892** Show available command line options
11893*/
11894static const char zOptions[] =
11895#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11896  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11897#endif
11898  "   -append              append the database to the end of the file\n"
11899  "   -ascii               set output mode to 'ascii'\n"
11900  "   -bail                stop after hitting an error\n"
11901  "   -batch               force batch I/O\n"
11902  "   -box                 set output mode to 'box'\n"
11903  "   -column              set output mode to 'column'\n"
11904  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11905  "   -csv                 set output mode to 'csv'\n"
11906#if !defined(SQLITE_OMIT_DESERIALIZE)
11907  "   -deserialize         open the database using sqlite3_deserialize()\n"
11908#endif
11909  "   -echo                print inputs before execution\n"
11910  "   -init FILENAME       read/process named file\n"
11911  "   -[no]header          turn headers on or off\n"
11912#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11913  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11914#endif
11915  "   -help                show this message\n"
11916  "   -html                set output mode to HTML\n"
11917  "   -interactive         force interactive I/O\n"
11918  "   -json                set output mode to 'json'\n"
11919  "   -line                set output mode to 'line'\n"
11920  "   -list                set output mode to 'list'\n"
11921  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11922  "   -markdown            set output mode to 'markdown'\n"
11923#if !defined(SQLITE_OMIT_DESERIALIZE)
11924  "   -maxsize N           maximum size for a --deserialize database\n"
11925#endif
11926  "   -memtrace            trace all memory allocations and deallocations\n"
11927  "   -mmap N              default mmap size set to N\n"
11928#ifdef SQLITE_ENABLE_MULTIPLEX
11929  "   -multiplex           enable the multiplexor VFS\n"
11930#endif
11931  "   -newline SEP         set output row separator. Default: '\\n'\n"
11932  "   -nofollow            refuse to open symbolic links to database files\n"
11933  "   -nonce STRING        set the safe-mode escape nonce\n"
11934  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11935  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11936  "   -quote               set output mode to 'quote'\n"
11937  "   -readonly            open the database read-only\n"
11938  "   -safe                enable safe-mode\n"
11939  "   -separator SEP       set output column separator. Default: '|'\n"
11940#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11941  "   -sorterref SIZE      sorter references threshold size\n"
11942#endif
11943  "   -stats               print memory stats before each finalize\n"
11944  "   -table               set output mode to 'table'\n"
11945  "   -tabs                set output mode to 'tabs'\n"
11946  "   -version             show SQLite version\n"
11947  "   -vfs NAME            use NAME as the default VFS\n"
11948#ifdef SQLITE_ENABLE_VFSTRACE
11949  "   -vfstrace            enable tracing of all VFS calls\n"
11950#endif
11951#ifdef SQLITE_HAVE_ZLIB
11952  "   -zip                 open the file as a ZIP Archive\n"
11953#endif
11954;
11955static void usage(int showDetail){
11956  utf8_printf(stderr,
11957      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11958      "FILENAME is the name of an SQLite database. A new database is created\n"
11959      "if the file does not previously exist.\n", Argv0);
11960  if( showDetail ){
11961    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11962  }else{
11963    raw_printf(stderr, "Use the -help option for additional information\n");
11964  }
11965  exit(1);
11966}
11967
11968/*
11969** Internal check:  Verify that the SQLite is uninitialized.  Print a
11970** error message if it is initialized.
11971*/
11972static void verify_uninitialized(void){
11973  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11974    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11975                        " initialization.\n");
11976  }
11977}
11978
11979/*
11980** Initialize the state information in data
11981*/
11982static void main_init(ShellState *data) {
11983  memset(data, 0, sizeof(*data));
11984  data->normalMode = data->cMode = data->mode = MODE_List;
11985  data->autoExplain = 1;
11986  data->pAuxDb = &data->aAuxDb[0];
11987  memcpy(data->colSeparator,SEP_Column, 2);
11988  memcpy(data->rowSeparator,SEP_Row, 2);
11989  data->showHeader = 0;
11990  data->shellFlgs = SHFLG_Lookaside;
11991  verify_uninitialized();
11992  sqlite3_config(SQLITE_CONFIG_URI, 1);
11993  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11994  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11995  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11996  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11997}
11998
11999/*
12000** Output text to the console in a font that attracts extra attention.
12001*/
12002#ifdef _WIN32
12003static void printBold(const char *zText){
12004#if !SQLITE_OS_WINRT
12005  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
12006  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
12007  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
12008  SetConsoleTextAttribute(out,
12009         FOREGROUND_RED|FOREGROUND_INTENSITY
12010  );
12011#endif
12012  printf("%s", zText);
12013#if !SQLITE_OS_WINRT
12014  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
12015#endif
12016}
12017#else
12018static void printBold(const char *zText){
12019  printf("\033[1m%s\033[0m", zText);
12020}
12021#endif
12022
12023/*
12024** Get the argument to an --option.  Throw an error and die if no argument
12025** is available.
12026*/
12027static char *cmdline_option_value(int argc, char **argv, int i){
12028  if( i==argc ){
12029    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
12030            argv[0], argv[argc-1]);
12031    exit(1);
12032  }
12033  return argv[i];
12034}
12035
12036#ifndef SQLITE_SHELL_IS_UTF8
12037#  if (defined(_WIN32) || defined(WIN32)) \
12038   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12039#    define SQLITE_SHELL_IS_UTF8          (0)
12040#  else
12041#    define SQLITE_SHELL_IS_UTF8          (1)
12042#  endif
12043#endif
12044
12045#ifdef SQLITE_SHELL_FIDDLE
12046#  define main fiddle_main
12047#endif
12048
12049#if SQLITE_SHELL_IS_UTF8
12050int SQLITE_CDECL main(int argc, char **argv){
12051#else
12052int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
12053  char **argv;
12054#endif
12055#ifdef SQLITE_DEBUG
12056  sqlite3_int64 mem_main_enter = sqlite3_memory_used();
12057#endif
12058  char *zErrMsg = 0;
12059#ifdef SQLITE_SHELL_FIDDLE
12060#  define data shellState
12061#else
12062  ShellState data;
12063#endif
12064  const char *zInitFile = 0;
12065  int i;
12066  int rc = 0;
12067  int warnInmemoryDb = 0;
12068  int readStdin = 1;
12069  int nCmd = 0;
12070  char **azCmd = 0;
12071  const char *zVfs = 0;           /* Value of -vfs command-line option */
12072#if !SQLITE_SHELL_IS_UTF8
12073  char **argvToFree = 0;
12074  int argcToFree = 0;
12075#endif
12076
12077  setBinaryMode(stdin, 0);
12078  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12079#ifdef SQLITE_SHELL_FIDDLE
12080  stdin_is_interactive = 0;
12081  stdout_is_console = 1;
12082  data.wasm.zDefaultDbName = "/fiddle.sqlite3";
12083#else
12084  stdin_is_interactive = isatty(0);
12085  stdout_is_console = isatty(1);
12086#endif
12087
12088#if !defined(_WIN32_WCE)
12089  if( getenv("SQLITE_DEBUG_BREAK") ){
12090    if( isatty(0) && isatty(2) ){
12091      fprintf(stderr,
12092          "attach debugger to process %d and press any key to continue.\n",
12093          GETPID());
12094      fgetc(stdin);
12095    }else{
12096#if defined(_WIN32) || defined(WIN32)
12097#if SQLITE_OS_WINRT
12098      __debugbreak();
12099#else
12100      DebugBreak();
12101#endif
12102#elif defined(SIGTRAP)
12103      raise(SIGTRAP);
12104#endif
12105    }
12106  }
12107#endif
12108
12109#if USE_SYSTEM_SQLITE+0!=1
12110  if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12111    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
12112            sqlite3_sourceid(), SQLITE_SOURCE_ID);
12113    exit(1);
12114  }
12115#endif
12116  main_init(&data);
12117
12118  /* On Windows, we must translate command-line arguments into UTF-8.
12119  ** The SQLite memory allocator subsystem has to be enabled in order to
12120  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
12121  ** subsequent sqlite3_config() calls will work.  So copy all results into
12122  ** memory that does not come from the SQLite memory allocator.
12123  */
12124#if !SQLITE_SHELL_IS_UTF8
12125  sqlite3_initialize();
12126  argvToFree = malloc(sizeof(argv[0])*argc*2);
12127  shell_check_oom(argvToFree);
12128  argcToFree = argc;
12129  argv = argvToFree + argc;
12130  for(i=0; i<argc; i++){
12131    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12132    i64 n;
12133    shell_check_oom(z);
12134    n = strlen(z);
12135    argv[i] = malloc( n+1 );
12136    shell_check_oom(argv[i]);
12137    memcpy(argv[i], z, n+1);
12138    argvToFree[i] = argv[i];
12139    sqlite3_free(z);
12140  }
12141  sqlite3_shutdown();
12142#endif
12143
12144  assert( argc>=1 && argv && argv[0] );
12145  Argv0 = argv[0];
12146
12147  /* Make sure we have a valid signal handler early, before anything
12148  ** else is done.
12149  */
12150#ifdef SIGINT
12151  signal(SIGINT, interrupt_handler);
12152#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12153  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
12154#endif
12155
12156#ifdef SQLITE_SHELL_DBNAME_PROC
12157  {
12158    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12159    ** of a C-function that will provide the name of the database file.  Use
12160    ** this compile-time option to embed this shell program in larger
12161    ** applications. */
12162    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12163    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12164    warnInmemoryDb = 0;
12165  }
12166#endif
12167
12168  /* Do an initial pass through the command-line argument to locate
12169  ** the name of the database file, the name of the initialization file,
12170  ** the size of the alternative malloc heap,
12171  ** and the first command to execute.
12172  */
12173  verify_uninitialized();
12174  for(i=1; i<argc; i++){
12175    char *z;
12176    z = argv[i];
12177    if( z[0]!='-' ){
12178      if( data.aAuxDb->zDbFilename==0 ){
12179        data.aAuxDb->zDbFilename = z;
12180      }else{
12181        /* Excesss arguments are interpreted as SQL (or dot-commands) and
12182        ** mean that nothing is read from stdin */
12183        readStdin = 0;
12184        nCmd++;
12185        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12186        shell_check_oom(azCmd);
12187        azCmd[nCmd-1] = z;
12188      }
12189    }
12190    if( z[1]=='-' ) z++;
12191    if( cli_strcmp(z,"-separator")==0
12192     || cli_strcmp(z,"-nullvalue")==0
12193     || cli_strcmp(z,"-newline")==0
12194     || cli_strcmp(z,"-cmd")==0
12195    ){
12196      (void)cmdline_option_value(argc, argv, ++i);
12197    }else if( cli_strcmp(z,"-init")==0 ){
12198      zInitFile = cmdline_option_value(argc, argv, ++i);
12199    }else if( cli_strcmp(z,"-batch")==0 ){
12200      /* Need to check for batch mode here to so we can avoid printing
12201      ** informational messages (like from process_sqliterc) before
12202      ** we do the actual processing of arguments later in a second pass.
12203      */
12204      stdin_is_interactive = 0;
12205    }else if( cli_strcmp(z,"-heap")==0 ){
12206#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12207      const char *zSize;
12208      sqlite3_int64 szHeap;
12209
12210      zSize = cmdline_option_value(argc, argv, ++i);
12211      szHeap = integerValue(zSize);
12212      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12213      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12214#else
12215      (void)cmdline_option_value(argc, argv, ++i);
12216#endif
12217    }else if( cli_strcmp(z,"-pagecache")==0 ){
12218      sqlite3_int64 n, sz;
12219      sz = integerValue(cmdline_option_value(argc,argv,++i));
12220      if( sz>70000 ) sz = 70000;
12221      if( sz<0 ) sz = 0;
12222      n = integerValue(cmdline_option_value(argc,argv,++i));
12223      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12224        n = 0xffffffffffffLL/sz;
12225      }
12226      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12227                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12228      data.shellFlgs |= SHFLG_Pagecache;
12229    }else if( cli_strcmp(z,"-lookaside")==0 ){
12230      int n, sz;
12231      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12232      if( sz<0 ) sz = 0;
12233      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12234      if( n<0 ) n = 0;
12235      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12236      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12237    }else if( cli_strcmp(z,"-threadsafe")==0 ){
12238      int n;
12239      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12240      switch( n ){
12241         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
12242         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
12243         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
12244      }
12245#ifdef SQLITE_ENABLE_VFSTRACE
12246    }else if( cli_strcmp(z,"-vfstrace")==0 ){
12247      extern int vfstrace_register(
12248         const char *zTraceName,
12249         const char *zOldVfsName,
12250         int (*xOut)(const char*,void*),
12251         void *pOutArg,
12252         int makeDefault
12253      );
12254      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
12255#endif
12256#ifdef SQLITE_ENABLE_MULTIPLEX
12257    }else if( cli_strcmp(z,"-multiplex")==0 ){
12258      extern int sqlite3_multiple_initialize(const char*,int);
12259      sqlite3_multiplex_initialize(0, 1);
12260#endif
12261    }else if( cli_strcmp(z,"-mmap")==0 ){
12262      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12263      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12264#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12265    }else if( cli_strcmp(z,"-sorterref")==0 ){
12266      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12267      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12268#endif
12269    }else if( cli_strcmp(z,"-vfs")==0 ){
12270      zVfs = cmdline_option_value(argc, argv, ++i);
12271#ifdef SQLITE_HAVE_ZLIB
12272    }else if( cli_strcmp(z,"-zip")==0 ){
12273      data.openMode = SHELL_OPEN_ZIPFILE;
12274#endif
12275    }else if( cli_strcmp(z,"-append")==0 ){
12276      data.openMode = SHELL_OPEN_APPENDVFS;
12277#ifndef SQLITE_OMIT_DESERIALIZE
12278    }else if( cli_strcmp(z,"-deserialize")==0 ){
12279      data.openMode = SHELL_OPEN_DESERIALIZE;
12280    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12281      data.szMax = integerValue(argv[++i]);
12282#endif
12283    }else if( cli_strcmp(z,"-readonly")==0 ){
12284      data.openMode = SHELL_OPEN_READONLY;
12285    }else if( cli_strcmp(z,"-nofollow")==0 ){
12286      data.openFlags = SQLITE_OPEN_NOFOLLOW;
12287#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12288    }else if( cli_strncmp(z, "-A",2)==0 ){
12289      /* All remaining command-line arguments are passed to the ".archive"
12290      ** command, so ignore them */
12291      break;
12292#endif
12293    }else if( cli_strcmp(z, "-memtrace")==0 ){
12294      sqlite3MemTraceActivate(stderr);
12295    }else if( cli_strcmp(z,"-bail")==0 ){
12296      bail_on_error = 1;
12297    }else if( cli_strcmp(z,"-nonce")==0 ){
12298      free(data.zNonce);
12299      data.zNonce = strdup(argv[++i]);
12300    }else if( cli_strcmp(z,"-safe")==0 ){
12301      /* no-op - catch this on the second pass */
12302    }
12303  }
12304  verify_uninitialized();
12305
12306
12307#ifdef SQLITE_SHELL_INIT_PROC
12308  {
12309    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12310    ** of a C-function that will perform initialization actions on SQLite that
12311    ** occur just before or after sqlite3_initialize(). Use this compile-time
12312    ** option to embed this shell program in larger applications. */
12313    extern void SQLITE_SHELL_INIT_PROC(void);
12314    SQLITE_SHELL_INIT_PROC();
12315  }
12316#else
12317  /* All the sqlite3_config() calls have now been made. So it is safe
12318  ** to call sqlite3_initialize() and process any command line -vfs option. */
12319  sqlite3_initialize();
12320#endif
12321
12322  if( zVfs ){
12323    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12324    if( pVfs ){
12325      sqlite3_vfs_register(pVfs, 1);
12326    }else{
12327      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
12328      exit(1);
12329    }
12330  }
12331
12332  if( data.pAuxDb->zDbFilename==0 ){
12333#ifndef SQLITE_OMIT_MEMORYDB
12334    data.pAuxDb->zDbFilename = ":memory:";
12335    warnInmemoryDb = argc==1;
12336#else
12337    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
12338    return 1;
12339#endif
12340  }
12341  data.out = stdout;
12342#ifndef SQLITE_SHELL_FIDDLE
12343  sqlite3_appendvfs_init(0,0,0);
12344#endif
12345
12346  /* Go ahead and open the database file if it already exists.  If the
12347  ** file does not exist, delay opening it.  This prevents empty database
12348  ** files from being created if a user mistypes the database name argument
12349  ** to the sqlite command-line tool.
12350  */
12351  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
12352    open_db(&data, 0);
12353  }
12354
12355  /* Process the initialization file if there is one.  If no -init option
12356  ** is given on the command line, look for a file named ~/.sqliterc and
12357  ** try to process it.
12358  */
12359  process_sqliterc(&data,zInitFile);
12360
12361  /* Make a second pass through the command-line argument and set
12362  ** options.  This second pass is delayed until after the initialization
12363  ** file is processed so that the command-line arguments will override
12364  ** settings in the initialization file.
12365  */
12366  for(i=1; i<argc; i++){
12367    char *z = argv[i];
12368    if( z[0]!='-' ) continue;
12369    if( z[1]=='-' ){ z++; }
12370    if( cli_strcmp(z,"-init")==0 ){
12371      i++;
12372    }else if( cli_strcmp(z,"-html")==0 ){
12373      data.mode = MODE_Html;
12374    }else if( cli_strcmp(z,"-list")==0 ){
12375      data.mode = MODE_List;
12376    }else if( cli_strcmp(z,"-quote")==0 ){
12377      data.mode = MODE_Quote;
12378      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
12379      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12380    }else if( cli_strcmp(z,"-line")==0 ){
12381      data.mode = MODE_Line;
12382    }else if( cli_strcmp(z,"-column")==0 ){
12383      data.mode = MODE_Column;
12384    }else if( cli_strcmp(z,"-json")==0 ){
12385      data.mode = MODE_Json;
12386    }else if( cli_strcmp(z,"-markdown")==0 ){
12387      data.mode = MODE_Markdown;
12388    }else if( cli_strcmp(z,"-table")==0 ){
12389      data.mode = MODE_Table;
12390    }else if( cli_strcmp(z,"-box")==0 ){
12391      data.mode = MODE_Box;
12392    }else if( cli_strcmp(z,"-csv")==0 ){
12393      data.mode = MODE_Csv;
12394      memcpy(data.colSeparator,",",2);
12395#ifdef SQLITE_HAVE_ZLIB
12396    }else if( cli_strcmp(z,"-zip")==0 ){
12397      data.openMode = SHELL_OPEN_ZIPFILE;
12398#endif
12399    }else if( cli_strcmp(z,"-append")==0 ){
12400      data.openMode = SHELL_OPEN_APPENDVFS;
12401#ifndef SQLITE_OMIT_DESERIALIZE
12402    }else if( cli_strcmp(z,"-deserialize")==0 ){
12403      data.openMode = SHELL_OPEN_DESERIALIZE;
12404    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12405      data.szMax = integerValue(argv[++i]);
12406#endif
12407    }else if( cli_strcmp(z,"-readonly")==0 ){
12408      data.openMode = SHELL_OPEN_READONLY;
12409    }else if( cli_strcmp(z,"-nofollow")==0 ){
12410      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
12411    }else if( cli_strcmp(z,"-ascii")==0 ){
12412      data.mode = MODE_Ascii;
12413      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
12414      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
12415    }else if( cli_strcmp(z,"-tabs")==0 ){
12416      data.mode = MODE_List;
12417      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
12418      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12419    }else if( cli_strcmp(z,"-separator")==0 ){
12420      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
12421                       "%s",cmdline_option_value(argc,argv,++i));
12422    }else if( cli_strcmp(z,"-newline")==0 ){
12423      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
12424                       "%s",cmdline_option_value(argc,argv,++i));
12425    }else if( cli_strcmp(z,"-nullvalue")==0 ){
12426      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
12427                       "%s",cmdline_option_value(argc,argv,++i));
12428    }else if( cli_strcmp(z,"-header")==0 ){
12429      data.showHeader = 1;
12430      ShellSetFlag(&data, SHFLG_HeaderSet);
12431     }else if( cli_strcmp(z,"-noheader")==0 ){
12432      data.showHeader = 0;
12433      ShellSetFlag(&data, SHFLG_HeaderSet);
12434    }else if( cli_strcmp(z,"-echo")==0 ){
12435      ShellSetFlag(&data, SHFLG_Echo);
12436    }else if( cli_strcmp(z,"-eqp")==0 ){
12437      data.autoEQP = AUTOEQP_on;
12438    }else if( cli_strcmp(z,"-eqpfull")==0 ){
12439      data.autoEQP = AUTOEQP_full;
12440    }else if( cli_strcmp(z,"-stats")==0 ){
12441      data.statsOn = 1;
12442    }else if( cli_strcmp(z,"-scanstats")==0 ){
12443      data.scanstatsOn = 1;
12444    }else if( cli_strcmp(z,"-backslash")==0 ){
12445      /* Undocumented command-line option: -backslash
12446      ** Causes C-style backslash escapes to be evaluated in SQL statements
12447      ** prior to sending the SQL into SQLite.  Useful for injecting
12448      ** crazy bytes in the middle of SQL statements for testing and debugging.
12449      */
12450      ShellSetFlag(&data, SHFLG_Backslash);
12451    }else if( cli_strcmp(z,"-bail")==0 ){
12452      /* No-op.  The bail_on_error flag should already be set. */
12453    }else if( cli_strcmp(z,"-version")==0 ){
12454      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
12455      return 0;
12456    }else if( cli_strcmp(z,"-interactive")==0 ){
12457      stdin_is_interactive = 1;
12458    }else if( cli_strcmp(z,"-batch")==0 ){
12459      stdin_is_interactive = 0;
12460    }else if( cli_strcmp(z,"-heap")==0 ){
12461      i++;
12462    }else if( cli_strcmp(z,"-pagecache")==0 ){
12463      i+=2;
12464    }else if( cli_strcmp(z,"-lookaside")==0 ){
12465      i+=2;
12466    }else if( cli_strcmp(z,"-threadsafe")==0 ){
12467      i+=2;
12468    }else if( cli_strcmp(z,"-nonce")==0 ){
12469      i += 2;
12470    }else if( cli_strcmp(z,"-mmap")==0 ){
12471      i++;
12472    }else if( cli_strcmp(z,"-memtrace")==0 ){
12473      i++;
12474#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12475    }else if( cli_strcmp(z,"-sorterref")==0 ){
12476      i++;
12477#endif
12478    }else if( cli_strcmp(z,"-vfs")==0 ){
12479      i++;
12480#ifdef SQLITE_ENABLE_VFSTRACE
12481    }else if( cli_strcmp(z,"-vfstrace")==0 ){
12482      i++;
12483#endif
12484#ifdef SQLITE_ENABLE_MULTIPLEX
12485    }else if( cli_strcmp(z,"-multiplex")==0 ){
12486      i++;
12487#endif
12488    }else if( cli_strcmp(z,"-help")==0 ){
12489      usage(1);
12490    }else if( cli_strcmp(z,"-cmd")==0 ){
12491      /* Run commands that follow -cmd first and separately from commands
12492      ** that simply appear on the command-line.  This seems goofy.  It would
12493      ** be better if all commands ran in the order that they appear.  But
12494      ** we retain the goofy behavior for historical compatibility. */
12495      if( i==argc-1 ) break;
12496      z = cmdline_option_value(argc,argv,++i);
12497      if( z[0]=='.' ){
12498        rc = do_meta_command(z, &data);
12499        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
12500      }else{
12501        open_db(&data, 0);
12502        rc = shell_exec(&data, z, &zErrMsg);
12503        if( zErrMsg!=0 ){
12504          utf8_printf(stderr,"Error: %s\n", zErrMsg);
12505          if( bail_on_error ) return rc!=0 ? rc : 1;
12506        }else if( rc!=0 ){
12507          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
12508          if( bail_on_error ) return rc;
12509        }
12510      }
12511#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12512    }else if( cli_strncmp(z, "-A", 2)==0 ){
12513      if( nCmd>0 ){
12514        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
12515                            " with \"%s\"\n", z);
12516        return 1;
12517      }
12518      open_db(&data, OPEN_DB_ZIPFILE);
12519      if( z[2] ){
12520        argv[i] = &z[2];
12521        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
12522      }else{
12523        arDotCommand(&data, 1, argv+i, argc-i);
12524      }
12525      readStdin = 0;
12526      break;
12527#endif
12528    }else if( cli_strcmp(z,"-safe")==0 ){
12529      data.bSafeMode = data.bSafeModePersist = 1;
12530    }else{
12531      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
12532      raw_printf(stderr,"Use -help for a list of options.\n");
12533      return 1;
12534    }
12535    data.cMode = data.mode;
12536  }
12537
12538  if( !readStdin ){
12539    /* Run all arguments that do not begin with '-' as if they were separate
12540    ** command-line inputs, except for the argToSkip argument which contains
12541    ** the database filename.
12542    */
12543    for(i=0; i<nCmd; i++){
12544      if( azCmd[i][0]=='.' ){
12545        rc = do_meta_command(azCmd[i], &data);
12546        if( rc ){
12547          free(azCmd);
12548          return rc==2 ? 0 : rc;
12549        }
12550      }else{
12551        open_db(&data, 0);
12552        rc = shell_exec(&data, azCmd[i], &zErrMsg);
12553        if( zErrMsg || rc ){
12554          if( zErrMsg!=0 ){
12555            utf8_printf(stderr,"Error: %s\n", zErrMsg);
12556          }else{
12557            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
12558          }
12559          sqlite3_free(zErrMsg);
12560          free(azCmd);
12561          return rc!=0 ? rc : 1;
12562        }
12563      }
12564    }
12565  }else{
12566    /* Run commands received from standard input
12567    */
12568    if( stdin_is_interactive ){
12569      char *zHome;
12570      char *zHistory;
12571      int nHistory;
12572      printf(
12573        "SQLite version %s %.19s\n" /*extra-version-info*/
12574        "Enter \".help\" for usage hints.\n",
12575        sqlite3_libversion(), sqlite3_sourceid()
12576      );
12577      if( warnInmemoryDb ){
12578        printf("Connected to a ");
12579        printBold("transient in-memory database");
12580        printf(".\nUse \".open FILENAME\" to reopen on a "
12581               "persistent database.\n");
12582      }
12583      zHistory = getenv("SQLITE_HISTORY");
12584      if( zHistory ){
12585        zHistory = strdup(zHistory);
12586      }else if( (zHome = find_home_dir(0))!=0 ){
12587        nHistory = strlen30(zHome) + 20;
12588        if( (zHistory = malloc(nHistory))!=0 ){
12589          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
12590        }
12591      }
12592      if( zHistory ){ shell_read_history(zHistory); }
12593#if HAVE_READLINE || HAVE_EDITLINE
12594      rl_attempted_completion_function = readline_completion;
12595#elif HAVE_LINENOISE
12596      linenoiseSetCompletionCallback(linenoise_completion);
12597#endif
12598      data.in = 0;
12599      rc = process_input(&data);
12600      if( zHistory ){
12601        shell_stifle_history(2000);
12602        shell_write_history(zHistory);
12603        free(zHistory);
12604      }
12605    }else{
12606      data.in = stdin;
12607      rc = process_input(&data);
12608    }
12609  }
12610#ifndef SQLITE_SHELL_FIDDLE
12611  /* In WASM mode we have to leave the db state in place so that
12612  ** client code can "push" SQL into it after this call returns. */
12613  free(azCmd);
12614  set_table_name(&data, 0);
12615  if( data.db ){
12616    session_close_all(&data, -1);
12617    close_db(data.db);
12618  }
12619  for(i=0; i<ArraySize(data.aAuxDb); i++){
12620    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12621    if( data.aAuxDb[i].db ){
12622      session_close_all(&data, i);
12623      close_db(data.aAuxDb[i].db);
12624    }
12625  }
12626  find_home_dir(1);
12627  output_reset(&data);
12628  data.doXdgOpen = 0;
12629  clearTempFile(&data);
12630#if !SQLITE_SHELL_IS_UTF8
12631  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12632  free(argvToFree);
12633#endif
12634  free(data.colWidth);
12635  free(data.zNonce);
12636  /* Clear the global data structure so that valgrind will detect memory
12637  ** leaks */
12638  memset(&data, 0, sizeof(data));
12639#ifdef SQLITE_DEBUG
12640  if( sqlite3_memory_used()>mem_main_enter ){
12641    utf8_printf(stderr, "Memory leaked: %u bytes\n",
12642                (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12643  }
12644#endif
12645#endif /* !SQLITE_SHELL_FIDDLE */
12646  return rc;
12647}
12648
12649
12650#ifdef SQLITE_SHELL_FIDDLE
12651/* Only for emcc experimentation purposes. */
12652int fiddle_experiment(int a,int b){
12653  return a + b;
12654}
12655
12656/*
12657** Returns a pointer to the current DB handle.
12658*/
12659sqlite3 * fiddle_db_handle(){
12660  return globalDb;
12661}
12662
12663/*
12664** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
12665** "main" is assumed. Returns 0 if no db with the given name is
12666** open.
12667*/
12668sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
12669  sqlite3_vfs * pVfs = 0;
12670  if(globalDb){
12671    sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
12672                         SQLITE_FCNTL_VFS_POINTER, &pVfs);
12673  }
12674  return pVfs;
12675}
12676
12677/* Only for emcc experimentation purposes. */
12678sqlite3 * fiddle_db_arg(sqlite3 *arg){
12679    printf("fiddle_db_arg(%p)\n", (const void*)arg);
12680    return arg;
12681}
12682
12683/*
12684** Intended to be called via a SharedWorker() while a separate
12685** SharedWorker() (which manages the wasm module) is performing work
12686** which should be interrupted. Unfortunately, SharedWorker is not
12687** portable enough to make real use of.
12688*/
12689void fiddle_interrupt(void){
12690  if( globalDb ) sqlite3_interrupt(globalDb);
12691}
12692
12693/*
12694** Returns the filename of the given db name, assuming "main" if
12695** zDbName is NULL. Returns NULL if globalDb is not opened.
12696*/
12697const char * fiddle_db_filename(const char * zDbName){
12698    return globalDb
12699      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12700      : NULL;
12701}
12702
12703/*
12704** Completely wipes out the contents of the currently-opened database
12705** but leaves its storage intact for reuse.
12706*/
12707void fiddle_reset_db(void){
12708  if( globalDb ){
12709    int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
12710    if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
12711    sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
12712  }
12713}
12714
12715/*
12716** Uses the current database's VFS xRead to stream the db file's
12717** contents out to the given callback. The callback gets a single
12718** chunk of size n (its 2nd argument) on each call and must return 0
12719** on success, non-0 on error. This function returns 0 on success,
12720** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
12721** code from the callback. Note that this is not thread-friendly: it
12722** expects that it will be the only thread reading the db file and
12723** takes no measures to ensure that is the case.
12724*/
12725int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
12726  sqlite3_int64 nSize = 0;
12727  sqlite3_int64 nPos = 0;
12728  sqlite3_file * pFile = 0;
12729  unsigned char buf[1024 * 8];
12730  int nBuf = (int)sizeof(buf);
12731  int rc = shellState.db
12732    ? sqlite3_file_control(shellState.db, "main",
12733                           SQLITE_FCNTL_FILE_POINTER, &pFile)
12734    : SQLITE_NOTFOUND;
12735  if( rc ) return rc;
12736  rc = pFile->pMethods->xFileSize(pFile, &nSize);
12737  if( rc ) return rc;
12738  if(nSize % nBuf){
12739    /* DB size is not an even multiple of the buffer size. Reduce
12740    ** buffer size so that we do not unduly inflate the db size when
12741    ** exporting. */
12742    if(0 == nSize % 4096) nBuf = 4096;
12743    else if(0 == nSize % 2048) nBuf = 2048;
12744    else if(0 == nSize % 1024) nBuf = 1024;
12745    else nBuf = 512;
12746  }
12747  for( ; 0==rc && nPos<nSize; nPos += nBuf ){
12748    rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
12749    if(SQLITE_IOERR_SHORT_READ == rc){
12750      rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
12751    }
12752    if( 0==rc ) rc = xCallback(buf, nBuf);
12753  }
12754  return rc;
12755}
12756
12757/*
12758** Trivial exportable function for emscripten. It processes zSql as if
12759** it were input to the sqlite3 shell and redirects all output to the
12760** wasm binding. fiddle_main() must have been called before this
12761** is called, or results are undefined.
12762*/
12763void fiddle_exec(const char * zSql){
12764  if(zSql && *zSql){
12765    if('.'==*zSql) puts(zSql);
12766    shellState.wasm.zInput = zSql;
12767    shellState.wasm.zPos = zSql;
12768    process_input(&shellState);
12769    shellState.wasm.zInput = shellState.wasm.zPos = 0;
12770  }
12771}
12772#endif /* SQLITE_SHELL_FIDDLE */
12773