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