xref: /sqlite-3.40.0/src/shell.c.in (revision 7d23d157)
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#include <stdlib.h>
89#include <string.h>
90#include <stdio.h>
91#include <assert.h>
92#include "sqlite3.h"
93typedef sqlite3_int64 i64;
94typedef sqlite3_uint64 u64;
95typedef unsigned char u8;
96#if SQLITE_USER_AUTHENTICATION
97# include "sqlite3userauth.h"
98#endif
99#include <ctype.h>
100#include <stdarg.h>
101
102#if !defined(_WIN32) && !defined(WIN32)
103# include <signal.h>
104# if !defined(__RTP__) && !defined(_WRS_KERNEL)
105#  include <pwd.h>
106# endif
107#endif
108#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
109# include <unistd.h>
110# include <dirent.h>
111# define GETPID getpid
112# if defined(__MINGW32__)
113#  define DIRENT dirent
114#  ifndef S_ISLNK
115#   define S_ISLNK(mode) (0)
116#  endif
117# endif
118#else
119# define GETPID (int)GetCurrentProcessId
120#endif
121#include <sys/types.h>
122#include <sys/stat.h>
123
124#if HAVE_READLINE
125# include <readline/readline.h>
126# include <readline/history.h>
127#endif
128
129#if HAVE_EDITLINE
130# include <editline/readline.h>
131#endif
132
133#if HAVE_EDITLINE || HAVE_READLINE
134
135# define shell_add_history(X) add_history(X)
136# define shell_read_history(X) read_history(X)
137# define shell_write_history(X) write_history(X)
138# define shell_stifle_history(X) stifle_history(X)
139# define shell_readline(X) readline(X)
140
141#elif HAVE_LINENOISE
142
143# include "linenoise.h"
144# define shell_add_history(X) linenoiseHistoryAdd(X)
145# define shell_read_history(X) linenoiseHistoryLoad(X)
146# define shell_write_history(X) linenoiseHistorySave(X)
147# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
148# define shell_readline(X) linenoise(X)
149
150#else
151
152# define shell_read_history(X)
153# define shell_write_history(X)
154# define shell_stifle_history(X)
155
156# define SHELL_USE_LOCAL_GETLINE 1
157#endif
158
159
160#if defined(_WIN32) || defined(WIN32)
161# if SQLITE_OS_WINRT
162#  define SQLITE_OMIT_POPEN 1
163# else
164#  include <io.h>
165#  include <fcntl.h>
166#  define isatty(h) _isatty(h)
167#  ifndef access
168#   define access(f,m) _access((f),(m))
169#  endif
170#  ifndef unlink
171#   define unlink _unlink
172#  endif
173#  ifndef strdup
174#   define strdup _strdup
175#  endif
176#  undef popen
177#  define popen _popen
178#  undef pclose
179#  define pclose _pclose
180# endif
181#else
182 /* Make sure isatty() has a prototype. */
183 extern int isatty(int);
184
185# if !defined(__RTP__) && !defined(_WRS_KERNEL)
186  /* popen and pclose are not C89 functions and so are
187  ** sometimes omitted from the <stdio.h> header */
188   extern FILE *popen(const char*,const char*);
189   extern int pclose(FILE*);
190# else
191#  define SQLITE_OMIT_POPEN 1
192# endif
193#endif
194
195#if defined(_WIN32_WCE)
196/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
197 * thus we always assume that we have a console. That can be
198 * overridden with the -batch command line option.
199 */
200#define isatty(x) 1
201#endif
202
203/* ctype macros that work with signed characters */
204#define IsSpace(X)  isspace((unsigned char)X)
205#define IsDigit(X)  isdigit((unsigned char)X)
206#define ToLower(X)  (char)tolower((unsigned char)X)
207
208#if defined(_WIN32) || defined(WIN32)
209#if SQLITE_OS_WINRT
210#include <intrin.h>
211#endif
212#include <windows.h>
213
214/* string conversion routines only needed on Win32 */
215extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
216extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
217extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
218extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
219#endif
220
221/* On Windows, we normally run with output mode of TEXT so that \n characters
222** are automatically translated into \r\n.  However, this behavior needs
223** to be disabled in some cases (ex: when generating CSV output and when
224** rendering quoted strings that contain \n characters).  The following
225** routines take care of that.
226*/
227#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
228static void setBinaryMode(FILE *file, int isOutput){
229  if( isOutput ) fflush(file);
230  _setmode(_fileno(file), _O_BINARY);
231}
232static void setTextMode(FILE *file, int isOutput){
233  if( isOutput ) fflush(file);
234  _setmode(_fileno(file), _O_TEXT);
235}
236#else
237# define setBinaryMode(X,Y)
238# define setTextMode(X,Y)
239#endif
240
241/* True if the timer is enabled */
242static int enableTimer = 0;
243
244/* Return the current wall-clock time */
245static sqlite3_int64 timeOfDay(void){
246  static sqlite3_vfs *clockVfs = 0;
247  sqlite3_int64 t;
248  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
249  if( clockVfs==0 ) return 0;  /* Never actually happens */
250  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
251    clockVfs->xCurrentTimeInt64(clockVfs, &t);
252  }else{
253    double r;
254    clockVfs->xCurrentTime(clockVfs, &r);
255    t = (sqlite3_int64)(r*86400000.0);
256  }
257  return t;
258}
259
260#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
261#include <sys/time.h>
262#include <sys/resource.h>
263
264/* VxWorks does not support getrusage() as far as we can determine */
265#if defined(_WRS_KERNEL) || defined(__RTP__)
266struct rusage {
267  struct timeval ru_utime; /* user CPU time used */
268  struct timeval ru_stime; /* system CPU time used */
269};
270#define getrusage(A,B) memset(B,0,sizeof(*B))
271#endif
272
273/* Saved resource information for the beginning of an operation */
274static struct rusage sBegin;  /* CPU time at start */
275static sqlite3_int64 iBegin;  /* Wall-clock time at start */
276
277/*
278** Begin timing an operation
279*/
280static void beginTimer(void){
281  if( enableTimer ){
282    getrusage(RUSAGE_SELF, &sBegin);
283    iBegin = timeOfDay();
284  }
285}
286
287/* Return the difference of two time_structs in seconds */
288static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
289  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
290         (double)(pEnd->tv_sec - pStart->tv_sec);
291}
292
293/*
294** Print the timing results.
295*/
296static void endTimer(void){
297  if( enableTimer ){
298    sqlite3_int64 iEnd = timeOfDay();
299    struct rusage sEnd;
300    getrusage(RUSAGE_SELF, &sEnd);
301    printf("Run Time: real %.3f user %f sys %f\n",
302       (iEnd - iBegin)*0.001,
303       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
304       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
305  }
306}
307
308#define BEGIN_TIMER beginTimer()
309#define END_TIMER endTimer()
310#define HAS_TIMER 1
311
312#elif (defined(_WIN32) || defined(WIN32))
313
314/* Saved resource information for the beginning of an operation */
315static HANDLE hProcess;
316static FILETIME ftKernelBegin;
317static FILETIME ftUserBegin;
318static sqlite3_int64 ftWallBegin;
319typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
320                                    LPFILETIME, LPFILETIME);
321static GETPROCTIMES getProcessTimesAddr = NULL;
322
323/*
324** Check to see if we have timer support.  Return 1 if necessary
325** support found (or found previously).
326*/
327static int hasTimer(void){
328  if( getProcessTimesAddr ){
329    return 1;
330  } else {
331#if !SQLITE_OS_WINRT
332    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
333    ** versions. See if the version we are running on has it, and if it
334    ** does, save off a pointer to it and the current process handle.
335    */
336    hProcess = GetCurrentProcess();
337    if( hProcess ){
338      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
339      if( NULL != hinstLib ){
340        getProcessTimesAddr =
341            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
342        if( NULL != getProcessTimesAddr ){
343          return 1;
344        }
345        FreeLibrary(hinstLib);
346      }
347    }
348#endif
349  }
350  return 0;
351}
352
353/*
354** Begin timing an operation
355*/
356static void beginTimer(void){
357  if( enableTimer && getProcessTimesAddr ){
358    FILETIME ftCreation, ftExit;
359    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
360                        &ftKernelBegin,&ftUserBegin);
361    ftWallBegin = timeOfDay();
362  }
363}
364
365/* Return the difference of two FILETIME structs in seconds */
366static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
367  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
368  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
369  return (double) ((i64End - i64Start) / 10000000.0);
370}
371
372/*
373** Print the timing results.
374*/
375static void endTimer(void){
376  if( enableTimer && getProcessTimesAddr){
377    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
378    sqlite3_int64 ftWallEnd = timeOfDay();
379    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
380    printf("Run Time: real %.3f user %f sys %f\n",
381       (ftWallEnd - ftWallBegin)*0.001,
382       timeDiff(&ftUserBegin, &ftUserEnd),
383       timeDiff(&ftKernelBegin, &ftKernelEnd));
384  }
385}
386
387#define BEGIN_TIMER beginTimer()
388#define END_TIMER endTimer()
389#define HAS_TIMER hasTimer()
390
391#else
392#define BEGIN_TIMER
393#define END_TIMER
394#define HAS_TIMER 0
395#endif
396
397/*
398** Used to prevent warnings about unused parameters
399*/
400#define UNUSED_PARAMETER(x) (void)(x)
401
402/*
403** Number of elements in an array
404*/
405#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
406
407/*
408** If the following flag is set, then command execution stops
409** at an error if we are not interactive.
410*/
411static int bail_on_error = 0;
412
413/*
414** Threat stdin as an interactive input if the following variable
415** is true.  Otherwise, assume stdin is connected to a file or pipe.
416*/
417static int stdin_is_interactive = 1;
418
419/*
420** On Windows systems we have to know if standard output is a console
421** in order to translate UTF-8 into MBCS.  The following variable is
422** true if translation is required.
423*/
424static int stdout_is_console = 1;
425
426/*
427** The following is the open SQLite database.  We make a pointer
428** to this database a static variable so that it can be accessed
429** by the SIGINT handler to interrupt database processing.
430*/
431static sqlite3 *globalDb = 0;
432
433/*
434** True if an interrupt (Control-C) has been received.
435*/
436static volatile int seenInterrupt = 0;
437
438/*
439** This is the name of our program. It is set in main(), used
440** in a number of other places, mostly for error messages.
441*/
442static char *Argv0;
443
444/*
445** Prompt strings. Initialized in main. Settable with
446**   .prompt main continue
447*/
448static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
449static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
450
451/*
452** Render output like fprintf().  Except, if the output is going to the
453** console and if this is running on a Windows machine, translate the
454** output from UTF-8 into MBCS.
455*/
456#if defined(_WIN32) || defined(WIN32)
457void utf8_printf(FILE *out, const char *zFormat, ...){
458  va_list ap;
459  va_start(ap, zFormat);
460  if( stdout_is_console && (out==stdout || out==stderr) ){
461    char *z1 = sqlite3_vmprintf(zFormat, ap);
462    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
463    sqlite3_free(z1);
464    fputs(z2, out);
465    sqlite3_free(z2);
466  }else{
467    vfprintf(out, zFormat, ap);
468  }
469  va_end(ap);
470}
471#elif !defined(utf8_printf)
472# define utf8_printf fprintf
473#endif
474
475/*
476** Render output like fprintf().  This should not be used on anything that
477** includes string formatting (e.g. "%s").
478*/
479#if !defined(raw_printf)
480# define raw_printf fprintf
481#endif
482
483/* Indicate out-of-memory and exit. */
484static void shell_out_of_memory(void){
485  raw_printf(stderr,"Error: out of memory\n");
486  exit(1);
487}
488
489/* Check a pointer to see if it is NULL.  If it is NULL, exit with an
490** out-of-memory error.
491*/
492static void shell_check_oom(void *p){
493  if( p==0 ) shell_out_of_memory();
494}
495
496/*
497** Write I/O traces to the following stream.
498*/
499#ifdef SQLITE_ENABLE_IOTRACE
500static FILE *iotrace = 0;
501#endif
502
503/*
504** This routine works like printf in that its first argument is a
505** format string and subsequent arguments are values to be substituted
506** in place of % fields.  The result of formatting this string
507** is written to iotrace.
508*/
509#ifdef SQLITE_ENABLE_IOTRACE
510static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
511  va_list ap;
512  char *z;
513  if( iotrace==0 ) return;
514  va_start(ap, zFormat);
515  z = sqlite3_vmprintf(zFormat, ap);
516  va_end(ap);
517  utf8_printf(iotrace, "%s", z);
518  sqlite3_free(z);
519}
520#endif
521
522/*
523** Output string zUtf to stream pOut as w characters.  If w is negative,
524** then right-justify the text.  W is the width in UTF-8 characters, not
525** in bytes.  This is different from the %*.*s specification in printf
526** since with %*.*s the width is measured in bytes, not characters.
527*/
528static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
529  int i;
530  int n;
531  int aw = w<0 ? -w : w;
532  for(i=n=0; zUtf[i]; i++){
533    if( (zUtf[i]&0xc0)!=0x80 ){
534      n++;
535      if( n==aw ){
536        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
537        break;
538      }
539    }
540  }
541  if( n>=aw ){
542    utf8_printf(pOut, "%.*s", i, zUtf);
543  }else if( w<0 ){
544    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
545  }else{
546    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
547  }
548}
549
550
551/*
552** Determines if a string is a number of not.
553*/
554static int isNumber(const char *z, int *realnum){
555  if( *z=='-' || *z=='+' ) z++;
556  if( !IsDigit(*z) ){
557    return 0;
558  }
559  z++;
560  if( realnum ) *realnum = 0;
561  while( IsDigit(*z) ){ z++; }
562  if( *z=='.' ){
563    z++;
564    if( !IsDigit(*z) ) return 0;
565    while( IsDigit(*z) ){ z++; }
566    if( realnum ) *realnum = 1;
567  }
568  if( *z=='e' || *z=='E' ){
569    z++;
570    if( *z=='+' || *z=='-' ) z++;
571    if( !IsDigit(*z) ) return 0;
572    while( IsDigit(*z) ){ z++; }
573    if( realnum ) *realnum = 1;
574  }
575  return *z==0;
576}
577
578/*
579** Compute a string length that is limited to what can be stored in
580** lower 30 bits of a 32-bit signed integer.
581*/
582static int strlen30(const char *z){
583  const char *z2 = z;
584  while( *z2 ){ z2++; }
585  return 0x3fffffff & (int)(z2 - z);
586}
587
588/*
589** Return the length of a string in characters.  Multibyte UTF8 characters
590** count as a single character.
591*/
592static int strlenChar(const char *z){
593  int n = 0;
594  while( *z ){
595    if( (0xc0&*(z++))!=0x80 ) n++;
596  }
597  return n;
598}
599
600/*
601** Return open FILE * if zFile exists, can be opened for read
602** and is an ordinary file or a character stream source.
603** Otherwise return 0.
604*/
605static FILE * openChrSource(const char *zFile){
606#ifdef _WIN32
607  struct _stat x = {0};
608# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
609  /* On Windows, open first, then check the stream nature. This order
610  ** is necessary because _stat() and sibs, when checking a named pipe,
611  ** effectively break the pipe as its supplier sees it. */
612  FILE *rv = fopen(zFile, "rb");
613  if( rv==0 ) return 0;
614  if( _fstat(_fileno(rv), &x) != 0
615      || !STAT_CHR_SRC(x.st_mode)){
616    fclose(rv);
617    rv = 0;
618  }
619  return rv;
620#else
621  struct stat x = {0};
622  int rc = stat(zFile, &x);
623# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
624  if( rc!=0 ) return 0;
625  if( STAT_CHR_SRC(x.st_mode) ){
626    return fopen(zFile, "rb");
627  }else{
628    return 0;
629  }
630#endif
631#undef STAT_CHR_SRC
632}
633
634/*
635** This routine reads a line of text from FILE in, stores
636** the text in memory obtained from malloc() and returns a pointer
637** to the text.  NULL is returned at end of file, or if malloc()
638** fails.
639**
640** If zLine is not NULL then it is a malloced buffer returned from
641** a previous call to this routine that may be reused.
642*/
643static char *local_getline(char *zLine, FILE *in){
644  int nLine = zLine==0 ? 0 : 100;
645  int n = 0;
646
647  while( 1 ){
648    if( n+100>nLine ){
649      nLine = nLine*2 + 100;
650      zLine = realloc(zLine, nLine);
651      shell_check_oom(zLine);
652    }
653    if( fgets(&zLine[n], nLine - n, in)==0 ){
654      if( n==0 ){
655        free(zLine);
656        return 0;
657      }
658      zLine[n] = 0;
659      break;
660    }
661    while( zLine[n] ) n++;
662    if( n>0 && zLine[n-1]=='\n' ){
663      n--;
664      if( n>0 && zLine[n-1]=='\r' ) n--;
665      zLine[n] = 0;
666      break;
667    }
668  }
669#if defined(_WIN32) || defined(WIN32)
670  /* For interactive input on Windows systems, translate the
671  ** multi-byte characterset characters into UTF-8. */
672  if( stdin_is_interactive && in==stdin ){
673    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
674    if( zTrans ){
675      i64 nTrans = strlen(zTrans)+1;
676      if( nTrans>nLine ){
677        zLine = realloc(zLine, nTrans);
678        shell_check_oom(zLine);
679      }
680      memcpy(zLine, zTrans, nTrans);
681      sqlite3_free(zTrans);
682    }
683  }
684#endif /* defined(_WIN32) || defined(WIN32) */
685  return zLine;
686}
687
688/*
689** Retrieve a single line of input text.
690**
691** If in==0 then read from standard input and prompt before each line.
692** If isContinuation is true, then a continuation prompt is appropriate.
693** If isContinuation is zero, then the main prompt should be used.
694**
695** If zPrior is not NULL then it is a buffer from a prior call to this
696** routine that can be reused.
697**
698** The result is stored in space obtained from malloc() and must either
699** be freed by the caller or else passed back into this routine via the
700** zPrior argument for reuse.
701*/
702#ifndef SQLITE_SHELL_FIDDLE
703static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
704  char *zPrompt;
705  char *zResult;
706  if( in!=0 ){
707    zResult = local_getline(zPrior, in);
708  }else{
709    zPrompt = isContinuation ? continuePrompt : mainPrompt;
710#if SHELL_USE_LOCAL_GETLINE
711    printf("%s", zPrompt);
712    fflush(stdout);
713    zResult = local_getline(zPrior, stdin);
714#else
715    free(zPrior);
716    zResult = shell_readline(zPrompt);
717    if( zResult && *zResult ) shell_add_history(zResult);
718#endif
719  }
720  return zResult;
721}
722#endif /* !SQLITE_SHELL_FIDDLE */
723
724/*
725** Return the value of a hexadecimal digit.  Return -1 if the input
726** is not a hex digit.
727*/
728static int hexDigitValue(char c){
729  if( c>='0' && c<='9' ) return c - '0';
730  if( c>='a' && c<='f' ) return c - 'a' + 10;
731  if( c>='A' && c<='F' ) return c - 'A' + 10;
732  return -1;
733}
734
735/*
736** Interpret zArg as an integer value, possibly with suffixes.
737*/
738static sqlite3_int64 integerValue(const char *zArg){
739  sqlite3_int64 v = 0;
740  static const struct { char *zSuffix; int iMult; } aMult[] = {
741    { "KiB", 1024 },
742    { "MiB", 1024*1024 },
743    { "GiB", 1024*1024*1024 },
744    { "KB",  1000 },
745    { "MB",  1000000 },
746    { "GB",  1000000000 },
747    { "K",   1000 },
748    { "M",   1000000 },
749    { "G",   1000000000 },
750  };
751  int i;
752  int isNeg = 0;
753  if( zArg[0]=='-' ){
754    isNeg = 1;
755    zArg++;
756  }else if( zArg[0]=='+' ){
757    zArg++;
758  }
759  if( zArg[0]=='0' && zArg[1]=='x' ){
760    int x;
761    zArg += 2;
762    while( (x = hexDigitValue(zArg[0]))>=0 ){
763      v = (v<<4) + x;
764      zArg++;
765    }
766  }else{
767    while( IsDigit(zArg[0]) ){
768      v = v*10 + zArg[0] - '0';
769      zArg++;
770    }
771  }
772  for(i=0; i<ArraySize(aMult); i++){
773    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
774      v *= aMult[i].iMult;
775      break;
776    }
777  }
778  return isNeg? -v : v;
779}
780
781/*
782** A variable length string to which one can append text.
783*/
784typedef struct ShellText ShellText;
785struct ShellText {
786  char *z;
787  int n;
788  int nAlloc;
789};
790
791/*
792** Initialize and destroy a ShellText object
793*/
794static void initText(ShellText *p){
795  memset(p, 0, sizeof(*p));
796}
797static void freeText(ShellText *p){
798  free(p->z);
799  initText(p);
800}
801
802/* zIn is either a pointer to a NULL-terminated string in memory obtained
803** from malloc(), or a NULL pointer. The string pointed to by zAppend is
804** added to zIn, and the result returned in memory obtained from malloc().
805** zIn, if it was not NULL, is freed.
806**
807** If the third argument, quote, is not '\0', then it is used as a
808** quote character for zAppend.
809*/
810static void appendText(ShellText *p, const char *zAppend, char quote){
811  i64 len;
812  i64 i;
813  i64 nAppend = strlen30(zAppend);
814
815  len = nAppend+p->n+1;
816  if( quote ){
817    len += 2;
818    for(i=0; i<nAppend; i++){
819      if( zAppend[i]==quote ) len++;
820    }
821  }
822
823  if( p->z==0 || p->n+len>=p->nAlloc ){
824    p->nAlloc = p->nAlloc*2 + len + 20;
825    p->z = realloc(p->z, p->nAlloc);
826    shell_check_oom(p->z);
827  }
828
829  if( quote ){
830    char *zCsr = p->z+p->n;
831    *zCsr++ = quote;
832    for(i=0; i<nAppend; i++){
833      *zCsr++ = zAppend[i];
834      if( zAppend[i]==quote ) *zCsr++ = quote;
835    }
836    *zCsr++ = quote;
837    p->n = (int)(zCsr - p->z);
838    *zCsr = '\0';
839  }else{
840    memcpy(p->z+p->n, zAppend, nAppend);
841    p->n += nAppend;
842    p->z[p->n] = '\0';
843  }
844}
845
846/*
847** Attempt to determine if identifier zName needs to be quoted, either
848** because it contains non-alphanumeric characters, or because it is an
849** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
850** that quoting is required.
851**
852** Return '"' if quoting is required.  Return 0 if no quoting is required.
853*/
854static char quoteChar(const char *zName){
855  int i;
856  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
857  for(i=0; zName[i]; i++){
858    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
859  }
860  return sqlite3_keyword_check(zName, i) ? '"' : 0;
861}
862
863/*
864** Construct a fake object name and column list to describe the structure
865** of the view, virtual table, or table valued function zSchema.zName.
866*/
867static char *shellFakeSchema(
868  sqlite3 *db,            /* The database connection containing the vtab */
869  const char *zSchema,    /* Schema of the database holding the vtab */
870  const char *zName       /* The name of the virtual table */
871){
872  sqlite3_stmt *pStmt = 0;
873  char *zSql;
874  ShellText s;
875  char cQuote;
876  char *zDiv = "(";
877  int nRow = 0;
878
879  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
880                         zSchema ? zSchema : "main", zName);
881  shell_check_oom(zSql);
882  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
883  sqlite3_free(zSql);
884  initText(&s);
885  if( zSchema ){
886    cQuote = quoteChar(zSchema);
887    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
888    appendText(&s, zSchema, cQuote);
889    appendText(&s, ".", 0);
890  }
891  cQuote = quoteChar(zName);
892  appendText(&s, zName, cQuote);
893  while( sqlite3_step(pStmt)==SQLITE_ROW ){
894    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
895    nRow++;
896    appendText(&s, zDiv, 0);
897    zDiv = ",";
898    if( zCol==0 ) zCol = "";
899    cQuote = quoteChar(zCol);
900    appendText(&s, zCol, cQuote);
901  }
902  appendText(&s, ")", 0);
903  sqlite3_finalize(pStmt);
904  if( nRow==0 ){
905    freeText(&s);
906    s.z = 0;
907  }
908  return s.z;
909}
910
911/*
912** SQL function:  shell_module_schema(X)
913**
914** Return a fake schema for the table-valued function or eponymous virtual
915** table X.
916*/
917static void shellModuleSchema(
918  sqlite3_context *pCtx,
919  int nVal,
920  sqlite3_value **apVal
921){
922  const char *zName;
923  char *zFake;
924  UNUSED_PARAMETER(nVal);
925  zName = (const char*)sqlite3_value_text(apVal[0]);
926  zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
927  if( zFake ){
928    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
929                        -1, sqlite3_free);
930    free(zFake);
931  }
932}
933
934/*
935** SQL function:  shell_add_schema(S,X)
936**
937** Add the schema name X to the CREATE statement in S and return the result.
938** Examples:
939**
940**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
941**
942** Also works on
943**
944**    CREATE INDEX
945**    CREATE UNIQUE INDEX
946**    CREATE VIEW
947**    CREATE TRIGGER
948**    CREATE VIRTUAL TABLE
949**
950** This UDF is used by the .schema command to insert the schema name of
951** attached databases into the middle of the sqlite_schema.sql field.
952*/
953static void shellAddSchemaName(
954  sqlite3_context *pCtx,
955  int nVal,
956  sqlite3_value **apVal
957){
958  static const char *aPrefix[] = {
959     "TABLE",
960     "INDEX",
961     "UNIQUE INDEX",
962     "VIEW",
963     "TRIGGER",
964     "VIRTUAL TABLE"
965  };
966  int i = 0;
967  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
968  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
969  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
970  sqlite3 *db = sqlite3_context_db_handle(pCtx);
971  UNUSED_PARAMETER(nVal);
972  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
973    for(i=0; i<ArraySize(aPrefix); i++){
974      int n = strlen30(aPrefix[i]);
975      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
976        char *z = 0;
977        char *zFake = 0;
978        if( zSchema ){
979          char cQuote = quoteChar(zSchema);
980          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
981            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
982          }else{
983            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
984          }
985        }
986        if( zName
987         && aPrefix[i][0]=='V'
988         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
989        ){
990          if( z==0 ){
991            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
992          }else{
993            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
994          }
995          free(zFake);
996        }
997        if( z ){
998          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
999          return;
1000        }
1001      }
1002    }
1003  }
1004  sqlite3_result_value(pCtx, apVal[0]);
1005}
1006
1007/*
1008** The source code for several run-time loadable extensions is inserted
1009** below by the ../tool/mkshellc.tcl script.  Before processing that included
1010** code, we need to override some macros to make the included program code
1011** work here in the middle of this regular program.
1012*/
1013#define SQLITE_EXTENSION_INIT1
1014#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1015
1016#if defined(_WIN32) && defined(_MSC_VER)
1017INCLUDE test_windirent.h
1018INCLUDE test_windirent.c
1019#define dirent DIRENT
1020#endif
1021INCLUDE ../ext/misc/memtrace.c
1022INCLUDE ../ext/misc/shathree.c
1023INCLUDE ../ext/misc/uint.c
1024INCLUDE ../ext/misc/decimal.c
1025INCLUDE ../ext/misc/ieee754.c
1026INCLUDE ../ext/misc/series.c
1027INCLUDE ../ext/misc/regexp.c
1028#ifndef SQLITE_SHELL_FIDDLE
1029INCLUDE ../ext/misc/fileio.c
1030INCLUDE ../ext/misc/completion.c
1031INCLUDE ../ext/misc/appendvfs.c
1032#endif
1033#ifdef SQLITE_HAVE_ZLIB
1034INCLUDE ../ext/misc/zipfile.c
1035INCLUDE ../ext/misc/sqlar.c
1036#endif
1037INCLUDE ../ext/expert/sqlite3expert.h
1038INCLUDE ../ext/expert/sqlite3expert.c
1039
1040#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1041INCLUDE ../ext/misc/dbdata.c
1042#endif
1043
1044#if defined(SQLITE_ENABLE_SESSION)
1045/*
1046** State information for a single open session
1047*/
1048typedef struct OpenSession OpenSession;
1049struct OpenSession {
1050  char *zName;             /* Symbolic name for this session */
1051  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1052  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1053  sqlite3_session *p;      /* The open session */
1054};
1055#endif
1056
1057typedef struct ExpertInfo ExpertInfo;
1058struct ExpertInfo {
1059  sqlite3expert *pExpert;
1060  int bVerbose;
1061};
1062
1063/* A single line in the EQP output */
1064typedef struct EQPGraphRow EQPGraphRow;
1065struct EQPGraphRow {
1066  int iEqpId;           /* ID for this row */
1067  int iParentId;        /* ID of the parent row */
1068  EQPGraphRow *pNext;   /* Next row in sequence */
1069  char zText[1];        /* Text to display for this row */
1070};
1071
1072/* All EQP output is collected into an instance of the following */
1073typedef struct EQPGraph EQPGraph;
1074struct EQPGraph {
1075  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1076  EQPGraphRow *pLast;   /* Last element of the pRow list */
1077  char zPrefix[100];    /* Graph prefix */
1078};
1079
1080/* Parameters affecting columnar mode result display (defaulting together) */
1081typedef struct ColModeOpts {
1082  int iWrap;            /* In columnar modes, wrap lines reaching this limit */
1083  u8 bQuote;            /* Quote results for .mode box and table */
1084  u8 bWordWrap;         /* In columnar modes, wrap at word boundaries  */
1085} ColModeOpts;
1086#define ColModeOpts_default { 60, 0, 0 }
1087#define ColModeOpts_default_qbox { 60, 1, 0 }
1088
1089/*
1090** State information about the database connection is contained in an
1091** instance of the following structure.
1092*/
1093typedef struct ShellState ShellState;
1094struct ShellState {
1095  sqlite3 *db;           /* The database */
1096  u8 autoExplain;        /* Automatically turn on .explain mode */
1097  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1098  u8 autoEQPtest;        /* autoEQP is in test mode */
1099  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1100  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1101  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1102  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1103  u8 nEqpLevel;          /* Depth of the EQP output graph */
1104  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1105  u8 bSafeMode;          /* True to prohibit unsafe operations */
1106  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
1107  ColModeOpts cmOpts;    /* Option values affecting columnar mode output */
1108  unsigned statsOn;      /* True to display memory stats before each finalize */
1109  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1110  int inputNesting;      /* Track nesting level of .read and other redirects */
1111  int outCount;          /* Revert to stdout when reaching zero */
1112  int cnt;               /* Number of records displayed so far */
1113  int lineno;            /* Line number of last line read from in */
1114  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1115  FILE *in;              /* Read commands from this stream */
1116  FILE *out;             /* Write results here */
1117  FILE *traceOut;        /* Output for sqlite3_trace() */
1118  int nErr;              /* Number of errors seen */
1119  int mode;              /* An output mode setting */
1120  int modePrior;         /* Saved mode */
1121  int cMode;             /* temporary output mode for the current query */
1122  int normalMode;        /* Output mode before ".explain on" */
1123  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1124  int showHeader;        /* True to show column names in List or Column mode */
1125  int nCheck;            /* Number of ".check" commands run */
1126  unsigned nProgress;    /* Number of progress callbacks encountered */
1127  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1128  unsigned flgProgress;  /* Flags for the progress callback */
1129  unsigned shellFlgs;    /* Various flags */
1130  unsigned priorShFlgs;  /* Saved copy of flags */
1131  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1132  char *zDestTable;      /* Name of destination table when MODE_Insert */
1133  char *zTempFile;       /* Temporary file that might need deleting */
1134  char zTestcase[30];    /* Name of current test case */
1135  char colSeparator[20]; /* Column separator character for several modes */
1136  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1137  char colSepPrior[20];  /* Saved column separator */
1138  char rowSepPrior[20];  /* Saved row separator */
1139  int *colWidth;         /* Requested width of each column in columnar modes */
1140  int *actualWidth;      /* Actual width of each column */
1141  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1142  char nullValue[20];    /* The text to print when a NULL comes back from
1143                         ** the database */
1144  char outfile[FILENAME_MAX]; /* Filename for *out */
1145  sqlite3_stmt *pStmt;   /* Current statement if any. */
1146  FILE *pLog;            /* Write log output here */
1147  struct AuxDb {         /* Storage space for auxiliary database connections */
1148    sqlite3 *db;               /* Connection pointer */
1149    const char *zDbFilename;   /* Filename used to open the connection */
1150    char *zFreeOnClose;        /* Free this memory allocation on close */
1151#if defined(SQLITE_ENABLE_SESSION)
1152    int nSession;              /* Number of active sessions */
1153    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
1154#endif
1155  } aAuxDb[5],           /* Array of all database connections */
1156    *pAuxDb;             /* Currently active database connection */
1157  int *aiIndent;         /* Array of indents used in MODE_Explain */
1158  int nIndent;           /* Size of array aiIndent[] */
1159  int iIndent;           /* Index of current op in aiIndent[] */
1160  char *zNonce;          /* Nonce for temporary safe-mode excapes */
1161  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1162  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
1163#ifdef SQLITE_SHELL_FIDDLE
1164  struct {
1165    const char * zInput; /* Input string from wasm/JS proxy */
1166    const char * zPos;   /* Cursor pos into zInput */
1167  } wasm;
1168#endif
1169};
1170
1171#ifdef SQLITE_SHELL_FIDDLE
1172static ShellState shellState;
1173#endif
1174
1175
1176/* Allowed values for ShellState.autoEQP
1177*/
1178#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1179#define AUTOEQP_on       1           /* Automatic EQP is on */
1180#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1181#define AUTOEQP_full     3           /* Show full EXPLAIN */
1182
1183/* Allowed values for ShellState.openMode
1184*/
1185#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1186#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1187#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1188#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1189#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1190#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1191#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1192
1193/* Allowed values for ShellState.eTraceType
1194*/
1195#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1196#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1197#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1198
1199/* Bits in the ShellState.flgProgress variable */
1200#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1201#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1202                                   ** callback limit is reached, and for each
1203                                   ** top-level SQL statement */
1204#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1205
1206/*
1207** These are the allowed shellFlgs values
1208*/
1209#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1210#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1211#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1212#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1213#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1214#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1215#define SHFLG_Echo           0x00000040 /* .echo on/off, or --echo setting */
1216#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1217#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1218#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1219
1220/*
1221** Macros for testing and setting shellFlgs
1222*/
1223#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1224#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1225#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1226
1227/*
1228** These are the allowed modes.
1229*/
1230#define MODE_Line     0  /* One column per line.  Blank line between records */
1231#define MODE_Column   1  /* One record per line in neat columns */
1232#define MODE_List     2  /* One record per line with a separator */
1233#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1234#define MODE_Html     4  /* Generate an XHTML table */
1235#define MODE_Insert   5  /* Generate SQL "insert" statements */
1236#define MODE_Quote    6  /* Quote values as for SQL */
1237#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1238#define MODE_Csv      8  /* Quote strings, numbers are plain */
1239#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1240#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1241#define MODE_Pretty  11  /* Pretty-print schemas */
1242#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1243#define MODE_Json    13  /* Output JSON */
1244#define MODE_Markdown 14 /* Markdown formatting */
1245#define MODE_Table   15  /* MySQL-style table formatting */
1246#define MODE_Box     16  /* Unicode box-drawing characters */
1247#define MODE_Count   17  /* Output only a count of the rows of output */
1248#define MODE_Off     18  /* No query output shown */
1249
1250static const char *modeDescr[] = {
1251  "line",
1252  "column",
1253  "list",
1254  "semi",
1255  "html",
1256  "insert",
1257  "quote",
1258  "tcl",
1259  "csv",
1260  "explain",
1261  "ascii",
1262  "prettyprint",
1263  "eqp",
1264  "json",
1265  "markdown",
1266  "table",
1267  "box",
1268  "count",
1269  "off"
1270};
1271
1272/*
1273** These are the column/row/line separators used by the various
1274** import/export modes.
1275*/
1276#define SEP_Column    "|"
1277#define SEP_Row       "\n"
1278#define SEP_Tab       "\t"
1279#define SEP_Space     " "
1280#define SEP_Comma     ","
1281#define SEP_CrLf      "\r\n"
1282#define SEP_Unit      "\x1F"
1283#define SEP_Record    "\x1E"
1284
1285/*
1286** Limit input nesting via .read or any other input redirect.
1287** It's not too expensive, so a generous allowance can be made.
1288*/
1289#define MAX_INPUT_NESTING 25
1290
1291/*
1292** A callback for the sqlite3_log() interface.
1293*/
1294static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1295  ShellState *p = (ShellState*)pArg;
1296  if( p->pLog==0 ) return;
1297  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1298  fflush(p->pLog);
1299}
1300
1301/*
1302** SQL function:  shell_putsnl(X)
1303**
1304** Write the text X to the screen (or whatever output is being directed)
1305** adding a newline at the end, and then return X.
1306*/
1307static void shellPutsFunc(
1308  sqlite3_context *pCtx,
1309  int nVal,
1310  sqlite3_value **apVal
1311){
1312  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1313  (void)nVal;
1314  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1315  sqlite3_result_value(pCtx, apVal[0]);
1316}
1317
1318/*
1319** If in safe mode, print an error message described by the arguments
1320** and exit immediately.
1321*/
1322static void failIfSafeMode(
1323  ShellState *p,
1324  const char *zErrMsg,
1325  ...
1326){
1327  if( p->bSafeMode ){
1328    va_list ap;
1329    char *zMsg;
1330    va_start(ap, zErrMsg);
1331    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1332    va_end(ap);
1333    raw_printf(stderr, "line %d: ", p->lineno);
1334    utf8_printf(stderr, "%s\n", zMsg);
1335    exit(1);
1336  }
1337}
1338
1339/*
1340** SQL function:   edit(VALUE)
1341**                 edit(VALUE,EDITOR)
1342**
1343** These steps:
1344**
1345**     (1) Write VALUE into a temporary file.
1346**     (2) Run program EDITOR on that temporary file.
1347**     (3) Read the temporary file back and return its content as the result.
1348**     (4) Delete the temporary file
1349**
1350** If the EDITOR argument is omitted, use the value in the VISUAL
1351** environment variable.  If still there is no EDITOR, through an error.
1352**
1353** Also throw an error if the EDITOR program returns a non-zero exit code.
1354*/
1355#ifndef SQLITE_NOHAVE_SYSTEM
1356static void editFunc(
1357  sqlite3_context *context,
1358  int argc,
1359  sqlite3_value **argv
1360){
1361  const char *zEditor;
1362  char *zTempFile = 0;
1363  sqlite3 *db;
1364  char *zCmd = 0;
1365  int bBin;
1366  int rc;
1367  int hasCRNL = 0;
1368  FILE *f = 0;
1369  sqlite3_int64 sz;
1370  sqlite3_int64 x;
1371  unsigned char *p = 0;
1372
1373  if( argc==2 ){
1374    zEditor = (const char*)sqlite3_value_text(argv[1]);
1375  }else{
1376    zEditor = getenv("VISUAL");
1377  }
1378  if( zEditor==0 ){
1379    sqlite3_result_error(context, "no editor for edit()", -1);
1380    return;
1381  }
1382  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1383    sqlite3_result_error(context, "NULL input to edit()", -1);
1384    return;
1385  }
1386  db = sqlite3_context_db_handle(context);
1387  zTempFile = 0;
1388  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1389  if( zTempFile==0 ){
1390    sqlite3_uint64 r = 0;
1391    sqlite3_randomness(sizeof(r), &r);
1392    zTempFile = sqlite3_mprintf("temp%llx", r);
1393    if( zTempFile==0 ){
1394      sqlite3_result_error_nomem(context);
1395      return;
1396    }
1397  }
1398  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1399  /* When writing the file to be edited, do \n to \r\n conversions on systems
1400  ** that want \r\n line endings */
1401  f = fopen(zTempFile, bBin ? "wb" : "w");
1402  if( f==0 ){
1403    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1404    goto edit_func_end;
1405  }
1406  sz = sqlite3_value_bytes(argv[0]);
1407  if( bBin ){
1408    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1409  }else{
1410    const char *z = (const char*)sqlite3_value_text(argv[0]);
1411    /* Remember whether or not the value originally contained \r\n */
1412    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1413    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1414  }
1415  fclose(f);
1416  f = 0;
1417  if( x!=sz ){
1418    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1419    goto edit_func_end;
1420  }
1421  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1422  if( zCmd==0 ){
1423    sqlite3_result_error_nomem(context);
1424    goto edit_func_end;
1425  }
1426  rc = system(zCmd);
1427  sqlite3_free(zCmd);
1428  if( rc ){
1429    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1430    goto edit_func_end;
1431  }
1432  f = fopen(zTempFile, "rb");
1433  if( f==0 ){
1434    sqlite3_result_error(context,
1435      "edit() cannot reopen temp file after edit", -1);
1436    goto edit_func_end;
1437  }
1438  fseek(f, 0, SEEK_END);
1439  sz = ftell(f);
1440  rewind(f);
1441  p = sqlite3_malloc64( sz+1 );
1442  if( p==0 ){
1443    sqlite3_result_error_nomem(context);
1444    goto edit_func_end;
1445  }
1446  x = fread(p, 1, (size_t)sz, f);
1447  fclose(f);
1448  f = 0;
1449  if( x!=sz ){
1450    sqlite3_result_error(context, "could not read back the whole file", -1);
1451    goto edit_func_end;
1452  }
1453  if( bBin ){
1454    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1455  }else{
1456    sqlite3_int64 i, j;
1457    if( hasCRNL ){
1458      /* If the original contains \r\n then do no conversions back to \n */
1459    }else{
1460      /* If the file did not originally contain \r\n then convert any new
1461      ** \r\n back into \n */
1462      for(i=j=0; i<sz; i++){
1463        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1464        p[j++] = p[i];
1465      }
1466      sz = j;
1467      p[sz] = 0;
1468    }
1469    sqlite3_result_text64(context, (const char*)p, sz,
1470                          sqlite3_free, SQLITE_UTF8);
1471  }
1472  p = 0;
1473
1474edit_func_end:
1475  if( f ) fclose(f);
1476  unlink(zTempFile);
1477  sqlite3_free(zTempFile);
1478  sqlite3_free(p);
1479}
1480#endif /* SQLITE_NOHAVE_SYSTEM */
1481
1482/*
1483** Save or restore the current output mode
1484*/
1485static void outputModePush(ShellState *p){
1486  p->modePrior = p->mode;
1487  p->priorShFlgs = p->shellFlgs;
1488  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1489  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1490}
1491static void outputModePop(ShellState *p){
1492  p->mode = p->modePrior;
1493  p->shellFlgs = p->priorShFlgs;
1494  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1495  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1496}
1497
1498/*
1499** Output the given string as a hex-encoded blob (eg. X'1234' )
1500*/
1501static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1502  int i;
1503  unsigned char *aBlob = (unsigned char*)pBlob;
1504
1505  char *zStr = sqlite3_malloc(nBlob*2 + 1);
1506  shell_check_oom(zStr);
1507
1508  for(i=0; i<nBlob; i++){
1509    static const char aHex[] = {
1510        '0', '1', '2', '3', '4', '5', '6', '7',
1511        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1512    };
1513    zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1514    zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1515  }
1516  zStr[i*2] = '\0';
1517
1518  raw_printf(out,"X'%s'", zStr);
1519  sqlite3_free(zStr);
1520}
1521
1522/*
1523** Find a string that is not found anywhere in z[].  Return a pointer
1524** to that string.
1525**
1526** Try to use zA and zB first.  If both of those are already found in z[]
1527** then make up some string and store it in the buffer zBuf.
1528*/
1529static const char *unused_string(
1530  const char *z,                    /* Result must not appear anywhere in z */
1531  const char *zA, const char *zB,   /* Try these first */
1532  char *zBuf                        /* Space to store a generated string */
1533){
1534  unsigned i = 0;
1535  if( strstr(z, zA)==0 ) return zA;
1536  if( strstr(z, zB)==0 ) return zB;
1537  do{
1538    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1539  }while( strstr(z,zBuf)!=0 );
1540  return zBuf;
1541}
1542
1543/*
1544** Output the given string as a quoted string using SQL quoting conventions.
1545**
1546** See also: output_quoted_escaped_string()
1547*/
1548static void output_quoted_string(FILE *out, const char *z){
1549  int i;
1550  char c;
1551  setBinaryMode(out, 1);
1552  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1553  if( c==0 ){
1554    utf8_printf(out,"'%s'",z);
1555  }else{
1556    raw_printf(out, "'");
1557    while( *z ){
1558      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1559      if( c=='\'' ) i++;
1560      if( i ){
1561        utf8_printf(out, "%.*s", i, z);
1562        z += i;
1563      }
1564      if( c=='\'' ){
1565        raw_printf(out, "'");
1566        continue;
1567      }
1568      if( c==0 ){
1569        break;
1570      }
1571      z++;
1572    }
1573    raw_printf(out, "'");
1574  }
1575  setTextMode(out, 1);
1576}
1577
1578/*
1579** Output the given string as a quoted string using SQL quoting conventions.
1580** Additionallly , escape the "\n" and "\r" characters so that they do not
1581** get corrupted by end-of-line translation facilities in some operating
1582** systems.
1583**
1584** This is like output_quoted_string() but with the addition of the \r\n
1585** escape mechanism.
1586*/
1587static void output_quoted_escaped_string(FILE *out, const char *z){
1588  int i;
1589  char c;
1590  setBinaryMode(out, 1);
1591  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1592  if( c==0 ){
1593    utf8_printf(out,"'%s'",z);
1594  }else{
1595    const char *zNL = 0;
1596    const char *zCR = 0;
1597    int nNL = 0;
1598    int nCR = 0;
1599    char zBuf1[20], zBuf2[20];
1600    for(i=0; z[i]; i++){
1601      if( z[i]=='\n' ) nNL++;
1602      if( z[i]=='\r' ) nCR++;
1603    }
1604    if( nNL ){
1605      raw_printf(out, "replace(");
1606      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1607    }
1608    if( nCR ){
1609      raw_printf(out, "replace(");
1610      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1611    }
1612    raw_printf(out, "'");
1613    while( *z ){
1614      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1615      if( c=='\'' ) i++;
1616      if( i ){
1617        utf8_printf(out, "%.*s", i, z);
1618        z += i;
1619      }
1620      if( c=='\'' ){
1621        raw_printf(out, "'");
1622        continue;
1623      }
1624      if( c==0 ){
1625        break;
1626      }
1627      z++;
1628      if( c=='\n' ){
1629        raw_printf(out, "%s", zNL);
1630        continue;
1631      }
1632      raw_printf(out, "%s", zCR);
1633    }
1634    raw_printf(out, "'");
1635    if( nCR ){
1636      raw_printf(out, ",'%s',char(13))", zCR);
1637    }
1638    if( nNL ){
1639      raw_printf(out, ",'%s',char(10))", zNL);
1640    }
1641  }
1642  setTextMode(out, 1);
1643}
1644
1645/*
1646** Output the given string as a quoted according to C or TCL quoting rules.
1647*/
1648static void output_c_string(FILE *out, const char *z){
1649  unsigned int c;
1650  fputc('"', out);
1651  while( (c = *(z++))!=0 ){
1652    if( c=='\\' ){
1653      fputc(c, out);
1654      fputc(c, out);
1655    }else if( c=='"' ){
1656      fputc('\\', out);
1657      fputc('"', out);
1658    }else if( c=='\t' ){
1659      fputc('\\', out);
1660      fputc('t', out);
1661    }else if( c=='\n' ){
1662      fputc('\\', out);
1663      fputc('n', out);
1664    }else if( c=='\r' ){
1665      fputc('\\', out);
1666      fputc('r', out);
1667    }else if( !isprint(c&0xff) ){
1668      raw_printf(out, "\\%03o", c&0xff);
1669    }else{
1670      fputc(c, out);
1671    }
1672  }
1673  fputc('"', out);
1674}
1675
1676/*
1677** Output the given string as a quoted according to JSON quoting rules.
1678*/
1679static void output_json_string(FILE *out, const char *z, i64 n){
1680  unsigned int c;
1681  if( n<0 ) n = strlen(z);
1682  fputc('"', out);
1683  while( n-- ){
1684    c = *(z++);
1685    if( c=='\\' || c=='"' ){
1686      fputc('\\', out);
1687      fputc(c, out);
1688    }else if( c<=0x1f ){
1689      fputc('\\', out);
1690      if( c=='\b' ){
1691        fputc('b', out);
1692      }else if( c=='\f' ){
1693        fputc('f', out);
1694      }else if( c=='\n' ){
1695        fputc('n', out);
1696      }else if( c=='\r' ){
1697        fputc('r', out);
1698      }else if( c=='\t' ){
1699        fputc('t', out);
1700      }else{
1701         raw_printf(out, "u%04x",c);
1702      }
1703    }else{
1704      fputc(c, out);
1705    }
1706  }
1707  fputc('"', out);
1708}
1709
1710/*
1711** Output the given string with characters that are special to
1712** HTML escaped.
1713*/
1714static void output_html_string(FILE *out, const char *z){
1715  int i;
1716  if( z==0 ) z = "";
1717  while( *z ){
1718    for(i=0;   z[i]
1719            && z[i]!='<'
1720            && z[i]!='&'
1721            && z[i]!='>'
1722            && z[i]!='\"'
1723            && z[i]!='\'';
1724        i++){}
1725    if( i>0 ){
1726      utf8_printf(out,"%.*s",i,z);
1727    }
1728    if( z[i]=='<' ){
1729      raw_printf(out,"&lt;");
1730    }else if( z[i]=='&' ){
1731      raw_printf(out,"&amp;");
1732    }else if( z[i]=='>' ){
1733      raw_printf(out,"&gt;");
1734    }else if( z[i]=='\"' ){
1735      raw_printf(out,"&quot;");
1736    }else if( z[i]=='\'' ){
1737      raw_printf(out,"&#39;");
1738    }else{
1739      break;
1740    }
1741    z += i + 1;
1742  }
1743}
1744
1745/*
1746** If a field contains any character identified by a 1 in the following
1747** array, then the string must be quoted for CSV.
1748*/
1749static const char needCsvQuote[] = {
1750  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1751  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1752  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1753  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1754  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1755  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1756  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1757  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1758  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1759  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1760  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1761  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1762  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1763  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1764  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1765  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1766};
1767
1768/*
1769** Output a single term of CSV.  Actually, p->colSeparator is used for
1770** the separator, which may or may not be a comma.  p->nullValue is
1771** the null value.  Strings are quoted if necessary.  The separator
1772** is only issued if bSep is true.
1773*/
1774static void output_csv(ShellState *p, const char *z, int bSep){
1775  FILE *out = p->out;
1776  if( z==0 ){
1777    utf8_printf(out,"%s",p->nullValue);
1778  }else{
1779    unsigned i;
1780    for(i=0; z[i]; i++){
1781      if( needCsvQuote[((unsigned char*)z)[i]] ){
1782        i = 0;
1783        break;
1784      }
1785    }
1786    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1787      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1788      shell_check_oom(zQuoted);
1789      utf8_printf(out, "%s", zQuoted);
1790      sqlite3_free(zQuoted);
1791    }else{
1792      utf8_printf(out, "%s", z);
1793    }
1794  }
1795  if( bSep ){
1796    utf8_printf(p->out, "%s", p->colSeparator);
1797  }
1798}
1799
1800/*
1801** This routine runs when the user presses Ctrl-C
1802*/
1803static void interrupt_handler(int NotUsed){
1804  UNUSED_PARAMETER(NotUsed);
1805  seenInterrupt++;
1806  if( seenInterrupt>2 ) exit(1);
1807  if( globalDb ) sqlite3_interrupt(globalDb);
1808}
1809
1810#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1811/*
1812** This routine runs for console events (e.g. Ctrl-C) on Win32
1813*/
1814static BOOL WINAPI ConsoleCtrlHandler(
1815  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1816){
1817  if( dwCtrlType==CTRL_C_EVENT ){
1818    interrupt_handler(0);
1819    return TRUE;
1820  }
1821  return FALSE;
1822}
1823#endif
1824
1825#ifndef SQLITE_OMIT_AUTHORIZATION
1826/*
1827** This authorizer runs in safe mode.
1828*/
1829static int safeModeAuth(
1830  void *pClientData,
1831  int op,
1832  const char *zA1,
1833  const char *zA2,
1834  const char *zA3,
1835  const char *zA4
1836){
1837  ShellState *p = (ShellState*)pClientData;
1838  static const char *azProhibitedFunctions[] = {
1839    "edit",
1840    "fts3_tokenizer",
1841    "load_extension",
1842    "readfile",
1843    "writefile",
1844    "zipfile",
1845    "zipfile_cds",
1846  };
1847  UNUSED_PARAMETER(zA2);
1848  UNUSED_PARAMETER(zA3);
1849  UNUSED_PARAMETER(zA4);
1850  switch( op ){
1851    case SQLITE_ATTACH: {
1852#ifndef SQLITE_SHELL_FIDDLE
1853      /* In WASM builds the filesystem is a virtual sandbox, so
1854      ** there's no harm in using ATTACH. */
1855      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1856#endif
1857      break;
1858    }
1859    case SQLITE_FUNCTION: {
1860      int i;
1861      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1862        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1863          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1864                         azProhibitedFunctions[i]);
1865        }
1866      }
1867      break;
1868    }
1869  }
1870  return SQLITE_OK;
1871}
1872
1873/*
1874** When the ".auth ON" is set, the following authorizer callback is
1875** invoked.  It always returns SQLITE_OK.
1876*/
1877static int shellAuth(
1878  void *pClientData,
1879  int op,
1880  const char *zA1,
1881  const char *zA2,
1882  const char *zA3,
1883  const char *zA4
1884){
1885  ShellState *p = (ShellState*)pClientData;
1886  static const char *azAction[] = { 0,
1887     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1888     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1889     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1890     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1891     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1892     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1893     "PRAGMA",               "READ",                 "SELECT",
1894     "TRANSACTION",          "UPDATE",               "ATTACH",
1895     "DETACH",               "ALTER_TABLE",          "REINDEX",
1896     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1897     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1898  };
1899  int i;
1900  const char *az[4];
1901  az[0] = zA1;
1902  az[1] = zA2;
1903  az[2] = zA3;
1904  az[3] = zA4;
1905  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1906  for(i=0; i<4; i++){
1907    raw_printf(p->out, " ");
1908    if( az[i] ){
1909      output_c_string(p->out, az[i]);
1910    }else{
1911      raw_printf(p->out, "NULL");
1912    }
1913  }
1914  raw_printf(p->out, "\n");
1915  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1916  return SQLITE_OK;
1917}
1918#endif
1919
1920/*
1921** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1922**
1923** This routine converts some CREATE TABLE statements for shadow tables
1924** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1925**
1926** If the schema statement in z[] contains a start-of-comment and if
1927** sqlite3_complete() returns false, try to terminate the comment before
1928** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
1929*/
1930static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1931  char *zToFree = 0;
1932  if( z==0 ) return;
1933  if( zTail==0 ) return;
1934  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
1935    const char *zOrig = z;
1936    static const char *azTerm[] = { "", "*/", "\n" };
1937    int i;
1938    for(i=0; i<ArraySize(azTerm); i++){
1939      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
1940      if( sqlite3_complete(zNew) ){
1941        size_t n = strlen(zNew);
1942        zNew[n-1] = 0;
1943        zToFree = zNew;
1944        z = zNew;
1945        break;
1946      }
1947      sqlite3_free(zNew);
1948    }
1949  }
1950  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1951    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1952  }else{
1953    utf8_printf(out, "%s%s", z, zTail);
1954  }
1955  sqlite3_free(zToFree);
1956}
1957static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1958  char c = z[n];
1959  z[n] = 0;
1960  printSchemaLine(out, z, zTail);
1961  z[n] = c;
1962}
1963
1964/*
1965** Return true if string z[] has nothing but whitespace and comments to the
1966** end of the first line.
1967*/
1968static int wsToEol(const char *z){
1969  int i;
1970  for(i=0; z[i]; i++){
1971    if( z[i]=='\n' ) return 1;
1972    if( IsSpace(z[i]) ) continue;
1973    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1974    return 0;
1975  }
1976  return 1;
1977}
1978
1979/*
1980** Add a new entry to the EXPLAIN QUERY PLAN data
1981*/
1982static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1983  EQPGraphRow *pNew;
1984  i64 nText = strlen(zText);
1985  if( p->autoEQPtest ){
1986    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1987  }
1988  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1989  shell_check_oom(pNew);
1990  pNew->iEqpId = iEqpId;
1991  pNew->iParentId = p2;
1992  memcpy(pNew->zText, zText, nText+1);
1993  pNew->pNext = 0;
1994  if( p->sGraph.pLast ){
1995    p->sGraph.pLast->pNext = pNew;
1996  }else{
1997    p->sGraph.pRow = pNew;
1998  }
1999  p->sGraph.pLast = pNew;
2000}
2001
2002/*
2003** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2004** in p->sGraph.
2005*/
2006static void eqp_reset(ShellState *p){
2007  EQPGraphRow *pRow, *pNext;
2008  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2009    pNext = pRow->pNext;
2010    sqlite3_free(pRow);
2011  }
2012  memset(&p->sGraph, 0, sizeof(p->sGraph));
2013}
2014
2015/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2016** pOld, or return the first such line if pOld is NULL
2017*/
2018static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2019  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2020  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2021  return pRow;
2022}
2023
2024/* Render a single level of the graph that has iEqpId as its parent.  Called
2025** recursively to render sublevels.
2026*/
2027static void eqp_render_level(ShellState *p, int iEqpId){
2028  EQPGraphRow *pRow, *pNext;
2029  i64 n = strlen(p->sGraph.zPrefix);
2030  char *z;
2031  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2032    pNext = eqp_next_row(p, iEqpId, pRow);
2033    z = pRow->zText;
2034    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2035                pNext ? "|--" : "`--", z);
2036    if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2037      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
2038      eqp_render_level(p, pRow->iEqpId);
2039      p->sGraph.zPrefix[n] = 0;
2040    }
2041  }
2042}
2043
2044/*
2045** Display and reset the EXPLAIN QUERY PLAN data
2046*/
2047static void eqp_render(ShellState *p){
2048  EQPGraphRow *pRow = p->sGraph.pRow;
2049  if( pRow ){
2050    if( pRow->zText[0]=='-' ){
2051      if( pRow->pNext==0 ){
2052        eqp_reset(p);
2053        return;
2054      }
2055      utf8_printf(p->out, "%s\n", pRow->zText+3);
2056      p->sGraph.pRow = pRow->pNext;
2057      sqlite3_free(pRow);
2058    }else{
2059      utf8_printf(p->out, "QUERY PLAN\n");
2060    }
2061    p->sGraph.zPrefix[0] = 0;
2062    eqp_render_level(p, 0);
2063    eqp_reset(p);
2064  }
2065}
2066
2067#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2068/*
2069** Progress handler callback.
2070*/
2071static int progress_handler(void *pClientData) {
2072  ShellState *p = (ShellState*)pClientData;
2073  p->nProgress++;
2074  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2075    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2076    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2077    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2078    return 1;
2079  }
2080  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2081    raw_printf(p->out, "Progress %u\n", p->nProgress);
2082  }
2083  return 0;
2084}
2085#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2086
2087/*
2088** Print N dashes
2089*/
2090static void print_dashes(FILE *out, int N){
2091  const char zDash[] = "--------------------------------------------------";
2092  const int nDash = sizeof(zDash) - 1;
2093  while( N>nDash ){
2094    fputs(zDash, out);
2095    N -= nDash;
2096  }
2097  raw_printf(out, "%.*s", N, zDash);
2098}
2099
2100/*
2101** Print a markdown or table-style row separator using ascii-art
2102*/
2103static void print_row_separator(
2104  ShellState *p,
2105  int nArg,
2106  const char *zSep
2107){
2108  int i;
2109  if( nArg>0 ){
2110    fputs(zSep, p->out);
2111    print_dashes(p->out, p->actualWidth[0]+2);
2112    for(i=1; i<nArg; i++){
2113      fputs(zSep, p->out);
2114      print_dashes(p->out, p->actualWidth[i]+2);
2115    }
2116    fputs(zSep, p->out);
2117  }
2118  fputs("\n", p->out);
2119}
2120
2121/*
2122** This is the callback routine that the shell
2123** invokes for each row of a query result.
2124*/
2125static int shell_callback(
2126  void *pArg,
2127  int nArg,        /* Number of result columns */
2128  char **azArg,    /* Text of each result column */
2129  char **azCol,    /* Column names */
2130  int *aiType      /* Column types.  Might be NULL */
2131){
2132  int i;
2133  ShellState *p = (ShellState*)pArg;
2134
2135  if( azArg==0 ) return 0;
2136  switch( p->cMode ){
2137    case MODE_Count:
2138    case MODE_Off: {
2139      break;
2140    }
2141    case MODE_Line: {
2142      int w = 5;
2143      if( azArg==0 ) break;
2144      for(i=0; i<nArg; i++){
2145        int len = strlen30(azCol[i] ? azCol[i] : "");
2146        if( len>w ) w = len;
2147      }
2148      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2149      for(i=0; i<nArg; i++){
2150        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2151                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2152      }
2153      break;
2154    }
2155    case MODE_Explain: {
2156      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2157      if( nArg>ArraySize(aExplainWidth) ){
2158        nArg = ArraySize(aExplainWidth);
2159      }
2160      if( p->cnt++==0 ){
2161        for(i=0; i<nArg; i++){
2162          int w = aExplainWidth[i];
2163          utf8_width_print(p->out, w, azCol[i]);
2164          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2165        }
2166        for(i=0; i<nArg; i++){
2167          int w = aExplainWidth[i];
2168          print_dashes(p->out, w);
2169          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2170        }
2171      }
2172      if( azArg==0 ) break;
2173      for(i=0; i<nArg; i++){
2174        int w = aExplainWidth[i];
2175        if( i==nArg-1 ) w = 0;
2176        if( azArg[i] && strlenChar(azArg[i])>w ){
2177          w = strlenChar(azArg[i]);
2178        }
2179        if( i==1 && p->aiIndent && p->pStmt ){
2180          if( p->iIndent<p->nIndent ){
2181            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2182          }
2183          p->iIndent++;
2184        }
2185        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2186        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2187      }
2188      break;
2189    }
2190    case MODE_Semi: {   /* .schema and .fullschema output */
2191      printSchemaLine(p->out, azArg[0], ";\n");
2192      break;
2193    }
2194    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2195      char *z;
2196      int j;
2197      int nParen = 0;
2198      char cEnd = 0;
2199      char c;
2200      int nLine = 0;
2201      assert( nArg==1 );
2202      if( azArg[0]==0 ) break;
2203      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2204       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2205      ){
2206        utf8_printf(p->out, "%s;\n", azArg[0]);
2207        break;
2208      }
2209      z = sqlite3_mprintf("%s", azArg[0]);
2210      shell_check_oom(z);
2211      j = 0;
2212      for(i=0; IsSpace(z[i]); i++){}
2213      for(; (c = z[i])!=0; i++){
2214        if( IsSpace(c) ){
2215          if( z[j-1]=='\r' ) z[j-1] = '\n';
2216          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2217        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2218          j--;
2219        }
2220        z[j++] = c;
2221      }
2222      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2223      z[j] = 0;
2224      if( strlen30(z)>=79 ){
2225        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2226          if( c==cEnd ){
2227            cEnd = 0;
2228          }else if( c=='"' || c=='\'' || c=='`' ){
2229            cEnd = c;
2230          }else if( c=='[' ){
2231            cEnd = ']';
2232          }else if( c=='-' && z[i+1]=='-' ){
2233            cEnd = '\n';
2234          }else if( c=='(' ){
2235            nParen++;
2236          }else if( c==')' ){
2237            nParen--;
2238            if( nLine>0 && nParen==0 && j>0 ){
2239              printSchemaLineN(p->out, z, j, "\n");
2240              j = 0;
2241            }
2242          }
2243          z[j++] = c;
2244          if( nParen==1 && cEnd==0
2245           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2246          ){
2247            if( c=='\n' ) j--;
2248            printSchemaLineN(p->out, z, j, "\n  ");
2249            j = 0;
2250            nLine++;
2251            while( IsSpace(z[i+1]) ){ i++; }
2252          }
2253        }
2254        z[j] = 0;
2255      }
2256      printSchemaLine(p->out, z, ";\n");
2257      sqlite3_free(z);
2258      break;
2259    }
2260    case MODE_List: {
2261      if( p->cnt++==0 && p->showHeader ){
2262        for(i=0; i<nArg; i++){
2263          utf8_printf(p->out,"%s%s",azCol[i],
2264                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2265        }
2266      }
2267      if( azArg==0 ) break;
2268      for(i=0; i<nArg; i++){
2269        char *z = azArg[i];
2270        if( z==0 ) z = p->nullValue;
2271        utf8_printf(p->out, "%s", z);
2272        if( i<nArg-1 ){
2273          utf8_printf(p->out, "%s", p->colSeparator);
2274        }else{
2275          utf8_printf(p->out, "%s", p->rowSeparator);
2276        }
2277      }
2278      break;
2279    }
2280    case MODE_Html: {
2281      if( p->cnt++==0 && p->showHeader ){
2282        raw_printf(p->out,"<TR>");
2283        for(i=0; i<nArg; i++){
2284          raw_printf(p->out,"<TH>");
2285          output_html_string(p->out, azCol[i]);
2286          raw_printf(p->out,"</TH>\n");
2287        }
2288        raw_printf(p->out,"</TR>\n");
2289      }
2290      if( azArg==0 ) break;
2291      raw_printf(p->out,"<TR>");
2292      for(i=0; i<nArg; i++){
2293        raw_printf(p->out,"<TD>");
2294        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2295        raw_printf(p->out,"</TD>\n");
2296      }
2297      raw_printf(p->out,"</TR>\n");
2298      break;
2299    }
2300    case MODE_Tcl: {
2301      if( p->cnt++==0 && p->showHeader ){
2302        for(i=0; i<nArg; i++){
2303          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2304          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2305        }
2306        utf8_printf(p->out, "%s", p->rowSeparator);
2307      }
2308      if( azArg==0 ) break;
2309      for(i=0; i<nArg; i++){
2310        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2311        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2312      }
2313      utf8_printf(p->out, "%s", p->rowSeparator);
2314      break;
2315    }
2316    case MODE_Csv: {
2317      setBinaryMode(p->out, 1);
2318      if( p->cnt++==0 && p->showHeader ){
2319        for(i=0; i<nArg; i++){
2320          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2321        }
2322        utf8_printf(p->out, "%s", p->rowSeparator);
2323      }
2324      if( nArg>0 ){
2325        for(i=0; i<nArg; i++){
2326          output_csv(p, azArg[i], i<nArg-1);
2327        }
2328        utf8_printf(p->out, "%s", p->rowSeparator);
2329      }
2330      setTextMode(p->out, 1);
2331      break;
2332    }
2333    case MODE_Insert: {
2334      if( azArg==0 ) break;
2335      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2336      if( p->showHeader ){
2337        raw_printf(p->out,"(");
2338        for(i=0; i<nArg; i++){
2339          if( i>0 ) raw_printf(p->out, ",");
2340          if( quoteChar(azCol[i]) ){
2341            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2342            shell_check_oom(z);
2343            utf8_printf(p->out, "%s", z);
2344            sqlite3_free(z);
2345          }else{
2346            raw_printf(p->out, "%s", azCol[i]);
2347          }
2348        }
2349        raw_printf(p->out,")");
2350      }
2351      p->cnt++;
2352      for(i=0; i<nArg; i++){
2353        raw_printf(p->out, i>0 ? "," : " VALUES(");
2354        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2355          utf8_printf(p->out,"NULL");
2356        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2357          if( ShellHasFlag(p, SHFLG_Newlines) ){
2358            output_quoted_string(p->out, azArg[i]);
2359          }else{
2360            output_quoted_escaped_string(p->out, azArg[i]);
2361          }
2362        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2363          utf8_printf(p->out,"%s", azArg[i]);
2364        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2365          char z[50];
2366          double r = sqlite3_column_double(p->pStmt, i);
2367          sqlite3_uint64 ur;
2368          memcpy(&ur,&r,sizeof(r));
2369          if( ur==0x7ff0000000000000LL ){
2370            raw_printf(p->out, "1e999");
2371          }else if( ur==0xfff0000000000000LL ){
2372            raw_printf(p->out, "-1e999");
2373          }else{
2374            sqlite3_int64 ir = (sqlite3_int64)r;
2375            if( r==(double)ir ){
2376              sqlite3_snprintf(50,z,"%lld.0", ir);
2377            }else{
2378              sqlite3_snprintf(50,z,"%!.20g", r);
2379            }
2380            raw_printf(p->out, "%s", z);
2381          }
2382        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2383          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2384          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2385          output_hex_blob(p->out, pBlob, nBlob);
2386        }else if( isNumber(azArg[i], 0) ){
2387          utf8_printf(p->out,"%s", azArg[i]);
2388        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2389          output_quoted_string(p->out, azArg[i]);
2390        }else{
2391          output_quoted_escaped_string(p->out, azArg[i]);
2392        }
2393      }
2394      raw_printf(p->out,");\n");
2395      break;
2396    }
2397    case MODE_Json: {
2398      if( azArg==0 ) break;
2399      if( p->cnt==0 ){
2400        fputs("[{", p->out);
2401      }else{
2402        fputs(",\n{", p->out);
2403      }
2404      p->cnt++;
2405      for(i=0; i<nArg; i++){
2406        output_json_string(p->out, azCol[i], -1);
2407        putc(':', p->out);
2408        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2409          fputs("null",p->out);
2410        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2411          char z[50];
2412          double r = sqlite3_column_double(p->pStmt, i);
2413          sqlite3_uint64 ur;
2414          memcpy(&ur,&r,sizeof(r));
2415          if( ur==0x7ff0000000000000LL ){
2416            raw_printf(p->out, "1e999");
2417          }else if( ur==0xfff0000000000000LL ){
2418            raw_printf(p->out, "-1e999");
2419          }else{
2420            sqlite3_snprintf(50,z,"%!.20g", r);
2421            raw_printf(p->out, "%s", z);
2422          }
2423        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2424          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2425          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2426          output_json_string(p->out, pBlob, nBlob);
2427        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2428          output_json_string(p->out, azArg[i], -1);
2429        }else{
2430          utf8_printf(p->out,"%s", azArg[i]);
2431        }
2432        if( i<nArg-1 ){
2433          putc(',', p->out);
2434        }
2435      }
2436      putc('}', p->out);
2437      break;
2438    }
2439    case MODE_Quote: {
2440      if( azArg==0 ) break;
2441      if( p->cnt==0 && p->showHeader ){
2442        for(i=0; i<nArg; i++){
2443          if( i>0 ) fputs(p->colSeparator, p->out);
2444          output_quoted_string(p->out, azCol[i]);
2445        }
2446        fputs(p->rowSeparator, p->out);
2447      }
2448      p->cnt++;
2449      for(i=0; i<nArg; i++){
2450        if( i>0 ) fputs(p->colSeparator, p->out);
2451        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2452          utf8_printf(p->out,"NULL");
2453        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2454          output_quoted_string(p->out, azArg[i]);
2455        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2456          utf8_printf(p->out,"%s", azArg[i]);
2457        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2458          char z[50];
2459          double r = sqlite3_column_double(p->pStmt, i);
2460          sqlite3_snprintf(50,z,"%!.20g", r);
2461          raw_printf(p->out, "%s", z);
2462        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2463          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2464          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2465          output_hex_blob(p->out, pBlob, nBlob);
2466        }else if( isNumber(azArg[i], 0) ){
2467          utf8_printf(p->out,"%s", azArg[i]);
2468        }else{
2469          output_quoted_string(p->out, azArg[i]);
2470        }
2471      }
2472      fputs(p->rowSeparator, p->out);
2473      break;
2474    }
2475    case MODE_Ascii: {
2476      if( p->cnt++==0 && p->showHeader ){
2477        for(i=0; i<nArg; i++){
2478          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2479          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2480        }
2481        utf8_printf(p->out, "%s", p->rowSeparator);
2482      }
2483      if( azArg==0 ) break;
2484      for(i=0; i<nArg; i++){
2485        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2486        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2487      }
2488      utf8_printf(p->out, "%s", p->rowSeparator);
2489      break;
2490    }
2491    case MODE_EQP: {
2492      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2493      break;
2494    }
2495  }
2496  return 0;
2497}
2498
2499/*
2500** This is the callback routine that the SQLite library
2501** invokes for each row of a query result.
2502*/
2503static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2504  /* since we don't have type info, call the shell_callback with a NULL value */
2505  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2506}
2507
2508/*
2509** This is the callback routine from sqlite3_exec() that appends all
2510** output onto the end of a ShellText object.
2511*/
2512static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2513  ShellText *p = (ShellText*)pArg;
2514  int i;
2515  UNUSED_PARAMETER(az);
2516  if( azArg==0 ) return 0;
2517  if( p->n ) appendText(p, "|", 0);
2518  for(i=0; i<nArg; i++){
2519    if( i ) appendText(p, ",", 0);
2520    if( azArg[i] ) appendText(p, azArg[i], 0);
2521  }
2522  return 0;
2523}
2524
2525/*
2526** Generate an appropriate SELFTEST table in the main database.
2527*/
2528static void createSelftestTable(ShellState *p){
2529  char *zErrMsg = 0;
2530  sqlite3_exec(p->db,
2531    "SAVEPOINT selftest_init;\n"
2532    "CREATE TABLE IF NOT EXISTS selftest(\n"
2533    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2534    "  op TEXT,\n"                   /* Operator:  memo run */
2535    "  cmd TEXT,\n"                  /* Command text */
2536    "  ans TEXT\n"                   /* Desired answer */
2537    ");"
2538    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2539    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2540    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2541    "         'memo','Tests generated by --init');\n"
2542    "INSERT INTO [_shell$self]\n"
2543    "  SELECT 'run',\n"
2544    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2545                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2546    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2547                          "FROM sqlite_schema ORDER BY 2',224));\n"
2548    "INSERT INTO [_shell$self]\n"
2549    "  SELECT 'run',"
2550    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2551    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2552    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2553    "  FROM (\n"
2554    "    SELECT name FROM sqlite_schema\n"
2555    "     WHERE type='table'\n"
2556    "       AND name<>'selftest'\n"
2557    "       AND coalesce(rootpage,0)>0\n"
2558    "  )\n"
2559    " ORDER BY name;\n"
2560    "INSERT INTO [_shell$self]\n"
2561    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2562    "INSERT INTO selftest(tno,op,cmd,ans)"
2563    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2564    "DROP TABLE [_shell$self];"
2565    ,0,0,&zErrMsg);
2566  if( zErrMsg ){
2567    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2568    sqlite3_free(zErrMsg);
2569  }
2570  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2571}
2572
2573
2574/*
2575** Set the destination table field of the ShellState structure to
2576** the name of the table given.  Escape any quote characters in the
2577** table name.
2578*/
2579static void set_table_name(ShellState *p, const char *zName){
2580  int i, n;
2581  char cQuote;
2582  char *z;
2583
2584  if( p->zDestTable ){
2585    free(p->zDestTable);
2586    p->zDestTable = 0;
2587  }
2588  if( zName==0 ) return;
2589  cQuote = quoteChar(zName);
2590  n = strlen30(zName);
2591  if( cQuote ) n += n+2;
2592  z = p->zDestTable = malloc( n+1 );
2593  shell_check_oom(z);
2594  n = 0;
2595  if( cQuote ) z[n++] = cQuote;
2596  for(i=0; zName[i]; i++){
2597    z[n++] = zName[i];
2598    if( zName[i]==cQuote ) z[n++] = cQuote;
2599  }
2600  if( cQuote ) z[n++] = cQuote;
2601  z[n] = 0;
2602}
2603
2604/*
2605** Maybe construct two lines of text that point out the position of a
2606** syntax error.  Return a pointer to the text, in memory obtained from
2607** sqlite3_malloc().  Or, if the most recent error does not involve a
2608** specific token that we can point to, return an empty string.
2609**
2610** In all cases, the memory returned is obtained from sqlite3_malloc64()
2611** and should be released by the caller invoking sqlite3_free().
2612*/
2613static char *shell_error_context(const char *zSql, sqlite3 *db){
2614  int iOffset;
2615  size_t len;
2616  char *zCode;
2617  char *zMsg;
2618  int i;
2619  if( db==0
2620   || zSql==0
2621   || (iOffset = sqlite3_error_offset(db))<0
2622  ){
2623    return sqlite3_mprintf("");
2624  }
2625  while( iOffset>50 ){
2626    iOffset--;
2627    zSql++;
2628    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
2629  }
2630  len = strlen(zSql);
2631  if( len>78 ){
2632    len = 78;
2633    while( (zSql[len]&0xc0)==0x80 ) len--;
2634  }
2635  zCode = sqlite3_mprintf("%.*s", len, zSql);
2636  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
2637  if( iOffset<25 ){
2638    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");
2639  }else{
2640    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");
2641  }
2642  return zMsg;
2643}
2644
2645
2646/*
2647** Execute a query statement that will generate SQL output.  Print
2648** the result columns, comma-separated, on a line and then add a
2649** semicolon terminator to the end of that line.
2650**
2651** If the number of columns is 1 and that column contains text "--"
2652** then write the semicolon on a separate line.  That way, if a
2653** "--" comment occurs at the end of the statement, the comment
2654** won't consume the semicolon terminator.
2655*/
2656static int run_table_dump_query(
2657  ShellState *p,           /* Query context */
2658  const char *zSelect      /* SELECT statement to extract content */
2659){
2660  sqlite3_stmt *pSelect;
2661  int rc;
2662  int nResult;
2663  int i;
2664  const char *z;
2665  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2666  if( rc!=SQLITE_OK || !pSelect ){
2667    char *zContext = shell_error_context(zSelect, p->db);
2668    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
2669                sqlite3_errmsg(p->db), zContext);
2670    sqlite3_free(zContext);
2671    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2672    return rc;
2673  }
2674  rc = sqlite3_step(pSelect);
2675  nResult = sqlite3_column_count(pSelect);
2676  while( rc==SQLITE_ROW ){
2677    z = (const char*)sqlite3_column_text(pSelect, 0);
2678    utf8_printf(p->out, "%s", z);
2679    for(i=1; i<nResult; i++){
2680      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2681    }
2682    if( z==0 ) z = "";
2683    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2684    if( z[0] ){
2685      raw_printf(p->out, "\n;\n");
2686    }else{
2687      raw_printf(p->out, ";\n");
2688    }
2689    rc = sqlite3_step(pSelect);
2690  }
2691  rc = sqlite3_finalize(pSelect);
2692  if( rc!=SQLITE_OK ){
2693    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2694                sqlite3_errmsg(p->db));
2695    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2696  }
2697  return rc;
2698}
2699
2700/*
2701** Allocate space and save off string indicating current error.
2702*/
2703static char *save_err_msg(
2704  sqlite3 *db,           /* Database to query */
2705  const char *zPhase,    /* When the error occcurs */
2706  int rc,                /* Error code returned from API */
2707  const char *zSql       /* SQL string, or NULL */
2708){
2709  char *zErr;
2710  char *zContext;
2711  sqlite3_str *pStr = sqlite3_str_new(0);
2712  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
2713  if( rc>1 ){
2714    sqlite3_str_appendf(pStr, " (%d)", rc);
2715  }
2716  zContext = shell_error_context(zSql, db);
2717  if( zContext ){
2718    sqlite3_str_appendall(pStr, zContext);
2719    sqlite3_free(zContext);
2720  }
2721  zErr = sqlite3_str_finish(pStr);
2722  shell_check_oom(zErr);
2723  return zErr;
2724}
2725
2726#ifdef __linux__
2727/*
2728** Attempt to display I/O stats on Linux using /proc/PID/io
2729*/
2730static void displayLinuxIoStats(FILE *out){
2731  FILE *in;
2732  char z[200];
2733  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2734  in = fopen(z, "rb");
2735  if( in==0 ) return;
2736  while( fgets(z, sizeof(z), in)!=0 ){
2737    static const struct {
2738      const char *zPattern;
2739      const char *zDesc;
2740    } aTrans[] = {
2741      { "rchar: ",                  "Bytes received by read():" },
2742      { "wchar: ",                  "Bytes sent to write():"    },
2743      { "syscr: ",                  "Read() system calls:"      },
2744      { "syscw: ",                  "Write() system calls:"     },
2745      { "read_bytes: ",             "Bytes read from storage:"  },
2746      { "write_bytes: ",            "Bytes written to storage:" },
2747      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2748    };
2749    int i;
2750    for(i=0; i<ArraySize(aTrans); i++){
2751      int n = strlen30(aTrans[i].zPattern);
2752      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2753        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2754        break;
2755      }
2756    }
2757  }
2758  fclose(in);
2759}
2760#endif
2761
2762/*
2763** Display a single line of status using 64-bit values.
2764*/
2765static void displayStatLine(
2766  ShellState *p,            /* The shell context */
2767  char *zLabel,             /* Label for this one line */
2768  char *zFormat,            /* Format for the result */
2769  int iStatusCtrl,          /* Which status to display */
2770  int bReset                /* True to reset the stats */
2771){
2772  sqlite3_int64 iCur = -1;
2773  sqlite3_int64 iHiwtr = -1;
2774  int i, nPercent;
2775  char zLine[200];
2776  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2777  for(i=0, nPercent=0; zFormat[i]; i++){
2778    if( zFormat[i]=='%' ) nPercent++;
2779  }
2780  if( nPercent>1 ){
2781    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2782  }else{
2783    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2784  }
2785  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2786}
2787
2788/*
2789** Display memory stats.
2790*/
2791static int display_stats(
2792  sqlite3 *db,                /* Database to query */
2793  ShellState *pArg,           /* Pointer to ShellState */
2794  int bReset                  /* True to reset the stats */
2795){
2796  int iCur;
2797  int iHiwtr;
2798  FILE *out;
2799  if( pArg==0 || pArg->out==0 ) return 0;
2800  out = pArg->out;
2801
2802  if( pArg->pStmt && pArg->statsOn==2 ){
2803    int nCol, i, x;
2804    sqlite3_stmt *pStmt = pArg->pStmt;
2805    char z[100];
2806    nCol = sqlite3_column_count(pStmt);
2807    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2808    for(i=0; i<nCol; i++){
2809      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2810      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2811#ifndef SQLITE_OMIT_DECLTYPE
2812      sqlite3_snprintf(30, z+x, "declared type:");
2813      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2814#endif
2815#ifdef SQLITE_ENABLE_COLUMN_METADATA
2816      sqlite3_snprintf(30, z+x, "database name:");
2817      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2818      sqlite3_snprintf(30, z+x, "table name:");
2819      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2820      sqlite3_snprintf(30, z+x, "origin name:");
2821      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2822#endif
2823    }
2824  }
2825
2826  if( pArg->statsOn==3 ){
2827    if( pArg->pStmt ){
2828      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2829      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2830    }
2831    return 0;
2832  }
2833
2834  displayStatLine(pArg, "Memory Used:",
2835     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2836  displayStatLine(pArg, "Number of Outstanding Allocations:",
2837     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2838  if( pArg->shellFlgs & SHFLG_Pagecache ){
2839    displayStatLine(pArg, "Number of Pcache Pages Used:",
2840       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2841  }
2842  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2843     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2844  displayStatLine(pArg, "Largest Allocation:",
2845     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2846  displayStatLine(pArg, "Largest Pcache Allocation:",
2847     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2848#ifdef YYTRACKMAXSTACKDEPTH
2849  displayStatLine(pArg, "Deepest Parser Stack:",
2850     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2851#endif
2852
2853  if( db ){
2854    if( pArg->shellFlgs & SHFLG_Lookaside ){
2855      iHiwtr = iCur = -1;
2856      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2857                        &iCur, &iHiwtr, bReset);
2858      raw_printf(pArg->out,
2859              "Lookaside Slots Used:                %d (max %d)\n",
2860              iCur, iHiwtr);
2861      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2862                        &iCur, &iHiwtr, bReset);
2863      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2864              iHiwtr);
2865      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2866                        &iCur, &iHiwtr, bReset);
2867      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2868              iHiwtr);
2869      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2870                        &iCur, &iHiwtr, bReset);
2871      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2872              iHiwtr);
2873    }
2874    iHiwtr = iCur = -1;
2875    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2876    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2877            iCur);
2878    iHiwtr = iCur = -1;
2879    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2880    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2881    iHiwtr = iCur = -1;
2882    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2883    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2884    iHiwtr = iCur = -1;
2885    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2886    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2887    iHiwtr = iCur = -1;
2888    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2889    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2890    iHiwtr = iCur = -1;
2891    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2892    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2893            iCur);
2894    iHiwtr = iCur = -1;
2895    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2896    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2897            iCur);
2898  }
2899
2900  if( pArg->pStmt ){
2901    int iHit, iMiss;
2902    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2903                               bReset);
2904    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2905    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2906    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2907    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2908    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2909    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);
2910    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);
2911    if( iHit || iMiss ){
2912      raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",
2913            iHit, iHit+iMiss);
2914    }
2915    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2916    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2917    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2918    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2919    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2920    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2921    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2922    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2923  }
2924
2925#ifdef __linux__
2926  displayLinuxIoStats(pArg->out);
2927#endif
2928
2929  /* Do not remove this machine readable comment: extra-stats-output-here */
2930
2931  return 0;
2932}
2933
2934/*
2935** Display scan stats.
2936*/
2937static void display_scanstats(
2938  sqlite3 *db,                    /* Database to query */
2939  ShellState *pArg                /* Pointer to ShellState */
2940){
2941#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2942  UNUSED_PARAMETER(db);
2943  UNUSED_PARAMETER(pArg);
2944#else
2945  int i, k, n, mx;
2946  raw_printf(pArg->out, "-------- scanstats --------\n");
2947  mx = 0;
2948  for(k=0; k<=mx; k++){
2949    double rEstLoop = 1.0;
2950    for(i=n=0; 1; i++){
2951      sqlite3_stmt *p = pArg->pStmt;
2952      sqlite3_int64 nLoop, nVisit;
2953      double rEst;
2954      int iSid;
2955      const char *zExplain;
2956      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2957        break;
2958      }
2959      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2960      if( iSid>mx ) mx = iSid;
2961      if( iSid!=k ) continue;
2962      if( n==0 ){
2963        rEstLoop = (double)nLoop;
2964        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2965      }
2966      n++;
2967      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2968      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2969      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2970      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2971      rEstLoop *= rEst;
2972      raw_printf(pArg->out,
2973          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2974          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2975      );
2976    }
2977  }
2978  raw_printf(pArg->out, "---------------------------\n");
2979#endif
2980}
2981
2982/*
2983** Parameter azArray points to a zero-terminated array of strings. zStr
2984** points to a single nul-terminated string. Return non-zero if zStr
2985** is equal, according to strcmp(), to any of the strings in the array.
2986** Otherwise, return zero.
2987*/
2988static int str_in_array(const char *zStr, const char **azArray){
2989  int i;
2990  for(i=0; azArray[i]; i++){
2991    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2992  }
2993  return 0;
2994}
2995
2996/*
2997** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2998** and populate the ShellState.aiIndent[] array with the number of
2999** spaces each opcode should be indented before it is output.
3000**
3001** The indenting rules are:
3002**
3003**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3004**       all opcodes that occur between the p2 jump destination and the opcode
3005**       itself by 2 spaces.
3006**
3007**     * Do the previous for "Return" instructions for when P2 is positive.
3008**       See tag-20220407a in wherecode.c and vdbe.c.
3009**
3010**     * For each "Goto", if the jump destination is earlier in the program
3011**       and ends on one of:
3012**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
3013**       or if the P1 parameter is one instead of zero,
3014**       then indent all opcodes between the earlier instruction
3015**       and "Goto" by 2 spaces.
3016*/
3017static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3018  const char *zSql;               /* The text of the SQL statement */
3019  const char *z;                  /* Used to check if this is an EXPLAIN */
3020  int *abYield = 0;               /* True if op is an OP_Yield */
3021  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
3022  int iOp;                        /* Index of operation in p->aiIndent[] */
3023
3024  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3025                           "Return", 0 };
3026  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3027                            "Rewind", 0 };
3028  const char *azGoto[] = { "Goto", 0 };
3029
3030  /* Try to figure out if this is really an EXPLAIN statement. If this
3031  ** cannot be verified, return early.  */
3032  if( sqlite3_column_count(pSql)!=8 ){
3033    p->cMode = p->mode;
3034    return;
3035  }
3036  zSql = sqlite3_sql(pSql);
3037  if( zSql==0 ) return;
3038  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3039  if( sqlite3_strnicmp(z, "explain", 7) ){
3040    p->cMode = p->mode;
3041    return;
3042  }
3043
3044  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3045    int i;
3046    int iAddr = sqlite3_column_int(pSql, 0);
3047    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3048
3049    /* Set p2 to the P2 field of the current opcode. Then, assuming that
3050    ** p2 is an instruction address, set variable p2op to the index of that
3051    ** instruction in the aiIndent[] array. p2 and p2op may be different if
3052    ** the current instruction is part of a sub-program generated by an
3053    ** SQL trigger or foreign key.  */
3054    int p2 = sqlite3_column_int(pSql, 3);
3055    int p2op = (p2 + (iOp-iAddr));
3056
3057    /* Grow the p->aiIndent array as required */
3058    if( iOp>=nAlloc ){
3059      if( iOp==0 ){
3060        /* Do further verfication that this is explain output.  Abort if
3061        ** it is not */
3062        static const char *explainCols[] = {
3063           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3064        int jj;
3065        for(jj=0; jj<ArraySize(explainCols); jj++){
3066          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3067            p->cMode = p->mode;
3068            sqlite3_reset(pSql);
3069            return;
3070          }
3071        }
3072      }
3073      nAlloc += 100;
3074      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3075      shell_check_oom(p->aiIndent);
3076      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3077      shell_check_oom(abYield);
3078    }
3079    abYield[iOp] = str_in_array(zOp, azYield);
3080    p->aiIndent[iOp] = 0;
3081    p->nIndent = iOp+1;
3082
3083    if( str_in_array(zOp, azNext) && p2op>0 ){
3084      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3085    }
3086    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3087     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3088    ){
3089      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3090    }
3091  }
3092
3093  p->iIndent = 0;
3094  sqlite3_free(abYield);
3095  sqlite3_reset(pSql);
3096}
3097
3098/*
3099** Free the array allocated by explain_data_prepare().
3100*/
3101static void explain_data_delete(ShellState *p){
3102  sqlite3_free(p->aiIndent);
3103  p->aiIndent = 0;
3104  p->nIndent = 0;
3105  p->iIndent = 0;
3106}
3107
3108/*
3109** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3110*/
3111static unsigned int savedSelectTrace;
3112static unsigned int savedWhereTrace;
3113static void disable_debug_trace_modes(void){
3114  unsigned int zero = 0;
3115  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3116  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3117  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3118  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3119}
3120static void restore_debug_trace_modes(void){
3121  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3122  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3123}
3124
3125/* Create the TEMP table used to store parameter bindings */
3126static void bind_table_init(ShellState *p){
3127  int wrSchema = 0;
3128  int defensiveMode = 0;
3129  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3130  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3131  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3132  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3133  sqlite3_exec(p->db,
3134    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3135    "  key TEXT PRIMARY KEY,\n"
3136    "  value\n"
3137    ") WITHOUT ROWID;",
3138    0, 0, 0);
3139  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3140  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3141}
3142
3143/*
3144** Bind parameters on a prepared statement.
3145**
3146** Parameter bindings are taken from a TEMP table of the form:
3147**
3148**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3149**    WITHOUT ROWID;
3150**
3151** No bindings occur if this table does not exist.  The name of the table
3152** begins with "sqlite_" so that it will not collide with ordinary application
3153** tables.  The table must be in the TEMP schema.
3154*/
3155static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3156  int nVar;
3157  int i;
3158  int rc;
3159  sqlite3_stmt *pQ = 0;
3160
3161  nVar = sqlite3_bind_parameter_count(pStmt);
3162  if( nVar==0 ) return;  /* Nothing to do */
3163  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3164                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3165    return; /* Parameter table does not exist */
3166  }
3167  rc = sqlite3_prepare_v2(pArg->db,
3168          "SELECT value FROM temp.sqlite_parameters"
3169          " WHERE key=?1", -1, &pQ, 0);
3170  if( rc || pQ==0 ) return;
3171  for(i=1; i<=nVar; i++){
3172    char zNum[30];
3173    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3174    if( zVar==0 ){
3175      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3176      zVar = zNum;
3177    }
3178    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3179    if( sqlite3_step(pQ)==SQLITE_ROW ){
3180      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3181    }else{
3182      sqlite3_bind_null(pStmt, i);
3183    }
3184    sqlite3_reset(pQ);
3185  }
3186  sqlite3_finalize(pQ);
3187}
3188
3189/*
3190** UTF8 box-drawing characters.  Imagine box lines like this:
3191**
3192**           1
3193**           |
3194**       4 --+-- 2
3195**           |
3196**           3
3197**
3198** Each box characters has between 2 and 4 of the lines leading from
3199** the center.  The characters are here identified by the numbers of
3200** their corresponding lines.
3201*/
3202#define BOX_24   "\342\224\200"  /* U+2500 --- */
3203#define BOX_13   "\342\224\202"  /* U+2502  |  */
3204#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3205#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3206#define BOX_12   "\342\224\224"  /* U+2514  '- */
3207#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3208#define BOX_123  "\342\224\234"  /* U+251c  |- */
3209#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3210#define BOX_234  "\342\224\254"  /* U+252c -,- */
3211#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3212#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3213
3214/* Draw horizontal line N characters long using unicode box
3215** characters
3216*/
3217static void print_box_line(FILE *out, int N){
3218  const char zDash[] =
3219      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3220      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3221  const int nDash = sizeof(zDash) - 1;
3222  N *= 3;
3223  while( N>nDash ){
3224    utf8_printf(out, zDash);
3225    N -= nDash;
3226  }
3227  utf8_printf(out, "%.*s", N, zDash);
3228}
3229
3230/*
3231** Draw a horizontal separator for a MODE_Box table.
3232*/
3233static void print_box_row_separator(
3234  ShellState *p,
3235  int nArg,
3236  const char *zSep1,
3237  const char *zSep2,
3238  const char *zSep3
3239){
3240  int i;
3241  if( nArg>0 ){
3242    utf8_printf(p->out, "%s", zSep1);
3243    print_box_line(p->out, p->actualWidth[0]+2);
3244    for(i=1; i<nArg; i++){
3245      utf8_printf(p->out, "%s", zSep2);
3246      print_box_line(p->out, p->actualWidth[i]+2);
3247    }
3248    utf8_printf(p->out, "%s", zSep3);
3249  }
3250  fputs("\n", p->out);
3251}
3252
3253/*
3254** z[] is a line of text that is to be displayed the .mode box or table or
3255** similar tabular formats.  z[] might contain control characters such
3256** as \n, \t, \f, or \r.
3257**
3258** Compute characters to display on the first line of z[].  Stop at the
3259** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
3260** from malloc()) of that first line, which caller should free sometime.
3261** Write anything to display on the next line into *pzTail.  If this is
3262** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3263*/
3264static char *translateForDisplayAndDup(
3265  const unsigned char *z,            /* Input text to be transformed */
3266  const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
3267  int mxWidth,                       /* Max width.  0 means no limit */
3268  u8 bWordWrap                       /* If true, avoid breaking mid-word */
3269){
3270  int i;                 /* Input bytes consumed */
3271  int j;                 /* Output bytes generated */
3272  int k;                 /* Input bytes to be displayed */
3273  int n;                 /* Output column number */
3274  unsigned char *zOut;   /* Output text */
3275
3276  if( z==0 ){
3277    *pzTail = 0;
3278    return 0;
3279  }
3280  if( mxWidth<0 ) mxWidth = -mxWidth;
3281  if( mxWidth==0 ) mxWidth = 1000000;
3282  i = j = n = 0;
3283  while( n<mxWidth ){
3284    if( z[i]>=' ' ){
3285      n++;
3286      do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
3287      continue;
3288    }
3289    if( z[i]=='\t' ){
3290      do{
3291        n++;
3292        j++;
3293      }while( (n&7)!=0 && n<mxWidth );
3294      i++;
3295      continue;
3296    }
3297    break;
3298  }
3299  if( n>=mxWidth && bWordWrap  ){
3300    /* Perhaps try to back up to a better place to break the line */
3301    for(k=i; k>i/2; k--){
3302      if( isspace(z[k-1]) ) break;
3303    }
3304    if( k<=i/2 ){
3305      for(k=i; k>i/2; k--){
3306        if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3307      }
3308    }
3309    if( k<=i/2 ){
3310      k = i;
3311    }else{
3312      i = k;
3313      while( z[i]==' ' ) i++;
3314    }
3315  }else{
3316    k = i;
3317  }
3318  if( n>=mxWidth && z[i]>=' ' ){
3319   *pzTail = &z[i];
3320  }else if( z[i]=='\r' && z[i+1]=='\n' ){
3321    *pzTail = z[i+2] ? &z[i+2] : 0;
3322  }else if( z[i]==0 || z[i+1]==0 ){
3323    *pzTail = 0;
3324  }else{
3325    *pzTail = &z[i+1];
3326  }
3327  zOut = malloc( j+1 );
3328  shell_check_oom(zOut);
3329  i = j = n = 0;
3330  while( i<k ){
3331    if( z[i]>=' ' ){
3332      n++;
3333      do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
3334      continue;
3335    }
3336    if( z[i]=='\t' ){
3337      do{
3338        n++;
3339        zOut[j++] = ' ';
3340      }while( (n&7)!=0 && n<mxWidth );
3341      i++;
3342      continue;
3343    }
3344    break;
3345  }
3346  zOut[j] = 0;
3347  return (char*)zOut;
3348}
3349
3350/* Extract the value of the i-th current column for pStmt as an SQL literal
3351** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
3352** the caller.
3353*/
3354static char *quoted_column(sqlite3_stmt *pStmt, int i){
3355  switch( sqlite3_column_type(pStmt, i) ){
3356    case SQLITE_NULL: {
3357      return sqlite3_mprintf("NULL");
3358    }
3359    case SQLITE_INTEGER:
3360    case SQLITE_FLOAT: {
3361      return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3362    }
3363    case SQLITE_TEXT: {
3364      return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3365    }
3366    case SQLITE_BLOB: {
3367      int j;
3368      sqlite3_str *pStr = sqlite3_str_new(0);
3369      const unsigned char *a = sqlite3_column_blob(pStmt,i);
3370      int n = sqlite3_column_bytes(pStmt,i);
3371      sqlite3_str_append(pStr, "x'", 2);
3372      for(j=0; j<n; j++){
3373        sqlite3_str_appendf(pStr, "%02x", a[j]);
3374      }
3375      sqlite3_str_append(pStr, "'", 1);
3376      return sqlite3_str_finish(pStr);
3377    }
3378  }
3379  return 0; /* Not reached */
3380}
3381
3382/*
3383** Run a prepared statement and output the result in one of the
3384** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3385** or MODE_Box.
3386**
3387** This is different from ordinary exec_prepared_stmt() in that
3388** it has to run the entire query and gather the results into memory
3389** first, in order to determine column widths, before providing
3390** any output.
3391*/
3392static void exec_prepared_stmt_columnar(
3393  ShellState *p,                        /* Pointer to ShellState */
3394  sqlite3_stmt *pStmt                   /* Statment to run */
3395){
3396  sqlite3_int64 nRow = 0;
3397  int nColumn = 0;
3398  char **azData = 0;
3399  sqlite3_int64 nAlloc = 0;
3400  char *abRowDiv = 0;
3401  const unsigned char *uz;
3402  const char *z;
3403  char **azQuoted = 0;
3404  int rc;
3405  sqlite3_int64 i, nData;
3406  int j, nTotal, w, n;
3407  const char *colSep = 0;
3408  const char *rowSep = 0;
3409  const unsigned char **azNextLine = 0;
3410  int bNextLine = 0;
3411  int bMultiLineRowExists = 0;
3412  int bw = p->cmOpts.bWordWrap;
3413  const char *zEmpty = "";
3414  const char *zShowNull = p->nullValue;
3415
3416  rc = sqlite3_step(pStmt);
3417  if( rc!=SQLITE_ROW ) return;
3418  nColumn = sqlite3_column_count(pStmt);
3419  nAlloc = nColumn*4;
3420  if( nAlloc<=0 ) nAlloc = 1;
3421  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3422  shell_check_oom(azData);
3423  azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
3424  shell_check_oom((void*)azNextLine);
3425  memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
3426  if( p->cmOpts.bQuote ){
3427    azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
3428    shell_check_oom(azQuoted);
3429    memset(azQuoted, 0, nColumn*sizeof(char*) );
3430  }
3431  abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
3432  shell_check_oom(abRowDiv);
3433  if( nColumn>p->nWidth ){
3434    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3435    shell_check_oom(p->colWidth);
3436    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3437    p->nWidth = nColumn;
3438    p->actualWidth = &p->colWidth[nColumn];
3439  }
3440  memset(p->actualWidth, 0, nColumn*sizeof(int));
3441  for(i=0; i<nColumn; i++){
3442    w = p->colWidth[i];
3443    if( w<0 ) w = -w;
3444    p->actualWidth[i] = w;
3445  }
3446  for(i=0; i<nColumn; i++){
3447    const unsigned char *zNotUsed;
3448    int wx = p->colWidth[i];
3449    if( wx==0 ){
3450      wx = p->cmOpts.iWrap;
3451    }
3452    if( wx<0 ) wx = -wx;
3453    uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
3454    azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
3455  }
3456  do{
3457    int useNextLine = bNextLine;
3458    bNextLine = 0;
3459    if( (nRow+2)*nColumn >= nAlloc ){
3460      nAlloc *= 2;
3461      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3462      shell_check_oom(azData);
3463      abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
3464      shell_check_oom(abRowDiv);
3465    }
3466    abRowDiv[nRow] = 1;
3467    nRow++;
3468    for(i=0; i<nColumn; i++){
3469      int wx = p->colWidth[i];
3470      if( wx==0 ){
3471        wx = p->cmOpts.iWrap;
3472      }
3473      if( wx<0 ) wx = -wx;
3474      if( useNextLine ){
3475        uz = azNextLine[i];
3476        if( uz==0 ) uz = (u8*)zEmpty;
3477      }else if( p->cmOpts.bQuote ){
3478        sqlite3_free(azQuoted[i]);
3479        azQuoted[i] = quoted_column(pStmt,i);
3480        uz = (const unsigned char*)azQuoted[i];
3481      }else{
3482        uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
3483        if( uz==0 ) uz = (u8*)zShowNull;
3484      }
3485      azData[nRow*nColumn + i]
3486        = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
3487      if( azNextLine[i] ){
3488        bNextLine = 1;
3489        abRowDiv[nRow-1] = 0;
3490        bMultiLineRowExists = 1;
3491      }
3492    }
3493  }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
3494  nTotal = nColumn*(nRow+1);
3495  for(i=0; i<nTotal; i++){
3496    z = azData[i];
3497    if( z==0 ) z = (char*)zEmpty;
3498    n = strlenChar(z);
3499    j = i%nColumn;
3500    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3501  }
3502  if( seenInterrupt ) goto columnar_end;
3503  if( nColumn==0 ) goto columnar_end;
3504  switch( p->cMode ){
3505    case MODE_Column: {
3506      colSep = "  ";
3507      rowSep = "\n";
3508      if( p->showHeader ){
3509        for(i=0; i<nColumn; i++){
3510          w = p->actualWidth[i];
3511          if( p->colWidth[i]<0 ) w = -w;
3512          utf8_width_print(p->out, w, azData[i]);
3513          fputs(i==nColumn-1?"\n":"  ", p->out);
3514        }
3515        for(i=0; i<nColumn; i++){
3516          print_dashes(p->out, p->actualWidth[i]);
3517          fputs(i==nColumn-1?"\n":"  ", p->out);
3518        }
3519      }
3520      break;
3521    }
3522    case MODE_Table: {
3523      colSep = " | ";
3524      rowSep = " |\n";
3525      print_row_separator(p, nColumn, "+");
3526      fputs("| ", p->out);
3527      for(i=0; i<nColumn; i++){
3528        w = p->actualWidth[i];
3529        n = strlenChar(azData[i]);
3530        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3531        fputs(i==nColumn-1?" |\n":" | ", p->out);
3532      }
3533      print_row_separator(p, nColumn, "+");
3534      break;
3535    }
3536    case MODE_Markdown: {
3537      colSep = " | ";
3538      rowSep = " |\n";
3539      fputs("| ", p->out);
3540      for(i=0; i<nColumn; i++){
3541        w = p->actualWidth[i];
3542        n = strlenChar(azData[i]);
3543        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3544        fputs(i==nColumn-1?" |\n":" | ", p->out);
3545      }
3546      print_row_separator(p, nColumn, "|");
3547      break;
3548    }
3549    case MODE_Box: {
3550      colSep = " " BOX_13 " ";
3551      rowSep = " " BOX_13 "\n";
3552      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3553      utf8_printf(p->out, BOX_13 " ");
3554      for(i=0; i<nColumn; i++){
3555        w = p->actualWidth[i];
3556        n = strlenChar(azData[i]);
3557        utf8_printf(p->out, "%*s%s%*s%s",
3558            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3559            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3560      }
3561      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3562      break;
3563    }
3564  }
3565  for(i=nColumn, j=0; i<nTotal; i++, j++){
3566    if( j==0 && p->cMode!=MODE_Column ){
3567      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3568    }
3569    z = azData[i];
3570    if( z==0 ) z = p->nullValue;
3571    w = p->actualWidth[j];
3572    if( p->colWidth[j]<0 ) w = -w;
3573    utf8_width_print(p->out, w, z);
3574    if( j==nColumn-1 ){
3575      utf8_printf(p->out, "%s", rowSep);
3576      if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
3577        if( p->cMode==MODE_Table ){
3578          print_row_separator(p, nColumn, "+");
3579        }else if( p->cMode==MODE_Box ){
3580          print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3581        }else if( p->cMode==MODE_Column ){
3582          raw_printf(p->out, "\n");
3583        }
3584      }
3585      j = -1;
3586      if( seenInterrupt ) goto columnar_end;
3587    }else{
3588      utf8_printf(p->out, "%s", colSep);
3589    }
3590  }
3591  if( p->cMode==MODE_Table ){
3592    print_row_separator(p, nColumn, "+");
3593  }else if( p->cMode==MODE_Box ){
3594    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3595  }
3596columnar_end:
3597  if( seenInterrupt ){
3598    utf8_printf(p->out, "Interrupt\n");
3599  }
3600  nData = (nRow+1)*nColumn;
3601  for(i=0; i<nData; i++){
3602    z = azData[i];
3603    if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
3604  }
3605  sqlite3_free(azData);
3606  sqlite3_free((void*)azNextLine);
3607  sqlite3_free(abRowDiv);
3608  if( azQuoted ){
3609    for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
3610    sqlite3_free(azQuoted);
3611  }
3612}
3613
3614/*
3615** Run a prepared statement
3616*/
3617static void exec_prepared_stmt(
3618  ShellState *pArg,                                /* Pointer to ShellState */
3619  sqlite3_stmt *pStmt                              /* Statment to run */
3620){
3621  int rc;
3622  sqlite3_uint64 nRow = 0;
3623
3624  if( pArg->cMode==MODE_Column
3625   || pArg->cMode==MODE_Table
3626   || pArg->cMode==MODE_Box
3627   || pArg->cMode==MODE_Markdown
3628  ){
3629    exec_prepared_stmt_columnar(pArg, pStmt);
3630    return;
3631  }
3632
3633  /* perform the first step.  this will tell us if we
3634  ** have a result set or not and how wide it is.
3635  */
3636  rc = sqlite3_step(pStmt);
3637  /* if we have a result set... */
3638  if( SQLITE_ROW == rc ){
3639    /* allocate space for col name ptr, value ptr, and type */
3640    int nCol = sqlite3_column_count(pStmt);
3641    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3642    if( !pData ){
3643      shell_out_of_memory();
3644    }else{
3645      char **azCols = (char **)pData;      /* Names of result columns */
3646      char **azVals = &azCols[nCol];       /* Results */
3647      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3648      int i, x;
3649      assert(sizeof(int) <= sizeof(char *));
3650      /* save off ptrs to column names */
3651      for(i=0; i<nCol; i++){
3652        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3653      }
3654      do{
3655        nRow++;
3656        /* extract the data and data types */
3657        for(i=0; i<nCol; i++){
3658          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3659          if( x==SQLITE_BLOB
3660           && pArg
3661           && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
3662          ){
3663            azVals[i] = "";
3664          }else{
3665            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3666          }
3667          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3668            rc = SQLITE_NOMEM;
3669            break; /* from for */
3670          }
3671        } /* end for */
3672
3673        /* if data and types extracted successfully... */
3674        if( SQLITE_ROW == rc ){
3675          /* call the supplied callback with the result row data */
3676          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3677            rc = SQLITE_ABORT;
3678          }else{
3679            rc = sqlite3_step(pStmt);
3680          }
3681        }
3682      } while( SQLITE_ROW == rc );
3683      sqlite3_free(pData);
3684      if( pArg->cMode==MODE_Json ){
3685        fputs("]\n", pArg->out);
3686      }else if( pArg->cMode==MODE_Count ){
3687        char zBuf[200];
3688        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
3689                         nRow, nRow!=1 ? "s" : "");
3690        printf("%s", zBuf);
3691      }
3692    }
3693  }
3694}
3695
3696#ifndef SQLITE_OMIT_VIRTUALTABLE
3697/*
3698** This function is called to process SQL if the previous shell command
3699** was ".expert". It passes the SQL in the second argument directly to
3700** the sqlite3expert object.
3701**
3702** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3703** code. In this case, (*pzErr) may be set to point to a buffer containing
3704** an English language error message. It is the responsibility of the
3705** caller to eventually free this buffer using sqlite3_free().
3706*/
3707static int expertHandleSQL(
3708  ShellState *pState,
3709  const char *zSql,
3710  char **pzErr
3711){
3712  assert( pState->expert.pExpert );
3713  assert( pzErr==0 || *pzErr==0 );
3714  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3715}
3716
3717/*
3718** This function is called either to silently clean up the object
3719** created by the ".expert" command (if bCancel==1), or to generate a
3720** report from it and then clean it up (if bCancel==0).
3721**
3722** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3723** code. In this case, (*pzErr) may be set to point to a buffer containing
3724** an English language error message. It is the responsibility of the
3725** caller to eventually free this buffer using sqlite3_free().
3726*/
3727static int expertFinish(
3728  ShellState *pState,
3729  int bCancel,
3730  char **pzErr
3731){
3732  int rc = SQLITE_OK;
3733  sqlite3expert *p = pState->expert.pExpert;
3734  assert( p );
3735  assert( bCancel || pzErr==0 || *pzErr==0 );
3736  if( bCancel==0 ){
3737    FILE *out = pState->out;
3738    int bVerbose = pState->expert.bVerbose;
3739
3740    rc = sqlite3_expert_analyze(p, pzErr);
3741    if( rc==SQLITE_OK ){
3742      int nQuery = sqlite3_expert_count(p);
3743      int i;
3744
3745      if( bVerbose ){
3746        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3747        raw_printf(out, "-- Candidates -----------------------------\n");
3748        raw_printf(out, "%s\n", zCand);
3749      }
3750      for(i=0; i<nQuery; i++){
3751        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3752        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3753        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3754        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3755        if( bVerbose ){
3756          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3757          raw_printf(out, "%s\n\n", zSql);
3758        }
3759        raw_printf(out, "%s\n", zIdx);
3760        raw_printf(out, "%s\n", zEQP);
3761      }
3762    }
3763  }
3764  sqlite3_expert_destroy(p);
3765  pState->expert.pExpert = 0;
3766  return rc;
3767}
3768
3769/*
3770** Implementation of ".expert" dot command.
3771*/
3772static int expertDotCommand(
3773  ShellState *pState,             /* Current shell tool state */
3774  char **azArg,                   /* Array of arguments passed to dot command */
3775  int nArg                        /* Number of entries in azArg[] */
3776){
3777  int rc = SQLITE_OK;
3778  char *zErr = 0;
3779  int i;
3780  int iSample = 0;
3781
3782  assert( pState->expert.pExpert==0 );
3783  memset(&pState->expert, 0, sizeof(ExpertInfo));
3784
3785  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3786    char *z = azArg[i];
3787    int n;
3788    if( z[0]=='-' && z[1]=='-' ) z++;
3789    n = strlen30(z);
3790    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3791      pState->expert.bVerbose = 1;
3792    }
3793    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3794      if( i==(nArg-1) ){
3795        raw_printf(stderr, "option requires an argument: %s\n", z);
3796        rc = SQLITE_ERROR;
3797      }else{
3798        iSample = (int)integerValue(azArg[++i]);
3799        if( iSample<0 || iSample>100 ){
3800          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3801          rc = SQLITE_ERROR;
3802        }
3803      }
3804    }
3805    else{
3806      raw_printf(stderr, "unknown option: %s\n", z);
3807      rc = SQLITE_ERROR;
3808    }
3809  }
3810
3811  if( rc==SQLITE_OK ){
3812    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3813    if( pState->expert.pExpert==0 ){
3814      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
3815      rc = SQLITE_ERROR;
3816    }else{
3817      sqlite3_expert_config(
3818          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3819      );
3820    }
3821  }
3822  sqlite3_free(zErr);
3823
3824  return rc;
3825}
3826#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3827
3828/*
3829** Execute a statement or set of statements.  Print
3830** any result rows/columns depending on the current mode
3831** set via the supplied callback.
3832**
3833** This is very similar to SQLite's built-in sqlite3_exec()
3834** function except it takes a slightly different callback
3835** and callback data argument.
3836*/
3837static int shell_exec(
3838  ShellState *pArg,                         /* Pointer to ShellState */
3839  const char *zSql,                         /* SQL to be evaluated */
3840  char **pzErrMsg                           /* Error msg written here */
3841){
3842  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3843  int rc = SQLITE_OK;             /* Return Code */
3844  int rc2;
3845  const char *zLeftover;          /* Tail of unprocessed SQL */
3846  sqlite3 *db = pArg->db;
3847
3848  if( pzErrMsg ){
3849    *pzErrMsg = NULL;
3850  }
3851
3852#ifndef SQLITE_OMIT_VIRTUALTABLE
3853  if( pArg->expert.pExpert ){
3854    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3855    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3856  }
3857#endif
3858
3859  while( zSql[0] && (SQLITE_OK == rc) ){
3860    static const char *zStmtSql;
3861    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3862    if( SQLITE_OK != rc ){
3863      if( pzErrMsg ){
3864        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
3865      }
3866    }else{
3867      if( !pStmt ){
3868        /* this happens for a comment or white-space */
3869        zSql = zLeftover;
3870        while( IsSpace(zSql[0]) ) zSql++;
3871        continue;
3872      }
3873      zStmtSql = sqlite3_sql(pStmt);
3874      if( zStmtSql==0 ) zStmtSql = "";
3875      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3876
3877      /* save off the prepared statment handle and reset row count */
3878      if( pArg ){
3879        pArg->pStmt = pStmt;
3880        pArg->cnt = 0;
3881      }
3882
3883      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3884      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3885        sqlite3_stmt *pExplain;
3886        char *zEQP;
3887        int triggerEQP = 0;
3888        disable_debug_trace_modes();
3889        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3890        if( pArg->autoEQP>=AUTOEQP_trigger ){
3891          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3892        }
3893        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3894        shell_check_oom(zEQP);
3895        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3896        if( rc==SQLITE_OK ){
3897          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3898            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3899            int iEqpId = sqlite3_column_int(pExplain, 0);
3900            int iParentId = sqlite3_column_int(pExplain, 1);
3901            if( zEQPLine==0 ) zEQPLine = "";
3902            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3903            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3904          }
3905          eqp_render(pArg);
3906        }
3907        sqlite3_finalize(pExplain);
3908        sqlite3_free(zEQP);
3909        if( pArg->autoEQP>=AUTOEQP_full ){
3910          /* Also do an EXPLAIN for ".eqp full" mode */
3911          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3912          shell_check_oom(zEQP);
3913          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3914          if( rc==SQLITE_OK ){
3915            pArg->cMode = MODE_Explain;
3916            explain_data_prepare(pArg, pExplain);
3917            exec_prepared_stmt(pArg, pExplain);
3918            explain_data_delete(pArg);
3919          }
3920          sqlite3_finalize(pExplain);
3921          sqlite3_free(zEQP);
3922        }
3923        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3924          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3925          /* Reprepare pStmt before reactiving trace modes */
3926          sqlite3_finalize(pStmt);
3927          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3928          if( pArg ) pArg->pStmt = pStmt;
3929        }
3930        restore_debug_trace_modes();
3931      }
3932
3933      if( pArg ){
3934        pArg->cMode = pArg->mode;
3935        if( pArg->autoExplain ){
3936          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3937            pArg->cMode = MODE_Explain;
3938          }
3939          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3940            pArg->cMode = MODE_EQP;
3941          }
3942        }
3943
3944        /* If the shell is currently in ".explain" mode, gather the extra
3945        ** data required to add indents to the output.*/
3946        if( pArg->cMode==MODE_Explain ){
3947          explain_data_prepare(pArg, pStmt);
3948        }
3949      }
3950
3951      bind_prepared_stmt(pArg, pStmt);
3952      exec_prepared_stmt(pArg, pStmt);
3953      explain_data_delete(pArg);
3954      eqp_render(pArg);
3955
3956      /* print usage stats if stats on */
3957      if( pArg && pArg->statsOn ){
3958        display_stats(db, pArg, 0);
3959      }
3960
3961      /* print loop-counters if required */
3962      if( pArg && pArg->scanstatsOn ){
3963        display_scanstats(db, pArg);
3964      }
3965
3966      /* Finalize the statement just executed. If this fails, save a
3967      ** copy of the error message. Otherwise, set zSql to point to the
3968      ** next statement to execute. */
3969      rc2 = sqlite3_finalize(pStmt);
3970      if( rc!=SQLITE_NOMEM ) rc = rc2;
3971      if( rc==SQLITE_OK ){
3972        zSql = zLeftover;
3973        while( IsSpace(zSql[0]) ) zSql++;
3974      }else if( pzErrMsg ){
3975        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
3976      }
3977
3978      /* clear saved stmt handle */
3979      if( pArg ){
3980        pArg->pStmt = NULL;
3981      }
3982    }
3983  } /* end while */
3984
3985  return rc;
3986}
3987
3988/*
3989** Release memory previously allocated by tableColumnList().
3990*/
3991static void freeColumnList(char **azCol){
3992  int i;
3993  for(i=1; azCol[i]; i++){
3994    sqlite3_free(azCol[i]);
3995  }
3996  /* azCol[0] is a static string */
3997  sqlite3_free(azCol);
3998}
3999
4000/*
4001** Return a list of pointers to strings which are the names of all
4002** columns in table zTab.   The memory to hold the names is dynamically
4003** allocated and must be released by the caller using a subsequent call
4004** to freeColumnList().
4005**
4006** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
4007** value that needs to be preserved, then azCol[0] is filled in with the
4008** name of the rowid column.
4009**
4010** The first regular column in the table is azCol[1].  The list is terminated
4011** by an entry with azCol[i]==0.
4012*/
4013static char **tableColumnList(ShellState *p, const char *zTab){
4014  char **azCol = 0;
4015  sqlite3_stmt *pStmt;
4016  char *zSql;
4017  int nCol = 0;
4018  int nAlloc = 0;
4019  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
4020  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
4021  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4022  int rc;
4023
4024  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4025  shell_check_oom(zSql);
4026  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4027  sqlite3_free(zSql);
4028  if( rc ) return 0;
4029  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4030    if( nCol>=nAlloc-2 ){
4031      nAlloc = nAlloc*2 + nCol + 10;
4032      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4033      shell_check_oom(azCol);
4034    }
4035    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4036    shell_check_oom(azCol[nCol]);
4037    if( sqlite3_column_int(pStmt, 5) ){
4038      nPK++;
4039      if( nPK==1
4040       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4041                          "INTEGER")==0
4042      ){
4043        isIPK = 1;
4044      }else{
4045        isIPK = 0;
4046      }
4047    }
4048  }
4049  sqlite3_finalize(pStmt);
4050  if( azCol==0 ) return 0;
4051  azCol[0] = 0;
4052  azCol[nCol+1] = 0;
4053
4054  /* The decision of whether or not a rowid really needs to be preserved
4055  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
4056  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
4057  ** rowids on tables where the rowid is inaccessible because there are other
4058  ** columns in the table named "rowid", "_rowid_", and "oid".
4059  */
4060  if( preserveRowid && isIPK ){
4061    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4062    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
4063    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4064    ** ROWID aliases.  To distinguish these cases, check to see if
4065    ** there is a "pk" entry in "PRAGMA index_list".  There will be
4066    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4067    */
4068    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4069                           " WHERE origin='pk'", zTab);
4070    shell_check_oom(zSql);
4071    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4072    sqlite3_free(zSql);
4073    if( rc ){
4074      freeColumnList(azCol);
4075      return 0;
4076    }
4077    rc = sqlite3_step(pStmt);
4078    sqlite3_finalize(pStmt);
4079    preserveRowid = rc==SQLITE_ROW;
4080  }
4081  if( preserveRowid ){
4082    /* Only preserve the rowid if we can find a name to use for the
4083    ** rowid */
4084    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4085    int i, j;
4086    for(j=0; j<3; j++){
4087      for(i=1; i<=nCol; i++){
4088        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4089      }
4090      if( i>nCol ){
4091        /* At this point, we know that azRowid[j] is not the name of any
4092        ** ordinary column in the table.  Verify that azRowid[j] is a valid
4093        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
4094        ** tables will fail this last check */
4095        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4096        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4097        break;
4098      }
4099    }
4100  }
4101  return azCol;
4102}
4103
4104/*
4105** Toggle the reverse_unordered_selects setting.
4106*/
4107static void toggleSelectOrder(sqlite3 *db){
4108  sqlite3_stmt *pStmt = 0;
4109  int iSetting = 0;
4110  char zStmt[100];
4111  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4112  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4113    iSetting = sqlite3_column_int(pStmt, 0);
4114  }
4115  sqlite3_finalize(pStmt);
4116  sqlite3_snprintf(sizeof(zStmt), zStmt,
4117       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4118  sqlite3_exec(db, zStmt, 0, 0, 0);
4119}
4120
4121/*
4122** This is a different callback routine used for dumping the database.
4123** Each row received by this callback consists of a table name,
4124** the table type ("index" or "table") and SQL to create the table.
4125** This routine should print text sufficient to recreate the table.
4126*/
4127static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4128  int rc;
4129  const char *zTable;
4130  const char *zType;
4131  const char *zSql;
4132  ShellState *p = (ShellState *)pArg;
4133  int dataOnly;
4134  int noSys;
4135
4136  UNUSED_PARAMETER(azNotUsed);
4137  if( nArg!=3 || azArg==0 ) return 0;
4138  zTable = azArg[0];
4139  zType = azArg[1];
4140  zSql = azArg[2];
4141  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4142  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4143
4144  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4145    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4146  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4147    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4148  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
4149    return 0;
4150  }else if( dataOnly ){
4151    /* no-op */
4152  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4153    char *zIns;
4154    if( !p->writableSchema ){
4155      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4156      p->writableSchema = 1;
4157    }
4158    zIns = sqlite3_mprintf(
4159       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4160       "VALUES('table','%q','%q',0,'%q');",
4161       zTable, zTable, zSql);
4162    shell_check_oom(zIns);
4163    utf8_printf(p->out, "%s\n", zIns);
4164    sqlite3_free(zIns);
4165    return 0;
4166  }else{
4167    printSchemaLine(p->out, zSql, ";\n");
4168  }
4169
4170  if( strcmp(zType, "table")==0 ){
4171    ShellText sSelect;
4172    ShellText sTable;
4173    char **azCol;
4174    int i;
4175    char *savedDestTable;
4176    int savedMode;
4177
4178    azCol = tableColumnList(p, zTable);
4179    if( azCol==0 ){
4180      p->nErr++;
4181      return 0;
4182    }
4183
4184    /* Always quote the table name, even if it appears to be pure ascii,
4185    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
4186    initText(&sTable);
4187    appendText(&sTable, zTable, quoteChar(zTable));
4188    /* If preserving the rowid, add a column list after the table name.
4189    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4190    ** instead of the usual "INSERT INTO tab VALUES(...)".
4191    */
4192    if( azCol[0] ){
4193      appendText(&sTable, "(", 0);
4194      appendText(&sTable, azCol[0], 0);
4195      for(i=1; azCol[i]; i++){
4196        appendText(&sTable, ",", 0);
4197        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4198      }
4199      appendText(&sTable, ")", 0);
4200    }
4201
4202    /* Build an appropriate SELECT statement */
4203    initText(&sSelect);
4204    appendText(&sSelect, "SELECT ", 0);
4205    if( azCol[0] ){
4206      appendText(&sSelect, azCol[0], 0);
4207      appendText(&sSelect, ",", 0);
4208    }
4209    for(i=1; azCol[i]; i++){
4210      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4211      if( azCol[i+1] ){
4212        appendText(&sSelect, ",", 0);
4213      }
4214    }
4215    freeColumnList(azCol);
4216    appendText(&sSelect, " FROM ", 0);
4217    appendText(&sSelect, zTable, quoteChar(zTable));
4218
4219    savedDestTable = p->zDestTable;
4220    savedMode = p->mode;
4221    p->zDestTable = sTable.z;
4222    p->mode = p->cMode = MODE_Insert;
4223    rc = shell_exec(p, sSelect.z, 0);
4224    if( (rc&0xff)==SQLITE_CORRUPT ){
4225      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4226      toggleSelectOrder(p->db);
4227      shell_exec(p, sSelect.z, 0);
4228      toggleSelectOrder(p->db);
4229    }
4230    p->zDestTable = savedDestTable;
4231    p->mode = savedMode;
4232    freeText(&sTable);
4233    freeText(&sSelect);
4234    if( rc ) p->nErr++;
4235  }
4236  return 0;
4237}
4238
4239/*
4240** Run zQuery.  Use dump_callback() as the callback routine so that
4241** the contents of the query are output as SQL statements.
4242**
4243** If we get a SQLITE_CORRUPT error, rerun the query after appending
4244** "ORDER BY rowid DESC" to the end.
4245*/
4246static int run_schema_dump_query(
4247  ShellState *p,
4248  const char *zQuery
4249){
4250  int rc;
4251  char *zErr = 0;
4252  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4253  if( rc==SQLITE_CORRUPT ){
4254    char *zQ2;
4255    int len = strlen30(zQuery);
4256    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4257    if( zErr ){
4258      utf8_printf(p->out, "/****** %s ******/\n", zErr);
4259      sqlite3_free(zErr);
4260      zErr = 0;
4261    }
4262    zQ2 = malloc( len+100 );
4263    if( zQ2==0 ) return rc;
4264    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4265    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4266    if( rc ){
4267      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4268    }else{
4269      rc = SQLITE_CORRUPT;
4270    }
4271    sqlite3_free(zErr);
4272    free(zQ2);
4273  }
4274  return rc;
4275}
4276
4277/*
4278** Text of help messages.
4279**
4280** The help text for each individual command begins with a line that starts
4281** with ".".  Subsequent lines are supplemental information.
4282**
4283** There must be two or more spaces between the end of the command and the
4284** start of the description of what that command does.
4285*/
4286static const char *(azHelp[]) = {
4287#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4288  && !defined(SQLITE_SHELL_FIDDLE)
4289  ".archive ...             Manage SQL archives",
4290  "   Each command must have exactly one of the following options:",
4291  "     -c, --create               Create a new archive",
4292  "     -u, --update               Add or update files with changed mtime",
4293  "     -i, --insert               Like -u but always add even if unchanged",
4294  "     -r, --remove               Remove files from archive",
4295  "     -t, --list                 List contents of archive",
4296  "     -x, --extract              Extract files from archive",
4297  "   Optional arguments:",
4298  "     -v, --verbose              Print each filename as it is processed",
4299  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
4300  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
4301  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
4302  "     -g, --glob                 Use glob matching for names in archive",
4303  "     -n, --dryrun               Show the SQL that would have occurred",
4304  "   Examples:",
4305  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
4306  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
4307  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
4308  "   See also:",
4309  "      http://sqlite.org/cli.html#sqlite_archive_support",
4310#endif
4311#ifndef SQLITE_OMIT_AUTHORIZATION
4312  ".auth ON|OFF             Show authorizer callbacks",
4313#endif
4314#ifndef SQLITE_SHELL_FIDDLE
4315  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
4316  "   Options:",
4317  "       --append            Use the appendvfs",
4318  "       --async             Write to FILE without journal and fsync()",
4319#endif
4320  ".bail on|off             Stop after hitting an error.  Default OFF",
4321  ".binary on|off           Turn binary output on or off.  Default OFF",
4322#ifndef SQLITE_SHELL_FIDDLE
4323  ".cd DIRECTORY            Change the working directory to DIRECTORY",
4324#endif
4325  ".changes on|off          Show number of rows changed by SQL",
4326#ifndef SQLITE_SHELL_FIDDLE
4327  ".check GLOB              Fail if output since .testcase does not match",
4328  ".clone NEWDB             Clone data into NEWDB from the existing database",
4329#endif
4330  ".connection [close] [#]  Open or close an auxiliary database connection",
4331  ".databases               List names and files of attached databases",
4332  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4333#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4334  ".dbinfo ?DB?             Show status information about the database",
4335#endif
4336  ".dump ?OBJECTS?          Render database content as SQL",
4337  "   Options:",
4338  "     --data-only            Output only INSERT statements",
4339  "     --newlines             Allow unescaped newline characters in output",
4340  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4341  "     --preserve-rowids      Include ROWID values in the output",
4342  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4343  "   Additional LIKE patterns can be given in subsequent arguments",
4344  ".echo on|off             Turn command echo on or off",
4345  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4346  "   Other Modes:",
4347#ifdef SQLITE_DEBUG
4348  "      test                  Show raw EXPLAIN QUERY PLAN output",
4349  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4350#endif
4351  "      trigger               Like \"full\" but also show trigger bytecode",
4352#ifndef SQLITE_SHELL_FIDDLE
4353  ".excel                   Display the output of next command in spreadsheet",
4354  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4355#endif
4356#ifndef SQLITE_SHELL_FIDDLE
4357  ".exit ?CODE?             Exit this program with return-code CODE",
4358#endif
4359  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4360  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4361  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4362  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4363  "   --help                  Show CMD details",
4364  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4365  ".headers on|off          Turn display of headers on or off",
4366  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4367#ifndef SQLITE_SHELL_FIDDLE
4368  ".import FILE TABLE       Import data from FILE into TABLE",
4369  "   Options:",
4370  "     --ascii               Use \\037 and \\036 as column and row separators",
4371  "     --csv                 Use , and \\n as column and row separators",
4372  "     --skip N              Skip the first N rows of input",
4373  "     --schema S            Target table to be S.TABLE",
4374  "     -v                    \"Verbose\" - increase auxiliary output",
4375  "   Notes:",
4376  "     *  If TABLE does not exist, it is created.  The first row of input",
4377  "        determines the column names.",
4378  "     *  If neither --csv or --ascii are used, the input mode is derived",
4379  "        from the \".mode\" output mode",
4380  "     *  If FILE begins with \"|\" then it is a command that generates the",
4381  "        input text.",
4382#endif
4383#ifndef SQLITE_OMIT_TEST_CONTROL
4384  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4385#endif
4386  ".indexes ?TABLE?         Show names of indexes",
4387  "                           If TABLE is specified, only show indexes for",
4388  "                           tables matching TABLE using the LIKE operator.",
4389#ifdef SQLITE_ENABLE_IOTRACE
4390  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4391#endif
4392  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4393  ".lint OPTIONS            Report potential schema issues.",
4394  "     Options:",
4395  "        fkey-indexes     Find missing foreign key indexes",
4396#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4397  ".load FILE ?ENTRY?       Load an extension library",
4398#endif
4399#ifndef SQLITE_SHELL_FIDDLE
4400  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4401#endif
4402  ".mode MODE ?OPTIONS?     Set output mode",
4403  "   MODE is one of:",
4404  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
4405  "     box         Tables using unicode box-drawing characters",
4406  "     csv         Comma-separated values",
4407  "     column      Output in columns.  (See .width)",
4408  "     html        HTML <table> code",
4409  "     insert      SQL insert statements for TABLE",
4410  "     json        Results in a JSON array",
4411  "     line        One value per line",
4412  "     list        Values delimited by \"|\"",
4413  "     markdown    Markdown table format",
4414  "     qbox        Shorthand for \"box --width 60 --quote\"",
4415  "     quote       Escape answers as for SQL",
4416  "     table       ASCII-art table",
4417  "     tabs        Tab-separated values",
4418  "     tcl         TCL list elements",
4419  "   OPTIONS: (for columnar modes or insert mode):",
4420  "     --wrap N       Wrap output lines to no longer than N characters",
4421  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
4422  "     --ww           Shorthand for \"--wordwrap 1\"",
4423  "     --quote        Quote output text as SQL literals",
4424  "     --noquote      Do not quote output text",
4425  "     TABLE          The name of SQL table used for \"insert\" mode",
4426#ifndef SQLITE_SHELL_FIDDLE
4427  ".nonce STRING            Suspend safe mode for one command if nonce matches",
4428#endif
4429  ".nullvalue STRING        Use STRING in place of NULL values",
4430#ifndef SQLITE_SHELL_FIDDLE
4431  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4432  "     If FILE begins with '|' then open as a pipe",
4433  "       --bom  Put a UTF8 byte-order mark at the beginning",
4434  "       -e     Send output to the system text editor",
4435  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4436  /* Note that .open is (partially) available in WASM builds but is
4437  ** currently only intended to be used by the fiddle tool, not
4438  ** end users, so is "undocumented." */
4439  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4440  "     Options:",
4441  "        --append        Use appendvfs to append database to the end of FILE",
4442#endif
4443#ifndef SQLITE_OMIT_DESERIALIZE
4444  "        --deserialize   Load into memory using sqlite3_deserialize()",
4445  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4446  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4447#endif
4448  "        --new           Initialize FILE to an empty database",
4449  "        --nofollow      Do not follow symbolic links",
4450  "        --readonly      Open FILE readonly",
4451  "        --zip           FILE is a ZIP archive",
4452#ifndef SQLITE_SHELL_FIDDLE
4453  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4454  "   If FILE begins with '|' then open it as a pipe.",
4455  "   Options:",
4456  "     --bom                 Prefix output with a UTF8 byte-order mark",
4457  "     -e                    Send output to the system text editor",
4458  "     -x                    Send output as CSV to a spreadsheet",
4459#endif
4460  ".parameter CMD ...       Manage SQL parameter bindings",
4461  "   clear                   Erase all bindings",
4462  "   init                    Initialize the TEMP table that holds bindings",
4463  "   list                    List the current parameter bindings",
4464  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4465  "                           PARAMETER should start with one of: $ : @ ?",
4466  "   unset PARAMETER         Remove PARAMETER from the binding table",
4467  ".print STRING...         Print literal STRING",
4468#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4469  ".progress N              Invoke progress handler after every N opcodes",
4470  "   --limit N                 Interrupt after N progress callbacks",
4471  "   --once                    Do no more than one progress interrupt",
4472  "   --quiet|-q                No output except at interrupts",
4473  "   --reset                   Reset the count for each input and interrupt",
4474#endif
4475  ".prompt MAIN CONTINUE    Replace the standard prompts",
4476#ifndef SQLITE_SHELL_FIDDLE
4477  ".quit                    Exit this program",
4478  ".read FILE               Read input from FILE or command output",
4479  "    If FILE begins with \"|\", it is a command that generates the input.",
4480#endif
4481#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4482  ".recover                 Recover as much data as possible from corrupt db.",
4483  "   --freelist-corrupt       Assume the freelist is corrupt",
4484  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4485  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4486  "   --no-rowids              Do not attempt to recover rowid values",
4487  "                            that are not also INTEGER PRIMARY KEYs",
4488#endif
4489#ifndef SQLITE_SHELL_FIDDLE
4490  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4491  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
4492#endif
4493  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4494  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4495  "   Options:",
4496  "      --indent             Try to pretty-print the schema",
4497  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4498  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4499  "    Options:",
4500  "       --init               Create a new SELFTEST table",
4501  "       -v                   Verbose output",
4502  ".separator COL ?ROW?     Change the column and row separators",
4503#if defined(SQLITE_ENABLE_SESSION)
4504  ".session ?NAME? CMD ...  Create or control sessions",
4505  "   Subcommands:",
4506  "     attach TABLE             Attach TABLE",
4507  "     changeset FILE           Write a changeset into FILE",
4508  "     close                    Close one session",
4509  "     enable ?BOOLEAN?         Set or query the enable bit",
4510  "     filter GLOB...           Reject tables matching GLOBs",
4511  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4512  "     isempty                  Query whether the session is empty",
4513  "     list                     List currently open session names",
4514  "     open DB NAME             Open a new session on DB",
4515  "     patchset FILE            Write a patchset into FILE",
4516  "   If ?NAME? is omitted, the first defined session is used.",
4517#endif
4518  ".sha3sum ...             Compute a SHA3 hash of database content",
4519  "    Options:",
4520  "      --schema              Also hash the sqlite_schema table",
4521  "      --sha3-224            Use the sha3-224 algorithm",
4522  "      --sha3-256            Use the sha3-256 algorithm (default)",
4523  "      --sha3-384            Use the sha3-384 algorithm",
4524  "      --sha3-512            Use the sha3-512 algorithm",
4525  "    Any other argument is a LIKE pattern for tables to hash",
4526#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4527  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4528#endif
4529  ".show                    Show the current values for various settings",
4530  ".stats ?ARG?             Show stats or turn stats on or off",
4531  "   off                      Turn off automatic stat display",
4532  "   on                       Turn on automatic stat display",
4533  "   stmt                     Show statement stats",
4534  "   vmstep                   Show the virtual machine step count only",
4535#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4536  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4537#endif
4538  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4539#ifndef SQLITE_SHELL_FIDDLE
4540  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4541#endif
4542  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4543  "                           Run \".testctrl\" with no arguments for details",
4544  ".timeout MS              Try opening locked tables for MS milliseconds",
4545  ".timer on|off            Turn SQL timer on or off",
4546#ifndef SQLITE_OMIT_TRACE
4547  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4548  "    FILE                    Send output to FILE",
4549  "    stdout                  Send output to stdout",
4550  "    stderr                  Send output to stderr",
4551  "    off                     Disable tracing",
4552  "    --expanded              Expand query parameters",
4553#ifdef SQLITE_ENABLE_NORMALIZE
4554  "    --normalized            Normal the SQL statements",
4555#endif
4556  "    --plain                 Show SQL as it is input",
4557  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4558  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4559  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4560  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4561#endif /* SQLITE_OMIT_TRACE */
4562#ifdef SQLITE_DEBUG
4563  ".unmodule NAME ...       Unregister virtual table modules",
4564  "    --allexcept             Unregister everything except those named",
4565#endif
4566  ".vfsinfo ?AUX?           Information about the top-level VFS",
4567  ".vfslist                 List all available VFSes",
4568  ".vfsname ?AUX?           Print the name of the VFS stack",
4569  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4570  "     Negative values right-justify",
4571};
4572
4573/*
4574** Output help text.
4575**
4576** zPattern describes the set of commands for which help text is provided.
4577** If zPattern is NULL, then show all commands, but only give a one-line
4578** description of each.
4579**
4580** Return the number of matches.
4581*/
4582static int showHelp(FILE *out, const char *zPattern){
4583  int i = 0;
4584  int j = 0;
4585  int n = 0;
4586  char *zPat;
4587  if( zPattern==0
4588   || zPattern[0]=='0'
4589   || strcmp(zPattern,"-a")==0
4590   || strcmp(zPattern,"-all")==0
4591   || strcmp(zPattern,"--all")==0
4592  ){
4593    /* Show all commands, but only one line per command */
4594    if( zPattern==0 ) zPattern = "";
4595    for(i=0; i<ArraySize(azHelp); i++){
4596      if( azHelp[i][0]=='.' || zPattern[0] ){
4597        utf8_printf(out, "%s\n", azHelp[i]);
4598        n++;
4599      }
4600    }
4601  }else{
4602    /* Look for commands that for which zPattern is an exact prefix */
4603    zPat = sqlite3_mprintf(".%s*", zPattern);
4604    shell_check_oom(zPat);
4605    for(i=0; i<ArraySize(azHelp); i++){
4606      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4607        utf8_printf(out, "%s\n", azHelp[i]);
4608        j = i+1;
4609        n++;
4610      }
4611    }
4612    sqlite3_free(zPat);
4613    if( n ){
4614      if( n==1 ){
4615        /* when zPattern is a prefix of exactly one command, then include the
4616        ** details of that command, which should begin at offset j */
4617        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4618          utf8_printf(out, "%s\n", azHelp[j]);
4619          j++;
4620        }
4621      }
4622      return n;
4623    }
4624    /* Look for commands that contain zPattern anywhere.  Show the complete
4625    ** text of all commands that match. */
4626    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4627    shell_check_oom(zPat);
4628    for(i=0; i<ArraySize(azHelp); i++){
4629      if( azHelp[i][0]=='.' ) j = i;
4630      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4631        utf8_printf(out, "%s\n", azHelp[j]);
4632        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4633          j++;
4634          utf8_printf(out, "%s\n", azHelp[j]);
4635        }
4636        i = j;
4637        n++;
4638      }
4639    }
4640    sqlite3_free(zPat);
4641  }
4642  return n;
4643}
4644
4645/* Forward reference */
4646static int process_input(ShellState *p);
4647
4648/*
4649** Read the content of file zName into memory obtained from sqlite3_malloc64()
4650** and return a pointer to the buffer. The caller is responsible for freeing
4651** the memory.
4652**
4653** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4654** read.
4655**
4656** For convenience, a nul-terminator byte is always appended to the data read
4657** from the file before the buffer is returned. This byte is not included in
4658** the final value of (*pnByte), if applicable.
4659**
4660** NULL is returned if any error is encountered. The final value of *pnByte
4661** is undefined in this case.
4662*/
4663static char *readFile(const char *zName, int *pnByte){
4664  FILE *in = fopen(zName, "rb");
4665  long nIn;
4666  size_t nRead;
4667  char *pBuf;
4668  if( in==0 ) return 0;
4669  fseek(in, 0, SEEK_END);
4670  nIn = ftell(in);
4671  rewind(in);
4672  pBuf = sqlite3_malloc64( nIn+1 );
4673  if( pBuf==0 ){ fclose(in); return 0; }
4674  nRead = fread(pBuf, nIn, 1, in);
4675  fclose(in);
4676  if( nRead!=1 ){
4677    sqlite3_free(pBuf);
4678    return 0;
4679  }
4680  pBuf[nIn] = 0;
4681  if( pnByte ) *pnByte = nIn;
4682  return pBuf;
4683}
4684
4685#if defined(SQLITE_ENABLE_SESSION)
4686/*
4687** Close a single OpenSession object and release all of its associated
4688** resources.
4689*/
4690static void session_close(OpenSession *pSession){
4691  int i;
4692  sqlite3session_delete(pSession->p);
4693  sqlite3_free(pSession->zName);
4694  for(i=0; i<pSession->nFilter; i++){
4695    sqlite3_free(pSession->azFilter[i]);
4696  }
4697  sqlite3_free(pSession->azFilter);
4698  memset(pSession, 0, sizeof(OpenSession));
4699}
4700#endif
4701
4702/*
4703** Close all OpenSession objects and release all associated resources.
4704*/
4705#if defined(SQLITE_ENABLE_SESSION)
4706static void session_close_all(ShellState *p, int i){
4707  int j;
4708  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4709  for(j=0; j<pAuxDb->nSession; j++){
4710    session_close(&pAuxDb->aSession[j]);
4711  }
4712  pAuxDb->nSession = 0;
4713}
4714#else
4715# define session_close_all(X,Y)
4716#endif
4717
4718/*
4719** Implementation of the xFilter function for an open session.  Omit
4720** any tables named by ".session filter" but let all other table through.
4721*/
4722#if defined(SQLITE_ENABLE_SESSION)
4723static int session_filter(void *pCtx, const char *zTab){
4724  OpenSession *pSession = (OpenSession*)pCtx;
4725  int i;
4726  for(i=0; i<pSession->nFilter; i++){
4727    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4728  }
4729  return 1;
4730}
4731#endif
4732
4733/*
4734** Try to deduce the type of file for zName based on its content.  Return
4735** one of the SHELL_OPEN_* constants.
4736**
4737** If the file does not exist or is empty but its name looks like a ZIP
4738** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4739** Otherwise, assume an ordinary database regardless of the filename if
4740** the type cannot be determined from content.
4741*/
4742int deduceDatabaseType(const char *zName, int dfltZip){
4743  FILE *f = fopen(zName, "rb");
4744  size_t n;
4745  int rc = SHELL_OPEN_UNSPEC;
4746  char zBuf[100];
4747  if( f==0 ){
4748    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4749       return SHELL_OPEN_ZIPFILE;
4750    }else{
4751       return SHELL_OPEN_NORMAL;
4752    }
4753  }
4754  n = fread(zBuf, 16, 1, f);
4755  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4756    fclose(f);
4757    return SHELL_OPEN_NORMAL;
4758  }
4759  fseek(f, -25, SEEK_END);
4760  n = fread(zBuf, 25, 1, f);
4761  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4762    rc = SHELL_OPEN_APPENDVFS;
4763  }else{
4764    fseek(f, -22, SEEK_END);
4765    n = fread(zBuf, 22, 1, f);
4766    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4767       && zBuf[3]==0x06 ){
4768      rc = SHELL_OPEN_ZIPFILE;
4769    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4770      rc = SHELL_OPEN_ZIPFILE;
4771    }
4772  }
4773  fclose(f);
4774  return rc;
4775}
4776
4777#ifndef SQLITE_OMIT_DESERIALIZE
4778/*
4779** Reconstruct an in-memory database using the output from the "dbtotxt"
4780** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4781** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4782*/
4783static unsigned char *readHexDb(ShellState *p, int *pnData){
4784  unsigned char *a = 0;
4785  int nLine;
4786  int n = 0;
4787  int pgsz = 0;
4788  int iOffset = 0;
4789  int j, k;
4790  int rc;
4791  FILE *in;
4792  const char *zDbFilename = p->pAuxDb->zDbFilename;
4793  unsigned int x[16];
4794  char zLine[1000];
4795  if( zDbFilename ){
4796    in = fopen(zDbFilename, "r");
4797    if( in==0 ){
4798      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4799      return 0;
4800    }
4801    nLine = 0;
4802  }else{
4803    in = p->in;
4804    nLine = p->lineno;
4805    if( in==0 ) in = stdin;
4806  }
4807  *pnData = 0;
4808  nLine++;
4809  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4810  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4811  if( rc!=2 ) goto readHexDb_error;
4812  if( n<0 ) goto readHexDb_error;
4813  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4814  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4815  a = sqlite3_malloc( n ? n : 1 );
4816  shell_check_oom(a);
4817  memset(a, 0, n);
4818  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4819    utf8_printf(stderr, "invalid pagesize\n");
4820    goto readHexDb_error;
4821  }
4822  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4823    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4824    if( rc==2 ){
4825      iOffset = k;
4826      continue;
4827    }
4828    if( strncmp(zLine, "| end ", 6)==0 ){
4829      break;
4830    }
4831    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4832                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4833                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4834    if( rc==17 ){
4835      k = iOffset+j;
4836      if( k+16<=n && k>=0 ){
4837        int ii;
4838        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4839      }
4840    }
4841  }
4842  *pnData = n;
4843  if( in!=p->in ){
4844    fclose(in);
4845  }else{
4846    p->lineno = nLine;
4847  }
4848  return a;
4849
4850readHexDb_error:
4851  if( in!=p->in ){
4852    fclose(in);
4853  }else{
4854    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4855      nLine++;
4856      if(strncmp(zLine, "| end ", 6)==0 ) break;
4857    }
4858    p->lineno = nLine;
4859  }
4860  sqlite3_free(a);
4861  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4862  return 0;
4863}
4864#endif /* SQLITE_OMIT_DESERIALIZE */
4865
4866/*
4867** Scalar function "shell_int32". The first argument to this function
4868** must be a blob. The second a non-negative integer. This function
4869** reads and returns a 32-bit big-endian integer from byte
4870** offset (4*<arg2>) of the blob.
4871*/
4872static void shellInt32(
4873  sqlite3_context *context,
4874  int argc,
4875  sqlite3_value **argv
4876){
4877  const unsigned char *pBlob;
4878  int nBlob;
4879  int iInt;
4880
4881  UNUSED_PARAMETER(argc);
4882  nBlob = sqlite3_value_bytes(argv[0]);
4883  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4884  iInt = sqlite3_value_int(argv[1]);
4885
4886  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4887    const unsigned char *a = &pBlob[iInt*4];
4888    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4889                       + ((sqlite3_int64)a[1]<<16)
4890                       + ((sqlite3_int64)a[2]<< 8)
4891                       + ((sqlite3_int64)a[3]<< 0);
4892    sqlite3_result_int64(context, iVal);
4893  }
4894}
4895
4896/*
4897** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4898** using "..." with internal double-quote characters doubled.
4899*/
4900static void shellIdQuote(
4901  sqlite3_context *context,
4902  int argc,
4903  sqlite3_value **argv
4904){
4905  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4906  UNUSED_PARAMETER(argc);
4907  if( zName ){
4908    char *z = sqlite3_mprintf("\"%w\"", zName);
4909    sqlite3_result_text(context, z, -1, sqlite3_free);
4910  }
4911}
4912
4913/*
4914** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4915*/
4916static void shellUSleepFunc(
4917  sqlite3_context *context,
4918  int argcUnused,
4919  sqlite3_value **argv
4920){
4921  int sleep = sqlite3_value_int(argv[0]);
4922  (void)argcUnused;
4923  sqlite3_sleep(sleep/1000);
4924  sqlite3_result_int(context, sleep);
4925}
4926
4927/*
4928** Scalar function "shell_escape_crnl" used by the .recover command.
4929** The argument passed to this function is the output of built-in
4930** function quote(). If the first character of the input is "'",
4931** indicating that the value passed to quote() was a text value,
4932** then this function searches the input for "\n" and "\r" characters
4933** and adds a wrapper similar to the following:
4934**
4935**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4936**
4937** Or, if the first character of the input is not "'", then a copy
4938** of the input is returned.
4939*/
4940static void shellEscapeCrnl(
4941  sqlite3_context *context,
4942  int argc,
4943  sqlite3_value **argv
4944){
4945  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4946  UNUSED_PARAMETER(argc);
4947  if( zText && zText[0]=='\'' ){
4948    i64 nText = sqlite3_value_bytes(argv[0]);
4949    i64 i;
4950    char zBuf1[20];
4951    char zBuf2[20];
4952    const char *zNL = 0;
4953    const char *zCR = 0;
4954    i64 nCR = 0;
4955    i64 nNL = 0;
4956
4957    for(i=0; zText[i]; i++){
4958      if( zNL==0 && zText[i]=='\n' ){
4959        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4960        nNL = strlen(zNL);
4961      }
4962      if( zCR==0 && zText[i]=='\r' ){
4963        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4964        nCR = strlen(zCR);
4965      }
4966    }
4967
4968    if( zNL || zCR ){
4969      i64 iOut = 0;
4970      i64 nMax = (nNL > nCR) ? nNL : nCR;
4971      i64 nAlloc = nMax * nText + (nMax+64)*2;
4972      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4973      if( zOut==0 ){
4974        sqlite3_result_error_nomem(context);
4975        return;
4976      }
4977
4978      if( zNL && zCR ){
4979        memcpy(&zOut[iOut], "replace(replace(", 16);
4980        iOut += 16;
4981      }else{
4982        memcpy(&zOut[iOut], "replace(", 8);
4983        iOut += 8;
4984      }
4985      for(i=0; zText[i]; i++){
4986        if( zText[i]=='\n' ){
4987          memcpy(&zOut[iOut], zNL, nNL);
4988          iOut += nNL;
4989        }else if( zText[i]=='\r' ){
4990          memcpy(&zOut[iOut], zCR, nCR);
4991          iOut += nCR;
4992        }else{
4993          zOut[iOut] = zText[i];
4994          iOut++;
4995        }
4996      }
4997
4998      if( zNL ){
4999        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5000        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
5001        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
5002      }
5003      if( zCR ){
5004        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5005        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
5006        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
5007      }
5008
5009      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
5010      sqlite3_free(zOut);
5011      return;
5012    }
5013  }
5014
5015  sqlite3_result_value(context, argv[0]);
5016}
5017
5018/* Flags for open_db().
5019**
5020** The default behavior of open_db() is to exit(1) if the database fails to
5021** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5022** but still returns without calling exit.
5023**
5024** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5025** ZIP archive if the file does not exist or is empty and its name matches
5026** the *.zip pattern.
5027*/
5028#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
5029#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
5030
5031/*
5032** Make sure the database is open.  If it is not, then open it.  If
5033** the database fails to open, print an error message and exit.
5034*/
5035static void open_db(ShellState *p, int openFlags){
5036  if( p->db==0 ){
5037    const char *zDbFilename = p->pAuxDb->zDbFilename;
5038    if( p->openMode==SHELL_OPEN_UNSPEC ){
5039      if( zDbFilename==0 || zDbFilename[0]==0 ){
5040        p->openMode = SHELL_OPEN_NORMAL;
5041      }else{
5042        p->openMode = (u8)deduceDatabaseType(zDbFilename,
5043                             (openFlags & OPEN_DB_ZIPFILE)!=0);
5044      }
5045    }
5046    switch( p->openMode ){
5047      case SHELL_OPEN_APPENDVFS: {
5048        sqlite3_open_v2(zDbFilename, &p->db,
5049           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5050        break;
5051      }
5052      case SHELL_OPEN_HEXDB:
5053      case SHELL_OPEN_DESERIALIZE: {
5054        sqlite3_open(0, &p->db);
5055        break;
5056      }
5057      case SHELL_OPEN_ZIPFILE: {
5058        sqlite3_open(":memory:", &p->db);
5059        break;
5060      }
5061      case SHELL_OPEN_READONLY: {
5062        sqlite3_open_v2(zDbFilename, &p->db,
5063            SQLITE_OPEN_READONLY|p->openFlags, 0);
5064        break;
5065      }
5066      case SHELL_OPEN_UNSPEC:
5067      case SHELL_OPEN_NORMAL: {
5068        sqlite3_open_v2(zDbFilename, &p->db,
5069           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5070        break;
5071      }
5072    }
5073    globalDb = p->db;
5074    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5075      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5076          zDbFilename, sqlite3_errmsg(p->db));
5077      if( openFlags & OPEN_DB_KEEPALIVE ){
5078        sqlite3_open(":memory:", &p->db);
5079        return;
5080      }
5081      exit(1);
5082    }
5083#ifndef SQLITE_OMIT_LOAD_EXTENSION
5084    sqlite3_enable_load_extension(p->db, 1);
5085#endif
5086    sqlite3_shathree_init(p->db, 0, 0);
5087    sqlite3_uint_init(p->db, 0, 0);
5088    sqlite3_decimal_init(p->db, 0, 0);
5089    sqlite3_regexp_init(p->db, 0, 0);
5090    sqlite3_ieee_init(p->db, 0, 0);
5091    sqlite3_series_init(p->db, 0, 0);
5092#ifndef SQLITE_SHELL_FIDDLE
5093    sqlite3_fileio_init(p->db, 0, 0);
5094    sqlite3_completion_init(p->db, 0, 0);
5095#endif
5096#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5097    sqlite3_dbdata_init(p->db, 0, 0);
5098#endif
5099#ifdef SQLITE_HAVE_ZLIB
5100    if( !p->bSafeModePersist ){
5101      sqlite3_zipfile_init(p->db, 0, 0);
5102      sqlite3_sqlar_init(p->db, 0, 0);
5103    }
5104#endif
5105    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5106                            shellAddSchemaName, 0, 0);
5107    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5108                            shellModuleSchema, 0, 0);
5109    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5110                            shellPutsFunc, 0, 0);
5111    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
5112                            shellEscapeCrnl, 0, 0);
5113    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
5114                            shellInt32, 0, 0);
5115    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
5116                            shellIdQuote, 0, 0);
5117    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5118                            shellUSleepFunc, 0, 0);
5119#ifndef SQLITE_NOHAVE_SYSTEM
5120    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5121                            editFunc, 0, 0);
5122    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5123                            editFunc, 0, 0);
5124#endif
5125    if( p->openMode==SHELL_OPEN_ZIPFILE ){
5126      char *zSql = sqlite3_mprintf(
5127         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5128      shell_check_oom(zSql);
5129      sqlite3_exec(p->db, zSql, 0, 0, 0);
5130      sqlite3_free(zSql);
5131    }
5132#ifndef SQLITE_OMIT_DESERIALIZE
5133    else
5134    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5135      int rc;
5136      int nData = 0;
5137      unsigned char *aData;
5138      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5139        aData = (unsigned char*)readFile(zDbFilename, &nData);
5140      }else{
5141        aData = readHexDb(p, &nData);
5142        if( aData==0 ){
5143          return;
5144        }
5145      }
5146      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5147                   SQLITE_DESERIALIZE_RESIZEABLE |
5148                   SQLITE_DESERIALIZE_FREEONCLOSE);
5149      if( rc ){
5150        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5151      }
5152      if( p->szMax>0 ){
5153        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5154      }
5155    }
5156#endif
5157  }
5158  if( p->bSafeModePersist && p->db!=0 ){
5159    sqlite3_set_authorizer(p->db, safeModeAuth, p);
5160  }
5161}
5162
5163/*
5164** Attempt to close the databaes connection.  Report errors.
5165*/
5166void close_db(sqlite3 *db){
5167  int rc = sqlite3_close(db);
5168  if( rc ){
5169    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5170        rc, sqlite3_errmsg(db));
5171  }
5172}
5173
5174#if HAVE_READLINE || HAVE_EDITLINE
5175/*
5176** Readline completion callbacks
5177*/
5178static char *readline_completion_generator(const char *text, int state){
5179  static sqlite3_stmt *pStmt = 0;
5180  char *zRet;
5181  if( state==0 ){
5182    char *zSql;
5183    sqlite3_finalize(pStmt);
5184    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5185                           "  FROM completion(%Q) ORDER BY 1", text);
5186    shell_check_oom(zSql);
5187    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5188    sqlite3_free(zSql);
5189  }
5190  if( sqlite3_step(pStmt)==SQLITE_ROW ){
5191    const char *z = (const char*)sqlite3_column_text(pStmt,0);
5192    zRet = z ? strdup(z) : 0;
5193  }else{
5194    sqlite3_finalize(pStmt);
5195    pStmt = 0;
5196    zRet = 0;
5197  }
5198  return zRet;
5199}
5200static char **readline_completion(const char *zText, int iStart, int iEnd){
5201  rl_attempted_completion_over = 1;
5202  return rl_completion_matches(zText, readline_completion_generator);
5203}
5204
5205#elif HAVE_LINENOISE
5206/*
5207** Linenoise completion callback
5208*/
5209static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5210  i64 nLine = strlen(zLine);
5211  i64 i, iStart;
5212  sqlite3_stmt *pStmt = 0;
5213  char *zSql;
5214  char zBuf[1000];
5215
5216  if( nLine>sizeof(zBuf)-30 ) return;
5217  if( zLine[0]=='.' || zLine[0]=='#') return;
5218  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5219  if( i==nLine-1 ) return;
5220  iStart = i+1;
5221  memcpy(zBuf, zLine, iStart);
5222  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5223                         "  FROM completion(%Q,%Q) ORDER BY 1",
5224                         &zLine[iStart], zLine);
5225  shell_check_oom(zSql);
5226  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5227  sqlite3_free(zSql);
5228  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5229  while( sqlite3_step(pStmt)==SQLITE_ROW ){
5230    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5231    int nCompletion = sqlite3_column_bytes(pStmt, 0);
5232    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
5233      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5234      linenoiseAddCompletion(lc, zBuf);
5235    }
5236  }
5237  sqlite3_finalize(pStmt);
5238}
5239#endif
5240
5241/*
5242** Do C-language style dequoting.
5243**
5244**    \a    -> alarm
5245**    \b    -> backspace
5246**    \t    -> tab
5247**    \n    -> newline
5248**    \v    -> vertical tab
5249**    \f    -> form feed
5250**    \r    -> carriage return
5251**    \s    -> space
5252**    \"    -> "
5253**    \'    -> '
5254**    \\    -> backslash
5255**    \NNN  -> ascii character NNN in octal
5256*/
5257static void resolve_backslashes(char *z){
5258  int i, j;
5259  char c;
5260  while( *z && *z!='\\' ) z++;
5261  for(i=j=0; (c = z[i])!=0; i++, j++){
5262    if( c=='\\' && z[i+1]!=0 ){
5263      c = z[++i];
5264      if( c=='a' ){
5265        c = '\a';
5266      }else if( c=='b' ){
5267        c = '\b';
5268      }else if( c=='t' ){
5269        c = '\t';
5270      }else if( c=='n' ){
5271        c = '\n';
5272      }else if( c=='v' ){
5273        c = '\v';
5274      }else if( c=='f' ){
5275        c = '\f';
5276      }else if( c=='r' ){
5277        c = '\r';
5278      }else if( c=='"' ){
5279        c = '"';
5280      }else if( c=='\'' ){
5281        c = '\'';
5282      }else if( c=='\\' ){
5283        c = '\\';
5284      }else if( c>='0' && c<='7' ){
5285        c -= '0';
5286        if( z[i+1]>='0' && z[i+1]<='7' ){
5287          i++;
5288          c = (c<<3) + z[i] - '0';
5289          if( z[i+1]>='0' && z[i+1]<='7' ){
5290            i++;
5291            c = (c<<3) + z[i] - '0';
5292          }
5293        }
5294      }
5295    }
5296    z[j] = c;
5297  }
5298  if( j<i ) z[j] = 0;
5299}
5300
5301/*
5302** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
5303** for TRUE and FALSE.  Return the integer value if appropriate.
5304*/
5305static int booleanValue(const char *zArg){
5306  int i;
5307  if( zArg[0]=='0' && zArg[1]=='x' ){
5308    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5309  }else{
5310    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5311  }
5312  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5313  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5314    return 1;
5315  }
5316  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5317    return 0;
5318  }
5319  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5320          zArg);
5321  return 0;
5322}
5323
5324/*
5325** Set or clear a shell flag according to a boolean value.
5326*/
5327static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5328  if( booleanValue(zArg) ){
5329    ShellSetFlag(p, mFlag);
5330  }else{
5331    ShellClearFlag(p, mFlag);
5332  }
5333}
5334
5335/*
5336** Close an output file, assuming it is not stderr or stdout
5337*/
5338static void output_file_close(FILE *f){
5339  if( f && f!=stdout && f!=stderr ) fclose(f);
5340}
5341
5342/*
5343** Try to open an output file.   The names "stdout" and "stderr" are
5344** recognized and do the right thing.  NULL is returned if the output
5345** filename is "off".
5346*/
5347static FILE *output_file_open(const char *zFile, int bTextMode){
5348  FILE *f;
5349  if( strcmp(zFile,"stdout")==0 ){
5350    f = stdout;
5351  }else if( strcmp(zFile, "stderr")==0 ){
5352    f = stderr;
5353  }else if( strcmp(zFile, "off")==0 ){
5354    f = 0;
5355  }else{
5356    f = fopen(zFile, bTextMode ? "w" : "wb");
5357    if( f==0 ){
5358      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5359    }
5360  }
5361  return f;
5362}
5363
5364#ifndef SQLITE_OMIT_TRACE
5365/*
5366** A routine for handling output from sqlite3_trace().
5367*/
5368static int sql_trace_callback(
5369  unsigned mType,         /* The trace type */
5370  void *pArg,             /* The ShellState pointer */
5371  void *pP,               /* Usually a pointer to sqlite_stmt */
5372  void *pX                /* Auxiliary output */
5373){
5374  ShellState *p = (ShellState*)pArg;
5375  sqlite3_stmt *pStmt;
5376  const char *zSql;
5377  i64 nSql;
5378  if( p->traceOut==0 ) return 0;
5379  if( mType==SQLITE_TRACE_CLOSE ){
5380    utf8_printf(p->traceOut, "-- closing database connection\n");
5381    return 0;
5382  }
5383  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5384    zSql = (const char*)pX;
5385  }else{
5386    pStmt = (sqlite3_stmt*)pP;
5387    switch( p->eTraceType ){
5388      case SHELL_TRACE_EXPANDED: {
5389        zSql = sqlite3_expanded_sql(pStmt);
5390        break;
5391      }
5392#ifdef SQLITE_ENABLE_NORMALIZE
5393      case SHELL_TRACE_NORMALIZED: {
5394        zSql = sqlite3_normalized_sql(pStmt);
5395        break;
5396      }
5397#endif
5398      default: {
5399        zSql = sqlite3_sql(pStmt);
5400        break;
5401      }
5402    }
5403  }
5404  if( zSql==0 ) return 0;
5405  nSql = strlen(zSql);
5406  if( nSql>1000000000 ) nSql = 1000000000;
5407  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5408  switch( mType ){
5409    case SQLITE_TRACE_ROW:
5410    case SQLITE_TRACE_STMT: {
5411      utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5412      break;
5413    }
5414    case SQLITE_TRACE_PROFILE: {
5415      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5416      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5417      break;
5418    }
5419  }
5420  return 0;
5421}
5422#endif
5423
5424/*
5425** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5426** a useful spot to set a debugger breakpoint.
5427*/
5428static void test_breakpoint(void){
5429  static int nCall = 0;
5430  nCall++;
5431}
5432
5433/*
5434** An object used to read a CSV and other files for import.
5435*/
5436typedef struct ImportCtx ImportCtx;
5437struct ImportCtx {
5438  const char *zFile;  /* Name of the input file */
5439  FILE *in;           /* Read the CSV text from this input stream */
5440  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5441  char *z;            /* Accumulated text for a field */
5442  int n;              /* Number of bytes in z */
5443  int nAlloc;         /* Space allocated for z[] */
5444  int nLine;          /* Current line number */
5445  int nRow;           /* Number of rows imported */
5446  int nErr;           /* Number of errors encountered */
5447  int bNotFirst;      /* True if one or more bytes already read */
5448  int cTerm;          /* Character that terminated the most recent field */
5449  int cColSep;        /* The column separator character.  (Usually ",") */
5450  int cRowSep;        /* The row separator character.  (Usually "\n") */
5451};
5452
5453/* Clean up resourced used by an ImportCtx */
5454static void import_cleanup(ImportCtx *p){
5455  if( p->in!=0 && p->xCloser!=0 ){
5456    p->xCloser(p->in);
5457    p->in = 0;
5458  }
5459  sqlite3_free(p->z);
5460  p->z = 0;
5461}
5462
5463/* Append a single byte to z[] */
5464static void import_append_char(ImportCtx *p, int c){
5465  if( p->n+1>=p->nAlloc ){
5466    p->nAlloc += p->nAlloc + 100;
5467    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5468    shell_check_oom(p->z);
5469  }
5470  p->z[p->n++] = (char)c;
5471}
5472
5473/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5474** with the option of having a separator other than ",".
5475**
5476**   +  Input comes from p->in.
5477**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5478**      from sqlite3_malloc64().
5479**   +  Use p->cSep as the column separator.  The default is ",".
5480**   +  Use p->rSep as the row separator.  The default is "\n".
5481**   +  Keep track of the line number in p->nLine.
5482**   +  Store the character that terminates the field in p->cTerm.  Store
5483**      EOF on end-of-file.
5484**   +  Report syntax errors on stderr
5485*/
5486static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5487  int c;
5488  int cSep = p->cColSep;
5489  int rSep = p->cRowSep;
5490  p->n = 0;
5491  c = fgetc(p->in);
5492  if( c==EOF || seenInterrupt ){
5493    p->cTerm = EOF;
5494    return 0;
5495  }
5496  if( c=='"' ){
5497    int pc, ppc;
5498    int startLine = p->nLine;
5499    int cQuote = c;
5500    pc = ppc = 0;
5501    while( 1 ){
5502      c = fgetc(p->in);
5503      if( c==rSep ) p->nLine++;
5504      if( c==cQuote ){
5505        if( pc==cQuote ){
5506          pc = 0;
5507          continue;
5508        }
5509      }
5510      if( (c==cSep && pc==cQuote)
5511       || (c==rSep && pc==cQuote)
5512       || (c==rSep && pc=='\r' && ppc==cQuote)
5513       || (c==EOF && pc==cQuote)
5514      ){
5515        do{ p->n--; }while( p->z[p->n]!=cQuote );
5516        p->cTerm = c;
5517        break;
5518      }
5519      if( pc==cQuote && c!='\r' ){
5520        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5521                p->zFile, p->nLine, cQuote);
5522      }
5523      if( c==EOF ){
5524        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5525                p->zFile, startLine, cQuote);
5526        p->cTerm = c;
5527        break;
5528      }
5529      import_append_char(p, c);
5530      ppc = pc;
5531      pc = c;
5532    }
5533  }else{
5534    /* If this is the first field being parsed and it begins with the
5535    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5536    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5537      import_append_char(p, c);
5538      c = fgetc(p->in);
5539      if( (c&0xff)==0xbb ){
5540        import_append_char(p, c);
5541        c = fgetc(p->in);
5542        if( (c&0xff)==0xbf ){
5543          p->bNotFirst = 1;
5544          p->n = 0;
5545          return csv_read_one_field(p);
5546        }
5547      }
5548    }
5549    while( c!=EOF && c!=cSep && c!=rSep ){
5550      import_append_char(p, c);
5551      c = fgetc(p->in);
5552    }
5553    if( c==rSep ){
5554      p->nLine++;
5555      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5556    }
5557    p->cTerm = c;
5558  }
5559  if( p->z ) p->z[p->n] = 0;
5560  p->bNotFirst = 1;
5561  return p->z;
5562}
5563
5564/* Read a single field of ASCII delimited text.
5565**
5566**   +  Input comes from p->in.
5567**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5568**      from sqlite3_malloc64().
5569**   +  Use p->cSep as the column separator.  The default is "\x1F".
5570**   +  Use p->rSep as the row separator.  The default is "\x1E".
5571**   +  Keep track of the row number in p->nLine.
5572**   +  Store the character that terminates the field in p->cTerm.  Store
5573**      EOF on end-of-file.
5574**   +  Report syntax errors on stderr
5575*/
5576static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5577  int c;
5578  int cSep = p->cColSep;
5579  int rSep = p->cRowSep;
5580  p->n = 0;
5581  c = fgetc(p->in);
5582  if( c==EOF || seenInterrupt ){
5583    p->cTerm = EOF;
5584    return 0;
5585  }
5586  while( c!=EOF && c!=cSep && c!=rSep ){
5587    import_append_char(p, c);
5588    c = fgetc(p->in);
5589  }
5590  if( c==rSep ){
5591    p->nLine++;
5592  }
5593  p->cTerm = c;
5594  if( p->z ) p->z[p->n] = 0;
5595  return p->z;
5596}
5597
5598/*
5599** Try to transfer data for table zTable.  If an error is seen while
5600** moving forward, try to go backwards.  The backwards movement won't
5601** work for WITHOUT ROWID tables.
5602*/
5603static void tryToCloneData(
5604  ShellState *p,
5605  sqlite3 *newDb,
5606  const char *zTable
5607){
5608  sqlite3_stmt *pQuery = 0;
5609  sqlite3_stmt *pInsert = 0;
5610  char *zQuery = 0;
5611  char *zInsert = 0;
5612  int rc;
5613  int i, j, n;
5614  int nTable = strlen30(zTable);
5615  int k = 0;
5616  int cnt = 0;
5617  const int spinRate = 10000;
5618
5619  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5620  shell_check_oom(zQuery);
5621  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5622  if( rc ){
5623    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5624            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5625            zQuery);
5626    goto end_data_xfer;
5627  }
5628  n = sqlite3_column_count(pQuery);
5629  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5630  shell_check_oom(zInsert);
5631  sqlite3_snprintf(200+nTable,zInsert,
5632                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5633  i = strlen30(zInsert);
5634  for(j=1; j<n; j++){
5635    memcpy(zInsert+i, ",?", 2);
5636    i += 2;
5637  }
5638  memcpy(zInsert+i, ");", 3);
5639  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5640  if( rc ){
5641    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5642            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5643            zQuery);
5644    goto end_data_xfer;
5645  }
5646  for(k=0; k<2; k++){
5647    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5648      for(i=0; i<n; i++){
5649        switch( sqlite3_column_type(pQuery, i) ){
5650          case SQLITE_NULL: {
5651            sqlite3_bind_null(pInsert, i+1);
5652            break;
5653          }
5654          case SQLITE_INTEGER: {
5655            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5656            break;
5657          }
5658          case SQLITE_FLOAT: {
5659            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5660            break;
5661          }
5662          case SQLITE_TEXT: {
5663            sqlite3_bind_text(pInsert, i+1,
5664                             (const char*)sqlite3_column_text(pQuery,i),
5665                             -1, SQLITE_STATIC);
5666            break;
5667          }
5668          case SQLITE_BLOB: {
5669            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5670                                            sqlite3_column_bytes(pQuery,i),
5671                                            SQLITE_STATIC);
5672            break;
5673          }
5674        }
5675      } /* End for */
5676      rc = sqlite3_step(pInsert);
5677      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5678        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5679                        sqlite3_errmsg(newDb));
5680      }
5681      sqlite3_reset(pInsert);
5682      cnt++;
5683      if( (cnt%spinRate)==0 ){
5684        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5685        fflush(stdout);
5686      }
5687    } /* End while */
5688    if( rc==SQLITE_DONE ) break;
5689    sqlite3_finalize(pQuery);
5690    sqlite3_free(zQuery);
5691    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5692                             zTable);
5693    shell_check_oom(zQuery);
5694    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5695    if( rc ){
5696      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5697      break;
5698    }
5699  } /* End for(k=0...) */
5700
5701end_data_xfer:
5702  sqlite3_finalize(pQuery);
5703  sqlite3_finalize(pInsert);
5704  sqlite3_free(zQuery);
5705  sqlite3_free(zInsert);
5706}
5707
5708
5709/*
5710** Try to transfer all rows of the schema that match zWhere.  For
5711** each row, invoke xForEach() on the object defined by that row.
5712** If an error is encountered while moving forward through the
5713** sqlite_schema table, try again moving backwards.
5714*/
5715static void tryToCloneSchema(
5716  ShellState *p,
5717  sqlite3 *newDb,
5718  const char *zWhere,
5719  void (*xForEach)(ShellState*,sqlite3*,const char*)
5720){
5721  sqlite3_stmt *pQuery = 0;
5722  char *zQuery = 0;
5723  int rc;
5724  const unsigned char *zName;
5725  const unsigned char *zSql;
5726  char *zErrMsg = 0;
5727
5728  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5729                           " WHERE %s", zWhere);
5730  shell_check_oom(zQuery);
5731  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5732  if( rc ){
5733    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5734                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5735                    zQuery);
5736    goto end_schema_xfer;
5737  }
5738  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5739    zName = sqlite3_column_text(pQuery, 0);
5740    zSql = sqlite3_column_text(pQuery, 1);
5741    if( zName==0 || zSql==0 ) continue;
5742    printf("%s... ", zName); fflush(stdout);
5743    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5744    if( zErrMsg ){
5745      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5746      sqlite3_free(zErrMsg);
5747      zErrMsg = 0;
5748    }
5749    if( xForEach ){
5750      xForEach(p, newDb, (const char*)zName);
5751    }
5752    printf("done\n");
5753  }
5754  if( rc!=SQLITE_DONE ){
5755    sqlite3_finalize(pQuery);
5756    sqlite3_free(zQuery);
5757    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5758                             " WHERE %s ORDER BY rowid DESC", zWhere);
5759    shell_check_oom(zQuery);
5760    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5761    if( rc ){
5762      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5763                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5764                      zQuery);
5765      goto end_schema_xfer;
5766    }
5767    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5768      zName = sqlite3_column_text(pQuery, 0);
5769      zSql = sqlite3_column_text(pQuery, 1);
5770      if( zName==0 || zSql==0 ) continue;
5771      printf("%s... ", zName); fflush(stdout);
5772      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5773      if( zErrMsg ){
5774        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5775        sqlite3_free(zErrMsg);
5776        zErrMsg = 0;
5777      }
5778      if( xForEach ){
5779        xForEach(p, newDb, (const char*)zName);
5780      }
5781      printf("done\n");
5782    }
5783  }
5784end_schema_xfer:
5785  sqlite3_finalize(pQuery);
5786  sqlite3_free(zQuery);
5787}
5788
5789/*
5790** Open a new database file named "zNewDb".  Try to recover as much information
5791** as possible out of the main database (which might be corrupt) and write it
5792** into zNewDb.
5793*/
5794static void tryToClone(ShellState *p, const char *zNewDb){
5795  int rc;
5796  sqlite3 *newDb = 0;
5797  if( access(zNewDb,0)==0 ){
5798    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5799    return;
5800  }
5801  rc = sqlite3_open(zNewDb, &newDb);
5802  if( rc ){
5803    utf8_printf(stderr, "Cannot create output database: %s\n",
5804            sqlite3_errmsg(newDb));
5805  }else{
5806    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5807    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5808    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5809    tryToCloneSchema(p, newDb, "type!='table'", 0);
5810    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5811    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5812  }
5813  close_db(newDb);
5814}
5815
5816/*
5817** Change the output file back to stdout.
5818**
5819** If the p->doXdgOpen flag is set, that means the output was being
5820** redirected to a temporary file named by p->zTempFile.  In that case,
5821** launch start/open/xdg-open on that temporary file.
5822*/
5823static void output_reset(ShellState *p){
5824  if( p->outfile[0]=='|' ){
5825#ifndef SQLITE_OMIT_POPEN
5826    pclose(p->out);
5827#endif
5828  }else{
5829    output_file_close(p->out);
5830#ifndef SQLITE_NOHAVE_SYSTEM
5831    if( p->doXdgOpen ){
5832      const char *zXdgOpenCmd =
5833#if defined(_WIN32)
5834      "start";
5835#elif defined(__APPLE__)
5836      "open";
5837#else
5838      "xdg-open";
5839#endif
5840      char *zCmd;
5841      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5842      if( system(zCmd) ){
5843        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5844      }else{
5845        /* Give the start/open/xdg-open command some time to get
5846        ** going before we continue, and potential delete the
5847        ** p->zTempFile data file out from under it */
5848        sqlite3_sleep(2000);
5849      }
5850      sqlite3_free(zCmd);
5851      outputModePop(p);
5852      p->doXdgOpen = 0;
5853    }
5854#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5855  }
5856  p->outfile[0] = 0;
5857  p->out = stdout;
5858}
5859
5860/*
5861** Run an SQL command and return the single integer result.
5862*/
5863static int db_int(sqlite3 *db, const char *zSql){
5864  sqlite3_stmt *pStmt;
5865  int res = 0;
5866  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
5867  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5868    res = sqlite3_column_int(pStmt,0);
5869  }
5870  sqlite3_finalize(pStmt);
5871  return res;
5872}
5873
5874#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5875/*
5876** Convert a 2-byte or 4-byte big-endian integer into a native integer
5877*/
5878static unsigned int get2byteInt(unsigned char *a){
5879  return (a[0]<<8) + a[1];
5880}
5881static unsigned int get4byteInt(unsigned char *a){
5882  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5883}
5884
5885/*
5886** Implementation of the ".dbinfo" command.
5887**
5888** Return 1 on error, 2 to exit, and 0 otherwise.
5889*/
5890static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5891  static const struct { const char *zName; int ofst; } aField[] = {
5892     { "file change counter:",  24  },
5893     { "database page count:",  28  },
5894     { "freelist page count:",  36  },
5895     { "schema cookie:",        40  },
5896     { "schema format:",        44  },
5897     { "default cache size:",   48  },
5898     { "autovacuum top root:",  52  },
5899     { "incremental vacuum:",   64  },
5900     { "text encoding:",        56  },
5901     { "user version:",         60  },
5902     { "application id:",       68  },
5903     { "software version:",     96  },
5904  };
5905  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5906     { "number of tables:",
5907       "SELECT count(*) FROM %s WHERE type='table'" },
5908     { "number of indexes:",
5909       "SELECT count(*) FROM %s WHERE type='index'" },
5910     { "number of triggers:",
5911       "SELECT count(*) FROM %s WHERE type='trigger'" },
5912     { "number of views:",
5913       "SELECT count(*) FROM %s WHERE type='view'" },
5914     { "schema size:",
5915       "SELECT total(length(sql)) FROM %s" },
5916  };
5917  int i, rc;
5918  unsigned iDataVersion;
5919  char *zSchemaTab;
5920  char *zDb = nArg>=2 ? azArg[1] : "main";
5921  sqlite3_stmt *pStmt = 0;
5922  unsigned char aHdr[100];
5923  open_db(p, 0);
5924  if( p->db==0 ) return 1;
5925  rc = sqlite3_prepare_v2(p->db,
5926             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5927             -1, &pStmt, 0);
5928  if( rc ){
5929    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5930    sqlite3_finalize(pStmt);
5931    return 1;
5932  }
5933  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5934  if( sqlite3_step(pStmt)==SQLITE_ROW
5935   && sqlite3_column_bytes(pStmt,0)>100
5936  ){
5937    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5938    sqlite3_finalize(pStmt);
5939  }else{
5940    raw_printf(stderr, "unable to read database header\n");
5941    sqlite3_finalize(pStmt);
5942    return 1;
5943  }
5944  i = get2byteInt(aHdr+16);
5945  if( i==1 ) i = 65536;
5946  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5947  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5948  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5949  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5950  for(i=0; i<ArraySize(aField); i++){
5951    int ofst = aField[i].ofst;
5952    unsigned int val = get4byteInt(aHdr + ofst);
5953    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5954    switch( ofst ){
5955      case 56: {
5956        if( val==1 ) raw_printf(p->out, " (utf8)");
5957        if( val==2 ) raw_printf(p->out, " (utf16le)");
5958        if( val==3 ) raw_printf(p->out, " (utf16be)");
5959      }
5960    }
5961    raw_printf(p->out, "\n");
5962  }
5963  if( zDb==0 ){
5964    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5965  }else if( strcmp(zDb,"temp")==0 ){
5966    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5967  }else{
5968    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5969  }
5970  for(i=0; i<ArraySize(aQuery); i++){
5971    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5972    int val = db_int(p->db, zSql);
5973    sqlite3_free(zSql);
5974    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5975  }
5976  sqlite3_free(zSchemaTab);
5977  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5978  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5979  return 0;
5980}
5981#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE)
5982          && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
5983
5984/*
5985** Print the current sqlite3_errmsg() value to stderr and return 1.
5986*/
5987static int shellDatabaseError(sqlite3 *db){
5988  const char *zErr = sqlite3_errmsg(db);
5989  utf8_printf(stderr, "Error: %s\n", zErr);
5990  return 1;
5991}
5992
5993/*
5994** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5995** if they match and FALSE (0) if they do not match.
5996**
5997** Globbing rules:
5998**
5999**      '*'       Matches any sequence of zero or more characters.
6000**
6001**      '?'       Matches exactly one character.
6002**
6003**     [...]      Matches one character from the enclosed list of
6004**                characters.
6005**
6006**     [^...]     Matches one character not in the enclosed list.
6007**
6008**      '#'       Matches any sequence of one or more digits with an
6009**                optional + or - sign in front
6010**
6011**      ' '       Any span of whitespace matches any other span of
6012**                whitespace.
6013**
6014** Extra whitespace at the end of z[] is ignored.
6015*/
6016static int testcase_glob(const char *zGlob, const char *z){
6017  int c, c2;
6018  int invert;
6019  int seen;
6020
6021  while( (c = (*(zGlob++)))!=0 ){
6022    if( IsSpace(c) ){
6023      if( !IsSpace(*z) ) return 0;
6024      while( IsSpace(*zGlob) ) zGlob++;
6025      while( IsSpace(*z) ) z++;
6026    }else if( c=='*' ){
6027      while( (c=(*(zGlob++))) == '*' || c=='?' ){
6028        if( c=='?' && (*(z++))==0 ) return 0;
6029      }
6030      if( c==0 ){
6031        return 1;
6032      }else if( c=='[' ){
6033        while( *z && testcase_glob(zGlob-1,z)==0 ){
6034          z++;
6035        }
6036        return (*z)!=0;
6037      }
6038      while( (c2 = (*(z++)))!=0 ){
6039        while( c2!=c ){
6040          c2 = *(z++);
6041          if( c2==0 ) return 0;
6042        }
6043        if( testcase_glob(zGlob,z) ) return 1;
6044      }
6045      return 0;
6046    }else if( c=='?' ){
6047      if( (*(z++))==0 ) return 0;
6048    }else if( c=='[' ){
6049      int prior_c = 0;
6050      seen = 0;
6051      invert = 0;
6052      c = *(z++);
6053      if( c==0 ) return 0;
6054      c2 = *(zGlob++);
6055      if( c2=='^' ){
6056        invert = 1;
6057        c2 = *(zGlob++);
6058      }
6059      if( c2==']' ){
6060        if( c==']' ) seen = 1;
6061        c2 = *(zGlob++);
6062      }
6063      while( c2 && c2!=']' ){
6064        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6065          c2 = *(zGlob++);
6066          if( c>=prior_c && c<=c2 ) seen = 1;
6067          prior_c = 0;
6068        }else{
6069          if( c==c2 ){
6070            seen = 1;
6071          }
6072          prior_c = c2;
6073        }
6074        c2 = *(zGlob++);
6075      }
6076      if( c2==0 || (seen ^ invert)==0 ) return 0;
6077    }else if( c=='#' ){
6078      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6079      if( !IsDigit(z[0]) ) return 0;
6080      z++;
6081      while( IsDigit(z[0]) ){ z++; }
6082    }else{
6083      if( c!=(*(z++)) ) return 0;
6084    }
6085  }
6086  while( IsSpace(*z) ){ z++; }
6087  return *z==0;
6088}
6089
6090
6091/*
6092** Compare the string as a command-line option with either one or two
6093** initial "-" characters.
6094*/
6095static int optionMatch(const char *zStr, const char *zOpt){
6096  if( zStr[0]!='-' ) return 0;
6097  zStr++;
6098  if( zStr[0]=='-' ) zStr++;
6099  return strcmp(zStr, zOpt)==0;
6100}
6101
6102/*
6103** Delete a file.
6104*/
6105int shellDeleteFile(const char *zFilename){
6106  int rc;
6107#ifdef _WIN32
6108  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6109  rc = _wunlink(z);
6110  sqlite3_free(z);
6111#else
6112  rc = unlink(zFilename);
6113#endif
6114  return rc;
6115}
6116
6117/*
6118** Try to delete the temporary file (if there is one) and free the
6119** memory used to hold the name of the temp file.
6120*/
6121static void clearTempFile(ShellState *p){
6122  if( p->zTempFile==0 ) return;
6123  if( p->doXdgOpen ) return;
6124  if( shellDeleteFile(p->zTempFile) ) return;
6125  sqlite3_free(p->zTempFile);
6126  p->zTempFile = 0;
6127}
6128
6129/*
6130** Create a new temp file name with the given suffix.
6131*/
6132static void newTempFile(ShellState *p, const char *zSuffix){
6133  clearTempFile(p);
6134  sqlite3_free(p->zTempFile);
6135  p->zTempFile = 0;
6136  if( p->db ){
6137    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6138  }
6139  if( p->zTempFile==0 ){
6140    /* If p->db is an in-memory database then the TEMPFILENAME file-control
6141    ** will not work and we will need to fallback to guessing */
6142    char *zTemp;
6143    sqlite3_uint64 r;
6144    sqlite3_randomness(sizeof(r), &r);
6145    zTemp = getenv("TEMP");
6146    if( zTemp==0 ) zTemp = getenv("TMP");
6147    if( zTemp==0 ){
6148#ifdef _WIN32
6149      zTemp = "\\tmp";
6150#else
6151      zTemp = "/tmp";
6152#endif
6153    }
6154    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6155  }else{
6156    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6157  }
6158  shell_check_oom(p->zTempFile);
6159}
6160
6161
6162/*
6163** The implementation of SQL scalar function fkey_collate_clause(), used
6164** by the ".lint fkey-indexes" command. This scalar function is always
6165** called with four arguments - the parent table name, the parent column name,
6166** the child table name and the child column name.
6167**
6168**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6169**
6170** If either of the named tables or columns do not exist, this function
6171** returns an empty string. An empty string is also returned if both tables
6172** and columns exist but have the same default collation sequence. Or,
6173** if both exist but the default collation sequences are different, this
6174** function returns the string " COLLATE <parent-collation>", where
6175** <parent-collation> is the default collation sequence of the parent column.
6176*/
6177static void shellFkeyCollateClause(
6178  sqlite3_context *pCtx,
6179  int nVal,
6180  sqlite3_value **apVal
6181){
6182  sqlite3 *db = sqlite3_context_db_handle(pCtx);
6183  const char *zParent;
6184  const char *zParentCol;
6185  const char *zParentSeq;
6186  const char *zChild;
6187  const char *zChildCol;
6188  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
6189  int rc;
6190
6191  assert( nVal==4 );
6192  zParent = (const char*)sqlite3_value_text(apVal[0]);
6193  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6194  zChild = (const char*)sqlite3_value_text(apVal[2]);
6195  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6196
6197  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6198  rc = sqlite3_table_column_metadata(
6199      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6200  );
6201  if( rc==SQLITE_OK ){
6202    rc = sqlite3_table_column_metadata(
6203        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6204    );
6205  }
6206
6207  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6208    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6209    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6210    sqlite3_free(z);
6211  }
6212}
6213
6214
6215/*
6216** The implementation of dot-command ".lint fkey-indexes".
6217*/
6218static int lintFkeyIndexes(
6219  ShellState *pState,             /* Current shell tool state */
6220  char **azArg,                   /* Array of arguments passed to dot command */
6221  int nArg                        /* Number of entries in azArg[] */
6222){
6223  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
6224  FILE *out = pState->out;        /* Stream to write non-error output to */
6225  int bVerbose = 0;               /* If -verbose is present */
6226  int bGroupByParent = 0;         /* If -groupbyparent is present */
6227  int i;                          /* To iterate through azArg[] */
6228  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
6229  int rc;                         /* Return code */
6230  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
6231
6232  /*
6233  ** This SELECT statement returns one row for each foreign key constraint
6234  ** in the schema of the main database. The column values are:
6235  **
6236  ** 0. The text of an SQL statement similar to:
6237  **
6238  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6239  **
6240  **    This SELECT is similar to the one that the foreign keys implementation
6241  **    needs to run internally on child tables. If there is an index that can
6242  **    be used to optimize this query, then it can also be used by the FK
6243  **    implementation to optimize DELETE or UPDATE statements on the parent
6244  **    table.
6245  **
6246  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6247  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6248  **    contains an index that can be used to optimize the query.
6249  **
6250  ** 2. Human readable text that describes the child table and columns. e.g.
6251  **
6252  **       "child_table(child_key1, child_key2)"
6253  **
6254  ** 3. Human readable text that describes the parent table and columns. e.g.
6255  **
6256  **       "parent_table(parent_key1, parent_key2)"
6257  **
6258  ** 4. A full CREATE INDEX statement for an index that could be used to
6259  **    optimize DELETE or UPDATE statements on the parent table. e.g.
6260  **
6261  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
6262  **
6263  ** 5. The name of the parent table.
6264  **
6265  ** These six values are used by the C logic below to generate the report.
6266  */
6267  const char *zSql =
6268  "SELECT "
6269    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6270    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6271    "  || fkey_collate_clause("
6272    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6273    ", "
6274    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6275    "  || group_concat('*=?', ' AND ') || ')'"
6276    ", "
6277    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
6278    ", "
6279    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6280    ", "
6281    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6282    "  || ' ON ' || quote(s.name) || '('"
6283    "  || group_concat(quote(f.[from]) ||"
6284    "        fkey_collate_clause("
6285    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6286    "  || ');'"
6287    ", "
6288    "     f.[table] "
6289    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6290    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6291    "GROUP BY s.name, f.id "
6292    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6293  ;
6294  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6295
6296  for(i=2; i<nArg; i++){
6297    int n = strlen30(azArg[i]);
6298    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6299      bVerbose = 1;
6300    }
6301    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6302      bGroupByParent = 1;
6303      zIndent = "    ";
6304    }
6305    else{
6306      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6307          azArg[0], azArg[1]
6308      );
6309      return SQLITE_ERROR;
6310    }
6311  }
6312
6313  /* Register the fkey_collate_clause() SQL function */
6314  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6315      0, shellFkeyCollateClause, 0, 0
6316  );
6317
6318
6319  if( rc==SQLITE_OK ){
6320    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6321  }
6322  if( rc==SQLITE_OK ){
6323    sqlite3_bind_int(pSql, 1, bGroupByParent);
6324  }
6325
6326  if( rc==SQLITE_OK ){
6327    int rc2;
6328    char *zPrev = 0;
6329    while( SQLITE_ROW==sqlite3_step(pSql) ){
6330      int res = -1;
6331      sqlite3_stmt *pExplain = 0;
6332      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6333      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6334      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6335      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6336      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6337      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6338
6339      if( zEQP==0 ) continue;
6340      if( zGlob==0 ) continue;
6341      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6342      if( rc!=SQLITE_OK ) break;
6343      if( SQLITE_ROW==sqlite3_step(pExplain) ){
6344        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6345        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
6346                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
6347      }
6348      rc = sqlite3_finalize(pExplain);
6349      if( rc!=SQLITE_OK ) break;
6350
6351      if( res<0 ){
6352        raw_printf(stderr, "Error: internal error");
6353        break;
6354      }else{
6355        if( bGroupByParent
6356        && (bVerbose || res==0)
6357        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6358        ){
6359          raw_printf(out, "-- Parent table %s\n", zParent);
6360          sqlite3_free(zPrev);
6361          zPrev = sqlite3_mprintf("%s", zParent);
6362        }
6363
6364        if( res==0 ){
6365          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6366        }else if( bVerbose ){
6367          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6368              zIndent, zFrom, zTarget
6369          );
6370        }
6371      }
6372    }
6373    sqlite3_free(zPrev);
6374
6375    if( rc!=SQLITE_OK ){
6376      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6377    }
6378
6379    rc2 = sqlite3_finalize(pSql);
6380    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6381      rc = rc2;
6382      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6383    }
6384  }else{
6385    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6386  }
6387
6388  return rc;
6389}
6390
6391/*
6392** Implementation of ".lint" dot command.
6393*/
6394static int lintDotCommand(
6395  ShellState *pState,             /* Current shell tool state */
6396  char **azArg,                   /* Array of arguments passed to dot command */
6397  int nArg                        /* Number of entries in azArg[] */
6398){
6399  int n;
6400  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6401  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6402  return lintFkeyIndexes(pState, azArg, nArg);
6403
6404 usage:
6405  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6406  raw_printf(stderr, "Where sub-commands are:\n");
6407  raw_printf(stderr, "    fkey-indexes\n");
6408  return SQLITE_ERROR;
6409}
6410
6411#if !defined SQLITE_OMIT_VIRTUALTABLE
6412static void shellPrepare(
6413  sqlite3 *db,
6414  int *pRc,
6415  const char *zSql,
6416  sqlite3_stmt **ppStmt
6417){
6418  *ppStmt = 0;
6419  if( *pRc==SQLITE_OK ){
6420    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6421    if( rc!=SQLITE_OK ){
6422      raw_printf(stderr, "sql error: %s (%d)\n",
6423          sqlite3_errmsg(db), sqlite3_errcode(db)
6424      );
6425      *pRc = rc;
6426    }
6427  }
6428}
6429
6430/*
6431** Create a prepared statement using printf-style arguments for the SQL.
6432**
6433** This routine is could be marked "static".  But it is not always used,
6434** depending on compile-time options.  By omitting the "static", we avoid
6435** nuisance compiler warnings about "defined but not used".
6436*/
6437void shellPreparePrintf(
6438  sqlite3 *db,
6439  int *pRc,
6440  sqlite3_stmt **ppStmt,
6441  const char *zFmt,
6442  ...
6443){
6444  *ppStmt = 0;
6445  if( *pRc==SQLITE_OK ){
6446    va_list ap;
6447    char *z;
6448    va_start(ap, zFmt);
6449    z = sqlite3_vmprintf(zFmt, ap);
6450    va_end(ap);
6451    if( z==0 ){
6452      *pRc = SQLITE_NOMEM;
6453    }else{
6454      shellPrepare(db, pRc, z, ppStmt);
6455      sqlite3_free(z);
6456    }
6457  }
6458}
6459
6460/* Finalize the prepared statement created using shellPreparePrintf().
6461**
6462** This routine is could be marked "static".  But it is not always used,
6463** depending on compile-time options.  By omitting the "static", we avoid
6464** nuisance compiler warnings about "defined but not used".
6465*/
6466void shellFinalize(
6467  int *pRc,
6468  sqlite3_stmt *pStmt
6469){
6470  if( pStmt ){
6471    sqlite3 *db = sqlite3_db_handle(pStmt);
6472    int rc = sqlite3_finalize(pStmt);
6473    if( *pRc==SQLITE_OK ){
6474      if( rc!=SQLITE_OK ){
6475        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6476      }
6477      *pRc = rc;
6478    }
6479  }
6480}
6481
6482/* Reset the prepared statement created using shellPreparePrintf().
6483**
6484** This routine is could be marked "static".  But it is not always used,
6485** depending on compile-time options.  By omitting the "static", we avoid
6486** nuisance compiler warnings about "defined but not used".
6487*/
6488void shellReset(
6489  int *pRc,
6490  sqlite3_stmt *pStmt
6491){
6492  int rc = sqlite3_reset(pStmt);
6493  if( *pRc==SQLITE_OK ){
6494    if( rc!=SQLITE_OK ){
6495      sqlite3 *db = sqlite3_db_handle(pStmt);
6496      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6497    }
6498    *pRc = rc;
6499  }
6500}
6501#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6502
6503#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6504/******************************************************************************
6505** The ".archive" or ".ar" command.
6506*/
6507/*
6508** Structure representing a single ".ar" command.
6509*/
6510typedef struct ArCommand ArCommand;
6511struct ArCommand {
6512  u8 eCmd;                        /* An AR_CMD_* value */
6513  u8 bVerbose;                    /* True if --verbose */
6514  u8 bZip;                        /* True if the archive is a ZIP */
6515  u8 bDryRun;                     /* True if --dry-run */
6516  u8 bAppend;                     /* True if --append */
6517  u8 bGlob;                       /* True if --glob */
6518  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6519  int nArg;                       /* Number of command arguments */
6520  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6521  const char *zFile;              /* --file argument, or NULL */
6522  const char *zDir;               /* --directory argument, or NULL */
6523  char **azArg;                   /* Array of command arguments */
6524  ShellState *p;                  /* Shell state */
6525  sqlite3 *db;                    /* Database containing the archive */
6526};
6527
6528/*
6529** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6530*/
6531static int arUsage(FILE *f){
6532  showHelp(f,"archive");
6533  return SQLITE_ERROR;
6534}
6535
6536/*
6537** Print an error message for the .ar command to stderr and return
6538** SQLITE_ERROR.
6539*/
6540static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6541  va_list ap;
6542  char *z;
6543  va_start(ap, zFmt);
6544  z = sqlite3_vmprintf(zFmt, ap);
6545  va_end(ap);
6546  utf8_printf(stderr, "Error: %s\n", z);
6547  if( pAr->fromCmdLine ){
6548    utf8_printf(stderr, "Use \"-A\" for more help\n");
6549  }else{
6550    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6551  }
6552  sqlite3_free(z);
6553  return SQLITE_ERROR;
6554}
6555
6556/*
6557** Values for ArCommand.eCmd.
6558*/
6559#define AR_CMD_CREATE       1
6560#define AR_CMD_UPDATE       2
6561#define AR_CMD_INSERT       3
6562#define AR_CMD_EXTRACT      4
6563#define AR_CMD_LIST         5
6564#define AR_CMD_HELP         6
6565#define AR_CMD_REMOVE       7
6566
6567/*
6568** Other (non-command) switches.
6569*/
6570#define AR_SWITCH_VERBOSE     8
6571#define AR_SWITCH_FILE        9
6572#define AR_SWITCH_DIRECTORY  10
6573#define AR_SWITCH_APPEND     11
6574#define AR_SWITCH_DRYRUN     12
6575#define AR_SWITCH_GLOB       13
6576
6577static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6578  switch( eSwitch ){
6579    case AR_CMD_CREATE:
6580    case AR_CMD_EXTRACT:
6581    case AR_CMD_LIST:
6582    case AR_CMD_REMOVE:
6583    case AR_CMD_UPDATE:
6584    case AR_CMD_INSERT:
6585    case AR_CMD_HELP:
6586      if( pAr->eCmd ){
6587        return arErrorMsg(pAr, "multiple command options");
6588      }
6589      pAr->eCmd = eSwitch;
6590      break;
6591
6592    case AR_SWITCH_DRYRUN:
6593      pAr->bDryRun = 1;
6594      break;
6595    case AR_SWITCH_GLOB:
6596      pAr->bGlob = 1;
6597      break;
6598    case AR_SWITCH_VERBOSE:
6599      pAr->bVerbose = 1;
6600      break;
6601    case AR_SWITCH_APPEND:
6602      pAr->bAppend = 1;
6603      /* Fall thru into --file */
6604    case AR_SWITCH_FILE:
6605      pAr->zFile = zArg;
6606      break;
6607    case AR_SWITCH_DIRECTORY:
6608      pAr->zDir = zArg;
6609      break;
6610  }
6611
6612  return SQLITE_OK;
6613}
6614
6615/*
6616** Parse the command line for an ".ar" command. The results are written into
6617** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6618** successfully, otherwise an error message is written to stderr and
6619** SQLITE_ERROR returned.
6620*/
6621static int arParseCommand(
6622  char **azArg,                   /* Array of arguments passed to dot command */
6623  int nArg,                       /* Number of entries in azArg[] */
6624  ArCommand *pAr                  /* Populate this object */
6625){
6626  struct ArSwitch {
6627    const char *zLong;
6628    char cShort;
6629    u8 eSwitch;
6630    u8 bArg;
6631  } aSwitch[] = {
6632    { "create",    'c', AR_CMD_CREATE,       0 },
6633    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6634    { "insert",    'i', AR_CMD_INSERT,       0 },
6635    { "list",      't', AR_CMD_LIST,         0 },
6636    { "remove",    'r', AR_CMD_REMOVE,       0 },
6637    { "update",    'u', AR_CMD_UPDATE,       0 },
6638    { "help",      'h', AR_CMD_HELP,         0 },
6639    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6640    { "file",      'f', AR_SWITCH_FILE,      1 },
6641    { "append",    'a', AR_SWITCH_APPEND,    1 },
6642    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6643    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6644    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6645  };
6646  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6647  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6648
6649  if( nArg<=1 ){
6650    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6651    return arUsage(stderr);
6652  }else{
6653    char *z = azArg[1];
6654    if( z[0]!='-' ){
6655      /* Traditional style [tar] invocation */
6656      int i;
6657      int iArg = 2;
6658      for(i=0; z[i]; i++){
6659        const char *zArg = 0;
6660        struct ArSwitch *pOpt;
6661        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6662          if( z[i]==pOpt->cShort ) break;
6663        }
6664        if( pOpt==pEnd ){
6665          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6666        }
6667        if( pOpt->bArg ){
6668          if( iArg>=nArg ){
6669            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6670          }
6671          zArg = azArg[iArg++];
6672        }
6673        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6674      }
6675      pAr->nArg = nArg-iArg;
6676      if( pAr->nArg>0 ){
6677        pAr->azArg = &azArg[iArg];
6678      }
6679    }else{
6680      /* Non-traditional invocation */
6681      int iArg;
6682      for(iArg=1; iArg<nArg; iArg++){
6683        int n;
6684        z = azArg[iArg];
6685        if( z[0]!='-' ){
6686          /* All remaining command line words are command arguments. */
6687          pAr->azArg = &azArg[iArg];
6688          pAr->nArg = nArg-iArg;
6689          break;
6690        }
6691        n = strlen30(z);
6692
6693        if( z[1]!='-' ){
6694          int i;
6695          /* One or more short options */
6696          for(i=1; i<n; i++){
6697            const char *zArg = 0;
6698            struct ArSwitch *pOpt;
6699            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6700              if( z[i]==pOpt->cShort ) break;
6701            }
6702            if( pOpt==pEnd ){
6703              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6704            }
6705            if( pOpt->bArg ){
6706              if( i<(n-1) ){
6707                zArg = &z[i+1];
6708                i = n;
6709              }else{
6710                if( iArg>=(nArg-1) ){
6711                  return arErrorMsg(pAr, "option requires an argument: %c",
6712                                    z[i]);
6713                }
6714                zArg = azArg[++iArg];
6715              }
6716            }
6717            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6718          }
6719        }else if( z[2]=='\0' ){
6720          /* A -- option, indicating that all remaining command line words
6721          ** are command arguments.  */
6722          pAr->azArg = &azArg[iArg+1];
6723          pAr->nArg = nArg-iArg-1;
6724          break;
6725        }else{
6726          /* A long option */
6727          const char *zArg = 0;             /* Argument for option, if any */
6728          struct ArSwitch *pMatch = 0;      /* Matching option */
6729          struct ArSwitch *pOpt;            /* Iterator */
6730          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6731            const char *zLong = pOpt->zLong;
6732            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6733              if( pMatch ){
6734                return arErrorMsg(pAr, "ambiguous option: %s",z);
6735              }else{
6736                pMatch = pOpt;
6737              }
6738            }
6739          }
6740
6741          if( pMatch==0 ){
6742            return arErrorMsg(pAr, "unrecognized option: %s", z);
6743          }
6744          if( pMatch->bArg ){
6745            if( iArg>=(nArg-1) ){
6746              return arErrorMsg(pAr, "option requires an argument: %s", z);
6747            }
6748            zArg = azArg[++iArg];
6749          }
6750          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6751        }
6752      }
6753    }
6754  }
6755
6756  return SQLITE_OK;
6757}
6758
6759/*
6760** This function assumes that all arguments within the ArCommand.azArg[]
6761** array refer to archive members, as for the --extract, --list or --remove
6762** commands. It checks that each of them are "present". If any specified
6763** file is not present in the archive, an error is printed to stderr and an
6764** error code returned. Otherwise, if all specified arguments are present
6765** in the archive, SQLITE_OK is returned. Here, "present" means either an
6766** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6767** when pAr->bGlob is true.
6768**
6769** This function strips any trailing '/' characters from each argument.
6770** This is consistent with the way the [tar] command seems to work on
6771** Linux.
6772*/
6773static int arCheckEntries(ArCommand *pAr){
6774  int rc = SQLITE_OK;
6775  if( pAr->nArg ){
6776    int i, j;
6777    sqlite3_stmt *pTest = 0;
6778    const char *zSel = (pAr->bGlob)
6779      ? "SELECT name FROM %s WHERE glob($name,name)"
6780      : "SELECT name FROM %s WHERE name=$name";
6781
6782    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6783    j = sqlite3_bind_parameter_index(pTest, "$name");
6784    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6785      char *z = pAr->azArg[i];
6786      int n = strlen30(z);
6787      int bOk = 0;
6788      while( n>0 && z[n-1]=='/' ) n--;
6789      z[n] = '\0';
6790      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6791      if( SQLITE_ROW==sqlite3_step(pTest) ){
6792        bOk = 1;
6793      }
6794      shellReset(&rc, pTest);
6795      if( rc==SQLITE_OK && bOk==0 ){
6796        utf8_printf(stderr, "not found in archive: %s\n", z);
6797        rc = SQLITE_ERROR;
6798      }
6799    }
6800    shellFinalize(&rc, pTest);
6801  }
6802  return rc;
6803}
6804
6805/*
6806** Format a WHERE clause that can be used against the "sqlar" table to
6807** identify all archive members that match the command arguments held
6808** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6809** The caller is responsible for eventually calling sqlite3_free() on
6810** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6811** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6812*/
6813static void arWhereClause(
6814  int *pRc,
6815  ArCommand *pAr,
6816  char **pzWhere                  /* OUT: New WHERE clause */
6817){
6818  char *zWhere = 0;
6819  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6820  if( *pRc==SQLITE_OK ){
6821    if( pAr->nArg==0 ){
6822      zWhere = sqlite3_mprintf("1");
6823    }else{
6824      int i;
6825      const char *zSep = "";
6826      for(i=0; i<pAr->nArg; i++){
6827        const char *z = pAr->azArg[i];
6828        zWhere = sqlite3_mprintf(
6829          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6830          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6831        );
6832        if( zWhere==0 ){
6833          *pRc = SQLITE_NOMEM;
6834          break;
6835        }
6836        zSep = " OR ";
6837      }
6838    }
6839  }
6840  *pzWhere = zWhere;
6841}
6842
6843/*
6844** Implementation of .ar "lisT" command.
6845*/
6846static int arListCommand(ArCommand *pAr){
6847  const char *zSql = "SELECT %s FROM %s WHERE %s";
6848  const char *azCols[] = {
6849    "name",
6850    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6851  };
6852
6853  char *zWhere = 0;
6854  sqlite3_stmt *pSql = 0;
6855  int rc;
6856
6857  rc = arCheckEntries(pAr);
6858  arWhereClause(&rc, pAr, &zWhere);
6859
6860  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6861                     pAr->zSrcTable, zWhere);
6862  if( pAr->bDryRun ){
6863    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6864  }else{
6865    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6866      if( pAr->bVerbose ){
6867        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6868            sqlite3_column_text(pSql, 0),
6869            sqlite3_column_int(pSql, 1),
6870            sqlite3_column_text(pSql, 2),
6871            sqlite3_column_text(pSql, 3)
6872        );
6873      }else{
6874        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6875      }
6876    }
6877  }
6878  shellFinalize(&rc, pSql);
6879  sqlite3_free(zWhere);
6880  return rc;
6881}
6882
6883
6884/*
6885** Implementation of .ar "Remove" command.
6886*/
6887static int arRemoveCommand(ArCommand *pAr){
6888  int rc = 0;
6889  char *zSql = 0;
6890  char *zWhere = 0;
6891
6892  if( pAr->nArg ){
6893    /* Verify that args actually exist within the archive before proceeding.
6894    ** And formulate a WHERE clause to match them.  */
6895    rc = arCheckEntries(pAr);
6896    arWhereClause(&rc, pAr, &zWhere);
6897  }
6898  if( rc==SQLITE_OK ){
6899    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6900                           pAr->zSrcTable, zWhere);
6901    if( pAr->bDryRun ){
6902      utf8_printf(pAr->p->out, "%s\n", zSql);
6903    }else{
6904      char *zErr = 0;
6905      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6906      if( rc==SQLITE_OK ){
6907        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6908        if( rc!=SQLITE_OK ){
6909          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6910        }else{
6911          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6912        }
6913      }
6914      if( zErr ){
6915        utf8_printf(stdout, "ERROR: %s\n", zErr);
6916        sqlite3_free(zErr);
6917      }
6918    }
6919  }
6920  sqlite3_free(zWhere);
6921  sqlite3_free(zSql);
6922  return rc;
6923}
6924
6925/*
6926** Implementation of .ar "eXtract" command.
6927*/
6928static int arExtractCommand(ArCommand *pAr){
6929  const char *zSql1 =
6930    "SELECT "
6931    " ($dir || name),"
6932    " writefile(($dir || name), %s, mode, mtime) "
6933    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6934    " AND name NOT GLOB '*..[/\\]*'";
6935
6936  const char *azExtraArg[] = {
6937    "sqlar_uncompress(data, sz)",
6938    "data"
6939  };
6940
6941  sqlite3_stmt *pSql = 0;
6942  int rc = SQLITE_OK;
6943  char *zDir = 0;
6944  char *zWhere = 0;
6945  int i, j;
6946
6947  /* If arguments are specified, check that they actually exist within
6948  ** the archive before proceeding. And formulate a WHERE clause to
6949  ** match them.  */
6950  rc = arCheckEntries(pAr);
6951  arWhereClause(&rc, pAr, &zWhere);
6952
6953  if( rc==SQLITE_OK ){
6954    if( pAr->zDir ){
6955      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6956    }else{
6957      zDir = sqlite3_mprintf("");
6958    }
6959    if( zDir==0 ) rc = SQLITE_NOMEM;
6960  }
6961
6962  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6963      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6964  );
6965
6966  if( rc==SQLITE_OK ){
6967    j = sqlite3_bind_parameter_index(pSql, "$dir");
6968    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6969
6970    /* Run the SELECT statement twice. The first time, writefile() is called
6971    ** for all archive members that should be extracted. The second time,
6972    ** only for the directories. This is because the timestamps for
6973    ** extracted directories must be reset after they are populated (as
6974    ** populating them changes the timestamp).  */
6975    for(i=0; i<2; i++){
6976      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6977      sqlite3_bind_int(pSql, j, i);
6978      if( pAr->bDryRun ){
6979        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6980      }else{
6981        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6982          if( i==0 && pAr->bVerbose ){
6983            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6984          }
6985        }
6986      }
6987      shellReset(&rc, pSql);
6988    }
6989    shellFinalize(&rc, pSql);
6990  }
6991
6992  sqlite3_free(zDir);
6993  sqlite3_free(zWhere);
6994  return rc;
6995}
6996
6997/*
6998** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6999*/
7000static int arExecSql(ArCommand *pAr, const char *zSql){
7001  int rc;
7002  if( pAr->bDryRun ){
7003    utf8_printf(pAr->p->out, "%s\n", zSql);
7004    rc = SQLITE_OK;
7005  }else{
7006    char *zErr = 0;
7007    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7008    if( zErr ){
7009      utf8_printf(stdout, "ERROR: %s\n", zErr);
7010      sqlite3_free(zErr);
7011    }
7012  }
7013  return rc;
7014}
7015
7016
7017/*
7018** Implementation of .ar "create", "insert", and "update" commands.
7019**
7020**     create    ->     Create a new SQL archive
7021**     insert    ->     Insert or reinsert all files listed
7022**     update    ->     Insert files that have changed or that were not
7023**                      previously in the archive
7024**
7025** Create the "sqlar" table in the database if it does not already exist.
7026** Then add each file in the azFile[] array to the archive. Directories
7027** are added recursively. If argument bVerbose is non-zero, a message is
7028** printed on stdout for each file archived.
7029**
7030** The create command is the same as update, except that it drops
7031** any existing "sqlar" table before beginning.  The "insert" command
7032** always overwrites every file named on the command-line, where as
7033** "update" only overwrites if the size or mtime or mode has changed.
7034*/
7035static int arCreateOrUpdateCommand(
7036  ArCommand *pAr,                 /* Command arguments and options */
7037  int bUpdate,                    /* true for a --create. */
7038  int bOnlyIfChanged              /* Only update if file has changed */
7039){
7040  const char *zCreate =
7041      "CREATE TABLE IF NOT EXISTS sqlar(\n"
7042      "  name TEXT PRIMARY KEY,  -- name of the file\n"
7043      "  mode INT,               -- access permissions\n"
7044      "  mtime INT,              -- last modification time\n"
7045      "  sz INT,                 -- original file size\n"
7046      "  data BLOB               -- compressed content\n"
7047      ")";
7048  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7049  const char *zInsertFmt[2] = {
7050     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7051     "  SELECT\n"
7052     "    %s,\n"
7053     "    mode,\n"
7054     "    mtime,\n"
7055     "    CASE substr(lsmode(mode),1,1)\n"
7056     "      WHEN '-' THEN length(data)\n"
7057     "      WHEN 'd' THEN 0\n"
7058     "      ELSE -1 END,\n"
7059     "    sqlar_compress(data)\n"
7060     "  FROM fsdir(%Q,%Q) AS disk\n"
7061     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7062     ,
7063     "REPLACE INTO %s(name,mode,mtime,data)\n"
7064     "  SELECT\n"
7065     "    %s,\n"
7066     "    mode,\n"
7067     "    mtime,\n"
7068     "    data\n"
7069     "  FROM fsdir(%Q,%Q) AS disk\n"
7070     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7071  };
7072  int i;                          /* For iterating through azFile[] */
7073  int rc;                         /* Return code */
7074  const char *zTab = 0;           /* SQL table into which to insert */
7075  char *zSql;
7076  char zTemp[50];
7077  char *zExists = 0;
7078
7079  arExecSql(pAr, "PRAGMA page_size=512");
7080  rc = arExecSql(pAr, "SAVEPOINT ar;");
7081  if( rc!=SQLITE_OK ) return rc;
7082  zTemp[0] = 0;
7083  if( pAr->bZip ){
7084    /* Initialize the zipfile virtual table, if necessary */
7085    if( pAr->zFile ){
7086      sqlite3_uint64 r;
7087      sqlite3_randomness(sizeof(r),&r);
7088      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7089      zTab = zTemp;
7090      zSql = sqlite3_mprintf(
7091         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7092         zTab, pAr->zFile
7093      );
7094      rc = arExecSql(pAr, zSql);
7095      sqlite3_free(zSql);
7096    }else{
7097      zTab = "zip";
7098    }
7099  }else{
7100    /* Initialize the table for an SQLAR */
7101    zTab = "sqlar";
7102    if( bUpdate==0 ){
7103      rc = arExecSql(pAr, zDrop);
7104      if( rc!=SQLITE_OK ) goto end_ar_transaction;
7105    }
7106    rc = arExecSql(pAr, zCreate);
7107  }
7108  if( bOnlyIfChanged ){
7109    zExists = sqlite3_mprintf(
7110      " AND NOT EXISTS("
7111          "SELECT 1 FROM %s AS mem"
7112          " WHERE mem.name=disk.name"
7113          " AND mem.mtime=disk.mtime"
7114          " AND mem.mode=disk.mode)", zTab);
7115  }else{
7116    zExists = sqlite3_mprintf("");
7117  }
7118  if( zExists==0 ) rc = SQLITE_NOMEM;
7119  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7120    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7121        pAr->bVerbose ? "shell_putsnl(name)" : "name",
7122        pAr->azArg[i], pAr->zDir, zExists);
7123    rc = arExecSql(pAr, zSql2);
7124    sqlite3_free(zSql2);
7125  }
7126end_ar_transaction:
7127  if( rc!=SQLITE_OK ){
7128    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7129  }else{
7130    rc = arExecSql(pAr, "RELEASE ar;");
7131    if( pAr->bZip && pAr->zFile ){
7132      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7133      arExecSql(pAr, zSql);
7134      sqlite3_free(zSql);
7135    }
7136  }
7137  sqlite3_free(zExists);
7138  return rc;
7139}
7140
7141/*
7142** Implementation of ".ar" dot command.
7143*/
7144static int arDotCommand(
7145  ShellState *pState,          /* Current shell tool state */
7146  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
7147  char **azArg,                /* Array of arguments passed to dot command */
7148  int nArg                     /* Number of entries in azArg[] */
7149){
7150  ArCommand cmd;
7151  int rc;
7152  memset(&cmd, 0, sizeof(cmd));
7153  cmd.fromCmdLine = fromCmdLine;
7154  rc = arParseCommand(azArg, nArg, &cmd);
7155  if( rc==SQLITE_OK ){
7156    int eDbType = SHELL_OPEN_UNSPEC;
7157    cmd.p = pState;
7158    cmd.db = pState->db;
7159    if( cmd.zFile ){
7160      eDbType = deduceDatabaseType(cmd.zFile, 1);
7161    }else{
7162      eDbType = pState->openMode;
7163    }
7164    if( eDbType==SHELL_OPEN_ZIPFILE ){
7165      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7166        if( cmd.zFile==0 ){
7167          cmd.zSrcTable = sqlite3_mprintf("zip");
7168        }else{
7169          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7170        }
7171      }
7172      cmd.bZip = 1;
7173    }else if( cmd.zFile ){
7174      int flags;
7175      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7176      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7177           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7178        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7179      }else{
7180        flags = SQLITE_OPEN_READONLY;
7181      }
7182      cmd.db = 0;
7183      if( cmd.bDryRun ){
7184        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7185             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7186      }
7187      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7188             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7189      if( rc!=SQLITE_OK ){
7190        utf8_printf(stderr, "cannot open file: %s (%s)\n",
7191            cmd.zFile, sqlite3_errmsg(cmd.db)
7192        );
7193        goto end_ar_command;
7194      }
7195      sqlite3_fileio_init(cmd.db, 0, 0);
7196      sqlite3_sqlar_init(cmd.db, 0, 0);
7197      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7198                              shellPutsFunc, 0, 0);
7199
7200    }
7201    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7202      if( cmd.eCmd!=AR_CMD_CREATE
7203       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7204      ){
7205        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7206        rc = SQLITE_ERROR;
7207        goto end_ar_command;
7208      }
7209      cmd.zSrcTable = sqlite3_mprintf("sqlar");
7210    }
7211
7212    switch( cmd.eCmd ){
7213      case AR_CMD_CREATE:
7214        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7215        break;
7216
7217      case AR_CMD_EXTRACT:
7218        rc = arExtractCommand(&cmd);
7219        break;
7220
7221      case AR_CMD_LIST:
7222        rc = arListCommand(&cmd);
7223        break;
7224
7225      case AR_CMD_HELP:
7226        arUsage(pState->out);
7227        break;
7228
7229      case AR_CMD_INSERT:
7230        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7231        break;
7232
7233      case AR_CMD_REMOVE:
7234        rc = arRemoveCommand(&cmd);
7235        break;
7236
7237      default:
7238        assert( cmd.eCmd==AR_CMD_UPDATE );
7239        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7240        break;
7241    }
7242  }
7243end_ar_command:
7244  if( cmd.db!=pState->db ){
7245    close_db(cmd.db);
7246  }
7247  sqlite3_free(cmd.zSrcTable);
7248
7249  return rc;
7250}
7251/* End of the ".archive" or ".ar" command logic
7252*******************************************************************************/
7253#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7254
7255#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7256/*
7257** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
7258** Otherwise, the SQL statement or statements in zSql are executed using
7259** database connection db and the error code written to *pRc before
7260** this function returns.
7261*/
7262static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
7263  int rc = *pRc;
7264  if( rc==SQLITE_OK ){
7265    char *zErr = 0;
7266    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
7267    if( rc!=SQLITE_OK ){
7268      raw_printf(stderr, "SQL error: %s\n", zErr);
7269    }
7270    sqlite3_free(zErr);
7271    *pRc = rc;
7272  }
7273}
7274
7275/*
7276** Like shellExec(), except that zFmt is a printf() style format string.
7277*/
7278static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
7279  char *z = 0;
7280  if( *pRc==SQLITE_OK ){
7281    va_list ap;
7282    va_start(ap, zFmt);
7283    z = sqlite3_vmprintf(zFmt, ap);
7284    va_end(ap);
7285    if( z==0 ){
7286      *pRc = SQLITE_NOMEM;
7287    }else{
7288      shellExec(db, pRc, z);
7289    }
7290    sqlite3_free(z);
7291  }
7292}
7293
7294/*
7295** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7296** Otherwise, an attempt is made to allocate, zero and return a pointer
7297** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
7298** to SQLITE_NOMEM and NULL returned.
7299*/
7300static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
7301  void *pRet = 0;
7302  if( *pRc==SQLITE_OK ){
7303    pRet = sqlite3_malloc64(nByte);
7304    if( pRet==0 ){
7305      *pRc = SQLITE_NOMEM;
7306    }else{
7307      memset(pRet, 0, nByte);
7308    }
7309  }
7310  return pRet;
7311}
7312
7313/*
7314** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
7315** Otherwise, zFmt is treated as a printf() style string. The result of
7316** formatting it along with any trailing arguments is written into a
7317** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
7318** It is the responsibility of the caller to eventually free this buffer
7319** using a call to sqlite3_free().
7320**
7321** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
7322** pointer returned.
7323*/
7324static char *shellMPrintf(int *pRc, const char *zFmt, ...){
7325  char *z = 0;
7326  if( *pRc==SQLITE_OK ){
7327    va_list ap;
7328    va_start(ap, zFmt);
7329    z = sqlite3_vmprintf(zFmt, ap);
7330    va_end(ap);
7331    if( z==0 ){
7332      *pRc = SQLITE_NOMEM;
7333    }
7334  }
7335  return z;
7336}
7337
7338
7339/*
7340** When running the ".recover" command, each output table, and the special
7341** orphaned row table if it is required, is represented by an instance
7342** of the following struct.
7343*/
7344typedef struct RecoverTable RecoverTable;
7345struct RecoverTable {
7346  char *zQuoted;                  /* Quoted version of table name */
7347  int nCol;                       /* Number of columns in table */
7348  char **azlCol;                  /* Array of column lists */
7349  int iPk;                        /* Index of IPK column */
7350};
7351
7352/*
7353** Free a RecoverTable object allocated by recoverFindTable() or
7354** recoverOrphanTable().
7355*/
7356static void recoverFreeTable(RecoverTable *pTab){
7357  if( pTab ){
7358    sqlite3_free(pTab->zQuoted);
7359    if( pTab->azlCol ){
7360      int i;
7361      for(i=0; i<=pTab->nCol; i++){
7362        sqlite3_free(pTab->azlCol[i]);
7363      }
7364      sqlite3_free(pTab->azlCol);
7365    }
7366    sqlite3_free(pTab);
7367  }
7368}
7369
7370/*
7371** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
7372** Otherwise, it allocates and returns a RecoverTable object based on the
7373** final four arguments passed to this function. It is the responsibility
7374** of the caller to eventually free the returned object using
7375** recoverFreeTable().
7376*/
7377static RecoverTable *recoverNewTable(
7378  int *pRc,                       /* IN/OUT: Error code */
7379  const char *zName,              /* Name of table */
7380  const char *zSql,               /* CREATE TABLE statement */
7381  int bIntkey,
7382  int nCol
7383){
7384  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
7385  int rc = *pRc;
7386  RecoverTable *pTab = 0;
7387
7388  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
7389  if( rc==SQLITE_OK ){
7390    int nSqlCol = 0;
7391    int bSqlIntkey = 0;
7392    sqlite3_stmt *pStmt = 0;
7393
7394    rc = sqlite3_open("", &dbtmp);
7395    if( rc==SQLITE_OK ){
7396      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
7397                              shellIdQuote, 0, 0);
7398    }
7399    if( rc==SQLITE_OK ){
7400      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
7401    }
7402    if( rc==SQLITE_OK ){
7403      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
7404      if( rc==SQLITE_ERROR ){
7405        rc = SQLITE_OK;
7406        goto finished;
7407      }
7408    }
7409    shellPreparePrintf(dbtmp, &rc, &pStmt,
7410        "SELECT count(*) FROM pragma_table_info(%Q)", zName
7411    );
7412    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7413      nSqlCol = sqlite3_column_int(pStmt, 0);
7414    }
7415    shellFinalize(&rc, pStmt);
7416
7417    if( rc!=SQLITE_OK || nSqlCol<nCol ){
7418      goto finished;
7419    }
7420
7421    shellPreparePrintf(dbtmp, &rc, &pStmt,
7422      "SELECT ("
7423      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
7424      ") FROM sqlite_schema WHERE name = %Q", zName
7425    );
7426    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7427      bSqlIntkey = sqlite3_column_int(pStmt, 0);
7428    }
7429    shellFinalize(&rc, pStmt);
7430
7431    if( bIntkey==bSqlIntkey ){
7432      int i;
7433      const char *zPk = "_rowid_";
7434      sqlite3_stmt *pPkFinder = 0;
7435
7436      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
7437      ** set zPk to the name of the PK column, and pTab->iPk to the index
7438      ** of the column, where columns are 0-numbered from left to right.
7439      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7440      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7441      pTab->iPk = -2;
7442      if( bIntkey ){
7443        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7444          "SELECT cid, name FROM pragma_table_info(%Q) "
7445          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7446          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7447          , zName, zName
7448        );
7449        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7450          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7451          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7452          if( zPk==0 ){ zPk = "_";  /* Defensive.  Should never happen */ }
7453        }
7454      }
7455
7456      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7457      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7458      pTab->nCol = nSqlCol;
7459
7460      if( bIntkey ){
7461        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7462      }else{
7463        pTab->azlCol[0] = shellMPrintf(&rc, "");
7464      }
7465      i = 1;
7466      shellPreparePrintf(dbtmp, &rc, &pStmt,
7467          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7468          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7469          "FROM pragma_table_info(%Q)",
7470          bIntkey ? ", " : "", pTab->iPk,
7471          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7472          zName
7473      );
7474      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7475        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7476        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7477        i++;
7478      }
7479      shellFinalize(&rc, pStmt);
7480
7481      shellFinalize(&rc, pPkFinder);
7482    }
7483  }
7484
7485 finished:
7486  sqlite3_close(dbtmp);
7487  *pRc = rc;
7488  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7489    recoverFreeTable(pTab);
7490    pTab = 0;
7491  }
7492  return pTab;
7493}
7494
7495/*
7496** This function is called to search the schema recovered from the
7497** sqlite_schema table of the (possibly) corrupt database as part
7498** of a ".recover" command. Specifically, for a table with root page
7499** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7500** table must be a WITHOUT ROWID table, or if non-zero, not one of
7501** those.
7502**
7503** If a table is found, a (RecoverTable*) object is returned. Or, if
7504** no such table is found, but bIntkey is false and iRoot is the
7505** root page of an index in the recovered schema, then (*pbNoop) is
7506** set to true and NULL returned. Or, if there is no such table or
7507** index, NULL is returned and (*pbNoop) set to 0, indicating that
7508** the caller should write data to the orphans table.
7509*/
7510static RecoverTable *recoverFindTable(
7511  ShellState *pState,             /* Shell state object */
7512  int *pRc,                       /* IN/OUT: Error code */
7513  int iRoot,                      /* Root page of table */
7514  int bIntkey,                    /* True for an intkey table */
7515  int nCol,                       /* Number of columns in table */
7516  int *pbNoop                     /* OUT: True if iRoot is root of index */
7517){
7518  sqlite3_stmt *pStmt = 0;
7519  RecoverTable *pRet = 0;
7520  int bNoop = 0;
7521  const char *zSql = 0;
7522  const char *zName = 0;
7523
7524  /* Search the recovered schema for an object with root page iRoot. */
7525  shellPreparePrintf(pState->db, pRc, &pStmt,
7526      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7527  );
7528  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7529    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7530    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7531      bNoop = 1;
7532      break;
7533    }
7534    if( sqlite3_stricmp(zType, "table")==0 ){
7535      zName = (const char*)sqlite3_column_text(pStmt, 1);
7536      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7537      if( zName!=0 && zSql!=0 ){
7538        pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7539        break;
7540      }
7541    }
7542  }
7543
7544  shellFinalize(pRc, pStmt);
7545  *pbNoop = bNoop;
7546  return pRet;
7547}
7548
7549/*
7550** Return a RecoverTable object representing the orphans table.
7551*/
7552static RecoverTable *recoverOrphanTable(
7553  ShellState *pState,             /* Shell state object */
7554  int *pRc,                       /* IN/OUT: Error code */
7555  const char *zLostAndFound,      /* Base name for orphans table */
7556  int nCol                        /* Number of user data columns */
7557){
7558  RecoverTable *pTab = 0;
7559  if( nCol>=0 && *pRc==SQLITE_OK ){
7560    int i;
7561
7562    /* This block determines the name of the orphan table. The prefered
7563    ** name is zLostAndFound. But if that clashes with another name
7564    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7565    ** and so on until a non-clashing name is found.  */
7566    int iTab = 0;
7567    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7568    sqlite3_stmt *pTest = 0;
7569    shellPrepare(pState->db, pRc,
7570        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7571    );
7572    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7573    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7574      shellReset(pRc, pTest);
7575      sqlite3_free(zTab);
7576      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7577      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7578    }
7579    shellFinalize(pRc, pTest);
7580
7581    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7582    if( pTab ){
7583      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7584      pTab->nCol = nCol;
7585      pTab->iPk = -2;
7586      if( nCol>0 ){
7587        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7588        if( pTab->azlCol ){
7589          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7590          for(i=nCol-1; i>=0; i--){
7591            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7592          }
7593        }
7594      }
7595
7596      if( *pRc!=SQLITE_OK ){
7597        recoverFreeTable(pTab);
7598        pTab = 0;
7599      }else{
7600        raw_printf(pState->out,
7601            "CREATE TABLE %s(rootpgno INTEGER, "
7602            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7603        );
7604        for(i=0; i<nCol; i++){
7605          raw_printf(pState->out, ", c%d", i);
7606        }
7607        raw_printf(pState->out, ");\n");
7608      }
7609    }
7610    sqlite3_free(zTab);
7611  }
7612  return pTab;
7613}
7614
7615/*
7616** This function is called to recover data from the database. A script
7617** to construct a new database containing all recovered data is output
7618** on stream pState->out.
7619*/
7620static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7621  int rc = SQLITE_OK;
7622  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7623  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7624  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7625  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7626  const char *zLostAndFound = "lost_and_found";
7627  int i;
7628  int nOrphan = -1;
7629  RecoverTable *pOrphan = 0;
7630
7631  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7632  int bRowids = 1;                /* 0 if --no-rowids */
7633  for(i=1; i<nArg; i++){
7634    char *z = azArg[i];
7635    int n;
7636    if( z[0]=='-' && z[1]=='-' ) z++;
7637    n = strlen30(z);
7638    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7639      bFreelist = 0;
7640    }else
7641    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7642      i++;
7643      zRecoveryDb = azArg[i];
7644    }else
7645    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7646      i++;
7647      zLostAndFound = azArg[i];
7648    }else
7649    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7650      bRowids = 0;
7651    }
7652    else{
7653      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7654      showHelp(pState->out, azArg[0]);
7655      return 1;
7656    }
7657  }
7658
7659  shellExecPrintf(pState->db, &rc,
7660    /* Attach an in-memory database named 'recovery'. Create an indexed
7661    ** cache of the sqlite_dbptr virtual table. */
7662    "PRAGMA writable_schema = on;"
7663    "ATTACH %Q AS recovery;"
7664    "DROP TABLE IF EXISTS recovery.dbptr;"
7665    "DROP TABLE IF EXISTS recovery.freelist;"
7666    "DROP TABLE IF EXISTS recovery.map;"
7667    "DROP TABLE IF EXISTS recovery.schema;"
7668    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7669  );
7670
7671  if( bFreelist ){
7672    shellExec(pState->db, &rc,
7673      "WITH trunk(pgno) AS ("
7674      "  SELECT shell_int32("
7675      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7676      "      WHERE x>0"
7677      "    UNION"
7678      "  SELECT shell_int32("
7679      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7680      "      FROM trunk WHERE x>0"
7681      "),"
7682      "freelist(data, n, freepgno) AS ("
7683      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7684      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7685      "    UNION ALL"
7686      "  SELECT data, n-1, shell_int32(data, 2+n) "
7687      "      FROM freelist WHERE n>=0"
7688      ")"
7689      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7690    );
7691  }
7692
7693  /* If this is an auto-vacuum database, add all pointer-map pages to
7694  ** the freelist table. Do this regardless of whether or not
7695  ** --freelist-corrupt was specified.  */
7696  shellExec(pState->db, &rc,
7697    "WITH ptrmap(pgno) AS ("
7698    "  SELECT 2 WHERE shell_int32("
7699    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7700    "  )"
7701    "    UNION ALL "
7702    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7703    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7704    ")"
7705    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7706  );
7707
7708  shellExec(pState->db, &rc,
7709    "CREATE TABLE recovery.dbptr("
7710    "      pgno, child, PRIMARY KEY(child, pgno)"
7711    ") WITHOUT ROWID;"
7712    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7713    "    SELECT * FROM sqlite_dbptr"
7714    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7715
7716    /* Delete any pointer to page 1. This ensures that page 1 is considered
7717    ** a root page, regardless of how corrupt the db is. */
7718    "DELETE FROM recovery.dbptr WHERE child = 1;"
7719
7720    /* Delete all pointers to any pages that have more than one pointer
7721    ** to them. Such pages will be treated as root pages when recovering
7722    ** data.  */
7723    "DELETE FROM recovery.dbptr WHERE child IN ("
7724    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7725    ");"
7726
7727    /* Create the "map" table that will (eventually) contain instructions
7728    ** for dealing with each page in the db that contains one or more
7729    ** records. */
7730    "CREATE TABLE recovery.map("
7731      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7732    ");"
7733
7734    /* Populate table [map]. If there are circular loops of pages in the
7735    ** database, the following adds all pages in such a loop to the map
7736    ** as individual root pages. This could be handled better.  */
7737    "WITH pages(i, maxlen) AS ("
7738    "  SELECT page_count, ("
7739    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7740    "  ) FROM pragma_page_count WHERE page_count>0"
7741    "    UNION ALL"
7742    "  SELECT i-1, ("
7743    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7744    "  ) FROM pages WHERE i>=2"
7745    ")"
7746    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7747    "  SELECT i, maxlen, NULL, ("
7748    "    WITH p(orig, pgno, parent) AS ("
7749    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7750    "        UNION "
7751    "      SELECT i, p.parent, "
7752    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7753    "    )"
7754    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7755    ") "
7756    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7757    "UPDATE recovery.map AS o SET intkey = ("
7758    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7759    ");"
7760
7761    /* Extract data from page 1 and any linked pages into table
7762    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7763    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7764    "INSERT INTO recovery.schema SELECT "
7765    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7766    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7767    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7768    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7769    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7770    "FROM sqlite_dbdata WHERE pgno IN ("
7771    "  SELECT pgno FROM recovery.map WHERE root=1"
7772    ")"
7773    "GROUP BY pgno, cell;"
7774    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7775  );
7776
7777  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7778  ** CREATE TABLE statements that extracted from the existing schema.  */
7779  if( rc==SQLITE_OK ){
7780    sqlite3_stmt *pStmt = 0;
7781    /* ".recover" might output content in an order which causes immediate
7782    ** foreign key constraints to be violated. So disable foreign-key
7783    ** constraint enforcement to prevent problems when running the output
7784    ** script. */
7785    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7786    raw_printf(pState->out, "BEGIN;\n");
7787    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7788    shellPrepare(pState->db, &rc,
7789        "SELECT sql FROM recovery.schema "
7790        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7791    );
7792    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7793      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7794      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7795          &zCreateTable[12]
7796      );
7797    }
7798    shellFinalize(&rc, pStmt);
7799  }
7800
7801  /* Figure out if an orphan table will be required. And if so, how many
7802  ** user columns it should contain */
7803  shellPrepare(pState->db, &rc,
7804      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7805      , &pLoop
7806  );
7807  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7808    nOrphan = sqlite3_column_int(pLoop, 0);
7809  }
7810  shellFinalize(&rc, pLoop);
7811  pLoop = 0;
7812
7813  shellPrepare(pState->db, &rc,
7814      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7815  );
7816
7817  shellPrepare(pState->db, &rc,
7818      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7819      "(case when (? AND field<0) then NULL else value end)"
7820      "), ', ')"
7821      ", min(field) "
7822      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7823      "GROUP BY cell", &pCells
7824  );
7825
7826  /* Loop through each root page. */
7827  shellPrepare(pState->db, &rc,
7828      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7829      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7830      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7831      ")", &pLoop
7832  );
7833  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7834    int iRoot = sqlite3_column_int(pLoop, 0);
7835    int bIntkey = sqlite3_column_int(pLoop, 1);
7836    int nCol = sqlite3_column_int(pLoop, 2);
7837    int bNoop = 0;
7838    RecoverTable *pTab;
7839
7840    assert( bIntkey==0 || bIntkey==1 );
7841    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7842    if( bNoop || rc ) continue;
7843    if( pTab==0 ){
7844      if( pOrphan==0 ){
7845        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7846      }
7847      pTab = pOrphan;
7848      if( pTab==0 ) break;
7849    }
7850
7851    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7852      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7853    }
7854    sqlite3_bind_int(pPages, 1, iRoot);
7855    if( bRowids==0 && pTab->iPk<0 ){
7856      sqlite3_bind_int(pCells, 1, 1);
7857    }else{
7858      sqlite3_bind_int(pCells, 1, 0);
7859    }
7860    sqlite3_bind_int(pCells, 3, pTab->iPk);
7861
7862    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7863      int iPgno = sqlite3_column_int(pPages, 0);
7864      sqlite3_bind_int(pCells, 2, iPgno);
7865      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7866        int nField = sqlite3_column_int(pCells, 0);
7867        int iMin = sqlite3_column_int(pCells, 2);
7868        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7869
7870        RecoverTable *pTab2 = pTab;
7871        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7872          if( pOrphan==0 ){
7873            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7874          }
7875          pTab2 = pOrphan;
7876          if( pTab2==0 ) break;
7877        }
7878
7879        nField = nField+1;
7880        if( pTab2==pOrphan ){
7881          raw_printf(pState->out,
7882              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7883              pTab2->zQuoted, iRoot, iPgno, nField,
7884              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7885          );
7886        }else{
7887          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7888              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7889          );
7890        }
7891      }
7892      shellReset(&rc, pCells);
7893    }
7894    shellReset(&rc, pPages);
7895    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7896  }
7897  shellFinalize(&rc, pLoop);
7898  shellFinalize(&rc, pPages);
7899  shellFinalize(&rc, pCells);
7900  recoverFreeTable(pOrphan);
7901
7902  /* The rest of the schema */
7903  if( rc==SQLITE_OK ){
7904    sqlite3_stmt *pStmt = 0;
7905    shellPrepare(pState->db, &rc,
7906        "SELECT sql, name FROM recovery.schema "
7907        "WHERE sql NOT LIKE 'create table%'", &pStmt
7908    );
7909    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7910      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7911      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7912        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7913        char *zPrint = shellMPrintf(&rc,
7914          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7915          zName, zName, zSql
7916        );
7917        raw_printf(pState->out, "%s;\n", zPrint);
7918        sqlite3_free(zPrint);
7919      }else{
7920        raw_printf(pState->out, "%s;\n", zSql);
7921      }
7922    }
7923    shellFinalize(&rc, pStmt);
7924  }
7925
7926  if( rc==SQLITE_OK ){
7927    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7928    raw_printf(pState->out, "COMMIT;\n");
7929  }
7930  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7931  return rc;
7932}
7933#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7934
7935
7936/*
7937 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7938 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7939 *   close db and set it to 0, and return the columns spec, to later
7940 *   be sqlite3_free()'ed by the caller.
7941 * The return is 0 when either:
7942 *   (a) The db was not initialized and zCol==0 (There are no columns.)
7943 *   (b) zCol!=0  (Column was added, db initialized as needed.)
7944 * The 3rd argument, pRenamed, references an out parameter. If the
7945 * pointer is non-zero, its referent will be set to a summary of renames
7946 * done if renaming was necessary, or set to 0 if none was done. The out
7947 * string (if any) must be sqlite3_free()'ed by the caller.
7948 */
7949#ifdef SHELL_DEBUG
7950#define rc_err_oom_die(rc) \
7951  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7952  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7953    fprintf(stderr,"E:%d\n",rc), assert(0)
7954#else
7955static void rc_err_oom_die(int rc){
7956  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7957  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7958}
7959#endif
7960
7961#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7962static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7963#else  /* Otherwise, memory is faster/better for the transient DB. */
7964static const char *zCOL_DB = ":memory:";
7965#endif
7966
7967/* Define character (as C string) to separate generated column ordinal
7968 * from protected part of incoming column names. This defaults to "_"
7969 * so that incoming column identifiers that did not need not be quoted
7970 * remain usable without being quoted. It must be one character.
7971 */
7972#ifndef SHELL_AUTOCOLUMN_SEP
7973# define AUTOCOLUMN_SEP "_"
7974#else
7975# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7976#endif
7977
7978static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
7979  /* Queries and D{D,M}L used here */
7980  static const char * const zTabMake = "\
7981CREATE TABLE ColNames(\
7982 cpos INTEGER PRIMARY KEY,\
7983 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7984CREATE VIEW RepeatedNames AS \
7985SELECT DISTINCT t.name FROM ColNames t \
7986WHERE t.name COLLATE NOCASE IN (\
7987 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7988);\
7989";
7990  static const char * const zTabFill = "\
7991INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7992 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
7993";
7994  static const char * const zHasDupes = "\
7995SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
7996 <count(name) FROM ColNames\
7997";
7998#ifdef SHELL_COLUMN_RENAME_CLEAN
7999  static const char * const zDedoctor = "\
8000UPDATE ColNames SET chop=iif(\
8001  (substring(name,nlen,1) BETWEEN '0' AND '9')\
8002  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
8003 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
8004 0\
8005)\
8006";
8007#endif
8008  static const char * const zSetReps = "\
8009UPDATE ColNames AS t SET reps=\
8010(SELECT count(*) FROM ColNames d \
8011 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
8012 COLLATE NOCASE\
8013)\
8014";
8015#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8016  static const char * const zColDigits = "\
8017SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8018";
8019#else
8020  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8021  static const char * const zColDigits = "\
8022SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8023 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8024 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8025";
8026#endif
8027  static const char * const zRenameRank =
8028#ifdef SHELL_COLUMN_RENAME_CLEAN
8029    "UPDATE ColNames AS t SET suff="
8030    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
8031#else /* ...RENAME_MINIMAL_ONE_PASS */
8032"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8033"  SELECT 0 AS nlz"
8034"  UNION"
8035"  SELECT nlz+1 AS nlz FROM Lzn"
8036"  WHERE EXISTS("
8037"   SELECT 1"
8038"   FROM ColNames t, ColNames o"
8039"   WHERE"
8040"    iif(t.name IN (SELECT * FROM RepeatedNames),"
8041"     printf('%s"AUTOCOLUMN_SEP"%s',"
8042"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8043"     t.name"
8044"    )"
8045"    ="
8046"    iif(o.name IN (SELECT * FROM RepeatedNames),"
8047"     printf('%s"AUTOCOLUMN_SEP"%s',"
8048"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8049"     o.name"
8050"    )"
8051"    COLLATE NOCASE"
8052"    AND o.cpos<>t.cpos"
8053"   GROUP BY t.cpos"
8054"  )"
8055") UPDATE Colnames AS t SET"
8056" chop = 0," /* No chopping, never touch incoming names. */
8057" suff = iif(name IN (SELECT * FROM RepeatedNames),"
8058"  printf('"AUTOCOLUMN_SEP"%s', substring("
8059"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8060"  ''"
8061" )"
8062#endif
8063    ;
8064  static const char * const zCollectVar = "\
8065SELECT\
8066 '('||x'0a'\
8067 || group_concat(\
8068  cname||' TEXT',\
8069  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8070 ||')' AS ColsSpec \
8071FROM (\
8072 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8073 FROM ColNames ORDER BY cpos\
8074)";
8075  static const char * const zRenamesDone =
8076    "SELECT group_concat("
8077    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8078    " ','||x'0a')"
8079    "FROM ColNames WHERE suff<>'' OR chop!=0"
8080    ;
8081  int rc;
8082  sqlite3_stmt *pStmt = 0;
8083  assert(pDb!=0);
8084  if( zColNew ){
8085    /* Add initial or additional column. Init db if necessary. */
8086    if( *pDb==0 ){
8087      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
8088#ifdef SHELL_COLFIX_DB
8089      if(*zCOL_DB!=':')
8090        sqlite3_exec(*pDb,"drop table if exists ColNames;"
8091                     "drop view if exists RepeatedNames;",0,0,0);
8092#endif
8093      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
8094      rc_err_oom_die(rc);
8095    }
8096    assert(*pDb!=0);
8097    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
8098    rc_err_oom_die(rc);
8099    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
8100    rc_err_oom_die(rc);
8101    rc = sqlite3_step(pStmt);
8102    rc_err_oom_die(rc);
8103    sqlite3_finalize(pStmt);
8104    return 0;
8105  }else if( *pDb==0 ){
8106    return 0;
8107  }else{
8108    /* Formulate the columns spec, close the DB, zero *pDb. */
8109    char *zColsSpec = 0;
8110    int hasDupes = db_int(*pDb, zHasDupes);
8111    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
8112    if( hasDupes ){
8113#ifdef SHELL_COLUMN_RENAME_CLEAN
8114      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
8115      rc_err_oom_die(rc);
8116#endif
8117      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
8118      rc_err_oom_die(rc);
8119      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
8120      rc_err_oom_die(rc);
8121      sqlite3_bind_int(pStmt, 1, nDigits);
8122      rc = sqlite3_step(pStmt);
8123      sqlite3_finalize(pStmt);
8124      assert(rc==SQLITE_DONE);
8125    }
8126    assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
8127    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
8128    rc_err_oom_die(rc);
8129    rc = sqlite3_step(pStmt);
8130    if( rc==SQLITE_ROW ){
8131      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8132    }else{
8133      zColsSpec = 0;
8134    }
8135    if( pzRenamed!=0 ){
8136      if( !hasDupes ) *pzRenamed = 0;
8137      else{
8138        sqlite3_finalize(pStmt);
8139        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
8140            && SQLITE_ROW==sqlite3_step(pStmt) ){
8141          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8142        }else
8143          *pzRenamed = 0;
8144      }
8145    }
8146    sqlite3_finalize(pStmt);
8147    sqlite3_close(*pDb);
8148    *pDb = 0;
8149    return zColsSpec;
8150  }
8151}
8152
8153/*
8154** If an input line begins with "." then invoke this routine to
8155** process that line.
8156**
8157** Return 1 on error, 2 to exit, and 0 otherwise.
8158*/
8159static int do_meta_command(char *zLine, ShellState *p){
8160  int h = 1;
8161  int nArg = 0;
8162  int n, c;
8163  int rc = 0;
8164  char *azArg[52];
8165
8166#ifndef SQLITE_OMIT_VIRTUALTABLE
8167  if( p->expert.pExpert ){
8168    expertFinish(p, 1, 0);
8169  }
8170#endif
8171
8172  /* Parse the input line into tokens.
8173  */
8174  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
8175    while( IsSpace(zLine[h]) ){ h++; }
8176    if( zLine[h]==0 ) break;
8177    if( zLine[h]=='\'' || zLine[h]=='"' ){
8178      int delim = zLine[h++];
8179      azArg[nArg++] = &zLine[h];
8180      while( zLine[h] && zLine[h]!=delim ){
8181        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
8182        h++;
8183      }
8184      if( zLine[h]==delim ){
8185        zLine[h++] = 0;
8186      }
8187      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
8188    }else{
8189      azArg[nArg++] = &zLine[h];
8190      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
8191      if( zLine[h] ) zLine[h++] = 0;
8192      resolve_backslashes(azArg[nArg-1]);
8193    }
8194  }
8195  azArg[nArg] = 0;
8196
8197  /* Process the input line.
8198  */
8199  if( nArg==0 ) return 0; /* no tokens, no error */
8200  n = strlen30(azArg[0]);
8201  c = azArg[0][0];
8202  clearTempFile(p);
8203
8204#ifndef SQLITE_OMIT_AUTHORIZATION
8205  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
8206    if( nArg!=2 ){
8207      raw_printf(stderr, "Usage: .auth ON|OFF\n");
8208      rc = 1;
8209      goto meta_command_exit;
8210    }
8211    open_db(p, 0);
8212    if( booleanValue(azArg[1]) ){
8213      sqlite3_set_authorizer(p->db, shellAuth, p);
8214    }else if( p->bSafeModePersist ){
8215      sqlite3_set_authorizer(p->db, safeModeAuth, p);
8216    }else{
8217      sqlite3_set_authorizer(p->db, 0, 0);
8218    }
8219  }else
8220#endif
8221
8222#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8223  && !defined(SQLITE_SHELL_FIDDLE)
8224  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
8225    open_db(p, 0);
8226    failIfSafeMode(p, "cannot run .archive in safe mode");
8227    rc = arDotCommand(p, 0, azArg, nArg);
8228  }else
8229#endif
8230
8231#ifndef SQLITE_SHELL_FIDDLE
8232  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
8233   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
8234  ){
8235    const char *zDestFile = 0;
8236    const char *zDb = 0;
8237    sqlite3 *pDest;
8238    sqlite3_backup *pBackup;
8239    int j;
8240    int bAsync = 0;
8241    const char *zVfs = 0;
8242    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8243    for(j=1; j<nArg; j++){
8244      const char *z = azArg[j];
8245      if( z[0]=='-' ){
8246        if( z[1]=='-' ) z++;
8247        if( strcmp(z, "-append")==0 ){
8248          zVfs = "apndvfs";
8249        }else
8250        if( strcmp(z, "-async")==0 ){
8251          bAsync = 1;
8252        }else
8253        {
8254          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
8255          return 1;
8256        }
8257      }else if( zDestFile==0 ){
8258        zDestFile = azArg[j];
8259      }else if( zDb==0 ){
8260        zDb = zDestFile;
8261        zDestFile = azArg[j];
8262      }else{
8263        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8264        return 1;
8265      }
8266    }
8267    if( zDestFile==0 ){
8268      raw_printf(stderr, "missing FILENAME argument on .backup\n");
8269      return 1;
8270    }
8271    if( zDb==0 ) zDb = "main";
8272    rc = sqlite3_open_v2(zDestFile, &pDest,
8273                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8274    if( rc!=SQLITE_OK ){
8275      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
8276      close_db(pDest);
8277      return 1;
8278    }
8279    if( bAsync ){
8280      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8281                   0, 0, 0);
8282    }
8283    open_db(p, 0);
8284    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8285    if( pBackup==0 ){
8286      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8287      close_db(pDest);
8288      return 1;
8289    }
8290    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8291    sqlite3_backup_finish(pBackup);
8292    if( rc==SQLITE_DONE ){
8293      rc = 0;
8294    }else{
8295      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8296      rc = 1;
8297    }
8298    close_db(pDest);
8299  }else
8300#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8301
8302  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
8303    if( nArg==2 ){
8304      bail_on_error = booleanValue(azArg[1]);
8305    }else{
8306      raw_printf(stderr, "Usage: .bail on|off\n");
8307      rc = 1;
8308    }
8309  }else
8310
8311  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
8312    if( nArg==2 ){
8313      if( booleanValue(azArg[1]) ){
8314        setBinaryMode(p->out, 1);
8315      }else{
8316        setTextMode(p->out, 1);
8317      }
8318    }else{
8319      raw_printf(stderr, "Usage: .binary on|off\n");
8320      rc = 1;
8321    }
8322  }else
8323
8324  /* The undocumented ".breakpoint" command causes a call to the no-op
8325  ** routine named test_breakpoint().
8326  */
8327  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
8328    test_breakpoint();
8329  }else
8330
8331#ifndef SQLITE_SHELL_FIDDLE
8332  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
8333    failIfSafeMode(p, "cannot run .cd in safe mode");
8334    if( nArg==2 ){
8335#if defined(_WIN32) || defined(WIN32)
8336      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8337      rc = !SetCurrentDirectoryW(z);
8338      sqlite3_free(z);
8339#else
8340      rc = chdir(azArg[1]);
8341#endif
8342      if( rc ){
8343        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
8344        rc = 1;
8345      }
8346    }else{
8347      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
8348      rc = 1;
8349    }
8350  }else
8351#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8352
8353  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
8354    if( nArg==2 ){
8355      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8356    }else{
8357      raw_printf(stderr, "Usage: .changes on|off\n");
8358      rc = 1;
8359    }
8360  }else
8361
8362#ifndef SQLITE_SHELL_FIDDLE
8363  /* Cancel output redirection, if it is currently set (by .testcase)
8364  ** Then read the content of the testcase-out.txt file and compare against
8365  ** azArg[1].  If there are differences, report an error and exit.
8366  */
8367  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
8368    char *zRes = 0;
8369    output_reset(p);
8370    if( nArg!=2 ){
8371      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
8372      rc = 2;
8373    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8374      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
8375      rc = 2;
8376    }else if( testcase_glob(azArg[1],zRes)==0 ){
8377      utf8_printf(stderr,
8378                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
8379                 p->zTestcase, azArg[1], zRes);
8380      rc = 1;
8381    }else{
8382      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
8383      p->nCheck++;
8384    }
8385    sqlite3_free(zRes);
8386  }else
8387#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8388
8389#ifndef SQLITE_SHELL_FIDDLE
8390  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
8391    failIfSafeMode(p, "cannot run .clone in safe mode");
8392    if( nArg==2 ){
8393      tryToClone(p, azArg[1]);
8394    }else{
8395      raw_printf(stderr, "Usage: .clone FILENAME\n");
8396      rc = 1;
8397    }
8398  }else
8399#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8400
8401  if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
8402    if( nArg==1 ){
8403      /* List available connections */
8404      int i;
8405      for(i=0; i<ArraySize(p->aAuxDb); i++){
8406        const char *zFile = p->aAuxDb[i].zDbFilename;
8407        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8408          zFile = "(not open)";
8409        }else if( zFile==0 ){
8410          zFile = "(memory)";
8411        }else if( zFile[0]==0 ){
8412          zFile = "(temporary-file)";
8413        }
8414        if( p->pAuxDb == &p->aAuxDb[i] ){
8415          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
8416        }else if( p->aAuxDb[i].db!=0 ){
8417          utf8_printf(stdout, "       %d: %s\n", i, zFile);
8418        }
8419      }
8420    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8421      int i = azArg[1][0] - '0';
8422      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8423        p->pAuxDb->db = p->db;
8424        p->pAuxDb = &p->aAuxDb[i];
8425        globalDb = p->db = p->pAuxDb->db;
8426        p->pAuxDb->db = 0;
8427      }
8428    }else if( nArg==3 && strcmp(azArg[1], "close")==0
8429           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8430      int i = azArg[2][0] - '0';
8431      if( i<0 || i>=ArraySize(p->aAuxDb) ){
8432        /* No-op */
8433      }else if( p->pAuxDb == &p->aAuxDb[i] ){
8434        raw_printf(stderr, "cannot close the active database connection\n");
8435        rc = 1;
8436      }else if( p->aAuxDb[i].db ){
8437        session_close_all(p, i);
8438        close_db(p->aAuxDb[i].db);
8439        p->aAuxDb[i].db = 0;
8440      }
8441    }else{
8442      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
8443      rc = 1;
8444    }
8445  }else
8446
8447  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
8448    char **azName = 0;
8449    int nName = 0;
8450    sqlite3_stmt *pStmt;
8451    int i;
8452    open_db(p, 0);
8453    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8454    if( rc ){
8455      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8456      rc = 1;
8457    }else{
8458      while( sqlite3_step(pStmt)==SQLITE_ROW ){
8459        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8460        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8461        if( zSchema==0 || zFile==0 ) continue;
8462        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8463        shell_check_oom(azName);
8464        azName[nName*2] = strdup(zSchema);
8465        azName[nName*2+1] = strdup(zFile);
8466        nName++;
8467      }
8468    }
8469    sqlite3_finalize(pStmt);
8470    for(i=0; i<nName; i++){
8471      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8472      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8473      const char *z = azName[i*2+1];
8474      utf8_printf(p->out, "%s: %s %s%s\n",
8475         azName[i*2],
8476         z && z[0] ? z : "\"\"",
8477         bRdonly ? "r/o" : "r/w",
8478         eTxn==SQLITE_TXN_NONE ? "" :
8479            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8480      free(azName[i*2]);
8481      free(azName[i*2+1]);
8482    }
8483    sqlite3_free(azName);
8484  }else
8485
8486  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
8487    static const struct DbConfigChoices {
8488      const char *zName;
8489      int op;
8490    } aDbConfig[] = {
8491        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
8492        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
8493        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
8494        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
8495        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
8496        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
8497        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
8498        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8499        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
8500        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
8501        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8502        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
8503        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
8504        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
8505        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
8506        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
8507    };
8508    int ii, v;
8509    open_db(p, 0);
8510    for(ii=0; ii<ArraySize(aDbConfig); ii++){
8511      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8512      if( nArg>=3 ){
8513        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8514      }
8515      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8516      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
8517      if( nArg>1 ) break;
8518    }
8519    if( nArg>1 && ii==ArraySize(aDbConfig) ){
8520      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
8521      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
8522    }
8523  }else
8524
8525#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8526  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
8527    rc = shell_dbinfo_command(p, nArg, azArg);
8528  }else
8529
8530  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
8531    open_db(p, 0);
8532    rc = recoverDatabaseCmd(p, nArg, azArg);
8533  }else
8534#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
8535
8536  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
8537    char *zLike = 0;
8538    char *zSql;
8539    int i;
8540    int savedShowHeader = p->showHeader;
8541    int savedShellFlags = p->shellFlgs;
8542    ShellClearFlag(p,
8543       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8544       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8545    for(i=1; i<nArg; i++){
8546      if( azArg[i][0]=='-' ){
8547        const char *z = azArg[i]+1;
8548        if( z[0]=='-' ) z++;
8549        if( strcmp(z,"preserve-rowids")==0 ){
8550#ifdef SQLITE_OMIT_VIRTUALTABLE
8551          raw_printf(stderr, "The --preserve-rowids option is not compatible"
8552                             " with SQLITE_OMIT_VIRTUALTABLE\n");
8553          rc = 1;
8554          sqlite3_free(zLike);
8555          goto meta_command_exit;
8556#else
8557          ShellSetFlag(p, SHFLG_PreserveRowid);
8558#endif
8559        }else
8560        if( strcmp(z,"newlines")==0 ){
8561          ShellSetFlag(p, SHFLG_Newlines);
8562        }else
8563        if( strcmp(z,"data-only")==0 ){
8564          ShellSetFlag(p, SHFLG_DumpDataOnly);
8565        }else
8566        if( strcmp(z,"nosys")==0 ){
8567          ShellSetFlag(p, SHFLG_DumpNoSys);
8568        }else
8569        {
8570          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8571          rc = 1;
8572          sqlite3_free(zLike);
8573          goto meta_command_exit;
8574        }
8575      }else{
8576        /* azArg[i] contains a LIKE pattern. This ".dump" request should
8577        ** only dump data for tables for which either the table name matches
8578        ** the LIKE pattern, or the table appears to be a shadow table of
8579        ** a virtual table for which the name matches the LIKE pattern.
8580        */
8581        char *zExpr = sqlite3_mprintf(
8582            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8583            "  SELECT 1 FROM sqlite_schema WHERE "
8584            "    name LIKE %Q ESCAPE '\\' AND"
8585            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8586            "    substr(o.name, 1, length(name)+1) == (name||'_')"
8587            ")", azArg[i], azArg[i]
8588        );
8589
8590        if( zLike ){
8591          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8592        }else{
8593          zLike = zExpr;
8594        }
8595      }
8596    }
8597
8598    open_db(p, 0);
8599
8600    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8601      /* When playing back a "dump", the content might appear in an order
8602      ** which causes immediate foreign key constraints to be violated.
8603      ** So disable foreign-key constraint enforcement to prevent problems. */
8604      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8605      raw_printf(p->out, "BEGIN TRANSACTION;\n");
8606    }
8607    p->writableSchema = 0;
8608    p->showHeader = 0;
8609    /* Set writable_schema=ON since doing so forces SQLite to initialize
8610    ** as much of the schema as it can even if the sqlite_schema table is
8611    ** corrupt. */
8612    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8613    p->nErr = 0;
8614    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8615    zSql = sqlite3_mprintf(
8616      "SELECT name, type, sql FROM sqlite_schema AS o "
8617      "WHERE (%s) AND type=='table'"
8618      "  AND sql NOT NULL"
8619      " ORDER BY tbl_name='sqlite_sequence', rowid",
8620      zLike
8621    );
8622    run_schema_dump_query(p,zSql);
8623    sqlite3_free(zSql);
8624    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8625      zSql = sqlite3_mprintf(
8626        "SELECT sql FROM sqlite_schema AS o "
8627        "WHERE (%s) AND sql NOT NULL"
8628        "  AND type IN ('index','trigger','view')",
8629        zLike
8630      );
8631      run_table_dump_query(p, zSql);
8632      sqlite3_free(zSql);
8633    }
8634    sqlite3_free(zLike);
8635    if( p->writableSchema ){
8636      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8637      p->writableSchema = 0;
8638    }
8639    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8640    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8641    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8642      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8643    }
8644    p->showHeader = savedShowHeader;
8645    p->shellFlgs = savedShellFlags;
8646  }else
8647
8648  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
8649    if( nArg==2 ){
8650      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8651    }else{
8652      raw_printf(stderr, "Usage: .echo on|off\n");
8653      rc = 1;
8654    }
8655  }else
8656
8657  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
8658    if( nArg==2 ){
8659      p->autoEQPtest = 0;
8660      if( p->autoEQPtrace ){
8661        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8662        p->autoEQPtrace = 0;
8663      }
8664      if( strcmp(azArg[1],"full")==0 ){
8665        p->autoEQP = AUTOEQP_full;
8666      }else if( strcmp(azArg[1],"trigger")==0 ){
8667        p->autoEQP = AUTOEQP_trigger;
8668#ifdef SQLITE_DEBUG
8669      }else if( strcmp(azArg[1],"test")==0 ){
8670        p->autoEQP = AUTOEQP_on;
8671        p->autoEQPtest = 1;
8672      }else if( strcmp(azArg[1],"trace")==0 ){
8673        p->autoEQP = AUTOEQP_full;
8674        p->autoEQPtrace = 1;
8675        open_db(p, 0);
8676        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8677        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8678#endif
8679      }else{
8680        p->autoEQP = (u8)booleanValue(azArg[1]);
8681      }
8682    }else{
8683      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8684      rc = 1;
8685    }
8686  }else
8687
8688#ifndef SQLITE_SHELL_FIDDLE
8689  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
8690    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8691    rc = 2;
8692  }else
8693#endif
8694
8695  /* The ".explain" command is automatic now.  It is largely pointless.  It
8696  ** retained purely for backwards compatibility */
8697  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
8698    int val = 1;
8699    if( nArg>=2 ){
8700      if( strcmp(azArg[1],"auto")==0 ){
8701        val = 99;
8702      }else{
8703        val =  booleanValue(azArg[1]);
8704      }
8705    }
8706    if( val==1 && p->mode!=MODE_Explain ){
8707      p->normalMode = p->mode;
8708      p->mode = MODE_Explain;
8709      p->autoExplain = 0;
8710    }else if( val==0 ){
8711      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8712      p->autoExplain = 0;
8713    }else if( val==99 ){
8714      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8715      p->autoExplain = 1;
8716    }
8717  }else
8718
8719#ifndef SQLITE_OMIT_VIRTUALTABLE
8720  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
8721    if( p->bSafeMode ){
8722      raw_printf(stderr,
8723        "Cannot run experimental commands such as \"%s\" in safe mode\n",
8724        azArg[0]);
8725      rc = 1;
8726    }else{
8727      open_db(p, 0);
8728      expertDotCommand(p, azArg, nArg);
8729    }
8730  }else
8731#endif
8732
8733  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
8734    static const struct {
8735       const char *zCtrlName;   /* Name of a test-control option */
8736       int ctrlCode;            /* Integer code for that option */
8737       const char *zUsage;      /* Usage notes */
8738    } aCtrl[] = {
8739      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8740      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8741      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8742      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8743      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8744   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8745      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8746      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8747      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8748      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8749   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8750    };
8751    int filectrl = -1;
8752    int iCtrl = -1;
8753    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8754    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8755    int n2, i;
8756    const char *zCmd = 0;
8757    const char *zSchema = 0;
8758
8759    open_db(p, 0);
8760    zCmd = nArg>=2 ? azArg[1] : "help";
8761
8762    if( zCmd[0]=='-'
8763     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
8764     && nArg>=4
8765    ){
8766      zSchema = azArg[2];
8767      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8768      nArg -= 2;
8769      zCmd = azArg[1];
8770    }
8771
8772    /* The argument can optionally begin with "-" or "--" */
8773    if( zCmd[0]=='-' && zCmd[1] ){
8774      zCmd++;
8775      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8776    }
8777
8778    /* --help lists all file-controls */
8779    if( strcmp(zCmd,"help")==0 ){
8780      utf8_printf(p->out, "Available file-controls:\n");
8781      for(i=0; i<ArraySize(aCtrl); i++){
8782        utf8_printf(p->out, "  .filectrl %s %s\n",
8783                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8784      }
8785      rc = 1;
8786      goto meta_command_exit;
8787    }
8788
8789    /* convert filectrl text option to value. allow any unique prefix
8790    ** of the option name, or a numerical value. */
8791    n2 = strlen30(zCmd);
8792    for(i=0; i<ArraySize(aCtrl); i++){
8793      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8794        if( filectrl<0 ){
8795          filectrl = aCtrl[i].ctrlCode;
8796          iCtrl = i;
8797        }else{
8798          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8799                              "Use \".filectrl --help\" for help\n", zCmd);
8800          rc = 1;
8801          goto meta_command_exit;
8802        }
8803      }
8804    }
8805    if( filectrl<0 ){
8806      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8807                         "Use \".filectrl --help\" for help\n", zCmd);
8808    }else{
8809      switch(filectrl){
8810        case SQLITE_FCNTL_SIZE_LIMIT: {
8811          if( nArg!=2 && nArg!=3 ) break;
8812          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8813          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8814          isOk = 1;
8815          break;
8816        }
8817        case SQLITE_FCNTL_LOCK_TIMEOUT:
8818        case SQLITE_FCNTL_CHUNK_SIZE: {
8819          int x;
8820          if( nArg!=3 ) break;
8821          x = (int)integerValue(azArg[2]);
8822          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8823          isOk = 2;
8824          break;
8825        }
8826        case SQLITE_FCNTL_PERSIST_WAL:
8827        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8828          int x;
8829          if( nArg!=2 && nArg!=3 ) break;
8830          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8831          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8832          iRes = x;
8833          isOk = 1;
8834          break;
8835        }
8836        case SQLITE_FCNTL_DATA_VERSION:
8837        case SQLITE_FCNTL_HAS_MOVED: {
8838          int x;
8839          if( nArg!=2 ) break;
8840          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8841          iRes = x;
8842          isOk = 1;
8843          break;
8844        }
8845        case SQLITE_FCNTL_TEMPFILENAME: {
8846          char *z = 0;
8847          if( nArg!=2 ) break;
8848          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8849          if( z ){
8850            utf8_printf(p->out, "%s\n", z);
8851            sqlite3_free(z);
8852          }
8853          isOk = 2;
8854          break;
8855        }
8856        case SQLITE_FCNTL_RESERVE_BYTES: {
8857          int x;
8858          if( nArg>=3 ){
8859            x = atoi(azArg[2]);
8860            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8861          }
8862          x = -1;
8863          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8864          utf8_printf(p->out,"%d\n", x);
8865          isOk = 2;
8866          break;
8867        }
8868      }
8869    }
8870    if( isOk==0 && iCtrl>=0 ){
8871      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8872      rc = 1;
8873    }else if( isOk==1 ){
8874      char zBuf[100];
8875      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8876      raw_printf(p->out, "%s\n", zBuf);
8877    }
8878  }else
8879
8880  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8881    ShellState data;
8882    int doStats = 0;
8883    memcpy(&data, p, sizeof(data));
8884    data.showHeader = 0;
8885    data.cMode = data.mode = MODE_Semi;
8886    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8887      data.cMode = data.mode = MODE_Pretty;
8888      nArg = 1;
8889    }
8890    if( nArg!=1 ){
8891      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8892      rc = 1;
8893      goto meta_command_exit;
8894    }
8895    open_db(p, 0);
8896    rc = sqlite3_exec(p->db,
8897       "SELECT sql FROM"
8898       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8899       "     FROM sqlite_schema UNION ALL"
8900       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8901       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8902       "ORDER BY x",
8903       callback, &data, 0
8904    );
8905    if( rc==SQLITE_OK ){
8906      sqlite3_stmt *pStmt;
8907      rc = sqlite3_prepare_v2(p->db,
8908               "SELECT rowid FROM sqlite_schema"
8909               " WHERE name GLOB 'sqlite_stat[134]'",
8910               -1, &pStmt, 0);
8911      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8912      sqlite3_finalize(pStmt);
8913    }
8914    if( doStats==0 ){
8915      raw_printf(p->out, "/* No STAT tables available */\n");
8916    }else{
8917      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8918      data.cMode = data.mode = MODE_Insert;
8919      data.zDestTable = "sqlite_stat1";
8920      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8921      data.zDestTable = "sqlite_stat4";
8922      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8923      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8924    }
8925  }else
8926
8927  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8928    if( nArg==2 ){
8929      p->showHeader = booleanValue(azArg[1]);
8930      p->shellFlgs |= SHFLG_HeaderSet;
8931    }else{
8932      raw_printf(stderr, "Usage: .headers on|off\n");
8933      rc = 1;
8934    }
8935  }else
8936
8937  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8938    if( nArg>=2 ){
8939      n = showHelp(p->out, azArg[1]);
8940      if( n==0 ){
8941        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8942      }
8943    }else{
8944      showHelp(p->out, 0);
8945    }
8946  }else
8947
8948#ifndef SQLITE_SHELL_FIDDLE
8949  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8950    char *zTable = 0;           /* Insert data into this table */
8951    char *zSchema = 0;          /* within this schema (may default to "main") */
8952    char *zFile = 0;            /* Name of file to extra content from */
8953    sqlite3_stmt *pStmt = NULL; /* A statement */
8954    int nCol;                   /* Number of columns in the table */
8955    int nByte;                  /* Number of bytes in an SQL string */
8956    int i, j;                   /* Loop counters */
8957    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8958    int nSep;                   /* Number of bytes in p->colSeparator[] */
8959    char *zSql;                 /* An SQL statement */
8960    char *zFullTabName;         /* Table name with schema if applicable */
8961    ImportCtx sCtx;             /* Reader context */
8962    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8963    int eVerbose = 0;           /* Larger for more console output */
8964    int nSkip = 0;              /* Initial lines to skip */
8965    int useOutputMode = 1;      /* Use output mode to determine separators */
8966    char *zCreate = 0;          /* CREATE TABLE statement text */
8967
8968    failIfSafeMode(p, "cannot run .import in safe mode");
8969    memset(&sCtx, 0, sizeof(sCtx));
8970    if( p->mode==MODE_Ascii ){
8971      xRead = ascii_read_one_field;
8972    }else{
8973      xRead = csv_read_one_field;
8974    }
8975    rc = 1;
8976    for(i=1; i<nArg; i++){
8977      char *z = azArg[i];
8978      if( z[0]=='-' && z[1]=='-' ) z++;
8979      if( z[0]!='-' ){
8980        if( zFile==0 ){
8981          zFile = z;
8982        }else if( zTable==0 ){
8983          zTable = z;
8984        }else{
8985          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8986          showHelp(p->out, "import");
8987          goto meta_command_exit;
8988        }
8989      }else if( strcmp(z,"-v")==0 ){
8990        eVerbose++;
8991      }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){
8992        zSchema = azArg[++i];
8993      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8994        nSkip = integerValue(azArg[++i]);
8995      }else if( strcmp(z,"-ascii")==0 ){
8996        sCtx.cColSep = SEP_Unit[0];
8997        sCtx.cRowSep = SEP_Record[0];
8998        xRead = ascii_read_one_field;
8999        useOutputMode = 0;
9000      }else if( strcmp(z,"-csv")==0 ){
9001        sCtx.cColSep = ',';
9002        sCtx.cRowSep = '\n';
9003        xRead = csv_read_one_field;
9004        useOutputMode = 0;
9005      }else{
9006        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
9007        showHelp(p->out, "import");
9008        goto meta_command_exit;
9009      }
9010    }
9011    if( zTable==0 ){
9012      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
9013                  zFile==0 ? "FILE" : "TABLE");
9014      showHelp(p->out, "import");
9015      goto meta_command_exit;
9016    }
9017    seenInterrupt = 0;
9018    open_db(p, 0);
9019    if( useOutputMode ){
9020      /* If neither the --csv or --ascii options are specified, then set
9021      ** the column and row separator characters from the output mode. */
9022      nSep = strlen30(p->colSeparator);
9023      if( nSep==0 ){
9024        raw_printf(stderr,
9025                   "Error: non-null column separator required for import\n");
9026        goto meta_command_exit;
9027      }
9028      if( nSep>1 ){
9029        raw_printf(stderr,
9030              "Error: multi-character column separators not allowed"
9031              " for import\n");
9032        goto meta_command_exit;
9033      }
9034      nSep = strlen30(p->rowSeparator);
9035      if( nSep==0 ){
9036        raw_printf(stderr,
9037            "Error: non-null row separator required for import\n");
9038        goto meta_command_exit;
9039      }
9040      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
9041        /* When importing CSV (only), if the row separator is set to the
9042        ** default output row separator, change it to the default input
9043        ** row separator.  This avoids having to maintain different input
9044        ** and output row separators. */
9045        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9046        nSep = strlen30(p->rowSeparator);
9047      }
9048      if( nSep>1 ){
9049        raw_printf(stderr, "Error: multi-character row separators not allowed"
9050                           " for import\n");
9051        goto meta_command_exit;
9052      }
9053      sCtx.cColSep = p->colSeparator[0];
9054      sCtx.cRowSep = p->rowSeparator[0];
9055    }
9056    sCtx.zFile = zFile;
9057    sCtx.nLine = 1;
9058    if( sCtx.zFile[0]=='|' ){
9059#ifdef SQLITE_OMIT_POPEN
9060      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9061      goto meta_command_exit;
9062#else
9063      sCtx.in = popen(sCtx.zFile+1, "r");
9064      sCtx.zFile = "<pipe>";
9065      sCtx.xCloser = pclose;
9066#endif
9067    }else{
9068      sCtx.in = fopen(sCtx.zFile, "rb");
9069      sCtx.xCloser = fclose;
9070    }
9071    if( sCtx.in==0 ){
9072      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
9073      goto meta_command_exit;
9074    }
9075    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
9076      char zSep[2];
9077      zSep[1] = 0;
9078      zSep[0] = sCtx.cColSep;
9079      utf8_printf(p->out, "Column separator ");
9080      output_c_string(p->out, zSep);
9081      utf8_printf(p->out, ", row separator ");
9082      zSep[0] = sCtx.cRowSep;
9083      output_c_string(p->out, zSep);
9084      utf8_printf(p->out, "\n");
9085    }
9086    sCtx.z = sqlite3_malloc64(120);
9087    if( sCtx.z==0 ){
9088      import_cleanup(&sCtx);
9089      shell_out_of_memory();
9090    }
9091    /* Below, resources must be freed before exit. */
9092    while( (nSkip--)>0 ){
9093      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
9094    }
9095    if( zSchema!=0 ){
9096      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
9097    }else{
9098      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
9099    }
9100    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
9101    if( zSql==0 || zFullTabName==0 ){
9102      import_cleanup(&sCtx);
9103      shell_out_of_memory();
9104    }
9105    nByte = strlen30(zSql);
9106    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9107    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
9108    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
9109      sqlite3 *dbCols = 0;
9110      char *zRenames = 0;
9111      char *zColDefs;
9112      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
9113      while( xRead(&sCtx) ){
9114        zAutoColumn(sCtx.z, &dbCols, 0);
9115        if( sCtx.cTerm!=sCtx.cColSep ) break;
9116      }
9117      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
9118      if( zRenames!=0 ){
9119        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
9120                    "Columns renamed during .import %s due to duplicates:\n"
9121                    "%s\n", sCtx.zFile, zRenames);
9122        sqlite3_free(zRenames);
9123      }
9124      assert(dbCols==0);
9125      if( zColDefs==0 ){
9126        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
9127      import_fail:
9128        sqlite3_free(zCreate);
9129        sqlite3_free(zSql);
9130        sqlite3_free(zFullTabName);
9131        import_cleanup(&sCtx);
9132        rc = 1;
9133        goto meta_command_exit;
9134      }
9135      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
9136      if( eVerbose>=1 ){
9137        utf8_printf(p->out, "%s\n", zCreate);
9138      }
9139      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
9140      if( rc ){
9141        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
9142        goto import_fail;
9143      }
9144      sqlite3_free(zCreate);
9145      zCreate = 0;
9146      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9147    }
9148    if( rc ){
9149      if (pStmt) sqlite3_finalize(pStmt);
9150      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
9151      goto import_fail;
9152    }
9153    sqlite3_free(zSql);
9154    nCol = sqlite3_column_count(pStmt);
9155    sqlite3_finalize(pStmt);
9156    pStmt = 0;
9157    if( nCol==0 ) return 0; /* no columns, no error */
9158    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
9159    if( zSql==0 ){
9160      import_cleanup(&sCtx);
9161      shell_out_of_memory();
9162    }
9163    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
9164    j = strlen30(zSql);
9165    for(i=1; i<nCol; i++){
9166      zSql[j++] = ',';
9167      zSql[j++] = '?';
9168    }
9169    zSql[j++] = ')';
9170    zSql[j] = 0;
9171    if( eVerbose>=2 ){
9172      utf8_printf(p->out, "Insert using: %s\n", zSql);
9173    }
9174    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9175    if( rc ){
9176      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9177      if (pStmt) sqlite3_finalize(pStmt);
9178      goto import_fail;
9179    }
9180    sqlite3_free(zSql);
9181    sqlite3_free(zFullTabName);
9182    needCommit = sqlite3_get_autocommit(p->db);
9183    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
9184    do{
9185      int startLine = sCtx.nLine;
9186      for(i=0; i<nCol; i++){
9187        char *z = xRead(&sCtx);
9188        /*
9189        ** Did we reach end-of-file before finding any columns?
9190        ** If so, stop instead of NULL filling the remaining columns.
9191        */
9192        if( z==0 && i==0 ) break;
9193        /*
9194        ** Did we reach end-of-file OR end-of-line before finding any
9195        ** columns in ASCII mode?  If so, stop instead of NULL filling
9196        ** the remaining columns.
9197        */
9198        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
9199        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
9200        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
9201          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9202                          "filling the rest with NULL\n",
9203                          sCtx.zFile, startLine, nCol, i+1);
9204          i += 2;
9205          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9206        }
9207      }
9208      if( sCtx.cTerm==sCtx.cColSep ){
9209        do{
9210          xRead(&sCtx);
9211          i++;
9212        }while( sCtx.cTerm==sCtx.cColSep );
9213        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9214                        "extras ignored\n",
9215                        sCtx.zFile, startLine, nCol, i);
9216      }
9217      if( i>=nCol ){
9218        sqlite3_step(pStmt);
9219        rc = sqlite3_reset(pStmt);
9220        if( rc!=SQLITE_OK ){
9221          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
9222                      startLine, sqlite3_errmsg(p->db));
9223          sCtx.nErr++;
9224        }else{
9225          sCtx.nRow++;
9226        }
9227      }
9228    }while( sCtx.cTerm!=EOF );
9229
9230    import_cleanup(&sCtx);
9231    sqlite3_finalize(pStmt);
9232    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9233    if( eVerbose>0 ){
9234      utf8_printf(p->out,
9235          "Added %d rows with %d errors using %d lines of input\n",
9236          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9237    }
9238  }else
9239#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9240
9241#ifndef SQLITE_UNTESTABLE
9242  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
9243    char *zSql;
9244    char *zCollist = 0;
9245    sqlite3_stmt *pStmt;
9246    int tnum = 0;
9247    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
9248    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9249    int i;
9250    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9251      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
9252                          "       .imposter off\n");
9253      /* Also allowed, but not documented:
9254      **
9255      **    .imposter TABLE IMPOSTER
9256      **
9257      ** where TABLE is a WITHOUT ROWID table.  In that case, the
9258      ** imposter is another WITHOUT ROWID table with the columns in
9259      ** storage order. */
9260      rc = 1;
9261      goto meta_command_exit;
9262    }
9263    open_db(p, 0);
9264    if( nArg==2 ){
9265      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9266      goto meta_command_exit;
9267    }
9268    zSql = sqlite3_mprintf(
9269      "SELECT rootpage, 0 FROM sqlite_schema"
9270      " WHERE name='%q' AND type='index'"
9271      "UNION ALL "
9272      "SELECT rootpage, 1 FROM sqlite_schema"
9273      " WHERE name='%q' AND type='table'"
9274      "   AND sql LIKE '%%without%%rowid%%'",
9275      azArg[1], azArg[1]
9276    );
9277    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9278    sqlite3_free(zSql);
9279    if( sqlite3_step(pStmt)==SQLITE_ROW ){
9280      tnum = sqlite3_column_int(pStmt, 0);
9281      isWO = sqlite3_column_int(pStmt, 1);
9282    }
9283    sqlite3_finalize(pStmt);
9284    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9285    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9286    sqlite3_free(zSql);
9287    i = 0;
9288    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9289      char zLabel[20];
9290      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9291      i++;
9292      if( zCol==0 ){
9293        if( sqlite3_column_int(pStmt,1)==-1 ){
9294          zCol = "_ROWID_";
9295        }else{
9296          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9297          zCol = zLabel;
9298        }
9299      }
9300      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9301        lenPK = (int)strlen(zCollist);
9302      }
9303      if( zCollist==0 ){
9304        zCollist = sqlite3_mprintf("\"%w\"", zCol);
9305      }else{
9306        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9307      }
9308    }
9309    sqlite3_finalize(pStmt);
9310    if( i==0 || tnum==0 ){
9311      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
9312      rc = 1;
9313      sqlite3_free(zCollist);
9314      goto meta_command_exit;
9315    }
9316    if( lenPK==0 ) lenPK = 100000;
9317    zSql = sqlite3_mprintf(
9318          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9319          azArg[2], zCollist, lenPK, zCollist);
9320    sqlite3_free(zCollist);
9321    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9322    if( rc==SQLITE_OK ){
9323      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9324      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9325      if( rc ){
9326        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9327      }else{
9328        utf8_printf(stdout, "%s;\n", zSql);
9329        raw_printf(stdout,
9330          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
9331          azArg[1], isWO ? "table" : "index"
9332        );
9333      }
9334    }else{
9335      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9336      rc = 1;
9337    }
9338    sqlite3_free(zSql);
9339  }else
9340#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9341
9342#ifdef SQLITE_ENABLE_IOTRACE
9343  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
9344    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9345    if( iotrace && iotrace!=stdout ) fclose(iotrace);
9346    iotrace = 0;
9347    if( nArg<2 ){
9348      sqlite3IoTrace = 0;
9349    }else if( strcmp(azArg[1], "-")==0 ){
9350      sqlite3IoTrace = iotracePrintf;
9351      iotrace = stdout;
9352    }else{
9353      iotrace = fopen(azArg[1], "w");
9354      if( iotrace==0 ){
9355        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9356        sqlite3IoTrace = 0;
9357        rc = 1;
9358      }else{
9359        sqlite3IoTrace = iotracePrintf;
9360      }
9361    }
9362  }else
9363#endif
9364
9365  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
9366    static const struct {
9367       const char *zLimitName;   /* Name of a limit */
9368       int limitCode;            /* Integer code for that limit */
9369    } aLimit[] = {
9370      { "length",                SQLITE_LIMIT_LENGTH                    },
9371      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
9372      { "column",                SQLITE_LIMIT_COLUMN                    },
9373      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
9374      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
9375      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
9376      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
9377      { "attached",              SQLITE_LIMIT_ATTACHED                  },
9378      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
9379      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
9380      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
9381      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
9382    };
9383    int i, n2;
9384    open_db(p, 0);
9385    if( nArg==1 ){
9386      for(i=0; i<ArraySize(aLimit); i++){
9387        printf("%20s %d\n", aLimit[i].zLimitName,
9388               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9389      }
9390    }else if( nArg>3 ){
9391      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
9392      rc = 1;
9393      goto meta_command_exit;
9394    }else{
9395      int iLimit = -1;
9396      n2 = strlen30(azArg[1]);
9397      for(i=0; i<ArraySize(aLimit); i++){
9398        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9399          if( iLimit<0 ){
9400            iLimit = i;
9401          }else{
9402            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
9403            rc = 1;
9404            goto meta_command_exit;
9405          }
9406        }
9407      }
9408      if( iLimit<0 ){
9409        utf8_printf(stderr, "unknown limit: \"%s\"\n"
9410                        "enter \".limits\" with no arguments for a list.\n",
9411                         azArg[1]);
9412        rc = 1;
9413        goto meta_command_exit;
9414      }
9415      if( nArg==3 ){
9416        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9417                      (int)integerValue(azArg[2]));
9418      }
9419      printf("%20s %d\n", aLimit[iLimit].zLimitName,
9420             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9421    }
9422  }else
9423
9424  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
9425    open_db(p, 0);
9426    lintDotCommand(p, azArg, nArg);
9427  }else
9428
9429#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9430  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
9431    const char *zFile, *zProc;
9432    char *zErrMsg = 0;
9433    failIfSafeMode(p, "cannot run .load in safe mode");
9434    if( nArg<2 ){
9435      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
9436      rc = 1;
9437      goto meta_command_exit;
9438    }
9439    zFile = azArg[1];
9440    zProc = nArg>=3 ? azArg[2] : 0;
9441    open_db(p, 0);
9442    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9443    if( rc!=SQLITE_OK ){
9444      utf8_printf(stderr, "Error: %s\n", zErrMsg);
9445      sqlite3_free(zErrMsg);
9446      rc = 1;
9447    }
9448  }else
9449#endif
9450
9451#ifndef SQLITE_SHELL_FIDDLE
9452  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
9453    failIfSafeMode(p, "cannot run .log in safe mode");
9454    if( nArg!=2 ){
9455      raw_printf(stderr, "Usage: .log FILENAME\n");
9456      rc = 1;
9457    }else{
9458      const char *zFile = azArg[1];
9459      output_file_close(p->pLog);
9460      p->pLog = output_file_open(zFile, 0);
9461    }
9462  }else
9463#endif
9464
9465  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
9466    const char *zMode = 0;
9467    const char *zTabname = 0;
9468    int i, n2;
9469    ColModeOpts cmOpts = ColModeOpts_default;
9470    for(i=1; i<nArg; i++){
9471      const char *z = azArg[i];
9472      if( optionMatch(z,"wrap") && i+1<nArg ){
9473        cmOpts.iWrap = integerValue(azArg[++i]);
9474      }else if( optionMatch(z,"ww") ){
9475        cmOpts.bWordWrap = 1;
9476      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9477        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9478      }else if( optionMatch(z,"quote") ){
9479        cmOpts.bQuote = 1;
9480      }else if( optionMatch(z,"noquote") ){
9481        cmOpts.bQuote = 0;
9482      }else if( zMode==0 ){
9483        zMode = z;
9484        /* Apply defaults for qbox pseudo-mods. If that
9485         * overwrites already-set values, user was informed of this.
9486         */
9487        if( strcmp(z, "qbox")==0 ){
9488          ColModeOpts cmo = ColModeOpts_default_qbox;
9489          zMode = "box";
9490          cmOpts = cmo;
9491        }
9492      }else if( zTabname==0 ){
9493        zTabname = z;
9494      }else if( z[0]=='-' ){
9495        utf8_printf(stderr, "unknown option: %s\n", z);
9496        utf8_printf(stderr, "options:\n"
9497                            "  --noquote\n"
9498                            "  --quote\n"
9499                            "  --wordwrap on/off\n"
9500                            "  --wrap N\n"
9501                            "  --ww\n");
9502        rc = 1;
9503        goto meta_command_exit;
9504      }else{
9505        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9506        rc = 1;
9507        goto meta_command_exit;
9508      }
9509    }
9510    if( zMode==0 ){
9511      if( p->mode==MODE_Column
9512       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9513      ){
9514        raw_printf
9515          (p->out,
9516           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9517           modeDescr[p->mode], p->cmOpts.iWrap,
9518           p->cmOpts.bWordWrap ? "on" : "off",
9519           p->cmOpts.bQuote ? "" : "no");
9520      }else{
9521        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
9522      }
9523      zMode = modeDescr[p->mode];
9524    }
9525    n2 = strlen30(zMode);
9526    if( strncmp(zMode,"lines",n2)==0 ){
9527      p->mode = MODE_Line;
9528      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9529    }else if( strncmp(zMode,"columns",n2)==0 ){
9530      p->mode = MODE_Column;
9531      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9532        p->showHeader = 1;
9533      }
9534      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9535      p->cmOpts = cmOpts;
9536    }else if( strncmp(zMode,"list",n2)==0 ){
9537      p->mode = MODE_List;
9538      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9539      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9540    }else if( strncmp(zMode,"html",n2)==0 ){
9541      p->mode = MODE_Html;
9542    }else if( strncmp(zMode,"tcl",n2)==0 ){
9543      p->mode = MODE_Tcl;
9544      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9545      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9546    }else if( strncmp(zMode,"csv",n2)==0 ){
9547      p->mode = MODE_Csv;
9548      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9549      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9550    }else if( strncmp(zMode,"tabs",n2)==0 ){
9551      p->mode = MODE_List;
9552      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9553    }else if( strncmp(zMode,"insert",n2)==0 ){
9554      p->mode = MODE_Insert;
9555      set_table_name(p, zTabname ? zTabname : "table");
9556    }else if( strncmp(zMode,"quote",n2)==0 ){
9557      p->mode = MODE_Quote;
9558      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9559      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9560    }else if( strncmp(zMode,"ascii",n2)==0 ){
9561      p->mode = MODE_Ascii;
9562      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9563      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9564    }else if( strncmp(zMode,"markdown",n2)==0 ){
9565      p->mode = MODE_Markdown;
9566      p->cmOpts = cmOpts;
9567    }else if( strncmp(zMode,"table",n2)==0 ){
9568      p->mode = MODE_Table;
9569      p->cmOpts = cmOpts;
9570    }else if( strncmp(zMode,"box",n2)==0 ){
9571      p->mode = MODE_Box;
9572      p->cmOpts = cmOpts;
9573    }else if( strncmp(zMode,"count",n2)==0 ){
9574      p->mode = MODE_Count;
9575    }else if( strncmp(zMode,"off",n2)==0 ){
9576      p->mode = MODE_Off;
9577    }else if( strncmp(zMode,"json",n2)==0 ){
9578      p->mode = MODE_Json;
9579    }else{
9580      raw_printf(stderr, "Error: mode should be one of: "
9581         "ascii box column csv html insert json line list markdown "
9582         "qbox quote table tabs tcl\n");
9583      rc = 1;
9584    }
9585    p->cMode = p->mode;
9586  }else
9587
9588#ifndef SQLITE_SHELL_FIDDLE
9589  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
9590    if( nArg!=2 ){
9591      raw_printf(stderr, "Usage: .nonce NONCE\n");
9592      rc = 1;
9593    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
9594      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9595                 p->lineno, azArg[1]);
9596      exit(1);
9597    }else{
9598      p->bSafeMode = 0;
9599      return 0;  /* Return immediately to bypass the safe mode reset
9600                 ** at the end of this procedure */
9601    }
9602  }else
9603#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9604
9605  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
9606    if( nArg==2 ){
9607      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9608                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9609    }else{
9610      raw_printf(stderr, "Usage: .nullvalue STRING\n");
9611      rc = 1;
9612    }
9613  }else
9614
9615  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
9616    const char *zFN = 0;     /* Pointer to constant filename */
9617    char *zNewFilename = 0;  /* Name of the database file to open */
9618    int iName = 1;           /* Index in azArg[] of the filename */
9619    int newFlag = 0;         /* True to delete file before opening */
9620    int openMode = SHELL_OPEN_UNSPEC;
9621
9622    /* Check for command-line arguments */
9623    for(iName=1; iName<nArg; iName++){
9624      const char *z = azArg[iName];
9625#ifndef SQLITE_SHELL_FIDDLE
9626      if( optionMatch(z,"new") ){
9627        newFlag = 1;
9628#ifdef SQLITE_HAVE_ZLIB
9629      }else if( optionMatch(z, "zip") ){
9630        openMode = SHELL_OPEN_ZIPFILE;
9631#endif
9632      }else if( optionMatch(z, "append") ){
9633        openMode = SHELL_OPEN_APPENDVFS;
9634      }else if( optionMatch(z, "readonly") ){
9635        openMode = SHELL_OPEN_READONLY;
9636      }else if( optionMatch(z, "nofollow") ){
9637        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9638#ifndef SQLITE_OMIT_DESERIALIZE
9639      }else if( optionMatch(z, "deserialize") ){
9640        openMode = SHELL_OPEN_DESERIALIZE;
9641      }else if( optionMatch(z, "hexdb") ){
9642        openMode = SHELL_OPEN_HEXDB;
9643      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9644        p->szMax = integerValue(azArg[++iName]);
9645#endif /* SQLITE_OMIT_DESERIALIZE */
9646      }else
9647#endif /* !SQLITE_SHELL_FIDDLE */
9648      if( z[0]=='-' ){
9649        utf8_printf(stderr, "unknown option: %s\n", z);
9650        rc = 1;
9651        goto meta_command_exit;
9652      }else if( zFN ){
9653        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9654        rc = 1;
9655        goto meta_command_exit;
9656      }else{
9657        zFN = z;
9658      }
9659    }
9660
9661    /* Close the existing database */
9662    session_close_all(p, -1);
9663    close_db(p->db);
9664    p->db = 0;
9665    p->pAuxDb->zDbFilename = 0;
9666    sqlite3_free(p->pAuxDb->zFreeOnClose);
9667    p->pAuxDb->zFreeOnClose = 0;
9668    p->openMode = openMode;
9669    p->openFlags = 0;
9670    p->szMax = 0;
9671
9672    /* If a filename is specified, try to open it first */
9673    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9674      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9675#ifndef SQLITE_SHELL_FIDDLE
9676      if( p->bSafeMode
9677       && p->openMode!=SHELL_OPEN_HEXDB
9678       && zFN
9679       && strcmp(zFN,":memory:")!=0
9680      ){
9681        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9682      }
9683#else
9684      /* WASM mode has its own sandboxed pseudo-filesystem. */
9685#endif
9686      if( zFN ){
9687        zNewFilename = sqlite3_mprintf("%s", zFN);
9688        shell_check_oom(zNewFilename);
9689      }else{
9690        zNewFilename = 0;
9691      }
9692      p->pAuxDb->zDbFilename = zNewFilename;
9693      open_db(p, OPEN_DB_KEEPALIVE);
9694      if( p->db==0 ){
9695        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9696        sqlite3_free(zNewFilename);
9697      }else{
9698        p->pAuxDb->zFreeOnClose = zNewFilename;
9699      }
9700    }
9701    if( p->db==0 ){
9702      /* As a fall-back open a TEMP database */
9703      p->pAuxDb->zDbFilename = 0;
9704      open_db(p, 0);
9705    }
9706  }else
9707
9708#ifndef SQLITE_SHELL_FIDDLE
9709  if( (c=='o'
9710        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
9711   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
9712  ){
9713    char *zFile = 0;
9714    int bTxtMode = 0;
9715    int i;
9716    int eMode = 0;
9717    int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
9718    unsigned char zBOM[4];    /* Byte-order mark to using if --bom is present */
9719
9720    zBOM[0] = 0;
9721    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9722    if( c=='e' ){
9723      eMode = 'x';
9724      bOnce = 2;
9725    }else if( strncmp(azArg[0],"once",n)==0 ){
9726      bOnce = 1;
9727    }
9728    for(i=1; i<nArg; i++){
9729      char *z = azArg[i];
9730      if( z[0]=='-' ){
9731        if( z[1]=='-' ) z++;
9732        if( strcmp(z,"-bom")==0 ){
9733          zBOM[0] = 0xef;
9734          zBOM[1] = 0xbb;
9735          zBOM[2] = 0xbf;
9736          zBOM[3] = 0;
9737        }else if( c!='e' && strcmp(z,"-x")==0 ){
9738          eMode = 'x';  /* spreadsheet */
9739        }else if( c!='e' && strcmp(z,"-e")==0 ){
9740          eMode = 'e';  /* text editor */
9741        }else{
9742          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9743                      azArg[i]);
9744          showHelp(p->out, azArg[0]);
9745          rc = 1;
9746          goto meta_command_exit;
9747        }
9748      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9749        zFile = sqlite3_mprintf("%s", z);
9750        if( zFile && zFile[0]=='|' ){
9751          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9752          break;
9753        }
9754      }else{
9755        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9756                    azArg[i]);
9757        showHelp(p->out, azArg[0]);
9758        rc = 1;
9759        sqlite3_free(zFile);
9760        goto meta_command_exit;
9761      }
9762    }
9763    if( zFile==0 ){
9764      zFile = sqlite3_mprintf("stdout");
9765    }
9766    if( bOnce ){
9767      p->outCount = 2;
9768    }else{
9769      p->outCount = 0;
9770    }
9771    output_reset(p);
9772#ifndef SQLITE_NOHAVE_SYSTEM
9773    if( eMode=='e' || eMode=='x' ){
9774      p->doXdgOpen = 1;
9775      outputModePush(p);
9776      if( eMode=='x' ){
9777        /* spreadsheet mode.  Output as CSV. */
9778        newTempFile(p, "csv");
9779        ShellClearFlag(p, SHFLG_Echo);
9780        p->mode = MODE_Csv;
9781        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9782        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9783      }else{
9784        /* text editor mode */
9785        newTempFile(p, "txt");
9786        bTxtMode = 1;
9787      }
9788      sqlite3_free(zFile);
9789      zFile = sqlite3_mprintf("%s", p->zTempFile);
9790    }
9791#endif /* SQLITE_NOHAVE_SYSTEM */
9792    shell_check_oom(zFile);
9793    if( zFile[0]=='|' ){
9794#ifdef SQLITE_OMIT_POPEN
9795      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9796      rc = 1;
9797      p->out = stdout;
9798#else
9799      p->out = popen(zFile + 1, "w");
9800      if( p->out==0 ){
9801        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9802        p->out = stdout;
9803        rc = 1;
9804      }else{
9805        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9806        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9807      }
9808#endif
9809    }else{
9810      p->out = output_file_open(zFile, bTxtMode);
9811      if( p->out==0 ){
9812        if( strcmp(zFile,"off")!=0 ){
9813          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9814        }
9815        p->out = stdout;
9816        rc = 1;
9817      } else {
9818        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9819        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9820      }
9821    }
9822    sqlite3_free(zFile);
9823  }else
9824#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9825
9826  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
9827    open_db(p,0);
9828    if( nArg<=1 ) goto parameter_syntax_error;
9829
9830    /* .parameter clear
9831    ** Clear all bind parameters by dropping the TEMP table that holds them.
9832    */
9833    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
9834      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9835                   0, 0, 0);
9836    }else
9837
9838    /* .parameter list
9839    ** List all bind parameters.
9840    */
9841    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
9842      sqlite3_stmt *pStmt = 0;
9843      int rx;
9844      int len = 0;
9845      rx = sqlite3_prepare_v2(p->db,
9846             "SELECT max(length(key)) "
9847             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9848      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9849        len = sqlite3_column_int(pStmt, 0);
9850        if( len>40 ) len = 40;
9851      }
9852      sqlite3_finalize(pStmt);
9853      pStmt = 0;
9854      if( len ){
9855        rx = sqlite3_prepare_v2(p->db,
9856             "SELECT key, quote(value) "
9857             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9858        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9859          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9860                      sqlite3_column_text(pStmt,1));
9861        }
9862        sqlite3_finalize(pStmt);
9863      }
9864    }else
9865
9866    /* .parameter init
9867    ** Make sure the TEMP table used to hold bind parameters exists.
9868    ** Create it if necessary.
9869    */
9870    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
9871      bind_table_init(p);
9872    }else
9873
9874    /* .parameter set NAME VALUE
9875    ** Set or reset a bind parameter.  NAME should be the full parameter
9876    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9877    ** VALUE can be in either SQL literal notation, or if not it will be
9878    ** understood to be a text string.
9879    */
9880    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
9881      int rx;
9882      char *zSql;
9883      sqlite3_stmt *pStmt;
9884      const char *zKey = azArg[2];
9885      const char *zValue = azArg[3];
9886      bind_table_init(p);
9887      zSql = sqlite3_mprintf(
9888                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9889                  "VALUES(%Q,%s);", zKey, zValue);
9890      shell_check_oom(zSql);
9891      pStmt = 0;
9892      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9893      sqlite3_free(zSql);
9894      if( rx!=SQLITE_OK ){
9895        sqlite3_finalize(pStmt);
9896        pStmt = 0;
9897        zSql = sqlite3_mprintf(
9898                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9899                   "VALUES(%Q,%Q);", zKey, zValue);
9900        shell_check_oom(zSql);
9901        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9902        sqlite3_free(zSql);
9903        if( rx!=SQLITE_OK ){
9904          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9905          sqlite3_finalize(pStmt);
9906          pStmt = 0;
9907          rc = 1;
9908        }
9909      }
9910      sqlite3_step(pStmt);
9911      sqlite3_finalize(pStmt);
9912    }else
9913
9914    /* .parameter unset NAME
9915    ** Remove the NAME binding from the parameter binding table, if it
9916    ** exists.
9917    */
9918    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
9919      char *zSql = sqlite3_mprintf(
9920          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9921      shell_check_oom(zSql);
9922      sqlite3_exec(p->db, zSql, 0, 0, 0);
9923      sqlite3_free(zSql);
9924    }else
9925    /* If no command name matches, show a syntax error */
9926    parameter_syntax_error:
9927    showHelp(p->out, "parameter");
9928  }else
9929
9930  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
9931    int i;
9932    for(i=1; i<nArg; i++){
9933      if( i>1 ) raw_printf(p->out, " ");
9934      utf8_printf(p->out, "%s", azArg[i]);
9935    }
9936    raw_printf(p->out, "\n");
9937  }else
9938
9939#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9940  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
9941    int i;
9942    int nn = 0;
9943    p->flgProgress = 0;
9944    p->mxProgress = 0;
9945    p->nProgress = 0;
9946    for(i=1; i<nArg; i++){
9947      const char *z = azArg[i];
9948      if( z[0]=='-' ){
9949        z++;
9950        if( z[0]=='-' ) z++;
9951        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9952          p->flgProgress |= SHELL_PROGRESS_QUIET;
9953          continue;
9954        }
9955        if( strcmp(z,"reset")==0 ){
9956          p->flgProgress |= SHELL_PROGRESS_RESET;
9957          continue;
9958        }
9959        if( strcmp(z,"once")==0 ){
9960          p->flgProgress |= SHELL_PROGRESS_ONCE;
9961          continue;
9962        }
9963        if( strcmp(z,"limit")==0 ){
9964          if( i+1>=nArg ){
9965            utf8_printf(stderr, "Error: missing argument on --limit\n");
9966            rc = 1;
9967            goto meta_command_exit;
9968          }else{
9969            p->mxProgress = (int)integerValue(azArg[++i]);
9970          }
9971          continue;
9972        }
9973        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9974        rc = 1;
9975        goto meta_command_exit;
9976      }else{
9977        nn = (int)integerValue(z);
9978      }
9979    }
9980    open_db(p, 0);
9981    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9982  }else
9983#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9984
9985  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9986    if( nArg >= 2) {
9987      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9988    }
9989    if( nArg >= 3) {
9990      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9991    }
9992  }else
9993
9994#ifndef SQLITE_SHELL_FIDDLE
9995  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9996    rc = 2;
9997  }else
9998#endif
9999
10000#ifndef SQLITE_SHELL_FIDDLE
10001  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
10002    FILE *inSaved = p->in;
10003    int savedLineno = p->lineno;
10004    failIfSafeMode(p, "cannot run .read in safe mode");
10005    if( nArg!=2 ){
10006      raw_printf(stderr, "Usage: .read FILE\n");
10007      rc = 1;
10008      goto meta_command_exit;
10009    }
10010    if( azArg[1][0]=='|' ){
10011#ifdef SQLITE_OMIT_POPEN
10012      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
10013      rc = 1;
10014      p->out = stdout;
10015#else
10016      p->in = popen(azArg[1]+1, "r");
10017      if( p->in==0 ){
10018        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
10019        rc = 1;
10020      }else{
10021        rc = process_input(p);
10022        pclose(p->in);
10023      }
10024#endif
10025    }else if( (p->in = openChrSource(azArg[1]))==0 ){
10026      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
10027      rc = 1;
10028    }else{
10029      rc = process_input(p);
10030      fclose(p->in);
10031    }
10032    p->in = inSaved;
10033    p->lineno = savedLineno;
10034  }else
10035#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10036
10037#ifndef SQLITE_SHELL_FIDDLE
10038  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
10039    const char *zSrcFile;
10040    const char *zDb;
10041    sqlite3 *pSrc;
10042    sqlite3_backup *pBackup;
10043    int nTimeout = 0;
10044
10045    failIfSafeMode(p, "cannot run .restore in safe mode");
10046    if( nArg==2 ){
10047      zSrcFile = azArg[1];
10048      zDb = "main";
10049    }else if( nArg==3 ){
10050      zSrcFile = azArg[2];
10051      zDb = azArg[1];
10052    }else{
10053      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
10054      rc = 1;
10055      goto meta_command_exit;
10056    }
10057    rc = sqlite3_open(zSrcFile, &pSrc);
10058    if( rc!=SQLITE_OK ){
10059      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
10060      close_db(pSrc);
10061      return 1;
10062    }
10063    open_db(p, 0);
10064    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
10065    if( pBackup==0 ){
10066      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10067      close_db(pSrc);
10068      return 1;
10069    }
10070    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
10071          || rc==SQLITE_BUSY  ){
10072      if( rc==SQLITE_BUSY ){
10073        if( nTimeout++ >= 3 ) break;
10074        sqlite3_sleep(100);
10075      }
10076    }
10077    sqlite3_backup_finish(pBackup);
10078    if( rc==SQLITE_DONE ){
10079      rc = 0;
10080    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
10081      raw_printf(stderr, "Error: source database is busy\n");
10082      rc = 1;
10083    }else{
10084      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10085      rc = 1;
10086    }
10087    close_db(pSrc);
10088  }else
10089#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10090
10091  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
10092    if( nArg==2 ){
10093      p->scanstatsOn = (u8)booleanValue(azArg[1]);
10094#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10095      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
10096#endif
10097    }else{
10098      raw_printf(stderr, "Usage: .scanstats on|off\n");
10099      rc = 1;
10100    }
10101  }else
10102
10103  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
10104    ShellText sSelect;
10105    ShellState data;
10106    char *zErrMsg = 0;
10107    const char *zDiv = "(";
10108    const char *zName = 0;
10109    int iSchema = 0;
10110    int bDebug = 0;
10111    int bNoSystemTabs = 0;
10112    int ii;
10113
10114    open_db(p, 0);
10115    memcpy(&data, p, sizeof(data));
10116    data.showHeader = 0;
10117    data.cMode = data.mode = MODE_Semi;
10118    initText(&sSelect);
10119    for(ii=1; ii<nArg; ii++){
10120      if( optionMatch(azArg[ii],"indent") ){
10121        data.cMode = data.mode = MODE_Pretty;
10122      }else if( optionMatch(azArg[ii],"debug") ){
10123        bDebug = 1;
10124      }else if( optionMatch(azArg[ii],"nosys") ){
10125        bNoSystemTabs = 1;
10126      }else if( azArg[ii][0]=='-' ){
10127        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
10128        rc = 1;
10129        goto meta_command_exit;
10130      }else if( zName==0 ){
10131        zName = azArg[ii];
10132      }else{
10133        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10134        rc = 1;
10135        goto meta_command_exit;
10136      }
10137    }
10138    if( zName!=0 ){
10139      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
10140                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
10141                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
10142                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
10143      if( isSchema ){
10144        char *new_argv[2], *new_colv[2];
10145        new_argv[0] = sqlite3_mprintf(
10146                      "CREATE TABLE %s (\n"
10147                      "  type text,\n"
10148                      "  name text,\n"
10149                      "  tbl_name text,\n"
10150                      "  rootpage integer,\n"
10151                      "  sql text\n"
10152                      ")", zName);
10153        shell_check_oom(new_argv[0]);
10154        new_argv[1] = 0;
10155        new_colv[0] = "sql";
10156        new_colv[1] = 0;
10157        callback(&data, 1, new_argv, new_colv);
10158        sqlite3_free(new_argv[0]);
10159      }
10160    }
10161    if( zDiv ){
10162      sqlite3_stmt *pStmt = 0;
10163      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
10164                              -1, &pStmt, 0);
10165      if( rc ){
10166        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
10167        sqlite3_finalize(pStmt);
10168        rc = 1;
10169        goto meta_command_exit;
10170      }
10171      appendText(&sSelect, "SELECT sql FROM", 0);
10172      iSchema = 0;
10173      while( sqlite3_step(pStmt)==SQLITE_ROW ){
10174        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
10175        char zScNum[30];
10176        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
10177        appendText(&sSelect, zDiv, 0);
10178        zDiv = " UNION ALL ";
10179        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
10180        if( sqlite3_stricmp(zDb, "main")!=0 ){
10181          appendText(&sSelect, zDb, '\'');
10182        }else{
10183          appendText(&sSelect, "NULL", 0);
10184        }
10185        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10186        appendText(&sSelect, zScNum, 0);
10187        appendText(&sSelect, " AS snum, ", 0);
10188        appendText(&sSelect, zDb, '\'');
10189        appendText(&sSelect, " AS sname FROM ", 0);
10190        appendText(&sSelect, zDb, quoteChar(zDb));
10191        appendText(&sSelect, ".sqlite_schema", 0);
10192      }
10193      sqlite3_finalize(pStmt);
10194#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10195      if( zName ){
10196        appendText(&sSelect,
10197           " UNION ALL SELECT shell_module_schema(name),"
10198           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10199        0);
10200      }
10201#endif
10202      appendText(&sSelect, ") WHERE ", 0);
10203      if( zName ){
10204        char *zQarg = sqlite3_mprintf("%Q", zName);
10205        int bGlob;
10206        shell_check_oom(zQarg);
10207        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10208                strchr(zName, '[') != 0;
10209        if( strchr(zName, '.') ){
10210          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10211        }else{
10212          appendText(&sSelect, "lower(tbl_name)", 0);
10213        }
10214        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10215        appendText(&sSelect, zQarg, 0);
10216        if( !bGlob ){
10217          appendText(&sSelect, " ESCAPE '\\' ", 0);
10218        }
10219        appendText(&sSelect, " AND ", 0);
10220        sqlite3_free(zQarg);
10221      }
10222      if( bNoSystemTabs ){
10223        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10224      }
10225      appendText(&sSelect, "sql IS NOT NULL"
10226                           " ORDER BY snum, rowid", 0);
10227      if( bDebug ){
10228        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
10229      }else{
10230        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10231      }
10232      freeText(&sSelect);
10233    }
10234    if( zErrMsg ){
10235      utf8_printf(stderr,"Error: %s\n", zErrMsg);
10236      sqlite3_free(zErrMsg);
10237      rc = 1;
10238    }else if( rc != SQLITE_OK ){
10239      raw_printf(stderr,"Error: querying schema information\n");
10240      rc = 1;
10241    }else{
10242      rc = 0;
10243    }
10244  }else
10245
10246  if( (c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0)
10247   || (c=='t' && n==9  && strncmp(azArg[0], "treetrace", n)==0)
10248  ){
10249    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10250    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10251  }else
10252
10253#if defined(SQLITE_ENABLE_SESSION)
10254  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
10255    struct AuxDb *pAuxDb = p->pAuxDb;
10256    OpenSession *pSession = &pAuxDb->aSession[0];
10257    char **azCmd = &azArg[1];
10258    int iSes = 0;
10259    int nCmd = nArg - 1;
10260    int i;
10261    if( nArg<=1 ) goto session_syntax_error;
10262    open_db(p, 0);
10263    if( nArg>=3 ){
10264      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10265        if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10266      }
10267      if( iSes<pAuxDb->nSession ){
10268        pSession = &pAuxDb->aSession[iSes];
10269        azCmd++;
10270        nCmd--;
10271      }else{
10272        pSession = &pAuxDb->aSession[0];
10273        iSes = 0;
10274      }
10275    }
10276
10277    /* .session attach TABLE
10278    ** Invoke the sqlite3session_attach() interface to attach a particular
10279    ** table so that it is never filtered.
10280    */
10281    if( strcmp(azCmd[0],"attach")==0 ){
10282      if( nCmd!=2 ) goto session_syntax_error;
10283      if( pSession->p==0 ){
10284        session_not_open:
10285        raw_printf(stderr, "ERROR: No sessions are open\n");
10286      }else{
10287        rc = sqlite3session_attach(pSession->p, azCmd[1]);
10288        if( rc ){
10289          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
10290          rc = 0;
10291        }
10292      }
10293    }else
10294
10295    /* .session changeset FILE
10296    ** .session patchset FILE
10297    ** Write a changeset or patchset into a file.  The file is overwritten.
10298    */
10299    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
10300      FILE *out = 0;
10301      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10302      if( nCmd!=2 ) goto session_syntax_error;
10303      if( pSession->p==0 ) goto session_not_open;
10304      out = fopen(azCmd[1], "wb");
10305      if( out==0 ){
10306        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
10307                    azCmd[1]);
10308      }else{
10309        int szChng;
10310        void *pChng;
10311        if( azCmd[0][0]=='c' ){
10312          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10313        }else{
10314          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10315        }
10316        if( rc ){
10317          printf("Error: error code %d\n", rc);
10318          rc = 0;
10319        }
10320        if( pChng
10321          && fwrite(pChng, szChng, 1, out)!=1 ){
10322          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
10323                  szChng);
10324        }
10325        sqlite3_free(pChng);
10326        fclose(out);
10327      }
10328    }else
10329
10330    /* .session close
10331    ** Close the identified session
10332    */
10333    if( strcmp(azCmd[0], "close")==0 ){
10334      if( nCmd!=1 ) goto session_syntax_error;
10335      if( pAuxDb->nSession ){
10336        session_close(pSession);
10337        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10338      }
10339    }else
10340
10341    /* .session enable ?BOOLEAN?
10342    ** Query or set the enable flag
10343    */
10344    if( strcmp(azCmd[0], "enable")==0 ){
10345      int ii;
10346      if( nCmd>2 ) goto session_syntax_error;
10347      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10348      if( pAuxDb->nSession ){
10349        ii = sqlite3session_enable(pSession->p, ii);
10350        utf8_printf(p->out, "session %s enable flag = %d\n",
10351                    pSession->zName, ii);
10352      }
10353    }else
10354
10355    /* .session filter GLOB ....
10356    ** Set a list of GLOB patterns of table names to be excluded.
10357    */
10358    if( strcmp(azCmd[0], "filter")==0 ){
10359      int ii, nByte;
10360      if( nCmd<2 ) goto session_syntax_error;
10361      if( pAuxDb->nSession ){
10362        for(ii=0; ii<pSession->nFilter; ii++){
10363          sqlite3_free(pSession->azFilter[ii]);
10364        }
10365        sqlite3_free(pSession->azFilter);
10366        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10367        pSession->azFilter = sqlite3_malloc( nByte );
10368        if( pSession->azFilter==0 ){
10369          raw_printf(stderr, "Error: out or memory\n");
10370          exit(1);
10371        }
10372        for(ii=1; ii<nCmd; ii++){
10373          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10374          shell_check_oom(x);
10375        }
10376        pSession->nFilter = ii-1;
10377      }
10378    }else
10379
10380    /* .session indirect ?BOOLEAN?
10381    ** Query or set the indirect flag
10382    */
10383    if( strcmp(azCmd[0], "indirect")==0 ){
10384      int ii;
10385      if( nCmd>2 ) goto session_syntax_error;
10386      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10387      if( pAuxDb->nSession ){
10388        ii = sqlite3session_indirect(pSession->p, ii);
10389        utf8_printf(p->out, "session %s indirect flag = %d\n",
10390                    pSession->zName, ii);
10391      }
10392    }else
10393
10394    /* .session isempty
10395    ** Determine if the session is empty
10396    */
10397    if( strcmp(azCmd[0], "isempty")==0 ){
10398      int ii;
10399      if( nCmd!=1 ) goto session_syntax_error;
10400      if( pAuxDb->nSession ){
10401        ii = sqlite3session_isempty(pSession->p);
10402        utf8_printf(p->out, "session %s isempty flag = %d\n",
10403                    pSession->zName, ii);
10404      }
10405    }else
10406
10407    /* .session list
10408    ** List all currently open sessions
10409    */
10410    if( strcmp(azCmd[0],"list")==0 ){
10411      for(i=0; i<pAuxDb->nSession; i++){
10412        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
10413      }
10414    }else
10415
10416    /* .session open DB NAME
10417    ** Open a new session called NAME on the attached database DB.
10418    ** DB is normally "main".
10419    */
10420    if( strcmp(azCmd[0],"open")==0 ){
10421      char *zName;
10422      if( nCmd!=3 ) goto session_syntax_error;
10423      zName = azCmd[2];
10424      if( zName[0]==0 ) goto session_syntax_error;
10425      for(i=0; i<pAuxDb->nSession; i++){
10426        if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10427          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
10428          goto meta_command_exit;
10429        }
10430      }
10431      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10432        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10433        goto meta_command_exit;
10434      }
10435      pSession = &pAuxDb->aSession[pAuxDb->nSession];
10436      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10437      if( rc ){
10438        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
10439        rc = 0;
10440        goto meta_command_exit;
10441      }
10442      pSession->nFilter = 0;
10443      sqlite3session_table_filter(pSession->p, session_filter, pSession);
10444      pAuxDb->nSession++;
10445      pSession->zName = sqlite3_mprintf("%s", zName);
10446      shell_check_oom(pSession->zName);
10447    }else
10448    /* If no command name matches, show a syntax error */
10449    session_syntax_error:
10450    showHelp(p->out, "session");
10451  }else
10452#endif
10453
10454#ifdef SQLITE_DEBUG
10455  /* Undocumented commands for internal testing.  Subject to change
10456  ** without notice. */
10457  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
10458    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10459      int i, v;
10460      for(i=1; i<nArg; i++){
10461        v = booleanValue(azArg[i]);
10462        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
10463      }
10464    }
10465    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
10466      int i; sqlite3_int64 v;
10467      for(i=1; i<nArg; i++){
10468        char zBuf[200];
10469        v = integerValue(azArg[i]);
10470        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10471        utf8_printf(p->out, "%s", zBuf);
10472      }
10473    }
10474  }else
10475#endif
10476
10477  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
10478    int bIsInit = 0;         /* True to initialize the SELFTEST table */
10479    int bVerbose = 0;        /* Verbose output */
10480    int bSelftestExists;     /* True if SELFTEST already exists */
10481    int i, k;                /* Loop counters */
10482    int nTest = 0;           /* Number of tests runs */
10483    int nErr = 0;            /* Number of errors seen */
10484    ShellText str;           /* Answer for a query */
10485    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10486
10487    open_db(p,0);
10488    for(i=1; i<nArg; i++){
10489      const char *z = azArg[i];
10490      if( z[0]=='-' && z[1]=='-' ) z++;
10491      if( strcmp(z,"-init")==0 ){
10492        bIsInit = 1;
10493      }else
10494      if( strcmp(z,"-v")==0 ){
10495        bVerbose++;
10496      }else
10497      {
10498        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10499                    azArg[i], azArg[0]);
10500        raw_printf(stderr, "Should be one of: --init -v\n");
10501        rc = 1;
10502        goto meta_command_exit;
10503      }
10504    }
10505    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10506           != SQLITE_OK ){
10507      bSelftestExists = 0;
10508    }else{
10509      bSelftestExists = 1;
10510    }
10511    if( bIsInit ){
10512      createSelftestTable(p);
10513      bSelftestExists = 1;
10514    }
10515    initText(&str);
10516    appendText(&str, "x", 0);
10517    for(k=bSelftestExists; k>=0; k--){
10518      if( k==1 ){
10519        rc = sqlite3_prepare_v2(p->db,
10520            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10521            -1, &pStmt, 0);
10522      }else{
10523        rc = sqlite3_prepare_v2(p->db,
10524          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10525          "      (1,'run','PRAGMA integrity_check','ok')",
10526          -1, &pStmt, 0);
10527      }
10528      if( rc ){
10529        raw_printf(stderr, "Error querying the selftest table\n");
10530        rc = 1;
10531        sqlite3_finalize(pStmt);
10532        goto meta_command_exit;
10533      }
10534      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10535        int tno = sqlite3_column_int(pStmt, 0);
10536        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10537        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10538        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10539
10540        if( zOp==0 ) continue;
10541        if( zSql==0 ) continue;
10542        if( zAns==0 ) continue;
10543        k = 0;
10544        if( bVerbose>0 ){
10545          printf("%d: %s %s\n", tno, zOp, zSql);
10546        }
10547        if( strcmp(zOp,"memo")==0 ){
10548          utf8_printf(p->out, "%s\n", zSql);
10549        }else
10550        if( strcmp(zOp,"run")==0 ){
10551          char *zErrMsg = 0;
10552          str.n = 0;
10553          str.z[0] = 0;
10554          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10555          nTest++;
10556          if( bVerbose ){
10557            utf8_printf(p->out, "Result: %s\n", str.z);
10558          }
10559          if( rc || zErrMsg ){
10560            nErr++;
10561            rc = 1;
10562            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
10563            sqlite3_free(zErrMsg);
10564          }else if( strcmp(zAns,str.z)!=0 ){
10565            nErr++;
10566            rc = 1;
10567            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10568            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
10569          }
10570        }else
10571        {
10572          utf8_printf(stderr,
10573            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10574          rc = 1;
10575          break;
10576        }
10577      } /* End loop over rows of content from SELFTEST */
10578      sqlite3_finalize(pStmt);
10579    } /* End loop over k */
10580    freeText(&str);
10581    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10582  }else
10583
10584  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
10585    if( nArg<2 || nArg>3 ){
10586      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10587      rc = 1;
10588    }
10589    if( nArg>=2 ){
10590      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10591                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10592    }
10593    if( nArg>=3 ){
10594      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10595                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10596    }
10597  }else
10598
10599  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
10600    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
10601    int i;                   /* Loop counter */
10602    int bSchema = 0;         /* Also hash the schema */
10603    int bSeparate = 0;       /* Hash each table separately */
10604    int iSize = 224;         /* Hash algorithm to use */
10605    int bDebug = 0;          /* Only show the query that would have run */
10606    sqlite3_stmt *pStmt;     /* For querying tables names */
10607    char *zSql;              /* SQL to be run */
10608    char *zSep;              /* Separator */
10609    ShellText sSql;          /* Complete SQL for the query to run the hash */
10610    ShellText sQuery;        /* Set of queries used to read all content */
10611    open_db(p, 0);
10612    for(i=1; i<nArg; i++){
10613      const char *z = azArg[i];
10614      if( z[0]=='-' ){
10615        z++;
10616        if( z[0]=='-' ) z++;
10617        if( strcmp(z,"schema")==0 ){
10618          bSchema = 1;
10619        }else
10620        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
10621         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
10622        ){
10623          iSize = atoi(&z[5]);
10624        }else
10625        if( strcmp(z,"debug")==0 ){
10626          bDebug = 1;
10627        }else
10628        {
10629          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10630                      azArg[i], azArg[0]);
10631          showHelp(p->out, azArg[0]);
10632          rc = 1;
10633          goto meta_command_exit;
10634        }
10635      }else if( zLike ){
10636        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10637        rc = 1;
10638        goto meta_command_exit;
10639      }else{
10640        zLike = z;
10641        bSeparate = 1;
10642        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10643      }
10644    }
10645    if( bSchema ){
10646      zSql = "SELECT lower(name) FROM sqlite_schema"
10647             " WHERE type='table' AND coalesce(rootpage,0)>1"
10648             " UNION ALL SELECT 'sqlite_schema'"
10649             " ORDER BY 1 collate nocase";
10650    }else{
10651      zSql = "SELECT lower(name) FROM sqlite_schema"
10652             " WHERE type='table' AND coalesce(rootpage,0)>1"
10653             " AND name NOT LIKE 'sqlite_%'"
10654             " ORDER BY 1 collate nocase";
10655    }
10656    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10657    initText(&sQuery);
10658    initText(&sSql);
10659    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10660    zSep = "VALUES(";
10661    while( SQLITE_ROW==sqlite3_step(pStmt) ){
10662      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10663      if( zTab==0 ) continue;
10664      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10665      if( strncmp(zTab, "sqlite_",7)!=0 ){
10666        appendText(&sQuery,"SELECT * FROM ", 0);
10667        appendText(&sQuery,zTab,'"');
10668        appendText(&sQuery," NOT INDEXED;", 0);
10669      }else if( strcmp(zTab, "sqlite_schema")==0 ){
10670        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10671                           " ORDER BY name;", 0);
10672      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
10673        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10674                           " ORDER BY name;", 0);
10675      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
10676        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10677                           " ORDER BY tbl,idx;", 0);
10678      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
10679        appendText(&sQuery, "SELECT * FROM ", 0);
10680        appendText(&sQuery, zTab, 0);
10681        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10682      }
10683      appendText(&sSql, zSep, 0);
10684      appendText(&sSql, sQuery.z, '\'');
10685      sQuery.n = 0;
10686      appendText(&sSql, ",", 0);
10687      appendText(&sSql, zTab, '\'');
10688      zSep = "),(";
10689    }
10690    sqlite3_finalize(pStmt);
10691    if( bSeparate ){
10692      zSql = sqlite3_mprintf(
10693          "%s))"
10694          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10695          "   FROM [sha3sum$query]",
10696          sSql.z, iSize);
10697    }else{
10698      zSql = sqlite3_mprintf(
10699          "%s))"
10700          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10701          "   FROM [sha3sum$query]",
10702          sSql.z, iSize);
10703    }
10704    shell_check_oom(zSql);
10705    freeText(&sQuery);
10706    freeText(&sSql);
10707    if( bDebug ){
10708      utf8_printf(p->out, "%s\n", zSql);
10709    }else{
10710      shell_exec(p, zSql, 0);
10711    }
10712    sqlite3_free(zSql);
10713  }else
10714
10715#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10716  if( c=='s'
10717   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
10718  ){
10719    char *zCmd;
10720    int i, x;
10721    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10722    if( nArg<2 ){
10723      raw_printf(stderr, "Usage: .system COMMAND\n");
10724      rc = 1;
10725      goto meta_command_exit;
10726    }
10727    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10728    for(i=2; i<nArg && zCmd!=0; i++){
10729      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10730                             zCmd, azArg[i]);
10731    }
10732    x = zCmd!=0 ? system(zCmd) : 1;
10733    sqlite3_free(zCmd);
10734    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10735  }else
10736#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10737
10738  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
10739    static const char *azBool[] = { "off", "on", "trigger", "full"};
10740    const char *zOut;
10741    int i;
10742    if( nArg!=1 ){
10743      raw_printf(stderr, "Usage: .show\n");
10744      rc = 1;
10745      goto meta_command_exit;
10746    }
10747    utf8_printf(p->out, "%12.12s: %s\n","echo",
10748                azBool[ShellHasFlag(p, SHFLG_Echo)]);
10749    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10750    utf8_printf(p->out, "%12.12s: %s\n","explain",
10751         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10752    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10753    if( p->mode==MODE_Column
10754     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10755    ){
10756      utf8_printf
10757        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10758         modeDescr[p->mode], p->cmOpts.iWrap,
10759         p->cmOpts.bWordWrap ? "on" : "off",
10760         p->cmOpts.bQuote ? "" : "no");
10761    }else{
10762      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10763    }
10764    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10765      output_c_string(p->out, p->nullValue);
10766      raw_printf(p->out, "\n");
10767    utf8_printf(p->out,"%12.12s: %s\n","output",
10768            strlen30(p->outfile) ? p->outfile : "stdout");
10769    utf8_printf(p->out,"%12.12s: ", "colseparator");
10770      output_c_string(p->out, p->colSeparator);
10771      raw_printf(p->out, "\n");
10772    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10773      output_c_string(p->out, p->rowSeparator);
10774      raw_printf(p->out, "\n");
10775    switch( p->statsOn ){
10776      case 0:  zOut = "off";     break;
10777      default: zOut = "on";      break;
10778      case 2:  zOut = "stmt";    break;
10779      case 3:  zOut = "vmstep";  break;
10780    }
10781    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10782    utf8_printf(p->out, "%12.12s: ", "width");
10783    for (i=0;i<p->nWidth;i++) {
10784      raw_printf(p->out, "%d ", p->colWidth[i]);
10785    }
10786    raw_printf(p->out, "\n");
10787    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10788                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10789  }else
10790
10791  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
10792    if( nArg==2 ){
10793      if( strcmp(azArg[1],"stmt")==0 ){
10794        p->statsOn = 2;
10795      }else if( strcmp(azArg[1],"vmstep")==0 ){
10796        p->statsOn = 3;
10797      }else{
10798        p->statsOn = (u8)booleanValue(azArg[1]);
10799      }
10800    }else if( nArg==1 ){
10801      display_stats(p->db, p, 0);
10802    }else{
10803      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10804      rc = 1;
10805    }
10806  }else
10807
10808  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
10809   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
10810                 || strncmp(azArg[0], "indexes", n)==0) )
10811  ){
10812    sqlite3_stmt *pStmt;
10813    char **azResult;
10814    int nRow, nAlloc;
10815    int ii;
10816    ShellText s;
10817    initText(&s);
10818    open_db(p, 0);
10819    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10820    if( rc ){
10821      sqlite3_finalize(pStmt);
10822      return shellDatabaseError(p->db);
10823    }
10824
10825    if( nArg>2 && c=='i' ){
10826      /* It is an historical accident that the .indexes command shows an error
10827      ** when called with the wrong number of arguments whereas the .tables
10828      ** command does not. */
10829      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10830      rc = 1;
10831      sqlite3_finalize(pStmt);
10832      goto meta_command_exit;
10833    }
10834    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10835      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10836      if( zDbName==0 ) continue;
10837      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10838      if( sqlite3_stricmp(zDbName, "main")==0 ){
10839        appendText(&s, "SELECT name FROM ", 0);
10840      }else{
10841        appendText(&s, "SELECT ", 0);
10842        appendText(&s, zDbName, '\'');
10843        appendText(&s, "||'.'||name FROM ", 0);
10844      }
10845      appendText(&s, zDbName, '"');
10846      appendText(&s, ".sqlite_schema ", 0);
10847      if( c=='t' ){
10848        appendText(&s," WHERE type IN ('table','view')"
10849                      "   AND name NOT LIKE 'sqlite_%'"
10850                      "   AND name LIKE ?1", 0);
10851      }else{
10852        appendText(&s," WHERE type='index'"
10853                      "   AND tbl_name LIKE ?1", 0);
10854      }
10855    }
10856    rc = sqlite3_finalize(pStmt);
10857    if( rc==SQLITE_OK ){
10858      appendText(&s, " ORDER BY 1", 0);
10859      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10860    }
10861    freeText(&s);
10862    if( rc ) return shellDatabaseError(p->db);
10863
10864    /* Run the SQL statement prepared by the above block. Store the results
10865    ** as an array of nul-terminated strings in azResult[].  */
10866    nRow = nAlloc = 0;
10867    azResult = 0;
10868    if( nArg>1 ){
10869      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10870    }else{
10871      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10872    }
10873    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10874      if( nRow>=nAlloc ){
10875        char **azNew;
10876        int n2 = nAlloc*2 + 10;
10877        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10878        shell_check_oom(azNew);
10879        nAlloc = n2;
10880        azResult = azNew;
10881      }
10882      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10883      shell_check_oom(azResult[nRow]);
10884      nRow++;
10885    }
10886    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10887      rc = shellDatabaseError(p->db);
10888    }
10889
10890    /* Pretty-print the contents of array azResult[] to the output */
10891    if( rc==0 && nRow>0 ){
10892      int len, maxlen = 0;
10893      int i, j;
10894      int nPrintCol, nPrintRow;
10895      for(i=0; i<nRow; i++){
10896        len = strlen30(azResult[i]);
10897        if( len>maxlen ) maxlen = len;
10898      }
10899      nPrintCol = 80/(maxlen+2);
10900      if( nPrintCol<1 ) nPrintCol = 1;
10901      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10902      for(i=0; i<nPrintRow; i++){
10903        for(j=i; j<nRow; j+=nPrintRow){
10904          char *zSp = j<nPrintRow ? "" : "  ";
10905          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10906                      azResult[j] ? azResult[j]:"");
10907        }
10908        raw_printf(p->out, "\n");
10909      }
10910    }
10911
10912    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10913    sqlite3_free(azResult);
10914  }else
10915
10916#ifndef SQLITE_SHELL_FIDDLE
10917  /* Begin redirecting output to the file "testcase-out.txt" */
10918  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
10919    output_reset(p);
10920    p->out = output_file_open("testcase-out.txt", 0);
10921    if( p->out==0 ){
10922      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10923    }
10924    if( nArg>=2 ){
10925      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10926    }else{
10927      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10928    }
10929  }else
10930#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10931
10932#ifndef SQLITE_UNTESTABLE
10933  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
10934    static const struct {
10935       const char *zCtrlName;   /* Name of a test-control option */
10936       int ctrlCode;            /* Integer code for that option */
10937       int unSafe;              /* Not valid for --safe mode */
10938       const char *zUsage;      /* Usage notes */
10939    } aCtrl[] = {
10940      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10941      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10942    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10943    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10944      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10945      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10946    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10947      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10948      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10949      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10950      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10951      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10952#ifdef YYCOVERAGE
10953      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10954#endif
10955      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10956      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10957      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10958      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10959      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10960      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10961      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10962    };
10963    int testctrl = -1;
10964    int iCtrl = -1;
10965    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10966    int isOk = 0;
10967    int i, n2;
10968    const char *zCmd = 0;
10969
10970    open_db(p, 0);
10971    zCmd = nArg>=2 ? azArg[1] : "help";
10972
10973    /* The argument can optionally begin with "-" or "--" */
10974    if( zCmd[0]=='-' && zCmd[1] ){
10975      zCmd++;
10976      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10977    }
10978
10979    /* --help lists all test-controls */
10980    if( strcmp(zCmd,"help")==0 ){
10981      utf8_printf(p->out, "Available test-controls:\n");
10982      for(i=0; i<ArraySize(aCtrl); i++){
10983        utf8_printf(p->out, "  .testctrl %s %s\n",
10984                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10985      }
10986      rc = 1;
10987      goto meta_command_exit;
10988    }
10989
10990    /* convert testctrl text option to value. allow any unique prefix
10991    ** of the option name, or a numerical value. */
10992    n2 = strlen30(zCmd);
10993    for(i=0; i<ArraySize(aCtrl); i++){
10994      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10995        if( testctrl<0 ){
10996          testctrl = aCtrl[i].ctrlCode;
10997          iCtrl = i;
10998        }else{
10999          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
11000                              "Use \".testctrl --help\" for help\n", zCmd);
11001          rc = 1;
11002          goto meta_command_exit;
11003        }
11004      }
11005    }
11006    if( testctrl<0 ){
11007      utf8_printf(stderr,"Error: unknown test-control: %s\n"
11008                         "Use \".testctrl --help\" for help\n", zCmd);
11009    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
11010      utf8_printf(stderr,
11011         "line %d: \".testctrl %s\" may not be used in safe mode\n",
11012         p->lineno, aCtrl[iCtrl].zCtrlName);
11013      exit(1);
11014    }else{
11015      switch(testctrl){
11016
11017        /* sqlite3_test_control(int, db, int) */
11018        case SQLITE_TESTCTRL_OPTIMIZATIONS:
11019          if( nArg==3 ){
11020            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
11021            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11022            isOk = 3;
11023          }
11024          break;
11025
11026        /* sqlite3_test_control(int) */
11027        case SQLITE_TESTCTRL_PRNG_SAVE:
11028        case SQLITE_TESTCTRL_PRNG_RESTORE:
11029        case SQLITE_TESTCTRL_BYTEORDER:
11030          if( nArg==2 ){
11031            rc2 = sqlite3_test_control(testctrl);
11032            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
11033          }
11034          break;
11035
11036        /* sqlite3_test_control(int, uint) */
11037        case SQLITE_TESTCTRL_PENDING_BYTE:
11038          if( nArg==3 ){
11039            unsigned int opt = (unsigned int)integerValue(azArg[2]);
11040            rc2 = sqlite3_test_control(testctrl, opt);
11041            isOk = 3;
11042          }
11043          break;
11044
11045        /* sqlite3_test_control(int, int, sqlite3*) */
11046        case SQLITE_TESTCTRL_PRNG_SEED:
11047          if( nArg==3 || nArg==4 ){
11048            int ii = (int)integerValue(azArg[2]);
11049            sqlite3 *db;
11050            if( ii==0 && strcmp(azArg[2],"random")==0 ){
11051              sqlite3_randomness(sizeof(ii),&ii);
11052              printf("-- random seed: %d\n", ii);
11053            }
11054            if( nArg==3 ){
11055              db = 0;
11056            }else{
11057              db = p->db;
11058              /* Make sure the schema has been loaded */
11059              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
11060            }
11061            rc2 = sqlite3_test_control(testctrl, ii, db);
11062            isOk = 3;
11063          }
11064          break;
11065
11066        /* sqlite3_test_control(int, int) */
11067        case SQLITE_TESTCTRL_ASSERT:
11068        case SQLITE_TESTCTRL_ALWAYS:
11069          if( nArg==3 ){
11070            int opt = booleanValue(azArg[2]);
11071            rc2 = sqlite3_test_control(testctrl, opt);
11072            isOk = 1;
11073          }
11074          break;
11075
11076        /* sqlite3_test_control(int, int) */
11077        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
11078        case SQLITE_TESTCTRL_NEVER_CORRUPT:
11079          if( nArg==3 ){
11080            int opt = booleanValue(azArg[2]);
11081            rc2 = sqlite3_test_control(testctrl, opt);
11082            isOk = 3;
11083          }
11084          break;
11085
11086        /* sqlite3_test_control(sqlite3*) */
11087        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
11088          rc2 = sqlite3_test_control(testctrl, p->db);
11089          isOk = 3;
11090          break;
11091
11092        case SQLITE_TESTCTRL_IMPOSTER:
11093          if( nArg==5 ){
11094            rc2 = sqlite3_test_control(testctrl, p->db,
11095                          azArg[2],
11096                          integerValue(azArg[3]),
11097                          integerValue(azArg[4]));
11098            isOk = 3;
11099          }
11100          break;
11101
11102        case SQLITE_TESTCTRL_SEEK_COUNT: {
11103          u64 x = 0;
11104          rc2 = sqlite3_test_control(testctrl, p->db, &x);
11105          utf8_printf(p->out, "%llu\n", x);
11106          isOk = 3;
11107          break;
11108        }
11109
11110#ifdef YYCOVERAGE
11111        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
11112          if( nArg==2 ){
11113            sqlite3_test_control(testctrl, p->out);
11114            isOk = 3;
11115          }
11116          break;
11117        }
11118#endif
11119#ifdef SQLITE_DEBUG
11120        case SQLITE_TESTCTRL_TUNE: {
11121          if( nArg==4 ){
11122            int id = (int)integerValue(azArg[2]);
11123            int val = (int)integerValue(azArg[3]);
11124            sqlite3_test_control(testctrl, id, &val);
11125            isOk = 3;
11126          }else if( nArg==3 ){
11127            int id = (int)integerValue(azArg[2]);
11128            sqlite3_test_control(testctrl, -id, &rc2);
11129            isOk = 1;
11130          }else if( nArg==2 ){
11131            int id = 1;
11132            while(1){
11133              int val = 0;
11134              rc2 = sqlite3_test_control(testctrl, -id, &val);
11135              if( rc2!=SQLITE_OK ) break;
11136              if( id>1 ) utf8_printf(p->out, "  ");
11137              utf8_printf(p->out, "%d: %d", id, val);
11138              id++;
11139            }
11140            if( id>1 ) utf8_printf(p->out, "\n");
11141            isOk = 3;
11142          }
11143          break;
11144        }
11145#endif
11146        case SQLITE_TESTCTRL_SORTER_MMAP:
11147          if( nArg==3 ){
11148            int opt = (unsigned int)integerValue(azArg[2]);
11149            rc2 = sqlite3_test_control(testctrl, p->db, opt);
11150            isOk = 3;
11151          }
11152          break;
11153      }
11154    }
11155    if( isOk==0 && iCtrl>=0 ){
11156      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11157      rc = 1;
11158    }else if( isOk==1 ){
11159      raw_printf(p->out, "%d\n", rc2);
11160    }else if( isOk==2 ){
11161      raw_printf(p->out, "0x%08x\n", rc2);
11162    }
11163  }else
11164#endif /* !defined(SQLITE_UNTESTABLE) */
11165
11166  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
11167    open_db(p, 0);
11168    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11169  }else
11170
11171  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
11172    if( nArg==2 ){
11173      enableTimer = booleanValue(azArg[1]);
11174      if( enableTimer && !HAS_TIMER ){
11175        raw_printf(stderr, "Error: timer not available on this system.\n");
11176        enableTimer = 0;
11177      }
11178    }else{
11179      raw_printf(stderr, "Usage: .timer on|off\n");
11180      rc = 1;
11181    }
11182  }else
11183
11184#ifndef SQLITE_OMIT_TRACE
11185  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
11186    int mType = 0;
11187    int jj;
11188    open_db(p, 0);
11189    for(jj=1; jj<nArg; jj++){
11190      const char *z = azArg[jj];
11191      if( z[0]=='-' ){
11192        if( optionMatch(z, "expanded") ){
11193          p->eTraceType = SHELL_TRACE_EXPANDED;
11194        }
11195#ifdef SQLITE_ENABLE_NORMALIZE
11196        else if( optionMatch(z, "normalized") ){
11197          p->eTraceType = SHELL_TRACE_NORMALIZED;
11198        }
11199#endif
11200        else if( optionMatch(z, "plain") ){
11201          p->eTraceType = SHELL_TRACE_PLAIN;
11202        }
11203        else if( optionMatch(z, "profile") ){
11204          mType |= SQLITE_TRACE_PROFILE;
11205        }
11206        else if( optionMatch(z, "row") ){
11207          mType |= SQLITE_TRACE_ROW;
11208        }
11209        else if( optionMatch(z, "stmt") ){
11210          mType |= SQLITE_TRACE_STMT;
11211        }
11212        else if( optionMatch(z, "close") ){
11213          mType |= SQLITE_TRACE_CLOSE;
11214        }
11215        else {
11216          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
11217          rc = 1;
11218          goto meta_command_exit;
11219        }
11220      }else{
11221        output_file_close(p->traceOut);
11222        p->traceOut = output_file_open(azArg[1], 0);
11223      }
11224    }
11225    if( p->traceOut==0 ){
11226      sqlite3_trace_v2(p->db, 0, 0, 0);
11227    }else{
11228      if( mType==0 ) mType = SQLITE_TRACE_STMT;
11229      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11230    }
11231  }else
11232#endif /* !defined(SQLITE_OMIT_TRACE) */
11233
11234#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11235  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
11236    int ii;
11237    int lenOpt;
11238    char *zOpt;
11239    if( nArg<2 ){
11240      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
11241      rc = 1;
11242      goto meta_command_exit;
11243    }
11244    open_db(p, 0);
11245    zOpt = azArg[1];
11246    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11247    lenOpt = (int)strlen(zOpt);
11248    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11249      assert( azArg[nArg]==0 );
11250      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11251    }else{
11252      for(ii=1; ii<nArg; ii++){
11253        sqlite3_create_module(p->db, azArg[ii], 0, 0);
11254      }
11255    }
11256  }else
11257#endif
11258
11259#if SQLITE_USER_AUTHENTICATION
11260  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
11261    if( nArg<2 ){
11262      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
11263      rc = 1;
11264      goto meta_command_exit;
11265    }
11266    open_db(p, 0);
11267    if( strcmp(azArg[1],"login")==0 ){
11268      if( nArg!=4 ){
11269        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
11270        rc = 1;
11271        goto meta_command_exit;
11272      }
11273      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11274                                     strlen30(azArg[3]));
11275      if( rc ){
11276        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
11277        rc = 1;
11278      }
11279    }else if( strcmp(azArg[1],"add")==0 ){
11280      if( nArg!=5 ){
11281        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
11282        rc = 1;
11283        goto meta_command_exit;
11284      }
11285      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11286                            booleanValue(azArg[4]));
11287      if( rc ){
11288        raw_printf(stderr, "User-Add failed: %d\n", rc);
11289        rc = 1;
11290      }
11291    }else if( strcmp(azArg[1],"edit")==0 ){
11292      if( nArg!=5 ){
11293        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
11294        rc = 1;
11295        goto meta_command_exit;
11296      }
11297      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11298                              booleanValue(azArg[4]));
11299      if( rc ){
11300        raw_printf(stderr, "User-Edit failed: %d\n", rc);
11301        rc = 1;
11302      }
11303    }else if( strcmp(azArg[1],"delete")==0 ){
11304      if( nArg!=3 ){
11305        raw_printf(stderr, "Usage: .user delete USER\n");
11306        rc = 1;
11307        goto meta_command_exit;
11308      }
11309      rc = sqlite3_user_delete(p->db, azArg[2]);
11310      if( rc ){
11311        raw_printf(stderr, "User-Delete failed: %d\n", rc);
11312        rc = 1;
11313      }
11314    }else{
11315      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
11316      rc = 1;
11317      goto meta_command_exit;
11318    }
11319  }else
11320#endif /* SQLITE_USER_AUTHENTICATION */
11321
11322  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
11323    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
11324        sqlite3_libversion(), sqlite3_sourceid());
11325#if SQLITE_HAVE_ZLIB
11326    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
11327#endif
11328#define CTIMEOPT_VAL_(opt) #opt
11329#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11330#if defined(__clang__) && defined(__clang_major__)
11331    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
11332                    CTIMEOPT_VAL(__clang_minor__) "."
11333                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
11334#elif defined(_MSC_VER)
11335    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
11336#elif defined(__GNUC__) && defined(__VERSION__)
11337    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
11338#endif
11339  }else
11340
11341  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
11342    const char *zDbName = nArg==2 ? azArg[1] : "main";
11343    sqlite3_vfs *pVfs = 0;
11344    if( p->db ){
11345      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11346      if( pVfs ){
11347        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
11348        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11349        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11350        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11351      }
11352    }
11353  }else
11354
11355  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
11356    sqlite3_vfs *pVfs;
11357    sqlite3_vfs *pCurrent = 0;
11358    if( p->db ){
11359      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11360    }
11361    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11362      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
11363           pVfs==pCurrent ? "  <--- CURRENT" : "");
11364      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
11365      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
11366      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11367      if( pVfs->pNext ){
11368        raw_printf(p->out, "-----------------------------------\n");
11369      }
11370    }
11371  }else
11372
11373  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
11374    const char *zDbName = nArg==2 ? azArg[1] : "main";
11375    char *zVfsName = 0;
11376    if( p->db ){
11377      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11378      if( zVfsName ){
11379        utf8_printf(p->out, "%s\n", zVfsName);
11380        sqlite3_free(zVfsName);
11381      }
11382    }
11383  }else
11384
11385  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
11386    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11387    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11388  }else
11389
11390  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
11391    int j;
11392    assert( nArg<=ArraySize(azArg) );
11393    p->nWidth = nArg-1;
11394    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11395    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11396    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11397    for(j=1; j<nArg; j++){
11398      p->colWidth[j-1] = (int)integerValue(azArg[j]);
11399    }
11400  }else
11401
11402  {
11403    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
11404      " \"%s\". Enter \".help\" for help\n", azArg[0]);
11405    rc = 1;
11406  }
11407
11408meta_command_exit:
11409  if( p->outCount ){
11410    p->outCount--;
11411    if( p->outCount==0 ) output_reset(p);
11412  }
11413  p->bSafeMode = p->bSafeModePersist;
11414  return rc;
11415}
11416
11417/* Line scan result and intermediate states (supporting scan resumption)
11418*/
11419#ifndef CHAR_BIT
11420# define CHAR_BIT 8
11421#endif
11422typedef enum {
11423  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11424  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11425  QSS_Start = 0
11426} QuickScanState;
11427#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11428#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11429#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11430#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11431#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11432
11433/*
11434** Scan line for classification to guide shell's handling.
11435** The scan is resumable for subsequent lines when prior
11436** return values are passed as the 2nd argument.
11437*/
11438static QuickScanState quickscan(char *zLine, QuickScanState qss){
11439  char cin;
11440  char cWait = (char)qss; /* intentional narrowing loss */
11441  if( cWait==0 ){
11442  PlainScan:
11443    assert( cWait==0 );
11444    while( (cin = *zLine++)!=0 ){
11445      if( IsSpace(cin) )
11446        continue;
11447      switch (cin){
11448      case '-':
11449        if( *zLine!='-' )
11450          break;
11451        while((cin = *++zLine)!=0 )
11452          if( cin=='\n')
11453            goto PlainScan;
11454        return qss;
11455      case ';':
11456        qss |= QSS_EndingSemi;
11457        continue;
11458      case '/':
11459        if( *zLine=='*' ){
11460          ++zLine;
11461          cWait = '*';
11462          qss = QSS_SETV(qss, cWait);
11463          goto TermScan;
11464        }
11465        break;
11466      case '[':
11467        cin = ']';
11468        /* fall thru */
11469      case '`': case '\'': case '"':
11470        cWait = cin;
11471        qss = QSS_HasDark | cWait;
11472        goto TermScan;
11473      default:
11474        break;
11475      }
11476      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11477    }
11478  }else{
11479  TermScan:
11480    while( (cin = *zLine++)!=0 ){
11481      if( cin==cWait ){
11482        switch( cWait ){
11483        case '*':
11484          if( *zLine != '/' )
11485            continue;
11486          ++zLine;
11487          cWait = 0;
11488          qss = QSS_SETV(qss, 0);
11489          goto PlainScan;
11490        case '`': case '\'': case '"':
11491          if(*zLine==cWait){
11492            ++zLine;
11493            continue;
11494          }
11495          /* fall thru */
11496        case ']':
11497          cWait = 0;
11498          qss = QSS_SETV(qss, 0);
11499          goto PlainScan;
11500        default: assert(0);
11501        }
11502      }
11503    }
11504  }
11505  return qss;
11506}
11507
11508/*
11509** Return TRUE if the line typed in is an SQL command terminator other
11510** than a semi-colon.  The SQL Server style "go" command is understood
11511** as is the Oracle "/".
11512*/
11513static int line_is_command_terminator(char *zLine){
11514  while( IsSpace(zLine[0]) ){ zLine++; };
11515  if( zLine[0]=='/' )
11516    zLine += 1; /* Oracle */
11517  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
11518    zLine += 2; /* SQL Server */
11519  else
11520    return 0;
11521  return quickscan(zLine, QSS_Start)==QSS_Start;
11522}
11523
11524/*
11525** We need a default sqlite3_complete() implementation to use in case
11526** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
11527** any arbitrary text is a complete SQL statement.  This is not very
11528** user-friendly, but it does seem to work.
11529*/
11530#ifdef SQLITE_OMIT_COMPLETE
11531#define sqlite3_complete(x) 1
11532#endif
11533
11534/*
11535** Return true if zSql is a complete SQL statement.  Return false if it
11536** ends in the middle of a string literal or C-style comment.
11537*/
11538static int line_is_complete(char *zSql, int nSql){
11539  int rc;
11540  if( zSql==0 ) return 1;
11541  zSql[nSql] = ';';
11542  zSql[nSql+1] = 0;
11543  rc = sqlite3_complete(zSql);
11544  zSql[nSql] = 0;
11545  return rc;
11546}
11547
11548/*
11549** Run a single line of SQL.  Return the number of errors.
11550*/
11551static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
11552  int rc;
11553  char *zErrMsg = 0;
11554
11555  open_db(p, 0);
11556  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
11557  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11558  BEGIN_TIMER;
11559  rc = shell_exec(p, zSql, &zErrMsg);
11560  END_TIMER;
11561  if( rc || zErrMsg ){
11562    char zPrefix[100];
11563    const char *zErrorTail;
11564    const char *zErrorType;
11565    if( zErrMsg==0 ){
11566      zErrorType = "Error";
11567      zErrorTail = sqlite3_errmsg(p->db);
11568    }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){
11569      zErrorType = "Parse error";
11570      zErrorTail = &zErrMsg[12];
11571    }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){
11572      zErrorType = "Runtime error";
11573      zErrorTail = &zErrMsg[10];
11574    }else{
11575      zErrorType = "Error";
11576      zErrorTail = zErrMsg;
11577    }
11578    if( in!=0 || !stdin_is_interactive ){
11579      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11580                       "%s near line %d:", zErrorType, startline);
11581    }else{
11582      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11583    }
11584    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11585    sqlite3_free(zErrMsg);
11586    zErrMsg = 0;
11587    return 1;
11588  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11589    char zLineBuf[2000];
11590    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11591            "changes: %lld   total_changes: %lld",
11592            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11593    raw_printf(p->out, "%s\n", zLineBuf);
11594  }
11595  return 0;
11596}
11597
11598static void echo_group_input(ShellState *p, const char *zDo){
11599  if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11600}
11601
11602#ifdef SQLITE_SHELL_FIDDLE
11603/*
11604** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
11605** because we need the global shellState and cannot access it from that function
11606** without moving lots of code around (creating a larger/messier diff).
11607*/
11608static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11609  /* Parse the next line from shellState.wasm.zInput. */
11610  const char *zBegin = shellState.wasm.zPos;
11611  const char *z = zBegin;
11612  char *zLine = 0;
11613  i64 nZ = 0;
11614
11615  UNUSED_PARAMETER(in);
11616  UNUSED_PARAMETER(isContinuation);
11617  if(!z || !*z){
11618    return 0;
11619  }
11620  while(*z && isspace(*z)) ++z;
11621  zBegin = z;
11622  for(; *z && '\n'!=*z; ++nZ, ++z){}
11623  if(nZ>0 && '\r'==zBegin[nZ-1]){
11624    --nZ;
11625  }
11626  shellState.wasm.zPos = z;
11627  zLine = realloc(zPrior, nZ+1);
11628  shell_check_oom(zLine);
11629  memcpy(zLine, zBegin, nZ);
11630  zLine[nZ] = 0;
11631  return zLine;
11632}
11633#endif /* SQLITE_SHELL_FIDDLE */
11634
11635/*
11636** Read input from *in and process it.  If *in==0 then input
11637** is interactive - the user is typing it it.  Otherwise, input
11638** is coming from a file or device.  A prompt is issued and history
11639** is saved only if input is interactive.  An interrupt signal will
11640** cause this routine to exit immediately, unless input is interactive.
11641**
11642** Return the number of errors.
11643*/
11644static int process_input(ShellState *p){
11645  char *zLine = 0;          /* A single input line */
11646  char *zSql = 0;           /* Accumulated SQL text */
11647  i64 nLine;                /* Length of current line */
11648  i64 nSql = 0;             /* Bytes of zSql[] used */
11649  i64 nAlloc = 0;           /* Allocated zSql[] space */
11650  int rc;                   /* Error code */
11651  int errCnt = 0;           /* Number of errors seen */
11652  i64 startline = 0;        /* Line number for start of current input */
11653  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11654
11655  if( p->inputNesting==MAX_INPUT_NESTING ){
11656    /* This will be more informative in a later version. */
11657    utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11658                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11659    return 1;
11660  }
11661  ++p->inputNesting;
11662  p->lineno = 0;
11663  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11664    fflush(p->out);
11665    zLine = one_input_line(p->in, zLine, nSql>0);
11666    if( zLine==0 ){
11667      /* End of input */
11668      if( p->in==0 && stdin_is_interactive ) printf("\n");
11669      break;
11670    }
11671    if( seenInterrupt ){
11672      if( p->in!=0 ) break;
11673      seenInterrupt = 0;
11674    }
11675    p->lineno++;
11676    if( QSS_INPLAIN(qss)
11677        && line_is_command_terminator(zLine)
11678        && line_is_complete(zSql, nSql) ){
11679      memcpy(zLine,";",2);
11680    }
11681    qss = quickscan(zLine, qss);
11682    if( QSS_PLAINWHITE(qss) && nSql==0 ){
11683      /* Just swallow single-line whitespace */
11684      echo_group_input(p, zLine);
11685      qss = QSS_Start;
11686      continue;
11687    }
11688    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11689      echo_group_input(p, zLine);
11690      if( zLine[0]=='.' ){
11691        rc = do_meta_command(zLine, p);
11692        if( rc==2 ){ /* exit requested */
11693          break;
11694        }else if( rc ){
11695          errCnt++;
11696        }
11697      }
11698      qss = QSS_Start;
11699      continue;
11700    }
11701    /* No single-line dispositions remain; accumulate line(s). */
11702    nLine = strlen(zLine);
11703    if( nSql+nLine+2>=nAlloc ){
11704      /* Grow buffer by half-again increments when big. */
11705      nAlloc = nSql+(nSql>>1)+nLine+100;
11706      zSql = realloc(zSql, nAlloc);
11707      shell_check_oom(zSql);
11708    }
11709    if( nSql==0 ){
11710      i64 i;
11711      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11712      assert( nAlloc>0 && zSql!=0 );
11713      memcpy(zSql, zLine+i, nLine+1-i);
11714      startline = p->lineno;
11715      nSql = nLine-i;
11716    }else{
11717      zSql[nSql++] = '\n';
11718      memcpy(zSql+nSql, zLine, nLine+1);
11719      nSql += nLine;
11720    }
11721    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11722      echo_group_input(p, zSql);
11723      errCnt += runOneSqlLine(p, zSql, p->in, startline);
11724      nSql = 0;
11725      if( p->outCount ){
11726        output_reset(p);
11727        p->outCount = 0;
11728      }else{
11729        clearTempFile(p);
11730      }
11731      p->bSafeMode = p->bSafeModePersist;
11732      qss = QSS_Start;
11733    }else if( nSql && QSS_PLAINWHITE(qss) ){
11734      echo_group_input(p, zSql);
11735      nSql = 0;
11736      qss = QSS_Start;
11737    }
11738  }
11739  if( nSql ){
11740    /* This may be incomplete. Let the SQL parser deal with that. */
11741    echo_group_input(p, zSql);
11742    errCnt += runOneSqlLine(p, zSql, p->in, startline);
11743  }
11744  free(zSql);
11745  free(zLine);
11746  --p->inputNesting;
11747  return errCnt>0;
11748}
11749
11750/*
11751** Return a pathname which is the user's home directory.  A
11752** 0 return indicates an error of some kind.
11753*/
11754static char *find_home_dir(int clearFlag){
11755  static char *home_dir = NULL;
11756  if( clearFlag ){
11757    free(home_dir);
11758    home_dir = 0;
11759    return 0;
11760  }
11761  if( home_dir ) return home_dir;
11762
11763#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11764     && !defined(__RTP__) && !defined(_WRS_KERNEL)
11765  {
11766    struct passwd *pwent;
11767    uid_t uid = getuid();
11768    if( (pwent=getpwuid(uid)) != NULL) {
11769      home_dir = pwent->pw_dir;
11770    }
11771  }
11772#endif
11773
11774#if defined(_WIN32_WCE)
11775  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11776   */
11777  home_dir = "/";
11778#else
11779
11780#if defined(_WIN32) || defined(WIN32)
11781  if (!home_dir) {
11782    home_dir = getenv("USERPROFILE");
11783  }
11784#endif
11785
11786  if (!home_dir) {
11787    home_dir = getenv("HOME");
11788  }
11789
11790#if defined(_WIN32) || defined(WIN32)
11791  if (!home_dir) {
11792    char *zDrive, *zPath;
11793    int n;
11794    zDrive = getenv("HOMEDRIVE");
11795    zPath = getenv("HOMEPATH");
11796    if( zDrive && zPath ){
11797      n = strlen30(zDrive) + strlen30(zPath) + 1;
11798      home_dir = malloc( n );
11799      if( home_dir==0 ) return 0;
11800      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11801      return home_dir;
11802    }
11803    home_dir = "c:\\";
11804  }
11805#endif
11806
11807#endif /* !_WIN32_WCE */
11808
11809  if( home_dir ){
11810    i64 n = strlen(home_dir) + 1;
11811    char *z = malloc( n );
11812    if( z ) memcpy(z, home_dir, n);
11813    home_dir = z;
11814  }
11815
11816  return home_dir;
11817}
11818
11819/*
11820** Read input from the file given by sqliterc_override.  Or if that
11821** parameter is NULL, take input from ~/.sqliterc
11822**
11823** Returns the number of errors.
11824*/
11825static void process_sqliterc(
11826  ShellState *p,                  /* Configuration data */
11827  const char *sqliterc_override   /* Name of config file. NULL to use default */
11828){
11829  char *home_dir = NULL;
11830  const char *sqliterc = sqliterc_override;
11831  char *zBuf = 0;
11832  FILE *inSaved = p->in;
11833  int savedLineno = p->lineno;
11834
11835  if (sqliterc == NULL) {
11836    home_dir = find_home_dir(0);
11837    if( home_dir==0 ){
11838      raw_printf(stderr, "-- warning: cannot find home directory;"
11839                      " cannot read ~/.sqliterc\n");
11840      return;
11841    }
11842    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11843    shell_check_oom(zBuf);
11844    sqliterc = zBuf;
11845  }
11846  p->in = fopen(sqliterc,"rb");
11847  if( p->in ){
11848    if( stdin_is_interactive ){
11849      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11850    }
11851    if( process_input(p) && bail_on_error ) exit(1);
11852    fclose(p->in);
11853  }else if( sqliterc_override!=0 ){
11854    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11855    if( bail_on_error ) exit(1);
11856  }
11857  p->in = inSaved;
11858  p->lineno = savedLineno;
11859  sqlite3_free(zBuf);
11860}
11861
11862/*
11863** Show available command line options
11864*/
11865static const char zOptions[] =
11866#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11867  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11868#endif
11869  "   -append              append the database to the end of the file\n"
11870  "   -ascii               set output mode to 'ascii'\n"
11871  "   -bail                stop after hitting an error\n"
11872  "   -batch               force batch I/O\n"
11873  "   -box                 set output mode to 'box'\n"
11874  "   -column              set output mode to 'column'\n"
11875  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11876  "   -csv                 set output mode to 'csv'\n"
11877#if !defined(SQLITE_OMIT_DESERIALIZE)
11878  "   -deserialize         open the database using sqlite3_deserialize()\n"
11879#endif
11880  "   -echo                print inputs before execution\n"
11881  "   -init FILENAME       read/process named file\n"
11882  "   -[no]header          turn headers on or off\n"
11883#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11884  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11885#endif
11886  "   -help                show this message\n"
11887  "   -html                set output mode to HTML\n"
11888  "   -interactive         force interactive I/O\n"
11889  "   -json                set output mode to 'json'\n"
11890  "   -line                set output mode to 'line'\n"
11891  "   -list                set output mode to 'list'\n"
11892  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11893  "   -markdown            set output mode to 'markdown'\n"
11894#if !defined(SQLITE_OMIT_DESERIALIZE)
11895  "   -maxsize N           maximum size for a --deserialize database\n"
11896#endif
11897  "   -memtrace            trace all memory allocations and deallocations\n"
11898  "   -mmap N              default mmap size set to N\n"
11899#ifdef SQLITE_ENABLE_MULTIPLEX
11900  "   -multiplex           enable the multiplexor VFS\n"
11901#endif
11902  "   -newline SEP         set output row separator. Default: '\\n'\n"
11903  "   -nofollow            refuse to open symbolic links to database files\n"
11904  "   -nonce STRING        set the safe-mode escape nonce\n"
11905  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11906  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11907  "   -quote               set output mode to 'quote'\n"
11908  "   -readonly            open the database read-only\n"
11909  "   -safe                enable safe-mode\n"
11910  "   -separator SEP       set output column separator. Default: '|'\n"
11911#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11912  "   -sorterref SIZE      sorter references threshold size\n"
11913#endif
11914  "   -stats               print memory stats before each finalize\n"
11915  "   -table               set output mode to 'table'\n"
11916  "   -tabs                set output mode to 'tabs'\n"
11917  "   -version             show SQLite version\n"
11918  "   -vfs NAME            use NAME as the default VFS\n"
11919#ifdef SQLITE_ENABLE_VFSTRACE
11920  "   -vfstrace            enable tracing of all VFS calls\n"
11921#endif
11922#ifdef SQLITE_HAVE_ZLIB
11923  "   -zip                 open the file as a ZIP Archive\n"
11924#endif
11925;
11926static void usage(int showDetail){
11927  utf8_printf(stderr,
11928      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11929      "FILENAME is the name of an SQLite database. A new database is created\n"
11930      "if the file does not previously exist.\n", Argv0);
11931  if( showDetail ){
11932    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11933  }else{
11934    raw_printf(stderr, "Use the -help option for additional information\n");
11935  }
11936  exit(1);
11937}
11938
11939/*
11940** Internal check:  Verify that the SQLite is uninitialized.  Print a
11941** error message if it is initialized.
11942*/
11943static void verify_uninitialized(void){
11944  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11945    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11946                        " initialization.\n");
11947  }
11948}
11949
11950/*
11951** Initialize the state information in data
11952*/
11953static void main_init(ShellState *data) {
11954  memset(data, 0, sizeof(*data));
11955  data->normalMode = data->cMode = data->mode = MODE_List;
11956  data->autoExplain = 1;
11957  data->pAuxDb = &data->aAuxDb[0];
11958  memcpy(data->colSeparator,SEP_Column, 2);
11959  memcpy(data->rowSeparator,SEP_Row, 2);
11960  data->showHeader = 0;
11961  data->shellFlgs = SHFLG_Lookaside;
11962  verify_uninitialized();
11963  sqlite3_config(SQLITE_CONFIG_URI, 1);
11964  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11965  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11966  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11967  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11968}
11969
11970/*
11971** Output text to the console in a font that attracts extra attention.
11972*/
11973#ifdef _WIN32
11974static void printBold(const char *zText){
11975#if !SQLITE_OS_WINRT
11976  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11977  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11978  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11979  SetConsoleTextAttribute(out,
11980         FOREGROUND_RED|FOREGROUND_INTENSITY
11981  );
11982#endif
11983  printf("%s", zText);
11984#if !SQLITE_OS_WINRT
11985  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11986#endif
11987}
11988#else
11989static void printBold(const char *zText){
11990  printf("\033[1m%s\033[0m", zText);
11991}
11992#endif
11993
11994/*
11995** Get the argument to an --option.  Throw an error and die if no argument
11996** is available.
11997*/
11998static char *cmdline_option_value(int argc, char **argv, int i){
11999  if( i==argc ){
12000    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
12001            argv[0], argv[argc-1]);
12002    exit(1);
12003  }
12004  return argv[i];
12005}
12006
12007#ifndef SQLITE_SHELL_IS_UTF8
12008#  if (defined(_WIN32) || defined(WIN32)) \
12009   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12010#    define SQLITE_SHELL_IS_UTF8          (0)
12011#  else
12012#    define SQLITE_SHELL_IS_UTF8          (1)
12013#  endif
12014#endif
12015
12016#ifdef SQLITE_SHELL_FIDDLE
12017#  define main fiddle_main
12018#endif
12019
12020#if SQLITE_SHELL_IS_UTF8
12021int SQLITE_CDECL main(int argc, char **argv){
12022#else
12023int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
12024  char **argv;
12025#endif
12026#ifdef SQLITE_DEBUG
12027  sqlite3_int64 mem_main_enter = sqlite3_memory_used();
12028#endif
12029  char *zErrMsg = 0;
12030#ifdef SQLITE_SHELL_FIDDLE
12031#  define data shellState
12032#else
12033  ShellState data;
12034#endif
12035  const char *zInitFile = 0;
12036  int i;
12037  int rc = 0;
12038  int warnInmemoryDb = 0;
12039  int readStdin = 1;
12040  int nCmd = 0;
12041  char **azCmd = 0;
12042  const char *zVfs = 0;           /* Value of -vfs command-line option */
12043#if !SQLITE_SHELL_IS_UTF8
12044  char **argvToFree = 0;
12045  int argcToFree = 0;
12046#endif
12047
12048  setBinaryMode(stdin, 0);
12049  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12050#ifdef SQLITE_SHELL_FIDDLE
12051  stdin_is_interactive = 0;
12052  stdout_is_console = 1;
12053#else
12054  stdin_is_interactive = isatty(0);
12055  stdout_is_console = isatty(1);
12056#endif
12057
12058#if !defined(_WIN32_WCE)
12059  if( getenv("SQLITE_DEBUG_BREAK") ){
12060    if( isatty(0) && isatty(2) ){
12061      fprintf(stderr,
12062          "attach debugger to process %d and press any key to continue.\n",
12063          GETPID());
12064      fgetc(stdin);
12065    }else{
12066#if defined(_WIN32) || defined(WIN32)
12067#if SQLITE_OS_WINRT
12068      __debugbreak();
12069#else
12070      DebugBreak();
12071#endif
12072#elif defined(SIGTRAP)
12073      raise(SIGTRAP);
12074#endif
12075    }
12076  }
12077#endif
12078
12079#if USE_SYSTEM_SQLITE+0!=1
12080  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12081    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
12082            sqlite3_sourceid(), SQLITE_SOURCE_ID);
12083    exit(1);
12084  }
12085#endif
12086  main_init(&data);
12087
12088  /* On Windows, we must translate command-line arguments into UTF-8.
12089  ** The SQLite memory allocator subsystem has to be enabled in order to
12090  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
12091  ** subsequent sqlite3_config() calls will work.  So copy all results into
12092  ** memory that does not come from the SQLite memory allocator.
12093  */
12094#if !SQLITE_SHELL_IS_UTF8
12095  sqlite3_initialize();
12096  argvToFree = malloc(sizeof(argv[0])*argc*2);
12097  shell_check_oom(argvToFree);
12098  argcToFree = argc;
12099  argv = argvToFree + argc;
12100  for(i=0; i<argc; i++){
12101    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12102    i64 n;
12103    shell_check_oom(z);
12104    n = strlen(z);
12105    argv[i] = malloc( n+1 );
12106    shell_check_oom(argv[i]);
12107    memcpy(argv[i], z, n+1);
12108    argvToFree[i] = argv[i];
12109    sqlite3_free(z);
12110  }
12111  sqlite3_shutdown();
12112#endif
12113
12114  assert( argc>=1 && argv && argv[0] );
12115  Argv0 = argv[0];
12116
12117  /* Make sure we have a valid signal handler early, before anything
12118  ** else is done.
12119  */
12120#ifdef SIGINT
12121  signal(SIGINT, interrupt_handler);
12122#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12123  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
12124#endif
12125
12126#ifdef SQLITE_SHELL_DBNAME_PROC
12127  {
12128    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12129    ** of a C-function that will provide the name of the database file.  Use
12130    ** this compile-time option to embed this shell program in larger
12131    ** applications. */
12132    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12133    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12134    warnInmemoryDb = 0;
12135  }
12136#endif
12137
12138  /* Do an initial pass through the command-line argument to locate
12139  ** the name of the database file, the name of the initialization file,
12140  ** the size of the alternative malloc heap,
12141  ** and the first command to execute.
12142  */
12143  verify_uninitialized();
12144  for(i=1; i<argc; i++){
12145    char *z;
12146    z = argv[i];
12147    if( z[0]!='-' ){
12148      if( data.aAuxDb->zDbFilename==0 ){
12149        data.aAuxDb->zDbFilename = z;
12150      }else{
12151        /* Excesss arguments are interpreted as SQL (or dot-commands) and
12152        ** mean that nothing is read from stdin */
12153        readStdin = 0;
12154        nCmd++;
12155        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12156        shell_check_oom(azCmd);
12157        azCmd[nCmd-1] = z;
12158      }
12159    }
12160    if( z[1]=='-' ) z++;
12161    if( strcmp(z,"-separator")==0
12162     || strcmp(z,"-nullvalue")==0
12163     || strcmp(z,"-newline")==0
12164     || strcmp(z,"-cmd")==0
12165    ){
12166      (void)cmdline_option_value(argc, argv, ++i);
12167    }else if( strcmp(z,"-init")==0 ){
12168      zInitFile = cmdline_option_value(argc, argv, ++i);
12169    }else if( strcmp(z,"-batch")==0 ){
12170      /* Need to check for batch mode here to so we can avoid printing
12171      ** informational messages (like from process_sqliterc) before
12172      ** we do the actual processing of arguments later in a second pass.
12173      */
12174      stdin_is_interactive = 0;
12175    }else if( strcmp(z,"-heap")==0 ){
12176#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12177      const char *zSize;
12178      sqlite3_int64 szHeap;
12179
12180      zSize = cmdline_option_value(argc, argv, ++i);
12181      szHeap = integerValue(zSize);
12182      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12183      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12184#else
12185      (void)cmdline_option_value(argc, argv, ++i);
12186#endif
12187    }else if( strcmp(z,"-pagecache")==0 ){
12188      sqlite3_int64 n, sz;
12189      sz = integerValue(cmdline_option_value(argc,argv,++i));
12190      if( sz>70000 ) sz = 70000;
12191      if( sz<0 ) sz = 0;
12192      n = integerValue(cmdline_option_value(argc,argv,++i));
12193      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12194        n = 0xffffffffffffLL/sz;
12195      }
12196      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12197                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12198      data.shellFlgs |= SHFLG_Pagecache;
12199    }else if( strcmp(z,"-lookaside")==0 ){
12200      int n, sz;
12201      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12202      if( sz<0 ) sz = 0;
12203      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12204      if( n<0 ) n = 0;
12205      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12206      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12207    }else if( strcmp(z,"-threadsafe")==0 ){
12208      int n;
12209      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12210      switch( n ){
12211         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
12212         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
12213         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
12214      }
12215#ifdef SQLITE_ENABLE_VFSTRACE
12216    }else if( strcmp(z,"-vfstrace")==0 ){
12217      extern int vfstrace_register(
12218         const char *zTraceName,
12219         const char *zOldVfsName,
12220         int (*xOut)(const char*,void*),
12221         void *pOutArg,
12222         int makeDefault
12223      );
12224      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
12225#endif
12226#ifdef SQLITE_ENABLE_MULTIPLEX
12227    }else if( strcmp(z,"-multiplex")==0 ){
12228      extern int sqlite3_multiple_initialize(const char*,int);
12229      sqlite3_multiplex_initialize(0, 1);
12230#endif
12231    }else if( strcmp(z,"-mmap")==0 ){
12232      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12233      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12234#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12235    }else if( strcmp(z,"-sorterref")==0 ){
12236      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12237      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12238#endif
12239    }else if( strcmp(z,"-vfs")==0 ){
12240      zVfs = cmdline_option_value(argc, argv, ++i);
12241#ifdef SQLITE_HAVE_ZLIB
12242    }else if( strcmp(z,"-zip")==0 ){
12243      data.openMode = SHELL_OPEN_ZIPFILE;
12244#endif
12245    }else if( strcmp(z,"-append")==0 ){
12246      data.openMode = SHELL_OPEN_APPENDVFS;
12247#ifndef SQLITE_OMIT_DESERIALIZE
12248    }else if( strcmp(z,"-deserialize")==0 ){
12249      data.openMode = SHELL_OPEN_DESERIALIZE;
12250    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
12251      data.szMax = integerValue(argv[++i]);
12252#endif
12253    }else if( strcmp(z,"-readonly")==0 ){
12254      data.openMode = SHELL_OPEN_READONLY;
12255    }else if( strcmp(z,"-nofollow")==0 ){
12256      data.openFlags = SQLITE_OPEN_NOFOLLOW;
12257#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12258    }else if( strncmp(z, "-A",2)==0 ){
12259      /* All remaining command-line arguments are passed to the ".archive"
12260      ** command, so ignore them */
12261      break;
12262#endif
12263    }else if( strcmp(z, "-memtrace")==0 ){
12264      sqlite3MemTraceActivate(stderr);
12265    }else if( strcmp(z,"-bail")==0 ){
12266      bail_on_error = 1;
12267    }else if( strcmp(z,"-nonce")==0 ){
12268      free(data.zNonce);
12269      data.zNonce = strdup(argv[++i]);
12270    }else if( strcmp(z,"-safe")==0 ){
12271      /* no-op - catch this on the second pass */
12272    }
12273  }
12274  verify_uninitialized();
12275
12276
12277#ifdef SQLITE_SHELL_INIT_PROC
12278  {
12279    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12280    ** of a C-function that will perform initialization actions on SQLite that
12281    ** occur just before or after sqlite3_initialize(). Use this compile-time
12282    ** option to embed this shell program in larger applications. */
12283    extern void SQLITE_SHELL_INIT_PROC(void);
12284    SQLITE_SHELL_INIT_PROC();
12285  }
12286#else
12287  /* All the sqlite3_config() calls have now been made. So it is safe
12288  ** to call sqlite3_initialize() and process any command line -vfs option. */
12289  sqlite3_initialize();
12290#endif
12291
12292  if( zVfs ){
12293    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12294    if( pVfs ){
12295      sqlite3_vfs_register(pVfs, 1);
12296    }else{
12297      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
12298      exit(1);
12299    }
12300  }
12301
12302  if( data.pAuxDb->zDbFilename==0 ){
12303#ifndef SQLITE_OMIT_MEMORYDB
12304    data.pAuxDb->zDbFilename = ":memory:";
12305    warnInmemoryDb = argc==1;
12306#else
12307    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
12308    return 1;
12309#endif
12310  }
12311  data.out = stdout;
12312#ifndef SQLITE_SHELL_FIDDLE
12313  sqlite3_appendvfs_init(0,0,0);
12314#endif
12315
12316  /* Go ahead and open the database file if it already exists.  If the
12317  ** file does not exist, delay opening it.  This prevents empty database
12318  ** files from being created if a user mistypes the database name argument
12319  ** to the sqlite command-line tool.
12320  */
12321  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
12322    open_db(&data, 0);
12323  }
12324
12325  /* Process the initialization file if there is one.  If no -init option
12326  ** is given on the command line, look for a file named ~/.sqliterc and
12327  ** try to process it.
12328  */
12329  process_sqliterc(&data,zInitFile);
12330
12331  /* Make a second pass through the command-line argument and set
12332  ** options.  This second pass is delayed until after the initialization
12333  ** file is processed so that the command-line arguments will override
12334  ** settings in the initialization file.
12335  */
12336  for(i=1; i<argc; i++){
12337    char *z = argv[i];
12338    if( z[0]!='-' ) continue;
12339    if( z[1]=='-' ){ z++; }
12340    if( strcmp(z,"-init")==0 ){
12341      i++;
12342    }else if( strcmp(z,"-html")==0 ){
12343      data.mode = MODE_Html;
12344    }else if( strcmp(z,"-list")==0 ){
12345      data.mode = MODE_List;
12346    }else if( strcmp(z,"-quote")==0 ){
12347      data.mode = MODE_Quote;
12348      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
12349      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12350    }else if( strcmp(z,"-line")==0 ){
12351      data.mode = MODE_Line;
12352    }else if( strcmp(z,"-column")==0 ){
12353      data.mode = MODE_Column;
12354    }else if( strcmp(z,"-json")==0 ){
12355      data.mode = MODE_Json;
12356    }else if( strcmp(z,"-markdown")==0 ){
12357      data.mode = MODE_Markdown;
12358    }else if( strcmp(z,"-table")==0 ){
12359      data.mode = MODE_Table;
12360    }else if( strcmp(z,"-box")==0 ){
12361      data.mode = MODE_Box;
12362    }else if( strcmp(z,"-csv")==0 ){
12363      data.mode = MODE_Csv;
12364      memcpy(data.colSeparator,",",2);
12365#ifdef SQLITE_HAVE_ZLIB
12366    }else if( strcmp(z,"-zip")==0 ){
12367      data.openMode = SHELL_OPEN_ZIPFILE;
12368#endif
12369    }else if( strcmp(z,"-append")==0 ){
12370      data.openMode = SHELL_OPEN_APPENDVFS;
12371#ifndef SQLITE_OMIT_DESERIALIZE
12372    }else if( strcmp(z,"-deserialize")==0 ){
12373      data.openMode = SHELL_OPEN_DESERIALIZE;
12374    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
12375      data.szMax = integerValue(argv[++i]);
12376#endif
12377    }else if( strcmp(z,"-readonly")==0 ){
12378      data.openMode = SHELL_OPEN_READONLY;
12379    }else if( strcmp(z,"-nofollow")==0 ){
12380      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
12381    }else if( strcmp(z,"-ascii")==0 ){
12382      data.mode = MODE_Ascii;
12383      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
12384      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
12385    }else if( strcmp(z,"-tabs")==0 ){
12386      data.mode = MODE_List;
12387      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
12388      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12389    }else if( strcmp(z,"-separator")==0 ){
12390      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
12391                       "%s",cmdline_option_value(argc,argv,++i));
12392    }else if( strcmp(z,"-newline")==0 ){
12393      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
12394                       "%s",cmdline_option_value(argc,argv,++i));
12395    }else if( strcmp(z,"-nullvalue")==0 ){
12396      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
12397                       "%s",cmdline_option_value(argc,argv,++i));
12398    }else if( strcmp(z,"-header")==0 ){
12399      data.showHeader = 1;
12400      ShellSetFlag(&data, SHFLG_HeaderSet);
12401     }else if( strcmp(z,"-noheader")==0 ){
12402      data.showHeader = 0;
12403      ShellSetFlag(&data, SHFLG_HeaderSet);
12404    }else if( strcmp(z,"-echo")==0 ){
12405      ShellSetFlag(&data, SHFLG_Echo);
12406    }else if( strcmp(z,"-eqp")==0 ){
12407      data.autoEQP = AUTOEQP_on;
12408    }else if( strcmp(z,"-eqpfull")==0 ){
12409      data.autoEQP = AUTOEQP_full;
12410    }else if( strcmp(z,"-stats")==0 ){
12411      data.statsOn = 1;
12412    }else if( strcmp(z,"-scanstats")==0 ){
12413      data.scanstatsOn = 1;
12414    }else if( strcmp(z,"-backslash")==0 ){
12415      /* Undocumented command-line option: -backslash
12416      ** Causes C-style backslash escapes to be evaluated in SQL statements
12417      ** prior to sending the SQL into SQLite.  Useful for injecting
12418      ** crazy bytes in the middle of SQL statements for testing and debugging.
12419      */
12420      ShellSetFlag(&data, SHFLG_Backslash);
12421    }else if( strcmp(z,"-bail")==0 ){
12422      /* No-op.  The bail_on_error flag should already be set. */
12423    }else if( strcmp(z,"-version")==0 ){
12424      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
12425      return 0;
12426    }else if( strcmp(z,"-interactive")==0 ){
12427      stdin_is_interactive = 1;
12428    }else if( strcmp(z,"-batch")==0 ){
12429      stdin_is_interactive = 0;
12430    }else if( strcmp(z,"-heap")==0 ){
12431      i++;
12432    }else if( strcmp(z,"-pagecache")==0 ){
12433      i+=2;
12434    }else if( strcmp(z,"-lookaside")==0 ){
12435      i+=2;
12436    }else if( strcmp(z,"-threadsafe")==0 ){
12437      i+=2;
12438    }else if( strcmp(z,"-nonce")==0 ){
12439      i += 2;
12440    }else if( strcmp(z,"-mmap")==0 ){
12441      i++;
12442    }else if( strcmp(z,"-memtrace")==0 ){
12443      i++;
12444#ifdef SQLITE_ENABLE_SORTER_REFERENCES
12445    }else if( strcmp(z,"-sorterref")==0 ){
12446      i++;
12447#endif
12448    }else if( strcmp(z,"-vfs")==0 ){
12449      i++;
12450#ifdef SQLITE_ENABLE_VFSTRACE
12451    }else if( strcmp(z,"-vfstrace")==0 ){
12452      i++;
12453#endif
12454#ifdef SQLITE_ENABLE_MULTIPLEX
12455    }else if( strcmp(z,"-multiplex")==0 ){
12456      i++;
12457#endif
12458    }else if( strcmp(z,"-help")==0 ){
12459      usage(1);
12460    }else if( strcmp(z,"-cmd")==0 ){
12461      /* Run commands that follow -cmd first and separately from commands
12462      ** that simply appear on the command-line.  This seems goofy.  It would
12463      ** be better if all commands ran in the order that they appear.  But
12464      ** we retain the goofy behavior for historical compatibility. */
12465      if( i==argc-1 ) break;
12466      z = cmdline_option_value(argc,argv,++i);
12467      if( z[0]=='.' ){
12468        rc = do_meta_command(z, &data);
12469        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
12470      }else{
12471        open_db(&data, 0);
12472        rc = shell_exec(&data, z, &zErrMsg);
12473        if( zErrMsg!=0 ){
12474          utf8_printf(stderr,"Error: %s\n", zErrMsg);
12475          if( bail_on_error ) return rc!=0 ? rc : 1;
12476        }else if( rc!=0 ){
12477          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
12478          if( bail_on_error ) return rc;
12479        }
12480      }
12481#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12482    }else if( strncmp(z, "-A", 2)==0 ){
12483      if( nCmd>0 ){
12484        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
12485                            " with \"%s\"\n", z);
12486        return 1;
12487      }
12488      open_db(&data, OPEN_DB_ZIPFILE);
12489      if( z[2] ){
12490        argv[i] = &z[2];
12491        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
12492      }else{
12493        arDotCommand(&data, 1, argv+i, argc-i);
12494      }
12495      readStdin = 0;
12496      break;
12497#endif
12498    }else if( strcmp(z,"-safe")==0 ){
12499      data.bSafeMode = data.bSafeModePersist = 1;
12500    }else{
12501      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
12502      raw_printf(stderr,"Use -help for a list of options.\n");
12503      return 1;
12504    }
12505    data.cMode = data.mode;
12506  }
12507
12508  if( !readStdin ){
12509    /* Run all arguments that do not begin with '-' as if they were separate
12510    ** command-line inputs, except for the argToSkip argument which contains
12511    ** the database filename.
12512    */
12513    for(i=0; i<nCmd; i++){
12514      if( azCmd[i][0]=='.' ){
12515        rc = do_meta_command(azCmd[i], &data);
12516        if( rc ){
12517          free(azCmd);
12518          return rc==2 ? 0 : rc;
12519        }
12520      }else{
12521        open_db(&data, 0);
12522        rc = shell_exec(&data, azCmd[i], &zErrMsg);
12523        if( zErrMsg || rc ){
12524          if( zErrMsg!=0 ){
12525            utf8_printf(stderr,"Error: %s\n", zErrMsg);
12526          }else{
12527            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
12528          }
12529          sqlite3_free(zErrMsg);
12530          free(azCmd);
12531          return rc!=0 ? rc : 1;
12532        }
12533      }
12534    }
12535  }else{
12536    /* Run commands received from standard input
12537    */
12538    if( stdin_is_interactive ){
12539      char *zHome;
12540      char *zHistory;
12541      int nHistory;
12542      printf(
12543        "SQLite version %s %.19s\n" /*extra-version-info*/
12544        "Enter \".help\" for usage hints.\n",
12545        sqlite3_libversion(), sqlite3_sourceid()
12546      );
12547      if( warnInmemoryDb ){
12548        printf("Connected to a ");
12549        printBold("transient in-memory database");
12550        printf(".\nUse \".open FILENAME\" to reopen on a "
12551               "persistent database.\n");
12552      }
12553      zHistory = getenv("SQLITE_HISTORY");
12554      if( zHistory ){
12555        zHistory = strdup(zHistory);
12556      }else if( (zHome = find_home_dir(0))!=0 ){
12557        nHistory = strlen30(zHome) + 20;
12558        if( (zHistory = malloc(nHistory))!=0 ){
12559          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
12560        }
12561      }
12562      if( zHistory ){ shell_read_history(zHistory); }
12563#if HAVE_READLINE || HAVE_EDITLINE
12564      rl_attempted_completion_function = readline_completion;
12565#elif HAVE_LINENOISE
12566      linenoiseSetCompletionCallback(linenoise_completion);
12567#endif
12568      data.in = 0;
12569      rc = process_input(&data);
12570      if( zHistory ){
12571        shell_stifle_history(2000);
12572        shell_write_history(zHistory);
12573        free(zHistory);
12574      }
12575    }else{
12576      data.in = stdin;
12577      rc = process_input(&data);
12578    }
12579  }
12580#ifndef SQLITE_SHELL_FIDDLE
12581  /* In WASM mode we have to leave the db state in place so that
12582  ** client code can "push" SQL into it after this call returns. */
12583  free(azCmd);
12584  set_table_name(&data, 0);
12585  if( data.db ){
12586    session_close_all(&data, -1);
12587    close_db(data.db);
12588  }
12589  for(i=0; i<ArraySize(data.aAuxDb); i++){
12590    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12591    if( data.aAuxDb[i].db ){
12592      session_close_all(&data, i);
12593      close_db(data.aAuxDb[i].db);
12594    }
12595  }
12596  find_home_dir(1);
12597  output_reset(&data);
12598  data.doXdgOpen = 0;
12599  clearTempFile(&data);
12600#if !SQLITE_SHELL_IS_UTF8
12601  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12602  free(argvToFree);
12603#endif
12604  free(data.colWidth);
12605  free(data.zNonce);
12606  /* Clear the global data structure so that valgrind will detect memory
12607  ** leaks */
12608  memset(&data, 0, sizeof(data));
12609#ifdef SQLITE_DEBUG
12610  if( sqlite3_memory_used()>mem_main_enter ){
12611    utf8_printf(stderr, "Memory leaked: %u bytes\n",
12612                (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12613  }
12614#endif
12615#endif /* !SQLITE_SHELL_FIDDLE */
12616  return rc;
12617}
12618
12619
12620#ifdef SQLITE_SHELL_FIDDLE
12621/* Only for emcc experimentation purposes. */
12622int fiddle_experiment(int a,int b){
12623   return a + b;
12624}
12625
12626/* Only for emcc experimentation purposes.
12627
12628  Define this function in JS using:
12629
12630  emcc ... --js-library somefile.js
12631
12632  containing:
12633
12634mergeInto(LibraryManager.library, {
12635    my_foo: function(){
12636        console.debug("my_foo()",arguments);
12637    }
12638});
12639*/
12640/*extern void my_foo(sqlite3 *);*/
12641/* Only for emcc experimentation purposes. */
12642sqlite3 * fiddle_the_db(){
12643    printf("fiddle_the_db(%p)\n", (const void*)globalDb);
12644    /*my_foo(globalDb);*/
12645    return globalDb;
12646}
12647/* Only for emcc experimentation purposes. */
12648sqlite3 * fiddle_db_arg(sqlite3 *arg){
12649    printf("fiddle_db_arg(%p)\n", (const void*)arg);
12650    return arg;
12651}
12652
12653/*
12654** Intended to be called via a SharedWorker() while a separate
12655** SharedWorker() (which manages the wasm module) is performing work
12656** which should be interrupted. Unfortunately, SharedWorker is not
12657** portable enough to make real use of.
12658*/
12659void fiddle_interrupt(void){
12660  if(globalDb) sqlite3_interrupt(globalDb);
12661}
12662
12663/*
12664** Returns the filename of the given db name, assuming "main" if
12665** zDbName is NULL. Returns NULL if globalDb is not opened.
12666*/
12667const char * fiddle_db_filename(const char * zDbName){
12668    return globalDb
12669      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12670      : NULL;
12671}
12672
12673/*
12674** Closes, unlinks, and reopens the db using its current filename (or
12675** the default if the db is currently closed). It is assumed, for
12676** purposes of the fiddle build, that the file is in a transient
12677** virtual filesystem within the browser.
12678*/
12679void fiddle_reset_db(void){
12680  char *zFilename = 0;
12681  if(0==globalDb){
12682    shellState.pAuxDb->zDbFilename = "/fiddle.sqlite3";
12683  }else{
12684    zFilename =
12685      sqlite3_mprintf("%s", sqlite3_db_filename(globalDb, "main"));
12686    shell_check_oom(zFilename);
12687    close_db(globalDb);
12688    shellDeleteFile(zFilename);
12689    shellState.db = 0;
12690    shellState.pAuxDb->zDbFilename = zFilename;
12691  }
12692  open_db(&shellState, 0);
12693  sqlite3_free(zFilename);
12694}
12695
12696/*
12697** Trivial exportable function for emscripten. Needs to be exported using:
12698**
12699** emcc ..flags... -sEXPORTED_FUNCTIONS=_fiddle_exec -sEXPORTED_RUNTIME_METHODS=ccall,cwrap
12700**
12701** (Note the underscore before the function name.) It processes zSql
12702** as if it were input to the sqlite3 shell and redirects all output
12703** to the wasm binding.
12704*/
12705void fiddle_exec(const char * zSql){
12706  static int once = 0;
12707  int rc = 0;
12708  if(!once){
12709    /* Simulate an argv array for main() */
12710    static char * argv[] = {"fiddle",
12711                            "-bail",
12712                            "-safe"};
12713    rc = fiddle_main((int)(sizeof(argv)/sizeof(argv[0])), argv);
12714    once = rc ? -1 : 1;
12715    memset(&shellState.wasm, 0, sizeof(shellState.wasm));
12716    printf(
12717        "SQLite version %s %.19s\n" /*extra-version-info*/,
12718        sqlite3_libversion(), sqlite3_sourceid()
12719    );
12720    puts("WASM shell");
12721    puts("Enter \".help\" for usage hints.");
12722    if(once>0){
12723      fiddle_reset_db();
12724    }
12725    if(shellState.db){
12726      printf("Connected to %s.\n", fiddle_db_filename(NULL));
12727    }else{
12728      fprintf(stderr,"ERROR initializing db!\n");
12729      return;
12730    }
12731  }
12732  if(once<0){
12733    puts("DB init failed. Not executing SQL.");
12734  }else if(zSql && *zSql){
12735    shellState.wasm.zInput = zSql;
12736    shellState.wasm.zPos = zSql;
12737    process_input(&shellState);
12738    memset(&shellState.wasm, 0, sizeof(shellState.wasm));
12739  }
12740}
12741#endif /* SQLITE_SHELL_FIDDLE */
12742