xref: /sqlite-3.40.0/src/shell.c.in (revision 3d420836)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Optionally #include a user-defined header, whereby compilation options
22** may be set prior to where they take effect, but after platform setup.
23** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
24** file. Note that this macro has a like effect on sqlite3.c compilation.
25*/
26# define SHELL_STRINGIFY_(f) #f
27# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
28#ifdef SQLITE_CUSTOM_INCLUDE
29# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
30#endif
31
32/*
33** Determine if we are dealing with WinRT, which provides only a subset of
34** the full Win32 API.
35*/
36#if !defined(SQLITE_OS_WINRT)
37# define SQLITE_OS_WINRT 0
38#endif
39
40/*
41** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
42** somewhat for use as a WASM module in a web browser. This flag
43** should only be used when building the "fiddle" web application, as
44** the browser-mode build has much different user input requirements
45** and this build mode rewires the user input subsystem to account for
46** that.
47*/
48
49/*
50** Warning pragmas copied from msvc.h in the core.
51*/
52#if defined(_MSC_VER)
53#pragma warning(disable : 4054)
54#pragma warning(disable : 4055)
55#pragma warning(disable : 4100)
56#pragma warning(disable : 4127)
57#pragma warning(disable : 4130)
58#pragma warning(disable : 4152)
59#pragma warning(disable : 4189)
60#pragma warning(disable : 4206)
61#pragma warning(disable : 4210)
62#pragma warning(disable : 4232)
63#pragma warning(disable : 4244)
64#pragma warning(disable : 4305)
65#pragma warning(disable : 4306)
66#pragma warning(disable : 4702)
67#pragma warning(disable : 4706)
68#endif /* defined(_MSC_VER) */
69
70/*
71** No support for loadable extensions in VxWorks.
72*/
73#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
74# define SQLITE_OMIT_LOAD_EXTENSION 1
75#endif
76
77/*
78** Enable large-file support for fopen() and friends on unix.
79*/
80#ifndef SQLITE_DISABLE_LFS
81# define _LARGE_FILE       1
82# ifndef _FILE_OFFSET_BITS
83#   define _FILE_OFFSET_BITS 64
84# endif
85# define _LARGEFILE_SOURCE 1
86#endif
87
88#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
89/*
90** emcc requires _POSIX_SOURCE (or one of several similar defines)
91** to expose strdup().
92*/
93# define _POSIX_SOURCE
94#endif
95
96#include <stdlib.h>
97#include <string.h>
98#include <stdio.h>
99#include <assert.h>
100#include "sqlite3.h"
101typedef sqlite3_int64 i64;
102typedef sqlite3_uint64 u64;
103typedef unsigned char u8;
104#if SQLITE_USER_AUTHENTICATION
105# include "sqlite3userauth.h"
106#endif
107#include <ctype.h>
108#include <stdarg.h>
109
110#if !defined(_WIN32) && !defined(WIN32)
111# include <signal.h>
112# if !defined(__RTP__) && !defined(_WRS_KERNEL)
113#  include <pwd.h>
114# endif
115#endif
116#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
117# include <unistd.h>
118# include <dirent.h>
119# define GETPID getpid
120# if defined(__MINGW32__)
121#  define DIRENT dirent
122#  ifndef S_ISLNK
123#   define S_ISLNK(mode) (0)
124#  endif
125# endif
126#else
127# define GETPID (int)GetCurrentProcessId
128#endif
129#include <sys/types.h>
130#include <sys/stat.h>
131
132#if HAVE_READLINE
133# include <readline/readline.h>
134# include <readline/history.h>
135#endif
136
137#if HAVE_EDITLINE
138# include <editline/readline.h>
139#endif
140
141#if HAVE_EDITLINE || HAVE_READLINE
142
143# define shell_add_history(X) add_history(X)
144# define shell_read_history(X) read_history(X)
145# define shell_write_history(X) write_history(X)
146# define shell_stifle_history(X) stifle_history(X)
147# define shell_readline(X) readline(X)
148
149#elif HAVE_LINENOISE
150
151# include "linenoise.h"
152# define shell_add_history(X) linenoiseHistoryAdd(X)
153# define shell_read_history(X) linenoiseHistoryLoad(X)
154# define shell_write_history(X) linenoiseHistorySave(X)
155# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
156# define shell_readline(X) linenoise(X)
157
158#else
159
160# define shell_read_history(X)
161# define shell_write_history(X)
162# define shell_stifle_history(X)
163
164# define SHELL_USE_LOCAL_GETLINE 1
165#endif
166
167
168#if defined(_WIN32) || defined(WIN32)
169# if SQLITE_OS_WINRT
170#  define SQLITE_OMIT_POPEN 1
171# else
172#  include <io.h>
173#  include <fcntl.h>
174#  define isatty(h) _isatty(h)
175#  ifndef access
176#   define access(f,m) _access((f),(m))
177#  endif
178#  ifndef unlink
179#   define unlink _unlink
180#  endif
181#  ifndef strdup
182#   define strdup _strdup
183#  endif
184#  undef popen
185#  define popen _popen
186#  undef pclose
187#  define pclose _pclose
188# endif
189#else
190 /* Make sure isatty() has a prototype. */
191 extern int isatty(int);
192
193# if !defined(__RTP__) && !defined(_WRS_KERNEL)
194  /* popen and pclose are not C89 functions and so are
195  ** sometimes omitted from the <stdio.h> header */
196   extern FILE *popen(const char*,const char*);
197   extern int pclose(FILE*);
198# else
199#  define SQLITE_OMIT_POPEN 1
200# endif
201#endif
202
203#if defined(_WIN32_WCE)
204/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
205 * thus we always assume that we have a console. That can be
206 * overridden with the -batch command line option.
207 */
208#define isatty(x) 1
209#endif
210
211/* ctype macros that work with signed characters */
212#define IsSpace(X)  isspace((unsigned char)X)
213#define IsDigit(X)  isdigit((unsigned char)X)
214#define ToLower(X)  (char)tolower((unsigned char)X)
215
216#if defined(_WIN32) || defined(WIN32)
217#if SQLITE_OS_WINRT
218#include <intrin.h>
219#endif
220#include <windows.h>
221
222/* string conversion routines only needed on Win32 */
223extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
224extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
225extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
226extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
227#endif
228
229/* On Windows, we normally run with output mode of TEXT so that \n characters
230** are automatically translated into \r\n.  However, this behavior needs
231** to be disabled in some cases (ex: when generating CSV output and when
232** rendering quoted strings that contain \n characters).  The following
233** routines take care of that.
234*/
235#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
236static void setBinaryMode(FILE *file, int isOutput){
237  if( isOutput ) fflush(file);
238  _setmode(_fileno(file), _O_BINARY);
239}
240static void setTextMode(FILE *file, int isOutput){
241  if( isOutput ) fflush(file);
242  _setmode(_fileno(file), _O_TEXT);
243}
244#else
245# define setBinaryMode(X,Y)
246# define setTextMode(X,Y)
247#endif
248
249/* True if the timer is enabled */
250static int enableTimer = 0;
251
252/* A version of strcmp() that works with NULL values */
253static int cli_strcmp(const char *a, const char *b){
254  if( a==0 ) a = "";
255  if( b==0 ) b = "";
256  return strcmp(a,b);
257}
258static int cli_strncmp(const char *a, const char *b, size_t n){
259  if( a==0 ) a = "";
260  if( b==0 ) b = "";
261  return strncmp(a,b,n);
262}
263
264/* Return the current wall-clock time */
265static sqlite3_int64 timeOfDay(void){
266  static sqlite3_vfs *clockVfs = 0;
267  sqlite3_int64 t;
268  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
269  if( clockVfs==0 ) return 0;  /* Never actually happens */
270  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
271    clockVfs->xCurrentTimeInt64(clockVfs, &t);
272  }else{
273    double r;
274    clockVfs->xCurrentTime(clockVfs, &r);
275    t = (sqlite3_int64)(r*86400000.0);
276  }
277  return t;
278}
279
280#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
281#include <sys/time.h>
282#include <sys/resource.h>
283
284/* VxWorks does not support getrusage() as far as we can determine */
285#if defined(_WRS_KERNEL) || defined(__RTP__)
286struct rusage {
287  struct timeval ru_utime; /* user CPU time used */
288  struct timeval ru_stime; /* system CPU time used */
289};
290#define getrusage(A,B) memset(B,0,sizeof(*B))
291#endif
292
293/* Saved resource information for the beginning of an operation */
294static struct rusage sBegin;  /* CPU time at start */
295static sqlite3_int64 iBegin;  /* Wall-clock time at start */
296
297/*
298** Begin timing an operation
299*/
300static void beginTimer(void){
301  if( enableTimer ){
302    getrusage(RUSAGE_SELF, &sBegin);
303    iBegin = timeOfDay();
304  }
305}
306
307/* Return the difference of two time_structs in seconds */
308static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
309  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
310         (double)(pEnd->tv_sec - pStart->tv_sec);
311}
312
313/*
314** Print the timing results.
315*/
316static void endTimer(void){
317  if( enableTimer ){
318    sqlite3_int64 iEnd = timeOfDay();
319    struct rusage sEnd;
320    getrusage(RUSAGE_SELF, &sEnd);
321    printf("Run Time: real %.3f user %f sys %f\n",
322       (iEnd - iBegin)*0.001,
323       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
324       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
325  }
326}
327
328#define BEGIN_TIMER beginTimer()
329#define END_TIMER endTimer()
330#define HAS_TIMER 1
331
332#elif (defined(_WIN32) || defined(WIN32))
333
334/* Saved resource information for the beginning of an operation */
335static HANDLE hProcess;
336static FILETIME ftKernelBegin;
337static FILETIME ftUserBegin;
338static sqlite3_int64 ftWallBegin;
339typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
340                                    LPFILETIME, LPFILETIME);
341static GETPROCTIMES getProcessTimesAddr = NULL;
342
343/*
344** Check to see if we have timer support.  Return 1 if necessary
345** support found (or found previously).
346*/
347static int hasTimer(void){
348  if( getProcessTimesAddr ){
349    return 1;
350  } else {
351#if !SQLITE_OS_WINRT
352    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
353    ** versions. See if the version we are running on has it, and if it
354    ** does, save off a pointer to it and the current process handle.
355    */
356    hProcess = GetCurrentProcess();
357    if( hProcess ){
358      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
359      if( NULL != hinstLib ){
360        getProcessTimesAddr =
361            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
362        if( NULL != getProcessTimesAddr ){
363          return 1;
364        }
365        FreeLibrary(hinstLib);
366      }
367    }
368#endif
369  }
370  return 0;
371}
372
373/*
374** Begin timing an operation
375*/
376static void beginTimer(void){
377  if( enableTimer && getProcessTimesAddr ){
378    FILETIME ftCreation, ftExit;
379    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
380                        &ftKernelBegin,&ftUserBegin);
381    ftWallBegin = timeOfDay();
382  }
383}
384
385/* Return the difference of two FILETIME structs in seconds */
386static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
387  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
388  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
389  return (double) ((i64End - i64Start) / 10000000.0);
390}
391
392/*
393** Print the timing results.
394*/
395static void endTimer(void){
396  if( enableTimer && getProcessTimesAddr){
397    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
398    sqlite3_int64 ftWallEnd = timeOfDay();
399    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
400    printf("Run Time: real %.3f user %f sys %f\n",
401       (ftWallEnd - ftWallBegin)*0.001,
402       timeDiff(&ftUserBegin, &ftUserEnd),
403       timeDiff(&ftKernelBegin, &ftKernelEnd));
404  }
405}
406
407#define BEGIN_TIMER beginTimer()
408#define END_TIMER endTimer()
409#define HAS_TIMER hasTimer()
410
411#else
412#define BEGIN_TIMER
413#define END_TIMER
414#define HAS_TIMER 0
415#endif
416
417/*
418** Used to prevent warnings about unused parameters
419*/
420#define UNUSED_PARAMETER(x) (void)(x)
421
422/*
423** Number of elements in an array
424*/
425#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
426
427/*
428** If the following flag is set, then command execution stops
429** at an error if we are not interactive.
430*/
431static int bail_on_error = 0;
432
433/*
434** Threat stdin as an interactive input if the following variable
435** is true.  Otherwise, assume stdin is connected to a file or pipe.
436*/
437static int stdin_is_interactive = 1;
438
439/*
440** On Windows systems we have to know if standard output is a console
441** in order to translate UTF-8 into MBCS.  The following variable is
442** true if translation is required.
443*/
444static int stdout_is_console = 1;
445
446/*
447** The following is the open SQLite database.  We make a pointer
448** to this database a static variable so that it can be accessed
449** by the SIGINT handler to interrupt database processing.
450*/
451static sqlite3 *globalDb = 0;
452
453/*
454** True if an interrupt (Control-C) has been received.
455*/
456static volatile int seenInterrupt = 0;
457
458/*
459** This is the name of our program. It is set in main(), used
460** in a number of other places, mostly for error messages.
461*/
462static char *Argv0;
463
464/*
465** Prompt strings. Initialized in main. Settable with
466**   .prompt main continue
467*/
468static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
469static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
470
471/*
472** Render output like fprintf().  Except, if the output is going to the
473** console and if this is running on a Windows machine, translate the
474** output from UTF-8 into MBCS.
475*/
476#if defined(_WIN32) || defined(WIN32)
477void utf8_printf(FILE *out, const char *zFormat, ...){
478  va_list ap;
479  va_start(ap, zFormat);
480  if( stdout_is_console && (out==stdout || out==stderr) ){
481    char *z1 = sqlite3_vmprintf(zFormat, ap);
482    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
483    sqlite3_free(z1);
484    fputs(z2, out);
485    sqlite3_free(z2);
486  }else{
487    vfprintf(out, zFormat, ap);
488  }
489  va_end(ap);
490}
491#elif !defined(utf8_printf)
492# define utf8_printf fprintf
493#endif
494
495/*
496** Render output like fprintf().  This should not be used on anything that
497** includes string formatting (e.g. "%s").
498*/
499#if !defined(raw_printf)
500# define raw_printf fprintf
501#endif
502
503/* Indicate out-of-memory and exit. */
504static void shell_out_of_memory(void){
505  raw_printf(stderr,"Error: out of memory\n");
506  exit(1);
507}
508
509/* Check a pointer to see if it is NULL.  If it is NULL, exit with an
510** out-of-memory error.
511*/
512static void shell_check_oom(void *p){
513  if( p==0 ) shell_out_of_memory();
514}
515
516/*
517** Write I/O traces to the following stream.
518*/
519#ifdef SQLITE_ENABLE_IOTRACE
520static FILE *iotrace = 0;
521#endif
522
523/*
524** This routine works like printf in that its first argument is a
525** format string and subsequent arguments are values to be substituted
526** in place of % fields.  The result of formatting this string
527** is written to iotrace.
528*/
529#ifdef SQLITE_ENABLE_IOTRACE
530static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
531  va_list ap;
532  char *z;
533  if( iotrace==0 ) return;
534  va_start(ap, zFormat);
535  z = sqlite3_vmprintf(zFormat, ap);
536  va_end(ap);
537  utf8_printf(iotrace, "%s", z);
538  sqlite3_free(z);
539}
540#endif
541
542/*
543** Output string zUtf to stream pOut as w characters.  If w is negative,
544** then right-justify the text.  W is the width in UTF-8 characters, not
545** in bytes.  This is different from the %*.*s specification in printf
546** since with %*.*s the width is measured in bytes, not characters.
547*/
548static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
549  int i;
550  int n;
551  int aw = w<0 ? -w : w;
552  for(i=n=0; zUtf[i]; i++){
553    if( (zUtf[i]&0xc0)!=0x80 ){
554      n++;
555      if( n==aw ){
556        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
557        break;
558      }
559    }
560  }
561  if( n>=aw ){
562    utf8_printf(pOut, "%.*s", i, zUtf);
563  }else if( w<0 ){
564    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
565  }else{
566    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
567  }
568}
569
570
571/*
572** Determines if a string is a number of not.
573*/
574static int isNumber(const char *z, int *realnum){
575  if( *z=='-' || *z=='+' ) z++;
576  if( !IsDigit(*z) ){
577    return 0;
578  }
579  z++;
580  if( realnum ) *realnum = 0;
581  while( IsDigit(*z) ){ z++; }
582  if( *z=='.' ){
583    z++;
584    if( !IsDigit(*z) ) return 0;
585    while( IsDigit(*z) ){ z++; }
586    if( realnum ) *realnum = 1;
587  }
588  if( *z=='e' || *z=='E' ){
589    z++;
590    if( *z=='+' || *z=='-' ) z++;
591    if( !IsDigit(*z) ) return 0;
592    while( IsDigit(*z) ){ z++; }
593    if( realnum ) *realnum = 1;
594  }
595  return *z==0;
596}
597
598/*
599** Compute a string length that is limited to what can be stored in
600** lower 30 bits of a 32-bit signed integer.
601*/
602static int strlen30(const char *z){
603  const char *z2 = z;
604  while( *z2 ){ z2++; }
605  return 0x3fffffff & (int)(z2 - z);
606}
607
608/*
609** Return the length of a string in characters.  Multibyte UTF8 characters
610** count as a single character.
611*/
612static int strlenChar(const char *z){
613  int n = 0;
614  while( *z ){
615    if( (0xc0&*(z++))!=0x80 ) n++;
616  }
617  return n;
618}
619
620/*
621** Return open FILE * if zFile exists, can be opened for read
622** and is an ordinary file or a character stream source.
623** Otherwise return 0.
624*/
625static FILE * openChrSource(const char *zFile){
626#ifdef _WIN32
627  struct _stat x = {0};
628# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
629  /* On Windows, open first, then check the stream nature. This order
630  ** is necessary because _stat() and sibs, when checking a named pipe,
631  ** effectively break the pipe as its supplier sees it. */
632  FILE *rv = fopen(zFile, "rb");
633  if( rv==0 ) return 0;
634  if( _fstat(_fileno(rv), &x) != 0
635      || !STAT_CHR_SRC(x.st_mode)){
636    fclose(rv);
637    rv = 0;
638  }
639  return rv;
640#else
641  struct stat x = {0};
642  int rc = stat(zFile, &x);
643# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
644  if( rc!=0 ) return 0;
645  if( STAT_CHR_SRC(x.st_mode) ){
646    return fopen(zFile, "rb");
647  }else{
648    return 0;
649  }
650#endif
651#undef STAT_CHR_SRC
652}
653
654/*
655** This routine reads a line of text from FILE in, stores
656** the text in memory obtained from malloc() and returns a pointer
657** to the text.  NULL is returned at end of file, or if malloc()
658** fails.
659**
660** If zLine is not NULL then it is a malloced buffer returned from
661** a previous call to this routine that may be reused.
662*/
663static char *local_getline(char *zLine, FILE *in){
664  int nLine = zLine==0 ? 0 : 100;
665  int n = 0;
666
667  while( 1 ){
668    if( n+100>nLine ){
669      nLine = nLine*2 + 100;
670      zLine = realloc(zLine, nLine);
671      shell_check_oom(zLine);
672    }
673    if( fgets(&zLine[n], nLine - n, in)==0 ){
674      if( n==0 ){
675        free(zLine);
676        return 0;
677      }
678      zLine[n] = 0;
679      break;
680    }
681    while( zLine[n] ) n++;
682    if( n>0 && zLine[n-1]=='\n' ){
683      n--;
684      if( n>0 && zLine[n-1]=='\r' ) n--;
685      zLine[n] = 0;
686      break;
687    }
688  }
689#if defined(_WIN32) || defined(WIN32)
690  /* For interactive input on Windows systems, translate the
691  ** multi-byte characterset characters into UTF-8. */
692  if( stdin_is_interactive && in==stdin ){
693    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
694    if( zTrans ){
695      i64 nTrans = strlen(zTrans)+1;
696      if( nTrans>nLine ){
697        zLine = realloc(zLine, nTrans);
698        shell_check_oom(zLine);
699      }
700      memcpy(zLine, zTrans, nTrans);
701      sqlite3_free(zTrans);
702    }
703  }
704#endif /* defined(_WIN32) || defined(WIN32) */
705  return zLine;
706}
707
708/*
709** Retrieve a single line of input text.
710**
711** If in==0 then read from standard input and prompt before each line.
712** If isContinuation is true, then a continuation prompt is appropriate.
713** If isContinuation is zero, then the main prompt should be used.
714**
715** If zPrior is not NULL then it is a buffer from a prior call to this
716** routine that can be reused.
717**
718** The result is stored in space obtained from malloc() and must either
719** be freed by the caller or else passed back into this routine via the
720** zPrior argument for reuse.
721*/
722#ifndef SQLITE_SHELL_FIDDLE
723static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
724  char *zPrompt;
725  char *zResult;
726  if( in!=0 ){
727    zResult = local_getline(zPrior, in);
728  }else{
729    zPrompt = isContinuation ? continuePrompt : mainPrompt;
730#if SHELL_USE_LOCAL_GETLINE
731    printf("%s", zPrompt);
732    fflush(stdout);
733    zResult = local_getline(zPrior, stdin);
734#else
735    free(zPrior);
736    zResult = shell_readline(zPrompt);
737    if( zResult && *zResult ) shell_add_history(zResult);
738#endif
739  }
740  return zResult;
741}
742#endif /* !SQLITE_SHELL_FIDDLE */
743
744/*
745** Return the value of a hexadecimal digit.  Return -1 if the input
746** is not a hex digit.
747*/
748static int hexDigitValue(char c){
749  if( c>='0' && c<='9' ) return c - '0';
750  if( c>='a' && c<='f' ) return c - 'a' + 10;
751  if( c>='A' && c<='F' ) return c - 'A' + 10;
752  return -1;
753}
754
755/*
756** Interpret zArg as an integer value, possibly with suffixes.
757*/
758static sqlite3_int64 integerValue(const char *zArg){
759  sqlite3_int64 v = 0;
760  static const struct { char *zSuffix; int iMult; } aMult[] = {
761    { "KiB", 1024 },
762    { "MiB", 1024*1024 },
763    { "GiB", 1024*1024*1024 },
764    { "KB",  1000 },
765    { "MB",  1000000 },
766    { "GB",  1000000000 },
767    { "K",   1000 },
768    { "M",   1000000 },
769    { "G",   1000000000 },
770  };
771  int i;
772  int isNeg = 0;
773  if( zArg[0]=='-' ){
774    isNeg = 1;
775    zArg++;
776  }else if( zArg[0]=='+' ){
777    zArg++;
778  }
779  if( zArg[0]=='0' && zArg[1]=='x' ){
780    int x;
781    zArg += 2;
782    while( (x = hexDigitValue(zArg[0]))>=0 ){
783      v = (v<<4) + x;
784      zArg++;
785    }
786  }else{
787    while( IsDigit(zArg[0]) ){
788      v = v*10 + zArg[0] - '0';
789      zArg++;
790    }
791  }
792  for(i=0; i<ArraySize(aMult); i++){
793    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
794      v *= aMult[i].iMult;
795      break;
796    }
797  }
798  return isNeg? -v : v;
799}
800
801/*
802** A variable length string to which one can append text.
803*/
804typedef struct ShellText ShellText;
805struct ShellText {
806  char *z;
807  int n;
808  int nAlloc;
809};
810
811/*
812** Initialize and destroy a ShellText object
813*/
814static void initText(ShellText *p){
815  memset(p, 0, sizeof(*p));
816}
817static void freeText(ShellText *p){
818  free(p->z);
819  initText(p);
820}
821
822/* zIn is either a pointer to a NULL-terminated string in memory obtained
823** from malloc(), or a NULL pointer. The string pointed to by zAppend is
824** added to zIn, and the result returned in memory obtained from malloc().
825** zIn, if it was not NULL, is freed.
826**
827** If the third argument, quote, is not '\0', then it is used as a
828** quote character for zAppend.
829*/
830static void appendText(ShellText *p, const char *zAppend, char quote){
831  i64 len;
832  i64 i;
833  i64 nAppend = strlen30(zAppend);
834
835  len = nAppend+p->n+1;
836  if( quote ){
837    len += 2;
838    for(i=0; i<nAppend; i++){
839      if( zAppend[i]==quote ) len++;
840    }
841  }
842
843  if( p->z==0 || p->n+len>=p->nAlloc ){
844    p->nAlloc = p->nAlloc*2 + len + 20;
845    p->z = realloc(p->z, p->nAlloc);
846    shell_check_oom(p->z);
847  }
848
849  if( quote ){
850    char *zCsr = p->z+p->n;
851    *zCsr++ = quote;
852    for(i=0; i<nAppend; i++){
853      *zCsr++ = zAppend[i];
854      if( zAppend[i]==quote ) *zCsr++ = quote;
855    }
856    *zCsr++ = quote;
857    p->n = (int)(zCsr - p->z);
858    *zCsr = '\0';
859  }else{
860    memcpy(p->z+p->n, zAppend, nAppend);
861    p->n += nAppend;
862    p->z[p->n] = '\0';
863  }
864}
865
866/*
867** Attempt to determine if identifier zName needs to be quoted, either
868** because it contains non-alphanumeric characters, or because it is an
869** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
870** that quoting is required.
871**
872** Return '"' if quoting is required.  Return 0 if no quoting is required.
873*/
874static char quoteChar(const char *zName){
875  int i;
876  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
877  for(i=0; zName[i]; i++){
878    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
879  }
880  return sqlite3_keyword_check(zName, i) ? '"' : 0;
881}
882
883/*
884** Construct a fake object name and column list to describe the structure
885** of the view, virtual table, or table valued function zSchema.zName.
886*/
887static char *shellFakeSchema(
888  sqlite3 *db,            /* The database connection containing the vtab */
889  const char *zSchema,    /* Schema of the database holding the vtab */
890  const char *zName       /* The name of the virtual table */
891){
892  sqlite3_stmt *pStmt = 0;
893  char *zSql;
894  ShellText s;
895  char cQuote;
896  char *zDiv = "(";
897  int nRow = 0;
898
899  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
900                         zSchema ? zSchema : "main", zName);
901  shell_check_oom(zSql);
902  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
903  sqlite3_free(zSql);
904  initText(&s);
905  if( zSchema ){
906    cQuote = quoteChar(zSchema);
907    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
908    appendText(&s, zSchema, cQuote);
909    appendText(&s, ".", 0);
910  }
911  cQuote = quoteChar(zName);
912  appendText(&s, zName, cQuote);
913  while( sqlite3_step(pStmt)==SQLITE_ROW ){
914    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
915    nRow++;
916    appendText(&s, zDiv, 0);
917    zDiv = ",";
918    if( zCol==0 ) zCol = "";
919    cQuote = quoteChar(zCol);
920    appendText(&s, zCol, cQuote);
921  }
922  appendText(&s, ")", 0);
923  sqlite3_finalize(pStmt);
924  if( nRow==0 ){
925    freeText(&s);
926    s.z = 0;
927  }
928  return s.z;
929}
930
931/*
932** SQL function:  shell_module_schema(X)
933**
934** Return a fake schema for the table-valued function or eponymous virtual
935** table X.
936*/
937static void shellModuleSchema(
938  sqlite3_context *pCtx,
939  int nVal,
940  sqlite3_value **apVal
941){
942  const char *zName;
943  char *zFake;
944  UNUSED_PARAMETER(nVal);
945  zName = (const char*)sqlite3_value_text(apVal[0]);
946  zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
947  if( zFake ){
948    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
949                        -1, sqlite3_free);
950    free(zFake);
951  }
952}
953
954/*
955** SQL function:  shell_add_schema(S,X)
956**
957** Add the schema name X to the CREATE statement in S and return the result.
958** Examples:
959**
960**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
961**
962** Also works on
963**
964**    CREATE INDEX
965**    CREATE UNIQUE INDEX
966**    CREATE VIEW
967**    CREATE TRIGGER
968**    CREATE VIRTUAL TABLE
969**
970** This UDF is used by the .schema command to insert the schema name of
971** attached databases into the middle of the sqlite_schema.sql field.
972*/
973static void shellAddSchemaName(
974  sqlite3_context *pCtx,
975  int nVal,
976  sqlite3_value **apVal
977){
978  static const char *aPrefix[] = {
979     "TABLE",
980     "INDEX",
981     "UNIQUE INDEX",
982     "VIEW",
983     "TRIGGER",
984     "VIRTUAL TABLE"
985  };
986  int i = 0;
987  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
988  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
989  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
990  sqlite3 *db = sqlite3_context_db_handle(pCtx);
991  UNUSED_PARAMETER(nVal);
992  if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
993    for(i=0; i<ArraySize(aPrefix); i++){
994      int n = strlen30(aPrefix[i]);
995      if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
996        char *z = 0;
997        char *zFake = 0;
998        if( zSchema ){
999          char cQuote = quoteChar(zSchema);
1000          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1001            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1002          }else{
1003            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1004          }
1005        }
1006        if( zName
1007         && aPrefix[i][0]=='V'
1008         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1009        ){
1010          if( z==0 ){
1011            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1012          }else{
1013            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1014          }
1015          free(zFake);
1016        }
1017        if( z ){
1018          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1019          return;
1020        }
1021      }
1022    }
1023  }
1024  sqlite3_result_value(pCtx, apVal[0]);
1025}
1026
1027/*
1028** The source code for several run-time loadable extensions is inserted
1029** below by the ../tool/mkshellc.tcl script.  Before processing that included
1030** code, we need to override some macros to make the included program code
1031** work here in the middle of this regular program.
1032*/
1033#define SQLITE_EXTENSION_INIT1
1034#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1035
1036#if defined(_WIN32) && defined(_MSC_VER)
1037INCLUDE test_windirent.h
1038INCLUDE test_windirent.c
1039#define dirent DIRENT
1040#endif
1041INCLUDE ../ext/misc/memtrace.c
1042INCLUDE ../ext/misc/shathree.c
1043INCLUDE ../ext/misc/uint.c
1044INCLUDE ../ext/misc/decimal.c
1045INCLUDE ../ext/misc/ieee754.c
1046INCLUDE ../ext/misc/series.c
1047INCLUDE ../ext/misc/regexp.c
1048#ifndef SQLITE_SHELL_FIDDLE
1049INCLUDE ../ext/misc/fileio.c
1050INCLUDE ../ext/misc/completion.c
1051INCLUDE ../ext/misc/appendvfs.c
1052#endif
1053#ifdef SQLITE_HAVE_ZLIB
1054INCLUDE ../ext/misc/zipfile.c
1055INCLUDE ../ext/misc/sqlar.c
1056#endif
1057INCLUDE ../ext/expert/sqlite3expert.h
1058INCLUDE ../ext/expert/sqlite3expert.c
1059
1060#if !defined(SQLITE_OMIT_VIRTUALTABLE)  \
1061  && defined(SQLITE_ENABLE_DBPAGE_VTAB) \
1062  && !defined(SQLITE_SHELL_FIDDLE)
1063/* Including the recovery API causes the fiddle build to fail because
1064** recovery-related code invokes mutex- and UTF16-related APIs which
1065** we specifically disable in the wasm builds. */
1066#define SQLITE_SHELL_HAVE_RECOVER 1
1067#else
1068#define SQLITE_SHELL_HAVE_RECOVER 0
1069#endif
1070#if SQLITE_SHELL_HAVE_RECOVER
1071INCLUDE ../ext/recover/dbdata.c
1072INCLUDE ../ext/recover/sqlite3recover.h
1073INCLUDE ../ext/recover/sqlite3recover.c
1074#endif
1075
1076#if defined(SQLITE_ENABLE_SESSION)
1077/*
1078** State information for a single open session
1079*/
1080typedef struct OpenSession OpenSession;
1081struct OpenSession {
1082  char *zName;             /* Symbolic name for this session */
1083  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1084  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1085  sqlite3_session *p;      /* The open session */
1086};
1087#endif
1088
1089typedef struct ExpertInfo ExpertInfo;
1090struct ExpertInfo {
1091  sqlite3expert *pExpert;
1092  int bVerbose;
1093};
1094
1095/* A single line in the EQP output */
1096typedef struct EQPGraphRow EQPGraphRow;
1097struct EQPGraphRow {
1098  int iEqpId;           /* ID for this row */
1099  int iParentId;        /* ID of the parent row */
1100  EQPGraphRow *pNext;   /* Next row in sequence */
1101  char zText[1];        /* Text to display for this row */
1102};
1103
1104/* All EQP output is collected into an instance of the following */
1105typedef struct EQPGraph EQPGraph;
1106struct EQPGraph {
1107  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1108  EQPGraphRow *pLast;   /* Last element of the pRow list */
1109  char zPrefix[100];    /* Graph prefix */
1110};
1111
1112/* Parameters affecting columnar mode result display (defaulting together) */
1113typedef struct ColModeOpts {
1114  int iWrap;            /* In columnar modes, wrap lines reaching this limit */
1115  u8 bQuote;            /* Quote results for .mode box and table */
1116  u8 bWordWrap;         /* In columnar modes, wrap at word boundaries  */
1117} ColModeOpts;
1118#define ColModeOpts_default { 60, 0, 0 }
1119#define ColModeOpts_default_qbox { 60, 1, 0 }
1120
1121/*
1122** State information about the database connection is contained in an
1123** instance of the following structure.
1124*/
1125typedef struct ShellState ShellState;
1126struct ShellState {
1127  sqlite3 *db;           /* The database */
1128  u8 autoExplain;        /* Automatically turn on .explain mode */
1129  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1130  u8 autoEQPtest;        /* autoEQP is in test mode */
1131  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1132  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1133  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1134  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1135  u8 nEqpLevel;          /* Depth of the EQP output graph */
1136  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1137  u8 bSafeMode;          /* True to prohibit unsafe operations */
1138  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
1139  ColModeOpts cmOpts;    /* Option values affecting columnar mode output */
1140  unsigned statsOn;      /* True to display memory stats before each finalize */
1141  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1142  int inputNesting;      /* Track nesting level of .read and other redirects */
1143  int outCount;          /* Revert to stdout when reaching zero */
1144  int cnt;               /* Number of records displayed so far */
1145  int lineno;            /* Line number of last line read from in */
1146  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1147  FILE *in;              /* Read commands from this stream */
1148  FILE *out;             /* Write results here */
1149  FILE *traceOut;        /* Output for sqlite3_trace() */
1150  int nErr;              /* Number of errors seen */
1151  int mode;              /* An output mode setting */
1152  int modePrior;         /* Saved mode */
1153  int cMode;             /* temporary output mode for the current query */
1154  int normalMode;        /* Output mode before ".explain on" */
1155  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1156  int showHeader;        /* True to show column names in List or Column mode */
1157  int nCheck;            /* Number of ".check" commands run */
1158  unsigned nProgress;    /* Number of progress callbacks encountered */
1159  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1160  unsigned flgProgress;  /* Flags for the progress callback */
1161  unsigned shellFlgs;    /* Various flags */
1162  unsigned priorShFlgs;  /* Saved copy of flags */
1163  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1164  char *zDestTable;      /* Name of destination table when MODE_Insert */
1165  char *zTempFile;       /* Temporary file that might need deleting */
1166  char zTestcase[30];    /* Name of current test case */
1167  char colSeparator[20]; /* Column separator character for several modes */
1168  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1169  char colSepPrior[20];  /* Saved column separator */
1170  char rowSepPrior[20];  /* Saved row separator */
1171  int *colWidth;         /* Requested width of each column in columnar modes */
1172  int *actualWidth;      /* Actual width of each column */
1173  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1174  char nullValue[20];    /* The text to print when a NULL comes back from
1175                         ** the database */
1176  char outfile[FILENAME_MAX]; /* Filename for *out */
1177  sqlite3_stmt *pStmt;   /* Current statement if any. */
1178  FILE *pLog;            /* Write log output here */
1179  struct AuxDb {         /* Storage space for auxiliary database connections */
1180    sqlite3 *db;               /* Connection pointer */
1181    const char *zDbFilename;   /* Filename used to open the connection */
1182    char *zFreeOnClose;        /* Free this memory allocation on close */
1183#if defined(SQLITE_ENABLE_SESSION)
1184    int nSession;              /* Number of active sessions */
1185    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
1186#endif
1187  } aAuxDb[5],           /* Array of all database connections */
1188    *pAuxDb;             /* Currently active database connection */
1189  int *aiIndent;         /* Array of indents used in MODE_Explain */
1190  int nIndent;           /* Size of array aiIndent[] */
1191  int iIndent;           /* Index of current op in aiIndent[] */
1192  char *zNonce;          /* Nonce for temporary safe-mode excapes */
1193  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1194  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
1195#ifdef SQLITE_SHELL_FIDDLE
1196  struct {
1197    const char * zInput; /* Input string from wasm/JS proxy */
1198    const char * zPos;   /* Cursor pos into zInput */
1199    const char * zDefaultDbName; /* Default name for db file */
1200  } wasm;
1201#endif
1202};
1203
1204#ifdef SQLITE_SHELL_FIDDLE
1205static ShellState shellState;
1206#endif
1207
1208
1209/* Allowed values for ShellState.autoEQP
1210*/
1211#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1212#define AUTOEQP_on       1           /* Automatic EQP is on */
1213#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1214#define AUTOEQP_full     3           /* Show full EXPLAIN */
1215
1216/* Allowed values for ShellState.openMode
1217*/
1218#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1219#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1220#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1221#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1222#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1223#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1224#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1225
1226/* Allowed values for ShellState.eTraceType
1227*/
1228#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1229#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1230#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1231
1232/* Bits in the ShellState.flgProgress variable */
1233#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1234#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1235                                   ** callback limit is reached, and for each
1236                                   ** top-level SQL statement */
1237#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1238
1239/*
1240** These are the allowed shellFlgs values
1241*/
1242#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1243#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1244#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1245#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1246#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1247#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1248#define SHFLG_Echo           0x00000040 /* .echo on/off, or --echo setting */
1249#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1250#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1251#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1252
1253/*
1254** Macros for testing and setting shellFlgs
1255*/
1256#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1257#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1258#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1259
1260/*
1261** These are the allowed modes.
1262*/
1263#define MODE_Line     0  /* One column per line.  Blank line between records */
1264#define MODE_Column   1  /* One record per line in neat columns */
1265#define MODE_List     2  /* One record per line with a separator */
1266#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1267#define MODE_Html     4  /* Generate an XHTML table */
1268#define MODE_Insert   5  /* Generate SQL "insert" statements */
1269#define MODE_Quote    6  /* Quote values as for SQL */
1270#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1271#define MODE_Csv      8  /* Quote strings, numbers are plain */
1272#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1273#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1274#define MODE_Pretty  11  /* Pretty-print schemas */
1275#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1276#define MODE_Json    13  /* Output JSON */
1277#define MODE_Markdown 14 /* Markdown formatting */
1278#define MODE_Table   15  /* MySQL-style table formatting */
1279#define MODE_Box     16  /* Unicode box-drawing characters */
1280#define MODE_Count   17  /* Output only a count of the rows of output */
1281#define MODE_Off     18  /* No query output shown */
1282
1283static const char *modeDescr[] = {
1284  "line",
1285  "column",
1286  "list",
1287  "semi",
1288  "html",
1289  "insert",
1290  "quote",
1291  "tcl",
1292  "csv",
1293  "explain",
1294  "ascii",
1295  "prettyprint",
1296  "eqp",
1297  "json",
1298  "markdown",
1299  "table",
1300  "box",
1301  "count",
1302  "off"
1303};
1304
1305/*
1306** These are the column/row/line separators used by the various
1307** import/export modes.
1308*/
1309#define SEP_Column    "|"
1310#define SEP_Row       "\n"
1311#define SEP_Tab       "\t"
1312#define SEP_Space     " "
1313#define SEP_Comma     ","
1314#define SEP_CrLf      "\r\n"
1315#define SEP_Unit      "\x1F"
1316#define SEP_Record    "\x1E"
1317
1318/*
1319** Limit input nesting via .read or any other input redirect.
1320** It's not too expensive, so a generous allowance can be made.
1321*/
1322#define MAX_INPUT_NESTING 25
1323
1324/*
1325** A callback for the sqlite3_log() interface.
1326*/
1327static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1328  ShellState *p = (ShellState*)pArg;
1329  if( p->pLog==0 ) return;
1330  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1331  fflush(p->pLog);
1332}
1333
1334/*
1335** SQL function:  shell_putsnl(X)
1336**
1337** Write the text X to the screen (or whatever output is being directed)
1338** adding a newline at the end, and then return X.
1339*/
1340static void shellPutsFunc(
1341  sqlite3_context *pCtx,
1342  int nVal,
1343  sqlite3_value **apVal
1344){
1345  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1346  (void)nVal;
1347  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1348  sqlite3_result_value(pCtx, apVal[0]);
1349}
1350
1351/*
1352** If in safe mode, print an error message described by the arguments
1353** and exit immediately.
1354*/
1355static void failIfSafeMode(
1356  ShellState *p,
1357  const char *zErrMsg,
1358  ...
1359){
1360  if( p->bSafeMode ){
1361    va_list ap;
1362    char *zMsg;
1363    va_start(ap, zErrMsg);
1364    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1365    va_end(ap);
1366    raw_printf(stderr, "line %d: ", p->lineno);
1367    utf8_printf(stderr, "%s\n", zMsg);
1368    exit(1);
1369  }
1370}
1371
1372/*
1373** SQL function:   edit(VALUE)
1374**                 edit(VALUE,EDITOR)
1375**
1376** These steps:
1377**
1378**     (1) Write VALUE into a temporary file.
1379**     (2) Run program EDITOR on that temporary file.
1380**     (3) Read the temporary file back and return its content as the result.
1381**     (4) Delete the temporary file
1382**
1383** If the EDITOR argument is omitted, use the value in the VISUAL
1384** environment variable.  If still there is no EDITOR, through an error.
1385**
1386** Also throw an error if the EDITOR program returns a non-zero exit code.
1387*/
1388#ifndef SQLITE_NOHAVE_SYSTEM
1389static void editFunc(
1390  sqlite3_context *context,
1391  int argc,
1392  sqlite3_value **argv
1393){
1394  const char *zEditor;
1395  char *zTempFile = 0;
1396  sqlite3 *db;
1397  char *zCmd = 0;
1398  int bBin;
1399  int rc;
1400  int hasCRNL = 0;
1401  FILE *f = 0;
1402  sqlite3_int64 sz;
1403  sqlite3_int64 x;
1404  unsigned char *p = 0;
1405
1406  if( argc==2 ){
1407    zEditor = (const char*)sqlite3_value_text(argv[1]);
1408  }else{
1409    zEditor = getenv("VISUAL");
1410  }
1411  if( zEditor==0 ){
1412    sqlite3_result_error(context, "no editor for edit()", -1);
1413    return;
1414  }
1415  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1416    sqlite3_result_error(context, "NULL input to edit()", -1);
1417    return;
1418  }
1419  db = sqlite3_context_db_handle(context);
1420  zTempFile = 0;
1421  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1422  if( zTempFile==0 ){
1423    sqlite3_uint64 r = 0;
1424    sqlite3_randomness(sizeof(r), &r);
1425    zTempFile = sqlite3_mprintf("temp%llx", r);
1426    if( zTempFile==0 ){
1427      sqlite3_result_error_nomem(context);
1428      return;
1429    }
1430  }
1431  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1432  /* When writing the file to be edited, do \n to \r\n conversions on systems
1433  ** that want \r\n line endings */
1434  f = fopen(zTempFile, bBin ? "wb" : "w");
1435  if( f==0 ){
1436    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1437    goto edit_func_end;
1438  }
1439  sz = sqlite3_value_bytes(argv[0]);
1440  if( bBin ){
1441    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1442  }else{
1443    const char *z = (const char*)sqlite3_value_text(argv[0]);
1444    /* Remember whether or not the value originally contained \r\n */
1445    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1446    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1447  }
1448  fclose(f);
1449  f = 0;
1450  if( x!=sz ){
1451    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1452    goto edit_func_end;
1453  }
1454  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1455  if( zCmd==0 ){
1456    sqlite3_result_error_nomem(context);
1457    goto edit_func_end;
1458  }
1459  rc = system(zCmd);
1460  sqlite3_free(zCmd);
1461  if( rc ){
1462    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1463    goto edit_func_end;
1464  }
1465  f = fopen(zTempFile, "rb");
1466  if( f==0 ){
1467    sqlite3_result_error(context,
1468      "edit() cannot reopen temp file after edit", -1);
1469    goto edit_func_end;
1470  }
1471  fseek(f, 0, SEEK_END);
1472  sz = ftell(f);
1473  rewind(f);
1474  p = sqlite3_malloc64( sz+1 );
1475  if( p==0 ){
1476    sqlite3_result_error_nomem(context);
1477    goto edit_func_end;
1478  }
1479  x = fread(p, 1, (size_t)sz, f);
1480  fclose(f);
1481  f = 0;
1482  if( x!=sz ){
1483    sqlite3_result_error(context, "could not read back the whole file", -1);
1484    goto edit_func_end;
1485  }
1486  if( bBin ){
1487    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1488  }else{
1489    sqlite3_int64 i, j;
1490    if( hasCRNL ){
1491      /* If the original contains \r\n then do no conversions back to \n */
1492    }else{
1493      /* If the file did not originally contain \r\n then convert any new
1494      ** \r\n back into \n */
1495      for(i=j=0; i<sz; i++){
1496        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1497        p[j++] = p[i];
1498      }
1499      sz = j;
1500      p[sz] = 0;
1501    }
1502    sqlite3_result_text64(context, (const char*)p, sz,
1503                          sqlite3_free, SQLITE_UTF8);
1504  }
1505  p = 0;
1506
1507edit_func_end:
1508  if( f ) fclose(f);
1509  unlink(zTempFile);
1510  sqlite3_free(zTempFile);
1511  sqlite3_free(p);
1512}
1513#endif /* SQLITE_NOHAVE_SYSTEM */
1514
1515/*
1516** Save or restore the current output mode
1517*/
1518static void outputModePush(ShellState *p){
1519  p->modePrior = p->mode;
1520  p->priorShFlgs = p->shellFlgs;
1521  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1522  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1523}
1524static void outputModePop(ShellState *p){
1525  p->mode = p->modePrior;
1526  p->shellFlgs = p->priorShFlgs;
1527  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1528  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1529}
1530
1531/*
1532** Output the given string as a hex-encoded blob (eg. X'1234' )
1533*/
1534static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1535  int i;
1536  unsigned char *aBlob = (unsigned char*)pBlob;
1537
1538  char *zStr = sqlite3_malloc(nBlob*2 + 1);
1539  shell_check_oom(zStr);
1540
1541  for(i=0; i<nBlob; i++){
1542    static const char aHex[] = {
1543        '0', '1', '2', '3', '4', '5', '6', '7',
1544        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1545    };
1546    zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1547    zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1548  }
1549  zStr[i*2] = '\0';
1550
1551  raw_printf(out,"X'%s'", zStr);
1552  sqlite3_free(zStr);
1553}
1554
1555/*
1556** Find a string that is not found anywhere in z[].  Return a pointer
1557** to that string.
1558**
1559** Try to use zA and zB first.  If both of those are already found in z[]
1560** then make up some string and store it in the buffer zBuf.
1561*/
1562static const char *unused_string(
1563  const char *z,                    /* Result must not appear anywhere in z */
1564  const char *zA, const char *zB,   /* Try these first */
1565  char *zBuf                        /* Space to store a generated string */
1566){
1567  unsigned i = 0;
1568  if( strstr(z, zA)==0 ) return zA;
1569  if( strstr(z, zB)==0 ) return zB;
1570  do{
1571    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1572  }while( strstr(z,zBuf)!=0 );
1573  return zBuf;
1574}
1575
1576/*
1577** Output the given string as a quoted string using SQL quoting conventions.
1578**
1579** See also: output_quoted_escaped_string()
1580*/
1581static void output_quoted_string(FILE *out, const char *z){
1582  int i;
1583  char c;
1584  setBinaryMode(out, 1);
1585  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1586  if( c==0 ){
1587    utf8_printf(out,"'%s'",z);
1588  }else{
1589    raw_printf(out, "'");
1590    while( *z ){
1591      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1592      if( c=='\'' ) i++;
1593      if( i ){
1594        utf8_printf(out, "%.*s", i, z);
1595        z += i;
1596      }
1597      if( c=='\'' ){
1598        raw_printf(out, "'");
1599        continue;
1600      }
1601      if( c==0 ){
1602        break;
1603      }
1604      z++;
1605    }
1606    raw_printf(out, "'");
1607  }
1608  setTextMode(out, 1);
1609}
1610
1611/*
1612** Output the given string as a quoted string using SQL quoting conventions.
1613** Additionallly , escape the "\n" and "\r" characters so that they do not
1614** get corrupted by end-of-line translation facilities in some operating
1615** systems.
1616**
1617** This is like output_quoted_string() but with the addition of the \r\n
1618** escape mechanism.
1619*/
1620static void output_quoted_escaped_string(FILE *out, const char *z){
1621  int i;
1622  char c;
1623  setBinaryMode(out, 1);
1624  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1625  if( c==0 ){
1626    utf8_printf(out,"'%s'",z);
1627  }else{
1628    const char *zNL = 0;
1629    const char *zCR = 0;
1630    int nNL = 0;
1631    int nCR = 0;
1632    char zBuf1[20], zBuf2[20];
1633    for(i=0; z[i]; i++){
1634      if( z[i]=='\n' ) nNL++;
1635      if( z[i]=='\r' ) nCR++;
1636    }
1637    if( nNL ){
1638      raw_printf(out, "replace(");
1639      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1640    }
1641    if( nCR ){
1642      raw_printf(out, "replace(");
1643      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1644    }
1645    raw_printf(out, "'");
1646    while( *z ){
1647      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1648      if( c=='\'' ) i++;
1649      if( i ){
1650        utf8_printf(out, "%.*s", i, z);
1651        z += i;
1652      }
1653      if( c=='\'' ){
1654        raw_printf(out, "'");
1655        continue;
1656      }
1657      if( c==0 ){
1658        break;
1659      }
1660      z++;
1661      if( c=='\n' ){
1662        raw_printf(out, "%s", zNL);
1663        continue;
1664      }
1665      raw_printf(out, "%s", zCR);
1666    }
1667    raw_printf(out, "'");
1668    if( nCR ){
1669      raw_printf(out, ",'%s',char(13))", zCR);
1670    }
1671    if( nNL ){
1672      raw_printf(out, ",'%s',char(10))", zNL);
1673    }
1674  }
1675  setTextMode(out, 1);
1676}
1677
1678/*
1679** Output the given string as a quoted according to C or TCL quoting rules.
1680*/
1681static void output_c_string(FILE *out, const char *z){
1682  unsigned int c;
1683  fputc('"', out);
1684  while( (c = *(z++))!=0 ){
1685    if( c=='\\' ){
1686      fputc(c, out);
1687      fputc(c, out);
1688    }else if( c=='"' ){
1689      fputc('\\', out);
1690      fputc('"', out);
1691    }else if( c=='\t' ){
1692      fputc('\\', out);
1693      fputc('t', out);
1694    }else if( c=='\n' ){
1695      fputc('\\', out);
1696      fputc('n', out);
1697    }else if( c=='\r' ){
1698      fputc('\\', out);
1699      fputc('r', out);
1700    }else if( !isprint(c&0xff) ){
1701      raw_printf(out, "\\%03o", c&0xff);
1702    }else{
1703      fputc(c, out);
1704    }
1705  }
1706  fputc('"', out);
1707}
1708
1709/*
1710** Output the given string as a quoted according to JSON quoting rules.
1711*/
1712static void output_json_string(FILE *out, const char *z, i64 n){
1713  unsigned int c;
1714  if( n<0 ) n = strlen(z);
1715  fputc('"', out);
1716  while( n-- ){
1717    c = *(z++);
1718    if( c=='\\' || c=='"' ){
1719      fputc('\\', out);
1720      fputc(c, out);
1721    }else if( c<=0x1f ){
1722      fputc('\\', out);
1723      if( c=='\b' ){
1724        fputc('b', out);
1725      }else if( c=='\f' ){
1726        fputc('f', out);
1727      }else if( c=='\n' ){
1728        fputc('n', out);
1729      }else if( c=='\r' ){
1730        fputc('r', out);
1731      }else if( c=='\t' ){
1732        fputc('t', out);
1733      }else{
1734         raw_printf(out, "u%04x",c);
1735      }
1736    }else{
1737      fputc(c, out);
1738    }
1739  }
1740  fputc('"', out);
1741}
1742
1743/*
1744** Output the given string with characters that are special to
1745** HTML escaped.
1746*/
1747static void output_html_string(FILE *out, const char *z){
1748  int i;
1749  if( z==0 ) z = "";
1750  while( *z ){
1751    for(i=0;   z[i]
1752            && z[i]!='<'
1753            && z[i]!='&'
1754            && z[i]!='>'
1755            && z[i]!='\"'
1756            && z[i]!='\'';
1757        i++){}
1758    if( i>0 ){
1759      utf8_printf(out,"%.*s",i,z);
1760    }
1761    if( z[i]=='<' ){
1762      raw_printf(out,"&lt;");
1763    }else if( z[i]=='&' ){
1764      raw_printf(out,"&amp;");
1765    }else if( z[i]=='>' ){
1766      raw_printf(out,"&gt;");
1767    }else if( z[i]=='\"' ){
1768      raw_printf(out,"&quot;");
1769    }else if( z[i]=='\'' ){
1770      raw_printf(out,"&#39;");
1771    }else{
1772      break;
1773    }
1774    z += i + 1;
1775  }
1776}
1777
1778/*
1779** If a field contains any character identified by a 1 in the following
1780** array, then the string must be quoted for CSV.
1781*/
1782static const char needCsvQuote[] = {
1783  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1784  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1785  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1786  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1787  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1788  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1789  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1790  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1791  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1792  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1793  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1794  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1795  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1796  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1797  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1798  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1799};
1800
1801/*
1802** Output a single term of CSV.  Actually, p->colSeparator is used for
1803** the separator, which may or may not be a comma.  p->nullValue is
1804** the null value.  Strings are quoted if necessary.  The separator
1805** is only issued if bSep is true.
1806*/
1807static void output_csv(ShellState *p, const char *z, int bSep){
1808  FILE *out = p->out;
1809  if( z==0 ){
1810    utf8_printf(out,"%s",p->nullValue);
1811  }else{
1812    unsigned i;
1813    for(i=0; z[i]; i++){
1814      if( needCsvQuote[((unsigned char*)z)[i]] ){
1815        i = 0;
1816        break;
1817      }
1818    }
1819    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1820      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1821      shell_check_oom(zQuoted);
1822      utf8_printf(out, "%s", zQuoted);
1823      sqlite3_free(zQuoted);
1824    }else{
1825      utf8_printf(out, "%s", z);
1826    }
1827  }
1828  if( bSep ){
1829    utf8_printf(p->out, "%s", p->colSeparator);
1830  }
1831}
1832
1833/*
1834** This routine runs when the user presses Ctrl-C
1835*/
1836static void interrupt_handler(int NotUsed){
1837  UNUSED_PARAMETER(NotUsed);
1838  seenInterrupt++;
1839  if( seenInterrupt>2 ) exit(1);
1840  if( globalDb ) sqlite3_interrupt(globalDb);
1841}
1842
1843#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1844/*
1845** This routine runs for console events (e.g. Ctrl-C) on Win32
1846*/
1847static BOOL WINAPI ConsoleCtrlHandler(
1848  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1849){
1850  if( dwCtrlType==CTRL_C_EVENT ){
1851    interrupt_handler(0);
1852    return TRUE;
1853  }
1854  return FALSE;
1855}
1856#endif
1857
1858#ifndef SQLITE_OMIT_AUTHORIZATION
1859/*
1860** This authorizer runs in safe mode.
1861*/
1862static int safeModeAuth(
1863  void *pClientData,
1864  int op,
1865  const char *zA1,
1866  const char *zA2,
1867  const char *zA3,
1868  const char *zA4
1869){
1870  ShellState *p = (ShellState*)pClientData;
1871  static const char *azProhibitedFunctions[] = {
1872    "edit",
1873    "fts3_tokenizer",
1874    "load_extension",
1875    "readfile",
1876    "writefile",
1877    "zipfile",
1878    "zipfile_cds",
1879  };
1880  UNUSED_PARAMETER(zA2);
1881  UNUSED_PARAMETER(zA3);
1882  UNUSED_PARAMETER(zA4);
1883  switch( op ){
1884    case SQLITE_ATTACH: {
1885#ifndef SQLITE_SHELL_FIDDLE
1886      /* In WASM builds the filesystem is a virtual sandbox, so
1887      ** there's no harm in using ATTACH. */
1888      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1889#endif
1890      break;
1891    }
1892    case SQLITE_FUNCTION: {
1893      int i;
1894      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1895        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1896          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1897                         azProhibitedFunctions[i]);
1898        }
1899      }
1900      break;
1901    }
1902  }
1903  return SQLITE_OK;
1904}
1905
1906/*
1907** When the ".auth ON" is set, the following authorizer callback is
1908** invoked.  It always returns SQLITE_OK.
1909*/
1910static int shellAuth(
1911  void *pClientData,
1912  int op,
1913  const char *zA1,
1914  const char *zA2,
1915  const char *zA3,
1916  const char *zA4
1917){
1918  ShellState *p = (ShellState*)pClientData;
1919  static const char *azAction[] = { 0,
1920     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1921     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1922     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1923     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1924     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1925     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1926     "PRAGMA",               "READ",                 "SELECT",
1927     "TRANSACTION",          "UPDATE",               "ATTACH",
1928     "DETACH",               "ALTER_TABLE",          "REINDEX",
1929     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1930     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1931  };
1932  int i;
1933  const char *az[4];
1934  az[0] = zA1;
1935  az[1] = zA2;
1936  az[2] = zA3;
1937  az[3] = zA4;
1938  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1939  for(i=0; i<4; i++){
1940    raw_printf(p->out, " ");
1941    if( az[i] ){
1942      output_c_string(p->out, az[i]);
1943    }else{
1944      raw_printf(p->out, "NULL");
1945    }
1946  }
1947  raw_printf(p->out, "\n");
1948  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1949  return SQLITE_OK;
1950}
1951#endif
1952
1953/*
1954** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1955**
1956** This routine converts some CREATE TABLE statements for shadow tables
1957** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1958**
1959** If the schema statement in z[] contains a start-of-comment and if
1960** sqlite3_complete() returns false, try to terminate the comment before
1961** printing the result.  https://sqlite.org/forum/forumpost/d7be961c5c
1962*/
1963static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1964  char *zToFree = 0;
1965  if( z==0 ) return;
1966  if( zTail==0 ) return;
1967  if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
1968    const char *zOrig = z;
1969    static const char *azTerm[] = { "", "*/", "\n" };
1970    int i;
1971    for(i=0; i<ArraySize(azTerm); i++){
1972      char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
1973      if( sqlite3_complete(zNew) ){
1974        size_t n = strlen(zNew);
1975        zNew[n-1] = 0;
1976        zToFree = zNew;
1977        z = zNew;
1978        break;
1979      }
1980      sqlite3_free(zNew);
1981    }
1982  }
1983  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1984    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1985  }else{
1986    utf8_printf(out, "%s%s", z, zTail);
1987  }
1988  sqlite3_free(zToFree);
1989}
1990static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1991  char c = z[n];
1992  z[n] = 0;
1993  printSchemaLine(out, z, zTail);
1994  z[n] = c;
1995}
1996
1997/*
1998** Return true if string z[] has nothing but whitespace and comments to the
1999** end of the first line.
2000*/
2001static int wsToEol(const char *z){
2002  int i;
2003  for(i=0; z[i]; i++){
2004    if( z[i]=='\n' ) return 1;
2005    if( IsSpace(z[i]) ) continue;
2006    if( z[i]=='-' && z[i+1]=='-' ) return 1;
2007    return 0;
2008  }
2009  return 1;
2010}
2011
2012/*
2013** Add a new entry to the EXPLAIN QUERY PLAN data
2014*/
2015static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
2016  EQPGraphRow *pNew;
2017  i64 nText;
2018  if( zText==0 ) return;
2019  nText = strlen(zText);
2020  if( p->autoEQPtest ){
2021    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
2022  }
2023  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2024  shell_check_oom(pNew);
2025  pNew->iEqpId = iEqpId;
2026  pNew->iParentId = p2;
2027  memcpy(pNew->zText, zText, nText+1);
2028  pNew->pNext = 0;
2029  if( p->sGraph.pLast ){
2030    p->sGraph.pLast->pNext = pNew;
2031  }else{
2032    p->sGraph.pRow = pNew;
2033  }
2034  p->sGraph.pLast = pNew;
2035}
2036
2037/*
2038** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2039** in p->sGraph.
2040*/
2041static void eqp_reset(ShellState *p){
2042  EQPGraphRow *pRow, *pNext;
2043  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2044    pNext = pRow->pNext;
2045    sqlite3_free(pRow);
2046  }
2047  memset(&p->sGraph, 0, sizeof(p->sGraph));
2048}
2049
2050/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2051** pOld, or return the first such line if pOld is NULL
2052*/
2053static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2054  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2055  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2056  return pRow;
2057}
2058
2059/* Render a single level of the graph that has iEqpId as its parent.  Called
2060** recursively to render sublevels.
2061*/
2062static void eqp_render_level(ShellState *p, int iEqpId){
2063  EQPGraphRow *pRow, *pNext;
2064  i64 n = strlen(p->sGraph.zPrefix);
2065  char *z;
2066  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2067    pNext = eqp_next_row(p, iEqpId, pRow);
2068    z = pRow->zText;
2069    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2070                pNext ? "|--" : "`--", z);
2071    if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2072      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
2073      eqp_render_level(p, pRow->iEqpId);
2074      p->sGraph.zPrefix[n] = 0;
2075    }
2076  }
2077}
2078
2079/*
2080** Display and reset the EXPLAIN QUERY PLAN data
2081*/
2082static void eqp_render(ShellState *p){
2083  EQPGraphRow *pRow = p->sGraph.pRow;
2084  if( pRow ){
2085    if( pRow->zText[0]=='-' ){
2086      if( pRow->pNext==0 ){
2087        eqp_reset(p);
2088        return;
2089      }
2090      utf8_printf(p->out, "%s\n", pRow->zText+3);
2091      p->sGraph.pRow = pRow->pNext;
2092      sqlite3_free(pRow);
2093    }else{
2094      utf8_printf(p->out, "QUERY PLAN\n");
2095    }
2096    p->sGraph.zPrefix[0] = 0;
2097    eqp_render_level(p, 0);
2098    eqp_reset(p);
2099  }
2100}
2101
2102#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2103/*
2104** Progress handler callback.
2105*/
2106static int progress_handler(void *pClientData) {
2107  ShellState *p = (ShellState*)pClientData;
2108  p->nProgress++;
2109  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2110    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2111    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2112    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2113    return 1;
2114  }
2115  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2116    raw_printf(p->out, "Progress %u\n", p->nProgress);
2117  }
2118  return 0;
2119}
2120#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2121
2122/*
2123** Print N dashes
2124*/
2125static void print_dashes(FILE *out, int N){
2126  const char zDash[] = "--------------------------------------------------";
2127  const int nDash = sizeof(zDash) - 1;
2128  while( N>nDash ){
2129    fputs(zDash, out);
2130    N -= nDash;
2131  }
2132  raw_printf(out, "%.*s", N, zDash);
2133}
2134
2135/*
2136** Print a markdown or table-style row separator using ascii-art
2137*/
2138static void print_row_separator(
2139  ShellState *p,
2140  int nArg,
2141  const char *zSep
2142){
2143  int i;
2144  if( nArg>0 ){
2145    fputs(zSep, p->out);
2146    print_dashes(p->out, p->actualWidth[0]+2);
2147    for(i=1; i<nArg; i++){
2148      fputs(zSep, p->out);
2149      print_dashes(p->out, p->actualWidth[i]+2);
2150    }
2151    fputs(zSep, p->out);
2152  }
2153  fputs("\n", p->out);
2154}
2155
2156/*
2157** This is the callback routine that the shell
2158** invokes for each row of a query result.
2159*/
2160static int shell_callback(
2161  void *pArg,
2162  int nArg,        /* Number of result columns */
2163  char **azArg,    /* Text of each result column */
2164  char **azCol,    /* Column names */
2165  int *aiType      /* Column types.  Might be NULL */
2166){
2167  int i;
2168  ShellState *p = (ShellState*)pArg;
2169
2170  if( azArg==0 ) return 0;
2171  switch( p->cMode ){
2172    case MODE_Count:
2173    case MODE_Off: {
2174      break;
2175    }
2176    case MODE_Line: {
2177      int w = 5;
2178      if( azArg==0 ) break;
2179      for(i=0; i<nArg; i++){
2180        int len = strlen30(azCol[i] ? azCol[i] : "");
2181        if( len>w ) w = len;
2182      }
2183      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2184      for(i=0; i<nArg; i++){
2185        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2186                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2187      }
2188      break;
2189    }
2190    case MODE_Explain: {
2191      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2192      if( nArg>ArraySize(aExplainWidth) ){
2193        nArg = ArraySize(aExplainWidth);
2194      }
2195      if( p->cnt++==0 ){
2196        for(i=0; i<nArg; i++){
2197          int w = aExplainWidth[i];
2198          utf8_width_print(p->out, w, azCol[i]);
2199          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2200        }
2201        for(i=0; i<nArg; i++){
2202          int w = aExplainWidth[i];
2203          print_dashes(p->out, w);
2204          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2205        }
2206      }
2207      if( azArg==0 ) break;
2208      for(i=0; i<nArg; i++){
2209        int w = aExplainWidth[i];
2210        if( i==nArg-1 ) w = 0;
2211        if( azArg[i] && strlenChar(azArg[i])>w ){
2212          w = strlenChar(azArg[i]);
2213        }
2214        if( i==1 && p->aiIndent && p->pStmt ){
2215          if( p->iIndent<p->nIndent ){
2216            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2217          }
2218          p->iIndent++;
2219        }
2220        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2221        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2222      }
2223      break;
2224    }
2225    case MODE_Semi: {   /* .schema and .fullschema output */
2226      printSchemaLine(p->out, azArg[0], ";\n");
2227      break;
2228    }
2229    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2230      char *z;
2231      int j;
2232      int nParen = 0;
2233      char cEnd = 0;
2234      char c;
2235      int nLine = 0;
2236      assert( nArg==1 );
2237      if( azArg[0]==0 ) break;
2238      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2239       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2240      ){
2241        utf8_printf(p->out, "%s;\n", azArg[0]);
2242        break;
2243      }
2244      z = sqlite3_mprintf("%s", azArg[0]);
2245      shell_check_oom(z);
2246      j = 0;
2247      for(i=0; IsSpace(z[i]); i++){}
2248      for(; (c = z[i])!=0; i++){
2249        if( IsSpace(c) ){
2250          if( z[j-1]=='\r' ) z[j-1] = '\n';
2251          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2252        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2253          j--;
2254        }
2255        z[j++] = c;
2256      }
2257      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2258      z[j] = 0;
2259      if( strlen30(z)>=79 ){
2260        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2261          if( c==cEnd ){
2262            cEnd = 0;
2263          }else if( c=='"' || c=='\'' || c=='`' ){
2264            cEnd = c;
2265          }else if( c=='[' ){
2266            cEnd = ']';
2267          }else if( c=='-' && z[i+1]=='-' ){
2268            cEnd = '\n';
2269          }else if( c=='(' ){
2270            nParen++;
2271          }else if( c==')' ){
2272            nParen--;
2273            if( nLine>0 && nParen==0 && j>0 ){
2274              printSchemaLineN(p->out, z, j, "\n");
2275              j = 0;
2276            }
2277          }
2278          z[j++] = c;
2279          if( nParen==1 && cEnd==0
2280           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2281          ){
2282            if( c=='\n' ) j--;
2283            printSchemaLineN(p->out, z, j, "\n  ");
2284            j = 0;
2285            nLine++;
2286            while( IsSpace(z[i+1]) ){ i++; }
2287          }
2288        }
2289        z[j] = 0;
2290      }
2291      printSchemaLine(p->out, z, ";\n");
2292      sqlite3_free(z);
2293      break;
2294    }
2295    case MODE_List: {
2296      if( p->cnt++==0 && p->showHeader ){
2297        for(i=0; i<nArg; i++){
2298          utf8_printf(p->out,"%s%s",azCol[i],
2299                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2300        }
2301      }
2302      if( azArg==0 ) break;
2303      for(i=0; i<nArg; i++){
2304        char *z = azArg[i];
2305        if( z==0 ) z = p->nullValue;
2306        utf8_printf(p->out, "%s", z);
2307        if( i<nArg-1 ){
2308          utf8_printf(p->out, "%s", p->colSeparator);
2309        }else{
2310          utf8_printf(p->out, "%s", p->rowSeparator);
2311        }
2312      }
2313      break;
2314    }
2315    case MODE_Html: {
2316      if( p->cnt++==0 && p->showHeader ){
2317        raw_printf(p->out,"<TR>");
2318        for(i=0; i<nArg; i++){
2319          raw_printf(p->out,"<TH>");
2320          output_html_string(p->out, azCol[i]);
2321          raw_printf(p->out,"</TH>\n");
2322        }
2323        raw_printf(p->out,"</TR>\n");
2324      }
2325      if( azArg==0 ) break;
2326      raw_printf(p->out,"<TR>");
2327      for(i=0; i<nArg; i++){
2328        raw_printf(p->out,"<TD>");
2329        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2330        raw_printf(p->out,"</TD>\n");
2331      }
2332      raw_printf(p->out,"</TR>\n");
2333      break;
2334    }
2335    case MODE_Tcl: {
2336      if( p->cnt++==0 && p->showHeader ){
2337        for(i=0; i<nArg; i++){
2338          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2339          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2340        }
2341        utf8_printf(p->out, "%s", p->rowSeparator);
2342      }
2343      if( azArg==0 ) break;
2344      for(i=0; i<nArg; i++){
2345        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2346        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2347      }
2348      utf8_printf(p->out, "%s", p->rowSeparator);
2349      break;
2350    }
2351    case MODE_Csv: {
2352      setBinaryMode(p->out, 1);
2353      if( p->cnt++==0 && p->showHeader ){
2354        for(i=0; i<nArg; i++){
2355          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2356        }
2357        utf8_printf(p->out, "%s", p->rowSeparator);
2358      }
2359      if( nArg>0 ){
2360        for(i=0; i<nArg; i++){
2361          output_csv(p, azArg[i], i<nArg-1);
2362        }
2363        utf8_printf(p->out, "%s", p->rowSeparator);
2364      }
2365      setTextMode(p->out, 1);
2366      break;
2367    }
2368    case MODE_Insert: {
2369      if( azArg==0 ) break;
2370      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2371      if( p->showHeader ){
2372        raw_printf(p->out,"(");
2373        for(i=0; i<nArg; i++){
2374          if( i>0 ) raw_printf(p->out, ",");
2375          if( quoteChar(azCol[i]) ){
2376            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2377            shell_check_oom(z);
2378            utf8_printf(p->out, "%s", z);
2379            sqlite3_free(z);
2380          }else{
2381            raw_printf(p->out, "%s", azCol[i]);
2382          }
2383        }
2384        raw_printf(p->out,")");
2385      }
2386      p->cnt++;
2387      for(i=0; i<nArg; i++){
2388        raw_printf(p->out, i>0 ? "," : " VALUES(");
2389        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2390          utf8_printf(p->out,"NULL");
2391        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2392          if( ShellHasFlag(p, SHFLG_Newlines) ){
2393            output_quoted_string(p->out, azArg[i]);
2394          }else{
2395            output_quoted_escaped_string(p->out, azArg[i]);
2396          }
2397        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2398          utf8_printf(p->out,"%s", azArg[i]);
2399        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2400          char z[50];
2401          double r = sqlite3_column_double(p->pStmt, i);
2402          sqlite3_uint64 ur;
2403          memcpy(&ur,&r,sizeof(r));
2404          if( ur==0x7ff0000000000000LL ){
2405            raw_printf(p->out, "1e999");
2406          }else if( ur==0xfff0000000000000LL ){
2407            raw_printf(p->out, "-1e999");
2408          }else{
2409            sqlite3_int64 ir = (sqlite3_int64)r;
2410            if( r==(double)ir ){
2411              sqlite3_snprintf(50,z,"%lld.0", ir);
2412            }else{
2413              sqlite3_snprintf(50,z,"%!.20g", r);
2414            }
2415            raw_printf(p->out, "%s", z);
2416          }
2417        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2418          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2419          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2420          output_hex_blob(p->out, pBlob, nBlob);
2421        }else if( isNumber(azArg[i], 0) ){
2422          utf8_printf(p->out,"%s", azArg[i]);
2423        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2424          output_quoted_string(p->out, azArg[i]);
2425        }else{
2426          output_quoted_escaped_string(p->out, azArg[i]);
2427        }
2428      }
2429      raw_printf(p->out,");\n");
2430      break;
2431    }
2432    case MODE_Json: {
2433      if( azArg==0 ) break;
2434      if( p->cnt==0 ){
2435        fputs("[{", p->out);
2436      }else{
2437        fputs(",\n{", p->out);
2438      }
2439      p->cnt++;
2440      for(i=0; i<nArg; i++){
2441        output_json_string(p->out, azCol[i], -1);
2442        putc(':', p->out);
2443        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2444          fputs("null",p->out);
2445        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2446          char z[50];
2447          double r = sqlite3_column_double(p->pStmt, i);
2448          sqlite3_uint64 ur;
2449          memcpy(&ur,&r,sizeof(r));
2450          if( ur==0x7ff0000000000000LL ){
2451            raw_printf(p->out, "1e999");
2452          }else if( ur==0xfff0000000000000LL ){
2453            raw_printf(p->out, "-1e999");
2454          }else{
2455            sqlite3_snprintf(50,z,"%!.20g", r);
2456            raw_printf(p->out, "%s", z);
2457          }
2458        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2459          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2460          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2461          output_json_string(p->out, pBlob, nBlob);
2462        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2463          output_json_string(p->out, azArg[i], -1);
2464        }else{
2465          utf8_printf(p->out,"%s", azArg[i]);
2466        }
2467        if( i<nArg-1 ){
2468          putc(',', p->out);
2469        }
2470      }
2471      putc('}', p->out);
2472      break;
2473    }
2474    case MODE_Quote: {
2475      if( azArg==0 ) break;
2476      if( p->cnt==0 && p->showHeader ){
2477        for(i=0; i<nArg; i++){
2478          if( i>0 ) fputs(p->colSeparator, p->out);
2479          output_quoted_string(p->out, azCol[i]);
2480        }
2481        fputs(p->rowSeparator, p->out);
2482      }
2483      p->cnt++;
2484      for(i=0; i<nArg; i++){
2485        if( i>0 ) fputs(p->colSeparator, p->out);
2486        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2487          utf8_printf(p->out,"NULL");
2488        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2489          output_quoted_string(p->out, azArg[i]);
2490        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2491          utf8_printf(p->out,"%s", azArg[i]);
2492        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2493          char z[50];
2494          double r = sqlite3_column_double(p->pStmt, i);
2495          sqlite3_snprintf(50,z,"%!.20g", r);
2496          raw_printf(p->out, "%s", z);
2497        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2498          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2499          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2500          output_hex_blob(p->out, pBlob, nBlob);
2501        }else if( isNumber(azArg[i], 0) ){
2502          utf8_printf(p->out,"%s", azArg[i]);
2503        }else{
2504          output_quoted_string(p->out, azArg[i]);
2505        }
2506      }
2507      fputs(p->rowSeparator, p->out);
2508      break;
2509    }
2510    case MODE_Ascii: {
2511      if( p->cnt++==0 && p->showHeader ){
2512        for(i=0; i<nArg; i++){
2513          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2514          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2515        }
2516        utf8_printf(p->out, "%s", p->rowSeparator);
2517      }
2518      if( azArg==0 ) break;
2519      for(i=0; i<nArg; i++){
2520        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2521        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2522      }
2523      utf8_printf(p->out, "%s", p->rowSeparator);
2524      break;
2525    }
2526    case MODE_EQP: {
2527      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2528      break;
2529    }
2530  }
2531  return 0;
2532}
2533
2534/*
2535** This is the callback routine that the SQLite library
2536** invokes for each row of a query result.
2537*/
2538static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2539  /* since we don't have type info, call the shell_callback with a NULL value */
2540  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2541}
2542
2543/*
2544** This is the callback routine from sqlite3_exec() that appends all
2545** output onto the end of a ShellText object.
2546*/
2547static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2548  ShellText *p = (ShellText*)pArg;
2549  int i;
2550  UNUSED_PARAMETER(az);
2551  if( azArg==0 ) return 0;
2552  if( p->n ) appendText(p, "|", 0);
2553  for(i=0; i<nArg; i++){
2554    if( i ) appendText(p, ",", 0);
2555    if( azArg[i] ) appendText(p, azArg[i], 0);
2556  }
2557  return 0;
2558}
2559
2560/*
2561** Generate an appropriate SELFTEST table in the main database.
2562*/
2563static void createSelftestTable(ShellState *p){
2564  char *zErrMsg = 0;
2565  sqlite3_exec(p->db,
2566    "SAVEPOINT selftest_init;\n"
2567    "CREATE TABLE IF NOT EXISTS selftest(\n"
2568    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2569    "  op TEXT,\n"                   /* Operator:  memo run */
2570    "  cmd TEXT,\n"                  /* Command text */
2571    "  ans TEXT\n"                   /* Desired answer */
2572    ");"
2573    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2574    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2575    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2576    "         'memo','Tests generated by --init');\n"
2577    "INSERT INTO [_shell$self]\n"
2578    "  SELECT 'run',\n"
2579    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2580                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2581    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2582                          "FROM sqlite_schema ORDER BY 2',224));\n"
2583    "INSERT INTO [_shell$self]\n"
2584    "  SELECT 'run',"
2585    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2586    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2587    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2588    "  FROM (\n"
2589    "    SELECT name FROM sqlite_schema\n"
2590    "     WHERE type='table'\n"
2591    "       AND name<>'selftest'\n"
2592    "       AND coalesce(rootpage,0)>0\n"
2593    "  )\n"
2594    " ORDER BY name;\n"
2595    "INSERT INTO [_shell$self]\n"
2596    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2597    "INSERT INTO selftest(tno,op,cmd,ans)"
2598    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2599    "DROP TABLE [_shell$self];"
2600    ,0,0,&zErrMsg);
2601  if( zErrMsg ){
2602    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2603    sqlite3_free(zErrMsg);
2604  }
2605  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2606}
2607
2608
2609/*
2610** Set the destination table field of the ShellState structure to
2611** the name of the table given.  Escape any quote characters in the
2612** table name.
2613*/
2614static void set_table_name(ShellState *p, const char *zName){
2615  int i, n;
2616  char cQuote;
2617  char *z;
2618
2619  if( p->zDestTable ){
2620    free(p->zDestTable);
2621    p->zDestTable = 0;
2622  }
2623  if( zName==0 ) return;
2624  cQuote = quoteChar(zName);
2625  n = strlen30(zName);
2626  if( cQuote ) n += n+2;
2627  z = p->zDestTable = malloc( n+1 );
2628  shell_check_oom(z);
2629  n = 0;
2630  if( cQuote ) z[n++] = cQuote;
2631  for(i=0; zName[i]; i++){
2632    z[n++] = zName[i];
2633    if( zName[i]==cQuote ) z[n++] = cQuote;
2634  }
2635  if( cQuote ) z[n++] = cQuote;
2636  z[n] = 0;
2637}
2638
2639/*
2640** Maybe construct two lines of text that point out the position of a
2641** syntax error.  Return a pointer to the text, in memory obtained from
2642** sqlite3_malloc().  Or, if the most recent error does not involve a
2643** specific token that we can point to, return an empty string.
2644**
2645** In all cases, the memory returned is obtained from sqlite3_malloc64()
2646** and should be released by the caller invoking sqlite3_free().
2647*/
2648static char *shell_error_context(const char *zSql, sqlite3 *db){
2649  int iOffset;
2650  size_t len;
2651  char *zCode;
2652  char *zMsg;
2653  int i;
2654  if( db==0
2655   || zSql==0
2656   || (iOffset = sqlite3_error_offset(db))<0
2657  ){
2658    return sqlite3_mprintf("");
2659  }
2660  while( iOffset>50 ){
2661    iOffset--;
2662    zSql++;
2663    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
2664  }
2665  len = strlen(zSql);
2666  if( len>78 ){
2667    len = 78;
2668    while( (zSql[len]&0xc0)==0x80 ) len--;
2669  }
2670  zCode = sqlite3_mprintf("%.*s", len, zSql);
2671  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
2672  if( iOffset<25 ){
2673    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");
2674  }else{
2675    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");
2676  }
2677  return zMsg;
2678}
2679
2680
2681/*
2682** Execute a query statement that will generate SQL output.  Print
2683** the result columns, comma-separated, on a line and then add a
2684** semicolon terminator to the end of that line.
2685**
2686** If the number of columns is 1 and that column contains text "--"
2687** then write the semicolon on a separate line.  That way, if a
2688** "--" comment occurs at the end of the statement, the comment
2689** won't consume the semicolon terminator.
2690*/
2691static int run_table_dump_query(
2692  ShellState *p,           /* Query context */
2693  const char *zSelect      /* SELECT statement to extract content */
2694){
2695  sqlite3_stmt *pSelect;
2696  int rc;
2697  int nResult;
2698  int i;
2699  const char *z;
2700  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2701  if( rc!=SQLITE_OK || !pSelect ){
2702    char *zContext = shell_error_context(zSelect, p->db);
2703    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
2704                sqlite3_errmsg(p->db), zContext);
2705    sqlite3_free(zContext);
2706    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2707    return rc;
2708  }
2709  rc = sqlite3_step(pSelect);
2710  nResult = sqlite3_column_count(pSelect);
2711  while( rc==SQLITE_ROW ){
2712    z = (const char*)sqlite3_column_text(pSelect, 0);
2713    utf8_printf(p->out, "%s", z);
2714    for(i=1; i<nResult; i++){
2715      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2716    }
2717    if( z==0 ) z = "";
2718    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2719    if( z[0] ){
2720      raw_printf(p->out, "\n;\n");
2721    }else{
2722      raw_printf(p->out, ";\n");
2723    }
2724    rc = sqlite3_step(pSelect);
2725  }
2726  rc = sqlite3_finalize(pSelect);
2727  if( rc!=SQLITE_OK ){
2728    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2729                sqlite3_errmsg(p->db));
2730    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2731  }
2732  return rc;
2733}
2734
2735/*
2736** Allocate space and save off string indicating current error.
2737*/
2738static char *save_err_msg(
2739  sqlite3 *db,           /* Database to query */
2740  const char *zPhase,    /* When the error occcurs */
2741  int rc,                /* Error code returned from API */
2742  const char *zSql       /* SQL string, or NULL */
2743){
2744  char *zErr;
2745  char *zContext;
2746  sqlite3_str *pStr = sqlite3_str_new(0);
2747  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
2748  if( rc>1 ){
2749    sqlite3_str_appendf(pStr, " (%d)", rc);
2750  }
2751  zContext = shell_error_context(zSql, db);
2752  if( zContext ){
2753    sqlite3_str_appendall(pStr, zContext);
2754    sqlite3_free(zContext);
2755  }
2756  zErr = sqlite3_str_finish(pStr);
2757  shell_check_oom(zErr);
2758  return zErr;
2759}
2760
2761#ifdef __linux__
2762/*
2763** Attempt to display I/O stats on Linux using /proc/PID/io
2764*/
2765static void displayLinuxIoStats(FILE *out){
2766  FILE *in;
2767  char z[200];
2768  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2769  in = fopen(z, "rb");
2770  if( in==0 ) return;
2771  while( fgets(z, sizeof(z), in)!=0 ){
2772    static const struct {
2773      const char *zPattern;
2774      const char *zDesc;
2775    } aTrans[] = {
2776      { "rchar: ",                  "Bytes received by read():" },
2777      { "wchar: ",                  "Bytes sent to write():"    },
2778      { "syscr: ",                  "Read() system calls:"      },
2779      { "syscw: ",                  "Write() system calls:"     },
2780      { "read_bytes: ",             "Bytes read from storage:"  },
2781      { "write_bytes: ",            "Bytes written to storage:" },
2782      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2783    };
2784    int i;
2785    for(i=0; i<ArraySize(aTrans); i++){
2786      int n = strlen30(aTrans[i].zPattern);
2787      if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
2788        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2789        break;
2790      }
2791    }
2792  }
2793  fclose(in);
2794}
2795#endif
2796
2797/*
2798** Display a single line of status using 64-bit values.
2799*/
2800static void displayStatLine(
2801  ShellState *p,            /* The shell context */
2802  char *zLabel,             /* Label for this one line */
2803  char *zFormat,            /* Format for the result */
2804  int iStatusCtrl,          /* Which status to display */
2805  int bReset                /* True to reset the stats */
2806){
2807  sqlite3_int64 iCur = -1;
2808  sqlite3_int64 iHiwtr = -1;
2809  int i, nPercent;
2810  char zLine[200];
2811  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2812  for(i=0, nPercent=0; zFormat[i]; i++){
2813    if( zFormat[i]=='%' ) nPercent++;
2814  }
2815  if( nPercent>1 ){
2816    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2817  }else{
2818    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2819  }
2820  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2821}
2822
2823/*
2824** Display memory stats.
2825*/
2826static int display_stats(
2827  sqlite3 *db,                /* Database to query */
2828  ShellState *pArg,           /* Pointer to ShellState */
2829  int bReset                  /* True to reset the stats */
2830){
2831  int iCur;
2832  int iHiwtr;
2833  FILE *out;
2834  if( pArg==0 || pArg->out==0 ) return 0;
2835  out = pArg->out;
2836
2837  if( pArg->pStmt && pArg->statsOn==2 ){
2838    int nCol, i, x;
2839    sqlite3_stmt *pStmt = pArg->pStmt;
2840    char z[100];
2841    nCol = sqlite3_column_count(pStmt);
2842    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2843    for(i=0; i<nCol; i++){
2844      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2845      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2846#ifndef SQLITE_OMIT_DECLTYPE
2847      sqlite3_snprintf(30, z+x, "declared type:");
2848      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2849#endif
2850#ifdef SQLITE_ENABLE_COLUMN_METADATA
2851      sqlite3_snprintf(30, z+x, "database name:");
2852      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2853      sqlite3_snprintf(30, z+x, "table name:");
2854      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2855      sqlite3_snprintf(30, z+x, "origin name:");
2856      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2857#endif
2858    }
2859  }
2860
2861  if( pArg->statsOn==3 ){
2862    if( pArg->pStmt ){
2863      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2864      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2865    }
2866    return 0;
2867  }
2868
2869  displayStatLine(pArg, "Memory Used:",
2870     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2871  displayStatLine(pArg, "Number of Outstanding Allocations:",
2872     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2873  if( pArg->shellFlgs & SHFLG_Pagecache ){
2874    displayStatLine(pArg, "Number of Pcache Pages Used:",
2875       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2876  }
2877  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2878     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2879  displayStatLine(pArg, "Largest Allocation:",
2880     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2881  displayStatLine(pArg, "Largest Pcache Allocation:",
2882     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2883#ifdef YYTRACKMAXSTACKDEPTH
2884  displayStatLine(pArg, "Deepest Parser Stack:",
2885     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2886#endif
2887
2888  if( db ){
2889    if( pArg->shellFlgs & SHFLG_Lookaside ){
2890      iHiwtr = iCur = -1;
2891      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2892                        &iCur, &iHiwtr, bReset);
2893      raw_printf(pArg->out,
2894              "Lookaside Slots Used:                %d (max %d)\n",
2895              iCur, iHiwtr);
2896      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2897                        &iCur, &iHiwtr, bReset);
2898      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2899              iHiwtr);
2900      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2901                        &iCur, &iHiwtr, bReset);
2902      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2903              iHiwtr);
2904      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2905                        &iCur, &iHiwtr, bReset);
2906      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2907              iHiwtr);
2908    }
2909    iHiwtr = iCur = -1;
2910    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2911    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2912            iCur);
2913    iHiwtr = iCur = -1;
2914    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2915    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2916    iHiwtr = iCur = -1;
2917    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2918    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2919    iHiwtr = iCur = -1;
2920    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2921    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2922    iHiwtr = iCur = -1;
2923    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2924    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2925    iHiwtr = iCur = -1;
2926    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2927    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2928            iCur);
2929    iHiwtr = iCur = -1;
2930    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2931    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2932            iCur);
2933  }
2934
2935  if( pArg->pStmt ){
2936    int iHit, iMiss;
2937    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2938                               bReset);
2939    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2940    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2941    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2942    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2943    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2944    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);
2945    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);
2946    if( iHit || iMiss ){
2947      raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",
2948            iHit, iHit+iMiss);
2949    }
2950    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2951    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2952    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2953    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2954    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2955    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2956    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2957    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2958  }
2959
2960#ifdef __linux__
2961  displayLinuxIoStats(pArg->out);
2962#endif
2963
2964  /* Do not remove this machine readable comment: extra-stats-output-here */
2965
2966  return 0;
2967}
2968
2969/*
2970** Display scan stats.
2971*/
2972static void display_scanstats(
2973  sqlite3 *db,                    /* Database to query */
2974  ShellState *pArg                /* Pointer to ShellState */
2975){
2976#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2977  UNUSED_PARAMETER(db);
2978  UNUSED_PARAMETER(pArg);
2979#else
2980  int i, k, n, mx;
2981  raw_printf(pArg->out, "-------- scanstats --------\n");
2982  mx = 0;
2983  for(k=0; k<=mx; k++){
2984    double rEstLoop = 1.0;
2985    for(i=n=0; 1; i++){
2986      sqlite3_stmt *p = pArg->pStmt;
2987      sqlite3_int64 nLoop, nVisit;
2988      double rEst;
2989      int iSid;
2990      const char *zExplain;
2991      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2992        break;
2993      }
2994      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2995      if( iSid>mx ) mx = iSid;
2996      if( iSid!=k ) continue;
2997      if( n==0 ){
2998        rEstLoop = (double)nLoop;
2999        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
3000      }
3001      n++;
3002      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
3003      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
3004      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
3005      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
3006      rEstLoop *= rEst;
3007      raw_printf(pArg->out,
3008          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
3009          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
3010      );
3011    }
3012  }
3013  raw_printf(pArg->out, "---------------------------\n");
3014#endif
3015}
3016
3017/*
3018** Parameter azArray points to a zero-terminated array of strings. zStr
3019** points to a single nul-terminated string. Return non-zero if zStr
3020** is equal, according to strcmp(), to any of the strings in the array.
3021** Otherwise, return zero.
3022*/
3023static int str_in_array(const char *zStr, const char **azArray){
3024  int i;
3025  for(i=0; azArray[i]; i++){
3026    if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
3027  }
3028  return 0;
3029}
3030
3031/*
3032** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3033** and populate the ShellState.aiIndent[] array with the number of
3034** spaces each opcode should be indented before it is output.
3035**
3036** The indenting rules are:
3037**
3038**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3039**       all opcodes that occur between the p2 jump destination and the opcode
3040**       itself by 2 spaces.
3041**
3042**     * Do the previous for "Return" instructions for when P2 is positive.
3043**       See tag-20220407a in wherecode.c and vdbe.c.
3044**
3045**     * For each "Goto", if the jump destination is earlier in the program
3046**       and ends on one of:
3047**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
3048**       or if the P1 parameter is one instead of zero,
3049**       then indent all opcodes between the earlier instruction
3050**       and "Goto" by 2 spaces.
3051*/
3052static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3053  const char *zSql;               /* The text of the SQL statement */
3054  const char *z;                  /* Used to check if this is an EXPLAIN */
3055  int *abYield = 0;               /* True if op is an OP_Yield */
3056  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
3057  int iOp;                        /* Index of operation in p->aiIndent[] */
3058
3059  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3060                           "Return", 0 };
3061  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3062                            "Rewind", 0 };
3063  const char *azGoto[] = { "Goto", 0 };
3064
3065  /* Try to figure out if this is really an EXPLAIN statement. If this
3066  ** cannot be verified, return early.  */
3067  if( sqlite3_column_count(pSql)!=8 ){
3068    p->cMode = p->mode;
3069    return;
3070  }
3071  zSql = sqlite3_sql(pSql);
3072  if( zSql==0 ) return;
3073  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3074  if( sqlite3_strnicmp(z, "explain", 7) ){
3075    p->cMode = p->mode;
3076    return;
3077  }
3078
3079  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3080    int i;
3081    int iAddr = sqlite3_column_int(pSql, 0);
3082    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3083
3084    /* Set p2 to the P2 field of the current opcode. Then, assuming that
3085    ** p2 is an instruction address, set variable p2op to the index of that
3086    ** instruction in the aiIndent[] array. p2 and p2op may be different if
3087    ** the current instruction is part of a sub-program generated by an
3088    ** SQL trigger or foreign key.  */
3089    int p2 = sqlite3_column_int(pSql, 3);
3090    int p2op = (p2 + (iOp-iAddr));
3091
3092    /* Grow the p->aiIndent array as required */
3093    if( iOp>=nAlloc ){
3094      if( iOp==0 ){
3095        /* Do further verfication that this is explain output.  Abort if
3096        ** it is not */
3097        static const char *explainCols[] = {
3098           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3099        int jj;
3100        for(jj=0; jj<ArraySize(explainCols); jj++){
3101          if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3102            p->cMode = p->mode;
3103            sqlite3_reset(pSql);
3104            return;
3105          }
3106        }
3107      }
3108      nAlloc += 100;
3109      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3110      shell_check_oom(p->aiIndent);
3111      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3112      shell_check_oom(abYield);
3113    }
3114    abYield[iOp] = str_in_array(zOp, azYield);
3115    p->aiIndent[iOp] = 0;
3116    p->nIndent = iOp+1;
3117
3118    if( str_in_array(zOp, azNext) && p2op>0 ){
3119      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3120    }
3121    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3122     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3123    ){
3124      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3125    }
3126  }
3127
3128  p->iIndent = 0;
3129  sqlite3_free(abYield);
3130  sqlite3_reset(pSql);
3131}
3132
3133/*
3134** Free the array allocated by explain_data_prepare().
3135*/
3136static void explain_data_delete(ShellState *p){
3137  sqlite3_free(p->aiIndent);
3138  p->aiIndent = 0;
3139  p->nIndent = 0;
3140  p->iIndent = 0;
3141}
3142
3143/*
3144** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3145*/
3146static unsigned int savedSelectTrace;
3147static unsigned int savedWhereTrace;
3148static void disable_debug_trace_modes(void){
3149  unsigned int zero = 0;
3150  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3151  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3152  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3153  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3154}
3155static void restore_debug_trace_modes(void){
3156  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3157  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3158}
3159
3160/* Create the TEMP table used to store parameter bindings */
3161static void bind_table_init(ShellState *p){
3162  int wrSchema = 0;
3163  int defensiveMode = 0;
3164  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3165  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3166  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3167  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3168  sqlite3_exec(p->db,
3169    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3170    "  key TEXT PRIMARY KEY,\n"
3171    "  value\n"
3172    ") WITHOUT ROWID;",
3173    0, 0, 0);
3174  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3175  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3176}
3177
3178/*
3179** Bind parameters on a prepared statement.
3180**
3181** Parameter bindings are taken from a TEMP table of the form:
3182**
3183**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3184**    WITHOUT ROWID;
3185**
3186** No bindings occur if this table does not exist.  The name of the table
3187** begins with "sqlite_" so that it will not collide with ordinary application
3188** tables.  The table must be in the TEMP schema.
3189*/
3190static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3191  int nVar;
3192  int i;
3193  int rc;
3194  sqlite3_stmt *pQ = 0;
3195
3196  nVar = sqlite3_bind_parameter_count(pStmt);
3197  if( nVar==0 ) return;  /* Nothing to do */
3198  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3199                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3200    return; /* Parameter table does not exist */
3201  }
3202  rc = sqlite3_prepare_v2(pArg->db,
3203          "SELECT value FROM temp.sqlite_parameters"
3204          " WHERE key=?1", -1, &pQ, 0);
3205  if( rc || pQ==0 ) return;
3206  for(i=1; i<=nVar; i++){
3207    char zNum[30];
3208    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3209    if( zVar==0 ){
3210      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3211      zVar = zNum;
3212    }
3213    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3214    if( sqlite3_step(pQ)==SQLITE_ROW ){
3215      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3216    }else{
3217      sqlite3_bind_null(pStmt, i);
3218    }
3219    sqlite3_reset(pQ);
3220  }
3221  sqlite3_finalize(pQ);
3222}
3223
3224/*
3225** UTF8 box-drawing characters.  Imagine box lines like this:
3226**
3227**           1
3228**           |
3229**       4 --+-- 2
3230**           |
3231**           3
3232**
3233** Each box characters has between 2 and 4 of the lines leading from
3234** the center.  The characters are here identified by the numbers of
3235** their corresponding lines.
3236*/
3237#define BOX_24   "\342\224\200"  /* U+2500 --- */
3238#define BOX_13   "\342\224\202"  /* U+2502  |  */
3239#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3240#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3241#define BOX_12   "\342\224\224"  /* U+2514  '- */
3242#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3243#define BOX_123  "\342\224\234"  /* U+251c  |- */
3244#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3245#define BOX_234  "\342\224\254"  /* U+252c -,- */
3246#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3247#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3248
3249/* Draw horizontal line N characters long using unicode box
3250** characters
3251*/
3252static void print_box_line(FILE *out, int N){
3253  const char zDash[] =
3254      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3255      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3256  const int nDash = sizeof(zDash) - 1;
3257  N *= 3;
3258  while( N>nDash ){
3259    utf8_printf(out, zDash);
3260    N -= nDash;
3261  }
3262  utf8_printf(out, "%.*s", N, zDash);
3263}
3264
3265/*
3266** Draw a horizontal separator for a MODE_Box table.
3267*/
3268static void print_box_row_separator(
3269  ShellState *p,
3270  int nArg,
3271  const char *zSep1,
3272  const char *zSep2,
3273  const char *zSep3
3274){
3275  int i;
3276  if( nArg>0 ){
3277    utf8_printf(p->out, "%s", zSep1);
3278    print_box_line(p->out, p->actualWidth[0]+2);
3279    for(i=1; i<nArg; i++){
3280      utf8_printf(p->out, "%s", zSep2);
3281      print_box_line(p->out, p->actualWidth[i]+2);
3282    }
3283    utf8_printf(p->out, "%s", zSep3);
3284  }
3285  fputs("\n", p->out);
3286}
3287
3288/*
3289** z[] is a line of text that is to be displayed the .mode box or table or
3290** similar tabular formats.  z[] might contain control characters such
3291** as \n, \t, \f, or \r.
3292**
3293** Compute characters to display on the first line of z[].  Stop at the
3294** first \r, \n, or \f.  Expand \t into spaces.  Return a copy (obtained
3295** from malloc()) of that first line, which caller should free sometime.
3296** Write anything to display on the next line into *pzTail.  If this is
3297** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3298*/
3299static char *translateForDisplayAndDup(
3300  const unsigned char *z,            /* Input text to be transformed */
3301  const unsigned char **pzTail,      /* OUT: Tail of the input for next line */
3302  int mxWidth,                       /* Max width.  0 means no limit */
3303  u8 bWordWrap                       /* If true, avoid breaking mid-word */
3304){
3305  int i;                 /* Input bytes consumed */
3306  int j;                 /* Output bytes generated */
3307  int k;                 /* Input bytes to be displayed */
3308  int n;                 /* Output column number */
3309  unsigned char *zOut;   /* Output text */
3310
3311  if( z==0 ){
3312    *pzTail = 0;
3313    return 0;
3314  }
3315  if( mxWidth<0 ) mxWidth = -mxWidth;
3316  if( mxWidth==0 ) mxWidth = 1000000;
3317  i = j = n = 0;
3318  while( n<mxWidth ){
3319    if( z[i]>=' ' ){
3320      n++;
3321      do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
3322      continue;
3323    }
3324    if( z[i]=='\t' ){
3325      do{
3326        n++;
3327        j++;
3328      }while( (n&7)!=0 && n<mxWidth );
3329      i++;
3330      continue;
3331    }
3332    break;
3333  }
3334  if( n>=mxWidth && bWordWrap  ){
3335    /* Perhaps try to back up to a better place to break the line */
3336    for(k=i; k>i/2; k--){
3337      if( isspace(z[k-1]) ) break;
3338    }
3339    if( k<=i/2 ){
3340      for(k=i; k>i/2; k--){
3341        if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3342      }
3343    }
3344    if( k<=i/2 ){
3345      k = i;
3346    }else{
3347      i = k;
3348      while( z[i]==' ' ) i++;
3349    }
3350  }else{
3351    k = i;
3352  }
3353  if( n>=mxWidth && z[i]>=' ' ){
3354   *pzTail = &z[i];
3355  }else if( z[i]=='\r' && z[i+1]=='\n' ){
3356    *pzTail = z[i+2] ? &z[i+2] : 0;
3357  }else if( z[i]==0 || z[i+1]==0 ){
3358    *pzTail = 0;
3359  }else{
3360    *pzTail = &z[i+1];
3361  }
3362  zOut = malloc( j+1 );
3363  shell_check_oom(zOut);
3364  i = j = n = 0;
3365  while( i<k ){
3366    if( z[i]>=' ' ){
3367      n++;
3368      do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
3369      continue;
3370    }
3371    if( z[i]=='\t' ){
3372      do{
3373        n++;
3374        zOut[j++] = ' ';
3375      }while( (n&7)!=0 && n<mxWidth );
3376      i++;
3377      continue;
3378    }
3379    break;
3380  }
3381  zOut[j] = 0;
3382  return (char*)zOut;
3383}
3384
3385/* Extract the value of the i-th current column for pStmt as an SQL literal
3386** value.  Memory is obtained from sqlite3_malloc64() and must be freed by
3387** the caller.
3388*/
3389static char *quoted_column(sqlite3_stmt *pStmt, int i){
3390  switch( sqlite3_column_type(pStmt, i) ){
3391    case SQLITE_NULL: {
3392      return sqlite3_mprintf("NULL");
3393    }
3394    case SQLITE_INTEGER:
3395    case SQLITE_FLOAT: {
3396      return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3397    }
3398    case SQLITE_TEXT: {
3399      return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3400    }
3401    case SQLITE_BLOB: {
3402      int j;
3403      sqlite3_str *pStr = sqlite3_str_new(0);
3404      const unsigned char *a = sqlite3_column_blob(pStmt,i);
3405      int n = sqlite3_column_bytes(pStmt,i);
3406      sqlite3_str_append(pStr, "x'", 2);
3407      for(j=0; j<n; j++){
3408        sqlite3_str_appendf(pStr, "%02x", a[j]);
3409      }
3410      sqlite3_str_append(pStr, "'", 1);
3411      return sqlite3_str_finish(pStr);
3412    }
3413  }
3414  return 0; /* Not reached */
3415}
3416
3417/*
3418** Run a prepared statement and output the result in one of the
3419** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3420** or MODE_Box.
3421**
3422** This is different from ordinary exec_prepared_stmt() in that
3423** it has to run the entire query and gather the results into memory
3424** first, in order to determine column widths, before providing
3425** any output.
3426*/
3427static void exec_prepared_stmt_columnar(
3428  ShellState *p,                        /* Pointer to ShellState */
3429  sqlite3_stmt *pStmt                   /* Statment to run */
3430){
3431  sqlite3_int64 nRow = 0;
3432  int nColumn = 0;
3433  char **azData = 0;
3434  sqlite3_int64 nAlloc = 0;
3435  char *abRowDiv = 0;
3436  const unsigned char *uz;
3437  const char *z;
3438  char **azQuoted = 0;
3439  int rc;
3440  sqlite3_int64 i, nData;
3441  int j, nTotal, w, n;
3442  const char *colSep = 0;
3443  const char *rowSep = 0;
3444  const unsigned char **azNextLine = 0;
3445  int bNextLine = 0;
3446  int bMultiLineRowExists = 0;
3447  int bw = p->cmOpts.bWordWrap;
3448  const char *zEmpty = "";
3449  const char *zShowNull = p->nullValue;
3450
3451  rc = sqlite3_step(pStmt);
3452  if( rc!=SQLITE_ROW ) return;
3453  nColumn = sqlite3_column_count(pStmt);
3454  nAlloc = nColumn*4;
3455  if( nAlloc<=0 ) nAlloc = 1;
3456  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3457  shell_check_oom(azData);
3458  azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
3459  shell_check_oom((void*)azNextLine);
3460  memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
3461  if( p->cmOpts.bQuote ){
3462    azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
3463    shell_check_oom(azQuoted);
3464    memset(azQuoted, 0, nColumn*sizeof(char*) );
3465  }
3466  abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
3467  shell_check_oom(abRowDiv);
3468  if( nColumn>p->nWidth ){
3469    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3470    shell_check_oom(p->colWidth);
3471    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3472    p->nWidth = nColumn;
3473    p->actualWidth = &p->colWidth[nColumn];
3474  }
3475  memset(p->actualWidth, 0, nColumn*sizeof(int));
3476  for(i=0; i<nColumn; i++){
3477    w = p->colWidth[i];
3478    if( w<0 ) w = -w;
3479    p->actualWidth[i] = w;
3480  }
3481  for(i=0; i<nColumn; i++){
3482    const unsigned char *zNotUsed;
3483    int wx = p->colWidth[i];
3484    if( wx==0 ){
3485      wx = p->cmOpts.iWrap;
3486    }
3487    if( wx<0 ) wx = -wx;
3488    uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
3489    azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
3490  }
3491  do{
3492    int useNextLine = bNextLine;
3493    bNextLine = 0;
3494    if( (nRow+2)*nColumn >= nAlloc ){
3495      nAlloc *= 2;
3496      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3497      shell_check_oom(azData);
3498      abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
3499      shell_check_oom(abRowDiv);
3500    }
3501    abRowDiv[nRow] = 1;
3502    nRow++;
3503    for(i=0; i<nColumn; i++){
3504      int wx = p->colWidth[i];
3505      if( wx==0 ){
3506        wx = p->cmOpts.iWrap;
3507      }
3508      if( wx<0 ) wx = -wx;
3509      if( useNextLine ){
3510        uz = azNextLine[i];
3511        if( uz==0 ) uz = (u8*)zEmpty;
3512      }else if( p->cmOpts.bQuote ){
3513        sqlite3_free(azQuoted[i]);
3514        azQuoted[i] = quoted_column(pStmt,i);
3515        uz = (const unsigned char*)azQuoted[i];
3516      }else{
3517        uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
3518        if( uz==0 ) uz = (u8*)zShowNull;
3519      }
3520      azData[nRow*nColumn + i]
3521        = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
3522      if( azNextLine[i] ){
3523        bNextLine = 1;
3524        abRowDiv[nRow-1] = 0;
3525        bMultiLineRowExists = 1;
3526      }
3527    }
3528  }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
3529  nTotal = nColumn*(nRow+1);
3530  for(i=0; i<nTotal; i++){
3531    z = azData[i];
3532    if( z==0 ) z = (char*)zEmpty;
3533    n = strlenChar(z);
3534    j = i%nColumn;
3535    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3536  }
3537  if( seenInterrupt ) goto columnar_end;
3538  if( nColumn==0 ) goto columnar_end;
3539  switch( p->cMode ){
3540    case MODE_Column: {
3541      colSep = "  ";
3542      rowSep = "\n";
3543      if( p->showHeader ){
3544        for(i=0; i<nColumn; i++){
3545          w = p->actualWidth[i];
3546          if( p->colWidth[i]<0 ) w = -w;
3547          utf8_width_print(p->out, w, azData[i]);
3548          fputs(i==nColumn-1?"\n":"  ", p->out);
3549        }
3550        for(i=0; i<nColumn; i++){
3551          print_dashes(p->out, p->actualWidth[i]);
3552          fputs(i==nColumn-1?"\n":"  ", p->out);
3553        }
3554      }
3555      break;
3556    }
3557    case MODE_Table: {
3558      colSep = " | ";
3559      rowSep = " |\n";
3560      print_row_separator(p, nColumn, "+");
3561      fputs("| ", p->out);
3562      for(i=0; i<nColumn; i++){
3563        w = p->actualWidth[i];
3564        n = strlenChar(azData[i]);
3565        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3566        fputs(i==nColumn-1?" |\n":" | ", p->out);
3567      }
3568      print_row_separator(p, nColumn, "+");
3569      break;
3570    }
3571    case MODE_Markdown: {
3572      colSep = " | ";
3573      rowSep = " |\n";
3574      fputs("| ", p->out);
3575      for(i=0; i<nColumn; i++){
3576        w = p->actualWidth[i];
3577        n = strlenChar(azData[i]);
3578        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3579        fputs(i==nColumn-1?" |\n":" | ", p->out);
3580      }
3581      print_row_separator(p, nColumn, "|");
3582      break;
3583    }
3584    case MODE_Box: {
3585      colSep = " " BOX_13 " ";
3586      rowSep = " " BOX_13 "\n";
3587      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3588      utf8_printf(p->out, BOX_13 " ");
3589      for(i=0; i<nColumn; i++){
3590        w = p->actualWidth[i];
3591        n = strlenChar(azData[i]);
3592        utf8_printf(p->out, "%*s%s%*s%s",
3593            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3594            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3595      }
3596      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3597      break;
3598    }
3599  }
3600  for(i=nColumn, j=0; i<nTotal; i++, j++){
3601    if( j==0 && p->cMode!=MODE_Column ){
3602      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3603    }
3604    z = azData[i];
3605    if( z==0 ) z = p->nullValue;
3606    w = p->actualWidth[j];
3607    if( p->colWidth[j]<0 ) w = -w;
3608    utf8_width_print(p->out, w, z);
3609    if( j==nColumn-1 ){
3610      utf8_printf(p->out, "%s", rowSep);
3611      if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
3612        if( p->cMode==MODE_Table ){
3613          print_row_separator(p, nColumn, "+");
3614        }else if( p->cMode==MODE_Box ){
3615          print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3616        }else if( p->cMode==MODE_Column ){
3617          raw_printf(p->out, "\n");
3618        }
3619      }
3620      j = -1;
3621      if( seenInterrupt ) goto columnar_end;
3622    }else{
3623      utf8_printf(p->out, "%s", colSep);
3624    }
3625  }
3626  if( p->cMode==MODE_Table ){
3627    print_row_separator(p, nColumn, "+");
3628  }else if( p->cMode==MODE_Box ){
3629    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3630  }
3631columnar_end:
3632  if( seenInterrupt ){
3633    utf8_printf(p->out, "Interrupt\n");
3634  }
3635  nData = (nRow+1)*nColumn;
3636  for(i=0; i<nData; i++){
3637    z = azData[i];
3638    if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
3639  }
3640  sqlite3_free(azData);
3641  sqlite3_free((void*)azNextLine);
3642  sqlite3_free(abRowDiv);
3643  if( azQuoted ){
3644    for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
3645    sqlite3_free(azQuoted);
3646  }
3647}
3648
3649/*
3650** Run a prepared statement
3651*/
3652static void exec_prepared_stmt(
3653  ShellState *pArg,                                /* Pointer to ShellState */
3654  sqlite3_stmt *pStmt                              /* Statment to run */
3655){
3656  int rc;
3657  sqlite3_uint64 nRow = 0;
3658
3659  if( pArg->cMode==MODE_Column
3660   || pArg->cMode==MODE_Table
3661   || pArg->cMode==MODE_Box
3662   || pArg->cMode==MODE_Markdown
3663  ){
3664    exec_prepared_stmt_columnar(pArg, pStmt);
3665    return;
3666  }
3667
3668  /* perform the first step.  this will tell us if we
3669  ** have a result set or not and how wide it is.
3670  */
3671  rc = sqlite3_step(pStmt);
3672  /* if we have a result set... */
3673  if( SQLITE_ROW == rc ){
3674    /* allocate space for col name ptr, value ptr, and type */
3675    int nCol = sqlite3_column_count(pStmt);
3676    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3677    if( !pData ){
3678      shell_out_of_memory();
3679    }else{
3680      char **azCols = (char **)pData;      /* Names of result columns */
3681      char **azVals = &azCols[nCol];       /* Results */
3682      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3683      int i, x;
3684      assert(sizeof(int) <= sizeof(char *));
3685      /* save off ptrs to column names */
3686      for(i=0; i<nCol; i++){
3687        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3688      }
3689      do{
3690        nRow++;
3691        /* extract the data and data types */
3692        for(i=0; i<nCol; i++){
3693          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3694          if( x==SQLITE_BLOB
3695           && pArg
3696           && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
3697          ){
3698            azVals[i] = "";
3699          }else{
3700            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3701          }
3702          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3703            rc = SQLITE_NOMEM;
3704            break; /* from for */
3705          }
3706        } /* end for */
3707
3708        /* if data and types extracted successfully... */
3709        if( SQLITE_ROW == rc ){
3710          /* call the supplied callback with the result row data */
3711          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3712            rc = SQLITE_ABORT;
3713          }else{
3714            rc = sqlite3_step(pStmt);
3715          }
3716        }
3717      } while( SQLITE_ROW == rc );
3718      sqlite3_free(pData);
3719      if( pArg->cMode==MODE_Json ){
3720        fputs("]\n", pArg->out);
3721      }else if( pArg->cMode==MODE_Count ){
3722        char zBuf[200];
3723        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
3724                         nRow, nRow!=1 ? "s" : "");
3725        printf("%s", zBuf);
3726      }
3727    }
3728  }
3729}
3730
3731#ifndef SQLITE_OMIT_VIRTUALTABLE
3732/*
3733** This function is called to process SQL if the previous shell command
3734** was ".expert". It passes the SQL in the second argument directly to
3735** the sqlite3expert object.
3736**
3737** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3738** code. In this case, (*pzErr) may be set to point to a buffer containing
3739** an English language error message. It is the responsibility of the
3740** caller to eventually free this buffer using sqlite3_free().
3741*/
3742static int expertHandleSQL(
3743  ShellState *pState,
3744  const char *zSql,
3745  char **pzErr
3746){
3747  assert( pState->expert.pExpert );
3748  assert( pzErr==0 || *pzErr==0 );
3749  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3750}
3751
3752/*
3753** This function is called either to silently clean up the object
3754** created by the ".expert" command (if bCancel==1), or to generate a
3755** report from it and then clean it up (if bCancel==0).
3756**
3757** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3758** code. In this case, (*pzErr) may be set to point to a buffer containing
3759** an English language error message. It is the responsibility of the
3760** caller to eventually free this buffer using sqlite3_free().
3761*/
3762static int expertFinish(
3763  ShellState *pState,
3764  int bCancel,
3765  char **pzErr
3766){
3767  int rc = SQLITE_OK;
3768  sqlite3expert *p = pState->expert.pExpert;
3769  assert( p );
3770  assert( bCancel || pzErr==0 || *pzErr==0 );
3771  if( bCancel==0 ){
3772    FILE *out = pState->out;
3773    int bVerbose = pState->expert.bVerbose;
3774
3775    rc = sqlite3_expert_analyze(p, pzErr);
3776    if( rc==SQLITE_OK ){
3777      int nQuery = sqlite3_expert_count(p);
3778      int i;
3779
3780      if( bVerbose ){
3781        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3782        raw_printf(out, "-- Candidates -----------------------------\n");
3783        raw_printf(out, "%s\n", zCand);
3784      }
3785      for(i=0; i<nQuery; i++){
3786        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3787        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3788        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3789        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3790        if( bVerbose ){
3791          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3792          raw_printf(out, "%s\n\n", zSql);
3793        }
3794        raw_printf(out, "%s\n", zIdx);
3795        raw_printf(out, "%s\n", zEQP);
3796      }
3797    }
3798  }
3799  sqlite3_expert_destroy(p);
3800  pState->expert.pExpert = 0;
3801  return rc;
3802}
3803
3804/*
3805** Implementation of ".expert" dot command.
3806*/
3807static int expertDotCommand(
3808  ShellState *pState,             /* Current shell tool state */
3809  char **azArg,                   /* Array of arguments passed to dot command */
3810  int nArg                        /* Number of entries in azArg[] */
3811){
3812  int rc = SQLITE_OK;
3813  char *zErr = 0;
3814  int i;
3815  int iSample = 0;
3816
3817  assert( pState->expert.pExpert==0 );
3818  memset(&pState->expert, 0, sizeof(ExpertInfo));
3819
3820  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3821    char *z = azArg[i];
3822    int n;
3823    if( z[0]=='-' && z[1]=='-' ) z++;
3824    n = strlen30(z);
3825    if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
3826      pState->expert.bVerbose = 1;
3827    }
3828    else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
3829      if( i==(nArg-1) ){
3830        raw_printf(stderr, "option requires an argument: %s\n", z);
3831        rc = SQLITE_ERROR;
3832      }else{
3833        iSample = (int)integerValue(azArg[++i]);
3834        if( iSample<0 || iSample>100 ){
3835          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3836          rc = SQLITE_ERROR;
3837        }
3838      }
3839    }
3840    else{
3841      raw_printf(stderr, "unknown option: %s\n", z);
3842      rc = SQLITE_ERROR;
3843    }
3844  }
3845
3846  if( rc==SQLITE_OK ){
3847    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3848    if( pState->expert.pExpert==0 ){
3849      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
3850      rc = SQLITE_ERROR;
3851    }else{
3852      sqlite3_expert_config(
3853          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3854      );
3855    }
3856  }
3857  sqlite3_free(zErr);
3858
3859  return rc;
3860}
3861#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3862
3863/*
3864** Execute a statement or set of statements.  Print
3865** any result rows/columns depending on the current mode
3866** set via the supplied callback.
3867**
3868** This is very similar to SQLite's built-in sqlite3_exec()
3869** function except it takes a slightly different callback
3870** and callback data argument.
3871*/
3872static int shell_exec(
3873  ShellState *pArg,                         /* Pointer to ShellState */
3874  const char *zSql,                         /* SQL to be evaluated */
3875  char **pzErrMsg                           /* Error msg written here */
3876){
3877  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3878  int rc = SQLITE_OK;             /* Return Code */
3879  int rc2;
3880  const char *zLeftover;          /* Tail of unprocessed SQL */
3881  sqlite3 *db = pArg->db;
3882
3883  if( pzErrMsg ){
3884    *pzErrMsg = NULL;
3885  }
3886
3887#ifndef SQLITE_OMIT_VIRTUALTABLE
3888  if( pArg->expert.pExpert ){
3889    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3890    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3891  }
3892#endif
3893
3894  while( zSql[0] && (SQLITE_OK == rc) ){
3895    static const char *zStmtSql;
3896    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3897    if( SQLITE_OK != rc ){
3898      if( pzErrMsg ){
3899        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
3900      }
3901    }else{
3902      if( !pStmt ){
3903        /* this happens for a comment or white-space */
3904        zSql = zLeftover;
3905        while( IsSpace(zSql[0]) ) zSql++;
3906        continue;
3907      }
3908      zStmtSql = sqlite3_sql(pStmt);
3909      if( zStmtSql==0 ) zStmtSql = "";
3910      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3911
3912      /* save off the prepared statment handle and reset row count */
3913      if( pArg ){
3914        pArg->pStmt = pStmt;
3915        pArg->cnt = 0;
3916      }
3917
3918      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3919      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3920        sqlite3_stmt *pExplain;
3921        char *zEQP;
3922        int triggerEQP = 0;
3923        disable_debug_trace_modes();
3924        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3925        if( pArg->autoEQP>=AUTOEQP_trigger ){
3926          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3927        }
3928        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3929        shell_check_oom(zEQP);
3930        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3931        if( rc==SQLITE_OK ){
3932          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3933            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3934            int iEqpId = sqlite3_column_int(pExplain, 0);
3935            int iParentId = sqlite3_column_int(pExplain, 1);
3936            if( zEQPLine==0 ) zEQPLine = "";
3937            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3938            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3939          }
3940          eqp_render(pArg);
3941        }
3942        sqlite3_finalize(pExplain);
3943        sqlite3_free(zEQP);
3944        if( pArg->autoEQP>=AUTOEQP_full ){
3945          /* Also do an EXPLAIN for ".eqp full" mode */
3946          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3947          shell_check_oom(zEQP);
3948          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3949          if( rc==SQLITE_OK ){
3950            pArg->cMode = MODE_Explain;
3951            explain_data_prepare(pArg, pExplain);
3952            exec_prepared_stmt(pArg, pExplain);
3953            explain_data_delete(pArg);
3954          }
3955          sqlite3_finalize(pExplain);
3956          sqlite3_free(zEQP);
3957        }
3958        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3959          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3960          /* Reprepare pStmt before reactiving trace modes */
3961          sqlite3_finalize(pStmt);
3962          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3963          if( pArg ) pArg->pStmt = pStmt;
3964        }
3965        restore_debug_trace_modes();
3966      }
3967
3968      if( pArg ){
3969        pArg->cMode = pArg->mode;
3970        if( pArg->autoExplain ){
3971          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3972            pArg->cMode = MODE_Explain;
3973          }
3974          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3975            pArg->cMode = MODE_EQP;
3976          }
3977        }
3978
3979        /* If the shell is currently in ".explain" mode, gather the extra
3980        ** data required to add indents to the output.*/
3981        if( pArg->cMode==MODE_Explain ){
3982          explain_data_prepare(pArg, pStmt);
3983        }
3984      }
3985
3986      bind_prepared_stmt(pArg, pStmt);
3987      exec_prepared_stmt(pArg, pStmt);
3988      explain_data_delete(pArg);
3989      eqp_render(pArg);
3990
3991      /* print usage stats if stats on */
3992      if( pArg && pArg->statsOn ){
3993        display_stats(db, pArg, 0);
3994      }
3995
3996      /* print loop-counters if required */
3997      if( pArg && pArg->scanstatsOn ){
3998        display_scanstats(db, pArg);
3999      }
4000
4001      /* Finalize the statement just executed. If this fails, save a
4002      ** copy of the error message. Otherwise, set zSql to point to the
4003      ** next statement to execute. */
4004      rc2 = sqlite3_finalize(pStmt);
4005      if( rc!=SQLITE_NOMEM ) rc = rc2;
4006      if( rc==SQLITE_OK ){
4007        zSql = zLeftover;
4008        while( IsSpace(zSql[0]) ) zSql++;
4009      }else if( pzErrMsg ){
4010        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
4011      }
4012
4013      /* clear saved stmt handle */
4014      if( pArg ){
4015        pArg->pStmt = NULL;
4016      }
4017    }
4018  } /* end while */
4019
4020  return rc;
4021}
4022
4023/*
4024** Release memory previously allocated by tableColumnList().
4025*/
4026static void freeColumnList(char **azCol){
4027  int i;
4028  for(i=1; azCol[i]; i++){
4029    sqlite3_free(azCol[i]);
4030  }
4031  /* azCol[0] is a static string */
4032  sqlite3_free(azCol);
4033}
4034
4035/*
4036** Return a list of pointers to strings which are the names of all
4037** columns in table zTab.   The memory to hold the names is dynamically
4038** allocated and must be released by the caller using a subsequent call
4039** to freeColumnList().
4040**
4041** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
4042** value that needs to be preserved, then azCol[0] is filled in with the
4043** name of the rowid column.
4044**
4045** The first regular column in the table is azCol[1].  The list is terminated
4046** by an entry with azCol[i]==0.
4047*/
4048static char **tableColumnList(ShellState *p, const char *zTab){
4049  char **azCol = 0;
4050  sqlite3_stmt *pStmt;
4051  char *zSql;
4052  int nCol = 0;
4053  int nAlloc = 0;
4054  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
4055  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
4056  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4057  int rc;
4058
4059  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4060  shell_check_oom(zSql);
4061  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4062  sqlite3_free(zSql);
4063  if( rc ) return 0;
4064  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4065    if( nCol>=nAlloc-2 ){
4066      nAlloc = nAlloc*2 + nCol + 10;
4067      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4068      shell_check_oom(azCol);
4069    }
4070    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4071    shell_check_oom(azCol[nCol]);
4072    if( sqlite3_column_int(pStmt, 5) ){
4073      nPK++;
4074      if( nPK==1
4075       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4076                          "INTEGER")==0
4077      ){
4078        isIPK = 1;
4079      }else{
4080        isIPK = 0;
4081      }
4082    }
4083  }
4084  sqlite3_finalize(pStmt);
4085  if( azCol==0 ) return 0;
4086  azCol[0] = 0;
4087  azCol[nCol+1] = 0;
4088
4089  /* The decision of whether or not a rowid really needs to be preserved
4090  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
4091  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
4092  ** rowids on tables where the rowid is inaccessible because there are other
4093  ** columns in the table named "rowid", "_rowid_", and "oid".
4094  */
4095  if( preserveRowid && isIPK ){
4096    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4097    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
4098    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4099    ** ROWID aliases.  To distinguish these cases, check to see if
4100    ** there is a "pk" entry in "PRAGMA index_list".  There will be
4101    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4102    */
4103    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4104                           " WHERE origin='pk'", zTab);
4105    shell_check_oom(zSql);
4106    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4107    sqlite3_free(zSql);
4108    if( rc ){
4109      freeColumnList(azCol);
4110      return 0;
4111    }
4112    rc = sqlite3_step(pStmt);
4113    sqlite3_finalize(pStmt);
4114    preserveRowid = rc==SQLITE_ROW;
4115  }
4116  if( preserveRowid ){
4117    /* Only preserve the rowid if we can find a name to use for the
4118    ** rowid */
4119    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4120    int i, j;
4121    for(j=0; j<3; j++){
4122      for(i=1; i<=nCol; i++){
4123        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4124      }
4125      if( i>nCol ){
4126        /* At this point, we know that azRowid[j] is not the name of any
4127        ** ordinary column in the table.  Verify that azRowid[j] is a valid
4128        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
4129        ** tables will fail this last check */
4130        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4131        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4132        break;
4133      }
4134    }
4135  }
4136  return azCol;
4137}
4138
4139/*
4140** Toggle the reverse_unordered_selects setting.
4141*/
4142static void toggleSelectOrder(sqlite3 *db){
4143  sqlite3_stmt *pStmt = 0;
4144  int iSetting = 0;
4145  char zStmt[100];
4146  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4147  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4148    iSetting = sqlite3_column_int(pStmt, 0);
4149  }
4150  sqlite3_finalize(pStmt);
4151  sqlite3_snprintf(sizeof(zStmt), zStmt,
4152       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4153  sqlite3_exec(db, zStmt, 0, 0, 0);
4154}
4155
4156/*
4157** This is a different callback routine used for dumping the database.
4158** Each row received by this callback consists of a table name,
4159** the table type ("index" or "table") and SQL to create the table.
4160** This routine should print text sufficient to recreate the table.
4161*/
4162static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4163  int rc;
4164  const char *zTable;
4165  const char *zType;
4166  const char *zSql;
4167  ShellState *p = (ShellState *)pArg;
4168  int dataOnly;
4169  int noSys;
4170
4171  UNUSED_PARAMETER(azNotUsed);
4172  if( nArg!=3 || azArg==0 ) return 0;
4173  zTable = azArg[0];
4174  zType = azArg[1];
4175  zSql = azArg[2];
4176  if( zTable==0 ) return 0;
4177  if( zType==0 ) return 0;
4178  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4179  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4180
4181  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4182    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4183  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4184    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4185  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
4186    return 0;
4187  }else if( dataOnly ){
4188    /* no-op */
4189  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4190    char *zIns;
4191    if( !p->writableSchema ){
4192      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4193      p->writableSchema = 1;
4194    }
4195    zIns = sqlite3_mprintf(
4196       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4197       "VALUES('table','%q','%q',0,'%q');",
4198       zTable, zTable, zSql);
4199    shell_check_oom(zIns);
4200    utf8_printf(p->out, "%s\n", zIns);
4201    sqlite3_free(zIns);
4202    return 0;
4203  }else{
4204    printSchemaLine(p->out, zSql, ";\n");
4205  }
4206
4207  if( cli_strcmp(zType, "table")==0 ){
4208    ShellText sSelect;
4209    ShellText sTable;
4210    char **azCol;
4211    int i;
4212    char *savedDestTable;
4213    int savedMode;
4214
4215    azCol = tableColumnList(p, zTable);
4216    if( azCol==0 ){
4217      p->nErr++;
4218      return 0;
4219    }
4220
4221    /* Always quote the table name, even if it appears to be pure ascii,
4222    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
4223    initText(&sTable);
4224    appendText(&sTable, zTable, quoteChar(zTable));
4225    /* If preserving the rowid, add a column list after the table name.
4226    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4227    ** instead of the usual "INSERT INTO tab VALUES(...)".
4228    */
4229    if( azCol[0] ){
4230      appendText(&sTable, "(", 0);
4231      appendText(&sTable, azCol[0], 0);
4232      for(i=1; azCol[i]; i++){
4233        appendText(&sTable, ",", 0);
4234        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4235      }
4236      appendText(&sTable, ")", 0);
4237    }
4238
4239    /* Build an appropriate SELECT statement */
4240    initText(&sSelect);
4241    appendText(&sSelect, "SELECT ", 0);
4242    if( azCol[0] ){
4243      appendText(&sSelect, azCol[0], 0);
4244      appendText(&sSelect, ",", 0);
4245    }
4246    for(i=1; azCol[i]; i++){
4247      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4248      if( azCol[i+1] ){
4249        appendText(&sSelect, ",", 0);
4250      }
4251    }
4252    freeColumnList(azCol);
4253    appendText(&sSelect, " FROM ", 0);
4254    appendText(&sSelect, zTable, quoteChar(zTable));
4255
4256    savedDestTable = p->zDestTable;
4257    savedMode = p->mode;
4258    p->zDestTable = sTable.z;
4259    p->mode = p->cMode = MODE_Insert;
4260    rc = shell_exec(p, sSelect.z, 0);
4261    if( (rc&0xff)==SQLITE_CORRUPT ){
4262      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4263      toggleSelectOrder(p->db);
4264      shell_exec(p, sSelect.z, 0);
4265      toggleSelectOrder(p->db);
4266    }
4267    p->zDestTable = savedDestTable;
4268    p->mode = savedMode;
4269    freeText(&sTable);
4270    freeText(&sSelect);
4271    if( rc ) p->nErr++;
4272  }
4273  return 0;
4274}
4275
4276/*
4277** Run zQuery.  Use dump_callback() as the callback routine so that
4278** the contents of the query are output as SQL statements.
4279**
4280** If we get a SQLITE_CORRUPT error, rerun the query after appending
4281** "ORDER BY rowid DESC" to the end.
4282*/
4283static int run_schema_dump_query(
4284  ShellState *p,
4285  const char *zQuery
4286){
4287  int rc;
4288  char *zErr = 0;
4289  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4290  if( rc==SQLITE_CORRUPT ){
4291    char *zQ2;
4292    int len = strlen30(zQuery);
4293    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4294    if( zErr ){
4295      utf8_printf(p->out, "/****** %s ******/\n", zErr);
4296      sqlite3_free(zErr);
4297      zErr = 0;
4298    }
4299    zQ2 = malloc( len+100 );
4300    if( zQ2==0 ) return rc;
4301    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4302    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4303    if( rc ){
4304      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4305    }else{
4306      rc = SQLITE_CORRUPT;
4307    }
4308    sqlite3_free(zErr);
4309    free(zQ2);
4310  }
4311  return rc;
4312}
4313
4314/*
4315** Text of help messages.
4316**
4317** The help text for each individual command begins with a line that starts
4318** with ".".  Subsequent lines are supplemental information.
4319**
4320** There must be two or more spaces between the end of the command and the
4321** start of the description of what that command does.
4322*/
4323static const char *(azHelp[]) = {
4324#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4325  && !defined(SQLITE_SHELL_FIDDLE)
4326  ".archive ...             Manage SQL archives",
4327  "   Each command must have exactly one of the following options:",
4328  "     -c, --create               Create a new archive",
4329  "     -u, --update               Add or update files with changed mtime",
4330  "     -i, --insert               Like -u but always add even if unchanged",
4331  "     -r, --remove               Remove files from archive",
4332  "     -t, --list                 List contents of archive",
4333  "     -x, --extract              Extract files from archive",
4334  "   Optional arguments:",
4335  "     -v, --verbose              Print each filename as it is processed",
4336  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
4337  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
4338  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
4339  "     -g, --glob                 Use glob matching for names in archive",
4340  "     -n, --dryrun               Show the SQL that would have occurred",
4341  "   Examples:",
4342  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
4343  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
4344  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
4345  "   See also:",
4346  "      http://sqlite.org/cli.html#sqlite_archive_support",
4347#endif
4348#ifndef SQLITE_OMIT_AUTHORIZATION
4349  ".auth ON|OFF             Show authorizer callbacks",
4350#endif
4351#ifndef SQLITE_SHELL_FIDDLE
4352  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
4353  "   Options:",
4354  "       --append            Use the appendvfs",
4355  "       --async             Write to FILE without journal and fsync()",
4356#endif
4357  ".bail on|off             Stop after hitting an error.  Default OFF",
4358  ".binary on|off           Turn binary output on or off.  Default OFF",
4359#ifndef SQLITE_SHELL_FIDDLE
4360  ".cd DIRECTORY            Change the working directory to DIRECTORY",
4361#endif
4362  ".changes on|off          Show number of rows changed by SQL",
4363#ifndef SQLITE_SHELL_FIDDLE
4364  ".check GLOB              Fail if output since .testcase does not match",
4365  ".clone NEWDB             Clone data into NEWDB from the existing database",
4366#endif
4367  ".connection [close] [#]  Open or close an auxiliary database connection",
4368  ".databases               List names and files of attached databases",
4369  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4370#if SQLITE_SHELL_HAVE_RECOVER
4371  ".dbinfo ?DB?             Show status information about the database",
4372#endif
4373  ".dump ?OBJECTS?          Render database content as SQL",
4374  "   Options:",
4375  "     --data-only            Output only INSERT statements",
4376  "     --newlines             Allow unescaped newline characters in output",
4377  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4378  "     --preserve-rowids      Include ROWID values in the output",
4379  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4380  "   Additional LIKE patterns can be given in subsequent arguments",
4381  ".echo on|off             Turn command echo on or off",
4382  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4383  "   Other Modes:",
4384#ifdef SQLITE_DEBUG
4385  "      test                  Show raw EXPLAIN QUERY PLAN output",
4386  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4387#endif
4388  "      trigger               Like \"full\" but also show trigger bytecode",
4389#ifndef SQLITE_SHELL_FIDDLE
4390  ".excel                   Display the output of next command in spreadsheet",
4391  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4392#endif
4393#ifndef SQLITE_SHELL_FIDDLE
4394  ".exit ?CODE?             Exit this program with return-code CODE",
4395#endif
4396  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4397  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4398  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4399  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4400  "   --help                  Show CMD details",
4401  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4402  ".headers on|off          Turn display of headers on or off",
4403  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4404#ifndef SQLITE_SHELL_FIDDLE
4405  ".import FILE TABLE       Import data from FILE into TABLE",
4406  "   Options:",
4407  "     --ascii               Use \\037 and \\036 as column and row separators",
4408  "     --csv                 Use , and \\n as column and row separators",
4409  "     --skip N              Skip the first N rows of input",
4410  "     --schema S            Target table to be S.TABLE",
4411  "     -v                    \"Verbose\" - increase auxiliary output",
4412  "   Notes:",
4413  "     *  If TABLE does not exist, it is created.  The first row of input",
4414  "        determines the column names.",
4415  "     *  If neither --csv or --ascii are used, the input mode is derived",
4416  "        from the \".mode\" output mode",
4417  "     *  If FILE begins with \"|\" then it is a command that generates the",
4418  "        input text.",
4419#endif
4420#ifndef SQLITE_OMIT_TEST_CONTROL
4421  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4422#endif
4423  ".indexes ?TABLE?         Show names of indexes",
4424  "                           If TABLE is specified, only show indexes for",
4425  "                           tables matching TABLE using the LIKE operator.",
4426#ifdef SQLITE_ENABLE_IOTRACE
4427  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4428#endif
4429  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4430  ".lint OPTIONS            Report potential schema issues.",
4431  "     Options:",
4432  "        fkey-indexes     Find missing foreign key indexes",
4433#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4434  ".load FILE ?ENTRY?       Load an extension library",
4435#endif
4436#ifndef SQLITE_SHELL_FIDDLE
4437  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4438#endif
4439  ".mode MODE ?OPTIONS?     Set output mode",
4440  "   MODE is one of:",
4441  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
4442  "     box         Tables using unicode box-drawing characters",
4443  "     csv         Comma-separated values",
4444  "     column      Output in columns.  (See .width)",
4445  "     html        HTML <table> code",
4446  "     insert      SQL insert statements for TABLE",
4447  "     json        Results in a JSON array",
4448  "     line        One value per line",
4449  "     list        Values delimited by \"|\"",
4450  "     markdown    Markdown table format",
4451  "     qbox        Shorthand for \"box --wrap 60 --quote\"",
4452  "     quote       Escape answers as for SQL",
4453  "     table       ASCII-art table",
4454  "     tabs        Tab-separated values",
4455  "     tcl         TCL list elements",
4456  "   OPTIONS: (for columnar modes or insert mode):",
4457  "     --wrap N       Wrap output lines to no longer than N characters",
4458  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
4459  "     --ww           Shorthand for \"--wordwrap 1\"",
4460  "     --quote        Quote output text as SQL literals",
4461  "     --noquote      Do not quote output text",
4462  "     TABLE          The name of SQL table used for \"insert\" mode",
4463#ifndef SQLITE_SHELL_FIDDLE
4464  ".nonce STRING            Suspend safe mode for one command if nonce matches",
4465#endif
4466  ".nullvalue STRING        Use STRING in place of NULL values",
4467#ifndef SQLITE_SHELL_FIDDLE
4468  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4469  "     If FILE begins with '|' then open as a pipe",
4470  "       --bom  Put a UTF8 byte-order mark at the beginning",
4471  "       -e     Send output to the system text editor",
4472  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4473  /* Note that .open is (partially) available in WASM builds but is
4474  ** currently only intended to be used by the fiddle tool, not
4475  ** end users, so is "undocumented." */
4476  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4477  "     Options:",
4478  "        --append        Use appendvfs to append database to the end of FILE",
4479#endif
4480#ifndef SQLITE_OMIT_DESERIALIZE
4481  "        --deserialize   Load into memory using sqlite3_deserialize()",
4482  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4483  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4484#endif
4485  "        --new           Initialize FILE to an empty database",
4486  "        --nofollow      Do not follow symbolic links",
4487  "        --readonly      Open FILE readonly",
4488  "        --zip           FILE is a ZIP archive",
4489#ifndef SQLITE_SHELL_FIDDLE
4490  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4491  "   If FILE begins with '|' then open it as a pipe.",
4492  "   Options:",
4493  "     --bom                 Prefix output with a UTF8 byte-order mark",
4494  "     -e                    Send output to the system text editor",
4495  "     -x                    Send output as CSV to a spreadsheet",
4496#endif
4497  ".parameter CMD ...       Manage SQL parameter bindings",
4498  "   clear                   Erase all bindings",
4499  "   init                    Initialize the TEMP table that holds bindings",
4500  "   list                    List the current parameter bindings",
4501  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4502  "                           PARAMETER should start with one of: $ : @ ?",
4503  "   unset PARAMETER         Remove PARAMETER from the binding table",
4504  ".print STRING...         Print literal STRING",
4505#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4506  ".progress N              Invoke progress handler after every N opcodes",
4507  "   --limit N                 Interrupt after N progress callbacks",
4508  "   --once                    Do no more than one progress interrupt",
4509  "   --quiet|-q                No output except at interrupts",
4510  "   --reset                   Reset the count for each input and interrupt",
4511#endif
4512  ".prompt MAIN CONTINUE    Replace the standard prompts",
4513#ifndef SQLITE_SHELL_FIDDLE
4514  ".quit                    Exit this program",
4515  ".read FILE               Read input from FILE or command output",
4516  "    If FILE begins with \"|\", it is a command that generates the input.",
4517#endif
4518#if SQLITE_SHELL_HAVE_RECOVER
4519  ".recover                 Recover as much data as possible from corrupt db.",
4520  "   --freelist-corrupt       Assume the freelist is corrupt",
4521  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4522  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4523  "   --no-rowids              Do not attempt to recover rowid values",
4524  "                            that are not also INTEGER PRIMARY KEYs",
4525#endif
4526#ifndef SQLITE_SHELL_FIDDLE
4527  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4528  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
4529#endif
4530  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4531  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4532  "   Options:",
4533  "      --indent             Try to pretty-print the schema",
4534  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4535  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4536  "    Options:",
4537  "       --init               Create a new SELFTEST table",
4538  "       -v                   Verbose output",
4539  ".separator COL ?ROW?     Change the column and row separators",
4540#if defined(SQLITE_ENABLE_SESSION)
4541  ".session ?NAME? CMD ...  Create or control sessions",
4542  "   Subcommands:",
4543  "     attach TABLE             Attach TABLE",
4544  "     changeset FILE           Write a changeset into FILE",
4545  "     close                    Close one session",
4546  "     enable ?BOOLEAN?         Set or query the enable bit",
4547  "     filter GLOB...           Reject tables matching GLOBs",
4548  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4549  "     isempty                  Query whether the session is empty",
4550  "     list                     List currently open session names",
4551  "     open DB NAME             Open a new session on DB",
4552  "     patchset FILE            Write a patchset into FILE",
4553  "   If ?NAME? is omitted, the first defined session is used.",
4554#endif
4555  ".sha3sum ...             Compute a SHA3 hash of database content",
4556  "    Options:",
4557  "      --schema              Also hash the sqlite_schema table",
4558  "      --sha3-224            Use the sha3-224 algorithm",
4559  "      --sha3-256            Use the sha3-256 algorithm (default)",
4560  "      --sha3-384            Use the sha3-384 algorithm",
4561  "      --sha3-512            Use the sha3-512 algorithm",
4562  "    Any other argument is a LIKE pattern for tables to hash",
4563#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4564  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4565#endif
4566  ".show                    Show the current values for various settings",
4567  ".stats ?ARG?             Show stats or turn stats on or off",
4568  "   off                      Turn off automatic stat display",
4569  "   on                       Turn on automatic stat display",
4570  "   stmt                     Show statement stats",
4571  "   vmstep                   Show the virtual machine step count only",
4572#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4573  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4574#endif
4575  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4576#ifndef SQLITE_SHELL_FIDDLE
4577  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4578#endif
4579  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4580  "                           Run \".testctrl\" with no arguments for details",
4581  ".timeout MS              Try opening locked tables for MS milliseconds",
4582  ".timer on|off            Turn SQL timer on or off",
4583#ifndef SQLITE_OMIT_TRACE
4584  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4585  "    FILE                    Send output to FILE",
4586  "    stdout                  Send output to stdout",
4587  "    stderr                  Send output to stderr",
4588  "    off                     Disable tracing",
4589  "    --expanded              Expand query parameters",
4590#ifdef SQLITE_ENABLE_NORMALIZE
4591  "    --normalized            Normal the SQL statements",
4592#endif
4593  "    --plain                 Show SQL as it is input",
4594  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4595  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4596  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4597  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4598#endif /* SQLITE_OMIT_TRACE */
4599#ifdef SQLITE_DEBUG
4600  ".unmodule NAME ...       Unregister virtual table modules",
4601  "    --allexcept             Unregister everything except those named",
4602#endif
4603  ".vfsinfo ?AUX?           Information about the top-level VFS",
4604  ".vfslist                 List all available VFSes",
4605  ".vfsname ?AUX?           Print the name of the VFS stack",
4606  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4607  "     Negative values right-justify",
4608};
4609
4610/*
4611** Output help text.
4612**
4613** zPattern describes the set of commands for which help text is provided.
4614** If zPattern is NULL, then show all commands, but only give a one-line
4615** description of each.
4616**
4617** Return the number of matches.
4618*/
4619static int showHelp(FILE *out, const char *zPattern){
4620  int i = 0;
4621  int j = 0;
4622  int n = 0;
4623  char *zPat;
4624  if( zPattern==0
4625   || zPattern[0]=='0'
4626   || cli_strcmp(zPattern,"-a")==0
4627   || cli_strcmp(zPattern,"-all")==0
4628   || cli_strcmp(zPattern,"--all")==0
4629  ){
4630    /* Show all commands, but only one line per command */
4631    if( zPattern==0 ) zPattern = "";
4632    for(i=0; i<ArraySize(azHelp); i++){
4633      if( azHelp[i][0]=='.' || zPattern[0] ){
4634        utf8_printf(out, "%s\n", azHelp[i]);
4635        n++;
4636      }
4637    }
4638  }else{
4639    /* Look for commands that for which zPattern is an exact prefix */
4640    zPat = sqlite3_mprintf(".%s*", zPattern);
4641    shell_check_oom(zPat);
4642    for(i=0; i<ArraySize(azHelp); i++){
4643      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4644        utf8_printf(out, "%s\n", azHelp[i]);
4645        j = i+1;
4646        n++;
4647      }
4648    }
4649    sqlite3_free(zPat);
4650    if( n ){
4651      if( n==1 ){
4652        /* when zPattern is a prefix of exactly one command, then include the
4653        ** details of that command, which should begin at offset j */
4654        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4655          utf8_printf(out, "%s\n", azHelp[j]);
4656          j++;
4657        }
4658      }
4659      return n;
4660    }
4661    /* Look for commands that contain zPattern anywhere.  Show the complete
4662    ** text of all commands that match. */
4663    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4664    shell_check_oom(zPat);
4665    for(i=0; i<ArraySize(azHelp); i++){
4666      if( azHelp[i][0]=='.' ) j = i;
4667      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4668        utf8_printf(out, "%s\n", azHelp[j]);
4669        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4670          j++;
4671          utf8_printf(out, "%s\n", azHelp[j]);
4672        }
4673        i = j;
4674        n++;
4675      }
4676    }
4677    sqlite3_free(zPat);
4678  }
4679  return n;
4680}
4681
4682/* Forward reference */
4683static int process_input(ShellState *p);
4684
4685/*
4686** Read the content of file zName into memory obtained from sqlite3_malloc64()
4687** and return a pointer to the buffer. The caller is responsible for freeing
4688** the memory.
4689**
4690** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4691** read.
4692**
4693** For convenience, a nul-terminator byte is always appended to the data read
4694** from the file before the buffer is returned. This byte is not included in
4695** the final value of (*pnByte), if applicable.
4696**
4697** NULL is returned if any error is encountered. The final value of *pnByte
4698** is undefined in this case.
4699*/
4700static char *readFile(const char *zName, int *pnByte){
4701  FILE *in = fopen(zName, "rb");
4702  long nIn;
4703  size_t nRead;
4704  char *pBuf;
4705  if( in==0 ) return 0;
4706  fseek(in, 0, SEEK_END);
4707  nIn = ftell(in);
4708  rewind(in);
4709  pBuf = sqlite3_malloc64( nIn+1 );
4710  if( pBuf==0 ){ fclose(in); return 0; }
4711  nRead = fread(pBuf, nIn, 1, in);
4712  fclose(in);
4713  if( nRead!=1 ){
4714    sqlite3_free(pBuf);
4715    return 0;
4716  }
4717  pBuf[nIn] = 0;
4718  if( pnByte ) *pnByte = nIn;
4719  return pBuf;
4720}
4721
4722#if defined(SQLITE_ENABLE_SESSION)
4723/*
4724** Close a single OpenSession object and release all of its associated
4725** resources.
4726*/
4727static void session_close(OpenSession *pSession){
4728  int i;
4729  sqlite3session_delete(pSession->p);
4730  sqlite3_free(pSession->zName);
4731  for(i=0; i<pSession->nFilter; i++){
4732    sqlite3_free(pSession->azFilter[i]);
4733  }
4734  sqlite3_free(pSession->azFilter);
4735  memset(pSession, 0, sizeof(OpenSession));
4736}
4737#endif
4738
4739/*
4740** Close all OpenSession objects and release all associated resources.
4741*/
4742#if defined(SQLITE_ENABLE_SESSION)
4743static void session_close_all(ShellState *p, int i){
4744  int j;
4745  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4746  for(j=0; j<pAuxDb->nSession; j++){
4747    session_close(&pAuxDb->aSession[j]);
4748  }
4749  pAuxDb->nSession = 0;
4750}
4751#else
4752# define session_close_all(X,Y)
4753#endif
4754
4755/*
4756** Implementation of the xFilter function for an open session.  Omit
4757** any tables named by ".session filter" but let all other table through.
4758*/
4759#if defined(SQLITE_ENABLE_SESSION)
4760static int session_filter(void *pCtx, const char *zTab){
4761  OpenSession *pSession = (OpenSession*)pCtx;
4762  int i;
4763  for(i=0; i<pSession->nFilter; i++){
4764    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4765  }
4766  return 1;
4767}
4768#endif
4769
4770/*
4771** Try to deduce the type of file for zName based on its content.  Return
4772** one of the SHELL_OPEN_* constants.
4773**
4774** If the file does not exist or is empty but its name looks like a ZIP
4775** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4776** Otherwise, assume an ordinary database regardless of the filename if
4777** the type cannot be determined from content.
4778*/
4779int deduceDatabaseType(const char *zName, int dfltZip){
4780  FILE *f = fopen(zName, "rb");
4781  size_t n;
4782  int rc = SHELL_OPEN_UNSPEC;
4783  char zBuf[100];
4784  if( f==0 ){
4785    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4786       return SHELL_OPEN_ZIPFILE;
4787    }else{
4788       return SHELL_OPEN_NORMAL;
4789    }
4790  }
4791  n = fread(zBuf, 16, 1, f);
4792  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4793    fclose(f);
4794    return SHELL_OPEN_NORMAL;
4795  }
4796  fseek(f, -25, SEEK_END);
4797  n = fread(zBuf, 25, 1, f);
4798  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4799    rc = SHELL_OPEN_APPENDVFS;
4800  }else{
4801    fseek(f, -22, SEEK_END);
4802    n = fread(zBuf, 22, 1, f);
4803    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4804       && zBuf[3]==0x06 ){
4805      rc = SHELL_OPEN_ZIPFILE;
4806    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4807      rc = SHELL_OPEN_ZIPFILE;
4808    }
4809  }
4810  fclose(f);
4811  return rc;
4812}
4813
4814#ifndef SQLITE_OMIT_DESERIALIZE
4815/*
4816** Reconstruct an in-memory database using the output from the "dbtotxt"
4817** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4818** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4819*/
4820static unsigned char *readHexDb(ShellState *p, int *pnData){
4821  unsigned char *a = 0;
4822  int nLine;
4823  int n = 0;
4824  int pgsz = 0;
4825  int iOffset = 0;
4826  int j, k;
4827  int rc;
4828  FILE *in;
4829  const char *zDbFilename = p->pAuxDb->zDbFilename;
4830  unsigned int x[16];
4831  char zLine[1000];
4832  if( zDbFilename ){
4833    in = fopen(zDbFilename, "r");
4834    if( in==0 ){
4835      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4836      return 0;
4837    }
4838    nLine = 0;
4839  }else{
4840    in = p->in;
4841    nLine = p->lineno;
4842    if( in==0 ) in = stdin;
4843  }
4844  *pnData = 0;
4845  nLine++;
4846  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4847  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4848  if( rc!=2 ) goto readHexDb_error;
4849  if( n<0 ) goto readHexDb_error;
4850  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4851  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4852  a = sqlite3_malloc( n ? n : 1 );
4853  shell_check_oom(a);
4854  memset(a, 0, n);
4855  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4856    utf8_printf(stderr, "invalid pagesize\n");
4857    goto readHexDb_error;
4858  }
4859  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4860    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4861    if( rc==2 ){
4862      iOffset = k;
4863      continue;
4864    }
4865    if( cli_strncmp(zLine, "| end ", 6)==0 ){
4866      break;
4867    }
4868    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4869                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4870                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4871    if( rc==17 ){
4872      k = iOffset+j;
4873      if( k+16<=n && k>=0 ){
4874        int ii;
4875        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4876      }
4877    }
4878  }
4879  *pnData = n;
4880  if( in!=p->in ){
4881    fclose(in);
4882  }else{
4883    p->lineno = nLine;
4884  }
4885  return a;
4886
4887readHexDb_error:
4888  if( in!=p->in ){
4889    fclose(in);
4890  }else{
4891    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4892      nLine++;
4893      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
4894    }
4895    p->lineno = nLine;
4896  }
4897  sqlite3_free(a);
4898  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4899  return 0;
4900}
4901#endif /* SQLITE_OMIT_DESERIALIZE */
4902
4903/*
4904** Scalar function "shell_int32". The first argument to this function
4905** must be a blob. The second a non-negative integer. This function
4906** reads and returns a 32-bit big-endian integer from byte
4907** offset (4*<arg2>) of the blob.
4908*/
4909static void shellInt32(
4910  sqlite3_context *context,
4911  int argc,
4912  sqlite3_value **argv
4913){
4914  const unsigned char *pBlob;
4915  int nBlob;
4916  int iInt;
4917
4918  UNUSED_PARAMETER(argc);
4919  nBlob = sqlite3_value_bytes(argv[0]);
4920  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4921  iInt = sqlite3_value_int(argv[1]);
4922
4923  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4924    const unsigned char *a = &pBlob[iInt*4];
4925    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4926                       + ((sqlite3_int64)a[1]<<16)
4927                       + ((sqlite3_int64)a[2]<< 8)
4928                       + ((sqlite3_int64)a[3]<< 0);
4929    sqlite3_result_int64(context, iVal);
4930  }
4931}
4932
4933/*
4934** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4935** using "..." with internal double-quote characters doubled.
4936*/
4937static void shellIdQuote(
4938  sqlite3_context *context,
4939  int argc,
4940  sqlite3_value **argv
4941){
4942  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4943  UNUSED_PARAMETER(argc);
4944  if( zName ){
4945    char *z = sqlite3_mprintf("\"%w\"", zName);
4946    sqlite3_result_text(context, z, -1, sqlite3_free);
4947  }
4948}
4949
4950/*
4951** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4952*/
4953static void shellUSleepFunc(
4954  sqlite3_context *context,
4955  int argcUnused,
4956  sqlite3_value **argv
4957){
4958  int sleep = sqlite3_value_int(argv[0]);
4959  (void)argcUnused;
4960  sqlite3_sleep(sleep/1000);
4961  sqlite3_result_int(context, sleep);
4962}
4963
4964/*
4965** Scalar function "shell_escape_crnl" used by the .recover command.
4966** The argument passed to this function is the output of built-in
4967** function quote(). If the first character of the input is "'",
4968** indicating that the value passed to quote() was a text value,
4969** then this function searches the input for "\n" and "\r" characters
4970** and adds a wrapper similar to the following:
4971**
4972**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4973**
4974** Or, if the first character of the input is not "'", then a copy
4975** of the input is returned.
4976*/
4977static void shellEscapeCrnl(
4978  sqlite3_context *context,
4979  int argc,
4980  sqlite3_value **argv
4981){
4982  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4983  UNUSED_PARAMETER(argc);
4984  if( zText && zText[0]=='\'' ){
4985    i64 nText = sqlite3_value_bytes(argv[0]);
4986    i64 i;
4987    char zBuf1[20];
4988    char zBuf2[20];
4989    const char *zNL = 0;
4990    const char *zCR = 0;
4991    i64 nCR = 0;
4992    i64 nNL = 0;
4993
4994    for(i=0; zText[i]; i++){
4995      if( zNL==0 && zText[i]=='\n' ){
4996        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4997        nNL = strlen(zNL);
4998      }
4999      if( zCR==0 && zText[i]=='\r' ){
5000        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
5001        nCR = strlen(zCR);
5002      }
5003    }
5004
5005    if( zNL || zCR ){
5006      i64 iOut = 0;
5007      i64 nMax = (nNL > nCR) ? nNL : nCR;
5008      i64 nAlloc = nMax * nText + (nMax+64)*2;
5009      char *zOut = (char*)sqlite3_malloc64(nAlloc);
5010      if( zOut==0 ){
5011        sqlite3_result_error_nomem(context);
5012        return;
5013      }
5014
5015      if( zNL && zCR ){
5016        memcpy(&zOut[iOut], "replace(replace(", 16);
5017        iOut += 16;
5018      }else{
5019        memcpy(&zOut[iOut], "replace(", 8);
5020        iOut += 8;
5021      }
5022      for(i=0; zText[i]; i++){
5023        if( zText[i]=='\n' ){
5024          memcpy(&zOut[iOut], zNL, nNL);
5025          iOut += nNL;
5026        }else if( zText[i]=='\r' ){
5027          memcpy(&zOut[iOut], zCR, nCR);
5028          iOut += nCR;
5029        }else{
5030          zOut[iOut] = zText[i];
5031          iOut++;
5032        }
5033      }
5034
5035      if( zNL ){
5036        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5037        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
5038        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
5039      }
5040      if( zCR ){
5041        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
5042        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
5043        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
5044      }
5045
5046      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
5047      sqlite3_free(zOut);
5048      return;
5049    }
5050  }
5051
5052  sqlite3_result_value(context, argv[0]);
5053}
5054
5055/* Flags for open_db().
5056**
5057** The default behavior of open_db() is to exit(1) if the database fails to
5058** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5059** but still returns without calling exit.
5060**
5061** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5062** ZIP archive if the file does not exist or is empty and its name matches
5063** the *.zip pattern.
5064*/
5065#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
5066#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
5067
5068/*
5069** Make sure the database is open.  If it is not, then open it.  If
5070** the database fails to open, print an error message and exit.
5071*/
5072static void open_db(ShellState *p, int openFlags){
5073  if( p->db==0 ){
5074    const char *zDbFilename = p->pAuxDb->zDbFilename;
5075    if( p->openMode==SHELL_OPEN_UNSPEC ){
5076      if( zDbFilename==0 || zDbFilename[0]==0 ){
5077        p->openMode = SHELL_OPEN_NORMAL;
5078      }else{
5079        p->openMode = (u8)deduceDatabaseType(zDbFilename,
5080                             (openFlags & OPEN_DB_ZIPFILE)!=0);
5081      }
5082    }
5083    switch( p->openMode ){
5084      case SHELL_OPEN_APPENDVFS: {
5085        sqlite3_open_v2(zDbFilename, &p->db,
5086           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5087        break;
5088      }
5089      case SHELL_OPEN_HEXDB:
5090      case SHELL_OPEN_DESERIALIZE: {
5091        sqlite3_open(0, &p->db);
5092        break;
5093      }
5094      case SHELL_OPEN_ZIPFILE: {
5095        sqlite3_open(":memory:", &p->db);
5096        break;
5097      }
5098      case SHELL_OPEN_READONLY: {
5099        sqlite3_open_v2(zDbFilename, &p->db,
5100            SQLITE_OPEN_READONLY|p->openFlags, 0);
5101        break;
5102      }
5103      case SHELL_OPEN_UNSPEC:
5104      case SHELL_OPEN_NORMAL: {
5105        sqlite3_open_v2(zDbFilename, &p->db,
5106           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5107        break;
5108      }
5109    }
5110    globalDb = p->db;
5111    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5112      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5113          zDbFilename, sqlite3_errmsg(p->db));
5114      if( openFlags & OPEN_DB_KEEPALIVE ){
5115        sqlite3_open(":memory:", &p->db);
5116        return;
5117      }
5118      exit(1);
5119    }
5120#ifndef SQLITE_OMIT_LOAD_EXTENSION
5121    sqlite3_enable_load_extension(p->db, 1);
5122#endif
5123    sqlite3_shathree_init(p->db, 0, 0);
5124    sqlite3_uint_init(p->db, 0, 0);
5125    sqlite3_decimal_init(p->db, 0, 0);
5126    sqlite3_regexp_init(p->db, 0, 0);
5127    sqlite3_ieee_init(p->db, 0, 0);
5128    sqlite3_series_init(p->db, 0, 0);
5129#ifndef SQLITE_SHELL_FIDDLE
5130    sqlite3_fileio_init(p->db, 0, 0);
5131    sqlite3_completion_init(p->db, 0, 0);
5132#endif
5133#if SQLITE_SHELL_HAVE_RECOVER
5134    sqlite3_dbdata_init(p->db, 0, 0);
5135#endif
5136#ifdef SQLITE_HAVE_ZLIB
5137    if( !p->bSafeModePersist ){
5138      sqlite3_zipfile_init(p->db, 0, 0);
5139      sqlite3_sqlar_init(p->db, 0, 0);
5140    }
5141#endif
5142    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5143                            shellAddSchemaName, 0, 0);
5144    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5145                            shellModuleSchema, 0, 0);
5146    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5147                            shellPutsFunc, 0, 0);
5148    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
5149                            shellEscapeCrnl, 0, 0);
5150    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
5151                            shellInt32, 0, 0);
5152    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
5153                            shellIdQuote, 0, 0);
5154    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5155                            shellUSleepFunc, 0, 0);
5156#ifndef SQLITE_NOHAVE_SYSTEM
5157    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5158                            editFunc, 0, 0);
5159    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5160                            editFunc, 0, 0);
5161#endif
5162    if( p->openMode==SHELL_OPEN_ZIPFILE ){
5163      char *zSql = sqlite3_mprintf(
5164         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5165      shell_check_oom(zSql);
5166      sqlite3_exec(p->db, zSql, 0, 0, 0);
5167      sqlite3_free(zSql);
5168    }
5169#ifndef SQLITE_OMIT_DESERIALIZE
5170    else
5171    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5172      int rc;
5173      int nData = 0;
5174      unsigned char *aData;
5175      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5176        aData = (unsigned char*)readFile(zDbFilename, &nData);
5177      }else{
5178        aData = readHexDb(p, &nData);
5179        if( aData==0 ){
5180          return;
5181        }
5182      }
5183      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5184                   SQLITE_DESERIALIZE_RESIZEABLE |
5185                   SQLITE_DESERIALIZE_FREEONCLOSE);
5186      if( rc ){
5187        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5188      }
5189      if( p->szMax>0 ){
5190        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5191      }
5192    }
5193#endif
5194  }
5195  if( p->bSafeModePersist && p->db!=0 ){
5196    sqlite3_set_authorizer(p->db, safeModeAuth, p);
5197  }
5198}
5199
5200/*
5201** Attempt to close the databaes connection.  Report errors.
5202*/
5203void close_db(sqlite3 *db){
5204  int rc = sqlite3_close(db);
5205  if( rc ){
5206    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5207        rc, sqlite3_errmsg(db));
5208  }
5209}
5210
5211#if HAVE_READLINE || HAVE_EDITLINE
5212/*
5213** Readline completion callbacks
5214*/
5215static char *readline_completion_generator(const char *text, int state){
5216  static sqlite3_stmt *pStmt = 0;
5217  char *zRet;
5218  if( state==0 ){
5219    char *zSql;
5220    sqlite3_finalize(pStmt);
5221    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5222                           "  FROM completion(%Q) ORDER BY 1", text);
5223    shell_check_oom(zSql);
5224    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5225    sqlite3_free(zSql);
5226  }
5227  if( sqlite3_step(pStmt)==SQLITE_ROW ){
5228    const char *z = (const char*)sqlite3_column_text(pStmt,0);
5229    zRet = z ? strdup(z) : 0;
5230  }else{
5231    sqlite3_finalize(pStmt);
5232    pStmt = 0;
5233    zRet = 0;
5234  }
5235  return zRet;
5236}
5237static char **readline_completion(const char *zText, int iStart, int iEnd){
5238  rl_attempted_completion_over = 1;
5239  return rl_completion_matches(zText, readline_completion_generator);
5240}
5241
5242#elif HAVE_LINENOISE
5243/*
5244** Linenoise completion callback
5245*/
5246static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5247  i64 nLine = strlen(zLine);
5248  i64 i, iStart;
5249  sqlite3_stmt *pStmt = 0;
5250  char *zSql;
5251  char zBuf[1000];
5252
5253  if( nLine>sizeof(zBuf)-30 ) return;
5254  if( zLine[0]=='.' || zLine[0]=='#') return;
5255  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5256  if( i==nLine-1 ) return;
5257  iStart = i+1;
5258  memcpy(zBuf, zLine, iStart);
5259  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5260                         "  FROM completion(%Q,%Q) ORDER BY 1",
5261                         &zLine[iStart], zLine);
5262  shell_check_oom(zSql);
5263  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5264  sqlite3_free(zSql);
5265  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5266  while( sqlite3_step(pStmt)==SQLITE_ROW ){
5267    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5268    int nCompletion = sqlite3_column_bytes(pStmt, 0);
5269    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){
5270      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5271      linenoiseAddCompletion(lc, zBuf);
5272    }
5273  }
5274  sqlite3_finalize(pStmt);
5275}
5276#endif
5277
5278/*
5279** Do C-language style dequoting.
5280**
5281**    \a    -> alarm
5282**    \b    -> backspace
5283**    \t    -> tab
5284**    \n    -> newline
5285**    \v    -> vertical tab
5286**    \f    -> form feed
5287**    \r    -> carriage return
5288**    \s    -> space
5289**    \"    -> "
5290**    \'    -> '
5291**    \\    -> backslash
5292**    \NNN  -> ascii character NNN in octal
5293*/
5294static void resolve_backslashes(char *z){
5295  int i, j;
5296  char c;
5297  while( *z && *z!='\\' ) z++;
5298  for(i=j=0; (c = z[i])!=0; i++, j++){
5299    if( c=='\\' && z[i+1]!=0 ){
5300      c = z[++i];
5301      if( c=='a' ){
5302        c = '\a';
5303      }else if( c=='b' ){
5304        c = '\b';
5305      }else if( c=='t' ){
5306        c = '\t';
5307      }else if( c=='n' ){
5308        c = '\n';
5309      }else if( c=='v' ){
5310        c = '\v';
5311      }else if( c=='f' ){
5312        c = '\f';
5313      }else if( c=='r' ){
5314        c = '\r';
5315      }else if( c=='"' ){
5316        c = '"';
5317      }else if( c=='\'' ){
5318        c = '\'';
5319      }else if( c=='\\' ){
5320        c = '\\';
5321      }else if( c>='0' && c<='7' ){
5322        c -= '0';
5323        if( z[i+1]>='0' && z[i+1]<='7' ){
5324          i++;
5325          c = (c<<3) + z[i] - '0';
5326          if( z[i+1]>='0' && z[i+1]<='7' ){
5327            i++;
5328            c = (c<<3) + z[i] - '0';
5329          }
5330        }
5331      }
5332    }
5333    z[j] = c;
5334  }
5335  if( j<i ) z[j] = 0;
5336}
5337
5338/*
5339** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
5340** for TRUE and FALSE.  Return the integer value if appropriate.
5341*/
5342static int booleanValue(const char *zArg){
5343  int i;
5344  if( zArg[0]=='0' && zArg[1]=='x' ){
5345    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5346  }else{
5347    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5348  }
5349  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5350  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5351    return 1;
5352  }
5353  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5354    return 0;
5355  }
5356  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5357          zArg);
5358  return 0;
5359}
5360
5361/*
5362** Set or clear a shell flag according to a boolean value.
5363*/
5364static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5365  if( booleanValue(zArg) ){
5366    ShellSetFlag(p, mFlag);
5367  }else{
5368    ShellClearFlag(p, mFlag);
5369  }
5370}
5371
5372/*
5373** Close an output file, assuming it is not stderr or stdout
5374*/
5375static void output_file_close(FILE *f){
5376  if( f && f!=stdout && f!=stderr ) fclose(f);
5377}
5378
5379/*
5380** Try to open an output file.   The names "stdout" and "stderr" are
5381** recognized and do the right thing.  NULL is returned if the output
5382** filename is "off".
5383*/
5384static FILE *output_file_open(const char *zFile, int bTextMode){
5385  FILE *f;
5386  if( cli_strcmp(zFile,"stdout")==0 ){
5387    f = stdout;
5388  }else if( cli_strcmp(zFile, "stderr")==0 ){
5389    f = stderr;
5390  }else if( cli_strcmp(zFile, "off")==0 ){
5391    f = 0;
5392  }else{
5393    f = fopen(zFile, bTextMode ? "w" : "wb");
5394    if( f==0 ){
5395      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5396    }
5397  }
5398  return f;
5399}
5400
5401#ifndef SQLITE_OMIT_TRACE
5402/*
5403** A routine for handling output from sqlite3_trace().
5404*/
5405static int sql_trace_callback(
5406  unsigned mType,         /* The trace type */
5407  void *pArg,             /* The ShellState pointer */
5408  void *pP,               /* Usually a pointer to sqlite_stmt */
5409  void *pX                /* Auxiliary output */
5410){
5411  ShellState *p = (ShellState*)pArg;
5412  sqlite3_stmt *pStmt;
5413  const char *zSql;
5414  i64 nSql;
5415  if( p->traceOut==0 ) return 0;
5416  if( mType==SQLITE_TRACE_CLOSE ){
5417    utf8_printf(p->traceOut, "-- closing database connection\n");
5418    return 0;
5419  }
5420  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5421    zSql = (const char*)pX;
5422  }else{
5423    pStmt = (sqlite3_stmt*)pP;
5424    switch( p->eTraceType ){
5425      case SHELL_TRACE_EXPANDED: {
5426        zSql = sqlite3_expanded_sql(pStmt);
5427        break;
5428      }
5429#ifdef SQLITE_ENABLE_NORMALIZE
5430      case SHELL_TRACE_NORMALIZED: {
5431        zSql = sqlite3_normalized_sql(pStmt);
5432        break;
5433      }
5434#endif
5435      default: {
5436        zSql = sqlite3_sql(pStmt);
5437        break;
5438      }
5439    }
5440  }
5441  if( zSql==0 ) return 0;
5442  nSql = strlen(zSql);
5443  if( nSql>1000000000 ) nSql = 1000000000;
5444  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5445  switch( mType ){
5446    case SQLITE_TRACE_ROW:
5447    case SQLITE_TRACE_STMT: {
5448      utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5449      break;
5450    }
5451    case SQLITE_TRACE_PROFILE: {
5452      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5453      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5454      break;
5455    }
5456  }
5457  return 0;
5458}
5459#endif
5460
5461/*
5462** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5463** a useful spot to set a debugger breakpoint.
5464*/
5465static void test_breakpoint(void){
5466  static int nCall = 0;
5467  nCall++;
5468}
5469
5470/*
5471** An object used to read a CSV and other files for import.
5472*/
5473typedef struct ImportCtx ImportCtx;
5474struct ImportCtx {
5475  const char *zFile;  /* Name of the input file */
5476  FILE *in;           /* Read the CSV text from this input stream */
5477  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5478  char *z;            /* Accumulated text for a field */
5479  int n;              /* Number of bytes in z */
5480  int nAlloc;         /* Space allocated for z[] */
5481  int nLine;          /* Current line number */
5482  int nRow;           /* Number of rows imported */
5483  int nErr;           /* Number of errors encountered */
5484  int bNotFirst;      /* True if one or more bytes already read */
5485  int cTerm;          /* Character that terminated the most recent field */
5486  int cColSep;        /* The column separator character.  (Usually ",") */
5487  int cRowSep;        /* The row separator character.  (Usually "\n") */
5488};
5489
5490/* Clean up resourced used by an ImportCtx */
5491static void import_cleanup(ImportCtx *p){
5492  if( p->in!=0 && p->xCloser!=0 ){
5493    p->xCloser(p->in);
5494    p->in = 0;
5495  }
5496  sqlite3_free(p->z);
5497  p->z = 0;
5498}
5499
5500/* Append a single byte to z[] */
5501static void import_append_char(ImportCtx *p, int c){
5502  if( p->n+1>=p->nAlloc ){
5503    p->nAlloc += p->nAlloc + 100;
5504    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5505    shell_check_oom(p->z);
5506  }
5507  p->z[p->n++] = (char)c;
5508}
5509
5510/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5511** with the option of having a separator other than ",".
5512**
5513**   +  Input comes from p->in.
5514**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5515**      from sqlite3_malloc64().
5516**   +  Use p->cSep as the column separator.  The default is ",".
5517**   +  Use p->rSep as the row separator.  The default is "\n".
5518**   +  Keep track of the line number in p->nLine.
5519**   +  Store the character that terminates the field in p->cTerm.  Store
5520**      EOF on end-of-file.
5521**   +  Report syntax errors on stderr
5522*/
5523static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5524  int c;
5525  int cSep = p->cColSep;
5526  int rSep = p->cRowSep;
5527  p->n = 0;
5528  c = fgetc(p->in);
5529  if( c==EOF || seenInterrupt ){
5530    p->cTerm = EOF;
5531    return 0;
5532  }
5533  if( c=='"' ){
5534    int pc, ppc;
5535    int startLine = p->nLine;
5536    int cQuote = c;
5537    pc = ppc = 0;
5538    while( 1 ){
5539      c = fgetc(p->in);
5540      if( c==rSep ) p->nLine++;
5541      if( c==cQuote ){
5542        if( pc==cQuote ){
5543          pc = 0;
5544          continue;
5545        }
5546      }
5547      if( (c==cSep && pc==cQuote)
5548       || (c==rSep && pc==cQuote)
5549       || (c==rSep && pc=='\r' && ppc==cQuote)
5550       || (c==EOF && pc==cQuote)
5551      ){
5552        do{ p->n--; }while( p->z[p->n]!=cQuote );
5553        p->cTerm = c;
5554        break;
5555      }
5556      if( pc==cQuote && c!='\r' ){
5557        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5558                p->zFile, p->nLine, cQuote);
5559      }
5560      if( c==EOF ){
5561        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5562                p->zFile, startLine, cQuote);
5563        p->cTerm = c;
5564        break;
5565      }
5566      import_append_char(p, c);
5567      ppc = pc;
5568      pc = c;
5569    }
5570  }else{
5571    /* If this is the first field being parsed and it begins with the
5572    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5573    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5574      import_append_char(p, c);
5575      c = fgetc(p->in);
5576      if( (c&0xff)==0xbb ){
5577        import_append_char(p, c);
5578        c = fgetc(p->in);
5579        if( (c&0xff)==0xbf ){
5580          p->bNotFirst = 1;
5581          p->n = 0;
5582          return csv_read_one_field(p);
5583        }
5584      }
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      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5593    }
5594    p->cTerm = c;
5595  }
5596  if( p->z ) p->z[p->n] = 0;
5597  p->bNotFirst = 1;
5598  return p->z;
5599}
5600
5601/* Read a single field of ASCII delimited text.
5602**
5603**   +  Input comes from p->in.
5604**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5605**      from sqlite3_malloc64().
5606**   +  Use p->cSep as the column separator.  The default is "\x1F".
5607**   +  Use p->rSep as the row separator.  The default is "\x1E".
5608**   +  Keep track of the row number in p->nLine.
5609**   +  Store the character that terminates the field in p->cTerm.  Store
5610**      EOF on end-of-file.
5611**   +  Report syntax errors on stderr
5612*/
5613static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5614  int c;
5615  int cSep = p->cColSep;
5616  int rSep = p->cRowSep;
5617  p->n = 0;
5618  c = fgetc(p->in);
5619  if( c==EOF || seenInterrupt ){
5620    p->cTerm = EOF;
5621    return 0;
5622  }
5623  while( c!=EOF && c!=cSep && c!=rSep ){
5624    import_append_char(p, c);
5625    c = fgetc(p->in);
5626  }
5627  if( c==rSep ){
5628    p->nLine++;
5629  }
5630  p->cTerm = c;
5631  if( p->z ) p->z[p->n] = 0;
5632  return p->z;
5633}
5634
5635/*
5636** Try to transfer data for table zTable.  If an error is seen while
5637** moving forward, try to go backwards.  The backwards movement won't
5638** work for WITHOUT ROWID tables.
5639*/
5640static void tryToCloneData(
5641  ShellState *p,
5642  sqlite3 *newDb,
5643  const char *zTable
5644){
5645  sqlite3_stmt *pQuery = 0;
5646  sqlite3_stmt *pInsert = 0;
5647  char *zQuery = 0;
5648  char *zInsert = 0;
5649  int rc;
5650  int i, j, n;
5651  int nTable = strlen30(zTable);
5652  int k = 0;
5653  int cnt = 0;
5654  const int spinRate = 10000;
5655
5656  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5657  shell_check_oom(zQuery);
5658  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5659  if( rc ){
5660    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5661            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5662            zQuery);
5663    goto end_data_xfer;
5664  }
5665  n = sqlite3_column_count(pQuery);
5666  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5667  shell_check_oom(zInsert);
5668  sqlite3_snprintf(200+nTable,zInsert,
5669                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5670  i = strlen30(zInsert);
5671  for(j=1; j<n; j++){
5672    memcpy(zInsert+i, ",?", 2);
5673    i += 2;
5674  }
5675  memcpy(zInsert+i, ");", 3);
5676  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5677  if( rc ){
5678    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5679            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5680            zQuery);
5681    goto end_data_xfer;
5682  }
5683  for(k=0; k<2; k++){
5684    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5685      for(i=0; i<n; i++){
5686        switch( sqlite3_column_type(pQuery, i) ){
5687          case SQLITE_NULL: {
5688            sqlite3_bind_null(pInsert, i+1);
5689            break;
5690          }
5691          case SQLITE_INTEGER: {
5692            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5693            break;
5694          }
5695          case SQLITE_FLOAT: {
5696            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5697            break;
5698          }
5699          case SQLITE_TEXT: {
5700            sqlite3_bind_text(pInsert, i+1,
5701                             (const char*)sqlite3_column_text(pQuery,i),
5702                             -1, SQLITE_STATIC);
5703            break;
5704          }
5705          case SQLITE_BLOB: {
5706            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5707                                            sqlite3_column_bytes(pQuery,i),
5708                                            SQLITE_STATIC);
5709            break;
5710          }
5711        }
5712      } /* End for */
5713      rc = sqlite3_step(pInsert);
5714      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5715        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5716                        sqlite3_errmsg(newDb));
5717      }
5718      sqlite3_reset(pInsert);
5719      cnt++;
5720      if( (cnt%spinRate)==0 ){
5721        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5722        fflush(stdout);
5723      }
5724    } /* End while */
5725    if( rc==SQLITE_DONE ) break;
5726    sqlite3_finalize(pQuery);
5727    sqlite3_free(zQuery);
5728    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5729                             zTable);
5730    shell_check_oom(zQuery);
5731    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5732    if( rc ){
5733      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5734      break;
5735    }
5736  } /* End for(k=0...) */
5737
5738end_data_xfer:
5739  sqlite3_finalize(pQuery);
5740  sqlite3_finalize(pInsert);
5741  sqlite3_free(zQuery);
5742  sqlite3_free(zInsert);
5743}
5744
5745
5746/*
5747** Try to transfer all rows of the schema that match zWhere.  For
5748** each row, invoke xForEach() on the object defined by that row.
5749** If an error is encountered while moving forward through the
5750** sqlite_schema table, try again moving backwards.
5751*/
5752static void tryToCloneSchema(
5753  ShellState *p,
5754  sqlite3 *newDb,
5755  const char *zWhere,
5756  void (*xForEach)(ShellState*,sqlite3*,const char*)
5757){
5758  sqlite3_stmt *pQuery = 0;
5759  char *zQuery = 0;
5760  int rc;
5761  const unsigned char *zName;
5762  const unsigned char *zSql;
5763  char *zErrMsg = 0;
5764
5765  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5766                           " WHERE %s", zWhere);
5767  shell_check_oom(zQuery);
5768  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5769  if( rc ){
5770    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5771                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5772                    zQuery);
5773    goto end_schema_xfer;
5774  }
5775  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5776    zName = sqlite3_column_text(pQuery, 0);
5777    zSql = sqlite3_column_text(pQuery, 1);
5778    if( zName==0 || zSql==0 ) continue;
5779    printf("%s... ", zName); fflush(stdout);
5780    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5781    if( zErrMsg ){
5782      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5783      sqlite3_free(zErrMsg);
5784      zErrMsg = 0;
5785    }
5786    if( xForEach ){
5787      xForEach(p, newDb, (const char*)zName);
5788    }
5789    printf("done\n");
5790  }
5791  if( rc!=SQLITE_DONE ){
5792    sqlite3_finalize(pQuery);
5793    sqlite3_free(zQuery);
5794    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5795                             " WHERE %s ORDER BY rowid DESC", zWhere);
5796    shell_check_oom(zQuery);
5797    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5798    if( rc ){
5799      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5800                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5801                      zQuery);
5802      goto end_schema_xfer;
5803    }
5804    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5805      zName = sqlite3_column_text(pQuery, 0);
5806      zSql = sqlite3_column_text(pQuery, 1);
5807      if( zName==0 || zSql==0 ) continue;
5808      printf("%s... ", zName); fflush(stdout);
5809      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5810      if( zErrMsg ){
5811        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5812        sqlite3_free(zErrMsg);
5813        zErrMsg = 0;
5814      }
5815      if( xForEach ){
5816        xForEach(p, newDb, (const char*)zName);
5817      }
5818      printf("done\n");
5819    }
5820  }
5821end_schema_xfer:
5822  sqlite3_finalize(pQuery);
5823  sqlite3_free(zQuery);
5824}
5825
5826/*
5827** Open a new database file named "zNewDb".  Try to recover as much information
5828** as possible out of the main database (which might be corrupt) and write it
5829** into zNewDb.
5830*/
5831static void tryToClone(ShellState *p, const char *zNewDb){
5832  int rc;
5833  sqlite3 *newDb = 0;
5834  if( access(zNewDb,0)==0 ){
5835    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5836    return;
5837  }
5838  rc = sqlite3_open(zNewDb, &newDb);
5839  if( rc ){
5840    utf8_printf(stderr, "Cannot create output database: %s\n",
5841            sqlite3_errmsg(newDb));
5842  }else{
5843    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5844    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5845    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5846    tryToCloneSchema(p, newDb, "type!='table'", 0);
5847    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5848    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5849  }
5850  close_db(newDb);
5851}
5852
5853/*
5854** Change the output file back to stdout.
5855**
5856** If the p->doXdgOpen flag is set, that means the output was being
5857** redirected to a temporary file named by p->zTempFile.  In that case,
5858** launch start/open/xdg-open on that temporary file.
5859*/
5860static void output_reset(ShellState *p){
5861  if( p->outfile[0]=='|' ){
5862#ifndef SQLITE_OMIT_POPEN
5863    pclose(p->out);
5864#endif
5865  }else{
5866    output_file_close(p->out);
5867#ifndef SQLITE_NOHAVE_SYSTEM
5868    if( p->doXdgOpen ){
5869      const char *zXdgOpenCmd =
5870#if defined(_WIN32)
5871      "start";
5872#elif defined(__APPLE__)
5873      "open";
5874#else
5875      "xdg-open";
5876#endif
5877      char *zCmd;
5878      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5879      if( system(zCmd) ){
5880        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5881      }else{
5882        /* Give the start/open/xdg-open command some time to get
5883        ** going before we continue, and potential delete the
5884        ** p->zTempFile data file out from under it */
5885        sqlite3_sleep(2000);
5886      }
5887      sqlite3_free(zCmd);
5888      outputModePop(p);
5889      p->doXdgOpen = 0;
5890    }
5891#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5892  }
5893  p->outfile[0] = 0;
5894  p->out = stdout;
5895}
5896
5897/*
5898** Run an SQL command and return the single integer result.
5899*/
5900static int db_int(sqlite3 *db, const char *zSql){
5901  sqlite3_stmt *pStmt;
5902  int res = 0;
5903  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
5904  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5905    res = sqlite3_column_int(pStmt,0);
5906  }
5907  sqlite3_finalize(pStmt);
5908  return res;
5909}
5910
5911#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
5912/*
5913** Convert a 2-byte or 4-byte big-endian integer into a native integer
5914*/
5915static unsigned int get2byteInt(unsigned char *a){
5916  return (a[0]<<8) + a[1];
5917}
5918static unsigned int get4byteInt(unsigned char *a){
5919  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5920}
5921
5922/*
5923** Implementation of the ".dbinfo" command.
5924**
5925** Return 1 on error, 2 to exit, and 0 otherwise.
5926*/
5927static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5928  static const struct { const char *zName; int ofst; } aField[] = {
5929     { "file change counter:",  24  },
5930     { "database page count:",  28  },
5931     { "freelist page count:",  36  },
5932     { "schema cookie:",        40  },
5933     { "schema format:",        44  },
5934     { "default cache size:",   48  },
5935     { "autovacuum top root:",  52  },
5936     { "incremental vacuum:",   64  },
5937     { "text encoding:",        56  },
5938     { "user version:",         60  },
5939     { "application id:",       68  },
5940     { "software version:",     96  },
5941  };
5942  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5943     { "number of tables:",
5944       "SELECT count(*) FROM %s WHERE type='table'" },
5945     { "number of indexes:",
5946       "SELECT count(*) FROM %s WHERE type='index'" },
5947     { "number of triggers:",
5948       "SELECT count(*) FROM %s WHERE type='trigger'" },
5949     { "number of views:",
5950       "SELECT count(*) FROM %s WHERE type='view'" },
5951     { "schema size:",
5952       "SELECT total(length(sql)) FROM %s" },
5953  };
5954  int i, rc;
5955  unsigned iDataVersion;
5956  char *zSchemaTab;
5957  char *zDb = nArg>=2 ? azArg[1] : "main";
5958  sqlite3_stmt *pStmt = 0;
5959  unsigned char aHdr[100];
5960  open_db(p, 0);
5961  if( p->db==0 ) return 1;
5962  rc = sqlite3_prepare_v2(p->db,
5963             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5964             -1, &pStmt, 0);
5965  if( rc ){
5966    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5967    sqlite3_finalize(pStmt);
5968    return 1;
5969  }
5970  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5971  if( sqlite3_step(pStmt)==SQLITE_ROW
5972   && sqlite3_column_bytes(pStmt,0)>100
5973  ){
5974    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5975    sqlite3_finalize(pStmt);
5976  }else{
5977    raw_printf(stderr, "unable to read database header\n");
5978    sqlite3_finalize(pStmt);
5979    return 1;
5980  }
5981  i = get2byteInt(aHdr+16);
5982  if( i==1 ) i = 65536;
5983  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5984  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5985  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5986  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5987  for(i=0; i<ArraySize(aField); i++){
5988    int ofst = aField[i].ofst;
5989    unsigned int val = get4byteInt(aHdr + ofst);
5990    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5991    switch( ofst ){
5992      case 56: {
5993        if( val==1 ) raw_printf(p->out, " (utf8)");
5994        if( val==2 ) raw_printf(p->out, " (utf16le)");
5995        if( val==3 ) raw_printf(p->out, " (utf16be)");
5996      }
5997    }
5998    raw_printf(p->out, "\n");
5999  }
6000  if( zDb==0 ){
6001    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
6002  }else if( cli_strcmp(zDb,"temp")==0 ){
6003    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
6004  }else{
6005    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
6006  }
6007  for(i=0; i<ArraySize(aQuery); i++){
6008    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
6009    int val = db_int(p->db, zSql);
6010    sqlite3_free(zSql);
6011    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
6012  }
6013  sqlite3_free(zSchemaTab);
6014  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
6015  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
6016  return 0;
6017}
6018#endif /* SQLITE_SHELL_HAVE_RECOVER */
6019
6020/*
6021** Print the current sqlite3_errmsg() value to stderr and return 1.
6022*/
6023static int shellDatabaseError(sqlite3 *db){
6024  const char *zErr = sqlite3_errmsg(db);
6025  utf8_printf(stderr, "Error: %s\n", zErr);
6026  return 1;
6027}
6028
6029/*
6030** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
6031** if they match and FALSE (0) if they do not match.
6032**
6033** Globbing rules:
6034**
6035**      '*'       Matches any sequence of zero or more characters.
6036**
6037**      '?'       Matches exactly one character.
6038**
6039**     [...]      Matches one character from the enclosed list of
6040**                characters.
6041**
6042**     [^...]     Matches one character not in the enclosed list.
6043**
6044**      '#'       Matches any sequence of one or more digits with an
6045**                optional + or - sign in front
6046**
6047**      ' '       Any span of whitespace matches any other span of
6048**                whitespace.
6049**
6050** Extra whitespace at the end of z[] is ignored.
6051*/
6052static int testcase_glob(const char *zGlob, const char *z){
6053  int c, c2;
6054  int invert;
6055  int seen;
6056
6057  while( (c = (*(zGlob++)))!=0 ){
6058    if( IsSpace(c) ){
6059      if( !IsSpace(*z) ) return 0;
6060      while( IsSpace(*zGlob) ) zGlob++;
6061      while( IsSpace(*z) ) z++;
6062    }else if( c=='*' ){
6063      while( (c=(*(zGlob++))) == '*' || c=='?' ){
6064        if( c=='?' && (*(z++))==0 ) return 0;
6065      }
6066      if( c==0 ){
6067        return 1;
6068      }else if( c=='[' ){
6069        while( *z && testcase_glob(zGlob-1,z)==0 ){
6070          z++;
6071        }
6072        return (*z)!=0;
6073      }
6074      while( (c2 = (*(z++)))!=0 ){
6075        while( c2!=c ){
6076          c2 = *(z++);
6077          if( c2==0 ) return 0;
6078        }
6079        if( testcase_glob(zGlob,z) ) return 1;
6080      }
6081      return 0;
6082    }else if( c=='?' ){
6083      if( (*(z++))==0 ) return 0;
6084    }else if( c=='[' ){
6085      int prior_c = 0;
6086      seen = 0;
6087      invert = 0;
6088      c = *(z++);
6089      if( c==0 ) return 0;
6090      c2 = *(zGlob++);
6091      if( c2=='^' ){
6092        invert = 1;
6093        c2 = *(zGlob++);
6094      }
6095      if( c2==']' ){
6096        if( c==']' ) seen = 1;
6097        c2 = *(zGlob++);
6098      }
6099      while( c2 && c2!=']' ){
6100        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6101          c2 = *(zGlob++);
6102          if( c>=prior_c && c<=c2 ) seen = 1;
6103          prior_c = 0;
6104        }else{
6105          if( c==c2 ){
6106            seen = 1;
6107          }
6108          prior_c = c2;
6109        }
6110        c2 = *(zGlob++);
6111      }
6112      if( c2==0 || (seen ^ invert)==0 ) return 0;
6113    }else if( c=='#' ){
6114      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6115      if( !IsDigit(z[0]) ) return 0;
6116      z++;
6117      while( IsDigit(z[0]) ){ z++; }
6118    }else{
6119      if( c!=(*(z++)) ) return 0;
6120    }
6121  }
6122  while( IsSpace(*z) ){ z++; }
6123  return *z==0;
6124}
6125
6126
6127/*
6128** Compare the string as a command-line option with either one or two
6129** initial "-" characters.
6130*/
6131static int optionMatch(const char *zStr, const char *zOpt){
6132  if( zStr[0]!='-' ) return 0;
6133  zStr++;
6134  if( zStr[0]=='-' ) zStr++;
6135  return cli_strcmp(zStr, zOpt)==0;
6136}
6137
6138/*
6139** Delete a file.
6140*/
6141int shellDeleteFile(const char *zFilename){
6142  int rc;
6143#ifdef _WIN32
6144  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6145  rc = _wunlink(z);
6146  sqlite3_free(z);
6147#else
6148  rc = unlink(zFilename);
6149#endif
6150  return rc;
6151}
6152
6153/*
6154** Try to delete the temporary file (if there is one) and free the
6155** memory used to hold the name of the temp file.
6156*/
6157static void clearTempFile(ShellState *p){
6158  if( p->zTempFile==0 ) return;
6159  if( p->doXdgOpen ) return;
6160  if( shellDeleteFile(p->zTempFile) ) return;
6161  sqlite3_free(p->zTempFile);
6162  p->zTempFile = 0;
6163}
6164
6165/*
6166** Create a new temp file name with the given suffix.
6167*/
6168static void newTempFile(ShellState *p, const char *zSuffix){
6169  clearTempFile(p);
6170  sqlite3_free(p->zTempFile);
6171  p->zTempFile = 0;
6172  if( p->db ){
6173    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6174  }
6175  if( p->zTempFile==0 ){
6176    /* If p->db is an in-memory database then the TEMPFILENAME file-control
6177    ** will not work and we will need to fallback to guessing */
6178    char *zTemp;
6179    sqlite3_uint64 r;
6180    sqlite3_randomness(sizeof(r), &r);
6181    zTemp = getenv("TEMP");
6182    if( zTemp==0 ) zTemp = getenv("TMP");
6183    if( zTemp==0 ){
6184#ifdef _WIN32
6185      zTemp = "\\tmp";
6186#else
6187      zTemp = "/tmp";
6188#endif
6189    }
6190    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6191  }else{
6192    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6193  }
6194  shell_check_oom(p->zTempFile);
6195}
6196
6197
6198/*
6199** The implementation of SQL scalar function fkey_collate_clause(), used
6200** by the ".lint fkey-indexes" command. This scalar function is always
6201** called with four arguments - the parent table name, the parent column name,
6202** the child table name and the child column name.
6203**
6204**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6205**
6206** If either of the named tables or columns do not exist, this function
6207** returns an empty string. An empty string is also returned if both tables
6208** and columns exist but have the same default collation sequence. Or,
6209** if both exist but the default collation sequences are different, this
6210** function returns the string " COLLATE <parent-collation>", where
6211** <parent-collation> is the default collation sequence of the parent column.
6212*/
6213static void shellFkeyCollateClause(
6214  sqlite3_context *pCtx,
6215  int nVal,
6216  sqlite3_value **apVal
6217){
6218  sqlite3 *db = sqlite3_context_db_handle(pCtx);
6219  const char *zParent;
6220  const char *zParentCol;
6221  const char *zParentSeq;
6222  const char *zChild;
6223  const char *zChildCol;
6224  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
6225  int rc;
6226
6227  assert( nVal==4 );
6228  zParent = (const char*)sqlite3_value_text(apVal[0]);
6229  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6230  zChild = (const char*)sqlite3_value_text(apVal[2]);
6231  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6232
6233  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6234  rc = sqlite3_table_column_metadata(
6235      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6236  );
6237  if( rc==SQLITE_OK ){
6238    rc = sqlite3_table_column_metadata(
6239        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6240    );
6241  }
6242
6243  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6244    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6245    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6246    sqlite3_free(z);
6247  }
6248}
6249
6250
6251/*
6252** The implementation of dot-command ".lint fkey-indexes".
6253*/
6254static int lintFkeyIndexes(
6255  ShellState *pState,             /* Current shell tool state */
6256  char **azArg,                   /* Array of arguments passed to dot command */
6257  int nArg                        /* Number of entries in azArg[] */
6258){
6259  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
6260  FILE *out = pState->out;        /* Stream to write non-error output to */
6261  int bVerbose = 0;               /* If -verbose is present */
6262  int bGroupByParent = 0;         /* If -groupbyparent is present */
6263  int i;                          /* To iterate through azArg[] */
6264  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
6265  int rc;                         /* Return code */
6266  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
6267
6268  /*
6269  ** This SELECT statement returns one row for each foreign key constraint
6270  ** in the schema of the main database. The column values are:
6271  **
6272  ** 0. The text of an SQL statement similar to:
6273  **
6274  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6275  **
6276  **    This SELECT is similar to the one that the foreign keys implementation
6277  **    needs to run internally on child tables. If there is an index that can
6278  **    be used to optimize this query, then it can also be used by the FK
6279  **    implementation to optimize DELETE or UPDATE statements on the parent
6280  **    table.
6281  **
6282  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6283  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6284  **    contains an index that can be used to optimize the query.
6285  **
6286  ** 2. Human readable text that describes the child table and columns. e.g.
6287  **
6288  **       "child_table(child_key1, child_key2)"
6289  **
6290  ** 3. Human readable text that describes the parent table and columns. e.g.
6291  **
6292  **       "parent_table(parent_key1, parent_key2)"
6293  **
6294  ** 4. A full CREATE INDEX statement for an index that could be used to
6295  **    optimize DELETE or UPDATE statements on the parent table. e.g.
6296  **
6297  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
6298  **
6299  ** 5. The name of the parent table.
6300  **
6301  ** These six values are used by the C logic below to generate the report.
6302  */
6303  const char *zSql =
6304  "SELECT "
6305    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6306    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6307    "  || fkey_collate_clause("
6308    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6309    ", "
6310    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6311    "  || group_concat('*=?', ' AND ') || ')'"
6312    ", "
6313    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
6314    ", "
6315    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6316    ", "
6317    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6318    "  || ' ON ' || quote(s.name) || '('"
6319    "  || group_concat(quote(f.[from]) ||"
6320    "        fkey_collate_clause("
6321    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6322    "  || ');'"
6323    ", "
6324    "     f.[table] "
6325    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6326    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6327    "GROUP BY s.name, f.id "
6328    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6329  ;
6330  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6331
6332  for(i=2; i<nArg; i++){
6333    int n = strlen30(azArg[i]);
6334    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6335      bVerbose = 1;
6336    }
6337    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6338      bGroupByParent = 1;
6339      zIndent = "    ";
6340    }
6341    else{
6342      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6343          azArg[0], azArg[1]
6344      );
6345      return SQLITE_ERROR;
6346    }
6347  }
6348
6349  /* Register the fkey_collate_clause() SQL function */
6350  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6351      0, shellFkeyCollateClause, 0, 0
6352  );
6353
6354
6355  if( rc==SQLITE_OK ){
6356    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6357  }
6358  if( rc==SQLITE_OK ){
6359    sqlite3_bind_int(pSql, 1, bGroupByParent);
6360  }
6361
6362  if( rc==SQLITE_OK ){
6363    int rc2;
6364    char *zPrev = 0;
6365    while( SQLITE_ROW==sqlite3_step(pSql) ){
6366      int res = -1;
6367      sqlite3_stmt *pExplain = 0;
6368      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6369      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6370      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6371      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6372      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6373      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6374
6375      if( zEQP==0 ) continue;
6376      if( zGlob==0 ) continue;
6377      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6378      if( rc!=SQLITE_OK ) break;
6379      if( SQLITE_ROW==sqlite3_step(pExplain) ){
6380        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6381        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)
6382                          || 0==sqlite3_strglob(zGlobIPK, zPlan));
6383      }
6384      rc = sqlite3_finalize(pExplain);
6385      if( rc!=SQLITE_OK ) break;
6386
6387      if( res<0 ){
6388        raw_printf(stderr, "Error: internal error");
6389        break;
6390      }else{
6391        if( bGroupByParent
6392        && (bVerbose || res==0)
6393        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6394        ){
6395          raw_printf(out, "-- Parent table %s\n", zParent);
6396          sqlite3_free(zPrev);
6397          zPrev = sqlite3_mprintf("%s", zParent);
6398        }
6399
6400        if( res==0 ){
6401          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6402        }else if( bVerbose ){
6403          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6404              zIndent, zFrom, zTarget
6405          );
6406        }
6407      }
6408    }
6409    sqlite3_free(zPrev);
6410
6411    if( rc!=SQLITE_OK ){
6412      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6413    }
6414
6415    rc2 = sqlite3_finalize(pSql);
6416    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6417      rc = rc2;
6418      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6419    }
6420  }else{
6421    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6422  }
6423
6424  return rc;
6425}
6426
6427/*
6428** Implementation of ".lint" dot command.
6429*/
6430static int lintDotCommand(
6431  ShellState *pState,             /* Current shell tool state */
6432  char **azArg,                   /* Array of arguments passed to dot command */
6433  int nArg                        /* Number of entries in azArg[] */
6434){
6435  int n;
6436  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6437  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6438  return lintFkeyIndexes(pState, azArg, nArg);
6439
6440 usage:
6441  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6442  raw_printf(stderr, "Where sub-commands are:\n");
6443  raw_printf(stderr, "    fkey-indexes\n");
6444  return SQLITE_ERROR;
6445}
6446
6447#if !defined SQLITE_OMIT_VIRTUALTABLE
6448static void shellPrepare(
6449  sqlite3 *db,
6450  int *pRc,
6451  const char *zSql,
6452  sqlite3_stmt **ppStmt
6453){
6454  *ppStmt = 0;
6455  if( *pRc==SQLITE_OK ){
6456    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6457    if( rc!=SQLITE_OK ){
6458      raw_printf(stderr, "sql error: %s (%d)\n",
6459          sqlite3_errmsg(db), sqlite3_errcode(db)
6460      );
6461      *pRc = rc;
6462    }
6463  }
6464}
6465
6466/*
6467** Create a prepared statement using printf-style arguments for the SQL.
6468**
6469** This routine is could be marked "static".  But it is not always used,
6470** depending on compile-time options.  By omitting the "static", we avoid
6471** nuisance compiler warnings about "defined but not used".
6472*/
6473void shellPreparePrintf(
6474  sqlite3 *db,
6475  int *pRc,
6476  sqlite3_stmt **ppStmt,
6477  const char *zFmt,
6478  ...
6479){
6480  *ppStmt = 0;
6481  if( *pRc==SQLITE_OK ){
6482    va_list ap;
6483    char *z;
6484    va_start(ap, zFmt);
6485    z = sqlite3_vmprintf(zFmt, ap);
6486    va_end(ap);
6487    if( z==0 ){
6488      *pRc = SQLITE_NOMEM;
6489    }else{
6490      shellPrepare(db, pRc, z, ppStmt);
6491      sqlite3_free(z);
6492    }
6493  }
6494}
6495
6496/* Finalize the prepared statement created using shellPreparePrintf().
6497**
6498** This routine is could be marked "static".  But it is not always used,
6499** depending on compile-time options.  By omitting the "static", we avoid
6500** nuisance compiler warnings about "defined but not used".
6501*/
6502void shellFinalize(
6503  int *pRc,
6504  sqlite3_stmt *pStmt
6505){
6506  if( pStmt ){
6507    sqlite3 *db = sqlite3_db_handle(pStmt);
6508    int rc = sqlite3_finalize(pStmt);
6509    if( *pRc==SQLITE_OK ){
6510      if( rc!=SQLITE_OK ){
6511        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6512      }
6513      *pRc = rc;
6514    }
6515  }
6516}
6517
6518/* Reset the prepared statement created using shellPreparePrintf().
6519**
6520** This routine is could be marked "static".  But it is not always used,
6521** depending on compile-time options.  By omitting the "static", we avoid
6522** nuisance compiler warnings about "defined but not used".
6523*/
6524void shellReset(
6525  int *pRc,
6526  sqlite3_stmt *pStmt
6527){
6528  int rc = sqlite3_reset(pStmt);
6529  if( *pRc==SQLITE_OK ){
6530    if( rc!=SQLITE_OK ){
6531      sqlite3 *db = sqlite3_db_handle(pStmt);
6532      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6533    }
6534    *pRc = rc;
6535  }
6536}
6537#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6538
6539#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6540/******************************************************************************
6541** The ".archive" or ".ar" command.
6542*/
6543/*
6544** Structure representing a single ".ar" command.
6545*/
6546typedef struct ArCommand ArCommand;
6547struct ArCommand {
6548  u8 eCmd;                        /* An AR_CMD_* value */
6549  u8 bVerbose;                    /* True if --verbose */
6550  u8 bZip;                        /* True if the archive is a ZIP */
6551  u8 bDryRun;                     /* True if --dry-run */
6552  u8 bAppend;                     /* True if --append */
6553  u8 bGlob;                       /* True if --glob */
6554  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6555  int nArg;                       /* Number of command arguments */
6556  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6557  const char *zFile;              /* --file argument, or NULL */
6558  const char *zDir;               /* --directory argument, or NULL */
6559  char **azArg;                   /* Array of command arguments */
6560  ShellState *p;                  /* Shell state */
6561  sqlite3 *db;                    /* Database containing the archive */
6562};
6563
6564/*
6565** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6566*/
6567static int arUsage(FILE *f){
6568  showHelp(f,"archive");
6569  return SQLITE_ERROR;
6570}
6571
6572/*
6573** Print an error message for the .ar command to stderr and return
6574** SQLITE_ERROR.
6575*/
6576static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6577  va_list ap;
6578  char *z;
6579  va_start(ap, zFmt);
6580  z = sqlite3_vmprintf(zFmt, ap);
6581  va_end(ap);
6582  utf8_printf(stderr, "Error: %s\n", z);
6583  if( pAr->fromCmdLine ){
6584    utf8_printf(stderr, "Use \"-A\" for more help\n");
6585  }else{
6586    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6587  }
6588  sqlite3_free(z);
6589  return SQLITE_ERROR;
6590}
6591
6592/*
6593** Values for ArCommand.eCmd.
6594*/
6595#define AR_CMD_CREATE       1
6596#define AR_CMD_UPDATE       2
6597#define AR_CMD_INSERT       3
6598#define AR_CMD_EXTRACT      4
6599#define AR_CMD_LIST         5
6600#define AR_CMD_HELP         6
6601#define AR_CMD_REMOVE       7
6602
6603/*
6604** Other (non-command) switches.
6605*/
6606#define AR_SWITCH_VERBOSE     8
6607#define AR_SWITCH_FILE        9
6608#define AR_SWITCH_DIRECTORY  10
6609#define AR_SWITCH_APPEND     11
6610#define AR_SWITCH_DRYRUN     12
6611#define AR_SWITCH_GLOB       13
6612
6613static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6614  switch( eSwitch ){
6615    case AR_CMD_CREATE:
6616    case AR_CMD_EXTRACT:
6617    case AR_CMD_LIST:
6618    case AR_CMD_REMOVE:
6619    case AR_CMD_UPDATE:
6620    case AR_CMD_INSERT:
6621    case AR_CMD_HELP:
6622      if( pAr->eCmd ){
6623        return arErrorMsg(pAr, "multiple command options");
6624      }
6625      pAr->eCmd = eSwitch;
6626      break;
6627
6628    case AR_SWITCH_DRYRUN:
6629      pAr->bDryRun = 1;
6630      break;
6631    case AR_SWITCH_GLOB:
6632      pAr->bGlob = 1;
6633      break;
6634    case AR_SWITCH_VERBOSE:
6635      pAr->bVerbose = 1;
6636      break;
6637    case AR_SWITCH_APPEND:
6638      pAr->bAppend = 1;
6639      /* Fall thru into --file */
6640    case AR_SWITCH_FILE:
6641      pAr->zFile = zArg;
6642      break;
6643    case AR_SWITCH_DIRECTORY:
6644      pAr->zDir = zArg;
6645      break;
6646  }
6647
6648  return SQLITE_OK;
6649}
6650
6651/*
6652** Parse the command line for an ".ar" command. The results are written into
6653** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6654** successfully, otherwise an error message is written to stderr and
6655** SQLITE_ERROR returned.
6656*/
6657static int arParseCommand(
6658  char **azArg,                   /* Array of arguments passed to dot command */
6659  int nArg,                       /* Number of entries in azArg[] */
6660  ArCommand *pAr                  /* Populate this object */
6661){
6662  struct ArSwitch {
6663    const char *zLong;
6664    char cShort;
6665    u8 eSwitch;
6666    u8 bArg;
6667  } aSwitch[] = {
6668    { "create",    'c', AR_CMD_CREATE,       0 },
6669    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6670    { "insert",    'i', AR_CMD_INSERT,       0 },
6671    { "list",      't', AR_CMD_LIST,         0 },
6672    { "remove",    'r', AR_CMD_REMOVE,       0 },
6673    { "update",    'u', AR_CMD_UPDATE,       0 },
6674    { "help",      'h', AR_CMD_HELP,         0 },
6675    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6676    { "file",      'f', AR_SWITCH_FILE,      1 },
6677    { "append",    'a', AR_SWITCH_APPEND,    1 },
6678    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6679    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6680    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6681  };
6682  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6683  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6684
6685  if( nArg<=1 ){
6686    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6687    return arUsage(stderr);
6688  }else{
6689    char *z = azArg[1];
6690    if( z[0]!='-' ){
6691      /* Traditional style [tar] invocation */
6692      int i;
6693      int iArg = 2;
6694      for(i=0; z[i]; i++){
6695        const char *zArg = 0;
6696        struct ArSwitch *pOpt;
6697        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6698          if( z[i]==pOpt->cShort ) break;
6699        }
6700        if( pOpt==pEnd ){
6701          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6702        }
6703        if( pOpt->bArg ){
6704          if( iArg>=nArg ){
6705            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6706          }
6707          zArg = azArg[iArg++];
6708        }
6709        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6710      }
6711      pAr->nArg = nArg-iArg;
6712      if( pAr->nArg>0 ){
6713        pAr->azArg = &azArg[iArg];
6714      }
6715    }else{
6716      /* Non-traditional invocation */
6717      int iArg;
6718      for(iArg=1; iArg<nArg; iArg++){
6719        int n;
6720        z = azArg[iArg];
6721        if( z[0]!='-' ){
6722          /* All remaining command line words are command arguments. */
6723          pAr->azArg = &azArg[iArg];
6724          pAr->nArg = nArg-iArg;
6725          break;
6726        }
6727        n = strlen30(z);
6728
6729        if( z[1]!='-' ){
6730          int i;
6731          /* One or more short options */
6732          for(i=1; i<n; i++){
6733            const char *zArg = 0;
6734            struct ArSwitch *pOpt;
6735            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6736              if( z[i]==pOpt->cShort ) break;
6737            }
6738            if( pOpt==pEnd ){
6739              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6740            }
6741            if( pOpt->bArg ){
6742              if( i<(n-1) ){
6743                zArg = &z[i+1];
6744                i = n;
6745              }else{
6746                if( iArg>=(nArg-1) ){
6747                  return arErrorMsg(pAr, "option requires an argument: %c",
6748                                    z[i]);
6749                }
6750                zArg = azArg[++iArg];
6751              }
6752            }
6753            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6754          }
6755        }else if( z[2]=='\0' ){
6756          /* A -- option, indicating that all remaining command line words
6757          ** are command arguments.  */
6758          pAr->azArg = &azArg[iArg+1];
6759          pAr->nArg = nArg-iArg-1;
6760          break;
6761        }else{
6762          /* A long option */
6763          const char *zArg = 0;             /* Argument for option, if any */
6764          struct ArSwitch *pMatch = 0;      /* Matching option */
6765          struct ArSwitch *pOpt;            /* Iterator */
6766          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6767            const char *zLong = pOpt->zLong;
6768            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6769              if( pMatch ){
6770                return arErrorMsg(pAr, "ambiguous option: %s",z);
6771              }else{
6772                pMatch = pOpt;
6773              }
6774            }
6775          }
6776
6777          if( pMatch==0 ){
6778            return arErrorMsg(pAr, "unrecognized option: %s", z);
6779          }
6780          if( pMatch->bArg ){
6781            if( iArg>=(nArg-1) ){
6782              return arErrorMsg(pAr, "option requires an argument: %s", z);
6783            }
6784            zArg = azArg[++iArg];
6785          }
6786          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6787        }
6788      }
6789    }
6790  }
6791
6792  return SQLITE_OK;
6793}
6794
6795/*
6796** This function assumes that all arguments within the ArCommand.azArg[]
6797** array refer to archive members, as for the --extract, --list or --remove
6798** commands. It checks that each of them are "present". If any specified
6799** file is not present in the archive, an error is printed to stderr and an
6800** error code returned. Otherwise, if all specified arguments are present
6801** in the archive, SQLITE_OK is returned. Here, "present" means either an
6802** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6803** when pAr->bGlob is true.
6804**
6805** This function strips any trailing '/' characters from each argument.
6806** This is consistent with the way the [tar] command seems to work on
6807** Linux.
6808*/
6809static int arCheckEntries(ArCommand *pAr){
6810  int rc = SQLITE_OK;
6811  if( pAr->nArg ){
6812    int i, j;
6813    sqlite3_stmt *pTest = 0;
6814    const char *zSel = (pAr->bGlob)
6815      ? "SELECT name FROM %s WHERE glob($name,name)"
6816      : "SELECT name FROM %s WHERE name=$name";
6817
6818    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6819    j = sqlite3_bind_parameter_index(pTest, "$name");
6820    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6821      char *z = pAr->azArg[i];
6822      int n = strlen30(z);
6823      int bOk = 0;
6824      while( n>0 && z[n-1]=='/' ) n--;
6825      z[n] = '\0';
6826      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6827      if( SQLITE_ROW==sqlite3_step(pTest) ){
6828        bOk = 1;
6829      }
6830      shellReset(&rc, pTest);
6831      if( rc==SQLITE_OK && bOk==0 ){
6832        utf8_printf(stderr, "not found in archive: %s\n", z);
6833        rc = SQLITE_ERROR;
6834      }
6835    }
6836    shellFinalize(&rc, pTest);
6837  }
6838  return rc;
6839}
6840
6841/*
6842** Format a WHERE clause that can be used against the "sqlar" table to
6843** identify all archive members that match the command arguments held
6844** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6845** The caller is responsible for eventually calling sqlite3_free() on
6846** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6847** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6848*/
6849static void arWhereClause(
6850  int *pRc,
6851  ArCommand *pAr,
6852  char **pzWhere                  /* OUT: New WHERE clause */
6853){
6854  char *zWhere = 0;
6855  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6856  if( *pRc==SQLITE_OK ){
6857    if( pAr->nArg==0 ){
6858      zWhere = sqlite3_mprintf("1");
6859    }else{
6860      int i;
6861      const char *zSep = "";
6862      for(i=0; i<pAr->nArg; i++){
6863        const char *z = pAr->azArg[i];
6864        zWhere = sqlite3_mprintf(
6865          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6866          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6867        );
6868        if( zWhere==0 ){
6869          *pRc = SQLITE_NOMEM;
6870          break;
6871        }
6872        zSep = " OR ";
6873      }
6874    }
6875  }
6876  *pzWhere = zWhere;
6877}
6878
6879/*
6880** Implementation of .ar "lisT" command.
6881*/
6882static int arListCommand(ArCommand *pAr){
6883  const char *zSql = "SELECT %s FROM %s WHERE %s";
6884  const char *azCols[] = {
6885    "name",
6886    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6887  };
6888
6889  char *zWhere = 0;
6890  sqlite3_stmt *pSql = 0;
6891  int rc;
6892
6893  rc = arCheckEntries(pAr);
6894  arWhereClause(&rc, pAr, &zWhere);
6895
6896  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6897                     pAr->zSrcTable, zWhere);
6898  if( pAr->bDryRun ){
6899    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6900  }else{
6901    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6902      if( pAr->bVerbose ){
6903        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6904            sqlite3_column_text(pSql, 0),
6905            sqlite3_column_int(pSql, 1),
6906            sqlite3_column_text(pSql, 2),
6907            sqlite3_column_text(pSql, 3)
6908        );
6909      }else{
6910        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6911      }
6912    }
6913  }
6914  shellFinalize(&rc, pSql);
6915  sqlite3_free(zWhere);
6916  return rc;
6917}
6918
6919
6920/*
6921** Implementation of .ar "Remove" command.
6922*/
6923static int arRemoveCommand(ArCommand *pAr){
6924  int rc = 0;
6925  char *zSql = 0;
6926  char *zWhere = 0;
6927
6928  if( pAr->nArg ){
6929    /* Verify that args actually exist within the archive before proceeding.
6930    ** And formulate a WHERE clause to match them.  */
6931    rc = arCheckEntries(pAr);
6932    arWhereClause(&rc, pAr, &zWhere);
6933  }
6934  if( rc==SQLITE_OK ){
6935    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6936                           pAr->zSrcTable, zWhere);
6937    if( pAr->bDryRun ){
6938      utf8_printf(pAr->p->out, "%s\n", zSql);
6939    }else{
6940      char *zErr = 0;
6941      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6942      if( rc==SQLITE_OK ){
6943        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6944        if( rc!=SQLITE_OK ){
6945          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6946        }else{
6947          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6948        }
6949      }
6950      if( zErr ){
6951        utf8_printf(stdout, "ERROR: %s\n", zErr);
6952        sqlite3_free(zErr);
6953      }
6954    }
6955  }
6956  sqlite3_free(zWhere);
6957  sqlite3_free(zSql);
6958  return rc;
6959}
6960
6961/*
6962** Implementation of .ar "eXtract" command.
6963*/
6964static int arExtractCommand(ArCommand *pAr){
6965  const char *zSql1 =
6966    "SELECT "
6967    " ($dir || name),"
6968    " writefile(($dir || name), %s, mode, mtime) "
6969    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6970    " AND name NOT GLOB '*..[/\\]*'";
6971
6972  const char *azExtraArg[] = {
6973    "sqlar_uncompress(data, sz)",
6974    "data"
6975  };
6976
6977  sqlite3_stmt *pSql = 0;
6978  int rc = SQLITE_OK;
6979  char *zDir = 0;
6980  char *zWhere = 0;
6981  int i, j;
6982
6983  /* If arguments are specified, check that they actually exist within
6984  ** the archive before proceeding. And formulate a WHERE clause to
6985  ** match them.  */
6986  rc = arCheckEntries(pAr);
6987  arWhereClause(&rc, pAr, &zWhere);
6988
6989  if( rc==SQLITE_OK ){
6990    if( pAr->zDir ){
6991      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6992    }else{
6993      zDir = sqlite3_mprintf("");
6994    }
6995    if( zDir==0 ) rc = SQLITE_NOMEM;
6996  }
6997
6998  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6999      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
7000  );
7001
7002  if( rc==SQLITE_OK ){
7003    j = sqlite3_bind_parameter_index(pSql, "$dir");
7004    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
7005
7006    /* Run the SELECT statement twice. The first time, writefile() is called
7007    ** for all archive members that should be extracted. The second time,
7008    ** only for the directories. This is because the timestamps for
7009    ** extracted directories must be reset after they are populated (as
7010    ** populating them changes the timestamp).  */
7011    for(i=0; i<2; i++){
7012      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
7013      sqlite3_bind_int(pSql, j, i);
7014      if( pAr->bDryRun ){
7015        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
7016      }else{
7017        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7018          if( i==0 && pAr->bVerbose ){
7019            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
7020          }
7021        }
7022      }
7023      shellReset(&rc, pSql);
7024    }
7025    shellFinalize(&rc, pSql);
7026  }
7027
7028  sqlite3_free(zDir);
7029  sqlite3_free(zWhere);
7030  return rc;
7031}
7032
7033/*
7034** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
7035*/
7036static int arExecSql(ArCommand *pAr, const char *zSql){
7037  int rc;
7038  if( pAr->bDryRun ){
7039    utf8_printf(pAr->p->out, "%s\n", zSql);
7040    rc = SQLITE_OK;
7041  }else{
7042    char *zErr = 0;
7043    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7044    if( zErr ){
7045      utf8_printf(stdout, "ERROR: %s\n", zErr);
7046      sqlite3_free(zErr);
7047    }
7048  }
7049  return rc;
7050}
7051
7052
7053/*
7054** Implementation of .ar "create", "insert", and "update" commands.
7055**
7056**     create    ->     Create a new SQL archive
7057**     insert    ->     Insert or reinsert all files listed
7058**     update    ->     Insert files that have changed or that were not
7059**                      previously in the archive
7060**
7061** Create the "sqlar" table in the database if it does not already exist.
7062** Then add each file in the azFile[] array to the archive. Directories
7063** are added recursively. If argument bVerbose is non-zero, a message is
7064** printed on stdout for each file archived.
7065**
7066** The create command is the same as update, except that it drops
7067** any existing "sqlar" table before beginning.  The "insert" command
7068** always overwrites every file named on the command-line, where as
7069** "update" only overwrites if the size or mtime or mode has changed.
7070*/
7071static int arCreateOrUpdateCommand(
7072  ArCommand *pAr,                 /* Command arguments and options */
7073  int bUpdate,                    /* true for a --create. */
7074  int bOnlyIfChanged              /* Only update if file has changed */
7075){
7076  const char *zCreate =
7077      "CREATE TABLE IF NOT EXISTS sqlar(\n"
7078      "  name TEXT PRIMARY KEY,  -- name of the file\n"
7079      "  mode INT,               -- access permissions\n"
7080      "  mtime INT,              -- last modification time\n"
7081      "  sz INT,                 -- original file size\n"
7082      "  data BLOB               -- compressed content\n"
7083      ")";
7084  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7085  const char *zInsertFmt[2] = {
7086     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7087     "  SELECT\n"
7088     "    %s,\n"
7089     "    mode,\n"
7090     "    mtime,\n"
7091     "    CASE substr(lsmode(mode),1,1)\n"
7092     "      WHEN '-' THEN length(data)\n"
7093     "      WHEN 'd' THEN 0\n"
7094     "      ELSE -1 END,\n"
7095     "    sqlar_compress(data)\n"
7096     "  FROM fsdir(%Q,%Q) AS disk\n"
7097     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7098     ,
7099     "REPLACE INTO %s(name,mode,mtime,data)\n"
7100     "  SELECT\n"
7101     "    %s,\n"
7102     "    mode,\n"
7103     "    mtime,\n"
7104     "    data\n"
7105     "  FROM fsdir(%Q,%Q) AS disk\n"
7106     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7107  };
7108  int i;                          /* For iterating through azFile[] */
7109  int rc;                         /* Return code */
7110  const char *zTab = 0;           /* SQL table into which to insert */
7111  char *zSql;
7112  char zTemp[50];
7113  char *zExists = 0;
7114
7115  arExecSql(pAr, "PRAGMA page_size=512");
7116  rc = arExecSql(pAr, "SAVEPOINT ar;");
7117  if( rc!=SQLITE_OK ) return rc;
7118  zTemp[0] = 0;
7119  if( pAr->bZip ){
7120    /* Initialize the zipfile virtual table, if necessary */
7121    if( pAr->zFile ){
7122      sqlite3_uint64 r;
7123      sqlite3_randomness(sizeof(r),&r);
7124      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7125      zTab = zTemp;
7126      zSql = sqlite3_mprintf(
7127         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7128         zTab, pAr->zFile
7129      );
7130      rc = arExecSql(pAr, zSql);
7131      sqlite3_free(zSql);
7132    }else{
7133      zTab = "zip";
7134    }
7135  }else{
7136    /* Initialize the table for an SQLAR */
7137    zTab = "sqlar";
7138    if( bUpdate==0 ){
7139      rc = arExecSql(pAr, zDrop);
7140      if( rc!=SQLITE_OK ) goto end_ar_transaction;
7141    }
7142    rc = arExecSql(pAr, zCreate);
7143  }
7144  if( bOnlyIfChanged ){
7145    zExists = sqlite3_mprintf(
7146      " AND NOT EXISTS("
7147          "SELECT 1 FROM %s AS mem"
7148          " WHERE mem.name=disk.name"
7149          " AND mem.mtime=disk.mtime"
7150          " AND mem.mode=disk.mode)", zTab);
7151  }else{
7152    zExists = sqlite3_mprintf("");
7153  }
7154  if( zExists==0 ) rc = SQLITE_NOMEM;
7155  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7156    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7157        pAr->bVerbose ? "shell_putsnl(name)" : "name",
7158        pAr->azArg[i], pAr->zDir, zExists);
7159    rc = arExecSql(pAr, zSql2);
7160    sqlite3_free(zSql2);
7161  }
7162end_ar_transaction:
7163  if( rc!=SQLITE_OK ){
7164    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7165  }else{
7166    rc = arExecSql(pAr, "RELEASE ar;");
7167    if( pAr->bZip && pAr->zFile ){
7168      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7169      arExecSql(pAr, zSql);
7170      sqlite3_free(zSql);
7171    }
7172  }
7173  sqlite3_free(zExists);
7174  return rc;
7175}
7176
7177/*
7178** Implementation of ".ar" dot command.
7179*/
7180static int arDotCommand(
7181  ShellState *pState,          /* Current shell tool state */
7182  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
7183  char **azArg,                /* Array of arguments passed to dot command */
7184  int nArg                     /* Number of entries in azArg[] */
7185){
7186  ArCommand cmd;
7187  int rc;
7188  memset(&cmd, 0, sizeof(cmd));
7189  cmd.fromCmdLine = fromCmdLine;
7190  rc = arParseCommand(azArg, nArg, &cmd);
7191  if( rc==SQLITE_OK ){
7192    int eDbType = SHELL_OPEN_UNSPEC;
7193    cmd.p = pState;
7194    cmd.db = pState->db;
7195    if( cmd.zFile ){
7196      eDbType = deduceDatabaseType(cmd.zFile, 1);
7197    }else{
7198      eDbType = pState->openMode;
7199    }
7200    if( eDbType==SHELL_OPEN_ZIPFILE ){
7201      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7202        if( cmd.zFile==0 ){
7203          cmd.zSrcTable = sqlite3_mprintf("zip");
7204        }else{
7205          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7206        }
7207      }
7208      cmd.bZip = 1;
7209    }else if( cmd.zFile ){
7210      int flags;
7211      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7212      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7213           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7214        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7215      }else{
7216        flags = SQLITE_OPEN_READONLY;
7217      }
7218      cmd.db = 0;
7219      if( cmd.bDryRun ){
7220        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7221             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7222      }
7223      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7224             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7225      if( rc!=SQLITE_OK ){
7226        utf8_printf(stderr, "cannot open file: %s (%s)\n",
7227            cmd.zFile, sqlite3_errmsg(cmd.db)
7228        );
7229        goto end_ar_command;
7230      }
7231      sqlite3_fileio_init(cmd.db, 0, 0);
7232      sqlite3_sqlar_init(cmd.db, 0, 0);
7233      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7234                              shellPutsFunc, 0, 0);
7235
7236    }
7237    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7238      if( cmd.eCmd!=AR_CMD_CREATE
7239       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7240      ){
7241        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7242        rc = SQLITE_ERROR;
7243        goto end_ar_command;
7244      }
7245      cmd.zSrcTable = sqlite3_mprintf("sqlar");
7246    }
7247
7248    switch( cmd.eCmd ){
7249      case AR_CMD_CREATE:
7250        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7251        break;
7252
7253      case AR_CMD_EXTRACT:
7254        rc = arExtractCommand(&cmd);
7255        break;
7256
7257      case AR_CMD_LIST:
7258        rc = arListCommand(&cmd);
7259        break;
7260
7261      case AR_CMD_HELP:
7262        arUsage(pState->out);
7263        break;
7264
7265      case AR_CMD_INSERT:
7266        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7267        break;
7268
7269      case AR_CMD_REMOVE:
7270        rc = arRemoveCommand(&cmd);
7271        break;
7272
7273      default:
7274        assert( cmd.eCmd==AR_CMD_UPDATE );
7275        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7276        break;
7277    }
7278  }
7279end_ar_command:
7280  if( cmd.db!=pState->db ){
7281    close_db(cmd.db);
7282  }
7283  sqlite3_free(cmd.zSrcTable);
7284
7285  return rc;
7286}
7287/* End of the ".archive" or ".ar" command logic
7288*******************************************************************************/
7289#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7290
7291#if SQLITE_SHELL_HAVE_RECOVER
7292
7293/*
7294** This function is used as a callback by the recover extension. Simply
7295** print the supplied SQL statement to stdout.
7296*/
7297static int recoverSqlCb(void *pCtx, const char *zSql){
7298  ShellState *pState = (ShellState*)pCtx;
7299  utf8_printf(pState->out, "%s;\n", zSql);
7300  return SQLITE_OK;
7301}
7302
7303/*
7304** This function is called to recover data from the database. A script
7305** to construct a new database containing all recovered data is output
7306** on stream pState->out.
7307*/
7308static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7309  int rc = SQLITE_OK;
7310  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7311  const char *zLAF = "lost_and_found";
7312  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7313  int bRowids = 1;                /* 0 if --no-rowids */
7314  sqlite3_recover *p = 0;
7315  int i = 0;
7316
7317  for(i=1; i<nArg; i++){
7318    char *z = azArg[i];
7319    int n;
7320    if( z[0]=='-' && z[1]=='-' ) z++;
7321    n = strlen30(z);
7322    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7323      bFreelist = 0;
7324    }else
7325    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7326      i++;
7327      zRecoveryDb = azArg[i];
7328    }else
7329    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7330      i++;
7331      zLAF = azArg[i];
7332    }else
7333    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7334      bRowids = 0;
7335    }
7336    else{
7337      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7338      showHelp(pState->out, azArg[0]);
7339      return 1;
7340    }
7341  }
7342
7343  p = sqlite3_recover_init_sql(
7344      pState->db, "main", recoverSqlCb, (void*)pState
7345  );
7346
7347  sqlite3_recover_config(p, 789, (void*)zRecoveryDb);
7348  sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
7349  sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
7350  sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
7351
7352  sqlite3_recover_run(p);
7353  if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
7354    const char *zErr = sqlite3_recover_errmsg(p);
7355    int errCode = sqlite3_recover_errcode(p);
7356    raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
7357  }
7358  rc = sqlite3_recover_finish(p);
7359  return rc;
7360}
7361#endif /* SQLITE_SHELL_HAVE_RECOVER */
7362
7363
7364/*
7365 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7366 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7367 *   close db and set it to 0, and return the columns spec, to later
7368 *   be sqlite3_free()'ed by the caller.
7369 * The return is 0 when either:
7370 *   (a) The db was not initialized and zCol==0 (There are no columns.)
7371 *   (b) zCol!=0  (Column was added, db initialized as needed.)
7372 * The 3rd argument, pRenamed, references an out parameter. If the
7373 * pointer is non-zero, its referent will be set to a summary of renames
7374 * done if renaming was necessary, or set to 0 if none was done. The out
7375 * string (if any) must be sqlite3_free()'ed by the caller.
7376 */
7377#ifdef SHELL_DEBUG
7378#define rc_err_oom_die(rc) \
7379  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7380  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7381    fprintf(stderr,"E:%d\n",rc), assert(0)
7382#else
7383static void rc_err_oom_die(int rc){
7384  if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7385  assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7386}
7387#endif
7388
7389#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7390static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7391#else  /* Otherwise, memory is faster/better for the transient DB. */
7392static const char *zCOL_DB = ":memory:";
7393#endif
7394
7395/* Define character (as C string) to separate generated column ordinal
7396 * from protected part of incoming column names. This defaults to "_"
7397 * so that incoming column identifiers that did not need not be quoted
7398 * remain usable without being quoted. It must be one character.
7399 */
7400#ifndef SHELL_AUTOCOLUMN_SEP
7401# define AUTOCOLUMN_SEP "_"
7402#else
7403# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7404#endif
7405
7406static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
7407  /* Queries and D{D,M}L used here */
7408  static const char * const zTabMake = "\
7409CREATE TABLE ColNames(\
7410 cpos INTEGER PRIMARY KEY,\
7411 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7412CREATE VIEW RepeatedNames AS \
7413SELECT DISTINCT t.name FROM ColNames t \
7414WHERE t.name COLLATE NOCASE IN (\
7415 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7416);\
7417";
7418  static const char * const zTabFill = "\
7419INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7420 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
7421";
7422  static const char * const zHasDupes = "\
7423SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
7424 <count(name) FROM ColNames\
7425";
7426#ifdef SHELL_COLUMN_RENAME_CLEAN
7427  static const char * const zDedoctor = "\
7428UPDATE ColNames SET chop=iif(\
7429  (substring(name,nlen,1) BETWEEN '0' AND '9')\
7430  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
7431 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
7432 0\
7433)\
7434";
7435#endif
7436  static const char * const zSetReps = "\
7437UPDATE ColNames AS t SET reps=\
7438(SELECT count(*) FROM ColNames d \
7439 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
7440 COLLATE NOCASE\
7441)\
7442";
7443#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
7444  static const char * const zColDigits = "\
7445SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
7446";
7447#else
7448  /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
7449  static const char * const zColDigits = "\
7450SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
7451 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
7452 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
7453";
7454#endif
7455  static const char * const zRenameRank =
7456#ifdef SHELL_COLUMN_RENAME_CLEAN
7457    "UPDATE ColNames AS t SET suff="
7458    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
7459#else /* ...RENAME_MINIMAL_ONE_PASS */
7460"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
7461"  SELECT 0 AS nlz"
7462"  UNION"
7463"  SELECT nlz+1 AS nlz FROM Lzn"
7464"  WHERE EXISTS("
7465"   SELECT 1"
7466"   FROM ColNames t, ColNames o"
7467"   WHERE"
7468"    iif(t.name IN (SELECT * FROM RepeatedNames),"
7469"     printf('%s"AUTOCOLUMN_SEP"%s',"
7470"      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
7471"     t.name"
7472"    )"
7473"    ="
7474"    iif(o.name IN (SELECT * FROM RepeatedNames),"
7475"     printf('%s"AUTOCOLUMN_SEP"%s',"
7476"      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
7477"     o.name"
7478"    )"
7479"    COLLATE NOCASE"
7480"    AND o.cpos<>t.cpos"
7481"   GROUP BY t.cpos"
7482"  )"
7483") UPDATE Colnames AS t SET"
7484" chop = 0," /* No chopping, never touch incoming names. */
7485" suff = iif(name IN (SELECT * FROM RepeatedNames),"
7486"  printf('"AUTOCOLUMN_SEP"%s', substring("
7487"   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
7488"  ''"
7489" )"
7490#endif
7491    ;
7492  static const char * const zCollectVar = "\
7493SELECT\
7494 '('||x'0a'\
7495 || group_concat(\
7496  cname||' TEXT',\
7497  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
7498 ||')' AS ColsSpec \
7499FROM (\
7500 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
7501 FROM ColNames ORDER BY cpos\
7502)";
7503  static const char * const zRenamesDone =
7504    "SELECT group_concat("
7505    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
7506    " ','||x'0a')"
7507    "FROM ColNames WHERE suff<>'' OR chop!=0"
7508    ;
7509  int rc;
7510  sqlite3_stmt *pStmt = 0;
7511  assert(pDb!=0);
7512  if( zColNew ){
7513    /* Add initial or additional column. Init db if necessary. */
7514    if( *pDb==0 ){
7515      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
7516#ifdef SHELL_COLFIX_DB
7517      if(*zCOL_DB!=':')
7518        sqlite3_exec(*pDb,"drop table if exists ColNames;"
7519                     "drop view if exists RepeatedNames;",0,0,0);
7520#endif
7521      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
7522      rc_err_oom_die(rc);
7523    }
7524    assert(*pDb!=0);
7525    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
7526    rc_err_oom_die(rc);
7527    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
7528    rc_err_oom_die(rc);
7529    rc = sqlite3_step(pStmt);
7530    rc_err_oom_die(rc);
7531    sqlite3_finalize(pStmt);
7532    return 0;
7533  }else if( *pDb==0 ){
7534    return 0;
7535  }else{
7536    /* Formulate the columns spec, close the DB, zero *pDb. */
7537    char *zColsSpec = 0;
7538    int hasDupes = db_int(*pDb, zHasDupes);
7539    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
7540    if( hasDupes ){
7541#ifdef SHELL_COLUMN_RENAME_CLEAN
7542      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
7543      rc_err_oom_die(rc);
7544#endif
7545      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
7546      rc_err_oom_die(rc);
7547      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
7548      rc_err_oom_die(rc);
7549      sqlite3_bind_int(pStmt, 1, nDigits);
7550      rc = sqlite3_step(pStmt);
7551      sqlite3_finalize(pStmt);
7552      assert(rc==SQLITE_DONE);
7553    }
7554    assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
7555    rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
7556    rc_err_oom_die(rc);
7557    rc = sqlite3_step(pStmt);
7558    if( rc==SQLITE_ROW ){
7559      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7560    }else{
7561      zColsSpec = 0;
7562    }
7563    if( pzRenamed!=0 ){
7564      if( !hasDupes ) *pzRenamed = 0;
7565      else{
7566        sqlite3_finalize(pStmt);
7567        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
7568            && SQLITE_ROW==sqlite3_step(pStmt) ){
7569          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7570        }else
7571          *pzRenamed = 0;
7572      }
7573    }
7574    sqlite3_finalize(pStmt);
7575    sqlite3_close(*pDb);
7576    *pDb = 0;
7577    return zColsSpec;
7578  }
7579}
7580
7581/*
7582** If an input line begins with "." then invoke this routine to
7583** process that line.
7584**
7585** Return 1 on error, 2 to exit, and 0 otherwise.
7586*/
7587static int do_meta_command(char *zLine, ShellState *p){
7588  int h = 1;
7589  int nArg = 0;
7590  int n, c;
7591  int rc = 0;
7592  char *azArg[52];
7593
7594#ifndef SQLITE_OMIT_VIRTUALTABLE
7595  if( p->expert.pExpert ){
7596    expertFinish(p, 1, 0);
7597  }
7598#endif
7599
7600  /* Parse the input line into tokens.
7601  */
7602  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7603    while( IsSpace(zLine[h]) ){ h++; }
7604    if( zLine[h]==0 ) break;
7605    if( zLine[h]=='\'' || zLine[h]=='"' ){
7606      int delim = zLine[h++];
7607      azArg[nArg++] = &zLine[h];
7608      while( zLine[h] && zLine[h]!=delim ){
7609        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7610        h++;
7611      }
7612      if( zLine[h]==delim ){
7613        zLine[h++] = 0;
7614      }
7615      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7616    }else{
7617      azArg[nArg++] = &zLine[h];
7618      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7619      if( zLine[h] ) zLine[h++] = 0;
7620      resolve_backslashes(azArg[nArg-1]);
7621    }
7622  }
7623  azArg[nArg] = 0;
7624
7625  /* Process the input line.
7626  */
7627  if( nArg==0 ) return 0; /* no tokens, no error */
7628  n = strlen30(azArg[0]);
7629  c = azArg[0][0];
7630  clearTempFile(p);
7631
7632#ifndef SQLITE_OMIT_AUTHORIZATION
7633  if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
7634    if( nArg!=2 ){
7635      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7636      rc = 1;
7637      goto meta_command_exit;
7638    }
7639    open_db(p, 0);
7640    if( booleanValue(azArg[1]) ){
7641      sqlite3_set_authorizer(p->db, shellAuth, p);
7642    }else if( p->bSafeModePersist ){
7643      sqlite3_set_authorizer(p->db, safeModeAuth, p);
7644    }else{
7645      sqlite3_set_authorizer(p->db, 0, 0);
7646    }
7647  }else
7648#endif
7649
7650#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
7651  && !defined(SQLITE_SHELL_FIDDLE)
7652  if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
7653    open_db(p, 0);
7654    failIfSafeMode(p, "cannot run .archive in safe mode");
7655    rc = arDotCommand(p, 0, azArg, nArg);
7656  }else
7657#endif
7658
7659#ifndef SQLITE_SHELL_FIDDLE
7660  if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
7661   || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
7662  ){
7663    const char *zDestFile = 0;
7664    const char *zDb = 0;
7665    sqlite3 *pDest;
7666    sqlite3_backup *pBackup;
7667    int j;
7668    int bAsync = 0;
7669    const char *zVfs = 0;
7670    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
7671    for(j=1; j<nArg; j++){
7672      const char *z = azArg[j];
7673      if( z[0]=='-' ){
7674        if( z[1]=='-' ) z++;
7675        if( cli_strcmp(z, "-append")==0 ){
7676          zVfs = "apndvfs";
7677        }else
7678        if( cli_strcmp(z, "-async")==0 ){
7679          bAsync = 1;
7680        }else
7681        {
7682          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7683          return 1;
7684        }
7685      }else if( zDestFile==0 ){
7686        zDestFile = azArg[j];
7687      }else if( zDb==0 ){
7688        zDb = zDestFile;
7689        zDestFile = azArg[j];
7690      }else{
7691        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7692        return 1;
7693      }
7694    }
7695    if( zDestFile==0 ){
7696      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7697      return 1;
7698    }
7699    if( zDb==0 ) zDb = "main";
7700    rc = sqlite3_open_v2(zDestFile, &pDest,
7701                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7702    if( rc!=SQLITE_OK ){
7703      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7704      close_db(pDest);
7705      return 1;
7706    }
7707    if( bAsync ){
7708      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7709                   0, 0, 0);
7710    }
7711    open_db(p, 0);
7712    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7713    if( pBackup==0 ){
7714      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7715      close_db(pDest);
7716      return 1;
7717    }
7718    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7719    sqlite3_backup_finish(pBackup);
7720    if( rc==SQLITE_DONE ){
7721      rc = 0;
7722    }else{
7723      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7724      rc = 1;
7725    }
7726    close_db(pDest);
7727  }else
7728#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7729
7730  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
7731    if( nArg==2 ){
7732      bail_on_error = booleanValue(azArg[1]);
7733    }else{
7734      raw_printf(stderr, "Usage: .bail on|off\n");
7735      rc = 1;
7736    }
7737  }else
7738
7739  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
7740    if( nArg==2 ){
7741      if( booleanValue(azArg[1]) ){
7742        setBinaryMode(p->out, 1);
7743      }else{
7744        setTextMode(p->out, 1);
7745      }
7746    }else{
7747      raw_printf(stderr, "Usage: .binary on|off\n");
7748      rc = 1;
7749    }
7750  }else
7751
7752  /* The undocumented ".breakpoint" command causes a call to the no-op
7753  ** routine named test_breakpoint().
7754  */
7755  if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
7756    test_breakpoint();
7757  }else
7758
7759#ifndef SQLITE_SHELL_FIDDLE
7760  if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
7761    failIfSafeMode(p, "cannot run .cd in safe mode");
7762    if( nArg==2 ){
7763#if defined(_WIN32) || defined(WIN32)
7764      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7765      rc = !SetCurrentDirectoryW(z);
7766      sqlite3_free(z);
7767#else
7768      rc = chdir(azArg[1]);
7769#endif
7770      if( rc ){
7771        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7772        rc = 1;
7773      }
7774    }else{
7775      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7776      rc = 1;
7777    }
7778  }else
7779#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7780
7781  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
7782    if( nArg==2 ){
7783      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7784    }else{
7785      raw_printf(stderr, "Usage: .changes on|off\n");
7786      rc = 1;
7787    }
7788  }else
7789
7790#ifndef SQLITE_SHELL_FIDDLE
7791  /* Cancel output redirection, if it is currently set (by .testcase)
7792  ** Then read the content of the testcase-out.txt file and compare against
7793  ** azArg[1].  If there are differences, report an error and exit.
7794  */
7795  if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
7796    char *zRes = 0;
7797    output_reset(p);
7798    if( nArg!=2 ){
7799      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7800      rc = 2;
7801    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7802      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7803      rc = 2;
7804    }else if( testcase_glob(azArg[1],zRes)==0 ){
7805      utf8_printf(stderr,
7806                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7807                 p->zTestcase, azArg[1], zRes);
7808      rc = 1;
7809    }else{
7810      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7811      p->nCheck++;
7812    }
7813    sqlite3_free(zRes);
7814  }else
7815#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7816
7817#ifndef SQLITE_SHELL_FIDDLE
7818  if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
7819    failIfSafeMode(p, "cannot run .clone in safe mode");
7820    if( nArg==2 ){
7821      tryToClone(p, azArg[1]);
7822    }else{
7823      raw_printf(stderr, "Usage: .clone FILENAME\n");
7824      rc = 1;
7825    }
7826  }else
7827#endif /* !defined(SQLITE_SHELL_FIDDLE) */
7828
7829  if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
7830    if( nArg==1 ){
7831      /* List available connections */
7832      int i;
7833      for(i=0; i<ArraySize(p->aAuxDb); i++){
7834        const char *zFile = p->aAuxDb[i].zDbFilename;
7835        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
7836          zFile = "(not open)";
7837        }else if( zFile==0 ){
7838          zFile = "(memory)";
7839        }else if( zFile[0]==0 ){
7840          zFile = "(temporary-file)";
7841        }
7842        if( p->pAuxDb == &p->aAuxDb[i] ){
7843          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
7844        }else if( p->aAuxDb[i].db!=0 ){
7845          utf8_printf(stdout, "       %d: %s\n", i, zFile);
7846        }
7847      }
7848    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
7849      int i = azArg[1][0] - '0';
7850      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
7851        p->pAuxDb->db = p->db;
7852        p->pAuxDb = &p->aAuxDb[i];
7853        globalDb = p->db = p->pAuxDb->db;
7854        p->pAuxDb->db = 0;
7855      }
7856    }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
7857           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
7858      int i = azArg[2][0] - '0';
7859      if( i<0 || i>=ArraySize(p->aAuxDb) ){
7860        /* No-op */
7861      }else if( p->pAuxDb == &p->aAuxDb[i] ){
7862        raw_printf(stderr, "cannot close the active database connection\n");
7863        rc = 1;
7864      }else if( p->aAuxDb[i].db ){
7865        session_close_all(p, i);
7866        close_db(p->aAuxDb[i].db);
7867        p->aAuxDb[i].db = 0;
7868      }
7869    }else{
7870      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
7871      rc = 1;
7872    }
7873  }else
7874
7875  if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
7876    char **azName = 0;
7877    int nName = 0;
7878    sqlite3_stmt *pStmt;
7879    int i;
7880    open_db(p, 0);
7881    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7882    if( rc ){
7883      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7884      rc = 1;
7885    }else{
7886      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7887        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7888        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7889        if( zSchema==0 || zFile==0 ) continue;
7890        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7891        shell_check_oom(azName);
7892        azName[nName*2] = strdup(zSchema);
7893        azName[nName*2+1] = strdup(zFile);
7894        nName++;
7895      }
7896    }
7897    sqlite3_finalize(pStmt);
7898    for(i=0; i<nName; i++){
7899      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7900      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7901      const char *z = azName[i*2+1];
7902      utf8_printf(p->out, "%s: %s %s%s\n",
7903         azName[i*2],
7904         z && z[0] ? z : "\"\"",
7905         bRdonly ? "r/o" : "r/w",
7906         eTxn==SQLITE_TXN_NONE ? "" :
7907            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7908      free(azName[i*2]);
7909      free(azName[i*2+1]);
7910    }
7911    sqlite3_free(azName);
7912  }else
7913
7914  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
7915    static const struct DbConfigChoices {
7916      const char *zName;
7917      int op;
7918    } aDbConfig[] = {
7919        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7920        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7921        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7922        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7923        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7924        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7925        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7926        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7927        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7928        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7929        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7930        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7931        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7932        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7933        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7934        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7935    };
7936    int ii, v;
7937    open_db(p, 0);
7938    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7939      if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7940      if( nArg>=3 ){
7941        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7942      }
7943      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7944      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7945      if( nArg>1 ) break;
7946    }
7947    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7948      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7949      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7950    }
7951  }else
7952
7953#if SQLITE_SHELL_HAVE_RECOVER
7954  if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
7955    rc = shell_dbinfo_command(p, nArg, azArg);
7956  }else
7957
7958  if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
7959    open_db(p, 0);
7960    rc = recoverDatabaseCmd(p, nArg, azArg);
7961  }else
7962#endif /* SQLITE_SHELL_HAVE_RECOVER */
7963
7964  if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
7965    char *zLike = 0;
7966    char *zSql;
7967    int i;
7968    int savedShowHeader = p->showHeader;
7969    int savedShellFlags = p->shellFlgs;
7970    ShellClearFlag(p,
7971       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7972       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7973    for(i=1; i<nArg; i++){
7974      if( azArg[i][0]=='-' ){
7975        const char *z = azArg[i]+1;
7976        if( z[0]=='-' ) z++;
7977        if( cli_strcmp(z,"preserve-rowids")==0 ){
7978#ifdef SQLITE_OMIT_VIRTUALTABLE
7979          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7980                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7981          rc = 1;
7982          sqlite3_free(zLike);
7983          goto meta_command_exit;
7984#else
7985          ShellSetFlag(p, SHFLG_PreserveRowid);
7986#endif
7987        }else
7988        if( cli_strcmp(z,"newlines")==0 ){
7989          ShellSetFlag(p, SHFLG_Newlines);
7990        }else
7991        if( cli_strcmp(z,"data-only")==0 ){
7992          ShellSetFlag(p, SHFLG_DumpDataOnly);
7993        }else
7994        if( cli_strcmp(z,"nosys")==0 ){
7995          ShellSetFlag(p, SHFLG_DumpNoSys);
7996        }else
7997        {
7998          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7999          rc = 1;
8000          sqlite3_free(zLike);
8001          goto meta_command_exit;
8002        }
8003      }else{
8004        /* azArg[i] contains a LIKE pattern. This ".dump" request should
8005        ** only dump data for tables for which either the table name matches
8006        ** the LIKE pattern, or the table appears to be a shadow table of
8007        ** a virtual table for which the name matches the LIKE pattern.
8008        */
8009        char *zExpr = sqlite3_mprintf(
8010            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8011            "  SELECT 1 FROM sqlite_schema WHERE "
8012            "    name LIKE %Q ESCAPE '\\' AND"
8013            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8014            "    substr(o.name, 1, length(name)+1) == (name||'_')"
8015            ")", azArg[i], azArg[i]
8016        );
8017
8018        if( zLike ){
8019          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8020        }else{
8021          zLike = zExpr;
8022        }
8023      }
8024    }
8025
8026    open_db(p, 0);
8027
8028    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8029      /* When playing back a "dump", the content might appear in an order
8030      ** which causes immediate foreign key constraints to be violated.
8031      ** So disable foreign-key constraint enforcement to prevent problems. */
8032      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8033      raw_printf(p->out, "BEGIN TRANSACTION;\n");
8034    }
8035    p->writableSchema = 0;
8036    p->showHeader = 0;
8037    /* Set writable_schema=ON since doing so forces SQLite to initialize
8038    ** as much of the schema as it can even if the sqlite_schema table is
8039    ** corrupt. */
8040    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8041    p->nErr = 0;
8042    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8043    zSql = sqlite3_mprintf(
8044      "SELECT name, type, sql FROM sqlite_schema AS o "
8045      "WHERE (%s) AND type=='table'"
8046      "  AND sql NOT NULL"
8047      " ORDER BY tbl_name='sqlite_sequence', rowid",
8048      zLike
8049    );
8050    run_schema_dump_query(p,zSql);
8051    sqlite3_free(zSql);
8052    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8053      zSql = sqlite3_mprintf(
8054        "SELECT sql FROM sqlite_schema AS o "
8055        "WHERE (%s) AND sql NOT NULL"
8056        "  AND type IN ('index','trigger','view')",
8057        zLike
8058      );
8059      run_table_dump_query(p, zSql);
8060      sqlite3_free(zSql);
8061    }
8062    sqlite3_free(zLike);
8063    if( p->writableSchema ){
8064      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8065      p->writableSchema = 0;
8066    }
8067    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8068    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8069    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8070      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8071    }
8072    p->showHeader = savedShowHeader;
8073    p->shellFlgs = savedShellFlags;
8074  }else
8075
8076  if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
8077    if( nArg==2 ){
8078      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8079    }else{
8080      raw_printf(stderr, "Usage: .echo on|off\n");
8081      rc = 1;
8082    }
8083  }else
8084
8085  if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
8086    if( nArg==2 ){
8087      p->autoEQPtest = 0;
8088      if( p->autoEQPtrace ){
8089        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8090        p->autoEQPtrace = 0;
8091      }
8092      if( cli_strcmp(azArg[1],"full")==0 ){
8093        p->autoEQP = AUTOEQP_full;
8094      }else if( cli_strcmp(azArg[1],"trigger")==0 ){
8095        p->autoEQP = AUTOEQP_trigger;
8096#ifdef SQLITE_DEBUG
8097      }else if( cli_strcmp(azArg[1],"test")==0 ){
8098        p->autoEQP = AUTOEQP_on;
8099        p->autoEQPtest = 1;
8100      }else if( cli_strcmp(azArg[1],"trace")==0 ){
8101        p->autoEQP = AUTOEQP_full;
8102        p->autoEQPtrace = 1;
8103        open_db(p, 0);
8104        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8105        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8106#endif
8107      }else{
8108        p->autoEQP = (u8)booleanValue(azArg[1]);
8109      }
8110    }else{
8111      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8112      rc = 1;
8113    }
8114  }else
8115
8116#ifndef SQLITE_SHELL_FIDDLE
8117  if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
8118    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8119    rc = 2;
8120  }else
8121#endif
8122
8123  /* The ".explain" command is automatic now.  It is largely pointless.  It
8124  ** retained purely for backwards compatibility */
8125  if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
8126    int val = 1;
8127    if( nArg>=2 ){
8128      if( cli_strcmp(azArg[1],"auto")==0 ){
8129        val = 99;
8130      }else{
8131        val =  booleanValue(azArg[1]);
8132      }
8133    }
8134    if( val==1 && p->mode!=MODE_Explain ){
8135      p->normalMode = p->mode;
8136      p->mode = MODE_Explain;
8137      p->autoExplain = 0;
8138    }else if( val==0 ){
8139      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8140      p->autoExplain = 0;
8141    }else if( val==99 ){
8142      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8143      p->autoExplain = 1;
8144    }
8145  }else
8146
8147#ifndef SQLITE_OMIT_VIRTUALTABLE
8148  if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
8149    if( p->bSafeMode ){
8150      raw_printf(stderr,
8151        "Cannot run experimental commands such as \"%s\" in safe mode\n",
8152        azArg[0]);
8153      rc = 1;
8154    }else{
8155      open_db(p, 0);
8156      expertDotCommand(p, azArg, nArg);
8157    }
8158  }else
8159#endif
8160
8161  if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
8162    static const struct {
8163       const char *zCtrlName;   /* Name of a test-control option */
8164       int ctrlCode;            /* Integer code for that option */
8165       const char *zUsage;      /* Usage notes */
8166    } aCtrl[] = {
8167      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8168      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8169      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8170      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8171      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8172   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8173      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8174      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8175      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8176      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8177   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8178    };
8179    int filectrl = -1;
8180    int iCtrl = -1;
8181    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8182    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8183    int n2, i;
8184    const char *zCmd = 0;
8185    const char *zSchema = 0;
8186
8187    open_db(p, 0);
8188    zCmd = nArg>=2 ? azArg[1] : "help";
8189
8190    if( zCmd[0]=='-'
8191     && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
8192     && nArg>=4
8193    ){
8194      zSchema = azArg[2];
8195      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8196      nArg -= 2;
8197      zCmd = azArg[1];
8198    }
8199
8200    /* The argument can optionally begin with "-" or "--" */
8201    if( zCmd[0]=='-' && zCmd[1] ){
8202      zCmd++;
8203      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8204    }
8205
8206    /* --help lists all file-controls */
8207    if( cli_strcmp(zCmd,"help")==0 ){
8208      utf8_printf(p->out, "Available file-controls:\n");
8209      for(i=0; i<ArraySize(aCtrl); i++){
8210        utf8_printf(p->out, "  .filectrl %s %s\n",
8211                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8212      }
8213      rc = 1;
8214      goto meta_command_exit;
8215    }
8216
8217    /* convert filectrl text option to value. allow any unique prefix
8218    ** of the option name, or a numerical value. */
8219    n2 = strlen30(zCmd);
8220    for(i=0; i<ArraySize(aCtrl); i++){
8221      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8222        if( filectrl<0 ){
8223          filectrl = aCtrl[i].ctrlCode;
8224          iCtrl = i;
8225        }else{
8226          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8227                              "Use \".filectrl --help\" for help\n", zCmd);
8228          rc = 1;
8229          goto meta_command_exit;
8230        }
8231      }
8232    }
8233    if( filectrl<0 ){
8234      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8235                         "Use \".filectrl --help\" for help\n", zCmd);
8236    }else{
8237      switch(filectrl){
8238        case SQLITE_FCNTL_SIZE_LIMIT: {
8239          if( nArg!=2 && nArg!=3 ) break;
8240          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8241          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8242          isOk = 1;
8243          break;
8244        }
8245        case SQLITE_FCNTL_LOCK_TIMEOUT:
8246        case SQLITE_FCNTL_CHUNK_SIZE: {
8247          int x;
8248          if( nArg!=3 ) break;
8249          x = (int)integerValue(azArg[2]);
8250          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8251          isOk = 2;
8252          break;
8253        }
8254        case SQLITE_FCNTL_PERSIST_WAL:
8255        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8256          int x;
8257          if( nArg!=2 && nArg!=3 ) break;
8258          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8259          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8260          iRes = x;
8261          isOk = 1;
8262          break;
8263        }
8264        case SQLITE_FCNTL_DATA_VERSION:
8265        case SQLITE_FCNTL_HAS_MOVED: {
8266          int x;
8267          if( nArg!=2 ) break;
8268          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8269          iRes = x;
8270          isOk = 1;
8271          break;
8272        }
8273        case SQLITE_FCNTL_TEMPFILENAME: {
8274          char *z = 0;
8275          if( nArg!=2 ) break;
8276          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8277          if( z ){
8278            utf8_printf(p->out, "%s\n", z);
8279            sqlite3_free(z);
8280          }
8281          isOk = 2;
8282          break;
8283        }
8284        case SQLITE_FCNTL_RESERVE_BYTES: {
8285          int x;
8286          if( nArg>=3 ){
8287            x = atoi(azArg[2]);
8288            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8289          }
8290          x = -1;
8291          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8292          utf8_printf(p->out,"%d\n", x);
8293          isOk = 2;
8294          break;
8295        }
8296      }
8297    }
8298    if( isOk==0 && iCtrl>=0 ){
8299      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8300      rc = 1;
8301    }else if( isOk==1 ){
8302      char zBuf[100];
8303      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8304      raw_printf(p->out, "%s\n", zBuf);
8305    }
8306  }else
8307
8308  if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
8309    ShellState data;
8310    int doStats = 0;
8311    memcpy(&data, p, sizeof(data));
8312    data.showHeader = 0;
8313    data.cMode = data.mode = MODE_Semi;
8314    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8315      data.cMode = data.mode = MODE_Pretty;
8316      nArg = 1;
8317    }
8318    if( nArg!=1 ){
8319      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8320      rc = 1;
8321      goto meta_command_exit;
8322    }
8323    open_db(p, 0);
8324    rc = sqlite3_exec(p->db,
8325       "SELECT sql FROM"
8326       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8327       "     FROM sqlite_schema UNION ALL"
8328       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8329       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8330       "ORDER BY x",
8331       callback, &data, 0
8332    );
8333    if( rc==SQLITE_OK ){
8334      sqlite3_stmt *pStmt;
8335      rc = sqlite3_prepare_v2(p->db,
8336               "SELECT rowid FROM sqlite_schema"
8337               " WHERE name GLOB 'sqlite_stat[134]'",
8338               -1, &pStmt, 0);
8339      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8340      sqlite3_finalize(pStmt);
8341    }
8342    if( doStats==0 ){
8343      raw_printf(p->out, "/* No STAT tables available */\n");
8344    }else{
8345      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8346      data.cMode = data.mode = MODE_Insert;
8347      data.zDestTable = "sqlite_stat1";
8348      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8349      data.zDestTable = "sqlite_stat4";
8350      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8351      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8352    }
8353  }else
8354
8355  if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
8356    if( nArg==2 ){
8357      p->showHeader = booleanValue(azArg[1]);
8358      p->shellFlgs |= SHFLG_HeaderSet;
8359    }else{
8360      raw_printf(stderr, "Usage: .headers on|off\n");
8361      rc = 1;
8362    }
8363  }else
8364
8365  if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
8366    if( nArg>=2 ){
8367      n = showHelp(p->out, azArg[1]);
8368      if( n==0 ){
8369        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8370      }
8371    }else{
8372      showHelp(p->out, 0);
8373    }
8374  }else
8375
8376#ifndef SQLITE_SHELL_FIDDLE
8377  if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
8378    char *zTable = 0;           /* Insert data into this table */
8379    char *zSchema = 0;          /* within this schema (may default to "main") */
8380    char *zFile = 0;            /* Name of file to extra content from */
8381    sqlite3_stmt *pStmt = NULL; /* A statement */
8382    int nCol;                   /* Number of columns in the table */
8383    int nByte;                  /* Number of bytes in an SQL string */
8384    int i, j;                   /* Loop counters */
8385    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8386    int nSep;                   /* Number of bytes in p->colSeparator[] */
8387    char *zSql;                 /* An SQL statement */
8388    char *zFullTabName;         /* Table name with schema if applicable */
8389    ImportCtx sCtx;             /* Reader context */
8390    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8391    int eVerbose = 0;           /* Larger for more console output */
8392    int nSkip = 0;              /* Initial lines to skip */
8393    int useOutputMode = 1;      /* Use output mode to determine separators */
8394    char *zCreate = 0;          /* CREATE TABLE statement text */
8395
8396    failIfSafeMode(p, "cannot run .import in safe mode");
8397    memset(&sCtx, 0, sizeof(sCtx));
8398    if( p->mode==MODE_Ascii ){
8399      xRead = ascii_read_one_field;
8400    }else{
8401      xRead = csv_read_one_field;
8402    }
8403    rc = 1;
8404    for(i=1; i<nArg; i++){
8405      char *z = azArg[i];
8406      if( z[0]=='-' && z[1]=='-' ) z++;
8407      if( z[0]!='-' ){
8408        if( zFile==0 ){
8409          zFile = z;
8410        }else if( zTable==0 ){
8411          zTable = z;
8412        }else{
8413          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8414          showHelp(p->out, "import");
8415          goto meta_command_exit;
8416        }
8417      }else if( cli_strcmp(z,"-v")==0 ){
8418        eVerbose++;
8419      }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
8420        zSchema = azArg[++i];
8421      }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
8422        nSkip = integerValue(azArg[++i]);
8423      }else if( cli_strcmp(z,"-ascii")==0 ){
8424        sCtx.cColSep = SEP_Unit[0];
8425        sCtx.cRowSep = SEP_Record[0];
8426        xRead = ascii_read_one_field;
8427        useOutputMode = 0;
8428      }else if( cli_strcmp(z,"-csv")==0 ){
8429        sCtx.cColSep = ',';
8430        sCtx.cRowSep = '\n';
8431        xRead = csv_read_one_field;
8432        useOutputMode = 0;
8433      }else{
8434        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8435        showHelp(p->out, "import");
8436        goto meta_command_exit;
8437      }
8438    }
8439    if( zTable==0 ){
8440      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8441                  zFile==0 ? "FILE" : "TABLE");
8442      showHelp(p->out, "import");
8443      goto meta_command_exit;
8444    }
8445    seenInterrupt = 0;
8446    open_db(p, 0);
8447    if( useOutputMode ){
8448      /* If neither the --csv or --ascii options are specified, then set
8449      ** the column and row separator characters from the output mode. */
8450      nSep = strlen30(p->colSeparator);
8451      if( nSep==0 ){
8452        raw_printf(stderr,
8453                   "Error: non-null column separator required for import\n");
8454        goto meta_command_exit;
8455      }
8456      if( nSep>1 ){
8457        raw_printf(stderr,
8458              "Error: multi-character column separators not allowed"
8459              " for import\n");
8460        goto meta_command_exit;
8461      }
8462      nSep = strlen30(p->rowSeparator);
8463      if( nSep==0 ){
8464        raw_printf(stderr,
8465            "Error: non-null row separator required for import\n");
8466        goto meta_command_exit;
8467      }
8468      if( nSep==2 && p->mode==MODE_Csv
8469       && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
8470      ){
8471        /* When importing CSV (only), if the row separator is set to the
8472        ** default output row separator, change it to the default input
8473        ** row separator.  This avoids having to maintain different input
8474        ** and output row separators. */
8475        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8476        nSep = strlen30(p->rowSeparator);
8477      }
8478      if( nSep>1 ){
8479        raw_printf(stderr, "Error: multi-character row separators not allowed"
8480                           " for import\n");
8481        goto meta_command_exit;
8482      }
8483      sCtx.cColSep = p->colSeparator[0];
8484      sCtx.cRowSep = p->rowSeparator[0];
8485    }
8486    sCtx.zFile = zFile;
8487    sCtx.nLine = 1;
8488    if( sCtx.zFile[0]=='|' ){
8489#ifdef SQLITE_OMIT_POPEN
8490      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8491      goto meta_command_exit;
8492#else
8493      sCtx.in = popen(sCtx.zFile+1, "r");
8494      sCtx.zFile = "<pipe>";
8495      sCtx.xCloser = pclose;
8496#endif
8497    }else{
8498      sCtx.in = fopen(sCtx.zFile, "rb");
8499      sCtx.xCloser = fclose;
8500    }
8501    if( sCtx.in==0 ){
8502      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8503      goto meta_command_exit;
8504    }
8505    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8506      char zSep[2];
8507      zSep[1] = 0;
8508      zSep[0] = sCtx.cColSep;
8509      utf8_printf(p->out, "Column separator ");
8510      output_c_string(p->out, zSep);
8511      utf8_printf(p->out, ", row separator ");
8512      zSep[0] = sCtx.cRowSep;
8513      output_c_string(p->out, zSep);
8514      utf8_printf(p->out, "\n");
8515    }
8516    sCtx.z = sqlite3_malloc64(120);
8517    if( sCtx.z==0 ){
8518      import_cleanup(&sCtx);
8519      shell_out_of_memory();
8520    }
8521    /* Below, resources must be freed before exit. */
8522    while( (nSkip--)>0 ){
8523      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8524    }
8525    if( zSchema!=0 ){
8526      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
8527    }else{
8528      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
8529    }
8530    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
8531    if( zSql==0 || zFullTabName==0 ){
8532      import_cleanup(&sCtx);
8533      shell_out_of_memory();
8534    }
8535    nByte = strlen30(zSql);
8536    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8537    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8538    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8539      sqlite3 *dbCols = 0;
8540      char *zRenames = 0;
8541      char *zColDefs;
8542      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
8543      while( xRead(&sCtx) ){
8544        zAutoColumn(sCtx.z, &dbCols, 0);
8545        if( sCtx.cTerm!=sCtx.cColSep ) break;
8546      }
8547      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
8548      if( zRenames!=0 ){
8549        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
8550                    "Columns renamed during .import %s due to duplicates:\n"
8551                    "%s\n", sCtx.zFile, zRenames);
8552        sqlite3_free(zRenames);
8553      }
8554      assert(dbCols==0);
8555      if( zColDefs==0 ){
8556        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8557      import_fail:
8558        sqlite3_free(zCreate);
8559        sqlite3_free(zSql);
8560        sqlite3_free(zFullTabName);
8561        import_cleanup(&sCtx);
8562        rc = 1;
8563        goto meta_command_exit;
8564      }
8565      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
8566      if( eVerbose>=1 ){
8567        utf8_printf(p->out, "%s\n", zCreate);
8568      }
8569      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8570      if( rc ){
8571        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
8572        goto import_fail;
8573      }
8574      sqlite3_free(zCreate);
8575      zCreate = 0;
8576      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8577    }
8578    if( rc ){
8579      if (pStmt) sqlite3_finalize(pStmt);
8580      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8581      goto import_fail;
8582    }
8583    sqlite3_free(zSql);
8584    nCol = sqlite3_column_count(pStmt);
8585    sqlite3_finalize(pStmt);
8586    pStmt = 0;
8587    if( nCol==0 ) return 0; /* no columns, no error */
8588    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8589    if( zSql==0 ){
8590      import_cleanup(&sCtx);
8591      shell_out_of_memory();
8592    }
8593    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
8594    j = strlen30(zSql);
8595    for(i=1; i<nCol; i++){
8596      zSql[j++] = ',';
8597      zSql[j++] = '?';
8598    }
8599    zSql[j++] = ')';
8600    zSql[j] = 0;
8601    if( eVerbose>=2 ){
8602      utf8_printf(p->out, "Insert using: %s\n", zSql);
8603    }
8604    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8605    if( rc ){
8606      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8607      if (pStmt) sqlite3_finalize(pStmt);
8608      goto import_fail;
8609    }
8610    sqlite3_free(zSql);
8611    sqlite3_free(zFullTabName);
8612    needCommit = sqlite3_get_autocommit(p->db);
8613    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8614    do{
8615      int startLine = sCtx.nLine;
8616      for(i=0; i<nCol; i++){
8617        char *z = xRead(&sCtx);
8618        /*
8619        ** Did we reach end-of-file before finding any columns?
8620        ** If so, stop instead of NULL filling the remaining columns.
8621        */
8622        if( z==0 && i==0 ) break;
8623        /*
8624        ** Did we reach end-of-file OR end-of-line before finding any
8625        ** columns in ASCII mode?  If so, stop instead of NULL filling
8626        ** the remaining columns.
8627        */
8628        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8629        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8630        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8631          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8632                          "filling the rest with NULL\n",
8633                          sCtx.zFile, startLine, nCol, i+1);
8634          i += 2;
8635          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8636        }
8637      }
8638      if( sCtx.cTerm==sCtx.cColSep ){
8639        do{
8640          xRead(&sCtx);
8641          i++;
8642        }while( sCtx.cTerm==sCtx.cColSep );
8643        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8644                        "extras ignored\n",
8645                        sCtx.zFile, startLine, nCol, i);
8646      }
8647      if( i>=nCol ){
8648        sqlite3_step(pStmt);
8649        rc = sqlite3_reset(pStmt);
8650        if( rc!=SQLITE_OK ){
8651          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8652                      startLine, sqlite3_errmsg(p->db));
8653          sCtx.nErr++;
8654        }else{
8655          sCtx.nRow++;
8656        }
8657      }
8658    }while( sCtx.cTerm!=EOF );
8659
8660    import_cleanup(&sCtx);
8661    sqlite3_finalize(pStmt);
8662    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8663    if( eVerbose>0 ){
8664      utf8_printf(p->out,
8665          "Added %d rows with %d errors using %d lines of input\n",
8666          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8667    }
8668  }else
8669#endif /* !defined(SQLITE_SHELL_FIDDLE) */
8670
8671#ifndef SQLITE_UNTESTABLE
8672  if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
8673    char *zSql;
8674    char *zCollist = 0;
8675    sqlite3_stmt *pStmt;
8676    int tnum = 0;
8677    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8678    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8679    int i;
8680    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8681      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8682                          "       .imposter off\n");
8683      /* Also allowed, but not documented:
8684      **
8685      **    .imposter TABLE IMPOSTER
8686      **
8687      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8688      ** imposter is another WITHOUT ROWID table with the columns in
8689      ** storage order. */
8690      rc = 1;
8691      goto meta_command_exit;
8692    }
8693    open_db(p, 0);
8694    if( nArg==2 ){
8695      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8696      goto meta_command_exit;
8697    }
8698    zSql = sqlite3_mprintf(
8699      "SELECT rootpage, 0 FROM sqlite_schema"
8700      " WHERE name='%q' AND type='index'"
8701      "UNION ALL "
8702      "SELECT rootpage, 1 FROM sqlite_schema"
8703      " WHERE name='%q' AND type='table'"
8704      "   AND sql LIKE '%%without%%rowid%%'",
8705      azArg[1], azArg[1]
8706    );
8707    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8708    sqlite3_free(zSql);
8709    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8710      tnum = sqlite3_column_int(pStmt, 0);
8711      isWO = sqlite3_column_int(pStmt, 1);
8712    }
8713    sqlite3_finalize(pStmt);
8714    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8715    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8716    sqlite3_free(zSql);
8717    i = 0;
8718    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8719      char zLabel[20];
8720      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8721      i++;
8722      if( zCol==0 ){
8723        if( sqlite3_column_int(pStmt,1)==-1 ){
8724          zCol = "_ROWID_";
8725        }else{
8726          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8727          zCol = zLabel;
8728        }
8729      }
8730      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8731        lenPK = (int)strlen(zCollist);
8732      }
8733      if( zCollist==0 ){
8734        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8735      }else{
8736        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8737      }
8738    }
8739    sqlite3_finalize(pStmt);
8740    if( i==0 || tnum==0 ){
8741      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8742      rc = 1;
8743      sqlite3_free(zCollist);
8744      goto meta_command_exit;
8745    }
8746    if( lenPK==0 ) lenPK = 100000;
8747    zSql = sqlite3_mprintf(
8748          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8749          azArg[2], zCollist, lenPK, zCollist);
8750    sqlite3_free(zCollist);
8751    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8752    if( rc==SQLITE_OK ){
8753      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8754      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8755      if( rc ){
8756        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8757      }else{
8758        utf8_printf(stdout, "%s;\n", zSql);
8759        raw_printf(stdout,
8760          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8761          azArg[1], isWO ? "table" : "index"
8762        );
8763      }
8764    }else{
8765      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8766      rc = 1;
8767    }
8768    sqlite3_free(zSql);
8769  }else
8770#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8771
8772#ifdef SQLITE_ENABLE_IOTRACE
8773  if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
8774    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8775    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8776    iotrace = 0;
8777    if( nArg<2 ){
8778      sqlite3IoTrace = 0;
8779    }else if( cli_strcmp(azArg[1], "-")==0 ){
8780      sqlite3IoTrace = iotracePrintf;
8781      iotrace = stdout;
8782    }else{
8783      iotrace = fopen(azArg[1], "w");
8784      if( iotrace==0 ){
8785        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8786        sqlite3IoTrace = 0;
8787        rc = 1;
8788      }else{
8789        sqlite3IoTrace = iotracePrintf;
8790      }
8791    }
8792  }else
8793#endif
8794
8795  if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
8796    static const struct {
8797       const char *zLimitName;   /* Name of a limit */
8798       int limitCode;            /* Integer code for that limit */
8799    } aLimit[] = {
8800      { "length",                SQLITE_LIMIT_LENGTH                    },
8801      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8802      { "column",                SQLITE_LIMIT_COLUMN                    },
8803      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8804      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8805      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8806      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8807      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8808      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8809      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8810      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8811      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8812    };
8813    int i, n2;
8814    open_db(p, 0);
8815    if( nArg==1 ){
8816      for(i=0; i<ArraySize(aLimit); i++){
8817        printf("%20s %d\n", aLimit[i].zLimitName,
8818               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8819      }
8820    }else if( nArg>3 ){
8821      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8822      rc = 1;
8823      goto meta_command_exit;
8824    }else{
8825      int iLimit = -1;
8826      n2 = strlen30(azArg[1]);
8827      for(i=0; i<ArraySize(aLimit); i++){
8828        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8829          if( iLimit<0 ){
8830            iLimit = i;
8831          }else{
8832            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8833            rc = 1;
8834            goto meta_command_exit;
8835          }
8836        }
8837      }
8838      if( iLimit<0 ){
8839        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8840                        "enter \".limits\" with no arguments for a list.\n",
8841                         azArg[1]);
8842        rc = 1;
8843        goto meta_command_exit;
8844      }
8845      if( nArg==3 ){
8846        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8847                      (int)integerValue(azArg[2]));
8848      }
8849      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8850             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8851    }
8852  }else
8853
8854  if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
8855    open_db(p, 0);
8856    lintDotCommand(p, azArg, nArg);
8857  }else
8858
8859#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
8860  if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
8861    const char *zFile, *zProc;
8862    char *zErrMsg = 0;
8863    failIfSafeMode(p, "cannot run .load in safe mode");
8864    if( nArg<2 ){
8865      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8866      rc = 1;
8867      goto meta_command_exit;
8868    }
8869    zFile = azArg[1];
8870    zProc = nArg>=3 ? azArg[2] : 0;
8871    open_db(p, 0);
8872    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8873    if( rc!=SQLITE_OK ){
8874      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8875      sqlite3_free(zErrMsg);
8876      rc = 1;
8877    }
8878  }else
8879#endif
8880
8881#ifndef SQLITE_SHELL_FIDDLE
8882  if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
8883    failIfSafeMode(p, "cannot run .log in safe mode");
8884    if( nArg!=2 ){
8885      raw_printf(stderr, "Usage: .log FILENAME\n");
8886      rc = 1;
8887    }else{
8888      const char *zFile = azArg[1];
8889      output_file_close(p->pLog);
8890      p->pLog = output_file_open(zFile, 0);
8891    }
8892  }else
8893#endif
8894
8895  if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
8896    const char *zMode = 0;
8897    const char *zTabname = 0;
8898    int i, n2;
8899    ColModeOpts cmOpts = ColModeOpts_default;
8900    for(i=1; i<nArg; i++){
8901      const char *z = azArg[i];
8902      if( optionMatch(z,"wrap") && i+1<nArg ){
8903        cmOpts.iWrap = integerValue(azArg[++i]);
8904      }else if( optionMatch(z,"ww") ){
8905        cmOpts.bWordWrap = 1;
8906      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
8907        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
8908      }else if( optionMatch(z,"quote") ){
8909        cmOpts.bQuote = 1;
8910      }else if( optionMatch(z,"noquote") ){
8911        cmOpts.bQuote = 0;
8912      }else if( zMode==0 ){
8913        zMode = z;
8914        /* Apply defaults for qbox pseudo-mode.  If that
8915         * overwrites already-set values, user was informed of this.
8916         */
8917        if( cli_strcmp(z, "qbox")==0 ){
8918          ColModeOpts cmo = ColModeOpts_default_qbox;
8919          zMode = "box";
8920          cmOpts = cmo;
8921        }
8922      }else if( zTabname==0 ){
8923        zTabname = z;
8924      }else if( z[0]=='-' ){
8925        utf8_printf(stderr, "unknown option: %s\n", z);
8926        utf8_printf(stderr, "options:\n"
8927                            "  --noquote\n"
8928                            "  --quote\n"
8929                            "  --wordwrap on/off\n"
8930                            "  --wrap N\n"
8931                            "  --ww\n");
8932        rc = 1;
8933        goto meta_command_exit;
8934      }else{
8935        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8936        rc = 1;
8937        goto meta_command_exit;
8938      }
8939    }
8940    if( zMode==0 ){
8941      if( p->mode==MODE_Column
8942       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
8943      ){
8944        raw_printf
8945          (p->out,
8946           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
8947           modeDescr[p->mode], p->cmOpts.iWrap,
8948           p->cmOpts.bWordWrap ? "on" : "off",
8949           p->cmOpts.bQuote ? "" : "no");
8950      }else{
8951        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8952      }
8953      zMode = modeDescr[p->mode];
8954    }
8955    n2 = strlen30(zMode);
8956    if( cli_strncmp(zMode,"lines",n2)==0 ){
8957      p->mode = MODE_Line;
8958      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8959    }else if( cli_strncmp(zMode,"columns",n2)==0 ){
8960      p->mode = MODE_Column;
8961      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8962        p->showHeader = 1;
8963      }
8964      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8965      p->cmOpts = cmOpts;
8966    }else if( cli_strncmp(zMode,"list",n2)==0 ){
8967      p->mode = MODE_List;
8968      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8969      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8970    }else if( cli_strncmp(zMode,"html",n2)==0 ){
8971      p->mode = MODE_Html;
8972    }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
8973      p->mode = MODE_Tcl;
8974      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8975      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8976    }else if( cli_strncmp(zMode,"csv",n2)==0 ){
8977      p->mode = MODE_Csv;
8978      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8979      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8980    }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
8981      p->mode = MODE_List;
8982      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8983    }else if( cli_strncmp(zMode,"insert",n2)==0 ){
8984      p->mode = MODE_Insert;
8985      set_table_name(p, zTabname ? zTabname : "table");
8986    }else if( cli_strncmp(zMode,"quote",n2)==0 ){
8987      p->mode = MODE_Quote;
8988      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8989      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8990    }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
8991      p->mode = MODE_Ascii;
8992      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8993      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8994    }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
8995      p->mode = MODE_Markdown;
8996      p->cmOpts = cmOpts;
8997    }else if( cli_strncmp(zMode,"table",n2)==0 ){
8998      p->mode = MODE_Table;
8999      p->cmOpts = cmOpts;
9000    }else if( cli_strncmp(zMode,"box",n2)==0 ){
9001      p->mode = MODE_Box;
9002      p->cmOpts = cmOpts;
9003    }else if( cli_strncmp(zMode,"count",n2)==0 ){
9004      p->mode = MODE_Count;
9005    }else if( cli_strncmp(zMode,"off",n2)==0 ){
9006      p->mode = MODE_Off;
9007    }else if( cli_strncmp(zMode,"json",n2)==0 ){
9008      p->mode = MODE_Json;
9009    }else{
9010      raw_printf(stderr, "Error: mode should be one of: "
9011         "ascii box column csv html insert json line list markdown "
9012         "qbox quote table tabs tcl\n");
9013      rc = 1;
9014    }
9015    p->cMode = p->mode;
9016  }else
9017
9018#ifndef SQLITE_SHELL_FIDDLE
9019  if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
9020    if( nArg!=2 ){
9021      raw_printf(stderr, "Usage: .nonce NONCE\n");
9022      rc = 1;
9023    }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
9024      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9025                 p->lineno, azArg[1]);
9026      exit(1);
9027    }else{
9028      p->bSafeMode = 0;
9029      return 0;  /* Return immediately to bypass the safe mode reset
9030                 ** at the end of this procedure */
9031    }
9032  }else
9033#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9034
9035  if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
9036    if( nArg==2 ){
9037      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9038                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9039    }else{
9040      raw_printf(stderr, "Usage: .nullvalue STRING\n");
9041      rc = 1;
9042    }
9043  }else
9044
9045  if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
9046    const char *zFN = 0;     /* Pointer to constant filename */
9047    char *zNewFilename = 0;  /* Name of the database file to open */
9048    int iName = 1;           /* Index in azArg[] of the filename */
9049    int newFlag = 0;         /* True to delete file before opening */
9050    int openMode = SHELL_OPEN_UNSPEC;
9051
9052    /* Check for command-line arguments */
9053    for(iName=1; iName<nArg; iName++){
9054      const char *z = azArg[iName];
9055#ifndef SQLITE_SHELL_FIDDLE
9056      if( optionMatch(z,"new") ){
9057        newFlag = 1;
9058#ifdef SQLITE_HAVE_ZLIB
9059      }else if( optionMatch(z, "zip") ){
9060        openMode = SHELL_OPEN_ZIPFILE;
9061#endif
9062      }else if( optionMatch(z, "append") ){
9063        openMode = SHELL_OPEN_APPENDVFS;
9064      }else if( optionMatch(z, "readonly") ){
9065        openMode = SHELL_OPEN_READONLY;
9066      }else if( optionMatch(z, "nofollow") ){
9067        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9068#ifndef SQLITE_OMIT_DESERIALIZE
9069      }else if( optionMatch(z, "deserialize") ){
9070        openMode = SHELL_OPEN_DESERIALIZE;
9071      }else if( optionMatch(z, "hexdb") ){
9072        openMode = SHELL_OPEN_HEXDB;
9073      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9074        p->szMax = integerValue(azArg[++iName]);
9075#endif /* SQLITE_OMIT_DESERIALIZE */
9076      }else
9077#endif /* !SQLITE_SHELL_FIDDLE */
9078      if( z[0]=='-' ){
9079        utf8_printf(stderr, "unknown option: %s\n", z);
9080        rc = 1;
9081        goto meta_command_exit;
9082      }else if( zFN ){
9083        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9084        rc = 1;
9085        goto meta_command_exit;
9086      }else{
9087        zFN = z;
9088      }
9089    }
9090
9091    /* Close the existing database */
9092    session_close_all(p, -1);
9093    close_db(p->db);
9094    p->db = 0;
9095    p->pAuxDb->zDbFilename = 0;
9096    sqlite3_free(p->pAuxDb->zFreeOnClose);
9097    p->pAuxDb->zFreeOnClose = 0;
9098    p->openMode = openMode;
9099    p->openFlags = 0;
9100    p->szMax = 0;
9101
9102    /* If a filename is specified, try to open it first */
9103    if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9104      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9105#ifndef SQLITE_SHELL_FIDDLE
9106      if( p->bSafeMode
9107       && p->openMode!=SHELL_OPEN_HEXDB
9108       && zFN
9109       && cli_strcmp(zFN,":memory:")!=0
9110      ){
9111        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9112      }
9113#else
9114      /* WASM mode has its own sandboxed pseudo-filesystem. */
9115#endif
9116      if( zFN ){
9117        zNewFilename = sqlite3_mprintf("%s", zFN);
9118        shell_check_oom(zNewFilename);
9119      }else{
9120        zNewFilename = 0;
9121      }
9122      p->pAuxDb->zDbFilename = zNewFilename;
9123      open_db(p, OPEN_DB_KEEPALIVE);
9124      if( p->db==0 ){
9125        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9126        sqlite3_free(zNewFilename);
9127      }else{
9128        p->pAuxDb->zFreeOnClose = zNewFilename;
9129      }
9130    }
9131    if( p->db==0 ){
9132      /* As a fall-back open a TEMP database */
9133      p->pAuxDb->zDbFilename = 0;
9134      open_db(p, 0);
9135    }
9136  }else
9137
9138#ifndef SQLITE_SHELL_FIDDLE
9139  if( (c=='o'
9140        && (cli_strncmp(azArg[0], "output", n)==0
9141            || cli_strncmp(azArg[0], "once", n)==0))
9142   || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
9143  ){
9144    char *zFile = 0;
9145    int bTxtMode = 0;
9146    int i;
9147    int eMode = 0;
9148    int bOnce = 0;            /* 0: .output, 1: .once, 2: .excel */
9149    unsigned char zBOM[4];    /* Byte-order mark to using if --bom is present */
9150
9151    zBOM[0] = 0;
9152    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9153    if( c=='e' ){
9154      eMode = 'x';
9155      bOnce = 2;
9156    }else if( cli_strncmp(azArg[0],"once",n)==0 ){
9157      bOnce = 1;
9158    }
9159    for(i=1; i<nArg; i++){
9160      char *z = azArg[i];
9161      if( z[0]=='-' ){
9162        if( z[1]=='-' ) z++;
9163        if( cli_strcmp(z,"-bom")==0 ){
9164          zBOM[0] = 0xef;
9165          zBOM[1] = 0xbb;
9166          zBOM[2] = 0xbf;
9167          zBOM[3] = 0;
9168        }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
9169          eMode = 'x';  /* spreadsheet */
9170        }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
9171          eMode = 'e';  /* text editor */
9172        }else{
9173          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9174                      azArg[i]);
9175          showHelp(p->out, azArg[0]);
9176          rc = 1;
9177          goto meta_command_exit;
9178        }
9179      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9180        zFile = sqlite3_mprintf("%s", z);
9181        if( zFile && zFile[0]=='|' ){
9182          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9183          break;
9184        }
9185      }else{
9186        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9187                    azArg[i]);
9188        showHelp(p->out, azArg[0]);
9189        rc = 1;
9190        sqlite3_free(zFile);
9191        goto meta_command_exit;
9192      }
9193    }
9194    if( zFile==0 ){
9195      zFile = sqlite3_mprintf("stdout");
9196    }
9197    if( bOnce ){
9198      p->outCount = 2;
9199    }else{
9200      p->outCount = 0;
9201    }
9202    output_reset(p);
9203#ifndef SQLITE_NOHAVE_SYSTEM
9204    if( eMode=='e' || eMode=='x' ){
9205      p->doXdgOpen = 1;
9206      outputModePush(p);
9207      if( eMode=='x' ){
9208        /* spreadsheet mode.  Output as CSV. */
9209        newTempFile(p, "csv");
9210        ShellClearFlag(p, SHFLG_Echo);
9211        p->mode = MODE_Csv;
9212        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9213        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9214      }else{
9215        /* text editor mode */
9216        newTempFile(p, "txt");
9217        bTxtMode = 1;
9218      }
9219      sqlite3_free(zFile);
9220      zFile = sqlite3_mprintf("%s", p->zTempFile);
9221    }
9222#endif /* SQLITE_NOHAVE_SYSTEM */
9223    shell_check_oom(zFile);
9224    if( zFile[0]=='|' ){
9225#ifdef SQLITE_OMIT_POPEN
9226      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9227      rc = 1;
9228      p->out = stdout;
9229#else
9230      p->out = popen(zFile + 1, "w");
9231      if( p->out==0 ){
9232        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9233        p->out = stdout;
9234        rc = 1;
9235      }else{
9236        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9237        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9238      }
9239#endif
9240    }else{
9241      p->out = output_file_open(zFile, bTxtMode);
9242      if( p->out==0 ){
9243        if( cli_strcmp(zFile,"off")!=0 ){
9244          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9245        }
9246        p->out = stdout;
9247        rc = 1;
9248      } else {
9249        if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9250        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9251      }
9252    }
9253    sqlite3_free(zFile);
9254  }else
9255#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9256
9257  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
9258    open_db(p,0);
9259    if( nArg<=1 ) goto parameter_syntax_error;
9260
9261    /* .parameter clear
9262    ** Clear all bind parameters by dropping the TEMP table that holds them.
9263    */
9264    if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
9265      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9266                   0, 0, 0);
9267    }else
9268
9269    /* .parameter list
9270    ** List all bind parameters.
9271    */
9272    if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
9273      sqlite3_stmt *pStmt = 0;
9274      int rx;
9275      int len = 0;
9276      rx = sqlite3_prepare_v2(p->db,
9277             "SELECT max(length(key)) "
9278             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9279      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9280        len = sqlite3_column_int(pStmt, 0);
9281        if( len>40 ) len = 40;
9282      }
9283      sqlite3_finalize(pStmt);
9284      pStmt = 0;
9285      if( len ){
9286        rx = sqlite3_prepare_v2(p->db,
9287             "SELECT key, quote(value) "
9288             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9289        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9290          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9291                      sqlite3_column_text(pStmt,1));
9292        }
9293        sqlite3_finalize(pStmt);
9294      }
9295    }else
9296
9297    /* .parameter init
9298    ** Make sure the TEMP table used to hold bind parameters exists.
9299    ** Create it if necessary.
9300    */
9301    if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
9302      bind_table_init(p);
9303    }else
9304
9305    /* .parameter set NAME VALUE
9306    ** Set or reset a bind parameter.  NAME should be the full parameter
9307    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9308    ** VALUE can be in either SQL literal notation, or if not it will be
9309    ** understood to be a text string.
9310    */
9311    if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
9312      int rx;
9313      char *zSql;
9314      sqlite3_stmt *pStmt;
9315      const char *zKey = azArg[2];
9316      const char *zValue = azArg[3];
9317      bind_table_init(p);
9318      zSql = sqlite3_mprintf(
9319                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9320                  "VALUES(%Q,%s);", zKey, zValue);
9321      shell_check_oom(zSql);
9322      pStmt = 0;
9323      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9324      sqlite3_free(zSql);
9325      if( rx!=SQLITE_OK ){
9326        sqlite3_finalize(pStmt);
9327        pStmt = 0;
9328        zSql = sqlite3_mprintf(
9329                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9330                   "VALUES(%Q,%Q);", zKey, zValue);
9331        shell_check_oom(zSql);
9332        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9333        sqlite3_free(zSql);
9334        if( rx!=SQLITE_OK ){
9335          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9336          sqlite3_finalize(pStmt);
9337          pStmt = 0;
9338          rc = 1;
9339        }
9340      }
9341      sqlite3_step(pStmt);
9342      sqlite3_finalize(pStmt);
9343    }else
9344
9345    /* .parameter unset NAME
9346    ** Remove the NAME binding from the parameter binding table, if it
9347    ** exists.
9348    */
9349    if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
9350      char *zSql = sqlite3_mprintf(
9351          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9352      shell_check_oom(zSql);
9353      sqlite3_exec(p->db, zSql, 0, 0, 0);
9354      sqlite3_free(zSql);
9355    }else
9356    /* If no command name matches, show a syntax error */
9357    parameter_syntax_error:
9358    showHelp(p->out, "parameter");
9359  }else
9360
9361  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
9362    int i;
9363    for(i=1; i<nArg; i++){
9364      if( i>1 ) raw_printf(p->out, " ");
9365      utf8_printf(p->out, "%s", azArg[i]);
9366    }
9367    raw_printf(p->out, "\n");
9368  }else
9369
9370#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9371  if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
9372    int i;
9373    int nn = 0;
9374    p->flgProgress = 0;
9375    p->mxProgress = 0;
9376    p->nProgress = 0;
9377    for(i=1; i<nArg; i++){
9378      const char *z = azArg[i];
9379      if( z[0]=='-' ){
9380        z++;
9381        if( z[0]=='-' ) z++;
9382        if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
9383          p->flgProgress |= SHELL_PROGRESS_QUIET;
9384          continue;
9385        }
9386        if( cli_strcmp(z,"reset")==0 ){
9387          p->flgProgress |= SHELL_PROGRESS_RESET;
9388          continue;
9389        }
9390        if( cli_strcmp(z,"once")==0 ){
9391          p->flgProgress |= SHELL_PROGRESS_ONCE;
9392          continue;
9393        }
9394        if( cli_strcmp(z,"limit")==0 ){
9395          if( i+1>=nArg ){
9396            utf8_printf(stderr, "Error: missing argument on --limit\n");
9397            rc = 1;
9398            goto meta_command_exit;
9399          }else{
9400            p->mxProgress = (int)integerValue(azArg[++i]);
9401          }
9402          continue;
9403        }
9404        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9405        rc = 1;
9406        goto meta_command_exit;
9407      }else{
9408        nn = (int)integerValue(z);
9409      }
9410    }
9411    open_db(p, 0);
9412    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9413  }else
9414#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9415
9416  if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
9417    if( nArg >= 2) {
9418      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9419    }
9420    if( nArg >= 3) {
9421      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9422    }
9423  }else
9424
9425#ifndef SQLITE_SHELL_FIDDLE
9426  if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
9427    rc = 2;
9428  }else
9429#endif
9430
9431#ifndef SQLITE_SHELL_FIDDLE
9432  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
9433    FILE *inSaved = p->in;
9434    int savedLineno = p->lineno;
9435    failIfSafeMode(p, "cannot run .read in safe mode");
9436    if( nArg!=2 ){
9437      raw_printf(stderr, "Usage: .read FILE\n");
9438      rc = 1;
9439      goto meta_command_exit;
9440    }
9441    if( azArg[1][0]=='|' ){
9442#ifdef SQLITE_OMIT_POPEN
9443      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9444      rc = 1;
9445      p->out = stdout;
9446#else
9447      p->in = popen(azArg[1]+1, "r");
9448      if( p->in==0 ){
9449        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9450        rc = 1;
9451      }else{
9452        rc = process_input(p);
9453        pclose(p->in);
9454      }
9455#endif
9456    }else if( (p->in = openChrSource(azArg[1]))==0 ){
9457      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9458      rc = 1;
9459    }else{
9460      rc = process_input(p);
9461      fclose(p->in);
9462    }
9463    p->in = inSaved;
9464    p->lineno = savedLineno;
9465  }else
9466#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9467
9468#ifndef SQLITE_SHELL_FIDDLE
9469  if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
9470    const char *zSrcFile;
9471    const char *zDb;
9472    sqlite3 *pSrc;
9473    sqlite3_backup *pBackup;
9474    int nTimeout = 0;
9475
9476    failIfSafeMode(p, "cannot run .restore in safe mode");
9477    if( nArg==2 ){
9478      zSrcFile = azArg[1];
9479      zDb = "main";
9480    }else if( nArg==3 ){
9481      zSrcFile = azArg[2];
9482      zDb = azArg[1];
9483    }else{
9484      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9485      rc = 1;
9486      goto meta_command_exit;
9487    }
9488    rc = sqlite3_open(zSrcFile, &pSrc);
9489    if( rc!=SQLITE_OK ){
9490      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9491      close_db(pSrc);
9492      return 1;
9493    }
9494    open_db(p, 0);
9495    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9496    if( pBackup==0 ){
9497      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9498      close_db(pSrc);
9499      return 1;
9500    }
9501    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9502          || rc==SQLITE_BUSY  ){
9503      if( rc==SQLITE_BUSY ){
9504        if( nTimeout++ >= 3 ) break;
9505        sqlite3_sleep(100);
9506      }
9507    }
9508    sqlite3_backup_finish(pBackup);
9509    if( rc==SQLITE_DONE ){
9510      rc = 0;
9511    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9512      raw_printf(stderr, "Error: source database is busy\n");
9513      rc = 1;
9514    }else{
9515      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9516      rc = 1;
9517    }
9518    close_db(pSrc);
9519  }else
9520#endif /* !defined(SQLITE_SHELL_FIDDLE) */
9521
9522  if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
9523    if( nArg==2 ){
9524      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9525#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9526      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9527#endif
9528    }else{
9529      raw_printf(stderr, "Usage: .scanstats on|off\n");
9530      rc = 1;
9531    }
9532  }else
9533
9534  if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
9535    ShellText sSelect;
9536    ShellState data;
9537    char *zErrMsg = 0;
9538    const char *zDiv = "(";
9539    const char *zName = 0;
9540    int iSchema = 0;
9541    int bDebug = 0;
9542    int bNoSystemTabs = 0;
9543    int ii;
9544
9545    open_db(p, 0);
9546    memcpy(&data, p, sizeof(data));
9547    data.showHeader = 0;
9548    data.cMode = data.mode = MODE_Semi;
9549    initText(&sSelect);
9550    for(ii=1; ii<nArg; ii++){
9551      if( optionMatch(azArg[ii],"indent") ){
9552        data.cMode = data.mode = MODE_Pretty;
9553      }else if( optionMatch(azArg[ii],"debug") ){
9554        bDebug = 1;
9555      }else if( optionMatch(azArg[ii],"nosys") ){
9556        bNoSystemTabs = 1;
9557      }else if( azArg[ii][0]=='-' ){
9558        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9559        rc = 1;
9560        goto meta_command_exit;
9561      }else if( zName==0 ){
9562        zName = azArg[ii];
9563      }else{
9564        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9565        rc = 1;
9566        goto meta_command_exit;
9567      }
9568    }
9569    if( zName!=0 ){
9570      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9571                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9572                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9573                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9574      if( isSchema ){
9575        char *new_argv[2], *new_colv[2];
9576        new_argv[0] = sqlite3_mprintf(
9577                      "CREATE TABLE %s (\n"
9578                      "  type text,\n"
9579                      "  name text,\n"
9580                      "  tbl_name text,\n"
9581                      "  rootpage integer,\n"
9582                      "  sql text\n"
9583                      ")", zName);
9584        shell_check_oom(new_argv[0]);
9585        new_argv[1] = 0;
9586        new_colv[0] = "sql";
9587        new_colv[1] = 0;
9588        callback(&data, 1, new_argv, new_colv);
9589        sqlite3_free(new_argv[0]);
9590      }
9591    }
9592    if( zDiv ){
9593      sqlite3_stmt *pStmt = 0;
9594      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9595                              -1, &pStmt, 0);
9596      if( rc ){
9597        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9598        sqlite3_finalize(pStmt);
9599        rc = 1;
9600        goto meta_command_exit;
9601      }
9602      appendText(&sSelect, "SELECT sql FROM", 0);
9603      iSchema = 0;
9604      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9605        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9606        char zScNum[30];
9607        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9608        appendText(&sSelect, zDiv, 0);
9609        zDiv = " UNION ALL ";
9610        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9611        if( sqlite3_stricmp(zDb, "main")!=0 ){
9612          appendText(&sSelect, zDb, '\'');
9613        }else{
9614          appendText(&sSelect, "NULL", 0);
9615        }
9616        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9617        appendText(&sSelect, zScNum, 0);
9618        appendText(&sSelect, " AS snum, ", 0);
9619        appendText(&sSelect, zDb, '\'');
9620        appendText(&sSelect, " AS sname FROM ", 0);
9621        appendText(&sSelect, zDb, quoteChar(zDb));
9622        appendText(&sSelect, ".sqlite_schema", 0);
9623      }
9624      sqlite3_finalize(pStmt);
9625#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9626      if( zName ){
9627        appendText(&sSelect,
9628           " UNION ALL SELECT shell_module_schema(name),"
9629           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9630        0);
9631      }
9632#endif
9633      appendText(&sSelect, ") WHERE ", 0);
9634      if( zName ){
9635        char *zQarg = sqlite3_mprintf("%Q", zName);
9636        int bGlob;
9637        shell_check_oom(zQarg);
9638        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9639                strchr(zName, '[') != 0;
9640        if( strchr(zName, '.') ){
9641          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9642        }else{
9643          appendText(&sSelect, "lower(tbl_name)", 0);
9644        }
9645        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9646        appendText(&sSelect, zQarg, 0);
9647        if( !bGlob ){
9648          appendText(&sSelect, " ESCAPE '\\' ", 0);
9649        }
9650        appendText(&sSelect, " AND ", 0);
9651        sqlite3_free(zQarg);
9652      }
9653      if( bNoSystemTabs ){
9654        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9655      }
9656      appendText(&sSelect, "sql IS NOT NULL"
9657                           " ORDER BY snum, rowid", 0);
9658      if( bDebug ){
9659        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9660      }else{
9661        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9662      }
9663      freeText(&sSelect);
9664    }
9665    if( zErrMsg ){
9666      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9667      sqlite3_free(zErrMsg);
9668      rc = 1;
9669    }else if( rc != SQLITE_OK ){
9670      raw_printf(stderr,"Error: querying schema information\n");
9671      rc = 1;
9672    }else{
9673      rc = 0;
9674    }
9675  }else
9676
9677  if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
9678   || (c=='t' && n==9  && cli_strncmp(azArg[0], "treetrace", n)==0)
9679  ){
9680    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9681    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9682  }else
9683
9684#if defined(SQLITE_ENABLE_SESSION)
9685  if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
9686    struct AuxDb *pAuxDb = p->pAuxDb;
9687    OpenSession *pSession = &pAuxDb->aSession[0];
9688    char **azCmd = &azArg[1];
9689    int iSes = 0;
9690    int nCmd = nArg - 1;
9691    int i;
9692    if( nArg<=1 ) goto session_syntax_error;
9693    open_db(p, 0);
9694    if( nArg>=3 ){
9695      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
9696        if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
9697      }
9698      if( iSes<pAuxDb->nSession ){
9699        pSession = &pAuxDb->aSession[iSes];
9700        azCmd++;
9701        nCmd--;
9702      }else{
9703        pSession = &pAuxDb->aSession[0];
9704        iSes = 0;
9705      }
9706    }
9707
9708    /* .session attach TABLE
9709    ** Invoke the sqlite3session_attach() interface to attach a particular
9710    ** table so that it is never filtered.
9711    */
9712    if( cli_strcmp(azCmd[0],"attach")==0 ){
9713      if( nCmd!=2 ) goto session_syntax_error;
9714      if( pSession->p==0 ){
9715        session_not_open:
9716        raw_printf(stderr, "ERROR: No sessions are open\n");
9717      }else{
9718        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9719        if( rc ){
9720          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9721          rc = 0;
9722        }
9723      }
9724    }else
9725
9726    /* .session changeset FILE
9727    ** .session patchset FILE
9728    ** Write a changeset or patchset into a file.  The file is overwritten.
9729    */
9730    if( cli_strcmp(azCmd[0],"changeset")==0
9731     || cli_strcmp(azCmd[0],"patchset")==0
9732    ){
9733      FILE *out = 0;
9734      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
9735      if( nCmd!=2 ) goto session_syntax_error;
9736      if( pSession->p==0 ) goto session_not_open;
9737      out = fopen(azCmd[1], "wb");
9738      if( out==0 ){
9739        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9740                    azCmd[1]);
9741      }else{
9742        int szChng;
9743        void *pChng;
9744        if( azCmd[0][0]=='c' ){
9745          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9746        }else{
9747          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9748        }
9749        if( rc ){
9750          printf("Error: error code %d\n", rc);
9751          rc = 0;
9752        }
9753        if( pChng
9754          && fwrite(pChng, szChng, 1, out)!=1 ){
9755          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9756                  szChng);
9757        }
9758        sqlite3_free(pChng);
9759        fclose(out);
9760      }
9761    }else
9762
9763    /* .session close
9764    ** Close the identified session
9765    */
9766    if( cli_strcmp(azCmd[0], "close")==0 ){
9767      if( nCmd!=1 ) goto session_syntax_error;
9768      if( pAuxDb->nSession ){
9769        session_close(pSession);
9770        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
9771      }
9772    }else
9773
9774    /* .session enable ?BOOLEAN?
9775    ** Query or set the enable flag
9776    */
9777    if( cli_strcmp(azCmd[0], "enable")==0 ){
9778      int ii;
9779      if( nCmd>2 ) goto session_syntax_error;
9780      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9781      if( pAuxDb->nSession ){
9782        ii = sqlite3session_enable(pSession->p, ii);
9783        utf8_printf(p->out, "session %s enable flag = %d\n",
9784                    pSession->zName, ii);
9785      }
9786    }else
9787
9788    /* .session filter GLOB ....
9789    ** Set a list of GLOB patterns of table names to be excluded.
9790    */
9791    if( cli_strcmp(azCmd[0], "filter")==0 ){
9792      int ii, nByte;
9793      if( nCmd<2 ) goto session_syntax_error;
9794      if( pAuxDb->nSession ){
9795        for(ii=0; ii<pSession->nFilter; ii++){
9796          sqlite3_free(pSession->azFilter[ii]);
9797        }
9798        sqlite3_free(pSession->azFilter);
9799        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9800        pSession->azFilter = sqlite3_malloc( nByte );
9801        if( pSession->azFilter==0 ){
9802          raw_printf(stderr, "Error: out or memory\n");
9803          exit(1);
9804        }
9805        for(ii=1; ii<nCmd; ii++){
9806          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9807          shell_check_oom(x);
9808        }
9809        pSession->nFilter = ii-1;
9810      }
9811    }else
9812
9813    /* .session indirect ?BOOLEAN?
9814    ** Query or set the indirect flag
9815    */
9816    if( cli_strcmp(azCmd[0], "indirect")==0 ){
9817      int ii;
9818      if( nCmd>2 ) goto session_syntax_error;
9819      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9820      if( pAuxDb->nSession ){
9821        ii = sqlite3session_indirect(pSession->p, ii);
9822        utf8_printf(p->out, "session %s indirect flag = %d\n",
9823                    pSession->zName, ii);
9824      }
9825    }else
9826
9827    /* .session isempty
9828    ** Determine if the session is empty
9829    */
9830    if( cli_strcmp(azCmd[0], "isempty")==0 ){
9831      int ii;
9832      if( nCmd!=1 ) goto session_syntax_error;
9833      if( pAuxDb->nSession ){
9834        ii = sqlite3session_isempty(pSession->p);
9835        utf8_printf(p->out, "session %s isempty flag = %d\n",
9836                    pSession->zName, ii);
9837      }
9838    }else
9839
9840    /* .session list
9841    ** List all currently open sessions
9842    */
9843    if( cli_strcmp(azCmd[0],"list")==0 ){
9844      for(i=0; i<pAuxDb->nSession; i++){
9845        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
9846      }
9847    }else
9848
9849    /* .session open DB NAME
9850    ** Open a new session called NAME on the attached database DB.
9851    ** DB is normally "main".
9852    */
9853    if( cli_strcmp(azCmd[0],"open")==0 ){
9854      char *zName;
9855      if( nCmd!=3 ) goto session_syntax_error;
9856      zName = azCmd[2];
9857      if( zName[0]==0 ) goto session_syntax_error;
9858      for(i=0; i<pAuxDb->nSession; i++){
9859        if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
9860          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9861          goto meta_command_exit;
9862        }
9863      }
9864      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
9865        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
9866        goto meta_command_exit;
9867      }
9868      pSession = &pAuxDb->aSession[pAuxDb->nSession];
9869      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9870      if( rc ){
9871        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9872        rc = 0;
9873        goto meta_command_exit;
9874      }
9875      pSession->nFilter = 0;
9876      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9877      pAuxDb->nSession++;
9878      pSession->zName = sqlite3_mprintf("%s", zName);
9879      shell_check_oom(pSession->zName);
9880    }else
9881    /* If no command name matches, show a syntax error */
9882    session_syntax_error:
9883    showHelp(p->out, "session");
9884  }else
9885#endif
9886
9887#ifdef SQLITE_DEBUG
9888  /* Undocumented commands for internal testing.  Subject to change
9889  ** without notice. */
9890  if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
9891    if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9892      int i, v;
9893      for(i=1; i<nArg; i++){
9894        v = booleanValue(azArg[i]);
9895        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9896      }
9897    }
9898    if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
9899      int i; sqlite3_int64 v;
9900      for(i=1; i<nArg; i++){
9901        char zBuf[200];
9902        v = integerValue(azArg[i]);
9903        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9904        utf8_printf(p->out, "%s", zBuf);
9905      }
9906    }
9907  }else
9908#endif
9909
9910  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
9911    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9912    int bVerbose = 0;        /* Verbose output */
9913    int bSelftestExists;     /* True if SELFTEST already exists */
9914    int i, k;                /* Loop counters */
9915    int nTest = 0;           /* Number of tests runs */
9916    int nErr = 0;            /* Number of errors seen */
9917    ShellText str;           /* Answer for a query */
9918    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9919
9920    open_db(p,0);
9921    for(i=1; i<nArg; i++){
9922      const char *z = azArg[i];
9923      if( z[0]=='-' && z[1]=='-' ) z++;
9924      if( cli_strcmp(z,"-init")==0 ){
9925        bIsInit = 1;
9926      }else
9927      if( cli_strcmp(z,"-v")==0 ){
9928        bVerbose++;
9929      }else
9930      {
9931        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9932                    azArg[i], azArg[0]);
9933        raw_printf(stderr, "Should be one of: --init -v\n");
9934        rc = 1;
9935        goto meta_command_exit;
9936      }
9937    }
9938    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9939           != SQLITE_OK ){
9940      bSelftestExists = 0;
9941    }else{
9942      bSelftestExists = 1;
9943    }
9944    if( bIsInit ){
9945      createSelftestTable(p);
9946      bSelftestExists = 1;
9947    }
9948    initText(&str);
9949    appendText(&str, "x", 0);
9950    for(k=bSelftestExists; k>=0; k--){
9951      if( k==1 ){
9952        rc = sqlite3_prepare_v2(p->db,
9953            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9954            -1, &pStmt, 0);
9955      }else{
9956        rc = sqlite3_prepare_v2(p->db,
9957          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9958          "      (1,'run','PRAGMA integrity_check','ok')",
9959          -1, &pStmt, 0);
9960      }
9961      if( rc ){
9962        raw_printf(stderr, "Error querying the selftest table\n");
9963        rc = 1;
9964        sqlite3_finalize(pStmt);
9965        goto meta_command_exit;
9966      }
9967      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9968        int tno = sqlite3_column_int(pStmt, 0);
9969        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9970        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9971        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9972
9973        if( zOp==0 ) continue;
9974        if( zSql==0 ) continue;
9975        if( zAns==0 ) continue;
9976        k = 0;
9977        if( bVerbose>0 ){
9978          printf("%d: %s %s\n", tno, zOp, zSql);
9979        }
9980        if( cli_strcmp(zOp,"memo")==0 ){
9981          utf8_printf(p->out, "%s\n", zSql);
9982        }else
9983        if( cli_strcmp(zOp,"run")==0 ){
9984          char *zErrMsg = 0;
9985          str.n = 0;
9986          str.z[0] = 0;
9987          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9988          nTest++;
9989          if( bVerbose ){
9990            utf8_printf(p->out, "Result: %s\n", str.z);
9991          }
9992          if( rc || zErrMsg ){
9993            nErr++;
9994            rc = 1;
9995            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9996            sqlite3_free(zErrMsg);
9997          }else if( cli_strcmp(zAns,str.z)!=0 ){
9998            nErr++;
9999            rc = 1;
10000            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10001            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
10002          }
10003        }else
10004        {
10005          utf8_printf(stderr,
10006            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10007          rc = 1;
10008          break;
10009        }
10010      } /* End loop over rows of content from SELFTEST */
10011      sqlite3_finalize(pStmt);
10012    } /* End loop over k */
10013    freeText(&str);
10014    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10015  }else
10016
10017  if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
10018    if( nArg<2 || nArg>3 ){
10019      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10020      rc = 1;
10021    }
10022    if( nArg>=2 ){
10023      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10024                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10025    }
10026    if( nArg>=3 ){
10027      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10028                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10029    }
10030  }else
10031
10032  if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
10033    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
10034    int i;                   /* Loop counter */
10035    int bSchema = 0;         /* Also hash the schema */
10036    int bSeparate = 0;       /* Hash each table separately */
10037    int iSize = 224;         /* Hash algorithm to use */
10038    int bDebug = 0;          /* Only show the query that would have run */
10039    sqlite3_stmt *pStmt;     /* For querying tables names */
10040    char *zSql;              /* SQL to be run */
10041    char *zSep;              /* Separator */
10042    ShellText sSql;          /* Complete SQL for the query to run the hash */
10043    ShellText sQuery;        /* Set of queries used to read all content */
10044    open_db(p, 0);
10045    for(i=1; i<nArg; i++){
10046      const char *z = azArg[i];
10047      if( z[0]=='-' ){
10048        z++;
10049        if( z[0]=='-' ) z++;
10050        if( cli_strcmp(z,"schema")==0 ){
10051          bSchema = 1;
10052        }else
10053        if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
10054         || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
10055        ){
10056          iSize = atoi(&z[5]);
10057        }else
10058        if( cli_strcmp(z,"debug")==0 ){
10059          bDebug = 1;
10060        }else
10061        {
10062          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10063                      azArg[i], azArg[0]);
10064          showHelp(p->out, azArg[0]);
10065          rc = 1;
10066          goto meta_command_exit;
10067        }
10068      }else if( zLike ){
10069        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10070        rc = 1;
10071        goto meta_command_exit;
10072      }else{
10073        zLike = z;
10074        bSeparate = 1;
10075        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10076      }
10077    }
10078    if( bSchema ){
10079      zSql = "SELECT lower(name) FROM sqlite_schema"
10080             " WHERE type='table' AND coalesce(rootpage,0)>1"
10081             " UNION ALL SELECT 'sqlite_schema'"
10082             " ORDER BY 1 collate nocase";
10083    }else{
10084      zSql = "SELECT lower(name) FROM sqlite_schema"
10085             " WHERE type='table' AND coalesce(rootpage,0)>1"
10086             " AND name NOT LIKE 'sqlite_%'"
10087             " ORDER BY 1 collate nocase";
10088    }
10089    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10090    initText(&sQuery);
10091    initText(&sSql);
10092    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10093    zSep = "VALUES(";
10094    while( SQLITE_ROW==sqlite3_step(pStmt) ){
10095      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10096      if( zTab==0 ) continue;
10097      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10098      if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
10099        appendText(&sQuery,"SELECT * FROM ", 0);
10100        appendText(&sQuery,zTab,'"');
10101        appendText(&sQuery," NOT INDEXED;", 0);
10102      }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
10103        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10104                           " ORDER BY name;", 0);
10105      }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
10106        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10107                           " ORDER BY name;", 0);
10108      }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
10109        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10110                           " ORDER BY tbl,idx;", 0);
10111      }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
10112        appendText(&sQuery, "SELECT * FROM ", 0);
10113        appendText(&sQuery, zTab, 0);
10114        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10115      }
10116      appendText(&sSql, zSep, 0);
10117      appendText(&sSql, sQuery.z, '\'');
10118      sQuery.n = 0;
10119      appendText(&sSql, ",", 0);
10120      appendText(&sSql, zTab, '\'');
10121      zSep = "),(";
10122    }
10123    sqlite3_finalize(pStmt);
10124    if( bSeparate ){
10125      zSql = sqlite3_mprintf(
10126          "%s))"
10127          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10128          "   FROM [sha3sum$query]",
10129          sSql.z, iSize);
10130    }else{
10131      zSql = sqlite3_mprintf(
10132          "%s))"
10133          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10134          "   FROM [sha3sum$query]",
10135          sSql.z, iSize);
10136    }
10137    shell_check_oom(zSql);
10138    freeText(&sQuery);
10139    freeText(&sSql);
10140    if( bDebug ){
10141      utf8_printf(p->out, "%s\n", zSql);
10142    }else{
10143      shell_exec(p, zSql, 0);
10144    }
10145    sqlite3_free(zSql);
10146  }else
10147
10148#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10149  if( c=='s'
10150   && (cli_strncmp(azArg[0], "shell", n)==0
10151       || cli_strncmp(azArg[0],"system",n)==0)
10152  ){
10153    char *zCmd;
10154    int i, x;
10155    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10156    if( nArg<2 ){
10157      raw_printf(stderr, "Usage: .system COMMAND\n");
10158      rc = 1;
10159      goto meta_command_exit;
10160    }
10161    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10162    for(i=2; i<nArg && zCmd!=0; i++){
10163      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10164                             zCmd, azArg[i]);
10165    }
10166    x = zCmd!=0 ? system(zCmd) : 1;
10167    sqlite3_free(zCmd);
10168    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10169  }else
10170#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10171
10172  if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
10173    static const char *azBool[] = { "off", "on", "trigger", "full"};
10174    const char *zOut;
10175    int i;
10176    if( nArg!=1 ){
10177      raw_printf(stderr, "Usage: .show\n");
10178      rc = 1;
10179      goto meta_command_exit;
10180    }
10181    utf8_printf(p->out, "%12.12s: %s\n","echo",
10182                azBool[ShellHasFlag(p, SHFLG_Echo)]);
10183    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10184    utf8_printf(p->out, "%12.12s: %s\n","explain",
10185         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10186    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10187    if( p->mode==MODE_Column
10188     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10189    ){
10190      utf8_printf
10191        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10192         modeDescr[p->mode], p->cmOpts.iWrap,
10193         p->cmOpts.bWordWrap ? "on" : "off",
10194         p->cmOpts.bQuote ? "" : "no");
10195    }else{
10196      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10197    }
10198    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10199      output_c_string(p->out, p->nullValue);
10200      raw_printf(p->out, "\n");
10201    utf8_printf(p->out,"%12.12s: %s\n","output",
10202            strlen30(p->outfile) ? p->outfile : "stdout");
10203    utf8_printf(p->out,"%12.12s: ", "colseparator");
10204      output_c_string(p->out, p->colSeparator);
10205      raw_printf(p->out, "\n");
10206    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10207      output_c_string(p->out, p->rowSeparator);
10208      raw_printf(p->out, "\n");
10209    switch( p->statsOn ){
10210      case 0:  zOut = "off";     break;
10211      default: zOut = "on";      break;
10212      case 2:  zOut = "stmt";    break;
10213      case 3:  zOut = "vmstep";  break;
10214    }
10215    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10216    utf8_printf(p->out, "%12.12s: ", "width");
10217    for (i=0;i<p->nWidth;i++) {
10218      raw_printf(p->out, "%d ", p->colWidth[i]);
10219    }
10220    raw_printf(p->out, "\n");
10221    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10222                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10223  }else
10224
10225  if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
10226    if( nArg==2 ){
10227      if( cli_strcmp(azArg[1],"stmt")==0 ){
10228        p->statsOn = 2;
10229      }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
10230        p->statsOn = 3;
10231      }else{
10232        p->statsOn = (u8)booleanValue(azArg[1]);
10233      }
10234    }else if( nArg==1 ){
10235      display_stats(p->db, p, 0);
10236    }else{
10237      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10238      rc = 1;
10239    }
10240  }else
10241
10242  if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
10243   || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
10244                 || cli_strncmp(azArg[0], "indexes", n)==0) )
10245  ){
10246    sqlite3_stmt *pStmt;
10247    char **azResult;
10248    int nRow, nAlloc;
10249    int ii;
10250    ShellText s;
10251    initText(&s);
10252    open_db(p, 0);
10253    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10254    if( rc ){
10255      sqlite3_finalize(pStmt);
10256      return shellDatabaseError(p->db);
10257    }
10258
10259    if( nArg>2 && c=='i' ){
10260      /* It is an historical accident that the .indexes command shows an error
10261      ** when called with the wrong number of arguments whereas the .tables
10262      ** command does not. */
10263      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10264      rc = 1;
10265      sqlite3_finalize(pStmt);
10266      goto meta_command_exit;
10267    }
10268    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10269      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10270      if( zDbName==0 ) continue;
10271      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10272      if( sqlite3_stricmp(zDbName, "main")==0 ){
10273        appendText(&s, "SELECT name FROM ", 0);
10274      }else{
10275        appendText(&s, "SELECT ", 0);
10276        appendText(&s, zDbName, '\'');
10277        appendText(&s, "||'.'||name FROM ", 0);
10278      }
10279      appendText(&s, zDbName, '"');
10280      appendText(&s, ".sqlite_schema ", 0);
10281      if( c=='t' ){
10282        appendText(&s," WHERE type IN ('table','view')"
10283                      "   AND name NOT LIKE 'sqlite_%'"
10284                      "   AND name LIKE ?1", 0);
10285      }else{
10286        appendText(&s," WHERE type='index'"
10287                      "   AND tbl_name LIKE ?1", 0);
10288      }
10289    }
10290    rc = sqlite3_finalize(pStmt);
10291    if( rc==SQLITE_OK ){
10292      appendText(&s, " ORDER BY 1", 0);
10293      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10294    }
10295    freeText(&s);
10296    if( rc ) return shellDatabaseError(p->db);
10297
10298    /* Run the SQL statement prepared by the above block. Store the results
10299    ** as an array of nul-terminated strings in azResult[].  */
10300    nRow = nAlloc = 0;
10301    azResult = 0;
10302    if( nArg>1 ){
10303      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10304    }else{
10305      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10306    }
10307    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10308      if( nRow>=nAlloc ){
10309        char **azNew;
10310        int n2 = nAlloc*2 + 10;
10311        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10312        shell_check_oom(azNew);
10313        nAlloc = n2;
10314        azResult = azNew;
10315      }
10316      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10317      shell_check_oom(azResult[nRow]);
10318      nRow++;
10319    }
10320    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10321      rc = shellDatabaseError(p->db);
10322    }
10323
10324    /* Pretty-print the contents of array azResult[] to the output */
10325    if( rc==0 && nRow>0 ){
10326      int len, maxlen = 0;
10327      int i, j;
10328      int nPrintCol, nPrintRow;
10329      for(i=0; i<nRow; i++){
10330        len = strlen30(azResult[i]);
10331        if( len>maxlen ) maxlen = len;
10332      }
10333      nPrintCol = 80/(maxlen+2);
10334      if( nPrintCol<1 ) nPrintCol = 1;
10335      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10336      for(i=0; i<nPrintRow; i++){
10337        for(j=i; j<nRow; j+=nPrintRow){
10338          char *zSp = j<nPrintRow ? "" : "  ";
10339          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10340                      azResult[j] ? azResult[j]:"");
10341        }
10342        raw_printf(p->out, "\n");
10343      }
10344    }
10345
10346    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10347    sqlite3_free(azResult);
10348  }else
10349
10350#ifndef SQLITE_SHELL_FIDDLE
10351  /* Begin redirecting output to the file "testcase-out.txt" */
10352  if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
10353    output_reset(p);
10354    p->out = output_file_open("testcase-out.txt", 0);
10355    if( p->out==0 ){
10356      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10357    }
10358    if( nArg>=2 ){
10359      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10360    }else{
10361      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10362    }
10363  }else
10364#endif /* !defined(SQLITE_SHELL_FIDDLE) */
10365
10366#ifndef SQLITE_UNTESTABLE
10367  if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
10368    static const struct {
10369       const char *zCtrlName;   /* Name of a test-control option */
10370       int ctrlCode;            /* Integer code for that option */
10371       int unSafe;              /* Not valid for --safe mode */
10372       const char *zUsage;      /* Usage notes */
10373    } aCtrl[] = {
10374      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10375      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10376    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10377    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10378      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10379      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10380    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10381      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10382      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10383      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10384      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10385      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10386#ifdef YYCOVERAGE
10387      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10388#endif
10389      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10390      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10391      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10392      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10393      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10394      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10395      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10396    };
10397    int testctrl = -1;
10398    int iCtrl = -1;
10399    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10400    int isOk = 0;
10401    int i, n2;
10402    const char *zCmd = 0;
10403
10404    open_db(p, 0);
10405    zCmd = nArg>=2 ? azArg[1] : "help";
10406
10407    /* The argument can optionally begin with "-" or "--" */
10408    if( zCmd[0]=='-' && zCmd[1] ){
10409      zCmd++;
10410      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10411    }
10412
10413    /* --help lists all test-controls */
10414    if( cli_strcmp(zCmd,"help")==0 ){
10415      utf8_printf(p->out, "Available test-controls:\n");
10416      for(i=0; i<ArraySize(aCtrl); i++){
10417        utf8_printf(p->out, "  .testctrl %s %s\n",
10418                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10419      }
10420      rc = 1;
10421      goto meta_command_exit;
10422    }
10423
10424    /* convert testctrl text option to value. allow any unique prefix
10425    ** of the option name, or a numerical value. */
10426    n2 = strlen30(zCmd);
10427    for(i=0; i<ArraySize(aCtrl); i++){
10428      if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10429        if( testctrl<0 ){
10430          testctrl = aCtrl[i].ctrlCode;
10431          iCtrl = i;
10432        }else{
10433          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10434                              "Use \".testctrl --help\" for help\n", zCmd);
10435          rc = 1;
10436          goto meta_command_exit;
10437        }
10438      }
10439    }
10440    if( testctrl<0 ){
10441      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10442                         "Use \".testctrl --help\" for help\n", zCmd);
10443    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
10444      utf8_printf(stderr,
10445         "line %d: \".testctrl %s\" may not be used in safe mode\n",
10446         p->lineno, aCtrl[iCtrl].zCtrlName);
10447      exit(1);
10448    }else{
10449      switch(testctrl){
10450
10451        /* sqlite3_test_control(int, db, int) */
10452        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10453          if( nArg==3 ){
10454            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10455            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10456            isOk = 3;
10457          }
10458          break;
10459
10460        /* sqlite3_test_control(int) */
10461        case SQLITE_TESTCTRL_PRNG_SAVE:
10462        case SQLITE_TESTCTRL_PRNG_RESTORE:
10463        case SQLITE_TESTCTRL_BYTEORDER:
10464          if( nArg==2 ){
10465            rc2 = sqlite3_test_control(testctrl);
10466            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10467          }
10468          break;
10469
10470        /* sqlite3_test_control(int, uint) */
10471        case SQLITE_TESTCTRL_PENDING_BYTE:
10472          if( nArg==3 ){
10473            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10474            rc2 = sqlite3_test_control(testctrl, opt);
10475            isOk = 3;
10476          }
10477          break;
10478
10479        /* sqlite3_test_control(int, int, sqlite3*) */
10480        case SQLITE_TESTCTRL_PRNG_SEED:
10481          if( nArg==3 || nArg==4 ){
10482            int ii = (int)integerValue(azArg[2]);
10483            sqlite3 *db;
10484            if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
10485              sqlite3_randomness(sizeof(ii),&ii);
10486              printf("-- random seed: %d\n", ii);
10487            }
10488            if( nArg==3 ){
10489              db = 0;
10490            }else{
10491              db = p->db;
10492              /* Make sure the schema has been loaded */
10493              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10494            }
10495            rc2 = sqlite3_test_control(testctrl, ii, db);
10496            isOk = 3;
10497          }
10498          break;
10499
10500        /* sqlite3_test_control(int, int) */
10501        case SQLITE_TESTCTRL_ASSERT:
10502        case SQLITE_TESTCTRL_ALWAYS:
10503          if( nArg==3 ){
10504            int opt = booleanValue(azArg[2]);
10505            rc2 = sqlite3_test_control(testctrl, opt);
10506            isOk = 1;
10507          }
10508          break;
10509
10510        /* sqlite3_test_control(int, int) */
10511        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10512        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10513          if( nArg==3 ){
10514            int opt = booleanValue(azArg[2]);
10515            rc2 = sqlite3_test_control(testctrl, opt);
10516            isOk = 3;
10517          }
10518          break;
10519
10520        /* sqlite3_test_control(sqlite3*) */
10521        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10522          rc2 = sqlite3_test_control(testctrl, p->db);
10523          isOk = 3;
10524          break;
10525
10526        case SQLITE_TESTCTRL_IMPOSTER:
10527          if( nArg==5 ){
10528            rc2 = sqlite3_test_control(testctrl, p->db,
10529                          azArg[2],
10530                          integerValue(azArg[3]),
10531                          integerValue(azArg[4]));
10532            isOk = 3;
10533          }
10534          break;
10535
10536        case SQLITE_TESTCTRL_SEEK_COUNT: {
10537          u64 x = 0;
10538          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10539          utf8_printf(p->out, "%llu\n", x);
10540          isOk = 3;
10541          break;
10542        }
10543
10544#ifdef YYCOVERAGE
10545        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10546          if( nArg==2 ){
10547            sqlite3_test_control(testctrl, p->out);
10548            isOk = 3;
10549          }
10550          break;
10551        }
10552#endif
10553#ifdef SQLITE_DEBUG
10554        case SQLITE_TESTCTRL_TUNE: {
10555          if( nArg==4 ){
10556            int id = (int)integerValue(azArg[2]);
10557            int val = (int)integerValue(azArg[3]);
10558            sqlite3_test_control(testctrl, id, &val);
10559            isOk = 3;
10560          }else if( nArg==3 ){
10561            int id = (int)integerValue(azArg[2]);
10562            sqlite3_test_control(testctrl, -id, &rc2);
10563            isOk = 1;
10564          }else if( nArg==2 ){
10565            int id = 1;
10566            while(1){
10567              int val = 0;
10568              rc2 = sqlite3_test_control(testctrl, -id, &val);
10569              if( rc2!=SQLITE_OK ) break;
10570              if( id>1 ) utf8_printf(p->out, "  ");
10571              utf8_printf(p->out, "%d: %d", id, val);
10572              id++;
10573            }
10574            if( id>1 ) utf8_printf(p->out, "\n");
10575            isOk = 3;
10576          }
10577          break;
10578        }
10579#endif
10580        case SQLITE_TESTCTRL_SORTER_MMAP:
10581          if( nArg==3 ){
10582            int opt = (unsigned int)integerValue(azArg[2]);
10583            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10584            isOk = 3;
10585          }
10586          break;
10587      }
10588    }
10589    if( isOk==0 && iCtrl>=0 ){
10590      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10591      rc = 1;
10592    }else if( isOk==1 ){
10593      raw_printf(p->out, "%d\n", rc2);
10594    }else if( isOk==2 ){
10595      raw_printf(p->out, "0x%08x\n", rc2);
10596    }
10597  }else
10598#endif /* !defined(SQLITE_UNTESTABLE) */
10599
10600  if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
10601    open_db(p, 0);
10602    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10603  }else
10604
10605  if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
10606    if( nArg==2 ){
10607      enableTimer = booleanValue(azArg[1]);
10608      if( enableTimer && !HAS_TIMER ){
10609        raw_printf(stderr, "Error: timer not available on this system.\n");
10610        enableTimer = 0;
10611      }
10612    }else{
10613      raw_printf(stderr, "Usage: .timer on|off\n");
10614      rc = 1;
10615    }
10616  }else
10617
10618#ifndef SQLITE_OMIT_TRACE
10619  if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
10620    int mType = 0;
10621    int jj;
10622    open_db(p, 0);
10623    for(jj=1; jj<nArg; jj++){
10624      const char *z = azArg[jj];
10625      if( z[0]=='-' ){
10626        if( optionMatch(z, "expanded") ){
10627          p->eTraceType = SHELL_TRACE_EXPANDED;
10628        }
10629#ifdef SQLITE_ENABLE_NORMALIZE
10630        else if( optionMatch(z, "normalized") ){
10631          p->eTraceType = SHELL_TRACE_NORMALIZED;
10632        }
10633#endif
10634        else if( optionMatch(z, "plain") ){
10635          p->eTraceType = SHELL_TRACE_PLAIN;
10636        }
10637        else if( optionMatch(z, "profile") ){
10638          mType |= SQLITE_TRACE_PROFILE;
10639        }
10640        else if( optionMatch(z, "row") ){
10641          mType |= SQLITE_TRACE_ROW;
10642        }
10643        else if( optionMatch(z, "stmt") ){
10644          mType |= SQLITE_TRACE_STMT;
10645        }
10646        else if( optionMatch(z, "close") ){
10647          mType |= SQLITE_TRACE_CLOSE;
10648        }
10649        else {
10650          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10651          rc = 1;
10652          goto meta_command_exit;
10653        }
10654      }else{
10655        output_file_close(p->traceOut);
10656        p->traceOut = output_file_open(azArg[1], 0);
10657      }
10658    }
10659    if( p->traceOut==0 ){
10660      sqlite3_trace_v2(p->db, 0, 0, 0);
10661    }else{
10662      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10663      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10664    }
10665  }else
10666#endif /* !defined(SQLITE_OMIT_TRACE) */
10667
10668#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10669  if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
10670    int ii;
10671    int lenOpt;
10672    char *zOpt;
10673    if( nArg<2 ){
10674      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10675      rc = 1;
10676      goto meta_command_exit;
10677    }
10678    open_db(p, 0);
10679    zOpt = azArg[1];
10680    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10681    lenOpt = (int)strlen(zOpt);
10682    if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10683      assert( azArg[nArg]==0 );
10684      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10685    }else{
10686      for(ii=1; ii<nArg; ii++){
10687        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10688      }
10689    }
10690  }else
10691#endif
10692
10693#if SQLITE_USER_AUTHENTICATION
10694  if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
10695    if( nArg<2 ){
10696      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10697      rc = 1;
10698      goto meta_command_exit;
10699    }
10700    open_db(p, 0);
10701    if( cli_strcmp(azArg[1],"login")==0 ){
10702      if( nArg!=4 ){
10703        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10704        rc = 1;
10705        goto meta_command_exit;
10706      }
10707      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10708                                     strlen30(azArg[3]));
10709      if( rc ){
10710        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10711        rc = 1;
10712      }
10713    }else if( cli_strcmp(azArg[1],"add")==0 ){
10714      if( nArg!=5 ){
10715        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10716        rc = 1;
10717        goto meta_command_exit;
10718      }
10719      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10720                            booleanValue(azArg[4]));
10721      if( rc ){
10722        raw_printf(stderr, "User-Add failed: %d\n", rc);
10723        rc = 1;
10724      }
10725    }else if( cli_strcmp(azArg[1],"edit")==0 ){
10726      if( nArg!=5 ){
10727        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10728        rc = 1;
10729        goto meta_command_exit;
10730      }
10731      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10732                              booleanValue(azArg[4]));
10733      if( rc ){
10734        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10735        rc = 1;
10736      }
10737    }else if( cli_strcmp(azArg[1],"delete")==0 ){
10738      if( nArg!=3 ){
10739        raw_printf(stderr, "Usage: .user delete USER\n");
10740        rc = 1;
10741        goto meta_command_exit;
10742      }
10743      rc = sqlite3_user_delete(p->db, azArg[2]);
10744      if( rc ){
10745        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10746        rc = 1;
10747      }
10748    }else{
10749      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10750      rc = 1;
10751      goto meta_command_exit;
10752    }
10753  }else
10754#endif /* SQLITE_USER_AUTHENTICATION */
10755
10756  if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
10757    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10758        sqlite3_libversion(), sqlite3_sourceid());
10759#if SQLITE_HAVE_ZLIB
10760    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10761#endif
10762#define CTIMEOPT_VAL_(opt) #opt
10763#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10764#if defined(__clang__) && defined(__clang_major__)
10765    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10766                    CTIMEOPT_VAL(__clang_minor__) "."
10767                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10768#elif defined(_MSC_VER)
10769    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10770#elif defined(__GNUC__) && defined(__VERSION__)
10771    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10772#endif
10773  }else
10774
10775  if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
10776    const char *zDbName = nArg==2 ? azArg[1] : "main";
10777    sqlite3_vfs *pVfs = 0;
10778    if( p->db ){
10779      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10780      if( pVfs ){
10781        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10782        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10783        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10784        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10785      }
10786    }
10787  }else
10788
10789  if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
10790    sqlite3_vfs *pVfs;
10791    sqlite3_vfs *pCurrent = 0;
10792    if( p->db ){
10793      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10794    }
10795    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10796      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10797           pVfs==pCurrent ? "  <--- CURRENT" : "");
10798      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10799      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10800      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10801      if( pVfs->pNext ){
10802        raw_printf(p->out, "-----------------------------------\n");
10803      }
10804    }
10805  }else
10806
10807  if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
10808    const char *zDbName = nArg==2 ? azArg[1] : "main";
10809    char *zVfsName = 0;
10810    if( p->db ){
10811      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10812      if( zVfsName ){
10813        utf8_printf(p->out, "%s\n", zVfsName);
10814        sqlite3_free(zVfsName);
10815      }
10816    }
10817  }else
10818
10819  if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
10820    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10821    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10822  }else
10823
10824  if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
10825    int j;
10826    assert( nArg<=ArraySize(azArg) );
10827    p->nWidth = nArg-1;
10828    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
10829    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10830    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10831    for(j=1; j<nArg; j++){
10832      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10833    }
10834  }else
10835
10836  {
10837    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10838      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10839    rc = 1;
10840  }
10841
10842meta_command_exit:
10843  if( p->outCount ){
10844    p->outCount--;
10845    if( p->outCount==0 ) output_reset(p);
10846  }
10847  p->bSafeMode = p->bSafeModePersist;
10848  return rc;
10849}
10850
10851/* Line scan result and intermediate states (supporting scan resumption)
10852*/
10853#ifndef CHAR_BIT
10854# define CHAR_BIT 8
10855#endif
10856typedef enum {
10857  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
10858  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
10859  QSS_Start = 0
10860} QuickScanState;
10861#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
10862#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
10863#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
10864#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
10865#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
10866
10867/*
10868** Scan line for classification to guide shell's handling.
10869** The scan is resumable for subsequent lines when prior
10870** return values are passed as the 2nd argument.
10871*/
10872static QuickScanState quickscan(char *zLine, QuickScanState qss){
10873  char cin;
10874  char cWait = (char)qss; /* intentional narrowing loss */
10875  if( cWait==0 ){
10876  PlainScan:
10877    assert( cWait==0 );
10878    while( (cin = *zLine++)!=0 ){
10879      if( IsSpace(cin) )
10880        continue;
10881      switch (cin){
10882      case '-':
10883        if( *zLine!='-' )
10884          break;
10885        while((cin = *++zLine)!=0 )
10886          if( cin=='\n')
10887            goto PlainScan;
10888        return qss;
10889      case ';':
10890        qss |= QSS_EndingSemi;
10891        continue;
10892      case '/':
10893        if( *zLine=='*' ){
10894          ++zLine;
10895          cWait = '*';
10896          qss = QSS_SETV(qss, cWait);
10897          goto TermScan;
10898        }
10899        break;
10900      case '[':
10901        cin = ']';
10902        /* fall thru */
10903      case '`': case '\'': case '"':
10904        cWait = cin;
10905        qss = QSS_HasDark | cWait;
10906        goto TermScan;
10907      default:
10908        break;
10909      }
10910      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
10911    }
10912  }else{
10913  TermScan:
10914    while( (cin = *zLine++)!=0 ){
10915      if( cin==cWait ){
10916        switch( cWait ){
10917        case '*':
10918          if( *zLine != '/' )
10919            continue;
10920          ++zLine;
10921          cWait = 0;
10922          qss = QSS_SETV(qss, 0);
10923          goto PlainScan;
10924        case '`': case '\'': case '"':
10925          if(*zLine==cWait){
10926            ++zLine;
10927            continue;
10928          }
10929          /* fall thru */
10930        case ']':
10931          cWait = 0;
10932          qss = QSS_SETV(qss, 0);
10933          goto PlainScan;
10934        default: assert(0);
10935        }
10936      }
10937    }
10938  }
10939  return qss;
10940}
10941
10942/*
10943** Return TRUE if the line typed in is an SQL command terminator other
10944** than a semi-colon.  The SQL Server style "go" command is understood
10945** as is the Oracle "/".
10946*/
10947static int line_is_command_terminator(char *zLine){
10948  while( IsSpace(zLine[0]) ){ zLine++; };
10949  if( zLine[0]=='/' )
10950    zLine += 1; /* Oracle */
10951  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
10952    zLine += 2; /* SQL Server */
10953  else
10954    return 0;
10955  return quickscan(zLine, QSS_Start)==QSS_Start;
10956}
10957
10958/*
10959** We need a default sqlite3_complete() implementation to use in case
10960** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10961** any arbitrary text is a complete SQL statement.  This is not very
10962** user-friendly, but it does seem to work.
10963*/
10964#ifdef SQLITE_OMIT_COMPLETE
10965#define sqlite3_complete(x) 1
10966#endif
10967
10968/*
10969** Return true if zSql is a complete SQL statement.  Return false if it
10970** ends in the middle of a string literal or C-style comment.
10971*/
10972static int line_is_complete(char *zSql, int nSql){
10973  int rc;
10974  if( zSql==0 ) return 1;
10975  zSql[nSql] = ';';
10976  zSql[nSql+1] = 0;
10977  rc = sqlite3_complete(zSql);
10978  zSql[nSql] = 0;
10979  return rc;
10980}
10981
10982/*
10983** Run a single line of SQL.  Return the number of errors.
10984*/
10985static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10986  int rc;
10987  char *zErrMsg = 0;
10988
10989  open_db(p, 0);
10990  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10991  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10992  BEGIN_TIMER;
10993  rc = shell_exec(p, zSql, &zErrMsg);
10994  END_TIMER;
10995  if( rc || zErrMsg ){
10996    char zPrefix[100];
10997    const char *zErrorTail;
10998    const char *zErrorType;
10999    if( zErrMsg==0 ){
11000      zErrorType = "Error";
11001      zErrorTail = sqlite3_errmsg(p->db);
11002    }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
11003      zErrorType = "Parse error";
11004      zErrorTail = &zErrMsg[12];
11005    }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
11006      zErrorType = "Runtime error";
11007      zErrorTail = &zErrMsg[10];
11008    }else{
11009      zErrorType = "Error";
11010      zErrorTail = zErrMsg;
11011    }
11012    if( in!=0 || !stdin_is_interactive ){
11013      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11014                       "%s near line %d:", zErrorType, startline);
11015    }else{
11016      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11017    }
11018    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11019    sqlite3_free(zErrMsg);
11020    zErrMsg = 0;
11021    return 1;
11022  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11023    char zLineBuf[2000];
11024    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11025            "changes: %lld   total_changes: %lld",
11026            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11027    raw_printf(p->out, "%s\n", zLineBuf);
11028  }
11029  return 0;
11030}
11031
11032static void echo_group_input(ShellState *p, const char *zDo){
11033  if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11034}
11035
11036#ifdef SQLITE_SHELL_FIDDLE
11037/*
11038** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
11039** because we need the global shellState and cannot access it from that function
11040** without moving lots of code around (creating a larger/messier diff).
11041*/
11042static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11043  /* Parse the next line from shellState.wasm.zInput. */
11044  const char *zBegin = shellState.wasm.zPos;
11045  const char *z = zBegin;
11046  char *zLine = 0;
11047  i64 nZ = 0;
11048
11049  UNUSED_PARAMETER(in);
11050  UNUSED_PARAMETER(isContinuation);
11051  if(!z || !*z){
11052    return 0;
11053  }
11054  while(*z && isspace(*z)) ++z;
11055  zBegin = z;
11056  for(; *z && '\n'!=*z; ++nZ, ++z){}
11057  if(nZ>0 && '\r'==zBegin[nZ-1]){
11058    --nZ;
11059  }
11060  shellState.wasm.zPos = z;
11061  zLine = realloc(zPrior, nZ+1);
11062  shell_check_oom(zLine);
11063  memcpy(zLine, zBegin, nZ);
11064  zLine[nZ] = 0;
11065  return zLine;
11066}
11067#endif /* SQLITE_SHELL_FIDDLE */
11068
11069/*
11070** Read input from *in and process it.  If *in==0 then input
11071** is interactive - the user is typing it it.  Otherwise, input
11072** is coming from a file or device.  A prompt is issued and history
11073** is saved only if input is interactive.  An interrupt signal will
11074** cause this routine to exit immediately, unless input is interactive.
11075**
11076** Return the number of errors.
11077*/
11078static int process_input(ShellState *p){
11079  char *zLine = 0;          /* A single input line */
11080  char *zSql = 0;           /* Accumulated SQL text */
11081  i64 nLine;                /* Length of current line */
11082  i64 nSql = 0;             /* Bytes of zSql[] used */
11083  i64 nAlloc = 0;           /* Allocated zSql[] space */
11084  int rc;                   /* Error code */
11085  int errCnt = 0;           /* Number of errors seen */
11086  i64 startline = 0;        /* Line number for start of current input */
11087  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11088
11089  if( p->inputNesting==MAX_INPUT_NESTING ){
11090    /* This will be more informative in a later version. */
11091    utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11092                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11093    return 1;
11094  }
11095  ++p->inputNesting;
11096  p->lineno = 0;
11097  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11098    fflush(p->out);
11099    zLine = one_input_line(p->in, zLine, nSql>0);
11100    if( zLine==0 ){
11101      /* End of input */
11102      if( p->in==0 && stdin_is_interactive ) printf("\n");
11103      break;
11104    }
11105    if( seenInterrupt ){
11106      if( p->in!=0 ) break;
11107      seenInterrupt = 0;
11108    }
11109    p->lineno++;
11110    if( QSS_INPLAIN(qss)
11111        && line_is_command_terminator(zLine)
11112        && line_is_complete(zSql, nSql) ){
11113      memcpy(zLine,";",2);
11114    }
11115    qss = quickscan(zLine, qss);
11116    if( QSS_PLAINWHITE(qss) && nSql==0 ){
11117      /* Just swallow single-line whitespace */
11118      echo_group_input(p, zLine);
11119      qss = QSS_Start;
11120      continue;
11121    }
11122    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11123      echo_group_input(p, zLine);
11124      if( zLine[0]=='.' ){
11125        rc = do_meta_command(zLine, p);
11126        if( rc==2 ){ /* exit requested */
11127          break;
11128        }else if( rc ){
11129          errCnt++;
11130        }
11131      }
11132      qss = QSS_Start;
11133      continue;
11134    }
11135    /* No single-line dispositions remain; accumulate line(s). */
11136    nLine = strlen(zLine);
11137    if( nSql+nLine+2>=nAlloc ){
11138      /* Grow buffer by half-again increments when big. */
11139      nAlloc = nSql+(nSql>>1)+nLine+100;
11140      zSql = realloc(zSql, nAlloc);
11141      shell_check_oom(zSql);
11142    }
11143    if( nSql==0 ){
11144      i64 i;
11145      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11146      assert( nAlloc>0 && zSql!=0 );
11147      memcpy(zSql, zLine+i, nLine+1-i);
11148      startline = p->lineno;
11149      nSql = nLine-i;
11150    }else{
11151      zSql[nSql++] = '\n';
11152      memcpy(zSql+nSql, zLine, nLine+1);
11153      nSql += nLine;
11154    }
11155    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11156      echo_group_input(p, zSql);
11157      errCnt += runOneSqlLine(p, zSql, p->in, startline);
11158      nSql = 0;
11159      if( p->outCount ){
11160        output_reset(p);
11161        p->outCount = 0;
11162      }else{
11163        clearTempFile(p);
11164      }
11165      p->bSafeMode = p->bSafeModePersist;
11166      qss = QSS_Start;
11167    }else if( nSql && QSS_PLAINWHITE(qss) ){
11168      echo_group_input(p, zSql);
11169      nSql = 0;
11170      qss = QSS_Start;
11171    }
11172  }
11173  if( nSql ){
11174    /* This may be incomplete. Let the SQL parser deal with that. */
11175    echo_group_input(p, zSql);
11176    errCnt += runOneSqlLine(p, zSql, p->in, startline);
11177  }
11178  free(zSql);
11179  free(zLine);
11180  --p->inputNesting;
11181  return errCnt>0;
11182}
11183
11184/*
11185** Return a pathname which is the user's home directory.  A
11186** 0 return indicates an error of some kind.
11187*/
11188static char *find_home_dir(int clearFlag){
11189  static char *home_dir = NULL;
11190  if( clearFlag ){
11191    free(home_dir);
11192    home_dir = 0;
11193    return 0;
11194  }
11195  if( home_dir ) return home_dir;
11196
11197#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11198     && !defined(__RTP__) && !defined(_WRS_KERNEL)
11199  {
11200    struct passwd *pwent;
11201    uid_t uid = getuid();
11202    if( (pwent=getpwuid(uid)) != NULL) {
11203      home_dir = pwent->pw_dir;
11204    }
11205  }
11206#endif
11207
11208#if defined(_WIN32_WCE)
11209  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11210   */
11211  home_dir = "/";
11212#else
11213
11214#if defined(_WIN32) || defined(WIN32)
11215  if (!home_dir) {
11216    home_dir = getenv("USERPROFILE");
11217  }
11218#endif
11219
11220  if (!home_dir) {
11221    home_dir = getenv("HOME");
11222  }
11223
11224#if defined(_WIN32) || defined(WIN32)
11225  if (!home_dir) {
11226    char *zDrive, *zPath;
11227    int n;
11228    zDrive = getenv("HOMEDRIVE");
11229    zPath = getenv("HOMEPATH");
11230    if( zDrive && zPath ){
11231      n = strlen30(zDrive) + strlen30(zPath) + 1;
11232      home_dir = malloc( n );
11233      if( home_dir==0 ) return 0;
11234      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11235      return home_dir;
11236    }
11237    home_dir = "c:\\";
11238  }
11239#endif
11240
11241#endif /* !_WIN32_WCE */
11242
11243  if( home_dir ){
11244    i64 n = strlen(home_dir) + 1;
11245    char *z = malloc( n );
11246    if( z ) memcpy(z, home_dir, n);
11247    home_dir = z;
11248  }
11249
11250  return home_dir;
11251}
11252
11253/*
11254** Read input from the file given by sqliterc_override.  Or if that
11255** parameter is NULL, take input from ~/.sqliterc
11256**
11257** Returns the number of errors.
11258*/
11259static void process_sqliterc(
11260  ShellState *p,                  /* Configuration data */
11261  const char *sqliterc_override   /* Name of config file. NULL to use default */
11262){
11263  char *home_dir = NULL;
11264  const char *sqliterc = sqliterc_override;
11265  char *zBuf = 0;
11266  FILE *inSaved = p->in;
11267  int savedLineno = p->lineno;
11268
11269  if (sqliterc == NULL) {
11270    home_dir = find_home_dir(0);
11271    if( home_dir==0 ){
11272      raw_printf(stderr, "-- warning: cannot find home directory;"
11273                      " cannot read ~/.sqliterc\n");
11274      return;
11275    }
11276    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11277    shell_check_oom(zBuf);
11278    sqliterc = zBuf;
11279  }
11280  p->in = fopen(sqliterc,"rb");
11281  if( p->in ){
11282    if( stdin_is_interactive ){
11283      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11284    }
11285    if( process_input(p) && bail_on_error ) exit(1);
11286    fclose(p->in);
11287  }else if( sqliterc_override!=0 ){
11288    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11289    if( bail_on_error ) exit(1);
11290  }
11291  p->in = inSaved;
11292  p->lineno = savedLineno;
11293  sqlite3_free(zBuf);
11294}
11295
11296/*
11297** Show available command line options
11298*/
11299static const char zOptions[] =
11300#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11301  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11302#endif
11303  "   -append              append the database to the end of the file\n"
11304  "   -ascii               set output mode to 'ascii'\n"
11305  "   -bail                stop after hitting an error\n"
11306  "   -batch               force batch I/O\n"
11307  "   -box                 set output mode to 'box'\n"
11308  "   -column              set output mode to 'column'\n"
11309  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11310  "   -csv                 set output mode to 'csv'\n"
11311#if !defined(SQLITE_OMIT_DESERIALIZE)
11312  "   -deserialize         open the database using sqlite3_deserialize()\n"
11313#endif
11314  "   -echo                print inputs before execution\n"
11315  "   -init FILENAME       read/process named file\n"
11316  "   -[no]header          turn headers on or off\n"
11317#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11318  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11319#endif
11320  "   -help                show this message\n"
11321  "   -html                set output mode to HTML\n"
11322  "   -interactive         force interactive I/O\n"
11323  "   -json                set output mode to 'json'\n"
11324  "   -line                set output mode to 'line'\n"
11325  "   -list                set output mode to 'list'\n"
11326  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11327  "   -markdown            set output mode to 'markdown'\n"
11328#if !defined(SQLITE_OMIT_DESERIALIZE)
11329  "   -maxsize N           maximum size for a --deserialize database\n"
11330#endif
11331  "   -memtrace            trace all memory allocations and deallocations\n"
11332  "   -mmap N              default mmap size set to N\n"
11333#ifdef SQLITE_ENABLE_MULTIPLEX
11334  "   -multiplex           enable the multiplexor VFS\n"
11335#endif
11336  "   -newline SEP         set output row separator. Default: '\\n'\n"
11337  "   -nofollow            refuse to open symbolic links to database files\n"
11338  "   -nonce STRING        set the safe-mode escape nonce\n"
11339  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11340  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11341  "   -quote               set output mode to 'quote'\n"
11342  "   -readonly            open the database read-only\n"
11343  "   -safe                enable safe-mode\n"
11344  "   -separator SEP       set output column separator. Default: '|'\n"
11345#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11346  "   -sorterref SIZE      sorter references threshold size\n"
11347#endif
11348  "   -stats               print memory stats before each finalize\n"
11349  "   -table               set output mode to 'table'\n"
11350  "   -tabs                set output mode to 'tabs'\n"
11351  "   -version             show SQLite version\n"
11352  "   -vfs NAME            use NAME as the default VFS\n"
11353#ifdef SQLITE_ENABLE_VFSTRACE
11354  "   -vfstrace            enable tracing of all VFS calls\n"
11355#endif
11356#ifdef SQLITE_HAVE_ZLIB
11357  "   -zip                 open the file as a ZIP Archive\n"
11358#endif
11359;
11360static void usage(int showDetail){
11361  utf8_printf(stderr,
11362      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11363      "FILENAME is the name of an SQLite database. A new database is created\n"
11364      "if the file does not previously exist.\n", Argv0);
11365  if( showDetail ){
11366    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11367  }else{
11368    raw_printf(stderr, "Use the -help option for additional information\n");
11369  }
11370  exit(1);
11371}
11372
11373/*
11374** Internal check:  Verify that the SQLite is uninitialized.  Print a
11375** error message if it is initialized.
11376*/
11377static void verify_uninitialized(void){
11378  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11379    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11380                        " initialization.\n");
11381  }
11382}
11383
11384/*
11385** Initialize the state information in data
11386*/
11387static void main_init(ShellState *data) {
11388  memset(data, 0, sizeof(*data));
11389  data->normalMode = data->cMode = data->mode = MODE_List;
11390  data->autoExplain = 1;
11391  data->pAuxDb = &data->aAuxDb[0];
11392  memcpy(data->colSeparator,SEP_Column, 2);
11393  memcpy(data->rowSeparator,SEP_Row, 2);
11394  data->showHeader = 0;
11395  data->shellFlgs = SHFLG_Lookaside;
11396  verify_uninitialized();
11397  sqlite3_config(SQLITE_CONFIG_URI, 1);
11398  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11399  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11400  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11401  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11402}
11403
11404/*
11405** Output text to the console in a font that attracts extra attention.
11406*/
11407#ifdef _WIN32
11408static void printBold(const char *zText){
11409#if !SQLITE_OS_WINRT
11410  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11411  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11412  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11413  SetConsoleTextAttribute(out,
11414         FOREGROUND_RED|FOREGROUND_INTENSITY
11415  );
11416#endif
11417  printf("%s", zText);
11418#if !SQLITE_OS_WINRT
11419  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11420#endif
11421}
11422#else
11423static void printBold(const char *zText){
11424  printf("\033[1m%s\033[0m", zText);
11425}
11426#endif
11427
11428/*
11429** Get the argument to an --option.  Throw an error and die if no argument
11430** is available.
11431*/
11432static char *cmdline_option_value(int argc, char **argv, int i){
11433  if( i==argc ){
11434    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
11435            argv[0], argv[argc-1]);
11436    exit(1);
11437  }
11438  return argv[i];
11439}
11440
11441#ifndef SQLITE_SHELL_IS_UTF8
11442#  if (defined(_WIN32) || defined(WIN32)) \
11443   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
11444#    define SQLITE_SHELL_IS_UTF8          (0)
11445#  else
11446#    define SQLITE_SHELL_IS_UTF8          (1)
11447#  endif
11448#endif
11449
11450#ifdef SQLITE_SHELL_FIDDLE
11451#  define main fiddle_main
11452#endif
11453
11454#if SQLITE_SHELL_IS_UTF8
11455int SQLITE_CDECL main(int argc, char **argv){
11456#else
11457int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
11458  char **argv;
11459#endif
11460#ifdef SQLITE_DEBUG
11461  sqlite3_int64 mem_main_enter = sqlite3_memory_used();
11462#endif
11463  char *zErrMsg = 0;
11464#ifdef SQLITE_SHELL_FIDDLE
11465#  define data shellState
11466#else
11467  ShellState data;
11468#endif
11469  const char *zInitFile = 0;
11470  int i;
11471  int rc = 0;
11472  int warnInmemoryDb = 0;
11473  int readStdin = 1;
11474  int nCmd = 0;
11475  char **azCmd = 0;
11476  const char *zVfs = 0;           /* Value of -vfs command-line option */
11477#if !SQLITE_SHELL_IS_UTF8
11478  char **argvToFree = 0;
11479  int argcToFree = 0;
11480#endif
11481
11482  setBinaryMode(stdin, 0);
11483  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
11484#ifdef SQLITE_SHELL_FIDDLE
11485  stdin_is_interactive = 0;
11486  stdout_is_console = 1;
11487  data.wasm.zDefaultDbName = "/fiddle.sqlite3";
11488#else
11489  stdin_is_interactive = isatty(0);
11490  stdout_is_console = isatty(1);
11491#endif
11492
11493#if !defined(_WIN32_WCE)
11494  if( getenv("SQLITE_DEBUG_BREAK") ){
11495    if( isatty(0) && isatty(2) ){
11496      fprintf(stderr,
11497          "attach debugger to process %d and press any key to continue.\n",
11498          GETPID());
11499      fgetc(stdin);
11500    }else{
11501#if defined(_WIN32) || defined(WIN32)
11502#if SQLITE_OS_WINRT
11503      __debugbreak();
11504#else
11505      DebugBreak();
11506#endif
11507#elif defined(SIGTRAP)
11508      raise(SIGTRAP);
11509#endif
11510    }
11511  }
11512#endif
11513
11514#if USE_SYSTEM_SQLITE+0!=1
11515  if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
11516    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
11517            sqlite3_sourceid(), SQLITE_SOURCE_ID);
11518    exit(1);
11519  }
11520#endif
11521  main_init(&data);
11522
11523  /* On Windows, we must translate command-line arguments into UTF-8.
11524  ** The SQLite memory allocator subsystem has to be enabled in order to
11525  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
11526  ** subsequent sqlite3_config() calls will work.  So copy all results into
11527  ** memory that does not come from the SQLite memory allocator.
11528  */
11529#if !SQLITE_SHELL_IS_UTF8
11530  sqlite3_initialize();
11531  argvToFree = malloc(sizeof(argv[0])*argc*2);
11532  shell_check_oom(argvToFree);
11533  argcToFree = argc;
11534  argv = argvToFree + argc;
11535  for(i=0; i<argc; i++){
11536    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
11537    i64 n;
11538    shell_check_oom(z);
11539    n = strlen(z);
11540    argv[i] = malloc( n+1 );
11541    shell_check_oom(argv[i]);
11542    memcpy(argv[i], z, n+1);
11543    argvToFree[i] = argv[i];
11544    sqlite3_free(z);
11545  }
11546  sqlite3_shutdown();
11547#endif
11548
11549  assert( argc>=1 && argv && argv[0] );
11550  Argv0 = argv[0];
11551
11552  /* Make sure we have a valid signal handler early, before anything
11553  ** else is done.
11554  */
11555#ifdef SIGINT
11556  signal(SIGINT, interrupt_handler);
11557#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11558  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
11559#endif
11560
11561#ifdef SQLITE_SHELL_DBNAME_PROC
11562  {
11563    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
11564    ** of a C-function that will provide the name of the database file.  Use
11565    ** this compile-time option to embed this shell program in larger
11566    ** applications. */
11567    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
11568    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
11569    warnInmemoryDb = 0;
11570  }
11571#endif
11572
11573  /* Do an initial pass through the command-line argument to locate
11574  ** the name of the database file, the name of the initialization file,
11575  ** the size of the alternative malloc heap,
11576  ** and the first command to execute.
11577  */
11578  verify_uninitialized();
11579  for(i=1; i<argc; i++){
11580    char *z;
11581    z = argv[i];
11582    if( z[0]!='-' ){
11583      if( data.aAuxDb->zDbFilename==0 ){
11584        data.aAuxDb->zDbFilename = z;
11585      }else{
11586        /* Excesss arguments are interpreted as SQL (or dot-commands) and
11587        ** mean that nothing is read from stdin */
11588        readStdin = 0;
11589        nCmd++;
11590        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
11591        shell_check_oom(azCmd);
11592        azCmd[nCmd-1] = z;
11593      }
11594    }
11595    if( z[1]=='-' ) z++;
11596    if( cli_strcmp(z,"-separator")==0
11597     || cli_strcmp(z,"-nullvalue")==0
11598     || cli_strcmp(z,"-newline")==0
11599     || cli_strcmp(z,"-cmd")==0
11600    ){
11601      (void)cmdline_option_value(argc, argv, ++i);
11602    }else if( cli_strcmp(z,"-init")==0 ){
11603      zInitFile = cmdline_option_value(argc, argv, ++i);
11604    }else if( cli_strcmp(z,"-batch")==0 ){
11605      /* Need to check for batch mode here to so we can avoid printing
11606      ** informational messages (like from process_sqliterc) before
11607      ** we do the actual processing of arguments later in a second pass.
11608      */
11609      stdin_is_interactive = 0;
11610    }else if( cli_strcmp(z,"-heap")==0 ){
11611#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11612      const char *zSize;
11613      sqlite3_int64 szHeap;
11614
11615      zSize = cmdline_option_value(argc, argv, ++i);
11616      szHeap = integerValue(zSize);
11617      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11618      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11619#else
11620      (void)cmdline_option_value(argc, argv, ++i);
11621#endif
11622    }else if( cli_strcmp(z,"-pagecache")==0 ){
11623      sqlite3_int64 n, sz;
11624      sz = integerValue(cmdline_option_value(argc,argv,++i));
11625      if( sz>70000 ) sz = 70000;
11626      if( sz<0 ) sz = 0;
11627      n = integerValue(cmdline_option_value(argc,argv,++i));
11628      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11629        n = 0xffffffffffffLL/sz;
11630      }
11631      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11632                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11633      data.shellFlgs |= SHFLG_Pagecache;
11634    }else if( cli_strcmp(z,"-lookaside")==0 ){
11635      int n, sz;
11636      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11637      if( sz<0 ) sz = 0;
11638      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11639      if( n<0 ) n = 0;
11640      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11641      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11642    }else if( cli_strcmp(z,"-threadsafe")==0 ){
11643      int n;
11644      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11645      switch( n ){
11646         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
11647         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
11648         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
11649      }
11650#ifdef SQLITE_ENABLE_VFSTRACE
11651    }else if( cli_strcmp(z,"-vfstrace")==0 ){
11652      extern int vfstrace_register(
11653         const char *zTraceName,
11654         const char *zOldVfsName,
11655         int (*xOut)(const char*,void*),
11656         void *pOutArg,
11657         int makeDefault
11658      );
11659      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11660#endif
11661#ifdef SQLITE_ENABLE_MULTIPLEX
11662    }else if( cli_strcmp(z,"-multiplex")==0 ){
11663      extern int sqlite3_multiple_initialize(const char*,int);
11664      sqlite3_multiplex_initialize(0, 1);
11665#endif
11666    }else if( cli_strcmp(z,"-mmap")==0 ){
11667      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11668      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11669#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11670    }else if( cli_strcmp(z,"-sorterref")==0 ){
11671      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11672      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11673#endif
11674    }else if( cli_strcmp(z,"-vfs")==0 ){
11675      zVfs = cmdline_option_value(argc, argv, ++i);
11676#ifdef SQLITE_HAVE_ZLIB
11677    }else if( cli_strcmp(z,"-zip")==0 ){
11678      data.openMode = SHELL_OPEN_ZIPFILE;
11679#endif
11680    }else if( cli_strcmp(z,"-append")==0 ){
11681      data.openMode = SHELL_OPEN_APPENDVFS;
11682#ifndef SQLITE_OMIT_DESERIALIZE
11683    }else if( cli_strcmp(z,"-deserialize")==0 ){
11684      data.openMode = SHELL_OPEN_DESERIALIZE;
11685    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
11686      data.szMax = integerValue(argv[++i]);
11687#endif
11688    }else if( cli_strcmp(z,"-readonly")==0 ){
11689      data.openMode = SHELL_OPEN_READONLY;
11690    }else if( cli_strcmp(z,"-nofollow")==0 ){
11691      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11692#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11693    }else if( cli_strncmp(z, "-A",2)==0 ){
11694      /* All remaining command-line arguments are passed to the ".archive"
11695      ** command, so ignore them */
11696      break;
11697#endif
11698    }else if( cli_strcmp(z, "-memtrace")==0 ){
11699      sqlite3MemTraceActivate(stderr);
11700    }else if( cli_strcmp(z,"-bail")==0 ){
11701      bail_on_error = 1;
11702    }else if( cli_strcmp(z,"-nonce")==0 ){
11703      free(data.zNonce);
11704      data.zNonce = strdup(argv[++i]);
11705    }else if( cli_strcmp(z,"-safe")==0 ){
11706      /* no-op - catch this on the second pass */
11707    }
11708  }
11709  verify_uninitialized();
11710
11711
11712#ifdef SQLITE_SHELL_INIT_PROC
11713  {
11714    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11715    ** of a C-function that will perform initialization actions on SQLite that
11716    ** occur just before or after sqlite3_initialize(). Use this compile-time
11717    ** option to embed this shell program in larger applications. */
11718    extern void SQLITE_SHELL_INIT_PROC(void);
11719    SQLITE_SHELL_INIT_PROC();
11720  }
11721#else
11722  /* All the sqlite3_config() calls have now been made. So it is safe
11723  ** to call sqlite3_initialize() and process any command line -vfs option. */
11724  sqlite3_initialize();
11725#endif
11726
11727  if( zVfs ){
11728    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11729    if( pVfs ){
11730      sqlite3_vfs_register(pVfs, 1);
11731    }else{
11732      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11733      exit(1);
11734    }
11735  }
11736
11737  if( data.pAuxDb->zDbFilename==0 ){
11738#ifndef SQLITE_OMIT_MEMORYDB
11739    data.pAuxDb->zDbFilename = ":memory:";
11740    warnInmemoryDb = argc==1;
11741#else
11742    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11743    return 1;
11744#endif
11745  }
11746  data.out = stdout;
11747#ifndef SQLITE_SHELL_FIDDLE
11748  sqlite3_appendvfs_init(0,0,0);
11749#endif
11750
11751  /* Go ahead and open the database file if it already exists.  If the
11752  ** file does not exist, delay opening it.  This prevents empty database
11753  ** files from being created if a user mistypes the database name argument
11754  ** to the sqlite command-line tool.
11755  */
11756  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
11757    open_db(&data, 0);
11758  }
11759
11760  /* Process the initialization file if there is one.  If no -init option
11761  ** is given on the command line, look for a file named ~/.sqliterc and
11762  ** try to process it.
11763  */
11764  process_sqliterc(&data,zInitFile);
11765
11766  /* Make a second pass through the command-line argument and set
11767  ** options.  This second pass is delayed until after the initialization
11768  ** file is processed so that the command-line arguments will override
11769  ** settings in the initialization file.
11770  */
11771  for(i=1; i<argc; i++){
11772    char *z = argv[i];
11773    if( z[0]!='-' ) continue;
11774    if( z[1]=='-' ){ z++; }
11775    if( cli_strcmp(z,"-init")==0 ){
11776      i++;
11777    }else if( cli_strcmp(z,"-html")==0 ){
11778      data.mode = MODE_Html;
11779    }else if( cli_strcmp(z,"-list")==0 ){
11780      data.mode = MODE_List;
11781    }else if( cli_strcmp(z,"-quote")==0 ){
11782      data.mode = MODE_Quote;
11783      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11784      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11785    }else if( cli_strcmp(z,"-line")==0 ){
11786      data.mode = MODE_Line;
11787    }else if( cli_strcmp(z,"-column")==0 ){
11788      data.mode = MODE_Column;
11789    }else if( cli_strcmp(z,"-json")==0 ){
11790      data.mode = MODE_Json;
11791    }else if( cli_strcmp(z,"-markdown")==0 ){
11792      data.mode = MODE_Markdown;
11793    }else if( cli_strcmp(z,"-table")==0 ){
11794      data.mode = MODE_Table;
11795    }else if( cli_strcmp(z,"-box")==0 ){
11796      data.mode = MODE_Box;
11797    }else if( cli_strcmp(z,"-csv")==0 ){
11798      data.mode = MODE_Csv;
11799      memcpy(data.colSeparator,",",2);
11800#ifdef SQLITE_HAVE_ZLIB
11801    }else if( cli_strcmp(z,"-zip")==0 ){
11802      data.openMode = SHELL_OPEN_ZIPFILE;
11803#endif
11804    }else if( cli_strcmp(z,"-append")==0 ){
11805      data.openMode = SHELL_OPEN_APPENDVFS;
11806#ifndef SQLITE_OMIT_DESERIALIZE
11807    }else if( cli_strcmp(z,"-deserialize")==0 ){
11808      data.openMode = SHELL_OPEN_DESERIALIZE;
11809    }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
11810      data.szMax = integerValue(argv[++i]);
11811#endif
11812    }else if( cli_strcmp(z,"-readonly")==0 ){
11813      data.openMode = SHELL_OPEN_READONLY;
11814    }else if( cli_strcmp(z,"-nofollow")==0 ){
11815      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11816    }else if( cli_strcmp(z,"-ascii")==0 ){
11817      data.mode = MODE_Ascii;
11818      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11819      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11820    }else if( cli_strcmp(z,"-tabs")==0 ){
11821      data.mode = MODE_List;
11822      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11823      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11824    }else if( cli_strcmp(z,"-separator")==0 ){
11825      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11826                       "%s",cmdline_option_value(argc,argv,++i));
11827    }else if( cli_strcmp(z,"-newline")==0 ){
11828      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11829                       "%s",cmdline_option_value(argc,argv,++i));
11830    }else if( cli_strcmp(z,"-nullvalue")==0 ){
11831      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11832                       "%s",cmdline_option_value(argc,argv,++i));
11833    }else if( cli_strcmp(z,"-header")==0 ){
11834      data.showHeader = 1;
11835      ShellSetFlag(&data, SHFLG_HeaderSet);
11836     }else if( cli_strcmp(z,"-noheader")==0 ){
11837      data.showHeader = 0;
11838      ShellSetFlag(&data, SHFLG_HeaderSet);
11839    }else if( cli_strcmp(z,"-echo")==0 ){
11840      ShellSetFlag(&data, SHFLG_Echo);
11841    }else if( cli_strcmp(z,"-eqp")==0 ){
11842      data.autoEQP = AUTOEQP_on;
11843    }else if( cli_strcmp(z,"-eqpfull")==0 ){
11844      data.autoEQP = AUTOEQP_full;
11845    }else if( cli_strcmp(z,"-stats")==0 ){
11846      data.statsOn = 1;
11847    }else if( cli_strcmp(z,"-scanstats")==0 ){
11848      data.scanstatsOn = 1;
11849    }else if( cli_strcmp(z,"-backslash")==0 ){
11850      /* Undocumented command-line option: -backslash
11851      ** Causes C-style backslash escapes to be evaluated in SQL statements
11852      ** prior to sending the SQL into SQLite.  Useful for injecting
11853      ** crazy bytes in the middle of SQL statements for testing and debugging.
11854      */
11855      ShellSetFlag(&data, SHFLG_Backslash);
11856    }else if( cli_strcmp(z,"-bail")==0 ){
11857      /* No-op.  The bail_on_error flag should already be set. */
11858    }else if( cli_strcmp(z,"-version")==0 ){
11859      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11860      return 0;
11861    }else if( cli_strcmp(z,"-interactive")==0 ){
11862      stdin_is_interactive = 1;
11863    }else if( cli_strcmp(z,"-batch")==0 ){
11864      stdin_is_interactive = 0;
11865    }else if( cli_strcmp(z,"-heap")==0 ){
11866      i++;
11867    }else if( cli_strcmp(z,"-pagecache")==0 ){
11868      i+=2;
11869    }else if( cli_strcmp(z,"-lookaside")==0 ){
11870      i+=2;
11871    }else if( cli_strcmp(z,"-threadsafe")==0 ){
11872      i+=2;
11873    }else if( cli_strcmp(z,"-nonce")==0 ){
11874      i += 2;
11875    }else if( cli_strcmp(z,"-mmap")==0 ){
11876      i++;
11877    }else if( cli_strcmp(z,"-memtrace")==0 ){
11878      i++;
11879#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11880    }else if( cli_strcmp(z,"-sorterref")==0 ){
11881      i++;
11882#endif
11883    }else if( cli_strcmp(z,"-vfs")==0 ){
11884      i++;
11885#ifdef SQLITE_ENABLE_VFSTRACE
11886    }else if( cli_strcmp(z,"-vfstrace")==0 ){
11887      i++;
11888#endif
11889#ifdef SQLITE_ENABLE_MULTIPLEX
11890    }else if( cli_strcmp(z,"-multiplex")==0 ){
11891      i++;
11892#endif
11893    }else if( cli_strcmp(z,"-help")==0 ){
11894      usage(1);
11895    }else if( cli_strcmp(z,"-cmd")==0 ){
11896      /* Run commands that follow -cmd first and separately from commands
11897      ** that simply appear on the command-line.  This seems goofy.  It would
11898      ** be better if all commands ran in the order that they appear.  But
11899      ** we retain the goofy behavior for historical compatibility. */
11900      if( i==argc-1 ) break;
11901      z = cmdline_option_value(argc,argv,++i);
11902      if( z[0]=='.' ){
11903        rc = do_meta_command(z, &data);
11904        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11905      }else{
11906        open_db(&data, 0);
11907        rc = shell_exec(&data, z, &zErrMsg);
11908        if( zErrMsg!=0 ){
11909          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11910          if( bail_on_error ) return rc!=0 ? rc : 1;
11911        }else if( rc!=0 ){
11912          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11913          if( bail_on_error ) return rc;
11914        }
11915      }
11916#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11917    }else if( cli_strncmp(z, "-A", 2)==0 ){
11918      if( nCmd>0 ){
11919        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11920                            " with \"%s\"\n", z);
11921        return 1;
11922      }
11923      open_db(&data, OPEN_DB_ZIPFILE);
11924      if( z[2] ){
11925        argv[i] = &z[2];
11926        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11927      }else{
11928        arDotCommand(&data, 1, argv+i, argc-i);
11929      }
11930      readStdin = 0;
11931      break;
11932#endif
11933    }else if( cli_strcmp(z,"-safe")==0 ){
11934      data.bSafeMode = data.bSafeModePersist = 1;
11935    }else{
11936      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11937      raw_printf(stderr,"Use -help for a list of options.\n");
11938      return 1;
11939    }
11940    data.cMode = data.mode;
11941  }
11942
11943  if( !readStdin ){
11944    /* Run all arguments that do not begin with '-' as if they were separate
11945    ** command-line inputs, except for the argToSkip argument which contains
11946    ** the database filename.
11947    */
11948    for(i=0; i<nCmd; i++){
11949      if( azCmd[i][0]=='.' ){
11950        rc = do_meta_command(azCmd[i], &data);
11951        if( rc ){
11952          free(azCmd);
11953          return rc==2 ? 0 : rc;
11954        }
11955      }else{
11956        open_db(&data, 0);
11957        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11958        if( zErrMsg || rc ){
11959          if( zErrMsg!=0 ){
11960            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11961          }else{
11962            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11963          }
11964          sqlite3_free(zErrMsg);
11965          free(azCmd);
11966          return rc!=0 ? rc : 1;
11967        }
11968      }
11969    }
11970  }else{
11971    /* Run commands received from standard input
11972    */
11973    if( stdin_is_interactive ){
11974      char *zHome;
11975      char *zHistory;
11976      int nHistory;
11977      printf(
11978        "SQLite version %s %.19s\n" /*extra-version-info*/
11979        "Enter \".help\" for usage hints.\n",
11980        sqlite3_libversion(), sqlite3_sourceid()
11981      );
11982      if( warnInmemoryDb ){
11983        printf("Connected to a ");
11984        printBold("transient in-memory database");
11985        printf(".\nUse \".open FILENAME\" to reopen on a "
11986               "persistent database.\n");
11987      }
11988      zHistory = getenv("SQLITE_HISTORY");
11989      if( zHistory ){
11990        zHistory = strdup(zHistory);
11991      }else if( (zHome = find_home_dir(0))!=0 ){
11992        nHistory = strlen30(zHome) + 20;
11993        if( (zHistory = malloc(nHistory))!=0 ){
11994          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11995        }
11996      }
11997      if( zHistory ){ shell_read_history(zHistory); }
11998#if HAVE_READLINE || HAVE_EDITLINE
11999      rl_attempted_completion_function = readline_completion;
12000#elif HAVE_LINENOISE
12001      linenoiseSetCompletionCallback(linenoise_completion);
12002#endif
12003      data.in = 0;
12004      rc = process_input(&data);
12005      if( zHistory ){
12006        shell_stifle_history(2000);
12007        shell_write_history(zHistory);
12008        free(zHistory);
12009      }
12010    }else{
12011      data.in = stdin;
12012      rc = process_input(&data);
12013    }
12014  }
12015#ifndef SQLITE_SHELL_FIDDLE
12016  /* In WASM mode we have to leave the db state in place so that
12017  ** client code can "push" SQL into it after this call returns. */
12018  free(azCmd);
12019  set_table_name(&data, 0);
12020  if( data.db ){
12021    session_close_all(&data, -1);
12022    close_db(data.db);
12023  }
12024  for(i=0; i<ArraySize(data.aAuxDb); i++){
12025    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12026    if( data.aAuxDb[i].db ){
12027      session_close_all(&data, i);
12028      close_db(data.aAuxDb[i].db);
12029    }
12030  }
12031  find_home_dir(1);
12032  output_reset(&data);
12033  data.doXdgOpen = 0;
12034  clearTempFile(&data);
12035#if !SQLITE_SHELL_IS_UTF8
12036  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12037  free(argvToFree);
12038#endif
12039  free(data.colWidth);
12040  free(data.zNonce);
12041  /* Clear the global data structure so that valgrind will detect memory
12042  ** leaks */
12043  memset(&data, 0, sizeof(data));
12044#ifdef SQLITE_DEBUG
12045  if( sqlite3_memory_used()>mem_main_enter ){
12046    utf8_printf(stderr, "Memory leaked: %u bytes\n",
12047                (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12048  }
12049#endif
12050#endif /* !SQLITE_SHELL_FIDDLE */
12051  return rc;
12052}
12053
12054
12055#ifdef SQLITE_SHELL_FIDDLE
12056/* Only for emcc experimentation purposes. */
12057int fiddle_experiment(int a,int b){
12058  return a + b;
12059}
12060
12061/*
12062** Returns a pointer to the current DB handle.
12063*/
12064sqlite3 * fiddle_db_handle(){
12065  return globalDb;
12066}
12067
12068/*
12069** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
12070** "main" is assumed. Returns 0 if no db with the given name is
12071** open.
12072*/
12073sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
12074  sqlite3_vfs * pVfs = 0;
12075  if(globalDb){
12076    sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
12077                         SQLITE_FCNTL_VFS_POINTER, &pVfs);
12078  }
12079  return pVfs;
12080}
12081
12082/* Only for emcc experimentation purposes. */
12083sqlite3 * fiddle_db_arg(sqlite3 *arg){
12084    printf("fiddle_db_arg(%p)\n", (const void*)arg);
12085    return arg;
12086}
12087
12088/*
12089** Intended to be called via a SharedWorker() while a separate
12090** SharedWorker() (which manages the wasm module) is performing work
12091** which should be interrupted. Unfortunately, SharedWorker is not
12092** portable enough to make real use of.
12093*/
12094void fiddle_interrupt(void){
12095  if( globalDb ) sqlite3_interrupt(globalDb);
12096}
12097
12098/*
12099** Returns the filename of the given db name, assuming "main" if
12100** zDbName is NULL. Returns NULL if globalDb is not opened.
12101*/
12102const char * fiddle_db_filename(const char * zDbName){
12103    return globalDb
12104      ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12105      : NULL;
12106}
12107
12108/*
12109** Completely wipes out the contents of the currently-opened database
12110** but leaves its storage intact for reuse.
12111*/
12112void fiddle_reset_db(void){
12113  if( globalDb ){
12114    int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
12115    if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
12116    sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
12117  }
12118}
12119
12120/*
12121** Uses the current database's VFS xRead to stream the db file's
12122** contents out to the given callback. The callback gets a single
12123** chunk of size n (its 2nd argument) on each call and must return 0
12124** on success, non-0 on error. This function returns 0 on success,
12125** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
12126** code from the callback. Note that this is not thread-friendly: it
12127** expects that it will be the only thread reading the db file and
12128** takes no measures to ensure that is the case.
12129*/
12130int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
12131  sqlite3_int64 nSize = 0;
12132  sqlite3_int64 nPos = 0;
12133  sqlite3_file * pFile = 0;
12134  unsigned char buf[1024 * 8];
12135  int nBuf = (int)sizeof(buf);
12136  int rc = shellState.db
12137    ? sqlite3_file_control(shellState.db, "main",
12138                           SQLITE_FCNTL_FILE_POINTER, &pFile)
12139    : SQLITE_NOTFOUND;
12140  if( rc ) return rc;
12141  rc = pFile->pMethods->xFileSize(pFile, &nSize);
12142  if( rc ) return rc;
12143  if(nSize % nBuf){
12144    /* DB size is not an even multiple of the buffer size. Reduce
12145    ** buffer size so that we do not unduly inflate the db size when
12146    ** exporting. */
12147    if(0 == nSize % 4096) nBuf = 4096;
12148    else if(0 == nSize % 2048) nBuf = 2048;
12149    else if(0 == nSize % 1024) nBuf = 1024;
12150    else nBuf = 512;
12151  }
12152  for( ; 0==rc && nPos<nSize; nPos += nBuf ){
12153    rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
12154    if(SQLITE_IOERR_SHORT_READ == rc){
12155      rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
12156    }
12157    if( 0==rc ) rc = xCallback(buf, nBuf);
12158  }
12159  return rc;
12160}
12161
12162/*
12163** Trivial exportable function for emscripten. It processes zSql as if
12164** it were input to the sqlite3 shell and redirects all output to the
12165** wasm binding. fiddle_main() must have been called before this
12166** is called, or results are undefined.
12167*/
12168void fiddle_exec(const char * zSql){
12169  if(zSql && *zSql){
12170    if('.'==*zSql) puts(zSql);
12171    shellState.wasm.zInput = zSql;
12172    shellState.wasm.zPos = zSql;
12173    process_input(&shellState);
12174    shellState.wasm.zInput = shellState.wasm.zPos = 0;
12175  }
12176}
12177#endif /* SQLITE_SHELL_FIDDLE */
12178