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