]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/sqlite3/shell.c
MFC r347139:
[FreeBSD/FreeBSD.git] / contrib / sqlite3 / shell.c
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"
82 typedef sqlite3_int64 i64;
83 typedef sqlite3_uint64 u64;
84 typedef 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 # define GETPID getpid
101 # if defined(__MINGW32__)
102 #  define DIRENT dirent
103 #  ifndef S_ISLNK
104 #   define S_ISLNK(mode) (0)
105 #  endif
106 # endif
107 #else
108 # define GETPID (int)GetCurrentProcessId
109 #endif
110 #include <sys/types.h>
111 #include <sys/stat.h>
112
113 #if HAVE_READLINE
114 # include <readline/readline.h>
115 # include <readline/history.h>
116 #endif
117
118 #if HAVE_EDITLINE
119 # include <editline/readline.h>
120 #endif
121
122 #if HAVE_EDITLINE || HAVE_READLINE
123
124 # define shell_add_history(X) add_history(X)
125 # define shell_read_history(X) read_history(X)
126 # define shell_write_history(X) write_history(X)
127 # define shell_stifle_history(X) stifle_history(X)
128 # define shell_readline(X) readline(X)
129
130 #elif HAVE_LINENOISE
131
132 # include "linenoise.h"
133 # define shell_add_history(X) linenoiseHistoryAdd(X)
134 # define shell_read_history(X) linenoiseHistoryLoad(X)
135 # define shell_write_history(X) linenoiseHistorySave(X)
136 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
137 # define shell_readline(X) linenoise(X)
138
139 #else
140
141 # define shell_read_history(X)
142 # define shell_write_history(X)
143 # define shell_stifle_history(X)
144
145 # define SHELL_USE_LOCAL_GETLINE 1
146 #endif
147
148
149 #if defined(_WIN32) || defined(WIN32)
150 # include <io.h>
151 # include <fcntl.h>
152 # define isatty(h) _isatty(h)
153 # ifndef access
154 #  define access(f,m) _access((f),(m))
155 # endif
156 # ifndef unlink
157 #  define unlink _unlink
158 # endif
159 # ifndef strdup
160 #  define strdup _strdup
161 # endif
162 # undef popen
163 # define popen _popen
164 # undef pclose
165 # define pclose _pclose
166 #else
167  /* Make sure isatty() has a prototype. */
168  extern int isatty(int);
169
170 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
171   /* popen and pclose are not C89 functions and so are
172   ** sometimes omitted from the <stdio.h> header */
173    extern FILE *popen(const char*,const char*);
174    extern int pclose(FILE*);
175 # else
176 #  define SQLITE_OMIT_POPEN 1
177 # endif
178 #endif
179
180 #if defined(_WIN32_WCE)
181 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
182  * thus we always assume that we have a console. That can be
183  * overridden with the -batch command line option.
184  */
185 #define isatty(x) 1
186 #endif
187
188 /* ctype macros that work with signed characters */
189 #define IsSpace(X)  isspace((unsigned char)X)
190 #define IsDigit(X)  isdigit((unsigned char)X)
191 #define ToLower(X)  (char)tolower((unsigned char)X)
192
193 #if defined(_WIN32) || defined(WIN32)
194 #include <windows.h>
195
196 /* string conversion routines only needed on Win32 */
197 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
198 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
199 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
200 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
201 #endif
202
203 /* On Windows, we normally run with output mode of TEXT so that \n characters
204 ** are automatically translated into \r\n.  However, this behavior needs
205 ** to be disabled in some cases (ex: when generating CSV output and when
206 ** rendering quoted strings that contain \n characters).  The following
207 ** routines take care of that.
208 */
209 #if defined(_WIN32) || defined(WIN32)
210 static void setBinaryMode(FILE *file, int isOutput){
211   if( isOutput ) fflush(file);
212   _setmode(_fileno(file), _O_BINARY);
213 }
214 static void setTextMode(FILE *file, int isOutput){
215   if( isOutput ) fflush(file);
216   _setmode(_fileno(file), _O_TEXT);
217 }
218 #else
219 # define setBinaryMode(X,Y)
220 # define setTextMode(X,Y)
221 #endif
222
223
224 /* True if the timer is enabled */
225 static int enableTimer = 0;
226
227 /* Return the current wall-clock time */
228 static sqlite3_int64 timeOfDay(void){
229   static sqlite3_vfs *clockVfs = 0;
230   sqlite3_int64 t;
231   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
232   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
233     clockVfs->xCurrentTimeInt64(clockVfs, &t);
234   }else{
235     double r;
236     clockVfs->xCurrentTime(clockVfs, &r);
237     t = (sqlite3_int64)(r*86400000.0);
238   }
239   return t;
240 }
241
242 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
243 #include <sys/time.h>
244 #include <sys/resource.h>
245
246 /* VxWorks does not support getrusage() as far as we can determine */
247 #if defined(_WRS_KERNEL) || defined(__RTP__)
248 struct rusage {
249   struct timeval ru_utime; /* user CPU time used */
250   struct timeval ru_stime; /* system CPU time used */
251 };
252 #define getrusage(A,B) memset(B,0,sizeof(*B))
253 #endif
254
255 /* Saved resource information for the beginning of an operation */
256 static struct rusage sBegin;  /* CPU time at start */
257 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
258
259 /*
260 ** Begin timing an operation
261 */
262 static void beginTimer(void){
263   if( enableTimer ){
264     getrusage(RUSAGE_SELF, &sBegin);
265     iBegin = timeOfDay();
266   }
267 }
268
269 /* Return the difference of two time_structs in seconds */
270 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
271   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
272          (double)(pEnd->tv_sec - pStart->tv_sec);
273 }
274
275 /*
276 ** Print the timing results.
277 */
278 static void endTimer(void){
279   if( enableTimer ){
280     sqlite3_int64 iEnd = timeOfDay();
281     struct rusage sEnd;
282     getrusage(RUSAGE_SELF, &sEnd);
283     printf("Run Time: real %.3f user %f sys %f\n",
284        (iEnd - iBegin)*0.001,
285        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
286        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
287   }
288 }
289
290 #define BEGIN_TIMER beginTimer()
291 #define END_TIMER endTimer()
292 #define HAS_TIMER 1
293
294 #elif (defined(_WIN32) || defined(WIN32))
295
296 /* Saved resource information for the beginning of an operation */
297 static HANDLE hProcess;
298 static FILETIME ftKernelBegin;
299 static FILETIME ftUserBegin;
300 static sqlite3_int64 ftWallBegin;
301 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
302                                     LPFILETIME, LPFILETIME);
303 static GETPROCTIMES getProcessTimesAddr = NULL;
304
305 /*
306 ** Check to see if we have timer support.  Return 1 if necessary
307 ** support found (or found previously).
308 */
309 static int hasTimer(void){
310   if( getProcessTimesAddr ){
311     return 1;
312   } else {
313     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
314     ** versions. See if the version we are running on has it, and if it
315     ** does, save off a pointer to it and the current process handle.
316     */
317     hProcess = GetCurrentProcess();
318     if( hProcess ){
319       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
320       if( NULL != hinstLib ){
321         getProcessTimesAddr =
322             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
323         if( NULL != getProcessTimesAddr ){
324           return 1;
325         }
326         FreeLibrary(hinstLib);
327       }
328     }
329   }
330   return 0;
331 }
332
333 /*
334 ** Begin timing an operation
335 */
336 static void beginTimer(void){
337   if( enableTimer && getProcessTimesAddr ){
338     FILETIME ftCreation, ftExit;
339     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
340                         &ftKernelBegin,&ftUserBegin);
341     ftWallBegin = timeOfDay();
342   }
343 }
344
345 /* Return the difference of two FILETIME structs in seconds */
346 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
347   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
348   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
349   return (double) ((i64End - i64Start) / 10000000.0);
350 }
351
352 /*
353 ** Print the timing results.
354 */
355 static void endTimer(void){
356   if( enableTimer && getProcessTimesAddr){
357     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
358     sqlite3_int64 ftWallEnd = timeOfDay();
359     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
360     printf("Run Time: real %.3f user %f sys %f\n",
361        (ftWallEnd - ftWallBegin)*0.001,
362        timeDiff(&ftUserBegin, &ftUserEnd),
363        timeDiff(&ftKernelBegin, &ftKernelEnd));
364   }
365 }
366
367 #define BEGIN_TIMER beginTimer()
368 #define END_TIMER endTimer()
369 #define HAS_TIMER hasTimer()
370
371 #else
372 #define BEGIN_TIMER
373 #define END_TIMER
374 #define HAS_TIMER 0
375 #endif
376
377 /*
378 ** Used to prevent warnings about unused parameters
379 */
380 #define UNUSED_PARAMETER(x) (void)(x)
381
382 /*
383 ** Number of elements in an array
384 */
385 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
386
387 /*
388 ** If the following flag is set, then command execution stops
389 ** at an error if we are not interactive.
390 */
391 static int bail_on_error = 0;
392
393 /*
394 ** Threat stdin as an interactive input if the following variable
395 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
396 */
397 static int stdin_is_interactive = 1;
398
399 /*
400 ** On Windows systems we have to know if standard output is a console
401 ** in order to translate UTF-8 into MBCS.  The following variable is
402 ** true if translation is required.
403 */
404 static int stdout_is_console = 1;
405
406 /*
407 ** The following is the open SQLite database.  We make a pointer
408 ** to this database a static variable so that it can be accessed
409 ** by the SIGINT handler to interrupt database processing.
410 */
411 static sqlite3 *globalDb = 0;
412
413 /*
414 ** True if an interrupt (Control-C) has been received.
415 */
416 static volatile int seenInterrupt = 0;
417
418 /*
419 ** This is the name of our program. It is set in main(), used
420 ** in a number of other places, mostly for error messages.
421 */
422 static char *Argv0;
423
424 /*
425 ** Prompt strings. Initialized in main. Settable with
426 **   .prompt main continue
427 */
428 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
429 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
430
431 /*
432 ** Render output like fprintf().  Except, if the output is going to the
433 ** console and if this is running on a Windows machine, translate the
434 ** output from UTF-8 into MBCS.
435 */
436 #if defined(_WIN32) || defined(WIN32)
437 void utf8_printf(FILE *out, const char *zFormat, ...){
438   va_list ap;
439   va_start(ap, zFormat);
440   if( stdout_is_console && (out==stdout || out==stderr) ){
441     char *z1 = sqlite3_vmprintf(zFormat, ap);
442     char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
443     sqlite3_free(z1);
444     fputs(z2, out);
445     sqlite3_free(z2);
446   }else{
447     vfprintf(out, zFormat, ap);
448   }
449   va_end(ap);
450 }
451 #elif !defined(utf8_printf)
452 # define utf8_printf fprintf
453 #endif
454
455 /*
456 ** Render output like fprintf().  This should not be used on anything that
457 ** includes string formatting (e.g. "%s").
458 */
459 #if !defined(raw_printf)
460 # define raw_printf fprintf
461 #endif
462
463 /* Indicate out-of-memory and exit. */
464 static void shell_out_of_memory(void){
465   raw_printf(stderr,"Error: out of memory\n");
466   exit(1);
467 }
468
469 /*
470 ** Write I/O traces to the following stream.
471 */
472 #ifdef SQLITE_ENABLE_IOTRACE
473 static FILE *iotrace = 0;
474 #endif
475
476 /*
477 ** This routine works like printf in that its first argument is a
478 ** format string and subsequent arguments are values to be substituted
479 ** in place of % fields.  The result of formatting this string
480 ** is written to iotrace.
481 */
482 #ifdef SQLITE_ENABLE_IOTRACE
483 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
484   va_list ap;
485   char *z;
486   if( iotrace==0 ) return;
487   va_start(ap, zFormat);
488   z = sqlite3_vmprintf(zFormat, ap);
489   va_end(ap);
490   utf8_printf(iotrace, "%s", z);
491   sqlite3_free(z);
492 }
493 #endif
494
495 /*
496 ** Output string zUtf to stream pOut as w characters.  If w is negative,
497 ** then right-justify the text.  W is the width in UTF-8 characters, not
498 ** in bytes.  This is different from the %*.*s specification in printf
499 ** since with %*.*s the width is measured in bytes, not characters.
500 */
501 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
502   int i;
503   int n;
504   int aw = w<0 ? -w : w;
505   char zBuf[1000];
506   if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
507   for(i=n=0; zUtf[i]; i++){
508     if( (zUtf[i]&0xc0)!=0x80 ){
509       n++;
510       if( n==aw ){
511         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
512         break;
513       }
514     }
515   }
516   if( n>=aw ){
517     utf8_printf(pOut, "%.*s", i, zUtf);
518   }else if( w<0 ){
519     utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
520   }else{
521     utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
522   }
523 }
524
525
526 /*
527 ** Determines if a string is a number of not.
528 */
529 static int isNumber(const char *z, int *realnum){
530   if( *z=='-' || *z=='+' ) z++;
531   if( !IsDigit(*z) ){
532     return 0;
533   }
534   z++;
535   if( realnum ) *realnum = 0;
536   while( IsDigit(*z) ){ z++; }
537   if( *z=='.' ){
538     z++;
539     if( !IsDigit(*z) ) return 0;
540     while( IsDigit(*z) ){ z++; }
541     if( realnum ) *realnum = 1;
542   }
543   if( *z=='e' || *z=='E' ){
544     z++;
545     if( *z=='+' || *z=='-' ) z++;
546     if( !IsDigit(*z) ) return 0;
547     while( IsDigit(*z) ){ z++; }
548     if( realnum ) *realnum = 1;
549   }
550   return *z==0;
551 }
552
553 /*
554 ** Compute a string length that is limited to what can be stored in
555 ** lower 30 bits of a 32-bit signed integer.
556 */
557 static int strlen30(const char *z){
558   const char *z2 = z;
559   while( *z2 ){ z2++; }
560   return 0x3fffffff & (int)(z2 - z);
561 }
562
563 /*
564 ** Return the length of a string in characters.  Multibyte UTF8 characters
565 ** count as a single character.
566 */
567 static int strlenChar(const char *z){
568   int n = 0;
569   while( *z ){
570     if( (0xc0&*(z++))!=0x80 ) n++;
571   }
572   return n;
573 }
574
575 /*
576 ** This routine reads a line of text from FILE in, stores
577 ** the text in memory obtained from malloc() and returns a pointer
578 ** to the text.  NULL is returned at end of file, or if malloc()
579 ** fails.
580 **
581 ** If zLine is not NULL then it is a malloced buffer returned from
582 ** a previous call to this routine that may be reused.
583 */
584 static char *local_getline(char *zLine, FILE *in){
585   int nLine = zLine==0 ? 0 : 100;
586   int n = 0;
587
588   while( 1 ){
589     if( n+100>nLine ){
590       nLine = nLine*2 + 100;
591       zLine = realloc(zLine, nLine);
592       if( zLine==0 ) shell_out_of_memory();
593     }
594     if( fgets(&zLine[n], nLine - n, in)==0 ){
595       if( n==0 ){
596         free(zLine);
597         return 0;
598       }
599       zLine[n] = 0;
600       break;
601     }
602     while( zLine[n] ) n++;
603     if( n>0 && zLine[n-1]=='\n' ){
604       n--;
605       if( n>0 && zLine[n-1]=='\r' ) n--;
606       zLine[n] = 0;
607       break;
608     }
609   }
610 #if defined(_WIN32) || defined(WIN32)
611   /* For interactive input on Windows systems, translate the
612   ** multi-byte characterset characters into UTF-8. */
613   if( stdin_is_interactive && in==stdin ){
614     char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
615     if( zTrans ){
616       int nTrans = strlen30(zTrans)+1;
617       if( nTrans>nLine ){
618         zLine = realloc(zLine, nTrans);
619         if( zLine==0 ) shell_out_of_memory();
620       }
621       memcpy(zLine, zTrans, nTrans);
622       sqlite3_free(zTrans);
623     }
624   }
625 #endif /* defined(_WIN32) || defined(WIN32) */
626   return zLine;
627 }
628
629 /*
630 ** Retrieve a single line of input text.
631 **
632 ** If in==0 then read from standard input and prompt before each line.
633 ** If isContinuation is true, then a continuation prompt is appropriate.
634 ** If isContinuation is zero, then the main prompt should be used.
635 **
636 ** If zPrior is not NULL then it is a buffer from a prior call to this
637 ** routine that can be reused.
638 **
639 ** The result is stored in space obtained from malloc() and must either
640 ** be freed by the caller or else passed back into this routine via the
641 ** zPrior argument for reuse.
642 */
643 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
644   char *zPrompt;
645   char *zResult;
646   if( in!=0 ){
647     zResult = local_getline(zPrior, in);
648   }else{
649     zPrompt = isContinuation ? continuePrompt : mainPrompt;
650 #if SHELL_USE_LOCAL_GETLINE
651     printf("%s", zPrompt);
652     fflush(stdout);
653     zResult = local_getline(zPrior, stdin);
654 #else
655     free(zPrior);
656     zResult = shell_readline(zPrompt);
657     if( zResult && *zResult ) shell_add_history(zResult);
658 #endif
659   }
660   return zResult;
661 }
662
663
664 /*
665 ** Return the value of a hexadecimal digit.  Return -1 if the input
666 ** is not a hex digit.
667 */
668 static int hexDigitValue(char c){
669   if( c>='0' && c<='9' ) return c - '0';
670   if( c>='a' && c<='f' ) return c - 'a' + 10;
671   if( c>='A' && c<='F' ) return c - 'A' + 10;
672   return -1;
673 }
674
675 /*
676 ** Interpret zArg as an integer value, possibly with suffixes.
677 */
678 static sqlite3_int64 integerValue(const char *zArg){
679   sqlite3_int64 v = 0;
680   static const struct { char *zSuffix; int iMult; } aMult[] = {
681     { "KiB", 1024 },
682     { "MiB", 1024*1024 },
683     { "GiB", 1024*1024*1024 },
684     { "KB",  1000 },
685     { "MB",  1000000 },
686     { "GB",  1000000000 },
687     { "K",   1000 },
688     { "M",   1000000 },
689     { "G",   1000000000 },
690   };
691   int i;
692   int isNeg = 0;
693   if( zArg[0]=='-' ){
694     isNeg = 1;
695     zArg++;
696   }else if( zArg[0]=='+' ){
697     zArg++;
698   }
699   if( zArg[0]=='0' && zArg[1]=='x' ){
700     int x;
701     zArg += 2;
702     while( (x = hexDigitValue(zArg[0]))>=0 ){
703       v = (v<<4) + x;
704       zArg++;
705     }
706   }else{
707     while( IsDigit(zArg[0]) ){
708       v = v*10 + zArg[0] - '0';
709       zArg++;
710     }
711   }
712   for(i=0; i<ArraySize(aMult); i++){
713     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
714       v *= aMult[i].iMult;
715       break;
716     }
717   }
718   return isNeg? -v : v;
719 }
720
721 /*
722 ** A variable length string to which one can append text.
723 */
724 typedef struct ShellText ShellText;
725 struct ShellText {
726   char *z;
727   int n;
728   int nAlloc;
729 };
730
731 /*
732 ** Initialize and destroy a ShellText object
733 */
734 static void initText(ShellText *p){
735   memset(p, 0, sizeof(*p));
736 }
737 static void freeText(ShellText *p){
738   free(p->z);
739   initText(p);
740 }
741
742 /* zIn is either a pointer to a NULL-terminated string in memory obtained
743 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
744 ** added to zIn, and the result returned in memory obtained from malloc().
745 ** zIn, if it was not NULL, is freed.
746 **
747 ** If the third argument, quote, is not '\0', then it is used as a
748 ** quote character for zAppend.
749 */
750 static void appendText(ShellText *p, char const *zAppend, char quote){
751   int len;
752   int i;
753   int nAppend = strlen30(zAppend);
754
755   len = nAppend+p->n+1;
756   if( quote ){
757     len += 2;
758     for(i=0; i<nAppend; i++){
759       if( zAppend[i]==quote ) len++;
760     }
761   }
762
763   if( p->n+len>=p->nAlloc ){
764     p->nAlloc = p->nAlloc*2 + len + 20;
765     p->z = realloc(p->z, p->nAlloc);
766     if( p->z==0 ) shell_out_of_memory();
767   }
768
769   if( quote ){
770     char *zCsr = p->z+p->n;
771     *zCsr++ = quote;
772     for(i=0; i<nAppend; i++){
773       *zCsr++ = zAppend[i];
774       if( zAppend[i]==quote ) *zCsr++ = quote;
775     }
776     *zCsr++ = quote;
777     p->n = (int)(zCsr - p->z);
778     *zCsr = '\0';
779   }else{
780     memcpy(p->z+p->n, zAppend, nAppend);
781     p->n += nAppend;
782     p->z[p->n] = '\0';
783   }
784 }
785
786 /*
787 ** Attempt to determine if identifier zName needs to be quoted, either
788 ** because it contains non-alphanumeric characters, or because it is an
789 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
790 ** that quoting is required.
791 **
792 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
793 */
794 static char quoteChar(const char *zName){
795   int i;
796   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
797   for(i=0; zName[i]; i++){
798     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
799   }
800   return sqlite3_keyword_check(zName, i) ? '"' : 0;
801 }
802
803 /*
804 ** Construct a fake object name and column list to describe the structure
805 ** of the view, virtual table, or table valued function zSchema.zName.
806 */
807 static char *shellFakeSchema(
808   sqlite3 *db,            /* The database connection containing the vtab */
809   const char *zSchema,    /* Schema of the database holding the vtab */
810   const char *zName       /* The name of the virtual table */
811 ){
812   sqlite3_stmt *pStmt = 0;
813   char *zSql;
814   ShellText s;
815   char cQuote;
816   char *zDiv = "(";
817   int nRow = 0;
818
819   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
820                          zSchema ? zSchema : "main", zName);
821   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
822   sqlite3_free(zSql);
823   initText(&s);
824   if( zSchema ){
825     cQuote = quoteChar(zSchema);
826     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
827     appendText(&s, zSchema, cQuote);
828     appendText(&s, ".", 0);
829   }
830   cQuote = quoteChar(zName);
831   appendText(&s, zName, cQuote);
832   while( sqlite3_step(pStmt)==SQLITE_ROW ){
833     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
834     nRow++;
835     appendText(&s, zDiv, 0);
836     zDiv = ",";
837     cQuote = quoteChar(zCol);
838     appendText(&s, zCol, cQuote);
839   }
840   appendText(&s, ")", 0);
841   sqlite3_finalize(pStmt);
842   if( nRow==0 ){
843     freeText(&s);
844     s.z = 0;
845   }
846   return s.z;
847 }
848
849 /*
850 ** SQL function:  shell_module_schema(X)
851 **
852 ** Return a fake schema for the table-valued function or eponymous virtual
853 ** table X.
854 */
855 static void shellModuleSchema(
856   sqlite3_context *pCtx,
857   int nVal,
858   sqlite3_value **apVal
859 ){
860   const char *zName = (const char*)sqlite3_value_text(apVal[0]);
861   char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
862   UNUSED_PARAMETER(nVal);
863   if( zFake ){
864     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
865                         -1, sqlite3_free);
866     free(zFake);
867   }
868 }
869
870 /*
871 ** SQL function:  shell_add_schema(S,X)
872 **
873 ** Add the schema name X to the CREATE statement in S and return the result.
874 ** Examples:
875 **
876 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
877 **
878 ** Also works on
879 **
880 **    CREATE INDEX
881 **    CREATE UNIQUE INDEX
882 **    CREATE VIEW
883 **    CREATE TRIGGER
884 **    CREATE VIRTUAL TABLE
885 **
886 ** This UDF is used by the .schema command to insert the schema name of
887 ** attached databases into the middle of the sqlite_master.sql field.
888 */
889 static void shellAddSchemaName(
890   sqlite3_context *pCtx,
891   int nVal,
892   sqlite3_value **apVal
893 ){
894   static const char *aPrefix[] = {
895      "TABLE",
896      "INDEX",
897      "UNIQUE INDEX",
898      "VIEW",
899      "TRIGGER",
900      "VIRTUAL TABLE"
901   };
902   int i = 0;
903   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
904   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
905   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
906   sqlite3 *db = sqlite3_context_db_handle(pCtx);
907   UNUSED_PARAMETER(nVal);
908   if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
909     for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
910       int n = strlen30(aPrefix[i]);
911       if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
912         char *z = 0;
913         char *zFake = 0;
914         if( zSchema ){
915           char cQuote = quoteChar(zSchema);
916           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
917             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
918           }else{
919             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
920           }
921         }
922         if( zName
923          && aPrefix[i][0]=='V'
924          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
925         ){
926           if( z==0 ){
927             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
928           }else{
929             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
930           }
931           free(zFake);
932         }
933         if( z ){
934           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
935           return;
936         }
937       }
938     }
939   }
940   sqlite3_result_value(pCtx, apVal[0]);
941 }
942
943 /*
944 ** The source code for several run-time loadable extensions is inserted
945 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
946 ** code, we need to override some macros to make the included program code
947 ** work here in the middle of this regular program.
948 */
949 #define SQLITE_EXTENSION_INIT1
950 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
951
952 #if defined(_WIN32) && defined(_MSC_VER)
953 /************************* Begin test_windirent.h ******************/
954 /*
955 ** 2015 November 30
956 **
957 ** The author disclaims copyright to this source code.  In place of
958 ** a legal notice, here is a blessing:
959 **
960 **    May you do good and not evil.
961 **    May you find forgiveness for yourself and forgive others.
962 **    May you share freely, never taking more than you give.
963 **
964 *************************************************************************
965 ** This file contains declarations for most of the opendir() family of
966 ** POSIX functions on Win32 using the MSVCRT.
967 */
968
969 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
970 #define SQLITE_WINDIRENT_H
971
972 /*
973 ** We need several data types from the Windows SDK header.
974 */
975
976 #ifndef WIN32_LEAN_AND_MEAN
977 #define WIN32_LEAN_AND_MEAN
978 #endif
979
980 #include "windows.h"
981
982 /*
983 ** We need several support functions from the SQLite core.
984 */
985
986
987 /*
988 ** We need several things from the ANSI and MSVCRT headers.
989 */
990
991 #include <stdio.h>
992 #include <stdlib.h>
993 #include <errno.h>
994 #include <io.h>
995 #include <limits.h>
996 #include <sys/types.h>
997 #include <sys/stat.h>
998
999 /*
1000 ** We may need several defines that should have been in "sys/stat.h".
1001 */
1002
1003 #ifndef S_ISREG
1004 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1005 #endif
1006
1007 #ifndef S_ISDIR
1008 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1009 #endif
1010
1011 #ifndef S_ISLNK
1012 #define S_ISLNK(mode) (0)
1013 #endif
1014
1015 /*
1016 ** We may need to provide the "mode_t" type.
1017 */
1018
1019 #ifndef MODE_T_DEFINED
1020   #define MODE_T_DEFINED
1021   typedef unsigned short mode_t;
1022 #endif
1023
1024 /*
1025 ** We may need to provide the "ino_t" type.
1026 */
1027
1028 #ifndef INO_T_DEFINED
1029   #define INO_T_DEFINED
1030   typedef unsigned short ino_t;
1031 #endif
1032
1033 /*
1034 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1035 */
1036
1037 #ifndef NAME_MAX
1038 #  ifdef FILENAME_MAX
1039 #    define NAME_MAX (FILENAME_MAX)
1040 #  else
1041 #    define NAME_MAX (260)
1042 #  endif
1043 #endif
1044
1045 /*
1046 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1047 */
1048
1049 #ifndef NULL_INTPTR_T
1050 #  define NULL_INTPTR_T ((intptr_t)(0))
1051 #endif
1052
1053 #ifndef BAD_INTPTR_T
1054 #  define BAD_INTPTR_T ((intptr_t)(-1))
1055 #endif
1056
1057 /*
1058 ** We need to provide the necessary structures and related types.
1059 */
1060
1061 #ifndef DIRENT_DEFINED
1062 #define DIRENT_DEFINED
1063 typedef struct DIRENT DIRENT;
1064 typedef DIRENT *LPDIRENT;
1065 struct DIRENT {
1066   ino_t d_ino;               /* Sequence number, do not use. */
1067   unsigned d_attributes;     /* Win32 file attributes. */
1068   char d_name[NAME_MAX + 1]; /* Name within the directory. */
1069 };
1070 #endif
1071
1072 #ifndef DIR_DEFINED
1073 #define DIR_DEFINED
1074 typedef struct DIR DIR;
1075 typedef DIR *LPDIR;
1076 struct DIR {
1077   intptr_t d_handle; /* Value returned by "_findfirst". */
1078   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1079   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1080 };
1081 #endif
1082
1083 /*
1084 ** Provide a macro, for use by the implementation, to determine if a
1085 ** particular directory entry should be skipped over when searching for
1086 ** the next directory entry that should be returned by the readdir() or
1087 ** readdir_r() functions.
1088 */
1089
1090 #ifndef is_filtered
1091 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1092 #endif
1093
1094 /*
1095 ** Provide the function prototype for the POSIX compatiable getenv()
1096 ** function.  This function is not thread-safe.
1097 */
1098
1099 extern const char *windirent_getenv(const char *name);
1100
1101 /*
1102 ** Finally, we can provide the function prototypes for the opendir(),
1103 ** readdir(), readdir_r(), and closedir() POSIX functions.
1104 */
1105
1106 extern LPDIR opendir(const char *dirname);
1107 extern LPDIRENT readdir(LPDIR dirp);
1108 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1109 extern INT closedir(LPDIR dirp);
1110
1111 #endif /* defined(WIN32) && defined(_MSC_VER) */
1112
1113 /************************* End test_windirent.h ********************/
1114 /************************* Begin test_windirent.c ******************/
1115 /*
1116 ** 2015 November 30
1117 **
1118 ** The author disclaims copyright to this source code.  In place of
1119 ** a legal notice, here is a blessing:
1120 **
1121 **    May you do good and not evil.
1122 **    May you find forgiveness for yourself and forgive others.
1123 **    May you share freely, never taking more than you give.
1124 **
1125 *************************************************************************
1126 ** This file contains code to implement most of the opendir() family of
1127 ** POSIX functions on Win32 using the MSVCRT.
1128 */
1129
1130 #if defined(_WIN32) && defined(_MSC_VER)
1131 /* #include "test_windirent.h" */
1132
1133 /*
1134 ** Implementation of the POSIX getenv() function using the Win32 API.
1135 ** This function is not thread-safe.
1136 */
1137 const char *windirent_getenv(
1138   const char *name
1139 ){
1140   static char value[32768]; /* Maximum length, per MSDN */
1141   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1142   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1143
1144   memset(value, 0, sizeof(value));
1145   dwRet = GetEnvironmentVariableA(name, value, dwSize);
1146   if( dwRet==0 || dwRet>dwSize ){
1147     /*
1148     ** The function call to GetEnvironmentVariableA() failed -OR-
1149     ** the buffer is not large enough.  Either way, return NULL.
1150     */
1151     return 0;
1152   }else{
1153     /*
1154     ** The function call to GetEnvironmentVariableA() succeeded
1155     ** -AND- the buffer contains the entire value.
1156     */
1157     return value;
1158   }
1159 }
1160
1161 /*
1162 ** Implementation of the POSIX opendir() function using the MSVCRT.
1163 */
1164 LPDIR opendir(
1165   const char *dirname
1166 ){
1167   struct _finddata_t data;
1168   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1169   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1170
1171   if( dirp==NULL ) return NULL;
1172   memset(dirp, 0, sizeof(DIR));
1173
1174   /* TODO: Remove this if Unix-style root paths are not used. */
1175   if( sqlite3_stricmp(dirname, "/")==0 ){
1176     dirname = windirent_getenv("SystemDrive");
1177   }
1178
1179   memset(&data, 0, sizeof(struct _finddata_t));
1180   _snprintf(data.name, namesize, "%s\\*", dirname);
1181   dirp->d_handle = _findfirst(data.name, &data);
1182
1183   if( dirp->d_handle==BAD_INTPTR_T ){
1184     closedir(dirp);
1185     return NULL;
1186   }
1187
1188   /* TODO: Remove this block to allow hidden and/or system files. */
1189   if( is_filtered(data) ){
1190 next:
1191
1192     memset(&data, 0, sizeof(struct _finddata_t));
1193     if( _findnext(dirp->d_handle, &data)==-1 ){
1194       closedir(dirp);
1195       return NULL;
1196     }
1197
1198     /* TODO: Remove this block to allow hidden and/or system files. */
1199     if( is_filtered(data) ) goto next;
1200   }
1201
1202   dirp->d_first.d_attributes = data.attrib;
1203   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1204   dirp->d_first.d_name[NAME_MAX] = '\0';
1205
1206   return dirp;
1207 }
1208
1209 /*
1210 ** Implementation of the POSIX readdir() function using the MSVCRT.
1211 */
1212 LPDIRENT readdir(
1213   LPDIR dirp
1214 ){
1215   struct _finddata_t data;
1216
1217   if( dirp==NULL ) return NULL;
1218
1219   if( dirp->d_first.d_ino==0 ){
1220     dirp->d_first.d_ino++;
1221     dirp->d_next.d_ino++;
1222
1223     return &dirp->d_first;
1224   }
1225
1226 next:
1227
1228   memset(&data, 0, sizeof(struct _finddata_t));
1229   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1230
1231   /* TODO: Remove this block to allow hidden and/or system files. */
1232   if( is_filtered(data) ) goto next;
1233
1234   dirp->d_next.d_ino++;
1235   dirp->d_next.d_attributes = data.attrib;
1236   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1237   dirp->d_next.d_name[NAME_MAX] = '\0';
1238
1239   return &dirp->d_next;
1240 }
1241
1242 /*
1243 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1244 */
1245 INT readdir_r(
1246   LPDIR dirp,
1247   LPDIRENT entry,
1248   LPDIRENT *result
1249 ){
1250   struct _finddata_t data;
1251
1252   if( dirp==NULL ) return EBADF;
1253
1254   if( dirp->d_first.d_ino==0 ){
1255     dirp->d_first.d_ino++;
1256     dirp->d_next.d_ino++;
1257
1258     entry->d_ino = dirp->d_first.d_ino;
1259     entry->d_attributes = dirp->d_first.d_attributes;
1260     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1261     entry->d_name[NAME_MAX] = '\0';
1262
1263     *result = entry;
1264     return 0;
1265   }
1266
1267 next:
1268
1269   memset(&data, 0, sizeof(struct _finddata_t));
1270   if( _findnext(dirp->d_handle, &data)==-1 ){
1271     *result = NULL;
1272     return ENOENT;
1273   }
1274
1275   /* TODO: Remove this block to allow hidden and/or system files. */
1276   if( is_filtered(data) ) goto next;
1277
1278   entry->d_ino = (ino_t)-1; /* not available */
1279   entry->d_attributes = data.attrib;
1280   strncpy(entry->d_name, data.name, NAME_MAX);
1281   entry->d_name[NAME_MAX] = '\0';
1282
1283   *result = entry;
1284   return 0;
1285 }
1286
1287 /*
1288 ** Implementation of the POSIX closedir() function using the MSVCRT.
1289 */
1290 INT closedir(
1291   LPDIR dirp
1292 ){
1293   INT result = 0;
1294
1295   if( dirp==NULL ) return EINVAL;
1296
1297   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1298     result = _findclose(dirp->d_handle);
1299   }
1300
1301   sqlite3_free(dirp);
1302   return result;
1303 }
1304
1305 #endif /* defined(WIN32) && defined(_MSC_VER) */
1306
1307 /************************* End test_windirent.c ********************/
1308 #define dirent DIRENT
1309 #endif
1310 /************************* Begin ../ext/misc/shathree.c ******************/
1311 /*
1312 ** 2017-03-08
1313 **
1314 ** The author disclaims copyright to this source code.  In place of
1315 ** a legal notice, here is a blessing:
1316 **
1317 **    May you do good and not evil.
1318 **    May you find forgiveness for yourself and forgive others.
1319 **    May you share freely, never taking more than you give.
1320 **
1321 ******************************************************************************
1322 **
1323 ** This SQLite extension implements functions that compute SHA3 hashes.
1324 ** Two SQL functions are implemented:
1325 **
1326 **     sha3(X,SIZE)
1327 **     sha3_query(Y,SIZE)
1328 **
1329 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1330 ** X is NULL.
1331 **
1332 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1333 ** and returns a hash of their results.
1334 **
1335 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1336 ** is used.  If SIZE is included it must be one of the integers 224, 256,
1337 ** 384, or 512, to determine SHA3 hash variant that is computed.
1338 */
1339 SQLITE_EXTENSION_INIT1
1340 #include <assert.h>
1341 #include <string.h>
1342 #include <stdarg.h>
1343 /* typedef sqlite3_uint64 u64; */
1344
1345 /******************************************************************************
1346 ** The Hash Engine
1347 */
1348 /*
1349 ** Macros to determine whether the machine is big or little endian,
1350 ** and whether or not that determination is run-time or compile-time.
1351 **
1352 ** For best performance, an attempt is made to guess at the byte-order
1353 ** using C-preprocessor macros.  If that is unsuccessful, or if
1354 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1355 ** at run-time.
1356 */
1357 #ifndef SHA3_BYTEORDER
1358 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1359      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1360      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1361      defined(__arm__)
1362 #   define SHA3_BYTEORDER    1234
1363 # elif defined(sparc)    || defined(__ppc__)
1364 #   define SHA3_BYTEORDER    4321
1365 # else
1366 #   define SHA3_BYTEORDER 0
1367 # endif
1368 #endif
1369
1370
1371 /*
1372 ** State structure for a SHA3 hash in progress
1373 */
1374 typedef struct SHA3Context SHA3Context;
1375 struct SHA3Context {
1376   union {
1377     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1378     unsigned char x[1600];    /* ... or 1600 bytes */
1379   } u;
1380   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1381   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1382   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1383 };
1384
1385 /*
1386 ** A single step of the Keccak mixing function for a 1600-bit state
1387 */
1388 static void KeccakF1600Step(SHA3Context *p){
1389   int i;
1390   u64 b0, b1, b2, b3, b4;
1391   u64 c0, c1, c2, c3, c4;
1392   u64 d0, d1, d2, d3, d4;
1393   static const u64 RC[] = {
1394     0x0000000000000001ULL,  0x0000000000008082ULL,
1395     0x800000000000808aULL,  0x8000000080008000ULL,
1396     0x000000000000808bULL,  0x0000000080000001ULL,
1397     0x8000000080008081ULL,  0x8000000000008009ULL,
1398     0x000000000000008aULL,  0x0000000000000088ULL,
1399     0x0000000080008009ULL,  0x000000008000000aULL,
1400     0x000000008000808bULL,  0x800000000000008bULL,
1401     0x8000000000008089ULL,  0x8000000000008003ULL,
1402     0x8000000000008002ULL,  0x8000000000000080ULL,
1403     0x000000000000800aULL,  0x800000008000000aULL,
1404     0x8000000080008081ULL,  0x8000000000008080ULL,
1405     0x0000000080000001ULL,  0x8000000080008008ULL
1406   };
1407 # define a00 (p->u.s[0])
1408 # define a01 (p->u.s[1])
1409 # define a02 (p->u.s[2])
1410 # define a03 (p->u.s[3])
1411 # define a04 (p->u.s[4])
1412 # define a10 (p->u.s[5])
1413 # define a11 (p->u.s[6])
1414 # define a12 (p->u.s[7])
1415 # define a13 (p->u.s[8])
1416 # define a14 (p->u.s[9])
1417 # define a20 (p->u.s[10])
1418 # define a21 (p->u.s[11])
1419 # define a22 (p->u.s[12])
1420 # define a23 (p->u.s[13])
1421 # define a24 (p->u.s[14])
1422 # define a30 (p->u.s[15])
1423 # define a31 (p->u.s[16])
1424 # define a32 (p->u.s[17])
1425 # define a33 (p->u.s[18])
1426 # define a34 (p->u.s[19])
1427 # define a40 (p->u.s[20])
1428 # define a41 (p->u.s[21])
1429 # define a42 (p->u.s[22])
1430 # define a43 (p->u.s[23])
1431 # define a44 (p->u.s[24])
1432 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1433
1434   for(i=0; i<24; i+=4){
1435     c0 = a00^a10^a20^a30^a40;
1436     c1 = a01^a11^a21^a31^a41;
1437     c2 = a02^a12^a22^a32^a42;
1438     c3 = a03^a13^a23^a33^a43;
1439     c4 = a04^a14^a24^a34^a44;
1440     d0 = c4^ROL64(c1, 1);
1441     d1 = c0^ROL64(c2, 1);
1442     d2 = c1^ROL64(c3, 1);
1443     d3 = c2^ROL64(c4, 1);
1444     d4 = c3^ROL64(c0, 1);
1445
1446     b0 = (a00^d0);
1447     b1 = ROL64((a11^d1), 44);
1448     b2 = ROL64((a22^d2), 43);
1449     b3 = ROL64((a33^d3), 21);
1450     b4 = ROL64((a44^d4), 14);
1451     a00 =   b0 ^((~b1)&  b2 );
1452     a00 ^= RC[i];
1453     a11 =   b1 ^((~b2)&  b3 );
1454     a22 =   b2 ^((~b3)&  b4 );
1455     a33 =   b3 ^((~b4)&  b0 );
1456     a44 =   b4 ^((~b0)&  b1 );
1457
1458     b2 = ROL64((a20^d0), 3);
1459     b3 = ROL64((a31^d1), 45);
1460     b4 = ROL64((a42^d2), 61);
1461     b0 = ROL64((a03^d3), 28);
1462     b1 = ROL64((a14^d4), 20);
1463     a20 =   b0 ^((~b1)&  b2 );
1464     a31 =   b1 ^((~b2)&  b3 );
1465     a42 =   b2 ^((~b3)&  b4 );
1466     a03 =   b3 ^((~b4)&  b0 );
1467     a14 =   b4 ^((~b0)&  b1 );
1468
1469     b4 = ROL64((a40^d0), 18);
1470     b0 = ROL64((a01^d1), 1);
1471     b1 = ROL64((a12^d2), 6);
1472     b2 = ROL64((a23^d3), 25);
1473     b3 = ROL64((a34^d4), 8);
1474     a40 =   b0 ^((~b1)&  b2 );
1475     a01 =   b1 ^((~b2)&  b3 );
1476     a12 =   b2 ^((~b3)&  b4 );
1477     a23 =   b3 ^((~b4)&  b0 );
1478     a34 =   b4 ^((~b0)&  b1 );
1479
1480     b1 = ROL64((a10^d0), 36);
1481     b2 = ROL64((a21^d1), 10);
1482     b3 = ROL64((a32^d2), 15);
1483     b4 = ROL64((a43^d3), 56);
1484     b0 = ROL64((a04^d4), 27);
1485     a10 =   b0 ^((~b1)&  b2 );
1486     a21 =   b1 ^((~b2)&  b3 );
1487     a32 =   b2 ^((~b3)&  b4 );
1488     a43 =   b3 ^((~b4)&  b0 );
1489     a04 =   b4 ^((~b0)&  b1 );
1490
1491     b3 = ROL64((a30^d0), 41);
1492     b4 = ROL64((a41^d1), 2);
1493     b0 = ROL64((a02^d2), 62);
1494     b1 = ROL64((a13^d3), 55);
1495     b2 = ROL64((a24^d4), 39);
1496     a30 =   b0 ^((~b1)&  b2 );
1497     a41 =   b1 ^((~b2)&  b3 );
1498     a02 =   b2 ^((~b3)&  b4 );
1499     a13 =   b3 ^((~b4)&  b0 );
1500     a24 =   b4 ^((~b0)&  b1 );
1501
1502     c0 = a00^a20^a40^a10^a30;
1503     c1 = a11^a31^a01^a21^a41;
1504     c2 = a22^a42^a12^a32^a02;
1505     c3 = a33^a03^a23^a43^a13;
1506     c4 = a44^a14^a34^a04^a24;
1507     d0 = c4^ROL64(c1, 1);
1508     d1 = c0^ROL64(c2, 1);
1509     d2 = c1^ROL64(c3, 1);
1510     d3 = c2^ROL64(c4, 1);
1511     d4 = c3^ROL64(c0, 1);
1512
1513     b0 = (a00^d0);
1514     b1 = ROL64((a31^d1), 44);
1515     b2 = ROL64((a12^d2), 43);
1516     b3 = ROL64((a43^d3), 21);
1517     b4 = ROL64((a24^d4), 14);
1518     a00 =   b0 ^((~b1)&  b2 );
1519     a00 ^= RC[i+1];
1520     a31 =   b1 ^((~b2)&  b3 );
1521     a12 =   b2 ^((~b3)&  b4 );
1522     a43 =   b3 ^((~b4)&  b0 );
1523     a24 =   b4 ^((~b0)&  b1 );
1524
1525     b2 = ROL64((a40^d0), 3);
1526     b3 = ROL64((a21^d1), 45);
1527     b4 = ROL64((a02^d2), 61);
1528     b0 = ROL64((a33^d3), 28);
1529     b1 = ROL64((a14^d4), 20);
1530     a40 =   b0 ^((~b1)&  b2 );
1531     a21 =   b1 ^((~b2)&  b3 );
1532     a02 =   b2 ^((~b3)&  b4 );
1533     a33 =   b3 ^((~b4)&  b0 );
1534     a14 =   b4 ^((~b0)&  b1 );
1535
1536     b4 = ROL64((a30^d0), 18);
1537     b0 = ROL64((a11^d1), 1);
1538     b1 = ROL64((a42^d2), 6);
1539     b2 = ROL64((a23^d3), 25);
1540     b3 = ROL64((a04^d4), 8);
1541     a30 =   b0 ^((~b1)&  b2 );
1542     a11 =   b1 ^((~b2)&  b3 );
1543     a42 =   b2 ^((~b3)&  b4 );
1544     a23 =   b3 ^((~b4)&  b0 );
1545     a04 =   b4 ^((~b0)&  b1 );
1546
1547     b1 = ROL64((a20^d0), 36);
1548     b2 = ROL64((a01^d1), 10);
1549     b3 = ROL64((a32^d2), 15);
1550     b4 = ROL64((a13^d3), 56);
1551     b0 = ROL64((a44^d4), 27);
1552     a20 =   b0 ^((~b1)&  b2 );
1553     a01 =   b1 ^((~b2)&  b3 );
1554     a32 =   b2 ^((~b3)&  b4 );
1555     a13 =   b3 ^((~b4)&  b0 );
1556     a44 =   b4 ^((~b0)&  b1 );
1557
1558     b3 = ROL64((a10^d0), 41);
1559     b4 = ROL64((a41^d1), 2);
1560     b0 = ROL64((a22^d2), 62);
1561     b1 = ROL64((a03^d3), 55);
1562     b2 = ROL64((a34^d4), 39);
1563     a10 =   b0 ^((~b1)&  b2 );
1564     a41 =   b1 ^((~b2)&  b3 );
1565     a22 =   b2 ^((~b3)&  b4 );
1566     a03 =   b3 ^((~b4)&  b0 );
1567     a34 =   b4 ^((~b0)&  b1 );
1568
1569     c0 = a00^a40^a30^a20^a10;
1570     c1 = a31^a21^a11^a01^a41;
1571     c2 = a12^a02^a42^a32^a22;
1572     c3 = a43^a33^a23^a13^a03;
1573     c4 = a24^a14^a04^a44^a34;
1574     d0 = c4^ROL64(c1, 1);
1575     d1 = c0^ROL64(c2, 1);
1576     d2 = c1^ROL64(c3, 1);
1577     d3 = c2^ROL64(c4, 1);
1578     d4 = c3^ROL64(c0, 1);
1579
1580     b0 = (a00^d0);
1581     b1 = ROL64((a21^d1), 44);
1582     b2 = ROL64((a42^d2), 43);
1583     b3 = ROL64((a13^d3), 21);
1584     b4 = ROL64((a34^d4), 14);
1585     a00 =   b0 ^((~b1)&  b2 );
1586     a00 ^= RC[i+2];
1587     a21 =   b1 ^((~b2)&  b3 );
1588     a42 =   b2 ^((~b3)&  b4 );
1589     a13 =   b3 ^((~b4)&  b0 );
1590     a34 =   b4 ^((~b0)&  b1 );
1591
1592     b2 = ROL64((a30^d0), 3);
1593     b3 = ROL64((a01^d1), 45);
1594     b4 = ROL64((a22^d2), 61);
1595     b0 = ROL64((a43^d3), 28);
1596     b1 = ROL64((a14^d4), 20);
1597     a30 =   b0 ^((~b1)&  b2 );
1598     a01 =   b1 ^((~b2)&  b3 );
1599     a22 =   b2 ^((~b3)&  b4 );
1600     a43 =   b3 ^((~b4)&  b0 );
1601     a14 =   b4 ^((~b0)&  b1 );
1602
1603     b4 = ROL64((a10^d0), 18);
1604     b0 = ROL64((a31^d1), 1);
1605     b1 = ROL64((a02^d2), 6);
1606     b2 = ROL64((a23^d3), 25);
1607     b3 = ROL64((a44^d4), 8);
1608     a10 =   b0 ^((~b1)&  b2 );
1609     a31 =   b1 ^((~b2)&  b3 );
1610     a02 =   b2 ^((~b3)&  b4 );
1611     a23 =   b3 ^((~b4)&  b0 );
1612     a44 =   b4 ^((~b0)&  b1 );
1613
1614     b1 = ROL64((a40^d0), 36);
1615     b2 = ROL64((a11^d1), 10);
1616     b3 = ROL64((a32^d2), 15);
1617     b4 = ROL64((a03^d3), 56);
1618     b0 = ROL64((a24^d4), 27);
1619     a40 =   b0 ^((~b1)&  b2 );
1620     a11 =   b1 ^((~b2)&  b3 );
1621     a32 =   b2 ^((~b3)&  b4 );
1622     a03 =   b3 ^((~b4)&  b0 );
1623     a24 =   b4 ^((~b0)&  b1 );
1624
1625     b3 = ROL64((a20^d0), 41);
1626     b4 = ROL64((a41^d1), 2);
1627     b0 = ROL64((a12^d2), 62);
1628     b1 = ROL64((a33^d3), 55);
1629     b2 = ROL64((a04^d4), 39);
1630     a20 =   b0 ^((~b1)&  b2 );
1631     a41 =   b1 ^((~b2)&  b3 );
1632     a12 =   b2 ^((~b3)&  b4 );
1633     a33 =   b3 ^((~b4)&  b0 );
1634     a04 =   b4 ^((~b0)&  b1 );
1635
1636     c0 = a00^a30^a10^a40^a20;
1637     c1 = a21^a01^a31^a11^a41;
1638     c2 = a42^a22^a02^a32^a12;
1639     c3 = a13^a43^a23^a03^a33;
1640     c4 = a34^a14^a44^a24^a04;
1641     d0 = c4^ROL64(c1, 1);
1642     d1 = c0^ROL64(c2, 1);
1643     d2 = c1^ROL64(c3, 1);
1644     d3 = c2^ROL64(c4, 1);
1645     d4 = c3^ROL64(c0, 1);
1646
1647     b0 = (a00^d0);
1648     b1 = ROL64((a01^d1), 44);
1649     b2 = ROL64((a02^d2), 43);
1650     b3 = ROL64((a03^d3), 21);
1651     b4 = ROL64((a04^d4), 14);
1652     a00 =   b0 ^((~b1)&  b2 );
1653     a00 ^= RC[i+3];
1654     a01 =   b1 ^((~b2)&  b3 );
1655     a02 =   b2 ^((~b3)&  b4 );
1656     a03 =   b3 ^((~b4)&  b0 );
1657     a04 =   b4 ^((~b0)&  b1 );
1658
1659     b2 = ROL64((a10^d0), 3);
1660     b3 = ROL64((a11^d1), 45);
1661     b4 = ROL64((a12^d2), 61);
1662     b0 = ROL64((a13^d3), 28);
1663     b1 = ROL64((a14^d4), 20);
1664     a10 =   b0 ^((~b1)&  b2 );
1665     a11 =   b1 ^((~b2)&  b3 );
1666     a12 =   b2 ^((~b3)&  b4 );
1667     a13 =   b3 ^((~b4)&  b0 );
1668     a14 =   b4 ^((~b0)&  b1 );
1669
1670     b4 = ROL64((a20^d0), 18);
1671     b0 = ROL64((a21^d1), 1);
1672     b1 = ROL64((a22^d2), 6);
1673     b2 = ROL64((a23^d3), 25);
1674     b3 = ROL64((a24^d4), 8);
1675     a20 =   b0 ^((~b1)&  b2 );
1676     a21 =   b1 ^((~b2)&  b3 );
1677     a22 =   b2 ^((~b3)&  b4 );
1678     a23 =   b3 ^((~b4)&  b0 );
1679     a24 =   b4 ^((~b0)&  b1 );
1680
1681     b1 = ROL64((a30^d0), 36);
1682     b2 = ROL64((a31^d1), 10);
1683     b3 = ROL64((a32^d2), 15);
1684     b4 = ROL64((a33^d3), 56);
1685     b0 = ROL64((a34^d4), 27);
1686     a30 =   b0 ^((~b1)&  b2 );
1687     a31 =   b1 ^((~b2)&  b3 );
1688     a32 =   b2 ^((~b3)&  b4 );
1689     a33 =   b3 ^((~b4)&  b0 );
1690     a34 =   b4 ^((~b0)&  b1 );
1691
1692     b3 = ROL64((a40^d0), 41);
1693     b4 = ROL64((a41^d1), 2);
1694     b0 = ROL64((a42^d2), 62);
1695     b1 = ROL64((a43^d3), 55);
1696     b2 = ROL64((a44^d4), 39);
1697     a40 =   b0 ^((~b1)&  b2 );
1698     a41 =   b1 ^((~b2)&  b3 );
1699     a42 =   b2 ^((~b3)&  b4 );
1700     a43 =   b3 ^((~b4)&  b0 );
1701     a44 =   b4 ^((~b0)&  b1 );
1702   }
1703 }
1704
1705 /*
1706 ** Initialize a new hash.  iSize determines the size of the hash
1707 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1708 ** can be zero to use the default hash size of 256 bits.
1709 */
1710 static void SHA3Init(SHA3Context *p, int iSize){
1711   memset(p, 0, sizeof(*p));
1712   if( iSize>=128 && iSize<=512 ){
1713     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1714   }else{
1715     p->nRate = (1600 - 2*256)/8;
1716   }
1717 #if SHA3_BYTEORDER==1234
1718   /* Known to be little-endian at compile-time. No-op */
1719 #elif SHA3_BYTEORDER==4321
1720   p->ixMask = 7;  /* Big-endian */
1721 #else
1722   {
1723     static unsigned int one = 1;
1724     if( 1==*(unsigned char*)&one ){
1725       /* Little endian.  No byte swapping. */
1726       p->ixMask = 0;
1727     }else{
1728       /* Big endian.  Byte swap. */
1729       p->ixMask = 7;
1730     }
1731   }
1732 #endif
1733 }
1734
1735 /*
1736 ** Make consecutive calls to the SHA3Update function to add new content
1737 ** to the hash
1738 */
1739 static void SHA3Update(
1740   SHA3Context *p,
1741   const unsigned char *aData,
1742   unsigned int nData
1743 ){
1744   unsigned int i = 0;
1745 #if SHA3_BYTEORDER==1234
1746   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1747     for(; i+7<nData; i+=8){
1748       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1749       p->nLoaded += 8;
1750       if( p->nLoaded>=p->nRate ){
1751         KeccakF1600Step(p);
1752         p->nLoaded = 0;
1753       }
1754     }
1755   }
1756 #endif
1757   for(; i<nData; i++){
1758 #if SHA3_BYTEORDER==1234
1759     p->u.x[p->nLoaded] ^= aData[i];
1760 #elif SHA3_BYTEORDER==4321
1761     p->u.x[p->nLoaded^0x07] ^= aData[i];
1762 #else
1763     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1764 #endif
1765     p->nLoaded++;
1766     if( p->nLoaded==p->nRate ){
1767       KeccakF1600Step(p);
1768       p->nLoaded = 0;
1769     }
1770   }
1771 }
1772
1773 /*
1774 ** After all content has been added, invoke SHA3Final() to compute
1775 ** the final hash.  The function returns a pointer to the binary
1776 ** hash value.
1777 */
1778 static unsigned char *SHA3Final(SHA3Context *p){
1779   unsigned int i;
1780   if( p->nLoaded==p->nRate-1 ){
1781     const unsigned char c1 = 0x86;
1782     SHA3Update(p, &c1, 1);
1783   }else{
1784     const unsigned char c2 = 0x06;
1785     const unsigned char c3 = 0x80;
1786     SHA3Update(p, &c2, 1);
1787     p->nLoaded = p->nRate - 1;
1788     SHA3Update(p, &c3, 1);
1789   }
1790   for(i=0; i<p->nRate; i++){
1791     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1792   }
1793   return &p->u.x[p->nRate];
1794 }
1795 /* End of the hashing logic
1796 *****************************************************************************/
1797
1798 /*
1799 ** Implementation of the sha3(X,SIZE) function.
1800 **
1801 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1802 ** size is 256.  If X is a BLOB, it is hashed as is.  
1803 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1804 ** and the string is hashed without the trailing 0x00 terminator.  The hash
1805 ** of a NULL value is NULL.
1806 */
1807 static void sha3Func(
1808   sqlite3_context *context,
1809   int argc,
1810   sqlite3_value **argv
1811 ){
1812   SHA3Context cx;
1813   int eType = sqlite3_value_type(argv[0]);
1814   int nByte = sqlite3_value_bytes(argv[0]);
1815   int iSize;
1816   if( argc==1 ){
1817     iSize = 256;
1818   }else{
1819     iSize = sqlite3_value_int(argv[1]);
1820     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1821       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1822                                     "384 512", -1);
1823       return;
1824     }
1825   }
1826   if( eType==SQLITE_NULL ) return;
1827   SHA3Init(&cx, iSize);
1828   if( eType==SQLITE_BLOB ){
1829     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1830   }else{
1831     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1832   }
1833   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1834 }
1835
1836 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1837 ** of 50 bytes and add it to the hash.
1838 */
1839 static void hash_step_vformat(
1840   SHA3Context *p,                 /* Add content to this context */
1841   const char *zFormat,
1842   ...
1843 ){
1844   va_list ap;
1845   int n;
1846   char zBuf[50];
1847   va_start(ap, zFormat);
1848   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1849   va_end(ap);
1850   n = (int)strlen(zBuf);
1851   SHA3Update(p, (unsigned char*)zBuf, n);
1852 }
1853
1854 /*
1855 ** Implementation of the sha3_query(SQL,SIZE) function.
1856 **
1857 ** This function compiles and runs the SQL statement(s) given in the
1858 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
1859 ** size is 256.
1860 **
1861 ** The format of the byte stream that is hashed is summarized as follows:
1862 **
1863 **       S<n>:<sql>
1864 **       R
1865 **       N
1866 **       I<int>
1867 **       F<ieee-float>
1868 **       B<size>:<bytes>
1869 **       T<size>:<text>
1870 **
1871 ** <sql> is the original SQL text for each statement run and <n> is
1872 ** the size of that text.  The SQL text is UTF-8.  A single R character
1873 ** occurs before the start of each row.  N means a NULL value.
1874 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
1875 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1876 ** B means blobs of <size> bytes.  T means text rendered as <size>
1877 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1878 ** text integers.
1879 **
1880 ** For each SQL statement in the X input, there is one S segment.  Each
1881 ** S segment is followed by zero or more R segments, one for each row in the
1882 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
1883 ** one for each column in the result set.  Segments are concatentated directly
1884 ** with no delimiters of any kind.
1885 */
1886 static void sha3QueryFunc(
1887   sqlite3_context *context,
1888   int argc,
1889   sqlite3_value **argv
1890 ){
1891   sqlite3 *db = sqlite3_context_db_handle(context);
1892   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1893   sqlite3_stmt *pStmt = 0;
1894   int nCol;                   /* Number of columns in the result set */
1895   int i;                      /* Loop counter */
1896   int rc;
1897   int n;
1898   const char *z;
1899   SHA3Context cx;
1900   int iSize;
1901
1902   if( argc==1 ){
1903     iSize = 256;
1904   }else{
1905     iSize = sqlite3_value_int(argv[1]);
1906     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1907       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1908                                     "384 512", -1);
1909       return;
1910     }
1911   }
1912   if( zSql==0 ) return;
1913   SHA3Init(&cx, iSize);
1914   while( zSql[0] ){
1915     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1916     if( rc ){
1917       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1918                                    zSql, sqlite3_errmsg(db));
1919       sqlite3_finalize(pStmt);
1920       sqlite3_result_error(context, zMsg, -1);
1921       sqlite3_free(zMsg);
1922       return;
1923     }
1924     if( !sqlite3_stmt_readonly(pStmt) ){
1925       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1926       sqlite3_finalize(pStmt);
1927       sqlite3_result_error(context, zMsg, -1);
1928       sqlite3_free(zMsg);
1929       return;
1930     }
1931     nCol = sqlite3_column_count(pStmt);
1932     z = sqlite3_sql(pStmt);
1933     n = (int)strlen(z);
1934     hash_step_vformat(&cx,"S%d:",n);
1935     SHA3Update(&cx,(unsigned char*)z,n);
1936
1937     /* Compute a hash over the result of the query */
1938     while( SQLITE_ROW==sqlite3_step(pStmt) ){
1939       SHA3Update(&cx,(const unsigned char*)"R",1);
1940       for(i=0; i<nCol; i++){
1941         switch( sqlite3_column_type(pStmt,i) ){
1942           case SQLITE_NULL: {
1943             SHA3Update(&cx, (const unsigned char*)"N",1);
1944             break;
1945           }
1946           case SQLITE_INTEGER: {
1947             sqlite3_uint64 u;
1948             int j;
1949             unsigned char x[9];
1950             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1951             memcpy(&u, &v, 8);
1952             for(j=8; j>=1; j--){
1953               x[j] = u & 0xff;
1954               u >>= 8;
1955             }
1956             x[0] = 'I';
1957             SHA3Update(&cx, x, 9);
1958             break;
1959           }
1960           case SQLITE_FLOAT: {
1961             sqlite3_uint64 u;
1962             int j;
1963             unsigned char x[9];
1964             double r = sqlite3_column_double(pStmt,i);
1965             memcpy(&u, &r, 8);
1966             for(j=8; j>=1; j--){
1967               x[j] = u & 0xff;
1968               u >>= 8;
1969             }
1970             x[0] = 'F';
1971             SHA3Update(&cx,x,9);
1972             break;
1973           }
1974           case SQLITE_TEXT: {
1975             int n2 = sqlite3_column_bytes(pStmt, i);
1976             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1977             hash_step_vformat(&cx,"T%d:",n2);
1978             SHA3Update(&cx, z2, n2);
1979             break;
1980           }
1981           case SQLITE_BLOB: {
1982             int n2 = sqlite3_column_bytes(pStmt, i);
1983             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1984             hash_step_vformat(&cx,"B%d:",n2);
1985             SHA3Update(&cx, z2, n2);
1986             break;
1987           }
1988         }
1989       }
1990     }
1991     sqlite3_finalize(pStmt);
1992   }
1993   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1994 }
1995
1996
1997 #ifdef _WIN32
1998
1999 #endif
2000 int sqlite3_shathree_init(
2001   sqlite3 *db,
2002   char **pzErrMsg,
2003   const sqlite3_api_routines *pApi
2004 ){
2005   int rc = SQLITE_OK;
2006   SQLITE_EXTENSION_INIT2(pApi);
2007   (void)pzErrMsg;  /* Unused parameter */
2008   rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2009                                sha3Func, 0, 0);
2010   if( rc==SQLITE_OK ){
2011     rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2012                                  sha3Func, 0, 0);
2013   }
2014   if( rc==SQLITE_OK ){
2015     rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2016                                  sha3QueryFunc, 0, 0);
2017   }
2018   if( rc==SQLITE_OK ){
2019     rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2020                                  sha3QueryFunc, 0, 0);
2021   }
2022   return rc;
2023 }
2024
2025 /************************* End ../ext/misc/shathree.c ********************/
2026 /************************* Begin ../ext/misc/fileio.c ******************/
2027 /*
2028 ** 2014-06-13
2029 **
2030 ** The author disclaims copyright to this source code.  In place of
2031 ** a legal notice, here is a blessing:
2032 **
2033 **    May you do good and not evil.
2034 **    May you find forgiveness for yourself and forgive others.
2035 **    May you share freely, never taking more than you give.
2036 **
2037 ******************************************************************************
2038 **
2039 ** This SQLite extension implements SQL functions readfile() and
2040 ** writefile(), and eponymous virtual type "fsdir".
2041 **
2042 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2043 **
2044 **   If neither of the optional arguments is present, then this UDF
2045 **   function writes blob DATA to file FILE. If successful, the number
2046 **   of bytes written is returned. If an error occurs, NULL is returned.
2047 **
2048 **   If the first option argument - MODE - is present, then it must
2049 **   be passed an integer value that corresponds to a POSIX mode
2050 **   value (file type + permissions, as returned in the stat.st_mode
2051 **   field by the stat() system call). Three types of files may
2052 **   be written/created:
2053 **
2054 **     regular files:  (mode & 0170000)==0100000
2055 **     symbolic links: (mode & 0170000)==0120000
2056 **     directories:    (mode & 0170000)==0040000
2057 **
2058 **   For a directory, the DATA is ignored. For a symbolic link, it is
2059 **   interpreted as text and used as the target of the link. For a
2060 **   regular file, it is interpreted as a blob and written into the
2061 **   named file. Regardless of the type of file, its permissions are
2062 **   set to (mode & 0777) before returning.
2063 **
2064 **   If the optional MTIME argument is present, then it is interpreted
2065 **   as an integer - the number of seconds since the unix epoch. The
2066 **   modification-time of the target file is set to this value before
2067 **   returning.
2068 **
2069 **   If three or more arguments are passed to this function and an
2070 **   error is encountered, an exception is raised.
2071 **
2072 ** READFILE(FILE):
2073 **
2074 **   Read and return the contents of file FILE (type blob) from disk.
2075 **
2076 ** FSDIR:
2077 **
2078 **   Used as follows:
2079 **
2080 **     SELECT * FROM fsdir($path [, $dir]);
2081 **
2082 **   Parameter $path is an absolute or relative pathname. If the file that it
2083 **   refers to does not exist, it is an error. If the path refers to a regular
2084 **   file or symbolic link, it returns a single row. Or, if the path refers
2085 **   to a directory, it returns one row for the directory, and one row for each
2086 **   file within the hierarchy rooted at $path.
2087 **
2088 **   Each row has the following columns:
2089 **
2090 **     name:  Path to file or directory (text value).
2091 **     mode:  Value of stat.st_mode for directory entry (an integer).
2092 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2093 **     data:  For a regular file, a blob containing the file data. For a
2094 **            symlink, a text value containing the text of the link. For a
2095 **            directory, NULL.
2096 **
2097 **   If a non-NULL value is specified for the optional $dir parameter and
2098 **   $path is a relative path, then $path is interpreted relative to $dir. 
2099 **   And the paths returned in the "name" column of the table are also 
2100 **   relative to directory $dir.
2101 */
2102 SQLITE_EXTENSION_INIT1
2103 #include <stdio.h>
2104 #include <string.h>
2105 #include <assert.h>
2106
2107 #include <sys/types.h>
2108 #include <sys/stat.h>
2109 #include <fcntl.h>
2110 #if !defined(_WIN32) && !defined(WIN32)
2111 #  include <unistd.h>
2112 #  include <dirent.h>
2113 #  include <utime.h>
2114 #  include <sys/time.h>
2115 #else
2116 #  include "windows.h"
2117 #  include <io.h>
2118 #  include <direct.h>
2119 /* #  include "test_windirent.h" */
2120 #  define dirent DIRENT
2121 #  ifndef chmod
2122 #    define chmod _chmod
2123 #  endif
2124 #  ifndef stat
2125 #    define stat _stat
2126 #  endif
2127 #  define mkdir(path,mode) _mkdir(path)
2128 #  define lstat(path,buf) stat(path,buf)
2129 #endif
2130 #include <time.h>
2131 #include <errno.h>
2132
2133
2134 /*
2135 ** Structure of the fsdir() table-valued function
2136 */
2137                  /*    0    1    2     3    4           5             */
2138 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2139 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
2140 #define FSDIR_COLUMN_MODE     1     /* Access mode */
2141 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2142 #define FSDIR_COLUMN_DATA     3     /* File content */
2143 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2144 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2145
2146
2147 /*
2148 ** Set the result stored by context ctx to a blob containing the 
2149 ** contents of file zName.  Or, leave the result unchanged (NULL)
2150 ** if the file does not exist or is unreadable.
2151 **
2152 ** If the file exceeds the SQLite blob size limit, through an
2153 ** SQLITE_TOOBIG error.
2154 **
2155 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2156 ** off of disk.
2157 */
2158 static void readFileContents(sqlite3_context *ctx, const char *zName){
2159   FILE *in;
2160   sqlite3_int64 nIn;
2161   void *pBuf;
2162   sqlite3 *db;
2163   int mxBlob;
2164
2165   in = fopen(zName, "rb");
2166   if( in==0 ){
2167     /* File does not exist or is unreadable. Leave the result set to NULL. */
2168     return;
2169   }
2170   fseek(in, 0, SEEK_END);
2171   nIn = ftell(in);
2172   rewind(in);
2173   db = sqlite3_context_db_handle(ctx);
2174   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2175   if( nIn>mxBlob ){
2176     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2177     fclose(in);
2178     return;
2179   }
2180   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2181   if( pBuf==0 ){
2182     sqlite3_result_error_nomem(ctx);
2183     fclose(in);
2184     return;
2185   }
2186   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2187     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2188   }else{
2189     sqlite3_result_error_code(ctx, SQLITE_IOERR);
2190     sqlite3_free(pBuf);
2191   }
2192   fclose(in);
2193 }
2194
2195 /*
2196 ** Implementation of the "readfile(X)" SQL function.  The entire content
2197 ** of the file named X is read and returned as a BLOB.  NULL is returned
2198 ** if the file does not exist or is unreadable.
2199 */
2200 static void readfileFunc(
2201   sqlite3_context *context,
2202   int argc,
2203   sqlite3_value **argv
2204 ){
2205   const char *zName;
2206   (void)(argc);  /* Unused parameter */
2207   zName = (const char*)sqlite3_value_text(argv[0]);
2208   if( zName==0 ) return;
2209   readFileContents(context, zName);
2210 }
2211
2212 /*
2213 ** Set the error message contained in context ctx to the results of
2214 ** vprintf(zFmt, ...).
2215 */
2216 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2217   char *zMsg = 0;
2218   va_list ap;
2219   va_start(ap, zFmt);
2220   zMsg = sqlite3_vmprintf(zFmt, ap);
2221   sqlite3_result_error(ctx, zMsg, -1);
2222   sqlite3_free(zMsg);
2223   va_end(ap);
2224 }
2225
2226 #if defined(_WIN32)
2227 /*
2228 ** This function is designed to convert a Win32 FILETIME structure into the
2229 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2230 */
2231 static sqlite3_uint64 fileTimeToUnixTime(
2232   LPFILETIME pFileTime
2233 ){
2234   SYSTEMTIME epochSystemTime;
2235   ULARGE_INTEGER epochIntervals;
2236   FILETIME epochFileTime;
2237   ULARGE_INTEGER fileIntervals;
2238
2239   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2240   epochSystemTime.wYear = 1970;
2241   epochSystemTime.wMonth = 1;
2242   epochSystemTime.wDay = 1;
2243   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2244   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2245   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2246
2247   fileIntervals.LowPart = pFileTime->dwLowDateTime;
2248   fileIntervals.HighPart = pFileTime->dwHighDateTime;
2249
2250   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2251 }
2252
2253 /*
2254 ** This function attempts to normalize the time values found in the stat()
2255 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2256 ** appears to return these values as local times.
2257 */
2258 static void statTimesToUtc(
2259   const char *zPath,
2260   struct stat *pStatBuf
2261 ){
2262   HANDLE hFindFile;
2263   WIN32_FIND_DATAW fd;
2264   LPWSTR zUnicodeName;
2265   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2266   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2267   if( zUnicodeName ){
2268     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2269     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2270     if( hFindFile!=NULL ){
2271       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2272       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2273       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2274       FindClose(hFindFile);
2275     }
2276     sqlite3_free(zUnicodeName);
2277   }
2278 }
2279 #endif
2280
2281 /*
2282 ** This function is used in place of stat().  On Windows, special handling
2283 ** is required in order for the included time to be returned as UTC.  On all
2284 ** other systems, this function simply calls stat().
2285 */
2286 static int fileStat(
2287   const char *zPath,
2288   struct stat *pStatBuf
2289 ){
2290 #if defined(_WIN32)
2291   int rc = stat(zPath, pStatBuf);
2292   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2293   return rc;
2294 #else
2295   return stat(zPath, pStatBuf);
2296 #endif
2297 }
2298
2299 /*
2300 ** This function is used in place of lstat().  On Windows, special handling
2301 ** is required in order for the included time to be returned as UTC.  On all
2302 ** other systems, this function simply calls lstat().
2303 */
2304 static int fileLinkStat(
2305   const char *zPath,
2306   struct stat *pStatBuf
2307 ){
2308 #if defined(_WIN32)
2309   int rc = lstat(zPath, pStatBuf);
2310   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2311   return rc;
2312 #else
2313   return lstat(zPath, pStatBuf);
2314 #endif
2315 }
2316
2317 /*
2318 ** Argument zFile is the name of a file that will be created and/or written
2319 ** by SQL function writefile(). This function ensures that the directory
2320 ** zFile will be written to exists, creating it if required. The permissions
2321 ** for any path components created by this function are set in accordance
2322 ** with the current umask.
2323 **
2324 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2325 ** SQLITE_OK is returned if the directory is successfully created, or
2326 ** SQLITE_ERROR otherwise.
2327 */
2328 static int makeDirectory(
2329   const char *zFile
2330 ){
2331   char *zCopy = sqlite3_mprintf("%s", zFile);
2332   int rc = SQLITE_OK;
2333
2334   if( zCopy==0 ){
2335     rc = SQLITE_NOMEM;
2336   }else{
2337     int nCopy = (int)strlen(zCopy);
2338     int i = 1;
2339
2340     while( rc==SQLITE_OK ){
2341       struct stat sStat;
2342       int rc2;
2343
2344       for(; zCopy[i]!='/' && i<nCopy; i++);
2345       if( i==nCopy ) break;
2346       zCopy[i] = '\0';
2347
2348       rc2 = fileStat(zCopy, &sStat);
2349       if( rc2!=0 ){
2350         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2351       }else{
2352         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2353       }
2354       zCopy[i] = '/';
2355       i++;
2356     }
2357
2358     sqlite3_free(zCopy);
2359   }
2360
2361   return rc;
2362 }
2363
2364 /*
2365 ** This function does the work for the writefile() UDF. Refer to 
2366 ** header comments at the top of this file for details.
2367 */
2368 static int writeFile(
2369   sqlite3_context *pCtx,          /* Context to return bytes written in */
2370   const char *zFile,              /* File to write */
2371   sqlite3_value *pData,           /* Data to write */
2372   mode_t mode,                    /* MODE parameter passed to writefile() */
2373   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2374 ){
2375 #if !defined(_WIN32) && !defined(WIN32)
2376   if( S_ISLNK(mode) ){
2377     const char *zTo = (const char*)sqlite3_value_text(pData);
2378     if( symlink(zTo, zFile)<0 ) return 1;
2379   }else
2380 #endif
2381   {
2382     if( S_ISDIR(mode) ){
2383       if( mkdir(zFile, mode) ){
2384         /* The mkdir() call to create the directory failed. This might not
2385         ** be an error though - if there is already a directory at the same
2386         ** path and either the permissions already match or can be changed
2387         ** to do so using chmod(), it is not an error.  */
2388         struct stat sStat;
2389         if( errno!=EEXIST
2390          || 0!=fileStat(zFile, &sStat)
2391          || !S_ISDIR(sStat.st_mode)
2392          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2393         ){
2394           return 1;
2395         }
2396       }
2397     }else{
2398       sqlite3_int64 nWrite = 0;
2399       const char *z;
2400       int rc = 0;
2401       FILE *out = fopen(zFile, "wb");
2402       if( out==0 ) return 1;
2403       z = (const char*)sqlite3_value_blob(pData);
2404       if( z ){
2405         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2406         nWrite = sqlite3_value_bytes(pData);
2407         if( nWrite!=n ){
2408           rc = 1;
2409         }
2410       }
2411       fclose(out);
2412       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2413         rc = 1;
2414       }
2415       if( rc ) return 2;
2416       sqlite3_result_int64(pCtx, nWrite);
2417     }
2418   }
2419
2420   if( mtime>=0 ){
2421 #if defined(_WIN32)
2422     /* Windows */
2423     FILETIME lastAccess;
2424     FILETIME lastWrite;
2425     SYSTEMTIME currentTime;
2426     LONGLONG intervals;
2427     HANDLE hFile;
2428     LPWSTR zUnicodeName;
2429     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2430
2431     GetSystemTime(&currentTime);
2432     SystemTimeToFileTime(&currentTime, &lastAccess);
2433     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2434     lastWrite.dwLowDateTime = (DWORD)intervals;
2435     lastWrite.dwHighDateTime = intervals >> 32;
2436     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2437     if( zUnicodeName==0 ){
2438       return 1;
2439     }
2440     hFile = CreateFileW(
2441       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2442       FILE_FLAG_BACKUP_SEMANTICS, NULL
2443     );
2444     sqlite3_free(zUnicodeName);
2445     if( hFile!=INVALID_HANDLE_VALUE ){
2446       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2447       CloseHandle(hFile);
2448       return !bResult;
2449     }else{
2450       return 1;
2451     }
2452 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2453     /* Recent unix */
2454     struct timespec times[2];
2455     times[0].tv_nsec = times[1].tv_nsec = 0;
2456     times[0].tv_sec = time(0);
2457     times[1].tv_sec = mtime;
2458     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2459       return 1;
2460     }
2461 #else
2462     /* Legacy unix */
2463     struct timeval times[2];
2464     times[0].tv_usec = times[1].tv_usec = 0;
2465     times[0].tv_sec = time(0);
2466     times[1].tv_sec = mtime;
2467     if( utimes(zFile, times) ){
2468       return 1;
2469     }
2470 #endif
2471   }
2472
2473   return 0;
2474 }
2475
2476 /*
2477 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.  
2478 ** Refer to header comments at the top of this file for details.
2479 */
2480 static void writefileFunc(
2481   sqlite3_context *context,
2482   int argc,
2483   sqlite3_value **argv
2484 ){
2485   const char *zFile;
2486   mode_t mode = 0;
2487   int res;
2488   sqlite3_int64 mtime = -1;
2489
2490   if( argc<2 || argc>4 ){
2491     sqlite3_result_error(context, 
2492         "wrong number of arguments to function writefile()", -1
2493     );
2494     return;
2495   }
2496
2497   zFile = (const char*)sqlite3_value_text(argv[0]);
2498   if( zFile==0 ) return;
2499   if( argc>=3 ){
2500     mode = (mode_t)sqlite3_value_int(argv[2]);
2501   }
2502   if( argc==4 ){
2503     mtime = sqlite3_value_int64(argv[3]);
2504   }
2505
2506   res = writeFile(context, zFile, argv[1], mode, mtime);
2507   if( res==1 && errno==ENOENT ){
2508     if( makeDirectory(zFile)==SQLITE_OK ){
2509       res = writeFile(context, zFile, argv[1], mode, mtime);
2510     }
2511   }
2512
2513   if( argc>2 && res!=0 ){
2514     if( S_ISLNK(mode) ){
2515       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2516     }else if( S_ISDIR(mode) ){
2517       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2518     }else{
2519       ctxErrorMsg(context, "failed to write file: %s", zFile);
2520     }
2521   }
2522 }
2523
2524 /*
2525 ** SQL function:   lsmode(MODE)
2526 **
2527 ** Given a numberic st_mode from stat(), convert it into a human-readable
2528 ** text string in the style of "ls -l".
2529 */
2530 static void lsModeFunc(
2531   sqlite3_context *context,
2532   int argc,
2533   sqlite3_value **argv
2534 ){
2535   int i;
2536   int iMode = sqlite3_value_int(argv[0]);
2537   char z[16];
2538   (void)argc;
2539   if( S_ISLNK(iMode) ){
2540     z[0] = 'l';
2541   }else if( S_ISREG(iMode) ){
2542     z[0] = '-';
2543   }else if( S_ISDIR(iMode) ){
2544     z[0] = 'd';
2545   }else{
2546     z[0] = '?';
2547   }
2548   for(i=0; i<3; i++){
2549     int m = (iMode >> ((2-i)*3));
2550     char *a = &z[1 + i*3];
2551     a[0] = (m & 0x4) ? 'r' : '-';
2552     a[1] = (m & 0x2) ? 'w' : '-';
2553     a[2] = (m & 0x1) ? 'x' : '-';
2554   }
2555   z[10] = '\0';
2556   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2557 }
2558
2559 #ifndef SQLITE_OMIT_VIRTUALTABLE
2560
2561 /* 
2562 ** Cursor type for recursively iterating through a directory structure.
2563 */
2564 typedef struct fsdir_cursor fsdir_cursor;
2565 typedef struct FsdirLevel FsdirLevel;
2566
2567 struct FsdirLevel {
2568   DIR *pDir;                 /* From opendir() */
2569   char *zDir;                /* Name of directory (nul-terminated) */
2570 };
2571
2572 struct fsdir_cursor {
2573   sqlite3_vtab_cursor base;  /* Base class - must be first */
2574
2575   int nLvl;                  /* Number of entries in aLvl[] array */
2576   int iLvl;                  /* Index of current entry */
2577   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2578
2579   const char *zBase;
2580   int nBase;
2581
2582   struct stat sStat;         /* Current lstat() results */
2583   char *zPath;               /* Path to current entry */
2584   sqlite3_int64 iRowid;      /* Current rowid */
2585 };
2586
2587 typedef struct fsdir_tab fsdir_tab;
2588 struct fsdir_tab {
2589   sqlite3_vtab base;         /* Base class - must be first */
2590 };
2591
2592 /*
2593 ** Construct a new fsdir virtual table object.
2594 */
2595 static int fsdirConnect(
2596   sqlite3 *db,
2597   void *pAux,
2598   int argc, const char *const*argv,
2599   sqlite3_vtab **ppVtab,
2600   char **pzErr
2601 ){
2602   fsdir_tab *pNew = 0;
2603   int rc;
2604   (void)pAux;
2605   (void)argc;
2606   (void)argv;
2607   (void)pzErr;
2608   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2609   if( rc==SQLITE_OK ){
2610     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2611     if( pNew==0 ) return SQLITE_NOMEM;
2612     memset(pNew, 0, sizeof(*pNew));
2613   }
2614   *ppVtab = (sqlite3_vtab*)pNew;
2615   return rc;
2616 }
2617
2618 /*
2619 ** This method is the destructor for fsdir vtab objects.
2620 */
2621 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2622   sqlite3_free(pVtab);
2623   return SQLITE_OK;
2624 }
2625
2626 /*
2627 ** Constructor for a new fsdir_cursor object.
2628 */
2629 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2630   fsdir_cursor *pCur;
2631   (void)p;
2632   pCur = sqlite3_malloc( sizeof(*pCur) );
2633   if( pCur==0 ) return SQLITE_NOMEM;
2634   memset(pCur, 0, sizeof(*pCur));
2635   pCur->iLvl = -1;
2636   *ppCursor = &pCur->base;
2637   return SQLITE_OK;
2638 }
2639
2640 /*
2641 ** Reset a cursor back to the state it was in when first returned
2642 ** by fsdirOpen().
2643 */
2644 static void fsdirResetCursor(fsdir_cursor *pCur){
2645   int i;
2646   for(i=0; i<=pCur->iLvl; i++){
2647     FsdirLevel *pLvl = &pCur->aLvl[i];
2648     if( pLvl->pDir ) closedir(pLvl->pDir);
2649     sqlite3_free(pLvl->zDir);
2650   }
2651   sqlite3_free(pCur->zPath);
2652   sqlite3_free(pCur->aLvl);
2653   pCur->aLvl = 0;
2654   pCur->zPath = 0;
2655   pCur->zBase = 0;
2656   pCur->nBase = 0;
2657   pCur->nLvl = 0;
2658   pCur->iLvl = -1;
2659   pCur->iRowid = 1;
2660 }
2661
2662 /*
2663 ** Destructor for an fsdir_cursor.
2664 */
2665 static int fsdirClose(sqlite3_vtab_cursor *cur){
2666   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2667
2668   fsdirResetCursor(pCur);
2669   sqlite3_free(pCur);
2670   return SQLITE_OK;
2671 }
2672
2673 /*
2674 ** Set the error message for the virtual table associated with cursor
2675 ** pCur to the results of vprintf(zFmt, ...).
2676 */
2677 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2678   va_list ap;
2679   va_start(ap, zFmt);
2680   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2681   va_end(ap);
2682 }
2683
2684
2685 /*
2686 ** Advance an fsdir_cursor to its next row of output.
2687 */
2688 static int fsdirNext(sqlite3_vtab_cursor *cur){
2689   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2690   mode_t m = pCur->sStat.st_mode;
2691
2692   pCur->iRowid++;
2693   if( S_ISDIR(m) ){
2694     /* Descend into this directory */
2695     int iNew = pCur->iLvl + 1;
2696     FsdirLevel *pLvl;
2697     if( iNew>=pCur->nLvl ){
2698       int nNew = iNew+1;
2699       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2700       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2701       if( aNew==0 ) return SQLITE_NOMEM;
2702       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2703       pCur->aLvl = aNew;
2704       pCur->nLvl = nNew;
2705     }
2706     pCur->iLvl = iNew;
2707     pLvl = &pCur->aLvl[iNew];
2708     
2709     pLvl->zDir = pCur->zPath;
2710     pCur->zPath = 0;
2711     pLvl->pDir = opendir(pLvl->zDir);
2712     if( pLvl->pDir==0 ){
2713       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2714       return SQLITE_ERROR;
2715     }
2716   }
2717
2718   while( pCur->iLvl>=0 ){
2719     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2720     struct dirent *pEntry = readdir(pLvl->pDir);
2721     if( pEntry ){
2722       if( pEntry->d_name[0]=='.' ){
2723        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2724        if( pEntry->d_name[1]=='\0' ) continue;
2725       }
2726       sqlite3_free(pCur->zPath);
2727       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2728       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2729       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2730         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2731         return SQLITE_ERROR;
2732       }
2733       return SQLITE_OK;
2734     }
2735     closedir(pLvl->pDir);
2736     sqlite3_free(pLvl->zDir);
2737     pLvl->pDir = 0;
2738     pLvl->zDir = 0;
2739     pCur->iLvl--;
2740   }
2741
2742   /* EOF */
2743   sqlite3_free(pCur->zPath);
2744   pCur->zPath = 0;
2745   return SQLITE_OK;
2746 }
2747
2748 /*
2749 ** Return values of columns for the row at which the series_cursor
2750 ** is currently pointing.
2751 */
2752 static int fsdirColumn(
2753   sqlite3_vtab_cursor *cur,   /* The cursor */
2754   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2755   int i                       /* Which column to return */
2756 ){
2757   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2758   switch( i ){
2759     case FSDIR_COLUMN_NAME: {
2760       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2761       break;
2762     }
2763
2764     case FSDIR_COLUMN_MODE:
2765       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2766       break;
2767
2768     case FSDIR_COLUMN_MTIME:
2769       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2770       break;
2771
2772     case FSDIR_COLUMN_DATA: {
2773       mode_t m = pCur->sStat.st_mode;
2774       if( S_ISDIR(m) ){
2775         sqlite3_result_null(ctx);
2776 #if !defined(_WIN32) && !defined(WIN32)
2777       }else if( S_ISLNK(m) ){
2778         char aStatic[64];
2779         char *aBuf = aStatic;
2780         sqlite3_int64 nBuf = 64;
2781         int n;
2782
2783         while( 1 ){
2784           n = readlink(pCur->zPath, aBuf, nBuf);
2785           if( n<nBuf ) break;
2786           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2787           nBuf = nBuf*2;
2788           aBuf = sqlite3_malloc64(nBuf);
2789           if( aBuf==0 ){
2790             sqlite3_result_error_nomem(ctx);
2791             return SQLITE_NOMEM;
2792           }
2793         }
2794
2795         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2796         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2797 #endif
2798       }else{
2799         readFileContents(ctx, pCur->zPath);
2800       }
2801     }
2802     case FSDIR_COLUMN_PATH:
2803     default: {
2804       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2805       ** always return their values as NULL */
2806       break;
2807     }
2808   }
2809   return SQLITE_OK;
2810 }
2811
2812 /*
2813 ** Return the rowid for the current row. In this implementation, the
2814 ** first row returned is assigned rowid value 1, and each subsequent
2815 ** row a value 1 more than that of the previous.
2816 */
2817 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2818   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2819   *pRowid = pCur->iRowid;
2820   return SQLITE_OK;
2821 }
2822
2823 /*
2824 ** Return TRUE if the cursor has been moved off of the last
2825 ** row of output.
2826 */
2827 static int fsdirEof(sqlite3_vtab_cursor *cur){
2828   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2829   return (pCur->zPath==0);
2830 }
2831
2832 /*
2833 ** xFilter callback.
2834 **
2835 ** idxNum==1   PATH parameter only
2836 ** idxNum==2   Both PATH and DIR supplied
2837 */
2838 static int fsdirFilter(
2839   sqlite3_vtab_cursor *cur, 
2840   int idxNum, const char *idxStr,
2841   int argc, sqlite3_value **argv
2842 ){
2843   const char *zDir = 0;
2844   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2845   (void)idxStr;
2846   fsdirResetCursor(pCur);
2847
2848   if( idxNum==0 ){
2849     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2850     return SQLITE_ERROR;
2851   }
2852
2853   assert( argc==idxNum && (argc==1 || argc==2) );
2854   zDir = (const char*)sqlite3_value_text(argv[0]);
2855   if( zDir==0 ){
2856     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2857     return SQLITE_ERROR;
2858   }
2859   if( argc==2 ){
2860     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2861   }
2862   if( pCur->zBase ){
2863     pCur->nBase = (int)strlen(pCur->zBase)+1;
2864     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2865   }else{
2866     pCur->zPath = sqlite3_mprintf("%s", zDir);
2867   }
2868
2869   if( pCur->zPath==0 ){
2870     return SQLITE_NOMEM;
2871   }
2872   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2873     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2874     return SQLITE_ERROR;
2875   }
2876
2877   return SQLITE_OK;
2878 }
2879
2880 /*
2881 ** SQLite will invoke this method one or more times while planning a query
2882 ** that uses the generate_series virtual table.  This routine needs to create
2883 ** a query plan for each invocation and compute an estimated cost for that
2884 ** plan.
2885 **
2886 ** In this implementation idxNum is used to represent the
2887 ** query plan.  idxStr is unused.
2888 **
2889 ** The query plan is represented by values of idxNum:
2890 **
2891 **  (1)  The path value is supplied by argv[0]
2892 **  (2)  Path is in argv[0] and dir is in argv[1]
2893 */
2894 static int fsdirBestIndex(
2895   sqlite3_vtab *tab,
2896   sqlite3_index_info *pIdxInfo
2897 ){
2898   int i;                 /* Loop over constraints */
2899   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
2900   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
2901   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
2902   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
2903   const struct sqlite3_index_constraint *pConstraint;
2904
2905   (void)tab;
2906   pConstraint = pIdxInfo->aConstraint;
2907   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2908     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2909     switch( pConstraint->iColumn ){
2910       case FSDIR_COLUMN_PATH: {
2911         if( pConstraint->usable ){
2912           idxPath = i;
2913           seenPath = 0;
2914         }else if( idxPath<0 ){
2915           seenPath = 1;
2916         }
2917         break;
2918       }
2919       case FSDIR_COLUMN_DIR: {
2920         if( pConstraint->usable ){
2921           idxDir = i;
2922           seenDir = 0;
2923         }else if( idxDir<0 ){
2924           seenDir = 1;
2925         }
2926         break;
2927       }
2928     } 
2929   }
2930   if( seenPath || seenDir ){
2931     /* If input parameters are unusable, disallow this plan */
2932     return SQLITE_CONSTRAINT;
2933   }
2934
2935   if( idxPath<0 ){
2936     pIdxInfo->idxNum = 0;
2937     /* The pIdxInfo->estimatedCost should have been initialized to a huge
2938     ** number.  Leave it unchanged. */
2939     pIdxInfo->estimatedRows = 0x7fffffff;
2940   }else{
2941     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
2942     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
2943     if( idxDir>=0 ){
2944       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
2945       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
2946       pIdxInfo->idxNum = 2;
2947       pIdxInfo->estimatedCost = 10.0;
2948     }else{
2949       pIdxInfo->idxNum = 1;
2950       pIdxInfo->estimatedCost = 100.0;
2951     }
2952   }
2953
2954   return SQLITE_OK;
2955 }
2956
2957 /*
2958 ** Register the "fsdir" virtual table.
2959 */
2960 static int fsdirRegister(sqlite3 *db){
2961   static sqlite3_module fsdirModule = {
2962     0,                         /* iVersion */
2963     0,                         /* xCreate */
2964     fsdirConnect,              /* xConnect */
2965     fsdirBestIndex,            /* xBestIndex */
2966     fsdirDisconnect,           /* xDisconnect */
2967     0,                         /* xDestroy */
2968     fsdirOpen,                 /* xOpen - open a cursor */
2969     fsdirClose,                /* xClose - close a cursor */
2970     fsdirFilter,               /* xFilter - configure scan constraints */
2971     fsdirNext,                 /* xNext - advance a cursor */
2972     fsdirEof,                  /* xEof - check for end of scan */
2973     fsdirColumn,               /* xColumn - read data */
2974     fsdirRowid,                /* xRowid - read data */
2975     0,                         /* xUpdate */
2976     0,                         /* xBegin */
2977     0,                         /* xSync */
2978     0,                         /* xCommit */
2979     0,                         /* xRollback */
2980     0,                         /* xFindMethod */
2981     0,                         /* xRename */
2982     0,                         /* xSavepoint */
2983     0,                         /* xRelease */
2984     0,                         /* xRollbackTo */
2985     0,                         /* xShadowName */
2986   };
2987
2988   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2989   return rc;
2990 }
2991 #else         /* SQLITE_OMIT_VIRTUALTABLE */
2992 # define fsdirRegister(x) SQLITE_OK
2993 #endif
2994
2995 #ifdef _WIN32
2996
2997 #endif
2998 int sqlite3_fileio_init(
2999   sqlite3 *db, 
3000   char **pzErrMsg, 
3001   const sqlite3_api_routines *pApi
3002 ){
3003   int rc = SQLITE_OK;
3004   SQLITE_EXTENSION_INIT2(pApi);
3005   (void)pzErrMsg;  /* Unused parameter */
3006   rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
3007                                readfileFunc, 0, 0);
3008   if( rc==SQLITE_OK ){
3009     rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
3010                                  writefileFunc, 0, 0);
3011   }
3012   if( rc==SQLITE_OK ){
3013     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3014                                  lsModeFunc, 0, 0);
3015   }
3016   if( rc==SQLITE_OK ){
3017     rc = fsdirRegister(db);
3018   }
3019   return rc;
3020 }
3021
3022 /************************* End ../ext/misc/fileio.c ********************/
3023 /************************* Begin ../ext/misc/completion.c ******************/
3024 /*
3025 ** 2017-07-10
3026 **
3027 ** The author disclaims copyright to this source code.  In place of
3028 ** a legal notice, here is a blessing:
3029 **
3030 **    May you do good and not evil.
3031 **    May you find forgiveness for yourself and forgive others.
3032 **    May you share freely, never taking more than you give.
3033 **
3034 *************************************************************************
3035 **
3036 ** This file implements an eponymous virtual table that returns suggested
3037 ** completions for a partial SQL input.
3038 **
3039 ** Suggested usage:
3040 **
3041 **     SELECT DISTINCT candidate COLLATE nocase
3042 **       FROM completion($prefix,$wholeline)
3043 **      ORDER BY 1;
3044 **
3045 ** The two query parameters are optional.  $prefix is the text of the
3046 ** current word being typed and that is to be completed.  $wholeline is
3047 ** the complete input line, used for context.
3048 **
3049 ** The raw completion() table might return the same candidate multiple
3050 ** times, for example if the same column name is used to two or more
3051 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
3052 ** the DISTINCT and ORDER BY are recommended.
3053 **
3054 ** This virtual table operates at the speed of human typing, and so there
3055 ** is no attempt to make it fast.  Even a slow implementation will be much
3056 ** faster than any human can type.
3057 **
3058 */
3059 SQLITE_EXTENSION_INIT1
3060 #include <assert.h>
3061 #include <string.h>
3062 #include <ctype.h>
3063
3064 #ifndef SQLITE_OMIT_VIRTUALTABLE
3065
3066 /* completion_vtab is a subclass of sqlite3_vtab which will
3067 ** serve as the underlying representation of a completion virtual table
3068 */
3069 typedef struct completion_vtab completion_vtab;
3070 struct completion_vtab {
3071   sqlite3_vtab base;  /* Base class - must be first */
3072   sqlite3 *db;        /* Database connection for this completion vtab */
3073 };
3074
3075 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3076 ** serve as the underlying representation of a cursor that scans
3077 ** over rows of the result
3078 */
3079 typedef struct completion_cursor completion_cursor;
3080 struct completion_cursor {
3081   sqlite3_vtab_cursor base;  /* Base class - must be first */
3082   sqlite3 *db;               /* Database connection for this cursor */
3083   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3084   char *zPrefix;             /* The prefix for the word we want to complete */
3085   char *zLine;               /* The whole that we want to complete */
3086   const char *zCurrentRow;   /* Current output row */
3087   int szRow;                 /* Length of the zCurrentRow string */
3088   sqlite3_stmt *pStmt;       /* Current statement */
3089   sqlite3_int64 iRowid;      /* The rowid */
3090   int ePhase;                /* Current phase */
3091   int j;                     /* inter-phase counter */
3092 };
3093
3094 /* Values for ePhase:
3095 */
3096 #define COMPLETION_FIRST_PHASE   1
3097 #define COMPLETION_KEYWORDS      1
3098 #define COMPLETION_PRAGMAS       2
3099 #define COMPLETION_FUNCTIONS     3
3100 #define COMPLETION_COLLATIONS    4
3101 #define COMPLETION_INDEXES       5
3102 #define COMPLETION_TRIGGERS      6
3103 #define COMPLETION_DATABASES     7
3104 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3105 #define COMPLETION_COLUMNS       9
3106 #define COMPLETION_MODULES       10
3107 #define COMPLETION_EOF           11
3108
3109 /*
3110 ** The completionConnect() method is invoked to create a new
3111 ** completion_vtab that describes the completion virtual table.
3112 **
3113 ** Think of this routine as the constructor for completion_vtab objects.
3114 **
3115 ** All this routine needs to do is:
3116 **
3117 **    (1) Allocate the completion_vtab object and initialize all fields.
3118 **
3119 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3120 **        result set of queries against completion will look like.
3121 */
3122 static int completionConnect(
3123   sqlite3 *db,
3124   void *pAux,
3125   int argc, const char *const*argv,
3126   sqlite3_vtab **ppVtab,
3127   char **pzErr
3128 ){
3129   completion_vtab *pNew;
3130   int rc;
3131
3132   (void)(pAux);    /* Unused parameter */
3133   (void)(argc);    /* Unused parameter */
3134   (void)(argv);    /* Unused parameter */
3135   (void)(pzErr);   /* Unused parameter */
3136
3137 /* Column numbers */
3138 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3139 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3140 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3141 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3142
3143   rc = sqlite3_declare_vtab(db,
3144       "CREATE TABLE x("
3145       "  candidate TEXT,"
3146       "  prefix TEXT HIDDEN,"
3147       "  wholeline TEXT HIDDEN,"
3148       "  phase INT HIDDEN"        /* Used for debugging only */
3149       ")");
3150   if( rc==SQLITE_OK ){
3151     pNew = sqlite3_malloc( sizeof(*pNew) );
3152     *ppVtab = (sqlite3_vtab*)pNew;
3153     if( pNew==0 ) return SQLITE_NOMEM;
3154     memset(pNew, 0, sizeof(*pNew));
3155     pNew->db = db;
3156   }
3157   return rc;
3158 }
3159
3160 /*
3161 ** This method is the destructor for completion_cursor objects.
3162 */
3163 static int completionDisconnect(sqlite3_vtab *pVtab){
3164   sqlite3_free(pVtab);
3165   return SQLITE_OK;
3166 }
3167
3168 /*
3169 ** Constructor for a new completion_cursor object.
3170 */
3171 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3172   completion_cursor *pCur;
3173   pCur = sqlite3_malloc( sizeof(*pCur) );
3174   if( pCur==0 ) return SQLITE_NOMEM;
3175   memset(pCur, 0, sizeof(*pCur));
3176   pCur->db = ((completion_vtab*)p)->db;
3177   *ppCursor = &pCur->base;
3178   return SQLITE_OK;
3179 }
3180
3181 /*
3182 ** Reset the completion_cursor.
3183 */
3184 static void completionCursorReset(completion_cursor *pCur){
3185   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3186   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3187   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3188   pCur->j = 0;
3189 }
3190
3191 /*
3192 ** Destructor for a completion_cursor.
3193 */
3194 static int completionClose(sqlite3_vtab_cursor *cur){
3195   completionCursorReset((completion_cursor*)cur);
3196   sqlite3_free(cur);
3197   return SQLITE_OK;
3198 }
3199
3200 /*
3201 ** Advance a completion_cursor to its next row of output.
3202 **
3203 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3204 ** record the current state of the scan.  This routine sets ->zCurrentRow
3205 ** to the current row of output and then returns.  If no more rows remain,
3206 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3207 ** table that has reached the end of its scan.
3208 **
3209 ** The current implementation just lists potential identifiers and
3210 ** keywords and filters them by zPrefix.  Future enhancements should
3211 ** take zLine into account to try to restrict the set of identifiers and
3212 ** keywords based on what would be legal at the current point of input.
3213 */
3214 static int completionNext(sqlite3_vtab_cursor *cur){
3215   completion_cursor *pCur = (completion_cursor*)cur;
3216   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3217   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3218   pCur->iRowid++;
3219   while( pCur->ePhase!=COMPLETION_EOF ){
3220     switch( pCur->ePhase ){
3221       case COMPLETION_KEYWORDS: {
3222         if( pCur->j >= sqlite3_keyword_count() ){
3223           pCur->zCurrentRow = 0;
3224           pCur->ePhase = COMPLETION_DATABASES;
3225         }else{
3226           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3227         }
3228         iCol = -1;
3229         break;
3230       }
3231       case COMPLETION_DATABASES: {
3232         if( pCur->pStmt==0 ){
3233           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3234                              &pCur->pStmt, 0);
3235         }
3236         iCol = 1;
3237         eNextPhase = COMPLETION_TABLES;
3238         break;
3239       }
3240       case COMPLETION_TABLES: {
3241         if( pCur->pStmt==0 ){
3242           sqlite3_stmt *pS2;
3243           char *zSql = 0;
3244           const char *zSep = "";
3245           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3246           while( sqlite3_step(pS2)==SQLITE_ROW ){
3247             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3248             zSql = sqlite3_mprintf(
3249                "%z%s"
3250                "SELECT name FROM \"%w\".sqlite_master",
3251                zSql, zSep, zDb
3252             );
3253             if( zSql==0 ) return SQLITE_NOMEM;
3254             zSep = " UNION ";
3255           }
3256           sqlite3_finalize(pS2);
3257           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3258           sqlite3_free(zSql);
3259         }
3260         iCol = 0;
3261         eNextPhase = COMPLETION_COLUMNS;
3262         break;
3263       }
3264       case COMPLETION_COLUMNS: {
3265         if( pCur->pStmt==0 ){
3266           sqlite3_stmt *pS2;
3267           char *zSql = 0;
3268           const char *zSep = "";
3269           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3270           while( sqlite3_step(pS2)==SQLITE_ROW ){
3271             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3272             zSql = sqlite3_mprintf(
3273                "%z%s"
3274                "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3275                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3276                " WHERE sm.type='table'",
3277                zSql, zSep, zDb, zDb
3278             );
3279             if( zSql==0 ) return SQLITE_NOMEM;
3280             zSep = " UNION ";
3281           }
3282           sqlite3_finalize(pS2);
3283           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3284           sqlite3_free(zSql);
3285         }
3286         iCol = 0;
3287         eNextPhase = COMPLETION_EOF;
3288         break;
3289       }
3290     }
3291     if( iCol<0 ){
3292       /* This case is when the phase presets zCurrentRow */
3293       if( pCur->zCurrentRow==0 ) continue;
3294     }else{
3295       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3296         /* Extract the next row of content */
3297         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3298         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3299       }else{
3300         /* When all rows are finished, advance to the next phase */
3301         sqlite3_finalize(pCur->pStmt);
3302         pCur->pStmt = 0;
3303         pCur->ePhase = eNextPhase;
3304         continue;
3305       }
3306     }
3307     if( pCur->nPrefix==0 ) break;
3308     if( pCur->nPrefix<=pCur->szRow
3309      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3310     ){
3311       break;
3312     }
3313   }
3314
3315   return SQLITE_OK;
3316 }
3317
3318 /*
3319 ** Return values of columns for the row at which the completion_cursor
3320 ** is currently pointing.
3321 */
3322 static int completionColumn(
3323   sqlite3_vtab_cursor *cur,   /* The cursor */
3324   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3325   int i                       /* Which column to return */
3326 ){
3327   completion_cursor *pCur = (completion_cursor*)cur;
3328   switch( i ){
3329     case COMPLETION_COLUMN_CANDIDATE: {
3330       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3331       break;
3332     }
3333     case COMPLETION_COLUMN_PREFIX: {
3334       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3335       break;
3336     }
3337     case COMPLETION_COLUMN_WHOLELINE: {
3338       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3339       break;
3340     }
3341     case COMPLETION_COLUMN_PHASE: {
3342       sqlite3_result_int(ctx, pCur->ePhase);
3343       break;
3344     }
3345   }
3346   return SQLITE_OK;
3347 }
3348
3349 /*
3350 ** Return the rowid for the current row.  In this implementation, the
3351 ** rowid is the same as the output value.
3352 */
3353 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3354   completion_cursor *pCur = (completion_cursor*)cur;
3355   *pRowid = pCur->iRowid;
3356   return SQLITE_OK;
3357 }
3358
3359 /*
3360 ** Return TRUE if the cursor has been moved off of the last
3361 ** row of output.
3362 */
3363 static int completionEof(sqlite3_vtab_cursor *cur){
3364   completion_cursor *pCur = (completion_cursor*)cur;
3365   return pCur->ePhase >= COMPLETION_EOF;
3366 }
3367
3368 /*
3369 ** This method is called to "rewind" the completion_cursor object back
3370 ** to the first row of output.  This method is always called at least
3371 ** once prior to any call to completionColumn() or completionRowid() or 
3372 ** completionEof().
3373 */
3374 static int completionFilter(
3375   sqlite3_vtab_cursor *pVtabCursor, 
3376   int idxNum, const char *idxStr,
3377   int argc, sqlite3_value **argv
3378 ){
3379   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3380   int iArg = 0;
3381   (void)(idxStr);   /* Unused parameter */
3382   (void)(argc);     /* Unused parameter */
3383   completionCursorReset(pCur);
3384   if( idxNum & 1 ){
3385     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3386     if( pCur->nPrefix>0 ){
3387       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3388       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3389     }
3390     iArg = 1;
3391   }
3392   if( idxNum & 2 ){
3393     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3394     if( pCur->nLine>0 ){
3395       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3396       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3397     }
3398   }
3399   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3400     int i = pCur->nLine;
3401     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3402       i--;
3403     }
3404     pCur->nPrefix = pCur->nLine - i;
3405     if( pCur->nPrefix>0 ){
3406       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3407       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3408     }
3409   }
3410   pCur->iRowid = 0;
3411   pCur->ePhase = COMPLETION_FIRST_PHASE;
3412   return completionNext(pVtabCursor);
3413 }
3414
3415 /*
3416 ** SQLite will invoke this method one or more times while planning a query
3417 ** that uses the completion virtual table.  This routine needs to create
3418 ** a query plan for each invocation and compute an estimated cost for that
3419 ** plan.
3420 **
3421 ** There are two hidden parameters that act as arguments to the table-valued
3422 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3423 ** is available and bit 1 is set if "wholeline" is available.
3424 */
3425 static int completionBestIndex(
3426   sqlite3_vtab *tab,
3427   sqlite3_index_info *pIdxInfo
3428 ){
3429   int i;                 /* Loop over constraints */
3430   int idxNum = 0;        /* The query plan bitmask */
3431   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3432   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3433   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3434   const struct sqlite3_index_constraint *pConstraint;
3435
3436   (void)(tab);    /* Unused parameter */
3437   pConstraint = pIdxInfo->aConstraint;
3438   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3439     if( pConstraint->usable==0 ) continue;
3440     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3441     switch( pConstraint->iColumn ){
3442       case COMPLETION_COLUMN_PREFIX:
3443         prefixIdx = i;
3444         idxNum |= 1;
3445         break;
3446       case COMPLETION_COLUMN_WHOLELINE:
3447         wholelineIdx = i;
3448         idxNum |= 2;
3449         break;
3450     }
3451   }
3452   if( prefixIdx>=0 ){
3453     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3454     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3455   }
3456   if( wholelineIdx>=0 ){
3457     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3458     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3459   }
3460   pIdxInfo->idxNum = idxNum;
3461   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3462   pIdxInfo->estimatedRows = 500 - 100*nArg;
3463   return SQLITE_OK;
3464 }
3465
3466 /*
3467 ** This following structure defines all the methods for the 
3468 ** completion virtual table.
3469 */
3470 static sqlite3_module completionModule = {
3471   0,                         /* iVersion */
3472   0,                         /* xCreate */
3473   completionConnect,         /* xConnect */
3474   completionBestIndex,       /* xBestIndex */
3475   completionDisconnect,      /* xDisconnect */
3476   0,                         /* xDestroy */
3477   completionOpen,            /* xOpen - open a cursor */
3478   completionClose,           /* xClose - close a cursor */
3479   completionFilter,          /* xFilter - configure scan constraints */
3480   completionNext,            /* xNext - advance a cursor */
3481   completionEof,             /* xEof - check for end of scan */
3482   completionColumn,          /* xColumn - read data */
3483   completionRowid,           /* xRowid - read data */
3484   0,                         /* xUpdate */
3485   0,                         /* xBegin */
3486   0,                         /* xSync */
3487   0,                         /* xCommit */
3488   0,                         /* xRollback */
3489   0,                         /* xFindMethod */
3490   0,                         /* xRename */
3491   0,                         /* xSavepoint */
3492   0,                         /* xRelease */
3493   0,                         /* xRollbackTo */
3494   0                          /* xShadowName */
3495 };
3496
3497 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3498
3499 int sqlite3CompletionVtabInit(sqlite3 *db){
3500   int rc = SQLITE_OK;
3501 #ifndef SQLITE_OMIT_VIRTUALTABLE
3502   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3503 #endif
3504   return rc;
3505 }
3506
3507 #ifdef _WIN32
3508
3509 #endif
3510 int sqlite3_completion_init(
3511   sqlite3 *db, 
3512   char **pzErrMsg, 
3513   const sqlite3_api_routines *pApi
3514 ){
3515   int rc = SQLITE_OK;
3516   SQLITE_EXTENSION_INIT2(pApi);
3517   (void)(pzErrMsg);  /* Unused parameter */
3518 #ifndef SQLITE_OMIT_VIRTUALTABLE
3519   rc = sqlite3CompletionVtabInit(db);
3520 #endif
3521   return rc;
3522 }
3523
3524 /************************* End ../ext/misc/completion.c ********************/
3525 /************************* Begin ../ext/misc/appendvfs.c ******************/
3526 /*
3527 ** 2017-10-20
3528 **
3529 ** The author disclaims copyright to this source code.  In place of
3530 ** a legal notice, here is a blessing:
3531 **
3532 **    May you do good and not evil.
3533 **    May you find forgiveness for yourself and forgive others.
3534 **    May you share freely, never taking more than you give.
3535 **
3536 ******************************************************************************
3537 **
3538 ** This file implements a VFS shim that allows an SQLite database to be
3539 ** appended onto the end of some other file, such as an executable.
3540 **
3541 ** A special record must appear at the end of the file that identifies the
3542 ** file as an appended database and provides an offset to page 1.  For
3543 ** best performance page 1 should be located at a disk page boundary, though
3544 ** that is not required.
3545 **
3546 ** When opening a database using this VFS, the connection might treat
3547 ** the file as an ordinary SQLite database, or it might treat is as a
3548 ** database appended onto some other file.  Here are the rules:
3549 **
3550 **  (1)  When opening a new empty file, that file is treated as an ordinary
3551 **       database.
3552 **
3553 **  (2)  When opening a file that begins with the standard SQLite prefix
3554 **       string "SQLite format 3", that file is treated as an ordinary
3555 **       database.
3556 **
3557 **  (3)  When opening a file that ends with the appendvfs trailer string
3558 **       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3559 **       database.
3560 **
3561 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3562 **       set, then a new database is appended to the already existing file.
3563 **
3564 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3565 **
3566 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3567 ** the file containing the database is limited to 1GB.  This VFS will refuse
3568 ** to read or write past the 1GB mark.  This restriction might be lifted in
3569 ** future versions.  For now, if you need a large database, then keep the
3570 ** database in a separate file.
3571 **
3572 ** If the file being opened is not an appended database, then this shim is
3573 ** a pass-through into the default underlying VFS.
3574 **/
3575 SQLITE_EXTENSION_INIT1
3576 #include <string.h>
3577 #include <assert.h>
3578
3579 /* The append mark at the end of the database is:
3580 **
3581 **     Start-Of-SQLite3-NNNNNNNN
3582 **     123456789 123456789 12345
3583 **
3584 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3585 ** the offset to page 1.
3586 */
3587 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3588 #define APND_MARK_PREFIX_SZ  17
3589 #define APND_MARK_SIZE       25
3590
3591 /*
3592 ** Maximum size of the combined prefix + database + append-mark.  This
3593 ** must be less than 0x40000000 to avoid locking issues on Windows.
3594 */
3595 #define APND_MAX_SIZE  (65536*15259)
3596
3597 /*
3598 ** Forward declaration of objects used by this utility
3599 */
3600 typedef struct sqlite3_vfs ApndVfs;
3601 typedef struct ApndFile ApndFile;
3602
3603 /* Access to a lower-level VFS that (might) implement dynamic loading,
3604 ** access to randomness, etc.
3605 */
3606 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3607 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3608
3609 /* An open file */
3610 struct ApndFile {
3611   sqlite3_file base;              /* IO methods */
3612   sqlite3_int64 iPgOne;           /* File offset to page 1 */
3613   sqlite3_int64 iMark;            /* Start of the append-mark */
3614 };
3615
3616 /*
3617 ** Methods for ApndFile
3618 */
3619 static int apndClose(sqlite3_file*);
3620 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3621 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3622 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3623 static int apndSync(sqlite3_file*, int flags);
3624 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3625 static int apndLock(sqlite3_file*, int);
3626 static int apndUnlock(sqlite3_file*, int);
3627 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3628 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3629 static int apndSectorSize(sqlite3_file*);
3630 static int apndDeviceCharacteristics(sqlite3_file*);
3631 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3632 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3633 static void apndShmBarrier(sqlite3_file*);
3634 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3635 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3636 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3637
3638 /*
3639 ** Methods for ApndVfs
3640 */
3641 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3642 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3643 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3644 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3645 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3646 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3647 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3648 static void apndDlClose(sqlite3_vfs*, void*);
3649 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3650 static int apndSleep(sqlite3_vfs*, int microseconds);
3651 static int apndCurrentTime(sqlite3_vfs*, double*);
3652 static int apndGetLastError(sqlite3_vfs*, int, char *);
3653 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3654 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3655 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3656 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3657
3658 static sqlite3_vfs apnd_vfs = {
3659   3,                            /* iVersion (set when registered) */
3660   0,                            /* szOsFile (set when registered) */
3661   1024,                         /* mxPathname */
3662   0,                            /* pNext */
3663   "apndvfs",                    /* zName */
3664   0,                            /* pAppData (set when registered) */ 
3665   apndOpen,                     /* xOpen */
3666   apndDelete,                   /* xDelete */
3667   apndAccess,                   /* xAccess */
3668   apndFullPathname,             /* xFullPathname */
3669   apndDlOpen,                   /* xDlOpen */
3670   apndDlError,                  /* xDlError */
3671   apndDlSym,                    /* xDlSym */
3672   apndDlClose,                  /* xDlClose */
3673   apndRandomness,               /* xRandomness */
3674   apndSleep,                    /* xSleep */
3675   apndCurrentTime,              /* xCurrentTime */
3676   apndGetLastError,             /* xGetLastError */
3677   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3678   apndSetSystemCall,            /* xSetSystemCall */
3679   apndGetSystemCall,            /* xGetSystemCall */
3680   apndNextSystemCall            /* xNextSystemCall */
3681 };
3682
3683 static const sqlite3_io_methods apnd_io_methods = {
3684   3,                              /* iVersion */
3685   apndClose,                      /* xClose */
3686   apndRead,                       /* xRead */
3687   apndWrite,                      /* xWrite */
3688   apndTruncate,                   /* xTruncate */
3689   apndSync,                       /* xSync */
3690   apndFileSize,                   /* xFileSize */
3691   apndLock,                       /* xLock */
3692   apndUnlock,                     /* xUnlock */
3693   apndCheckReservedLock,          /* xCheckReservedLock */
3694   apndFileControl,                /* xFileControl */
3695   apndSectorSize,                 /* xSectorSize */
3696   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3697   apndShmMap,                     /* xShmMap */
3698   apndShmLock,                    /* xShmLock */
3699   apndShmBarrier,                 /* xShmBarrier */
3700   apndShmUnmap,                   /* xShmUnmap */
3701   apndFetch,                      /* xFetch */
3702   apndUnfetch                     /* xUnfetch */
3703 };
3704
3705
3706
3707 /*
3708 ** Close an apnd-file.
3709 */
3710 static int apndClose(sqlite3_file *pFile){
3711   pFile = ORIGFILE(pFile);
3712   return pFile->pMethods->xClose(pFile);
3713 }
3714
3715 /*
3716 ** Read data from an apnd-file.
3717 */
3718 static int apndRead(
3719   sqlite3_file *pFile, 
3720   void *zBuf, 
3721   int iAmt, 
3722   sqlite_int64 iOfst
3723 ){
3724   ApndFile *p = (ApndFile *)pFile;
3725   pFile = ORIGFILE(pFile);
3726   return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3727 }
3728
3729 /*
3730 ** Add the append-mark onto the end of the file.
3731 */
3732 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3733   int i;
3734   unsigned char a[APND_MARK_SIZE];
3735   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3736   for(i=0; i<8; i++){
3737     a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3738   }
3739   return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3740 }
3741
3742 /*
3743 ** Write data to an apnd-file.
3744 */
3745 static int apndWrite(
3746   sqlite3_file *pFile,
3747   const void *zBuf,
3748   int iAmt,
3749   sqlite_int64 iOfst
3750 ){
3751   int rc;
3752   ApndFile *p = (ApndFile *)pFile;
3753   pFile = ORIGFILE(pFile);
3754   if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3755   rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3756   if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
3757     sqlite3_int64 sz = 0;
3758     rc = pFile->pMethods->xFileSize(pFile, &sz);
3759     if( rc==SQLITE_OK ){
3760       p->iMark = sz - APND_MARK_SIZE;
3761       if( iOfst + iAmt + p->iPgOne > p->iMark ){
3762         p->iMark = p->iPgOne + iOfst + iAmt;
3763         rc = apndWriteMark(p, pFile);
3764       }
3765     }
3766   }
3767   return rc;
3768 }
3769
3770 /*
3771 ** Truncate an apnd-file.
3772 */
3773 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3774   int rc;
3775   ApndFile *p = (ApndFile *)pFile;
3776   pFile = ORIGFILE(pFile);
3777   rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3778   if( rc==SQLITE_OK ){
3779     p->iMark = p->iPgOne+size;
3780     rc = apndWriteMark(p, pFile);
3781   }
3782   return rc;
3783 }
3784
3785 /*
3786 ** Sync an apnd-file.
3787 */
3788 static int apndSync(sqlite3_file *pFile, int flags){
3789   pFile = ORIGFILE(pFile);
3790   return pFile->pMethods->xSync(pFile, flags);
3791 }
3792
3793 /*
3794 ** Return the current file-size of an apnd-file.
3795 */
3796 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3797   ApndFile *p = (ApndFile *)pFile;
3798   int rc;
3799   pFile = ORIGFILE(p);
3800   rc = pFile->pMethods->xFileSize(pFile, pSize);
3801   if( rc==SQLITE_OK && p->iPgOne ){
3802     *pSize -= p->iPgOne + APND_MARK_SIZE;
3803   }
3804   return rc;
3805 }
3806
3807 /*
3808 ** Lock an apnd-file.
3809 */
3810 static int apndLock(sqlite3_file *pFile, int eLock){
3811   pFile = ORIGFILE(pFile);
3812   return pFile->pMethods->xLock(pFile, eLock);
3813 }
3814
3815 /*
3816 ** Unlock an apnd-file.
3817 */
3818 static int apndUnlock(sqlite3_file *pFile, int eLock){
3819   pFile = ORIGFILE(pFile);
3820   return pFile->pMethods->xUnlock(pFile, eLock);
3821 }
3822
3823 /*
3824 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3825 */
3826 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3827   pFile = ORIGFILE(pFile);
3828   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3829 }
3830
3831 /*
3832 ** File control method. For custom operations on an apnd-file.
3833 */
3834 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3835   ApndFile *p = (ApndFile *)pFile;
3836   int rc;
3837   pFile = ORIGFILE(pFile);
3838   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3839   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3840     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3841   }
3842   return rc;
3843 }
3844
3845 /*
3846 ** Return the sector-size in bytes for an apnd-file.
3847 */
3848 static int apndSectorSize(sqlite3_file *pFile){
3849   pFile = ORIGFILE(pFile);
3850   return pFile->pMethods->xSectorSize(pFile);
3851 }
3852
3853 /*
3854 ** Return the device characteristic flags supported by an apnd-file.
3855 */
3856 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3857   pFile = ORIGFILE(pFile);
3858   return pFile->pMethods->xDeviceCharacteristics(pFile);
3859 }
3860
3861 /* Create a shared memory file mapping */
3862 static int apndShmMap(
3863   sqlite3_file *pFile,
3864   int iPg,
3865   int pgsz,
3866   int bExtend,
3867   void volatile **pp
3868 ){
3869   pFile = ORIGFILE(pFile);
3870   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3871 }
3872
3873 /* Perform locking on a shared-memory segment */
3874 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3875   pFile = ORIGFILE(pFile);
3876   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3877 }
3878
3879 /* Memory barrier operation on shared memory */
3880 static void apndShmBarrier(sqlite3_file *pFile){
3881   pFile = ORIGFILE(pFile);
3882   pFile->pMethods->xShmBarrier(pFile);
3883 }
3884
3885 /* Unmap a shared memory segment */
3886 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3887   pFile = ORIGFILE(pFile);
3888   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3889 }
3890
3891 /* Fetch a page of a memory-mapped file */
3892 static int apndFetch(
3893   sqlite3_file *pFile,
3894   sqlite3_int64 iOfst,
3895   int iAmt,
3896   void **pp
3897 ){
3898   ApndFile *p = (ApndFile *)pFile;
3899   pFile = ORIGFILE(pFile);
3900   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3901 }
3902
3903 /* Release a memory-mapped page */
3904 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3905   ApndFile *p = (ApndFile *)pFile;
3906   pFile = ORIGFILE(pFile);
3907   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3908 }
3909
3910 /*
3911 ** Check to see if the file is an ordinary SQLite database file.
3912 */
3913 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3914   int rc;
3915   char zHdr[16];
3916   static const char aSqliteHdr[] = "SQLite format 3";
3917   if( sz<512 ) return 0;
3918   rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3919   if( rc ) return 0;
3920   return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3921 }
3922
3923 /*
3924 ** Try to read the append-mark off the end of a file.  Return the
3925 ** start of the appended database if the append-mark is present.  If
3926 ** there is no append-mark, return -1;
3927 */
3928 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3929   int rc, i;
3930   sqlite3_int64 iMark;
3931   unsigned char a[APND_MARK_SIZE];
3932
3933   if( sz<=APND_MARK_SIZE ) return -1;
3934   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3935   if( rc ) return -1;
3936   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3937   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3938   for(i=1; i<8; i++){    
3939     iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3940   }
3941   return iMark;
3942 }
3943
3944 /*
3945 ** Open an apnd file handle.
3946 */
3947 static int apndOpen(
3948   sqlite3_vfs *pVfs,
3949   const char *zName,
3950   sqlite3_file *pFile,
3951   int flags,
3952   int *pOutFlags
3953 ){
3954   ApndFile *p;
3955   sqlite3_file *pSubFile;
3956   sqlite3_vfs *pSubVfs;
3957   int rc;
3958   sqlite3_int64 sz;
3959   pSubVfs = ORIGVFS(pVfs);
3960   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3961     return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3962   }
3963   p = (ApndFile*)pFile;
3964   memset(p, 0, sizeof(*p));
3965   pSubFile = ORIGFILE(pFile);
3966   p->base.pMethods = &apnd_io_methods;
3967   rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3968   if( rc ) goto apnd_open_done;
3969   rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3970   if( rc ){
3971     pSubFile->pMethods->xClose(pSubFile);
3972     goto apnd_open_done;
3973   }
3974   if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3975     memmove(pFile, pSubFile, pSubVfs->szOsFile);
3976     return SQLITE_OK;
3977   }
3978   p->iMark = 0;
3979   p->iPgOne = apndReadMark(sz, pFile);
3980   if( p->iPgOne>0 ){
3981     return SQLITE_OK;
3982   }
3983   if( (flags & SQLITE_OPEN_CREATE)==0 ){
3984     pSubFile->pMethods->xClose(pSubFile);
3985     rc = SQLITE_CANTOPEN;
3986   }
3987   p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3988 apnd_open_done:
3989   if( rc ) pFile->pMethods = 0;
3990   return rc;
3991 }
3992
3993 /*
3994 ** All other VFS methods are pass-thrus.
3995 */
3996 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3997   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
3998 }
3999 static int apndAccess(
4000   sqlite3_vfs *pVfs, 
4001   const char *zPath, 
4002   int flags, 
4003   int *pResOut
4004 ){
4005   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4006 }
4007 static int apndFullPathname(
4008   sqlite3_vfs *pVfs, 
4009   const char *zPath, 
4010   int nOut, 
4011   char *zOut
4012 ){
4013   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4014 }
4015 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4016   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4017 }
4018 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4019   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4020 }
4021 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4022   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4023 }
4024 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4025   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4026 }
4027 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4028   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4029 }
4030 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4031   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4032 }
4033 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4034   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4035 }
4036 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4037   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4038 }
4039 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4040   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4041 }
4042 static int apndSetSystemCall(
4043   sqlite3_vfs *pVfs,
4044   const char *zName,
4045   sqlite3_syscall_ptr pCall
4046 ){
4047   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4048 }
4049 static sqlite3_syscall_ptr apndGetSystemCall(
4050   sqlite3_vfs *pVfs,
4051   const char *zName
4052 ){
4053   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4054 }
4055 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4056   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4057 }
4058
4059   
4060 #ifdef _WIN32
4061
4062 #endif
4063 /* 
4064 ** This routine is called when the extension is loaded.
4065 ** Register the new VFS.
4066 */
4067 int sqlite3_appendvfs_init(
4068   sqlite3 *db, 
4069   char **pzErrMsg, 
4070   const sqlite3_api_routines *pApi
4071 ){
4072   int rc = SQLITE_OK;
4073   sqlite3_vfs *pOrig;
4074   SQLITE_EXTENSION_INIT2(pApi);
4075   (void)pzErrMsg;
4076   (void)db;
4077   pOrig = sqlite3_vfs_find(0);
4078   apnd_vfs.iVersion = pOrig->iVersion;
4079   apnd_vfs.pAppData = pOrig;
4080   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4081   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4082 #ifdef APPENDVFS_TEST
4083   if( rc==SQLITE_OK ){
4084     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4085   }
4086 #endif
4087   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4088   return rc;
4089 }
4090
4091 /************************* End ../ext/misc/appendvfs.c ********************/
4092 /************************* Begin ../ext/misc/memtrace.c ******************/
4093 /*
4094 ** 2019-01-21
4095 **
4096 ** The author disclaims copyright to this source code.  In place of
4097 ** a legal notice, here is a blessing:
4098 **
4099 **    May you do good and not evil.
4100 **    May you find forgiveness for yourself and forgive others.
4101 **    May you share freely, never taking more than you give.
4102 **
4103 *************************************************************************
4104 **
4105 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4106 ** mechanism to add a tracing layer on top of SQLite.  If this extension
4107 ** is registered prior to sqlite3_initialize(), it will cause all memory
4108 ** allocation activities to be logged on standard output, or to some other
4109 ** FILE specified by the initializer.
4110 **
4111 ** This file needs to be compiled into the application that uses it.
4112 **
4113 ** This extension is used to implement the --memtrace option of the
4114 ** command-line shell.
4115 */
4116 #include <assert.h>
4117 #include <string.h>
4118 #include <stdio.h>
4119
4120 /* The original memory allocation routines */
4121 static sqlite3_mem_methods memtraceBase;
4122 static FILE *memtraceOut;
4123
4124 /* Methods that trace memory allocations */
4125 static void *memtraceMalloc(int n){
4126   if( memtraceOut ){
4127     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 
4128             memtraceBase.xRoundup(n));
4129   }
4130   return memtraceBase.xMalloc(n);
4131 }
4132 static void memtraceFree(void *p){
4133   if( p==0 ) return;
4134   if( memtraceOut ){
4135     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4136   }
4137   memtraceBase.xFree(p);
4138 }
4139 static void *memtraceRealloc(void *p, int n){
4140   if( p==0 ) return memtraceMalloc(n);
4141   if( n==0 ){
4142     memtraceFree(p);
4143     return 0;
4144   }
4145   if( memtraceOut ){
4146     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4147             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4148   }
4149   return memtraceBase.xRealloc(p, n);
4150 }
4151 static int memtraceSize(void *p){
4152   return memtraceBase.xSize(p);
4153 }
4154 static int memtraceRoundup(int n){
4155   return memtraceBase.xRoundup(n);
4156 }
4157 static int memtraceInit(void *p){
4158   return memtraceBase.xInit(p);
4159 }
4160 static void memtraceShutdown(void *p){
4161   memtraceBase.xShutdown(p);
4162 }
4163
4164 /* The substitute memory allocator */
4165 static sqlite3_mem_methods ersaztMethods = {
4166   memtraceMalloc,
4167   memtraceFree,
4168   memtraceRealloc,
4169   memtraceSize,
4170   memtraceRoundup,
4171   memtraceInit,
4172   memtraceShutdown,
4173   0
4174 };
4175
4176 /* Begin tracing memory allocations to out. */
4177 int sqlite3MemTraceActivate(FILE *out){
4178   int rc = SQLITE_OK;
4179   if( memtraceBase.xMalloc==0 ){
4180     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4181     if( rc==SQLITE_OK ){
4182       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4183     }
4184   }
4185   memtraceOut = out;
4186   return rc;
4187 }
4188
4189 /* Deactivate memory tracing */
4190 int sqlite3MemTraceDeactivate(void){
4191   int rc = SQLITE_OK;
4192   if( memtraceBase.xMalloc!=0 ){
4193     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4194     if( rc==SQLITE_OK ){
4195       memset(&memtraceBase, 0, sizeof(memtraceBase));
4196     }
4197   }
4198   memtraceOut = 0;
4199   return rc;
4200 }
4201
4202 /************************* End ../ext/misc/memtrace.c ********************/
4203 #ifdef SQLITE_HAVE_ZLIB
4204 /************************* Begin ../ext/misc/zipfile.c ******************/
4205 /*
4206 ** 2017-12-26
4207 **
4208 ** The author disclaims copyright to this source code.  In place of
4209 ** a legal notice, here is a blessing:
4210 **
4211 **    May you do good and not evil.
4212 **    May you find forgiveness for yourself and forgive others.
4213 **    May you share freely, never taking more than you give.
4214 **
4215 ******************************************************************************
4216 **
4217 ** This file implements a virtual table for reading and writing ZIP archive
4218 ** files.
4219 **
4220 ** Usage example:
4221 **
4222 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4223 **
4224 ** Current limitations:
4225 **
4226 **    *  No support for encryption
4227 **    *  No support for ZIP archives spanning multiple files
4228 **    *  No support for zip64 extensions
4229 **    *  Only the "inflate/deflate" (zlib) compression method is supported
4230 */
4231 SQLITE_EXTENSION_INIT1
4232 #include <stdio.h>
4233 #include <string.h>
4234 #include <assert.h>
4235
4236 #include <zlib.h>
4237
4238 #ifndef SQLITE_OMIT_VIRTUALTABLE
4239
4240 #ifndef SQLITE_AMALGAMATION
4241
4242 /* typedef sqlite3_int64 i64; */
4243 /* typedef unsigned char u8; */
4244 typedef unsigned short u16;
4245 typedef unsigned long u32;
4246 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4247
4248 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4249 # define ALWAYS(X)      (1)
4250 # define NEVER(X)       (0)
4251 #elif !defined(NDEBUG)
4252 # define ALWAYS(X)      ((X)?1:(assert(0),0))
4253 # define NEVER(X)       ((X)?(assert(0),1):0)
4254 #else
4255 # define ALWAYS(X)      (X)
4256 # define NEVER(X)       (X)
4257 #endif
4258
4259 #endif   /* SQLITE_AMALGAMATION */
4260
4261 /*
4262 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4263 **
4264 ** In some ways it would be better to obtain these values from system 
4265 ** header files. But, the dependency is undesirable and (a) these
4266 ** have been stable for decades, (b) the values are part of POSIX and
4267 ** are also made explicit in [man stat], and (c) are part of the 
4268 ** file format for zip archives.
4269 */
4270 #ifndef S_IFDIR
4271 # define S_IFDIR 0040000
4272 #endif
4273 #ifndef S_IFREG
4274 # define S_IFREG 0100000
4275 #endif
4276 #ifndef S_IFLNK
4277 # define S_IFLNK 0120000
4278 #endif
4279
4280 static const char ZIPFILE_SCHEMA[] = 
4281   "CREATE TABLE y("
4282     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
4283     "mode,"              /* 1: POSIX mode for file */
4284     "mtime,"             /* 2: Last modification time (secs since 1970)*/
4285     "sz,"                /* 3: Size of object */
4286     "rawdata,"           /* 4: Raw data */
4287     "data,"              /* 5: Uncompressed data */
4288     "method,"            /* 6: Compression method (integer) */
4289     "z HIDDEN"           /* 7: Name of zip file */
4290   ") WITHOUT ROWID;";
4291
4292 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
4293 #define ZIPFILE_BUFFER_SIZE (64*1024)
4294
4295
4296 /*
4297 ** Magic numbers used to read and write zip files.
4298 **
4299 ** ZIPFILE_NEWENTRY_MADEBY:
4300 **   Use this value for the "version-made-by" field in new zip file
4301 **   entries. The upper byte indicates "unix", and the lower byte 
4302 **   indicates that the zip file matches pkzip specification 3.0. 
4303 **   This is what info-zip seems to do.
4304 **
4305 ** ZIPFILE_NEWENTRY_REQUIRED:
4306 **   Value for "version-required-to-extract" field of new entries.
4307 **   Version 2.0 is required to support folders and deflate compression.
4308 **
4309 ** ZIPFILE_NEWENTRY_FLAGS:
4310 **   Value for "general-purpose-bit-flags" field of new entries. Bit
4311 **   11 means "utf-8 filename and comment".
4312 **
4313 ** ZIPFILE_SIGNATURE_CDS:
4314 **   First 4 bytes of a valid CDS record.
4315 **
4316 ** ZIPFILE_SIGNATURE_LFH:
4317 **   First 4 bytes of a valid LFH record.
4318 **
4319 ** ZIPFILE_SIGNATURE_EOCD
4320 **   First 4 bytes of a valid EOCD record.
4321 */
4322 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
4323 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
4324 #define ZIPFILE_NEWENTRY_REQUIRED 20
4325 #define ZIPFILE_NEWENTRY_FLAGS    0x800
4326 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
4327 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
4328 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
4329
4330 /*
4331 ** The sizes of the fixed-size part of each of the three main data 
4332 ** structures in a zip archive.
4333 */
4334 #define ZIPFILE_LFH_FIXED_SZ      30
4335 #define ZIPFILE_EOCD_FIXED_SZ     22
4336 #define ZIPFILE_CDS_FIXED_SZ      46
4337
4338 /*
4339 *** 4.3.16  End of central directory record:
4340 ***
4341 ***   end of central dir signature    4 bytes  (0x06054b50)
4342 ***   number of this disk             2 bytes
4343 ***   number of the disk with the
4344 ***   start of the central directory  2 bytes
4345 ***   total number of entries in the
4346 ***   central directory on this disk  2 bytes
4347 ***   total number of entries in
4348 ***   the central directory           2 bytes
4349 ***   size of the central directory   4 bytes
4350 ***   offset of start of central
4351 ***   directory with respect to
4352 ***   the starting disk number        4 bytes
4353 ***   .ZIP file comment length        2 bytes
4354 ***   .ZIP file comment       (variable size)
4355 */
4356 typedef struct ZipfileEOCD ZipfileEOCD;
4357 struct ZipfileEOCD {
4358   u16 iDisk;
4359   u16 iFirstDisk;
4360   u16 nEntry;
4361   u16 nEntryTotal;
4362   u32 nSize;
4363   u32 iOffset;
4364 };
4365
4366 /*
4367 *** 4.3.12  Central directory structure:
4368 ***
4369 *** ...
4370 ***
4371 ***   central file header signature   4 bytes  (0x02014b50)
4372 ***   version made by                 2 bytes
4373 ***   version needed to extract       2 bytes
4374 ***   general purpose bit flag        2 bytes
4375 ***   compression method              2 bytes
4376 ***   last mod file time              2 bytes
4377 ***   last mod file date              2 bytes
4378 ***   crc-32                          4 bytes
4379 ***   compressed size                 4 bytes
4380 ***   uncompressed size               4 bytes
4381 ***   file name length                2 bytes
4382 ***   extra field length              2 bytes
4383 ***   file comment length             2 bytes
4384 ***   disk number start               2 bytes
4385 ***   internal file attributes        2 bytes
4386 ***   external file attributes        4 bytes
4387 ***   relative offset of local header 4 bytes
4388 */
4389 typedef struct ZipfileCDS ZipfileCDS;
4390 struct ZipfileCDS {
4391   u16 iVersionMadeBy;
4392   u16 iVersionExtract;
4393   u16 flags;
4394   u16 iCompression;
4395   u16 mTime;
4396   u16 mDate;
4397   u32 crc32;
4398   u32 szCompressed;
4399   u32 szUncompressed;
4400   u16 nFile;
4401   u16 nExtra;
4402   u16 nComment;
4403   u16 iDiskStart;
4404   u16 iInternalAttr;
4405   u32 iExternalAttr;
4406   u32 iOffset;
4407   char *zFile;                    /* Filename (sqlite3_malloc()) */
4408 };
4409
4410 /*
4411 *** 4.3.7  Local file header:
4412 ***
4413 ***   local file header signature     4 bytes  (0x04034b50)
4414 ***   version needed to extract       2 bytes
4415 ***   general purpose bit flag        2 bytes
4416 ***   compression method              2 bytes
4417 ***   last mod file time              2 bytes
4418 ***   last mod file date              2 bytes
4419 ***   crc-32                          4 bytes
4420 ***   compressed size                 4 bytes
4421 ***   uncompressed size               4 bytes
4422 ***   file name length                2 bytes
4423 ***   extra field length              2 bytes
4424 ***   
4425 */
4426 typedef struct ZipfileLFH ZipfileLFH;
4427 struct ZipfileLFH {
4428   u16 iVersionExtract;
4429   u16 flags;
4430   u16 iCompression;
4431   u16 mTime;
4432   u16 mDate;
4433   u32 crc32;
4434   u32 szCompressed;
4435   u32 szUncompressed;
4436   u16 nFile;
4437   u16 nExtra;
4438 };
4439
4440 typedef struct ZipfileEntry ZipfileEntry;
4441 struct ZipfileEntry {
4442   ZipfileCDS cds;            /* Parsed CDS record */
4443   u32 mUnixTime;             /* Modification time, in UNIX format */
4444   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
4445   i64 iDataOff;              /* Offset to data in file (if aData==0) */
4446   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
4447   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
4448 };
4449
4450 /* 
4451 ** Cursor type for zipfile tables.
4452 */
4453 typedef struct ZipfileCsr ZipfileCsr;
4454 struct ZipfileCsr {
4455   sqlite3_vtab_cursor base;  /* Base class - must be first */
4456   i64 iId;                   /* Cursor ID */
4457   u8 bEof;                   /* True when at EOF */
4458   u8 bNoop;                  /* If next xNext() call is no-op */
4459
4460   /* Used outside of write transactions */
4461   FILE *pFile;               /* Zip file */
4462   i64 iNextOff;              /* Offset of next record in central directory */
4463   ZipfileEOCD eocd;          /* Parse of central directory record */
4464
4465   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
4466   ZipfileEntry *pCurrent;    /* Current entry */
4467   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
4468 };
4469
4470 typedef struct ZipfileTab ZipfileTab;
4471 struct ZipfileTab {
4472   sqlite3_vtab base;         /* Base class - must be first */
4473   char *zFile;               /* Zip file this table accesses (may be NULL) */
4474   sqlite3 *db;               /* Host database connection */
4475   u8 *aBuffer;               /* Temporary buffer used for various tasks */
4476
4477   ZipfileCsr *pCsrList;      /* List of cursors */
4478   i64 iNextCsrid;
4479
4480   /* The following are used by write transactions only */
4481   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4482   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
4483   FILE *pWriteFd;            /* File handle open on zip archive */
4484   i64 szCurrent;             /* Current size of zip archive */
4485   i64 szOrig;                /* Size of archive at start of transaction */
4486 };
4487
4488 /*
4489 ** Set the error message contained in context ctx to the results of
4490 ** vprintf(zFmt, ...).
4491 */
4492 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4493   char *zMsg = 0;
4494   va_list ap;
4495   va_start(ap, zFmt);
4496   zMsg = sqlite3_vmprintf(zFmt, ap);
4497   sqlite3_result_error(ctx, zMsg, -1);
4498   sqlite3_free(zMsg);
4499   va_end(ap);
4500 }
4501
4502 /*
4503 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4504 ** is not quoted, do nothing.
4505 */
4506 static void zipfileDequote(char *zIn){
4507   char q = zIn[0];
4508   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4509     int iIn = 1;
4510     int iOut = 0;
4511     if( q=='[' ) q = ']';
4512     while( ALWAYS(zIn[iIn]) ){
4513       char c = zIn[iIn++];
4514       if( c==q && zIn[iIn++]!=q ) break;
4515       zIn[iOut++] = c;
4516     }
4517     zIn[iOut] = '\0';
4518   }
4519 }
4520
4521 /*
4522 ** Construct a new ZipfileTab virtual table object.
4523 ** 
4524 **   argv[0]   -> module name  ("zipfile")
4525 **   argv[1]   -> database name
4526 **   argv[2]   -> table name
4527 **   argv[...] -> "column name" and other module argument fields.
4528 */
4529 static int zipfileConnect(
4530   sqlite3 *db,
4531   void *pAux,
4532   int argc, const char *const*argv,
4533   sqlite3_vtab **ppVtab,
4534   char **pzErr
4535 ){
4536   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4537   int nFile = 0;
4538   const char *zFile = 0;
4539   ZipfileTab *pNew = 0;
4540   int rc;
4541
4542   /* If the table name is not "zipfile", require that the argument be
4543   ** specified. This stops zipfile tables from being created as:
4544   **
4545   **   CREATE VIRTUAL TABLE zzz USING zipfile();
4546   **
4547   ** It does not prevent:
4548   **
4549   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
4550   */
4551   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4552   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4553     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4554     return SQLITE_ERROR;
4555   }
4556
4557   if( argc>3 ){
4558     zFile = argv[3];
4559     nFile = (int)strlen(zFile)+1;
4560   }
4561
4562   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4563   if( rc==SQLITE_OK ){
4564     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
4565     if( pNew==0 ) return SQLITE_NOMEM;
4566     memset(pNew, 0, nByte+nFile);
4567     pNew->db = db;
4568     pNew->aBuffer = (u8*)&pNew[1];
4569     if( zFile ){
4570       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4571       memcpy(pNew->zFile, zFile, nFile);
4572       zipfileDequote(pNew->zFile);
4573     }
4574   }
4575   *ppVtab = (sqlite3_vtab*)pNew;
4576   return rc;
4577 }
4578
4579 /*
4580 ** Free the ZipfileEntry structure indicated by the only argument.
4581 */
4582 static void zipfileEntryFree(ZipfileEntry *p){
4583   if( p ){
4584     sqlite3_free(p->cds.zFile);
4585     sqlite3_free(p);
4586   }
4587 }
4588
4589 /*
4590 ** Release resources that should be freed at the end of a write 
4591 ** transaction.
4592 */
4593 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4594   ZipfileEntry *pEntry;
4595   ZipfileEntry *pNext;
4596
4597   if( pTab->pWriteFd ){
4598     fclose(pTab->pWriteFd);
4599     pTab->pWriteFd = 0;
4600   }
4601   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4602     pNext = pEntry->pNext;
4603     zipfileEntryFree(pEntry);
4604   }
4605   pTab->pFirstEntry = 0;
4606   pTab->pLastEntry = 0;
4607   pTab->szCurrent = 0;
4608   pTab->szOrig = 0;
4609 }
4610
4611 /*
4612 ** This method is the destructor for zipfile vtab objects.
4613 */
4614 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4615   zipfileCleanupTransaction((ZipfileTab*)pVtab);
4616   sqlite3_free(pVtab);
4617   return SQLITE_OK;
4618 }
4619
4620 /*
4621 ** Constructor for a new ZipfileCsr object.
4622 */
4623 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4624   ZipfileTab *pTab = (ZipfileTab*)p;
4625   ZipfileCsr *pCsr;
4626   pCsr = sqlite3_malloc(sizeof(*pCsr));
4627   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4628   if( pCsr==0 ){
4629     return SQLITE_NOMEM;
4630   }
4631   memset(pCsr, 0, sizeof(*pCsr));
4632   pCsr->iId = ++pTab->iNextCsrid;
4633   pCsr->pCsrNext = pTab->pCsrList;
4634   pTab->pCsrList = pCsr;
4635   return SQLITE_OK;
4636 }
4637
4638 /*
4639 ** Reset a cursor back to the state it was in when first returned
4640 ** by zipfileOpen().
4641 */
4642 static void zipfileResetCursor(ZipfileCsr *pCsr){
4643   ZipfileEntry *p;
4644   ZipfileEntry *pNext;
4645
4646   pCsr->bEof = 0;
4647   if( pCsr->pFile ){
4648     fclose(pCsr->pFile);
4649     pCsr->pFile = 0;
4650     zipfileEntryFree(pCsr->pCurrent);
4651     pCsr->pCurrent = 0;
4652   }
4653
4654   for(p=pCsr->pFreeEntry; p; p=pNext){
4655     pNext = p->pNext;
4656     zipfileEntryFree(p);
4657   }
4658 }
4659
4660 /*
4661 ** Destructor for an ZipfileCsr.
4662 */
4663 static int zipfileClose(sqlite3_vtab_cursor *cur){
4664   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4665   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4666   ZipfileCsr **pp;
4667   zipfileResetCursor(pCsr);
4668
4669   /* Remove this cursor from the ZipfileTab.pCsrList list. */
4670   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4671   *pp = pCsr->pCsrNext;
4672
4673   sqlite3_free(pCsr);
4674   return SQLITE_OK;
4675 }
4676
4677 /*
4678 ** Set the error message for the virtual table associated with cursor
4679 ** pCsr to the results of vprintf(zFmt, ...).
4680 */
4681 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4682   va_list ap;
4683   va_start(ap, zFmt);
4684   sqlite3_free(pTab->base.zErrMsg);
4685   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4686   va_end(ap);
4687 }
4688 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4689   va_list ap;
4690   va_start(ap, zFmt);
4691   sqlite3_free(pCsr->base.pVtab->zErrMsg);
4692   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4693   va_end(ap);
4694 }
4695
4696 /*
4697 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4698 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4699 ** otherwise. 
4700 **
4701 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4702 ** to an English language error message. It is the responsibility of the
4703 ** caller to eventually free this buffer using
4704 ** sqlite3_free().
4705 */
4706 static int zipfileReadData(
4707   FILE *pFile,                    /* Read from this file */
4708   u8 *aRead,                      /* Read into this buffer */
4709   int nRead,                      /* Number of bytes to read */
4710   i64 iOff,                       /* Offset to read from */
4711   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
4712 ){
4713   size_t n;
4714   fseek(pFile, (long)iOff, SEEK_SET);
4715   n = fread(aRead, 1, nRead, pFile);
4716   if( (int)n!=nRead ){
4717     *pzErrmsg = sqlite3_mprintf("error in fread()");
4718     return SQLITE_ERROR;
4719   }
4720   return SQLITE_OK;
4721 }
4722
4723 static int zipfileAppendData(
4724   ZipfileTab *pTab,
4725   const u8 *aWrite,
4726   int nWrite
4727 ){
4728   size_t n;
4729   fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4730   n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4731   if( (int)n!=nWrite ){
4732     pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4733     return SQLITE_ERROR;
4734   }
4735   pTab->szCurrent += nWrite;
4736   return SQLITE_OK;
4737 }
4738
4739 /*
4740 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4741 */
4742 static u16 zipfileGetU16(const u8 *aBuf){
4743   return (aBuf[1] << 8) + aBuf[0];
4744 }
4745
4746 /*
4747 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4748 */
4749 static u32 zipfileGetU32(const u8 *aBuf){
4750   return ((u32)(aBuf[3]) << 24)
4751        + ((u32)(aBuf[2]) << 16)
4752        + ((u32)(aBuf[1]) <<  8)
4753        + ((u32)(aBuf[0]) <<  0);
4754 }
4755
4756 /*
4757 ** Write a 16-bit little endiate integer into buffer aBuf.
4758 */
4759 static void zipfilePutU16(u8 *aBuf, u16 val){
4760   aBuf[0] = val & 0xFF;
4761   aBuf[1] = (val>>8) & 0xFF;
4762 }
4763
4764 /*
4765 ** Write a 32-bit little endiate integer into buffer aBuf.
4766 */
4767 static void zipfilePutU32(u8 *aBuf, u32 val){
4768   aBuf[0] = val & 0xFF;
4769   aBuf[1] = (val>>8) & 0xFF;
4770   aBuf[2] = (val>>16) & 0xFF;
4771   aBuf[3] = (val>>24) & 0xFF;
4772 }
4773
4774 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4775 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4776
4777 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4778 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4779
4780 /*
4781 ** Magic numbers used to read CDS records.
4782 */
4783 #define ZIPFILE_CDS_NFILE_OFF        28
4784 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4785
4786 /*
4787 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4788 ** if the record is not well-formed, or SQLITE_OK otherwise.
4789 */
4790 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4791   u8 *aRead = aBuf;
4792   u32 sig = zipfileRead32(aRead);
4793   int rc = SQLITE_OK;
4794   if( sig!=ZIPFILE_SIGNATURE_CDS ){
4795     rc = SQLITE_ERROR;
4796   }else{
4797     pCDS->iVersionMadeBy = zipfileRead16(aRead);
4798     pCDS->iVersionExtract = zipfileRead16(aRead);
4799     pCDS->flags = zipfileRead16(aRead);
4800     pCDS->iCompression = zipfileRead16(aRead);
4801     pCDS->mTime = zipfileRead16(aRead);
4802     pCDS->mDate = zipfileRead16(aRead);
4803     pCDS->crc32 = zipfileRead32(aRead);
4804     pCDS->szCompressed = zipfileRead32(aRead);
4805     pCDS->szUncompressed = zipfileRead32(aRead);
4806     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4807     pCDS->nFile = zipfileRead16(aRead);
4808     pCDS->nExtra = zipfileRead16(aRead);
4809     pCDS->nComment = zipfileRead16(aRead);
4810     pCDS->iDiskStart = zipfileRead16(aRead);
4811     pCDS->iInternalAttr = zipfileRead16(aRead);
4812     pCDS->iExternalAttr = zipfileRead32(aRead);
4813     pCDS->iOffset = zipfileRead32(aRead);
4814     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4815   }
4816
4817   return rc;
4818 }
4819
4820 /*
4821 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4822 ** if the record is not well-formed, or SQLITE_OK otherwise.
4823 */
4824 static int zipfileReadLFH(
4825   u8 *aBuffer,
4826   ZipfileLFH *pLFH
4827 ){
4828   u8 *aRead = aBuffer;
4829   int rc = SQLITE_OK;
4830
4831   u32 sig = zipfileRead32(aRead);
4832   if( sig!=ZIPFILE_SIGNATURE_LFH ){
4833     rc = SQLITE_ERROR;
4834   }else{
4835     pLFH->iVersionExtract = zipfileRead16(aRead);
4836     pLFH->flags = zipfileRead16(aRead);
4837     pLFH->iCompression = zipfileRead16(aRead);
4838     pLFH->mTime = zipfileRead16(aRead);
4839     pLFH->mDate = zipfileRead16(aRead);
4840     pLFH->crc32 = zipfileRead32(aRead);
4841     pLFH->szCompressed = zipfileRead32(aRead);
4842     pLFH->szUncompressed = zipfileRead32(aRead);
4843     pLFH->nFile = zipfileRead16(aRead);
4844     pLFH->nExtra = zipfileRead16(aRead);
4845   }
4846   return rc;
4847 }
4848
4849
4850 /*
4851 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4852 ** Scan through this buffer to find an "extra-timestamp" field. If one
4853 ** exists, extract the 32-bit modification-timestamp from it and store
4854 ** the value in output parameter *pmTime.
4855 **
4856 ** Zero is returned if no extra-timestamp record could be found (and so
4857 ** *pmTime is left unchanged), or non-zero otherwise.
4858 **
4859 ** The general format of an extra field is:
4860 **
4861 **   Header ID    2 bytes
4862 **   Data Size    2 bytes
4863 **   Data         N bytes
4864 */
4865 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4866   int ret = 0;
4867   u8 *p = aExtra;
4868   u8 *pEnd = &aExtra[nExtra];
4869
4870   while( p<pEnd ){
4871     u16 id = zipfileRead16(p);
4872     u16 nByte = zipfileRead16(p);
4873
4874     switch( id ){
4875       case ZIPFILE_EXTRA_TIMESTAMP: {
4876         u8 b = p[0];
4877         if( b & 0x01 ){     /* 0x01 -> modtime is present */
4878           *pmTime = zipfileGetU32(&p[1]);
4879           ret = 1;
4880         }
4881         break;
4882       }
4883     }
4884
4885     p += nByte;
4886   }
4887   return ret;
4888 }
4889
4890 /*
4891 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4892 ** fields of the CDS structure passed as the only argument to a 32-bit
4893 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4894 **
4895 ** "Standard" MS-DOS time format:
4896 **
4897 **   File modification time:
4898 **     Bits 00-04: seconds divided by 2
4899 **     Bits 05-10: minute
4900 **     Bits 11-15: hour
4901 **   File modification date:
4902 **     Bits 00-04: day
4903 **     Bits 05-08: month (1-12)
4904 **     Bits 09-15: years from 1980 
4905 **
4906 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4907 */
4908 static u32 zipfileMtime(ZipfileCDS *pCDS){
4909   int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4910   int M = ((pCDS->mDate >> 5) & 0x0F);
4911   int D = (pCDS->mDate & 0x1F);
4912   int B = -13;
4913
4914   int sec = (pCDS->mTime & 0x1F)*2;
4915   int min = (pCDS->mTime >> 5) & 0x3F;
4916   int hr = (pCDS->mTime >> 11) & 0x1F;
4917   i64 JD;
4918
4919   /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4920
4921   /* Calculate the JD in seconds for noon on the day in question */
4922   if( M<3 ){
4923     Y = Y-1;
4924     M = M+12;
4925   }
4926   JD = (i64)(24*60*60) * (
4927       (int)(365.25 * (Y + 4716))
4928     + (int)(30.6001 * (M + 1))
4929     + D + B - 1524
4930   );
4931
4932   /* Correct the JD for the time within the day */
4933   JD += (hr-12) * 3600 + min * 60 + sec;
4934
4935   /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4936   return (u32)(JD - (i64)(24405875) * 24*60*6);
4937 }
4938
4939 /*
4940 ** The opposite of zipfileMtime(). This function populates the mTime and
4941 ** mDate fields of the CDS structure passed as the first argument according
4942 ** to the UNIX timestamp value passed as the second.
4943 */
4944 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4945   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4946   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4947
4948   int A, B, C, D, E;
4949   int yr, mon, day;
4950   int hr, min, sec;
4951
4952   A = (int)((JD - 1867216.25)/36524.25);
4953   A = (int)(JD + 1 + A - (A/4));
4954   B = A + 1524;
4955   C = (int)((B - 122.1)/365.25);
4956   D = (36525*(C&32767))/100;
4957   E = (int)((B-D)/30.6001);
4958
4959   day = B - D - (int)(30.6001*E);
4960   mon = (E<14 ? E-1 : E-13);
4961   yr = mon>2 ? C-4716 : C-4715;
4962
4963   hr = (mUnixTime % (24*60*60)) / (60*60);
4964   min = (mUnixTime % (60*60)) / 60;
4965   sec = (mUnixTime % 60);
4966
4967   if( yr>=1980 ){
4968     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4969     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4970   }else{
4971     pCds->mDate = pCds->mTime = 0;
4972   }
4973
4974   assert( mUnixTime<315507600 
4975        || mUnixTime==zipfileMtime(pCds) 
4976        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
4977        /* || (mUnixTime % 2) */
4978   );
4979 }
4980
4981 /*
4982 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4983 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4984 ** then pFile is a file-handle open on a zip file. In either case, this
4985 ** function creates a ZipfileEntry object based on the zip archive entry
4986 ** for which the CDS record is at offset iOff.
4987 **
4988 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4989 ** the new object. Otherwise, an SQLite error code is returned and the
4990 ** final value of (*ppEntry) undefined.
4991 */
4992 static int zipfileGetEntry(
4993   ZipfileTab *pTab,               /* Store any error message here */
4994   const u8 *aBlob,                /* Pointer to in-memory file image */
4995   int nBlob,                      /* Size of aBlob[] in bytes */
4996   FILE *pFile,                    /* If aBlob==0, read from this file */
4997   i64 iOff,                       /* Offset of CDS record */
4998   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
4999 ){
5000   u8 *aRead;
5001   char **pzErr = &pTab->base.zErrMsg;
5002   int rc = SQLITE_OK;
5003
5004   if( aBlob==0 ){
5005     aRead = pTab->aBuffer;
5006     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
5007   }else{
5008     aRead = (u8*)&aBlob[iOff];
5009   }
5010
5011   if( rc==SQLITE_OK ){
5012     sqlite3_int64 nAlloc;
5013     ZipfileEntry *pNew;
5014
5015     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
5016     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
5017     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
5018
5019     nAlloc = sizeof(ZipfileEntry) + nExtra;
5020     if( aBlob ){
5021       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
5022     }
5023
5024     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
5025     if( pNew==0 ){
5026       rc = SQLITE_NOMEM;
5027     }else{
5028       memset(pNew, 0, sizeof(ZipfileEntry));
5029       rc = zipfileReadCDS(aRead, &pNew->cds);
5030       if( rc!=SQLITE_OK ){
5031         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
5032       }else if( aBlob==0 ){
5033         rc = zipfileReadData(
5034             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
5035         );
5036       }else{
5037         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
5038       }
5039     }
5040
5041     if( rc==SQLITE_OK ){
5042       u32 *pt = &pNew->mUnixTime;
5043       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
5044       pNew->aExtra = (u8*)&pNew[1];
5045       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
5046       if( pNew->cds.zFile==0 ){
5047         rc = SQLITE_NOMEM;
5048       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
5049         pNew->mUnixTime = zipfileMtime(&pNew->cds);
5050       }
5051     }
5052
5053     if( rc==SQLITE_OK ){
5054       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
5055       ZipfileLFH lfh;
5056       if( pFile ){
5057         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
5058       }else{
5059         aRead = (u8*)&aBlob[pNew->cds.iOffset];
5060       }
5061
5062       rc = zipfileReadLFH(aRead, &lfh);
5063       if( rc==SQLITE_OK ){
5064         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
5065         pNew->iDataOff += lfh.nFile + lfh.nExtra;
5066         if( aBlob && pNew->cds.szCompressed ){
5067           pNew->aData = &pNew->aExtra[nExtra];
5068           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
5069         }
5070       }else{
5071         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
5072             (int)pNew->cds.iOffset
5073         );
5074       }
5075     }
5076
5077     if( rc!=SQLITE_OK ){
5078       zipfileEntryFree(pNew);
5079     }else{
5080       *ppEntry = pNew;
5081     }
5082   }
5083
5084   return rc;
5085 }
5086
5087 /*
5088 ** Advance an ZipfileCsr to its next row of output.
5089 */
5090 static int zipfileNext(sqlite3_vtab_cursor *cur){
5091   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5092   int rc = SQLITE_OK;
5093
5094   if( pCsr->pFile ){
5095     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
5096     zipfileEntryFree(pCsr->pCurrent);
5097     pCsr->pCurrent = 0;
5098     if( pCsr->iNextOff>=iEof ){
5099       pCsr->bEof = 1;
5100     }else{
5101       ZipfileEntry *p = 0;
5102       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
5103       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
5104       if( rc==SQLITE_OK ){
5105         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
5106         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
5107       }
5108       pCsr->pCurrent = p;
5109     }
5110   }else{
5111     if( !pCsr->bNoop ){
5112       pCsr->pCurrent = pCsr->pCurrent->pNext;
5113     }
5114     if( pCsr->pCurrent==0 ){
5115       pCsr->bEof = 1;
5116     }
5117   }
5118
5119   pCsr->bNoop = 0;
5120   return rc;
5121 }
5122
5123 static void zipfileFree(void *p) { 
5124   sqlite3_free(p); 
5125 }
5126
5127 /*
5128 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
5129 ** size is nOut bytes. This function uncompresses the data and sets the
5130 ** return value in context pCtx to the result (a blob).
5131 **
5132 ** If an error occurs, an error code is left in pCtx instead.
5133 */
5134 static void zipfileInflate(
5135   sqlite3_context *pCtx,          /* Store result here */
5136   const u8 *aIn,                  /* Compressed data */
5137   int nIn,                        /* Size of buffer aIn[] in bytes */
5138   int nOut                        /* Expected output size */
5139 ){
5140   u8 *aRes = sqlite3_malloc(nOut);
5141   if( aRes==0 ){
5142     sqlite3_result_error_nomem(pCtx);
5143   }else{
5144     int err;
5145     z_stream str;
5146     memset(&str, 0, sizeof(str));
5147
5148     str.next_in = (Byte*)aIn;
5149     str.avail_in = nIn;
5150     str.next_out = (Byte*)aRes;
5151     str.avail_out = nOut;
5152
5153     err = inflateInit2(&str, -15);
5154     if( err!=Z_OK ){
5155       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
5156     }else{
5157       err = inflate(&str, Z_NO_FLUSH);
5158       if( err!=Z_STREAM_END ){
5159         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
5160       }else{
5161         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
5162         aRes = 0;
5163       }
5164     }
5165     sqlite3_free(aRes);
5166     inflateEnd(&str);
5167   }
5168 }
5169
5170 /*
5171 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
5172 ** compresses it and sets (*ppOut) to point to a buffer containing the
5173 ** compressed data. The caller is responsible for eventually calling
5174 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
5175 ** is set to the size of buffer (*ppOut) in bytes.
5176 **
5177 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
5178 ** code is returned and an error message left in virtual-table handle
5179 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5180 ** case.
5181 */
5182 static int zipfileDeflate(
5183   const u8 *aIn, int nIn,         /* Input */
5184   u8 **ppOut, int *pnOut,         /* Output */
5185   char **pzErr                    /* OUT: Error message */
5186 ){
5187   sqlite3_int64 nAlloc = compressBound(nIn);
5188   u8 *aOut;
5189   int rc = SQLITE_OK;
5190
5191   aOut = (u8*)sqlite3_malloc64(nAlloc);
5192   if( aOut==0 ){
5193     rc = SQLITE_NOMEM;
5194   }else{
5195     int res;
5196     z_stream str;
5197     memset(&str, 0, sizeof(str));
5198     str.next_in = (Bytef*)aIn;
5199     str.avail_in = nIn;
5200     str.next_out = aOut;
5201     str.avail_out = nAlloc;
5202
5203     deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5204     res = deflate(&str, Z_FINISH);
5205
5206     if( res==Z_STREAM_END ){
5207       *ppOut = aOut;
5208       *pnOut = (int)str.total_out;
5209     }else{
5210       sqlite3_free(aOut);
5211       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5212       rc = SQLITE_ERROR;
5213     }
5214     deflateEnd(&str);
5215   }
5216
5217   return rc;
5218 }
5219
5220
5221 /*
5222 ** Return values of columns for the row at which the series_cursor
5223 ** is currently pointing.
5224 */
5225 static int zipfileColumn(
5226   sqlite3_vtab_cursor *cur,   /* The cursor */
5227   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5228   int i                       /* Which column to return */
5229 ){
5230   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5231   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5232   int rc = SQLITE_OK;
5233   switch( i ){
5234     case 0:   /* name */
5235       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5236       break;
5237     case 1:   /* mode */
5238       /* TODO: Whether or not the following is correct surely depends on
5239       ** the platform on which the archive was created.  */
5240       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5241       break;
5242     case 2: { /* mtime */
5243       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5244       break;
5245     }
5246     case 3: { /* sz */
5247       if( sqlite3_vtab_nochange(ctx)==0 ){
5248         sqlite3_result_int64(ctx, pCDS->szUncompressed);
5249       }
5250       break;
5251     }
5252     case 4:   /* rawdata */
5253       if( sqlite3_vtab_nochange(ctx) ) break;
5254     case 5: { /* data */
5255       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5256         int sz = pCDS->szCompressed;
5257         int szFinal = pCDS->szUncompressed;
5258         if( szFinal>0 ){
5259           u8 *aBuf;
5260           u8 *aFree = 0;
5261           if( pCsr->pCurrent->aData ){
5262             aBuf = pCsr->pCurrent->aData;
5263           }else{
5264             aBuf = aFree = sqlite3_malloc64(sz);
5265             if( aBuf==0 ){
5266               rc = SQLITE_NOMEM;
5267             }else{
5268               FILE *pFile = pCsr->pFile;
5269               if( pFile==0 ){
5270                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5271               }
5272               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5273                   &pCsr->base.pVtab->zErrMsg
5274               );
5275             }
5276           }
5277           if( rc==SQLITE_OK ){
5278             if( i==5 && pCDS->iCompression ){
5279               zipfileInflate(ctx, aBuf, sz, szFinal);
5280             }else{
5281               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5282             }
5283           }
5284           sqlite3_free(aFree);
5285         }else{
5286           /* Figure out if this is a directory or a zero-sized file. Consider
5287           ** it to be a directory either if the mode suggests so, or if
5288           ** the final character in the name is '/'.  */
5289           u32 mode = pCDS->iExternalAttr >> 16;
5290           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5291             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5292           }
5293         }
5294       }
5295       break;
5296     }
5297     case 6:   /* method */
5298       sqlite3_result_int(ctx, pCDS->iCompression);
5299       break;
5300     default:  /* z */
5301       assert( i==7 );
5302       sqlite3_result_int64(ctx, pCsr->iId);
5303       break;
5304   }
5305
5306   return rc;
5307 }
5308
5309 /*
5310 ** Return TRUE if the cursor is at EOF.
5311 */
5312 static int zipfileEof(sqlite3_vtab_cursor *cur){
5313   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5314   return pCsr->bEof;
5315 }
5316
5317 /*
5318 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5319 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5320 ** is guaranteed to be a file-handle open on a zip file.
5321 **
5322 ** This function attempts to locate the EOCD record within the zip archive
5323 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5324 ** returned if successful. Otherwise, an SQLite error code is returned and
5325 ** an English language error message may be left in virtual-table pTab.
5326 */
5327 static int zipfileReadEOCD(
5328   ZipfileTab *pTab,               /* Return errors here */
5329   const u8 *aBlob,                /* Pointer to in-memory file image */
5330   int nBlob,                      /* Size of aBlob[] in bytes */
5331   FILE *pFile,                    /* Read from this file if aBlob==0 */
5332   ZipfileEOCD *pEOCD              /* Object to populate */
5333 ){
5334   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
5335   int nRead;                      /* Bytes to read from file */
5336   int rc = SQLITE_OK;
5337
5338   if( aBlob==0 ){
5339     i64 iOff;                     /* Offset to read from */
5340     i64 szFile;                   /* Total size of file in bytes */
5341     fseek(pFile, 0, SEEK_END);
5342     szFile = (i64)ftell(pFile);
5343     if( szFile==0 ){
5344       memset(pEOCD, 0, sizeof(ZipfileEOCD));
5345       return SQLITE_OK;
5346     }
5347     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5348     iOff = szFile - nRead;
5349     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5350   }else{
5351     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5352     aRead = (u8*)&aBlob[nBlob-nRead];
5353   }
5354
5355   if( rc==SQLITE_OK ){
5356     int i;
5357
5358     /* Scan backwards looking for the signature bytes */
5359     for(i=nRead-20; i>=0; i--){
5360       if( aRead[i]==0x50 && aRead[i+1]==0x4b 
5361        && aRead[i+2]==0x05 && aRead[i+3]==0x06 
5362       ){
5363         break;
5364       }
5365     }
5366     if( i<0 ){
5367       pTab->base.zErrMsg = sqlite3_mprintf(
5368           "cannot find end of central directory record"
5369       );
5370       return SQLITE_ERROR;
5371     }
5372
5373     aRead += i+4;
5374     pEOCD->iDisk = zipfileRead16(aRead);
5375     pEOCD->iFirstDisk = zipfileRead16(aRead);
5376     pEOCD->nEntry = zipfileRead16(aRead);
5377     pEOCD->nEntryTotal = zipfileRead16(aRead);
5378     pEOCD->nSize = zipfileRead32(aRead);
5379     pEOCD->iOffset = zipfileRead32(aRead);
5380   }
5381
5382   return rc;
5383 }
5384
5385 /*
5386 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
5387 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5388 ** to the end of the list. Otherwise, it is added to the list immediately
5389 ** before pBefore (which is guaranteed to be a part of said list).
5390 */
5391 static void zipfileAddEntry(
5392   ZipfileTab *pTab, 
5393   ZipfileEntry *pBefore, 
5394   ZipfileEntry *pNew
5395 ){
5396   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5397   assert( pNew->pNext==0 );
5398   if( pBefore==0 ){
5399     if( pTab->pFirstEntry==0 ){
5400       pTab->pFirstEntry = pTab->pLastEntry = pNew;
5401     }else{
5402       assert( pTab->pLastEntry->pNext==0 );
5403       pTab->pLastEntry->pNext = pNew;
5404       pTab->pLastEntry = pNew;
5405     }
5406   }else{
5407     ZipfileEntry **pp;
5408     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5409     pNew->pNext = pBefore;
5410     *pp = pNew;
5411   }
5412 }
5413
5414 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5415   ZipfileEOCD eocd;
5416   int rc;
5417   int i;
5418   i64 iOff;
5419
5420   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5421   iOff = eocd.iOffset;
5422   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5423     ZipfileEntry *pNew = 0;
5424     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5425
5426     if( rc==SQLITE_OK ){
5427       zipfileAddEntry(pTab, 0, pNew);
5428       iOff += ZIPFILE_CDS_FIXED_SZ;
5429       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5430     }
5431   }
5432   return rc;
5433 }
5434
5435 /*
5436 ** xFilter callback.
5437 */
5438 static int zipfileFilter(
5439   sqlite3_vtab_cursor *cur, 
5440   int idxNum, const char *idxStr,
5441   int argc, sqlite3_value **argv
5442 ){
5443   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5444   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5445   const char *zFile = 0;          /* Zip file to scan */
5446   int rc = SQLITE_OK;             /* Return Code */
5447   int bInMemory = 0;              /* True for an in-memory zipfile */
5448
5449   zipfileResetCursor(pCsr);
5450
5451   if( pTab->zFile ){
5452     zFile = pTab->zFile;
5453   }else if( idxNum==0 ){
5454     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5455     return SQLITE_ERROR;
5456   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5457     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5458     int nBlob = sqlite3_value_bytes(argv[0]);
5459     assert( pTab->pFirstEntry==0 );
5460     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5461     pCsr->pFreeEntry = pTab->pFirstEntry;
5462     pTab->pFirstEntry = pTab->pLastEntry = 0;
5463     if( rc!=SQLITE_OK ) return rc;
5464     bInMemory = 1;
5465   }else{
5466     zFile = (const char*)sqlite3_value_text(argv[0]);
5467   }
5468
5469   if( 0==pTab->pWriteFd && 0==bInMemory ){
5470     pCsr->pFile = fopen(zFile, "rb");
5471     if( pCsr->pFile==0 ){
5472       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5473       rc = SQLITE_ERROR;
5474     }else{
5475       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5476       if( rc==SQLITE_OK ){
5477         if( pCsr->eocd.nEntry==0 ){
5478           pCsr->bEof = 1;
5479         }else{
5480           pCsr->iNextOff = pCsr->eocd.iOffset;
5481           rc = zipfileNext(cur);
5482         }
5483       }
5484     }
5485   }else{
5486     pCsr->bNoop = 1;
5487     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5488     rc = zipfileNext(cur);
5489   }
5490
5491   return rc;
5492 }
5493
5494 /*
5495 ** xBestIndex callback.
5496 */
5497 static int zipfileBestIndex(
5498   sqlite3_vtab *tab,
5499   sqlite3_index_info *pIdxInfo
5500 ){
5501   int i;
5502   int idx = -1;
5503   int unusable = 0;
5504
5505   for(i=0; i<pIdxInfo->nConstraint; i++){
5506     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5507     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5508     if( pCons->usable==0 ){
5509       unusable = 1;
5510     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5511       idx = i;
5512     }
5513   }
5514   if( idx>=0 ){
5515     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
5516     pIdxInfo->aConstraintUsage[idx].omit = 1;
5517     pIdxInfo->estimatedCost = 1000.0;
5518     pIdxInfo->idxNum = 1;
5519   }else if( unusable ){
5520     return SQLITE_CONSTRAINT;
5521   }
5522   return SQLITE_OK;
5523 }
5524
5525 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5526   ZipfileEntry *pNew;
5527   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5528   if( pNew ){
5529     memset(pNew, 0, sizeof(ZipfileEntry));
5530     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5531     if( pNew->cds.zFile==0 ){
5532       sqlite3_free(pNew);
5533       pNew = 0;
5534     }
5535   }
5536   return pNew;
5537 }
5538
5539 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5540   ZipfileCDS *pCds = &pEntry->cds;
5541   u8 *a = aBuf;
5542
5543   pCds->nExtra = 9;
5544
5545   /* Write the LFH itself */
5546   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5547   zipfileWrite16(a, pCds->iVersionExtract);
5548   zipfileWrite16(a, pCds->flags);
5549   zipfileWrite16(a, pCds->iCompression);
5550   zipfileWrite16(a, pCds->mTime);
5551   zipfileWrite16(a, pCds->mDate);
5552   zipfileWrite32(a, pCds->crc32);
5553   zipfileWrite32(a, pCds->szCompressed);
5554   zipfileWrite32(a, pCds->szUncompressed);
5555   zipfileWrite16(a, (u16)pCds->nFile);
5556   zipfileWrite16(a, pCds->nExtra);
5557   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5558
5559   /* Add the file name */
5560   memcpy(a, pCds->zFile, (int)pCds->nFile);
5561   a += (int)pCds->nFile;
5562
5563   /* The "extra" data */
5564   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5565   zipfileWrite16(a, 5);
5566   *a++ = 0x01;
5567   zipfileWrite32(a, pEntry->mUnixTime);
5568
5569   return a-aBuf;
5570 }
5571
5572 static int zipfileAppendEntry(
5573   ZipfileTab *pTab,
5574   ZipfileEntry *pEntry,
5575   const u8 *pData,
5576   int nData
5577 ){
5578   u8 *aBuf = pTab->aBuffer;
5579   int nBuf;
5580   int rc;
5581
5582   nBuf = zipfileSerializeLFH(pEntry, aBuf);
5583   rc = zipfileAppendData(pTab, aBuf, nBuf);
5584   if( rc==SQLITE_OK ){
5585     pEntry->iDataOff = pTab->szCurrent;
5586     rc = zipfileAppendData(pTab, pData, nData);
5587   }
5588
5589   return rc;
5590 }
5591
5592 static int zipfileGetMode(
5593   sqlite3_value *pVal, 
5594   int bIsDir,                     /* If true, default to directory */
5595   u32 *pMode,                     /* OUT: Mode value */
5596   char **pzErr                    /* OUT: Error message */
5597 ){
5598   const char *z = (const char*)sqlite3_value_text(pVal);
5599   u32 mode = 0;
5600   if( z==0 ){
5601     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5602   }else if( z[0]>='0' && z[0]<='9' ){
5603     mode = (unsigned int)sqlite3_value_int(pVal);
5604   }else{
5605     const char zTemplate[11] = "-rwxrwxrwx";
5606     int i;
5607     if( strlen(z)!=10 ) goto parse_error;
5608     switch( z[0] ){
5609       case '-': mode |= S_IFREG; break;
5610       case 'd': mode |= S_IFDIR; break;
5611       case 'l': mode |= S_IFLNK; break;
5612       default: goto parse_error;
5613     }
5614     for(i=1; i<10; i++){
5615       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5616       else if( z[i]!='-' ) goto parse_error;
5617     }
5618   }
5619   if( ((mode & S_IFDIR)==0)==bIsDir ){
5620     /* The "mode" attribute is a directory, but data has been specified.
5621     ** Or vice-versa - no data but "mode" is a file or symlink.  */
5622     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5623     return SQLITE_CONSTRAINT;
5624   }
5625   *pMode = mode;
5626   return SQLITE_OK;
5627
5628  parse_error:
5629   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5630   return SQLITE_ERROR;
5631 }
5632
5633 /*
5634 ** Both (const char*) arguments point to nul-terminated strings. Argument
5635 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5636 ** identical, ignoring any trailing '/' character in either path.  */
5637 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5638   int nA = (int)strlen(zA);
5639   if( zA[nA-1]=='/' ) nA--;
5640   if( zB[nB-1]=='/' ) nB--;
5641   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5642   return 1;
5643 }
5644
5645 static int zipfileBegin(sqlite3_vtab *pVtab){
5646   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5647   int rc = SQLITE_OK;
5648
5649   assert( pTab->pWriteFd==0 );
5650
5651   /* Open a write fd on the file. Also load the entire central directory
5652   ** structure into memory. During the transaction any new file data is 
5653   ** appended to the archive file, but the central directory is accumulated
5654   ** in main-memory until the transaction is committed.  */
5655   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5656   if( pTab->pWriteFd==0 ){
5657     pTab->base.zErrMsg = sqlite3_mprintf(
5658         "zipfile: failed to open file %s for writing", pTab->zFile
5659         );
5660     rc = SQLITE_ERROR;
5661   }else{
5662     fseek(pTab->pWriteFd, 0, SEEK_END);
5663     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5664     rc = zipfileLoadDirectory(pTab, 0, 0);
5665   }
5666
5667   if( rc!=SQLITE_OK ){
5668     zipfileCleanupTransaction(pTab);
5669   }
5670
5671   return rc;
5672 }
5673
5674 /*
5675 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5676 ** time(2)).
5677 */
5678 static u32 zipfileTime(void){
5679   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5680   u32 ret;
5681   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5682     i64 ms;
5683     pVfs->xCurrentTimeInt64(pVfs, &ms);
5684     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5685   }else{
5686     double day;
5687     pVfs->xCurrentTime(pVfs, &day);
5688     ret = (u32)((day - 2440587.5) * 86400);
5689   }
5690   return ret;
5691 }
5692
5693 /*
5694 ** Return a 32-bit timestamp in UNIX epoch format.
5695 **
5696 ** If the value passed as the only argument is either NULL or an SQL NULL,
5697 ** return the current time. Otherwise, return the value stored in (*pVal)
5698 ** cast to a 32-bit unsigned integer.
5699 */
5700 static u32 zipfileGetTime(sqlite3_value *pVal){
5701   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5702     return zipfileTime();
5703   }
5704   return (u32)sqlite3_value_int64(pVal);
5705 }
5706
5707 /*
5708 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5709 ** linked list.  Remove it from the list and free the object.
5710 */
5711 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5712   if( pOld ){
5713     ZipfileEntry **pp;
5714     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5715     *pp = (*pp)->pNext;
5716     zipfileEntryFree(pOld);
5717   }
5718 }
5719
5720 /*
5721 ** xUpdate method.
5722 */
5723 static int zipfileUpdate(
5724   sqlite3_vtab *pVtab, 
5725   int nVal, 
5726   sqlite3_value **apVal, 
5727   sqlite_int64 *pRowid
5728 ){
5729   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5730   int rc = SQLITE_OK;             /* Return Code */
5731   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
5732
5733   u32 mode = 0;                   /* Mode for new entry */
5734   u32 mTime = 0;                  /* Modification time for new entry */
5735   i64 sz = 0;                     /* Uncompressed size */
5736   const char *zPath = 0;          /* Path for new entry */
5737   int nPath = 0;                  /* strlen(zPath) */
5738   const u8 *pData = 0;            /* Pointer to buffer containing content */
5739   int nData = 0;                  /* Size of pData buffer in bytes */
5740   int iMethod = 0;                /* Compression method for new entry */
5741   u8 *pFree = 0;                  /* Free this */
5742   char *zFree = 0;                /* Also free this */
5743   ZipfileEntry *pOld = 0;
5744   ZipfileEntry *pOld2 = 0;
5745   int bUpdate = 0;                /* True for an update that modifies "name" */
5746   int bIsDir = 0;
5747   u32 iCrc32 = 0;
5748
5749   if( pTab->pWriteFd==0 ){
5750     rc = zipfileBegin(pVtab);
5751     if( rc!=SQLITE_OK ) return rc;
5752   }
5753
5754   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5755   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5756     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5757     int nDelete = (int)strlen(zDelete);
5758     if( nVal>1 ){
5759       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5760       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5761         bUpdate = 1;
5762       }
5763     }
5764     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5765       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5766         break;
5767       }
5768       assert( pOld->pNext );
5769     }
5770   }
5771
5772   if( nVal>1 ){
5773     /* Check that "sz" and "rawdata" are both NULL: */
5774     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5775       zipfileTableErr(pTab, "sz must be NULL");
5776       rc = SQLITE_CONSTRAINT;
5777     }
5778     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5779       zipfileTableErr(pTab, "rawdata must be NULL"); 
5780       rc = SQLITE_CONSTRAINT;
5781     }
5782
5783     if( rc==SQLITE_OK ){
5784       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5785         /* data=NULL. A directory */
5786         bIsDir = 1;
5787       }else{
5788         /* Value specified for "data", and possibly "method". This must be
5789         ** a regular file or a symlink. */
5790         const u8 *aIn = sqlite3_value_blob(apVal[7]);
5791         int nIn = sqlite3_value_bytes(apVal[7]);
5792         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5793
5794         iMethod = sqlite3_value_int(apVal[8]);
5795         sz = nIn;
5796         pData = aIn;
5797         nData = nIn;
5798         if( iMethod!=0 && iMethod!=8 ){
5799           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5800           rc = SQLITE_CONSTRAINT;
5801         }else{
5802           if( bAuto || iMethod ){
5803             int nCmp;
5804             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5805             if( rc==SQLITE_OK ){
5806               if( iMethod || nCmp<nIn ){
5807                 iMethod = 8;
5808                 pData = pFree;
5809                 nData = nCmp;
5810               }
5811             }
5812           }
5813           iCrc32 = crc32(0, aIn, nIn);
5814         }
5815       }
5816     }
5817
5818     if( rc==SQLITE_OK ){
5819       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5820     }
5821
5822     if( rc==SQLITE_OK ){
5823       zPath = (const char*)sqlite3_value_text(apVal[2]);
5824       nPath = (int)strlen(zPath);
5825       mTime = zipfileGetTime(apVal[4]);
5826     }
5827
5828     if( rc==SQLITE_OK && bIsDir ){
5829       /* For a directory, check that the last character in the path is a
5830       ** '/'. This appears to be required for compatibility with info-zip
5831       ** (the unzip command on unix). It does not create directories
5832       ** otherwise.  */
5833       if( zPath[nPath-1]!='/' ){
5834         zFree = sqlite3_mprintf("%s/", zPath);
5835         if( zFree==0 ){ rc = SQLITE_NOMEM; }
5836         zPath = (const char*)zFree;
5837         nPath++;
5838       }
5839     }
5840
5841     /* Check that we're not inserting a duplicate entry -OR- updating an
5842     ** entry with a path, thereby making it into a duplicate. */
5843     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
5844       ZipfileEntry *p;
5845       for(p=pTab->pFirstEntry; p; p=p->pNext){
5846         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5847           switch( sqlite3_vtab_on_conflict(pTab->db) ){
5848             case SQLITE_IGNORE: {
5849               goto zipfile_update_done;
5850             }
5851             case SQLITE_REPLACE: {
5852               pOld2 = p;
5853               break;
5854             }
5855             default: {
5856               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5857               rc = SQLITE_CONSTRAINT;
5858               break;
5859             }
5860           }
5861           break;
5862         }
5863       }
5864     }
5865
5866     if( rc==SQLITE_OK ){
5867       /* Create the new CDS record. */
5868       pNew = zipfileNewEntry(zPath);
5869       if( pNew==0 ){
5870         rc = SQLITE_NOMEM;
5871       }else{
5872         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5873         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5874         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5875         pNew->cds.iCompression = (u16)iMethod;
5876         zipfileMtimeToDos(&pNew->cds, mTime);
5877         pNew->cds.crc32 = iCrc32;
5878         pNew->cds.szCompressed = nData;
5879         pNew->cds.szUncompressed = (u32)sz;
5880         pNew->cds.iExternalAttr = (mode<<16);
5881         pNew->cds.iOffset = (u32)pTab->szCurrent;
5882         pNew->cds.nFile = (u16)nPath;
5883         pNew->mUnixTime = (u32)mTime;
5884         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5885         zipfileAddEntry(pTab, pOld, pNew);
5886       }
5887     }
5888   }
5889
5890   if( rc==SQLITE_OK && (pOld || pOld2) ){
5891     ZipfileCsr *pCsr;
5892     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5893       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
5894         pCsr->pCurrent = pCsr->pCurrent->pNext;
5895         pCsr->bNoop = 1;
5896       }
5897     }
5898
5899     zipfileRemoveEntryFromList(pTab, pOld);
5900     zipfileRemoveEntryFromList(pTab, pOld2);
5901   }
5902
5903 zipfile_update_done:
5904   sqlite3_free(pFree);
5905   sqlite3_free(zFree);
5906   return rc;
5907 }
5908
5909 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5910   u8 *a = aBuf;
5911   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5912   zipfileWrite16(a, p->iDisk);
5913   zipfileWrite16(a, p->iFirstDisk);
5914   zipfileWrite16(a, p->nEntry);
5915   zipfileWrite16(a, p->nEntryTotal);
5916   zipfileWrite32(a, p->nSize);
5917   zipfileWrite32(a, p->iOffset);
5918   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
5919
5920   return a-aBuf;
5921 }
5922
5923 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5924   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5925   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5926   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5927 }
5928
5929 /*
5930 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5931 ** of bytes written.
5932 */
5933 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5934   u8 *a = aBuf;
5935   ZipfileCDS *pCDS = &pEntry->cds;
5936
5937   if( pEntry->aExtra==0 ){
5938     pCDS->nExtra = 9;
5939   }
5940
5941   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5942   zipfileWrite16(a, pCDS->iVersionMadeBy);
5943   zipfileWrite16(a, pCDS->iVersionExtract);
5944   zipfileWrite16(a, pCDS->flags);
5945   zipfileWrite16(a, pCDS->iCompression);
5946   zipfileWrite16(a, pCDS->mTime);
5947   zipfileWrite16(a, pCDS->mDate);
5948   zipfileWrite32(a, pCDS->crc32);
5949   zipfileWrite32(a, pCDS->szCompressed);
5950   zipfileWrite32(a, pCDS->szUncompressed);
5951   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5952   zipfileWrite16(a, pCDS->nFile);
5953   zipfileWrite16(a, pCDS->nExtra);
5954   zipfileWrite16(a, pCDS->nComment);
5955   zipfileWrite16(a, pCDS->iDiskStart);
5956   zipfileWrite16(a, pCDS->iInternalAttr);
5957   zipfileWrite32(a, pCDS->iExternalAttr);
5958   zipfileWrite32(a, pCDS->iOffset);
5959
5960   memcpy(a, pCDS->zFile, pCDS->nFile);
5961   a += pCDS->nFile;
5962
5963   if( pEntry->aExtra ){
5964     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5965     memcpy(a, pEntry->aExtra, n);
5966     a += n;
5967   }else{
5968     assert( pCDS->nExtra==9 );
5969     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5970     zipfileWrite16(a, 5);
5971     *a++ = 0x01;
5972     zipfileWrite32(a, pEntry->mUnixTime);
5973   }
5974
5975   return a-aBuf;
5976 }
5977
5978 static int zipfileCommit(sqlite3_vtab *pVtab){
5979   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5980   int rc = SQLITE_OK;
5981   if( pTab->pWriteFd ){
5982     i64 iOffset = pTab->szCurrent;
5983     ZipfileEntry *p;
5984     ZipfileEOCD eocd;
5985     int nEntry = 0;
5986
5987     /* Write out all entries */
5988     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5989       int n = zipfileSerializeCDS(p, pTab->aBuffer);
5990       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
5991       nEntry++;
5992     }
5993
5994     /* Write out the EOCD record */
5995     eocd.iDisk = 0;
5996     eocd.iFirstDisk = 0;
5997     eocd.nEntry = (u16)nEntry;
5998     eocd.nEntryTotal = (u16)nEntry;
5999     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
6000     eocd.iOffset = (u32)iOffset;
6001     rc = zipfileAppendEOCD(pTab, &eocd);
6002
6003     zipfileCleanupTransaction(pTab);
6004   }
6005   return rc;
6006 }
6007
6008 static int zipfileRollback(sqlite3_vtab *pVtab){
6009   return zipfileCommit(pVtab);
6010 }
6011
6012 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
6013   ZipfileCsr *pCsr;
6014   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6015     if( iId==pCsr->iId ) break;
6016   }
6017   return pCsr;
6018 }
6019
6020 static void zipfileFunctionCds(
6021   sqlite3_context *context,
6022   int argc,
6023   sqlite3_value **argv
6024 ){
6025   ZipfileCsr *pCsr;
6026   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
6027   assert( argc>0 );
6028
6029   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
6030   if( pCsr ){
6031     ZipfileCDS *p = &pCsr->pCurrent->cds;
6032     char *zRes = sqlite3_mprintf("{"
6033         "\"version-made-by\" : %u, "
6034         "\"version-to-extract\" : %u, "
6035         "\"flags\" : %u, "
6036         "\"compression\" : %u, "
6037         "\"time\" : %u, "
6038         "\"date\" : %u, "
6039         "\"crc32\" : %u, "
6040         "\"compressed-size\" : %u, "
6041         "\"uncompressed-size\" : %u, "
6042         "\"file-name-length\" : %u, "
6043         "\"extra-field-length\" : %u, "
6044         "\"file-comment-length\" : %u, "
6045         "\"disk-number-start\" : %u, "
6046         "\"internal-attr\" : %u, "
6047         "\"external-attr\" : %u, "
6048         "\"offset\" : %u }",
6049         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
6050         (u32)p->flags, (u32)p->iCompression,
6051         (u32)p->mTime, (u32)p->mDate,
6052         (u32)p->crc32, (u32)p->szCompressed,
6053         (u32)p->szUncompressed, (u32)p->nFile,
6054         (u32)p->nExtra, (u32)p->nComment,
6055         (u32)p->iDiskStart, (u32)p->iInternalAttr,
6056         (u32)p->iExternalAttr, (u32)p->iOffset
6057     );
6058
6059     if( zRes==0 ){
6060       sqlite3_result_error_nomem(context);
6061     }else{
6062       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
6063       sqlite3_free(zRes);
6064     }
6065   }
6066 }
6067
6068 /*
6069 ** xFindFunction method.
6070 */
6071 static int zipfileFindFunction(
6072   sqlite3_vtab *pVtab,            /* Virtual table handle */
6073   int nArg,                       /* Number of SQL function arguments */
6074   const char *zName,              /* Name of SQL function */
6075   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
6076   void **ppArg                    /* OUT: User data for *pxFunc */
6077 ){
6078   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
6079     *pxFunc = zipfileFunctionCds;
6080     *ppArg = (void*)pVtab;
6081     return 1;
6082   }
6083   return 0;
6084 }
6085
6086 typedef struct ZipfileBuffer ZipfileBuffer;
6087 struct ZipfileBuffer {
6088   u8 *a;                          /* Pointer to buffer */
6089   int n;                          /* Size of buffer in bytes */
6090   int nAlloc;                     /* Byte allocated at a[] */
6091 };
6092
6093 typedef struct ZipfileCtx ZipfileCtx;
6094 struct ZipfileCtx {
6095   int nEntry;
6096   ZipfileBuffer body;
6097   ZipfileBuffer cds;
6098 };
6099
6100 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
6101   if( pBuf->n+nByte>pBuf->nAlloc ){
6102     u8 *aNew;
6103     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
6104     int nReq = pBuf->n + nByte;
6105
6106     while( nNew<nReq ) nNew = nNew*2;
6107     aNew = sqlite3_realloc64(pBuf->a, nNew);
6108     if( aNew==0 ) return SQLITE_NOMEM;
6109     pBuf->a = aNew;
6110     pBuf->nAlloc = (int)nNew;
6111   }
6112   return SQLITE_OK;
6113 }
6114
6115 /*
6116 ** xStep() callback for the zipfile() aggregate. This can be called in
6117 ** any of the following ways:
6118 **
6119 **   SELECT zipfile(name,data) ...
6120 **   SELECT zipfile(name,mode,mtime,data) ...
6121 **   SELECT zipfile(name,mode,mtime,data,method) ...
6122 */
6123 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
6124   ZipfileCtx *p;                  /* Aggregate function context */
6125   ZipfileEntry e;                 /* New entry to add to zip archive */
6126
6127   sqlite3_value *pName = 0;
6128   sqlite3_value *pMode = 0;
6129   sqlite3_value *pMtime = 0;
6130   sqlite3_value *pData = 0;
6131   sqlite3_value *pMethod = 0;
6132
6133   int bIsDir = 0;
6134   u32 mode;
6135   int rc = SQLITE_OK;
6136   char *zErr = 0;
6137
6138   int iMethod = -1;               /* Compression method to use (0 or 8) */
6139
6140   const u8 *aData = 0;            /* Possibly compressed data for new entry */
6141   int nData = 0;                  /* Size of aData[] in bytes */
6142   int szUncompressed = 0;         /* Size of data before compression */
6143   u8 *aFree = 0;                  /* Free this before returning */
6144   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
6145
6146   char *zName = 0;                /* Path (name) of new entry */
6147   int nName = 0;                  /* Size of zName in bytes */
6148   char *zFree = 0;                /* Free this before returning */
6149   int nByte;
6150
6151   memset(&e, 0, sizeof(e));
6152   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6153   if( p==0 ) return;
6154
6155   /* Martial the arguments into stack variables */
6156   if( nVal!=2 && nVal!=4 && nVal!=5 ){
6157     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
6158     rc = SQLITE_ERROR;
6159     goto zipfile_step_out;
6160   }
6161   pName = apVal[0];
6162   if( nVal==2 ){
6163     pData = apVal[1];
6164   }else{
6165     pMode = apVal[1];
6166     pMtime = apVal[2];
6167     pData = apVal[3];
6168     if( nVal==5 ){
6169       pMethod = apVal[4];
6170     }
6171   }
6172
6173   /* Check that the 'name' parameter looks ok. */
6174   zName = (char*)sqlite3_value_text(pName);
6175   nName = sqlite3_value_bytes(pName);
6176   if( zName==0 ){
6177     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
6178     rc = SQLITE_ERROR;
6179     goto zipfile_step_out;
6180   }
6181
6182   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6183   ** deflate compression) or NULL (choose automatically).  */
6184   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6185     iMethod = (int)sqlite3_value_int64(pMethod);
6186     if( iMethod!=0 && iMethod!=8 ){
6187       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6188       rc = SQLITE_ERROR;
6189       goto zipfile_step_out;
6190     }
6191   }
6192
6193   /* Now inspect the data. If this is NULL, then the new entry must be a
6194   ** directory.  Otherwise, figure out whether or not the data should
6195   ** be deflated or simply stored in the zip archive. */
6196   if( sqlite3_value_type(pData)==SQLITE_NULL ){
6197     bIsDir = 1;
6198     iMethod = 0;
6199   }else{
6200     aData = sqlite3_value_blob(pData);
6201     szUncompressed = nData = sqlite3_value_bytes(pData);
6202     iCrc32 = crc32(0, aData, nData);
6203     if( iMethod<0 || iMethod==8 ){
6204       int nOut = 0;
6205       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6206       if( rc!=SQLITE_OK ){
6207         goto zipfile_step_out;
6208       }
6209       if( iMethod==8 || nOut<nData ){
6210         aData = aFree;
6211         nData = nOut;
6212         iMethod = 8;
6213       }else{
6214         iMethod = 0;
6215       }
6216     }
6217   }
6218
6219   /* Decode the "mode" argument. */
6220   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6221   if( rc ) goto zipfile_step_out;
6222
6223   /* Decode the "mtime" argument. */
6224   e.mUnixTime = zipfileGetTime(pMtime);
6225
6226   /* If this is a directory entry, ensure that there is exactly one '/'
6227   ** at the end of the path. Or, if this is not a directory and the path
6228   ** ends in '/' it is an error. */
6229   if( bIsDir==0 ){
6230     if( zName[nName-1]=='/' ){
6231       zErr = sqlite3_mprintf("non-directory name must not end with /");
6232       rc = SQLITE_ERROR;
6233       goto zipfile_step_out;
6234     }
6235   }else{
6236     if( zName[nName-1]!='/' ){
6237       zName = zFree = sqlite3_mprintf("%s/", zName);
6238       nName++;
6239       if( zName==0 ){
6240         rc = SQLITE_NOMEM;
6241         goto zipfile_step_out;
6242       }
6243     }else{
6244       while( nName>1 && zName[nName-2]=='/' ) nName--;
6245     }
6246   }
6247
6248   /* Assemble the ZipfileEntry object for the new zip archive entry */
6249   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6250   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6251   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6252   e.cds.iCompression = (u16)iMethod;
6253   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6254   e.cds.crc32 = iCrc32;
6255   e.cds.szCompressed = nData;
6256   e.cds.szUncompressed = szUncompressed;
6257   e.cds.iExternalAttr = (mode<<16);
6258   e.cds.iOffset = p->body.n;
6259   e.cds.nFile = (u16)nName;
6260   e.cds.zFile = zName;
6261
6262   /* Append the LFH to the body of the new archive */
6263   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6264   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6265   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6266
6267   /* Append the data to the body of the new archive */
6268   if( nData>0 ){
6269     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6270     memcpy(&p->body.a[p->body.n], aData, nData);
6271     p->body.n += nData;
6272   }
6273
6274   /* Append the CDS record to the directory of the new archive */
6275   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6276   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6277   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6278
6279   /* Increment the count of entries in the archive */
6280   p->nEntry++;
6281
6282  zipfile_step_out:
6283   sqlite3_free(aFree);
6284   sqlite3_free(zFree);
6285   if( rc ){
6286     if( zErr ){
6287       sqlite3_result_error(pCtx, zErr, -1);
6288     }else{
6289       sqlite3_result_error_code(pCtx, rc);
6290     }
6291   }
6292   sqlite3_free(zErr);
6293 }
6294
6295 /*
6296 ** xFinalize() callback for zipfile aggregate function.
6297 */
6298 void zipfileFinal(sqlite3_context *pCtx){
6299   ZipfileCtx *p;
6300   ZipfileEOCD eocd;
6301   sqlite3_int64 nZip;
6302   u8 *aZip;
6303
6304   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6305   if( p==0 ) return;
6306   if( p->nEntry>0 ){
6307     memset(&eocd, 0, sizeof(eocd));
6308     eocd.nEntry = (u16)p->nEntry;
6309     eocd.nEntryTotal = (u16)p->nEntry;
6310     eocd.nSize = p->cds.n;
6311     eocd.iOffset = p->body.n;
6312
6313     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6314     aZip = (u8*)sqlite3_malloc64(nZip);
6315     if( aZip==0 ){
6316       sqlite3_result_error_nomem(pCtx);
6317     }else{
6318       memcpy(aZip, p->body.a, p->body.n);
6319       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6320       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6321       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
6322     }
6323   }
6324
6325   sqlite3_free(p->body.a);
6326   sqlite3_free(p->cds.a);
6327 }
6328
6329
6330 /*
6331 ** Register the "zipfile" virtual table.
6332 */
6333 static int zipfileRegister(sqlite3 *db){
6334   static sqlite3_module zipfileModule = {
6335     1,                         /* iVersion */
6336     zipfileConnect,            /* xCreate */
6337     zipfileConnect,            /* xConnect */
6338     zipfileBestIndex,          /* xBestIndex */
6339     zipfileDisconnect,         /* xDisconnect */
6340     zipfileDisconnect,         /* xDestroy */
6341     zipfileOpen,               /* xOpen - open a cursor */
6342     zipfileClose,              /* xClose - close a cursor */
6343     zipfileFilter,             /* xFilter - configure scan constraints */
6344     zipfileNext,               /* xNext - advance a cursor */
6345     zipfileEof,                /* xEof - check for end of scan */
6346     zipfileColumn,             /* xColumn - read data */
6347     0,                         /* xRowid - read data */
6348     zipfileUpdate,             /* xUpdate */
6349     zipfileBegin,              /* xBegin */
6350     0,                         /* xSync */
6351     zipfileCommit,             /* xCommit */
6352     zipfileRollback,           /* xRollback */
6353     zipfileFindFunction,       /* xFindMethod */
6354     0,                         /* xRename */
6355   };
6356
6357   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
6358   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6359   if( rc==SQLITE_OK ){
6360     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
6361         zipfileStep, zipfileFinal
6362     );
6363   }
6364   return rc;
6365 }
6366 #else         /* SQLITE_OMIT_VIRTUALTABLE */
6367 # define zipfileRegister(x) SQLITE_OK
6368 #endif
6369
6370 #ifdef _WIN32
6371
6372 #endif
6373 int sqlite3_zipfile_init(
6374   sqlite3 *db, 
6375   char **pzErrMsg, 
6376   const sqlite3_api_routines *pApi
6377 ){
6378   SQLITE_EXTENSION_INIT2(pApi);
6379   (void)pzErrMsg;  /* Unused parameter */
6380   return zipfileRegister(db);
6381 }
6382
6383 /************************* End ../ext/misc/zipfile.c ********************/
6384 /************************* Begin ../ext/misc/sqlar.c ******************/
6385 /*
6386 ** 2017-12-17
6387 **
6388 ** The author disclaims copyright to this source code.  In place of
6389 ** a legal notice, here is a blessing:
6390 **
6391 **    May you do good and not evil.
6392 **    May you find forgiveness for yourself and forgive others.
6393 **    May you share freely, never taking more than you give.
6394 **
6395 ******************************************************************************
6396 **
6397 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6398 ** for working with sqlar archives and used by the shell tool's built-in
6399 ** sqlar support.
6400 */
6401 SQLITE_EXTENSION_INIT1
6402 #include <zlib.h>
6403
6404 /*
6405 ** Implementation of the "sqlar_compress(X)" SQL function.
6406 **
6407 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6408 ** zlib utility function compress() yields a smaller blob, return the
6409 ** compressed blob. Otherwise, return a copy of X.
6410 **
6411 ** SQLar uses the "zlib format" for compressed content.  The zlib format
6412 ** contains a two-byte identification header and a four-byte checksum at
6413 ** the end.  This is different from ZIP which uses the raw deflate format.
6414 **
6415 ** Future enhancements to SQLar might add support for new compression formats.
6416 ** If so, those new formats will be identified by alternative headers in the
6417 ** compressed data.
6418 */
6419 static void sqlarCompressFunc(
6420   sqlite3_context *context,
6421   int argc,
6422   sqlite3_value **argv
6423 ){
6424   assert( argc==1 );
6425   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6426     const Bytef *pData = sqlite3_value_blob(argv[0]);
6427     uLong nData = sqlite3_value_bytes(argv[0]);
6428     uLongf nOut = compressBound(nData);
6429     Bytef *pOut;
6430
6431     pOut = (Bytef*)sqlite3_malloc(nOut);
6432     if( pOut==0 ){
6433       sqlite3_result_error_nomem(context);
6434       return;
6435     }else{
6436       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6437         sqlite3_result_error(context, "error in compress()", -1);
6438       }else if( nOut<nData ){
6439         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6440       }else{
6441         sqlite3_result_value(context, argv[0]);
6442       }
6443       sqlite3_free(pOut);
6444     }
6445   }else{
6446     sqlite3_result_value(context, argv[0]);
6447   }
6448 }
6449
6450 /*
6451 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6452 **
6453 ** Parameter SZ is interpreted as an integer. If it is less than or
6454 ** equal to zero, then this function returns a copy of X. Or, if
6455 ** SZ is equal to the size of X when interpreted as a blob, also
6456 ** return a copy of X. Otherwise, decompress blob X using zlib
6457 ** utility function uncompress() and return the results (another
6458 ** blob).
6459 */
6460 static void sqlarUncompressFunc(
6461   sqlite3_context *context,
6462   int argc,
6463   sqlite3_value **argv
6464 ){
6465   uLong nData;
6466   uLongf sz;
6467
6468   assert( argc==2 );
6469   sz = sqlite3_value_int(argv[1]);
6470
6471   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6472     sqlite3_result_value(context, argv[0]);
6473   }else{
6474     const Bytef *pData= sqlite3_value_blob(argv[0]);
6475     Bytef *pOut = sqlite3_malloc(sz);
6476     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6477       sqlite3_result_error(context, "error in uncompress()", -1);
6478     }else{
6479       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6480     }
6481     sqlite3_free(pOut);
6482   }
6483 }
6484
6485
6486 #ifdef _WIN32
6487
6488 #endif
6489 int sqlite3_sqlar_init(
6490   sqlite3 *db, 
6491   char **pzErrMsg, 
6492   const sqlite3_api_routines *pApi
6493 ){
6494   int rc = SQLITE_OK;
6495   SQLITE_EXTENSION_INIT2(pApi);
6496   (void)pzErrMsg;  /* Unused parameter */
6497   rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
6498                                sqlarCompressFunc, 0, 0);
6499   if( rc==SQLITE_OK ){
6500     rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
6501                                  sqlarUncompressFunc, 0, 0);
6502   }
6503   return rc;
6504 }
6505
6506 /************************* End ../ext/misc/sqlar.c ********************/
6507 #endif
6508 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6509 /*
6510 ** 2017 April 07
6511 **
6512 ** The author disclaims copyright to this source code.  In place of
6513 ** a legal notice, here is a blessing:
6514 **
6515 **    May you do good and not evil.
6516 **    May you find forgiveness for yourself and forgive others.
6517 **    May you share freely, never taking more than you give.
6518 **
6519 *************************************************************************
6520 */
6521
6522
6523
6524 typedef struct sqlite3expert sqlite3expert;
6525
6526 /*
6527 ** Create a new sqlite3expert object.
6528 **
6529 ** If successful, a pointer to the new object is returned and (*pzErr) set
6530 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6531 ** an English-language error message. In this case it is the responsibility
6532 ** of the caller to eventually free the error message buffer using
6533 ** sqlite3_free().
6534 */
6535 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6536
6537 /*
6538 ** Configure an sqlite3expert object.
6539 **
6540 ** EXPERT_CONFIG_SAMPLE:
6541 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6542 **   each candidate index. This involves scanning and sorting the entire
6543 **   contents of each user database table once for each candidate index
6544 **   associated with the table. For large databases, this can be 
6545 **   prohibitively slow. This option allows the sqlite3expert object to
6546 **   be configured so that sqlite_stat1 data is instead generated based on a
6547 **   subset of each table, or so that no sqlite_stat1 data is used at all.
6548 **
6549 **   A single integer argument is passed to this option. If the value is less
6550 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
6551 **   the analysis - indexes are recommended based on the database schema only.
6552 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
6553 **   generated for each candidate index (this is the default). Finally, if the
6554 **   value falls between 0 and 100, then it represents the percentage of user
6555 **   table rows that should be considered when generating sqlite_stat1 data.
6556 **
6557 **   Examples:
6558 **
6559 **     // Do not generate any sqlite_stat1 data
6560 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6561 **
6562 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
6563 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6564 */
6565 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6566
6567 #define EXPERT_CONFIG_SAMPLE 1    /* int */
6568
6569 /*
6570 ** Specify zero or more SQL statements to be included in the analysis.
6571 **
6572 ** Buffer zSql must contain zero or more complete SQL statements. This
6573 ** function parses all statements contained in the buffer and adds them
6574 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6575 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6576 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6577 ** may be set to point to an English language error message. In this case
6578 ** the caller is responsible for eventually freeing the error message buffer
6579 ** using sqlite3_free().
6580 **
6581 ** If an error does occur while processing one of the statements in the
6582 ** buffer passed as the second argument, none of the statements in the
6583 ** buffer are added to the analysis.
6584 **
6585 ** This function must be called before sqlite3_expert_analyze(). If a call
6586 ** to this function is made on an sqlite3expert object that has already
6587 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6588 ** immediately and no statements are added to the analysis.
6589 */
6590 int sqlite3_expert_sql(
6591   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
6592   const char *zSql,               /* SQL statement(s) to add */
6593   char **pzErr                    /* OUT: Error message (if any) */
6594 );
6595
6596
6597 /*
6598 ** This function is called after the sqlite3expert object has been configured
6599 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6600 ** the analysis. Once this function has been called, it is not possible to
6601 ** add further SQL statements to the analysis.
6602 **
6603 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6604 ** an error occurs, an SQLite error code is returned and (*pzErr) set to 
6605 ** point to a buffer containing an English language error message. In this
6606 ** case it is the responsibility of the caller to eventually free the buffer
6607 ** using sqlite3_free().
6608 **
6609 ** If an error does occur within this function, the sqlite3expert object
6610 ** is no longer useful for any purpose. At that point it is no longer
6611 ** possible to add further SQL statements to the object or to re-attempt
6612 ** the analysis. The sqlite3expert object must still be freed using a call
6613 ** sqlite3_expert_destroy().
6614 */
6615 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6616
6617 /*
6618 ** Return the total number of statements loaded using sqlite3_expert_sql().
6619 ** The total number of SQL statements may be different from the total number
6620 ** to calls to sqlite3_expert_sql().
6621 */
6622 int sqlite3_expert_count(sqlite3expert*);
6623
6624 /*
6625 ** Return a component of the report.
6626 **
6627 ** This function is called after sqlite3_expert_analyze() to extract the
6628 ** results of the analysis. Each call to this function returns either a
6629 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6630 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6631 ** #define constants defined below.
6632 **
6633 ** For some EXPERT_REPORT_* parameters, the buffer returned contains 
6634 ** information relating to a specific SQL statement. In these cases that
6635 ** SQL statement is identified by the value passed as the second argument.
6636 ** SQL statements are numbered from 0 in the order in which they are parsed.
6637 ** If an out-of-range value (less than zero or equal to or greater than the
6638 ** value returned by sqlite3_expert_count()) is passed as the second argument
6639 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6640 **
6641 ** EXPERT_REPORT_SQL:
6642 **   Return the text of SQL statement iStmt.
6643 **
6644 ** EXPERT_REPORT_INDEXES:
6645 **   Return a buffer containing the CREATE INDEX statements for all recommended
6646 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL 
6647 **   is returned.
6648 **
6649 ** EXPERT_REPORT_PLAN:
6650 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6651 **   iStmt after the proposed indexes have been added to the database schema.
6652 **
6653 ** EXPERT_REPORT_CANDIDATES:
6654 **   Return a pointer to a buffer containing the CREATE INDEX statements 
6655 **   for all indexes that were tested (for all SQL statements). The iStmt
6656 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6657 */
6658 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6659
6660 /*
6661 ** Values for the third argument passed to sqlite3_expert_report().
6662 */
6663 #define EXPERT_REPORT_SQL        1
6664 #define EXPERT_REPORT_INDEXES    2
6665 #define EXPERT_REPORT_PLAN       3
6666 #define EXPERT_REPORT_CANDIDATES 4
6667
6668 /*
6669 ** Free an (sqlite3expert*) handle and all associated resources. There 
6670 ** should be one call to this function for each successful call to 
6671 ** sqlite3-expert_new().
6672 */
6673 void sqlite3_expert_destroy(sqlite3expert*);
6674
6675
6676
6677 /************************* End ../ext/expert/sqlite3expert.h ********************/
6678 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6679 /*
6680 ** 2017 April 09
6681 **
6682 ** The author disclaims copyright to this source code.  In place of
6683 ** a legal notice, here is a blessing:
6684 **
6685 **    May you do good and not evil.
6686 **    May you find forgiveness for yourself and forgive others.
6687 **    May you share freely, never taking more than you give.
6688 **
6689 *************************************************************************
6690 */
6691 #include <assert.h>
6692 #include <string.h>
6693 #include <stdio.h>
6694
6695 #ifndef SQLITE_OMIT_VIRTUALTABLE 
6696
6697 /* typedef sqlite3_int64 i64; */
6698 /* typedef sqlite3_uint64 u64; */
6699
6700 typedef struct IdxColumn IdxColumn;
6701 typedef struct IdxConstraint IdxConstraint;
6702 typedef struct IdxScan IdxScan;
6703 typedef struct IdxStatement IdxStatement;
6704 typedef struct IdxTable IdxTable;
6705 typedef struct IdxWrite IdxWrite;
6706
6707 #define STRLEN  (int)strlen
6708
6709 /*
6710 ** A temp table name that we assume no user database will actually use.
6711 ** If this assumption proves incorrect triggers on the table with the
6712 ** conflicting name will be ignored.
6713 */
6714 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6715
6716 /*
6717 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6718 ** any other type of single-ended range constraint on a column).
6719 **
6720 ** pLink:
6721 **   Used to temporarily link IdxConstraint objects into lists while
6722 **   creating candidate indexes.
6723 */
6724 struct IdxConstraint {
6725   char *zColl;                    /* Collation sequence */
6726   int bRange;                     /* True for range, false for eq */
6727   int iCol;                       /* Constrained table column */
6728   int bFlag;                      /* Used by idxFindCompatible() */
6729   int bDesc;                      /* True if ORDER BY <expr> DESC */
6730   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
6731   IdxConstraint *pLink;           /* See above */
6732 };
6733
6734 /*
6735 ** A single scan of a single table.
6736 */
6737 struct IdxScan {
6738   IdxTable *pTab;                 /* Associated table object */
6739   int iDb;                        /* Database containing table zTable */
6740   i64 covering;                   /* Mask of columns required for cov. index */
6741   IdxConstraint *pOrder;          /* ORDER BY columns */
6742   IdxConstraint *pEq;             /* List of == constraints */
6743   IdxConstraint *pRange;          /* List of < constraints */
6744   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
6745 };
6746
6747 /*
6748 ** Information regarding a single database table. Extracted from 
6749 ** "PRAGMA table_info" by function idxGetTableInfo().
6750 */
6751 struct IdxColumn {
6752   char *zName;
6753   char *zColl;
6754   int iPk;
6755 };
6756 struct IdxTable {
6757   int nCol;
6758   char *zName;                    /* Table name */
6759   IdxColumn *aCol;
6760   IdxTable *pNext;                /* Next table in linked list of all tables */
6761 };
6762
6763 /*
6764 ** An object of the following type is created for each unique table/write-op
6765 ** seen. The objects are stored in a singly-linked list beginning at
6766 ** sqlite3expert.pWrite.
6767 */
6768 struct IdxWrite {
6769   IdxTable *pTab;
6770   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
6771   IdxWrite *pNext;
6772 };
6773
6774 /*
6775 ** Each statement being analyzed is represented by an instance of this
6776 ** structure.
6777 */
6778 struct IdxStatement {
6779   int iId;                        /* Statement number */
6780   char *zSql;                     /* SQL statement */
6781   char *zIdx;                     /* Indexes */
6782   char *zEQP;                     /* Plan */
6783   IdxStatement *pNext;
6784 };
6785
6786
6787 /*
6788 ** A hash table for storing strings. With space for a payload string
6789 ** with each entry. Methods are:
6790 **
6791 **   idxHashInit()
6792 **   idxHashClear()
6793 **   idxHashAdd()
6794 **   idxHashSearch()
6795 */
6796 #define IDX_HASH_SIZE 1023
6797 typedef struct IdxHashEntry IdxHashEntry;
6798 typedef struct IdxHash IdxHash;
6799 struct IdxHashEntry {
6800   char *zKey;                     /* nul-terminated key */
6801   char *zVal;                     /* nul-terminated value string */
6802   char *zVal2;                    /* nul-terminated value string 2 */
6803   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
6804   IdxHashEntry *pNext;            /* Next entry in hash */
6805 };
6806 struct IdxHash {
6807   IdxHashEntry *pFirst;
6808   IdxHashEntry *aHash[IDX_HASH_SIZE];
6809 };
6810
6811 /*
6812 ** sqlite3expert object.
6813 */
6814 struct sqlite3expert {
6815   int iSample;                    /* Percentage of tables to sample for stat1 */
6816   sqlite3 *db;                    /* User database */
6817   sqlite3 *dbm;                   /* In-memory db for this analysis */
6818   sqlite3 *dbv;                   /* Vtab schema for this analysis */
6819   IdxTable *pTable;               /* List of all IdxTable objects */
6820   IdxScan *pScan;                 /* List of scan objects */
6821   IdxWrite *pWrite;               /* List of write objects */
6822   IdxStatement *pStatement;       /* List of IdxStatement objects */
6823   int bRun;                       /* True once analysis has run */
6824   char **pzErrmsg;
6825   int rc;                         /* Error code from whereinfo hook */
6826   IdxHash hIdx;                   /* Hash containing all candidate indexes */
6827   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
6828 };
6829
6830
6831 /*
6832 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). 
6833 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6834 */
6835 static void *idxMalloc(int *pRc, int nByte){
6836   void *pRet;
6837   assert( *pRc==SQLITE_OK );
6838   assert( nByte>0 );
6839   pRet = sqlite3_malloc(nByte);
6840   if( pRet ){
6841     memset(pRet, 0, nByte);
6842   }else{
6843     *pRc = SQLITE_NOMEM;
6844   }
6845   return pRet;
6846 }
6847
6848 /*
6849 ** Initialize an IdxHash hash table.
6850 */
6851 static void idxHashInit(IdxHash *pHash){
6852   memset(pHash, 0, sizeof(IdxHash));
6853 }
6854
6855 /*
6856 ** Reset an IdxHash hash table.
6857 */
6858 static void idxHashClear(IdxHash *pHash){
6859   int i;
6860   for(i=0; i<IDX_HASH_SIZE; i++){
6861     IdxHashEntry *pEntry;
6862     IdxHashEntry *pNext;
6863     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6864       pNext = pEntry->pHashNext;
6865       sqlite3_free(pEntry->zVal2);
6866       sqlite3_free(pEntry);
6867     }
6868   }
6869   memset(pHash, 0, sizeof(IdxHash));
6870 }
6871
6872 /*
6873 ** Return the index of the hash bucket that the string specified by the
6874 ** arguments to this function belongs.
6875 */
6876 static int idxHashString(const char *z, int n){
6877   unsigned int ret = 0;
6878   int i;
6879   for(i=0; i<n; i++){
6880     ret += (ret<<3) + (unsigned char)(z[i]);
6881   }
6882   return (int)(ret % IDX_HASH_SIZE);
6883 }
6884
6885 /*
6886 ** If zKey is already present in the hash table, return non-zero and do
6887 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6888 ** the hash table passed as the second argument. 
6889 */
6890 static int idxHashAdd(
6891   int *pRc, 
6892   IdxHash *pHash, 
6893   const char *zKey,
6894   const char *zVal
6895 ){
6896   int nKey = STRLEN(zKey);
6897   int iHash = idxHashString(zKey, nKey);
6898   int nVal = (zVal ? STRLEN(zVal) : 0);
6899   IdxHashEntry *pEntry;
6900   assert( iHash>=0 );
6901   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6902     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6903       return 1;
6904     }
6905   }
6906   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6907   if( pEntry ){
6908     pEntry->zKey = (char*)&pEntry[1];
6909     memcpy(pEntry->zKey, zKey, nKey);
6910     if( zVal ){
6911       pEntry->zVal = &pEntry->zKey[nKey+1];
6912       memcpy(pEntry->zVal, zVal, nVal);
6913     }
6914     pEntry->pHashNext = pHash->aHash[iHash];
6915     pHash->aHash[iHash] = pEntry;
6916
6917     pEntry->pNext = pHash->pFirst;
6918     pHash->pFirst = pEntry;
6919   }
6920   return 0;
6921 }
6922
6923 /*
6924 ** If zKey/nKey is present in the hash table, return a pointer to the 
6925 ** hash-entry object.
6926 */
6927 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6928   int iHash;
6929   IdxHashEntry *pEntry;
6930   if( nKey<0 ) nKey = STRLEN(zKey);
6931   iHash = idxHashString(zKey, nKey);
6932   assert( iHash>=0 );
6933   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6934     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6935       return pEntry;
6936     }
6937   }
6938   return 0;
6939 }
6940
6941 /*
6942 ** If the hash table contains an entry with a key equal to the string
6943 ** passed as the final two arguments to this function, return a pointer
6944 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6945 ** hash table, return NULL.
6946 */
6947 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6948   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6949   if( pEntry ) return pEntry->zVal;
6950   return 0;
6951 }
6952
6953 /*
6954 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6955 ** variable to point to a copy of nul-terminated string zColl.
6956 */
6957 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6958   IdxConstraint *pNew;
6959   int nColl = STRLEN(zColl);
6960
6961   assert( *pRc==SQLITE_OK );
6962   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6963   if( pNew ){
6964     pNew->zColl = (char*)&pNew[1];
6965     memcpy(pNew->zColl, zColl, nColl+1);
6966   }
6967   return pNew;
6968 }
6969
6970 /*
6971 ** An error associated with database handle db has just occurred. Pass
6972 ** the error message to callback function xOut.
6973 */
6974 static void idxDatabaseError(
6975   sqlite3 *db,                    /* Database handle */
6976   char **pzErrmsg                 /* Write error here */
6977 ){
6978   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6979 }
6980
6981 /*
6982 ** Prepare an SQL statement.
6983 */
6984 static int idxPrepareStmt(
6985   sqlite3 *db,                    /* Database handle to compile against */
6986   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
6987   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
6988   const char *zSql                /* SQL statement to compile */
6989 ){
6990   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6991   if( rc!=SQLITE_OK ){
6992     *ppStmt = 0;
6993     idxDatabaseError(db, pzErrmsg);
6994   }
6995   return rc;
6996 }
6997
6998 /*
6999 ** Prepare an SQL statement using the results of a printf() formatting.
7000 */
7001 static int idxPrintfPrepareStmt(
7002   sqlite3 *db,                    /* Database handle to compile against */
7003   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
7004   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
7005   const char *zFmt,               /* printf() format of SQL statement */
7006   ...                             /* Trailing printf() arguments */
7007 ){
7008   va_list ap;
7009   int rc;
7010   char *zSql;
7011   va_start(ap, zFmt);
7012   zSql = sqlite3_vmprintf(zFmt, ap);
7013   if( zSql==0 ){
7014     rc = SQLITE_NOMEM;
7015   }else{
7016     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
7017     sqlite3_free(zSql);
7018   }
7019   va_end(ap);
7020   return rc;
7021 }
7022
7023
7024 /*************************************************************************
7025 ** Beginning of virtual table implementation.
7026 */
7027 typedef struct ExpertVtab ExpertVtab;
7028 struct ExpertVtab {
7029   sqlite3_vtab base;
7030   IdxTable *pTab;
7031   sqlite3expert *pExpert;
7032 };
7033
7034 typedef struct ExpertCsr ExpertCsr;
7035 struct ExpertCsr {
7036   sqlite3_vtab_cursor base;
7037   sqlite3_stmt *pData;
7038 };
7039
7040 static char *expertDequote(const char *zIn){
7041   int n = STRLEN(zIn);
7042   char *zRet = sqlite3_malloc(n);
7043
7044   assert( zIn[0]=='\'' );
7045   assert( zIn[n-1]=='\'' );
7046
7047   if( zRet ){
7048     int iOut = 0;
7049     int iIn = 0;
7050     for(iIn=1; iIn<(n-1); iIn++){
7051       if( zIn[iIn]=='\'' ){
7052         assert( zIn[iIn+1]=='\'' );
7053         iIn++;
7054       }
7055       zRet[iOut++] = zIn[iIn];
7056     }
7057     zRet[iOut] = '\0';
7058   }
7059
7060   return zRet;
7061 }
7062
7063 /* 
7064 ** This function is the implementation of both the xConnect and xCreate
7065 ** methods of the r-tree virtual table.
7066 **
7067 **   argv[0]   -> module name
7068 **   argv[1]   -> database name
7069 **   argv[2]   -> table name
7070 **   argv[...] -> column names...
7071 */
7072 static int expertConnect(
7073   sqlite3 *db,
7074   void *pAux,
7075   int argc, const char *const*argv,
7076   sqlite3_vtab **ppVtab,
7077   char **pzErr
7078 ){
7079   sqlite3expert *pExpert = (sqlite3expert*)pAux;
7080   ExpertVtab *p = 0;
7081   int rc;
7082
7083   if( argc!=4 ){
7084     *pzErr = sqlite3_mprintf("internal error!");
7085     rc = SQLITE_ERROR;
7086   }else{
7087     char *zCreateTable = expertDequote(argv[3]);
7088     if( zCreateTable ){
7089       rc = sqlite3_declare_vtab(db, zCreateTable);
7090       if( rc==SQLITE_OK ){
7091         p = idxMalloc(&rc, sizeof(ExpertVtab));
7092       }
7093       if( rc==SQLITE_OK ){
7094         p->pExpert = pExpert;
7095         p->pTab = pExpert->pTable;
7096         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
7097       }
7098       sqlite3_free(zCreateTable);
7099     }else{
7100       rc = SQLITE_NOMEM;
7101     }
7102   }
7103
7104   *ppVtab = (sqlite3_vtab*)p;
7105   return rc;
7106 }
7107
7108 static int expertDisconnect(sqlite3_vtab *pVtab){
7109   ExpertVtab *p = (ExpertVtab*)pVtab;
7110   sqlite3_free(p);
7111   return SQLITE_OK;
7112 }
7113
7114 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
7115   ExpertVtab *p = (ExpertVtab*)pVtab;
7116   int rc = SQLITE_OK;
7117   int n = 0;
7118   IdxScan *pScan;
7119   const int opmask = 
7120     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
7121     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
7122     SQLITE_INDEX_CONSTRAINT_LE;
7123
7124   pScan = idxMalloc(&rc, sizeof(IdxScan));
7125   if( pScan ){
7126     int i;
7127
7128     /* Link the new scan object into the list */
7129     pScan->pTab = p->pTab;
7130     pScan->pNextScan = p->pExpert->pScan;
7131     p->pExpert->pScan = pScan;
7132
7133     /* Add the constraints to the IdxScan object */
7134     for(i=0; i<pIdxInfo->nConstraint; i++){
7135       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7136       if( pCons->usable 
7137        && pCons->iColumn>=0 
7138        && p->pTab->aCol[pCons->iColumn].iPk==0
7139        && (pCons->op & opmask) 
7140       ){
7141         IdxConstraint *pNew;
7142         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
7143         pNew = idxNewConstraint(&rc, zColl);
7144         if( pNew ){
7145           pNew->iCol = pCons->iColumn;
7146           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7147             pNew->pNext = pScan->pEq;
7148             pScan->pEq = pNew;
7149           }else{
7150             pNew->bRange = 1;
7151             pNew->pNext = pScan->pRange;
7152             pScan->pRange = pNew;
7153           }
7154         }
7155         n++;
7156         pIdxInfo->aConstraintUsage[i].argvIndex = n;
7157       }
7158     }
7159
7160     /* Add the ORDER BY to the IdxScan object */
7161     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
7162       int iCol = pIdxInfo->aOrderBy[i].iColumn;
7163       if( iCol>=0 ){
7164         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
7165         if( pNew ){
7166           pNew->iCol = iCol;
7167           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
7168           pNew->pNext = pScan->pOrder;
7169           pNew->pLink = pScan->pOrder;
7170           pScan->pOrder = pNew;
7171           n++;
7172         }
7173       }
7174     }
7175   }
7176
7177   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7178   return rc;
7179 }
7180
7181 static int expertUpdate(
7182   sqlite3_vtab *pVtab, 
7183   int nData, 
7184   sqlite3_value **azData, 
7185   sqlite_int64 *pRowid
7186 ){
7187   (void)pVtab;
7188   (void)nData;
7189   (void)azData;
7190   (void)pRowid;
7191   return SQLITE_OK;
7192 }
7193
7194 /* 
7195 ** Virtual table module xOpen method.
7196 */
7197 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7198   int rc = SQLITE_OK;
7199   ExpertCsr *pCsr;
7200   (void)pVTab;
7201   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7202   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7203   return rc;
7204 }
7205
7206 /* 
7207 ** Virtual table module xClose method.
7208 */
7209 static int expertClose(sqlite3_vtab_cursor *cur){
7210   ExpertCsr *pCsr = (ExpertCsr*)cur;
7211   sqlite3_finalize(pCsr->pData);
7212   sqlite3_free(pCsr);
7213   return SQLITE_OK;
7214 }
7215
7216 /*
7217 ** Virtual table module xEof method.
7218 **
7219 ** Return non-zero if the cursor does not currently point to a valid 
7220 ** record (i.e if the scan has finished), or zero otherwise.
7221 */
7222 static int expertEof(sqlite3_vtab_cursor *cur){
7223   ExpertCsr *pCsr = (ExpertCsr*)cur;
7224   return pCsr->pData==0;
7225 }
7226
7227 /* 
7228 ** Virtual table module xNext method.
7229 */
7230 static int expertNext(sqlite3_vtab_cursor *cur){
7231   ExpertCsr *pCsr = (ExpertCsr*)cur;
7232   int rc = SQLITE_OK;
7233
7234   assert( pCsr->pData );
7235   rc = sqlite3_step(pCsr->pData);
7236   if( rc!=SQLITE_ROW ){
7237     rc = sqlite3_finalize(pCsr->pData);
7238     pCsr->pData = 0;
7239   }else{
7240     rc = SQLITE_OK;
7241   }
7242
7243   return rc;
7244 }
7245
7246 /* 
7247 ** Virtual table module xRowid method.
7248 */
7249 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7250   (void)cur;
7251   *pRowid = 0;
7252   return SQLITE_OK;
7253 }
7254
7255 /* 
7256 ** Virtual table module xColumn method.
7257 */
7258 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7259   ExpertCsr *pCsr = (ExpertCsr*)cur;
7260   sqlite3_value *pVal;
7261   pVal = sqlite3_column_value(pCsr->pData, i);
7262   if( pVal ){
7263     sqlite3_result_value(ctx, pVal);
7264   }
7265   return SQLITE_OK;
7266 }
7267
7268 /* 
7269 ** Virtual table module xFilter method.
7270 */
7271 static int expertFilter(
7272   sqlite3_vtab_cursor *cur, 
7273   int idxNum, const char *idxStr,
7274   int argc, sqlite3_value **argv
7275 ){
7276   ExpertCsr *pCsr = (ExpertCsr*)cur;
7277   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7278   sqlite3expert *pExpert = pVtab->pExpert;
7279   int rc;
7280
7281   (void)idxNum;
7282   (void)idxStr;
7283   (void)argc;
7284   (void)argv;
7285   rc = sqlite3_finalize(pCsr->pData);
7286   pCsr->pData = 0;
7287   if( rc==SQLITE_OK ){
7288     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7289         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7290     );
7291   }
7292
7293   if( rc==SQLITE_OK ){
7294     rc = expertNext(cur);
7295   }
7296   return rc;
7297 }
7298
7299 static int idxRegisterVtab(sqlite3expert *p){
7300   static sqlite3_module expertModule = {
7301     2,                            /* iVersion */
7302     expertConnect,                /* xCreate - create a table */
7303     expertConnect,                /* xConnect - connect to an existing table */
7304     expertBestIndex,              /* xBestIndex - Determine search strategy */
7305     expertDisconnect,             /* xDisconnect - Disconnect from a table */
7306     expertDisconnect,             /* xDestroy - Drop a table */
7307     expertOpen,                   /* xOpen - open a cursor */
7308     expertClose,                  /* xClose - close a cursor */
7309     expertFilter,                 /* xFilter - configure scan constraints */
7310     expertNext,                   /* xNext - advance a cursor */
7311     expertEof,                    /* xEof */
7312     expertColumn,                 /* xColumn - read data */
7313     expertRowid,                  /* xRowid - read data */
7314     expertUpdate,                 /* xUpdate - write data */
7315     0,                            /* xBegin - begin transaction */
7316     0,                            /* xSync - sync transaction */
7317     0,                            /* xCommit - commit transaction */
7318     0,                            /* xRollback - rollback transaction */
7319     0,                            /* xFindFunction - function overloading */
7320     0,                            /* xRename - rename the table */
7321     0,                            /* xSavepoint */
7322     0,                            /* xRelease */
7323     0,                            /* xRollbackTo */
7324     0,                            /* xShadowName */
7325   };
7326
7327   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7328 }
7329 /*
7330 ** End of virtual table implementation.
7331 *************************************************************************/
7332 /*
7333 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7334 ** is called, set it to the return value of sqlite3_finalize() before
7335 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7336 */
7337 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7338   int rc = sqlite3_finalize(pStmt);
7339   if( *pRc==SQLITE_OK ) *pRc = rc;
7340 }
7341
7342 /*
7343 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7344 ** in the main database of connection db. If successful, set (*ppOut) to
7345 ** point to the new object and return SQLITE_OK. Otherwise, return an
7346 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7347 ** set to point to an error string.
7348 **
7349 ** It is the responsibility of the caller to eventually free either the
7350 ** IdxTable object or error message using sqlite3_free().
7351 */
7352 static int idxGetTableInfo(
7353   sqlite3 *db,                    /* Database connection to read details from */
7354   const char *zTab,               /* Table name */
7355   IdxTable **ppOut,               /* OUT: New object (if successful) */
7356   char **pzErrmsg                 /* OUT: Error message (if not) */
7357 ){
7358   sqlite3_stmt *p1 = 0;
7359   int nCol = 0;
7360   int nTab = STRLEN(zTab);
7361   int nByte = sizeof(IdxTable) + nTab + 1;
7362   IdxTable *pNew = 0;
7363   int rc, rc2;
7364   char *pCsr = 0;
7365
7366   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7367   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7368     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7369     nByte += 1 + STRLEN(zCol);
7370     rc = sqlite3_table_column_metadata(
7371         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7372     );
7373     nByte += 1 + STRLEN(zCol);
7374     nCol++;
7375   }
7376   rc2 = sqlite3_reset(p1);
7377   if( rc==SQLITE_OK ) rc = rc2;
7378
7379   nByte += sizeof(IdxColumn) * nCol;
7380   if( rc==SQLITE_OK ){
7381     pNew = idxMalloc(&rc, nByte);
7382   }
7383   if( rc==SQLITE_OK ){
7384     pNew->aCol = (IdxColumn*)&pNew[1];
7385     pNew->nCol = nCol;
7386     pCsr = (char*)&pNew->aCol[nCol];
7387   }
7388
7389   nCol = 0;
7390   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7391     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7392     int nCopy = STRLEN(zCol) + 1;
7393     pNew->aCol[nCol].zName = pCsr;
7394     pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7395     memcpy(pCsr, zCol, nCopy);
7396     pCsr += nCopy;
7397
7398     rc = sqlite3_table_column_metadata(
7399         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7400     );
7401     if( rc==SQLITE_OK ){
7402       nCopy = STRLEN(zCol) + 1;
7403       pNew->aCol[nCol].zColl = pCsr;
7404       memcpy(pCsr, zCol, nCopy);
7405       pCsr += nCopy;
7406     }
7407
7408     nCol++;
7409   }
7410   idxFinalize(&rc, p1);
7411
7412   if( rc!=SQLITE_OK ){
7413     sqlite3_free(pNew);
7414     pNew = 0;
7415   }else{
7416     pNew->zName = pCsr;
7417     memcpy(pNew->zName, zTab, nTab+1);
7418   }
7419
7420   *ppOut = pNew;
7421   return rc;
7422 }
7423
7424 /*
7425 ** This function is a no-op if *pRc is set to anything other than 
7426 ** SQLITE_OK when it is called.
7427 **
7428 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7429 ** the printf() style arguments is appended to zIn and the result returned
7430 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7431 ** zIn before returning.
7432 */
7433 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7434   va_list ap;
7435   char *zAppend = 0;
7436   char *zRet = 0;
7437   int nIn = zIn ? STRLEN(zIn) : 0;
7438   int nAppend = 0;
7439   va_start(ap, zFmt);
7440   if( *pRc==SQLITE_OK ){
7441     zAppend = sqlite3_vmprintf(zFmt, ap);
7442     if( zAppend ){
7443       nAppend = STRLEN(zAppend);
7444       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7445     }
7446     if( zAppend && zRet ){
7447       if( nIn ) memcpy(zRet, zIn, nIn);
7448       memcpy(&zRet[nIn], zAppend, nAppend+1);
7449     }else{
7450       sqlite3_free(zRet);
7451       zRet = 0;
7452       *pRc = SQLITE_NOMEM;
7453     }
7454     sqlite3_free(zAppend);
7455     sqlite3_free(zIn);
7456   }
7457   va_end(ap);
7458   return zRet;
7459 }
7460
7461 /*
7462 ** Return true if zId must be quoted in order to use it as an SQL
7463 ** identifier, or false otherwise.
7464 */
7465 static int idxIdentifierRequiresQuotes(const char *zId){
7466   int i;
7467   for(i=0; zId[i]; i++){
7468     if( !(zId[i]=='_')
7469      && !(zId[i]>='0' && zId[i]<='9')
7470      && !(zId[i]>='a' && zId[i]<='z')
7471      && !(zId[i]>='A' && zId[i]<='Z')
7472     ){
7473       return 1;
7474     }
7475   }
7476   return 0;
7477 }
7478
7479 /*
7480 ** This function appends an index column definition suitable for constraint
7481 ** pCons to the string passed as zIn and returns the result.
7482 */
7483 static char *idxAppendColDefn(
7484   int *pRc,                       /* IN/OUT: Error code */
7485   char *zIn,                      /* Column defn accumulated so far */
7486   IdxTable *pTab,                 /* Table index will be created on */
7487   IdxConstraint *pCons
7488 ){
7489   char *zRet = zIn;
7490   IdxColumn *p = &pTab->aCol[pCons->iCol];
7491   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7492
7493   if( idxIdentifierRequiresQuotes(p->zName) ){
7494     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7495   }else{
7496     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7497   }
7498
7499   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7500     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7501       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7502     }else{
7503       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7504     }
7505   }
7506
7507   if( pCons->bDesc ){
7508     zRet = idxAppendText(pRc, zRet, " DESC");
7509   }
7510   return zRet;
7511 }
7512
7513 /*
7514 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7515 ** would create from arguments pScan, pEq and pTail. If no error occurs and 
7516 ** such an index is found, return non-zero. Or, if no such index is found,
7517 ** return zero.
7518 **
7519 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7520 */
7521 static int idxFindCompatible(
7522   int *pRc,                       /* OUT: Error code */
7523   sqlite3* dbm,                   /* Database to search */
7524   IdxScan *pScan,                 /* Scan for table to search for index on */
7525   IdxConstraint *pEq,             /* List of == constraints */
7526   IdxConstraint *pTail            /* List of range constraints */
7527 ){
7528   const char *zTbl = pScan->pTab->zName;
7529   sqlite3_stmt *pIdxList = 0;
7530   IdxConstraint *pIter;
7531   int nEq = 0;                    /* Number of elements in pEq */
7532   int rc;
7533
7534   /* Count the elements in list pEq */
7535   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7536
7537   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7538   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7539     int bMatch = 1;
7540     IdxConstraint *pT = pTail;
7541     sqlite3_stmt *pInfo = 0;
7542     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7543
7544     /* Zero the IdxConstraint.bFlag values in the pEq list */
7545     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7546
7547     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7548     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7549       int iIdx = sqlite3_column_int(pInfo, 0);
7550       int iCol = sqlite3_column_int(pInfo, 1);
7551       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7552
7553       if( iIdx<nEq ){
7554         for(pIter=pEq; pIter; pIter=pIter->pLink){
7555           if( pIter->bFlag ) continue;
7556           if( pIter->iCol!=iCol ) continue;
7557           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7558           pIter->bFlag = 1;
7559           break;
7560         }
7561         if( pIter==0 ){
7562           bMatch = 0;
7563           break;
7564         }
7565       }else{
7566         if( pT ){
7567           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7568             bMatch = 0;
7569             break;
7570           }
7571           pT = pT->pLink;
7572         }
7573       }
7574     }
7575     idxFinalize(&rc, pInfo);
7576
7577     if( rc==SQLITE_OK && bMatch ){
7578       sqlite3_finalize(pIdxList);
7579       return 1;
7580     }
7581   }
7582   idxFinalize(&rc, pIdxList);
7583
7584   *pRc = rc;
7585   return 0;
7586 }
7587
7588 static int idxCreateFromCons(
7589   sqlite3expert *p,
7590   IdxScan *pScan,
7591   IdxConstraint *pEq, 
7592   IdxConstraint *pTail
7593 ){
7594   sqlite3 *dbm = p->dbm;
7595   int rc = SQLITE_OK;
7596   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7597     IdxTable *pTab = pScan->pTab;
7598     char *zCols = 0;
7599     char *zIdx = 0;
7600     IdxConstraint *pCons;
7601     unsigned int h = 0;
7602     const char *zFmt;
7603
7604     for(pCons=pEq; pCons; pCons=pCons->pLink){
7605       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7606     }
7607     for(pCons=pTail; pCons; pCons=pCons->pLink){
7608       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7609     }
7610
7611     if( rc==SQLITE_OK ){
7612       /* Hash the list of columns to come up with a name for the index */
7613       const char *zTable = pScan->pTab->zName;
7614       char *zName;                /* Index name */
7615       int i;
7616       for(i=0; zCols[i]; i++){
7617         h += ((h<<3) + zCols[i]);
7618       }
7619       zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7620       if( zName==0 ){ 
7621         rc = SQLITE_NOMEM;
7622       }else{
7623         if( idxIdentifierRequiresQuotes(zTable) ){
7624           zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7625         }else{
7626           zFmt = "CREATE INDEX %s ON %s(%s)";
7627         }
7628         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7629         if( !zIdx ){
7630           rc = SQLITE_NOMEM;
7631         }else{
7632           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7633           idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7634         }
7635         sqlite3_free(zName);
7636         sqlite3_free(zIdx);
7637       }
7638     }
7639
7640     sqlite3_free(zCols);
7641   }
7642   return rc;
7643 }
7644
7645 /*
7646 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7647 ** a constraint compatible with *p. Otherwise return false.
7648 */
7649 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7650   IdxConstraint *pCmp;
7651   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7652     if( p->iCol==pCmp->iCol ) return 1;
7653   }
7654   return 0;
7655 }
7656
7657 static int idxCreateFromWhere(
7658   sqlite3expert *p, 
7659   IdxScan *pScan,                 /* Create indexes for this scan */
7660   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
7661 ){
7662   IdxConstraint *p1 = 0;
7663   IdxConstraint *pCon;
7664   int rc;
7665
7666   /* Gather up all the == constraints. */
7667   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7668     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7669       pCon->pLink = p1;
7670       p1 = pCon;
7671     }
7672   }
7673
7674   /* Create an index using the == constraints collected above. And the
7675   ** range constraint/ORDER BY terms passed in by the caller, if any. */
7676   rc = idxCreateFromCons(p, pScan, p1, pTail);
7677
7678   /* If no range/ORDER BY passed by the caller, create a version of the
7679   ** index for each range constraint.  */
7680   if( pTail==0 ){
7681     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7682       assert( pCon->pLink==0 );
7683       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7684         rc = idxCreateFromCons(p, pScan, p1, pCon);
7685       }
7686     }
7687   }
7688
7689   return rc;
7690 }
7691
7692 /*
7693 ** Create candidate indexes in database [dbm] based on the data in 
7694 ** linked-list pScan.
7695 */
7696 static int idxCreateCandidates(sqlite3expert *p){
7697   int rc = SQLITE_OK;
7698   IdxScan *pIter;
7699
7700   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7701     rc = idxCreateFromWhere(p, pIter, 0);
7702     if( rc==SQLITE_OK && pIter->pOrder ){
7703       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7704     }
7705   }
7706
7707   return rc;
7708 }
7709
7710 /*
7711 ** Free all elements of the linked list starting at pConstraint.
7712 */
7713 static void idxConstraintFree(IdxConstraint *pConstraint){
7714   IdxConstraint *pNext;
7715   IdxConstraint *p;
7716
7717   for(p=pConstraint; p; p=pNext){
7718     pNext = p->pNext;
7719     sqlite3_free(p);
7720   }
7721 }
7722
7723 /*
7724 ** Free all elements of the linked list starting from pScan up until pLast
7725 ** (pLast is not freed).
7726 */
7727 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7728   IdxScan *p;
7729   IdxScan *pNext;
7730   for(p=pScan; p!=pLast; p=pNext){
7731     pNext = p->pNextScan;
7732     idxConstraintFree(p->pOrder);
7733     idxConstraintFree(p->pEq);
7734     idxConstraintFree(p->pRange);
7735     sqlite3_free(p);
7736   }
7737 }
7738
7739 /*
7740 ** Free all elements of the linked list starting from pStatement up 
7741 ** until pLast (pLast is not freed).
7742 */
7743 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7744   IdxStatement *p;
7745   IdxStatement *pNext;
7746   for(p=pStatement; p!=pLast; p=pNext){
7747     pNext = p->pNext;
7748     sqlite3_free(p->zEQP);
7749     sqlite3_free(p->zIdx);
7750     sqlite3_free(p);
7751   }
7752 }
7753
7754 /*
7755 ** Free the linked list of IdxTable objects starting at pTab.
7756 */
7757 static void idxTableFree(IdxTable *pTab){
7758   IdxTable *pIter;
7759   IdxTable *pNext;
7760   for(pIter=pTab; pIter; pIter=pNext){
7761     pNext = pIter->pNext;
7762     sqlite3_free(pIter);
7763   }
7764 }
7765
7766 /*
7767 ** Free the linked list of IdxWrite objects starting at pTab.
7768 */
7769 static void idxWriteFree(IdxWrite *pTab){
7770   IdxWrite *pIter;
7771   IdxWrite *pNext;
7772   for(pIter=pTab; pIter; pIter=pNext){
7773     pNext = pIter->pNext;
7774     sqlite3_free(pIter);
7775   }
7776 }
7777
7778
7779
7780 /*
7781 ** This function is called after candidate indexes have been created. It
7782 ** runs all the queries to see which indexes they prefer, and populates
7783 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7784 */
7785 int idxFindIndexes(
7786   sqlite3expert *p,
7787   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
7788 ){
7789   IdxStatement *pStmt;
7790   sqlite3 *dbm = p->dbm;
7791   int rc = SQLITE_OK;
7792
7793   IdxHash hIdx;
7794   idxHashInit(&hIdx);
7795
7796   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7797     IdxHashEntry *pEntry;
7798     sqlite3_stmt *pExplain = 0;
7799     idxHashClear(&hIdx);
7800     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7801         "EXPLAIN QUERY PLAN %s", pStmt->zSql
7802     );
7803     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7804       /* int iId = sqlite3_column_int(pExplain, 0); */
7805       /* int iParent = sqlite3_column_int(pExplain, 1); */
7806       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7807       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7808       int nDetail = STRLEN(zDetail);
7809       int i;
7810
7811       for(i=0; i<nDetail; i++){
7812         const char *zIdx = 0;
7813         if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7814           zIdx = &zDetail[i+13];
7815         }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7816           zIdx = &zDetail[i+22];
7817         }
7818         if( zIdx ){
7819           const char *zSql;
7820           int nIdx = 0;
7821           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7822             nIdx++;
7823           }
7824           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7825           if( zSql ){
7826             idxHashAdd(&rc, &hIdx, zSql, 0);
7827             if( rc ) goto find_indexes_out;
7828           }
7829           break;
7830         }
7831       }
7832
7833       if( zDetail[0]!='-' ){
7834         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7835       }
7836     }
7837
7838     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7839       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7840     }
7841
7842     idxFinalize(&rc, pExplain);
7843   }
7844
7845  find_indexes_out:
7846   idxHashClear(&hIdx);
7847   return rc;
7848 }
7849
7850 static int idxAuthCallback(
7851   void *pCtx,
7852   int eOp,
7853   const char *z3,
7854   const char *z4,
7855   const char *zDb,
7856   const char *zTrigger
7857 ){
7858   int rc = SQLITE_OK;
7859   (void)z4;
7860   (void)zTrigger;
7861   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7862     if( sqlite3_stricmp(zDb, "main")==0 ){
7863       sqlite3expert *p = (sqlite3expert*)pCtx;
7864       IdxTable *pTab;
7865       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7866         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7867       }
7868       if( pTab ){
7869         IdxWrite *pWrite;
7870         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7871           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7872         }
7873         if( pWrite==0 ){
7874           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7875           if( rc==SQLITE_OK ){
7876             pWrite->pTab = pTab;
7877             pWrite->eOp = eOp;
7878             pWrite->pNext = p->pWrite;
7879             p->pWrite = pWrite;
7880           }
7881         }
7882       }
7883     }
7884   }
7885   return rc;
7886 }
7887
7888 static int idxProcessOneTrigger(
7889   sqlite3expert *p, 
7890   IdxWrite *pWrite, 
7891   char **pzErr
7892 ){
7893   static const char *zInt = UNIQUE_TABLE_NAME;
7894   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7895   IdxTable *pTab = pWrite->pTab;
7896   const char *zTab = pTab->zName;
7897   const char *zSql = 
7898     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7899     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7900     "ORDER BY type;";
7901   sqlite3_stmt *pSelect = 0;
7902   int rc = SQLITE_OK;
7903   char *zWrite = 0;
7904
7905   /* Create the table and its triggers in the temp schema */
7906   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7907   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7908     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7909     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7910   }
7911   idxFinalize(&rc, pSelect);
7912
7913   /* Rename the table in the temp schema to zInt */
7914   if( rc==SQLITE_OK ){
7915     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7916     if( z==0 ){
7917       rc = SQLITE_NOMEM;
7918     }else{
7919       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7920       sqlite3_free(z);
7921     }
7922   }
7923
7924   switch( pWrite->eOp ){
7925     case SQLITE_INSERT: {
7926       int i;
7927       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7928       for(i=0; i<pTab->nCol; i++){
7929         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7930       }
7931       zWrite = idxAppendText(&rc, zWrite, ")");
7932       break;
7933     }
7934     case SQLITE_UPDATE: {
7935       int i;
7936       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7937       for(i=0; i<pTab->nCol; i++){
7938         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", 
7939             pTab->aCol[i].zName
7940         );
7941       }
7942       break;
7943     }
7944     default: {
7945       assert( pWrite->eOp==SQLITE_DELETE );
7946       if( rc==SQLITE_OK ){
7947         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7948         if( zWrite==0 ) rc = SQLITE_NOMEM;
7949       }
7950     }
7951   }
7952
7953   if( rc==SQLITE_OK ){
7954     sqlite3_stmt *pX = 0;
7955     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7956     idxFinalize(&rc, pX);
7957     if( rc!=SQLITE_OK ){
7958       idxDatabaseError(p->dbv, pzErr);
7959     }
7960   }
7961   sqlite3_free(zWrite);
7962
7963   if( rc==SQLITE_OK ){
7964     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7965   }
7966
7967   return rc;
7968 }
7969
7970 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7971   int rc = SQLITE_OK;
7972   IdxWrite *pEnd = 0;
7973   IdxWrite *pFirst = p->pWrite;
7974
7975   while( rc==SQLITE_OK && pFirst!=pEnd ){
7976     IdxWrite *pIter;
7977     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7978       rc = idxProcessOneTrigger(p, pIter, pzErr);
7979     }
7980     pEnd = pFirst;
7981     pFirst = p->pWrite;
7982   }
7983
7984   return rc;
7985 }
7986
7987
7988 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7989   int rc = idxRegisterVtab(p);
7990   sqlite3_stmt *pSchema = 0;
7991
7992   /* For each table in the main db schema:
7993   **
7994   **   1) Add an entry to the p->pTable list, and
7995   **   2) Create the equivalent virtual table in dbv.
7996   */
7997   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
7998       "SELECT type, name, sql, 1 FROM sqlite_master "
7999       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
8000       " UNION ALL "
8001       "SELECT type, name, sql, 2 FROM sqlite_master "
8002       "WHERE type = 'trigger'"
8003       "  AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
8004       "ORDER BY 4, 1"
8005   );
8006   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
8007     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
8008     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
8009     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
8010
8011     if( zType[0]=='v' || zType[1]=='r' ){
8012       rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
8013     }else{
8014       IdxTable *pTab;
8015       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
8016       if( rc==SQLITE_OK ){
8017         int i;
8018         char *zInner = 0;
8019         char *zOuter = 0;
8020         pTab->pNext = p->pTable;
8021         p->pTable = pTab;
8022
8023         /* The statement the vtab will pass to sqlite3_declare_vtab() */
8024         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
8025         for(i=0; i<pTab->nCol; i++){
8026           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", 
8027               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
8028           );
8029         }
8030         zInner = idxAppendText(&rc, zInner, ")");
8031
8032         /* The CVT statement to create the vtab */
8033         zOuter = idxAppendText(&rc, 0, 
8034             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
8035         );
8036         if( rc==SQLITE_OK ){
8037           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
8038         }
8039         sqlite3_free(zInner);
8040         sqlite3_free(zOuter);
8041       }
8042     }
8043   }
8044   idxFinalize(&rc, pSchema);
8045   return rc;
8046 }
8047
8048 struct IdxSampleCtx {
8049   int iTarget;
8050   double target;                  /* Target nRet/nRow value */
8051   double nRow;                    /* Number of rows seen */
8052   double nRet;                    /* Number of rows returned */
8053 };
8054
8055 static void idxSampleFunc(
8056   sqlite3_context *pCtx,
8057   int argc,
8058   sqlite3_value **argv
8059 ){
8060   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
8061   int bRet;
8062
8063   (void)argv;
8064   assert( argc==0 );
8065   if( p->nRow==0.0 ){
8066     bRet = 1;
8067   }else{
8068     bRet = (p->nRet / p->nRow) <= p->target;
8069     if( bRet==0 ){
8070       unsigned short rnd;
8071       sqlite3_randomness(2, (void*)&rnd);
8072       bRet = ((int)rnd % 100) <= p->iTarget;
8073     }
8074   }
8075
8076   sqlite3_result_int(pCtx, bRet);
8077   p->nRow += 1.0;
8078   p->nRet += (double)bRet;
8079 }
8080
8081 struct IdxRemCtx {
8082   int nSlot;
8083   struct IdxRemSlot {
8084     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
8085     i64 iVal;                     /* SQLITE_INTEGER value */
8086     double rVal;                  /* SQLITE_FLOAT value */
8087     int nByte;                    /* Bytes of space allocated at z */
8088     int n;                        /* Size of buffer z */
8089     char *z;                      /* SQLITE_TEXT/BLOB value */
8090   } aSlot[1];
8091 };
8092
8093 /*
8094 ** Implementation of scalar function rem().
8095 */
8096 static void idxRemFunc(
8097   sqlite3_context *pCtx,
8098   int argc,
8099   sqlite3_value **argv
8100 ){
8101   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
8102   struct IdxRemSlot *pSlot;
8103   int iSlot;
8104   assert( argc==2 );
8105
8106   iSlot = sqlite3_value_int(argv[0]);
8107   assert( iSlot<=p->nSlot );
8108   pSlot = &p->aSlot[iSlot];
8109
8110   switch( pSlot->eType ){
8111     case SQLITE_NULL:
8112       /* no-op */
8113       break;
8114
8115     case SQLITE_INTEGER:
8116       sqlite3_result_int64(pCtx, pSlot->iVal);
8117       break;
8118
8119     case SQLITE_FLOAT:
8120       sqlite3_result_double(pCtx, pSlot->rVal);
8121       break;
8122
8123     case SQLITE_BLOB:
8124       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8125       break;
8126
8127     case SQLITE_TEXT:
8128       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8129       break;
8130   }
8131
8132   pSlot->eType = sqlite3_value_type(argv[1]);
8133   switch( pSlot->eType ){
8134     case SQLITE_NULL:
8135       /* no-op */
8136       break;
8137
8138     case SQLITE_INTEGER:
8139       pSlot->iVal = sqlite3_value_int64(argv[1]);
8140       break;
8141
8142     case SQLITE_FLOAT:
8143       pSlot->rVal = sqlite3_value_double(argv[1]);
8144       break;
8145
8146     case SQLITE_BLOB:
8147     case SQLITE_TEXT: {
8148       int nByte = sqlite3_value_bytes(argv[1]);
8149       if( nByte>pSlot->nByte ){
8150         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
8151         if( zNew==0 ){
8152           sqlite3_result_error_nomem(pCtx);
8153           return;
8154         }
8155         pSlot->nByte = nByte*2;
8156         pSlot->z = zNew;
8157       }
8158       pSlot->n = nByte;
8159       if( pSlot->eType==SQLITE_BLOB ){
8160         memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
8161       }else{
8162         memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
8163       }
8164       break;
8165     }
8166   }
8167 }
8168
8169 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
8170   int rc = SQLITE_OK;
8171   const char *zMax = 
8172     "SELECT max(i.seqno) FROM "
8173     "  sqlite_master AS s, "
8174     "  pragma_index_list(s.name) AS l, "
8175     "  pragma_index_info(l.name) AS i "
8176     "WHERE s.type = 'table'";
8177   sqlite3_stmt *pMax = 0;
8178
8179   *pnMax = 0;
8180   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
8181   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8182     *pnMax = sqlite3_column_int(pMax, 0) + 1;
8183   }
8184   idxFinalize(&rc, pMax);
8185
8186   return rc;
8187 }
8188
8189 static int idxPopulateOneStat1(
8190   sqlite3expert *p,
8191   sqlite3_stmt *pIndexXInfo,
8192   sqlite3_stmt *pWriteStat,
8193   const char *zTab,
8194   const char *zIdx,
8195   char **pzErr
8196 ){
8197   char *zCols = 0;
8198   char *zOrder = 0;
8199   char *zQuery = 0;
8200   int nCol = 0;
8201   int i;
8202   sqlite3_stmt *pQuery = 0;
8203   int *aStat = 0;
8204   int rc = SQLITE_OK;
8205
8206   assert( p->iSample>0 );
8207
8208   /* Formulate the query text */
8209   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8210   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8211     const char *zComma = zCols==0 ? "" : ", ";
8212     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8213     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8214     zCols = idxAppendText(&rc, zCols, 
8215         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8216     );
8217     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8218   }
8219   sqlite3_reset(pIndexXInfo);
8220   if( rc==SQLITE_OK ){
8221     if( p->iSample==100 ){
8222       zQuery = sqlite3_mprintf(
8223           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8224       );
8225     }else{
8226       zQuery = sqlite3_mprintf(
8227           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8228       );
8229     }
8230   }
8231   sqlite3_free(zCols);
8232   sqlite3_free(zOrder);
8233
8234   /* Formulate the query text */
8235   if( rc==SQLITE_OK ){
8236     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8237     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8238   }
8239   sqlite3_free(zQuery);
8240
8241   if( rc==SQLITE_OK ){
8242     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8243   }
8244   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8245     IdxHashEntry *pEntry;
8246     char *zStat = 0;
8247     for(i=0; i<=nCol; i++) aStat[i] = 1;
8248     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8249       aStat[0]++;
8250       for(i=0; i<nCol; i++){
8251         if( sqlite3_column_int(pQuery, i)==0 ) break;
8252       }
8253       for(/*no-op*/; i<nCol; i++){
8254         aStat[i+1]++;
8255       }
8256     }
8257
8258     if( rc==SQLITE_OK ){
8259       int s0 = aStat[0];
8260       zStat = sqlite3_mprintf("%d", s0);
8261       if( zStat==0 ) rc = SQLITE_NOMEM;
8262       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8263         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8264       }
8265     }
8266
8267     if( rc==SQLITE_OK ){
8268       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8269       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8270       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8271       sqlite3_step(pWriteStat);
8272       rc = sqlite3_reset(pWriteStat);
8273     }
8274
8275     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8276     if( pEntry ){
8277       assert( pEntry->zVal2==0 );
8278       pEntry->zVal2 = zStat;
8279     }else{
8280       sqlite3_free(zStat);
8281     }
8282   }
8283   sqlite3_free(aStat);
8284   idxFinalize(&rc, pQuery);
8285
8286   return rc;
8287 }
8288
8289 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8290   int rc;
8291   char *zSql;
8292
8293   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8294   if( rc!=SQLITE_OK ) return rc;
8295
8296   zSql = sqlite3_mprintf(
8297       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8298   );
8299   if( zSql==0 ) return SQLITE_NOMEM;
8300   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8301   sqlite3_free(zSql);
8302
8303   return rc;
8304 }
8305
8306 /*
8307 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8308 ** indexes have already been created in database sqlite3expert.dbm, this
8309 ** function populates sqlite_stat1 table in the same database.
8310 **
8311 ** The stat1 data is generated by querying the 
8312 */
8313 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8314   int rc = SQLITE_OK;
8315   int nMax =0;
8316   struct IdxRemCtx *pCtx = 0;
8317   struct IdxSampleCtx samplectx; 
8318   int i;
8319   i64 iPrev = -100000;
8320   sqlite3_stmt *pAllIndex = 0;
8321   sqlite3_stmt *pIndexXInfo = 0;
8322   sqlite3_stmt *pWrite = 0;
8323
8324   const char *zAllIndex =
8325     "SELECT s.rowid, s.name, l.name FROM "
8326     "  sqlite_master AS s, "
8327     "  pragma_index_list(s.name) AS l "
8328     "WHERE s.type = 'table'";
8329   const char *zIndexXInfo = 
8330     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8331   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8332
8333   /* If iSample==0, no sqlite_stat1 data is required. */
8334   if( p->iSample==0 ) return SQLITE_OK;
8335
8336   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8337   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8338
8339   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8340
8341   if( rc==SQLITE_OK ){
8342     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8343     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8344   }
8345
8346   if( rc==SQLITE_OK ){
8347     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8348     rc = sqlite3_create_function(
8349         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8350     );
8351   }
8352   if( rc==SQLITE_OK ){
8353     rc = sqlite3_create_function(
8354         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8355     );
8356   }
8357
8358   if( rc==SQLITE_OK ){
8359     pCtx->nSlot = nMax+1;
8360     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8361   }
8362   if( rc==SQLITE_OK ){
8363     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8364   }
8365   if( rc==SQLITE_OK ){
8366     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8367   }
8368
8369   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8370     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8371     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8372     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8373     if( p->iSample<100 && iPrev!=iRowid ){
8374       samplectx.target = (double)p->iSample / 100.0;
8375       samplectx.iTarget = p->iSample;
8376       samplectx.nRow = 0.0;
8377       samplectx.nRet = 0.0;
8378       rc = idxBuildSampleTable(p, zTab);
8379       if( rc!=SQLITE_OK ) break;
8380     }
8381     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8382     iPrev = iRowid;
8383   }
8384   if( rc==SQLITE_OK && p->iSample<100 ){
8385     rc = sqlite3_exec(p->dbv, 
8386         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8387     );
8388   }
8389
8390   idxFinalize(&rc, pAllIndex);
8391   idxFinalize(&rc, pIndexXInfo);
8392   idxFinalize(&rc, pWrite);
8393
8394   for(i=0; i<pCtx->nSlot; i++){
8395     sqlite3_free(pCtx->aSlot[i].z);
8396   }
8397   sqlite3_free(pCtx);
8398
8399   if( rc==SQLITE_OK ){
8400     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8401   }
8402
8403   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8404   return rc;
8405 }
8406
8407 /*
8408 ** Allocate a new sqlite3expert object.
8409 */
8410 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8411   int rc = SQLITE_OK;
8412   sqlite3expert *pNew;
8413
8414   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8415
8416   /* Open two in-memory databases to work with. The "vtab database" (dbv)
8417   ** will contain a virtual table corresponding to each real table in
8418   ** the user database schema, and a copy of each view. It is used to
8419   ** collect information regarding the WHERE, ORDER BY and other clauses
8420   ** of the user's query.
8421   */
8422   if( rc==SQLITE_OK ){
8423     pNew->db = db;
8424     pNew->iSample = 100;
8425     rc = sqlite3_open(":memory:", &pNew->dbv);
8426   }
8427   if( rc==SQLITE_OK ){
8428     rc = sqlite3_open(":memory:", &pNew->dbm);
8429     if( rc==SQLITE_OK ){
8430       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8431     }
8432   }
8433   
8434
8435   /* Copy the entire schema of database [db] into [dbm]. */
8436   if( rc==SQLITE_OK ){
8437     sqlite3_stmt *pSql;
8438     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, 
8439         "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8440         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8441     );
8442     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8443       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8444       rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8445     }
8446     idxFinalize(&rc, pSql);
8447   }
8448
8449   /* Create the vtab schema */
8450   if( rc==SQLITE_OK ){
8451     rc = idxCreateVtabSchema(pNew, pzErrmsg);
8452   }
8453
8454   /* Register the auth callback with dbv */
8455   if( rc==SQLITE_OK ){
8456     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8457   }
8458
8459   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8460   ** return the new sqlite3expert handle.  */
8461   if( rc!=SQLITE_OK ){
8462     sqlite3_expert_destroy(pNew);
8463     pNew = 0;
8464   }
8465   return pNew;
8466 }
8467
8468 /*
8469 ** Configure an sqlite3expert object.
8470 */
8471 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8472   int rc = SQLITE_OK;
8473   va_list ap;
8474   va_start(ap, op);
8475   switch( op ){
8476     case EXPERT_CONFIG_SAMPLE: {
8477       int iVal = va_arg(ap, int);
8478       if( iVal<0 ) iVal = 0;
8479       if( iVal>100 ) iVal = 100;
8480       p->iSample = iVal;
8481       break;
8482     }
8483     default:
8484       rc = SQLITE_NOTFOUND;
8485       break;
8486   }
8487
8488   va_end(ap);
8489   return rc;
8490 }
8491
8492 /*
8493 ** Add an SQL statement to the analysis.
8494 */
8495 int sqlite3_expert_sql(
8496   sqlite3expert *p,               /* From sqlite3_expert_new() */
8497   const char *zSql,               /* SQL statement to add */
8498   char **pzErr                    /* OUT: Error message (if any) */
8499 ){
8500   IdxScan *pScanOrig = p->pScan;
8501   IdxStatement *pStmtOrig = p->pStatement;
8502   int rc = SQLITE_OK;
8503   const char *zStmt = zSql;
8504
8505   if( p->bRun ) return SQLITE_MISUSE;
8506
8507   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8508     sqlite3_stmt *pStmt = 0;
8509     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8510     if( rc==SQLITE_OK ){
8511       if( pStmt ){
8512         IdxStatement *pNew;
8513         const char *z = sqlite3_sql(pStmt);
8514         int n = STRLEN(z);
8515         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8516         if( rc==SQLITE_OK ){
8517           pNew->zSql = (char*)&pNew[1];
8518           memcpy(pNew->zSql, z, n+1);
8519           pNew->pNext = p->pStatement;
8520           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8521           p->pStatement = pNew;
8522         }
8523         sqlite3_finalize(pStmt);
8524       }
8525     }else{
8526       idxDatabaseError(p->dbv, pzErr);
8527     }
8528   }
8529
8530   if( rc!=SQLITE_OK ){
8531     idxScanFree(p->pScan, pScanOrig);
8532     idxStatementFree(p->pStatement, pStmtOrig);
8533     p->pScan = pScanOrig;
8534     p->pStatement = pStmtOrig;
8535   }
8536
8537   return rc;
8538 }
8539
8540 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8541   int rc;
8542   IdxHashEntry *pEntry;
8543
8544   /* Do trigger processing to collect any extra IdxScan structures */
8545   rc = idxProcessTriggers(p, pzErr);
8546
8547   /* Create candidate indexes within the in-memory database file */
8548   if( rc==SQLITE_OK ){
8549     rc = idxCreateCandidates(p);
8550   }
8551
8552   /* Generate the stat1 data */
8553   if( rc==SQLITE_OK ){
8554     rc = idxPopulateStat1(p, pzErr);
8555   }
8556
8557   /* Formulate the EXPERT_REPORT_CANDIDATES text */
8558   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8559     p->zCandidates = idxAppendText(&rc, p->zCandidates, 
8560         "%s;%s%s\n", pEntry->zVal, 
8561         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8562     );
8563   }
8564
8565   /* Figure out which of the candidate indexes are preferred by the query
8566   ** planner and report the results to the user.  */
8567   if( rc==SQLITE_OK ){
8568     rc = idxFindIndexes(p, pzErr);
8569   }
8570
8571   if( rc==SQLITE_OK ){
8572     p->bRun = 1;
8573   }
8574   return rc;
8575 }
8576
8577 /*
8578 ** Return the total number of statements that have been added to this
8579 ** sqlite3expert using sqlite3_expert_sql().
8580 */
8581 int sqlite3_expert_count(sqlite3expert *p){
8582   int nRet = 0;
8583   if( p->pStatement ) nRet = p->pStatement->iId+1;
8584   return nRet;
8585 }
8586
8587 /*
8588 ** Return a component of the report.
8589 */
8590 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8591   const char *zRet = 0;
8592   IdxStatement *pStmt;
8593
8594   if( p->bRun==0 ) return 0;
8595   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8596   switch( eReport ){
8597     case EXPERT_REPORT_SQL:
8598       if( pStmt ) zRet = pStmt->zSql;
8599       break;
8600     case EXPERT_REPORT_INDEXES:
8601       if( pStmt ) zRet = pStmt->zIdx;
8602       break;
8603     case EXPERT_REPORT_PLAN:
8604       if( pStmt ) zRet = pStmt->zEQP;
8605       break;
8606     case EXPERT_REPORT_CANDIDATES:
8607       zRet = p->zCandidates;
8608       break;
8609   }
8610   return zRet;
8611 }
8612
8613 /*
8614 ** Free an sqlite3expert object.
8615 */
8616 void sqlite3_expert_destroy(sqlite3expert *p){
8617   if( p ){
8618     sqlite3_close(p->dbm);
8619     sqlite3_close(p->dbv);
8620     idxScanFree(p->pScan, 0);
8621     idxStatementFree(p->pStatement, 0);
8622     idxTableFree(p->pTable);
8623     idxWriteFree(p->pWrite);
8624     idxHashClear(&p->hIdx);
8625     sqlite3_free(p->zCandidates);
8626     sqlite3_free(p);
8627   }
8628 }
8629
8630 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
8631
8632 /************************* End ../ext/expert/sqlite3expert.c ********************/
8633
8634 #if defined(SQLITE_ENABLE_SESSION)
8635 /*
8636 ** State information for a single open session
8637 */
8638 typedef struct OpenSession OpenSession;
8639 struct OpenSession {
8640   char *zName;             /* Symbolic name for this session */
8641   int nFilter;             /* Number of xFilter rejection GLOB patterns */
8642   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
8643   sqlite3_session *p;      /* The open session */
8644 };
8645 #endif
8646
8647 /*
8648 ** Shell output mode information from before ".explain on",
8649 ** saved so that it can be restored by ".explain off"
8650 */
8651 typedef struct SavedModeInfo SavedModeInfo;
8652 struct SavedModeInfo {
8653   int valid;          /* Is there legit data in here? */
8654   int mode;           /* Mode prior to ".explain on" */
8655   int showHeader;     /* The ".header" setting prior to ".explain on" */
8656   int colWidth[100];  /* Column widths prior to ".explain on" */
8657 };
8658
8659 typedef struct ExpertInfo ExpertInfo;
8660 struct ExpertInfo {
8661   sqlite3expert *pExpert;
8662   int bVerbose;
8663 };
8664
8665 /* A single line in the EQP output */
8666 typedef struct EQPGraphRow EQPGraphRow;
8667 struct EQPGraphRow {
8668   int iEqpId;           /* ID for this row */
8669   int iParentId;        /* ID of the parent row */
8670   EQPGraphRow *pNext;   /* Next row in sequence */
8671   char zText[1];        /* Text to display for this row */
8672 };
8673
8674 /* All EQP output is collected into an instance of the following */
8675 typedef struct EQPGraph EQPGraph;
8676 struct EQPGraph {
8677   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
8678   EQPGraphRow *pLast;   /* Last element of the pRow list */
8679   char zPrefix[100];    /* Graph prefix */
8680 };
8681
8682 /*
8683 ** State information about the database connection is contained in an
8684 ** instance of the following structure.
8685 */
8686 typedef struct ShellState ShellState;
8687 struct ShellState {
8688   sqlite3 *db;           /* The database */
8689   u8 autoExplain;        /* Automatically turn on .explain mode */
8690   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
8691   u8 autoEQPtest;        /* autoEQP is in test mode */
8692   u8 autoEQPtrace;       /* autoEQP is in trace mode */
8693   u8 statsOn;            /* True to display memory stats before each finalize */
8694   u8 scanstatsOn;        /* True to display scan stats before each finalize */
8695   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
8696   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
8697   u8 nEqpLevel;          /* Depth of the EQP output graph */
8698   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
8699   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
8700   int outCount;          /* Revert to stdout when reaching zero */
8701   int cnt;               /* Number of records displayed so far */
8702   int lineno;            /* Line number of last line read from in */
8703   FILE *in;              /* Read commands from this stream */
8704   FILE *out;             /* Write results here */
8705   FILE *traceOut;        /* Output for sqlite3_trace() */
8706   int nErr;              /* Number of errors seen */
8707   int mode;              /* An output mode setting */
8708   int modePrior;         /* Saved mode */
8709   int cMode;             /* temporary output mode for the current query */
8710   int normalMode;        /* Output mode before ".explain on" */
8711   int writableSchema;    /* True if PRAGMA writable_schema=ON */
8712   int showHeader;        /* True to show column names in List or Column mode */
8713   int nCheck;            /* Number of ".check" commands run */
8714   unsigned nProgress;    /* Number of progress callbacks encountered */
8715   unsigned mxProgress;   /* Maximum progress callbacks before failing */
8716   unsigned flgProgress;  /* Flags for the progress callback */
8717   unsigned shellFlgs;    /* Various flags */
8718   sqlite3_int64 szMax;   /* --maxsize argument to .open */
8719   char *zDestTable;      /* Name of destination table when MODE_Insert */
8720   char *zTempFile;       /* Temporary file that might need deleting */
8721   char zTestcase[30];    /* Name of current test case */
8722   char colSeparator[20]; /* Column separator character for several modes */
8723   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
8724   char colSepPrior[20];  /* Saved column separator */
8725   char rowSepPrior[20];  /* Saved row separator */
8726   int colWidth[100];     /* Requested width of each column when in column mode*/
8727   int actualWidth[100];  /* Actual width of each column */
8728   char nullValue[20];    /* The text to print when a NULL comes back from
8729                          ** the database */
8730   char outfile[FILENAME_MAX]; /* Filename for *out */
8731   const char *zDbFilename;    /* name of the database file */
8732   char *zFreeOnClose;         /* Filename to free when closing */
8733   const char *zVfs;           /* Name of VFS to use */
8734   sqlite3_stmt *pStmt;   /* Current statement if any. */
8735   FILE *pLog;            /* Write log output here */
8736   int *aiIndent;         /* Array of indents used in MODE_Explain */
8737   int nIndent;           /* Size of array aiIndent[] */
8738   int iIndent;           /* Index of current op in aiIndent[] */
8739   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
8740 #if defined(SQLITE_ENABLE_SESSION)
8741   int nSession;             /* Number of active sessions */
8742   OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
8743 #endif
8744   ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
8745 };
8746
8747
8748 /* Allowed values for ShellState.autoEQP
8749 */
8750 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
8751 #define AUTOEQP_on       1           /* Automatic EQP is on */
8752 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
8753 #define AUTOEQP_full     3           /* Show full EXPLAIN */
8754
8755 /* Allowed values for ShellState.openMode
8756 */
8757 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
8758 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
8759 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
8760 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
8761 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
8762 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
8763 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
8764
8765 /* Allowed values for ShellState.eTraceType
8766 */
8767 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
8768 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
8769 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
8770
8771 /* Bits in the ShellState.flgProgress variable */
8772 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
8773 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
8774                                    ** callback limit is reached, and for each
8775                                    ** top-level SQL statement */
8776 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
8777
8778 /*
8779 ** These are the allowed shellFlgs values
8780 */
8781 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
8782 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
8783 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
8784 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
8785 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
8786 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
8787 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
8788
8789 /*
8790 ** Macros for testing and setting shellFlgs
8791 */
8792 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
8793 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
8794 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
8795
8796 /*
8797 ** These are the allowed modes.
8798 */
8799 #define MODE_Line     0  /* One column per line.  Blank line between records */
8800 #define MODE_Column   1  /* One record per line in neat columns */
8801 #define MODE_List     2  /* One record per line with a separator */
8802 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
8803 #define MODE_Html     4  /* Generate an XHTML table */
8804 #define MODE_Insert   5  /* Generate SQL "insert" statements */
8805 #define MODE_Quote    6  /* Quote values as for SQL */
8806 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
8807 #define MODE_Csv      8  /* Quote strings, numbers are plain */
8808 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
8809 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
8810 #define MODE_Pretty  11  /* Pretty-print schemas */
8811 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
8812
8813 static const char *modeDescr[] = {
8814   "line",
8815   "column",
8816   "list",
8817   "semi",
8818   "html",
8819   "insert",
8820   "quote",
8821   "tcl",
8822   "csv",
8823   "explain",
8824   "ascii",
8825   "prettyprint",
8826   "eqp"
8827 };
8828
8829 /*
8830 ** These are the column/row/line separators used by the various
8831 ** import/export modes.
8832 */
8833 #define SEP_Column    "|"
8834 #define SEP_Row       "\n"
8835 #define SEP_Tab       "\t"
8836 #define SEP_Space     " "
8837 #define SEP_Comma     ","
8838 #define SEP_CrLf      "\r\n"
8839 #define SEP_Unit      "\x1F"
8840 #define SEP_Record    "\x1E"
8841
8842 /*
8843 ** A callback for the sqlite3_log() interface.
8844 */
8845 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
8846   ShellState *p = (ShellState*)pArg;
8847   if( p->pLog==0 ) return;
8848   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
8849   fflush(p->pLog);
8850 }
8851
8852 /*
8853 ** SQL function:  shell_putsnl(X)
8854 **
8855 ** Write the text X to the screen (or whatever output is being directed)
8856 ** adding a newline at the end, and then return X.
8857 */
8858 static void shellPutsFunc(
8859   sqlite3_context *pCtx,
8860   int nVal,
8861   sqlite3_value **apVal
8862 ){
8863   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
8864   (void)nVal;
8865   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
8866   sqlite3_result_value(pCtx, apVal[0]);
8867 }
8868
8869 /*
8870 ** SQL function:   edit(VALUE)
8871 **                 edit(VALUE,EDITOR)
8872 **
8873 ** These steps:
8874 **
8875 **     (1) Write VALUE into a temporary file.
8876 **     (2) Run program EDITOR on that temporary file.
8877 **     (3) Read the temporary file back and return its content as the result.
8878 **     (4) Delete the temporary file
8879 **
8880 ** If the EDITOR argument is omitted, use the value in the VISUAL
8881 ** environment variable.  If still there is no EDITOR, through an error.
8882 **
8883 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8884 */
8885 #ifndef SQLITE_NOHAVE_SYSTEM
8886 static void editFunc(
8887   sqlite3_context *context,
8888   int argc,
8889   sqlite3_value **argv
8890 ){
8891   const char *zEditor;
8892   char *zTempFile = 0;
8893   sqlite3 *db;
8894   char *zCmd = 0;
8895   int bBin;
8896   int rc;
8897   int hasCRNL = 0;
8898   FILE *f = 0;
8899   sqlite3_int64 sz;
8900   sqlite3_int64 x;
8901   unsigned char *p = 0;
8902
8903   if( argc==2 ){
8904     zEditor = (const char*)sqlite3_value_text(argv[1]);
8905   }else{
8906     zEditor = getenv("VISUAL");
8907   }
8908   if( zEditor==0 ){
8909     sqlite3_result_error(context, "no editor for edit()", -1);
8910     return;
8911   }
8912   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
8913     sqlite3_result_error(context, "NULL input to edit()", -1);
8914     return;
8915   }
8916   db = sqlite3_context_db_handle(context);
8917   zTempFile = 0;
8918   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
8919   if( zTempFile==0 ){
8920     sqlite3_uint64 r = 0;
8921     sqlite3_randomness(sizeof(r), &r);
8922     zTempFile = sqlite3_mprintf("temp%llx", r);
8923     if( zTempFile==0 ){
8924       sqlite3_result_error_nomem(context);
8925       return;
8926     }
8927   }
8928   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
8929   /* When writing the file to be edited, do \n to \r\n conversions on systems
8930   ** that want \r\n line endings */
8931   f = fopen(zTempFile, bBin ? "wb" : "w");
8932   if( f==0 ){
8933     sqlite3_result_error(context, "edit() cannot open temp file", -1);
8934     goto edit_func_end;
8935   }
8936   sz = sqlite3_value_bytes(argv[0]);
8937   if( bBin ){
8938     x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
8939   }else{
8940     const char *z = (const char*)sqlite3_value_text(argv[0]);
8941     /* Remember whether or not the value originally contained \r\n */
8942     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
8943     x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
8944   }
8945   fclose(f);
8946   f = 0;
8947   if( x!=sz ){
8948     sqlite3_result_error(context, "edit() could not write the whole file", -1);
8949     goto edit_func_end;
8950   }
8951   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
8952   if( zCmd==0 ){
8953     sqlite3_result_error_nomem(context);
8954     goto edit_func_end;
8955   }
8956   rc = system(zCmd);
8957   sqlite3_free(zCmd);
8958   if( rc ){
8959     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
8960     goto edit_func_end;
8961   }
8962   f = fopen(zTempFile, "rb");
8963   if( f==0 ){
8964     sqlite3_result_error(context,
8965       "edit() cannot reopen temp file after edit", -1);
8966     goto edit_func_end;
8967   }
8968   fseek(f, 0, SEEK_END);
8969   sz = ftell(f);
8970   rewind(f);
8971   p = sqlite3_malloc64( sz+(bBin==0) );
8972   if( p==0 ){
8973     sqlite3_result_error_nomem(context);
8974     goto edit_func_end;
8975   }
8976   x = fread(p, 1, sz, f);
8977   fclose(f);
8978   f = 0;
8979   if( x!=sz ){
8980     sqlite3_result_error(context, "could not read back the whole file", -1);
8981     goto edit_func_end;
8982   }
8983   if( bBin ){
8984     sqlite3_result_blob64(context, p, sz, sqlite3_free);
8985   }else{
8986     sqlite3_int64 i, j;
8987     if( hasCRNL ){
8988       /* If the original contains \r\n then do no conversions back to \n */
8989       j = sz;
8990     }else{
8991       /* If the file did not originally contain \r\n then convert any new
8992       ** \r\n back into \n */
8993       for(i=j=0; i<sz; i++){
8994         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
8995         p[j++] = p[i];
8996       }
8997       sz = j;
8998       p[sz] = 0;
8999     } 
9000     sqlite3_result_text64(context, (const char*)p, sz,
9001                           sqlite3_free, SQLITE_UTF8);
9002   }
9003   p = 0;
9004
9005 edit_func_end:
9006   if( f ) fclose(f);
9007   unlink(zTempFile);
9008   sqlite3_free(zTempFile);
9009   sqlite3_free(p);
9010 }
9011 #endif /* SQLITE_NOHAVE_SYSTEM */
9012
9013 /*
9014 ** Save or restore the current output mode
9015 */
9016 static void outputModePush(ShellState *p){
9017   p->modePrior = p->mode;
9018   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
9019   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
9020 }
9021 static void outputModePop(ShellState *p){
9022   p->mode = p->modePrior;
9023   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
9024   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
9025 }
9026
9027 /*
9028 ** Output the given string as a hex-encoded blob (eg. X'1234' )
9029 */
9030 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
9031   int i;
9032   char *zBlob = (char *)pBlob;
9033   raw_printf(out,"X'");
9034   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
9035   raw_printf(out,"'");
9036 }
9037
9038 /*
9039 ** Find a string that is not found anywhere in z[].  Return a pointer
9040 ** to that string.
9041 **
9042 ** Try to use zA and zB first.  If both of those are already found in z[]
9043 ** then make up some string and store it in the buffer zBuf.
9044 */
9045 static const char *unused_string(
9046   const char *z,                    /* Result must not appear anywhere in z */
9047   const char *zA, const char *zB,   /* Try these first */
9048   char *zBuf                        /* Space to store a generated string */
9049 ){
9050   unsigned i = 0;
9051   if( strstr(z, zA)==0 ) return zA;
9052   if( strstr(z, zB)==0 ) return zB;
9053   do{
9054     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
9055   }while( strstr(z,zBuf)!=0 );
9056   return zBuf;
9057 }
9058
9059 /*
9060 ** Output the given string as a quoted string using SQL quoting conventions.
9061 **
9062 ** See also: output_quoted_escaped_string()
9063 */
9064 static void output_quoted_string(FILE *out, const char *z){
9065   int i;
9066   char c;
9067   setBinaryMode(out, 1);
9068   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9069   if( c==0 ){
9070     utf8_printf(out,"'%s'",z);
9071   }else{
9072     raw_printf(out, "'");
9073     while( *z ){
9074       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9075       if( c=='\'' ) i++;
9076       if( i ){
9077         utf8_printf(out, "%.*s", i, z);
9078         z += i;
9079       }
9080       if( c=='\'' ){
9081         raw_printf(out, "'");
9082         continue;
9083       }
9084       if( c==0 ){
9085         break;
9086       }
9087       z++;
9088     }
9089     raw_printf(out, "'");
9090   }
9091   setTextMode(out, 1);
9092 }
9093
9094 /*
9095 ** Output the given string as a quoted string using SQL quoting conventions.
9096 ** Additionallly , escape the "\n" and "\r" characters so that they do not
9097 ** get corrupted by end-of-line translation facilities in some operating
9098 ** systems.
9099 **
9100 ** This is like output_quoted_string() but with the addition of the \r\n
9101 ** escape mechanism.
9102 */
9103 static void output_quoted_escaped_string(FILE *out, const char *z){
9104   int i;
9105   char c;
9106   setBinaryMode(out, 1);
9107   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
9108   if( c==0 ){
9109     utf8_printf(out,"'%s'",z);
9110   }else{
9111     const char *zNL = 0;
9112     const char *zCR = 0;
9113     int nNL = 0;
9114     int nCR = 0;
9115     char zBuf1[20], zBuf2[20];
9116     for(i=0; z[i]; i++){
9117       if( z[i]=='\n' ) nNL++;
9118       if( z[i]=='\r' ) nCR++;
9119     }
9120     if( nNL ){
9121       raw_printf(out, "replace(");
9122       zNL = unused_string(z, "\\n", "\\012", zBuf1);
9123     }
9124     if( nCR ){
9125       raw_printf(out, "replace(");
9126       zCR = unused_string(z, "\\r", "\\015", zBuf2);
9127     }
9128     raw_printf(out, "'");
9129     while( *z ){
9130       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
9131       if( c=='\'' ) i++;
9132       if( i ){
9133         utf8_printf(out, "%.*s", i, z);
9134         z += i;
9135       }
9136       if( c=='\'' ){
9137         raw_printf(out, "'");
9138         continue;
9139       }
9140       if( c==0 ){
9141         break;
9142       }
9143       z++;
9144       if( c=='\n' ){
9145         raw_printf(out, "%s", zNL);
9146         continue;
9147       }
9148       raw_printf(out, "%s", zCR);
9149     }
9150     raw_printf(out, "'");
9151     if( nCR ){
9152       raw_printf(out, ",'%s',char(13))", zCR);
9153     }
9154     if( nNL ){
9155       raw_printf(out, ",'%s',char(10))", zNL);
9156     }
9157   }
9158   setTextMode(out, 1);
9159 }
9160
9161 /*
9162 ** Output the given string as a quoted according to C or TCL quoting rules.
9163 */
9164 static void output_c_string(FILE *out, const char *z){
9165   unsigned int c;
9166   fputc('"', out);
9167   while( (c = *(z++))!=0 ){
9168     if( c=='\\' ){
9169       fputc(c, out);
9170       fputc(c, out);
9171     }else if( c=='"' ){
9172       fputc('\\', out);
9173       fputc('"', out);
9174     }else if( c=='\t' ){
9175       fputc('\\', out);
9176       fputc('t', out);
9177     }else if( c=='\n' ){
9178       fputc('\\', out);
9179       fputc('n', out);
9180     }else if( c=='\r' ){
9181       fputc('\\', out);
9182       fputc('r', out);
9183     }else if( !isprint(c&0xff) ){
9184       raw_printf(out, "\\%03o", c&0xff);
9185     }else{
9186       fputc(c, out);
9187     }
9188   }
9189   fputc('"', out);
9190 }
9191
9192 /*
9193 ** Output the given string with characters that are special to
9194 ** HTML escaped.
9195 */
9196 static void output_html_string(FILE *out, const char *z){
9197   int i;
9198   if( z==0 ) z = "";
9199   while( *z ){
9200     for(i=0;   z[i]
9201             && z[i]!='<'
9202             && z[i]!='&'
9203             && z[i]!='>'
9204             && z[i]!='\"'
9205             && z[i]!='\'';
9206         i++){}
9207     if( i>0 ){
9208       utf8_printf(out,"%.*s",i,z);
9209     }
9210     if( z[i]=='<' ){
9211       raw_printf(out,"&lt;");
9212     }else if( z[i]=='&' ){
9213       raw_printf(out,"&amp;");
9214     }else if( z[i]=='>' ){
9215       raw_printf(out,"&gt;");
9216     }else if( z[i]=='\"' ){
9217       raw_printf(out,"&quot;");
9218     }else if( z[i]=='\'' ){
9219       raw_printf(out,"&#39;");
9220     }else{
9221       break;
9222     }
9223     z += i + 1;
9224   }
9225 }
9226
9227 /*
9228 ** If a field contains any character identified by a 1 in the following
9229 ** array, then the string must be quoted for CSV.
9230 */
9231 static const char needCsvQuote[] = {
9232   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9233   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9234   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
9235   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9236   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9237   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9238   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9239   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
9240   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9241   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9242   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9243   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9244   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9245   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9246   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9247   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9248 };
9249
9250 /*
9251 ** Output a single term of CSV.  Actually, p->colSeparator is used for
9252 ** the separator, which may or may not be a comma.  p->nullValue is
9253 ** the null value.  Strings are quoted if necessary.  The separator
9254 ** is only issued if bSep is true.
9255 */
9256 static void output_csv(ShellState *p, const char *z, int bSep){
9257   FILE *out = p->out;
9258   if( z==0 ){
9259     utf8_printf(out,"%s",p->nullValue);
9260   }else{
9261     int i;
9262     int nSep = strlen30(p->colSeparator);
9263     for(i=0; z[i]; i++){
9264       if( needCsvQuote[((unsigned char*)z)[i]]
9265          || (z[i]==p->colSeparator[0] &&
9266              (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
9267         i = 0;
9268         break;
9269       }
9270     }
9271     if( i==0 ){
9272       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
9273       utf8_printf(out, "%s", zQuoted);
9274       sqlite3_free(zQuoted);
9275     }else{
9276       utf8_printf(out, "%s", z);
9277     }
9278   }
9279   if( bSep ){
9280     utf8_printf(p->out, "%s", p->colSeparator);
9281   }
9282 }
9283
9284 /*
9285 ** This routine runs when the user presses Ctrl-C
9286 */
9287 static void interrupt_handler(int NotUsed){
9288   UNUSED_PARAMETER(NotUsed);
9289   seenInterrupt++;
9290   if( seenInterrupt>2 ) exit(1);
9291   if( globalDb ) sqlite3_interrupt(globalDb);
9292 }
9293
9294 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
9295 /*
9296 ** This routine runs for console events (e.g. Ctrl-C) on Win32
9297 */
9298 static BOOL WINAPI ConsoleCtrlHandler(
9299   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
9300 ){
9301   if( dwCtrlType==CTRL_C_EVENT ){
9302     interrupt_handler(0);
9303     return TRUE;
9304   }
9305   return FALSE;
9306 }
9307 #endif
9308
9309 #ifndef SQLITE_OMIT_AUTHORIZATION
9310 /*
9311 ** When the ".auth ON" is set, the following authorizer callback is
9312 ** invoked.  It always returns SQLITE_OK.
9313 */
9314 static int shellAuth(
9315   void *pClientData,
9316   int op,
9317   const char *zA1,
9318   const char *zA2,
9319   const char *zA3,
9320   const char *zA4
9321 ){
9322   ShellState *p = (ShellState*)pClientData;
9323   static const char *azAction[] = { 0,
9324      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
9325      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
9326      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
9327      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
9328      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
9329      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
9330      "PRAGMA",               "READ",                 "SELECT",
9331      "TRANSACTION",          "UPDATE",               "ATTACH",
9332      "DETACH",               "ALTER_TABLE",          "REINDEX",
9333      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
9334      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
9335   };
9336   int i;
9337   const char *az[4];
9338   az[0] = zA1;
9339   az[1] = zA2;
9340   az[2] = zA3;
9341   az[3] = zA4;
9342   utf8_printf(p->out, "authorizer: %s", azAction[op]);
9343   for(i=0; i<4; i++){
9344     raw_printf(p->out, " ");
9345     if( az[i] ){
9346       output_c_string(p->out, az[i]);
9347     }else{
9348       raw_printf(p->out, "NULL");
9349     }
9350   }
9351   raw_printf(p->out, "\n");
9352   return SQLITE_OK;
9353 }
9354 #endif
9355
9356 /*
9357 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
9358 **
9359 ** This routine converts some CREATE TABLE statements for shadow tables
9360 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
9361 */
9362 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
9363   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
9364     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
9365   }else{
9366     utf8_printf(out, "%s%s", z, zTail);
9367   }
9368 }
9369 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
9370   char c = z[n];
9371   z[n] = 0;
9372   printSchemaLine(out, z, zTail);
9373   z[n] = c;
9374 }
9375
9376 /*
9377 ** Return true if string z[] has nothing but whitespace and comments to the
9378 ** end of the first line.
9379 */
9380 static int wsToEol(const char *z){
9381   int i;
9382   for(i=0; z[i]; i++){
9383     if( z[i]=='\n' ) return 1;
9384     if( IsSpace(z[i]) ) continue;
9385     if( z[i]=='-' && z[i+1]=='-' ) return 1;
9386     return 0;
9387   }
9388   return 1;
9389 }
9390
9391 /*
9392 ** Add a new entry to the EXPLAIN QUERY PLAN data
9393 */
9394 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
9395   EQPGraphRow *pNew;
9396   int nText = strlen30(zText);
9397   if( p->autoEQPtest ){
9398     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
9399   }
9400   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
9401   if( pNew==0 ) shell_out_of_memory();
9402   pNew->iEqpId = iEqpId;
9403   pNew->iParentId = p2;
9404   memcpy(pNew->zText, zText, nText+1);
9405   pNew->pNext = 0;
9406   if( p->sGraph.pLast ){
9407     p->sGraph.pLast->pNext = pNew;
9408   }else{
9409     p->sGraph.pRow = pNew;
9410   }
9411   p->sGraph.pLast = pNew;
9412 }
9413
9414 /*
9415 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
9416 ** in p->sGraph.
9417 */
9418 static void eqp_reset(ShellState *p){
9419   EQPGraphRow *pRow, *pNext;
9420   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
9421     pNext = pRow->pNext;
9422     sqlite3_free(pRow);
9423   }
9424   memset(&p->sGraph, 0, sizeof(p->sGraph));
9425 }
9426
9427 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
9428 ** pOld, or return the first such line if pOld is NULL
9429 */
9430 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
9431   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9432   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
9433   return pRow;
9434 }
9435
9436 /* Render a single level of the graph that has iEqpId as its parent.  Called
9437 ** recursively to render sublevels.
9438 */
9439 static void eqp_render_level(ShellState *p, int iEqpId){
9440   EQPGraphRow *pRow, *pNext;
9441   int n = strlen30(p->sGraph.zPrefix);
9442   char *z;
9443   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
9444     pNext = eqp_next_row(p, iEqpId, pRow);
9445     z = pRow->zText;
9446     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9447     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
9448       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
9449       eqp_render_level(p, pRow->iEqpId);
9450       p->sGraph.zPrefix[n] = 0;
9451     }
9452   }
9453 }
9454
9455 /*
9456 ** Display and reset the EXPLAIN QUERY PLAN data
9457 */
9458 static void eqp_render(ShellState *p){
9459   EQPGraphRow *pRow = p->sGraph.pRow;
9460   if( pRow ){
9461     if( pRow->zText[0]=='-' ){
9462       if( pRow->pNext==0 ){
9463         eqp_reset(p);
9464         return;
9465       }
9466       utf8_printf(p->out, "%s\n", pRow->zText+3);
9467       p->sGraph.pRow = pRow->pNext;
9468       sqlite3_free(pRow);
9469     }else{
9470       utf8_printf(p->out, "QUERY PLAN\n");
9471     }
9472     p->sGraph.zPrefix[0] = 0;
9473     eqp_render_level(p, 0);
9474     eqp_reset(p);
9475   }
9476 }
9477
9478 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9479 /*
9480 ** Progress handler callback.
9481 */
9482 static int progress_handler(void *pClientData) {
9483   ShellState *p = (ShellState*)pClientData;
9484   p->nProgress++;
9485   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
9486     raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
9487     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
9488     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
9489     return 1;
9490   }
9491   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
9492     raw_printf(p->out, "Progress %u\n", p->nProgress);
9493   }
9494   return 0;
9495 }
9496 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9497
9498 /*
9499 ** This is the callback routine that the shell
9500 ** invokes for each row of a query result.
9501 */
9502 static int shell_callback(
9503   void *pArg,
9504   int nArg,        /* Number of result columns */
9505   char **azArg,    /* Text of each result column */
9506   char **azCol,    /* Column names */
9507   int *aiType      /* Column types */
9508 ){
9509   int i;
9510   ShellState *p = (ShellState*)pArg;
9511
9512   if( azArg==0 ) return 0;
9513   switch( p->cMode ){
9514     case MODE_Line: {
9515       int w = 5;
9516       if( azArg==0 ) break;
9517       for(i=0; i<nArg; i++){
9518         int len = strlen30(azCol[i] ? azCol[i] : "");
9519         if( len>w ) w = len;
9520       }
9521       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
9522       for(i=0; i<nArg; i++){
9523         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
9524                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
9525       }
9526       break;
9527     }
9528     case MODE_Explain:
9529     case MODE_Column: {
9530       static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
9531       const int *colWidth;
9532       int showHdr;
9533       char *rowSep;
9534       if( p->cMode==MODE_Column ){
9535         colWidth = p->colWidth;
9536         showHdr = p->showHeader;
9537         rowSep = p->rowSeparator;
9538       }else{
9539         colWidth = aExplainWidths;
9540         showHdr = 1;
9541         rowSep = SEP_Row;
9542       }
9543       if( p->cnt++==0 ){
9544         for(i=0; i<nArg; i++){
9545           int w, n;
9546           if( i<ArraySize(p->colWidth) ){
9547             w = colWidth[i];
9548           }else{
9549             w = 0;
9550           }
9551           if( w==0 ){
9552             w = strlenChar(azCol[i] ? azCol[i] : "");
9553             if( w<10 ) w = 10;
9554             n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
9555             if( w<n ) w = n;
9556           }
9557           if( i<ArraySize(p->actualWidth) ){
9558             p->actualWidth[i] = w;
9559           }
9560           if( showHdr ){
9561             utf8_width_print(p->out, w, azCol[i]);
9562             utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
9563           }
9564         }
9565         if( showHdr ){
9566           for(i=0; i<nArg; i++){
9567             int w;
9568             if( i<ArraySize(p->actualWidth) ){
9569                w = p->actualWidth[i];
9570                if( w<0 ) w = -w;
9571             }else{
9572                w = 10;
9573             }
9574             utf8_printf(p->out,"%-*.*s%s",w,w,
9575                    "----------------------------------------------------------"
9576                    "----------------------------------------------------------",
9577                     i==nArg-1 ? rowSep : "  ");
9578           }
9579         }
9580       }
9581       if( azArg==0 ) break;
9582       for(i=0; i<nArg; i++){
9583         int w;
9584         if( i<ArraySize(p->actualWidth) ){
9585            w = p->actualWidth[i];
9586         }else{
9587            w = 10;
9588         }
9589         if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
9590           w = strlenChar(azArg[i]);
9591         }
9592         if( i==1 && p->aiIndent && p->pStmt ){
9593           if( p->iIndent<p->nIndent ){
9594             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
9595           }
9596           p->iIndent++;
9597         }
9598         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
9599         utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
9600       }
9601       break;
9602     }
9603     case MODE_Semi: {   /* .schema and .fullschema output */
9604       printSchemaLine(p->out, azArg[0], ";\n");
9605       break;
9606     }
9607     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
9608       char *z;
9609       int j;
9610       int nParen = 0;
9611       char cEnd = 0;
9612       char c;
9613       int nLine = 0;
9614       assert( nArg==1 );
9615       if( azArg[0]==0 ) break;
9616       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
9617        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
9618       ){
9619         utf8_printf(p->out, "%s;\n", azArg[0]);
9620         break;
9621       }
9622       z = sqlite3_mprintf("%s", azArg[0]);
9623       j = 0;
9624       for(i=0; IsSpace(z[i]); i++){}
9625       for(; (c = z[i])!=0; i++){
9626         if( IsSpace(c) ){
9627           if( z[j-1]=='\r' ) z[j-1] = '\n';
9628           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
9629         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
9630           j--;
9631         }
9632         z[j++] = c;
9633       }
9634       while( j>0 && IsSpace(z[j-1]) ){ j--; }
9635       z[j] = 0;
9636       if( strlen30(z)>=79 ){
9637         for(i=j=0; (c = z[i])!=0; i++){  /* Copy changes from z[i] back to z[j] */
9638           if( c==cEnd ){
9639             cEnd = 0;
9640           }else if( c=='"' || c=='\'' || c=='`' ){
9641             cEnd = c;
9642           }else if( c=='[' ){
9643             cEnd = ']';
9644           }else if( c=='-' && z[i+1]=='-' ){
9645             cEnd = '\n';
9646           }else if( c=='(' ){
9647             nParen++;
9648           }else if( c==')' ){
9649             nParen--;
9650             if( nLine>0 && nParen==0 && j>0 ){
9651               printSchemaLineN(p->out, z, j, "\n");
9652               j = 0;
9653             }
9654           }
9655           z[j++] = c;
9656           if( nParen==1 && cEnd==0
9657            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
9658           ){
9659             if( c=='\n' ) j--;
9660             printSchemaLineN(p->out, z, j, "\n  ");
9661             j = 0;
9662             nLine++;
9663             while( IsSpace(z[i+1]) ){ i++; }
9664           }
9665         }
9666         z[j] = 0;
9667       }
9668       printSchemaLine(p->out, z, ";\n");
9669       sqlite3_free(z);
9670       break;
9671     }
9672     case MODE_List: {
9673       if( p->cnt++==0 && p->showHeader ){
9674         for(i=0; i<nArg; i++){
9675           utf8_printf(p->out,"%s%s",azCol[i],
9676                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
9677         }
9678       }
9679       if( azArg==0 ) break;
9680       for(i=0; i<nArg; i++){
9681         char *z = azArg[i];
9682         if( z==0 ) z = p->nullValue;
9683         utf8_printf(p->out, "%s", z);
9684         if( i<nArg-1 ){
9685           utf8_printf(p->out, "%s", p->colSeparator);
9686         }else{
9687           utf8_printf(p->out, "%s", p->rowSeparator);
9688         }
9689       }
9690       break;
9691     }
9692     case MODE_Html: {
9693       if( p->cnt++==0 && p->showHeader ){
9694         raw_printf(p->out,"<TR>");
9695         for(i=0; i<nArg; i++){
9696           raw_printf(p->out,"<TH>");
9697           output_html_string(p->out, azCol[i]);
9698           raw_printf(p->out,"</TH>\n");
9699         }
9700         raw_printf(p->out,"</TR>\n");
9701       }
9702       if( azArg==0 ) break;
9703       raw_printf(p->out,"<TR>");
9704       for(i=0; i<nArg; i++){
9705         raw_printf(p->out,"<TD>");
9706         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9707         raw_printf(p->out,"</TD>\n");
9708       }
9709       raw_printf(p->out,"</TR>\n");
9710       break;
9711     }
9712     case MODE_Tcl: {
9713       if( p->cnt++==0 && p->showHeader ){
9714         for(i=0; i<nArg; i++){
9715           output_c_string(p->out,azCol[i] ? azCol[i] : "");
9716           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9717         }
9718         utf8_printf(p->out, "%s", p->rowSeparator);
9719       }
9720       if( azArg==0 ) break;
9721       for(i=0; i<nArg; i++){
9722         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9723         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9724       }
9725       utf8_printf(p->out, "%s", p->rowSeparator);
9726       break;
9727     }
9728     case MODE_Csv: {
9729       setBinaryMode(p->out, 1);
9730       if( p->cnt++==0 && p->showHeader ){
9731         for(i=0; i<nArg; i++){
9732           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
9733         }
9734         utf8_printf(p->out, "%s", p->rowSeparator);
9735       }
9736       if( nArg>0 ){
9737         for(i=0; i<nArg; i++){
9738           output_csv(p, azArg[i], i<nArg-1);
9739         }
9740         utf8_printf(p->out, "%s", p->rowSeparator);
9741       }
9742       setTextMode(p->out, 1);
9743       break;
9744     }
9745     case MODE_Insert: {
9746       if( azArg==0 ) break;
9747       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
9748       if( p->showHeader ){
9749         raw_printf(p->out,"(");
9750         for(i=0; i<nArg; i++){
9751           if( i>0 ) raw_printf(p->out, ",");
9752           if( quoteChar(azCol[i]) ){
9753             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
9754             utf8_printf(p->out, "%s", z);
9755             sqlite3_free(z);
9756           }else{
9757             raw_printf(p->out, "%s", azCol[i]);
9758           }
9759         }
9760         raw_printf(p->out,")");
9761       }
9762       p->cnt++;
9763       for(i=0; i<nArg; i++){
9764         raw_printf(p->out, i>0 ? "," : " VALUES(");
9765         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9766           utf8_printf(p->out,"NULL");
9767         }else if( aiType && aiType[i]==SQLITE_TEXT ){
9768           if( ShellHasFlag(p, SHFLG_Newlines) ){
9769             output_quoted_string(p->out, azArg[i]);
9770           }else{
9771             output_quoted_escaped_string(p->out, azArg[i]);
9772           }
9773         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9774           utf8_printf(p->out,"%s", azArg[i]);
9775         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9776           char z[50];
9777           double r = sqlite3_column_double(p->pStmt, i);
9778           sqlite3_uint64 ur;
9779           memcpy(&ur,&r,sizeof(r));
9780           if( ur==0x7ff0000000000000LL ){
9781             raw_printf(p->out, "1e999");
9782           }else if( ur==0xfff0000000000000LL ){
9783             raw_printf(p->out, "-1e999");
9784           }else{
9785             sqlite3_snprintf(50,z,"%!.20g", r);
9786             raw_printf(p->out, "%s", z);
9787           }
9788         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9789           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9790           int nBlob = sqlite3_column_bytes(p->pStmt, i);
9791           output_hex_blob(p->out, pBlob, nBlob);
9792         }else if( isNumber(azArg[i], 0) ){
9793           utf8_printf(p->out,"%s", azArg[i]);
9794         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
9795           output_quoted_string(p->out, azArg[i]);
9796         }else{
9797           output_quoted_escaped_string(p->out, azArg[i]);
9798         }
9799       }
9800       raw_printf(p->out,");\n");
9801       break;
9802     }
9803     case MODE_Quote: {
9804       if( azArg==0 ) break;
9805       if( p->cnt==0 && p->showHeader ){
9806         for(i=0; i<nArg; i++){
9807           if( i>0 ) raw_printf(p->out, ",");
9808           output_quoted_string(p->out, azCol[i]);
9809         }
9810         raw_printf(p->out,"\n");
9811       }
9812       p->cnt++;
9813       for(i=0; i<nArg; i++){
9814         if( i>0 ) raw_printf(p->out, ",");
9815         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9816           utf8_printf(p->out,"NULL");
9817         }else if( aiType && aiType[i]==SQLITE_TEXT ){
9818           output_quoted_string(p->out, azArg[i]);
9819         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9820           utf8_printf(p->out,"%s", azArg[i]);
9821         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9822           char z[50];
9823           double r = sqlite3_column_double(p->pStmt, i);
9824           sqlite3_snprintf(50,z,"%!.20g", r);
9825           raw_printf(p->out, "%s", z);
9826         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9827           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9828           int nBlob = sqlite3_column_bytes(p->pStmt, i);
9829           output_hex_blob(p->out, pBlob, nBlob);
9830         }else if( isNumber(azArg[i], 0) ){
9831           utf8_printf(p->out,"%s", azArg[i]);
9832         }else{
9833           output_quoted_string(p->out, azArg[i]);
9834         }
9835       }
9836       raw_printf(p->out,"\n");
9837       break;
9838     }
9839     case MODE_Ascii: {
9840       if( p->cnt++==0 && p->showHeader ){
9841         for(i=0; i<nArg; i++){
9842           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9843           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
9844         }
9845         utf8_printf(p->out, "%s", p->rowSeparator);
9846       }
9847       if( azArg==0 ) break;
9848       for(i=0; i<nArg; i++){
9849         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9850         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
9851       }
9852       utf8_printf(p->out, "%s", p->rowSeparator);
9853       break;
9854     }
9855     case MODE_EQP: {
9856       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
9857       break;
9858     }
9859   }
9860   return 0;
9861 }
9862
9863 /*
9864 ** This is the callback routine that the SQLite library
9865 ** invokes for each row of a query result.
9866 */
9867 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
9868   /* since we don't have type info, call the shell_callback with a NULL value */
9869   return shell_callback(pArg, nArg, azArg, azCol, NULL);
9870 }
9871
9872 /*
9873 ** This is the callback routine from sqlite3_exec() that appends all
9874 ** output onto the end of a ShellText object.
9875 */
9876 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
9877   ShellText *p = (ShellText*)pArg;
9878   int i;
9879   UNUSED_PARAMETER(az);
9880   if( azArg==0 ) return 0;
9881   if( p->n ) appendText(p, "|", 0);
9882   for(i=0; i<nArg; i++){
9883     if( i ) appendText(p, ",", 0);
9884     if( azArg[i] ) appendText(p, azArg[i], 0);
9885   }
9886   return 0;
9887 }
9888
9889 /*
9890 ** Generate an appropriate SELFTEST table in the main database.
9891 */
9892 static void createSelftestTable(ShellState *p){
9893   char *zErrMsg = 0;
9894   sqlite3_exec(p->db,
9895     "SAVEPOINT selftest_init;\n"
9896     "CREATE TABLE IF NOT EXISTS selftest(\n"
9897     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
9898     "  op TEXT,\n"                   /* Operator:  memo run */
9899     "  cmd TEXT,\n"                  /* Command text */
9900     "  ans TEXT\n"                   /* Desired answer */
9901     ");"
9902     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
9903     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
9904     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
9905     "         'memo','Tests generated by --init');\n"
9906     "INSERT INTO [_shell$self]\n"
9907     "  SELECT 'run',\n"
9908     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
9909                                  "FROM sqlite_master ORDER BY 2'',224))',\n"
9910     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
9911                           "FROM sqlite_master ORDER BY 2',224));\n"
9912     "INSERT INTO [_shell$self]\n"
9913     "  SELECT 'run',"
9914     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
9915     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
9916     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
9917     "  FROM (\n"
9918     "    SELECT name FROM sqlite_master\n"
9919     "     WHERE type='table'\n"
9920     "       AND name<>'selftest'\n"
9921     "       AND coalesce(rootpage,0)>0\n"
9922     "  )\n"
9923     " ORDER BY name;\n"
9924     "INSERT INTO [_shell$self]\n"
9925     "  VALUES('run','PRAGMA integrity_check','ok');\n"
9926     "INSERT INTO selftest(tno,op,cmd,ans)"
9927     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
9928     "DROP TABLE [_shell$self];"
9929     ,0,0,&zErrMsg);
9930   if( zErrMsg ){
9931     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
9932     sqlite3_free(zErrMsg);
9933   }
9934   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
9935 }
9936
9937
9938 /*
9939 ** Set the destination table field of the ShellState structure to
9940 ** the name of the table given.  Escape any quote characters in the
9941 ** table name.
9942 */
9943 static void set_table_name(ShellState *p, const char *zName){
9944   int i, n;
9945   char cQuote;
9946   char *z;
9947
9948   if( p->zDestTable ){
9949     free(p->zDestTable);
9950     p->zDestTable = 0;
9951   }
9952   if( zName==0 ) return;
9953   cQuote = quoteChar(zName);
9954   n = strlen30(zName);
9955   if( cQuote ) n += n+2;
9956   z = p->zDestTable = malloc( n+1 );
9957   if( z==0 ) shell_out_of_memory();
9958   n = 0;
9959   if( cQuote ) z[n++] = cQuote;
9960   for(i=0; zName[i]; i++){
9961     z[n++] = zName[i];
9962     if( zName[i]==cQuote ) z[n++] = cQuote;
9963   }
9964   if( cQuote ) z[n++] = cQuote;
9965   z[n] = 0;
9966 }
9967
9968
9969 /*
9970 ** Execute a query statement that will generate SQL output.  Print
9971 ** the result columns, comma-separated, on a line and then add a
9972 ** semicolon terminator to the end of that line.
9973 **
9974 ** If the number of columns is 1 and that column contains text "--"
9975 ** then write the semicolon on a separate line.  That way, if a
9976 ** "--" comment occurs at the end of the statement, the comment
9977 ** won't consume the semicolon terminator.
9978 */
9979 static int run_table_dump_query(
9980   ShellState *p,           /* Query context */
9981   const char *zSelect,     /* SELECT statement to extract content */
9982   const char *zFirstRow    /* Print before first row, if not NULL */
9983 ){
9984   sqlite3_stmt *pSelect;
9985   int rc;
9986   int nResult;
9987   int i;
9988   const char *z;
9989   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
9990   if( rc!=SQLITE_OK || !pSelect ){
9991     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9992                 sqlite3_errmsg(p->db));
9993     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9994     return rc;
9995   }
9996   rc = sqlite3_step(pSelect);
9997   nResult = sqlite3_column_count(pSelect);
9998   while( rc==SQLITE_ROW ){
9999     if( zFirstRow ){
10000       utf8_printf(p->out, "%s", zFirstRow);
10001       zFirstRow = 0;
10002     }
10003     z = (const char*)sqlite3_column_text(pSelect, 0);
10004     utf8_printf(p->out, "%s", z);
10005     for(i=1; i<nResult; i++){
10006       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
10007     }
10008     if( z==0 ) z = "";
10009     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
10010     if( z[0] ){
10011       raw_printf(p->out, "\n;\n");
10012     }else{
10013       raw_printf(p->out, ";\n");
10014     }
10015     rc = sqlite3_step(pSelect);
10016   }
10017   rc = sqlite3_finalize(pSelect);
10018   if( rc!=SQLITE_OK ){
10019     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
10020                 sqlite3_errmsg(p->db));
10021     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
10022   }
10023   return rc;
10024 }
10025
10026 /*
10027 ** Allocate space and save off current error string.
10028 */
10029 static char *save_err_msg(
10030   sqlite3 *db            /* Database to query */
10031 ){
10032   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
10033   char *zErrMsg = sqlite3_malloc64(nErrMsg);
10034   if( zErrMsg ){
10035     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
10036   }
10037   return zErrMsg;
10038 }
10039
10040 #ifdef __linux__
10041 /*
10042 ** Attempt to display I/O stats on Linux using /proc/PID/io
10043 */
10044 static void displayLinuxIoStats(FILE *out){
10045   FILE *in;
10046   char z[200];
10047   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
10048   in = fopen(z, "rb");
10049   if( in==0 ) return;
10050   while( fgets(z, sizeof(z), in)!=0 ){
10051     static const struct {
10052       const char *zPattern;
10053       const char *zDesc;
10054     } aTrans[] = {
10055       { "rchar: ",                  "Bytes received by read():" },
10056       { "wchar: ",                  "Bytes sent to write():"    },
10057       { "syscr: ",                  "Read() system calls:"      },
10058       { "syscw: ",                  "Write() system calls:"     },
10059       { "read_bytes: ",             "Bytes read from storage:"  },
10060       { "write_bytes: ",            "Bytes written to storage:" },
10061       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
10062     };
10063     int i;
10064     for(i=0; i<ArraySize(aTrans); i++){
10065       int n = strlen30(aTrans[i].zPattern);
10066       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
10067         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
10068         break;
10069       }
10070     }
10071   }
10072   fclose(in);
10073 }
10074 #endif
10075
10076 /*
10077 ** Display a single line of status using 64-bit values.
10078 */
10079 static void displayStatLine(
10080   ShellState *p,            /* The shell context */
10081   char *zLabel,             /* Label for this one line */
10082   char *zFormat,            /* Format for the result */
10083   int iStatusCtrl,          /* Which status to display */
10084   int bReset                /* True to reset the stats */
10085 ){
10086   sqlite3_int64 iCur = -1;
10087   sqlite3_int64 iHiwtr = -1;
10088   int i, nPercent;
10089   char zLine[200];
10090   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
10091   for(i=0, nPercent=0; zFormat[i]; i++){
10092     if( zFormat[i]=='%' ) nPercent++;
10093   }
10094   if( nPercent>1 ){
10095     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
10096   }else{
10097     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
10098   }
10099   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
10100 }
10101
10102 /*
10103 ** Display memory stats.
10104 */
10105 static int display_stats(
10106   sqlite3 *db,                /* Database to query */
10107   ShellState *pArg,           /* Pointer to ShellState */
10108   int bReset                  /* True to reset the stats */
10109 ){
10110   int iCur;
10111   int iHiwtr;
10112   FILE *out;
10113   if( pArg==0 || pArg->out==0 ) return 0;
10114   out = pArg->out;
10115
10116   if( pArg->pStmt && (pArg->statsOn & 2) ){
10117     int nCol, i, x;
10118     sqlite3_stmt *pStmt = pArg->pStmt;
10119     char z[100];
10120     nCol = sqlite3_column_count(pStmt);
10121     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
10122     for(i=0; i<nCol; i++){
10123       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
10124       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
10125 #ifndef SQLITE_OMIT_DECLTYPE
10126       sqlite3_snprintf(30, z+x, "declared type:");
10127       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
10128 #endif
10129 #ifdef SQLITE_ENABLE_COLUMN_METADATA
10130       sqlite3_snprintf(30, z+x, "database name:");
10131       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
10132       sqlite3_snprintf(30, z+x, "table name:");
10133       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
10134       sqlite3_snprintf(30, z+x, "origin name:");
10135       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
10136 #endif
10137     }
10138   }
10139
10140   displayStatLine(pArg, "Memory Used:",
10141      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
10142   displayStatLine(pArg, "Number of Outstanding Allocations:",
10143      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
10144   if( pArg->shellFlgs & SHFLG_Pagecache ){
10145     displayStatLine(pArg, "Number of Pcache Pages Used:",
10146        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
10147   }
10148   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
10149      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
10150   displayStatLine(pArg, "Largest Allocation:",
10151      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
10152   displayStatLine(pArg, "Largest Pcache Allocation:",
10153      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
10154 #ifdef YYTRACKMAXSTACKDEPTH
10155   displayStatLine(pArg, "Deepest Parser Stack:",
10156      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
10157 #endif
10158
10159   if( db ){
10160     if( pArg->shellFlgs & SHFLG_Lookaside ){
10161       iHiwtr = iCur = -1;
10162       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
10163                         &iCur, &iHiwtr, bReset);
10164       raw_printf(pArg->out,
10165               "Lookaside Slots Used:                %d (max %d)\n",
10166               iCur, iHiwtr);
10167       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
10168                         &iCur, &iHiwtr, bReset);
10169       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
10170               iHiwtr);
10171       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
10172                         &iCur, &iHiwtr, bReset);
10173       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
10174               iHiwtr);
10175       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
10176                         &iCur, &iHiwtr, bReset);
10177       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
10178               iHiwtr);
10179     }
10180     iHiwtr = iCur = -1;
10181     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
10182     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
10183             iCur);
10184     iHiwtr = iCur = -1;
10185     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
10186     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
10187     iHiwtr = iCur = -1;
10188     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
10189     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
10190     iHiwtr = iCur = -1;
10191     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
10192     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
10193     iHiwtr = iCur = -1;
10194     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
10195     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
10196     iHiwtr = iCur = -1;
10197     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
10198     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
10199             iCur);
10200     iHiwtr = iCur = -1;
10201     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
10202     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
10203             iCur);
10204   }
10205
10206   if( pArg->pStmt ){
10207     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
10208                                bReset);
10209     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
10210     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
10211     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
10212     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
10213     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
10214     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
10215     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
10216     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
10217     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
10218     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
10219     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
10220     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
10221     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
10222   }
10223
10224 #ifdef __linux__
10225   displayLinuxIoStats(pArg->out);
10226 #endif
10227
10228   /* Do not remove this machine readable comment: extra-stats-output-here */
10229
10230   return 0;
10231 }
10232
10233 /*
10234 ** Display scan stats.
10235 */
10236 static void display_scanstats(
10237   sqlite3 *db,                    /* Database to query */
10238   ShellState *pArg                /* Pointer to ShellState */
10239 ){
10240 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10241   UNUSED_PARAMETER(db);
10242   UNUSED_PARAMETER(pArg);
10243 #else
10244   int i, k, n, mx;
10245   raw_printf(pArg->out, "-------- scanstats --------\n");
10246   mx = 0;
10247   for(k=0; k<=mx; k++){
10248     double rEstLoop = 1.0;
10249     for(i=n=0; 1; i++){
10250       sqlite3_stmt *p = pArg->pStmt;
10251       sqlite3_int64 nLoop, nVisit;
10252       double rEst;
10253       int iSid;
10254       const char *zExplain;
10255       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
10256         break;
10257       }
10258       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
10259       if( iSid>mx ) mx = iSid;
10260       if( iSid!=k ) continue;
10261       if( n==0 ){
10262         rEstLoop = (double)nLoop;
10263         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
10264       }
10265       n++;
10266       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
10267       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
10268       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
10269       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
10270       rEstLoop *= rEst;
10271       raw_printf(pArg->out,
10272           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
10273           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
10274       );
10275     }
10276   }
10277   raw_printf(pArg->out, "---------------------------\n");
10278 #endif
10279 }
10280
10281 /*
10282 ** Parameter azArray points to a zero-terminated array of strings. zStr
10283 ** points to a single nul-terminated string. Return non-zero if zStr
10284 ** is equal, according to strcmp(), to any of the strings in the array.
10285 ** Otherwise, return zero.
10286 */
10287 static int str_in_array(const char *zStr, const char **azArray){
10288   int i;
10289   for(i=0; azArray[i]; i++){
10290     if( 0==strcmp(zStr, azArray[i]) ) return 1;
10291   }
10292   return 0;
10293 }
10294
10295 /*
10296 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
10297 ** and populate the ShellState.aiIndent[] array with the number of
10298 ** spaces each opcode should be indented before it is output.
10299 **
10300 ** The indenting rules are:
10301 **
10302 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
10303 **       all opcodes that occur between the p2 jump destination and the opcode
10304 **       itself by 2 spaces.
10305 **
10306 **     * For each "Goto", if the jump destination is earlier in the program
10307 **       and ends on one of:
10308 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
10309 **       or if the P1 parameter is one instead of zero,
10310 **       then indent all opcodes between the earlier instruction
10311 **       and "Goto" by 2 spaces.
10312 */
10313 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
10314   const char *zSql;               /* The text of the SQL statement */
10315   const char *z;                  /* Used to check if this is an EXPLAIN */
10316   int *abYield = 0;               /* True if op is an OP_Yield */
10317   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
10318   int iOp;                        /* Index of operation in p->aiIndent[] */
10319
10320   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
10321   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
10322                             "Rewind", 0 };
10323   const char *azGoto[] = { "Goto", 0 };
10324
10325   /* Try to figure out if this is really an EXPLAIN statement. If this
10326   ** cannot be verified, return early.  */
10327   if( sqlite3_column_count(pSql)!=8 ){
10328     p->cMode = p->mode;
10329     return;
10330   }
10331   zSql = sqlite3_sql(pSql);
10332   if( zSql==0 ) return;
10333   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
10334   if( sqlite3_strnicmp(z, "explain", 7) ){
10335     p->cMode = p->mode;
10336     return;
10337   }
10338
10339   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
10340     int i;
10341     int iAddr = sqlite3_column_int(pSql, 0);
10342     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
10343
10344     /* Set p2 to the P2 field of the current opcode. Then, assuming that
10345     ** p2 is an instruction address, set variable p2op to the index of that
10346     ** instruction in the aiIndent[] array. p2 and p2op may be different if
10347     ** the current instruction is part of a sub-program generated by an
10348     ** SQL trigger or foreign key.  */
10349     int p2 = sqlite3_column_int(pSql, 3);
10350     int p2op = (p2 + (iOp-iAddr));
10351
10352     /* Grow the p->aiIndent array as required */
10353     if( iOp>=nAlloc ){
10354       if( iOp==0 ){
10355         /* Do further verfication that this is explain output.  Abort if
10356         ** it is not */
10357         static const char *explainCols[] = {
10358            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
10359         int jj;
10360         for(jj=0; jj<ArraySize(explainCols); jj++){
10361           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
10362             p->cMode = p->mode;
10363             sqlite3_reset(pSql);
10364             return;
10365           }
10366         }
10367       }
10368       nAlloc += 100;
10369       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
10370       if( p->aiIndent==0 ) shell_out_of_memory();
10371       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
10372       if( abYield==0 ) shell_out_of_memory();
10373     }
10374     abYield[iOp] = str_in_array(zOp, azYield);
10375     p->aiIndent[iOp] = 0;
10376     p->nIndent = iOp+1;
10377
10378     if( str_in_array(zOp, azNext) ){
10379       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10380     }
10381     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
10382      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
10383     ){
10384       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10385     }
10386   }
10387
10388   p->iIndent = 0;
10389   sqlite3_free(abYield);
10390   sqlite3_reset(pSql);
10391 }
10392
10393 /*
10394 ** Free the array allocated by explain_data_prepare().
10395 */
10396 static void explain_data_delete(ShellState *p){
10397   sqlite3_free(p->aiIndent);
10398   p->aiIndent = 0;
10399   p->nIndent = 0;
10400   p->iIndent = 0;
10401 }
10402
10403 /*
10404 ** Disable and restore .wheretrace and .selecttrace settings.
10405 */
10406 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10407 extern int sqlite3SelectTrace;
10408 static int savedSelectTrace;
10409 #endif
10410 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10411 extern int sqlite3WhereTrace;
10412 static int savedWhereTrace;
10413 #endif
10414 static void disable_debug_trace_modes(void){
10415 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10416   savedSelectTrace = sqlite3SelectTrace;
10417   sqlite3SelectTrace = 0;
10418 #endif
10419 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10420   savedWhereTrace = sqlite3WhereTrace;
10421   sqlite3WhereTrace = 0;
10422 #endif
10423 }
10424 static void restore_debug_trace_modes(void){
10425 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10426   sqlite3SelectTrace = savedSelectTrace;
10427 #endif
10428 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10429   sqlite3WhereTrace = savedWhereTrace;
10430 #endif
10431 }
10432
10433 /* Create the TEMP table used to store parameter bindings */
10434 static void bind_table_init(ShellState *p){
10435   int wrSchema = 0;
10436   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
10437   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
10438   sqlite3_exec(p->db,
10439     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
10440     "  key TEXT PRIMARY KEY,\n"
10441     "  value ANY\n"
10442     ") WITHOUT ROWID;",
10443     0, 0, 0);
10444   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
10445 }
10446
10447 /*
10448 ** Bind parameters on a prepared statement.
10449 **
10450 ** Parameter bindings are taken from a TEMP table of the form:
10451 **
10452 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
10453 **    WITHOUT ROWID;
10454 **
10455 ** No bindings occur if this table does not exist.  The special character '$'
10456 ** is included in the table name to help prevent collisions with actual tables.
10457 ** The table must be in the TEMP schema.
10458 */
10459 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
10460   int nVar;
10461   int i;
10462   int rc;
10463   sqlite3_stmt *pQ = 0;
10464
10465   nVar = sqlite3_bind_parameter_count(pStmt);
10466   if( nVar==0 ) return;  /* Nothing to do */
10467   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
10468                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
10469     return; /* Parameter table does not exist */
10470   }
10471   rc = sqlite3_prepare_v2(pArg->db,
10472           "SELECT value FROM temp.sqlite_parameters"
10473           " WHERE key=?1", -1, &pQ, 0);
10474   if( rc || pQ==0 ) return;
10475   for(i=1; i<=nVar; i++){
10476     char zNum[30];
10477     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
10478     if( zVar==0 ){
10479       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
10480       zVar = zNum;
10481     }
10482     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
10483     if( sqlite3_step(pQ)==SQLITE_ROW ){
10484       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
10485     }else{
10486       sqlite3_bind_null(pStmt, i);
10487     }
10488     sqlite3_reset(pQ);
10489   }
10490   sqlite3_finalize(pQ);
10491 }
10492
10493 /*
10494 ** Run a prepared statement
10495 */
10496 static void exec_prepared_stmt(
10497   ShellState *pArg,                                /* Pointer to ShellState */
10498   sqlite3_stmt *pStmt                              /* Statment to run */
10499 ){
10500   int rc;
10501
10502   /* perform the first step.  this will tell us if we
10503   ** have a result set or not and how wide it is.
10504   */
10505   rc = sqlite3_step(pStmt);
10506   /* if we have a result set... */
10507   if( SQLITE_ROW == rc ){
10508     /* allocate space for col name ptr, value ptr, and type */
10509     int nCol = sqlite3_column_count(pStmt);
10510     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10511     if( !pData ){
10512       rc = SQLITE_NOMEM;
10513     }else{
10514       char **azCols = (char **)pData;      /* Names of result columns */
10515       char **azVals = &azCols[nCol];       /* Results */
10516       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10517       int i, x;
10518       assert(sizeof(int) <= sizeof(char *));
10519       /* save off ptrs to column names */
10520       for(i=0; i<nCol; i++){
10521         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10522       }
10523       do{
10524         /* extract the data and data types */
10525         for(i=0; i<nCol; i++){
10526           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10527           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10528             azVals[i] = "";
10529           }else{
10530             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10531           }
10532           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10533             rc = SQLITE_NOMEM;
10534             break; /* from for */
10535           }
10536         } /* end for */
10537
10538         /* if data and types extracted successfully... */
10539         if( SQLITE_ROW == rc ){
10540           /* call the supplied callback with the result row data */
10541           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10542             rc = SQLITE_ABORT;
10543           }else{
10544             rc = sqlite3_step(pStmt);
10545           }
10546         }
10547       } while( SQLITE_ROW == rc );
10548       sqlite3_free(pData);
10549     }
10550   }
10551 }
10552
10553 #ifndef SQLITE_OMIT_VIRTUALTABLE
10554 /*
10555 ** This function is called to process SQL if the previous shell command
10556 ** was ".expert". It passes the SQL in the second argument directly to
10557 ** the sqlite3expert object.
10558 **
10559 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10560 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10561 ** an English language error message. It is the responsibility of the
10562 ** caller to eventually free this buffer using sqlite3_free().
10563 */
10564 static int expertHandleSQL(
10565   ShellState *pState, 
10566   const char *zSql, 
10567   char **pzErr
10568 ){
10569   assert( pState->expert.pExpert );
10570   assert( pzErr==0 || *pzErr==0 );
10571   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
10572 }
10573
10574 /*
10575 ** This function is called either to silently clean up the object
10576 ** created by the ".expert" command (if bCancel==1), or to generate a 
10577 ** report from it and then clean it up (if bCancel==0).
10578 **
10579 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10580 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10581 ** an English language error message. It is the responsibility of the
10582 ** caller to eventually free this buffer using sqlite3_free().
10583 */
10584 static int expertFinish(
10585   ShellState *pState,
10586   int bCancel,
10587   char **pzErr
10588 ){
10589   int rc = SQLITE_OK;
10590   sqlite3expert *p = pState->expert.pExpert;
10591   assert( p );
10592   assert( bCancel || pzErr==0 || *pzErr==0 );
10593   if( bCancel==0 ){
10594     FILE *out = pState->out;
10595     int bVerbose = pState->expert.bVerbose;
10596
10597     rc = sqlite3_expert_analyze(p, pzErr);
10598     if( rc==SQLITE_OK ){
10599       int nQuery = sqlite3_expert_count(p);
10600       int i;
10601
10602       if( bVerbose ){
10603         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
10604         raw_printf(out, "-- Candidates -----------------------------\n");
10605         raw_printf(out, "%s\n", zCand);
10606       }
10607       for(i=0; i<nQuery; i++){
10608         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
10609         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
10610         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
10611         if( zIdx==0 ) zIdx = "(no new indexes)\n";
10612         if( bVerbose ){
10613           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
10614           raw_printf(out, "%s\n\n", zSql);
10615         }
10616         raw_printf(out, "%s\n", zIdx);
10617         raw_printf(out, "%s\n", zEQP);
10618       }
10619     }
10620   }
10621   sqlite3_expert_destroy(p);
10622   pState->expert.pExpert = 0;
10623   return rc;
10624 }
10625
10626 /*
10627 ** Implementation of ".expert" dot command.
10628 */
10629 static int expertDotCommand(
10630   ShellState *pState,             /* Current shell tool state */
10631   char **azArg,                   /* Array of arguments passed to dot command */
10632   int nArg                        /* Number of entries in azArg[] */
10633 ){
10634   int rc = SQLITE_OK;
10635   char *zErr = 0;
10636   int i;
10637   int iSample = 0;
10638
10639   assert( pState->expert.pExpert==0 );
10640   memset(&pState->expert, 0, sizeof(ExpertInfo));
10641
10642   for(i=1; rc==SQLITE_OK && i<nArg; i++){
10643     char *z = azArg[i];
10644     int n;
10645     if( z[0]=='-' && z[1]=='-' ) z++;
10646     n = strlen30(z);
10647     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
10648       pState->expert.bVerbose = 1;
10649     }
10650     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
10651       if( i==(nArg-1) ){
10652         raw_printf(stderr, "option requires an argument: %s\n", z);
10653         rc = SQLITE_ERROR;
10654       }else{
10655         iSample = (int)integerValue(azArg[++i]);
10656         if( iSample<0 || iSample>100 ){
10657           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
10658           rc = SQLITE_ERROR;
10659         }
10660       }
10661     }
10662     else{
10663       raw_printf(stderr, "unknown option: %s\n", z);
10664       rc = SQLITE_ERROR;
10665     }
10666   }
10667
10668   if( rc==SQLITE_OK ){
10669     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
10670     if( pState->expert.pExpert==0 ){
10671       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
10672       rc = SQLITE_ERROR;
10673     }else{
10674       sqlite3_expert_config(
10675           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
10676       );
10677     }
10678   }
10679
10680   return rc;
10681 }
10682 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10683
10684 /*
10685 ** Execute a statement or set of statements.  Print
10686 ** any result rows/columns depending on the current mode
10687 ** set via the supplied callback.
10688 **
10689 ** This is very similar to SQLite's built-in sqlite3_exec()
10690 ** function except it takes a slightly different callback
10691 ** and callback data argument.
10692 */
10693 static int shell_exec(
10694   ShellState *pArg,                         /* Pointer to ShellState */
10695   const char *zSql,                         /* SQL to be evaluated */
10696   char **pzErrMsg                           /* Error msg written here */
10697 ){
10698   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
10699   int rc = SQLITE_OK;             /* Return Code */
10700   int rc2;
10701   const char *zLeftover;          /* Tail of unprocessed SQL */
10702   sqlite3 *db = pArg->db;
10703
10704   if( pzErrMsg ){
10705     *pzErrMsg = NULL;
10706   }
10707
10708 #ifndef SQLITE_OMIT_VIRTUALTABLE
10709   if( pArg->expert.pExpert ){
10710     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
10711     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
10712   }
10713 #endif
10714
10715   while( zSql[0] && (SQLITE_OK == rc) ){
10716     static const char *zStmtSql;
10717     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
10718     if( SQLITE_OK != rc ){
10719       if( pzErrMsg ){
10720         *pzErrMsg = save_err_msg(db);
10721       }
10722     }else{
10723       if( !pStmt ){
10724         /* this happens for a comment or white-space */
10725         zSql = zLeftover;
10726         while( IsSpace(zSql[0]) ) zSql++;
10727         continue;
10728       }
10729       zStmtSql = sqlite3_sql(pStmt);
10730       if( zStmtSql==0 ) zStmtSql = "";
10731       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
10732
10733       /* save off the prepared statment handle and reset row count */
10734       if( pArg ){
10735         pArg->pStmt = pStmt;
10736         pArg->cnt = 0;
10737       }
10738
10739       /* echo the sql statement if echo on */
10740       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10741         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10742       }
10743
10744       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10745       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
10746         sqlite3_stmt *pExplain;
10747         char *zEQP;
10748         int triggerEQP = 0;
10749         disable_debug_trace_modes();
10750         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
10751         if( pArg->autoEQP>=AUTOEQP_trigger ){
10752           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
10753         }
10754         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10755         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10756         if( rc==SQLITE_OK ){
10757           while( sqlite3_step(pExplain)==SQLITE_ROW ){
10758             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10759             int iEqpId = sqlite3_column_int(pExplain, 0);
10760             int iParentId = sqlite3_column_int(pExplain, 1);
10761             if( zEQPLine[0]=='-' ) eqp_render(pArg);
10762             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10763           }
10764           eqp_render(pArg);
10765         }
10766         sqlite3_finalize(pExplain);
10767         sqlite3_free(zEQP);
10768         if( pArg->autoEQP>=AUTOEQP_full ){
10769           /* Also do an EXPLAIN for ".eqp full" mode */
10770           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10771           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10772           if( rc==SQLITE_OK ){
10773             pArg->cMode = MODE_Explain;
10774             explain_data_prepare(pArg, pExplain);
10775             exec_prepared_stmt(pArg, pExplain);
10776             explain_data_delete(pArg);
10777           }
10778           sqlite3_finalize(pExplain);
10779           sqlite3_free(zEQP);
10780         }
10781         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
10782           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
10783           /* Reprepare pStmt before reactiving trace modes */
10784           sqlite3_finalize(pStmt);
10785           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
10786           if( pArg ) pArg->pStmt = pStmt;
10787         }
10788         restore_debug_trace_modes();
10789       }
10790
10791       if( pArg ){
10792         pArg->cMode = pArg->mode;
10793         if( pArg->autoExplain ){
10794           if( sqlite3_stmt_isexplain(pStmt)==1 ){
10795             pArg->cMode = MODE_Explain;
10796           }
10797           if( sqlite3_stmt_isexplain(pStmt)==2 ){
10798             pArg->cMode = MODE_EQP;
10799           }
10800         }
10801
10802         /* If the shell is currently in ".explain" mode, gather the extra
10803         ** data required to add indents to the output.*/
10804         if( pArg->cMode==MODE_Explain ){
10805           explain_data_prepare(pArg, pStmt);
10806         }
10807       }
10808
10809       bind_prepared_stmt(pArg, pStmt);
10810       exec_prepared_stmt(pArg, pStmt);
10811       explain_data_delete(pArg);
10812       eqp_render(pArg);
10813
10814       /* print usage stats if stats on */
10815       if( pArg && pArg->statsOn ){
10816         display_stats(db, pArg, 0);
10817       }
10818
10819       /* print loop-counters if required */
10820       if( pArg && pArg->scanstatsOn ){
10821         display_scanstats(db, pArg);
10822       }
10823
10824       /* Finalize the statement just executed. If this fails, save a
10825       ** copy of the error message. Otherwise, set zSql to point to the
10826       ** next statement to execute. */
10827       rc2 = sqlite3_finalize(pStmt);
10828       if( rc!=SQLITE_NOMEM ) rc = rc2;
10829       if( rc==SQLITE_OK ){
10830         zSql = zLeftover;
10831         while( IsSpace(zSql[0]) ) zSql++;
10832       }else if( pzErrMsg ){
10833         *pzErrMsg = save_err_msg(db);
10834       }
10835
10836       /* clear saved stmt handle */
10837       if( pArg ){
10838         pArg->pStmt = NULL;
10839       }
10840     }
10841   } /* end while */
10842
10843   return rc;
10844 }
10845
10846 /*
10847 ** Release memory previously allocated by tableColumnList().
10848 */
10849 static void freeColumnList(char **azCol){
10850   int i;
10851   for(i=1; azCol[i]; i++){
10852     sqlite3_free(azCol[i]);
10853   }
10854   /* azCol[0] is a static string */
10855   sqlite3_free(azCol);
10856 }
10857
10858 /*
10859 ** Return a list of pointers to strings which are the names of all
10860 ** columns in table zTab.   The memory to hold the names is dynamically
10861 ** allocated and must be released by the caller using a subsequent call
10862 ** to freeColumnList().
10863 **
10864 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
10865 ** value that needs to be preserved, then azCol[0] is filled in with the
10866 ** name of the rowid column.
10867 **
10868 ** The first regular column in the table is azCol[1].  The list is terminated
10869 ** by an entry with azCol[i]==0.
10870 */
10871 static char **tableColumnList(ShellState *p, const char *zTab){
10872   char **azCol = 0;
10873   sqlite3_stmt *pStmt;
10874   char *zSql;
10875   int nCol = 0;
10876   int nAlloc = 0;
10877   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
10878   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
10879   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
10880   int rc;
10881
10882   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
10883   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10884   sqlite3_free(zSql);
10885   if( rc ) return 0;
10886   while( sqlite3_step(pStmt)==SQLITE_ROW ){
10887     if( nCol>=nAlloc-2 ){
10888       nAlloc = nAlloc*2 + nCol + 10;
10889       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
10890       if( azCol==0 ) shell_out_of_memory();
10891     }
10892     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
10893     if( sqlite3_column_int(pStmt, 5) ){
10894       nPK++;
10895       if( nPK==1
10896        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
10897                           "INTEGER")==0
10898       ){
10899         isIPK = 1;
10900       }else{
10901         isIPK = 0;
10902       }
10903     }
10904   }
10905   sqlite3_finalize(pStmt);
10906   if( azCol==0 ) return 0;
10907   azCol[0] = 0;
10908   azCol[nCol+1] = 0;
10909
10910   /* The decision of whether or not a rowid really needs to be preserved
10911   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
10912   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
10913   ** rowids on tables where the rowid is inaccessible because there are other
10914   ** columns in the table named "rowid", "_rowid_", and "oid".
10915   */
10916   if( preserveRowid && isIPK ){
10917     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
10918     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
10919     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
10920     ** ROWID aliases.  To distinguish these cases, check to see if
10921     ** there is a "pk" entry in "PRAGMA index_list".  There will be
10922     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
10923     */
10924     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
10925                            " WHERE origin='pk'", zTab);
10926     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10927     sqlite3_free(zSql);
10928     if( rc ){
10929       freeColumnList(azCol);
10930       return 0;
10931     }
10932     rc = sqlite3_step(pStmt);
10933     sqlite3_finalize(pStmt);
10934     preserveRowid = rc==SQLITE_ROW;
10935   }
10936   if( preserveRowid ){
10937     /* Only preserve the rowid if we can find a name to use for the
10938     ** rowid */
10939     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
10940     int i, j;
10941     for(j=0; j<3; j++){
10942       for(i=1; i<=nCol; i++){
10943         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
10944       }
10945       if( i>nCol ){
10946         /* At this point, we know that azRowid[j] is not the name of any
10947         ** ordinary column in the table.  Verify that azRowid[j] is a valid
10948         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
10949         ** tables will fail this last check */
10950         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
10951         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
10952         break;
10953       }
10954     }
10955   }
10956   return azCol;
10957 }
10958
10959 /*
10960 ** Toggle the reverse_unordered_selects setting.
10961 */
10962 static void toggleSelectOrder(sqlite3 *db){
10963   sqlite3_stmt *pStmt = 0;
10964   int iSetting = 0;
10965   char zStmt[100];
10966   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
10967   if( sqlite3_step(pStmt)==SQLITE_ROW ){
10968     iSetting = sqlite3_column_int(pStmt, 0);
10969   }
10970   sqlite3_finalize(pStmt);
10971   sqlite3_snprintf(sizeof(zStmt), zStmt,
10972        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
10973   sqlite3_exec(db, zStmt, 0, 0, 0);
10974 }
10975
10976 /*
10977 ** This is a different callback routine used for dumping the database.
10978 ** Each row received by this callback consists of a table name,
10979 ** the table type ("index" or "table") and SQL to create the table.
10980 ** This routine should print text sufficient to recreate the table.
10981 */
10982 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
10983   int rc;
10984   const char *zTable;
10985   const char *zType;
10986   const char *zSql;
10987   ShellState *p = (ShellState *)pArg;
10988
10989   UNUSED_PARAMETER(azNotUsed);
10990   if( nArg!=3 || azArg==0 ) return 0;
10991   zTable = azArg[0];
10992   zType = azArg[1];
10993   zSql = azArg[2];
10994
10995   if( strcmp(zTable, "sqlite_sequence")==0 ){
10996     raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
10997   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
10998     raw_printf(p->out, "ANALYZE sqlite_master;\n");
10999   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
11000     return 0;
11001   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
11002     char *zIns;
11003     if( !p->writableSchema ){
11004       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
11005       p->writableSchema = 1;
11006     }
11007     zIns = sqlite3_mprintf(
11008        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
11009        "VALUES('table','%q','%q',0,'%q');",
11010        zTable, zTable, zSql);
11011     utf8_printf(p->out, "%s\n", zIns);
11012     sqlite3_free(zIns);
11013     return 0;
11014   }else{
11015     printSchemaLine(p->out, zSql, ";\n");
11016   }
11017
11018   if( strcmp(zType, "table")==0 ){
11019     ShellText sSelect;
11020     ShellText sTable;
11021     char **azCol;
11022     int i;
11023     char *savedDestTable;
11024     int savedMode;
11025
11026     azCol = tableColumnList(p, zTable);
11027     if( azCol==0 ){
11028       p->nErr++;
11029       return 0;
11030     }
11031
11032     /* Always quote the table name, even if it appears to be pure ascii,
11033     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
11034     initText(&sTable);
11035     appendText(&sTable, zTable, quoteChar(zTable));
11036     /* If preserving the rowid, add a column list after the table name.
11037     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
11038     ** instead of the usual "INSERT INTO tab VALUES(...)".
11039     */
11040     if( azCol[0] ){
11041       appendText(&sTable, "(", 0);
11042       appendText(&sTable, azCol[0], 0);
11043       for(i=1; azCol[i]; i++){
11044         appendText(&sTable, ",", 0);
11045         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
11046       }
11047       appendText(&sTable, ")", 0);
11048     }
11049
11050     /* Build an appropriate SELECT statement */
11051     initText(&sSelect);
11052     appendText(&sSelect, "SELECT ", 0);
11053     if( azCol[0] ){
11054       appendText(&sSelect, azCol[0], 0);
11055       appendText(&sSelect, ",", 0);
11056     }
11057     for(i=1; azCol[i]; i++){
11058       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
11059       if( azCol[i+1] ){
11060         appendText(&sSelect, ",", 0);
11061       }
11062     }
11063     freeColumnList(azCol);
11064     appendText(&sSelect, " FROM ", 0);
11065     appendText(&sSelect, zTable, quoteChar(zTable));
11066
11067     savedDestTable = p->zDestTable;
11068     savedMode = p->mode;
11069     p->zDestTable = sTable.z;
11070     p->mode = p->cMode = MODE_Insert;
11071     rc = shell_exec(p, sSelect.z, 0);
11072     if( (rc&0xff)==SQLITE_CORRUPT ){
11073       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11074       toggleSelectOrder(p->db);
11075       shell_exec(p, sSelect.z, 0);
11076       toggleSelectOrder(p->db);
11077     }
11078     p->zDestTable = savedDestTable;
11079     p->mode = savedMode;
11080     freeText(&sTable);
11081     freeText(&sSelect);
11082     if( rc ) p->nErr++;
11083   }
11084   return 0;
11085 }
11086
11087 /*
11088 ** Run zQuery.  Use dump_callback() as the callback routine so that
11089 ** the contents of the query are output as SQL statements.
11090 **
11091 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
11092 ** "ORDER BY rowid DESC" to the end.
11093 */
11094 static int run_schema_dump_query(
11095   ShellState *p,
11096   const char *zQuery
11097 ){
11098   int rc;
11099   char *zErr = 0;
11100   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
11101   if( rc==SQLITE_CORRUPT ){
11102     char *zQ2;
11103     int len = strlen30(zQuery);
11104     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11105     if( zErr ){
11106       utf8_printf(p->out, "/****** %s ******/\n", zErr);
11107       sqlite3_free(zErr);
11108       zErr = 0;
11109     }
11110     zQ2 = malloc( len+100 );
11111     if( zQ2==0 ) return rc;
11112     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
11113     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
11114     if( rc ){
11115       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
11116     }else{
11117       rc = SQLITE_CORRUPT;
11118     }
11119     sqlite3_free(zErr);
11120     free(zQ2);
11121   }
11122   return rc;
11123 }
11124
11125 /*
11126 ** Text of help messages.
11127 **
11128 ** The help text for each individual command begins with a line that starts
11129 ** with ".".  Subsequent lines are supplimental information.
11130 **
11131 ** There must be two or more spaces between the end of the command and the
11132 ** start of the description of what that command does.
11133 */
11134 static const char *(azHelp[]) = {
11135 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11136   ".archive ...             Manage SQL archives",
11137   "   Each command must have exactly one of the following options:",
11138   "     -c, --create               Create a new archive",
11139   "     -u, --update               Add files or update files with changed mtime",
11140   "     -i, --insert               Like -u but always add even if mtime unchanged",
11141   "     -t, --list                 List contents of archive",
11142   "     -x, --extract              Extract files from archive",
11143   "   Optional arguments:",
11144   "     -v, --verbose              Print each filename as it is processed",
11145   "     -f FILE, --file FILE       Operate on archive FILE (default is current db)",
11146   "     -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS",
11147   "     -C DIR, --directory DIR    Change to directory DIR to read/extract files",
11148   "     -n, --dryrun               Show the SQL that would have occurred",
11149   "   Examples:",
11150   "     .ar -cf archive.sar foo bar  # Create archive.sar from files foo and bar",
11151   "     .ar -tf archive.sar          # List members of archive.sar",
11152   "     .ar -xvf archive.sar         # Verbosely extract files from archive.sar",
11153   "   See also:",
11154   "      http://sqlite.org/cli.html#sqlar_archive_support",
11155 #endif
11156 #ifndef SQLITE_OMIT_AUTHORIZATION
11157   ".auth ON|OFF             Show authorizer callbacks",
11158 #endif
11159   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
11160   "       --append            Use the appendvfs",
11161   "       --async             Write to FILE without a journal and without fsync()",
11162   ".bail on|off             Stop after hitting an error.  Default OFF",
11163   ".binary on|off           Turn binary output on or off.  Default OFF",
11164   ".cd DIRECTORY            Change the working directory to DIRECTORY",
11165   ".changes on|off          Show number of rows changed by SQL",
11166   ".check GLOB              Fail if output since .testcase does not match",
11167   ".clone NEWDB             Clone data into NEWDB from the existing database",
11168   ".databases               List names and files of attached databases",
11169   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
11170   ".dbinfo ?DB?             Show status information about the database",
11171   ".dump ?TABLE? ...        Render all database content as SQL",
11172   "   Options:",
11173   "     --preserve-rowids      Include ROWID values in the output",
11174   "     --newlines             Allow unescaped newline characters in output",
11175   "   TABLE is LIKE pattern for the tables to dump",
11176   ".echo on|off             Turn command echo on or off",
11177   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
11178   "   Other Modes:",
11179 #ifdef SQLITE_DEBUG
11180   "      test                  Show raw EXPLAIN QUERY PLAN output",
11181   "      trace                 Like \"full\" but also enable \"PRAGMA vdbe_trace\"",
11182 #endif
11183   "      trigger               Like \"full\" but also show trigger bytecode",
11184   ".excel                   Display the output of next command in a spreadsheet",
11185   ".exit ?CODE?             Exit this program with return-code CODE",
11186   ".expert                  EXPERIMENTAL. Suggest indexes for specified queries",
11187 /* Because explain mode comes on automatically now, the ".explain" mode
11188 ** is removed from the help screen.  It is still supported for legacy, however */
11189 /*".explain ?on|off|auto?   Turn EXPLAIN output mode on or off or to automatic",*/
11190   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
11191   ".headers on|off          Turn display of headers on or off",
11192   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
11193   ".import FILE TABLE       Import data from FILE into TABLE",
11194 #ifndef SQLITE_OMIT_TEST_CONTROL
11195   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
11196 #endif
11197   ".indexes ?TABLE?         Show names of indexes",
11198   "                           If TABLE is specified, only show indexes for",
11199   "                           tables matching TABLE using the LIKE operator.",
11200 #ifdef SQLITE_ENABLE_IOTRACE
11201   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
11202 #endif
11203   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
11204   ".lint OPTIONS            Report potential schema issues.",
11205   "     Options:",
11206   "        fkey-indexes     Find missing foreign key indexes",
11207 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11208   ".load FILE ?ENTRY?       Load an extension library",
11209 #endif
11210   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
11211   ".mode MODE ?TABLE?       Set output mode",
11212   "   MODE is one of:",
11213   "     ascii    Columns/rows delimited by 0x1F and 0x1E",
11214   "     csv      Comma-separated values",
11215   "     column   Left-aligned columns.  (See .width)",
11216   "     html     HTML <table> code",
11217   "     insert   SQL insert statements for TABLE",
11218   "     line     One value per line",
11219   "     list     Values delimited by \"|\"",
11220   "     quote    Escape answers as for SQL",
11221   "     tabs     Tab-separated values",
11222   "     tcl      TCL list elements",
11223   ".nullvalue STRING        Use STRING in place of NULL values",
11224   ".once (-e|-x|FILE)       Output for the next SQL command only to FILE",
11225   "     If FILE begins with '|' then open as a pipe",
11226   "     Other options:",
11227   "       -e    Invoke system text editor",
11228   "       -x    Open in a spreadsheet",
11229   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
11230   "     Options:",
11231   "        --append        Use appendvfs to append database to the end of FILE",
11232 #ifdef SQLITE_ENABLE_DESERIALIZE
11233   "        --deserialize   Load into memory useing sqlite3_deserialize()",
11234   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory database",
11235   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
11236 #endif
11237   "        --new           Initialize FILE to an empty database",
11238   "        --readonly      Open FILE readonly",
11239   "        --zip           FILE is a ZIP archive",
11240   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
11241   "     If FILE begins with '|' then open it as a pipe.",
11242   ".parameter CMD ...       Manage SQL parameter bindings",
11243   "   clear                   Erase all bindings",
11244   "   init                    Initialize the TEMP table that holds bindings",
11245   "   list                    List the current parameter bindings",
11246   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
11247   "                           PARAMETER should start with '$', ':', '@', or '?'",
11248   "   unset PARAMETER         Remove PARAMETER from the binding table",
11249   ".print STRING...         Print literal STRING",
11250 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11251   ".progress N              Invoke progress handler after every N opcodes",
11252   "   --limit N                 Interrupt after N progress callbacks",
11253   "   --once                    Do no more than one progress interrupt",
11254   "   --quiet|-q                No output except at interrupts",
11255   "   --reset                   Reset the count for each input and interrupt",
11256 #endif
11257   ".prompt MAIN CONTINUE    Replace the standard prompts",
11258   ".quit                    Exit this program",
11259   ".read FILE               Read input from FILE",
11260   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
11261   ".save FILE               Write in-memory database into FILE",
11262   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
11263   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
11264   "     Options:",
11265   "         --indent            Try to pretty-print the schema",
11266   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
11267   "    Options:",
11268   "       --init               Create a new SELFTEST table",
11269   "       -v                   Verbose output",
11270   ".separator COL ?ROW?     Change the column and row separators",
11271 #if defined(SQLITE_ENABLE_SESSION)
11272   ".session ?NAME? CMD ...  Create or control sessions",
11273   "   Subcommands:",
11274   "     attach TABLE             Attach TABLE",
11275   "     changeset FILE           Write a changeset into FILE",
11276   "     close                    Close one session",
11277   "     enable ?BOOLEAN?         Set or query the enable bit",
11278   "     filter GLOB...           Reject tables matching GLOBs",
11279   "     indirect ?BOOLEAN?       Mark or query the indirect status",
11280   "     isempty                  Query whether the session is empty",
11281   "     list                     List currently open session names",
11282   "     open DB NAME             Open a new session on DB",
11283   "     patchset FILE            Write a patchset into FILE",
11284   "   If ?NAME? is omitted, the first defined session is used.",
11285 #endif
11286   ".sha3sum ...             Compute a SHA3 hash of database content",
11287   "    Options:",
11288   "      --schema              Also hash the sqlite_master table",
11289   "      --sha3-224            Use the sha3-224 algorithm",
11290   "      --sha3-256            Use the sha3-256 algorithm.  This is the default.",
11291   "      --sha3-384            Use the sha3-384 algorithm",
11292   "      --sha3-512            Use the sha3-512 algorithm",
11293   "    Any other argument is a LIKE pattern for tables to hash",
11294 #ifndef SQLITE_NOHAVE_SYSTEM
11295   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
11296 #endif
11297   ".show                    Show the current values for various settings",
11298   ".stats ?on|off?          Show stats or turn stats on or off",
11299 #ifndef SQLITE_NOHAVE_SYSTEM
11300   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
11301 #endif
11302   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
11303   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
11304   ".timeout MS              Try opening locked tables for MS milliseconds",
11305   ".timer on|off            Turn SQL timer on or off",
11306 #ifndef SQLITE_OMIT_TRACE
11307   ".trace ?OPTIONS?         Output each SQL statement as it is run",
11308   "    FILE                    Send output to FILE",
11309   "    stdout                  Send output to stdout",
11310   "    stderr                  Send output to stderr",
11311   "    off                     Disable tracing",
11312   "    --expanded              Expand query parameters",
11313 #ifdef SQLITE_ENABLE_NORMALIZE
11314   "    --normalized            Normal the SQL statements",
11315 #endif
11316   "    --plain                 Show SQL as it is input",
11317   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
11318   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
11319   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
11320   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
11321 #endif /* SQLITE_OMIT_TRACE */
11322   ".vfsinfo ?AUX?           Information about the top-level VFS",
11323   ".vfslist                 List all available VFSes",
11324   ".vfsname ?AUX?           Print the name of the VFS stack",
11325   ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
11326   "     Negative values right-justify",
11327 };
11328
11329 /*
11330 ** Output help text.
11331 **
11332 ** zPattern describes the set of commands for which help text is provided.
11333 ** If zPattern is NULL, then show all commands, but only give a one-line
11334 ** description of each.
11335 **
11336 ** Return the number of matches.
11337 */
11338 static int showHelp(FILE *out, const char *zPattern){
11339   int i = 0;
11340   int j = 0;
11341   int n = 0;
11342   char *zPat;
11343   if( zPattern==0
11344    || zPattern[0]=='0'
11345    || strcmp(zPattern,"-a")==0
11346    || strcmp(zPattern,"-all")==0
11347   ){
11348     /* Show all commands, but only one line per command */
11349     if( zPattern==0 ) zPattern = "";
11350     for(i=0; i<ArraySize(azHelp); i++){
11351       if( azHelp[i][0]=='.' || zPattern[0] ){
11352         utf8_printf(out, "%s\n", azHelp[i]);
11353         n++;
11354       }
11355     }
11356   }else{
11357     /* Look for commands that for which zPattern is an exact prefix */
11358     zPat = sqlite3_mprintf(".%s*", zPattern);
11359     for(i=0; i<ArraySize(azHelp); i++){
11360       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
11361         utf8_printf(out, "%s\n", azHelp[i]);
11362         j = i+1;
11363         n++;
11364       }
11365     }
11366     sqlite3_free(zPat);
11367     if( n ){
11368       if( n==1 ){
11369         /* when zPattern is a prefix of exactly one command, then include the
11370         ** details of that command, which should begin at offset j */
11371         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
11372           utf8_printf(out, "%s\n", azHelp[j]);
11373           j++;
11374         }
11375       }
11376       return n;
11377     }
11378     /* Look for commands that contain zPattern anywhere.  Show the complete
11379     ** text of all commands that match. */
11380     zPat = sqlite3_mprintf("%%%s%%", zPattern);
11381     for(i=0; i<ArraySize(azHelp); i++){
11382       if( azHelp[i][0]=='.' ) j = i;
11383       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
11384         utf8_printf(out, "%s\n", azHelp[j]);
11385         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
11386           j++;
11387           utf8_printf(out, "%s\n", azHelp[j]);
11388         }
11389         i = j;
11390         n++;
11391       }
11392     }
11393     sqlite3_free(zPat);
11394   }
11395   return n;
11396 }
11397
11398 /* Forward reference */
11399 static int process_input(ShellState *p);
11400
11401 /*
11402 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
11403 ** and return a pointer to the buffer. The caller is responsible for freeing
11404 ** the memory.
11405 **
11406 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
11407 ** read.
11408 **
11409 ** For convenience, a nul-terminator byte is always appended to the data read
11410 ** from the file before the buffer is returned. This byte is not included in
11411 ** the final value of (*pnByte), if applicable.
11412 **
11413 ** NULL is returned if any error is encountered. The final value of *pnByte
11414 ** is undefined in this case.
11415 */
11416 static char *readFile(const char *zName, int *pnByte){
11417   FILE *in = fopen(zName, "rb");
11418   long nIn;
11419   size_t nRead;
11420   char *pBuf;
11421   if( in==0 ) return 0;
11422   fseek(in, 0, SEEK_END);
11423   nIn = ftell(in);
11424   rewind(in);
11425   pBuf = sqlite3_malloc64( nIn+1 );
11426   if( pBuf==0 ){ fclose(in); return 0; }
11427   nRead = fread(pBuf, nIn, 1, in);
11428   fclose(in);
11429   if( nRead!=1 ){
11430     sqlite3_free(pBuf);
11431     return 0;
11432   }
11433   pBuf[nIn] = 0;
11434   if( pnByte ) *pnByte = nIn;
11435   return pBuf;
11436 }
11437
11438 #if defined(SQLITE_ENABLE_SESSION)
11439 /*
11440 ** Close a single OpenSession object and release all of its associated
11441 ** resources.
11442 */
11443 static void session_close(OpenSession *pSession){
11444   int i;
11445   sqlite3session_delete(pSession->p);
11446   sqlite3_free(pSession->zName);
11447   for(i=0; i<pSession->nFilter; i++){
11448     sqlite3_free(pSession->azFilter[i]);
11449   }
11450   sqlite3_free(pSession->azFilter);
11451   memset(pSession, 0, sizeof(OpenSession));
11452 }
11453 #endif
11454
11455 /*
11456 ** Close all OpenSession objects and release all associated resources.
11457 */
11458 #if defined(SQLITE_ENABLE_SESSION)
11459 static void session_close_all(ShellState *p){
11460   int i;
11461   for(i=0; i<p->nSession; i++){
11462     session_close(&p->aSession[i]);
11463   }
11464   p->nSession = 0;
11465 }
11466 #else
11467 # define session_close_all(X)
11468 #endif
11469
11470 /*
11471 ** Implementation of the xFilter function for an open session.  Omit
11472 ** any tables named by ".session filter" but let all other table through.
11473 */
11474 #if defined(SQLITE_ENABLE_SESSION)
11475 static int session_filter(void *pCtx, const char *zTab){
11476   OpenSession *pSession = (OpenSession*)pCtx;
11477   int i;
11478   for(i=0; i<pSession->nFilter; i++){
11479     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
11480   }
11481   return 1;
11482 }
11483 #endif
11484
11485 /*
11486 ** Try to deduce the type of file for zName based on its content.  Return
11487 ** one of the SHELL_OPEN_* constants.
11488 **
11489 ** If the file does not exist or is empty but its name looks like a ZIP
11490 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
11491 ** Otherwise, assume an ordinary database regardless of the filename if
11492 ** the type cannot be determined from content.
11493 */
11494 int deduceDatabaseType(const char *zName, int dfltZip){
11495   FILE *f = fopen(zName, "rb");
11496   size_t n;
11497   int rc = SHELL_OPEN_UNSPEC;
11498   char zBuf[100];
11499   if( f==0 ){
11500     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11501        return SHELL_OPEN_ZIPFILE;
11502     }else{
11503        return SHELL_OPEN_NORMAL;
11504     }
11505   }
11506   n = fread(zBuf, 16, 1, f);
11507   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
11508     fclose(f);
11509     return SHELL_OPEN_NORMAL;
11510   }
11511   fseek(f, -25, SEEK_END);
11512   n = fread(zBuf, 25, 1, f);
11513   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11514     rc = SHELL_OPEN_APPENDVFS;
11515   }else{
11516     fseek(f, -22, SEEK_END);
11517     n = fread(zBuf, 22, 1, f);
11518     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11519        && zBuf[3]==0x06 ){
11520       rc = SHELL_OPEN_ZIPFILE;
11521     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11522       rc = SHELL_OPEN_ZIPFILE;
11523     }
11524   }
11525   fclose(f);
11526   return rc;  
11527 }
11528
11529 #ifdef SQLITE_ENABLE_DESERIALIZE
11530 /*
11531 ** Reconstruct an in-memory database using the output from the "dbtotxt"
11532 ** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
11533 ** is 0, then read from standard input.
11534 */
11535 static unsigned char *readHexDb(ShellState *p, int *pnData){
11536   unsigned char *a = 0;
11537   int nLine;
11538   int n = 0;
11539   int pgsz = 0;
11540   int iOffset = 0;
11541   int j, k;
11542   int rc;
11543   FILE *in;
11544   unsigned char x[16];
11545   char zLine[1000];
11546   if( p->zDbFilename ){
11547     in = fopen(p->zDbFilename, "r");
11548     if( in==0 ){
11549       utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
11550       return 0;
11551     }
11552     nLine = 0;
11553   }else{
11554     in = p->in;
11555     nLine = p->lineno;
11556   }
11557   *pnData = 0;
11558   nLine++;
11559   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
11560   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
11561   if( rc!=2 ) goto readHexDb_error;
11562   if( n<=0 ) goto readHexDb_error;
11563   a = sqlite3_malloc( n );
11564   if( a==0 ){
11565     utf8_printf(stderr, "Out of memory!\n");
11566     goto readHexDb_error;
11567   }
11568   memset(a, 0, n);
11569   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
11570     utf8_printf(stderr, "invalid pagesize\n");
11571     goto readHexDb_error;
11572   }
11573   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
11574     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
11575     if( rc==2 ){
11576       iOffset = k;
11577       continue;
11578     }
11579     if( strncmp(zLine, "| end ", 6)==0 ){
11580       break;
11581     }
11582     rc = sscanf(zLine,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
11583                       "  %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
11584                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
11585                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
11586     if( rc==17 ){
11587       k = iOffset+j;
11588       if( k+16<=n ){
11589         memcpy(a+k, x, 16);
11590       }
11591     }
11592   }
11593   *pnData = n;
11594   if( in!=p->in ){
11595     fclose(in);
11596   }else{
11597     p->lineno = nLine;
11598   }
11599   return a;
11600
11601 readHexDb_error:
11602   if( in!=stdin ){
11603     fclose(in);
11604   }else{
11605     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
11606       nLine++;
11607       if(strncmp(zLine, "| end ", 6)==0 ) break;
11608     }
11609     p->lineno = nLine;
11610   }
11611   sqlite3_free(a);
11612   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
11613   return 0;
11614 }
11615 #endif /* SQLITE_ENABLE_DESERIALIZE */
11616
11617 /* Flags for open_db().
11618 **
11619 ** The default behavior of open_db() is to exit(1) if the database fails to
11620 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11621 ** but still returns without calling exit.
11622 **
11623 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11624 ** ZIP archive if the file does not exist or is empty and its name matches
11625 ** the *.zip pattern.
11626 */
11627 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
11628 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
11629
11630 /*
11631 ** Make sure the database is open.  If it is not, then open it.  If
11632 ** the database fails to open, print an error message and exit.
11633 */
11634 static void open_db(ShellState *p, int openFlags){
11635   if( p->db==0 ){
11636     if( p->openMode==SHELL_OPEN_UNSPEC ){
11637       if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11638         p->openMode = SHELL_OPEN_NORMAL;
11639       }else{
11640         p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 
11641                              (openFlags & OPEN_DB_ZIPFILE)!=0);
11642       }
11643     }
11644     switch( p->openMode ){
11645       case SHELL_OPEN_APPENDVFS: {
11646         sqlite3_open_v2(p->zDbFilename, &p->db, 
11647            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
11648         break;
11649       }
11650       case SHELL_OPEN_HEXDB:
11651       case SHELL_OPEN_DESERIALIZE: {
11652         sqlite3_open(0, &p->db);
11653         break;
11654       }
11655       case SHELL_OPEN_ZIPFILE: {
11656         sqlite3_open(":memory:", &p->db);
11657         break;
11658       }
11659       case SHELL_OPEN_READONLY: {
11660         sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
11661         break;
11662       }
11663       case SHELL_OPEN_UNSPEC:
11664       case SHELL_OPEN_NORMAL: {
11665         sqlite3_open(p->zDbFilename, &p->db);
11666         break;
11667       }
11668     }
11669     globalDb = p->db;
11670     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11671       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11672           p->zDbFilename, sqlite3_errmsg(p->db));
11673       if( openFlags & OPEN_DB_KEEPALIVE ){
11674         sqlite3_open(":memory:", &p->db);
11675         return;
11676       }
11677       exit(1);
11678     }
11679 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11680     sqlite3_enable_load_extension(p->db, 1);
11681 #endif
11682     sqlite3_fileio_init(p->db, 0, 0);
11683     sqlite3_shathree_init(p->db, 0, 0);
11684     sqlite3_completion_init(p->db, 0, 0);
11685 #ifdef SQLITE_HAVE_ZLIB
11686     sqlite3_zipfile_init(p->db, 0, 0);
11687     sqlite3_sqlar_init(p->db, 0, 0);
11688 #endif
11689     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
11690                             shellAddSchemaName, 0, 0);
11691     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
11692                             shellModuleSchema, 0, 0);
11693     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
11694                             shellPutsFunc, 0, 0);
11695 #ifndef SQLITE_NOHAVE_SYSTEM
11696     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
11697                             editFunc, 0, 0);
11698     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
11699                             editFunc, 0, 0);
11700 #endif
11701     if( p->openMode==SHELL_OPEN_ZIPFILE ){
11702       char *zSql = sqlite3_mprintf(
11703          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
11704       sqlite3_exec(p->db, zSql, 0, 0, 0);
11705       sqlite3_free(zSql);
11706     }
11707 #ifdef SQLITE_ENABLE_DESERIALIZE
11708     else
11709     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
11710       int rc;
11711       int nData = 0;
11712       unsigned char *aData;
11713       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
11714         aData = (unsigned char*)readFile(p->zDbFilename, &nData);
11715       }else{
11716         aData = readHexDb(p, &nData);
11717         if( aData==0 ){
11718           utf8_printf(stderr, "Error in hexdb input\n");
11719           return;
11720         }
11721       }
11722       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
11723                    SQLITE_DESERIALIZE_RESIZEABLE |
11724                    SQLITE_DESERIALIZE_FREEONCLOSE);
11725       if( rc ){
11726         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
11727       }
11728       if( p->szMax>0 ){
11729         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
11730       }
11731     }
11732 #endif
11733   }
11734 }
11735
11736 /*
11737 ** Attempt to close the databaes connection.  Report errors.
11738 */
11739 void close_db(sqlite3 *db){
11740   int rc = sqlite3_close(db);
11741   if( rc ){
11742     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11743         rc, sqlite3_errmsg(db));
11744   } 
11745 }
11746
11747 #if HAVE_READLINE || HAVE_EDITLINE
11748 /*
11749 ** Readline completion callbacks
11750 */
11751 static char *readline_completion_generator(const char *text, int state){
11752   static sqlite3_stmt *pStmt = 0;
11753   char *zRet;
11754   if( state==0 ){
11755     char *zSql;
11756     sqlite3_finalize(pStmt);
11757     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11758                            "  FROM completion(%Q) ORDER BY 1", text);
11759     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11760     sqlite3_free(zSql);
11761   }
11762   if( sqlite3_step(pStmt)==SQLITE_ROW ){
11763     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
11764   }else{
11765     sqlite3_finalize(pStmt);
11766     pStmt = 0;
11767     zRet = 0;
11768   }
11769   return zRet;
11770 }
11771 static char **readline_completion(const char *zText, int iStart, int iEnd){
11772   rl_attempted_completion_over = 1;
11773   return rl_completion_matches(zText, readline_completion_generator);
11774 }
11775
11776 #elif HAVE_LINENOISE
11777 /*
11778 ** Linenoise completion callback
11779 */
11780 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
11781   int nLine = strlen30(zLine);
11782   int i, iStart;
11783   sqlite3_stmt *pStmt = 0;
11784   char *zSql;
11785   char zBuf[1000];
11786
11787   if( nLine>sizeof(zBuf)-30 ) return;
11788   if( zLine[0]=='.' || zLine[0]=='#') return;
11789   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
11790   if( i==nLine-1 ) return;
11791   iStart = i+1;
11792   memcpy(zBuf, zLine, iStart);
11793   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11794                          "  FROM completion(%Q,%Q) ORDER BY 1",
11795                          &zLine[iStart], zLine);
11796   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11797   sqlite3_free(zSql);
11798   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
11799   while( sqlite3_step(pStmt)==SQLITE_ROW ){
11800     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
11801     int nCompletion = sqlite3_column_bytes(pStmt, 0);
11802     if( iStart+nCompletion < sizeof(zBuf)-1 ){
11803       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
11804       linenoiseAddCompletion(lc, zBuf);
11805     }
11806   }
11807   sqlite3_finalize(pStmt);
11808 }
11809 #endif
11810
11811 /*
11812 ** Do C-language style dequoting.
11813 **
11814 **    \a    -> alarm
11815 **    \b    -> backspace
11816 **    \t    -> tab
11817 **    \n    -> newline
11818 **    \v    -> vertical tab
11819 **    \f    -> form feed
11820 **    \r    -> carriage return
11821 **    \s    -> space
11822 **    \"    -> "
11823 **    \'    -> '
11824 **    \\    -> backslash
11825 **    \NNN  -> ascii character NNN in octal
11826 */
11827 static void resolve_backslashes(char *z){
11828   int i, j;
11829   char c;
11830   while( *z && *z!='\\' ) z++;
11831   for(i=j=0; (c = z[i])!=0; i++, j++){
11832     if( c=='\\' && z[i+1]!=0 ){
11833       c = z[++i];
11834       if( c=='a' ){
11835         c = '\a';
11836       }else if( c=='b' ){
11837         c = '\b';
11838       }else if( c=='t' ){
11839         c = '\t';
11840       }else if( c=='n' ){
11841         c = '\n';
11842       }else if( c=='v' ){
11843         c = '\v';
11844       }else if( c=='f' ){
11845         c = '\f';
11846       }else if( c=='r' ){
11847         c = '\r';
11848       }else if( c=='"' ){
11849         c = '"';
11850       }else if( c=='\'' ){
11851         c = '\'';
11852       }else if( c=='\\' ){
11853         c = '\\';
11854       }else if( c>='0' && c<='7' ){
11855         c -= '0';
11856         if( z[i+1]>='0' && z[i+1]<='7' ){
11857           i++;
11858           c = (c<<3) + z[i] - '0';
11859           if( z[i+1]>='0' && z[i+1]<='7' ){
11860             i++;
11861             c = (c<<3) + z[i] - '0';
11862           }
11863         }
11864       }
11865     }
11866     z[j] = c;
11867   }
11868   if( j<i ) z[j] = 0;
11869 }
11870
11871 /*
11872 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
11873 ** for TRUE and FALSE.  Return the integer value if appropriate.
11874 */
11875 static int booleanValue(const char *zArg){
11876   int i;
11877   if( zArg[0]=='0' && zArg[1]=='x' ){
11878     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
11879   }else{
11880     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
11881   }
11882   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
11883   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
11884     return 1;
11885   }
11886   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
11887     return 0;
11888   }
11889   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
11890           zArg);
11891   return 0;
11892 }
11893
11894 /*
11895 ** Set or clear a shell flag according to a boolean value.
11896 */
11897 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
11898   if( booleanValue(zArg) ){
11899     ShellSetFlag(p, mFlag);
11900   }else{
11901     ShellClearFlag(p, mFlag);
11902   }
11903 }
11904
11905 /*
11906 ** Close an output file, assuming it is not stderr or stdout
11907 */
11908 static void output_file_close(FILE *f){
11909   if( f && f!=stdout && f!=stderr ) fclose(f);
11910 }
11911
11912 /*
11913 ** Try to open an output file.   The names "stdout" and "stderr" are
11914 ** recognized and do the right thing.  NULL is returned if the output
11915 ** filename is "off".
11916 */
11917 static FILE *output_file_open(const char *zFile, int bTextMode){
11918   FILE *f;
11919   if( strcmp(zFile,"stdout")==0 ){
11920     f = stdout;
11921   }else if( strcmp(zFile, "stderr")==0 ){
11922     f = stderr;
11923   }else if( strcmp(zFile, "off")==0 ){
11924     f = 0;
11925   }else{
11926     f = fopen(zFile, bTextMode ? "w" : "wb");
11927     if( f==0 ){
11928       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
11929     }
11930   }
11931   return f;
11932 }
11933
11934 #ifndef SQLITE_OMIT_TRACE
11935 /*
11936 ** A routine for handling output from sqlite3_trace().
11937 */
11938 static int sql_trace_callback(
11939   unsigned mType,         /* The trace type */
11940   void *pArg,             /* The ShellState pointer */
11941   void *pP,               /* Usually a pointer to sqlite_stmt */
11942   void *pX                /* Auxiliary output */
11943 ){
11944   ShellState *p = (ShellState*)pArg;
11945   sqlite3_stmt *pStmt;
11946   const char *zSql;
11947   int nSql;
11948   if( p->traceOut==0 ) return 0;
11949   if( mType==SQLITE_TRACE_CLOSE ){
11950     utf8_printf(p->traceOut, "-- closing database connection\n");
11951     return 0;
11952   }
11953   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
11954     zSql = (const char*)pX;
11955   }else{
11956     pStmt = (sqlite3_stmt*)pP;
11957     switch( p->eTraceType ){
11958       case SHELL_TRACE_EXPANDED: {
11959         zSql = sqlite3_expanded_sql(pStmt);
11960         break;
11961       }
11962 #ifdef SQLITE_ENABLE_NORMALIZE
11963       case SHELL_TRACE_NORMALIZED: {
11964         zSql = sqlite3_normalized_sql(pStmt);
11965         break;
11966       }
11967 #endif
11968       default: {
11969         zSql = sqlite3_sql(pStmt);
11970         break;
11971       }
11972     }
11973   }
11974   if( zSql==0 ) return 0;
11975   nSql = strlen30(zSql);
11976   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
11977   switch( mType ){
11978     case SQLITE_TRACE_ROW:
11979     case SQLITE_TRACE_STMT: {
11980       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
11981       break;
11982     }
11983     case SQLITE_TRACE_PROFILE: {
11984       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
11985       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
11986       break;
11987     }
11988   }
11989   return 0;
11990 }
11991 #endif
11992
11993 /*
11994 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
11995 ** a useful spot to set a debugger breakpoint.
11996 */
11997 static void test_breakpoint(void){
11998   static int nCall = 0;
11999   nCall++;
12000 }
12001
12002 /*
12003 ** An object used to read a CSV and other files for import.
12004 */
12005 typedef struct ImportCtx ImportCtx;
12006 struct ImportCtx {
12007   const char *zFile;  /* Name of the input file */
12008   FILE *in;           /* Read the CSV text from this input stream */
12009   char *z;            /* Accumulated text for a field */
12010   int n;              /* Number of bytes in z */
12011   int nAlloc;         /* Space allocated for z[] */
12012   int nLine;          /* Current line number */
12013   int bNotFirst;      /* True if one or more bytes already read */
12014   int cTerm;          /* Character that terminated the most recent field */
12015   int cColSep;        /* The column separator character.  (Usually ",") */
12016   int cRowSep;        /* The row separator character.  (Usually "\n") */
12017 };
12018
12019 /* Append a single byte to z[] */
12020 static void import_append_char(ImportCtx *p, int c){
12021   if( p->n+1>=p->nAlloc ){
12022     p->nAlloc += p->nAlloc + 100;
12023     p->z = sqlite3_realloc64(p->z, p->nAlloc);
12024     if( p->z==0 ) shell_out_of_memory();
12025   }
12026   p->z[p->n++] = (char)c;
12027 }
12028
12029 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
12030 ** with the option of having a separator other than ",".
12031 **
12032 **   +  Input comes from p->in.
12033 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
12034 **      from sqlite3_malloc64().
12035 **   +  Use p->cSep as the column separator.  The default is ",".
12036 **   +  Use p->rSep as the row separator.  The default is "\n".
12037 **   +  Keep track of the line number in p->nLine.
12038 **   +  Store the character that terminates the field in p->cTerm.  Store
12039 **      EOF on end-of-file.
12040 **   +  Report syntax errors on stderr
12041 */
12042 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
12043   int c;
12044   int cSep = p->cColSep;
12045   int rSep = p->cRowSep;
12046   p->n = 0;
12047   c = fgetc(p->in);
12048   if( c==EOF || seenInterrupt ){
12049     p->cTerm = EOF;
12050     return 0;
12051   }
12052   if( c=='"' ){
12053     int pc, ppc;
12054     int startLine = p->nLine;
12055     int cQuote = c;
12056     pc = ppc = 0;
12057     while( 1 ){
12058       c = fgetc(p->in);
12059       if( c==rSep ) p->nLine++;
12060       if( c==cQuote ){
12061         if( pc==cQuote ){
12062           pc = 0;
12063           continue;
12064         }
12065       }
12066       if( (c==cSep && pc==cQuote)
12067        || (c==rSep && pc==cQuote)
12068        || (c==rSep && pc=='\r' && ppc==cQuote)
12069        || (c==EOF && pc==cQuote)
12070       ){
12071         do{ p->n--; }while( p->z[p->n]!=cQuote );
12072         p->cTerm = c;
12073         break;
12074       }
12075       if( pc==cQuote && c!='\r' ){
12076         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
12077                 p->zFile, p->nLine, cQuote);
12078       }
12079       if( c==EOF ){
12080         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
12081                 p->zFile, startLine, cQuote);
12082         p->cTerm = c;
12083         break;
12084       }
12085       import_append_char(p, c);
12086       ppc = pc;
12087       pc = c;
12088     }
12089   }else{
12090     /* If this is the first field being parsed and it begins with the
12091     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
12092     if( (c&0xff)==0xef && p->bNotFirst==0 ){
12093       import_append_char(p, c);
12094       c = fgetc(p->in);
12095       if( (c&0xff)==0xbb ){
12096         import_append_char(p, c);
12097         c = fgetc(p->in);
12098         if( (c&0xff)==0xbf ){
12099           p->bNotFirst = 1;
12100           p->n = 0;
12101           return csv_read_one_field(p);
12102         }
12103       }
12104     }
12105     while( c!=EOF && c!=cSep && c!=rSep ){
12106       import_append_char(p, c);
12107       c = fgetc(p->in);
12108     }
12109     if( c==rSep ){
12110       p->nLine++;
12111       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
12112     }
12113     p->cTerm = c;
12114   }
12115   if( p->z ) p->z[p->n] = 0;
12116   p->bNotFirst = 1;
12117   return p->z;
12118 }
12119
12120 /* Read a single field of ASCII delimited text.
12121 **
12122 **   +  Input comes from p->in.
12123 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
12124 **      from sqlite3_malloc64().
12125 **   +  Use p->cSep as the column separator.  The default is "\x1F".
12126 **   +  Use p->rSep as the row separator.  The default is "\x1E".
12127 **   +  Keep track of the row number in p->nLine.
12128 **   +  Store the character that terminates the field in p->cTerm.  Store
12129 **      EOF on end-of-file.
12130 **   +  Report syntax errors on stderr
12131 */
12132 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
12133   int c;
12134   int cSep = p->cColSep;
12135   int rSep = p->cRowSep;
12136   p->n = 0;
12137   c = fgetc(p->in);
12138   if( c==EOF || seenInterrupt ){
12139     p->cTerm = EOF;
12140     return 0;
12141   }
12142   while( c!=EOF && c!=cSep && c!=rSep ){
12143     import_append_char(p, c);
12144     c = fgetc(p->in);
12145   }
12146   if( c==rSep ){
12147     p->nLine++;
12148   }
12149   p->cTerm = c;
12150   if( p->z ) p->z[p->n] = 0;
12151   return p->z;
12152 }
12153
12154 /*
12155 ** Try to transfer data for table zTable.  If an error is seen while
12156 ** moving forward, try to go backwards.  The backwards movement won't
12157 ** work for WITHOUT ROWID tables.
12158 */
12159 static void tryToCloneData(
12160   ShellState *p,
12161   sqlite3 *newDb,
12162   const char *zTable
12163 ){
12164   sqlite3_stmt *pQuery = 0;
12165   sqlite3_stmt *pInsert = 0;
12166   char *zQuery = 0;
12167   char *zInsert = 0;
12168   int rc;
12169   int i, j, n;
12170   int nTable = strlen30(zTable);
12171   int k = 0;
12172   int cnt = 0;
12173   const int spinRate = 10000;
12174
12175   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
12176   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12177   if( rc ){
12178     utf8_printf(stderr, "Error %d: %s on [%s]\n",
12179             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12180             zQuery);
12181     goto end_data_xfer;
12182   }
12183   n = sqlite3_column_count(pQuery);
12184   zInsert = sqlite3_malloc64(200 + nTable + n*3);
12185   if( zInsert==0 ) shell_out_of_memory();
12186   sqlite3_snprintf(200+nTable,zInsert,
12187                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
12188   i = strlen30(zInsert);
12189   for(j=1; j<n; j++){
12190     memcpy(zInsert+i, ",?", 2);
12191     i += 2;
12192   }
12193   memcpy(zInsert+i, ");", 3);
12194   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
12195   if( rc ){
12196     utf8_printf(stderr, "Error %d: %s on [%s]\n",
12197             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
12198             zQuery);
12199     goto end_data_xfer;
12200   }
12201   for(k=0; k<2; k++){
12202     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12203       for(i=0; i<n; i++){
12204         switch( sqlite3_column_type(pQuery, i) ){
12205           case SQLITE_NULL: {
12206             sqlite3_bind_null(pInsert, i+1);
12207             break;
12208           }
12209           case SQLITE_INTEGER: {
12210             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
12211             break;
12212           }
12213           case SQLITE_FLOAT: {
12214             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
12215             break;
12216           }
12217           case SQLITE_TEXT: {
12218             sqlite3_bind_text(pInsert, i+1,
12219                              (const char*)sqlite3_column_text(pQuery,i),
12220                              -1, SQLITE_STATIC);
12221             break;
12222           }
12223           case SQLITE_BLOB: {
12224             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
12225                                             sqlite3_column_bytes(pQuery,i),
12226                                             SQLITE_STATIC);
12227             break;
12228           }
12229         }
12230       } /* End for */
12231       rc = sqlite3_step(pInsert);
12232       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
12233         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
12234                         sqlite3_errmsg(newDb));
12235       }
12236       sqlite3_reset(pInsert);
12237       cnt++;
12238       if( (cnt%spinRate)==0 ){
12239         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
12240         fflush(stdout);
12241       }
12242     } /* End while */
12243     if( rc==SQLITE_DONE ) break;
12244     sqlite3_finalize(pQuery);
12245     sqlite3_free(zQuery);
12246     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
12247                              zTable);
12248     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12249     if( rc ){
12250       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
12251       break;
12252     }
12253   } /* End for(k=0...) */
12254
12255 end_data_xfer:
12256   sqlite3_finalize(pQuery);
12257   sqlite3_finalize(pInsert);
12258   sqlite3_free(zQuery);
12259   sqlite3_free(zInsert);
12260 }
12261
12262
12263 /*
12264 ** Try to transfer all rows of the schema that match zWhere.  For
12265 ** each row, invoke xForEach() on the object defined by that row.
12266 ** If an error is encountered while moving forward through the
12267 ** sqlite_master table, try again moving backwards.
12268 */
12269 static void tryToCloneSchema(
12270   ShellState *p,
12271   sqlite3 *newDb,
12272   const char *zWhere,
12273   void (*xForEach)(ShellState*,sqlite3*,const char*)
12274 ){
12275   sqlite3_stmt *pQuery = 0;
12276   char *zQuery = 0;
12277   int rc;
12278   const unsigned char *zName;
12279   const unsigned char *zSql;
12280   char *zErrMsg = 0;
12281
12282   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12283                            " WHERE %s", zWhere);
12284   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12285   if( rc ){
12286     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12287                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12288                     zQuery);
12289     goto end_schema_xfer;
12290   }
12291   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12292     zName = sqlite3_column_text(pQuery, 0);
12293     zSql = sqlite3_column_text(pQuery, 1);
12294     printf("%s... ", zName); fflush(stdout);
12295     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12296     if( zErrMsg ){
12297       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12298       sqlite3_free(zErrMsg);
12299       zErrMsg = 0;
12300     }
12301     if( xForEach ){
12302       xForEach(p, newDb, (const char*)zName);
12303     }
12304     printf("done\n");
12305   }
12306   if( rc!=SQLITE_DONE ){
12307     sqlite3_finalize(pQuery);
12308     sqlite3_free(zQuery);
12309     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12310                              " WHERE %s ORDER BY rowid DESC", zWhere);
12311     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12312     if( rc ){
12313       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12314                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12315                       zQuery);
12316       goto end_schema_xfer;
12317     }
12318     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12319       zName = sqlite3_column_text(pQuery, 0);
12320       zSql = sqlite3_column_text(pQuery, 1);
12321       printf("%s... ", zName); fflush(stdout);
12322       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12323       if( zErrMsg ){
12324         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12325         sqlite3_free(zErrMsg);
12326         zErrMsg = 0;
12327       }
12328       if( xForEach ){
12329         xForEach(p, newDb, (const char*)zName);
12330       }
12331       printf("done\n");
12332     }
12333   }
12334 end_schema_xfer:
12335   sqlite3_finalize(pQuery);
12336   sqlite3_free(zQuery);
12337 }
12338
12339 /*
12340 ** Open a new database file named "zNewDb".  Try to recover as much information
12341 ** as possible out of the main database (which might be corrupt) and write it
12342 ** into zNewDb.
12343 */
12344 static void tryToClone(ShellState *p, const char *zNewDb){
12345   int rc;
12346   sqlite3 *newDb = 0;
12347   if( access(zNewDb,0)==0 ){
12348     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
12349     return;
12350   }
12351   rc = sqlite3_open(zNewDb, &newDb);
12352   if( rc ){
12353     utf8_printf(stderr, "Cannot create output database: %s\n",
12354             sqlite3_errmsg(newDb));
12355   }else{
12356     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
12357     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
12358     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
12359     tryToCloneSchema(p, newDb, "type!='table'", 0);
12360     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
12361     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
12362   }
12363   close_db(newDb);
12364 }
12365
12366 /*
12367 ** Change the output file back to stdout.
12368 **
12369 ** If the p->doXdgOpen flag is set, that means the output was being
12370 ** redirected to a temporary file named by p->zTempFile.  In that case,
12371 ** launch start/open/xdg-open on that temporary file.
12372 */
12373 static void output_reset(ShellState *p){
12374   if( p->outfile[0]=='|' ){
12375 #ifndef SQLITE_OMIT_POPEN
12376     pclose(p->out);
12377 #endif
12378   }else{
12379     output_file_close(p->out);
12380 #ifndef SQLITE_NOHAVE_SYSTEM
12381     if( p->doXdgOpen ){
12382       const char *zXdgOpenCmd =
12383 #if defined(_WIN32)
12384       "start";
12385 #elif defined(__APPLE__)
12386       "open";
12387 #else
12388       "xdg-open";
12389 #endif
12390       char *zCmd;
12391       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
12392       if( system(zCmd) ){
12393         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
12394       }
12395       sqlite3_free(zCmd);
12396       outputModePop(p);
12397       p->doXdgOpen = 0;
12398     }
12399 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
12400   }
12401   p->outfile[0] = 0;
12402   p->out = stdout;
12403 }
12404
12405 /*
12406 ** Run an SQL command and return the single integer result.
12407 */
12408 static int db_int(ShellState *p, const char *zSql){
12409   sqlite3_stmt *pStmt;
12410   int res = 0;
12411   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12412   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
12413     res = sqlite3_column_int(pStmt,0);
12414   }
12415   sqlite3_finalize(pStmt);
12416   return res;
12417 }
12418
12419 /*
12420 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
12421 */
12422 static unsigned int get2byteInt(unsigned char *a){
12423   return (a[0]<<8) + a[1];
12424 }
12425 static unsigned int get4byteInt(unsigned char *a){
12426   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
12427 }
12428
12429 /*
12430 ** Implementation of the ".info" command.
12431 **
12432 ** Return 1 on error, 2 to exit, and 0 otherwise.
12433 */
12434 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
12435   static const struct { const char *zName; int ofst; } aField[] = {
12436      { "file change counter:",  24  },
12437      { "database page count:",  28  },
12438      { "freelist page count:",  36  },
12439      { "schema cookie:",        40  },
12440      { "schema format:",        44  },
12441      { "default cache size:",   48  },
12442      { "autovacuum top root:",  52  },
12443      { "incremental vacuum:",   64  },
12444      { "text encoding:",        56  },
12445      { "user version:",         60  },
12446      { "application id:",       68  },
12447      { "software version:",     96  },
12448   };
12449   static const struct { const char *zName; const char *zSql; } aQuery[] = {
12450      { "number of tables:",
12451        "SELECT count(*) FROM %s WHERE type='table'" },
12452      { "number of indexes:",
12453        "SELECT count(*) FROM %s WHERE type='index'" },
12454      { "number of triggers:",
12455        "SELECT count(*) FROM %s WHERE type='trigger'" },
12456      { "number of views:",
12457        "SELECT count(*) FROM %s WHERE type='view'" },
12458      { "schema size:",
12459        "SELECT total(length(sql)) FROM %s" },
12460   };
12461   int i, rc;
12462   unsigned iDataVersion;
12463   char *zSchemaTab;
12464   char *zDb = nArg>=2 ? azArg[1] : "main";
12465   sqlite3_stmt *pStmt = 0;
12466   unsigned char aHdr[100];
12467   open_db(p, 0);
12468   if( p->db==0 ) return 1;
12469   rc = sqlite3_prepare_v2(p->db,
12470              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12471              -1, &pStmt, 0);
12472   if( rc ){
12473     if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
12474       utf8_printf(stderr, "the \".dbinfo\" command requires the "
12475                           "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
12476     }else{
12477       utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
12478     }
12479     sqlite3_finalize(pStmt);
12480     return 1;
12481   }
12482   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
12483   if( sqlite3_step(pStmt)==SQLITE_ROW
12484    && sqlite3_column_bytes(pStmt,0)>100
12485   ){
12486     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
12487     sqlite3_finalize(pStmt);
12488   }else{
12489     raw_printf(stderr, "unable to read database header\n");
12490     sqlite3_finalize(pStmt);
12491     return 1;
12492   }
12493   i = get2byteInt(aHdr+16);
12494   if( i==1 ) i = 65536;
12495   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
12496   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
12497   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
12498   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
12499   for(i=0; i<ArraySize(aField); i++){
12500     int ofst = aField[i].ofst;
12501     unsigned int val = get4byteInt(aHdr + ofst);
12502     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
12503     switch( ofst ){
12504       case 56: {
12505         if( val==1 ) raw_printf(p->out, " (utf8)");
12506         if( val==2 ) raw_printf(p->out, " (utf16le)");
12507         if( val==3 ) raw_printf(p->out, " (utf16be)");
12508       }
12509     }
12510     raw_printf(p->out, "\n");
12511   }
12512   if( zDb==0 ){
12513     zSchemaTab = sqlite3_mprintf("main.sqlite_master");
12514   }else if( strcmp(zDb,"temp")==0 ){
12515     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
12516   }else{
12517     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
12518   }
12519   for(i=0; i<ArraySize(aQuery); i++){
12520     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
12521     int val = db_int(p, zSql);
12522     sqlite3_free(zSql);
12523     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
12524   }
12525   sqlite3_free(zSchemaTab);
12526   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
12527   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
12528   return 0;
12529 }
12530
12531 /*
12532 ** Print the current sqlite3_errmsg() value to stderr and return 1.
12533 */
12534 static int shellDatabaseError(sqlite3 *db){
12535   const char *zErr = sqlite3_errmsg(db);
12536   utf8_printf(stderr, "Error: %s\n", zErr);
12537   return 1;
12538 }
12539
12540 /*
12541 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
12542 ** if they match and FALSE (0) if they do not match.
12543 **
12544 ** Globbing rules:
12545 **
12546 **      '*'       Matches any sequence of zero or more characters.
12547 **
12548 **      '?'       Matches exactly one character.
12549 **
12550 **     [...]      Matches one character from the enclosed list of
12551 **                characters.
12552 **
12553 **     [^...]     Matches one character not in the enclosed list.
12554 **
12555 **      '#'       Matches any sequence of one or more digits with an
12556 **                optional + or - sign in front
12557 **
12558 **      ' '       Any span of whitespace matches any other span of
12559 **                whitespace.
12560 **
12561 ** Extra whitespace at the end of z[] is ignored.
12562 */
12563 static int testcase_glob(const char *zGlob, const char *z){
12564   int c, c2;
12565   int invert;
12566   int seen;
12567
12568   while( (c = (*(zGlob++)))!=0 ){
12569     if( IsSpace(c) ){
12570       if( !IsSpace(*z) ) return 0;
12571       while( IsSpace(*zGlob) ) zGlob++;
12572       while( IsSpace(*z) ) z++;
12573     }else if( c=='*' ){
12574       while( (c=(*(zGlob++))) == '*' || c=='?' ){
12575         if( c=='?' && (*(z++))==0 ) return 0;
12576       }
12577       if( c==0 ){
12578         return 1;
12579       }else if( c=='[' ){
12580         while( *z && testcase_glob(zGlob-1,z)==0 ){
12581           z++;
12582         }
12583         return (*z)!=0;
12584       }
12585       while( (c2 = (*(z++)))!=0 ){
12586         while( c2!=c ){
12587           c2 = *(z++);
12588           if( c2==0 ) return 0;
12589         }
12590         if( testcase_glob(zGlob,z) ) return 1;
12591       }
12592       return 0;
12593     }else if( c=='?' ){
12594       if( (*(z++))==0 ) return 0;
12595     }else if( c=='[' ){
12596       int prior_c = 0;
12597       seen = 0;
12598       invert = 0;
12599       c = *(z++);
12600       if( c==0 ) return 0;
12601       c2 = *(zGlob++);
12602       if( c2=='^' ){
12603         invert = 1;
12604         c2 = *(zGlob++);
12605       }
12606       if( c2==']' ){
12607         if( c==']' ) seen = 1;
12608         c2 = *(zGlob++);
12609       }
12610       while( c2 && c2!=']' ){
12611         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
12612           c2 = *(zGlob++);
12613           if( c>=prior_c && c<=c2 ) seen = 1;
12614           prior_c = 0;
12615         }else{
12616           if( c==c2 ){
12617             seen = 1;
12618           }
12619           prior_c = c2;
12620         }
12621         c2 = *(zGlob++);
12622       }
12623       if( c2==0 || (seen ^ invert)==0 ) return 0;
12624     }else if( c=='#' ){
12625       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
12626       if( !IsDigit(z[0]) ) return 0;
12627       z++;
12628       while( IsDigit(z[0]) ){ z++; }
12629     }else{
12630       if( c!=(*(z++)) ) return 0;
12631     }
12632   }
12633   while( IsSpace(*z) ){ z++; }
12634   return *z==0;
12635 }
12636
12637
12638 /*
12639 ** Compare the string as a command-line option with either one or two
12640 ** initial "-" characters.
12641 */
12642 static int optionMatch(const char *zStr, const char *zOpt){
12643   if( zStr[0]!='-' ) return 0;
12644   zStr++;
12645   if( zStr[0]=='-' ) zStr++;
12646   return strcmp(zStr, zOpt)==0;
12647 }
12648
12649 /*
12650 ** Delete a file.
12651 */
12652 int shellDeleteFile(const char *zFilename){
12653   int rc;
12654 #ifdef _WIN32
12655   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
12656   rc = _wunlink(z);
12657   sqlite3_free(z);
12658 #else
12659   rc = unlink(zFilename);
12660 #endif
12661   return rc;
12662 }
12663
12664 /*
12665 ** Try to delete the temporary file (if there is one) and free the
12666 ** memory used to hold the name of the temp file.
12667 */
12668 static void clearTempFile(ShellState *p){
12669   if( p->zTempFile==0 ) return;
12670   if( p->doXdgOpen ) return;
12671   if( shellDeleteFile(p->zTempFile) ) return;
12672   sqlite3_free(p->zTempFile);
12673   p->zTempFile = 0;
12674 }
12675
12676 /*
12677 ** Create a new temp file name with the given suffix.
12678 */
12679 static void newTempFile(ShellState *p, const char *zSuffix){
12680   clearTempFile(p);
12681   sqlite3_free(p->zTempFile);
12682   p->zTempFile = 0;
12683   if( p->db ){
12684     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
12685   }
12686   if( p->zTempFile==0 ){
12687     sqlite3_uint64 r;
12688     sqlite3_randomness(sizeof(r), &r);
12689     p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
12690   }else{
12691     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
12692   }
12693   if( p->zTempFile==0 ){
12694     raw_printf(stderr, "out of memory\n");
12695     exit(1);
12696   }
12697 }
12698
12699
12700 /*
12701 ** The implementation of SQL scalar function fkey_collate_clause(), used
12702 ** by the ".lint fkey-indexes" command. This scalar function is always
12703 ** called with four arguments - the parent table name, the parent column name,
12704 ** the child table name and the child column name.
12705 **
12706 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
12707 **
12708 ** If either of the named tables or columns do not exist, this function
12709 ** returns an empty string. An empty string is also returned if both tables
12710 ** and columns exist but have the same default collation sequence. Or,
12711 ** if both exist but the default collation sequences are different, this
12712 ** function returns the string " COLLATE <parent-collation>", where
12713 ** <parent-collation> is the default collation sequence of the parent column.
12714 */
12715 static void shellFkeyCollateClause(
12716   sqlite3_context *pCtx,
12717   int nVal,
12718   sqlite3_value **apVal
12719 ){
12720   sqlite3 *db = sqlite3_context_db_handle(pCtx);
12721   const char *zParent;
12722   const char *zParentCol;
12723   const char *zParentSeq;
12724   const char *zChild;
12725   const char *zChildCol;
12726   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
12727   int rc;
12728
12729   assert( nVal==4 );
12730   zParent = (const char*)sqlite3_value_text(apVal[0]);
12731   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
12732   zChild = (const char*)sqlite3_value_text(apVal[2]);
12733   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
12734
12735   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
12736   rc = sqlite3_table_column_metadata(
12737       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
12738   );
12739   if( rc==SQLITE_OK ){
12740     rc = sqlite3_table_column_metadata(
12741         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
12742     );
12743   }
12744
12745   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
12746     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
12747     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
12748     sqlite3_free(z);
12749   }
12750 }
12751
12752
12753 /*
12754 ** The implementation of dot-command ".lint fkey-indexes".
12755 */
12756 static int lintFkeyIndexes(
12757   ShellState *pState,             /* Current shell tool state */
12758   char **azArg,                   /* Array of arguments passed to dot command */
12759   int nArg                        /* Number of entries in azArg[] */
12760 ){
12761   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
12762   FILE *out = pState->out;        /* Stream to write non-error output to */
12763   int bVerbose = 0;               /* If -verbose is present */
12764   int bGroupByParent = 0;         /* If -groupbyparent is present */
12765   int i;                          /* To iterate through azArg[] */
12766   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
12767   int rc;                         /* Return code */
12768   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
12769
12770   /*
12771   ** This SELECT statement returns one row for each foreign key constraint
12772   ** in the schema of the main database. The column values are:
12773   **
12774   ** 0. The text of an SQL statement similar to:
12775   **
12776   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
12777   **
12778   **    This SELECT is similar to the one that the foreign keys implementation
12779   **    needs to run internally on child tables. If there is an index that can
12780   **    be used to optimize this query, then it can also be used by the FK
12781   **    implementation to optimize DELETE or UPDATE statements on the parent
12782   **    table.
12783   **
12784   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
12785   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
12786   **    contains an index that can be used to optimize the query.
12787   **
12788   ** 2. Human readable text that describes the child table and columns. e.g.
12789   **
12790   **       "child_table(child_key1, child_key2)"
12791   **
12792   ** 3. Human readable text that describes the parent table and columns. e.g.
12793   **
12794   **       "parent_table(parent_key1, parent_key2)"
12795   **
12796   ** 4. A full CREATE INDEX statement for an index that could be used to
12797   **    optimize DELETE or UPDATE statements on the parent table. e.g.
12798   **
12799   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
12800   **
12801   ** 5. The name of the parent table.
12802   **
12803   ** These six values are used by the C logic below to generate the report.
12804   */
12805   const char *zSql =
12806   "SELECT "
12807     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
12808     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
12809     "  || fkey_collate_clause("
12810     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
12811     ", "
12812     "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
12813     "  || group_concat('*=?', ' AND ') || ')'"
12814     ", "
12815     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
12816     ", "
12817     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
12818     ", "
12819     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
12820     "  || ' ON ' || quote(s.name) || '('"
12821     "  || group_concat(quote(f.[from]) ||"
12822     "        fkey_collate_clause("
12823     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
12824     "  || ');'"
12825     ", "
12826     "     f.[table] "
12827     "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
12828     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
12829     "GROUP BY s.name, f.id "
12830     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
12831   ;
12832   const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
12833
12834   for(i=2; i<nArg; i++){
12835     int n = strlen30(azArg[i]);
12836     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
12837       bVerbose = 1;
12838     }
12839     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
12840       bGroupByParent = 1;
12841       zIndent = "    ";
12842     }
12843     else{
12844       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
12845           azArg[0], azArg[1]
12846       );
12847       return SQLITE_ERROR;
12848     }
12849   }
12850
12851   /* Register the fkey_collate_clause() SQL function */
12852   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
12853       0, shellFkeyCollateClause, 0, 0
12854   );
12855
12856
12857   if( rc==SQLITE_OK ){
12858     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
12859   }
12860   if( rc==SQLITE_OK ){
12861     sqlite3_bind_int(pSql, 1, bGroupByParent);
12862   }
12863
12864   if( rc==SQLITE_OK ){
12865     int rc2;
12866     char *zPrev = 0;
12867     while( SQLITE_ROW==sqlite3_step(pSql) ){
12868       int res = -1;
12869       sqlite3_stmt *pExplain = 0;
12870       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
12871       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
12872       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
12873       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
12874       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
12875       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
12876
12877       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
12878       if( rc!=SQLITE_OK ) break;
12879       if( SQLITE_ROW==sqlite3_step(pExplain) ){
12880         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
12881         res = (
12882               0==sqlite3_strglob(zGlob, zPlan)
12883            || 0==sqlite3_strglob(zGlobIPK, zPlan)
12884         );
12885       }
12886       rc = sqlite3_finalize(pExplain);
12887       if( rc!=SQLITE_OK ) break;
12888
12889       if( res<0 ){
12890         raw_printf(stderr, "Error: internal error");
12891         break;
12892       }else{
12893         if( bGroupByParent
12894         && (bVerbose || res==0)
12895         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
12896         ){
12897           raw_printf(out, "-- Parent table %s\n", zParent);
12898           sqlite3_free(zPrev);
12899           zPrev = sqlite3_mprintf("%s", zParent);
12900         }
12901
12902         if( res==0 ){
12903           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
12904         }else if( bVerbose ){
12905           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
12906               zIndent, zFrom, zTarget
12907           );
12908         }
12909       }
12910     }
12911     sqlite3_free(zPrev);
12912
12913     if( rc!=SQLITE_OK ){
12914       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12915     }
12916
12917     rc2 = sqlite3_finalize(pSql);
12918     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
12919       rc = rc2;
12920       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12921     }
12922   }else{
12923     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12924   }
12925
12926   return rc;
12927 }
12928
12929 /*
12930 ** Implementation of ".lint" dot command.
12931 */
12932 static int lintDotCommand(
12933   ShellState *pState,             /* Current shell tool state */
12934   char **azArg,                   /* Array of arguments passed to dot command */
12935   int nArg                        /* Number of entries in azArg[] */
12936 ){
12937   int n;
12938   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
12939   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
12940   return lintFkeyIndexes(pState, azArg, nArg);
12941
12942  usage:
12943   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
12944   raw_printf(stderr, "Where sub-commands are:\n");
12945   raw_printf(stderr, "    fkey-indexes\n");
12946   return SQLITE_ERROR;
12947 }
12948
12949 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12950 /*********************************************************************************
12951 ** The ".archive" or ".ar" command.
12952 */
12953 static void shellPrepare(
12954   sqlite3 *db, 
12955   int *pRc, 
12956   const char *zSql, 
12957   sqlite3_stmt **ppStmt
12958 ){
12959   *ppStmt = 0;
12960   if( *pRc==SQLITE_OK ){
12961     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12962     if( rc!=SQLITE_OK ){
12963       raw_printf(stderr, "sql error: %s (%d)\n", 
12964           sqlite3_errmsg(db), sqlite3_errcode(db)
12965       );
12966       *pRc = rc;
12967     }
12968   }
12969 }
12970
12971 static void shellPreparePrintf(
12972   sqlite3 *db, 
12973   int *pRc, 
12974   sqlite3_stmt **ppStmt,
12975   const char *zFmt, 
12976   ...
12977 ){
12978   *ppStmt = 0;
12979   if( *pRc==SQLITE_OK ){
12980     va_list ap;
12981     char *z;
12982     va_start(ap, zFmt);
12983     z = sqlite3_vmprintf(zFmt, ap);
12984     va_end(ap);
12985     if( z==0 ){
12986       *pRc = SQLITE_NOMEM;
12987     }else{
12988       shellPrepare(db, pRc, z, ppStmt);
12989       sqlite3_free(z);
12990     }
12991   }
12992 }
12993
12994 static void shellFinalize(
12995   int *pRc, 
12996   sqlite3_stmt *pStmt
12997 ){
12998   if( pStmt ){
12999     sqlite3 *db = sqlite3_db_handle(pStmt);
13000     int rc = sqlite3_finalize(pStmt);
13001     if( *pRc==SQLITE_OK ){
13002       if( rc!=SQLITE_OK ){
13003         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
13004       }
13005       *pRc = rc;
13006     }
13007   }
13008 }
13009
13010 static void shellReset(
13011   int *pRc, 
13012   sqlite3_stmt *pStmt
13013 ){
13014   int rc = sqlite3_reset(pStmt);
13015   if( *pRc==SQLITE_OK ){
13016     if( rc!=SQLITE_OK ){
13017       sqlite3 *db = sqlite3_db_handle(pStmt);
13018       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
13019     }
13020     *pRc = rc;
13021   }
13022 }
13023 /*
13024 ** Structure representing a single ".ar" command.
13025 */
13026 typedef struct ArCommand ArCommand;
13027 struct ArCommand {
13028   u8 eCmd;                        /* An AR_CMD_* value */
13029   u8 bVerbose;                    /* True if --verbose */
13030   u8 bZip;                        /* True if the archive is a ZIP */
13031   u8 bDryRun;                     /* True if --dry-run */
13032   u8 bAppend;                     /* True if --append */
13033   u8 fromCmdLine;                 /* Run from -A instead of .archive */
13034   int nArg;                       /* Number of command arguments */
13035   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
13036   const char *zFile;              /* --file argument, or NULL */
13037   const char *zDir;               /* --directory argument, or NULL */
13038   char **azArg;                   /* Array of command arguments */
13039   ShellState *p;                  /* Shell state */
13040   sqlite3 *db;                    /* Database containing the archive */
13041 };
13042
13043 /*
13044 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
13045 */
13046 static int arUsage(FILE *f){
13047   showHelp(f,"archive");
13048   return SQLITE_ERROR;
13049 }
13050
13051 /*
13052 ** Print an error message for the .ar command to stderr and return 
13053 ** SQLITE_ERROR.
13054 */
13055 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
13056   va_list ap;
13057   char *z;
13058   va_start(ap, zFmt);
13059   z = sqlite3_vmprintf(zFmt, ap);
13060   va_end(ap);
13061   utf8_printf(stderr, "Error: %s\n", z);
13062   if( pAr->fromCmdLine ){
13063     utf8_printf(stderr, "Use \"-A\" for more help\n");
13064   }else{
13065     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
13066   }
13067   sqlite3_free(z);
13068   return SQLITE_ERROR;
13069 }
13070
13071 /*
13072 ** Values for ArCommand.eCmd.
13073 */
13074 #define AR_CMD_CREATE       1
13075 #define AR_CMD_UPDATE       2
13076 #define AR_CMD_INSERT       3
13077 #define AR_CMD_EXTRACT      4
13078 #define AR_CMD_LIST         5
13079 #define AR_CMD_HELP         6
13080
13081 /*
13082 ** Other (non-command) switches.
13083 */
13084 #define AR_SWITCH_VERBOSE     7
13085 #define AR_SWITCH_FILE        8
13086 #define AR_SWITCH_DIRECTORY   9
13087 #define AR_SWITCH_APPEND     10
13088 #define AR_SWITCH_DRYRUN     11
13089
13090 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
13091   switch( eSwitch ){
13092     case AR_CMD_CREATE:
13093     case AR_CMD_EXTRACT:
13094     case AR_CMD_LIST:
13095     case AR_CMD_UPDATE:
13096     case AR_CMD_INSERT:
13097     case AR_CMD_HELP:
13098       if( pAr->eCmd ){
13099         return arErrorMsg(pAr, "multiple command options");
13100       }
13101       pAr->eCmd = eSwitch;
13102       break;
13103
13104     case AR_SWITCH_DRYRUN:
13105       pAr->bDryRun = 1;
13106       break;
13107     case AR_SWITCH_VERBOSE:
13108       pAr->bVerbose = 1;
13109       break;
13110     case AR_SWITCH_APPEND:
13111       pAr->bAppend = 1;
13112       /* Fall thru into --file */
13113     case AR_SWITCH_FILE:
13114       pAr->zFile = zArg;
13115       break;
13116     case AR_SWITCH_DIRECTORY:
13117       pAr->zDir = zArg;
13118       break;
13119   }
13120
13121   return SQLITE_OK;
13122 }
13123
13124 /*
13125 ** Parse the command line for an ".ar" command. The results are written into
13126 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
13127 ** successfully, otherwise an error message is written to stderr and 
13128 ** SQLITE_ERROR returned.
13129 */
13130 static int arParseCommand(
13131   char **azArg,                   /* Array of arguments passed to dot command */
13132   int nArg,                       /* Number of entries in azArg[] */
13133   ArCommand *pAr                  /* Populate this object */
13134 ){
13135   struct ArSwitch {
13136     const char *zLong;
13137     char cShort;
13138     u8 eSwitch;
13139     u8 bArg;
13140   } aSwitch[] = {
13141     { "create",    'c', AR_CMD_CREATE,       0 },
13142     { "extract",   'x', AR_CMD_EXTRACT,      0 },
13143     { "insert",    'i', AR_CMD_INSERT,       0 },
13144     { "list",      't', AR_CMD_LIST,         0 },
13145     { "update",    'u', AR_CMD_UPDATE,       0 },
13146     { "help",      'h', AR_CMD_HELP,         0 },
13147     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
13148     { "file",      'f', AR_SWITCH_FILE,      1 },
13149     { "append",    'a', AR_SWITCH_APPEND,    1 },
13150     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
13151     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
13152   };
13153   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
13154   struct ArSwitch *pEnd = &aSwitch[nSwitch];
13155
13156   if( nArg<=1 ){
13157     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
13158     return arUsage(stderr);
13159   }else{
13160     char *z = azArg[1];
13161     if( z[0]!='-' ){
13162       /* Traditional style [tar] invocation */
13163       int i;
13164       int iArg = 2;
13165       for(i=0; z[i]; i++){
13166         const char *zArg = 0;
13167         struct ArSwitch *pOpt;
13168         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13169           if( z[i]==pOpt->cShort ) break;
13170         }
13171         if( pOpt==pEnd ){
13172           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13173         }
13174         if( pOpt->bArg ){
13175           if( iArg>=nArg ){
13176             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13177           }
13178           zArg = azArg[iArg++];
13179         }
13180         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13181       }
13182       pAr->nArg = nArg-iArg;
13183       if( pAr->nArg>0 ){
13184         pAr->azArg = &azArg[iArg];
13185       }
13186     }else{
13187       /* Non-traditional invocation */
13188       int iArg;
13189       for(iArg=1; iArg<nArg; iArg++){
13190         int n;
13191         z = azArg[iArg];
13192         if( z[0]!='-' ){
13193           /* All remaining command line words are command arguments. */
13194           pAr->azArg = &azArg[iArg];
13195           pAr->nArg = nArg-iArg;
13196           break;
13197         }
13198         n = strlen30(z);
13199
13200         if( z[1]!='-' ){
13201           int i;
13202           /* One or more short options */
13203           for(i=1; i<n; i++){
13204             const char *zArg = 0;
13205             struct ArSwitch *pOpt;
13206             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13207               if( z[i]==pOpt->cShort ) break;
13208             }
13209             if( pOpt==pEnd ){
13210               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13211             }
13212             if( pOpt->bArg ){
13213               if( i<(n-1) ){
13214                 zArg = &z[i+1];
13215                 i = n;
13216               }else{
13217                 if( iArg>=(nArg-1) ){
13218                   return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13219                 }
13220                 zArg = azArg[++iArg];
13221               }
13222             }
13223             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13224           }
13225         }else if( z[2]=='\0' ){
13226           /* A -- option, indicating that all remaining command line words
13227           ** are command arguments.  */
13228           pAr->azArg = &azArg[iArg+1];
13229           pAr->nArg = nArg-iArg-1;
13230           break;
13231         }else{
13232           /* A long option */
13233           const char *zArg = 0;             /* Argument for option, if any */
13234           struct ArSwitch *pMatch = 0;      /* Matching option */
13235           struct ArSwitch *pOpt;            /* Iterator */
13236           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13237             const char *zLong = pOpt->zLong;
13238             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
13239               if( pMatch ){
13240                 return arErrorMsg(pAr, "ambiguous option: %s",z);
13241               }else{
13242                 pMatch = pOpt;
13243               }
13244             }
13245           }
13246
13247           if( pMatch==0 ){
13248             return arErrorMsg(pAr, "unrecognized option: %s", z);
13249           }
13250           if( pMatch->bArg ){
13251             if( iArg>=(nArg-1) ){
13252               return arErrorMsg(pAr, "option requires an argument: %s", z);
13253             }
13254             zArg = azArg[++iArg];
13255           }
13256           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
13257         }
13258       }
13259     }
13260   }
13261
13262   return SQLITE_OK;
13263 }
13264
13265 /*
13266 ** This function assumes that all arguments within the ArCommand.azArg[]
13267 ** array refer to archive members, as for the --extract or --list commands. 
13268 ** It checks that each of them are present. If any specified file is not
13269 ** present in the archive, an error is printed to stderr and an error
13270 ** code returned. Otherwise, if all specified arguments are present in
13271 ** the archive, SQLITE_OK is returned.
13272 **
13273 ** This function strips any trailing '/' characters from each argument.
13274 ** This is consistent with the way the [tar] command seems to work on
13275 ** Linux.
13276 */
13277 static int arCheckEntries(ArCommand *pAr){
13278   int rc = SQLITE_OK;
13279   if( pAr->nArg ){
13280     int i, j;
13281     sqlite3_stmt *pTest = 0;
13282
13283     shellPreparePrintf(pAr->db, &rc, &pTest,
13284         "SELECT name FROM %s WHERE name=$name", 
13285         pAr->zSrcTable
13286     );
13287     j = sqlite3_bind_parameter_index(pTest, "$name");
13288     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13289       char *z = pAr->azArg[i];
13290       int n = strlen30(z);
13291       int bOk = 0;
13292       while( n>0 && z[n-1]=='/' ) n--;
13293       z[n] = '\0';
13294       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
13295       if( SQLITE_ROW==sqlite3_step(pTest) ){
13296         bOk = 1;
13297       }
13298       shellReset(&rc, pTest);
13299       if( rc==SQLITE_OK && bOk==0 ){
13300         utf8_printf(stderr, "not found in archive: %s\n", z);
13301         rc = SQLITE_ERROR;
13302       }
13303     }
13304     shellFinalize(&rc, pTest);
13305   }
13306   return rc;
13307 }
13308
13309 /*
13310 ** Format a WHERE clause that can be used against the "sqlar" table to
13311 ** identify all archive members that match the command arguments held
13312 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
13313 ** The caller is responsible for eventually calling sqlite3_free() on
13314 ** any non-NULL (*pzWhere) value.
13315 */
13316 static void arWhereClause(
13317   int *pRc, 
13318   ArCommand *pAr, 
13319   char **pzWhere                  /* OUT: New WHERE clause */
13320 ){
13321   char *zWhere = 0;
13322   if( *pRc==SQLITE_OK ){
13323     if( pAr->nArg==0 ){
13324       zWhere = sqlite3_mprintf("1");
13325     }else{
13326       int i;
13327       const char *zSep = "";
13328       for(i=0; i<pAr->nArg; i++){
13329         const char *z = pAr->azArg[i];
13330         zWhere = sqlite3_mprintf(
13331           "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", 
13332           zWhere, zSep, z, strlen30(z)+1, z
13333         );
13334         if( zWhere==0 ){
13335           *pRc = SQLITE_NOMEM;
13336           break;
13337         }
13338         zSep = " OR ";
13339       }
13340     }
13341   }
13342   *pzWhere = zWhere;
13343 }
13344
13345 /*
13346 ** Implementation of .ar "lisT" command. 
13347 */
13348 static int arListCommand(ArCommand *pAr){
13349   const char *zSql = "SELECT %s FROM %s WHERE %s"; 
13350   const char *azCols[] = {
13351     "name",
13352     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
13353   };
13354
13355   char *zWhere = 0;
13356   sqlite3_stmt *pSql = 0;
13357   int rc;
13358
13359   rc = arCheckEntries(pAr);
13360   arWhereClause(&rc, pAr, &zWhere);
13361
13362   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
13363                      pAr->zSrcTable, zWhere);
13364   if( pAr->bDryRun ){
13365     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13366   }else{
13367     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13368       if( pAr->bVerbose ){
13369         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
13370             sqlite3_column_text(pSql, 0),
13371             sqlite3_column_int(pSql, 1), 
13372             sqlite3_column_text(pSql, 2),
13373             sqlite3_column_text(pSql, 3)
13374         );
13375       }else{
13376         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13377       }
13378     }
13379   }
13380   shellFinalize(&rc, pSql);
13381   sqlite3_free(zWhere);
13382   return rc;
13383 }
13384
13385
13386 /*
13387 ** Implementation of .ar "eXtract" command. 
13388 */
13389 static int arExtractCommand(ArCommand *pAr){
13390   const char *zSql1 = 
13391     "SELECT "
13392     " ($dir || name),"
13393     " writefile(($dir || name), %s, mode, mtime) "
13394     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
13395     " AND name NOT GLOB '*..[/\\]*'";
13396
13397   const char *azExtraArg[] = { 
13398     "sqlar_uncompress(data, sz)",
13399     "data"
13400   };
13401
13402   sqlite3_stmt *pSql = 0;
13403   int rc = SQLITE_OK;
13404   char *zDir = 0;
13405   char *zWhere = 0;
13406   int i, j;
13407
13408   /* If arguments are specified, check that they actually exist within
13409   ** the archive before proceeding. And formulate a WHERE clause to
13410   ** match them.  */
13411   rc = arCheckEntries(pAr);
13412   arWhereClause(&rc, pAr, &zWhere);
13413
13414   if( rc==SQLITE_OK ){
13415     if( pAr->zDir ){
13416       zDir = sqlite3_mprintf("%s/", pAr->zDir);
13417     }else{
13418       zDir = sqlite3_mprintf("");
13419     }
13420     if( zDir==0 ) rc = SQLITE_NOMEM;
13421   }
13422
13423   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 
13424       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
13425   );
13426
13427   if( rc==SQLITE_OK ){
13428     j = sqlite3_bind_parameter_index(pSql, "$dir");
13429     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
13430
13431     /* Run the SELECT statement twice. The first time, writefile() is called
13432     ** for all archive members that should be extracted. The second time,
13433     ** only for the directories. This is because the timestamps for
13434     ** extracted directories must be reset after they are populated (as
13435     ** populating them changes the timestamp).  */
13436     for(i=0; i<2; i++){
13437       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
13438       sqlite3_bind_int(pSql, j, i);
13439       if( pAr->bDryRun ){
13440         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13441       }else{
13442         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13443           if( i==0 && pAr->bVerbose ){
13444             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13445           }
13446         }
13447       }
13448       shellReset(&rc, pSql);
13449     }
13450     shellFinalize(&rc, pSql);
13451   }
13452
13453   sqlite3_free(zDir);
13454   sqlite3_free(zWhere);
13455   return rc;
13456 }
13457
13458 /*
13459 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
13460 */
13461 static int arExecSql(ArCommand *pAr, const char *zSql){
13462   int rc;
13463   if( pAr->bDryRun ){
13464     utf8_printf(pAr->p->out, "%s\n", zSql);
13465     rc = SQLITE_OK;
13466   }else{
13467     char *zErr = 0;
13468     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
13469     if( zErr ){
13470       utf8_printf(stdout, "ERROR: %s\n", zErr);
13471       sqlite3_free(zErr);
13472     }
13473   }
13474   return rc;
13475 }
13476
13477
13478 /*
13479 ** Implementation of .ar "create", "insert", and "update" commands.
13480 **
13481 **     create    ->     Create a new SQL archive
13482 **     insert    ->     Insert or reinsert all files listed
13483 **     update    ->     Insert files that have changed or that were not
13484 **                      previously in the archive
13485 **
13486 ** Create the "sqlar" table in the database if it does not already exist.
13487 ** Then add each file in the azFile[] array to the archive. Directories
13488 ** are added recursively. If argument bVerbose is non-zero, a message is
13489 ** printed on stdout for each file archived.
13490 **
13491 ** The create command is the same as update, except that it drops
13492 ** any existing "sqlar" table before beginning.  The "insert" command
13493 ** always overwrites every file named on the command-line, where as
13494 ** "update" only overwrites if the size or mtime or mode has changed.
13495 */
13496 static int arCreateOrUpdateCommand(
13497   ArCommand *pAr,                 /* Command arguments and options */
13498   int bUpdate,                    /* true for a --create. */
13499   int bOnlyIfChanged              /* Only update if file has changed */
13500 ){
13501   const char *zCreate = 
13502       "CREATE TABLE IF NOT EXISTS sqlar(\n"
13503       "  name TEXT PRIMARY KEY,  -- name of the file\n"
13504       "  mode INT,               -- access permissions\n"
13505       "  mtime INT,              -- last modification time\n"
13506       "  sz INT,                 -- original file size\n"
13507       "  data BLOB               -- compressed content\n"
13508       ")";
13509   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
13510   const char *zInsertFmt[2] = {
13511      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
13512      "  SELECT\n"
13513      "    %s,\n"
13514      "    mode,\n"
13515      "    mtime,\n"
13516      "    CASE substr(lsmode(mode),1,1)\n"
13517      "      WHEN '-' THEN length(data)\n"
13518      "      WHEN 'd' THEN 0\n"
13519      "      ELSE -1 END,\n"
13520      "    sqlar_compress(data)\n"
13521      "  FROM fsdir(%Q,%Q) AS disk\n"
13522      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
13523      ,
13524      "REPLACE INTO %s(name,mode,mtime,data)\n"
13525      "  SELECT\n"
13526      "    %s,\n"
13527      "    mode,\n"
13528      "    mtime,\n"
13529      "    data\n"
13530      "  FROM fsdir(%Q,%Q) AS disk\n"
13531      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
13532   };
13533   int i;                          /* For iterating through azFile[] */
13534   int rc;                         /* Return code */
13535   const char *zTab = 0;           /* SQL table into which to insert */
13536   char *zSql;
13537   char zTemp[50];
13538   char *zExists = 0;
13539
13540   arExecSql(pAr, "PRAGMA page_size=512");
13541   rc = arExecSql(pAr, "SAVEPOINT ar;");
13542   if( rc!=SQLITE_OK ) return rc;
13543   zTemp[0] = 0; 
13544   if( pAr->bZip ){
13545     /* Initialize the zipfile virtual table, if necessary */
13546     if( pAr->zFile ){
13547       sqlite3_uint64 r;
13548       sqlite3_randomness(sizeof(r),&r);
13549       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
13550       zTab = zTemp;
13551       zSql = sqlite3_mprintf(
13552          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
13553          zTab, pAr->zFile
13554       );
13555       rc = arExecSql(pAr, zSql);
13556       sqlite3_free(zSql);
13557     }else{
13558       zTab = "zip";
13559     }
13560   }else{
13561     /* Initialize the table for an SQLAR */
13562     zTab = "sqlar";
13563     if( bUpdate==0 ){
13564       rc = arExecSql(pAr, zDrop);
13565       if( rc!=SQLITE_OK ) goto end_ar_transaction;
13566     }
13567     rc = arExecSql(pAr, zCreate);
13568   }
13569   if( bOnlyIfChanged ){
13570     zExists = sqlite3_mprintf(
13571       " AND NOT EXISTS("
13572           "SELECT 1 FROM %s AS mem"
13573           " WHERE mem.name=disk.name"
13574           " AND mem.mtime=disk.mtime"
13575           " AND mem.mode=disk.mode)", zTab);
13576   }else{
13577     zExists = sqlite3_mprintf("");
13578   }
13579   if( zExists==0 ) rc = SQLITE_NOMEM;
13580   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13581     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
13582         pAr->bVerbose ? "shell_putsnl(name)" : "name",
13583         pAr->azArg[i], pAr->zDir, zExists);
13584     rc = arExecSql(pAr, zSql2);
13585     sqlite3_free(zSql2);
13586   }
13587 end_ar_transaction:
13588   if( rc!=SQLITE_OK ){
13589     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
13590   }else{
13591     rc = arExecSql(pAr, "RELEASE ar;");
13592     if( pAr->bZip && pAr->zFile ){
13593       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
13594       arExecSql(pAr, zSql);
13595       sqlite3_free(zSql);
13596     }
13597   }
13598   sqlite3_free(zExists);
13599   return rc;
13600 }
13601
13602 /*
13603 ** Implementation of ".ar" dot command.
13604 */
13605 static int arDotCommand(
13606   ShellState *pState,             /* Current shell tool state */
13607   int fromCmdLine,                /* True if -A command-line option, not .ar cmd */
13608   char **azArg,                   /* Array of arguments passed to dot command */
13609   int nArg                        /* Number of entries in azArg[] */
13610 ){
13611   ArCommand cmd;
13612   int rc;
13613   memset(&cmd, 0, sizeof(cmd));
13614   cmd.fromCmdLine = fromCmdLine;
13615   rc = arParseCommand(azArg, nArg, &cmd);
13616   if( rc==SQLITE_OK ){
13617     int eDbType = SHELL_OPEN_UNSPEC;
13618     cmd.p = pState;
13619     cmd.db = pState->db;
13620     if( cmd.zFile ){
13621       eDbType = deduceDatabaseType(cmd.zFile, 1);
13622     }else{
13623       eDbType = pState->openMode;
13624     }
13625     if( eDbType==SHELL_OPEN_ZIPFILE ){
13626       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
13627         if( cmd.zFile==0 ){
13628           cmd.zSrcTable = sqlite3_mprintf("zip");
13629         }else{
13630           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
13631         }
13632       }
13633       cmd.bZip = 1;
13634     }else if( cmd.zFile ){
13635       int flags;
13636       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13637       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 
13638            || cmd.eCmd==AR_CMD_UPDATE ){
13639         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13640       }else{
13641         flags = SQLITE_OPEN_READONLY;
13642       }
13643       cmd.db = 0;
13644       if( cmd.bDryRun ){
13645         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
13646              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
13647       }
13648       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 
13649              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
13650       if( rc!=SQLITE_OK ){
13651         utf8_printf(stderr, "cannot open file: %s (%s)\n", 
13652             cmd.zFile, sqlite3_errmsg(cmd.db)
13653         );
13654         goto end_ar_command;
13655       }
13656       sqlite3_fileio_init(cmd.db, 0, 0);
13657       sqlite3_sqlar_init(cmd.db, 0, 0);
13658       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13659                               shellPutsFunc, 0, 0);
13660
13661     }
13662     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13663       if( cmd.eCmd!=AR_CMD_CREATE
13664        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13665       ){
13666         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13667         rc = SQLITE_ERROR;
13668         goto end_ar_command;
13669       }
13670       cmd.zSrcTable = sqlite3_mprintf("sqlar");
13671     }
13672
13673     switch( cmd.eCmd ){
13674       case AR_CMD_CREATE:
13675         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
13676         break;
13677
13678       case AR_CMD_EXTRACT:
13679         rc = arExtractCommand(&cmd);
13680         break;
13681
13682       case AR_CMD_LIST:
13683         rc = arListCommand(&cmd);
13684         break;
13685
13686       case AR_CMD_HELP:
13687         arUsage(pState->out);
13688         break;
13689
13690       case AR_CMD_INSERT:
13691         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
13692         break;
13693
13694       default:
13695         assert( cmd.eCmd==AR_CMD_UPDATE );
13696         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
13697         break;
13698     }
13699   }
13700 end_ar_command:
13701   if( cmd.db!=pState->db ){
13702     close_db(cmd.db);
13703   }
13704   sqlite3_free(cmd.zSrcTable);
13705
13706   return rc;
13707 }
13708 /* End of the ".archive" or ".ar" command logic
13709 **********************************************************************************/
13710 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
13711
13712
13713 /*
13714 ** If an input line begins with "." then invoke this routine to
13715 ** process that line.
13716 **
13717 ** Return 1 on error, 2 to exit, and 0 otherwise.
13718 */
13719 static int do_meta_command(char *zLine, ShellState *p){
13720   int h = 1;
13721   int nArg = 0;
13722   int n, c;
13723   int rc = 0;
13724   char *azArg[50];
13725
13726 #ifndef SQLITE_OMIT_VIRTUALTABLE
13727   if( p->expert.pExpert ){
13728     expertFinish(p, 1, 0);
13729   }
13730 #endif
13731
13732   /* Parse the input line into tokens.
13733   */
13734   while( zLine[h] && nArg<ArraySize(azArg) ){
13735     while( IsSpace(zLine[h]) ){ h++; }
13736     if( zLine[h]==0 ) break;
13737     if( zLine[h]=='\'' || zLine[h]=='"' ){
13738       int delim = zLine[h++];
13739       azArg[nArg++] = &zLine[h];
13740       while( zLine[h] && zLine[h]!=delim ){
13741         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
13742         h++;
13743       }
13744       if( zLine[h]==delim ){
13745         zLine[h++] = 0;
13746       }
13747       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
13748     }else{
13749       azArg[nArg++] = &zLine[h];
13750       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
13751       if( zLine[h] ) zLine[h++] = 0;
13752       resolve_backslashes(azArg[nArg-1]);
13753     }
13754   }
13755
13756   /* Process the input line.
13757   */
13758   if( nArg==0 ) return 0; /* no tokens, no error */
13759   n = strlen30(azArg[0]);
13760   c = azArg[0][0];
13761   clearTempFile(p);
13762
13763 #ifndef SQLITE_OMIT_AUTHORIZATION
13764   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
13765     if( nArg!=2 ){
13766       raw_printf(stderr, "Usage: .auth ON|OFF\n");
13767       rc = 1;
13768       goto meta_command_exit;
13769     }
13770     open_db(p, 0);
13771     if( booleanValue(azArg[1]) ){
13772       sqlite3_set_authorizer(p->db, shellAuth, p);
13773     }else{
13774       sqlite3_set_authorizer(p->db, 0, 0);
13775     }
13776   }else
13777 #endif
13778
13779 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13780   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13781     open_db(p, 0);
13782     rc = arDotCommand(p, 0, azArg, nArg);
13783   }else
13784 #endif
13785
13786   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13787    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
13788   ){
13789     const char *zDestFile = 0;
13790     const char *zDb = 0;
13791     sqlite3 *pDest;
13792     sqlite3_backup *pBackup;
13793     int j;
13794     int bAsync = 0;
13795     const char *zVfs = 0;
13796     for(j=1; j<nArg; j++){
13797       const char *z = azArg[j];
13798       if( z[0]=='-' ){
13799         if( z[1]=='-' ) z++;
13800         if( strcmp(z, "-append")==0 ){
13801           zVfs = "apndvfs";
13802         }else
13803         if( strcmp(z, "-async")==0 ){
13804           bAsync = 1;
13805         }else
13806         {
13807           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
13808           return 1;
13809         }
13810       }else if( zDestFile==0 ){
13811         zDestFile = azArg[j];
13812       }else if( zDb==0 ){
13813         zDb = zDestFile;
13814         zDestFile = azArg[j];
13815       }else{
13816         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
13817         return 1;
13818       }
13819     }
13820     if( zDestFile==0 ){
13821       raw_printf(stderr, "missing FILENAME argument on .backup\n");
13822       return 1;
13823     }
13824     if( zDb==0 ) zDb = "main";
13825     rc = sqlite3_open_v2(zDestFile, &pDest, 
13826                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13827     if( rc!=SQLITE_OK ){
13828       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13829       close_db(pDest);
13830       return 1;
13831     }
13832     if( bAsync ){
13833       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
13834                    0, 0, 0);
13835     }
13836     open_db(p, 0);
13837     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13838     if( pBackup==0 ){
13839       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13840       close_db(pDest);
13841       return 1;
13842     }
13843     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13844     sqlite3_backup_finish(pBackup);
13845     if( rc==SQLITE_DONE ){
13846       rc = 0;
13847     }else{
13848       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13849       rc = 1;
13850     }
13851     close_db(pDest);
13852   }else
13853
13854   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13855     if( nArg==2 ){
13856       bail_on_error = booleanValue(azArg[1]);
13857     }else{
13858       raw_printf(stderr, "Usage: .bail on|off\n");
13859       rc = 1;
13860     }
13861   }else
13862
13863   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
13864     if( nArg==2 ){
13865       if( booleanValue(azArg[1]) ){
13866         setBinaryMode(p->out, 1);
13867       }else{
13868         setTextMode(p->out, 1);
13869       }
13870     }else{
13871       raw_printf(stderr, "Usage: .binary on|off\n");
13872       rc = 1;
13873     }
13874   }else
13875
13876   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
13877     if( nArg==2 ){
13878 #if defined(_WIN32) || defined(WIN32)
13879       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
13880       rc = !SetCurrentDirectoryW(z);
13881       sqlite3_free(z);
13882 #else
13883       rc = chdir(azArg[1]);
13884 #endif
13885       if( rc ){
13886         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
13887         rc = 1;
13888       }
13889     }else{
13890       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
13891       rc = 1;
13892     }
13893   }else
13894
13895   /* The undocumented ".breakpoint" command causes a call to the no-op
13896   ** routine named test_breakpoint().
13897   */
13898   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
13899     test_breakpoint();
13900   }else
13901
13902   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
13903     if( nArg==2 ){
13904       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
13905     }else{
13906       raw_printf(stderr, "Usage: .changes on|off\n");
13907       rc = 1;
13908     }
13909   }else
13910
13911   /* Cancel output redirection, if it is currently set (by .testcase)
13912   ** Then read the content of the testcase-out.txt file and compare against
13913   ** azArg[1].  If there are differences, report an error and exit.
13914   */
13915   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
13916     char *zRes = 0;
13917     output_reset(p);
13918     if( nArg!=2 ){
13919       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
13920       rc = 2;
13921     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
13922       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
13923       rc = 2;
13924     }else if( testcase_glob(azArg[1],zRes)==0 ){
13925       utf8_printf(stderr,
13926                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
13927                  p->zTestcase, azArg[1], zRes);
13928       rc = 1;
13929     }else{
13930       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
13931       p->nCheck++;
13932     }
13933     sqlite3_free(zRes);
13934   }else
13935
13936   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
13937     if( nArg==2 ){
13938       tryToClone(p, azArg[1]);
13939     }else{
13940       raw_printf(stderr, "Usage: .clone FILENAME\n");
13941       rc = 1;
13942     }
13943   }else
13944
13945   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
13946     ShellState data;
13947     char *zErrMsg = 0;
13948     open_db(p, 0);
13949     memcpy(&data, p, sizeof(data));
13950     data.showHeader = 0;
13951     data.cMode = data.mode = MODE_List;
13952     sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
13953     data.cnt = 0;
13954     sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
13955                  callback, &data, &zErrMsg);
13956     if( zErrMsg ){
13957       utf8_printf(stderr,"Error: %s\n", zErrMsg);
13958       sqlite3_free(zErrMsg);
13959       rc = 1;
13960     }
13961   }else
13962
13963   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13964     static const struct DbConfigChoices {
13965       const char *zName;
13966       int op;
13967     } aDbConfig[] = {
13968         { "enable_fkey",      SQLITE_DBCONFIG_ENABLE_FKEY            },
13969         { "enable_trigger",   SQLITE_DBCONFIG_ENABLE_TRIGGER         },
13970         { "fts3_tokenizer",   SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER  },
13971         { "load_extension",   SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION  },
13972         { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE       },
13973         { "enable_qpsg",      SQLITE_DBCONFIG_ENABLE_QPSG            },
13974         { "trigger_eqp",      SQLITE_DBCONFIG_TRIGGER_EQP            },
13975         { "reset_database",   SQLITE_DBCONFIG_RESET_DATABASE         },
13976         { "defensive",        SQLITE_DBCONFIG_DEFENSIVE              },
13977     };
13978     int ii, v;
13979     open_db(p, 0);
13980     for(ii=0; ii<ArraySize(aDbConfig); ii++){
13981       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13982       if( nArg>=3 ){
13983         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13984       }
13985       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13986       utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13987       if( nArg>1 ) break;
13988     }
13989     if( nArg>1 && ii==ArraySize(aDbConfig) ){
13990       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13991       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13992     }   
13993   }else
13994
13995   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13996     rc = shell_dbinfo_command(p, nArg, azArg);
13997   }else
13998
13999   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
14000     const char *zLike = 0;
14001     int i;
14002     int savedShowHeader = p->showHeader;
14003     int savedShellFlags = p->shellFlgs;
14004     ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
14005     for(i=1; i<nArg; i++){
14006       if( azArg[i][0]=='-' ){
14007         const char *z = azArg[i]+1;
14008         if( z[0]=='-' ) z++;
14009         if( strcmp(z,"preserve-rowids")==0 ){
14010 #ifdef SQLITE_OMIT_VIRTUALTABLE
14011           raw_printf(stderr, "The --preserve-rowids option is not compatible"
14012                              " with SQLITE_OMIT_VIRTUALTABLE\n");
14013           rc = 1;
14014           goto meta_command_exit;
14015 #else
14016           ShellSetFlag(p, SHFLG_PreserveRowid);
14017 #endif
14018         }else
14019         if( strcmp(z,"newlines")==0 ){
14020           ShellSetFlag(p, SHFLG_Newlines);
14021         }else
14022         {
14023           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
14024           rc = 1;
14025           goto meta_command_exit;
14026         }
14027       }else if( zLike ){
14028         raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
14029                            "?--newlines? ?LIKE-PATTERN?\n");
14030         rc = 1;
14031         goto meta_command_exit;
14032       }else{
14033         zLike = azArg[i];
14034       }
14035     }
14036     open_db(p, 0);
14037     /* When playing back a "dump", the content might appear in an order
14038     ** which causes immediate foreign key constraints to be violated.
14039     ** So disable foreign-key constraint enforcement to prevent problems. */
14040     raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
14041     raw_printf(p->out, "BEGIN TRANSACTION;\n");
14042     p->writableSchema = 0;
14043     p->showHeader = 0;
14044     /* Set writable_schema=ON since doing so forces SQLite to initialize
14045     ** as much of the schema as it can even if the sqlite_master table is
14046     ** corrupt. */
14047     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
14048     p->nErr = 0;
14049     if( zLike==0 ){
14050       run_schema_dump_query(p,
14051         "SELECT name, type, sql FROM sqlite_master "
14052         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
14053       );
14054       run_schema_dump_query(p,
14055         "SELECT name, type, sql FROM sqlite_master "
14056         "WHERE name=='sqlite_sequence'"
14057       );
14058       run_table_dump_query(p,
14059         "SELECT sql FROM sqlite_master "
14060         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
14061       );
14062     }else{
14063       char *zSql;
14064       zSql = sqlite3_mprintf(
14065         "SELECT name, type, sql FROM sqlite_master "
14066         "WHERE tbl_name LIKE %Q AND type=='table'"
14067         "  AND sql NOT NULL", zLike);
14068       run_schema_dump_query(p,zSql);
14069       sqlite3_free(zSql);
14070       zSql = sqlite3_mprintf(
14071         "SELECT sql FROM sqlite_master "
14072         "WHERE sql NOT NULL"
14073         "  AND type IN ('index','trigger','view')"
14074         "  AND tbl_name LIKE %Q", zLike);
14075       run_table_dump_query(p, zSql, 0);
14076       sqlite3_free(zSql);
14077     }
14078     if( p->writableSchema ){
14079       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
14080       p->writableSchema = 0;
14081     }
14082     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
14083     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
14084     raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
14085     p->showHeader = savedShowHeader;
14086     p->shellFlgs = savedShellFlags;
14087   }else
14088
14089   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
14090     if( nArg==2 ){
14091       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
14092     }else{
14093       raw_printf(stderr, "Usage: .echo on|off\n");
14094       rc = 1;
14095     }
14096   }else
14097
14098   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
14099     if( nArg==2 ){
14100       p->autoEQPtest = 0;
14101       if( p->autoEQPtrace ){
14102         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
14103         p->autoEQPtrace = 0;
14104       }
14105       if( strcmp(azArg[1],"full")==0 ){
14106         p->autoEQP = AUTOEQP_full;
14107       }else if( strcmp(azArg[1],"trigger")==0 ){
14108         p->autoEQP = AUTOEQP_trigger;
14109 #ifdef SQLITE_DEBUG
14110       }else if( strcmp(azArg[1],"test")==0 ){
14111         p->autoEQP = AUTOEQP_on;
14112         p->autoEQPtest = 1;
14113       }else if( strcmp(azArg[1],"trace")==0 ){
14114         p->autoEQP = AUTOEQP_full;
14115         p->autoEQPtrace = 1;
14116         open_db(p, 0);
14117         sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
14118         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
14119 #endif
14120       }else{
14121         p->autoEQP = (u8)booleanValue(azArg[1]);
14122       }
14123     }else{
14124       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
14125       rc = 1;
14126     }
14127   }else
14128
14129   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
14130     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
14131     rc = 2;
14132   }else
14133
14134   /* The ".explain" command is automatic now.  It is largely pointless.  It
14135   ** retained purely for backwards compatibility */
14136   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
14137     int val = 1;
14138     if( nArg>=2 ){
14139       if( strcmp(azArg[1],"auto")==0 ){
14140         val = 99;
14141       }else{
14142         val =  booleanValue(azArg[1]);
14143       }
14144     }
14145     if( val==1 && p->mode!=MODE_Explain ){
14146       p->normalMode = p->mode;
14147       p->mode = MODE_Explain;
14148       p->autoExplain = 0;
14149     }else if( val==0 ){
14150       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14151       p->autoExplain = 0;
14152     }else if( val==99 ){
14153       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14154       p->autoExplain = 1;
14155     }
14156   }else
14157
14158 #ifndef SQLITE_OMIT_VIRTUALTABLE
14159   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
14160     open_db(p, 0);
14161     expertDotCommand(p, azArg, nArg);
14162   }else
14163 #endif
14164
14165   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
14166     ShellState data;
14167     char *zErrMsg = 0;
14168     int doStats = 0;
14169     memcpy(&data, p, sizeof(data));
14170     data.showHeader = 0;
14171     data.cMode = data.mode = MODE_Semi;
14172     if( nArg==2 && optionMatch(azArg[1], "indent") ){
14173       data.cMode = data.mode = MODE_Pretty;
14174       nArg = 1;
14175     }
14176     if( nArg!=1 ){
14177       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
14178       rc = 1;
14179       goto meta_command_exit;
14180     }
14181     open_db(p, 0);
14182     rc = sqlite3_exec(p->db,
14183        "SELECT sql FROM"
14184        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
14185        "     FROM sqlite_master UNION ALL"
14186        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
14187        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
14188        "ORDER BY rowid",
14189        callback, &data, &zErrMsg
14190     );
14191     if( rc==SQLITE_OK ){
14192       sqlite3_stmt *pStmt;
14193       rc = sqlite3_prepare_v2(p->db,
14194                "SELECT rowid FROM sqlite_master"
14195                " WHERE name GLOB 'sqlite_stat[134]'",
14196                -1, &pStmt, 0);
14197       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
14198       sqlite3_finalize(pStmt);
14199     }
14200     if( doStats==0 ){
14201       raw_printf(p->out, "/* No STAT tables available */\n");
14202     }else{
14203       raw_printf(p->out, "ANALYZE sqlite_master;\n");
14204       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
14205                    callback, &data, &zErrMsg);
14206       data.cMode = data.mode = MODE_Insert;
14207       data.zDestTable = "sqlite_stat1";
14208       shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
14209       data.zDestTable = "sqlite_stat3";
14210       shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
14211       data.zDestTable = "sqlite_stat4";
14212       shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
14213       raw_printf(p->out, "ANALYZE sqlite_master;\n");
14214     }
14215   }else
14216
14217   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
14218     if( nArg==2 ){
14219       p->showHeader = booleanValue(azArg[1]);
14220     }else{
14221       raw_printf(stderr, "Usage: .headers on|off\n");
14222       rc = 1;
14223     }
14224   }else
14225
14226   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
14227     if( nArg>=2 ){
14228       n = showHelp(p->out, azArg[1]);
14229       if( n==0 ){
14230         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
14231       }
14232     }else{
14233       showHelp(p->out, 0);
14234     }
14235   }else
14236
14237   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
14238     char *zTable;               /* Insert data into this table */
14239     char *zFile;                /* Name of file to extra content from */
14240     sqlite3_stmt *pStmt = NULL; /* A statement */
14241     int nCol;                   /* Number of columns in the table */
14242     int nByte;                  /* Number of bytes in an SQL string */
14243     int i, j;                   /* Loop counters */
14244     int needCommit;             /* True to COMMIT or ROLLBACK at end */
14245     int nSep;                   /* Number of bytes in p->colSeparator[] */
14246     char *zSql;                 /* An SQL statement */
14247     ImportCtx sCtx;             /* Reader context */
14248     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
14249     int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
14250
14251     if( nArg!=3 ){
14252       raw_printf(stderr, "Usage: .import FILE TABLE\n");
14253       goto meta_command_exit;
14254     }
14255     zFile = azArg[1];
14256     zTable = azArg[2];
14257     seenInterrupt = 0;
14258     memset(&sCtx, 0, sizeof(sCtx));
14259     open_db(p, 0);
14260     nSep = strlen30(p->colSeparator);
14261     if( nSep==0 ){
14262       raw_printf(stderr,
14263                  "Error: non-null column separator required for import\n");
14264       return 1;
14265     }
14266     if( nSep>1 ){
14267       raw_printf(stderr, "Error: multi-character column separators not allowed"
14268                       " for import\n");
14269       return 1;
14270     }
14271     nSep = strlen30(p->rowSeparator);
14272     if( nSep==0 ){
14273       raw_printf(stderr, "Error: non-null row separator required for import\n");
14274       return 1;
14275     }
14276     if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
14277       /* When importing CSV (only), if the row separator is set to the
14278       ** default output row separator, change it to the default input
14279       ** row separator.  This avoids having to maintain different input
14280       ** and output row separators. */
14281       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14282       nSep = strlen30(p->rowSeparator);
14283     }
14284     if( nSep>1 ){
14285       raw_printf(stderr, "Error: multi-character row separators not allowed"
14286                       " for import\n");
14287       return 1;
14288     }
14289     sCtx.zFile = zFile;
14290     sCtx.nLine = 1;
14291     if( sCtx.zFile[0]=='|' ){
14292 #ifdef SQLITE_OMIT_POPEN
14293       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14294       return 1;
14295 #else
14296       sCtx.in = popen(sCtx.zFile+1, "r");
14297       sCtx.zFile = "<pipe>";
14298       xCloser = pclose;
14299 #endif
14300     }else{
14301       sCtx.in = fopen(sCtx.zFile, "rb");
14302       xCloser = fclose;
14303     }
14304     if( p->mode==MODE_Ascii ){
14305       xRead = ascii_read_one_field;
14306     }else{
14307       xRead = csv_read_one_field;
14308     }
14309     if( sCtx.in==0 ){
14310       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
14311       return 1;
14312     }
14313     sCtx.cColSep = p->colSeparator[0];
14314     sCtx.cRowSep = p->rowSeparator[0];
14315     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
14316     if( zSql==0 ){
14317       xCloser(sCtx.in);
14318       shell_out_of_memory();
14319     }
14320     nByte = strlen30(zSql);
14321     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14322     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
14323     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
14324       char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
14325       char cSep = '(';
14326       while( xRead(&sCtx) ){
14327         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
14328         cSep = ',';
14329         if( sCtx.cTerm!=sCtx.cColSep ) break;
14330       }
14331       if( cSep=='(' ){
14332         sqlite3_free(zCreate);
14333         sqlite3_free(sCtx.z);
14334         xCloser(sCtx.in);
14335         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
14336         return 1;
14337       }
14338       zCreate = sqlite3_mprintf("%z\n)", zCreate);
14339       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
14340       sqlite3_free(zCreate);
14341       if( rc ){
14342         utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
14343                 sqlite3_errmsg(p->db));
14344         sqlite3_free(sCtx.z);
14345         xCloser(sCtx.in);
14346         return 1;
14347       }
14348       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14349     }
14350     sqlite3_free(zSql);
14351     if( rc ){
14352       if (pStmt) sqlite3_finalize(pStmt);
14353       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
14354       xCloser(sCtx.in);
14355       return 1;
14356     }
14357     nCol = sqlite3_column_count(pStmt);
14358     sqlite3_finalize(pStmt);
14359     pStmt = 0;
14360     if( nCol==0 ) return 0; /* no columns, no error */
14361     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
14362     if( zSql==0 ){
14363       xCloser(sCtx.in);
14364       shell_out_of_memory();
14365     }
14366     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
14367     j = strlen30(zSql);
14368     for(i=1; i<nCol; i++){
14369       zSql[j++] = ',';
14370       zSql[j++] = '?';
14371     }
14372     zSql[j++] = ')';
14373     zSql[j] = 0;
14374     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14375     sqlite3_free(zSql);
14376     if( rc ){
14377       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14378       if (pStmt) sqlite3_finalize(pStmt);
14379       xCloser(sCtx.in);
14380       return 1;
14381     }
14382     needCommit = sqlite3_get_autocommit(p->db);
14383     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
14384     do{
14385       int startLine = sCtx.nLine;
14386       for(i=0; i<nCol; i++){
14387         char *z = xRead(&sCtx);
14388         /*
14389         ** Did we reach end-of-file before finding any columns?
14390         ** If so, stop instead of NULL filling the remaining columns.
14391         */
14392         if( z==0 && i==0 ) break;
14393         /*
14394         ** Did we reach end-of-file OR end-of-line before finding any
14395         ** columns in ASCII mode?  If so, stop instead of NULL filling
14396         ** the remaining columns.
14397         */
14398         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
14399         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
14400         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
14401           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14402                           "filling the rest with NULL\n",
14403                           sCtx.zFile, startLine, nCol, i+1);
14404           i += 2;
14405           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
14406         }
14407       }
14408       if( sCtx.cTerm==sCtx.cColSep ){
14409         do{
14410           xRead(&sCtx);
14411           i++;
14412         }while( sCtx.cTerm==sCtx.cColSep );
14413         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14414                         "extras ignored\n",
14415                         sCtx.zFile, startLine, nCol, i);
14416       }
14417       if( i>=nCol ){
14418         sqlite3_step(pStmt);
14419         rc = sqlite3_reset(pStmt);
14420         if( rc!=SQLITE_OK ){
14421           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
14422                       startLine, sqlite3_errmsg(p->db));
14423         }
14424       }
14425     }while( sCtx.cTerm!=EOF );
14426
14427     xCloser(sCtx.in);
14428     sqlite3_free(sCtx.z);
14429     sqlite3_finalize(pStmt);
14430     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
14431   }else
14432
14433 #ifndef SQLITE_UNTESTABLE
14434   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
14435     char *zSql;
14436     char *zCollist = 0;
14437     sqlite3_stmt *pStmt;
14438     int tnum = 0;
14439     int i;
14440     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
14441       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
14442                           "       .imposter off\n");
14443       rc = 1;
14444       goto meta_command_exit;
14445     }
14446     open_db(p, 0);
14447     if( nArg==2 ){
14448       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
14449       goto meta_command_exit;
14450     }
14451     zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
14452                            " WHERE name='%q' AND type='index'", azArg[1]);
14453     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14454     sqlite3_free(zSql);
14455     if( sqlite3_step(pStmt)==SQLITE_ROW ){
14456       tnum = sqlite3_column_int(pStmt, 0);
14457     }
14458     sqlite3_finalize(pStmt);
14459     if( tnum==0 ){
14460       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
14461       rc = 1;
14462       goto meta_command_exit;
14463     }
14464     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
14465     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14466     sqlite3_free(zSql);
14467     i = 0;
14468     while( sqlite3_step(pStmt)==SQLITE_ROW ){
14469       char zLabel[20];
14470       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
14471       i++;
14472       if( zCol==0 ){
14473         if( sqlite3_column_int(pStmt,1)==-1 ){
14474           zCol = "_ROWID_";
14475         }else{
14476           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
14477           zCol = zLabel;
14478         }
14479       }
14480       if( zCollist==0 ){
14481         zCollist = sqlite3_mprintf("\"%w\"", zCol);
14482       }else{
14483         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
14484       }
14485     }
14486     sqlite3_finalize(pStmt);
14487     zSql = sqlite3_mprintf(
14488           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
14489           azArg[2], zCollist, zCollist);
14490     sqlite3_free(zCollist);
14491     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
14492     if( rc==SQLITE_OK ){
14493       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
14494       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
14495       if( rc ){
14496         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
14497       }else{
14498         utf8_printf(stdout, "%s;\n", zSql);
14499         raw_printf(stdout,
14500            "WARNING: writing to an imposter table will corrupt the index!\n"
14501         );
14502       }
14503     }else{
14504       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
14505       rc = 1;
14506     }
14507     sqlite3_free(zSql);
14508   }else
14509 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
14510
14511 #ifdef SQLITE_ENABLE_IOTRACE
14512   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
14513     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
14514     if( iotrace && iotrace!=stdout ) fclose(iotrace);
14515     iotrace = 0;
14516     if( nArg<2 ){
14517       sqlite3IoTrace = 0;
14518     }else if( strcmp(azArg[1], "-")==0 ){
14519       sqlite3IoTrace = iotracePrintf;
14520       iotrace = stdout;
14521     }else{
14522       iotrace = fopen(azArg[1], "w");
14523       if( iotrace==0 ){
14524         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
14525         sqlite3IoTrace = 0;
14526         rc = 1;
14527       }else{
14528         sqlite3IoTrace = iotracePrintf;
14529       }
14530     }
14531   }else
14532 #endif
14533
14534   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
14535     static const struct {
14536        const char *zLimitName;   /* Name of a limit */
14537        int limitCode;            /* Integer code for that limit */
14538     } aLimit[] = {
14539       { "length",                SQLITE_LIMIT_LENGTH                    },
14540       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
14541       { "column",                SQLITE_LIMIT_COLUMN                    },
14542       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
14543       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
14544       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
14545       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
14546       { "attached",              SQLITE_LIMIT_ATTACHED                  },
14547       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
14548       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
14549       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
14550       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
14551     };
14552     int i, n2;
14553     open_db(p, 0);
14554     if( nArg==1 ){
14555       for(i=0; i<ArraySize(aLimit); i++){
14556         printf("%20s %d\n", aLimit[i].zLimitName,
14557                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
14558       }
14559     }else if( nArg>3 ){
14560       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
14561       rc = 1;
14562       goto meta_command_exit;
14563     }else{
14564       int iLimit = -1;
14565       n2 = strlen30(azArg[1]);
14566       for(i=0; i<ArraySize(aLimit); i++){
14567         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
14568           if( iLimit<0 ){
14569             iLimit = i;
14570           }else{
14571             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
14572             rc = 1;
14573             goto meta_command_exit;
14574           }
14575         }
14576       }
14577       if( iLimit<0 ){
14578         utf8_printf(stderr, "unknown limit: \"%s\"\n"
14579                         "enter \".limits\" with no arguments for a list.\n",
14580                          azArg[1]);
14581         rc = 1;
14582         goto meta_command_exit;
14583       }
14584       if( nArg==3 ){
14585         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
14586                       (int)integerValue(azArg[2]));
14587       }
14588       printf("%20s %d\n", aLimit[iLimit].zLimitName,
14589              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
14590     }
14591   }else
14592
14593   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
14594     open_db(p, 0);
14595     lintDotCommand(p, azArg, nArg);
14596   }else
14597
14598 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14599   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
14600     const char *zFile, *zProc;
14601     char *zErrMsg = 0;
14602     if( nArg<2 ){
14603       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
14604       rc = 1;
14605       goto meta_command_exit;
14606     }
14607     zFile = azArg[1];
14608     zProc = nArg>=3 ? azArg[2] : 0;
14609     open_db(p, 0);
14610     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
14611     if( rc!=SQLITE_OK ){
14612       utf8_printf(stderr, "Error: %s\n", zErrMsg);
14613       sqlite3_free(zErrMsg);
14614       rc = 1;
14615     }
14616   }else
14617 #endif
14618
14619   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
14620     if( nArg!=2 ){
14621       raw_printf(stderr, "Usage: .log FILENAME\n");
14622       rc = 1;
14623     }else{
14624       const char *zFile = azArg[1];
14625       output_file_close(p->pLog);
14626       p->pLog = output_file_open(zFile, 0);
14627     }
14628   }else
14629
14630   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
14631     const char *zMode = nArg>=2 ? azArg[1] : "";
14632     int n2 = strlen30(zMode);
14633     int c2 = zMode[0];
14634     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
14635       p->mode = MODE_Line;
14636       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14637     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
14638       p->mode = MODE_Column;
14639       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14640     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
14641       p->mode = MODE_List;
14642       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
14643       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14644     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
14645       p->mode = MODE_Html;
14646     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
14647       p->mode = MODE_Tcl;
14648       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
14649       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14650     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
14651       p->mode = MODE_Csv;
14652       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14653       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14654     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
14655       p->mode = MODE_List;
14656       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
14657     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
14658       p->mode = MODE_Insert;
14659       set_table_name(p, nArg>=3 ? azArg[2] : "table");
14660     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
14661       p->mode = MODE_Quote;
14662     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
14663       p->mode = MODE_Ascii;
14664       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
14665       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
14666     }else if( nArg==1 ){
14667       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
14668     }else{
14669       raw_printf(stderr, "Error: mode should be one of: "
14670          "ascii column csv html insert line list quote tabs tcl\n");
14671       rc = 1;
14672     }
14673     p->cMode = p->mode;
14674   }else
14675
14676   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
14677     if( nArg==2 ){
14678       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
14679                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
14680     }else{
14681       raw_printf(stderr, "Usage: .nullvalue STRING\n");
14682       rc = 1;
14683     }
14684   }else
14685
14686   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
14687     char *zNewFilename;  /* Name of the database file to open */
14688     int iName = 1;       /* Index in azArg[] of the filename */
14689     int newFlag = 0;     /* True to delete file before opening */
14690     /* Close the existing database */
14691     session_close_all(p);
14692     close_db(p->db);
14693     p->db = 0;
14694     p->zDbFilename = 0;
14695     sqlite3_free(p->zFreeOnClose);
14696     p->zFreeOnClose = 0;
14697     p->openMode = SHELL_OPEN_UNSPEC;
14698     p->szMax = 0;
14699     /* Check for command-line arguments */
14700     for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
14701       const char *z = azArg[iName];
14702       if( optionMatch(z,"new") ){
14703         newFlag = 1;
14704 #ifdef SQLITE_HAVE_ZLIB
14705       }else if( optionMatch(z, "zip") ){
14706         p->openMode = SHELL_OPEN_ZIPFILE;
14707 #endif
14708       }else if( optionMatch(z, "append") ){
14709         p->openMode = SHELL_OPEN_APPENDVFS;
14710       }else if( optionMatch(z, "readonly") ){
14711         p->openMode = SHELL_OPEN_READONLY;
14712 #ifdef SQLITE_ENABLE_DESERIALIZE
14713       }else if( optionMatch(z, "deserialize") ){
14714         p->openMode = SHELL_OPEN_DESERIALIZE;
14715       }else if( optionMatch(z, "hexdb") ){
14716         p->openMode = SHELL_OPEN_HEXDB;
14717       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
14718         p->szMax = integerValue(azArg[++iName]);
14719 #endif /* SQLITE_ENABLE_DESERIALIZE */
14720       }else if( z[0]=='-' ){
14721         utf8_printf(stderr, "unknown option: %s\n", z);
14722         rc = 1;
14723         goto meta_command_exit;
14724       }
14725     }
14726     /* If a filename is specified, try to open it first */
14727     zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14728     if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
14729       if( newFlag ) shellDeleteFile(zNewFilename);
14730       p->zDbFilename = zNewFilename;
14731       open_db(p, OPEN_DB_KEEPALIVE);
14732       if( p->db==0 ){
14733         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14734         sqlite3_free(zNewFilename);
14735       }else{
14736         p->zFreeOnClose = zNewFilename;
14737       }
14738     }
14739     if( p->db==0 ){
14740       /* As a fall-back open a TEMP database */
14741       p->zDbFilename = 0;
14742       open_db(p, 0);
14743     }
14744   }else
14745
14746   if( (c=='o'
14747         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
14748    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
14749   ){
14750     const char *zFile = nArg>=2 ? azArg[1] : "stdout";
14751     int bTxtMode = 0;
14752     if( azArg[0][0]=='e' ){
14753       /* Transform the ".excel" command into ".once -x" */
14754       nArg = 2;
14755       azArg[0] = "once";
14756       zFile = azArg[1] = "-x";
14757       n = 4;
14758     }
14759     if( nArg>2 ){
14760       utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
14761       rc = 1;
14762       goto meta_command_exit;
14763     }
14764     if( n>1 && strncmp(azArg[0], "once", n)==0 ){
14765       if( nArg<2 ){
14766         raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
14767         rc = 1;
14768         goto meta_command_exit;
14769       }
14770       p->outCount = 2;
14771     }else{
14772       p->outCount = 0;
14773     }
14774     output_reset(p);
14775     if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
14776 #ifndef SQLITE_NOHAVE_SYSTEM
14777     if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
14778       p->doXdgOpen = 1;
14779       outputModePush(p);
14780       if( zFile[1]=='x' ){
14781         newTempFile(p, "csv");
14782         p->mode = MODE_Csv;
14783         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14784         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14785       }else{
14786         newTempFile(p, "txt");
14787         bTxtMode = 1;
14788       }
14789       zFile = p->zTempFile;
14790     }
14791 #endif /* SQLITE_NOHAVE_SYSTEM */
14792     if( zFile[0]=='|' ){
14793 #ifdef SQLITE_OMIT_POPEN
14794       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14795       rc = 1;
14796       p->out = stdout;
14797 #else
14798       p->out = popen(zFile + 1, "w");
14799       if( p->out==0 ){
14800         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
14801         p->out = stdout;
14802         rc = 1;
14803       }else{
14804         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14805       }
14806 #endif
14807     }else{
14808       p->out = output_file_open(zFile, bTxtMode);
14809       if( p->out==0 ){
14810         if( strcmp(zFile,"off")!=0 ){
14811           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
14812         }
14813         p->out = stdout;
14814         rc = 1;
14815       } else {
14816         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14817       }
14818     }
14819   }else
14820
14821   if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
14822     open_db(p,0);
14823     if( nArg<=1 ) goto parameter_syntax_error;
14824
14825     /* .parameter clear
14826     ** Clear all bind parameters by dropping the TEMP table that holds them.
14827     */
14828     if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
14829       int wrSchema = 0;
14830       sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
14831       sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
14832       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
14833                    0, 0, 0);
14834       sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
14835     }else
14836
14837     /* .parameter list
14838     ** List all bind parameters.
14839     */
14840     if( nArg==2 && strcmp(azArg[1],"list")==0 ){
14841       sqlite3_stmt *pStmt = 0;
14842       int rx;
14843       int len = 0;
14844       rx = sqlite3_prepare_v2(p->db,
14845              "SELECT max(length(key)) "
14846              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14847       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
14848         len = sqlite3_column_int(pStmt, 0);
14849         if( len>40 ) len = 40;
14850       }
14851       sqlite3_finalize(pStmt);
14852       pStmt = 0;
14853       if( len ){
14854         rx = sqlite3_prepare_v2(p->db,
14855              "SELECT key, quote(value) "
14856              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14857         while( sqlite3_step(pStmt)==SQLITE_ROW ){
14858           utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
14859                       sqlite3_column_text(pStmt,1));
14860         }
14861         sqlite3_finalize(pStmt);
14862       }
14863     }else
14864
14865     /* .parameter init
14866     ** Make sure the TEMP table used to hold bind parameters exists.
14867     ** Create it if necessary.
14868     */
14869     if( nArg==2 && strcmp(azArg[1],"init")==0 ){
14870       bind_table_init(p);
14871     }else
14872
14873     /* .parameter set NAME VALUE
14874     ** Set or reset a bind parameter.  NAME should be the full parameter
14875     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
14876     ** VALUE can be in either SQL literal notation, or if not it will be
14877     ** understood to be a text string.
14878     */
14879     if( nArg==4 && strcmp(azArg[1],"set")==0 ){
14880       int rx;
14881       char *zSql;
14882       sqlite3_stmt *pStmt;
14883       const char *zKey = azArg[2];
14884       const char *zValue = azArg[3];
14885       bind_table_init(p);
14886       zSql = sqlite3_mprintf(
14887                   "REPLACE INTO temp.sqlite_parameters(key,value)"
14888                   "VALUES(%Q,%s);", zKey, zValue);
14889       if( zSql==0 ) shell_out_of_memory();
14890       pStmt = 0;
14891       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14892       sqlite3_free(zSql);
14893       if( rx!=SQLITE_OK ){
14894         sqlite3_finalize(pStmt);
14895         pStmt = 0;
14896         zSql = sqlite3_mprintf(
14897                    "REPLACE INTO temp.sqlite_parameters(key,value)"
14898                    "VALUES(%Q,%Q);", zKey, zValue);
14899         if( zSql==0 ) shell_out_of_memory();
14900         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14901         sqlite3_free(zSql);
14902         if( rx!=SQLITE_OK ){
14903           utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
14904           sqlite3_finalize(pStmt);
14905           pStmt = 0;
14906           rc = 1;
14907         }
14908       }
14909       sqlite3_step(pStmt);
14910       sqlite3_finalize(pStmt);
14911     }else
14912
14913     /* .parameter unset NAME
14914     ** Remove the NAME binding from the parameter binding table, if it
14915     ** exists.
14916     */
14917     if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
14918       char *zSql = sqlite3_mprintf(
14919           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
14920       if( zSql==0 ) shell_out_of_memory();
14921       sqlite3_exec(p->db, zSql, 0, 0, 0);
14922       sqlite3_free(zSql);
14923     }else
14924     /* If no command name matches, show a syntax error */
14925     parameter_syntax_error:
14926     showHelp(p->out, "parameter");
14927   }else
14928
14929   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14930     int i;
14931     for(i=1; i<nArg; i++){
14932       if( i>1 ) raw_printf(p->out, " ");
14933       utf8_printf(p->out, "%s", azArg[i]);
14934     }
14935     raw_printf(p->out, "\n");
14936   }else
14937
14938 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14939   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
14940     int i;
14941     int nn = 0;
14942     p->flgProgress = 0;
14943     p->mxProgress = 0;
14944     p->nProgress = 0;
14945     for(i=1; i<nArg; i++){
14946       const char *z = azArg[i];
14947       if( z[0]=='-' ){
14948         z++;
14949         if( z[0]=='-' ) z++;
14950         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
14951           p->flgProgress |= SHELL_PROGRESS_QUIET;
14952           continue;
14953         }
14954         if( strcmp(z,"reset")==0 ){
14955           p->flgProgress |= SHELL_PROGRESS_RESET;
14956           continue;
14957         }
14958         if( strcmp(z,"once")==0 ){
14959           p->flgProgress |= SHELL_PROGRESS_ONCE;
14960           continue;
14961         }
14962         if( strcmp(z,"limit")==0 ){
14963           if( i+1>=nArg ){
14964             utf8_printf(stderr, "Error: missing argument on --limit\n");
14965             rc = 1;
14966             goto meta_command_exit;
14967           }else{
14968             p->mxProgress = (int)integerValue(azArg[++i]);
14969           }
14970           continue;
14971         }
14972         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
14973         rc = 1;
14974         goto meta_command_exit;
14975       }else{
14976         nn = (int)integerValue(z);
14977       }
14978     }
14979     open_db(p, 0);
14980     sqlite3_progress_handler(p->db, nn, progress_handler, p);
14981   }else
14982 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
14983
14984   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
14985     if( nArg >= 2) {
14986       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
14987     }
14988     if( nArg >= 3) {
14989       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
14990     }
14991   }else
14992
14993   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
14994     rc = 2;
14995   }else
14996
14997   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
14998     FILE *inSaved = p->in;
14999     int savedLineno = p->lineno;
15000     if( nArg!=2 ){
15001       raw_printf(stderr, "Usage: .read FILE\n");
15002       rc = 1;
15003       goto meta_command_exit;
15004     }
15005     p->in = fopen(azArg[1], "rb");
15006     if( p->in==0 ){
15007       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
15008       rc = 1;
15009     }else{
15010       rc = process_input(p);
15011       fclose(p->in);
15012     }
15013     p->in = inSaved;
15014     p->lineno = savedLineno;
15015   }else
15016
15017   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
15018     const char *zSrcFile;
15019     const char *zDb;
15020     sqlite3 *pSrc;
15021     sqlite3_backup *pBackup;
15022     int nTimeout = 0;
15023
15024     if( nArg==2 ){
15025       zSrcFile = azArg[1];
15026       zDb = "main";
15027     }else if( nArg==3 ){
15028       zSrcFile = azArg[2];
15029       zDb = azArg[1];
15030     }else{
15031       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
15032       rc = 1;
15033       goto meta_command_exit;
15034     }
15035     rc = sqlite3_open(zSrcFile, &pSrc);
15036     if( rc!=SQLITE_OK ){
15037       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
15038       close_db(pSrc);
15039       return 1;
15040     }
15041     open_db(p, 0);
15042     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
15043     if( pBackup==0 ){
15044       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
15045       close_db(pSrc);
15046       return 1;
15047     }
15048     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
15049           || rc==SQLITE_BUSY  ){
15050       if( rc==SQLITE_BUSY ){
15051         if( nTimeout++ >= 3 ) break;
15052         sqlite3_sleep(100);
15053       }
15054     }
15055     sqlite3_backup_finish(pBackup);
15056     if( rc==SQLITE_DONE ){
15057       rc = 0;
15058     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
15059       raw_printf(stderr, "Error: source database is busy\n");
15060       rc = 1;
15061     }else{
15062       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
15063       rc = 1;
15064     }
15065     close_db(pSrc);
15066   }else
15067
15068   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
15069     if( nArg==2 ){
15070       p->scanstatsOn = (u8)booleanValue(azArg[1]);
15071 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
15072       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
15073 #endif
15074     }else{
15075       raw_printf(stderr, "Usage: .scanstats on|off\n");
15076       rc = 1;
15077     }
15078   }else
15079
15080   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
15081     ShellText sSelect;
15082     ShellState data;
15083     char *zErrMsg = 0;
15084     const char *zDiv = "(";
15085     const char *zName = 0;
15086     int iSchema = 0;
15087     int bDebug = 0;
15088     int ii;
15089
15090     open_db(p, 0);
15091     memcpy(&data, p, sizeof(data));
15092     data.showHeader = 0;
15093     data.cMode = data.mode = MODE_Semi;
15094     initText(&sSelect);
15095     for(ii=1; ii<nArg; ii++){
15096       if( optionMatch(azArg[ii],"indent") ){
15097         data.cMode = data.mode = MODE_Pretty;
15098       }else if( optionMatch(azArg[ii],"debug") ){
15099         bDebug = 1;
15100       }else if( zName==0 ){
15101         zName = azArg[ii];
15102       }else{
15103         raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
15104         rc = 1;
15105         goto meta_command_exit;
15106       }
15107     }
15108     if( zName!=0 ){
15109       int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
15110       if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
15111         char *new_argv[2], *new_colv[2];
15112         new_argv[0] = sqlite3_mprintf(
15113                       "CREATE TABLE %s (\n"
15114                       "  type text,\n"
15115                       "  name text,\n"
15116                       "  tbl_name text,\n"
15117                       "  rootpage integer,\n"
15118                       "  sql text\n"
15119                       ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
15120         new_argv[1] = 0;
15121         new_colv[0] = "sql";
15122         new_colv[1] = 0;
15123         callback(&data, 1, new_argv, new_colv);
15124         sqlite3_free(new_argv[0]);
15125       }
15126     }
15127     if( zDiv ){
15128       sqlite3_stmt *pStmt = 0;
15129       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
15130                               -1, &pStmt, 0);
15131       if( rc ){
15132         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
15133         sqlite3_finalize(pStmt);
15134         rc = 1;
15135         goto meta_command_exit;
15136       }
15137       appendText(&sSelect, "SELECT sql FROM", 0);
15138       iSchema = 0;
15139       while( sqlite3_step(pStmt)==SQLITE_ROW ){
15140         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
15141         char zScNum[30];
15142         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
15143         appendText(&sSelect, zDiv, 0);
15144         zDiv = " UNION ALL ";
15145         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
15146         if( sqlite3_stricmp(zDb, "main")!=0 ){
15147           appendText(&sSelect, zDb, '"');
15148         }else{
15149           appendText(&sSelect, "NULL", 0);
15150         }
15151         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
15152         appendText(&sSelect, zScNum, 0);
15153         appendText(&sSelect, " AS snum, ", 0);
15154         appendText(&sSelect, zDb, '\'');
15155         appendText(&sSelect, " AS sname FROM ", 0);
15156         appendText(&sSelect, zDb, '"');
15157         appendText(&sSelect, ".sqlite_master", 0);
15158       }
15159       sqlite3_finalize(pStmt);
15160 #ifdef SQLITE_INTROSPECTION_PRAGMAS
15161       if( zName ){
15162         appendText(&sSelect,
15163            " UNION ALL SELECT shell_module_schema(name),"
15164            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
15165       }
15166 #endif
15167       appendText(&sSelect, ") WHERE ", 0);
15168       if( zName ){
15169         char *zQarg = sqlite3_mprintf("%Q", zName);
15170         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
15171                     strchr(zName, '[') != 0;
15172         if( strchr(zName, '.') ){
15173           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
15174         }else{
15175           appendText(&sSelect, "lower(tbl_name)", 0);
15176         }
15177         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
15178         appendText(&sSelect, zQarg, 0);
15179         if( !bGlob ){
15180           appendText(&sSelect, " ESCAPE '\\' ", 0);
15181         }
15182         appendText(&sSelect, " AND ", 0);
15183         sqlite3_free(zQarg);
15184       }
15185       appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
15186                            " ORDER BY snum, rowid", 0);
15187       if( bDebug ){
15188         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
15189       }else{
15190         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
15191       }
15192       freeText(&sSelect);
15193     }
15194     if( zErrMsg ){
15195       utf8_printf(stderr,"Error: %s\n", zErrMsg);
15196       sqlite3_free(zErrMsg);
15197       rc = 1;
15198     }else if( rc != SQLITE_OK ){
15199       raw_printf(stderr,"Error: querying schema information\n");
15200       rc = 1;
15201     }else{
15202       rc = 0;
15203     }
15204   }else
15205
15206 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
15207   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
15208     sqlite3SelectTrace = (int)integerValue(azArg[1]);
15209   }else
15210 #endif
15211
15212 #if defined(SQLITE_ENABLE_SESSION)
15213   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
15214     OpenSession *pSession = &p->aSession[0];
15215     char **azCmd = &azArg[1];
15216     int iSes = 0;
15217     int nCmd = nArg - 1;
15218     int i;
15219     if( nArg<=1 ) goto session_syntax_error;
15220     open_db(p, 0);
15221     if( nArg>=3 ){
15222       for(iSes=0; iSes<p->nSession; iSes++){
15223         if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
15224       }
15225       if( iSes<p->nSession ){
15226         pSession = &p->aSession[iSes];
15227         azCmd++;
15228         nCmd--;
15229       }else{
15230         pSession = &p->aSession[0];
15231         iSes = 0;
15232       }
15233     }
15234
15235     /* .session attach TABLE
15236     ** Invoke the sqlite3session_attach() interface to attach a particular
15237     ** table so that it is never filtered.
15238     */
15239     if( strcmp(azCmd[0],"attach")==0 ){
15240       if( nCmd!=2 ) goto session_syntax_error;
15241       if( pSession->p==0 ){
15242         session_not_open:
15243         raw_printf(stderr, "ERROR: No sessions are open\n");
15244       }else{
15245         rc = sqlite3session_attach(pSession->p, azCmd[1]);
15246         if( rc ){
15247           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
15248           rc = 0;
15249         }
15250       }
15251     }else
15252
15253     /* .session changeset FILE
15254     ** .session patchset FILE
15255     ** Write a changeset or patchset into a file.  The file is overwritten.
15256     */
15257     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
15258       FILE *out = 0;
15259       if( nCmd!=2 ) goto session_syntax_error;
15260       if( pSession->p==0 ) goto session_not_open;
15261       out = fopen(azCmd[1], "wb");
15262       if( out==0 ){
15263         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
15264       }else{
15265         int szChng;
15266         void *pChng;
15267         if( azCmd[0][0]=='c' ){
15268           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
15269         }else{
15270           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
15271         }
15272         if( rc ){
15273           printf("Error: error code %d\n", rc);
15274           rc = 0;
15275         }
15276         if( pChng
15277           && fwrite(pChng, szChng, 1, out)!=1 ){
15278           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
15279                   szChng);
15280         }
15281         sqlite3_free(pChng);
15282         fclose(out);
15283       }
15284     }else
15285
15286     /* .session close
15287     ** Close the identified session
15288     */
15289     if( strcmp(azCmd[0], "close")==0 ){
15290       if( nCmd!=1 ) goto session_syntax_error;
15291       if( p->nSession ){
15292         session_close(pSession);
15293         p->aSession[iSes] = p->aSession[--p->nSession];
15294       }
15295     }else
15296
15297     /* .session enable ?BOOLEAN?
15298     ** Query or set the enable flag
15299     */
15300     if( strcmp(azCmd[0], "enable")==0 ){
15301       int ii;
15302       if( nCmd>2 ) goto session_syntax_error;
15303       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15304       if( p->nSession ){
15305         ii = sqlite3session_enable(pSession->p, ii);
15306         utf8_printf(p->out, "session %s enable flag = %d\n",
15307                     pSession->zName, ii);
15308       }
15309     }else
15310
15311     /* .session filter GLOB ....
15312     ** Set a list of GLOB patterns of table names to be excluded.
15313     */
15314     if( strcmp(azCmd[0], "filter")==0 ){
15315       int ii, nByte;
15316       if( nCmd<2 ) goto session_syntax_error;
15317       if( p->nSession ){
15318         for(ii=0; ii<pSession->nFilter; ii++){
15319           sqlite3_free(pSession->azFilter[ii]);
15320         }
15321         sqlite3_free(pSession->azFilter);
15322         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
15323         pSession->azFilter = sqlite3_malloc( nByte );
15324         if( pSession->azFilter==0 ){
15325           raw_printf(stderr, "Error: out or memory\n");
15326           exit(1);
15327         }
15328         for(ii=1; ii<nCmd; ii++){
15329           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
15330         }
15331         pSession->nFilter = ii-1;
15332       }
15333     }else
15334
15335     /* .session indirect ?BOOLEAN?
15336     ** Query or set the indirect flag
15337     */
15338     if( strcmp(azCmd[0], "indirect")==0 ){
15339       int ii;
15340       if( nCmd>2 ) goto session_syntax_error;
15341       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15342       if( p->nSession ){
15343         ii = sqlite3session_indirect(pSession->p, ii);
15344         utf8_printf(p->out, "session %s indirect flag = %d\n",
15345                     pSession->zName, ii);
15346       }
15347     }else
15348
15349     /* .session isempty
15350     ** Determine if the session is empty
15351     */
15352     if( strcmp(azCmd[0], "isempty")==0 ){
15353       int ii;
15354       if( nCmd!=1 ) goto session_syntax_error;
15355       if( p->nSession ){
15356         ii = sqlite3session_isempty(pSession->p);
15357         utf8_printf(p->out, "session %s isempty flag = %d\n",
15358                     pSession->zName, ii);
15359       }
15360     }else
15361
15362     /* .session list
15363     ** List all currently open sessions
15364     */
15365     if( strcmp(azCmd[0],"list")==0 ){
15366       for(i=0; i<p->nSession; i++){
15367         utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
15368       }
15369     }else
15370
15371     /* .session open DB NAME
15372     ** Open a new session called NAME on the attached database DB.
15373     ** DB is normally "main".
15374     */
15375     if( strcmp(azCmd[0],"open")==0 ){
15376       char *zName;
15377       if( nCmd!=3 ) goto session_syntax_error;
15378       zName = azCmd[2];
15379       if( zName[0]==0 ) goto session_syntax_error;
15380       for(i=0; i<p->nSession; i++){
15381         if( strcmp(p->aSession[i].zName,zName)==0 ){
15382           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
15383           goto meta_command_exit;
15384         }
15385       }
15386       if( p->nSession>=ArraySize(p->aSession) ){
15387         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
15388         goto meta_command_exit;
15389       }
15390       pSession = &p->aSession[p->nSession];
15391       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
15392       if( rc ){
15393         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
15394         rc = 0;
15395         goto meta_command_exit;
15396       }
15397       pSession->nFilter = 0;
15398       sqlite3session_table_filter(pSession->p, session_filter, pSession);
15399       p->nSession++;
15400       pSession->zName = sqlite3_mprintf("%s", zName);
15401     }else
15402     /* If no command name matches, show a syntax error */
15403     session_syntax_error:
15404     showHelp(p->out, "session");
15405   }else
15406 #endif
15407
15408 #ifdef SQLITE_DEBUG
15409   /* Undocumented commands for internal testing.  Subject to change
15410   ** without notice. */
15411   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
15412     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
15413       int i, v;
15414       for(i=1; i<nArg; i++){
15415         v = booleanValue(azArg[i]);
15416         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
15417       }
15418     }
15419     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
15420       int i; sqlite3_int64 v;
15421       for(i=1; i<nArg; i++){
15422         char zBuf[200];
15423         v = integerValue(azArg[i]);
15424         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
15425         utf8_printf(p->out, "%s", zBuf);
15426       }
15427     }
15428   }else
15429 #endif
15430
15431   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
15432     int bIsInit = 0;         /* True to initialize the SELFTEST table */
15433     int bVerbose = 0;        /* Verbose output */
15434     int bSelftestExists;     /* True if SELFTEST already exists */
15435     int i, k;                /* Loop counters */
15436     int nTest = 0;           /* Number of tests runs */
15437     int nErr = 0;            /* Number of errors seen */
15438     ShellText str;           /* Answer for a query */
15439     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
15440
15441     open_db(p,0);
15442     for(i=1; i<nArg; i++){
15443       const char *z = azArg[i];
15444       if( z[0]=='-' && z[1]=='-' ) z++;
15445       if( strcmp(z,"-init")==0 ){
15446         bIsInit = 1;
15447       }else
15448       if( strcmp(z,"-v")==0 ){
15449         bVerbose++;
15450       }else
15451       {
15452         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15453                     azArg[i], azArg[0]);
15454         raw_printf(stderr, "Should be one of: --init -v\n");
15455         rc = 1;
15456         goto meta_command_exit;
15457       }
15458     }
15459     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
15460            != SQLITE_OK ){
15461       bSelftestExists = 0;
15462     }else{
15463       bSelftestExists = 1;
15464     }
15465     if( bIsInit ){
15466       createSelftestTable(p);
15467       bSelftestExists = 1;
15468     }
15469     initText(&str);
15470     appendText(&str, "x", 0);
15471     for(k=bSelftestExists; k>=0; k--){
15472       if( k==1 ){
15473         rc = sqlite3_prepare_v2(p->db,
15474             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
15475             -1, &pStmt, 0);
15476       }else{
15477         rc = sqlite3_prepare_v2(p->db,
15478           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
15479           "      (1,'run','PRAGMA integrity_check','ok')",
15480           -1, &pStmt, 0);
15481       }
15482       if( rc ){
15483         raw_printf(stderr, "Error querying the selftest table\n");
15484         rc = 1;
15485         sqlite3_finalize(pStmt);
15486         goto meta_command_exit;
15487       }
15488       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
15489         int tno = sqlite3_column_int(pStmt, 0);
15490         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
15491         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
15492         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
15493
15494         k = 0;
15495         if( bVerbose>0 ){
15496           char *zQuote = sqlite3_mprintf("%q", zSql);
15497           printf("%d: %s %s\n", tno, zOp, zSql);
15498           sqlite3_free(zQuote);
15499         }
15500         if( strcmp(zOp,"memo")==0 ){
15501           utf8_printf(p->out, "%s\n", zSql);
15502         }else
15503         if( strcmp(zOp,"run")==0 ){
15504           char *zErrMsg = 0;
15505           str.n = 0;
15506           str.z[0] = 0;
15507           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
15508           nTest++;
15509           if( bVerbose ){
15510             utf8_printf(p->out, "Result: %s\n", str.z);
15511           }
15512           if( rc || zErrMsg ){
15513             nErr++;
15514             rc = 1;
15515             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
15516             sqlite3_free(zErrMsg);
15517           }else if( strcmp(zAns,str.z)!=0 ){
15518             nErr++;
15519             rc = 1;
15520             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
15521             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
15522           }
15523         }else
15524         {
15525           utf8_printf(stderr,
15526             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
15527           rc = 1;
15528           break;
15529         }
15530       } /* End loop over rows of content from SELFTEST */
15531       sqlite3_finalize(pStmt);
15532     } /* End loop over k */
15533     freeText(&str);
15534     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
15535   }else
15536
15537   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
15538     if( nArg<2 || nArg>3 ){
15539       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
15540       rc = 1;
15541     }
15542     if( nArg>=2 ){
15543       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
15544                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
15545     }
15546     if( nArg>=3 ){
15547       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
15548                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
15549     }
15550   }else
15551
15552   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
15553     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
15554     int i;                   /* Loop counter */
15555     int bSchema = 0;         /* Also hash the schema */
15556     int bSeparate = 0;       /* Hash each table separately */
15557     int iSize = 224;         /* Hash algorithm to use */
15558     int bDebug = 0;          /* Only show the query that would have run */
15559     sqlite3_stmt *pStmt;     /* For querying tables names */
15560     char *zSql;              /* SQL to be run */
15561     char *zSep;              /* Separator */
15562     ShellText sSql;          /* Complete SQL for the query to run the hash */
15563     ShellText sQuery;        /* Set of queries used to read all content */
15564     open_db(p, 0);
15565     for(i=1; i<nArg; i++){
15566       const char *z = azArg[i];
15567       if( z[0]=='-' ){
15568         z++;
15569         if( z[0]=='-' ) z++;
15570         if( strcmp(z,"schema")==0 ){
15571           bSchema = 1;
15572         }else
15573         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
15574          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
15575         ){
15576           iSize = atoi(&z[5]);
15577         }else
15578         if( strcmp(z,"debug")==0 ){
15579           bDebug = 1;
15580         }else
15581         {
15582           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15583                       azArg[i], azArg[0]);
15584           raw_printf(stderr, "Should be one of: --schema"
15585                              " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
15586           rc = 1;
15587           goto meta_command_exit;
15588         }
15589       }else if( zLike ){
15590         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
15591         rc = 1;
15592         goto meta_command_exit;
15593       }else{
15594         zLike = z;
15595         bSeparate = 1;
15596         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
15597       }
15598     }
15599     if( bSchema ){
15600       zSql = "SELECT lower(name) FROM sqlite_master"
15601              " WHERE type='table' AND coalesce(rootpage,0)>1"
15602              " UNION ALL SELECT 'sqlite_master'"
15603              " ORDER BY 1 collate nocase";
15604     }else{
15605       zSql = "SELECT lower(name) FROM sqlite_master"
15606              " WHERE type='table' AND coalesce(rootpage,0)>1"
15607              " AND name NOT LIKE 'sqlite_%'"
15608              " ORDER BY 1 collate nocase";
15609     }
15610     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15611     initText(&sQuery);
15612     initText(&sSql);
15613     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
15614     zSep = "VALUES(";
15615     while( SQLITE_ROW==sqlite3_step(pStmt) ){
15616       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
15617       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
15618       if( strncmp(zTab, "sqlite_",7)!=0 ){
15619         appendText(&sQuery,"SELECT * FROM ", 0);
15620         appendText(&sQuery,zTab,'"');
15621         appendText(&sQuery," NOT INDEXED;", 0);
15622       }else if( strcmp(zTab, "sqlite_master")==0 ){
15623         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
15624                            " ORDER BY name;", 0);
15625       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
15626         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
15627                            " ORDER BY name;", 0);
15628       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
15629         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
15630                            " ORDER BY tbl,idx;", 0);
15631       }else if( strcmp(zTab, "sqlite_stat3")==0
15632              || strcmp(zTab, "sqlite_stat4")==0 ){
15633         appendText(&sQuery, "SELECT * FROM ", 0);
15634         appendText(&sQuery, zTab, 0);
15635         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
15636       }
15637       appendText(&sSql, zSep, 0);
15638       appendText(&sSql, sQuery.z, '\'');
15639       sQuery.n = 0;
15640       appendText(&sSql, ",", 0);
15641       appendText(&sSql, zTab, '\'');
15642       zSep = "),(";
15643     }
15644     sqlite3_finalize(pStmt);
15645     if( bSeparate ){
15646       zSql = sqlite3_mprintf(
15647           "%s))"
15648           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
15649           "   FROM [sha3sum$query]",
15650           sSql.z, iSize);
15651     }else{
15652       zSql = sqlite3_mprintf(
15653           "%s))"
15654           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
15655           "   FROM [sha3sum$query]",
15656           sSql.z, iSize);
15657     }
15658     freeText(&sQuery);
15659     freeText(&sSql);
15660     if( bDebug ){
15661       utf8_printf(p->out, "%s\n", zSql);
15662     }else{
15663       shell_exec(p, zSql, 0);
15664     }
15665     sqlite3_free(zSql);
15666   }else
15667
15668 #ifndef SQLITE_NOHAVE_SYSTEM
15669   if( c=='s'
15670    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
15671   ){
15672     char *zCmd;
15673     int i, x;
15674     if( nArg<2 ){
15675       raw_printf(stderr, "Usage: .system COMMAND\n");
15676       rc = 1;
15677       goto meta_command_exit;
15678     }
15679     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
15680     for(i=2; i<nArg; i++){
15681       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
15682                              zCmd, azArg[i]);
15683     }
15684     x = system(zCmd);
15685     sqlite3_free(zCmd);
15686     if( x ) raw_printf(stderr, "System command returns %d\n", x);
15687   }else
15688 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15689
15690   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
15691     static const char *azBool[] = { "off", "on", "trigger", "full"};
15692     int i;
15693     if( nArg!=1 ){
15694       raw_printf(stderr, "Usage: .show\n");
15695       rc = 1;
15696       goto meta_command_exit;
15697     }
15698     utf8_printf(p->out, "%12.12s: %s\n","echo",
15699                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
15700     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
15701     utf8_printf(p->out, "%12.12s: %s\n","explain",
15702          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
15703     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
15704     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
15705     utf8_printf(p->out, "%12.12s: ", "nullvalue");
15706       output_c_string(p->out, p->nullValue);
15707       raw_printf(p->out, "\n");
15708     utf8_printf(p->out,"%12.12s: %s\n","output",
15709             strlen30(p->outfile) ? p->outfile : "stdout");
15710     utf8_printf(p->out,"%12.12s: ", "colseparator");
15711       output_c_string(p->out, p->colSeparator);
15712       raw_printf(p->out, "\n");
15713     utf8_printf(p->out,"%12.12s: ", "rowseparator");
15714       output_c_string(p->out, p->rowSeparator);
15715       raw_printf(p->out, "\n");
15716     utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
15717     utf8_printf(p->out, "%12.12s: ", "width");
15718     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
15719       raw_printf(p->out, "%d ", p->colWidth[i]);
15720     }
15721     raw_printf(p->out, "\n");
15722     utf8_printf(p->out, "%12.12s: %s\n", "filename",
15723                 p->zDbFilename ? p->zDbFilename : "");
15724   }else
15725
15726   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
15727     if( nArg==2 ){
15728       p->statsOn = (u8)booleanValue(azArg[1]);
15729     }else if( nArg==1 ){
15730       display_stats(p->db, p, 0);
15731     }else{
15732       raw_printf(stderr, "Usage: .stats ?on|off?\n");
15733       rc = 1;
15734     }
15735   }else
15736
15737   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
15738    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
15739                  || strncmp(azArg[0], "indexes", n)==0) )
15740   ){
15741     sqlite3_stmt *pStmt;
15742     char **azResult;
15743     int nRow, nAlloc;
15744     int ii;
15745     ShellText s;
15746     initText(&s);
15747     open_db(p, 0);
15748     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
15749     if( rc ){
15750       sqlite3_finalize(pStmt);
15751       return shellDatabaseError(p->db);
15752     }
15753
15754     if( nArg>2 && c=='i' ){
15755       /* It is an historical accident that the .indexes command shows an error
15756       ** when called with the wrong number of arguments whereas the .tables
15757       ** command does not. */
15758       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
15759       rc = 1;
15760       sqlite3_finalize(pStmt);
15761       goto meta_command_exit;
15762     }
15763     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
15764       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
15765       if( zDbName==0 ) continue;
15766       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
15767       if( sqlite3_stricmp(zDbName, "main")==0 ){
15768         appendText(&s, "SELECT name FROM ", 0);
15769       }else{
15770         appendText(&s, "SELECT ", 0);
15771         appendText(&s, zDbName, '\'');
15772         appendText(&s, "||'.'||name FROM ", 0);
15773       }
15774       appendText(&s, zDbName, '"');
15775       appendText(&s, ".sqlite_master ", 0);
15776       if( c=='t' ){
15777         appendText(&s," WHERE type IN ('table','view')"
15778                       "   AND name NOT LIKE 'sqlite_%'"
15779                       "   AND name LIKE ?1", 0);
15780       }else{
15781         appendText(&s," WHERE type='index'"
15782                       "   AND tbl_name LIKE ?1", 0);
15783       }
15784     }
15785     rc = sqlite3_finalize(pStmt);
15786     appendText(&s, " ORDER BY 1", 0);
15787     rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
15788     freeText(&s);
15789     if( rc ) return shellDatabaseError(p->db);
15790
15791     /* Run the SQL statement prepared by the above block. Store the results
15792     ** as an array of nul-terminated strings in azResult[].  */
15793     nRow = nAlloc = 0;
15794     azResult = 0;
15795     if( nArg>1 ){
15796       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
15797     }else{
15798       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
15799     }
15800     while( sqlite3_step(pStmt)==SQLITE_ROW ){
15801       if( nRow>=nAlloc ){
15802         char **azNew;
15803         int n2 = nAlloc*2 + 10;
15804         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
15805         if( azNew==0 ) shell_out_of_memory();
15806         nAlloc = n2;
15807         azResult = azNew;
15808       }
15809       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
15810       if( 0==azResult[nRow] ) shell_out_of_memory();
15811       nRow++;
15812     }
15813     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
15814       rc = shellDatabaseError(p->db);
15815     }
15816
15817     /* Pretty-print the contents of array azResult[] to the output */
15818     if( rc==0 && nRow>0 ){
15819       int len, maxlen = 0;
15820       int i, j;
15821       int nPrintCol, nPrintRow;
15822       for(i=0; i<nRow; i++){
15823         len = strlen30(azResult[i]);
15824         if( len>maxlen ) maxlen = len;
15825       }
15826       nPrintCol = 80/(maxlen+2);
15827       if( nPrintCol<1 ) nPrintCol = 1;
15828       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
15829       for(i=0; i<nPrintRow; i++){
15830         for(j=i; j<nRow; j+=nPrintRow){
15831           char *zSp = j<nPrintRow ? "" : "  ";
15832           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
15833                       azResult[j] ? azResult[j]:"");
15834         }
15835         raw_printf(p->out, "\n");
15836       }
15837     }
15838
15839     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
15840     sqlite3_free(azResult);
15841   }else
15842
15843   /* Begin redirecting output to the file "testcase-out.txt" */
15844   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
15845     output_reset(p);
15846     p->out = output_file_open("testcase-out.txt", 0);
15847     if( p->out==0 ){
15848       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
15849     }
15850     if( nArg>=2 ){
15851       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
15852     }else{
15853       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
15854     }
15855   }else
15856
15857 #ifndef SQLITE_UNTESTABLE
15858   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
15859     static const struct {
15860        const char *zCtrlName;   /* Name of a test-control option */
15861        int ctrlCode;            /* Integer code for that option */
15862        const char *zUsage;      /* Usage notes */
15863     } aCtrl[] = {
15864       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
15865       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
15866     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
15867     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
15868       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
15869     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
15870       { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
15871       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"       },
15872       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
15873       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
15874       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
15875 #ifdef YYCOVERAGE
15876       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
15877 #endif
15878       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
15879       { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
15880       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
15881       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
15882       { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
15883     };
15884     int testctrl = -1;
15885     int iCtrl = -1;
15886     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
15887     int isOk = 0;
15888     int i, n2;
15889     const char *zCmd = 0;
15890
15891     open_db(p, 0);
15892     zCmd = nArg>=2 ? azArg[1] : "help";
15893
15894     /* The argument can optionally begin with "-" or "--" */
15895     if( zCmd[0]=='-' && zCmd[1] ){
15896       zCmd++;
15897       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15898     }
15899
15900     /* --help lists all test-controls */
15901     if( strcmp(zCmd,"help")==0 ){
15902       utf8_printf(p->out, "Available test-controls:\n");
15903       for(i=0; i<ArraySize(aCtrl); i++){
15904         utf8_printf(p->out, "  .testctrl %s %s\n",
15905                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15906       }
15907       rc = 1;
15908       goto meta_command_exit;
15909     }
15910
15911     /* convert testctrl text option to value. allow any unique prefix
15912     ** of the option name, or a numerical value. */
15913     n2 = strlen30(zCmd);
15914     for(i=0; i<ArraySize(aCtrl); i++){
15915       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15916         if( testctrl<0 ){
15917           testctrl = aCtrl[i].ctrlCode;
15918           iCtrl = i;
15919         }else{
15920           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
15921                               "Use \".testctrl --help\" for help\n", zCmd);
15922           rc = 1;
15923           goto meta_command_exit;
15924         }
15925       }
15926     }
15927     if( testctrl<0 ){
15928       utf8_printf(stderr,"Error: unknown test-control: %s\n"
15929                          "Use \".testctrl --help\" for help\n", zCmd);
15930     }else{
15931       switch(testctrl){
15932
15933         /* sqlite3_test_control(int, db, int) */
15934         case SQLITE_TESTCTRL_OPTIMIZATIONS:
15935         case SQLITE_TESTCTRL_RESERVE:
15936           if( nArg==3 ){
15937             int opt = (int)strtol(azArg[2], 0, 0);
15938             rc2 = sqlite3_test_control(testctrl, p->db, opt);
15939             isOk = 3;
15940           }
15941           break;
15942
15943         /* sqlite3_test_control(int) */
15944         case SQLITE_TESTCTRL_PRNG_SAVE:
15945         case SQLITE_TESTCTRL_PRNG_RESTORE:
15946         case SQLITE_TESTCTRL_PRNG_RESET:
15947         case SQLITE_TESTCTRL_BYTEORDER:
15948           if( nArg==2 ){
15949             rc2 = sqlite3_test_control(testctrl);
15950             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
15951           }
15952           break;
15953
15954         /* sqlite3_test_control(int, uint) */
15955         case SQLITE_TESTCTRL_PENDING_BYTE:
15956           if( nArg==3 ){
15957             unsigned int opt = (unsigned int)integerValue(azArg[2]);
15958             rc2 = sqlite3_test_control(testctrl, opt);
15959             isOk = 3;
15960           }
15961           break;
15962
15963         /* sqlite3_test_control(int, int) */
15964         case SQLITE_TESTCTRL_ASSERT:
15965         case SQLITE_TESTCTRL_ALWAYS:
15966         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
15967           if( nArg==3 ){
15968             int opt = booleanValue(azArg[2]);
15969             rc2 = sqlite3_test_control(testctrl, opt);
15970             isOk = 1;
15971           }
15972           break;
15973
15974         /* sqlite3_test_control(int, int) */
15975         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
15976         case SQLITE_TESTCTRL_NEVER_CORRUPT:
15977           if( nArg==3 ){
15978             int opt = booleanValue(azArg[2]);
15979             rc2 = sqlite3_test_control(testctrl, opt);
15980             isOk = 3;
15981           }
15982           break;
15983
15984         case SQLITE_TESTCTRL_IMPOSTER:
15985           if( nArg==5 ){
15986             rc2 = sqlite3_test_control(testctrl, p->db,
15987                           azArg[2],
15988                           integerValue(azArg[3]),
15989                           integerValue(azArg[4]));
15990             isOk = 3;
15991           }
15992           break;
15993
15994 #ifdef YYCOVERAGE
15995         case SQLITE_TESTCTRL_PARSER_COVERAGE:
15996           if( nArg==2 ){
15997             sqlite3_test_control(testctrl, p->out);
15998             isOk = 3;
15999           }
16000 #endif
16001       }
16002     }
16003     if( isOk==0 && iCtrl>=0 ){
16004       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
16005       rc = 1;
16006     }else if( isOk==1 ){
16007       raw_printf(p->out, "%d\n", rc2);
16008     }else if( isOk==2 ){
16009       raw_printf(p->out, "0x%08x\n", rc2);
16010     }
16011   }else
16012 #endif /* !defined(SQLITE_UNTESTABLE) */
16013
16014   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
16015     open_db(p, 0);
16016     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
16017   }else
16018
16019   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
16020     if( nArg==2 ){
16021       enableTimer = booleanValue(azArg[1]);
16022       if( enableTimer && !HAS_TIMER ){
16023         raw_printf(stderr, "Error: timer not available on this system.\n");
16024         enableTimer = 0;
16025       }
16026     }else{
16027       raw_printf(stderr, "Usage: .timer on|off\n");
16028       rc = 1;
16029     }
16030   }else
16031
16032 #ifndef SQLITE_OMIT_TRACE
16033   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
16034     int mType = 0;
16035     int jj;
16036     open_db(p, 0);
16037     for(jj=1; jj<nArg; jj++){
16038       const char *z = azArg[jj];
16039       if( z[0]=='-' ){
16040         if( optionMatch(z, "expanded") ){
16041           p->eTraceType = SHELL_TRACE_EXPANDED;
16042         }
16043 #ifdef SQLITE_ENABLE_NORMALIZE
16044         else if( optionMatch(z, "normalized") ){
16045           p->eTraceType = SHELL_TRACE_NORMALIZED;
16046         }
16047 #endif
16048         else if( optionMatch(z, "plain") ){
16049           p->eTraceType = SHELL_TRACE_PLAIN;
16050         }
16051         else if( optionMatch(z, "profile") ){
16052           mType |= SQLITE_TRACE_PROFILE;
16053         }
16054         else if( optionMatch(z, "row") ){
16055           mType |= SQLITE_TRACE_ROW;
16056         }
16057         else if( optionMatch(z, "stmt") ){
16058           mType |= SQLITE_TRACE_STMT;
16059         }
16060         else if( optionMatch(z, "close") ){
16061           mType |= SQLITE_TRACE_CLOSE;
16062         }
16063         else {
16064           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
16065           rc = 1;
16066           goto meta_command_exit;
16067         }
16068       }else{
16069         output_file_close(p->traceOut);
16070         p->traceOut = output_file_open(azArg[1], 0);
16071       }
16072     }
16073     if( p->traceOut==0 ){
16074       sqlite3_trace_v2(p->db, 0, 0, 0);
16075     }else{
16076       if( mType==0 ) mType = SQLITE_TRACE_STMT;
16077       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
16078     }
16079   }else
16080 #endif /* !defined(SQLITE_OMIT_TRACE) */
16081
16082 #if SQLITE_USER_AUTHENTICATION
16083   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
16084     if( nArg<2 ){
16085       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
16086       rc = 1;
16087       goto meta_command_exit;
16088     }
16089     open_db(p, 0);
16090     if( strcmp(azArg[1],"login")==0 ){
16091       if( nArg!=4 ){
16092         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
16093         rc = 1;
16094         goto meta_command_exit;
16095       }
16096       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
16097       if( rc ){
16098         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
16099         rc = 1;
16100       }
16101     }else if( strcmp(azArg[1],"add")==0 ){
16102       if( nArg!=5 ){
16103         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
16104         rc = 1;
16105         goto meta_command_exit;
16106       }
16107       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
16108                             booleanValue(azArg[4]));
16109       if( rc ){
16110         raw_printf(stderr, "User-Add failed: %d\n", rc);
16111         rc = 1;
16112       }
16113     }else if( strcmp(azArg[1],"edit")==0 ){
16114       if( nArg!=5 ){
16115         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
16116         rc = 1;
16117         goto meta_command_exit;
16118       }
16119       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
16120                               booleanValue(azArg[4]));
16121       if( rc ){
16122         raw_printf(stderr, "User-Edit failed: %d\n", rc);
16123         rc = 1;
16124       }
16125     }else if( strcmp(azArg[1],"delete")==0 ){
16126       if( nArg!=3 ){
16127         raw_printf(stderr, "Usage: .user delete USER\n");
16128         rc = 1;
16129         goto meta_command_exit;
16130       }
16131       rc = sqlite3_user_delete(p->db, azArg[2]);
16132       if( rc ){
16133         raw_printf(stderr, "User-Delete failed: %d\n", rc);
16134         rc = 1;
16135       }
16136     }else{
16137       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
16138       rc = 1;
16139       goto meta_command_exit;
16140     }
16141   }else
16142 #endif /* SQLITE_USER_AUTHENTICATION */
16143
16144   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
16145     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
16146         sqlite3_libversion(), sqlite3_sourceid());
16147 #if SQLITE_HAVE_ZLIB
16148     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
16149 #endif
16150 #define CTIMEOPT_VAL_(opt) #opt
16151 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
16152 #if defined(__clang__) && defined(__clang_major__)
16153     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
16154                     CTIMEOPT_VAL(__clang_minor__) "."
16155                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
16156 #elif defined(_MSC_VER)
16157     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
16158 #elif defined(__GNUC__) && defined(__VERSION__)
16159     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
16160 #endif
16161   }else
16162
16163   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
16164     const char *zDbName = nArg==2 ? azArg[1] : "main";
16165     sqlite3_vfs *pVfs = 0;
16166     if( p->db ){
16167       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
16168       if( pVfs ){
16169         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
16170         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
16171         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
16172         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
16173       }
16174     }
16175   }else
16176
16177   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
16178     sqlite3_vfs *pVfs;
16179     sqlite3_vfs *pCurrent = 0;
16180     if( p->db ){
16181       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
16182     }
16183     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
16184       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
16185            pVfs==pCurrent ? "  <--- CURRENT" : "");
16186       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
16187       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
16188       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
16189       if( pVfs->pNext ){
16190         raw_printf(p->out, "-----------------------------------\n");
16191       }
16192     }
16193   }else
16194
16195   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
16196     const char *zDbName = nArg==2 ? azArg[1] : "main";
16197     char *zVfsName = 0;
16198     if( p->db ){
16199       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
16200       if( zVfsName ){
16201         utf8_printf(p->out, "%s\n", zVfsName);
16202         sqlite3_free(zVfsName);
16203       }
16204     }
16205   }else
16206
16207 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
16208   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
16209     sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
16210   }else
16211 #endif
16212
16213   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
16214     int j;
16215     assert( nArg<=ArraySize(azArg) );
16216     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
16217       p->colWidth[j-1] = (int)integerValue(azArg[j]);
16218     }
16219   }else
16220
16221   {
16222     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
16223       " \"%s\". Enter \".help\" for help\n", azArg[0]);
16224     rc = 1;
16225   }
16226
16227 meta_command_exit:
16228   if( p->outCount ){
16229     p->outCount--;
16230     if( p->outCount==0 ) output_reset(p);
16231   }
16232   return rc;
16233 }
16234
16235 /*
16236 ** Return TRUE if a semicolon occurs anywhere in the first N characters
16237 ** of string z[].
16238 */
16239 static int line_contains_semicolon(const char *z, int N){
16240   int i;
16241   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
16242   return 0;
16243 }
16244
16245 /*
16246 ** Test to see if a line consists entirely of whitespace.
16247 */
16248 static int _all_whitespace(const char *z){
16249   for(; *z; z++){
16250     if( IsSpace(z[0]) ) continue;
16251     if( *z=='/' && z[1]=='*' ){
16252       z += 2;
16253       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
16254       if( *z==0 ) return 0;
16255       z++;
16256       continue;
16257     }
16258     if( *z=='-' && z[1]=='-' ){
16259       z += 2;
16260       while( *z && *z!='\n' ){ z++; }
16261       if( *z==0 ) return 1;
16262       continue;
16263     }
16264     return 0;
16265   }
16266   return 1;
16267 }
16268
16269 /*
16270 ** Return TRUE if the line typed in is an SQL command terminator other
16271 ** than a semi-colon.  The SQL Server style "go" command is understood
16272 ** as is the Oracle "/".
16273 */
16274 static int line_is_command_terminator(const char *zLine){
16275   while( IsSpace(zLine[0]) ){ zLine++; };
16276   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
16277     return 1;  /* Oracle */
16278   }
16279   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
16280          && _all_whitespace(&zLine[2]) ){
16281     return 1;  /* SQL Server */
16282   }
16283   return 0;
16284 }
16285
16286 /*
16287 ** We need a default sqlite3_complete() implementation to use in case
16288 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
16289 ** any arbitrary text is a complete SQL statement.  This is not very
16290 ** user-friendly, but it does seem to work.
16291 */
16292 #ifdef SQLITE_OMIT_COMPLETE
16293 #define sqlite3_complete(x) 1
16294 #endif
16295
16296 /*
16297 ** Return true if zSql is a complete SQL statement.  Return false if it
16298 ** ends in the middle of a string literal or C-style comment.
16299 */
16300 static int line_is_complete(char *zSql, int nSql){
16301   int rc;
16302   if( zSql==0 ) return 1;
16303   zSql[nSql] = ';';
16304   zSql[nSql+1] = 0;
16305   rc = sqlite3_complete(zSql);
16306   zSql[nSql] = 0;
16307   return rc;
16308 }
16309
16310 /*
16311 ** Run a single line of SQL.  Return the number of errors.
16312 */
16313 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
16314   int rc;
16315   char *zErrMsg = 0;
16316
16317   open_db(p, 0);
16318   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
16319   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
16320   BEGIN_TIMER;
16321   rc = shell_exec(p, zSql, &zErrMsg);
16322   END_TIMER;
16323   if( rc || zErrMsg ){
16324     char zPrefix[100];
16325     if( in!=0 || !stdin_is_interactive ){
16326       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
16327                        "Error: near line %d:", startline);
16328     }else{
16329       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
16330     }
16331     if( zErrMsg!=0 ){
16332       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
16333       sqlite3_free(zErrMsg);
16334       zErrMsg = 0;
16335     }else{
16336       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
16337     }
16338     return 1;
16339   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
16340     raw_printf(p->out, "changes: %3d   total_changes: %d\n",
16341             sqlite3_changes(p->db), sqlite3_total_changes(p->db));
16342   }
16343   return 0;
16344 }
16345
16346
16347 /*
16348 ** Read input from *in and process it.  If *in==0 then input
16349 ** is interactive - the user is typing it it.  Otherwise, input
16350 ** is coming from a file or device.  A prompt is issued and history
16351 ** is saved only if input is interactive.  An interrupt signal will
16352 ** cause this routine to exit immediately, unless input is interactive.
16353 **
16354 ** Return the number of errors.
16355 */
16356 static int process_input(ShellState *p){
16357   char *zLine = 0;          /* A single input line */
16358   char *zSql = 0;           /* Accumulated SQL text */
16359   int nLine;                /* Length of current line */
16360   int nSql = 0;             /* Bytes of zSql[] used */
16361   int nAlloc = 0;           /* Allocated zSql[] space */
16362   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
16363   int rc;                   /* Error code */
16364   int errCnt = 0;           /* Number of errors seen */
16365   int startline = 0;        /* Line number for start of current input */
16366
16367   p->lineno = 0;
16368   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
16369     fflush(p->out);
16370     zLine = one_input_line(p->in, zLine, nSql>0);
16371     if( zLine==0 ){
16372       /* End of input */
16373       if( p->in==0 && stdin_is_interactive ) printf("\n");
16374       break;
16375     }
16376     if( seenInterrupt ){
16377       if( p->in!=0 ) break;
16378       seenInterrupt = 0;
16379     }
16380     p->lineno++;
16381     if( nSql==0 && _all_whitespace(zLine) ){
16382       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16383       continue;
16384     }
16385     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
16386       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16387       if( zLine[0]=='.' ){
16388         rc = do_meta_command(zLine, p);
16389         if( rc==2 ){ /* exit requested */
16390           break;
16391         }else if( rc ){
16392           errCnt++;
16393         }
16394       }
16395       continue;
16396     }
16397     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
16398       memcpy(zLine,";",2);
16399     }
16400     nLine = strlen30(zLine);
16401     if( nSql+nLine+2>=nAlloc ){
16402       nAlloc = nSql+nLine+100;
16403       zSql = realloc(zSql, nAlloc);
16404       if( zSql==0 ) shell_out_of_memory();
16405     }
16406     nSqlPrior = nSql;
16407     if( nSql==0 ){
16408       int i;
16409       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
16410       assert( nAlloc>0 && zSql!=0 );
16411       memcpy(zSql, zLine+i, nLine+1-i);
16412       startline = p->lineno;
16413       nSql = nLine-i;
16414     }else{
16415       zSql[nSql++] = '\n';
16416       memcpy(zSql+nSql, zLine, nLine+1);
16417       nSql += nLine;
16418     }
16419     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
16420                 && sqlite3_complete(zSql) ){
16421       errCnt += runOneSqlLine(p, zSql, p->in, startline);
16422       nSql = 0;
16423       if( p->outCount ){
16424         output_reset(p);
16425         p->outCount = 0;
16426       }else{
16427         clearTempFile(p);
16428       }
16429     }else if( nSql && _all_whitespace(zSql) ){
16430       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
16431       nSql = 0;
16432     }
16433   }
16434   if( nSql && !_all_whitespace(zSql) ){
16435     errCnt += runOneSqlLine(p, zSql, p->in, startline);
16436   }
16437   free(zSql);
16438   free(zLine);
16439   return errCnt>0;
16440 }
16441
16442 /*
16443 ** Return a pathname which is the user's home directory.  A
16444 ** 0 return indicates an error of some kind.
16445 */
16446 static char *find_home_dir(int clearFlag){
16447   static char *home_dir = NULL;
16448   if( clearFlag ){
16449     free(home_dir);
16450     home_dir = 0;
16451     return 0;
16452   }
16453   if( home_dir ) return home_dir;
16454
16455 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
16456      && !defined(__RTP__) && !defined(_WRS_KERNEL)
16457   {
16458     struct passwd *pwent;
16459     uid_t uid = getuid();
16460     if( (pwent=getpwuid(uid)) != NULL) {
16461       home_dir = pwent->pw_dir;
16462     }
16463   }
16464 #endif
16465
16466 #if defined(_WIN32_WCE)
16467   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
16468    */
16469   home_dir = "/";
16470 #else
16471
16472 #if defined(_WIN32) || defined(WIN32)
16473   if (!home_dir) {
16474     home_dir = getenv("USERPROFILE");
16475   }
16476 #endif
16477
16478   if (!home_dir) {
16479     home_dir = getenv("HOME");
16480   }
16481
16482 #if defined(_WIN32) || defined(WIN32)
16483   if (!home_dir) {
16484     char *zDrive, *zPath;
16485     int n;
16486     zDrive = getenv("HOMEDRIVE");
16487     zPath = getenv("HOMEPATH");
16488     if( zDrive && zPath ){
16489       n = strlen30(zDrive) + strlen30(zPath) + 1;
16490       home_dir = malloc( n );
16491       if( home_dir==0 ) return 0;
16492       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
16493       return home_dir;
16494     }
16495     home_dir = "c:\\";
16496   }
16497 #endif
16498
16499 #endif /* !_WIN32_WCE */
16500
16501   if( home_dir ){
16502     int n = strlen30(home_dir) + 1;
16503     char *z = malloc( n );
16504     if( z ) memcpy(z, home_dir, n);
16505     home_dir = z;
16506   }
16507
16508   return home_dir;
16509 }
16510
16511 /*
16512 ** Read input from the file given by sqliterc_override.  Or if that
16513 ** parameter is NULL, take input from ~/.sqliterc
16514 **
16515 ** Returns the number of errors.
16516 */
16517 static void process_sqliterc(
16518   ShellState *p,                  /* Configuration data */
16519   const char *sqliterc_override   /* Name of config file. NULL to use default */
16520 ){
16521   char *home_dir = NULL;
16522   const char *sqliterc = sqliterc_override;
16523   char *zBuf = 0;
16524   FILE *inSaved = p->in;
16525   int savedLineno = p->lineno;
16526
16527   if (sqliterc == NULL) {
16528     home_dir = find_home_dir(0);
16529     if( home_dir==0 ){
16530       raw_printf(stderr, "-- warning: cannot find home directory;"
16531                       " cannot read ~/.sqliterc\n");
16532       return;
16533     }
16534     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
16535     sqliterc = zBuf;
16536   }
16537   p->in = fopen(sqliterc,"rb");
16538   if( p->in ){
16539     if( stdin_is_interactive ){
16540       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
16541     }
16542     process_input(p);
16543     fclose(p->in);
16544   }
16545   p->in = inSaved;
16546   p->lineno = savedLineno;
16547   sqlite3_free(zBuf);
16548 }
16549
16550 /*
16551 ** Show available command line options
16552 */
16553 static const char zOptions[] =
16554 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
16555   "   -A ARGS...           run \".archive ARGS\" and exit\n"
16556 #endif
16557   "   -append              append the database to the end of the file\n"
16558   "   -ascii               set output mode to 'ascii'\n"
16559   "   -bail                stop after hitting an error\n"
16560   "   -batch               force batch I/O\n"
16561   "   -column              set output mode to 'column'\n"
16562   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
16563   "   -csv                 set output mode to 'csv'\n"
16564 #if defined(SQLITE_ENABLE_DESERIALIZE)
16565   "   -deserialize         open the database using sqlite3_deserialize()\n"
16566 #endif
16567   "   -echo                print commands before execution\n"
16568   "   -init FILENAME       read/process named file\n"
16569   "   -[no]header          turn headers on or off\n"
16570 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16571   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
16572 #endif
16573   "   -help                show this message\n"
16574   "   -html                set output mode to HTML\n"
16575   "   -interactive         force interactive I/O\n"
16576   "   -line                set output mode to 'line'\n"
16577   "   -list                set output mode to 'list'\n"
16578   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
16579 #if defined(SQLITE_ENABLE_DESERIALIZE)
16580   "   -maxsize N           maximum size for a --deserialize database\n"
16581 #endif
16582   "   -memtrace            trace all memory allocations and deallocations\n"
16583   "   -mmap N              default mmap size set to N\n"
16584 #ifdef SQLITE_ENABLE_MULTIPLEX
16585   "   -multiplex           enable the multiplexor VFS\n"
16586 #endif
16587   "   -newline SEP         set output row separator. Default: '\\n'\n"
16588   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
16589   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
16590   "   -quote               set output mode to 'quote'\n"
16591   "   -readonly            open the database read-only\n"
16592   "   -separator SEP       set output column separator. Default: '|'\n"
16593 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16594   "   -sorterref SIZE      sorter references threshold size\n"
16595 #endif
16596   "   -stats               print memory stats before each finalize\n"
16597   "   -version             show SQLite version\n"
16598   "   -vfs NAME            use NAME as the default VFS\n"
16599 #ifdef SQLITE_ENABLE_VFSTRACE
16600   "   -vfstrace            enable tracing of all VFS calls\n"
16601 #endif
16602 #ifdef SQLITE_HAVE_ZLIB
16603   "   -zip                 open the file as a ZIP Archive\n"
16604 #endif
16605 ;
16606 static void usage(int showDetail){
16607   utf8_printf(stderr,
16608       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
16609       "FILENAME is the name of an SQLite database. A new database is created\n"
16610       "if the file does not previously exist.\n", Argv0);
16611   if( showDetail ){
16612     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
16613   }else{
16614     raw_printf(stderr, "Use the -help option for additional information\n");
16615   }
16616   exit(1);
16617 }
16618
16619 /*
16620 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
16621 ** error message if it is initialized.
16622 */
16623 static void verify_uninitialized(void){
16624   if( sqlite3_config(-1)==SQLITE_MISUSE ){
16625     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
16626                         " initialization.\n");
16627   }
16628 }
16629
16630 /*
16631 ** Initialize the state information in data
16632 */
16633 static void main_init(ShellState *data) {
16634   memset(data, 0, sizeof(*data));
16635   data->normalMode = data->cMode = data->mode = MODE_List;
16636   data->autoExplain = 1;
16637   memcpy(data->colSeparator,SEP_Column, 2);
16638   memcpy(data->rowSeparator,SEP_Row, 2);
16639   data->showHeader = 0;
16640   data->shellFlgs = SHFLG_Lookaside;
16641   verify_uninitialized();
16642   sqlite3_config(SQLITE_CONFIG_URI, 1);
16643   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
16644   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
16645   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
16646   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
16647 }
16648
16649 /*
16650 ** Output text to the console in a font that attracts extra attention.
16651 */
16652 #ifdef _WIN32
16653 static void printBold(const char *zText){
16654   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
16655   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
16656   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
16657   SetConsoleTextAttribute(out,
16658          FOREGROUND_RED|FOREGROUND_INTENSITY
16659   );
16660   printf("%s", zText);
16661   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
16662 }
16663 #else
16664 static void printBold(const char *zText){
16665   printf("\033[1m%s\033[0m", zText);
16666 }
16667 #endif
16668
16669 /*
16670 ** Get the argument to an --option.  Throw an error and die if no argument
16671 ** is available.
16672 */
16673 static char *cmdline_option_value(int argc, char **argv, int i){
16674   if( i==argc ){
16675     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
16676             argv[0], argv[argc-1]);
16677     exit(1);
16678   }
16679   return argv[i];
16680 }
16681
16682 #ifndef SQLITE_SHELL_IS_UTF8
16683 #  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
16684 #    define SQLITE_SHELL_IS_UTF8          (0)
16685 #  else
16686 #    define SQLITE_SHELL_IS_UTF8          (1)
16687 #  endif
16688 #endif
16689
16690 #if SQLITE_SHELL_IS_UTF8
16691 int SQLITE_CDECL main(int argc, char **argv){
16692 #else
16693 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
16694   char **argv;
16695 #endif
16696   char *zErrMsg = 0;
16697   ShellState data;
16698   const char *zInitFile = 0;
16699   int i;
16700   int rc = 0;
16701   int warnInmemoryDb = 0;
16702   int readStdin = 1;
16703   int nCmd = 0;
16704   char **azCmd = 0;
16705   const char *zVfs = 0;           /* Value of -vfs command-line option */
16706 #if !SQLITE_SHELL_IS_UTF8
16707   char **argvToFree = 0;
16708   int argcToFree = 0;
16709 #endif
16710
16711   setBinaryMode(stdin, 0);
16712   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
16713   stdin_is_interactive = isatty(0);
16714   stdout_is_console = isatty(1);
16715
16716 #if !defined(_WIN32_WCE)
16717   if( getenv("SQLITE_DEBUG_BREAK") ){
16718     if( isatty(0) && isatty(2) ){
16719       fprintf(stderr,
16720           "attach debugger to process %d and press any key to continue.\n",
16721           GETPID());
16722       fgetc(stdin);
16723     }else{
16724 #if defined(_WIN32) || defined(WIN32)
16725       DebugBreak();
16726 #elif defined(SIGTRAP)
16727       raise(SIGTRAP);
16728 #endif
16729     }
16730   }
16731 #endif
16732
16733 #if USE_SYSTEM_SQLITE+0!=1
16734   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
16735     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
16736             sqlite3_sourceid(), SQLITE_SOURCE_ID);
16737     exit(1);
16738   }
16739 #endif
16740   main_init(&data);
16741
16742   /* On Windows, we must translate command-line arguments into UTF-8.
16743   ** The SQLite memory allocator subsystem has to be enabled in order to
16744   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
16745   ** subsequent sqlite3_config() calls will work.  So copy all results into
16746   ** memory that does not come from the SQLite memory allocator.
16747   */
16748 #if !SQLITE_SHELL_IS_UTF8
16749   sqlite3_initialize();
16750   argvToFree = malloc(sizeof(argv[0])*argc*2);
16751   argcToFree = argc;
16752   argv = argvToFree + argc;
16753   if( argv==0 ) shell_out_of_memory();
16754   for(i=0; i<argc; i++){
16755     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
16756     int n;
16757     if( z==0 ) shell_out_of_memory();
16758     n = (int)strlen(z);
16759     argv[i] = malloc( n+1 );
16760     if( argv[i]==0 ) shell_out_of_memory();
16761     memcpy(argv[i], z, n+1);
16762     argvToFree[i] = argv[i];
16763     sqlite3_free(z);
16764   }
16765   sqlite3_shutdown();
16766 #endif
16767
16768   assert( argc>=1 && argv && argv[0] );
16769   Argv0 = argv[0];
16770
16771   /* Make sure we have a valid signal handler early, before anything
16772   ** else is done.
16773   */
16774 #ifdef SIGINT
16775   signal(SIGINT, interrupt_handler);
16776 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
16777   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
16778 #endif
16779
16780 #ifdef SQLITE_SHELL_DBNAME_PROC
16781   {
16782     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
16783     ** of a C-function that will provide the name of the database file.  Use
16784     ** this compile-time option to embed this shell program in larger
16785     ** applications. */
16786     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
16787     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
16788     warnInmemoryDb = 0;
16789   }
16790 #endif
16791
16792   /* Do an initial pass through the command-line argument to locate
16793   ** the name of the database file, the name of the initialization file,
16794   ** the size of the alternative malloc heap,
16795   ** and the first command to execute.
16796   */
16797   verify_uninitialized();
16798   for(i=1; i<argc; i++){
16799     char *z;
16800     z = argv[i];
16801     if( z[0]!='-' ){
16802       if( data.zDbFilename==0 ){
16803         data.zDbFilename = z;
16804       }else{
16805         /* Excesss arguments are interpreted as SQL (or dot-commands) and
16806         ** mean that nothing is read from stdin */
16807         readStdin = 0;
16808         nCmd++;
16809         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
16810         if( azCmd==0 ) shell_out_of_memory();
16811         azCmd[nCmd-1] = z;
16812       }
16813     }
16814     if( z[1]=='-' ) z++;
16815     if( strcmp(z,"-separator")==0
16816      || strcmp(z,"-nullvalue")==0
16817      || strcmp(z,"-newline")==0
16818      || strcmp(z,"-cmd")==0
16819     ){
16820       (void)cmdline_option_value(argc, argv, ++i);
16821     }else if( strcmp(z,"-init")==0 ){
16822       zInitFile = cmdline_option_value(argc, argv, ++i);
16823     }else if( strcmp(z,"-batch")==0 ){
16824       /* Need to check for batch mode here to so we can avoid printing
16825       ** informational messages (like from process_sqliterc) before
16826       ** we do the actual processing of arguments later in a second pass.
16827       */
16828       stdin_is_interactive = 0;
16829     }else if( strcmp(z,"-heap")==0 ){
16830 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16831       const char *zSize;
16832       sqlite3_int64 szHeap;
16833
16834       zSize = cmdline_option_value(argc, argv, ++i);
16835       szHeap = integerValue(zSize);
16836       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
16837       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
16838 #else
16839       (void)cmdline_option_value(argc, argv, ++i);
16840 #endif
16841     }else if( strcmp(z,"-pagecache")==0 ){
16842       int n, sz;
16843       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16844       if( sz>70000 ) sz = 70000;
16845       if( sz<0 ) sz = 0;
16846       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16847       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
16848                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
16849       data.shellFlgs |= SHFLG_Pagecache;
16850     }else if( strcmp(z,"-lookaside")==0 ){
16851       int n, sz;
16852       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16853       if( sz<0 ) sz = 0;
16854       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16855       if( n<0 ) n = 0;
16856       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
16857       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
16858 #ifdef SQLITE_ENABLE_VFSTRACE
16859     }else if( strcmp(z,"-vfstrace")==0 ){
16860       extern int vfstrace_register(
16861          const char *zTraceName,
16862          const char *zOldVfsName,
16863          int (*xOut)(const char*,void*),
16864          void *pOutArg,
16865          int makeDefault
16866       );
16867       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
16868 #endif
16869 #ifdef SQLITE_ENABLE_MULTIPLEX
16870     }else if( strcmp(z,"-multiplex")==0 ){
16871       extern int sqlite3_multiple_initialize(const char*,int);
16872       sqlite3_multiplex_initialize(0, 1);
16873 #endif
16874     }else if( strcmp(z,"-mmap")==0 ){
16875       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16876       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
16877 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16878     }else if( strcmp(z,"-sorterref")==0 ){
16879       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16880       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
16881 #endif
16882     }else if( strcmp(z,"-vfs")==0 ){
16883       zVfs = cmdline_option_value(argc, argv, ++i);
16884 #ifdef SQLITE_HAVE_ZLIB
16885     }else if( strcmp(z,"-zip")==0 ){
16886       data.openMode = SHELL_OPEN_ZIPFILE;
16887 #endif
16888     }else if( strcmp(z,"-append")==0 ){
16889       data.openMode = SHELL_OPEN_APPENDVFS;
16890 #ifdef SQLITE_ENABLE_DESERIALIZE
16891     }else if( strcmp(z,"-deserialize")==0 ){
16892       data.openMode = SHELL_OPEN_DESERIALIZE;
16893     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16894       data.szMax = integerValue(argv[++i]);
16895 #endif
16896     }else if( strcmp(z,"-readonly")==0 ){
16897       data.openMode = SHELL_OPEN_READONLY;
16898 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16899     }else if( strncmp(z, "-A",2)==0 ){
16900       /* All remaining command-line arguments are passed to the ".archive"
16901       ** command, so ignore them */
16902       break;
16903 #endif
16904     }else if( strcmp(z, "-memtrace")==0 ){
16905       sqlite3MemTraceActivate(stderr);
16906     }
16907   }
16908   verify_uninitialized();
16909
16910
16911 #ifdef SQLITE_SHELL_INIT_PROC
16912   {
16913     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
16914     ** of a C-function that will perform initialization actions on SQLite that
16915     ** occur just before or after sqlite3_initialize(). Use this compile-time
16916     ** option to embed this shell program in larger applications. */
16917     extern void SQLITE_SHELL_INIT_PROC(void);
16918     SQLITE_SHELL_INIT_PROC();
16919   }
16920 #else
16921   /* All the sqlite3_config() calls have now been made. So it is safe
16922   ** to call sqlite3_initialize() and process any command line -vfs option. */
16923   sqlite3_initialize();
16924 #endif
16925
16926   if( zVfs ){
16927     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
16928     if( pVfs ){
16929       sqlite3_vfs_register(pVfs, 1);
16930     }else{
16931       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
16932       exit(1);
16933     }
16934   }
16935
16936   if( data.zDbFilename==0 ){
16937 #ifndef SQLITE_OMIT_MEMORYDB
16938     data.zDbFilename = ":memory:";
16939     warnInmemoryDb = argc==1;
16940 #else
16941     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
16942     return 1;
16943 #endif
16944   }
16945   data.out = stdout;
16946   sqlite3_appendvfs_init(0,0,0);
16947
16948   /* Go ahead and open the database file if it already exists.  If the
16949   ** file does not exist, delay opening it.  This prevents empty database
16950   ** files from being created if a user mistypes the database name argument
16951   ** to the sqlite command-line tool.
16952   */
16953   if( access(data.zDbFilename, 0)==0 ){
16954     open_db(&data, 0);
16955   }
16956
16957   /* Process the initialization file if there is one.  If no -init option
16958   ** is given on the command line, look for a file named ~/.sqliterc and
16959   ** try to process it.
16960   */
16961   process_sqliterc(&data,zInitFile);
16962
16963   /* Make a second pass through the command-line argument and set
16964   ** options.  This second pass is delayed until after the initialization
16965   ** file is processed so that the command-line arguments will override
16966   ** settings in the initialization file.
16967   */
16968   for(i=1; i<argc; i++){
16969     char *z = argv[i];
16970     if( z[0]!='-' ) continue;
16971     if( z[1]=='-' ){ z++; }
16972     if( strcmp(z,"-init")==0 ){
16973       i++;
16974     }else if( strcmp(z,"-html")==0 ){
16975       data.mode = MODE_Html;
16976     }else if( strcmp(z,"-list")==0 ){
16977       data.mode = MODE_List;
16978     }else if( strcmp(z,"-quote")==0 ){
16979       data.mode = MODE_Quote;
16980     }else if( strcmp(z,"-line")==0 ){
16981       data.mode = MODE_Line;
16982     }else if( strcmp(z,"-column")==0 ){
16983       data.mode = MODE_Column;
16984     }else if( strcmp(z,"-csv")==0 ){
16985       data.mode = MODE_Csv;
16986       memcpy(data.colSeparator,",",2);
16987 #ifdef SQLITE_HAVE_ZLIB
16988     }else if( strcmp(z,"-zip")==0 ){
16989       data.openMode = SHELL_OPEN_ZIPFILE;
16990 #endif
16991     }else if( strcmp(z,"-append")==0 ){
16992       data.openMode = SHELL_OPEN_APPENDVFS;
16993 #ifdef SQLITE_ENABLE_DESERIALIZE
16994     }else if( strcmp(z,"-deserialize")==0 ){
16995       data.openMode = SHELL_OPEN_DESERIALIZE;
16996     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16997       data.szMax = integerValue(argv[++i]);
16998 #endif
16999     }else if( strcmp(z,"-readonly")==0 ){
17000       data.openMode = SHELL_OPEN_READONLY;
17001     }else if( strcmp(z,"-ascii")==0 ){
17002       data.mode = MODE_Ascii;
17003       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
17004                        SEP_Unit);
17005       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
17006                        SEP_Record);
17007     }else if( strcmp(z,"-separator")==0 ){
17008       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
17009                        "%s",cmdline_option_value(argc,argv,++i));
17010     }else if( strcmp(z,"-newline")==0 ){
17011       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
17012                        "%s",cmdline_option_value(argc,argv,++i));
17013     }else if( strcmp(z,"-nullvalue")==0 ){
17014       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
17015                        "%s",cmdline_option_value(argc,argv,++i));
17016     }else if( strcmp(z,"-header")==0 ){
17017       data.showHeader = 1;
17018     }else if( strcmp(z,"-noheader")==0 ){
17019       data.showHeader = 0;
17020     }else if( strcmp(z,"-echo")==0 ){
17021       ShellSetFlag(&data, SHFLG_Echo);
17022     }else if( strcmp(z,"-eqp")==0 ){
17023       data.autoEQP = AUTOEQP_on;
17024     }else if( strcmp(z,"-eqpfull")==0 ){
17025       data.autoEQP = AUTOEQP_full;
17026     }else if( strcmp(z,"-stats")==0 ){
17027       data.statsOn = 1;
17028     }else if( strcmp(z,"-scanstats")==0 ){
17029       data.scanstatsOn = 1;
17030     }else if( strcmp(z,"-backslash")==0 ){
17031       /* Undocumented command-line option: -backslash
17032       ** Causes C-style backslash escapes to be evaluated in SQL statements
17033       ** prior to sending the SQL into SQLite.  Useful for injecting
17034       ** crazy bytes in the middle of SQL statements for testing and debugging.
17035       */
17036       ShellSetFlag(&data, SHFLG_Backslash);
17037     }else if( strcmp(z,"-bail")==0 ){
17038       bail_on_error = 1;
17039     }else if( strcmp(z,"-version")==0 ){
17040       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
17041       return 0;
17042     }else if( strcmp(z,"-interactive")==0 ){
17043       stdin_is_interactive = 1;
17044     }else if( strcmp(z,"-batch")==0 ){
17045       stdin_is_interactive = 0;
17046     }else if( strcmp(z,"-heap")==0 ){
17047       i++;
17048     }else if( strcmp(z,"-pagecache")==0 ){
17049       i+=2;
17050     }else if( strcmp(z,"-lookaside")==0 ){
17051       i+=2;
17052     }else if( strcmp(z,"-mmap")==0 ){
17053       i++;
17054     }else if( strcmp(z,"-memtrace")==0 ){
17055       i++;
17056 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
17057     }else if( strcmp(z,"-sorterref")==0 ){
17058       i++;
17059 #endif
17060     }else if( strcmp(z,"-vfs")==0 ){
17061       i++;
17062 #ifdef SQLITE_ENABLE_VFSTRACE
17063     }else if( strcmp(z,"-vfstrace")==0 ){
17064       i++;
17065 #endif
17066 #ifdef SQLITE_ENABLE_MULTIPLEX
17067     }else if( strcmp(z,"-multiplex")==0 ){
17068       i++;
17069 #endif
17070     }else if( strcmp(z,"-help")==0 ){
17071       usage(1);
17072     }else if( strcmp(z,"-cmd")==0 ){
17073       /* Run commands that follow -cmd first and separately from commands
17074       ** that simply appear on the command-line.  This seems goofy.  It would
17075       ** be better if all commands ran in the order that they appear.  But
17076       ** we retain the goofy behavior for historical compatibility. */
17077       if( i==argc-1 ) break;
17078       z = cmdline_option_value(argc,argv,++i);
17079       if( z[0]=='.' ){
17080         rc = do_meta_command(z, &data);
17081         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
17082       }else{
17083         open_db(&data, 0);
17084         rc = shell_exec(&data, z, &zErrMsg);
17085         if( zErrMsg!=0 ){
17086           utf8_printf(stderr,"Error: %s\n", zErrMsg);
17087           if( bail_on_error ) return rc!=0 ? rc : 1;
17088         }else if( rc!=0 ){
17089           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
17090           if( bail_on_error ) return rc;
17091         }
17092       }
17093 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17094     }else if( strncmp(z, "-A", 2)==0 ){
17095       if( nCmd>0 ){
17096         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
17097                             " with \"%s\"\n", z);
17098         return 1;
17099       }
17100       open_db(&data, OPEN_DB_ZIPFILE);
17101       if( z[2] ){
17102         argv[i] = &z[2];
17103         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
17104       }else{
17105         arDotCommand(&data, 1, argv+i, argc-i);
17106       }
17107       readStdin = 0;
17108       break;
17109 #endif
17110     }else{
17111       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
17112       raw_printf(stderr,"Use -help for a list of options.\n");
17113       return 1;
17114     }
17115     data.cMode = data.mode;
17116   }
17117
17118   if( !readStdin ){
17119     /* Run all arguments that do not begin with '-' as if they were separate
17120     ** command-line inputs, except for the argToSkip argument which contains
17121     ** the database filename.
17122     */
17123     for(i=0; i<nCmd; i++){
17124       if( azCmd[i][0]=='.' ){
17125         rc = do_meta_command(azCmd[i], &data);
17126         if( rc ) return rc==2 ? 0 : rc;
17127       }else{
17128         open_db(&data, 0);
17129         rc = shell_exec(&data, azCmd[i], &zErrMsg);
17130         if( zErrMsg!=0 ){
17131           utf8_printf(stderr,"Error: %s\n", zErrMsg);
17132           return rc!=0 ? rc : 1;
17133         }else if( rc!=0 ){
17134           utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
17135           return rc;
17136         }
17137       }
17138     }
17139     free(azCmd);
17140   }else{
17141     /* Run commands received from standard input
17142     */
17143     if( stdin_is_interactive ){
17144       char *zHome;
17145       char *zHistory;
17146       int nHistory;
17147       printf(
17148         "SQLite version %s %.19s\n" /*extra-version-info*/
17149         "Enter \".help\" for usage hints.\n",
17150         sqlite3_libversion(), sqlite3_sourceid()
17151       );
17152       if( warnInmemoryDb ){
17153         printf("Connected to a ");
17154         printBold("transient in-memory database");
17155         printf(".\nUse \".open FILENAME\" to reopen on a "
17156                "persistent database.\n");
17157       }
17158       zHistory = getenv("SQLITE_HISTORY");
17159       if( zHistory ){
17160         zHistory = strdup(zHistory);
17161       }else if( (zHome = find_home_dir(0))!=0 ){
17162         nHistory = strlen30(zHome) + 20;
17163         if( (zHistory = malloc(nHistory))!=0 ){
17164           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
17165         }
17166       }
17167       if( zHistory ){ shell_read_history(zHistory); }
17168 #if HAVE_READLINE || HAVE_EDITLINE
17169       rl_attempted_completion_function = readline_completion;
17170 #elif HAVE_LINENOISE
17171       linenoiseSetCompletionCallback(linenoise_completion);
17172 #endif
17173       data.in = 0;
17174       rc = process_input(&data);
17175       if( zHistory ){
17176         shell_stifle_history(2000);
17177         shell_write_history(zHistory);
17178         free(zHistory);
17179       }
17180     }else{
17181       data.in = stdin;
17182       rc = process_input(&data);
17183     }
17184   }
17185   set_table_name(&data, 0);
17186   if( data.db ){
17187     session_close_all(&data);
17188     close_db(data.db);
17189   }
17190   sqlite3_free(data.zFreeOnClose);
17191   find_home_dir(1);
17192   output_reset(&data);
17193   data.doXdgOpen = 0;
17194   clearTempFile(&data);
17195 #if !SQLITE_SHELL_IS_UTF8
17196   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
17197   free(argvToFree);
17198 #endif
17199   /* Clear the global data structure so that valgrind will detect memory
17200   ** leaks */
17201   memset(&data, 0, sizeof(data));
17202   return rc;
17203 }