xref: /sqlite-3.40.0/src/shell.c.in (revision 43a9385d)
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    sqlite3_initialize();
3488    if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
3489      p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
3490    }
3491    switch( p->openMode ){
3492      case SHELL_OPEN_APPENDVFS: {
3493        sqlite3_open_v2(p->zDbFilename, &p->db,
3494           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
3495        break;
3496      }
3497      case SHELL_OPEN_ZIPFILE: {
3498        sqlite3_open(":memory:", &p->db);
3499        break;
3500      }
3501      case SHELL_OPEN_READONLY: {
3502        sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
3503        break;
3504      }
3505      case SHELL_OPEN_UNSPEC:
3506      case SHELL_OPEN_NORMAL: {
3507        sqlite3_open(p->zDbFilename, &p->db);
3508        break;
3509      }
3510    }
3511    globalDb = p->db;
3512    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
3513      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
3514          p->zDbFilename, sqlite3_errmsg(p->db));
3515      if( keepAlive ) return;
3516      exit(1);
3517    }
3518#ifndef SQLITE_OMIT_LOAD_EXTENSION
3519    sqlite3_enable_load_extension(p->db, 1);
3520#endif
3521    sqlite3_fileio_init(p->db, 0, 0);
3522    sqlite3_shathree_init(p->db, 0, 0);
3523    sqlite3_completion_init(p->db, 0, 0);
3524#ifdef SQLITE_HAVE_ZLIB
3525    sqlite3_zipfile_init(p->db, 0, 0);
3526    sqlite3_sqlar_init(p->db, 0, 0);
3527#endif
3528    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
3529                            shellAddSchemaName, 0, 0);
3530    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
3531                            shellModuleSchema, 0, 0);
3532    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
3533                            shellPutsFunc, 0, 0);
3534#ifndef SQLITE_NOHAVE_SYSTEM
3535    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
3536                            editFunc, 0, 0);
3537    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
3538                            editFunc, 0, 0);
3539#endif
3540    if( p->openMode==SHELL_OPEN_ZIPFILE ){
3541      char *zSql = sqlite3_mprintf(
3542         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
3543      sqlite3_exec(p->db, zSql, 0, 0, 0);
3544      sqlite3_free(zSql);
3545    }
3546  }
3547}
3548
3549#if HAVE_READLINE || HAVE_EDITLINE
3550/*
3551** Readline completion callbacks
3552*/
3553static char *readline_completion_generator(const char *text, int state){
3554  static sqlite3_stmt *pStmt = 0;
3555  char *zRet;
3556  if( state==0 ){
3557    char *zSql;
3558    sqlite3_finalize(pStmt);
3559    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
3560                           "  FROM completion(%Q) ORDER BY 1", text);
3561    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
3562    sqlite3_free(zSql);
3563  }
3564  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3565    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
3566  }else{
3567    sqlite3_finalize(pStmt);
3568    pStmt = 0;
3569    zRet = 0;
3570  }
3571  return zRet;
3572}
3573static char **readline_completion(const char *zText, int iStart, int iEnd){
3574  rl_attempted_completion_over = 1;
3575  return rl_completion_matches(zText, readline_completion_generator);
3576}
3577
3578#elif HAVE_LINENOISE
3579/*
3580** Linenoise completion callback
3581*/
3582static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
3583  int nLine = strlen30(zLine);
3584  int i, iStart;
3585  sqlite3_stmt *pStmt = 0;
3586  char *zSql;
3587  char zBuf[1000];
3588
3589  if( nLine>sizeof(zBuf)-30 ) return;
3590  if( zLine[0]=='.' ) return;
3591  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
3592  if( i==nLine-1 ) return;
3593  iStart = i+1;
3594  memcpy(zBuf, zLine, iStart);
3595  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
3596                         "  FROM completion(%Q,%Q) ORDER BY 1",
3597                         &zLine[iStart], zLine);
3598  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
3599  sqlite3_free(zSql);
3600  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
3601  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3602    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
3603    int nCompletion = sqlite3_column_bytes(pStmt, 0);
3604    if( iStart+nCompletion < sizeof(zBuf)-1 ){
3605      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
3606      linenoiseAddCompletion(lc, zBuf);
3607    }
3608  }
3609  sqlite3_finalize(pStmt);
3610}
3611#endif
3612
3613/*
3614** Do C-language style dequoting.
3615**
3616**    \a    -> alarm
3617**    \b    -> backspace
3618**    \t    -> tab
3619**    \n    -> newline
3620**    \v    -> vertical tab
3621**    \f    -> form feed
3622**    \r    -> carriage return
3623**    \s    -> space
3624**    \"    -> "
3625**    \'    -> '
3626**    \\    -> backslash
3627**    \NNN  -> ascii character NNN in octal
3628*/
3629static void resolve_backslashes(char *z){
3630  int i, j;
3631  char c;
3632  while( *z && *z!='\\' ) z++;
3633  for(i=j=0; (c = z[i])!=0; i++, j++){
3634    if( c=='\\' && z[i+1]!=0 ){
3635      c = z[++i];
3636      if( c=='a' ){
3637        c = '\a';
3638      }else if( c=='b' ){
3639        c = '\b';
3640      }else if( c=='t' ){
3641        c = '\t';
3642      }else if( c=='n' ){
3643        c = '\n';
3644      }else if( c=='v' ){
3645        c = '\v';
3646      }else if( c=='f' ){
3647        c = '\f';
3648      }else if( c=='r' ){
3649        c = '\r';
3650      }else if( c=='"' ){
3651        c = '"';
3652      }else if( c=='\'' ){
3653        c = '\'';
3654      }else if( c=='\\' ){
3655        c = '\\';
3656      }else if( c>='0' && c<='7' ){
3657        c -= '0';
3658        if( z[i+1]>='0' && z[i+1]<='7' ){
3659          i++;
3660          c = (c<<3) + z[i] - '0';
3661          if( z[i+1]>='0' && z[i+1]<='7' ){
3662            i++;
3663            c = (c<<3) + z[i] - '0';
3664          }
3665        }
3666      }
3667    }
3668    z[j] = c;
3669  }
3670  if( j<i ) z[j] = 0;
3671}
3672
3673/*
3674** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
3675** for TRUE and FALSE.  Return the integer value if appropriate.
3676*/
3677static int booleanValue(const char *zArg){
3678  int i;
3679  if( zArg[0]=='0' && zArg[1]=='x' ){
3680    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
3681  }else{
3682    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
3683  }
3684  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
3685  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
3686    return 1;
3687  }
3688  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
3689    return 0;
3690  }
3691  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
3692          zArg);
3693  return 0;
3694}
3695
3696/*
3697** Set or clear a shell flag according to a boolean value.
3698*/
3699static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
3700  if( booleanValue(zArg) ){
3701    ShellSetFlag(p, mFlag);
3702  }else{
3703    ShellClearFlag(p, mFlag);
3704  }
3705}
3706
3707/*
3708** Close an output file, assuming it is not stderr or stdout
3709*/
3710static void output_file_close(FILE *f){
3711  if( f && f!=stdout && f!=stderr ) fclose(f);
3712}
3713
3714/*
3715** Try to open an output file.   The names "stdout" and "stderr" are
3716** recognized and do the right thing.  NULL is returned if the output
3717** filename is "off".
3718*/
3719static FILE *output_file_open(const char *zFile, int bTextMode){
3720  FILE *f;
3721  if( strcmp(zFile,"stdout")==0 ){
3722    f = stdout;
3723  }else if( strcmp(zFile, "stderr")==0 ){
3724    f = stderr;
3725  }else if( strcmp(zFile, "off")==0 ){
3726    f = 0;
3727  }else{
3728    f = fopen(zFile, bTextMode ? "w" : "wb");
3729    if( f==0 ){
3730      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
3731    }
3732  }
3733  return f;
3734}
3735
3736#if !defined(SQLITE_UNTESTABLE)
3737#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3738/*
3739** A routine for handling output from sqlite3_trace().
3740*/
3741static int sql_trace_callback(
3742  unsigned mType,
3743  void *pArg,
3744  void *pP,
3745  void *pX
3746){
3747  FILE *f = (FILE*)pArg;
3748  UNUSED_PARAMETER(mType);
3749  UNUSED_PARAMETER(pP);
3750  if( f ){
3751    const char *z = (const char*)pX;
3752    int i = strlen30(z);
3753    while( i>0 && z[i-1]==';' ){ i--; }
3754    utf8_printf(f, "%.*s;\n", i, z);
3755  }
3756  return 0;
3757}
3758#endif
3759#endif
3760
3761/*
3762** A no-op routine that runs with the ".breakpoint" doc-command.  This is
3763** a useful spot to set a debugger breakpoint.
3764*/
3765static void test_breakpoint(void){
3766  static int nCall = 0;
3767  nCall++;
3768}
3769
3770/*
3771** An object used to read a CSV and other files for import.
3772*/
3773typedef struct ImportCtx ImportCtx;
3774struct ImportCtx {
3775  const char *zFile;  /* Name of the input file */
3776  FILE *in;           /* Read the CSV text from this input stream */
3777  char *z;            /* Accumulated text for a field */
3778  int n;              /* Number of bytes in z */
3779  int nAlloc;         /* Space allocated for z[] */
3780  int nLine;          /* Current line number */
3781  int bNotFirst;      /* True if one or more bytes already read */
3782  int cTerm;          /* Character that terminated the most recent field */
3783  int cColSep;        /* The column separator character.  (Usually ",") */
3784  int cRowSep;        /* The row separator character.  (Usually "\n") */
3785};
3786
3787/* Append a single byte to z[] */
3788static void import_append_char(ImportCtx *p, int c){
3789  if( p->n+1>=p->nAlloc ){
3790    p->nAlloc += p->nAlloc + 100;
3791    p->z = sqlite3_realloc64(p->z, p->nAlloc);
3792    if( p->z==0 ){
3793      raw_printf(stderr, "out of memory\n");
3794      exit(1);
3795    }
3796  }
3797  p->z[p->n++] = (char)c;
3798}
3799
3800/* Read a single field of CSV text.  Compatible with rfc4180 and extended
3801** with the option of having a separator other than ",".
3802**
3803**   +  Input comes from p->in.
3804**   +  Store results in p->z of length p->n.  Space to hold p->z comes
3805**      from sqlite3_malloc64().
3806**   +  Use p->cSep as the column separator.  The default is ",".
3807**   +  Use p->rSep as the row separator.  The default is "\n".
3808**   +  Keep track of the line number in p->nLine.
3809**   +  Store the character that terminates the field in p->cTerm.  Store
3810**      EOF on end-of-file.
3811**   +  Report syntax errors on stderr
3812*/
3813static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
3814  int c;
3815  int cSep = p->cColSep;
3816  int rSep = p->cRowSep;
3817  p->n = 0;
3818  c = fgetc(p->in);
3819  if( c==EOF || seenInterrupt ){
3820    p->cTerm = EOF;
3821    return 0;
3822  }
3823  if( c=='"' ){
3824    int pc, ppc;
3825    int startLine = p->nLine;
3826    int cQuote = c;
3827    pc = ppc = 0;
3828    while( 1 ){
3829      c = fgetc(p->in);
3830      if( c==rSep ) p->nLine++;
3831      if( c==cQuote ){
3832        if( pc==cQuote ){
3833          pc = 0;
3834          continue;
3835        }
3836      }
3837      if( (c==cSep && pc==cQuote)
3838       || (c==rSep && pc==cQuote)
3839       || (c==rSep && pc=='\r' && ppc==cQuote)
3840       || (c==EOF && pc==cQuote)
3841      ){
3842        do{ p->n--; }while( p->z[p->n]!=cQuote );
3843        p->cTerm = c;
3844        break;
3845      }
3846      if( pc==cQuote && c!='\r' ){
3847        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
3848                p->zFile, p->nLine, cQuote);
3849      }
3850      if( c==EOF ){
3851        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
3852                p->zFile, startLine, cQuote);
3853        p->cTerm = c;
3854        break;
3855      }
3856      import_append_char(p, c);
3857      ppc = pc;
3858      pc = c;
3859    }
3860  }else{
3861    /* If this is the first field being parsed and it begins with the
3862    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
3863    if( (c&0xff)==0xef && p->bNotFirst==0 ){
3864      import_append_char(p, c);
3865      c = fgetc(p->in);
3866      if( (c&0xff)==0xbb ){
3867        import_append_char(p, c);
3868        c = fgetc(p->in);
3869        if( (c&0xff)==0xbf ){
3870          p->bNotFirst = 1;
3871          p->n = 0;
3872          return csv_read_one_field(p);
3873        }
3874      }
3875    }
3876    while( c!=EOF && c!=cSep && c!=rSep ){
3877      import_append_char(p, c);
3878      c = fgetc(p->in);
3879    }
3880    if( c==rSep ){
3881      p->nLine++;
3882      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
3883    }
3884    p->cTerm = c;
3885  }
3886  if( p->z ) p->z[p->n] = 0;
3887  p->bNotFirst = 1;
3888  return p->z;
3889}
3890
3891/* Read a single field of ASCII delimited text.
3892**
3893**   +  Input comes from p->in.
3894**   +  Store results in p->z of length p->n.  Space to hold p->z comes
3895**      from sqlite3_malloc64().
3896**   +  Use p->cSep as the column separator.  The default is "\x1F".
3897**   +  Use p->rSep as the row separator.  The default is "\x1E".
3898**   +  Keep track of the row number in p->nLine.
3899**   +  Store the character that terminates the field in p->cTerm.  Store
3900**      EOF on end-of-file.
3901**   +  Report syntax errors on stderr
3902*/
3903static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
3904  int c;
3905  int cSep = p->cColSep;
3906  int rSep = p->cRowSep;
3907  p->n = 0;
3908  c = fgetc(p->in);
3909  if( c==EOF || seenInterrupt ){
3910    p->cTerm = EOF;
3911    return 0;
3912  }
3913  while( c!=EOF && c!=cSep && c!=rSep ){
3914    import_append_char(p, c);
3915    c = fgetc(p->in);
3916  }
3917  if( c==rSep ){
3918    p->nLine++;
3919  }
3920  p->cTerm = c;
3921  if( p->z ) p->z[p->n] = 0;
3922  return p->z;
3923}
3924
3925/*
3926** Try to transfer data for table zTable.  If an error is seen while
3927** moving forward, try to go backwards.  The backwards movement won't
3928** work for WITHOUT ROWID tables.
3929*/
3930static void tryToCloneData(
3931  ShellState *p,
3932  sqlite3 *newDb,
3933  const char *zTable
3934){
3935  sqlite3_stmt *pQuery = 0;
3936  sqlite3_stmt *pInsert = 0;
3937  char *zQuery = 0;
3938  char *zInsert = 0;
3939  int rc;
3940  int i, j, n;
3941  int nTable = strlen30(zTable);
3942  int k = 0;
3943  int cnt = 0;
3944  const int spinRate = 10000;
3945
3946  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
3947  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3948  if( rc ){
3949    utf8_printf(stderr, "Error %d: %s on [%s]\n",
3950            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3951            zQuery);
3952    goto end_data_xfer;
3953  }
3954  n = sqlite3_column_count(pQuery);
3955  zInsert = sqlite3_malloc64(200 + nTable + n*3);
3956  if( zInsert==0 ){
3957    raw_printf(stderr, "out of memory\n");
3958    goto end_data_xfer;
3959  }
3960  sqlite3_snprintf(200+nTable,zInsert,
3961                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
3962  i = strlen30(zInsert);
3963  for(j=1; j<n; j++){
3964    memcpy(zInsert+i, ",?", 2);
3965    i += 2;
3966  }
3967  memcpy(zInsert+i, ");", 3);
3968  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
3969  if( rc ){
3970    utf8_printf(stderr, "Error %d: %s on [%s]\n",
3971            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
3972            zQuery);
3973    goto end_data_xfer;
3974  }
3975  for(k=0; k<2; k++){
3976    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3977      for(i=0; i<n; i++){
3978        switch( sqlite3_column_type(pQuery, i) ){
3979          case SQLITE_NULL: {
3980            sqlite3_bind_null(pInsert, i+1);
3981            break;
3982          }
3983          case SQLITE_INTEGER: {
3984            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
3985            break;
3986          }
3987          case SQLITE_FLOAT: {
3988            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
3989            break;
3990          }
3991          case SQLITE_TEXT: {
3992            sqlite3_bind_text(pInsert, i+1,
3993                             (const char*)sqlite3_column_text(pQuery,i),
3994                             -1, SQLITE_STATIC);
3995            break;
3996          }
3997          case SQLITE_BLOB: {
3998            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
3999                                            sqlite3_column_bytes(pQuery,i),
4000                                            SQLITE_STATIC);
4001            break;
4002          }
4003        }
4004      } /* End for */
4005      rc = sqlite3_step(pInsert);
4006      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
4007        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
4008                        sqlite3_errmsg(newDb));
4009      }
4010      sqlite3_reset(pInsert);
4011      cnt++;
4012      if( (cnt%spinRate)==0 ){
4013        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
4014        fflush(stdout);
4015      }
4016    } /* End while */
4017    if( rc==SQLITE_DONE ) break;
4018    sqlite3_finalize(pQuery);
4019    sqlite3_free(zQuery);
4020    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
4021                             zTable);
4022    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4023    if( rc ){
4024      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
4025      break;
4026    }
4027  } /* End for(k=0...) */
4028
4029end_data_xfer:
4030  sqlite3_finalize(pQuery);
4031  sqlite3_finalize(pInsert);
4032  sqlite3_free(zQuery);
4033  sqlite3_free(zInsert);
4034}
4035
4036
4037/*
4038** Try to transfer all rows of the schema that match zWhere.  For
4039** each row, invoke xForEach() on the object defined by that row.
4040** If an error is encountered while moving forward through the
4041** sqlite_master table, try again moving backwards.
4042*/
4043static void tryToCloneSchema(
4044  ShellState *p,
4045  sqlite3 *newDb,
4046  const char *zWhere,
4047  void (*xForEach)(ShellState*,sqlite3*,const char*)
4048){
4049  sqlite3_stmt *pQuery = 0;
4050  char *zQuery = 0;
4051  int rc;
4052  const unsigned char *zName;
4053  const unsigned char *zSql;
4054  char *zErrMsg = 0;
4055
4056  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4057                           " WHERE %s", zWhere);
4058  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4059  if( rc ){
4060    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4061                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4062                    zQuery);
4063    goto end_schema_xfer;
4064  }
4065  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4066    zName = sqlite3_column_text(pQuery, 0);
4067    zSql = sqlite3_column_text(pQuery, 1);
4068    printf("%s... ", zName); fflush(stdout);
4069    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4070    if( zErrMsg ){
4071      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4072      sqlite3_free(zErrMsg);
4073      zErrMsg = 0;
4074    }
4075    if( xForEach ){
4076      xForEach(p, newDb, (const char*)zName);
4077    }
4078    printf("done\n");
4079  }
4080  if( rc!=SQLITE_DONE ){
4081    sqlite3_finalize(pQuery);
4082    sqlite3_free(zQuery);
4083    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4084                             " WHERE %s ORDER BY rowid DESC", zWhere);
4085    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4086    if( rc ){
4087      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4088                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4089                      zQuery);
4090      goto end_schema_xfer;
4091    }
4092    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4093      zName = sqlite3_column_text(pQuery, 0);
4094      zSql = sqlite3_column_text(pQuery, 1);
4095      printf("%s... ", zName); fflush(stdout);
4096      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4097      if( zErrMsg ){
4098        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4099        sqlite3_free(zErrMsg);
4100        zErrMsg = 0;
4101      }
4102      if( xForEach ){
4103        xForEach(p, newDb, (const char*)zName);
4104      }
4105      printf("done\n");
4106    }
4107  }
4108end_schema_xfer:
4109  sqlite3_finalize(pQuery);
4110  sqlite3_free(zQuery);
4111}
4112
4113/*
4114** Open a new database file named "zNewDb".  Try to recover as much information
4115** as possible out of the main database (which might be corrupt) and write it
4116** into zNewDb.
4117*/
4118static void tryToClone(ShellState *p, const char *zNewDb){
4119  int rc;
4120  sqlite3 *newDb = 0;
4121  if( access(zNewDb,0)==0 ){
4122    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
4123    return;
4124  }
4125  rc = sqlite3_open(zNewDb, &newDb);
4126  if( rc ){
4127    utf8_printf(stderr, "Cannot create output database: %s\n",
4128            sqlite3_errmsg(newDb));
4129  }else{
4130    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
4131    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
4132    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
4133    tryToCloneSchema(p, newDb, "type!='table'", 0);
4134    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
4135    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4136  }
4137  sqlite3_close(newDb);
4138}
4139
4140/*
4141** Change the output file back to stdout.
4142**
4143** If the p->doXdgOpen flag is set, that means the output was being
4144** redirected to a temporary file named by p->zTempFile.  In that case,
4145** launch start/open/xdg-open on that temporary file.
4146*/
4147static void output_reset(ShellState *p){
4148  if( p->outfile[0]=='|' ){
4149#ifndef SQLITE_OMIT_POPEN
4150    pclose(p->out);
4151#endif
4152  }else{
4153    output_file_close(p->out);
4154#ifndef SQLITE_NOHAVE_SYSTEM
4155    if( p->doXdgOpen ){
4156      const char *zXdgOpenCmd =
4157#if defined(_WIN32)
4158      "start";
4159#elif defined(__APPLE__)
4160      "open";
4161#else
4162      "xdg-open";
4163#endif
4164      char *zCmd;
4165      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
4166      if( system(zCmd) ){
4167        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
4168      }
4169      sqlite3_free(zCmd);
4170      outputModePop(p);
4171      p->doXdgOpen = 0;
4172    }
4173#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
4174  }
4175  p->outfile[0] = 0;
4176  p->out = stdout;
4177}
4178
4179/*
4180** Run an SQL command and return the single integer result.
4181*/
4182static int db_int(ShellState *p, const char *zSql){
4183  sqlite3_stmt *pStmt;
4184  int res = 0;
4185  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4186  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
4187    res = sqlite3_column_int(pStmt,0);
4188  }
4189  sqlite3_finalize(pStmt);
4190  return res;
4191}
4192
4193/*
4194** Convert a 2-byte or 4-byte big-endian integer into a native integer
4195*/
4196static unsigned int get2byteInt(unsigned char *a){
4197  return (a[0]<<8) + a[1];
4198}
4199static unsigned int get4byteInt(unsigned char *a){
4200  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
4201}
4202
4203/*
4204** Implementation of the ".info" command.
4205**
4206** Return 1 on error, 2 to exit, and 0 otherwise.
4207*/
4208static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
4209  static const struct { const char *zName; int ofst; } aField[] = {
4210     { "file change counter:",  24  },
4211     { "database page count:",  28  },
4212     { "freelist page count:",  36  },
4213     { "schema cookie:",        40  },
4214     { "schema format:",        44  },
4215     { "default cache size:",   48  },
4216     { "autovacuum top root:",  52  },
4217     { "incremental vacuum:",   64  },
4218     { "text encoding:",        56  },
4219     { "user version:",         60  },
4220     { "application id:",       68  },
4221     { "software version:",     96  },
4222  };
4223  static const struct { const char *zName; const char *zSql; } aQuery[] = {
4224     { "number of tables:",
4225       "SELECT count(*) FROM %s WHERE type='table'" },
4226     { "number of indexes:",
4227       "SELECT count(*) FROM %s WHERE type='index'" },
4228     { "number of triggers:",
4229       "SELECT count(*) FROM %s WHERE type='trigger'" },
4230     { "number of views:",
4231       "SELECT count(*) FROM %s WHERE type='view'" },
4232     { "schema size:",
4233       "SELECT total(length(sql)) FROM %s" },
4234  };
4235  int i;
4236  char *zSchemaTab;
4237  char *zDb = nArg>=2 ? azArg[1] : "main";
4238  sqlite3_stmt *pStmt = 0;
4239  unsigned char aHdr[100];
4240  open_db(p, 0);
4241  if( p->db==0 ) return 1;
4242  sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
4243                     -1, &pStmt, 0);
4244  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
4245  if( sqlite3_step(pStmt)==SQLITE_ROW
4246   && sqlite3_column_bytes(pStmt,0)>100
4247  ){
4248    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
4249    sqlite3_finalize(pStmt);
4250  }else{
4251    raw_printf(stderr, "unable to read database header\n");
4252    sqlite3_finalize(pStmt);
4253    return 1;
4254  }
4255  i = get2byteInt(aHdr+16);
4256  if( i==1 ) i = 65536;
4257  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
4258  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
4259  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
4260  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
4261  for(i=0; i<ArraySize(aField); i++){
4262    int ofst = aField[i].ofst;
4263    unsigned int val = get4byteInt(aHdr + ofst);
4264    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
4265    switch( ofst ){
4266      case 56: {
4267        if( val==1 ) raw_printf(p->out, " (utf8)");
4268        if( val==2 ) raw_printf(p->out, " (utf16le)");
4269        if( val==3 ) raw_printf(p->out, " (utf16be)");
4270      }
4271    }
4272    raw_printf(p->out, "\n");
4273  }
4274  if( zDb==0 ){
4275    zSchemaTab = sqlite3_mprintf("main.sqlite_master");
4276  }else if( strcmp(zDb,"temp")==0 ){
4277    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
4278  }else{
4279    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
4280  }
4281  for(i=0; i<ArraySize(aQuery); i++){
4282    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
4283    int val = db_int(p, zSql);
4284    sqlite3_free(zSql);
4285    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
4286  }
4287  sqlite3_free(zSchemaTab);
4288  return 0;
4289}
4290
4291/*
4292** Print the current sqlite3_errmsg() value to stderr and return 1.
4293*/
4294static int shellDatabaseError(sqlite3 *db){
4295  const char *zErr = sqlite3_errmsg(db);
4296  utf8_printf(stderr, "Error: %s\n", zErr);
4297  return 1;
4298}
4299
4300/*
4301** Print an out-of-memory message to stderr and return 1.
4302*/
4303static int shellNomemError(void){
4304  raw_printf(stderr, "Error: out of memory\n");
4305  return 1;
4306}
4307
4308/*
4309** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
4310** if they match and FALSE (0) if they do not match.
4311**
4312** Globbing rules:
4313**
4314**      '*'       Matches any sequence of zero or more characters.
4315**
4316**      '?'       Matches exactly one character.
4317**
4318**     [...]      Matches one character from the enclosed list of
4319**                characters.
4320**
4321**     [^...]     Matches one character not in the enclosed list.
4322**
4323**      '#'       Matches any sequence of one or more digits with an
4324**                optional + or - sign in front
4325**
4326**      ' '       Any span of whitespace matches any other span of
4327**                whitespace.
4328**
4329** Extra whitespace at the end of z[] is ignored.
4330*/
4331static int testcase_glob(const char *zGlob, const char *z){
4332  int c, c2;
4333  int invert;
4334  int seen;
4335
4336  while( (c = (*(zGlob++)))!=0 ){
4337    if( IsSpace(c) ){
4338      if( !IsSpace(*z) ) return 0;
4339      while( IsSpace(*zGlob) ) zGlob++;
4340      while( IsSpace(*z) ) z++;
4341    }else if( c=='*' ){
4342      while( (c=(*(zGlob++))) == '*' || c=='?' ){
4343        if( c=='?' && (*(z++))==0 ) return 0;
4344      }
4345      if( c==0 ){
4346        return 1;
4347      }else if( c=='[' ){
4348        while( *z && testcase_glob(zGlob-1,z)==0 ){
4349          z++;
4350        }
4351        return (*z)!=0;
4352      }
4353      while( (c2 = (*(z++)))!=0 ){
4354        while( c2!=c ){
4355          c2 = *(z++);
4356          if( c2==0 ) return 0;
4357        }
4358        if( testcase_glob(zGlob,z) ) return 1;
4359      }
4360      return 0;
4361    }else if( c=='?' ){
4362      if( (*(z++))==0 ) return 0;
4363    }else if( c=='[' ){
4364      int prior_c = 0;
4365      seen = 0;
4366      invert = 0;
4367      c = *(z++);
4368      if( c==0 ) return 0;
4369      c2 = *(zGlob++);
4370      if( c2=='^' ){
4371        invert = 1;
4372        c2 = *(zGlob++);
4373      }
4374      if( c2==']' ){
4375        if( c==']' ) seen = 1;
4376        c2 = *(zGlob++);
4377      }
4378      while( c2 && c2!=']' ){
4379        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
4380          c2 = *(zGlob++);
4381          if( c>=prior_c && c<=c2 ) seen = 1;
4382          prior_c = 0;
4383        }else{
4384          if( c==c2 ){
4385            seen = 1;
4386          }
4387          prior_c = c2;
4388        }
4389        c2 = *(zGlob++);
4390      }
4391      if( c2==0 || (seen ^ invert)==0 ) return 0;
4392    }else if( c=='#' ){
4393      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
4394      if( !IsDigit(z[0]) ) return 0;
4395      z++;
4396      while( IsDigit(z[0]) ){ z++; }
4397    }else{
4398      if( c!=(*(z++)) ) return 0;
4399    }
4400  }
4401  while( IsSpace(*z) ){ z++; }
4402  return *z==0;
4403}
4404
4405
4406/*
4407** Compare the string as a command-line option with either one or two
4408** initial "-" characters.
4409*/
4410static int optionMatch(const char *zStr, const char *zOpt){
4411  if( zStr[0]!='-' ) return 0;
4412  zStr++;
4413  if( zStr[0]=='-' ) zStr++;
4414  return strcmp(zStr, zOpt)==0;
4415}
4416
4417/*
4418** Delete a file.
4419*/
4420int shellDeleteFile(const char *zFilename){
4421  int rc;
4422#ifdef _WIN32
4423  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
4424  rc = _wunlink(z);
4425  sqlite3_free(z);
4426#else
4427  rc = unlink(zFilename);
4428#endif
4429  return rc;
4430}
4431
4432/*
4433** Try to delete the temporary file (if there is one) and free the
4434** memory used to hold the name of the temp file.
4435*/
4436static void clearTempFile(ShellState *p){
4437  if( p->zTempFile==0 ) return;
4438  if( p->doXdgOpen ) return;
4439  if( shellDeleteFile(p->zTempFile) ) return;
4440  sqlite3_free(p->zTempFile);
4441  p->zTempFile = 0;
4442}
4443
4444/*
4445** Create a new temp file name with the given suffix.
4446*/
4447static void newTempFile(ShellState *p, const char *zSuffix){
4448  clearTempFile(p);
4449  sqlite3_free(p->zTempFile);
4450  p->zTempFile = 0;
4451  if( p->db ){
4452    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
4453  }
4454  if( p->zTempFile==0 ){
4455    sqlite3_uint64 r;
4456    sqlite3_randomness(sizeof(r), &r);
4457    p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
4458  }else{
4459    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
4460  }
4461  if( p->zTempFile==0 ){
4462    raw_printf(stderr, "out of memory\n");
4463    exit(1);
4464  }
4465}
4466
4467
4468/*
4469** The implementation of SQL scalar function fkey_collate_clause(), used
4470** by the ".lint fkey-indexes" command. This scalar function is always
4471** called with four arguments - the parent table name, the parent column name,
4472** the child table name and the child column name.
4473**
4474**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
4475**
4476** If either of the named tables or columns do not exist, this function
4477** returns an empty string. An empty string is also returned if both tables
4478** and columns exist but have the same default collation sequence. Or,
4479** if both exist but the default collation sequences are different, this
4480** function returns the string " COLLATE <parent-collation>", where
4481** <parent-collation> is the default collation sequence of the parent column.
4482*/
4483static void shellFkeyCollateClause(
4484  sqlite3_context *pCtx,
4485  int nVal,
4486  sqlite3_value **apVal
4487){
4488  sqlite3 *db = sqlite3_context_db_handle(pCtx);
4489  const char *zParent;
4490  const char *zParentCol;
4491  const char *zParentSeq;
4492  const char *zChild;
4493  const char *zChildCol;
4494  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
4495  int rc;
4496
4497  assert( nVal==4 );
4498  zParent = (const char*)sqlite3_value_text(apVal[0]);
4499  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
4500  zChild = (const char*)sqlite3_value_text(apVal[2]);
4501  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
4502
4503  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
4504  rc = sqlite3_table_column_metadata(
4505      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
4506  );
4507  if( rc==SQLITE_OK ){
4508    rc = sqlite3_table_column_metadata(
4509        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
4510    );
4511  }
4512
4513  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
4514    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
4515    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
4516    sqlite3_free(z);
4517  }
4518}
4519
4520
4521/*
4522** The implementation of dot-command ".lint fkey-indexes".
4523*/
4524static int lintFkeyIndexes(
4525  ShellState *pState,             /* Current shell tool state */
4526  char **azArg,                   /* Array of arguments passed to dot command */
4527  int nArg                        /* Number of entries in azArg[] */
4528){
4529  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
4530  FILE *out = pState->out;        /* Stream to write non-error output to */
4531  int bVerbose = 0;               /* If -verbose is present */
4532  int bGroupByParent = 0;         /* If -groupbyparent is present */
4533  int i;                          /* To iterate through azArg[] */
4534  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
4535  int rc;                         /* Return code */
4536  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
4537
4538  /*
4539  ** This SELECT statement returns one row for each foreign key constraint
4540  ** in the schema of the main database. The column values are:
4541  **
4542  ** 0. The text of an SQL statement similar to:
4543  **
4544  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
4545  **
4546  **    This SELECT is similar to the one that the foreign keys implementation
4547  **    needs to run internally on child tables. If there is an index that can
4548  **    be used to optimize this query, then it can also be used by the FK
4549  **    implementation to optimize DELETE or UPDATE statements on the parent
4550  **    table.
4551  **
4552  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
4553  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
4554  **    contains an index that can be used to optimize the query.
4555  **
4556  ** 2. Human readable text that describes the child table and columns. e.g.
4557  **
4558  **       "child_table(child_key1, child_key2)"
4559  **
4560  ** 3. Human readable text that describes the parent table and columns. e.g.
4561  **
4562  **       "parent_table(parent_key1, parent_key2)"
4563  **
4564  ** 4. A full CREATE INDEX statement for an index that could be used to
4565  **    optimize DELETE or UPDATE statements on the parent table. e.g.
4566  **
4567  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
4568  **
4569  ** 5. The name of the parent table.
4570  **
4571  ** These six values are used by the C logic below to generate the report.
4572  */
4573  const char *zSql =
4574  "SELECT "
4575    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
4576    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
4577    "  || fkey_collate_clause("
4578    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
4579    ", "
4580    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
4581    "  || group_concat('*=?', ' AND ') || ')'"
4582    ", "
4583    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
4584    ", "
4585    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
4586    ", "
4587    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
4588    "  || ' ON ' || quote(s.name) || '('"
4589    "  || group_concat(quote(f.[from]) ||"
4590    "        fkey_collate_clause("
4591    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
4592    "  || ');'"
4593    ", "
4594    "     f.[table] "
4595    "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
4596    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
4597    "GROUP BY s.name, f.id "
4598    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
4599  ;
4600  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
4601
4602  for(i=2; i<nArg; i++){
4603    int n = strlen30(azArg[i]);
4604    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
4605      bVerbose = 1;
4606    }
4607    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
4608      bGroupByParent = 1;
4609      zIndent = "    ";
4610    }
4611    else{
4612      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
4613          azArg[0], azArg[1]
4614      );
4615      return SQLITE_ERROR;
4616    }
4617  }
4618
4619  /* Register the fkey_collate_clause() SQL function */
4620  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
4621      0, shellFkeyCollateClause, 0, 0
4622  );
4623
4624
4625  if( rc==SQLITE_OK ){
4626    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
4627  }
4628  if( rc==SQLITE_OK ){
4629    sqlite3_bind_int(pSql, 1, bGroupByParent);
4630  }
4631
4632  if( rc==SQLITE_OK ){
4633    int rc2;
4634    char *zPrev = 0;
4635    while( SQLITE_ROW==sqlite3_step(pSql) ){
4636      int res = -1;
4637      sqlite3_stmt *pExplain = 0;
4638      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
4639      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
4640      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
4641      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
4642      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
4643      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
4644
4645      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
4646      if( rc!=SQLITE_OK ) break;
4647      if( SQLITE_ROW==sqlite3_step(pExplain) ){
4648        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
4649        res = (
4650              0==sqlite3_strglob(zGlob, zPlan)
4651           || 0==sqlite3_strglob(zGlobIPK, zPlan)
4652        );
4653      }
4654      rc = sqlite3_finalize(pExplain);
4655      if( rc!=SQLITE_OK ) break;
4656
4657      if( res<0 ){
4658        raw_printf(stderr, "Error: internal error");
4659        break;
4660      }else{
4661        if( bGroupByParent
4662        && (bVerbose || res==0)
4663        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
4664        ){
4665          raw_printf(out, "-- Parent table %s\n", zParent);
4666          sqlite3_free(zPrev);
4667          zPrev = sqlite3_mprintf("%s", zParent);
4668        }
4669
4670        if( res==0 ){
4671          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
4672        }else if( bVerbose ){
4673          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
4674              zIndent, zFrom, zTarget
4675          );
4676        }
4677      }
4678    }
4679    sqlite3_free(zPrev);
4680
4681    if( rc!=SQLITE_OK ){
4682      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4683    }
4684
4685    rc2 = sqlite3_finalize(pSql);
4686    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
4687      rc = rc2;
4688      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4689    }
4690  }else{
4691    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4692  }
4693
4694  return rc;
4695}
4696
4697/*
4698** Implementation of ".lint" dot command.
4699*/
4700static int lintDotCommand(
4701  ShellState *pState,             /* Current shell tool state */
4702  char **azArg,                   /* Array of arguments passed to dot command */
4703  int nArg                        /* Number of entries in azArg[] */
4704){
4705  int n;
4706  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
4707  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
4708  return lintFkeyIndexes(pState, azArg, nArg);
4709
4710 usage:
4711  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
4712  raw_printf(stderr, "Where sub-commands are:\n");
4713  raw_printf(stderr, "    fkey-indexes\n");
4714  return SQLITE_ERROR;
4715}
4716
4717#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
4718/*********************************************************************************
4719** The ".archive" or ".ar" command.
4720*/
4721static void shellPrepare(
4722  sqlite3 *db,
4723  int *pRc,
4724  const char *zSql,
4725  sqlite3_stmt **ppStmt
4726){
4727  *ppStmt = 0;
4728  if( *pRc==SQLITE_OK ){
4729    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
4730    if( rc!=SQLITE_OK ){
4731      raw_printf(stderr, "sql error: %s (%d)\n",
4732          sqlite3_errmsg(db), sqlite3_errcode(db)
4733      );
4734      *pRc = rc;
4735    }
4736  }
4737}
4738
4739static void shellPreparePrintf(
4740  sqlite3 *db,
4741  int *pRc,
4742  sqlite3_stmt **ppStmt,
4743  const char *zFmt,
4744  ...
4745){
4746  *ppStmt = 0;
4747  if( *pRc==SQLITE_OK ){
4748    va_list ap;
4749    char *z;
4750    va_start(ap, zFmt);
4751    z = sqlite3_vmprintf(zFmt, ap);
4752    if( z==0 ){
4753      *pRc = SQLITE_NOMEM;
4754    }else{
4755      shellPrepare(db, pRc, z, ppStmt);
4756      sqlite3_free(z);
4757    }
4758  }
4759}
4760
4761static void shellFinalize(
4762  int *pRc,
4763  sqlite3_stmt *pStmt
4764){
4765  if( pStmt ){
4766    sqlite3 *db = sqlite3_db_handle(pStmt);
4767    int rc = sqlite3_finalize(pStmt);
4768    if( *pRc==SQLITE_OK ){
4769      if( rc!=SQLITE_OK ){
4770        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
4771      }
4772      *pRc = rc;
4773    }
4774  }
4775}
4776
4777static void shellReset(
4778  int *pRc,
4779  sqlite3_stmt *pStmt
4780){
4781  int rc = sqlite3_reset(pStmt);
4782  if( *pRc==SQLITE_OK ){
4783    if( rc!=SQLITE_OK ){
4784      sqlite3 *db = sqlite3_db_handle(pStmt);
4785      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
4786    }
4787    *pRc = rc;
4788  }
4789}
4790/*
4791** Structure representing a single ".ar" command.
4792*/
4793typedef struct ArCommand ArCommand;
4794struct ArCommand {
4795  u8 eCmd;                        /* An AR_CMD_* value */
4796  u8 bVerbose;                    /* True if --verbose */
4797  u8 bZip;                        /* True if the archive is a ZIP */
4798  u8 bDryRun;                     /* True if --dry-run */
4799  u8 bAppend;                     /* True if --append */
4800  int nArg;                       /* Number of command arguments */
4801  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
4802  const char *zFile;              /* --file argument, or NULL */
4803  const char *zDir;               /* --directory argument, or NULL */
4804  char **azArg;                   /* Array of command arguments */
4805  ShellState *p;                  /* Shell state */
4806  sqlite3 *db;                    /* Database containing the archive */
4807};
4808
4809/*
4810** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
4811*/
4812static int arUsage(FILE *f){
4813  raw_printf(f,
4814"\n"
4815"Usage: .ar [OPTION...] [FILE...]\n"
4816"The .ar command manages sqlar archives.\n"
4817"\n"
4818"Examples:\n"
4819"  .ar -cf archive.sar foo bar    # Create archive.sar from files foo and bar\n"
4820"  .ar -tf archive.sar            # List members of archive.sar\n"
4821"  .ar -xvf archive.sar           # Verbosely extract files from archive.sar\n"
4822"\n"
4823"Each command line must feature exactly one command option:\n"
4824"  -c, --create               Create a new archive\n"
4825"  -u, --update               Update or add files to an existing archive\n"
4826"  -t, --list                 List contents of archive\n"
4827"  -x, --extract              Extract files from archive\n"
4828"\n"
4829"And zero or more optional options:\n"
4830"  -v, --verbose              Print each filename as it is processed\n"
4831"  -f FILE, --file FILE       Operate on archive FILE (default is current db)\n"
4832"  -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS\n"
4833"  -C DIR, --directory DIR    Change to directory DIR to read/extract files\n"
4834"  -n, --dryrun               Show the SQL that would have occurred\n"
4835"\n"
4836"See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
4837"\n"
4838);
4839  return SQLITE_ERROR;
4840}
4841
4842/*
4843** Print an error message for the .ar command to stderr and return
4844** SQLITE_ERROR.
4845*/
4846static int arErrorMsg(const char *zFmt, ...){
4847  va_list ap;
4848  char *z;
4849  va_start(ap, zFmt);
4850  z = sqlite3_vmprintf(zFmt, ap);
4851  va_end(ap);
4852  raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z);
4853  sqlite3_free(z);
4854  return SQLITE_ERROR;
4855}
4856
4857/*
4858** Values for ArCommand.eCmd.
4859*/
4860#define AR_CMD_CREATE       1
4861#define AR_CMD_EXTRACT      2
4862#define AR_CMD_LIST         3
4863#define AR_CMD_UPDATE       4
4864#define AR_CMD_HELP         5
4865
4866/*
4867** Other (non-command) switches.
4868*/
4869#define AR_SWITCH_VERBOSE     6
4870#define AR_SWITCH_FILE        7
4871#define AR_SWITCH_DIRECTORY   8
4872#define AR_SWITCH_APPEND      9
4873#define AR_SWITCH_DRYRUN     10
4874
4875static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
4876  switch( eSwitch ){
4877    case AR_CMD_CREATE:
4878    case AR_CMD_EXTRACT:
4879    case AR_CMD_LIST:
4880    case AR_CMD_UPDATE:
4881    case AR_CMD_HELP:
4882      if( pAr->eCmd ){
4883        return arErrorMsg("multiple command options");
4884      }
4885      pAr->eCmd = eSwitch;
4886      break;
4887
4888    case AR_SWITCH_DRYRUN:
4889      pAr->bDryRun = 1;
4890      break;
4891    case AR_SWITCH_VERBOSE:
4892      pAr->bVerbose = 1;
4893      break;
4894    case AR_SWITCH_APPEND:
4895      pAr->bAppend = 1;
4896      /* Fall thru into --file */
4897    case AR_SWITCH_FILE:
4898      pAr->zFile = zArg;
4899      break;
4900    case AR_SWITCH_DIRECTORY:
4901      pAr->zDir = zArg;
4902      break;
4903  }
4904
4905  return SQLITE_OK;
4906}
4907
4908/*
4909** Parse the command line for an ".ar" command. The results are written into
4910** structure (*pAr). SQLITE_OK is returned if the command line is parsed
4911** successfully, otherwise an error message is written to stderr and
4912** SQLITE_ERROR returned.
4913*/
4914static int arParseCommand(
4915  char **azArg,                   /* Array of arguments passed to dot command */
4916  int nArg,                       /* Number of entries in azArg[] */
4917  ArCommand *pAr                  /* Populate this object */
4918){
4919  struct ArSwitch {
4920    const char *zLong;
4921    char cShort;
4922    u8 eSwitch;
4923    u8 bArg;
4924  } aSwitch[] = {
4925    { "create",    'c', AR_CMD_CREATE,       0 },
4926    { "extract",   'x', AR_CMD_EXTRACT,      0 },
4927    { "list",      't', AR_CMD_LIST,         0 },
4928    { "update",    'u', AR_CMD_UPDATE,       0 },
4929    { "help",      'h', AR_CMD_HELP,         0 },
4930    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
4931    { "file",      'f', AR_SWITCH_FILE,      1 },
4932    { "append",    'a', AR_SWITCH_APPEND,    1 },
4933    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
4934    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
4935  };
4936  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
4937  struct ArSwitch *pEnd = &aSwitch[nSwitch];
4938
4939  if( nArg<=1 ){
4940    return arUsage(stderr);
4941  }else{
4942    char *z = azArg[1];
4943    memset(pAr, 0, sizeof(ArCommand));
4944
4945    if( z[0]!='-' ){
4946      /* Traditional style [tar] invocation */
4947      int i;
4948      int iArg = 2;
4949      for(i=0; z[i]; i++){
4950        const char *zArg = 0;
4951        struct ArSwitch *pOpt;
4952        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
4953          if( z[i]==pOpt->cShort ) break;
4954        }
4955        if( pOpt==pEnd ){
4956          return arErrorMsg("unrecognized option: %c", z[i]);
4957        }
4958        if( pOpt->bArg ){
4959          if( iArg>=nArg ){
4960            return arErrorMsg("option requires an argument: %c",z[i]);
4961          }
4962          zArg = azArg[iArg++];
4963        }
4964        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
4965      }
4966      pAr->nArg = nArg-iArg;
4967      if( pAr->nArg>0 ){
4968        pAr->azArg = &azArg[iArg];
4969      }
4970    }else{
4971      /* Non-traditional invocation */
4972      int iArg;
4973      for(iArg=1; iArg<nArg; iArg++){
4974        int n;
4975        z = azArg[iArg];
4976        if( z[0]!='-' ){
4977          /* All remaining command line words are command arguments. */
4978          pAr->azArg = &azArg[iArg];
4979          pAr->nArg = nArg-iArg;
4980          break;
4981        }
4982        n = strlen30(z);
4983
4984        if( z[1]!='-' ){
4985          int i;
4986          /* One or more short options */
4987          for(i=1; i<n; i++){
4988            const char *zArg = 0;
4989            struct ArSwitch *pOpt;
4990            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
4991              if( z[i]==pOpt->cShort ) break;
4992            }
4993            if( pOpt==pEnd ){
4994              return arErrorMsg("unrecognized option: %c\n", z[i]);
4995            }
4996            if( pOpt->bArg ){
4997              if( i<(n-1) ){
4998                zArg = &z[i+1];
4999                i = n;
5000              }else{
5001                if( iArg>=(nArg-1) ){
5002                  return arErrorMsg("option requires an argument: %c\n",z[i]);
5003                }
5004                zArg = azArg[++iArg];
5005              }
5006            }
5007            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
5008          }
5009        }else if( z[2]=='\0' ){
5010          /* A -- option, indicating that all remaining command line words
5011          ** are command arguments.  */
5012          pAr->azArg = &azArg[iArg+1];
5013          pAr->nArg = nArg-iArg-1;
5014          break;
5015        }else{
5016          /* A long option */
5017          const char *zArg = 0;             /* Argument for option, if any */
5018          struct ArSwitch *pMatch = 0;      /* Matching option */
5019          struct ArSwitch *pOpt;            /* Iterator */
5020          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5021            const char *zLong = pOpt->zLong;
5022            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
5023              if( pMatch ){
5024                return arErrorMsg("ambiguous option: %s",z);
5025              }else{
5026                pMatch = pOpt;
5027              }
5028            }
5029          }
5030
5031          if( pMatch==0 ){
5032            return arErrorMsg("unrecognized option: %s", z);
5033          }
5034          if( pMatch->bArg ){
5035            if( iArg>=(nArg-1) ){
5036              return arErrorMsg("option requires an argument: %s", z);
5037            }
5038            zArg = azArg[++iArg];
5039          }
5040          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
5041        }
5042      }
5043    }
5044  }
5045
5046  return SQLITE_OK;
5047}
5048
5049/*
5050** This function assumes that all arguments within the ArCommand.azArg[]
5051** array refer to archive members, as for the --extract or --list commands.
5052** It checks that each of them are present. If any specified file is not
5053** present in the archive, an error is printed to stderr and an error
5054** code returned. Otherwise, if all specified arguments are present in
5055** the archive, SQLITE_OK is returned.
5056**
5057** This function strips any trailing '/' characters from each argument.
5058** This is consistent with the way the [tar] command seems to work on
5059** Linux.
5060*/
5061static int arCheckEntries(ArCommand *pAr){
5062  int rc = SQLITE_OK;
5063  if( pAr->nArg ){
5064    int i, j;
5065    sqlite3_stmt *pTest = 0;
5066
5067    shellPreparePrintf(pAr->db, &rc, &pTest,
5068        "SELECT name FROM %s WHERE name=$name",
5069        pAr->zSrcTable
5070    );
5071    j = sqlite3_bind_parameter_index(pTest, "$name");
5072    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
5073      char *z = pAr->azArg[i];
5074      int n = strlen30(z);
5075      int bOk = 0;
5076      while( n>0 && z[n-1]=='/' ) n--;
5077      z[n] = '\0';
5078      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
5079      if( SQLITE_ROW==sqlite3_step(pTest) ){
5080        bOk = 1;
5081      }
5082      shellReset(&rc, pTest);
5083      if( rc==SQLITE_OK && bOk==0 ){
5084        utf8_printf(stderr, "not found in archive: %s\n", z);
5085        rc = SQLITE_ERROR;
5086      }
5087    }
5088    shellFinalize(&rc, pTest);
5089  }
5090  return rc;
5091}
5092
5093/*
5094** Format a WHERE clause that can be used against the "sqlar" table to
5095** identify all archive members that match the command arguments held
5096** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
5097** The caller is responsible for eventually calling sqlite3_free() on
5098** any non-NULL (*pzWhere) value.
5099*/
5100static void arWhereClause(
5101  int *pRc,
5102  ArCommand *pAr,
5103  char **pzWhere                  /* OUT: New WHERE clause */
5104){
5105  char *zWhere = 0;
5106  if( *pRc==SQLITE_OK ){
5107    if( pAr->nArg==0 ){
5108      zWhere = sqlite3_mprintf("1");
5109    }else{
5110      int i;
5111      const char *zSep = "";
5112      for(i=0; i<pAr->nArg; i++){
5113        const char *z = pAr->azArg[i];
5114        zWhere = sqlite3_mprintf(
5115          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
5116          zWhere, zSep, z, strlen30(z)+1, z
5117        );
5118        if( zWhere==0 ){
5119          *pRc = SQLITE_NOMEM;
5120          break;
5121        }
5122        zSep = " OR ";
5123      }
5124    }
5125  }
5126  *pzWhere = zWhere;
5127}
5128
5129/*
5130** Implementation of .ar "lisT" command.
5131*/
5132static int arListCommand(ArCommand *pAr){
5133  const char *zSql = "SELECT %s FROM %s WHERE %s";
5134  const char *azCols[] = {
5135    "name",
5136    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
5137  };
5138
5139  char *zWhere = 0;
5140  sqlite3_stmt *pSql = 0;
5141  int rc;
5142
5143  rc = arCheckEntries(pAr);
5144  arWhereClause(&rc, pAr, &zWhere);
5145
5146  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
5147                     pAr->zSrcTable, zWhere);
5148  if( pAr->bDryRun ){
5149    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5150  }else{
5151    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5152      if( pAr->bVerbose ){
5153        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
5154            sqlite3_column_text(pSql, 0),
5155            sqlite3_column_int(pSql, 1),
5156            sqlite3_column_text(pSql, 2),
5157            sqlite3_column_text(pSql, 3)
5158        );
5159      }else{
5160        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5161      }
5162    }
5163  }
5164  shellFinalize(&rc, pSql);
5165  return rc;
5166}
5167
5168
5169/*
5170** Implementation of .ar "eXtract" command.
5171*/
5172static int arExtractCommand(ArCommand *pAr){
5173  const char *zSql1 =
5174    "SELECT "
5175    " ($dir || name),"
5176    " writefile(($dir || name), %s, mode, mtime) "
5177    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
5178
5179  const char *azExtraArg[] = {
5180    "sqlar_uncompress(data, sz)",
5181    "data"
5182  };
5183
5184  sqlite3_stmt *pSql = 0;
5185  int rc = SQLITE_OK;
5186  char *zDir = 0;
5187  char *zWhere = 0;
5188  int i, j;
5189
5190  /* If arguments are specified, check that they actually exist within
5191  ** the archive before proceeding. And formulate a WHERE clause to
5192  ** match them.  */
5193  rc = arCheckEntries(pAr);
5194  arWhereClause(&rc, pAr, &zWhere);
5195
5196  if( rc==SQLITE_OK ){
5197    if( pAr->zDir ){
5198      zDir = sqlite3_mprintf("%s/", pAr->zDir);
5199    }else{
5200      zDir = sqlite3_mprintf("");
5201    }
5202    if( zDir==0 ) rc = SQLITE_NOMEM;
5203  }
5204
5205  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
5206      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
5207  );
5208
5209  if( rc==SQLITE_OK ){
5210    j = sqlite3_bind_parameter_index(pSql, "$dir");
5211    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
5212
5213    /* Run the SELECT statement twice. The first time, writefile() is called
5214    ** for all archive members that should be extracted. The second time,
5215    ** only for the directories. This is because the timestamps for
5216    ** extracted directories must be reset after they are populated (as
5217    ** populating them changes the timestamp).  */
5218    for(i=0; i<2; i++){
5219      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
5220      sqlite3_bind_int(pSql, j, i);
5221      if( pAr->bDryRun ){
5222        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5223      }else{
5224        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5225          if( i==0 && pAr->bVerbose ){
5226            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5227          }
5228        }
5229      }
5230      shellReset(&rc, pSql);
5231    }
5232    shellFinalize(&rc, pSql);
5233  }
5234
5235  sqlite3_free(zDir);
5236  sqlite3_free(zWhere);
5237  return rc;
5238}
5239
5240/*
5241** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
5242*/
5243static int arExecSql(ArCommand *pAr, const char *zSql){
5244  int rc;
5245  if( pAr->bDryRun ){
5246    utf8_printf(pAr->p->out, "%s\n", zSql);
5247    rc = SQLITE_OK;
5248  }else{
5249    char *zErr = 0;
5250    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
5251    if( zErr ){
5252      utf8_printf(stdout, "ERROR: %s\n", zErr);
5253      sqlite3_free(zErr);
5254    }
5255  }
5256  return rc;
5257}
5258
5259
5260/*
5261** Implementation of .ar "create" and "update" commands.
5262**
5263** Create the "sqlar" table in the database if it does not already exist.
5264** Then add each file in the azFile[] array to the archive. Directories
5265** are added recursively. If argument bVerbose is non-zero, a message is
5266** printed on stdout for each file archived.
5267**
5268** The create command is the same as update, except that it drops
5269** any existing "sqlar" table before beginning.
5270*/
5271static int arCreateOrUpdateCommand(
5272  ArCommand *pAr,                 /* Command arguments and options */
5273  int bUpdate                     /* true for a --create.  false for --update */
5274){
5275  const char *zCreate =
5276      "CREATE TABLE IF NOT EXISTS sqlar(\n"
5277      "  name TEXT PRIMARY KEY,  -- name of the file\n"
5278      "  mode INT,               -- access permissions\n"
5279      "  mtime INT,              -- last modification time\n"
5280      "  sz INT,                 -- original file size\n"
5281      "  data BLOB               -- compressed content\n"
5282      ")";
5283  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
5284  const char *zInsertFmt[2] = {
5285     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
5286     "  SELECT\n"
5287     "    %s,\n"
5288     "    mode,\n"
5289     "    mtime,\n"
5290     "    CASE substr(lsmode(mode),1,1)\n"
5291     "      WHEN '-' THEN length(data)\n"
5292     "      WHEN 'd' THEN 0\n"
5293     "      ELSE -1 END,\n"
5294     "    sqlar_compress(data)\n"
5295     "  FROM fsdir(%Q,%Q)\n"
5296     "  WHERE lsmode(mode) NOT LIKE '?%%';",
5297     "REPLACE INTO %s(name,mode,mtime,data)\n"
5298     "  SELECT\n"
5299     "    %s,\n"
5300     "    mode,\n"
5301     "    mtime,\n"
5302     "    data\n"
5303     "  FROM fsdir(%Q,%Q)\n"
5304     "  WHERE lsmode(mode) NOT LIKE '?%%';"
5305  };
5306  int i;                          /* For iterating through azFile[] */
5307  int rc;                         /* Return code */
5308  const char *zTab = 0;           /* SQL table into which to insert */
5309  char *zSql;
5310  char zTemp[50];
5311
5312  arExecSql(pAr, "PRAGMA page_size=512");
5313  rc = arExecSql(pAr, "SAVEPOINT ar;");
5314  if( rc!=SQLITE_OK ) return rc;
5315  zTemp[0] = 0;
5316  if( pAr->bZip ){
5317    /* Initialize the zipfile virtual table, if necessary */
5318    if( pAr->zFile ){
5319      sqlite3_uint64 r;
5320      sqlite3_randomness(sizeof(r),&r);
5321      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
5322      zTab = zTemp;
5323      zSql = sqlite3_mprintf(
5324         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
5325         zTab, pAr->zFile
5326      );
5327      rc = arExecSql(pAr, zSql);
5328      sqlite3_free(zSql);
5329    }else{
5330      zTab = "zip";
5331    }
5332  }else{
5333    /* Initialize the table for an SQLAR */
5334    zTab = "sqlar";
5335    if( bUpdate==0 ){
5336      rc = arExecSql(pAr, zDrop);
5337      if( rc!=SQLITE_OK ) goto end_ar_transaction;
5338    }
5339    rc = arExecSql(pAr, zCreate);
5340  }
5341  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
5342    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
5343        pAr->bVerbose ? "shell_putsnl(name)" : "name",
5344        pAr->azArg[i], pAr->zDir);
5345    rc = arExecSql(pAr, zSql2);
5346    sqlite3_free(zSql2);
5347  }
5348end_ar_transaction:
5349  if( rc!=SQLITE_OK ){
5350    arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
5351  }else{
5352    rc = arExecSql(pAr, "RELEASE ar;");
5353    if( pAr->bZip && pAr->zFile ){
5354      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
5355      arExecSql(pAr, zSql);
5356      sqlite3_free(zSql);
5357    }
5358  }
5359  return rc;
5360}
5361
5362/*
5363** Implementation of ".ar" dot command.
5364*/
5365static int arDotCommand(
5366  ShellState *pState,             /* Current shell tool state */
5367  char **azArg,                   /* Array of arguments passed to dot command */
5368  int nArg                        /* Number of entries in azArg[] */
5369){
5370  ArCommand cmd;
5371  int rc;
5372  memset(&cmd, 0, sizeof(cmd));
5373  rc = arParseCommand(azArg, nArg, &cmd);
5374  if( rc==SQLITE_OK ){
5375    int eDbType = SHELL_OPEN_UNSPEC;
5376    cmd.p = pState;
5377    cmd.db = pState->db;
5378    if( cmd.zFile ){
5379      eDbType = deduceDatabaseType(cmd.zFile, 1);
5380    }else{
5381      eDbType = pState->openMode;
5382    }
5383    if( eDbType==SHELL_OPEN_ZIPFILE ){
5384      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
5385        if( cmd.zFile==0 ){
5386          cmd.zSrcTable = sqlite3_mprintf("zip");
5387        }else{
5388          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
5389        }
5390      }
5391      cmd.bZip = 1;
5392    }else if( cmd.zFile ){
5393      int flags;
5394      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
5395      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
5396        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
5397      }else{
5398        flags = SQLITE_OPEN_READONLY;
5399      }
5400      cmd.db = 0;
5401      if( cmd.bDryRun ){
5402        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
5403             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
5404      }
5405      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
5406             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
5407      if( rc!=SQLITE_OK ){
5408        utf8_printf(stderr, "cannot open file: %s (%s)\n",
5409            cmd.zFile, sqlite3_errmsg(cmd.db)
5410        );
5411        goto end_ar_command;
5412      }
5413      sqlite3_fileio_init(cmd.db, 0, 0);
5414      sqlite3_sqlar_init(cmd.db, 0, 0);
5415      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
5416                              shellPutsFunc, 0, 0);
5417
5418    }
5419    if( cmd.zSrcTable==0 && cmd.bZip==0 ){
5420      if( cmd.eCmd!=AR_CMD_CREATE
5421       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
5422      ){
5423        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
5424        rc = SQLITE_ERROR;
5425        goto end_ar_command;
5426      }
5427      cmd.zSrcTable = sqlite3_mprintf("sqlar");
5428    }
5429
5430    switch( cmd.eCmd ){
5431      case AR_CMD_CREATE:
5432        rc = arCreateOrUpdateCommand(&cmd, 0);
5433        break;
5434
5435      case AR_CMD_EXTRACT:
5436        rc = arExtractCommand(&cmd);
5437        break;
5438
5439      case AR_CMD_LIST:
5440        rc = arListCommand(&cmd);
5441        break;
5442
5443      case AR_CMD_HELP:
5444        arUsage(pState->out);
5445        break;
5446
5447      default:
5448        assert( cmd.eCmd==AR_CMD_UPDATE );
5449        rc = arCreateOrUpdateCommand(&cmd, 1);
5450        break;
5451    }
5452  }
5453end_ar_command:
5454  if( cmd.db!=pState->db ){
5455    sqlite3_close(cmd.db);
5456  }
5457  sqlite3_free(cmd.zSrcTable);
5458
5459  return rc;
5460}
5461/* End of the ".archive" or ".ar" command logic
5462**********************************************************************************/
5463#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
5464
5465
5466/*
5467** If an input line begins with "." then invoke this routine to
5468** process that line.
5469**
5470** Return 1 on error, 2 to exit, and 0 otherwise.
5471*/
5472static int do_meta_command(char *zLine, ShellState *p){
5473  int h = 1;
5474  int nArg = 0;
5475  int n, c;
5476  int rc = 0;
5477  char *azArg[50];
5478
5479#ifndef SQLITE_OMIT_VIRTUALTABLE
5480  if( p->expert.pExpert ){
5481    expertFinish(p, 1, 0);
5482  }
5483#endif
5484
5485  /* Parse the input line into tokens.
5486  */
5487  while( zLine[h] && nArg<ArraySize(azArg) ){
5488    while( IsSpace(zLine[h]) ){ h++; }
5489    if( zLine[h]==0 ) break;
5490    if( zLine[h]=='\'' || zLine[h]=='"' ){
5491      int delim = zLine[h++];
5492      azArg[nArg++] = &zLine[h];
5493      while( zLine[h] && zLine[h]!=delim ){
5494        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
5495        h++;
5496      }
5497      if( zLine[h]==delim ){
5498        zLine[h++] = 0;
5499      }
5500      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
5501    }else{
5502      azArg[nArg++] = &zLine[h];
5503      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
5504      if( zLine[h] ) zLine[h++] = 0;
5505      resolve_backslashes(azArg[nArg-1]);
5506    }
5507  }
5508
5509  /* Process the input line.
5510  */
5511  if( nArg==0 ) return 0; /* no tokens, no error */
5512  n = strlen30(azArg[0]);
5513  c = azArg[0][0];
5514  clearTempFile(p);
5515
5516#ifndef SQLITE_OMIT_AUTHORIZATION
5517  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
5518    if( nArg!=2 ){
5519      raw_printf(stderr, "Usage: .auth ON|OFF\n");
5520      rc = 1;
5521      goto meta_command_exit;
5522    }
5523    open_db(p, 0);
5524    if( booleanValue(azArg[1]) ){
5525      sqlite3_set_authorizer(p->db, shellAuth, p);
5526    }else{
5527      sqlite3_set_authorizer(p->db, 0, 0);
5528    }
5529  }else
5530#endif
5531
5532#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5533  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
5534    open_db(p, 0);
5535    rc = arDotCommand(p, azArg, nArg);
5536  }else
5537#endif
5538
5539  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
5540   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
5541  ){
5542    const char *zDestFile = 0;
5543    const char *zDb = 0;
5544    sqlite3 *pDest;
5545    sqlite3_backup *pBackup;
5546    int j;
5547    for(j=1; j<nArg; j++){
5548      const char *z = azArg[j];
5549      if( z[0]=='-' ){
5550        while( z[0]=='-' ) z++;
5551        /* No options to process at this time */
5552        {
5553          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
5554          return 1;
5555        }
5556      }else if( zDestFile==0 ){
5557        zDestFile = azArg[j];
5558      }else if( zDb==0 ){
5559        zDb = zDestFile;
5560        zDestFile = azArg[j];
5561      }else{
5562        raw_printf(stderr, "too many arguments to .backup\n");
5563        return 1;
5564      }
5565    }
5566    if( zDestFile==0 ){
5567      raw_printf(stderr, "missing FILENAME argument on .backup\n");
5568      return 1;
5569    }
5570    if( zDb==0 ) zDb = "main";
5571    rc = sqlite3_open(zDestFile, &pDest);
5572    if( rc!=SQLITE_OK ){
5573      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
5574      sqlite3_close(pDest);
5575      return 1;
5576    }
5577    open_db(p, 0);
5578    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
5579    if( pBackup==0 ){
5580      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
5581      sqlite3_close(pDest);
5582      return 1;
5583    }
5584    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
5585    sqlite3_backup_finish(pBackup);
5586    if( rc==SQLITE_DONE ){
5587      rc = 0;
5588    }else{
5589      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
5590      rc = 1;
5591    }
5592    sqlite3_close(pDest);
5593  }else
5594
5595  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
5596    if( nArg==2 ){
5597      bail_on_error = booleanValue(azArg[1]);
5598    }else{
5599      raw_printf(stderr, "Usage: .bail on|off\n");
5600      rc = 1;
5601    }
5602  }else
5603
5604  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
5605    if( nArg==2 ){
5606      if( booleanValue(azArg[1]) ){
5607        setBinaryMode(p->out, 1);
5608      }else{
5609        setTextMode(p->out, 1);
5610      }
5611    }else{
5612      raw_printf(stderr, "Usage: .binary on|off\n");
5613      rc = 1;
5614    }
5615  }else
5616
5617  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
5618    if( nArg==2 ){
5619#if defined(_WIN32) || defined(WIN32)
5620      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
5621      rc = !SetCurrentDirectoryW(z);
5622      sqlite3_free(z);
5623#else
5624      rc = chdir(azArg[1]);
5625#endif
5626      if( rc ){
5627        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
5628        rc = 1;
5629      }
5630    }else{
5631      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
5632      rc = 1;
5633    }
5634  }else
5635
5636  /* The undocumented ".breakpoint" command causes a call to the no-op
5637  ** routine named test_breakpoint().
5638  */
5639  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
5640    test_breakpoint();
5641  }else
5642
5643  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
5644    if( nArg==2 ){
5645      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
5646    }else{
5647      raw_printf(stderr, "Usage: .changes on|off\n");
5648      rc = 1;
5649    }
5650  }else
5651
5652  /* Cancel output redirection, if it is currently set (by .testcase)
5653  ** Then read the content of the testcase-out.txt file and compare against
5654  ** azArg[1].  If there are differences, report an error and exit.
5655  */
5656  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
5657    char *zRes = 0;
5658    output_reset(p);
5659    if( nArg!=2 ){
5660      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
5661      rc = 2;
5662    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
5663      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
5664      rc = 2;
5665    }else if( testcase_glob(azArg[1],zRes)==0 ){
5666      utf8_printf(stderr,
5667                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
5668                 p->zTestcase, azArg[1], zRes);
5669      rc = 1;
5670    }else{
5671      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
5672      p->nCheck++;
5673    }
5674    sqlite3_free(zRes);
5675  }else
5676
5677  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
5678    if( nArg==2 ){
5679      tryToClone(p, azArg[1]);
5680    }else{
5681      raw_printf(stderr, "Usage: .clone FILENAME\n");
5682      rc = 1;
5683    }
5684  }else
5685
5686  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
5687    ShellState data;
5688    char *zErrMsg = 0;
5689    open_db(p, 0);
5690    memcpy(&data, p, sizeof(data));
5691    data.showHeader = 0;
5692    data.cMode = data.mode = MODE_List;
5693    sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
5694    data.cnt = 0;
5695    sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
5696                 callback, &data, &zErrMsg);
5697    if( zErrMsg ){
5698      utf8_printf(stderr,"Error: %s\n", zErrMsg);
5699      sqlite3_free(zErrMsg);
5700      rc = 1;
5701    }
5702  }else
5703
5704  if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
5705    rc = shell_dbinfo_command(p, nArg, azArg);
5706  }else
5707
5708  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
5709    const char *zLike = 0;
5710    int i;
5711    int savedShowHeader = p->showHeader;
5712    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
5713    for(i=1; i<nArg; i++){
5714      if( azArg[i][0]=='-' ){
5715        const char *z = azArg[i]+1;
5716        if( z[0]=='-' ) z++;
5717        if( strcmp(z,"preserve-rowids")==0 ){
5718#ifdef SQLITE_OMIT_VIRTUALTABLE
5719          raw_printf(stderr, "The --preserve-rowids option is not compatible"
5720                             " with SQLITE_OMIT_VIRTUALTABLE\n");
5721          rc = 1;
5722          goto meta_command_exit;
5723#else
5724          ShellSetFlag(p, SHFLG_PreserveRowid);
5725#endif
5726        }else
5727        if( strcmp(z,"newlines")==0 ){
5728          ShellSetFlag(p, SHFLG_Newlines);
5729        }else
5730        {
5731          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
5732          rc = 1;
5733          goto meta_command_exit;
5734        }
5735      }else if( zLike ){
5736        raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
5737                           "?--newlines? ?LIKE-PATTERN?\n");
5738        rc = 1;
5739        goto meta_command_exit;
5740      }else{
5741        zLike = azArg[i];
5742      }
5743    }
5744    open_db(p, 0);
5745    /* When playing back a "dump", the content might appear in an order
5746    ** which causes immediate foreign key constraints to be violated.
5747    ** So disable foreign-key constraint enforcement to prevent problems. */
5748    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
5749    raw_printf(p->out, "BEGIN TRANSACTION;\n");
5750    p->writableSchema = 0;
5751    p->showHeader = 0;
5752    /* Set writable_schema=ON since doing so forces SQLite to initialize
5753    ** as much of the schema as it can even if the sqlite_master table is
5754    ** corrupt. */
5755    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
5756    p->nErr = 0;
5757    if( zLike==0 ){
5758      run_schema_dump_query(p,
5759        "SELECT name, type, sql FROM sqlite_master "
5760        "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
5761      );
5762      run_schema_dump_query(p,
5763        "SELECT name, type, sql FROM sqlite_master "
5764        "WHERE name=='sqlite_sequence'"
5765      );
5766      run_table_dump_query(p,
5767        "SELECT sql FROM sqlite_master "
5768        "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
5769      );
5770    }else{
5771      char *zSql;
5772      zSql = sqlite3_mprintf(
5773        "SELECT name, type, sql FROM sqlite_master "
5774        "WHERE tbl_name LIKE %Q AND type=='table'"
5775        "  AND sql NOT NULL", zLike);
5776      run_schema_dump_query(p,zSql);
5777      sqlite3_free(zSql);
5778      zSql = sqlite3_mprintf(
5779        "SELECT sql FROM sqlite_master "
5780        "WHERE sql NOT NULL"
5781        "  AND type IN ('index','trigger','view')"
5782        "  AND tbl_name LIKE %Q", zLike);
5783      run_table_dump_query(p, zSql, 0);
5784      sqlite3_free(zSql);
5785    }
5786    if( p->writableSchema ){
5787      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
5788      p->writableSchema = 0;
5789    }
5790    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5791    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
5792    raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
5793    p->showHeader = savedShowHeader;
5794  }else
5795
5796  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
5797    if( nArg==2 ){
5798      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
5799    }else{
5800      raw_printf(stderr, "Usage: .echo on|off\n");
5801      rc = 1;
5802    }
5803  }else
5804
5805  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
5806    if( nArg==2 ){
5807      if( strcmp(azArg[1],"full")==0 ){
5808        p->autoEQP = AUTOEQP_full;
5809      }else if( strcmp(azArg[1],"trigger")==0 ){
5810        p->autoEQP = AUTOEQP_trigger;
5811      }else{
5812        p->autoEQP = (u8)booleanValue(azArg[1]);
5813      }
5814    }else{
5815      raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
5816      rc = 1;
5817    }
5818  }else
5819
5820  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
5821    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
5822    rc = 2;
5823  }else
5824
5825  /* The ".explain" command is automatic now.  It is largely pointless.  It
5826  ** retained purely for backwards compatibility */
5827  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
5828    int val = 1;
5829    if( nArg>=2 ){
5830      if( strcmp(azArg[1],"auto")==0 ){
5831        val = 99;
5832      }else{
5833        val =  booleanValue(azArg[1]);
5834      }
5835    }
5836    if( val==1 && p->mode!=MODE_Explain ){
5837      p->normalMode = p->mode;
5838      p->mode = MODE_Explain;
5839      p->autoExplain = 0;
5840    }else if( val==0 ){
5841      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
5842      p->autoExplain = 0;
5843    }else if( val==99 ){
5844      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
5845      p->autoExplain = 1;
5846    }
5847  }else
5848
5849#ifndef SQLITE_OMIT_VIRTUALTABLE
5850  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
5851    open_db(p, 0);
5852    expertDotCommand(p, azArg, nArg);
5853  }else
5854#endif
5855
5856  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
5857    ShellState data;
5858    char *zErrMsg = 0;
5859    int doStats = 0;
5860    memcpy(&data, p, sizeof(data));
5861    data.showHeader = 0;
5862    data.cMode = data.mode = MODE_Semi;
5863    if( nArg==2 && optionMatch(azArg[1], "indent") ){
5864      data.cMode = data.mode = MODE_Pretty;
5865      nArg = 1;
5866    }
5867    if( nArg!=1 ){
5868      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
5869      rc = 1;
5870      goto meta_command_exit;
5871    }
5872    open_db(p, 0);
5873    rc = sqlite3_exec(p->db,
5874       "SELECT sql FROM"
5875       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
5876       "     FROM sqlite_master UNION ALL"
5877       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
5878       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
5879       "ORDER BY rowid",
5880       callback, &data, &zErrMsg
5881    );
5882    if( rc==SQLITE_OK ){
5883      sqlite3_stmt *pStmt;
5884      rc = sqlite3_prepare_v2(p->db,
5885               "SELECT rowid FROM sqlite_master"
5886               " WHERE name GLOB 'sqlite_stat[134]'",
5887               -1, &pStmt, 0);
5888      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
5889      sqlite3_finalize(pStmt);
5890    }
5891    if( doStats==0 ){
5892      raw_printf(p->out, "/* No STAT tables available */\n");
5893    }else{
5894      raw_printf(p->out, "ANALYZE sqlite_master;\n");
5895      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
5896                   callback, &data, &zErrMsg);
5897      data.cMode = data.mode = MODE_Insert;
5898      data.zDestTable = "sqlite_stat1";
5899      shell_exec(p, "SELECT * FROM sqlite_stat1", &zErrMsg);
5900      data.zDestTable = "sqlite_stat3";
5901      shell_exec(p, "SELECT * FROM sqlite_stat3", &zErrMsg);
5902      data.zDestTable = "sqlite_stat4";
5903      shell_exec(p, "SELECT * FROM sqlite_stat4", &zErrMsg);
5904      raw_printf(p->out, "ANALYZE sqlite_master;\n");
5905    }
5906  }else
5907
5908  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
5909    if( nArg==2 ){
5910      p->showHeader = booleanValue(azArg[1]);
5911    }else{
5912      raw_printf(stderr, "Usage: .headers on|off\n");
5913      rc = 1;
5914    }
5915  }else
5916
5917  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
5918    utf8_printf(p->out, "%s", zHelp);
5919  }else
5920
5921  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
5922    char *zTable;               /* Insert data into this table */
5923    char *zFile;                /* Name of file to extra content from */
5924    sqlite3_stmt *pStmt = NULL; /* A statement */
5925    int nCol;                   /* Number of columns in the table */
5926    int nByte;                  /* Number of bytes in an SQL string */
5927    int i, j;                   /* Loop counters */
5928    int needCommit;             /* True to COMMIT or ROLLBACK at end */
5929    int nSep;                   /* Number of bytes in p->colSeparator[] */
5930    char *zSql;                 /* An SQL statement */
5931    ImportCtx sCtx;             /* Reader context */
5932    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
5933    int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
5934
5935    if( nArg!=3 ){
5936      raw_printf(stderr, "Usage: .import FILE TABLE\n");
5937      goto meta_command_exit;
5938    }
5939    zFile = azArg[1];
5940    zTable = azArg[2];
5941    seenInterrupt = 0;
5942    memset(&sCtx, 0, sizeof(sCtx));
5943    open_db(p, 0);
5944    nSep = strlen30(p->colSeparator);
5945    if( nSep==0 ){
5946      raw_printf(stderr,
5947                 "Error: non-null column separator required for import\n");
5948      return 1;
5949    }
5950    if( nSep>1 ){
5951      raw_printf(stderr, "Error: multi-character column separators not allowed"
5952                      " for import\n");
5953      return 1;
5954    }
5955    nSep = strlen30(p->rowSeparator);
5956    if( nSep==0 ){
5957      raw_printf(stderr, "Error: non-null row separator required for import\n");
5958      return 1;
5959    }
5960    if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
5961      /* When importing CSV (only), if the row separator is set to the
5962      ** default output row separator, change it to the default input
5963      ** row separator.  This avoids having to maintain different input
5964      ** and output row separators. */
5965      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
5966      nSep = strlen30(p->rowSeparator);
5967    }
5968    if( nSep>1 ){
5969      raw_printf(stderr, "Error: multi-character row separators not allowed"
5970                      " for import\n");
5971      return 1;
5972    }
5973    sCtx.zFile = zFile;
5974    sCtx.nLine = 1;
5975    if( sCtx.zFile[0]=='|' ){
5976#ifdef SQLITE_OMIT_POPEN
5977      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
5978      return 1;
5979#else
5980      sCtx.in = popen(sCtx.zFile+1, "r");
5981      sCtx.zFile = "<pipe>";
5982      xCloser = pclose;
5983#endif
5984    }else{
5985      sCtx.in = fopen(sCtx.zFile, "rb");
5986      xCloser = fclose;
5987    }
5988    if( p->mode==MODE_Ascii ){
5989      xRead = ascii_read_one_field;
5990    }else{
5991      xRead = csv_read_one_field;
5992    }
5993    if( sCtx.in==0 ){
5994      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5995      return 1;
5996    }
5997    sCtx.cColSep = p->colSeparator[0];
5998    sCtx.cRowSep = p->rowSeparator[0];
5999    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
6000    if( zSql==0 ){
6001      raw_printf(stderr, "Error: out of memory\n");
6002      xCloser(sCtx.in);
6003      return 1;
6004    }
6005    nByte = strlen30(zSql);
6006    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6007    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
6008    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
6009      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
6010      char cSep = '(';
6011      while( xRead(&sCtx) ){
6012        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
6013        cSep = ',';
6014        if( sCtx.cTerm!=sCtx.cColSep ) break;
6015      }
6016      if( cSep=='(' ){
6017        sqlite3_free(zCreate);
6018        sqlite3_free(sCtx.z);
6019        xCloser(sCtx.in);
6020        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
6021        return 1;
6022      }
6023      zCreate = sqlite3_mprintf("%z\n)", zCreate);
6024      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
6025      sqlite3_free(zCreate);
6026      if( rc ){
6027        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
6028                sqlite3_errmsg(p->db));
6029        sqlite3_free(sCtx.z);
6030        xCloser(sCtx.in);
6031        return 1;
6032      }
6033      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6034    }
6035    sqlite3_free(zSql);
6036    if( rc ){
6037      if (pStmt) sqlite3_finalize(pStmt);
6038      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
6039      xCloser(sCtx.in);
6040      return 1;
6041    }
6042    nCol = sqlite3_column_count(pStmt);
6043    sqlite3_finalize(pStmt);
6044    pStmt = 0;
6045    if( nCol==0 ) return 0; /* no columns, no error */
6046    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
6047    if( zSql==0 ){
6048      raw_printf(stderr, "Error: out of memory\n");
6049      xCloser(sCtx.in);
6050      return 1;
6051    }
6052    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
6053    j = strlen30(zSql);
6054    for(i=1; i<nCol; i++){
6055      zSql[j++] = ',';
6056      zSql[j++] = '?';
6057    }
6058    zSql[j++] = ')';
6059    zSql[j] = 0;
6060    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6061    sqlite3_free(zSql);
6062    if( rc ){
6063      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6064      if (pStmt) sqlite3_finalize(pStmt);
6065      xCloser(sCtx.in);
6066      return 1;
6067    }
6068    needCommit = sqlite3_get_autocommit(p->db);
6069    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
6070    do{
6071      int startLine = sCtx.nLine;
6072      for(i=0; i<nCol; i++){
6073        char *z = xRead(&sCtx);
6074        /*
6075        ** Did we reach end-of-file before finding any columns?
6076        ** If so, stop instead of NULL filling the remaining columns.
6077        */
6078        if( z==0 && i==0 ) break;
6079        /*
6080        ** Did we reach end-of-file OR end-of-line before finding any
6081        ** columns in ASCII mode?  If so, stop instead of NULL filling
6082        ** the remaining columns.
6083        */
6084        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
6085        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
6086        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
6087          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
6088                          "filling the rest with NULL\n",
6089                          sCtx.zFile, startLine, nCol, i+1);
6090          i += 2;
6091          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
6092        }
6093      }
6094      if( sCtx.cTerm==sCtx.cColSep ){
6095        do{
6096          xRead(&sCtx);
6097          i++;
6098        }while( sCtx.cTerm==sCtx.cColSep );
6099        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
6100                        "extras ignored\n",
6101                        sCtx.zFile, startLine, nCol, i);
6102      }
6103      if( i>=nCol ){
6104        sqlite3_step(pStmt);
6105        rc = sqlite3_reset(pStmt);
6106        if( rc!=SQLITE_OK ){
6107          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
6108                      startLine, sqlite3_errmsg(p->db));
6109        }
6110      }
6111    }while( sCtx.cTerm!=EOF );
6112
6113    xCloser(sCtx.in);
6114    sqlite3_free(sCtx.z);
6115    sqlite3_finalize(pStmt);
6116    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
6117  }else
6118
6119#ifndef SQLITE_UNTESTABLE
6120  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
6121    char *zSql;
6122    char *zCollist = 0;
6123    sqlite3_stmt *pStmt;
6124    int tnum = 0;
6125    int i;
6126    if( nArg!=3 ){
6127      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
6128      rc = 1;
6129      goto meta_command_exit;
6130    }
6131    open_db(p, 0);
6132    zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
6133                           " WHERE name='%q' AND type='index'", azArg[1]);
6134    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6135    sqlite3_free(zSql);
6136    if( sqlite3_step(pStmt)==SQLITE_ROW ){
6137      tnum = sqlite3_column_int(pStmt, 0);
6138    }
6139    sqlite3_finalize(pStmt);
6140    if( tnum==0 ){
6141      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
6142      rc = 1;
6143      goto meta_command_exit;
6144    }
6145    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
6146    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6147    sqlite3_free(zSql);
6148    i = 0;
6149    while( sqlite3_step(pStmt)==SQLITE_ROW ){
6150      char zLabel[20];
6151      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
6152      i++;
6153      if( zCol==0 ){
6154        if( sqlite3_column_int(pStmt,1)==-1 ){
6155          zCol = "_ROWID_";
6156        }else{
6157          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
6158          zCol = zLabel;
6159        }
6160      }
6161      if( zCollist==0 ){
6162        zCollist = sqlite3_mprintf("\"%w\"", zCol);
6163      }else{
6164        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
6165      }
6166    }
6167    sqlite3_finalize(pStmt);
6168    zSql = sqlite3_mprintf(
6169          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
6170          azArg[2], zCollist, zCollist);
6171    sqlite3_free(zCollist);
6172    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
6173    if( rc==SQLITE_OK ){
6174      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
6175      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
6176      if( rc ){
6177        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
6178      }else{
6179        utf8_printf(stdout, "%s;\n", zSql);
6180        raw_printf(stdout,
6181           "WARNING: writing to an imposter table will corrupt the index!\n"
6182        );
6183      }
6184    }else{
6185      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
6186      rc = 1;
6187    }
6188    sqlite3_free(zSql);
6189  }else
6190#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
6191
6192#ifdef SQLITE_ENABLE_IOTRACE
6193  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
6194    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
6195    if( iotrace && iotrace!=stdout ) fclose(iotrace);
6196    iotrace = 0;
6197    if( nArg<2 ){
6198      sqlite3IoTrace = 0;
6199    }else if( strcmp(azArg[1], "-")==0 ){
6200      sqlite3IoTrace = iotracePrintf;
6201      iotrace = stdout;
6202    }else{
6203      iotrace = fopen(azArg[1], "w");
6204      if( iotrace==0 ){
6205        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
6206        sqlite3IoTrace = 0;
6207        rc = 1;
6208      }else{
6209        sqlite3IoTrace = iotracePrintf;
6210      }
6211    }
6212  }else
6213#endif
6214
6215  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
6216    static const struct {
6217       const char *zLimitName;   /* Name of a limit */
6218       int limitCode;            /* Integer code for that limit */
6219    } aLimit[] = {
6220      { "length",                SQLITE_LIMIT_LENGTH                    },
6221      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
6222      { "column",                SQLITE_LIMIT_COLUMN                    },
6223      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
6224      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
6225      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
6226      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
6227      { "attached",              SQLITE_LIMIT_ATTACHED                  },
6228      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
6229      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
6230      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
6231      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
6232    };
6233    int i, n2;
6234    open_db(p, 0);
6235    if( nArg==1 ){
6236      for(i=0; i<ArraySize(aLimit); i++){
6237        printf("%20s %d\n", aLimit[i].zLimitName,
6238               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
6239      }
6240    }else if( nArg>3 ){
6241      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
6242      rc = 1;
6243      goto meta_command_exit;
6244    }else{
6245      int iLimit = -1;
6246      n2 = strlen30(azArg[1]);
6247      for(i=0; i<ArraySize(aLimit); i++){
6248        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
6249          if( iLimit<0 ){
6250            iLimit = i;
6251          }else{
6252            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
6253            rc = 1;
6254            goto meta_command_exit;
6255          }
6256        }
6257      }
6258      if( iLimit<0 ){
6259        utf8_printf(stderr, "unknown limit: \"%s\"\n"
6260                        "enter \".limits\" with no arguments for a list.\n",
6261                         azArg[1]);
6262        rc = 1;
6263        goto meta_command_exit;
6264      }
6265      if( nArg==3 ){
6266        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
6267                      (int)integerValue(azArg[2]));
6268      }
6269      printf("%20s %d\n", aLimit[iLimit].zLimitName,
6270             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
6271    }
6272  }else
6273
6274  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
6275    open_db(p, 0);
6276    lintDotCommand(p, azArg, nArg);
6277  }else
6278
6279#ifndef SQLITE_OMIT_LOAD_EXTENSION
6280  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
6281    const char *zFile, *zProc;
6282    char *zErrMsg = 0;
6283    if( nArg<2 ){
6284      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
6285      rc = 1;
6286      goto meta_command_exit;
6287    }
6288    zFile = azArg[1];
6289    zProc = nArg>=3 ? azArg[2] : 0;
6290    open_db(p, 0);
6291    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
6292    if( rc!=SQLITE_OK ){
6293      utf8_printf(stderr, "Error: %s\n", zErrMsg);
6294      sqlite3_free(zErrMsg);
6295      rc = 1;
6296    }
6297  }else
6298#endif
6299
6300  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
6301    if( nArg!=2 ){
6302      raw_printf(stderr, "Usage: .log FILENAME\n");
6303      rc = 1;
6304    }else{
6305      const char *zFile = azArg[1];
6306      output_file_close(p->pLog);
6307      p->pLog = output_file_open(zFile, 0);
6308    }
6309  }else
6310
6311  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
6312    const char *zMode = nArg>=2 ? azArg[1] : "";
6313    int n2 = strlen30(zMode);
6314    int c2 = zMode[0];
6315    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
6316      p->mode = MODE_Line;
6317      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6318    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
6319      p->mode = MODE_Column;
6320      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6321    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
6322      p->mode = MODE_List;
6323      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
6324      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6325    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
6326      p->mode = MODE_Html;
6327    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
6328      p->mode = MODE_Tcl;
6329      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
6330      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6331    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
6332      p->mode = MODE_Csv;
6333      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
6334      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
6335    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
6336      p->mode = MODE_List;
6337      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
6338    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
6339      p->mode = MODE_Insert;
6340      set_table_name(p, nArg>=3 ? azArg[2] : "table");
6341    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
6342      p->mode = MODE_Quote;
6343    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
6344      p->mode = MODE_Ascii;
6345      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
6346      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
6347    }else if( nArg==1 ){
6348      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
6349    }else{
6350      raw_printf(stderr, "Error: mode should be one of: "
6351         "ascii column csv html insert line list quote tabs tcl\n");
6352      rc = 1;
6353    }
6354    p->cMode = p->mode;
6355  }else
6356
6357  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
6358    if( nArg==2 ){
6359      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
6360                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
6361    }else{
6362      raw_printf(stderr, "Usage: .nullvalue STRING\n");
6363      rc = 1;
6364    }
6365  }else
6366
6367  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
6368    char *zNewFilename;  /* Name of the database file to open */
6369    int iName = 1;       /* Index in azArg[] of the filename */
6370    int newFlag = 0;     /* True to delete file before opening */
6371    /* Close the existing database */
6372    session_close_all(p);
6373    sqlite3_close(p->db);
6374    p->db = 0;
6375    p->zDbFilename = 0;
6376    sqlite3_free(p->zFreeOnClose);
6377    p->zFreeOnClose = 0;
6378    p->openMode = SHELL_OPEN_UNSPEC;
6379    /* Check for command-line arguments */
6380    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
6381      const char *z = azArg[iName];
6382      if( optionMatch(z,"new") ){
6383        newFlag = 1;
6384#ifdef SQLITE_HAVE_ZLIB
6385      }else if( optionMatch(z, "zip") ){
6386        p->openMode = SHELL_OPEN_ZIPFILE;
6387#endif
6388      }else if( optionMatch(z, "append") ){
6389        p->openMode = SHELL_OPEN_APPENDVFS;
6390      }else if( optionMatch(z, "readonly") ){
6391        p->openMode = SHELL_OPEN_READONLY;
6392      }else if( z[0]=='-' ){
6393        utf8_printf(stderr, "unknown option: %s\n", z);
6394        rc = 1;
6395        goto meta_command_exit;
6396      }
6397    }
6398    /* If a filename is specified, try to open it first */
6399    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
6400    if( zNewFilename ){
6401      if( newFlag ) shellDeleteFile(zNewFilename);
6402      p->zDbFilename = zNewFilename;
6403      open_db(p, 1);
6404      if( p->db==0 ){
6405        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
6406        sqlite3_free(zNewFilename);
6407      }else{
6408        p->zFreeOnClose = zNewFilename;
6409      }
6410    }
6411    if( p->db==0 ){
6412      /* As a fall-back open a TEMP database */
6413      p->zDbFilename = 0;
6414      open_db(p, 0);
6415    }
6416  }else
6417
6418  if( (c=='o'
6419        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
6420   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
6421  ){
6422    const char *zFile = nArg>=2 ? azArg[1] : "stdout";
6423    int bTxtMode = 0;
6424    if( azArg[0][0]=='e' ){
6425      /* Transform the ".excel" command into ".once -x" */
6426      nArg = 2;
6427      azArg[0] = "once";
6428      zFile = azArg[1] = "-x";
6429      n = 4;
6430    }
6431    if( nArg>2 ){
6432      utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
6433      rc = 1;
6434      goto meta_command_exit;
6435    }
6436    if( n>1 && strncmp(azArg[0], "once", n)==0 ){
6437      if( nArg<2 ){
6438        raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
6439        rc = 1;
6440        goto meta_command_exit;
6441      }
6442      p->outCount = 2;
6443    }else{
6444      p->outCount = 0;
6445    }
6446    output_reset(p);
6447    if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
6448#ifndef SQLITE_NOHAVE_SYSTEM
6449    if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
6450      p->doXdgOpen = 1;
6451      outputModePush(p);
6452      if( zFile[1]=='x' ){
6453        newTempFile(p, "csv");
6454        p->mode = MODE_Csv;
6455        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
6456        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
6457      }else{
6458        newTempFile(p, "txt");
6459        bTxtMode = 1;
6460      }
6461      zFile = p->zTempFile;
6462    }
6463#endif /* SQLITE_NOHAVE_SYSTEM */
6464    if( zFile[0]=='|' ){
6465#ifdef SQLITE_OMIT_POPEN
6466      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
6467      rc = 1;
6468      p->out = stdout;
6469#else
6470      p->out = popen(zFile + 1, "w");
6471      if( p->out==0 ){
6472        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
6473        p->out = stdout;
6474        rc = 1;
6475      }else{
6476        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
6477      }
6478#endif
6479    }else{
6480      p->out = output_file_open(zFile, bTxtMode);
6481      if( p->out==0 ){
6482        if( strcmp(zFile,"off")!=0 ){
6483          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
6484        }
6485        p->out = stdout;
6486        rc = 1;
6487      } else {
6488        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
6489      }
6490    }
6491  }else
6492
6493  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
6494    int i;
6495    for(i=1; i<nArg; i++){
6496      if( i>1 ) raw_printf(p->out, " ");
6497      utf8_printf(p->out, "%s", azArg[i]);
6498    }
6499    raw_printf(p->out, "\n");
6500  }else
6501
6502  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
6503    if( nArg >= 2) {
6504      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
6505    }
6506    if( nArg >= 3) {
6507      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
6508    }
6509  }else
6510
6511  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
6512    rc = 2;
6513  }else
6514
6515  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
6516    FILE *alt;
6517    if( nArg!=2 ){
6518      raw_printf(stderr, "Usage: .read FILE\n");
6519      rc = 1;
6520      goto meta_command_exit;
6521    }
6522    alt = fopen(azArg[1], "rb");
6523    if( alt==0 ){
6524      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
6525      rc = 1;
6526    }else{
6527      rc = process_input(p, alt);
6528      fclose(alt);
6529    }
6530  }else
6531
6532  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
6533    const char *zSrcFile;
6534    const char *zDb;
6535    sqlite3 *pSrc;
6536    sqlite3_backup *pBackup;
6537    int nTimeout = 0;
6538
6539    if( nArg==2 ){
6540      zSrcFile = azArg[1];
6541      zDb = "main";
6542    }else if( nArg==3 ){
6543      zSrcFile = azArg[2];
6544      zDb = azArg[1];
6545    }else{
6546      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
6547      rc = 1;
6548      goto meta_command_exit;
6549    }
6550    rc = sqlite3_open(zSrcFile, &pSrc);
6551    if( rc!=SQLITE_OK ){
6552      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
6553      sqlite3_close(pSrc);
6554      return 1;
6555    }
6556    open_db(p, 0);
6557    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
6558    if( pBackup==0 ){
6559      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6560      sqlite3_close(pSrc);
6561      return 1;
6562    }
6563    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
6564          || rc==SQLITE_BUSY  ){
6565      if( rc==SQLITE_BUSY ){
6566        if( nTimeout++ >= 3 ) break;
6567        sqlite3_sleep(100);
6568      }
6569    }
6570    sqlite3_backup_finish(pBackup);
6571    if( rc==SQLITE_DONE ){
6572      rc = 0;
6573    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
6574      raw_printf(stderr, "Error: source database is busy\n");
6575      rc = 1;
6576    }else{
6577      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6578      rc = 1;
6579    }
6580    sqlite3_close(pSrc);
6581  }else
6582
6583  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
6584    if( nArg==2 ){
6585      p->scanstatsOn = (u8)booleanValue(azArg[1]);
6586#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
6587      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
6588#endif
6589    }else{
6590      raw_printf(stderr, "Usage: .scanstats on|off\n");
6591      rc = 1;
6592    }
6593  }else
6594
6595  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
6596    ShellText sSelect;
6597    ShellState data;
6598    char *zErrMsg = 0;
6599    const char *zDiv = "(";
6600    const char *zName = 0;
6601    int iSchema = 0;
6602    int bDebug = 0;
6603    int ii;
6604
6605    open_db(p, 0);
6606    memcpy(&data, p, sizeof(data));
6607    data.showHeader = 0;
6608    data.cMode = data.mode = MODE_Semi;
6609    initText(&sSelect);
6610    for(ii=1; ii<nArg; ii++){
6611      if( optionMatch(azArg[ii],"indent") ){
6612        data.cMode = data.mode = MODE_Pretty;
6613      }else if( optionMatch(azArg[ii],"debug") ){
6614        bDebug = 1;
6615      }else if( zName==0 ){
6616        zName = azArg[ii];
6617      }else{
6618        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
6619        rc = 1;
6620        goto meta_command_exit;
6621      }
6622    }
6623    if( zName!=0 ){
6624      int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
6625      if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
6626        char *new_argv[2], *new_colv[2];
6627        new_argv[0] = sqlite3_mprintf(
6628                      "CREATE TABLE %s (\n"
6629                      "  type text,\n"
6630                      "  name text,\n"
6631                      "  tbl_name text,\n"
6632                      "  rootpage integer,\n"
6633                      "  sql text\n"
6634                      ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
6635        new_argv[1] = 0;
6636        new_colv[0] = "sql";
6637        new_colv[1] = 0;
6638        callback(&data, 1, new_argv, new_colv);
6639        sqlite3_free(new_argv[0]);
6640      }
6641    }
6642    if( zDiv ){
6643      sqlite3_stmt *pStmt = 0;
6644      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
6645                              -1, &pStmt, 0);
6646      if( rc ){
6647        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6648        sqlite3_finalize(pStmt);
6649        rc = 1;
6650        goto meta_command_exit;
6651      }
6652      appendText(&sSelect, "SELECT sql FROM", 0);
6653      iSchema = 0;
6654      while( sqlite3_step(pStmt)==SQLITE_ROW ){
6655        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
6656        char zScNum[30];
6657        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
6658        appendText(&sSelect, zDiv, 0);
6659        zDiv = " UNION ALL ";
6660        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
6661        if( sqlite3_stricmp(zDb, "main")!=0 ){
6662          appendText(&sSelect, zDb, '"');
6663        }else{
6664          appendText(&sSelect, "NULL", 0);
6665        }
6666        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
6667        appendText(&sSelect, zScNum, 0);
6668        appendText(&sSelect, " AS snum, ", 0);
6669        appendText(&sSelect, zDb, '\'');
6670        appendText(&sSelect, " AS sname FROM ", 0);
6671        appendText(&sSelect, zDb, '"');
6672        appendText(&sSelect, ".sqlite_master", 0);
6673      }
6674      sqlite3_finalize(pStmt);
6675#ifdef SQLITE_INTROSPECTION_PRAGMAS
6676      if( zName ){
6677        appendText(&sSelect,
6678           " UNION ALL SELECT shell_module_schema(name),"
6679           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
6680      }
6681#endif
6682      appendText(&sSelect, ") WHERE ", 0);
6683      if( zName ){
6684        char *zQarg = sqlite3_mprintf("%Q", zName);
6685        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
6686                    strchr(zName, '[') != 0;
6687        if( strchr(zName, '.') ){
6688          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
6689        }else{
6690          appendText(&sSelect, "lower(tbl_name)", 0);
6691        }
6692        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
6693        appendText(&sSelect, zQarg, 0);
6694        if( !bGlob ){
6695          appendText(&sSelect, " ESCAPE '\\' ", 0);
6696        }
6697        appendText(&sSelect, " AND ", 0);
6698        sqlite3_free(zQarg);
6699      }
6700      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
6701                           " ORDER BY snum, rowid", 0);
6702      if( bDebug ){
6703        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
6704      }else{
6705        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
6706      }
6707      freeText(&sSelect);
6708    }
6709    if( zErrMsg ){
6710      utf8_printf(stderr,"Error: %s\n", zErrMsg);
6711      sqlite3_free(zErrMsg);
6712      rc = 1;
6713    }else if( rc != SQLITE_OK ){
6714      raw_printf(stderr,"Error: querying schema information\n");
6715      rc = 1;
6716    }else{
6717      rc = 0;
6718    }
6719  }else
6720
6721#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
6722  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
6723    sqlite3SelectTrace = (int)integerValue(azArg[1]);
6724  }else
6725#endif
6726
6727#if defined(SQLITE_ENABLE_SESSION)
6728  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
6729    OpenSession *pSession = &p->aSession[0];
6730    char **azCmd = &azArg[1];
6731    int iSes = 0;
6732    int nCmd = nArg - 1;
6733    int i;
6734    if( nArg<=1 ) goto session_syntax_error;
6735    open_db(p, 0);
6736    if( nArg>=3 ){
6737      for(iSes=0; iSes<p->nSession; iSes++){
6738        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
6739      }
6740      if( iSes<p->nSession ){
6741        pSession = &p->aSession[iSes];
6742        azCmd++;
6743        nCmd--;
6744      }else{
6745        pSession = &p->aSession[0];
6746        iSes = 0;
6747      }
6748    }
6749
6750    /* .session attach TABLE
6751    ** Invoke the sqlite3session_attach() interface to attach a particular
6752    ** table so that it is never filtered.
6753    */
6754    if( strcmp(azCmd[0],"attach")==0 ){
6755      if( nCmd!=2 ) goto session_syntax_error;
6756      if( pSession->p==0 ){
6757        session_not_open:
6758        raw_printf(stderr, "ERROR: No sessions are open\n");
6759      }else{
6760        rc = sqlite3session_attach(pSession->p, azCmd[1]);
6761        if( rc ){
6762          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
6763          rc = 0;
6764        }
6765      }
6766    }else
6767
6768    /* .session changeset FILE
6769    ** .session patchset FILE
6770    ** Write a changeset or patchset into a file.  The file is overwritten.
6771    */
6772    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
6773      FILE *out = 0;
6774      if( nCmd!=2 ) goto session_syntax_error;
6775      if( pSession->p==0 ) goto session_not_open;
6776      out = fopen(azCmd[1], "wb");
6777      if( out==0 ){
6778        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
6779      }else{
6780        int szChng;
6781        void *pChng;
6782        if( azCmd[0][0]=='c' ){
6783          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
6784        }else{
6785          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
6786        }
6787        if( rc ){
6788          printf("Error: error code %d\n", rc);
6789          rc = 0;
6790        }
6791        if( pChng
6792          && fwrite(pChng, szChng, 1, out)!=1 ){
6793          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
6794                  szChng);
6795        }
6796        sqlite3_free(pChng);
6797        fclose(out);
6798      }
6799    }else
6800
6801    /* .session close
6802    ** Close the identified session
6803    */
6804    if( strcmp(azCmd[0], "close")==0 ){
6805      if( nCmd!=1 ) goto session_syntax_error;
6806      if( p->nSession ){
6807        session_close(pSession);
6808        p->aSession[iSes] = p->aSession[--p->nSession];
6809      }
6810    }else
6811
6812    /* .session enable ?BOOLEAN?
6813    ** Query or set the enable flag
6814    */
6815    if( strcmp(azCmd[0], "enable")==0 ){
6816      int ii;
6817      if( nCmd>2 ) goto session_syntax_error;
6818      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
6819      if( p->nSession ){
6820        ii = sqlite3session_enable(pSession->p, ii);
6821        utf8_printf(p->out, "session %s enable flag = %d\n",
6822                    pSession->zName, ii);
6823      }
6824    }else
6825
6826    /* .session filter GLOB ....
6827    ** Set a list of GLOB patterns of table names to be excluded.
6828    */
6829    if( strcmp(azCmd[0], "filter")==0 ){
6830      int ii, nByte;
6831      if( nCmd<2 ) goto session_syntax_error;
6832      if( p->nSession ){
6833        for(ii=0; ii<pSession->nFilter; ii++){
6834          sqlite3_free(pSession->azFilter[ii]);
6835        }
6836        sqlite3_free(pSession->azFilter);
6837        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
6838        pSession->azFilter = sqlite3_malloc( nByte );
6839        if( pSession->azFilter==0 ){
6840          raw_printf(stderr, "Error: out or memory\n");
6841          exit(1);
6842        }
6843        for(ii=1; ii<nCmd; ii++){
6844          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
6845        }
6846        pSession->nFilter = ii-1;
6847      }
6848    }else
6849
6850    /* .session indirect ?BOOLEAN?
6851    ** Query or set the indirect flag
6852    */
6853    if( strcmp(azCmd[0], "indirect")==0 ){
6854      int ii;
6855      if( nCmd>2 ) goto session_syntax_error;
6856      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
6857      if( p->nSession ){
6858        ii = sqlite3session_indirect(pSession->p, ii);
6859        utf8_printf(p->out, "session %s indirect flag = %d\n",
6860                    pSession->zName, ii);
6861      }
6862    }else
6863
6864    /* .session isempty
6865    ** Determine if the session is empty
6866    */
6867    if( strcmp(azCmd[0], "isempty")==0 ){
6868      int ii;
6869      if( nCmd!=1 ) goto session_syntax_error;
6870      if( p->nSession ){
6871        ii = sqlite3session_isempty(pSession->p);
6872        utf8_printf(p->out, "session %s isempty flag = %d\n",
6873                    pSession->zName, ii);
6874      }
6875    }else
6876
6877    /* .session list
6878    ** List all currently open sessions
6879    */
6880    if( strcmp(azCmd[0],"list")==0 ){
6881      for(i=0; i<p->nSession; i++){
6882        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
6883      }
6884    }else
6885
6886    /* .session open DB NAME
6887    ** Open a new session called NAME on the attached database DB.
6888    ** DB is normally "main".
6889    */
6890    if( strcmp(azCmd[0],"open")==0 ){
6891      char *zName;
6892      if( nCmd!=3 ) goto session_syntax_error;
6893      zName = azCmd[2];
6894      if( zName[0]==0 ) goto session_syntax_error;
6895      for(i=0; i<p->nSession; i++){
6896        if( strcmp(p->aSession[i].zName,zName)==0 ){
6897          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
6898          goto meta_command_exit;
6899        }
6900      }
6901      if( p->nSession>=ArraySize(p->aSession) ){
6902        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
6903        goto meta_command_exit;
6904      }
6905      pSession = &p->aSession[p->nSession];
6906      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
6907      if( rc ){
6908        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
6909        rc = 0;
6910        goto meta_command_exit;
6911      }
6912      pSession->nFilter = 0;
6913      sqlite3session_table_filter(pSession->p, session_filter, pSession);
6914      p->nSession++;
6915      pSession->zName = sqlite3_mprintf("%s", zName);
6916    }else
6917    /* If no command name matches, show a syntax error */
6918    session_syntax_error:
6919    session_help(p);
6920  }else
6921#endif
6922
6923#ifdef SQLITE_DEBUG
6924  /* Undocumented commands for internal testing.  Subject to change
6925  ** without notice. */
6926  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
6927    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
6928      int i, v;
6929      for(i=1; i<nArg; i++){
6930        v = booleanValue(azArg[i]);
6931        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
6932      }
6933    }
6934    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
6935      int i; sqlite3_int64 v;
6936      for(i=1; i<nArg; i++){
6937        char zBuf[200];
6938        v = integerValue(azArg[i]);
6939        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
6940        utf8_printf(p->out, "%s", zBuf);
6941      }
6942    }
6943  }else
6944#endif
6945
6946  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
6947    int bIsInit = 0;         /* True to initialize the SELFTEST table */
6948    int bVerbose = 0;        /* Verbose output */
6949    int bSelftestExists;     /* True if SELFTEST already exists */
6950    int i, k;                /* Loop counters */
6951    int nTest = 0;           /* Number of tests runs */
6952    int nErr = 0;            /* Number of errors seen */
6953    ShellText str;           /* Answer for a query */
6954    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
6955
6956    open_db(p,0);
6957    for(i=1; i<nArg; i++){
6958      const char *z = azArg[i];
6959      if( z[0]=='-' && z[1]=='-' ) z++;
6960      if( strcmp(z,"-init")==0 ){
6961        bIsInit = 1;
6962      }else
6963      if( strcmp(z,"-v")==0 ){
6964        bVerbose++;
6965      }else
6966      {
6967        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
6968                    azArg[i], azArg[0]);
6969        raw_printf(stderr, "Should be one of: --init -v\n");
6970        rc = 1;
6971        goto meta_command_exit;
6972      }
6973    }
6974    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
6975           != SQLITE_OK ){
6976      bSelftestExists = 0;
6977    }else{
6978      bSelftestExists = 1;
6979    }
6980    if( bIsInit ){
6981      createSelftestTable(p);
6982      bSelftestExists = 1;
6983    }
6984    initText(&str);
6985    appendText(&str, "x", 0);
6986    for(k=bSelftestExists; k>=0; k--){
6987      if( k==1 ){
6988        rc = sqlite3_prepare_v2(p->db,
6989            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
6990            -1, &pStmt, 0);
6991      }else{
6992        rc = sqlite3_prepare_v2(p->db,
6993          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
6994          "      (1,'run','PRAGMA integrity_check','ok')",
6995          -1, &pStmt, 0);
6996      }
6997      if( rc ){
6998        raw_printf(stderr, "Error querying the selftest table\n");
6999        rc = 1;
7000        sqlite3_finalize(pStmt);
7001        goto meta_command_exit;
7002      }
7003      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
7004        int tno = sqlite3_column_int(pStmt, 0);
7005        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
7006        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
7007        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
7008
7009        k = 0;
7010        if( bVerbose>0 ){
7011          char *zQuote = sqlite3_mprintf("%q", zSql);
7012          printf("%d: %s %s\n", tno, zOp, zSql);
7013          sqlite3_free(zQuote);
7014        }
7015        if( strcmp(zOp,"memo")==0 ){
7016          utf8_printf(p->out, "%s\n", zSql);
7017        }else
7018        if( strcmp(zOp,"run")==0 ){
7019          char *zErrMsg = 0;
7020          str.n = 0;
7021          str.z[0] = 0;
7022          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
7023          nTest++;
7024          if( bVerbose ){
7025            utf8_printf(p->out, "Result: %s\n", str.z);
7026          }
7027          if( rc || zErrMsg ){
7028            nErr++;
7029            rc = 1;
7030            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
7031            sqlite3_free(zErrMsg);
7032          }else if( strcmp(zAns,str.z)!=0 ){
7033            nErr++;
7034            rc = 1;
7035            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
7036            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
7037          }
7038        }else
7039        {
7040          utf8_printf(stderr,
7041            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
7042          rc = 1;
7043          break;
7044        }
7045      } /* End loop over rows of content from SELFTEST */
7046      sqlite3_finalize(pStmt);
7047    } /* End loop over k */
7048    freeText(&str);
7049    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
7050  }else
7051
7052  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
7053    if( nArg<2 || nArg>3 ){
7054      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
7055      rc = 1;
7056    }
7057    if( nArg>=2 ){
7058      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
7059                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
7060    }
7061    if( nArg>=3 ){
7062      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
7063                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
7064    }
7065  }else
7066
7067  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
7068    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
7069    int i;                   /* Loop counter */
7070    int bSchema = 0;         /* Also hash the schema */
7071    int bSeparate = 0;       /* Hash each table separately */
7072    int iSize = 224;         /* Hash algorithm to use */
7073    int bDebug = 0;          /* Only show the query that would have run */
7074    sqlite3_stmt *pStmt;     /* For querying tables names */
7075    char *zSql;              /* SQL to be run */
7076    char *zSep;              /* Separator */
7077    ShellText sSql;          /* Complete SQL for the query to run the hash */
7078    ShellText sQuery;        /* Set of queries used to read all content */
7079    open_db(p, 0);
7080    for(i=1; i<nArg; i++){
7081      const char *z = azArg[i];
7082      if( z[0]=='-' ){
7083        z++;
7084        if( z[0]=='-' ) z++;
7085        if( strcmp(z,"schema")==0 ){
7086          bSchema = 1;
7087        }else
7088        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
7089         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
7090        ){
7091          iSize = atoi(&z[5]);
7092        }else
7093        if( strcmp(z,"debug")==0 ){
7094          bDebug = 1;
7095        }else
7096        {
7097          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
7098                      azArg[i], azArg[0]);
7099          raw_printf(stderr, "Should be one of: --schema"
7100                             " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
7101          rc = 1;
7102          goto meta_command_exit;
7103        }
7104      }else if( zLike ){
7105        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
7106        rc = 1;
7107        goto meta_command_exit;
7108      }else{
7109        zLike = z;
7110        bSeparate = 1;
7111        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
7112      }
7113    }
7114    if( bSchema ){
7115      zSql = "SELECT lower(name) FROM sqlite_master"
7116             " WHERE type='table' AND coalesce(rootpage,0)>1"
7117             " UNION ALL SELECT 'sqlite_master'"
7118             " ORDER BY 1 collate nocase";
7119    }else{
7120      zSql = "SELECT lower(name) FROM sqlite_master"
7121             " WHERE type='table' AND coalesce(rootpage,0)>1"
7122             " AND name NOT LIKE 'sqlite_%'"
7123             " ORDER BY 1 collate nocase";
7124    }
7125    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7126    initText(&sQuery);
7127    initText(&sSql);
7128    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
7129    zSep = "VALUES(";
7130    while( SQLITE_ROW==sqlite3_step(pStmt) ){
7131      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
7132      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
7133      if( strncmp(zTab, "sqlite_",7)!=0 ){
7134        appendText(&sQuery,"SELECT * FROM ", 0);
7135        appendText(&sQuery,zTab,'"');
7136        appendText(&sQuery," NOT INDEXED;", 0);
7137      }else if( strcmp(zTab, "sqlite_master")==0 ){
7138        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
7139                           " ORDER BY name;", 0);
7140      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
7141        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
7142                           " ORDER BY name;", 0);
7143      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
7144        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
7145                           " ORDER BY tbl,idx;", 0);
7146      }else if( strcmp(zTab, "sqlite_stat3")==0
7147             || strcmp(zTab, "sqlite_stat4")==0 ){
7148        appendText(&sQuery, "SELECT * FROM ", 0);
7149        appendText(&sQuery, zTab, 0);
7150        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
7151      }
7152      appendText(&sSql, zSep, 0);
7153      appendText(&sSql, sQuery.z, '\'');
7154      sQuery.n = 0;
7155      appendText(&sSql, ",", 0);
7156      appendText(&sSql, zTab, '\'');
7157      zSep = "),(";
7158    }
7159    sqlite3_finalize(pStmt);
7160    if( bSeparate ){
7161      zSql = sqlite3_mprintf(
7162          "%s))"
7163          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
7164          "   FROM [sha3sum$query]",
7165          sSql.z, iSize);
7166    }else{
7167      zSql = sqlite3_mprintf(
7168          "%s))"
7169          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
7170          "   FROM [sha3sum$query]",
7171          sSql.z, iSize);
7172    }
7173    freeText(&sQuery);
7174    freeText(&sSql);
7175    if( bDebug ){
7176      utf8_printf(p->out, "%s\n", zSql);
7177    }else{
7178      shell_exec(p, zSql, 0);
7179    }
7180    sqlite3_free(zSql);
7181  }else
7182
7183#ifndef SQLITE_NOHAVE_SYSTEM
7184  if( c=='s'
7185   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
7186  ){
7187    char *zCmd;
7188    int i, x;
7189    if( nArg<2 ){
7190      raw_printf(stderr, "Usage: .system COMMAND\n");
7191      rc = 1;
7192      goto meta_command_exit;
7193    }
7194    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
7195    for(i=2; i<nArg; i++){
7196      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
7197                             zCmd, azArg[i]);
7198    }
7199    x = system(zCmd);
7200    sqlite3_free(zCmd);
7201    if( x ) raw_printf(stderr, "System command returns %d\n", x);
7202  }else
7203#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
7204
7205  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
7206    static const char *azBool[] = { "off", "on", "trigger", "full"};
7207    int i;
7208    if( nArg!=1 ){
7209      raw_printf(stderr, "Usage: .show\n");
7210      rc = 1;
7211      goto meta_command_exit;
7212    }
7213    utf8_printf(p->out, "%12.12s: %s\n","echo",
7214                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
7215    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
7216    utf8_printf(p->out, "%12.12s: %s\n","explain",
7217         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
7218    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
7219    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
7220    utf8_printf(p->out, "%12.12s: ", "nullvalue");
7221      output_c_string(p->out, p->nullValue);
7222      raw_printf(p->out, "\n");
7223    utf8_printf(p->out,"%12.12s: %s\n","output",
7224            strlen30(p->outfile) ? p->outfile : "stdout");
7225    utf8_printf(p->out,"%12.12s: ", "colseparator");
7226      output_c_string(p->out, p->colSeparator);
7227      raw_printf(p->out, "\n");
7228    utf8_printf(p->out,"%12.12s: ", "rowseparator");
7229      output_c_string(p->out, p->rowSeparator);
7230      raw_printf(p->out, "\n");
7231    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
7232    utf8_printf(p->out, "%12.12s: ", "width");
7233    for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
7234      raw_printf(p->out, "%d ", p->colWidth[i]);
7235    }
7236    raw_printf(p->out, "\n");
7237    utf8_printf(p->out, "%12.12s: %s\n", "filename",
7238                p->zDbFilename ? p->zDbFilename : "");
7239  }else
7240
7241  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
7242    if( nArg==2 ){
7243      p->statsOn = (u8)booleanValue(azArg[1]);
7244    }else if( nArg==1 ){
7245      display_stats(p->db, p, 0);
7246    }else{
7247      raw_printf(stderr, "Usage: .stats ?on|off?\n");
7248      rc = 1;
7249    }
7250  }else
7251
7252  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
7253   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
7254                 || strncmp(azArg[0], "indexes", n)==0) )
7255  ){
7256    sqlite3_stmt *pStmt;
7257    char **azResult;
7258    int nRow, nAlloc;
7259    int ii;
7260    ShellText s;
7261    initText(&s);
7262    open_db(p, 0);
7263    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7264    if( rc ) return shellDatabaseError(p->db);
7265
7266    if( nArg>2 && c=='i' ){
7267      /* It is an historical accident that the .indexes command shows an error
7268      ** when called with the wrong number of arguments whereas the .tables
7269      ** command does not. */
7270      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
7271      rc = 1;
7272      goto meta_command_exit;
7273    }
7274    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
7275      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
7276      if( zDbName==0 ) continue;
7277      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
7278      if( sqlite3_stricmp(zDbName, "main")==0 ){
7279        appendText(&s, "SELECT name FROM ", 0);
7280      }else{
7281        appendText(&s, "SELECT ", 0);
7282        appendText(&s, zDbName, '\'');
7283        appendText(&s, "||'.'||name FROM ", 0);
7284      }
7285      appendText(&s, zDbName, '"');
7286      appendText(&s, ".sqlite_master ", 0);
7287      if( c=='t' ){
7288        appendText(&s," WHERE type IN ('table','view')"
7289                      "   AND name NOT LIKE 'sqlite_%'"
7290                      "   AND name LIKE ?1", 0);
7291      }else{
7292        appendText(&s," WHERE type='index'"
7293                      "   AND tbl_name LIKE ?1", 0);
7294      }
7295    }
7296    rc = sqlite3_finalize(pStmt);
7297    appendText(&s, " ORDER BY 1", 0);
7298    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
7299    freeText(&s);
7300    if( rc ) return shellDatabaseError(p->db);
7301
7302    /* Run the SQL statement prepared by the above block. Store the results
7303    ** as an array of nul-terminated strings in azResult[].  */
7304    nRow = nAlloc = 0;
7305    azResult = 0;
7306    if( nArg>1 ){
7307      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
7308    }else{
7309      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
7310    }
7311    while( sqlite3_step(pStmt)==SQLITE_ROW ){
7312      if( nRow>=nAlloc ){
7313        char **azNew;
7314        int n2 = nAlloc*2 + 10;
7315        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
7316        if( azNew==0 ){
7317          rc = shellNomemError();
7318          break;
7319        }
7320        nAlloc = n2;
7321        azResult = azNew;
7322      }
7323      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7324      if( 0==azResult[nRow] ){
7325        rc = shellNomemError();
7326        break;
7327      }
7328      nRow++;
7329    }
7330    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
7331      rc = shellDatabaseError(p->db);
7332    }
7333
7334    /* Pretty-print the contents of array azResult[] to the output */
7335    if( rc==0 && nRow>0 ){
7336      int len, maxlen = 0;
7337      int i, j;
7338      int nPrintCol, nPrintRow;
7339      for(i=0; i<nRow; i++){
7340        len = strlen30(azResult[i]);
7341        if( len>maxlen ) maxlen = len;
7342      }
7343      nPrintCol = 80/(maxlen+2);
7344      if( nPrintCol<1 ) nPrintCol = 1;
7345      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
7346      for(i=0; i<nPrintRow; i++){
7347        for(j=i; j<nRow; j+=nPrintRow){
7348          char *zSp = j<nPrintRow ? "" : "  ";
7349          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
7350                      azResult[j] ? azResult[j]:"");
7351        }
7352        raw_printf(p->out, "\n");
7353      }
7354    }
7355
7356    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
7357    sqlite3_free(azResult);
7358  }else
7359
7360  /* Begin redirecting output to the file "testcase-out.txt" */
7361  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
7362    output_reset(p);
7363    p->out = output_file_open("testcase-out.txt", 0);
7364    if( p->out==0 ){
7365      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
7366    }
7367    if( nArg>=2 ){
7368      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
7369    }else{
7370      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
7371    }
7372  }else
7373
7374#ifndef SQLITE_UNTESTABLE
7375  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
7376    static const struct {
7377       const char *zCtrlName;   /* Name of a test-control option */
7378       int ctrlCode;            /* Integer code for that option */
7379       const char *zUsage;      /* Usage notes */
7380    } aCtrl[] = {
7381      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
7382      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
7383    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
7384    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
7385      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
7386    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
7387      { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
7388#ifdef SQLITE_N_KEYWORD
7389      { "iskeyword",          SQLITE_TESTCTRL_ISKEYWORD,     "IDENTIFIER"         },
7390#endif
7391      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
7392      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
7393      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
7394#ifdef YYCOVERAGE
7395      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
7396#endif
7397      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
7398      { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
7399      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
7400      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
7401      { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
7402    };
7403    int testctrl = -1;
7404    int iCtrl = -1;
7405    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
7406    int isOk = 0;
7407    int i, n2;
7408    const char *zCmd = 0;
7409
7410    open_db(p, 0);
7411    zCmd = nArg>=2 ? azArg[1] : "help";
7412
7413    /* The argument can optionally begin with "-" or "--" */
7414    if( zCmd[0]=='-' && zCmd[1] ){
7415      zCmd++;
7416      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7417    }
7418
7419    /* --help lists all test-controls */
7420    if( strcmp(zCmd,"help")==0 ){
7421      utf8_printf(p->out, "Available test-controls:\n");
7422      for(i=0; i<ArraySize(aCtrl); i++){
7423        utf8_printf(p->out, "  .testctrl %s %s\n",
7424                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7425      }
7426      rc = 1;
7427      goto meta_command_exit;
7428    }
7429
7430    /* convert testctrl text option to value. allow any unique prefix
7431    ** of the option name, or a numerical value. */
7432    n2 = strlen30(zCmd);
7433    for(i=0; i<ArraySize(aCtrl); i++){
7434      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7435        if( testctrl<0 ){
7436          testctrl = aCtrl[i].ctrlCode;
7437          iCtrl = i;
7438        }else{
7439          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
7440                              "Use \".testctrl --help\" for help\n", zCmd);
7441          rc = 1;
7442          goto meta_command_exit;
7443        }
7444      }
7445    }
7446    if( testctrl<0 ){
7447      utf8_printf(stderr,"Error: unknown test-control: %s\n"
7448                         "Use \".testctrl --help\" for help\n", zCmd);
7449    }else{
7450      switch(testctrl){
7451
7452        /* sqlite3_test_control(int, db, int) */
7453        case SQLITE_TESTCTRL_OPTIMIZATIONS:
7454        case SQLITE_TESTCTRL_RESERVE:
7455          if( nArg==3 ){
7456            int opt = (int)strtol(azArg[2], 0, 0);
7457            rc2 = sqlite3_test_control(testctrl, p->db, opt);
7458            isOk = 3;
7459          }
7460          break;
7461
7462        /* sqlite3_test_control(int) */
7463        case SQLITE_TESTCTRL_PRNG_SAVE:
7464        case SQLITE_TESTCTRL_PRNG_RESTORE:
7465        case SQLITE_TESTCTRL_PRNG_RESET:
7466        case SQLITE_TESTCTRL_BYTEORDER:
7467          if( nArg==2 ){
7468            rc2 = sqlite3_test_control(testctrl);
7469            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
7470          }
7471          break;
7472
7473        /* sqlite3_test_control(int, uint) */
7474        case SQLITE_TESTCTRL_PENDING_BYTE:
7475          if( nArg==3 ){
7476            unsigned int opt = (unsigned int)integerValue(azArg[2]);
7477            rc2 = sqlite3_test_control(testctrl, opt);
7478            isOk = 3;
7479          }
7480          break;
7481
7482        /* sqlite3_test_control(int, int) */
7483        case SQLITE_TESTCTRL_ASSERT:
7484        case SQLITE_TESTCTRL_ALWAYS:
7485          if( nArg==3 ){
7486            int opt = booleanValue(azArg[2]);
7487            rc2 = sqlite3_test_control(testctrl, opt);
7488            isOk = 1;
7489          }
7490          break;
7491
7492        /* sqlite3_test_control(int, int) */
7493        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
7494        case SQLITE_TESTCTRL_NEVER_CORRUPT:
7495          if( nArg==3 ){
7496            int opt = booleanValue(azArg[2]);
7497            rc2 = sqlite3_test_control(testctrl, opt);
7498            isOk = 3;
7499          }
7500          break;
7501
7502        /* sqlite3_test_control(int, char *) */
7503#ifdef SQLITE_N_KEYWORD
7504        case SQLITE_TESTCTRL_ISKEYWORD:
7505          if( nArg==3 ){
7506            const char *opt = azArg[2];
7507            rc2 = sqlite3_test_control(testctrl, opt);
7508            isOk = 1;
7509          }
7510          break;
7511#endif
7512
7513        case SQLITE_TESTCTRL_IMPOSTER:
7514          if( nArg==5 ){
7515            rc2 = sqlite3_test_control(testctrl, p->db,
7516                          azArg[2],
7517                          integerValue(azArg[3]),
7518                          integerValue(azArg[4]));
7519            isOk = 3;
7520          }
7521          break;
7522
7523#ifdef YYCOVERAGE
7524        case SQLITE_TESTCTRL_PARSER_COVERAGE:
7525          if( nArg==2 ){
7526            sqlite3_test_control(testctrl, p->out);
7527            isOk = 3;
7528          }
7529#endif
7530      }
7531    }
7532    if( isOk==0 && iCtrl>=0 ){
7533      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
7534      rc = 1;
7535    }else if( isOk==1 ){
7536      raw_printf(p->out, "%d\n", rc2);
7537    }else if( isOk==2 ){
7538      raw_printf(p->out, "0x%08x\n", rc2);
7539    }
7540  }else
7541#endif /* !defined(SQLITE_UNTESTABLE) */
7542
7543  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
7544    open_db(p, 0);
7545    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
7546  }else
7547
7548  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
7549    if( nArg==2 ){
7550      enableTimer = booleanValue(azArg[1]);
7551      if( enableTimer && !HAS_TIMER ){
7552        raw_printf(stderr, "Error: timer not available on this system.\n");
7553        enableTimer = 0;
7554      }
7555    }else{
7556      raw_printf(stderr, "Usage: .timer on|off\n");
7557      rc = 1;
7558    }
7559  }else
7560
7561  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
7562    open_db(p, 0);
7563    if( nArg!=2 ){
7564      raw_printf(stderr, "Usage: .trace FILE|off\n");
7565      rc = 1;
7566      goto meta_command_exit;
7567    }
7568    output_file_close(p->traceOut);
7569    p->traceOut = output_file_open(azArg[1], 0);
7570#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
7571    if( p->traceOut==0 ){
7572      sqlite3_trace_v2(p->db, 0, 0, 0);
7573    }else{
7574      sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
7575    }
7576#endif
7577  }else
7578
7579#if SQLITE_USER_AUTHENTICATION
7580  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
7581    if( nArg<2 ){
7582      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
7583      rc = 1;
7584      goto meta_command_exit;
7585    }
7586    open_db(p, 0);
7587    if( strcmp(azArg[1],"login")==0 ){
7588      if( nArg!=4 ){
7589        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
7590        rc = 1;
7591        goto meta_command_exit;
7592      }
7593      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
7594      if( rc ){
7595        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
7596        rc = 1;
7597      }
7598    }else if( strcmp(azArg[1],"add")==0 ){
7599      if( nArg!=5 ){
7600        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
7601        rc = 1;
7602        goto meta_command_exit;
7603      }
7604      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
7605                            booleanValue(azArg[4]));
7606      if( rc ){
7607        raw_printf(stderr, "User-Add failed: %d\n", rc);
7608        rc = 1;
7609      }
7610    }else if( strcmp(azArg[1],"edit")==0 ){
7611      if( nArg!=5 ){
7612        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
7613        rc = 1;
7614        goto meta_command_exit;
7615      }
7616      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
7617                              booleanValue(azArg[4]));
7618      if( rc ){
7619        raw_printf(stderr, "User-Edit failed: %d\n", rc);
7620        rc = 1;
7621      }
7622    }else if( strcmp(azArg[1],"delete")==0 ){
7623      if( nArg!=3 ){
7624        raw_printf(stderr, "Usage: .user delete USER\n");
7625        rc = 1;
7626        goto meta_command_exit;
7627      }
7628      rc = sqlite3_user_delete(p->db, azArg[2]);
7629      if( rc ){
7630        raw_printf(stderr, "User-Delete failed: %d\n", rc);
7631        rc = 1;
7632      }
7633    }else{
7634      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
7635      rc = 1;
7636      goto meta_command_exit;
7637    }
7638  }else
7639#endif /* SQLITE_USER_AUTHENTICATION */
7640
7641  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
7642    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
7643        sqlite3_libversion(), sqlite3_sourceid());
7644#if SQLITE_HAVE_ZLIB
7645    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
7646#endif
7647#define CTIMEOPT_VAL_(opt) #opt
7648#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
7649#if defined(__clang__) && defined(__clang_major__)
7650    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
7651                    CTIMEOPT_VAL(__clang_minor__) "."
7652                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
7653#elif defined(_MSC_VER)
7654    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
7655#elif defined(__GNUC__) && defined(__VERSION__)
7656    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
7657#endif
7658  }else
7659
7660  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
7661    const char *zDbName = nArg==2 ? azArg[1] : "main";
7662    sqlite3_vfs *pVfs = 0;
7663    if( p->db ){
7664      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
7665      if( pVfs ){
7666        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
7667        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
7668        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
7669        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
7670      }
7671    }
7672  }else
7673
7674  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
7675    sqlite3_vfs *pVfs;
7676    sqlite3_vfs *pCurrent = 0;
7677    if( p->db ){
7678      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
7679    }
7680    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
7681      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
7682           pVfs==pCurrent ? "  <--- CURRENT" : "");
7683      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
7684      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
7685      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
7686      if( pVfs->pNext ){
7687        raw_printf(p->out, "-----------------------------------\n");
7688      }
7689    }
7690  }else
7691
7692  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
7693    const char *zDbName = nArg==2 ? azArg[1] : "main";
7694    char *zVfsName = 0;
7695    if( p->db ){
7696      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
7697      if( zVfsName ){
7698        utf8_printf(p->out, "%s\n", zVfsName);
7699        sqlite3_free(zVfsName);
7700      }
7701    }
7702  }else
7703
7704#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
7705  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
7706    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
7707  }else
7708#endif
7709
7710  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
7711    int j;
7712    assert( nArg<=ArraySize(azArg) );
7713    for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
7714      p->colWidth[j-1] = (int)integerValue(azArg[j]);
7715    }
7716  }else
7717
7718  {
7719    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
7720      " \"%s\". Enter \".help\" for help\n", azArg[0]);
7721    rc = 1;
7722  }
7723
7724meta_command_exit:
7725  if( p->outCount ){
7726    p->outCount--;
7727    if( p->outCount==0 ) output_reset(p);
7728  }
7729  return rc;
7730}
7731
7732/*
7733** Return TRUE if a semicolon occurs anywhere in the first N characters
7734** of string z[].
7735*/
7736static int line_contains_semicolon(const char *z, int N){
7737  int i;
7738  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
7739  return 0;
7740}
7741
7742/*
7743** Test to see if a line consists entirely of whitespace.
7744*/
7745static int _all_whitespace(const char *z){
7746  for(; *z; z++){
7747    if( IsSpace(z[0]) ) continue;
7748    if( *z=='/' && z[1]=='*' ){
7749      z += 2;
7750      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
7751      if( *z==0 ) return 0;
7752      z++;
7753      continue;
7754    }
7755    if( *z=='-' && z[1]=='-' ){
7756      z += 2;
7757      while( *z && *z!='\n' ){ z++; }
7758      if( *z==0 ) return 1;
7759      continue;
7760    }
7761    return 0;
7762  }
7763  return 1;
7764}
7765
7766/*
7767** Return TRUE if the line typed in is an SQL command terminator other
7768** than a semi-colon.  The SQL Server style "go" command is understood
7769** as is the Oracle "/".
7770*/
7771static int line_is_command_terminator(const char *zLine){
7772  while( IsSpace(zLine[0]) ){ zLine++; };
7773  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
7774    return 1;  /* Oracle */
7775  }
7776  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
7777         && _all_whitespace(&zLine[2]) ){
7778    return 1;  /* SQL Server */
7779  }
7780  return 0;
7781}
7782
7783/*
7784** We need a default sqlite3_complete() implementation to use in case
7785** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
7786** any arbitrary text is a complete SQL statement.  This is not very
7787** user-friendly, but it does seem to work.
7788*/
7789#ifdef SQLITE_OMIT_COMPLETE
7790int sqlite3_complete(const char *zSql){ return 1; }
7791#endif
7792
7793/*
7794** Return true if zSql is a complete SQL statement.  Return false if it
7795** ends in the middle of a string literal or C-style comment.
7796*/
7797static int line_is_complete(char *zSql, int nSql){
7798  int rc;
7799  if( zSql==0 ) return 1;
7800  zSql[nSql] = ';';
7801  zSql[nSql+1] = 0;
7802  rc = sqlite3_complete(zSql);
7803  zSql[nSql] = 0;
7804  return rc;
7805}
7806
7807/*
7808** Run a single line of SQL
7809*/
7810static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
7811  int rc;
7812  char *zErrMsg = 0;
7813
7814  open_db(p, 0);
7815  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
7816  BEGIN_TIMER;
7817  rc = shell_exec(p, zSql, &zErrMsg);
7818  END_TIMER;
7819  if( rc || zErrMsg ){
7820    char zPrefix[100];
7821    if( in!=0 || !stdin_is_interactive ){
7822      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
7823                       "Error: near line %d:", startline);
7824    }else{
7825      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
7826    }
7827    if( zErrMsg!=0 ){
7828      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
7829      sqlite3_free(zErrMsg);
7830      zErrMsg = 0;
7831    }else{
7832      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
7833    }
7834    return 1;
7835  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
7836    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
7837            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
7838  }
7839  return 0;
7840}
7841
7842
7843/*
7844** Read input from *in and process it.  If *in==0 then input
7845** is interactive - the user is typing it it.  Otherwise, input
7846** is coming from a file or device.  A prompt is issued and history
7847** is saved only if input is interactive.  An interrupt signal will
7848** cause this routine to exit immediately, unless input is interactive.
7849**
7850** Return the number of errors.
7851*/
7852static int process_input(ShellState *p, FILE *in){
7853  char *zLine = 0;          /* A single input line */
7854  char *zSql = 0;           /* Accumulated SQL text */
7855  int nLine;                /* Length of current line */
7856  int nSql = 0;             /* Bytes of zSql[] used */
7857  int nAlloc = 0;           /* Allocated zSql[] space */
7858  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
7859  int rc;                   /* Error code */
7860  int errCnt = 0;           /* Number of errors seen */
7861  int lineno = 0;           /* Current line number */
7862  int startline = 0;        /* Line number for start of current input */
7863
7864  while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
7865    fflush(p->out);
7866    zLine = one_input_line(in, zLine, nSql>0);
7867    if( zLine==0 ){
7868      /* End of input */
7869      if( in==0 && stdin_is_interactive ) printf("\n");
7870      break;
7871    }
7872    if( seenInterrupt ){
7873      if( in!=0 ) break;
7874      seenInterrupt = 0;
7875    }
7876    lineno++;
7877    if( nSql==0 && _all_whitespace(zLine) ){
7878      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
7879      continue;
7880    }
7881    if( zLine && zLine[0]=='.' && nSql==0 ){
7882      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
7883      rc = do_meta_command(zLine, p);
7884      if( rc==2 ){ /* exit requested */
7885        break;
7886      }else if( rc ){
7887        errCnt++;
7888      }
7889      continue;
7890    }
7891    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
7892      memcpy(zLine,";",2);
7893    }
7894    nLine = strlen30(zLine);
7895    if( nSql+nLine+2>=nAlloc ){
7896      nAlloc = nSql+nLine+100;
7897      zSql = realloc(zSql, nAlloc);
7898      if( zSql==0 ){
7899        raw_printf(stderr, "Error: out of memory\n");
7900        exit(1);
7901      }
7902    }
7903    nSqlPrior = nSql;
7904    if( nSql==0 ){
7905      int i;
7906      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
7907      assert( nAlloc>0 && zSql!=0 );
7908      memcpy(zSql, zLine+i, nLine+1-i);
7909      startline = lineno;
7910      nSql = nLine-i;
7911    }else{
7912      zSql[nSql++] = '\n';
7913      memcpy(zSql+nSql, zLine, nLine+1);
7914      nSql += nLine;
7915    }
7916    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
7917                && sqlite3_complete(zSql) ){
7918      errCnt += runOneSqlLine(p, zSql, in, startline);
7919      nSql = 0;
7920      if( p->outCount ){
7921        output_reset(p);
7922        p->outCount = 0;
7923      }else{
7924        clearTempFile(p);
7925      }
7926    }else if( nSql && _all_whitespace(zSql) ){
7927      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
7928      nSql = 0;
7929    }
7930  }
7931  if( nSql && !_all_whitespace(zSql) ){
7932    runOneSqlLine(p, zSql, in, startline);
7933  }
7934  free(zSql);
7935  free(zLine);
7936  return errCnt>0;
7937}
7938
7939/*
7940** Return a pathname which is the user's home directory.  A
7941** 0 return indicates an error of some kind.
7942*/
7943static char *find_home_dir(int clearFlag){
7944  static char *home_dir = NULL;
7945  if( clearFlag ){
7946    free(home_dir);
7947    home_dir = 0;
7948    return 0;
7949  }
7950  if( home_dir ) return home_dir;
7951
7952#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
7953     && !defined(__RTP__) && !defined(_WRS_KERNEL)
7954  {
7955    struct passwd *pwent;
7956    uid_t uid = getuid();
7957    if( (pwent=getpwuid(uid)) != NULL) {
7958      home_dir = pwent->pw_dir;
7959    }
7960  }
7961#endif
7962
7963#if defined(_WIN32_WCE)
7964  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
7965   */
7966  home_dir = "/";
7967#else
7968
7969#if defined(_WIN32) || defined(WIN32)
7970  if (!home_dir) {
7971    home_dir = getenv("USERPROFILE");
7972  }
7973#endif
7974
7975  if (!home_dir) {
7976    home_dir = getenv("HOME");
7977  }
7978
7979#if defined(_WIN32) || defined(WIN32)
7980  if (!home_dir) {
7981    char *zDrive, *zPath;
7982    int n;
7983    zDrive = getenv("HOMEDRIVE");
7984    zPath = getenv("HOMEPATH");
7985    if( zDrive && zPath ){
7986      n = strlen30(zDrive) + strlen30(zPath) + 1;
7987      home_dir = malloc( n );
7988      if( home_dir==0 ) return 0;
7989      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
7990      return home_dir;
7991    }
7992    home_dir = "c:\\";
7993  }
7994#endif
7995
7996#endif /* !_WIN32_WCE */
7997
7998  if( home_dir ){
7999    int n = strlen30(home_dir) + 1;
8000    char *z = malloc( n );
8001    if( z ) memcpy(z, home_dir, n);
8002    home_dir = z;
8003  }
8004
8005  return home_dir;
8006}
8007
8008/*
8009** Read input from the file given by sqliterc_override.  Or if that
8010** parameter is NULL, take input from ~/.sqliterc
8011**
8012** Returns the number of errors.
8013*/
8014static void process_sqliterc(
8015  ShellState *p,                  /* Configuration data */
8016  const char *sqliterc_override   /* Name of config file. NULL to use default */
8017){
8018  char *home_dir = NULL;
8019  const char *sqliterc = sqliterc_override;
8020  char *zBuf = 0;
8021  FILE *in = NULL;
8022
8023  if (sqliterc == NULL) {
8024    home_dir = find_home_dir(0);
8025    if( home_dir==0 ){
8026      raw_printf(stderr, "-- warning: cannot find home directory;"
8027                      " cannot read ~/.sqliterc\n");
8028      return;
8029    }
8030    sqlite3_initialize();
8031    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
8032    sqliterc = zBuf;
8033  }
8034  in = fopen(sqliterc,"rb");
8035  if( in ){
8036    if( stdin_is_interactive ){
8037      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
8038    }
8039    process_input(p,in);
8040    fclose(in);
8041  }
8042  sqlite3_free(zBuf);
8043}
8044
8045/*
8046** Show available command line options
8047*/
8048static const char zOptions[] =
8049#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
8050  "   -A ARGS...           run \".archive ARGS\" and exit\n"
8051#endif
8052  "   -append              append the database to the end of the file\n"
8053  "   -ascii               set output mode to 'ascii'\n"
8054  "   -bail                stop after hitting an error\n"
8055  "   -batch               force batch I/O\n"
8056  "   -column              set output mode to 'column'\n"
8057  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
8058  "   -csv                 set output mode to 'csv'\n"
8059  "   -echo                print commands before execution\n"
8060  "   -init FILENAME       read/process named file\n"
8061  "   -[no]header          turn headers on or off\n"
8062#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8063  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
8064#endif
8065  "   -help                show this message\n"
8066  "   -html                set output mode to HTML\n"
8067  "   -interactive         force interactive I/O\n"
8068  "   -line                set output mode to 'line'\n"
8069  "   -list                set output mode to 'list'\n"
8070  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
8071  "   -mmap N              default mmap size set to N\n"
8072#ifdef SQLITE_ENABLE_MULTIPLEX
8073  "   -multiplex           enable the multiplexor VFS\n"
8074#endif
8075  "   -newline SEP         set output row separator. Default: '\\n'\n"
8076  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
8077  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
8078  "   -quote               set output mode to 'quote'\n"
8079  "   -readonly            open the database read-only\n"
8080  "   -separator SEP       set output column separator. Default: '|'\n"
8081  "   -stats               print memory stats before each finalize\n"
8082  "   -version             show SQLite version\n"
8083  "   -vfs NAME            use NAME as the default VFS\n"
8084#ifdef SQLITE_ENABLE_VFSTRACE
8085  "   -vfstrace            enable tracing of all VFS calls\n"
8086#endif
8087#ifdef SQLITE_HAVE_ZLIB
8088  "   -zip                 open the file as a ZIP Archive\n"
8089#endif
8090;
8091static void usage(int showDetail){
8092  utf8_printf(stderr,
8093      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
8094      "FILENAME is the name of an SQLite database. A new database is created\n"
8095      "if the file does not previously exist.\n", Argv0);
8096  if( showDetail ){
8097    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
8098  }else{
8099    raw_printf(stderr, "Use the -help option for additional information\n");
8100  }
8101  exit(1);
8102}
8103
8104/*
8105** Initialize the state information in data
8106*/
8107static void main_init(ShellState *data) {
8108  memset(data, 0, sizeof(*data));
8109  data->normalMode = data->cMode = data->mode = MODE_List;
8110  data->autoExplain = 1;
8111  memcpy(data->colSeparator,SEP_Column, 2);
8112  memcpy(data->rowSeparator,SEP_Row, 2);
8113  data->showHeader = 0;
8114  data->shellFlgs = SHFLG_Lookaside;
8115  sqlite3_config(SQLITE_CONFIG_URI, 1);
8116  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
8117  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
8118  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
8119  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
8120}
8121
8122/*
8123** Output text to the console in a font that attracts extra attention.
8124*/
8125#ifdef _WIN32
8126static void printBold(const char *zText){
8127  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
8128  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
8129  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
8130  SetConsoleTextAttribute(out,
8131         FOREGROUND_RED|FOREGROUND_INTENSITY
8132  );
8133  printf("%s", zText);
8134  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
8135}
8136#else
8137static void printBold(const char *zText){
8138  printf("\033[1m%s\033[0m", zText);
8139}
8140#endif
8141
8142/*
8143** Get the argument to an --option.  Throw an error and die if no argument
8144** is available.
8145*/
8146static char *cmdline_option_value(int argc, char **argv, int i){
8147  if( i==argc ){
8148    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
8149            argv[0], argv[argc-1]);
8150    exit(1);
8151  }
8152  return argv[i];
8153}
8154
8155#ifndef SQLITE_SHELL_IS_UTF8
8156#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
8157#    define SQLITE_SHELL_IS_UTF8          (0)
8158#  else
8159#    define SQLITE_SHELL_IS_UTF8          (1)
8160#  endif
8161#endif
8162
8163#if SQLITE_SHELL_IS_UTF8
8164int SQLITE_CDECL main(int argc, char **argv){
8165#else
8166int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
8167  char **argv;
8168#endif
8169  char *zErrMsg = 0;
8170  ShellState data;
8171  const char *zInitFile = 0;
8172  int i;
8173  int rc = 0;
8174  int warnInmemoryDb = 0;
8175  int readStdin = 1;
8176  int nCmd = 0;
8177  char **azCmd = 0;
8178
8179  setBinaryMode(stdin, 0);
8180  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
8181  stdin_is_interactive = isatty(0);
8182  stdout_is_console = isatty(1);
8183
8184#if USE_SYSTEM_SQLITE+0!=1
8185  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
8186    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
8187            sqlite3_sourceid(), SQLITE_SOURCE_ID);
8188    exit(1);
8189  }
8190#endif
8191  main_init(&data);
8192
8193  /* On Windows, we must translate command-line arguments into UTF-8.
8194  ** The SQLite memory allocator subsystem has to be enabled in order to
8195  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
8196  ** subsequent sqlite3_config() calls will work.  So copy all results into
8197  ** memory that does not come from the SQLite memory allocator.
8198  */
8199#if !SQLITE_SHELL_IS_UTF8
8200  sqlite3_initialize();
8201  argv = malloc(sizeof(argv[0])*argc);
8202  if( argv==0 ){
8203    raw_printf(stderr, "out of memory\n");
8204    exit(1);
8205  }
8206  for(i=0; i<argc; i++){
8207    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
8208    int n;
8209    if( z==0 ){
8210      raw_printf(stderr, "out of memory\n");
8211      exit(1);
8212    }
8213    n = (int)strlen(z);
8214    argv[i] = malloc( n+1 );
8215    if( argv[i]==0 ){
8216      raw_printf(stderr, "out of memory\n");
8217      exit(1);
8218    }
8219    memcpy(argv[i], z, n+1);
8220    sqlite3_free(z);
8221  }
8222  sqlite3_shutdown();
8223#endif
8224
8225  assert( argc>=1 && argv && argv[0] );
8226  Argv0 = argv[0];
8227
8228  /* Make sure we have a valid signal handler early, before anything
8229  ** else is done.
8230  */
8231#ifdef SIGINT
8232  signal(SIGINT, interrupt_handler);
8233#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
8234  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
8235#endif
8236
8237#ifdef SQLITE_SHELL_DBNAME_PROC
8238  {
8239    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
8240    ** of a C-function that will provide the name of the database file.  Use
8241    ** this compile-time option to embed this shell program in larger
8242    ** applications. */
8243    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
8244    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
8245    warnInmemoryDb = 0;
8246  }
8247#endif
8248
8249  /* Do an initial pass through the command-line argument to locate
8250  ** the name of the database file, the name of the initialization file,
8251  ** the size of the alternative malloc heap,
8252  ** and the first command to execute.
8253  */
8254  for(i=1; i<argc; i++){
8255    char *z;
8256    z = argv[i];
8257    if( z[0]!='-' ){
8258      if( data.zDbFilename==0 ){
8259        data.zDbFilename = z;
8260      }else{
8261        /* Excesss arguments are interpreted as SQL (or dot-commands) and
8262        ** mean that nothing is read from stdin */
8263        readStdin = 0;
8264        nCmd++;
8265        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
8266        if( azCmd==0 ){
8267          raw_printf(stderr, "out of memory\n");
8268          exit(1);
8269        }
8270        azCmd[nCmd-1] = z;
8271      }
8272    }
8273    if( z[1]=='-' ) z++;
8274    if( strcmp(z,"-separator")==0
8275     || strcmp(z,"-nullvalue")==0
8276     || strcmp(z,"-newline")==0
8277     || strcmp(z,"-cmd")==0
8278    ){
8279      (void)cmdline_option_value(argc, argv, ++i);
8280    }else if( strcmp(z,"-init")==0 ){
8281      zInitFile = cmdline_option_value(argc, argv, ++i);
8282    }else if( strcmp(z,"-batch")==0 ){
8283      /* Need to check for batch mode here to so we can avoid printing
8284      ** informational messages (like from process_sqliterc) before
8285      ** we do the actual processing of arguments later in a second pass.
8286      */
8287      stdin_is_interactive = 0;
8288    }else if( strcmp(z,"-heap")==0 ){
8289#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8290      const char *zSize;
8291      sqlite3_int64 szHeap;
8292
8293      zSize = cmdline_option_value(argc, argv, ++i);
8294      szHeap = integerValue(zSize);
8295      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
8296      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
8297#else
8298      (void)cmdline_option_value(argc, argv, ++i);
8299#endif
8300    }else if( strcmp(z,"-pagecache")==0 ){
8301      int n, sz;
8302      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
8303      if( sz>70000 ) sz = 70000;
8304      if( sz<0 ) sz = 0;
8305      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
8306      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
8307                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
8308      data.shellFlgs |= SHFLG_Pagecache;
8309    }else if( strcmp(z,"-lookaside")==0 ){
8310      int n, sz;
8311      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
8312      if( sz<0 ) sz = 0;
8313      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
8314      if( n<0 ) n = 0;
8315      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
8316      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
8317#ifdef SQLITE_ENABLE_VFSTRACE
8318    }else if( strcmp(z,"-vfstrace")==0 ){
8319      extern int vfstrace_register(
8320         const char *zTraceName,
8321         const char *zOldVfsName,
8322         int (*xOut)(const char*,void*),
8323         void *pOutArg,
8324         int makeDefault
8325      );
8326      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
8327#endif
8328#ifdef SQLITE_ENABLE_MULTIPLEX
8329    }else if( strcmp(z,"-multiplex")==0 ){
8330      extern int sqlite3_multiple_initialize(const char*,int);
8331      sqlite3_multiplex_initialize(0, 1);
8332#endif
8333    }else if( strcmp(z,"-mmap")==0 ){
8334      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
8335      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
8336    }else if( strcmp(z,"-vfs")==0 ){
8337      sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
8338      if( pVfs ){
8339        sqlite3_vfs_register(pVfs, 1);
8340      }else{
8341        utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
8342        exit(1);
8343      }
8344#ifdef SQLITE_HAVE_ZLIB
8345    }else if( strcmp(z,"-zip")==0 ){
8346      data.openMode = SHELL_OPEN_ZIPFILE;
8347#endif
8348    }else if( strcmp(z,"-append")==0 ){
8349      data.openMode = SHELL_OPEN_APPENDVFS;
8350    }else if( strcmp(z,"-readonly")==0 ){
8351      data.openMode = SHELL_OPEN_READONLY;
8352#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
8353    }else if( strncmp(z, "-A",2)==0 ){
8354      /* All remaining command-line arguments are passed to the ".archive"
8355      ** command, so ignore them */
8356      break;
8357#endif
8358    }
8359  }
8360  if( data.zDbFilename==0 ){
8361#ifndef SQLITE_OMIT_MEMORYDB
8362    data.zDbFilename = ":memory:";
8363    warnInmemoryDb = argc==1;
8364#else
8365    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
8366    return 1;
8367#endif
8368  }
8369  data.out = stdout;
8370  sqlite3_appendvfs_init(0,0,0);
8371
8372  /* Go ahead and open the database file if it already exists.  If the
8373  ** file does not exist, delay opening it.  This prevents empty database
8374  ** files from being created if a user mistypes the database name argument
8375  ** to the sqlite command-line tool.
8376  */
8377  if( access(data.zDbFilename, 0)==0 ){
8378    open_db(&data, 0);
8379  }
8380
8381  /* Process the initialization file if there is one.  If no -init option
8382  ** is given on the command line, look for a file named ~/.sqliterc and
8383  ** try to process it.
8384  */
8385  process_sqliterc(&data,zInitFile);
8386
8387  /* Make a second pass through the command-line argument and set
8388  ** options.  This second pass is delayed until after the initialization
8389  ** file is processed so that the command-line arguments will override
8390  ** settings in the initialization file.
8391  */
8392  for(i=1; i<argc; i++){
8393    char *z = argv[i];
8394    if( z[0]!='-' ) continue;
8395    if( z[1]=='-' ){ z++; }
8396    if( strcmp(z,"-init")==0 ){
8397      i++;
8398    }else if( strcmp(z,"-html")==0 ){
8399      data.mode = MODE_Html;
8400    }else if( strcmp(z,"-list")==0 ){
8401      data.mode = MODE_List;
8402    }else if( strcmp(z,"-quote")==0 ){
8403      data.mode = MODE_Quote;
8404    }else if( strcmp(z,"-line")==0 ){
8405      data.mode = MODE_Line;
8406    }else if( strcmp(z,"-column")==0 ){
8407      data.mode = MODE_Column;
8408    }else if( strcmp(z,"-csv")==0 ){
8409      data.mode = MODE_Csv;
8410      memcpy(data.colSeparator,",",2);
8411#ifdef SQLITE_HAVE_ZLIB
8412    }else if( strcmp(z,"-zip")==0 ){
8413      data.openMode = SHELL_OPEN_ZIPFILE;
8414#endif
8415    }else if( strcmp(z,"-append")==0 ){
8416      data.openMode = SHELL_OPEN_APPENDVFS;
8417    }else if( strcmp(z,"-readonly")==0 ){
8418      data.openMode = SHELL_OPEN_READONLY;
8419    }else if( strcmp(z,"-ascii")==0 ){
8420      data.mode = MODE_Ascii;
8421      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
8422                       SEP_Unit);
8423      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
8424                       SEP_Record);
8425    }else if( strcmp(z,"-separator")==0 ){
8426      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
8427                       "%s",cmdline_option_value(argc,argv,++i));
8428    }else if( strcmp(z,"-newline")==0 ){
8429      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
8430                       "%s",cmdline_option_value(argc,argv,++i));
8431    }else if( strcmp(z,"-nullvalue")==0 ){
8432      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
8433                       "%s",cmdline_option_value(argc,argv,++i));
8434    }else if( strcmp(z,"-header")==0 ){
8435      data.showHeader = 1;
8436    }else if( strcmp(z,"-noheader")==0 ){
8437      data.showHeader = 0;
8438    }else if( strcmp(z,"-echo")==0 ){
8439      ShellSetFlag(&data, SHFLG_Echo);
8440    }else if( strcmp(z,"-eqp")==0 ){
8441      data.autoEQP = AUTOEQP_on;
8442    }else if( strcmp(z,"-eqpfull")==0 ){
8443      data.autoEQP = AUTOEQP_full;
8444    }else if( strcmp(z,"-stats")==0 ){
8445      data.statsOn = 1;
8446    }else if( strcmp(z,"-scanstats")==0 ){
8447      data.scanstatsOn = 1;
8448    }else if( strcmp(z,"-backslash")==0 ){
8449      /* Undocumented command-line option: -backslash
8450      ** Causes C-style backslash escapes to be evaluated in SQL statements
8451      ** prior to sending the SQL into SQLite.  Useful for injecting
8452      ** crazy bytes in the middle of SQL statements for testing and debugging.
8453      */
8454      ShellSetFlag(&data, SHFLG_Backslash);
8455    }else if( strcmp(z,"-bail")==0 ){
8456      bail_on_error = 1;
8457    }else if( strcmp(z,"-version")==0 ){
8458      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
8459      return 0;
8460    }else if( strcmp(z,"-interactive")==0 ){
8461      stdin_is_interactive = 1;
8462    }else if( strcmp(z,"-batch")==0 ){
8463      stdin_is_interactive = 0;
8464    }else if( strcmp(z,"-heap")==0 ){
8465      i++;
8466    }else if( strcmp(z,"-pagecache")==0 ){
8467      i+=2;
8468    }else if( strcmp(z,"-lookaside")==0 ){
8469      i+=2;
8470    }else if( strcmp(z,"-mmap")==0 ){
8471      i++;
8472    }else if( strcmp(z,"-vfs")==0 ){
8473      i++;
8474#ifdef SQLITE_ENABLE_VFSTRACE
8475    }else if( strcmp(z,"-vfstrace")==0 ){
8476      i++;
8477#endif
8478#ifdef SQLITE_ENABLE_MULTIPLEX
8479    }else if( strcmp(z,"-multiplex")==0 ){
8480      i++;
8481#endif
8482    }else if( strcmp(z,"-help")==0 ){
8483      usage(1);
8484    }else if( strcmp(z,"-cmd")==0 ){
8485      /* Run commands that follow -cmd first and separately from commands
8486      ** that simply appear on the command-line.  This seems goofy.  It would
8487      ** be better if all commands ran in the order that they appear.  But
8488      ** we retain the goofy behavior for historical compatibility. */
8489      if( i==argc-1 ) break;
8490      z = cmdline_option_value(argc,argv,++i);
8491      if( z[0]=='.' ){
8492        rc = do_meta_command(z, &data);
8493        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
8494      }else{
8495        open_db(&data, 0);
8496        rc = shell_exec(&data, z, &zErrMsg);
8497        if( zErrMsg!=0 ){
8498          utf8_printf(stderr,"Error: %s\n", zErrMsg);
8499          if( bail_on_error ) return rc!=0 ? rc : 1;
8500        }else if( rc!=0 ){
8501          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
8502          if( bail_on_error ) return rc;
8503        }
8504      }
8505#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
8506    }else if( strncmp(z, "-A", 2)==0 ){
8507      if( nCmd>0 ){
8508        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
8509                            " with \"%s\"\n", z);
8510        return 1;
8511      }
8512      open_db(&data, 0);
8513      if( z[2] ){
8514        argv[i] = &z[2];
8515        arDotCommand(&data, argv+(i-1), argc-(i-1));
8516      }else{
8517        arDotCommand(&data, argv+i, argc-i);
8518      }
8519      readStdin = 0;
8520      break;
8521#endif
8522    }else{
8523      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
8524      raw_printf(stderr,"Use -help for a list of options.\n");
8525      return 1;
8526    }
8527    data.cMode = data.mode;
8528  }
8529
8530  if( !readStdin ){
8531    /* Run all arguments that do not begin with '-' as if they were separate
8532    ** command-line inputs, except for the argToSkip argument which contains
8533    ** the database filename.
8534    */
8535    for(i=0; i<nCmd; i++){
8536      if( azCmd[i][0]=='.' ){
8537        rc = do_meta_command(azCmd[i], &data);
8538        if( rc ) return rc==2 ? 0 : rc;
8539      }else{
8540        open_db(&data, 0);
8541        rc = shell_exec(&data, azCmd[i], &zErrMsg);
8542        if( zErrMsg!=0 ){
8543          utf8_printf(stderr,"Error: %s\n", zErrMsg);
8544          return rc!=0 ? rc : 1;
8545        }else if( rc!=0 ){
8546          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
8547          return rc;
8548        }
8549      }
8550    }
8551    free(azCmd);
8552  }else{
8553    /* Run commands received from standard input
8554    */
8555    if( stdin_is_interactive ){
8556      char *zHome;
8557      char *zHistory = 0;
8558      int nHistory;
8559      printf(
8560        "SQLite version %s %.19s\n" /*extra-version-info*/
8561        "Enter \".help\" for usage hints.\n",
8562        sqlite3_libversion(), sqlite3_sourceid()
8563      );
8564      if( warnInmemoryDb ){
8565        printf("Connected to a ");
8566        printBold("transient in-memory database");
8567        printf(".\nUse \".open FILENAME\" to reopen on a "
8568               "persistent database.\n");
8569      }
8570      zHome = find_home_dir(0);
8571      if( zHome ){
8572        nHistory = strlen30(zHome) + 20;
8573        if( (zHistory = malloc(nHistory))!=0 ){
8574          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
8575        }
8576      }
8577      if( zHistory ){ shell_read_history(zHistory); }
8578#if HAVE_READLINE || HAVE_EDITLINE
8579      rl_attempted_completion_function = readline_completion;
8580#elif HAVE_LINENOISE
8581      linenoiseSetCompletionCallback(linenoise_completion);
8582#endif
8583      rc = process_input(&data, 0);
8584      if( zHistory ){
8585        shell_stifle_history(2000);
8586        shell_write_history(zHistory);
8587        free(zHistory);
8588      }
8589    }else{
8590      rc = process_input(&data, stdin);
8591    }
8592  }
8593  set_table_name(&data, 0);
8594  if( data.db ){
8595    session_close_all(&data);
8596    sqlite3_close(data.db);
8597  }
8598  sqlite3_free(data.zFreeOnClose);
8599  find_home_dir(1);
8600  output_reset(&data);
8601  data.doXdgOpen = 0;
8602  clearTempFile(&data);
8603#if !SQLITE_SHELL_IS_UTF8
8604  for(i=0; i<argc; i++) free(argv[i]);
8605  free(argv);
8606#endif
8607  return rc;
8608}
8609