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