xref: /sqlite-3.40.0/src/shell.c.in (revision de868175)
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/recover/dbdata.c
1062INCLUDE ../ext/recover/sqlite3recover.h
1063INCLUDE ../ext/recover/sqlite3recover.c
1064#endif
1065
1066#if defined(SQLITE_ENABLE_SESSION)
1067/*
1068** State information for a single open session
1069*/
1070typedef struct OpenSession OpenSession;
1071struct OpenSession {
1072  char *zName;             /* Symbolic name for this session */
1073  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1074  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1075  sqlite3_session *p;      /* The open session */
1076};
1077#endif
1078
1079typedef struct ExpertInfo ExpertInfo;
1080struct ExpertInfo {
1081  sqlite3expert *pExpert;
1082  int bVerbose;
1083};
1084
1085/* A single line in the EQP output */
1086typedef struct EQPGraphRow EQPGraphRow;
1087struct EQPGraphRow {
1088  int iEqpId;           /* ID for this row */
1089  int iParentId;        /* ID of the parent row */
1090  EQPGraphRow *pNext;   /* Next row in sequence */
1091  char zText[1];        /* Text to display for this row */
1092};
1093
1094/* All EQP output is collected into an instance of the following */
1095typedef struct EQPGraph EQPGraph;
1096struct EQPGraph {
1097  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1098  EQPGraphRow *pLast;   /* Last element of the pRow list */
1099  char zPrefix[100];    /* Graph prefix */
1100};
1101
1102/* Parameters affecting columnar mode result display (defaulting together) */
1103typedef struct ColModeOpts {
1104  int iWrap;            /* In columnar modes, wrap lines reaching this limit */
1105  u8 bQuote;            /* Quote results for .mode box and table */
1106  u8 bWordWrap;         /* In columnar modes, wrap at word boundaries  */
1107} ColModeOpts;
1108#define ColModeOpts_default { 60, 0, 0 }
1109#define ColModeOpts_default_qbox { 60, 1, 0 }
1110
1111/*
1112** State information about the database connection is contained in an
1113** instance of the following structure.
1114*/
1115typedef struct ShellState ShellState;
1116struct ShellState {
1117  sqlite3 *db;           /* The database */
1118  u8 autoExplain;        /* Automatically turn on .explain mode */
1119  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1120  u8 autoEQPtest;        /* autoEQP is in test mode */
1121  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1122  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1123  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1124  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1125  u8 nEqpLevel;          /* Depth of the EQP output graph */
1126  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1127  u8 bSafeMode;          /* True to prohibit unsafe operations */
1128  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
1129  ColModeOpts cmOpts;    /* Option values affecting columnar mode output */
1130  unsigned statsOn;      /* True to display memory stats before each finalize */
1131  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1132  int inputNesting;      /* Track nesting level of .read and other redirects */
1133  int outCount;          /* Revert to stdout when reaching zero */
1134  int cnt;               /* Number of records displayed so far */
1135  int lineno;            /* Line number of last line read from in */
1136  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1137  FILE *in;              /* Read commands from this stream */
1138  FILE *out;             /* Write results here */
1139  FILE *traceOut;        /* Output for sqlite3_trace() */
1140  int nErr;              /* Number of errors seen */
1141  int mode;              /* An output mode setting */
1142  int modePrior;         /* Saved mode */
1143  int cMode;             /* temporary output mode for the current query */
1144  int normalMode;        /* Output mode before ".explain on" */
1145  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1146  int showHeader;        /* True to show column names in List or Column mode */
1147  int nCheck;            /* Number of ".check" commands run */
1148  unsigned nProgress;    /* Number of progress callbacks encountered */
1149  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1150  unsigned flgProgress;  /* Flags for the progress callback */
1151  unsigned shellFlgs;    /* Various flags */
1152  unsigned priorShFlgs;  /* Saved copy of flags */
1153  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1154  char *zDestTable;      /* Name of destination table when MODE_Insert */
1155  char *zTempFile;       /* Temporary file that might need deleting */
1156  char zTestcase[30];    /* Name of current test case */
1157  char colSeparator[20]; /* Column separator character for several modes */
1158  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1159  char colSepPrior[20];  /* Saved column separator */
1160  char rowSepPrior[20];  /* Saved row separator */
1161  int *colWidth;         /* Requested width of each column in columnar modes */
1162  int *actualWidth;      /* Actual width of each column */
1163  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1164  char nullValue[20];    /* The text to print when a NULL comes back from
1165                         ** the database */
1166  char outfile[FILENAME_MAX]; /* Filename for *out */
1167  sqlite3_stmt *pStmt;   /* Current statement if any. */
1168  FILE *pLog;            /* Write log output here */
1169  struct AuxDb {         /* Storage space for auxiliary database connections */
1170    sqlite3 *db;               /* Connection pointer */
1171    const char *zDbFilename;   /* Filename used to open the connection */
1172    char *zFreeOnClose;        /* Free this memory allocation on close */
1173#if defined(SQLITE_ENABLE_SESSION)
1174    int nSession;              /* Number of active sessions */
1175    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
1176#endif
1177  } aAuxDb[5],           /* Array of all database connections */
1178    *pAuxDb;             /* Currently active database connection */
1179  int *aiIndent;         /* Array of indents used in MODE_Explain */
1180  int nIndent;           /* Size of array aiIndent[] */
1181  int iIndent;           /* Index of current op in aiIndent[] */
1182  char *zNonce;          /* Nonce for temporary safe-mode excapes */
1183  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1184  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
1185#ifdef SQLITE_SHELL_FIDDLE
1186  struct {
1187    const char * zInput; /* Input string from wasm/JS proxy */
1188    const char * zPos;   /* Cursor pos into zInput */
1189    const char * zDefaultDbName; /* Default name for db file */
1190  } wasm;
1191#endif
1192};
1193
1194#ifdef SQLITE_SHELL_FIDDLE
1195static ShellState shellState;
1196#endif
1197
1198
1199/* Allowed values for ShellState.autoEQP
1200*/
1201#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1202#define AUTOEQP_on       1           /* Automatic EQP is on */
1203#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1204#define AUTOEQP_full     3           /* Show full EXPLAIN */
1205
1206/* Allowed values for ShellState.openMode
1207*/
1208#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1209#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1210#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1211#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1212#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1213#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1214#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1215
1216/* Allowed values for ShellState.eTraceType
1217*/
1218#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1219#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1220#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1221
1222/* Bits in the ShellState.flgProgress variable */
1223#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1224#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1225                                   ** callback limit is reached, and for each
1226                                   ** top-level SQL statement */
1227#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1228
1229/*
1230** These are the allowed shellFlgs values
1231*/
1232#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1233#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1234#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1235#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1236#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1237#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1238#define SHFLG_Echo           0x00000040 /* .echo on/off, or --echo setting */
1239#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1240#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1241#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1242
1243/*
1244** Macros for testing and setting shellFlgs
1245*/
1246#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1247#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1248#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1249
1250/*
1251** These are the allowed modes.
1252*/
1253#define MODE_Line     0  /* One column per line.  Blank line between records */
1254#define MODE_Column   1  /* One record per line in neat columns */
1255#define MODE_List     2  /* One record per line with a separator */
1256#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1257#define MODE_Html     4  /* Generate an XHTML table */
1258#define MODE_Insert   5  /* Generate SQL "insert" statements */
1259#define MODE_Quote    6  /* Quote values as for SQL */
1260#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1261#define MODE_Csv      8  /* Quote strings, numbers are plain */
1262#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1263#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1264#define MODE_Pretty  11  /* Pretty-print schemas */
1265#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1266#define MODE_Json    13  /* Output JSON */
1267#define MODE_Markdown 14 /* Markdown formatting */
1268#define MODE_Table   15  /* MySQL-style table formatting */
1269#define MODE_Box     16  /* Unicode box-drawing characters */
1270#define MODE_Count   17  /* Output only a count of the rows of output */
1271#define MODE_Off     18  /* No query output shown */
1272
1273static const char *modeDescr[] = {
1274  "line",
1275  "column",
1276  "list",
1277  "semi",
1278  "html",
1279  "insert",
1280  "quote",
1281  "tcl",
1282  "csv",
1283  "explain",
1284  "ascii",
1285  "prettyprint",
1286  "eqp",
1287  "json",
1288  "markdown",
1289  "table",
1290  "box",
1291  "count",
1292  "off"
1293};
1294
1295/*
1296** These are the column/row/line separators used by the various
1297** import/export modes.
1298*/
1299#define SEP_Column    "|"
1300#define SEP_Row       "\n"
1301#define SEP_Tab       "\t"
1302#define SEP_Space     " "
1303#define SEP_Comma     ","
1304#define SEP_CrLf      "\r\n"
1305#define SEP_Unit      "\x1F"
1306#define SEP_Record    "\x1E"
1307
1308/*
1309** Limit input nesting via .read or any other input redirect.
1310** It's not too expensive, so a generous allowance can be made.
1311*/
1312#define MAX_INPUT_NESTING 25
1313
1314/*
1315** A callback for the sqlite3_log() interface.
1316*/
1317static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1318  ShellState *p = (ShellState*)pArg;
1319  if( p->pLog==0 ) return;
1320  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1321  fflush(p->pLog);
1322}
1323
1324/*
1325** SQL function:  shell_putsnl(X)
1326**
1327** Write the text X to the screen (or whatever output is being directed)
1328** adding a newline at the end, and then return X.
1329*/
1330static void shellPutsFunc(
1331  sqlite3_context *pCtx,
1332  int nVal,
1333  sqlite3_value **apVal
1334){
1335  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1336  (void)nVal;
1337  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1338  sqlite3_result_value(pCtx, apVal[0]);
1339}
1340
1341/*
1342** If in safe mode, print an error message described by the arguments
1343** and exit immediately.
1344*/
1345static void failIfSafeMode(
1346  ShellState *p,
1347  const char *zErrMsg,
1348  ...
1349){
1350  if( p->bSafeMode ){
1351    va_list ap;
1352    char *zMsg;
1353    va_start(ap, zErrMsg);
1354    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1355    va_end(ap);
1356    raw_printf(stderr, "line %d: ", p->lineno);
1357    utf8_printf(stderr, "%s\n", zMsg);
1358    exit(1);
1359  }
1360}
1361
1362/*
1363** SQL function:   edit(VALUE)
1364**                 edit(VALUE,EDITOR)
1365**
1366** These steps:
1367**
1368**     (1) Write VALUE into a temporary file.
1369**     (2) Run program EDITOR on that temporary file.
1370**     (3) Read the temporary file back and return its content as the result.
1371**     (4) Delete the temporary file
1372**
1373** If the EDITOR argument is omitted, use the value in the VISUAL
1374** environment variable.  If still there is no EDITOR, through an error.
1375**
1376** Also throw an error if the EDITOR program returns a non-zero exit code.
1377*/
1378#ifndef SQLITE_NOHAVE_SYSTEM
1379static void editFunc(
1380  sqlite3_context *context,
1381  int argc,
1382  sqlite3_value **argv
1383){
1384  const char *zEditor;
1385  char *zTempFile = 0;
1386  sqlite3 *db;
1387  char *zCmd = 0;
1388  int bBin;
1389  int rc;
1390  int hasCRNL = 0;
1391  FILE *f = 0;
1392  sqlite3_int64 sz;
1393  sqlite3_int64 x;
1394  unsigned char *p = 0;
1395
1396  if( argc==2 ){
1397    zEditor = (const char*)sqlite3_value_text(argv[1]);
1398  }else{
1399    zEditor = getenv("VISUAL");
1400  }
1401  if( zEditor==0 ){
1402    sqlite3_result_error(context, "no editor for edit()", -1);
1403    return;
1404  }
1405  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1406    sqlite3_result_error(context, "NULL input to edit()", -1);
1407    return;
1408  }
1409  db = sqlite3_context_db_handle(context);
1410  zTempFile = 0;
1411  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1412  if( zTempFile==0 ){
1413    sqlite3_uint64 r = 0;
1414    sqlite3_randomness(sizeof(r), &r);
1415    zTempFile = sqlite3_mprintf("temp%llx", r);
1416    if( zTempFile==0 ){
1417      sqlite3_result_error_nomem(context);
1418      return;
1419    }
1420  }
1421  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1422  /* When writing the file to be edited, do \n to \r\n conversions on systems
1423  ** that want \r\n line endings */
1424  f = fopen(zTempFile, bBin ? "wb" : "w");
1425  if( f==0 ){
1426    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1427    goto edit_func_end;
1428  }
1429  sz = sqlite3_value_bytes(argv[0]);
1430  if( bBin ){
1431    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1432  }else{
1433    const char *z = (const char*)sqlite3_value_text(argv[0]);
1434    /* Remember whether or not the value originally contained \r\n */
1435    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1436    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1437  }
1438  fclose(f);
1439  f = 0;
1440  if( x!=sz ){
1441    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1442    goto edit_func_end;
1443  }
1444  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1445  if( zCmd==0 ){
1446    sqlite3_result_error_nomem(context);
1447    goto edit_func_end;
1448  }
1449  rc = system(zCmd);
1450  sqlite3_free(zCmd);
1451  if( rc ){
1452    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1453    goto edit_func_end;
1454  }
1455  f = fopen(zTempFile, "rb");
1456  if( f==0 ){
1457    sqlite3_result_error(context,
1458      "edit() cannot reopen temp file after edit", -1);
1459    goto edit_func_end;
1460  }
1461  fseek(f, 0, SEEK_END);
1462  sz = ftell(f);
1463  rewind(f);
1464  p = sqlite3_malloc64( sz+1 );
1465  if( p==0 ){
1466    sqlite3_result_error_nomem(context);
1467    goto edit_func_end;
1468  }
1469  x = fread(p, 1, (size_t)sz, f);
1470  fclose(f);
1471  f = 0;
1472  if( x!=sz ){
1473    sqlite3_result_error(context, "could not read back the whole file", -1);
1474    goto edit_func_end;
1475  }
1476  if( bBin ){
1477    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1478  }else{
1479    sqlite3_int64 i, j;
1480    if( hasCRNL ){
1481      /* If the original contains \r\n then do no conversions back to \n */
1482    }else{
1483      /* If the file did not originally contain \r\n then convert any new
1484      ** \r\n back into \n */
1485      for(i=j=0; i<sz; i++){
1486        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1487        p[j++] = p[i];
1488      }
1489      sz = j;
1490      p[sz] = 0;
1491    }
1492    sqlite3_result_text64(context, (const char*)p, sz,
1493                          sqlite3_free, SQLITE_UTF8);
1494  }
1495  p = 0;
1496
1497edit_func_end:
1498  if( f ) fclose(f);
1499  unlink(zTempFile);
1500  sqlite3_free(zTempFile);
1501  sqlite3_free(p);
1502}
1503#endif /* SQLITE_NOHAVE_SYSTEM */
1504
1505/*
1506** Save or restore the current output mode
1507*/
1508static void outputModePush(ShellState *p){
1509  p->modePrior = p->mode;
1510  p->priorShFlgs = p->shellFlgs;
1511  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1512  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1513}
1514static void outputModePop(ShellState *p){
1515  p->mode = p->modePrior;
1516  p->shellFlgs = p->priorShFlgs;
1517  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1518  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1519}
1520
1521/*
1522** Output the given string as a hex-encoded blob (eg. X'1234' )
1523*/
1524static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1525  int i;
1526  unsigned char *aBlob = (unsigned char*)pBlob;
1527
1528  char *zStr = sqlite3_malloc(nBlob*2 + 1);
1529  shell_check_oom(zStr);
1530
1531  for(i=0; i<nBlob; i++){
1532    static const char aHex[] = {
1533        '0', '1', '2', '3', '4', '5', '6', '7',
1534        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1535    };
1536    zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1537    zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1538  }
1539  zStr[i*2] = '\0';
1540
1541  raw_printf(out,"X'%s'", zStr);
1542  sqlite3_free(zStr);
1543}
1544
1545/*
1546** Find a string that is not found anywhere in z[].  Return a pointer
1547** to that string.
1548**
1549** Try to use zA and zB first.  If both of those are already found in z[]
1550** then make up some string and store it in the buffer zBuf.
1551*/
1552static const char *unused_string(
1553  const char *z,                    /* Result must not appear anywhere in z */
1554  const char *zA, const char *zB,   /* Try these first */
1555  char *zBuf                        /* Space to store a generated string */
1556){
1557  unsigned i = 0;
1558  if( strstr(z, zA)==0 ) return zA;
1559  if( strstr(z, zB)==0 ) return zB;
1560  do{
1561    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1562  }while( strstr(z,zBuf)!=0 );
1563  return zBuf;
1564}
1565
1566/*
1567** Output the given string as a quoted string using SQL quoting conventions.
1568**
1569** See also: output_quoted_escaped_string()
1570*/
1571static void output_quoted_string(FILE *out, const char *z){
1572  int i;
1573  char c;
1574  setBinaryMode(out, 1);
1575  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1576  if( c==0 ){
1577    utf8_printf(out,"'%s'",z);
1578  }else{
1579    raw_printf(out, "'");
1580    while( *z ){
1581      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1582      if( c=='\'' ) i++;
1583      if( i ){
1584        utf8_printf(out, "%.*s", i, z);
1585        z += i;
1586      }
1587      if( c=='\'' ){
1588        raw_printf(out, "'");
1589        continue;
1590      }
1591      if( c==0 ){
1592        break;
1593      }
1594      z++;
1595    }
1596    raw_printf(out, "'");
1597  }
1598  setTextMode(out, 1);
1599}
1600
1601/*
1602** Output the given string as a quoted string using SQL quoting conventions.
1603** Additionallly , escape the "\n" and "\r" characters so that they do not
1604** get corrupted by end-of-line translation facilities in some operating
1605** systems.
1606**
1607** This is like output_quoted_string() but with the addition of the \r\n
1608** escape mechanism.
1609*/
1610static void output_quoted_escaped_string(FILE *out, const char *z){
1611  int i;
1612  char c;
1613  setBinaryMode(out, 1);
1614  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1615  if( c==0 ){
1616    utf8_printf(out,"'%s'",z);
1617  }else{
1618    const char *zNL = 0;
1619    const char *zCR = 0;
1620    int nNL = 0;
1621    int nCR = 0;
1622    char zBuf1[20], zBuf2[20];
1623    for(i=0; z[i]; i++){
1624      if( z[i]=='\n' ) nNL++;
1625      if( z[i]=='\r' ) nCR++;
1626    }
1627    if( nNL ){
1628      raw_printf(out, "replace(");
1629      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1630    }
1631    if( nCR ){
1632      raw_printf(out, "replace(");
1633      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1634    }
1635    raw_printf(out, "'");
1636    while( *z ){
1637      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1638      if( c=='\'' ) i++;
1639      if( i ){
1640        utf8_printf(out, "%.*s", i, z);
1641        z += i;
1642      }
1643      if( c=='\'' ){
1644        raw_printf(out, "'");
1645        continue;
1646      }
1647      if( c==0 ){
1648        break;
1649      }
1650      z++;
1651      if( c=='\n' ){
1652        raw_printf(out, "%s", zNL);
1653        continue;
1654      }
1655      raw_printf(out, "%s", zCR);
1656    }
1657    raw_printf(out, "'");
1658    if( nCR ){
1659      raw_printf(out, ",'%s',char(13))", zCR);
1660    }
1661    if( nNL ){
1662      raw_printf(out, ",'%s',char(10))", zNL);
1663    }
1664  }
1665  setTextMode(out, 1);
1666}
1667
1668/*
1669** Output the given string as a quoted according to C or TCL quoting rules.
1670*/
1671static void output_c_string(FILE *out, const char *z){
1672  unsigned int c;
1673  fputc('"', out);
1674  while( (c = *(z++))!=0 ){
1675    if( c=='\\' ){
1676      fputc(c, out);
1677      fputc(c, out);
1678    }else if( c=='"' ){
1679      fputc('\\', out);
1680      fputc('"', out);
1681    }else if( c=='\t' ){
1682      fputc('\\', out);
1683      fputc('t', out);
1684    }else if( c=='\n' ){
1685      fputc('\\', out);
1686      fputc('n', out);
1687    }else if( c=='\r' ){
1688      fputc('\\', out);
1689      fputc('r', out);
1690    }else if( !isprint(c&0xff) ){
1691      raw_printf(out, "\\%03o", c&0xff);
1692    }else{
1693      fputc(c, out);
1694    }
1695  }
1696  fputc('"', out);
1697}
1698
1699/*
1700** Output the given string as a quoted according to JSON quoting rules.
1701*/
1702static void output_json_string(FILE *out, const char *z, i64 n){
1703  unsigned int c;
1704  if( n<0 ) n = strlen(z);
1705  fputc('"', out);
1706  while( n-- ){
1707    c = *(z++);
1708    if( c=='\\' || c=='"' ){
1709      fputc('\\', out);
1710      fputc(c, out);
1711    }else if( c<=0x1f ){
1712      fputc('\\', out);
1713      if( c=='\b' ){
1714        fputc('b', out);
1715      }else if( c=='\f' ){
1716        fputc('f', out);
1717      }else if( c=='\n' ){
1718        fputc('n', out);
1719      }else if( c=='\r' ){
1720        fputc('r', out);
1721      }else if( c=='\t' ){
1722        fputc('t', out);
1723      }else{
1724         raw_printf(out, "u%04x",c);
1725      }
1726    }else{
1727      fputc(c, out);
1728    }
1729  }
1730  fputc('"', out);
1731}
1732
1733/*
1734** Output the given string with characters that are special to
1735** HTML escaped.
1736*/
1737static void output_html_string(FILE *out, const char *z){
1738  int i;
1739  if( z==0 ) z = "";
1740  while( *z ){
1741    for(i=0;   z[i]
1742            && z[i]!='<'
1743            && z[i]!='&'
1744            && z[i]!='>'
1745            && z[i]!='\"'
1746            && z[i]!='\'';
1747        i++){}
1748    if( i>0 ){
1749      utf8_printf(out,"%.*s",i,z);
1750    }
1751    if( z[i]=='<' ){
1752      raw_printf(out,"&lt;");
1753    }else if( z[i]=='&' ){
1754      raw_printf(out,"&amp;");
1755    }else if( z[i]=='>' ){
1756      raw_printf(out,"&gt;");
1757    }else if( z[i]=='\"' ){
1758      raw_printf(out,"&quot;");
1759    }else if( z[i]=='\'' ){
1760      raw_printf(out,"&#39;");
1761    }else{
1762      break;
1763    }
1764    z += i + 1;
1765  }
1766}
1767
1768/*
1769** If a field contains any character identified by a 1 in the following
1770** array, then the string must be quoted for CSV.
1771*/
1772static const char needCsvQuote[] = {
1773  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1774  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1775  1, 0, 1, 0, 0, 0, 0, 1,   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, 0,
1779  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1780  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 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  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1788  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1789};
1790
1791/*
1792** Output a single term of CSV.  Actually, p->colSeparator is used for
1793** the separator, which may or may not be a comma.  p->nullValue is
1794** the null value.  Strings are quoted if necessary.  The separator
1795** is only issued if bSep is true.
1796*/
1797static void output_csv(ShellState *p, const char *z, int bSep){
1798  FILE *out = p->out;
1799  if( z==0 ){
1800    utf8_printf(out,"%s",p->nullValue);
1801  }else{
1802    unsigned i;
1803    for(i=0; z[i]; i++){
1804      if( needCsvQuote[((unsigned char*)z)[i]] ){
1805        i = 0;
1806        break;
1807      }
1808    }
1809    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1810      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1811      shell_check_oom(zQuoted);
1812      utf8_printf(out, "%s", zQuoted);
1813      sqlite3_free(zQuoted);
1814    }else{
1815      utf8_printf(out, "%s", z);
1816    }
1817  }
1818  if( bSep ){
1819    utf8_printf(p->out, "%s", p->colSeparator);
1820  }
1821}
1822
1823/*
1824** This routine runs when the user presses Ctrl-C
1825*/
1826static void interrupt_handler(int NotUsed){
1827  UNUSED_PARAMETER(NotUsed);
1828  seenInterrupt++;
1829  if( seenInterrupt>2 ) exit(1);
1830  if( globalDb ) sqlite3_interrupt(globalDb);
1831}
1832
1833#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1834/*
1835** This routine runs for console events (e.g. Ctrl-C) on Win32
1836*/
1837static BOOL WINAPI ConsoleCtrlHandler(
1838  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1839){
1840  if( dwCtrlType==CTRL_C_EVENT ){
1841    interrupt_handler(0);
1842    return TRUE;
1843  }
1844  return FALSE;
1845}
1846#endif
1847
1848#ifndef SQLITE_OMIT_AUTHORIZATION
1849/*
1850** This authorizer runs in safe mode.
1851*/
1852static int safeModeAuth(
1853  void *pClientData,
1854  int op,
1855  const char *zA1,
1856  const char *zA2,
1857  const char *zA3,
1858  const char *zA4
1859){
1860  ShellState *p = (ShellState*)pClientData;
1861  static const char *azProhibitedFunctions[] = {
1862    "edit",
1863    "fts3_tokenizer",
1864    "load_extension",
1865    "readfile",
1866    "writefile",
1867    "zipfile",
1868    "zipfile_cds",
1869  };
1870  UNUSED_PARAMETER(zA2);
1871  UNUSED_PARAMETER(zA3);
1872  UNUSED_PARAMETER(zA4);
1873  switch( op ){
1874    case SQLITE_ATTACH: {
1875#ifndef SQLITE_SHELL_FIDDLE
1876      /* In WASM builds the filesystem is a virtual sandbox, so
1877      ** there's no harm in using ATTACH. */
1878      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1879#endif
1880      break;
1881    }
1882    case SQLITE_FUNCTION: {
1883      int i;
1884      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1885        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1886          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1887                         azProhibitedFunctions[i]);
1888        }
1889      }
1890      break;
1891    }
1892  }
1893  return SQLITE_OK;
1894}
1895
1896/*
1897** When the ".auth ON" is set, the following authorizer callback is
1898** invoked.  It always returns SQLITE_OK.
1899*/
1900static int shellAuth(
1901  void *pClientData,
1902  int op,
1903  const char *zA1,
1904  const char *zA2,
1905  const char *zA3,
1906  const char *zA4
1907){
1908  ShellState *p = (ShellState*)pClientData;
1909  static const char *azAction[] = { 0,
1910     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1911     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1912     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1913     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1914     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1915     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1916     "PRAGMA",               "READ",                 "SELECT",
1917     "TRANSACTION",          "UPDATE",               "ATTACH",
1918     "DETACH",               "ALTER_TABLE",          "REINDEX",
1919     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1920     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1921  };
1922  int i;
1923  const char *az[4];
1924  az[0] = zA1;
1925  az[1] = zA2;
1926  az[2] = zA3;
1927  az[3] = zA4;
1928  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1929  for(i=0; i<4; i++){
1930    raw_printf(p->out, " ");
1931    if( az[i] ){
1932      output_c_string(p->out, az[i]);
1933    }else{
1934      raw_printf(p->out, "NULL");
1935    }
1936  }
1937  raw_printf(p->out, "\n");
1938  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1939  return SQLITE_OK;
1940}
1941#endif
1942
1943/*
1944** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1945**
1946** This routine converts some CREATE TABLE statements for shadow tables
1947** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1948**
1949** If the schema statement in z[] contains a start-of-comment and if
1950** sqlite3_complete() returns false, try to terminate the comment before
1951** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
1952*/
1953static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1954  char *zToFree = 0;
1955  if( z==0 ) return;
1956  if( zTail==0 ) return;
1957  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
1958    const char *zOrig = z;
1959    static const char *azTerm[] = { "", "*/", "\n" };
1960    int i;
1961    for(i=0; i<ArraySize(azTerm); i++){
1962      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
1963      if( sqlite3_complete(zNew) ){
1964        size_t n = strlen(zNew);
1965        zNew[n-1] = 0;
1966        zToFree = zNew;
1967        z = zNew;
1968        break;
1969      }
1970      sqlite3_free(zNew);
1971    }
1972  }
1973  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1974    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1975  }else{
1976    utf8_printf(out, "%s%s", z, zTail);
1977  }
1978  sqlite3_free(zToFree);
1979}
1980static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1981  char c = z[n];
1982  z[n] = 0;
1983  printSchemaLine(out, z, zTail);
1984  z[n] = c;
1985}
1986
1987/*
1988** Return true if string z[] has nothing but whitespace and comments to the
1989** end of the first line.
1990*/
1991static int wsToEol(const char *z){
1992  int i;
1993  for(i=0; z[i]; i++){
1994    if( z[i]=='\n' ) return 1;
1995    if( IsSpace(z[i]) ) continue;
1996    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1997    return 0;
1998  }
1999  return 1;
2000}
2001
2002/*
2003** Add a new entry to the EXPLAIN QUERY PLAN data
2004*/
2005static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
2006  EQPGraphRow *pNew;
2007  i64 nText;
2008  if( zText==0 ) return;
2009  nText = strlen(zText);
2010  if( p->autoEQPtest ){
2011    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
2012  }
2013  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2014  shell_check_oom(pNew);
2015  pNew->iEqpId = iEqpId;
2016  pNew->iParentId = p2;
2017  memcpy(pNew->zText, zText, nText+1);
2018  pNew->pNext = 0;
2019  if( p->sGraph.pLast ){
2020    p->sGraph.pLast->pNext = pNew;
2021  }else{
2022    p->sGraph.pRow = pNew;
2023  }
2024  p->sGraph.pLast = pNew;
2025}
2026
2027/*
2028** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2029** in p->sGraph.
2030*/
2031static void eqp_reset(ShellState *p){
2032  EQPGraphRow *pRow, *pNext;
2033  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2034    pNext = pRow->pNext;
2035    sqlite3_free(pRow);
2036  }
2037  memset(&p->sGraph, 0, sizeof(p->sGraph));
2038}
2039
2040/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2041** pOld, or return the first such line if pOld is NULL
2042*/
2043static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2044  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2045  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2046  return pRow;
2047}
2048
2049/* Render a single level of the graph that has iEqpId as its parent.  Called
2050** recursively to render sublevels.
2051*/
2052static void eqp_render_level(ShellState *p, int iEqpId){
2053  EQPGraphRow *pRow, *pNext;
2054  i64 n = strlen(p->sGraph.zPrefix);
2055  char *z;
2056  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2057    pNext = eqp_next_row(p, iEqpId, pRow);
2058    z = pRow->zText;
2059    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2060                pNext ? "|--" : "`--", z);
2061    if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2062      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
2063      eqp_render_level(p, pRow->iEqpId);
2064      p->sGraph.zPrefix[n] = 0;
2065    }
2066  }
2067}
2068
2069/*
2070** Display and reset the EXPLAIN QUERY PLAN data
2071*/
2072static void eqp_render(ShellState *p){
2073  EQPGraphRow *pRow = p->sGraph.pRow;
2074  if( pRow ){
2075    if( pRow->zText[0]=='-' ){
2076      if( pRow->pNext==0 ){
2077        eqp_reset(p);
2078        return;
2079      }
2080      utf8_printf(p->out, "%s\n", pRow->zText+3);
2081      p->sGraph.pRow = pRow->pNext;
2082      sqlite3_free(pRow);
2083    }else{
2084      utf8_printf(p->out, "QUERY PLAN\n");
2085    }
2086    p->sGraph.zPrefix[0] = 0;
2087    eqp_render_level(p, 0);
2088    eqp_reset(p);
2089  }
2090}
2091
2092#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2093/*
2094** Progress handler callback.
2095*/
2096static int progress_handler(void *pClientData) {
2097  ShellState *p = (ShellState*)pClientData;
2098  p->nProgress++;
2099  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2100    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2101    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2102    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2103    return 1;
2104  }
2105  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2106    raw_printf(p->out, "Progress %u\n", p->nProgress);
2107  }
2108  return 0;
2109}
2110#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2111
2112/*
2113** Print N dashes
2114*/
2115static void print_dashes(FILE *out, int N){
2116  const char zDash[] = "--------------------------------------------------";
2117  const int nDash = sizeof(zDash) - 1;
2118  while( N>nDash ){
2119    fputs(zDash, out);
2120    N -= nDash;
2121  }
2122  raw_printf(out, "%.*s", N, zDash);
2123}
2124
2125/*
2126** Print a markdown or table-style row separator using ascii-art
2127*/
2128static void print_row_separator(
2129  ShellState *p,
2130  int nArg,
2131  const char *zSep
2132){
2133  int i;
2134  if( nArg>0 ){
2135    fputs(zSep, p->out);
2136    print_dashes(p->out, p->actualWidth[0]+2);
2137    for(i=1; i<nArg; i++){
2138      fputs(zSep, p->out);
2139      print_dashes(p->out, p->actualWidth[i]+2);
2140    }
2141    fputs(zSep, p->out);
2142  }
2143  fputs("\n", p->out);
2144}
2145
2146/*
2147** This is the callback routine that the shell
2148** invokes for each row of a query result.
2149*/
2150static int shell_callback(
2151  void *pArg,
2152  int nArg,        /* Number of result columns */
2153  char **azArg,    /* Text of each result column */
2154  char **azCol,    /* Column names */
2155  int *aiType      /* Column types.  Might be NULL */
2156){
2157  int i;
2158  ShellState *p = (ShellState*)pArg;
2159
2160  if( azArg==0 ) return 0;
2161  switch( p->cMode ){
2162    case MODE_Count:
2163    case MODE_Off: {
2164      break;
2165    }
2166    case MODE_Line: {
2167      int w = 5;
2168      if( azArg==0 ) break;
2169      for(i=0; i<nArg; i++){
2170        int len = strlen30(azCol[i] ? azCol[i] : "");
2171        if( len>w ) w = len;
2172      }
2173      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2174      for(i=0; i<nArg; i++){
2175        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2176                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2177      }
2178      break;
2179    }
2180    case MODE_Explain: {
2181      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2182      if( nArg>ArraySize(aExplainWidth) ){
2183        nArg = ArraySize(aExplainWidth);
2184      }
2185      if( p->cnt++==0 ){
2186        for(i=0; i<nArg; i++){
2187          int w = aExplainWidth[i];
2188          utf8_width_print(p->out, w, azCol[i]);
2189          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2190        }
2191        for(i=0; i<nArg; i++){
2192          int w = aExplainWidth[i];
2193          print_dashes(p->out, w);
2194          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2195        }
2196      }
2197      if( azArg==0 ) break;
2198      for(i=0; i<nArg; i++){
2199        int w = aExplainWidth[i];
2200        if( i==nArg-1 ) w = 0;
2201        if( azArg[i] && strlenChar(azArg[i])>w ){
2202          w = strlenChar(azArg[i]);
2203        }
2204        if( i==1 && p->aiIndent && p->pStmt ){
2205          if( p->iIndent<p->nIndent ){
2206            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2207          }
2208          p->iIndent++;
2209        }
2210        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2211        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2212      }
2213      break;
2214    }
2215    case MODE_Semi: {   /* .schema and .fullschema output */
2216      printSchemaLine(p->out, azArg[0], ";\n");
2217      break;
2218    }
2219    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2220      char *z;
2221      int j;
2222      int nParen = 0;
2223      char cEnd = 0;
2224      char c;
2225      int nLine = 0;
2226      assert( nArg==1 );
2227      if( azArg[0]==0 ) break;
2228      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2229       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2230      ){
2231        utf8_printf(p->out, "%s;\n", azArg[0]);
2232        break;
2233      }
2234      z = sqlite3_mprintf("%s", azArg[0]);
2235      shell_check_oom(z);
2236      j = 0;
2237      for(i=0; IsSpace(z[i]); i++){}
2238      for(; (c = z[i])!=0; i++){
2239        if( IsSpace(c) ){
2240          if( z[j-1]=='\r' ) z[j-1] = '\n';
2241          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2242        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2243          j--;
2244        }
2245        z[j++] = c;
2246      }
2247      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2248      z[j] = 0;
2249      if( strlen30(z)>=79 ){
2250        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2251          if( c==cEnd ){
2252            cEnd = 0;
2253          }else if( c=='"' || c=='\'' || c=='`' ){
2254            cEnd = c;
2255          }else if( c=='[' ){
2256            cEnd = ']';
2257          }else if( c=='-' && z[i+1]=='-' ){
2258            cEnd = '\n';
2259          }else if( c=='(' ){
2260            nParen++;
2261          }else if( c==')' ){
2262            nParen--;
2263            if( nLine>0 && nParen==0 && j>0 ){
2264              printSchemaLineN(p->out, z, j, "\n");
2265              j = 0;
2266            }
2267          }
2268          z[j++] = c;
2269          if( nParen==1 && cEnd==0
2270           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2271          ){
2272            if( c=='\n' ) j--;
2273            printSchemaLineN(p->out, z, j, "\n  ");
2274            j = 0;
2275            nLine++;
2276            while( IsSpace(z[i+1]) ){ i++; }
2277          }
2278        }
2279        z[j] = 0;
2280      }
2281      printSchemaLine(p->out, z, ";\n");
2282      sqlite3_free(z);
2283      break;
2284    }
2285    case MODE_List: {
2286      if( p->cnt++==0 && p->showHeader ){
2287        for(i=0; i<nArg; i++){
2288          utf8_printf(p->out,"%s%s",azCol[i],
2289                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2290        }
2291      }
2292      if( azArg==0 ) break;
2293      for(i=0; i<nArg; i++){
2294        char *z = azArg[i];
2295        if( z==0 ) z = p->nullValue;
2296        utf8_printf(p->out, "%s", z);
2297        if( i<nArg-1 ){
2298          utf8_printf(p->out, "%s", p->colSeparator);
2299        }else{
2300          utf8_printf(p->out, "%s", p->rowSeparator);
2301        }
2302      }
2303      break;
2304    }
2305    case MODE_Html: {
2306      if( p->cnt++==0 && p->showHeader ){
2307        raw_printf(p->out,"<TR>");
2308        for(i=0; i<nArg; i++){
2309          raw_printf(p->out,"<TH>");
2310          output_html_string(p->out, azCol[i]);
2311          raw_printf(p->out,"</TH>\n");
2312        }
2313        raw_printf(p->out,"</TR>\n");
2314      }
2315      if( azArg==0 ) break;
2316      raw_printf(p->out,"<TR>");
2317      for(i=0; i<nArg; i++){
2318        raw_printf(p->out,"<TD>");
2319        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2320        raw_printf(p->out,"</TD>\n");
2321      }
2322      raw_printf(p->out,"</TR>\n");
2323      break;
2324    }
2325    case MODE_Tcl: {
2326      if( p->cnt++==0 && p->showHeader ){
2327        for(i=0; i<nArg; i++){
2328          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2329          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2330        }
2331        utf8_printf(p->out, "%s", p->rowSeparator);
2332      }
2333      if( azArg==0 ) break;
2334      for(i=0; i<nArg; i++){
2335        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2336        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2337      }
2338      utf8_printf(p->out, "%s", p->rowSeparator);
2339      break;
2340    }
2341    case MODE_Csv: {
2342      setBinaryMode(p->out, 1);
2343      if( p->cnt++==0 && p->showHeader ){
2344        for(i=0; i<nArg; i++){
2345          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2346        }
2347        utf8_printf(p->out, "%s", p->rowSeparator);
2348      }
2349      if( nArg>0 ){
2350        for(i=0; i<nArg; i++){
2351          output_csv(p, azArg[i], i<nArg-1);
2352        }
2353        utf8_printf(p->out, "%s", p->rowSeparator);
2354      }
2355      setTextMode(p->out, 1);
2356      break;
2357    }
2358    case MODE_Insert: {
2359      if( azArg==0 ) break;
2360      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2361      if( p->showHeader ){
2362        raw_printf(p->out,"(");
2363        for(i=0; i<nArg; i++){
2364          if( i>0 ) raw_printf(p->out, ",");
2365          if( quoteChar(azCol[i]) ){
2366            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2367            shell_check_oom(z);
2368            utf8_printf(p->out, "%s", z);
2369            sqlite3_free(z);
2370          }else{
2371            raw_printf(p->out, "%s", azCol[i]);
2372          }
2373        }
2374        raw_printf(p->out,")");
2375      }
2376      p->cnt++;
2377      for(i=0; i<nArg; i++){
2378        raw_printf(p->out, i>0 ? "," : " VALUES(");
2379        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2380          utf8_printf(p->out,"NULL");
2381        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2382          if( ShellHasFlag(p, SHFLG_Newlines) ){
2383            output_quoted_string(p->out, azArg[i]);
2384          }else{
2385            output_quoted_escaped_string(p->out, azArg[i]);
2386          }
2387        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2388          utf8_printf(p->out,"%s", azArg[i]);
2389        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2390          char z[50];
2391          double r = sqlite3_column_double(p->pStmt, i);
2392          sqlite3_uint64 ur;
2393          memcpy(&ur,&r,sizeof(r));
2394          if( ur==0x7ff0000000000000LL ){
2395            raw_printf(p->out, "1e999");
2396          }else if( ur==0xfff0000000000000LL ){
2397            raw_printf(p->out, "-1e999");
2398          }else{
2399            sqlite3_int64 ir = (sqlite3_int64)r;
2400            if( r==(double)ir ){
2401              sqlite3_snprintf(50,z,"%lld.0", ir);
2402            }else{
2403              sqlite3_snprintf(50,z,"%!.20g", r);
2404            }
2405            raw_printf(p->out, "%s", z);
2406          }
2407        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2408          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2409          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2410          output_hex_blob(p->out, pBlob, nBlob);
2411        }else if( isNumber(azArg[i], 0) ){
2412          utf8_printf(p->out,"%s", azArg[i]);
2413        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2414          output_quoted_string(p->out, azArg[i]);
2415        }else{
2416          output_quoted_escaped_string(p->out, azArg[i]);
2417        }
2418      }
2419      raw_printf(p->out,");\n");
2420      break;
2421    }
2422    case MODE_Json: {
2423      if( azArg==0 ) break;
2424      if( p->cnt==0 ){
2425        fputs("[{", p->out);
2426      }else{
2427        fputs(",\n{", p->out);
2428      }
2429      p->cnt++;
2430      for(i=0; i<nArg; i++){
2431        output_json_string(p->out, azCol[i], -1);
2432        putc(':', p->out);
2433        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2434          fputs("null",p->out);
2435        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2436          char z[50];
2437          double r = sqlite3_column_double(p->pStmt, i);
2438          sqlite3_uint64 ur;
2439          memcpy(&ur,&r,sizeof(r));
2440          if( ur==0x7ff0000000000000LL ){
2441            raw_printf(p->out, "1e999");
2442          }else if( ur==0xfff0000000000000LL ){
2443            raw_printf(p->out, "-1e999");
2444          }else{
2445            sqlite3_snprintf(50,z,"%!.20g", r);
2446            raw_printf(p->out, "%s", z);
2447          }
2448        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2449          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2450          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2451          output_json_string(p->out, pBlob, nBlob);
2452        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2453          output_json_string(p->out, azArg[i], -1);
2454        }else{
2455          utf8_printf(p->out,"%s", azArg[i]);
2456        }
2457        if( i<nArg-1 ){
2458          putc(',', p->out);
2459        }
2460      }
2461      putc('}', p->out);
2462      break;
2463    }
2464    case MODE_Quote: {
2465      if( azArg==0 ) break;
2466      if( p->cnt==0 && p->showHeader ){
2467        for(i=0; i<nArg; i++){
2468          if( i>0 ) fputs(p->colSeparator, p->out);
2469          output_quoted_string(p->out, azCol[i]);
2470        }
2471        fputs(p->rowSeparator, p->out);
2472      }
2473      p->cnt++;
2474      for(i=0; i<nArg; i++){
2475        if( i>0 ) fputs(p->colSeparator, p->out);
2476        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2477          utf8_printf(p->out,"NULL");
2478        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2479          output_quoted_string(p->out, azArg[i]);
2480        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2481          utf8_printf(p->out,"%s", azArg[i]);
2482        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2483          char z[50];
2484          double r = sqlite3_column_double(p->pStmt, i);
2485          sqlite3_snprintf(50,z,"%!.20g", r);
2486          raw_printf(p->out, "%s", z);
2487        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2488          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2489          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2490          output_hex_blob(p->out, pBlob, nBlob);
2491        }else if( isNumber(azArg[i], 0) ){
2492          utf8_printf(p->out,"%s", azArg[i]);
2493        }else{
2494          output_quoted_string(p->out, azArg[i]);
2495        }
2496      }
2497      fputs(p->rowSeparator, p->out);
2498      break;
2499    }
2500    case MODE_Ascii: {
2501      if( p->cnt++==0 && p->showHeader ){
2502        for(i=0; i<nArg; i++){
2503          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2504          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2505        }
2506        utf8_printf(p->out, "%s", p->rowSeparator);
2507      }
2508      if( azArg==0 ) break;
2509      for(i=0; i<nArg; i++){
2510        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2511        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2512      }
2513      utf8_printf(p->out, "%s", p->rowSeparator);
2514      break;
2515    }
2516    case MODE_EQP: {
2517      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2518      break;
2519    }
2520  }
2521  return 0;
2522}
2523
2524/*
2525** This is the callback routine that the SQLite library
2526** invokes for each row of a query result.
2527*/
2528static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2529  /* since we don't have type info, call the shell_callback with a NULL value */
2530  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2531}
2532
2533/*
2534** This is the callback routine from sqlite3_exec() that appends all
2535** output onto the end of a ShellText object.
2536*/
2537static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2538  ShellText *p = (ShellText*)pArg;
2539  int i;
2540  UNUSED_PARAMETER(az);
2541  if( azArg==0 ) return 0;
2542  if( p->n ) appendText(p, "|", 0);
2543  for(i=0; i<nArg; i++){
2544    if( i ) appendText(p, ",", 0);
2545    if( azArg[i] ) appendText(p, azArg[i], 0);
2546  }
2547  return 0;
2548}
2549
2550/*
2551** Generate an appropriate SELFTEST table in the main database.
2552*/
2553static void createSelftestTable(ShellState *p){
2554  char *zErrMsg = 0;
2555  sqlite3_exec(p->db,
2556    "SAVEPOINT selftest_init;\n"
2557    "CREATE TABLE IF NOT EXISTS selftest(\n"
2558    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2559    "  op TEXT,\n"                   /* Operator:  memo run */
2560    "  cmd TEXT,\n"                  /* Command text */
2561    "  ans TEXT\n"                   /* Desired answer */
2562    ");"
2563    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2564    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2565    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2566    "         'memo','Tests generated by --init');\n"
2567    "INSERT INTO [_shell$self]\n"
2568    "  SELECT 'run',\n"
2569    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2570                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2571    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2572                          "FROM sqlite_schema ORDER BY 2',224));\n"
2573    "INSERT INTO [_shell$self]\n"
2574    "  SELECT 'run',"
2575    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2576    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2577    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2578    "  FROM (\n"
2579    "    SELECT name FROM sqlite_schema\n"
2580    "     WHERE type='table'\n"
2581    "       AND name<>'selftest'\n"
2582    "       AND coalesce(rootpage,0)>0\n"
2583    "  )\n"
2584    " ORDER BY name;\n"
2585    "INSERT INTO [_shell$self]\n"
2586    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2587    "INSERT INTO selftest(tno,op,cmd,ans)"
2588    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2589    "DROP TABLE [_shell$self];"
2590    ,0,0,&zErrMsg);
2591  if( zErrMsg ){
2592    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2593    sqlite3_free(zErrMsg);
2594  }
2595  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2596}
2597
2598
2599/*
2600** Set the destination table field of the ShellState structure to
2601** the name of the table given.  Escape any quote characters in the
2602** table name.
2603*/
2604static void set_table_name(ShellState *p, const char *zName){
2605  int i, n;
2606  char cQuote;
2607  char *z;
2608
2609  if( p->zDestTable ){
2610    free(p->zDestTable);
2611    p->zDestTable = 0;
2612  }
2613  if( zName==0 ) return;
2614  cQuote = quoteChar(zName);
2615  n = strlen30(zName);
2616  if( cQuote ) n += n+2;
2617  z = p->zDestTable = malloc( n+1 );
2618  shell_check_oom(z);
2619  n = 0;
2620  if( cQuote ) z[n++] = cQuote;
2621  for(i=0; zName[i]; i++){
2622    z[n++] = zName[i];
2623    if( zName[i]==cQuote ) z[n++] = cQuote;
2624  }
2625  if( cQuote ) z[n++] = cQuote;
2626  z[n] = 0;
2627}
2628
2629/*
2630** Maybe construct two lines of text that point out the position of a
2631** syntax error.  Return a pointer to the text, in memory obtained from
2632** sqlite3_malloc().  Or, if the most recent error does not involve a
2633** specific token that we can point to, return an empty string.
2634**
2635** In all cases, the memory returned is obtained from sqlite3_malloc64()
2636** and should be released by the caller invoking sqlite3_free().
2637*/
2638static char *shell_error_context(const char *zSql, sqlite3 *db){
2639  int iOffset;
2640  size_t len;
2641  char *zCode;
2642  char *zMsg;
2643  int i;
2644  if( db==0
2645   || zSql==0
2646   || (iOffset = sqlite3_error_offset(db))<0
2647  ){
2648    return sqlite3_mprintf("");
2649  }
2650  while( iOffset>50 ){
2651    iOffset--;
2652    zSql++;
2653    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
2654  }
2655  len = strlen(zSql);
2656  if( len>78 ){
2657    len = 78;
2658    while( (zSql[len]&0xc0)==0x80 ) len--;
2659  }
2660  zCode = sqlite3_mprintf("%.*s", len, zSql);
2661  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
2662  if( iOffset<25 ){
2663    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");
2664  }else{
2665    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");
2666  }
2667  return zMsg;
2668}
2669
2670
2671/*
2672** Execute a query statement that will generate SQL output.  Print
2673** the result columns, comma-separated, on a line and then add a
2674** semicolon terminator to the end of that line.
2675**
2676** If the number of columns is 1 and that column contains text "--"
2677** then write the semicolon on a separate line.  That way, if a
2678** "--" comment occurs at the end of the statement, the comment
2679** won't consume the semicolon terminator.
2680*/
2681static int run_table_dump_query(
2682  ShellState *p,           /* Query context */
2683  const char *zSelect      /* SELECT statement to extract content */
2684){
2685  sqlite3_stmt *pSelect;
2686  int rc;
2687  int nResult;
2688  int i;
2689  const char *z;
2690  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2691  if( rc!=SQLITE_OK || !pSelect ){
2692    char *zContext = shell_error_context(zSelect, p->db);
2693    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
2694                sqlite3_errmsg(p->db), zContext);
2695    sqlite3_free(zContext);
2696    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2697    return rc;
2698  }
2699  rc = sqlite3_step(pSelect);
2700  nResult = sqlite3_column_count(pSelect);
2701  while( rc==SQLITE_ROW ){
2702    z = (const char*)sqlite3_column_text(pSelect, 0);
2703    utf8_printf(p->out, "%s", z);
2704    for(i=1; i<nResult; i++){
2705      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2706    }
2707    if( z==0 ) z = "";
2708    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2709    if( z[0] ){
2710      raw_printf(p->out, "\n;\n");
2711    }else{
2712      raw_printf(p->out, ";\n");
2713    }
2714    rc = sqlite3_step(pSelect);
2715  }
2716  rc = sqlite3_finalize(pSelect);
2717  if( rc!=SQLITE_OK ){
2718    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2719                sqlite3_errmsg(p->db));
2720    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2721  }
2722  return rc;
2723}
2724
2725/*
2726** Allocate space and save off string indicating current error.
2727*/
2728static char *save_err_msg(
2729  sqlite3 *db,           /* Database to query */
2730  const char *zPhase,    /* When the error occcurs */
2731  int rc,                /* Error code returned from API */
2732  const char *zSql       /* SQL string, or NULL */
2733){
2734  char *zErr;
2735  char *zContext;
2736  sqlite3_str *pStr = sqlite3_str_new(0);
2737  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
2738  if( rc>1 ){
2739    sqlite3_str_appendf(pStr, " (%d)", rc);
2740  }
2741  zContext = shell_error_context(zSql, db);
2742  if( zContext ){
2743    sqlite3_str_appendall(pStr, zContext);
2744    sqlite3_free(zContext);
2745  }
2746  zErr = sqlite3_str_finish(pStr);
2747  shell_check_oom(zErr);
2748  return zErr;
2749}
2750
2751#ifdef __linux__
2752/*
2753** Attempt to display I/O stats on Linux using /proc/PID/io
2754*/
2755static void displayLinuxIoStats(FILE *out){
2756  FILE *in;
2757  char z[200];
2758  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2759  in = fopen(z, "rb");
2760  if( in==0 ) return;
2761  while( fgets(z, sizeof(z), in)!=0 ){
2762    static const struct {
2763      const char *zPattern;
2764      const char *zDesc;
2765    } aTrans[] = {
2766      { "rchar: ",                  "Bytes received by read():" },
2767      { "wchar: ",                  "Bytes sent to write():"    },
2768      { "syscr: ",                  "Read() system calls:"      },
2769      { "syscw: ",                  "Write() system calls:"     },
2770      { "read_bytes: ",             "Bytes read from storage:"  },
2771      { "write_bytes: ",            "Bytes written to storage:" },
2772      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2773    };
2774    int i;
2775    for(i=0; i<ArraySize(aTrans); i++){
2776      int n = strlen30(aTrans[i].zPattern);
2777      if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
2778        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2779        break;
2780      }
2781    }
2782  }
2783  fclose(in);
2784}
2785#endif
2786
2787/*
2788** Display a single line of status using 64-bit values.
2789*/
2790static void displayStatLine(
2791  ShellState *p,            /* The shell context */
2792  char *zLabel,             /* Label for this one line */
2793  char *zFormat,            /* Format for the result */
2794  int iStatusCtrl,          /* Which status to display */
2795  int bReset                /* True to reset the stats */
2796){
2797  sqlite3_int64 iCur = -1;
2798  sqlite3_int64 iHiwtr = -1;
2799  int i, nPercent;
2800  char zLine[200];
2801  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2802  for(i=0, nPercent=0; zFormat[i]; i++){
2803    if( zFormat[i]=='%' ) nPercent++;
2804  }
2805  if( nPercent>1 ){
2806    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2807  }else{
2808    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2809  }
2810  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2811}
2812
2813/*
2814** Display memory stats.
2815*/
2816static int display_stats(
2817  sqlite3 *db,                /* Database to query */
2818  ShellState *pArg,           /* Pointer to ShellState */
2819  int bReset                  /* True to reset the stats */
2820){
2821  int iCur;
2822  int iHiwtr;
2823  FILE *out;
2824  if( pArg==0 || pArg->out==0 ) return 0;
2825  out = pArg->out;
2826
2827  if( pArg->pStmt && pArg->statsOn==2 ){
2828    int nCol, i, x;
2829    sqlite3_stmt *pStmt = pArg->pStmt;
2830    char z[100];
2831    nCol = sqlite3_column_count(pStmt);
2832    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2833    for(i=0; i<nCol; i++){
2834      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2835      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2836#ifndef SQLITE_OMIT_DECLTYPE
2837      sqlite3_snprintf(30, z+x, "declared type:");
2838      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2839#endif
2840#ifdef SQLITE_ENABLE_COLUMN_METADATA
2841      sqlite3_snprintf(30, z+x, "database name:");
2842      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2843      sqlite3_snprintf(30, z+x, "table name:");
2844      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2845      sqlite3_snprintf(30, z+x, "origin name:");
2846      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2847#endif
2848    }
2849  }
2850
2851  if( pArg->statsOn==3 ){
2852    if( pArg->pStmt ){
2853      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2854      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2855    }
2856    return 0;
2857  }
2858
2859  displayStatLine(pArg, "Memory Used:",
2860     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2861  displayStatLine(pArg, "Number of Outstanding Allocations:",
2862     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2863  if( pArg->shellFlgs & SHFLG_Pagecache ){
2864    displayStatLine(pArg, "Number of Pcache Pages Used:",
2865       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2866  }
2867  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2868     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2869  displayStatLine(pArg, "Largest Allocation:",
2870     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2871  displayStatLine(pArg, "Largest Pcache Allocation:",
2872     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2873#ifdef YYTRACKMAXSTACKDEPTH
2874  displayStatLine(pArg, "Deepest Parser Stack:",
2875     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2876#endif
2877
2878  if( db ){
2879    if( pArg->shellFlgs & SHFLG_Lookaside ){
2880      iHiwtr = iCur = -1;
2881      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2882                        &iCur, &iHiwtr, bReset);
2883      raw_printf(pArg->out,
2884              "Lookaside Slots Used:                %d (max %d)\n",
2885              iCur, iHiwtr);
2886      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2887                        &iCur, &iHiwtr, bReset);
2888      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2889              iHiwtr);
2890      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2891                        &iCur, &iHiwtr, bReset);
2892      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2893              iHiwtr);
2894      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2895                        &iCur, &iHiwtr, bReset);
2896      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2897              iHiwtr);
2898    }
2899    iHiwtr = iCur = -1;
2900    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2901    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2902            iCur);
2903    iHiwtr = iCur = -1;
2904    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2905    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2906    iHiwtr = iCur = -1;
2907    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2908    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2909    iHiwtr = iCur = -1;
2910    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2911    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2912    iHiwtr = iCur = -1;
2913    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2914    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2915    iHiwtr = iCur = -1;
2916    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2917    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2918            iCur);
2919    iHiwtr = iCur = -1;
2920    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2921    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2922            iCur);
2923  }
2924
2925  if( pArg->pStmt ){
2926    int iHit, iMiss;
2927    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2928                               bReset);
2929    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2930    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2931    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2932    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2933    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2934    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);
2935    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);
2936    if( iHit || iMiss ){
2937      raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",
2938            iHit, iHit+iMiss);
2939    }
2940    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2941    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2942    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2943    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2944    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2945    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2946    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2947    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2948  }
2949
2950#ifdef __linux__
2951  displayLinuxIoStats(pArg->out);
2952#endif
2953
2954  /* Do not remove this machine readable comment: extra-stats-output-here */
2955
2956  return 0;
2957}
2958
2959/*
2960** Display scan stats.
2961*/
2962static void display_scanstats(
2963  sqlite3 *db,                    /* Database to query */
2964  ShellState *pArg                /* Pointer to ShellState */
2965){
2966#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2967  UNUSED_PARAMETER(db);
2968  UNUSED_PARAMETER(pArg);
2969#else
2970  int i, k, n, mx;
2971  raw_printf(pArg->out, "-------- scanstats --------\n");
2972  mx = 0;
2973  for(k=0; k<=mx; k++){
2974    double rEstLoop = 1.0;
2975    for(i=n=0; 1; i++){
2976      sqlite3_stmt *p = pArg->pStmt;
2977      sqlite3_int64 nLoop, nVisit;
2978      double rEst;
2979      int iSid;
2980      const char *zExplain;
2981      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2982        break;
2983      }
2984      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2985      if( iSid>mx ) mx = iSid;
2986      if( iSid!=k ) continue;
2987      if( n==0 ){
2988        rEstLoop = (double)nLoop;
2989        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2990      }
2991      n++;
2992      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2993      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2994      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2995      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2996      rEstLoop *= rEst;
2997      raw_printf(pArg->out,
2998          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2999          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
3000      );
3001    }
3002  }
3003  raw_printf(pArg->out, "---------------------------\n");
3004#endif
3005}
3006
3007/*
3008** Parameter azArray points to a zero-terminated array of strings. zStr
3009** points to a single nul-terminated string. Return non-zero if zStr
3010** is equal, according to strcmp(), to any of the strings in the array.
3011** Otherwise, return zero.
3012*/
3013static int str_in_array(const char *zStr, const char **azArray){
3014  int i;
3015  for(i=0; azArray[i]; i++){
3016    if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
3017  }
3018  return 0;
3019}
3020
3021/*
3022** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3023** and populate the ShellState.aiIndent[] array with the number of
3024** spaces each opcode should be indented before it is output.
3025**
3026** The indenting rules are:
3027**
3028**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3029**       all opcodes that occur between the p2 jump destination and the opcode
3030**       itself by 2 spaces.
3031**
3032**     * Do the previous for "Return" instructions for when P2 is positive.
3033**       See tag-20220407a in wherecode.c and vdbe.c.
3034**
3035**     * For each "Goto", if the jump destination is earlier in the program
3036**       and ends on one of:
3037**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
3038**       or if the P1 parameter is one instead of zero,
3039**       then indent all opcodes between the earlier instruction
3040**       and "Goto" by 2 spaces.
3041*/
3042static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3043  const char *zSql;               /* The text of the SQL statement */
3044  const char *z;                  /* Used to check if this is an EXPLAIN */
3045  int *abYield = 0;               /* True if op is an OP_Yield */
3046  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
3047  int iOp;                        /* Index of operation in p->aiIndent[] */
3048
3049  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3050                           "Return", 0 };
3051  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3052                            "Rewind", 0 };
3053  const char *azGoto[] = { "Goto", 0 };
3054
3055  /* Try to figure out if this is really an EXPLAIN statement. If this
3056  ** cannot be verified, return early.  */
3057  if( sqlite3_column_count(pSql)!=8 ){
3058    p->cMode = p->mode;
3059    return;
3060  }
3061  zSql = sqlite3_sql(pSql);
3062  if( zSql==0 ) return;
3063  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3064  if( sqlite3_strnicmp(z, "explain", 7) ){
3065    p->cMode = p->mode;
3066    return;
3067  }
3068
3069  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3070    int i;
3071    int iAddr = sqlite3_column_int(pSql, 0);
3072    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3073
3074    /* Set p2 to the P2 field of the current opcode. Then, assuming that
3075    ** p2 is an instruction address, set variable p2op to the index of that
3076    ** instruction in the aiIndent[] array. p2 and p2op may be different if
3077    ** the current instruction is part of a sub-program generated by an
3078    ** SQL trigger or foreign key.  */
3079    int p2 = sqlite3_column_int(pSql, 3);
3080    int p2op = (p2 + (iOp-iAddr));
3081
3082    /* Grow the p->aiIndent array as required */
3083    if( iOp>=nAlloc ){
3084      if( iOp==0 ){
3085        /* Do further verfication that this is explain output.  Abort if
3086        ** it is not */
3087        static const char *explainCols[] = {
3088           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3089        int jj;
3090        for(jj=0; jj<ArraySize(explainCols); jj++){
3091          if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3092            p->cMode = p->mode;
3093            sqlite3_reset(pSql);
3094            return;
3095          }
3096        }
3097      }
3098      nAlloc += 100;
3099      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3100      shell_check_oom(p->aiIndent);
3101      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3102      shell_check_oom(abYield);
3103    }
3104    abYield[iOp] = str_in_array(zOp, azYield);
3105    p->aiIndent[iOp] = 0;
3106    p->nIndent = iOp+1;
3107
3108    if( str_in_array(zOp, azNext) && p2op>0 ){
3109      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3110    }
3111    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3112     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3113    ){
3114      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3115    }
3116  }
3117
3118  p->iIndent = 0;
3119  sqlite3_free(abYield);
3120  sqlite3_reset(pSql);
3121}
3122
3123/*
3124** Free the array allocated by explain_data_prepare().
3125*/
3126static void explain_data_delete(ShellState *p){
3127  sqlite3_free(p->aiIndent);
3128  p->aiIndent = 0;
3129  p->nIndent = 0;
3130  p->iIndent = 0;
3131}
3132
3133/*
3134** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3135*/
3136static unsigned int savedSelectTrace;
3137static unsigned int savedWhereTrace;
3138static void disable_debug_trace_modes(void){
3139  unsigned int zero = 0;
3140  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3141  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3142  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3143  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3144}
3145static void restore_debug_trace_modes(void){
3146  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3147  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3148}
3149
3150/* Create the TEMP table used to store parameter bindings */
3151static void bind_table_init(ShellState *p){
3152  int wrSchema = 0;
3153  int defensiveMode = 0;
3154  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3155  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3156  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3157  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3158  sqlite3_exec(p->db,
3159    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3160    "  key TEXT PRIMARY KEY,\n"
3161    "  value\n"
3162    ") WITHOUT ROWID;",
3163    0, 0, 0);
3164  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3165  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3166}
3167
3168/*
3169** Bind parameters on a prepared statement.
3170**
3171** Parameter bindings are taken from a TEMP table of the form:
3172**
3173**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3174**    WITHOUT ROWID;
3175**
3176** No bindings occur if this table does not exist.  The name of the table
3177** begins with "sqlite_" so that it will not collide with ordinary application
3178** tables.  The table must be in the TEMP schema.
3179*/
3180static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3181  int nVar;
3182  int i;
3183  int rc;
3184  sqlite3_stmt *pQ = 0;
3185
3186  nVar = sqlite3_bind_parameter_count(pStmt);
3187  if( nVar==0 ) return;  /* Nothing to do */
3188  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3189                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3190    return; /* Parameter table does not exist */
3191  }
3192  rc = sqlite3_prepare_v2(pArg->db,
3193          "SELECT value FROM temp.sqlite_parameters"
3194          " WHERE key=?1", -1, &pQ, 0);
3195  if( rc || pQ==0 ) return;
3196  for(i=1; i<=nVar; i++){
3197    char zNum[30];
3198    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3199    if( zVar==0 ){
3200      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3201      zVar = zNum;
3202    }
3203    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3204    if( sqlite3_step(pQ)==SQLITE_ROW ){
3205      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3206    }else{
3207      sqlite3_bind_null(pStmt, i);
3208    }
3209    sqlite3_reset(pQ);
3210  }
3211  sqlite3_finalize(pQ);
3212}
3213
3214/*
3215** UTF8 box-drawing characters.  Imagine box lines like this:
3216**
3217**           1
3218**           |
3219**       4 --+-- 2
3220**           |
3221**           3
3222**
3223** Each box characters has between 2 and 4 of the lines leading from
3224** the center.  The characters are here identified by the numbers of
3225** their corresponding lines.
3226*/
3227#define BOX_24   "\342\224\200"  /* U+2500 --- */
3228#define BOX_13   "\342\224\202"  /* U+2502  |  */
3229#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3230#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3231#define BOX_12   "\342\224\224"  /* U+2514  '- */
3232#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3233#define BOX_123  "\342\224\234"  /* U+251c  |- */
3234#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3235#define BOX_234  "\342\224\254"  /* U+252c -,- */
3236#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3237#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3238
3239/* Draw horizontal line N characters long using unicode box
3240** characters
3241*/
3242static void print_box_line(FILE *out, int N){
3243  const char zDash[] =
3244      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3245      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3246  const int nDash = sizeof(zDash) - 1;
3247  N *= 3;
3248  while( N>nDash ){
3249    utf8_printf(out, zDash);
3250    N -= nDash;
3251  }
3252  utf8_printf(out, "%.*s", N, zDash);
3253}
3254
3255/*
3256** Draw a horizontal separator for a MODE_Box table.
3257*/
3258static void print_box_row_separator(
3259  ShellState *p,
3260  int nArg,
3261  const char *zSep1,
3262  const char *zSep2,
3263  const char *zSep3
3264){
3265  int i;
3266  if( nArg>0 ){
3267    utf8_printf(p->out, "%s", zSep1);
3268    print_box_line(p->out, p->actualWidth[0]+2);
3269    for(i=1; i<nArg; i++){
3270      utf8_printf(p->out, "%s", zSep2);
3271      print_box_line(p->out, p->actualWidth[i]+2);
3272    }
3273    utf8_printf(p->out, "%s", zSep3);
3274  }
3275  fputs("\n", p->out);
3276}
3277
3278/*
3279** z[] is a line of text that is to be displayed the .mode box or table or
3280** similar tabular formats.  z[] might contain control characters such
3281** as \n, \t, \f, or \r.
3282**
3283** Compute characters to display on the first line of z[].  Stop at the
3284** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
3285** from malloc()) of that first line, which caller should free sometime.
3286** Write anything to display on the next line into *pzTail.  If this is
3287** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3288*/
3289static char *translateForDisplayAndDup(
3290  const unsigned char *z,            /* Input text to be transformed */
3291  const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
3292  int mxWidth,                       /* Max width.  0 means no limit */
3293  u8 bWordWrap                       /* If true, avoid breaking mid-word */
3294){
3295  int i;                 /* Input bytes consumed */
3296  int j;                 /* Output bytes generated */
3297  int k;                 /* Input bytes to be displayed */
3298  int n;                 /* Output column number */
3299  unsigned char *zOut;   /* Output text */
3300
3301  if( z==0 ){
3302    *pzTail = 0;
3303    return 0;
3304  }
3305  if( mxWidth<0 ) mxWidth = -mxWidth;
3306  if( mxWidth==0 ) mxWidth = 1000000;
3307  i = j = n = 0;
3308  while( n<mxWidth ){
3309    if( z[i]>=' ' ){
3310      n++;
3311      do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
3312      continue;
3313    }
3314    if( z[i]=='\t' ){
3315      do{
3316        n++;
3317        j++;
3318      }while( (n&7)!=0 && n<mxWidth );
3319      i++;
3320      continue;
3321    }
3322    break;
3323  }
3324  if( n>=mxWidth && bWordWrap  ){
3325    /* Perhaps try to back up to a better place to break the line */
3326    for(k=i; k>i/2; k--){
3327      if( isspace(z[k-1]) ) break;
3328    }
3329    if( k<=i/2 ){
3330      for(k=i; k>i/2; k--){
3331        if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3332      }
3333    }
3334    if( k<=i/2 ){
3335      k = i;
3336    }else{
3337      i = k;
3338      while( z[i]==' ' ) i++;
3339    }
3340  }else{
3341    k = i;
3342  }
3343  if( n>=mxWidth && z[i]>=' ' ){
3344   *pzTail = &z[i];
3345  }else if( z[i]=='\r' && z[i+1]=='\n' ){
3346    *pzTail = z[i+2] ? &z[i+2] : 0;
3347  }else if( z[i]==0 || z[i+1]==0 ){
3348    *pzTail = 0;
3349  }else{
3350    *pzTail = &z[i+1];
3351  }
3352  zOut = malloc( j+1 );
3353  shell_check_oom(zOut);
3354  i = j = n = 0;
3355  while( i<k ){
3356    if( z[i]>=' ' ){
3357      n++;
3358      do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
3359      continue;
3360    }
3361    if( z[i]=='\t' ){
3362      do{
3363        n++;
3364        zOut[j++] = ' ';
3365      }while( (n&7)!=0 && n<mxWidth );
3366      i++;
3367      continue;
3368    }
3369    break;
3370  }
3371  zOut[j] = 0;
3372  return (char*)zOut;
3373}
3374
3375/* Extract the value of the i-th current column for pStmt as an SQL literal
3376** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
3377** the caller.
3378*/
3379static char *quoted_column(sqlite3_stmt *pStmt, int i){
3380  switch( sqlite3_column_type(pStmt, i) ){
3381    case SQLITE_NULL: {
3382      return sqlite3_mprintf("NULL");
3383    }
3384    case SQLITE_INTEGER:
3385    case SQLITE_FLOAT: {
3386      return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3387    }
3388    case SQLITE_TEXT: {
3389      return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3390    }
3391    case SQLITE_BLOB: {
3392      int j;
3393      sqlite3_str *pStr = sqlite3_str_new(0);
3394      const unsigned char *a = sqlite3_column_blob(pStmt,i);
3395      int n = sqlite3_column_bytes(pStmt,i);
3396      sqlite3_str_append(pStr, "x'", 2);
3397      for(j=0; j<n; j++){
3398        sqlite3_str_appendf(pStr, "%02x", a[j]);
3399      }
3400      sqlite3_str_append(pStr, "'", 1);
3401      return sqlite3_str_finish(pStr);
3402    }
3403  }
3404  return 0; /* Not reached */
3405}
3406
3407/*
3408** Run a prepared statement and output the result in one of the
3409** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3410** or MODE_Box.
3411**
3412** This is different from ordinary exec_prepared_stmt() in that
3413** it has to run the entire query and gather the results into memory
3414** first, in order to determine column widths, before providing
3415** any output.
3416*/
3417static void exec_prepared_stmt_columnar(
3418  ShellState *p,                        /* Pointer to ShellState */
3419  sqlite3_stmt *pStmt                   /* Statment to run */
3420){
3421  sqlite3_int64 nRow = 0;
3422  int nColumn = 0;
3423  char **azData = 0;
3424  sqlite3_int64 nAlloc = 0;
3425  char *abRowDiv = 0;
3426  const unsigned char *uz;
3427  const char *z;
3428  char **azQuoted = 0;
3429  int rc;
3430  sqlite3_int64 i, nData;
3431  int j, nTotal, w, n;
3432  const char *colSep = 0;
3433  const char *rowSep = 0;
3434  const unsigned char **azNextLine = 0;
3435  int bNextLine = 0;
3436  int bMultiLineRowExists = 0;
3437  int bw = p->cmOpts.bWordWrap;
3438  const char *zEmpty = "";
3439  const char *zShowNull = p->nullValue;
3440
3441  rc = sqlite3_step(pStmt);
3442  if( rc!=SQLITE_ROW ) return;
3443  nColumn = sqlite3_column_count(pStmt);
3444  nAlloc = nColumn*4;
3445  if( nAlloc<=0 ) nAlloc = 1;
3446  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3447  shell_check_oom(azData);
3448  azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
3449  shell_check_oom((void*)azNextLine);
3450  memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
3451  if( p->cmOpts.bQuote ){
3452    azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
3453    shell_check_oom(azQuoted);
3454    memset(azQuoted, 0, nColumn*sizeof(char*) );
3455  }
3456  abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
3457  shell_check_oom(abRowDiv);
3458  if( nColumn>p->nWidth ){
3459    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3460    shell_check_oom(p->colWidth);
3461    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3462    p->nWidth = nColumn;
3463    p->actualWidth = &p->colWidth[nColumn];
3464  }
3465  memset(p->actualWidth, 0, nColumn*sizeof(int));
3466  for(i=0; i<nColumn; i++){
3467    w = p->colWidth[i];
3468    if( w<0 ) w = -w;
3469    p->actualWidth[i] = w;
3470  }
3471  for(i=0; i<nColumn; i++){
3472    const unsigned char *zNotUsed;
3473    int wx = p->colWidth[i];
3474    if( wx==0 ){
3475      wx = p->cmOpts.iWrap;
3476    }
3477    if( wx<0 ) wx = -wx;
3478    uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
3479    azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
3480  }
3481  do{
3482    int useNextLine = bNextLine;
3483    bNextLine = 0;
3484    if( (nRow+2)*nColumn >= nAlloc ){
3485      nAlloc *= 2;
3486      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3487      shell_check_oom(azData);
3488      abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
3489      shell_check_oom(abRowDiv);
3490    }
3491    abRowDiv[nRow] = 1;
3492    nRow++;
3493    for(i=0; i<nColumn; i++){
3494      int wx = p->colWidth[i];
3495      if( wx==0 ){
3496        wx = p->cmOpts.iWrap;
3497      }
3498      if( wx<0 ) wx = -wx;
3499      if( useNextLine ){
3500        uz = azNextLine[i];
3501        if( uz==0 ) uz = (u8*)zEmpty;
3502      }else if( p->cmOpts.bQuote ){
3503        sqlite3_free(azQuoted[i]);
3504        azQuoted[i] = quoted_column(pStmt,i);
3505        uz = (const unsigned char*)azQuoted[i];
3506      }else{
3507        uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
3508        if( uz==0 ) uz = (u8*)zShowNull;
3509      }
3510      azData[nRow*nColumn + i]
3511        = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
3512      if( azNextLine[i] ){
3513        bNextLine = 1;
3514        abRowDiv[nRow-1] = 0;
3515        bMultiLineRowExists = 1;
3516      }
3517    }
3518  }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
3519  nTotal = nColumn*(nRow+1);
3520  for(i=0; i<nTotal; i++){
3521    z = azData[i];
3522    if( z==0 ) z = (char*)zEmpty;
3523    n = strlenChar(z);
3524    j = i%nColumn;
3525    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3526  }
3527  if( seenInterrupt ) goto columnar_end;
3528  if( nColumn==0 ) goto columnar_end;
3529  switch( p->cMode ){
3530    case MODE_Column: {
3531      colSep = "  ";
3532      rowSep = "\n";
3533      if( p->showHeader ){
3534        for(i=0; i<nColumn; i++){
3535          w = p->actualWidth[i];
3536          if( p->colWidth[i]<0 ) w = -w;
3537          utf8_width_print(p->out, w, azData[i]);
3538          fputs(i==nColumn-1?"\n":"  ", p->out);
3539        }
3540        for(i=0; i<nColumn; i++){
3541          print_dashes(p->out, p->actualWidth[i]);
3542          fputs(i==nColumn-1?"\n":"  ", p->out);
3543        }
3544      }
3545      break;
3546    }
3547    case MODE_Table: {
3548      colSep = " | ";
3549      rowSep = " |\n";
3550      print_row_separator(p, nColumn, "+");
3551      fputs("| ", p->out);
3552      for(i=0; i<nColumn; i++){
3553        w = p->actualWidth[i];
3554        n = strlenChar(azData[i]);
3555        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3556        fputs(i==nColumn-1?" |\n":" | ", p->out);
3557      }
3558      print_row_separator(p, nColumn, "+");
3559      break;
3560    }
3561    case MODE_Markdown: {
3562      colSep = " | ";
3563      rowSep = " |\n";
3564      fputs("| ", p->out);
3565      for(i=0; i<nColumn; i++){
3566        w = p->actualWidth[i];
3567        n = strlenChar(azData[i]);
3568        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3569        fputs(i==nColumn-1?" |\n":" | ", p->out);
3570      }
3571      print_row_separator(p, nColumn, "|");
3572      break;
3573    }
3574    case MODE_Box: {
3575      colSep = " " BOX_13 " ";
3576      rowSep = " " BOX_13 "\n";
3577      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3578      utf8_printf(p->out, BOX_13 " ");
3579      for(i=0; i<nColumn; i++){
3580        w = p->actualWidth[i];
3581        n = strlenChar(azData[i]);
3582        utf8_printf(p->out, "%*s%s%*s%s",
3583            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3584            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3585      }
3586      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3587      break;
3588    }
3589  }
3590  for(i=nColumn, j=0; i<nTotal; i++, j++){
3591    if( j==0 && p->cMode!=MODE_Column ){
3592      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3593    }
3594    z = azData[i];
3595    if( z==0 ) z = p->nullValue;
3596    w = p->actualWidth[j];
3597    if( p->colWidth[j]<0 ) w = -w;
3598    utf8_width_print(p->out, w, z);
3599    if( j==nColumn-1 ){
3600      utf8_printf(p->out, "%s", rowSep);
3601      if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
3602        if( p->cMode==MODE_Table ){
3603          print_row_separator(p, nColumn, "+");
3604        }else if( p->cMode==MODE_Box ){
3605          print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3606        }else if( p->cMode==MODE_Column ){
3607          raw_printf(p->out, "\n");
3608        }
3609      }
3610      j = -1;
3611      if( seenInterrupt ) goto columnar_end;
3612    }else{
3613      utf8_printf(p->out, "%s", colSep);
3614    }
3615  }
3616  if( p->cMode==MODE_Table ){
3617    print_row_separator(p, nColumn, "+");
3618  }else if( p->cMode==MODE_Box ){
3619    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3620  }
3621columnar_end:
3622  if( seenInterrupt ){
3623    utf8_printf(p->out, "Interrupt\n");
3624  }
3625  nData = (nRow+1)*nColumn;
3626  for(i=0; i<nData; i++){
3627    z = azData[i];
3628    if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
3629  }
3630  sqlite3_free(azData);
3631  sqlite3_free((void*)azNextLine);
3632  sqlite3_free(abRowDiv);
3633  if( azQuoted ){
3634    for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
3635    sqlite3_free(azQuoted);
3636  }
3637}
3638
3639/*
3640** Run a prepared statement
3641*/
3642static void exec_prepared_stmt(
3643  ShellState *pArg,                                /* Pointer to ShellState */
3644  sqlite3_stmt *pStmt                              /* Statment to run */
3645){
3646  int rc;
3647  sqlite3_uint64 nRow = 0;
3648
3649  if( pArg->cMode==MODE_Column
3650   || pArg->cMode==MODE_Table
3651   || pArg->cMode==MODE_Box
3652   || pArg->cMode==MODE_Markdown
3653  ){
3654    exec_prepared_stmt_columnar(pArg, pStmt);
3655    return;
3656  }
3657
3658  /* perform the first step.  this will tell us if we
3659  ** have a result set or not and how wide it is.
3660  */
3661  rc = sqlite3_step(pStmt);
3662  /* if we have a result set... */
3663  if( SQLITE_ROW == rc ){
3664    /* allocate space for col name ptr, value ptr, and type */
3665    int nCol = sqlite3_column_count(pStmt);
3666    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3667    if( !pData ){
3668      shell_out_of_memory();
3669    }else{
3670      char **azCols = (char **)pData;      /* Names of result columns */
3671      char **azVals = &azCols[nCol];       /* Results */
3672      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3673      int i, x;
3674      assert(sizeof(int) <= sizeof(char *));
3675      /* save off ptrs to column names */
3676      for(i=0; i<nCol; i++){
3677        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3678      }
3679      do{
3680        nRow++;
3681        /* extract the data and data types */
3682        for(i=0; i<nCol; i++){
3683          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3684          if( x==SQLITE_BLOB
3685           && pArg
3686           && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
3687          ){
3688            azVals[i] = "";
3689          }else{
3690            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3691          }
3692          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3693            rc = SQLITE_NOMEM;
3694            break; /* from for */
3695          }
3696        } /* end for */
3697
3698        /* if data and types extracted successfully... */
3699        if( SQLITE_ROW == rc ){
3700          /* call the supplied callback with the result row data */
3701          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3702            rc = SQLITE_ABORT;
3703          }else{
3704            rc = sqlite3_step(pStmt);
3705          }
3706        }
3707      } while( SQLITE_ROW == rc );
3708      sqlite3_free(pData);
3709      if( pArg->cMode==MODE_Json ){
3710        fputs("]\n", pArg->out);
3711      }else if( pArg->cMode==MODE_Count ){
3712        char zBuf[200];
3713        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
3714                         nRow, nRow!=1 ? "s" : "");
3715        printf("%s", zBuf);
3716      }
3717    }
3718  }
3719}
3720
3721#ifndef SQLITE_OMIT_VIRTUALTABLE
3722/*
3723** This function is called to process SQL if the previous shell command
3724** was ".expert". It passes the SQL in the second argument directly to
3725** the sqlite3expert object.
3726**
3727** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3728** code. In this case, (*pzErr) may be set to point to a buffer containing
3729** an English language error message. It is the responsibility of the
3730** caller to eventually free this buffer using sqlite3_free().
3731*/
3732static int expertHandleSQL(
3733  ShellState *pState,
3734  const char *zSql,
3735  char **pzErr
3736){
3737  assert( pState->expert.pExpert );
3738  assert( pzErr==0 || *pzErr==0 );
3739  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3740}
3741
3742/*
3743** This function is called either to silently clean up the object
3744** created by the ".expert" command (if bCancel==1), or to generate a
3745** report from it and then clean it up (if bCancel==0).
3746**
3747** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3748** code. In this case, (*pzErr) may be set to point to a buffer containing
3749** an English language error message. It is the responsibility of the
3750** caller to eventually free this buffer using sqlite3_free().
3751*/
3752static int expertFinish(
3753  ShellState *pState,
3754  int bCancel,
3755  char **pzErr
3756){
3757  int rc = SQLITE_OK;
3758  sqlite3expert *p = pState->expert.pExpert;
3759  assert( p );
3760  assert( bCancel || pzErr==0 || *pzErr==0 );
3761  if( bCancel==0 ){
3762    FILE *out = pState->out;
3763    int bVerbose = pState->expert.bVerbose;
3764
3765    rc = sqlite3_expert_analyze(p, pzErr);
3766    if( rc==SQLITE_OK ){
3767      int nQuery = sqlite3_expert_count(p);
3768      int i;
3769
3770      if( bVerbose ){
3771        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3772        raw_printf(out, "-- Candidates -----------------------------\n");
3773        raw_printf(out, "%s\n", zCand);
3774      }
3775      for(i=0; i<nQuery; i++){
3776        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3777        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3778        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3779        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3780        if( bVerbose ){
3781          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3782          raw_printf(out, "%s\n\n", zSql);
3783        }
3784        raw_printf(out, "%s\n", zIdx);
3785        raw_printf(out, "%s\n", zEQP);
3786      }
3787    }
3788  }
3789  sqlite3_expert_destroy(p);
3790  pState->expert.pExpert = 0;
3791  return rc;
3792}
3793
3794/*
3795** Implementation of ".expert" dot command.
3796*/
3797static int expertDotCommand(
3798  ShellState *pState,             /* Current shell tool state */
3799  char **azArg,                   /* Array of arguments passed to dot command */
3800  int nArg                        /* Number of entries in azArg[] */
3801){
3802  int rc = SQLITE_OK;
3803  char *zErr = 0;
3804  int i;
3805  int iSample = 0;
3806
3807  assert( pState->expert.pExpert==0 );
3808  memset(&pState->expert, 0, sizeof(ExpertInfo));
3809
3810  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3811    char *z = azArg[i];
3812    int n;
3813    if( z[0]=='-' && z[1]=='-' ) z++;
3814    n = strlen30(z);
3815    if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
3816      pState->expert.bVerbose = 1;
3817    }
3818    else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
3819      if( i==(nArg-1) ){
3820        raw_printf(stderr, "option requires an argument: %s\n", z);
3821        rc = SQLITE_ERROR;
3822      }else{
3823        iSample = (int)integerValue(azArg[++i]);
3824        if( iSample<0 || iSample>100 ){
3825          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3826          rc = SQLITE_ERROR;
3827        }
3828      }
3829    }
3830    else{
3831      raw_printf(stderr, "unknown option: %s\n", z);
3832      rc = SQLITE_ERROR;
3833    }
3834  }
3835
3836  if( rc==SQLITE_OK ){
3837    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3838    if( pState->expert.pExpert==0 ){
3839      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
3840      rc = SQLITE_ERROR;
3841    }else{
3842      sqlite3_expert_config(
3843          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3844      );
3845    }
3846  }
3847  sqlite3_free(zErr);
3848
3849  return rc;
3850}
3851#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3852
3853/*
3854** Execute a statement or set of statements.  Print
3855** any result rows/columns depending on the current mode
3856** set via the supplied callback.
3857**
3858** This is very similar to SQLite's built-in sqlite3_exec()
3859** function except it takes a slightly different callback
3860** and callback data argument.
3861*/
3862static int shell_exec(
3863  ShellState *pArg,                         /* Pointer to ShellState */
3864  const char *zSql,                         /* SQL to be evaluated */
3865  char **pzErrMsg                           /* Error msg written here */
3866){
3867  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3868  int rc = SQLITE_OK;             /* Return Code */
3869  int rc2;
3870  const char *zLeftover;          /* Tail of unprocessed SQL */
3871  sqlite3 *db = pArg->db;
3872
3873  if( pzErrMsg ){
3874    *pzErrMsg = NULL;
3875  }
3876
3877#ifndef SQLITE_OMIT_VIRTUALTABLE
3878  if( pArg->expert.pExpert ){
3879    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3880    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3881  }
3882#endif
3883
3884  while( zSql[0] && (SQLITE_OK == rc) ){
3885    static const char *zStmtSql;
3886    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3887    if( SQLITE_OK != rc ){
3888      if( pzErrMsg ){
3889        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
3890      }
3891    }else{
3892      if( !pStmt ){
3893        /* this happens for a comment or white-space */
3894        zSql = zLeftover;
3895        while( IsSpace(zSql[0]) ) zSql++;
3896        continue;
3897      }
3898      zStmtSql = sqlite3_sql(pStmt);
3899      if( zStmtSql==0 ) zStmtSql = "";
3900      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3901
3902      /* save off the prepared statment handle and reset row count */
3903      if( pArg ){
3904        pArg->pStmt = pStmt;
3905        pArg->cnt = 0;
3906      }
3907
3908      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3909      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3910        sqlite3_stmt *pExplain;
3911        char *zEQP;
3912        int triggerEQP = 0;
3913        disable_debug_trace_modes();
3914        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3915        if( pArg->autoEQP>=AUTOEQP_trigger ){
3916          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3917        }
3918        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3919        shell_check_oom(zEQP);
3920        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3921        if( rc==SQLITE_OK ){
3922          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3923            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3924            int iEqpId = sqlite3_column_int(pExplain, 0);
3925            int iParentId = sqlite3_column_int(pExplain, 1);
3926            if( zEQPLine==0 ) zEQPLine = "";
3927            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3928            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3929          }
3930          eqp_render(pArg);
3931        }
3932        sqlite3_finalize(pExplain);
3933        sqlite3_free(zEQP);
3934        if( pArg->autoEQP>=AUTOEQP_full ){
3935          /* Also do an EXPLAIN for ".eqp full" mode */
3936          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3937          shell_check_oom(zEQP);
3938          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3939          if( rc==SQLITE_OK ){
3940            pArg->cMode = MODE_Explain;
3941            explain_data_prepare(pArg, pExplain);
3942            exec_prepared_stmt(pArg, pExplain);
3943            explain_data_delete(pArg);
3944          }
3945          sqlite3_finalize(pExplain);
3946          sqlite3_free(zEQP);
3947        }
3948        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3949          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3950          /* Reprepare pStmt before reactiving trace modes */
3951          sqlite3_finalize(pStmt);
3952          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3953          if( pArg ) pArg->pStmt = pStmt;
3954        }
3955        restore_debug_trace_modes();
3956      }
3957
3958      if( pArg ){
3959        pArg->cMode = pArg->mode;
3960        if( pArg->autoExplain ){
3961          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3962            pArg->cMode = MODE_Explain;
3963          }
3964          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3965            pArg->cMode = MODE_EQP;
3966          }
3967        }
3968
3969        /* If the shell is currently in ".explain" mode, gather the extra
3970        ** data required to add indents to the output.*/
3971        if( pArg->cMode==MODE_Explain ){
3972          explain_data_prepare(pArg, pStmt);
3973        }
3974      }
3975
3976      bind_prepared_stmt(pArg, pStmt);
3977      exec_prepared_stmt(pArg, pStmt);
3978      explain_data_delete(pArg);
3979      eqp_render(pArg);
3980
3981      /* print usage stats if stats on */
3982      if( pArg && pArg->statsOn ){
3983        display_stats(db, pArg, 0);
3984      }
3985
3986      /* print loop-counters if required */
3987      if( pArg && pArg->scanstatsOn ){
3988        display_scanstats(db, pArg);
3989      }
3990
3991      /* Finalize the statement just executed. If this fails, save a
3992      ** copy of the error message. Otherwise, set zSql to point to the
3993      ** next statement to execute. */
3994      rc2 = sqlite3_finalize(pStmt);
3995      if( rc!=SQLITE_NOMEM ) rc = rc2;
3996      if( rc==SQLITE_OK ){
3997        zSql = zLeftover;
3998        while( IsSpace(zSql[0]) ) zSql++;
3999      }else if( pzErrMsg ){
4000        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
4001      }
4002
4003      /* clear saved stmt handle */
4004      if( pArg ){
4005        pArg->pStmt = NULL;
4006      }
4007    }
4008  } /* end while */
4009
4010  return rc;
4011}
4012
4013/*
4014** Release memory previously allocated by tableColumnList().
4015*/
4016static void freeColumnList(char **azCol){
4017  int i;
4018  for(i=1; azCol[i]; i++){
4019    sqlite3_free(azCol[i]);
4020  }
4021  /* azCol[0] is a static string */
4022  sqlite3_free(azCol);
4023}
4024
4025/*
4026** Return a list of pointers to strings which are the names of all
4027** columns in table zTab.   The memory to hold the names is dynamically
4028** allocated and must be released by the caller using a subsequent call
4029** to freeColumnList().
4030**
4031** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
4032** value that needs to be preserved, then azCol[0] is filled in with the
4033** name of the rowid column.
4034**
4035** The first regular column in the table is azCol[1].  The list is terminated
4036** by an entry with azCol[i]==0.
4037*/
4038static char **tableColumnList(ShellState *p, const char *zTab){
4039  char **azCol = 0;
4040  sqlite3_stmt *pStmt;
4041  char *zSql;
4042  int nCol = 0;
4043  int nAlloc = 0;
4044  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
4045  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
4046  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4047  int rc;
4048
4049  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4050  shell_check_oom(zSql);
4051  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4052  sqlite3_free(zSql);
4053  if( rc ) return 0;
4054  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4055    if( nCol>=nAlloc-2 ){
4056      nAlloc = nAlloc*2 + nCol + 10;
4057      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4058      shell_check_oom(azCol);
4059    }
4060    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4061    shell_check_oom(azCol[nCol]);
4062    if( sqlite3_column_int(pStmt, 5) ){
4063      nPK++;
4064      if( nPK==1
4065       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4066                          "INTEGER")==0
4067      ){
4068        isIPK = 1;
4069      }else{
4070        isIPK = 0;
4071      }
4072    }
4073  }
4074  sqlite3_finalize(pStmt);
4075  if( azCol==0 ) return 0;
4076  azCol[0] = 0;
4077  azCol[nCol+1] = 0;
4078
4079  /* The decision of whether or not a rowid really needs to be preserved
4080  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
4081  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
4082  ** rowids on tables where the rowid is inaccessible because there are other
4083  ** columns in the table named "rowid", "_rowid_", and "oid".
4084  */
4085  if( preserveRowid && isIPK ){
4086    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4087    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
4088    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4089    ** ROWID aliases.  To distinguish these cases, check to see if
4090    ** there is a "pk" entry in "PRAGMA index_list".  There will be
4091    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4092    */
4093    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4094                           " WHERE origin='pk'", zTab);
4095    shell_check_oom(zSql);
4096    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4097    sqlite3_free(zSql);
4098    if( rc ){
4099      freeColumnList(azCol);
4100      return 0;
4101    }
4102    rc = sqlite3_step(pStmt);
4103    sqlite3_finalize(pStmt);
4104    preserveRowid = rc==SQLITE_ROW;
4105  }
4106  if( preserveRowid ){
4107    /* Only preserve the rowid if we can find a name to use for the
4108    ** rowid */
4109    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4110    int i, j;
4111    for(j=0; j<3; j++){
4112      for(i=1; i<=nCol; i++){
4113        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4114      }
4115      if( i>nCol ){
4116        /* At this point, we know that azRowid[j] is not the name of any
4117        ** ordinary column in the table.  Verify that azRowid[j] is a valid
4118        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
4119        ** tables will fail this last check */
4120        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4121        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4122        break;
4123      }
4124    }
4125  }
4126  return azCol;
4127}
4128
4129/*
4130** Toggle the reverse_unordered_selects setting.
4131*/
4132static void toggleSelectOrder(sqlite3 *db){
4133  sqlite3_stmt *pStmt = 0;
4134  int iSetting = 0;
4135  char zStmt[100];
4136  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4137  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4138    iSetting = sqlite3_column_int(pStmt, 0);
4139  }
4140  sqlite3_finalize(pStmt);
4141  sqlite3_snprintf(sizeof(zStmt), zStmt,
4142       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4143  sqlite3_exec(db, zStmt, 0, 0, 0);
4144}
4145
4146/*
4147** This is a different callback routine used for dumping the database.
4148** Each row received by this callback consists of a table name,
4149** the table type ("index" or "table") and SQL to create the table.
4150** This routine should print text sufficient to recreate the table.
4151*/
4152static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4153  int rc;
4154  const char *zTable;
4155  const char *zType;
4156  const char *zSql;
4157  ShellState *p = (ShellState *)pArg;
4158  int dataOnly;
4159  int noSys;
4160
4161  UNUSED_PARAMETER(azNotUsed);
4162  if( nArg!=3 || azArg==0 ) return 0;
4163  zTable = azArg[0];
4164  zType = azArg[1];
4165  zSql = azArg[2];
4166  if( zTable==0 ) return 0;
4167  if( zType==0 ) return 0;
4168  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4169  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4170
4171  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4172    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4173  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4174    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4175  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
4176    return 0;
4177  }else if( dataOnly ){
4178    /* no-op */
4179  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4180    char *zIns;
4181    if( !p->writableSchema ){
4182      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4183      p->writableSchema = 1;
4184    }
4185    zIns = sqlite3_mprintf(
4186       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4187       "VALUES('table','%q','%q',0,'%q');",
4188       zTable, zTable, zSql);
4189    shell_check_oom(zIns);
4190    utf8_printf(p->out, "%s\n", zIns);
4191    sqlite3_free(zIns);
4192    return 0;
4193  }else{
4194    printSchemaLine(p->out, zSql, ";\n");
4195  }
4196
4197  if( cli_strcmp(zType, "table")==0 ){
4198    ShellText sSelect;
4199    ShellText sTable;
4200    char **azCol;
4201    int i;
4202    char *savedDestTable;
4203    int savedMode;
4204
4205    azCol = tableColumnList(p, zTable);
4206    if( azCol==0 ){
4207      p->nErr++;
4208      return 0;
4209    }
4210
4211    /* Always quote the table name, even if it appears to be pure ascii,
4212    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
4213    initText(&sTable);
4214    appendText(&sTable, zTable, quoteChar(zTable));
4215    /* If preserving the rowid, add a column list after the table name.
4216    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4217    ** instead of the usual "INSERT INTO tab VALUES(...)".
4218    */
4219    if( azCol[0] ){
4220      appendText(&sTable, "(", 0);
4221      appendText(&sTable, azCol[0], 0);
4222      for(i=1; azCol[i]; i++){
4223        appendText(&sTable, ",", 0);
4224        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4225      }
4226      appendText(&sTable, ")", 0);
4227    }
4228
4229    /* Build an appropriate SELECT statement */
4230    initText(&sSelect);
4231    appendText(&sSelect, "SELECT ", 0);
4232    if( azCol[0] ){
4233      appendText(&sSelect, azCol[0], 0);
4234      appendText(&sSelect, ",", 0);
4235    }
4236    for(i=1; azCol[i]; i++){
4237      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4238      if( azCol[i+1] ){
4239        appendText(&sSelect, ",", 0);
4240      }
4241    }
4242    freeColumnList(azCol);
4243    appendText(&sSelect, " FROM ", 0);
4244    appendText(&sSelect, zTable, quoteChar(zTable));
4245
4246    savedDestTable = p->zDestTable;
4247    savedMode = p->mode;
4248    p->zDestTable = sTable.z;
4249    p->mode = p->cMode = MODE_Insert;
4250    rc = shell_exec(p, sSelect.z, 0);
4251    if( (rc&0xff)==SQLITE_CORRUPT ){
4252      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4253      toggleSelectOrder(p->db);
4254      shell_exec(p, sSelect.z, 0);
4255      toggleSelectOrder(p->db);
4256    }
4257    p->zDestTable = savedDestTable;
4258    p->mode = savedMode;
4259    freeText(&sTable);
4260    freeText(&sSelect);
4261    if( rc ) p->nErr++;
4262  }
4263  return 0;
4264}
4265
4266/*
4267** Run zQuery.  Use dump_callback() as the callback routine so that
4268** the contents of the query are output as SQL statements.
4269**
4270** If we get a SQLITE_CORRUPT error, rerun the query after appending
4271** "ORDER BY rowid DESC" to the end.
4272*/
4273static int run_schema_dump_query(
4274  ShellState *p,
4275  const char *zQuery
4276){
4277  int rc;
4278  char *zErr = 0;
4279  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4280  if( rc==SQLITE_CORRUPT ){
4281    char *zQ2;
4282    int len = strlen30(zQuery);
4283    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4284    if( zErr ){
4285      utf8_printf(p->out, "/****** %s ******/\n", zErr);
4286      sqlite3_free(zErr);
4287      zErr = 0;
4288    }
4289    zQ2 = malloc( len+100 );
4290    if( zQ2==0 ) return rc;
4291    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4292    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4293    if( rc ){
4294      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4295    }else{
4296      rc = SQLITE_CORRUPT;
4297    }
4298    sqlite3_free(zErr);
4299    free(zQ2);
4300  }
4301  return rc;
4302}
4303
4304/*
4305** Text of help messages.
4306**
4307** The help text for each individual command begins with a line that starts
4308** with ".".  Subsequent lines are supplemental information.
4309**
4310** There must be two or more spaces between the end of the command and the
4311** start of the description of what that command does.
4312*/
4313static const char *(azHelp[]) = {
4314#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4315  && !defined(SQLITE_SHELL_FIDDLE)
4316  ".archive ...             Manage SQL archives",
4317  "   Each command must have exactly one of the following options:",
4318  "     -c, --create               Create a new archive",
4319  "     -u, --update               Add or update files with changed mtime",
4320  "     -i, --insert               Like -u but always add even if unchanged",
4321  "     -r, --remove               Remove files from archive",
4322  "     -t, --list                 List contents of archive",
4323  "     -x, --extract              Extract files from archive",
4324  "   Optional arguments:",
4325  "     -v, --verbose              Print each filename as it is processed",
4326  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
4327  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
4328  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
4329  "     -g, --glob                 Use glob matching for names in archive",
4330  "     -n, --dryrun               Show the SQL that would have occurred",
4331  "   Examples:",
4332  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
4333  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
4334  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
4335  "   See also:",
4336  "      http://sqlite.org/cli.html#sqlite_archive_support",
4337#endif
4338#ifndef SQLITE_OMIT_AUTHORIZATION
4339  ".auth ON|OFF             Show authorizer callbacks",
4340#endif
4341#ifndef SQLITE_SHELL_FIDDLE
4342  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
4343  "   Options:",
4344  "       --append            Use the appendvfs",
4345  "       --async             Write to FILE without journal and fsync()",
4346#endif
4347  ".bail on|off             Stop after hitting an error.  Default OFF",
4348  ".binary on|off           Turn binary output on or off.  Default OFF",
4349#ifndef SQLITE_SHELL_FIDDLE
4350  ".cd DIRECTORY            Change the working directory to DIRECTORY",
4351#endif
4352  ".changes on|off          Show number of rows changed by SQL",
4353#ifndef SQLITE_SHELL_FIDDLE
4354  ".check GLOB              Fail if output since .testcase does not match",
4355  ".clone NEWDB             Clone data into NEWDB from the existing database",
4356#endif
4357  ".connection [close] [#]  Open or close an auxiliary database connection",
4358  ".databases               List names and files of attached databases",
4359  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4360#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4361  ".dbinfo ?DB?             Show status information about the database",
4362#endif
4363  ".dump ?OBJECTS?          Render database content as SQL",
4364  "   Options:",
4365  "     --data-only            Output only INSERT statements",
4366  "     --newlines             Allow unescaped newline characters in output",
4367  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4368  "     --preserve-rowids      Include ROWID values in the output",
4369  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4370  "   Additional LIKE patterns can be given in subsequent arguments",
4371  ".echo on|off             Turn command echo on or off",
4372  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4373  "   Other Modes:",
4374#ifdef SQLITE_DEBUG
4375  "      test                  Show raw EXPLAIN QUERY PLAN output",
4376  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4377#endif
4378  "      trigger               Like \"full\" but also show trigger bytecode",
4379#ifndef SQLITE_SHELL_FIDDLE
4380  ".excel                   Display the output of next command in spreadsheet",
4381  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4382#endif
4383#ifndef SQLITE_SHELL_FIDDLE
4384  ".exit ?CODE?             Exit this program with return-code CODE",
4385#endif
4386  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4387  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4388  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4389  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4390  "   --help                  Show CMD details",
4391  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4392  ".headers on|off          Turn display of headers on or off",
4393  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4394#ifndef SQLITE_SHELL_FIDDLE
4395  ".import FILE TABLE       Import data from FILE into TABLE",
4396  "   Options:",
4397  "     --ascii               Use \\037 and \\036 as column and row separators",
4398  "     --csv                 Use , and \\n as column and row separators",
4399  "     --skip N              Skip the first N rows of input",
4400  "     --schema S            Target table to be S.TABLE",
4401  "     -v                    \"Verbose\" - increase auxiliary output",
4402  "   Notes:",
4403  "     *  If TABLE does not exist, it is created.  The first row of input",
4404  "        determines the column names.",
4405  "     *  If neither --csv or --ascii are used, the input mode is derived",
4406  "        from the \".mode\" output mode",
4407  "     *  If FILE begins with \"|\" then it is a command that generates the",
4408  "        input text.",
4409#endif
4410#ifndef SQLITE_OMIT_TEST_CONTROL
4411  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4412#endif
4413  ".indexes ?TABLE?         Show names of indexes",
4414  "                           If TABLE is specified, only show indexes for",
4415  "                           tables matching TABLE using the LIKE operator.",
4416#ifdef SQLITE_ENABLE_IOTRACE
4417  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4418#endif
4419  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4420  ".lint OPTIONS            Report potential schema issues.",
4421  "     Options:",
4422  "        fkey-indexes     Find missing foreign key indexes",
4423#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4424  ".load FILE ?ENTRY?       Load an extension library",
4425#endif
4426#ifndef SQLITE_SHELL_FIDDLE
4427  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4428#endif
4429  ".mode MODE ?OPTIONS?     Set output mode",
4430  "   MODE is one of:",
4431  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
4432  "     box         Tables using unicode box-drawing characters",
4433  "     csv         Comma-separated values",
4434  "     column      Output in columns.  (See .width)",
4435  "     html        HTML <table> code",
4436  "     insert      SQL insert statements for TABLE",
4437  "     json        Results in a JSON array",
4438  "     line        One value per line",
4439  "     list        Values delimited by \"|\"",
4440  "     markdown    Markdown table format",
4441  "     qbox        Shorthand for \"box --wrap 60 --quote\"",
4442  "     quote       Escape answers as for SQL",
4443  "     table       ASCII-art table",
4444  "     tabs        Tab-separated values",
4445  "     tcl         TCL list elements",
4446  "   OPTIONS: (for columnar modes or insert mode):",
4447  "     --wrap N       Wrap output lines to no longer than N characters",
4448  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
4449  "     --ww           Shorthand for \"--wordwrap 1\"",
4450  "     --quote        Quote output text as SQL literals",
4451  "     --noquote      Do not quote output text",
4452  "     TABLE          The name of SQL table used for \"insert\" mode",
4453#ifndef SQLITE_SHELL_FIDDLE
4454  ".nonce STRING            Suspend safe mode for one command if nonce matches",
4455#endif
4456  ".nullvalue STRING        Use STRING in place of NULL values",
4457#ifndef SQLITE_SHELL_FIDDLE
4458  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4459  "     If FILE begins with '|' then open as a pipe",
4460  "       --bom  Put a UTF8 byte-order mark at the beginning",
4461  "       -e     Send output to the system text editor",
4462  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4463  /* Note that .open is (partially) available in WASM builds but is
4464  ** currently only intended to be used by the fiddle tool, not
4465  ** end users, so is "undocumented." */
4466  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4467  "     Options:",
4468  "        --append        Use appendvfs to append database to the end of FILE",
4469#endif
4470#ifndef SQLITE_OMIT_DESERIALIZE
4471  "        --deserialize   Load into memory using sqlite3_deserialize()",
4472  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4473  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4474#endif
4475  "        --new           Initialize FILE to an empty database",
4476  "        --nofollow      Do not follow symbolic links",
4477  "        --readonly      Open FILE readonly",
4478  "        --zip           FILE is a ZIP archive",
4479#ifndef SQLITE_SHELL_FIDDLE
4480  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4481  "   If FILE begins with '|' then open it as a pipe.",
4482  "   Options:",
4483  "     --bom                 Prefix output with a UTF8 byte-order mark",
4484  "     -e                    Send output to the system text editor",
4485  "     -x                    Send output as CSV to a spreadsheet",
4486#endif
4487  ".parameter CMD ...       Manage SQL parameter bindings",
4488  "   clear                   Erase all bindings",
4489  "   init                    Initialize the TEMP table that holds bindings",
4490  "   list                    List the current parameter bindings",
4491  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4492  "                           PARAMETER should start with one of: $ : @ ?",
4493  "   unset PARAMETER         Remove PARAMETER from the binding table",
4494  ".print STRING...         Print literal STRING",
4495#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4496  ".progress N              Invoke progress handler after every N opcodes",
4497  "   --limit N                 Interrupt after N progress callbacks",
4498  "   --once                    Do no more than one progress interrupt",
4499  "   --quiet|-q                No output except at interrupts",
4500  "   --reset                   Reset the count for each input and interrupt",
4501#endif
4502  ".prompt MAIN CONTINUE    Replace the standard prompts",
4503#ifndef SQLITE_SHELL_FIDDLE
4504  ".quit                    Exit this program",
4505  ".read FILE               Read input from FILE or command output",
4506  "    If FILE begins with \"|\", it is a command that generates the input.",
4507#endif
4508#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4509  ".recover                 Recover as much data as possible from corrupt db.",
4510  "   --freelist-corrupt       Assume the freelist is corrupt",
4511  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4512  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4513  "   --no-rowids              Do not attempt to recover rowid values",
4514  "                            that are not also INTEGER PRIMARY KEYs",
4515#endif
4516#ifndef SQLITE_SHELL_FIDDLE
4517  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4518  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
4519#endif
4520  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4521  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4522  "   Options:",
4523  "      --indent             Try to pretty-print the schema",
4524  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4525  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4526  "    Options:",
4527  "       --init               Create a new SELFTEST table",
4528  "       -v                   Verbose output",
4529  ".separator COL ?ROW?     Change the column and row separators",
4530#if defined(SQLITE_ENABLE_SESSION)
4531  ".session ?NAME? CMD ...  Create or control sessions",
4532  "   Subcommands:",
4533  "     attach TABLE             Attach TABLE",
4534  "     changeset FILE           Write a changeset into FILE",
4535  "     close                    Close one session",
4536  "     enable ?BOOLEAN?         Set or query the enable bit",
4537  "     filter GLOB...           Reject tables matching GLOBs",
4538  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4539  "     isempty                  Query whether the session is empty",
4540  "     list                     List currently open session names",
4541  "     open DB NAME             Open a new session on DB",
4542  "     patchset FILE            Write a patchset into FILE",
4543  "   If ?NAME? is omitted, the first defined session is used.",
4544#endif
4545  ".sha3sum ...             Compute a SHA3 hash of database content",
4546  "    Options:",
4547  "      --schema              Also hash the sqlite_schema table",
4548  "      --sha3-224            Use the sha3-224 algorithm",
4549  "      --sha3-256            Use the sha3-256 algorithm (default)",
4550  "      --sha3-384            Use the sha3-384 algorithm",
4551  "      --sha3-512            Use the sha3-512 algorithm",
4552  "    Any other argument is a LIKE pattern for tables to hash",
4553#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4554  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4555#endif
4556  ".show                    Show the current values for various settings",
4557  ".stats ?ARG?             Show stats or turn stats on or off",
4558  "   off                      Turn off automatic stat display",
4559  "   on                       Turn on automatic stat display",
4560  "   stmt                     Show statement stats",
4561  "   vmstep                   Show the virtual machine step count only",
4562#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4563  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4564#endif
4565  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4566#ifndef SQLITE_SHELL_FIDDLE
4567  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4568#endif
4569  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4570  "                           Run \".testctrl\" with no arguments for details",
4571  ".timeout MS              Try opening locked tables for MS milliseconds",
4572  ".timer on|off            Turn SQL timer on or off",
4573#ifndef SQLITE_OMIT_TRACE
4574  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4575  "    FILE                    Send output to FILE",
4576  "    stdout                  Send output to stdout",
4577  "    stderr                  Send output to stderr",
4578  "    off                     Disable tracing",
4579  "    --expanded              Expand query parameters",
4580#ifdef SQLITE_ENABLE_NORMALIZE
4581  "    --normalized            Normal the SQL statements",
4582#endif
4583  "    --plain                 Show SQL as it is input",
4584  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4585  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4586  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4587  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4588#endif /* SQLITE_OMIT_TRACE */
4589#ifdef SQLITE_DEBUG
4590  ".unmodule NAME ...       Unregister virtual table modules",
4591  "    --allexcept             Unregister everything except those named",
4592#endif
4593  ".vfsinfo ?AUX?           Information about the top-level VFS",
4594  ".vfslist                 List all available VFSes",
4595  ".vfsname ?AUX?           Print the name of the VFS stack",
4596  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4597  "     Negative values right-justify",
4598};
4599
4600/*
4601** Output help text.
4602**
4603** zPattern describes the set of commands for which help text is provided.
4604** If zPattern is NULL, then show all commands, but only give a one-line
4605** description of each.
4606**
4607** Return the number of matches.
4608*/
4609static int showHelp(FILE *out, const char *zPattern){
4610  int i = 0;
4611  int j = 0;
4612  int n = 0;
4613  char *zPat;
4614  if( zPattern==0
4615   || zPattern[0]=='0'
4616   || cli_strcmp(zPattern,"-a")==0
4617   || cli_strcmp(zPattern,"-all")==0
4618   || cli_strcmp(zPattern,"--all")==0
4619  ){
4620    /* Show all commands, but only one line per command */
4621    if( zPattern==0 ) zPattern = "";
4622    for(i=0; i<ArraySize(azHelp); i++){
4623      if( azHelp[i][0]=='.' || zPattern[0] ){
4624        utf8_printf(out, "%s\n", azHelp[i]);
4625        n++;
4626      }
4627    }
4628  }else{
4629    /* Look for commands that for which zPattern is an exact prefix */
4630    zPat = sqlite3_mprintf(".%s*", zPattern);
4631    shell_check_oom(zPat);
4632    for(i=0; i<ArraySize(azHelp); i++){
4633      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4634        utf8_printf(out, "%s\n", azHelp[i]);
4635        j = i+1;
4636        n++;
4637      }
4638    }
4639    sqlite3_free(zPat);
4640    if( n ){
4641      if( n==1 ){
4642        /* when zPattern is a prefix of exactly one command, then include the
4643        ** details of that command, which should begin at offset j */
4644        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4645          utf8_printf(out, "%s\n", azHelp[j]);
4646          j++;
4647        }
4648      }
4649      return n;
4650    }
4651    /* Look for commands that contain zPattern anywhere.  Show the complete
4652    ** text of all commands that match. */
4653    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4654    shell_check_oom(zPat);
4655    for(i=0; i<ArraySize(azHelp); i++){
4656      if( azHelp[i][0]=='.' ) j = i;
4657      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4658        utf8_printf(out, "%s\n", azHelp[j]);
4659        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4660          j++;
4661          utf8_printf(out, "%s\n", azHelp[j]);
4662        }
4663        i = j;
4664        n++;
4665      }
4666    }
4667    sqlite3_free(zPat);
4668  }
4669  return n;
4670}
4671
4672/* Forward reference */
4673static int process_input(ShellState *p);
4674
4675/*
4676** Read the content of file zName into memory obtained from sqlite3_malloc64()
4677** and return a pointer to the buffer. The caller is responsible for freeing
4678** the memory.
4679**
4680** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4681** read.
4682**
4683** For convenience, a nul-terminator byte is always appended to the data read
4684** from the file before the buffer is returned. This byte is not included in
4685** the final value of (*pnByte), if applicable.
4686**
4687** NULL is returned if any error is encountered. The final value of *pnByte
4688** is undefined in this case.
4689*/
4690static char *readFile(const char *zName, int *pnByte){
4691  FILE *in = fopen(zName, "rb");
4692  long nIn;
4693  size_t nRead;
4694  char *pBuf;
4695  if( in==0 ) return 0;
4696  fseek(in, 0, SEEK_END);
4697  nIn = ftell(in);
4698  rewind(in);
4699  pBuf = sqlite3_malloc64( nIn+1 );
4700  if( pBuf==0 ){ fclose(in); return 0; }
4701  nRead = fread(pBuf, nIn, 1, in);
4702  fclose(in);
4703  if( nRead!=1 ){
4704    sqlite3_free(pBuf);
4705    return 0;
4706  }
4707  pBuf[nIn] = 0;
4708  if( pnByte ) *pnByte = nIn;
4709  return pBuf;
4710}
4711
4712#if defined(SQLITE_ENABLE_SESSION)
4713/*
4714** Close a single OpenSession object and release all of its associated
4715** resources.
4716*/
4717static void session_close(OpenSession *pSession){
4718  int i;
4719  sqlite3session_delete(pSession->p);
4720  sqlite3_free(pSession->zName);
4721  for(i=0; i<pSession->nFilter; i++){
4722    sqlite3_free(pSession->azFilter[i]);
4723  }
4724  sqlite3_free(pSession->azFilter);
4725  memset(pSession, 0, sizeof(OpenSession));
4726}
4727#endif
4728
4729/*
4730** Close all OpenSession objects and release all associated resources.
4731*/
4732#if defined(SQLITE_ENABLE_SESSION)
4733static void session_close_all(ShellState *p, int i){
4734  int j;
4735  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4736  for(j=0; j<pAuxDb->nSession; j++){
4737    session_close(&pAuxDb->aSession[j]);
4738  }
4739  pAuxDb->nSession = 0;
4740}
4741#else
4742# define session_close_all(X,Y)
4743#endif
4744
4745/*
4746** Implementation of the xFilter function for an open session.  Omit
4747** any tables named by ".session filter" but let all other table through.
4748*/
4749#if defined(SQLITE_ENABLE_SESSION)
4750static int session_filter(void *pCtx, const char *zTab){
4751  OpenSession *pSession = (OpenSession*)pCtx;
4752  int i;
4753  for(i=0; i<pSession->nFilter; i++){
4754    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4755  }
4756  return 1;
4757}
4758#endif
4759
4760/*
4761** Try to deduce the type of file for zName based on its content.  Return
4762** one of the SHELL_OPEN_* constants.
4763**
4764** If the file does not exist or is empty but its name looks like a ZIP
4765** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4766** Otherwise, assume an ordinary database regardless of the filename if
4767** the type cannot be determined from content.
4768*/
4769int deduceDatabaseType(const char *zName, int dfltZip){
4770  FILE *f = fopen(zName, "rb");
4771  size_t n;
4772  int rc = SHELL_OPEN_UNSPEC;
4773  char zBuf[100];
4774  if( f==0 ){
4775    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4776       return SHELL_OPEN_ZIPFILE;
4777    }else{
4778       return SHELL_OPEN_NORMAL;
4779    }
4780  }
4781  n = fread(zBuf, 16, 1, f);
4782  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4783    fclose(f);
4784    return SHELL_OPEN_NORMAL;
4785  }
4786  fseek(f, -25, SEEK_END);
4787  n = fread(zBuf, 25, 1, f);
4788  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4789    rc = SHELL_OPEN_APPENDVFS;
4790  }else{
4791    fseek(f, -22, SEEK_END);
4792    n = fread(zBuf, 22, 1, f);
4793    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4794       && zBuf[3]==0x06 ){
4795      rc = SHELL_OPEN_ZIPFILE;
4796    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4797      rc = SHELL_OPEN_ZIPFILE;
4798    }
4799  }
4800  fclose(f);
4801  return rc;
4802}
4803
4804#ifndef SQLITE_OMIT_DESERIALIZE
4805/*
4806** Reconstruct an in-memory database using the output from the "dbtotxt"
4807** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4808** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4809*/
4810static unsigned char *readHexDb(ShellState *p, int *pnData){
4811  unsigned char *a = 0;
4812  int nLine;
4813  int n = 0;
4814  int pgsz = 0;
4815  int iOffset = 0;
4816  int j, k;
4817  int rc;
4818  FILE *in;
4819  const char *zDbFilename = p->pAuxDb->zDbFilename;
4820  unsigned int x[16];
4821  char zLine[1000];
4822  if( zDbFilename ){
4823    in = fopen(zDbFilename, "r");
4824    if( in==0 ){
4825      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4826      return 0;
4827    }
4828    nLine = 0;
4829  }else{
4830    in = p->in;
4831    nLine = p->lineno;
4832    if( in==0 ) in = stdin;
4833  }
4834  *pnData = 0;
4835  nLine++;
4836  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4837  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4838  if( rc!=2 ) goto readHexDb_error;
4839  if( n<0 ) goto readHexDb_error;
4840  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4841  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4842  a = sqlite3_malloc( n ? n : 1 );
4843  shell_check_oom(a);
4844  memset(a, 0, n);
4845  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4846    utf8_printf(stderr, "invalid pagesize\n");
4847    goto readHexDb_error;
4848  }
4849  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4850    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4851    if( rc==2 ){
4852      iOffset = k;
4853      continue;
4854    }
4855    if( cli_strncmp(zLine, "| end ", 6)==0 ){
4856      break;
4857    }
4858    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4859                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4860                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4861    if( rc==17 ){
4862      k = iOffset+j;
4863      if( k+16<=n && k>=0 ){
4864        int ii;
4865        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4866      }
4867    }
4868  }
4869  *pnData = n;
4870  if( in!=p->in ){
4871    fclose(in);
4872  }else{
4873    p->lineno = nLine;
4874  }
4875  return a;
4876
4877readHexDb_error:
4878  if( in!=p->in ){
4879    fclose(in);
4880  }else{
4881    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4882      nLine++;
4883      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
4884    }
4885    p->lineno = nLine;
4886  }
4887  sqlite3_free(a);
4888  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4889  return 0;
4890}
4891#endif /* SQLITE_OMIT_DESERIALIZE */
4892
4893/*
4894** Scalar function "shell_int32". The first argument to this function
4895** must be a blob. The second a non-negative integer. This function
4896** reads and returns a 32-bit big-endian integer from byte
4897** offset (4*<arg2>) of the blob.
4898*/
4899static void shellInt32(
4900  sqlite3_context *context,
4901  int argc,
4902  sqlite3_value **argv
4903){
4904  const unsigned char *pBlob;
4905  int nBlob;
4906  int iInt;
4907
4908  UNUSED_PARAMETER(argc);
4909  nBlob = sqlite3_value_bytes(argv[0]);
4910  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4911  iInt = sqlite3_value_int(argv[1]);
4912
4913  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4914    const unsigned char *a = &pBlob[iInt*4];
4915    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4916                       + ((sqlite3_int64)a[1]<<16)
4917                       + ((sqlite3_int64)a[2]<< 8)
4918                       + ((sqlite3_int64)a[3]<< 0);
4919    sqlite3_result_int64(context, iVal);
4920  }
4921}
4922
4923/*
4924** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4925** using "..." with internal double-quote characters doubled.
4926*/
4927static void shellIdQuote(
4928  sqlite3_context *context,
4929  int argc,
4930  sqlite3_value **argv
4931){
4932  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4933  UNUSED_PARAMETER(argc);
4934  if( zName ){
4935    char *z = sqlite3_mprintf("\"%w\"", zName);
4936    sqlite3_result_text(context, z, -1, sqlite3_free);
4937  }
4938}
4939
4940/*
4941** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4942*/
4943static void shellUSleepFunc(
4944  sqlite3_context *context,
4945  int argcUnused,
4946  sqlite3_value **argv
4947){
4948  int sleep = sqlite3_value_int(argv[0]);
4949  (void)argcUnused;
4950  sqlite3_sleep(sleep/1000);
4951  sqlite3_result_int(context, sleep);
4952}
4953
4954/*
4955** Scalar function "shell_escape_crnl" used by the .recover command.
4956** The argument passed to this function is the output of built-in
4957** function quote(). If the first character of the input is "'",
4958** indicating that the value passed to quote() was a text value,
4959** then this function searches the input for "\n" and "\r" characters
4960** and adds a wrapper similar to the following:
4961**
4962**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4963**
4964** Or, if the first character of the input is not "'", then a copy
4965** of the input is returned.
4966*/
4967static void shellEscapeCrnl(
4968  sqlite3_context *context,
4969  int argc,
4970  sqlite3_value **argv
4971){
4972  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4973  UNUSED_PARAMETER(argc);
4974  if( zText && zText[0]=='\'' ){
4975    i64 nText = sqlite3_value_bytes(argv[0]);
4976    i64 i;
4977    char zBuf1[20];
4978    char zBuf2[20];
4979    const char *zNL = 0;
4980    const char *zCR = 0;
4981    i64 nCR = 0;
4982    i64 nNL = 0;
4983
4984    for(i=0; zText[i]; i++){
4985      if( zNL==0 && zText[i]=='\n' ){
4986        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4987        nNL = strlen(zNL);
4988      }
4989      if( zCR==0 && zText[i]=='\r' ){
4990        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4991        nCR = strlen(zCR);
4992      }
4993    }
4994
4995    if( zNL || zCR ){
4996      i64 iOut = 0;
4997      i64 nMax = (nNL > nCR) ? nNL : nCR;
4998      i64 nAlloc = nMax * nText + (nMax+64)*2;
4999      char *zOut = (char*)sqlite3_malloc64(nAlloc);
5000      if( zOut==0 ){
5001        sqlite3_result_error_nomem(context);
5002        return;
5003      }
5004
5005      if( zNL && zCR ){
5006        memcpy(&zOut[iOut], "replace(replace(", 16);
5007        iOut += 16;
5008      }else{
5009        memcpy(&zOut[iOut], "replace(", 8);
5010        iOut += 8;
5011      }
5012      for(i=0; zText[i]; i++){
5013        if( zText[i]=='\n' ){
5014          memcpy(&zOut[iOut], zNL, nNL);
5015          iOut += nNL;
5016        }else if( zText[i]=='\r' ){
5017          memcpy(&zOut[iOut], zCR, nCR);
5018          iOut += nCR;
5019        }else{
5020          zOut[iOut] = zText[i];
5021          iOut++;
5022        }
5023      }
5024
5025      if( zNL ){
5026        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5027        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
5028        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
5029      }
5030      if( zCR ){
5031        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5032        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
5033        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
5034      }
5035
5036      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
5037      sqlite3_free(zOut);
5038      return;
5039    }
5040  }
5041
5042  sqlite3_result_value(context, argv[0]);
5043}
5044
5045/* Flags for open_db().
5046**
5047** The default behavior of open_db() is to exit(1) if the database fails to
5048** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5049** but still returns without calling exit.
5050**
5051** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5052** ZIP archive if the file does not exist or is empty and its name matches
5053** the *.zip pattern.
5054*/
5055#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
5056#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
5057
5058/*
5059** Make sure the database is open.  If it is not, then open it.  If
5060** the database fails to open, print an error message and exit.
5061*/
5062static void open_db(ShellState *p, int openFlags){
5063  if( p->db==0 ){
5064    const char *zDbFilename = p->pAuxDb->zDbFilename;
5065    if( p->openMode==SHELL_OPEN_UNSPEC ){
5066      if( zDbFilename==0 || zDbFilename[0]==0 ){
5067        p->openMode = SHELL_OPEN_NORMAL;
5068      }else{
5069        p->openMode = (u8)deduceDatabaseType(zDbFilename,
5070                             (openFlags & OPEN_DB_ZIPFILE)!=0);
5071      }
5072    }
5073    switch( p->openMode ){
5074      case SHELL_OPEN_APPENDVFS: {
5075        sqlite3_open_v2(zDbFilename, &p->db,
5076           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5077        break;
5078      }
5079      case SHELL_OPEN_HEXDB:
5080      case SHELL_OPEN_DESERIALIZE: {
5081        sqlite3_open(0, &p->db);
5082        break;
5083      }
5084      case SHELL_OPEN_ZIPFILE: {
5085        sqlite3_open(":memory:", &p->db);
5086        break;
5087      }
5088      case SHELL_OPEN_READONLY: {
5089        sqlite3_open_v2(zDbFilename, &p->db,
5090            SQLITE_OPEN_READONLY|p->openFlags, 0);
5091        break;
5092      }
5093      case SHELL_OPEN_UNSPEC:
5094      case SHELL_OPEN_NORMAL: {
5095        sqlite3_open_v2(zDbFilename, &p->db,
5096           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5097        break;
5098      }
5099    }
5100    globalDb = p->db;
5101    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5102      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5103          zDbFilename, sqlite3_errmsg(p->db));
5104      if( openFlags & OPEN_DB_KEEPALIVE ){
5105        sqlite3_open(":memory:", &p->db);
5106        return;
5107      }
5108      exit(1);
5109    }
5110#ifndef SQLITE_OMIT_LOAD_EXTENSION
5111    sqlite3_enable_load_extension(p->db, 1);
5112#endif
5113    sqlite3_shathree_init(p->db, 0, 0);
5114    sqlite3_uint_init(p->db, 0, 0);
5115    sqlite3_decimal_init(p->db, 0, 0);
5116    sqlite3_regexp_init(p->db, 0, 0);
5117    sqlite3_ieee_init(p->db, 0, 0);
5118    sqlite3_series_init(p->db, 0, 0);
5119#ifndef SQLITE_SHELL_FIDDLE
5120    sqlite3_fileio_init(p->db, 0, 0);
5121    sqlite3_completion_init(p->db, 0, 0);
5122#endif
5123#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5124    sqlite3_dbdata_init(p->db, 0, 0);
5125#endif
5126#ifdef SQLITE_HAVE_ZLIB
5127    if( !p->bSafeModePersist ){
5128      sqlite3_zipfile_init(p->db, 0, 0);
5129      sqlite3_sqlar_init(p->db, 0, 0);
5130    }
5131#endif
5132    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5133                            shellAddSchemaName, 0, 0);
5134    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5135                            shellModuleSchema, 0, 0);
5136    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5137                            shellPutsFunc, 0, 0);
5138    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
5139                            shellEscapeCrnl, 0, 0);
5140    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
5141                            shellInt32, 0, 0);
5142    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
5143                            shellIdQuote, 0, 0);
5144    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5145                            shellUSleepFunc, 0, 0);
5146#ifndef SQLITE_NOHAVE_SYSTEM
5147    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5148                            editFunc, 0, 0);
5149    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5150                            editFunc, 0, 0);
5151#endif
5152    if( p->openMode==SHELL_OPEN_ZIPFILE ){
5153      char *zSql = sqlite3_mprintf(
5154         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5155      shell_check_oom(zSql);
5156      sqlite3_exec(p->db, zSql, 0, 0, 0);
5157      sqlite3_free(zSql);
5158    }
5159#ifndef SQLITE_OMIT_DESERIALIZE
5160    else
5161    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5162      int rc;
5163      int nData = 0;
5164      unsigned char *aData;
5165      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5166        aData = (unsigned char*)readFile(zDbFilename, &nData);
5167      }else{
5168        aData = readHexDb(p, &nData);
5169        if( aData==0 ){
5170          return;
5171        }
5172      }
5173      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5174                   SQLITE_DESERIALIZE_RESIZEABLE |
5175                   SQLITE_DESERIALIZE_FREEONCLOSE);
5176      if( rc ){
5177        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5178      }
5179      if( p->szMax>0 ){
5180        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5181      }
5182    }
5183#endif
5184  }
5185  if( p->bSafeModePersist && p->db!=0 ){
5186    sqlite3_set_authorizer(p->db, safeModeAuth, p);
5187  }
5188}
5189
5190/*
5191** Attempt to close the databaes connection.  Report errors.
5192*/
5193void close_db(sqlite3 *db){
5194  int rc = sqlite3_close(db);
5195  if( rc ){
5196    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5197        rc, sqlite3_errmsg(db));
5198  }
5199}
5200
5201#if HAVE_READLINE || HAVE_EDITLINE
5202/*
5203** Readline completion callbacks
5204*/
5205static char *readline_completion_generator(const char *text, int state){
5206  static sqlite3_stmt *pStmt = 0;
5207  char *zRet;
5208  if( state==0 ){
5209    char *zSql;
5210    sqlite3_finalize(pStmt);
5211    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5212                           "  FROM completion(%Q) ORDER BY 1", text);
5213    shell_check_oom(zSql);
5214    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5215    sqlite3_free(zSql);
5216  }
5217  if( sqlite3_step(pStmt)==SQLITE_ROW ){
5218    const char *z = (const char*)sqlite3_column_text(pStmt,0);
5219    zRet = z ? strdup(z) : 0;
5220  }else{
5221    sqlite3_finalize(pStmt);
5222    pStmt = 0;
5223    zRet = 0;
5224  }
5225  return zRet;
5226}
5227static char **readline_completion(const char *zText, int iStart, int iEnd){
5228  rl_attempted_completion_over = 1;
5229  return rl_completion_matches(zText, readline_completion_generator);
5230}
5231
5232#elif HAVE_LINENOISE
5233/*
5234** Linenoise completion callback
5235*/
5236static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5237  i64 nLine = strlen(zLine);
5238  i64 i, iStart;
5239  sqlite3_stmt *pStmt = 0;
5240  char *zSql;
5241  char zBuf[1000];
5242
5243  if( nLine>sizeof(zBuf)-30 ) return;
5244  if( zLine[0]=='.' || zLine[0]=='#') return;
5245  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5246  if( i==nLine-1 ) return;
5247  iStart = i+1;
5248  memcpy(zBuf, zLine, iStart);
5249  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5250                         "  FROM completion(%Q,%Q) ORDER BY 1",
5251                         &zLine[iStart], zLine);
5252  shell_check_oom(zSql);
5253  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5254  sqlite3_free(zSql);
5255  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5256  while( sqlite3_step(pStmt)==SQLITE_ROW ){
5257    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5258    int nCompletion = sqlite3_column_bytes(pStmt, 0);
5259    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
5260      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5261      linenoiseAddCompletion(lc, zBuf);
5262    }
5263  }
5264  sqlite3_finalize(pStmt);
5265}
5266#endif
5267
5268/*
5269** Do C-language style dequoting.
5270**
5271**    \a    -> alarm
5272**    \b    -> backspace
5273**    \t    -> tab
5274**    \n    -> newline
5275**    \v    -> vertical tab
5276**    \f    -> form feed
5277**    \r    -> carriage return
5278**    \s    -> space
5279**    \"    -> "
5280**    \'    -> '
5281**    \\    -> backslash
5282**    \NNN  -> ascii character NNN in octal
5283*/
5284static void resolve_backslashes(char *z){
5285  int i, j;
5286  char c;
5287  while( *z && *z!='\\' ) z++;
5288  for(i=j=0; (c = z[i])!=0; i++, j++){
5289    if( c=='\\' && z[i+1]!=0 ){
5290      c = z[++i];
5291      if( c=='a' ){
5292        c = '\a';
5293      }else if( c=='b' ){
5294        c = '\b';
5295      }else if( c=='t' ){
5296        c = '\t';
5297      }else if( c=='n' ){
5298        c = '\n';
5299      }else if( c=='v' ){
5300        c = '\v';
5301      }else if( c=='f' ){
5302        c = '\f';
5303      }else if( c=='r' ){
5304        c = '\r';
5305      }else if( c=='"' ){
5306        c = '"';
5307      }else if( c=='\'' ){
5308        c = '\'';
5309      }else if( c=='\\' ){
5310        c = '\\';
5311      }else if( c>='0' && c<='7' ){
5312        c -= '0';
5313        if( z[i+1]>='0' && z[i+1]<='7' ){
5314          i++;
5315          c = (c<<3) + z[i] - '0';
5316          if( z[i+1]>='0' && z[i+1]<='7' ){
5317            i++;
5318            c = (c<<3) + z[i] - '0';
5319          }
5320        }
5321      }
5322    }
5323    z[j] = c;
5324  }
5325  if( j<i ) z[j] = 0;
5326}
5327
5328/*
5329** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
5330** for TRUE and FALSE.  Return the integer value if appropriate.
5331*/
5332static int booleanValue(const char *zArg){
5333  int i;
5334  if( zArg[0]=='0' && zArg[1]=='x' ){
5335    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5336  }else{
5337    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5338  }
5339  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5340  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5341    return 1;
5342  }
5343  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5344    return 0;
5345  }
5346  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5347          zArg);
5348  return 0;
5349}
5350
5351/*
5352** Set or clear a shell flag according to a boolean value.
5353*/
5354static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5355  if( booleanValue(zArg) ){
5356    ShellSetFlag(p, mFlag);
5357  }else{
5358    ShellClearFlag(p, mFlag);
5359  }
5360}
5361
5362/*
5363** Close an output file, assuming it is not stderr or stdout
5364*/
5365static void output_file_close(FILE *f){
5366  if( f && f!=stdout && f!=stderr ) fclose(f);
5367}
5368
5369/*
5370** Try to open an output file.   The names "stdout" and "stderr" are
5371** recognized and do the right thing.  NULL is returned if the output
5372** filename is "off".
5373*/
5374static FILE *output_file_open(const char *zFile, int bTextMode){
5375  FILE *f;
5376  if( cli_strcmp(zFile,"stdout")==0 ){
5377    f = stdout;
5378  }else if( cli_strcmp(zFile, "stderr")==0 ){
5379    f = stderr;
5380  }else if( cli_strcmp(zFile, "off")==0 ){
5381    f = 0;
5382  }else{
5383    f = fopen(zFile, bTextMode ? "w" : "wb");
5384    if( f==0 ){
5385      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5386    }
5387  }
5388  return f;
5389}
5390
5391#ifndef SQLITE_OMIT_TRACE
5392/*
5393** A routine for handling output from sqlite3_trace().
5394*/
5395static int sql_trace_callback(
5396  unsigned mType,         /* The trace type */
5397  void *pArg,             /* The ShellState pointer */
5398  void *pP,               /* Usually a pointer to sqlite_stmt */
5399  void *pX                /* Auxiliary output */
5400){
5401  ShellState *p = (ShellState*)pArg;
5402  sqlite3_stmt *pStmt;
5403  const char *zSql;
5404  i64 nSql;
5405  if( p->traceOut==0 ) return 0;
5406  if( mType==SQLITE_TRACE_CLOSE ){
5407    utf8_printf(p->traceOut, "-- closing database connection\n");
5408    return 0;
5409  }
5410  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5411    zSql = (const char*)pX;
5412  }else{
5413    pStmt = (sqlite3_stmt*)pP;
5414    switch( p->eTraceType ){
5415      case SHELL_TRACE_EXPANDED: {
5416        zSql = sqlite3_expanded_sql(pStmt);
5417        break;
5418      }
5419#ifdef SQLITE_ENABLE_NORMALIZE
5420      case SHELL_TRACE_NORMALIZED: {
5421        zSql = sqlite3_normalized_sql(pStmt);
5422        break;
5423      }
5424#endif
5425      default: {
5426        zSql = sqlite3_sql(pStmt);
5427        break;
5428      }
5429    }
5430  }
5431  if( zSql==0 ) return 0;
5432  nSql = strlen(zSql);
5433  if( nSql>1000000000 ) nSql = 1000000000;
5434  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5435  switch( mType ){
5436    case SQLITE_TRACE_ROW:
5437    case SQLITE_TRACE_STMT: {
5438      utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5439      break;
5440    }
5441    case SQLITE_TRACE_PROFILE: {
5442      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5443      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5444      break;
5445    }
5446  }
5447  return 0;
5448}
5449#endif
5450
5451/*
5452** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5453** a useful spot to set a debugger breakpoint.
5454*/
5455static void test_breakpoint(void){
5456  static int nCall = 0;
5457  nCall++;
5458}
5459
5460/*
5461** An object used to read a CSV and other files for import.
5462*/
5463typedef struct ImportCtx ImportCtx;
5464struct ImportCtx {
5465  const char *zFile;  /* Name of the input file */
5466  FILE *in;           /* Read the CSV text from this input stream */
5467  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5468  char *z;            /* Accumulated text for a field */
5469  int n;              /* Number of bytes in z */
5470  int nAlloc;         /* Space allocated for z[] */
5471  int nLine;          /* Current line number */
5472  int nRow;           /* Number of rows imported */
5473  int nErr;           /* Number of errors encountered */
5474  int bNotFirst;      /* True if one or more bytes already read */
5475  int cTerm;          /* Character that terminated the most recent field */
5476  int cColSep;        /* The column separator character.  (Usually ",") */
5477  int cRowSep;        /* The row separator character.  (Usually "\n") */
5478};
5479
5480/* Clean up resourced used by an ImportCtx */
5481static void import_cleanup(ImportCtx *p){
5482  if( p->in!=0 && p->xCloser!=0 ){
5483    p->xCloser(p->in);
5484    p->in = 0;
5485  }
5486  sqlite3_free(p->z);
5487  p->z = 0;
5488}
5489
5490/* Append a single byte to z[] */
5491static void import_append_char(ImportCtx *p, int c){
5492  if( p->n+1>=p->nAlloc ){
5493    p->nAlloc += p->nAlloc + 100;
5494    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5495    shell_check_oom(p->z);
5496  }
5497  p->z[p->n++] = (char)c;
5498}
5499
5500/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5501** with the option of having a separator other than ",".
5502**
5503**   +  Input comes from p->in.
5504**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5505**      from sqlite3_malloc64().
5506**   +  Use p->cSep as the column separator.  The default is ",".
5507**   +  Use p->rSep as the row separator.  The default is "\n".
5508**   +  Keep track of the line number in p->nLine.
5509**   +  Store the character that terminates the field in p->cTerm.  Store
5510**      EOF on end-of-file.
5511**   +  Report syntax errors on stderr
5512*/
5513static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5514  int c;
5515  int cSep = p->cColSep;
5516  int rSep = p->cRowSep;
5517  p->n = 0;
5518  c = fgetc(p->in);
5519  if( c==EOF || seenInterrupt ){
5520    p->cTerm = EOF;
5521    return 0;
5522  }
5523  if( c=='"' ){
5524    int pc, ppc;
5525    int startLine = p->nLine;
5526    int cQuote = c;
5527    pc = ppc = 0;
5528    while( 1 ){
5529      c = fgetc(p->in);
5530      if( c==rSep ) p->nLine++;
5531      if( c==cQuote ){
5532        if( pc==cQuote ){
5533          pc = 0;
5534          continue;
5535        }
5536      }
5537      if( (c==cSep && pc==cQuote)
5538       || (c==rSep && pc==cQuote)
5539       || (c==rSep && pc=='\r' && ppc==cQuote)
5540       || (c==EOF && pc==cQuote)
5541      ){
5542        do{ p->n--; }while( p->z[p->n]!=cQuote );
5543        p->cTerm = c;
5544        break;
5545      }
5546      if( pc==cQuote && c!='\r' ){
5547        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5548                p->zFile, p->nLine, cQuote);
5549      }
5550      if( c==EOF ){
5551        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5552                p->zFile, startLine, cQuote);
5553        p->cTerm = c;
5554        break;
5555      }
5556      import_append_char(p, c);
5557      ppc = pc;
5558      pc = c;
5559    }
5560  }else{
5561    /* If this is the first field being parsed and it begins with the
5562    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5563    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5564      import_append_char(p, c);
5565      c = fgetc(p->in);
5566      if( (c&0xff)==0xbb ){
5567        import_append_char(p, c);
5568        c = fgetc(p->in);
5569        if( (c&0xff)==0xbf ){
5570          p->bNotFirst = 1;
5571          p->n = 0;
5572          return csv_read_one_field(p);
5573        }
5574      }
5575    }
5576    while( c!=EOF && c!=cSep && c!=rSep ){
5577      import_append_char(p, c);
5578      c = fgetc(p->in);
5579    }
5580    if( c==rSep ){
5581      p->nLine++;
5582      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5583    }
5584    p->cTerm = c;
5585  }
5586  if( p->z ) p->z[p->n] = 0;
5587  p->bNotFirst = 1;
5588  return p->z;
5589}
5590
5591/* Read a single field of ASCII delimited text.
5592**
5593**   +  Input comes from p->in.
5594**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5595**      from sqlite3_malloc64().
5596**   +  Use p->cSep as the column separator.  The default is "\x1F".
5597**   +  Use p->rSep as the row separator.  The default is "\x1E".
5598**   +  Keep track of the row number in p->nLine.
5599**   +  Store the character that terminates the field in p->cTerm.  Store
5600**      EOF on end-of-file.
5601**   +  Report syntax errors on stderr
5602*/
5603static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5604  int c;
5605  int cSep = p->cColSep;
5606  int rSep = p->cRowSep;
5607  p->n = 0;
5608  c = fgetc(p->in);
5609  if( c==EOF || seenInterrupt ){
5610    p->cTerm = EOF;
5611    return 0;
5612  }
5613  while( c!=EOF && c!=cSep && c!=rSep ){
5614    import_append_char(p, c);
5615    c = fgetc(p->in);
5616  }
5617  if( c==rSep ){
5618    p->nLine++;
5619  }
5620  p->cTerm = c;
5621  if( p->z ) p->z[p->n] = 0;
5622  return p->z;
5623}
5624
5625/*
5626** Try to transfer data for table zTable.  If an error is seen while
5627** moving forward, try to go backwards.  The backwards movement won't
5628** work for WITHOUT ROWID tables.
5629*/
5630static void tryToCloneData(
5631  ShellState *p,
5632  sqlite3 *newDb,
5633  const char *zTable
5634){
5635  sqlite3_stmt *pQuery = 0;
5636  sqlite3_stmt *pInsert = 0;
5637  char *zQuery = 0;
5638  char *zInsert = 0;
5639  int rc;
5640  int i, j, n;
5641  int nTable = strlen30(zTable);
5642  int k = 0;
5643  int cnt = 0;
5644  const int spinRate = 10000;
5645
5646  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5647  shell_check_oom(zQuery);
5648  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5649  if( rc ){
5650    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5651            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5652            zQuery);
5653    goto end_data_xfer;
5654  }
5655  n = sqlite3_column_count(pQuery);
5656  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5657  shell_check_oom(zInsert);
5658  sqlite3_snprintf(200+nTable,zInsert,
5659                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5660  i = strlen30(zInsert);
5661  for(j=1; j<n; j++){
5662    memcpy(zInsert+i, ",?", 2);
5663    i += 2;
5664  }
5665  memcpy(zInsert+i, ");", 3);
5666  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5667  if( rc ){
5668    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5669            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5670            zQuery);
5671    goto end_data_xfer;
5672  }
5673  for(k=0; k<2; k++){
5674    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5675      for(i=0; i<n; i++){
5676        switch( sqlite3_column_type(pQuery, i) ){
5677          case SQLITE_NULL: {
5678            sqlite3_bind_null(pInsert, i+1);
5679            break;
5680          }
5681          case SQLITE_INTEGER: {
5682            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5683            break;
5684          }
5685          case SQLITE_FLOAT: {
5686            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5687            break;
5688          }
5689          case SQLITE_TEXT: {
5690            sqlite3_bind_text(pInsert, i+1,
5691                             (const char*)sqlite3_column_text(pQuery,i),
5692                             -1, SQLITE_STATIC);
5693            break;
5694          }
5695          case SQLITE_BLOB: {
5696            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5697                                            sqlite3_column_bytes(pQuery,i),
5698                                            SQLITE_STATIC);
5699            break;
5700          }
5701        }
5702      } /* End for */
5703      rc = sqlite3_step(pInsert);
5704      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5705        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5706                        sqlite3_errmsg(newDb));
5707      }
5708      sqlite3_reset(pInsert);
5709      cnt++;
5710      if( (cnt%spinRate)==0 ){
5711        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5712        fflush(stdout);
5713      }
5714    } /* End while */
5715    if( rc==SQLITE_DONE ) break;
5716    sqlite3_finalize(pQuery);
5717    sqlite3_free(zQuery);
5718    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5719                             zTable);
5720    shell_check_oom(zQuery);
5721    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5722    if( rc ){
5723      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5724      break;
5725    }
5726  } /* End for(k=0...) */
5727
5728end_data_xfer:
5729  sqlite3_finalize(pQuery);
5730  sqlite3_finalize(pInsert);
5731  sqlite3_free(zQuery);
5732  sqlite3_free(zInsert);
5733}
5734
5735
5736/*
5737** Try to transfer all rows of the schema that match zWhere.  For
5738** each row, invoke xForEach() on the object defined by that row.
5739** If an error is encountered while moving forward through the
5740** sqlite_schema table, try again moving backwards.
5741*/
5742static void tryToCloneSchema(
5743  ShellState *p,
5744  sqlite3 *newDb,
5745  const char *zWhere,
5746  void (*xForEach)(ShellState*,sqlite3*,const char*)
5747){
5748  sqlite3_stmt *pQuery = 0;
5749  char *zQuery = 0;
5750  int rc;
5751  const unsigned char *zName;
5752  const unsigned char *zSql;
5753  char *zErrMsg = 0;
5754
5755  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5756                           " WHERE %s", zWhere);
5757  shell_check_oom(zQuery);
5758  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5759  if( rc ){
5760    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5761                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5762                    zQuery);
5763    goto end_schema_xfer;
5764  }
5765  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5766    zName = sqlite3_column_text(pQuery, 0);
5767    zSql = sqlite3_column_text(pQuery, 1);
5768    if( zName==0 || zSql==0 ) continue;
5769    printf("%s... ", zName); fflush(stdout);
5770    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5771    if( zErrMsg ){
5772      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5773      sqlite3_free(zErrMsg);
5774      zErrMsg = 0;
5775    }
5776    if( xForEach ){
5777      xForEach(p, newDb, (const char*)zName);
5778    }
5779    printf("done\n");
5780  }
5781  if( rc!=SQLITE_DONE ){
5782    sqlite3_finalize(pQuery);
5783    sqlite3_free(zQuery);
5784    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5785                             " WHERE %s ORDER BY rowid DESC", zWhere);
5786    shell_check_oom(zQuery);
5787    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5788    if( rc ){
5789      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5790                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5791                      zQuery);
5792      goto end_schema_xfer;
5793    }
5794    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5795      zName = sqlite3_column_text(pQuery, 0);
5796      zSql = sqlite3_column_text(pQuery, 1);
5797      if( zName==0 || zSql==0 ) continue;
5798      printf("%s... ", zName); fflush(stdout);
5799      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5800      if( zErrMsg ){
5801        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5802        sqlite3_free(zErrMsg);
5803        zErrMsg = 0;
5804      }
5805      if( xForEach ){
5806        xForEach(p, newDb, (const char*)zName);
5807      }
5808      printf("done\n");
5809    }
5810  }
5811end_schema_xfer:
5812  sqlite3_finalize(pQuery);
5813  sqlite3_free(zQuery);
5814}
5815
5816/*
5817** Open a new database file named "zNewDb".  Try to recover as much information
5818** as possible out of the main database (which might be corrupt) and write it
5819** into zNewDb.
5820*/
5821static void tryToClone(ShellState *p, const char *zNewDb){
5822  int rc;
5823  sqlite3 *newDb = 0;
5824  if( access(zNewDb,0)==0 ){
5825    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5826    return;
5827  }
5828  rc = sqlite3_open(zNewDb, &newDb);
5829  if( rc ){
5830    utf8_printf(stderr, "Cannot create output database: %s\n",
5831            sqlite3_errmsg(newDb));
5832  }else{
5833    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5834    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5835    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5836    tryToCloneSchema(p, newDb, "type!='table'", 0);
5837    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5838    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5839  }
5840  close_db(newDb);
5841}
5842
5843/*
5844** Change the output file back to stdout.
5845**
5846** If the p->doXdgOpen flag is set, that means the output was being
5847** redirected to a temporary file named by p->zTempFile.  In that case,
5848** launch start/open/xdg-open on that temporary file.
5849*/
5850static void output_reset(ShellState *p){
5851  if( p->outfile[0]=='|' ){
5852#ifndef SQLITE_OMIT_POPEN
5853    pclose(p->out);
5854#endif
5855  }else{
5856    output_file_close(p->out);
5857#ifndef SQLITE_NOHAVE_SYSTEM
5858    if( p->doXdgOpen ){
5859      const char *zXdgOpenCmd =
5860#if defined(_WIN32)
5861      "start";
5862#elif defined(__APPLE__)
5863      "open";
5864#else
5865      "xdg-open";
5866#endif
5867      char *zCmd;
5868      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5869      if( system(zCmd) ){
5870        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5871      }else{
5872        /* Give the start/open/xdg-open command some time to get
5873        ** going before we continue, and potential delete the
5874        ** p->zTempFile data file out from under it */
5875        sqlite3_sleep(2000);
5876      }
5877      sqlite3_free(zCmd);
5878      outputModePop(p);
5879      p->doXdgOpen = 0;
5880    }
5881#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5882  }
5883  p->outfile[0] = 0;
5884  p->out = stdout;
5885}
5886
5887/*
5888** Run an SQL command and return the single integer result.
5889*/
5890static int db_int(sqlite3 *db, const char *zSql){
5891  sqlite3_stmt *pStmt;
5892  int res = 0;
5893  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
5894  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5895    res = sqlite3_column_int(pStmt,0);
5896  }
5897  sqlite3_finalize(pStmt);
5898  return res;
5899}
5900
5901#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5902/*
5903** Convert a 2-byte or 4-byte big-endian integer into a native integer
5904*/
5905static unsigned int get2byteInt(unsigned char *a){
5906  return (a[0]<<8) + a[1];
5907}
5908static unsigned int get4byteInt(unsigned char *a){
5909  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5910}
5911
5912/*
5913** Implementation of the ".dbinfo" command.
5914**
5915** Return 1 on error, 2 to exit, and 0 otherwise.
5916*/
5917static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5918  static const struct { const char *zName; int ofst; } aField[] = {
5919     { "file change counter:",  24  },
5920     { "database page count:",  28  },
5921     { "freelist page count:",  36  },
5922     { "schema cookie:",        40  },
5923     { "schema format:",        44  },
5924     { "default cache size:",   48  },
5925     { "autovacuum top root:",  52  },
5926     { "incremental vacuum:",   64  },
5927     { "text encoding:",        56  },
5928     { "user version:",         60  },
5929     { "application id:",       68  },
5930     { "software version:",     96  },
5931  };
5932  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5933     { "number of tables:",
5934       "SELECT count(*) FROM %s WHERE type='table'" },
5935     { "number of indexes:",
5936       "SELECT count(*) FROM %s WHERE type='index'" },
5937     { "number of triggers:",
5938       "SELECT count(*) FROM %s WHERE type='trigger'" },
5939     { "number of views:",
5940       "SELECT count(*) FROM %s WHERE type='view'" },
5941     { "schema size:",
5942       "SELECT total(length(sql)) FROM %s" },
5943  };
5944  int i, rc;
5945  unsigned iDataVersion;
5946  char *zSchemaTab;
5947  char *zDb = nArg>=2 ? azArg[1] : "main";
5948  sqlite3_stmt *pStmt = 0;
5949  unsigned char aHdr[100];
5950  open_db(p, 0);
5951  if( p->db==0 ) return 1;
5952  rc = sqlite3_prepare_v2(p->db,
5953             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5954             -1, &pStmt, 0);
5955  if( rc ){
5956    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5957    sqlite3_finalize(pStmt);
5958    return 1;
5959  }
5960  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5961  if( sqlite3_step(pStmt)==SQLITE_ROW
5962   && sqlite3_column_bytes(pStmt,0)>100
5963  ){
5964    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5965    sqlite3_finalize(pStmt);
5966  }else{
5967    raw_printf(stderr, "unable to read database header\n");
5968    sqlite3_finalize(pStmt);
5969    return 1;
5970  }
5971  i = get2byteInt(aHdr+16);
5972  if( i==1 ) i = 65536;
5973  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5974  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5975  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5976  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5977  for(i=0; i<ArraySize(aField); i++){
5978    int ofst = aField[i].ofst;
5979    unsigned int val = get4byteInt(aHdr + ofst);
5980    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5981    switch( ofst ){
5982      case 56: {
5983        if( val==1 ) raw_printf(p->out, " (utf8)");
5984        if( val==2 ) raw_printf(p->out, " (utf16le)");
5985        if( val==3 ) raw_printf(p->out, " (utf16be)");
5986      }
5987    }
5988    raw_printf(p->out, "\n");
5989  }
5990  if( zDb==0 ){
5991    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5992  }else if( cli_strcmp(zDb,"temp")==0 ){
5993    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5994  }else{
5995    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5996  }
5997  for(i=0; i<ArraySize(aQuery); i++){
5998    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5999    int val = db_int(p->db, zSql);
6000    sqlite3_free(zSql);
6001    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
6002  }
6003  sqlite3_free(zSchemaTab);
6004  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
6005  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
6006  return 0;
6007}
6008#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
6009          && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
6010
6011/*
6012** Print the current sqlite3_errmsg() value to stderr and return 1.
6013*/
6014static int shellDatabaseError(sqlite3 *db){
6015  const char *zErr = sqlite3_errmsg(db);
6016  utf8_printf(stderr, "Error: %s\n", zErr);
6017  return 1;
6018}
6019
6020/*
6021** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
6022** if they match and FALSE (0) if they do not match.
6023**
6024** Globbing rules:
6025**
6026**      '*'       Matches any sequence of zero or more characters.
6027**
6028**      '?'       Matches exactly one character.
6029**
6030**     [...]      Matches one character from the enclosed list of
6031**                characters.
6032**
6033**     [^...]     Matches one character not in the enclosed list.
6034**
6035**      '#'       Matches any sequence of one or more digits with an
6036**                optional + or - sign in front
6037**
6038**      ' '       Any span of whitespace matches any other span of
6039**                whitespace.
6040**
6041** Extra whitespace at the end of z[] is ignored.
6042*/
6043static int testcase_glob(const char *zGlob, const char *z){
6044  int c, c2;
6045  int invert;
6046  int seen;
6047
6048  while( (c = (*(zGlob++)))!=0 ){
6049    if( IsSpace(c) ){
6050      if( !IsSpace(*z) ) return 0;
6051      while( IsSpace(*zGlob) ) zGlob++;
6052      while( IsSpace(*z) ) z++;
6053    }else if( c=='*' ){
6054      while( (c=(*(zGlob++))) == '*' || c=='?' ){
6055        if( c=='?' && (*(z++))==0 ) return 0;
6056      }
6057      if( c==0 ){
6058        return 1;
6059      }else if( c=='[' ){
6060        while( *z && testcase_glob(zGlob-1,z)==0 ){
6061          z++;
6062        }
6063        return (*z)!=0;
6064      }
6065      while( (c2 = (*(z++)))!=0 ){
6066        while( c2!=c ){
6067          c2 = *(z++);
6068          if( c2==0 ) return 0;
6069        }
6070        if( testcase_glob(zGlob,z) ) return 1;
6071      }
6072      return 0;
6073    }else if( c=='?' ){
6074      if( (*(z++))==0 ) return 0;
6075    }else if( c=='[' ){
6076      int prior_c = 0;
6077      seen = 0;
6078      invert = 0;
6079      c = *(z++);
6080      if( c==0 ) return 0;
6081      c2 = *(zGlob++);
6082      if( c2=='^' ){
6083        invert = 1;
6084        c2 = *(zGlob++);
6085      }
6086      if( c2==']' ){
6087        if( c==']' ) seen = 1;
6088        c2 = *(zGlob++);
6089      }
6090      while( c2 && c2!=']' ){
6091        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6092          c2 = *(zGlob++);
6093          if( c>=prior_c && c<=c2 ) seen = 1;
6094          prior_c = 0;
6095        }else{
6096          if( c==c2 ){
6097            seen = 1;
6098          }
6099          prior_c = c2;
6100        }
6101        c2 = *(zGlob++);
6102      }
6103      if( c2==0 || (seen ^ invert)==0 ) return 0;
6104    }else if( c=='#' ){
6105      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6106      if( !IsDigit(z[0]) ) return 0;
6107      z++;
6108      while( IsDigit(z[0]) ){ z++; }
6109    }else{
6110      if( c!=(*(z++)) ) return 0;
6111    }
6112  }
6113  while( IsSpace(*z) ){ z++; }
6114  return *z==0;
6115}
6116
6117
6118/*
6119** Compare the string as a command-line option with either one or two
6120** initial "-" characters.
6121*/
6122static int optionMatch(const char *zStr, const char *zOpt){
6123  if( zStr[0]!='-' ) return 0;
6124  zStr++;
6125  if( zStr[0]=='-' ) zStr++;
6126  return cli_strcmp(zStr, zOpt)==0;
6127}
6128
6129/*
6130** Delete a file.
6131*/
6132int shellDeleteFile(const char *zFilename){
6133  int rc;
6134#ifdef _WIN32
6135  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6136  rc = _wunlink(z);
6137  sqlite3_free(z);
6138#else
6139  rc = unlink(zFilename);
6140#endif
6141  return rc;
6142}
6143
6144/*
6145** Try to delete the temporary file (if there is one) and free the
6146** memory used to hold the name of the temp file.
6147*/
6148static void clearTempFile(ShellState *p){
6149  if( p->zTempFile==0 ) return;
6150  if( p->doXdgOpen ) return;
6151  if( shellDeleteFile(p->zTempFile) ) return;
6152  sqlite3_free(p->zTempFile);
6153  p->zTempFile = 0;
6154}
6155
6156/*
6157** Create a new temp file name with the given suffix.
6158*/
6159static void newTempFile(ShellState *p, const char *zSuffix){
6160  clearTempFile(p);
6161  sqlite3_free(p->zTempFile);
6162  p->zTempFile = 0;
6163  if( p->db ){
6164    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6165  }
6166  if( p->zTempFile==0 ){
6167    /* If p->db is an in-memory database then the TEMPFILENAME file-control
6168    ** will not work and we will need to fallback to guessing */
6169    char *zTemp;
6170    sqlite3_uint64 r;
6171    sqlite3_randomness(sizeof(r), &r);
6172    zTemp = getenv("TEMP");
6173    if( zTemp==0 ) zTemp = getenv("TMP");
6174    if( zTemp==0 ){
6175#ifdef _WIN32
6176      zTemp = "\\tmp";
6177#else
6178      zTemp = "/tmp";
6179#endif
6180    }
6181    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6182  }else{
6183    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6184  }
6185  shell_check_oom(p->zTempFile);
6186}
6187
6188
6189/*
6190** The implementation of SQL scalar function fkey_collate_clause(), used
6191** by the ".lint fkey-indexes" command. This scalar function is always
6192** called with four arguments - the parent table name, the parent column name,
6193** the child table name and the child column name.
6194**
6195**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6196**
6197** If either of the named tables or columns do not exist, this function
6198** returns an empty string. An empty string is also returned if both tables
6199** and columns exist but have the same default collation sequence. Or,
6200** if both exist but the default collation sequences are different, this
6201** function returns the string " COLLATE <parent-collation>", where
6202** <parent-collation> is the default collation sequence of the parent column.
6203*/
6204static void shellFkeyCollateClause(
6205  sqlite3_context *pCtx,
6206  int nVal,
6207  sqlite3_value **apVal
6208){
6209  sqlite3 *db = sqlite3_context_db_handle(pCtx);
6210  const char *zParent;
6211  const char *zParentCol;
6212  const char *zParentSeq;
6213  const char *zChild;
6214  const char *zChildCol;
6215  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
6216  int rc;
6217
6218  assert( nVal==4 );
6219  zParent = (const char*)sqlite3_value_text(apVal[0]);
6220  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6221  zChild = (const char*)sqlite3_value_text(apVal[2]);
6222  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6223
6224  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6225  rc = sqlite3_table_column_metadata(
6226      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6227  );
6228  if( rc==SQLITE_OK ){
6229    rc = sqlite3_table_column_metadata(
6230        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6231    );
6232  }
6233
6234  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6235    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6236    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6237    sqlite3_free(z);
6238  }
6239}
6240
6241
6242/*
6243** The implementation of dot-command ".lint fkey-indexes".
6244*/
6245static int lintFkeyIndexes(
6246  ShellState *pState,             /* Current shell tool state */
6247  char **azArg,                   /* Array of arguments passed to dot command */
6248  int nArg                        /* Number of entries in azArg[] */
6249){
6250  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
6251  FILE *out = pState->out;        /* Stream to write non-error output to */
6252  int bVerbose = 0;               /* If -verbose is present */
6253  int bGroupByParent = 0;         /* If -groupbyparent is present */
6254  int i;                          /* To iterate through azArg[] */
6255  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
6256  int rc;                         /* Return code */
6257  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
6258
6259  /*
6260  ** This SELECT statement returns one row for each foreign key constraint
6261  ** in the schema of the main database. The column values are:
6262  **
6263  ** 0. The text of an SQL statement similar to:
6264  **
6265  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6266  **
6267  **    This SELECT is similar to the one that the foreign keys implementation
6268  **    needs to run internally on child tables. If there is an index that can
6269  **    be used to optimize this query, then it can also be used by the FK
6270  **    implementation to optimize DELETE or UPDATE statements on the parent
6271  **    table.
6272  **
6273  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6274  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6275  **    contains an index that can be used to optimize the query.
6276  **
6277  ** 2. Human readable text that describes the child table and columns. e.g.
6278  **
6279  **       "child_table(child_key1, child_key2)"
6280  **
6281  ** 3. Human readable text that describes the parent table and columns. e.g.
6282  **
6283  **       "parent_table(parent_key1, parent_key2)"
6284  **
6285  ** 4. A full CREATE INDEX statement for an index that could be used to
6286  **    optimize DELETE or UPDATE statements on the parent table. e.g.
6287  **
6288  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
6289  **
6290  ** 5. The name of the parent table.
6291  **
6292  ** These six values are used by the C logic below to generate the report.
6293  */
6294  const char *zSql =
6295  "SELECT "
6296    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6297    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6298    "  || fkey_collate_clause("
6299    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6300    ", "
6301    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6302    "  || group_concat('*=?', ' AND ') || ')'"
6303    ", "
6304    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
6305    ", "
6306    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6307    ", "
6308    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6309    "  || ' ON ' || quote(s.name) || '('"
6310    "  || group_concat(quote(f.[from]) ||"
6311    "        fkey_collate_clause("
6312    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6313    "  || ');'"
6314    ", "
6315    "     f.[table] "
6316    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6317    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6318    "GROUP BY s.name, f.id "
6319    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6320  ;
6321  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6322
6323  for(i=2; i<nArg; i++){
6324    int n = strlen30(azArg[i]);
6325    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6326      bVerbose = 1;
6327    }
6328    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6329      bGroupByParent = 1;
6330      zIndent = "    ";
6331    }
6332    else{
6333      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6334          azArg[0], azArg[1]
6335      );
6336      return SQLITE_ERROR;
6337    }
6338  }
6339
6340  /* Register the fkey_collate_clause() SQL function */
6341  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6342      0, shellFkeyCollateClause, 0, 0
6343  );
6344
6345
6346  if( rc==SQLITE_OK ){
6347    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6348  }
6349  if( rc==SQLITE_OK ){
6350    sqlite3_bind_int(pSql, 1, bGroupByParent);
6351  }
6352
6353  if( rc==SQLITE_OK ){
6354    int rc2;
6355    char *zPrev = 0;
6356    while( SQLITE_ROW==sqlite3_step(pSql) ){
6357      int res = -1;
6358      sqlite3_stmt *pExplain = 0;
6359      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6360      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6361      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6362      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6363      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6364      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6365
6366      if( zEQP==0 ) continue;
6367      if( zGlob==0 ) continue;
6368      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6369      if( rc!=SQLITE_OK ) break;
6370      if( SQLITE_ROW==sqlite3_step(pExplain) ){
6371        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6372        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
6373                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
6374      }
6375      rc = sqlite3_finalize(pExplain);
6376      if( rc!=SQLITE_OK ) break;
6377
6378      if( res<0 ){
6379        raw_printf(stderr, "Error: internal error");
6380        break;
6381      }else{
6382        if( bGroupByParent
6383        && (bVerbose || res==0)
6384        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6385        ){
6386          raw_printf(out, "-- Parent table %s\n", zParent);
6387          sqlite3_free(zPrev);
6388          zPrev = sqlite3_mprintf("%s", zParent);
6389        }
6390
6391        if( res==0 ){
6392          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6393        }else if( bVerbose ){
6394          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6395              zIndent, zFrom, zTarget
6396          );
6397        }
6398      }
6399    }
6400    sqlite3_free(zPrev);
6401
6402    if( rc!=SQLITE_OK ){
6403      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6404    }
6405
6406    rc2 = sqlite3_finalize(pSql);
6407    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6408      rc = rc2;
6409      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6410    }
6411  }else{
6412    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6413  }
6414
6415  return rc;
6416}
6417
6418/*
6419** Implementation of ".lint" dot command.
6420*/
6421static int lintDotCommand(
6422  ShellState *pState,             /* Current shell tool state */
6423  char **azArg,                   /* Array of arguments passed to dot command */
6424  int nArg                        /* Number of entries in azArg[] */
6425){
6426  int n;
6427  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6428  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6429  return lintFkeyIndexes(pState, azArg, nArg);
6430
6431 usage:
6432  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6433  raw_printf(stderr, "Where sub-commands are:\n");
6434  raw_printf(stderr, "    fkey-indexes\n");
6435  return SQLITE_ERROR;
6436}
6437
6438#if !defined SQLITE_OMIT_VIRTUALTABLE
6439static void shellPrepare(
6440  sqlite3 *db,
6441  int *pRc,
6442  const char *zSql,
6443  sqlite3_stmt **ppStmt
6444){
6445  *ppStmt = 0;
6446  if( *pRc==SQLITE_OK ){
6447    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6448    if( rc!=SQLITE_OK ){
6449      raw_printf(stderr, "sql error: %s (%d)\n",
6450          sqlite3_errmsg(db), sqlite3_errcode(db)
6451      );
6452      *pRc = rc;
6453    }
6454  }
6455}
6456
6457/*
6458** Create a prepared statement using printf-style arguments for the SQL.
6459**
6460** This routine is could be marked "static".  But it is not always used,
6461** depending on compile-time options.  By omitting the "static", we avoid
6462** nuisance compiler warnings about "defined but not used".
6463*/
6464void shellPreparePrintf(
6465  sqlite3 *db,
6466  int *pRc,
6467  sqlite3_stmt **ppStmt,
6468  const char *zFmt,
6469  ...
6470){
6471  *ppStmt = 0;
6472  if( *pRc==SQLITE_OK ){
6473    va_list ap;
6474    char *z;
6475    va_start(ap, zFmt);
6476    z = sqlite3_vmprintf(zFmt, ap);
6477    va_end(ap);
6478    if( z==0 ){
6479      *pRc = SQLITE_NOMEM;
6480    }else{
6481      shellPrepare(db, pRc, z, ppStmt);
6482      sqlite3_free(z);
6483    }
6484  }
6485}
6486
6487/* Finalize the prepared statement created using shellPreparePrintf().
6488**
6489** This routine is could be marked "static".  But it is not always used,
6490** depending on compile-time options.  By omitting the "static", we avoid
6491** nuisance compiler warnings about "defined but not used".
6492*/
6493void shellFinalize(
6494  int *pRc,
6495  sqlite3_stmt *pStmt
6496){
6497  if( pStmt ){
6498    sqlite3 *db = sqlite3_db_handle(pStmt);
6499    int rc = sqlite3_finalize(pStmt);
6500    if( *pRc==SQLITE_OK ){
6501      if( rc!=SQLITE_OK ){
6502        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6503      }
6504      *pRc = rc;
6505    }
6506  }
6507}
6508
6509/* Reset the prepared statement created using shellPreparePrintf().
6510**
6511** This routine is could be marked "static".  But it is not always used,
6512** depending on compile-time options.  By omitting the "static", we avoid
6513** nuisance compiler warnings about "defined but not used".
6514*/
6515void shellReset(
6516  int *pRc,
6517  sqlite3_stmt *pStmt
6518){
6519  int rc = sqlite3_reset(pStmt);
6520  if( *pRc==SQLITE_OK ){
6521    if( rc!=SQLITE_OK ){
6522      sqlite3 *db = sqlite3_db_handle(pStmt);
6523      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6524    }
6525    *pRc = rc;
6526  }
6527}
6528#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6529
6530#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6531/******************************************************************************
6532** The ".archive" or ".ar" command.
6533*/
6534/*
6535** Structure representing a single ".ar" command.
6536*/
6537typedef struct ArCommand ArCommand;
6538struct ArCommand {
6539  u8 eCmd;                        /* An AR_CMD_* value */
6540  u8 bVerbose;                    /* True if --verbose */
6541  u8 bZip;                        /* True if the archive is a ZIP */
6542  u8 bDryRun;                     /* True if --dry-run */
6543  u8 bAppend;                     /* True if --append */
6544  u8 bGlob;                       /* True if --glob */
6545  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6546  int nArg;                       /* Number of command arguments */
6547  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6548  const char *zFile;              /* --file argument, or NULL */
6549  const char *zDir;               /* --directory argument, or NULL */
6550  char **azArg;                   /* Array of command arguments */
6551  ShellState *p;                  /* Shell state */
6552  sqlite3 *db;                    /* Database containing the archive */
6553};
6554
6555/*
6556** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6557*/
6558static int arUsage(FILE *f){
6559  showHelp(f,"archive");
6560  return SQLITE_ERROR;
6561}
6562
6563/*
6564** Print an error message for the .ar command to stderr and return
6565** SQLITE_ERROR.
6566*/
6567static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6568  va_list ap;
6569  char *z;
6570  va_start(ap, zFmt);
6571  z = sqlite3_vmprintf(zFmt, ap);
6572  va_end(ap);
6573  utf8_printf(stderr, "Error: %s\n", z);
6574  if( pAr->fromCmdLine ){
6575    utf8_printf(stderr, "Use \"-A\" for more help\n");
6576  }else{
6577    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6578  }
6579  sqlite3_free(z);
6580  return SQLITE_ERROR;
6581}
6582
6583/*
6584** Values for ArCommand.eCmd.
6585*/
6586#define AR_CMD_CREATE       1
6587#define AR_CMD_UPDATE       2
6588#define AR_CMD_INSERT       3
6589#define AR_CMD_EXTRACT      4
6590#define AR_CMD_LIST         5
6591#define AR_CMD_HELP         6
6592#define AR_CMD_REMOVE       7
6593
6594/*
6595** Other (non-command) switches.
6596*/
6597#define AR_SWITCH_VERBOSE     8
6598#define AR_SWITCH_FILE        9
6599#define AR_SWITCH_DIRECTORY  10
6600#define AR_SWITCH_APPEND     11
6601#define AR_SWITCH_DRYRUN     12
6602#define AR_SWITCH_GLOB       13
6603
6604static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6605  switch( eSwitch ){
6606    case AR_CMD_CREATE:
6607    case AR_CMD_EXTRACT:
6608    case AR_CMD_LIST:
6609    case AR_CMD_REMOVE:
6610    case AR_CMD_UPDATE:
6611    case AR_CMD_INSERT:
6612    case AR_CMD_HELP:
6613      if( pAr->eCmd ){
6614        return arErrorMsg(pAr, "multiple command options");
6615      }
6616      pAr->eCmd = eSwitch;
6617      break;
6618
6619    case AR_SWITCH_DRYRUN:
6620      pAr->bDryRun = 1;
6621      break;
6622    case AR_SWITCH_GLOB:
6623      pAr->bGlob = 1;
6624      break;
6625    case AR_SWITCH_VERBOSE:
6626      pAr->bVerbose = 1;
6627      break;
6628    case AR_SWITCH_APPEND:
6629      pAr->bAppend = 1;
6630      /* Fall thru into --file */
6631    case AR_SWITCH_FILE:
6632      pAr->zFile = zArg;
6633      break;
6634    case AR_SWITCH_DIRECTORY:
6635      pAr->zDir = zArg;
6636      break;
6637  }
6638
6639  return SQLITE_OK;
6640}
6641
6642/*
6643** Parse the command line for an ".ar" command. The results are written into
6644** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6645** successfully, otherwise an error message is written to stderr and
6646** SQLITE_ERROR returned.
6647*/
6648static int arParseCommand(
6649  char **azArg,                   /* Array of arguments passed to dot command */
6650  int nArg,                       /* Number of entries in azArg[] */
6651  ArCommand *pAr                  /* Populate this object */
6652){
6653  struct ArSwitch {
6654    const char *zLong;
6655    char cShort;
6656    u8 eSwitch;
6657    u8 bArg;
6658  } aSwitch[] = {
6659    { "create",    'c', AR_CMD_CREATE,       0 },
6660    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6661    { "insert",    'i', AR_CMD_INSERT,       0 },
6662    { "list",      't', AR_CMD_LIST,         0 },
6663    { "remove",    'r', AR_CMD_REMOVE,       0 },
6664    { "update",    'u', AR_CMD_UPDATE,       0 },
6665    { "help",      'h', AR_CMD_HELP,         0 },
6666    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6667    { "file",      'f', AR_SWITCH_FILE,      1 },
6668    { "append",    'a', AR_SWITCH_APPEND,    1 },
6669    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6670    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6671    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6672  };
6673  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6674  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6675
6676  if( nArg<=1 ){
6677    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6678    return arUsage(stderr);
6679  }else{
6680    char *z = azArg[1];
6681    if( z[0]!='-' ){
6682      /* Traditional style [tar] invocation */
6683      int i;
6684      int iArg = 2;
6685      for(i=0; z[i]; i++){
6686        const char *zArg = 0;
6687        struct ArSwitch *pOpt;
6688        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6689          if( z[i]==pOpt->cShort ) break;
6690        }
6691        if( pOpt==pEnd ){
6692          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6693        }
6694        if( pOpt->bArg ){
6695          if( iArg>=nArg ){
6696            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6697          }
6698          zArg = azArg[iArg++];
6699        }
6700        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6701      }
6702      pAr->nArg = nArg-iArg;
6703      if( pAr->nArg>0 ){
6704        pAr->azArg = &azArg[iArg];
6705      }
6706    }else{
6707      /* Non-traditional invocation */
6708      int iArg;
6709      for(iArg=1; iArg<nArg; iArg++){
6710        int n;
6711        z = azArg[iArg];
6712        if( z[0]!='-' ){
6713          /* All remaining command line words are command arguments. */
6714          pAr->azArg = &azArg[iArg];
6715          pAr->nArg = nArg-iArg;
6716          break;
6717        }
6718        n = strlen30(z);
6719
6720        if( z[1]!='-' ){
6721          int i;
6722          /* One or more short options */
6723          for(i=1; i<n; i++){
6724            const char *zArg = 0;
6725            struct ArSwitch *pOpt;
6726            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6727              if( z[i]==pOpt->cShort ) break;
6728            }
6729            if( pOpt==pEnd ){
6730              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6731            }
6732            if( pOpt->bArg ){
6733              if( i<(n-1) ){
6734                zArg = &z[i+1];
6735                i = n;
6736              }else{
6737                if( iArg>=(nArg-1) ){
6738                  return arErrorMsg(pAr, "option requires an argument: %c",
6739                                    z[i]);
6740                }
6741                zArg = azArg[++iArg];
6742              }
6743            }
6744            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6745          }
6746        }else if( z[2]=='\0' ){
6747          /* A -- option, indicating that all remaining command line words
6748          ** are command arguments.  */
6749          pAr->azArg = &azArg[iArg+1];
6750          pAr->nArg = nArg-iArg-1;
6751          break;
6752        }else{
6753          /* A long option */
6754          const char *zArg = 0;             /* Argument for option, if any */
6755          struct ArSwitch *pMatch = 0;      /* Matching option */
6756          struct ArSwitch *pOpt;            /* Iterator */
6757          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6758            const char *zLong = pOpt->zLong;
6759            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6760              if( pMatch ){
6761                return arErrorMsg(pAr, "ambiguous option: %s",z);
6762              }else{
6763                pMatch = pOpt;
6764              }
6765            }
6766          }
6767
6768          if( pMatch==0 ){
6769            return arErrorMsg(pAr, "unrecognized option: %s", z);
6770          }
6771          if( pMatch->bArg ){
6772            if( iArg>=(nArg-1) ){
6773              return arErrorMsg(pAr, "option requires an argument: %s", z);
6774            }
6775            zArg = azArg[++iArg];
6776          }
6777          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6778        }
6779      }
6780    }
6781  }
6782
6783  return SQLITE_OK;
6784}
6785
6786/*
6787** This function assumes that all arguments within the ArCommand.azArg[]
6788** array refer to archive members, as for the --extract, --list or --remove
6789** commands. It checks that each of them are "present". If any specified
6790** file is not present in the archive, an error is printed to stderr and an
6791** error code returned. Otherwise, if all specified arguments are present
6792** in the archive, SQLITE_OK is returned. Here, "present" means either an
6793** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6794** when pAr->bGlob is true.
6795**
6796** This function strips any trailing '/' characters from each argument.
6797** This is consistent with the way the [tar] command seems to work on
6798** Linux.
6799*/
6800static int arCheckEntries(ArCommand *pAr){
6801  int rc = SQLITE_OK;
6802  if( pAr->nArg ){
6803    int i, j;
6804    sqlite3_stmt *pTest = 0;
6805    const char *zSel = (pAr->bGlob)
6806      ? "SELECT name FROM %s WHERE glob($name,name)"
6807      : "SELECT name FROM %s WHERE name=$name";
6808
6809    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6810    j = sqlite3_bind_parameter_index(pTest, "$name");
6811    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6812      char *z = pAr->azArg[i];
6813      int n = strlen30(z);
6814      int bOk = 0;
6815      while( n>0 && z[n-1]=='/' ) n--;
6816      z[n] = '\0';
6817      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6818      if( SQLITE_ROW==sqlite3_step(pTest) ){
6819        bOk = 1;
6820      }
6821      shellReset(&rc, pTest);
6822      if( rc==SQLITE_OK && bOk==0 ){
6823        utf8_printf(stderr, "not found in archive: %s\n", z);
6824        rc = SQLITE_ERROR;
6825      }
6826    }
6827    shellFinalize(&rc, pTest);
6828  }
6829  return rc;
6830}
6831
6832/*
6833** Format a WHERE clause that can be used against the "sqlar" table to
6834** identify all archive members that match the command arguments held
6835** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6836** The caller is responsible for eventually calling sqlite3_free() on
6837** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6838** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6839*/
6840static void arWhereClause(
6841  int *pRc,
6842  ArCommand *pAr,
6843  char **pzWhere                  /* OUT: New WHERE clause */
6844){
6845  char *zWhere = 0;
6846  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6847  if( *pRc==SQLITE_OK ){
6848    if( pAr->nArg==0 ){
6849      zWhere = sqlite3_mprintf("1");
6850    }else{
6851      int i;
6852      const char *zSep = "";
6853      for(i=0; i<pAr->nArg; i++){
6854        const char *z = pAr->azArg[i];
6855        zWhere = sqlite3_mprintf(
6856          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6857          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6858        );
6859        if( zWhere==0 ){
6860          *pRc = SQLITE_NOMEM;
6861          break;
6862        }
6863        zSep = " OR ";
6864      }
6865    }
6866  }
6867  *pzWhere = zWhere;
6868}
6869
6870/*
6871** Implementation of .ar "lisT" command.
6872*/
6873static int arListCommand(ArCommand *pAr){
6874  const char *zSql = "SELECT %s FROM %s WHERE %s";
6875  const char *azCols[] = {
6876    "name",
6877    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6878  };
6879
6880  char *zWhere = 0;
6881  sqlite3_stmt *pSql = 0;
6882  int rc;
6883
6884  rc = arCheckEntries(pAr);
6885  arWhereClause(&rc, pAr, &zWhere);
6886
6887  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6888                     pAr->zSrcTable, zWhere);
6889  if( pAr->bDryRun ){
6890    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6891  }else{
6892    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6893      if( pAr->bVerbose ){
6894        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6895            sqlite3_column_text(pSql, 0),
6896            sqlite3_column_int(pSql, 1),
6897            sqlite3_column_text(pSql, 2),
6898            sqlite3_column_text(pSql, 3)
6899        );
6900      }else{
6901        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6902      }
6903    }
6904  }
6905  shellFinalize(&rc, pSql);
6906  sqlite3_free(zWhere);
6907  return rc;
6908}
6909
6910
6911/*
6912** Implementation of .ar "Remove" command.
6913*/
6914static int arRemoveCommand(ArCommand *pAr){
6915  int rc = 0;
6916  char *zSql = 0;
6917  char *zWhere = 0;
6918
6919  if( pAr->nArg ){
6920    /* Verify that args actually exist within the archive before proceeding.
6921    ** And formulate a WHERE clause to match them.  */
6922    rc = arCheckEntries(pAr);
6923    arWhereClause(&rc, pAr, &zWhere);
6924  }
6925  if( rc==SQLITE_OK ){
6926    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6927                           pAr->zSrcTable, zWhere);
6928    if( pAr->bDryRun ){
6929      utf8_printf(pAr->p->out, "%s\n", zSql);
6930    }else{
6931      char *zErr = 0;
6932      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6933      if( rc==SQLITE_OK ){
6934        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6935        if( rc!=SQLITE_OK ){
6936          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6937        }else{
6938          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6939        }
6940      }
6941      if( zErr ){
6942        utf8_printf(stdout, "ERROR: %s\n", zErr);
6943        sqlite3_free(zErr);
6944      }
6945    }
6946  }
6947  sqlite3_free(zWhere);
6948  sqlite3_free(zSql);
6949  return rc;
6950}
6951
6952/*
6953** Implementation of .ar "eXtract" command.
6954*/
6955static int arExtractCommand(ArCommand *pAr){
6956  const char *zSql1 =
6957    "SELECT "
6958    " ($dir || name),"
6959    " writefile(($dir || name), %s, mode, mtime) "
6960    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6961    " AND name NOT GLOB '*..[/\\]*'";
6962
6963  const char *azExtraArg[] = {
6964    "sqlar_uncompress(data, sz)",
6965    "data"
6966  };
6967
6968  sqlite3_stmt *pSql = 0;
6969  int rc = SQLITE_OK;
6970  char *zDir = 0;
6971  char *zWhere = 0;
6972  int i, j;
6973
6974  /* If arguments are specified, check that they actually exist within
6975  ** the archive before proceeding. And formulate a WHERE clause to
6976  ** match them.  */
6977  rc = arCheckEntries(pAr);
6978  arWhereClause(&rc, pAr, &zWhere);
6979
6980  if( rc==SQLITE_OK ){
6981    if( pAr->zDir ){
6982      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6983    }else{
6984      zDir = sqlite3_mprintf("");
6985    }
6986    if( zDir==0 ) rc = SQLITE_NOMEM;
6987  }
6988
6989  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6990      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6991  );
6992
6993  if( rc==SQLITE_OK ){
6994    j = sqlite3_bind_parameter_index(pSql, "$dir");
6995    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6996
6997    /* Run the SELECT statement twice. The first time, writefile() is called
6998    ** for all archive members that should be extracted. The second time,
6999    ** only for the directories. This is because the timestamps for
7000    ** extracted directories must be reset after they are populated (as
7001    ** populating them changes the timestamp).  */
7002    for(i=0; i<2; i++){
7003      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
7004      sqlite3_bind_int(pSql, j, i);
7005      if( pAr->bDryRun ){
7006        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
7007      }else{
7008        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7009          if( i==0 && pAr->bVerbose ){
7010            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
7011          }
7012        }
7013      }
7014      shellReset(&rc, pSql);
7015    }
7016    shellFinalize(&rc, pSql);
7017  }
7018
7019  sqlite3_free(zDir);
7020  sqlite3_free(zWhere);
7021  return rc;
7022}
7023
7024/*
7025** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
7026*/
7027static int arExecSql(ArCommand *pAr, const char *zSql){
7028  int rc;
7029  if( pAr->bDryRun ){
7030    utf8_printf(pAr->p->out, "%s\n", zSql);
7031    rc = SQLITE_OK;
7032  }else{
7033    char *zErr = 0;
7034    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7035    if( zErr ){
7036      utf8_printf(stdout, "ERROR: %s\n", zErr);
7037      sqlite3_free(zErr);
7038    }
7039  }
7040  return rc;
7041}
7042
7043
7044/*
7045** Implementation of .ar "create", "insert", and "update" commands.
7046**
7047**     create    ->     Create a new SQL archive
7048**     insert    ->     Insert or reinsert all files listed
7049**     update    ->     Insert files that have changed or that were not
7050**                      previously in the archive
7051**
7052** Create the "sqlar" table in the database if it does not already exist.
7053** Then add each file in the azFile[] array to the archive. Directories
7054** are added recursively. If argument bVerbose is non-zero, a message is
7055** printed on stdout for each file archived.
7056**
7057** The create command is the same as update, except that it drops
7058** any existing "sqlar" table before beginning.  The "insert" command
7059** always overwrites every file named on the command-line, where as
7060** "update" only overwrites if the size or mtime or mode has changed.
7061*/
7062static int arCreateOrUpdateCommand(
7063  ArCommand *pAr,                 /* Command arguments and options */
7064  int bUpdate,                    /* true for a --create. */
7065  int bOnlyIfChanged              /* Only update if file has changed */
7066){
7067  const char *zCreate =
7068      "CREATE TABLE IF NOT EXISTS sqlar(\n"
7069      "  name TEXT PRIMARY KEY,  -- name of the file\n"
7070      "  mode INT,               -- access permissions\n"
7071      "  mtime INT,              -- last modification time\n"
7072      "  sz INT,                 -- original file size\n"
7073      "  data BLOB               -- compressed content\n"
7074      ")";
7075  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7076  const char *zInsertFmt[2] = {
7077     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7078     "  SELECT\n"
7079     "    %s,\n"
7080     "    mode,\n"
7081     "    mtime,\n"
7082     "    CASE substr(lsmode(mode),1,1)\n"
7083     "      WHEN '-' THEN length(data)\n"
7084     "      WHEN 'd' THEN 0\n"
7085     "      ELSE -1 END,\n"
7086     "    sqlar_compress(data)\n"
7087     "  FROM fsdir(%Q,%Q) AS disk\n"
7088     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7089     ,
7090     "REPLACE INTO %s(name,mode,mtime,data)\n"
7091     "  SELECT\n"
7092     "    %s,\n"
7093     "    mode,\n"
7094     "    mtime,\n"
7095     "    data\n"
7096     "  FROM fsdir(%Q,%Q) AS disk\n"
7097     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7098  };
7099  int i;                          /* For iterating through azFile[] */
7100  int rc;                         /* Return code */
7101  const char *zTab = 0;           /* SQL table into which to insert */
7102  char *zSql;
7103  char zTemp[50];
7104  char *zExists = 0;
7105
7106  arExecSql(pAr, "PRAGMA page_size=512");
7107  rc = arExecSql(pAr, "SAVEPOINT ar;");
7108  if( rc!=SQLITE_OK ) return rc;
7109  zTemp[0] = 0;
7110  if( pAr->bZip ){
7111    /* Initialize the zipfile virtual table, if necessary */
7112    if( pAr->zFile ){
7113      sqlite3_uint64 r;
7114      sqlite3_randomness(sizeof(r),&r);
7115      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7116      zTab = zTemp;
7117      zSql = sqlite3_mprintf(
7118         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7119         zTab, pAr->zFile
7120      );
7121      rc = arExecSql(pAr, zSql);
7122      sqlite3_free(zSql);
7123    }else{
7124      zTab = "zip";
7125    }
7126  }else{
7127    /* Initialize the table for an SQLAR */
7128    zTab = "sqlar";
7129    if( bUpdate==0 ){
7130      rc = arExecSql(pAr, zDrop);
7131      if( rc!=SQLITE_OK ) goto end_ar_transaction;
7132    }
7133    rc = arExecSql(pAr, zCreate);
7134  }
7135  if( bOnlyIfChanged ){
7136    zExists = sqlite3_mprintf(
7137      " AND NOT EXISTS("
7138          "SELECT 1 FROM %s AS mem"
7139          " WHERE mem.name=disk.name"
7140          " AND mem.mtime=disk.mtime"
7141          " AND mem.mode=disk.mode)", zTab);
7142  }else{
7143    zExists = sqlite3_mprintf("");
7144  }
7145  if( zExists==0 ) rc = SQLITE_NOMEM;
7146  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7147    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7148        pAr->bVerbose ? "shell_putsnl(name)" : "name",
7149        pAr->azArg[i], pAr->zDir, zExists);
7150    rc = arExecSql(pAr, zSql2);
7151    sqlite3_free(zSql2);
7152  }
7153end_ar_transaction:
7154  if( rc!=SQLITE_OK ){
7155    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7156  }else{
7157    rc = arExecSql(pAr, "RELEASE ar;");
7158    if( pAr->bZip && pAr->zFile ){
7159      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7160      arExecSql(pAr, zSql);
7161      sqlite3_free(zSql);
7162    }
7163  }
7164  sqlite3_free(zExists);
7165  return rc;
7166}
7167
7168/*
7169** Implementation of ".ar" dot command.
7170*/
7171static int arDotCommand(
7172  ShellState *pState,          /* Current shell tool state */
7173  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
7174  char **azArg,                /* Array of arguments passed to dot command */
7175  int nArg                     /* Number of entries in azArg[] */
7176){
7177  ArCommand cmd;
7178  int rc;
7179  memset(&cmd, 0, sizeof(cmd));
7180  cmd.fromCmdLine = fromCmdLine;
7181  rc = arParseCommand(azArg, nArg, &cmd);
7182  if( rc==SQLITE_OK ){
7183    int eDbType = SHELL_OPEN_UNSPEC;
7184    cmd.p = pState;
7185    cmd.db = pState->db;
7186    if( cmd.zFile ){
7187      eDbType = deduceDatabaseType(cmd.zFile, 1);
7188    }else{
7189      eDbType = pState->openMode;
7190    }
7191    if( eDbType==SHELL_OPEN_ZIPFILE ){
7192      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7193        if( cmd.zFile==0 ){
7194          cmd.zSrcTable = sqlite3_mprintf("zip");
7195        }else{
7196          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7197        }
7198      }
7199      cmd.bZip = 1;
7200    }else if( cmd.zFile ){
7201      int flags;
7202      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7203      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7204           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7205        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7206      }else{
7207        flags = SQLITE_OPEN_READONLY;
7208      }
7209      cmd.db = 0;
7210      if( cmd.bDryRun ){
7211        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7212             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7213      }
7214      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7215             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7216      if( rc!=SQLITE_OK ){
7217        utf8_printf(stderr, "cannot open file: %s (%s)\n",
7218            cmd.zFile, sqlite3_errmsg(cmd.db)
7219        );
7220        goto end_ar_command;
7221      }
7222      sqlite3_fileio_init(cmd.db, 0, 0);
7223      sqlite3_sqlar_init(cmd.db, 0, 0);
7224      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7225                              shellPutsFunc, 0, 0);
7226
7227    }
7228    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7229      if( cmd.eCmd!=AR_CMD_CREATE
7230       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7231      ){
7232        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7233        rc = SQLITE_ERROR;
7234        goto end_ar_command;
7235      }
7236      cmd.zSrcTable = sqlite3_mprintf("sqlar");
7237    }
7238
7239    switch( cmd.eCmd ){
7240      case AR_CMD_CREATE:
7241        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7242        break;
7243
7244      case AR_CMD_EXTRACT:
7245        rc = arExtractCommand(&cmd);
7246        break;
7247
7248      case AR_CMD_LIST:
7249        rc = arListCommand(&cmd);
7250        break;
7251
7252      case AR_CMD_HELP:
7253        arUsage(pState->out);
7254        break;
7255
7256      case AR_CMD_INSERT:
7257        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7258        break;
7259
7260      case AR_CMD_REMOVE:
7261        rc = arRemoveCommand(&cmd);
7262        break;
7263
7264      default:
7265        assert( cmd.eCmd==AR_CMD_UPDATE );
7266        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7267        break;
7268    }
7269  }
7270end_ar_command:
7271  if( cmd.db!=pState->db ){
7272    close_db(cmd.db);
7273  }
7274  sqlite3_free(cmd.zSrcTable);
7275
7276  return rc;
7277}
7278/* End of the ".archive" or ".ar" command logic
7279*******************************************************************************/
7280#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7281
7282#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7283
7284/*
7285** This function is used as a callback by the recover extension. Simply
7286** print the supplied SQL statement to stdout.
7287*/
7288static int recoverSqlCb(void *pCtx, const char *zSql){
7289  ShellState *pState = (ShellState*)pCtx;
7290  utf8_printf(pState->out, "%s;\n", zSql);
7291  return SQLITE_OK;
7292}
7293
7294/*
7295** This function is called to recover data from the database. A script
7296** to construct a new database containing all recovered data is output
7297** on stream pState->out.
7298*/
7299static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7300  int rc = SQLITE_OK;
7301  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7302  const char *zLAF = "lost_and_found";
7303  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7304  int bRowids = 1;                /* 0 if --no-rowids */
7305  sqlite3_recover *p = 0;
7306  int i = 0;
7307
7308  for(i=1; i<nArg; i++){
7309    char *z = azArg[i];
7310    int n;
7311    if( z[0]=='-' && z[1]=='-' ) z++;
7312    n = strlen30(z);
7313    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7314      bFreelist = 0;
7315    }else
7316    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7317      i++;
7318      zRecoveryDb = azArg[i];
7319    }else
7320    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7321      i++;
7322      zLAF = azArg[i];
7323    }else
7324    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7325      bRowids = 0;
7326    }
7327    else{
7328      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7329      showHelp(pState->out, azArg[0]);
7330      return 1;
7331    }
7332  }
7333
7334  p = sqlite3_recover_init_sql(
7335      pState->db, "main", recoverSqlCb, (void*)pState
7336  );
7337
7338  sqlite3_recover_config(p, 789, (void*)zRecoveryDb);
7339  sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
7340  sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
7341  sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
7342
7343  sqlite3_recover_run(p);
7344  if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
7345    const char *zErr = sqlite3_recover_errmsg(p);
7346    int errCode = sqlite3_recover_errcode(p);
7347    raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
7348  }
7349  rc = sqlite3_recover_finish(p);
7350  return rc;
7351}
7352#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7353
7354
7355/*
7356 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7357 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7358 *   close db and set it to 0, and return the columns spec, to later
7359 *   be sqlite3_free()'ed by the caller.
7360 * The return is 0 when either:
7361 *   (a) The db was not initialized and zCol==0 (There are no columns.)
7362 *   (b) zCol!=0  (Column was added, db initialized as needed.)
7363 * The 3rd argument, pRenamed, references an out parameter. If the
7364 * pointer is non-zero, its referent will be set to a summary of renames
7365 * done if renaming was necessary, or set to 0 if none was done. The out
7366 * string (if any) must be sqlite3_free()'ed by the caller.
7367 */
7368#ifdef SHELL_DEBUG
7369#define rc_err_oom_die(rc) \
7370  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7371  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7372    fprintf(stderr,"E:%d\n",rc), assert(0)
7373#else
7374static void rc_err_oom_die(int rc){
7375  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7376  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7377}
7378#endif
7379
7380#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7381static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7382#else  /* Otherwise, memory is faster/better for the transient DB. */
7383static const char *zCOL_DB = ":memory:";
7384#endif
7385
7386/* Define character (as C string) to separate generated column ordinal
7387 * from protected part of incoming column names. This defaults to "_"
7388 * so that incoming column identifiers that did not need not be quoted
7389 * remain usable without being quoted. It must be one character.
7390 */
7391#ifndef SHELL_AUTOCOLUMN_SEP
7392# define AUTOCOLUMN_SEP "_"
7393#else
7394# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7395#endif
7396
7397static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
7398  /* Queries and D{D,M}L used here */
7399  static const char * const zTabMake = "\
7400CREATE TABLE ColNames(\
7401 cpos INTEGER PRIMARY KEY,\
7402 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7403CREATE VIEW RepeatedNames AS \
7404SELECT DISTINCT t.name FROM ColNames t \
7405WHERE t.name COLLATE NOCASE IN (\
7406 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7407);\
7408";
7409  static const char * const zTabFill = "\
7410INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7411 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
7412";
7413  static const char * const zHasDupes = "\
7414SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
7415 <count(name) FROM ColNames\
7416";
7417#ifdef SHELL_COLUMN_RENAME_CLEAN
7418  static const char * const zDedoctor = "\
7419UPDATE ColNames SET chop=iif(\
7420  (substring(name,nlen,1) BETWEEN '0' AND '9')\
7421  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
7422 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
7423 0\
7424)\
7425";
7426#endif
7427  static const char * const zSetReps = "\
7428UPDATE ColNames AS t SET reps=\
7429(SELECT count(*) FROM ColNames d \
7430 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
7431 COLLATE NOCASE\
7432)\
7433";
7434#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
7435  static const char * const zColDigits = "\
7436SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
7437";
7438#else
7439  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
7440  static const char * const zColDigits = "\
7441SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
7442 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
7443 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
7444";
7445#endif
7446  static const char * const zRenameRank =
7447#ifdef SHELL_COLUMN_RENAME_CLEAN
7448    "UPDATE ColNames AS t SET suff="
7449    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
7450#else /* ...RENAME_MINIMAL_ONE_PASS */
7451"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
7452"  SELECT 0 AS nlz"
7453"  UNION"
7454"  SELECT nlz+1 AS nlz FROM Lzn"
7455"  WHERE EXISTS("
7456"   SELECT 1"
7457"   FROM ColNames t, ColNames o"
7458"   WHERE"
7459"    iif(t.name IN (SELECT * FROM RepeatedNames),"
7460"     printf('%s"AUTOCOLUMN_SEP"%s',"
7461"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
7462"     t.name"
7463"    )"
7464"    ="
7465"    iif(o.name IN (SELECT * FROM RepeatedNames),"
7466"     printf('%s"AUTOCOLUMN_SEP"%s',"
7467"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
7468"     o.name"
7469"    )"
7470"    COLLATE NOCASE"
7471"    AND o.cpos<>t.cpos"
7472"   GROUP BY t.cpos"
7473"  )"
7474") UPDATE Colnames AS t SET"
7475" chop = 0," /* No chopping, never touch incoming names. */
7476" suff = iif(name IN (SELECT * FROM RepeatedNames),"
7477"  printf('"AUTOCOLUMN_SEP"%s', substring("
7478"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
7479"  ''"
7480" )"
7481#endif
7482    ;
7483  static const char * const zCollectVar = "\
7484SELECT\
7485 '('||x'0a'\
7486 || group_concat(\
7487  cname||' TEXT',\
7488  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
7489 ||')' AS ColsSpec \
7490FROM (\
7491 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
7492 FROM ColNames ORDER BY cpos\
7493)";
7494  static const char * const zRenamesDone =
7495    "SELECT group_concat("
7496    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
7497    " ','||x'0a')"
7498    "FROM ColNames WHERE suff<>'' OR chop!=0"
7499    ;
7500  int rc;
7501  sqlite3_stmt *pStmt = 0;
7502  assert(pDb!=0);
7503  if( zColNew ){
7504    /* Add initial or additional column. Init db if necessary. */
7505    if( *pDb==0 ){
7506      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
7507#ifdef SHELL_COLFIX_DB
7508      if(*zCOL_DB!=':')
7509        sqlite3_exec(*pDb,"drop table if exists ColNames;"
7510                     "drop view if exists RepeatedNames;",0,0,0);
7511#endif
7512      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
7513      rc_err_oom_die(rc);
7514    }
7515    assert(*pDb!=0);
7516    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
7517    rc_err_oom_die(rc);
7518    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
7519    rc_err_oom_die(rc);
7520    rc = sqlite3_step(pStmt);
7521    rc_err_oom_die(rc);
7522    sqlite3_finalize(pStmt);
7523    return 0;
7524  }else if( *pDb==0 ){
7525    return 0;
7526  }else{
7527    /* Formulate the columns spec, close the DB, zero *pDb. */
7528    char *zColsSpec = 0;
7529    int hasDupes = db_int(*pDb, zHasDupes);
7530    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
7531    if( hasDupes ){
7532#ifdef SHELL_COLUMN_RENAME_CLEAN
7533      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
7534      rc_err_oom_die(rc);
7535#endif
7536      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
7537      rc_err_oom_die(rc);
7538      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
7539      rc_err_oom_die(rc);
7540      sqlite3_bind_int(pStmt, 1, nDigits);
7541      rc = sqlite3_step(pStmt);
7542      sqlite3_finalize(pStmt);
7543      assert(rc==SQLITE_DONE);
7544    }
7545    assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
7546    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
7547    rc_err_oom_die(rc);
7548    rc = sqlite3_step(pStmt);
7549    if( rc==SQLITE_ROW ){
7550      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7551    }else{
7552      zColsSpec = 0;
7553    }
7554    if( pzRenamed!=0 ){
7555      if( !hasDupes ) *pzRenamed = 0;
7556      else{
7557        sqlite3_finalize(pStmt);
7558        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
7559            && SQLITE_ROW==sqlite3_step(pStmt) ){
7560          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7561        }else
7562          *pzRenamed = 0;
7563      }
7564    }
7565    sqlite3_finalize(pStmt);
7566    sqlite3_close(*pDb);
7567    *pDb = 0;
7568    return zColsSpec;
7569  }
7570}
7571
7572/*
7573** If an input line begins with "." then invoke this routine to
7574** process that line.
7575**
7576** Return 1 on error, 2 to exit, and 0 otherwise.
7577*/
7578static int do_meta_command(char *zLine, ShellState *p){
7579  int h = 1;
7580  int nArg = 0;
7581  int n, c;
7582  int rc = 0;
7583  char *azArg[52];
7584
7585#ifndef SQLITE_OMIT_VIRTUALTABLE
7586  if( p->expert.pExpert ){
7587    expertFinish(p, 1, 0);
7588  }
7589#endif
7590
7591  /* Parse the input line into tokens.
7592  */
7593  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7594    while( IsSpace(zLine[h]) ){ h++; }
7595    if( zLine[h]==0 ) break;
7596    if( zLine[h]=='\'' || zLine[h]=='"' ){
7597      int delim = zLine[h++];
7598      azArg[nArg++] = &zLine[h];
7599      while( zLine[h] && zLine[h]!=delim ){
7600        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7601        h++;
7602      }
7603      if( zLine[h]==delim ){
7604        zLine[h++] = 0;
7605      }
7606      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7607    }else{
7608      azArg[nArg++] = &zLine[h];
7609      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7610      if( zLine[h] ) zLine[h++] = 0;
7611      resolve_backslashes(azArg[nArg-1]);
7612    }
7613  }
7614  azArg[nArg] = 0;
7615
7616  /* Process the input line.
7617  */
7618  if( nArg==0 ) return 0; /* no tokens, no error */
7619  n = strlen30(azArg[0]);
7620  c = azArg[0][0];
7621  clearTempFile(p);
7622
7623#ifndef SQLITE_OMIT_AUTHORIZATION
7624  if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
7625    if( nArg!=2 ){
7626      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7627      rc = 1;
7628      goto meta_command_exit;
7629    }
7630    open_db(p, 0);
7631    if( booleanValue(azArg[1]) ){
7632      sqlite3_set_authorizer(p->db, shellAuth, p);
7633    }else if( p->bSafeModePersist ){
7634      sqlite3_set_authorizer(p->db, safeModeAuth, p);
7635    }else{
7636      sqlite3_set_authorizer(p->db, 0, 0);
7637    }
7638  }else
7639#endif
7640
7641#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
7642  && !defined(SQLITE_SHELL_FIDDLE)
7643  if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
7644    open_db(p, 0);
7645    failIfSafeMode(p, "cannot run .archive in safe mode");
7646    rc = arDotCommand(p, 0, azArg, nArg);
7647  }else
7648#endif
7649
7650#ifndef SQLITE_SHELL_FIDDLE
7651  if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
7652   || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
7653  ){
7654    const char *zDestFile = 0;
7655    const char *zDb = 0;
7656    sqlite3 *pDest;
7657    sqlite3_backup *pBackup;
7658    int j;
7659    int bAsync = 0;
7660    const char *zVfs = 0;
7661    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
7662    for(j=1; j<nArg; j++){
7663      const char *z = azArg[j];
7664      if( z[0]=='-' ){
7665        if( z[1]=='-' ) z++;
7666        if( cli_strcmp(z, "-append")==0 ){
7667          zVfs = "apndvfs";
7668        }else
7669        if( cli_strcmp(z, "-async")==0 ){
7670          bAsync = 1;
7671        }else
7672        {
7673          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7674          return 1;
7675        }
7676      }else if( zDestFile==0 ){
7677        zDestFile = azArg[j];
7678      }else if( zDb==0 ){
7679        zDb = zDestFile;
7680        zDestFile = azArg[j];
7681      }else{
7682        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7683        return 1;
7684      }
7685    }
7686    if( zDestFile==0 ){
7687      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7688      return 1;
7689    }
7690    if( zDb==0 ) zDb = "main";
7691    rc = sqlite3_open_v2(zDestFile, &pDest,
7692                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7693    if( rc!=SQLITE_OK ){
7694      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7695      close_db(pDest);
7696      return 1;
7697    }
7698    if( bAsync ){
7699      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7700                   0, 0, 0);
7701    }
7702    open_db(p, 0);
7703    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7704    if( pBackup==0 ){
7705      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7706      close_db(pDest);
7707      return 1;
7708    }
7709    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7710    sqlite3_backup_finish(pBackup);
7711    if( rc==SQLITE_DONE ){
7712      rc = 0;
7713    }else{
7714      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7715      rc = 1;
7716    }
7717    close_db(pDest);
7718  }else
7719#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7720
7721  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
7722    if( nArg==2 ){
7723      bail_on_error = booleanValue(azArg[1]);
7724    }else{
7725      raw_printf(stderr, "Usage: .bail on|off\n");
7726      rc = 1;
7727    }
7728  }else
7729
7730  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
7731    if( nArg==2 ){
7732      if( booleanValue(azArg[1]) ){
7733        setBinaryMode(p->out, 1);
7734      }else{
7735        setTextMode(p->out, 1);
7736      }
7737    }else{
7738      raw_printf(stderr, "Usage: .binary on|off\n");
7739      rc = 1;
7740    }
7741  }else
7742
7743  /* The undocumented ".breakpoint" command causes a call to the no-op
7744  ** routine named test_breakpoint().
7745  */
7746  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
7747    test_breakpoint();
7748  }else
7749
7750#ifndef SQLITE_SHELL_FIDDLE
7751  if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
7752    failIfSafeMode(p, "cannot run .cd in safe mode");
7753    if( nArg==2 ){
7754#if defined(_WIN32) || defined(WIN32)
7755      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7756      rc = !SetCurrentDirectoryW(z);
7757      sqlite3_free(z);
7758#else
7759      rc = chdir(azArg[1]);
7760#endif
7761      if( rc ){
7762        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7763        rc = 1;
7764      }
7765    }else{
7766      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7767      rc = 1;
7768    }
7769  }else
7770#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7771
7772  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
7773    if( nArg==2 ){
7774      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7775    }else{
7776      raw_printf(stderr, "Usage: .changes on|off\n");
7777      rc = 1;
7778    }
7779  }else
7780
7781#ifndef SQLITE_SHELL_FIDDLE
7782  /* Cancel output redirection, if it is currently set (by .testcase)
7783  ** Then read the content of the testcase-out.txt file and compare against
7784  ** azArg[1].  If there are differences, report an error and exit.
7785  */
7786  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
7787    char *zRes = 0;
7788    output_reset(p);
7789    if( nArg!=2 ){
7790      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7791      rc = 2;
7792    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7793      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7794      rc = 2;
7795    }else if( testcase_glob(azArg[1],zRes)==0 ){
7796      utf8_printf(stderr,
7797                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7798                 p->zTestcase, azArg[1], zRes);
7799      rc = 1;
7800    }else{
7801      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7802      p->nCheck++;
7803    }
7804    sqlite3_free(zRes);
7805  }else
7806#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7807
7808#ifndef SQLITE_SHELL_FIDDLE
7809  if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
7810    failIfSafeMode(p, "cannot run .clone in safe mode");
7811    if( nArg==2 ){
7812      tryToClone(p, azArg[1]);
7813    }else{
7814      raw_printf(stderr, "Usage: .clone FILENAME\n");
7815      rc = 1;
7816    }
7817  }else
7818#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7819
7820  if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
7821    if( nArg==1 ){
7822      /* List available connections */
7823      int i;
7824      for(i=0; i<ArraySize(p->aAuxDb); i++){
7825        const char *zFile = p->aAuxDb[i].zDbFilename;
7826        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
7827          zFile = "(not open)";
7828        }else if( zFile==0 ){
7829          zFile = "(memory)";
7830        }else if( zFile[0]==0 ){
7831          zFile = "(temporary-file)";
7832        }
7833        if( p->pAuxDb == &p->aAuxDb[i] ){
7834          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
7835        }else if( p->aAuxDb[i].db!=0 ){
7836          utf8_printf(stdout, "       %d: %s\n", i, zFile);
7837        }
7838      }
7839    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
7840      int i = azArg[1][0] - '0';
7841      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
7842        p->pAuxDb->db = p->db;
7843        p->pAuxDb = &p->aAuxDb[i];
7844        globalDb = p->db = p->pAuxDb->db;
7845        p->pAuxDb->db = 0;
7846      }
7847    }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
7848           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
7849      int i = azArg[2][0] - '0';
7850      if( i<0 || i>=ArraySize(p->aAuxDb) ){
7851        /* No-op */
7852      }else if( p->pAuxDb == &p->aAuxDb[i] ){
7853        raw_printf(stderr, "cannot close the active database connection\n");
7854        rc = 1;
7855      }else if( p->aAuxDb[i].db ){
7856        session_close_all(p, i);
7857        close_db(p->aAuxDb[i].db);
7858        p->aAuxDb[i].db = 0;
7859      }
7860    }else{
7861      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
7862      rc = 1;
7863    }
7864  }else
7865
7866  if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
7867    char **azName = 0;
7868    int nName = 0;
7869    sqlite3_stmt *pStmt;
7870    int i;
7871    open_db(p, 0);
7872    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7873    if( rc ){
7874      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7875      rc = 1;
7876    }else{
7877      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7878        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7879        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7880        if( zSchema==0 || zFile==0 ) continue;
7881        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7882        shell_check_oom(azName);
7883        azName[nName*2] = strdup(zSchema);
7884        azName[nName*2+1] = strdup(zFile);
7885        nName++;
7886      }
7887    }
7888    sqlite3_finalize(pStmt);
7889    for(i=0; i<nName; i++){
7890      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7891      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7892      const char *z = azName[i*2+1];
7893      utf8_printf(p->out, "%s: %s %s%s\n",
7894         azName[i*2],
7895         z && z[0] ? z : "\"\"",
7896         bRdonly ? "r/o" : "r/w",
7897         eTxn==SQLITE_TXN_NONE ? "" :
7898            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7899      free(azName[i*2]);
7900      free(azName[i*2+1]);
7901    }
7902    sqlite3_free(azName);
7903  }else
7904
7905  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
7906    static const struct DbConfigChoices {
7907      const char *zName;
7908      int op;
7909    } aDbConfig[] = {
7910        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7911        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7912        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7913        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7914        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7915        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7916        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7917        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7918        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7919        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7920        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7921        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7922        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7923        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7924        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7925        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7926    };
7927    int ii, v;
7928    open_db(p, 0);
7929    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7930      if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7931      if( nArg>=3 ){
7932        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7933      }
7934      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7935      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7936      if( nArg>1 ) break;
7937    }
7938    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7939      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7940      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7941    }
7942  }else
7943
7944#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7945  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
7946    rc = shell_dbinfo_command(p, nArg, azArg);
7947  }else
7948
7949  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
7950    open_db(p, 0);
7951    rc = recoverDatabaseCmd(p, nArg, azArg);
7952  }else
7953#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7954
7955  if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
7956    char *zLike = 0;
7957    char *zSql;
7958    int i;
7959    int savedShowHeader = p->showHeader;
7960    int savedShellFlags = p->shellFlgs;
7961    ShellClearFlag(p,
7962       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7963       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7964    for(i=1; i<nArg; i++){
7965      if( azArg[i][0]=='-' ){
7966        const char *z = azArg[i]+1;
7967        if( z[0]=='-' ) z++;
7968        if( cli_strcmp(z,"preserve-rowids")==0 ){
7969#ifdef SQLITE_OMIT_VIRTUALTABLE
7970          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7971                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7972          rc = 1;
7973          sqlite3_free(zLike);
7974          goto meta_command_exit;
7975#else
7976          ShellSetFlag(p, SHFLG_PreserveRowid);
7977#endif
7978        }else
7979        if( cli_strcmp(z,"newlines")==0 ){
7980          ShellSetFlag(p, SHFLG_Newlines);
7981        }else
7982        if( cli_strcmp(z,"data-only")==0 ){
7983          ShellSetFlag(p, SHFLG_DumpDataOnly);
7984        }else
7985        if( cli_strcmp(z,"nosys")==0 ){
7986          ShellSetFlag(p, SHFLG_DumpNoSys);
7987        }else
7988        {
7989          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7990          rc = 1;
7991          sqlite3_free(zLike);
7992          goto meta_command_exit;
7993        }
7994      }else{
7995        /* azArg[i] contains a LIKE pattern. This ".dump" request should
7996        ** only dump data for tables for which either the table name matches
7997        ** the LIKE pattern, or the table appears to be a shadow table of
7998        ** a virtual table for which the name matches the LIKE pattern.
7999        */
8000        char *zExpr = sqlite3_mprintf(
8001            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8002            "  SELECT 1 FROM sqlite_schema WHERE "
8003            "    name LIKE %Q ESCAPE '\\' AND"
8004            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8005            "    substr(o.name, 1, length(name)+1) == (name||'_')"
8006            ")", azArg[i], azArg[i]
8007        );
8008
8009        if( zLike ){
8010          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8011        }else{
8012          zLike = zExpr;
8013        }
8014      }
8015    }
8016
8017    open_db(p, 0);
8018
8019    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8020      /* When playing back a "dump", the content might appear in an order
8021      ** which causes immediate foreign key constraints to be violated.
8022      ** So disable foreign-key constraint enforcement to prevent problems. */
8023      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8024      raw_printf(p->out, "BEGIN TRANSACTION;\n");
8025    }
8026    p->writableSchema = 0;
8027    p->showHeader = 0;
8028    /* Set writable_schema=ON since doing so forces SQLite to initialize
8029    ** as much of the schema as it can even if the sqlite_schema table is
8030    ** corrupt. */
8031    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8032    p->nErr = 0;
8033    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8034    zSql = sqlite3_mprintf(
8035      "SELECT name, type, sql FROM sqlite_schema AS o "
8036      "WHERE (%s) AND type=='table'"
8037      "  AND sql NOT NULL"
8038      " ORDER BY tbl_name='sqlite_sequence', rowid",
8039      zLike
8040    );
8041    run_schema_dump_query(p,zSql);
8042    sqlite3_free(zSql);
8043    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8044      zSql = sqlite3_mprintf(
8045        "SELECT sql FROM sqlite_schema AS o "
8046        "WHERE (%s) AND sql NOT NULL"
8047        "  AND type IN ('index','trigger','view')",
8048        zLike
8049      );
8050      run_table_dump_query(p, zSql);
8051      sqlite3_free(zSql);
8052    }
8053    sqlite3_free(zLike);
8054    if( p->writableSchema ){
8055      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8056      p->writableSchema = 0;
8057    }
8058    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8059    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8060    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8061      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8062    }
8063    p->showHeader = savedShowHeader;
8064    p->shellFlgs = savedShellFlags;
8065  }else
8066
8067  if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
8068    if( nArg==2 ){
8069      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8070    }else{
8071      raw_printf(stderr, "Usage: .echo on|off\n");
8072      rc = 1;
8073    }
8074  }else
8075
8076  if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
8077    if( nArg==2 ){
8078      p->autoEQPtest = 0;
8079      if( p->autoEQPtrace ){
8080        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8081        p->autoEQPtrace = 0;
8082      }
8083      if( cli_strcmp(azArg[1],"full")==0 ){
8084        p->autoEQP = AUTOEQP_full;
8085      }else if( cli_strcmp(azArg[1],"trigger")==0 ){
8086        p->autoEQP = AUTOEQP_trigger;
8087#ifdef SQLITE_DEBUG
8088      }else if( cli_strcmp(azArg[1],"test")==0 ){
8089        p->autoEQP = AUTOEQP_on;
8090        p->autoEQPtest = 1;
8091      }else if( cli_strcmp(azArg[1],"trace")==0 ){
8092        p->autoEQP = AUTOEQP_full;
8093        p->autoEQPtrace = 1;
8094        open_db(p, 0);
8095        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8096        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8097#endif
8098      }else{
8099        p->autoEQP = (u8)booleanValue(azArg[1]);
8100      }
8101    }else{
8102      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8103      rc = 1;
8104    }
8105  }else
8106
8107#ifndef SQLITE_SHELL_FIDDLE
8108  if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
8109    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8110    rc = 2;
8111  }else
8112#endif
8113
8114  /* The ".explain" command is automatic now.  It is largely pointless.  It
8115  ** retained purely for backwards compatibility */
8116  if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
8117    int val = 1;
8118    if( nArg>=2 ){
8119      if( cli_strcmp(azArg[1],"auto")==0 ){
8120        val = 99;
8121      }else{
8122        val =  booleanValue(azArg[1]);
8123      }
8124    }
8125    if( val==1 && p->mode!=MODE_Explain ){
8126      p->normalMode = p->mode;
8127      p->mode = MODE_Explain;
8128      p->autoExplain = 0;
8129    }else if( val==0 ){
8130      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8131      p->autoExplain = 0;
8132    }else if( val==99 ){
8133      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8134      p->autoExplain = 1;
8135    }
8136  }else
8137
8138#ifndef SQLITE_OMIT_VIRTUALTABLE
8139  if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
8140    if( p->bSafeMode ){
8141      raw_printf(stderr,
8142        "Cannot run experimental commands such as \"%s\" in safe mode\n",
8143        azArg[0]);
8144      rc = 1;
8145    }else{
8146      open_db(p, 0);
8147      expertDotCommand(p, azArg, nArg);
8148    }
8149  }else
8150#endif
8151
8152  if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
8153    static const struct {
8154       const char *zCtrlName;   /* Name of a test-control option */
8155       int ctrlCode;            /* Integer code for that option */
8156       const char *zUsage;      /* Usage notes */
8157    } aCtrl[] = {
8158      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8159      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8160      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8161      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8162      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8163   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8164      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8165      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8166      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8167      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8168   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8169    };
8170    int filectrl = -1;
8171    int iCtrl = -1;
8172    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8173    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8174    int n2, i;
8175    const char *zCmd = 0;
8176    const char *zSchema = 0;
8177
8178    open_db(p, 0);
8179    zCmd = nArg>=2 ? azArg[1] : "help";
8180
8181    if( zCmd[0]=='-'
8182     && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
8183     && nArg>=4
8184    ){
8185      zSchema = azArg[2];
8186      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8187      nArg -= 2;
8188      zCmd = azArg[1];
8189    }
8190
8191    /* The argument can optionally begin with "-" or "--" */
8192    if( zCmd[0]=='-' && zCmd[1] ){
8193      zCmd++;
8194      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8195    }
8196
8197    /* --help lists all file-controls */
8198    if( cli_strcmp(zCmd,"help")==0 ){
8199      utf8_printf(p->out, "Available file-controls:\n");
8200      for(i=0; i<ArraySize(aCtrl); i++){
8201        utf8_printf(p->out, "  .filectrl %s %s\n",
8202                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8203      }
8204      rc = 1;
8205      goto meta_command_exit;
8206    }
8207
8208    /* convert filectrl text option to value. allow any unique prefix
8209    ** of the option name, or a numerical value. */
8210    n2 = strlen30(zCmd);
8211    for(i=0; i<ArraySize(aCtrl); i++){
8212      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8213        if( filectrl<0 ){
8214          filectrl = aCtrl[i].ctrlCode;
8215          iCtrl = i;
8216        }else{
8217          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8218                              "Use \".filectrl --help\" for help\n", zCmd);
8219          rc = 1;
8220          goto meta_command_exit;
8221        }
8222      }
8223    }
8224    if( filectrl<0 ){
8225      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8226                         "Use \".filectrl --help\" for help\n", zCmd);
8227    }else{
8228      switch(filectrl){
8229        case SQLITE_FCNTL_SIZE_LIMIT: {
8230          if( nArg!=2 && nArg!=3 ) break;
8231          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8232          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8233          isOk = 1;
8234          break;
8235        }
8236        case SQLITE_FCNTL_LOCK_TIMEOUT:
8237        case SQLITE_FCNTL_CHUNK_SIZE: {
8238          int x;
8239          if( nArg!=3 ) break;
8240          x = (int)integerValue(azArg[2]);
8241          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8242          isOk = 2;
8243          break;
8244        }
8245        case SQLITE_FCNTL_PERSIST_WAL:
8246        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8247          int x;
8248          if( nArg!=2 && nArg!=3 ) break;
8249          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8250          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8251          iRes = x;
8252          isOk = 1;
8253          break;
8254        }
8255        case SQLITE_FCNTL_DATA_VERSION:
8256        case SQLITE_FCNTL_HAS_MOVED: {
8257          int x;
8258          if( nArg!=2 ) break;
8259          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8260          iRes = x;
8261          isOk = 1;
8262          break;
8263        }
8264        case SQLITE_FCNTL_TEMPFILENAME: {
8265          char *z = 0;
8266          if( nArg!=2 ) break;
8267          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8268          if( z ){
8269            utf8_printf(p->out, "%s\n", z);
8270            sqlite3_free(z);
8271          }
8272          isOk = 2;
8273          break;
8274        }
8275        case SQLITE_FCNTL_RESERVE_BYTES: {
8276          int x;
8277          if( nArg>=3 ){
8278            x = atoi(azArg[2]);
8279            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8280          }
8281          x = -1;
8282          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8283          utf8_printf(p->out,"%d\n", x);
8284          isOk = 2;
8285          break;
8286        }
8287      }
8288    }
8289    if( isOk==0 && iCtrl>=0 ){
8290      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8291      rc = 1;
8292    }else if( isOk==1 ){
8293      char zBuf[100];
8294      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8295      raw_printf(p->out, "%s\n", zBuf);
8296    }
8297  }else
8298
8299  if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
8300    ShellState data;
8301    int doStats = 0;
8302    memcpy(&data, p, sizeof(data));
8303    data.showHeader = 0;
8304    data.cMode = data.mode = MODE_Semi;
8305    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8306      data.cMode = data.mode = MODE_Pretty;
8307      nArg = 1;
8308    }
8309    if( nArg!=1 ){
8310      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8311      rc = 1;
8312      goto meta_command_exit;
8313    }
8314    open_db(p, 0);
8315    rc = sqlite3_exec(p->db,
8316       "SELECT sql FROM"
8317       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8318       "     FROM sqlite_schema UNION ALL"
8319       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8320       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8321       "ORDER BY x",
8322       callback, &data, 0
8323    );
8324    if( rc==SQLITE_OK ){
8325      sqlite3_stmt *pStmt;
8326      rc = sqlite3_prepare_v2(p->db,
8327               "SELECT rowid FROM sqlite_schema"
8328               " WHERE name GLOB 'sqlite_stat[134]'",
8329               -1, &pStmt, 0);
8330      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8331      sqlite3_finalize(pStmt);
8332    }
8333    if( doStats==0 ){
8334      raw_printf(p->out, "/* No STAT tables available */\n");
8335    }else{
8336      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8337      data.cMode = data.mode = MODE_Insert;
8338      data.zDestTable = "sqlite_stat1";
8339      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8340      data.zDestTable = "sqlite_stat4";
8341      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8342      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8343    }
8344  }else
8345
8346  if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
8347    if( nArg==2 ){
8348      p->showHeader = booleanValue(azArg[1]);
8349      p->shellFlgs |= SHFLG_HeaderSet;
8350    }else{
8351      raw_printf(stderr, "Usage: .headers on|off\n");
8352      rc = 1;
8353    }
8354  }else
8355
8356  if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
8357    if( nArg>=2 ){
8358      n = showHelp(p->out, azArg[1]);
8359      if( n==0 ){
8360        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8361      }
8362    }else{
8363      showHelp(p->out, 0);
8364    }
8365  }else
8366
8367#ifndef SQLITE_SHELL_FIDDLE
8368  if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
8369    char *zTable = 0;           /* Insert data into this table */
8370    char *zSchema = 0;          /* within this schema (may default to "main") */
8371    char *zFile = 0;            /* Name of file to extra content from */
8372    sqlite3_stmt *pStmt = NULL; /* A statement */
8373    int nCol;                   /* Number of columns in the table */
8374    int nByte;                  /* Number of bytes in an SQL string */
8375    int i, j;                   /* Loop counters */
8376    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8377    int nSep;                   /* Number of bytes in p->colSeparator[] */
8378    char *zSql;                 /* An SQL statement */
8379    char *zFullTabName;         /* Table name with schema if applicable */
8380    ImportCtx sCtx;             /* Reader context */
8381    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8382    int eVerbose = 0;           /* Larger for more console output */
8383    int nSkip = 0;              /* Initial lines to skip */
8384    int useOutputMode = 1;      /* Use output mode to determine separators */
8385    char *zCreate = 0;          /* CREATE TABLE statement text */
8386
8387    failIfSafeMode(p, "cannot run .import in safe mode");
8388    memset(&sCtx, 0, sizeof(sCtx));
8389    if( p->mode==MODE_Ascii ){
8390      xRead = ascii_read_one_field;
8391    }else{
8392      xRead = csv_read_one_field;
8393    }
8394    rc = 1;
8395    for(i=1; i<nArg; i++){
8396      char *z = azArg[i];
8397      if( z[0]=='-' && z[1]=='-' ) z++;
8398      if( z[0]!='-' ){
8399        if( zFile==0 ){
8400          zFile = z;
8401        }else if( zTable==0 ){
8402          zTable = z;
8403        }else{
8404          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8405          showHelp(p->out, "import");
8406          goto meta_command_exit;
8407        }
8408      }else if( cli_strcmp(z,"-v")==0 ){
8409        eVerbose++;
8410      }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
8411        zSchema = azArg[++i];
8412      }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
8413        nSkip = integerValue(azArg[++i]);
8414      }else if( cli_strcmp(z,"-ascii")==0 ){
8415        sCtx.cColSep = SEP_Unit[0];
8416        sCtx.cRowSep = SEP_Record[0];
8417        xRead = ascii_read_one_field;
8418        useOutputMode = 0;
8419      }else if( cli_strcmp(z,"-csv")==0 ){
8420        sCtx.cColSep = ',';
8421        sCtx.cRowSep = '\n';
8422        xRead = csv_read_one_field;
8423        useOutputMode = 0;
8424      }else{
8425        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8426        showHelp(p->out, "import");
8427        goto meta_command_exit;
8428      }
8429    }
8430    if( zTable==0 ){
8431      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8432                  zFile==0 ? "FILE" : "TABLE");
8433      showHelp(p->out, "import");
8434      goto meta_command_exit;
8435    }
8436    seenInterrupt = 0;
8437    open_db(p, 0);
8438    if( useOutputMode ){
8439      /* If neither the --csv or --ascii options are specified, then set
8440      ** the column and row separator characters from the output mode. */
8441      nSep = strlen30(p->colSeparator);
8442      if( nSep==0 ){
8443        raw_printf(stderr,
8444                   "Error: non-null column separator required for import\n");
8445        goto meta_command_exit;
8446      }
8447      if( nSep>1 ){
8448        raw_printf(stderr,
8449              "Error: multi-character column separators not allowed"
8450              " for import\n");
8451        goto meta_command_exit;
8452      }
8453      nSep = strlen30(p->rowSeparator);
8454      if( nSep==0 ){
8455        raw_printf(stderr,
8456            "Error: non-null row separator required for import\n");
8457        goto meta_command_exit;
8458      }
8459      if( nSep==2 && p->mode==MODE_Csv
8460       && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
8461      ){
8462        /* When importing CSV (only), if the row separator is set to the
8463        ** default output row separator, change it to the default input
8464        ** row separator.  This avoids having to maintain different input
8465        ** and output row separators. */
8466        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8467        nSep = strlen30(p->rowSeparator);
8468      }
8469      if( nSep>1 ){
8470        raw_printf(stderr, "Error: multi-character row separators not allowed"
8471                           " for import\n");
8472        goto meta_command_exit;
8473      }
8474      sCtx.cColSep = p->colSeparator[0];
8475      sCtx.cRowSep = p->rowSeparator[0];
8476    }
8477    sCtx.zFile = zFile;
8478    sCtx.nLine = 1;
8479    if( sCtx.zFile[0]=='|' ){
8480#ifdef SQLITE_OMIT_POPEN
8481      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8482      goto meta_command_exit;
8483#else
8484      sCtx.in = popen(sCtx.zFile+1, "r");
8485      sCtx.zFile = "<pipe>";
8486      sCtx.xCloser = pclose;
8487#endif
8488    }else{
8489      sCtx.in = fopen(sCtx.zFile, "rb");
8490      sCtx.xCloser = fclose;
8491    }
8492    if( sCtx.in==0 ){
8493      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8494      goto meta_command_exit;
8495    }
8496    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8497      char zSep[2];
8498      zSep[1] = 0;
8499      zSep[0] = sCtx.cColSep;
8500      utf8_printf(p->out, "Column separator ");
8501      output_c_string(p->out, zSep);
8502      utf8_printf(p->out, ", row separator ");
8503      zSep[0] = sCtx.cRowSep;
8504      output_c_string(p->out, zSep);
8505      utf8_printf(p->out, "\n");
8506    }
8507    sCtx.z = sqlite3_malloc64(120);
8508    if( sCtx.z==0 ){
8509      import_cleanup(&sCtx);
8510      shell_out_of_memory();
8511    }
8512    /* Below, resources must be freed before exit. */
8513    while( (nSkip--)>0 ){
8514      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8515    }
8516    if( zSchema!=0 ){
8517      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
8518    }else{
8519      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
8520    }
8521    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
8522    if( zSql==0 || zFullTabName==0 ){
8523      import_cleanup(&sCtx);
8524      shell_out_of_memory();
8525    }
8526    nByte = strlen30(zSql);
8527    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8528    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8529    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8530      sqlite3 *dbCols = 0;
8531      char *zRenames = 0;
8532      char *zColDefs;
8533      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
8534      while( xRead(&sCtx) ){
8535        zAutoColumn(sCtx.z, &dbCols, 0);
8536        if( sCtx.cTerm!=sCtx.cColSep ) break;
8537      }
8538      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
8539      if( zRenames!=0 ){
8540        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
8541                    "Columns renamed during .import %s due to duplicates:\n"
8542                    "%s\n", sCtx.zFile, zRenames);
8543        sqlite3_free(zRenames);
8544      }
8545      assert(dbCols==0);
8546      if( zColDefs==0 ){
8547        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8548      import_fail:
8549        sqlite3_free(zCreate);
8550        sqlite3_free(zSql);
8551        sqlite3_free(zFullTabName);
8552        import_cleanup(&sCtx);
8553        rc = 1;
8554        goto meta_command_exit;
8555      }
8556      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
8557      if( eVerbose>=1 ){
8558        utf8_printf(p->out, "%s\n", zCreate);
8559      }
8560      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8561      if( rc ){
8562        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
8563        goto import_fail;
8564      }
8565      sqlite3_free(zCreate);
8566      zCreate = 0;
8567      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8568    }
8569    if( rc ){
8570      if (pStmt) sqlite3_finalize(pStmt);
8571      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8572      goto import_fail;
8573    }
8574    sqlite3_free(zSql);
8575    nCol = sqlite3_column_count(pStmt);
8576    sqlite3_finalize(pStmt);
8577    pStmt = 0;
8578    if( nCol==0 ) return 0; /* no columns, no error */
8579    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8580    if( zSql==0 ){
8581      import_cleanup(&sCtx);
8582      shell_out_of_memory();
8583    }
8584    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
8585    j = strlen30(zSql);
8586    for(i=1; i<nCol; i++){
8587      zSql[j++] = ',';
8588      zSql[j++] = '?';
8589    }
8590    zSql[j++] = ')';
8591    zSql[j] = 0;
8592    if( eVerbose>=2 ){
8593      utf8_printf(p->out, "Insert using: %s\n", zSql);
8594    }
8595    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8596    if( rc ){
8597      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8598      if (pStmt) sqlite3_finalize(pStmt);
8599      goto import_fail;
8600    }
8601    sqlite3_free(zSql);
8602    sqlite3_free(zFullTabName);
8603    needCommit = sqlite3_get_autocommit(p->db);
8604    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8605    do{
8606      int startLine = sCtx.nLine;
8607      for(i=0; i<nCol; i++){
8608        char *z = xRead(&sCtx);
8609        /*
8610        ** Did we reach end-of-file before finding any columns?
8611        ** If so, stop instead of NULL filling the remaining columns.
8612        */
8613        if( z==0 && i==0 ) break;
8614        /*
8615        ** Did we reach end-of-file OR end-of-line before finding any
8616        ** columns in ASCII mode?  If so, stop instead of NULL filling
8617        ** the remaining columns.
8618        */
8619        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8620        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8621        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8622          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8623                          "filling the rest with NULL\n",
8624                          sCtx.zFile, startLine, nCol, i+1);
8625          i += 2;
8626          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8627        }
8628      }
8629      if( sCtx.cTerm==sCtx.cColSep ){
8630        do{
8631          xRead(&sCtx);
8632          i++;
8633        }while( sCtx.cTerm==sCtx.cColSep );
8634        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8635                        "extras ignored\n",
8636                        sCtx.zFile, startLine, nCol, i);
8637      }
8638      if( i>=nCol ){
8639        sqlite3_step(pStmt);
8640        rc = sqlite3_reset(pStmt);
8641        if( rc!=SQLITE_OK ){
8642          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8643                      startLine, sqlite3_errmsg(p->db));
8644          sCtx.nErr++;
8645        }else{
8646          sCtx.nRow++;
8647        }
8648      }
8649    }while( sCtx.cTerm!=EOF );
8650
8651    import_cleanup(&sCtx);
8652    sqlite3_finalize(pStmt);
8653    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8654    if( eVerbose>0 ){
8655      utf8_printf(p->out,
8656          "Added %d rows with %d errors using %d lines of input\n",
8657          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8658    }
8659  }else
8660#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8661
8662#ifndef SQLITE_UNTESTABLE
8663  if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
8664    char *zSql;
8665    char *zCollist = 0;
8666    sqlite3_stmt *pStmt;
8667    int tnum = 0;
8668    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8669    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8670    int i;
8671    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8672      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8673                          "       .imposter off\n");
8674      /* Also allowed, but not documented:
8675      **
8676      **    .imposter TABLE IMPOSTER
8677      **
8678      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8679      ** imposter is another WITHOUT ROWID table with the columns in
8680      ** storage order. */
8681      rc = 1;
8682      goto meta_command_exit;
8683    }
8684    open_db(p, 0);
8685    if( nArg==2 ){
8686      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8687      goto meta_command_exit;
8688    }
8689    zSql = sqlite3_mprintf(
8690      "SELECT rootpage, 0 FROM sqlite_schema"
8691      " WHERE name='%q' AND type='index'"
8692      "UNION ALL "
8693      "SELECT rootpage, 1 FROM sqlite_schema"
8694      " WHERE name='%q' AND type='table'"
8695      "   AND sql LIKE '%%without%%rowid%%'",
8696      azArg[1], azArg[1]
8697    );
8698    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8699    sqlite3_free(zSql);
8700    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8701      tnum = sqlite3_column_int(pStmt, 0);
8702      isWO = sqlite3_column_int(pStmt, 1);
8703    }
8704    sqlite3_finalize(pStmt);
8705    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8706    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8707    sqlite3_free(zSql);
8708    i = 0;
8709    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8710      char zLabel[20];
8711      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8712      i++;
8713      if( zCol==0 ){
8714        if( sqlite3_column_int(pStmt,1)==-1 ){
8715          zCol = "_ROWID_";
8716        }else{
8717          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8718          zCol = zLabel;
8719        }
8720      }
8721      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8722        lenPK = (int)strlen(zCollist);
8723      }
8724      if( zCollist==0 ){
8725        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8726      }else{
8727        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8728      }
8729    }
8730    sqlite3_finalize(pStmt);
8731    if( i==0 || tnum==0 ){
8732      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8733      rc = 1;
8734      sqlite3_free(zCollist);
8735      goto meta_command_exit;
8736    }
8737    if( lenPK==0 ) lenPK = 100000;
8738    zSql = sqlite3_mprintf(
8739          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8740          azArg[2], zCollist, lenPK, zCollist);
8741    sqlite3_free(zCollist);
8742    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8743    if( rc==SQLITE_OK ){
8744      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8745      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8746      if( rc ){
8747        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8748      }else{
8749        utf8_printf(stdout, "%s;\n", zSql);
8750        raw_printf(stdout,
8751          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8752          azArg[1], isWO ? "table" : "index"
8753        );
8754      }
8755    }else{
8756      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8757      rc = 1;
8758    }
8759    sqlite3_free(zSql);
8760  }else
8761#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8762
8763#ifdef SQLITE_ENABLE_IOTRACE
8764  if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
8765    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8766    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8767    iotrace = 0;
8768    if( nArg<2 ){
8769      sqlite3IoTrace = 0;
8770    }else if( cli_strcmp(azArg[1], "-")==0 ){
8771      sqlite3IoTrace = iotracePrintf;
8772      iotrace = stdout;
8773    }else{
8774      iotrace = fopen(azArg[1], "w");
8775      if( iotrace==0 ){
8776        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8777        sqlite3IoTrace = 0;
8778        rc = 1;
8779      }else{
8780        sqlite3IoTrace = iotracePrintf;
8781      }
8782    }
8783  }else
8784#endif
8785
8786  if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
8787    static const struct {
8788       const char *zLimitName;   /* Name of a limit */
8789       int limitCode;            /* Integer code for that limit */
8790    } aLimit[] = {
8791      { "length",                SQLITE_LIMIT_LENGTH                    },
8792      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8793      { "column",                SQLITE_LIMIT_COLUMN                    },
8794      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8795      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8796      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8797      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8798      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8799      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8800      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8801      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8802      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8803    };
8804    int i, n2;
8805    open_db(p, 0);
8806    if( nArg==1 ){
8807      for(i=0; i<ArraySize(aLimit); i++){
8808        printf("%20s %d\n", aLimit[i].zLimitName,
8809               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8810      }
8811    }else if( nArg>3 ){
8812      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8813      rc = 1;
8814      goto meta_command_exit;
8815    }else{
8816      int iLimit = -1;
8817      n2 = strlen30(azArg[1]);
8818      for(i=0; i<ArraySize(aLimit); i++){
8819        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8820          if( iLimit<0 ){
8821            iLimit = i;
8822          }else{
8823            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8824            rc = 1;
8825            goto meta_command_exit;
8826          }
8827        }
8828      }
8829      if( iLimit<0 ){
8830        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8831                        "enter \".limits\" with no arguments for a list.\n",
8832                         azArg[1]);
8833        rc = 1;
8834        goto meta_command_exit;
8835      }
8836      if( nArg==3 ){
8837        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8838                      (int)integerValue(azArg[2]));
8839      }
8840      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8841             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8842    }
8843  }else
8844
8845  if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
8846    open_db(p, 0);
8847    lintDotCommand(p, azArg, nArg);
8848  }else
8849
8850#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
8851  if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
8852    const char *zFile, *zProc;
8853    char *zErrMsg = 0;
8854    failIfSafeMode(p, "cannot run .load in safe mode");
8855    if( nArg<2 ){
8856      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8857      rc = 1;
8858      goto meta_command_exit;
8859    }
8860    zFile = azArg[1];
8861    zProc = nArg>=3 ? azArg[2] : 0;
8862    open_db(p, 0);
8863    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8864    if( rc!=SQLITE_OK ){
8865      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8866      sqlite3_free(zErrMsg);
8867      rc = 1;
8868    }
8869  }else
8870#endif
8871
8872#ifndef SQLITE_SHELL_FIDDLE
8873  if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
8874    failIfSafeMode(p, "cannot run .log in safe mode");
8875    if( nArg!=2 ){
8876      raw_printf(stderr, "Usage: .log FILENAME\n");
8877      rc = 1;
8878    }else{
8879      const char *zFile = azArg[1];
8880      output_file_close(p->pLog);
8881      p->pLog = output_file_open(zFile, 0);
8882    }
8883  }else
8884#endif
8885
8886  if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
8887    const char *zMode = 0;
8888    const char *zTabname = 0;
8889    int i, n2;
8890    ColModeOpts cmOpts = ColModeOpts_default;
8891    for(i=1; i<nArg; i++){
8892      const char *z = azArg[i];
8893      if( optionMatch(z,"wrap") && i+1<nArg ){
8894        cmOpts.iWrap = integerValue(azArg[++i]);
8895      }else if( optionMatch(z,"ww") ){
8896        cmOpts.bWordWrap = 1;
8897      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
8898        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
8899      }else if( optionMatch(z,"quote") ){
8900        cmOpts.bQuote = 1;
8901      }else if( optionMatch(z,"noquote") ){
8902        cmOpts.bQuote = 0;
8903      }else if( zMode==0 ){
8904        zMode = z;
8905        /* Apply defaults for qbox pseudo-mode.  If that
8906         * overwrites already-set values, user was informed of this.
8907         */
8908        if( cli_strcmp(z, "qbox")==0 ){
8909          ColModeOpts cmo = ColModeOpts_default_qbox;
8910          zMode = "box";
8911          cmOpts = cmo;
8912        }
8913      }else if( zTabname==0 ){
8914        zTabname = z;
8915      }else if( z[0]=='-' ){
8916        utf8_printf(stderr, "unknown option: %s\n", z);
8917        utf8_printf(stderr, "options:\n"
8918                            "  --noquote\n"
8919                            "  --quote\n"
8920                            "  --wordwrap on/off\n"
8921                            "  --wrap N\n"
8922                            "  --ww\n");
8923        rc = 1;
8924        goto meta_command_exit;
8925      }else{
8926        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8927        rc = 1;
8928        goto meta_command_exit;
8929      }
8930    }
8931    if( zMode==0 ){
8932      if( p->mode==MODE_Column
8933       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
8934      ){
8935        raw_printf
8936          (p->out,
8937           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
8938           modeDescr[p->mode], p->cmOpts.iWrap,
8939           p->cmOpts.bWordWrap ? "on" : "off",
8940           p->cmOpts.bQuote ? "" : "no");
8941      }else{
8942        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8943      }
8944      zMode = modeDescr[p->mode];
8945    }
8946    n2 = strlen30(zMode);
8947    if( cli_strncmp(zMode,"lines",n2)==0 ){
8948      p->mode = MODE_Line;
8949      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8950    }else if( cli_strncmp(zMode,"columns",n2)==0 ){
8951      p->mode = MODE_Column;
8952      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8953        p->showHeader = 1;
8954      }
8955      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8956      p->cmOpts = cmOpts;
8957    }else if( cli_strncmp(zMode,"list",n2)==0 ){
8958      p->mode = MODE_List;
8959      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8960      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8961    }else if( cli_strncmp(zMode,"html",n2)==0 ){
8962      p->mode = MODE_Html;
8963    }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
8964      p->mode = MODE_Tcl;
8965      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8966      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8967    }else if( cli_strncmp(zMode,"csv",n2)==0 ){
8968      p->mode = MODE_Csv;
8969      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8970      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8971    }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
8972      p->mode = MODE_List;
8973      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8974    }else if( cli_strncmp(zMode,"insert",n2)==0 ){
8975      p->mode = MODE_Insert;
8976      set_table_name(p, zTabname ? zTabname : "table");
8977    }else if( cli_strncmp(zMode,"quote",n2)==0 ){
8978      p->mode = MODE_Quote;
8979      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8980      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8981    }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
8982      p->mode = MODE_Ascii;
8983      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8984      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8985    }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
8986      p->mode = MODE_Markdown;
8987      p->cmOpts = cmOpts;
8988    }else if( cli_strncmp(zMode,"table",n2)==0 ){
8989      p->mode = MODE_Table;
8990      p->cmOpts = cmOpts;
8991    }else if( cli_strncmp(zMode,"box",n2)==0 ){
8992      p->mode = MODE_Box;
8993      p->cmOpts = cmOpts;
8994    }else if( cli_strncmp(zMode,"count",n2)==0 ){
8995      p->mode = MODE_Count;
8996    }else if( cli_strncmp(zMode,"off",n2)==0 ){
8997      p->mode = MODE_Off;
8998    }else if( cli_strncmp(zMode,"json",n2)==0 ){
8999      p->mode = MODE_Json;
9000    }else{
9001      raw_printf(stderr, "Error: mode should be one of: "
9002         "ascii box column csv html insert json line list markdown "
9003         "qbox quote table tabs tcl\n");
9004      rc = 1;
9005    }
9006    p->cMode = p->mode;
9007  }else
9008
9009#ifndef SQLITE_SHELL_FIDDLE
9010  if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
9011    if( nArg!=2 ){
9012      raw_printf(stderr, "Usage: .nonce NONCE\n");
9013      rc = 1;
9014    }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
9015      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9016                 p->lineno, azArg[1]);
9017      exit(1);
9018    }else{
9019      p->bSafeMode = 0;
9020      return 0;  /* Return immediately to bypass the safe mode reset
9021                 ** at the end of this procedure */
9022    }
9023  }else
9024#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9025
9026  if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
9027    if( nArg==2 ){
9028      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9029                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9030    }else{
9031      raw_printf(stderr, "Usage: .nullvalue STRING\n");
9032      rc = 1;
9033    }
9034  }else
9035
9036  if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
9037    const char *zFN = 0;     /* Pointer to constant filename */
9038    char *zNewFilename = 0;  /* Name of the database file to open */
9039    int iName = 1;           /* Index in azArg[] of the filename */
9040    int newFlag = 0;         /* True to delete file before opening */
9041    int openMode = SHELL_OPEN_UNSPEC;
9042
9043    /* Check for command-line arguments */
9044    for(iName=1; iName<nArg; iName++){
9045      const char *z = azArg[iName];
9046#ifndef SQLITE_SHELL_FIDDLE
9047      if( optionMatch(z,"new") ){
9048        newFlag = 1;
9049#ifdef SQLITE_HAVE_ZLIB
9050      }else if( optionMatch(z, "zip") ){
9051        openMode = SHELL_OPEN_ZIPFILE;
9052#endif
9053      }else if( optionMatch(z, "append") ){
9054        openMode = SHELL_OPEN_APPENDVFS;
9055      }else if( optionMatch(z, "readonly") ){
9056        openMode = SHELL_OPEN_READONLY;
9057      }else if( optionMatch(z, "nofollow") ){
9058        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9059#ifndef SQLITE_OMIT_DESERIALIZE
9060      }else if( optionMatch(z, "deserialize") ){
9061        openMode = SHELL_OPEN_DESERIALIZE;
9062      }else if( optionMatch(z, "hexdb") ){
9063        openMode = SHELL_OPEN_HEXDB;
9064      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9065        p->szMax = integerValue(azArg[++iName]);
9066#endif /* SQLITE_OMIT_DESERIALIZE */
9067      }else
9068#endif /* !SQLITE_SHELL_FIDDLE */
9069      if( z[0]=='-' ){
9070        utf8_printf(stderr, "unknown option: %s\n", z);
9071        rc = 1;
9072        goto meta_command_exit;
9073      }else if( zFN ){
9074        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9075        rc = 1;
9076        goto meta_command_exit;
9077      }else{
9078        zFN = z;
9079      }
9080    }
9081
9082    /* Close the existing database */
9083    session_close_all(p, -1);
9084    close_db(p->db);
9085    p->db = 0;
9086    p->pAuxDb->zDbFilename = 0;
9087    sqlite3_free(p->pAuxDb->zFreeOnClose);
9088    p->pAuxDb->zFreeOnClose = 0;
9089    p->openMode = openMode;
9090    p->openFlags = 0;
9091    p->szMax = 0;
9092
9093    /* If a filename is specified, try to open it first */
9094    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9095      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9096#ifndef SQLITE_SHELL_FIDDLE
9097      if( p->bSafeMode
9098       && p->openMode!=SHELL_OPEN_HEXDB
9099       && zFN
9100       && cli_strcmp(zFN,":memory:")!=0
9101      ){
9102        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9103      }
9104#else
9105      /* WASM mode has its own sandboxed pseudo-filesystem. */
9106#endif
9107      if( zFN ){
9108        zNewFilename = sqlite3_mprintf("%s", zFN);
9109        shell_check_oom(zNewFilename);
9110      }else{
9111        zNewFilename = 0;
9112      }
9113      p->pAuxDb->zDbFilename = zNewFilename;
9114      open_db(p, OPEN_DB_KEEPALIVE);
9115      if( p->db==0 ){
9116        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9117        sqlite3_free(zNewFilename);
9118      }else{
9119        p->pAuxDb->zFreeOnClose = zNewFilename;
9120      }
9121    }
9122    if( p->db==0 ){
9123      /* As a fall-back open a TEMP database */
9124      p->pAuxDb->zDbFilename = 0;
9125      open_db(p, 0);
9126    }
9127  }else
9128
9129#ifndef SQLITE_SHELL_FIDDLE
9130  if( (c=='o'
9131        && (cli_strncmp(azArg[0], "output", n)==0
9132            || cli_strncmp(azArg[0], "once", n)==0))
9133   || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
9134  ){
9135    char *zFile = 0;
9136    int bTxtMode = 0;
9137    int i;
9138    int eMode = 0;
9139    int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
9140    unsigned char zBOM[4];    /* Byte-order mark to using if --bom is present */
9141
9142    zBOM[0] = 0;
9143    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9144    if( c=='e' ){
9145      eMode = 'x';
9146      bOnce = 2;
9147    }else if( cli_strncmp(azArg[0],"once",n)==0 ){
9148      bOnce = 1;
9149    }
9150    for(i=1; i<nArg; i++){
9151      char *z = azArg[i];
9152      if( z[0]=='-' ){
9153        if( z[1]=='-' ) z++;
9154        if( cli_strcmp(z,"-bom")==0 ){
9155          zBOM[0] = 0xef;
9156          zBOM[1] = 0xbb;
9157          zBOM[2] = 0xbf;
9158          zBOM[3] = 0;
9159        }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
9160          eMode = 'x';  /* spreadsheet */
9161        }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
9162          eMode = 'e';  /* text editor */
9163        }else{
9164          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9165                      azArg[i]);
9166          showHelp(p->out, azArg[0]);
9167          rc = 1;
9168          goto meta_command_exit;
9169        }
9170      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9171        zFile = sqlite3_mprintf("%s", z);
9172        if( zFile && zFile[0]=='|' ){
9173          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9174          break;
9175        }
9176      }else{
9177        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9178                    azArg[i]);
9179        showHelp(p->out, azArg[0]);
9180        rc = 1;
9181        sqlite3_free(zFile);
9182        goto meta_command_exit;
9183      }
9184    }
9185    if( zFile==0 ){
9186      zFile = sqlite3_mprintf("stdout");
9187    }
9188    if( bOnce ){
9189      p->outCount = 2;
9190    }else{
9191      p->outCount = 0;
9192    }
9193    output_reset(p);
9194#ifndef SQLITE_NOHAVE_SYSTEM
9195    if( eMode=='e' || eMode=='x' ){
9196      p->doXdgOpen = 1;
9197      outputModePush(p);
9198      if( eMode=='x' ){
9199        /* spreadsheet mode.  Output as CSV. */
9200        newTempFile(p, "csv");
9201        ShellClearFlag(p, SHFLG_Echo);
9202        p->mode = MODE_Csv;
9203        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9204        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9205      }else{
9206        /* text editor mode */
9207        newTempFile(p, "txt");
9208        bTxtMode = 1;
9209      }
9210      sqlite3_free(zFile);
9211      zFile = sqlite3_mprintf("%s", p->zTempFile);
9212    }
9213#endif /* SQLITE_NOHAVE_SYSTEM */
9214    shell_check_oom(zFile);
9215    if( zFile[0]=='|' ){
9216#ifdef SQLITE_OMIT_POPEN
9217      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9218      rc = 1;
9219      p->out = stdout;
9220#else
9221      p->out = popen(zFile + 1, "w");
9222      if( p->out==0 ){
9223        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9224        p->out = stdout;
9225        rc = 1;
9226      }else{
9227        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9228        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9229      }
9230#endif
9231    }else{
9232      p->out = output_file_open(zFile, bTxtMode);
9233      if( p->out==0 ){
9234        if( cli_strcmp(zFile,"off")!=0 ){
9235          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9236        }
9237        p->out = stdout;
9238        rc = 1;
9239      } else {
9240        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9241        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9242      }
9243    }
9244    sqlite3_free(zFile);
9245  }else
9246#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9247
9248  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
9249    open_db(p,0);
9250    if( nArg<=1 ) goto parameter_syntax_error;
9251
9252    /* .parameter clear
9253    ** Clear all bind parameters by dropping the TEMP table that holds them.
9254    */
9255    if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
9256      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9257                   0, 0, 0);
9258    }else
9259
9260    /* .parameter list
9261    ** List all bind parameters.
9262    */
9263    if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
9264      sqlite3_stmt *pStmt = 0;
9265      int rx;
9266      int len = 0;
9267      rx = sqlite3_prepare_v2(p->db,
9268             "SELECT max(length(key)) "
9269             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9270      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9271        len = sqlite3_column_int(pStmt, 0);
9272        if( len>40 ) len = 40;
9273      }
9274      sqlite3_finalize(pStmt);
9275      pStmt = 0;
9276      if( len ){
9277        rx = sqlite3_prepare_v2(p->db,
9278             "SELECT key, quote(value) "
9279             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9280        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9281          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9282                      sqlite3_column_text(pStmt,1));
9283        }
9284        sqlite3_finalize(pStmt);
9285      }
9286    }else
9287
9288    /* .parameter init
9289    ** Make sure the TEMP table used to hold bind parameters exists.
9290    ** Create it if necessary.
9291    */
9292    if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
9293      bind_table_init(p);
9294    }else
9295
9296    /* .parameter set NAME VALUE
9297    ** Set or reset a bind parameter.  NAME should be the full parameter
9298    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9299    ** VALUE can be in either SQL literal notation, or if not it will be
9300    ** understood to be a text string.
9301    */
9302    if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
9303      int rx;
9304      char *zSql;
9305      sqlite3_stmt *pStmt;
9306      const char *zKey = azArg[2];
9307      const char *zValue = azArg[3];
9308      bind_table_init(p);
9309      zSql = sqlite3_mprintf(
9310                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9311                  "VALUES(%Q,%s);", zKey, zValue);
9312      shell_check_oom(zSql);
9313      pStmt = 0;
9314      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9315      sqlite3_free(zSql);
9316      if( rx!=SQLITE_OK ){
9317        sqlite3_finalize(pStmt);
9318        pStmt = 0;
9319        zSql = sqlite3_mprintf(
9320                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9321                   "VALUES(%Q,%Q);", zKey, zValue);
9322        shell_check_oom(zSql);
9323        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9324        sqlite3_free(zSql);
9325        if( rx!=SQLITE_OK ){
9326          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9327          sqlite3_finalize(pStmt);
9328          pStmt = 0;
9329          rc = 1;
9330        }
9331      }
9332      sqlite3_step(pStmt);
9333      sqlite3_finalize(pStmt);
9334    }else
9335
9336    /* .parameter unset NAME
9337    ** Remove the NAME binding from the parameter binding table, if it
9338    ** exists.
9339    */
9340    if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
9341      char *zSql = sqlite3_mprintf(
9342          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9343      shell_check_oom(zSql);
9344      sqlite3_exec(p->db, zSql, 0, 0, 0);
9345      sqlite3_free(zSql);
9346    }else
9347    /* If no command name matches, show a syntax error */
9348    parameter_syntax_error:
9349    showHelp(p->out, "parameter");
9350  }else
9351
9352  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
9353    int i;
9354    for(i=1; i<nArg; i++){
9355      if( i>1 ) raw_printf(p->out, " ");
9356      utf8_printf(p->out, "%s", azArg[i]);
9357    }
9358    raw_printf(p->out, "\n");
9359  }else
9360
9361#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9362  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
9363    int i;
9364    int nn = 0;
9365    p->flgProgress = 0;
9366    p->mxProgress = 0;
9367    p->nProgress = 0;
9368    for(i=1; i<nArg; i++){
9369      const char *z = azArg[i];
9370      if( z[0]=='-' ){
9371        z++;
9372        if( z[0]=='-' ) z++;
9373        if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
9374          p->flgProgress |= SHELL_PROGRESS_QUIET;
9375          continue;
9376        }
9377        if( cli_strcmp(z,"reset")==0 ){
9378          p->flgProgress |= SHELL_PROGRESS_RESET;
9379          continue;
9380        }
9381        if( cli_strcmp(z,"once")==0 ){
9382          p->flgProgress |= SHELL_PROGRESS_ONCE;
9383          continue;
9384        }
9385        if( cli_strcmp(z,"limit")==0 ){
9386          if( i+1>=nArg ){
9387            utf8_printf(stderr, "Error: missing argument on --limit\n");
9388            rc = 1;
9389            goto meta_command_exit;
9390          }else{
9391            p->mxProgress = (int)integerValue(azArg[++i]);
9392          }
9393          continue;
9394        }
9395        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9396        rc = 1;
9397        goto meta_command_exit;
9398      }else{
9399        nn = (int)integerValue(z);
9400      }
9401    }
9402    open_db(p, 0);
9403    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9404  }else
9405#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9406
9407  if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
9408    if( nArg >= 2) {
9409      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9410    }
9411    if( nArg >= 3) {
9412      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9413    }
9414  }else
9415
9416#ifndef SQLITE_SHELL_FIDDLE
9417  if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
9418    rc = 2;
9419  }else
9420#endif
9421
9422#ifndef SQLITE_SHELL_FIDDLE
9423  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
9424    FILE *inSaved = p->in;
9425    int savedLineno = p->lineno;
9426    failIfSafeMode(p, "cannot run .read in safe mode");
9427    if( nArg!=2 ){
9428      raw_printf(stderr, "Usage: .read FILE\n");
9429      rc = 1;
9430      goto meta_command_exit;
9431    }
9432    if( azArg[1][0]=='|' ){
9433#ifdef SQLITE_OMIT_POPEN
9434      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9435      rc = 1;
9436      p->out = stdout;
9437#else
9438      p->in = popen(azArg[1]+1, "r");
9439      if( p->in==0 ){
9440        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9441        rc = 1;
9442      }else{
9443        rc = process_input(p);
9444        pclose(p->in);
9445      }
9446#endif
9447    }else if( (p->in = openChrSource(azArg[1]))==0 ){
9448      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9449      rc = 1;
9450    }else{
9451      rc = process_input(p);
9452      fclose(p->in);
9453    }
9454    p->in = inSaved;
9455    p->lineno = savedLineno;
9456  }else
9457#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9458
9459#ifndef SQLITE_SHELL_FIDDLE
9460  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
9461    const char *zSrcFile;
9462    const char *zDb;
9463    sqlite3 *pSrc;
9464    sqlite3_backup *pBackup;
9465    int nTimeout = 0;
9466
9467    failIfSafeMode(p, "cannot run .restore in safe mode");
9468    if( nArg==2 ){
9469      zSrcFile = azArg[1];
9470      zDb = "main";
9471    }else if( nArg==3 ){
9472      zSrcFile = azArg[2];
9473      zDb = azArg[1];
9474    }else{
9475      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9476      rc = 1;
9477      goto meta_command_exit;
9478    }
9479    rc = sqlite3_open(zSrcFile, &pSrc);
9480    if( rc!=SQLITE_OK ){
9481      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9482      close_db(pSrc);
9483      return 1;
9484    }
9485    open_db(p, 0);
9486    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9487    if( pBackup==0 ){
9488      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9489      close_db(pSrc);
9490      return 1;
9491    }
9492    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9493          || rc==SQLITE_BUSY  ){
9494      if( rc==SQLITE_BUSY ){
9495        if( nTimeout++ >= 3 ) break;
9496        sqlite3_sleep(100);
9497      }
9498    }
9499    sqlite3_backup_finish(pBackup);
9500    if( rc==SQLITE_DONE ){
9501      rc = 0;
9502    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9503      raw_printf(stderr, "Error: source database is busy\n");
9504      rc = 1;
9505    }else{
9506      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9507      rc = 1;
9508    }
9509    close_db(pSrc);
9510  }else
9511#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9512
9513  if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
9514    if( nArg==2 ){
9515      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9516#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9517      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9518#endif
9519    }else{
9520      raw_printf(stderr, "Usage: .scanstats on|off\n");
9521      rc = 1;
9522    }
9523  }else
9524
9525  if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
9526    ShellText sSelect;
9527    ShellState data;
9528    char *zErrMsg = 0;
9529    const char *zDiv = "(";
9530    const char *zName = 0;
9531    int iSchema = 0;
9532    int bDebug = 0;
9533    int bNoSystemTabs = 0;
9534    int ii;
9535
9536    open_db(p, 0);
9537    memcpy(&data, p, sizeof(data));
9538    data.showHeader = 0;
9539    data.cMode = data.mode = MODE_Semi;
9540    initText(&sSelect);
9541    for(ii=1; ii<nArg; ii++){
9542      if( optionMatch(azArg[ii],"indent") ){
9543        data.cMode = data.mode = MODE_Pretty;
9544      }else if( optionMatch(azArg[ii],"debug") ){
9545        bDebug = 1;
9546      }else if( optionMatch(azArg[ii],"nosys") ){
9547        bNoSystemTabs = 1;
9548      }else if( azArg[ii][0]=='-' ){
9549        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9550        rc = 1;
9551        goto meta_command_exit;
9552      }else if( zName==0 ){
9553        zName = azArg[ii];
9554      }else{
9555        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9556        rc = 1;
9557        goto meta_command_exit;
9558      }
9559    }
9560    if( zName!=0 ){
9561      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9562                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9563                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9564                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9565      if( isSchema ){
9566        char *new_argv[2], *new_colv[2];
9567        new_argv[0] = sqlite3_mprintf(
9568                      "CREATE TABLE %s (\n"
9569                      "  type text,\n"
9570                      "  name text,\n"
9571                      "  tbl_name text,\n"
9572                      "  rootpage integer,\n"
9573                      "  sql text\n"
9574                      ")", zName);
9575        shell_check_oom(new_argv[0]);
9576        new_argv[1] = 0;
9577        new_colv[0] = "sql";
9578        new_colv[1] = 0;
9579        callback(&data, 1, new_argv, new_colv);
9580        sqlite3_free(new_argv[0]);
9581      }
9582    }
9583    if( zDiv ){
9584      sqlite3_stmt *pStmt = 0;
9585      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9586                              -1, &pStmt, 0);
9587      if( rc ){
9588        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9589        sqlite3_finalize(pStmt);
9590        rc = 1;
9591        goto meta_command_exit;
9592      }
9593      appendText(&sSelect, "SELECT sql FROM", 0);
9594      iSchema = 0;
9595      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9596        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9597        char zScNum[30];
9598        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9599        appendText(&sSelect, zDiv, 0);
9600        zDiv = " UNION ALL ";
9601        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9602        if( sqlite3_stricmp(zDb, "main")!=0 ){
9603          appendText(&sSelect, zDb, '\'');
9604        }else{
9605          appendText(&sSelect, "NULL", 0);
9606        }
9607        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9608        appendText(&sSelect, zScNum, 0);
9609        appendText(&sSelect, " AS snum, ", 0);
9610        appendText(&sSelect, zDb, '\'');
9611        appendText(&sSelect, " AS sname FROM ", 0);
9612        appendText(&sSelect, zDb, quoteChar(zDb));
9613        appendText(&sSelect, ".sqlite_schema", 0);
9614      }
9615      sqlite3_finalize(pStmt);
9616#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9617      if( zName ){
9618        appendText(&sSelect,
9619           " UNION ALL SELECT shell_module_schema(name),"
9620           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9621        0);
9622      }
9623#endif
9624      appendText(&sSelect, ") WHERE ", 0);
9625      if( zName ){
9626        char *zQarg = sqlite3_mprintf("%Q", zName);
9627        int bGlob;
9628        shell_check_oom(zQarg);
9629        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9630                strchr(zName, '[') != 0;
9631        if( strchr(zName, '.') ){
9632          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9633        }else{
9634          appendText(&sSelect, "lower(tbl_name)", 0);
9635        }
9636        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9637        appendText(&sSelect, zQarg, 0);
9638        if( !bGlob ){
9639          appendText(&sSelect, " ESCAPE '\\' ", 0);
9640        }
9641        appendText(&sSelect, " AND ", 0);
9642        sqlite3_free(zQarg);
9643      }
9644      if( bNoSystemTabs ){
9645        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9646      }
9647      appendText(&sSelect, "sql IS NOT NULL"
9648                           " ORDER BY snum, rowid", 0);
9649      if( bDebug ){
9650        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9651      }else{
9652        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9653      }
9654      freeText(&sSelect);
9655    }
9656    if( zErrMsg ){
9657      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9658      sqlite3_free(zErrMsg);
9659      rc = 1;
9660    }else if( rc != SQLITE_OK ){
9661      raw_printf(stderr,"Error: querying schema information\n");
9662      rc = 1;
9663    }else{
9664      rc = 0;
9665    }
9666  }else
9667
9668  if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
9669   || (c=='t' && n==9  && cli_strncmp(azArg[0], "treetrace", n)==0)
9670  ){
9671    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9672    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9673  }else
9674
9675#if defined(SQLITE_ENABLE_SESSION)
9676  if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
9677    struct AuxDb *pAuxDb = p->pAuxDb;
9678    OpenSession *pSession = &pAuxDb->aSession[0];
9679    char **azCmd = &azArg[1];
9680    int iSes = 0;
9681    int nCmd = nArg - 1;
9682    int i;
9683    if( nArg<=1 ) goto session_syntax_error;
9684    open_db(p, 0);
9685    if( nArg>=3 ){
9686      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
9687        if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
9688      }
9689      if( iSes<pAuxDb->nSession ){
9690        pSession = &pAuxDb->aSession[iSes];
9691        azCmd++;
9692        nCmd--;
9693      }else{
9694        pSession = &pAuxDb->aSession[0];
9695        iSes = 0;
9696      }
9697    }
9698
9699    /* .session attach TABLE
9700    ** Invoke the sqlite3session_attach() interface to attach a particular
9701    ** table so that it is never filtered.
9702    */
9703    if( cli_strcmp(azCmd[0],"attach")==0 ){
9704      if( nCmd!=2 ) goto session_syntax_error;
9705      if( pSession->p==0 ){
9706        session_not_open:
9707        raw_printf(stderr, "ERROR: No sessions are open\n");
9708      }else{
9709        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9710        if( rc ){
9711          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9712          rc = 0;
9713        }
9714      }
9715    }else
9716
9717    /* .session changeset FILE
9718    ** .session patchset FILE
9719    ** Write a changeset or patchset into a file.  The file is overwritten.
9720    */
9721    if( cli_strcmp(azCmd[0],"changeset")==0
9722     || cli_strcmp(azCmd[0],"patchset")==0
9723    ){
9724      FILE *out = 0;
9725      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
9726      if( nCmd!=2 ) goto session_syntax_error;
9727      if( pSession->p==0 ) goto session_not_open;
9728      out = fopen(azCmd[1], "wb");
9729      if( out==0 ){
9730        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9731                    azCmd[1]);
9732      }else{
9733        int szChng;
9734        void *pChng;
9735        if( azCmd[0][0]=='c' ){
9736          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9737        }else{
9738          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9739        }
9740        if( rc ){
9741          printf("Error: error code %d\n", rc);
9742          rc = 0;
9743        }
9744        if( pChng
9745          && fwrite(pChng, szChng, 1, out)!=1 ){
9746          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9747                  szChng);
9748        }
9749        sqlite3_free(pChng);
9750        fclose(out);
9751      }
9752    }else
9753
9754    /* .session close
9755    ** Close the identified session
9756    */
9757    if( cli_strcmp(azCmd[0], "close")==0 ){
9758      if( nCmd!=1 ) goto session_syntax_error;
9759      if( pAuxDb->nSession ){
9760        session_close(pSession);
9761        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
9762      }
9763    }else
9764
9765    /* .session enable ?BOOLEAN?
9766    ** Query or set the enable flag
9767    */
9768    if( cli_strcmp(azCmd[0], "enable")==0 ){
9769      int ii;
9770      if( nCmd>2 ) goto session_syntax_error;
9771      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9772      if( pAuxDb->nSession ){
9773        ii = sqlite3session_enable(pSession->p, ii);
9774        utf8_printf(p->out, "session %s enable flag = %d\n",
9775                    pSession->zName, ii);
9776      }
9777    }else
9778
9779    /* .session filter GLOB ....
9780    ** Set a list of GLOB patterns of table names to be excluded.
9781    */
9782    if( cli_strcmp(azCmd[0], "filter")==0 ){
9783      int ii, nByte;
9784      if( nCmd<2 ) goto session_syntax_error;
9785      if( pAuxDb->nSession ){
9786        for(ii=0; ii<pSession->nFilter; ii++){
9787          sqlite3_free(pSession->azFilter[ii]);
9788        }
9789        sqlite3_free(pSession->azFilter);
9790        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9791        pSession->azFilter = sqlite3_malloc( nByte );
9792        if( pSession->azFilter==0 ){
9793          raw_printf(stderr, "Error: out or memory\n");
9794          exit(1);
9795        }
9796        for(ii=1; ii<nCmd; ii++){
9797          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9798          shell_check_oom(x);
9799        }
9800        pSession->nFilter = ii-1;
9801      }
9802    }else
9803
9804    /* .session indirect ?BOOLEAN?
9805    ** Query or set the indirect flag
9806    */
9807    if( cli_strcmp(azCmd[0], "indirect")==0 ){
9808      int ii;
9809      if( nCmd>2 ) goto session_syntax_error;
9810      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9811      if( pAuxDb->nSession ){
9812        ii = sqlite3session_indirect(pSession->p, ii);
9813        utf8_printf(p->out, "session %s indirect flag = %d\n",
9814                    pSession->zName, ii);
9815      }
9816    }else
9817
9818    /* .session isempty
9819    ** Determine if the session is empty
9820    */
9821    if( cli_strcmp(azCmd[0], "isempty")==0 ){
9822      int ii;
9823      if( nCmd!=1 ) goto session_syntax_error;
9824      if( pAuxDb->nSession ){
9825        ii = sqlite3session_isempty(pSession->p);
9826        utf8_printf(p->out, "session %s isempty flag = %d\n",
9827                    pSession->zName, ii);
9828      }
9829    }else
9830
9831    /* .session list
9832    ** List all currently open sessions
9833    */
9834    if( cli_strcmp(azCmd[0],"list")==0 ){
9835      for(i=0; i<pAuxDb->nSession; i++){
9836        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
9837      }
9838    }else
9839
9840    /* .session open DB NAME
9841    ** Open a new session called NAME on the attached database DB.
9842    ** DB is normally "main".
9843    */
9844    if( cli_strcmp(azCmd[0],"open")==0 ){
9845      char *zName;
9846      if( nCmd!=3 ) goto session_syntax_error;
9847      zName = azCmd[2];
9848      if( zName[0]==0 ) goto session_syntax_error;
9849      for(i=0; i<pAuxDb->nSession; i++){
9850        if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
9851          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9852          goto meta_command_exit;
9853        }
9854      }
9855      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
9856        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
9857        goto meta_command_exit;
9858      }
9859      pSession = &pAuxDb->aSession[pAuxDb->nSession];
9860      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9861      if( rc ){
9862        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9863        rc = 0;
9864        goto meta_command_exit;
9865      }
9866      pSession->nFilter = 0;
9867      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9868      pAuxDb->nSession++;
9869      pSession->zName = sqlite3_mprintf("%s", zName);
9870      shell_check_oom(pSession->zName);
9871    }else
9872    /* If no command name matches, show a syntax error */
9873    session_syntax_error:
9874    showHelp(p->out, "session");
9875  }else
9876#endif
9877
9878#ifdef SQLITE_DEBUG
9879  /* Undocumented commands for internal testing.  Subject to change
9880  ** without notice. */
9881  if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
9882    if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9883      int i, v;
9884      for(i=1; i<nArg; i++){
9885        v = booleanValue(azArg[i]);
9886        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9887      }
9888    }
9889    if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
9890      int i; sqlite3_int64 v;
9891      for(i=1; i<nArg; i++){
9892        char zBuf[200];
9893        v = integerValue(azArg[i]);
9894        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9895        utf8_printf(p->out, "%s", zBuf);
9896      }
9897    }
9898  }else
9899#endif
9900
9901  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
9902    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9903    int bVerbose = 0;        /* Verbose output */
9904    int bSelftestExists;     /* True if SELFTEST already exists */
9905    int i, k;                /* Loop counters */
9906    int nTest = 0;           /* Number of tests runs */
9907    int nErr = 0;            /* Number of errors seen */
9908    ShellText str;           /* Answer for a query */
9909    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9910
9911    open_db(p,0);
9912    for(i=1; i<nArg; i++){
9913      const char *z = azArg[i];
9914      if( z[0]=='-' && z[1]=='-' ) z++;
9915      if( cli_strcmp(z,"-init")==0 ){
9916        bIsInit = 1;
9917      }else
9918      if( cli_strcmp(z,"-v")==0 ){
9919        bVerbose++;
9920      }else
9921      {
9922        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9923                    azArg[i], azArg[0]);
9924        raw_printf(stderr, "Should be one of: --init -v\n");
9925        rc = 1;
9926        goto meta_command_exit;
9927      }
9928    }
9929    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9930           != SQLITE_OK ){
9931      bSelftestExists = 0;
9932    }else{
9933      bSelftestExists = 1;
9934    }
9935    if( bIsInit ){
9936      createSelftestTable(p);
9937      bSelftestExists = 1;
9938    }
9939    initText(&str);
9940    appendText(&str, "x", 0);
9941    for(k=bSelftestExists; k>=0; k--){
9942      if( k==1 ){
9943        rc = sqlite3_prepare_v2(p->db,
9944            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9945            -1, &pStmt, 0);
9946      }else{
9947        rc = sqlite3_prepare_v2(p->db,
9948          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9949          "      (1,'run','PRAGMA integrity_check','ok')",
9950          -1, &pStmt, 0);
9951      }
9952      if( rc ){
9953        raw_printf(stderr, "Error querying the selftest table\n");
9954        rc = 1;
9955        sqlite3_finalize(pStmt);
9956        goto meta_command_exit;
9957      }
9958      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9959        int tno = sqlite3_column_int(pStmt, 0);
9960        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9961        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9962        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9963
9964        if( zOp==0 ) continue;
9965        if( zSql==0 ) continue;
9966        if( zAns==0 ) continue;
9967        k = 0;
9968        if( bVerbose>0 ){
9969          printf("%d: %s %s\n", tno, zOp, zSql);
9970        }
9971        if( cli_strcmp(zOp,"memo")==0 ){
9972          utf8_printf(p->out, "%s\n", zSql);
9973        }else
9974        if( cli_strcmp(zOp,"run")==0 ){
9975          char *zErrMsg = 0;
9976          str.n = 0;
9977          str.z[0] = 0;
9978          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9979          nTest++;
9980          if( bVerbose ){
9981            utf8_printf(p->out, "Result: %s\n", str.z);
9982          }
9983          if( rc || zErrMsg ){
9984            nErr++;
9985            rc = 1;
9986            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9987            sqlite3_free(zErrMsg);
9988          }else if( cli_strcmp(zAns,str.z)!=0 ){
9989            nErr++;
9990            rc = 1;
9991            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9992            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9993          }
9994        }else
9995        {
9996          utf8_printf(stderr,
9997            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9998          rc = 1;
9999          break;
10000        }
10001      } /* End loop over rows of content from SELFTEST */
10002      sqlite3_finalize(pStmt);
10003    } /* End loop over k */
10004    freeText(&str);
10005    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10006  }else
10007
10008  if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
10009    if( nArg<2 || nArg>3 ){
10010      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10011      rc = 1;
10012    }
10013    if( nArg>=2 ){
10014      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10015                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10016    }
10017    if( nArg>=3 ){
10018      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10019                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10020    }
10021  }else
10022
10023  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
10024    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
10025    int i;                   /* Loop counter */
10026    int bSchema = 0;         /* Also hash the schema */
10027    int bSeparate = 0;       /* Hash each table separately */
10028    int iSize = 224;         /* Hash algorithm to use */
10029    int bDebug = 0;          /* Only show the query that would have run */
10030    sqlite3_stmt *pStmt;     /* For querying tables names */
10031    char *zSql;              /* SQL to be run */
10032    char *zSep;              /* Separator */
10033    ShellText sSql;          /* Complete SQL for the query to run the hash */
10034    ShellText sQuery;        /* Set of queries used to read all content */
10035    open_db(p, 0);
10036    for(i=1; i<nArg; i++){
10037      const char *z = azArg[i];
10038      if( z[0]=='-' ){
10039        z++;
10040        if( z[0]=='-' ) z++;
10041        if( cli_strcmp(z,"schema")==0 ){
10042          bSchema = 1;
10043        }else
10044        if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
10045         || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
10046        ){
10047          iSize = atoi(&z[5]);
10048        }else
10049        if( cli_strcmp(z,"debug")==0 ){
10050          bDebug = 1;
10051        }else
10052        {
10053          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10054                      azArg[i], azArg[0]);
10055          showHelp(p->out, azArg[0]);
10056          rc = 1;
10057          goto meta_command_exit;
10058        }
10059      }else if( zLike ){
10060        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10061        rc = 1;
10062        goto meta_command_exit;
10063      }else{
10064        zLike = z;
10065        bSeparate = 1;
10066        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10067      }
10068    }
10069    if( bSchema ){
10070      zSql = "SELECT lower(name) FROM sqlite_schema"
10071             " WHERE type='table' AND coalesce(rootpage,0)>1"
10072             " UNION ALL SELECT 'sqlite_schema'"
10073             " ORDER BY 1 collate nocase";
10074    }else{
10075      zSql = "SELECT lower(name) FROM sqlite_schema"
10076             " WHERE type='table' AND coalesce(rootpage,0)>1"
10077             " AND name NOT LIKE 'sqlite_%'"
10078             " ORDER BY 1 collate nocase";
10079    }
10080    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10081    initText(&sQuery);
10082    initText(&sSql);
10083    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10084    zSep = "VALUES(";
10085    while( SQLITE_ROW==sqlite3_step(pStmt) ){
10086      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10087      if( zTab==0 ) continue;
10088      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10089      if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
10090        appendText(&sQuery,"SELECT * FROM ", 0);
10091        appendText(&sQuery,zTab,'"');
10092        appendText(&sQuery," NOT INDEXED;", 0);
10093      }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
10094        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10095                           " ORDER BY name;", 0);
10096      }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
10097        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10098                           " ORDER BY name;", 0);
10099      }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
10100        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10101                           " ORDER BY tbl,idx;", 0);
10102      }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
10103        appendText(&sQuery, "SELECT * FROM ", 0);
10104        appendText(&sQuery, zTab, 0);
10105        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10106      }
10107      appendText(&sSql, zSep, 0);
10108      appendText(&sSql, sQuery.z, '\'');
10109      sQuery.n = 0;
10110      appendText(&sSql, ",", 0);
10111      appendText(&sSql, zTab, '\'');
10112      zSep = "),(";
10113    }
10114    sqlite3_finalize(pStmt);
10115    if( bSeparate ){
10116      zSql = sqlite3_mprintf(
10117          "%s))"
10118          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10119          "   FROM [sha3sum$query]",
10120          sSql.z, iSize);
10121    }else{
10122      zSql = sqlite3_mprintf(
10123          "%s))"
10124          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10125          "   FROM [sha3sum$query]",
10126          sSql.z, iSize);
10127    }
10128    shell_check_oom(zSql);
10129    freeText(&sQuery);
10130    freeText(&sSql);
10131    if( bDebug ){
10132      utf8_printf(p->out, "%s\n", zSql);
10133    }else{
10134      shell_exec(p, zSql, 0);
10135    }
10136    sqlite3_free(zSql);
10137  }else
10138
10139#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10140  if( c=='s'
10141   && (cli_strncmp(azArg[0], "shell", n)==0
10142       || cli_strncmp(azArg[0],"system",n)==0)
10143  ){
10144    char *zCmd;
10145    int i, x;
10146    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10147    if( nArg<2 ){
10148      raw_printf(stderr, "Usage: .system COMMAND\n");
10149      rc = 1;
10150      goto meta_command_exit;
10151    }
10152    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10153    for(i=2; i<nArg && zCmd!=0; i++){
10154      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10155                             zCmd, azArg[i]);
10156    }
10157    x = zCmd!=0 ? system(zCmd) : 1;
10158    sqlite3_free(zCmd);
10159    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10160  }else
10161#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10162
10163  if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
10164    static const char *azBool[] = { "off", "on", "trigger", "full"};
10165    const char *zOut;
10166    int i;
10167    if( nArg!=1 ){
10168      raw_printf(stderr, "Usage: .show\n");
10169      rc = 1;
10170      goto meta_command_exit;
10171    }
10172    utf8_printf(p->out, "%12.12s: %s\n","echo",
10173                azBool[ShellHasFlag(p, SHFLG_Echo)]);
10174    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10175    utf8_printf(p->out, "%12.12s: %s\n","explain",
10176         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10177    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10178    if( p->mode==MODE_Column
10179     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10180    ){
10181      utf8_printf
10182        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10183         modeDescr[p->mode], p->cmOpts.iWrap,
10184         p->cmOpts.bWordWrap ? "on" : "off",
10185         p->cmOpts.bQuote ? "" : "no");
10186    }else{
10187      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10188    }
10189    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10190      output_c_string(p->out, p->nullValue);
10191      raw_printf(p->out, "\n");
10192    utf8_printf(p->out,"%12.12s: %s\n","output",
10193            strlen30(p->outfile) ? p->outfile : "stdout");
10194    utf8_printf(p->out,"%12.12s: ", "colseparator");
10195      output_c_string(p->out, p->colSeparator);
10196      raw_printf(p->out, "\n");
10197    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10198      output_c_string(p->out, p->rowSeparator);
10199      raw_printf(p->out, "\n");
10200    switch( p->statsOn ){
10201      case 0:  zOut = "off";     break;
10202      default: zOut = "on";      break;
10203      case 2:  zOut = "stmt";    break;
10204      case 3:  zOut = "vmstep";  break;
10205    }
10206    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10207    utf8_printf(p->out, "%12.12s: ", "width");
10208    for (i=0;i<p->nWidth;i++) {
10209      raw_printf(p->out, "%d ", p->colWidth[i]);
10210    }
10211    raw_printf(p->out, "\n");
10212    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10213                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10214  }else
10215
10216  if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
10217    if( nArg==2 ){
10218      if( cli_strcmp(azArg[1],"stmt")==0 ){
10219        p->statsOn = 2;
10220      }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
10221        p->statsOn = 3;
10222      }else{
10223        p->statsOn = (u8)booleanValue(azArg[1]);
10224      }
10225    }else if( nArg==1 ){
10226      display_stats(p->db, p, 0);
10227    }else{
10228      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10229      rc = 1;
10230    }
10231  }else
10232
10233  if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
10234   || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
10235                 || cli_strncmp(azArg[0], "indexes", n)==0) )
10236  ){
10237    sqlite3_stmt *pStmt;
10238    char **azResult;
10239    int nRow, nAlloc;
10240    int ii;
10241    ShellText s;
10242    initText(&s);
10243    open_db(p, 0);
10244    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10245    if( rc ){
10246      sqlite3_finalize(pStmt);
10247      return shellDatabaseError(p->db);
10248    }
10249
10250    if( nArg>2 && c=='i' ){
10251      /* It is an historical accident that the .indexes command shows an error
10252      ** when called with the wrong number of arguments whereas the .tables
10253      ** command does not. */
10254      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10255      rc = 1;
10256      sqlite3_finalize(pStmt);
10257      goto meta_command_exit;
10258    }
10259    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10260      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10261      if( zDbName==0 ) continue;
10262      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10263      if( sqlite3_stricmp(zDbName, "main")==0 ){
10264        appendText(&s, "SELECT name FROM ", 0);
10265      }else{
10266        appendText(&s, "SELECT ", 0);
10267        appendText(&s, zDbName, '\'');
10268        appendText(&s, "||'.'||name FROM ", 0);
10269      }
10270      appendText(&s, zDbName, '"');
10271      appendText(&s, ".sqlite_schema ", 0);
10272      if( c=='t' ){
10273        appendText(&s," WHERE type IN ('table','view')"
10274                      "   AND name NOT LIKE 'sqlite_%'"
10275                      "   AND name LIKE ?1", 0);
10276      }else{
10277        appendText(&s," WHERE type='index'"
10278                      "   AND tbl_name LIKE ?1", 0);
10279      }
10280    }
10281    rc = sqlite3_finalize(pStmt);
10282    if( rc==SQLITE_OK ){
10283      appendText(&s, " ORDER BY 1", 0);
10284      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10285    }
10286    freeText(&s);
10287    if( rc ) return shellDatabaseError(p->db);
10288
10289    /* Run the SQL statement prepared by the above block. Store the results
10290    ** as an array of nul-terminated strings in azResult[].  */
10291    nRow = nAlloc = 0;
10292    azResult = 0;
10293    if( nArg>1 ){
10294      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10295    }else{
10296      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10297    }
10298    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10299      if( nRow>=nAlloc ){
10300        char **azNew;
10301        int n2 = nAlloc*2 + 10;
10302        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10303        shell_check_oom(azNew);
10304        nAlloc = n2;
10305        azResult = azNew;
10306      }
10307      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10308      shell_check_oom(azResult[nRow]);
10309      nRow++;
10310    }
10311    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10312      rc = shellDatabaseError(p->db);
10313    }
10314
10315    /* Pretty-print the contents of array azResult[] to the output */
10316    if( rc==0 && nRow>0 ){
10317      int len, maxlen = 0;
10318      int i, j;
10319      int nPrintCol, nPrintRow;
10320      for(i=0; i<nRow; i++){
10321        len = strlen30(azResult[i]);
10322        if( len>maxlen ) maxlen = len;
10323      }
10324      nPrintCol = 80/(maxlen+2);
10325      if( nPrintCol<1 ) nPrintCol = 1;
10326      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10327      for(i=0; i<nPrintRow; i++){
10328        for(j=i; j<nRow; j+=nPrintRow){
10329          char *zSp = j<nPrintRow ? "" : "  ";
10330          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10331                      azResult[j] ? azResult[j]:"");
10332        }
10333        raw_printf(p->out, "\n");
10334      }
10335    }
10336
10337    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10338    sqlite3_free(azResult);
10339  }else
10340
10341#ifndef SQLITE_SHELL_FIDDLE
10342  /* Begin redirecting output to the file "testcase-out.txt" */
10343  if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
10344    output_reset(p);
10345    p->out = output_file_open("testcase-out.txt", 0);
10346    if( p->out==0 ){
10347      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10348    }
10349    if( nArg>=2 ){
10350      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10351    }else{
10352      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10353    }
10354  }else
10355#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10356
10357#ifndef SQLITE_UNTESTABLE
10358  if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
10359    static const struct {
10360       const char *zCtrlName;   /* Name of a test-control option */
10361       int ctrlCode;            /* Integer code for that option */
10362       int unSafe;              /* Not valid for --safe mode */
10363       const char *zUsage;      /* Usage notes */
10364    } aCtrl[] = {
10365      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10366      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10367    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10368    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10369      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10370      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10371    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10372      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10373      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10374      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10375      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10376      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10377#ifdef YYCOVERAGE
10378      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10379#endif
10380      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10381      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10382      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10383      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10384      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10385      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10386      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10387    };
10388    int testctrl = -1;
10389    int iCtrl = -1;
10390    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10391    int isOk = 0;
10392    int i, n2;
10393    const char *zCmd = 0;
10394
10395    open_db(p, 0);
10396    zCmd = nArg>=2 ? azArg[1] : "help";
10397
10398    /* The argument can optionally begin with "-" or "--" */
10399    if( zCmd[0]=='-' && zCmd[1] ){
10400      zCmd++;
10401      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10402    }
10403
10404    /* --help lists all test-controls */
10405    if( cli_strcmp(zCmd,"help")==0 ){
10406      utf8_printf(p->out, "Available test-controls:\n");
10407      for(i=0; i<ArraySize(aCtrl); i++){
10408        utf8_printf(p->out, "  .testctrl %s %s\n",
10409                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10410      }
10411      rc = 1;
10412      goto meta_command_exit;
10413    }
10414
10415    /* convert testctrl text option to value. allow any unique prefix
10416    ** of the option name, or a numerical value. */
10417    n2 = strlen30(zCmd);
10418    for(i=0; i<ArraySize(aCtrl); i++){
10419      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10420        if( testctrl<0 ){
10421          testctrl = aCtrl[i].ctrlCode;
10422          iCtrl = i;
10423        }else{
10424          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10425                              "Use \".testctrl --help\" for help\n", zCmd);
10426          rc = 1;
10427          goto meta_command_exit;
10428        }
10429      }
10430    }
10431    if( testctrl<0 ){
10432      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10433                         "Use \".testctrl --help\" for help\n", zCmd);
10434    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
10435      utf8_printf(stderr,
10436         "line %d: \".testctrl %s\" may not be used in safe mode\n",
10437         p->lineno, aCtrl[iCtrl].zCtrlName);
10438      exit(1);
10439    }else{
10440      switch(testctrl){
10441
10442        /* sqlite3_test_control(int, db, int) */
10443        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10444          if( nArg==3 ){
10445            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10446            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10447            isOk = 3;
10448          }
10449          break;
10450
10451        /* sqlite3_test_control(int) */
10452        case SQLITE_TESTCTRL_PRNG_SAVE:
10453        case SQLITE_TESTCTRL_PRNG_RESTORE:
10454        case SQLITE_TESTCTRL_BYTEORDER:
10455          if( nArg==2 ){
10456            rc2 = sqlite3_test_control(testctrl);
10457            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10458          }
10459          break;
10460
10461        /* sqlite3_test_control(int, uint) */
10462        case SQLITE_TESTCTRL_PENDING_BYTE:
10463          if( nArg==3 ){
10464            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10465            rc2 = sqlite3_test_control(testctrl, opt);
10466            isOk = 3;
10467          }
10468          break;
10469
10470        /* sqlite3_test_control(int, int, sqlite3*) */
10471        case SQLITE_TESTCTRL_PRNG_SEED:
10472          if( nArg==3 || nArg==4 ){
10473            int ii = (int)integerValue(azArg[2]);
10474            sqlite3 *db;
10475            if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
10476              sqlite3_randomness(sizeof(ii),&ii);
10477              printf("-- random seed: %d\n", ii);
10478            }
10479            if( nArg==3 ){
10480              db = 0;
10481            }else{
10482              db = p->db;
10483              /* Make sure the schema has been loaded */
10484              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10485            }
10486            rc2 = sqlite3_test_control(testctrl, ii, db);
10487            isOk = 3;
10488          }
10489          break;
10490
10491        /* sqlite3_test_control(int, int) */
10492        case SQLITE_TESTCTRL_ASSERT:
10493        case SQLITE_TESTCTRL_ALWAYS:
10494          if( nArg==3 ){
10495            int opt = booleanValue(azArg[2]);
10496            rc2 = sqlite3_test_control(testctrl, opt);
10497            isOk = 1;
10498          }
10499          break;
10500
10501        /* sqlite3_test_control(int, int) */
10502        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10503        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10504          if( nArg==3 ){
10505            int opt = booleanValue(azArg[2]);
10506            rc2 = sqlite3_test_control(testctrl, opt);
10507            isOk = 3;
10508          }
10509          break;
10510
10511        /* sqlite3_test_control(sqlite3*) */
10512        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10513          rc2 = sqlite3_test_control(testctrl, p->db);
10514          isOk = 3;
10515          break;
10516
10517        case SQLITE_TESTCTRL_IMPOSTER:
10518          if( nArg==5 ){
10519            rc2 = sqlite3_test_control(testctrl, p->db,
10520                          azArg[2],
10521                          integerValue(azArg[3]),
10522                          integerValue(azArg[4]));
10523            isOk = 3;
10524          }
10525          break;
10526
10527        case SQLITE_TESTCTRL_SEEK_COUNT: {
10528          u64 x = 0;
10529          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10530          utf8_printf(p->out, "%llu\n", x);
10531          isOk = 3;
10532          break;
10533        }
10534
10535#ifdef YYCOVERAGE
10536        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10537          if( nArg==2 ){
10538            sqlite3_test_control(testctrl, p->out);
10539            isOk = 3;
10540          }
10541          break;
10542        }
10543#endif
10544#ifdef SQLITE_DEBUG
10545        case SQLITE_TESTCTRL_TUNE: {
10546          if( nArg==4 ){
10547            int id = (int)integerValue(azArg[2]);
10548            int val = (int)integerValue(azArg[3]);
10549            sqlite3_test_control(testctrl, id, &val);
10550            isOk = 3;
10551          }else if( nArg==3 ){
10552            int id = (int)integerValue(azArg[2]);
10553            sqlite3_test_control(testctrl, -id, &rc2);
10554            isOk = 1;
10555          }else if( nArg==2 ){
10556            int id = 1;
10557            while(1){
10558              int val = 0;
10559              rc2 = sqlite3_test_control(testctrl, -id, &val);
10560              if( rc2!=SQLITE_OK ) break;
10561              if( id>1 ) utf8_printf(p->out, "  ");
10562              utf8_printf(p->out, "%d: %d", id, val);
10563              id++;
10564            }
10565            if( id>1 ) utf8_printf(p->out, "\n");
10566            isOk = 3;
10567          }
10568          break;
10569        }
10570#endif
10571        case SQLITE_TESTCTRL_SORTER_MMAP:
10572          if( nArg==3 ){
10573            int opt = (unsigned int)integerValue(azArg[2]);
10574            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10575            isOk = 3;
10576          }
10577          break;
10578      }
10579    }
10580    if( isOk==0 && iCtrl>=0 ){
10581      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10582      rc = 1;
10583    }else if( isOk==1 ){
10584      raw_printf(p->out, "%d\n", rc2);
10585    }else if( isOk==2 ){
10586      raw_printf(p->out, "0x%08x\n", rc2);
10587    }
10588  }else
10589#endif /* !defined(SQLITE_UNTESTABLE) */
10590
10591  if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
10592    open_db(p, 0);
10593    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10594  }else
10595
10596  if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
10597    if( nArg==2 ){
10598      enableTimer = booleanValue(azArg[1]);
10599      if( enableTimer && !HAS_TIMER ){
10600        raw_printf(stderr, "Error: timer not available on this system.\n");
10601        enableTimer = 0;
10602      }
10603    }else{
10604      raw_printf(stderr, "Usage: .timer on|off\n");
10605      rc = 1;
10606    }
10607  }else
10608
10609#ifndef SQLITE_OMIT_TRACE
10610  if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
10611    int mType = 0;
10612    int jj;
10613    open_db(p, 0);
10614    for(jj=1; jj<nArg; jj++){
10615      const char *z = azArg[jj];
10616      if( z[0]=='-' ){
10617        if( optionMatch(z, "expanded") ){
10618          p->eTraceType = SHELL_TRACE_EXPANDED;
10619        }
10620#ifdef SQLITE_ENABLE_NORMALIZE
10621        else if( optionMatch(z, "normalized") ){
10622          p->eTraceType = SHELL_TRACE_NORMALIZED;
10623        }
10624#endif
10625        else if( optionMatch(z, "plain") ){
10626          p->eTraceType = SHELL_TRACE_PLAIN;
10627        }
10628        else if( optionMatch(z, "profile") ){
10629          mType |= SQLITE_TRACE_PROFILE;
10630        }
10631        else if( optionMatch(z, "row") ){
10632          mType |= SQLITE_TRACE_ROW;
10633        }
10634        else if( optionMatch(z, "stmt") ){
10635          mType |= SQLITE_TRACE_STMT;
10636        }
10637        else if( optionMatch(z, "close") ){
10638          mType |= SQLITE_TRACE_CLOSE;
10639        }
10640        else {
10641          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10642          rc = 1;
10643          goto meta_command_exit;
10644        }
10645      }else{
10646        output_file_close(p->traceOut);
10647        p->traceOut = output_file_open(azArg[1], 0);
10648      }
10649    }
10650    if( p->traceOut==0 ){
10651      sqlite3_trace_v2(p->db, 0, 0, 0);
10652    }else{
10653      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10654      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10655    }
10656  }else
10657#endif /* !defined(SQLITE_OMIT_TRACE) */
10658
10659#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10660  if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
10661    int ii;
10662    int lenOpt;
10663    char *zOpt;
10664    if( nArg<2 ){
10665      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10666      rc = 1;
10667      goto meta_command_exit;
10668    }
10669    open_db(p, 0);
10670    zOpt = azArg[1];
10671    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10672    lenOpt = (int)strlen(zOpt);
10673    if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10674      assert( azArg[nArg]==0 );
10675      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10676    }else{
10677      for(ii=1; ii<nArg; ii++){
10678        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10679      }
10680    }
10681  }else
10682#endif
10683
10684#if SQLITE_USER_AUTHENTICATION
10685  if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
10686    if( nArg<2 ){
10687      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10688      rc = 1;
10689      goto meta_command_exit;
10690    }
10691    open_db(p, 0);
10692    if( cli_strcmp(azArg[1],"login")==0 ){
10693      if( nArg!=4 ){
10694        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10695        rc = 1;
10696        goto meta_command_exit;
10697      }
10698      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10699                                     strlen30(azArg[3]));
10700      if( rc ){
10701        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10702        rc = 1;
10703      }
10704    }else if( cli_strcmp(azArg[1],"add")==0 ){
10705      if( nArg!=5 ){
10706        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10707        rc = 1;
10708        goto meta_command_exit;
10709      }
10710      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10711                            booleanValue(azArg[4]));
10712      if( rc ){
10713        raw_printf(stderr, "User-Add failed: %d\n", rc);
10714        rc = 1;
10715      }
10716    }else if( cli_strcmp(azArg[1],"edit")==0 ){
10717      if( nArg!=5 ){
10718        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10719        rc = 1;
10720        goto meta_command_exit;
10721      }
10722      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10723                              booleanValue(azArg[4]));
10724      if( rc ){
10725        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10726        rc = 1;
10727      }
10728    }else if( cli_strcmp(azArg[1],"delete")==0 ){
10729      if( nArg!=3 ){
10730        raw_printf(stderr, "Usage: .user delete USER\n");
10731        rc = 1;
10732        goto meta_command_exit;
10733      }
10734      rc = sqlite3_user_delete(p->db, azArg[2]);
10735      if( rc ){
10736        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10737        rc = 1;
10738      }
10739    }else{
10740      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10741      rc = 1;
10742      goto meta_command_exit;
10743    }
10744  }else
10745#endif /* SQLITE_USER_AUTHENTICATION */
10746
10747  if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
10748    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10749        sqlite3_libversion(), sqlite3_sourceid());
10750#if SQLITE_HAVE_ZLIB
10751    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10752#endif
10753#define CTIMEOPT_VAL_(opt) #opt
10754#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10755#if defined(__clang__) && defined(__clang_major__)
10756    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10757                    CTIMEOPT_VAL(__clang_minor__) "."
10758                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10759#elif defined(_MSC_VER)
10760    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10761#elif defined(__GNUC__) && defined(__VERSION__)
10762    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10763#endif
10764  }else
10765
10766  if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
10767    const char *zDbName = nArg==2 ? azArg[1] : "main";
10768    sqlite3_vfs *pVfs = 0;
10769    if( p->db ){
10770      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10771      if( pVfs ){
10772        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10773        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10774        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10775        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10776      }
10777    }
10778  }else
10779
10780  if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
10781    sqlite3_vfs *pVfs;
10782    sqlite3_vfs *pCurrent = 0;
10783    if( p->db ){
10784      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10785    }
10786    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10787      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10788           pVfs==pCurrent ? "  <--- CURRENT" : "");
10789      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10790      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10791      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10792      if( pVfs->pNext ){
10793        raw_printf(p->out, "-----------------------------------\n");
10794      }
10795    }
10796  }else
10797
10798  if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
10799    const char *zDbName = nArg==2 ? azArg[1] : "main";
10800    char *zVfsName = 0;
10801    if( p->db ){
10802      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10803      if( zVfsName ){
10804        utf8_printf(p->out, "%s\n", zVfsName);
10805        sqlite3_free(zVfsName);
10806      }
10807    }
10808  }else
10809
10810  if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
10811    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10812    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10813  }else
10814
10815  if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
10816    int j;
10817    assert( nArg<=ArraySize(azArg) );
10818    p->nWidth = nArg-1;
10819    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
10820    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10821    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10822    for(j=1; j<nArg; j++){
10823      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10824    }
10825  }else
10826
10827  {
10828    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10829      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10830    rc = 1;
10831  }
10832
10833meta_command_exit:
10834  if( p->outCount ){
10835    p->outCount--;
10836    if( p->outCount==0 ) output_reset(p);
10837  }
10838  p->bSafeMode = p->bSafeModePersist;
10839  return rc;
10840}
10841
10842/* Line scan result and intermediate states (supporting scan resumption)
10843*/
10844#ifndef CHAR_BIT
10845# define CHAR_BIT 8
10846#endif
10847typedef enum {
10848  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
10849  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
10850  QSS_Start = 0
10851} QuickScanState;
10852#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
10853#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
10854#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
10855#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
10856#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
10857
10858/*
10859** Scan line for classification to guide shell's handling.
10860** The scan is resumable for subsequent lines when prior
10861** return values are passed as the 2nd argument.
10862*/
10863static QuickScanState quickscan(char *zLine, QuickScanState qss){
10864  char cin;
10865  char cWait = (char)qss; /* intentional narrowing loss */
10866  if( cWait==0 ){
10867  PlainScan:
10868    assert( cWait==0 );
10869    while( (cin = *zLine++)!=0 ){
10870      if( IsSpace(cin) )
10871        continue;
10872      switch (cin){
10873      case '-':
10874        if( *zLine!='-' )
10875          break;
10876        while((cin = *++zLine)!=0 )
10877          if( cin=='\n')
10878            goto PlainScan;
10879        return qss;
10880      case ';':
10881        qss |= QSS_EndingSemi;
10882        continue;
10883      case '/':
10884        if( *zLine=='*' ){
10885          ++zLine;
10886          cWait = '*';
10887          qss = QSS_SETV(qss, cWait);
10888          goto TermScan;
10889        }
10890        break;
10891      case '[':
10892        cin = ']';
10893        /* fall thru */
10894      case '`': case '\'': case '"':
10895        cWait = cin;
10896        qss = QSS_HasDark | cWait;
10897        goto TermScan;
10898      default:
10899        break;
10900      }
10901      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
10902    }
10903  }else{
10904  TermScan:
10905    while( (cin = *zLine++)!=0 ){
10906      if( cin==cWait ){
10907        switch( cWait ){
10908        case '*':
10909          if( *zLine != '/' )
10910            continue;
10911          ++zLine;
10912          cWait = 0;
10913          qss = QSS_SETV(qss, 0);
10914          goto PlainScan;
10915        case '`': case '\'': case '"':
10916          if(*zLine==cWait){
10917            ++zLine;
10918            continue;
10919          }
10920          /* fall thru */
10921        case ']':
10922          cWait = 0;
10923          qss = QSS_SETV(qss, 0);
10924          goto PlainScan;
10925        default: assert(0);
10926        }
10927      }
10928    }
10929  }
10930  return qss;
10931}
10932
10933/*
10934** Return TRUE if the line typed in is an SQL command terminator other
10935** than a semi-colon.  The SQL Server style "go" command is understood
10936** as is the Oracle "/".
10937*/
10938static int line_is_command_terminator(char *zLine){
10939  while( IsSpace(zLine[0]) ){ zLine++; };
10940  if( zLine[0]=='/' )
10941    zLine += 1; /* Oracle */
10942  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
10943    zLine += 2; /* SQL Server */
10944  else
10945    return 0;
10946  return quickscan(zLine, QSS_Start)==QSS_Start;
10947}
10948
10949/*
10950** We need a default sqlite3_complete() implementation to use in case
10951** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10952** any arbitrary text is a complete SQL statement.  This is not very
10953** user-friendly, but it does seem to work.
10954*/
10955#ifdef SQLITE_OMIT_COMPLETE
10956#define sqlite3_complete(x) 1
10957#endif
10958
10959/*
10960** Return true if zSql is a complete SQL statement.  Return false if it
10961** ends in the middle of a string literal or C-style comment.
10962*/
10963static int line_is_complete(char *zSql, int nSql){
10964  int rc;
10965  if( zSql==0 ) return 1;
10966  zSql[nSql] = ';';
10967  zSql[nSql+1] = 0;
10968  rc = sqlite3_complete(zSql);
10969  zSql[nSql] = 0;
10970  return rc;
10971}
10972
10973/*
10974** Run a single line of SQL.  Return the number of errors.
10975*/
10976static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10977  int rc;
10978  char *zErrMsg = 0;
10979
10980  open_db(p, 0);
10981  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10982  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10983  BEGIN_TIMER;
10984  rc = shell_exec(p, zSql, &zErrMsg);
10985  END_TIMER;
10986  if( rc || zErrMsg ){
10987    char zPrefix[100];
10988    const char *zErrorTail;
10989    const char *zErrorType;
10990    if( zErrMsg==0 ){
10991      zErrorType = "Error";
10992      zErrorTail = sqlite3_errmsg(p->db);
10993    }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
10994      zErrorType = "Parse error";
10995      zErrorTail = &zErrMsg[12];
10996    }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
10997      zErrorType = "Runtime error";
10998      zErrorTail = &zErrMsg[10];
10999    }else{
11000      zErrorType = "Error";
11001      zErrorTail = zErrMsg;
11002    }
11003    if( in!=0 || !stdin_is_interactive ){
11004      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11005                       "%s near line %d:", zErrorType, startline);
11006    }else{
11007      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11008    }
11009    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11010    sqlite3_free(zErrMsg);
11011    zErrMsg = 0;
11012    return 1;
11013  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11014    char zLineBuf[2000];
11015    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11016            "changes: %lld   total_changes: %lld",
11017            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11018    raw_printf(p->out, "%s\n", zLineBuf);
11019  }
11020  return 0;
11021}
11022
11023static void echo_group_input(ShellState *p, const char *zDo){
11024  if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11025}
11026
11027#ifdef SQLITE_SHELL_FIDDLE
11028/*
11029** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
11030** because we need the global shellState and cannot access it from that function
11031** without moving lots of code around (creating a larger/messier diff).
11032*/
11033static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11034  /* Parse the next line from shellState.wasm.zInput. */
11035  const char *zBegin = shellState.wasm.zPos;
11036  const char *z = zBegin;
11037  char *zLine = 0;
11038  i64 nZ = 0;
11039
11040  UNUSED_PARAMETER(in);
11041  UNUSED_PARAMETER(isContinuation);
11042  if(!z || !*z){
11043    return 0;
11044  }
11045  while(*z && isspace(*z)) ++z;
11046  zBegin = z;
11047  for(; *z && '\n'!=*z; ++nZ, ++z){}
11048  if(nZ>0 && '\r'==zBegin[nZ-1]){
11049    --nZ;
11050  }
11051  shellState.wasm.zPos = z;
11052  zLine = realloc(zPrior, nZ+1);
11053  shell_check_oom(zLine);
11054  memcpy(zLine, zBegin, nZ);
11055  zLine[nZ] = 0;
11056  return zLine;
11057}
11058#endif /* SQLITE_SHELL_FIDDLE */
11059
11060/*
11061** Read input from *in and process it.  If *in==0 then input
11062** is interactive - the user is typing it it.  Otherwise, input
11063** is coming from a file or device.  A prompt is issued and history
11064** is saved only if input is interactive.  An interrupt signal will
11065** cause this routine to exit immediately, unless input is interactive.
11066**
11067** Return the number of errors.
11068*/
11069static int process_input(ShellState *p){
11070  char *zLine = 0;          /* A single input line */
11071  char *zSql = 0;           /* Accumulated SQL text */
11072  i64 nLine;                /* Length of current line */
11073  i64 nSql = 0;             /* Bytes of zSql[] used */
11074  i64 nAlloc = 0;           /* Allocated zSql[] space */
11075  int rc;                   /* Error code */
11076  int errCnt = 0;           /* Number of errors seen */
11077  i64 startline = 0;        /* Line number for start of current input */
11078  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11079
11080  if( p->inputNesting==MAX_INPUT_NESTING ){
11081    /* This will be more informative in a later version. */
11082    utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11083                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11084    return 1;
11085  }
11086  ++p->inputNesting;
11087  p->lineno = 0;
11088  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11089    fflush(p->out);
11090    zLine = one_input_line(p->in, zLine, nSql>0);
11091    if( zLine==0 ){
11092      /* End of input */
11093      if( p->in==0 && stdin_is_interactive ) printf("\n");
11094      break;
11095    }
11096    if( seenInterrupt ){
11097      if( p->in!=0 ) break;
11098      seenInterrupt = 0;
11099    }
11100    p->lineno++;
11101    if( QSS_INPLAIN(qss)
11102        && line_is_command_terminator(zLine)
11103        && line_is_complete(zSql, nSql) ){
11104      memcpy(zLine,";",2);
11105    }
11106    qss = quickscan(zLine, qss);
11107    if( QSS_PLAINWHITE(qss) && nSql==0 ){
11108      /* Just swallow single-line whitespace */
11109      echo_group_input(p, zLine);
11110      qss = QSS_Start;
11111      continue;
11112    }
11113    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11114      echo_group_input(p, zLine);
11115      if( zLine[0]=='.' ){
11116        rc = do_meta_command(zLine, p);
11117        if( rc==2 ){ /* exit requested */
11118          break;
11119        }else if( rc ){
11120          errCnt++;
11121        }
11122      }
11123      qss = QSS_Start;
11124      continue;
11125    }
11126    /* No single-line dispositions remain; accumulate line(s). */
11127    nLine = strlen(zLine);
11128    if( nSql+nLine+2>=nAlloc ){
11129      /* Grow buffer by half-again increments when big. */
11130      nAlloc = nSql+(nSql>>1)+nLine+100;
11131      zSql = realloc(zSql, nAlloc);
11132      shell_check_oom(zSql);
11133    }
11134    if( nSql==0 ){
11135      i64 i;
11136      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11137      assert( nAlloc>0 && zSql!=0 );
11138      memcpy(zSql, zLine+i, nLine+1-i);
11139      startline = p->lineno;
11140      nSql = nLine-i;
11141    }else{
11142      zSql[nSql++] = '\n';
11143      memcpy(zSql+nSql, zLine, nLine+1);
11144      nSql += nLine;
11145    }
11146    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11147      echo_group_input(p, zSql);
11148      errCnt += runOneSqlLine(p, zSql, p->in, startline);
11149      nSql = 0;
11150      if( p->outCount ){
11151        output_reset(p);
11152        p->outCount = 0;
11153      }else{
11154        clearTempFile(p);
11155      }
11156      p->bSafeMode = p->bSafeModePersist;
11157      qss = QSS_Start;
11158    }else if( nSql && QSS_PLAINWHITE(qss) ){
11159      echo_group_input(p, zSql);
11160      nSql = 0;
11161      qss = QSS_Start;
11162    }
11163  }
11164  if( nSql ){
11165    /* This may be incomplete. Let the SQL parser deal with that. */
11166    echo_group_input(p, zSql);
11167    errCnt += runOneSqlLine(p, zSql, p->in, startline);
11168  }
11169  free(zSql);
11170  free(zLine);
11171  --p->inputNesting;
11172  return errCnt>0;
11173}
11174
11175/*
11176** Return a pathname which is the user's home directory.  A
11177** 0 return indicates an error of some kind.
11178*/
11179static char *find_home_dir(int clearFlag){
11180  static char *home_dir = NULL;
11181  if( clearFlag ){
11182    free(home_dir);
11183    home_dir = 0;
11184    return 0;
11185  }
11186  if( home_dir ) return home_dir;
11187
11188#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11189     && !defined(__RTP__) && !defined(_WRS_KERNEL)
11190  {
11191    struct passwd *pwent;
11192    uid_t uid = getuid();
11193    if( (pwent=getpwuid(uid)) != NULL) {
11194      home_dir = pwent->pw_dir;
11195    }
11196  }
11197#endif
11198
11199#if defined(_WIN32_WCE)
11200  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11201   */
11202  home_dir = "/";
11203#else
11204
11205#if defined(_WIN32) || defined(WIN32)
11206  if (!home_dir) {
11207    home_dir = getenv("USERPROFILE");
11208  }
11209#endif
11210
11211  if (!home_dir) {
11212    home_dir = getenv("HOME");
11213  }
11214
11215#if defined(_WIN32) || defined(WIN32)
11216  if (!home_dir) {
11217    char *zDrive, *zPath;
11218    int n;
11219    zDrive = getenv("HOMEDRIVE");
11220    zPath = getenv("HOMEPATH");
11221    if( zDrive && zPath ){
11222      n = strlen30(zDrive) + strlen30(zPath) + 1;
11223      home_dir = malloc( n );
11224      if( home_dir==0 ) return 0;
11225      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11226      return home_dir;
11227    }
11228    home_dir = "c:\\";
11229  }
11230#endif
11231
11232#endif /* !_WIN32_WCE */
11233
11234  if( home_dir ){
11235    i64 n = strlen(home_dir) + 1;
11236    char *z = malloc( n );
11237    if( z ) memcpy(z, home_dir, n);
11238    home_dir = z;
11239  }
11240
11241  return home_dir;
11242}
11243
11244/*
11245** Read input from the file given by sqliterc_override.  Or if that
11246** parameter is NULL, take input from ~/.sqliterc
11247**
11248** Returns the number of errors.
11249*/
11250static void process_sqliterc(
11251  ShellState *p,                  /* Configuration data */
11252  const char *sqliterc_override   /* Name of config file. NULL to use default */
11253){
11254  char *home_dir = NULL;
11255  const char *sqliterc = sqliterc_override;
11256  char *zBuf = 0;
11257  FILE *inSaved = p->in;
11258  int savedLineno = p->lineno;
11259
11260  if (sqliterc == NULL) {
11261    home_dir = find_home_dir(0);
11262    if( home_dir==0 ){
11263      raw_printf(stderr, "-- warning: cannot find home directory;"
11264                      " cannot read ~/.sqliterc\n");
11265      return;
11266    }
11267    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11268    shell_check_oom(zBuf);
11269    sqliterc = zBuf;
11270  }
11271  p->in = fopen(sqliterc,"rb");
11272  if( p->in ){
11273    if( stdin_is_interactive ){
11274      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11275    }
11276    if( process_input(p) && bail_on_error ) exit(1);
11277    fclose(p->in);
11278  }else if( sqliterc_override!=0 ){
11279    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11280    if( bail_on_error ) exit(1);
11281  }
11282  p->in = inSaved;
11283  p->lineno = savedLineno;
11284  sqlite3_free(zBuf);
11285}
11286
11287/*
11288** Show available command line options
11289*/
11290static const char zOptions[] =
11291#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11292  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11293#endif
11294  "   -append              append the database to the end of the file\n"
11295  "   -ascii               set output mode to 'ascii'\n"
11296  "   -bail                stop after hitting an error\n"
11297  "   -batch               force batch I/O\n"
11298  "   -box                 set output mode to 'box'\n"
11299  "   -column              set output mode to 'column'\n"
11300  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11301  "   -csv                 set output mode to 'csv'\n"
11302#if !defined(SQLITE_OMIT_DESERIALIZE)
11303  "   -deserialize         open the database using sqlite3_deserialize()\n"
11304#endif
11305  "   -echo                print inputs before execution\n"
11306  "   -init FILENAME       read/process named file\n"
11307  "   -[no]header          turn headers on or off\n"
11308#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11309  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11310#endif
11311  "   -help                show this message\n"
11312  "   -html                set output mode to HTML\n"
11313  "   -interactive         force interactive I/O\n"
11314  "   -json                set output mode to 'json'\n"
11315  "   -line                set output mode to 'line'\n"
11316  "   -list                set output mode to 'list'\n"
11317  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11318  "   -markdown            set output mode to 'markdown'\n"
11319#if !defined(SQLITE_OMIT_DESERIALIZE)
11320  "   -maxsize N           maximum size for a --deserialize database\n"
11321#endif
11322  "   -memtrace            trace all memory allocations and deallocations\n"
11323  "   -mmap N              default mmap size set to N\n"
11324#ifdef SQLITE_ENABLE_MULTIPLEX
11325  "   -multiplex           enable the multiplexor VFS\n"
11326#endif
11327  "   -newline SEP         set output row separator. Default: '\\n'\n"
11328  "   -nofollow            refuse to open symbolic links to database files\n"
11329  "   -nonce STRING        set the safe-mode escape nonce\n"
11330  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11331  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11332  "   -quote               set output mode to 'quote'\n"
11333  "   -readonly            open the database read-only\n"
11334  "   -safe                enable safe-mode\n"
11335  "   -separator SEP       set output column separator. Default: '|'\n"
11336#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11337  "   -sorterref SIZE      sorter references threshold size\n"
11338#endif
11339  "   -stats               print memory stats before each finalize\n"
11340  "   -table               set output mode to 'table'\n"
11341  "   -tabs                set output mode to 'tabs'\n"
11342  "   -version             show SQLite version\n"
11343  "   -vfs NAME            use NAME as the default VFS\n"
11344#ifdef SQLITE_ENABLE_VFSTRACE
11345  "   -vfstrace            enable tracing of all VFS calls\n"
11346#endif
11347#ifdef SQLITE_HAVE_ZLIB
11348  "   -zip                 open the file as a ZIP Archive\n"
11349#endif
11350;
11351static void usage(int showDetail){
11352  utf8_printf(stderr,
11353      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11354      "FILENAME is the name of an SQLite database. A new database is created\n"
11355      "if the file does not previously exist.\n", Argv0);
11356  if( showDetail ){
11357    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11358  }else{
11359    raw_printf(stderr, "Use the -help option for additional information\n");
11360  }
11361  exit(1);
11362}
11363
11364/*
11365** Internal check:  Verify that the SQLite is uninitialized.  Print a
11366** error message if it is initialized.
11367*/
11368static void verify_uninitialized(void){
11369  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11370    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11371                        " initialization.\n");
11372  }
11373}
11374
11375/*
11376** Initialize the state information in data
11377*/
11378static void main_init(ShellState *data) {
11379  memset(data, 0, sizeof(*data));
11380  data->normalMode = data->cMode = data->mode = MODE_List;
11381  data->autoExplain = 1;
11382  data->pAuxDb = &data->aAuxDb[0];
11383  memcpy(data->colSeparator,SEP_Column, 2);
11384  memcpy(data->rowSeparator,SEP_Row, 2);
11385  data->showHeader = 0;
11386  data->shellFlgs = SHFLG_Lookaside;
11387  verify_uninitialized();
11388  sqlite3_config(SQLITE_CONFIG_URI, 1);
11389  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11390  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11391  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11392  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11393}
11394
11395/*
11396** Output text to the console in a font that attracts extra attention.
11397*/
11398#ifdef _WIN32
11399static void printBold(const char *zText){
11400#if !SQLITE_OS_WINRT
11401  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11402  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11403  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11404  SetConsoleTextAttribute(out,
11405         FOREGROUND_RED|FOREGROUND_INTENSITY
11406  );
11407#endif
11408  printf("%s", zText);
11409#if !SQLITE_OS_WINRT
11410  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11411#endif
11412}
11413#else
11414static void printBold(const char *zText){
11415  printf("\033[1m%s\033[0m", zText);
11416}
11417#endif
11418
11419/*
11420** Get the argument to an --option.  Throw an error and die if no argument
11421** is available.
11422*/
11423static char *cmdline_option_value(int argc, char **argv, int i){
11424  if( i==argc ){
11425    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
11426            argv[0], argv[argc-1]);
11427    exit(1);
11428  }
11429  return argv[i];
11430}
11431
11432#ifndef SQLITE_SHELL_IS_UTF8
11433#  if (defined(_WIN32) || defined(WIN32)) \
11434   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
11435#    define SQLITE_SHELL_IS_UTF8          (0)
11436#  else
11437#    define SQLITE_SHELL_IS_UTF8          (1)
11438#  endif
11439#endif
11440
11441#ifdef SQLITE_SHELL_FIDDLE
11442#  define main fiddle_main
11443#endif
11444
11445#if SQLITE_SHELL_IS_UTF8
11446int SQLITE_CDECL main(int argc, char **argv){
11447#else
11448int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
11449  char **argv;
11450#endif
11451#ifdef SQLITE_DEBUG
11452  sqlite3_int64 mem_main_enter = sqlite3_memory_used();
11453#endif
11454  char *zErrMsg = 0;
11455#ifdef SQLITE_SHELL_FIDDLE
11456#  define data shellState
11457#else
11458  ShellState data;
11459#endif
11460  const char *zInitFile = 0;
11461  int i;
11462  int rc = 0;
11463  int warnInmemoryDb = 0;
11464  int readStdin = 1;
11465  int nCmd = 0;
11466  char **azCmd = 0;
11467  const char *zVfs = 0;           /* Value of -vfs command-line option */
11468#if !SQLITE_SHELL_IS_UTF8
11469  char **argvToFree = 0;
11470  int argcToFree = 0;
11471#endif
11472
11473  setBinaryMode(stdin, 0);
11474  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
11475#ifdef SQLITE_SHELL_FIDDLE
11476  stdin_is_interactive = 0;
11477  stdout_is_console = 1;
11478  data.wasm.zDefaultDbName = "/fiddle.sqlite3";
11479#else
11480  stdin_is_interactive = isatty(0);
11481  stdout_is_console = isatty(1);
11482#endif
11483
11484#if !defined(_WIN32_WCE)
11485  if( getenv("SQLITE_DEBUG_BREAK") ){
11486    if( isatty(0) && isatty(2) ){
11487      fprintf(stderr,
11488          "attach debugger to process %d and press any key to continue.\n",
11489          GETPID());
11490      fgetc(stdin);
11491    }else{
11492#if defined(_WIN32) || defined(WIN32)
11493#if SQLITE_OS_WINRT
11494      __debugbreak();
11495#else
11496      DebugBreak();
11497#endif
11498#elif defined(SIGTRAP)
11499      raise(SIGTRAP);
11500#endif
11501    }
11502  }
11503#endif
11504
11505#if USE_SYSTEM_SQLITE+0!=1
11506  if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
11507    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
11508            sqlite3_sourceid(), SQLITE_SOURCE_ID);
11509    exit(1);
11510  }
11511#endif
11512  main_init(&data);
11513
11514  /* On Windows, we must translate command-line arguments into UTF-8.
11515  ** The SQLite memory allocator subsystem has to be enabled in order to
11516  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
11517  ** subsequent sqlite3_config() calls will work.  So copy all results into
11518  ** memory that does not come from the SQLite memory allocator.
11519  */
11520#if !SQLITE_SHELL_IS_UTF8
11521  sqlite3_initialize();
11522  argvToFree = malloc(sizeof(argv[0])*argc*2);
11523  shell_check_oom(argvToFree);
11524  argcToFree = argc;
11525  argv = argvToFree + argc;
11526  for(i=0; i<argc; i++){
11527    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
11528    i64 n;
11529    shell_check_oom(z);
11530    n = strlen(z);
11531    argv[i] = malloc( n+1 );
11532    shell_check_oom(argv[i]);
11533    memcpy(argv[i], z, n+1);
11534    argvToFree[i] = argv[i];
11535    sqlite3_free(z);
11536  }
11537  sqlite3_shutdown();
11538#endif
11539
11540  assert( argc>=1 && argv && argv[0] );
11541  Argv0 = argv[0];
11542
11543  /* Make sure we have a valid signal handler early, before anything
11544  ** else is done.
11545  */
11546#ifdef SIGINT
11547  signal(SIGINT, interrupt_handler);
11548#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11549  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
11550#endif
11551
11552#ifdef SQLITE_SHELL_DBNAME_PROC
11553  {
11554    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
11555    ** of a C-function that will provide the name of the database file.  Use
11556    ** this compile-time option to embed this shell program in larger
11557    ** applications. */
11558    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
11559    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
11560    warnInmemoryDb = 0;
11561  }
11562#endif
11563
11564  /* Do an initial pass through the command-line argument to locate
11565  ** the name of the database file, the name of the initialization file,
11566  ** the size of the alternative malloc heap,
11567  ** and the first command to execute.
11568  */
11569  verify_uninitialized();
11570  for(i=1; i<argc; i++){
11571    char *z;
11572    z = argv[i];
11573    if( z[0]!='-' ){
11574      if( data.aAuxDb->zDbFilename==0 ){
11575        data.aAuxDb->zDbFilename = z;
11576      }else{
11577        /* Excesss arguments are interpreted as SQL (or dot-commands) and
11578        ** mean that nothing is read from stdin */
11579        readStdin = 0;
11580        nCmd++;
11581        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
11582        shell_check_oom(azCmd);
11583        azCmd[nCmd-1] = z;
11584      }
11585    }
11586    if( z[1]=='-' ) z++;
11587    if( cli_strcmp(z,"-separator")==0
11588     || cli_strcmp(z,"-nullvalue")==0
11589     || cli_strcmp(z,"-newline")==0
11590     || cli_strcmp(z,"-cmd")==0
11591    ){
11592      (void)cmdline_option_value(argc, argv, ++i);
11593    }else if( cli_strcmp(z,"-init")==0 ){
11594      zInitFile = cmdline_option_value(argc, argv, ++i);
11595    }else if( cli_strcmp(z,"-batch")==0 ){
11596      /* Need to check for batch mode here to so we can avoid printing
11597      ** informational messages (like from process_sqliterc) before
11598      ** we do the actual processing of arguments later in a second pass.
11599      */
11600      stdin_is_interactive = 0;
11601    }else if( cli_strcmp(z,"-heap")==0 ){
11602#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11603      const char *zSize;
11604      sqlite3_int64 szHeap;
11605
11606      zSize = cmdline_option_value(argc, argv, ++i);
11607      szHeap = integerValue(zSize);
11608      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11609      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11610#else
11611      (void)cmdline_option_value(argc, argv, ++i);
11612#endif
11613    }else if( cli_strcmp(z,"-pagecache")==0 ){
11614      sqlite3_int64 n, sz;
11615      sz = integerValue(cmdline_option_value(argc,argv,++i));
11616      if( sz>70000 ) sz = 70000;
11617      if( sz<0 ) sz = 0;
11618      n = integerValue(cmdline_option_value(argc,argv,++i));
11619      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11620        n = 0xffffffffffffLL/sz;
11621      }
11622      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11623                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11624      data.shellFlgs |= SHFLG_Pagecache;
11625    }else if( cli_strcmp(z,"-lookaside")==0 ){
11626      int n, sz;
11627      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11628      if( sz<0 ) sz = 0;
11629      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11630      if( n<0 ) n = 0;
11631      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11632      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11633    }else if( cli_strcmp(z,"-threadsafe")==0 ){
11634      int n;
11635      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11636      switch( n ){
11637         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
11638         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
11639         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
11640      }
11641#ifdef SQLITE_ENABLE_VFSTRACE
11642    }else if( cli_strcmp(z,"-vfstrace")==0 ){
11643      extern int vfstrace_register(
11644         const char *zTraceName,
11645         const char *zOldVfsName,
11646         int (*xOut)(const char*,void*),
11647         void *pOutArg,
11648         int makeDefault
11649      );
11650      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11651#endif
11652#ifdef SQLITE_ENABLE_MULTIPLEX
11653    }else if( cli_strcmp(z,"-multiplex")==0 ){
11654      extern int sqlite3_multiple_initialize(const char*,int);
11655      sqlite3_multiplex_initialize(0, 1);
11656#endif
11657    }else if( cli_strcmp(z,"-mmap")==0 ){
11658      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11659      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11660#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11661    }else if( cli_strcmp(z,"-sorterref")==0 ){
11662      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11663      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11664#endif
11665    }else if( cli_strcmp(z,"-vfs")==0 ){
11666      zVfs = cmdline_option_value(argc, argv, ++i);
11667#ifdef SQLITE_HAVE_ZLIB
11668    }else if( cli_strcmp(z,"-zip")==0 ){
11669      data.openMode = SHELL_OPEN_ZIPFILE;
11670#endif
11671    }else if( cli_strcmp(z,"-append")==0 ){
11672      data.openMode = SHELL_OPEN_APPENDVFS;
11673#ifndef SQLITE_OMIT_DESERIALIZE
11674    }else if( cli_strcmp(z,"-deserialize")==0 ){
11675      data.openMode = SHELL_OPEN_DESERIALIZE;
11676    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
11677      data.szMax = integerValue(argv[++i]);
11678#endif
11679    }else if( cli_strcmp(z,"-readonly")==0 ){
11680      data.openMode = SHELL_OPEN_READONLY;
11681    }else if( cli_strcmp(z,"-nofollow")==0 ){
11682      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11683#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11684    }else if( cli_strncmp(z, "-A",2)==0 ){
11685      /* All remaining command-line arguments are passed to the ".archive"
11686      ** command, so ignore them */
11687      break;
11688#endif
11689    }else if( cli_strcmp(z, "-memtrace")==0 ){
11690      sqlite3MemTraceActivate(stderr);
11691    }else if( cli_strcmp(z,"-bail")==0 ){
11692      bail_on_error = 1;
11693    }else if( cli_strcmp(z,"-nonce")==0 ){
11694      free(data.zNonce);
11695      data.zNonce = strdup(argv[++i]);
11696    }else if( cli_strcmp(z,"-safe")==0 ){
11697      /* no-op - catch this on the second pass */
11698    }
11699  }
11700  verify_uninitialized();
11701
11702
11703#ifdef SQLITE_SHELL_INIT_PROC
11704  {
11705    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11706    ** of a C-function that will perform initialization actions on SQLite that
11707    ** occur just before or after sqlite3_initialize(). Use this compile-time
11708    ** option to embed this shell program in larger applications. */
11709    extern void SQLITE_SHELL_INIT_PROC(void);
11710    SQLITE_SHELL_INIT_PROC();
11711  }
11712#else
11713  /* All the sqlite3_config() calls have now been made. So it is safe
11714  ** to call sqlite3_initialize() and process any command line -vfs option. */
11715  sqlite3_initialize();
11716#endif
11717
11718  if( zVfs ){
11719    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11720    if( pVfs ){
11721      sqlite3_vfs_register(pVfs, 1);
11722    }else{
11723      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11724      exit(1);
11725    }
11726  }
11727
11728  if( data.pAuxDb->zDbFilename==0 ){
11729#ifndef SQLITE_OMIT_MEMORYDB
11730    data.pAuxDb->zDbFilename = ":memory:";
11731    warnInmemoryDb = argc==1;
11732#else
11733    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11734    return 1;
11735#endif
11736  }
11737  data.out = stdout;
11738#ifndef SQLITE_SHELL_FIDDLE
11739  sqlite3_appendvfs_init(0,0,0);
11740#endif
11741
11742  /* Go ahead and open the database file if it already exists.  If the
11743  ** file does not exist, delay opening it.  This prevents empty database
11744  ** files from being created if a user mistypes the database name argument
11745  ** to the sqlite command-line tool.
11746  */
11747  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
11748    open_db(&data, 0);
11749  }
11750
11751  /* Process the initialization file if there is one.  If no -init option
11752  ** is given on the command line, look for a file named ~/.sqliterc and
11753  ** try to process it.
11754  */
11755  process_sqliterc(&data,zInitFile);
11756
11757  /* Make a second pass through the command-line argument and set
11758  ** options.  This second pass is delayed until after the initialization
11759  ** file is processed so that the command-line arguments will override
11760  ** settings in the initialization file.
11761  */
11762  for(i=1; i<argc; i++){
11763    char *z = argv[i];
11764    if( z[0]!='-' ) continue;
11765    if( z[1]=='-' ){ z++; }
11766    if( cli_strcmp(z,"-init")==0 ){
11767      i++;
11768    }else if( cli_strcmp(z,"-html")==0 ){
11769      data.mode = MODE_Html;
11770    }else if( cli_strcmp(z,"-list")==0 ){
11771      data.mode = MODE_List;
11772    }else if( cli_strcmp(z,"-quote")==0 ){
11773      data.mode = MODE_Quote;
11774      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11775      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11776    }else if( cli_strcmp(z,"-line")==0 ){
11777      data.mode = MODE_Line;
11778    }else if( cli_strcmp(z,"-column")==0 ){
11779      data.mode = MODE_Column;
11780    }else if( cli_strcmp(z,"-json")==0 ){
11781      data.mode = MODE_Json;
11782    }else if( cli_strcmp(z,"-markdown")==0 ){
11783      data.mode = MODE_Markdown;
11784    }else if( cli_strcmp(z,"-table")==0 ){
11785      data.mode = MODE_Table;
11786    }else if( cli_strcmp(z,"-box")==0 ){
11787      data.mode = MODE_Box;
11788    }else if( cli_strcmp(z,"-csv")==0 ){
11789      data.mode = MODE_Csv;
11790      memcpy(data.colSeparator,",",2);
11791#ifdef SQLITE_HAVE_ZLIB
11792    }else if( cli_strcmp(z,"-zip")==0 ){
11793      data.openMode = SHELL_OPEN_ZIPFILE;
11794#endif
11795    }else if( cli_strcmp(z,"-append")==0 ){
11796      data.openMode = SHELL_OPEN_APPENDVFS;
11797#ifndef SQLITE_OMIT_DESERIALIZE
11798    }else if( cli_strcmp(z,"-deserialize")==0 ){
11799      data.openMode = SHELL_OPEN_DESERIALIZE;
11800    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
11801      data.szMax = integerValue(argv[++i]);
11802#endif
11803    }else if( cli_strcmp(z,"-readonly")==0 ){
11804      data.openMode = SHELL_OPEN_READONLY;
11805    }else if( cli_strcmp(z,"-nofollow")==0 ){
11806      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11807    }else if( cli_strcmp(z,"-ascii")==0 ){
11808      data.mode = MODE_Ascii;
11809      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11810      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11811    }else if( cli_strcmp(z,"-tabs")==0 ){
11812      data.mode = MODE_List;
11813      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11814      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11815    }else if( cli_strcmp(z,"-separator")==0 ){
11816      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11817                       "%s",cmdline_option_value(argc,argv,++i));
11818    }else if( cli_strcmp(z,"-newline")==0 ){
11819      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11820                       "%s",cmdline_option_value(argc,argv,++i));
11821    }else if( cli_strcmp(z,"-nullvalue")==0 ){
11822      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11823                       "%s",cmdline_option_value(argc,argv,++i));
11824    }else if( cli_strcmp(z,"-header")==0 ){
11825      data.showHeader = 1;
11826      ShellSetFlag(&data, SHFLG_HeaderSet);
11827     }else if( cli_strcmp(z,"-noheader")==0 ){
11828      data.showHeader = 0;
11829      ShellSetFlag(&data, SHFLG_HeaderSet);
11830    }else if( cli_strcmp(z,"-echo")==0 ){
11831      ShellSetFlag(&data, SHFLG_Echo);
11832    }else if( cli_strcmp(z,"-eqp")==0 ){
11833      data.autoEQP = AUTOEQP_on;
11834    }else if( cli_strcmp(z,"-eqpfull")==0 ){
11835      data.autoEQP = AUTOEQP_full;
11836    }else if( cli_strcmp(z,"-stats")==0 ){
11837      data.statsOn = 1;
11838    }else if( cli_strcmp(z,"-scanstats")==0 ){
11839      data.scanstatsOn = 1;
11840    }else if( cli_strcmp(z,"-backslash")==0 ){
11841      /* Undocumented command-line option: -backslash
11842      ** Causes C-style backslash escapes to be evaluated in SQL statements
11843      ** prior to sending the SQL into SQLite.  Useful for injecting
11844      ** crazy bytes in the middle of SQL statements for testing and debugging.
11845      */
11846      ShellSetFlag(&data, SHFLG_Backslash);
11847    }else if( cli_strcmp(z,"-bail")==0 ){
11848      /* No-op.  The bail_on_error flag should already be set. */
11849    }else if( cli_strcmp(z,"-version")==0 ){
11850      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11851      return 0;
11852    }else if( cli_strcmp(z,"-interactive")==0 ){
11853      stdin_is_interactive = 1;
11854    }else if( cli_strcmp(z,"-batch")==0 ){
11855      stdin_is_interactive = 0;
11856    }else if( cli_strcmp(z,"-heap")==0 ){
11857      i++;
11858    }else if( cli_strcmp(z,"-pagecache")==0 ){
11859      i+=2;
11860    }else if( cli_strcmp(z,"-lookaside")==0 ){
11861      i+=2;
11862    }else if( cli_strcmp(z,"-threadsafe")==0 ){
11863      i+=2;
11864    }else if( cli_strcmp(z,"-nonce")==0 ){
11865      i += 2;
11866    }else if( cli_strcmp(z,"-mmap")==0 ){
11867      i++;
11868    }else if( cli_strcmp(z,"-memtrace")==0 ){
11869      i++;
11870#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11871    }else if( cli_strcmp(z,"-sorterref")==0 ){
11872      i++;
11873#endif
11874    }else if( cli_strcmp(z,"-vfs")==0 ){
11875      i++;
11876#ifdef SQLITE_ENABLE_VFSTRACE
11877    }else if( cli_strcmp(z,"-vfstrace")==0 ){
11878      i++;
11879#endif
11880#ifdef SQLITE_ENABLE_MULTIPLEX
11881    }else if( cli_strcmp(z,"-multiplex")==0 ){
11882      i++;
11883#endif
11884    }else if( cli_strcmp(z,"-help")==0 ){
11885      usage(1);
11886    }else if( cli_strcmp(z,"-cmd")==0 ){
11887      /* Run commands that follow -cmd first and separately from commands
11888      ** that simply appear on the command-line.  This seems goofy.  It would
11889      ** be better if all commands ran in the order that they appear.  But
11890      ** we retain the goofy behavior for historical compatibility. */
11891      if( i==argc-1 ) break;
11892      z = cmdline_option_value(argc,argv,++i);
11893      if( z[0]=='.' ){
11894        rc = do_meta_command(z, &data);
11895        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11896      }else{
11897        open_db(&data, 0);
11898        rc = shell_exec(&data, z, &zErrMsg);
11899        if( zErrMsg!=0 ){
11900          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11901          if( bail_on_error ) return rc!=0 ? rc : 1;
11902        }else if( rc!=0 ){
11903          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11904          if( bail_on_error ) return rc;
11905        }
11906      }
11907#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11908    }else if( cli_strncmp(z, "-A", 2)==0 ){
11909      if( nCmd>0 ){
11910        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11911                            " with \"%s\"\n", z);
11912        return 1;
11913      }
11914      open_db(&data, OPEN_DB_ZIPFILE);
11915      if( z[2] ){
11916        argv[i] = &z[2];
11917        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11918      }else{
11919        arDotCommand(&data, 1, argv+i, argc-i);
11920      }
11921      readStdin = 0;
11922      break;
11923#endif
11924    }else if( cli_strcmp(z,"-safe")==0 ){
11925      data.bSafeMode = data.bSafeModePersist = 1;
11926    }else{
11927      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11928      raw_printf(stderr,"Use -help for a list of options.\n");
11929      return 1;
11930    }
11931    data.cMode = data.mode;
11932  }
11933
11934  if( !readStdin ){
11935    /* Run all arguments that do not begin with '-' as if they were separate
11936    ** command-line inputs, except for the argToSkip argument which contains
11937    ** the database filename.
11938    */
11939    for(i=0; i<nCmd; i++){
11940      if( azCmd[i][0]=='.' ){
11941        rc = do_meta_command(azCmd[i], &data);
11942        if( rc ){
11943          free(azCmd);
11944          return rc==2 ? 0 : rc;
11945        }
11946      }else{
11947        open_db(&data, 0);
11948        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11949        if( zErrMsg || rc ){
11950          if( zErrMsg!=0 ){
11951            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11952          }else{
11953            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11954          }
11955          sqlite3_free(zErrMsg);
11956          free(azCmd);
11957          return rc!=0 ? rc : 1;
11958        }
11959      }
11960    }
11961  }else{
11962    /* Run commands received from standard input
11963    */
11964    if( stdin_is_interactive ){
11965      char *zHome;
11966      char *zHistory;
11967      int nHistory;
11968      printf(
11969        "SQLite version %s %.19s\n" /*extra-version-info*/
11970        "Enter \".help\" for usage hints.\n",
11971        sqlite3_libversion(), sqlite3_sourceid()
11972      );
11973      if( warnInmemoryDb ){
11974        printf("Connected to a ");
11975        printBold("transient in-memory database");
11976        printf(".\nUse \".open FILENAME\" to reopen on a "
11977               "persistent database.\n");
11978      }
11979      zHistory = getenv("SQLITE_HISTORY");
11980      if( zHistory ){
11981        zHistory = strdup(zHistory);
11982      }else if( (zHome = find_home_dir(0))!=0 ){
11983        nHistory = strlen30(zHome) + 20;
11984        if( (zHistory = malloc(nHistory))!=0 ){
11985          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11986        }
11987      }
11988      if( zHistory ){ shell_read_history(zHistory); }
11989#if HAVE_READLINE || HAVE_EDITLINE
11990      rl_attempted_completion_function = readline_completion;
11991#elif HAVE_LINENOISE
11992      linenoiseSetCompletionCallback(linenoise_completion);
11993#endif
11994      data.in = 0;
11995      rc = process_input(&data);
11996      if( zHistory ){
11997        shell_stifle_history(2000);
11998        shell_write_history(zHistory);
11999        free(zHistory);
12000      }
12001    }else{
12002      data.in = stdin;
12003      rc = process_input(&data);
12004    }
12005  }
12006#ifndef SQLITE_SHELL_FIDDLE
12007  /* In WASM mode we have to leave the db state in place so that
12008  ** client code can "push" SQL into it after this call returns. */
12009  free(azCmd);
12010  set_table_name(&data, 0);
12011  if( data.db ){
12012    session_close_all(&data, -1);
12013    close_db(data.db);
12014  }
12015  for(i=0; i<ArraySize(data.aAuxDb); i++){
12016    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12017    if( data.aAuxDb[i].db ){
12018      session_close_all(&data, i);
12019      close_db(data.aAuxDb[i].db);
12020    }
12021  }
12022  find_home_dir(1);
12023  output_reset(&data);
12024  data.doXdgOpen = 0;
12025  clearTempFile(&data);
12026#if !SQLITE_SHELL_IS_UTF8
12027  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12028  free(argvToFree);
12029#endif
12030  free(data.colWidth);
12031  free(data.zNonce);
12032  /* Clear the global data structure so that valgrind will detect memory
12033  ** leaks */
12034  memset(&data, 0, sizeof(data));
12035#ifdef SQLITE_DEBUG
12036  if( sqlite3_memory_used()>mem_main_enter ){
12037    utf8_printf(stderr, "Memory leaked: %u bytes\n",
12038                (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12039  }
12040#endif
12041#endif /* !SQLITE_SHELL_FIDDLE */
12042  return rc;
12043}
12044
12045
12046#ifdef SQLITE_SHELL_FIDDLE
12047/* Only for emcc experimentation purposes. */
12048int fiddle_experiment(int a,int b){
12049  return a + b;
12050}
12051
12052/*
12053** Returns a pointer to the current DB handle.
12054*/
12055sqlite3 * fiddle_db_handle(){
12056  return globalDb;
12057}
12058
12059/*
12060** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
12061** "main" is assumed. Returns 0 if no db with the given name is
12062** open.
12063*/
12064sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
12065  sqlite3_vfs * pVfs = 0;
12066  if(globalDb){
12067    sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
12068                         SQLITE_FCNTL_VFS_POINTER, &pVfs);
12069  }
12070  return pVfs;
12071}
12072
12073/* Only for emcc experimentation purposes. */
12074sqlite3 * fiddle_db_arg(sqlite3 *arg){
12075    printf("fiddle_db_arg(%p)\n", (const void*)arg);
12076    return arg;
12077}
12078
12079/*
12080** Intended to be called via a SharedWorker() while a separate
12081** SharedWorker() (which manages the wasm module) is performing work
12082** which should be interrupted. Unfortunately, SharedWorker is not
12083** portable enough to make real use of.
12084*/
12085void fiddle_interrupt(void){
12086  if( globalDb ) sqlite3_interrupt(globalDb);
12087}
12088
12089/*
12090** Returns the filename of the given db name, assuming "main" if
12091** zDbName is NULL. Returns NULL if globalDb is not opened.
12092*/
12093const char * fiddle_db_filename(const char * zDbName){
12094    return globalDb
12095      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12096      : NULL;
12097}
12098
12099/*
12100** Completely wipes out the contents of the currently-opened database
12101** but leaves its storage intact for reuse.
12102*/
12103void fiddle_reset_db(void){
12104  if( globalDb ){
12105    int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
12106    if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
12107    sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
12108  }
12109}
12110
12111/*
12112** Uses the current database's VFS xRead to stream the db file's
12113** contents out to the given callback. The callback gets a single
12114** chunk of size n (its 2nd argument) on each call and must return 0
12115** on success, non-0 on error. This function returns 0 on success,
12116** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
12117** code from the callback. Note that this is not thread-friendly: it
12118** expects that it will be the only thread reading the db file and
12119** takes no measures to ensure that is the case.
12120*/
12121int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
12122  sqlite3_int64 nSize = 0;
12123  sqlite3_int64 nPos = 0;
12124  sqlite3_file * pFile = 0;
12125  unsigned char buf[1024 * 8];
12126  int nBuf = (int)sizeof(buf);
12127  int rc = shellState.db
12128    ? sqlite3_file_control(shellState.db, "main",
12129                           SQLITE_FCNTL_FILE_POINTER, &pFile)
12130    : SQLITE_NOTFOUND;
12131  if( rc ) return rc;
12132  rc = pFile->pMethods->xFileSize(pFile, &nSize);
12133  if( rc ) return rc;
12134  if(nSize % nBuf){
12135    /* DB size is not an even multiple of the buffer size. Reduce
12136    ** buffer size so that we do not unduly inflate the db size when
12137    ** exporting. */
12138    if(0 == nSize % 4096) nBuf = 4096;
12139    else if(0 == nSize % 2048) nBuf = 2048;
12140    else if(0 == nSize % 1024) nBuf = 1024;
12141    else nBuf = 512;
12142  }
12143  for( ; 0==rc && nPos<nSize; nPos += nBuf ){
12144    rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
12145    if(SQLITE_IOERR_SHORT_READ == rc){
12146      rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
12147    }
12148    if( 0==rc ) rc = xCallback(buf, nBuf);
12149  }
12150  return rc;
12151}
12152
12153/*
12154** Trivial exportable function for emscripten. It processes zSql as if
12155** it were input to the sqlite3 shell and redirects all output to the
12156** wasm binding. fiddle_main() must have been called before this
12157** is called, or results are undefined.
12158*/
12159void fiddle_exec(const char * zSql){
12160  if(zSql && *zSql){
12161    if('.'==*zSql) puts(zSql);
12162    shellState.wasm.zInput = zSql;
12163    shellState.wasm.zPos = zSql;
12164    process_input(&shellState);
12165    shellState.wasm.zInput = shellState.wasm.zPos = 0;
12166  }
12167}
12168#endif /* SQLITE_SHELL_FIDDLE */
12169