xref: /sqlite-3.40.0/src/shell.c.in (revision ce2d4ae6)
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  if( zTable==0 ) return 0;
4165  if( zType==0 ) return 0;
4166  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4167  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4168
4169  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4170    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4171  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4172    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4173  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
4174    return 0;
4175  }else if( dataOnly ){
4176    /* no-op */
4177  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4178    char *zIns;
4179    if( !p->writableSchema ){
4180      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4181      p->writableSchema = 1;
4182    }
4183    zIns = sqlite3_mprintf(
4184       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4185       "VALUES('table','%q','%q',0,'%q');",
4186       zTable, zTable, zSql);
4187    shell_check_oom(zIns);
4188    utf8_printf(p->out, "%s\n", zIns);
4189    sqlite3_free(zIns);
4190    return 0;
4191  }else{
4192    printSchemaLine(p->out, zSql, ";\n");
4193  }
4194
4195  if( cli_strcmp(zType, "table")==0 ){
4196    ShellText sSelect;
4197    ShellText sTable;
4198    char **azCol;
4199    int i;
4200    char *savedDestTable;
4201    int savedMode;
4202
4203    azCol = tableColumnList(p, zTable);
4204    if( azCol==0 ){
4205      p->nErr++;
4206      return 0;
4207    }
4208
4209    /* Always quote the table name, even if it appears to be pure ascii,
4210    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
4211    initText(&sTable);
4212    appendText(&sTable, zTable, quoteChar(zTable));
4213    /* If preserving the rowid, add a column list after the table name.
4214    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4215    ** instead of the usual "INSERT INTO tab VALUES(...)".
4216    */
4217    if( azCol[0] ){
4218      appendText(&sTable, "(", 0);
4219      appendText(&sTable, azCol[0], 0);
4220      for(i=1; azCol[i]; i++){
4221        appendText(&sTable, ",", 0);
4222        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4223      }
4224      appendText(&sTable, ")", 0);
4225    }
4226
4227    /* Build an appropriate SELECT statement */
4228    initText(&sSelect);
4229    appendText(&sSelect, "SELECT ", 0);
4230    if( azCol[0] ){
4231      appendText(&sSelect, azCol[0], 0);
4232      appendText(&sSelect, ",", 0);
4233    }
4234    for(i=1; azCol[i]; i++){
4235      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4236      if( azCol[i+1] ){
4237        appendText(&sSelect, ",", 0);
4238      }
4239    }
4240    freeColumnList(azCol);
4241    appendText(&sSelect, " FROM ", 0);
4242    appendText(&sSelect, zTable, quoteChar(zTable));
4243
4244    savedDestTable = p->zDestTable;
4245    savedMode = p->mode;
4246    p->zDestTable = sTable.z;
4247    p->mode = p->cMode = MODE_Insert;
4248    rc = shell_exec(p, sSelect.z, 0);
4249    if( (rc&0xff)==SQLITE_CORRUPT ){
4250      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4251      toggleSelectOrder(p->db);
4252      shell_exec(p, sSelect.z, 0);
4253      toggleSelectOrder(p->db);
4254    }
4255    p->zDestTable = savedDestTable;
4256    p->mode = savedMode;
4257    freeText(&sTable);
4258    freeText(&sSelect);
4259    if( rc ) p->nErr++;
4260  }
4261  return 0;
4262}
4263
4264/*
4265** Run zQuery.  Use dump_callback() as the callback routine so that
4266** the contents of the query are output as SQL statements.
4267**
4268** If we get a SQLITE_CORRUPT error, rerun the query after appending
4269** "ORDER BY rowid DESC" to the end.
4270*/
4271static int run_schema_dump_query(
4272  ShellState *p,
4273  const char *zQuery
4274){
4275  int rc;
4276  char *zErr = 0;
4277  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4278  if( rc==SQLITE_CORRUPT ){
4279    char *zQ2;
4280    int len = strlen30(zQuery);
4281    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4282    if( zErr ){
4283      utf8_printf(p->out, "/****** %s ******/\n", zErr);
4284      sqlite3_free(zErr);
4285      zErr = 0;
4286    }
4287    zQ2 = malloc( len+100 );
4288    if( zQ2==0 ) return rc;
4289    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4290    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4291    if( rc ){
4292      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4293    }else{
4294      rc = SQLITE_CORRUPT;
4295    }
4296    sqlite3_free(zErr);
4297    free(zQ2);
4298  }
4299  return rc;
4300}
4301
4302/*
4303** Text of help messages.
4304**
4305** The help text for each individual command begins with a line that starts
4306** with ".".  Subsequent lines are supplemental information.
4307**
4308** There must be two or more spaces between the end of the command and the
4309** start of the description of what that command does.
4310*/
4311static const char *(azHelp[]) = {
4312#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4313  && !defined(SQLITE_SHELL_FIDDLE)
4314  ".archive ...             Manage SQL archives",
4315  "   Each command must have exactly one of the following options:",
4316  "     -c, --create               Create a new archive",
4317  "     -u, --update               Add or update files with changed mtime",
4318  "     -i, --insert               Like -u but always add even if unchanged",
4319  "     -r, --remove               Remove files from archive",
4320  "     -t, --list                 List contents of archive",
4321  "     -x, --extract              Extract files from archive",
4322  "   Optional arguments:",
4323  "     -v, --verbose              Print each filename as it is processed",
4324  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
4325  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
4326  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
4327  "     -g, --glob                 Use glob matching for names in archive",
4328  "     -n, --dryrun               Show the SQL that would have occurred",
4329  "   Examples:",
4330  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
4331  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
4332  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
4333  "   See also:",
4334  "      http://sqlite.org/cli.html#sqlite_archive_support",
4335#endif
4336#ifndef SQLITE_OMIT_AUTHORIZATION
4337  ".auth ON|OFF             Show authorizer callbacks",
4338#endif
4339#ifndef SQLITE_SHELL_FIDDLE
4340  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
4341  "   Options:",
4342  "       --append            Use the appendvfs",
4343  "       --async             Write to FILE without journal and fsync()",
4344#endif
4345  ".bail on|off             Stop after hitting an error.  Default OFF",
4346  ".binary on|off           Turn binary output on or off.  Default OFF",
4347#ifndef SQLITE_SHELL_FIDDLE
4348  ".cd DIRECTORY            Change the working directory to DIRECTORY",
4349#endif
4350  ".changes on|off          Show number of rows changed by SQL",
4351#ifndef SQLITE_SHELL_FIDDLE
4352  ".check GLOB              Fail if output since .testcase does not match",
4353  ".clone NEWDB             Clone data into NEWDB from the existing database",
4354#endif
4355  ".connection [close] [#]  Open or close an auxiliary database connection",
4356  ".databases               List names and files of attached databases",
4357  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4358#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4359  ".dbinfo ?DB?             Show status information about the database",
4360#endif
4361  ".dump ?OBJECTS?          Render database content as SQL",
4362  "   Options:",
4363  "     --data-only            Output only INSERT statements",
4364  "     --newlines             Allow unescaped newline characters in output",
4365  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4366  "     --preserve-rowids      Include ROWID values in the output",
4367  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4368  "   Additional LIKE patterns can be given in subsequent arguments",
4369  ".echo on|off             Turn command echo on or off",
4370  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4371  "   Other Modes:",
4372#ifdef SQLITE_DEBUG
4373  "      test                  Show raw EXPLAIN QUERY PLAN output",
4374  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4375#endif
4376  "      trigger               Like \"full\" but also show trigger bytecode",
4377#ifndef SQLITE_SHELL_FIDDLE
4378  ".excel                   Display the output of next command in spreadsheet",
4379  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4380#endif
4381#ifndef SQLITE_SHELL_FIDDLE
4382  ".exit ?CODE?             Exit this program with return-code CODE",
4383#endif
4384  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4385  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4386  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4387  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4388  "   --help                  Show CMD details",
4389  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4390  ".headers on|off          Turn display of headers on or off",
4391  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4392#ifndef SQLITE_SHELL_FIDDLE
4393  ".import FILE TABLE       Import data from FILE into TABLE",
4394  "   Options:",
4395  "     --ascii               Use \\037 and \\036 as column and row separators",
4396  "     --csv                 Use , and \\n as column and row separators",
4397  "     --skip N              Skip the first N rows of input",
4398  "     --schema S            Target table to be S.TABLE",
4399  "     -v                    \"Verbose\" - increase auxiliary output",
4400  "   Notes:",
4401  "     *  If TABLE does not exist, it is created.  The first row of input",
4402  "        determines the column names.",
4403  "     *  If neither --csv or --ascii are used, the input mode is derived",
4404  "        from the \".mode\" output mode",
4405  "     *  If FILE begins with \"|\" then it is a command that generates the",
4406  "        input text.",
4407#endif
4408#ifndef SQLITE_OMIT_TEST_CONTROL
4409  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4410#endif
4411  ".indexes ?TABLE?         Show names of indexes",
4412  "                           If TABLE is specified, only show indexes for",
4413  "                           tables matching TABLE using the LIKE operator.",
4414#ifdef SQLITE_ENABLE_IOTRACE
4415  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4416#endif
4417  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4418  ".lint OPTIONS            Report potential schema issues.",
4419  "     Options:",
4420  "        fkey-indexes     Find missing foreign key indexes",
4421#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4422  ".load FILE ?ENTRY?       Load an extension library",
4423#endif
4424#ifndef SQLITE_SHELL_FIDDLE
4425  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4426#endif
4427  ".mode MODE ?OPTIONS?     Set output mode",
4428  "   MODE is one of:",
4429  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
4430  "     box         Tables using unicode box-drawing characters",
4431  "     csv         Comma-separated values",
4432  "     column      Output in columns.  (See .width)",
4433  "     html        HTML <table> code",
4434  "     insert      SQL insert statements for TABLE",
4435  "     json        Results in a JSON array",
4436  "     line        One value per line",
4437  "     list        Values delimited by \"|\"",
4438  "     markdown    Markdown table format",
4439  "     qbox        Shorthand for \"box --wrap 60 --quote\"",
4440  "     quote       Escape answers as for SQL",
4441  "     table       ASCII-art table",
4442  "     tabs        Tab-separated values",
4443  "     tcl         TCL list elements",
4444  "   OPTIONS: (for columnar modes or insert mode):",
4445  "     --wrap N       Wrap output lines to no longer than N characters",
4446  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
4447  "     --ww           Shorthand for \"--wordwrap 1\"",
4448  "     --quote        Quote output text as SQL literals",
4449  "     --noquote      Do not quote output text",
4450  "     TABLE          The name of SQL table used for \"insert\" mode",
4451#ifndef SQLITE_SHELL_FIDDLE
4452  ".nonce STRING            Suspend safe mode for one command if nonce matches",
4453#endif
4454  ".nullvalue STRING        Use STRING in place of NULL values",
4455#ifndef SQLITE_SHELL_FIDDLE
4456  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4457  "     If FILE begins with '|' then open as a pipe",
4458  "       --bom  Put a UTF8 byte-order mark at the beginning",
4459  "       -e     Send output to the system text editor",
4460  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4461  /* Note that .open is (partially) available in WASM builds but is
4462  ** currently only intended to be used by the fiddle tool, not
4463  ** end users, so is "undocumented." */
4464  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4465  "     Options:",
4466  "        --append        Use appendvfs to append database to the end of FILE",
4467#endif
4468#ifndef SQLITE_OMIT_DESERIALIZE
4469  "        --deserialize   Load into memory using sqlite3_deserialize()",
4470  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4471  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4472#endif
4473  "        --new           Initialize FILE to an empty database",
4474  "        --nofollow      Do not follow symbolic links",
4475  "        --readonly      Open FILE readonly",
4476  "        --zip           FILE is a ZIP archive",
4477#ifndef SQLITE_SHELL_FIDDLE
4478  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4479  "   If FILE begins with '|' then open it as a pipe.",
4480  "   Options:",
4481  "     --bom                 Prefix output with a UTF8 byte-order mark",
4482  "     -e                    Send output to the system text editor",
4483  "     -x                    Send output as CSV to a spreadsheet",
4484#endif
4485  ".parameter CMD ...       Manage SQL parameter bindings",
4486  "   clear                   Erase all bindings",
4487  "   init                    Initialize the TEMP table that holds bindings",
4488  "   list                    List the current parameter bindings",
4489  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4490  "                           PARAMETER should start with one of: $ : @ ?",
4491  "   unset PARAMETER         Remove PARAMETER from the binding table",
4492  ".print STRING...         Print literal STRING",
4493#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4494  ".progress N              Invoke progress handler after every N opcodes",
4495  "   --limit N                 Interrupt after N progress callbacks",
4496  "   --once                    Do no more than one progress interrupt",
4497  "   --quiet|-q                No output except at interrupts",
4498  "   --reset                   Reset the count for each input and interrupt",
4499#endif
4500  ".prompt MAIN CONTINUE    Replace the standard prompts",
4501#ifndef SQLITE_SHELL_FIDDLE
4502  ".quit                    Exit this program",
4503  ".read FILE               Read input from FILE or command output",
4504  "    If FILE begins with \"|\", it is a command that generates the input.",
4505#endif
4506#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4507  ".recover                 Recover as much data as possible from corrupt db.",
4508  "   --freelist-corrupt       Assume the freelist is corrupt",
4509  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4510  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4511  "   --no-rowids              Do not attempt to recover rowid values",
4512  "                            that are not also INTEGER PRIMARY KEYs",
4513#endif
4514#ifndef SQLITE_SHELL_FIDDLE
4515  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4516  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
4517#endif
4518  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4519  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4520  "   Options:",
4521  "      --indent             Try to pretty-print the schema",
4522  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4523  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4524  "    Options:",
4525  "       --init               Create a new SELFTEST table",
4526  "       -v                   Verbose output",
4527  ".separator COL ?ROW?     Change the column and row separators",
4528#if defined(SQLITE_ENABLE_SESSION)
4529  ".session ?NAME? CMD ...  Create or control sessions",
4530  "   Subcommands:",
4531  "     attach TABLE             Attach TABLE",
4532  "     changeset FILE           Write a changeset into FILE",
4533  "     close                    Close one session",
4534  "     enable ?BOOLEAN?         Set or query the enable bit",
4535  "     filter GLOB...           Reject tables matching GLOBs",
4536  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4537  "     isempty                  Query whether the session is empty",
4538  "     list                     List currently open session names",
4539  "     open DB NAME             Open a new session on DB",
4540  "     patchset FILE            Write a patchset into FILE",
4541  "   If ?NAME? is omitted, the first defined session is used.",
4542#endif
4543  ".sha3sum ...             Compute a SHA3 hash of database content",
4544  "    Options:",
4545  "      --schema              Also hash the sqlite_schema table",
4546  "      --sha3-224            Use the sha3-224 algorithm",
4547  "      --sha3-256            Use the sha3-256 algorithm (default)",
4548  "      --sha3-384            Use the sha3-384 algorithm",
4549  "      --sha3-512            Use the sha3-512 algorithm",
4550  "    Any other argument is a LIKE pattern for tables to hash",
4551#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4552  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4553#endif
4554  ".show                    Show the current values for various settings",
4555  ".stats ?ARG?             Show stats or turn stats on or off",
4556  "   off                      Turn off automatic stat display",
4557  "   on                       Turn on automatic stat display",
4558  "   stmt                     Show statement stats",
4559  "   vmstep                   Show the virtual machine step count only",
4560#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4561  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4562#endif
4563  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4564#ifndef SQLITE_SHELL_FIDDLE
4565  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4566#endif
4567  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4568  "                           Run \".testctrl\" with no arguments for details",
4569  ".timeout MS              Try opening locked tables for MS milliseconds",
4570  ".timer on|off            Turn SQL timer on or off",
4571#ifndef SQLITE_OMIT_TRACE
4572  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4573  "    FILE                    Send output to FILE",
4574  "    stdout                  Send output to stdout",
4575  "    stderr                  Send output to stderr",
4576  "    off                     Disable tracing",
4577  "    --expanded              Expand query parameters",
4578#ifdef SQLITE_ENABLE_NORMALIZE
4579  "    --normalized            Normal the SQL statements",
4580#endif
4581  "    --plain                 Show SQL as it is input",
4582  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4583  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4584  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4585  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4586#endif /* SQLITE_OMIT_TRACE */
4587#ifdef SQLITE_DEBUG
4588  ".unmodule NAME ...       Unregister virtual table modules",
4589  "    --allexcept             Unregister everything except those named",
4590#endif
4591  ".vfsinfo ?AUX?           Information about the top-level VFS",
4592  ".vfslist                 List all available VFSes",
4593  ".vfsname ?AUX?           Print the name of the VFS stack",
4594  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4595  "     Negative values right-justify",
4596};
4597
4598/*
4599** Output help text.
4600**
4601** zPattern describes the set of commands for which help text is provided.
4602** If zPattern is NULL, then show all commands, but only give a one-line
4603** description of each.
4604**
4605** Return the number of matches.
4606*/
4607static int showHelp(FILE *out, const char *zPattern){
4608  int i = 0;
4609  int j = 0;
4610  int n = 0;
4611  char *zPat;
4612  if( zPattern==0
4613   || zPattern[0]=='0'
4614   || cli_strcmp(zPattern,"-a")==0
4615   || cli_strcmp(zPattern,"-all")==0
4616   || cli_strcmp(zPattern,"--all")==0
4617  ){
4618    /* Show all commands, but only one line per command */
4619    if( zPattern==0 ) zPattern = "";
4620    for(i=0; i<ArraySize(azHelp); i++){
4621      if( azHelp[i][0]=='.' || zPattern[0] ){
4622        utf8_printf(out, "%s\n", azHelp[i]);
4623        n++;
4624      }
4625    }
4626  }else{
4627    /* Look for commands that for which zPattern is an exact prefix */
4628    zPat = sqlite3_mprintf(".%s*", zPattern);
4629    shell_check_oom(zPat);
4630    for(i=0; i<ArraySize(azHelp); i++){
4631      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4632        utf8_printf(out, "%s\n", azHelp[i]);
4633        j = i+1;
4634        n++;
4635      }
4636    }
4637    sqlite3_free(zPat);
4638    if( n ){
4639      if( n==1 ){
4640        /* when zPattern is a prefix of exactly one command, then include the
4641        ** details of that command, which should begin at offset j */
4642        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4643          utf8_printf(out, "%s\n", azHelp[j]);
4644          j++;
4645        }
4646      }
4647      return n;
4648    }
4649    /* Look for commands that contain zPattern anywhere.  Show the complete
4650    ** text of all commands that match. */
4651    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4652    shell_check_oom(zPat);
4653    for(i=0; i<ArraySize(azHelp); i++){
4654      if( azHelp[i][0]=='.' ) j = i;
4655      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4656        utf8_printf(out, "%s\n", azHelp[j]);
4657        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4658          j++;
4659          utf8_printf(out, "%s\n", azHelp[j]);
4660        }
4661        i = j;
4662        n++;
4663      }
4664    }
4665    sqlite3_free(zPat);
4666  }
4667  return n;
4668}
4669
4670/* Forward reference */
4671static int process_input(ShellState *p);
4672
4673/*
4674** Read the content of file zName into memory obtained from sqlite3_malloc64()
4675** and return a pointer to the buffer. The caller is responsible for freeing
4676** the memory.
4677**
4678** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4679** read.
4680**
4681** For convenience, a nul-terminator byte is always appended to the data read
4682** from the file before the buffer is returned. This byte is not included in
4683** the final value of (*pnByte), if applicable.
4684**
4685** NULL is returned if any error is encountered. The final value of *pnByte
4686** is undefined in this case.
4687*/
4688static char *readFile(const char *zName, int *pnByte){
4689  FILE *in = fopen(zName, "rb");
4690  long nIn;
4691  size_t nRead;
4692  char *pBuf;
4693  if( in==0 ) return 0;
4694  fseek(in, 0, SEEK_END);
4695  nIn = ftell(in);
4696  rewind(in);
4697  pBuf = sqlite3_malloc64( nIn+1 );
4698  if( pBuf==0 ){ fclose(in); return 0; }
4699  nRead = fread(pBuf, nIn, 1, in);
4700  fclose(in);
4701  if( nRead!=1 ){
4702    sqlite3_free(pBuf);
4703    return 0;
4704  }
4705  pBuf[nIn] = 0;
4706  if( pnByte ) *pnByte = nIn;
4707  return pBuf;
4708}
4709
4710#if defined(SQLITE_ENABLE_SESSION)
4711/*
4712** Close a single OpenSession object and release all of its associated
4713** resources.
4714*/
4715static void session_close(OpenSession *pSession){
4716  int i;
4717  sqlite3session_delete(pSession->p);
4718  sqlite3_free(pSession->zName);
4719  for(i=0; i<pSession->nFilter; i++){
4720    sqlite3_free(pSession->azFilter[i]);
4721  }
4722  sqlite3_free(pSession->azFilter);
4723  memset(pSession, 0, sizeof(OpenSession));
4724}
4725#endif
4726
4727/*
4728** Close all OpenSession objects and release all associated resources.
4729*/
4730#if defined(SQLITE_ENABLE_SESSION)
4731static void session_close_all(ShellState *p, int i){
4732  int j;
4733  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4734  for(j=0; j<pAuxDb->nSession; j++){
4735    session_close(&pAuxDb->aSession[j]);
4736  }
4737  pAuxDb->nSession = 0;
4738}
4739#else
4740# define session_close_all(X,Y)
4741#endif
4742
4743/*
4744** Implementation of the xFilter function for an open session.  Omit
4745** any tables named by ".session filter" but let all other table through.
4746*/
4747#if defined(SQLITE_ENABLE_SESSION)
4748static int session_filter(void *pCtx, const char *zTab){
4749  OpenSession *pSession = (OpenSession*)pCtx;
4750  int i;
4751  for(i=0; i<pSession->nFilter; i++){
4752    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4753  }
4754  return 1;
4755}
4756#endif
4757
4758/*
4759** Try to deduce the type of file for zName based on its content.  Return
4760** one of the SHELL_OPEN_* constants.
4761**
4762** If the file does not exist or is empty but its name looks like a ZIP
4763** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4764** Otherwise, assume an ordinary database regardless of the filename if
4765** the type cannot be determined from content.
4766*/
4767int deduceDatabaseType(const char *zName, int dfltZip){
4768  FILE *f = fopen(zName, "rb");
4769  size_t n;
4770  int rc = SHELL_OPEN_UNSPEC;
4771  char zBuf[100];
4772  if( f==0 ){
4773    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4774       return SHELL_OPEN_ZIPFILE;
4775    }else{
4776       return SHELL_OPEN_NORMAL;
4777    }
4778  }
4779  n = fread(zBuf, 16, 1, f);
4780  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4781    fclose(f);
4782    return SHELL_OPEN_NORMAL;
4783  }
4784  fseek(f, -25, SEEK_END);
4785  n = fread(zBuf, 25, 1, f);
4786  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4787    rc = SHELL_OPEN_APPENDVFS;
4788  }else{
4789    fseek(f, -22, SEEK_END);
4790    n = fread(zBuf, 22, 1, f);
4791    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4792       && zBuf[3]==0x06 ){
4793      rc = SHELL_OPEN_ZIPFILE;
4794    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4795      rc = SHELL_OPEN_ZIPFILE;
4796    }
4797  }
4798  fclose(f);
4799  return rc;
4800}
4801
4802#ifndef SQLITE_OMIT_DESERIALIZE
4803/*
4804** Reconstruct an in-memory database using the output from the "dbtotxt"
4805** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4806** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4807*/
4808static unsigned char *readHexDb(ShellState *p, int *pnData){
4809  unsigned char *a = 0;
4810  int nLine;
4811  int n = 0;
4812  int pgsz = 0;
4813  int iOffset = 0;
4814  int j, k;
4815  int rc;
4816  FILE *in;
4817  const char *zDbFilename = p->pAuxDb->zDbFilename;
4818  unsigned int x[16];
4819  char zLine[1000];
4820  if( zDbFilename ){
4821    in = fopen(zDbFilename, "r");
4822    if( in==0 ){
4823      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4824      return 0;
4825    }
4826    nLine = 0;
4827  }else{
4828    in = p->in;
4829    nLine = p->lineno;
4830    if( in==0 ) in = stdin;
4831  }
4832  *pnData = 0;
4833  nLine++;
4834  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4835  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4836  if( rc!=2 ) goto readHexDb_error;
4837  if( n<0 ) goto readHexDb_error;
4838  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4839  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4840  a = sqlite3_malloc( n ? n : 1 );
4841  shell_check_oom(a);
4842  memset(a, 0, n);
4843  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4844    utf8_printf(stderr, "invalid pagesize\n");
4845    goto readHexDb_error;
4846  }
4847  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4848    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4849    if( rc==2 ){
4850      iOffset = k;
4851      continue;
4852    }
4853    if( cli_strncmp(zLine, "| end ", 6)==0 ){
4854      break;
4855    }
4856    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4857                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4858                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4859    if( rc==17 ){
4860      k = iOffset+j;
4861      if( k+16<=n && k>=0 ){
4862        int ii;
4863        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4864      }
4865    }
4866  }
4867  *pnData = n;
4868  if( in!=p->in ){
4869    fclose(in);
4870  }else{
4871    p->lineno = nLine;
4872  }
4873  return a;
4874
4875readHexDb_error:
4876  if( in!=p->in ){
4877    fclose(in);
4878  }else{
4879    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4880      nLine++;
4881      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
4882    }
4883    p->lineno = nLine;
4884  }
4885  sqlite3_free(a);
4886  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4887  return 0;
4888}
4889#endif /* SQLITE_OMIT_DESERIALIZE */
4890
4891/*
4892** Scalar function "shell_int32". The first argument to this function
4893** must be a blob. The second a non-negative integer. This function
4894** reads and returns a 32-bit big-endian integer from byte
4895** offset (4*<arg2>) of the blob.
4896*/
4897static void shellInt32(
4898  sqlite3_context *context,
4899  int argc,
4900  sqlite3_value **argv
4901){
4902  const unsigned char *pBlob;
4903  int nBlob;
4904  int iInt;
4905
4906  UNUSED_PARAMETER(argc);
4907  nBlob = sqlite3_value_bytes(argv[0]);
4908  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4909  iInt = sqlite3_value_int(argv[1]);
4910
4911  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4912    const unsigned char *a = &pBlob[iInt*4];
4913    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4914                       + ((sqlite3_int64)a[1]<<16)
4915                       + ((sqlite3_int64)a[2]<< 8)
4916                       + ((sqlite3_int64)a[3]<< 0);
4917    sqlite3_result_int64(context, iVal);
4918  }
4919}
4920
4921/*
4922** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4923** using "..." with internal double-quote characters doubled.
4924*/
4925static void shellIdQuote(
4926  sqlite3_context *context,
4927  int argc,
4928  sqlite3_value **argv
4929){
4930  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4931  UNUSED_PARAMETER(argc);
4932  if( zName ){
4933    char *z = sqlite3_mprintf("\"%w\"", zName);
4934    sqlite3_result_text(context, z, -1, sqlite3_free);
4935  }
4936}
4937
4938/*
4939** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4940*/
4941static void shellUSleepFunc(
4942  sqlite3_context *context,
4943  int argcUnused,
4944  sqlite3_value **argv
4945){
4946  int sleep = sqlite3_value_int(argv[0]);
4947  (void)argcUnused;
4948  sqlite3_sleep(sleep/1000);
4949  sqlite3_result_int(context, sleep);
4950}
4951
4952/*
4953** Scalar function "shell_escape_crnl" used by the .recover command.
4954** The argument passed to this function is the output of built-in
4955** function quote(). If the first character of the input is "'",
4956** indicating that the value passed to quote() was a text value,
4957** then this function searches the input for "\n" and "\r" characters
4958** and adds a wrapper similar to the following:
4959**
4960**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4961**
4962** Or, if the first character of the input is not "'", then a copy
4963** of the input is returned.
4964*/
4965static void shellEscapeCrnl(
4966  sqlite3_context *context,
4967  int argc,
4968  sqlite3_value **argv
4969){
4970  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4971  UNUSED_PARAMETER(argc);
4972  if( zText && zText[0]=='\'' ){
4973    i64 nText = sqlite3_value_bytes(argv[0]);
4974    i64 i;
4975    char zBuf1[20];
4976    char zBuf2[20];
4977    const char *zNL = 0;
4978    const char *zCR = 0;
4979    i64 nCR = 0;
4980    i64 nNL = 0;
4981
4982    for(i=0; zText[i]; i++){
4983      if( zNL==0 && zText[i]=='\n' ){
4984        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4985        nNL = strlen(zNL);
4986      }
4987      if( zCR==0 && zText[i]=='\r' ){
4988        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4989        nCR = strlen(zCR);
4990      }
4991    }
4992
4993    if( zNL || zCR ){
4994      i64 iOut = 0;
4995      i64 nMax = (nNL > nCR) ? nNL : nCR;
4996      i64 nAlloc = nMax * nText + (nMax+64)*2;
4997      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4998      if( zOut==0 ){
4999        sqlite3_result_error_nomem(context);
5000        return;
5001      }
5002
5003      if( zNL && zCR ){
5004        memcpy(&zOut[iOut], "replace(replace(", 16);
5005        iOut += 16;
5006      }else{
5007        memcpy(&zOut[iOut], "replace(", 8);
5008        iOut += 8;
5009      }
5010      for(i=0; zText[i]; i++){
5011        if( zText[i]=='\n' ){
5012          memcpy(&zOut[iOut], zNL, nNL);
5013          iOut += nNL;
5014        }else if( zText[i]=='\r' ){
5015          memcpy(&zOut[iOut], zCR, nCR);
5016          iOut += nCR;
5017        }else{
5018          zOut[iOut] = zText[i];
5019          iOut++;
5020        }
5021      }
5022
5023      if( zNL ){
5024        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5025        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
5026        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
5027      }
5028      if( zCR ){
5029        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5030        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
5031        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
5032      }
5033
5034      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
5035      sqlite3_free(zOut);
5036      return;
5037    }
5038  }
5039
5040  sqlite3_result_value(context, argv[0]);
5041}
5042
5043/* Flags for open_db().
5044**
5045** The default behavior of open_db() is to exit(1) if the database fails to
5046** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5047** but still returns without calling exit.
5048**
5049** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5050** ZIP archive if the file does not exist or is empty and its name matches
5051** the *.zip pattern.
5052*/
5053#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
5054#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
5055
5056/*
5057** Make sure the database is open.  If it is not, then open it.  If
5058** the database fails to open, print an error message and exit.
5059*/
5060static void open_db(ShellState *p, int openFlags){
5061  if( p->db==0 ){
5062    const char *zDbFilename = p->pAuxDb->zDbFilename;
5063    if( p->openMode==SHELL_OPEN_UNSPEC ){
5064      if( zDbFilename==0 || zDbFilename[0]==0 ){
5065        p->openMode = SHELL_OPEN_NORMAL;
5066      }else{
5067        p->openMode = (u8)deduceDatabaseType(zDbFilename,
5068                             (openFlags & OPEN_DB_ZIPFILE)!=0);
5069      }
5070    }
5071    switch( p->openMode ){
5072      case SHELL_OPEN_APPENDVFS: {
5073        sqlite3_open_v2(zDbFilename, &p->db,
5074           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5075        break;
5076      }
5077      case SHELL_OPEN_HEXDB:
5078      case SHELL_OPEN_DESERIALIZE: {
5079        sqlite3_open(0, &p->db);
5080        break;
5081      }
5082      case SHELL_OPEN_ZIPFILE: {
5083        sqlite3_open(":memory:", &p->db);
5084        break;
5085      }
5086      case SHELL_OPEN_READONLY: {
5087        sqlite3_open_v2(zDbFilename, &p->db,
5088            SQLITE_OPEN_READONLY|p->openFlags, 0);
5089        break;
5090      }
5091      case SHELL_OPEN_UNSPEC:
5092      case SHELL_OPEN_NORMAL: {
5093        sqlite3_open_v2(zDbFilename, &p->db,
5094           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5095        break;
5096      }
5097    }
5098    globalDb = p->db;
5099    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5100      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5101          zDbFilename, sqlite3_errmsg(p->db));
5102      if( openFlags & OPEN_DB_KEEPALIVE ){
5103        sqlite3_open(":memory:", &p->db);
5104        return;
5105      }
5106      exit(1);
5107    }
5108#ifndef SQLITE_OMIT_LOAD_EXTENSION
5109    sqlite3_enable_load_extension(p->db, 1);
5110#endif
5111    sqlite3_shathree_init(p->db, 0, 0);
5112    sqlite3_uint_init(p->db, 0, 0);
5113    sqlite3_decimal_init(p->db, 0, 0);
5114    sqlite3_regexp_init(p->db, 0, 0);
5115    sqlite3_ieee_init(p->db, 0, 0);
5116    sqlite3_series_init(p->db, 0, 0);
5117#ifndef SQLITE_SHELL_FIDDLE
5118    sqlite3_fileio_init(p->db, 0, 0);
5119    sqlite3_completion_init(p->db, 0, 0);
5120#endif
5121#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5122    sqlite3_dbdata_init(p->db, 0, 0);
5123#endif
5124#ifdef SQLITE_HAVE_ZLIB
5125    if( !p->bSafeModePersist ){
5126      sqlite3_zipfile_init(p->db, 0, 0);
5127      sqlite3_sqlar_init(p->db, 0, 0);
5128    }
5129#endif
5130    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5131                            shellAddSchemaName, 0, 0);
5132    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5133                            shellModuleSchema, 0, 0);
5134    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5135                            shellPutsFunc, 0, 0);
5136    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
5137                            shellEscapeCrnl, 0, 0);
5138    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
5139                            shellInt32, 0, 0);
5140    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
5141                            shellIdQuote, 0, 0);
5142    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5143                            shellUSleepFunc, 0, 0);
5144#ifndef SQLITE_NOHAVE_SYSTEM
5145    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5146                            editFunc, 0, 0);
5147    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5148                            editFunc, 0, 0);
5149#endif
5150    if( p->openMode==SHELL_OPEN_ZIPFILE ){
5151      char *zSql = sqlite3_mprintf(
5152         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5153      shell_check_oom(zSql);
5154      sqlite3_exec(p->db, zSql, 0, 0, 0);
5155      sqlite3_free(zSql);
5156    }
5157#ifndef SQLITE_OMIT_DESERIALIZE
5158    else
5159    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5160      int rc;
5161      int nData = 0;
5162      unsigned char *aData;
5163      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5164        aData = (unsigned char*)readFile(zDbFilename, &nData);
5165      }else{
5166        aData = readHexDb(p, &nData);
5167        if( aData==0 ){
5168          return;
5169        }
5170      }
5171      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5172                   SQLITE_DESERIALIZE_RESIZEABLE |
5173                   SQLITE_DESERIALIZE_FREEONCLOSE);
5174      if( rc ){
5175        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5176      }
5177      if( p->szMax>0 ){
5178        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5179      }
5180    }
5181#endif
5182  }
5183  if( p->bSafeModePersist && p->db!=0 ){
5184    sqlite3_set_authorizer(p->db, safeModeAuth, p);
5185  }
5186}
5187
5188/*
5189** Attempt to close the databaes connection.  Report errors.
5190*/
5191void close_db(sqlite3 *db){
5192  int rc = sqlite3_close(db);
5193  if( rc ){
5194    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5195        rc, sqlite3_errmsg(db));
5196  }
5197}
5198
5199#if HAVE_READLINE || HAVE_EDITLINE
5200/*
5201** Readline completion callbacks
5202*/
5203static char *readline_completion_generator(const char *text, int state){
5204  static sqlite3_stmt *pStmt = 0;
5205  char *zRet;
5206  if( state==0 ){
5207    char *zSql;
5208    sqlite3_finalize(pStmt);
5209    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5210                           "  FROM completion(%Q) ORDER BY 1", text);
5211    shell_check_oom(zSql);
5212    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5213    sqlite3_free(zSql);
5214  }
5215  if( sqlite3_step(pStmt)==SQLITE_ROW ){
5216    const char *z = (const char*)sqlite3_column_text(pStmt,0);
5217    zRet = z ? strdup(z) : 0;
5218  }else{
5219    sqlite3_finalize(pStmt);
5220    pStmt = 0;
5221    zRet = 0;
5222  }
5223  return zRet;
5224}
5225static char **readline_completion(const char *zText, int iStart, int iEnd){
5226  rl_attempted_completion_over = 1;
5227  return rl_completion_matches(zText, readline_completion_generator);
5228}
5229
5230#elif HAVE_LINENOISE
5231/*
5232** Linenoise completion callback
5233*/
5234static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5235  i64 nLine = strlen(zLine);
5236  i64 i, iStart;
5237  sqlite3_stmt *pStmt = 0;
5238  char *zSql;
5239  char zBuf[1000];
5240
5241  if( nLine>sizeof(zBuf)-30 ) return;
5242  if( zLine[0]=='.' || zLine[0]=='#') return;
5243  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5244  if( i==nLine-1 ) return;
5245  iStart = i+1;
5246  memcpy(zBuf, zLine, iStart);
5247  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5248                         "  FROM completion(%Q,%Q) ORDER BY 1",
5249                         &zLine[iStart], zLine);
5250  shell_check_oom(zSql);
5251  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5252  sqlite3_free(zSql);
5253  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5254  while( sqlite3_step(pStmt)==SQLITE_ROW ){
5255    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5256    int nCompletion = sqlite3_column_bytes(pStmt, 0);
5257    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
5258      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5259      linenoiseAddCompletion(lc, zBuf);
5260    }
5261  }
5262  sqlite3_finalize(pStmt);
5263}
5264#endif
5265
5266/*
5267** Do C-language style dequoting.
5268**
5269**    \a    -> alarm
5270**    \b    -> backspace
5271**    \t    -> tab
5272**    \n    -> newline
5273**    \v    -> vertical tab
5274**    \f    -> form feed
5275**    \r    -> carriage return
5276**    \s    -> space
5277**    \"    -> "
5278**    \'    -> '
5279**    \\    -> backslash
5280**    \NNN  -> ascii character NNN in octal
5281*/
5282static void resolve_backslashes(char *z){
5283  int i, j;
5284  char c;
5285  while( *z && *z!='\\' ) z++;
5286  for(i=j=0; (c = z[i])!=0; i++, j++){
5287    if( c=='\\' && z[i+1]!=0 ){
5288      c = z[++i];
5289      if( c=='a' ){
5290        c = '\a';
5291      }else if( c=='b' ){
5292        c = '\b';
5293      }else if( c=='t' ){
5294        c = '\t';
5295      }else if( c=='n' ){
5296        c = '\n';
5297      }else if( c=='v' ){
5298        c = '\v';
5299      }else if( c=='f' ){
5300        c = '\f';
5301      }else if( c=='r' ){
5302        c = '\r';
5303      }else if( c=='"' ){
5304        c = '"';
5305      }else if( c=='\'' ){
5306        c = '\'';
5307      }else if( c=='\\' ){
5308        c = '\\';
5309      }else if( c>='0' && c<='7' ){
5310        c -= '0';
5311        if( z[i+1]>='0' && z[i+1]<='7' ){
5312          i++;
5313          c = (c<<3) + z[i] - '0';
5314          if( z[i+1]>='0' && z[i+1]<='7' ){
5315            i++;
5316            c = (c<<3) + z[i] - '0';
5317          }
5318        }
5319      }
5320    }
5321    z[j] = c;
5322  }
5323  if( j<i ) z[j] = 0;
5324}
5325
5326/*
5327** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
5328** for TRUE and FALSE.  Return the integer value if appropriate.
5329*/
5330static int booleanValue(const char *zArg){
5331  int i;
5332  if( zArg[0]=='0' && zArg[1]=='x' ){
5333    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5334  }else{
5335    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5336  }
5337  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5338  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5339    return 1;
5340  }
5341  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5342    return 0;
5343  }
5344  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5345          zArg);
5346  return 0;
5347}
5348
5349/*
5350** Set or clear a shell flag according to a boolean value.
5351*/
5352static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5353  if( booleanValue(zArg) ){
5354    ShellSetFlag(p, mFlag);
5355  }else{
5356    ShellClearFlag(p, mFlag);
5357  }
5358}
5359
5360/*
5361** Close an output file, assuming it is not stderr or stdout
5362*/
5363static void output_file_close(FILE *f){
5364  if( f && f!=stdout && f!=stderr ) fclose(f);
5365}
5366
5367/*
5368** Try to open an output file.   The names "stdout" and "stderr" are
5369** recognized and do the right thing.  NULL is returned if the output
5370** filename is "off".
5371*/
5372static FILE *output_file_open(const char *zFile, int bTextMode){
5373  FILE *f;
5374  if( cli_strcmp(zFile,"stdout")==0 ){
5375    f = stdout;
5376  }else if( cli_strcmp(zFile, "stderr")==0 ){
5377    f = stderr;
5378  }else if( cli_strcmp(zFile, "off")==0 ){
5379    f = 0;
5380  }else{
5381    f = fopen(zFile, bTextMode ? "w" : "wb");
5382    if( f==0 ){
5383      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5384    }
5385  }
5386  return f;
5387}
5388
5389#ifndef SQLITE_OMIT_TRACE
5390/*
5391** A routine for handling output from sqlite3_trace().
5392*/
5393static int sql_trace_callback(
5394  unsigned mType,         /* The trace type */
5395  void *pArg,             /* The ShellState pointer */
5396  void *pP,               /* Usually a pointer to sqlite_stmt */
5397  void *pX                /* Auxiliary output */
5398){
5399  ShellState *p = (ShellState*)pArg;
5400  sqlite3_stmt *pStmt;
5401  const char *zSql;
5402  i64 nSql;
5403  if( p->traceOut==0 ) return 0;
5404  if( mType==SQLITE_TRACE_CLOSE ){
5405    utf8_printf(p->traceOut, "-- closing database connection\n");
5406    return 0;
5407  }
5408  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5409    zSql = (const char*)pX;
5410  }else{
5411    pStmt = (sqlite3_stmt*)pP;
5412    switch( p->eTraceType ){
5413      case SHELL_TRACE_EXPANDED: {
5414        zSql = sqlite3_expanded_sql(pStmt);
5415        break;
5416      }
5417#ifdef SQLITE_ENABLE_NORMALIZE
5418      case SHELL_TRACE_NORMALIZED: {
5419        zSql = sqlite3_normalized_sql(pStmt);
5420        break;
5421      }
5422#endif
5423      default: {
5424        zSql = sqlite3_sql(pStmt);
5425        break;
5426      }
5427    }
5428  }
5429  if( zSql==0 ) return 0;
5430  nSql = strlen(zSql);
5431  if( nSql>1000000000 ) nSql = 1000000000;
5432  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5433  switch( mType ){
5434    case SQLITE_TRACE_ROW:
5435    case SQLITE_TRACE_STMT: {
5436      utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5437      break;
5438    }
5439    case SQLITE_TRACE_PROFILE: {
5440      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5441      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5442      break;
5443    }
5444  }
5445  return 0;
5446}
5447#endif
5448
5449/*
5450** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5451** a useful spot to set a debugger breakpoint.
5452*/
5453static void test_breakpoint(void){
5454  static int nCall = 0;
5455  nCall++;
5456}
5457
5458/*
5459** An object used to read a CSV and other files for import.
5460*/
5461typedef struct ImportCtx ImportCtx;
5462struct ImportCtx {
5463  const char *zFile;  /* Name of the input file */
5464  FILE *in;           /* Read the CSV text from this input stream */
5465  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5466  char *z;            /* Accumulated text for a field */
5467  int n;              /* Number of bytes in z */
5468  int nAlloc;         /* Space allocated for z[] */
5469  int nLine;          /* Current line number */
5470  int nRow;           /* Number of rows imported */
5471  int nErr;           /* Number of errors encountered */
5472  int bNotFirst;      /* True if one or more bytes already read */
5473  int cTerm;          /* Character that terminated the most recent field */
5474  int cColSep;        /* The column separator character.  (Usually ",") */
5475  int cRowSep;        /* The row separator character.  (Usually "\n") */
5476};
5477
5478/* Clean up resourced used by an ImportCtx */
5479static void import_cleanup(ImportCtx *p){
5480  if( p->in!=0 && p->xCloser!=0 ){
5481    p->xCloser(p->in);
5482    p->in = 0;
5483  }
5484  sqlite3_free(p->z);
5485  p->z = 0;
5486}
5487
5488/* Append a single byte to z[] */
5489static void import_append_char(ImportCtx *p, int c){
5490  if( p->n+1>=p->nAlloc ){
5491    p->nAlloc += p->nAlloc + 100;
5492    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5493    shell_check_oom(p->z);
5494  }
5495  p->z[p->n++] = (char)c;
5496}
5497
5498/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5499** with the option of having a separator other than ",".
5500**
5501**   +  Input comes from p->in.
5502**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5503**      from sqlite3_malloc64().
5504**   +  Use p->cSep as the column separator.  The default is ",".
5505**   +  Use p->rSep as the row separator.  The default is "\n".
5506**   +  Keep track of the line number in p->nLine.
5507**   +  Store the character that terminates the field in p->cTerm.  Store
5508**      EOF on end-of-file.
5509**   +  Report syntax errors on stderr
5510*/
5511static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5512  int c;
5513  int cSep = p->cColSep;
5514  int rSep = p->cRowSep;
5515  p->n = 0;
5516  c = fgetc(p->in);
5517  if( c==EOF || seenInterrupt ){
5518    p->cTerm = EOF;
5519    return 0;
5520  }
5521  if( c=='"' ){
5522    int pc, ppc;
5523    int startLine = p->nLine;
5524    int cQuote = c;
5525    pc = ppc = 0;
5526    while( 1 ){
5527      c = fgetc(p->in);
5528      if( c==rSep ) p->nLine++;
5529      if( c==cQuote ){
5530        if( pc==cQuote ){
5531          pc = 0;
5532          continue;
5533        }
5534      }
5535      if( (c==cSep && pc==cQuote)
5536       || (c==rSep && pc==cQuote)
5537       || (c==rSep && pc=='\r' && ppc==cQuote)
5538       || (c==EOF && pc==cQuote)
5539      ){
5540        do{ p->n--; }while( p->z[p->n]!=cQuote );
5541        p->cTerm = c;
5542        break;
5543      }
5544      if( pc==cQuote && c!='\r' ){
5545        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5546                p->zFile, p->nLine, cQuote);
5547      }
5548      if( c==EOF ){
5549        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5550                p->zFile, startLine, cQuote);
5551        p->cTerm = c;
5552        break;
5553      }
5554      import_append_char(p, c);
5555      ppc = pc;
5556      pc = c;
5557    }
5558  }else{
5559    /* If this is the first field being parsed and it begins with the
5560    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5561    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5562      import_append_char(p, c);
5563      c = fgetc(p->in);
5564      if( (c&0xff)==0xbb ){
5565        import_append_char(p, c);
5566        c = fgetc(p->in);
5567        if( (c&0xff)==0xbf ){
5568          p->bNotFirst = 1;
5569          p->n = 0;
5570          return csv_read_one_field(p);
5571        }
5572      }
5573    }
5574    while( c!=EOF && c!=cSep && c!=rSep ){
5575      import_append_char(p, c);
5576      c = fgetc(p->in);
5577    }
5578    if( c==rSep ){
5579      p->nLine++;
5580      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5581    }
5582    p->cTerm = c;
5583  }
5584  if( p->z ) p->z[p->n] = 0;
5585  p->bNotFirst = 1;
5586  return p->z;
5587}
5588
5589/* Read a single field of ASCII delimited text.
5590**
5591**   +  Input comes from p->in.
5592**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5593**      from sqlite3_malloc64().
5594**   +  Use p->cSep as the column separator.  The default is "\x1F".
5595**   +  Use p->rSep as the row separator.  The default is "\x1E".
5596**   +  Keep track of the row number in p->nLine.
5597**   +  Store the character that terminates the field in p->cTerm.  Store
5598**      EOF on end-of-file.
5599**   +  Report syntax errors on stderr
5600*/
5601static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5602  int c;
5603  int cSep = p->cColSep;
5604  int rSep = p->cRowSep;
5605  p->n = 0;
5606  c = fgetc(p->in);
5607  if( c==EOF || seenInterrupt ){
5608    p->cTerm = EOF;
5609    return 0;
5610  }
5611  while( c!=EOF && c!=cSep && c!=rSep ){
5612    import_append_char(p, c);
5613    c = fgetc(p->in);
5614  }
5615  if( c==rSep ){
5616    p->nLine++;
5617  }
5618  p->cTerm = c;
5619  if( p->z ) p->z[p->n] = 0;
5620  return p->z;
5621}
5622
5623/*
5624** Try to transfer data for table zTable.  If an error is seen while
5625** moving forward, try to go backwards.  The backwards movement won't
5626** work for WITHOUT ROWID tables.
5627*/
5628static void tryToCloneData(
5629  ShellState *p,
5630  sqlite3 *newDb,
5631  const char *zTable
5632){
5633  sqlite3_stmt *pQuery = 0;
5634  sqlite3_stmt *pInsert = 0;
5635  char *zQuery = 0;
5636  char *zInsert = 0;
5637  int rc;
5638  int i, j, n;
5639  int nTable = strlen30(zTable);
5640  int k = 0;
5641  int cnt = 0;
5642  const int spinRate = 10000;
5643
5644  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5645  shell_check_oom(zQuery);
5646  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5647  if( rc ){
5648    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5649            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5650            zQuery);
5651    goto end_data_xfer;
5652  }
5653  n = sqlite3_column_count(pQuery);
5654  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5655  shell_check_oom(zInsert);
5656  sqlite3_snprintf(200+nTable,zInsert,
5657                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5658  i = strlen30(zInsert);
5659  for(j=1; j<n; j++){
5660    memcpy(zInsert+i, ",?", 2);
5661    i += 2;
5662  }
5663  memcpy(zInsert+i, ");", 3);
5664  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5665  if( rc ){
5666    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5667            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5668            zQuery);
5669    goto end_data_xfer;
5670  }
5671  for(k=0; k<2; k++){
5672    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5673      for(i=0; i<n; i++){
5674        switch( sqlite3_column_type(pQuery, i) ){
5675          case SQLITE_NULL: {
5676            sqlite3_bind_null(pInsert, i+1);
5677            break;
5678          }
5679          case SQLITE_INTEGER: {
5680            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5681            break;
5682          }
5683          case SQLITE_FLOAT: {
5684            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5685            break;
5686          }
5687          case SQLITE_TEXT: {
5688            sqlite3_bind_text(pInsert, i+1,
5689                             (const char*)sqlite3_column_text(pQuery,i),
5690                             -1, SQLITE_STATIC);
5691            break;
5692          }
5693          case SQLITE_BLOB: {
5694            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5695                                            sqlite3_column_bytes(pQuery,i),
5696                                            SQLITE_STATIC);
5697            break;
5698          }
5699        }
5700      } /* End for */
5701      rc = sqlite3_step(pInsert);
5702      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5703        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5704                        sqlite3_errmsg(newDb));
5705      }
5706      sqlite3_reset(pInsert);
5707      cnt++;
5708      if( (cnt%spinRate)==0 ){
5709        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5710        fflush(stdout);
5711      }
5712    } /* End while */
5713    if( rc==SQLITE_DONE ) break;
5714    sqlite3_finalize(pQuery);
5715    sqlite3_free(zQuery);
5716    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5717                             zTable);
5718    shell_check_oom(zQuery);
5719    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5720    if( rc ){
5721      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5722      break;
5723    }
5724  } /* End for(k=0...) */
5725
5726end_data_xfer:
5727  sqlite3_finalize(pQuery);
5728  sqlite3_finalize(pInsert);
5729  sqlite3_free(zQuery);
5730  sqlite3_free(zInsert);
5731}
5732
5733
5734/*
5735** Try to transfer all rows of the schema that match zWhere.  For
5736** each row, invoke xForEach() on the object defined by that row.
5737** If an error is encountered while moving forward through the
5738** sqlite_schema table, try again moving backwards.
5739*/
5740static void tryToCloneSchema(
5741  ShellState *p,
5742  sqlite3 *newDb,
5743  const char *zWhere,
5744  void (*xForEach)(ShellState*,sqlite3*,const char*)
5745){
5746  sqlite3_stmt *pQuery = 0;
5747  char *zQuery = 0;
5748  int rc;
5749  const unsigned char *zName;
5750  const unsigned char *zSql;
5751  char *zErrMsg = 0;
5752
5753  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5754                           " WHERE %s", zWhere);
5755  shell_check_oom(zQuery);
5756  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5757  if( rc ){
5758    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5759                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5760                    zQuery);
5761    goto end_schema_xfer;
5762  }
5763  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5764    zName = sqlite3_column_text(pQuery, 0);
5765    zSql = sqlite3_column_text(pQuery, 1);
5766    if( zName==0 || zSql==0 ) continue;
5767    printf("%s... ", zName); fflush(stdout);
5768    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5769    if( zErrMsg ){
5770      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5771      sqlite3_free(zErrMsg);
5772      zErrMsg = 0;
5773    }
5774    if( xForEach ){
5775      xForEach(p, newDb, (const char*)zName);
5776    }
5777    printf("done\n");
5778  }
5779  if( rc!=SQLITE_DONE ){
5780    sqlite3_finalize(pQuery);
5781    sqlite3_free(zQuery);
5782    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5783                             " WHERE %s ORDER BY rowid DESC", zWhere);
5784    shell_check_oom(zQuery);
5785    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5786    if( rc ){
5787      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5788                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5789                      zQuery);
5790      goto end_schema_xfer;
5791    }
5792    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5793      zName = sqlite3_column_text(pQuery, 0);
5794      zSql = sqlite3_column_text(pQuery, 1);
5795      if( zName==0 || zSql==0 ) continue;
5796      printf("%s... ", zName); fflush(stdout);
5797      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5798      if( zErrMsg ){
5799        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5800        sqlite3_free(zErrMsg);
5801        zErrMsg = 0;
5802      }
5803      if( xForEach ){
5804        xForEach(p, newDb, (const char*)zName);
5805      }
5806      printf("done\n");
5807    }
5808  }
5809end_schema_xfer:
5810  sqlite3_finalize(pQuery);
5811  sqlite3_free(zQuery);
5812}
5813
5814/*
5815** Open a new database file named "zNewDb".  Try to recover as much information
5816** as possible out of the main database (which might be corrupt) and write it
5817** into zNewDb.
5818*/
5819static void tryToClone(ShellState *p, const char *zNewDb){
5820  int rc;
5821  sqlite3 *newDb = 0;
5822  if( access(zNewDb,0)==0 ){
5823    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5824    return;
5825  }
5826  rc = sqlite3_open(zNewDb, &newDb);
5827  if( rc ){
5828    utf8_printf(stderr, "Cannot create output database: %s\n",
5829            sqlite3_errmsg(newDb));
5830  }else{
5831    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5832    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5833    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5834    tryToCloneSchema(p, newDb, "type!='table'", 0);
5835    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5836    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5837  }
5838  close_db(newDb);
5839}
5840
5841/*
5842** Change the output file back to stdout.
5843**
5844** If the p->doXdgOpen flag is set, that means the output was being
5845** redirected to a temporary file named by p->zTempFile.  In that case,
5846** launch start/open/xdg-open on that temporary file.
5847*/
5848static void output_reset(ShellState *p){
5849  if( p->outfile[0]=='|' ){
5850#ifndef SQLITE_OMIT_POPEN
5851    pclose(p->out);
5852#endif
5853  }else{
5854    output_file_close(p->out);
5855#ifndef SQLITE_NOHAVE_SYSTEM
5856    if( p->doXdgOpen ){
5857      const char *zXdgOpenCmd =
5858#if defined(_WIN32)
5859      "start";
5860#elif defined(__APPLE__)
5861      "open";
5862#else
5863      "xdg-open";
5864#endif
5865      char *zCmd;
5866      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5867      if( system(zCmd) ){
5868        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5869      }else{
5870        /* Give the start/open/xdg-open command some time to get
5871        ** going before we continue, and potential delete the
5872        ** p->zTempFile data file out from under it */
5873        sqlite3_sleep(2000);
5874      }
5875      sqlite3_free(zCmd);
5876      outputModePop(p);
5877      p->doXdgOpen = 0;
5878    }
5879#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5880  }
5881  p->outfile[0] = 0;
5882  p->out = stdout;
5883}
5884
5885/*
5886** Run an SQL command and return the single integer result.
5887*/
5888static int db_int(sqlite3 *db, const char *zSql){
5889  sqlite3_stmt *pStmt;
5890  int res = 0;
5891  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
5892  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5893    res = sqlite3_column_int(pStmt,0);
5894  }
5895  sqlite3_finalize(pStmt);
5896  return res;
5897}
5898
5899#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5900/*
5901** Convert a 2-byte or 4-byte big-endian integer into a native integer
5902*/
5903static unsigned int get2byteInt(unsigned char *a){
5904  return (a[0]<<8) + a[1];
5905}
5906static unsigned int get4byteInt(unsigned char *a){
5907  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5908}
5909
5910/*
5911** Implementation of the ".dbinfo" command.
5912**
5913** Return 1 on error, 2 to exit, and 0 otherwise.
5914*/
5915static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5916  static const struct { const char *zName; int ofst; } aField[] = {
5917     { "file change counter:",  24  },
5918     { "database page count:",  28  },
5919     { "freelist page count:",  36  },
5920     { "schema cookie:",        40  },
5921     { "schema format:",        44  },
5922     { "default cache size:",   48  },
5923     { "autovacuum top root:",  52  },
5924     { "incremental vacuum:",   64  },
5925     { "text encoding:",        56  },
5926     { "user version:",         60  },
5927     { "application id:",       68  },
5928     { "software version:",     96  },
5929  };
5930  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5931     { "number of tables:",
5932       "SELECT count(*) FROM %s WHERE type='table'" },
5933     { "number of indexes:",
5934       "SELECT count(*) FROM %s WHERE type='index'" },
5935     { "number of triggers:",
5936       "SELECT count(*) FROM %s WHERE type='trigger'" },
5937     { "number of views:",
5938       "SELECT count(*) FROM %s WHERE type='view'" },
5939     { "schema size:",
5940       "SELECT total(length(sql)) FROM %s" },
5941  };
5942  int i, rc;
5943  unsigned iDataVersion;
5944  char *zSchemaTab;
5945  char *zDb = nArg>=2 ? azArg[1] : "main";
5946  sqlite3_stmt *pStmt = 0;
5947  unsigned char aHdr[100];
5948  open_db(p, 0);
5949  if( p->db==0 ) return 1;
5950  rc = sqlite3_prepare_v2(p->db,
5951             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5952             -1, &pStmt, 0);
5953  if( rc ){
5954    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5955    sqlite3_finalize(pStmt);
5956    return 1;
5957  }
5958  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5959  if( sqlite3_step(pStmt)==SQLITE_ROW
5960   && sqlite3_column_bytes(pStmt,0)>100
5961  ){
5962    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5963    sqlite3_finalize(pStmt);
5964  }else{
5965    raw_printf(stderr, "unable to read database header\n");
5966    sqlite3_finalize(pStmt);
5967    return 1;
5968  }
5969  i = get2byteInt(aHdr+16);
5970  if( i==1 ) i = 65536;
5971  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5972  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5973  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5974  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5975  for(i=0; i<ArraySize(aField); i++){
5976    int ofst = aField[i].ofst;
5977    unsigned int val = get4byteInt(aHdr + ofst);
5978    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5979    switch( ofst ){
5980      case 56: {
5981        if( val==1 ) raw_printf(p->out, " (utf8)");
5982        if( val==2 ) raw_printf(p->out, " (utf16le)");
5983        if( val==3 ) raw_printf(p->out, " (utf16be)");
5984      }
5985    }
5986    raw_printf(p->out, "\n");
5987  }
5988  if( zDb==0 ){
5989    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5990  }else if( cli_strcmp(zDb,"temp")==0 ){
5991    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5992  }else{
5993    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5994  }
5995  for(i=0; i<ArraySize(aQuery); i++){
5996    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5997    int val = db_int(p->db, zSql);
5998    sqlite3_free(zSql);
5999    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
6000  }
6001  sqlite3_free(zSchemaTab);
6002  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
6003  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
6004  return 0;
6005}
6006#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
6007          && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
6008
6009/*
6010** Print the current sqlite3_errmsg() value to stderr and return 1.
6011*/
6012static int shellDatabaseError(sqlite3 *db){
6013  const char *zErr = sqlite3_errmsg(db);
6014  utf8_printf(stderr, "Error: %s\n", zErr);
6015  return 1;
6016}
6017
6018/*
6019** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
6020** if they match and FALSE (0) if they do not match.
6021**
6022** Globbing rules:
6023**
6024**      '*'       Matches any sequence of zero or more characters.
6025**
6026**      '?'       Matches exactly one character.
6027**
6028**     [...]      Matches one character from the enclosed list of
6029**                characters.
6030**
6031**     [^...]     Matches one character not in the enclosed list.
6032**
6033**      '#'       Matches any sequence of one or more digits with an
6034**                optional + or - sign in front
6035**
6036**      ' '       Any span of whitespace matches any other span of
6037**                whitespace.
6038**
6039** Extra whitespace at the end of z[] is ignored.
6040*/
6041static int testcase_glob(const char *zGlob, const char *z){
6042  int c, c2;
6043  int invert;
6044  int seen;
6045
6046  while( (c = (*(zGlob++)))!=0 ){
6047    if( IsSpace(c) ){
6048      if( !IsSpace(*z) ) return 0;
6049      while( IsSpace(*zGlob) ) zGlob++;
6050      while( IsSpace(*z) ) z++;
6051    }else if( c=='*' ){
6052      while( (c=(*(zGlob++))) == '*' || c=='?' ){
6053        if( c=='?' && (*(z++))==0 ) return 0;
6054      }
6055      if( c==0 ){
6056        return 1;
6057      }else if( c=='[' ){
6058        while( *z && testcase_glob(zGlob-1,z)==0 ){
6059          z++;
6060        }
6061        return (*z)!=0;
6062      }
6063      while( (c2 = (*(z++)))!=0 ){
6064        while( c2!=c ){
6065          c2 = *(z++);
6066          if( c2==0 ) return 0;
6067        }
6068        if( testcase_glob(zGlob,z) ) return 1;
6069      }
6070      return 0;
6071    }else if( c=='?' ){
6072      if( (*(z++))==0 ) return 0;
6073    }else if( c=='[' ){
6074      int prior_c = 0;
6075      seen = 0;
6076      invert = 0;
6077      c = *(z++);
6078      if( c==0 ) return 0;
6079      c2 = *(zGlob++);
6080      if( c2=='^' ){
6081        invert = 1;
6082        c2 = *(zGlob++);
6083      }
6084      if( c2==']' ){
6085        if( c==']' ) seen = 1;
6086        c2 = *(zGlob++);
6087      }
6088      while( c2 && c2!=']' ){
6089        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6090          c2 = *(zGlob++);
6091          if( c>=prior_c && c<=c2 ) seen = 1;
6092          prior_c = 0;
6093        }else{
6094          if( c==c2 ){
6095            seen = 1;
6096          }
6097          prior_c = c2;
6098        }
6099        c2 = *(zGlob++);
6100      }
6101      if( c2==0 || (seen ^ invert)==0 ) return 0;
6102    }else if( c=='#' ){
6103      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6104      if( !IsDigit(z[0]) ) return 0;
6105      z++;
6106      while( IsDigit(z[0]) ){ z++; }
6107    }else{
6108      if( c!=(*(z++)) ) return 0;
6109    }
6110  }
6111  while( IsSpace(*z) ){ z++; }
6112  return *z==0;
6113}
6114
6115
6116/*
6117** Compare the string as a command-line option with either one or two
6118** initial "-" characters.
6119*/
6120static int optionMatch(const char *zStr, const char *zOpt){
6121  if( zStr[0]!='-' ) return 0;
6122  zStr++;
6123  if( zStr[0]=='-' ) zStr++;
6124  return cli_strcmp(zStr, zOpt)==0;
6125}
6126
6127/*
6128** Delete a file.
6129*/
6130int shellDeleteFile(const char *zFilename){
6131  int rc;
6132#ifdef _WIN32
6133  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6134  rc = _wunlink(z);
6135  sqlite3_free(z);
6136#else
6137  rc = unlink(zFilename);
6138#endif
6139  return rc;
6140}
6141
6142/*
6143** Try to delete the temporary file (if there is one) and free the
6144** memory used to hold the name of the temp file.
6145*/
6146static void clearTempFile(ShellState *p){
6147  if( p->zTempFile==0 ) return;
6148  if( p->doXdgOpen ) return;
6149  if( shellDeleteFile(p->zTempFile) ) return;
6150  sqlite3_free(p->zTempFile);
6151  p->zTempFile = 0;
6152}
6153
6154/*
6155** Create a new temp file name with the given suffix.
6156*/
6157static void newTempFile(ShellState *p, const char *zSuffix){
6158  clearTempFile(p);
6159  sqlite3_free(p->zTempFile);
6160  p->zTempFile = 0;
6161  if( p->db ){
6162    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6163  }
6164  if( p->zTempFile==0 ){
6165    /* If p->db is an in-memory database then the TEMPFILENAME file-control
6166    ** will not work and we will need to fallback to guessing */
6167    char *zTemp;
6168    sqlite3_uint64 r;
6169    sqlite3_randomness(sizeof(r), &r);
6170    zTemp = getenv("TEMP");
6171    if( zTemp==0 ) zTemp = getenv("TMP");
6172    if( zTemp==0 ){
6173#ifdef _WIN32
6174      zTemp = "\\tmp";
6175#else
6176      zTemp = "/tmp";
6177#endif
6178    }
6179    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6180  }else{
6181    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6182  }
6183  shell_check_oom(p->zTempFile);
6184}
6185
6186
6187/*
6188** The implementation of SQL scalar function fkey_collate_clause(), used
6189** by the ".lint fkey-indexes" command. This scalar function is always
6190** called with four arguments - the parent table name, the parent column name,
6191** the child table name and the child column name.
6192**
6193**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6194**
6195** If either of the named tables or columns do not exist, this function
6196** returns an empty string. An empty string is also returned if both tables
6197** and columns exist but have the same default collation sequence. Or,
6198** if both exist but the default collation sequences are different, this
6199** function returns the string " COLLATE <parent-collation>", where
6200** <parent-collation> is the default collation sequence of the parent column.
6201*/
6202static void shellFkeyCollateClause(
6203  sqlite3_context *pCtx,
6204  int nVal,
6205  sqlite3_value **apVal
6206){
6207  sqlite3 *db = sqlite3_context_db_handle(pCtx);
6208  const char *zParent;
6209  const char *zParentCol;
6210  const char *zParentSeq;
6211  const char *zChild;
6212  const char *zChildCol;
6213  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
6214  int rc;
6215
6216  assert( nVal==4 );
6217  zParent = (const char*)sqlite3_value_text(apVal[0]);
6218  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6219  zChild = (const char*)sqlite3_value_text(apVal[2]);
6220  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6221
6222  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6223  rc = sqlite3_table_column_metadata(
6224      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6225  );
6226  if( rc==SQLITE_OK ){
6227    rc = sqlite3_table_column_metadata(
6228        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6229    );
6230  }
6231
6232  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6233    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6234    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6235    sqlite3_free(z);
6236  }
6237}
6238
6239
6240/*
6241** The implementation of dot-command ".lint fkey-indexes".
6242*/
6243static int lintFkeyIndexes(
6244  ShellState *pState,             /* Current shell tool state */
6245  char **azArg,                   /* Array of arguments passed to dot command */
6246  int nArg                        /* Number of entries in azArg[] */
6247){
6248  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
6249  FILE *out = pState->out;        /* Stream to write non-error output to */
6250  int bVerbose = 0;               /* If -verbose is present */
6251  int bGroupByParent = 0;         /* If -groupbyparent is present */
6252  int i;                          /* To iterate through azArg[] */
6253  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
6254  int rc;                         /* Return code */
6255  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
6256
6257  /*
6258  ** This SELECT statement returns one row for each foreign key constraint
6259  ** in the schema of the main database. The column values are:
6260  **
6261  ** 0. The text of an SQL statement similar to:
6262  **
6263  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6264  **
6265  **    This SELECT is similar to the one that the foreign keys implementation
6266  **    needs to run internally on child tables. If there is an index that can
6267  **    be used to optimize this query, then it can also be used by the FK
6268  **    implementation to optimize DELETE or UPDATE statements on the parent
6269  **    table.
6270  **
6271  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6272  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6273  **    contains an index that can be used to optimize the query.
6274  **
6275  ** 2. Human readable text that describes the child table and columns. e.g.
6276  **
6277  **       "child_table(child_key1, child_key2)"
6278  **
6279  ** 3. Human readable text that describes the parent table and columns. e.g.
6280  **
6281  **       "parent_table(parent_key1, parent_key2)"
6282  **
6283  ** 4. A full CREATE INDEX statement for an index that could be used to
6284  **    optimize DELETE or UPDATE statements on the parent table. e.g.
6285  **
6286  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
6287  **
6288  ** 5. The name of the parent table.
6289  **
6290  ** These six values are used by the C logic below to generate the report.
6291  */
6292  const char *zSql =
6293  "SELECT "
6294    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6295    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6296    "  || fkey_collate_clause("
6297    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6298    ", "
6299    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6300    "  || group_concat('*=?', ' AND ') || ')'"
6301    ", "
6302    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
6303    ", "
6304    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6305    ", "
6306    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6307    "  || ' ON ' || quote(s.name) || '('"
6308    "  || group_concat(quote(f.[from]) ||"
6309    "        fkey_collate_clause("
6310    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6311    "  || ');'"
6312    ", "
6313    "     f.[table] "
6314    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6315    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6316    "GROUP BY s.name, f.id "
6317    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6318  ;
6319  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6320
6321  for(i=2; i<nArg; i++){
6322    int n = strlen30(azArg[i]);
6323    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6324      bVerbose = 1;
6325    }
6326    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6327      bGroupByParent = 1;
6328      zIndent = "    ";
6329    }
6330    else{
6331      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6332          azArg[0], azArg[1]
6333      );
6334      return SQLITE_ERROR;
6335    }
6336  }
6337
6338  /* Register the fkey_collate_clause() SQL function */
6339  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6340      0, shellFkeyCollateClause, 0, 0
6341  );
6342
6343
6344  if( rc==SQLITE_OK ){
6345    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6346  }
6347  if( rc==SQLITE_OK ){
6348    sqlite3_bind_int(pSql, 1, bGroupByParent);
6349  }
6350
6351  if( rc==SQLITE_OK ){
6352    int rc2;
6353    char *zPrev = 0;
6354    while( SQLITE_ROW==sqlite3_step(pSql) ){
6355      int res = -1;
6356      sqlite3_stmt *pExplain = 0;
6357      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6358      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6359      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6360      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6361      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6362      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6363
6364      if( zEQP==0 ) continue;
6365      if( zGlob==0 ) continue;
6366      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6367      if( rc!=SQLITE_OK ) break;
6368      if( SQLITE_ROW==sqlite3_step(pExplain) ){
6369        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6370        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
6371                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
6372      }
6373      rc = sqlite3_finalize(pExplain);
6374      if( rc!=SQLITE_OK ) break;
6375
6376      if( res<0 ){
6377        raw_printf(stderr, "Error: internal error");
6378        break;
6379      }else{
6380        if( bGroupByParent
6381        && (bVerbose || res==0)
6382        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6383        ){
6384          raw_printf(out, "-- Parent table %s\n", zParent);
6385          sqlite3_free(zPrev);
6386          zPrev = sqlite3_mprintf("%s", zParent);
6387        }
6388
6389        if( res==0 ){
6390          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6391        }else if( bVerbose ){
6392          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6393              zIndent, zFrom, zTarget
6394          );
6395        }
6396      }
6397    }
6398    sqlite3_free(zPrev);
6399
6400    if( rc!=SQLITE_OK ){
6401      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6402    }
6403
6404    rc2 = sqlite3_finalize(pSql);
6405    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6406      rc = rc2;
6407      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6408    }
6409  }else{
6410    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6411  }
6412
6413  return rc;
6414}
6415
6416/*
6417** Implementation of ".lint" dot command.
6418*/
6419static int lintDotCommand(
6420  ShellState *pState,             /* Current shell tool state */
6421  char **azArg,                   /* Array of arguments passed to dot command */
6422  int nArg                        /* Number of entries in azArg[] */
6423){
6424  int n;
6425  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6426  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6427  return lintFkeyIndexes(pState, azArg, nArg);
6428
6429 usage:
6430  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6431  raw_printf(stderr, "Where sub-commands are:\n");
6432  raw_printf(stderr, "    fkey-indexes\n");
6433  return SQLITE_ERROR;
6434}
6435
6436#if !defined SQLITE_OMIT_VIRTUALTABLE
6437static void shellPrepare(
6438  sqlite3 *db,
6439  int *pRc,
6440  const char *zSql,
6441  sqlite3_stmt **ppStmt
6442){
6443  *ppStmt = 0;
6444  if( *pRc==SQLITE_OK ){
6445    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6446    if( rc!=SQLITE_OK ){
6447      raw_printf(stderr, "sql error: %s (%d)\n",
6448          sqlite3_errmsg(db), sqlite3_errcode(db)
6449      );
6450      *pRc = rc;
6451    }
6452  }
6453}
6454
6455/*
6456** Create a prepared statement using printf-style arguments for the SQL.
6457**
6458** This routine is could be marked "static".  But it is not always used,
6459** depending on compile-time options.  By omitting the "static", we avoid
6460** nuisance compiler warnings about "defined but not used".
6461*/
6462void shellPreparePrintf(
6463  sqlite3 *db,
6464  int *pRc,
6465  sqlite3_stmt **ppStmt,
6466  const char *zFmt,
6467  ...
6468){
6469  *ppStmt = 0;
6470  if( *pRc==SQLITE_OK ){
6471    va_list ap;
6472    char *z;
6473    va_start(ap, zFmt);
6474    z = sqlite3_vmprintf(zFmt, ap);
6475    va_end(ap);
6476    if( z==0 ){
6477      *pRc = SQLITE_NOMEM;
6478    }else{
6479      shellPrepare(db, pRc, z, ppStmt);
6480      sqlite3_free(z);
6481    }
6482  }
6483}
6484
6485/* Finalize the prepared statement created using shellPreparePrintf().
6486**
6487** This routine is could be marked "static".  But it is not always used,
6488** depending on compile-time options.  By omitting the "static", we avoid
6489** nuisance compiler warnings about "defined but not used".
6490*/
6491void shellFinalize(
6492  int *pRc,
6493  sqlite3_stmt *pStmt
6494){
6495  if( pStmt ){
6496    sqlite3 *db = sqlite3_db_handle(pStmt);
6497    int rc = sqlite3_finalize(pStmt);
6498    if( *pRc==SQLITE_OK ){
6499      if( rc!=SQLITE_OK ){
6500        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6501      }
6502      *pRc = rc;
6503    }
6504  }
6505}
6506
6507/* Reset the prepared statement created using shellPreparePrintf().
6508**
6509** This routine is could be marked "static".  But it is not always used,
6510** depending on compile-time options.  By omitting the "static", we avoid
6511** nuisance compiler warnings about "defined but not used".
6512*/
6513void shellReset(
6514  int *pRc,
6515  sqlite3_stmt *pStmt
6516){
6517  int rc = sqlite3_reset(pStmt);
6518  if( *pRc==SQLITE_OK ){
6519    if( rc!=SQLITE_OK ){
6520      sqlite3 *db = sqlite3_db_handle(pStmt);
6521      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6522    }
6523    *pRc = rc;
6524  }
6525}
6526#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6527
6528#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6529/******************************************************************************
6530** The ".archive" or ".ar" command.
6531*/
6532/*
6533** Structure representing a single ".ar" command.
6534*/
6535typedef struct ArCommand ArCommand;
6536struct ArCommand {
6537  u8 eCmd;                        /* An AR_CMD_* value */
6538  u8 bVerbose;                    /* True if --verbose */
6539  u8 bZip;                        /* True if the archive is a ZIP */
6540  u8 bDryRun;                     /* True if --dry-run */
6541  u8 bAppend;                     /* True if --append */
6542  u8 bGlob;                       /* True if --glob */
6543  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6544  int nArg;                       /* Number of command arguments */
6545  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6546  const char *zFile;              /* --file argument, or NULL */
6547  const char *zDir;               /* --directory argument, or NULL */
6548  char **azArg;                   /* Array of command arguments */
6549  ShellState *p;                  /* Shell state */
6550  sqlite3 *db;                    /* Database containing the archive */
6551};
6552
6553/*
6554** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6555*/
6556static int arUsage(FILE *f){
6557  showHelp(f,"archive");
6558  return SQLITE_ERROR;
6559}
6560
6561/*
6562** Print an error message for the .ar command to stderr and return
6563** SQLITE_ERROR.
6564*/
6565static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6566  va_list ap;
6567  char *z;
6568  va_start(ap, zFmt);
6569  z = sqlite3_vmprintf(zFmt, ap);
6570  va_end(ap);
6571  utf8_printf(stderr, "Error: %s\n", z);
6572  if( pAr->fromCmdLine ){
6573    utf8_printf(stderr, "Use \"-A\" for more help\n");
6574  }else{
6575    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6576  }
6577  sqlite3_free(z);
6578  return SQLITE_ERROR;
6579}
6580
6581/*
6582** Values for ArCommand.eCmd.
6583*/
6584#define AR_CMD_CREATE       1
6585#define AR_CMD_UPDATE       2
6586#define AR_CMD_INSERT       3
6587#define AR_CMD_EXTRACT      4
6588#define AR_CMD_LIST         5
6589#define AR_CMD_HELP         6
6590#define AR_CMD_REMOVE       7
6591
6592/*
6593** Other (non-command) switches.
6594*/
6595#define AR_SWITCH_VERBOSE     8
6596#define AR_SWITCH_FILE        9
6597#define AR_SWITCH_DIRECTORY  10
6598#define AR_SWITCH_APPEND     11
6599#define AR_SWITCH_DRYRUN     12
6600#define AR_SWITCH_GLOB       13
6601
6602static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6603  switch( eSwitch ){
6604    case AR_CMD_CREATE:
6605    case AR_CMD_EXTRACT:
6606    case AR_CMD_LIST:
6607    case AR_CMD_REMOVE:
6608    case AR_CMD_UPDATE:
6609    case AR_CMD_INSERT:
6610    case AR_CMD_HELP:
6611      if( pAr->eCmd ){
6612        return arErrorMsg(pAr, "multiple command options");
6613      }
6614      pAr->eCmd = eSwitch;
6615      break;
6616
6617    case AR_SWITCH_DRYRUN:
6618      pAr->bDryRun = 1;
6619      break;
6620    case AR_SWITCH_GLOB:
6621      pAr->bGlob = 1;
6622      break;
6623    case AR_SWITCH_VERBOSE:
6624      pAr->bVerbose = 1;
6625      break;
6626    case AR_SWITCH_APPEND:
6627      pAr->bAppend = 1;
6628      /* Fall thru into --file */
6629    case AR_SWITCH_FILE:
6630      pAr->zFile = zArg;
6631      break;
6632    case AR_SWITCH_DIRECTORY:
6633      pAr->zDir = zArg;
6634      break;
6635  }
6636
6637  return SQLITE_OK;
6638}
6639
6640/*
6641** Parse the command line for an ".ar" command. The results are written into
6642** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6643** successfully, otherwise an error message is written to stderr and
6644** SQLITE_ERROR returned.
6645*/
6646static int arParseCommand(
6647  char **azArg,                   /* Array of arguments passed to dot command */
6648  int nArg,                       /* Number of entries in azArg[] */
6649  ArCommand *pAr                  /* Populate this object */
6650){
6651  struct ArSwitch {
6652    const char *zLong;
6653    char cShort;
6654    u8 eSwitch;
6655    u8 bArg;
6656  } aSwitch[] = {
6657    { "create",    'c', AR_CMD_CREATE,       0 },
6658    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6659    { "insert",    'i', AR_CMD_INSERT,       0 },
6660    { "list",      't', AR_CMD_LIST,         0 },
6661    { "remove",    'r', AR_CMD_REMOVE,       0 },
6662    { "update",    'u', AR_CMD_UPDATE,       0 },
6663    { "help",      'h', AR_CMD_HELP,         0 },
6664    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6665    { "file",      'f', AR_SWITCH_FILE,      1 },
6666    { "append",    'a', AR_SWITCH_APPEND,    1 },
6667    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6668    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6669    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6670  };
6671  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6672  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6673
6674  if( nArg<=1 ){
6675    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6676    return arUsage(stderr);
6677  }else{
6678    char *z = azArg[1];
6679    if( z[0]!='-' ){
6680      /* Traditional style [tar] invocation */
6681      int i;
6682      int iArg = 2;
6683      for(i=0; z[i]; i++){
6684        const char *zArg = 0;
6685        struct ArSwitch *pOpt;
6686        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6687          if( z[i]==pOpt->cShort ) break;
6688        }
6689        if( pOpt==pEnd ){
6690          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6691        }
6692        if( pOpt->bArg ){
6693          if( iArg>=nArg ){
6694            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6695          }
6696          zArg = azArg[iArg++];
6697        }
6698        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6699      }
6700      pAr->nArg = nArg-iArg;
6701      if( pAr->nArg>0 ){
6702        pAr->azArg = &azArg[iArg];
6703      }
6704    }else{
6705      /* Non-traditional invocation */
6706      int iArg;
6707      for(iArg=1; iArg<nArg; iArg++){
6708        int n;
6709        z = azArg[iArg];
6710        if( z[0]!='-' ){
6711          /* All remaining command line words are command arguments. */
6712          pAr->azArg = &azArg[iArg];
6713          pAr->nArg = nArg-iArg;
6714          break;
6715        }
6716        n = strlen30(z);
6717
6718        if( z[1]!='-' ){
6719          int i;
6720          /* One or more short options */
6721          for(i=1; i<n; i++){
6722            const char *zArg = 0;
6723            struct ArSwitch *pOpt;
6724            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6725              if( z[i]==pOpt->cShort ) break;
6726            }
6727            if( pOpt==pEnd ){
6728              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6729            }
6730            if( pOpt->bArg ){
6731              if( i<(n-1) ){
6732                zArg = &z[i+1];
6733                i = n;
6734              }else{
6735                if( iArg>=(nArg-1) ){
6736                  return arErrorMsg(pAr, "option requires an argument: %c",
6737                                    z[i]);
6738                }
6739                zArg = azArg[++iArg];
6740              }
6741            }
6742            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6743          }
6744        }else if( z[2]=='\0' ){
6745          /* A -- option, indicating that all remaining command line words
6746          ** are command arguments.  */
6747          pAr->azArg = &azArg[iArg+1];
6748          pAr->nArg = nArg-iArg-1;
6749          break;
6750        }else{
6751          /* A long option */
6752          const char *zArg = 0;             /* Argument for option, if any */
6753          struct ArSwitch *pMatch = 0;      /* Matching option */
6754          struct ArSwitch *pOpt;            /* Iterator */
6755          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6756            const char *zLong = pOpt->zLong;
6757            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6758              if( pMatch ){
6759                return arErrorMsg(pAr, "ambiguous option: %s",z);
6760              }else{
6761                pMatch = pOpt;
6762              }
6763            }
6764          }
6765
6766          if( pMatch==0 ){
6767            return arErrorMsg(pAr, "unrecognized option: %s", z);
6768          }
6769          if( pMatch->bArg ){
6770            if( iArg>=(nArg-1) ){
6771              return arErrorMsg(pAr, "option requires an argument: %s", z);
6772            }
6773            zArg = azArg[++iArg];
6774          }
6775          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6776        }
6777      }
6778    }
6779  }
6780
6781  return SQLITE_OK;
6782}
6783
6784/*
6785** This function assumes that all arguments within the ArCommand.azArg[]
6786** array refer to archive members, as for the --extract, --list or --remove
6787** commands. It checks that each of them are "present". If any specified
6788** file is not present in the archive, an error is printed to stderr and an
6789** error code returned. Otherwise, if all specified arguments are present
6790** in the archive, SQLITE_OK is returned. Here, "present" means either an
6791** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6792** when pAr->bGlob is true.
6793**
6794** This function strips any trailing '/' characters from each argument.
6795** This is consistent with the way the [tar] command seems to work on
6796** Linux.
6797*/
6798static int arCheckEntries(ArCommand *pAr){
6799  int rc = SQLITE_OK;
6800  if( pAr->nArg ){
6801    int i, j;
6802    sqlite3_stmt *pTest = 0;
6803    const char *zSel = (pAr->bGlob)
6804      ? "SELECT name FROM %s WHERE glob($name,name)"
6805      : "SELECT name FROM %s WHERE name=$name";
6806
6807    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6808    j = sqlite3_bind_parameter_index(pTest, "$name");
6809    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6810      char *z = pAr->azArg[i];
6811      int n = strlen30(z);
6812      int bOk = 0;
6813      while( n>0 && z[n-1]=='/' ) n--;
6814      z[n] = '\0';
6815      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6816      if( SQLITE_ROW==sqlite3_step(pTest) ){
6817        bOk = 1;
6818      }
6819      shellReset(&rc, pTest);
6820      if( rc==SQLITE_OK && bOk==0 ){
6821        utf8_printf(stderr, "not found in archive: %s\n", z);
6822        rc = SQLITE_ERROR;
6823      }
6824    }
6825    shellFinalize(&rc, pTest);
6826  }
6827  return rc;
6828}
6829
6830/*
6831** Format a WHERE clause that can be used against the "sqlar" table to
6832** identify all archive members that match the command arguments held
6833** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6834** The caller is responsible for eventually calling sqlite3_free() on
6835** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6836** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6837*/
6838static void arWhereClause(
6839  int *pRc,
6840  ArCommand *pAr,
6841  char **pzWhere                  /* OUT: New WHERE clause */
6842){
6843  char *zWhere = 0;
6844  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6845  if( *pRc==SQLITE_OK ){
6846    if( pAr->nArg==0 ){
6847      zWhere = sqlite3_mprintf("1");
6848    }else{
6849      int i;
6850      const char *zSep = "";
6851      for(i=0; i<pAr->nArg; i++){
6852        const char *z = pAr->azArg[i];
6853        zWhere = sqlite3_mprintf(
6854          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6855          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6856        );
6857        if( zWhere==0 ){
6858          *pRc = SQLITE_NOMEM;
6859          break;
6860        }
6861        zSep = " OR ";
6862      }
6863    }
6864  }
6865  *pzWhere = zWhere;
6866}
6867
6868/*
6869** Implementation of .ar "lisT" command.
6870*/
6871static int arListCommand(ArCommand *pAr){
6872  const char *zSql = "SELECT %s FROM %s WHERE %s";
6873  const char *azCols[] = {
6874    "name",
6875    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6876  };
6877
6878  char *zWhere = 0;
6879  sqlite3_stmt *pSql = 0;
6880  int rc;
6881
6882  rc = arCheckEntries(pAr);
6883  arWhereClause(&rc, pAr, &zWhere);
6884
6885  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6886                     pAr->zSrcTable, zWhere);
6887  if( pAr->bDryRun ){
6888    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6889  }else{
6890    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6891      if( pAr->bVerbose ){
6892        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6893            sqlite3_column_text(pSql, 0),
6894            sqlite3_column_int(pSql, 1),
6895            sqlite3_column_text(pSql, 2),
6896            sqlite3_column_text(pSql, 3)
6897        );
6898      }else{
6899        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6900      }
6901    }
6902  }
6903  shellFinalize(&rc, pSql);
6904  sqlite3_free(zWhere);
6905  return rc;
6906}
6907
6908
6909/*
6910** Implementation of .ar "Remove" command.
6911*/
6912static int arRemoveCommand(ArCommand *pAr){
6913  int rc = 0;
6914  char *zSql = 0;
6915  char *zWhere = 0;
6916
6917  if( pAr->nArg ){
6918    /* Verify that args actually exist within the archive before proceeding.
6919    ** And formulate a WHERE clause to match them.  */
6920    rc = arCheckEntries(pAr);
6921    arWhereClause(&rc, pAr, &zWhere);
6922  }
6923  if( rc==SQLITE_OK ){
6924    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6925                           pAr->zSrcTable, zWhere);
6926    if( pAr->bDryRun ){
6927      utf8_printf(pAr->p->out, "%s\n", zSql);
6928    }else{
6929      char *zErr = 0;
6930      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6931      if( rc==SQLITE_OK ){
6932        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6933        if( rc!=SQLITE_OK ){
6934          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6935        }else{
6936          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6937        }
6938      }
6939      if( zErr ){
6940        utf8_printf(stdout, "ERROR: %s\n", zErr);
6941        sqlite3_free(zErr);
6942      }
6943    }
6944  }
6945  sqlite3_free(zWhere);
6946  sqlite3_free(zSql);
6947  return rc;
6948}
6949
6950/*
6951** Implementation of .ar "eXtract" command.
6952*/
6953static int arExtractCommand(ArCommand *pAr){
6954  const char *zSql1 =
6955    "SELECT "
6956    " ($dir || name),"
6957    " writefile(($dir || name), %s, mode, mtime) "
6958    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6959    " AND name NOT GLOB '*..[/\\]*'";
6960
6961  const char *azExtraArg[] = {
6962    "sqlar_uncompress(data, sz)",
6963    "data"
6964  };
6965
6966  sqlite3_stmt *pSql = 0;
6967  int rc = SQLITE_OK;
6968  char *zDir = 0;
6969  char *zWhere = 0;
6970  int i, j;
6971
6972  /* If arguments are specified, check that they actually exist within
6973  ** the archive before proceeding. And formulate a WHERE clause to
6974  ** match them.  */
6975  rc = arCheckEntries(pAr);
6976  arWhereClause(&rc, pAr, &zWhere);
6977
6978  if( rc==SQLITE_OK ){
6979    if( pAr->zDir ){
6980      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6981    }else{
6982      zDir = sqlite3_mprintf("");
6983    }
6984    if( zDir==0 ) rc = SQLITE_NOMEM;
6985  }
6986
6987  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6988      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6989  );
6990
6991  if( rc==SQLITE_OK ){
6992    j = sqlite3_bind_parameter_index(pSql, "$dir");
6993    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6994
6995    /* Run the SELECT statement twice. The first time, writefile() is called
6996    ** for all archive members that should be extracted. The second time,
6997    ** only for the directories. This is because the timestamps for
6998    ** extracted directories must be reset after they are populated (as
6999    ** populating them changes the timestamp).  */
7000    for(i=0; i<2; i++){
7001      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
7002      sqlite3_bind_int(pSql, j, i);
7003      if( pAr->bDryRun ){
7004        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
7005      }else{
7006        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7007          if( i==0 && pAr->bVerbose ){
7008            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
7009          }
7010        }
7011      }
7012      shellReset(&rc, pSql);
7013    }
7014    shellFinalize(&rc, pSql);
7015  }
7016
7017  sqlite3_free(zDir);
7018  sqlite3_free(zWhere);
7019  return rc;
7020}
7021
7022/*
7023** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
7024*/
7025static int arExecSql(ArCommand *pAr, const char *zSql){
7026  int rc;
7027  if( pAr->bDryRun ){
7028    utf8_printf(pAr->p->out, "%s\n", zSql);
7029    rc = SQLITE_OK;
7030  }else{
7031    char *zErr = 0;
7032    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7033    if( zErr ){
7034      utf8_printf(stdout, "ERROR: %s\n", zErr);
7035      sqlite3_free(zErr);
7036    }
7037  }
7038  return rc;
7039}
7040
7041
7042/*
7043** Implementation of .ar "create", "insert", and "update" commands.
7044**
7045**     create    ->     Create a new SQL archive
7046**     insert    ->     Insert or reinsert all files listed
7047**     update    ->     Insert files that have changed or that were not
7048**                      previously in the archive
7049**
7050** Create the "sqlar" table in the database if it does not already exist.
7051** Then add each file in the azFile[] array to the archive. Directories
7052** are added recursively. If argument bVerbose is non-zero, a message is
7053** printed on stdout for each file archived.
7054**
7055** The create command is the same as update, except that it drops
7056** any existing "sqlar" table before beginning.  The "insert" command
7057** always overwrites every file named on the command-line, where as
7058** "update" only overwrites if the size or mtime or mode has changed.
7059*/
7060static int arCreateOrUpdateCommand(
7061  ArCommand *pAr,                 /* Command arguments and options */
7062  int bUpdate,                    /* true for a --create. */
7063  int bOnlyIfChanged              /* Only update if file has changed */
7064){
7065  const char *zCreate =
7066      "CREATE TABLE IF NOT EXISTS sqlar(\n"
7067      "  name TEXT PRIMARY KEY,  -- name of the file\n"
7068      "  mode INT,               -- access permissions\n"
7069      "  mtime INT,              -- last modification time\n"
7070      "  sz INT,                 -- original file size\n"
7071      "  data BLOB               -- compressed content\n"
7072      ")";
7073  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7074  const char *zInsertFmt[2] = {
7075     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7076     "  SELECT\n"
7077     "    %s,\n"
7078     "    mode,\n"
7079     "    mtime,\n"
7080     "    CASE substr(lsmode(mode),1,1)\n"
7081     "      WHEN '-' THEN length(data)\n"
7082     "      WHEN 'd' THEN 0\n"
7083     "      ELSE -1 END,\n"
7084     "    sqlar_compress(data)\n"
7085     "  FROM fsdir(%Q,%Q) AS disk\n"
7086     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7087     ,
7088     "REPLACE INTO %s(name,mode,mtime,data)\n"
7089     "  SELECT\n"
7090     "    %s,\n"
7091     "    mode,\n"
7092     "    mtime,\n"
7093     "    data\n"
7094     "  FROM fsdir(%Q,%Q) AS disk\n"
7095     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7096  };
7097  int i;                          /* For iterating through azFile[] */
7098  int rc;                         /* Return code */
7099  const char *zTab = 0;           /* SQL table into which to insert */
7100  char *zSql;
7101  char zTemp[50];
7102  char *zExists = 0;
7103
7104  arExecSql(pAr, "PRAGMA page_size=512");
7105  rc = arExecSql(pAr, "SAVEPOINT ar;");
7106  if( rc!=SQLITE_OK ) return rc;
7107  zTemp[0] = 0;
7108  if( pAr->bZip ){
7109    /* Initialize the zipfile virtual table, if necessary */
7110    if( pAr->zFile ){
7111      sqlite3_uint64 r;
7112      sqlite3_randomness(sizeof(r),&r);
7113      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7114      zTab = zTemp;
7115      zSql = sqlite3_mprintf(
7116         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7117         zTab, pAr->zFile
7118      );
7119      rc = arExecSql(pAr, zSql);
7120      sqlite3_free(zSql);
7121    }else{
7122      zTab = "zip";
7123    }
7124  }else{
7125    /* Initialize the table for an SQLAR */
7126    zTab = "sqlar";
7127    if( bUpdate==0 ){
7128      rc = arExecSql(pAr, zDrop);
7129      if( rc!=SQLITE_OK ) goto end_ar_transaction;
7130    }
7131    rc = arExecSql(pAr, zCreate);
7132  }
7133  if( bOnlyIfChanged ){
7134    zExists = sqlite3_mprintf(
7135      " AND NOT EXISTS("
7136          "SELECT 1 FROM %s AS mem"
7137          " WHERE mem.name=disk.name"
7138          " AND mem.mtime=disk.mtime"
7139          " AND mem.mode=disk.mode)", zTab);
7140  }else{
7141    zExists = sqlite3_mprintf("");
7142  }
7143  if( zExists==0 ) rc = SQLITE_NOMEM;
7144  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7145    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7146        pAr->bVerbose ? "shell_putsnl(name)" : "name",
7147        pAr->azArg[i], pAr->zDir, zExists);
7148    rc = arExecSql(pAr, zSql2);
7149    sqlite3_free(zSql2);
7150  }
7151end_ar_transaction:
7152  if( rc!=SQLITE_OK ){
7153    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7154  }else{
7155    rc = arExecSql(pAr, "RELEASE ar;");
7156    if( pAr->bZip && pAr->zFile ){
7157      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7158      arExecSql(pAr, zSql);
7159      sqlite3_free(zSql);
7160    }
7161  }
7162  sqlite3_free(zExists);
7163  return rc;
7164}
7165
7166/*
7167** Implementation of ".ar" dot command.
7168*/
7169static int arDotCommand(
7170  ShellState *pState,          /* Current shell tool state */
7171  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
7172  char **azArg,                /* Array of arguments passed to dot command */
7173  int nArg                     /* Number of entries in azArg[] */
7174){
7175  ArCommand cmd;
7176  int rc;
7177  memset(&cmd, 0, sizeof(cmd));
7178  cmd.fromCmdLine = fromCmdLine;
7179  rc = arParseCommand(azArg, nArg, &cmd);
7180  if( rc==SQLITE_OK ){
7181    int eDbType = SHELL_OPEN_UNSPEC;
7182    cmd.p = pState;
7183    cmd.db = pState->db;
7184    if( cmd.zFile ){
7185      eDbType = deduceDatabaseType(cmd.zFile, 1);
7186    }else{
7187      eDbType = pState->openMode;
7188    }
7189    if( eDbType==SHELL_OPEN_ZIPFILE ){
7190      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7191        if( cmd.zFile==0 ){
7192          cmd.zSrcTable = sqlite3_mprintf("zip");
7193        }else{
7194          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7195        }
7196      }
7197      cmd.bZip = 1;
7198    }else if( cmd.zFile ){
7199      int flags;
7200      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7201      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7202           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7203        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7204      }else{
7205        flags = SQLITE_OPEN_READONLY;
7206      }
7207      cmd.db = 0;
7208      if( cmd.bDryRun ){
7209        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7210             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7211      }
7212      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7213             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7214      if( rc!=SQLITE_OK ){
7215        utf8_printf(stderr, "cannot open file: %s (%s)\n",
7216            cmd.zFile, sqlite3_errmsg(cmd.db)
7217        );
7218        goto end_ar_command;
7219      }
7220      sqlite3_fileio_init(cmd.db, 0, 0);
7221      sqlite3_sqlar_init(cmd.db, 0, 0);
7222      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7223                              shellPutsFunc, 0, 0);
7224
7225    }
7226    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7227      if( cmd.eCmd!=AR_CMD_CREATE
7228       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7229      ){
7230        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7231        rc = SQLITE_ERROR;
7232        goto end_ar_command;
7233      }
7234      cmd.zSrcTable = sqlite3_mprintf("sqlar");
7235    }
7236
7237    switch( cmd.eCmd ){
7238      case AR_CMD_CREATE:
7239        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7240        break;
7241
7242      case AR_CMD_EXTRACT:
7243        rc = arExtractCommand(&cmd);
7244        break;
7245
7246      case AR_CMD_LIST:
7247        rc = arListCommand(&cmd);
7248        break;
7249
7250      case AR_CMD_HELP:
7251        arUsage(pState->out);
7252        break;
7253
7254      case AR_CMD_INSERT:
7255        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7256        break;
7257
7258      case AR_CMD_REMOVE:
7259        rc = arRemoveCommand(&cmd);
7260        break;
7261
7262      default:
7263        assert( cmd.eCmd==AR_CMD_UPDATE );
7264        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7265        break;
7266    }
7267  }
7268end_ar_command:
7269  if( cmd.db!=pState->db ){
7270    close_db(cmd.db);
7271  }
7272  sqlite3_free(cmd.zSrcTable);
7273
7274  return rc;
7275}
7276/* End of the ".archive" or ".ar" command logic
7277*******************************************************************************/
7278#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7279
7280#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7281/*
7282** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
7283** Otherwise, the SQL statement or statements in zSql are executed using
7284** database connection db and the error code written to *pRc before
7285** this function returns.
7286*/
7287static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
7288  int rc = *pRc;
7289  if( rc==SQLITE_OK ){
7290    char *zErr = 0;
7291    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
7292    if( rc!=SQLITE_OK ){
7293      raw_printf(stderr, "SQL error: %s\n", zErr);
7294    }
7295    sqlite3_free(zErr);
7296    *pRc = rc;
7297  }
7298}
7299
7300/*
7301** Like shellExec(), except that zFmt is a printf() style format string.
7302*/
7303static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
7304  char *z = 0;
7305  if( *pRc==SQLITE_OK ){
7306    va_list ap;
7307    va_start(ap, zFmt);
7308    z = sqlite3_vmprintf(zFmt, ap);
7309    va_end(ap);
7310    if( z==0 ){
7311      *pRc = SQLITE_NOMEM;
7312    }else{
7313      shellExec(db, pRc, z);
7314    }
7315    sqlite3_free(z);
7316  }
7317}
7318
7319/*
7320** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7321** Otherwise, an attempt is made to allocate, zero and return a pointer
7322** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
7323** to SQLITE_NOMEM and NULL returned.
7324*/
7325static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
7326  void *pRet = 0;
7327  if( *pRc==SQLITE_OK ){
7328    pRet = sqlite3_malloc64(nByte);
7329    if( pRet==0 ){
7330      *pRc = SQLITE_NOMEM;
7331    }else{
7332      memset(pRet, 0, nByte);
7333    }
7334  }
7335  return pRet;
7336}
7337
7338/*
7339** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7340** Otherwise, zFmt is treated as a printf() style string. The result of
7341** formatting it along with any trailing arguments is written into a
7342** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
7343** It is the responsibility of the caller to eventually free this buffer
7344** using a call to sqlite3_free().
7345**
7346** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
7347** pointer returned.
7348*/
7349static char *shellMPrintf(int *pRc, const char *zFmt, ...){
7350  char *z = 0;
7351  if( *pRc==SQLITE_OK ){
7352    va_list ap;
7353    va_start(ap, zFmt);
7354    z = sqlite3_vmprintf(zFmt, ap);
7355    va_end(ap);
7356    if( z==0 ){
7357      *pRc = SQLITE_NOMEM;
7358    }
7359  }
7360  return z;
7361}
7362
7363
7364/*
7365** When running the ".recover" command, each output table, and the special
7366** orphaned row table if it is required, is represented by an instance
7367** of the following struct.
7368*/
7369typedef struct RecoverTable RecoverTable;
7370struct RecoverTable {
7371  char *zQuoted;                  /* Quoted version of table name */
7372  int nCol;                       /* Number of columns in table */
7373  char **azlCol;                  /* Array of column lists */
7374  int iPk;                        /* Index of IPK column */
7375};
7376
7377/*
7378** Free a RecoverTable object allocated by recoverFindTable() or
7379** recoverOrphanTable().
7380*/
7381static void recoverFreeTable(RecoverTable *pTab){
7382  if( pTab ){
7383    sqlite3_free(pTab->zQuoted);
7384    if( pTab->azlCol ){
7385      int i;
7386      for(i=0; i<=pTab->nCol; i++){
7387        sqlite3_free(pTab->azlCol[i]);
7388      }
7389      sqlite3_free(pTab->azlCol);
7390    }
7391    sqlite3_free(pTab);
7392  }
7393}
7394
7395/*
7396** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
7397** Otherwise, it allocates and returns a RecoverTable object based on the
7398** final four arguments passed to this function. It is the responsibility
7399** of the caller to eventually free the returned object using
7400** recoverFreeTable().
7401*/
7402static RecoverTable *recoverNewTable(
7403  int *pRc,                       /* IN/OUT: Error code */
7404  const char *zName,              /* Name of table */
7405  const char *zSql,               /* CREATE TABLE statement */
7406  int bIntkey,
7407  int nCol
7408){
7409  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
7410  int rc = *pRc;
7411  RecoverTable *pTab = 0;
7412
7413  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
7414  if( rc==SQLITE_OK ){
7415    int nSqlCol = 0;
7416    int bSqlIntkey = 0;
7417    sqlite3_stmt *pStmt = 0;
7418
7419    rc = sqlite3_open("", &dbtmp);
7420    if( rc==SQLITE_OK ){
7421      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
7422                              shellIdQuote, 0, 0);
7423    }
7424    if( rc==SQLITE_OK ){
7425      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
7426    }
7427    if( rc==SQLITE_OK ){
7428      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
7429      if( rc==SQLITE_ERROR ){
7430        rc = SQLITE_OK;
7431        goto finished;
7432      }
7433    }
7434    shellPreparePrintf(dbtmp, &rc, &pStmt,
7435        "SELECT count(*) FROM pragma_table_info(%Q)", zName
7436    );
7437    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7438      nSqlCol = sqlite3_column_int(pStmt, 0);
7439    }
7440    shellFinalize(&rc, pStmt);
7441
7442    if( rc!=SQLITE_OK || nSqlCol<nCol ){
7443      goto finished;
7444    }
7445
7446    shellPreparePrintf(dbtmp, &rc, &pStmt,
7447      "SELECT ("
7448      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
7449      ") FROM sqlite_schema WHERE name = %Q", zName
7450    );
7451    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7452      bSqlIntkey = sqlite3_column_int(pStmt, 0);
7453    }
7454    shellFinalize(&rc, pStmt);
7455
7456    if( bIntkey==bSqlIntkey ){
7457      int i;
7458      const char *zPk = "_rowid_";
7459      sqlite3_stmt *pPkFinder = 0;
7460
7461      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
7462      ** set zPk to the name of the PK column, and pTab->iPk to the index
7463      ** of the column, where columns are 0-numbered from left to right.
7464      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7465      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7466      pTab->iPk = -2;
7467      if( bIntkey ){
7468        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7469          "SELECT cid, name FROM pragma_table_info(%Q) "
7470          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7471          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7472          , zName, zName
7473        );
7474        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7475          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7476          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7477          if( zPk==0 ){ zPk = "_";  /* Defensive.  Should never happen */ }
7478        }
7479      }
7480
7481      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7482      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7483      pTab->nCol = nSqlCol;
7484
7485      if( bIntkey ){
7486        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7487      }else{
7488        pTab->azlCol[0] = shellMPrintf(&rc, "");
7489      }
7490      i = 1;
7491      shellPreparePrintf(dbtmp, &rc, &pStmt,
7492          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7493          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7494          "FROM pragma_table_info(%Q)",
7495          bIntkey ? ", " : "", pTab->iPk,
7496          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7497          zName
7498      );
7499      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7500        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7501        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7502        i++;
7503      }
7504      shellFinalize(&rc, pStmt);
7505
7506      shellFinalize(&rc, pPkFinder);
7507    }
7508  }
7509
7510 finished:
7511  sqlite3_close(dbtmp);
7512  *pRc = rc;
7513  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7514    recoverFreeTable(pTab);
7515    pTab = 0;
7516  }
7517  return pTab;
7518}
7519
7520/*
7521** This function is called to search the schema recovered from the
7522** sqlite_schema table of the (possibly) corrupt database as part
7523** of a ".recover" command. Specifically, for a table with root page
7524** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7525** table must be a WITHOUT ROWID table, or if non-zero, not one of
7526** those.
7527**
7528** If a table is found, a (RecoverTable*) object is returned. Or, if
7529** no such table is found, but bIntkey is false and iRoot is the
7530** root page of an index in the recovered schema, then (*pbNoop) is
7531** set to true and NULL returned. Or, if there is no such table or
7532** index, NULL is returned and (*pbNoop) set to 0, indicating that
7533** the caller should write data to the orphans table.
7534*/
7535static RecoverTable *recoverFindTable(
7536  ShellState *pState,             /* Shell state object */
7537  int *pRc,                       /* IN/OUT: Error code */
7538  int iRoot,                      /* Root page of table */
7539  int bIntkey,                    /* True for an intkey table */
7540  int nCol,                       /* Number of columns in table */
7541  int *pbNoop                     /* OUT: True if iRoot is root of index */
7542){
7543  sqlite3_stmt *pStmt = 0;
7544  RecoverTable *pRet = 0;
7545  int bNoop = 0;
7546  const char *zSql = 0;
7547  const char *zName = 0;
7548
7549  /* Search the recovered schema for an object with root page iRoot. */
7550  shellPreparePrintf(pState->db, pRc, &pStmt,
7551      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7552  );
7553  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7554    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7555    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7556      bNoop = 1;
7557      break;
7558    }
7559    if( sqlite3_stricmp(zType, "table")==0 ){
7560      zName = (const char*)sqlite3_column_text(pStmt, 1);
7561      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7562      if( zName!=0 && zSql!=0 ){
7563        pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7564        break;
7565      }
7566    }
7567  }
7568
7569  shellFinalize(pRc, pStmt);
7570  *pbNoop = bNoop;
7571  return pRet;
7572}
7573
7574/*
7575** Return a RecoverTable object representing the orphans table.
7576*/
7577static RecoverTable *recoverOrphanTable(
7578  ShellState *pState,             /* Shell state object */
7579  int *pRc,                       /* IN/OUT: Error code */
7580  const char *zLostAndFound,      /* Base name for orphans table */
7581  int nCol                        /* Number of user data columns */
7582){
7583  RecoverTable *pTab = 0;
7584  if( nCol>=0 && *pRc==SQLITE_OK ){
7585    int i;
7586
7587    /* This block determines the name of the orphan table. The prefered
7588    ** name is zLostAndFound. But if that clashes with another name
7589    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7590    ** and so on until a non-clashing name is found.  */
7591    int iTab = 0;
7592    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7593    sqlite3_stmt *pTest = 0;
7594    shellPrepare(pState->db, pRc,
7595        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7596    );
7597    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7598    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7599      shellReset(pRc, pTest);
7600      sqlite3_free(zTab);
7601      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7602      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7603    }
7604    shellFinalize(pRc, pTest);
7605
7606    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7607    if( pTab ){
7608      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7609      pTab->nCol = nCol;
7610      pTab->iPk = -2;
7611      if( nCol>0 ){
7612        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7613        if( pTab->azlCol ){
7614          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7615          for(i=nCol-1; i>=0; i--){
7616            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7617          }
7618        }
7619      }
7620
7621      if( *pRc!=SQLITE_OK ){
7622        recoverFreeTable(pTab);
7623        pTab = 0;
7624      }else{
7625        raw_printf(pState->out,
7626            "CREATE TABLE %s(rootpgno INTEGER, "
7627            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7628        );
7629        for(i=0; i<nCol; i++){
7630          raw_printf(pState->out, ", c%d", i);
7631        }
7632        raw_printf(pState->out, ");\n");
7633      }
7634    }
7635    sqlite3_free(zTab);
7636  }
7637  return pTab;
7638}
7639
7640/*
7641** This function is called to recover data from the database. A script
7642** to construct a new database containing all recovered data is output
7643** on stream pState->out.
7644*/
7645static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7646  int rc = SQLITE_OK;
7647  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7648  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7649  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7650  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7651  const char *zLostAndFound = "lost_and_found";
7652  int i;
7653  int nOrphan = -1;
7654  RecoverTable *pOrphan = 0;
7655
7656  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7657  int bRowids = 1;                /* 0 if --no-rowids */
7658  for(i=1; i<nArg; i++){
7659    char *z = azArg[i];
7660    int n;
7661    if( z[0]=='-' && z[1]=='-' ) z++;
7662    n = strlen30(z);
7663    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7664      bFreelist = 0;
7665    }else
7666    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7667      i++;
7668      zRecoveryDb = azArg[i];
7669    }else
7670    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7671      i++;
7672      zLostAndFound = azArg[i];
7673    }else
7674    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7675      bRowids = 0;
7676    }
7677    else{
7678      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7679      showHelp(pState->out, azArg[0]);
7680      return 1;
7681    }
7682  }
7683
7684  shellExecPrintf(pState->db, &rc,
7685    /* Attach an in-memory database named 'recovery'. Create an indexed
7686    ** cache of the sqlite_dbptr virtual table. */
7687    "PRAGMA writable_schema = on;"
7688    "ATTACH %Q AS recovery;"
7689    "DROP TABLE IF EXISTS recovery.dbptr;"
7690    "DROP TABLE IF EXISTS recovery.freelist;"
7691    "DROP TABLE IF EXISTS recovery.map;"
7692    "DROP TABLE IF EXISTS recovery.schema;"
7693    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7694  );
7695
7696  if( bFreelist ){
7697    shellExec(pState->db, &rc,
7698      "WITH trunk(pgno) AS ("
7699      "  SELECT shell_int32("
7700      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7701      "      WHERE x>0"
7702      "    UNION"
7703      "  SELECT shell_int32("
7704      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7705      "      FROM trunk WHERE x>0"
7706      "),"
7707      "freelist(data, n, freepgno) AS ("
7708      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7709      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7710      "    UNION ALL"
7711      "  SELECT data, n-1, shell_int32(data, 2+n) "
7712      "      FROM freelist WHERE n>=0"
7713      ")"
7714      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7715    );
7716  }
7717
7718  /* If this is an auto-vacuum database, add all pointer-map pages to
7719  ** the freelist table. Do this regardless of whether or not
7720  ** --freelist-corrupt was specified.  */
7721  shellExec(pState->db, &rc,
7722    "WITH ptrmap(pgno) AS ("
7723    "  SELECT 2 WHERE shell_int32("
7724    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7725    "  )"
7726    "    UNION ALL "
7727    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7728    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7729    ")"
7730    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7731  );
7732
7733  shellExec(pState->db, &rc,
7734    "CREATE TABLE recovery.dbptr("
7735    "      pgno, child, PRIMARY KEY(child, pgno)"
7736    ") WITHOUT ROWID;"
7737    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7738    "    SELECT * FROM sqlite_dbptr"
7739    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7740
7741    /* Delete any pointer to page 1. This ensures that page 1 is considered
7742    ** a root page, regardless of how corrupt the db is. */
7743    "DELETE FROM recovery.dbptr WHERE child = 1;"
7744
7745    /* Delete all pointers to any pages that have more than one pointer
7746    ** to them. Such pages will be treated as root pages when recovering
7747    ** data.  */
7748    "DELETE FROM recovery.dbptr WHERE child IN ("
7749    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7750    ");"
7751
7752    /* Create the "map" table that will (eventually) contain instructions
7753    ** for dealing with each page in the db that contains one or more
7754    ** records. */
7755    "CREATE TABLE recovery.map("
7756      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7757    ");"
7758
7759    /* Populate table [map]. If there are circular loops of pages in the
7760    ** database, the following adds all pages in such a loop to the map
7761    ** as individual root pages. This could be handled better.  */
7762    "WITH pages(i, maxlen) AS ("
7763    "  SELECT page_count, ("
7764    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7765    "  ) FROM pragma_page_count WHERE page_count>0"
7766    "    UNION ALL"
7767    "  SELECT i-1, ("
7768    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7769    "  ) FROM pages WHERE i>=2"
7770    ")"
7771    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7772    "  SELECT i, maxlen, NULL, ("
7773    "    WITH p(orig, pgno, parent) AS ("
7774    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7775    "        UNION "
7776    "      SELECT i, p.parent, "
7777    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7778    "    )"
7779    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7780    ") "
7781    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7782    "UPDATE recovery.map AS o SET intkey = ("
7783    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7784    ");"
7785
7786    /* Extract data from page 1 and any linked pages into table
7787    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7788    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7789    "INSERT INTO recovery.schema SELECT "
7790    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7791    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7792    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7793    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7794    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7795    "FROM sqlite_dbdata WHERE pgno IN ("
7796    "  SELECT pgno FROM recovery.map WHERE root=1"
7797    ")"
7798    "GROUP BY pgno, cell;"
7799    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7800  );
7801
7802  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7803  ** CREATE TABLE statements that extracted from the existing schema.  */
7804  if( rc==SQLITE_OK ){
7805    sqlite3_stmt *pStmt = 0;
7806    /* ".recover" might output content in an order which causes immediate
7807    ** foreign key constraints to be violated. So disable foreign-key
7808    ** constraint enforcement to prevent problems when running the output
7809    ** script. */
7810    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7811    raw_printf(pState->out, "BEGIN;\n");
7812    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7813    shellPrepare(pState->db, &rc,
7814        "SELECT sql FROM recovery.schema "
7815        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7816    );
7817    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7818      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7819      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7820          &zCreateTable[12]
7821      );
7822    }
7823    shellFinalize(&rc, pStmt);
7824  }
7825
7826  /* Figure out if an orphan table will be required. And if so, how many
7827  ** user columns it should contain */
7828  shellPrepare(pState->db, &rc,
7829      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7830      , &pLoop
7831  );
7832  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7833    nOrphan = sqlite3_column_int(pLoop, 0);
7834  }
7835  shellFinalize(&rc, pLoop);
7836  pLoop = 0;
7837
7838  shellPrepare(pState->db, &rc,
7839      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7840  );
7841
7842  shellPrepare(pState->db, &rc,
7843      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7844      "(case when (? AND field<0) then NULL else value end)"
7845      "), ', ')"
7846      ", min(field) "
7847      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7848      "GROUP BY cell", &pCells
7849  );
7850
7851  /* Loop through each root page. */
7852  shellPrepare(pState->db, &rc,
7853      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7854      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7855      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7856      ")", &pLoop
7857  );
7858  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7859    int iRoot = sqlite3_column_int(pLoop, 0);
7860    int bIntkey = sqlite3_column_int(pLoop, 1);
7861    int nCol = sqlite3_column_int(pLoop, 2);
7862    int bNoop = 0;
7863    RecoverTable *pTab;
7864
7865    assert( bIntkey==0 || bIntkey==1 );
7866    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7867    if( bNoop || rc ) continue;
7868    if( pTab==0 ){
7869      if( pOrphan==0 ){
7870        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7871      }
7872      pTab = pOrphan;
7873      if( pTab==0 ) break;
7874    }
7875
7876    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7877      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7878    }
7879    sqlite3_bind_int(pPages, 1, iRoot);
7880    if( bRowids==0 && pTab->iPk<0 ){
7881      sqlite3_bind_int(pCells, 1, 1);
7882    }else{
7883      sqlite3_bind_int(pCells, 1, 0);
7884    }
7885    sqlite3_bind_int(pCells, 3, pTab->iPk);
7886
7887    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7888      int iPgno = sqlite3_column_int(pPages, 0);
7889      sqlite3_bind_int(pCells, 2, iPgno);
7890      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7891        int nField = sqlite3_column_int(pCells, 0);
7892        int iMin = sqlite3_column_int(pCells, 2);
7893        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7894
7895        RecoverTable *pTab2 = pTab;
7896        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7897          if( pOrphan==0 ){
7898            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7899          }
7900          pTab2 = pOrphan;
7901          if( pTab2==0 ) break;
7902        }
7903
7904        nField = nField+1;
7905        if( pTab2==pOrphan ){
7906          raw_printf(pState->out,
7907              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7908              pTab2->zQuoted, iRoot, iPgno, nField,
7909              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7910          );
7911        }else{
7912          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7913              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7914          );
7915        }
7916      }
7917      shellReset(&rc, pCells);
7918    }
7919    shellReset(&rc, pPages);
7920    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7921  }
7922  shellFinalize(&rc, pLoop);
7923  shellFinalize(&rc, pPages);
7924  shellFinalize(&rc, pCells);
7925  recoverFreeTable(pOrphan);
7926
7927  /* The rest of the schema */
7928  if( rc==SQLITE_OK ){
7929    sqlite3_stmt *pStmt = 0;
7930    shellPrepare(pState->db, &rc,
7931        "SELECT sql, name FROM recovery.schema "
7932        "WHERE sql NOT LIKE 'create table%'", &pStmt
7933    );
7934    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7935      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7936      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7937        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7938        char *zPrint = shellMPrintf(&rc,
7939          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7940          zName, zName, zSql
7941        );
7942        raw_printf(pState->out, "%s;\n", zPrint);
7943        sqlite3_free(zPrint);
7944      }else{
7945        raw_printf(pState->out, "%s;\n", zSql);
7946      }
7947    }
7948    shellFinalize(&rc, pStmt);
7949  }
7950
7951  if( rc==SQLITE_OK ){
7952    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7953    raw_printf(pState->out, "COMMIT;\n");
7954  }
7955  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7956  return rc;
7957}
7958#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7959
7960
7961/*
7962 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7963 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7964 *   close db and set it to 0, and return the columns spec, to later
7965 *   be sqlite3_free()'ed by the caller.
7966 * The return is 0 when either:
7967 *   (a) The db was not initialized and zCol==0 (There are no columns.)
7968 *   (b) zCol!=0  (Column was added, db initialized as needed.)
7969 * The 3rd argument, pRenamed, references an out parameter. If the
7970 * pointer is non-zero, its referent will be set to a summary of renames
7971 * done if renaming was necessary, or set to 0 if none was done. The out
7972 * string (if any) must be sqlite3_free()'ed by the caller.
7973 */
7974#ifdef SHELL_DEBUG
7975#define rc_err_oom_die(rc) \
7976  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7977  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7978    fprintf(stderr,"E:%d\n",rc), assert(0)
7979#else
7980static void rc_err_oom_die(int rc){
7981  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7982  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7983}
7984#endif
7985
7986#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7987static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7988#else  /* Otherwise, memory is faster/better for the transient DB. */
7989static const char *zCOL_DB = ":memory:";
7990#endif
7991
7992/* Define character (as C string) to separate generated column ordinal
7993 * from protected part of incoming column names. This defaults to "_"
7994 * so that incoming column identifiers that did not need not be quoted
7995 * remain usable without being quoted. It must be one character.
7996 */
7997#ifndef SHELL_AUTOCOLUMN_SEP
7998# define AUTOCOLUMN_SEP "_"
7999#else
8000# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
8001#endif
8002
8003static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
8004  /* Queries and D{D,M}L used here */
8005  static const char * const zTabMake = "\
8006CREATE TABLE ColNames(\
8007 cpos INTEGER PRIMARY KEY,\
8008 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
8009CREATE VIEW RepeatedNames AS \
8010SELECT DISTINCT t.name FROM ColNames t \
8011WHERE t.name COLLATE NOCASE IN (\
8012 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
8013);\
8014";
8015  static const char * const zTabFill = "\
8016INSERT INTO ColNames(name,nlen,chop,reps,suff)\
8017 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
8018";
8019  static const char * const zHasDupes = "\
8020SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
8021 <count(name) FROM ColNames\
8022";
8023#ifdef SHELL_COLUMN_RENAME_CLEAN
8024  static const char * const zDedoctor = "\
8025UPDATE ColNames SET chop=iif(\
8026  (substring(name,nlen,1) BETWEEN '0' AND '9')\
8027  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
8028 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
8029 0\
8030)\
8031";
8032#endif
8033  static const char * const zSetReps = "\
8034UPDATE ColNames AS t SET reps=\
8035(SELECT count(*) FROM ColNames d \
8036 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
8037 COLLATE NOCASE\
8038)\
8039";
8040#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8041  static const char * const zColDigits = "\
8042SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8043";
8044#else
8045  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8046  static const char * const zColDigits = "\
8047SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8048 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8049 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8050";
8051#endif
8052  static const char * const zRenameRank =
8053#ifdef SHELL_COLUMN_RENAME_CLEAN
8054    "UPDATE ColNames AS t SET suff="
8055    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
8056#else /* ...RENAME_MINIMAL_ONE_PASS */
8057"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8058"  SELECT 0 AS nlz"
8059"  UNION"
8060"  SELECT nlz+1 AS nlz FROM Lzn"
8061"  WHERE EXISTS("
8062"   SELECT 1"
8063"   FROM ColNames t, ColNames o"
8064"   WHERE"
8065"    iif(t.name IN (SELECT * FROM RepeatedNames),"
8066"     printf('%s"AUTOCOLUMN_SEP"%s',"
8067"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8068"     t.name"
8069"    )"
8070"    ="
8071"    iif(o.name IN (SELECT * FROM RepeatedNames),"
8072"     printf('%s"AUTOCOLUMN_SEP"%s',"
8073"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8074"     o.name"
8075"    )"
8076"    COLLATE NOCASE"
8077"    AND o.cpos<>t.cpos"
8078"   GROUP BY t.cpos"
8079"  )"
8080") UPDATE Colnames AS t SET"
8081" chop = 0," /* No chopping, never touch incoming names. */
8082" suff = iif(name IN (SELECT * FROM RepeatedNames),"
8083"  printf('"AUTOCOLUMN_SEP"%s', substring("
8084"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8085"  ''"
8086" )"
8087#endif
8088    ;
8089  static const char * const zCollectVar = "\
8090SELECT\
8091 '('||x'0a'\
8092 || group_concat(\
8093  cname||' TEXT',\
8094  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8095 ||')' AS ColsSpec \
8096FROM (\
8097 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8098 FROM ColNames ORDER BY cpos\
8099)";
8100  static const char * const zRenamesDone =
8101    "SELECT group_concat("
8102    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8103    " ','||x'0a')"
8104    "FROM ColNames WHERE suff<>'' OR chop!=0"
8105    ;
8106  int rc;
8107  sqlite3_stmt *pStmt = 0;
8108  assert(pDb!=0);
8109  if( zColNew ){
8110    /* Add initial or additional column. Init db if necessary. */
8111    if( *pDb==0 ){
8112      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
8113#ifdef SHELL_COLFIX_DB
8114      if(*zCOL_DB!=':')
8115        sqlite3_exec(*pDb,"drop table if exists ColNames;"
8116                     "drop view if exists RepeatedNames;",0,0,0);
8117#endif
8118      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
8119      rc_err_oom_die(rc);
8120    }
8121    assert(*pDb!=0);
8122    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
8123    rc_err_oom_die(rc);
8124    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
8125    rc_err_oom_die(rc);
8126    rc = sqlite3_step(pStmt);
8127    rc_err_oom_die(rc);
8128    sqlite3_finalize(pStmt);
8129    return 0;
8130  }else if( *pDb==0 ){
8131    return 0;
8132  }else{
8133    /* Formulate the columns spec, close the DB, zero *pDb. */
8134    char *zColsSpec = 0;
8135    int hasDupes = db_int(*pDb, zHasDupes);
8136    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
8137    if( hasDupes ){
8138#ifdef SHELL_COLUMN_RENAME_CLEAN
8139      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
8140      rc_err_oom_die(rc);
8141#endif
8142      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
8143      rc_err_oom_die(rc);
8144      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
8145      rc_err_oom_die(rc);
8146      sqlite3_bind_int(pStmt, 1, nDigits);
8147      rc = sqlite3_step(pStmt);
8148      sqlite3_finalize(pStmt);
8149      assert(rc==SQLITE_DONE);
8150    }
8151    assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
8152    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
8153    rc_err_oom_die(rc);
8154    rc = sqlite3_step(pStmt);
8155    if( rc==SQLITE_ROW ){
8156      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8157    }else{
8158      zColsSpec = 0;
8159    }
8160    if( pzRenamed!=0 ){
8161      if( !hasDupes ) *pzRenamed = 0;
8162      else{
8163        sqlite3_finalize(pStmt);
8164        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
8165            && SQLITE_ROW==sqlite3_step(pStmt) ){
8166          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8167        }else
8168          *pzRenamed = 0;
8169      }
8170    }
8171    sqlite3_finalize(pStmt);
8172    sqlite3_close(*pDb);
8173    *pDb = 0;
8174    return zColsSpec;
8175  }
8176}
8177
8178/*
8179** If an input line begins with "." then invoke this routine to
8180** process that line.
8181**
8182** Return 1 on error, 2 to exit, and 0 otherwise.
8183*/
8184static int do_meta_command(char *zLine, ShellState *p){
8185  int h = 1;
8186  int nArg = 0;
8187  int n, c;
8188  int rc = 0;
8189  char *azArg[52];
8190
8191#ifndef SQLITE_OMIT_VIRTUALTABLE
8192  if( p->expert.pExpert ){
8193    expertFinish(p, 1, 0);
8194  }
8195#endif
8196
8197  /* Parse the input line into tokens.
8198  */
8199  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
8200    while( IsSpace(zLine[h]) ){ h++; }
8201    if( zLine[h]==0 ) break;
8202    if( zLine[h]=='\'' || zLine[h]=='"' ){
8203      int delim = zLine[h++];
8204      azArg[nArg++] = &zLine[h];
8205      while( zLine[h] && zLine[h]!=delim ){
8206        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
8207        h++;
8208      }
8209      if( zLine[h]==delim ){
8210        zLine[h++] = 0;
8211      }
8212      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
8213    }else{
8214      azArg[nArg++] = &zLine[h];
8215      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
8216      if( zLine[h] ) zLine[h++] = 0;
8217      resolve_backslashes(azArg[nArg-1]);
8218    }
8219  }
8220  azArg[nArg] = 0;
8221
8222  /* Process the input line.
8223  */
8224  if( nArg==0 ) return 0; /* no tokens, no error */
8225  n = strlen30(azArg[0]);
8226  c = azArg[0][0];
8227  clearTempFile(p);
8228
8229#ifndef SQLITE_OMIT_AUTHORIZATION
8230  if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
8231    if( nArg!=2 ){
8232      raw_printf(stderr, "Usage: .auth ON|OFF\n");
8233      rc = 1;
8234      goto meta_command_exit;
8235    }
8236    open_db(p, 0);
8237    if( booleanValue(azArg[1]) ){
8238      sqlite3_set_authorizer(p->db, shellAuth, p);
8239    }else if( p->bSafeModePersist ){
8240      sqlite3_set_authorizer(p->db, safeModeAuth, p);
8241    }else{
8242      sqlite3_set_authorizer(p->db, 0, 0);
8243    }
8244  }else
8245#endif
8246
8247#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8248  && !defined(SQLITE_SHELL_FIDDLE)
8249  if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
8250    open_db(p, 0);
8251    failIfSafeMode(p, "cannot run .archive in safe mode");
8252    rc = arDotCommand(p, 0, azArg, nArg);
8253  }else
8254#endif
8255
8256#ifndef SQLITE_SHELL_FIDDLE
8257  if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
8258   || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
8259  ){
8260    const char *zDestFile = 0;
8261    const char *zDb = 0;
8262    sqlite3 *pDest;
8263    sqlite3_backup *pBackup;
8264    int j;
8265    int bAsync = 0;
8266    const char *zVfs = 0;
8267    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8268    for(j=1; j<nArg; j++){
8269      const char *z = azArg[j];
8270      if( z[0]=='-' ){
8271        if( z[1]=='-' ) z++;
8272        if( cli_strcmp(z, "-append")==0 ){
8273          zVfs = "apndvfs";
8274        }else
8275        if( cli_strcmp(z, "-async")==0 ){
8276          bAsync = 1;
8277        }else
8278        {
8279          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
8280          return 1;
8281        }
8282      }else if( zDestFile==0 ){
8283        zDestFile = azArg[j];
8284      }else if( zDb==0 ){
8285        zDb = zDestFile;
8286        zDestFile = azArg[j];
8287      }else{
8288        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8289        return 1;
8290      }
8291    }
8292    if( zDestFile==0 ){
8293      raw_printf(stderr, "missing FILENAME argument on .backup\n");
8294      return 1;
8295    }
8296    if( zDb==0 ) zDb = "main";
8297    rc = sqlite3_open_v2(zDestFile, &pDest,
8298                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8299    if( rc!=SQLITE_OK ){
8300      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
8301      close_db(pDest);
8302      return 1;
8303    }
8304    if( bAsync ){
8305      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8306                   0, 0, 0);
8307    }
8308    open_db(p, 0);
8309    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8310    if( pBackup==0 ){
8311      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8312      close_db(pDest);
8313      return 1;
8314    }
8315    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8316    sqlite3_backup_finish(pBackup);
8317    if( rc==SQLITE_DONE ){
8318      rc = 0;
8319    }else{
8320      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8321      rc = 1;
8322    }
8323    close_db(pDest);
8324  }else
8325#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8326
8327  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
8328    if( nArg==2 ){
8329      bail_on_error = booleanValue(azArg[1]);
8330    }else{
8331      raw_printf(stderr, "Usage: .bail on|off\n");
8332      rc = 1;
8333    }
8334  }else
8335
8336  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
8337    if( nArg==2 ){
8338      if( booleanValue(azArg[1]) ){
8339        setBinaryMode(p->out, 1);
8340      }else{
8341        setTextMode(p->out, 1);
8342      }
8343    }else{
8344      raw_printf(stderr, "Usage: .binary on|off\n");
8345      rc = 1;
8346    }
8347  }else
8348
8349  /* The undocumented ".breakpoint" command causes a call to the no-op
8350  ** routine named test_breakpoint().
8351  */
8352  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
8353    test_breakpoint();
8354  }else
8355
8356#ifndef SQLITE_SHELL_FIDDLE
8357  if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
8358    failIfSafeMode(p, "cannot run .cd in safe mode");
8359    if( nArg==2 ){
8360#if defined(_WIN32) || defined(WIN32)
8361      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8362      rc = !SetCurrentDirectoryW(z);
8363      sqlite3_free(z);
8364#else
8365      rc = chdir(azArg[1]);
8366#endif
8367      if( rc ){
8368        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
8369        rc = 1;
8370      }
8371    }else{
8372      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
8373      rc = 1;
8374    }
8375  }else
8376#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8377
8378  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
8379    if( nArg==2 ){
8380      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8381    }else{
8382      raw_printf(stderr, "Usage: .changes on|off\n");
8383      rc = 1;
8384    }
8385  }else
8386
8387#ifndef SQLITE_SHELL_FIDDLE
8388  /* Cancel output redirection, if it is currently set (by .testcase)
8389  ** Then read the content of the testcase-out.txt file and compare against
8390  ** azArg[1].  If there are differences, report an error and exit.
8391  */
8392  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
8393    char *zRes = 0;
8394    output_reset(p);
8395    if( nArg!=2 ){
8396      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
8397      rc = 2;
8398    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8399      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
8400      rc = 2;
8401    }else if( testcase_glob(azArg[1],zRes)==0 ){
8402      utf8_printf(stderr,
8403                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
8404                 p->zTestcase, azArg[1], zRes);
8405      rc = 1;
8406    }else{
8407      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
8408      p->nCheck++;
8409    }
8410    sqlite3_free(zRes);
8411  }else
8412#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8413
8414#ifndef SQLITE_SHELL_FIDDLE
8415  if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
8416    failIfSafeMode(p, "cannot run .clone in safe mode");
8417    if( nArg==2 ){
8418      tryToClone(p, azArg[1]);
8419    }else{
8420      raw_printf(stderr, "Usage: .clone FILENAME\n");
8421      rc = 1;
8422    }
8423  }else
8424#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8425
8426  if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
8427    if( nArg==1 ){
8428      /* List available connections */
8429      int i;
8430      for(i=0; i<ArraySize(p->aAuxDb); i++){
8431        const char *zFile = p->aAuxDb[i].zDbFilename;
8432        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8433          zFile = "(not open)";
8434        }else if( zFile==0 ){
8435          zFile = "(memory)";
8436        }else if( zFile[0]==0 ){
8437          zFile = "(temporary-file)";
8438        }
8439        if( p->pAuxDb == &p->aAuxDb[i] ){
8440          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
8441        }else if( p->aAuxDb[i].db!=0 ){
8442          utf8_printf(stdout, "       %d: %s\n", i, zFile);
8443        }
8444      }
8445    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8446      int i = azArg[1][0] - '0';
8447      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8448        p->pAuxDb->db = p->db;
8449        p->pAuxDb = &p->aAuxDb[i];
8450        globalDb = p->db = p->pAuxDb->db;
8451        p->pAuxDb->db = 0;
8452      }
8453    }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
8454           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8455      int i = azArg[2][0] - '0';
8456      if( i<0 || i>=ArraySize(p->aAuxDb) ){
8457        /* No-op */
8458      }else if( p->pAuxDb == &p->aAuxDb[i] ){
8459        raw_printf(stderr, "cannot close the active database connection\n");
8460        rc = 1;
8461      }else if( p->aAuxDb[i].db ){
8462        session_close_all(p, i);
8463        close_db(p->aAuxDb[i].db);
8464        p->aAuxDb[i].db = 0;
8465      }
8466    }else{
8467      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
8468      rc = 1;
8469    }
8470  }else
8471
8472  if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
8473    char **azName = 0;
8474    int nName = 0;
8475    sqlite3_stmt *pStmt;
8476    int i;
8477    open_db(p, 0);
8478    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8479    if( rc ){
8480      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8481      rc = 1;
8482    }else{
8483      while( sqlite3_step(pStmt)==SQLITE_ROW ){
8484        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8485        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8486        if( zSchema==0 || zFile==0 ) continue;
8487        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8488        shell_check_oom(azName);
8489        azName[nName*2] = strdup(zSchema);
8490        azName[nName*2+1] = strdup(zFile);
8491        nName++;
8492      }
8493    }
8494    sqlite3_finalize(pStmt);
8495    for(i=0; i<nName; i++){
8496      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8497      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8498      const char *z = azName[i*2+1];
8499      utf8_printf(p->out, "%s: %s %s%s\n",
8500         azName[i*2],
8501         z && z[0] ? z : "\"\"",
8502         bRdonly ? "r/o" : "r/w",
8503         eTxn==SQLITE_TXN_NONE ? "" :
8504            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8505      free(azName[i*2]);
8506      free(azName[i*2+1]);
8507    }
8508    sqlite3_free(azName);
8509  }else
8510
8511  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
8512    static const struct DbConfigChoices {
8513      const char *zName;
8514      int op;
8515    } aDbConfig[] = {
8516        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
8517        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
8518        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
8519        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
8520        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
8521        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
8522        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
8523        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8524        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
8525        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
8526        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8527        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
8528        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
8529        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
8530        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
8531        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
8532    };
8533    int ii, v;
8534    open_db(p, 0);
8535    for(ii=0; ii<ArraySize(aDbConfig); ii++){
8536      if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8537      if( nArg>=3 ){
8538        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8539      }
8540      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8541      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
8542      if( nArg>1 ) break;
8543    }
8544    if( nArg>1 && ii==ArraySize(aDbConfig) ){
8545      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
8546      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
8547    }
8548  }else
8549
8550#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8551  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
8552    rc = shell_dbinfo_command(p, nArg, azArg);
8553  }else
8554
8555  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
8556    open_db(p, 0);
8557    rc = recoverDatabaseCmd(p, nArg, azArg);
8558  }else
8559#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
8560
8561  if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
8562    char *zLike = 0;
8563    char *zSql;
8564    int i;
8565    int savedShowHeader = p->showHeader;
8566    int savedShellFlags = p->shellFlgs;
8567    ShellClearFlag(p,
8568       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8569       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8570    for(i=1; i<nArg; i++){
8571      if( azArg[i][0]=='-' ){
8572        const char *z = azArg[i]+1;
8573        if( z[0]=='-' ) z++;
8574        if( cli_strcmp(z,"preserve-rowids")==0 ){
8575#ifdef SQLITE_OMIT_VIRTUALTABLE
8576          raw_printf(stderr, "The --preserve-rowids option is not compatible"
8577                             " with SQLITE_OMIT_VIRTUALTABLE\n");
8578          rc = 1;
8579          sqlite3_free(zLike);
8580          goto meta_command_exit;
8581#else
8582          ShellSetFlag(p, SHFLG_PreserveRowid);
8583#endif
8584        }else
8585        if( cli_strcmp(z,"newlines")==0 ){
8586          ShellSetFlag(p, SHFLG_Newlines);
8587        }else
8588        if( cli_strcmp(z,"data-only")==0 ){
8589          ShellSetFlag(p, SHFLG_DumpDataOnly);
8590        }else
8591        if( cli_strcmp(z,"nosys")==0 ){
8592          ShellSetFlag(p, SHFLG_DumpNoSys);
8593        }else
8594        {
8595          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8596          rc = 1;
8597          sqlite3_free(zLike);
8598          goto meta_command_exit;
8599        }
8600      }else{
8601        /* azArg[i] contains a LIKE pattern. This ".dump" request should
8602        ** only dump data for tables for which either the table name matches
8603        ** the LIKE pattern, or the table appears to be a shadow table of
8604        ** a virtual table for which the name matches the LIKE pattern.
8605        */
8606        char *zExpr = sqlite3_mprintf(
8607            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8608            "  SELECT 1 FROM sqlite_schema WHERE "
8609            "    name LIKE %Q ESCAPE '\\' AND"
8610            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8611            "    substr(o.name, 1, length(name)+1) == (name||'_')"
8612            ")", azArg[i], azArg[i]
8613        );
8614
8615        if( zLike ){
8616          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8617        }else{
8618          zLike = zExpr;
8619        }
8620      }
8621    }
8622
8623    open_db(p, 0);
8624
8625    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8626      /* When playing back a "dump", the content might appear in an order
8627      ** which causes immediate foreign key constraints to be violated.
8628      ** So disable foreign-key constraint enforcement to prevent problems. */
8629      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8630      raw_printf(p->out, "BEGIN TRANSACTION;\n");
8631    }
8632    p->writableSchema = 0;
8633    p->showHeader = 0;
8634    /* Set writable_schema=ON since doing so forces SQLite to initialize
8635    ** as much of the schema as it can even if the sqlite_schema table is
8636    ** corrupt. */
8637    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8638    p->nErr = 0;
8639    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8640    zSql = sqlite3_mprintf(
8641      "SELECT name, type, sql FROM sqlite_schema AS o "
8642      "WHERE (%s) AND type=='table'"
8643      "  AND sql NOT NULL"
8644      " ORDER BY tbl_name='sqlite_sequence', rowid",
8645      zLike
8646    );
8647    run_schema_dump_query(p,zSql);
8648    sqlite3_free(zSql);
8649    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8650      zSql = sqlite3_mprintf(
8651        "SELECT sql FROM sqlite_schema AS o "
8652        "WHERE (%s) AND sql NOT NULL"
8653        "  AND type IN ('index','trigger','view')",
8654        zLike
8655      );
8656      run_table_dump_query(p, zSql);
8657      sqlite3_free(zSql);
8658    }
8659    sqlite3_free(zLike);
8660    if( p->writableSchema ){
8661      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8662      p->writableSchema = 0;
8663    }
8664    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8665    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8666    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8667      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8668    }
8669    p->showHeader = savedShowHeader;
8670    p->shellFlgs = savedShellFlags;
8671  }else
8672
8673  if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
8674    if( nArg==2 ){
8675      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8676    }else{
8677      raw_printf(stderr, "Usage: .echo on|off\n");
8678      rc = 1;
8679    }
8680  }else
8681
8682  if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
8683    if( nArg==2 ){
8684      p->autoEQPtest = 0;
8685      if( p->autoEQPtrace ){
8686        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8687        p->autoEQPtrace = 0;
8688      }
8689      if( cli_strcmp(azArg[1],"full")==0 ){
8690        p->autoEQP = AUTOEQP_full;
8691      }else if( cli_strcmp(azArg[1],"trigger")==0 ){
8692        p->autoEQP = AUTOEQP_trigger;
8693#ifdef SQLITE_DEBUG
8694      }else if( cli_strcmp(azArg[1],"test")==0 ){
8695        p->autoEQP = AUTOEQP_on;
8696        p->autoEQPtest = 1;
8697      }else if( cli_strcmp(azArg[1],"trace")==0 ){
8698        p->autoEQP = AUTOEQP_full;
8699        p->autoEQPtrace = 1;
8700        open_db(p, 0);
8701        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8702        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8703#endif
8704      }else{
8705        p->autoEQP = (u8)booleanValue(azArg[1]);
8706      }
8707    }else{
8708      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8709      rc = 1;
8710    }
8711  }else
8712
8713#ifndef SQLITE_SHELL_FIDDLE
8714  if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
8715    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8716    rc = 2;
8717  }else
8718#endif
8719
8720  /* The ".explain" command is automatic now.  It is largely pointless.  It
8721  ** retained purely for backwards compatibility */
8722  if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
8723    int val = 1;
8724    if( nArg>=2 ){
8725      if( cli_strcmp(azArg[1],"auto")==0 ){
8726        val = 99;
8727      }else{
8728        val =  booleanValue(azArg[1]);
8729      }
8730    }
8731    if( val==1 && p->mode!=MODE_Explain ){
8732      p->normalMode = p->mode;
8733      p->mode = MODE_Explain;
8734      p->autoExplain = 0;
8735    }else if( val==0 ){
8736      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8737      p->autoExplain = 0;
8738    }else if( val==99 ){
8739      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8740      p->autoExplain = 1;
8741    }
8742  }else
8743
8744#ifndef SQLITE_OMIT_VIRTUALTABLE
8745  if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
8746    if( p->bSafeMode ){
8747      raw_printf(stderr,
8748        "Cannot run experimental commands such as \"%s\" in safe mode\n",
8749        azArg[0]);
8750      rc = 1;
8751    }else{
8752      open_db(p, 0);
8753      expertDotCommand(p, azArg, nArg);
8754    }
8755  }else
8756#endif
8757
8758  if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
8759    static const struct {
8760       const char *zCtrlName;   /* Name of a test-control option */
8761       int ctrlCode;            /* Integer code for that option */
8762       const char *zUsage;      /* Usage notes */
8763    } aCtrl[] = {
8764      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8765      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8766      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8767      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8768      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8769   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8770      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8771      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8772      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8773      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8774   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8775    };
8776    int filectrl = -1;
8777    int iCtrl = -1;
8778    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8779    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8780    int n2, i;
8781    const char *zCmd = 0;
8782    const char *zSchema = 0;
8783
8784    open_db(p, 0);
8785    zCmd = nArg>=2 ? azArg[1] : "help";
8786
8787    if( zCmd[0]=='-'
8788     && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
8789     && nArg>=4
8790    ){
8791      zSchema = azArg[2];
8792      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8793      nArg -= 2;
8794      zCmd = azArg[1];
8795    }
8796
8797    /* The argument can optionally begin with "-" or "--" */
8798    if( zCmd[0]=='-' && zCmd[1] ){
8799      zCmd++;
8800      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8801    }
8802
8803    /* --help lists all file-controls */
8804    if( cli_strcmp(zCmd,"help")==0 ){
8805      utf8_printf(p->out, "Available file-controls:\n");
8806      for(i=0; i<ArraySize(aCtrl); i++){
8807        utf8_printf(p->out, "  .filectrl %s %s\n",
8808                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8809      }
8810      rc = 1;
8811      goto meta_command_exit;
8812    }
8813
8814    /* convert filectrl text option to value. allow any unique prefix
8815    ** of the option name, or a numerical value. */
8816    n2 = strlen30(zCmd);
8817    for(i=0; i<ArraySize(aCtrl); i++){
8818      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8819        if( filectrl<0 ){
8820          filectrl = aCtrl[i].ctrlCode;
8821          iCtrl = i;
8822        }else{
8823          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8824                              "Use \".filectrl --help\" for help\n", zCmd);
8825          rc = 1;
8826          goto meta_command_exit;
8827        }
8828      }
8829    }
8830    if( filectrl<0 ){
8831      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8832                         "Use \".filectrl --help\" for help\n", zCmd);
8833    }else{
8834      switch(filectrl){
8835        case SQLITE_FCNTL_SIZE_LIMIT: {
8836          if( nArg!=2 && nArg!=3 ) break;
8837          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8838          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8839          isOk = 1;
8840          break;
8841        }
8842        case SQLITE_FCNTL_LOCK_TIMEOUT:
8843        case SQLITE_FCNTL_CHUNK_SIZE: {
8844          int x;
8845          if( nArg!=3 ) break;
8846          x = (int)integerValue(azArg[2]);
8847          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8848          isOk = 2;
8849          break;
8850        }
8851        case SQLITE_FCNTL_PERSIST_WAL:
8852        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8853          int x;
8854          if( nArg!=2 && nArg!=3 ) break;
8855          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8856          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8857          iRes = x;
8858          isOk = 1;
8859          break;
8860        }
8861        case SQLITE_FCNTL_DATA_VERSION:
8862        case SQLITE_FCNTL_HAS_MOVED: {
8863          int x;
8864          if( nArg!=2 ) break;
8865          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8866          iRes = x;
8867          isOk = 1;
8868          break;
8869        }
8870        case SQLITE_FCNTL_TEMPFILENAME: {
8871          char *z = 0;
8872          if( nArg!=2 ) break;
8873          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8874          if( z ){
8875            utf8_printf(p->out, "%s\n", z);
8876            sqlite3_free(z);
8877          }
8878          isOk = 2;
8879          break;
8880        }
8881        case SQLITE_FCNTL_RESERVE_BYTES: {
8882          int x;
8883          if( nArg>=3 ){
8884            x = atoi(azArg[2]);
8885            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8886          }
8887          x = -1;
8888          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8889          utf8_printf(p->out,"%d\n", x);
8890          isOk = 2;
8891          break;
8892        }
8893      }
8894    }
8895    if( isOk==0 && iCtrl>=0 ){
8896      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8897      rc = 1;
8898    }else if( isOk==1 ){
8899      char zBuf[100];
8900      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8901      raw_printf(p->out, "%s\n", zBuf);
8902    }
8903  }else
8904
8905  if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
8906    ShellState data;
8907    int doStats = 0;
8908    memcpy(&data, p, sizeof(data));
8909    data.showHeader = 0;
8910    data.cMode = data.mode = MODE_Semi;
8911    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8912      data.cMode = data.mode = MODE_Pretty;
8913      nArg = 1;
8914    }
8915    if( nArg!=1 ){
8916      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8917      rc = 1;
8918      goto meta_command_exit;
8919    }
8920    open_db(p, 0);
8921    rc = sqlite3_exec(p->db,
8922       "SELECT sql FROM"
8923       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8924       "     FROM sqlite_schema UNION ALL"
8925       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8926       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8927       "ORDER BY x",
8928       callback, &data, 0
8929    );
8930    if( rc==SQLITE_OK ){
8931      sqlite3_stmt *pStmt;
8932      rc = sqlite3_prepare_v2(p->db,
8933               "SELECT rowid FROM sqlite_schema"
8934               " WHERE name GLOB 'sqlite_stat[134]'",
8935               -1, &pStmt, 0);
8936      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8937      sqlite3_finalize(pStmt);
8938    }
8939    if( doStats==0 ){
8940      raw_printf(p->out, "/* No STAT tables available */\n");
8941    }else{
8942      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8943      data.cMode = data.mode = MODE_Insert;
8944      data.zDestTable = "sqlite_stat1";
8945      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8946      data.zDestTable = "sqlite_stat4";
8947      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8948      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8949    }
8950  }else
8951
8952  if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
8953    if( nArg==2 ){
8954      p->showHeader = booleanValue(azArg[1]);
8955      p->shellFlgs |= SHFLG_HeaderSet;
8956    }else{
8957      raw_printf(stderr, "Usage: .headers on|off\n");
8958      rc = 1;
8959    }
8960  }else
8961
8962  if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
8963    if( nArg>=2 ){
8964      n = showHelp(p->out, azArg[1]);
8965      if( n==0 ){
8966        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8967      }
8968    }else{
8969      showHelp(p->out, 0);
8970    }
8971  }else
8972
8973#ifndef SQLITE_SHELL_FIDDLE
8974  if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
8975    char *zTable = 0;           /* Insert data into this table */
8976    char *zSchema = 0;          /* within this schema (may default to "main") */
8977    char *zFile = 0;            /* Name of file to extra content from */
8978    sqlite3_stmt *pStmt = NULL; /* A statement */
8979    int nCol;                   /* Number of columns in the table */
8980    int nByte;                  /* Number of bytes in an SQL string */
8981    int i, j;                   /* Loop counters */
8982    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8983    int nSep;                   /* Number of bytes in p->colSeparator[] */
8984    char *zSql;                 /* An SQL statement */
8985    char *zFullTabName;         /* Table name with schema if applicable */
8986    ImportCtx sCtx;             /* Reader context */
8987    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8988    int eVerbose = 0;           /* Larger for more console output */
8989    int nSkip = 0;              /* Initial lines to skip */
8990    int useOutputMode = 1;      /* Use output mode to determine separators */
8991    char *zCreate = 0;          /* CREATE TABLE statement text */
8992
8993    failIfSafeMode(p, "cannot run .import in safe mode");
8994    memset(&sCtx, 0, sizeof(sCtx));
8995    if( p->mode==MODE_Ascii ){
8996      xRead = ascii_read_one_field;
8997    }else{
8998      xRead = csv_read_one_field;
8999    }
9000    rc = 1;
9001    for(i=1; i<nArg; i++){
9002      char *z = azArg[i];
9003      if( z[0]=='-' && z[1]=='-' ) z++;
9004      if( z[0]!='-' ){
9005        if( zFile==0 ){
9006          zFile = z;
9007        }else if( zTable==0 ){
9008          zTable = z;
9009        }else{
9010          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
9011          showHelp(p->out, "import");
9012          goto meta_command_exit;
9013        }
9014      }else if( cli_strcmp(z,"-v")==0 ){
9015        eVerbose++;
9016      }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
9017        zSchema = azArg[++i];
9018      }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
9019        nSkip = integerValue(azArg[++i]);
9020      }else if( cli_strcmp(z,"-ascii")==0 ){
9021        sCtx.cColSep = SEP_Unit[0];
9022        sCtx.cRowSep = SEP_Record[0];
9023        xRead = ascii_read_one_field;
9024        useOutputMode = 0;
9025      }else if( cli_strcmp(z,"-csv")==0 ){
9026        sCtx.cColSep = ',';
9027        sCtx.cRowSep = '\n';
9028        xRead = csv_read_one_field;
9029        useOutputMode = 0;
9030      }else{
9031        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
9032        showHelp(p->out, "import");
9033        goto meta_command_exit;
9034      }
9035    }
9036    if( zTable==0 ){
9037      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
9038                  zFile==0 ? "FILE" : "TABLE");
9039      showHelp(p->out, "import");
9040      goto meta_command_exit;
9041    }
9042    seenInterrupt = 0;
9043    open_db(p, 0);
9044    if( useOutputMode ){
9045      /* If neither the --csv or --ascii options are specified, then set
9046      ** the column and row separator characters from the output mode. */
9047      nSep = strlen30(p->colSeparator);
9048      if( nSep==0 ){
9049        raw_printf(stderr,
9050                   "Error: non-null column separator required for import\n");
9051        goto meta_command_exit;
9052      }
9053      if( nSep>1 ){
9054        raw_printf(stderr,
9055              "Error: multi-character column separators not allowed"
9056              " for import\n");
9057        goto meta_command_exit;
9058      }
9059      nSep = strlen30(p->rowSeparator);
9060      if( nSep==0 ){
9061        raw_printf(stderr,
9062            "Error: non-null row separator required for import\n");
9063        goto meta_command_exit;
9064      }
9065      if( nSep==2 && p->mode==MODE_Csv
9066       && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
9067      ){
9068        /* When importing CSV (only), if the row separator is set to the
9069        ** default output row separator, change it to the default input
9070        ** row separator.  This avoids having to maintain different input
9071        ** and output row separators. */
9072        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9073        nSep = strlen30(p->rowSeparator);
9074      }
9075      if( nSep>1 ){
9076        raw_printf(stderr, "Error: multi-character row separators not allowed"
9077                           " for import\n");
9078        goto meta_command_exit;
9079      }
9080      sCtx.cColSep = p->colSeparator[0];
9081      sCtx.cRowSep = p->rowSeparator[0];
9082    }
9083    sCtx.zFile = zFile;
9084    sCtx.nLine = 1;
9085    if( sCtx.zFile[0]=='|' ){
9086#ifdef SQLITE_OMIT_POPEN
9087      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9088      goto meta_command_exit;
9089#else
9090      sCtx.in = popen(sCtx.zFile+1, "r");
9091      sCtx.zFile = "<pipe>";
9092      sCtx.xCloser = pclose;
9093#endif
9094    }else{
9095      sCtx.in = fopen(sCtx.zFile, "rb");
9096      sCtx.xCloser = fclose;
9097    }
9098    if( sCtx.in==0 ){
9099      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
9100      goto meta_command_exit;
9101    }
9102    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
9103      char zSep[2];
9104      zSep[1] = 0;
9105      zSep[0] = sCtx.cColSep;
9106      utf8_printf(p->out, "Column separator ");
9107      output_c_string(p->out, zSep);
9108      utf8_printf(p->out, ", row separator ");
9109      zSep[0] = sCtx.cRowSep;
9110      output_c_string(p->out, zSep);
9111      utf8_printf(p->out, "\n");
9112    }
9113    sCtx.z = sqlite3_malloc64(120);
9114    if( sCtx.z==0 ){
9115      import_cleanup(&sCtx);
9116      shell_out_of_memory();
9117    }
9118    /* Below, resources must be freed before exit. */
9119    while( (nSkip--)>0 ){
9120      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
9121    }
9122    if( zSchema!=0 ){
9123      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
9124    }else{
9125      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
9126    }
9127    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
9128    if( zSql==0 || zFullTabName==0 ){
9129      import_cleanup(&sCtx);
9130      shell_out_of_memory();
9131    }
9132    nByte = strlen30(zSql);
9133    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9134    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
9135    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
9136      sqlite3 *dbCols = 0;
9137      char *zRenames = 0;
9138      char *zColDefs;
9139      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
9140      while( xRead(&sCtx) ){
9141        zAutoColumn(sCtx.z, &dbCols, 0);
9142        if( sCtx.cTerm!=sCtx.cColSep ) break;
9143      }
9144      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
9145      if( zRenames!=0 ){
9146        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
9147                    "Columns renamed during .import %s due to duplicates:\n"
9148                    "%s\n", sCtx.zFile, zRenames);
9149        sqlite3_free(zRenames);
9150      }
9151      assert(dbCols==0);
9152      if( zColDefs==0 ){
9153        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
9154      import_fail:
9155        sqlite3_free(zCreate);
9156        sqlite3_free(zSql);
9157        sqlite3_free(zFullTabName);
9158        import_cleanup(&sCtx);
9159        rc = 1;
9160        goto meta_command_exit;
9161      }
9162      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
9163      if( eVerbose>=1 ){
9164        utf8_printf(p->out, "%s\n", zCreate);
9165      }
9166      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
9167      if( rc ){
9168        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
9169        goto import_fail;
9170      }
9171      sqlite3_free(zCreate);
9172      zCreate = 0;
9173      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9174    }
9175    if( rc ){
9176      if (pStmt) sqlite3_finalize(pStmt);
9177      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
9178      goto import_fail;
9179    }
9180    sqlite3_free(zSql);
9181    nCol = sqlite3_column_count(pStmt);
9182    sqlite3_finalize(pStmt);
9183    pStmt = 0;
9184    if( nCol==0 ) return 0; /* no columns, no error */
9185    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
9186    if( zSql==0 ){
9187      import_cleanup(&sCtx);
9188      shell_out_of_memory();
9189    }
9190    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
9191    j = strlen30(zSql);
9192    for(i=1; i<nCol; i++){
9193      zSql[j++] = ',';
9194      zSql[j++] = '?';
9195    }
9196    zSql[j++] = ')';
9197    zSql[j] = 0;
9198    if( eVerbose>=2 ){
9199      utf8_printf(p->out, "Insert using: %s\n", zSql);
9200    }
9201    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9202    if( rc ){
9203      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9204      if (pStmt) sqlite3_finalize(pStmt);
9205      goto import_fail;
9206    }
9207    sqlite3_free(zSql);
9208    sqlite3_free(zFullTabName);
9209    needCommit = sqlite3_get_autocommit(p->db);
9210    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
9211    do{
9212      int startLine = sCtx.nLine;
9213      for(i=0; i<nCol; i++){
9214        char *z = xRead(&sCtx);
9215        /*
9216        ** Did we reach end-of-file before finding any columns?
9217        ** If so, stop instead of NULL filling the remaining columns.
9218        */
9219        if( z==0 && i==0 ) break;
9220        /*
9221        ** Did we reach end-of-file OR end-of-line before finding any
9222        ** columns in ASCII mode?  If so, stop instead of NULL filling
9223        ** the remaining columns.
9224        */
9225        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
9226        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
9227        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
9228          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9229                          "filling the rest with NULL\n",
9230                          sCtx.zFile, startLine, nCol, i+1);
9231          i += 2;
9232          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9233        }
9234      }
9235      if( sCtx.cTerm==sCtx.cColSep ){
9236        do{
9237          xRead(&sCtx);
9238          i++;
9239        }while( sCtx.cTerm==sCtx.cColSep );
9240        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9241                        "extras ignored\n",
9242                        sCtx.zFile, startLine, nCol, i);
9243      }
9244      if( i>=nCol ){
9245        sqlite3_step(pStmt);
9246        rc = sqlite3_reset(pStmt);
9247        if( rc!=SQLITE_OK ){
9248          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
9249                      startLine, sqlite3_errmsg(p->db));
9250          sCtx.nErr++;
9251        }else{
9252          sCtx.nRow++;
9253        }
9254      }
9255    }while( sCtx.cTerm!=EOF );
9256
9257    import_cleanup(&sCtx);
9258    sqlite3_finalize(pStmt);
9259    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9260    if( eVerbose>0 ){
9261      utf8_printf(p->out,
9262          "Added %d rows with %d errors using %d lines of input\n",
9263          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9264    }
9265  }else
9266#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9267
9268#ifndef SQLITE_UNTESTABLE
9269  if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
9270    char *zSql;
9271    char *zCollist = 0;
9272    sqlite3_stmt *pStmt;
9273    int tnum = 0;
9274    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
9275    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9276    int i;
9277    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9278      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
9279                          "       .imposter off\n");
9280      /* Also allowed, but not documented:
9281      **
9282      **    .imposter TABLE IMPOSTER
9283      **
9284      ** where TABLE is a WITHOUT ROWID table.  In that case, the
9285      ** imposter is another WITHOUT ROWID table with the columns in
9286      ** storage order. */
9287      rc = 1;
9288      goto meta_command_exit;
9289    }
9290    open_db(p, 0);
9291    if( nArg==2 ){
9292      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9293      goto meta_command_exit;
9294    }
9295    zSql = sqlite3_mprintf(
9296      "SELECT rootpage, 0 FROM sqlite_schema"
9297      " WHERE name='%q' AND type='index'"
9298      "UNION ALL "
9299      "SELECT rootpage, 1 FROM sqlite_schema"
9300      " WHERE name='%q' AND type='table'"
9301      "   AND sql LIKE '%%without%%rowid%%'",
9302      azArg[1], azArg[1]
9303    );
9304    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9305    sqlite3_free(zSql);
9306    if( sqlite3_step(pStmt)==SQLITE_ROW ){
9307      tnum = sqlite3_column_int(pStmt, 0);
9308      isWO = sqlite3_column_int(pStmt, 1);
9309    }
9310    sqlite3_finalize(pStmt);
9311    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9312    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9313    sqlite3_free(zSql);
9314    i = 0;
9315    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9316      char zLabel[20];
9317      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9318      i++;
9319      if( zCol==0 ){
9320        if( sqlite3_column_int(pStmt,1)==-1 ){
9321          zCol = "_ROWID_";
9322        }else{
9323          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9324          zCol = zLabel;
9325        }
9326      }
9327      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9328        lenPK = (int)strlen(zCollist);
9329      }
9330      if( zCollist==0 ){
9331        zCollist = sqlite3_mprintf("\"%w\"", zCol);
9332      }else{
9333        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9334      }
9335    }
9336    sqlite3_finalize(pStmt);
9337    if( i==0 || tnum==0 ){
9338      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
9339      rc = 1;
9340      sqlite3_free(zCollist);
9341      goto meta_command_exit;
9342    }
9343    if( lenPK==0 ) lenPK = 100000;
9344    zSql = sqlite3_mprintf(
9345          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9346          azArg[2], zCollist, lenPK, zCollist);
9347    sqlite3_free(zCollist);
9348    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9349    if( rc==SQLITE_OK ){
9350      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9351      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9352      if( rc ){
9353        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9354      }else{
9355        utf8_printf(stdout, "%s;\n", zSql);
9356        raw_printf(stdout,
9357          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
9358          azArg[1], isWO ? "table" : "index"
9359        );
9360      }
9361    }else{
9362      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9363      rc = 1;
9364    }
9365    sqlite3_free(zSql);
9366  }else
9367#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9368
9369#ifdef SQLITE_ENABLE_IOTRACE
9370  if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
9371    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9372    if( iotrace && iotrace!=stdout ) fclose(iotrace);
9373    iotrace = 0;
9374    if( nArg<2 ){
9375      sqlite3IoTrace = 0;
9376    }else if( cli_strcmp(azArg[1], "-")==0 ){
9377      sqlite3IoTrace = iotracePrintf;
9378      iotrace = stdout;
9379    }else{
9380      iotrace = fopen(azArg[1], "w");
9381      if( iotrace==0 ){
9382        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9383        sqlite3IoTrace = 0;
9384        rc = 1;
9385      }else{
9386        sqlite3IoTrace = iotracePrintf;
9387      }
9388    }
9389  }else
9390#endif
9391
9392  if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
9393    static const struct {
9394       const char *zLimitName;   /* Name of a limit */
9395       int limitCode;            /* Integer code for that limit */
9396    } aLimit[] = {
9397      { "length",                SQLITE_LIMIT_LENGTH                    },
9398      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
9399      { "column",                SQLITE_LIMIT_COLUMN                    },
9400      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
9401      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
9402      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
9403      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
9404      { "attached",              SQLITE_LIMIT_ATTACHED                  },
9405      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
9406      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
9407      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
9408      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
9409    };
9410    int i, n2;
9411    open_db(p, 0);
9412    if( nArg==1 ){
9413      for(i=0; i<ArraySize(aLimit); i++){
9414        printf("%20s %d\n", aLimit[i].zLimitName,
9415               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9416      }
9417    }else if( nArg>3 ){
9418      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
9419      rc = 1;
9420      goto meta_command_exit;
9421    }else{
9422      int iLimit = -1;
9423      n2 = strlen30(azArg[1]);
9424      for(i=0; i<ArraySize(aLimit); i++){
9425        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9426          if( iLimit<0 ){
9427            iLimit = i;
9428          }else{
9429            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
9430            rc = 1;
9431            goto meta_command_exit;
9432          }
9433        }
9434      }
9435      if( iLimit<0 ){
9436        utf8_printf(stderr, "unknown limit: \"%s\"\n"
9437                        "enter \".limits\" with no arguments for a list.\n",
9438                         azArg[1]);
9439        rc = 1;
9440        goto meta_command_exit;
9441      }
9442      if( nArg==3 ){
9443        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9444                      (int)integerValue(azArg[2]));
9445      }
9446      printf("%20s %d\n", aLimit[iLimit].zLimitName,
9447             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9448    }
9449  }else
9450
9451  if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
9452    open_db(p, 0);
9453    lintDotCommand(p, azArg, nArg);
9454  }else
9455
9456#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9457  if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
9458    const char *zFile, *zProc;
9459    char *zErrMsg = 0;
9460    failIfSafeMode(p, "cannot run .load in safe mode");
9461    if( nArg<2 ){
9462      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
9463      rc = 1;
9464      goto meta_command_exit;
9465    }
9466    zFile = azArg[1];
9467    zProc = nArg>=3 ? azArg[2] : 0;
9468    open_db(p, 0);
9469    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9470    if( rc!=SQLITE_OK ){
9471      utf8_printf(stderr, "Error: %s\n", zErrMsg);
9472      sqlite3_free(zErrMsg);
9473      rc = 1;
9474    }
9475  }else
9476#endif
9477
9478#ifndef SQLITE_SHELL_FIDDLE
9479  if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
9480    failIfSafeMode(p, "cannot run .log in safe mode");
9481    if( nArg!=2 ){
9482      raw_printf(stderr, "Usage: .log FILENAME\n");
9483      rc = 1;
9484    }else{
9485      const char *zFile = azArg[1];
9486      output_file_close(p->pLog);
9487      p->pLog = output_file_open(zFile, 0);
9488    }
9489  }else
9490#endif
9491
9492  if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
9493    const char *zMode = 0;
9494    const char *zTabname = 0;
9495    int i, n2;
9496    ColModeOpts cmOpts = ColModeOpts_default;
9497    for(i=1; i<nArg; i++){
9498      const char *z = azArg[i];
9499      if( optionMatch(z,"wrap") && i+1<nArg ){
9500        cmOpts.iWrap = integerValue(azArg[++i]);
9501      }else if( optionMatch(z,"ww") ){
9502        cmOpts.bWordWrap = 1;
9503      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9504        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9505      }else if( optionMatch(z,"quote") ){
9506        cmOpts.bQuote = 1;
9507      }else if( optionMatch(z,"noquote") ){
9508        cmOpts.bQuote = 0;
9509      }else if( zMode==0 ){
9510        zMode = z;
9511        /* Apply defaults for qbox pseudo-mode.  If that
9512         * overwrites already-set values, user was informed of this.
9513         */
9514        if( cli_strcmp(z, "qbox")==0 ){
9515          ColModeOpts cmo = ColModeOpts_default_qbox;
9516          zMode = "box";
9517          cmOpts = cmo;
9518        }
9519      }else if( zTabname==0 ){
9520        zTabname = z;
9521      }else if( z[0]=='-' ){
9522        utf8_printf(stderr, "unknown option: %s\n", z);
9523        utf8_printf(stderr, "options:\n"
9524                            "  --noquote\n"
9525                            "  --quote\n"
9526                            "  --wordwrap on/off\n"
9527                            "  --wrap N\n"
9528                            "  --ww\n");
9529        rc = 1;
9530        goto meta_command_exit;
9531      }else{
9532        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9533        rc = 1;
9534        goto meta_command_exit;
9535      }
9536    }
9537    if( zMode==0 ){
9538      if( p->mode==MODE_Column
9539       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9540      ){
9541        raw_printf
9542          (p->out,
9543           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9544           modeDescr[p->mode], p->cmOpts.iWrap,
9545           p->cmOpts.bWordWrap ? "on" : "off",
9546           p->cmOpts.bQuote ? "" : "no");
9547      }else{
9548        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
9549      }
9550      zMode = modeDescr[p->mode];
9551    }
9552    n2 = strlen30(zMode);
9553    if( cli_strncmp(zMode,"lines",n2)==0 ){
9554      p->mode = MODE_Line;
9555      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9556    }else if( cli_strncmp(zMode,"columns",n2)==0 ){
9557      p->mode = MODE_Column;
9558      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9559        p->showHeader = 1;
9560      }
9561      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9562      p->cmOpts = cmOpts;
9563    }else if( cli_strncmp(zMode,"list",n2)==0 ){
9564      p->mode = MODE_List;
9565      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9566      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9567    }else if( cli_strncmp(zMode,"html",n2)==0 ){
9568      p->mode = MODE_Html;
9569    }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
9570      p->mode = MODE_Tcl;
9571      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9572      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9573    }else if( cli_strncmp(zMode,"csv",n2)==0 ){
9574      p->mode = MODE_Csv;
9575      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9576      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9577    }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
9578      p->mode = MODE_List;
9579      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9580    }else if( cli_strncmp(zMode,"insert",n2)==0 ){
9581      p->mode = MODE_Insert;
9582      set_table_name(p, zTabname ? zTabname : "table");
9583    }else if( cli_strncmp(zMode,"quote",n2)==0 ){
9584      p->mode = MODE_Quote;
9585      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9586      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9587    }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
9588      p->mode = MODE_Ascii;
9589      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9590      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9591    }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
9592      p->mode = MODE_Markdown;
9593      p->cmOpts = cmOpts;
9594    }else if( cli_strncmp(zMode,"table",n2)==0 ){
9595      p->mode = MODE_Table;
9596      p->cmOpts = cmOpts;
9597    }else if( cli_strncmp(zMode,"box",n2)==0 ){
9598      p->mode = MODE_Box;
9599      p->cmOpts = cmOpts;
9600    }else if( cli_strncmp(zMode,"count",n2)==0 ){
9601      p->mode = MODE_Count;
9602    }else if( cli_strncmp(zMode,"off",n2)==0 ){
9603      p->mode = MODE_Off;
9604    }else if( cli_strncmp(zMode,"json",n2)==0 ){
9605      p->mode = MODE_Json;
9606    }else{
9607      raw_printf(stderr, "Error: mode should be one of: "
9608         "ascii box column csv html insert json line list markdown "
9609         "qbox quote table tabs tcl\n");
9610      rc = 1;
9611    }
9612    p->cMode = p->mode;
9613  }else
9614
9615#ifndef SQLITE_SHELL_FIDDLE
9616  if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
9617    if( nArg!=2 ){
9618      raw_printf(stderr, "Usage: .nonce NONCE\n");
9619      rc = 1;
9620    }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
9621      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9622                 p->lineno, azArg[1]);
9623      exit(1);
9624    }else{
9625      p->bSafeMode = 0;
9626      return 0;  /* Return immediately to bypass the safe mode reset
9627                 ** at the end of this procedure */
9628    }
9629  }else
9630#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9631
9632  if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
9633    if( nArg==2 ){
9634      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9635                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9636    }else{
9637      raw_printf(stderr, "Usage: .nullvalue STRING\n");
9638      rc = 1;
9639    }
9640  }else
9641
9642  if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
9643    const char *zFN = 0;     /* Pointer to constant filename */
9644    char *zNewFilename = 0;  /* Name of the database file to open */
9645    int iName = 1;           /* Index in azArg[] of the filename */
9646    int newFlag = 0;         /* True to delete file before opening */
9647    int openMode = SHELL_OPEN_UNSPEC;
9648
9649    /* Check for command-line arguments */
9650    for(iName=1; iName<nArg; iName++){
9651      const char *z = azArg[iName];
9652#ifndef SQLITE_SHELL_FIDDLE
9653      if( optionMatch(z,"new") ){
9654        newFlag = 1;
9655#ifdef SQLITE_HAVE_ZLIB
9656      }else if( optionMatch(z, "zip") ){
9657        openMode = SHELL_OPEN_ZIPFILE;
9658#endif
9659      }else if( optionMatch(z, "append") ){
9660        openMode = SHELL_OPEN_APPENDVFS;
9661      }else if( optionMatch(z, "readonly") ){
9662        openMode = SHELL_OPEN_READONLY;
9663      }else if( optionMatch(z, "nofollow") ){
9664        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9665#ifndef SQLITE_OMIT_DESERIALIZE
9666      }else if( optionMatch(z, "deserialize") ){
9667        openMode = SHELL_OPEN_DESERIALIZE;
9668      }else if( optionMatch(z, "hexdb") ){
9669        openMode = SHELL_OPEN_HEXDB;
9670      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9671        p->szMax = integerValue(azArg[++iName]);
9672#endif /* SQLITE_OMIT_DESERIALIZE */
9673      }else
9674#endif /* !SQLITE_SHELL_FIDDLE */
9675      if( z[0]=='-' ){
9676        utf8_printf(stderr, "unknown option: %s\n", z);
9677        rc = 1;
9678        goto meta_command_exit;
9679      }else if( zFN ){
9680        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9681        rc = 1;
9682        goto meta_command_exit;
9683      }else{
9684        zFN = z;
9685      }
9686    }
9687
9688    /* Close the existing database */
9689    session_close_all(p, -1);
9690    close_db(p->db);
9691    p->db = 0;
9692    p->pAuxDb->zDbFilename = 0;
9693    sqlite3_free(p->pAuxDb->zFreeOnClose);
9694    p->pAuxDb->zFreeOnClose = 0;
9695    p->openMode = openMode;
9696    p->openFlags = 0;
9697    p->szMax = 0;
9698
9699    /* If a filename is specified, try to open it first */
9700    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9701      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9702#ifndef SQLITE_SHELL_FIDDLE
9703      if( p->bSafeMode
9704       && p->openMode!=SHELL_OPEN_HEXDB
9705       && zFN
9706       && cli_strcmp(zFN,":memory:")!=0
9707      ){
9708        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9709      }
9710#else
9711      /* WASM mode has its own sandboxed pseudo-filesystem. */
9712#endif
9713      if( zFN ){
9714        zNewFilename = sqlite3_mprintf("%s", zFN);
9715        shell_check_oom(zNewFilename);
9716      }else{
9717        zNewFilename = 0;
9718      }
9719      p->pAuxDb->zDbFilename = zNewFilename;
9720      open_db(p, OPEN_DB_KEEPALIVE);
9721      if( p->db==0 ){
9722        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9723        sqlite3_free(zNewFilename);
9724      }else{
9725        p->pAuxDb->zFreeOnClose = zNewFilename;
9726      }
9727    }
9728    if( p->db==0 ){
9729      /* As a fall-back open a TEMP database */
9730      p->pAuxDb->zDbFilename = 0;
9731      open_db(p, 0);
9732    }
9733  }else
9734
9735#ifndef SQLITE_SHELL_FIDDLE
9736  if( (c=='o'
9737        && (cli_strncmp(azArg[0], "output", n)==0
9738            || cli_strncmp(azArg[0], "once", n)==0))
9739   || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
9740  ){
9741    char *zFile = 0;
9742    int bTxtMode = 0;
9743    int i;
9744    int eMode = 0;
9745    int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
9746    unsigned char zBOM[4];    /* Byte-order mark to using if --bom is present */
9747
9748    zBOM[0] = 0;
9749    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9750    if( c=='e' ){
9751      eMode = 'x';
9752      bOnce = 2;
9753    }else if( cli_strncmp(azArg[0],"once",n)==0 ){
9754      bOnce = 1;
9755    }
9756    for(i=1; i<nArg; i++){
9757      char *z = azArg[i];
9758      if( z[0]=='-' ){
9759        if( z[1]=='-' ) z++;
9760        if( cli_strcmp(z,"-bom")==0 ){
9761          zBOM[0] = 0xef;
9762          zBOM[1] = 0xbb;
9763          zBOM[2] = 0xbf;
9764          zBOM[3] = 0;
9765        }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
9766          eMode = 'x';  /* spreadsheet */
9767        }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
9768          eMode = 'e';  /* text editor */
9769        }else{
9770          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9771                      azArg[i]);
9772          showHelp(p->out, azArg[0]);
9773          rc = 1;
9774          goto meta_command_exit;
9775        }
9776      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9777        zFile = sqlite3_mprintf("%s", z);
9778        if( zFile && zFile[0]=='|' ){
9779          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9780          break;
9781        }
9782      }else{
9783        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9784                    azArg[i]);
9785        showHelp(p->out, azArg[0]);
9786        rc = 1;
9787        sqlite3_free(zFile);
9788        goto meta_command_exit;
9789      }
9790    }
9791    if( zFile==0 ){
9792      zFile = sqlite3_mprintf("stdout");
9793    }
9794    if( bOnce ){
9795      p->outCount = 2;
9796    }else{
9797      p->outCount = 0;
9798    }
9799    output_reset(p);
9800#ifndef SQLITE_NOHAVE_SYSTEM
9801    if( eMode=='e' || eMode=='x' ){
9802      p->doXdgOpen = 1;
9803      outputModePush(p);
9804      if( eMode=='x' ){
9805        /* spreadsheet mode.  Output as CSV. */
9806        newTempFile(p, "csv");
9807        ShellClearFlag(p, SHFLG_Echo);
9808        p->mode = MODE_Csv;
9809        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9810        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9811      }else{
9812        /* text editor mode */
9813        newTempFile(p, "txt");
9814        bTxtMode = 1;
9815      }
9816      sqlite3_free(zFile);
9817      zFile = sqlite3_mprintf("%s", p->zTempFile);
9818    }
9819#endif /* SQLITE_NOHAVE_SYSTEM */
9820    shell_check_oom(zFile);
9821    if( zFile[0]=='|' ){
9822#ifdef SQLITE_OMIT_POPEN
9823      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9824      rc = 1;
9825      p->out = stdout;
9826#else
9827      p->out = popen(zFile + 1, "w");
9828      if( p->out==0 ){
9829        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9830        p->out = stdout;
9831        rc = 1;
9832      }else{
9833        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9834        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9835      }
9836#endif
9837    }else{
9838      p->out = output_file_open(zFile, bTxtMode);
9839      if( p->out==0 ){
9840        if( cli_strcmp(zFile,"off")!=0 ){
9841          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9842        }
9843        p->out = stdout;
9844        rc = 1;
9845      } else {
9846        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9847        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9848      }
9849    }
9850    sqlite3_free(zFile);
9851  }else
9852#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9853
9854  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
9855    open_db(p,0);
9856    if( nArg<=1 ) goto parameter_syntax_error;
9857
9858    /* .parameter clear
9859    ** Clear all bind parameters by dropping the TEMP table that holds them.
9860    */
9861    if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
9862      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9863                   0, 0, 0);
9864    }else
9865
9866    /* .parameter list
9867    ** List all bind parameters.
9868    */
9869    if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
9870      sqlite3_stmt *pStmt = 0;
9871      int rx;
9872      int len = 0;
9873      rx = sqlite3_prepare_v2(p->db,
9874             "SELECT max(length(key)) "
9875             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9876      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9877        len = sqlite3_column_int(pStmt, 0);
9878        if( len>40 ) len = 40;
9879      }
9880      sqlite3_finalize(pStmt);
9881      pStmt = 0;
9882      if( len ){
9883        rx = sqlite3_prepare_v2(p->db,
9884             "SELECT key, quote(value) "
9885             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9886        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9887          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9888                      sqlite3_column_text(pStmt,1));
9889        }
9890        sqlite3_finalize(pStmt);
9891      }
9892    }else
9893
9894    /* .parameter init
9895    ** Make sure the TEMP table used to hold bind parameters exists.
9896    ** Create it if necessary.
9897    */
9898    if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
9899      bind_table_init(p);
9900    }else
9901
9902    /* .parameter set NAME VALUE
9903    ** Set or reset a bind parameter.  NAME should be the full parameter
9904    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9905    ** VALUE can be in either SQL literal notation, or if not it will be
9906    ** understood to be a text string.
9907    */
9908    if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
9909      int rx;
9910      char *zSql;
9911      sqlite3_stmt *pStmt;
9912      const char *zKey = azArg[2];
9913      const char *zValue = azArg[3];
9914      bind_table_init(p);
9915      zSql = sqlite3_mprintf(
9916                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9917                  "VALUES(%Q,%s);", zKey, zValue);
9918      shell_check_oom(zSql);
9919      pStmt = 0;
9920      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9921      sqlite3_free(zSql);
9922      if( rx!=SQLITE_OK ){
9923        sqlite3_finalize(pStmt);
9924        pStmt = 0;
9925        zSql = sqlite3_mprintf(
9926                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9927                   "VALUES(%Q,%Q);", zKey, zValue);
9928        shell_check_oom(zSql);
9929        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9930        sqlite3_free(zSql);
9931        if( rx!=SQLITE_OK ){
9932          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9933          sqlite3_finalize(pStmt);
9934          pStmt = 0;
9935          rc = 1;
9936        }
9937      }
9938      sqlite3_step(pStmt);
9939      sqlite3_finalize(pStmt);
9940    }else
9941
9942    /* .parameter unset NAME
9943    ** Remove the NAME binding from the parameter binding table, if it
9944    ** exists.
9945    */
9946    if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
9947      char *zSql = sqlite3_mprintf(
9948          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9949      shell_check_oom(zSql);
9950      sqlite3_exec(p->db, zSql, 0, 0, 0);
9951      sqlite3_free(zSql);
9952    }else
9953    /* If no command name matches, show a syntax error */
9954    parameter_syntax_error:
9955    showHelp(p->out, "parameter");
9956  }else
9957
9958  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
9959    int i;
9960    for(i=1; i<nArg; i++){
9961      if( i>1 ) raw_printf(p->out, " ");
9962      utf8_printf(p->out, "%s", azArg[i]);
9963    }
9964    raw_printf(p->out, "\n");
9965  }else
9966
9967#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9968  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
9969    int i;
9970    int nn = 0;
9971    p->flgProgress = 0;
9972    p->mxProgress = 0;
9973    p->nProgress = 0;
9974    for(i=1; i<nArg; i++){
9975      const char *z = azArg[i];
9976      if( z[0]=='-' ){
9977        z++;
9978        if( z[0]=='-' ) z++;
9979        if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
9980          p->flgProgress |= SHELL_PROGRESS_QUIET;
9981          continue;
9982        }
9983        if( cli_strcmp(z,"reset")==0 ){
9984          p->flgProgress |= SHELL_PROGRESS_RESET;
9985          continue;
9986        }
9987        if( cli_strcmp(z,"once")==0 ){
9988          p->flgProgress |= SHELL_PROGRESS_ONCE;
9989          continue;
9990        }
9991        if( cli_strcmp(z,"limit")==0 ){
9992          if( i+1>=nArg ){
9993            utf8_printf(stderr, "Error: missing argument on --limit\n");
9994            rc = 1;
9995            goto meta_command_exit;
9996          }else{
9997            p->mxProgress = (int)integerValue(azArg[++i]);
9998          }
9999          continue;
10000        }
10001        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
10002        rc = 1;
10003        goto meta_command_exit;
10004      }else{
10005        nn = (int)integerValue(z);
10006      }
10007    }
10008    open_db(p, 0);
10009    sqlite3_progress_handler(p->db, nn, progress_handler, p);
10010  }else
10011#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10012
10013  if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
10014    if( nArg >= 2) {
10015      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
10016    }
10017    if( nArg >= 3) {
10018      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
10019    }
10020  }else
10021
10022#ifndef SQLITE_SHELL_FIDDLE
10023  if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
10024    rc = 2;
10025  }else
10026#endif
10027
10028#ifndef SQLITE_SHELL_FIDDLE
10029  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
10030    FILE *inSaved = p->in;
10031    int savedLineno = p->lineno;
10032    failIfSafeMode(p, "cannot run .read in safe mode");
10033    if( nArg!=2 ){
10034      raw_printf(stderr, "Usage: .read FILE\n");
10035      rc = 1;
10036      goto meta_command_exit;
10037    }
10038    if( azArg[1][0]=='|' ){
10039#ifdef SQLITE_OMIT_POPEN
10040      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
10041      rc = 1;
10042      p->out = stdout;
10043#else
10044      p->in = popen(azArg[1]+1, "r");
10045      if( p->in==0 ){
10046        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
10047        rc = 1;
10048      }else{
10049        rc = process_input(p);
10050        pclose(p->in);
10051      }
10052#endif
10053    }else if( (p->in = openChrSource(azArg[1]))==0 ){
10054      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
10055      rc = 1;
10056    }else{
10057      rc = process_input(p);
10058      fclose(p->in);
10059    }
10060    p->in = inSaved;
10061    p->lineno = savedLineno;
10062  }else
10063#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10064
10065#ifndef SQLITE_SHELL_FIDDLE
10066  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
10067    const char *zSrcFile;
10068    const char *zDb;
10069    sqlite3 *pSrc;
10070    sqlite3_backup *pBackup;
10071    int nTimeout = 0;
10072
10073    failIfSafeMode(p, "cannot run .restore in safe mode");
10074    if( nArg==2 ){
10075      zSrcFile = azArg[1];
10076      zDb = "main";
10077    }else if( nArg==3 ){
10078      zSrcFile = azArg[2];
10079      zDb = azArg[1];
10080    }else{
10081      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
10082      rc = 1;
10083      goto meta_command_exit;
10084    }
10085    rc = sqlite3_open(zSrcFile, &pSrc);
10086    if( rc!=SQLITE_OK ){
10087      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
10088      close_db(pSrc);
10089      return 1;
10090    }
10091    open_db(p, 0);
10092    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
10093    if( pBackup==0 ){
10094      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10095      close_db(pSrc);
10096      return 1;
10097    }
10098    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
10099          || rc==SQLITE_BUSY  ){
10100      if( rc==SQLITE_BUSY ){
10101        if( nTimeout++ >= 3 ) break;
10102        sqlite3_sleep(100);
10103      }
10104    }
10105    sqlite3_backup_finish(pBackup);
10106    if( rc==SQLITE_DONE ){
10107      rc = 0;
10108    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
10109      raw_printf(stderr, "Error: source database is busy\n");
10110      rc = 1;
10111    }else{
10112      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10113      rc = 1;
10114    }
10115    close_db(pSrc);
10116  }else
10117#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10118
10119  if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
10120    if( nArg==2 ){
10121      p->scanstatsOn = (u8)booleanValue(azArg[1]);
10122#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10123      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
10124#endif
10125    }else{
10126      raw_printf(stderr, "Usage: .scanstats on|off\n");
10127      rc = 1;
10128    }
10129  }else
10130
10131  if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
10132    ShellText sSelect;
10133    ShellState data;
10134    char *zErrMsg = 0;
10135    const char *zDiv = "(";
10136    const char *zName = 0;
10137    int iSchema = 0;
10138    int bDebug = 0;
10139    int bNoSystemTabs = 0;
10140    int ii;
10141
10142    open_db(p, 0);
10143    memcpy(&data, p, sizeof(data));
10144    data.showHeader = 0;
10145    data.cMode = data.mode = MODE_Semi;
10146    initText(&sSelect);
10147    for(ii=1; ii<nArg; ii++){
10148      if( optionMatch(azArg[ii],"indent") ){
10149        data.cMode = data.mode = MODE_Pretty;
10150      }else if( optionMatch(azArg[ii],"debug") ){
10151        bDebug = 1;
10152      }else if( optionMatch(azArg[ii],"nosys") ){
10153        bNoSystemTabs = 1;
10154      }else if( azArg[ii][0]=='-' ){
10155        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
10156        rc = 1;
10157        goto meta_command_exit;
10158      }else if( zName==0 ){
10159        zName = azArg[ii];
10160      }else{
10161        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10162        rc = 1;
10163        goto meta_command_exit;
10164      }
10165    }
10166    if( zName!=0 ){
10167      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
10168                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
10169                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
10170                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
10171      if( isSchema ){
10172        char *new_argv[2], *new_colv[2];
10173        new_argv[0] = sqlite3_mprintf(
10174                      "CREATE TABLE %s (\n"
10175                      "  type text,\n"
10176                      "  name text,\n"
10177                      "  tbl_name text,\n"
10178                      "  rootpage integer,\n"
10179                      "  sql text\n"
10180                      ")", zName);
10181        shell_check_oom(new_argv[0]);
10182        new_argv[1] = 0;
10183        new_colv[0] = "sql";
10184        new_colv[1] = 0;
10185        callback(&data, 1, new_argv, new_colv);
10186        sqlite3_free(new_argv[0]);
10187      }
10188    }
10189    if( zDiv ){
10190      sqlite3_stmt *pStmt = 0;
10191      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
10192                              -1, &pStmt, 0);
10193      if( rc ){
10194        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10195        sqlite3_finalize(pStmt);
10196        rc = 1;
10197        goto meta_command_exit;
10198      }
10199      appendText(&sSelect, "SELECT sql FROM", 0);
10200      iSchema = 0;
10201      while( sqlite3_step(pStmt)==SQLITE_ROW ){
10202        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
10203        char zScNum[30];
10204        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
10205        appendText(&sSelect, zDiv, 0);
10206        zDiv = " UNION ALL ";
10207        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
10208        if( sqlite3_stricmp(zDb, "main")!=0 ){
10209          appendText(&sSelect, zDb, '\'');
10210        }else{
10211          appendText(&sSelect, "NULL", 0);
10212        }
10213        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10214        appendText(&sSelect, zScNum, 0);
10215        appendText(&sSelect, " AS snum, ", 0);
10216        appendText(&sSelect, zDb, '\'');
10217        appendText(&sSelect, " AS sname FROM ", 0);
10218        appendText(&sSelect, zDb, quoteChar(zDb));
10219        appendText(&sSelect, ".sqlite_schema", 0);
10220      }
10221      sqlite3_finalize(pStmt);
10222#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10223      if( zName ){
10224        appendText(&sSelect,
10225           " UNION ALL SELECT shell_module_schema(name),"
10226           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10227        0);
10228      }
10229#endif
10230      appendText(&sSelect, ") WHERE ", 0);
10231      if( zName ){
10232        char *zQarg = sqlite3_mprintf("%Q", zName);
10233        int bGlob;
10234        shell_check_oom(zQarg);
10235        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10236                strchr(zName, '[') != 0;
10237        if( strchr(zName, '.') ){
10238          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10239        }else{
10240          appendText(&sSelect, "lower(tbl_name)", 0);
10241        }
10242        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10243        appendText(&sSelect, zQarg, 0);
10244        if( !bGlob ){
10245          appendText(&sSelect, " ESCAPE '\\' ", 0);
10246        }
10247        appendText(&sSelect, " AND ", 0);
10248        sqlite3_free(zQarg);
10249      }
10250      if( bNoSystemTabs ){
10251        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10252      }
10253      appendText(&sSelect, "sql IS NOT NULL"
10254                           " ORDER BY snum, rowid", 0);
10255      if( bDebug ){
10256        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
10257      }else{
10258        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10259      }
10260      freeText(&sSelect);
10261    }
10262    if( zErrMsg ){
10263      utf8_printf(stderr,"Error: %s\n", zErrMsg);
10264      sqlite3_free(zErrMsg);
10265      rc = 1;
10266    }else if( rc != SQLITE_OK ){
10267      raw_printf(stderr,"Error: querying schema information\n");
10268      rc = 1;
10269    }else{
10270      rc = 0;
10271    }
10272  }else
10273
10274  if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
10275   || (c=='t' && n==9  && cli_strncmp(azArg[0], "treetrace", n)==0)
10276  ){
10277    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10278    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10279  }else
10280
10281#if defined(SQLITE_ENABLE_SESSION)
10282  if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
10283    struct AuxDb *pAuxDb = p->pAuxDb;
10284    OpenSession *pSession = &pAuxDb->aSession[0];
10285    char **azCmd = &azArg[1];
10286    int iSes = 0;
10287    int nCmd = nArg - 1;
10288    int i;
10289    if( nArg<=1 ) goto session_syntax_error;
10290    open_db(p, 0);
10291    if( nArg>=3 ){
10292      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10293        if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10294      }
10295      if( iSes<pAuxDb->nSession ){
10296        pSession = &pAuxDb->aSession[iSes];
10297        azCmd++;
10298        nCmd--;
10299      }else{
10300        pSession = &pAuxDb->aSession[0];
10301        iSes = 0;
10302      }
10303    }
10304
10305    /* .session attach TABLE
10306    ** Invoke the sqlite3session_attach() interface to attach a particular
10307    ** table so that it is never filtered.
10308    */
10309    if( cli_strcmp(azCmd[0],"attach")==0 ){
10310      if( nCmd!=2 ) goto session_syntax_error;
10311      if( pSession->p==0 ){
10312        session_not_open:
10313        raw_printf(stderr, "ERROR: No sessions are open\n");
10314      }else{
10315        rc = sqlite3session_attach(pSession->p, azCmd[1]);
10316        if( rc ){
10317          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
10318          rc = 0;
10319        }
10320      }
10321    }else
10322
10323    /* .session changeset FILE
10324    ** .session patchset FILE
10325    ** Write a changeset or patchset into a file.  The file is overwritten.
10326    */
10327    if( cli_strcmp(azCmd[0],"changeset")==0
10328     || cli_strcmp(azCmd[0],"patchset")==0
10329    ){
10330      FILE *out = 0;
10331      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10332      if( nCmd!=2 ) goto session_syntax_error;
10333      if( pSession->p==0 ) goto session_not_open;
10334      out = fopen(azCmd[1], "wb");
10335      if( out==0 ){
10336        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
10337                    azCmd[1]);
10338      }else{
10339        int szChng;
10340        void *pChng;
10341        if( azCmd[0][0]=='c' ){
10342          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10343        }else{
10344          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10345        }
10346        if( rc ){
10347          printf("Error: error code %d\n", rc);
10348          rc = 0;
10349        }
10350        if( pChng
10351          && fwrite(pChng, szChng, 1, out)!=1 ){
10352          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
10353                  szChng);
10354        }
10355        sqlite3_free(pChng);
10356        fclose(out);
10357      }
10358    }else
10359
10360    /* .session close
10361    ** Close the identified session
10362    */
10363    if( cli_strcmp(azCmd[0], "close")==0 ){
10364      if( nCmd!=1 ) goto session_syntax_error;
10365      if( pAuxDb->nSession ){
10366        session_close(pSession);
10367        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10368      }
10369    }else
10370
10371    /* .session enable ?BOOLEAN?
10372    ** Query or set the enable flag
10373    */
10374    if( cli_strcmp(azCmd[0], "enable")==0 ){
10375      int ii;
10376      if( nCmd>2 ) goto session_syntax_error;
10377      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10378      if( pAuxDb->nSession ){
10379        ii = sqlite3session_enable(pSession->p, ii);
10380        utf8_printf(p->out, "session %s enable flag = %d\n",
10381                    pSession->zName, ii);
10382      }
10383    }else
10384
10385    /* .session filter GLOB ....
10386    ** Set a list of GLOB patterns of table names to be excluded.
10387    */
10388    if( cli_strcmp(azCmd[0], "filter")==0 ){
10389      int ii, nByte;
10390      if( nCmd<2 ) goto session_syntax_error;
10391      if( pAuxDb->nSession ){
10392        for(ii=0; ii<pSession->nFilter; ii++){
10393          sqlite3_free(pSession->azFilter[ii]);
10394        }
10395        sqlite3_free(pSession->azFilter);
10396        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10397        pSession->azFilter = sqlite3_malloc( nByte );
10398        if( pSession->azFilter==0 ){
10399          raw_printf(stderr, "Error: out or memory\n");
10400          exit(1);
10401        }
10402        for(ii=1; ii<nCmd; ii++){
10403          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10404          shell_check_oom(x);
10405        }
10406        pSession->nFilter = ii-1;
10407      }
10408    }else
10409
10410    /* .session indirect ?BOOLEAN?
10411    ** Query or set the indirect flag
10412    */
10413    if( cli_strcmp(azCmd[0], "indirect")==0 ){
10414      int ii;
10415      if( nCmd>2 ) goto session_syntax_error;
10416      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10417      if( pAuxDb->nSession ){
10418        ii = sqlite3session_indirect(pSession->p, ii);
10419        utf8_printf(p->out, "session %s indirect flag = %d\n",
10420                    pSession->zName, ii);
10421      }
10422    }else
10423
10424    /* .session isempty
10425    ** Determine if the session is empty
10426    */
10427    if( cli_strcmp(azCmd[0], "isempty")==0 ){
10428      int ii;
10429      if( nCmd!=1 ) goto session_syntax_error;
10430      if( pAuxDb->nSession ){
10431        ii = sqlite3session_isempty(pSession->p);
10432        utf8_printf(p->out, "session %s isempty flag = %d\n",
10433                    pSession->zName, ii);
10434      }
10435    }else
10436
10437    /* .session list
10438    ** List all currently open sessions
10439    */
10440    if( cli_strcmp(azCmd[0],"list")==0 ){
10441      for(i=0; i<pAuxDb->nSession; i++){
10442        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
10443      }
10444    }else
10445
10446    /* .session open DB NAME
10447    ** Open a new session called NAME on the attached database DB.
10448    ** DB is normally "main".
10449    */
10450    if( cli_strcmp(azCmd[0],"open")==0 ){
10451      char *zName;
10452      if( nCmd!=3 ) goto session_syntax_error;
10453      zName = azCmd[2];
10454      if( zName[0]==0 ) goto session_syntax_error;
10455      for(i=0; i<pAuxDb->nSession; i++){
10456        if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10457          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
10458          goto meta_command_exit;
10459        }
10460      }
10461      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10462        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10463        goto meta_command_exit;
10464      }
10465      pSession = &pAuxDb->aSession[pAuxDb->nSession];
10466      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10467      if( rc ){
10468        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
10469        rc = 0;
10470        goto meta_command_exit;
10471      }
10472      pSession->nFilter = 0;
10473      sqlite3session_table_filter(pSession->p, session_filter, pSession);
10474      pAuxDb->nSession++;
10475      pSession->zName = sqlite3_mprintf("%s", zName);
10476      shell_check_oom(pSession->zName);
10477    }else
10478    /* If no command name matches, show a syntax error */
10479    session_syntax_error:
10480    showHelp(p->out, "session");
10481  }else
10482#endif
10483
10484#ifdef SQLITE_DEBUG
10485  /* Undocumented commands for internal testing.  Subject to change
10486  ** without notice. */
10487  if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
10488    if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10489      int i, v;
10490      for(i=1; i<nArg; i++){
10491        v = booleanValue(azArg[i]);
10492        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
10493      }
10494    }
10495    if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
10496      int i; sqlite3_int64 v;
10497      for(i=1; i<nArg; i++){
10498        char zBuf[200];
10499        v = integerValue(azArg[i]);
10500        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10501        utf8_printf(p->out, "%s", zBuf);
10502      }
10503    }
10504  }else
10505#endif
10506
10507  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
10508    int bIsInit = 0;         /* True to initialize the SELFTEST table */
10509    int bVerbose = 0;        /* Verbose output */
10510    int bSelftestExists;     /* True if SELFTEST already exists */
10511    int i, k;                /* Loop counters */
10512    int nTest = 0;           /* Number of tests runs */
10513    int nErr = 0;            /* Number of errors seen */
10514    ShellText str;           /* Answer for a query */
10515    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10516
10517    open_db(p,0);
10518    for(i=1; i<nArg; i++){
10519      const char *z = azArg[i];
10520      if( z[0]=='-' && z[1]=='-' ) z++;
10521      if( cli_strcmp(z,"-init")==0 ){
10522        bIsInit = 1;
10523      }else
10524      if( cli_strcmp(z,"-v")==0 ){
10525        bVerbose++;
10526      }else
10527      {
10528        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10529                    azArg[i], azArg[0]);
10530        raw_printf(stderr, "Should be one of: --init -v\n");
10531        rc = 1;
10532        goto meta_command_exit;
10533      }
10534    }
10535    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10536           != SQLITE_OK ){
10537      bSelftestExists = 0;
10538    }else{
10539      bSelftestExists = 1;
10540    }
10541    if( bIsInit ){
10542      createSelftestTable(p);
10543      bSelftestExists = 1;
10544    }
10545    initText(&str);
10546    appendText(&str, "x", 0);
10547    for(k=bSelftestExists; k>=0; k--){
10548      if( k==1 ){
10549        rc = sqlite3_prepare_v2(p->db,
10550            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10551            -1, &pStmt, 0);
10552      }else{
10553        rc = sqlite3_prepare_v2(p->db,
10554          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10555          "      (1,'run','PRAGMA integrity_check','ok')",
10556          -1, &pStmt, 0);
10557      }
10558      if( rc ){
10559        raw_printf(stderr, "Error querying the selftest table\n");
10560        rc = 1;
10561        sqlite3_finalize(pStmt);
10562        goto meta_command_exit;
10563      }
10564      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10565        int tno = sqlite3_column_int(pStmt, 0);
10566        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10567        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10568        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10569
10570        if( zOp==0 ) continue;
10571        if( zSql==0 ) continue;
10572        if( zAns==0 ) continue;
10573        k = 0;
10574        if( bVerbose>0 ){
10575          printf("%d: %s %s\n", tno, zOp, zSql);
10576        }
10577        if( cli_strcmp(zOp,"memo")==0 ){
10578          utf8_printf(p->out, "%s\n", zSql);
10579        }else
10580        if( cli_strcmp(zOp,"run")==0 ){
10581          char *zErrMsg = 0;
10582          str.n = 0;
10583          str.z[0] = 0;
10584          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10585          nTest++;
10586          if( bVerbose ){
10587            utf8_printf(p->out, "Result: %s\n", str.z);
10588          }
10589          if( rc || zErrMsg ){
10590            nErr++;
10591            rc = 1;
10592            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
10593            sqlite3_free(zErrMsg);
10594          }else if( cli_strcmp(zAns,str.z)!=0 ){
10595            nErr++;
10596            rc = 1;
10597            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10598            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
10599          }
10600        }else
10601        {
10602          utf8_printf(stderr,
10603            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10604          rc = 1;
10605          break;
10606        }
10607      } /* End loop over rows of content from SELFTEST */
10608      sqlite3_finalize(pStmt);
10609    } /* End loop over k */
10610    freeText(&str);
10611    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10612  }else
10613
10614  if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
10615    if( nArg<2 || nArg>3 ){
10616      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10617      rc = 1;
10618    }
10619    if( nArg>=2 ){
10620      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10621                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10622    }
10623    if( nArg>=3 ){
10624      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10625                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10626    }
10627  }else
10628
10629  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
10630    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
10631    int i;                   /* Loop counter */
10632    int bSchema = 0;         /* Also hash the schema */
10633    int bSeparate = 0;       /* Hash each table separately */
10634    int iSize = 224;         /* Hash algorithm to use */
10635    int bDebug = 0;          /* Only show the query that would have run */
10636    sqlite3_stmt *pStmt;     /* For querying tables names */
10637    char *zSql;              /* SQL to be run */
10638    char *zSep;              /* Separator */
10639    ShellText sSql;          /* Complete SQL for the query to run the hash */
10640    ShellText sQuery;        /* Set of queries used to read all content */
10641    open_db(p, 0);
10642    for(i=1; i<nArg; i++){
10643      const char *z = azArg[i];
10644      if( z[0]=='-' ){
10645        z++;
10646        if( z[0]=='-' ) z++;
10647        if( cli_strcmp(z,"schema")==0 ){
10648          bSchema = 1;
10649        }else
10650        if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
10651         || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
10652        ){
10653          iSize = atoi(&z[5]);
10654        }else
10655        if( cli_strcmp(z,"debug")==0 ){
10656          bDebug = 1;
10657        }else
10658        {
10659          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10660                      azArg[i], azArg[0]);
10661          showHelp(p->out, azArg[0]);
10662          rc = 1;
10663          goto meta_command_exit;
10664        }
10665      }else if( zLike ){
10666        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10667        rc = 1;
10668        goto meta_command_exit;
10669      }else{
10670        zLike = z;
10671        bSeparate = 1;
10672        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10673      }
10674    }
10675    if( bSchema ){
10676      zSql = "SELECT lower(name) FROM sqlite_schema"
10677             " WHERE type='table' AND coalesce(rootpage,0)>1"
10678             " UNION ALL SELECT 'sqlite_schema'"
10679             " ORDER BY 1 collate nocase";
10680    }else{
10681      zSql = "SELECT lower(name) FROM sqlite_schema"
10682             " WHERE type='table' AND coalesce(rootpage,0)>1"
10683             " AND name NOT LIKE 'sqlite_%'"
10684             " ORDER BY 1 collate nocase";
10685    }
10686    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10687    initText(&sQuery);
10688    initText(&sSql);
10689    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10690    zSep = "VALUES(";
10691    while( SQLITE_ROW==sqlite3_step(pStmt) ){
10692      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10693      if( zTab==0 ) continue;
10694      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10695      if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
10696        appendText(&sQuery,"SELECT * FROM ", 0);
10697        appendText(&sQuery,zTab,'"');
10698        appendText(&sQuery," NOT INDEXED;", 0);
10699      }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
10700        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10701                           " ORDER BY name;", 0);
10702      }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
10703        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10704                           " ORDER BY name;", 0);
10705      }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
10706        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10707                           " ORDER BY tbl,idx;", 0);
10708      }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
10709        appendText(&sQuery, "SELECT * FROM ", 0);
10710        appendText(&sQuery, zTab, 0);
10711        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10712      }
10713      appendText(&sSql, zSep, 0);
10714      appendText(&sSql, sQuery.z, '\'');
10715      sQuery.n = 0;
10716      appendText(&sSql, ",", 0);
10717      appendText(&sSql, zTab, '\'');
10718      zSep = "),(";
10719    }
10720    sqlite3_finalize(pStmt);
10721    if( bSeparate ){
10722      zSql = sqlite3_mprintf(
10723          "%s))"
10724          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10725          "   FROM [sha3sum$query]",
10726          sSql.z, iSize);
10727    }else{
10728      zSql = sqlite3_mprintf(
10729          "%s))"
10730          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10731          "   FROM [sha3sum$query]",
10732          sSql.z, iSize);
10733    }
10734    shell_check_oom(zSql);
10735    freeText(&sQuery);
10736    freeText(&sSql);
10737    if( bDebug ){
10738      utf8_printf(p->out, "%s\n", zSql);
10739    }else{
10740      shell_exec(p, zSql, 0);
10741    }
10742    sqlite3_free(zSql);
10743  }else
10744
10745#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10746  if( c=='s'
10747   && (cli_strncmp(azArg[0], "shell", n)==0
10748       || cli_strncmp(azArg[0],"system",n)==0)
10749  ){
10750    char *zCmd;
10751    int i, x;
10752    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10753    if( nArg<2 ){
10754      raw_printf(stderr, "Usage: .system COMMAND\n");
10755      rc = 1;
10756      goto meta_command_exit;
10757    }
10758    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10759    for(i=2; i<nArg && zCmd!=0; i++){
10760      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10761                             zCmd, azArg[i]);
10762    }
10763    x = zCmd!=0 ? system(zCmd) : 1;
10764    sqlite3_free(zCmd);
10765    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10766  }else
10767#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10768
10769  if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
10770    static const char *azBool[] = { "off", "on", "trigger", "full"};
10771    const char *zOut;
10772    int i;
10773    if( nArg!=1 ){
10774      raw_printf(stderr, "Usage: .show\n");
10775      rc = 1;
10776      goto meta_command_exit;
10777    }
10778    utf8_printf(p->out, "%12.12s: %s\n","echo",
10779                azBool[ShellHasFlag(p, SHFLG_Echo)]);
10780    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10781    utf8_printf(p->out, "%12.12s: %s\n","explain",
10782         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10783    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10784    if( p->mode==MODE_Column
10785     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10786    ){
10787      utf8_printf
10788        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10789         modeDescr[p->mode], p->cmOpts.iWrap,
10790         p->cmOpts.bWordWrap ? "on" : "off",
10791         p->cmOpts.bQuote ? "" : "no");
10792    }else{
10793      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10794    }
10795    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10796      output_c_string(p->out, p->nullValue);
10797      raw_printf(p->out, "\n");
10798    utf8_printf(p->out,"%12.12s: %s\n","output",
10799            strlen30(p->outfile) ? p->outfile : "stdout");
10800    utf8_printf(p->out,"%12.12s: ", "colseparator");
10801      output_c_string(p->out, p->colSeparator);
10802      raw_printf(p->out, "\n");
10803    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10804      output_c_string(p->out, p->rowSeparator);
10805      raw_printf(p->out, "\n");
10806    switch( p->statsOn ){
10807      case 0:  zOut = "off";     break;
10808      default: zOut = "on";      break;
10809      case 2:  zOut = "stmt";    break;
10810      case 3:  zOut = "vmstep";  break;
10811    }
10812    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10813    utf8_printf(p->out, "%12.12s: ", "width");
10814    for (i=0;i<p->nWidth;i++) {
10815      raw_printf(p->out, "%d ", p->colWidth[i]);
10816    }
10817    raw_printf(p->out, "\n");
10818    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10819                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10820  }else
10821
10822  if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
10823    if( nArg==2 ){
10824      if( cli_strcmp(azArg[1],"stmt")==0 ){
10825        p->statsOn = 2;
10826      }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
10827        p->statsOn = 3;
10828      }else{
10829        p->statsOn = (u8)booleanValue(azArg[1]);
10830      }
10831    }else if( nArg==1 ){
10832      display_stats(p->db, p, 0);
10833    }else{
10834      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10835      rc = 1;
10836    }
10837  }else
10838
10839  if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
10840   || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
10841                 || cli_strncmp(azArg[0], "indexes", n)==0) )
10842  ){
10843    sqlite3_stmt *pStmt;
10844    char **azResult;
10845    int nRow, nAlloc;
10846    int ii;
10847    ShellText s;
10848    initText(&s);
10849    open_db(p, 0);
10850    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10851    if( rc ){
10852      sqlite3_finalize(pStmt);
10853      return shellDatabaseError(p->db);
10854    }
10855
10856    if( nArg>2 && c=='i' ){
10857      /* It is an historical accident that the .indexes command shows an error
10858      ** when called with the wrong number of arguments whereas the .tables
10859      ** command does not. */
10860      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10861      rc = 1;
10862      sqlite3_finalize(pStmt);
10863      goto meta_command_exit;
10864    }
10865    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10866      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10867      if( zDbName==0 ) continue;
10868      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10869      if( sqlite3_stricmp(zDbName, "main")==0 ){
10870        appendText(&s, "SELECT name FROM ", 0);
10871      }else{
10872        appendText(&s, "SELECT ", 0);
10873        appendText(&s, zDbName, '\'');
10874        appendText(&s, "||'.'||name FROM ", 0);
10875      }
10876      appendText(&s, zDbName, '"');
10877      appendText(&s, ".sqlite_schema ", 0);
10878      if( c=='t' ){
10879        appendText(&s," WHERE type IN ('table','view')"
10880                      "   AND name NOT LIKE 'sqlite_%'"
10881                      "   AND name LIKE ?1", 0);
10882      }else{
10883        appendText(&s," WHERE type='index'"
10884                      "   AND tbl_name LIKE ?1", 0);
10885      }
10886    }
10887    rc = sqlite3_finalize(pStmt);
10888    if( rc==SQLITE_OK ){
10889      appendText(&s, " ORDER BY 1", 0);
10890      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10891    }
10892    freeText(&s);
10893    if( rc ) return shellDatabaseError(p->db);
10894
10895    /* Run the SQL statement prepared by the above block. Store the results
10896    ** as an array of nul-terminated strings in azResult[].  */
10897    nRow = nAlloc = 0;
10898    azResult = 0;
10899    if( nArg>1 ){
10900      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10901    }else{
10902      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10903    }
10904    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10905      if( nRow>=nAlloc ){
10906        char **azNew;
10907        int n2 = nAlloc*2 + 10;
10908        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10909        shell_check_oom(azNew);
10910        nAlloc = n2;
10911        azResult = azNew;
10912      }
10913      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10914      shell_check_oom(azResult[nRow]);
10915      nRow++;
10916    }
10917    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10918      rc = shellDatabaseError(p->db);
10919    }
10920
10921    /* Pretty-print the contents of array azResult[] to the output */
10922    if( rc==0 && nRow>0 ){
10923      int len, maxlen = 0;
10924      int i, j;
10925      int nPrintCol, nPrintRow;
10926      for(i=0; i<nRow; i++){
10927        len = strlen30(azResult[i]);
10928        if( len>maxlen ) maxlen = len;
10929      }
10930      nPrintCol = 80/(maxlen+2);
10931      if( nPrintCol<1 ) nPrintCol = 1;
10932      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10933      for(i=0; i<nPrintRow; i++){
10934        for(j=i; j<nRow; j+=nPrintRow){
10935          char *zSp = j<nPrintRow ? "" : "  ";
10936          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10937                      azResult[j] ? azResult[j]:"");
10938        }
10939        raw_printf(p->out, "\n");
10940      }
10941    }
10942
10943    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10944    sqlite3_free(azResult);
10945  }else
10946
10947#ifndef SQLITE_SHELL_FIDDLE
10948  /* Begin redirecting output to the file "testcase-out.txt" */
10949  if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
10950    output_reset(p);
10951    p->out = output_file_open("testcase-out.txt", 0);
10952    if( p->out==0 ){
10953      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10954    }
10955    if( nArg>=2 ){
10956      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10957    }else{
10958      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10959    }
10960  }else
10961#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10962
10963#ifndef SQLITE_UNTESTABLE
10964  if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
10965    static const struct {
10966       const char *zCtrlName;   /* Name of a test-control option */
10967       int ctrlCode;            /* Integer code for that option */
10968       int unSafe;              /* Not valid for --safe mode */
10969       const char *zUsage;      /* Usage notes */
10970    } aCtrl[] = {
10971      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10972      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10973    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10974    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10975      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10976      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10977    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10978      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10979      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10980      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10981      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10982      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10983#ifdef YYCOVERAGE
10984      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10985#endif
10986      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10987      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10988      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10989      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10990      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10991      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10992      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10993    };
10994    int testctrl = -1;
10995    int iCtrl = -1;
10996    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10997    int isOk = 0;
10998    int i, n2;
10999    const char *zCmd = 0;
11000
11001    open_db(p, 0);
11002    zCmd = nArg>=2 ? azArg[1] : "help";
11003
11004    /* The argument can optionally begin with "-" or "--" */
11005    if( zCmd[0]=='-' && zCmd[1] ){
11006      zCmd++;
11007      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
11008    }
11009
11010    /* --help lists all test-controls */
11011    if( cli_strcmp(zCmd,"help")==0 ){
11012      utf8_printf(p->out, "Available test-controls:\n");
11013      for(i=0; i<ArraySize(aCtrl); i++){
11014        utf8_printf(p->out, "  .testctrl %s %s\n",
11015                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
11016      }
11017      rc = 1;
11018      goto meta_command_exit;
11019    }
11020
11021    /* convert testctrl text option to value. allow any unique prefix
11022    ** of the option name, or a numerical value. */
11023    n2 = strlen30(zCmd);
11024    for(i=0; i<ArraySize(aCtrl); i++){
11025      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
11026        if( testctrl<0 ){
11027          testctrl = aCtrl[i].ctrlCode;
11028          iCtrl = i;
11029        }else{
11030          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
11031                              "Use \".testctrl --help\" for help\n", zCmd);
11032          rc = 1;
11033          goto meta_command_exit;
11034        }
11035      }
11036    }
11037    if( testctrl<0 ){
11038      utf8_printf(stderr,"Error: unknown test-control: %s\n"
11039                         "Use \".testctrl --help\" for help\n", zCmd);
11040    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
11041      utf8_printf(stderr,
11042         "line %d: \".testctrl %s\" may not be used in safe mode\n",
11043         p->lineno, aCtrl[iCtrl].zCtrlName);
11044      exit(1);
11045    }else{
11046      switch(testctrl){
11047
11048        /* sqlite3_test_control(int, db, int) */
11049        case SQLITE_TESTCTRL_OPTIMIZATIONS:
11050          if( nArg==3 ){
11051            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
11052            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11053            isOk = 3;
11054          }
11055          break;
11056
11057        /* sqlite3_test_control(int) */
11058        case SQLITE_TESTCTRL_PRNG_SAVE:
11059        case SQLITE_TESTCTRL_PRNG_RESTORE:
11060        case SQLITE_TESTCTRL_BYTEORDER:
11061          if( nArg==2 ){
11062            rc2 = sqlite3_test_control(testctrl);
11063            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
11064          }
11065          break;
11066
11067        /* sqlite3_test_control(int, uint) */
11068        case SQLITE_TESTCTRL_PENDING_BYTE:
11069          if( nArg==3 ){
11070            unsigned int opt = (unsigned int)integerValue(azArg[2]);
11071            rc2 = sqlite3_test_control(testctrl, opt);
11072            isOk = 3;
11073          }
11074          break;
11075
11076        /* sqlite3_test_control(int, int, sqlite3*) */
11077        case SQLITE_TESTCTRL_PRNG_SEED:
11078          if( nArg==3 || nArg==4 ){
11079            int ii = (int)integerValue(azArg[2]);
11080            sqlite3 *db;
11081            if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
11082              sqlite3_randomness(sizeof(ii),&ii);
11083              printf("-- random seed: %d\n", ii);
11084            }
11085            if( nArg==3 ){
11086              db = 0;
11087            }else{
11088              db = p->db;
11089              /* Make sure the schema has been loaded */
11090              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
11091            }
11092            rc2 = sqlite3_test_control(testctrl, ii, db);
11093            isOk = 3;
11094          }
11095          break;
11096
11097        /* sqlite3_test_control(int, int) */
11098        case SQLITE_TESTCTRL_ASSERT:
11099        case SQLITE_TESTCTRL_ALWAYS:
11100          if( nArg==3 ){
11101            int opt = booleanValue(azArg[2]);
11102            rc2 = sqlite3_test_control(testctrl, opt);
11103            isOk = 1;
11104          }
11105          break;
11106
11107        /* sqlite3_test_control(int, int) */
11108        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
11109        case SQLITE_TESTCTRL_NEVER_CORRUPT:
11110          if( nArg==3 ){
11111            int opt = booleanValue(azArg[2]);
11112            rc2 = sqlite3_test_control(testctrl, opt);
11113            isOk = 3;
11114          }
11115          break;
11116
11117        /* sqlite3_test_control(sqlite3*) */
11118        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
11119          rc2 = sqlite3_test_control(testctrl, p->db);
11120          isOk = 3;
11121          break;
11122
11123        case SQLITE_TESTCTRL_IMPOSTER:
11124          if( nArg==5 ){
11125            rc2 = sqlite3_test_control(testctrl, p->db,
11126                          azArg[2],
11127                          integerValue(azArg[3]),
11128                          integerValue(azArg[4]));
11129            isOk = 3;
11130          }
11131          break;
11132
11133        case SQLITE_TESTCTRL_SEEK_COUNT: {
11134          u64 x = 0;
11135          rc2 = sqlite3_test_control(testctrl, p->db, &x);
11136          utf8_printf(p->out, "%llu\n", x);
11137          isOk = 3;
11138          break;
11139        }
11140
11141#ifdef YYCOVERAGE
11142        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
11143          if( nArg==2 ){
11144            sqlite3_test_control(testctrl, p->out);
11145            isOk = 3;
11146          }
11147          break;
11148        }
11149#endif
11150#ifdef SQLITE_DEBUG
11151        case SQLITE_TESTCTRL_TUNE: {
11152          if( nArg==4 ){
11153            int id = (int)integerValue(azArg[2]);
11154            int val = (int)integerValue(azArg[3]);
11155            sqlite3_test_control(testctrl, id, &val);
11156            isOk = 3;
11157          }else if( nArg==3 ){
11158            int id = (int)integerValue(azArg[2]);
11159            sqlite3_test_control(testctrl, -id, &rc2);
11160            isOk = 1;
11161          }else if( nArg==2 ){
11162            int id = 1;
11163            while(1){
11164              int val = 0;
11165              rc2 = sqlite3_test_control(testctrl, -id, &val);
11166              if( rc2!=SQLITE_OK ) break;
11167              if( id>1 ) utf8_printf(p->out, "  ");
11168              utf8_printf(p->out, "%d: %d", id, val);
11169              id++;
11170            }
11171            if( id>1 ) utf8_printf(p->out, "\n");
11172            isOk = 3;
11173          }
11174          break;
11175        }
11176#endif
11177        case SQLITE_TESTCTRL_SORTER_MMAP:
11178          if( nArg==3 ){
11179            int opt = (unsigned int)integerValue(azArg[2]);
11180            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11181            isOk = 3;
11182          }
11183          break;
11184      }
11185    }
11186    if( isOk==0 && iCtrl>=0 ){
11187      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11188      rc = 1;
11189    }else if( isOk==1 ){
11190      raw_printf(p->out, "%d\n", rc2);
11191    }else if( isOk==2 ){
11192      raw_printf(p->out, "0x%08x\n", rc2);
11193    }
11194  }else
11195#endif /* !defined(SQLITE_UNTESTABLE) */
11196
11197  if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
11198    open_db(p, 0);
11199    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11200  }else
11201
11202  if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
11203    if( nArg==2 ){
11204      enableTimer = booleanValue(azArg[1]);
11205      if( enableTimer && !HAS_TIMER ){
11206        raw_printf(stderr, "Error: timer not available on this system.\n");
11207        enableTimer = 0;
11208      }
11209    }else{
11210      raw_printf(stderr, "Usage: .timer on|off\n");
11211      rc = 1;
11212    }
11213  }else
11214
11215#ifndef SQLITE_OMIT_TRACE
11216  if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
11217    int mType = 0;
11218    int jj;
11219    open_db(p, 0);
11220    for(jj=1; jj<nArg; jj++){
11221      const char *z = azArg[jj];
11222      if( z[0]=='-' ){
11223        if( optionMatch(z, "expanded") ){
11224          p->eTraceType = SHELL_TRACE_EXPANDED;
11225        }
11226#ifdef SQLITE_ENABLE_NORMALIZE
11227        else if( optionMatch(z, "normalized") ){
11228          p->eTraceType = SHELL_TRACE_NORMALIZED;
11229        }
11230#endif
11231        else if( optionMatch(z, "plain") ){
11232          p->eTraceType = SHELL_TRACE_PLAIN;
11233        }
11234        else if( optionMatch(z, "profile") ){
11235          mType |= SQLITE_TRACE_PROFILE;
11236        }
11237        else if( optionMatch(z, "row") ){
11238          mType |= SQLITE_TRACE_ROW;
11239        }
11240        else if( optionMatch(z, "stmt") ){
11241          mType |= SQLITE_TRACE_STMT;
11242        }
11243        else if( optionMatch(z, "close") ){
11244          mType |= SQLITE_TRACE_CLOSE;
11245        }
11246        else {
11247          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
11248          rc = 1;
11249          goto meta_command_exit;
11250        }
11251      }else{
11252        output_file_close(p->traceOut);
11253        p->traceOut = output_file_open(azArg[1], 0);
11254      }
11255    }
11256    if( p->traceOut==0 ){
11257      sqlite3_trace_v2(p->db, 0, 0, 0);
11258    }else{
11259      if( mType==0 ) mType = SQLITE_TRACE_STMT;
11260      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11261    }
11262  }else
11263#endif /* !defined(SQLITE_OMIT_TRACE) */
11264
11265#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11266  if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
11267    int ii;
11268    int lenOpt;
11269    char *zOpt;
11270    if( nArg<2 ){
11271      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
11272      rc = 1;
11273      goto meta_command_exit;
11274    }
11275    open_db(p, 0);
11276    zOpt = azArg[1];
11277    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11278    lenOpt = (int)strlen(zOpt);
11279    if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11280      assert( azArg[nArg]==0 );
11281      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11282    }else{
11283      for(ii=1; ii<nArg; ii++){
11284        sqlite3_create_module(p->db, azArg[ii], 0, 0);
11285      }
11286    }
11287  }else
11288#endif
11289
11290#if SQLITE_USER_AUTHENTICATION
11291  if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
11292    if( nArg<2 ){
11293      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
11294      rc = 1;
11295      goto meta_command_exit;
11296    }
11297    open_db(p, 0);
11298    if( cli_strcmp(azArg[1],"login")==0 ){
11299      if( nArg!=4 ){
11300        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
11301        rc = 1;
11302        goto meta_command_exit;
11303      }
11304      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11305                                     strlen30(azArg[3]));
11306      if( rc ){
11307        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
11308        rc = 1;
11309      }
11310    }else if( cli_strcmp(azArg[1],"add")==0 ){
11311      if( nArg!=5 ){
11312        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
11313        rc = 1;
11314        goto meta_command_exit;
11315      }
11316      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11317                            booleanValue(azArg[4]));
11318      if( rc ){
11319        raw_printf(stderr, "User-Add failed: %d\n", rc);
11320        rc = 1;
11321      }
11322    }else if( cli_strcmp(azArg[1],"edit")==0 ){
11323      if( nArg!=5 ){
11324        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
11325        rc = 1;
11326        goto meta_command_exit;
11327      }
11328      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11329                              booleanValue(azArg[4]));
11330      if( rc ){
11331        raw_printf(stderr, "User-Edit failed: %d\n", rc);
11332        rc = 1;
11333      }
11334    }else if( cli_strcmp(azArg[1],"delete")==0 ){
11335      if( nArg!=3 ){
11336        raw_printf(stderr, "Usage: .user delete USER\n");
11337        rc = 1;
11338        goto meta_command_exit;
11339      }
11340      rc = sqlite3_user_delete(p->db, azArg[2]);
11341      if( rc ){
11342        raw_printf(stderr, "User-Delete failed: %d\n", rc);
11343        rc = 1;
11344      }
11345    }else{
11346      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
11347      rc = 1;
11348      goto meta_command_exit;
11349    }
11350  }else
11351#endif /* SQLITE_USER_AUTHENTICATION */
11352
11353  if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
11354    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
11355        sqlite3_libversion(), sqlite3_sourceid());
11356#if SQLITE_HAVE_ZLIB
11357    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
11358#endif
11359#define CTIMEOPT_VAL_(opt) #opt
11360#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11361#if defined(__clang__) && defined(__clang_major__)
11362    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
11363                    CTIMEOPT_VAL(__clang_minor__) "."
11364                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
11365#elif defined(_MSC_VER)
11366    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
11367#elif defined(__GNUC__) && defined(__VERSION__)
11368    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
11369#endif
11370  }else
11371
11372  if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
11373    const char *zDbName = nArg==2 ? azArg[1] : "main";
11374    sqlite3_vfs *pVfs = 0;
11375    if( p->db ){
11376      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11377      if( pVfs ){
11378        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
11379        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11380        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11381        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11382      }
11383    }
11384  }else
11385
11386  if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
11387    sqlite3_vfs *pVfs;
11388    sqlite3_vfs *pCurrent = 0;
11389    if( p->db ){
11390      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11391    }
11392    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11393      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
11394           pVfs==pCurrent ? "  <--- CURRENT" : "");
11395      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11396      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11397      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11398      if( pVfs->pNext ){
11399        raw_printf(p->out, "-----------------------------------\n");
11400      }
11401    }
11402  }else
11403
11404  if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
11405    const char *zDbName = nArg==2 ? azArg[1] : "main";
11406    char *zVfsName = 0;
11407    if( p->db ){
11408      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11409      if( zVfsName ){
11410        utf8_printf(p->out, "%s\n", zVfsName);
11411        sqlite3_free(zVfsName);
11412      }
11413    }
11414  }else
11415
11416  if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
11417    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11418    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11419  }else
11420
11421  if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
11422    int j;
11423    assert( nArg<=ArraySize(azArg) );
11424    p->nWidth = nArg-1;
11425    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11426    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11427    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11428    for(j=1; j<nArg; j++){
11429      p->colWidth[j-1] = (int)integerValue(azArg[j]);
11430    }
11431  }else
11432
11433  {
11434    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
11435      " \"%s\". Enter \".help\" for help\n", azArg[0]);
11436    rc = 1;
11437  }
11438
11439meta_command_exit:
11440  if( p->outCount ){
11441    p->outCount--;
11442    if( p->outCount==0 ) output_reset(p);
11443  }
11444  p->bSafeMode = p->bSafeModePersist;
11445  return rc;
11446}
11447
11448/* Line scan result and intermediate states (supporting scan resumption)
11449*/
11450#ifndef CHAR_BIT
11451# define CHAR_BIT 8
11452#endif
11453typedef enum {
11454  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11455  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11456  QSS_Start = 0
11457} QuickScanState;
11458#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11459#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11460#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11461#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11462#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11463
11464/*
11465** Scan line for classification to guide shell's handling.
11466** The scan is resumable for subsequent lines when prior
11467** return values are passed as the 2nd argument.
11468*/
11469static QuickScanState quickscan(char *zLine, QuickScanState qss){
11470  char cin;
11471  char cWait = (char)qss; /* intentional narrowing loss */
11472  if( cWait==0 ){
11473  PlainScan:
11474    assert( cWait==0 );
11475    while( (cin = *zLine++)!=0 ){
11476      if( IsSpace(cin) )
11477        continue;
11478      switch (cin){
11479      case '-':
11480        if( *zLine!='-' )
11481          break;
11482        while((cin = *++zLine)!=0 )
11483          if( cin=='\n')
11484            goto PlainScan;
11485        return qss;
11486      case ';':
11487        qss |= QSS_EndingSemi;
11488        continue;
11489      case '/':
11490        if( *zLine=='*' ){
11491          ++zLine;
11492          cWait = '*';
11493          qss = QSS_SETV(qss, cWait);
11494          goto TermScan;
11495        }
11496        break;
11497      case '[':
11498        cin = ']';
11499        /* fall thru */
11500      case '`': case '\'': case '"':
11501        cWait = cin;
11502        qss = QSS_HasDark | cWait;
11503        goto TermScan;
11504      default:
11505        break;
11506      }
11507      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11508    }
11509  }else{
11510  TermScan:
11511    while( (cin = *zLine++)!=0 ){
11512      if( cin==cWait ){
11513        switch( cWait ){
11514        case '*':
11515          if( *zLine != '/' )
11516            continue;
11517          ++zLine;
11518          cWait = 0;
11519          qss = QSS_SETV(qss, 0);
11520          goto PlainScan;
11521        case '`': case '\'': case '"':
11522          if(*zLine==cWait){
11523            ++zLine;
11524            continue;
11525          }
11526          /* fall thru */
11527        case ']':
11528          cWait = 0;
11529          qss = QSS_SETV(qss, 0);
11530          goto PlainScan;
11531        default: assert(0);
11532        }
11533      }
11534    }
11535  }
11536  return qss;
11537}
11538
11539/*
11540** Return TRUE if the line typed in is an SQL command terminator other
11541** than a semi-colon.  The SQL Server style "go" command is understood
11542** as is the Oracle "/".
11543*/
11544static int line_is_command_terminator(char *zLine){
11545  while( IsSpace(zLine[0]) ){ zLine++; };
11546  if( zLine[0]=='/' )
11547    zLine += 1; /* Oracle */
11548  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
11549    zLine += 2; /* SQL Server */
11550  else
11551    return 0;
11552  return quickscan(zLine, QSS_Start)==QSS_Start;
11553}
11554
11555/*
11556** We need a default sqlite3_complete() implementation to use in case
11557** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
11558** any arbitrary text is a complete SQL statement.  This is not very
11559** user-friendly, but it does seem to work.
11560*/
11561#ifdef SQLITE_OMIT_COMPLETE
11562#define sqlite3_complete(x) 1
11563#endif
11564
11565/*
11566** Return true if zSql is a complete SQL statement.  Return false if it
11567** ends in the middle of a string literal or C-style comment.
11568*/
11569static int line_is_complete(char *zSql, int nSql){
11570  int rc;
11571  if( zSql==0 ) return 1;
11572  zSql[nSql] = ';';
11573  zSql[nSql+1] = 0;
11574  rc = sqlite3_complete(zSql);
11575  zSql[nSql] = 0;
11576  return rc;
11577}
11578
11579/*
11580** Run a single line of SQL.  Return the number of errors.
11581*/
11582static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
11583  int rc;
11584  char *zErrMsg = 0;
11585
11586  open_db(p, 0);
11587  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
11588  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11589  BEGIN_TIMER;
11590  rc = shell_exec(p, zSql, &zErrMsg);
11591  END_TIMER;
11592  if( rc || zErrMsg ){
11593    char zPrefix[100];
11594    const char *zErrorTail;
11595    const char *zErrorType;
11596    if( zErrMsg==0 ){
11597      zErrorType = "Error";
11598      zErrorTail = sqlite3_errmsg(p->db);
11599    }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
11600      zErrorType = "Parse error";
11601      zErrorTail = &zErrMsg[12];
11602    }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
11603      zErrorType = "Runtime error";
11604      zErrorTail = &zErrMsg[10];
11605    }else{
11606      zErrorType = "Error";
11607      zErrorTail = zErrMsg;
11608    }
11609    if( in!=0 || !stdin_is_interactive ){
11610      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11611                       "%s near line %d:", zErrorType, startline);
11612    }else{
11613      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11614    }
11615    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11616    sqlite3_free(zErrMsg);
11617    zErrMsg = 0;
11618    return 1;
11619  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11620    char zLineBuf[2000];
11621    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11622            "changes: %lld   total_changes: %lld",
11623            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11624    raw_printf(p->out, "%s\n", zLineBuf);
11625  }
11626  return 0;
11627}
11628
11629static void echo_group_input(ShellState *p, const char *zDo){
11630  if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11631}
11632
11633#ifdef SQLITE_SHELL_FIDDLE
11634/*
11635** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
11636** because we need the global shellState and cannot access it from that function
11637** without moving lots of code around (creating a larger/messier diff).
11638*/
11639static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11640  /* Parse the next line from shellState.wasm.zInput. */
11641  const char *zBegin = shellState.wasm.zPos;
11642  const char *z = zBegin;
11643  char *zLine = 0;
11644  i64 nZ = 0;
11645
11646  UNUSED_PARAMETER(in);
11647  UNUSED_PARAMETER(isContinuation);
11648  if(!z || !*z){
11649    return 0;
11650  }
11651  while(*z && isspace(*z)) ++z;
11652  zBegin = z;
11653  for(; *z && '\n'!=*z; ++nZ, ++z){}
11654  if(nZ>0 && '\r'==zBegin[nZ-1]){
11655    --nZ;
11656  }
11657  shellState.wasm.zPos = z;
11658  zLine = realloc(zPrior, nZ+1);
11659  shell_check_oom(zLine);
11660  memcpy(zLine, zBegin, nZ);
11661  zLine[nZ] = 0;
11662  return zLine;
11663}
11664#endif /* SQLITE_SHELL_FIDDLE */
11665
11666/*
11667** Read input from *in and process it.  If *in==0 then input
11668** is interactive - the user is typing it it.  Otherwise, input
11669** is coming from a file or device.  A prompt is issued and history
11670** is saved only if input is interactive.  An interrupt signal will
11671** cause this routine to exit immediately, unless input is interactive.
11672**
11673** Return the number of errors.
11674*/
11675static int process_input(ShellState *p){
11676  char *zLine = 0;          /* A single input line */
11677  char *zSql = 0;           /* Accumulated SQL text */
11678  i64 nLine;                /* Length of current line */
11679  i64 nSql = 0;             /* Bytes of zSql[] used */
11680  i64 nAlloc = 0;           /* Allocated zSql[] space */
11681  int rc;                   /* Error code */
11682  int errCnt = 0;           /* Number of errors seen */
11683  i64 startline = 0;        /* Line number for start of current input */
11684  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11685
11686  if( p->inputNesting==MAX_INPUT_NESTING ){
11687    /* This will be more informative in a later version. */
11688    utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11689                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11690    return 1;
11691  }
11692  ++p->inputNesting;
11693  p->lineno = 0;
11694  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11695    fflush(p->out);
11696    zLine = one_input_line(p->in, zLine, nSql>0);
11697    if( zLine==0 ){
11698      /* End of input */
11699      if( p->in==0 && stdin_is_interactive ) printf("\n");
11700      break;
11701    }
11702    if( seenInterrupt ){
11703      if( p->in!=0 ) break;
11704      seenInterrupt = 0;
11705    }
11706    p->lineno++;
11707    if( QSS_INPLAIN(qss)
11708        && line_is_command_terminator(zLine)
11709        && line_is_complete(zSql, nSql) ){
11710      memcpy(zLine,";",2);
11711    }
11712    qss = quickscan(zLine, qss);
11713    if( QSS_PLAINWHITE(qss) && nSql==0 ){
11714      /* Just swallow single-line whitespace */
11715      echo_group_input(p, zLine);
11716      qss = QSS_Start;
11717      continue;
11718    }
11719    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11720      echo_group_input(p, zLine);
11721      if( zLine[0]=='.' ){
11722        rc = do_meta_command(zLine, p);
11723        if( rc==2 ){ /* exit requested */
11724          break;
11725        }else if( rc ){
11726          errCnt++;
11727        }
11728      }
11729      qss = QSS_Start;
11730      continue;
11731    }
11732    /* No single-line dispositions remain; accumulate line(s). */
11733    nLine = strlen(zLine);
11734    if( nSql+nLine+2>=nAlloc ){
11735      /* Grow buffer by half-again increments when big. */
11736      nAlloc = nSql+(nSql>>1)+nLine+100;
11737      zSql = realloc(zSql, nAlloc);
11738      shell_check_oom(zSql);
11739    }
11740    if( nSql==0 ){
11741      i64 i;
11742      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11743      assert( nAlloc>0 && zSql!=0 );
11744      memcpy(zSql, zLine+i, nLine+1-i);
11745      startline = p->lineno;
11746      nSql = nLine-i;
11747    }else{
11748      zSql[nSql++] = '\n';
11749      memcpy(zSql+nSql, zLine, nLine+1);
11750      nSql += nLine;
11751    }
11752    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11753      echo_group_input(p, zSql);
11754      errCnt += runOneSqlLine(p, zSql, p->in, startline);
11755      nSql = 0;
11756      if( p->outCount ){
11757        output_reset(p);
11758        p->outCount = 0;
11759      }else{
11760        clearTempFile(p);
11761      }
11762      p->bSafeMode = p->bSafeModePersist;
11763      qss = QSS_Start;
11764    }else if( nSql && QSS_PLAINWHITE(qss) ){
11765      echo_group_input(p, zSql);
11766      nSql = 0;
11767      qss = QSS_Start;
11768    }
11769  }
11770  if( nSql ){
11771    /* This may be incomplete. Let the SQL parser deal with that. */
11772    echo_group_input(p, zSql);
11773    errCnt += runOneSqlLine(p, zSql, p->in, startline);
11774  }
11775  free(zSql);
11776  free(zLine);
11777  --p->inputNesting;
11778  return errCnt>0;
11779}
11780
11781/*
11782** Return a pathname which is the user's home directory.  A
11783** 0 return indicates an error of some kind.
11784*/
11785static char *find_home_dir(int clearFlag){
11786  static char *home_dir = NULL;
11787  if( clearFlag ){
11788    free(home_dir);
11789    home_dir = 0;
11790    return 0;
11791  }
11792  if( home_dir ) return home_dir;
11793
11794#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11795     && !defined(__RTP__) && !defined(_WRS_KERNEL)
11796  {
11797    struct passwd *pwent;
11798    uid_t uid = getuid();
11799    if( (pwent=getpwuid(uid)) != NULL) {
11800      home_dir = pwent->pw_dir;
11801    }
11802  }
11803#endif
11804
11805#if defined(_WIN32_WCE)
11806  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11807   */
11808  home_dir = "/";
11809#else
11810
11811#if defined(_WIN32) || defined(WIN32)
11812  if (!home_dir) {
11813    home_dir = getenv("USERPROFILE");
11814  }
11815#endif
11816
11817  if (!home_dir) {
11818    home_dir = getenv("HOME");
11819  }
11820
11821#if defined(_WIN32) || defined(WIN32)
11822  if (!home_dir) {
11823    char *zDrive, *zPath;
11824    int n;
11825    zDrive = getenv("HOMEDRIVE");
11826    zPath = getenv("HOMEPATH");
11827    if( zDrive && zPath ){
11828      n = strlen30(zDrive) + strlen30(zPath) + 1;
11829      home_dir = malloc( n );
11830      if( home_dir==0 ) return 0;
11831      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11832      return home_dir;
11833    }
11834    home_dir = "c:\\";
11835  }
11836#endif
11837
11838#endif /* !_WIN32_WCE */
11839
11840  if( home_dir ){
11841    i64 n = strlen(home_dir) + 1;
11842    char *z = malloc( n );
11843    if( z ) memcpy(z, home_dir, n);
11844    home_dir = z;
11845  }
11846
11847  return home_dir;
11848}
11849
11850/*
11851** Read input from the file given by sqliterc_override.  Or if that
11852** parameter is NULL, take input from ~/.sqliterc
11853**
11854** Returns the number of errors.
11855*/
11856static void process_sqliterc(
11857  ShellState *p,                  /* Configuration data */
11858  const char *sqliterc_override   /* Name of config file. NULL to use default */
11859){
11860  char *home_dir = NULL;
11861  const char *sqliterc = sqliterc_override;
11862  char *zBuf = 0;
11863  FILE *inSaved = p->in;
11864  int savedLineno = p->lineno;
11865
11866  if (sqliterc == NULL) {
11867    home_dir = find_home_dir(0);
11868    if( home_dir==0 ){
11869      raw_printf(stderr, "-- warning: cannot find home directory;"
11870                      " cannot read ~/.sqliterc\n");
11871      return;
11872    }
11873    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11874    shell_check_oom(zBuf);
11875    sqliterc = zBuf;
11876  }
11877  p->in = fopen(sqliterc,"rb");
11878  if( p->in ){
11879    if( stdin_is_interactive ){
11880      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11881    }
11882    if( process_input(p) && bail_on_error ) exit(1);
11883    fclose(p->in);
11884  }else if( sqliterc_override!=0 ){
11885    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11886    if( bail_on_error ) exit(1);
11887  }
11888  p->in = inSaved;
11889  p->lineno = savedLineno;
11890  sqlite3_free(zBuf);
11891}
11892
11893/*
11894** Show available command line options
11895*/
11896static const char zOptions[] =
11897#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11898  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11899#endif
11900  "   -append              append the database to the end of the file\n"
11901  "   -ascii               set output mode to 'ascii'\n"
11902  "   -bail                stop after hitting an error\n"
11903  "   -batch               force batch I/O\n"
11904  "   -box                 set output mode to 'box'\n"
11905  "   -column              set output mode to 'column'\n"
11906  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11907  "   -csv                 set output mode to 'csv'\n"
11908#if !defined(SQLITE_OMIT_DESERIALIZE)
11909  "   -deserialize         open the database using sqlite3_deserialize()\n"
11910#endif
11911  "   -echo                print inputs before execution\n"
11912  "   -init FILENAME       read/process named file\n"
11913  "   -[no]header          turn headers on or off\n"
11914#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11915  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11916#endif
11917  "   -help                show this message\n"
11918  "   -html                set output mode to HTML\n"
11919  "   -interactive         force interactive I/O\n"
11920  "   -json                set output mode to 'json'\n"
11921  "   -line                set output mode to 'line'\n"
11922  "   -list                set output mode to 'list'\n"
11923  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11924  "   -markdown            set output mode to 'markdown'\n"
11925#if !defined(SQLITE_OMIT_DESERIALIZE)
11926  "   -maxsize N           maximum size for a --deserialize database\n"
11927#endif
11928  "   -memtrace            trace all memory allocations and deallocations\n"
11929  "   -mmap N              default mmap size set to N\n"
11930#ifdef SQLITE_ENABLE_MULTIPLEX
11931  "   -multiplex           enable the multiplexor VFS\n"
11932#endif
11933  "   -newline SEP         set output row separator. Default: '\\n'\n"
11934  "   -nofollow            refuse to open symbolic links to database files\n"
11935  "   -nonce STRING        set the safe-mode escape nonce\n"
11936  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11937  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11938  "   -quote               set output mode to 'quote'\n"
11939  "   -readonly            open the database read-only\n"
11940  "   -safe                enable safe-mode\n"
11941  "   -separator SEP       set output column separator. Default: '|'\n"
11942#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11943  "   -sorterref SIZE      sorter references threshold size\n"
11944#endif
11945  "   -stats               print memory stats before each finalize\n"
11946  "   -table               set output mode to 'table'\n"
11947  "   -tabs                set output mode to 'tabs'\n"
11948  "   -version             show SQLite version\n"
11949  "   -vfs NAME            use NAME as the default VFS\n"
11950#ifdef SQLITE_ENABLE_VFSTRACE
11951  "   -vfstrace            enable tracing of all VFS calls\n"
11952#endif
11953#ifdef SQLITE_HAVE_ZLIB
11954  "   -zip                 open the file as a ZIP Archive\n"
11955#endif
11956;
11957static void usage(int showDetail){
11958  utf8_printf(stderr,
11959      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11960      "FILENAME is the name of an SQLite database. A new database is created\n"
11961      "if the file does not previously exist.\n", Argv0);
11962  if( showDetail ){
11963    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11964  }else{
11965    raw_printf(stderr, "Use the -help option for additional information\n");
11966  }
11967  exit(1);
11968}
11969
11970/*
11971** Internal check:  Verify that the SQLite is uninitialized.  Print a
11972** error message if it is initialized.
11973*/
11974static void verify_uninitialized(void){
11975  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11976    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11977                        " initialization.\n");
11978  }
11979}
11980
11981/*
11982** Initialize the state information in data
11983*/
11984static void main_init(ShellState *data) {
11985  memset(data, 0, sizeof(*data));
11986  data->normalMode = data->cMode = data->mode = MODE_List;
11987  data->autoExplain = 1;
11988  data->pAuxDb = &data->aAuxDb[0];
11989  memcpy(data->colSeparator,SEP_Column, 2);
11990  memcpy(data->rowSeparator,SEP_Row, 2);
11991  data->showHeader = 0;
11992  data->shellFlgs = SHFLG_Lookaside;
11993  verify_uninitialized();
11994  sqlite3_config(SQLITE_CONFIG_URI, 1);
11995  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11996  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11997  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11998  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11999}
12000
12001/*
12002** Output text to the console in a font that attracts extra attention.
12003*/
12004#ifdef _WIN32
12005static void printBold(const char *zText){
12006#if !SQLITE_OS_WINRT
12007  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
12008  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
12009  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
12010  SetConsoleTextAttribute(out,
12011         FOREGROUND_RED|FOREGROUND_INTENSITY
12012  );
12013#endif
12014  printf("%s", zText);
12015#if !SQLITE_OS_WINRT
12016  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
12017#endif
12018}
12019#else
12020static void printBold(const char *zText){
12021  printf("\033[1m%s\033[0m", zText);
12022}
12023#endif
12024
12025/*
12026** Get the argument to an --option.  Throw an error and die if no argument
12027** is available.
12028*/
12029static char *cmdline_option_value(int argc, char **argv, int i){
12030  if( i==argc ){
12031    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
12032            argv[0], argv[argc-1]);
12033    exit(1);
12034  }
12035  return argv[i];
12036}
12037
12038#ifndef SQLITE_SHELL_IS_UTF8
12039#  if (defined(_WIN32) || defined(WIN32)) \
12040   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12041#    define SQLITE_SHELL_IS_UTF8          (0)
12042#  else
12043#    define SQLITE_SHELL_IS_UTF8          (1)
12044#  endif
12045#endif
12046
12047#ifdef SQLITE_SHELL_FIDDLE
12048#  define main fiddle_main
12049#endif
12050
12051#if SQLITE_SHELL_IS_UTF8
12052int SQLITE_CDECL main(int argc, char **argv){
12053#else
12054int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
12055  char **argv;
12056#endif
12057#ifdef SQLITE_DEBUG
12058  sqlite3_int64 mem_main_enter = sqlite3_memory_used();
12059#endif
12060  char *zErrMsg = 0;
12061#ifdef SQLITE_SHELL_FIDDLE
12062#  define data shellState
12063#else
12064  ShellState data;
12065#endif
12066  const char *zInitFile = 0;
12067  int i;
12068  int rc = 0;
12069  int warnInmemoryDb = 0;
12070  int readStdin = 1;
12071  int nCmd = 0;
12072  char **azCmd = 0;
12073  const char *zVfs = 0;           /* Value of -vfs command-line option */
12074#if !SQLITE_SHELL_IS_UTF8
12075  char **argvToFree = 0;
12076  int argcToFree = 0;
12077#endif
12078
12079  setBinaryMode(stdin, 0);
12080  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12081#ifdef SQLITE_SHELL_FIDDLE
12082  stdin_is_interactive = 0;
12083  stdout_is_console = 1;
12084  data.wasm.zDefaultDbName = "/fiddle.sqlite3";
12085#else
12086  stdin_is_interactive = isatty(0);
12087  stdout_is_console = isatty(1);
12088#endif
12089
12090#if !defined(_WIN32_WCE)
12091  if( getenv("SQLITE_DEBUG_BREAK") ){
12092    if( isatty(0) && isatty(2) ){
12093      fprintf(stderr,
12094          "attach debugger to process %d and press any key to continue.\n",
12095          GETPID());
12096      fgetc(stdin);
12097    }else{
12098#if defined(_WIN32) || defined(WIN32)
12099#if SQLITE_OS_WINRT
12100      __debugbreak();
12101#else
12102      DebugBreak();
12103#endif
12104#elif defined(SIGTRAP)
12105      raise(SIGTRAP);
12106#endif
12107    }
12108  }
12109#endif
12110
12111#if USE_SYSTEM_SQLITE+0!=1
12112  if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12113    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
12114            sqlite3_sourceid(), SQLITE_SOURCE_ID);
12115    exit(1);
12116  }
12117#endif
12118  main_init(&data);
12119
12120  /* On Windows, we must translate command-line arguments into UTF-8.
12121  ** The SQLite memory allocator subsystem has to be enabled in order to
12122  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
12123  ** subsequent sqlite3_config() calls will work.  So copy all results into
12124  ** memory that does not come from the SQLite memory allocator.
12125  */
12126#if !SQLITE_SHELL_IS_UTF8
12127  sqlite3_initialize();
12128  argvToFree = malloc(sizeof(argv[0])*argc*2);
12129  shell_check_oom(argvToFree);
12130  argcToFree = argc;
12131  argv = argvToFree + argc;
12132  for(i=0; i<argc; i++){
12133    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12134    i64 n;
12135    shell_check_oom(z);
12136    n = strlen(z);
12137    argv[i] = malloc( n+1 );
12138    shell_check_oom(argv[i]);
12139    memcpy(argv[i], z, n+1);
12140    argvToFree[i] = argv[i];
12141    sqlite3_free(z);
12142  }
12143  sqlite3_shutdown();
12144#endif
12145
12146  assert( argc>=1 && argv && argv[0] );
12147  Argv0 = argv[0];
12148
12149  /* Make sure we have a valid signal handler early, before anything
12150  ** else is done.
12151  */
12152#ifdef SIGINT
12153  signal(SIGINT, interrupt_handler);
12154#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12155  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
12156#endif
12157
12158#ifdef SQLITE_SHELL_DBNAME_PROC
12159  {
12160    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12161    ** of a C-function that will provide the name of the database file.  Use
12162    ** this compile-time option to embed this shell program in larger
12163    ** applications. */
12164    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12165    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12166    warnInmemoryDb = 0;
12167  }
12168#endif
12169
12170  /* Do an initial pass through the command-line argument to locate
12171  ** the name of the database file, the name of the initialization file,
12172  ** the size of the alternative malloc heap,
12173  ** and the first command to execute.
12174  */
12175  verify_uninitialized();
12176  for(i=1; i<argc; i++){
12177    char *z;
12178    z = argv[i];
12179    if( z[0]!='-' ){
12180      if( data.aAuxDb->zDbFilename==0 ){
12181        data.aAuxDb->zDbFilename = z;
12182      }else{
12183        /* Excesss arguments are interpreted as SQL (or dot-commands) and
12184        ** mean that nothing is read from stdin */
12185        readStdin = 0;
12186        nCmd++;
12187        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12188        shell_check_oom(azCmd);
12189        azCmd[nCmd-1] = z;
12190      }
12191    }
12192    if( z[1]=='-' ) z++;
12193    if( cli_strcmp(z,"-separator")==0
12194     || cli_strcmp(z,"-nullvalue")==0
12195     || cli_strcmp(z,"-newline")==0
12196     || cli_strcmp(z,"-cmd")==0
12197    ){
12198      (void)cmdline_option_value(argc, argv, ++i);
12199    }else if( cli_strcmp(z,"-init")==0 ){
12200      zInitFile = cmdline_option_value(argc, argv, ++i);
12201    }else if( cli_strcmp(z,"-batch")==0 ){
12202      /* Need to check for batch mode here to so we can avoid printing
12203      ** informational messages (like from process_sqliterc) before
12204      ** we do the actual processing of arguments later in a second pass.
12205      */
12206      stdin_is_interactive = 0;
12207    }else if( cli_strcmp(z,"-heap")==0 ){
12208#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12209      const char *zSize;
12210      sqlite3_int64 szHeap;
12211
12212      zSize = cmdline_option_value(argc, argv, ++i);
12213      szHeap = integerValue(zSize);
12214      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12215      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12216#else
12217      (void)cmdline_option_value(argc, argv, ++i);
12218#endif
12219    }else if( cli_strcmp(z,"-pagecache")==0 ){
12220      sqlite3_int64 n, sz;
12221      sz = integerValue(cmdline_option_value(argc,argv,++i));
12222      if( sz>70000 ) sz = 70000;
12223      if( sz<0 ) sz = 0;
12224      n = integerValue(cmdline_option_value(argc,argv,++i));
12225      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12226        n = 0xffffffffffffLL/sz;
12227      }
12228      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12229                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12230      data.shellFlgs |= SHFLG_Pagecache;
12231    }else if( cli_strcmp(z,"-lookaside")==0 ){
12232      int n, sz;
12233      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12234      if( sz<0 ) sz = 0;
12235      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12236      if( n<0 ) n = 0;
12237      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12238      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12239    }else if( cli_strcmp(z,"-threadsafe")==0 ){
12240      int n;
12241      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12242      switch( n ){
12243         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
12244         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
12245         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
12246      }
12247#ifdef SQLITE_ENABLE_VFSTRACE
12248    }else if( cli_strcmp(z,"-vfstrace")==0 ){
12249      extern int vfstrace_register(
12250         const char *zTraceName,
12251         const char *zOldVfsName,
12252         int (*xOut)(const char*,void*),
12253         void *pOutArg,
12254         int makeDefault
12255      );
12256      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
12257#endif
12258#ifdef SQLITE_ENABLE_MULTIPLEX
12259    }else if( cli_strcmp(z,"-multiplex")==0 ){
12260      extern int sqlite3_multiple_initialize(const char*,int);
12261      sqlite3_multiplex_initialize(0, 1);
12262#endif
12263    }else if( cli_strcmp(z,"-mmap")==0 ){
12264      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12265      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12266#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12267    }else if( cli_strcmp(z,"-sorterref")==0 ){
12268      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12269      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12270#endif
12271    }else if( cli_strcmp(z,"-vfs")==0 ){
12272      zVfs = cmdline_option_value(argc, argv, ++i);
12273#ifdef SQLITE_HAVE_ZLIB
12274    }else if( cli_strcmp(z,"-zip")==0 ){
12275      data.openMode = SHELL_OPEN_ZIPFILE;
12276#endif
12277    }else if( cli_strcmp(z,"-append")==0 ){
12278      data.openMode = SHELL_OPEN_APPENDVFS;
12279#ifndef SQLITE_OMIT_DESERIALIZE
12280    }else if( cli_strcmp(z,"-deserialize")==0 ){
12281      data.openMode = SHELL_OPEN_DESERIALIZE;
12282    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12283      data.szMax = integerValue(argv[++i]);
12284#endif
12285    }else if( cli_strcmp(z,"-readonly")==0 ){
12286      data.openMode = SHELL_OPEN_READONLY;
12287    }else if( cli_strcmp(z,"-nofollow")==0 ){
12288      data.openFlags = SQLITE_OPEN_NOFOLLOW;
12289#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12290    }else if( cli_strncmp(z, "-A",2)==0 ){
12291      /* All remaining command-line arguments are passed to the ".archive"
12292      ** command, so ignore them */
12293      break;
12294#endif
12295    }else if( cli_strcmp(z, "-memtrace")==0 ){
12296      sqlite3MemTraceActivate(stderr);
12297    }else if( cli_strcmp(z,"-bail")==0 ){
12298      bail_on_error = 1;
12299    }else if( cli_strcmp(z,"-nonce")==0 ){
12300      free(data.zNonce);
12301      data.zNonce = strdup(argv[++i]);
12302    }else if( cli_strcmp(z,"-safe")==0 ){
12303      /* no-op - catch this on the second pass */
12304    }
12305  }
12306  verify_uninitialized();
12307
12308
12309#ifdef SQLITE_SHELL_INIT_PROC
12310  {
12311    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12312    ** of a C-function that will perform initialization actions on SQLite that
12313    ** occur just before or after sqlite3_initialize(). Use this compile-time
12314    ** option to embed this shell program in larger applications. */
12315    extern void SQLITE_SHELL_INIT_PROC(void);
12316    SQLITE_SHELL_INIT_PROC();
12317  }
12318#else
12319  /* All the sqlite3_config() calls have now been made. So it is safe
12320  ** to call sqlite3_initialize() and process any command line -vfs option. */
12321  sqlite3_initialize();
12322#endif
12323
12324  if( zVfs ){
12325    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12326    if( pVfs ){
12327      sqlite3_vfs_register(pVfs, 1);
12328    }else{
12329      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
12330      exit(1);
12331    }
12332  }
12333
12334  if( data.pAuxDb->zDbFilename==0 ){
12335#ifndef SQLITE_OMIT_MEMORYDB
12336    data.pAuxDb->zDbFilename = ":memory:";
12337    warnInmemoryDb = argc==1;
12338#else
12339    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
12340    return 1;
12341#endif
12342  }
12343  data.out = stdout;
12344#ifndef SQLITE_SHELL_FIDDLE
12345  sqlite3_appendvfs_init(0,0,0);
12346#endif
12347
12348  /* Go ahead and open the database file if it already exists.  If the
12349  ** file does not exist, delay opening it.  This prevents empty database
12350  ** files from being created if a user mistypes the database name argument
12351  ** to the sqlite command-line tool.
12352  */
12353  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
12354    open_db(&data, 0);
12355  }
12356
12357  /* Process the initialization file if there is one.  If no -init option
12358  ** is given on the command line, look for a file named ~/.sqliterc and
12359  ** try to process it.
12360  */
12361  process_sqliterc(&data,zInitFile);
12362
12363  /* Make a second pass through the command-line argument and set
12364  ** options.  This second pass is delayed until after the initialization
12365  ** file is processed so that the command-line arguments will override
12366  ** settings in the initialization file.
12367  */
12368  for(i=1; i<argc; i++){
12369    char *z = argv[i];
12370    if( z[0]!='-' ) continue;
12371    if( z[1]=='-' ){ z++; }
12372    if( cli_strcmp(z,"-init")==0 ){
12373      i++;
12374    }else if( cli_strcmp(z,"-html")==0 ){
12375      data.mode = MODE_Html;
12376    }else if( cli_strcmp(z,"-list")==0 ){
12377      data.mode = MODE_List;
12378    }else if( cli_strcmp(z,"-quote")==0 ){
12379      data.mode = MODE_Quote;
12380      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
12381      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12382    }else if( cli_strcmp(z,"-line")==0 ){
12383      data.mode = MODE_Line;
12384    }else if( cli_strcmp(z,"-column")==0 ){
12385      data.mode = MODE_Column;
12386    }else if( cli_strcmp(z,"-json")==0 ){
12387      data.mode = MODE_Json;
12388    }else if( cli_strcmp(z,"-markdown")==0 ){
12389      data.mode = MODE_Markdown;
12390    }else if( cli_strcmp(z,"-table")==0 ){
12391      data.mode = MODE_Table;
12392    }else if( cli_strcmp(z,"-box")==0 ){
12393      data.mode = MODE_Box;
12394    }else if( cli_strcmp(z,"-csv")==0 ){
12395      data.mode = MODE_Csv;
12396      memcpy(data.colSeparator,",",2);
12397#ifdef SQLITE_HAVE_ZLIB
12398    }else if( cli_strcmp(z,"-zip")==0 ){
12399      data.openMode = SHELL_OPEN_ZIPFILE;
12400#endif
12401    }else if( cli_strcmp(z,"-append")==0 ){
12402      data.openMode = SHELL_OPEN_APPENDVFS;
12403#ifndef SQLITE_OMIT_DESERIALIZE
12404    }else if( cli_strcmp(z,"-deserialize")==0 ){
12405      data.openMode = SHELL_OPEN_DESERIALIZE;
12406    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12407      data.szMax = integerValue(argv[++i]);
12408#endif
12409    }else if( cli_strcmp(z,"-readonly")==0 ){
12410      data.openMode = SHELL_OPEN_READONLY;
12411    }else if( cli_strcmp(z,"-nofollow")==0 ){
12412      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
12413    }else if( cli_strcmp(z,"-ascii")==0 ){
12414      data.mode = MODE_Ascii;
12415      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
12416      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
12417    }else if( cli_strcmp(z,"-tabs")==0 ){
12418      data.mode = MODE_List;
12419      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
12420      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12421    }else if( cli_strcmp(z,"-separator")==0 ){
12422      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
12423                       "%s",cmdline_option_value(argc,argv,++i));
12424    }else if( cli_strcmp(z,"-newline")==0 ){
12425      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
12426                       "%s",cmdline_option_value(argc,argv,++i));
12427    }else if( cli_strcmp(z,"-nullvalue")==0 ){
12428      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
12429                       "%s",cmdline_option_value(argc,argv,++i));
12430    }else if( cli_strcmp(z,"-header")==0 ){
12431      data.showHeader = 1;
12432      ShellSetFlag(&data, SHFLG_HeaderSet);
12433     }else if( cli_strcmp(z,"-noheader")==0 ){
12434      data.showHeader = 0;
12435      ShellSetFlag(&data, SHFLG_HeaderSet);
12436    }else if( cli_strcmp(z,"-echo")==0 ){
12437      ShellSetFlag(&data, SHFLG_Echo);
12438    }else if( cli_strcmp(z,"-eqp")==0 ){
12439      data.autoEQP = AUTOEQP_on;
12440    }else if( cli_strcmp(z,"-eqpfull")==0 ){
12441      data.autoEQP = AUTOEQP_full;
12442    }else if( cli_strcmp(z,"-stats")==0 ){
12443      data.statsOn = 1;
12444    }else if( cli_strcmp(z,"-scanstats")==0 ){
12445      data.scanstatsOn = 1;
12446    }else if( cli_strcmp(z,"-backslash")==0 ){
12447      /* Undocumented command-line option: -backslash
12448      ** Causes C-style backslash escapes to be evaluated in SQL statements
12449      ** prior to sending the SQL into SQLite.  Useful for injecting
12450      ** crazy bytes in the middle of SQL statements for testing and debugging.
12451      */
12452      ShellSetFlag(&data, SHFLG_Backslash);
12453    }else if( cli_strcmp(z,"-bail")==0 ){
12454      /* No-op.  The bail_on_error flag should already be set. */
12455    }else if( cli_strcmp(z,"-version")==0 ){
12456      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
12457      return 0;
12458    }else if( cli_strcmp(z,"-interactive")==0 ){
12459      stdin_is_interactive = 1;
12460    }else if( cli_strcmp(z,"-batch")==0 ){
12461      stdin_is_interactive = 0;
12462    }else if( cli_strcmp(z,"-heap")==0 ){
12463      i++;
12464    }else if( cli_strcmp(z,"-pagecache")==0 ){
12465      i+=2;
12466    }else if( cli_strcmp(z,"-lookaside")==0 ){
12467      i+=2;
12468    }else if( cli_strcmp(z,"-threadsafe")==0 ){
12469      i+=2;
12470    }else if( cli_strcmp(z,"-nonce")==0 ){
12471      i += 2;
12472    }else if( cli_strcmp(z,"-mmap")==0 ){
12473      i++;
12474    }else if( cli_strcmp(z,"-memtrace")==0 ){
12475      i++;
12476#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12477    }else if( cli_strcmp(z,"-sorterref")==0 ){
12478      i++;
12479#endif
12480    }else if( cli_strcmp(z,"-vfs")==0 ){
12481      i++;
12482#ifdef SQLITE_ENABLE_VFSTRACE
12483    }else if( cli_strcmp(z,"-vfstrace")==0 ){
12484      i++;
12485#endif
12486#ifdef SQLITE_ENABLE_MULTIPLEX
12487    }else if( cli_strcmp(z,"-multiplex")==0 ){
12488      i++;
12489#endif
12490    }else if( cli_strcmp(z,"-help")==0 ){
12491      usage(1);
12492    }else if( cli_strcmp(z,"-cmd")==0 ){
12493      /* Run commands that follow -cmd first and separately from commands
12494      ** that simply appear on the command-line.  This seems goofy.  It would
12495      ** be better if all commands ran in the order that they appear.  But
12496      ** we retain the goofy behavior for historical compatibility. */
12497      if( i==argc-1 ) break;
12498      z = cmdline_option_value(argc,argv,++i);
12499      if( z[0]=='.' ){
12500        rc = do_meta_command(z, &data);
12501        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
12502      }else{
12503        open_db(&data, 0);
12504        rc = shell_exec(&data, z, &zErrMsg);
12505        if( zErrMsg!=0 ){
12506          utf8_printf(stderr,"Error: %s\n", zErrMsg);
12507          if( bail_on_error ) return rc!=0 ? rc : 1;
12508        }else if( rc!=0 ){
12509          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
12510          if( bail_on_error ) return rc;
12511        }
12512      }
12513#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12514    }else if( cli_strncmp(z, "-A", 2)==0 ){
12515      if( nCmd>0 ){
12516        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
12517                            " with \"%s\"\n", z);
12518        return 1;
12519      }
12520      open_db(&data, OPEN_DB_ZIPFILE);
12521      if( z[2] ){
12522        argv[i] = &z[2];
12523        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
12524      }else{
12525        arDotCommand(&data, 1, argv+i, argc-i);
12526      }
12527      readStdin = 0;
12528      break;
12529#endif
12530    }else if( cli_strcmp(z,"-safe")==0 ){
12531      data.bSafeMode = data.bSafeModePersist = 1;
12532    }else{
12533      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
12534      raw_printf(stderr,"Use -help for a list of options.\n");
12535      return 1;
12536    }
12537    data.cMode = data.mode;
12538  }
12539
12540  if( !readStdin ){
12541    /* Run all arguments that do not begin with '-' as if they were separate
12542    ** command-line inputs, except for the argToSkip argument which contains
12543    ** the database filename.
12544    */
12545    for(i=0; i<nCmd; i++){
12546      if( azCmd[i][0]=='.' ){
12547        rc = do_meta_command(azCmd[i], &data);
12548        if( rc ){
12549          free(azCmd);
12550          return rc==2 ? 0 : rc;
12551        }
12552      }else{
12553        open_db(&data, 0);
12554        rc = shell_exec(&data, azCmd[i], &zErrMsg);
12555        if( zErrMsg || rc ){
12556          if( zErrMsg!=0 ){
12557            utf8_printf(stderr,"Error: %s\n", zErrMsg);
12558          }else{
12559            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
12560          }
12561          sqlite3_free(zErrMsg);
12562          free(azCmd);
12563          return rc!=0 ? rc : 1;
12564        }
12565      }
12566    }
12567  }else{
12568    /* Run commands received from standard input
12569    */
12570    if( stdin_is_interactive ){
12571      char *zHome;
12572      char *zHistory;
12573      int nHistory;
12574      printf(
12575        "SQLite version %s %.19s\n" /*extra-version-info*/
12576        "Enter \".help\" for usage hints.\n",
12577        sqlite3_libversion(), sqlite3_sourceid()
12578      );
12579      if( warnInmemoryDb ){
12580        printf("Connected to a ");
12581        printBold("transient in-memory database");
12582        printf(".\nUse \".open FILENAME\" to reopen on a "
12583               "persistent database.\n");
12584      }
12585      zHistory = getenv("SQLITE_HISTORY");
12586      if( zHistory ){
12587        zHistory = strdup(zHistory);
12588      }else if( (zHome = find_home_dir(0))!=0 ){
12589        nHistory = strlen30(zHome) + 20;
12590        if( (zHistory = malloc(nHistory))!=0 ){
12591          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
12592        }
12593      }
12594      if( zHistory ){ shell_read_history(zHistory); }
12595#if HAVE_READLINE || HAVE_EDITLINE
12596      rl_attempted_completion_function = readline_completion;
12597#elif HAVE_LINENOISE
12598      linenoiseSetCompletionCallback(linenoise_completion);
12599#endif
12600      data.in = 0;
12601      rc = process_input(&data);
12602      if( zHistory ){
12603        shell_stifle_history(2000);
12604        shell_write_history(zHistory);
12605        free(zHistory);
12606      }
12607    }else{
12608      data.in = stdin;
12609      rc = process_input(&data);
12610    }
12611  }
12612#ifndef SQLITE_SHELL_FIDDLE
12613  /* In WASM mode we have to leave the db state in place so that
12614  ** client code can "push" SQL into it after this call returns. */
12615  free(azCmd);
12616  set_table_name(&data, 0);
12617  if( data.db ){
12618    session_close_all(&data, -1);
12619    close_db(data.db);
12620  }
12621  for(i=0; i<ArraySize(data.aAuxDb); i++){
12622    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12623    if( data.aAuxDb[i].db ){
12624      session_close_all(&data, i);
12625      close_db(data.aAuxDb[i].db);
12626    }
12627  }
12628  find_home_dir(1);
12629  output_reset(&data);
12630  data.doXdgOpen = 0;
12631  clearTempFile(&data);
12632#if !SQLITE_SHELL_IS_UTF8
12633  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12634  free(argvToFree);
12635#endif
12636  free(data.colWidth);
12637  free(data.zNonce);
12638  /* Clear the global data structure so that valgrind will detect memory
12639  ** leaks */
12640  memset(&data, 0, sizeof(data));
12641#ifdef SQLITE_DEBUG
12642  if( sqlite3_memory_used()>mem_main_enter ){
12643    utf8_printf(stderr, "Memory leaked: %u bytes\n",
12644                (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12645  }
12646#endif
12647#endif /* !SQLITE_SHELL_FIDDLE */
12648  return rc;
12649}
12650
12651
12652#ifdef SQLITE_SHELL_FIDDLE
12653/* Only for emcc experimentation purposes. */
12654int fiddle_experiment(int a,int b){
12655  return a + b;
12656}
12657
12658/*
12659** Returns a pointer to the current DB handle.
12660*/
12661sqlite3 * fiddle_db_handle(){
12662  return globalDb;
12663}
12664
12665/*
12666** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
12667** "main" is assumed. Returns 0 if no db with the given name is
12668** open.
12669*/
12670sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
12671  sqlite3_vfs * pVfs = 0;
12672  if(globalDb){
12673    sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
12674                         SQLITE_FCNTL_VFS_POINTER, &pVfs);
12675  }
12676  return pVfs;
12677}
12678
12679/* Only for emcc experimentation purposes. */
12680sqlite3 * fiddle_db_arg(sqlite3 *arg){
12681    printf("fiddle_db_arg(%p)\n", (const void*)arg);
12682    return arg;
12683}
12684
12685/*
12686** Intended to be called via a SharedWorker() while a separate
12687** SharedWorker() (which manages the wasm module) is performing work
12688** which should be interrupted. Unfortunately, SharedWorker is not
12689** portable enough to make real use of.
12690*/
12691void fiddle_interrupt(void){
12692  if( globalDb ) sqlite3_interrupt(globalDb);
12693}
12694
12695/*
12696** Returns the filename of the given db name, assuming "main" if
12697** zDbName is NULL. Returns NULL if globalDb is not opened.
12698*/
12699const char * fiddle_db_filename(const char * zDbName){
12700    return globalDb
12701      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12702      : NULL;
12703}
12704
12705/*
12706** Completely wipes out the contents of the currently-opened database
12707** but leaves its storage intact for reuse.
12708*/
12709void fiddle_reset_db(void){
12710  if( globalDb ){
12711    int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
12712    if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
12713    sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
12714  }
12715}
12716
12717/*
12718** Uses the current database's VFS xRead to stream the db file's
12719** contents out to the given callback. The callback gets a single
12720** chunk of size n (its 2nd argument) on each call and must return 0
12721** on success, non-0 on error. This function returns 0 on success,
12722** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
12723** code from the callback. Note that this is not thread-friendly: it
12724** expects that it will be the only thread reading the db file and
12725** takes no measures to ensure that is the case.
12726*/
12727int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
12728  sqlite3_int64 nSize = 0;
12729  sqlite3_int64 nPos = 0;
12730  sqlite3_file * pFile = 0;
12731  unsigned char buf[1024 * 8];
12732  int nBuf = (int)sizeof(buf);
12733  int rc = shellState.db
12734    ? sqlite3_file_control(shellState.db, "main",
12735                           SQLITE_FCNTL_FILE_POINTER, &pFile)
12736    : SQLITE_NOTFOUND;
12737  if( rc ) return rc;
12738  rc = pFile->pMethods->xFileSize(pFile, &nSize);
12739  if( rc ) return rc;
12740  if(nSize % nBuf){
12741    /* DB size is not an even multiple of the buffer size. Reduce
12742    ** buffer size so that we do not unduly inflate the db size when
12743    ** exporting. */
12744    if(0 == nSize % 4096) nBuf = 4096;
12745    else if(0 == nSize % 2048) nBuf = 2048;
12746    else if(0 == nSize % 1024) nBuf = 1024;
12747    else nBuf = 512;
12748  }
12749  for( ; 0==rc && nPos<nSize; nPos += nBuf ){
12750    rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
12751    if(SQLITE_IOERR_SHORT_READ == rc){
12752      rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
12753    }
12754    if( 0==rc ) rc = xCallback(buf, nBuf);
12755  }
12756  return rc;
12757}
12758
12759/*
12760** Trivial exportable function for emscripten. It processes zSql as if
12761** it were input to the sqlite3 shell and redirects all output to the
12762** wasm binding. fiddle_main() must have been called before this
12763** is called, or results are undefined.
12764*/
12765void fiddle_exec(const char * zSql){
12766  if(zSql && *zSql){
12767    if('.'==*zSql) puts(zSql);
12768    shellState.wasm.zInput = zSql;
12769    shellState.wasm.zPos = zSql;
12770    process_input(&shellState);
12771    shellState.wasm.zInput = shellState.wasm.zPos = 0;
12772  }
12773}
12774#endif /* SQLITE_SHELL_FIDDLE */
12775