xref: /sqlite-3.40.0/src/shell.c.in (revision fb32c44e)
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** Warning pragmas copied from msvc.h in the core.
22*/
23#if defined(_MSC_VER)
24#pragma warning(disable : 4054)
25#pragma warning(disable : 4055)
26#pragma warning(disable : 4100)
27#pragma warning(disable : 4127)
28#pragma warning(disable : 4130)
29#pragma warning(disable : 4152)
30#pragma warning(disable : 4189)
31#pragma warning(disable : 4206)
32#pragma warning(disable : 4210)
33#pragma warning(disable : 4232)
34#pragma warning(disable : 4244)
35#pragma warning(disable : 4305)
36#pragma warning(disable : 4306)
37#pragma warning(disable : 4702)
38#pragma warning(disable : 4706)
39#endif /* defined(_MSC_VER) */
40
41/*
42** No support for loadable extensions in VxWorks.
43*/
44#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
45# define SQLITE_OMIT_LOAD_EXTENSION 1
46#endif
47
48/*
49** Enable large-file support for fopen() and friends on unix.
50*/
51#ifndef SQLITE_DISABLE_LFS
52# define _LARGE_FILE       1
53# ifndef _FILE_OFFSET_BITS
54#   define _FILE_OFFSET_BITS 64
55# endif
56# define _LARGEFILE_SOURCE 1
57#endif
58
59#include <stdlib.h>
60#include <string.h>
61#include <stdio.h>
62#include <assert.h>
63#include "sqlite3.h"
64typedef sqlite3_int64 i64;
65typedef sqlite3_uint64 u64;
66typedef unsigned char u8;
67#if SQLITE_USER_AUTHENTICATION
68# include "sqlite3userauth.h"
69#endif
70#include <ctype.h>
71#include <stdarg.h>
72
73#if !defined(_WIN32) && !defined(WIN32)
74# include <signal.h>
75# if !defined(__RTP__) && !defined(_WRS_KERNEL)
76#  include <pwd.h>
77# endif
78#endif
79#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
80# include <unistd.h>
81# include <dirent.h>
82# if defined(__MINGW32__)
83#  define DIRENT dirent
84#  ifndef S_ISLNK
85#   define S_ISLNK(mode) (0)
86#  endif
87# endif
88#endif
89#include <sys/types.h>
90#include <sys/stat.h>
91
92#if HAVE_READLINE
93# include <readline/readline.h>
94# include <readline/history.h>
95#endif
96
97#if HAVE_EDITLINE
98# include <editline/readline.h>
99#endif
100
101#if HAVE_EDITLINE || HAVE_READLINE
102
103# define shell_add_history(X) add_history(X)
104# define shell_read_history(X) read_history(X)
105# define shell_write_history(X) write_history(X)
106# define shell_stifle_history(X) stifle_history(X)
107# define shell_readline(X) readline(X)
108
109#elif HAVE_LINENOISE
110
111# include "linenoise.h"
112# define shell_add_history(X) linenoiseHistoryAdd(X)
113# define shell_read_history(X) linenoiseHistoryLoad(X)
114# define shell_write_history(X) linenoiseHistorySave(X)
115# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
116# define shell_readline(X) linenoise(X)
117
118#else
119
120# define shell_read_history(X)
121# define shell_write_history(X)
122# define shell_stifle_history(X)
123
124# define SHELL_USE_LOCAL_GETLINE 1
125#endif
126
127
128#if defined(_WIN32) || defined(WIN32)
129# include <io.h>
130# include <fcntl.h>
131# define isatty(h) _isatty(h)
132# ifndef access
133#  define access(f,m) _access((f),(m))
134# endif
135# ifndef unlink
136#  define unlink _unlink
137# endif
138# undef popen
139# define popen _popen
140# undef pclose
141# define pclose _pclose
142#else
143 /* Make sure isatty() has a prototype. */
144 extern int isatty(int);
145
146# if !defined(__RTP__) && !defined(_WRS_KERNEL)
147  /* popen and pclose are not C89 functions and so are
148  ** sometimes omitted from the <stdio.h> header */
149   extern FILE *popen(const char*,const char*);
150   extern int pclose(FILE*);
151# else
152#  define SQLITE_OMIT_POPEN 1
153# endif
154#endif
155
156#if defined(_WIN32_WCE)
157/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
158 * thus we always assume that we have a console. That can be
159 * overridden with the -batch command line option.
160 */
161#define isatty(x) 1
162#endif
163
164/* ctype macros that work with signed characters */
165#define IsSpace(X)  isspace((unsigned char)X)
166#define IsDigit(X)  isdigit((unsigned char)X)
167#define ToLower(X)  (char)tolower((unsigned char)X)
168
169#if defined(_WIN32) || defined(WIN32)
170#include <windows.h>
171
172/* string conversion routines only needed on Win32 */
173extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
174extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
175extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
176extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
177#endif
178
179/* On Windows, we normally run with output mode of TEXT so that \n characters
180** are automatically translated into \r\n.  However, this behavior needs
181** to be disabled in some cases (ex: when generating CSV output and when
182** rendering quoted strings that contain \n characters).  The following
183** routines take care of that.
184*/
185#if defined(_WIN32) || defined(WIN32)
186static void setBinaryMode(FILE *file, int isOutput){
187  if( isOutput ) fflush(file);
188  _setmode(_fileno(file), _O_BINARY);
189}
190static void setTextMode(FILE *file, int isOutput){
191  if( isOutput ) fflush(file);
192  _setmode(_fileno(file), _O_TEXT);
193}
194#else
195# define setBinaryMode(X,Y)
196# define setTextMode(X,Y)
197#endif
198
199
200/* True if the timer is enabled */
201static int enableTimer = 0;
202
203/* Return the current wall-clock time */
204static sqlite3_int64 timeOfDay(void){
205  static sqlite3_vfs *clockVfs = 0;
206  sqlite3_int64 t;
207  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
208  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
209    clockVfs->xCurrentTimeInt64(clockVfs, &t);
210  }else{
211    double r;
212    clockVfs->xCurrentTime(clockVfs, &r);
213    t = (sqlite3_int64)(r*86400000.0);
214  }
215  return t;
216}
217
218#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
219#include <sys/time.h>
220#include <sys/resource.h>
221
222/* VxWorks does not support getrusage() as far as we can determine */
223#if defined(_WRS_KERNEL) || defined(__RTP__)
224struct rusage {
225  struct timeval ru_utime; /* user CPU time used */
226  struct timeval ru_stime; /* system CPU time used */
227};
228#define getrusage(A,B) memset(B,0,sizeof(*B))
229#endif
230
231/* Saved resource information for the beginning of an operation */
232static struct rusage sBegin;  /* CPU time at start */
233static sqlite3_int64 iBegin;  /* Wall-clock time at start */
234
235/*
236** Begin timing an operation
237*/
238static void beginTimer(void){
239  if( enableTimer ){
240    getrusage(RUSAGE_SELF, &sBegin);
241    iBegin = timeOfDay();
242  }
243}
244
245/* Return the difference of two time_structs in seconds */
246static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
247  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
248         (double)(pEnd->tv_sec - pStart->tv_sec);
249}
250
251/*
252** Print the timing results.
253*/
254static void endTimer(void){
255  if( enableTimer ){
256    sqlite3_int64 iEnd = timeOfDay();
257    struct rusage sEnd;
258    getrusage(RUSAGE_SELF, &sEnd);
259    printf("Run Time: real %.3f user %f sys %f\n",
260       (iEnd - iBegin)*0.001,
261       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
262       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
263  }
264}
265
266#define BEGIN_TIMER beginTimer()
267#define END_TIMER endTimer()
268#define HAS_TIMER 1
269
270#elif (defined(_WIN32) || defined(WIN32))
271
272/* Saved resource information for the beginning of an operation */
273static HANDLE hProcess;
274static FILETIME ftKernelBegin;
275static FILETIME ftUserBegin;
276static sqlite3_int64 ftWallBegin;
277typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
278                                    LPFILETIME, LPFILETIME);
279static GETPROCTIMES getProcessTimesAddr = NULL;
280
281/*
282** Check to see if we have timer support.  Return 1 if necessary
283** support found (or found previously).
284*/
285static int hasTimer(void){
286  if( getProcessTimesAddr ){
287    return 1;
288  } else {
289    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
290    ** versions. See if the version we are running on has it, and if it
291    ** does, save off a pointer to it and the current process handle.
292    */
293    hProcess = GetCurrentProcess();
294    if( hProcess ){
295      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
296      if( NULL != hinstLib ){
297        getProcessTimesAddr =
298            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
299        if( NULL != getProcessTimesAddr ){
300          return 1;
301        }
302        FreeLibrary(hinstLib);
303      }
304    }
305  }
306  return 0;
307}
308
309/*
310** Begin timing an operation
311*/
312static void beginTimer(void){
313  if( enableTimer && getProcessTimesAddr ){
314    FILETIME ftCreation, ftExit;
315    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
316                        &ftKernelBegin,&ftUserBegin);
317    ftWallBegin = timeOfDay();
318  }
319}
320
321/* Return the difference of two FILETIME structs in seconds */
322static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
323  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
324  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
325  return (double) ((i64End - i64Start) / 10000000.0);
326}
327
328/*
329** Print the timing results.
330*/
331static void endTimer(void){
332  if( enableTimer && getProcessTimesAddr){
333    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
334    sqlite3_int64 ftWallEnd = timeOfDay();
335    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
336    printf("Run Time: real %.3f user %f sys %f\n",
337       (ftWallEnd - ftWallBegin)*0.001,
338       timeDiff(&ftUserBegin, &ftUserEnd),
339       timeDiff(&ftKernelBegin, &ftKernelEnd));
340  }
341}
342
343#define BEGIN_TIMER beginTimer()
344#define END_TIMER endTimer()
345#define HAS_TIMER hasTimer()
346
347#else
348#define BEGIN_TIMER
349#define END_TIMER
350#define HAS_TIMER 0
351#endif
352
353/*
354** Used to prevent warnings about unused parameters
355*/
356#define UNUSED_PARAMETER(x) (void)(x)
357
358/*
359** Number of elements in an array
360*/
361#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
362
363/*
364** If the following flag is set, then command execution stops
365** at an error if we are not interactive.
366*/
367static int bail_on_error = 0;
368
369/*
370** Threat stdin as an interactive input if the following variable
371** is true.  Otherwise, assume stdin is connected to a file or pipe.
372*/
373static int stdin_is_interactive = 1;
374
375/*
376** On Windows systems we have to know if standard output is a console
377** in order to translate UTF-8 into MBCS.  The following variable is
378** true if translation is required.
379*/
380static int stdout_is_console = 1;
381
382/*
383** The following is the open SQLite database.  We make a pointer
384** to this database a static variable so that it can be accessed
385** by the SIGINT handler to interrupt database processing.
386*/
387static sqlite3 *globalDb = 0;
388
389/*
390** True if an interrupt (Control-C) has been received.
391*/
392static volatile int seenInterrupt = 0;
393
394/*
395** This is the name of our program. It is set in main(), used
396** in a number of other places, mostly for error messages.
397*/
398static char *Argv0;
399
400/*
401** Prompt strings. Initialized in main. Settable with
402**   .prompt main continue
403*/
404static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
405static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
406
407/*
408** Render output like fprintf().  Except, if the output is going to the
409** console and if this is running on a Windows machine, translate the
410** output from UTF-8 into MBCS.
411*/
412#if defined(_WIN32) || defined(WIN32)
413void utf8_printf(FILE *out, const char *zFormat, ...){
414  va_list ap;
415  va_start(ap, zFormat);
416  if( stdout_is_console && (out==stdout || out==stderr) ){
417    char *z1 = sqlite3_vmprintf(zFormat, ap);
418    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
419    sqlite3_free(z1);
420    fputs(z2, out);
421    sqlite3_free(z2);
422  }else{
423    vfprintf(out, zFormat, ap);
424  }
425  va_end(ap);
426}
427#elif !defined(utf8_printf)
428# define utf8_printf fprintf
429#endif
430
431/*
432** Render output like fprintf().  This should not be used on anything that
433** includes string formatting (e.g. "%s").
434*/
435#if !defined(raw_printf)
436# define raw_printf fprintf
437#endif
438
439/*
440** Write I/O traces to the following stream.
441*/
442#ifdef SQLITE_ENABLE_IOTRACE
443static FILE *iotrace = 0;
444#endif
445
446/*
447** This routine works like printf in that its first argument is a
448** format string and subsequent arguments are values to be substituted
449** in place of % fields.  The result of formatting this string
450** is written to iotrace.
451*/
452#ifdef SQLITE_ENABLE_IOTRACE
453static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
454  va_list ap;
455  char *z;
456  if( iotrace==0 ) return;
457  va_start(ap, zFormat);
458  z = sqlite3_vmprintf(zFormat, ap);
459  va_end(ap);
460  utf8_printf(iotrace, "%s", z);
461  sqlite3_free(z);
462}
463#endif
464
465/*
466** Output string zUtf to stream pOut as w characters.  If w is negative,
467** then right-justify the text.  W is the width in UTF-8 characters, not
468** in bytes.  This is different from the %*.*s specification in printf
469** since with %*.*s the width is measured in bytes, not characters.
470*/
471static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
472  int i;
473  int n;
474  int aw = w<0 ? -w : w;
475  char zBuf[1000];
476  if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
477  for(i=n=0; zUtf[i]; i++){
478    if( (zUtf[i]&0xc0)!=0x80 ){
479      n++;
480      if( n==aw ){
481        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
482        break;
483      }
484    }
485  }
486  if( n>=aw ){
487    utf8_printf(pOut, "%.*s", i, zUtf);
488  }else if( w<0 ){
489    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
490  }else{
491    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
492  }
493}
494
495
496/*
497** Determines if a string is a number of not.
498*/
499static int isNumber(const char *z, int *realnum){
500  if( *z=='-' || *z=='+' ) z++;
501  if( !IsDigit(*z) ){
502    return 0;
503  }
504  z++;
505  if( realnum ) *realnum = 0;
506  while( IsDigit(*z) ){ z++; }
507  if( *z=='.' ){
508    z++;
509    if( !IsDigit(*z) ) return 0;
510    while( IsDigit(*z) ){ z++; }
511    if( realnum ) *realnum = 1;
512  }
513  if( *z=='e' || *z=='E' ){
514    z++;
515    if( *z=='+' || *z=='-' ) z++;
516    if( !IsDigit(*z) ) return 0;
517    while( IsDigit(*z) ){ z++; }
518    if( realnum ) *realnum = 1;
519  }
520  return *z==0;
521}
522
523/*
524** Compute a string length that is limited to what can be stored in
525** lower 30 bits of a 32-bit signed integer.
526*/
527static int strlen30(const char *z){
528  const char *z2 = z;
529  while( *z2 ){ z2++; }
530  return 0x3fffffff & (int)(z2 - z);
531}
532
533/*
534** Return the length of a string in characters.  Multibyte UTF8 characters
535** count as a single character.
536*/
537static int strlenChar(const char *z){
538  int n = 0;
539  while( *z ){
540    if( (0xc0&*(z++))!=0x80 ) n++;
541  }
542  return n;
543}
544
545/*
546** This routine reads a line of text from FILE in, stores
547** the text in memory obtained from malloc() and returns a pointer
548** to the text.  NULL is returned at end of file, or if malloc()
549** fails.
550**
551** If zLine is not NULL then it is a malloced buffer returned from
552** a previous call to this routine that may be reused.
553*/
554static char *local_getline(char *zLine, FILE *in){
555  int nLine = zLine==0 ? 0 : 100;
556  int n = 0;
557
558  while( 1 ){
559    if( n+100>nLine ){
560      nLine = nLine*2 + 100;
561      zLine = realloc(zLine, nLine);
562      if( zLine==0 ) return 0;
563    }
564    if( fgets(&zLine[n], nLine - n, in)==0 ){
565      if( n==0 ){
566        free(zLine);
567        return 0;
568      }
569      zLine[n] = 0;
570      break;
571    }
572    while( zLine[n] ) n++;
573    if( n>0 && zLine[n-1]=='\n' ){
574      n--;
575      if( n>0 && zLine[n-1]=='\r' ) n--;
576      zLine[n] = 0;
577      break;
578    }
579  }
580#if defined(_WIN32) || defined(WIN32)
581  /* For interactive input on Windows systems, translate the
582  ** multi-byte characterset characters into UTF-8. */
583  if( stdin_is_interactive && in==stdin ){
584    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
585    if( zTrans ){
586      int nTrans = strlen30(zTrans)+1;
587      if( nTrans>nLine ){
588        zLine = realloc(zLine, nTrans);
589        if( zLine==0 ){
590          sqlite3_free(zTrans);
591          return 0;
592        }
593      }
594      memcpy(zLine, zTrans, nTrans);
595      sqlite3_free(zTrans);
596    }
597  }
598#endif /* defined(_WIN32) || defined(WIN32) */
599  return zLine;
600}
601
602/*
603** Retrieve a single line of input text.
604**
605** If in==0 then read from standard input and prompt before each line.
606** If isContinuation is true, then a continuation prompt is appropriate.
607** If isContinuation is zero, then the main prompt should be used.
608**
609** If zPrior is not NULL then it is a buffer from a prior call to this
610** routine that can be reused.
611**
612** The result is stored in space obtained from malloc() and must either
613** be freed by the caller or else passed back into this routine via the
614** zPrior argument for reuse.
615*/
616static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
617  char *zPrompt;
618  char *zResult;
619  if( in!=0 ){
620    zResult = local_getline(zPrior, in);
621  }else{
622    zPrompt = isContinuation ? continuePrompt : mainPrompt;
623#if SHELL_USE_LOCAL_GETLINE
624    printf("%s", zPrompt);
625    fflush(stdout);
626    zResult = local_getline(zPrior, stdin);
627#else
628    free(zPrior);
629    zResult = shell_readline(zPrompt);
630    if( zResult && *zResult ) shell_add_history(zResult);
631#endif
632  }
633  return zResult;
634}
635
636
637/*
638** Return the value of a hexadecimal digit.  Return -1 if the input
639** is not a hex digit.
640*/
641static int hexDigitValue(char c){
642  if( c>='0' && c<='9' ) return c - '0';
643  if( c>='a' && c<='f' ) return c - 'a' + 10;
644  if( c>='A' && c<='F' ) return c - 'A' + 10;
645  return -1;
646}
647
648/*
649** Interpret zArg as an integer value, possibly with suffixes.
650*/
651static sqlite3_int64 integerValue(const char *zArg){
652  sqlite3_int64 v = 0;
653  static const struct { char *zSuffix; int iMult; } aMult[] = {
654    { "KiB", 1024 },
655    { "MiB", 1024*1024 },
656    { "GiB", 1024*1024*1024 },
657    { "KB",  1000 },
658    { "MB",  1000000 },
659    { "GB",  1000000000 },
660    { "K",   1000 },
661    { "M",   1000000 },
662    { "G",   1000000000 },
663  };
664  int i;
665  int isNeg = 0;
666  if( zArg[0]=='-' ){
667    isNeg = 1;
668    zArg++;
669  }else if( zArg[0]=='+' ){
670    zArg++;
671  }
672  if( zArg[0]=='0' && zArg[1]=='x' ){
673    int x;
674    zArg += 2;
675    while( (x = hexDigitValue(zArg[0]))>=0 ){
676      v = (v<<4) + x;
677      zArg++;
678    }
679  }else{
680    while( IsDigit(zArg[0]) ){
681      v = v*10 + zArg[0] - '0';
682      zArg++;
683    }
684  }
685  for(i=0; i<ArraySize(aMult); i++){
686    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
687      v *= aMult[i].iMult;
688      break;
689    }
690  }
691  return isNeg? -v : v;
692}
693
694/*
695** A variable length string to which one can append text.
696*/
697typedef struct ShellText ShellText;
698struct ShellText {
699  char *z;
700  int n;
701  int nAlloc;
702};
703
704/*
705** Initialize and destroy a ShellText object
706*/
707static void initText(ShellText *p){
708  memset(p, 0, sizeof(*p));
709}
710static void freeText(ShellText *p){
711  free(p->z);
712  initText(p);
713}
714
715/* zIn is either a pointer to a NULL-terminated string in memory obtained
716** from malloc(), or a NULL pointer. The string pointed to by zAppend is
717** added to zIn, and the result returned in memory obtained from malloc().
718** zIn, if it was not NULL, is freed.
719**
720** If the third argument, quote, is not '\0', then it is used as a
721** quote character for zAppend.
722*/
723static void appendText(ShellText *p, char const *zAppend, char quote){
724  int len;
725  int i;
726  int nAppend = strlen30(zAppend);
727
728  len = nAppend+p->n+1;
729  if( quote ){
730    len += 2;
731    for(i=0; i<nAppend; i++){
732      if( zAppend[i]==quote ) len++;
733    }
734  }
735
736  if( p->n+len>=p->nAlloc ){
737    p->nAlloc = p->nAlloc*2 + len + 20;
738    p->z = realloc(p->z, p->nAlloc);
739    if( p->z==0 ){
740      memset(p, 0, sizeof(*p));
741      return;
742    }
743  }
744
745  if( quote ){
746    char *zCsr = p->z+p->n;
747    *zCsr++ = quote;
748    for(i=0; i<nAppend; i++){
749      *zCsr++ = zAppend[i];
750      if( zAppend[i]==quote ) *zCsr++ = quote;
751    }
752    *zCsr++ = quote;
753    p->n = (int)(zCsr - p->z);
754    *zCsr = '\0';
755  }else{
756    memcpy(p->z+p->n, zAppend, nAppend);
757    p->n += nAppend;
758    p->z[p->n] = '\0';
759  }
760}
761
762/*
763** Attempt to determine if identifier zName needs to be quoted, either
764** because it contains non-alphanumeric characters, or because it is an
765** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
766** that quoting is required.
767**
768** Return '"' if quoting is required.  Return 0 if no quoting is required.
769*/
770static char quoteChar(const char *zName){
771  /* All SQLite keywords, in alphabetical order */
772  static const char *azKeywords[] = {
773    "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
774    "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
775    "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
776    "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
777    "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
778    "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
779    "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
780    "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
781    "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
782    "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
783    "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
784    "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
785    "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
786    "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
787    "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
788    "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
789    "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
790    "WITH", "WITHOUT",
791  };
792  int i, lwr, upr, mid, c;
793  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
794  for(i=0; zName[i]; i++){
795    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
796  }
797  lwr = 0;
798  upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
799  while( lwr<=upr ){
800    mid = (lwr+upr)/2;
801    c = sqlite3_stricmp(azKeywords[mid], zName);
802    if( c==0 ) return '"';
803    if( c<0 ){
804      lwr = mid+1;
805    }else{
806      upr = mid-1;
807    }
808  }
809  return 0;
810}
811
812/*
813** Construct a fake object name and column list to describe the structure
814** of the view, virtual table, or table valued function zSchema.zName.
815*/
816static char *shellFakeSchema(
817  sqlite3 *db,            /* The database connection containing the vtab */
818  const char *zSchema,    /* Schema of the database holding the vtab */
819  const char *zName       /* The name of the virtual table */
820){
821  sqlite3_stmt *pStmt = 0;
822  char *zSql;
823  ShellText s;
824  char cQuote;
825  char *zDiv = "(";
826  int nRow = 0;
827
828  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
829                         zSchema ? zSchema : "main", zName);
830  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
831  sqlite3_free(zSql);
832  initText(&s);
833  if( zSchema ){
834    cQuote = quoteChar(zSchema);
835    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
836    appendText(&s, zSchema, cQuote);
837    appendText(&s, ".", 0);
838  }
839  cQuote = quoteChar(zName);
840  appendText(&s, zName, cQuote);
841  while( sqlite3_step(pStmt)==SQLITE_ROW ){
842    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
843    nRow++;
844    appendText(&s, zDiv, 0);
845    zDiv = ",";
846    cQuote = quoteChar(zCol);
847    appendText(&s, zCol, cQuote);
848  }
849  appendText(&s, ")", 0);
850  sqlite3_finalize(pStmt);
851  if( nRow==0 ){
852    freeText(&s);
853    s.z = 0;
854  }
855  return s.z;
856}
857
858/*
859** SQL function:  shell_module_schema(X)
860**
861** Return a fake schema for the table-valued function or eponymous virtual
862** table X.
863*/
864static void shellModuleSchema(
865  sqlite3_context *pCtx,
866  int nVal,
867  sqlite3_value **apVal
868){
869  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
870  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
871  UNUSED_PARAMETER(nVal);
872  if( zFake ){
873    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
874                        -1, sqlite3_free);
875    free(zFake);
876  }
877}
878
879/*
880** SQL function:  shell_add_schema(S,X)
881**
882** Add the schema name X to the CREATE statement in S and return the result.
883** Examples:
884**
885**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
886**
887** Also works on
888**
889**    CREATE INDEX
890**    CREATE UNIQUE INDEX
891**    CREATE VIEW
892**    CREATE TRIGGER
893**    CREATE VIRTUAL TABLE
894**
895** This UDF is used by the .schema command to insert the schema name of
896** attached databases into the middle of the sqlite_master.sql field.
897*/
898static void shellAddSchemaName(
899  sqlite3_context *pCtx,
900  int nVal,
901  sqlite3_value **apVal
902){
903  static const char *aPrefix[] = {
904     "TABLE",
905     "INDEX",
906     "UNIQUE INDEX",
907     "VIEW",
908     "TRIGGER",
909     "VIRTUAL TABLE"
910  };
911  int i = 0;
912  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
913  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
914  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
915  sqlite3 *db = sqlite3_context_db_handle(pCtx);
916  UNUSED_PARAMETER(nVal);
917  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
918    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
919      int n = strlen30(aPrefix[i]);
920      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
921        char *z = 0;
922        char *zFake = 0;
923        if( zSchema ){
924          char cQuote = quoteChar(zSchema);
925          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
926            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
927          }else{
928            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
929          }
930        }
931        if( zName
932         && aPrefix[i][0]=='V'
933         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
934        ){
935          if( z==0 ){
936            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
937          }else{
938            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
939          }
940          free(zFake);
941        }
942        if( z ){
943          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
944          return;
945        }
946      }
947    }
948  }
949  sqlite3_result_value(pCtx, apVal[0]);
950}
951
952/*
953** The source code for several run-time loadable extensions is inserted
954** below by the ../tool/mkshellc.tcl script.  Before processing that included
955** code, we need to override some macros to make the included program code
956** work here in the middle of this regular program.
957*/
958#define SQLITE_EXTENSION_INIT1
959#define SQLITE_EXTENSION_INIT2(X) (void)(X)
960
961#if defined(_WIN32) && defined(_MSC_VER)
962INCLUDE test_windirent.h
963INCLUDE test_windirent.c
964#define dirent DIRENT
965#endif
966INCLUDE ../ext/misc/shathree.c
967INCLUDE ../ext/misc/fileio.c
968INCLUDE ../ext/misc/completion.c
969INCLUDE ../ext/misc/appendvfs.c
970#ifdef SQLITE_HAVE_ZLIB
971INCLUDE ../ext/misc/zipfile.c
972INCLUDE ../ext/misc/sqlar.c
973#endif
974INCLUDE ../ext/expert/sqlite3expert.h
975INCLUDE ../ext/expert/sqlite3expert.c
976
977#if defined(SQLITE_ENABLE_SESSION)
978/*
979** State information for a single open session
980*/
981typedef struct OpenSession OpenSession;
982struct OpenSession {
983  char *zName;             /* Symbolic name for this session */
984  int nFilter;             /* Number of xFilter rejection GLOB patterns */
985  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
986  sqlite3_session *p;      /* The open session */
987};
988#endif
989
990/*
991** Shell output mode information from before ".explain on",
992** saved so that it can be restored by ".explain off"
993*/
994typedef struct SavedModeInfo SavedModeInfo;
995struct SavedModeInfo {
996  int valid;          /* Is there legit data in here? */
997  int mode;           /* Mode prior to ".explain on" */
998  int showHeader;     /* The ".header" setting prior to ".explain on" */
999  int colWidth[100];  /* Column widths prior to ".explain on" */
1000};
1001
1002typedef struct ExpertInfo ExpertInfo;
1003struct ExpertInfo {
1004  sqlite3expert *pExpert;
1005  int bVerbose;
1006};
1007
1008/*
1009** State information about the database connection is contained in an
1010** instance of the following structure.
1011*/
1012typedef struct ShellState ShellState;
1013struct ShellState {
1014  sqlite3 *db;           /* The database */
1015  u8 autoExplain;        /* Automatically turn on .explain mode */
1016  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1017  u8 statsOn;            /* True to display memory stats before each finalize */
1018  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1019  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1020  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1021  int outCount;          /* Revert to stdout when reaching zero */
1022  int cnt;               /* Number of records displayed so far */
1023  FILE *out;             /* Write results here */
1024  FILE *traceOut;        /* Output for sqlite3_trace() */
1025  int nErr;              /* Number of errors seen */
1026  int mode;              /* An output mode setting */
1027  int modePrior;         /* Saved mode */
1028  int cMode;             /* temporary output mode for the current query */
1029  int normalMode;        /* Output mode before ".explain on" */
1030  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1031  int showHeader;        /* True to show column names in List or Column mode */
1032  int nCheck;            /* Number of ".check" commands run */
1033  unsigned shellFlgs;    /* Various flags */
1034  char *zDestTable;      /* Name of destination table when MODE_Insert */
1035  char *zTempFile;       /* Temporary file that might need deleting */
1036  char zTestcase[30];    /* Name of current test case */
1037  char colSeparator[20]; /* Column separator character for several modes */
1038  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1039  char colSepPrior[20];  /* Saved column separator */
1040  char rowSepPrior[20];  /* Saved row separator */
1041  int colWidth[100];     /* Requested width of each column when in column mode*/
1042  int actualWidth[100];  /* Actual width of each column */
1043  char nullValue[20];    /* The text to print when a NULL comes back from
1044                         ** the database */
1045  char outfile[FILENAME_MAX]; /* Filename for *out */
1046  const char *zDbFilename;    /* name of the database file */
1047  char *zFreeOnClose;         /* Filename to free when closing */
1048  const char *zVfs;           /* Name of VFS to use */
1049  sqlite3_stmt *pStmt;   /* Current statement if any. */
1050  FILE *pLog;            /* Write log output here */
1051  int *aiIndent;         /* Array of indents used in MODE_Explain */
1052  int nIndent;           /* Size of array aiIndent[] */
1053  int iIndent;           /* Index of current op in aiIndent[] */
1054#if defined(SQLITE_ENABLE_SESSION)
1055  int nSession;             /* Number of active sessions */
1056  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1057#endif
1058  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1059};
1060
1061
1062/* Allowed values for ShellState.autoEQP
1063*/
1064#define AUTOEQP_off      0
1065#define AUTOEQP_on       1
1066#define AUTOEQP_trigger  2
1067#define AUTOEQP_full     3
1068
1069/* Allowed values for ShellState.openMode
1070*/
1071#define SHELL_OPEN_UNSPEC     0      /* No open-mode specified */
1072#define SHELL_OPEN_NORMAL     1      /* Normal database file */
1073#define SHELL_OPEN_APPENDVFS  2      /* Use appendvfs */
1074#define SHELL_OPEN_ZIPFILE    3      /* Use the zipfile virtual table */
1075#define SHELL_OPEN_READONLY   4      /* Open a normal database read-only */
1076
1077/*
1078** These are the allowed shellFlgs values
1079*/
1080#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1081#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1082#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1083#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1084#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1085#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1086#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1087
1088/*
1089** Macros for testing and setting shellFlgs
1090*/
1091#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1092#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1093#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1094
1095/*
1096** These are the allowed modes.
1097*/
1098#define MODE_Line     0  /* One column per line.  Blank line between records */
1099#define MODE_Column   1  /* One record per line in neat columns */
1100#define MODE_List     2  /* One record per line with a separator */
1101#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1102#define MODE_Html     4  /* Generate an XHTML table */
1103#define MODE_Insert   5  /* Generate SQL "insert" statements */
1104#define MODE_Quote    6  /* Quote values as for SQL */
1105#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1106#define MODE_Csv      8  /* Quote strings, numbers are plain */
1107#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1108#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1109#define MODE_Pretty  11  /* Pretty-print schemas */
1110
1111static const char *modeDescr[] = {
1112  "line",
1113  "column",
1114  "list",
1115  "semi",
1116  "html",
1117  "insert",
1118  "quote",
1119  "tcl",
1120  "csv",
1121  "explain",
1122  "ascii",
1123  "prettyprint",
1124};
1125
1126/*
1127** These are the column/row/line separators used by the various
1128** import/export modes.
1129*/
1130#define SEP_Column    "|"
1131#define SEP_Row       "\n"
1132#define SEP_Tab       "\t"
1133#define SEP_Space     " "
1134#define SEP_Comma     ","
1135#define SEP_CrLf      "\r\n"
1136#define SEP_Unit      "\x1F"
1137#define SEP_Record    "\x1E"
1138
1139/*
1140** A callback for the sqlite3_log() interface.
1141*/
1142static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1143  ShellState *p = (ShellState*)pArg;
1144  if( p->pLog==0 ) return;
1145  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1146  fflush(p->pLog);
1147}
1148
1149/*
1150** SQL function:  shell_putsnl(X)
1151**
1152** Write the text X to the screen (or whatever output is being directed)
1153** adding a newline at the end, and then return X.
1154*/
1155static void shellPutsFunc(
1156  sqlite3_context *pCtx,
1157  int nVal,
1158  sqlite3_value **apVal
1159){
1160  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1161  (void)nVal;
1162  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1163  sqlite3_result_value(pCtx, apVal[0]);
1164}
1165
1166/*
1167** SQL function:   edit(VALUE)
1168**                 edit(VALUE,EDITOR)
1169**
1170** These steps:
1171**
1172**     (1) Write VALUE into a temporary file.
1173**     (2) Run program EDITOR on that temporary file.
1174**     (3) Read the temporary file back and return its content as the result.
1175**     (4) Delete the temporary file
1176**
1177** If the EDITOR argument is omitted, use the value in the VISUAL
1178** environment variable.  If still there is no EDITOR, through an error.
1179**
1180** Also throw an error if the EDITOR program returns a non-zero exit code.
1181*/
1182#ifndef SQLITE_NOHAVE_SYSTEM
1183static void editFunc(
1184  sqlite3_context *context,
1185  int argc,
1186  sqlite3_value **argv
1187){
1188  const char *zEditor;
1189  char *zTempFile = 0;
1190  sqlite3 *db;
1191  char *zCmd = 0;
1192  int bBin;
1193  int rc;
1194  FILE *f = 0;
1195  sqlite3_int64 sz;
1196  sqlite3_int64 x;
1197  unsigned char *p = 0;
1198
1199  if( argc==2 ){
1200    zEditor = (const char*)sqlite3_value_text(argv[1]);
1201  }else{
1202    zEditor = getenv("VISUAL");
1203  }
1204  if( zEditor==0 ){
1205    sqlite3_result_error(context, "no editor for edit()", -1);
1206    return;
1207  }
1208  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1209    sqlite3_result_error(context, "NULL input to edit()", -1);
1210    return;
1211  }
1212  db = sqlite3_context_db_handle(context);
1213  zTempFile = 0;
1214  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1215  if( zTempFile==0 ){
1216    sqlite3_uint64 r = 0;
1217    sqlite3_randomness(sizeof(r), &r);
1218    zTempFile = sqlite3_mprintf("temp%llx", r);
1219    if( zTempFile==0 ){
1220      sqlite3_result_error_nomem(context);
1221      return;
1222    }
1223  }
1224  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1225  f = fopen(zTempFile, bBin ? "wb" : "w");
1226  if( f==0 ){
1227    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1228    goto edit_func_end;
1229  }
1230  sz = sqlite3_value_bytes(argv[0]);
1231  if( bBin ){
1232    x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
1233  }else{
1234    x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
1235  }
1236  fclose(f);
1237  f = 0;
1238  if( x!=sz ){
1239    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1240    goto edit_func_end;
1241  }
1242  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1243  if( zCmd==0 ){
1244    sqlite3_result_error_nomem(context);
1245    goto edit_func_end;
1246  }
1247  rc = system(zCmd);
1248  sqlite3_free(zCmd);
1249  if( rc ){
1250    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1251    goto edit_func_end;
1252  }
1253  f = fopen(zTempFile, bBin ? "rb" : "r");
1254  if( f==0 ){
1255    sqlite3_result_error(context,
1256      "edit() cannot reopen temp file after edit", -1);
1257    goto edit_func_end;
1258  }
1259  fseek(f, 0, SEEK_END);
1260  sz = ftell(f);
1261  rewind(f);
1262  p = sqlite3_malloc64( sz+(bBin==0) );
1263  if( p==0 ){
1264    sqlite3_result_error_nomem(context);
1265    goto edit_func_end;
1266  }
1267  if( bBin ){
1268    x = fread(p, 1, sz, f);
1269  }else{
1270    x = fread(p, 1, sz, f);
1271    p[sz] = 0;
1272  }
1273  fclose(f);
1274  f = 0;
1275  if( x!=sz ){
1276    sqlite3_result_error(context, "could not read back the whole file", -1);
1277    goto edit_func_end;
1278  }
1279  if( bBin ){
1280    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1281  }else{
1282    sqlite3_result_text64(context, (const char*)p, sz,
1283                          sqlite3_free, SQLITE_UTF8);
1284  }
1285  p = 0;
1286
1287edit_func_end:
1288  if( f ) fclose(f);
1289  unlink(zTempFile);
1290  sqlite3_free(zTempFile);
1291  sqlite3_free(p);
1292}
1293#endif /* SQLITE_NOHAVE_SYSTEM */
1294
1295/*
1296** Save or restore the current output mode
1297*/
1298static void outputModePush(ShellState *p){
1299  p->modePrior = p->mode;
1300  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1301  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1302}
1303static void outputModePop(ShellState *p){
1304  p->mode = p->modePrior;
1305  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1306  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1307}
1308
1309/*
1310** Output the given string as a hex-encoded blob (eg. X'1234' )
1311*/
1312static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1313  int i;
1314  char *zBlob = (char *)pBlob;
1315  raw_printf(out,"X'");
1316  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1317  raw_printf(out,"'");
1318}
1319
1320/*
1321** Find a string that is not found anywhere in z[].  Return a pointer
1322** to that string.
1323**
1324** Try to use zA and zB first.  If both of those are already found in z[]
1325** then make up some string and store it in the buffer zBuf.
1326*/
1327static const char *unused_string(
1328  const char *z,                    /* Result must not appear anywhere in z */
1329  const char *zA, const char *zB,   /* Try these first */
1330  char *zBuf                        /* Space to store a generated string */
1331){
1332  unsigned i = 0;
1333  if( strstr(z, zA)==0 ) return zA;
1334  if( strstr(z, zB)==0 ) return zB;
1335  do{
1336    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1337  }while( strstr(z,zBuf)!=0 );
1338  return zBuf;
1339}
1340
1341/*
1342** Output the given string as a quoted string using SQL quoting conventions.
1343**
1344** See also: output_quoted_escaped_string()
1345*/
1346static void output_quoted_string(FILE *out, const char *z){
1347  int i;
1348  char c;
1349  setBinaryMode(out, 1);
1350  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1351  if( c==0 ){
1352    utf8_printf(out,"'%s'",z);
1353  }else{
1354    raw_printf(out, "'");
1355    while( *z ){
1356      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1357      if( c=='\'' ) i++;
1358      if( i ){
1359        utf8_printf(out, "%.*s", i, z);
1360        z += i;
1361      }
1362      if( c=='\'' ){
1363        raw_printf(out, "'");
1364        continue;
1365      }
1366      if( c==0 ){
1367        break;
1368      }
1369      z++;
1370    }
1371    raw_printf(out, "'");
1372  }
1373  setTextMode(out, 1);
1374}
1375
1376/*
1377** Output the given string as a quoted string using SQL quoting conventions.
1378** Additionallly , escape the "\n" and "\r" characters so that they do not
1379** get corrupted by end-of-line translation facilities in some operating
1380** systems.
1381**
1382** This is like output_quoted_string() but with the addition of the \r\n
1383** escape mechanism.
1384*/
1385static void output_quoted_escaped_string(FILE *out, const char *z){
1386  int i;
1387  char c;
1388  setBinaryMode(out, 1);
1389  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1390  if( c==0 ){
1391    utf8_printf(out,"'%s'",z);
1392  }else{
1393    const char *zNL = 0;
1394    const char *zCR = 0;
1395    int nNL = 0;
1396    int nCR = 0;
1397    char zBuf1[20], zBuf2[20];
1398    for(i=0; z[i]; i++){
1399      if( z[i]=='\n' ) nNL++;
1400      if( z[i]=='\r' ) nCR++;
1401    }
1402    if( nNL ){
1403      raw_printf(out, "replace(");
1404      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1405    }
1406    if( nCR ){
1407      raw_printf(out, "replace(");
1408      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1409    }
1410    raw_printf(out, "'");
1411    while( *z ){
1412      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1413      if( c=='\'' ) i++;
1414      if( i ){
1415        utf8_printf(out, "%.*s", i, z);
1416        z += i;
1417      }
1418      if( c=='\'' ){
1419        raw_printf(out, "'");
1420        continue;
1421      }
1422      if( c==0 ){
1423        break;
1424      }
1425      z++;
1426      if( c=='\n' ){
1427        raw_printf(out, "%s", zNL);
1428        continue;
1429      }
1430      raw_printf(out, "%s", zCR);
1431    }
1432    raw_printf(out, "'");
1433    if( nCR ){
1434      raw_printf(out, ",'%s',char(13))", zCR);
1435    }
1436    if( nNL ){
1437      raw_printf(out, ",'%s',char(10))", zNL);
1438    }
1439  }
1440  setTextMode(out, 1);
1441}
1442
1443/*
1444** Output the given string as a quoted according to C or TCL quoting rules.
1445*/
1446static void output_c_string(FILE *out, const char *z){
1447  unsigned int c;
1448  fputc('"', out);
1449  while( (c = *(z++))!=0 ){
1450    if( c=='\\' ){
1451      fputc(c, out);
1452      fputc(c, out);
1453    }else if( c=='"' ){
1454      fputc('\\', out);
1455      fputc('"', out);
1456    }else if( c=='\t' ){
1457      fputc('\\', out);
1458      fputc('t', out);
1459    }else if( c=='\n' ){
1460      fputc('\\', out);
1461      fputc('n', out);
1462    }else if( c=='\r' ){
1463      fputc('\\', out);
1464      fputc('r', out);
1465    }else if( !isprint(c&0xff) ){
1466      raw_printf(out, "\\%03o", c&0xff);
1467    }else{
1468      fputc(c, out);
1469    }
1470  }
1471  fputc('"', out);
1472}
1473
1474/*
1475** Output the given string with characters that are special to
1476** HTML escaped.
1477*/
1478static void output_html_string(FILE *out, const char *z){
1479  int i;
1480  if( z==0 ) z = "";
1481  while( *z ){
1482    for(i=0;   z[i]
1483            && z[i]!='<'
1484            && z[i]!='&'
1485            && z[i]!='>'
1486            && z[i]!='\"'
1487            && z[i]!='\'';
1488        i++){}
1489    if( i>0 ){
1490      utf8_printf(out,"%.*s",i,z);
1491    }
1492    if( z[i]=='<' ){
1493      raw_printf(out,"&lt;");
1494    }else if( z[i]=='&' ){
1495      raw_printf(out,"&amp;");
1496    }else if( z[i]=='>' ){
1497      raw_printf(out,"&gt;");
1498    }else if( z[i]=='\"' ){
1499      raw_printf(out,"&quot;");
1500    }else if( z[i]=='\'' ){
1501      raw_printf(out,"&#39;");
1502    }else{
1503      break;
1504    }
1505    z += i + 1;
1506  }
1507}
1508
1509/*
1510** If a field contains any character identified by a 1 in the following
1511** array, then the string must be quoted for CSV.
1512*/
1513static const char needCsvQuote[] = {
1514  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1515  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1516  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1517  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1518  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1519  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1520  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1521  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1522  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1523  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1524  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1525  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1526  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1527  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1528  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1529  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1530};
1531
1532/*
1533** Output a single term of CSV.  Actually, p->colSeparator is used for
1534** the separator, which may or may not be a comma.  p->nullValue is
1535** the null value.  Strings are quoted if necessary.  The separator
1536** is only issued if bSep is true.
1537*/
1538static void output_csv(ShellState *p, const char *z, int bSep){
1539  FILE *out = p->out;
1540  if( z==0 ){
1541    utf8_printf(out,"%s",p->nullValue);
1542  }else{
1543    int i;
1544    int nSep = strlen30(p->colSeparator);
1545    for(i=0; z[i]; i++){
1546      if( needCsvQuote[((unsigned char*)z)[i]]
1547         || (z[i]==p->colSeparator[0] &&
1548             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1549        i = 0;
1550        break;
1551      }
1552    }
1553    if( i==0 ){
1554      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1555      utf8_printf(out, "%s", zQuoted);
1556      sqlite3_free(zQuoted);
1557    }else{
1558      utf8_printf(out, "%s", z);
1559    }
1560  }
1561  if( bSep ){
1562    utf8_printf(p->out, "%s", p->colSeparator);
1563  }
1564}
1565
1566/*
1567** This routine runs when the user presses Ctrl-C
1568*/
1569static void interrupt_handler(int NotUsed){
1570  UNUSED_PARAMETER(NotUsed);
1571  seenInterrupt++;
1572  if( seenInterrupt>2 ) exit(1);
1573  if( globalDb ) sqlite3_interrupt(globalDb);
1574}
1575
1576#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1577/*
1578** This routine runs for console events (e.g. Ctrl-C) on Win32
1579*/
1580static BOOL WINAPI ConsoleCtrlHandler(
1581  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1582){
1583  if( dwCtrlType==CTRL_C_EVENT ){
1584    interrupt_handler(0);
1585    return TRUE;
1586  }
1587  return FALSE;
1588}
1589#endif
1590
1591#ifndef SQLITE_OMIT_AUTHORIZATION
1592/*
1593** When the ".auth ON" is set, the following authorizer callback is
1594** invoked.  It always returns SQLITE_OK.
1595*/
1596static int shellAuth(
1597  void *pClientData,
1598  int op,
1599  const char *zA1,
1600  const char *zA2,
1601  const char *zA3,
1602  const char *zA4
1603){
1604  ShellState *p = (ShellState*)pClientData;
1605  static const char *azAction[] = { 0,
1606     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1607     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1608     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1609     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1610     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1611     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1612     "PRAGMA",               "READ",                 "SELECT",
1613     "TRANSACTION",          "UPDATE",               "ATTACH",
1614     "DETACH",               "ALTER_TABLE",          "REINDEX",
1615     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1616     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1617  };
1618  int i;
1619  const char *az[4];
1620  az[0] = zA1;
1621  az[1] = zA2;
1622  az[2] = zA3;
1623  az[3] = zA4;
1624  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1625  for(i=0; i<4; i++){
1626    raw_printf(p->out, " ");
1627    if( az[i] ){
1628      output_c_string(p->out, az[i]);
1629    }else{
1630      raw_printf(p->out, "NULL");
1631    }
1632  }
1633  raw_printf(p->out, "\n");
1634  return SQLITE_OK;
1635}
1636#endif
1637
1638/*
1639** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1640**
1641** This routine converts some CREATE TABLE statements for shadow tables
1642** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1643*/
1644static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1645  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1646    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1647  }else{
1648    utf8_printf(out, "%s%s", z, zTail);
1649  }
1650}
1651static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1652  char c = z[n];
1653  z[n] = 0;
1654  printSchemaLine(out, z, zTail);
1655  z[n] = c;
1656}
1657
1658/*
1659** Return true if string z[] has nothing but whitespace and comments to the
1660** end of the first line.
1661*/
1662static int wsToEol(const char *z){
1663  int i;
1664  for(i=0; z[i]; i++){
1665    if( z[i]=='\n' ) return 1;
1666    if( IsSpace(z[i]) ) continue;
1667    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1668    return 0;
1669  }
1670  return 1;
1671}
1672
1673
1674/*
1675** This is the callback routine that the shell
1676** invokes for each row of a query result.
1677*/
1678static int shell_callback(
1679  void *pArg,
1680  int nArg,        /* Number of result columns */
1681  char **azArg,    /* Text of each result column */
1682  char **azCol,    /* Column names */
1683  int *aiType      /* Column types */
1684){
1685  int i;
1686  ShellState *p = (ShellState*)pArg;
1687
1688  if( azArg==0 ) return 0;
1689  switch( p->cMode ){
1690    case MODE_Line: {
1691      int w = 5;
1692      if( azArg==0 ) break;
1693      for(i=0; i<nArg; i++){
1694        int len = strlen30(azCol[i] ? azCol[i] : "");
1695        if( len>w ) w = len;
1696      }
1697      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
1698      for(i=0; i<nArg; i++){
1699        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
1700                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1701      }
1702      break;
1703    }
1704    case MODE_Explain:
1705    case MODE_Column: {
1706      static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
1707      const int *colWidth;
1708      int showHdr;
1709      char *rowSep;
1710      if( p->cMode==MODE_Column ){
1711        colWidth = p->colWidth;
1712        showHdr = p->showHeader;
1713        rowSep = p->rowSeparator;
1714      }else{
1715        colWidth = aExplainWidths;
1716        showHdr = 1;
1717        rowSep = SEP_Row;
1718      }
1719      if( p->cnt++==0 ){
1720        for(i=0; i<nArg; i++){
1721          int w, n;
1722          if( i<ArraySize(p->colWidth) ){
1723            w = colWidth[i];
1724          }else{
1725            w = 0;
1726          }
1727          if( w==0 ){
1728            w = strlenChar(azCol[i] ? azCol[i] : "");
1729            if( w<10 ) w = 10;
1730            n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
1731            if( w<n ) w = n;
1732          }
1733          if( i<ArraySize(p->actualWidth) ){
1734            p->actualWidth[i] = w;
1735          }
1736          if( showHdr ){
1737            utf8_width_print(p->out, w, azCol[i]);
1738            utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
1739          }
1740        }
1741        if( showHdr ){
1742          for(i=0; i<nArg; i++){
1743            int w;
1744            if( i<ArraySize(p->actualWidth) ){
1745               w = p->actualWidth[i];
1746               if( w<0 ) w = -w;
1747            }else{
1748               w = 10;
1749            }
1750            utf8_printf(p->out,"%-*.*s%s",w,w,
1751                   "----------------------------------------------------------"
1752                   "----------------------------------------------------------",
1753                    i==nArg-1 ? rowSep : "  ");
1754          }
1755        }
1756      }
1757      if( azArg==0 ) break;
1758      for(i=0; i<nArg; i++){
1759        int w;
1760        if( i<ArraySize(p->actualWidth) ){
1761           w = p->actualWidth[i];
1762        }else{
1763           w = 10;
1764        }
1765        if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
1766          w = strlenChar(azArg[i]);
1767        }
1768        if( i==1 && p->aiIndent && p->pStmt ){
1769          if( p->iIndent<p->nIndent ){
1770            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
1771          }
1772          p->iIndent++;
1773        }
1774        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
1775        utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
1776      }
1777      break;
1778    }
1779    case MODE_Semi: {   /* .schema and .fullschema output */
1780      printSchemaLine(p->out, azArg[0], ";\n");
1781      break;
1782    }
1783    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
1784      char *z;
1785      int j;
1786      int nParen = 0;
1787      char cEnd = 0;
1788      char c;
1789      int nLine = 0;
1790      assert( nArg==1 );
1791      if( azArg[0]==0 ) break;
1792      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
1793       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
1794      ){
1795        utf8_printf(p->out, "%s;\n", azArg[0]);
1796        break;
1797      }
1798      z = sqlite3_mprintf("%s", azArg[0]);
1799      j = 0;
1800      for(i=0; IsSpace(z[i]); i++){}
1801      for(; (c = z[i])!=0; i++){
1802        if( IsSpace(c) ){
1803          if( z[j-1]=='\r' ) z[j-1] = '\n';
1804          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
1805        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
1806          j--;
1807        }
1808        z[j++] = c;
1809      }
1810      while( j>0 && IsSpace(z[j-1]) ){ j--; }
1811      z[j] = 0;
1812      if( strlen30(z)>=79 ){
1813        for(i=j=0; (c = z[i])!=0; i++){  /* Copy changes from z[i] back to z[j] */
1814          if( c==cEnd ){
1815            cEnd = 0;
1816          }else if( c=='"' || c=='\'' || c=='`' ){
1817            cEnd = c;
1818          }else if( c=='[' ){
1819            cEnd = ']';
1820          }else if( c=='-' && z[i+1]=='-' ){
1821            cEnd = '\n';
1822          }else if( c=='(' ){
1823            nParen++;
1824          }else if( c==')' ){
1825            nParen--;
1826            if( nLine>0 && nParen==0 && j>0 ){
1827              printSchemaLineN(p->out, z, j, "\n");
1828              j = 0;
1829            }
1830          }
1831          z[j++] = c;
1832          if( nParen==1 && cEnd==0
1833           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
1834          ){
1835            if( c=='\n' ) j--;
1836            printSchemaLineN(p->out, z, j, "\n  ");
1837            j = 0;
1838            nLine++;
1839            while( IsSpace(z[i+1]) ){ i++; }
1840          }
1841        }
1842        z[j] = 0;
1843      }
1844      printSchemaLine(p->out, z, ";\n");
1845      sqlite3_free(z);
1846      break;
1847    }
1848    case MODE_List: {
1849      if( p->cnt++==0 && p->showHeader ){
1850        for(i=0; i<nArg; i++){
1851          utf8_printf(p->out,"%s%s",azCol[i],
1852                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
1853        }
1854      }
1855      if( azArg==0 ) break;
1856      for(i=0; i<nArg; i++){
1857        char *z = azArg[i];
1858        if( z==0 ) z = p->nullValue;
1859        utf8_printf(p->out, "%s", z);
1860        if( i<nArg-1 ){
1861          utf8_printf(p->out, "%s", p->colSeparator);
1862        }else{
1863          utf8_printf(p->out, "%s", p->rowSeparator);
1864        }
1865      }
1866      break;
1867    }
1868    case MODE_Html: {
1869      if( p->cnt++==0 && p->showHeader ){
1870        raw_printf(p->out,"<TR>");
1871        for(i=0; i<nArg; i++){
1872          raw_printf(p->out,"<TH>");
1873          output_html_string(p->out, azCol[i]);
1874          raw_printf(p->out,"</TH>\n");
1875        }
1876        raw_printf(p->out,"</TR>\n");
1877      }
1878      if( azArg==0 ) break;
1879      raw_printf(p->out,"<TR>");
1880      for(i=0; i<nArg; i++){
1881        raw_printf(p->out,"<TD>");
1882        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1883        raw_printf(p->out,"</TD>\n");
1884      }
1885      raw_printf(p->out,"</TR>\n");
1886      break;
1887    }
1888    case MODE_Tcl: {
1889      if( p->cnt++==0 && p->showHeader ){
1890        for(i=0; i<nArg; i++){
1891          output_c_string(p->out,azCol[i] ? azCol[i] : "");
1892          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1893        }
1894        utf8_printf(p->out, "%s", p->rowSeparator);
1895      }
1896      if( azArg==0 ) break;
1897      for(i=0; i<nArg; i++){
1898        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1899        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1900      }
1901      utf8_printf(p->out, "%s", p->rowSeparator);
1902      break;
1903    }
1904    case MODE_Csv: {
1905      setBinaryMode(p->out, 1);
1906      if( p->cnt++==0 && p->showHeader ){
1907        for(i=0; i<nArg; i++){
1908          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
1909        }
1910        utf8_printf(p->out, "%s", p->rowSeparator);
1911      }
1912      if( nArg>0 ){
1913        for(i=0; i<nArg; i++){
1914          output_csv(p, azArg[i], i<nArg-1);
1915        }
1916        utf8_printf(p->out, "%s", p->rowSeparator);
1917      }
1918      setTextMode(p->out, 1);
1919      break;
1920    }
1921    case MODE_Insert: {
1922      if( azArg==0 ) break;
1923      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
1924      if( p->showHeader ){
1925        raw_printf(p->out,"(");
1926        for(i=0; i<nArg; i++){
1927          if( i>0 ) raw_printf(p->out, ",");
1928          if( quoteChar(azCol[i]) ){
1929            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
1930            utf8_printf(p->out, "%s", z);
1931            sqlite3_free(z);
1932          }else{
1933            raw_printf(p->out, "%s", azCol[i]);
1934          }
1935        }
1936        raw_printf(p->out,")");
1937      }
1938      p->cnt++;
1939      for(i=0; i<nArg; i++){
1940        raw_printf(p->out, i>0 ? "," : " VALUES(");
1941        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1942          utf8_printf(p->out,"NULL");
1943        }else if( aiType && aiType[i]==SQLITE_TEXT ){
1944          if( ShellHasFlag(p, SHFLG_Newlines) ){
1945            output_quoted_string(p->out, azArg[i]);
1946          }else{
1947            output_quoted_escaped_string(p->out, azArg[i]);
1948          }
1949        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1950          utf8_printf(p->out,"%s", azArg[i]);
1951        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1952          char z[50];
1953          double r = sqlite3_column_double(p->pStmt, i);
1954          sqlite3_snprintf(50,z,"%!.20g", r);
1955          raw_printf(p->out, "%s", z);
1956        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1957          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1958          int nBlob = sqlite3_column_bytes(p->pStmt, i);
1959          output_hex_blob(p->out, pBlob, nBlob);
1960        }else if( isNumber(azArg[i], 0) ){
1961          utf8_printf(p->out,"%s", azArg[i]);
1962        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
1963          output_quoted_string(p->out, azArg[i]);
1964        }else{
1965          output_quoted_escaped_string(p->out, azArg[i]);
1966        }
1967      }
1968      raw_printf(p->out,");\n");
1969      break;
1970    }
1971    case MODE_Quote: {
1972      if( azArg==0 ) break;
1973      if( p->cnt==0 && p->showHeader ){
1974        for(i=0; i<nArg; i++){
1975          if( i>0 ) raw_printf(p->out, ",");
1976          output_quoted_string(p->out, azCol[i]);
1977        }
1978        raw_printf(p->out,"\n");
1979      }
1980      p->cnt++;
1981      for(i=0; i<nArg; i++){
1982        if( i>0 ) raw_printf(p->out, ",");
1983        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1984          utf8_printf(p->out,"NULL");
1985        }else if( aiType && aiType[i]==SQLITE_TEXT ){
1986          output_quoted_string(p->out, azArg[i]);
1987        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1988          utf8_printf(p->out,"%s", azArg[i]);
1989        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1990          char z[50];
1991          double r = sqlite3_column_double(p->pStmt, i);
1992          sqlite3_snprintf(50,z,"%!.20g", r);
1993          raw_printf(p->out, "%s", z);
1994        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1995          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1996          int nBlob = sqlite3_column_bytes(p->pStmt, i);
1997          output_hex_blob(p->out, pBlob, nBlob);
1998        }else if( isNumber(azArg[i], 0) ){
1999          utf8_printf(p->out,"%s", azArg[i]);
2000        }else{
2001          output_quoted_string(p->out, azArg[i]);
2002        }
2003      }
2004      raw_printf(p->out,"\n");
2005      break;
2006    }
2007    case MODE_Ascii: {
2008      if( p->cnt++==0 && p->showHeader ){
2009        for(i=0; i<nArg; i++){
2010          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2011          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2012        }
2013        utf8_printf(p->out, "%s", p->rowSeparator);
2014      }
2015      if( azArg==0 ) break;
2016      for(i=0; i<nArg; i++){
2017        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2018        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2019      }
2020      utf8_printf(p->out, "%s", p->rowSeparator);
2021      break;
2022    }
2023  }
2024  return 0;
2025}
2026
2027/*
2028** This is the callback routine that the SQLite library
2029** invokes for each row of a query result.
2030*/
2031static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2032  /* since we don't have type info, call the shell_callback with a NULL value */
2033  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2034}
2035
2036/*
2037** This is the callback routine from sqlite3_exec() that appends all
2038** output onto the end of a ShellText object.
2039*/
2040static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2041  ShellText *p = (ShellText*)pArg;
2042  int i;
2043  UNUSED_PARAMETER(az);
2044  if( azArg==0 ) return 0;
2045  if( p->n ) appendText(p, "|", 0);
2046  for(i=0; i<nArg; i++){
2047    if( i ) appendText(p, ",", 0);
2048    if( azArg[i] ) appendText(p, azArg[i], 0);
2049  }
2050  return 0;
2051}
2052
2053/*
2054** Generate an appropriate SELFTEST table in the main database.
2055*/
2056static void createSelftestTable(ShellState *p){
2057  char *zErrMsg = 0;
2058  sqlite3_exec(p->db,
2059    "SAVEPOINT selftest_init;\n"
2060    "CREATE TABLE IF NOT EXISTS selftest(\n"
2061    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2062    "  op TEXT,\n"                   /* Operator:  memo run */
2063    "  cmd TEXT,\n"                  /* Command text */
2064    "  ans TEXT\n"                   /* Desired answer */
2065    ");"
2066    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2067    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2068    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2069    "         'memo','Tests generated by --init');\n"
2070    "INSERT INTO [_shell$self]\n"
2071    "  SELECT 'run',\n"
2072    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2073                                 "FROM sqlite_master ORDER BY 2'',224))',\n"
2074    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2075                          "FROM sqlite_master ORDER BY 2',224));\n"
2076    "INSERT INTO [_shell$self]\n"
2077    "  SELECT 'run',"
2078    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2079    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2080    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2081    "  FROM (\n"
2082    "    SELECT name FROM sqlite_master\n"
2083    "     WHERE type='table'\n"
2084    "       AND name<>'selftest'\n"
2085    "       AND coalesce(rootpage,0)>0\n"
2086    "  )\n"
2087    " ORDER BY name;\n"
2088    "INSERT INTO [_shell$self]\n"
2089    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2090    "INSERT INTO selftest(tno,op,cmd,ans)"
2091    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2092    "DROP TABLE [_shell$self];"
2093    ,0,0,&zErrMsg);
2094  if( zErrMsg ){
2095    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2096    sqlite3_free(zErrMsg);
2097  }
2098  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2099}
2100
2101
2102/*
2103** Set the destination table field of the ShellState structure to
2104** the name of the table given.  Escape any quote characters in the
2105** table name.
2106*/
2107static void set_table_name(ShellState *p, const char *zName){
2108  int i, n;
2109  char cQuote;
2110  char *z;
2111
2112  if( p->zDestTable ){
2113    free(p->zDestTable);
2114    p->zDestTable = 0;
2115  }
2116  if( zName==0 ) return;
2117  cQuote = quoteChar(zName);
2118  n = strlen30(zName);
2119  if( cQuote ) n += n+2;
2120  z = p->zDestTable = malloc( n+1 );
2121  if( z==0 ){
2122    raw_printf(stderr,"Error: out of memory\n");
2123    exit(1);
2124  }
2125  n = 0;
2126  if( cQuote ) z[n++] = cQuote;
2127  for(i=0; zName[i]; i++){
2128    z[n++] = zName[i];
2129    if( zName[i]==cQuote ) z[n++] = cQuote;
2130  }
2131  if( cQuote ) z[n++] = cQuote;
2132  z[n] = 0;
2133}
2134
2135
2136/*
2137** Execute a query statement that will generate SQL output.  Print
2138** the result columns, comma-separated, on a line and then add a
2139** semicolon terminator to the end of that line.
2140**
2141** If the number of columns is 1 and that column contains text "--"
2142** then write the semicolon on a separate line.  That way, if a
2143** "--" comment occurs at the end of the statement, the comment
2144** won't consume the semicolon terminator.
2145*/
2146static int run_table_dump_query(
2147  ShellState *p,           /* Query context */
2148  const char *zSelect,     /* SELECT statement to extract content */
2149  const char *zFirstRow    /* Print before first row, if not NULL */
2150){
2151  sqlite3_stmt *pSelect;
2152  int rc;
2153  int nResult;
2154  int i;
2155  const char *z;
2156  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2157  if( rc!=SQLITE_OK || !pSelect ){
2158    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2159                sqlite3_errmsg(p->db));
2160    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2161    return rc;
2162  }
2163  rc = sqlite3_step(pSelect);
2164  nResult = sqlite3_column_count(pSelect);
2165  while( rc==SQLITE_ROW ){
2166    if( zFirstRow ){
2167      utf8_printf(p->out, "%s", zFirstRow);
2168      zFirstRow = 0;
2169    }
2170    z = (const char*)sqlite3_column_text(pSelect, 0);
2171    utf8_printf(p->out, "%s", z);
2172    for(i=1; i<nResult; i++){
2173      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2174    }
2175    if( z==0 ) z = "";
2176    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2177    if( z[0] ){
2178      raw_printf(p->out, "\n;\n");
2179    }else{
2180      raw_printf(p->out, ";\n");
2181    }
2182    rc = sqlite3_step(pSelect);
2183  }
2184  rc = sqlite3_finalize(pSelect);
2185  if( rc!=SQLITE_OK ){
2186    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2187                sqlite3_errmsg(p->db));
2188    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2189  }
2190  return rc;
2191}
2192
2193/*
2194** Allocate space and save off current error string.
2195*/
2196static char *save_err_msg(
2197  sqlite3 *db            /* Database to query */
2198){
2199  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2200  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2201  if( zErrMsg ){
2202    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2203  }
2204  return zErrMsg;
2205}
2206
2207#ifdef __linux__
2208/*
2209** Attempt to display I/O stats on Linux using /proc/PID/io
2210*/
2211static void displayLinuxIoStats(FILE *out){
2212  FILE *in;
2213  char z[200];
2214  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2215  in = fopen(z, "rb");
2216  if( in==0 ) return;
2217  while( fgets(z, sizeof(z), in)!=0 ){
2218    static const struct {
2219      const char *zPattern;
2220      const char *zDesc;
2221    } aTrans[] = {
2222      { "rchar: ",                  "Bytes received by read():" },
2223      { "wchar: ",                  "Bytes sent to write():"    },
2224      { "syscr: ",                  "Read() system calls:"      },
2225      { "syscw: ",                  "Write() system calls:"     },
2226      { "read_bytes: ",             "Bytes read from storage:"  },
2227      { "write_bytes: ",            "Bytes written to storage:" },
2228      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2229    };
2230    int i;
2231    for(i=0; i<ArraySize(aTrans); i++){
2232      int n = strlen30(aTrans[i].zPattern);
2233      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2234        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2235        break;
2236      }
2237    }
2238  }
2239  fclose(in);
2240}
2241#endif
2242
2243/*
2244** Display a single line of status using 64-bit values.
2245*/
2246static void displayStatLine(
2247  ShellState *p,            /* The shell context */
2248  char *zLabel,             /* Label for this one line */
2249  char *zFormat,            /* Format for the result */
2250  int iStatusCtrl,          /* Which status to display */
2251  int bReset                /* True to reset the stats */
2252){
2253  sqlite3_int64 iCur = -1;
2254  sqlite3_int64 iHiwtr = -1;
2255  int i, nPercent;
2256  char zLine[200];
2257  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2258  for(i=0, nPercent=0; zFormat[i]; i++){
2259    if( zFormat[i]=='%' ) nPercent++;
2260  }
2261  if( nPercent>1 ){
2262    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2263  }else{
2264    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2265  }
2266  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2267}
2268
2269/*
2270** Display memory stats.
2271*/
2272static int display_stats(
2273  sqlite3 *db,                /* Database to query */
2274  ShellState *pArg,           /* Pointer to ShellState */
2275  int bReset                  /* True to reset the stats */
2276){
2277  int iCur;
2278  int iHiwtr;
2279  FILE *out;
2280  if( pArg==0 || pArg->out==0 ) return 0;
2281  out = pArg->out;
2282
2283  if( pArg->pStmt && (pArg->statsOn & 2) ){
2284    int nCol, i, x;
2285    sqlite3_stmt *pStmt = pArg->pStmt;
2286    char z[100];
2287    nCol = sqlite3_column_count(pStmt);
2288    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2289    for(i=0; i<nCol; i++){
2290      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2291      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2292#ifndef SQLITE_OMIT_DECLTYPE
2293      sqlite3_snprintf(30, z+x, "declared type:");
2294      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2295#endif
2296#ifdef SQLITE_ENABLE_COLUMN_METADATA
2297      sqlite3_snprintf(30, z+x, "database name:");
2298      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2299      sqlite3_snprintf(30, z+x, "table name:");
2300      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2301      sqlite3_snprintf(30, z+x, "origin name:");
2302      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2303#endif
2304    }
2305  }
2306
2307  displayStatLine(pArg, "Memory Used:",
2308     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2309  displayStatLine(pArg, "Number of Outstanding Allocations:",
2310     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2311  if( pArg->shellFlgs & SHFLG_Pagecache ){
2312    displayStatLine(pArg, "Number of Pcache Pages Used:",
2313       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2314  }
2315  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2316     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2317  displayStatLine(pArg, "Largest Allocation:",
2318     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2319  displayStatLine(pArg, "Largest Pcache Allocation:",
2320     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2321#ifdef YYTRACKMAXSTACKDEPTH
2322  displayStatLine(pArg, "Deepest Parser Stack:",
2323     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2324#endif
2325
2326  if( db ){
2327    if( pArg->shellFlgs & SHFLG_Lookaside ){
2328      iHiwtr = iCur = -1;
2329      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2330                        &iCur, &iHiwtr, bReset);
2331      raw_printf(pArg->out,
2332              "Lookaside Slots Used:                %d (max %d)\n",
2333              iCur, iHiwtr);
2334      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2335                        &iCur, &iHiwtr, bReset);
2336      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2337              iHiwtr);
2338      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2339                        &iCur, &iHiwtr, bReset);
2340      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2341              iHiwtr);
2342      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2343                        &iCur, &iHiwtr, bReset);
2344      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2345              iHiwtr);
2346    }
2347    iHiwtr = iCur = -1;
2348    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2349    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2350            iCur);
2351    iHiwtr = iCur = -1;
2352    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2353    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2354    iHiwtr = iCur = -1;
2355    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2356    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2357    iHiwtr = iCur = -1;
2358    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2359    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2360    iHiwtr = iCur = -1;
2361    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2362    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2363    iHiwtr = iCur = -1;
2364    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2365    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2366            iCur);
2367    iHiwtr = iCur = -1;
2368    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2369    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2370            iCur);
2371  }
2372
2373  if( pArg->pStmt ){
2374    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2375                               bReset);
2376    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2377    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2378    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2379    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2380    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2381    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2382    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2383    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
2384    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2385    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2386    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2387    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2388    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2389  }
2390
2391#ifdef __linux__
2392  displayLinuxIoStats(pArg->out);
2393#endif
2394
2395  /* Do not remove this machine readable comment: extra-stats-output-here */
2396
2397  return 0;
2398}
2399
2400/*
2401** Display scan stats.
2402*/
2403static void display_scanstats(
2404  sqlite3 *db,                    /* Database to query */
2405  ShellState *pArg                /* Pointer to ShellState */
2406){
2407#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2408  UNUSED_PARAMETER(db);
2409  UNUSED_PARAMETER(pArg);
2410#else
2411  int i, k, n, mx;
2412  raw_printf(pArg->out, "-------- scanstats --------\n");
2413  mx = 0;
2414  for(k=0; k<=mx; k++){
2415    double rEstLoop = 1.0;
2416    for(i=n=0; 1; i++){
2417      sqlite3_stmt *p = pArg->pStmt;
2418      sqlite3_int64 nLoop, nVisit;
2419      double rEst;
2420      int iSid;
2421      const char *zExplain;
2422      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2423        break;
2424      }
2425      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2426      if( iSid>mx ) mx = iSid;
2427      if( iSid!=k ) continue;
2428      if( n==0 ){
2429        rEstLoop = (double)nLoop;
2430        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2431      }
2432      n++;
2433      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2434      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2435      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2436      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2437      rEstLoop *= rEst;
2438      raw_printf(pArg->out,
2439          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2440          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2441      );
2442    }
2443  }
2444  raw_printf(pArg->out, "---------------------------\n");
2445#endif
2446}
2447
2448/*
2449** Parameter azArray points to a zero-terminated array of strings. zStr
2450** points to a single nul-terminated string. Return non-zero if zStr
2451** is equal, according to strcmp(), to any of the strings in the array.
2452** Otherwise, return zero.
2453*/
2454static int str_in_array(const char *zStr, const char **azArray){
2455  int i;
2456  for(i=0; azArray[i]; i++){
2457    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2458  }
2459  return 0;
2460}
2461
2462/*
2463** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2464** and populate the ShellState.aiIndent[] array with the number of
2465** spaces each opcode should be indented before it is output.
2466**
2467** The indenting rules are:
2468**
2469**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2470**       all opcodes that occur between the p2 jump destination and the opcode
2471**       itself by 2 spaces.
2472**
2473**     * For each "Goto", if the jump destination is earlier in the program
2474**       and ends on one of:
2475**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2476**       or if the P1 parameter is one instead of zero,
2477**       then indent all opcodes between the earlier instruction
2478**       and "Goto" by 2 spaces.
2479*/
2480static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2481  const char *zSql;               /* The text of the SQL statement */
2482  const char *z;                  /* Used to check if this is an EXPLAIN */
2483  int *abYield = 0;               /* True if op is an OP_Yield */
2484  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2485  int iOp;                        /* Index of operation in p->aiIndent[] */
2486
2487  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
2488                           "NextIfOpen", "PrevIfOpen", 0 };
2489  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2490                            "Rewind", 0 };
2491  const char *azGoto[] = { "Goto", 0 };
2492
2493  /* Try to figure out if this is really an EXPLAIN statement. If this
2494  ** cannot be verified, return early.  */
2495  if( sqlite3_column_count(pSql)!=8 ){
2496    p->cMode = p->mode;
2497    return;
2498  }
2499  zSql = sqlite3_sql(pSql);
2500  if( zSql==0 ) return;
2501  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2502  if( sqlite3_strnicmp(z, "explain", 7) ){
2503    p->cMode = p->mode;
2504    return;
2505  }
2506
2507  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2508    int i;
2509    int iAddr = sqlite3_column_int(pSql, 0);
2510    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2511
2512    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2513    ** p2 is an instruction address, set variable p2op to the index of that
2514    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2515    ** the current instruction is part of a sub-program generated by an
2516    ** SQL trigger or foreign key.  */
2517    int p2 = sqlite3_column_int(pSql, 3);
2518    int p2op = (p2 + (iOp-iAddr));
2519
2520    /* Grow the p->aiIndent array as required */
2521    if( iOp>=nAlloc ){
2522      if( iOp==0 ){
2523        /* Do further verfication that this is explain output.  Abort if
2524        ** it is not */
2525        static const char *explainCols[] = {
2526           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2527        int jj;
2528        for(jj=0; jj<ArraySize(explainCols); jj++){
2529          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2530            p->cMode = p->mode;
2531            sqlite3_reset(pSql);
2532            return;
2533          }
2534        }
2535      }
2536      nAlloc += 100;
2537      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2538      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2539    }
2540    abYield[iOp] = str_in_array(zOp, azYield);
2541    p->aiIndent[iOp] = 0;
2542    p->nIndent = iOp+1;
2543
2544    if( str_in_array(zOp, azNext) ){
2545      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2546    }
2547    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2548     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2549    ){
2550      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2551    }
2552  }
2553
2554  p->iIndent = 0;
2555  sqlite3_free(abYield);
2556  sqlite3_reset(pSql);
2557}
2558
2559/*
2560** Free the array allocated by explain_data_prepare().
2561*/
2562static void explain_data_delete(ShellState *p){
2563  sqlite3_free(p->aiIndent);
2564  p->aiIndent = 0;
2565  p->nIndent = 0;
2566  p->iIndent = 0;
2567}
2568
2569/*
2570** Disable and restore .wheretrace and .selecttrace settings.
2571*/
2572#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2573extern int sqlite3SelectTrace;
2574static int savedSelectTrace;
2575#endif
2576#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2577extern int sqlite3WhereTrace;
2578static int savedWhereTrace;
2579#endif
2580static void disable_debug_trace_modes(void){
2581#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2582  savedSelectTrace = sqlite3SelectTrace;
2583  sqlite3SelectTrace = 0;
2584#endif
2585#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2586  savedWhereTrace = sqlite3WhereTrace;
2587  sqlite3WhereTrace = 0;
2588#endif
2589}
2590static void restore_debug_trace_modes(void){
2591#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2592  sqlite3SelectTrace = savedSelectTrace;
2593#endif
2594#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2595  sqlite3WhereTrace = savedWhereTrace;
2596#endif
2597}
2598
2599/*
2600** Run a prepared statement
2601*/
2602static void exec_prepared_stmt(
2603  ShellState *pArg,                                /* Pointer to ShellState */
2604  sqlite3_stmt *pStmt                              /* Statment to run */
2605){
2606  int rc;
2607
2608  /* perform the first step.  this will tell us if we
2609  ** have a result set or not and how wide it is.
2610  */
2611  rc = sqlite3_step(pStmt);
2612  /* if we have a result set... */
2613  if( SQLITE_ROW == rc ){
2614    /* allocate space for col name ptr, value ptr, and type */
2615    int nCol = sqlite3_column_count(pStmt);
2616    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
2617    if( !pData ){
2618      rc = SQLITE_NOMEM;
2619    }else{
2620      char **azCols = (char **)pData;      /* Names of result columns */
2621      char **azVals = &azCols[nCol];       /* Results */
2622      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
2623      int i, x;
2624      assert(sizeof(int) <= sizeof(char *));
2625      /* save off ptrs to column names */
2626      for(i=0; i<nCol; i++){
2627        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
2628      }
2629      do{
2630        /* extract the data and data types */
2631        for(i=0; i<nCol; i++){
2632          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
2633          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
2634            azVals[i] = "";
2635          }else{
2636            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
2637          }
2638          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
2639            rc = SQLITE_NOMEM;
2640            break; /* from for */
2641          }
2642        } /* end for */
2643
2644        /* if data and types extracted successfully... */
2645        if( SQLITE_ROW == rc ){
2646          /* call the supplied callback with the result row data */
2647          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
2648            rc = SQLITE_ABORT;
2649          }else{
2650            rc = sqlite3_step(pStmt);
2651          }
2652        }
2653      } while( SQLITE_ROW == rc );
2654      sqlite3_free(pData);
2655    }
2656  }
2657}
2658
2659#ifndef SQLITE_OMIT_VIRTUALTABLE
2660/*
2661** This function is called to process SQL if the previous shell command
2662** was ".expert". It passes the SQL in the second argument directly to
2663** the sqlite3expert object.
2664**
2665** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2666** code. In this case, (*pzErr) may be set to point to a buffer containing
2667** an English language error message. It is the responsibility of the
2668** caller to eventually free this buffer using sqlite3_free().
2669*/
2670static int expertHandleSQL(
2671  ShellState *pState,
2672  const char *zSql,
2673  char **pzErr
2674){
2675  assert( pState->expert.pExpert );
2676  assert( pzErr==0 || *pzErr==0 );
2677  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
2678}
2679
2680/*
2681** This function is called either to silently clean up the object
2682** created by the ".expert" command (if bCancel==1), or to generate a
2683** report from it and then clean it up (if bCancel==0).
2684**
2685** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2686** code. In this case, (*pzErr) may be set to point to a buffer containing
2687** an English language error message. It is the responsibility of the
2688** caller to eventually free this buffer using sqlite3_free().
2689*/
2690static int expertFinish(
2691  ShellState *pState,
2692  int bCancel,
2693  char **pzErr
2694){
2695  int rc = SQLITE_OK;
2696  sqlite3expert *p = pState->expert.pExpert;
2697  assert( p );
2698  assert( bCancel || pzErr==0 || *pzErr==0 );
2699  if( bCancel==0 ){
2700    FILE *out = pState->out;
2701    int bVerbose = pState->expert.bVerbose;
2702
2703    rc = sqlite3_expert_analyze(p, pzErr);
2704    if( rc==SQLITE_OK ){
2705      int nQuery = sqlite3_expert_count(p);
2706      int i;
2707
2708      if( bVerbose ){
2709        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
2710        raw_printf(out, "-- Candidates -----------------------------\n");
2711        raw_printf(out, "%s\n", zCand);
2712      }
2713      for(i=0; i<nQuery; i++){
2714        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
2715        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
2716        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
2717        if( zIdx==0 ) zIdx = "(no new indexes)\n";
2718        if( bVerbose ){
2719          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
2720          raw_printf(out, "%s\n\n", zSql);
2721        }
2722        raw_printf(out, "%s\n", zIdx);
2723        raw_printf(out, "%s\n", zEQP);
2724      }
2725    }
2726  }
2727  sqlite3_expert_destroy(p);
2728  pState->expert.pExpert = 0;
2729  return rc;
2730}
2731
2732/*
2733** Implementation of ".expert" dot command.
2734*/
2735static int expertDotCommand(
2736  ShellState *pState,             /* Current shell tool state */
2737  char **azArg,                   /* Array of arguments passed to dot command */
2738  int nArg                        /* Number of entries in azArg[] */
2739){
2740  int rc = SQLITE_OK;
2741  char *zErr = 0;
2742  int i;
2743  int iSample = 0;
2744
2745  assert( pState->expert.pExpert==0 );
2746  memset(&pState->expert, 0, sizeof(ExpertInfo));
2747
2748  for(i=1; rc==SQLITE_OK && i<nArg; i++){
2749    char *z = azArg[i];
2750    int n;
2751    if( z[0]=='-' && z[1]=='-' ) z++;
2752    n = strlen30(z);
2753    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
2754      pState->expert.bVerbose = 1;
2755    }
2756    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
2757      if( i==(nArg-1) ){
2758        raw_printf(stderr, "option requires an argument: %s\n", z);
2759        rc = SQLITE_ERROR;
2760      }else{
2761        iSample = (int)integerValue(azArg[++i]);
2762        if( iSample<0 || iSample>100 ){
2763          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
2764          rc = SQLITE_ERROR;
2765        }
2766      }
2767    }
2768    else{
2769      raw_printf(stderr, "unknown option: %s\n", z);
2770      rc = SQLITE_ERROR;
2771    }
2772  }
2773
2774  if( rc==SQLITE_OK ){
2775    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
2776    if( pState->expert.pExpert==0 ){
2777      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
2778      rc = SQLITE_ERROR;
2779    }else{
2780      sqlite3_expert_config(
2781          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
2782      );
2783    }
2784  }
2785
2786  return rc;
2787}
2788#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
2789
2790/*
2791** Execute a statement or set of statements.  Print
2792** any result rows/columns depending on the current mode
2793** set via the supplied callback.
2794**
2795** This is very similar to SQLite's built-in sqlite3_exec()
2796** function except it takes a slightly different callback
2797** and callback data argument.
2798*/
2799static int shell_exec(
2800  ShellState *pArg,                         /* Pointer to ShellState */
2801  const char *zSql,                         /* SQL to be evaluated */
2802  char **pzErrMsg                           /* Error msg written here */
2803){
2804  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
2805  int rc = SQLITE_OK;             /* Return Code */
2806  int rc2;
2807  const char *zLeftover;          /* Tail of unprocessed SQL */
2808  sqlite3 *db = pArg->db;
2809
2810  if( pzErrMsg ){
2811    *pzErrMsg = NULL;
2812  }
2813
2814#ifndef SQLITE_OMIT_VIRTUALTABLE
2815  if( pArg->expert.pExpert ){
2816    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
2817    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
2818  }
2819#endif
2820
2821  while( zSql[0] && (SQLITE_OK == rc) ){
2822    static const char *zStmtSql;
2823    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
2824    if( SQLITE_OK != rc ){
2825      if( pzErrMsg ){
2826        *pzErrMsg = save_err_msg(db);
2827      }
2828    }else{
2829      if( !pStmt ){
2830        /* this happens for a comment or white-space */
2831        zSql = zLeftover;
2832        while( IsSpace(zSql[0]) ) zSql++;
2833        continue;
2834      }
2835      zStmtSql = sqlite3_sql(pStmt);
2836      if( zStmtSql==0 ) zStmtSql = "";
2837      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
2838
2839      /* save off the prepared statment handle and reset row count */
2840      if( pArg ){
2841        pArg->pStmt = pStmt;
2842        pArg->cnt = 0;
2843      }
2844
2845      /* echo the sql statement if echo on */
2846      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
2847        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
2848      }
2849
2850      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
2851      if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
2852        sqlite3_stmt *pExplain;
2853        char *zEQP;
2854        int triggerEQP = 0;
2855        disable_debug_trace_modes();
2856        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
2857        if( pArg->autoEQP>=AUTOEQP_trigger ){
2858          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
2859        }
2860        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
2861        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2862        if( rc==SQLITE_OK ){
2863          while( sqlite3_step(pExplain)==SQLITE_ROW ){
2864            raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
2865            raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
2866            raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
2867            utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
2868          }
2869        }
2870        sqlite3_finalize(pExplain);
2871        sqlite3_free(zEQP);
2872        if( pArg->autoEQP>=AUTOEQP_full ){
2873          /* Also do an EXPLAIN for ".eqp full" mode */
2874          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
2875          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2876          if( rc==SQLITE_OK ){
2877            pArg->cMode = MODE_Explain;
2878            explain_data_prepare(pArg, pExplain);
2879            exec_prepared_stmt(pArg, pExplain);
2880            explain_data_delete(pArg);
2881          }
2882          sqlite3_finalize(pExplain);
2883          sqlite3_free(zEQP);
2884        }
2885        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
2886          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
2887          /* Reprepare pStmt before reactiving trace modes */
2888          sqlite3_finalize(pStmt);
2889          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
2890        }
2891        restore_debug_trace_modes();
2892      }
2893
2894      if( pArg ){
2895        pArg->cMode = pArg->mode;
2896        if( pArg->autoExplain
2897         && sqlite3_column_count(pStmt)==8
2898         && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
2899        ){
2900          pArg->cMode = MODE_Explain;
2901        }
2902
2903        /* If the shell is currently in ".explain" mode, gather the extra
2904        ** data required to add indents to the output.*/
2905        if( pArg->cMode==MODE_Explain ){
2906          explain_data_prepare(pArg, pStmt);
2907        }
2908      }
2909
2910      exec_prepared_stmt(pArg, pStmt);
2911      explain_data_delete(pArg);
2912
2913      /* print usage stats if stats on */
2914      if( pArg && pArg->statsOn ){
2915        display_stats(db, pArg, 0);
2916      }
2917
2918      /* print loop-counters if required */
2919      if( pArg && pArg->scanstatsOn ){
2920        display_scanstats(db, pArg);
2921      }
2922
2923      /* Finalize the statement just executed. If this fails, save a
2924      ** copy of the error message. Otherwise, set zSql to point to the
2925      ** next statement to execute. */
2926      rc2 = sqlite3_finalize(pStmt);
2927      if( rc!=SQLITE_NOMEM ) rc = rc2;
2928      if( rc==SQLITE_OK ){
2929        zSql = zLeftover;
2930        while( IsSpace(zSql[0]) ) zSql++;
2931      }else if( pzErrMsg ){
2932        *pzErrMsg = save_err_msg(db);
2933      }
2934
2935      /* clear saved stmt handle */
2936      if( pArg ){
2937        pArg->pStmt = NULL;
2938      }
2939    }
2940  } /* end while */
2941
2942  return rc;
2943}
2944
2945/*
2946** Release memory previously allocated by tableColumnList().
2947*/
2948static void freeColumnList(char **azCol){
2949  int i;
2950  for(i=1; azCol[i]; i++){
2951    sqlite3_free(azCol[i]);
2952  }
2953  /* azCol[0] is a static string */
2954  sqlite3_free(azCol);
2955}
2956
2957/*
2958** Return a list of pointers to strings which are the names of all
2959** columns in table zTab.   The memory to hold the names is dynamically
2960** allocated and must be released by the caller using a subsequent call
2961** to freeColumnList().
2962**
2963** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
2964** value that needs to be preserved, then azCol[0] is filled in with the
2965** name of the rowid column.
2966**
2967** The first regular column in the table is azCol[1].  The list is terminated
2968** by an entry with azCol[i]==0.
2969*/
2970static char **tableColumnList(ShellState *p, const char *zTab){
2971  char **azCol = 0;
2972  sqlite3_stmt *pStmt;
2973  char *zSql;
2974  int nCol = 0;
2975  int nAlloc = 0;
2976  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
2977  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
2978  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
2979  int rc;
2980
2981  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
2982  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2983  sqlite3_free(zSql);
2984  if( rc ) return 0;
2985  while( sqlite3_step(pStmt)==SQLITE_ROW ){
2986    if( nCol>=nAlloc-2 ){
2987      nAlloc = nAlloc*2 + nCol + 10;
2988      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
2989      if( azCol==0 ){
2990        raw_printf(stderr, "Error: out of memory\n");
2991        exit(1);
2992      }
2993    }
2994    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2995    if( sqlite3_column_int(pStmt, 5) ){
2996      nPK++;
2997      if( nPK==1
2998       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
2999                          "INTEGER")==0
3000      ){
3001        isIPK = 1;
3002      }else{
3003        isIPK = 0;
3004      }
3005    }
3006  }
3007  sqlite3_finalize(pStmt);
3008  if( azCol==0 ) return 0;
3009  azCol[0] = 0;
3010  azCol[nCol+1] = 0;
3011
3012  /* The decision of whether or not a rowid really needs to be preserved
3013  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3014  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3015  ** rowids on tables where the rowid is inaccessible because there are other
3016  ** columns in the table named "rowid", "_rowid_", and "oid".
3017  */
3018  if( preserveRowid && isIPK ){
3019    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3020    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3021    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3022    ** ROWID aliases.  To distinguish these cases, check to see if
3023    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3024    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3025    */
3026    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3027                           " WHERE origin='pk'", zTab);
3028    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3029    sqlite3_free(zSql);
3030    if( rc ){
3031      freeColumnList(azCol);
3032      return 0;
3033    }
3034    rc = sqlite3_step(pStmt);
3035    sqlite3_finalize(pStmt);
3036    preserveRowid = rc==SQLITE_ROW;
3037  }
3038  if( preserveRowid ){
3039    /* Only preserve the rowid if we can find a name to use for the
3040    ** rowid */
3041    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3042    int i, j;
3043    for(j=0; j<3; j++){
3044      for(i=1; i<=nCol; i++){
3045        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3046      }
3047      if( i>nCol ){
3048        /* At this point, we know that azRowid[j] is not the name of any
3049        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3050        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3051        ** tables will fail this last check */
3052        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3053        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3054        break;
3055      }
3056    }
3057  }
3058  return azCol;
3059}
3060
3061/*
3062** Toggle the reverse_unordered_selects setting.
3063*/
3064static void toggleSelectOrder(sqlite3 *db){
3065  sqlite3_stmt *pStmt = 0;
3066  int iSetting = 0;
3067  char zStmt[100];
3068  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3069  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3070    iSetting = sqlite3_column_int(pStmt, 0);
3071  }
3072  sqlite3_finalize(pStmt);
3073  sqlite3_snprintf(sizeof(zStmt), zStmt,
3074       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3075  sqlite3_exec(db, zStmt, 0, 0, 0);
3076}
3077
3078/*
3079** This is a different callback routine used for dumping the database.
3080** Each row received by this callback consists of a table name,
3081** the table type ("index" or "table") and SQL to create the table.
3082** This routine should print text sufficient to recreate the table.
3083*/
3084static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3085  int rc;
3086  const char *zTable;
3087  const char *zType;
3088  const char *zSql;
3089  ShellState *p = (ShellState *)pArg;
3090
3091  UNUSED_PARAMETER(azNotUsed);
3092  if( nArg!=3 || azArg==0 ) return 0;
3093  zTable = azArg[0];
3094  zType = azArg[1];
3095  zSql = azArg[2];
3096
3097  if( strcmp(zTable, "sqlite_sequence")==0 ){
3098    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3099  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3100    raw_printf(p->out, "ANALYZE sqlite_master;\n");
3101  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3102    return 0;
3103  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3104    char *zIns;
3105    if( !p->writableSchema ){
3106      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3107      p->writableSchema = 1;
3108    }
3109    zIns = sqlite3_mprintf(
3110       "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
3111       "VALUES('table','%q','%q',0,'%q');",
3112       zTable, zTable, zSql);
3113    utf8_printf(p->out, "%s\n", zIns);
3114    sqlite3_free(zIns);
3115    return 0;
3116  }else{
3117    printSchemaLine(p->out, zSql, ";\n");
3118  }
3119
3120  if( strcmp(zType, "table")==0 ){
3121    ShellText sSelect;
3122    ShellText sTable;
3123    char **azCol;
3124    int i;
3125    char *savedDestTable;
3126    int savedMode;
3127
3128    azCol = tableColumnList(p, zTable);
3129    if( azCol==0 ){
3130      p->nErr++;
3131      return 0;
3132    }
3133
3134    /* Always quote the table name, even if it appears to be pure ascii,
3135    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3136    initText(&sTable);
3137    appendText(&sTable, zTable, quoteChar(zTable));
3138    /* If preserving the rowid, add a column list after the table name.
3139    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3140    ** instead of the usual "INSERT INTO tab VALUES(...)".
3141    */
3142    if( azCol[0] ){
3143      appendText(&sTable, "(", 0);
3144      appendText(&sTable, azCol[0], 0);
3145      for(i=1; azCol[i]; i++){
3146        appendText(&sTable, ",", 0);
3147        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3148      }
3149      appendText(&sTable, ")", 0);
3150    }
3151
3152    /* Build an appropriate SELECT statement */
3153    initText(&sSelect);
3154    appendText(&sSelect, "SELECT ", 0);
3155    if( azCol[0] ){
3156      appendText(&sSelect, azCol[0], 0);
3157      appendText(&sSelect, ",", 0);
3158    }
3159    for(i=1; azCol[i]; i++){
3160      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3161      if( azCol[i+1] ){
3162        appendText(&sSelect, ",", 0);
3163      }
3164    }
3165    freeColumnList(azCol);
3166    appendText(&sSelect, " FROM ", 0);
3167    appendText(&sSelect, zTable, quoteChar(zTable));
3168
3169    savedDestTable = p->zDestTable;
3170    savedMode = p->mode;
3171    p->zDestTable = sTable.z;
3172    p->mode = p->cMode = MODE_Insert;
3173    rc = shell_exec(p, sSelect.z, 0);
3174    if( (rc&0xff)==SQLITE_CORRUPT ){
3175      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3176      toggleSelectOrder(p->db);
3177      shell_exec(p, sSelect.z, 0);
3178      toggleSelectOrder(p->db);
3179    }
3180    p->zDestTable = savedDestTable;
3181    p->mode = savedMode;
3182    freeText(&sTable);
3183    freeText(&sSelect);
3184    if( rc ) p->nErr++;
3185  }
3186  return 0;
3187}
3188
3189/*
3190** Run zQuery.  Use dump_callback() as the callback routine so that
3191** the contents of the query are output as SQL statements.
3192**
3193** If we get a SQLITE_CORRUPT error, rerun the query after appending
3194** "ORDER BY rowid DESC" to the end.
3195*/
3196static int run_schema_dump_query(
3197  ShellState *p,
3198  const char *zQuery
3199){
3200  int rc;
3201  char *zErr = 0;
3202  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3203  if( rc==SQLITE_CORRUPT ){
3204    char *zQ2;
3205    int len = strlen30(zQuery);
3206    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3207    if( zErr ){
3208      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3209      sqlite3_free(zErr);
3210      zErr = 0;
3211    }
3212    zQ2 = malloc( len+100 );
3213    if( zQ2==0 ) return rc;
3214    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3215    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3216    if( rc ){
3217      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3218    }else{
3219      rc = SQLITE_CORRUPT;
3220    }
3221    sqlite3_free(zErr);
3222    free(zQ2);
3223  }
3224  return rc;
3225}
3226
3227/*
3228** Text of a help message
3229*/
3230static char zHelp[] =
3231#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3232  ".archive ...           Manage SQL archives: \".archive --help\" for details\n"
3233#endif
3234#ifndef SQLITE_OMIT_AUTHORIZATION
3235  ".auth ON|OFF           Show authorizer callbacks\n"
3236#endif
3237  ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
3238  ".bail on|off           Stop after hitting an error.  Default OFF\n"
3239  ".binary on|off         Turn binary output on or off.  Default OFF\n"
3240  ".cd DIRECTORY          Change the working directory to DIRECTORY\n"
3241  ".changes on|off        Show number of rows changed by SQL\n"
3242  ".check GLOB            Fail if output since .testcase does not match\n"
3243  ".clone NEWDB           Clone data into NEWDB from the existing database\n"
3244  ".databases             List names and files of attached databases\n"
3245  ".dbinfo ?DB?           Show status information about the database\n"
3246  ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
3247  "                         If TABLE specified, only dump tables matching\n"
3248  "                         LIKE pattern TABLE.\n"
3249  ".echo on|off           Turn command echo on or off\n"
3250  ".eqp on|off|full       Enable or disable automatic EXPLAIN QUERY PLAN\n"
3251  ".excel                 Display the output of next command in a spreadsheet\n"
3252  ".exit                  Exit this program\n"
3253  ".expert                EXPERIMENTAL. Suggest indexes for specified queries\n"
3254/* Because explain mode comes on automatically now, the ".explain" mode
3255** is removed from the help screen.  It is still supported for legacy, however */
3256/*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
3257  ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
3258  ".headers on|off        Turn display of headers on or off\n"
3259  ".help                  Show this message\n"
3260  ".import FILE TABLE     Import data from FILE into TABLE\n"
3261#ifndef SQLITE_OMIT_TEST_CONTROL
3262  ".imposter INDEX TABLE  Create imposter table TABLE on index INDEX\n"
3263#endif
3264  ".indexes ?TABLE?       Show names of all indexes\n"
3265  "                         If TABLE specified, only show indexes for tables\n"
3266  "                         matching LIKE pattern TABLE.\n"
3267#ifdef SQLITE_ENABLE_IOTRACE
3268  ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
3269#endif
3270  ".limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT\n"
3271  ".lint OPTIONS          Report potential schema issues. Options:\n"
3272  "                         fkey-indexes     Find missing foreign key indexes\n"
3273#ifndef SQLITE_OMIT_LOAD_EXTENSION
3274  ".load FILE ?ENTRY?     Load an extension library\n"
3275#endif
3276  ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
3277  ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
3278  "                         ascii    Columns/rows delimited by 0x1F and 0x1E\n"
3279  "                         csv      Comma-separated values\n"
3280  "                         column   Left-aligned columns.  (See .width)\n"
3281  "                         html     HTML <table> code\n"
3282  "                         insert   SQL insert statements for TABLE\n"
3283  "                         line     One value per line\n"
3284  "                         list     Values delimited by \"|\"\n"
3285  "                         quote    Escape answers as for SQL\n"
3286  "                         tabs     Tab-separated values\n"
3287  "                         tcl      TCL list elements\n"
3288  ".nullvalue STRING      Use STRING in place of NULL values\n"
3289  ".once (-e|-x|FILE)     Output for the next SQL command only to FILE\n"
3290  "                         or invoke system text editor (-e) or spreadsheet (-x)\n"
3291  "                         on the output.\n"
3292  ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
3293  "                         The --new option starts with an empty file\n"
3294  "                         Other options: --readonly --append --zip\n"
3295  ".output ?FILE?         Send output to FILE or stdout\n"
3296  ".print STRING...       Print literal STRING\n"
3297  ".prompt MAIN CONTINUE  Replace the standard prompts\n"
3298  ".quit                  Exit this program\n"
3299  ".read FILENAME         Execute SQL in FILENAME\n"
3300  ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
3301  ".save FILE             Write in-memory database into FILE\n"
3302  ".scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off\n"
3303  ".schema ?PATTERN?      Show the CREATE statements matching PATTERN\n"
3304  "                          Add --indent for pretty-printing\n"
3305  ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
3306  ".separator COL ?ROW?   Change the column separator and optionally the row\n"
3307  "                         separator for both the output mode and .import\n"
3308#if defined(SQLITE_ENABLE_SESSION)
3309  ".session CMD ...       Create or control sessions\n"
3310#endif
3311  ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
3312#ifndef SQLITE_NOHAVE_SYSTEM
3313  ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
3314#endif
3315  ".show                  Show the current values for various settings\n"
3316  ".stats ?on|off?        Show stats or turn stats on or off\n"
3317#ifndef SQLITE_NOHAVE_SYSTEM
3318  ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
3319#endif
3320  ".tables ?TABLE?        List names of tables\n"
3321  "                         If TABLE specified, only list tables matching\n"
3322  "                         LIKE pattern TABLE.\n"
3323  ".testcase NAME         Begin redirecting output to 'testcase-out.txt'\n"
3324  ".timeout MS            Try opening locked tables for MS milliseconds\n"
3325  ".timer on|off          Turn SQL timer on or off\n"
3326  ".trace FILE|off        Output each SQL statement as it is run\n"
3327  ".vfsinfo ?AUX?         Information about the top-level VFS\n"
3328  ".vfslist               List all available VFSes\n"
3329  ".vfsname ?AUX?         Print the name of the VFS stack\n"
3330  ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
3331  "                         Negative values right-justify\n"
3332;
3333
3334#if defined(SQLITE_ENABLE_SESSION)
3335/*
3336** Print help information for the ".sessions" command
3337*/
3338void session_help(ShellState *p){
3339  raw_printf(p->out,
3340    ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
3341    "If ?NAME? is omitted, the first defined session is used.\n"
3342    "Subcommands:\n"
3343    "   attach TABLE             Attach TABLE\n"
3344    "   changeset FILE           Write a changeset into FILE\n"
3345    "   close                    Close one session\n"
3346    "   enable ?BOOLEAN?         Set or query the enable bit\n"
3347    "   filter GLOB...           Reject tables matching GLOBs\n"
3348    "   indirect ?BOOLEAN?       Mark or query the indirect status\n"
3349    "   isempty                  Query whether the session is empty\n"
3350    "   list                     List currently open session names\n"
3351    "   open DB NAME             Open a new session on DB\n"
3352    "   patchset FILE            Write a patchset into FILE\n"
3353  );
3354}
3355#endif
3356
3357
3358/* Forward reference */
3359static int process_input(ShellState *p, FILE *in);
3360
3361/*
3362** Read the content of file zName into memory obtained from sqlite3_malloc64()
3363** and return a pointer to the buffer. The caller is responsible for freeing
3364** the memory.
3365**
3366** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
3367** read.
3368**
3369** For convenience, a nul-terminator byte is always appended to the data read
3370** from the file before the buffer is returned. This byte is not included in
3371** the final value of (*pnByte), if applicable.
3372**
3373** NULL is returned if any error is encountered. The final value of *pnByte
3374** is undefined in this case.
3375*/
3376static char *readFile(const char *zName, int *pnByte){
3377  FILE *in = fopen(zName, "rb");
3378  long nIn;
3379  size_t nRead;
3380  char *pBuf;
3381  if( in==0 ) return 0;
3382  fseek(in, 0, SEEK_END);
3383  nIn = ftell(in);
3384  rewind(in);
3385  pBuf = sqlite3_malloc64( nIn+1 );
3386  if( pBuf==0 ) return 0;
3387  nRead = fread(pBuf, nIn, 1, in);
3388  fclose(in);
3389  if( nRead!=1 ){
3390    sqlite3_free(pBuf);
3391    return 0;
3392  }
3393  pBuf[nIn] = 0;
3394  if( pnByte ) *pnByte = nIn;
3395  return pBuf;
3396}
3397
3398#if defined(SQLITE_ENABLE_SESSION)
3399/*
3400** Close a single OpenSession object and release all of its associated
3401** resources.
3402*/
3403static void session_close(OpenSession *pSession){
3404  int i;
3405  sqlite3session_delete(pSession->p);
3406  sqlite3_free(pSession->zName);
3407  for(i=0; i<pSession->nFilter; i++){
3408    sqlite3_free(pSession->azFilter[i]);
3409  }
3410  sqlite3_free(pSession->azFilter);
3411  memset(pSession, 0, sizeof(OpenSession));
3412}
3413#endif
3414
3415/*
3416** Close all OpenSession objects and release all associated resources.
3417*/
3418#if defined(SQLITE_ENABLE_SESSION)
3419static void session_close_all(ShellState *p){
3420  int i;
3421  for(i=0; i<p->nSession; i++){
3422    session_close(&p->aSession[i]);
3423  }
3424  p->nSession = 0;
3425}
3426#else
3427# define session_close_all(X)
3428#endif
3429
3430/*
3431** Implementation of the xFilter function for an open session.  Omit
3432** any tables named by ".session filter" but let all other table through.
3433*/
3434#if defined(SQLITE_ENABLE_SESSION)
3435static int session_filter(void *pCtx, const char *zTab){
3436  OpenSession *pSession = (OpenSession*)pCtx;
3437  int i;
3438  for(i=0; i<pSession->nFilter; i++){
3439    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
3440  }
3441  return 1;
3442}
3443#endif
3444
3445/*
3446** Try to deduce the type of file for zName based on its content.  Return
3447** one of the SHELL_OPEN_* constants.
3448**
3449** If the file does not exist or is empty but its name looks like a ZIP
3450** archive and the dfltZip flag is true, then assume it is a ZIP archive.
3451** Otherwise, assume an ordinary database regardless of the filename if
3452** the type cannot be determined from content.
3453*/
3454static int deduceDatabaseType(const char *zName, int dfltZip){
3455  FILE *f = fopen(zName, "rb");
3456  size_t n;
3457  int rc = SHELL_OPEN_UNSPEC;
3458  char zBuf[100];
3459  if( f==0 ){
3460    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
3461    return SHELL_OPEN_NORMAL;
3462  }
3463  fseek(f, -25, SEEK_END);
3464  n = fread(zBuf, 25, 1, f);
3465  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
3466    rc = SHELL_OPEN_APPENDVFS;
3467  }else{
3468    fseek(f, -22, SEEK_END);
3469    n = fread(zBuf, 22, 1, f);
3470    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
3471       && zBuf[3]==0x06 ){
3472      rc = SHELL_OPEN_ZIPFILE;
3473    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
3474      return SHELL_OPEN_ZIPFILE;
3475    }
3476  }
3477  fclose(f);
3478  return rc;
3479}
3480
3481/*
3482** Make sure the database is open.  If it is not, then open it.  If
3483** the database fails to open, print an error message and exit.
3484*/
3485static void open_db(ShellState *p, int keepAlive){
3486  if( p->db==0 ){
3487    if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
3488      p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
3489    }
3490    switch( p->openMode ){
3491      case SHELL_OPEN_APPENDVFS: {
3492        sqlite3_open_v2(p->zDbFilename, &p->db,
3493           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
3494        break;
3495      }
3496      case SHELL_OPEN_ZIPFILE: {
3497        sqlite3_open(":memory:", &p->db);
3498        break;
3499      }
3500      case SHELL_OPEN_READONLY: {
3501        sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
3502        break;
3503      }
3504      case SHELL_OPEN_UNSPEC:
3505      case SHELL_OPEN_NORMAL: {
3506        sqlite3_open(p->zDbFilename, &p->db);
3507        break;
3508      }
3509    }
3510    globalDb = p->db;
3511    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
3512      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
3513          p->zDbFilename, sqlite3_errmsg(p->db));
3514      if( keepAlive ) return;
3515      exit(1);
3516    }
3517#ifndef SQLITE_OMIT_LOAD_EXTENSION
3518    sqlite3_enable_load_extension(p->db, 1);
3519#endif
3520    sqlite3_fileio_init(p->db, 0, 0);
3521    sqlite3_shathree_init(p->db, 0, 0);
3522    sqlite3_completion_init(p->db, 0, 0);
3523#ifdef SQLITE_HAVE_ZLIB
3524    sqlite3_zipfile_init(p->db, 0, 0);
3525    sqlite3_sqlar_init(p->db, 0, 0);
3526#endif
3527    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
3528                            shellAddSchemaName, 0, 0);
3529    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
3530                            shellModuleSchema, 0, 0);
3531    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
3532                            shellPutsFunc, 0, 0);
3533#ifndef SQLITE_NOHAVE_SYSTEM
3534    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
3535                            editFunc, 0, 0);
3536    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
3537                            editFunc, 0, 0);
3538#endif
3539    if( p->openMode==SHELL_OPEN_ZIPFILE ){
3540      char *zSql = sqlite3_mprintf(
3541         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
3542      sqlite3_exec(p->db, zSql, 0, 0, 0);
3543      sqlite3_free(zSql);
3544    }
3545  }
3546}
3547
3548#if HAVE_READLINE || HAVE_EDITLINE
3549/*
3550** Readline completion callbacks
3551*/
3552static char *readline_completion_generator(const char *text, int state){
3553  static sqlite3_stmt *pStmt = 0;
3554  char *zRet;
3555  if( state==0 ){
3556    char *zSql;
3557    sqlite3_finalize(pStmt);
3558    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
3559                           "  FROM completion(%Q) ORDER BY 1", text);
3560    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
3561    sqlite3_free(zSql);
3562  }
3563  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3564    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
3565  }else{
3566    sqlite3_finalize(pStmt);
3567    pStmt = 0;
3568    zRet = 0;
3569  }
3570  return zRet;
3571}
3572static char **readline_completion(const char *zText, int iStart, int iEnd){
3573  rl_attempted_completion_over = 1;
3574  return rl_completion_matches(zText, readline_completion_generator);
3575}
3576
3577#elif HAVE_LINENOISE
3578/*
3579** Linenoise completion callback
3580*/
3581static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
3582  int nLine = strlen30(zLine);
3583  int i, iStart;
3584  sqlite3_stmt *pStmt = 0;
3585  char *zSql;
3586  char zBuf[1000];
3587
3588  if( nLine>sizeof(zBuf)-30 ) return;
3589  if( zLine[0]=='.' ) return;
3590  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
3591  if( i==nLine-1 ) return;
3592  iStart = i+1;
3593  memcpy(zBuf, zLine, iStart);
3594  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
3595                         "  FROM completion(%Q,%Q) ORDER BY 1",
3596                         &zLine[iStart], zLine);
3597  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
3598  sqlite3_free(zSql);
3599  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
3600  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3601    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
3602    int nCompletion = sqlite3_column_bytes(pStmt, 0);
3603    if( iStart+nCompletion < sizeof(zBuf)-1 ){
3604      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
3605      linenoiseAddCompletion(lc, zBuf);
3606    }
3607  }
3608  sqlite3_finalize(pStmt);
3609}
3610#endif
3611
3612/*
3613** Do C-language style dequoting.
3614**
3615**    \a    -> alarm
3616**    \b    -> backspace
3617**    \t    -> tab
3618**    \n    -> newline
3619**    \v    -> vertical tab
3620**    \f    -> form feed
3621**    \r    -> carriage return
3622**    \s    -> space
3623**    \"    -> "
3624**    \'    -> '
3625**    \\    -> backslash
3626**    \NNN  -> ascii character NNN in octal
3627*/
3628static void resolve_backslashes(char *z){
3629  int i, j;
3630  char c;
3631  while( *z && *z!='\\' ) z++;
3632  for(i=j=0; (c = z[i])!=0; i++, j++){
3633    if( c=='\\' && z[i+1]!=0 ){
3634      c = z[++i];
3635      if( c=='a' ){
3636        c = '\a';
3637      }else if( c=='b' ){
3638        c = '\b';
3639      }else if( c=='t' ){
3640        c = '\t';
3641      }else if( c=='n' ){
3642        c = '\n';
3643      }else if( c=='v' ){
3644        c = '\v';
3645      }else if( c=='f' ){
3646        c = '\f';
3647      }else if( c=='r' ){
3648        c = '\r';
3649      }else if( c=='"' ){
3650        c = '"';
3651      }else if( c=='\'' ){
3652        c = '\'';
3653      }else if( c=='\\' ){
3654        c = '\\';
3655      }else if( c>='0' && c<='7' ){
3656        c -= '0';
3657        if( z[i+1]>='0' && z[i+1]<='7' ){
3658          i++;
3659          c = (c<<3) + z[i] - '0';
3660          if( z[i+1]>='0' && z[i+1]<='7' ){
3661            i++;
3662            c = (c<<3) + z[i] - '0';
3663          }
3664        }
3665      }
3666    }
3667    z[j] = c;
3668  }
3669  if( j<i ) z[j] = 0;
3670}
3671
3672/*
3673** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
3674** for TRUE and FALSE.  Return the integer value if appropriate.
3675*/
3676static int booleanValue(const char *zArg){
3677  int i;
3678  if( zArg[0]=='0' && zArg[1]=='x' ){
3679    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
3680  }else{
3681    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
3682  }
3683  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
3684  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
3685    return 1;
3686  }
3687  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
3688    return 0;
3689  }
3690  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
3691          zArg);
3692  return 0;
3693}
3694
3695/*
3696** Set or clear a shell flag according to a boolean value.
3697*/
3698static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
3699  if( booleanValue(zArg) ){
3700    ShellSetFlag(p, mFlag);
3701  }else{
3702    ShellClearFlag(p, mFlag);
3703  }
3704}
3705
3706/*
3707** Close an output file, assuming it is not stderr or stdout
3708*/
3709static void output_file_close(FILE *f){
3710  if( f && f!=stdout && f!=stderr ) fclose(f);
3711}
3712
3713/*
3714** Try to open an output file.   The names "stdout" and "stderr" are
3715** recognized and do the right thing.  NULL is returned if the output
3716** filename is "off".
3717*/
3718static FILE *output_file_open(const char *zFile, int bTextMode){
3719  FILE *f;
3720  if( strcmp(zFile,"stdout")==0 ){
3721    f = stdout;
3722  }else if( strcmp(zFile, "stderr")==0 ){
3723    f = stderr;
3724  }else if( strcmp(zFile, "off")==0 ){
3725    f = 0;
3726  }else{
3727    f = fopen(zFile, bTextMode ? "w" : "wb");
3728    if( f==0 ){
3729      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
3730    }
3731  }
3732  return f;
3733}
3734
3735#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3736/*
3737** A routine for handling output from sqlite3_trace().
3738*/
3739static int sql_trace_callback(
3740  unsigned mType,
3741  void *pArg,
3742  void *pP,
3743  void *pX
3744){
3745  FILE *f = (FILE*)pArg;
3746  UNUSED_PARAMETER(mType);
3747  UNUSED_PARAMETER(pP);
3748  if( f ){
3749    const char *z = (const char*)pX;
3750    int i = strlen30(z);
3751    while( i>0 && z[i-1]==';' ){ i--; }
3752    utf8_printf(f, "%.*s;\n", i, z);
3753  }
3754  return 0;
3755}
3756#endif
3757
3758/*
3759** A no-op routine that runs with the ".breakpoint" doc-command.  This is
3760** a useful spot to set a debugger breakpoint.
3761*/
3762static void test_breakpoint(void){
3763  static int nCall = 0;
3764  nCall++;
3765}
3766
3767/*
3768** An object used to read a CSV and other files for import.
3769*/
3770typedef struct ImportCtx ImportCtx;
3771struct ImportCtx {
3772  const char *zFile;  /* Name of the input file */
3773  FILE *in;           /* Read the CSV text from this input stream */
3774  char *z;            /* Accumulated text for a field */
3775  int n;              /* Number of bytes in z */
3776  int nAlloc;         /* Space allocated for z[] */
3777  int nLine;          /* Current line number */
3778  int bNotFirst;      /* True if one or more bytes already read */
3779  int cTerm;          /* Character that terminated the most recent field */
3780  int cColSep;        /* The column separator character.  (Usually ",") */
3781  int cRowSep;        /* The row separator character.  (Usually "\n") */
3782};
3783
3784/* Append a single byte to z[] */
3785static void import_append_char(ImportCtx *p, int c){
3786  if( p->n+1>=p->nAlloc ){
3787    p->nAlloc += p->nAlloc + 100;
3788    p->z = sqlite3_realloc64(p->z, p->nAlloc);
3789    if( p->z==0 ){
3790      raw_printf(stderr, "out of memory\n");
3791      exit(1);
3792    }
3793  }
3794  p->z[p->n++] = (char)c;
3795}
3796
3797/* Read a single field of CSV text.  Compatible with rfc4180 and extended
3798** with the option of having a separator other than ",".
3799**
3800**   +  Input comes from p->in.
3801**   +  Store results in p->z of length p->n.  Space to hold p->z comes
3802**      from sqlite3_malloc64().
3803**   +  Use p->cSep as the column separator.  The default is ",".
3804**   +  Use p->rSep as the row separator.  The default is "\n".
3805**   +  Keep track of the line number in p->nLine.
3806**   +  Store the character that terminates the field in p->cTerm.  Store
3807**      EOF on end-of-file.
3808**   +  Report syntax errors on stderr
3809*/
3810static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
3811  int c;
3812  int cSep = p->cColSep;
3813  int rSep = p->cRowSep;
3814  p->n = 0;
3815  c = fgetc(p->in);
3816  if( c==EOF || seenInterrupt ){
3817    p->cTerm = EOF;
3818    return 0;
3819  }
3820  if( c=='"' ){
3821    int pc, ppc;
3822    int startLine = p->nLine;
3823    int cQuote = c;
3824    pc = ppc = 0;
3825    while( 1 ){
3826      c = fgetc(p->in);
3827      if( c==rSep ) p->nLine++;
3828      if( c==cQuote ){
3829        if( pc==cQuote ){
3830          pc = 0;
3831          continue;
3832        }
3833      }
3834      if( (c==cSep && pc==cQuote)
3835       || (c==rSep && pc==cQuote)
3836       || (c==rSep && pc=='\r' && ppc==cQuote)
3837       || (c==EOF && pc==cQuote)
3838      ){
3839        do{ p->n--; }while( p->z[p->n]!=cQuote );
3840        p->cTerm = c;
3841        break;
3842      }
3843      if( pc==cQuote && c!='\r' ){
3844        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
3845                p->zFile, p->nLine, cQuote);
3846      }
3847      if( c==EOF ){
3848        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
3849                p->zFile, startLine, cQuote);
3850        p->cTerm = c;
3851        break;
3852      }
3853      import_append_char(p, c);
3854      ppc = pc;
3855      pc = c;
3856    }
3857  }else{
3858    /* If this is the first field being parsed and it begins with the
3859    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
3860    if( (c&0xff)==0xef && p->bNotFirst==0 ){
3861      import_append_char(p, c);
3862      c = fgetc(p->in);
3863      if( (c&0xff)==0xbb ){
3864        import_append_char(p, c);
3865        c = fgetc(p->in);
3866        if( (c&0xff)==0xbf ){
3867          p->bNotFirst = 1;
3868          p->n = 0;
3869          return csv_read_one_field(p);
3870        }
3871      }
3872    }
3873    while( c!=EOF && c!=cSep && c!=rSep ){
3874      import_append_char(p, c);
3875      c = fgetc(p->in);
3876    }
3877    if( c==rSep ){
3878      p->nLine++;
3879      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
3880    }
3881    p->cTerm = c;
3882  }
3883  if( p->z ) p->z[p->n] = 0;
3884  p->bNotFirst = 1;
3885  return p->z;
3886}
3887
3888/* Read a single field of ASCII delimited text.
3889**
3890**   +  Input comes from p->in.
3891**   +  Store results in p->z of length p->n.  Space to hold p->z comes
3892**      from sqlite3_malloc64().
3893**   +  Use p->cSep as the column separator.  The default is "\x1F".
3894**   +  Use p->rSep as the row separator.  The default is "\x1E".
3895**   +  Keep track of the row number in p->nLine.
3896**   +  Store the character that terminates the field in p->cTerm.  Store
3897**      EOF on end-of-file.
3898**   +  Report syntax errors on stderr
3899*/
3900static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
3901  int c;
3902  int cSep = p->cColSep;
3903  int rSep = p->cRowSep;
3904  p->n = 0;
3905  c = fgetc(p->in);
3906  if( c==EOF || seenInterrupt ){
3907    p->cTerm = EOF;
3908    return 0;
3909  }
3910  while( c!=EOF && c!=cSep && c!=rSep ){
3911    import_append_char(p, c);
3912    c = fgetc(p->in);
3913  }
3914  if( c==rSep ){
3915    p->nLine++;
3916  }
3917  p->cTerm = c;
3918  if( p->z ) p->z[p->n] = 0;
3919  return p->z;
3920}
3921
3922/*
3923** Try to transfer data for table zTable.  If an error is seen while
3924** moving forward, try to go backwards.  The backwards movement won't
3925** work for WITHOUT ROWID tables.
3926*/
3927static void tryToCloneData(
3928  ShellState *p,
3929  sqlite3 *newDb,
3930  const char *zTable
3931){
3932  sqlite3_stmt *pQuery = 0;
3933  sqlite3_stmt *pInsert = 0;
3934  char *zQuery = 0;
3935  char *zInsert = 0;
3936  int rc;
3937  int i, j, n;
3938  int nTable = strlen30(zTable);
3939  int k = 0;
3940  int cnt = 0;
3941  const int spinRate = 10000;
3942
3943  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
3944  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3945  if( rc ){
3946    utf8_printf(stderr, "Error %d: %s on [%s]\n",
3947            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3948            zQuery);
3949    goto end_data_xfer;
3950  }
3951  n = sqlite3_column_count(pQuery);
3952  zInsert = sqlite3_malloc64(200 + nTable + n*3);
3953  if( zInsert==0 ){
3954    raw_printf(stderr, "out of memory\n");
3955    goto end_data_xfer;
3956  }
3957  sqlite3_snprintf(200+nTable,zInsert,
3958                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
3959  i = strlen30(zInsert);
3960  for(j=1; j<n; j++){
3961    memcpy(zInsert+i, ",?", 2);
3962    i += 2;
3963  }
3964  memcpy(zInsert+i, ");", 3);
3965  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
3966  if( rc ){
3967    utf8_printf(stderr, "Error %d: %s on [%s]\n",
3968            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
3969            zQuery);
3970    goto end_data_xfer;
3971  }
3972  for(k=0; k<2; k++){
3973    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3974      for(i=0; i<n; i++){
3975        switch( sqlite3_column_type(pQuery, i) ){
3976          case SQLITE_NULL: {
3977            sqlite3_bind_null(pInsert, i+1);
3978            break;
3979          }
3980          case SQLITE_INTEGER: {
3981            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
3982            break;
3983          }
3984          case SQLITE_FLOAT: {
3985            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
3986            break;
3987          }
3988          case SQLITE_TEXT: {
3989            sqlite3_bind_text(pInsert, i+1,
3990                             (const char*)sqlite3_column_text(pQuery,i),
3991                             -1, SQLITE_STATIC);
3992            break;
3993          }
3994          case SQLITE_BLOB: {
3995            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
3996                                            sqlite3_column_bytes(pQuery,i),
3997                                            SQLITE_STATIC);
3998            break;
3999          }
4000        }
4001      } /* End for */
4002      rc = sqlite3_step(pInsert);
4003      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
4004        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
4005                        sqlite3_errmsg(newDb));
4006      }
4007      sqlite3_reset(pInsert);
4008      cnt++;
4009      if( (cnt%spinRate)==0 ){
4010        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
4011        fflush(stdout);
4012      }
4013    } /* End while */
4014    if( rc==SQLITE_DONE ) break;
4015    sqlite3_finalize(pQuery);
4016    sqlite3_free(zQuery);
4017    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
4018                             zTable);
4019    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4020    if( rc ){
4021      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
4022      break;
4023    }
4024  } /* End for(k=0...) */
4025
4026end_data_xfer:
4027  sqlite3_finalize(pQuery);
4028  sqlite3_finalize(pInsert);
4029  sqlite3_free(zQuery);
4030  sqlite3_free(zInsert);
4031}
4032
4033
4034/*
4035** Try to transfer all rows of the schema that match zWhere.  For
4036** each row, invoke xForEach() on the object defined by that row.
4037** If an error is encountered while moving forward through the
4038** sqlite_master table, try again moving backwards.
4039*/
4040static void tryToCloneSchema(
4041  ShellState *p,
4042  sqlite3 *newDb,
4043  const char *zWhere,
4044  void (*xForEach)(ShellState*,sqlite3*,const char*)
4045){
4046  sqlite3_stmt *pQuery = 0;
4047  char *zQuery = 0;
4048  int rc;
4049  const unsigned char *zName;
4050  const unsigned char *zSql;
4051  char *zErrMsg = 0;
4052
4053  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4054                           " WHERE %s", zWhere);
4055  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4056  if( rc ){
4057    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4058                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4059                    zQuery);
4060    goto end_schema_xfer;
4061  }
4062  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4063    zName = sqlite3_column_text(pQuery, 0);
4064    zSql = sqlite3_column_text(pQuery, 1);
4065    printf("%s... ", zName); fflush(stdout);
4066    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4067    if( zErrMsg ){
4068      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4069      sqlite3_free(zErrMsg);
4070      zErrMsg = 0;
4071    }
4072    if( xForEach ){
4073      xForEach(p, newDb, (const char*)zName);
4074    }
4075    printf("done\n");
4076  }
4077  if( rc!=SQLITE_DONE ){
4078    sqlite3_finalize(pQuery);
4079    sqlite3_free(zQuery);
4080    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4081                             " WHERE %s ORDER BY rowid DESC", zWhere);
4082    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4083    if( rc ){
4084      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4085                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4086                      zQuery);
4087      goto end_schema_xfer;
4088    }
4089    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4090      zName = sqlite3_column_text(pQuery, 0);
4091      zSql = sqlite3_column_text(pQuery, 1);
4092      printf("%s... ", zName); fflush(stdout);
4093      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4094      if( zErrMsg ){
4095        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4096        sqlite3_free(zErrMsg);
4097        zErrMsg = 0;
4098      }
4099      if( xForEach ){
4100        xForEach(p, newDb, (const char*)zName);
4101      }
4102      printf("done\n");
4103    }
4104  }
4105end_schema_xfer:
4106  sqlite3_finalize(pQuery);
4107  sqlite3_free(zQuery);
4108}
4109
4110/*
4111** Open a new database file named "zNewDb".  Try to recover as much information
4112** as possible out of the main database (which might be corrupt) and write it
4113** into zNewDb.
4114*/
4115static void tryToClone(ShellState *p, const char *zNewDb){
4116  int rc;
4117  sqlite3 *newDb = 0;
4118  if( access(zNewDb,0)==0 ){
4119    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
4120    return;
4121  }
4122  rc = sqlite3_open(zNewDb, &newDb);
4123  if( rc ){
4124    utf8_printf(stderr, "Cannot create output database: %s\n",
4125            sqlite3_errmsg(newDb));
4126  }else{
4127    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
4128    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
4129    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
4130    tryToCloneSchema(p, newDb, "type!='table'", 0);
4131    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
4132    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4133  }
4134  sqlite3_close(newDb);
4135}
4136
4137/*
4138** Change the output file back to stdout.
4139**
4140** If the p->doXdgOpen flag is set, that means the output was being
4141** redirected to a temporary file named by p->zTempFile.  In that case,
4142** launch start/open/xdg-open on that temporary file.
4143*/
4144static void output_reset(ShellState *p){
4145  if( p->outfile[0]=='|' ){
4146#ifndef SQLITE_OMIT_POPEN
4147    pclose(p->out);
4148#endif
4149  }else{
4150    output_file_close(p->out);
4151#ifndef SQLITE_NOHAVE_SYSTEM
4152    if( p->doXdgOpen ){
4153      const char *zXdgOpenCmd =
4154#if defined(_WIN32)
4155      "start";
4156#elif defined(__APPLE__)
4157      "open";
4158#else
4159      "xdg-open";
4160#endif
4161      char *zCmd;
4162      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
4163      if( system(zCmd) ){
4164        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
4165      }
4166      sqlite3_free(zCmd);
4167      outputModePop(p);
4168      p->doXdgOpen = 0;
4169    }
4170#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
4171  }
4172  p->outfile[0] = 0;
4173  p->out = stdout;
4174}
4175
4176/*
4177** Run an SQL command and return the single integer result.
4178*/
4179static int db_int(ShellState *p, const char *zSql){
4180  sqlite3_stmt *pStmt;
4181  int res = 0;
4182  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4183  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
4184    res = sqlite3_column_int(pStmt,0);
4185  }
4186  sqlite3_finalize(pStmt);
4187  return res;
4188}
4189
4190/*
4191** Convert a 2-byte or 4-byte big-endian integer into a native integer
4192*/
4193static unsigned int get2byteInt(unsigned char *a){
4194  return (a[0]<<8) + a[1];
4195}
4196static unsigned int get4byteInt(unsigned char *a){
4197  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
4198}
4199
4200/*
4201** Implementation of the ".info" command.
4202**
4203** Return 1 on error, 2 to exit, and 0 otherwise.
4204*/
4205static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
4206  static const struct { const char *zName; int ofst; } aField[] = {
4207     { "file change counter:",  24  },
4208     { "database page count:",  28  },
4209     { "freelist page count:",  36  },
4210     { "schema cookie:",        40  },
4211     { "schema format:",        44  },
4212     { "default cache size:",   48  },
4213     { "autovacuum top root:",  52  },
4214     { "incremental vacuum:",   64  },
4215     { "text encoding:",        56  },
4216     { "user version:",         60  },
4217     { "application id:",       68  },
4218     { "software version:",     96  },
4219  };
4220  static const struct { const char *zName; const char *zSql; } aQuery[] = {
4221     { "number of tables:",
4222       "SELECT count(*) FROM %s WHERE type='table'" },
4223     { "number of indexes:",
4224       "SELECT count(*) FROM %s WHERE type='index'" },
4225     { "number of triggers:",
4226       "SELECT count(*) FROM %s WHERE type='trigger'" },
4227     { "number of views:",
4228       "SELECT count(*) FROM %s WHERE type='view'" },
4229     { "schema size:",
4230       "SELECT total(length(sql)) FROM %s" },
4231  };
4232  int i;
4233  char *zSchemaTab;
4234  char *zDb = nArg>=2 ? azArg[1] : "main";
4235  sqlite3_stmt *pStmt = 0;
4236  unsigned char aHdr[100];
4237  open_db(p, 0);
4238  if( p->db==0 ) return 1;
4239  sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
4240                     -1, &pStmt, 0);
4241  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
4242  if( sqlite3_step(pStmt)==SQLITE_ROW
4243   && sqlite3_column_bytes(pStmt,0)>100
4244  ){
4245    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
4246    sqlite3_finalize(pStmt);
4247  }else{
4248    raw_printf(stderr, "unable to read database header\n");
4249    sqlite3_finalize(pStmt);
4250    return 1;
4251  }
4252  i = get2byteInt(aHdr+16);
4253  if( i==1 ) i = 65536;
4254  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
4255  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
4256  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
4257  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
4258  for(i=0; i<ArraySize(aField); i++){
4259    int ofst = aField[i].ofst;
4260    unsigned int val = get4byteInt(aHdr + ofst);
4261    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
4262    switch( ofst ){
4263      case 56: {
4264        if( val==1 ) raw_printf(p->out, " (utf8)");
4265        if( val==2 ) raw_printf(p->out, " (utf16le)");
4266        if( val==3 ) raw_printf(p->out, " (utf16be)");
4267      }
4268    }
4269    raw_printf(p->out, "\n");
4270  }
4271  if( zDb==0 ){
4272    zSchemaTab = sqlite3_mprintf("main.sqlite_master");
4273  }else if( strcmp(zDb,"temp")==0 ){
4274    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
4275  }else{
4276    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
4277  }
4278  for(i=0; i<ArraySize(aQuery); i++){
4279    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
4280    int val = db_int(p, zSql);
4281    sqlite3_free(zSql);
4282    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
4283  }
4284  sqlite3_free(zSchemaTab);
4285  return 0;
4286}
4287
4288/*
4289** Print the current sqlite3_errmsg() value to stderr and return 1.
4290*/
4291static int shellDatabaseError(sqlite3 *db){
4292  const char *zErr = sqlite3_errmsg(db);
4293  utf8_printf(stderr, "Error: %s\n", zErr);
4294  return 1;
4295}
4296
4297/*
4298** Print an out-of-memory message to stderr and return 1.
4299*/
4300static int shellNomemError(void){
4301  raw_printf(stderr, "Error: out of memory\n");
4302  return 1;
4303}
4304
4305/*
4306** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
4307** if they match and FALSE (0) if they do not match.
4308**
4309** Globbing rules:
4310**
4311**      '*'       Matches any sequence of zero or more characters.
4312**
4313**      '?'       Matches exactly one character.
4314**
4315**     [...]      Matches one character from the enclosed list of
4316**                characters.
4317**
4318**     [^...]     Matches one character not in the enclosed list.
4319**
4320**      '#'       Matches any sequence of one or more digits with an
4321**                optional + or - sign in front
4322**
4323**      ' '       Any span of whitespace matches any other span of
4324**                whitespace.
4325**
4326** Extra whitespace at the end of z[] is ignored.
4327*/
4328static int testcase_glob(const char *zGlob, const char *z){
4329  int c, c2;
4330  int invert;
4331  int seen;
4332
4333  while( (c = (*(zGlob++)))!=0 ){
4334    if( IsSpace(c) ){
4335      if( !IsSpace(*z) ) return 0;
4336      while( IsSpace(*zGlob) ) zGlob++;
4337      while( IsSpace(*z) ) z++;
4338    }else if( c=='*' ){
4339      while( (c=(*(zGlob++))) == '*' || c=='?' ){
4340        if( c=='?' && (*(z++))==0 ) return 0;
4341      }
4342      if( c==0 ){
4343        return 1;
4344      }else if( c=='[' ){
4345        while( *z && testcase_glob(zGlob-1,z)==0 ){
4346          z++;
4347        }
4348        return (*z)!=0;
4349      }
4350      while( (c2 = (*(z++)))!=0 ){
4351        while( c2!=c ){
4352          c2 = *(z++);
4353          if( c2==0 ) return 0;
4354        }
4355        if( testcase_glob(zGlob,z) ) return 1;
4356      }
4357      return 0;
4358    }else if( c=='?' ){
4359      if( (*(z++))==0 ) return 0;
4360    }else if( c=='[' ){
4361      int prior_c = 0;
4362      seen = 0;
4363      invert = 0;
4364      c = *(z++);
4365      if( c==0 ) return 0;
4366      c2 = *(zGlob++);
4367      if( c2=='^' ){
4368        invert = 1;
4369        c2 = *(zGlob++);
4370      }
4371      if( c2==']' ){
4372        if( c==']' ) seen = 1;
4373        c2 = *(zGlob++);
4374      }
4375      while( c2 && c2!=']' ){
4376        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
4377          c2 = *(zGlob++);
4378          if( c>=prior_c && c<=c2 ) seen = 1;
4379          prior_c = 0;
4380        }else{
4381          if( c==c2 ){
4382            seen = 1;
4383          }
4384          prior_c = c2;
4385        }
4386        c2 = *(zGlob++);
4387      }
4388      if( c2==0 || (seen ^ invert)==0 ) return 0;
4389    }else if( c=='#' ){
4390      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
4391      if( !IsDigit(z[0]) ) return 0;
4392      z++;
4393      while( IsDigit(z[0]) ){ z++; }
4394    }else{
4395      if( c!=(*(z++)) ) return 0;
4396    }
4397  }
4398  while( IsSpace(*z) ){ z++; }
4399  return *z==0;
4400}
4401
4402
4403/*
4404** Compare the string as a command-line option with either one or two
4405** initial "-" characters.
4406*/
4407static int optionMatch(const char *zStr, const char *zOpt){
4408  if( zStr[0]!='-' ) return 0;
4409  zStr++;
4410  if( zStr[0]=='-' ) zStr++;
4411  return strcmp(zStr, zOpt)==0;
4412}
4413
4414/*
4415** Delete a file.
4416*/
4417int shellDeleteFile(const char *zFilename){
4418  int rc;
4419#ifdef _WIN32
4420  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
4421  rc = _wunlink(z);
4422  sqlite3_free(z);
4423#else
4424  rc = unlink(zFilename);
4425#endif
4426  return rc;
4427}
4428
4429/*
4430** Try to delete the temporary file (if there is one) and free the
4431** memory used to hold the name of the temp file.
4432*/
4433static void clearTempFile(ShellState *p){
4434  if( p->zTempFile==0 ) return;
4435  if( p->doXdgOpen ) return;
4436  if( shellDeleteFile(p->zTempFile) ) return;
4437  sqlite3_free(p->zTempFile);
4438  p->zTempFile = 0;
4439}
4440
4441/*
4442** Create a new temp file name with the given suffix.
4443*/
4444static void newTempFile(ShellState *p, const char *zSuffix){
4445  clearTempFile(p);
4446  sqlite3_free(p->zTempFile);
4447  p->zTempFile = 0;
4448  if( p->db ){
4449    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
4450  }
4451  if( p->zTempFile==0 ){
4452    sqlite3_uint64 r;
4453    sqlite3_randomness(sizeof(r), &r);
4454    p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
4455  }else{
4456    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
4457  }
4458  if( p->zTempFile==0 ){
4459    raw_printf(stderr, "out of memory\n");
4460    exit(1);
4461  }
4462}
4463
4464
4465/*
4466** The implementation of SQL scalar function fkey_collate_clause(), used
4467** by the ".lint fkey-indexes" command. This scalar function is always
4468** called with four arguments - the parent table name, the parent column name,
4469** the child table name and the child column name.
4470**
4471**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
4472**
4473** If either of the named tables or columns do not exist, this function
4474** returns an empty string. An empty string is also returned if both tables
4475** and columns exist but have the same default collation sequence. Or,
4476** if both exist but the default collation sequences are different, this
4477** function returns the string " COLLATE <parent-collation>", where
4478** <parent-collation> is the default collation sequence of the parent column.
4479*/
4480static void shellFkeyCollateClause(
4481  sqlite3_context *pCtx,
4482  int nVal,
4483  sqlite3_value **apVal
4484){
4485  sqlite3 *db = sqlite3_context_db_handle(pCtx);
4486  const char *zParent;
4487  const char *zParentCol;
4488  const char *zParentSeq;
4489  const char *zChild;
4490  const char *zChildCol;
4491  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
4492  int rc;
4493
4494  assert( nVal==4 );
4495  zParent = (const char*)sqlite3_value_text(apVal[0]);
4496  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
4497  zChild = (const char*)sqlite3_value_text(apVal[2]);
4498  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
4499
4500  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
4501  rc = sqlite3_table_column_metadata(
4502      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
4503  );
4504  if( rc==SQLITE_OK ){
4505    rc = sqlite3_table_column_metadata(
4506        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
4507    );
4508  }
4509
4510  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
4511    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
4512    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
4513    sqlite3_free(z);
4514  }
4515}
4516
4517
4518/*
4519** The implementation of dot-command ".lint fkey-indexes".
4520*/
4521static int lintFkeyIndexes(
4522  ShellState *pState,             /* Current shell tool state */
4523  char **azArg,                   /* Array of arguments passed to dot command */
4524  int nArg                        /* Number of entries in azArg[] */
4525){
4526  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
4527  FILE *out = pState->out;        /* Stream to write non-error output to */
4528  int bVerbose = 0;               /* If -verbose is present */
4529  int bGroupByParent = 0;         /* If -groupbyparent is present */
4530  int i;                          /* To iterate through azArg[] */
4531  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
4532  int rc;                         /* Return code */
4533  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
4534
4535  /*
4536  ** This SELECT statement returns one row for each foreign key constraint
4537  ** in the schema of the main database. The column values are:
4538  **
4539  ** 0. The text of an SQL statement similar to:
4540  **
4541  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
4542  **
4543  **    This SELECT is similar to the one that the foreign keys implementation
4544  **    needs to run internally on child tables. If there is an index that can
4545  **    be used to optimize this query, then it can also be used by the FK
4546  **    implementation to optimize DELETE or UPDATE statements on the parent
4547  **    table.
4548  **
4549  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
4550  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
4551  **    contains an index that can be used to optimize the query.
4552  **
4553  ** 2. Human readable text that describes the child table and columns. e.g.
4554  **
4555  **       "child_table(child_key1, child_key2)"
4556  **
4557  ** 3. Human readable text that describes the parent table and columns. e.g.
4558  **
4559  **       "parent_table(parent_key1, parent_key2)"
4560  **
4561  ** 4. A full CREATE INDEX statement for an index that could be used to
4562  **    optimize DELETE or UPDATE statements on the parent table. e.g.
4563  **
4564  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
4565  **
4566  ** 5. The name of the parent table.
4567  **
4568  ** These six values are used by the C logic below to generate the report.
4569  */
4570  const char *zSql =
4571  "SELECT "
4572    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
4573    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
4574    "  || fkey_collate_clause("
4575    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
4576    ", "
4577    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
4578    "  || group_concat('*=?', ' AND ') || ')'"
4579    ", "
4580    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
4581    ", "
4582    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
4583    ", "
4584    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
4585    "  || ' ON ' || quote(s.name) || '('"
4586    "  || group_concat(quote(f.[from]) ||"
4587    "        fkey_collate_clause("
4588    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
4589    "  || ');'"
4590    ", "
4591    "     f.[table] "
4592    "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
4593    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
4594    "GROUP BY s.name, f.id "
4595    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
4596  ;
4597  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
4598
4599  for(i=2; i<nArg; i++){
4600    int n = strlen30(azArg[i]);
4601    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
4602      bVerbose = 1;
4603    }
4604    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
4605      bGroupByParent = 1;
4606      zIndent = "    ";
4607    }
4608    else{
4609      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
4610          azArg[0], azArg[1]
4611      );
4612      return SQLITE_ERROR;
4613    }
4614  }
4615
4616  /* Register the fkey_collate_clause() SQL function */
4617  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
4618      0, shellFkeyCollateClause, 0, 0
4619  );
4620
4621
4622  if( rc==SQLITE_OK ){
4623    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
4624  }
4625  if( rc==SQLITE_OK ){
4626    sqlite3_bind_int(pSql, 1, bGroupByParent);
4627  }
4628
4629  if( rc==SQLITE_OK ){
4630    int rc2;
4631    char *zPrev = 0;
4632    while( SQLITE_ROW==sqlite3_step(pSql) ){
4633      int res = -1;
4634      sqlite3_stmt *pExplain = 0;
4635      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
4636      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
4637      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
4638      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
4639      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
4640      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
4641
4642      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
4643      if( rc!=SQLITE_OK ) break;
4644      if( SQLITE_ROW==sqlite3_step(pExplain) ){
4645        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
4646        res = (
4647              0==sqlite3_strglob(zGlob, zPlan)
4648           || 0==sqlite3_strglob(zGlobIPK, zPlan)
4649        );
4650      }
4651      rc = sqlite3_finalize(pExplain);
4652      if( rc!=SQLITE_OK ) break;
4653
4654      if( res<0 ){
4655        raw_printf(stderr, "Error: internal error");
4656        break;
4657      }else{
4658        if( bGroupByParent
4659        && (bVerbose || res==0)
4660        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
4661        ){
4662          raw_printf(out, "-- Parent table %s\n", zParent);
4663          sqlite3_free(zPrev);
4664          zPrev = sqlite3_mprintf("%s", zParent);
4665        }
4666
4667        if( res==0 ){
4668          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
4669        }else if( bVerbose ){
4670          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
4671              zIndent, zFrom, zTarget
4672          );
4673        }
4674      }
4675    }
4676    sqlite3_free(zPrev);
4677
4678    if( rc!=SQLITE_OK ){
4679      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4680    }
4681
4682    rc2 = sqlite3_finalize(pSql);
4683    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
4684      rc = rc2;
4685      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4686    }
4687  }else{
4688    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4689  }
4690
4691  return rc;
4692}
4693
4694/*
4695** Implementation of ".lint" dot command.
4696*/
4697static int lintDotCommand(
4698  ShellState *pState,             /* Current shell tool state */
4699  char **azArg,                   /* Array of arguments passed to dot command */
4700  int nArg                        /* Number of entries in azArg[] */
4701){
4702  int n;
4703  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
4704  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
4705  return lintFkeyIndexes(pState, azArg, nArg);
4706
4707 usage:
4708  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
4709  raw_printf(stderr, "Where sub-commands are:\n");
4710  raw_printf(stderr, "    fkey-indexes\n");
4711  return SQLITE_ERROR;
4712}
4713
4714#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
4715/*********************************************************************************
4716** The ".archive" or ".ar" command.
4717*/
4718static void shellPrepare(
4719  sqlite3 *db,
4720  int *pRc,
4721  const char *zSql,
4722  sqlite3_stmt **ppStmt
4723){
4724  *ppStmt = 0;
4725  if( *pRc==SQLITE_OK ){
4726    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
4727    if( rc!=SQLITE_OK ){
4728      raw_printf(stderr, "sql error: %s (%d)\n",
4729          sqlite3_errmsg(db), sqlite3_errcode(db)
4730      );
4731      *pRc = rc;
4732    }
4733  }
4734}
4735
4736static void shellPreparePrintf(
4737  sqlite3 *db,
4738  int *pRc,
4739  sqlite3_stmt **ppStmt,
4740  const char *zFmt,
4741  ...
4742){
4743  *ppStmt = 0;
4744  if( *pRc==SQLITE_OK ){
4745    va_list ap;
4746    char *z;
4747    va_start(ap, zFmt);
4748    z = sqlite3_vmprintf(zFmt, ap);
4749    if( z==0 ){
4750      *pRc = SQLITE_NOMEM;
4751    }else{
4752      shellPrepare(db, pRc, z, ppStmt);
4753      sqlite3_free(z);
4754    }
4755  }
4756}
4757
4758static void shellFinalize(
4759  int *pRc,
4760  sqlite3_stmt *pStmt
4761){
4762  if( pStmt ){
4763    sqlite3 *db = sqlite3_db_handle(pStmt);
4764    int rc = sqlite3_finalize(pStmt);
4765    if( *pRc==SQLITE_OK ){
4766      if( rc!=SQLITE_OK ){
4767        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
4768      }
4769      *pRc = rc;
4770    }
4771  }
4772}
4773
4774static void shellReset(
4775  int *pRc,
4776  sqlite3_stmt *pStmt
4777){
4778  int rc = sqlite3_reset(pStmt);
4779  if( *pRc==SQLITE_OK ){
4780    if( rc!=SQLITE_OK ){
4781      sqlite3 *db = sqlite3_db_handle(pStmt);
4782      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
4783    }
4784    *pRc = rc;
4785  }
4786}
4787/*
4788** Structure representing a single ".ar" command.
4789*/
4790typedef struct ArCommand ArCommand;
4791struct ArCommand {
4792  u8 eCmd;                        /* An AR_CMD_* value */
4793  u8 bVerbose;                    /* True if --verbose */
4794  u8 bZip;                        /* True if the archive is a ZIP */
4795  u8 bDryRun;                     /* True if --dry-run */
4796  u8 bAppend;                     /* True if --append */
4797  int nArg;                       /* Number of command arguments */
4798  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
4799  const char *zFile;              /* --file argument, or NULL */
4800  const char *zDir;               /* --directory argument, or NULL */
4801  char **azArg;                   /* Array of command arguments */
4802  ShellState *p;                  /* Shell state */
4803  sqlite3 *db;                    /* Database containing the archive */
4804};
4805
4806/*
4807** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
4808*/
4809static int arUsage(FILE *f){
4810  raw_printf(f,
4811"\n"
4812"Usage: .ar [OPTION...] [FILE...]\n"
4813"The .ar command manages sqlar archives.\n"
4814"\n"
4815"Examples:\n"
4816"  .ar -cf archive.sar foo bar    # Create archive.sar from files foo and bar\n"
4817"  .ar -tf archive.sar            # List members of archive.sar\n"
4818"  .ar -xvf archive.sar           # Verbosely extract files from archive.sar\n"
4819"\n"
4820"Each command line must feature exactly one command option:\n"
4821"  -c, --create               Create a new archive\n"
4822"  -u, --update               Update or add files to an existing archive\n"
4823"  -t, --list                 List contents of archive\n"
4824"  -x, --extract              Extract files from archive\n"
4825"\n"
4826"And zero or more optional options:\n"
4827"  -v, --verbose              Print each filename as it is processed\n"
4828"  -f FILE, --file FILE       Operate on archive FILE (default is current db)\n"
4829"  -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS\n"
4830"  -C DIR, --directory DIR    Change to directory DIR to read/extract files\n"
4831"  -n, --dryrun               Show the SQL that would have occurred\n"
4832"\n"
4833"See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
4834"\n"
4835);
4836  return SQLITE_ERROR;
4837}
4838
4839/*
4840** Print an error message for the .ar command to stderr and return
4841** SQLITE_ERROR.
4842*/
4843static int arErrorMsg(const char *zFmt, ...){
4844  va_list ap;
4845  char *z;
4846  va_start(ap, zFmt);
4847  z = sqlite3_vmprintf(zFmt, ap);
4848  va_end(ap);
4849  raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z);
4850  sqlite3_free(z);
4851  return SQLITE_ERROR;
4852}
4853
4854/*
4855** Values for ArCommand.eCmd.
4856*/
4857#define AR_CMD_CREATE       1
4858#define AR_CMD_EXTRACT      2
4859#define AR_CMD_LIST         3
4860#define AR_CMD_UPDATE       4
4861#define AR_CMD_HELP         5
4862
4863/*
4864** Other (non-command) switches.
4865*/
4866#define AR_SWITCH_VERBOSE     6
4867#define AR_SWITCH_FILE        7
4868#define AR_SWITCH_DIRECTORY   8
4869#define AR_SWITCH_APPEND      9
4870#define AR_SWITCH_DRYRUN     10
4871
4872static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
4873  switch( eSwitch ){
4874    case AR_CMD_CREATE:
4875    case AR_CMD_EXTRACT:
4876    case AR_CMD_LIST:
4877    case AR_CMD_UPDATE:
4878    case AR_CMD_HELP:
4879      if( pAr->eCmd ){
4880        return arErrorMsg("multiple command options");
4881      }
4882      pAr->eCmd = eSwitch;
4883      break;
4884
4885    case AR_SWITCH_DRYRUN:
4886      pAr->bDryRun = 1;
4887      break;
4888    case AR_SWITCH_VERBOSE:
4889      pAr->bVerbose = 1;
4890      break;
4891    case AR_SWITCH_APPEND:
4892      pAr->bAppend = 1;
4893      /* Fall thru into --file */
4894    case AR_SWITCH_FILE:
4895      pAr->zFile = zArg;
4896      break;
4897    case AR_SWITCH_DIRECTORY:
4898      pAr->zDir = zArg;
4899      break;
4900  }
4901
4902  return SQLITE_OK;
4903}
4904
4905/*
4906** Parse the command line for an ".ar" command. The results are written into
4907** structure (*pAr). SQLITE_OK is returned if the command line is parsed
4908** successfully, otherwise an error message is written to stderr and
4909** SQLITE_ERROR returned.
4910*/
4911static int arParseCommand(
4912  char **azArg,                   /* Array of arguments passed to dot command */
4913  int nArg,                       /* Number of entries in azArg[] */
4914  ArCommand *pAr                  /* Populate this object */
4915){
4916  struct ArSwitch {
4917    const char *zLong;
4918    char cShort;
4919    u8 eSwitch;
4920    u8 bArg;
4921  } aSwitch[] = {
4922    { "create",    'c', AR_CMD_CREATE,       0 },
4923    { "extract",   'x', AR_CMD_EXTRACT,      0 },
4924    { "list",      't', AR_CMD_LIST,         0 },
4925    { "update",    'u', AR_CMD_UPDATE,       0 },
4926    { "help",      'h', AR_CMD_HELP,         0 },
4927    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
4928    { "file",      'f', AR_SWITCH_FILE,      1 },
4929    { "append",    'a', AR_SWITCH_APPEND,    1 },
4930    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
4931    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
4932  };
4933  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
4934  struct ArSwitch *pEnd = &aSwitch[nSwitch];
4935
4936  if( nArg<=1 ){
4937    return arUsage(stderr);
4938  }else{
4939    char *z = azArg[1];
4940    memset(pAr, 0, sizeof(ArCommand));
4941
4942    if( z[0]!='-' ){
4943      /* Traditional style [tar] invocation */
4944      int i;
4945      int iArg = 2;
4946      for(i=0; z[i]; i++){
4947        const char *zArg = 0;
4948        struct ArSwitch *pOpt;
4949        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
4950          if( z[i]==pOpt->cShort ) break;
4951        }
4952        if( pOpt==pEnd ){
4953          return arErrorMsg("unrecognized option: %c", z[i]);
4954        }
4955        if( pOpt->bArg ){
4956          if( iArg>=nArg ){
4957            return arErrorMsg("option requires an argument: %c",z[i]);
4958          }
4959          zArg = azArg[iArg++];
4960        }
4961        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
4962      }
4963      pAr->nArg = nArg-iArg;
4964      if( pAr->nArg>0 ){
4965        pAr->azArg = &azArg[iArg];
4966      }
4967    }else{
4968      /* Non-traditional invocation */
4969      int iArg;
4970      for(iArg=1; iArg<nArg; iArg++){
4971        int n;
4972        z = azArg[iArg];
4973        if( z[0]!='-' ){
4974          /* All remaining command line words are command arguments. */
4975          pAr->azArg = &azArg[iArg];
4976          pAr->nArg = nArg-iArg;
4977          break;
4978        }
4979        n = strlen30(z);
4980
4981        if( z[1]!='-' ){
4982          int i;
4983          /* One or more short options */
4984          for(i=1; i<n; i++){
4985            const char *zArg = 0;
4986            struct ArSwitch *pOpt;
4987            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
4988              if( z[i]==pOpt->cShort ) break;
4989            }
4990            if( pOpt==pEnd ){
4991              return arErrorMsg("unrecognized option: %c\n", z[i]);
4992            }
4993            if( pOpt->bArg ){
4994              if( i<(n-1) ){
4995                zArg = &z[i+1];
4996                i = n;
4997              }else{
4998                if( iArg>=(nArg-1) ){
4999                  return arErrorMsg("option requires an argument: %c\n",z[i]);
5000                }
5001                zArg = azArg[++iArg];
5002              }
5003            }
5004            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
5005          }
5006        }else if( z[2]=='\0' ){
5007          /* A -- option, indicating that all remaining command line words
5008          ** are command arguments.  */
5009          pAr->azArg = &azArg[iArg+1];
5010          pAr->nArg = nArg-iArg-1;
5011          break;
5012        }else{
5013          /* A long option */
5014          const char *zArg = 0;             /* Argument for option, if any */
5015          struct ArSwitch *pMatch = 0;      /* Matching option */
5016          struct ArSwitch *pOpt;            /* Iterator */
5017          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5018            const char *zLong = pOpt->zLong;
5019            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
5020              if( pMatch ){
5021                return arErrorMsg("ambiguous option: %s",z);
5022              }else{
5023                pMatch = pOpt;
5024              }
5025            }
5026          }
5027
5028          if( pMatch==0 ){
5029            return arErrorMsg("unrecognized option: %s", z);
5030          }
5031          if( pMatch->bArg ){
5032            if( iArg>=(nArg-1) ){
5033              return arErrorMsg("option requires an argument: %s", z);
5034            }
5035            zArg = azArg[++iArg];
5036          }
5037          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
5038        }
5039      }
5040    }
5041  }
5042
5043  return SQLITE_OK;
5044}
5045
5046/*
5047** This function assumes that all arguments within the ArCommand.azArg[]
5048** array refer to archive members, as for the --extract or --list commands.
5049** It checks that each of them are present. If any specified file is not
5050** present in the archive, an error is printed to stderr and an error
5051** code returned. Otherwise, if all specified arguments are present in
5052** the archive, SQLITE_OK is returned.
5053**
5054** This function strips any trailing '/' characters from each argument.
5055** This is consistent with the way the [tar] command seems to work on
5056** Linux.
5057*/
5058static int arCheckEntries(ArCommand *pAr){
5059  int rc = SQLITE_OK;
5060  if( pAr->nArg ){
5061    int i, j;
5062    sqlite3_stmt *pTest = 0;
5063
5064    shellPreparePrintf(pAr->db, &rc, &pTest,
5065        "SELECT name FROM %s WHERE name=$name",
5066        pAr->zSrcTable
5067    );
5068    j = sqlite3_bind_parameter_index(pTest, "$name");
5069    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
5070      char *z = pAr->azArg[i];
5071      int n = strlen30(z);
5072      int bOk = 0;
5073      while( n>0 && z[n-1]=='/' ) n--;
5074      z[n] = '\0';
5075      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
5076      if( SQLITE_ROW==sqlite3_step(pTest) ){
5077        bOk = 1;
5078      }
5079      shellReset(&rc, pTest);
5080      if( rc==SQLITE_OK && bOk==0 ){
5081        utf8_printf(stderr, "not found in archive: %s\n", z);
5082        rc = SQLITE_ERROR;
5083      }
5084    }
5085    shellFinalize(&rc, pTest);
5086  }
5087  return rc;
5088}
5089
5090/*
5091** Format a WHERE clause that can be used against the "sqlar" table to
5092** identify all archive members that match the command arguments held
5093** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
5094** The caller is responsible for eventually calling sqlite3_free() on
5095** any non-NULL (*pzWhere) value.
5096*/
5097static void arWhereClause(
5098  int *pRc,
5099  ArCommand *pAr,
5100  char **pzWhere                  /* OUT: New WHERE clause */
5101){
5102  char *zWhere = 0;
5103  if( *pRc==SQLITE_OK ){
5104    if( pAr->nArg==0 ){
5105      zWhere = sqlite3_mprintf("1");
5106    }else{
5107      int i;
5108      const char *zSep = "";
5109      for(i=0; i<pAr->nArg; i++){
5110        const char *z = pAr->azArg[i];
5111        zWhere = sqlite3_mprintf(
5112          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
5113          zWhere, zSep, z, strlen30(z)+1, z
5114        );
5115        if( zWhere==0 ){
5116          *pRc = SQLITE_NOMEM;
5117          break;
5118        }
5119        zSep = " OR ";
5120      }
5121    }
5122  }
5123  *pzWhere = zWhere;
5124}
5125
5126/*
5127** Implementation of .ar "lisT" command.
5128*/
5129static int arListCommand(ArCommand *pAr){
5130  const char *zSql = "SELECT %s FROM %s WHERE %s";
5131  const char *azCols[] = {
5132    "name",
5133    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
5134  };
5135
5136  char *zWhere = 0;
5137  sqlite3_stmt *pSql = 0;
5138  int rc;
5139
5140  rc = arCheckEntries(pAr);
5141  arWhereClause(&rc, pAr, &zWhere);
5142
5143  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
5144                     pAr->zSrcTable, zWhere);
5145  if( pAr->bDryRun ){
5146    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5147  }else{
5148    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5149      if( pAr->bVerbose ){
5150        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
5151            sqlite3_column_text(pSql, 0),
5152            sqlite3_column_int(pSql, 1),
5153            sqlite3_column_text(pSql, 2),
5154            sqlite3_column_text(pSql, 3)
5155        );
5156      }else{
5157        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5158      }
5159    }
5160  }
5161  shellFinalize(&rc, pSql);
5162  return rc;
5163}
5164
5165
5166/*
5167** Implementation of .ar "eXtract" command.
5168*/
5169static int arExtractCommand(ArCommand *pAr){
5170  const char *zSql1 =
5171    "SELECT "
5172    " ($dir || name),"
5173    " writefile(($dir || name), %s, mode, mtime) "
5174    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
5175
5176  const char *azExtraArg[] = {
5177    "sqlar_uncompress(data, sz)",
5178    "data"
5179  };
5180
5181  sqlite3_stmt *pSql = 0;
5182  int rc = SQLITE_OK;
5183  char *zDir = 0;
5184  char *zWhere = 0;
5185  int i, j;
5186
5187  /* If arguments are specified, check that they actually exist within
5188  ** the archive before proceeding. And formulate a WHERE clause to
5189  ** match them.  */
5190  rc = arCheckEntries(pAr);
5191  arWhereClause(&rc, pAr, &zWhere);
5192
5193  if( rc==SQLITE_OK ){
5194    if( pAr->zDir ){
5195      zDir = sqlite3_mprintf("%s/", pAr->zDir);
5196    }else{
5197      zDir = sqlite3_mprintf("");
5198    }
5199    if( zDir==0 ) rc = SQLITE_NOMEM;
5200  }
5201
5202  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
5203      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
5204  );
5205
5206  if( rc==SQLITE_OK ){
5207    j = sqlite3_bind_parameter_index(pSql, "$dir");
5208    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
5209
5210    /* Run the SELECT statement twice. The first time, writefile() is called
5211    ** for all archive members that should be extracted. The second time,
5212    ** only for the directories. This is because the timestamps for
5213    ** extracted directories must be reset after they are populated (as
5214    ** populating them changes the timestamp).  */
5215    for(i=0; i<2; i++){
5216      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
5217      sqlite3_bind_int(pSql, j, i);
5218      if( pAr->bDryRun ){
5219        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5220      }else{
5221        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5222          if( i==0 && pAr->bVerbose ){
5223            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5224          }
5225        }
5226      }
5227      shellReset(&rc, pSql);
5228    }
5229    shellFinalize(&rc, pSql);
5230  }
5231
5232  sqlite3_free(zDir);
5233  sqlite3_free(zWhere);
5234  return rc;
5235}
5236
5237/*
5238** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
5239*/
5240static int arExecSql(ArCommand *pAr, const char *zSql){
5241  int rc;
5242  if( pAr->bDryRun ){
5243    utf8_printf(pAr->p->out, "%s\n", zSql);
5244    rc = SQLITE_OK;
5245  }else{
5246    char *zErr = 0;
5247    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
5248    if( zErr ){
5249      utf8_printf(stdout, "ERROR: %s\n", zErr);
5250      sqlite3_free(zErr);
5251    }
5252  }
5253  return rc;
5254}
5255
5256
5257/*
5258** Implementation of .ar "create" and "update" commands.
5259**
5260** Create the "sqlar" table in the database if it does not already exist.
5261** Then add each file in the azFile[] array to the archive. Directories
5262** are added recursively. If argument bVerbose is non-zero, a message is
5263** printed on stdout for each file archived.
5264**
5265** The create command is the same as update, except that it drops
5266** any existing "sqlar" table before beginning.
5267*/
5268static int arCreateOrUpdateCommand(
5269  ArCommand *pAr,                 /* Command arguments and options */
5270  int bUpdate                     /* true for a --create.  false for --update */
5271){
5272  const char *zCreate =
5273      "CREATE TABLE IF NOT EXISTS sqlar(\n"
5274      "  name TEXT PRIMARY KEY,  -- name of the file\n"
5275      "  mode INT,               -- access permissions\n"
5276      "  mtime INT,              -- last modification time\n"
5277      "  sz INT,                 -- original file size\n"
5278      "  data BLOB               -- compressed content\n"
5279      ")";
5280  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
5281  const char *zInsertFmt[2] = {
5282     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
5283     "  SELECT\n"
5284     "    %s,\n"
5285     "    mode,\n"
5286     "    mtime,\n"
5287     "    CASE substr(lsmode(mode),1,1)\n"
5288     "      WHEN '-' THEN length(data)\n"
5289     "      WHEN 'd' THEN 0\n"
5290     "      ELSE -1 END,\n"
5291     "    sqlar_compress(data)\n"
5292     "  FROM fsdir(%Q,%Q)\n"
5293     "  WHERE lsmode(mode) NOT LIKE '?%%';",
5294     "REPLACE INTO %s(name,mode,mtime,data)\n"
5295     "  SELECT\n"
5296     "    %s,\n"
5297     "    mode,\n"
5298     "    mtime,\n"
5299     "    data\n"
5300     "  FROM fsdir(%Q,%Q)\n"
5301     "  WHERE lsmode(mode) NOT LIKE '?%%';"
5302  };
5303  int i;                          /* For iterating through azFile[] */
5304  int rc;                         /* Return code */
5305  const char *zTab = 0;           /* SQL table into which to insert */
5306  char *zSql;
5307  char zTemp[50];
5308
5309  arExecSql(pAr, "PRAGMA page_size=512");
5310  rc = arExecSql(pAr, "SAVEPOINT ar;");
5311  if( rc!=SQLITE_OK ) return rc;
5312  zTemp[0] = 0;
5313  if( pAr->bZip ){
5314    /* Initialize the zipfile virtual table, if necessary */
5315    if( pAr->zFile ){
5316      sqlite3_uint64 r;
5317      sqlite3_randomness(sizeof(r),&r);
5318      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
5319      zTab = zTemp;
5320      zSql = sqlite3_mprintf(
5321         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
5322         zTab, pAr->zFile
5323      );
5324      rc = arExecSql(pAr, zSql);
5325      sqlite3_free(zSql);
5326    }else{
5327      zTab = "zip";
5328    }
5329  }else{
5330    /* Initialize the table for an SQLAR */
5331    zTab = "sqlar";
5332    if( bUpdate==0 ){
5333      rc = arExecSql(pAr, zDrop);
5334      if( rc!=SQLITE_OK ) goto end_ar_transaction;
5335    }
5336    rc = arExecSql(pAr, zCreate);
5337  }
5338  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
5339    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
5340        pAr->bVerbose ? "shell_putsnl(name)" : "name",
5341        pAr->azArg[i], pAr->zDir);
5342    rc = arExecSql(pAr, zSql2);
5343    sqlite3_free(zSql2);
5344  }
5345end_ar_transaction:
5346  if( rc!=SQLITE_OK ){
5347    arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
5348  }else{
5349    rc = arExecSql(pAr, "RELEASE ar;");
5350    if( pAr->bZip && pAr->zFile ){
5351      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
5352      arExecSql(pAr, zSql);
5353      sqlite3_free(zSql);
5354    }
5355  }
5356  return rc;
5357}
5358
5359/*
5360** Implementation of ".ar" dot command.
5361*/
5362static int arDotCommand(
5363  ShellState *pState,             /* Current shell tool state */
5364  char **azArg,                   /* Array of arguments passed to dot command */
5365  int nArg                        /* Number of entries in azArg[] */
5366){
5367  ArCommand cmd;
5368  int rc;
5369  memset(&cmd, 0, sizeof(cmd));
5370  rc = arParseCommand(azArg, nArg, &cmd);
5371  if( rc==SQLITE_OK ){
5372    int eDbType = SHELL_OPEN_UNSPEC;
5373    cmd.p = pState;
5374    cmd.db = pState->db;
5375    if( cmd.zFile ){
5376      eDbType = deduceDatabaseType(cmd.zFile, 1);
5377    }else{
5378      eDbType = pState->openMode;
5379    }
5380    if( eDbType==SHELL_OPEN_ZIPFILE ){
5381      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
5382        if( cmd.zFile==0 ){
5383          cmd.zSrcTable = sqlite3_mprintf("zip");
5384        }else{
5385          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
5386        }
5387      }
5388      cmd.bZip = 1;
5389    }else if( cmd.zFile ){
5390      int flags;
5391      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
5392      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
5393        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
5394      }else{
5395        flags = SQLITE_OPEN_READONLY;
5396      }
5397      cmd.db = 0;
5398      if( cmd.bDryRun ){
5399        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
5400             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
5401      }
5402      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
5403             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
5404      if( rc!=SQLITE_OK ){
5405        utf8_printf(stderr, "cannot open file: %s (%s)\n",
5406            cmd.zFile, sqlite3_errmsg(cmd.db)
5407        );
5408        goto end_ar_command;
5409      }
5410      sqlite3_fileio_init(cmd.db, 0, 0);
5411      sqlite3_sqlar_init(cmd.db, 0, 0);
5412      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
5413                              shellPutsFunc, 0, 0);
5414
5415    }
5416    if( cmd.zSrcTable==0 && cmd.bZip==0 ){
5417      if( cmd.eCmd!=AR_CMD_CREATE
5418       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
5419      ){
5420        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
5421        rc = SQLITE_ERROR;
5422        goto end_ar_command;
5423      }
5424      cmd.zSrcTable = sqlite3_mprintf("sqlar");
5425    }
5426
5427    switch( cmd.eCmd ){
5428      case AR_CMD_CREATE:
5429        rc = arCreateOrUpdateCommand(&cmd, 0);
5430        break;
5431
5432      case AR_CMD_EXTRACT:
5433        rc = arExtractCommand(&cmd);
5434        break;
5435
5436      case AR_CMD_LIST:
5437        rc = arListCommand(&cmd);
5438        break;
5439
5440      case AR_CMD_HELP:
5441        arUsage(pState->out);
5442        break;
5443
5444      default:
5445        assert( cmd.eCmd==AR_CMD_UPDATE );
5446        rc = arCreateOrUpdateCommand(&cmd, 1);
5447        break;
5448    }
5449  }
5450end_ar_command:
5451  if( cmd.db!=pState->db ){
5452    sqlite3_close(cmd.db);
5453  }
5454  sqlite3_free(cmd.zSrcTable);
5455
5456  return rc;
5457}
5458/* End of the ".archive" or ".ar" command logic
5459**********************************************************************************/
5460#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
5461
5462
5463/*
5464** If an input line begins with "." then invoke this routine to
5465** process that line.
5466**
5467** Return 1 on error, 2 to exit, and 0 otherwise.
5468*/
5469static int do_meta_command(char *zLine, ShellState *p){
5470  int h = 1;
5471  int nArg = 0;
5472  int n, c;
5473  int rc = 0;
5474  char *azArg[50];
5475
5476#ifndef SQLITE_OMIT_VIRTUALTABLE
5477  if( p->expert.pExpert ){
5478    expertFinish(p, 1, 0);
5479  }
5480#endif
5481
5482  /* Parse the input line into tokens.
5483  */
5484  while( zLine[h] && nArg<ArraySize(azArg) ){
5485    while( IsSpace(zLine[h]) ){ h++; }
5486    if( zLine[h]==0 ) break;
5487    if( zLine[h]=='\'' || zLine[h]=='"' ){
5488      int delim = zLine[h++];
5489      azArg[nArg++] = &zLine[h];
5490      while( zLine[h] && zLine[h]!=delim ){
5491        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
5492        h++;
5493      }
5494      if( zLine[h]==delim ){
5495        zLine[h++] = 0;
5496      }
5497      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
5498    }else{
5499      azArg[nArg++] = &zLine[h];
5500      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
5501      if( zLine[h] ) zLine[h++] = 0;
5502      resolve_backslashes(azArg[nArg-1]);
5503    }
5504  }
5505
5506  /* Process the input line.
5507  */
5508  if( nArg==0 ) return 0; /* no tokens, no error */
5509  n = strlen30(azArg[0]);
5510  c = azArg[0][0];
5511  clearTempFile(p);
5512
5513#ifndef SQLITE_OMIT_AUTHORIZATION
5514  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
5515    if( nArg!=2 ){
5516      raw_printf(stderr, "Usage: .auth ON|OFF\n");
5517      rc = 1;
5518      goto meta_command_exit;
5519    }
5520    open_db(p, 0);
5521    if( booleanValue(azArg[1]) ){
5522      sqlite3_set_authorizer(p->db, shellAuth, p);
5523    }else{
5524      sqlite3_set_authorizer(p->db, 0, 0);
5525    }
5526  }else
5527#endif
5528
5529#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5530  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
5531    open_db(p, 0);
5532    rc = arDotCommand(p, azArg, nArg);
5533  }else
5534#endif
5535
5536  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
5537   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
5538  ){
5539    const char *zDestFile = 0;
5540    const char *zDb = 0;
5541    sqlite3 *pDest;
5542    sqlite3_backup *pBackup;
5543    int j;
5544    for(j=1; j<nArg; j++){
5545      const char *z = azArg[j];
5546      if( z[0]=='-' ){
5547        while( z[0]=='-' ) z++;
5548        /* No options to process at this time */
5549        {
5550          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
5551          return 1;
5552        }
5553      }else if( zDestFile==0 ){
5554        zDestFile = azArg[j];
5555      }else if( zDb==0 ){
5556        zDb = zDestFile;
5557        zDestFile = azArg[j];
5558      }else{
5559        raw_printf(stderr, "too many arguments to .backup\n");
5560        return 1;
5561      }
5562    }
5563    if( zDestFile==0 ){
5564      raw_printf(stderr, "missing FILENAME argument on .backup\n");
5565      return 1;
5566    }
5567    if( zDb==0 ) zDb = "main";
5568    rc = sqlite3_open(zDestFile, &pDest);
5569    if( rc!=SQLITE_OK ){
5570      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
5571      sqlite3_close(pDest);
5572      return 1;
5573    }
5574    open_db(p, 0);
5575    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
5576    if( pBackup==0 ){
5577      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
5578      sqlite3_close(pDest);
5579      return 1;
5580    }
5581    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
5582    sqlite3_backup_finish(pBackup);
5583    if( rc==SQLITE_DONE ){
5584      rc = 0;
5585    }else{
5586      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
5587      rc = 1;
5588    }
5589    sqlite3_close(pDest);
5590  }else
5591
5592  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
5593    if( nArg==2 ){
5594      bail_on_error = booleanValue(azArg[1]);
5595    }else{
5596      raw_printf(stderr, "Usage: .bail on|off\n");
5597      rc = 1;
5598    }
5599  }else
5600
5601  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
5602    if( nArg==2 ){
5603      if( booleanValue(azArg[1]) ){
5604        setBinaryMode(p->out, 1);
5605      }else{
5606        setTextMode(p->out, 1);
5607      }
5608    }else{
5609      raw_printf(stderr, "Usage: .binary on|off\n");
5610      rc = 1;
5611    }
5612  }else
5613
5614  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
5615    if( nArg==2 ){
5616#if defined(_WIN32) || defined(WIN32)
5617      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
5618      rc = !SetCurrentDirectoryW(z);
5619      sqlite3_free(z);
5620#else
5621      rc = chdir(azArg[1]);
5622#endif
5623      if( rc ){
5624        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
5625        rc = 1;
5626      }
5627    }else{
5628      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
5629      rc = 1;
5630    }
5631  }else
5632
5633  /* The undocumented ".breakpoint" command causes a call to the no-op
5634  ** routine named test_breakpoint().
5635  */
5636  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
5637    test_breakpoint();
5638  }else
5639
5640  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
5641    if( nArg==2 ){
5642      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
5643    }else{
5644      raw_printf(stderr, "Usage: .changes on|off\n");
5645      rc = 1;
5646    }
5647  }else
5648
5649  /* Cancel output redirection, if it is currently set (by .testcase)
5650  ** Then read the content of the testcase-out.txt file and compare against
5651  ** azArg[1].  If there are differences, report an error and exit.
5652  */
5653  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
5654    char *zRes = 0;
5655    output_reset(p);
5656    if( nArg!=2 ){
5657      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
5658      rc = 2;
5659    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
5660      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
5661      rc = 2;
5662    }else if( testcase_glob(azArg[1],zRes)==0 ){
5663      utf8_printf(stderr,
5664                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
5665                 p->zTestcase, azArg[1], zRes);
5666      rc = 1;
5667    }else{
5668      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
5669      p->nCheck++;
5670    }
5671    sqlite3_free(zRes);
5672  }else
5673
5674  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
5675    if( nArg==2 ){
5676      tryToClone(p, azArg[1]);
5677    }else{
5678      raw_printf(stderr, "Usage: .clone FILENAME\n");
5679      rc = 1;
5680    }
5681  }else
5682
5683  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
5684    ShellState data;
5685    char *zErrMsg = 0;
5686    open_db(p, 0);
5687    memcpy(&data, p, sizeof(data));
5688    data.showHeader = 0;
5689    data.cMode = data.mode = MODE_List;
5690    sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
5691    data.cnt = 0;
5692    sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
5693                 callback, &data, &zErrMsg);
5694    if( zErrMsg ){
5695      utf8_printf(stderr,"Error: %s\n", zErrMsg);
5696      sqlite3_free(zErrMsg);
5697      rc = 1;
5698    }
5699  }else
5700
5701  if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
5702    rc = shell_dbinfo_command(p, nArg, azArg);
5703  }else
5704
5705  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
5706    const char *zLike = 0;
5707    int i;
5708    int savedShowHeader = p->showHeader;
5709    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
5710    for(i=1; i<nArg; i++){
5711      if( azArg[i][0]=='-' ){
5712        const char *z = azArg[i]+1;
5713        if( z[0]=='-' ) z++;
5714        if( strcmp(z,"preserve-rowids")==0 ){
5715#ifdef SQLITE_OMIT_VIRTUALTABLE
5716          raw_printf(stderr, "The --preserve-rowids option is not compatible"
5717                             " with SQLITE_OMIT_VIRTUALTABLE\n");
5718          rc = 1;
5719          goto meta_command_exit;
5720#else
5721          ShellSetFlag(p, SHFLG_PreserveRowid);
5722#endif
5723        }else
5724        if( strcmp(z,"newlines")==0 ){
5725          ShellSetFlag(p, SHFLG_Newlines);
5726        }else
5727        {
5728          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
5729          rc = 1;
5730          goto meta_command_exit;
5731        }
5732      }else if( zLike ){
5733        raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
5734                           "?--newlines? ?LIKE-PATTERN?\n");
5735        rc = 1;
5736        goto meta_command_exit;
5737      }else{
5738        zLike = azArg[i];
5739      }
5740    }
5741    open_db(p, 0);
5742    /* When playing back a "dump", the content might appear in an order
5743    ** which causes immediate foreign key constraints to be violated.
5744    ** So disable foreign-key constraint enforcement to prevent problems. */
5745    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
5746    raw_printf(p->out, "BEGIN TRANSACTION;\n");
5747    p->writableSchema = 0;
5748    p->showHeader = 0;
5749    /* Set writable_schema=ON since doing so forces SQLite to initialize
5750    ** as much of the schema as it can even if the sqlite_master table is
5751    ** corrupt. */
5752    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
5753    p->nErr = 0;
5754    if( zLike==0 ){
5755      run_schema_dump_query(p,
5756        "SELECT name, type, sql FROM sqlite_master "
5757        "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
5758      );
5759      run_schema_dump_query(p,
5760        "SELECT name, type, sql FROM sqlite_master "
5761        "WHERE name=='sqlite_sequence'"
5762      );
5763      run_table_dump_query(p,
5764        "SELECT sql FROM sqlite_master "
5765        "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
5766      );
5767    }else{
5768      char *zSql;
5769      zSql = sqlite3_mprintf(
5770        "SELECT name, type, sql FROM sqlite_master "
5771        "WHERE tbl_name LIKE %Q AND type=='table'"
5772        "  AND sql NOT NULL", zLike);
5773      run_schema_dump_query(p,zSql);
5774      sqlite3_free(zSql);
5775      zSql = sqlite3_mprintf(
5776        "SELECT sql FROM sqlite_master "
5777        "WHERE sql NOT NULL"
5778        "  AND type IN ('index','trigger','view')"
5779        "  AND tbl_name LIKE %Q", zLike);
5780      run_table_dump_query(p, zSql, 0);
5781      sqlite3_free(zSql);
5782    }
5783    if( p->writableSchema ){
5784      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
5785      p->writableSchema = 0;
5786    }
5787    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5788    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
5789    raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
5790    p->showHeader = savedShowHeader;
5791  }else
5792
5793  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
5794    if( nArg==2 ){
5795      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
5796    }else{
5797      raw_printf(stderr, "Usage: .echo on|off\n");
5798      rc = 1;
5799    }
5800  }else
5801
5802  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
5803    if( nArg==2 ){
5804      if( strcmp(azArg[1],"full")==0 ){
5805        p->autoEQP = AUTOEQP_full;
5806      }else if( strcmp(azArg[1],"trigger")==0 ){
5807        p->autoEQP = AUTOEQP_trigger;
5808      }else{
5809        p->autoEQP = (u8)booleanValue(azArg[1]);
5810      }
5811    }else{
5812      raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
5813      rc = 1;
5814    }
5815  }else
5816
5817  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
5818    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
5819    rc = 2;
5820  }else
5821
5822  /* The ".explain" command is automatic now.  It is largely pointless.  It
5823  ** retained purely for backwards compatibility */
5824  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
5825    int val = 1;
5826    if( nArg>=2 ){
5827      if( strcmp(azArg[1],"auto")==0 ){
5828        val = 99;
5829      }else{
5830        val =  booleanValue(azArg[1]);
5831      }
5832    }
5833    if( val==1 && p->mode!=MODE_Explain ){
5834      p->normalMode = p->mode;
5835      p->mode = MODE_Explain;
5836      p->autoExplain = 0;
5837    }else if( val==0 ){
5838      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
5839      p->autoExplain = 0;
5840    }else if( val==99 ){
5841      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
5842      p->autoExplain = 1;
5843    }
5844  }else
5845
5846#ifndef SQLITE_OMIT_VIRTUALTABLE
5847  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
5848    open_db(p, 0);
5849    expertDotCommand(p, azArg, nArg);
5850  }else
5851#endif
5852
5853  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
5854    ShellState data;
5855    char *zErrMsg = 0;
5856    int doStats = 0;
5857    memcpy(&data, p, sizeof(data));
5858    data.showHeader = 0;
5859    data.cMode = data.mode = MODE_Semi;
5860    if( nArg==2 && optionMatch(azArg[1], "indent") ){
5861      data.cMode = data.mode = MODE_Pretty;
5862      nArg = 1;
5863    }
5864    if( nArg!=1 ){
5865      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
5866      rc = 1;
5867      goto meta_command_exit;
5868    }
5869    open_db(p, 0);
5870    rc = sqlite3_exec(p->db,
5871       "SELECT sql FROM"
5872       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
5873       "     FROM sqlite_master UNION ALL"
5874       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
5875       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
5876       "ORDER BY rowid",
5877       callback, &data, &zErrMsg
5878    );
5879    if( rc==SQLITE_OK ){
5880      sqlite3_stmt *pStmt;
5881      rc = sqlite3_prepare_v2(p->db,
5882               "SELECT rowid FROM sqlite_master"
5883               " WHERE name GLOB 'sqlite_stat[134]'",
5884               -1, &pStmt, 0);
5885      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
5886      sqlite3_finalize(pStmt);
5887    }
5888    if( doStats==0 ){
5889      raw_printf(p->out, "/* No STAT tables available */\n");
5890    }else{
5891      raw_printf(p->out, "ANALYZE sqlite_master;\n");
5892      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
5893                   callback, &data, &zErrMsg);
5894      data.cMode = data.mode = MODE_Insert;
5895      data.zDestTable = "sqlite_stat1";
5896      shell_exec(p, "SELECT * FROM sqlite_stat1", &zErrMsg);
5897      data.zDestTable = "sqlite_stat3";
5898      shell_exec(p, "SELECT * FROM sqlite_stat3", &zErrMsg);
5899      data.zDestTable = "sqlite_stat4";
5900      shell_exec(p, "SELECT * FROM sqlite_stat4", &zErrMsg);
5901      raw_printf(p->out, "ANALYZE sqlite_master;\n");
5902    }
5903  }else
5904
5905  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
5906    if( nArg==2 ){
5907      p->showHeader = booleanValue(azArg[1]);
5908    }else{
5909      raw_printf(stderr, "Usage: .headers on|off\n");
5910      rc = 1;
5911    }
5912  }else
5913
5914  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
5915    utf8_printf(p->out, "%s", zHelp);
5916  }else
5917
5918  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
5919    char *zTable;               /* Insert data into this table */
5920    char *zFile;                /* Name of file to extra content from */
5921    sqlite3_stmt *pStmt = NULL; /* A statement */
5922    int nCol;                   /* Number of columns in the table */
5923    int nByte;                  /* Number of bytes in an SQL string */
5924    int i, j;                   /* Loop counters */
5925    int needCommit;             /* True to COMMIT or ROLLBACK at end */
5926    int nSep;                   /* Number of bytes in p->colSeparator[] */
5927    char *zSql;                 /* An SQL statement */
5928    ImportCtx sCtx;             /* Reader context */
5929    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
5930    int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
5931
5932    if( nArg!=3 ){
5933      raw_printf(stderr, "Usage: .import FILE TABLE\n");
5934      goto meta_command_exit;
5935    }
5936    zFile = azArg[1];
5937    zTable = azArg[2];
5938    seenInterrupt = 0;
5939    memset(&sCtx, 0, sizeof(sCtx));
5940    open_db(p, 0);
5941    nSep = strlen30(p->colSeparator);
5942    if( nSep==0 ){
5943      raw_printf(stderr,
5944                 "Error: non-null column separator required for import\n");
5945      return 1;
5946    }
5947    if( nSep>1 ){
5948      raw_printf(stderr, "Error: multi-character column separators not allowed"
5949                      " for import\n");
5950      return 1;
5951    }
5952    nSep = strlen30(p->rowSeparator);
5953    if( nSep==0 ){
5954      raw_printf(stderr, "Error: non-null row separator required for import\n");
5955      return 1;
5956    }
5957    if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
5958      /* When importing CSV (only), if the row separator is set to the
5959      ** default output row separator, change it to the default input
5960      ** row separator.  This avoids having to maintain different input
5961      ** and output row separators. */
5962      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
5963      nSep = strlen30(p->rowSeparator);
5964    }
5965    if( nSep>1 ){
5966      raw_printf(stderr, "Error: multi-character row separators not allowed"
5967                      " for import\n");
5968      return 1;
5969    }
5970    sCtx.zFile = zFile;
5971    sCtx.nLine = 1;
5972    if( sCtx.zFile[0]=='|' ){
5973#ifdef SQLITE_OMIT_POPEN
5974      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
5975      return 1;
5976#else
5977      sCtx.in = popen(sCtx.zFile+1, "r");
5978      sCtx.zFile = "<pipe>";
5979      xCloser = pclose;
5980#endif
5981    }else{
5982      sCtx.in = fopen(sCtx.zFile, "rb");
5983      xCloser = fclose;
5984    }
5985    if( p->mode==MODE_Ascii ){
5986      xRead = ascii_read_one_field;
5987    }else{
5988      xRead = csv_read_one_field;
5989    }
5990    if( sCtx.in==0 ){
5991      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5992      return 1;
5993    }
5994    sCtx.cColSep = p->colSeparator[0];
5995    sCtx.cRowSep = p->rowSeparator[0];
5996    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
5997    if( zSql==0 ){
5998      raw_printf(stderr, "Error: out of memory\n");
5999      xCloser(sCtx.in);
6000      return 1;
6001    }
6002    nByte = strlen30(zSql);
6003    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6004    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
6005    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
6006      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
6007      char cSep = '(';
6008      while( xRead(&sCtx) ){
6009        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
6010        cSep = ',';
6011        if( sCtx.cTerm!=sCtx.cColSep ) break;
6012      }
6013      if( cSep=='(' ){
6014        sqlite3_free(zCreate);
6015        sqlite3_free(sCtx.z);
6016        xCloser(sCtx.in);
6017        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
6018        return 1;
6019      }
6020      zCreate = sqlite3_mprintf("%z\n)", zCreate);
6021      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
6022      sqlite3_free(zCreate);
6023      if( rc ){
6024        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
6025                sqlite3_errmsg(p->db));
6026        sqlite3_free(sCtx.z);
6027        xCloser(sCtx.in);
6028        return 1;
6029      }
6030      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6031    }
6032    sqlite3_free(zSql);
6033    if( rc ){
6034      if (pStmt) sqlite3_finalize(pStmt);
6035      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
6036      xCloser(sCtx.in);
6037      return 1;
6038    }
6039    nCol = sqlite3_column_count(pStmt);
6040    sqlite3_finalize(pStmt);
6041    pStmt = 0;
6042    if( nCol==0 ) return 0; /* no columns, no error */
6043    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
6044    if( zSql==0 ){
6045      raw_printf(stderr, "Error: out of memory\n");
6046      xCloser(sCtx.in);
6047      return 1;
6048    }
6049    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
6050    j = strlen30(zSql);
6051    for(i=1; i<nCol; i++){
6052      zSql[j++] = ',';
6053      zSql[j++] = '?';
6054    }
6055    zSql[j++] = ')';
6056    zSql[j] = 0;
6057    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6058    sqlite3_free(zSql);
6059    if( rc ){
6060      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6061      if (pStmt) sqlite3_finalize(pStmt);
6062      xCloser(sCtx.in);
6063      return 1;
6064    }
6065    needCommit = sqlite3_get_autocommit(p->db);
6066    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
6067    do{
6068      int startLine = sCtx.nLine;
6069      for(i=0; i<nCol; i++){
6070        char *z = xRead(&sCtx);
6071        /*
6072        ** Did we reach end-of-file before finding any columns?
6073        ** If so, stop instead of NULL filling the remaining columns.
6074        */
6075        if( z==0 && i==0 ) break;
6076        /*
6077        ** Did we reach end-of-file OR end-of-line before finding any
6078        ** columns in ASCII mode?  If so, stop instead of NULL filling
6079        ** the remaining columns.
6080        */
6081        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
6082        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
6083        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
6084          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
6085                          "filling the rest with NULL\n",
6086                          sCtx.zFile, startLine, nCol, i+1);
6087          i += 2;
6088          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
6089        }
6090      }
6091      if( sCtx.cTerm==sCtx.cColSep ){
6092        do{
6093          xRead(&sCtx);
6094          i++;
6095        }while( sCtx.cTerm==sCtx.cColSep );
6096        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
6097                        "extras ignored\n",
6098                        sCtx.zFile, startLine, nCol, i);
6099      }
6100      if( i>=nCol ){
6101        sqlite3_step(pStmt);
6102        rc = sqlite3_reset(pStmt);
6103        if( rc!=SQLITE_OK ){
6104          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
6105                      startLine, sqlite3_errmsg(p->db));
6106        }
6107      }
6108    }while( sCtx.cTerm!=EOF );
6109
6110    xCloser(sCtx.in);
6111    sqlite3_free(sCtx.z);
6112    sqlite3_finalize(pStmt);
6113    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
6114  }else
6115
6116#ifndef SQLITE_UNTESTABLE
6117  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
6118    char *zSql;
6119    char *zCollist = 0;
6120    sqlite3_stmt *pStmt;
6121    int tnum = 0;
6122    int i;
6123    if( nArg!=3 ){
6124      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
6125      rc = 1;
6126      goto meta_command_exit;
6127    }
6128    open_db(p, 0);
6129    zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
6130                           " WHERE name='%q' AND type='index'", azArg[1]);
6131    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6132    sqlite3_free(zSql);
6133    if( sqlite3_step(pStmt)==SQLITE_ROW ){
6134      tnum = sqlite3_column_int(pStmt, 0);
6135    }
6136    sqlite3_finalize(pStmt);
6137    if( tnum==0 ){
6138      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
6139      rc = 1;
6140      goto meta_command_exit;
6141    }
6142    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
6143    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6144    sqlite3_free(zSql);
6145    i = 0;
6146    while( sqlite3_step(pStmt)==SQLITE_ROW ){
6147      char zLabel[20];
6148      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
6149      i++;
6150      if( zCol==0 ){
6151        if( sqlite3_column_int(pStmt,1)==-1 ){
6152          zCol = "_ROWID_";
6153        }else{
6154          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
6155          zCol = zLabel;
6156        }
6157      }
6158      if( zCollist==0 ){
6159        zCollist = sqlite3_mprintf("\"%w\"", zCol);
6160      }else{
6161        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
6162      }
6163    }
6164    sqlite3_finalize(pStmt);
6165    zSql = sqlite3_mprintf(
6166          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
6167          azArg[2], zCollist, zCollist);
6168    sqlite3_free(zCollist);
6169    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
6170    if( rc==SQLITE_OK ){
6171      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
6172      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
6173      if( rc ){
6174        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
6175      }else{
6176        utf8_printf(stdout, "%s;\n", zSql);
6177        raw_printf(stdout,
6178           "WARNING: writing to an imposter table will corrupt the index!\n"
6179        );
6180      }
6181    }else{
6182      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
6183      rc = 1;
6184    }
6185    sqlite3_free(zSql);
6186  }else
6187#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
6188
6189#ifdef SQLITE_ENABLE_IOTRACE
6190  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
6191    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
6192    if( iotrace && iotrace!=stdout ) fclose(iotrace);
6193    iotrace = 0;
6194    if( nArg<2 ){
6195      sqlite3IoTrace = 0;
6196    }else if( strcmp(azArg[1], "-")==0 ){
6197      sqlite3IoTrace = iotracePrintf;
6198      iotrace = stdout;
6199    }else{
6200      iotrace = fopen(azArg[1], "w");
6201      if( iotrace==0 ){
6202        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
6203        sqlite3IoTrace = 0;
6204        rc = 1;
6205      }else{
6206        sqlite3IoTrace = iotracePrintf;
6207      }
6208    }
6209  }else
6210#endif
6211
6212  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
6213    static const struct {
6214       const char *zLimitName;   /* Name of a limit */
6215       int limitCode;            /* Integer code for that limit */
6216    } aLimit[] = {
6217      { "length",                SQLITE_LIMIT_LENGTH                    },
6218      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
6219      { "column",                SQLITE_LIMIT_COLUMN                    },
6220      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
6221      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
6222      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
6223      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
6224      { "attached",              SQLITE_LIMIT_ATTACHED                  },
6225      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
6226      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
6227      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
6228      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
6229    };
6230    int i, n2;
6231    open_db(p, 0);
6232    if( nArg==1 ){
6233      for(i=0; i<ArraySize(aLimit); i++){
6234        printf("%20s %d\n", aLimit[i].zLimitName,
6235               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
6236      }
6237    }else if( nArg>3 ){
6238      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
6239      rc = 1;
6240      goto meta_command_exit;
6241    }else{
6242      int iLimit = -1;
6243      n2 = strlen30(azArg[1]);
6244      for(i=0; i<ArraySize(aLimit); i++){
6245        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
6246          if( iLimit<0 ){
6247            iLimit = i;
6248          }else{
6249            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
6250            rc = 1;
6251            goto meta_command_exit;
6252          }
6253        }
6254      }
6255      if( iLimit<0 ){
6256        utf8_printf(stderr, "unknown limit: \"%s\"\n"
6257                        "enter \".limits\" with no arguments for a list.\n",
6258                         azArg[1]);
6259        rc = 1;
6260        goto meta_command_exit;
6261      }
6262      if( nArg==3 ){
6263        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
6264                      (int)integerValue(azArg[2]));
6265      }
6266      printf("%20s %d\n", aLimit[iLimit].zLimitName,
6267             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
6268    }
6269  }else
6270
6271  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
6272    open_db(p, 0);
6273    lintDotCommand(p, azArg, nArg);
6274  }else
6275
6276#ifndef SQLITE_OMIT_LOAD_EXTENSION
6277  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
6278    const char *zFile, *zProc;
6279    char *zErrMsg = 0;
6280    if( nArg<2 ){
6281      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
6282      rc = 1;
6283      goto meta_command_exit;
6284    }
6285    zFile = azArg[1];
6286    zProc = nArg>=3 ? azArg[2] : 0;
6287    open_db(p, 0);
6288    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
6289    if( rc!=SQLITE_OK ){
6290      utf8_printf(stderr, "Error: %s\n", zErrMsg);
6291      sqlite3_free(zErrMsg);
6292      rc = 1;
6293    }
6294  }else
6295#endif
6296
6297  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
6298    if( nArg!=2 ){
6299      raw_printf(stderr, "Usage: .log FILENAME\n");
6300      rc = 1;
6301    }else{
6302      const char *zFile = azArg[1];
6303      output_file_close(p->pLog);
6304      p->pLog = output_file_open(zFile, 0);
6305    }
6306  }else
6307
6308  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
6309    const char *zMode = nArg>=2 ? azArg[1] : "";
6310    int n2 = strlen30(zMode);
6311    int c2 = zMode[0];
6312    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
6313      p->mode = MODE_Line;
6314      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6315    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
6316      p->mode = MODE_Column;
6317      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6318    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
6319      p->mode = MODE_List;
6320      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
6321      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6322    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
6323      p->mode = MODE_Html;
6324    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
6325      p->mode = MODE_Tcl;
6326      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
6327      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6328    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
6329      p->mode = MODE_Csv;
6330      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
6331      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
6332    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
6333      p->mode = MODE_List;
6334      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
6335    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
6336      p->mode = MODE_Insert;
6337      set_table_name(p, nArg>=3 ? azArg[2] : "table");
6338    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
6339      p->mode = MODE_Quote;
6340    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
6341      p->mode = MODE_Ascii;
6342      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
6343      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
6344    }else if( nArg==1 ){
6345      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
6346    }else{
6347      raw_printf(stderr, "Error: mode should be one of: "
6348         "ascii column csv html insert line list quote tabs tcl\n");
6349      rc = 1;
6350    }
6351    p->cMode = p->mode;
6352  }else
6353
6354  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
6355    if( nArg==2 ){
6356      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
6357                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
6358    }else{
6359      raw_printf(stderr, "Usage: .nullvalue STRING\n");
6360      rc = 1;
6361    }
6362  }else
6363
6364  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
6365    char *zNewFilename;  /* Name of the database file to open */
6366    int iName = 1;       /* Index in azArg[] of the filename */
6367    int newFlag = 0;     /* True to delete file before opening */
6368    /* Close the existing database */
6369    session_close_all(p);
6370    sqlite3_close(p->db);
6371    p->db = 0;
6372    p->zDbFilename = 0;
6373    sqlite3_free(p->zFreeOnClose);
6374    p->zFreeOnClose = 0;
6375    p->openMode = SHELL_OPEN_UNSPEC;
6376    /* Check for command-line arguments */
6377    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
6378      const char *z = azArg[iName];
6379      if( optionMatch(z,"new") ){
6380        newFlag = 1;
6381#ifdef SQLITE_HAVE_ZLIB
6382      }else if( optionMatch(z, "zip") ){
6383        p->openMode = SHELL_OPEN_ZIPFILE;
6384#endif
6385      }else if( optionMatch(z, "append") ){
6386        p->openMode = SHELL_OPEN_APPENDVFS;
6387      }else if( optionMatch(z, "readonly") ){
6388        p->openMode = SHELL_OPEN_READONLY;
6389      }else if( z[0]=='-' ){
6390        utf8_printf(stderr, "unknown option: %s\n", z);
6391        rc = 1;
6392        goto meta_command_exit;
6393      }
6394    }
6395    /* If a filename is specified, try to open it first */
6396    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
6397    if( zNewFilename ){
6398      if( newFlag ) shellDeleteFile(zNewFilename);
6399      p->zDbFilename = zNewFilename;
6400      open_db(p, 1);
6401      if( p->db==0 ){
6402        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
6403        sqlite3_free(zNewFilename);
6404      }else{
6405        p->zFreeOnClose = zNewFilename;
6406      }
6407    }
6408    if( p->db==0 ){
6409      /* As a fall-back open a TEMP database */
6410      p->zDbFilename = 0;
6411      open_db(p, 0);
6412    }
6413  }else
6414
6415  if( (c=='o'
6416        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
6417   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
6418  ){
6419    const char *zFile = nArg>=2 ? azArg[1] : "stdout";
6420    int bTxtMode = 0;
6421    if( azArg[0][0]=='e' ){
6422      /* Transform the ".excel" command into ".once -x" */
6423      nArg = 2;
6424      azArg[0] = "once";
6425      zFile = azArg[1] = "-x";
6426      n = 4;
6427    }
6428    if( nArg>2 ){
6429      utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
6430      rc = 1;
6431      goto meta_command_exit;
6432    }
6433    if( n>1 && strncmp(azArg[0], "once", n)==0 ){
6434      if( nArg<2 ){
6435        raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
6436        rc = 1;
6437        goto meta_command_exit;
6438      }
6439      p->outCount = 2;
6440    }else{
6441      p->outCount = 0;
6442    }
6443    output_reset(p);
6444    if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
6445#ifndef SQLITE_NOHAVE_SYSTEM
6446    if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
6447      p->doXdgOpen = 1;
6448      outputModePush(p);
6449      if( zFile[1]=='x' ){
6450        newTempFile(p, "csv");
6451        p->mode = MODE_Csv;
6452        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
6453        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
6454      }else{
6455        newTempFile(p, "txt");
6456        bTxtMode = 1;
6457      }
6458      zFile = p->zTempFile;
6459    }
6460#endif /* SQLITE_NOHAVE_SYSTEM */
6461    if( zFile[0]=='|' ){
6462#ifdef SQLITE_OMIT_POPEN
6463      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
6464      rc = 1;
6465      p->out = stdout;
6466#else
6467      p->out = popen(zFile + 1, "w");
6468      if( p->out==0 ){
6469        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
6470        p->out = stdout;
6471        rc = 1;
6472      }else{
6473        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
6474      }
6475#endif
6476    }else{
6477      p->out = output_file_open(zFile, bTxtMode);
6478      if( p->out==0 ){
6479        if( strcmp(zFile,"off")!=0 ){
6480          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
6481        }
6482        p->out = stdout;
6483        rc = 1;
6484      } else {
6485        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
6486      }
6487    }
6488  }else
6489
6490  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
6491    int i;
6492    for(i=1; i<nArg; i++){
6493      if( i>1 ) raw_printf(p->out, " ");
6494      utf8_printf(p->out, "%s", azArg[i]);
6495    }
6496    raw_printf(p->out, "\n");
6497  }else
6498
6499  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
6500    if( nArg >= 2) {
6501      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
6502    }
6503    if( nArg >= 3) {
6504      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
6505    }
6506  }else
6507
6508  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
6509    rc = 2;
6510  }else
6511
6512  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
6513    FILE *alt;
6514    if( nArg!=2 ){
6515      raw_printf(stderr, "Usage: .read FILE\n");
6516      rc = 1;
6517      goto meta_command_exit;
6518    }
6519    alt = fopen(azArg[1], "rb");
6520    if( alt==0 ){
6521      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
6522      rc = 1;
6523    }else{
6524      rc = process_input(p, alt);
6525      fclose(alt);
6526    }
6527  }else
6528
6529  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
6530    const char *zSrcFile;
6531    const char *zDb;
6532    sqlite3 *pSrc;
6533    sqlite3_backup *pBackup;
6534    int nTimeout = 0;
6535
6536    if( nArg==2 ){
6537      zSrcFile = azArg[1];
6538      zDb = "main";
6539    }else if( nArg==3 ){
6540      zSrcFile = azArg[2];
6541      zDb = azArg[1];
6542    }else{
6543      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
6544      rc = 1;
6545      goto meta_command_exit;
6546    }
6547    rc = sqlite3_open(zSrcFile, &pSrc);
6548    if( rc!=SQLITE_OK ){
6549      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
6550      sqlite3_close(pSrc);
6551      return 1;
6552    }
6553    open_db(p, 0);
6554    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
6555    if( pBackup==0 ){
6556      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6557      sqlite3_close(pSrc);
6558      return 1;
6559    }
6560    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
6561          || rc==SQLITE_BUSY  ){
6562      if( rc==SQLITE_BUSY ){
6563        if( nTimeout++ >= 3 ) break;
6564        sqlite3_sleep(100);
6565      }
6566    }
6567    sqlite3_backup_finish(pBackup);
6568    if( rc==SQLITE_DONE ){
6569      rc = 0;
6570    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
6571      raw_printf(stderr, "Error: source database is busy\n");
6572      rc = 1;
6573    }else{
6574      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6575      rc = 1;
6576    }
6577    sqlite3_close(pSrc);
6578  }else
6579
6580  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
6581    if( nArg==2 ){
6582      p->scanstatsOn = (u8)booleanValue(azArg[1]);
6583#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
6584      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
6585#endif
6586    }else{
6587      raw_printf(stderr, "Usage: .scanstats on|off\n");
6588      rc = 1;
6589    }
6590  }else
6591
6592  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
6593    ShellText sSelect;
6594    ShellState data;
6595    char *zErrMsg = 0;
6596    const char *zDiv = "(";
6597    const char *zName = 0;
6598    int iSchema = 0;
6599    int bDebug = 0;
6600    int ii;
6601
6602    open_db(p, 0);
6603    memcpy(&data, p, sizeof(data));
6604    data.showHeader = 0;
6605    data.cMode = data.mode = MODE_Semi;
6606    initText(&sSelect);
6607    for(ii=1; ii<nArg; ii++){
6608      if( optionMatch(azArg[ii],"indent") ){
6609        data.cMode = data.mode = MODE_Pretty;
6610      }else if( optionMatch(azArg[ii],"debug") ){
6611        bDebug = 1;
6612      }else if( zName==0 ){
6613        zName = azArg[ii];
6614      }else{
6615        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
6616        rc = 1;
6617        goto meta_command_exit;
6618      }
6619    }
6620    if( zName!=0 ){
6621      int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
6622      if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
6623        char *new_argv[2], *new_colv[2];
6624        new_argv[0] = sqlite3_mprintf(
6625                      "CREATE TABLE %s (\n"
6626                      "  type text,\n"
6627                      "  name text,\n"
6628                      "  tbl_name text,\n"
6629                      "  rootpage integer,\n"
6630                      "  sql text\n"
6631                      ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
6632        new_argv[1] = 0;
6633        new_colv[0] = "sql";
6634        new_colv[1] = 0;
6635        callback(&data, 1, new_argv, new_colv);
6636        sqlite3_free(new_argv[0]);
6637      }
6638    }
6639    if( zDiv ){
6640      sqlite3_stmt *pStmt = 0;
6641      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
6642                              -1, &pStmt, 0);
6643      if( rc ){
6644        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6645        sqlite3_finalize(pStmt);
6646        rc = 1;
6647        goto meta_command_exit;
6648      }
6649      appendText(&sSelect, "SELECT sql FROM", 0);
6650      iSchema = 0;
6651      while( sqlite3_step(pStmt)==SQLITE_ROW ){
6652        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
6653        char zScNum[30];
6654        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
6655        appendText(&sSelect, zDiv, 0);
6656        zDiv = " UNION ALL ";
6657        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
6658        if( sqlite3_stricmp(zDb, "main")!=0 ){
6659          appendText(&sSelect, zDb, '"');
6660        }else{
6661          appendText(&sSelect, "NULL", 0);
6662        }
6663        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
6664        appendText(&sSelect, zScNum, 0);
6665        appendText(&sSelect, " AS snum, ", 0);
6666        appendText(&sSelect, zDb, '\'');
6667        appendText(&sSelect, " AS sname FROM ", 0);
6668        appendText(&sSelect, zDb, '"');
6669        appendText(&sSelect, ".sqlite_master", 0);
6670      }
6671      sqlite3_finalize(pStmt);
6672#ifdef SQLITE_INTROSPECTION_PRAGMAS
6673      if( zName ){
6674        appendText(&sSelect,
6675           " UNION ALL SELECT shell_module_schema(name),"
6676           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
6677      }
6678#endif
6679      appendText(&sSelect, ") WHERE ", 0);
6680      if( zName ){
6681        char *zQarg = sqlite3_mprintf("%Q", zName);
6682        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
6683                    strchr(zName, '[') != 0;
6684        if( strchr(zName, '.') ){
6685          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
6686        }else{
6687          appendText(&sSelect, "lower(tbl_name)", 0);
6688        }
6689        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
6690        appendText(&sSelect, zQarg, 0);
6691        if( !bGlob ){
6692          appendText(&sSelect, " ESCAPE '\\' ", 0);
6693        }
6694        appendText(&sSelect, " AND ", 0);
6695        sqlite3_free(zQarg);
6696      }
6697      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
6698                           " ORDER BY snum, rowid", 0);
6699      if( bDebug ){
6700        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
6701      }else{
6702        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
6703      }
6704      freeText(&sSelect);
6705    }
6706    if( zErrMsg ){
6707      utf8_printf(stderr,"Error: %s\n", zErrMsg);
6708      sqlite3_free(zErrMsg);
6709      rc = 1;
6710    }else if( rc != SQLITE_OK ){
6711      raw_printf(stderr,"Error: querying schema information\n");
6712      rc = 1;
6713    }else{
6714      rc = 0;
6715    }
6716  }else
6717
6718#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
6719  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
6720    sqlite3SelectTrace = (int)integerValue(azArg[1]);
6721  }else
6722#endif
6723
6724#if defined(SQLITE_ENABLE_SESSION)
6725  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
6726    OpenSession *pSession = &p->aSession[0];
6727    char **azCmd = &azArg[1];
6728    int iSes = 0;
6729    int nCmd = nArg - 1;
6730    int i;
6731    if( nArg<=1 ) goto session_syntax_error;
6732    open_db(p, 0);
6733    if( nArg>=3 ){
6734      for(iSes=0; iSes<p->nSession; iSes++){
6735        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
6736      }
6737      if( iSes<p->nSession ){
6738        pSession = &p->aSession[iSes];
6739        azCmd++;
6740        nCmd--;
6741      }else{
6742        pSession = &p->aSession[0];
6743        iSes = 0;
6744      }
6745    }
6746
6747    /* .session attach TABLE
6748    ** Invoke the sqlite3session_attach() interface to attach a particular
6749    ** table so that it is never filtered.
6750    */
6751    if( strcmp(azCmd[0],"attach")==0 ){
6752      if( nCmd!=2 ) goto session_syntax_error;
6753      if( pSession->p==0 ){
6754        session_not_open:
6755        raw_printf(stderr, "ERROR: No sessions are open\n");
6756      }else{
6757        rc = sqlite3session_attach(pSession->p, azCmd[1]);
6758        if( rc ){
6759          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
6760          rc = 0;
6761        }
6762      }
6763    }else
6764
6765    /* .session changeset FILE
6766    ** .session patchset FILE
6767    ** Write a changeset or patchset into a file.  The file is overwritten.
6768    */
6769    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
6770      FILE *out = 0;
6771      if( nCmd!=2 ) goto session_syntax_error;
6772      if( pSession->p==0 ) goto session_not_open;
6773      out = fopen(azCmd[1], "wb");
6774      if( out==0 ){
6775        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
6776      }else{
6777        int szChng;
6778        void *pChng;
6779        if( azCmd[0][0]=='c' ){
6780          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
6781        }else{
6782          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
6783        }
6784        if( rc ){
6785          printf("Error: error code %d\n", rc);
6786          rc = 0;
6787        }
6788        if( pChng
6789          && fwrite(pChng, szChng, 1, out)!=1 ){
6790          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
6791                  szChng);
6792        }
6793        sqlite3_free(pChng);
6794        fclose(out);
6795      }
6796    }else
6797
6798    /* .session close
6799    ** Close the identified session
6800    */
6801    if( strcmp(azCmd[0], "close")==0 ){
6802      if( nCmd!=1 ) goto session_syntax_error;
6803      if( p->nSession ){
6804        session_close(pSession);
6805        p->aSession[iSes] = p->aSession[--p->nSession];
6806      }
6807    }else
6808
6809    /* .session enable ?BOOLEAN?
6810    ** Query or set the enable flag
6811    */
6812    if( strcmp(azCmd[0], "enable")==0 ){
6813      int ii;
6814      if( nCmd>2 ) goto session_syntax_error;
6815      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
6816      if( p->nSession ){
6817        ii = sqlite3session_enable(pSession->p, ii);
6818        utf8_printf(p->out, "session %s enable flag = %d\n",
6819                    pSession->zName, ii);
6820      }
6821    }else
6822
6823    /* .session filter GLOB ....
6824    ** Set a list of GLOB patterns of table names to be excluded.
6825    */
6826    if( strcmp(azCmd[0], "filter")==0 ){
6827      int ii, nByte;
6828      if( nCmd<2 ) goto session_syntax_error;
6829      if( p->nSession ){
6830        for(ii=0; ii<pSession->nFilter; ii++){
6831          sqlite3_free(pSession->azFilter[ii]);
6832        }
6833        sqlite3_free(pSession->azFilter);
6834        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
6835        pSession->azFilter = sqlite3_malloc( nByte );
6836        if( pSession->azFilter==0 ){
6837          raw_printf(stderr, "Error: out or memory\n");
6838          exit(1);
6839        }
6840        for(ii=1; ii<nCmd; ii++){
6841          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
6842        }
6843        pSession->nFilter = ii-1;
6844      }
6845    }else
6846
6847    /* .session indirect ?BOOLEAN?
6848    ** Query or set the indirect flag
6849    */
6850    if( strcmp(azCmd[0], "indirect")==0 ){
6851      int ii;
6852      if( nCmd>2 ) goto session_syntax_error;
6853      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
6854      if( p->nSession ){
6855        ii = sqlite3session_indirect(pSession->p, ii);
6856        utf8_printf(p->out, "session %s indirect flag = %d\n",
6857                    pSession->zName, ii);
6858      }
6859    }else
6860
6861    /* .session isempty
6862    ** Determine if the session is empty
6863    */
6864    if( strcmp(azCmd[0], "isempty")==0 ){
6865      int ii;
6866      if( nCmd!=1 ) goto session_syntax_error;
6867      if( p->nSession ){
6868        ii = sqlite3session_isempty(pSession->p);
6869        utf8_printf(p->out, "session %s isempty flag = %d\n",
6870                    pSession->zName, ii);
6871      }
6872    }else
6873
6874    /* .session list
6875    ** List all currently open sessions
6876    */
6877    if( strcmp(azCmd[0],"list")==0 ){
6878      for(i=0; i<p->nSession; i++){
6879        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
6880      }
6881    }else
6882
6883    /* .session open DB NAME
6884    ** Open a new session called NAME on the attached database DB.
6885    ** DB is normally "main".
6886    */
6887    if( strcmp(azCmd[0],"open")==0 ){
6888      char *zName;
6889      if( nCmd!=3 ) goto session_syntax_error;
6890      zName = azCmd[2];
6891      if( zName[0]==0 ) goto session_syntax_error;
6892      for(i=0; i<p->nSession; i++){
6893        if( strcmp(p->aSession[i].zName,zName)==0 ){
6894          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
6895          goto meta_command_exit;
6896        }
6897      }
6898      if( p->nSession>=ArraySize(p->aSession) ){
6899        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
6900        goto meta_command_exit;
6901      }
6902      pSession = &p->aSession[p->nSession];
6903      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
6904      if( rc ){
6905        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
6906        rc = 0;
6907        goto meta_command_exit;
6908      }
6909      pSession->nFilter = 0;
6910      sqlite3session_table_filter(pSession->p, session_filter, pSession);
6911      p->nSession++;
6912      pSession->zName = sqlite3_mprintf("%s", zName);
6913    }else
6914    /* If no command name matches, show a syntax error */
6915    session_syntax_error:
6916    session_help(p);
6917  }else
6918#endif
6919
6920#ifdef SQLITE_DEBUG
6921  /* Undocumented commands for internal testing.  Subject to change
6922  ** without notice. */
6923  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
6924    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
6925      int i, v;
6926      for(i=1; i<nArg; i++){
6927        v = booleanValue(azArg[i]);
6928        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
6929      }
6930    }
6931    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
6932      int i; sqlite3_int64 v;
6933      for(i=1; i<nArg; i++){
6934        char zBuf[200];
6935        v = integerValue(azArg[i]);
6936        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
6937        utf8_printf(p->out, "%s", zBuf);
6938      }
6939    }
6940  }else
6941#endif
6942
6943  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
6944    int bIsInit = 0;         /* True to initialize the SELFTEST table */
6945    int bVerbose = 0;        /* Verbose output */
6946    int bSelftestExists;     /* True if SELFTEST already exists */
6947    int i, k;                /* Loop counters */
6948    int nTest = 0;           /* Number of tests runs */
6949    int nErr = 0;            /* Number of errors seen */
6950    ShellText str;           /* Answer for a query */
6951    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
6952
6953    open_db(p,0);
6954    for(i=1; i<nArg; i++){
6955      const char *z = azArg[i];
6956      if( z[0]=='-' && z[1]=='-' ) z++;
6957      if( strcmp(z,"-init")==0 ){
6958        bIsInit = 1;
6959      }else
6960      if( strcmp(z,"-v")==0 ){
6961        bVerbose++;
6962      }else
6963      {
6964        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
6965                    azArg[i], azArg[0]);
6966        raw_printf(stderr, "Should be one of: --init -v\n");
6967        rc = 1;
6968        goto meta_command_exit;
6969      }
6970    }
6971    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
6972           != SQLITE_OK ){
6973      bSelftestExists = 0;
6974    }else{
6975      bSelftestExists = 1;
6976    }
6977    if( bIsInit ){
6978      createSelftestTable(p);
6979      bSelftestExists = 1;
6980    }
6981    initText(&str);
6982    appendText(&str, "x", 0);
6983    for(k=bSelftestExists; k>=0; k--){
6984      if( k==1 ){
6985        rc = sqlite3_prepare_v2(p->db,
6986            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
6987            -1, &pStmt, 0);
6988      }else{
6989        rc = sqlite3_prepare_v2(p->db,
6990          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
6991          "      (1,'run','PRAGMA integrity_check','ok')",
6992          -1, &pStmt, 0);
6993      }
6994      if( rc ){
6995        raw_printf(stderr, "Error querying the selftest table\n");
6996        rc = 1;
6997        sqlite3_finalize(pStmt);
6998        goto meta_command_exit;
6999      }
7000      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
7001        int tno = sqlite3_column_int(pStmt, 0);
7002        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
7003        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
7004        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
7005
7006        k = 0;
7007        if( bVerbose>0 ){
7008          char *zQuote = sqlite3_mprintf("%q", zSql);
7009          printf("%d: %s %s\n", tno, zOp, zSql);
7010          sqlite3_free(zQuote);
7011        }
7012        if( strcmp(zOp,"memo")==0 ){
7013          utf8_printf(p->out, "%s\n", zSql);
7014        }else
7015        if( strcmp(zOp,"run")==0 ){
7016          char *zErrMsg = 0;
7017          str.n = 0;
7018          str.z[0] = 0;
7019          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
7020          nTest++;
7021          if( bVerbose ){
7022            utf8_printf(p->out, "Result: %s\n", str.z);
7023          }
7024          if( rc || zErrMsg ){
7025            nErr++;
7026            rc = 1;
7027            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
7028            sqlite3_free(zErrMsg);
7029          }else if( strcmp(zAns,str.z)!=0 ){
7030            nErr++;
7031            rc = 1;
7032            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
7033            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
7034          }
7035        }else
7036        {
7037          utf8_printf(stderr,
7038            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
7039          rc = 1;
7040          break;
7041        }
7042      } /* End loop over rows of content from SELFTEST */
7043      sqlite3_finalize(pStmt);
7044    } /* End loop over k */
7045    freeText(&str);
7046    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
7047  }else
7048
7049  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
7050    if( nArg<2 || nArg>3 ){
7051      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
7052      rc = 1;
7053    }
7054    if( nArg>=2 ){
7055      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
7056                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
7057    }
7058    if( nArg>=3 ){
7059      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
7060                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
7061    }
7062  }else
7063
7064  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
7065    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
7066    int i;                   /* Loop counter */
7067    int bSchema = 0;         /* Also hash the schema */
7068    int bSeparate = 0;       /* Hash each table separately */
7069    int iSize = 224;         /* Hash algorithm to use */
7070    int bDebug = 0;          /* Only show the query that would have run */
7071    sqlite3_stmt *pStmt;     /* For querying tables names */
7072    char *zSql;              /* SQL to be run */
7073    char *zSep;              /* Separator */
7074    ShellText sSql;          /* Complete SQL for the query to run the hash */
7075    ShellText sQuery;        /* Set of queries used to read all content */
7076    open_db(p, 0);
7077    for(i=1; i<nArg; i++){
7078      const char *z = azArg[i];
7079      if( z[0]=='-' ){
7080        z++;
7081        if( z[0]=='-' ) z++;
7082        if( strcmp(z,"schema")==0 ){
7083          bSchema = 1;
7084        }else
7085        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
7086         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
7087        ){
7088          iSize = atoi(&z[5]);
7089        }else
7090        if( strcmp(z,"debug")==0 ){
7091          bDebug = 1;
7092        }else
7093        {
7094          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
7095                      azArg[i], azArg[0]);
7096          raw_printf(stderr, "Should be one of: --schema"
7097                             " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
7098          rc = 1;
7099          goto meta_command_exit;
7100        }
7101      }else if( zLike ){
7102        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
7103        rc = 1;
7104        goto meta_command_exit;
7105      }else{
7106        zLike = z;
7107        bSeparate = 1;
7108        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
7109      }
7110    }
7111    if( bSchema ){
7112      zSql = "SELECT lower(name) FROM sqlite_master"
7113             " WHERE type='table' AND coalesce(rootpage,0)>1"
7114             " UNION ALL SELECT 'sqlite_master'"
7115             " ORDER BY 1 collate nocase";
7116    }else{
7117      zSql = "SELECT lower(name) FROM sqlite_master"
7118             " WHERE type='table' AND coalesce(rootpage,0)>1"
7119             " AND name NOT LIKE 'sqlite_%'"
7120             " ORDER BY 1 collate nocase";
7121    }
7122    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7123    initText(&sQuery);
7124    initText(&sSql);
7125    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
7126    zSep = "VALUES(";
7127    while( SQLITE_ROW==sqlite3_step(pStmt) ){
7128      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
7129      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
7130      if( strncmp(zTab, "sqlite_",7)!=0 ){
7131        appendText(&sQuery,"SELECT * FROM ", 0);
7132        appendText(&sQuery,zTab,'"');
7133        appendText(&sQuery," NOT INDEXED;", 0);
7134      }else if( strcmp(zTab, "sqlite_master")==0 ){
7135        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
7136                           " ORDER BY name;", 0);
7137      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
7138        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
7139                           " ORDER BY name;", 0);
7140      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
7141        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
7142                           " ORDER BY tbl,idx;", 0);
7143      }else if( strcmp(zTab, "sqlite_stat3")==0
7144             || strcmp(zTab, "sqlite_stat4")==0 ){
7145        appendText(&sQuery, "SELECT * FROM ", 0);
7146        appendText(&sQuery, zTab, 0);
7147        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
7148      }
7149      appendText(&sSql, zSep, 0);
7150      appendText(&sSql, sQuery.z, '\'');
7151      sQuery.n = 0;
7152      appendText(&sSql, ",", 0);
7153      appendText(&sSql, zTab, '\'');
7154      zSep = "),(";
7155    }
7156    sqlite3_finalize(pStmt);
7157    if( bSeparate ){
7158      zSql = sqlite3_mprintf(
7159          "%s))"
7160          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
7161          "   FROM [sha3sum$query]",
7162          sSql.z, iSize);
7163    }else{
7164      zSql = sqlite3_mprintf(
7165          "%s))"
7166          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
7167          "   FROM [sha3sum$query]",
7168          sSql.z, iSize);
7169    }
7170    freeText(&sQuery);
7171    freeText(&sSql);
7172    if( bDebug ){
7173      utf8_printf(p->out, "%s\n", zSql);
7174    }else{
7175      shell_exec(p, zSql, 0);
7176    }
7177    sqlite3_free(zSql);
7178  }else
7179
7180#ifndef SQLITE_NOHAVE_SYSTEM
7181  if( c=='s'
7182   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
7183  ){
7184    char *zCmd;
7185    int i, x;
7186    if( nArg<2 ){
7187      raw_printf(stderr, "Usage: .system COMMAND\n");
7188      rc = 1;
7189      goto meta_command_exit;
7190    }
7191    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
7192    for(i=2; i<nArg; i++){
7193      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
7194                             zCmd, azArg[i]);
7195    }
7196    x = system(zCmd);
7197    sqlite3_free(zCmd);
7198    if( x ) raw_printf(stderr, "System command returns %d\n", x);
7199  }else
7200#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
7201
7202  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
7203    static const char *azBool[] = { "off", "on", "trigger", "full"};
7204    int i;
7205    if( nArg!=1 ){
7206      raw_printf(stderr, "Usage: .show\n");
7207      rc = 1;
7208      goto meta_command_exit;
7209    }
7210    utf8_printf(p->out, "%12.12s: %s\n","echo",
7211                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
7212    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
7213    utf8_printf(p->out, "%12.12s: %s\n","explain",
7214         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
7215    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
7216    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
7217    utf8_printf(p->out, "%12.12s: ", "nullvalue");
7218      output_c_string(p->out, p->nullValue);
7219      raw_printf(p->out, "\n");
7220    utf8_printf(p->out,"%12.12s: %s\n","output",
7221            strlen30(p->outfile) ? p->outfile : "stdout");
7222    utf8_printf(p->out,"%12.12s: ", "colseparator");
7223      output_c_string(p->out, p->colSeparator);
7224      raw_printf(p->out, "\n");
7225    utf8_printf(p->out,"%12.12s: ", "rowseparator");
7226      output_c_string(p->out, p->rowSeparator);
7227      raw_printf(p->out, "\n");
7228    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
7229    utf8_printf(p->out, "%12.12s: ", "width");
7230    for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
7231      raw_printf(p->out, "%d ", p->colWidth[i]);
7232    }
7233    raw_printf(p->out, "\n");
7234    utf8_printf(p->out, "%12.12s: %s\n", "filename",
7235                p->zDbFilename ? p->zDbFilename : "");
7236  }else
7237
7238  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
7239    if( nArg==2 ){
7240      p->statsOn = (u8)booleanValue(azArg[1]);
7241    }else if( nArg==1 ){
7242      display_stats(p->db, p, 0);
7243    }else{
7244      raw_printf(stderr, "Usage: .stats ?on|off?\n");
7245      rc = 1;
7246    }
7247  }else
7248
7249  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
7250   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
7251                 || strncmp(azArg[0], "indexes", n)==0) )
7252  ){
7253    sqlite3_stmt *pStmt;
7254    char **azResult;
7255    int nRow, nAlloc;
7256    int ii;
7257    ShellText s;
7258    initText(&s);
7259    open_db(p, 0);
7260    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7261    if( rc ) return shellDatabaseError(p->db);
7262
7263    if( nArg>2 && c=='i' ){
7264      /* It is an historical accident that the .indexes command shows an error
7265      ** when called with the wrong number of arguments whereas the .tables
7266      ** command does not. */
7267      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
7268      rc = 1;
7269      goto meta_command_exit;
7270    }
7271    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
7272      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
7273      if( zDbName==0 ) continue;
7274      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
7275      if( sqlite3_stricmp(zDbName, "main")==0 ){
7276        appendText(&s, "SELECT name FROM ", 0);
7277      }else{
7278        appendText(&s, "SELECT ", 0);
7279        appendText(&s, zDbName, '\'');
7280        appendText(&s, "||'.'||name FROM ", 0);
7281      }
7282      appendText(&s, zDbName, '"');
7283      appendText(&s, ".sqlite_master ", 0);
7284      if( c=='t' ){
7285        appendText(&s," WHERE type IN ('table','view')"
7286                      "   AND name NOT LIKE 'sqlite_%'"
7287                      "   AND name LIKE ?1", 0);
7288      }else{
7289        appendText(&s," WHERE type='index'"
7290                      "   AND tbl_name LIKE ?1", 0);
7291      }
7292    }
7293    rc = sqlite3_finalize(pStmt);
7294    appendText(&s, " ORDER BY 1", 0);
7295    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
7296    freeText(&s);
7297    if( rc ) return shellDatabaseError(p->db);
7298
7299    /* Run the SQL statement prepared by the above block. Store the results
7300    ** as an array of nul-terminated strings in azResult[].  */
7301    nRow = nAlloc = 0;
7302    azResult = 0;
7303    if( nArg>1 ){
7304      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
7305    }else{
7306      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
7307    }
7308    while( sqlite3_step(pStmt)==SQLITE_ROW ){
7309      if( nRow>=nAlloc ){
7310        char **azNew;
7311        int n2 = nAlloc*2 + 10;
7312        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
7313        if( azNew==0 ){
7314          rc = shellNomemError();
7315          break;
7316        }
7317        nAlloc = n2;
7318        azResult = azNew;
7319      }
7320      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7321      if( 0==azResult[nRow] ){
7322        rc = shellNomemError();
7323        break;
7324      }
7325      nRow++;
7326    }
7327    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
7328      rc = shellDatabaseError(p->db);
7329    }
7330
7331    /* Pretty-print the contents of array azResult[] to the output */
7332    if( rc==0 && nRow>0 ){
7333      int len, maxlen = 0;
7334      int i, j;
7335      int nPrintCol, nPrintRow;
7336      for(i=0; i<nRow; i++){
7337        len = strlen30(azResult[i]);
7338        if( len>maxlen ) maxlen = len;
7339      }
7340      nPrintCol = 80/(maxlen+2);
7341      if( nPrintCol<1 ) nPrintCol = 1;
7342      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
7343      for(i=0; i<nPrintRow; i++){
7344        for(j=i; j<nRow; j+=nPrintRow){
7345          char *zSp = j<nPrintRow ? "" : "  ";
7346          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
7347                      azResult[j] ? azResult[j]:"");
7348        }
7349        raw_printf(p->out, "\n");
7350      }
7351    }
7352
7353    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
7354    sqlite3_free(azResult);
7355  }else
7356
7357  /* Begin redirecting output to the file "testcase-out.txt" */
7358  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
7359    output_reset(p);
7360    p->out = output_file_open("testcase-out.txt", 0);
7361    if( p->out==0 ){
7362      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
7363    }
7364    if( nArg>=2 ){
7365      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
7366    }else{
7367      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
7368    }
7369  }else
7370
7371#ifndef SQLITE_UNTESTABLE
7372  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
7373    static const struct {
7374       const char *zCtrlName;   /* Name of a test-control option */
7375       int ctrlCode;            /* Integer code for that option */
7376       const char *zUsage;      /* Usage notes */
7377    } aCtrl[] = {
7378      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
7379      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
7380    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
7381    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
7382      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
7383    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
7384      { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
7385#ifdef SQLITE_N_KEYWORD
7386      { "iskeyword",          SQLITE_TESTCTRL_ISKEYWORD,     "IDENTIFIER"         },
7387#endif
7388      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
7389      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
7390      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
7391#ifdef YYCOVERAGE
7392      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
7393#endif
7394      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
7395      { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
7396      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
7397      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
7398      { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
7399    };
7400    int testctrl = -1;
7401    int iCtrl = -1;
7402    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
7403    int isOk = 0;
7404    int i, n2;
7405    const char *zCmd = 0;
7406
7407    open_db(p, 0);
7408    zCmd = nArg>=2 ? azArg[1] : "help";
7409
7410    /* The argument can optionally begin with "-" or "--" */
7411    if( zCmd[0]=='-' && zCmd[1] ){
7412      zCmd++;
7413      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7414    }
7415
7416    /* --help lists all test-controls */
7417    if( strcmp(zCmd,"help")==0 ){
7418      utf8_printf(p->out, "Available test-controls:\n");
7419      for(i=0; i<ArraySize(aCtrl); i++){
7420        utf8_printf(p->out, "  .testctrl %s %s\n",
7421                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7422      }
7423      rc = 1;
7424      goto meta_command_exit;
7425    }
7426
7427    /* convert testctrl text option to value. allow any unique prefix
7428    ** of the option name, or a numerical value. */
7429    n2 = strlen30(zCmd);
7430    for(i=0; i<ArraySize(aCtrl); i++){
7431      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7432        if( testctrl<0 ){
7433          testctrl = aCtrl[i].ctrlCode;
7434          iCtrl = i;
7435        }else{
7436          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
7437                              "Use \".testctrl --help\" for help\n", zCmd);
7438          rc = 1;
7439          goto meta_command_exit;
7440        }
7441      }
7442    }
7443    if( testctrl<0 ){
7444      utf8_printf(stderr,"Error: unknown test-control: %s\n"
7445                         "Use \".testctrl --help\" for help\n", zCmd);
7446    }else{
7447      switch(testctrl){
7448
7449        /* sqlite3_test_control(int, db, int) */
7450        case SQLITE_TESTCTRL_OPTIMIZATIONS:
7451        case SQLITE_TESTCTRL_RESERVE:
7452          if( nArg==3 ){
7453            int opt = (int)strtol(azArg[2], 0, 0);
7454            rc2 = sqlite3_test_control(testctrl, p->db, opt);
7455            isOk = 3;
7456          }
7457          break;
7458
7459        /* sqlite3_test_control(int) */
7460        case SQLITE_TESTCTRL_PRNG_SAVE:
7461        case SQLITE_TESTCTRL_PRNG_RESTORE:
7462        case SQLITE_TESTCTRL_PRNG_RESET:
7463        case SQLITE_TESTCTRL_BYTEORDER:
7464          if( nArg==2 ){
7465            rc2 = sqlite3_test_control(testctrl);
7466            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
7467          }
7468          break;
7469
7470        /* sqlite3_test_control(int, uint) */
7471        case SQLITE_TESTCTRL_PENDING_BYTE:
7472          if( nArg==3 ){
7473            unsigned int opt = (unsigned int)integerValue(azArg[2]);
7474            rc2 = sqlite3_test_control(testctrl, opt);
7475            isOk = 3;
7476          }
7477          break;
7478
7479        /* sqlite3_test_control(int, int) */
7480        case SQLITE_TESTCTRL_ASSERT:
7481        case SQLITE_TESTCTRL_ALWAYS:
7482          if( nArg==3 ){
7483            int opt = booleanValue(azArg[2]);
7484            rc2 = sqlite3_test_control(testctrl, opt);
7485            isOk = 1;
7486          }
7487          break;
7488
7489        /* sqlite3_test_control(int, int) */
7490        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
7491        case SQLITE_TESTCTRL_NEVER_CORRUPT:
7492          if( nArg==3 ){
7493            int opt = booleanValue(azArg[2]);
7494            rc2 = sqlite3_test_control(testctrl, opt);
7495            isOk = 3;
7496          }
7497          break;
7498
7499        /* sqlite3_test_control(int, char *) */
7500#ifdef SQLITE_N_KEYWORD
7501        case SQLITE_TESTCTRL_ISKEYWORD:
7502          if( nArg==3 ){
7503            const char *opt = azArg[2];
7504            rc2 = sqlite3_test_control(testctrl, opt);
7505            isOk = 1;
7506          }
7507          break;
7508#endif
7509
7510        case SQLITE_TESTCTRL_IMPOSTER:
7511          if( nArg==5 ){
7512            rc2 = sqlite3_test_control(testctrl, p->db,
7513                          azArg[2],
7514                          integerValue(azArg[3]),
7515                          integerValue(azArg[4]));
7516            isOk = 3;
7517          }
7518          break;
7519
7520#ifdef YYCOVERAGE
7521        case SQLITE_TESTCTRL_PARSER_COVERAGE:
7522          if( nArg==2 ){
7523            sqlite3_test_control(testctrl, p->out);
7524            isOk = 3;
7525          }
7526#endif
7527      }
7528    }
7529    if( isOk==0 && iCtrl>=0 ){
7530      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
7531      rc = 1;
7532    }else if( isOk==1 ){
7533      raw_printf(p->out, "%d\n", rc2);
7534    }else if( isOk==2 ){
7535      raw_printf(p->out, "0x%08x\n", rc2);
7536    }
7537  }else
7538#endif /* !defined(SQLITE_UNTESTABLE) */
7539
7540  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
7541    open_db(p, 0);
7542    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
7543  }else
7544
7545  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
7546    if( nArg==2 ){
7547      enableTimer = booleanValue(azArg[1]);
7548      if( enableTimer && !HAS_TIMER ){
7549        raw_printf(stderr, "Error: timer not available on this system.\n");
7550        enableTimer = 0;
7551      }
7552    }else{
7553      raw_printf(stderr, "Usage: .timer on|off\n");
7554      rc = 1;
7555    }
7556  }else
7557
7558  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
7559    open_db(p, 0);
7560    if( nArg!=2 ){
7561      raw_printf(stderr, "Usage: .trace FILE|off\n");
7562      rc = 1;
7563      goto meta_command_exit;
7564    }
7565    output_file_close(p->traceOut);
7566    p->traceOut = output_file_open(azArg[1], 0);
7567#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
7568    if( p->traceOut==0 ){
7569      sqlite3_trace_v2(p->db, 0, 0, 0);
7570    }else{
7571      sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
7572    }
7573#endif
7574  }else
7575
7576#if SQLITE_USER_AUTHENTICATION
7577  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
7578    if( nArg<2 ){
7579      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
7580      rc = 1;
7581      goto meta_command_exit;
7582    }
7583    open_db(p, 0);
7584    if( strcmp(azArg[1],"login")==0 ){
7585      if( nArg!=4 ){
7586        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
7587        rc = 1;
7588        goto meta_command_exit;
7589      }
7590      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
7591      if( rc ){
7592        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
7593        rc = 1;
7594      }
7595    }else if( strcmp(azArg[1],"add")==0 ){
7596      if( nArg!=5 ){
7597        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
7598        rc = 1;
7599        goto meta_command_exit;
7600      }
7601      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
7602                            booleanValue(azArg[4]));
7603      if( rc ){
7604        raw_printf(stderr, "User-Add failed: %d\n", rc);
7605        rc = 1;
7606      }
7607    }else if( strcmp(azArg[1],"edit")==0 ){
7608      if( nArg!=5 ){
7609        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
7610        rc = 1;
7611        goto meta_command_exit;
7612      }
7613      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
7614                              booleanValue(azArg[4]));
7615      if( rc ){
7616        raw_printf(stderr, "User-Edit failed: %d\n", rc);
7617        rc = 1;
7618      }
7619    }else if( strcmp(azArg[1],"delete")==0 ){
7620      if( nArg!=3 ){
7621        raw_printf(stderr, "Usage: .user delete USER\n");
7622        rc = 1;
7623        goto meta_command_exit;
7624      }
7625      rc = sqlite3_user_delete(p->db, azArg[2]);
7626      if( rc ){
7627        raw_printf(stderr, "User-Delete failed: %d\n", rc);
7628        rc = 1;
7629      }
7630    }else{
7631      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
7632      rc = 1;
7633      goto meta_command_exit;
7634    }
7635  }else
7636#endif /* SQLITE_USER_AUTHENTICATION */
7637
7638  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
7639    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
7640        sqlite3_libversion(), sqlite3_sourceid());
7641#if SQLITE_HAVE_ZLIB
7642    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
7643#endif
7644#define CTIMEOPT_VAL_(opt) #opt
7645#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
7646#if defined(__clang__) && defined(__clang_major__)
7647    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
7648                    CTIMEOPT_VAL(__clang_minor__) "."
7649                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
7650#elif defined(_MSC_VER)
7651    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
7652#elif defined(__GNUC__) && defined(__VERSION__)
7653    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
7654#endif
7655  }else
7656
7657  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
7658    const char *zDbName = nArg==2 ? azArg[1] : "main";
7659    sqlite3_vfs *pVfs = 0;
7660    if( p->db ){
7661      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
7662      if( pVfs ){
7663        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
7664        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
7665        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
7666        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
7667      }
7668    }
7669  }else
7670
7671  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
7672    sqlite3_vfs *pVfs;
7673    sqlite3_vfs *pCurrent = 0;
7674    if( p->db ){
7675      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
7676    }
7677    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
7678      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
7679           pVfs==pCurrent ? "  <--- CURRENT" : "");
7680      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
7681      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
7682      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
7683      if( pVfs->pNext ){
7684        raw_printf(p->out, "-----------------------------------\n");
7685      }
7686    }
7687  }else
7688
7689  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
7690    const char *zDbName = nArg==2 ? azArg[1] : "main";
7691    char *zVfsName = 0;
7692    if( p->db ){
7693      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
7694      if( zVfsName ){
7695        utf8_printf(p->out, "%s\n", zVfsName);
7696        sqlite3_free(zVfsName);
7697      }
7698    }
7699  }else
7700
7701#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
7702  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
7703    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
7704  }else
7705#endif
7706
7707  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
7708    int j;
7709    assert( nArg<=ArraySize(azArg) );
7710    for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
7711      p->colWidth[j-1] = (int)integerValue(azArg[j]);
7712    }
7713  }else
7714
7715  {
7716    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
7717      " \"%s\". Enter \".help\" for help\n", azArg[0]);
7718    rc = 1;
7719  }
7720
7721meta_command_exit:
7722  if( p->outCount ){
7723    p->outCount--;
7724    if( p->outCount==0 ) output_reset(p);
7725  }
7726  return rc;
7727}
7728
7729/*
7730** Return TRUE if a semicolon occurs anywhere in the first N characters
7731** of string z[].
7732*/
7733static int line_contains_semicolon(const char *z, int N){
7734  int i;
7735  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
7736  return 0;
7737}
7738
7739/*
7740** Test to see if a line consists entirely of whitespace.
7741*/
7742static int _all_whitespace(const char *z){
7743  for(; *z; z++){
7744    if( IsSpace(z[0]) ) continue;
7745    if( *z=='/' && z[1]=='*' ){
7746      z += 2;
7747      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
7748      if( *z==0 ) return 0;
7749      z++;
7750      continue;
7751    }
7752    if( *z=='-' && z[1]=='-' ){
7753      z += 2;
7754      while( *z && *z!='\n' ){ z++; }
7755      if( *z==0 ) return 1;
7756      continue;
7757    }
7758    return 0;
7759  }
7760  return 1;
7761}
7762
7763/*
7764** Return TRUE if the line typed in is an SQL command terminator other
7765** than a semi-colon.  The SQL Server style "go" command is understood
7766** as is the Oracle "/".
7767*/
7768static int line_is_command_terminator(const char *zLine){
7769  while( IsSpace(zLine[0]) ){ zLine++; };
7770  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
7771    return 1;  /* Oracle */
7772  }
7773  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
7774         && _all_whitespace(&zLine[2]) ){
7775    return 1;  /* SQL Server */
7776  }
7777  return 0;
7778}
7779
7780/*
7781** We need a default sqlite3_complete() implementation to use in case
7782** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
7783** any arbitrary text is a complete SQL statement.  This is not very
7784** user-friendly, but it does seem to work.
7785*/
7786#ifdef SQLITE_OMIT_COMPLETE
7787int sqlite3_complete(const char *zSql){ return 1; }
7788#endif
7789
7790/*
7791** Return true if zSql is a complete SQL statement.  Return false if it
7792** ends in the middle of a string literal or C-style comment.
7793*/
7794static int line_is_complete(char *zSql, int nSql){
7795  int rc;
7796  if( zSql==0 ) return 1;
7797  zSql[nSql] = ';';
7798  zSql[nSql+1] = 0;
7799  rc = sqlite3_complete(zSql);
7800  zSql[nSql] = 0;
7801  return rc;
7802}
7803
7804/*
7805** Run a single line of SQL
7806*/
7807static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
7808  int rc;
7809  char *zErrMsg = 0;
7810
7811  open_db(p, 0);
7812  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
7813  BEGIN_TIMER;
7814  rc = shell_exec(p, zSql, &zErrMsg);
7815  END_TIMER;
7816  if( rc || zErrMsg ){
7817    char zPrefix[100];
7818    if( in!=0 || !stdin_is_interactive ){
7819      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
7820                       "Error: near line %d:", startline);
7821    }else{
7822      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
7823    }
7824    if( zErrMsg!=0 ){
7825      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
7826      sqlite3_free(zErrMsg);
7827      zErrMsg = 0;
7828    }else{
7829      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
7830    }
7831    return 1;
7832  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
7833    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
7834            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
7835  }
7836  return 0;
7837}
7838
7839
7840/*
7841** Read input from *in and process it.  If *in==0 then input
7842** is interactive - the user is typing it it.  Otherwise, input
7843** is coming from a file or device.  A prompt is issued and history
7844** is saved only if input is interactive.  An interrupt signal will
7845** cause this routine to exit immediately, unless input is interactive.
7846**
7847** Return the number of errors.
7848*/
7849static int process_input(ShellState *p, FILE *in){
7850  char *zLine = 0;          /* A single input line */
7851  char *zSql = 0;           /* Accumulated SQL text */
7852  int nLine;                /* Length of current line */
7853  int nSql = 0;             /* Bytes of zSql[] used */
7854  int nAlloc = 0;           /* Allocated zSql[] space */
7855  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
7856  int rc;                   /* Error code */
7857  int errCnt = 0;           /* Number of errors seen */
7858  int lineno = 0;           /* Current line number */
7859  int startline = 0;        /* Line number for start of current input */
7860
7861  while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
7862    fflush(p->out);
7863    zLine = one_input_line(in, zLine, nSql>0);
7864    if( zLine==0 ){
7865      /* End of input */
7866      if( in==0 && stdin_is_interactive ) printf("\n");
7867      break;
7868    }
7869    if( seenInterrupt ){
7870      if( in!=0 ) break;
7871      seenInterrupt = 0;
7872    }
7873    lineno++;
7874    if( nSql==0 && _all_whitespace(zLine) ){
7875      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
7876      continue;
7877    }
7878    if( zLine && zLine[0]=='.' && nSql==0 ){
7879      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
7880      rc = do_meta_command(zLine, p);
7881      if( rc==2 ){ /* exit requested */
7882        break;
7883      }else if( rc ){
7884        errCnt++;
7885      }
7886      continue;
7887    }
7888    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
7889      memcpy(zLine,";",2);
7890    }
7891    nLine = strlen30(zLine);
7892    if( nSql+nLine+2>=nAlloc ){
7893      nAlloc = nSql+nLine+100;
7894      zSql = realloc(zSql, nAlloc);
7895      if( zSql==0 ){
7896        raw_printf(stderr, "Error: out of memory\n");
7897        exit(1);
7898      }
7899    }
7900    nSqlPrior = nSql;
7901    if( nSql==0 ){
7902      int i;
7903      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
7904      assert( nAlloc>0 && zSql!=0 );
7905      memcpy(zSql, zLine+i, nLine+1-i);
7906      startline = lineno;
7907      nSql = nLine-i;
7908    }else{
7909      zSql[nSql++] = '\n';
7910      memcpy(zSql+nSql, zLine, nLine+1);
7911      nSql += nLine;
7912    }
7913    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
7914                && sqlite3_complete(zSql) ){
7915      errCnt += runOneSqlLine(p, zSql, in, startline);
7916      nSql = 0;
7917      if( p->outCount ){
7918        output_reset(p);
7919        p->outCount = 0;
7920      }else{
7921        clearTempFile(p);
7922      }
7923    }else if( nSql && _all_whitespace(zSql) ){
7924      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
7925      nSql = 0;
7926    }
7927  }
7928  if( nSql && !_all_whitespace(zSql) ){
7929    runOneSqlLine(p, zSql, in, startline);
7930  }
7931  free(zSql);
7932  free(zLine);
7933  return errCnt>0;
7934}
7935
7936/*
7937** Return a pathname which is the user's home directory.  A
7938** 0 return indicates an error of some kind.
7939*/
7940static char *find_home_dir(int clearFlag){
7941  static char *home_dir = NULL;
7942  if( clearFlag ){
7943    free(home_dir);
7944    home_dir = 0;
7945    return 0;
7946  }
7947  if( home_dir ) return home_dir;
7948
7949#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
7950     && !defined(__RTP__) && !defined(_WRS_KERNEL)
7951  {
7952    struct passwd *pwent;
7953    uid_t uid = getuid();
7954    if( (pwent=getpwuid(uid)) != NULL) {
7955      home_dir = pwent->pw_dir;
7956    }
7957  }
7958#endif
7959
7960#if defined(_WIN32_WCE)
7961  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
7962   */
7963  home_dir = "/";
7964#else
7965
7966#if defined(_WIN32) || defined(WIN32)
7967  if (!home_dir) {
7968    home_dir = getenv("USERPROFILE");
7969  }
7970#endif
7971
7972  if (!home_dir) {
7973    home_dir = getenv("HOME");
7974  }
7975
7976#if defined(_WIN32) || defined(WIN32)
7977  if (!home_dir) {
7978    char *zDrive, *zPath;
7979    int n;
7980    zDrive = getenv("HOMEDRIVE");
7981    zPath = getenv("HOMEPATH");
7982    if( zDrive && zPath ){
7983      n = strlen30(zDrive) + strlen30(zPath) + 1;
7984      home_dir = malloc( n );
7985      if( home_dir==0 ) return 0;
7986      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
7987      return home_dir;
7988    }
7989    home_dir = "c:\\";
7990  }
7991#endif
7992
7993#endif /* !_WIN32_WCE */
7994
7995  if( home_dir ){
7996    int n = strlen30(home_dir) + 1;
7997    char *z = malloc( n );
7998    if( z ) memcpy(z, home_dir, n);
7999    home_dir = z;
8000  }
8001
8002  return home_dir;
8003}
8004
8005/*
8006** Read input from the file given by sqliterc_override.  Or if that
8007** parameter is NULL, take input from ~/.sqliterc
8008**
8009** Returns the number of errors.
8010*/
8011static void process_sqliterc(
8012  ShellState *p,                  /* Configuration data */
8013  const char *sqliterc_override   /* Name of config file. NULL to use default */
8014){
8015  char *home_dir = NULL;
8016  const char *sqliterc = sqliterc_override;
8017  char *zBuf = 0;
8018  FILE *in = NULL;
8019
8020  if (sqliterc == NULL) {
8021    home_dir = find_home_dir(0);
8022    if( home_dir==0 ){
8023      raw_printf(stderr, "-- warning: cannot find home directory;"
8024                      " cannot read ~/.sqliterc\n");
8025      return;
8026    }
8027    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
8028    sqliterc = zBuf;
8029  }
8030  in = fopen(sqliterc,"rb");
8031  if( in ){
8032    if( stdin_is_interactive ){
8033      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
8034    }
8035    process_input(p,in);
8036    fclose(in);
8037  }
8038  sqlite3_free(zBuf);
8039}
8040
8041/*
8042** Show available command line options
8043*/
8044static const char zOptions[] =
8045#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
8046  "   -A ARGS...           run \".archive ARGS\" and exit\n"
8047#endif
8048  "   -append              append the database to the end of the file\n"
8049  "   -ascii               set output mode to 'ascii'\n"
8050  "   -bail                stop after hitting an error\n"
8051  "   -batch               force batch I/O\n"
8052  "   -column              set output mode to 'column'\n"
8053  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
8054  "   -csv                 set output mode to 'csv'\n"
8055  "   -echo                print commands before execution\n"
8056  "   -init FILENAME       read/process named file\n"
8057  "   -[no]header          turn headers on or off\n"
8058#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8059  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
8060#endif
8061  "   -help                show this message\n"
8062  "   -html                set output mode to HTML\n"
8063  "   -interactive         force interactive I/O\n"
8064  "   -line                set output mode to 'line'\n"
8065  "   -list                set output mode to 'list'\n"
8066  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
8067  "   -mmap N              default mmap size set to N\n"
8068#ifdef SQLITE_ENABLE_MULTIPLEX
8069  "   -multiplex           enable the multiplexor VFS\n"
8070#endif
8071  "   -newline SEP         set output row separator. Default: '\\n'\n"
8072  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
8073  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
8074  "   -quote               set output mode to 'quote'\n"
8075  "   -readonly            open the database read-only\n"
8076  "   -separator SEP       set output column separator. Default: '|'\n"
8077#ifdef SQLITE_ENABLE_SORTER_REFERENCES
8078  "   -sorterref SIZE      sorter references threshold size\n"
8079#endif
8080  "   -stats               print memory stats before each finalize\n"
8081  "   -version             show SQLite version\n"
8082  "   -vfs NAME            use NAME as the default VFS\n"
8083#ifdef SQLITE_ENABLE_VFSTRACE
8084  "   -vfstrace            enable tracing of all VFS calls\n"
8085#endif
8086#ifdef SQLITE_HAVE_ZLIB
8087  "   -zip                 open the file as a ZIP Archive\n"
8088#endif
8089;
8090static void usage(int showDetail){
8091  utf8_printf(stderr,
8092      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
8093      "FILENAME is the name of an SQLite database. A new database is created\n"
8094      "if the file does not previously exist.\n", Argv0);
8095  if( showDetail ){
8096    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
8097  }else{
8098    raw_printf(stderr, "Use the -help option for additional information\n");
8099  }
8100  exit(1);
8101}
8102
8103/*
8104** Internal check:  Verify that the SQLite is uninitialized.  Print a
8105** error message if it is initialized.
8106*/
8107static void verify_uninitialized(void){
8108  if( sqlite3_config(-1)==SQLITE_MISUSE ){
8109    utf8_printf(stdout, "WARNING: attempt to configuration SQLite after"
8110                        " initialization.\n");
8111  }
8112}
8113
8114/*
8115** Initialize the state information in data
8116*/
8117static void main_init(ShellState *data) {
8118  memset(data, 0, sizeof(*data));
8119  data->normalMode = data->cMode = data->mode = MODE_List;
8120  data->autoExplain = 1;
8121  memcpy(data->colSeparator,SEP_Column, 2);
8122  memcpy(data->rowSeparator,SEP_Row, 2);
8123  data->showHeader = 0;
8124  data->shellFlgs = SHFLG_Lookaside;
8125  verify_uninitialized();
8126  sqlite3_config(SQLITE_CONFIG_URI, 1);
8127  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
8128  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
8129  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
8130  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
8131}
8132
8133/*
8134** Output text to the console in a font that attracts extra attention.
8135*/
8136#ifdef _WIN32
8137static void printBold(const char *zText){
8138  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
8139  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
8140  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
8141  SetConsoleTextAttribute(out,
8142         FOREGROUND_RED|FOREGROUND_INTENSITY
8143  );
8144  printf("%s", zText);
8145  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
8146}
8147#else
8148static void printBold(const char *zText){
8149  printf("\033[1m%s\033[0m", zText);
8150}
8151#endif
8152
8153/*
8154** Get the argument to an --option.  Throw an error and die if no argument
8155** is available.
8156*/
8157static char *cmdline_option_value(int argc, char **argv, int i){
8158  if( i==argc ){
8159    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
8160            argv[0], argv[argc-1]);
8161    exit(1);
8162  }
8163  return argv[i];
8164}
8165
8166#ifndef SQLITE_SHELL_IS_UTF8
8167#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
8168#    define SQLITE_SHELL_IS_UTF8          (0)
8169#  else
8170#    define SQLITE_SHELL_IS_UTF8          (1)
8171#  endif
8172#endif
8173
8174#if SQLITE_SHELL_IS_UTF8
8175int SQLITE_CDECL main(int argc, char **argv){
8176#else
8177int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
8178  char **argv;
8179#endif
8180  char *zErrMsg = 0;
8181  ShellState data;
8182  const char *zInitFile = 0;
8183  int i;
8184  int rc = 0;
8185  int warnInmemoryDb = 0;
8186  int readStdin = 1;
8187  int nCmd = 0;
8188  char **azCmd = 0;
8189  const char *zVfs = 0;           /* Value of -vfs command-line option */
8190
8191  setBinaryMode(stdin, 0);
8192  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
8193  stdin_is_interactive = isatty(0);
8194  stdout_is_console = isatty(1);
8195
8196#if USE_SYSTEM_SQLITE+0!=1
8197  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
8198    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
8199            sqlite3_sourceid(), SQLITE_SOURCE_ID);
8200    exit(1);
8201  }
8202#endif
8203  main_init(&data);
8204
8205  /* On Windows, we must translate command-line arguments into UTF-8.
8206  ** The SQLite memory allocator subsystem has to be enabled in order to
8207  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
8208  ** subsequent sqlite3_config() calls will work.  So copy all results into
8209  ** memory that does not come from the SQLite memory allocator.
8210  */
8211#if !SQLITE_SHELL_IS_UTF8
8212  sqlite3_initialize();
8213  argv = malloc(sizeof(argv[0])*argc);
8214  if( argv==0 ){
8215    raw_printf(stderr, "out of memory\n");
8216    exit(1);
8217  }
8218  for(i=0; i<argc; i++){
8219    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
8220    int n;
8221    if( z==0 ){
8222      raw_printf(stderr, "out of memory\n");
8223      exit(1);
8224    }
8225    n = (int)strlen(z);
8226    argv[i] = malloc( n+1 );
8227    if( argv[i]==0 ){
8228      raw_printf(stderr, "out of memory\n");
8229      exit(1);
8230    }
8231    memcpy(argv[i], z, n+1);
8232    sqlite3_free(z);
8233  }
8234  sqlite3_shutdown();
8235#endif
8236
8237  assert( argc>=1 && argv && argv[0] );
8238  Argv0 = argv[0];
8239
8240  /* Make sure we have a valid signal handler early, before anything
8241  ** else is done.
8242  */
8243#ifdef SIGINT
8244  signal(SIGINT, interrupt_handler);
8245#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
8246  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
8247#endif
8248
8249#ifdef SQLITE_SHELL_DBNAME_PROC
8250  {
8251    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
8252    ** of a C-function that will provide the name of the database file.  Use
8253    ** this compile-time option to embed this shell program in larger
8254    ** applications. */
8255    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
8256    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
8257    warnInmemoryDb = 0;
8258  }
8259#endif
8260
8261  /* Do an initial pass through the command-line argument to locate
8262  ** the name of the database file, the name of the initialization file,
8263  ** the size of the alternative malloc heap,
8264  ** and the first command to execute.
8265  */
8266  verify_uninitialized();
8267  for(i=1; i<argc; i++){
8268    char *z;
8269    z = argv[i];
8270    if( z[0]!='-' ){
8271      if( data.zDbFilename==0 ){
8272        data.zDbFilename = z;
8273      }else{
8274        /* Excesss arguments are interpreted as SQL (or dot-commands) and
8275        ** mean that nothing is read from stdin */
8276        readStdin = 0;
8277        nCmd++;
8278        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
8279        if( azCmd==0 ){
8280          raw_printf(stderr, "out of memory\n");
8281          exit(1);
8282        }
8283        azCmd[nCmd-1] = z;
8284      }
8285    }
8286    if( z[1]=='-' ) z++;
8287    if( strcmp(z,"-separator")==0
8288     || strcmp(z,"-nullvalue")==0
8289     || strcmp(z,"-newline")==0
8290     || strcmp(z,"-cmd")==0
8291    ){
8292      (void)cmdline_option_value(argc, argv, ++i);
8293    }else if( strcmp(z,"-init")==0 ){
8294      zInitFile = cmdline_option_value(argc, argv, ++i);
8295    }else if( strcmp(z,"-batch")==0 ){
8296      /* Need to check for batch mode here to so we can avoid printing
8297      ** informational messages (like from process_sqliterc) before
8298      ** we do the actual processing of arguments later in a second pass.
8299      */
8300      stdin_is_interactive = 0;
8301    }else if( strcmp(z,"-heap")==0 ){
8302#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8303      const char *zSize;
8304      sqlite3_int64 szHeap;
8305
8306      zSize = cmdline_option_value(argc, argv, ++i);
8307      szHeap = integerValue(zSize);
8308      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
8309      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
8310#else
8311      (void)cmdline_option_value(argc, argv, ++i);
8312#endif
8313    }else if( strcmp(z,"-pagecache")==0 ){
8314      int n, sz;
8315      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
8316      if( sz>70000 ) sz = 70000;
8317      if( sz<0 ) sz = 0;
8318      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
8319      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
8320                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
8321      data.shellFlgs |= SHFLG_Pagecache;
8322    }else if( strcmp(z,"-lookaside")==0 ){
8323      int n, sz;
8324      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
8325      if( sz<0 ) sz = 0;
8326      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
8327      if( n<0 ) n = 0;
8328      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
8329      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
8330#ifdef SQLITE_ENABLE_VFSTRACE
8331    }else if( strcmp(z,"-vfstrace")==0 ){
8332      extern int vfstrace_register(
8333         const char *zTraceName,
8334         const char *zOldVfsName,
8335         int (*xOut)(const char*,void*),
8336         void *pOutArg,
8337         int makeDefault
8338      );
8339      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
8340#endif
8341#ifdef SQLITE_ENABLE_MULTIPLEX
8342    }else if( strcmp(z,"-multiplex")==0 ){
8343      extern int sqlite3_multiple_initialize(const char*,int);
8344      sqlite3_multiplex_initialize(0, 1);
8345#endif
8346    }else if( strcmp(z,"-mmap")==0 ){
8347      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
8348      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
8349#ifdef SQLITE_ENABLE_SORTER_REFERENCES
8350    }else if( strcmp(z,"-sorterref")==0 ){
8351      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
8352      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
8353#endif
8354    }else if( strcmp(z,"-vfs")==0 ){
8355      zVfs = cmdline_option_value(argc, argv, ++i);
8356#ifdef SQLITE_HAVE_ZLIB
8357    }else if( strcmp(z,"-zip")==0 ){
8358      data.openMode = SHELL_OPEN_ZIPFILE;
8359#endif
8360    }else if( strcmp(z,"-append")==0 ){
8361      data.openMode = SHELL_OPEN_APPENDVFS;
8362    }else if( strcmp(z,"-readonly")==0 ){
8363      data.openMode = SHELL_OPEN_READONLY;
8364#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
8365    }else if( strncmp(z, "-A",2)==0 ){
8366      /* All remaining command-line arguments are passed to the ".archive"
8367      ** command, so ignore them */
8368      break;
8369#endif
8370    }
8371  }
8372  verify_uninitialized();
8373
8374
8375  /* All the sqlite3_config() calls have now been made. So it is safe
8376  ** to call sqlite3_initialize() and process any command line -vfs option. */
8377  sqlite3_initialize();
8378  if( zVfs ){
8379    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
8380    if( pVfs ){
8381      sqlite3_vfs_register(pVfs, 1);
8382    }else{
8383      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
8384      exit(1);
8385    }
8386  }
8387
8388  if( data.zDbFilename==0 ){
8389#ifndef SQLITE_OMIT_MEMORYDB
8390    data.zDbFilename = ":memory:";
8391    warnInmemoryDb = argc==1;
8392#else
8393    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
8394    return 1;
8395#endif
8396  }
8397  data.out = stdout;
8398  sqlite3_appendvfs_init(0,0,0);
8399
8400  /* Go ahead and open the database file if it already exists.  If the
8401  ** file does not exist, delay opening it.  This prevents empty database
8402  ** files from being created if a user mistypes the database name argument
8403  ** to the sqlite command-line tool.
8404  */
8405  if( access(data.zDbFilename, 0)==0 ){
8406    open_db(&data, 0);
8407  }
8408
8409  /* Process the initialization file if there is one.  If no -init option
8410  ** is given on the command line, look for a file named ~/.sqliterc and
8411  ** try to process it.
8412  */
8413  process_sqliterc(&data,zInitFile);
8414
8415  /* Make a second pass through the command-line argument and set
8416  ** options.  This second pass is delayed until after the initialization
8417  ** file is processed so that the command-line arguments will override
8418  ** settings in the initialization file.
8419  */
8420  for(i=1; i<argc; i++){
8421    char *z = argv[i];
8422    if( z[0]!='-' ) continue;
8423    if( z[1]=='-' ){ z++; }
8424    if( strcmp(z,"-init")==0 ){
8425      i++;
8426    }else if( strcmp(z,"-html")==0 ){
8427      data.mode = MODE_Html;
8428    }else if( strcmp(z,"-list")==0 ){
8429      data.mode = MODE_List;
8430    }else if( strcmp(z,"-quote")==0 ){
8431      data.mode = MODE_Quote;
8432    }else if( strcmp(z,"-line")==0 ){
8433      data.mode = MODE_Line;
8434    }else if( strcmp(z,"-column")==0 ){
8435      data.mode = MODE_Column;
8436    }else if( strcmp(z,"-csv")==0 ){
8437      data.mode = MODE_Csv;
8438      memcpy(data.colSeparator,",",2);
8439#ifdef SQLITE_HAVE_ZLIB
8440    }else if( strcmp(z,"-zip")==0 ){
8441      data.openMode = SHELL_OPEN_ZIPFILE;
8442#endif
8443    }else if( strcmp(z,"-append")==0 ){
8444      data.openMode = SHELL_OPEN_APPENDVFS;
8445    }else if( strcmp(z,"-readonly")==0 ){
8446      data.openMode = SHELL_OPEN_READONLY;
8447    }else if( strcmp(z,"-ascii")==0 ){
8448      data.mode = MODE_Ascii;
8449      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
8450                       SEP_Unit);
8451      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
8452                       SEP_Record);
8453    }else if( strcmp(z,"-separator")==0 ){
8454      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
8455                       "%s",cmdline_option_value(argc,argv,++i));
8456    }else if( strcmp(z,"-newline")==0 ){
8457      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
8458                       "%s",cmdline_option_value(argc,argv,++i));
8459    }else if( strcmp(z,"-nullvalue")==0 ){
8460      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
8461                       "%s",cmdline_option_value(argc,argv,++i));
8462    }else if( strcmp(z,"-header")==0 ){
8463      data.showHeader = 1;
8464    }else if( strcmp(z,"-noheader")==0 ){
8465      data.showHeader = 0;
8466    }else if( strcmp(z,"-echo")==0 ){
8467      ShellSetFlag(&data, SHFLG_Echo);
8468    }else if( strcmp(z,"-eqp")==0 ){
8469      data.autoEQP = AUTOEQP_on;
8470    }else if( strcmp(z,"-eqpfull")==0 ){
8471      data.autoEQP = AUTOEQP_full;
8472    }else if( strcmp(z,"-stats")==0 ){
8473      data.statsOn = 1;
8474    }else if( strcmp(z,"-scanstats")==0 ){
8475      data.scanstatsOn = 1;
8476    }else if( strcmp(z,"-backslash")==0 ){
8477      /* Undocumented command-line option: -backslash
8478      ** Causes C-style backslash escapes to be evaluated in SQL statements
8479      ** prior to sending the SQL into SQLite.  Useful for injecting
8480      ** crazy bytes in the middle of SQL statements for testing and debugging.
8481      */
8482      ShellSetFlag(&data, SHFLG_Backslash);
8483    }else if( strcmp(z,"-bail")==0 ){
8484      bail_on_error = 1;
8485    }else if( strcmp(z,"-version")==0 ){
8486      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
8487      return 0;
8488    }else if( strcmp(z,"-interactive")==0 ){
8489      stdin_is_interactive = 1;
8490    }else if( strcmp(z,"-batch")==0 ){
8491      stdin_is_interactive = 0;
8492    }else if( strcmp(z,"-heap")==0 ){
8493      i++;
8494    }else if( strcmp(z,"-pagecache")==0 ){
8495      i+=2;
8496    }else if( strcmp(z,"-lookaside")==0 ){
8497      i+=2;
8498    }else if( strcmp(z,"-mmap")==0 ){
8499      i++;
8500#ifdef SQLITE_ENABLE_SORTER_REFERENCES
8501    }else if( strcmp(z,"-sorterref")==0 ){
8502      i++;
8503#endif
8504    }else if( strcmp(z,"-vfs")==0 ){
8505      i++;
8506#ifdef SQLITE_ENABLE_VFSTRACE
8507    }else if( strcmp(z,"-vfstrace")==0 ){
8508      i++;
8509#endif
8510#ifdef SQLITE_ENABLE_MULTIPLEX
8511    }else if( strcmp(z,"-multiplex")==0 ){
8512      i++;
8513#endif
8514    }else if( strcmp(z,"-help")==0 ){
8515      usage(1);
8516    }else if( strcmp(z,"-cmd")==0 ){
8517      /* Run commands that follow -cmd first and separately from commands
8518      ** that simply appear on the command-line.  This seems goofy.  It would
8519      ** be better if all commands ran in the order that they appear.  But
8520      ** we retain the goofy behavior for historical compatibility. */
8521      if( i==argc-1 ) break;
8522      z = cmdline_option_value(argc,argv,++i);
8523      if( z[0]=='.' ){
8524        rc = do_meta_command(z, &data);
8525        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
8526      }else{
8527        open_db(&data, 0);
8528        rc = shell_exec(&data, z, &zErrMsg);
8529        if( zErrMsg!=0 ){
8530          utf8_printf(stderr,"Error: %s\n", zErrMsg);
8531          if( bail_on_error ) return rc!=0 ? rc : 1;
8532        }else if( rc!=0 ){
8533          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
8534          if( bail_on_error ) return rc;
8535        }
8536      }
8537#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
8538    }else if( strncmp(z, "-A", 2)==0 ){
8539      if( nCmd>0 ){
8540        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
8541                            " with \"%s\"\n", z);
8542        return 1;
8543      }
8544      open_db(&data, 0);
8545      if( z[2] ){
8546        argv[i] = &z[2];
8547        arDotCommand(&data, argv+(i-1), argc-(i-1));
8548      }else{
8549        arDotCommand(&data, argv+i, argc-i);
8550      }
8551      readStdin = 0;
8552      break;
8553#endif
8554    }else{
8555      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
8556      raw_printf(stderr,"Use -help for a list of options.\n");
8557      return 1;
8558    }
8559    data.cMode = data.mode;
8560  }
8561
8562  if( !readStdin ){
8563    /* Run all arguments that do not begin with '-' as if they were separate
8564    ** command-line inputs, except for the argToSkip argument which contains
8565    ** the database filename.
8566    */
8567    for(i=0; i<nCmd; i++){
8568      if( azCmd[i][0]=='.' ){
8569        rc = do_meta_command(azCmd[i], &data);
8570        if( rc ) return rc==2 ? 0 : rc;
8571      }else{
8572        open_db(&data, 0);
8573        rc = shell_exec(&data, azCmd[i], &zErrMsg);
8574        if( zErrMsg!=0 ){
8575          utf8_printf(stderr,"Error: %s\n", zErrMsg);
8576          return rc!=0 ? rc : 1;
8577        }else if( rc!=0 ){
8578          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
8579          return rc;
8580        }
8581      }
8582    }
8583    free(azCmd);
8584  }else{
8585    /* Run commands received from standard input
8586    */
8587    if( stdin_is_interactive ){
8588      char *zHome;
8589      char *zHistory = 0;
8590      int nHistory;
8591      printf(
8592        "SQLite version %s %.19s\n" /*extra-version-info*/
8593        "Enter \".help\" for usage hints.\n",
8594        sqlite3_libversion(), sqlite3_sourceid()
8595      );
8596      if( warnInmemoryDb ){
8597        printf("Connected to a ");
8598        printBold("transient in-memory database");
8599        printf(".\nUse \".open FILENAME\" to reopen on a "
8600               "persistent database.\n");
8601      }
8602      zHome = find_home_dir(0);
8603      if( zHome ){
8604        nHistory = strlen30(zHome) + 20;
8605        if( (zHistory = malloc(nHistory))!=0 ){
8606          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
8607        }
8608      }
8609      if( zHistory ){ shell_read_history(zHistory); }
8610#if HAVE_READLINE || HAVE_EDITLINE
8611      rl_attempted_completion_function = readline_completion;
8612#elif HAVE_LINENOISE
8613      linenoiseSetCompletionCallback(linenoise_completion);
8614#endif
8615      rc = process_input(&data, 0);
8616      if( zHistory ){
8617        shell_stifle_history(2000);
8618        shell_write_history(zHistory);
8619        free(zHistory);
8620      }
8621    }else{
8622      rc = process_input(&data, stdin);
8623    }
8624  }
8625  set_table_name(&data, 0);
8626  if( data.db ){
8627    session_close_all(&data);
8628    sqlite3_close(data.db);
8629  }
8630  sqlite3_free(data.zFreeOnClose);
8631  find_home_dir(1);
8632  output_reset(&data);
8633  data.doXdgOpen = 0;
8634  clearTempFile(&data);
8635#if !SQLITE_SHELL_IS_UTF8
8636  for(i=0; i<argc; i++) free(argv[i]);
8637  free(argv);
8638#endif
8639  return rc;
8640}
8641