1/* DO NOT EDIT!
2** This file is automatically generated by the script in the canonical
3** SQLite source tree at tool/mkshellc.tcl.  That script combines source
4** code from various constituent source files of SQLite into this single
5** "shell.c" file used to implement the SQLite command-line shell.
6**
7** Most of the code found below comes from the "src/shell.c.in" file in
8** the canonical SQLite source tree.  That main file contains "INCLUDE"
9** lines that specify other files in the canonical source tree that are
10** inserted to getnerate this complete program source file.
11**
12** The code from multiple files is combined into this single "shell.c"
13** source file to help make the command-line program easier to compile.
14**
15** To modify this program, get a copy of the canonical SQLite source tree,
16** edit the src/shell.c.in" and/or some of the other files that are included
17** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
18*/
19/*
20** 2001 September 15
21**
22** The author disclaims copyright to this source code.  In place of
23** a legal notice, here is a blessing:
24**
25**    May you do good and not evil.
26**    May you find forgiveness for yourself and forgive others.
27**    May you share freely, never taking more than you give.
28**
29*************************************************************************
30** This file contains code to implement the "sqlite" command line
31** utility for accessing SQLite databases.
32*/
33#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34/* This needs to come before any includes for MSVC compiler */
35#define _CRT_SECURE_NO_WARNINGS
36#endif
37
38/*
39** Warning pragmas copied from msvc.h in the core.
40*/
41#if defined(_MSC_VER)
42#pragma warning(disable : 4054)
43#pragma warning(disable : 4055)
44#pragma warning(disable : 4100)
45#pragma warning(disable : 4127)
46#pragma warning(disable : 4130)
47#pragma warning(disable : 4152)
48#pragma warning(disable : 4189)
49#pragma warning(disable : 4206)
50#pragma warning(disable : 4210)
51#pragma warning(disable : 4232)
52#pragma warning(disable : 4244)
53#pragma warning(disable : 4305)
54#pragma warning(disable : 4306)
55#pragma warning(disable : 4702)
56#pragma warning(disable : 4706)
57#endif /* defined(_MSC_VER) */
58
59/*
60** No support for loadable extensions in VxWorks.
61*/
62#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
63# define SQLITE_OMIT_LOAD_EXTENSION 1
64#endif
65
66/*
67** Enable large-file support for fopen() and friends on unix.
68*/
69#ifndef SQLITE_DISABLE_LFS
70# define _LARGE_FILE       1
71# ifndef _FILE_OFFSET_BITS
72#   define _FILE_OFFSET_BITS 64
73# endif
74# define _LARGEFILE_SOURCE 1
75#endif
76
77#include <stdlib.h>
78#include <string.h>
79#include <stdio.h>
80#include <assert.h>
81#include "sqlite3.h"
82typedef sqlite3_int64 i64;
83typedef sqlite3_uint64 u64;
84typedef unsigned char u8;
85#if SQLITE_USER_AUTHENTICATION
86# include "sqlite3userauth.h"
87#endif
88#include <ctype.h>
89#include <stdarg.h>
90
91#if !defined(_WIN32) && !defined(WIN32)
92# include <signal.h>
93# if !defined(__RTP__) && !defined(_WRS_KERNEL)
94#  include <pwd.h>
95# endif
96#endif
97#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
98# include <unistd.h>
99# include <dirent.h>
100# if defined(__MINGW32__)
101#  define DIRENT dirent
102#  ifndef S_ISLNK
103#   define S_ISLNK(mode) (0)
104#  endif
105# endif
106#endif
107#include <sys/types.h>
108#include <sys/stat.h>
109
110#if HAVE_READLINE
111# include <readline/readline.h>
112# include <readline/history.h>
113#endif
114
115#if HAVE_EDITLINE
116# include <editline/readline.h>
117#endif
118
119#if HAVE_EDITLINE || HAVE_READLINE
120
121# define shell_add_history(X) add_history(X)
122# define shell_read_history(X) read_history(X)
123# define shell_write_history(X) write_history(X)
124# define shell_stifle_history(X) stifle_history(X)
125# define shell_readline(X) readline(X)
126
127#elif HAVE_LINENOISE
128
129# include "linenoise.h"
130# define shell_add_history(X) linenoiseHistoryAdd(X)
131# define shell_read_history(X) linenoiseHistoryLoad(X)
132# define shell_write_history(X) linenoiseHistorySave(X)
133# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
134# define shell_readline(X) linenoise(X)
135
136#else
137
138# define shell_read_history(X)
139# define shell_write_history(X)
140# define shell_stifle_history(X)
141
142# define SHELL_USE_LOCAL_GETLINE 1
143#endif
144
145
146#if defined(_WIN32) || defined(WIN32)
147# include <io.h>
148# include <fcntl.h>
149# define isatty(h) _isatty(h)
150# ifndef access
151#  define access(f,m) _access((f),(m))
152# endif
153# undef popen
154# define popen _popen
155# undef pclose
156# define pclose _pclose
157#else
158 /* Make sure isatty() has a prototype. */
159 extern int isatty(int);
160
161# if !defined(__RTP__) && !defined(_WRS_KERNEL)
162  /* popen and pclose are not C89 functions and so are
163  ** sometimes omitted from the <stdio.h> header */
164   extern FILE *popen(const char*,const char*);
165   extern int pclose(FILE*);
166# else
167#  define SQLITE_OMIT_POPEN 1
168# endif
169#endif
170
171#if defined(_WIN32_WCE)
172/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
173 * thus we always assume that we have a console. That can be
174 * overridden with the -batch command line option.
175 */
176#define isatty(x) 1
177#endif
178
179/* ctype macros that work with signed characters */
180#define IsSpace(X)  isspace((unsigned char)X)
181#define IsDigit(X)  isdigit((unsigned char)X)
182#define ToLower(X)  (char)tolower((unsigned char)X)
183
184#if defined(_WIN32) || defined(WIN32)
185#include <windows.h>
186
187/* string conversion routines only needed on Win32 */
188extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
189extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
190extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
191extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
192#endif
193
194/* On Windows, we normally run with output mode of TEXT so that \n characters
195** are automatically translated into \r\n.  However, this behavior needs
196** to be disabled in some cases (ex: when generating CSV output and when
197** rendering quoted strings that contain \n characters).  The following
198** routines take care of that.
199*/
200#if defined(_WIN32) || defined(WIN32)
201static void setBinaryMode(FILE *file, int isOutput){
202  if( isOutput ) fflush(file);
203  _setmode(_fileno(file), _O_BINARY);
204}
205static void setTextMode(FILE *file, int isOutput){
206  if( isOutput ) fflush(file);
207  _setmode(_fileno(file), _O_TEXT);
208}
209#else
210# define setBinaryMode(X,Y)
211# define setTextMode(X,Y)
212#endif
213
214
215/* True if the timer is enabled */
216static int enableTimer = 0;
217
218/* Return the current wall-clock time */
219static sqlite3_int64 timeOfDay(void){
220  static sqlite3_vfs *clockVfs = 0;
221  sqlite3_int64 t;
222  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
223  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
224    clockVfs->xCurrentTimeInt64(clockVfs, &t);
225  }else{
226    double r;
227    clockVfs->xCurrentTime(clockVfs, &r);
228    t = (sqlite3_int64)(r*86400000.0);
229  }
230  return t;
231}
232
233#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
234#include <sys/time.h>
235#include <sys/resource.h>
236
237/* VxWorks does not support getrusage() as far as we can determine */
238#if defined(_WRS_KERNEL) || defined(__RTP__)
239struct rusage {
240  struct timeval ru_utime; /* user CPU time used */
241  struct timeval ru_stime; /* system CPU time used */
242};
243#define getrusage(A,B) memset(B,0,sizeof(*B))
244#endif
245
246/* Saved resource information for the beginning of an operation */
247static struct rusage sBegin;  /* CPU time at start */
248static sqlite3_int64 iBegin;  /* Wall-clock time at start */
249
250/*
251** Begin timing an operation
252*/
253static void beginTimer(void){
254  if( enableTimer ){
255    getrusage(RUSAGE_SELF, &sBegin);
256    iBegin = timeOfDay();
257  }
258}
259
260/* Return the difference of two time_structs in seconds */
261static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
262  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
263         (double)(pEnd->tv_sec - pStart->tv_sec);
264}
265
266/*
267** Print the timing results.
268*/
269static void endTimer(void){
270  if( enableTimer ){
271    sqlite3_int64 iEnd = timeOfDay();
272    struct rusage sEnd;
273    getrusage(RUSAGE_SELF, &sEnd);
274    printf("Run Time: real %.3f user %f sys %f\n",
275       (iEnd - iBegin)*0.001,
276       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
277       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
278  }
279}
280
281#define BEGIN_TIMER beginTimer()
282#define END_TIMER endTimer()
283#define HAS_TIMER 1
284
285#elif (defined(_WIN32) || defined(WIN32))
286
287/* Saved resource information for the beginning of an operation */
288static HANDLE hProcess;
289static FILETIME ftKernelBegin;
290static FILETIME ftUserBegin;
291static sqlite3_int64 ftWallBegin;
292typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
293                                    LPFILETIME, LPFILETIME);
294static GETPROCTIMES getProcessTimesAddr = NULL;
295
296/*
297** Check to see if we have timer support.  Return 1 if necessary
298** support found (or found previously).
299*/
300static int hasTimer(void){
301  if( getProcessTimesAddr ){
302    return 1;
303  } else {
304    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
305    ** versions. See if the version we are running on has it, and if it
306    ** does, save off a pointer to it and the current process handle.
307    */
308    hProcess = GetCurrentProcess();
309    if( hProcess ){
310      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
311      if( NULL != hinstLib ){
312        getProcessTimesAddr =
313            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
314        if( NULL != getProcessTimesAddr ){
315          return 1;
316        }
317        FreeLibrary(hinstLib);
318      }
319    }
320  }
321  return 0;
322}
323
324/*
325** Begin timing an operation
326*/
327static void beginTimer(void){
328  if( enableTimer && getProcessTimesAddr ){
329    FILETIME ftCreation, ftExit;
330    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
331                        &ftKernelBegin,&ftUserBegin);
332    ftWallBegin = timeOfDay();
333  }
334}
335
336/* Return the difference of two FILETIME structs in seconds */
337static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
338  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
339  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
340  return (double) ((i64End - i64Start) / 10000000.0);
341}
342
343/*
344** Print the timing results.
345*/
346static void endTimer(void){
347  if( enableTimer && getProcessTimesAddr){
348    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
349    sqlite3_int64 ftWallEnd = timeOfDay();
350    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
351    printf("Run Time: real %.3f user %f sys %f\n",
352       (ftWallEnd - ftWallBegin)*0.001,
353       timeDiff(&ftUserBegin, &ftUserEnd),
354       timeDiff(&ftKernelBegin, &ftKernelEnd));
355  }
356}
357
358#define BEGIN_TIMER beginTimer()
359#define END_TIMER endTimer()
360#define HAS_TIMER hasTimer()
361
362#else
363#define BEGIN_TIMER
364#define END_TIMER
365#define HAS_TIMER 0
366#endif
367
368/*
369** Used to prevent warnings about unused parameters
370*/
371#define UNUSED_PARAMETER(x) (void)(x)
372
373/*
374** Number of elements in an array
375*/
376#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
377
378/*
379** If the following flag is set, then command execution stops
380** at an error if we are not interactive.
381*/
382static int bail_on_error = 0;
383
384/*
385** Threat stdin as an interactive input if the following variable
386** is true.  Otherwise, assume stdin is connected to a file or pipe.
387*/
388static int stdin_is_interactive = 1;
389
390/*
391** On Windows systems we have to know if standard output is a console
392** in order to translate UTF-8 into MBCS.  The following variable is
393** true if translation is required.
394*/
395static int stdout_is_console = 1;
396
397/*
398** The following is the open SQLite database.  We make a pointer
399** to this database a static variable so that it can be accessed
400** by the SIGINT handler to interrupt database processing.
401*/
402static sqlite3 *globalDb = 0;
403
404/*
405** True if an interrupt (Control-C) has been received.
406*/
407static volatile int seenInterrupt = 0;
408
409/*
410** This is the name of our program. It is set in main(), used
411** in a number of other places, mostly for error messages.
412*/
413static char *Argv0;
414
415/*
416** Prompt strings. Initialized in main. Settable with
417**   .prompt main continue
418*/
419static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
420static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
421
422/*
423** Render output like fprintf().  Except, if the output is going to the
424** console and if this is running on a Windows machine, translate the
425** output from UTF-8 into MBCS.
426*/
427#if defined(_WIN32) || defined(WIN32)
428void utf8_printf(FILE *out, const char *zFormat, ...){
429  va_list ap;
430  va_start(ap, zFormat);
431  if( stdout_is_console && (out==stdout || out==stderr) ){
432    char *z1 = sqlite3_vmprintf(zFormat, ap);
433    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
434    sqlite3_free(z1);
435    fputs(z2, out);
436    sqlite3_free(z2);
437  }else{
438    vfprintf(out, zFormat, ap);
439  }
440  va_end(ap);
441}
442#elif !defined(utf8_printf)
443# define utf8_printf fprintf
444#endif
445
446/*
447** Render output like fprintf().  This should not be used on anything that
448** includes string formatting (e.g. "%s").
449*/
450#if !defined(raw_printf)
451# define raw_printf fprintf
452#endif
453
454/*
455** Write I/O traces to the following stream.
456*/
457#ifdef SQLITE_ENABLE_IOTRACE
458static FILE *iotrace = 0;
459#endif
460
461/*
462** This routine works like printf in that its first argument is a
463** format string and subsequent arguments are values to be substituted
464** in place of % fields.  The result of formatting this string
465** is written to iotrace.
466*/
467#ifdef SQLITE_ENABLE_IOTRACE
468static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
469  va_list ap;
470  char *z;
471  if( iotrace==0 ) return;
472  va_start(ap, zFormat);
473  z = sqlite3_vmprintf(zFormat, ap);
474  va_end(ap);
475  utf8_printf(iotrace, "%s", z);
476  sqlite3_free(z);
477}
478#endif
479
480/*
481** Output string zUtf to stream pOut as w characters.  If w is negative,
482** then right-justify the text.  W is the width in UTF-8 characters, not
483** in bytes.  This is different from the %*.*s specification in printf
484** since with %*.*s the width is measured in bytes, not characters.
485*/
486static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
487  int i;
488  int n;
489  int aw = w<0 ? -w : w;
490  char zBuf[1000];
491  if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
492  for(i=n=0; zUtf[i]; i++){
493    if( (zUtf[i]&0xc0)!=0x80 ){
494      n++;
495      if( n==aw ){
496        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
497        break;
498      }
499    }
500  }
501  if( n>=aw ){
502    utf8_printf(pOut, "%.*s", i, zUtf);
503  }else if( w<0 ){
504    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
505  }else{
506    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
507  }
508}
509
510
511/*
512** Determines if a string is a number of not.
513*/
514static int isNumber(const char *z, int *realnum){
515  if( *z=='-' || *z=='+' ) z++;
516  if( !IsDigit(*z) ){
517    return 0;
518  }
519  z++;
520  if( realnum ) *realnum = 0;
521  while( IsDigit(*z) ){ z++; }
522  if( *z=='.' ){
523    z++;
524    if( !IsDigit(*z) ) return 0;
525    while( IsDigit(*z) ){ z++; }
526    if( realnum ) *realnum = 1;
527  }
528  if( *z=='e' || *z=='E' ){
529    z++;
530    if( *z=='+' || *z=='-' ) z++;
531    if( !IsDigit(*z) ) return 0;
532    while( IsDigit(*z) ){ z++; }
533    if( realnum ) *realnum = 1;
534  }
535  return *z==0;
536}
537
538/*
539** Compute a string length that is limited to what can be stored in
540** lower 30 bits of a 32-bit signed integer.
541*/
542static int strlen30(const char *z){
543  const char *z2 = z;
544  while( *z2 ){ z2++; }
545  return 0x3fffffff & (int)(z2 - z);
546}
547
548/*
549** Return the length of a string in characters.  Multibyte UTF8 characters
550** count as a single character.
551*/
552static int strlenChar(const char *z){
553  int n = 0;
554  while( *z ){
555    if( (0xc0&*(z++))!=0x80 ) n++;
556  }
557  return n;
558}
559
560/*
561** This routine reads a line of text from FILE in, stores
562** the text in memory obtained from malloc() and returns a pointer
563** to the text.  NULL is returned at end of file, or if malloc()
564** fails.
565**
566** If zLine is not NULL then it is a malloced buffer returned from
567** a previous call to this routine that may be reused.
568*/
569static char *local_getline(char *zLine, FILE *in){
570  int nLine = zLine==0 ? 0 : 100;
571  int n = 0;
572
573  while( 1 ){
574    if( n+100>nLine ){
575      nLine = nLine*2 + 100;
576      zLine = realloc(zLine, nLine);
577      if( zLine==0 ) return 0;
578    }
579    if( fgets(&zLine[n], nLine - n, in)==0 ){
580      if( n==0 ){
581        free(zLine);
582        return 0;
583      }
584      zLine[n] = 0;
585      break;
586    }
587    while( zLine[n] ) n++;
588    if( n>0 && zLine[n-1]=='\n' ){
589      n--;
590      if( n>0 && zLine[n-1]=='\r' ) n--;
591      zLine[n] = 0;
592      break;
593    }
594  }
595#if defined(_WIN32) || defined(WIN32)
596  /* For interactive input on Windows systems, translate the
597  ** multi-byte characterset characters into UTF-8. */
598  if( stdin_is_interactive && in==stdin ){
599    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
600    if( zTrans ){
601      int nTrans = strlen30(zTrans)+1;
602      if( nTrans>nLine ){
603        zLine = realloc(zLine, nTrans);
604        if( zLine==0 ){
605          sqlite3_free(zTrans);
606          return 0;
607        }
608      }
609      memcpy(zLine, zTrans, nTrans);
610      sqlite3_free(zTrans);
611    }
612  }
613#endif /* defined(_WIN32) || defined(WIN32) */
614  return zLine;
615}
616
617/*
618** Retrieve a single line of input text.
619**
620** If in==0 then read from standard input and prompt before each line.
621** If isContinuation is true, then a continuation prompt is appropriate.
622** If isContinuation is zero, then the main prompt should be used.
623**
624** If zPrior is not NULL then it is a buffer from a prior call to this
625** routine that can be reused.
626**
627** The result is stored in space obtained from malloc() and must either
628** be freed by the caller or else passed back into this routine via the
629** zPrior argument for reuse.
630*/
631static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
632  char *zPrompt;
633  char *zResult;
634  if( in!=0 ){
635    zResult = local_getline(zPrior, in);
636  }else{
637    zPrompt = isContinuation ? continuePrompt : mainPrompt;
638#if SHELL_USE_LOCAL_GETLINE
639    printf("%s", zPrompt);
640    fflush(stdout);
641    zResult = local_getline(zPrior, stdin);
642#else
643    free(zPrior);
644    zResult = shell_readline(zPrompt);
645    if( zResult && *zResult ) shell_add_history(zResult);
646#endif
647  }
648  return zResult;
649}
650
651
652/*
653** Return the value of a hexadecimal digit.  Return -1 if the input
654** is not a hex digit.
655*/
656static int hexDigitValue(char c){
657  if( c>='0' && c<='9' ) return c - '0';
658  if( c>='a' && c<='f' ) return c - 'a' + 10;
659  if( c>='A' && c<='F' ) return c - 'A' + 10;
660  return -1;
661}
662
663/*
664** Interpret zArg as an integer value, possibly with suffixes.
665*/
666static sqlite3_int64 integerValue(const char *zArg){
667  sqlite3_int64 v = 0;
668  static const struct { char *zSuffix; int iMult; } aMult[] = {
669    { "KiB", 1024 },
670    { "MiB", 1024*1024 },
671    { "GiB", 1024*1024*1024 },
672    { "KB",  1000 },
673    { "MB",  1000000 },
674    { "GB",  1000000000 },
675    { "K",   1000 },
676    { "M",   1000000 },
677    { "G",   1000000000 },
678  };
679  int i;
680  int isNeg = 0;
681  if( zArg[0]=='-' ){
682    isNeg = 1;
683    zArg++;
684  }else if( zArg[0]=='+' ){
685    zArg++;
686  }
687  if( zArg[0]=='0' && zArg[1]=='x' ){
688    int x;
689    zArg += 2;
690    while( (x = hexDigitValue(zArg[0]))>=0 ){
691      v = (v<<4) + x;
692      zArg++;
693    }
694  }else{
695    while( IsDigit(zArg[0]) ){
696      v = v*10 + zArg[0] - '0';
697      zArg++;
698    }
699  }
700  for(i=0; i<ArraySize(aMult); i++){
701    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
702      v *= aMult[i].iMult;
703      break;
704    }
705  }
706  return isNeg? -v : v;
707}
708
709/*
710** A variable length string to which one can append text.
711*/
712typedef struct ShellText ShellText;
713struct ShellText {
714  char *z;
715  int n;
716  int nAlloc;
717};
718
719/*
720** Initialize and destroy a ShellText object
721*/
722static void initText(ShellText *p){
723  memset(p, 0, sizeof(*p));
724}
725static void freeText(ShellText *p){
726  free(p->z);
727  initText(p);
728}
729
730/* zIn is either a pointer to a NULL-terminated string in memory obtained
731** from malloc(), or a NULL pointer. The string pointed to by zAppend is
732** added to zIn, and the result returned in memory obtained from malloc().
733** zIn, if it was not NULL, is freed.
734**
735** If the third argument, quote, is not '\0', then it is used as a
736** quote character for zAppend.
737*/
738static void appendText(ShellText *p, char const *zAppend, char quote){
739  int len;
740  int i;
741  int nAppend = strlen30(zAppend);
742
743  len = nAppend+p->n+1;
744  if( quote ){
745    len += 2;
746    for(i=0; i<nAppend; i++){
747      if( zAppend[i]==quote ) len++;
748    }
749  }
750
751  if( p->n+len>=p->nAlloc ){
752    p->nAlloc = p->nAlloc*2 + len + 20;
753    p->z = realloc(p->z, p->nAlloc);
754    if( p->z==0 ){
755      memset(p, 0, sizeof(*p));
756      return;
757    }
758  }
759
760  if( quote ){
761    char *zCsr = p->z+p->n;
762    *zCsr++ = quote;
763    for(i=0; i<nAppend; i++){
764      *zCsr++ = zAppend[i];
765      if( zAppend[i]==quote ) *zCsr++ = quote;
766    }
767    *zCsr++ = quote;
768    p->n = (int)(zCsr - p->z);
769    *zCsr = '\0';
770  }else{
771    memcpy(p->z+p->n, zAppend, nAppend);
772    p->n += nAppend;
773    p->z[p->n] = '\0';
774  }
775}
776
777/*
778** Attempt to determine if identifier zName needs to be quoted, either
779** because it contains non-alphanumeric characters, or because it is an
780** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
781** that quoting is required.
782**
783** Return '"' if quoting is required.  Return 0 if no quoting is required.
784*/
785static char quoteChar(const char *zName){
786  /* All SQLite keywords, in alphabetical order */
787  static const char *azKeywords[] = {
788    "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
789    "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
790    "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
791    "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
792    "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
793    "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
794    "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
795    "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
796    "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
797    "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
798    "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
799    "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
800    "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
801    "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
802    "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
803    "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
804    "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
805    "WITH", "WITHOUT",
806  };
807  int i, lwr, upr, mid, c;
808  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
809  for(i=0; zName[i]; i++){
810    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
811  }
812  lwr = 0;
813  upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
814  while( lwr<=upr ){
815    mid = (lwr+upr)/2;
816    c = sqlite3_stricmp(azKeywords[mid], zName);
817    if( c==0 ) return '"';
818    if( c<0 ){
819      lwr = mid+1;
820    }else{
821      upr = mid-1;
822    }
823  }
824  return 0;
825}
826
827/*
828** Construct a fake object name and column list to describe the structure
829** of the view, virtual table, or table valued function zSchema.zName.
830*/
831static char *shellFakeSchema(
832  sqlite3 *db,            /* The database connection containing the vtab */
833  const char *zSchema,    /* Schema of the database holding the vtab */
834  const char *zName       /* The name of the virtual table */
835){
836  sqlite3_stmt *pStmt = 0;
837  char *zSql;
838  ShellText s;
839  char cQuote;
840  char *zDiv = "(";
841  int nRow = 0;
842
843  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
844                         zSchema ? zSchema : "main", zName);
845  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
846  sqlite3_free(zSql);
847  initText(&s);
848  if( zSchema ){
849    cQuote = quoteChar(zSchema);
850    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
851    appendText(&s, zSchema, cQuote);
852    appendText(&s, ".", 0);
853  }
854  cQuote = quoteChar(zName);
855  appendText(&s, zName, cQuote);
856  while( sqlite3_step(pStmt)==SQLITE_ROW ){
857    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
858    nRow++;
859    appendText(&s, zDiv, 0);
860    zDiv = ",";
861    cQuote = quoteChar(zCol);
862    appendText(&s, zCol, cQuote);
863  }
864  appendText(&s, ")", 0);
865  sqlite3_finalize(pStmt);
866  if( nRow==0 ){
867    freeText(&s);
868    s.z = 0;
869  }
870  return s.z;
871}
872
873/*
874** SQL function:  shell_module_schema(X)
875**
876** Return a fake schema for the table-valued function or eponymous virtual
877** table X.
878*/
879static void shellModuleSchema(
880  sqlite3_context *pCtx,
881  int nVal,
882  sqlite3_value **apVal
883){
884  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
885  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
886  UNUSED_PARAMETER(nVal);
887  if( zFake ){
888    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
889                        -1, sqlite3_free);
890    free(zFake);
891  }
892}
893
894/*
895** SQL function:  shell_add_schema(S,X)
896**
897** Add the schema name X to the CREATE statement in S and return the result.
898** Examples:
899**
900**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
901**
902** Also works on
903**
904**    CREATE INDEX
905**    CREATE UNIQUE INDEX
906**    CREATE VIEW
907**    CREATE TRIGGER
908**    CREATE VIRTUAL TABLE
909**
910** This UDF is used by the .schema command to insert the schema name of
911** attached databases into the middle of the sqlite_master.sql field.
912*/
913static void shellAddSchemaName(
914  sqlite3_context *pCtx,
915  int nVal,
916  sqlite3_value **apVal
917){
918  static const char *aPrefix[] = {
919     "TABLE",
920     "INDEX",
921     "UNIQUE INDEX",
922     "VIEW",
923     "TRIGGER",
924     "VIRTUAL TABLE"
925  };
926  int i = 0;
927  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
928  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
929  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
930  sqlite3 *db = sqlite3_context_db_handle(pCtx);
931  UNUSED_PARAMETER(nVal);
932  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
933    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
934      int n = strlen30(aPrefix[i]);
935      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
936        char *z = 0;
937        char *zFake = 0;
938        if( zSchema ){
939          char cQuote = quoteChar(zSchema);
940          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
941            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
942          }else{
943            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
944          }
945        }
946        if( zName
947         && aPrefix[i][0]=='V'
948         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
949        ){
950          if( z==0 ){
951            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
952          }else{
953            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
954          }
955          free(zFake);
956        }
957        if( z ){
958          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
959          return;
960        }
961      }
962    }
963  }
964  sqlite3_result_value(pCtx, apVal[0]);
965}
966
967/*
968** The source code for several run-time loadable extensions is inserted
969** below by the ../tool/mkshellc.tcl script.  Before processing that included
970** code, we need to override some macros to make the included program code
971** work here in the middle of this regular program.
972*/
973#define SQLITE_EXTENSION_INIT1
974#define SQLITE_EXTENSION_INIT2(X) (void)(X)
975
976#if defined(_WIN32) && defined(_MSC_VER)
977/************************* Begin test_windirent.h ******************/
978/*
979** 2015 November 30
980**
981** The author disclaims copyright to this source code.  In place of
982** a legal notice, here is a blessing:
983**
984**    May you do good and not evil.
985**    May you find forgiveness for yourself and forgive others.
986**    May you share freely, never taking more than you give.
987**
988*************************************************************************
989** This file contains declarations for most of the opendir() family of
990** POSIX functions on Win32 using the MSVCRT.
991*/
992
993#if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
994#define SQLITE_WINDIRENT_H
995
996/*
997** We need several data types from the Windows SDK header.
998*/
999
1000#define WIN32_LEAN_AND_MEAN
1001#include "windows.h"
1002
1003/*
1004** We need several support functions from the SQLite core.
1005*/
1006
1007
1008/*
1009** We need several things from the ANSI and MSVCRT headers.
1010*/
1011
1012#include <stdio.h>
1013#include <stdlib.h>
1014#include <errno.h>
1015#include <io.h>
1016#include <limits.h>
1017#include <sys/types.h>
1018#include <sys/stat.h>
1019
1020/*
1021** We may need several defines that should have been in "sys/stat.h".
1022*/
1023
1024#ifndef S_ISREG
1025#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1026#endif
1027
1028#ifndef S_ISDIR
1029#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1030#endif
1031
1032#ifndef S_ISLNK
1033#define S_ISLNK(mode) (0)
1034#endif
1035
1036/*
1037** We may need to provide the "mode_t" type.
1038*/
1039
1040#ifndef MODE_T_DEFINED
1041  #define MODE_T_DEFINED
1042  typedef unsigned short mode_t;
1043#endif
1044
1045/*
1046** We may need to provide the "ino_t" type.
1047*/
1048
1049#ifndef INO_T_DEFINED
1050  #define INO_T_DEFINED
1051  typedef unsigned short ino_t;
1052#endif
1053
1054/*
1055** We need to define "NAME_MAX" if it was not present in "limits.h".
1056*/
1057
1058#ifndef NAME_MAX
1059#  ifdef FILENAME_MAX
1060#    define NAME_MAX (FILENAME_MAX)
1061#  else
1062#    define NAME_MAX (260)
1063#  endif
1064#endif
1065
1066/*
1067** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1068*/
1069
1070#ifndef NULL_INTPTR_T
1071#  define NULL_INTPTR_T ((intptr_t)(0))
1072#endif
1073
1074#ifndef BAD_INTPTR_T
1075#  define BAD_INTPTR_T ((intptr_t)(-1))
1076#endif
1077
1078/*
1079** We need to provide the necessary structures and related types.
1080*/
1081
1082#ifndef DIRENT_DEFINED
1083#define DIRENT_DEFINED
1084typedef struct DIRENT DIRENT;
1085typedef DIRENT *LPDIRENT;
1086struct DIRENT {
1087  ino_t d_ino;               /* Sequence number, do not use. */
1088  unsigned d_attributes;     /* Win32 file attributes. */
1089  char d_name[NAME_MAX + 1]; /* Name within the directory. */
1090};
1091#endif
1092
1093#ifndef DIR_DEFINED
1094#define DIR_DEFINED
1095typedef struct DIR DIR;
1096typedef DIR *LPDIR;
1097struct DIR {
1098  intptr_t d_handle; /* Value returned by "_findfirst". */
1099  DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1100  DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1101};
1102#endif
1103
1104/*
1105** Provide a macro, for use by the implementation, to determine if a
1106** particular directory entry should be skipped over when searching for
1107** the next directory entry that should be returned by the readdir() or
1108** readdir_r() functions.
1109*/
1110
1111#ifndef is_filtered
1112#  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1113#endif
1114
1115/*
1116** Provide the function prototype for the POSIX compatiable getenv()
1117** function.  This function is not thread-safe.
1118*/
1119
1120extern const char *windirent_getenv(const char *name);
1121
1122/*
1123** Finally, we can provide the function prototypes for the opendir(),
1124** readdir(), readdir_r(), and closedir() POSIX functions.
1125*/
1126
1127extern LPDIR opendir(const char *dirname);
1128extern LPDIRENT readdir(LPDIR dirp);
1129extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1130extern INT closedir(LPDIR dirp);
1131
1132#endif /* defined(WIN32) && defined(_MSC_VER) */
1133
1134/************************* End test_windirent.h ********************/
1135/************************* Begin test_windirent.c ******************/
1136/*
1137** 2015 November 30
1138**
1139** The author disclaims copyright to this source code.  In place of
1140** a legal notice, here is a blessing:
1141**
1142**    May you do good and not evil.
1143**    May you find forgiveness for yourself and forgive others.
1144**    May you share freely, never taking more than you give.
1145**
1146*************************************************************************
1147** This file contains code to implement most of the opendir() family of
1148** POSIX functions on Win32 using the MSVCRT.
1149*/
1150
1151#if defined(_WIN32) && defined(_MSC_VER)
1152/* #include "test_windirent.h" */
1153
1154/*
1155** Implementation of the POSIX getenv() function using the Win32 API.
1156** This function is not thread-safe.
1157*/
1158const char *windirent_getenv(
1159  const char *name
1160){
1161  static char value[32768]; /* Maximum length, per MSDN */
1162  DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1163  DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1164
1165  memset(value, 0, sizeof(value));
1166  dwRet = GetEnvironmentVariableA(name, value, dwSize);
1167  if( dwRet==0 || dwRet>dwSize ){
1168    /*
1169    ** The function call to GetEnvironmentVariableA() failed -OR-
1170    ** the buffer is not large enough.  Either way, return NULL.
1171    */
1172    return 0;
1173  }else{
1174    /*
1175    ** The function call to GetEnvironmentVariableA() succeeded
1176    ** -AND- the buffer contains the entire value.
1177    */
1178    return value;
1179  }
1180}
1181
1182/*
1183** Implementation of the POSIX opendir() function using the MSVCRT.
1184*/
1185LPDIR opendir(
1186  const char *dirname
1187){
1188  struct _finddata_t data;
1189  LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1190  SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1191
1192  if( dirp==NULL ) return NULL;
1193  memset(dirp, 0, sizeof(DIR));
1194
1195  /* TODO: Remove this if Unix-style root paths are not used. */
1196  if( sqlite3_stricmp(dirname, "/")==0 ){
1197    dirname = windirent_getenv("SystemDrive");
1198  }
1199
1200  memset(&data, 0, sizeof(struct _finddata_t));
1201  _snprintf(data.name, namesize, "%s\\*", dirname);
1202  dirp->d_handle = _findfirst(data.name, &data);
1203
1204  if( dirp->d_handle==BAD_INTPTR_T ){
1205    closedir(dirp);
1206    return NULL;
1207  }
1208
1209  /* TODO: Remove this block to allow hidden and/or system files. */
1210  if( is_filtered(data) ){
1211next:
1212
1213    memset(&data, 0, sizeof(struct _finddata_t));
1214    if( _findnext(dirp->d_handle, &data)==-1 ){
1215      closedir(dirp);
1216      return NULL;
1217    }
1218
1219    /* TODO: Remove this block to allow hidden and/or system files. */
1220    if( is_filtered(data) ) goto next;
1221  }
1222
1223  dirp->d_first.d_attributes = data.attrib;
1224  strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1225  dirp->d_first.d_name[NAME_MAX] = '\0';
1226
1227  return dirp;
1228}
1229
1230/*
1231** Implementation of the POSIX readdir() function using the MSVCRT.
1232*/
1233LPDIRENT readdir(
1234  LPDIR dirp
1235){
1236  struct _finddata_t data;
1237
1238  if( dirp==NULL ) return NULL;
1239
1240  if( dirp->d_first.d_ino==0 ){
1241    dirp->d_first.d_ino++;
1242    dirp->d_next.d_ino++;
1243
1244    return &dirp->d_first;
1245  }
1246
1247next:
1248
1249  memset(&data, 0, sizeof(struct _finddata_t));
1250  if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1251
1252  /* TODO: Remove this block to allow hidden and/or system files. */
1253  if( is_filtered(data) ) goto next;
1254
1255  dirp->d_next.d_ino++;
1256  dirp->d_next.d_attributes = data.attrib;
1257  strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1258  dirp->d_next.d_name[NAME_MAX] = '\0';
1259
1260  return &dirp->d_next;
1261}
1262
1263/*
1264** Implementation of the POSIX readdir_r() function using the MSVCRT.
1265*/
1266INT readdir_r(
1267  LPDIR dirp,
1268  LPDIRENT entry,
1269  LPDIRENT *result
1270){
1271  struct _finddata_t data;
1272
1273  if( dirp==NULL ) return EBADF;
1274
1275  if( dirp->d_first.d_ino==0 ){
1276    dirp->d_first.d_ino++;
1277    dirp->d_next.d_ino++;
1278
1279    entry->d_ino = dirp->d_first.d_ino;
1280    entry->d_attributes = dirp->d_first.d_attributes;
1281    strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1282    entry->d_name[NAME_MAX] = '\0';
1283
1284    *result = entry;
1285    return 0;
1286  }
1287
1288next:
1289
1290  memset(&data, 0, sizeof(struct _finddata_t));
1291  if( _findnext(dirp->d_handle, &data)==-1 ){
1292    *result = NULL;
1293    return ENOENT;
1294  }
1295
1296  /* TODO: Remove this block to allow hidden and/or system files. */
1297  if( is_filtered(data) ) goto next;
1298
1299  entry->d_ino = (ino_t)-1; /* not available */
1300  entry->d_attributes = data.attrib;
1301  strncpy(entry->d_name, data.name, NAME_MAX);
1302  entry->d_name[NAME_MAX] = '\0';
1303
1304  *result = entry;
1305  return 0;
1306}
1307
1308/*
1309** Implementation of the POSIX closedir() function using the MSVCRT.
1310*/
1311INT closedir(
1312  LPDIR dirp
1313){
1314  INT result = 0;
1315
1316  if( dirp==NULL ) return EINVAL;
1317
1318  if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1319    result = _findclose(dirp->d_handle);
1320  }
1321
1322  sqlite3_free(dirp);
1323  return result;
1324}
1325
1326#endif /* defined(WIN32) && defined(_MSC_VER) */
1327
1328/************************* End test_windirent.c ********************/
1329#define dirent DIRENT
1330#endif
1331/************************* Begin ../ext/misc/shathree.c ******************/
1332/*
1333** 2017-03-08
1334**
1335** The author disclaims copyright to this source code.  In place of
1336** a legal notice, here is a blessing:
1337**
1338**    May you do good and not evil.
1339**    May you find forgiveness for yourself and forgive others.
1340**    May you share freely, never taking more than you give.
1341**
1342******************************************************************************
1343**
1344** This SQLite extension implements a functions that compute SHA1 hashes.
1345** Two SQL functions are implemented:
1346**
1347**     sha3(X,SIZE)
1348**     sha3_query(Y,SIZE)
1349**
1350** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1351** X is NULL.
1352**
1353** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1354** and returns a hash of their results.
1355**
1356** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1357** is used.  If SIZE is included it must be one of the integers 224, 256,
1358** 384, or 512, to determine SHA3 hash variant that is computed.
1359*/
1360SQLITE_EXTENSION_INIT1
1361#include <assert.h>
1362#include <string.h>
1363#include <stdarg.h>
1364/* typedef sqlite3_uint64 u64; */
1365
1366/******************************************************************************
1367** The Hash Engine
1368*/
1369/*
1370** Macros to determine whether the machine is big or little endian,
1371** and whether or not that determination is run-time or compile-time.
1372**
1373** For best performance, an attempt is made to guess at the byte-order
1374** using C-preprocessor macros.  If that is unsuccessful, or if
1375** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1376** at run-time.
1377*/
1378#ifndef SHA3_BYTEORDER
1379# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1380     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1381     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1382     defined(__arm__)
1383#   define SHA3_BYTEORDER    1234
1384# elif defined(sparc)    || defined(__ppc__)
1385#   define SHA3_BYTEORDER    4321
1386# else
1387#   define SHA3_BYTEORDER 0
1388# endif
1389#endif
1390
1391
1392/*
1393** State structure for a SHA3 hash in progress
1394*/
1395typedef struct SHA3Context SHA3Context;
1396struct SHA3Context {
1397  union {
1398    u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1399    unsigned char x[1600];    /* ... or 1600 bytes */
1400  } u;
1401  unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1402  unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1403  unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1404};
1405
1406/*
1407** A single step of the Keccak mixing function for a 1600-bit state
1408*/
1409static void KeccakF1600Step(SHA3Context *p){
1410  int i;
1411  u64 b0, b1, b2, b3, b4;
1412  u64 c0, c1, c2, c3, c4;
1413  u64 d0, d1, d2, d3, d4;
1414  static const u64 RC[] = {
1415    0x0000000000000001ULL,  0x0000000000008082ULL,
1416    0x800000000000808aULL,  0x8000000080008000ULL,
1417    0x000000000000808bULL,  0x0000000080000001ULL,
1418    0x8000000080008081ULL,  0x8000000000008009ULL,
1419    0x000000000000008aULL,  0x0000000000000088ULL,
1420    0x0000000080008009ULL,  0x000000008000000aULL,
1421    0x000000008000808bULL,  0x800000000000008bULL,
1422    0x8000000000008089ULL,  0x8000000000008003ULL,
1423    0x8000000000008002ULL,  0x8000000000000080ULL,
1424    0x000000000000800aULL,  0x800000008000000aULL,
1425    0x8000000080008081ULL,  0x8000000000008080ULL,
1426    0x0000000080000001ULL,  0x8000000080008008ULL
1427  };
1428# define a00 (p->u.s[0])
1429# define a01 (p->u.s[1])
1430# define a02 (p->u.s[2])
1431# define a03 (p->u.s[3])
1432# define a04 (p->u.s[4])
1433# define a10 (p->u.s[5])
1434# define a11 (p->u.s[6])
1435# define a12 (p->u.s[7])
1436# define a13 (p->u.s[8])
1437# define a14 (p->u.s[9])
1438# define a20 (p->u.s[10])
1439# define a21 (p->u.s[11])
1440# define a22 (p->u.s[12])
1441# define a23 (p->u.s[13])
1442# define a24 (p->u.s[14])
1443# define a30 (p->u.s[15])
1444# define a31 (p->u.s[16])
1445# define a32 (p->u.s[17])
1446# define a33 (p->u.s[18])
1447# define a34 (p->u.s[19])
1448# define a40 (p->u.s[20])
1449# define a41 (p->u.s[21])
1450# define a42 (p->u.s[22])
1451# define a43 (p->u.s[23])
1452# define a44 (p->u.s[24])
1453# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1454
1455  for(i=0; i<24; i+=4){
1456    c0 = a00^a10^a20^a30^a40;
1457    c1 = a01^a11^a21^a31^a41;
1458    c2 = a02^a12^a22^a32^a42;
1459    c3 = a03^a13^a23^a33^a43;
1460    c4 = a04^a14^a24^a34^a44;
1461    d0 = c4^ROL64(c1, 1);
1462    d1 = c0^ROL64(c2, 1);
1463    d2 = c1^ROL64(c3, 1);
1464    d3 = c2^ROL64(c4, 1);
1465    d4 = c3^ROL64(c0, 1);
1466
1467    b0 = (a00^d0);
1468    b1 = ROL64((a11^d1), 44);
1469    b2 = ROL64((a22^d2), 43);
1470    b3 = ROL64((a33^d3), 21);
1471    b4 = ROL64((a44^d4), 14);
1472    a00 =   b0 ^((~b1)&  b2 );
1473    a00 ^= RC[i];
1474    a11 =   b1 ^((~b2)&  b3 );
1475    a22 =   b2 ^((~b3)&  b4 );
1476    a33 =   b3 ^((~b4)&  b0 );
1477    a44 =   b4 ^((~b0)&  b1 );
1478
1479    b2 = ROL64((a20^d0), 3);
1480    b3 = ROL64((a31^d1), 45);
1481    b4 = ROL64((a42^d2), 61);
1482    b0 = ROL64((a03^d3), 28);
1483    b1 = ROL64((a14^d4), 20);
1484    a20 =   b0 ^((~b1)&  b2 );
1485    a31 =   b1 ^((~b2)&  b3 );
1486    a42 =   b2 ^((~b3)&  b4 );
1487    a03 =   b3 ^((~b4)&  b0 );
1488    a14 =   b4 ^((~b0)&  b1 );
1489
1490    b4 = ROL64((a40^d0), 18);
1491    b0 = ROL64((a01^d1), 1);
1492    b1 = ROL64((a12^d2), 6);
1493    b2 = ROL64((a23^d3), 25);
1494    b3 = ROL64((a34^d4), 8);
1495    a40 =   b0 ^((~b1)&  b2 );
1496    a01 =   b1 ^((~b2)&  b3 );
1497    a12 =   b2 ^((~b3)&  b4 );
1498    a23 =   b3 ^((~b4)&  b0 );
1499    a34 =   b4 ^((~b0)&  b1 );
1500
1501    b1 = ROL64((a10^d0), 36);
1502    b2 = ROL64((a21^d1), 10);
1503    b3 = ROL64((a32^d2), 15);
1504    b4 = ROL64((a43^d3), 56);
1505    b0 = ROL64((a04^d4), 27);
1506    a10 =   b0 ^((~b1)&  b2 );
1507    a21 =   b1 ^((~b2)&  b3 );
1508    a32 =   b2 ^((~b3)&  b4 );
1509    a43 =   b3 ^((~b4)&  b0 );
1510    a04 =   b4 ^((~b0)&  b1 );
1511
1512    b3 = ROL64((a30^d0), 41);
1513    b4 = ROL64((a41^d1), 2);
1514    b0 = ROL64((a02^d2), 62);
1515    b1 = ROL64((a13^d3), 55);
1516    b2 = ROL64((a24^d4), 39);
1517    a30 =   b0 ^((~b1)&  b2 );
1518    a41 =   b1 ^((~b2)&  b3 );
1519    a02 =   b2 ^((~b3)&  b4 );
1520    a13 =   b3 ^((~b4)&  b0 );
1521    a24 =   b4 ^((~b0)&  b1 );
1522
1523    c0 = a00^a20^a40^a10^a30;
1524    c1 = a11^a31^a01^a21^a41;
1525    c2 = a22^a42^a12^a32^a02;
1526    c3 = a33^a03^a23^a43^a13;
1527    c4 = a44^a14^a34^a04^a24;
1528    d0 = c4^ROL64(c1, 1);
1529    d1 = c0^ROL64(c2, 1);
1530    d2 = c1^ROL64(c3, 1);
1531    d3 = c2^ROL64(c4, 1);
1532    d4 = c3^ROL64(c0, 1);
1533
1534    b0 = (a00^d0);
1535    b1 = ROL64((a31^d1), 44);
1536    b2 = ROL64((a12^d2), 43);
1537    b3 = ROL64((a43^d3), 21);
1538    b4 = ROL64((a24^d4), 14);
1539    a00 =   b0 ^((~b1)&  b2 );
1540    a00 ^= RC[i+1];
1541    a31 =   b1 ^((~b2)&  b3 );
1542    a12 =   b2 ^((~b3)&  b4 );
1543    a43 =   b3 ^((~b4)&  b0 );
1544    a24 =   b4 ^((~b0)&  b1 );
1545
1546    b2 = ROL64((a40^d0), 3);
1547    b3 = ROL64((a21^d1), 45);
1548    b4 = ROL64((a02^d2), 61);
1549    b0 = ROL64((a33^d3), 28);
1550    b1 = ROL64((a14^d4), 20);
1551    a40 =   b0 ^((~b1)&  b2 );
1552    a21 =   b1 ^((~b2)&  b3 );
1553    a02 =   b2 ^((~b3)&  b4 );
1554    a33 =   b3 ^((~b4)&  b0 );
1555    a14 =   b4 ^((~b0)&  b1 );
1556
1557    b4 = ROL64((a30^d0), 18);
1558    b0 = ROL64((a11^d1), 1);
1559    b1 = ROL64((a42^d2), 6);
1560    b2 = ROL64((a23^d3), 25);
1561    b3 = ROL64((a04^d4), 8);
1562    a30 =   b0 ^((~b1)&  b2 );
1563    a11 =   b1 ^((~b2)&  b3 );
1564    a42 =   b2 ^((~b3)&  b4 );
1565    a23 =   b3 ^((~b4)&  b0 );
1566    a04 =   b4 ^((~b0)&  b1 );
1567
1568    b1 = ROL64((a20^d0), 36);
1569    b2 = ROL64((a01^d1), 10);
1570    b3 = ROL64((a32^d2), 15);
1571    b4 = ROL64((a13^d3), 56);
1572    b0 = ROL64((a44^d4), 27);
1573    a20 =   b0 ^((~b1)&  b2 );
1574    a01 =   b1 ^((~b2)&  b3 );
1575    a32 =   b2 ^((~b3)&  b4 );
1576    a13 =   b3 ^((~b4)&  b0 );
1577    a44 =   b4 ^((~b0)&  b1 );
1578
1579    b3 = ROL64((a10^d0), 41);
1580    b4 = ROL64((a41^d1), 2);
1581    b0 = ROL64((a22^d2), 62);
1582    b1 = ROL64((a03^d3), 55);
1583    b2 = ROL64((a34^d4), 39);
1584    a10 =   b0 ^((~b1)&  b2 );
1585    a41 =   b1 ^((~b2)&  b3 );
1586    a22 =   b2 ^((~b3)&  b4 );
1587    a03 =   b3 ^((~b4)&  b0 );
1588    a34 =   b4 ^((~b0)&  b1 );
1589
1590    c0 = a00^a40^a30^a20^a10;
1591    c1 = a31^a21^a11^a01^a41;
1592    c2 = a12^a02^a42^a32^a22;
1593    c3 = a43^a33^a23^a13^a03;
1594    c4 = a24^a14^a04^a44^a34;
1595    d0 = c4^ROL64(c1, 1);
1596    d1 = c0^ROL64(c2, 1);
1597    d2 = c1^ROL64(c3, 1);
1598    d3 = c2^ROL64(c4, 1);
1599    d4 = c3^ROL64(c0, 1);
1600
1601    b0 = (a00^d0);
1602    b1 = ROL64((a21^d1), 44);
1603    b2 = ROL64((a42^d2), 43);
1604    b3 = ROL64((a13^d3), 21);
1605    b4 = ROL64((a34^d4), 14);
1606    a00 =   b0 ^((~b1)&  b2 );
1607    a00 ^= RC[i+2];
1608    a21 =   b1 ^((~b2)&  b3 );
1609    a42 =   b2 ^((~b3)&  b4 );
1610    a13 =   b3 ^((~b4)&  b0 );
1611    a34 =   b4 ^((~b0)&  b1 );
1612
1613    b2 = ROL64((a30^d0), 3);
1614    b3 = ROL64((a01^d1), 45);
1615    b4 = ROL64((a22^d2), 61);
1616    b0 = ROL64((a43^d3), 28);
1617    b1 = ROL64((a14^d4), 20);
1618    a30 =   b0 ^((~b1)&  b2 );
1619    a01 =   b1 ^((~b2)&  b3 );
1620    a22 =   b2 ^((~b3)&  b4 );
1621    a43 =   b3 ^((~b4)&  b0 );
1622    a14 =   b4 ^((~b0)&  b1 );
1623
1624    b4 = ROL64((a10^d0), 18);
1625    b0 = ROL64((a31^d1), 1);
1626    b1 = ROL64((a02^d2), 6);
1627    b2 = ROL64((a23^d3), 25);
1628    b3 = ROL64((a44^d4), 8);
1629    a10 =   b0 ^((~b1)&  b2 );
1630    a31 =   b1 ^((~b2)&  b3 );
1631    a02 =   b2 ^((~b3)&  b4 );
1632    a23 =   b3 ^((~b4)&  b0 );
1633    a44 =   b4 ^((~b0)&  b1 );
1634
1635    b1 = ROL64((a40^d0), 36);
1636    b2 = ROL64((a11^d1), 10);
1637    b3 = ROL64((a32^d2), 15);
1638    b4 = ROL64((a03^d3), 56);
1639    b0 = ROL64((a24^d4), 27);
1640    a40 =   b0 ^((~b1)&  b2 );
1641    a11 =   b1 ^((~b2)&  b3 );
1642    a32 =   b2 ^((~b3)&  b4 );
1643    a03 =   b3 ^((~b4)&  b0 );
1644    a24 =   b4 ^((~b0)&  b1 );
1645
1646    b3 = ROL64((a20^d0), 41);
1647    b4 = ROL64((a41^d1), 2);
1648    b0 = ROL64((a12^d2), 62);
1649    b1 = ROL64((a33^d3), 55);
1650    b2 = ROL64((a04^d4), 39);
1651    a20 =   b0 ^((~b1)&  b2 );
1652    a41 =   b1 ^((~b2)&  b3 );
1653    a12 =   b2 ^((~b3)&  b4 );
1654    a33 =   b3 ^((~b4)&  b0 );
1655    a04 =   b4 ^((~b0)&  b1 );
1656
1657    c0 = a00^a30^a10^a40^a20;
1658    c1 = a21^a01^a31^a11^a41;
1659    c2 = a42^a22^a02^a32^a12;
1660    c3 = a13^a43^a23^a03^a33;
1661    c4 = a34^a14^a44^a24^a04;
1662    d0 = c4^ROL64(c1, 1);
1663    d1 = c0^ROL64(c2, 1);
1664    d2 = c1^ROL64(c3, 1);
1665    d3 = c2^ROL64(c4, 1);
1666    d4 = c3^ROL64(c0, 1);
1667
1668    b0 = (a00^d0);
1669    b1 = ROL64((a01^d1), 44);
1670    b2 = ROL64((a02^d2), 43);
1671    b3 = ROL64((a03^d3), 21);
1672    b4 = ROL64((a04^d4), 14);
1673    a00 =   b0 ^((~b1)&  b2 );
1674    a00 ^= RC[i+3];
1675    a01 =   b1 ^((~b2)&  b3 );
1676    a02 =   b2 ^((~b3)&  b4 );
1677    a03 =   b3 ^((~b4)&  b0 );
1678    a04 =   b4 ^((~b0)&  b1 );
1679
1680    b2 = ROL64((a10^d0), 3);
1681    b3 = ROL64((a11^d1), 45);
1682    b4 = ROL64((a12^d2), 61);
1683    b0 = ROL64((a13^d3), 28);
1684    b1 = ROL64((a14^d4), 20);
1685    a10 =   b0 ^((~b1)&  b2 );
1686    a11 =   b1 ^((~b2)&  b3 );
1687    a12 =   b2 ^((~b3)&  b4 );
1688    a13 =   b3 ^((~b4)&  b0 );
1689    a14 =   b4 ^((~b0)&  b1 );
1690
1691    b4 = ROL64((a20^d0), 18);
1692    b0 = ROL64((a21^d1), 1);
1693    b1 = ROL64((a22^d2), 6);
1694    b2 = ROL64((a23^d3), 25);
1695    b3 = ROL64((a24^d4), 8);
1696    a20 =   b0 ^((~b1)&  b2 );
1697    a21 =   b1 ^((~b2)&  b3 );
1698    a22 =   b2 ^((~b3)&  b4 );
1699    a23 =   b3 ^((~b4)&  b0 );
1700    a24 =   b4 ^((~b0)&  b1 );
1701
1702    b1 = ROL64((a30^d0), 36);
1703    b2 = ROL64((a31^d1), 10);
1704    b3 = ROL64((a32^d2), 15);
1705    b4 = ROL64((a33^d3), 56);
1706    b0 = ROL64((a34^d4), 27);
1707    a30 =   b0 ^((~b1)&  b2 );
1708    a31 =   b1 ^((~b2)&  b3 );
1709    a32 =   b2 ^((~b3)&  b4 );
1710    a33 =   b3 ^((~b4)&  b0 );
1711    a34 =   b4 ^((~b0)&  b1 );
1712
1713    b3 = ROL64((a40^d0), 41);
1714    b4 = ROL64((a41^d1), 2);
1715    b0 = ROL64((a42^d2), 62);
1716    b1 = ROL64((a43^d3), 55);
1717    b2 = ROL64((a44^d4), 39);
1718    a40 =   b0 ^((~b1)&  b2 );
1719    a41 =   b1 ^((~b2)&  b3 );
1720    a42 =   b2 ^((~b3)&  b4 );
1721    a43 =   b3 ^((~b4)&  b0 );
1722    a44 =   b4 ^((~b0)&  b1 );
1723  }
1724}
1725
1726/*
1727** Initialize a new hash.  iSize determines the size of the hash
1728** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1729** can be zero to use the default hash size of 256 bits.
1730*/
1731static void SHA3Init(SHA3Context *p, int iSize){
1732  memset(p, 0, sizeof(*p));
1733  if( iSize>=128 && iSize<=512 ){
1734    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1735  }else{
1736    p->nRate = (1600 - 2*256)/8;
1737  }
1738#if SHA3_BYTEORDER==1234
1739  /* Known to be little-endian at compile-time. No-op */
1740#elif SHA3_BYTEORDER==4321
1741  p->ixMask = 7;  /* Big-endian */
1742#else
1743  {
1744    static unsigned int one = 1;
1745    if( 1==*(unsigned char*)&one ){
1746      /* Little endian.  No byte swapping. */
1747      p->ixMask = 0;
1748    }else{
1749      /* Big endian.  Byte swap. */
1750      p->ixMask = 7;
1751    }
1752  }
1753#endif
1754}
1755
1756/*
1757** Make consecutive calls to the SHA3Update function to add new content
1758** to the hash
1759*/
1760static void SHA3Update(
1761  SHA3Context *p,
1762  const unsigned char *aData,
1763  unsigned int nData
1764){
1765  unsigned int i = 0;
1766#if SHA3_BYTEORDER==1234
1767  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1768    for(; i+7<nData; i+=8){
1769      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1770      p->nLoaded += 8;
1771      if( p->nLoaded>=p->nRate ){
1772        KeccakF1600Step(p);
1773        p->nLoaded = 0;
1774      }
1775    }
1776  }
1777#endif
1778  for(; i<nData; i++){
1779#if SHA3_BYTEORDER==1234
1780    p->u.x[p->nLoaded] ^= aData[i];
1781#elif SHA3_BYTEORDER==4321
1782    p->u.x[p->nLoaded^0x07] ^= aData[i];
1783#else
1784    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1785#endif
1786    p->nLoaded++;
1787    if( p->nLoaded==p->nRate ){
1788      KeccakF1600Step(p);
1789      p->nLoaded = 0;
1790    }
1791  }
1792}
1793
1794/*
1795** After all content has been added, invoke SHA3Final() to compute
1796** the final hash.  The function returns a pointer to the binary
1797** hash value.
1798*/
1799static unsigned char *SHA3Final(SHA3Context *p){
1800  unsigned int i;
1801  if( p->nLoaded==p->nRate-1 ){
1802    const unsigned char c1 = 0x86;
1803    SHA3Update(p, &c1, 1);
1804  }else{
1805    const unsigned char c2 = 0x06;
1806    const unsigned char c3 = 0x80;
1807    SHA3Update(p, &c2, 1);
1808    p->nLoaded = p->nRate - 1;
1809    SHA3Update(p, &c3, 1);
1810  }
1811  for(i=0; i<p->nRate; i++){
1812    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1813  }
1814  return &p->u.x[p->nRate];
1815}
1816/* End of the hashing logic
1817*****************************************************************************/
1818
1819/*
1820** Implementation of the sha3(X,SIZE) function.
1821**
1822** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1823** size is 256.  If X is a BLOB, it is hashed as is.
1824** For all other non-NULL types of input, X is converted into a UTF-8 string
1825** and the string is hashed without the trailing 0x00 terminator.  The hash
1826** of a NULL value is NULL.
1827*/
1828static void sha3Func(
1829  sqlite3_context *context,
1830  int argc,
1831  sqlite3_value **argv
1832){
1833  SHA3Context cx;
1834  int eType = sqlite3_value_type(argv[0]);
1835  int nByte = sqlite3_value_bytes(argv[0]);
1836  int iSize;
1837  if( argc==1 ){
1838    iSize = 256;
1839  }else{
1840    iSize = sqlite3_value_int(argv[1]);
1841    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1842      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1843                                    "384 512", -1);
1844      return;
1845    }
1846  }
1847  if( eType==SQLITE_NULL ) return;
1848  SHA3Init(&cx, iSize);
1849  if( eType==SQLITE_BLOB ){
1850    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1851  }else{
1852    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1853  }
1854  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1855}
1856
1857/* Compute a string using sqlite3_vsnprintf() with a maximum length
1858** of 50 bytes and add it to the hash.
1859*/
1860static void hash_step_vformat(
1861  SHA3Context *p,                 /* Add content to this context */
1862  const char *zFormat,
1863  ...
1864){
1865  va_list ap;
1866  int n;
1867  char zBuf[50];
1868  va_start(ap, zFormat);
1869  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1870  va_end(ap);
1871  n = (int)strlen(zBuf);
1872  SHA3Update(p, (unsigned char*)zBuf, n);
1873}
1874
1875/*
1876** Implementation of the sha3_query(SQL,SIZE) function.
1877**
1878** This function compiles and runs the SQL statement(s) given in the
1879** argument. The results are hashed using a SIZE-bit SHA3.  The default
1880** size is 256.
1881**
1882** The format of the byte stream that is hashed is summarized as follows:
1883**
1884**       S<n>:<sql>
1885**       R
1886**       N
1887**       I<int>
1888**       F<ieee-float>
1889**       B<size>:<bytes>
1890**       T<size>:<text>
1891**
1892** <sql> is the original SQL text for each statement run and <n> is
1893** the size of that text.  The SQL text is UTF-8.  A single R character
1894** occurs before the start of each row.  N means a NULL value.
1895** I mean an 8-byte little-endian integer <int>.  F is a floating point
1896** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1897** B means blobs of <size> bytes.  T means text rendered as <size>
1898** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1899** text integers.
1900**
1901** For each SQL statement in the X input, there is one S segment.  Each
1902** S segment is followed by zero or more R segments, one for each row in the
1903** result set.  After each R, there are one or more N, I, F, B, or T segments,
1904** one for each column in the result set.  Segments are concatentated directly
1905** with no delimiters of any kind.
1906*/
1907static void sha3QueryFunc(
1908  sqlite3_context *context,
1909  int argc,
1910  sqlite3_value **argv
1911){
1912  sqlite3 *db = sqlite3_context_db_handle(context);
1913  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1914  sqlite3_stmt *pStmt = 0;
1915  int nCol;                   /* Number of columns in the result set */
1916  int i;                      /* Loop counter */
1917  int rc;
1918  int n;
1919  const char *z;
1920  SHA3Context cx;
1921  int iSize;
1922
1923  if( argc==1 ){
1924    iSize = 256;
1925  }else{
1926    iSize = sqlite3_value_int(argv[1]);
1927    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1928      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1929                                    "384 512", -1);
1930      return;
1931    }
1932  }
1933  if( zSql==0 ) return;
1934  SHA3Init(&cx, iSize);
1935  while( zSql[0] ){
1936    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1937    if( rc ){
1938      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1939                                   zSql, sqlite3_errmsg(db));
1940      sqlite3_finalize(pStmt);
1941      sqlite3_result_error(context, zMsg, -1);
1942      sqlite3_free(zMsg);
1943      return;
1944    }
1945    if( !sqlite3_stmt_readonly(pStmt) ){
1946      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1947      sqlite3_finalize(pStmt);
1948      sqlite3_result_error(context, zMsg, -1);
1949      sqlite3_free(zMsg);
1950      return;
1951    }
1952    nCol = sqlite3_column_count(pStmt);
1953    z = sqlite3_sql(pStmt);
1954    n = (int)strlen(z);
1955    hash_step_vformat(&cx,"S%d:",n);
1956    SHA3Update(&cx,(unsigned char*)z,n);
1957
1958    /* Compute a hash over the result of the query */
1959    while( SQLITE_ROW==sqlite3_step(pStmt) ){
1960      SHA3Update(&cx,(const unsigned char*)"R",1);
1961      for(i=0; i<nCol; i++){
1962        switch( sqlite3_column_type(pStmt,i) ){
1963          case SQLITE_NULL: {
1964            SHA3Update(&cx, (const unsigned char*)"N",1);
1965            break;
1966          }
1967          case SQLITE_INTEGER: {
1968            sqlite3_uint64 u;
1969            int j;
1970            unsigned char x[9];
1971            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1972            memcpy(&u, &v, 8);
1973            for(j=8; j>=1; j--){
1974              x[j] = u & 0xff;
1975              u >>= 8;
1976            }
1977            x[0] = 'I';
1978            SHA3Update(&cx, x, 9);
1979            break;
1980          }
1981          case SQLITE_FLOAT: {
1982            sqlite3_uint64 u;
1983            int j;
1984            unsigned char x[9];
1985            double r = sqlite3_column_double(pStmt,i);
1986            memcpy(&u, &r, 8);
1987            for(j=8; j>=1; j--){
1988              x[j] = u & 0xff;
1989              u >>= 8;
1990            }
1991            x[0] = 'F';
1992            SHA3Update(&cx,x,9);
1993            break;
1994          }
1995          case SQLITE_TEXT: {
1996            int n2 = sqlite3_column_bytes(pStmt, i);
1997            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1998            hash_step_vformat(&cx,"T%d:",n2);
1999            SHA3Update(&cx, z2, n2);
2000            break;
2001          }
2002          case SQLITE_BLOB: {
2003            int n2 = sqlite3_column_bytes(pStmt, i);
2004            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2005            hash_step_vformat(&cx,"B%d:",n2);
2006            SHA3Update(&cx, z2, n2);
2007            break;
2008          }
2009        }
2010      }
2011    }
2012    sqlite3_finalize(pStmt);
2013  }
2014  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2015}
2016
2017
2018#ifdef _WIN32
2019
2020#endif
2021int sqlite3_shathree_init(
2022  sqlite3 *db,
2023  char **pzErrMsg,
2024  const sqlite3_api_routines *pApi
2025){
2026  int rc = SQLITE_OK;
2027  SQLITE_EXTENSION_INIT2(pApi);
2028  (void)pzErrMsg;  /* Unused parameter */
2029  rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2030                               sha3Func, 0, 0);
2031  if( rc==SQLITE_OK ){
2032    rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2033                                 sha3Func, 0, 0);
2034  }
2035  if( rc==SQLITE_OK ){
2036    rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2037                                 sha3QueryFunc, 0, 0);
2038  }
2039  if( rc==SQLITE_OK ){
2040    rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2041                                 sha3QueryFunc, 0, 0);
2042  }
2043  return rc;
2044}
2045
2046/************************* End ../ext/misc/shathree.c ********************/
2047/************************* Begin ../ext/misc/fileio.c ******************/
2048/*
2049** 2014-06-13
2050**
2051** The author disclaims copyright to this source code.  In place of
2052** a legal notice, here is a blessing:
2053**
2054**    May you do good and not evil.
2055**    May you find forgiveness for yourself and forgive others.
2056**    May you share freely, never taking more than you give.
2057**
2058******************************************************************************
2059**
2060** This SQLite extension implements SQL functions readfile() and
2061** writefile(), and eponymous virtual type "fsdir".
2062**
2063** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2064**
2065**   If neither of the optional arguments is present, then this UDF
2066**   function writes blob DATA to file FILE. If successful, the number
2067**   of bytes written is returned. If an error occurs, NULL is returned.
2068**
2069**   If the first option argument - MODE - is present, then it must
2070**   be passed an integer value that corresponds to a POSIX mode
2071**   value (file type + permissions, as returned in the stat.st_mode
2072**   field by the stat() system call). Three types of files may
2073**   be written/created:
2074**
2075**     regular files:  (mode & 0170000)==0100000
2076**     symbolic links: (mode & 0170000)==0120000
2077**     directories:    (mode & 0170000)==0040000
2078**
2079**   For a directory, the DATA is ignored. For a symbolic link, it is
2080**   interpreted as text and used as the target of the link. For a
2081**   regular file, it is interpreted as a blob and written into the
2082**   named file. Regardless of the type of file, its permissions are
2083**   set to (mode & 0777) before returning.
2084**
2085**   If the optional MTIME argument is present, then it is interpreted
2086**   as an integer - the number of seconds since the unix epoch. The
2087**   modification-time of the target file is set to this value before
2088**   returning.
2089**
2090**   If three or more arguments are passed to this function and an
2091**   error is encountered, an exception is raised.
2092**
2093** READFILE(FILE):
2094**
2095**   Read and return the contents of file FILE (type blob) from disk.
2096**
2097** FSDIR:
2098**
2099**   Used as follows:
2100**
2101**     SELECT * FROM fsdir($path [, $dir]);
2102**
2103**   Parameter $path is an absolute or relative pathname. If the file that it
2104**   refers to does not exist, it is an error. If the path refers to a regular
2105**   file or symbolic link, it returns a single row. Or, if the path refers
2106**   to a directory, it returns one row for the directory, and one row for each
2107**   file within the hierarchy rooted at $path.
2108**
2109**   Each row has the following columns:
2110**
2111**     name:  Path to file or directory (text value).
2112**     mode:  Value of stat.st_mode for directory entry (an integer).
2113**     mtime: Value of stat.st_mtime for directory entry (an integer).
2114**     data:  For a regular file, a blob containing the file data. For a
2115**            symlink, a text value containing the text of the link. For a
2116**            directory, NULL.
2117**
2118**   If a non-NULL value is specified for the optional $dir parameter and
2119**   $path is a relative path, then $path is interpreted relative to $dir.
2120**   And the paths returned in the "name" column of the table are also
2121**   relative to directory $dir.
2122*/
2123SQLITE_EXTENSION_INIT1
2124#include <stdio.h>
2125#include <string.h>
2126#include <assert.h>
2127
2128#include <sys/types.h>
2129#include <sys/stat.h>
2130#include <fcntl.h>
2131#if !defined(_WIN32) && !defined(WIN32)
2132#  include <unistd.h>
2133#  include <dirent.h>
2134#  include <utime.h>
2135#  include <sys/time.h>
2136#else
2137#  include "windows.h"
2138#  include <io.h>
2139#  include <direct.h>
2140/* #  include "test_windirent.h" */
2141#  define dirent DIRENT
2142#  ifndef stat
2143#    define stat _stat
2144#  endif
2145#  define mkdir(path,mode) _mkdir(path)
2146#  define lstat(path,buf) stat(path,buf)
2147#endif
2148#include <time.h>
2149#include <errno.h>
2150
2151
2152#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2153
2154/*
2155** Set the result stored by context ctx to a blob containing the
2156** contents of file zName.
2157*/
2158static void readFileContents(sqlite3_context *ctx, const char *zName){
2159  FILE *in;
2160  long nIn;
2161  void *pBuf;
2162
2163  in = fopen(zName, "rb");
2164  if( in==0 ) return;
2165  fseek(in, 0, SEEK_END);
2166  nIn = ftell(in);
2167  rewind(in);
2168  pBuf = sqlite3_malloc( nIn );
2169  if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
2170    sqlite3_result_blob(ctx, pBuf, nIn, sqlite3_free);
2171  }else{
2172    sqlite3_free(pBuf);
2173  }
2174  fclose(in);
2175}
2176
2177/*
2178** Implementation of the "readfile(X)" SQL function.  The entire content
2179** of the file named X is read and returned as a BLOB.  NULL is returned
2180** if the file does not exist or is unreadable.
2181*/
2182static void readfileFunc(
2183  sqlite3_context *context,
2184  int argc,
2185  sqlite3_value **argv
2186){
2187  const char *zName;
2188  (void)(argc);  /* Unused parameter */
2189  zName = (const char*)sqlite3_value_text(argv[0]);
2190  if( zName==0 ) return;
2191  readFileContents(context, zName);
2192}
2193
2194/*
2195** Set the error message contained in context ctx to the results of
2196** vprintf(zFmt, ...).
2197*/
2198static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2199  char *zMsg = 0;
2200  va_list ap;
2201  va_start(ap, zFmt);
2202  zMsg = sqlite3_vmprintf(zFmt, ap);
2203  sqlite3_result_error(ctx, zMsg, -1);
2204  sqlite3_free(zMsg);
2205  va_end(ap);
2206}
2207
2208/*
2209** Argument zFile is the name of a file that will be created and/or written
2210** by SQL function writefile(). This function ensures that the directory
2211** zFile will be written to exists, creating it if required. The permissions
2212** for any path components created by this function are set to (mode&0777).
2213**
2214** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2215** SQLITE_OK is returned if the directory is successfully created, or
2216** SQLITE_ERROR otherwise.
2217*/
2218static int makeDirectory(
2219  const char *zFile,
2220  mode_t mode
2221){
2222  char *zCopy = sqlite3_mprintf("%s", zFile);
2223  int rc = SQLITE_OK;
2224
2225  if( zCopy==0 ){
2226    rc = SQLITE_NOMEM;
2227  }else{
2228    int nCopy = (int)strlen(zCopy);
2229    int i = 1;
2230
2231    while( rc==SQLITE_OK ){
2232      struct stat sStat;
2233      int rc2;
2234
2235      for(; zCopy[i]!='/' && i<nCopy; i++);
2236      if( i==nCopy ) break;
2237      zCopy[i] = '\0';
2238
2239      rc2 = stat(zCopy, &sStat);
2240      if( rc2!=0 ){
2241        if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
2242      }else{
2243        if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2244      }
2245      zCopy[i] = '/';
2246      i++;
2247    }
2248
2249    sqlite3_free(zCopy);
2250  }
2251
2252  return rc;
2253}
2254
2255/*
2256** This function does the work for the writefile() UDF. Refer to
2257** header comments at the top of this file for details.
2258*/
2259static int writeFile(
2260  sqlite3_context *pCtx,          /* Context to return bytes written in */
2261  const char *zFile,              /* File to write */
2262  sqlite3_value *pData,           /* Data to write */
2263  mode_t mode,                    /* MODE parameter passed to writefile() */
2264  sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2265){
2266#if !defined(_WIN32) && !defined(WIN32)
2267  if( S_ISLNK(mode) ){
2268    const char *zTo = (const char*)sqlite3_value_text(pData);
2269    if( symlink(zTo, zFile)<0 ) return 1;
2270  }else
2271#endif
2272  {
2273    if( S_ISDIR(mode) ){
2274      if( mkdir(zFile, mode) ){
2275        /* The mkdir() call to create the directory failed. This might not
2276        ** be an error though - if there is already a directory at the same
2277        ** path and either the permissions already match or can be changed
2278        ** to do so using chmod(), it is not an error.  */
2279        struct stat sStat;
2280        if( errno!=EEXIST
2281         || 0!=stat(zFile, &sStat)
2282         || !S_ISDIR(sStat.st_mode)
2283         || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2284        ){
2285          return 1;
2286        }
2287      }
2288    }else{
2289      sqlite3_int64 nWrite = 0;
2290      const char *z;
2291      int rc = 0;
2292      FILE *out = fopen(zFile, "wb");
2293      if( out==0 ) return 1;
2294      z = (const char*)sqlite3_value_blob(pData);
2295      if( z ){
2296        sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2297        nWrite = sqlite3_value_bytes(pData);
2298        if( nWrite!=n ){
2299          rc = 1;
2300        }
2301      }
2302      fclose(out);
2303      if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2304        rc = 1;
2305      }
2306      if( rc ) return 2;
2307      sqlite3_result_int64(pCtx, nWrite);
2308    }
2309  }
2310
2311  if( mtime>=0 ){
2312#if defined(_WIN32)
2313    /* Windows */
2314    FILETIME lastAccess;
2315    FILETIME lastWrite;
2316    SYSTEMTIME currentTime;
2317    LONGLONG intervals;
2318    HANDLE hFile;
2319    GetSystemTime(&currentTime);
2320    SystemTimeToFileTime(&currentTime, &lastAccess);
2321    intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2322    lastWrite.dwLowDateTime = (DWORD)intervals;
2323    lastWrite.dwHighDateTime = intervals >> 32;
2324    hFile = CreateFile(
2325      zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2326      FILE_FLAG_BACKUP_SEMANTICS, NULL
2327    );
2328    if( hFile!=INVALID_HANDLE_VALUE ){
2329      BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2330      CloseHandle(hFile);
2331      return !bResult;
2332    }else{
2333      return 1;
2334    }
2335#elif defined(AT_FDCWD) && 0 /* utimensat() is not univerally available */
2336    /* Recent unix */
2337    struct timespec times[2];
2338    times[0].tv_nsec = times[1].tv_nsec = 0;
2339    times[0].tv_sec = time(0);
2340    times[1].tv_sec = mtime;
2341    if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2342      return 1;
2343    }
2344#else
2345    /* Legacy unix */
2346    struct timeval times[2];
2347    times[0].tv_usec = times[1].tv_usec = 0;
2348    times[0].tv_sec = time(0);
2349    times[1].tv_sec = mtime;
2350    if( utimes(zFile, times) ){
2351      return 1;
2352    }
2353#endif
2354  }
2355
2356  return 0;
2357}
2358
2359/*
2360** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2361** Refer to header comments at the top of this file for details.
2362*/
2363static void writefileFunc(
2364  sqlite3_context *context,
2365  int argc,
2366  sqlite3_value **argv
2367){
2368  const char *zFile;
2369  mode_t mode = 0;
2370  int res;
2371  sqlite3_int64 mtime = -1;
2372
2373  if( argc<2 || argc>4 ){
2374    sqlite3_result_error(context,
2375        "wrong number of arguments to function writefile()", -1
2376    );
2377    return;
2378  }
2379
2380  zFile = (const char*)sqlite3_value_text(argv[0]);
2381  if( zFile==0 ) return;
2382  if( argc>=3 ){
2383    mode = (mode_t)sqlite3_value_int(argv[2]);
2384  }
2385  if( argc==4 ){
2386    mtime = sqlite3_value_int64(argv[3]);
2387  }
2388
2389  res = writeFile(context, zFile, argv[1], mode, mtime);
2390  if( res==1 && errno==ENOENT ){
2391    if( makeDirectory(zFile, mode)==SQLITE_OK ){
2392      res = writeFile(context, zFile, argv[1], mode, mtime);
2393    }
2394  }
2395
2396  if( argc>2 && res!=0 ){
2397    if( S_ISLNK(mode) ){
2398      ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2399    }else if( S_ISDIR(mode) ){
2400      ctxErrorMsg(context, "failed to create directory: %s", zFile);
2401    }else{
2402      ctxErrorMsg(context, "failed to write file: %s", zFile);
2403    }
2404  }
2405}
2406
2407/*
2408** SQL function:   lsmode(MODE)
2409**
2410** Given a numberic st_mode from stat(), convert it into a human-readable
2411** text string in the style of "ls -l".
2412*/
2413static void lsModeFunc(
2414  sqlite3_context *context,
2415  int argc,
2416  sqlite3_value **argv
2417){
2418  int i;
2419  int iMode = sqlite3_value_int(argv[0]);
2420  char z[16];
2421  (void)argc;
2422  if( S_ISLNK(iMode) ){
2423    z[0] = 'l';
2424  }else if( S_ISREG(iMode) ){
2425    z[0] = '-';
2426  }else if( S_ISDIR(iMode) ){
2427    z[0] = 'd';
2428  }else{
2429    z[0] = '?';
2430  }
2431  for(i=0; i<3; i++){
2432    int m = (iMode >> ((2-i)*3));
2433    char *a = &z[1 + i*3];
2434    a[0] = (m & 0x4) ? 'r' : '-';
2435    a[1] = (m & 0x2) ? 'w' : '-';
2436    a[2] = (m & 0x1) ? 'x' : '-';
2437  }
2438  z[10] = '\0';
2439  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2440}
2441
2442#ifndef SQLITE_OMIT_VIRTUALTABLE
2443
2444/*
2445** Cursor type for recursively iterating through a directory structure.
2446*/
2447typedef struct fsdir_cursor fsdir_cursor;
2448typedef struct FsdirLevel FsdirLevel;
2449
2450struct FsdirLevel {
2451  DIR *pDir;                 /* From opendir() */
2452  char *zDir;                /* Name of directory (nul-terminated) */
2453};
2454
2455struct fsdir_cursor {
2456  sqlite3_vtab_cursor base;  /* Base class - must be first */
2457
2458  int nLvl;                  /* Number of entries in aLvl[] array */
2459  int iLvl;                  /* Index of current entry */
2460  FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2461
2462  const char *zBase;
2463  int nBase;
2464
2465  struct stat sStat;         /* Current lstat() results */
2466  char *zPath;               /* Path to current entry */
2467  sqlite3_int64 iRowid;      /* Current rowid */
2468};
2469
2470typedef struct fsdir_tab fsdir_tab;
2471struct fsdir_tab {
2472  sqlite3_vtab base;         /* Base class - must be first */
2473};
2474
2475/*
2476** Construct a new fsdir virtual table object.
2477*/
2478static int fsdirConnect(
2479  sqlite3 *db,
2480  void *pAux,
2481  int argc, const char *const*argv,
2482  sqlite3_vtab **ppVtab,
2483  char **pzErr
2484){
2485  fsdir_tab *pNew = 0;
2486  int rc;
2487  (void)pAux;
2488  (void)argc;
2489  (void)argv;
2490  (void)pzErr;
2491  rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2492  if( rc==SQLITE_OK ){
2493    pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2494    if( pNew==0 ) return SQLITE_NOMEM;
2495    memset(pNew, 0, sizeof(*pNew));
2496  }
2497  *ppVtab = (sqlite3_vtab*)pNew;
2498  return rc;
2499}
2500
2501/*
2502** This method is the destructor for fsdir vtab objects.
2503*/
2504static int fsdirDisconnect(sqlite3_vtab *pVtab){
2505  sqlite3_free(pVtab);
2506  return SQLITE_OK;
2507}
2508
2509/*
2510** Constructor for a new fsdir_cursor object.
2511*/
2512static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2513  fsdir_cursor *pCur;
2514  (void)p;
2515  pCur = sqlite3_malloc( sizeof(*pCur) );
2516  if( pCur==0 ) return SQLITE_NOMEM;
2517  memset(pCur, 0, sizeof(*pCur));
2518  pCur->iLvl = -1;
2519  *ppCursor = &pCur->base;
2520  return SQLITE_OK;
2521}
2522
2523/*
2524** Reset a cursor back to the state it was in when first returned
2525** by fsdirOpen().
2526*/
2527static void fsdirResetCursor(fsdir_cursor *pCur){
2528  int i;
2529  for(i=0; i<=pCur->iLvl; i++){
2530    FsdirLevel *pLvl = &pCur->aLvl[i];
2531    if( pLvl->pDir ) closedir(pLvl->pDir);
2532    sqlite3_free(pLvl->zDir);
2533  }
2534  sqlite3_free(pCur->zPath);
2535  pCur->aLvl = 0;
2536  pCur->zPath = 0;
2537  pCur->zBase = 0;
2538  pCur->nBase = 0;
2539  pCur->iLvl = -1;
2540  pCur->iRowid = 1;
2541}
2542
2543/*
2544** Destructor for an fsdir_cursor.
2545*/
2546static int fsdirClose(sqlite3_vtab_cursor *cur){
2547  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2548
2549  fsdirResetCursor(pCur);
2550  sqlite3_free(pCur->aLvl);
2551  sqlite3_free(pCur);
2552  return SQLITE_OK;
2553}
2554
2555/*
2556** Set the error message for the virtual table associated with cursor
2557** pCur to the results of vprintf(zFmt, ...).
2558*/
2559static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2560  va_list ap;
2561  va_start(ap, zFmt);
2562  pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2563  va_end(ap);
2564}
2565
2566
2567/*
2568** Advance an fsdir_cursor to its next row of output.
2569*/
2570static int fsdirNext(sqlite3_vtab_cursor *cur){
2571  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2572  mode_t m = pCur->sStat.st_mode;
2573
2574  pCur->iRowid++;
2575  if( S_ISDIR(m) ){
2576    /* Descend into this directory */
2577    int iNew = pCur->iLvl + 1;
2578    FsdirLevel *pLvl;
2579    if( iNew>=pCur->nLvl ){
2580      int nNew = iNew+1;
2581      int nByte = nNew*sizeof(FsdirLevel);
2582      FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc(pCur->aLvl, nByte);
2583      if( aNew==0 ) return SQLITE_NOMEM;
2584      memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2585      pCur->aLvl = aNew;
2586      pCur->nLvl = nNew;
2587    }
2588    pCur->iLvl = iNew;
2589    pLvl = &pCur->aLvl[iNew];
2590
2591    pLvl->zDir = pCur->zPath;
2592    pCur->zPath = 0;
2593    pLvl->pDir = opendir(pLvl->zDir);
2594    if( pLvl->pDir==0 ){
2595      fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2596      return SQLITE_ERROR;
2597    }
2598  }
2599
2600  while( pCur->iLvl>=0 ){
2601    FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2602    struct dirent *pEntry = readdir(pLvl->pDir);
2603    if( pEntry ){
2604      if( pEntry->d_name[0]=='.' ){
2605       if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2606       if( pEntry->d_name[1]=='\0' ) continue;
2607      }
2608      sqlite3_free(pCur->zPath);
2609      pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2610      if( pCur->zPath==0 ) return SQLITE_NOMEM;
2611      if( lstat(pCur->zPath, &pCur->sStat) ){
2612        fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2613        return SQLITE_ERROR;
2614      }
2615      return SQLITE_OK;
2616    }
2617    closedir(pLvl->pDir);
2618    sqlite3_free(pLvl->zDir);
2619    pLvl->pDir = 0;
2620    pLvl->zDir = 0;
2621    pCur->iLvl--;
2622  }
2623
2624  /* EOF */
2625  sqlite3_free(pCur->zPath);
2626  pCur->zPath = 0;
2627  return SQLITE_OK;
2628}
2629
2630/*
2631** Return values of columns for the row at which the series_cursor
2632** is currently pointing.
2633*/
2634static int fsdirColumn(
2635  sqlite3_vtab_cursor *cur,   /* The cursor */
2636  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2637  int i                       /* Which column to return */
2638){
2639  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2640  switch( i ){
2641    case 0: { /* name */
2642      sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2643      break;
2644    }
2645
2646    case 1: /* mode */
2647      sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2648      break;
2649
2650    case 2: /* mtime */
2651      sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2652      break;
2653
2654    case 3: { /* data */
2655      mode_t m = pCur->sStat.st_mode;
2656      if( S_ISDIR(m) ){
2657        sqlite3_result_null(ctx);
2658#if !defined(_WIN32) && !defined(WIN32)
2659      }else if( S_ISLNK(m) ){
2660        char aStatic[64];
2661        char *aBuf = aStatic;
2662        int nBuf = 64;
2663        int n;
2664
2665        while( 1 ){
2666          n = readlink(pCur->zPath, aBuf, nBuf);
2667          if( n<nBuf ) break;
2668          if( aBuf!=aStatic ) sqlite3_free(aBuf);
2669          nBuf = nBuf*2;
2670          aBuf = sqlite3_malloc(nBuf);
2671          if( aBuf==0 ){
2672            sqlite3_result_error_nomem(ctx);
2673            return SQLITE_NOMEM;
2674          }
2675        }
2676
2677        sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2678        if( aBuf!=aStatic ) sqlite3_free(aBuf);
2679#endif
2680      }else{
2681        readFileContents(ctx, pCur->zPath);
2682      }
2683    }
2684  }
2685  return SQLITE_OK;
2686}
2687
2688/*
2689** Return the rowid for the current row. In this implementation, the
2690** first row returned is assigned rowid value 1, and each subsequent
2691** row a value 1 more than that of the previous.
2692*/
2693static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2694  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2695  *pRowid = pCur->iRowid;
2696  return SQLITE_OK;
2697}
2698
2699/*
2700** Return TRUE if the cursor has been moved off of the last
2701** row of output.
2702*/
2703static int fsdirEof(sqlite3_vtab_cursor *cur){
2704  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2705  return (pCur->zPath==0);
2706}
2707
2708/*
2709** xFilter callback.
2710*/
2711static int fsdirFilter(
2712  sqlite3_vtab_cursor *cur,
2713  int idxNum, const char *idxStr,
2714  int argc, sqlite3_value **argv
2715){
2716  const char *zDir = 0;
2717  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2718  (void)idxStr;
2719  fsdirResetCursor(pCur);
2720
2721  if( idxNum==0 ){
2722    fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2723    return SQLITE_ERROR;
2724  }
2725
2726  assert( argc==idxNum && (argc==1 || argc==2) );
2727  zDir = (const char*)sqlite3_value_text(argv[0]);
2728  if( zDir==0 ){
2729    fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2730    return SQLITE_ERROR;
2731  }
2732  if( argc==2 ){
2733    pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2734  }
2735  if( pCur->zBase ){
2736    pCur->nBase = (int)strlen(pCur->zBase)+1;
2737    pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2738  }else{
2739    pCur->zPath = sqlite3_mprintf("%s", zDir);
2740  }
2741
2742  if( pCur->zPath==0 ){
2743    return SQLITE_NOMEM;
2744  }
2745  if( lstat(pCur->zPath, &pCur->sStat) ){
2746    fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2747    return SQLITE_ERROR;
2748  }
2749
2750  return SQLITE_OK;
2751}
2752
2753/*
2754** SQLite will invoke this method one or more times while planning a query
2755** that uses the generate_series virtual table.  This routine needs to create
2756** a query plan for each invocation and compute an estimated cost for that
2757** plan.
2758**
2759** In this implementation idxNum is used to represent the
2760** query plan.  idxStr is unused.
2761**
2762** The query plan is represented by bits in idxNum:
2763**
2764**  (1)  start = $value  -- constraint exists
2765**  (2)  stop = $value   -- constraint exists
2766**  (4)  step = $value   -- constraint exists
2767**  (8)  output in descending order
2768*/
2769static int fsdirBestIndex(
2770  sqlite3_vtab *tab,
2771  sqlite3_index_info *pIdxInfo
2772){
2773  int i;                 /* Loop over constraints */
2774  int idx4 = -1;
2775  int idx5 = -1;
2776  const struct sqlite3_index_constraint *pConstraint;
2777
2778  (void)tab;
2779  pConstraint = pIdxInfo->aConstraint;
2780  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2781    if( pConstraint->usable==0 ) continue;
2782    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2783    if( pConstraint->iColumn==4 ) idx4 = i;
2784    if( pConstraint->iColumn==5 ) idx5 = i;
2785  }
2786
2787  if( idx4<0 ){
2788    pIdxInfo->idxNum = 0;
2789    pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
2790  }else{
2791    pIdxInfo->aConstraintUsage[idx4].omit = 1;
2792    pIdxInfo->aConstraintUsage[idx4].argvIndex = 1;
2793    if( idx5>=0 ){
2794      pIdxInfo->aConstraintUsage[idx5].omit = 1;
2795      pIdxInfo->aConstraintUsage[idx5].argvIndex = 2;
2796      pIdxInfo->idxNum = 2;
2797      pIdxInfo->estimatedCost = 10.0;
2798    }else{
2799      pIdxInfo->idxNum = 1;
2800      pIdxInfo->estimatedCost = 100.0;
2801    }
2802  }
2803
2804  return SQLITE_OK;
2805}
2806
2807/*
2808** Register the "fsdir" virtual table.
2809*/
2810static int fsdirRegister(sqlite3 *db){
2811  static sqlite3_module fsdirModule = {
2812    0,                         /* iVersion */
2813    0,                         /* xCreate */
2814    fsdirConnect,              /* xConnect */
2815    fsdirBestIndex,            /* xBestIndex */
2816    fsdirDisconnect,           /* xDisconnect */
2817    0,                         /* xDestroy */
2818    fsdirOpen,                 /* xOpen - open a cursor */
2819    fsdirClose,                /* xClose - close a cursor */
2820    fsdirFilter,               /* xFilter - configure scan constraints */
2821    fsdirNext,                 /* xNext - advance a cursor */
2822    fsdirEof,                  /* xEof - check for end of scan */
2823    fsdirColumn,               /* xColumn - read data */
2824    fsdirRowid,                /* xRowid - read data */
2825    0,                         /* xUpdate */
2826    0,                         /* xBegin */
2827    0,                         /* xSync */
2828    0,                         /* xCommit */
2829    0,                         /* xRollback */
2830    0,                         /* xFindMethod */
2831    0,                         /* xRename */
2832    0,                         /* xSavepoint */
2833    0,                         /* xRelease */
2834    0                          /* xRollbackTo */
2835  };
2836
2837  int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2838  return rc;
2839}
2840#else         /* SQLITE_OMIT_VIRTUALTABLE */
2841# define fsdirRegister(x) SQLITE_OK
2842#endif
2843
2844#ifdef _WIN32
2845
2846#endif
2847int sqlite3_fileio_init(
2848  sqlite3 *db,
2849  char **pzErrMsg,
2850  const sqlite3_api_routines *pApi
2851){
2852  int rc = SQLITE_OK;
2853  SQLITE_EXTENSION_INIT2(pApi);
2854  (void)pzErrMsg;  /* Unused parameter */
2855  rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
2856                               readfileFunc, 0, 0);
2857  if( rc==SQLITE_OK ){
2858    rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
2859                                 writefileFunc, 0, 0);
2860  }
2861  if( rc==SQLITE_OK ){
2862    rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
2863                                 lsModeFunc, 0, 0);
2864  }
2865  if( rc==SQLITE_OK ){
2866    rc = fsdirRegister(db);
2867  }
2868  return rc;
2869}
2870
2871/************************* End ../ext/misc/fileio.c ********************/
2872/************************* Begin ../ext/misc/completion.c ******************/
2873/*
2874** 2017-07-10
2875**
2876** The author disclaims copyright to this source code.  In place of
2877** a legal notice, here is a blessing:
2878**
2879**    May you do good and not evil.
2880**    May you find forgiveness for yourself and forgive others.
2881**    May you share freely, never taking more than you give.
2882**
2883*************************************************************************
2884**
2885** This file implements an eponymous virtual table that returns suggested
2886** completions for a partial SQL input.
2887**
2888** Suggested usage:
2889**
2890**     SELECT DISTINCT candidate COLLATE nocase
2891**       FROM completion($prefix,$wholeline)
2892**      ORDER BY 1;
2893**
2894** The two query parameters are optional.  $prefix is the text of the
2895** current word being typed and that is to be completed.  $wholeline is
2896** the complete input line, used for context.
2897**
2898** The raw completion() table might return the same candidate multiple
2899** times, for example if the same column name is used to two or more
2900** tables.  And the candidates are returned in an arbitrary order.  Hence,
2901** the DISTINCT and ORDER BY are recommended.
2902**
2903** This virtual table operates at the speed of human typing, and so there
2904** is no attempt to make it fast.  Even a slow implementation will be much
2905** faster than any human can type.
2906**
2907*/
2908SQLITE_EXTENSION_INIT1
2909#include <assert.h>
2910#include <string.h>
2911#include <ctype.h>
2912
2913#ifndef SQLITE_OMIT_VIRTUALTABLE
2914
2915/* completion_vtab is a subclass of sqlite3_vtab which will
2916** serve as the underlying representation of a completion virtual table
2917*/
2918typedef struct completion_vtab completion_vtab;
2919struct completion_vtab {
2920  sqlite3_vtab base;  /* Base class - must be first */
2921  sqlite3 *db;        /* Database connection for this completion vtab */
2922};
2923
2924/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
2925** serve as the underlying representation of a cursor that scans
2926** over rows of the result
2927*/
2928typedef struct completion_cursor completion_cursor;
2929struct completion_cursor {
2930  sqlite3_vtab_cursor base;  /* Base class - must be first */
2931  sqlite3 *db;               /* Database connection for this cursor */
2932  int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
2933  char *zPrefix;             /* The prefix for the word we want to complete */
2934  char *zLine;               /* The whole that we want to complete */
2935  const char *zCurrentRow;   /* Current output row */
2936  sqlite3_stmt *pStmt;       /* Current statement */
2937  sqlite3_int64 iRowid;      /* The rowid */
2938  int ePhase;                /* Current phase */
2939  int j;                     /* inter-phase counter */
2940};
2941
2942/* Values for ePhase:
2943*/
2944#define COMPLETION_FIRST_PHASE   1
2945#define COMPLETION_KEYWORDS      1
2946#define COMPLETION_PRAGMAS       2
2947#define COMPLETION_FUNCTIONS     3
2948#define COMPLETION_COLLATIONS    4
2949#define COMPLETION_INDEXES       5
2950#define COMPLETION_TRIGGERS      6
2951#define COMPLETION_DATABASES     7
2952#define COMPLETION_TABLES        8
2953#define COMPLETION_COLUMNS       9
2954#define COMPLETION_MODULES       10
2955#define COMPLETION_EOF           11
2956
2957/*
2958** The completionConnect() method is invoked to create a new
2959** completion_vtab that describes the completion virtual table.
2960**
2961** Think of this routine as the constructor for completion_vtab objects.
2962**
2963** All this routine needs to do is:
2964**
2965**    (1) Allocate the completion_vtab object and initialize all fields.
2966**
2967**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
2968**        result set of queries against completion will look like.
2969*/
2970static int completionConnect(
2971  sqlite3 *db,
2972  void *pAux,
2973  int argc, const char *const*argv,
2974  sqlite3_vtab **ppVtab,
2975  char **pzErr
2976){
2977  completion_vtab *pNew;
2978  int rc;
2979
2980  (void)(pAux);    /* Unused parameter */
2981  (void)(argc);    /* Unused parameter */
2982  (void)(argv);    /* Unused parameter */
2983  (void)(pzErr);   /* Unused parameter */
2984
2985/* Column numbers */
2986#define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
2987#define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
2988#define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
2989#define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
2990
2991  rc = sqlite3_declare_vtab(db,
2992      "CREATE TABLE x("
2993      "  candidate TEXT,"
2994      "  prefix TEXT HIDDEN,"
2995      "  wholeline TEXT HIDDEN,"
2996      "  phase INT HIDDEN"        /* Used for debugging only */
2997      ")");
2998  if( rc==SQLITE_OK ){
2999    pNew = sqlite3_malloc( sizeof(*pNew) );
3000    *ppVtab = (sqlite3_vtab*)pNew;
3001    if( pNew==0 ) return SQLITE_NOMEM;
3002    memset(pNew, 0, sizeof(*pNew));
3003    pNew->db = db;
3004  }
3005  return rc;
3006}
3007
3008/*
3009** This method is the destructor for completion_cursor objects.
3010*/
3011static int completionDisconnect(sqlite3_vtab *pVtab){
3012  sqlite3_free(pVtab);
3013  return SQLITE_OK;
3014}
3015
3016/*
3017** Constructor for a new completion_cursor object.
3018*/
3019static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3020  completion_cursor *pCur;
3021  pCur = sqlite3_malloc( sizeof(*pCur) );
3022  if( pCur==0 ) return SQLITE_NOMEM;
3023  memset(pCur, 0, sizeof(*pCur));
3024  pCur->db = ((completion_vtab*)p)->db;
3025  *ppCursor = &pCur->base;
3026  return SQLITE_OK;
3027}
3028
3029/*
3030** Reset the completion_cursor.
3031*/
3032static void completionCursorReset(completion_cursor *pCur){
3033  sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3034  sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3035  sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3036  pCur->j = 0;
3037}
3038
3039/*
3040** Destructor for a completion_cursor.
3041*/
3042static int completionClose(sqlite3_vtab_cursor *cur){
3043  completionCursorReset((completion_cursor*)cur);
3044  sqlite3_free(cur);
3045  return SQLITE_OK;
3046}
3047
3048/*
3049** All SQL keywords understood by SQLite
3050*/
3051static const char *completionKwrds[] = {
3052  "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
3053  "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
3054  "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
3055  "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
3056  "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
3057  "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
3058  "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
3059  "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
3060  "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
3061  "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
3062  "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
3063  "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
3064  "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
3065  "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
3066  "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
3067  "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
3068  "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
3069  "WITH", "WITHOUT",
3070};
3071#define completionKwCount \
3072   (int)(sizeof(completionKwrds)/sizeof(completionKwrds[0]))
3073
3074/*
3075** Advance a completion_cursor to its next row of output.
3076**
3077** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3078** record the current state of the scan.  This routine sets ->zCurrentRow
3079** to the current row of output and then returns.  If no more rows remain,
3080** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3081** table that has reached the end of its scan.
3082**
3083** The current implementation just lists potential identifiers and
3084** keywords and filters them by zPrefix.  Future enhancements should
3085** take zLine into account to try to restrict the set of identifiers and
3086** keywords based on what would be legal at the current point of input.
3087*/
3088static int completionNext(sqlite3_vtab_cursor *cur){
3089  completion_cursor *pCur = (completion_cursor*)cur;
3090  int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3091  int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3092  pCur->iRowid++;
3093  while( pCur->ePhase!=COMPLETION_EOF ){
3094    switch( pCur->ePhase ){
3095      case COMPLETION_KEYWORDS: {
3096        if( pCur->j >= completionKwCount ){
3097          pCur->zCurrentRow = 0;
3098          pCur->ePhase = COMPLETION_DATABASES;
3099        }else{
3100          pCur->zCurrentRow = completionKwrds[pCur->j++];
3101        }
3102        iCol = -1;
3103        break;
3104      }
3105      case COMPLETION_DATABASES: {
3106        if( pCur->pStmt==0 ){
3107          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3108                             &pCur->pStmt, 0);
3109        }
3110        iCol = 1;
3111        eNextPhase = COMPLETION_TABLES;
3112        break;
3113      }
3114      case COMPLETION_TABLES: {
3115        if( pCur->pStmt==0 ){
3116          sqlite3_stmt *pS2;
3117          char *zSql = 0;
3118          const char *zSep = "";
3119          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3120          while( sqlite3_step(pS2)==SQLITE_ROW ){
3121            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3122            zSql = sqlite3_mprintf(
3123               "%z%s"
3124               "SELECT name FROM \"%w\".sqlite_master"
3125               " WHERE type='table'",
3126               zSql, zSep, zDb
3127            );
3128            if( zSql==0 ) return SQLITE_NOMEM;
3129            zSep = " UNION ";
3130          }
3131          sqlite3_finalize(pS2);
3132          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3133          sqlite3_free(zSql);
3134        }
3135        iCol = 0;
3136        eNextPhase = COMPLETION_COLUMNS;
3137        break;
3138      }
3139      case COMPLETION_COLUMNS: {
3140        if( pCur->pStmt==0 ){
3141          sqlite3_stmt *pS2;
3142          char *zSql = 0;
3143          const char *zSep = "";
3144          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3145          while( sqlite3_step(pS2)==SQLITE_ROW ){
3146            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3147            zSql = sqlite3_mprintf(
3148               "%z%s"
3149               "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3150                       " JOIN pragma_table_info(sm.name,%Q) AS pti"
3151               " WHERE sm.type='table'",
3152               zSql, zSep, zDb, zDb
3153            );
3154            if( zSql==0 ) return SQLITE_NOMEM;
3155            zSep = " UNION ";
3156          }
3157          sqlite3_finalize(pS2);
3158          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3159          sqlite3_free(zSql);
3160        }
3161        iCol = 0;
3162        eNextPhase = COMPLETION_EOF;
3163        break;
3164      }
3165    }
3166    if( iCol<0 ){
3167      /* This case is when the phase presets zCurrentRow */
3168      if( pCur->zCurrentRow==0 ) continue;
3169    }else{
3170      if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3171        /* Extract the next row of content */
3172        pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3173      }else{
3174        /* When all rows are finished, advance to the next phase */
3175        sqlite3_finalize(pCur->pStmt);
3176        pCur->pStmt = 0;
3177        pCur->ePhase = eNextPhase;
3178        continue;
3179      }
3180    }
3181    if( pCur->nPrefix==0 ) break;
3182    if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){
3183      break;
3184    }
3185  }
3186
3187  return SQLITE_OK;
3188}
3189
3190/*
3191** Return values of columns for the row at which the completion_cursor
3192** is currently pointing.
3193*/
3194static int completionColumn(
3195  sqlite3_vtab_cursor *cur,   /* The cursor */
3196  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3197  int i                       /* Which column to return */
3198){
3199  completion_cursor *pCur = (completion_cursor*)cur;
3200  switch( i ){
3201    case COMPLETION_COLUMN_CANDIDATE: {
3202      sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT);
3203      break;
3204    }
3205    case COMPLETION_COLUMN_PREFIX: {
3206      sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3207      break;
3208    }
3209    case COMPLETION_COLUMN_WHOLELINE: {
3210      sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3211      break;
3212    }
3213    case COMPLETION_COLUMN_PHASE: {
3214      sqlite3_result_int(ctx, pCur->ePhase);
3215      break;
3216    }
3217  }
3218  return SQLITE_OK;
3219}
3220
3221/*
3222** Return the rowid for the current row.  In this implementation, the
3223** rowid is the same as the output value.
3224*/
3225static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3226  completion_cursor *pCur = (completion_cursor*)cur;
3227  *pRowid = pCur->iRowid;
3228  return SQLITE_OK;
3229}
3230
3231/*
3232** Return TRUE if the cursor has been moved off of the last
3233** row of output.
3234*/
3235static int completionEof(sqlite3_vtab_cursor *cur){
3236  completion_cursor *pCur = (completion_cursor*)cur;
3237  return pCur->ePhase >= COMPLETION_EOF;
3238}
3239
3240/*
3241** This method is called to "rewind" the completion_cursor object back
3242** to the first row of output.  This method is always called at least
3243** once prior to any call to completionColumn() or completionRowid() or
3244** completionEof().
3245*/
3246static int completionFilter(
3247  sqlite3_vtab_cursor *pVtabCursor,
3248  int idxNum, const char *idxStr,
3249  int argc, sqlite3_value **argv
3250){
3251  completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3252  int iArg = 0;
3253  (void)(idxStr);   /* Unused parameter */
3254  (void)(argc);     /* Unused parameter */
3255  completionCursorReset(pCur);
3256  if( idxNum & 1 ){
3257    pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3258    if( pCur->nPrefix>0 ){
3259      pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3260      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3261    }
3262    iArg++;
3263  }
3264  if( idxNum & 2 ){
3265    pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3266    if( pCur->nLine>0 ){
3267      pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3268      if( pCur->zLine==0 ) return SQLITE_NOMEM;
3269    }
3270    iArg++;
3271  }
3272  if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3273    int i = pCur->nLine;
3274    while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3275      i--;
3276    }
3277    pCur->nPrefix = pCur->nLine - i;
3278    if( pCur->nPrefix>0 ){
3279      pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3280      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3281    }
3282  }
3283  pCur->iRowid = 0;
3284  pCur->ePhase = COMPLETION_FIRST_PHASE;
3285  return completionNext(pVtabCursor);
3286}
3287
3288/*
3289** SQLite will invoke this method one or more times while planning a query
3290** that uses the completion virtual table.  This routine needs to create
3291** a query plan for each invocation and compute an estimated cost for that
3292** plan.
3293**
3294** There are two hidden parameters that act as arguments to the table-valued
3295** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3296** is available and bit 1 is set if "wholeline" is available.
3297*/
3298static int completionBestIndex(
3299  sqlite3_vtab *tab,
3300  sqlite3_index_info *pIdxInfo
3301){
3302  int i;                 /* Loop over constraints */
3303  int idxNum = 0;        /* The query plan bitmask */
3304  int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3305  int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3306  int nArg = 0;          /* Number of arguments that completeFilter() expects */
3307  const struct sqlite3_index_constraint *pConstraint;
3308
3309  (void)(tab);    /* Unused parameter */
3310  pConstraint = pIdxInfo->aConstraint;
3311  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3312    if( pConstraint->usable==0 ) continue;
3313    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3314    switch( pConstraint->iColumn ){
3315      case COMPLETION_COLUMN_PREFIX:
3316        prefixIdx = i;
3317        idxNum |= 1;
3318        break;
3319      case COMPLETION_COLUMN_WHOLELINE:
3320        wholelineIdx = i;
3321        idxNum |= 2;
3322        break;
3323    }
3324  }
3325  if( prefixIdx>=0 ){
3326    pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3327    pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3328  }
3329  if( wholelineIdx>=0 ){
3330    pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3331    pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3332  }
3333  pIdxInfo->idxNum = idxNum;
3334  pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3335  pIdxInfo->estimatedRows = 500 - 100*nArg;
3336  return SQLITE_OK;
3337}
3338
3339/*
3340** This following structure defines all the methods for the
3341** completion virtual table.
3342*/
3343static sqlite3_module completionModule = {
3344  0,                         /* iVersion */
3345  0,                         /* xCreate */
3346  completionConnect,         /* xConnect */
3347  completionBestIndex,       /* xBestIndex */
3348  completionDisconnect,      /* xDisconnect */
3349  0,                         /* xDestroy */
3350  completionOpen,            /* xOpen - open a cursor */
3351  completionClose,           /* xClose - close a cursor */
3352  completionFilter,          /* xFilter - configure scan constraints */
3353  completionNext,            /* xNext - advance a cursor */
3354  completionEof,             /* xEof - check for end of scan */
3355  completionColumn,          /* xColumn - read data */
3356  completionRowid,           /* xRowid - read data */
3357  0,                         /* xUpdate */
3358  0,                         /* xBegin */
3359  0,                         /* xSync */
3360  0,                         /* xCommit */
3361  0,                         /* xRollback */
3362  0,                         /* xFindMethod */
3363  0,                         /* xRename */
3364  0,                         /* xSavepoint */
3365  0,                         /* xRelease */
3366  0                          /* xRollbackTo */
3367};
3368
3369#endif /* SQLITE_OMIT_VIRTUALTABLE */
3370
3371int sqlite3CompletionVtabInit(sqlite3 *db){
3372  int rc = SQLITE_OK;
3373#ifndef SQLITE_OMIT_VIRTUALTABLE
3374  rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3375#endif
3376  return rc;
3377}
3378
3379#ifdef _WIN32
3380
3381#endif
3382int sqlite3_completion_init(
3383  sqlite3 *db,
3384  char **pzErrMsg,
3385  const sqlite3_api_routines *pApi
3386){
3387  int rc = SQLITE_OK;
3388  SQLITE_EXTENSION_INIT2(pApi);
3389  (void)(pzErrMsg);  /* Unused parameter */
3390#ifndef SQLITE_OMIT_VIRTUALTABLE
3391  rc = sqlite3CompletionVtabInit(db);
3392#endif
3393  return rc;
3394}
3395
3396/************************* End ../ext/misc/completion.c ********************/
3397/************************* Begin ../ext/misc/appendvfs.c ******************/
3398/*
3399** 2017-10-20
3400**
3401** The author disclaims copyright to this source code.  In place of
3402** a legal notice, here is a blessing:
3403**
3404**    May you do good and not evil.
3405**    May you find forgiveness for yourself and forgive others.
3406**    May you share freely, never taking more than you give.
3407**
3408******************************************************************************
3409**
3410** This file implements a VFS shim that allows an SQLite database to be
3411** appended onto the end of some other file, such as an executable.
3412**
3413** A special record must appear at the end of the file that identifies the
3414** file as an appended database and provides an offset to page 1.  For
3415** best performance page 1 should be located at a disk page boundary, though
3416** that is not required.
3417**
3418** When opening a database using this VFS, the connection might treat
3419** the file as an ordinary SQLite database, or it might treat is as a
3420** database appended onto some other file.  Here are the rules:
3421**
3422**  (1)  When opening a new empty file, that file is treated as an ordinary
3423**       database.
3424**
3425**  (2)  When opening a file that begins with the standard SQLite prefix
3426**       string "SQLite format 3", that file is treated as an ordinary
3427**       database.
3428**
3429**  (3)  When opening a file that ends with the appendvfs trailer string
3430**       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3431**       database.
3432**
3433**  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3434**       set, then a new database is appended to the already existing file.
3435**
3436**  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3437**
3438** To avoid unnecessary complications with the PENDING_BYTE, the size of
3439** the file containing the database is limited to 1GB.  This VFS will refuse
3440** to read or write past the 1GB mark.  This restriction might be lifted in
3441** future versions.  For now, if you need a large database, then keep the
3442** database in a separate file.
3443**
3444** If the file being opened is not an appended database, then this shim is
3445** a pass-through into the default underlying VFS.
3446**/
3447SQLITE_EXTENSION_INIT1
3448#include <string.h>
3449#include <assert.h>
3450
3451/* The append mark at the end of the database is:
3452**
3453**     Start-Of-SQLite3-NNNNNNNN
3454**     123456789 123456789 12345
3455**
3456** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3457** the offset to page 1.
3458*/
3459#define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3460#define APND_MARK_PREFIX_SZ  17
3461#define APND_MARK_SIZE       25
3462
3463/*
3464** Maximum size of the combined prefix + database + append-mark.  This
3465** must be less than 0x40000000 to avoid locking issues on Windows.
3466*/
3467#define APND_MAX_SIZE  (65536*15259)
3468
3469/*
3470** Forward declaration of objects used by this utility
3471*/
3472typedef struct sqlite3_vfs ApndVfs;
3473typedef struct ApndFile ApndFile;
3474
3475/* Access to a lower-level VFS that (might) implement dynamic loading,
3476** access to randomness, etc.
3477*/
3478#define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3479#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3480
3481/* An open file */
3482struct ApndFile {
3483  sqlite3_file base;              /* IO methods */
3484  sqlite3_int64 iPgOne;           /* File offset to page 1 */
3485  sqlite3_int64 iMark;            /* Start of the append-mark */
3486};
3487
3488/*
3489** Methods for ApndFile
3490*/
3491static int apndClose(sqlite3_file*);
3492static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3493static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3494static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3495static int apndSync(sqlite3_file*, int flags);
3496static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3497static int apndLock(sqlite3_file*, int);
3498static int apndUnlock(sqlite3_file*, int);
3499static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3500static int apndFileControl(sqlite3_file*, int op, void *pArg);
3501static int apndSectorSize(sqlite3_file*);
3502static int apndDeviceCharacteristics(sqlite3_file*);
3503static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3504static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3505static void apndShmBarrier(sqlite3_file*);
3506static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3507static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3508static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3509
3510/*
3511** Methods for ApndVfs
3512*/
3513static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3514static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3515static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3516static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3517static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3518static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3519static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3520static void apndDlClose(sqlite3_vfs*, void*);
3521static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3522static int apndSleep(sqlite3_vfs*, int microseconds);
3523static int apndCurrentTime(sqlite3_vfs*, double*);
3524static int apndGetLastError(sqlite3_vfs*, int, char *);
3525static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3526static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3527static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3528static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3529
3530static sqlite3_vfs apnd_vfs = {
3531  3,                            /* iVersion (set when registered) */
3532  0,                            /* szOsFile (set when registered) */
3533  1024,                         /* mxPathname */
3534  0,                            /* pNext */
3535  "apndvfs",                    /* zName */
3536  0,                            /* pAppData (set when registered) */
3537  apndOpen,                     /* xOpen */
3538  apndDelete,                   /* xDelete */
3539  apndAccess,                   /* xAccess */
3540  apndFullPathname,             /* xFullPathname */
3541  apndDlOpen,                   /* xDlOpen */
3542  apndDlError,                  /* xDlError */
3543  apndDlSym,                    /* xDlSym */
3544  apndDlClose,                  /* xDlClose */
3545  apndRandomness,               /* xRandomness */
3546  apndSleep,                    /* xSleep */
3547  apndCurrentTime,              /* xCurrentTime */
3548  apndGetLastError,             /* xGetLastError */
3549  apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3550  apndSetSystemCall,            /* xSetSystemCall */
3551  apndGetSystemCall,            /* xGetSystemCall */
3552  apndNextSystemCall            /* xNextSystemCall */
3553};
3554
3555static const sqlite3_io_methods apnd_io_methods = {
3556  3,                              /* iVersion */
3557  apndClose,                      /* xClose */
3558  apndRead,                       /* xRead */
3559  apndWrite,                      /* xWrite */
3560  apndTruncate,                   /* xTruncate */
3561  apndSync,                       /* xSync */
3562  apndFileSize,                   /* xFileSize */
3563  apndLock,                       /* xLock */
3564  apndUnlock,                     /* xUnlock */
3565  apndCheckReservedLock,          /* xCheckReservedLock */
3566  apndFileControl,                /* xFileControl */
3567  apndSectorSize,                 /* xSectorSize */
3568  apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3569  apndShmMap,                     /* xShmMap */
3570  apndShmLock,                    /* xShmLock */
3571  apndShmBarrier,                 /* xShmBarrier */
3572  apndShmUnmap,                   /* xShmUnmap */
3573  apndFetch,                      /* xFetch */
3574  apndUnfetch                     /* xUnfetch */
3575};
3576
3577
3578
3579/*
3580** Close an apnd-file.
3581*/
3582static int apndClose(sqlite3_file *pFile){
3583  pFile = ORIGFILE(pFile);
3584  return pFile->pMethods->xClose(pFile);
3585}
3586
3587/*
3588** Read data from an apnd-file.
3589*/
3590static int apndRead(
3591  sqlite3_file *pFile,
3592  void *zBuf,
3593  int iAmt,
3594  sqlite_int64 iOfst
3595){
3596  ApndFile *p = (ApndFile *)pFile;
3597  pFile = ORIGFILE(pFile);
3598  return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3599}
3600
3601/*
3602** Add the append-mark onto the end of the file.
3603*/
3604static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3605  int i;
3606  unsigned char a[APND_MARK_SIZE];
3607  memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3608  for(i=0; i<8; i++){
3609    a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3610  }
3611  return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3612}
3613
3614/*
3615** Write data to an apnd-file.
3616*/
3617static int apndWrite(
3618  sqlite3_file *pFile,
3619  const void *zBuf,
3620  int iAmt,
3621  sqlite_int64 iOfst
3622){
3623  int rc;
3624  ApndFile *p = (ApndFile *)pFile;
3625  pFile = ORIGFILE(pFile);
3626  if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3627  rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3628  if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
3629    sqlite3_int64 sz = 0;
3630    rc = pFile->pMethods->xFileSize(pFile, &sz);
3631    if( rc==SQLITE_OK ){
3632      p->iMark = sz - APND_MARK_SIZE;
3633      if( iOfst + iAmt + p->iPgOne > p->iMark ){
3634        p->iMark = p->iPgOne + iOfst + iAmt;
3635        rc = apndWriteMark(p, pFile);
3636      }
3637    }
3638  }
3639  return rc;
3640}
3641
3642/*
3643** Truncate an apnd-file.
3644*/
3645static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3646  int rc;
3647  ApndFile *p = (ApndFile *)pFile;
3648  pFile = ORIGFILE(pFile);
3649  rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3650  if( rc==SQLITE_OK ){
3651    p->iMark = p->iPgOne+size;
3652    rc = apndWriteMark(p, pFile);
3653  }
3654  return rc;
3655}
3656
3657/*
3658** Sync an apnd-file.
3659*/
3660static int apndSync(sqlite3_file *pFile, int flags){
3661  pFile = ORIGFILE(pFile);
3662  return pFile->pMethods->xSync(pFile, flags);
3663}
3664
3665/*
3666** Return the current file-size of an apnd-file.
3667*/
3668static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3669  ApndFile *p = (ApndFile *)pFile;
3670  int rc;
3671  pFile = ORIGFILE(p);
3672  rc = pFile->pMethods->xFileSize(pFile, pSize);
3673  if( rc==SQLITE_OK && p->iPgOne ){
3674    *pSize -= p->iPgOne + APND_MARK_SIZE;
3675  }
3676  return rc;
3677}
3678
3679/*
3680** Lock an apnd-file.
3681*/
3682static int apndLock(sqlite3_file *pFile, int eLock){
3683  pFile = ORIGFILE(pFile);
3684  return pFile->pMethods->xLock(pFile, eLock);
3685}
3686
3687/*
3688** Unlock an apnd-file.
3689*/
3690static int apndUnlock(sqlite3_file *pFile, int eLock){
3691  pFile = ORIGFILE(pFile);
3692  return pFile->pMethods->xUnlock(pFile, eLock);
3693}
3694
3695/*
3696** Check if another file-handle holds a RESERVED lock on an apnd-file.
3697*/
3698static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3699  pFile = ORIGFILE(pFile);
3700  return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3701}
3702
3703/*
3704** File control method. For custom operations on an apnd-file.
3705*/
3706static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3707  ApndFile *p = (ApndFile *)pFile;
3708  int rc;
3709  pFile = ORIGFILE(pFile);
3710  rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3711  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3712    *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3713  }
3714  return rc;
3715}
3716
3717/*
3718** Return the sector-size in bytes for an apnd-file.
3719*/
3720static int apndSectorSize(sqlite3_file *pFile){
3721  pFile = ORIGFILE(pFile);
3722  return pFile->pMethods->xSectorSize(pFile);
3723}
3724
3725/*
3726** Return the device characteristic flags supported by an apnd-file.
3727*/
3728static int apndDeviceCharacteristics(sqlite3_file *pFile){
3729  pFile = ORIGFILE(pFile);
3730  return pFile->pMethods->xDeviceCharacteristics(pFile);
3731}
3732
3733/* Create a shared memory file mapping */
3734static int apndShmMap(
3735  sqlite3_file *pFile,
3736  int iPg,
3737  int pgsz,
3738  int bExtend,
3739  void volatile **pp
3740){
3741  pFile = ORIGFILE(pFile);
3742  return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3743}
3744
3745/* Perform locking on a shared-memory segment */
3746static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3747  pFile = ORIGFILE(pFile);
3748  return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3749}
3750
3751/* Memory barrier operation on shared memory */
3752static void apndShmBarrier(sqlite3_file *pFile){
3753  pFile = ORIGFILE(pFile);
3754  pFile->pMethods->xShmBarrier(pFile);
3755}
3756
3757/* Unmap a shared memory segment */
3758static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3759  pFile = ORIGFILE(pFile);
3760  return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3761}
3762
3763/* Fetch a page of a memory-mapped file */
3764static int apndFetch(
3765  sqlite3_file *pFile,
3766  sqlite3_int64 iOfst,
3767  int iAmt,
3768  void **pp
3769){
3770  ApndFile *p = (ApndFile *)pFile;
3771  pFile = ORIGFILE(pFile);
3772  return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3773}
3774
3775/* Release a memory-mapped page */
3776static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3777  ApndFile *p = (ApndFile *)pFile;
3778  pFile = ORIGFILE(pFile);
3779  return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3780}
3781
3782/*
3783** Check to see if the file is an ordinary SQLite database file.
3784*/
3785static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3786  int rc;
3787  char zHdr[16];
3788  static const char aSqliteHdr[] = "SQLite format 3";
3789  if( sz<512 ) return 0;
3790  rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3791  if( rc ) return 0;
3792  return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3793}
3794
3795/*
3796** Try to read the append-mark off the end of a file.  Return the
3797** start of the appended database if the append-mark is present.  If
3798** there is no append-mark, return -1;
3799*/
3800static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3801  int rc, i;
3802  sqlite3_int64 iMark;
3803  unsigned char a[APND_MARK_SIZE];
3804
3805  if( sz<=APND_MARK_SIZE ) return -1;
3806  rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3807  if( rc ) return -1;
3808  if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3809  iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3810  for(i=1; i<8; i++){
3811    iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3812  }
3813  return iMark;
3814}
3815
3816/*
3817** Open an apnd file handle.
3818*/
3819static int apndOpen(
3820  sqlite3_vfs *pVfs,
3821  const char *zName,
3822  sqlite3_file *pFile,
3823  int flags,
3824  int *pOutFlags
3825){
3826  ApndFile *p;
3827  sqlite3_file *pSubFile;
3828  sqlite3_vfs *pSubVfs;
3829  int rc;
3830  sqlite3_int64 sz;
3831  pSubVfs = ORIGVFS(pVfs);
3832  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3833    return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3834  }
3835  p = (ApndFile*)pFile;
3836  memset(p, 0, sizeof(*p));
3837  pSubFile = ORIGFILE(pFile);
3838  p->base.pMethods = &apnd_io_methods;
3839  rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3840  if( rc ) goto apnd_open_done;
3841  rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3842  if( rc ){
3843    pSubFile->pMethods->xClose(pSubFile);
3844    goto apnd_open_done;
3845  }
3846  if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3847    memmove(pFile, pSubFile, pSubVfs->szOsFile);
3848    return SQLITE_OK;
3849  }
3850  p->iMark = 0;
3851  p->iPgOne = apndReadMark(sz, pFile);
3852  if( p->iPgOne>0 ){
3853    return SQLITE_OK;
3854  }
3855  if( (flags & SQLITE_OPEN_CREATE)==0 ){
3856    pSubFile->pMethods->xClose(pSubFile);
3857    rc = SQLITE_CANTOPEN;
3858  }
3859  p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3860apnd_open_done:
3861  if( rc ) pFile->pMethods = 0;
3862  return rc;
3863}
3864
3865/*
3866** All other VFS methods are pass-thrus.
3867*/
3868static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3869  return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
3870}
3871static int apndAccess(
3872  sqlite3_vfs *pVfs,
3873  const char *zPath,
3874  int flags,
3875  int *pResOut
3876){
3877  return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
3878}
3879static int apndFullPathname(
3880  sqlite3_vfs *pVfs,
3881  const char *zPath,
3882  int nOut,
3883  char *zOut
3884){
3885  return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
3886}
3887static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
3888  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
3889}
3890static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
3891  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
3892}
3893static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
3894  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
3895}
3896static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
3897  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
3898}
3899static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
3900  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
3901}
3902static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
3903  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
3904}
3905static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
3906  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
3907}
3908static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
3909  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
3910}
3911static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
3912  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
3913}
3914static int apndSetSystemCall(
3915  sqlite3_vfs *pVfs,
3916  const char *zName,
3917  sqlite3_syscall_ptr pCall
3918){
3919  return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
3920}
3921static sqlite3_syscall_ptr apndGetSystemCall(
3922  sqlite3_vfs *pVfs,
3923  const char *zName
3924){
3925  return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
3926}
3927static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
3928  return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
3929}
3930
3931
3932#ifdef _WIN32
3933
3934#endif
3935/*
3936** This routine is called when the extension is loaded.
3937** Register the new VFS.
3938*/
3939int sqlite3_appendvfs_init(
3940  sqlite3 *db,
3941  char **pzErrMsg,
3942  const sqlite3_api_routines *pApi
3943){
3944  int rc = SQLITE_OK;
3945  sqlite3_vfs *pOrig;
3946  SQLITE_EXTENSION_INIT2(pApi);
3947  (void)pzErrMsg;
3948  (void)db;
3949  pOrig = sqlite3_vfs_find(0);
3950  apnd_vfs.iVersion = pOrig->iVersion;
3951  apnd_vfs.pAppData = pOrig;
3952  apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
3953  rc = sqlite3_vfs_register(&apnd_vfs, 0);
3954#ifdef APPENDVFS_TEST
3955  if( rc==SQLITE_OK ){
3956    rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
3957  }
3958#endif
3959  if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
3960  return rc;
3961}
3962
3963/************************* End ../ext/misc/appendvfs.c ********************/
3964#ifdef SQLITE_HAVE_ZLIB
3965/************************* Begin ../ext/misc/zipfile.c ******************/
3966/*
3967** 2017-12-26
3968**
3969** The author disclaims copyright to this source code.  In place of
3970** a legal notice, here is a blessing:
3971**
3972**    May you do good and not evil.
3973**    May you find forgiveness for yourself and forgive others.
3974**    May you share freely, never taking more than you give.
3975**
3976******************************************************************************
3977**
3978** This file implements a virtual table for reading and writing ZIP archive
3979** files.
3980**
3981** Usage example:
3982**
3983**     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
3984**
3985** Current limitations:
3986**
3987**    *  No support for encryption
3988**    *  No support for ZIP archives spanning multiple files
3989**    *  No support for zip64 extensions
3990**    *  Only the "inflate/deflate" (zlib) compression method is supported
3991*/
3992SQLITE_EXTENSION_INIT1
3993#include <stdio.h>
3994#include <string.h>
3995#include <assert.h>
3996
3997#include <sys/types.h>
3998#include <sys/stat.h>
3999#include <fcntl.h>
4000#if !defined(_WIN32) && !defined(WIN32)
4001#  include <unistd.h>
4002#  include <dirent.h>
4003#  include <utime.h>
4004#else
4005#  include <io.h>
4006#endif
4007#include <time.h>
4008#include <errno.h>
4009
4010#include <zlib.h>
4011
4012#ifndef SQLITE_OMIT_VIRTUALTABLE
4013
4014#ifndef SQLITE_AMALGAMATION
4015/* typedef sqlite3_int64 i64; */
4016/* typedef unsigned char u8; */
4017typedef unsigned short u16;
4018typedef unsigned long u32;
4019#define MIN(a,b) ((a)<(b) ? (a) : (b))
4020#endif
4021
4022static const char ZIPFILE_SCHEMA[] =
4023  "CREATE TABLE y("
4024    "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
4025    "mode,"              /* 1: POSIX mode for file */
4026    "mtime,"             /* 2: Last modification time (secs since 1970)*/
4027    "sz,"                /* 3: Size of object */
4028    "rawdata,"           /* 4: Raw data */
4029    "data,"              /* 5: Uncompressed data */
4030    "method,"            /* 6: Compression method (integer) */
4031    "z HIDDEN"           /* 7: Name of zip file */
4032  ") WITHOUT ROWID;";
4033
4034#define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
4035#define ZIPFILE_BUFFER_SIZE (64*1024)
4036
4037
4038/*
4039** Magic numbers used to read and write zip files.
4040**
4041** ZIPFILE_NEWENTRY_MADEBY:
4042**   Use this value for the "version-made-by" field in new zip file
4043**   entries. The upper byte indicates "unix", and the lower byte
4044**   indicates that the zip file matches pkzip specification 3.0.
4045**   This is what info-zip seems to do.
4046**
4047** ZIPFILE_NEWENTRY_REQUIRED:
4048**   Value for "version-required-to-extract" field of new entries.
4049**   Version 2.0 is required to support folders and deflate compression.
4050**
4051** ZIPFILE_NEWENTRY_FLAGS:
4052**   Value for "general-purpose-bit-flags" field of new entries. Bit
4053**   11 means "utf-8 filename and comment".
4054**
4055** ZIPFILE_SIGNATURE_CDS:
4056**   First 4 bytes of a valid CDS record.
4057**
4058** ZIPFILE_SIGNATURE_LFH:
4059**   First 4 bytes of a valid LFH record.
4060*/
4061#define ZIPFILE_EXTRA_TIMESTAMP   0x5455
4062#define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
4063#define ZIPFILE_NEWENTRY_REQUIRED 20
4064#define ZIPFILE_NEWENTRY_FLAGS    0x800
4065#define ZIPFILE_SIGNATURE_CDS     0x02014b50
4066#define ZIPFILE_SIGNATURE_LFH     0x04034b50
4067#define ZIPFILE_SIGNATURE_EOCD    0x06054b50
4068#define ZIPFILE_LFH_FIXED_SZ      30
4069
4070/*
4071** Set the error message contained in context ctx to the results of
4072** vprintf(zFmt, ...).
4073*/
4074static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4075  char *zMsg = 0;
4076  va_list ap;
4077  va_start(ap, zFmt);
4078  zMsg = sqlite3_vmprintf(zFmt, ap);
4079  sqlite3_result_error(ctx, zMsg, -1);
4080  sqlite3_free(zMsg);
4081  va_end(ap);
4082}
4083
4084
4085/*
4086*** 4.3.16  End of central directory record:
4087***
4088***   end of central dir signature    4 bytes  (0x06054b50)
4089***   number of this disk             2 bytes
4090***   number of the disk with the
4091***   start of the central directory  2 bytes
4092***   total number of entries in the
4093***   central directory on this disk  2 bytes
4094***   total number of entries in
4095***   the central directory           2 bytes
4096***   size of the central directory   4 bytes
4097***   offset of start of central
4098***   directory with respect to
4099***   the starting disk number        4 bytes
4100***   .ZIP file comment length        2 bytes
4101***   .ZIP file comment       (variable size)
4102*/
4103typedef struct ZipfileEOCD ZipfileEOCD;
4104struct ZipfileEOCD {
4105  u16 iDisk;
4106  u16 iFirstDisk;
4107  u16 nEntry;
4108  u16 nEntryTotal;
4109  u32 nSize;
4110  u32 iOffset;
4111};
4112
4113/*
4114*** 4.3.12  Central directory structure:
4115***
4116*** ...
4117***
4118***   central file header signature   4 bytes  (0x02014b50)
4119***   version made by                 2 bytes
4120***   version needed to extract       2 bytes
4121***   general purpose bit flag        2 bytes
4122***   compression method              2 bytes
4123***   last mod file time              2 bytes
4124***   last mod file date              2 bytes
4125***   crc-32                          4 bytes
4126***   compressed size                 4 bytes
4127***   uncompressed size               4 bytes
4128***   file name length                2 bytes
4129***   extra field length              2 bytes
4130***   file comment length             2 bytes
4131***   disk number start               2 bytes
4132***   internal file attributes        2 bytes
4133***   external file attributes        4 bytes
4134***   relative offset of local header 4 bytes
4135*/
4136typedef struct ZipfileCDS ZipfileCDS;
4137struct ZipfileCDS {
4138  u16 iVersionMadeBy;
4139  u16 iVersionExtract;
4140  u16 flags;
4141  u16 iCompression;
4142  u16 mTime;
4143  u16 mDate;
4144  u32 crc32;
4145  u32 szCompressed;
4146  u32 szUncompressed;
4147  u16 nFile;
4148  u16 nExtra;
4149  u16 nComment;
4150  u16 iDiskStart;
4151  u16 iInternalAttr;
4152  u32 iExternalAttr;
4153  u32 iOffset;
4154  char *zFile;                    /* Filename (sqlite3_malloc()) */
4155};
4156
4157/*
4158*** 4.3.7  Local file header:
4159***
4160***   local file header signature     4 bytes  (0x04034b50)
4161***   version needed to extract       2 bytes
4162***   general purpose bit flag        2 bytes
4163***   compression method              2 bytes
4164***   last mod file time              2 bytes
4165***   last mod file date              2 bytes
4166***   crc-32                          4 bytes
4167***   compressed size                 4 bytes
4168***   uncompressed size               4 bytes
4169***   file name length                2 bytes
4170***   extra field length              2 bytes
4171***
4172*/
4173typedef struct ZipfileLFH ZipfileLFH;
4174struct ZipfileLFH {
4175  u16 iVersionExtract;
4176  u16 flags;
4177  u16 iCompression;
4178  u16 mTime;
4179  u16 mDate;
4180  u32 crc32;
4181  u32 szCompressed;
4182  u32 szUncompressed;
4183  u16 nFile;
4184  u16 nExtra;
4185};
4186
4187typedef struct ZipfileEntry ZipfileEntry;
4188struct ZipfileEntry {
4189  char *zPath;               /* Path of zipfile entry */
4190  u8 *aCdsEntry;             /* Buffer containing entire CDS entry */
4191  int nCdsEntry;             /* Size of buffer aCdsEntry[] in bytes */
4192  int bDeleted;              /* True if entry has been deleted */
4193  ZipfileEntry *pNext;       /* Next element in in-memory CDS */
4194};
4195
4196/*
4197** Cursor type for recursively iterating through a directory structure.
4198*/
4199typedef struct ZipfileCsr ZipfileCsr;
4200struct ZipfileCsr {
4201  sqlite3_vtab_cursor base;  /* Base class - must be first */
4202  i64 iId;                   /* Cursor ID */
4203  int bEof;                  /* True when at EOF */
4204
4205  /* Used outside of write transactions */
4206  FILE *pFile;               /* Zip file */
4207  i64 iNextOff;              /* Offset of next record in central directory */
4208  ZipfileEOCD eocd;          /* Parse of central directory record */
4209
4210  /* Used inside write transactions */
4211  ZipfileEntry *pCurrent;
4212
4213  ZipfileCDS cds;            /* Central Directory Structure */
4214  ZipfileLFH lfh;            /* Local File Header for current entry */
4215  i64 iDataOff;              /* Offset in zipfile to data */
4216  u32 mTime;                 /* Extended mtime value */
4217  int flags;                 /* Flags byte (see below for bits) */
4218  ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
4219};
4220
4221/*
4222** Values for ZipfileCsr.flags.
4223*/
4224#define ZIPFILE_MTIME_VALID 0x0001
4225
4226typedef struct ZipfileTab ZipfileTab;
4227struct ZipfileTab {
4228  sqlite3_vtab base;         /* Base class - must be first */
4229  char *zFile;               /* Zip file this table accesses (may be NULL) */
4230  u8 *aBuffer;               /* Temporary buffer used for various tasks */
4231
4232  ZipfileCsr *pCsrList;      /* List of cursors */
4233  i64 iNextCsrid;
4234
4235  /* The following are used by write transactions only */
4236  ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4237  ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
4238  FILE *pWriteFd;            /* File handle open on zip archive */
4239  i64 szCurrent;             /* Current size of zip archive */
4240  i64 szOrig;                /* Size of archive at start of transaction */
4241};
4242
4243static void zipfileDequote(char *zIn){
4244  char q = zIn[0];
4245  if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4246    char c;
4247    int iIn = 1;
4248    int iOut = 0;
4249    if( q=='[' ) q = ']';
4250    while( (c = zIn[iIn++]) ){
4251      if( c==q ){
4252        if( zIn[iIn++]!=q ) break;
4253      }
4254      zIn[iOut++] = c;
4255    }
4256    zIn[iOut] = '\0';
4257  }
4258}
4259
4260/*
4261** Construct a new ZipfileTab virtual table object.
4262**
4263**   argv[0]   -> module name  ("zipfile")
4264**   argv[1]   -> database name
4265**   argv[2]   -> table name
4266**   argv[...] -> "column name" and other module argument fields.
4267*/
4268static int zipfileConnect(
4269  sqlite3 *db,
4270  void *pAux,
4271  int argc, const char *const*argv,
4272  sqlite3_vtab **ppVtab,
4273  char **pzErr
4274){
4275  int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4276  int nFile = 0;
4277  const char *zFile = 0;
4278  ZipfileTab *pNew = 0;
4279  int rc;
4280
4281  if( argc>3 ){
4282    zFile = argv[3];
4283    nFile = (int)strlen(zFile)+1;
4284  }
4285
4286  rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4287  if( rc==SQLITE_OK ){
4288    pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
4289    if( pNew==0 ) return SQLITE_NOMEM;
4290    memset(pNew, 0, nByte+nFile);
4291    pNew->aBuffer = (u8*)&pNew[1];
4292    if( zFile ){
4293      pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4294      memcpy(pNew->zFile, zFile, nFile);
4295      zipfileDequote(pNew->zFile);
4296    }
4297  }
4298  *ppVtab = (sqlite3_vtab*)pNew;
4299  return rc;
4300}
4301
4302/*
4303** This method is the destructor for zipfile vtab objects.
4304*/
4305static int zipfileDisconnect(sqlite3_vtab *pVtab){
4306  sqlite3_free(pVtab);
4307  return SQLITE_OK;
4308}
4309
4310/*
4311** Constructor for a new ZipfileCsr object.
4312*/
4313static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4314  ZipfileTab *pTab = (ZipfileTab*)p;
4315  ZipfileCsr *pCsr;
4316  pCsr = sqlite3_malloc(sizeof(*pCsr));
4317  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4318  if( pCsr==0 ){
4319    return SQLITE_NOMEM;
4320  }
4321  memset(pCsr, 0, sizeof(*pCsr));
4322  pCsr->iId = ++pTab->iNextCsrid;
4323  pCsr->pCsrNext = pTab->pCsrList;
4324  pTab->pCsrList = pCsr;
4325  return SQLITE_OK;
4326}
4327
4328/*
4329** Reset a cursor back to the state it was in when first returned
4330** by zipfileOpen().
4331*/
4332static void zipfileResetCursor(ZipfileCsr *pCsr){
4333  sqlite3_free(pCsr->cds.zFile);
4334  pCsr->cds.zFile = 0;
4335  pCsr->bEof = 0;
4336  if( pCsr->pFile ){
4337    fclose(pCsr->pFile);
4338    pCsr->pFile = 0;
4339  }
4340}
4341
4342/*
4343** Destructor for an ZipfileCsr.
4344*/
4345static int zipfileClose(sqlite3_vtab_cursor *cur){
4346  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4347  ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4348  ZipfileCsr **pp;
4349  zipfileResetCursor(pCsr);
4350
4351  /* Remove this cursor from the ZipfileTab.pCsrList list. */
4352  for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
4353    if( *pp==pCsr ){
4354      *pp = pCsr->pCsrNext;
4355      break;
4356    }
4357  }
4358
4359  sqlite3_free(pCsr);
4360  return SQLITE_OK;
4361}
4362
4363/*
4364** Set the error message for the virtual table associated with cursor
4365** pCsr to the results of vprintf(zFmt, ...).
4366*/
4367static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
4368  va_list ap;
4369  va_start(ap, zFmt);
4370  pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4371  va_end(ap);
4372}
4373
4374static int zipfileReadData(
4375  FILE *pFile,                    /* Read from this file */
4376  u8 *aRead,                      /* Read into this buffer */
4377  int nRead,                      /* Number of bytes to read */
4378  i64 iOff,                       /* Offset to read from */
4379  char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
4380){
4381  size_t n;
4382  fseek(pFile, (long)iOff, SEEK_SET);
4383  n = fread(aRead, 1, nRead, pFile);
4384  if( (int)n!=nRead ){
4385    *pzErrmsg = sqlite3_mprintf("error in fread()");
4386    return SQLITE_ERROR;
4387  }
4388  return SQLITE_OK;
4389}
4390
4391static int zipfileAppendData(
4392  ZipfileTab *pTab,
4393  const u8 *aWrite,
4394  int nWrite
4395){
4396  size_t n;
4397  fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4398  n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4399  if( (int)n!=nWrite ){
4400    pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4401    return SQLITE_ERROR;
4402  }
4403  pTab->szCurrent += nWrite;
4404  return SQLITE_OK;
4405}
4406
4407static u16 zipfileGetU16(const u8 *aBuf){
4408  return (aBuf[1] << 8) + aBuf[0];
4409}
4410static u32 zipfileGetU32(const u8 *aBuf){
4411  return ((u32)(aBuf[3]) << 24)
4412       + ((u32)(aBuf[2]) << 16)
4413       + ((u32)(aBuf[1]) <<  8)
4414       + ((u32)(aBuf[0]) <<  0);
4415}
4416
4417static void zipfilePutU16(u8 *aBuf, u16 val){
4418  aBuf[0] = val & 0xFF;
4419  aBuf[1] = (val>>8) & 0xFF;
4420}
4421static void zipfilePutU32(u8 *aBuf, u32 val){
4422  aBuf[0] = val & 0xFF;
4423  aBuf[1] = (val>>8) & 0xFF;
4424  aBuf[2] = (val>>16) & 0xFF;
4425  aBuf[3] = (val>>24) & 0xFF;
4426}
4427
4428#define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4429#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4430
4431#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4432#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4433
4434static u8* zipfileCsrBuffer(ZipfileCsr *pCsr){
4435  return ((ZipfileTab*)(pCsr->base.pVtab))->aBuffer;
4436}
4437
4438/*
4439** Magic numbers used to read CDS records.
4440*/
4441#define ZIPFILE_CDS_FIXED_SZ         46
4442#define ZIPFILE_CDS_NFILE_OFF        28
4443
4444/*
4445** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4446** if the record is not well-formed, or SQLITE_OK otherwise.
4447*/
4448static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4449  u8 *aRead = aBuf;
4450  u32 sig = zipfileRead32(aRead);
4451  int rc = SQLITE_OK;
4452  if( sig!=ZIPFILE_SIGNATURE_CDS ){
4453    rc = SQLITE_ERROR;
4454  }else{
4455    pCDS->iVersionMadeBy = zipfileRead16(aRead);
4456    pCDS->iVersionExtract = zipfileRead16(aRead);
4457    pCDS->flags = zipfileRead16(aRead);
4458    pCDS->iCompression = zipfileRead16(aRead);
4459    pCDS->mTime = zipfileRead16(aRead);
4460    pCDS->mDate = zipfileRead16(aRead);
4461    pCDS->crc32 = zipfileRead32(aRead);
4462    pCDS->szCompressed = zipfileRead32(aRead);
4463    pCDS->szUncompressed = zipfileRead32(aRead);
4464    assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4465    pCDS->nFile = zipfileRead16(aRead);
4466    pCDS->nExtra = zipfileRead16(aRead);
4467    pCDS->nComment = zipfileRead16(aRead);
4468    pCDS->iDiskStart = zipfileRead16(aRead);
4469    pCDS->iInternalAttr = zipfileRead16(aRead);
4470    pCDS->iExternalAttr = zipfileRead32(aRead);
4471    pCDS->iOffset = zipfileRead32(aRead);
4472    assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4473  }
4474
4475  return rc;
4476}
4477
4478/*
4479** Read the CDS record for the current entry from disk into pCsr->cds.
4480*/
4481static int zipfileCsrReadCDS(ZipfileCsr *pCsr){
4482  char **pzErr = &pCsr->base.pVtab->zErrMsg;
4483  u8 *aRead;
4484  int rc = SQLITE_OK;
4485
4486  sqlite3_free(pCsr->cds.zFile);
4487  pCsr->cds.zFile = 0;
4488
4489  if( pCsr->pCurrent==0 ){
4490    aRead = zipfileCsrBuffer(pCsr);
4491    rc = zipfileReadData(
4492        pCsr->pFile, aRead, ZIPFILE_CDS_FIXED_SZ, pCsr->iNextOff, pzErr
4493    );
4494  }else{
4495    aRead = pCsr->pCurrent->aCdsEntry;
4496  }
4497
4498  if( rc==SQLITE_OK ){
4499    rc = zipfileReadCDS(aRead, &pCsr->cds);
4500    if( rc!=SQLITE_OK ){
4501      assert( pCsr->pCurrent==0 );
4502      zipfileSetErrmsg(pCsr,"failed to read CDS at offset %lld",pCsr->iNextOff);
4503    }else{
4504      int nRead;
4505      if( pCsr->pCurrent==0 ){
4506        nRead = pCsr->cds.nFile + pCsr->cds.nExtra;
4507        aRead = zipfileCsrBuffer(pCsr);
4508        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4509        rc = zipfileReadData(pCsr->pFile, aRead, nRead, pCsr->iNextOff, pzErr);
4510      }else{
4511        aRead = &aRead[ZIPFILE_CDS_FIXED_SZ];
4512      }
4513
4514      if( rc==SQLITE_OK ){
4515        pCsr->cds.zFile = sqlite3_mprintf("%.*s", (int)pCsr->cds.nFile, aRead);
4516        pCsr->iNextOff += pCsr->cds.nFile;
4517        pCsr->iNextOff += pCsr->cds.nExtra;
4518        pCsr->iNextOff += pCsr->cds.nComment;
4519      }
4520
4521      /* Scan the cds.nExtra bytes of "extra" fields for any that can
4522      ** be interpreted. The general format of an extra field is:
4523      **
4524      **   Header ID    2 bytes
4525      **   Data Size    2 bytes
4526      **   Data         N bytes
4527      **
4528      */
4529      if( rc==SQLITE_OK ){
4530        u8 *p = &aRead[pCsr->cds.nFile];
4531        u8 *pEnd = &p[pCsr->cds.nExtra];
4532
4533        while( p<pEnd ){
4534          u16 id = zipfileRead16(p);
4535          u16 nByte = zipfileRead16(p);
4536
4537          switch( id ){
4538            case ZIPFILE_EXTRA_TIMESTAMP: {
4539              u8 b = p[0];
4540              if( b & 0x01 ){     /* 0x01 -> modtime is present */
4541                pCsr->mTime = zipfileGetU32(&p[1]);
4542                pCsr->flags |= ZIPFILE_MTIME_VALID;
4543              }
4544              break;
4545            }
4546          }
4547
4548          p += nByte;
4549        }
4550      }
4551    }
4552  }
4553
4554  return rc;
4555}
4556
4557static FILE *zipfileGetFd(ZipfileCsr *pCsr){
4558  if( pCsr->pFile ) return pCsr->pFile;
4559  return ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
4560}
4561
4562static int zipfileReadLFH(
4563  FILE *pFd,
4564  i64 iOffset,
4565  u8 *aTmp,
4566  ZipfileLFH *pLFH,
4567  char **pzErr
4568){
4569  u8 *aRead = aTmp;
4570  static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4571  int rc;
4572
4573  rc = zipfileReadData(pFd, aRead, szFix, iOffset, pzErr);
4574  if( rc==SQLITE_OK ){
4575    u32 sig = zipfileRead32(aRead);
4576    if( sig!=ZIPFILE_SIGNATURE_LFH ){
4577      *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", (int)iOffset);
4578      rc = SQLITE_ERROR;
4579    }else{
4580      pLFH->iVersionExtract = zipfileRead16(aRead);
4581      pLFH->flags = zipfileRead16(aRead);
4582      pLFH->iCompression = zipfileRead16(aRead);
4583      pLFH->mTime = zipfileRead16(aRead);
4584      pLFH->mDate = zipfileRead16(aRead);
4585      pLFH->crc32 = zipfileRead32(aRead);
4586      pLFH->szCompressed = zipfileRead32(aRead);
4587      pLFH->szUncompressed = zipfileRead32(aRead);
4588      pLFH->nFile = zipfileRead16(aRead);
4589      pLFH->nExtra = zipfileRead16(aRead);
4590      assert( aRead==&aTmp[szFix] );
4591    }
4592  }
4593  return rc;
4594}
4595
4596static int zipfileCsrReadLFH(ZipfileCsr *pCsr){
4597  FILE *pFile = zipfileGetFd(pCsr);
4598  char **pzErr = &pCsr->base.pVtab->zErrMsg;
4599  u8 *aRead = zipfileCsrBuffer(pCsr);
4600  int rc = zipfileReadLFH(pFile, pCsr->cds.iOffset, aRead, &pCsr->lfh, pzErr);
4601  pCsr->iDataOff =  pCsr->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4602  pCsr->iDataOff += pCsr->lfh.nFile+pCsr->lfh.nExtra;
4603  return rc;
4604}
4605
4606
4607/*
4608** Advance an ZipfileCsr to its next row of output.
4609*/
4610static int zipfileNext(sqlite3_vtab_cursor *cur){
4611  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4612  int rc = SQLITE_OK;
4613  pCsr->flags = 0;
4614
4615  if( pCsr->pCurrent==0 ){
4616    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4617    if( pCsr->iNextOff>=iEof ){
4618      pCsr->bEof = 1;
4619    }
4620  }else{
4621    assert( pCsr->pFile==0 );
4622    do {
4623      pCsr->pCurrent = pCsr->pCurrent->pNext;
4624    }while( pCsr->pCurrent && pCsr->pCurrent->bDeleted );
4625    if( pCsr->pCurrent==0 ){
4626      pCsr->bEof = 1;
4627    }
4628  }
4629
4630  if( pCsr->bEof==0 ){
4631    rc = zipfileCsrReadCDS(pCsr);
4632    if( rc==SQLITE_OK ){
4633      rc = zipfileCsrReadLFH(pCsr);
4634    }
4635  }
4636
4637  return rc;
4638}
4639
4640/*
4641** "Standard" MS-DOS time format:
4642**
4643**   File modification time:
4644**     Bits 00-04: seconds divided by 2
4645**     Bits 05-10: minute
4646**     Bits 11-15: hour
4647**   File modification date:
4648**     Bits 00-04: day
4649**     Bits 05-08: month (1-12)
4650**     Bits 09-15: years from 1980
4651*/
4652static time_t zipfileMtime(ZipfileCsr *pCsr){
4653  struct tm t;
4654  memset(&t, 0, sizeof(t));
4655  t.tm_sec = (pCsr->cds.mTime & 0x1F)*2;
4656  t.tm_min = (pCsr->cds.mTime >> 5) & 0x2F;
4657  t.tm_hour = (pCsr->cds.mTime >> 11) & 0x1F;
4658
4659  t.tm_mday = (pCsr->cds.mDate & 0x1F);
4660  t.tm_mon = ((pCsr->cds.mDate >> 5) & 0x0F) - 1;
4661  t.tm_year = 80 + ((pCsr->cds.mDate >> 9) & 0x7F);
4662
4663  return mktime(&t);
4664}
4665
4666static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mTime){
4667  time_t t = (time_t)mTime;
4668  struct tm res;
4669
4670#if !defined(_WIN32) && !defined(WIN32)
4671  localtime_r(&t, &res);
4672#else
4673  memcpy(&res, localtime(&t), sizeof(struct tm));
4674#endif
4675
4676  pCds->mTime = (u16)(
4677    (res.tm_sec / 2) +
4678    (res.tm_min << 5) +
4679    (res.tm_hour << 11));
4680
4681  pCds->mDate = (u16)(
4682    (res.tm_mday-1) +
4683    ((res.tm_mon+1) << 5) +
4684    ((res.tm_year-80) << 9));
4685}
4686
4687static void zipfileInflate(
4688  sqlite3_context *pCtx,          /* Store error here, if any */
4689  const u8 *aIn,                  /* Compressed data */
4690  int nIn,                        /* Size of buffer aIn[] in bytes */
4691  int nOut                        /* Expected output size */
4692){
4693  u8 *aRes = sqlite3_malloc(nOut);
4694  if( aRes==0 ){
4695    sqlite3_result_error_nomem(pCtx);
4696  }else{
4697    int err;
4698    z_stream str;
4699    memset(&str, 0, sizeof(str));
4700
4701    str.next_in = (Byte*)aIn;
4702    str.avail_in = nIn;
4703    str.next_out = (Byte*)aRes;
4704    str.avail_out = nOut;
4705
4706    err = inflateInit2(&str, -15);
4707    if( err!=Z_OK ){
4708      zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
4709    }else{
4710      err = inflate(&str, Z_NO_FLUSH);
4711      if( err!=Z_STREAM_END ){
4712        zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
4713      }else{
4714        sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT);
4715      }
4716    }
4717    sqlite3_free(aRes);
4718    inflateEnd(&str);
4719  }
4720}
4721
4722static int zipfileDeflate(
4723  ZipfileTab *pTab,               /* Set error message here */
4724  const u8 *aIn, int nIn,         /* Input */
4725  u8 **ppOut, int *pnOut          /* Output */
4726){
4727  int nAlloc = (int)compressBound(nIn);
4728  u8 *aOut;
4729  int rc = SQLITE_OK;
4730
4731  aOut = (u8*)sqlite3_malloc(nAlloc);
4732  if( aOut==0 ){
4733    rc = SQLITE_NOMEM;
4734  }else{
4735    int res;
4736    z_stream str;
4737    memset(&str, 0, sizeof(str));
4738    str.next_in = (Bytef*)aIn;
4739    str.avail_in = nIn;
4740    str.next_out = aOut;
4741    str.avail_out = nAlloc;
4742
4743    deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
4744    res = deflate(&str, Z_FINISH);
4745
4746    if( res==Z_STREAM_END ){
4747      *ppOut = aOut;
4748      *pnOut = (int)str.total_out;
4749    }else{
4750      sqlite3_free(aOut);
4751      pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error");
4752      rc = SQLITE_ERROR;
4753    }
4754    deflateEnd(&str);
4755  }
4756
4757  return rc;
4758}
4759
4760
4761/*
4762** Return values of columns for the row at which the series_cursor
4763** is currently pointing.
4764*/
4765static int zipfileColumn(
4766  sqlite3_vtab_cursor *cur,   /* The cursor */
4767  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
4768  int i                       /* Which column to return */
4769){
4770  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4771  int rc = SQLITE_OK;
4772  switch( i ){
4773    case 0:   /* name */
4774      sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
4775      break;
4776    case 1:   /* mode */
4777      /* TODO: Whether or not the following is correct surely depends on
4778      ** the platform on which the archive was created.  */
4779      sqlite3_result_int(ctx, pCsr->cds.iExternalAttr >> 16);
4780      break;
4781    case 2: { /* mtime */
4782      if( pCsr->flags & ZIPFILE_MTIME_VALID ){
4783        sqlite3_result_int64(ctx, pCsr->mTime);
4784      }else{
4785        sqlite3_result_int64(ctx, zipfileMtime(pCsr));
4786      }
4787      break;
4788    }
4789    case 3: { /* sz */
4790      if( sqlite3_vtab_nochange(ctx)==0 ){
4791        sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
4792      }
4793      break;
4794    }
4795    case 4:   /* rawdata */
4796      if( sqlite3_vtab_nochange(ctx) ) break;
4797    case 5: { /* data */
4798      if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
4799        int sz = pCsr->cds.szCompressed;
4800        int szFinal = pCsr->cds.szUncompressed;
4801        if( szFinal>0 ){
4802          u8 *aBuf = sqlite3_malloc(sz);
4803          if( aBuf==0 ){
4804            rc = SQLITE_NOMEM;
4805          }else{
4806            FILE *pFile = zipfileGetFd(pCsr);
4807            rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff,
4808                &pCsr->base.pVtab->zErrMsg
4809            );
4810          }
4811          if( rc==SQLITE_OK ){
4812            if( i==5 && pCsr->cds.iCompression ){
4813              zipfileInflate(ctx, aBuf, sz, szFinal);
4814            }else{
4815              sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
4816            }
4817            sqlite3_free(aBuf);
4818          }
4819        }else{
4820          /* Figure out if this is a directory or a zero-sized file. Consider
4821          ** it to be a directory either if the mode suggests so, or if
4822          ** the final character in the name is '/'.  */
4823          u32 mode = pCsr->cds.iExternalAttr >> 16;
4824          if( !(mode & S_IFDIR) && pCsr->cds.zFile[pCsr->cds.nFile-1]!='/' ){
4825            sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
4826          }
4827        }
4828      }
4829      break;
4830    }
4831    case 6:   /* method */
4832      sqlite3_result_int(ctx, pCsr->cds.iCompression);
4833      break;
4834    case 7:   /* z */
4835      sqlite3_result_int64(ctx, pCsr->iId);
4836      break;
4837  }
4838
4839  return rc;
4840}
4841
4842/*
4843** Return the rowid for the current row.
4844*/
4845static int zipfileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
4846  assert( 0 );
4847  return SQLITE_OK;
4848}
4849
4850/*
4851** Return TRUE if the cursor has been moved off of the last
4852** row of output.
4853*/
4854static int zipfileEof(sqlite3_vtab_cursor *cur){
4855  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4856  return pCsr->bEof;
4857}
4858
4859/*
4860*/
4861static int zipfileReadEOCD(
4862  ZipfileTab *pTab,               /* Return errors here */
4863  FILE *pFile,                    /* Read from this file */
4864  ZipfileEOCD *pEOCD              /* Object to populate */
4865){
4866  u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
4867  i64 szFile;                     /* Total size of file in bytes */
4868  int nRead;                      /* Bytes to read from file */
4869  i64 iOff;                       /* Offset to read from */
4870  int rc;
4871
4872  fseek(pFile, 0, SEEK_END);
4873  szFile = (i64)ftell(pFile);
4874  if( szFile==0 ){
4875    memset(pEOCD, 0, sizeof(ZipfileEOCD));
4876    return SQLITE_OK;
4877  }
4878  nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
4879  iOff = szFile - nRead;
4880
4881  rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
4882  if( rc==SQLITE_OK ){
4883    int i;
4884
4885    /* Scan backwards looking for the signature bytes */
4886    for(i=nRead-20; i>=0; i--){
4887      if( aRead[i]==0x50 && aRead[i+1]==0x4b
4888       && aRead[i+2]==0x05 && aRead[i+3]==0x06
4889      ){
4890        break;
4891      }
4892    }
4893    if( i<0 ){
4894      pTab->base.zErrMsg = sqlite3_mprintf(
4895          "cannot find end of central directory record"
4896      );
4897      return SQLITE_ERROR;
4898    }
4899
4900    aRead += i+4;
4901    pEOCD->iDisk = zipfileRead16(aRead);
4902    pEOCD->iFirstDisk = zipfileRead16(aRead);
4903    pEOCD->nEntry = zipfileRead16(aRead);
4904    pEOCD->nEntryTotal = zipfileRead16(aRead);
4905    pEOCD->nSize = zipfileRead32(aRead);
4906    pEOCD->iOffset = zipfileRead32(aRead);
4907
4908#if 0
4909    printf("iDisk=%d  iFirstDisk=%d  nEntry=%d  "
4910           "nEntryTotal=%d  nSize=%d  iOffset=%d",
4911           (int)pEOCD->iDisk, (int)pEOCD->iFirstDisk, (int)pEOCD->nEntry,
4912           (int)pEOCD->nEntryTotal, (int)pEOCD->nSize, (int)pEOCD->iOffset
4913    );
4914#endif
4915  }
4916
4917  return SQLITE_OK;
4918}
4919
4920/*
4921** xFilter callback.
4922*/
4923static int zipfileFilter(
4924  sqlite3_vtab_cursor *cur,
4925  int idxNum, const char *idxStr,
4926  int argc, sqlite3_value **argv
4927){
4928  ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
4929  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4930  const char *zFile;              /* Zip file to scan */
4931  int rc = SQLITE_OK;             /* Return Code */
4932
4933  zipfileResetCursor(pCsr);
4934
4935  if( pTab->zFile ){
4936    zFile = pTab->zFile;
4937  }else if( idxNum==0 ){
4938    /* Error. This is an eponymous virtual table and the user has not
4939    ** supplied a file name. */
4940    zipfileSetErrmsg(pCsr, "table function zipfile() requires an argument");
4941    return SQLITE_ERROR;
4942  }else{
4943    zFile = (const char*)sqlite3_value_text(argv[0]);
4944  }
4945
4946  if( pTab->pWriteFd==0 ){
4947    pCsr->pFile = fopen(zFile, "rb");
4948    if( pCsr->pFile==0 ){
4949      zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
4950      rc = SQLITE_ERROR;
4951    }else{
4952      rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
4953      if( rc==SQLITE_OK ){
4954        if( pCsr->eocd.nEntry==0 ){
4955          pCsr->bEof = 1;
4956        }else{
4957          pCsr->iNextOff = pCsr->eocd.iOffset;
4958          rc = zipfileNext(cur);
4959        }
4960      }
4961    }
4962  }else{
4963    ZipfileEntry e;
4964    memset(&e, 0, sizeof(e));
4965    e.pNext = pTab->pFirstEntry;
4966    pCsr->pCurrent = &e;
4967    rc = zipfileNext(cur);
4968    assert( pCsr->pCurrent!=&e );
4969  }
4970
4971  return rc;
4972}
4973
4974/*
4975** xBestIndex callback.
4976*/
4977static int zipfileBestIndex(
4978  sqlite3_vtab *tab,
4979  sqlite3_index_info *pIdxInfo
4980){
4981  int i;
4982
4983  for(i=0; i<pIdxInfo->nConstraint; i++){
4984    const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
4985    if( pCons->usable==0 ) continue;
4986    if( pCons->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
4987    if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
4988    break;
4989  }
4990
4991  if( i<pIdxInfo->nConstraint ){
4992    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
4993    pIdxInfo->aConstraintUsage[i].omit = 1;
4994    pIdxInfo->estimatedCost = 1000.0;
4995    pIdxInfo->idxNum = 1;
4996  }else{
4997    pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
4998    pIdxInfo->idxNum = 0;
4999  }
5000
5001  return SQLITE_OK;
5002}
5003
5004/*
5005** Add object pNew to the end of the linked list that begins at
5006** ZipfileTab.pFirstEntry and ends with pLastEntry.
5007*/
5008static void zipfileAddEntry(
5009  ZipfileTab *pTab,
5010  ZipfileEntry *pBefore,
5011  ZipfileEntry *pNew
5012){
5013  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5014  assert( pNew->pNext==0 );
5015  if( pBefore==0 ){
5016    if( pTab->pFirstEntry==0 ){
5017      pTab->pFirstEntry = pTab->pLastEntry = pNew;
5018    }else{
5019      assert( pTab->pLastEntry->pNext==0 );
5020      pTab->pLastEntry->pNext = pNew;
5021      pTab->pLastEntry = pNew;
5022    }
5023  }else{
5024    ZipfileEntry **pp;
5025    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5026    pNew->pNext = pBefore;
5027    *pp = pNew;
5028  }
5029}
5030
5031static int zipfileLoadDirectory(ZipfileTab *pTab){
5032  ZipfileEOCD eocd;
5033  int rc;
5034
5035  rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
5036  if( rc==SQLITE_OK && eocd.nEntry>0 ){
5037    int i;
5038    int iOff = 0;
5039    u8 *aBuf = sqlite3_malloc(eocd.nSize);
5040    if( aBuf==0 ){
5041      rc = SQLITE_NOMEM;
5042    }else{
5043      rc = zipfileReadData(
5044          pTab->pWriteFd, aBuf, eocd.nSize, eocd.iOffset, &pTab->base.zErrMsg
5045      );
5046    }
5047
5048    for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5049      u16 nFile;
5050      u16 nExtra;
5051      u16 nComment;
5052      ZipfileEntry *pNew;
5053      u8 *aRec = &aBuf[iOff];
5054
5055      nFile = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF]);
5056      nExtra = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+2]);
5057      nComment = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+4]);
5058
5059      pNew = sqlite3_malloc(
5060          sizeof(ZipfileEntry)
5061        + nFile+1
5062        + ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment
5063      );
5064      if( pNew==0 ){
5065        rc = SQLITE_NOMEM;
5066      }else{
5067        memset(pNew, 0, sizeof(ZipfileEntry));
5068        pNew->zPath = (char*)&pNew[1];
5069        memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
5070        pNew->zPath[nFile] = '\0';
5071        pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
5072        pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5073        memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
5074        zipfileAddEntry(pTab, 0, pNew);
5075      }
5076
5077      iOff += ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5078    }
5079
5080    sqlite3_free(aBuf);
5081  }
5082
5083  return rc;
5084}
5085
5086static ZipfileEntry *zipfileNewEntry(
5087  ZipfileCDS *pCds,               /* Values for fixed size part of CDS */
5088  const char *zPath,              /* Path for new entry */
5089  int nPath,                      /* strlen(zPath) */
5090  u32 mTime                       /* Modification time (or 0) */
5091){
5092  u8 *aWrite;
5093  ZipfileEntry *pNew;
5094  pCds->nFile = (u16)nPath;
5095  pCds->nExtra = mTime ? 9 : 0;
5096  pNew = (ZipfileEntry*)sqlite3_malloc(
5097    sizeof(ZipfileEntry) +
5098    nPath+1 +
5099    ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra
5100  );
5101
5102  if( pNew ){
5103    memset(pNew, 0, sizeof(ZipfileEntry));
5104    pNew->zPath = (char*)&pNew[1];
5105    pNew->aCdsEntry = (u8*)&pNew->zPath[nPath+1];
5106    pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra;
5107    memcpy(pNew->zPath, zPath, nPath+1);
5108
5109    aWrite = pNew->aCdsEntry;
5110    zipfileWrite32(aWrite, ZIPFILE_SIGNATURE_CDS);
5111    zipfileWrite16(aWrite, pCds->iVersionMadeBy);
5112    zipfileWrite16(aWrite, pCds->iVersionExtract);
5113    zipfileWrite16(aWrite, pCds->flags);
5114    zipfileWrite16(aWrite, pCds->iCompression);
5115    zipfileWrite16(aWrite, pCds->mTime);
5116    zipfileWrite16(aWrite, pCds->mDate);
5117    zipfileWrite32(aWrite, pCds->crc32);
5118    zipfileWrite32(aWrite, pCds->szCompressed);
5119    zipfileWrite32(aWrite, pCds->szUncompressed);
5120    zipfileWrite16(aWrite, pCds->nFile);
5121    zipfileWrite16(aWrite, pCds->nExtra);
5122    zipfileWrite16(aWrite, pCds->nComment);      assert( pCds->nComment==0 );
5123    zipfileWrite16(aWrite, pCds->iDiskStart);
5124    zipfileWrite16(aWrite, pCds->iInternalAttr);
5125    zipfileWrite32(aWrite, pCds->iExternalAttr);
5126    zipfileWrite32(aWrite, pCds->iOffset);
5127    assert( aWrite==&pNew->aCdsEntry[ZIPFILE_CDS_FIXED_SZ] );
5128    memcpy(aWrite, zPath, nPath);
5129    if( pCds->nExtra ){
5130      aWrite += nPath;
5131      zipfileWrite16(aWrite, ZIPFILE_EXTRA_TIMESTAMP);
5132      zipfileWrite16(aWrite, 5);
5133      *aWrite++ = 0x01;
5134      zipfileWrite32(aWrite, mTime);
5135    }
5136  }
5137
5138  return pNew;
5139}
5140
5141static int zipfileAppendEntry(
5142  ZipfileTab *pTab,
5143  ZipfileCDS *pCds,
5144  const char *zPath,              /* Path for new entry */
5145  int nPath,                      /* strlen(zPath) */
5146  const u8 *pData,
5147  int nData,
5148  u32 mTime
5149){
5150  u8 *aBuf = pTab->aBuffer;
5151  int rc;
5152
5153  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_LFH);
5154  zipfileWrite16(aBuf, pCds->iVersionExtract);
5155  zipfileWrite16(aBuf, pCds->flags);
5156  zipfileWrite16(aBuf, pCds->iCompression);
5157  zipfileWrite16(aBuf, pCds->mTime);
5158  zipfileWrite16(aBuf, pCds->mDate);
5159  zipfileWrite32(aBuf, pCds->crc32);
5160  zipfileWrite32(aBuf, pCds->szCompressed);
5161  zipfileWrite32(aBuf, pCds->szUncompressed);
5162  zipfileWrite16(aBuf, (u16)nPath);
5163  zipfileWrite16(aBuf, pCds->nExtra);
5164  assert( aBuf==&pTab->aBuffer[ZIPFILE_LFH_FIXED_SZ] );
5165  rc = zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5166  if( rc==SQLITE_OK ){
5167    rc = zipfileAppendData(pTab, (const u8*)zPath, nPath);
5168  }
5169
5170  if( rc==SQLITE_OK && pCds->nExtra ){
5171    aBuf = pTab->aBuffer;
5172    zipfileWrite16(aBuf, ZIPFILE_EXTRA_TIMESTAMP);
5173    zipfileWrite16(aBuf, 5);
5174    *aBuf++ = 0x01;
5175    zipfileWrite32(aBuf, mTime);
5176    rc = zipfileAppendData(pTab, pTab->aBuffer, 9);
5177  }
5178
5179  if( rc==SQLITE_OK ){
5180    rc = zipfileAppendData(pTab, pData, nData);
5181  }
5182
5183  return rc;
5184}
5185
5186static int zipfileGetMode(
5187  ZipfileTab *pTab,
5188  sqlite3_value *pVal,
5189  u32 defaultMode,                /* Value to use if pVal IS NULL */
5190  u32 *pMode
5191){
5192  const char *z = (const char*)sqlite3_value_text(pVal);
5193  u32 mode = 0;
5194  if( z==0 ){
5195    mode = defaultMode;
5196  }else if( z[0]>='0' && z[0]<='9' ){
5197    mode = (unsigned int)sqlite3_value_int(pVal);
5198  }else{
5199    const char zTemplate[11] = "-rwxrwxrwx";
5200    int i;
5201    if( strlen(z)!=10 ) goto parse_error;
5202    switch( z[0] ){
5203      case '-': mode |= S_IFREG; break;
5204      case 'd': mode |= S_IFDIR; break;
5205#if !defined(_WIN32) && !defined(WIN32)
5206      case 'l': mode |= S_IFLNK; break;
5207#endif
5208      default: goto parse_error;
5209    }
5210    for(i=1; i<10; i++){
5211      if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5212      else if( z[i]!='-' ) goto parse_error;
5213    }
5214  }
5215  *pMode = mode;
5216  return SQLITE_OK;
5217
5218 parse_error:
5219  pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5220  return SQLITE_ERROR;
5221}
5222
5223/*
5224** Both (const char*) arguments point to nul-terminated strings. Argument
5225** nB is the value of strlen(zB). This function returns 0 if the strings are
5226** identical, ignoring any trailing '/' character in either path.  */
5227static int zipfileComparePath(const char *zA, const char *zB, int nB){
5228  int nA = (int)strlen(zA);
5229  if( zA[nA-1]=='/' ) nA--;
5230  if( zB[nB-1]=='/' ) nB--;
5231  if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5232  return 1;
5233}
5234
5235/*
5236** xUpdate method.
5237*/
5238static int zipfileUpdate(
5239  sqlite3_vtab *pVtab,
5240  int nVal,
5241  sqlite3_value **apVal,
5242  sqlite_int64 *pRowid
5243){
5244  ZipfileTab *pTab = (ZipfileTab*)pVtab;
5245  int rc = SQLITE_OK;             /* Return Code */
5246  ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
5247
5248  u32 mode = 0;                   /* Mode for new entry */
5249  i64 mTime = 0;                  /* Modification time for new entry */
5250  i64 sz = 0;                     /* Uncompressed size */
5251  const char *zPath = 0;          /* Path for new entry */
5252  int nPath = 0;                  /* strlen(zPath) */
5253  const u8 *pData = 0;            /* Pointer to buffer containing content */
5254  int nData = 0;                  /* Size of pData buffer in bytes */
5255  int iMethod = 0;                /* Compression method for new entry */
5256  u8 *pFree = 0;                  /* Free this */
5257  char *zFree = 0;                /* Also free this */
5258  ZipfileCDS cds;                 /* New Central Directory Structure entry */
5259  ZipfileEntry *pOld = 0;
5260  int bIsDir = 0;
5261  u32 iCrc32 = 0;
5262
5263  assert( pTab->zFile );
5264  assert( pTab->pWriteFd );
5265
5266  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5267    const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5268    int nDelete = (int)strlen(zDelete);
5269    for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5270      if( pOld->bDeleted ) continue;
5271      if( zipfileComparePath(pOld->zPath, zDelete, nDelete)==0 ){
5272        pOld->bDeleted = 1;
5273        break;
5274      }
5275      assert( pOld->pNext );
5276    }
5277    if( nVal==1 ) return SQLITE_OK;
5278  }
5279
5280  /* Check that "sz" and "rawdata" are both NULL: */
5281  if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
5282   || sqlite3_value_type(apVal[6])!=SQLITE_NULL
5283  ){
5284    rc = SQLITE_CONSTRAINT;
5285  }
5286
5287  if( rc==SQLITE_OK ){
5288    if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5289      /* data=NULL. A directory */
5290      bIsDir = 1;
5291    }else{
5292      /* Value specified for "data", and possibly "method". This must be
5293      ** a regular file or a symlink. */
5294      const u8 *aIn = sqlite3_value_blob(apVal[7]);
5295      int nIn = sqlite3_value_bytes(apVal[7]);
5296      int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5297
5298      iMethod = sqlite3_value_int(apVal[8]);
5299      sz = nIn;
5300      pData = aIn;
5301      nData = nIn;
5302      if( iMethod!=0 && iMethod!=8 ){
5303        rc = SQLITE_CONSTRAINT;
5304      }else{
5305        if( bAuto || iMethod ){
5306          int nCmp;
5307          rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nCmp);
5308          if( rc==SQLITE_OK ){
5309            if( iMethod || nCmp<nIn ){
5310              iMethod = 8;
5311              pData = pFree;
5312              nData = nCmp;
5313            }
5314          }
5315        }
5316        iCrc32 = crc32(0, aIn, nIn);
5317      }
5318    }
5319  }
5320
5321  if( rc==SQLITE_OK ){
5322    rc = zipfileGetMode(pTab, apVal[3],
5323        (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode
5324    );
5325    if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){
5326      /* The "mode" attribute is a directory, but data has been specified.
5327      ** Or vice-versa - no data but "mode" is a file or symlink.  */
5328      rc = SQLITE_CONSTRAINT;
5329    }
5330  }
5331
5332  if( rc==SQLITE_OK ){
5333    zPath = (const char*)sqlite3_value_text(apVal[2]);
5334    nPath = (int)strlen(zPath);
5335    if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){
5336      mTime = (sqlite3_int64)time(0);
5337    }else{
5338      mTime = sqlite3_value_int64(apVal[4]);
5339    }
5340  }
5341
5342  if( rc==SQLITE_OK && bIsDir ){
5343    /* For a directory, check that the last character in the path is a
5344    ** '/'. This appears to be required for compatibility with info-zip
5345    ** (the unzip command on unix). It does not create directories
5346    ** otherwise.  */
5347    if( zPath[nPath-1]!='/' ){
5348      zFree = sqlite3_mprintf("%s/", zPath);
5349      if( zFree==0 ){ rc = SQLITE_NOMEM; }
5350      zPath = (const char*)zFree;
5351      nPath++;
5352    }
5353  }
5354
5355  /* Check that we're not inserting a duplicate entry */
5356  if( rc==SQLITE_OK ){
5357    ZipfileEntry *p;
5358    for(p=pTab->pFirstEntry; p; p=p->pNext){
5359      if( p->bDeleted ) continue;
5360      if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
5361        rc = SQLITE_CONSTRAINT;
5362        break;
5363      }
5364    }
5365  }
5366
5367  if( rc==SQLITE_OK ){
5368    /* Create the new CDS record. */
5369    memset(&cds, 0, sizeof(cds));
5370    cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5371    cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5372    cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5373    cds.iCompression = (u16)iMethod;
5374    zipfileMtimeToDos(&cds, (u32)mTime);
5375    cds.crc32 = iCrc32;
5376    cds.szCompressed = nData;
5377    cds.szUncompressed = (u32)sz;
5378    cds.iExternalAttr = (mode<<16);
5379    cds.iOffset = (u32)pTab->szCurrent;
5380    pNew = zipfileNewEntry(&cds, zPath, nPath, (u32)mTime);
5381    if( pNew==0 ){
5382      rc = SQLITE_NOMEM;
5383    }else{
5384      zipfileAddEntry(pTab, pOld, pNew);
5385    }
5386  }
5387
5388  /* Append the new header+file to the archive */
5389  if( rc==SQLITE_OK ){
5390    rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
5391  }
5392
5393  if( rc!=SQLITE_OK && pOld ){
5394    pOld->bDeleted = 0;
5395  }
5396  sqlite3_free(pFree);
5397  sqlite3_free(zFree);
5398  return rc;
5399}
5400
5401static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5402  u8 *aBuf = pTab->aBuffer;
5403
5404  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_EOCD);
5405  zipfileWrite16(aBuf, p->iDisk);
5406  zipfileWrite16(aBuf, p->iFirstDisk);
5407  zipfileWrite16(aBuf, p->nEntry);
5408  zipfileWrite16(aBuf, p->nEntryTotal);
5409  zipfileWrite32(aBuf, p->nSize);
5410  zipfileWrite32(aBuf, p->iOffset);
5411  zipfileWrite16(aBuf, 0);        /* Size of trailing comment in bytes*/
5412
5413  assert( (aBuf-pTab->aBuffer)==22 );
5414  return zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5415}
5416
5417static void zipfileCleanupTransaction(ZipfileTab *pTab){
5418  ZipfileEntry *pEntry;
5419  ZipfileEntry *pNext;
5420
5421  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
5422    pNext = pEntry->pNext;
5423    sqlite3_free(pEntry);
5424  }
5425  pTab->pFirstEntry = 0;
5426  pTab->pLastEntry = 0;
5427  fclose(pTab->pWriteFd);
5428  pTab->pWriteFd = 0;
5429  pTab->szCurrent = 0;
5430  pTab->szOrig = 0;
5431}
5432
5433static int zipfileBegin(sqlite3_vtab *pVtab){
5434  ZipfileTab *pTab = (ZipfileTab*)pVtab;
5435  int rc = SQLITE_OK;
5436
5437  assert( pTab->pWriteFd==0 );
5438
5439  /* This table is only writable if a default archive path was specified
5440  ** as part of the CREATE VIRTUAL TABLE statement. */
5441  if( pTab->zFile==0 ){
5442    pTab->base.zErrMsg = sqlite3_mprintf(
5443        "zipfile: writing requires a default archive"
5444    );
5445    return SQLITE_ERROR;
5446  }
5447
5448  /* Open a write fd on the file. Also load the entire central directory
5449  ** structure into memory. During the transaction any new file data is
5450  ** appended to the archive file, but the central directory is accumulated
5451  ** in main-memory until the transaction is committed.  */
5452  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5453  if( pTab->pWriteFd==0 ){
5454    pTab->base.zErrMsg = sqlite3_mprintf(
5455        "zipfile: failed to open file %s for writing", pTab->zFile
5456    );
5457    rc = SQLITE_ERROR;
5458  }else{
5459    fseek(pTab->pWriteFd, 0, SEEK_END);
5460    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5461    rc = zipfileLoadDirectory(pTab);
5462  }
5463
5464  if( rc!=SQLITE_OK ){
5465    zipfileCleanupTransaction(pTab);
5466  }
5467
5468  return rc;
5469}
5470
5471static int zipfileCommit(sqlite3_vtab *pVtab){
5472  ZipfileTab *pTab = (ZipfileTab*)pVtab;
5473  int rc = SQLITE_OK;
5474  if( pTab->pWriteFd ){
5475    i64 iOffset = pTab->szCurrent;
5476    ZipfileEntry *p;
5477    ZipfileEOCD eocd;
5478    int nEntry = 0;
5479
5480    /* Write out all undeleted entries */
5481    for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5482      if( p->bDeleted ) continue;
5483      rc = zipfileAppendData(pTab, p->aCdsEntry, p->nCdsEntry);
5484      nEntry++;
5485    }
5486
5487    /* Write out the EOCD record */
5488    eocd.iDisk = 0;
5489    eocd.iFirstDisk = 0;
5490    eocd.nEntry = (u16)nEntry;
5491    eocd.nEntryTotal = (u16)nEntry;
5492    eocd.nSize = (u32)(pTab->szCurrent - iOffset);
5493    eocd.iOffset = (u32)iOffset;
5494    rc = zipfileAppendEOCD(pTab, &eocd);
5495
5496    zipfileCleanupTransaction(pTab);
5497  }
5498  return rc;
5499}
5500
5501static int zipfileRollback(sqlite3_vtab *pVtab){
5502  return zipfileCommit(pVtab);
5503}
5504
5505static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
5506  ZipfileCsr *pCsr;
5507  for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5508    if( iId==pCsr->iId ) break;
5509  }
5510  return pCsr;
5511}
5512
5513static void zipfileFunctionCds(
5514  sqlite3_context *context,
5515  int argc,
5516  sqlite3_value **argv
5517){
5518  ZipfileCsr *pCsr;
5519  ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
5520  assert( argc>0 );
5521
5522  pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
5523  if( pCsr ){
5524    ZipfileCDS *p = &pCsr->cds;
5525    char *zRes = sqlite3_mprintf("{"
5526        "\"version-made-by\" : %u, "
5527        "\"version-to-extract\" : %u, "
5528        "\"flags\" : %u, "
5529        "\"compression\" : %u, "
5530        "\"time\" : %u, "
5531        "\"date\" : %u, "
5532        "\"crc32\" : %u, "
5533        "\"compressed-size\" : %u, "
5534        "\"uncompressed-size\" : %u, "
5535        "\"file-name-length\" : %u, "
5536        "\"extra-field-length\" : %u, "
5537        "\"file-comment-length\" : %u, "
5538        "\"disk-number-start\" : %u, "
5539        "\"internal-attr\" : %u, "
5540        "\"external-attr\" : %u, "
5541        "\"offset\" : %u }",
5542        (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
5543        (u32)p->flags, (u32)p->iCompression,
5544        (u32)p->mTime, (u32)p->mDate,
5545        (u32)p->crc32, (u32)p->szCompressed,
5546        (u32)p->szUncompressed, (u32)p->nFile,
5547        (u32)p->nExtra, (u32)p->nComment,
5548        (u32)p->iDiskStart, (u32)p->iInternalAttr,
5549        (u32)p->iExternalAttr, (u32)p->iOffset
5550    );
5551
5552    if( zRes==0 ){
5553      sqlite3_result_error_nomem(context);
5554    }else{
5555      sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
5556      sqlite3_free(zRes);
5557    }
5558  }
5559}
5560
5561
5562/*
5563** xFindFunction method.
5564*/
5565static int zipfileFindFunction(
5566  sqlite3_vtab *pVtab,            /* Virtual table handle */
5567  int nArg,                       /* Number of SQL function arguments */
5568  const char *zName,              /* Name of SQL function */
5569  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
5570  void **ppArg                    /* OUT: User data for *pxFunc */
5571){
5572  if( nArg>0 ){
5573    if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5574      *pxFunc = zipfileFunctionCds;
5575      *ppArg = (void*)pVtab;
5576      return 1;
5577    }
5578  }
5579
5580  return 0;
5581}
5582
5583/*
5584** Register the "zipfile" virtual table.
5585*/
5586static int zipfileRegister(sqlite3 *db){
5587  static sqlite3_module zipfileModule = {
5588    1,                         /* iVersion */
5589    zipfileConnect,            /* xCreate */
5590    zipfileConnect,            /* xConnect */
5591    zipfileBestIndex,          /* xBestIndex */
5592    zipfileDisconnect,         /* xDisconnect */
5593    zipfileDisconnect,         /* xDestroy */
5594    zipfileOpen,               /* xOpen - open a cursor */
5595    zipfileClose,              /* xClose - close a cursor */
5596    zipfileFilter,             /* xFilter - configure scan constraints */
5597    zipfileNext,               /* xNext - advance a cursor */
5598    zipfileEof,                /* xEof - check for end of scan */
5599    zipfileColumn,             /* xColumn - read data */
5600    zipfileRowid,              /* xRowid - read data */
5601    zipfileUpdate,             /* xUpdate */
5602    zipfileBegin,              /* xBegin */
5603    0,                         /* xSync */
5604    zipfileCommit,             /* xCommit */
5605    zipfileRollback,           /* xRollback */
5606    zipfileFindFunction,       /* xFindMethod */
5607    0,                         /* xRename */
5608  };
5609
5610  int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
5611  if( rc==SQLITE_OK ){
5612    rc = sqlite3_overload_function(db, "zipfile_cds", -1);
5613  }
5614  return rc;
5615}
5616#else         /* SQLITE_OMIT_VIRTUALTABLE */
5617# define zipfileRegister(x) SQLITE_OK
5618#endif
5619
5620#ifdef _WIN32
5621
5622#endif
5623int sqlite3_zipfile_init(
5624  sqlite3 *db,
5625  char **pzErrMsg,
5626  const sqlite3_api_routines *pApi
5627){
5628  SQLITE_EXTENSION_INIT2(pApi);
5629  (void)pzErrMsg;  /* Unused parameter */
5630  return zipfileRegister(db);
5631}
5632
5633/************************* End ../ext/misc/zipfile.c ********************/
5634/************************* Begin ../ext/misc/sqlar.c ******************/
5635/*
5636** 2017-12-17
5637**
5638** The author disclaims copyright to this source code.  In place of
5639** a legal notice, here is a blessing:
5640**
5641**    May you do good and not evil.
5642**    May you find forgiveness for yourself and forgive others.
5643**    May you share freely, never taking more than you give.
5644**
5645******************************************************************************
5646**
5647** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
5648** for working with sqlar archives and used by the shell tool's built-in
5649** sqlar support.
5650*/
5651SQLITE_EXTENSION_INIT1
5652#include <zlib.h>
5653
5654/*
5655** Implementation of the "sqlar_compress(X)" SQL function.
5656**
5657** If the type of X is SQLITE_BLOB, and compressing that blob using
5658** zlib utility function compress() yields a smaller blob, return the
5659** compressed blob. Otherwise, return a copy of X.
5660**
5661** SQLar uses the "zlib format" for compressed content.  The zlib format
5662** contains a two-byte identification header and a four-byte checksum at
5663** the end.  This is different from ZIP which uses the raw deflate format.
5664**
5665** Future enhancements to SQLar might add support for new compression formats.
5666** If so, those new formats will be identified by alternative headers in the
5667** compressed data.
5668*/
5669static void sqlarCompressFunc(
5670  sqlite3_context *context,
5671  int argc,
5672  sqlite3_value **argv
5673){
5674  assert( argc==1 );
5675  if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5676    const Bytef *pData = sqlite3_value_blob(argv[0]);
5677    uLong nData = sqlite3_value_bytes(argv[0]);
5678    uLongf nOut = compressBound(nData);
5679    Bytef *pOut;
5680
5681    pOut = (Bytef*)sqlite3_malloc(nOut);
5682    if( pOut==0 ){
5683      sqlite3_result_error_nomem(context);
5684      return;
5685    }else{
5686      if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
5687        sqlite3_result_error(context, "error in compress()", -1);
5688      }else if( nOut<nData ){
5689        sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
5690      }else{
5691        sqlite3_result_value(context, argv[0]);
5692      }
5693      sqlite3_free(pOut);
5694    }
5695  }else{
5696    sqlite3_result_value(context, argv[0]);
5697  }
5698}
5699
5700/*
5701** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
5702**
5703** Parameter SZ is interpreted as an integer. If it is less than or
5704** equal to zero, then this function returns a copy of X. Or, if
5705** SZ is equal to the size of X when interpreted as a blob, also
5706** return a copy of X. Otherwise, decompress blob X using zlib
5707** utility function uncompress() and return the results (another
5708** blob).
5709*/
5710static void sqlarUncompressFunc(
5711  sqlite3_context *context,
5712  int argc,
5713  sqlite3_value **argv
5714){
5715  uLong nData;
5716  uLongf sz;
5717
5718  assert( argc==2 );
5719  sz = sqlite3_value_int(argv[1]);
5720
5721  if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
5722    sqlite3_result_value(context, argv[0]);
5723  }else{
5724    const Bytef *pData= sqlite3_value_blob(argv[0]);
5725    Bytef *pOut = sqlite3_malloc(sz);
5726    if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
5727      sqlite3_result_error(context, "error in uncompress()", -1);
5728    }else{
5729      sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
5730    }
5731    sqlite3_free(pOut);
5732  }
5733}
5734
5735
5736#ifdef _WIN32
5737
5738#endif
5739int sqlite3_sqlar_init(
5740  sqlite3 *db,
5741  char **pzErrMsg,
5742  const sqlite3_api_routines *pApi
5743){
5744  int rc = SQLITE_OK;
5745  SQLITE_EXTENSION_INIT2(pApi);
5746  (void)pzErrMsg;  /* Unused parameter */
5747  rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
5748                               sqlarCompressFunc, 0, 0);
5749  if( rc==SQLITE_OK ){
5750    rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
5751                                 sqlarUncompressFunc, 0, 0);
5752  }
5753  return rc;
5754}
5755
5756/************************* End ../ext/misc/sqlar.c ********************/
5757#endif
5758/************************* Begin ../ext/expert/sqlite3expert.h ******************/
5759/*
5760** 2017 April 07
5761**
5762** The author disclaims copyright to this source code.  In place of
5763** a legal notice, here is a blessing:
5764**
5765**    May you do good and not evil.
5766**    May you find forgiveness for yourself and forgive others.
5767**    May you share freely, never taking more than you give.
5768**
5769*************************************************************************
5770*/
5771
5772
5773
5774typedef struct sqlite3expert sqlite3expert;
5775
5776/*
5777** Create a new sqlite3expert object.
5778**
5779** If successful, a pointer to the new object is returned and (*pzErr) set
5780** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
5781** an English-language error message. In this case it is the responsibility
5782** of the caller to eventually free the error message buffer using
5783** sqlite3_free().
5784*/
5785sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
5786
5787/*
5788** Configure an sqlite3expert object.
5789**
5790** EXPERT_CONFIG_SAMPLE:
5791**   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
5792**   each candidate index. This involves scanning and sorting the entire
5793**   contents of each user database table once for each candidate index
5794**   associated with the table. For large databases, this can be
5795**   prohibitively slow. This option allows the sqlite3expert object to
5796**   be configured so that sqlite_stat1 data is instead generated based on a
5797**   subset of each table, or so that no sqlite_stat1 data is used at all.
5798**
5799**   A single integer argument is passed to this option. If the value is less
5800**   than or equal to zero, then no sqlite_stat1 data is generated or used by
5801**   the analysis - indexes are recommended based on the database schema only.
5802**   Or, if the value is 100 or greater, complete sqlite_stat1 data is
5803**   generated for each candidate index (this is the default). Finally, if the
5804**   value falls between 0 and 100, then it represents the percentage of user
5805**   table rows that should be considered when generating sqlite_stat1 data.
5806**
5807**   Examples:
5808**
5809**     // Do not generate any sqlite_stat1 data
5810**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
5811**
5812**     // Generate sqlite_stat1 data based on 10% of the rows in each table.
5813**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
5814*/
5815int sqlite3_expert_config(sqlite3expert *p, int op, ...);
5816
5817#define EXPERT_CONFIG_SAMPLE 1    /* int */
5818
5819/*
5820** Specify zero or more SQL statements to be included in the analysis.
5821**
5822** Buffer zSql must contain zero or more complete SQL statements. This
5823** function parses all statements contained in the buffer and adds them
5824** to the internal list of statements to analyze. If successful, SQLITE_OK
5825** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
5826** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
5827** may be set to point to an English language error message. In this case
5828** the caller is responsible for eventually freeing the error message buffer
5829** using sqlite3_free().
5830**
5831** If an error does occur while processing one of the statements in the
5832** buffer passed as the second argument, none of the statements in the
5833** buffer are added to the analysis.
5834**
5835** This function must be called before sqlite3_expert_analyze(). If a call
5836** to this function is made on an sqlite3expert object that has already
5837** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
5838** immediately and no statements are added to the analysis.
5839*/
5840int sqlite3_expert_sql(
5841  sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
5842  const char *zSql,               /* SQL statement(s) to add */
5843  char **pzErr                    /* OUT: Error message (if any) */
5844);
5845
5846
5847/*
5848** This function is called after the sqlite3expert object has been configured
5849** with all SQL statements using sqlite3_expert_sql() to actually perform
5850** the analysis. Once this function has been called, it is not possible to
5851** add further SQL statements to the analysis.
5852**
5853** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
5854** an error occurs, an SQLite error code is returned and (*pzErr) set to
5855** point to a buffer containing an English language error message. In this
5856** case it is the responsibility of the caller to eventually free the buffer
5857** using sqlite3_free().
5858**
5859** If an error does occur within this function, the sqlite3expert object
5860** is no longer useful for any purpose. At that point it is no longer
5861** possible to add further SQL statements to the object or to re-attempt
5862** the analysis. The sqlite3expert object must still be freed using a call
5863** sqlite3_expert_destroy().
5864*/
5865int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
5866
5867/*
5868** Return the total number of statements loaded using sqlite3_expert_sql().
5869** The total number of SQL statements may be different from the total number
5870** to calls to sqlite3_expert_sql().
5871*/
5872int sqlite3_expert_count(sqlite3expert*);
5873
5874/*
5875** Return a component of the report.
5876**
5877** This function is called after sqlite3_expert_analyze() to extract the
5878** results of the analysis. Each call to this function returns either a
5879** NULL pointer or a pointer to a buffer containing a nul-terminated string.
5880** The value passed as the third argument must be one of the EXPERT_REPORT_*
5881** #define constants defined below.
5882**
5883** For some EXPERT_REPORT_* parameters, the buffer returned contains
5884** information relating to a specific SQL statement. In these cases that
5885** SQL statement is identified by the value passed as the second argument.
5886** SQL statements are numbered from 0 in the order in which they are parsed.
5887** If an out-of-range value (less than zero or equal to or greater than the
5888** value returned by sqlite3_expert_count()) is passed as the second argument
5889** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
5890**
5891** EXPERT_REPORT_SQL:
5892**   Return the text of SQL statement iStmt.
5893**
5894** EXPERT_REPORT_INDEXES:
5895**   Return a buffer containing the CREATE INDEX statements for all recommended
5896**   indexes for statement iStmt. If there are no new recommeded indexes, NULL
5897**   is returned.
5898**
5899** EXPERT_REPORT_PLAN:
5900**   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
5901**   iStmt after the proposed indexes have been added to the database schema.
5902**
5903** EXPERT_REPORT_CANDIDATES:
5904**   Return a pointer to a buffer containing the CREATE INDEX statements
5905**   for all indexes that were tested (for all SQL statements). The iStmt
5906**   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
5907*/
5908const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
5909
5910/*
5911** Values for the third argument passed to sqlite3_expert_report().
5912*/
5913#define EXPERT_REPORT_SQL        1
5914#define EXPERT_REPORT_INDEXES    2
5915#define EXPERT_REPORT_PLAN       3
5916#define EXPERT_REPORT_CANDIDATES 4
5917
5918/*
5919** Free an (sqlite3expert*) handle and all associated resources. There
5920** should be one call to this function for each successful call to
5921** sqlite3-expert_new().
5922*/
5923void sqlite3_expert_destroy(sqlite3expert*);
5924
5925
5926
5927/************************* End ../ext/expert/sqlite3expert.h ********************/
5928/************************* Begin ../ext/expert/sqlite3expert.c ******************/
5929/*
5930** 2017 April 09
5931**
5932** The author disclaims copyright to this source code.  In place of
5933** a legal notice, here is a blessing:
5934**
5935**    May you do good and not evil.
5936**    May you find forgiveness for yourself and forgive others.
5937**    May you share freely, never taking more than you give.
5938**
5939*************************************************************************
5940*/
5941#include <assert.h>
5942#include <string.h>
5943#include <stdio.h>
5944
5945#ifndef SQLITE_OMIT_VIRTUALTABLE
5946
5947/* typedef sqlite3_int64 i64; */
5948/* typedef sqlite3_uint64 u64; */
5949
5950typedef struct IdxColumn IdxColumn;
5951typedef struct IdxConstraint IdxConstraint;
5952typedef struct IdxScan IdxScan;
5953typedef struct IdxStatement IdxStatement;
5954typedef struct IdxTable IdxTable;
5955typedef struct IdxWrite IdxWrite;
5956
5957#define STRLEN  (int)strlen
5958
5959/*
5960** A temp table name that we assume no user database will actually use.
5961** If this assumption proves incorrect triggers on the table with the
5962** conflicting name will be ignored.
5963*/
5964#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
5965
5966/*
5967** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
5968** any other type of single-ended range constraint on a column).
5969**
5970** pLink:
5971**   Used to temporarily link IdxConstraint objects into lists while
5972**   creating candidate indexes.
5973*/
5974struct IdxConstraint {
5975  char *zColl;                    /* Collation sequence */
5976  int bRange;                     /* True for range, false for eq */
5977  int iCol;                       /* Constrained table column */
5978  int bFlag;                      /* Used by idxFindCompatible() */
5979  int bDesc;                      /* True if ORDER BY <expr> DESC */
5980  IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
5981  IdxConstraint *pLink;           /* See above */
5982};
5983
5984/*
5985** A single scan of a single table.
5986*/
5987struct IdxScan {
5988  IdxTable *pTab;                 /* Associated table object */
5989  int iDb;                        /* Database containing table zTable */
5990  i64 covering;                   /* Mask of columns required for cov. index */
5991  IdxConstraint *pOrder;          /* ORDER BY columns */
5992  IdxConstraint *pEq;             /* List of == constraints */
5993  IdxConstraint *pRange;          /* List of < constraints */
5994  IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
5995};
5996
5997/*
5998** Information regarding a single database table. Extracted from
5999** "PRAGMA table_info" by function idxGetTableInfo().
6000*/
6001struct IdxColumn {
6002  char *zName;
6003  char *zColl;
6004  int iPk;
6005};
6006struct IdxTable {
6007  int nCol;
6008  char *zName;                    /* Table name */
6009  IdxColumn *aCol;
6010  IdxTable *pNext;                /* Next table in linked list of all tables */
6011};
6012
6013/*
6014** An object of the following type is created for each unique table/write-op
6015** seen. The objects are stored in a singly-linked list beginning at
6016** sqlite3expert.pWrite.
6017*/
6018struct IdxWrite {
6019  IdxTable *pTab;
6020  int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
6021  IdxWrite *pNext;
6022};
6023
6024/*
6025** Each statement being analyzed is represented by an instance of this
6026** structure.
6027*/
6028struct IdxStatement {
6029  int iId;                        /* Statement number */
6030  char *zSql;                     /* SQL statement */
6031  char *zIdx;                     /* Indexes */
6032  char *zEQP;                     /* Plan */
6033  IdxStatement *pNext;
6034};
6035
6036
6037/*
6038** A hash table for storing strings. With space for a payload string
6039** with each entry. Methods are:
6040**
6041**   idxHashInit()
6042**   idxHashClear()
6043**   idxHashAdd()
6044**   idxHashSearch()
6045*/
6046#define IDX_HASH_SIZE 1023
6047typedef struct IdxHashEntry IdxHashEntry;
6048typedef struct IdxHash IdxHash;
6049struct IdxHashEntry {
6050  char *zKey;                     /* nul-terminated key */
6051  char *zVal;                     /* nul-terminated value string */
6052  char *zVal2;                    /* nul-terminated value string 2 */
6053  IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
6054  IdxHashEntry *pNext;            /* Next entry in hash */
6055};
6056struct IdxHash {
6057  IdxHashEntry *pFirst;
6058  IdxHashEntry *aHash[IDX_HASH_SIZE];
6059};
6060
6061/*
6062** sqlite3expert object.
6063*/
6064struct sqlite3expert {
6065  int iSample;                    /* Percentage of tables to sample for stat1 */
6066  sqlite3 *db;                    /* User database */
6067  sqlite3 *dbm;                   /* In-memory db for this analysis */
6068  sqlite3 *dbv;                   /* Vtab schema for this analysis */
6069  IdxTable *pTable;               /* List of all IdxTable objects */
6070  IdxScan *pScan;                 /* List of scan objects */
6071  IdxWrite *pWrite;               /* List of write objects */
6072  IdxStatement *pStatement;       /* List of IdxStatement objects */
6073  int bRun;                       /* True once analysis has run */
6074  char **pzErrmsg;
6075  int rc;                         /* Error code from whereinfo hook */
6076  IdxHash hIdx;                   /* Hash containing all candidate indexes */
6077  char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
6078};
6079
6080
6081/*
6082** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
6083** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6084*/
6085static void *idxMalloc(int *pRc, int nByte){
6086  void *pRet;
6087  assert( *pRc==SQLITE_OK );
6088  assert( nByte>0 );
6089  pRet = sqlite3_malloc(nByte);
6090  if( pRet ){
6091    memset(pRet, 0, nByte);
6092  }else{
6093    *pRc = SQLITE_NOMEM;
6094  }
6095  return pRet;
6096}
6097
6098/*
6099** Initialize an IdxHash hash table.
6100*/
6101static void idxHashInit(IdxHash *pHash){
6102  memset(pHash, 0, sizeof(IdxHash));
6103}
6104
6105/*
6106** Reset an IdxHash hash table.
6107*/
6108static void idxHashClear(IdxHash *pHash){
6109  int i;
6110  for(i=0; i<IDX_HASH_SIZE; i++){
6111    IdxHashEntry *pEntry;
6112    IdxHashEntry *pNext;
6113    for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6114      pNext = pEntry->pHashNext;
6115      sqlite3_free(pEntry->zVal2);
6116      sqlite3_free(pEntry);
6117    }
6118  }
6119  memset(pHash, 0, sizeof(IdxHash));
6120}
6121
6122/*
6123** Return the index of the hash bucket that the string specified by the
6124** arguments to this function belongs.
6125*/
6126static int idxHashString(const char *z, int n){
6127  unsigned int ret = 0;
6128  int i;
6129  for(i=0; i<n; i++){
6130    ret += (ret<<3) + (unsigned char)(z[i]);
6131  }
6132  return (int)(ret % IDX_HASH_SIZE);
6133}
6134
6135/*
6136** If zKey is already present in the hash table, return non-zero and do
6137** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6138** the hash table passed as the second argument.
6139*/
6140static int idxHashAdd(
6141  int *pRc,
6142  IdxHash *pHash,
6143  const char *zKey,
6144  const char *zVal
6145){
6146  int nKey = STRLEN(zKey);
6147  int iHash = idxHashString(zKey, nKey);
6148  int nVal = (zVal ? STRLEN(zVal) : 0);
6149  IdxHashEntry *pEntry;
6150  assert( iHash>=0 );
6151  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6152    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6153      return 1;
6154    }
6155  }
6156  pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6157  if( pEntry ){
6158    pEntry->zKey = (char*)&pEntry[1];
6159    memcpy(pEntry->zKey, zKey, nKey);
6160    if( zVal ){
6161      pEntry->zVal = &pEntry->zKey[nKey+1];
6162      memcpy(pEntry->zVal, zVal, nVal);
6163    }
6164    pEntry->pHashNext = pHash->aHash[iHash];
6165    pHash->aHash[iHash] = pEntry;
6166
6167    pEntry->pNext = pHash->pFirst;
6168    pHash->pFirst = pEntry;
6169  }
6170  return 0;
6171}
6172
6173/*
6174** If zKey/nKey is present in the hash table, return a pointer to the
6175** hash-entry object.
6176*/
6177static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6178  int iHash;
6179  IdxHashEntry *pEntry;
6180  if( nKey<0 ) nKey = STRLEN(zKey);
6181  iHash = idxHashString(zKey, nKey);
6182  assert( iHash>=0 );
6183  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6184    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6185      return pEntry;
6186    }
6187  }
6188  return 0;
6189}
6190
6191/*
6192** If the hash table contains an entry with a key equal to the string
6193** passed as the final two arguments to this function, return a pointer
6194** to the payload string. Otherwise, if zKey/nKey is not present in the
6195** hash table, return NULL.
6196*/
6197static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6198  IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6199  if( pEntry ) return pEntry->zVal;
6200  return 0;
6201}
6202
6203/*
6204** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6205** variable to point to a copy of nul-terminated string zColl.
6206*/
6207static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6208  IdxConstraint *pNew;
6209  int nColl = STRLEN(zColl);
6210
6211  assert( *pRc==SQLITE_OK );
6212  pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6213  if( pNew ){
6214    pNew->zColl = (char*)&pNew[1];
6215    memcpy(pNew->zColl, zColl, nColl+1);
6216  }
6217  return pNew;
6218}
6219
6220/*
6221** An error associated with database handle db has just occurred. Pass
6222** the error message to callback function xOut.
6223*/
6224static void idxDatabaseError(
6225  sqlite3 *db,                    /* Database handle */
6226  char **pzErrmsg                 /* Write error here */
6227){
6228  *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6229}
6230
6231/*
6232** Prepare an SQL statement.
6233*/
6234static int idxPrepareStmt(
6235  sqlite3 *db,                    /* Database handle to compile against */
6236  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
6237  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
6238  const char *zSql                /* SQL statement to compile */
6239){
6240  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6241  if( rc!=SQLITE_OK ){
6242    *ppStmt = 0;
6243    idxDatabaseError(db, pzErrmsg);
6244  }
6245  return rc;
6246}
6247
6248/*
6249** Prepare an SQL statement using the results of a printf() formatting.
6250*/
6251static int idxPrintfPrepareStmt(
6252  sqlite3 *db,                    /* Database handle to compile against */
6253  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
6254  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
6255  const char *zFmt,               /* printf() format of SQL statement */
6256  ...                             /* Trailing printf() arguments */
6257){
6258  va_list ap;
6259  int rc;
6260  char *zSql;
6261  va_start(ap, zFmt);
6262  zSql = sqlite3_vmprintf(zFmt, ap);
6263  if( zSql==0 ){
6264    rc = SQLITE_NOMEM;
6265  }else{
6266    rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
6267    sqlite3_free(zSql);
6268  }
6269  va_end(ap);
6270  return rc;
6271}
6272
6273
6274/*************************************************************************
6275** Beginning of virtual table implementation.
6276*/
6277typedef struct ExpertVtab ExpertVtab;
6278struct ExpertVtab {
6279  sqlite3_vtab base;
6280  IdxTable *pTab;
6281  sqlite3expert *pExpert;
6282};
6283
6284typedef struct ExpertCsr ExpertCsr;
6285struct ExpertCsr {
6286  sqlite3_vtab_cursor base;
6287  sqlite3_stmt *pData;
6288};
6289
6290static char *expertDequote(const char *zIn){
6291  int n = STRLEN(zIn);
6292  char *zRet = sqlite3_malloc(n);
6293
6294  assert( zIn[0]=='\'' );
6295  assert( zIn[n-1]=='\'' );
6296
6297  if( zRet ){
6298    int iOut = 0;
6299    int iIn = 0;
6300    for(iIn=1; iIn<(n-1); iIn++){
6301      if( zIn[iIn]=='\'' ){
6302        assert( zIn[iIn+1]=='\'' );
6303        iIn++;
6304      }
6305      zRet[iOut++] = zIn[iIn];
6306    }
6307    zRet[iOut] = '\0';
6308  }
6309
6310  return zRet;
6311}
6312
6313/*
6314** This function is the implementation of both the xConnect and xCreate
6315** methods of the r-tree virtual table.
6316**
6317**   argv[0]   -> module name
6318**   argv[1]   -> database name
6319**   argv[2]   -> table name
6320**   argv[...] -> column names...
6321*/
6322static int expertConnect(
6323  sqlite3 *db,
6324  void *pAux,
6325  int argc, const char *const*argv,
6326  sqlite3_vtab **ppVtab,
6327  char **pzErr
6328){
6329  sqlite3expert *pExpert = (sqlite3expert*)pAux;
6330  ExpertVtab *p = 0;
6331  int rc;
6332
6333  if( argc!=4 ){
6334    *pzErr = sqlite3_mprintf("internal error!");
6335    rc = SQLITE_ERROR;
6336  }else{
6337    char *zCreateTable = expertDequote(argv[3]);
6338    if( zCreateTable ){
6339      rc = sqlite3_declare_vtab(db, zCreateTable);
6340      if( rc==SQLITE_OK ){
6341        p = idxMalloc(&rc, sizeof(ExpertVtab));
6342      }
6343      if( rc==SQLITE_OK ){
6344        p->pExpert = pExpert;
6345        p->pTab = pExpert->pTable;
6346        assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
6347      }
6348      sqlite3_free(zCreateTable);
6349    }else{
6350      rc = SQLITE_NOMEM;
6351    }
6352  }
6353
6354  *ppVtab = (sqlite3_vtab*)p;
6355  return rc;
6356}
6357
6358static int expertDisconnect(sqlite3_vtab *pVtab){
6359  ExpertVtab *p = (ExpertVtab*)pVtab;
6360  sqlite3_free(p);
6361  return SQLITE_OK;
6362}
6363
6364static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
6365  ExpertVtab *p = (ExpertVtab*)pVtab;
6366  int rc = SQLITE_OK;
6367  int n = 0;
6368  IdxScan *pScan;
6369  const int opmask =
6370    SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
6371    SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
6372    SQLITE_INDEX_CONSTRAINT_LE;
6373
6374  pScan = idxMalloc(&rc, sizeof(IdxScan));
6375  if( pScan ){
6376    int i;
6377
6378    /* Link the new scan object into the list */
6379    pScan->pTab = p->pTab;
6380    pScan->pNextScan = p->pExpert->pScan;
6381    p->pExpert->pScan = pScan;
6382
6383    /* Add the constraints to the IdxScan object */
6384    for(i=0; i<pIdxInfo->nConstraint; i++){
6385      struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
6386      if( pCons->usable
6387       && pCons->iColumn>=0
6388       && p->pTab->aCol[pCons->iColumn].iPk==0
6389       && (pCons->op & opmask)
6390      ){
6391        IdxConstraint *pNew;
6392        const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
6393        pNew = idxNewConstraint(&rc, zColl);
6394        if( pNew ){
6395          pNew->iCol = pCons->iColumn;
6396          if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6397            pNew->pNext = pScan->pEq;
6398            pScan->pEq = pNew;
6399          }else{
6400            pNew->bRange = 1;
6401            pNew->pNext = pScan->pRange;
6402            pScan->pRange = pNew;
6403          }
6404        }
6405        n++;
6406        pIdxInfo->aConstraintUsage[i].argvIndex = n;
6407      }
6408    }
6409
6410    /* Add the ORDER BY to the IdxScan object */
6411    for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
6412      int iCol = pIdxInfo->aOrderBy[i].iColumn;
6413      if( iCol>=0 ){
6414        IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
6415        if( pNew ){
6416          pNew->iCol = iCol;
6417          pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
6418          pNew->pNext = pScan->pOrder;
6419          pNew->pLink = pScan->pOrder;
6420          pScan->pOrder = pNew;
6421          n++;
6422        }
6423      }
6424    }
6425  }
6426
6427  pIdxInfo->estimatedCost = 1000000.0 / (n+1);
6428  return rc;
6429}
6430
6431static int expertUpdate(
6432  sqlite3_vtab *pVtab,
6433  int nData,
6434  sqlite3_value **azData,
6435  sqlite_int64 *pRowid
6436){
6437  (void)pVtab;
6438  (void)nData;
6439  (void)azData;
6440  (void)pRowid;
6441  return SQLITE_OK;
6442}
6443
6444/*
6445** Virtual table module xOpen method.
6446*/
6447static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
6448  int rc = SQLITE_OK;
6449  ExpertCsr *pCsr;
6450  (void)pVTab;
6451  pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
6452  *ppCursor = (sqlite3_vtab_cursor*)pCsr;
6453  return rc;
6454}
6455
6456/*
6457** Virtual table module xClose method.
6458*/
6459static int expertClose(sqlite3_vtab_cursor *cur){
6460  ExpertCsr *pCsr = (ExpertCsr*)cur;
6461  sqlite3_finalize(pCsr->pData);
6462  sqlite3_free(pCsr);
6463  return SQLITE_OK;
6464}
6465
6466/*
6467** Virtual table module xEof method.
6468**
6469** Return non-zero if the cursor does not currently point to a valid
6470** record (i.e if the scan has finished), or zero otherwise.
6471*/
6472static int expertEof(sqlite3_vtab_cursor *cur){
6473  ExpertCsr *pCsr = (ExpertCsr*)cur;
6474  return pCsr->pData==0;
6475}
6476
6477/*
6478** Virtual table module xNext method.
6479*/
6480static int expertNext(sqlite3_vtab_cursor *cur){
6481  ExpertCsr *pCsr = (ExpertCsr*)cur;
6482  int rc = SQLITE_OK;
6483
6484  assert( pCsr->pData );
6485  rc = sqlite3_step(pCsr->pData);
6486  if( rc!=SQLITE_ROW ){
6487    rc = sqlite3_finalize(pCsr->pData);
6488    pCsr->pData = 0;
6489  }else{
6490    rc = SQLITE_OK;
6491  }
6492
6493  return rc;
6494}
6495
6496/*
6497** Virtual table module xRowid method.
6498*/
6499static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
6500  (void)cur;
6501  *pRowid = 0;
6502  return SQLITE_OK;
6503}
6504
6505/*
6506** Virtual table module xColumn method.
6507*/
6508static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
6509  ExpertCsr *pCsr = (ExpertCsr*)cur;
6510  sqlite3_value *pVal;
6511  pVal = sqlite3_column_value(pCsr->pData, i);
6512  if( pVal ){
6513    sqlite3_result_value(ctx, pVal);
6514  }
6515  return SQLITE_OK;
6516}
6517
6518/*
6519** Virtual table module xFilter method.
6520*/
6521static int expertFilter(
6522  sqlite3_vtab_cursor *cur,
6523  int idxNum, const char *idxStr,
6524  int argc, sqlite3_value **argv
6525){
6526  ExpertCsr *pCsr = (ExpertCsr*)cur;
6527  ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
6528  sqlite3expert *pExpert = pVtab->pExpert;
6529  int rc;
6530
6531  (void)idxNum;
6532  (void)idxStr;
6533  (void)argc;
6534  (void)argv;
6535  rc = sqlite3_finalize(pCsr->pData);
6536  pCsr->pData = 0;
6537  if( rc==SQLITE_OK ){
6538    rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
6539        "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
6540    );
6541  }
6542
6543  if( rc==SQLITE_OK ){
6544    rc = expertNext(cur);
6545  }
6546  return rc;
6547}
6548
6549static int idxRegisterVtab(sqlite3expert *p){
6550  static sqlite3_module expertModule = {
6551    2,                            /* iVersion */
6552    expertConnect,                /* xCreate - create a table */
6553    expertConnect,                /* xConnect - connect to an existing table */
6554    expertBestIndex,              /* xBestIndex - Determine search strategy */
6555    expertDisconnect,             /* xDisconnect - Disconnect from a table */
6556    expertDisconnect,             /* xDestroy - Drop a table */
6557    expertOpen,                   /* xOpen - open a cursor */
6558    expertClose,                  /* xClose - close a cursor */
6559    expertFilter,                 /* xFilter - configure scan constraints */
6560    expertNext,                   /* xNext - advance a cursor */
6561    expertEof,                    /* xEof */
6562    expertColumn,                 /* xColumn - read data */
6563    expertRowid,                  /* xRowid - read data */
6564    expertUpdate,                 /* xUpdate - write data */
6565    0,                            /* xBegin - begin transaction */
6566    0,                            /* xSync - sync transaction */
6567    0,                            /* xCommit - commit transaction */
6568    0,                            /* xRollback - rollback transaction */
6569    0,                            /* xFindFunction - function overloading */
6570    0,                            /* xRename - rename the table */
6571    0,                            /* xSavepoint */
6572    0,                            /* xRelease */
6573    0,                            /* xRollbackTo */
6574  };
6575
6576  return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
6577}
6578/*
6579** End of virtual table implementation.
6580*************************************************************************/
6581/*
6582** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
6583** is called, set it to the return value of sqlite3_finalize() before
6584** returning. Otherwise, discard the sqlite3_finalize() return value.
6585*/
6586static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
6587  int rc = sqlite3_finalize(pStmt);
6588  if( *pRc==SQLITE_OK ) *pRc = rc;
6589}
6590
6591/*
6592** Attempt to allocate an IdxTable structure corresponding to table zTab
6593** in the main database of connection db. If successful, set (*ppOut) to
6594** point to the new object and return SQLITE_OK. Otherwise, return an
6595** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
6596** set to point to an error string.
6597**
6598** It is the responsibility of the caller to eventually free either the
6599** IdxTable object or error message using sqlite3_free().
6600*/
6601static int idxGetTableInfo(
6602  sqlite3 *db,                    /* Database connection to read details from */
6603  const char *zTab,               /* Table name */
6604  IdxTable **ppOut,               /* OUT: New object (if successful) */
6605  char **pzErrmsg                 /* OUT: Error message (if not) */
6606){
6607  sqlite3_stmt *p1 = 0;
6608  int nCol = 0;
6609  int nTab = STRLEN(zTab);
6610  int nByte = sizeof(IdxTable) + nTab + 1;
6611  IdxTable *pNew = 0;
6612  int rc, rc2;
6613  char *pCsr = 0;
6614
6615  rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
6616  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
6617    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
6618    nByte += 1 + STRLEN(zCol);
6619    rc = sqlite3_table_column_metadata(
6620        db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
6621    );
6622    nByte += 1 + STRLEN(zCol);
6623    nCol++;
6624  }
6625  rc2 = sqlite3_reset(p1);
6626  if( rc==SQLITE_OK ) rc = rc2;
6627
6628  nByte += sizeof(IdxColumn) * nCol;
6629  if( rc==SQLITE_OK ){
6630    pNew = idxMalloc(&rc, nByte);
6631  }
6632  if( rc==SQLITE_OK ){
6633    pNew->aCol = (IdxColumn*)&pNew[1];
6634    pNew->nCol = nCol;
6635    pCsr = (char*)&pNew->aCol[nCol];
6636  }
6637
6638  nCol = 0;
6639  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
6640    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
6641    int nCopy = STRLEN(zCol) + 1;
6642    pNew->aCol[nCol].zName = pCsr;
6643    pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
6644    memcpy(pCsr, zCol, nCopy);
6645    pCsr += nCopy;
6646
6647    rc = sqlite3_table_column_metadata(
6648        db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
6649    );
6650    if( rc==SQLITE_OK ){
6651      nCopy = STRLEN(zCol) + 1;
6652      pNew->aCol[nCol].zColl = pCsr;
6653      memcpy(pCsr, zCol, nCopy);
6654      pCsr += nCopy;
6655    }
6656
6657    nCol++;
6658  }
6659  idxFinalize(&rc, p1);
6660
6661  if( rc!=SQLITE_OK ){
6662    sqlite3_free(pNew);
6663    pNew = 0;
6664  }else{
6665    pNew->zName = pCsr;
6666    memcpy(pNew->zName, zTab, nTab+1);
6667  }
6668
6669  *ppOut = pNew;
6670  return rc;
6671}
6672
6673/*
6674** This function is a no-op if *pRc is set to anything other than
6675** SQLITE_OK when it is called.
6676**
6677** If *pRc is initially set to SQLITE_OK, then the text specified by
6678** the printf() style arguments is appended to zIn and the result returned
6679** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
6680** zIn before returning.
6681*/
6682static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
6683  va_list ap;
6684  char *zAppend = 0;
6685  char *zRet = 0;
6686  int nIn = zIn ? STRLEN(zIn) : 0;
6687  int nAppend = 0;
6688  va_start(ap, zFmt);
6689  if( *pRc==SQLITE_OK ){
6690    zAppend = sqlite3_vmprintf(zFmt, ap);
6691    if( zAppend ){
6692      nAppend = STRLEN(zAppend);
6693      zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
6694    }
6695    if( zAppend && zRet ){
6696      if( nIn ) memcpy(zRet, zIn, nIn);
6697      memcpy(&zRet[nIn], zAppend, nAppend+1);
6698    }else{
6699      sqlite3_free(zRet);
6700      zRet = 0;
6701      *pRc = SQLITE_NOMEM;
6702    }
6703    sqlite3_free(zAppend);
6704    sqlite3_free(zIn);
6705  }
6706  va_end(ap);
6707  return zRet;
6708}
6709
6710/*
6711** Return true if zId must be quoted in order to use it as an SQL
6712** identifier, or false otherwise.
6713*/
6714static int idxIdentifierRequiresQuotes(const char *zId){
6715  int i;
6716  for(i=0; zId[i]; i++){
6717    if( !(zId[i]=='_')
6718     && !(zId[i]>='0' && zId[i]<='9')
6719     && !(zId[i]>='a' && zId[i]<='z')
6720     && !(zId[i]>='A' && zId[i]<='Z')
6721    ){
6722      return 1;
6723    }
6724  }
6725  return 0;
6726}
6727
6728/*
6729** This function appends an index column definition suitable for constraint
6730** pCons to the string passed as zIn and returns the result.
6731*/
6732static char *idxAppendColDefn(
6733  int *pRc,                       /* IN/OUT: Error code */
6734  char *zIn,                      /* Column defn accumulated so far */
6735  IdxTable *pTab,                 /* Table index will be created on */
6736  IdxConstraint *pCons
6737){
6738  char *zRet = zIn;
6739  IdxColumn *p = &pTab->aCol[pCons->iCol];
6740  if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
6741
6742  if( idxIdentifierRequiresQuotes(p->zName) ){
6743    zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
6744  }else{
6745    zRet = idxAppendText(pRc, zRet, "%s", p->zName);
6746  }
6747
6748  if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
6749    if( idxIdentifierRequiresQuotes(pCons->zColl) ){
6750      zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
6751    }else{
6752      zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
6753    }
6754  }
6755
6756  if( pCons->bDesc ){
6757    zRet = idxAppendText(pRc, zRet, " DESC");
6758  }
6759  return zRet;
6760}
6761
6762/*
6763** Search database dbm for an index compatible with the one idxCreateFromCons()
6764** would create from arguments pScan, pEq and pTail. If no error occurs and
6765** such an index is found, return non-zero. Or, if no such index is found,
6766** return zero.
6767**
6768** If an error occurs, set *pRc to an SQLite error code and return zero.
6769*/
6770static int idxFindCompatible(
6771  int *pRc,                       /* OUT: Error code */
6772  sqlite3* dbm,                   /* Database to search */
6773  IdxScan *pScan,                 /* Scan for table to search for index on */
6774  IdxConstraint *pEq,             /* List of == constraints */
6775  IdxConstraint *pTail            /* List of range constraints */
6776){
6777  const char *zTbl = pScan->pTab->zName;
6778  sqlite3_stmt *pIdxList = 0;
6779  IdxConstraint *pIter;
6780  int nEq = 0;                    /* Number of elements in pEq */
6781  int rc;
6782
6783  /* Count the elements in list pEq */
6784  for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
6785
6786  rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
6787  while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
6788    int bMatch = 1;
6789    IdxConstraint *pT = pTail;
6790    sqlite3_stmt *pInfo = 0;
6791    const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
6792
6793    /* Zero the IdxConstraint.bFlag values in the pEq list */
6794    for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
6795
6796    rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
6797    while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
6798      int iIdx = sqlite3_column_int(pInfo, 0);
6799      int iCol = sqlite3_column_int(pInfo, 1);
6800      const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
6801
6802      if( iIdx<nEq ){
6803        for(pIter=pEq; pIter; pIter=pIter->pLink){
6804          if( pIter->bFlag ) continue;
6805          if( pIter->iCol!=iCol ) continue;
6806          if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
6807          pIter->bFlag = 1;
6808          break;
6809        }
6810        if( pIter==0 ){
6811          bMatch = 0;
6812          break;
6813        }
6814      }else{
6815        if( pT ){
6816          if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
6817            bMatch = 0;
6818            break;
6819          }
6820          pT = pT->pLink;
6821        }
6822      }
6823    }
6824    idxFinalize(&rc, pInfo);
6825
6826    if( rc==SQLITE_OK && bMatch ){
6827      sqlite3_finalize(pIdxList);
6828      return 1;
6829    }
6830  }
6831  idxFinalize(&rc, pIdxList);
6832
6833  *pRc = rc;
6834  return 0;
6835}
6836
6837static int idxCreateFromCons(
6838  sqlite3expert *p,
6839  IdxScan *pScan,
6840  IdxConstraint *pEq,
6841  IdxConstraint *pTail
6842){
6843  sqlite3 *dbm = p->dbm;
6844  int rc = SQLITE_OK;
6845  if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
6846    IdxTable *pTab = pScan->pTab;
6847    char *zCols = 0;
6848    char *zIdx = 0;
6849    IdxConstraint *pCons;
6850    unsigned int h = 0;
6851    const char *zFmt;
6852
6853    for(pCons=pEq; pCons; pCons=pCons->pLink){
6854      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
6855    }
6856    for(pCons=pTail; pCons; pCons=pCons->pLink){
6857      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
6858    }
6859
6860    if( rc==SQLITE_OK ){
6861      /* Hash the list of columns to come up with a name for the index */
6862      const char *zTable = pScan->pTab->zName;
6863      char *zName;                /* Index name */
6864      int i;
6865      for(i=0; zCols[i]; i++){
6866        h += ((h<<3) + zCols[i]);
6867      }
6868      zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
6869      if( zName==0 ){
6870        rc = SQLITE_NOMEM;
6871      }else{
6872        if( idxIdentifierRequiresQuotes(zTable) ){
6873          zFmt = "CREATE INDEX '%q' ON %Q(%s)";
6874        }else{
6875          zFmt = "CREATE INDEX %s ON %s(%s)";
6876        }
6877        zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
6878        if( !zIdx ){
6879          rc = SQLITE_NOMEM;
6880        }else{
6881          rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
6882          idxHashAdd(&rc, &p->hIdx, zName, zIdx);
6883        }
6884        sqlite3_free(zName);
6885        sqlite3_free(zIdx);
6886      }
6887    }
6888
6889    sqlite3_free(zCols);
6890  }
6891  return rc;
6892}
6893
6894/*
6895** Return true if list pList (linked by IdxConstraint.pLink) contains
6896** a constraint compatible with *p. Otherwise return false.
6897*/
6898static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
6899  IdxConstraint *pCmp;
6900  for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
6901    if( p->iCol==pCmp->iCol ) return 1;
6902  }
6903  return 0;
6904}
6905
6906static int idxCreateFromWhere(
6907  sqlite3expert *p,
6908  IdxScan *pScan,                 /* Create indexes for this scan */
6909  IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
6910){
6911  IdxConstraint *p1 = 0;
6912  IdxConstraint *pCon;
6913  int rc;
6914
6915  /* Gather up all the == constraints. */
6916  for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
6917    if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
6918      pCon->pLink = p1;
6919      p1 = pCon;
6920    }
6921  }
6922
6923  /* Create an index using the == constraints collected above. And the
6924  ** range constraint/ORDER BY terms passed in by the caller, if any. */
6925  rc = idxCreateFromCons(p, pScan, p1, pTail);
6926
6927  /* If no range/ORDER BY passed by the caller, create a version of the
6928  ** index for each range constraint.  */
6929  if( pTail==0 ){
6930    for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
6931      assert( pCon->pLink==0 );
6932      if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
6933        rc = idxCreateFromCons(p, pScan, p1, pCon);
6934      }
6935    }
6936  }
6937
6938  return rc;
6939}
6940
6941/*
6942** Create candidate indexes in database [dbm] based on the data in
6943** linked-list pScan.
6944*/
6945static int idxCreateCandidates(sqlite3expert *p){
6946  int rc = SQLITE_OK;
6947  IdxScan *pIter;
6948
6949  for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
6950    rc = idxCreateFromWhere(p, pIter, 0);
6951    if( rc==SQLITE_OK && pIter->pOrder ){
6952      rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
6953    }
6954  }
6955
6956  return rc;
6957}
6958
6959/*
6960** Free all elements of the linked list starting at pConstraint.
6961*/
6962static void idxConstraintFree(IdxConstraint *pConstraint){
6963  IdxConstraint *pNext;
6964  IdxConstraint *p;
6965
6966  for(p=pConstraint; p; p=pNext){
6967    pNext = p->pNext;
6968    sqlite3_free(p);
6969  }
6970}
6971
6972/*
6973** Free all elements of the linked list starting from pScan up until pLast
6974** (pLast is not freed).
6975*/
6976static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
6977  IdxScan *p;
6978  IdxScan *pNext;
6979  for(p=pScan; p!=pLast; p=pNext){
6980    pNext = p->pNextScan;
6981    idxConstraintFree(p->pOrder);
6982    idxConstraintFree(p->pEq);
6983    idxConstraintFree(p->pRange);
6984    sqlite3_free(p);
6985  }
6986}
6987
6988/*
6989** Free all elements of the linked list starting from pStatement up
6990** until pLast (pLast is not freed).
6991*/
6992static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
6993  IdxStatement *p;
6994  IdxStatement *pNext;
6995  for(p=pStatement; p!=pLast; p=pNext){
6996    pNext = p->pNext;
6997    sqlite3_free(p->zEQP);
6998    sqlite3_free(p->zIdx);
6999    sqlite3_free(p);
7000  }
7001}
7002
7003/*
7004** Free the linked list of IdxTable objects starting at pTab.
7005*/
7006static void idxTableFree(IdxTable *pTab){
7007  IdxTable *pIter;
7008  IdxTable *pNext;
7009  for(pIter=pTab; pIter; pIter=pNext){
7010    pNext = pIter->pNext;
7011    sqlite3_free(pIter);
7012  }
7013}
7014
7015/*
7016** Free the linked list of IdxWrite objects starting at pTab.
7017*/
7018static void idxWriteFree(IdxWrite *pTab){
7019  IdxWrite *pIter;
7020  IdxWrite *pNext;
7021  for(pIter=pTab; pIter; pIter=pNext){
7022    pNext = pIter->pNext;
7023    sqlite3_free(pIter);
7024  }
7025}
7026
7027
7028
7029/*
7030** This function is called after candidate indexes have been created. It
7031** runs all the queries to see which indexes they prefer, and populates
7032** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7033*/
7034int idxFindIndexes(
7035  sqlite3expert *p,
7036  char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
7037){
7038  IdxStatement *pStmt;
7039  sqlite3 *dbm = p->dbm;
7040  int rc = SQLITE_OK;
7041
7042  IdxHash hIdx;
7043  idxHashInit(&hIdx);
7044
7045  for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7046    IdxHashEntry *pEntry;
7047    sqlite3_stmt *pExplain = 0;
7048    idxHashClear(&hIdx);
7049    rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7050        "EXPLAIN QUERY PLAN %s", pStmt->zSql
7051    );
7052    while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7053      int iSelectid = sqlite3_column_int(pExplain, 0);
7054      int iOrder = sqlite3_column_int(pExplain, 1);
7055      int iFrom = sqlite3_column_int(pExplain, 2);
7056      const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7057      int nDetail = STRLEN(zDetail);
7058      int i;
7059
7060      for(i=0; i<nDetail; i++){
7061        const char *zIdx = 0;
7062        if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7063          zIdx = &zDetail[i+13];
7064        }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7065          zIdx = &zDetail[i+22];
7066        }
7067        if( zIdx ){
7068          const char *zSql;
7069          int nIdx = 0;
7070          while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7071            nIdx++;
7072          }
7073          zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7074          if( zSql ){
7075            idxHashAdd(&rc, &hIdx, zSql, 0);
7076            if( rc ) goto find_indexes_out;
7077          }
7078          break;
7079        }
7080      }
7081
7082      pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%d|%d|%d|%s\n",
7083          iSelectid, iOrder, iFrom, zDetail
7084      );
7085    }
7086
7087    for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7088      pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7089    }
7090
7091    idxFinalize(&rc, pExplain);
7092  }
7093
7094 find_indexes_out:
7095  idxHashClear(&hIdx);
7096  return rc;
7097}
7098
7099static int idxAuthCallback(
7100  void *pCtx,
7101  int eOp,
7102  const char *z3,
7103  const char *z4,
7104  const char *zDb,
7105  const char *zTrigger
7106){
7107  int rc = SQLITE_OK;
7108  (void)z4;
7109  (void)zTrigger;
7110  if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7111    if( sqlite3_stricmp(zDb, "main")==0 ){
7112      sqlite3expert *p = (sqlite3expert*)pCtx;
7113      IdxTable *pTab;
7114      for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7115        if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7116      }
7117      if( pTab ){
7118        IdxWrite *pWrite;
7119        for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7120          if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7121        }
7122        if( pWrite==0 ){
7123          pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7124          if( rc==SQLITE_OK ){
7125            pWrite->pTab = pTab;
7126            pWrite->eOp = eOp;
7127            pWrite->pNext = p->pWrite;
7128            p->pWrite = pWrite;
7129          }
7130        }
7131      }
7132    }
7133  }
7134  return rc;
7135}
7136
7137static int idxProcessOneTrigger(
7138  sqlite3expert *p,
7139  IdxWrite *pWrite,
7140  char **pzErr
7141){
7142  static const char *zInt = UNIQUE_TABLE_NAME;
7143  static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7144  IdxTable *pTab = pWrite->pTab;
7145  const char *zTab = pTab->zName;
7146  const char *zSql =
7147    "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7148    "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7149    "ORDER BY type;";
7150  sqlite3_stmt *pSelect = 0;
7151  int rc = SQLITE_OK;
7152  char *zWrite = 0;
7153
7154  /* Create the table and its triggers in the temp schema */
7155  rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7156  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7157    const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7158    rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7159  }
7160  idxFinalize(&rc, pSelect);
7161
7162  /* Rename the table in the temp schema to zInt */
7163  if( rc==SQLITE_OK ){
7164    char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7165    if( z==0 ){
7166      rc = SQLITE_NOMEM;
7167    }else{
7168      rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7169      sqlite3_free(z);
7170    }
7171  }
7172
7173  switch( pWrite->eOp ){
7174    case SQLITE_INSERT: {
7175      int i;
7176      zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7177      for(i=0; i<pTab->nCol; i++){
7178        zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7179      }
7180      zWrite = idxAppendText(&rc, zWrite, ")");
7181      break;
7182    }
7183    case SQLITE_UPDATE: {
7184      int i;
7185      zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7186      for(i=0; i<pTab->nCol; i++){
7187        zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
7188            pTab->aCol[i].zName
7189        );
7190      }
7191      break;
7192    }
7193    default: {
7194      assert( pWrite->eOp==SQLITE_DELETE );
7195      if( rc==SQLITE_OK ){
7196        zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7197        if( zWrite==0 ) rc = SQLITE_NOMEM;
7198      }
7199    }
7200  }
7201
7202  if( rc==SQLITE_OK ){
7203    sqlite3_stmt *pX = 0;
7204    rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7205    idxFinalize(&rc, pX);
7206    if( rc!=SQLITE_OK ){
7207      idxDatabaseError(p->dbv, pzErr);
7208    }
7209  }
7210  sqlite3_free(zWrite);
7211
7212  if( rc==SQLITE_OK ){
7213    rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7214  }
7215
7216  return rc;
7217}
7218
7219static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7220  int rc = SQLITE_OK;
7221  IdxWrite *pEnd = 0;
7222  IdxWrite *pFirst = p->pWrite;
7223
7224  while( rc==SQLITE_OK && pFirst!=pEnd ){
7225    IdxWrite *pIter;
7226    for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7227      rc = idxProcessOneTrigger(p, pIter, pzErr);
7228    }
7229    pEnd = pFirst;
7230    pFirst = p->pWrite;
7231  }
7232
7233  return rc;
7234}
7235
7236
7237static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7238  int rc = idxRegisterVtab(p);
7239  sqlite3_stmt *pSchema = 0;
7240
7241  /* For each table in the main db schema:
7242  **
7243  **   1) Add an entry to the p->pTable list, and
7244  **   2) Create the equivalent virtual table in dbv.
7245  */
7246  rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
7247      "SELECT type, name, sql, 1 FROM sqlite_master "
7248      "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
7249      " UNION ALL "
7250      "SELECT type, name, sql, 2 FROM sqlite_master "
7251      "WHERE type = 'trigger'"
7252      "  AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
7253      "ORDER BY 4, 1"
7254  );
7255  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
7256    const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
7257    const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
7258    const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
7259
7260    if( zType[0]=='v' || zType[1]=='r' ){
7261      rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
7262    }else{
7263      IdxTable *pTab;
7264      rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
7265      if( rc==SQLITE_OK ){
7266        int i;
7267        char *zInner = 0;
7268        char *zOuter = 0;
7269        pTab->pNext = p->pTable;
7270        p->pTable = pTab;
7271
7272        /* The statement the vtab will pass to sqlite3_declare_vtab() */
7273        zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
7274        for(i=0; i<pTab->nCol; i++){
7275          zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
7276              (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
7277          );
7278        }
7279        zInner = idxAppendText(&rc, zInner, ")");
7280
7281        /* The CVT statement to create the vtab */
7282        zOuter = idxAppendText(&rc, 0,
7283            "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
7284        );
7285        if( rc==SQLITE_OK ){
7286          rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
7287        }
7288        sqlite3_free(zInner);
7289        sqlite3_free(zOuter);
7290      }
7291    }
7292  }
7293  idxFinalize(&rc, pSchema);
7294  return rc;
7295}
7296
7297struct IdxSampleCtx {
7298  int iTarget;
7299  double target;                  /* Target nRet/nRow value */
7300  double nRow;                    /* Number of rows seen */
7301  double nRet;                    /* Number of rows returned */
7302};
7303
7304static void idxSampleFunc(
7305  sqlite3_context *pCtx,
7306  int argc,
7307  sqlite3_value **argv
7308){
7309  struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
7310  int bRet;
7311
7312  (void)argv;
7313  assert( argc==0 );
7314  if( p->nRow==0.0 ){
7315    bRet = 1;
7316  }else{
7317    bRet = (p->nRet / p->nRow) <= p->target;
7318    if( bRet==0 ){
7319      unsigned short rnd;
7320      sqlite3_randomness(2, (void*)&rnd);
7321      bRet = ((int)rnd % 100) <= p->iTarget;
7322    }
7323  }
7324
7325  sqlite3_result_int(pCtx, bRet);
7326  p->nRow += 1.0;
7327  p->nRet += (double)bRet;
7328}
7329
7330struct IdxRemCtx {
7331  int nSlot;
7332  struct IdxRemSlot {
7333    int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
7334    i64 iVal;                     /* SQLITE_INTEGER value */
7335    double rVal;                  /* SQLITE_FLOAT value */
7336    int nByte;                    /* Bytes of space allocated at z */
7337    int n;                        /* Size of buffer z */
7338    char *z;                      /* SQLITE_TEXT/BLOB value */
7339  } aSlot[1];
7340};
7341
7342/*
7343** Implementation of scalar function rem().
7344*/
7345static void idxRemFunc(
7346  sqlite3_context *pCtx,
7347  int argc,
7348  sqlite3_value **argv
7349){
7350  struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
7351  struct IdxRemSlot *pSlot;
7352  int iSlot;
7353  assert( argc==2 );
7354
7355  iSlot = sqlite3_value_int(argv[0]);
7356  assert( iSlot<=p->nSlot );
7357  pSlot = &p->aSlot[iSlot];
7358
7359  switch( pSlot->eType ){
7360    case SQLITE_NULL:
7361      /* no-op */
7362      break;
7363
7364    case SQLITE_INTEGER:
7365      sqlite3_result_int64(pCtx, pSlot->iVal);
7366      break;
7367
7368    case SQLITE_FLOAT:
7369      sqlite3_result_double(pCtx, pSlot->rVal);
7370      break;
7371
7372    case SQLITE_BLOB:
7373      sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7374      break;
7375
7376    case SQLITE_TEXT:
7377      sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7378      break;
7379  }
7380
7381  pSlot->eType = sqlite3_value_type(argv[1]);
7382  switch( pSlot->eType ){
7383    case SQLITE_NULL:
7384      /* no-op */
7385      break;
7386
7387    case SQLITE_INTEGER:
7388      pSlot->iVal = sqlite3_value_int64(argv[1]);
7389      break;
7390
7391    case SQLITE_FLOAT:
7392      pSlot->rVal = sqlite3_value_double(argv[1]);
7393      break;
7394
7395    case SQLITE_BLOB:
7396    case SQLITE_TEXT: {
7397      int nByte = sqlite3_value_bytes(argv[1]);
7398      if( nByte>pSlot->nByte ){
7399        char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
7400        if( zNew==0 ){
7401          sqlite3_result_error_nomem(pCtx);
7402          return;
7403        }
7404        pSlot->nByte = nByte*2;
7405        pSlot->z = zNew;
7406      }
7407      pSlot->n = nByte;
7408      if( pSlot->eType==SQLITE_BLOB ){
7409        memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
7410      }else{
7411        memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
7412      }
7413      break;
7414    }
7415  }
7416}
7417
7418static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
7419  int rc = SQLITE_OK;
7420  const char *zMax =
7421    "SELECT max(i.seqno) FROM "
7422    "  sqlite_master AS s, "
7423    "  pragma_index_list(s.name) AS l, "
7424    "  pragma_index_info(l.name) AS i "
7425    "WHERE s.type = 'table'";
7426  sqlite3_stmt *pMax = 0;
7427
7428  *pnMax = 0;
7429  rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
7430  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
7431    *pnMax = sqlite3_column_int(pMax, 0) + 1;
7432  }
7433  idxFinalize(&rc, pMax);
7434
7435  return rc;
7436}
7437
7438static int idxPopulateOneStat1(
7439  sqlite3expert *p,
7440  sqlite3_stmt *pIndexXInfo,
7441  sqlite3_stmt *pWriteStat,
7442  const char *zTab,
7443  const char *zIdx,
7444  char **pzErr
7445){
7446  char *zCols = 0;
7447  char *zOrder = 0;
7448  char *zQuery = 0;
7449  int nCol = 0;
7450  int i;
7451  sqlite3_stmt *pQuery = 0;
7452  int *aStat = 0;
7453  int rc = SQLITE_OK;
7454
7455  assert( p->iSample>0 );
7456
7457  /* Formulate the query text */
7458  sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
7459  while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
7460    const char *zComma = zCols==0 ? "" : ", ";
7461    const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
7462    const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
7463    zCols = idxAppendText(&rc, zCols,
7464        "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
7465    );
7466    zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
7467  }
7468  sqlite3_reset(pIndexXInfo);
7469  if( rc==SQLITE_OK ){
7470    if( p->iSample==100 ){
7471      zQuery = sqlite3_mprintf(
7472          "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
7473      );
7474    }else{
7475      zQuery = sqlite3_mprintf(
7476          "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
7477      );
7478    }
7479  }
7480  sqlite3_free(zCols);
7481  sqlite3_free(zOrder);
7482
7483  /* Formulate the query text */
7484  if( rc==SQLITE_OK ){
7485    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
7486    rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
7487  }
7488  sqlite3_free(zQuery);
7489
7490  if( rc==SQLITE_OK ){
7491    aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
7492  }
7493  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
7494    IdxHashEntry *pEntry;
7495    char *zStat = 0;
7496    for(i=0; i<=nCol; i++) aStat[i] = 1;
7497    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
7498      aStat[0]++;
7499      for(i=0; i<nCol; i++){
7500        if( sqlite3_column_int(pQuery, i)==0 ) break;
7501      }
7502      for(/*no-op*/; i<nCol; i++){
7503        aStat[i+1]++;
7504      }
7505    }
7506
7507    if( rc==SQLITE_OK ){
7508      int s0 = aStat[0];
7509      zStat = sqlite3_mprintf("%d", s0);
7510      if( zStat==0 ) rc = SQLITE_NOMEM;
7511      for(i=1; rc==SQLITE_OK && i<=nCol; i++){
7512        zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
7513      }
7514    }
7515
7516    if( rc==SQLITE_OK ){
7517      sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
7518      sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
7519      sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
7520      sqlite3_step(pWriteStat);
7521      rc = sqlite3_reset(pWriteStat);
7522    }
7523
7524    pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
7525    if( pEntry ){
7526      assert( pEntry->zVal2==0 );
7527      pEntry->zVal2 = zStat;
7528    }else{
7529      sqlite3_free(zStat);
7530    }
7531  }
7532  sqlite3_free(aStat);
7533  idxFinalize(&rc, pQuery);
7534
7535  return rc;
7536}
7537
7538static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
7539  int rc;
7540  char *zSql;
7541
7542  rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
7543  if( rc!=SQLITE_OK ) return rc;
7544
7545  zSql = sqlite3_mprintf(
7546      "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
7547  );
7548  if( zSql==0 ) return SQLITE_NOMEM;
7549  rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
7550  sqlite3_free(zSql);
7551
7552  return rc;
7553}
7554
7555/*
7556** This function is called as part of sqlite3_expert_analyze(). Candidate
7557** indexes have already been created in database sqlite3expert.dbm, this
7558** function populates sqlite_stat1 table in the same database.
7559**
7560** The stat1 data is generated by querying the
7561*/
7562static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
7563  int rc = SQLITE_OK;
7564  int nMax =0;
7565  struct IdxRemCtx *pCtx = 0;
7566  struct IdxSampleCtx samplectx;
7567  int i;
7568  i64 iPrev = -100000;
7569  sqlite3_stmt *pAllIndex = 0;
7570  sqlite3_stmt *pIndexXInfo = 0;
7571  sqlite3_stmt *pWrite = 0;
7572
7573  const char *zAllIndex =
7574    "SELECT s.rowid, s.name, l.name FROM "
7575    "  sqlite_master AS s, "
7576    "  pragma_index_list(s.name) AS l "
7577    "WHERE s.type = 'table'";
7578  const char *zIndexXInfo =
7579    "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
7580  const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
7581
7582  /* If iSample==0, no sqlite_stat1 data is required. */
7583  if( p->iSample==0 ) return SQLITE_OK;
7584
7585  rc = idxLargestIndex(p->dbm, &nMax, pzErr);
7586  if( nMax<=0 || rc!=SQLITE_OK ) return rc;
7587
7588  rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
7589
7590  if( rc==SQLITE_OK ){
7591    int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
7592    pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
7593  }
7594
7595  if( rc==SQLITE_OK ){
7596    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
7597    rc = sqlite3_create_function(
7598        dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
7599    );
7600  }
7601  if( rc==SQLITE_OK ){
7602    rc = sqlite3_create_function(
7603        p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
7604    );
7605  }
7606
7607  if( rc==SQLITE_OK ){
7608    pCtx->nSlot = nMax+1;
7609    rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
7610  }
7611  if( rc==SQLITE_OK ){
7612    rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
7613  }
7614  if( rc==SQLITE_OK ){
7615    rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
7616  }
7617
7618  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
7619    i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
7620    const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
7621    const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
7622    if( p->iSample<100 && iPrev!=iRowid ){
7623      samplectx.target = (double)p->iSample / 100.0;
7624      samplectx.iTarget = p->iSample;
7625      samplectx.nRow = 0.0;
7626      samplectx.nRet = 0.0;
7627      rc = idxBuildSampleTable(p, zTab);
7628      if( rc!=SQLITE_OK ) break;
7629    }
7630    rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
7631    iPrev = iRowid;
7632  }
7633  if( rc==SQLITE_OK && p->iSample<100 ){
7634    rc = sqlite3_exec(p->dbv,
7635        "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
7636    );
7637  }
7638
7639  idxFinalize(&rc, pAllIndex);
7640  idxFinalize(&rc, pIndexXInfo);
7641  idxFinalize(&rc, pWrite);
7642
7643  for(i=0; i<pCtx->nSlot; i++){
7644    sqlite3_free(pCtx->aSlot[i].z);
7645  }
7646  sqlite3_free(pCtx);
7647
7648  if( rc==SQLITE_OK ){
7649    rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
7650  }
7651
7652  sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
7653  return rc;
7654}
7655
7656/*
7657** Allocate a new sqlite3expert object.
7658*/
7659sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
7660  int rc = SQLITE_OK;
7661  sqlite3expert *pNew;
7662
7663  pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
7664
7665  /* Open two in-memory databases to work with. The "vtab database" (dbv)
7666  ** will contain a virtual table corresponding to each real table in
7667  ** the user database schema, and a copy of each view. It is used to
7668  ** collect information regarding the WHERE, ORDER BY and other clauses
7669  ** of the user's query.
7670  */
7671  if( rc==SQLITE_OK ){
7672    pNew->db = db;
7673    pNew->iSample = 100;
7674    rc = sqlite3_open(":memory:", &pNew->dbv);
7675  }
7676  if( rc==SQLITE_OK ){
7677    rc = sqlite3_open(":memory:", &pNew->dbm);
7678    if( rc==SQLITE_OK ){
7679      sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
7680    }
7681  }
7682
7683
7684  /* Copy the entire schema of database [db] into [dbm]. */
7685  if( rc==SQLITE_OK ){
7686    sqlite3_stmt *pSql;
7687    rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
7688        "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
7689        " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
7690    );
7691    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7692      const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
7693      rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
7694    }
7695    idxFinalize(&rc, pSql);
7696  }
7697
7698  /* Create the vtab schema */
7699  if( rc==SQLITE_OK ){
7700    rc = idxCreateVtabSchema(pNew, pzErrmsg);
7701  }
7702
7703  /* Register the auth callback with dbv */
7704  if( rc==SQLITE_OK ){
7705    sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
7706  }
7707
7708  /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
7709  ** return the new sqlite3expert handle.  */
7710  if( rc!=SQLITE_OK ){
7711    sqlite3_expert_destroy(pNew);
7712    pNew = 0;
7713  }
7714  return pNew;
7715}
7716
7717/*
7718** Configure an sqlite3expert object.
7719*/
7720int sqlite3_expert_config(sqlite3expert *p, int op, ...){
7721  int rc = SQLITE_OK;
7722  va_list ap;
7723  va_start(ap, op);
7724  switch( op ){
7725    case EXPERT_CONFIG_SAMPLE: {
7726      int iVal = va_arg(ap, int);
7727      if( iVal<0 ) iVal = 0;
7728      if( iVal>100 ) iVal = 100;
7729      p->iSample = iVal;
7730      break;
7731    }
7732    default:
7733      rc = SQLITE_NOTFOUND;
7734      break;
7735  }
7736
7737  va_end(ap);
7738  return rc;
7739}
7740
7741/*
7742** Add an SQL statement to the analysis.
7743*/
7744int sqlite3_expert_sql(
7745  sqlite3expert *p,               /* From sqlite3_expert_new() */
7746  const char *zSql,               /* SQL statement to add */
7747  char **pzErr                    /* OUT: Error message (if any) */
7748){
7749  IdxScan *pScanOrig = p->pScan;
7750  IdxStatement *pStmtOrig = p->pStatement;
7751  int rc = SQLITE_OK;
7752  const char *zStmt = zSql;
7753
7754  if( p->bRun ) return SQLITE_MISUSE;
7755
7756  while( rc==SQLITE_OK && zStmt && zStmt[0] ){
7757    sqlite3_stmt *pStmt = 0;
7758    rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
7759    if( rc==SQLITE_OK ){
7760      if( pStmt ){
7761        IdxStatement *pNew;
7762        const char *z = sqlite3_sql(pStmt);
7763        int n = STRLEN(z);
7764        pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
7765        if( rc==SQLITE_OK ){
7766          pNew->zSql = (char*)&pNew[1];
7767          memcpy(pNew->zSql, z, n+1);
7768          pNew->pNext = p->pStatement;
7769          if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
7770          p->pStatement = pNew;
7771        }
7772        sqlite3_finalize(pStmt);
7773      }
7774    }else{
7775      idxDatabaseError(p->dbv, pzErr);
7776    }
7777  }
7778
7779  if( rc!=SQLITE_OK ){
7780    idxScanFree(p->pScan, pScanOrig);
7781    idxStatementFree(p->pStatement, pStmtOrig);
7782    p->pScan = pScanOrig;
7783    p->pStatement = pStmtOrig;
7784  }
7785
7786  return rc;
7787}
7788
7789int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
7790  int rc;
7791  IdxHashEntry *pEntry;
7792
7793  /* Do trigger processing to collect any extra IdxScan structures */
7794  rc = idxProcessTriggers(p, pzErr);
7795
7796  /* Create candidate indexes within the in-memory database file */
7797  if( rc==SQLITE_OK ){
7798    rc = idxCreateCandidates(p);
7799  }
7800
7801  /* Generate the stat1 data */
7802  if( rc==SQLITE_OK ){
7803    rc = idxPopulateStat1(p, pzErr);
7804  }
7805
7806  /* Formulate the EXPERT_REPORT_CANDIDATES text */
7807  for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7808    p->zCandidates = idxAppendText(&rc, p->zCandidates,
7809        "%s;%s%s\n", pEntry->zVal,
7810        pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
7811    );
7812  }
7813
7814  /* Figure out which of the candidate indexes are preferred by the query
7815  ** planner and report the results to the user.  */
7816  if( rc==SQLITE_OK ){
7817    rc = idxFindIndexes(p, pzErr);
7818  }
7819
7820  if( rc==SQLITE_OK ){
7821    p->bRun = 1;
7822  }
7823  return rc;
7824}
7825
7826/*
7827** Return the total number of statements that have been added to this
7828** sqlite3expert using sqlite3_expert_sql().
7829*/
7830int sqlite3_expert_count(sqlite3expert *p){
7831  int nRet = 0;
7832  if( p->pStatement ) nRet = p->pStatement->iId+1;
7833  return nRet;
7834}
7835
7836/*
7837** Return a component of the report.
7838*/
7839const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
7840  const char *zRet = 0;
7841  IdxStatement *pStmt;
7842
7843  if( p->bRun==0 ) return 0;
7844  for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
7845  switch( eReport ){
7846    case EXPERT_REPORT_SQL:
7847      if( pStmt ) zRet = pStmt->zSql;
7848      break;
7849    case EXPERT_REPORT_INDEXES:
7850      if( pStmt ) zRet = pStmt->zIdx;
7851      break;
7852    case EXPERT_REPORT_PLAN:
7853      if( pStmt ) zRet = pStmt->zEQP;
7854      break;
7855    case EXPERT_REPORT_CANDIDATES:
7856      zRet = p->zCandidates;
7857      break;
7858  }
7859  return zRet;
7860}
7861
7862/*
7863** Free an sqlite3expert object.
7864*/
7865void sqlite3_expert_destroy(sqlite3expert *p){
7866  if( p ){
7867    sqlite3_close(p->dbm);
7868    sqlite3_close(p->dbv);
7869    idxScanFree(p->pScan, 0);
7870    idxStatementFree(p->pStatement, 0);
7871    idxTableFree(p->pTable);
7872    idxWriteFree(p->pWrite);
7873    idxHashClear(&p->hIdx);
7874    sqlite3_free(p->zCandidates);
7875    sqlite3_free(p);
7876  }
7877}
7878
7879#endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
7880
7881/************************* End ../ext/expert/sqlite3expert.c ********************/
7882
7883#if defined(SQLITE_ENABLE_SESSION)
7884/*
7885** State information for a single open session
7886*/
7887typedef struct OpenSession OpenSession;
7888struct OpenSession {
7889  char *zName;             /* Symbolic name for this session */
7890  int nFilter;             /* Number of xFilter rejection GLOB patterns */
7891  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
7892  sqlite3_session *p;      /* The open session */
7893};
7894#endif
7895
7896/*
7897** Shell output mode information from before ".explain on",
7898** saved so that it can be restored by ".explain off"
7899*/
7900typedef struct SavedModeInfo SavedModeInfo;
7901struct SavedModeInfo {
7902  int valid;          /* Is there legit data in here? */
7903  int mode;           /* Mode prior to ".explain on" */
7904  int showHeader;     /* The ".header" setting prior to ".explain on" */
7905  int colWidth[100];  /* Column widths prior to ".explain on" */
7906};
7907
7908typedef struct ExpertInfo ExpertInfo;
7909struct ExpertInfo {
7910  sqlite3expert *pExpert;
7911  int bVerbose;
7912};
7913
7914/*
7915** State information about the database connection is contained in an
7916** instance of the following structure.
7917*/
7918typedef struct ShellState ShellState;
7919struct ShellState {
7920  sqlite3 *db;           /* The database */
7921  u8 autoExplain;        /* Automatically turn on .explain mode */
7922  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
7923  u8 statsOn;            /* True to display memory stats before each finalize */
7924  u8 scanstatsOn;        /* True to display scan stats before each finalize */
7925  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
7926  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
7927  int outCount;          /* Revert to stdout when reaching zero */
7928  int cnt;               /* Number of records displayed so far */
7929  FILE *out;             /* Write results here */
7930  FILE *traceOut;        /* Output for sqlite3_trace() */
7931  int nErr;              /* Number of errors seen */
7932  int mode;              /* An output mode setting */
7933  int modePrior;         /* Saved mode */
7934  int cMode;             /* temporary output mode for the current query */
7935  int normalMode;        /* Output mode before ".explain on" */
7936  int writableSchema;    /* True if PRAGMA writable_schema=ON */
7937  int showHeader;        /* True to show column names in List or Column mode */
7938  int nCheck;            /* Number of ".check" commands run */
7939  unsigned shellFlgs;    /* Various flags */
7940  char *zDestTable;      /* Name of destination table when MODE_Insert */
7941  char *zTempFile;       /* Temporary file that might need deleting */
7942  char zTestcase[30];    /* Name of current test case */
7943  char colSeparator[20]; /* Column separator character for several modes */
7944  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
7945  char colSepPrior[20];  /* Saved column separator */
7946  char rowSepPrior[20];  /* Saved row separator */
7947  int colWidth[100];     /* Requested width of each column when in column mode*/
7948  int actualWidth[100];  /* Actual width of each column */
7949  char nullValue[20];    /* The text to print when a NULL comes back from
7950                         ** the database */
7951  char outfile[FILENAME_MAX]; /* Filename for *out */
7952  const char *zDbFilename;    /* name of the database file */
7953  char *zFreeOnClose;         /* Filename to free when closing */
7954  const char *zVfs;           /* Name of VFS to use */
7955  sqlite3_stmt *pStmt;   /* Current statement if any. */
7956  FILE *pLog;            /* Write log output here */
7957  int *aiIndent;         /* Array of indents used in MODE_Explain */
7958  int nIndent;           /* Size of array aiIndent[] */
7959  int iIndent;           /* Index of current op in aiIndent[] */
7960#if defined(SQLITE_ENABLE_SESSION)
7961  int nSession;             /* Number of active sessions */
7962  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
7963#endif
7964  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
7965};
7966
7967
7968/* Allowed values for ShellState.autoEQP
7969*/
7970#define AUTOEQP_off      0
7971#define AUTOEQP_on       1
7972#define AUTOEQP_trigger  2
7973#define AUTOEQP_full     3
7974
7975/* Allowed values for ShellState.openMode
7976*/
7977#define SHELL_OPEN_UNSPEC     0      /* No open-mode specified */
7978#define SHELL_OPEN_NORMAL     1      /* Normal database file */
7979#define SHELL_OPEN_APPENDVFS  2      /* Use appendvfs */
7980#define SHELL_OPEN_ZIPFILE    3      /* Use the zipfile virtual table */
7981
7982/*
7983** These are the allowed shellFlgs values
7984*/
7985#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
7986#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
7987#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
7988#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
7989#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
7990#define SHFLG_CountChanges   0x00000020 /* .changes setting */
7991#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
7992
7993/*
7994** Macros for testing and setting shellFlgs
7995*/
7996#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
7997#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
7998#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
7999
8000/*
8001** These are the allowed modes.
8002*/
8003#define MODE_Line     0  /* One column per line.  Blank line between records */
8004#define MODE_Column   1  /* One record per line in neat columns */
8005#define MODE_List     2  /* One record per line with a separator */
8006#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
8007#define MODE_Html     4  /* Generate an XHTML table */
8008#define MODE_Insert   5  /* Generate SQL "insert" statements */
8009#define MODE_Quote    6  /* Quote values as for SQL */
8010#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
8011#define MODE_Csv      8  /* Quote strings, numbers are plain */
8012#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
8013#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
8014#define MODE_Pretty  11  /* Pretty-print schemas */
8015
8016static const char *modeDescr[] = {
8017  "line",
8018  "column",
8019  "list",
8020  "semi",
8021  "html",
8022  "insert",
8023  "quote",
8024  "tcl",
8025  "csv",
8026  "explain",
8027  "ascii",
8028  "prettyprint",
8029};
8030
8031/*
8032** These are the column/row/line separators used by the various
8033** import/export modes.
8034*/
8035#define SEP_Column    "|"
8036#define SEP_Row       "\n"
8037#define SEP_Tab       "\t"
8038#define SEP_Space     " "
8039#define SEP_Comma     ","
8040#define SEP_CrLf      "\r\n"
8041#define SEP_Unit      "\x1F"
8042#define SEP_Record    "\x1E"
8043
8044/*
8045** A callback for the sqlite3_log() interface.
8046*/
8047static void shellLog(void *pArg, int iErrCode, const char *zMsg){
8048  ShellState *p = (ShellState*)pArg;
8049  if( p->pLog==0 ) return;
8050  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
8051  fflush(p->pLog);
8052}
8053
8054/*
8055** SQL function:  shell_putsnl(X)
8056**
8057** Write the text X to the screen (or whatever output is being directed)
8058** adding a newline at the end, and then return X.
8059*/
8060static void shellPutsFunc(
8061  sqlite3_context *pCtx,
8062  int nVal,
8063  sqlite3_value **apVal
8064){
8065  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
8066  (void)nVal;
8067  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
8068  sqlite3_result_value(pCtx, apVal[0]);
8069}
8070
8071/*
8072** SQL function:   edit(VALUE)
8073**                 edit(VALUE,EDITOR)
8074**
8075** These steps:
8076**
8077**     (1) Write VALUE into a temporary file.
8078**     (2) Run program EDITOR on that temporary file.
8079**     (3) Read the temporary file back and return its content as the result.
8080**     (4) Delete the temporary file
8081**
8082** If the EDITOR argument is omitted, use the value in the VISUAL
8083** environment variable.  If still there is no EDITOR, through an error.
8084**
8085** Also throw an error if the EDITOR program returns a non-zero exit code.
8086*/
8087static void editFunc(
8088  sqlite3_context *context,
8089  int argc,
8090  sqlite3_value **argv
8091){
8092  const char *zEditor;
8093  char *zTempFile = 0;
8094  sqlite3 *db;
8095  char *zCmd = 0;
8096  int bBin;
8097  int rc;
8098  FILE *f = 0;
8099  sqlite3_int64 sz;
8100  sqlite3_int64 x;
8101  unsigned char *p = 0;
8102
8103  if( argc==2 ){
8104    zEditor = (const char*)sqlite3_value_text(argv[1]);
8105  }else{
8106    zEditor = getenv("VISUAL");
8107  }
8108  if( zEditor==0 ){
8109    sqlite3_result_error(context, "no editor for edit()", -1);
8110    return;
8111  }
8112  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
8113    sqlite3_result_error(context, "NULL input to edit()", -1);
8114    return;
8115  }
8116  db = sqlite3_context_db_handle(context);
8117  zTempFile = 0;
8118  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
8119  if( zTempFile==0 ){
8120    sqlite3_uint64 r = 0;
8121    sqlite3_randomness(sizeof(r), &r);
8122    zTempFile = sqlite3_mprintf("temp%llx", r);
8123    if( zTempFile==0 ){
8124      sqlite3_result_error_nomem(context);
8125      return;
8126    }
8127  }
8128  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
8129  f = fopen(zTempFile, bBin ? "wb" : "w");
8130  if( f==0 ){
8131    sqlite3_result_error(context, "edit() cannot open temp file", -1);
8132    goto edit_func_end;
8133  }
8134  sz = sqlite3_value_bytes(argv[0]);
8135  if( bBin ){
8136    x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
8137  }else{
8138    x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
8139  }
8140  fclose(f);
8141  f = 0;
8142  if( x!=sz ){
8143    sqlite3_result_error(context, "edit() could not write the whole file", -1);
8144    goto edit_func_end;
8145  }
8146  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
8147  if( zCmd==0 ){
8148    sqlite3_result_error_nomem(context);
8149    goto edit_func_end;
8150  }
8151  rc = system(zCmd);
8152  sqlite3_free(zCmd);
8153  if( rc ){
8154    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
8155    goto edit_func_end;
8156  }
8157  f = fopen(zTempFile, bBin ? "rb" : "r");
8158  if( f==0 ){
8159    sqlite3_result_error(context,
8160      "edit() cannot reopen temp file after edit", -1);
8161    goto edit_func_end;
8162  }
8163  fseek(f, 0, SEEK_END);
8164  sz = ftell(f);
8165  rewind(f);
8166  p = sqlite3_malloc64( sz+(bBin==0) );
8167  if( p==0 ){
8168    sqlite3_result_error_nomem(context);
8169    goto edit_func_end;
8170  }
8171  if( bBin ){
8172    x = fread(p, 1, sz, f);
8173  }else{
8174    x = fread(p, 1, sz, f);
8175    p[sz] = 0;
8176  }
8177  fclose(f);
8178  f = 0;
8179  if( x!=sz ){
8180    sqlite3_result_error(context, "could not read back the whole file", -1);
8181    goto edit_func_end;
8182  }
8183  if( bBin ){
8184    sqlite3_result_blob(context, p, sz, sqlite3_free);
8185  }else{
8186    sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
8187  }
8188  p = 0;
8189
8190edit_func_end:
8191  if( f ) fclose(f);
8192  unlink(zTempFile);
8193  sqlite3_free(zTempFile);
8194  sqlite3_free(p);
8195}
8196
8197/*
8198** Save or restore the current output mode
8199*/
8200static void outputModePush(ShellState *p){
8201  p->modePrior = p->mode;
8202  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
8203  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
8204}
8205static void outputModePop(ShellState *p){
8206  p->mode = p->modePrior;
8207  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
8208  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
8209}
8210
8211/*
8212** Output the given string as a hex-encoded blob (eg. X'1234' )
8213*/
8214static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
8215  int i;
8216  char *zBlob = (char *)pBlob;
8217  raw_printf(out,"X'");
8218  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
8219  raw_printf(out,"'");
8220}
8221
8222/*
8223** Find a string that is not found anywhere in z[].  Return a pointer
8224** to that string.
8225**
8226** Try to use zA and zB first.  If both of those are already found in z[]
8227** then make up some string and store it in the buffer zBuf.
8228*/
8229static const char *unused_string(
8230  const char *z,                    /* Result must not appear anywhere in z */
8231  const char *zA, const char *zB,   /* Try these first */
8232  char *zBuf                        /* Space to store a generated string */
8233){
8234  unsigned i = 0;
8235  if( strstr(z, zA)==0 ) return zA;
8236  if( strstr(z, zB)==0 ) return zB;
8237  do{
8238    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
8239  }while( strstr(z,zBuf)!=0 );
8240  return zBuf;
8241}
8242
8243/*
8244** Output the given string as a quoted string using SQL quoting conventions.
8245**
8246** See also: output_quoted_escaped_string()
8247*/
8248static void output_quoted_string(FILE *out, const char *z){
8249  int i;
8250  char c;
8251  setBinaryMode(out, 1);
8252  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8253  if( c==0 ){
8254    utf8_printf(out,"'%s'",z);
8255  }else{
8256    raw_printf(out, "'");
8257    while( *z ){
8258      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8259      if( c=='\'' ) i++;
8260      if( i ){
8261        utf8_printf(out, "%.*s", i, z);
8262        z += i;
8263      }
8264      if( c=='\'' ){
8265        raw_printf(out, "'");
8266        continue;
8267      }
8268      if( c==0 ){
8269        break;
8270      }
8271      z++;
8272    }
8273    raw_printf(out, "'");
8274  }
8275  setTextMode(out, 1);
8276}
8277
8278/*
8279** Output the given string as a quoted string using SQL quoting conventions.
8280** Additionallly , escape the "\n" and "\r" characters so that they do not
8281** get corrupted by end-of-line translation facilities in some operating
8282** systems.
8283**
8284** This is like output_quoted_string() but with the addition of the \r\n
8285** escape mechanism.
8286*/
8287static void output_quoted_escaped_string(FILE *out, const char *z){
8288  int i;
8289  char c;
8290  setBinaryMode(out, 1);
8291  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
8292  if( c==0 ){
8293    utf8_printf(out,"'%s'",z);
8294  }else{
8295    const char *zNL = 0;
8296    const char *zCR = 0;
8297    int nNL = 0;
8298    int nCR = 0;
8299    char zBuf1[20], zBuf2[20];
8300    for(i=0; z[i]; i++){
8301      if( z[i]=='\n' ) nNL++;
8302      if( z[i]=='\r' ) nCR++;
8303    }
8304    if( nNL ){
8305      raw_printf(out, "replace(");
8306      zNL = unused_string(z, "\\n", "\\012", zBuf1);
8307    }
8308    if( nCR ){
8309      raw_printf(out, "replace(");
8310      zCR = unused_string(z, "\\r", "\\015", zBuf2);
8311    }
8312    raw_printf(out, "'");
8313    while( *z ){
8314      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
8315      if( c=='\'' ) i++;
8316      if( i ){
8317        utf8_printf(out, "%.*s", i, z);
8318        z += i;
8319      }
8320      if( c=='\'' ){
8321        raw_printf(out, "'");
8322        continue;
8323      }
8324      if( c==0 ){
8325        break;
8326      }
8327      z++;
8328      if( c=='\n' ){
8329        raw_printf(out, "%s", zNL);
8330        continue;
8331      }
8332      raw_printf(out, "%s", zCR);
8333    }
8334    raw_printf(out, "'");
8335    if( nCR ){
8336      raw_printf(out, ",'%s',char(13))", zCR);
8337    }
8338    if( nNL ){
8339      raw_printf(out, ",'%s',char(10))", zNL);
8340    }
8341  }
8342  setTextMode(out, 1);
8343}
8344
8345/*
8346** Output the given string as a quoted according to C or TCL quoting rules.
8347*/
8348static void output_c_string(FILE *out, const char *z){
8349  unsigned int c;
8350  fputc('"', out);
8351  while( (c = *(z++))!=0 ){
8352    if( c=='\\' ){
8353      fputc(c, out);
8354      fputc(c, out);
8355    }else if( c=='"' ){
8356      fputc('\\', out);
8357      fputc('"', out);
8358    }else if( c=='\t' ){
8359      fputc('\\', out);
8360      fputc('t', out);
8361    }else if( c=='\n' ){
8362      fputc('\\', out);
8363      fputc('n', out);
8364    }else if( c=='\r' ){
8365      fputc('\\', out);
8366      fputc('r', out);
8367    }else if( !isprint(c&0xff) ){
8368      raw_printf(out, "\\%03o", c&0xff);
8369    }else{
8370      fputc(c, out);
8371    }
8372  }
8373  fputc('"', out);
8374}
8375
8376/*
8377** Output the given string with characters that are special to
8378** HTML escaped.
8379*/
8380static void output_html_string(FILE *out, const char *z){
8381  int i;
8382  if( z==0 ) z = "";
8383  while( *z ){
8384    for(i=0;   z[i]
8385            && z[i]!='<'
8386            && z[i]!='&'
8387            && z[i]!='>'
8388            && z[i]!='\"'
8389            && z[i]!='\'';
8390        i++){}
8391    if( i>0 ){
8392      utf8_printf(out,"%.*s",i,z);
8393    }
8394    if( z[i]=='<' ){
8395      raw_printf(out,"&lt;");
8396    }else if( z[i]=='&' ){
8397      raw_printf(out,"&amp;");
8398    }else if( z[i]=='>' ){
8399      raw_printf(out,"&gt;");
8400    }else if( z[i]=='\"' ){
8401      raw_printf(out,"&quot;");
8402    }else if( z[i]=='\'' ){
8403      raw_printf(out,"&#39;");
8404    }else{
8405      break;
8406    }
8407    z += i + 1;
8408  }
8409}
8410
8411/*
8412** If a field contains any character identified by a 1 in the following
8413** array, then the string must be quoted for CSV.
8414*/
8415static const char needCsvQuote[] = {
8416  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8417  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8418  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
8419  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
8420  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
8421  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
8422  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
8423  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
8424  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8425  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8426  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8427  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8428  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8429  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8430  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8431  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
8432};
8433
8434/*
8435** Output a single term of CSV.  Actually, p->colSeparator is used for
8436** the separator, which may or may not be a comma.  p->nullValue is
8437** the null value.  Strings are quoted if necessary.  The separator
8438** is only issued if bSep is true.
8439*/
8440static void output_csv(ShellState *p, const char *z, int bSep){
8441  FILE *out = p->out;
8442  if( z==0 ){
8443    utf8_printf(out,"%s",p->nullValue);
8444  }else{
8445    int i;
8446    int nSep = strlen30(p->colSeparator);
8447    for(i=0; z[i]; i++){
8448      if( needCsvQuote[((unsigned char*)z)[i]]
8449         || (z[i]==p->colSeparator[0] &&
8450             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
8451        i = 0;
8452        break;
8453      }
8454    }
8455    if( i==0 ){
8456      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
8457      utf8_printf(out, "%s", zQuoted);
8458      sqlite3_free(zQuoted);
8459    }else{
8460      utf8_printf(out, "%s", z);
8461    }
8462  }
8463  if( bSep ){
8464    utf8_printf(p->out, "%s", p->colSeparator);
8465  }
8466}
8467
8468/*
8469** This routine runs when the user presses Ctrl-C
8470*/
8471static void interrupt_handler(int NotUsed){
8472  UNUSED_PARAMETER(NotUsed);
8473  seenInterrupt++;
8474  if( seenInterrupt>2 ) exit(1);
8475  if( globalDb ) sqlite3_interrupt(globalDb);
8476}
8477
8478#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
8479/*
8480** This routine runs for console events (e.g. Ctrl-C) on Win32
8481*/
8482static BOOL WINAPI ConsoleCtrlHandler(
8483  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
8484){
8485  if( dwCtrlType==CTRL_C_EVENT ){
8486    interrupt_handler(0);
8487    return TRUE;
8488  }
8489  return FALSE;
8490}
8491#endif
8492
8493#ifndef SQLITE_OMIT_AUTHORIZATION
8494/*
8495** When the ".auth ON" is set, the following authorizer callback is
8496** invoked.  It always returns SQLITE_OK.
8497*/
8498static int shellAuth(
8499  void *pClientData,
8500  int op,
8501  const char *zA1,
8502  const char *zA2,
8503  const char *zA3,
8504  const char *zA4
8505){
8506  ShellState *p = (ShellState*)pClientData;
8507  static const char *azAction[] = { 0,
8508     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
8509     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
8510     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
8511     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
8512     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
8513     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
8514     "PRAGMA",               "READ",                 "SELECT",
8515     "TRANSACTION",          "UPDATE",               "ATTACH",
8516     "DETACH",               "ALTER_TABLE",          "REINDEX",
8517     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
8518     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
8519  };
8520  int i;
8521  const char *az[4];
8522  az[0] = zA1;
8523  az[1] = zA2;
8524  az[2] = zA3;
8525  az[3] = zA4;
8526  utf8_printf(p->out, "authorizer: %s", azAction[op]);
8527  for(i=0; i<4; i++){
8528    raw_printf(p->out, " ");
8529    if( az[i] ){
8530      output_c_string(p->out, az[i]);
8531    }else{
8532      raw_printf(p->out, "NULL");
8533    }
8534  }
8535  raw_printf(p->out, "\n");
8536  return SQLITE_OK;
8537}
8538#endif
8539
8540/*
8541** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
8542**
8543** This routine converts some CREATE TABLE statements for shadow tables
8544** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
8545*/
8546static void printSchemaLine(FILE *out, const char *z, const char *zTail){
8547  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
8548    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
8549  }else{
8550    utf8_printf(out, "%s%s", z, zTail);
8551  }
8552}
8553static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
8554  char c = z[n];
8555  z[n] = 0;
8556  printSchemaLine(out, z, zTail);
8557  z[n] = c;
8558}
8559
8560/*
8561** Return true if string z[] has nothing but whitespace and comments to the
8562** end of the first line.
8563*/
8564static int wsToEol(const char *z){
8565  int i;
8566  for(i=0; z[i]; i++){
8567    if( z[i]=='\n' ) return 1;
8568    if( IsSpace(z[i]) ) continue;
8569    if( z[i]=='-' && z[i+1]=='-' ) return 1;
8570    return 0;
8571  }
8572  return 1;
8573}
8574
8575
8576/*
8577** This is the callback routine that the shell
8578** invokes for each row of a query result.
8579*/
8580static int shell_callback(
8581  void *pArg,
8582  int nArg,        /* Number of result columns */
8583  char **azArg,    /* Text of each result column */
8584  char **azCol,    /* Column names */
8585  int *aiType      /* Column types */
8586){
8587  int i;
8588  ShellState *p = (ShellState*)pArg;
8589
8590  if( azArg==0 ) return 0;
8591  switch( p->cMode ){
8592    case MODE_Line: {
8593      int w = 5;
8594      if( azArg==0 ) break;
8595      for(i=0; i<nArg; i++){
8596        int len = strlen30(azCol[i] ? azCol[i] : "");
8597        if( len>w ) w = len;
8598      }
8599      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
8600      for(i=0; i<nArg; i++){
8601        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
8602                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
8603      }
8604      break;
8605    }
8606    case MODE_Explain:
8607    case MODE_Column: {
8608      static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
8609      const int *colWidth;
8610      int showHdr;
8611      char *rowSep;
8612      if( p->cMode==MODE_Column ){
8613        colWidth = p->colWidth;
8614        showHdr = p->showHeader;
8615        rowSep = p->rowSeparator;
8616      }else{
8617        colWidth = aExplainWidths;
8618        showHdr = 1;
8619        rowSep = SEP_Row;
8620      }
8621      if( p->cnt++==0 ){
8622        for(i=0; i<nArg; i++){
8623          int w, n;
8624          if( i<ArraySize(p->colWidth) ){
8625            w = colWidth[i];
8626          }else{
8627            w = 0;
8628          }
8629          if( w==0 ){
8630            w = strlenChar(azCol[i] ? azCol[i] : "");
8631            if( w<10 ) w = 10;
8632            n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
8633            if( w<n ) w = n;
8634          }
8635          if( i<ArraySize(p->actualWidth) ){
8636            p->actualWidth[i] = w;
8637          }
8638          if( showHdr ){
8639            utf8_width_print(p->out, w, azCol[i]);
8640            utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
8641          }
8642        }
8643        if( showHdr ){
8644          for(i=0; i<nArg; i++){
8645            int w;
8646            if( i<ArraySize(p->actualWidth) ){
8647               w = p->actualWidth[i];
8648               if( w<0 ) w = -w;
8649            }else{
8650               w = 10;
8651            }
8652            utf8_printf(p->out,"%-*.*s%s",w,w,
8653                   "----------------------------------------------------------"
8654                   "----------------------------------------------------------",
8655                    i==nArg-1 ? rowSep : "  ");
8656          }
8657        }
8658      }
8659      if( azArg==0 ) break;
8660      for(i=0; i<nArg; i++){
8661        int w;
8662        if( i<ArraySize(p->actualWidth) ){
8663           w = p->actualWidth[i];
8664        }else{
8665           w = 10;
8666        }
8667        if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
8668          w = strlenChar(azArg[i]);
8669        }
8670        if( i==1 && p->aiIndent && p->pStmt ){
8671          if( p->iIndent<p->nIndent ){
8672            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
8673          }
8674          p->iIndent++;
8675        }
8676        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
8677        utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
8678      }
8679      break;
8680    }
8681    case MODE_Semi: {   /* .schema and .fullschema output */
8682      printSchemaLine(p->out, azArg[0], ";\n");
8683      break;
8684    }
8685    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
8686      char *z;
8687      int j;
8688      int nParen = 0;
8689      char cEnd = 0;
8690      char c;
8691      int nLine = 0;
8692      assert( nArg==1 );
8693      if( azArg[0]==0 ) break;
8694      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
8695       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
8696      ){
8697        utf8_printf(p->out, "%s;\n", azArg[0]);
8698        break;
8699      }
8700      z = sqlite3_mprintf("%s", azArg[0]);
8701      j = 0;
8702      for(i=0; IsSpace(z[i]); i++){}
8703      for(; (c = z[i])!=0; i++){
8704        if( IsSpace(c) ){
8705          if( z[j-1]=='\r' ) z[j-1] = '\n';
8706          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
8707        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
8708          j--;
8709        }
8710        z[j++] = c;
8711      }
8712      while( j>0 && IsSpace(z[j-1]) ){ j--; }
8713      z[j] = 0;
8714      if( strlen30(z)>=79 ){
8715        for(i=j=0; (c = z[i])!=0; i++){  /* Copy changes from z[i] back to z[j] */
8716          if( c==cEnd ){
8717            cEnd = 0;
8718          }else if( c=='"' || c=='\'' || c=='`' ){
8719            cEnd = c;
8720          }else if( c=='[' ){
8721            cEnd = ']';
8722          }else if( c=='-' && z[i+1]=='-' ){
8723            cEnd = '\n';
8724          }else if( c=='(' ){
8725            nParen++;
8726          }else if( c==')' ){
8727            nParen--;
8728            if( nLine>0 && nParen==0 && j>0 ){
8729              printSchemaLineN(p->out, z, j, "\n");
8730              j = 0;
8731            }
8732          }
8733          z[j++] = c;
8734          if( nParen==1 && cEnd==0
8735           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
8736          ){
8737            if( c=='\n' ) j--;
8738            printSchemaLineN(p->out, z, j, "\n  ");
8739            j = 0;
8740            nLine++;
8741            while( IsSpace(z[i+1]) ){ i++; }
8742          }
8743        }
8744        z[j] = 0;
8745      }
8746      printSchemaLine(p->out, z, ";\n");
8747      sqlite3_free(z);
8748      break;
8749    }
8750    case MODE_List: {
8751      if( p->cnt++==0 && p->showHeader ){
8752        for(i=0; i<nArg; i++){
8753          utf8_printf(p->out,"%s%s",azCol[i],
8754                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
8755        }
8756      }
8757      if( azArg==0 ) break;
8758      for(i=0; i<nArg; i++){
8759        char *z = azArg[i];
8760        if( z==0 ) z = p->nullValue;
8761        utf8_printf(p->out, "%s", z);
8762        if( i<nArg-1 ){
8763          utf8_printf(p->out, "%s", p->colSeparator);
8764        }else{
8765          utf8_printf(p->out, "%s", p->rowSeparator);
8766        }
8767      }
8768      break;
8769    }
8770    case MODE_Html: {
8771      if( p->cnt++==0 && p->showHeader ){
8772        raw_printf(p->out,"<TR>");
8773        for(i=0; i<nArg; i++){
8774          raw_printf(p->out,"<TH>");
8775          output_html_string(p->out, azCol[i]);
8776          raw_printf(p->out,"</TH>\n");
8777        }
8778        raw_printf(p->out,"</TR>\n");
8779      }
8780      if( azArg==0 ) break;
8781      raw_printf(p->out,"<TR>");
8782      for(i=0; i<nArg; i++){
8783        raw_printf(p->out,"<TD>");
8784        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
8785        raw_printf(p->out,"</TD>\n");
8786      }
8787      raw_printf(p->out,"</TR>\n");
8788      break;
8789    }
8790    case MODE_Tcl: {
8791      if( p->cnt++==0 && p->showHeader ){
8792        for(i=0; i<nArg; i++){
8793          output_c_string(p->out,azCol[i] ? azCol[i] : "");
8794          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
8795        }
8796        utf8_printf(p->out, "%s", p->rowSeparator);
8797      }
8798      if( azArg==0 ) break;
8799      for(i=0; i<nArg; i++){
8800        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
8801        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
8802      }
8803      utf8_printf(p->out, "%s", p->rowSeparator);
8804      break;
8805    }
8806    case MODE_Csv: {
8807      setBinaryMode(p->out, 1);
8808      if( p->cnt++==0 && p->showHeader ){
8809        for(i=0; i<nArg; i++){
8810          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
8811        }
8812        utf8_printf(p->out, "%s", p->rowSeparator);
8813      }
8814      if( nArg>0 ){
8815        for(i=0; i<nArg; i++){
8816          output_csv(p, azArg[i], i<nArg-1);
8817        }
8818        utf8_printf(p->out, "%s", p->rowSeparator);
8819      }
8820      setTextMode(p->out, 1);
8821      break;
8822    }
8823    case MODE_Insert: {
8824      if( azArg==0 ) break;
8825      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
8826      if( p->showHeader ){
8827        raw_printf(p->out,"(");
8828        for(i=0; i<nArg; i++){
8829          if( i>0 ) raw_printf(p->out, ",");
8830          if( quoteChar(azCol[i]) ){
8831            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
8832            utf8_printf(p->out, "%s", z);
8833            sqlite3_free(z);
8834          }else{
8835            raw_printf(p->out, "%s", azCol[i]);
8836          }
8837        }
8838        raw_printf(p->out,")");
8839      }
8840      p->cnt++;
8841      for(i=0; i<nArg; i++){
8842        raw_printf(p->out, i>0 ? "," : " VALUES(");
8843        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
8844          utf8_printf(p->out,"NULL");
8845        }else if( aiType && aiType[i]==SQLITE_TEXT ){
8846          if( ShellHasFlag(p, SHFLG_Newlines) ){
8847            output_quoted_string(p->out, azArg[i]);
8848          }else{
8849            output_quoted_escaped_string(p->out, azArg[i]);
8850          }
8851        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
8852          utf8_printf(p->out,"%s", azArg[i]);
8853        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
8854          char z[50];
8855          double r = sqlite3_column_double(p->pStmt, i);
8856          sqlite3_snprintf(50,z,"%!.20g", r);
8857          raw_printf(p->out, "%s", z);
8858        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
8859          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
8860          int nBlob = sqlite3_column_bytes(p->pStmt, i);
8861          output_hex_blob(p->out, pBlob, nBlob);
8862        }else if( isNumber(azArg[i], 0) ){
8863          utf8_printf(p->out,"%s", azArg[i]);
8864        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
8865          output_quoted_string(p->out, azArg[i]);
8866        }else{
8867          output_quoted_escaped_string(p->out, azArg[i]);
8868        }
8869      }
8870      raw_printf(p->out,");\n");
8871      break;
8872    }
8873    case MODE_Quote: {
8874      if( azArg==0 ) break;
8875      if( p->cnt==0 && p->showHeader ){
8876        for(i=0; i<nArg; i++){
8877          if( i>0 ) raw_printf(p->out, ",");
8878          output_quoted_string(p->out, azCol[i]);
8879        }
8880        raw_printf(p->out,"\n");
8881      }
8882      p->cnt++;
8883      for(i=0; i<nArg; i++){
8884        if( i>0 ) raw_printf(p->out, ",");
8885        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
8886          utf8_printf(p->out,"NULL");
8887        }else if( aiType && aiType[i]==SQLITE_TEXT ){
8888          output_quoted_string(p->out, azArg[i]);
8889        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
8890          utf8_printf(p->out,"%s", azArg[i]);
8891        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
8892          char z[50];
8893          double r = sqlite3_column_double(p->pStmt, i);
8894          sqlite3_snprintf(50,z,"%!.20g", r);
8895          raw_printf(p->out, "%s", z);
8896        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
8897          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
8898          int nBlob = sqlite3_column_bytes(p->pStmt, i);
8899          output_hex_blob(p->out, pBlob, nBlob);
8900        }else if( isNumber(azArg[i], 0) ){
8901          utf8_printf(p->out,"%s", azArg[i]);
8902        }else{
8903          output_quoted_string(p->out, azArg[i]);
8904        }
8905      }
8906      raw_printf(p->out,"\n");
8907      break;
8908    }
8909    case MODE_Ascii: {
8910      if( p->cnt++==0 && p->showHeader ){
8911        for(i=0; i<nArg; i++){
8912          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
8913          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
8914        }
8915        utf8_printf(p->out, "%s", p->rowSeparator);
8916      }
8917      if( azArg==0 ) break;
8918      for(i=0; i<nArg; i++){
8919        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
8920        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
8921      }
8922      utf8_printf(p->out, "%s", p->rowSeparator);
8923      break;
8924    }
8925  }
8926  return 0;
8927}
8928
8929/*
8930** This is the callback routine that the SQLite library
8931** invokes for each row of a query result.
8932*/
8933static int callback(void *pArg, int nArg, char **azArg, char **azCol){
8934  /* since we don't have type info, call the shell_callback with a NULL value */
8935  return shell_callback(pArg, nArg, azArg, azCol, NULL);
8936}
8937
8938/*
8939** This is the callback routine from sqlite3_exec() that appends all
8940** output onto the end of a ShellText object.
8941*/
8942static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
8943  ShellText *p = (ShellText*)pArg;
8944  int i;
8945  UNUSED_PARAMETER(az);
8946  if( azArg==0 ) return 0;
8947  if( p->n ) appendText(p, "|", 0);
8948  for(i=0; i<nArg; i++){
8949    if( i ) appendText(p, ",", 0);
8950    if( azArg[i] ) appendText(p, azArg[i], 0);
8951  }
8952  return 0;
8953}
8954
8955/*
8956** Generate an appropriate SELFTEST table in the main database.
8957*/
8958static void createSelftestTable(ShellState *p){
8959  char *zErrMsg = 0;
8960  sqlite3_exec(p->db,
8961    "SAVEPOINT selftest_init;\n"
8962    "CREATE TABLE IF NOT EXISTS selftest(\n"
8963    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
8964    "  op TEXT,\n"                   /* Operator:  memo run */
8965    "  cmd TEXT,\n"                  /* Command text */
8966    "  ans TEXT\n"                   /* Desired answer */
8967    ");"
8968    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
8969    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
8970    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
8971    "         'memo','Tests generated by --init');\n"
8972    "INSERT INTO [_shell$self]\n"
8973    "  SELECT 'run',\n"
8974    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
8975                                 "FROM sqlite_master ORDER BY 2'',224))',\n"
8976    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
8977                          "FROM sqlite_master ORDER BY 2',224));\n"
8978    "INSERT INTO [_shell$self]\n"
8979    "  SELECT 'run',"
8980    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
8981    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
8982    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
8983    "  FROM (\n"
8984    "    SELECT name FROM sqlite_master\n"
8985    "     WHERE type='table'\n"
8986    "       AND name<>'selftest'\n"
8987    "       AND coalesce(rootpage,0)>0\n"
8988    "  )\n"
8989    " ORDER BY name;\n"
8990    "INSERT INTO [_shell$self]\n"
8991    "  VALUES('run','PRAGMA integrity_check','ok');\n"
8992    "INSERT INTO selftest(tno,op,cmd,ans)"
8993    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
8994    "DROP TABLE [_shell$self];"
8995    ,0,0,&zErrMsg);
8996  if( zErrMsg ){
8997    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
8998    sqlite3_free(zErrMsg);
8999  }
9000  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
9001}
9002
9003
9004/*
9005** Set the destination table field of the ShellState structure to
9006** the name of the table given.  Escape any quote characters in the
9007** table name.
9008*/
9009static void set_table_name(ShellState *p, const char *zName){
9010  int i, n;
9011  char cQuote;
9012  char *z;
9013
9014  if( p->zDestTable ){
9015    free(p->zDestTable);
9016    p->zDestTable = 0;
9017  }
9018  if( zName==0 ) return;
9019  cQuote = quoteChar(zName);
9020  n = strlen30(zName);
9021  if( cQuote ) n += n+2;
9022  z = p->zDestTable = malloc( n+1 );
9023  if( z==0 ){
9024    raw_printf(stderr,"Error: out of memory\n");
9025    exit(1);
9026  }
9027  n = 0;
9028  if( cQuote ) z[n++] = cQuote;
9029  for(i=0; zName[i]; i++){
9030    z[n++] = zName[i];
9031    if( zName[i]==cQuote ) z[n++] = cQuote;
9032  }
9033  if( cQuote ) z[n++] = cQuote;
9034  z[n] = 0;
9035}
9036
9037
9038/*
9039** Execute a query statement that will generate SQL output.  Print
9040** the result columns, comma-separated, on a line and then add a
9041** semicolon terminator to the end of that line.
9042**
9043** If the number of columns is 1 and that column contains text "--"
9044** then write the semicolon on a separate line.  That way, if a
9045** "--" comment occurs at the end of the statement, the comment
9046** won't consume the semicolon terminator.
9047*/
9048static int run_table_dump_query(
9049  ShellState *p,           /* Query context */
9050  const char *zSelect,     /* SELECT statement to extract content */
9051  const char *zFirstRow    /* Print before first row, if not NULL */
9052){
9053  sqlite3_stmt *pSelect;
9054  int rc;
9055  int nResult;
9056  int i;
9057  const char *z;
9058  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
9059  if( rc!=SQLITE_OK || !pSelect ){
9060    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9061                sqlite3_errmsg(p->db));
9062    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9063    return rc;
9064  }
9065  rc = sqlite3_step(pSelect);
9066  nResult = sqlite3_column_count(pSelect);
9067  while( rc==SQLITE_ROW ){
9068    if( zFirstRow ){
9069      utf8_printf(p->out, "%s", zFirstRow);
9070      zFirstRow = 0;
9071    }
9072    z = (const char*)sqlite3_column_text(pSelect, 0);
9073    utf8_printf(p->out, "%s", z);
9074    for(i=1; i<nResult; i++){
9075      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
9076    }
9077    if( z==0 ) z = "";
9078    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
9079    if( z[0] ){
9080      raw_printf(p->out, "\n;\n");
9081    }else{
9082      raw_printf(p->out, ";\n");
9083    }
9084    rc = sqlite3_step(pSelect);
9085  }
9086  rc = sqlite3_finalize(pSelect);
9087  if( rc!=SQLITE_OK ){
9088    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9089                sqlite3_errmsg(p->db));
9090    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9091  }
9092  return rc;
9093}
9094
9095/*
9096** Allocate space and save off current error string.
9097*/
9098static char *save_err_msg(
9099  sqlite3 *db            /* Database to query */
9100){
9101  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
9102  char *zErrMsg = sqlite3_malloc64(nErrMsg);
9103  if( zErrMsg ){
9104    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
9105  }
9106  return zErrMsg;
9107}
9108
9109#ifdef __linux__
9110/*
9111** Attempt to display I/O stats on Linux using /proc/PID/io
9112*/
9113static void displayLinuxIoStats(FILE *out){
9114  FILE *in;
9115  char z[200];
9116  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
9117  in = fopen(z, "rb");
9118  if( in==0 ) return;
9119  while( fgets(z, sizeof(z), in)!=0 ){
9120    static const struct {
9121      const char *zPattern;
9122      const char *zDesc;
9123    } aTrans[] = {
9124      { "rchar: ",                  "Bytes received by read():" },
9125      { "wchar: ",                  "Bytes sent to write():"    },
9126      { "syscr: ",                  "Read() system calls:"      },
9127      { "syscw: ",                  "Write() system calls:"     },
9128      { "read_bytes: ",             "Bytes read from storage:"  },
9129      { "write_bytes: ",            "Bytes written to storage:" },
9130      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
9131    };
9132    int i;
9133    for(i=0; i<ArraySize(aTrans); i++){
9134      int n = strlen30(aTrans[i].zPattern);
9135      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
9136        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
9137        break;
9138      }
9139    }
9140  }
9141  fclose(in);
9142}
9143#endif
9144
9145/*
9146** Display a single line of status using 64-bit values.
9147*/
9148static void displayStatLine(
9149  ShellState *p,            /* The shell context */
9150  char *zLabel,             /* Label for this one line */
9151  char *zFormat,            /* Format for the result */
9152  int iStatusCtrl,          /* Which status to display */
9153  int bReset                /* True to reset the stats */
9154){
9155  sqlite3_int64 iCur = -1;
9156  sqlite3_int64 iHiwtr = -1;
9157  int i, nPercent;
9158  char zLine[200];
9159  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
9160  for(i=0, nPercent=0; zFormat[i]; i++){
9161    if( zFormat[i]=='%' ) nPercent++;
9162  }
9163  if( nPercent>1 ){
9164    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
9165  }else{
9166    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
9167  }
9168  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
9169}
9170
9171/*
9172** Display memory stats.
9173*/
9174static int display_stats(
9175  sqlite3 *db,                /* Database to query */
9176  ShellState *pArg,           /* Pointer to ShellState */
9177  int bReset                  /* True to reset the stats */
9178){
9179  int iCur;
9180  int iHiwtr;
9181
9182  if( pArg && pArg->out ){
9183    displayStatLine(pArg, "Memory Used:",
9184       "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9185    displayStatLine(pArg, "Number of Outstanding Allocations:",
9186       "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9187    if( pArg->shellFlgs & SHFLG_Pagecache ){
9188      displayStatLine(pArg, "Number of Pcache Pages Used:",
9189         "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9190    }
9191    displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9192       "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9193    displayStatLine(pArg, "Largest Allocation:",
9194       "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9195    displayStatLine(pArg, "Largest Pcache Allocation:",
9196       "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
9197#ifdef YYTRACKMAXSTACKDEPTH
9198    displayStatLine(pArg, "Deepest Parser Stack:",
9199       "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9200#endif
9201  }
9202
9203  if( pArg && pArg->out && db ){
9204    if( pArg->shellFlgs & SHFLG_Lookaside ){
9205      iHiwtr = iCur = -1;
9206      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
9207                        &iCur, &iHiwtr, bReset);
9208      raw_printf(pArg->out,
9209              "Lookaside Slots Used:                %d (max %d)\n",
9210              iCur, iHiwtr);
9211      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
9212                        &iCur, &iHiwtr, bReset);
9213      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
9214              iHiwtr);
9215      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
9216                        &iCur, &iHiwtr, bReset);
9217      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
9218              iHiwtr);
9219      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
9220                        &iCur, &iHiwtr, bReset);
9221      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
9222              iHiwtr);
9223    }
9224    iHiwtr = iCur = -1;
9225    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
9226    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
9227            iCur);
9228    iHiwtr = iCur = -1;
9229    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
9230    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
9231    iHiwtr = iCur = -1;
9232    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
9233    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
9234    iHiwtr = iCur = -1;
9235    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
9236    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
9237    iHiwtr = iCur = -1;
9238    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
9239    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
9240            iCur);
9241    iHiwtr = iCur = -1;
9242    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
9243    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
9244            iCur);
9245  }
9246
9247  if( pArg && pArg->out && db && pArg->pStmt ){
9248    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
9249                               bReset);
9250    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
9251    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
9252    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
9253    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
9254    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
9255    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
9256    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
9257  }
9258
9259#ifdef __linux__
9260  displayLinuxIoStats(pArg->out);
9261#endif
9262
9263  /* Do not remove this machine readable comment: extra-stats-output-here */
9264
9265  return 0;
9266}
9267
9268/*
9269** Display scan stats.
9270*/
9271static void display_scanstats(
9272  sqlite3 *db,                    /* Database to query */
9273  ShellState *pArg                /* Pointer to ShellState */
9274){
9275#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9276  UNUSED_PARAMETER(db);
9277  UNUSED_PARAMETER(pArg);
9278#else
9279  int i, k, n, mx;
9280  raw_printf(pArg->out, "-------- scanstats --------\n");
9281  mx = 0;
9282  for(k=0; k<=mx; k++){
9283    double rEstLoop = 1.0;
9284    for(i=n=0; 1; i++){
9285      sqlite3_stmt *p = pArg->pStmt;
9286      sqlite3_int64 nLoop, nVisit;
9287      double rEst;
9288      int iSid;
9289      const char *zExplain;
9290      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
9291        break;
9292      }
9293      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
9294      if( iSid>mx ) mx = iSid;
9295      if( iSid!=k ) continue;
9296      if( n==0 ){
9297        rEstLoop = (double)nLoop;
9298        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
9299      }
9300      n++;
9301      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
9302      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
9303      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
9304      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
9305      rEstLoop *= rEst;
9306      raw_printf(pArg->out,
9307          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
9308          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
9309      );
9310    }
9311  }
9312  raw_printf(pArg->out, "---------------------------\n");
9313#endif
9314}
9315
9316/*
9317** Parameter azArray points to a zero-terminated array of strings. zStr
9318** points to a single nul-terminated string. Return non-zero if zStr
9319** is equal, according to strcmp(), to any of the strings in the array.
9320** Otherwise, return zero.
9321*/
9322static int str_in_array(const char *zStr, const char **azArray){
9323  int i;
9324  for(i=0; azArray[i]; i++){
9325    if( 0==strcmp(zStr, azArray[i]) ) return 1;
9326  }
9327  return 0;
9328}
9329
9330/*
9331** If compiled statement pSql appears to be an EXPLAIN statement, allocate
9332** and populate the ShellState.aiIndent[] array with the number of
9333** spaces each opcode should be indented before it is output.
9334**
9335** The indenting rules are:
9336**
9337**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
9338**       all opcodes that occur between the p2 jump destination and the opcode
9339**       itself by 2 spaces.
9340**
9341**     * For each "Goto", if the jump destination is earlier in the program
9342**       and ends on one of:
9343**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
9344**       or if the P1 parameter is one instead of zero,
9345**       then indent all opcodes between the earlier instruction
9346**       and "Goto" by 2 spaces.
9347*/
9348static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
9349  const char *zSql;               /* The text of the SQL statement */
9350  const char *z;                  /* Used to check if this is an EXPLAIN */
9351  int *abYield = 0;               /* True if op is an OP_Yield */
9352  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
9353  int iOp;                        /* Index of operation in p->aiIndent[] */
9354
9355  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
9356                           "NextIfOpen", "PrevIfOpen", 0 };
9357  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
9358                            "Rewind", 0 };
9359  const char *azGoto[] = { "Goto", 0 };
9360
9361  /* Try to figure out if this is really an EXPLAIN statement. If this
9362  ** cannot be verified, return early.  */
9363  if( sqlite3_column_count(pSql)!=8 ){
9364    p->cMode = p->mode;
9365    return;
9366  }
9367  zSql = sqlite3_sql(pSql);
9368  if( zSql==0 ) return;
9369  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
9370  if( sqlite3_strnicmp(z, "explain", 7) ){
9371    p->cMode = p->mode;
9372    return;
9373  }
9374
9375  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
9376    int i;
9377    int iAddr = sqlite3_column_int(pSql, 0);
9378    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
9379
9380    /* Set p2 to the P2 field of the current opcode. Then, assuming that
9381    ** p2 is an instruction address, set variable p2op to the index of that
9382    ** instruction in the aiIndent[] array. p2 and p2op may be different if
9383    ** the current instruction is part of a sub-program generated by an
9384    ** SQL trigger or foreign key.  */
9385    int p2 = sqlite3_column_int(pSql, 3);
9386    int p2op = (p2 + (iOp-iAddr));
9387
9388    /* Grow the p->aiIndent array as required */
9389    if( iOp>=nAlloc ){
9390      if( iOp==0 ){
9391        /* Do further verfication that this is explain output.  Abort if
9392        ** it is not */
9393        static const char *explainCols[] = {
9394           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
9395        int jj;
9396        for(jj=0; jj<ArraySize(explainCols); jj++){
9397          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
9398            p->cMode = p->mode;
9399            sqlite3_reset(pSql);
9400            return;
9401          }
9402        }
9403      }
9404      nAlloc += 100;
9405      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
9406      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
9407    }
9408    abYield[iOp] = str_in_array(zOp, azYield);
9409    p->aiIndent[iOp] = 0;
9410    p->nIndent = iOp+1;
9411
9412    if( str_in_array(zOp, azNext) ){
9413      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
9414    }
9415    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
9416     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
9417    ){
9418      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
9419    }
9420  }
9421
9422  p->iIndent = 0;
9423  sqlite3_free(abYield);
9424  sqlite3_reset(pSql);
9425}
9426
9427/*
9428** Free the array allocated by explain_data_prepare().
9429*/
9430static void explain_data_delete(ShellState *p){
9431  sqlite3_free(p->aiIndent);
9432  p->aiIndent = 0;
9433  p->nIndent = 0;
9434  p->iIndent = 0;
9435}
9436
9437/*
9438** Disable and restore .wheretrace and .selecttrace settings.
9439*/
9440#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9441extern int sqlite3SelectTrace;
9442static int savedSelectTrace;
9443#endif
9444#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9445extern int sqlite3WhereTrace;
9446static int savedWhereTrace;
9447#endif
9448static void disable_debug_trace_modes(void){
9449#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9450  savedSelectTrace = sqlite3SelectTrace;
9451  sqlite3SelectTrace = 0;
9452#endif
9453#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9454  savedWhereTrace = sqlite3WhereTrace;
9455  sqlite3WhereTrace = 0;
9456#endif
9457}
9458static void restore_debug_trace_modes(void){
9459#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9460  sqlite3SelectTrace = savedSelectTrace;
9461#endif
9462#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9463  sqlite3WhereTrace = savedWhereTrace;
9464#endif
9465}
9466
9467/*
9468** Run a prepared statement
9469*/
9470static void exec_prepared_stmt(
9471  ShellState *pArg,                                /* Pointer to ShellState */
9472  sqlite3_stmt *pStmt,                             /* Statment to run */
9473  int (*xCallback)(void*,int,char**,char**,int*)   /* Callback function */
9474){
9475  int rc;
9476
9477  /* perform the first step.  this will tell us if we
9478  ** have a result set or not and how wide it is.
9479  */
9480  rc = sqlite3_step(pStmt);
9481  /* if we have a result set... */
9482  if( SQLITE_ROW == rc ){
9483    /* if we have a callback... */
9484    if( xCallback ){
9485      /* allocate space for col name ptr, value ptr, and type */
9486      int nCol = sqlite3_column_count(pStmt);
9487      void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
9488      if( !pData ){
9489        rc = SQLITE_NOMEM;
9490      }else{
9491        char **azCols = (char **)pData;      /* Names of result columns */
9492        char **azVals = &azCols[nCol];       /* Results */
9493        int *aiTypes = (int *)&azVals[nCol]; /* Result types */
9494        int i, x;
9495        assert(sizeof(int) <= sizeof(char *));
9496        /* save off ptrs to column names */
9497        for(i=0; i<nCol; i++){
9498          azCols[i] = (char *)sqlite3_column_name(pStmt, i);
9499        }
9500        do{
9501          /* extract the data and data types */
9502          for(i=0; i<nCol; i++){
9503            aiTypes[i] = x = sqlite3_column_type(pStmt, i);
9504            if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
9505              azVals[i] = "";
9506            }else{
9507              azVals[i] = (char*)sqlite3_column_text(pStmt, i);
9508            }
9509            if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
9510              rc = SQLITE_NOMEM;
9511              break; /* from for */
9512            }
9513          } /* end for */
9514
9515          /* if data and types extracted successfully... */
9516          if( SQLITE_ROW == rc ){
9517            /* call the supplied callback with the result row data */
9518            if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
9519              rc = SQLITE_ABORT;
9520            }else{
9521              rc = sqlite3_step(pStmt);
9522            }
9523          }
9524        } while( SQLITE_ROW == rc );
9525        sqlite3_free(pData);
9526      }
9527    }else{
9528      do{
9529        rc = sqlite3_step(pStmt);
9530      } while( rc == SQLITE_ROW );
9531    }
9532  }
9533}
9534
9535#ifndef SQLITE_OMIT_VIRTUALTABLE
9536/*
9537** This function is called to process SQL if the previous shell command
9538** was ".expert". It passes the SQL in the second argument directly to
9539** the sqlite3expert object.
9540**
9541** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
9542** code. In this case, (*pzErr) may be set to point to a buffer containing
9543** an English language error message. It is the responsibility of the
9544** caller to eventually free this buffer using sqlite3_free().
9545*/
9546static int expertHandleSQL(
9547  ShellState *pState,
9548  const char *zSql,
9549  char **pzErr
9550){
9551  assert( pState->expert.pExpert );
9552  assert( pzErr==0 || *pzErr==0 );
9553  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
9554}
9555
9556/*
9557** This function is called either to silently clean up the object
9558** created by the ".expert" command (if bCancel==1), or to generate a
9559** report from it and then clean it up (if bCancel==0).
9560**
9561** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
9562** code. In this case, (*pzErr) may be set to point to a buffer containing
9563** an English language error message. It is the responsibility of the
9564** caller to eventually free this buffer using sqlite3_free().
9565*/
9566static int expertFinish(
9567  ShellState *pState,
9568  int bCancel,
9569  char **pzErr
9570){
9571  int rc = SQLITE_OK;
9572  sqlite3expert *p = pState->expert.pExpert;
9573  assert( p );
9574  assert( bCancel || pzErr==0 || *pzErr==0 );
9575  if( bCancel==0 ){
9576    FILE *out = pState->out;
9577    int bVerbose = pState->expert.bVerbose;
9578
9579    rc = sqlite3_expert_analyze(p, pzErr);
9580    if( rc==SQLITE_OK ){
9581      int nQuery = sqlite3_expert_count(p);
9582      int i;
9583
9584      if( bVerbose ){
9585        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
9586        raw_printf(out, "-- Candidates -----------------------------\n");
9587        raw_printf(out, "%s\n", zCand);
9588      }
9589      for(i=0; i<nQuery; i++){
9590        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
9591        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
9592        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
9593        if( zIdx==0 ) zIdx = "(no new indexes)\n";
9594        if( bVerbose ){
9595          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
9596          raw_printf(out, "%s\n\n", zSql);
9597        }
9598        raw_printf(out, "%s\n", zIdx);
9599        raw_printf(out, "%s\n", zEQP);
9600      }
9601    }
9602  }
9603  sqlite3_expert_destroy(p);
9604  pState->expert.pExpert = 0;
9605  return rc;
9606}
9607
9608/*
9609** Implementation of ".expert" dot command.
9610*/
9611static int expertDotCommand(
9612  ShellState *pState,             /* Current shell tool state */
9613  char **azArg,                   /* Array of arguments passed to dot command */
9614  int nArg                        /* Number of entries in azArg[] */
9615){
9616  int rc = SQLITE_OK;
9617  char *zErr = 0;
9618  int i;
9619  int iSample = 0;
9620
9621  assert( pState->expert.pExpert==0 );
9622  memset(&pState->expert, 0, sizeof(ExpertInfo));
9623
9624  for(i=1; rc==SQLITE_OK && i<nArg; i++){
9625    char *z = azArg[i];
9626    int n;
9627    if( z[0]=='-' && z[1]=='-' ) z++;
9628    n = strlen30(z);
9629    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
9630      pState->expert.bVerbose = 1;
9631    }
9632    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
9633      if( i==(nArg-1) ){
9634        raw_printf(stderr, "option requires an argument: %s\n", z);
9635        rc = SQLITE_ERROR;
9636      }else{
9637        iSample = (int)integerValue(azArg[++i]);
9638        if( iSample<0 || iSample>100 ){
9639          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
9640          rc = SQLITE_ERROR;
9641        }
9642      }
9643    }
9644    else{
9645      raw_printf(stderr, "unknown option: %s\n", z);
9646      rc = SQLITE_ERROR;
9647    }
9648  }
9649
9650  if( rc==SQLITE_OK ){
9651    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
9652    if( pState->expert.pExpert==0 ){
9653      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
9654      rc = SQLITE_ERROR;
9655    }else{
9656      sqlite3_expert_config(
9657          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
9658      );
9659    }
9660  }
9661
9662  return rc;
9663}
9664#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
9665
9666/*
9667** Execute a statement or set of statements.  Print
9668** any result rows/columns depending on the current mode
9669** set via the supplied callback.
9670**
9671** This is very similar to SQLite's built-in sqlite3_exec()
9672** function except it takes a slightly different callback
9673** and callback data argument.
9674*/
9675static int shell_exec(
9676  sqlite3 *db,                              /* An open database */
9677  const char *zSql,                         /* SQL to be evaluated */
9678  int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
9679                                            /* (not the same as sqlite3_exec) */
9680  ShellState *pArg,                         /* Pointer to ShellState */
9681  char **pzErrMsg                           /* Error msg written here */
9682){
9683  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
9684  int rc = SQLITE_OK;             /* Return Code */
9685  int rc2;
9686  const char *zLeftover;          /* Tail of unprocessed SQL */
9687
9688  if( pzErrMsg ){
9689    *pzErrMsg = NULL;
9690  }
9691
9692#ifndef SQLITE_OMIT_VIRTUALTABLE
9693  if( pArg->expert.pExpert ){
9694    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
9695    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
9696  }
9697#endif
9698
9699  while( zSql[0] && (SQLITE_OK == rc) ){
9700    static const char *zStmtSql;
9701    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
9702    if( SQLITE_OK != rc ){
9703      if( pzErrMsg ){
9704        *pzErrMsg = save_err_msg(db);
9705      }
9706    }else{
9707      if( !pStmt ){
9708        /* this happens for a comment or white-space */
9709        zSql = zLeftover;
9710        while( IsSpace(zSql[0]) ) zSql++;
9711        continue;
9712      }
9713      zStmtSql = sqlite3_sql(pStmt);
9714      if( zStmtSql==0 ) zStmtSql = "";
9715      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
9716
9717      /* save off the prepared statment handle and reset row count */
9718      if( pArg ){
9719        pArg->pStmt = pStmt;
9720        pArg->cnt = 0;
9721      }
9722
9723      /* echo the sql statement if echo on */
9724      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
9725        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
9726      }
9727
9728      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
9729      if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
9730        sqlite3_stmt *pExplain;
9731        char *zEQP;
9732        int triggerEQP = 0;
9733        disable_debug_trace_modes();
9734        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
9735        if( pArg->autoEQP>=AUTOEQP_trigger ){
9736          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
9737        }
9738        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
9739        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
9740        if( rc==SQLITE_OK ){
9741          while( sqlite3_step(pExplain)==SQLITE_ROW ){
9742            raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
9743            raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
9744            raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
9745            utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
9746          }
9747        }
9748        sqlite3_finalize(pExplain);
9749        sqlite3_free(zEQP);
9750        if( pArg->autoEQP>=AUTOEQP_full ){
9751          /* Also do an EXPLAIN for ".eqp full" mode */
9752          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
9753          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
9754          if( rc==SQLITE_OK ){
9755            pArg->cMode = MODE_Explain;
9756            explain_data_prepare(pArg, pExplain);
9757            exec_prepared_stmt(pArg, pExplain, xCallback);
9758            explain_data_delete(pArg);
9759          }
9760          sqlite3_finalize(pExplain);
9761          sqlite3_free(zEQP);
9762        }
9763        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, triggerEQP, 0);
9764        restore_debug_trace_modes();
9765      }
9766
9767      if( pArg ){
9768        pArg->cMode = pArg->mode;
9769        if( pArg->autoExplain
9770         && sqlite3_column_count(pStmt)==8
9771         && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
9772        ){
9773          pArg->cMode = MODE_Explain;
9774        }
9775
9776        /* If the shell is currently in ".explain" mode, gather the extra
9777        ** data required to add indents to the output.*/
9778        if( pArg->cMode==MODE_Explain ){
9779          explain_data_prepare(pArg, pStmt);
9780        }
9781      }
9782
9783      exec_prepared_stmt(pArg, pStmt, xCallback);
9784      explain_data_delete(pArg);
9785
9786      /* print usage stats if stats on */
9787      if( pArg && pArg->statsOn ){
9788        display_stats(db, pArg, 0);
9789      }
9790
9791      /* print loop-counters if required */
9792      if( pArg && pArg->scanstatsOn ){
9793        display_scanstats(db, pArg);
9794      }
9795
9796      /* Finalize the statement just executed. If this fails, save a
9797      ** copy of the error message. Otherwise, set zSql to point to the
9798      ** next statement to execute. */
9799      rc2 = sqlite3_finalize(pStmt);
9800      if( rc!=SQLITE_NOMEM ) rc = rc2;
9801      if( rc==SQLITE_OK ){
9802        zSql = zLeftover;
9803        while( IsSpace(zSql[0]) ) zSql++;
9804      }else if( pzErrMsg ){
9805        *pzErrMsg = save_err_msg(db);
9806      }
9807
9808      /* clear saved stmt handle */
9809      if( pArg ){
9810        pArg->pStmt = NULL;
9811      }
9812    }
9813  } /* end while */
9814
9815  return rc;
9816}
9817
9818/*
9819** Release memory previously allocated by tableColumnList().
9820*/
9821static void freeColumnList(char **azCol){
9822  int i;
9823  for(i=1; azCol[i]; i++){
9824    sqlite3_free(azCol[i]);
9825  }
9826  /* azCol[0] is a static string */
9827  sqlite3_free(azCol);
9828}
9829
9830/*
9831** Return a list of pointers to strings which are the names of all
9832** columns in table zTab.   The memory to hold the names is dynamically
9833** allocated and must be released by the caller using a subsequent call
9834** to freeColumnList().
9835**
9836** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
9837** value that needs to be preserved, then azCol[0] is filled in with the
9838** name of the rowid column.
9839**
9840** The first regular column in the table is azCol[1].  The list is terminated
9841** by an entry with azCol[i]==0.
9842*/
9843static char **tableColumnList(ShellState *p, const char *zTab){
9844  char **azCol = 0;
9845  sqlite3_stmt *pStmt;
9846  char *zSql;
9847  int nCol = 0;
9848  int nAlloc = 0;
9849  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
9850  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
9851  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
9852  int rc;
9853
9854  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
9855  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9856  sqlite3_free(zSql);
9857  if( rc ) return 0;
9858  while( sqlite3_step(pStmt)==SQLITE_ROW ){
9859    if( nCol>=nAlloc-2 ){
9860      nAlloc = nAlloc*2 + nCol + 10;
9861      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
9862      if( azCol==0 ){
9863        raw_printf(stderr, "Error: out of memory\n");
9864        exit(1);
9865      }
9866    }
9867    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
9868    if( sqlite3_column_int(pStmt, 5) ){
9869      nPK++;
9870      if( nPK==1
9871       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
9872                          "INTEGER")==0
9873      ){
9874        isIPK = 1;
9875      }else{
9876        isIPK = 0;
9877      }
9878    }
9879  }
9880  sqlite3_finalize(pStmt);
9881  if( azCol==0 ) return 0;
9882  azCol[0] = 0;
9883  azCol[nCol+1] = 0;
9884
9885  /* The decision of whether or not a rowid really needs to be preserved
9886  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
9887  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
9888  ** rowids on tables where the rowid is inaccessible because there are other
9889  ** columns in the table named "rowid", "_rowid_", and "oid".
9890  */
9891  if( preserveRowid && isIPK ){
9892    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
9893    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
9894    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
9895    ** ROWID aliases.  To distinguish these cases, check to see if
9896    ** there is a "pk" entry in "PRAGMA index_list".  There will be
9897    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
9898    */
9899    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
9900                           " WHERE origin='pk'", zTab);
9901    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9902    sqlite3_free(zSql);
9903    if( rc ){
9904      freeColumnList(azCol);
9905      return 0;
9906    }
9907    rc = sqlite3_step(pStmt);
9908    sqlite3_finalize(pStmt);
9909    preserveRowid = rc==SQLITE_ROW;
9910  }
9911  if( preserveRowid ){
9912    /* Only preserve the rowid if we can find a name to use for the
9913    ** rowid */
9914    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
9915    int i, j;
9916    for(j=0; j<3; j++){
9917      for(i=1; i<=nCol; i++){
9918        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
9919      }
9920      if( i>nCol ){
9921        /* At this point, we know that azRowid[j] is not the name of any
9922        ** ordinary column in the table.  Verify that azRowid[j] is a valid
9923        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
9924        ** tables will fail this last check */
9925        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
9926        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
9927        break;
9928      }
9929    }
9930  }
9931  return azCol;
9932}
9933
9934/*
9935** Toggle the reverse_unordered_selects setting.
9936*/
9937static void toggleSelectOrder(sqlite3 *db){
9938  sqlite3_stmt *pStmt = 0;
9939  int iSetting = 0;
9940  char zStmt[100];
9941  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
9942  if( sqlite3_step(pStmt)==SQLITE_ROW ){
9943    iSetting = sqlite3_column_int(pStmt, 0);
9944  }
9945  sqlite3_finalize(pStmt);
9946  sqlite3_snprintf(sizeof(zStmt), zStmt,
9947       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
9948  sqlite3_exec(db, zStmt, 0, 0, 0);
9949}
9950
9951/*
9952** This is a different callback routine used for dumping the database.
9953** Each row received by this callback consists of a table name,
9954** the table type ("index" or "table") and SQL to create the table.
9955** This routine should print text sufficient to recreate the table.
9956*/
9957static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
9958  int rc;
9959  const char *zTable;
9960  const char *zType;
9961  const char *zSql;
9962  ShellState *p = (ShellState *)pArg;
9963
9964  UNUSED_PARAMETER(azNotUsed);
9965  if( nArg!=3 || azArg==0 ) return 0;
9966  zTable = azArg[0];
9967  zType = azArg[1];
9968  zSql = azArg[2];
9969
9970  if( strcmp(zTable, "sqlite_sequence")==0 ){
9971    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
9972  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
9973    raw_printf(p->out, "ANALYZE sqlite_master;\n");
9974  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
9975    return 0;
9976  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
9977    char *zIns;
9978    if( !p->writableSchema ){
9979      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
9980      p->writableSchema = 1;
9981    }
9982    zIns = sqlite3_mprintf(
9983       "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
9984       "VALUES('table','%q','%q',0,'%q');",
9985       zTable, zTable, zSql);
9986    utf8_printf(p->out, "%s\n", zIns);
9987    sqlite3_free(zIns);
9988    return 0;
9989  }else{
9990    printSchemaLine(p->out, zSql, ";\n");
9991  }
9992
9993  if( strcmp(zType, "table")==0 ){
9994    ShellText sSelect;
9995    ShellText sTable;
9996    char **azCol;
9997    int i;
9998    char *savedDestTable;
9999    int savedMode;
10000
10001    azCol = tableColumnList(p, zTable);
10002    if( azCol==0 ){
10003      p->nErr++;
10004      return 0;
10005    }
10006
10007    /* Always quote the table name, even if it appears to be pure ascii,
10008    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
10009    initText(&sTable);
10010    appendText(&sTable, zTable, quoteChar(zTable));
10011    /* If preserving the rowid, add a column list after the table name.
10012    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
10013    ** instead of the usual "INSERT INTO tab VALUES(...)".
10014    */
10015    if( azCol[0] ){
10016      appendText(&sTable, "(", 0);
10017      appendText(&sTable, azCol[0], 0);
10018      for(i=1; azCol[i]; i++){
10019        appendText(&sTable, ",", 0);
10020        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
10021      }
10022      appendText(&sTable, ")", 0);
10023    }
10024
10025    /* Build an appropriate SELECT statement */
10026    initText(&sSelect);
10027    appendText(&sSelect, "SELECT ", 0);
10028    if( azCol[0] ){
10029      appendText(&sSelect, azCol[0], 0);
10030      appendText(&sSelect, ",", 0);
10031    }
10032    for(i=1; azCol[i]; i++){
10033      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
10034      if( azCol[i+1] ){
10035        appendText(&sSelect, ",", 0);
10036      }
10037    }
10038    freeColumnList(azCol);
10039    appendText(&sSelect, " FROM ", 0);
10040    appendText(&sSelect, zTable, quoteChar(zTable));
10041
10042    savedDestTable = p->zDestTable;
10043    savedMode = p->mode;
10044    p->zDestTable = sTable.z;
10045    p->mode = p->cMode = MODE_Insert;
10046    rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10047    if( (rc&0xff)==SQLITE_CORRUPT ){
10048      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10049      toggleSelectOrder(p->db);
10050      shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10051      toggleSelectOrder(p->db);
10052    }
10053    p->zDestTable = savedDestTable;
10054    p->mode = savedMode;
10055    freeText(&sTable);
10056    freeText(&sSelect);
10057    if( rc ) p->nErr++;
10058  }
10059  return 0;
10060}
10061
10062/*
10063** Run zQuery.  Use dump_callback() as the callback routine so that
10064** the contents of the query are output as SQL statements.
10065**
10066** If we get a SQLITE_CORRUPT error, rerun the query after appending
10067** "ORDER BY rowid DESC" to the end.
10068*/
10069static int run_schema_dump_query(
10070  ShellState *p,
10071  const char *zQuery
10072){
10073  int rc;
10074  char *zErr = 0;
10075  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
10076  if( rc==SQLITE_CORRUPT ){
10077    char *zQ2;
10078    int len = strlen30(zQuery);
10079    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10080    if( zErr ){
10081      utf8_printf(p->out, "/****** %s ******/\n", zErr);
10082      sqlite3_free(zErr);
10083      zErr = 0;
10084    }
10085    zQ2 = malloc( len+100 );
10086    if( zQ2==0 ) return rc;
10087    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
10088    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
10089    if( rc ){
10090      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
10091    }else{
10092      rc = SQLITE_CORRUPT;
10093    }
10094    sqlite3_free(zErr);
10095    free(zQ2);
10096  }
10097  return rc;
10098}
10099
10100/*
10101** Text of a help message
10102*/
10103static char zHelp[] =
10104#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10105  ".archive ...           Manage SQL archives: \".archive --help\" for details\n"
10106#endif
10107#ifndef SQLITE_OMIT_AUTHORIZATION
10108  ".auth ON|OFF           Show authorizer callbacks\n"
10109#endif
10110  ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
10111  ".bail on|off           Stop after hitting an error.  Default OFF\n"
10112  ".binary on|off         Turn binary output on or off.  Default OFF\n"
10113  ".cd DIRECTORY          Change the working directory to DIRECTORY\n"
10114  ".changes on|off        Show number of rows changed by SQL\n"
10115  ".check GLOB            Fail if output since .testcase does not match\n"
10116  ".clone NEWDB           Clone data into NEWDB from the existing database\n"
10117  ".databases             List names and files of attached databases\n"
10118  ".dbinfo ?DB?           Show status information about the database\n"
10119  ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
10120  "                         If TABLE specified, only dump tables matching\n"
10121  "                         LIKE pattern TABLE.\n"
10122  ".echo on|off           Turn command echo on or off\n"
10123  ".eqp on|off|full       Enable or disable automatic EXPLAIN QUERY PLAN\n"
10124  ".excel                 Display the output of next command in a spreadsheet\n"
10125  ".exit                  Exit this program\n"
10126  ".expert                EXPERIMENTAL. Suggest indexes for specified queries\n"
10127/* Because explain mode comes on automatically now, the ".explain" mode
10128** is removed from the help screen.  It is still supported for legacy, however */
10129/*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
10130  ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
10131  ".headers on|off        Turn display of headers on or off\n"
10132  ".help                  Show this message\n"
10133  ".import FILE TABLE     Import data from FILE into TABLE\n"
10134#ifndef SQLITE_OMIT_TEST_CONTROL
10135  ".imposter INDEX TABLE  Create imposter table TABLE on index INDEX\n"
10136#endif
10137  ".indexes ?TABLE?       Show names of all indexes\n"
10138  "                         If TABLE specified, only show indexes for tables\n"
10139  "                         matching LIKE pattern TABLE.\n"
10140#ifdef SQLITE_ENABLE_IOTRACE
10141  ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
10142#endif
10143  ".limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT\n"
10144  ".lint OPTIONS          Report potential schema issues. Options:\n"
10145  "                         fkey-indexes     Find missing foreign key indexes\n"
10146#ifndef SQLITE_OMIT_LOAD_EXTENSION
10147  ".load FILE ?ENTRY?     Load an extension library\n"
10148#endif
10149  ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
10150  ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
10151  "                         ascii    Columns/rows delimited by 0x1F and 0x1E\n"
10152  "                         csv      Comma-separated values\n"
10153  "                         column   Left-aligned columns.  (See .width)\n"
10154  "                         html     HTML <table> code\n"
10155  "                         insert   SQL insert statements for TABLE\n"
10156  "                         line     One value per line\n"
10157  "                         list     Values delimited by \"|\"\n"
10158  "                         quote    Escape answers as for SQL\n"
10159  "                         tabs     Tab-separated values\n"
10160  "                         tcl      TCL list elements\n"
10161  ".nullvalue STRING      Use STRING in place of NULL values\n"
10162  ".once (-e|-x|FILE)     Output for the next SQL command only to FILE\n"
10163  "                         or invoke system text editor (-e) or spreadsheet (-x)\n"
10164  "                         on the output.\n"
10165  ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
10166  "                         The --new option starts with an empty file\n"
10167  ".output ?FILE?         Send output to FILE or stdout\n"
10168  ".print STRING...       Print literal STRING\n"
10169  ".prompt MAIN CONTINUE  Replace the standard prompts\n"
10170  ".quit                  Exit this program\n"
10171  ".read FILENAME         Execute SQL in FILENAME\n"
10172  ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
10173  ".save FILE             Write in-memory database into FILE\n"
10174  ".scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off\n"
10175  ".schema ?PATTERN?      Show the CREATE statements matching PATTERN\n"
10176  "                          Add --indent for pretty-printing\n"
10177  ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
10178  ".separator COL ?ROW?   Change the column separator and optionally the row\n"
10179  "                         separator for both the output mode and .import\n"
10180#if defined(SQLITE_ENABLE_SESSION)
10181  ".session CMD ...       Create or control sessions\n"
10182#endif
10183  ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
10184  ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
10185  ".show                  Show the current values for various settings\n"
10186  ".stats ?on|off?        Show stats or turn stats on or off\n"
10187  ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
10188  ".tables ?TABLE?        List names of tables\n"
10189  "                         If TABLE specified, only list tables matching\n"
10190  "                         LIKE pattern TABLE.\n"
10191  ".testcase NAME         Begin redirecting output to 'testcase-out.txt'\n"
10192  ".timeout MS            Try opening locked tables for MS milliseconds\n"
10193  ".timer on|off          Turn SQL timer on or off\n"
10194  ".trace FILE|off        Output each SQL statement as it is run\n"
10195  ".vfsinfo ?AUX?         Information about the top-level VFS\n"
10196  ".vfslist               List all available VFSes\n"
10197  ".vfsname ?AUX?         Print the name of the VFS stack\n"
10198  ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
10199  "                         Negative values right-justify\n"
10200;
10201
10202#if defined(SQLITE_ENABLE_SESSION)
10203/*
10204** Print help information for the ".sessions" command
10205*/
10206void session_help(ShellState *p){
10207  raw_printf(p->out,
10208    ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
10209    "If ?NAME? is omitted, the first defined session is used.\n"
10210    "Subcommands:\n"
10211    "   attach TABLE             Attach TABLE\n"
10212    "   changeset FILE           Write a changeset into FILE\n"
10213    "   close                    Close one session\n"
10214    "   enable ?BOOLEAN?         Set or query the enable bit\n"
10215    "   filter GLOB...           Reject tables matching GLOBs\n"
10216    "   indirect ?BOOLEAN?       Mark or query the indirect status\n"
10217    "   isempty                  Query whether the session is empty\n"
10218    "   list                     List currently open session names\n"
10219    "   open DB NAME             Open a new session on DB\n"
10220    "   patchset FILE            Write a patchset into FILE\n"
10221  );
10222}
10223#endif
10224
10225
10226/* Forward reference */
10227static int process_input(ShellState *p, FILE *in);
10228
10229/*
10230** Read the content of file zName into memory obtained from sqlite3_malloc64()
10231** and return a pointer to the buffer. The caller is responsible for freeing
10232** the memory.
10233**
10234** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
10235** read.
10236**
10237** For convenience, a nul-terminator byte is always appended to the data read
10238** from the file before the buffer is returned. This byte is not included in
10239** the final value of (*pnByte), if applicable.
10240**
10241** NULL is returned if any error is encountered. The final value of *pnByte
10242** is undefined in this case.
10243*/
10244static char *readFile(const char *zName, int *pnByte){
10245  FILE *in = fopen(zName, "rb");
10246  long nIn;
10247  size_t nRead;
10248  char *pBuf;
10249  if( in==0 ) return 0;
10250  fseek(in, 0, SEEK_END);
10251  nIn = ftell(in);
10252  rewind(in);
10253  pBuf = sqlite3_malloc64( nIn+1 );
10254  if( pBuf==0 ) return 0;
10255  nRead = fread(pBuf, nIn, 1, in);
10256  fclose(in);
10257  if( nRead!=1 ){
10258    sqlite3_free(pBuf);
10259    return 0;
10260  }
10261  pBuf[nIn] = 0;
10262  if( pnByte ) *pnByte = nIn;
10263  return pBuf;
10264}
10265
10266#if defined(SQLITE_ENABLE_SESSION)
10267/*
10268** Close a single OpenSession object and release all of its associated
10269** resources.
10270*/
10271static void session_close(OpenSession *pSession){
10272  int i;
10273  sqlite3session_delete(pSession->p);
10274  sqlite3_free(pSession->zName);
10275  for(i=0; i<pSession->nFilter; i++){
10276    sqlite3_free(pSession->azFilter[i]);
10277  }
10278  sqlite3_free(pSession->azFilter);
10279  memset(pSession, 0, sizeof(OpenSession));
10280}
10281#endif
10282
10283/*
10284** Close all OpenSession objects and release all associated resources.
10285*/
10286#if defined(SQLITE_ENABLE_SESSION)
10287static void session_close_all(ShellState *p){
10288  int i;
10289  for(i=0; i<p->nSession; i++){
10290    session_close(&p->aSession[i]);
10291  }
10292  p->nSession = 0;
10293}
10294#else
10295# define session_close_all(X)
10296#endif
10297
10298/*
10299** Implementation of the xFilter function for an open session.  Omit
10300** any tables named by ".session filter" but let all other table through.
10301*/
10302#if defined(SQLITE_ENABLE_SESSION)
10303static int session_filter(void *pCtx, const char *zTab){
10304  OpenSession *pSession = (OpenSession*)pCtx;
10305  int i;
10306  for(i=0; i<pSession->nFilter; i++){
10307    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
10308  }
10309  return 1;
10310}
10311#endif
10312
10313/*
10314** Try to deduce the type of file for zName based on its content.  Return
10315** one of the SHELL_OPEN_* constants.
10316*/
10317static int deduceDatabaseType(const char *zName){
10318  FILE *f = fopen(zName, "rb");
10319  size_t n;
10320  int rc = SHELL_OPEN_UNSPEC;
10321  char zBuf[100];
10322  if( f==0 ) return SHELL_OPEN_NORMAL;
10323  fseek(f, -25, SEEK_END);
10324  n = fread(zBuf, 25, 1, f);
10325  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
10326    rc = SHELL_OPEN_APPENDVFS;
10327  }else{
10328    fseek(f, -22, SEEK_END);
10329    n = fread(zBuf, 22, 1, f);
10330    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
10331       && zBuf[3]==0x06 ){
10332      rc = SHELL_OPEN_ZIPFILE;
10333    }
10334  }
10335  fclose(f);
10336  return rc;
10337}
10338
10339/*
10340** Make sure the database is open.  If it is not, then open it.  If
10341** the database fails to open, print an error message and exit.
10342*/
10343static void open_db(ShellState *p, int keepAlive){
10344  if( p->db==0 ){
10345    sqlite3_initialize();
10346    if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
10347      p->openMode = deduceDatabaseType(p->zDbFilename);
10348    }
10349    switch( p->openMode ){
10350      case SHELL_OPEN_APPENDVFS: {
10351        sqlite3_open_v2(p->zDbFilename, &p->db,
10352           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
10353        break;
10354      }
10355      case SHELL_OPEN_ZIPFILE: {
10356        sqlite3_open(":memory:", &p->db);
10357        break;
10358      }
10359      case SHELL_OPEN_UNSPEC:
10360      case SHELL_OPEN_NORMAL: {
10361        sqlite3_open(p->zDbFilename, &p->db);
10362        break;
10363      }
10364    }
10365    globalDb = p->db;
10366    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
10367      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
10368          p->zDbFilename, sqlite3_errmsg(p->db));
10369      if( keepAlive ) return;
10370      exit(1);
10371    }
10372#ifndef SQLITE_OMIT_LOAD_EXTENSION
10373    sqlite3_enable_load_extension(p->db, 1);
10374#endif
10375    sqlite3_fileio_init(p->db, 0, 0);
10376    sqlite3_shathree_init(p->db, 0, 0);
10377    sqlite3_completion_init(p->db, 0, 0);
10378#ifdef SQLITE_HAVE_ZLIB
10379    sqlite3_zipfile_init(p->db, 0, 0);
10380    sqlite3_sqlar_init(p->db, 0, 0);
10381#endif
10382    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
10383                            shellAddSchemaName, 0, 0);
10384    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
10385                            shellModuleSchema, 0, 0);
10386    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
10387                            shellPutsFunc, 0, 0);
10388    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
10389                            editFunc, 0, 0);
10390    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
10391                            editFunc, 0, 0);
10392    if( p->openMode==SHELL_OPEN_ZIPFILE ){
10393      char *zSql = sqlite3_mprintf(
10394         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
10395      sqlite3_exec(p->db, zSql, 0, 0, 0);
10396      sqlite3_free(zSql);
10397    }
10398  }
10399}
10400
10401#if HAVE_READLINE || HAVE_EDITLINE
10402/*
10403** Readline completion callbacks
10404*/
10405static char *readline_completion_generator(const char *text, int state){
10406  static sqlite3_stmt *pStmt = 0;
10407  char *zRet;
10408  if( state==0 ){
10409    char *zSql;
10410    sqlite3_finalize(pStmt);
10411    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
10412                           "  FROM completion(%Q) ORDER BY 1", text);
10413    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
10414    sqlite3_free(zSql);
10415  }
10416  if( sqlite3_step(pStmt)==SQLITE_ROW ){
10417    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
10418  }else{
10419    sqlite3_finalize(pStmt);
10420    pStmt = 0;
10421    zRet = 0;
10422  }
10423  return zRet;
10424}
10425static char **readline_completion(const char *zText, int iStart, int iEnd){
10426  rl_attempted_completion_over = 1;
10427  return rl_completion_matches(zText, readline_completion_generator);
10428}
10429
10430#elif HAVE_LINENOISE
10431/*
10432** Linenoise completion callback
10433*/
10434static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
10435  int nLine = strlen30(zLine);
10436  int i, iStart;
10437  sqlite3_stmt *pStmt = 0;
10438  char *zSql;
10439  char zBuf[1000];
10440
10441  if( nLine>sizeof(zBuf)-30 ) return;
10442  if( zLine[0]=='.' ) return;
10443  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
10444  if( i==nLine-1 ) return;
10445  iStart = i+1;
10446  memcpy(zBuf, zLine, iStart);
10447  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
10448                         "  FROM completion(%Q,%Q) ORDER BY 1",
10449                         &zLine[iStart], zLine);
10450  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
10451  sqlite3_free(zSql);
10452  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
10453  while( sqlite3_step(pStmt)==SQLITE_ROW ){
10454    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
10455    int nCompletion = sqlite3_column_bytes(pStmt, 0);
10456    if( iStart+nCompletion < sizeof(zBuf)-1 ){
10457      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
10458      linenoiseAddCompletion(lc, zBuf);
10459    }
10460  }
10461  sqlite3_finalize(pStmt);
10462}
10463#endif
10464
10465/*
10466** Do C-language style dequoting.
10467**
10468**    \a    -> alarm
10469**    \b    -> backspace
10470**    \t    -> tab
10471**    \n    -> newline
10472**    \v    -> vertical tab
10473**    \f    -> form feed
10474**    \r    -> carriage return
10475**    \s    -> space
10476**    \"    -> "
10477**    \'    -> '
10478**    \\    -> backslash
10479**    \NNN  -> ascii character NNN in octal
10480*/
10481static void resolve_backslashes(char *z){
10482  int i, j;
10483  char c;
10484  while( *z && *z!='\\' ) z++;
10485  for(i=j=0; (c = z[i])!=0; i++, j++){
10486    if( c=='\\' && z[i+1]!=0 ){
10487      c = z[++i];
10488      if( c=='a' ){
10489        c = '\a';
10490      }else if( c=='b' ){
10491        c = '\b';
10492      }else if( c=='t' ){
10493        c = '\t';
10494      }else if( c=='n' ){
10495        c = '\n';
10496      }else if( c=='v' ){
10497        c = '\v';
10498      }else if( c=='f' ){
10499        c = '\f';
10500      }else if( c=='r' ){
10501        c = '\r';
10502      }else if( c=='"' ){
10503        c = '"';
10504      }else if( c=='\'' ){
10505        c = '\'';
10506      }else if( c=='\\' ){
10507        c = '\\';
10508      }else if( c>='0' && c<='7' ){
10509        c -= '0';
10510        if( z[i+1]>='0' && z[i+1]<='7' ){
10511          i++;
10512          c = (c<<3) + z[i] - '0';
10513          if( z[i+1]>='0' && z[i+1]<='7' ){
10514            i++;
10515            c = (c<<3) + z[i] - '0';
10516          }
10517        }
10518      }
10519    }
10520    z[j] = c;
10521  }
10522  if( j<i ) z[j] = 0;
10523}
10524
10525/*
10526** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
10527** for TRUE and FALSE.  Return the integer value if appropriate.
10528*/
10529static int booleanValue(const char *zArg){
10530  int i;
10531  if( zArg[0]=='0' && zArg[1]=='x' ){
10532    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
10533  }else{
10534    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
10535  }
10536  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
10537  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
10538    return 1;
10539  }
10540  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
10541    return 0;
10542  }
10543  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
10544          zArg);
10545  return 0;
10546}
10547
10548/*
10549** Set or clear a shell flag according to a boolean value.
10550*/
10551static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
10552  if( booleanValue(zArg) ){
10553    ShellSetFlag(p, mFlag);
10554  }else{
10555    ShellClearFlag(p, mFlag);
10556  }
10557}
10558
10559/*
10560** Close an output file, assuming it is not stderr or stdout
10561*/
10562static void output_file_close(FILE *f){
10563  if( f && f!=stdout && f!=stderr ) fclose(f);
10564}
10565
10566/*
10567** Try to open an output file.   The names "stdout" and "stderr" are
10568** recognized and do the right thing.  NULL is returned if the output
10569** filename is "off".
10570*/
10571static FILE *output_file_open(const char *zFile, int bTextMode){
10572  FILE *f;
10573  if( strcmp(zFile,"stdout")==0 ){
10574    f = stdout;
10575  }else if( strcmp(zFile, "stderr")==0 ){
10576    f = stderr;
10577  }else if( strcmp(zFile, "off")==0 ){
10578    f = 0;
10579  }else{
10580    f = fopen(zFile, bTextMode ? "w" : "wb");
10581    if( f==0 ){
10582      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
10583    }
10584  }
10585  return f;
10586}
10587
10588#if !defined(SQLITE_UNTESTABLE)
10589#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
10590/*
10591** A routine for handling output from sqlite3_trace().
10592*/
10593static int sql_trace_callback(
10594  unsigned mType,
10595  void *pArg,
10596  void *pP,
10597  void *pX
10598){
10599  FILE *f = (FILE*)pArg;
10600  UNUSED_PARAMETER(mType);
10601  UNUSED_PARAMETER(pP);
10602  if( f ){
10603    const char *z = (const char*)pX;
10604    int i = strlen30(z);
10605    while( i>0 && z[i-1]==';' ){ i--; }
10606    utf8_printf(f, "%.*s;\n", i, z);
10607  }
10608  return 0;
10609}
10610#endif
10611#endif
10612
10613/*
10614** A no-op routine that runs with the ".breakpoint" doc-command.  This is
10615** a useful spot to set a debugger breakpoint.
10616*/
10617static void test_breakpoint(void){
10618  static int nCall = 0;
10619  nCall++;
10620}
10621
10622/*
10623** An object used to read a CSV and other files for import.
10624*/
10625typedef struct ImportCtx ImportCtx;
10626struct ImportCtx {
10627  const char *zFile;  /* Name of the input file */
10628  FILE *in;           /* Read the CSV text from this input stream */
10629  char *z;            /* Accumulated text for a field */
10630  int n;              /* Number of bytes in z */
10631  int nAlloc;         /* Space allocated for z[] */
10632  int nLine;          /* Current line number */
10633  int bNotFirst;      /* True if one or more bytes already read */
10634  int cTerm;          /* Character that terminated the most recent field */
10635  int cColSep;        /* The column separator character.  (Usually ",") */
10636  int cRowSep;        /* The row separator character.  (Usually "\n") */
10637};
10638
10639/* Append a single byte to z[] */
10640static void import_append_char(ImportCtx *p, int c){
10641  if( p->n+1>=p->nAlloc ){
10642    p->nAlloc += p->nAlloc + 100;
10643    p->z = sqlite3_realloc64(p->z, p->nAlloc);
10644    if( p->z==0 ){
10645      raw_printf(stderr, "out of memory\n");
10646      exit(1);
10647    }
10648  }
10649  p->z[p->n++] = (char)c;
10650}
10651
10652/* Read a single field of CSV text.  Compatible with rfc4180 and extended
10653** with the option of having a separator other than ",".
10654**
10655**   +  Input comes from p->in.
10656**   +  Store results in p->z of length p->n.  Space to hold p->z comes
10657**      from sqlite3_malloc64().
10658**   +  Use p->cSep as the column separator.  The default is ",".
10659**   +  Use p->rSep as the row separator.  The default is "\n".
10660**   +  Keep track of the line number in p->nLine.
10661**   +  Store the character that terminates the field in p->cTerm.  Store
10662**      EOF on end-of-file.
10663**   +  Report syntax errors on stderr
10664*/
10665static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
10666  int c;
10667  int cSep = p->cColSep;
10668  int rSep = p->cRowSep;
10669  p->n = 0;
10670  c = fgetc(p->in);
10671  if( c==EOF || seenInterrupt ){
10672    p->cTerm = EOF;
10673    return 0;
10674  }
10675  if( c=='"' ){
10676    int pc, ppc;
10677    int startLine = p->nLine;
10678    int cQuote = c;
10679    pc = ppc = 0;
10680    while( 1 ){
10681      c = fgetc(p->in);
10682      if( c==rSep ) p->nLine++;
10683      if( c==cQuote ){
10684        if( pc==cQuote ){
10685          pc = 0;
10686          continue;
10687        }
10688      }
10689      if( (c==cSep && pc==cQuote)
10690       || (c==rSep && pc==cQuote)
10691       || (c==rSep && pc=='\r' && ppc==cQuote)
10692       || (c==EOF && pc==cQuote)
10693      ){
10694        do{ p->n--; }while( p->z[p->n]!=cQuote );
10695        p->cTerm = c;
10696        break;
10697      }
10698      if( pc==cQuote && c!='\r' ){
10699        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
10700                p->zFile, p->nLine, cQuote);
10701      }
10702      if( c==EOF ){
10703        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
10704                p->zFile, startLine, cQuote);
10705        p->cTerm = c;
10706        break;
10707      }
10708      import_append_char(p, c);
10709      ppc = pc;
10710      pc = c;
10711    }
10712  }else{
10713    /* If this is the first field being parsed and it begins with the
10714    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
10715    if( (c&0xff)==0xef && p->bNotFirst==0 ){
10716      import_append_char(p, c);
10717      c = fgetc(p->in);
10718      if( (c&0xff)==0xbb ){
10719        import_append_char(p, c);
10720        c = fgetc(p->in);
10721        if( (c&0xff)==0xbf ){
10722          p->bNotFirst = 1;
10723          p->n = 0;
10724          return csv_read_one_field(p);
10725        }
10726      }
10727    }
10728    while( c!=EOF && c!=cSep && c!=rSep ){
10729      import_append_char(p, c);
10730      c = fgetc(p->in);
10731    }
10732    if( c==rSep ){
10733      p->nLine++;
10734      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
10735    }
10736    p->cTerm = c;
10737  }
10738  if( p->z ) p->z[p->n] = 0;
10739  p->bNotFirst = 1;
10740  return p->z;
10741}
10742
10743/* Read a single field of ASCII delimited text.
10744**
10745**   +  Input comes from p->in.
10746**   +  Store results in p->z of length p->n.  Space to hold p->z comes
10747**      from sqlite3_malloc64().
10748**   +  Use p->cSep as the column separator.  The default is "\x1F".
10749**   +  Use p->rSep as the row separator.  The default is "\x1E".
10750**   +  Keep track of the row number in p->nLine.
10751**   +  Store the character that terminates the field in p->cTerm.  Store
10752**      EOF on end-of-file.
10753**   +  Report syntax errors on stderr
10754*/
10755static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
10756  int c;
10757  int cSep = p->cColSep;
10758  int rSep = p->cRowSep;
10759  p->n = 0;
10760  c = fgetc(p->in);
10761  if( c==EOF || seenInterrupt ){
10762    p->cTerm = EOF;
10763    return 0;
10764  }
10765  while( c!=EOF && c!=cSep && c!=rSep ){
10766    import_append_char(p, c);
10767    c = fgetc(p->in);
10768  }
10769  if( c==rSep ){
10770    p->nLine++;
10771  }
10772  p->cTerm = c;
10773  if( p->z ) p->z[p->n] = 0;
10774  return p->z;
10775}
10776
10777/*
10778** Try to transfer data for table zTable.  If an error is seen while
10779** moving forward, try to go backwards.  The backwards movement won't
10780** work for WITHOUT ROWID tables.
10781*/
10782static void tryToCloneData(
10783  ShellState *p,
10784  sqlite3 *newDb,
10785  const char *zTable
10786){
10787  sqlite3_stmt *pQuery = 0;
10788  sqlite3_stmt *pInsert = 0;
10789  char *zQuery = 0;
10790  char *zInsert = 0;
10791  int rc;
10792  int i, j, n;
10793  int nTable = strlen30(zTable);
10794  int k = 0;
10795  int cnt = 0;
10796  const int spinRate = 10000;
10797
10798  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
10799  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10800  if( rc ){
10801    utf8_printf(stderr, "Error %d: %s on [%s]\n",
10802            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
10803            zQuery);
10804    goto end_data_xfer;
10805  }
10806  n = sqlite3_column_count(pQuery);
10807  zInsert = sqlite3_malloc64(200 + nTable + n*3);
10808  if( zInsert==0 ){
10809    raw_printf(stderr, "out of memory\n");
10810    goto end_data_xfer;
10811  }
10812  sqlite3_snprintf(200+nTable,zInsert,
10813                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
10814  i = strlen30(zInsert);
10815  for(j=1; j<n; j++){
10816    memcpy(zInsert+i, ",?", 2);
10817    i += 2;
10818  }
10819  memcpy(zInsert+i, ");", 3);
10820  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
10821  if( rc ){
10822    utf8_printf(stderr, "Error %d: %s on [%s]\n",
10823            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
10824            zQuery);
10825    goto end_data_xfer;
10826  }
10827  for(k=0; k<2; k++){
10828    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
10829      for(i=0; i<n; i++){
10830        switch( sqlite3_column_type(pQuery, i) ){
10831          case SQLITE_NULL: {
10832            sqlite3_bind_null(pInsert, i+1);
10833            break;
10834          }
10835          case SQLITE_INTEGER: {
10836            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
10837            break;
10838          }
10839          case SQLITE_FLOAT: {
10840            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
10841            break;
10842          }
10843          case SQLITE_TEXT: {
10844            sqlite3_bind_text(pInsert, i+1,
10845                             (const char*)sqlite3_column_text(pQuery,i),
10846                             -1, SQLITE_STATIC);
10847            break;
10848          }
10849          case SQLITE_BLOB: {
10850            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
10851                                            sqlite3_column_bytes(pQuery,i),
10852                                            SQLITE_STATIC);
10853            break;
10854          }
10855        }
10856      } /* End for */
10857      rc = sqlite3_step(pInsert);
10858      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
10859        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
10860                        sqlite3_errmsg(newDb));
10861      }
10862      sqlite3_reset(pInsert);
10863      cnt++;
10864      if( (cnt%spinRate)==0 ){
10865        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
10866        fflush(stdout);
10867      }
10868    } /* End while */
10869    if( rc==SQLITE_DONE ) break;
10870    sqlite3_finalize(pQuery);
10871    sqlite3_free(zQuery);
10872    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
10873                             zTable);
10874    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10875    if( rc ){
10876      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
10877      break;
10878    }
10879  } /* End for(k=0...) */
10880
10881end_data_xfer:
10882  sqlite3_finalize(pQuery);
10883  sqlite3_finalize(pInsert);
10884  sqlite3_free(zQuery);
10885  sqlite3_free(zInsert);
10886}
10887
10888
10889/*
10890** Try to transfer all rows of the schema that match zWhere.  For
10891** each row, invoke xForEach() on the object defined by that row.
10892** If an error is encountered while moving forward through the
10893** sqlite_master table, try again moving backwards.
10894*/
10895static void tryToCloneSchema(
10896  ShellState *p,
10897  sqlite3 *newDb,
10898  const char *zWhere,
10899  void (*xForEach)(ShellState*,sqlite3*,const char*)
10900){
10901  sqlite3_stmt *pQuery = 0;
10902  char *zQuery = 0;
10903  int rc;
10904  const unsigned char *zName;
10905  const unsigned char *zSql;
10906  char *zErrMsg = 0;
10907
10908  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
10909                           " WHERE %s", zWhere);
10910  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10911  if( rc ){
10912    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
10913                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
10914                    zQuery);
10915    goto end_schema_xfer;
10916  }
10917  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
10918    zName = sqlite3_column_text(pQuery, 0);
10919    zSql = sqlite3_column_text(pQuery, 1);
10920    printf("%s... ", zName); fflush(stdout);
10921    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
10922    if( zErrMsg ){
10923      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
10924      sqlite3_free(zErrMsg);
10925      zErrMsg = 0;
10926    }
10927    if( xForEach ){
10928      xForEach(p, newDb, (const char*)zName);
10929    }
10930    printf("done\n");
10931  }
10932  if( rc!=SQLITE_DONE ){
10933    sqlite3_finalize(pQuery);
10934    sqlite3_free(zQuery);
10935    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
10936                             " WHERE %s ORDER BY rowid DESC", zWhere);
10937    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10938    if( rc ){
10939      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
10940                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
10941                      zQuery);
10942      goto end_schema_xfer;
10943    }
10944    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
10945      zName = sqlite3_column_text(pQuery, 0);
10946      zSql = sqlite3_column_text(pQuery, 1);
10947      printf("%s... ", zName); fflush(stdout);
10948      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
10949      if( zErrMsg ){
10950        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
10951        sqlite3_free(zErrMsg);
10952        zErrMsg = 0;
10953      }
10954      if( xForEach ){
10955        xForEach(p, newDb, (const char*)zName);
10956      }
10957      printf("done\n");
10958    }
10959  }
10960end_schema_xfer:
10961  sqlite3_finalize(pQuery);
10962  sqlite3_free(zQuery);
10963}
10964
10965/*
10966** Open a new database file named "zNewDb".  Try to recover as much information
10967** as possible out of the main database (which might be corrupt) and write it
10968** into zNewDb.
10969*/
10970static void tryToClone(ShellState *p, const char *zNewDb){
10971  int rc;
10972  sqlite3 *newDb = 0;
10973  if( access(zNewDb,0)==0 ){
10974    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
10975    return;
10976  }
10977  rc = sqlite3_open(zNewDb, &newDb);
10978  if( rc ){
10979    utf8_printf(stderr, "Cannot create output database: %s\n",
10980            sqlite3_errmsg(newDb));
10981  }else{
10982    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
10983    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
10984    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
10985    tryToCloneSchema(p, newDb, "type!='table'", 0);
10986    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
10987    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
10988  }
10989  sqlite3_close(newDb);
10990}
10991
10992/*
10993** Change the output file back to stdout.
10994**
10995** If the p->doXdgOpen flag is set, that means the output was being
10996** redirected to a temporary file named by p->zTempFile.  In that case,
10997** launch start/open/xdg-open on that temporary file.
10998*/
10999static void output_reset(ShellState *p){
11000  if( p->outfile[0]=='|' ){
11001#ifndef SQLITE_OMIT_POPEN
11002    pclose(p->out);
11003#endif
11004  }else{
11005    output_file_close(p->out);
11006    if( p->doXdgOpen ){
11007      const char *zXdgOpenCmd =
11008#if defined(_WIN32)
11009      "start";
11010#elif defined(__APPLE__)
11011      "open";
11012#else
11013      "xdg-open";
11014#endif
11015      char *zCmd;
11016      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
11017      if( system(zCmd) ){
11018        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
11019      }
11020      sqlite3_free(zCmd);
11021      outputModePop(p);
11022      p->doXdgOpen = 0;
11023    }
11024  }
11025  p->outfile[0] = 0;
11026  p->out = stdout;
11027}
11028
11029/*
11030** Run an SQL command and return the single integer result.
11031*/
11032static int db_int(ShellState *p, const char *zSql){
11033  sqlite3_stmt *pStmt;
11034  int res = 0;
11035  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11036  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
11037    res = sqlite3_column_int(pStmt,0);
11038  }
11039  sqlite3_finalize(pStmt);
11040  return res;
11041}
11042
11043/*
11044** Convert a 2-byte or 4-byte big-endian integer into a native integer
11045*/
11046static unsigned int get2byteInt(unsigned char *a){
11047  return (a[0]<<8) + a[1];
11048}
11049static unsigned int get4byteInt(unsigned char *a){
11050  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
11051}
11052
11053/*
11054** Implementation of the ".info" command.
11055**
11056** Return 1 on error, 2 to exit, and 0 otherwise.
11057*/
11058static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
11059  static const struct { const char *zName; int ofst; } aField[] = {
11060     { "file change counter:",  24  },
11061     { "database page count:",  28  },
11062     { "freelist page count:",  36  },
11063     { "schema cookie:",        40  },
11064     { "schema format:",        44  },
11065     { "default cache size:",   48  },
11066     { "autovacuum top root:",  52  },
11067     { "incremental vacuum:",   64  },
11068     { "text encoding:",        56  },
11069     { "user version:",         60  },
11070     { "application id:",       68  },
11071     { "software version:",     96  },
11072  };
11073  static const struct { const char *zName; const char *zSql; } aQuery[] = {
11074     { "number of tables:",
11075       "SELECT count(*) FROM %s WHERE type='table'" },
11076     { "number of indexes:",
11077       "SELECT count(*) FROM %s WHERE type='index'" },
11078     { "number of triggers:",
11079       "SELECT count(*) FROM %s WHERE type='trigger'" },
11080     { "number of views:",
11081       "SELECT count(*) FROM %s WHERE type='view'" },
11082     { "schema size:",
11083       "SELECT total(length(sql)) FROM %s" },
11084  };
11085  int i;
11086  char *zSchemaTab;
11087  char *zDb = nArg>=2 ? azArg[1] : "main";
11088  sqlite3_stmt *pStmt = 0;
11089  unsigned char aHdr[100];
11090  open_db(p, 0);
11091  if( p->db==0 ) return 1;
11092  sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
11093                     -1, &pStmt, 0);
11094  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
11095  if( sqlite3_step(pStmt)==SQLITE_ROW
11096   && sqlite3_column_bytes(pStmt,0)>100
11097  ){
11098    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
11099    sqlite3_finalize(pStmt);
11100  }else{
11101    raw_printf(stderr, "unable to read database header\n");
11102    sqlite3_finalize(pStmt);
11103    return 1;
11104  }
11105  i = get2byteInt(aHdr+16);
11106  if( i==1 ) i = 65536;
11107  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
11108  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
11109  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
11110  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
11111  for(i=0; i<ArraySize(aField); i++){
11112    int ofst = aField[i].ofst;
11113    unsigned int val = get4byteInt(aHdr + ofst);
11114    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
11115    switch( ofst ){
11116      case 56: {
11117        if( val==1 ) raw_printf(p->out, " (utf8)");
11118        if( val==2 ) raw_printf(p->out, " (utf16le)");
11119        if( val==3 ) raw_printf(p->out, " (utf16be)");
11120      }
11121    }
11122    raw_printf(p->out, "\n");
11123  }
11124  if( zDb==0 ){
11125    zSchemaTab = sqlite3_mprintf("main.sqlite_master");
11126  }else if( strcmp(zDb,"temp")==0 ){
11127    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
11128  }else{
11129    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
11130  }
11131  for(i=0; i<ArraySize(aQuery); i++){
11132    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
11133    int val = db_int(p, zSql);
11134    sqlite3_free(zSql);
11135    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
11136  }
11137  sqlite3_free(zSchemaTab);
11138  return 0;
11139}
11140
11141/*
11142** Print the current sqlite3_errmsg() value to stderr and return 1.
11143*/
11144static int shellDatabaseError(sqlite3 *db){
11145  const char *zErr = sqlite3_errmsg(db);
11146  utf8_printf(stderr, "Error: %s\n", zErr);
11147  return 1;
11148}
11149
11150/*
11151** Print an out-of-memory message to stderr and return 1.
11152*/
11153static int shellNomemError(void){
11154  raw_printf(stderr, "Error: out of memory\n");
11155  return 1;
11156}
11157
11158/*
11159** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
11160** if they match and FALSE (0) if they do not match.
11161**
11162** Globbing rules:
11163**
11164**      '*'       Matches any sequence of zero or more characters.
11165**
11166**      '?'       Matches exactly one character.
11167**
11168**     [...]      Matches one character from the enclosed list of
11169**                characters.
11170**
11171**     [^...]     Matches one character not in the enclosed list.
11172**
11173**      '#'       Matches any sequence of one or more digits with an
11174**                optional + or - sign in front
11175**
11176**      ' '       Any span of whitespace matches any other span of
11177**                whitespace.
11178**
11179** Extra whitespace at the end of z[] is ignored.
11180*/
11181static int testcase_glob(const char *zGlob, const char *z){
11182  int c, c2;
11183  int invert;
11184  int seen;
11185
11186  while( (c = (*(zGlob++)))!=0 ){
11187    if( IsSpace(c) ){
11188      if( !IsSpace(*z) ) return 0;
11189      while( IsSpace(*zGlob) ) zGlob++;
11190      while( IsSpace(*z) ) z++;
11191    }else if( c=='*' ){
11192      while( (c=(*(zGlob++))) == '*' || c=='?' ){
11193        if( c=='?' && (*(z++))==0 ) return 0;
11194      }
11195      if( c==0 ){
11196        return 1;
11197      }else if( c=='[' ){
11198        while( *z && testcase_glob(zGlob-1,z)==0 ){
11199          z++;
11200        }
11201        return (*z)!=0;
11202      }
11203      while( (c2 = (*(z++)))!=0 ){
11204        while( c2!=c ){
11205          c2 = *(z++);
11206          if( c2==0 ) return 0;
11207        }
11208        if( testcase_glob(zGlob,z) ) return 1;
11209      }
11210      return 0;
11211    }else if( c=='?' ){
11212      if( (*(z++))==0 ) return 0;
11213    }else if( c=='[' ){
11214      int prior_c = 0;
11215      seen = 0;
11216      invert = 0;
11217      c = *(z++);
11218      if( c==0 ) return 0;
11219      c2 = *(zGlob++);
11220      if( c2=='^' ){
11221        invert = 1;
11222        c2 = *(zGlob++);
11223      }
11224      if( c2==']' ){
11225        if( c==']' ) seen = 1;
11226        c2 = *(zGlob++);
11227      }
11228      while( c2 && c2!=']' ){
11229        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
11230          c2 = *(zGlob++);
11231          if( c>=prior_c && c<=c2 ) seen = 1;
11232          prior_c = 0;
11233        }else{
11234          if( c==c2 ){
11235            seen = 1;
11236          }
11237          prior_c = c2;
11238        }
11239        c2 = *(zGlob++);
11240      }
11241      if( c2==0 || (seen ^ invert)==0 ) return 0;
11242    }else if( c=='#' ){
11243      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
11244      if( !IsDigit(z[0]) ) return 0;
11245      z++;
11246      while( IsDigit(z[0]) ){ z++; }
11247    }else{
11248      if( c!=(*(z++)) ) return 0;
11249    }
11250  }
11251  while( IsSpace(*z) ){ z++; }
11252  return *z==0;
11253}
11254
11255
11256/*
11257** Compare the string as a command-line option with either one or two
11258** initial "-" characters.
11259*/
11260static int optionMatch(const char *zStr, const char *zOpt){
11261  if( zStr[0]!='-' ) return 0;
11262  zStr++;
11263  if( zStr[0]=='-' ) zStr++;
11264  return strcmp(zStr, zOpt)==0;
11265}
11266
11267/*
11268** Delete a file.
11269*/
11270int shellDeleteFile(const char *zFilename){
11271  int rc;
11272#ifdef _WIN32
11273  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
11274  rc = _wunlink(z);
11275  sqlite3_free(z);
11276#else
11277  rc = unlink(zFilename);
11278#endif
11279  return rc;
11280}
11281
11282/*
11283** Try to delete the temporary file (if there is one) and free the
11284** memory used to hold the name of the temp file.
11285*/
11286static void clearTempFile(ShellState *p){
11287  if( p->zTempFile==0 ) return;
11288  if( p->doXdgOpen ) return;
11289  if( shellDeleteFile(p->zTempFile) ) return;
11290  sqlite3_free(p->zTempFile);
11291  p->zTempFile = 0;
11292}
11293
11294/*
11295** Create a new temp file name with the given suffix.
11296*/
11297static void newTempFile(ShellState *p, const char *zSuffix){
11298  clearTempFile(p);
11299  sqlite3_free(p->zTempFile);
11300  p->zTempFile = 0;
11301  if( p->db ){
11302    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
11303  }
11304  if( p->zTempFile==0 ){
11305    sqlite3_uint64 r;
11306    sqlite3_randomness(sizeof(r), &r);
11307    p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
11308  }else{
11309    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
11310  }
11311  if( p->zTempFile==0 ){
11312    raw_printf(stderr, "out of memory\n");
11313    exit(1);
11314  }
11315}
11316
11317
11318/*
11319** The implementation of SQL scalar function fkey_collate_clause(), used
11320** by the ".lint fkey-indexes" command. This scalar function is always
11321** called with four arguments - the parent table name, the parent column name,
11322** the child table name and the child column name.
11323**
11324**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
11325**
11326** If either of the named tables or columns do not exist, this function
11327** returns an empty string. An empty string is also returned if both tables
11328** and columns exist but have the same default collation sequence. Or,
11329** if both exist but the default collation sequences are different, this
11330** function returns the string " COLLATE <parent-collation>", where
11331** <parent-collation> is the default collation sequence of the parent column.
11332*/
11333static void shellFkeyCollateClause(
11334  sqlite3_context *pCtx,
11335  int nVal,
11336  sqlite3_value **apVal
11337){
11338  sqlite3 *db = sqlite3_context_db_handle(pCtx);
11339  const char *zParent;
11340  const char *zParentCol;
11341  const char *zParentSeq;
11342  const char *zChild;
11343  const char *zChildCol;
11344  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
11345  int rc;
11346
11347  assert( nVal==4 );
11348  zParent = (const char*)sqlite3_value_text(apVal[0]);
11349  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
11350  zChild = (const char*)sqlite3_value_text(apVal[2]);
11351  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
11352
11353  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
11354  rc = sqlite3_table_column_metadata(
11355      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
11356  );
11357  if( rc==SQLITE_OK ){
11358    rc = sqlite3_table_column_metadata(
11359        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
11360    );
11361  }
11362
11363  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
11364    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
11365    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
11366    sqlite3_free(z);
11367  }
11368}
11369
11370
11371/*
11372** The implementation of dot-command ".lint fkey-indexes".
11373*/
11374static int lintFkeyIndexes(
11375  ShellState *pState,             /* Current shell tool state */
11376  char **azArg,                   /* Array of arguments passed to dot command */
11377  int nArg                        /* Number of entries in azArg[] */
11378){
11379  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
11380  FILE *out = pState->out;        /* Stream to write non-error output to */
11381  int bVerbose = 0;               /* If -verbose is present */
11382  int bGroupByParent = 0;         /* If -groupbyparent is present */
11383  int i;                          /* To iterate through azArg[] */
11384  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
11385  int rc;                         /* Return code */
11386  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
11387
11388  /*
11389  ** This SELECT statement returns one row for each foreign key constraint
11390  ** in the schema of the main database. The column values are:
11391  **
11392  ** 0. The text of an SQL statement similar to:
11393  **
11394  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
11395  **
11396  **    This SELECT is similar to the one that the foreign keys implementation
11397  **    needs to run internally on child tables. If there is an index that can
11398  **    be used to optimize this query, then it can also be used by the FK
11399  **    implementation to optimize DELETE or UPDATE statements on the parent
11400  **    table.
11401  **
11402  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
11403  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
11404  **    contains an index that can be used to optimize the query.
11405  **
11406  ** 2. Human readable text that describes the child table and columns. e.g.
11407  **
11408  **       "child_table(child_key1, child_key2)"
11409  **
11410  ** 3. Human readable text that describes the parent table and columns. e.g.
11411  **
11412  **       "parent_table(parent_key1, parent_key2)"
11413  **
11414  ** 4. A full CREATE INDEX statement for an index that could be used to
11415  **    optimize DELETE or UPDATE statements on the parent table. e.g.
11416  **
11417  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
11418  **
11419  ** 5. The name of the parent table.
11420  **
11421  ** These six values are used by the C logic below to generate the report.
11422  */
11423  const char *zSql =
11424  "SELECT "
11425    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
11426    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
11427    "  || fkey_collate_clause("
11428    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
11429    ", "
11430    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
11431    "  || group_concat('*=?', ' AND ') || ')'"
11432    ", "
11433    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
11434    ", "
11435    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
11436    ", "
11437    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
11438    "  || ' ON ' || quote(s.name) || '('"
11439    "  || group_concat(quote(f.[from]) ||"
11440    "        fkey_collate_clause("
11441    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
11442    "  || ');'"
11443    ", "
11444    "     f.[table] "
11445    "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
11446    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
11447    "GROUP BY s.name, f.id "
11448    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
11449  ;
11450  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
11451
11452  for(i=2; i<nArg; i++){
11453    int n = strlen30(azArg[i]);
11454    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
11455      bVerbose = 1;
11456    }
11457    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
11458      bGroupByParent = 1;
11459      zIndent = "    ";
11460    }
11461    else{
11462      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
11463          azArg[0], azArg[1]
11464      );
11465      return SQLITE_ERROR;
11466    }
11467  }
11468
11469  /* Register the fkey_collate_clause() SQL function */
11470  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
11471      0, shellFkeyCollateClause, 0, 0
11472  );
11473
11474
11475  if( rc==SQLITE_OK ){
11476    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
11477  }
11478  if( rc==SQLITE_OK ){
11479    sqlite3_bind_int(pSql, 1, bGroupByParent);
11480  }
11481
11482  if( rc==SQLITE_OK ){
11483    int rc2;
11484    char *zPrev = 0;
11485    while( SQLITE_ROW==sqlite3_step(pSql) ){
11486      int res = -1;
11487      sqlite3_stmt *pExplain = 0;
11488      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
11489      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
11490      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
11491      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
11492      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
11493      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
11494
11495      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11496      if( rc!=SQLITE_OK ) break;
11497      if( SQLITE_ROW==sqlite3_step(pExplain) ){
11498        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
11499        res = (
11500              0==sqlite3_strglob(zGlob, zPlan)
11501           || 0==sqlite3_strglob(zGlobIPK, zPlan)
11502        );
11503      }
11504      rc = sqlite3_finalize(pExplain);
11505      if( rc!=SQLITE_OK ) break;
11506
11507      if( res<0 ){
11508        raw_printf(stderr, "Error: internal error");
11509        break;
11510      }else{
11511        if( bGroupByParent
11512        && (bVerbose || res==0)
11513        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
11514        ){
11515          raw_printf(out, "-- Parent table %s\n", zParent);
11516          sqlite3_free(zPrev);
11517          zPrev = sqlite3_mprintf("%s", zParent);
11518        }
11519
11520        if( res==0 ){
11521          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
11522        }else if( bVerbose ){
11523          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
11524              zIndent, zFrom, zTarget
11525          );
11526        }
11527      }
11528    }
11529    sqlite3_free(zPrev);
11530
11531    if( rc!=SQLITE_OK ){
11532      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
11533    }
11534
11535    rc2 = sqlite3_finalize(pSql);
11536    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
11537      rc = rc2;
11538      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
11539    }
11540  }else{
11541    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
11542  }
11543
11544  return rc;
11545}
11546
11547/*
11548** Implementation of ".lint" dot command.
11549*/
11550static int lintDotCommand(
11551  ShellState *pState,             /* Current shell tool state */
11552  char **azArg,                   /* Array of arguments passed to dot command */
11553  int nArg                        /* Number of entries in azArg[] */
11554){
11555  int n;
11556  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
11557  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
11558  return lintFkeyIndexes(pState, azArg, nArg);
11559
11560 usage:
11561  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
11562  raw_printf(stderr, "Where sub-commands are:\n");
11563  raw_printf(stderr, "    fkey-indexes\n");
11564  return SQLITE_ERROR;
11565}
11566
11567#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11568/*********************************************************************************
11569** The ".archive" or ".ar" command.
11570*/
11571static void shellPrepare(
11572  sqlite3 *db,
11573  int *pRc,
11574  const char *zSql,
11575  sqlite3_stmt **ppStmt
11576){
11577  *ppStmt = 0;
11578  if( *pRc==SQLITE_OK ){
11579    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
11580    if( rc!=SQLITE_OK ){
11581      raw_printf(stderr, "sql error: %s (%d)\n",
11582          sqlite3_errmsg(db), sqlite3_errcode(db)
11583      );
11584      *pRc = rc;
11585    }
11586  }
11587}
11588
11589static void shellPreparePrintf(
11590  sqlite3 *db,
11591  int *pRc,
11592  sqlite3_stmt **ppStmt,
11593  const char *zFmt,
11594  ...
11595){
11596  *ppStmt = 0;
11597  if( *pRc==SQLITE_OK ){
11598    va_list ap;
11599    char *z;
11600    va_start(ap, zFmt);
11601    z = sqlite3_vmprintf(zFmt, ap);
11602    if( z==0 ){
11603      *pRc = SQLITE_NOMEM;
11604    }else{
11605      shellPrepare(db, pRc, z, ppStmt);
11606      sqlite3_free(z);
11607    }
11608  }
11609}
11610
11611static void shellFinalize(
11612  int *pRc,
11613  sqlite3_stmt *pStmt
11614){
11615  if( pStmt ){
11616    sqlite3 *db = sqlite3_db_handle(pStmt);
11617    int rc = sqlite3_finalize(pStmt);
11618    if( *pRc==SQLITE_OK ){
11619      if( rc!=SQLITE_OK ){
11620        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
11621      }
11622      *pRc = rc;
11623    }
11624  }
11625}
11626
11627static void shellReset(
11628  int *pRc,
11629  sqlite3_stmt *pStmt
11630){
11631  int rc = sqlite3_reset(pStmt);
11632  if( *pRc==SQLITE_OK ){
11633    if( rc!=SQLITE_OK ){
11634      sqlite3 *db = sqlite3_db_handle(pStmt);
11635      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
11636    }
11637    *pRc = rc;
11638  }
11639}
11640/*
11641** Structure representing a single ".ar" command.
11642*/
11643typedef struct ArCommand ArCommand;
11644struct ArCommand {
11645  u8 eCmd;                        /* An AR_CMD_* value */
11646  u8 bVerbose;                    /* True if --verbose */
11647  u8 bZip;                        /* True if the archive is a ZIP */
11648  u8 bDryRun;                     /* True if --dry-run */
11649  u8 bAppend;                     /* True if --append */
11650  int nArg;                       /* Number of command arguments */
11651  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
11652  const char *zFile;              /* --file argument, or NULL */
11653  const char *zDir;               /* --directory argument, or NULL */
11654  char **azArg;                   /* Array of command arguments */
11655  ShellState *p;                  /* Shell state */
11656  sqlite3 *db;                    /* Database containing the archive */
11657};
11658
11659/*
11660** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
11661*/
11662static int arUsage(FILE *f){
11663  raw_printf(f,
11664"\n"
11665"Usage: .ar [OPTION...] [FILE...]\n"
11666"The .ar command manages sqlar archives.\n"
11667"\n"
11668"Examples:\n"
11669"  .ar -cf archive.sar foo bar    # Create archive.sar from files foo and bar\n"
11670"  .ar -tf archive.sar            # List members of archive.sar\n"
11671"  .ar -xvf archive.sar           # Verbosely extract files from archive.sar\n"
11672"\n"
11673"Each command line must feature exactly one command option:\n"
11674"  -c, --create               Create a new archive\n"
11675"  -u, --update               Update or add files to an existing archive\n"
11676"  -t, --list                 List contents of archive\n"
11677"  -x, --extract              Extract files from archive\n"
11678"\n"
11679"And zero or more optional options:\n"
11680"  -v, --verbose              Print each filename as it is processed\n"
11681"  -f FILE, --file FILE       Operate on archive FILE (default is current db)\n"
11682"  -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS\n"
11683"  -C DIR, --directory DIR    Change to directory DIR to read/extract files\n"
11684"  -n, --dryrun               Show the SQL that would have occurred\n"
11685"\n"
11686"See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
11687"\n"
11688);
11689  return SQLITE_ERROR;
11690}
11691
11692/*
11693** Print an error message for the .ar command to stderr and return
11694** SQLITE_ERROR.
11695*/
11696static int arErrorMsg(const char *zFmt, ...){
11697  va_list ap;
11698  char *z;
11699  va_start(ap, zFmt);
11700  z = sqlite3_vmprintf(zFmt, ap);
11701  va_end(ap);
11702  raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z);
11703  sqlite3_free(z);
11704  return SQLITE_ERROR;
11705}
11706
11707/*
11708** Values for ArCommand.eCmd.
11709*/
11710#define AR_CMD_CREATE       1
11711#define AR_CMD_EXTRACT      2
11712#define AR_CMD_LIST         3
11713#define AR_CMD_UPDATE       4
11714#define AR_CMD_HELP         5
11715
11716/*
11717** Other (non-command) switches.
11718*/
11719#define AR_SWITCH_VERBOSE     6
11720#define AR_SWITCH_FILE        7
11721#define AR_SWITCH_DIRECTORY   8
11722#define AR_SWITCH_APPEND      9
11723#define AR_SWITCH_DRYRUN     10
11724
11725static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
11726  switch( eSwitch ){
11727    case AR_CMD_CREATE:
11728    case AR_CMD_EXTRACT:
11729    case AR_CMD_LIST:
11730    case AR_CMD_UPDATE:
11731    case AR_CMD_HELP:
11732      if( pAr->eCmd ){
11733        return arErrorMsg("multiple command options");
11734      }
11735      pAr->eCmd = eSwitch;
11736      break;
11737
11738    case AR_SWITCH_DRYRUN:
11739      pAr->bDryRun = 1;
11740      break;
11741    case AR_SWITCH_VERBOSE:
11742      pAr->bVerbose = 1;
11743      break;
11744    case AR_SWITCH_APPEND:
11745      pAr->bAppend = 1;
11746      /* Fall thru into --file */
11747    case AR_SWITCH_FILE:
11748      pAr->zFile = zArg;
11749      break;
11750    case AR_SWITCH_DIRECTORY:
11751      pAr->zDir = zArg;
11752      break;
11753  }
11754
11755  return SQLITE_OK;
11756}
11757
11758/*
11759** Parse the command line for an ".ar" command. The results are written into
11760** structure (*pAr). SQLITE_OK is returned if the command line is parsed
11761** successfully, otherwise an error message is written to stderr and
11762** SQLITE_ERROR returned.
11763*/
11764static int arParseCommand(
11765  char **azArg,                   /* Array of arguments passed to dot command */
11766  int nArg,                       /* Number of entries in azArg[] */
11767  ArCommand *pAr                  /* Populate this object */
11768){
11769  struct ArSwitch {
11770    const char *zLong;
11771    char cShort;
11772    u8 eSwitch;
11773    u8 bArg;
11774  } aSwitch[] = {
11775    { "create",    'c', AR_CMD_CREATE,       0 },
11776    { "extract",   'x', AR_CMD_EXTRACT,      0 },
11777    { "list",      't', AR_CMD_LIST,         0 },
11778    { "update",    'u', AR_CMD_UPDATE,       0 },
11779    { "help",      'h', AR_CMD_HELP,         0 },
11780    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
11781    { "file",      'f', AR_SWITCH_FILE,      1 },
11782    { "append",    'a', AR_SWITCH_APPEND,    1 },
11783    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
11784    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
11785  };
11786  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
11787  struct ArSwitch *pEnd = &aSwitch[nSwitch];
11788
11789  if( nArg<=1 ){
11790    return arUsage(stderr);
11791  }else{
11792    char *z = azArg[1];
11793    memset(pAr, 0, sizeof(ArCommand));
11794
11795    if( z[0]!='-' ){
11796      /* Traditional style [tar] invocation */
11797      int i;
11798      int iArg = 2;
11799      for(i=0; z[i]; i++){
11800        const char *zArg = 0;
11801        struct ArSwitch *pOpt;
11802        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
11803          if( z[i]==pOpt->cShort ) break;
11804        }
11805        if( pOpt==pEnd ){
11806          return arErrorMsg("unrecognized option: %c", z[i]);
11807        }
11808        if( pOpt->bArg ){
11809          if( iArg>=nArg ){
11810            return arErrorMsg("option requires an argument: %c",z[i]);
11811          }
11812          zArg = azArg[iArg++];
11813        }
11814        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
11815      }
11816      pAr->nArg = nArg-iArg;
11817      if( pAr->nArg>0 ){
11818        pAr->azArg = &azArg[iArg];
11819      }
11820    }else{
11821      /* Non-traditional invocation */
11822      int iArg;
11823      for(iArg=1; iArg<nArg; iArg++){
11824        int n;
11825        z = azArg[iArg];
11826        if( z[0]!='-' ){
11827          /* All remaining command line words are command arguments. */
11828          pAr->azArg = &azArg[iArg];
11829          pAr->nArg = nArg-iArg;
11830          break;
11831        }
11832        n = strlen30(z);
11833
11834        if( z[1]!='-' ){
11835          int i;
11836          /* One or more short options */
11837          for(i=1; i<n; i++){
11838            const char *zArg = 0;
11839            struct ArSwitch *pOpt;
11840            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
11841              if( z[i]==pOpt->cShort ) break;
11842            }
11843            if( pOpt==pEnd ){
11844              return arErrorMsg("unrecognized option: %c\n", z[i]);
11845            }
11846            if( pOpt->bArg ){
11847              if( i<(n-1) ){
11848                zArg = &z[i+1];
11849                i = n;
11850              }else{
11851                if( iArg>=(nArg-1) ){
11852                  return arErrorMsg("option requires an argument: %c\n",z[i]);
11853                }
11854                zArg = azArg[++iArg];
11855              }
11856            }
11857            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
11858          }
11859        }else if( z[2]=='\0' ){
11860          /* A -- option, indicating that all remaining command line words
11861          ** are command arguments.  */
11862          pAr->azArg = &azArg[iArg+1];
11863          pAr->nArg = nArg-iArg-1;
11864          break;
11865        }else{
11866          /* A long option */
11867          const char *zArg = 0;             /* Argument for option, if any */
11868          struct ArSwitch *pMatch = 0;      /* Matching option */
11869          struct ArSwitch *pOpt;            /* Iterator */
11870          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
11871            const char *zLong = pOpt->zLong;
11872            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
11873              if( pMatch ){
11874                return arErrorMsg("ambiguous option: %s",z);
11875              }else{
11876                pMatch = pOpt;
11877              }
11878            }
11879          }
11880
11881          if( pMatch==0 ){
11882            return arErrorMsg("unrecognized option: %s", z);
11883          }
11884          if( pMatch->bArg ){
11885            if( iArg>=(nArg-1) ){
11886              return arErrorMsg("option requires an argument: %s", z);
11887            }
11888            zArg = azArg[++iArg];
11889          }
11890          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
11891        }
11892      }
11893    }
11894  }
11895
11896  return SQLITE_OK;
11897}
11898
11899/*
11900** This function assumes that all arguments within the ArCommand.azArg[]
11901** array refer to archive members, as for the --extract or --list commands.
11902** It checks that each of them are present. If any specified file is not
11903** present in the archive, an error is printed to stderr and an error
11904** code returned. Otherwise, if all specified arguments are present in
11905** the archive, SQLITE_OK is returned.
11906**
11907** This function strips any trailing '/' characters from each argument.
11908** This is consistent with the way the [tar] command seems to work on
11909** Linux.
11910*/
11911static int arCheckEntries(ArCommand *pAr){
11912  int rc = SQLITE_OK;
11913  if( pAr->nArg ){
11914    int i, j;
11915    sqlite3_stmt *pTest = 0;
11916
11917    shellPreparePrintf(pAr->db, &rc, &pTest,
11918        "SELECT name FROM %s WHERE name=$name",
11919        pAr->zSrcTable
11920    );
11921    j = sqlite3_bind_parameter_index(pTest, "$name");
11922    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11923      char *z = pAr->azArg[i];
11924      int n = strlen30(z);
11925      int bOk = 0;
11926      while( n>0 && z[n-1]=='/' ) n--;
11927      z[n] = '\0';
11928      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
11929      if( SQLITE_ROW==sqlite3_step(pTest) ){
11930        bOk = 1;
11931      }
11932      shellReset(&rc, pTest);
11933      if( rc==SQLITE_OK && bOk==0 ){
11934        utf8_printf(stderr, "not found in archive: %s\n", z);
11935        rc = SQLITE_ERROR;
11936      }
11937    }
11938    shellFinalize(&rc, pTest);
11939  }
11940  return rc;
11941}
11942
11943/*
11944** Format a WHERE clause that can be used against the "sqlar" table to
11945** identify all archive members that match the command arguments held
11946** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
11947** The caller is responsible for eventually calling sqlite3_free() on
11948** any non-NULL (*pzWhere) value.
11949*/
11950static void arWhereClause(
11951  int *pRc,
11952  ArCommand *pAr,
11953  char **pzWhere                  /* OUT: New WHERE clause */
11954){
11955  char *zWhere = 0;
11956  if( *pRc==SQLITE_OK ){
11957    if( pAr->nArg==0 ){
11958      zWhere = sqlite3_mprintf("1");
11959    }else{
11960      int i;
11961      const char *zSep = "";
11962      for(i=0; i<pAr->nArg; i++){
11963        const char *z = pAr->azArg[i];
11964        zWhere = sqlite3_mprintf(
11965          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
11966          zWhere, zSep, z, strlen30(z)+1, z
11967        );
11968        if( zWhere==0 ){
11969          *pRc = SQLITE_NOMEM;
11970          break;
11971        }
11972        zSep = " OR ";
11973      }
11974    }
11975  }
11976  *pzWhere = zWhere;
11977}
11978
11979/*
11980** Implementation of .ar "lisT" command.
11981*/
11982static int arListCommand(ArCommand *pAr){
11983  const char *zSql = "SELECT %s FROM %s WHERE %s";
11984  const char *azCols[] = {
11985    "name",
11986    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
11987  };
11988
11989  char *zWhere = 0;
11990  sqlite3_stmt *pSql = 0;
11991  int rc;
11992
11993  rc = arCheckEntries(pAr);
11994  arWhereClause(&rc, pAr, &zWhere);
11995
11996  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
11997                     pAr->zSrcTable, zWhere);
11998  if( pAr->bDryRun ){
11999    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12000  }else{
12001    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12002      if( pAr->bVerbose ){
12003        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
12004            sqlite3_column_text(pSql, 0),
12005            sqlite3_column_int(pSql, 1),
12006            sqlite3_column_text(pSql, 2),
12007            sqlite3_column_text(pSql, 3)
12008        );
12009      }else{
12010        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12011      }
12012    }
12013  }
12014  shellFinalize(&rc, pSql);
12015  return rc;
12016}
12017
12018
12019/*
12020** Implementation of .ar "eXtract" command.
12021*/
12022static int arExtractCommand(ArCommand *pAr){
12023  const char *zSql1 =
12024    "SELECT "
12025    " ($dir || name),"
12026    " writefile(($dir || name), %s, mode, mtime) "
12027    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
12028
12029  const char *azExtraArg[] = {
12030    "sqlar_uncompress(data, sz)",
12031    "data"
12032  };
12033
12034  sqlite3_stmt *pSql = 0;
12035  int rc = SQLITE_OK;
12036  char *zDir = 0;
12037  char *zWhere = 0;
12038  int i, j;
12039
12040  /* If arguments are specified, check that they actually exist within
12041  ** the archive before proceeding. And formulate a WHERE clause to
12042  ** match them.  */
12043  rc = arCheckEntries(pAr);
12044  arWhereClause(&rc, pAr, &zWhere);
12045
12046  if( rc==SQLITE_OK ){
12047    if( pAr->zDir ){
12048      zDir = sqlite3_mprintf("%s/", pAr->zDir);
12049    }else{
12050      zDir = sqlite3_mprintf("");
12051    }
12052    if( zDir==0 ) rc = SQLITE_NOMEM;
12053  }
12054
12055  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
12056      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
12057  );
12058
12059  if( rc==SQLITE_OK ){
12060    j = sqlite3_bind_parameter_index(pSql, "$dir");
12061    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
12062
12063    /* Run the SELECT statement twice. The first time, writefile() is called
12064    ** for all archive members that should be extracted. The second time,
12065    ** only for the directories. This is because the timestamps for
12066    ** extracted directories must be reset after they are populated (as
12067    ** populating them changes the timestamp).  */
12068    for(i=0; i<2; i++){
12069      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
12070      sqlite3_bind_int(pSql, j, i);
12071      if( pAr->bDryRun ){
12072        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12073      }else{
12074        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12075          if( i==0 && pAr->bVerbose ){
12076            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12077          }
12078        }
12079      }
12080      shellReset(&rc, pSql);
12081    }
12082    shellFinalize(&rc, pSql);
12083  }
12084
12085  sqlite3_free(zDir);
12086  sqlite3_free(zWhere);
12087  return rc;
12088}
12089
12090/*
12091** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
12092*/
12093static int arExecSql(ArCommand *pAr, const char *zSql){
12094  int rc;
12095  if( pAr->bDryRun ){
12096    utf8_printf(pAr->p->out, "%s\n", zSql);
12097    rc = SQLITE_OK;
12098  }else{
12099    char *zErr = 0;
12100    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
12101    if( zErr ){
12102      utf8_printf(stdout, "ERROR: %s\n", zErr);
12103      sqlite3_free(zErr);
12104    }
12105  }
12106  return rc;
12107}
12108
12109
12110/*
12111** Implementation of .ar "create" and "update" commands.
12112**
12113** Create the "sqlar" table in the database if it does not already exist.
12114** Then add each file in the azFile[] array to the archive. Directories
12115** are added recursively. If argument bVerbose is non-zero, a message is
12116** printed on stdout for each file archived.
12117**
12118** The create command is the same as update, except that it drops
12119** any existing "sqlar" table before beginning.
12120*/
12121static int arCreateOrUpdateCommand(
12122  ArCommand *pAr,                 /* Command arguments and options */
12123  int bUpdate                     /* true for a --create.  false for --update */
12124){
12125  const char *zCreate =
12126      "CREATE TABLE IF NOT EXISTS sqlar(\n"
12127      "  name TEXT PRIMARY KEY,  -- name of the file\n"
12128      "  mode INT,               -- access permissions\n"
12129      "  mtime INT,              -- last modification time\n"
12130      "  sz INT,                 -- original file size\n"
12131      "  data BLOB               -- compressed content\n"
12132      ")";
12133  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
12134  const char *zInsertFmt =
12135     "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
12136     "  SELECT\n"
12137     "    %s,\n"
12138     "    mode,\n"
12139     "    mtime,\n"
12140     "    CASE substr(lsmode(mode),1,1)\n"
12141     "      WHEN '-' THEN length(data)\n"
12142     "      WHEN 'd' THEN 0\n"
12143     "      ELSE -1 END,\n"
12144     "    CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
12145     "  FROM fsdir(%Q,%Q)\n"
12146     "  WHERE lsmode(mode) NOT LIKE '?%%';";
12147  int i;                          /* For iterating through azFile[] */
12148  int rc;                         /* Return code */
12149
12150  rc = arExecSql(pAr, "SAVEPOINT ar;");
12151  if( rc!=SQLITE_OK ) return rc;
12152  if( bUpdate==0 ){
12153    rc = arExecSql(pAr, zDrop);
12154    if( rc!=SQLITE_OK ) return rc;
12155  }
12156  rc = arExecSql(pAr, zCreate);
12157  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12158    char *zSql = sqlite3_mprintf(zInsertFmt,
12159        pAr->bVerbose ? "shell_putsnl(name)" : "name",
12160        pAr->azArg[i], pAr->zDir);
12161    rc = arExecSql(pAr, zSql);
12162    sqlite3_free(zSql);
12163  }
12164  if( rc!=SQLITE_OK ){
12165    arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12166  }else{
12167    rc = arExecSql(pAr, "RELEASE ar;");
12168  }
12169  return rc;
12170}
12171
12172/*
12173** Implementation of ".ar" dot command.
12174*/
12175static int arDotCommand(
12176  ShellState *pState,             /* Current shell tool state */
12177  char **azArg,                   /* Array of arguments passed to dot command */
12178  int nArg                        /* Number of entries in azArg[] */
12179){
12180  ArCommand cmd;
12181  int rc;
12182  memset(&cmd, 0, sizeof(cmd));
12183  rc = arParseCommand(azArg, nArg, &cmd);
12184  if( rc==SQLITE_OK ){
12185    int eDbType = SHELL_OPEN_UNSPEC;
12186    cmd.p = pState;
12187    cmd.db = pState->db;
12188    if( cmd.zFile ){
12189      eDbType = deduceDatabaseType(cmd.zFile);
12190    }else{
12191      eDbType = pState->openMode;
12192    }
12193    if( eDbType==SHELL_OPEN_ZIPFILE ){
12194      if( cmd.zFile==0 ){
12195        cmd.zSrcTable = sqlite3_mprintf("zip");
12196      }else{
12197        cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12198      }
12199      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12200        utf8_printf(stderr, "zip archives are read-only\n");
12201        rc = SQLITE_ERROR;
12202        goto end_ar_command;
12203      }
12204      cmd.bZip = 1;
12205    }else if( cmd.zFile ){
12206      int flags;
12207      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
12208      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12209        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
12210      }else{
12211        flags = SQLITE_OPEN_READONLY;
12212      }
12213      cmd.db = 0;
12214      if( cmd.bDryRun ){
12215        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
12216             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
12217      }
12218      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
12219             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
12220      if( rc!=SQLITE_OK ){
12221        utf8_printf(stderr, "cannot open file: %s (%s)\n",
12222            cmd.zFile, sqlite3_errmsg(cmd.db)
12223        );
12224        goto end_ar_command;
12225      }
12226      sqlite3_fileio_init(cmd.db, 0, 0);
12227#ifdef SQLITE_HAVE_ZLIB
12228      sqlite3_sqlar_init(cmd.db, 0, 0);
12229#endif
12230      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
12231                              shellPutsFunc, 0, 0);
12232
12233    }
12234    if( cmd.zSrcTable==0 ){
12235      if( cmd.eCmd!=AR_CMD_CREATE
12236       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
12237      ){
12238        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
12239        rc = SQLITE_ERROR;
12240        goto end_ar_command;
12241      }
12242      cmd.zSrcTable = sqlite3_mprintf("sqlar");
12243    }
12244
12245    switch( cmd.eCmd ){
12246      case AR_CMD_CREATE:
12247        rc = arCreateOrUpdateCommand(&cmd, 0);
12248        break;
12249
12250      case AR_CMD_EXTRACT:
12251        rc = arExtractCommand(&cmd);
12252        break;
12253
12254      case AR_CMD_LIST:
12255        rc = arListCommand(&cmd);
12256        break;
12257
12258      case AR_CMD_HELP:
12259        arUsage(pState->out);
12260        break;
12261
12262      default:
12263        assert( cmd.eCmd==AR_CMD_UPDATE );
12264        rc = arCreateOrUpdateCommand(&cmd, 1);
12265        break;
12266    }
12267  }
12268end_ar_command:
12269  if( cmd.db!=pState->db ){
12270    sqlite3_close(cmd.db);
12271  }
12272  sqlite3_free(cmd.zSrcTable);
12273
12274  return rc;
12275}
12276/* End of the ".archive" or ".ar" command logic
12277**********************************************************************************/
12278#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
12279
12280
12281/*
12282** If an input line begins with "." then invoke this routine to
12283** process that line.
12284**
12285** Return 1 on error, 2 to exit, and 0 otherwise.
12286*/
12287static int do_meta_command(char *zLine, ShellState *p){
12288  int h = 1;
12289  int nArg = 0;
12290  int n, c;
12291  int rc = 0;
12292  char *azArg[50];
12293
12294#ifndef SQLITE_OMIT_VIRTUALTABLE
12295  if( p->expert.pExpert ){
12296    expertFinish(p, 1, 0);
12297  }
12298#endif
12299
12300  /* Parse the input line into tokens.
12301  */
12302  while( zLine[h] && nArg<ArraySize(azArg) ){
12303    while( IsSpace(zLine[h]) ){ h++; }
12304    if( zLine[h]==0 ) break;
12305    if( zLine[h]=='\'' || zLine[h]=='"' ){
12306      int delim = zLine[h++];
12307      azArg[nArg++] = &zLine[h];
12308      while( zLine[h] && zLine[h]!=delim ){
12309        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
12310        h++;
12311      }
12312      if( zLine[h]==delim ){
12313        zLine[h++] = 0;
12314      }
12315      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
12316    }else{
12317      azArg[nArg++] = &zLine[h];
12318      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
12319      if( zLine[h] ) zLine[h++] = 0;
12320      resolve_backslashes(azArg[nArg-1]);
12321    }
12322  }
12323
12324  /* Process the input line.
12325  */
12326  if( nArg==0 ) return 0; /* no tokens, no error */
12327  n = strlen30(azArg[0]);
12328  c = azArg[0][0];
12329  clearTempFile(p);
12330
12331#ifndef SQLITE_OMIT_AUTHORIZATION
12332  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
12333    if( nArg!=2 ){
12334      raw_printf(stderr, "Usage: .auth ON|OFF\n");
12335      rc = 1;
12336      goto meta_command_exit;
12337    }
12338    open_db(p, 0);
12339    if( booleanValue(azArg[1]) ){
12340      sqlite3_set_authorizer(p->db, shellAuth, p);
12341    }else{
12342      sqlite3_set_authorizer(p->db, 0, 0);
12343    }
12344  }else
12345#endif
12346
12347#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12348  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
12349    open_db(p, 0);
12350    rc = arDotCommand(p, azArg, nArg);
12351  }else
12352#endif
12353
12354  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
12355   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
12356  ){
12357    const char *zDestFile = 0;
12358    const char *zDb = 0;
12359    sqlite3 *pDest;
12360    sqlite3_backup *pBackup;
12361    int j;
12362    for(j=1; j<nArg; j++){
12363      const char *z = azArg[j];
12364      if( z[0]=='-' ){
12365        while( z[0]=='-' ) z++;
12366        /* No options to process at this time */
12367        {
12368          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
12369          return 1;
12370        }
12371      }else if( zDestFile==0 ){
12372        zDestFile = azArg[j];
12373      }else if( zDb==0 ){
12374        zDb = zDestFile;
12375        zDestFile = azArg[j];
12376      }else{
12377        raw_printf(stderr, "too many arguments to .backup\n");
12378        return 1;
12379      }
12380    }
12381    if( zDestFile==0 ){
12382      raw_printf(stderr, "missing FILENAME argument on .backup\n");
12383      return 1;
12384    }
12385    if( zDb==0 ) zDb = "main";
12386    rc = sqlite3_open(zDestFile, &pDest);
12387    if( rc!=SQLITE_OK ){
12388      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
12389      sqlite3_close(pDest);
12390      return 1;
12391    }
12392    open_db(p, 0);
12393    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
12394    if( pBackup==0 ){
12395      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
12396      sqlite3_close(pDest);
12397      return 1;
12398    }
12399    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
12400    sqlite3_backup_finish(pBackup);
12401    if( rc==SQLITE_DONE ){
12402      rc = 0;
12403    }else{
12404      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
12405      rc = 1;
12406    }
12407    sqlite3_close(pDest);
12408  }else
12409
12410  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
12411    if( nArg==2 ){
12412      bail_on_error = booleanValue(azArg[1]);
12413    }else{
12414      raw_printf(stderr, "Usage: .bail on|off\n");
12415      rc = 1;
12416    }
12417  }else
12418
12419  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
12420    if( nArg==2 ){
12421      if( booleanValue(azArg[1]) ){
12422        setBinaryMode(p->out, 1);
12423      }else{
12424        setTextMode(p->out, 1);
12425      }
12426    }else{
12427      raw_printf(stderr, "Usage: .binary on|off\n");
12428      rc = 1;
12429    }
12430  }else
12431
12432  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
12433    if( nArg==2 ){
12434#if defined(_WIN32) || defined(WIN32)
12435      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
12436      rc = !SetCurrentDirectoryW(z);
12437      sqlite3_free(z);
12438#else
12439      rc = chdir(azArg[1]);
12440#endif
12441      if( rc ){
12442        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
12443        rc = 1;
12444      }
12445    }else{
12446      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
12447      rc = 1;
12448    }
12449  }else
12450
12451  /* The undocumented ".breakpoint" command causes a call to the no-op
12452  ** routine named test_breakpoint().
12453  */
12454  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
12455    test_breakpoint();
12456  }else
12457
12458  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
12459    if( nArg==2 ){
12460      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
12461    }else{
12462      raw_printf(stderr, "Usage: .changes on|off\n");
12463      rc = 1;
12464    }
12465  }else
12466
12467  /* Cancel output redirection, if it is currently set (by .testcase)
12468  ** Then read the content of the testcase-out.txt file and compare against
12469  ** azArg[1].  If there are differences, report an error and exit.
12470  */
12471  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
12472    char *zRes = 0;
12473    output_reset(p);
12474    if( nArg!=2 ){
12475      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
12476      rc = 2;
12477    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
12478      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
12479      rc = 2;
12480    }else if( testcase_glob(azArg[1],zRes)==0 ){
12481      utf8_printf(stderr,
12482                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
12483                 p->zTestcase, azArg[1], zRes);
12484      rc = 1;
12485    }else{
12486      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
12487      p->nCheck++;
12488    }
12489    sqlite3_free(zRes);
12490  }else
12491
12492  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
12493    if( nArg==2 ){
12494      tryToClone(p, azArg[1]);
12495    }else{
12496      raw_printf(stderr, "Usage: .clone FILENAME\n");
12497      rc = 1;
12498    }
12499  }else
12500
12501  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
12502    ShellState data;
12503    char *zErrMsg = 0;
12504    open_db(p, 0);
12505    memcpy(&data, p, sizeof(data));
12506    data.showHeader = 0;
12507    data.cMode = data.mode = MODE_List;
12508    sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
12509    data.cnt = 0;
12510    sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
12511                 callback, &data, &zErrMsg);
12512    if( zErrMsg ){
12513      utf8_printf(stderr,"Error: %s\n", zErrMsg);
12514      sqlite3_free(zErrMsg);
12515      rc = 1;
12516    }
12517  }else
12518
12519  if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
12520    rc = shell_dbinfo_command(p, nArg, azArg);
12521  }else
12522
12523  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
12524    const char *zLike = 0;
12525    int i;
12526    int savedShowHeader = p->showHeader;
12527    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
12528    for(i=1; i<nArg; i++){
12529      if( azArg[i][0]=='-' ){
12530        const char *z = azArg[i]+1;
12531        if( z[0]=='-' ) z++;
12532        if( strcmp(z,"preserve-rowids")==0 ){
12533#ifdef SQLITE_OMIT_VIRTUALTABLE
12534          raw_printf(stderr, "The --preserve-rowids option is not compatible"
12535                             " with SQLITE_OMIT_VIRTUALTABLE\n");
12536          rc = 1;
12537          goto meta_command_exit;
12538#else
12539          ShellSetFlag(p, SHFLG_PreserveRowid);
12540#endif
12541        }else
12542        if( strcmp(z,"newlines")==0 ){
12543          ShellSetFlag(p, SHFLG_Newlines);
12544        }else
12545        {
12546          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
12547          rc = 1;
12548          goto meta_command_exit;
12549        }
12550      }else if( zLike ){
12551        raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
12552                           "?--newlines? ?LIKE-PATTERN?\n");
12553        rc = 1;
12554        goto meta_command_exit;
12555      }else{
12556        zLike = azArg[i];
12557      }
12558    }
12559    open_db(p, 0);
12560    /* When playing back a "dump", the content might appear in an order
12561    ** which causes immediate foreign key constraints to be violated.
12562    ** So disable foreign-key constraint enforcement to prevent problems. */
12563    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
12564    raw_printf(p->out, "BEGIN TRANSACTION;\n");
12565    p->writableSchema = 0;
12566    p->showHeader = 0;
12567    /* Set writable_schema=ON since doing so forces SQLite to initialize
12568    ** as much of the schema as it can even if the sqlite_master table is
12569    ** corrupt. */
12570    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
12571    p->nErr = 0;
12572    if( zLike==0 ){
12573      run_schema_dump_query(p,
12574        "SELECT name, type, sql FROM sqlite_master "
12575        "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
12576      );
12577      run_schema_dump_query(p,
12578        "SELECT name, type, sql FROM sqlite_master "
12579        "WHERE name=='sqlite_sequence'"
12580      );
12581      run_table_dump_query(p,
12582        "SELECT sql FROM sqlite_master "
12583        "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
12584      );
12585    }else{
12586      char *zSql;
12587      zSql = sqlite3_mprintf(
12588        "SELECT name, type, sql FROM sqlite_master "
12589        "WHERE tbl_name LIKE %Q AND type=='table'"
12590        "  AND sql NOT NULL", zLike);
12591      run_schema_dump_query(p,zSql);
12592      sqlite3_free(zSql);
12593      zSql = sqlite3_mprintf(
12594        "SELECT sql FROM sqlite_master "
12595        "WHERE sql NOT NULL"
12596        "  AND type IN ('index','trigger','view')"
12597        "  AND tbl_name LIKE %Q", zLike);
12598      run_table_dump_query(p, zSql, 0);
12599      sqlite3_free(zSql);
12600    }
12601    if( p->writableSchema ){
12602      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
12603      p->writableSchema = 0;
12604    }
12605    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
12606    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
12607    raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
12608    p->showHeader = savedShowHeader;
12609  }else
12610
12611  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
12612    if( nArg==2 ){
12613      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
12614    }else{
12615      raw_printf(stderr, "Usage: .echo on|off\n");
12616      rc = 1;
12617    }
12618  }else
12619
12620  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
12621    if( nArg==2 ){
12622      if( strcmp(azArg[1],"full")==0 ){
12623        p->autoEQP = AUTOEQP_full;
12624      }else if( strcmp(azArg[1],"trigger")==0 ){
12625        p->autoEQP = AUTOEQP_trigger;
12626      }else{
12627        p->autoEQP = booleanValue(azArg[1]);
12628      }
12629    }else{
12630      raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
12631      rc = 1;
12632    }
12633  }else
12634
12635  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
12636    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
12637    rc = 2;
12638  }else
12639
12640  /* The ".explain" command is automatic now.  It is largely pointless.  It
12641  ** retained purely for backwards compatibility */
12642  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
12643    int val = 1;
12644    if( nArg>=2 ){
12645      if( strcmp(azArg[1],"auto")==0 ){
12646        val = 99;
12647      }else{
12648        val =  booleanValue(azArg[1]);
12649      }
12650    }
12651    if( val==1 && p->mode!=MODE_Explain ){
12652      p->normalMode = p->mode;
12653      p->mode = MODE_Explain;
12654      p->autoExplain = 0;
12655    }else if( val==0 ){
12656      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
12657      p->autoExplain = 0;
12658    }else if( val==99 ){
12659      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
12660      p->autoExplain = 1;
12661    }
12662  }else
12663
12664#ifndef SQLITE_OMIT_VIRTUALTABLE
12665  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
12666    open_db(p, 0);
12667    expertDotCommand(p, azArg, nArg);
12668  }else
12669#endif
12670
12671  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
12672    ShellState data;
12673    char *zErrMsg = 0;
12674    int doStats = 0;
12675    memcpy(&data, p, sizeof(data));
12676    data.showHeader = 0;
12677    data.cMode = data.mode = MODE_Semi;
12678    if( nArg==2 && optionMatch(azArg[1], "indent") ){
12679      data.cMode = data.mode = MODE_Pretty;
12680      nArg = 1;
12681    }
12682    if( nArg!=1 ){
12683      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
12684      rc = 1;
12685      goto meta_command_exit;
12686    }
12687    open_db(p, 0);
12688    rc = sqlite3_exec(p->db,
12689       "SELECT sql FROM"
12690       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
12691       "     FROM sqlite_master UNION ALL"
12692       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
12693       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
12694       "ORDER BY rowid",
12695       callback, &data, &zErrMsg
12696    );
12697    if( rc==SQLITE_OK ){
12698      sqlite3_stmt *pStmt;
12699      rc = sqlite3_prepare_v2(p->db,
12700               "SELECT rowid FROM sqlite_master"
12701               " WHERE name GLOB 'sqlite_stat[134]'",
12702               -1, &pStmt, 0);
12703      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
12704      sqlite3_finalize(pStmt);
12705    }
12706    if( doStats==0 ){
12707      raw_printf(p->out, "/* No STAT tables available */\n");
12708    }else{
12709      raw_printf(p->out, "ANALYZE sqlite_master;\n");
12710      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
12711                   callback, &data, &zErrMsg);
12712      data.cMode = data.mode = MODE_Insert;
12713      data.zDestTable = "sqlite_stat1";
12714      shell_exec(p->db, "SELECT * FROM sqlite_stat1",
12715                 shell_callback, &data,&zErrMsg);
12716      data.zDestTable = "sqlite_stat3";
12717      shell_exec(p->db, "SELECT * FROM sqlite_stat3",
12718                 shell_callback, &data,&zErrMsg);
12719      data.zDestTable = "sqlite_stat4";
12720      shell_exec(p->db, "SELECT * FROM sqlite_stat4",
12721                 shell_callback, &data, &zErrMsg);
12722      raw_printf(p->out, "ANALYZE sqlite_master;\n");
12723    }
12724  }else
12725
12726  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
12727    if( nArg==2 ){
12728      p->showHeader = booleanValue(azArg[1]);
12729    }else{
12730      raw_printf(stderr, "Usage: .headers on|off\n");
12731      rc = 1;
12732    }
12733  }else
12734
12735  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
12736    utf8_printf(p->out, "%s", zHelp);
12737  }else
12738
12739  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
12740    char *zTable;               /* Insert data into this table */
12741    char *zFile;                /* Name of file to extra content from */
12742    sqlite3_stmt *pStmt = NULL; /* A statement */
12743    int nCol;                   /* Number of columns in the table */
12744    int nByte;                  /* Number of bytes in an SQL string */
12745    int i, j;                   /* Loop counters */
12746    int needCommit;             /* True to COMMIT or ROLLBACK at end */
12747    int nSep;                   /* Number of bytes in p->colSeparator[] */
12748    char *zSql;                 /* An SQL statement */
12749    ImportCtx sCtx;             /* Reader context */
12750    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
12751    int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
12752
12753    if( nArg!=3 ){
12754      raw_printf(stderr, "Usage: .import FILE TABLE\n");
12755      goto meta_command_exit;
12756    }
12757    zFile = azArg[1];
12758    zTable = azArg[2];
12759    seenInterrupt = 0;
12760    memset(&sCtx, 0, sizeof(sCtx));
12761    open_db(p, 0);
12762    nSep = strlen30(p->colSeparator);
12763    if( nSep==0 ){
12764      raw_printf(stderr,
12765                 "Error: non-null column separator required for import\n");
12766      return 1;
12767    }
12768    if( nSep>1 ){
12769      raw_printf(stderr, "Error: multi-character column separators not allowed"
12770                      " for import\n");
12771      return 1;
12772    }
12773    nSep = strlen30(p->rowSeparator);
12774    if( nSep==0 ){
12775      raw_printf(stderr, "Error: non-null row separator required for import\n");
12776      return 1;
12777    }
12778    if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
12779      /* When importing CSV (only), if the row separator is set to the
12780      ** default output row separator, change it to the default input
12781      ** row separator.  This avoids having to maintain different input
12782      ** and output row separators. */
12783      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
12784      nSep = strlen30(p->rowSeparator);
12785    }
12786    if( nSep>1 ){
12787      raw_printf(stderr, "Error: multi-character row separators not allowed"
12788                      " for import\n");
12789      return 1;
12790    }
12791    sCtx.zFile = zFile;
12792    sCtx.nLine = 1;
12793    if( sCtx.zFile[0]=='|' ){
12794#ifdef SQLITE_OMIT_POPEN
12795      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
12796      return 1;
12797#else
12798      sCtx.in = popen(sCtx.zFile+1, "r");
12799      sCtx.zFile = "<pipe>";
12800      xCloser = pclose;
12801#endif
12802    }else{
12803      sCtx.in = fopen(sCtx.zFile, "rb");
12804      xCloser = fclose;
12805    }
12806    if( p->mode==MODE_Ascii ){
12807      xRead = ascii_read_one_field;
12808    }else{
12809      xRead = csv_read_one_field;
12810    }
12811    if( sCtx.in==0 ){
12812      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
12813      return 1;
12814    }
12815    sCtx.cColSep = p->colSeparator[0];
12816    sCtx.cRowSep = p->rowSeparator[0];
12817    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
12818    if( zSql==0 ){
12819      raw_printf(stderr, "Error: out of memory\n");
12820      xCloser(sCtx.in);
12821      return 1;
12822    }
12823    nByte = strlen30(zSql);
12824    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12825    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
12826    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
12827      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
12828      char cSep = '(';
12829      while( xRead(&sCtx) ){
12830        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
12831        cSep = ',';
12832        if( sCtx.cTerm!=sCtx.cColSep ) break;
12833      }
12834      if( cSep=='(' ){
12835        sqlite3_free(zCreate);
12836        sqlite3_free(sCtx.z);
12837        xCloser(sCtx.in);
12838        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
12839        return 1;
12840      }
12841      zCreate = sqlite3_mprintf("%z\n)", zCreate);
12842      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
12843      sqlite3_free(zCreate);
12844      if( rc ){
12845        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
12846                sqlite3_errmsg(p->db));
12847        sqlite3_free(sCtx.z);
12848        xCloser(sCtx.in);
12849        return 1;
12850      }
12851      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12852    }
12853    sqlite3_free(zSql);
12854    if( rc ){
12855      if (pStmt) sqlite3_finalize(pStmt);
12856      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
12857      xCloser(sCtx.in);
12858      return 1;
12859    }
12860    nCol = sqlite3_column_count(pStmt);
12861    sqlite3_finalize(pStmt);
12862    pStmt = 0;
12863    if( nCol==0 ) return 0; /* no columns, no error */
12864    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
12865    if( zSql==0 ){
12866      raw_printf(stderr, "Error: out of memory\n");
12867      xCloser(sCtx.in);
12868      return 1;
12869    }
12870    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
12871    j = strlen30(zSql);
12872    for(i=1; i<nCol; i++){
12873      zSql[j++] = ',';
12874      zSql[j++] = '?';
12875    }
12876    zSql[j++] = ')';
12877    zSql[j] = 0;
12878    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12879    sqlite3_free(zSql);
12880    if( rc ){
12881      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
12882      if (pStmt) sqlite3_finalize(pStmt);
12883      xCloser(sCtx.in);
12884      return 1;
12885    }
12886    needCommit = sqlite3_get_autocommit(p->db);
12887    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
12888    do{
12889      int startLine = sCtx.nLine;
12890      for(i=0; i<nCol; i++){
12891        char *z = xRead(&sCtx);
12892        /*
12893        ** Did we reach end-of-file before finding any columns?
12894        ** If so, stop instead of NULL filling the remaining columns.
12895        */
12896        if( z==0 && i==0 ) break;
12897        /*
12898        ** Did we reach end-of-file OR end-of-line before finding any
12899        ** columns in ASCII mode?  If so, stop instead of NULL filling
12900        ** the remaining columns.
12901        */
12902        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
12903        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
12904        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
12905          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
12906                          "filling the rest with NULL\n",
12907                          sCtx.zFile, startLine, nCol, i+1);
12908          i += 2;
12909          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
12910        }
12911      }
12912      if( sCtx.cTerm==sCtx.cColSep ){
12913        do{
12914          xRead(&sCtx);
12915          i++;
12916        }while( sCtx.cTerm==sCtx.cColSep );
12917        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
12918                        "extras ignored\n",
12919                        sCtx.zFile, startLine, nCol, i);
12920      }
12921      if( i>=nCol ){
12922        sqlite3_step(pStmt);
12923        rc = sqlite3_reset(pStmt);
12924        if( rc!=SQLITE_OK ){
12925          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
12926                      startLine, sqlite3_errmsg(p->db));
12927        }
12928      }
12929    }while( sCtx.cTerm!=EOF );
12930
12931    xCloser(sCtx.in);
12932    sqlite3_free(sCtx.z);
12933    sqlite3_finalize(pStmt);
12934    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
12935  }else
12936
12937#ifndef SQLITE_UNTESTABLE
12938  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
12939    char *zSql;
12940    char *zCollist = 0;
12941    sqlite3_stmt *pStmt;
12942    int tnum = 0;
12943    int i;
12944    if( nArg!=3 ){
12945      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
12946      rc = 1;
12947      goto meta_command_exit;
12948    }
12949    open_db(p, 0);
12950    zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
12951                           " WHERE name='%q' AND type='index'", azArg[1]);
12952    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12953    sqlite3_free(zSql);
12954    if( sqlite3_step(pStmt)==SQLITE_ROW ){
12955      tnum = sqlite3_column_int(pStmt, 0);
12956    }
12957    sqlite3_finalize(pStmt);
12958    if( tnum==0 ){
12959      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
12960      rc = 1;
12961      goto meta_command_exit;
12962    }
12963    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
12964    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12965    sqlite3_free(zSql);
12966    i = 0;
12967    while( sqlite3_step(pStmt)==SQLITE_ROW ){
12968      char zLabel[20];
12969      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
12970      i++;
12971      if( zCol==0 ){
12972        if( sqlite3_column_int(pStmt,1)==-1 ){
12973          zCol = "_ROWID_";
12974        }else{
12975          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
12976          zCol = zLabel;
12977        }
12978      }
12979      if( zCollist==0 ){
12980        zCollist = sqlite3_mprintf("\"%w\"", zCol);
12981      }else{
12982        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
12983      }
12984    }
12985    sqlite3_finalize(pStmt);
12986    zSql = sqlite3_mprintf(
12987          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
12988          azArg[2], zCollist, zCollist);
12989    sqlite3_free(zCollist);
12990    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
12991    if( rc==SQLITE_OK ){
12992      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
12993      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
12994      if( rc ){
12995        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
12996      }else{
12997        utf8_printf(stdout, "%s;\n", zSql);
12998        raw_printf(stdout,
12999           "WARNING: writing to an imposter table will corrupt the index!\n"
13000        );
13001      }
13002    }else{
13003      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
13004      rc = 1;
13005    }
13006    sqlite3_free(zSql);
13007  }else
13008#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
13009
13010#ifdef SQLITE_ENABLE_IOTRACE
13011  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
13012    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
13013    if( iotrace && iotrace!=stdout ) fclose(iotrace);
13014    iotrace = 0;
13015    if( nArg<2 ){
13016      sqlite3IoTrace = 0;
13017    }else if( strcmp(azArg[1], "-")==0 ){
13018      sqlite3IoTrace = iotracePrintf;
13019      iotrace = stdout;
13020    }else{
13021      iotrace = fopen(azArg[1], "w");
13022      if( iotrace==0 ){
13023        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
13024        sqlite3IoTrace = 0;
13025        rc = 1;
13026      }else{
13027        sqlite3IoTrace = iotracePrintf;
13028      }
13029    }
13030  }else
13031#endif
13032
13033  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
13034    static const struct {
13035       const char *zLimitName;   /* Name of a limit */
13036       int limitCode;            /* Integer code for that limit */
13037    } aLimit[] = {
13038      { "length",                SQLITE_LIMIT_LENGTH                    },
13039      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
13040      { "column",                SQLITE_LIMIT_COLUMN                    },
13041      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
13042      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
13043      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
13044      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
13045      { "attached",              SQLITE_LIMIT_ATTACHED                  },
13046      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
13047      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
13048      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
13049      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
13050    };
13051    int i, n2;
13052    open_db(p, 0);
13053    if( nArg==1 ){
13054      for(i=0; i<ArraySize(aLimit); i++){
13055        printf("%20s %d\n", aLimit[i].zLimitName,
13056               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
13057      }
13058    }else if( nArg>3 ){
13059      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
13060      rc = 1;
13061      goto meta_command_exit;
13062    }else{
13063      int iLimit = -1;
13064      n2 = strlen30(azArg[1]);
13065      for(i=0; i<ArraySize(aLimit); i++){
13066        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
13067          if( iLimit<0 ){
13068            iLimit = i;
13069          }else{
13070            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
13071            rc = 1;
13072            goto meta_command_exit;
13073          }
13074        }
13075      }
13076      if( iLimit<0 ){
13077        utf8_printf(stderr, "unknown limit: \"%s\"\n"
13078                        "enter \".limits\" with no arguments for a list.\n",
13079                         azArg[1]);
13080        rc = 1;
13081        goto meta_command_exit;
13082      }
13083      if( nArg==3 ){
13084        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
13085                      (int)integerValue(azArg[2]));
13086      }
13087      printf("%20s %d\n", aLimit[iLimit].zLimitName,
13088             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
13089    }
13090  }else
13091
13092  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
13093    open_db(p, 0);
13094    lintDotCommand(p, azArg, nArg);
13095  }else
13096
13097#ifndef SQLITE_OMIT_LOAD_EXTENSION
13098  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
13099    const char *zFile, *zProc;
13100    char *zErrMsg = 0;
13101    if( nArg<2 ){
13102      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
13103      rc = 1;
13104      goto meta_command_exit;
13105    }
13106    zFile = azArg[1];
13107    zProc = nArg>=3 ? azArg[2] : 0;
13108    open_db(p, 0);
13109    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
13110    if( rc!=SQLITE_OK ){
13111      utf8_printf(stderr, "Error: %s\n", zErrMsg);
13112      sqlite3_free(zErrMsg);
13113      rc = 1;
13114    }
13115  }else
13116#endif
13117
13118  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
13119    if( nArg!=2 ){
13120      raw_printf(stderr, "Usage: .log FILENAME\n");
13121      rc = 1;
13122    }else{
13123      const char *zFile = azArg[1];
13124      output_file_close(p->pLog);
13125      p->pLog = output_file_open(zFile, 0);
13126    }
13127  }else
13128
13129  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
13130    const char *zMode = nArg>=2 ? azArg[1] : "";
13131    int n2 = strlen30(zMode);
13132    int c2 = zMode[0];
13133    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
13134      p->mode = MODE_Line;
13135      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13136    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
13137      p->mode = MODE_Column;
13138      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13139    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
13140      p->mode = MODE_List;
13141      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
13142      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13143    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
13144      p->mode = MODE_Html;
13145    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
13146      p->mode = MODE_Tcl;
13147      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
13148      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13149    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
13150      p->mode = MODE_Csv;
13151      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13152      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13153    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
13154      p->mode = MODE_List;
13155      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
13156    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
13157      p->mode = MODE_Insert;
13158      set_table_name(p, nArg>=3 ? azArg[2] : "table");
13159    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
13160      p->mode = MODE_Quote;
13161    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
13162      p->mode = MODE_Ascii;
13163      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
13164      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
13165    }else if( nArg==1 ){
13166      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
13167    }else{
13168      raw_printf(stderr, "Error: mode should be one of: "
13169         "ascii column csv html insert line list quote tabs tcl\n");
13170      rc = 1;
13171    }
13172    p->cMode = p->mode;
13173  }else
13174
13175  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
13176    if( nArg==2 ){
13177      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
13178                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
13179    }else{
13180      raw_printf(stderr, "Usage: .nullvalue STRING\n");
13181      rc = 1;
13182    }
13183  }else
13184
13185  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
13186    char *zNewFilename;  /* Name of the database file to open */
13187    int iName = 1;       /* Index in azArg[] of the filename */
13188    int newFlag = 0;     /* True to delete file before opening */
13189    /* Close the existing database */
13190    session_close_all(p);
13191    sqlite3_close(p->db);
13192    p->db = 0;
13193    p->zDbFilename = 0;
13194    sqlite3_free(p->zFreeOnClose);
13195    p->zFreeOnClose = 0;
13196    p->openMode = SHELL_OPEN_UNSPEC;
13197    /* Check for command-line arguments */
13198    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
13199      const char *z = azArg[iName];
13200      if( optionMatch(z,"new") ){
13201        newFlag = 1;
13202#ifdef SQLITE_HAVE_ZIP
13203      }else if( optionMatch(z, "zip") ){
13204        p->openMode = SHELL_OPEN_ZIPFILE;
13205#endif
13206      }else if( optionMatch(z, "append") ){
13207        p->openMode = SHELL_OPEN_APPENDVFS;
13208      }else if( z[0]=='-' ){
13209        utf8_printf(stderr, "unknown option: %s\n", z);
13210        rc = 1;
13211        goto meta_command_exit;
13212      }
13213    }
13214    /* If a filename is specified, try to open it first */
13215    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
13216    if( zNewFilename ){
13217      if( newFlag ) shellDeleteFile(zNewFilename);
13218      p->zDbFilename = zNewFilename;
13219      open_db(p, 1);
13220      if( p->db==0 ){
13221        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
13222        sqlite3_free(zNewFilename);
13223      }else{
13224        p->zFreeOnClose = zNewFilename;
13225      }
13226    }
13227    if( p->db==0 ){
13228      /* As a fall-back open a TEMP database */
13229      p->zDbFilename = 0;
13230      open_db(p, 0);
13231    }
13232  }else
13233
13234  if( (c=='o'
13235        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
13236   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
13237  ){
13238    const char *zFile = nArg>=2 ? azArg[1] : "stdout";
13239    int bTxtMode = 0;
13240    if( azArg[0][0]=='e' ){
13241      /* Transform the ".excel" command into ".once -x" */
13242      nArg = 2;
13243      azArg[0] = "once";
13244      zFile = azArg[1] = "-x";
13245      n = 4;
13246    }
13247    if( nArg>2 ){
13248      utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
13249      rc = 1;
13250      goto meta_command_exit;
13251    }
13252    if( n>1 && strncmp(azArg[0], "once", n)==0 ){
13253      if( nArg<2 ){
13254        raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
13255        rc = 1;
13256        goto meta_command_exit;
13257      }
13258      p->outCount = 2;
13259    }else{
13260      p->outCount = 0;
13261    }
13262    output_reset(p);
13263    if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
13264    if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
13265      p->doXdgOpen = 1;
13266      outputModePush(p);
13267      if( zFile[1]=='x' ){
13268        newTempFile(p, "csv");
13269        p->mode = MODE_Csv;
13270        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13271        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13272      }else{
13273        newTempFile(p, "txt");
13274        bTxtMode = 1;
13275      }
13276      zFile = p->zTempFile;
13277    }
13278    if( zFile[0]=='|' ){
13279#ifdef SQLITE_OMIT_POPEN
13280      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13281      rc = 1;
13282      p->out = stdout;
13283#else
13284      p->out = popen(zFile + 1, "w");
13285      if( p->out==0 ){
13286        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
13287        p->out = stdout;
13288        rc = 1;
13289      }else{
13290        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
13291      }
13292#endif
13293    }else{
13294      p->out = output_file_open(zFile, bTxtMode);
13295      if( p->out==0 ){
13296        if( strcmp(zFile,"off")!=0 ){
13297          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
13298        }
13299        p->out = stdout;
13300        rc = 1;
13301      } else {
13302        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
13303      }
13304    }
13305  }else
13306
13307  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
13308    int i;
13309    for(i=1; i<nArg; i++){
13310      if( i>1 ) raw_printf(p->out, " ");
13311      utf8_printf(p->out, "%s", azArg[i]);
13312    }
13313    raw_printf(p->out, "\n");
13314  }else
13315
13316  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
13317    if( nArg >= 2) {
13318      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
13319    }
13320    if( nArg >= 3) {
13321      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
13322    }
13323  }else
13324
13325  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
13326    rc = 2;
13327  }else
13328
13329  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
13330    FILE *alt;
13331    if( nArg!=2 ){
13332      raw_printf(stderr, "Usage: .read FILE\n");
13333      rc = 1;
13334      goto meta_command_exit;
13335    }
13336    alt = fopen(azArg[1], "rb");
13337    if( alt==0 ){
13338      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
13339      rc = 1;
13340    }else{
13341      rc = process_input(p, alt);
13342      fclose(alt);
13343    }
13344  }else
13345
13346  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
13347    const char *zSrcFile;
13348    const char *zDb;
13349    sqlite3 *pSrc;
13350    sqlite3_backup *pBackup;
13351    int nTimeout = 0;
13352
13353    if( nArg==2 ){
13354      zSrcFile = azArg[1];
13355      zDb = "main";
13356    }else if( nArg==3 ){
13357      zSrcFile = azArg[2];
13358      zDb = azArg[1];
13359    }else{
13360      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
13361      rc = 1;
13362      goto meta_command_exit;
13363    }
13364    rc = sqlite3_open(zSrcFile, &pSrc);
13365    if( rc!=SQLITE_OK ){
13366      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
13367      sqlite3_close(pSrc);
13368      return 1;
13369    }
13370    open_db(p, 0);
13371    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
13372    if( pBackup==0 ){
13373      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13374      sqlite3_close(pSrc);
13375      return 1;
13376    }
13377    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
13378          || rc==SQLITE_BUSY  ){
13379      if( rc==SQLITE_BUSY ){
13380        if( nTimeout++ >= 3 ) break;
13381        sqlite3_sleep(100);
13382      }
13383    }
13384    sqlite3_backup_finish(pBackup);
13385    if( rc==SQLITE_DONE ){
13386      rc = 0;
13387    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
13388      raw_printf(stderr, "Error: source database is busy\n");
13389      rc = 1;
13390    }else{
13391      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13392      rc = 1;
13393    }
13394    sqlite3_close(pSrc);
13395  }else
13396
13397
13398  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
13399    if( nArg==2 ){
13400      p->scanstatsOn = booleanValue(azArg[1]);
13401#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13402      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
13403#endif
13404    }else{
13405      raw_printf(stderr, "Usage: .scanstats on|off\n");
13406      rc = 1;
13407    }
13408  }else
13409
13410  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
13411    ShellText sSelect;
13412    ShellState data;
13413    char *zErrMsg = 0;
13414    const char *zDiv = "(";
13415    const char *zName = 0;
13416    int iSchema = 0;
13417    int bDebug = 0;
13418    int ii;
13419
13420    open_db(p, 0);
13421    memcpy(&data, p, sizeof(data));
13422    data.showHeader = 0;
13423    data.cMode = data.mode = MODE_Semi;
13424    initText(&sSelect);
13425    for(ii=1; ii<nArg; ii++){
13426      if( optionMatch(azArg[ii],"indent") ){
13427        data.cMode = data.mode = MODE_Pretty;
13428      }else if( optionMatch(azArg[ii],"debug") ){
13429        bDebug = 1;
13430      }else if( zName==0 ){
13431        zName = azArg[ii];
13432      }else{
13433        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
13434        rc = 1;
13435        goto meta_command_exit;
13436      }
13437    }
13438    if( zName!=0 ){
13439      int isMaster = sqlite3_strlike(zName, "sqlite_master", 0)==0;
13440      if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master",0)==0 ){
13441        char *new_argv[2], *new_colv[2];
13442        new_argv[0] = sqlite3_mprintf(
13443                      "CREATE TABLE %s (\n"
13444                      "  type text,\n"
13445                      "  name text,\n"
13446                      "  tbl_name text,\n"
13447                      "  rootpage integer,\n"
13448                      "  sql text\n"
13449                      ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
13450        new_argv[1] = 0;
13451        new_colv[0] = "sql";
13452        new_colv[1] = 0;
13453        callback(&data, 1, new_argv, new_colv);
13454        sqlite3_free(new_argv[0]);
13455      }
13456    }
13457    if( zDiv ){
13458      sqlite3_stmt *pStmt = 0;
13459      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
13460                              -1, &pStmt, 0);
13461      if( rc ){
13462        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13463        sqlite3_finalize(pStmt);
13464        rc = 1;
13465        goto meta_command_exit;
13466      }
13467      appendText(&sSelect, "SELECT sql FROM", 0);
13468      iSchema = 0;
13469      while( sqlite3_step(pStmt)==SQLITE_ROW ){
13470        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
13471        char zScNum[30];
13472        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
13473        appendText(&sSelect, zDiv, 0);
13474        zDiv = " UNION ALL ";
13475        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
13476        if( sqlite3_stricmp(zDb, "main")!=0 ){
13477          appendText(&sSelect, zDb, '"');
13478        }else{
13479          appendText(&sSelect, "NULL", 0);
13480        }
13481        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
13482        appendText(&sSelect, zScNum, 0);
13483        appendText(&sSelect, " AS snum, ", 0);
13484        appendText(&sSelect, zDb, '\'');
13485        appendText(&sSelect, " AS sname FROM ", 0);
13486        appendText(&sSelect, zDb, '"');
13487        appendText(&sSelect, ".sqlite_master", 0);
13488      }
13489      sqlite3_finalize(pStmt);
13490#ifdef SQLITE_INTROSPECTION_PRAGMAS
13491      if( zName ){
13492        appendText(&sSelect,
13493           " UNION ALL SELECT shell_module_schema(name),"
13494           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
13495      }
13496#endif
13497      appendText(&sSelect, ") WHERE ", 0);
13498      if( zName ){
13499        char *zQarg = sqlite3_mprintf("%Q", zName);
13500        if( strchr(zName, '.') ){
13501          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
13502        }else{
13503          appendText(&sSelect, "lower(tbl_name)", 0);
13504        }
13505        appendText(&sSelect, strchr(zName, '*') ? " GLOB " : " LIKE ", 0);
13506        appendText(&sSelect, zQarg, 0);
13507        appendText(&sSelect, " AND ", 0);
13508        sqlite3_free(zQarg);
13509      }
13510      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
13511                           " ORDER BY snum, rowid", 0);
13512      if( bDebug ){
13513        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
13514      }else{
13515        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
13516      }
13517      freeText(&sSelect);
13518    }
13519    if( zErrMsg ){
13520      utf8_printf(stderr,"Error: %s\n", zErrMsg);
13521      sqlite3_free(zErrMsg);
13522      rc = 1;
13523    }else if( rc != SQLITE_OK ){
13524      raw_printf(stderr,"Error: querying schema information\n");
13525      rc = 1;
13526    }else{
13527      rc = 0;
13528    }
13529  }else
13530
13531#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
13532  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
13533    sqlite3SelectTrace = (int)integerValue(azArg[1]);
13534  }else
13535#endif
13536
13537#if defined(SQLITE_ENABLE_SESSION)
13538  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
13539    OpenSession *pSession = &p->aSession[0];
13540    char **azCmd = &azArg[1];
13541    int iSes = 0;
13542    int nCmd = nArg - 1;
13543    int i;
13544    if( nArg<=1 ) goto session_syntax_error;
13545    open_db(p, 0);
13546    if( nArg>=3 ){
13547      for(iSes=0; iSes<p->nSession; iSes++){
13548        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
13549      }
13550      if( iSes<p->nSession ){
13551        pSession = &p->aSession[iSes];
13552        azCmd++;
13553        nCmd--;
13554      }else{
13555        pSession = &p->aSession[0];
13556        iSes = 0;
13557      }
13558    }
13559
13560    /* .session attach TABLE
13561    ** Invoke the sqlite3session_attach() interface to attach a particular
13562    ** table so that it is never filtered.
13563    */
13564    if( strcmp(azCmd[0],"attach")==0 ){
13565      if( nCmd!=2 ) goto session_syntax_error;
13566      if( pSession->p==0 ){
13567        session_not_open:
13568        raw_printf(stderr, "ERROR: No sessions are open\n");
13569      }else{
13570        rc = sqlite3session_attach(pSession->p, azCmd[1]);
13571        if( rc ){
13572          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
13573          rc = 0;
13574        }
13575      }
13576    }else
13577
13578    /* .session changeset FILE
13579    ** .session patchset FILE
13580    ** Write a changeset or patchset into a file.  The file is overwritten.
13581    */
13582    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
13583      FILE *out = 0;
13584      if( nCmd!=2 ) goto session_syntax_error;
13585      if( pSession->p==0 ) goto session_not_open;
13586      out = fopen(azCmd[1], "wb");
13587      if( out==0 ){
13588        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
13589      }else{
13590        int szChng;
13591        void *pChng;
13592        if( azCmd[0][0]=='c' ){
13593          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
13594        }else{
13595          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
13596        }
13597        if( rc ){
13598          printf("Error: error code %d\n", rc);
13599          rc = 0;
13600        }
13601        if( pChng
13602          && fwrite(pChng, szChng, 1, out)!=1 ){
13603          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
13604                  szChng);
13605        }
13606        sqlite3_free(pChng);
13607        fclose(out);
13608      }
13609    }else
13610
13611    /* .session close
13612    ** Close the identified session
13613    */
13614    if( strcmp(azCmd[0], "close")==0 ){
13615      if( nCmd!=1 ) goto session_syntax_error;
13616      if( p->nSession ){
13617        session_close(pSession);
13618        p->aSession[iSes] = p->aSession[--p->nSession];
13619      }
13620    }else
13621
13622    /* .session enable ?BOOLEAN?
13623    ** Query or set the enable flag
13624    */
13625    if( strcmp(azCmd[0], "enable")==0 ){
13626      int ii;
13627      if( nCmd>2 ) goto session_syntax_error;
13628      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
13629      if( p->nSession ){
13630        ii = sqlite3session_enable(pSession->p, ii);
13631        utf8_printf(p->out, "session %s enable flag = %d\n",
13632                    pSession->zName, ii);
13633      }
13634    }else
13635
13636    /* .session filter GLOB ....
13637    ** Set a list of GLOB patterns of table names to be excluded.
13638    */
13639    if( strcmp(azCmd[0], "filter")==0 ){
13640      int ii, nByte;
13641      if( nCmd<2 ) goto session_syntax_error;
13642      if( p->nSession ){
13643        for(ii=0; ii<pSession->nFilter; ii++){
13644          sqlite3_free(pSession->azFilter[ii]);
13645        }
13646        sqlite3_free(pSession->azFilter);
13647        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
13648        pSession->azFilter = sqlite3_malloc( nByte );
13649        if( pSession->azFilter==0 ){
13650          raw_printf(stderr, "Error: out or memory\n");
13651          exit(1);
13652        }
13653        for(ii=1; ii<nCmd; ii++){
13654          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
13655        }
13656        pSession->nFilter = ii-1;
13657      }
13658    }else
13659
13660    /* .session indirect ?BOOLEAN?
13661    ** Query or set the indirect flag
13662    */
13663    if( strcmp(azCmd[0], "indirect")==0 ){
13664      int ii;
13665      if( nCmd>2 ) goto session_syntax_error;
13666      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
13667      if( p->nSession ){
13668        ii = sqlite3session_indirect(pSession->p, ii);
13669        utf8_printf(p->out, "session %s indirect flag = %d\n",
13670                    pSession->zName, ii);
13671      }
13672    }else
13673
13674    /* .session isempty
13675    ** Determine if the session is empty
13676    */
13677    if( strcmp(azCmd[0], "isempty")==0 ){
13678      int ii;
13679      if( nCmd!=1 ) goto session_syntax_error;
13680      if( p->nSession ){
13681        ii = sqlite3session_isempty(pSession->p);
13682        utf8_printf(p->out, "session %s isempty flag = %d\n",
13683                    pSession->zName, ii);
13684      }
13685    }else
13686
13687    /* .session list
13688    ** List all currently open sessions
13689    */
13690    if( strcmp(azCmd[0],"list")==0 ){
13691      for(i=0; i<p->nSession; i++){
13692        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
13693      }
13694    }else
13695
13696    /* .session open DB NAME
13697    ** Open a new session called NAME on the attached database DB.
13698    ** DB is normally "main".
13699    */
13700    if( strcmp(azCmd[0],"open")==0 ){
13701      char *zName;
13702      if( nCmd!=3 ) goto session_syntax_error;
13703      zName = azCmd[2];
13704      if( zName[0]==0 ) goto session_syntax_error;
13705      for(i=0; i<p->nSession; i++){
13706        if( strcmp(p->aSession[i].zName,zName)==0 ){
13707          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
13708          goto meta_command_exit;
13709        }
13710      }
13711      if( p->nSession>=ArraySize(p->aSession) ){
13712        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
13713        goto meta_command_exit;
13714      }
13715      pSession = &p->aSession[p->nSession];
13716      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
13717      if( rc ){
13718        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
13719        rc = 0;
13720        goto meta_command_exit;
13721      }
13722      pSession->nFilter = 0;
13723      sqlite3session_table_filter(pSession->p, session_filter, pSession);
13724      p->nSession++;
13725      pSession->zName = sqlite3_mprintf("%s", zName);
13726    }else
13727    /* If no command name matches, show a syntax error */
13728    session_syntax_error:
13729    session_help(p);
13730  }else
13731#endif
13732
13733#ifdef SQLITE_DEBUG
13734  /* Undocumented commands for internal testing.  Subject to change
13735  ** without notice. */
13736  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
13737    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
13738      int i, v;
13739      for(i=1; i<nArg; i++){
13740        v = booleanValue(azArg[i]);
13741        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
13742      }
13743    }
13744    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
13745      int i; sqlite3_int64 v;
13746      for(i=1; i<nArg; i++){
13747        char zBuf[200];
13748        v = integerValue(azArg[i]);
13749        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
13750        utf8_printf(p->out, "%s", zBuf);
13751      }
13752    }
13753  }else
13754#endif
13755
13756  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
13757    int bIsInit = 0;         /* True to initialize the SELFTEST table */
13758    int bVerbose = 0;        /* Verbose output */
13759    int bSelftestExists;     /* True if SELFTEST already exists */
13760    int i, k;                /* Loop counters */
13761    int nTest = 0;           /* Number of tests runs */
13762    int nErr = 0;            /* Number of errors seen */
13763    ShellText str;           /* Answer for a query */
13764    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
13765
13766    open_db(p,0);
13767    for(i=1; i<nArg; i++){
13768      const char *z = azArg[i];
13769      if( z[0]=='-' && z[1]=='-' ) z++;
13770      if( strcmp(z,"-init")==0 ){
13771        bIsInit = 1;
13772      }else
13773      if( strcmp(z,"-v")==0 ){
13774        bVerbose++;
13775      }else
13776      {
13777        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
13778                    azArg[i], azArg[0]);
13779        raw_printf(stderr, "Should be one of: --init -v\n");
13780        rc = 1;
13781        goto meta_command_exit;
13782      }
13783    }
13784    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
13785           != SQLITE_OK ){
13786      bSelftestExists = 0;
13787    }else{
13788      bSelftestExists = 1;
13789    }
13790    if( bIsInit ){
13791      createSelftestTable(p);
13792      bSelftestExists = 1;
13793    }
13794    initText(&str);
13795    appendText(&str, "x", 0);
13796    for(k=bSelftestExists; k>=0; k--){
13797      if( k==1 ){
13798        rc = sqlite3_prepare_v2(p->db,
13799            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
13800            -1, &pStmt, 0);
13801      }else{
13802        rc = sqlite3_prepare_v2(p->db,
13803          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
13804          "      (1,'run','PRAGMA integrity_check','ok')",
13805          -1, &pStmt, 0);
13806      }
13807      if( rc ){
13808        raw_printf(stderr, "Error querying the selftest table\n");
13809        rc = 1;
13810        sqlite3_finalize(pStmt);
13811        goto meta_command_exit;
13812      }
13813      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
13814        int tno = sqlite3_column_int(pStmt, 0);
13815        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
13816        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
13817        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
13818
13819        k = 0;
13820        if( bVerbose>0 ){
13821          char *zQuote = sqlite3_mprintf("%q", zSql);
13822          printf("%d: %s %s\n", tno, zOp, zSql);
13823          sqlite3_free(zQuote);
13824        }
13825        if( strcmp(zOp,"memo")==0 ){
13826          utf8_printf(p->out, "%s\n", zSql);
13827        }else
13828        if( strcmp(zOp,"run")==0 ){
13829          char *zErrMsg = 0;
13830          str.n = 0;
13831          str.z[0] = 0;
13832          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
13833          nTest++;
13834          if( bVerbose ){
13835            utf8_printf(p->out, "Result: %s\n", str.z);
13836          }
13837          if( rc || zErrMsg ){
13838            nErr++;
13839            rc = 1;
13840            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
13841            sqlite3_free(zErrMsg);
13842          }else if( strcmp(zAns,str.z)!=0 ){
13843            nErr++;
13844            rc = 1;
13845            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
13846            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
13847          }
13848        }else
13849        {
13850          utf8_printf(stderr,
13851            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
13852          rc = 1;
13853          break;
13854        }
13855      } /* End loop over rows of content from SELFTEST */
13856      sqlite3_finalize(pStmt);
13857    } /* End loop over k */
13858    freeText(&str);
13859    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
13860  }else
13861
13862  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
13863    if( nArg<2 || nArg>3 ){
13864      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
13865      rc = 1;
13866    }
13867    if( nArg>=2 ){
13868      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
13869                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
13870    }
13871    if( nArg>=3 ){
13872      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
13873                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
13874    }
13875  }else
13876
13877  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
13878    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
13879    int i;                   /* Loop counter */
13880    int bSchema = 0;         /* Also hash the schema */
13881    int bSeparate = 0;       /* Hash each table separately */
13882    int iSize = 224;         /* Hash algorithm to use */
13883    int bDebug = 0;          /* Only show the query that would have run */
13884    sqlite3_stmt *pStmt;     /* For querying tables names */
13885    char *zSql;              /* SQL to be run */
13886    char *zSep;              /* Separator */
13887    ShellText sSql;          /* Complete SQL for the query to run the hash */
13888    ShellText sQuery;        /* Set of queries used to read all content */
13889    open_db(p, 0);
13890    for(i=1; i<nArg; i++){
13891      const char *z = azArg[i];
13892      if( z[0]=='-' ){
13893        z++;
13894        if( z[0]=='-' ) z++;
13895        if( strcmp(z,"schema")==0 ){
13896          bSchema = 1;
13897        }else
13898        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
13899         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
13900        ){
13901          iSize = atoi(&z[5]);
13902        }else
13903        if( strcmp(z,"debug")==0 ){
13904          bDebug = 1;
13905        }else
13906        {
13907          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
13908                      azArg[i], azArg[0]);
13909          raw_printf(stderr, "Should be one of: --schema"
13910                             " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
13911          rc = 1;
13912          goto meta_command_exit;
13913        }
13914      }else if( zLike ){
13915        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
13916        rc = 1;
13917        goto meta_command_exit;
13918      }else{
13919        zLike = z;
13920        bSeparate = 1;
13921        if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
13922      }
13923    }
13924    if( bSchema ){
13925      zSql = "SELECT lower(name) FROM sqlite_master"
13926             " WHERE type='table' AND coalesce(rootpage,0)>1"
13927             " UNION ALL SELECT 'sqlite_master'"
13928             " ORDER BY 1 collate nocase";
13929    }else{
13930      zSql = "SELECT lower(name) FROM sqlite_master"
13931             " WHERE type='table' AND coalesce(rootpage,0)>1"
13932             " AND name NOT LIKE 'sqlite_%'"
13933             " ORDER BY 1 collate nocase";
13934    }
13935    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13936    initText(&sQuery);
13937    initText(&sSql);
13938    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
13939    zSep = "VALUES(";
13940    while( SQLITE_ROW==sqlite3_step(pStmt) ){
13941      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
13942      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
13943      if( strncmp(zTab, "sqlite_",7)!=0 ){
13944        appendText(&sQuery,"SELECT * FROM ", 0);
13945        appendText(&sQuery,zTab,'"');
13946        appendText(&sQuery," NOT INDEXED;", 0);
13947      }else if( strcmp(zTab, "sqlite_master")==0 ){
13948        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
13949                           " ORDER BY name;", 0);
13950      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
13951        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
13952                           " ORDER BY name;", 0);
13953      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
13954        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
13955                           " ORDER BY tbl,idx;", 0);
13956      }else if( strcmp(zTab, "sqlite_stat3")==0
13957             || strcmp(zTab, "sqlite_stat4")==0 ){
13958        appendText(&sQuery, "SELECT * FROM ", 0);
13959        appendText(&sQuery, zTab, 0);
13960        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
13961      }
13962      appendText(&sSql, zSep, 0);
13963      appendText(&sSql, sQuery.z, '\'');
13964      sQuery.n = 0;
13965      appendText(&sSql, ",", 0);
13966      appendText(&sSql, zTab, '\'');
13967      zSep = "),(";
13968    }
13969    sqlite3_finalize(pStmt);
13970    if( bSeparate ){
13971      zSql = sqlite3_mprintf(
13972          "%s))"
13973          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
13974          "   FROM [sha3sum$query]",
13975          sSql.z, iSize);
13976    }else{
13977      zSql = sqlite3_mprintf(
13978          "%s))"
13979          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
13980          "   FROM [sha3sum$query]",
13981          sSql.z, iSize);
13982    }
13983    freeText(&sQuery);
13984    freeText(&sSql);
13985    if( bDebug ){
13986      utf8_printf(p->out, "%s\n", zSql);
13987    }else{
13988      shell_exec(p->db, zSql, shell_callback, p, 0);
13989    }
13990    sqlite3_free(zSql);
13991  }else
13992
13993  if( c=='s'
13994   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
13995  ){
13996    char *zCmd;
13997    int i, x;
13998    if( nArg<2 ){
13999      raw_printf(stderr, "Usage: .system COMMAND\n");
14000      rc = 1;
14001      goto meta_command_exit;
14002    }
14003    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
14004    for(i=2; i<nArg; i++){
14005      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
14006                             zCmd, azArg[i]);
14007    }
14008    x = system(zCmd);
14009    sqlite3_free(zCmd);
14010    if( x ) raw_printf(stderr, "System command returns %d\n", x);
14011  }else
14012
14013  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
14014    static const char *azBool[] = { "off", "on", "trigger", "full"};
14015    int i;
14016    if( nArg!=1 ){
14017      raw_printf(stderr, "Usage: .show\n");
14018      rc = 1;
14019      goto meta_command_exit;
14020    }
14021    utf8_printf(p->out, "%12.12s: %s\n","echo",
14022                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
14023    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
14024    utf8_printf(p->out, "%12.12s: %s\n","explain",
14025         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
14026    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
14027    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
14028    utf8_printf(p->out, "%12.12s: ", "nullvalue");
14029      output_c_string(p->out, p->nullValue);
14030      raw_printf(p->out, "\n");
14031    utf8_printf(p->out,"%12.12s: %s\n","output",
14032            strlen30(p->outfile) ? p->outfile : "stdout");
14033    utf8_printf(p->out,"%12.12s: ", "colseparator");
14034      output_c_string(p->out, p->colSeparator);
14035      raw_printf(p->out, "\n");
14036    utf8_printf(p->out,"%12.12s: ", "rowseparator");
14037      output_c_string(p->out, p->rowSeparator);
14038      raw_printf(p->out, "\n");
14039    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
14040    utf8_printf(p->out, "%12.12s: ", "width");
14041    for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
14042      raw_printf(p->out, "%d ", p->colWidth[i]);
14043    }
14044    raw_printf(p->out, "\n");
14045    utf8_printf(p->out, "%12.12s: %s\n", "filename",
14046                p->zDbFilename ? p->zDbFilename : "");
14047  }else
14048
14049  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
14050    if( nArg==2 ){
14051      p->statsOn = booleanValue(azArg[1]);
14052    }else if( nArg==1 ){
14053      display_stats(p->db, p, 0);
14054    }else{
14055      raw_printf(stderr, "Usage: .stats ?on|off?\n");
14056      rc = 1;
14057    }
14058  }else
14059
14060  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
14061   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
14062                 || strncmp(azArg[0], "indexes", n)==0) )
14063  ){
14064    sqlite3_stmt *pStmt;
14065    char **azResult;
14066    int nRow, nAlloc;
14067    int ii;
14068    ShellText s;
14069    initText(&s);
14070    open_db(p, 0);
14071    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
14072    if( rc ) return shellDatabaseError(p->db);
14073
14074    if( nArg>2 && c=='i' ){
14075      /* It is an historical accident that the .indexes command shows an error
14076      ** when called with the wrong number of arguments whereas the .tables
14077      ** command does not. */
14078      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
14079      rc = 1;
14080      goto meta_command_exit;
14081    }
14082    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
14083      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
14084      if( zDbName==0 ) continue;
14085      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
14086      if( sqlite3_stricmp(zDbName, "main")==0 ){
14087        appendText(&s, "SELECT name FROM ", 0);
14088      }else{
14089        appendText(&s, "SELECT ", 0);
14090        appendText(&s, zDbName, '\'');
14091        appendText(&s, "||'.'||name FROM ", 0);
14092      }
14093      appendText(&s, zDbName, '"');
14094      appendText(&s, ".sqlite_master ", 0);
14095      if( c=='t' ){
14096        appendText(&s," WHERE type IN ('table','view')"
14097                      "   AND name NOT LIKE 'sqlite_%'"
14098                      "   AND name LIKE ?1", 0);
14099      }else{
14100        appendText(&s," WHERE type='index'"
14101                      "   AND tbl_name LIKE ?1", 0);
14102      }
14103    }
14104    rc = sqlite3_finalize(pStmt);
14105    appendText(&s, " ORDER BY 1", 0);
14106    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
14107    freeText(&s);
14108    if( rc ) return shellDatabaseError(p->db);
14109
14110    /* Run the SQL statement prepared by the above block. Store the results
14111    ** as an array of nul-terminated strings in azResult[].  */
14112    nRow = nAlloc = 0;
14113    azResult = 0;
14114    if( nArg>1 ){
14115      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
14116    }else{
14117      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
14118    }
14119    while( sqlite3_step(pStmt)==SQLITE_ROW ){
14120      if( nRow>=nAlloc ){
14121        char **azNew;
14122        int n2 = nAlloc*2 + 10;
14123        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
14124        if( azNew==0 ){
14125          rc = shellNomemError();
14126          break;
14127        }
14128        nAlloc = n2;
14129        azResult = azNew;
14130      }
14131      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
14132      if( 0==azResult[nRow] ){
14133        rc = shellNomemError();
14134        break;
14135      }
14136      nRow++;
14137    }
14138    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
14139      rc = shellDatabaseError(p->db);
14140    }
14141
14142    /* Pretty-print the contents of array azResult[] to the output */
14143    if( rc==0 && nRow>0 ){
14144      int len, maxlen = 0;
14145      int i, j;
14146      int nPrintCol, nPrintRow;
14147      for(i=0; i<nRow; i++){
14148        len = strlen30(azResult[i]);
14149        if( len>maxlen ) maxlen = len;
14150      }
14151      nPrintCol = 80/(maxlen+2);
14152      if( nPrintCol<1 ) nPrintCol = 1;
14153      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
14154      for(i=0; i<nPrintRow; i++){
14155        for(j=i; j<nRow; j+=nPrintRow){
14156          char *zSp = j<nPrintRow ? "" : "  ";
14157          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
14158                      azResult[j] ? azResult[j]:"");
14159        }
14160        raw_printf(p->out, "\n");
14161      }
14162    }
14163
14164    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
14165    sqlite3_free(azResult);
14166  }else
14167
14168  /* Begin redirecting output to the file "testcase-out.txt" */
14169  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
14170    output_reset(p);
14171    p->out = output_file_open("testcase-out.txt", 0);
14172    if( p->out==0 ){
14173      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
14174    }
14175    if( nArg>=2 ){
14176      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
14177    }else{
14178      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
14179    }
14180  }else
14181
14182#ifndef SQLITE_UNTESTABLE
14183  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
14184    static const struct {
14185       const char *zCtrlName;   /* Name of a test-control option */
14186       int ctrlCode;            /* Integer code for that option */
14187       const char *zUsage;      /* Usage notes */
14188    } aCtrl[] = {
14189      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
14190      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
14191    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
14192    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
14193      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
14194    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
14195      { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
14196#ifdef SQLITE_N_KEYWORD
14197      { "iskeyword",          SQLITE_TESTCTRL_ISKEYWORD,     "IDENTIFIER"         },
14198#endif
14199      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
14200      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
14201      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
14202#ifdef YYCOVERAGE
14203      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
14204#endif
14205      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
14206      { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
14207      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
14208      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
14209      { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
14210    };
14211    int testctrl = -1;
14212    int iCtrl = -1;
14213    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
14214    int isOk = 0;
14215    int i, n2;
14216    const char *zCmd = 0;
14217
14218    open_db(p, 0);
14219    zCmd = nArg>=2 ? azArg[1] : "help";
14220
14221    /* The argument can optionally begin with "-" or "--" */
14222    if( zCmd[0]=='-' && zCmd[1] ){
14223      zCmd++;
14224      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
14225    }
14226
14227    /* --help lists all test-controls */
14228    if( strcmp(zCmd,"help")==0 ){
14229      utf8_printf(p->out, "Available test-controls:\n");
14230      for(i=0; i<ArraySize(aCtrl); i++){
14231        utf8_printf(p->out, "  .testctrl %s %s\n",
14232                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
14233      }
14234      rc = 1;
14235      goto meta_command_exit;
14236    }
14237
14238    /* convert testctrl text option to value. allow any unique prefix
14239    ** of the option name, or a numerical value. */
14240    n2 = strlen30(zCmd);
14241    for(i=0; i<ArraySize(aCtrl); i++){
14242      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
14243        if( testctrl<0 ){
14244          testctrl = aCtrl[i].ctrlCode;
14245          iCtrl = i;
14246        }else{
14247          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
14248                              "Use \".testctrl --help\" for help\n", zCmd);
14249          rc = 1;
14250          goto meta_command_exit;
14251        }
14252      }
14253    }
14254    if( testctrl<0 ){
14255      utf8_printf(stderr,"Error: unknown test-control: %s\n"
14256                         "Use \".testctrl --help\" for help\n", zCmd);
14257    }else{
14258      switch(testctrl){
14259
14260        /* sqlite3_test_control(int, db, int) */
14261        case SQLITE_TESTCTRL_OPTIMIZATIONS:
14262        case SQLITE_TESTCTRL_RESERVE:
14263          if( nArg==3 ){
14264            int opt = (int)strtol(azArg[2], 0, 0);
14265            rc2 = sqlite3_test_control(testctrl, p->db, opt);
14266            isOk = 3;
14267          }
14268          break;
14269
14270        /* sqlite3_test_control(int) */
14271        case SQLITE_TESTCTRL_PRNG_SAVE:
14272        case SQLITE_TESTCTRL_PRNG_RESTORE:
14273        case SQLITE_TESTCTRL_PRNG_RESET:
14274        case SQLITE_TESTCTRL_BYTEORDER:
14275          if( nArg==2 ){
14276            rc2 = sqlite3_test_control(testctrl);
14277            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
14278          }
14279          break;
14280
14281        /* sqlite3_test_control(int, uint) */
14282        case SQLITE_TESTCTRL_PENDING_BYTE:
14283          if( nArg==3 ){
14284            unsigned int opt = (unsigned int)integerValue(azArg[2]);
14285            rc2 = sqlite3_test_control(testctrl, opt);
14286            isOk = 3;
14287          }
14288          break;
14289
14290        /* sqlite3_test_control(int, int) */
14291        case SQLITE_TESTCTRL_ASSERT:
14292        case SQLITE_TESTCTRL_ALWAYS:
14293          if( nArg==3 ){
14294            int opt = booleanValue(azArg[2]);
14295            rc2 = sqlite3_test_control(testctrl, opt);
14296            isOk = 1;
14297          }
14298          break;
14299
14300        /* sqlite3_test_control(int, int) */
14301        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
14302        case SQLITE_TESTCTRL_NEVER_CORRUPT:
14303          if( nArg==3 ){
14304            int opt = booleanValue(azArg[2]);
14305            rc2 = sqlite3_test_control(testctrl, opt);
14306            isOk = 3;
14307          }
14308          break;
14309
14310        /* sqlite3_test_control(int, char *) */
14311#ifdef SQLITE_N_KEYWORD
14312        case SQLITE_TESTCTRL_ISKEYWORD:
14313          if( nArg==3 ){
14314            const char *opt = azArg[2];
14315            rc2 = sqlite3_test_control(testctrl, opt);
14316            isOk = 1;
14317          }
14318          break;
14319#endif
14320
14321        case SQLITE_TESTCTRL_IMPOSTER:
14322          if( nArg==5 ){
14323            rc2 = sqlite3_test_control(testctrl, p->db,
14324                          azArg[2],
14325                          integerValue(azArg[3]),
14326                          integerValue(azArg[4]));
14327            isOk = 3;
14328          }
14329          break;
14330
14331#ifdef YYCOVERAGE
14332        case SQLITE_TESTCTRL_PARSER_COVERAGE:
14333          if( nArg==2 ){
14334            sqlite3_test_control(testctrl, p->out);
14335            isOk = 3;
14336          }
14337#endif
14338      }
14339    }
14340    if( isOk==0 && iCtrl>=0 ){
14341      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
14342      rc = 1;
14343    }else if( isOk==1 ){
14344      raw_printf(p->out, "%d\n", rc2);
14345    }else if( isOk==2 ){
14346      raw_printf(p->out, "0x%08x\n", rc2);
14347    }
14348  }else
14349#endif /* !defined(SQLITE_UNTESTABLE) */
14350
14351  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
14352    open_db(p, 0);
14353    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
14354  }else
14355
14356  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
14357    if( nArg==2 ){
14358      enableTimer = booleanValue(azArg[1]);
14359      if( enableTimer && !HAS_TIMER ){
14360        raw_printf(stderr, "Error: timer not available on this system.\n");
14361        enableTimer = 0;
14362      }
14363    }else{
14364      raw_printf(stderr, "Usage: .timer on|off\n");
14365      rc = 1;
14366    }
14367  }else
14368
14369  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
14370    open_db(p, 0);
14371    if( nArg!=2 ){
14372      raw_printf(stderr, "Usage: .trace FILE|off\n");
14373      rc = 1;
14374      goto meta_command_exit;
14375    }
14376    output_file_close(p->traceOut);
14377    p->traceOut = output_file_open(azArg[1], 0);
14378#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
14379    if( p->traceOut==0 ){
14380      sqlite3_trace_v2(p->db, 0, 0, 0);
14381    }else{
14382      sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
14383    }
14384#endif
14385  }else
14386
14387#if SQLITE_USER_AUTHENTICATION
14388  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
14389    if( nArg<2 ){
14390      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
14391      rc = 1;
14392      goto meta_command_exit;
14393    }
14394    open_db(p, 0);
14395    if( strcmp(azArg[1],"login")==0 ){
14396      if( nArg!=4 ){
14397        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
14398        rc = 1;
14399        goto meta_command_exit;
14400      }
14401      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
14402      if( rc ){
14403        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
14404        rc = 1;
14405      }
14406    }else if( strcmp(azArg[1],"add")==0 ){
14407      if( nArg!=5 ){
14408        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
14409        rc = 1;
14410        goto meta_command_exit;
14411      }
14412      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
14413                            booleanValue(azArg[4]));
14414      if( rc ){
14415        raw_printf(stderr, "User-Add failed: %d\n", rc);
14416        rc = 1;
14417      }
14418    }else if( strcmp(azArg[1],"edit")==0 ){
14419      if( nArg!=5 ){
14420        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
14421        rc = 1;
14422        goto meta_command_exit;
14423      }
14424      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
14425                              booleanValue(azArg[4]));
14426      if( rc ){
14427        raw_printf(stderr, "User-Edit failed: %d\n", rc);
14428        rc = 1;
14429      }
14430    }else if( strcmp(azArg[1],"delete")==0 ){
14431      if( nArg!=3 ){
14432        raw_printf(stderr, "Usage: .user delete USER\n");
14433        rc = 1;
14434        goto meta_command_exit;
14435      }
14436      rc = sqlite3_user_delete(p->db, azArg[2]);
14437      if( rc ){
14438        raw_printf(stderr, "User-Delete failed: %d\n", rc);
14439        rc = 1;
14440      }
14441    }else{
14442      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
14443      rc = 1;
14444      goto meta_command_exit;
14445    }
14446  }else
14447#endif /* SQLITE_USER_AUTHENTICATION */
14448
14449  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
14450    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
14451        sqlite3_libversion(), sqlite3_sourceid());
14452#if SQLITE_HAVE_ZLIB
14453    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
14454#endif
14455#define CTIMEOPT_VAL_(opt) #opt
14456#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
14457#if defined(__clang__) && defined(__clang_major__)
14458    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
14459                    CTIMEOPT_VAL(__clang_minor__) "."
14460                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
14461#elif defined(_MSC_VER)
14462    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
14463#elif defined(__GNUC__) && defined(__VERSION__)
14464    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
14465#endif
14466  }else
14467
14468  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
14469    const char *zDbName = nArg==2 ? azArg[1] : "main";
14470    sqlite3_vfs *pVfs = 0;
14471    if( p->db ){
14472      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
14473      if( pVfs ){
14474        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
14475        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
14476        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
14477        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
14478      }
14479    }
14480  }else
14481
14482  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
14483    sqlite3_vfs *pVfs;
14484    sqlite3_vfs *pCurrent = 0;
14485    if( p->db ){
14486      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
14487    }
14488    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
14489      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
14490           pVfs==pCurrent ? "  <--- CURRENT" : "");
14491      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
14492      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
14493      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
14494      if( pVfs->pNext ){
14495        raw_printf(p->out, "-----------------------------------\n");
14496      }
14497    }
14498  }else
14499
14500  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
14501    const char *zDbName = nArg==2 ? azArg[1] : "main";
14502    char *zVfsName = 0;
14503    if( p->db ){
14504      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
14505      if( zVfsName ){
14506        utf8_printf(p->out, "%s\n", zVfsName);
14507        sqlite3_free(zVfsName);
14508      }
14509    }
14510  }else
14511
14512#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
14513  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
14514    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
14515  }else
14516#endif
14517
14518  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
14519    int j;
14520    assert( nArg<=ArraySize(azArg) );
14521    for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
14522      p->colWidth[j-1] = (int)integerValue(azArg[j]);
14523    }
14524  }else
14525
14526  {
14527    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
14528      " \"%s\". Enter \".help\" for help\n", azArg[0]);
14529    rc = 1;
14530  }
14531
14532meta_command_exit:
14533  if( p->outCount ){
14534    p->outCount--;
14535    if( p->outCount==0 ) output_reset(p);
14536  }
14537  return rc;
14538}
14539
14540/*
14541** Return TRUE if a semicolon occurs anywhere in the first N characters
14542** of string z[].
14543*/
14544static int line_contains_semicolon(const char *z, int N){
14545  int i;
14546  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
14547  return 0;
14548}
14549
14550/*
14551** Test to see if a line consists entirely of whitespace.
14552*/
14553static int _all_whitespace(const char *z){
14554  for(; *z; z++){
14555    if( IsSpace(z[0]) ) continue;
14556    if( *z=='/' && z[1]=='*' ){
14557      z += 2;
14558      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
14559      if( *z==0 ) return 0;
14560      z++;
14561      continue;
14562    }
14563    if( *z=='-' && z[1]=='-' ){
14564      z += 2;
14565      while( *z && *z!='\n' ){ z++; }
14566      if( *z==0 ) return 1;
14567      continue;
14568    }
14569    return 0;
14570  }
14571  return 1;
14572}
14573
14574/*
14575** Return TRUE if the line typed in is an SQL command terminator other
14576** than a semi-colon.  The SQL Server style "go" command is understood
14577** as is the Oracle "/".
14578*/
14579static int line_is_command_terminator(const char *zLine){
14580  while( IsSpace(zLine[0]) ){ zLine++; };
14581  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
14582    return 1;  /* Oracle */
14583  }
14584  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
14585         && _all_whitespace(&zLine[2]) ){
14586    return 1;  /* SQL Server */
14587  }
14588  return 0;
14589}
14590
14591/*
14592** Return true if zSql is a complete SQL statement.  Return false if it
14593** ends in the middle of a string literal or C-style comment.
14594*/
14595static int line_is_complete(char *zSql, int nSql){
14596  int rc;
14597  if( zSql==0 ) return 1;
14598  zSql[nSql] = ';';
14599  zSql[nSql+1] = 0;
14600  rc = sqlite3_complete(zSql);
14601  zSql[nSql] = 0;
14602  return rc;
14603}
14604
14605/*
14606** Run a single line of SQL
14607*/
14608static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
14609  int rc;
14610  char *zErrMsg = 0;
14611
14612  open_db(p, 0);
14613  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
14614  BEGIN_TIMER;
14615  rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
14616  END_TIMER;
14617  if( rc || zErrMsg ){
14618    char zPrefix[100];
14619    if( in!=0 || !stdin_is_interactive ){
14620      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
14621                       "Error: near line %d:", startline);
14622    }else{
14623      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
14624    }
14625    if( zErrMsg!=0 ){
14626      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
14627      sqlite3_free(zErrMsg);
14628      zErrMsg = 0;
14629    }else{
14630      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
14631    }
14632    return 1;
14633  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
14634    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
14635            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
14636  }
14637  return 0;
14638}
14639
14640
14641/*
14642** Read input from *in and process it.  If *in==0 then input
14643** is interactive - the user is typing it it.  Otherwise, input
14644** is coming from a file or device.  A prompt is issued and history
14645** is saved only if input is interactive.  An interrupt signal will
14646** cause this routine to exit immediately, unless input is interactive.
14647**
14648** Return the number of errors.
14649*/
14650static int process_input(ShellState *p, FILE *in){
14651  char *zLine = 0;          /* A single input line */
14652  char *zSql = 0;           /* Accumulated SQL text */
14653  int nLine;                /* Length of current line */
14654  int nSql = 0;             /* Bytes of zSql[] used */
14655  int nAlloc = 0;           /* Allocated zSql[] space */
14656  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
14657  int rc;                   /* Error code */
14658  int errCnt = 0;           /* Number of errors seen */
14659  int lineno = 0;           /* Current line number */
14660  int startline = 0;        /* Line number for start of current input */
14661
14662  while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
14663    fflush(p->out);
14664    zLine = one_input_line(in, zLine, nSql>0);
14665    if( zLine==0 ){
14666      /* End of input */
14667      if( in==0 && stdin_is_interactive ) printf("\n");
14668      break;
14669    }
14670    if( seenInterrupt ){
14671      if( in!=0 ) break;
14672      seenInterrupt = 0;
14673    }
14674    lineno++;
14675    if( nSql==0 && _all_whitespace(zLine) ){
14676      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
14677      continue;
14678    }
14679    if( zLine && zLine[0]=='.' && nSql==0 ){
14680      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
14681      rc = do_meta_command(zLine, p);
14682      if( rc==2 ){ /* exit requested */
14683        break;
14684      }else if( rc ){
14685        errCnt++;
14686      }
14687      continue;
14688    }
14689    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
14690      memcpy(zLine,";",2);
14691    }
14692    nLine = strlen30(zLine);
14693    if( nSql+nLine+2>=nAlloc ){
14694      nAlloc = nSql+nLine+100;
14695      zSql = realloc(zSql, nAlloc);
14696      if( zSql==0 ){
14697        raw_printf(stderr, "Error: out of memory\n");
14698        exit(1);
14699      }
14700    }
14701    nSqlPrior = nSql;
14702    if( nSql==0 ){
14703      int i;
14704      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
14705      assert( nAlloc>0 && zSql!=0 );
14706      memcpy(zSql, zLine+i, nLine+1-i);
14707      startline = lineno;
14708      nSql = nLine-i;
14709    }else{
14710      zSql[nSql++] = '\n';
14711      memcpy(zSql+nSql, zLine, nLine+1);
14712      nSql += nLine;
14713    }
14714    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
14715                && sqlite3_complete(zSql) ){
14716      errCnt += runOneSqlLine(p, zSql, in, startline);
14717      nSql = 0;
14718      if( p->outCount ){
14719        output_reset(p);
14720        p->outCount = 0;
14721      }else{
14722        clearTempFile(p);
14723      }
14724    }else if( nSql && _all_whitespace(zSql) ){
14725      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
14726      nSql = 0;
14727    }
14728  }
14729  if( nSql && !_all_whitespace(zSql) ){
14730    runOneSqlLine(p, zSql, in, startline);
14731  }
14732  free(zSql);
14733  free(zLine);
14734  return errCnt>0;
14735}
14736
14737/*
14738** Return a pathname which is the user's home directory.  A
14739** 0 return indicates an error of some kind.
14740*/
14741static char *find_home_dir(int clearFlag){
14742  static char *home_dir = NULL;
14743  if( clearFlag ){
14744    free(home_dir);
14745    home_dir = 0;
14746    return 0;
14747  }
14748  if( home_dir ) return home_dir;
14749
14750#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
14751     && !defined(__RTP__) && !defined(_WRS_KERNEL)
14752  {
14753    struct passwd *pwent;
14754    uid_t uid = getuid();
14755    if( (pwent=getpwuid(uid)) != NULL) {
14756      home_dir = pwent->pw_dir;
14757    }
14758  }
14759#endif
14760
14761#if defined(_WIN32_WCE)
14762  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
14763   */
14764  home_dir = "/";
14765#else
14766
14767#if defined(_WIN32) || defined(WIN32)
14768  if (!home_dir) {
14769    home_dir = getenv("USERPROFILE");
14770  }
14771#endif
14772
14773  if (!home_dir) {
14774    home_dir = getenv("HOME");
14775  }
14776
14777#if defined(_WIN32) || defined(WIN32)
14778  if (!home_dir) {
14779    char *zDrive, *zPath;
14780    int n;
14781    zDrive = getenv("HOMEDRIVE");
14782    zPath = getenv("HOMEPATH");
14783    if( zDrive && zPath ){
14784      n = strlen30(zDrive) + strlen30(zPath) + 1;
14785      home_dir = malloc( n );
14786      if( home_dir==0 ) return 0;
14787      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
14788      return home_dir;
14789    }
14790    home_dir = "c:\\";
14791  }
14792#endif
14793
14794#endif /* !_WIN32_WCE */
14795
14796  if( home_dir ){
14797    int n = strlen30(home_dir) + 1;
14798    char *z = malloc( n );
14799    if( z ) memcpy(z, home_dir, n);
14800    home_dir = z;
14801  }
14802
14803  return home_dir;
14804}
14805
14806/*
14807** Read input from the file given by sqliterc_override.  Or if that
14808** parameter is NULL, take input from ~/.sqliterc
14809**
14810** Returns the number of errors.
14811*/
14812static void process_sqliterc(
14813  ShellState *p,                  /* Configuration data */
14814  const char *sqliterc_override   /* Name of config file. NULL to use default */
14815){
14816  char *home_dir = NULL;
14817  const char *sqliterc = sqliterc_override;
14818  char *zBuf = 0;
14819  FILE *in = NULL;
14820
14821  if (sqliterc == NULL) {
14822    home_dir = find_home_dir(0);
14823    if( home_dir==0 ){
14824      raw_printf(stderr, "-- warning: cannot find home directory;"
14825                      " cannot read ~/.sqliterc\n");
14826      return;
14827    }
14828    sqlite3_initialize();
14829    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
14830    sqliterc = zBuf;
14831  }
14832  in = fopen(sqliterc,"rb");
14833  if( in ){
14834    if( stdin_is_interactive ){
14835      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
14836    }
14837    process_input(p,in);
14838    fclose(in);
14839  }
14840  sqlite3_free(zBuf);
14841}
14842
14843/*
14844** Show available command line options
14845*/
14846static const char zOptions[] =
14847  "   -ascii               set output mode to 'ascii'\n"
14848  "   -bail                stop after hitting an error\n"
14849  "   -batch               force batch I/O\n"
14850  "   -column              set output mode to 'column'\n"
14851  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
14852  "   -csv                 set output mode to 'csv'\n"
14853  "   -echo                print commands before execution\n"
14854  "   -init FILENAME       read/process named file\n"
14855  "   -[no]header          turn headers on or off\n"
14856#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
14857  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
14858#endif
14859  "   -help                show this message\n"
14860  "   -html                set output mode to HTML\n"
14861  "   -interactive         force interactive I/O\n"
14862  "   -line                set output mode to 'line'\n"
14863  "   -list                set output mode to 'list'\n"
14864  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
14865  "   -mmap N              default mmap size set to N\n"
14866#ifdef SQLITE_ENABLE_MULTIPLEX
14867  "   -multiplex           enable the multiplexor VFS\n"
14868#endif
14869  "   -newline SEP         set output row separator. Default: '\\n'\n"
14870  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
14871  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
14872  "   -quote               set output mode to 'quote'\n"
14873  "   -separator SEP       set output column separator. Default: '|'\n"
14874  "   -stats               print memory stats before each finalize\n"
14875  "   -version             show SQLite version\n"
14876  "   -vfs NAME            use NAME as the default VFS\n"
14877#ifdef SQLITE_ENABLE_VFSTRACE
14878  "   -vfstrace            enable tracing of all VFS calls\n"
14879#endif
14880;
14881static void usage(int showDetail){
14882  utf8_printf(stderr,
14883      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
14884      "FILENAME is the name of an SQLite database. A new database is created\n"
14885      "if the file does not previously exist.\n", Argv0);
14886  if( showDetail ){
14887    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
14888  }else{
14889    raw_printf(stderr, "Use the -help option for additional information\n");
14890  }
14891  exit(1);
14892}
14893
14894/*
14895** Initialize the state information in data
14896*/
14897static void main_init(ShellState *data) {
14898  memset(data, 0, sizeof(*data));
14899  data->normalMode = data->cMode = data->mode = MODE_List;
14900  data->autoExplain = 1;
14901  memcpy(data->colSeparator,SEP_Column, 2);
14902  memcpy(data->rowSeparator,SEP_Row, 2);
14903  data->showHeader = 0;
14904  data->shellFlgs = SHFLG_Lookaside;
14905  sqlite3_config(SQLITE_CONFIG_URI, 1);
14906  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
14907  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
14908  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
14909  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
14910}
14911
14912/*
14913** Output text to the console in a font that attracts extra attention.
14914*/
14915#ifdef _WIN32
14916static void printBold(const char *zText){
14917  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
14918  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
14919  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
14920  SetConsoleTextAttribute(out,
14921         FOREGROUND_RED|FOREGROUND_INTENSITY
14922  );
14923  printf("%s", zText);
14924  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
14925}
14926#else
14927static void printBold(const char *zText){
14928  printf("\033[1m%s\033[0m", zText);
14929}
14930#endif
14931
14932/*
14933** Get the argument to an --option.  Throw an error and die if no argument
14934** is available.
14935*/
14936static char *cmdline_option_value(int argc, char **argv, int i){
14937  if( i==argc ){
14938    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
14939            argv[0], argv[argc-1]);
14940    exit(1);
14941  }
14942  return argv[i];
14943}
14944
14945#ifndef SQLITE_SHELL_IS_UTF8
14946#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
14947#    define SQLITE_SHELL_IS_UTF8          (0)
14948#  else
14949#    define SQLITE_SHELL_IS_UTF8          (1)
14950#  endif
14951#endif
14952
14953#if SQLITE_SHELL_IS_UTF8
14954int SQLITE_CDECL main(int argc, char **argv){
14955#else
14956int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
14957  char **argv;
14958#endif
14959  char *zErrMsg = 0;
14960  ShellState data;
14961  const char *zInitFile = 0;
14962  int i;
14963  int rc = 0;
14964  int warnInmemoryDb = 0;
14965  int readStdin = 1;
14966  int nCmd = 0;
14967  char **azCmd = 0;
14968
14969  setBinaryMode(stdin, 0);
14970  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
14971  stdin_is_interactive = isatty(0);
14972  stdout_is_console = isatty(1);
14973
14974#if USE_SYSTEM_SQLITE+0!=1
14975  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
14976    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
14977            sqlite3_sourceid(), SQLITE_SOURCE_ID);
14978    exit(1);
14979  }
14980#endif
14981  main_init(&data);
14982#if !SQLITE_SHELL_IS_UTF8
14983  sqlite3_initialize();
14984  argv = sqlite3_malloc64(sizeof(argv[0])*argc);
14985  if( argv==0 ){
14986    raw_printf(stderr, "out of memory\n");
14987    exit(1);
14988  }
14989  for(i=0; i<argc; i++){
14990    argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
14991    if( argv[i]==0 ){
14992      raw_printf(stderr, "out of memory\n");
14993      exit(1);
14994    }
14995  }
14996#endif
14997  assert( argc>=1 && argv && argv[0] );
14998  Argv0 = argv[0];
14999
15000  /* Make sure we have a valid signal handler early, before anything
15001  ** else is done.
15002  */
15003#ifdef SIGINT
15004  signal(SIGINT, interrupt_handler);
15005#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
15006  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
15007#endif
15008
15009#ifdef SQLITE_SHELL_DBNAME_PROC
15010  {
15011    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
15012    ** of a C-function that will provide the name of the database file.  Use
15013    ** this compile-time option to embed this shell program in larger
15014    ** applications. */
15015    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
15016    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
15017    warnInmemoryDb = 0;
15018  }
15019#endif
15020
15021  /* Do an initial pass through the command-line argument to locate
15022  ** the name of the database file, the name of the initialization file,
15023  ** the size of the alternative malloc heap,
15024  ** and the first command to execute.
15025  */
15026  for(i=1; i<argc; i++){
15027    char *z;
15028    z = argv[i];
15029    if( z[0]!='-' ){
15030      if( data.zDbFilename==0 ){
15031        data.zDbFilename = z;
15032      }else{
15033        /* Excesss arguments are interpreted as SQL (or dot-commands) and
15034        ** mean that nothing is read from stdin */
15035        readStdin = 0;
15036        nCmd++;
15037        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
15038        if( azCmd==0 ){
15039          raw_printf(stderr, "out of memory\n");
15040          exit(1);
15041        }
15042        azCmd[nCmd-1] = z;
15043      }
15044    }
15045    if( z[1]=='-' ) z++;
15046    if( strcmp(z,"-separator")==0
15047     || strcmp(z,"-nullvalue")==0
15048     || strcmp(z,"-newline")==0
15049     || strcmp(z,"-cmd")==0
15050    ){
15051      (void)cmdline_option_value(argc, argv, ++i);
15052    }else if( strcmp(z,"-init")==0 ){
15053      zInitFile = cmdline_option_value(argc, argv, ++i);
15054    }else if( strcmp(z,"-batch")==0 ){
15055      /* Need to check for batch mode here to so we can avoid printing
15056      ** informational messages (like from process_sqliterc) before
15057      ** we do the actual processing of arguments later in a second pass.
15058      */
15059      stdin_is_interactive = 0;
15060    }else if( strcmp(z,"-heap")==0 ){
15061#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
15062      const char *zSize;
15063      sqlite3_int64 szHeap;
15064
15065      zSize = cmdline_option_value(argc, argv, ++i);
15066      szHeap = integerValue(zSize);
15067      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
15068      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
15069#else
15070      (void)cmdline_option_value(argc, argv, ++i);
15071#endif
15072    }else if( strcmp(z,"-pagecache")==0 ){
15073      int n, sz;
15074      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15075      if( sz>70000 ) sz = 70000;
15076      if( sz<0 ) sz = 0;
15077      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15078      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
15079                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
15080      data.shellFlgs |= SHFLG_Pagecache;
15081    }else if( strcmp(z,"-lookaside")==0 ){
15082      int n, sz;
15083      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15084      if( sz<0 ) sz = 0;
15085      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15086      if( n<0 ) n = 0;
15087      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
15088      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
15089#ifdef SQLITE_ENABLE_VFSTRACE
15090    }else if( strcmp(z,"-vfstrace")==0 ){
15091      extern int vfstrace_register(
15092         const char *zTraceName,
15093         const char *zOldVfsName,
15094         int (*xOut)(const char*,void*),
15095         void *pOutArg,
15096         int makeDefault
15097      );
15098      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
15099#endif
15100#ifdef SQLITE_ENABLE_MULTIPLEX
15101    }else if( strcmp(z,"-multiplex")==0 ){
15102      extern int sqlite3_multiple_initialize(const char*,int);
15103      sqlite3_multiplex_initialize(0, 1);
15104#endif
15105    }else if( strcmp(z,"-mmap")==0 ){
15106      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
15107      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
15108    }else if( strcmp(z,"-vfs")==0 ){
15109      sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
15110      if( pVfs ){
15111        sqlite3_vfs_register(pVfs, 1);
15112      }else{
15113        utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
15114        exit(1);
15115      }
15116#ifdef SQLITE_HAVE_ZIP
15117    }else if( strcmp(z,"-zip")==0 ){
15118      data.openMode = SHELL_OPEN_ZIPFILE;
15119#endif
15120    }else if( strcmp(z,"-append")==0 ){
15121      data.openMode = SHELL_OPEN_APPENDVFS;
15122    }
15123  }
15124  if( data.zDbFilename==0 ){
15125#ifndef SQLITE_OMIT_MEMORYDB
15126    data.zDbFilename = ":memory:";
15127    warnInmemoryDb = argc==1;
15128#else
15129    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
15130    return 1;
15131#endif
15132  }
15133  data.out = stdout;
15134  sqlite3_appendvfs_init(0,0,0);
15135
15136  /* Go ahead and open the database file if it already exists.  If the
15137  ** file does not exist, delay opening it.  This prevents empty database
15138  ** files from being created if a user mistypes the database name argument
15139  ** to the sqlite command-line tool.
15140  */
15141  if( access(data.zDbFilename, 0)==0 ){
15142    open_db(&data, 0);
15143  }
15144
15145  /* Process the initialization file if there is one.  If no -init option
15146  ** is given on the command line, look for a file named ~/.sqliterc and
15147  ** try to process it.
15148  */
15149  process_sqliterc(&data,zInitFile);
15150
15151  /* Make a second pass through the command-line argument and set
15152  ** options.  This second pass is delayed until after the initialization
15153  ** file is processed so that the command-line arguments will override
15154  ** settings in the initialization file.
15155  */
15156  for(i=1; i<argc; i++){
15157    char *z = argv[i];
15158    if( z[0]!='-' ) continue;
15159    if( z[1]=='-' ){ z++; }
15160    if( strcmp(z,"-init")==0 ){
15161      i++;
15162    }else if( strcmp(z,"-html")==0 ){
15163      data.mode = MODE_Html;
15164    }else if( strcmp(z,"-list")==0 ){
15165      data.mode = MODE_List;
15166    }else if( strcmp(z,"-quote")==0 ){
15167      data.mode = MODE_Quote;
15168    }else if( strcmp(z,"-line")==0 ){
15169      data.mode = MODE_Line;
15170    }else if( strcmp(z,"-column")==0 ){
15171      data.mode = MODE_Column;
15172    }else if( strcmp(z,"-csv")==0 ){
15173      data.mode = MODE_Csv;
15174      memcpy(data.colSeparator,",",2);
15175#ifdef SQLITE_HAVE_ZIP
15176    }else if( strcmp(z,"-zip")==0 ){
15177      data.openMode = SHELL_OPEN_ZIPFILE;
15178#endif
15179    }else if( strcmp(z,"-append")==0 ){
15180      data.openMode = SHELL_OPEN_APPENDVFS;
15181    }else if( strcmp(z,"-ascii")==0 ){
15182      data.mode = MODE_Ascii;
15183      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
15184                       SEP_Unit);
15185      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
15186                       SEP_Record);
15187    }else if( strcmp(z,"-separator")==0 ){
15188      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
15189                       "%s",cmdline_option_value(argc,argv,++i));
15190    }else if( strcmp(z,"-newline")==0 ){
15191      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
15192                       "%s",cmdline_option_value(argc,argv,++i));
15193    }else if( strcmp(z,"-nullvalue")==0 ){
15194      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
15195                       "%s",cmdline_option_value(argc,argv,++i));
15196    }else if( strcmp(z,"-header")==0 ){
15197      data.showHeader = 1;
15198    }else if( strcmp(z,"-noheader")==0 ){
15199      data.showHeader = 0;
15200    }else if( strcmp(z,"-echo")==0 ){
15201      ShellSetFlag(&data, SHFLG_Echo);
15202    }else if( strcmp(z,"-eqp")==0 ){
15203      data.autoEQP = AUTOEQP_on;
15204    }else if( strcmp(z,"-eqpfull")==0 ){
15205      data.autoEQP = AUTOEQP_full;
15206    }else if( strcmp(z,"-stats")==0 ){
15207      data.statsOn = 1;
15208    }else if( strcmp(z,"-scanstats")==0 ){
15209      data.scanstatsOn = 1;
15210    }else if( strcmp(z,"-backslash")==0 ){
15211      /* Undocumented command-line option: -backslash
15212      ** Causes C-style backslash escapes to be evaluated in SQL statements
15213      ** prior to sending the SQL into SQLite.  Useful for injecting
15214      ** crazy bytes in the middle of SQL statements for testing and debugging.
15215      */
15216      ShellSetFlag(&data, SHFLG_Backslash);
15217    }else if( strcmp(z,"-bail")==0 ){
15218      bail_on_error = 1;
15219    }else if( strcmp(z,"-version")==0 ){
15220      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
15221      return 0;
15222    }else if( strcmp(z,"-interactive")==0 ){
15223      stdin_is_interactive = 1;
15224    }else if( strcmp(z,"-batch")==0 ){
15225      stdin_is_interactive = 0;
15226    }else if( strcmp(z,"-heap")==0 ){
15227      i++;
15228    }else if( strcmp(z,"-pagecache")==0 ){
15229      i+=2;
15230    }else if( strcmp(z,"-lookaside")==0 ){
15231      i+=2;
15232    }else if( strcmp(z,"-mmap")==0 ){
15233      i++;
15234    }else if( strcmp(z,"-vfs")==0 ){
15235      i++;
15236#ifdef SQLITE_ENABLE_VFSTRACE
15237    }else if( strcmp(z,"-vfstrace")==0 ){
15238      i++;
15239#endif
15240#ifdef SQLITE_ENABLE_MULTIPLEX
15241    }else if( strcmp(z,"-multiplex")==0 ){
15242      i++;
15243#endif
15244    }else if( strcmp(z,"-help")==0 ){
15245      usage(1);
15246    }else if( strcmp(z,"-cmd")==0 ){
15247      /* Run commands that follow -cmd first and separately from commands
15248      ** that simply appear on the command-line.  This seems goofy.  It would
15249      ** be better if all commands ran in the order that they appear.  But
15250      ** we retain the goofy behavior for historical compatibility. */
15251      if( i==argc-1 ) break;
15252      z = cmdline_option_value(argc,argv,++i);
15253      if( z[0]=='.' ){
15254        rc = do_meta_command(z, &data);
15255        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
15256      }else{
15257        open_db(&data, 0);
15258        rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
15259        if( zErrMsg!=0 ){
15260          utf8_printf(stderr,"Error: %s\n", zErrMsg);
15261          if( bail_on_error ) return rc!=0 ? rc : 1;
15262        }else if( rc!=0 ){
15263          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
15264          if( bail_on_error ) return rc;
15265        }
15266      }
15267    }else{
15268      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
15269      raw_printf(stderr,"Use -help for a list of options.\n");
15270      return 1;
15271    }
15272    data.cMode = data.mode;
15273  }
15274
15275  if( !readStdin ){
15276    /* Run all arguments that do not begin with '-' as if they were separate
15277    ** command-line inputs, except for the argToSkip argument which contains
15278    ** the database filename.
15279    */
15280    for(i=0; i<nCmd; i++){
15281      if( azCmd[i][0]=='.' ){
15282        rc = do_meta_command(azCmd[i], &data);
15283        if( rc ) return rc==2 ? 0 : rc;
15284      }else{
15285        open_db(&data, 0);
15286        rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
15287        if( zErrMsg!=0 ){
15288          utf8_printf(stderr,"Error: %s\n", zErrMsg);
15289          return rc!=0 ? rc : 1;
15290        }else if( rc!=0 ){
15291          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
15292          return rc;
15293        }
15294      }
15295    }
15296    free(azCmd);
15297  }else{
15298    /* Run commands received from standard input
15299    */
15300    if( stdin_is_interactive ){
15301      char *zHome;
15302      char *zHistory = 0;
15303      int nHistory;
15304      printf(
15305        "SQLite version %s %.19s\n" /*extra-version-info*/
15306        "Enter \".help\" for usage hints.\n",
15307        sqlite3_libversion(), sqlite3_sourceid()
15308      );
15309      if( warnInmemoryDb ){
15310        printf("Connected to a ");
15311        printBold("transient in-memory database");
15312        printf(".\nUse \".open FILENAME\" to reopen on a "
15313               "persistent database.\n");
15314      }
15315      zHome = find_home_dir(0);
15316      if( zHome ){
15317        nHistory = strlen30(zHome) + 20;
15318        if( (zHistory = malloc(nHistory))!=0 ){
15319          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
15320        }
15321      }
15322      if( zHistory ){ shell_read_history(zHistory); }
15323#if HAVE_READLINE || HAVE_EDITLINE
15324      rl_attempted_completion_function = readline_completion;
15325#elif HAVE_LINENOISE
15326      linenoiseSetCompletionCallback(linenoise_completion);
15327#endif
15328      rc = process_input(&data, 0);
15329      if( zHistory ){
15330        shell_stifle_history(2000);
15331        shell_write_history(zHistory);
15332        free(zHistory);
15333      }
15334    }else{
15335      rc = process_input(&data, stdin);
15336    }
15337  }
15338  set_table_name(&data, 0);
15339  if( data.db ){
15340    session_close_all(&data);
15341    sqlite3_close(data.db);
15342  }
15343  sqlite3_free(data.zFreeOnClose);
15344  find_home_dir(1);
15345  output_reset(&data);
15346  data.doXdgOpen = 0;
15347  clearTempFile(&data);
15348#if !SQLITE_SHELL_IS_UTF8
15349  for(i=0; i<argc; i++) sqlite3_free(argv[i]);
15350  sqlite3_free(argv);
15351#endif
15352  return rc;
15353}
15354