]> pd.if.org Git - zpackage/blob - sqlite/shell.c
remove stray debug fprintf
[zpackage] / sqlite / 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 );
2181   if( pBuf==0 ){
2182     sqlite3_result_error_nomem(ctx);
2183     fclose(in);
2184     return;
2185   }
2186   if( 1==fread(pBuf, nIn, 1, 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 to (mode&0777).
2322 **
2323 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2324 ** SQLITE_OK is returned if the directory is successfully created, or
2325 ** SQLITE_ERROR otherwise.
2326 */
2327 static int makeDirectory(
2328   const char *zFile,
2329   mode_t mode
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, mode & 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, mode)==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 /*
10434 ** Run a prepared statement
10435 */
10436 static void exec_prepared_stmt(
10437   ShellState *pArg,                                /* Pointer to ShellState */
10438   sqlite3_stmt *pStmt                              /* Statment to run */
10439 ){
10440   int rc;
10441
10442   /* perform the first step.  this will tell us if we
10443   ** have a result set or not and how wide it is.
10444   */
10445   rc = sqlite3_step(pStmt);
10446   /* if we have a result set... */
10447   if( SQLITE_ROW == rc ){
10448     /* allocate space for col name ptr, value ptr, and type */
10449     int nCol = sqlite3_column_count(pStmt);
10450     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10451     if( !pData ){
10452       rc = SQLITE_NOMEM;
10453     }else{
10454       char **azCols = (char **)pData;      /* Names of result columns */
10455       char **azVals = &azCols[nCol];       /* Results */
10456       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10457       int i, x;
10458       assert(sizeof(int) <= sizeof(char *));
10459       /* save off ptrs to column names */
10460       for(i=0; i<nCol; i++){
10461         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10462       }
10463       do{
10464         /* extract the data and data types */
10465         for(i=0; i<nCol; i++){
10466           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10467           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10468             azVals[i] = "";
10469           }else{
10470             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10471           }
10472           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10473             rc = SQLITE_NOMEM;
10474             break; /* from for */
10475           }
10476         } /* end for */
10477
10478         /* if data and types extracted successfully... */
10479         if( SQLITE_ROW == rc ){
10480           /* call the supplied callback with the result row data */
10481           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10482             rc = SQLITE_ABORT;
10483           }else{
10484             rc = sqlite3_step(pStmt);
10485           }
10486         }
10487       } while( SQLITE_ROW == rc );
10488       sqlite3_free(pData);
10489     }
10490   }
10491 }
10492
10493 #ifndef SQLITE_OMIT_VIRTUALTABLE
10494 /*
10495 ** This function is called to process SQL if the previous shell command
10496 ** was ".expert". It passes the SQL in the second argument directly to
10497 ** the sqlite3expert object.
10498 **
10499 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10500 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10501 ** an English language error message. It is the responsibility of the
10502 ** caller to eventually free this buffer using sqlite3_free().
10503 */
10504 static int expertHandleSQL(
10505   ShellState *pState, 
10506   const char *zSql, 
10507   char **pzErr
10508 ){
10509   assert( pState->expert.pExpert );
10510   assert( pzErr==0 || *pzErr==0 );
10511   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
10512 }
10513
10514 /*
10515 ** This function is called either to silently clean up the object
10516 ** created by the ".expert" command (if bCancel==1), or to generate a 
10517 ** report from it and then clean it up (if bCancel==0).
10518 **
10519 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10520 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10521 ** an English language error message. It is the responsibility of the
10522 ** caller to eventually free this buffer using sqlite3_free().
10523 */
10524 static int expertFinish(
10525   ShellState *pState,
10526   int bCancel,
10527   char **pzErr
10528 ){
10529   int rc = SQLITE_OK;
10530   sqlite3expert *p = pState->expert.pExpert;
10531   assert( p );
10532   assert( bCancel || pzErr==0 || *pzErr==0 );
10533   if( bCancel==0 ){
10534     FILE *out = pState->out;
10535     int bVerbose = pState->expert.bVerbose;
10536
10537     rc = sqlite3_expert_analyze(p, pzErr);
10538     if( rc==SQLITE_OK ){
10539       int nQuery = sqlite3_expert_count(p);
10540       int i;
10541
10542       if( bVerbose ){
10543         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
10544         raw_printf(out, "-- Candidates -----------------------------\n");
10545         raw_printf(out, "%s\n", zCand);
10546       }
10547       for(i=0; i<nQuery; i++){
10548         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
10549         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
10550         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
10551         if( zIdx==0 ) zIdx = "(no new indexes)\n";
10552         if( bVerbose ){
10553           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
10554           raw_printf(out, "%s\n\n", zSql);
10555         }
10556         raw_printf(out, "%s\n", zIdx);
10557         raw_printf(out, "%s\n", zEQP);
10558       }
10559     }
10560   }
10561   sqlite3_expert_destroy(p);
10562   pState->expert.pExpert = 0;
10563   return rc;
10564 }
10565
10566 /*
10567 ** Implementation of ".expert" dot command.
10568 */
10569 static int expertDotCommand(
10570   ShellState *pState,             /* Current shell tool state */
10571   char **azArg,                   /* Array of arguments passed to dot command */
10572   int nArg                        /* Number of entries in azArg[] */
10573 ){
10574   int rc = SQLITE_OK;
10575   char *zErr = 0;
10576   int i;
10577   int iSample = 0;
10578
10579   assert( pState->expert.pExpert==0 );
10580   memset(&pState->expert, 0, sizeof(ExpertInfo));
10581
10582   for(i=1; rc==SQLITE_OK && i<nArg; i++){
10583     char *z = azArg[i];
10584     int n;
10585     if( z[0]=='-' && z[1]=='-' ) z++;
10586     n = strlen30(z);
10587     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
10588       pState->expert.bVerbose = 1;
10589     }
10590     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
10591       if( i==(nArg-1) ){
10592         raw_printf(stderr, "option requires an argument: %s\n", z);
10593         rc = SQLITE_ERROR;
10594       }else{
10595         iSample = (int)integerValue(azArg[++i]);
10596         if( iSample<0 || iSample>100 ){
10597           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
10598           rc = SQLITE_ERROR;
10599         }
10600       }
10601     }
10602     else{
10603       raw_printf(stderr, "unknown option: %s\n", z);
10604       rc = SQLITE_ERROR;
10605     }
10606   }
10607
10608   if( rc==SQLITE_OK ){
10609     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
10610     if( pState->expert.pExpert==0 ){
10611       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
10612       rc = SQLITE_ERROR;
10613     }else{
10614       sqlite3_expert_config(
10615           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
10616       );
10617     }
10618   }
10619
10620   return rc;
10621 }
10622 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10623
10624 /*
10625 ** Execute a statement or set of statements.  Print
10626 ** any result rows/columns depending on the current mode
10627 ** set via the supplied callback.
10628 **
10629 ** This is very similar to SQLite's built-in sqlite3_exec()
10630 ** function except it takes a slightly different callback
10631 ** and callback data argument.
10632 */
10633 static int shell_exec(
10634   ShellState *pArg,                         /* Pointer to ShellState */
10635   const char *zSql,                         /* SQL to be evaluated */
10636   char **pzErrMsg                           /* Error msg written here */
10637 ){
10638   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
10639   int rc = SQLITE_OK;             /* Return Code */
10640   int rc2;
10641   const char *zLeftover;          /* Tail of unprocessed SQL */
10642   sqlite3 *db = pArg->db;
10643
10644   if( pzErrMsg ){
10645     *pzErrMsg = NULL;
10646   }
10647
10648 #ifndef SQLITE_OMIT_VIRTUALTABLE
10649   if( pArg->expert.pExpert ){
10650     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
10651     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
10652   }
10653 #endif
10654
10655   while( zSql[0] && (SQLITE_OK == rc) ){
10656     static const char *zStmtSql;
10657     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
10658     if( SQLITE_OK != rc ){
10659       if( pzErrMsg ){
10660         *pzErrMsg = save_err_msg(db);
10661       }
10662     }else{
10663       if( !pStmt ){
10664         /* this happens for a comment or white-space */
10665         zSql = zLeftover;
10666         while( IsSpace(zSql[0]) ) zSql++;
10667         continue;
10668       }
10669       zStmtSql = sqlite3_sql(pStmt);
10670       if( zStmtSql==0 ) zStmtSql = "";
10671       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
10672
10673       /* save off the prepared statment handle and reset row count */
10674       if( pArg ){
10675         pArg->pStmt = pStmt;
10676         pArg->cnt = 0;
10677       }
10678
10679       /* echo the sql statement if echo on */
10680       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10681         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10682       }
10683
10684       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10685       if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
10686         sqlite3_stmt *pExplain;
10687         char *zEQP;
10688         int triggerEQP = 0;
10689         disable_debug_trace_modes();
10690         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
10691         if( pArg->autoEQP>=AUTOEQP_trigger ){
10692           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
10693         }
10694         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10695         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10696         if( rc==SQLITE_OK ){
10697           while( sqlite3_step(pExplain)==SQLITE_ROW ){
10698             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10699             int iEqpId = sqlite3_column_int(pExplain, 0);
10700             int iParentId = sqlite3_column_int(pExplain, 1);
10701             if( zEQPLine[0]=='-' ) eqp_render(pArg);
10702             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10703           }
10704           eqp_render(pArg);
10705         }
10706         sqlite3_finalize(pExplain);
10707         sqlite3_free(zEQP);
10708         if( pArg->autoEQP>=AUTOEQP_full ){
10709           /* Also do an EXPLAIN for ".eqp full" mode */
10710           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10711           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10712           if( rc==SQLITE_OK ){
10713             pArg->cMode = MODE_Explain;
10714             explain_data_prepare(pArg, pExplain);
10715             exec_prepared_stmt(pArg, pExplain);
10716             explain_data_delete(pArg);
10717           }
10718           sqlite3_finalize(pExplain);
10719           sqlite3_free(zEQP);
10720         }
10721         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
10722           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
10723           /* Reprepare pStmt before reactiving trace modes */
10724           sqlite3_finalize(pStmt);
10725           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
10726           if( pArg ) pArg->pStmt = pStmt;
10727         }
10728         restore_debug_trace_modes();
10729       }
10730
10731       if( pArg ){
10732         pArg->cMode = pArg->mode;
10733         if( pArg->autoExplain ){
10734           if( sqlite3_column_count(pStmt)==8
10735            && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
10736           ){
10737             pArg->cMode = MODE_Explain;
10738           }
10739           if( sqlite3_column_count(pStmt)==4
10740            && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){
10741             pArg->cMode = MODE_EQP;
10742           }
10743         }
10744
10745         /* If the shell is currently in ".explain" mode, gather the extra
10746         ** data required to add indents to the output.*/
10747         if( pArg->cMode==MODE_Explain ){
10748           explain_data_prepare(pArg, pStmt);
10749         }
10750       }
10751
10752       exec_prepared_stmt(pArg, pStmt);
10753       explain_data_delete(pArg);
10754       eqp_render(pArg);
10755
10756       /* print usage stats if stats on */
10757       if( pArg && pArg->statsOn ){
10758         display_stats(db, pArg, 0);
10759       }
10760
10761       /* print loop-counters if required */
10762       if( pArg && pArg->scanstatsOn ){
10763         display_scanstats(db, pArg);
10764       }
10765
10766       /* Finalize the statement just executed. If this fails, save a
10767       ** copy of the error message. Otherwise, set zSql to point to the
10768       ** next statement to execute. */
10769       rc2 = sqlite3_finalize(pStmt);
10770       if( rc!=SQLITE_NOMEM ) rc = rc2;
10771       if( rc==SQLITE_OK ){
10772         zSql = zLeftover;
10773         while( IsSpace(zSql[0]) ) zSql++;
10774       }else if( pzErrMsg ){
10775         *pzErrMsg = save_err_msg(db);
10776       }
10777
10778       /* clear saved stmt handle */
10779       if( pArg ){
10780         pArg->pStmt = NULL;
10781       }
10782     }
10783   } /* end while */
10784
10785   return rc;
10786 }
10787
10788 /*
10789 ** Release memory previously allocated by tableColumnList().
10790 */
10791 static void freeColumnList(char **azCol){
10792   int i;
10793   for(i=1; azCol[i]; i++){
10794     sqlite3_free(azCol[i]);
10795   }
10796   /* azCol[0] is a static string */
10797   sqlite3_free(azCol);
10798 }
10799
10800 /*
10801 ** Return a list of pointers to strings which are the names of all
10802 ** columns in table zTab.   The memory to hold the names is dynamically
10803 ** allocated and must be released by the caller using a subsequent call
10804 ** to freeColumnList().
10805 **
10806 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
10807 ** value that needs to be preserved, then azCol[0] is filled in with the
10808 ** name of the rowid column.
10809 **
10810 ** The first regular column in the table is azCol[1].  The list is terminated
10811 ** by an entry with azCol[i]==0.
10812 */
10813 static char **tableColumnList(ShellState *p, const char *zTab){
10814   char **azCol = 0;
10815   sqlite3_stmt *pStmt;
10816   char *zSql;
10817   int nCol = 0;
10818   int nAlloc = 0;
10819   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
10820   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
10821   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
10822   int rc;
10823
10824   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
10825   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10826   sqlite3_free(zSql);
10827   if( rc ) return 0;
10828   while( sqlite3_step(pStmt)==SQLITE_ROW ){
10829     if( nCol>=nAlloc-2 ){
10830       nAlloc = nAlloc*2 + nCol + 10;
10831       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
10832       if( azCol==0 ) shell_out_of_memory();
10833     }
10834     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
10835     if( sqlite3_column_int(pStmt, 5) ){
10836       nPK++;
10837       if( nPK==1
10838        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
10839                           "INTEGER")==0
10840       ){
10841         isIPK = 1;
10842       }else{
10843         isIPK = 0;
10844       }
10845     }
10846   }
10847   sqlite3_finalize(pStmt);
10848   if( azCol==0 ) return 0;
10849   azCol[0] = 0;
10850   azCol[nCol+1] = 0;
10851
10852   /* The decision of whether or not a rowid really needs to be preserved
10853   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
10854   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
10855   ** rowids on tables where the rowid is inaccessible because there are other
10856   ** columns in the table named "rowid", "_rowid_", and "oid".
10857   */
10858   if( preserveRowid && isIPK ){
10859     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
10860     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
10861     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
10862     ** ROWID aliases.  To distinguish these cases, check to see if
10863     ** there is a "pk" entry in "PRAGMA index_list".  There will be
10864     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
10865     */
10866     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
10867                            " WHERE origin='pk'", zTab);
10868     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10869     sqlite3_free(zSql);
10870     if( rc ){
10871       freeColumnList(azCol);
10872       return 0;
10873     }
10874     rc = sqlite3_step(pStmt);
10875     sqlite3_finalize(pStmt);
10876     preserveRowid = rc==SQLITE_ROW;
10877   }
10878   if( preserveRowid ){
10879     /* Only preserve the rowid if we can find a name to use for the
10880     ** rowid */
10881     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
10882     int i, j;
10883     for(j=0; j<3; j++){
10884       for(i=1; i<=nCol; i++){
10885         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
10886       }
10887       if( i>nCol ){
10888         /* At this point, we know that azRowid[j] is not the name of any
10889         ** ordinary column in the table.  Verify that azRowid[j] is a valid
10890         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
10891         ** tables will fail this last check */
10892         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
10893         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
10894         break;
10895       }
10896     }
10897   }
10898   return azCol;
10899 }
10900
10901 /*
10902 ** Toggle the reverse_unordered_selects setting.
10903 */
10904 static void toggleSelectOrder(sqlite3 *db){
10905   sqlite3_stmt *pStmt = 0;
10906   int iSetting = 0;
10907   char zStmt[100];
10908   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
10909   if( sqlite3_step(pStmt)==SQLITE_ROW ){
10910     iSetting = sqlite3_column_int(pStmt, 0);
10911   }
10912   sqlite3_finalize(pStmt);
10913   sqlite3_snprintf(sizeof(zStmt), zStmt,
10914        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
10915   sqlite3_exec(db, zStmt, 0, 0, 0);
10916 }
10917
10918 /*
10919 ** This is a different callback routine used for dumping the database.
10920 ** Each row received by this callback consists of a table name,
10921 ** the table type ("index" or "table") and SQL to create the table.
10922 ** This routine should print text sufficient to recreate the table.
10923 */
10924 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
10925   int rc;
10926   const char *zTable;
10927   const char *zType;
10928   const char *zSql;
10929   ShellState *p = (ShellState *)pArg;
10930
10931   UNUSED_PARAMETER(azNotUsed);
10932   if( nArg!=3 || azArg==0 ) return 0;
10933   zTable = azArg[0];
10934   zType = azArg[1];
10935   zSql = azArg[2];
10936
10937   if( strcmp(zTable, "sqlite_sequence")==0 ){
10938     raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
10939   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
10940     raw_printf(p->out, "ANALYZE sqlite_master;\n");
10941   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
10942     return 0;
10943   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
10944     char *zIns;
10945     if( !p->writableSchema ){
10946       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
10947       p->writableSchema = 1;
10948     }
10949     zIns = sqlite3_mprintf(
10950        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
10951        "VALUES('table','%q','%q',0,'%q');",
10952        zTable, zTable, zSql);
10953     utf8_printf(p->out, "%s\n", zIns);
10954     sqlite3_free(zIns);
10955     return 0;
10956   }else{
10957     printSchemaLine(p->out, zSql, ";\n");
10958   }
10959
10960   if( strcmp(zType, "table")==0 ){
10961     ShellText sSelect;
10962     ShellText sTable;
10963     char **azCol;
10964     int i;
10965     char *savedDestTable;
10966     int savedMode;
10967
10968     azCol = tableColumnList(p, zTable);
10969     if( azCol==0 ){
10970       p->nErr++;
10971       return 0;
10972     }
10973
10974     /* Always quote the table name, even if it appears to be pure ascii,
10975     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
10976     initText(&sTable);
10977     appendText(&sTable, zTable, quoteChar(zTable));
10978     /* If preserving the rowid, add a column list after the table name.
10979     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
10980     ** instead of the usual "INSERT INTO tab VALUES(...)".
10981     */
10982     if( azCol[0] ){
10983       appendText(&sTable, "(", 0);
10984       appendText(&sTable, azCol[0], 0);
10985       for(i=1; azCol[i]; i++){
10986         appendText(&sTable, ",", 0);
10987         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
10988       }
10989       appendText(&sTable, ")", 0);
10990     }
10991
10992     /* Build an appropriate SELECT statement */
10993     initText(&sSelect);
10994     appendText(&sSelect, "SELECT ", 0);
10995     if( azCol[0] ){
10996       appendText(&sSelect, azCol[0], 0);
10997       appendText(&sSelect, ",", 0);
10998     }
10999     for(i=1; azCol[i]; i++){
11000       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
11001       if( azCol[i+1] ){
11002         appendText(&sSelect, ",", 0);
11003       }
11004     }
11005     freeColumnList(azCol);
11006     appendText(&sSelect, " FROM ", 0);
11007     appendText(&sSelect, zTable, quoteChar(zTable));
11008
11009     savedDestTable = p->zDestTable;
11010     savedMode = p->mode;
11011     p->zDestTable = sTable.z;
11012     p->mode = p->cMode = MODE_Insert;
11013     rc = shell_exec(p, sSelect.z, 0);
11014     if( (rc&0xff)==SQLITE_CORRUPT ){
11015       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11016       toggleSelectOrder(p->db);
11017       shell_exec(p, sSelect.z, 0);
11018       toggleSelectOrder(p->db);
11019     }
11020     p->zDestTable = savedDestTable;
11021     p->mode = savedMode;
11022     freeText(&sTable);
11023     freeText(&sSelect);
11024     if( rc ) p->nErr++;
11025   }
11026   return 0;
11027 }
11028
11029 /*
11030 ** Run zQuery.  Use dump_callback() as the callback routine so that
11031 ** the contents of the query are output as SQL statements.
11032 **
11033 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
11034 ** "ORDER BY rowid DESC" to the end.
11035 */
11036 static int run_schema_dump_query(
11037   ShellState *p,
11038   const char *zQuery
11039 ){
11040   int rc;
11041   char *zErr = 0;
11042   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
11043   if( rc==SQLITE_CORRUPT ){
11044     char *zQ2;
11045     int len = strlen30(zQuery);
11046     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11047     if( zErr ){
11048       utf8_printf(p->out, "/****** %s ******/\n", zErr);
11049       sqlite3_free(zErr);
11050       zErr = 0;
11051     }
11052     zQ2 = malloc( len+100 );
11053     if( zQ2==0 ) return rc;
11054     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
11055     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
11056     if( rc ){
11057       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
11058     }else{
11059       rc = SQLITE_CORRUPT;
11060     }
11061     sqlite3_free(zErr);
11062     free(zQ2);
11063   }
11064   return rc;
11065 }
11066
11067 /*
11068 ** Text of help messages.
11069 **
11070 ** The help text for each individual command begins with a line that starts
11071 ** with ".".  Subsequent lines are supplimental information.
11072 **
11073 ** There must be two or more spaces between the end of the command and the
11074 ** start of the description of what that command does.
11075 */
11076 static const char *(azHelp[]) = {
11077 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11078   ".archive ...             Manage SQL archives",
11079   "   Each command must have exactly one of the following options:",
11080   "     -c, --create               Create a new archive",
11081   "     -u, --update               Update or add files to an existing archive",
11082   "     -t, --list                 List contents of archive",
11083   "     -x, --extract              Extract files from archive",
11084   "   Optional arguments:",
11085   "     -v, --verbose              Print each filename as it is processed",
11086   "     -f FILE, --file FILE       Operate on archive FILE (default is current db)",
11087   "     -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS",
11088   "     -C DIR, --directory DIR    Change to directory DIR to read/extract files",
11089   "     -n, --dryrun               Show the SQL that would have occurred",
11090   "   Examples:",
11091   "     .ar -cf archive.sar foo bar  # Create archive.sar from files foo and bar",
11092   "     .ar -tf archive.sar          # List members of archive.sar",
11093   "     .ar -xvf archive.sar         # Verbosely extract files from archive.sar",
11094   "   See also:",
11095   "      http://sqlite.org/cli.html#sqlar_archive_support",
11096 #endif
11097 #ifndef SQLITE_OMIT_AUTHORIZATION
11098   ".auth ON|OFF             Show authorizer callbacks",
11099 #endif
11100   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
11101   "       --append            Use the appendvfs",
11102   "       --async             Write to FILE without a journal and without fsync()",
11103   ".bail on|off             Stop after hitting an error.  Default OFF",
11104   ".binary on|off           Turn binary output on or off.  Default OFF",
11105   ".cd DIRECTORY            Change the working directory to DIRECTORY",
11106   ".changes on|off          Show number of rows changed by SQL",
11107   ".check GLOB              Fail if output since .testcase does not match",
11108   ".clone NEWDB             Clone data into NEWDB from the existing database",
11109   ".databases               List names and files of attached databases",
11110   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
11111   ".dbinfo ?DB?             Show status information about the database",
11112   ".dump ?TABLE? ...        Render all database content as SQL",
11113   "   Options:",
11114   "     --preserve-rowids      Include ROWID values in the output",
11115   "     --newlines             Allow unescaped newline characters in output",
11116   "   TABLE is LIKE pattern for the tables to dump",
11117   ".echo on|off             Turn command echo on or off",
11118   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
11119   "   Other Modes:",
11120 #ifdef SQLITE_DEBUG
11121   "      test                  Show raw EXPLAIN QUERY PLAN output",
11122   "      trace                 Like \"full\" but also enable \"PRAGMA vdbe_trace\"",
11123 #endif
11124   "      trigger               Like \"full\" but also show trigger bytecode",
11125   ".excel                   Display the output of next command in a spreadsheet",
11126   ".exit ?CODE?             Exit this program with return-code CODE",
11127   ".expert                  EXPERIMENTAL. Suggest indexes for specified queries",
11128 /* Because explain mode comes on automatically now, the ".explain" mode
11129 ** is removed from the help screen.  It is still supported for legacy, however */
11130 /*".explain ?on|off|auto?   Turn EXPLAIN output mode on or off or to automatic",*/
11131   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
11132   ".headers on|off          Turn display of headers on or off",
11133   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
11134   ".import FILE TABLE       Import data from FILE into TABLE",
11135 #ifndef SQLITE_OMIT_TEST_CONTROL
11136   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
11137 #endif
11138   ".indexes ?TABLE?         Show names of indexes",
11139   "                           If TABLE is specified, only show indexes for",
11140   "                           tables matching TABLE using the LIKE operator.",
11141 #ifdef SQLITE_ENABLE_IOTRACE
11142   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
11143 #endif
11144   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
11145   ".lint OPTIONS            Report potential schema issues.",
11146   "     Options:",
11147   "        fkey-indexes     Find missing foreign key indexes",
11148 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11149   ".load FILE ?ENTRY?       Load an extension library",
11150 #endif
11151   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
11152   ".mode MODE ?TABLE?       Set output mode",
11153   "   MODE is one of:",
11154   "     ascii    Columns/rows delimited by 0x1F and 0x1E",
11155   "     csv      Comma-separated values",
11156   "     column   Left-aligned columns.  (See .width)",
11157   "     html     HTML <table> code",
11158   "     insert   SQL insert statements for TABLE",
11159   "     line     One value per line",
11160   "     list     Values delimited by \"|\"",
11161   "     quote    Escape answers as for SQL",
11162   "     tabs     Tab-separated values",
11163   "     tcl      TCL list elements",
11164   ".nullvalue STRING        Use STRING in place of NULL values",
11165   ".once (-e|-x|FILE)       Output for the next SQL command only to FILE",
11166   "     If FILE begins with '|' then open as a pipe",
11167   "     Other options:",
11168   "       -e    Invoke system text editor",
11169   "       -x    Open in a spreadsheet",
11170   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
11171   "     Options:",
11172   "        --append        Use appendvfs to append database to the end of FILE",
11173 #ifdef SQLITE_ENABLE_DESERIALIZE
11174   "        --deserialize   Load into memory useing sqlite3_deserialize()",
11175   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory database",
11176   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
11177 #endif
11178   "        --new           Initialize FILE to an empty database",
11179   "        --readonly      Open FILE readonly",
11180   "        --zip           FILE is a ZIP archive",
11181   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
11182   "     If FILE begins with '|' then open it as a pipe.",
11183   ".print STRING...         Print literal STRING",
11184 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11185   ".progress N              Invoke progress handler after every N opcodes",
11186   "   --limit N                 Interrupt after N progress callbacks",
11187   "   --once                    Do no more than one progress interrupt",
11188   "   --quiet|-q                No output except at interrupts",
11189   "   --reset                   Reset the count for each input and interrupt",
11190 #endif
11191   ".prompt MAIN CONTINUE    Replace the standard prompts",
11192   ".quit                    Exit this program",
11193   ".read FILE               Read input from FILE",
11194   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
11195   ".save FILE               Write in-memory database into FILE",
11196   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
11197   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
11198   "     Options:",
11199   "         --indent            Try to pretty-print the schema",
11200   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
11201   "    Options:",
11202   "       --init               Create a new SELFTEST table",
11203   "       -v                   Verbose output",
11204   ".separator COL ?ROW?     Change the column and row separators",
11205 #if defined(SQLITE_ENABLE_SESSION)
11206   ".session ?NAME? CMD ...  Create or control sessions",
11207   "   Subcommands:",
11208   "     attach TABLE             Attach TABLE",
11209   "     changeset FILE           Write a changeset into FILE",
11210   "     close                    Close one session",
11211   "     enable ?BOOLEAN?         Set or query the enable bit",
11212   "     filter GLOB...           Reject tables matching GLOBs",
11213   "     indirect ?BOOLEAN?       Mark or query the indirect status",
11214   "     isempty                  Query whether the session is empty",
11215   "     list                     List currently open session names",
11216   "     open DB NAME             Open a new session on DB",
11217   "     patchset FILE            Write a patchset into FILE",
11218   "   If ?NAME? is omitted, the first defined session is used.",
11219 #endif
11220   ".sha3sum ...             Compute a SHA3 hash of database content",
11221   "    Options:",
11222   "      --schema              Also hash the sqlite_master table",
11223   "      --sha3-224            Use the sha3-224 algorithm",
11224   "      --sha3-256            Use the sha3-256 algorithm.  This is the default.",
11225   "      --sha3-384            Use the sha3-384 algorithm",
11226   "      --sha3-512            Use the sha3-512 algorithm",
11227   "    Any other argument is a LIKE pattern for tables to hash",
11228 #ifndef SQLITE_NOHAVE_SYSTEM
11229   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
11230 #endif
11231   ".show                    Show the current values for various settings",
11232   ".stats ?on|off?          Show stats or turn stats on or off",
11233 #ifndef SQLITE_NOHAVE_SYSTEM
11234   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
11235 #endif
11236   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
11237   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
11238   ".timeout MS              Try opening locked tables for MS milliseconds",
11239   ".timer on|off            Turn SQL timer on or off",
11240 #ifndef SQLITE_OMIT_TRACE
11241   ".trace ?OPTIONS?         Output each SQL statement as it is run",
11242   "    FILE                    Send output to FILE",
11243   "    stdout                  Send output to stdout",
11244   "    stderr                  Send output to stderr",
11245   "    off                     Disable tracing",
11246   "    --expanded              Expand query parameters",
11247 #ifdef SQLITE_ENABLE_NORMALIZE
11248   "    --normalized            Normal the SQL statements",
11249 #endif
11250   "    --plain                 Show SQL as it is input",
11251   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
11252   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
11253   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
11254   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
11255 #endif /* SQLITE_OMIT_TRACE */
11256   ".vfsinfo ?AUX?           Information about the top-level VFS",
11257   ".vfslist                 List all available VFSes",
11258   ".vfsname ?AUX?           Print the name of the VFS stack",
11259   ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
11260   "     Negative values right-justify",
11261 };
11262
11263 /*
11264 ** Output help text.
11265 **
11266 ** zPattern describes the set of commands for which help text is provided.
11267 ** If zPattern is NULL, then show all commands, but only give a one-line
11268 ** description of each.
11269 **
11270 ** Return the number of matches.
11271 */
11272 static int showHelp(FILE *out, const char *zPattern){
11273   int i = 0;
11274   int j = 0;
11275   int n = 0;
11276   char *zPat;
11277   if( zPattern==0
11278    || zPattern[0]=='0'
11279    || strcmp(zPattern,"-a")==0
11280    || strcmp(zPattern,"-all")==0
11281   ){
11282     /* Show all commands, but only one line per command */
11283     if( zPattern==0 ) zPattern = "";
11284     for(i=0; i<ArraySize(azHelp); i++){
11285       if( azHelp[i][0]=='.' || zPattern[0] ){
11286         utf8_printf(out, "%s\n", azHelp[i]);
11287         n++;
11288       }
11289     }
11290   }else{
11291     /* Look for commands that for which zPattern is an exact prefix */
11292     zPat = sqlite3_mprintf(".%s*", zPattern);
11293     for(i=0; i<ArraySize(azHelp); i++){
11294       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
11295         utf8_printf(out, "%s\n", azHelp[i]);
11296         j = i+1;
11297         n++;
11298       }
11299     }
11300     sqlite3_free(zPat);
11301     if( n ){
11302       if( n==1 ){
11303         /* when zPattern is a prefix of exactly one command, then include the
11304         ** details of that command, which should begin at offset j */
11305         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
11306           utf8_printf(out, "%s\n", azHelp[j]);
11307           j++;
11308         }
11309       }
11310       return n;
11311     }
11312     /* Look for commands that contain zPattern anywhere.  Show the complete
11313     ** text of all commands that match. */
11314     zPat = sqlite3_mprintf("%%%s%%", zPattern);
11315     for(i=0; i<ArraySize(azHelp); i++){
11316       if( azHelp[i][0]=='.' ) j = i;
11317       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
11318         utf8_printf(out, "%s\n", azHelp[j]);
11319         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
11320           j++;
11321           utf8_printf(out, "%s\n", azHelp[j]);
11322         }
11323         i = j;
11324         n++;
11325       }
11326     }
11327     sqlite3_free(zPat);
11328   }
11329   return n;
11330 }
11331
11332 /* Forward reference */
11333 static int process_input(ShellState *p);
11334
11335 /*
11336 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
11337 ** and return a pointer to the buffer. The caller is responsible for freeing
11338 ** the memory.
11339 **
11340 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
11341 ** read.
11342 **
11343 ** For convenience, a nul-terminator byte is always appended to the data read
11344 ** from the file before the buffer is returned. This byte is not included in
11345 ** the final value of (*pnByte), if applicable.
11346 **
11347 ** NULL is returned if any error is encountered. The final value of *pnByte
11348 ** is undefined in this case.
11349 */
11350 static char *readFile(const char *zName, int *pnByte){
11351   FILE *in = fopen(zName, "rb");
11352   long nIn;
11353   size_t nRead;
11354   char *pBuf;
11355   if( in==0 ) return 0;
11356   fseek(in, 0, SEEK_END);
11357   nIn = ftell(in);
11358   rewind(in);
11359   pBuf = sqlite3_malloc64( nIn+1 );
11360   if( pBuf==0 ){ fclose(in); return 0; }
11361   nRead = fread(pBuf, nIn, 1, in);
11362   fclose(in);
11363   if( nRead!=1 ){
11364     sqlite3_free(pBuf);
11365     return 0;
11366   }
11367   pBuf[nIn] = 0;
11368   if( pnByte ) *pnByte = nIn;
11369   return pBuf;
11370 }
11371
11372 #if defined(SQLITE_ENABLE_SESSION)
11373 /*
11374 ** Close a single OpenSession object and release all of its associated
11375 ** resources.
11376 */
11377 static void session_close(OpenSession *pSession){
11378   int i;
11379   sqlite3session_delete(pSession->p);
11380   sqlite3_free(pSession->zName);
11381   for(i=0; i<pSession->nFilter; i++){
11382     sqlite3_free(pSession->azFilter[i]);
11383   }
11384   sqlite3_free(pSession->azFilter);
11385   memset(pSession, 0, sizeof(OpenSession));
11386 }
11387 #endif
11388
11389 /*
11390 ** Close all OpenSession objects and release all associated resources.
11391 */
11392 #if defined(SQLITE_ENABLE_SESSION)
11393 static void session_close_all(ShellState *p){
11394   int i;
11395   for(i=0; i<p->nSession; i++){
11396     session_close(&p->aSession[i]);
11397   }
11398   p->nSession = 0;
11399 }
11400 #else
11401 # define session_close_all(X)
11402 #endif
11403
11404 /*
11405 ** Implementation of the xFilter function for an open session.  Omit
11406 ** any tables named by ".session filter" but let all other table through.
11407 */
11408 #if defined(SQLITE_ENABLE_SESSION)
11409 static int session_filter(void *pCtx, const char *zTab){
11410   OpenSession *pSession = (OpenSession*)pCtx;
11411   int i;
11412   for(i=0; i<pSession->nFilter; i++){
11413     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
11414   }
11415   return 1;
11416 }
11417 #endif
11418
11419 /*
11420 ** Try to deduce the type of file for zName based on its content.  Return
11421 ** one of the SHELL_OPEN_* constants.
11422 **
11423 ** If the file does not exist or is empty but its name looks like a ZIP
11424 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
11425 ** Otherwise, assume an ordinary database regardless of the filename if
11426 ** the type cannot be determined from content.
11427 */
11428 int deduceDatabaseType(const char *zName, int dfltZip){
11429   FILE *f = fopen(zName, "rb");
11430   size_t n;
11431   int rc = SHELL_OPEN_UNSPEC;
11432   char zBuf[100];
11433   if( f==0 ){
11434     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11435        return SHELL_OPEN_ZIPFILE;
11436     }else{
11437        return SHELL_OPEN_NORMAL;
11438     }
11439   }
11440   n = fread(zBuf, 16, 1, f);
11441   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
11442     fclose(f);
11443     return SHELL_OPEN_NORMAL;
11444   }
11445   fseek(f, -25, SEEK_END);
11446   n = fread(zBuf, 25, 1, f);
11447   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11448     rc = SHELL_OPEN_APPENDVFS;
11449   }else{
11450     fseek(f, -22, SEEK_END);
11451     n = fread(zBuf, 22, 1, f);
11452     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11453        && zBuf[3]==0x06 ){
11454       rc = SHELL_OPEN_ZIPFILE;
11455     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11456       rc = SHELL_OPEN_ZIPFILE;
11457     }
11458   }
11459   fclose(f);
11460   return rc;  
11461 }
11462
11463 #ifdef SQLITE_ENABLE_DESERIALIZE
11464 /*
11465 ** Reconstruct an in-memory database using the output from the "dbtotxt"
11466 ** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
11467 ** is 0, then read from standard input.
11468 */
11469 static unsigned char *readHexDb(ShellState *p, int *pnData){
11470   unsigned char *a = 0;
11471   int nLine;
11472   int n = 0;
11473   int pgsz = 0;
11474   int iOffset = 0;
11475   int j, k;
11476   int rc;
11477   FILE *in;
11478   unsigned char x[16];
11479   char zLine[1000];
11480   if( p->zDbFilename ){
11481     in = fopen(p->zDbFilename, "r");
11482     if( in==0 ){
11483       utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
11484       return 0;
11485     }
11486     nLine = 0;
11487   }else{
11488     in = p->in;
11489     nLine = p->lineno;
11490   }
11491   *pnData = 0;
11492   nLine++;
11493   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
11494   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
11495   if( rc!=2 ) goto readHexDb_error;
11496   if( n<=0 ) goto readHexDb_error;
11497   a = sqlite3_malloc( n );
11498   if( a==0 ){
11499     utf8_printf(stderr, "Out of memory!\n");
11500     goto readHexDb_error;
11501   }
11502   memset(a, 0, n);
11503   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
11504     utf8_printf(stderr, "invalid pagesize\n");
11505     goto readHexDb_error;
11506   }
11507   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
11508     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
11509     if( rc==2 ){
11510       iOffset = k;
11511       continue;
11512     }
11513     if( strncmp(zLine, "| end ", 6)==0 ){
11514       break;
11515     }
11516     rc = sscanf(zLine,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
11517                       "  %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
11518                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
11519                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
11520     if( rc==17 ){
11521       k = iOffset+j;
11522       if( k+16<=n ){
11523         memcpy(a+k, x, 16);
11524       }
11525     }
11526   }
11527   *pnData = n;
11528   if( in!=p->in ){
11529     fclose(in);
11530   }else{
11531     p->lineno = nLine;
11532   }
11533   return a;
11534
11535 readHexDb_error:
11536   if( in!=stdin ){
11537     fclose(in);
11538   }else{
11539     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
11540       nLine++;
11541       if(strncmp(zLine, "| end ", 6)==0 ) break;
11542     }
11543     p->lineno = nLine;
11544   }
11545   sqlite3_free(a);
11546   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
11547   return 0;
11548 }
11549 #endif /* SQLITE_ENABLE_DESERIALIZE */
11550
11551 /* Flags for open_db().
11552 **
11553 ** The default behavior of open_db() is to exit(1) if the database fails to
11554 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11555 ** but still returns without calling exit.
11556 **
11557 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11558 ** ZIP archive if the file does not exist or is empty and its name matches
11559 ** the *.zip pattern.
11560 */
11561 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
11562 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
11563
11564 /*
11565 ** Make sure the database is open.  If it is not, then open it.  If
11566 ** the database fails to open, print an error message and exit.
11567 */
11568 static void open_db(ShellState *p, int openFlags){
11569   if( p->db==0 ){
11570     if( p->openMode==SHELL_OPEN_UNSPEC ){
11571       if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11572         p->openMode = SHELL_OPEN_NORMAL;
11573       }else{
11574         p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 
11575                              (openFlags & OPEN_DB_ZIPFILE)!=0);
11576       }
11577     }
11578     switch( p->openMode ){
11579       case SHELL_OPEN_APPENDVFS: {
11580         sqlite3_open_v2(p->zDbFilename, &p->db, 
11581            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
11582         break;
11583       }
11584       case SHELL_OPEN_HEXDB:
11585       case SHELL_OPEN_DESERIALIZE: {
11586         sqlite3_open(0, &p->db);
11587         break;
11588       }
11589       case SHELL_OPEN_ZIPFILE: {
11590         sqlite3_open(":memory:", &p->db);
11591         break;
11592       }
11593       case SHELL_OPEN_READONLY: {
11594         sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
11595         break;
11596       }
11597       case SHELL_OPEN_UNSPEC:
11598       case SHELL_OPEN_NORMAL: {
11599         sqlite3_open(p->zDbFilename, &p->db);
11600         break;
11601       }
11602     }
11603     globalDb = p->db;
11604     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11605       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11606           p->zDbFilename, sqlite3_errmsg(p->db));
11607       if( openFlags & OPEN_DB_KEEPALIVE ){
11608         sqlite3_open(":memory:", &p->db);
11609         return;
11610       }
11611       exit(1);
11612     }
11613 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11614     sqlite3_enable_load_extension(p->db, 1);
11615 #endif
11616     sqlite3_fileio_init(p->db, 0, 0);
11617     sqlite3_shathree_init(p->db, 0, 0);
11618     sqlite3_completion_init(p->db, 0, 0);
11619 #ifdef SQLITE_HAVE_ZLIB
11620     sqlite3_zipfile_init(p->db, 0, 0);
11621     sqlite3_sqlar_init(p->db, 0, 0);
11622 #endif
11623     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
11624                             shellAddSchemaName, 0, 0);
11625     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
11626                             shellModuleSchema, 0, 0);
11627     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
11628                             shellPutsFunc, 0, 0);
11629 #ifndef SQLITE_NOHAVE_SYSTEM
11630     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
11631                             editFunc, 0, 0);
11632     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
11633                             editFunc, 0, 0);
11634 #endif
11635     if( p->openMode==SHELL_OPEN_ZIPFILE ){
11636       char *zSql = sqlite3_mprintf(
11637          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
11638       sqlite3_exec(p->db, zSql, 0, 0, 0);
11639       sqlite3_free(zSql);
11640     }
11641 #ifdef SQLITE_ENABLE_DESERIALIZE
11642     else
11643     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
11644       int rc;
11645       int nData = 0;
11646       unsigned char *aData;
11647       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
11648         aData = (unsigned char*)readFile(p->zDbFilename, &nData);
11649       }else{
11650         aData = readHexDb(p, &nData);
11651         if( aData==0 ){
11652           utf8_printf(stderr, "Error in hexdb input\n");
11653           return;
11654         }
11655       }
11656       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
11657                    SQLITE_DESERIALIZE_RESIZEABLE |
11658                    SQLITE_DESERIALIZE_FREEONCLOSE);
11659       if( rc ){
11660         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
11661       }
11662       if( p->szMax>0 ){
11663         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
11664       }
11665     }
11666 #endif
11667   }
11668 }
11669
11670 /*
11671 ** Attempt to close the databaes connection.  Report errors.
11672 */
11673 void close_db(sqlite3 *db){
11674   int rc = sqlite3_close(db);
11675   if( rc ){
11676     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11677         rc, sqlite3_errmsg(db));
11678   } 
11679 }
11680
11681 #if HAVE_READLINE || HAVE_EDITLINE
11682 /*
11683 ** Readline completion callbacks
11684 */
11685 static char *readline_completion_generator(const char *text, int state){
11686   static sqlite3_stmt *pStmt = 0;
11687   char *zRet;
11688   if( state==0 ){
11689     char *zSql;
11690     sqlite3_finalize(pStmt);
11691     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11692                            "  FROM completion(%Q) ORDER BY 1", text);
11693     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11694     sqlite3_free(zSql);
11695   }
11696   if( sqlite3_step(pStmt)==SQLITE_ROW ){
11697     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
11698   }else{
11699     sqlite3_finalize(pStmt);
11700     pStmt = 0;
11701     zRet = 0;
11702   }
11703   return zRet;
11704 }
11705 static char **readline_completion(const char *zText, int iStart, int iEnd){
11706   rl_attempted_completion_over = 1;
11707   return rl_completion_matches(zText, readline_completion_generator);
11708 }
11709
11710 #elif HAVE_LINENOISE
11711 /*
11712 ** Linenoise completion callback
11713 */
11714 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
11715   int nLine = strlen30(zLine);
11716   int i, iStart;
11717   sqlite3_stmt *pStmt = 0;
11718   char *zSql;
11719   char zBuf[1000];
11720
11721   if( nLine>sizeof(zBuf)-30 ) return;
11722   if( zLine[0]=='.' || zLine[0]=='#') return;
11723   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
11724   if( i==nLine-1 ) return;
11725   iStart = i+1;
11726   memcpy(zBuf, zLine, iStart);
11727   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11728                          "  FROM completion(%Q,%Q) ORDER BY 1",
11729                          &zLine[iStart], zLine);
11730   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11731   sqlite3_free(zSql);
11732   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
11733   while( sqlite3_step(pStmt)==SQLITE_ROW ){
11734     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
11735     int nCompletion = sqlite3_column_bytes(pStmt, 0);
11736     if( iStart+nCompletion < sizeof(zBuf)-1 ){
11737       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
11738       linenoiseAddCompletion(lc, zBuf);
11739     }
11740   }
11741   sqlite3_finalize(pStmt);
11742 }
11743 #endif
11744
11745 /*
11746 ** Do C-language style dequoting.
11747 **
11748 **    \a    -> alarm
11749 **    \b    -> backspace
11750 **    \t    -> tab
11751 **    \n    -> newline
11752 **    \v    -> vertical tab
11753 **    \f    -> form feed
11754 **    \r    -> carriage return
11755 **    \s    -> space
11756 **    \"    -> "
11757 **    \'    -> '
11758 **    \\    -> backslash
11759 **    \NNN  -> ascii character NNN in octal
11760 */
11761 static void resolve_backslashes(char *z){
11762   int i, j;
11763   char c;
11764   while( *z && *z!='\\' ) z++;
11765   for(i=j=0; (c = z[i])!=0; i++, j++){
11766     if( c=='\\' && z[i+1]!=0 ){
11767       c = z[++i];
11768       if( c=='a' ){
11769         c = '\a';
11770       }else if( c=='b' ){
11771         c = '\b';
11772       }else if( c=='t' ){
11773         c = '\t';
11774       }else if( c=='n' ){
11775         c = '\n';
11776       }else if( c=='v' ){
11777         c = '\v';
11778       }else if( c=='f' ){
11779         c = '\f';
11780       }else if( c=='r' ){
11781         c = '\r';
11782       }else if( c=='"' ){
11783         c = '"';
11784       }else if( c=='\'' ){
11785         c = '\'';
11786       }else if( c=='\\' ){
11787         c = '\\';
11788       }else if( c>='0' && c<='7' ){
11789         c -= '0';
11790         if( z[i+1]>='0' && z[i+1]<='7' ){
11791           i++;
11792           c = (c<<3) + z[i] - '0';
11793           if( z[i+1]>='0' && z[i+1]<='7' ){
11794             i++;
11795             c = (c<<3) + z[i] - '0';
11796           }
11797         }
11798       }
11799     }
11800     z[j] = c;
11801   }
11802   if( j<i ) z[j] = 0;
11803 }
11804
11805 /*
11806 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
11807 ** for TRUE and FALSE.  Return the integer value if appropriate.
11808 */
11809 static int booleanValue(const char *zArg){
11810   int i;
11811   if( zArg[0]=='0' && zArg[1]=='x' ){
11812     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
11813   }else{
11814     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
11815   }
11816   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
11817   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
11818     return 1;
11819   }
11820   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
11821     return 0;
11822   }
11823   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
11824           zArg);
11825   return 0;
11826 }
11827
11828 /*
11829 ** Set or clear a shell flag according to a boolean value.
11830 */
11831 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
11832   if( booleanValue(zArg) ){
11833     ShellSetFlag(p, mFlag);
11834   }else{
11835     ShellClearFlag(p, mFlag);
11836   }
11837 }
11838
11839 /*
11840 ** Close an output file, assuming it is not stderr or stdout
11841 */
11842 static void output_file_close(FILE *f){
11843   if( f && f!=stdout && f!=stderr ) fclose(f);
11844 }
11845
11846 /*
11847 ** Try to open an output file.   The names "stdout" and "stderr" are
11848 ** recognized and do the right thing.  NULL is returned if the output
11849 ** filename is "off".
11850 */
11851 static FILE *output_file_open(const char *zFile, int bTextMode){
11852   FILE *f;
11853   if( strcmp(zFile,"stdout")==0 ){
11854     f = stdout;
11855   }else if( strcmp(zFile, "stderr")==0 ){
11856     f = stderr;
11857   }else if( strcmp(zFile, "off")==0 ){
11858     f = 0;
11859   }else{
11860     f = fopen(zFile, bTextMode ? "w" : "wb");
11861     if( f==0 ){
11862       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
11863     }
11864   }
11865   return f;
11866 }
11867
11868 #ifndef SQLITE_OMIT_TRACE
11869 /*
11870 ** A routine for handling output from sqlite3_trace().
11871 */
11872 static int sql_trace_callback(
11873   unsigned mType,         /* The trace type */
11874   void *pArg,             /* The ShellState pointer */
11875   void *pP,               /* Usually a pointer to sqlite_stmt */
11876   void *pX                /* Auxiliary output */
11877 ){
11878   ShellState *p = (ShellState*)pArg;
11879   sqlite3_stmt *pStmt;
11880   const char *zSql;
11881   int nSql;
11882   if( p->traceOut==0 ) return 0;
11883   if( mType==SQLITE_TRACE_CLOSE ){
11884     utf8_printf(p->traceOut, "-- closing database connection\n");
11885     return 0;
11886   }
11887   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
11888     zSql = (const char*)pX;
11889   }else{
11890     pStmt = (sqlite3_stmt*)pP;
11891     switch( p->eTraceType ){
11892       case SHELL_TRACE_EXPANDED: {
11893         zSql = sqlite3_expanded_sql(pStmt);
11894         break;
11895       }
11896 #ifdef SQLITE_ENABLE_NORMALIZE
11897       case SHELL_TRACE_NORMALIZED: {
11898         zSql = sqlite3_normalized_sql(pStmt);
11899         break;
11900       }
11901 #endif
11902       default: {
11903         zSql = sqlite3_sql(pStmt);
11904         break;
11905       }
11906     }
11907   }
11908   if( zSql==0 ) return 0;
11909   nSql = strlen30(zSql);
11910   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
11911   switch( mType ){
11912     case SQLITE_TRACE_ROW:
11913     case SQLITE_TRACE_STMT: {
11914       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
11915       break;
11916     }
11917     case SQLITE_TRACE_PROFILE: {
11918       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
11919       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
11920       break;
11921     }
11922   }
11923   return 0;
11924 }
11925 #endif
11926
11927 /*
11928 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
11929 ** a useful spot to set a debugger breakpoint.
11930 */
11931 static void test_breakpoint(void){
11932   static int nCall = 0;
11933   nCall++;
11934 }
11935
11936 /*
11937 ** An object used to read a CSV and other files for import.
11938 */
11939 typedef struct ImportCtx ImportCtx;
11940 struct ImportCtx {
11941   const char *zFile;  /* Name of the input file */
11942   FILE *in;           /* Read the CSV text from this input stream */
11943   char *z;            /* Accumulated text for a field */
11944   int n;              /* Number of bytes in z */
11945   int nAlloc;         /* Space allocated for z[] */
11946   int nLine;          /* Current line number */
11947   int bNotFirst;      /* True if one or more bytes already read */
11948   int cTerm;          /* Character that terminated the most recent field */
11949   int cColSep;        /* The column separator character.  (Usually ",") */
11950   int cRowSep;        /* The row separator character.  (Usually "\n") */
11951 };
11952
11953 /* Append a single byte to z[] */
11954 static void import_append_char(ImportCtx *p, int c){
11955   if( p->n+1>=p->nAlloc ){
11956     p->nAlloc += p->nAlloc + 100;
11957     p->z = sqlite3_realloc64(p->z, p->nAlloc);
11958     if( p->z==0 ) shell_out_of_memory();
11959   }
11960   p->z[p->n++] = (char)c;
11961 }
11962
11963 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
11964 ** with the option of having a separator other than ",".
11965 **
11966 **   +  Input comes from p->in.
11967 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
11968 **      from sqlite3_malloc64().
11969 **   +  Use p->cSep as the column separator.  The default is ",".
11970 **   +  Use p->rSep as the row separator.  The default is "\n".
11971 **   +  Keep track of the line number in p->nLine.
11972 **   +  Store the character that terminates the field in p->cTerm.  Store
11973 **      EOF on end-of-file.
11974 **   +  Report syntax errors on stderr
11975 */
11976 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
11977   int c;
11978   int cSep = p->cColSep;
11979   int rSep = p->cRowSep;
11980   p->n = 0;
11981   c = fgetc(p->in);
11982   if( c==EOF || seenInterrupt ){
11983     p->cTerm = EOF;
11984     return 0;
11985   }
11986   if( c=='"' ){
11987     int pc, ppc;
11988     int startLine = p->nLine;
11989     int cQuote = c;
11990     pc = ppc = 0;
11991     while( 1 ){
11992       c = fgetc(p->in);
11993       if( c==rSep ) p->nLine++;
11994       if( c==cQuote ){
11995         if( pc==cQuote ){
11996           pc = 0;
11997           continue;
11998         }
11999       }
12000       if( (c==cSep && pc==cQuote)
12001        || (c==rSep && pc==cQuote)
12002        || (c==rSep && pc=='\r' && ppc==cQuote)
12003        || (c==EOF && pc==cQuote)
12004       ){
12005         do{ p->n--; }while( p->z[p->n]!=cQuote );
12006         p->cTerm = c;
12007         break;
12008       }
12009       if( pc==cQuote && c!='\r' ){
12010         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
12011                 p->zFile, p->nLine, cQuote);
12012       }
12013       if( c==EOF ){
12014         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
12015                 p->zFile, startLine, cQuote);
12016         p->cTerm = c;
12017         break;
12018       }
12019       import_append_char(p, c);
12020       ppc = pc;
12021       pc = c;
12022     }
12023   }else{
12024     /* If this is the first field being parsed and it begins with the
12025     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
12026     if( (c&0xff)==0xef && p->bNotFirst==0 ){
12027       import_append_char(p, c);
12028       c = fgetc(p->in);
12029       if( (c&0xff)==0xbb ){
12030         import_append_char(p, c);
12031         c = fgetc(p->in);
12032         if( (c&0xff)==0xbf ){
12033           p->bNotFirst = 1;
12034           p->n = 0;
12035           return csv_read_one_field(p);
12036         }
12037       }
12038     }
12039     while( c!=EOF && c!=cSep && c!=rSep ){
12040       import_append_char(p, c);
12041       c = fgetc(p->in);
12042     }
12043     if( c==rSep ){
12044       p->nLine++;
12045       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
12046     }
12047     p->cTerm = c;
12048   }
12049   if( p->z ) p->z[p->n] = 0;
12050   p->bNotFirst = 1;
12051   return p->z;
12052 }
12053
12054 /* Read a single field of ASCII delimited text.
12055 **
12056 **   +  Input comes from p->in.
12057 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
12058 **      from sqlite3_malloc64().
12059 **   +  Use p->cSep as the column separator.  The default is "\x1F".
12060 **   +  Use p->rSep as the row separator.  The default is "\x1E".
12061 **   +  Keep track of the row number in p->nLine.
12062 **   +  Store the character that terminates the field in p->cTerm.  Store
12063 **      EOF on end-of-file.
12064 **   +  Report syntax errors on stderr
12065 */
12066 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
12067   int c;
12068   int cSep = p->cColSep;
12069   int rSep = p->cRowSep;
12070   p->n = 0;
12071   c = fgetc(p->in);
12072   if( c==EOF || seenInterrupt ){
12073     p->cTerm = EOF;
12074     return 0;
12075   }
12076   while( c!=EOF && c!=cSep && c!=rSep ){
12077     import_append_char(p, c);
12078     c = fgetc(p->in);
12079   }
12080   if( c==rSep ){
12081     p->nLine++;
12082   }
12083   p->cTerm = c;
12084   if( p->z ) p->z[p->n] = 0;
12085   return p->z;
12086 }
12087
12088 /*
12089 ** Try to transfer data for table zTable.  If an error is seen while
12090 ** moving forward, try to go backwards.  The backwards movement won't
12091 ** work for WITHOUT ROWID tables.
12092 */
12093 static void tryToCloneData(
12094   ShellState *p,
12095   sqlite3 *newDb,
12096   const char *zTable
12097 ){
12098   sqlite3_stmt *pQuery = 0;
12099   sqlite3_stmt *pInsert = 0;
12100   char *zQuery = 0;
12101   char *zInsert = 0;
12102   int rc;
12103   int i, j, n;
12104   int nTable = strlen30(zTable);
12105   int k = 0;
12106   int cnt = 0;
12107   const int spinRate = 10000;
12108
12109   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
12110   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12111   if( rc ){
12112     utf8_printf(stderr, "Error %d: %s on [%s]\n",
12113             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12114             zQuery);
12115     goto end_data_xfer;
12116   }
12117   n = sqlite3_column_count(pQuery);
12118   zInsert = sqlite3_malloc64(200 + nTable + n*3);
12119   if( zInsert==0 ) shell_out_of_memory();
12120   sqlite3_snprintf(200+nTable,zInsert,
12121                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
12122   i = strlen30(zInsert);
12123   for(j=1; j<n; j++){
12124     memcpy(zInsert+i, ",?", 2);
12125     i += 2;
12126   }
12127   memcpy(zInsert+i, ");", 3);
12128   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
12129   if( rc ){
12130     utf8_printf(stderr, "Error %d: %s on [%s]\n",
12131             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
12132             zQuery);
12133     goto end_data_xfer;
12134   }
12135   for(k=0; k<2; k++){
12136     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12137       for(i=0; i<n; i++){
12138         switch( sqlite3_column_type(pQuery, i) ){
12139           case SQLITE_NULL: {
12140             sqlite3_bind_null(pInsert, i+1);
12141             break;
12142           }
12143           case SQLITE_INTEGER: {
12144             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
12145             break;
12146           }
12147           case SQLITE_FLOAT: {
12148             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
12149             break;
12150           }
12151           case SQLITE_TEXT: {
12152             sqlite3_bind_text(pInsert, i+1,
12153                              (const char*)sqlite3_column_text(pQuery,i),
12154                              -1, SQLITE_STATIC);
12155             break;
12156           }
12157           case SQLITE_BLOB: {
12158             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
12159                                             sqlite3_column_bytes(pQuery,i),
12160                                             SQLITE_STATIC);
12161             break;
12162           }
12163         }
12164       } /* End for */
12165       rc = sqlite3_step(pInsert);
12166       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
12167         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
12168                         sqlite3_errmsg(newDb));
12169       }
12170       sqlite3_reset(pInsert);
12171       cnt++;
12172       if( (cnt%spinRate)==0 ){
12173         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
12174         fflush(stdout);
12175       }
12176     } /* End while */
12177     if( rc==SQLITE_DONE ) break;
12178     sqlite3_finalize(pQuery);
12179     sqlite3_free(zQuery);
12180     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
12181                              zTable);
12182     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12183     if( rc ){
12184       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
12185       break;
12186     }
12187   } /* End for(k=0...) */
12188
12189 end_data_xfer:
12190   sqlite3_finalize(pQuery);
12191   sqlite3_finalize(pInsert);
12192   sqlite3_free(zQuery);
12193   sqlite3_free(zInsert);
12194 }
12195
12196
12197 /*
12198 ** Try to transfer all rows of the schema that match zWhere.  For
12199 ** each row, invoke xForEach() on the object defined by that row.
12200 ** If an error is encountered while moving forward through the
12201 ** sqlite_master table, try again moving backwards.
12202 */
12203 static void tryToCloneSchema(
12204   ShellState *p,
12205   sqlite3 *newDb,
12206   const char *zWhere,
12207   void (*xForEach)(ShellState*,sqlite3*,const char*)
12208 ){
12209   sqlite3_stmt *pQuery = 0;
12210   char *zQuery = 0;
12211   int rc;
12212   const unsigned char *zName;
12213   const unsigned char *zSql;
12214   char *zErrMsg = 0;
12215
12216   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12217                            " WHERE %s", zWhere);
12218   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12219   if( rc ){
12220     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12221                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12222                     zQuery);
12223     goto end_schema_xfer;
12224   }
12225   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12226     zName = sqlite3_column_text(pQuery, 0);
12227     zSql = sqlite3_column_text(pQuery, 1);
12228     printf("%s... ", zName); fflush(stdout);
12229     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12230     if( zErrMsg ){
12231       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12232       sqlite3_free(zErrMsg);
12233       zErrMsg = 0;
12234     }
12235     if( xForEach ){
12236       xForEach(p, newDb, (const char*)zName);
12237     }
12238     printf("done\n");
12239   }
12240   if( rc!=SQLITE_DONE ){
12241     sqlite3_finalize(pQuery);
12242     sqlite3_free(zQuery);
12243     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12244                              " WHERE %s ORDER BY rowid DESC", zWhere);
12245     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12246     if( rc ){
12247       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12248                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12249                       zQuery);
12250       goto end_schema_xfer;
12251     }
12252     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12253       zName = sqlite3_column_text(pQuery, 0);
12254       zSql = sqlite3_column_text(pQuery, 1);
12255       printf("%s... ", zName); fflush(stdout);
12256       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12257       if( zErrMsg ){
12258         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12259         sqlite3_free(zErrMsg);
12260         zErrMsg = 0;
12261       }
12262       if( xForEach ){
12263         xForEach(p, newDb, (const char*)zName);
12264       }
12265       printf("done\n");
12266     }
12267   }
12268 end_schema_xfer:
12269   sqlite3_finalize(pQuery);
12270   sqlite3_free(zQuery);
12271 }
12272
12273 /*
12274 ** Open a new database file named "zNewDb".  Try to recover as much information
12275 ** as possible out of the main database (which might be corrupt) and write it
12276 ** into zNewDb.
12277 */
12278 static void tryToClone(ShellState *p, const char *zNewDb){
12279   int rc;
12280   sqlite3 *newDb = 0;
12281   if( access(zNewDb,0)==0 ){
12282     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
12283     return;
12284   }
12285   rc = sqlite3_open(zNewDb, &newDb);
12286   if( rc ){
12287     utf8_printf(stderr, "Cannot create output database: %s\n",
12288             sqlite3_errmsg(newDb));
12289   }else{
12290     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
12291     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
12292     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
12293     tryToCloneSchema(p, newDb, "type!='table'", 0);
12294     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
12295     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
12296   }
12297   close_db(newDb);
12298 }
12299
12300 /*
12301 ** Change the output file back to stdout.
12302 **
12303 ** If the p->doXdgOpen flag is set, that means the output was being
12304 ** redirected to a temporary file named by p->zTempFile.  In that case,
12305 ** launch start/open/xdg-open on that temporary file.
12306 */
12307 static void output_reset(ShellState *p){
12308   if( p->outfile[0]=='|' ){
12309 #ifndef SQLITE_OMIT_POPEN
12310     pclose(p->out);
12311 #endif
12312   }else{
12313     output_file_close(p->out);
12314 #ifndef SQLITE_NOHAVE_SYSTEM
12315     if( p->doXdgOpen ){
12316       const char *zXdgOpenCmd =
12317 #if defined(_WIN32)
12318       "start";
12319 #elif defined(__APPLE__)
12320       "open";
12321 #else
12322       "xdg-open";
12323 #endif
12324       char *zCmd;
12325       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
12326       if( system(zCmd) ){
12327         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
12328       }
12329       sqlite3_free(zCmd);
12330       outputModePop(p);
12331       p->doXdgOpen = 0;
12332     }
12333 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
12334   }
12335   p->outfile[0] = 0;
12336   p->out = stdout;
12337 }
12338
12339 /*
12340 ** Run an SQL command and return the single integer result.
12341 */
12342 static int db_int(ShellState *p, const char *zSql){
12343   sqlite3_stmt *pStmt;
12344   int res = 0;
12345   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12346   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
12347     res = sqlite3_column_int(pStmt,0);
12348   }
12349   sqlite3_finalize(pStmt);
12350   return res;
12351 }
12352
12353 /*
12354 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
12355 */
12356 static unsigned int get2byteInt(unsigned char *a){
12357   return (a[0]<<8) + a[1];
12358 }
12359 static unsigned int get4byteInt(unsigned char *a){
12360   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
12361 }
12362
12363 /*
12364 ** Implementation of the ".info" command.
12365 **
12366 ** Return 1 on error, 2 to exit, and 0 otherwise.
12367 */
12368 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
12369   static const struct { const char *zName; int ofst; } aField[] = {
12370      { "file change counter:",  24  },
12371      { "database page count:",  28  },
12372      { "freelist page count:",  36  },
12373      { "schema cookie:",        40  },
12374      { "schema format:",        44  },
12375      { "default cache size:",   48  },
12376      { "autovacuum top root:",  52  },
12377      { "incremental vacuum:",   64  },
12378      { "text encoding:",        56  },
12379      { "user version:",         60  },
12380      { "application id:",       68  },
12381      { "software version:",     96  },
12382   };
12383   static const struct { const char *zName; const char *zSql; } aQuery[] = {
12384      { "number of tables:",
12385        "SELECT count(*) FROM %s WHERE type='table'" },
12386      { "number of indexes:",
12387        "SELECT count(*) FROM %s WHERE type='index'" },
12388      { "number of triggers:",
12389        "SELECT count(*) FROM %s WHERE type='trigger'" },
12390      { "number of views:",
12391        "SELECT count(*) FROM %s WHERE type='view'" },
12392      { "schema size:",
12393        "SELECT total(length(sql)) FROM %s" },
12394   };
12395   int i;
12396   unsigned iDataVersion;
12397   char *zSchemaTab;
12398   char *zDb = nArg>=2 ? azArg[1] : "main";
12399   sqlite3_stmt *pStmt = 0;
12400   unsigned char aHdr[100];
12401   open_db(p, 0);
12402   if( p->db==0 ) return 1;
12403   sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12404                      -1, &pStmt, 0);
12405   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
12406   if( sqlite3_step(pStmt)==SQLITE_ROW
12407    && sqlite3_column_bytes(pStmt,0)>100
12408   ){
12409     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
12410     sqlite3_finalize(pStmt);
12411   }else{
12412     raw_printf(stderr, "unable to read database header\n");
12413     sqlite3_finalize(pStmt);
12414     return 1;
12415   }
12416   i = get2byteInt(aHdr+16);
12417   if( i==1 ) i = 65536;
12418   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
12419   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
12420   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
12421   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
12422   for(i=0; i<ArraySize(aField); i++){
12423     int ofst = aField[i].ofst;
12424     unsigned int val = get4byteInt(aHdr + ofst);
12425     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
12426     switch( ofst ){
12427       case 56: {
12428         if( val==1 ) raw_printf(p->out, " (utf8)");
12429         if( val==2 ) raw_printf(p->out, " (utf16le)");
12430         if( val==3 ) raw_printf(p->out, " (utf16be)");
12431       }
12432     }
12433     raw_printf(p->out, "\n");
12434   }
12435   if( zDb==0 ){
12436     zSchemaTab = sqlite3_mprintf("main.sqlite_master");
12437   }else if( strcmp(zDb,"temp")==0 ){
12438     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
12439   }else{
12440     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
12441   }
12442   for(i=0; i<ArraySize(aQuery); i++){
12443     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
12444     int val = db_int(p, zSql);
12445     sqlite3_free(zSql);
12446     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
12447   }
12448   sqlite3_free(zSchemaTab);
12449   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
12450   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
12451   return 0;
12452 }
12453
12454 /*
12455 ** Print the current sqlite3_errmsg() value to stderr and return 1.
12456 */
12457 static int shellDatabaseError(sqlite3 *db){
12458   const char *zErr = sqlite3_errmsg(db);
12459   utf8_printf(stderr, "Error: %s\n", zErr);
12460   return 1;
12461 }
12462
12463 /*
12464 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
12465 ** if they match and FALSE (0) if they do not match.
12466 **
12467 ** Globbing rules:
12468 **
12469 **      '*'       Matches any sequence of zero or more characters.
12470 **
12471 **      '?'       Matches exactly one character.
12472 **
12473 **     [...]      Matches one character from the enclosed list of
12474 **                characters.
12475 **
12476 **     [^...]     Matches one character not in the enclosed list.
12477 **
12478 **      '#'       Matches any sequence of one or more digits with an
12479 **                optional + or - sign in front
12480 **
12481 **      ' '       Any span of whitespace matches any other span of
12482 **                whitespace.
12483 **
12484 ** Extra whitespace at the end of z[] is ignored.
12485 */
12486 static int testcase_glob(const char *zGlob, const char *z){
12487   int c, c2;
12488   int invert;
12489   int seen;
12490
12491   while( (c = (*(zGlob++)))!=0 ){
12492     if( IsSpace(c) ){
12493       if( !IsSpace(*z) ) return 0;
12494       while( IsSpace(*zGlob) ) zGlob++;
12495       while( IsSpace(*z) ) z++;
12496     }else if( c=='*' ){
12497       while( (c=(*(zGlob++))) == '*' || c=='?' ){
12498         if( c=='?' && (*(z++))==0 ) return 0;
12499       }
12500       if( c==0 ){
12501         return 1;
12502       }else if( c=='[' ){
12503         while( *z && testcase_glob(zGlob-1,z)==0 ){
12504           z++;
12505         }
12506         return (*z)!=0;
12507       }
12508       while( (c2 = (*(z++)))!=0 ){
12509         while( c2!=c ){
12510           c2 = *(z++);
12511           if( c2==0 ) return 0;
12512         }
12513         if( testcase_glob(zGlob,z) ) return 1;
12514       }
12515       return 0;
12516     }else if( c=='?' ){
12517       if( (*(z++))==0 ) return 0;
12518     }else if( c=='[' ){
12519       int prior_c = 0;
12520       seen = 0;
12521       invert = 0;
12522       c = *(z++);
12523       if( c==0 ) return 0;
12524       c2 = *(zGlob++);
12525       if( c2=='^' ){
12526         invert = 1;
12527         c2 = *(zGlob++);
12528       }
12529       if( c2==']' ){
12530         if( c==']' ) seen = 1;
12531         c2 = *(zGlob++);
12532       }
12533       while( c2 && c2!=']' ){
12534         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
12535           c2 = *(zGlob++);
12536           if( c>=prior_c && c<=c2 ) seen = 1;
12537           prior_c = 0;
12538         }else{
12539           if( c==c2 ){
12540             seen = 1;
12541           }
12542           prior_c = c2;
12543         }
12544         c2 = *(zGlob++);
12545       }
12546       if( c2==0 || (seen ^ invert)==0 ) return 0;
12547     }else if( c=='#' ){
12548       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
12549       if( !IsDigit(z[0]) ) return 0;
12550       z++;
12551       while( IsDigit(z[0]) ){ z++; }
12552     }else{
12553       if( c!=(*(z++)) ) return 0;
12554     }
12555   }
12556   while( IsSpace(*z) ){ z++; }
12557   return *z==0;
12558 }
12559
12560
12561 /*
12562 ** Compare the string as a command-line option with either one or two
12563 ** initial "-" characters.
12564 */
12565 static int optionMatch(const char *zStr, const char *zOpt){
12566   if( zStr[0]!='-' ) return 0;
12567   zStr++;
12568   if( zStr[0]=='-' ) zStr++;
12569   return strcmp(zStr, zOpt)==0;
12570 }
12571
12572 /*
12573 ** Delete a file.
12574 */
12575 int shellDeleteFile(const char *zFilename){
12576   int rc;
12577 #ifdef _WIN32
12578   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
12579   rc = _wunlink(z);
12580   sqlite3_free(z);
12581 #else
12582   rc = unlink(zFilename);
12583 #endif
12584   return rc;
12585 }
12586
12587 /*
12588 ** Try to delete the temporary file (if there is one) and free the
12589 ** memory used to hold the name of the temp file.
12590 */
12591 static void clearTempFile(ShellState *p){
12592   if( p->zTempFile==0 ) return;
12593   if( p->doXdgOpen ) return;
12594   if( shellDeleteFile(p->zTempFile) ) return;
12595   sqlite3_free(p->zTempFile);
12596   p->zTempFile = 0;
12597 }
12598
12599 /*
12600 ** Create a new temp file name with the given suffix.
12601 */
12602 static void newTempFile(ShellState *p, const char *zSuffix){
12603   clearTempFile(p);
12604   sqlite3_free(p->zTempFile);
12605   p->zTempFile = 0;
12606   if( p->db ){
12607     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
12608   }
12609   if( p->zTempFile==0 ){
12610     sqlite3_uint64 r;
12611     sqlite3_randomness(sizeof(r), &r);
12612     p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
12613   }else{
12614     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
12615   }
12616   if( p->zTempFile==0 ){
12617     raw_printf(stderr, "out of memory\n");
12618     exit(1);
12619   }
12620 }
12621
12622
12623 /*
12624 ** The implementation of SQL scalar function fkey_collate_clause(), used
12625 ** by the ".lint fkey-indexes" command. This scalar function is always
12626 ** called with four arguments - the parent table name, the parent column name,
12627 ** the child table name and the child column name.
12628 **
12629 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
12630 **
12631 ** If either of the named tables or columns do not exist, this function
12632 ** returns an empty string. An empty string is also returned if both tables
12633 ** and columns exist but have the same default collation sequence. Or,
12634 ** if both exist but the default collation sequences are different, this
12635 ** function returns the string " COLLATE <parent-collation>", where
12636 ** <parent-collation> is the default collation sequence of the parent column.
12637 */
12638 static void shellFkeyCollateClause(
12639   sqlite3_context *pCtx,
12640   int nVal,
12641   sqlite3_value **apVal
12642 ){
12643   sqlite3 *db = sqlite3_context_db_handle(pCtx);
12644   const char *zParent;
12645   const char *zParentCol;
12646   const char *zParentSeq;
12647   const char *zChild;
12648   const char *zChildCol;
12649   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
12650   int rc;
12651
12652   assert( nVal==4 );
12653   zParent = (const char*)sqlite3_value_text(apVal[0]);
12654   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
12655   zChild = (const char*)sqlite3_value_text(apVal[2]);
12656   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
12657
12658   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
12659   rc = sqlite3_table_column_metadata(
12660       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
12661   );
12662   if( rc==SQLITE_OK ){
12663     rc = sqlite3_table_column_metadata(
12664         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
12665     );
12666   }
12667
12668   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
12669     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
12670     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
12671     sqlite3_free(z);
12672   }
12673 }
12674
12675
12676 /*
12677 ** The implementation of dot-command ".lint fkey-indexes".
12678 */
12679 static int lintFkeyIndexes(
12680   ShellState *pState,             /* Current shell tool state */
12681   char **azArg,                   /* Array of arguments passed to dot command */
12682   int nArg                        /* Number of entries in azArg[] */
12683 ){
12684   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
12685   FILE *out = pState->out;        /* Stream to write non-error output to */
12686   int bVerbose = 0;               /* If -verbose is present */
12687   int bGroupByParent = 0;         /* If -groupbyparent is present */
12688   int i;                          /* To iterate through azArg[] */
12689   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
12690   int rc;                         /* Return code */
12691   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
12692
12693   /*
12694   ** This SELECT statement returns one row for each foreign key constraint
12695   ** in the schema of the main database. The column values are:
12696   **
12697   ** 0. The text of an SQL statement similar to:
12698   **
12699   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
12700   **
12701   **    This SELECT is similar to the one that the foreign keys implementation
12702   **    needs to run internally on child tables. If there is an index that can
12703   **    be used to optimize this query, then it can also be used by the FK
12704   **    implementation to optimize DELETE or UPDATE statements on the parent
12705   **    table.
12706   **
12707   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
12708   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
12709   **    contains an index that can be used to optimize the query.
12710   **
12711   ** 2. Human readable text that describes the child table and columns. e.g.
12712   **
12713   **       "child_table(child_key1, child_key2)"
12714   **
12715   ** 3. Human readable text that describes the parent table and columns. e.g.
12716   **
12717   **       "parent_table(parent_key1, parent_key2)"
12718   **
12719   ** 4. A full CREATE INDEX statement for an index that could be used to
12720   **    optimize DELETE or UPDATE statements on the parent table. e.g.
12721   **
12722   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
12723   **
12724   ** 5. The name of the parent table.
12725   **
12726   ** These six values are used by the C logic below to generate the report.
12727   */
12728   const char *zSql =
12729   "SELECT "
12730     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
12731     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
12732     "  || fkey_collate_clause("
12733     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
12734     ", "
12735     "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
12736     "  || group_concat('*=?', ' AND ') || ')'"
12737     ", "
12738     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
12739     ", "
12740     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
12741     ", "
12742     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
12743     "  || ' ON ' || quote(s.name) || '('"
12744     "  || group_concat(quote(f.[from]) ||"
12745     "        fkey_collate_clause("
12746     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
12747     "  || ');'"
12748     ", "
12749     "     f.[table] "
12750     "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
12751     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
12752     "GROUP BY s.name, f.id "
12753     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
12754   ;
12755   const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
12756
12757   for(i=2; i<nArg; i++){
12758     int n = strlen30(azArg[i]);
12759     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
12760       bVerbose = 1;
12761     }
12762     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
12763       bGroupByParent = 1;
12764       zIndent = "    ";
12765     }
12766     else{
12767       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
12768           azArg[0], azArg[1]
12769       );
12770       return SQLITE_ERROR;
12771     }
12772   }
12773
12774   /* Register the fkey_collate_clause() SQL function */
12775   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
12776       0, shellFkeyCollateClause, 0, 0
12777   );
12778
12779
12780   if( rc==SQLITE_OK ){
12781     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
12782   }
12783   if( rc==SQLITE_OK ){
12784     sqlite3_bind_int(pSql, 1, bGroupByParent);
12785   }
12786
12787   if( rc==SQLITE_OK ){
12788     int rc2;
12789     char *zPrev = 0;
12790     while( SQLITE_ROW==sqlite3_step(pSql) ){
12791       int res = -1;
12792       sqlite3_stmt *pExplain = 0;
12793       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
12794       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
12795       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
12796       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
12797       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
12798       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
12799
12800       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
12801       if( rc!=SQLITE_OK ) break;
12802       if( SQLITE_ROW==sqlite3_step(pExplain) ){
12803         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
12804         res = (
12805               0==sqlite3_strglob(zGlob, zPlan)
12806            || 0==sqlite3_strglob(zGlobIPK, zPlan)
12807         );
12808       }
12809       rc = sqlite3_finalize(pExplain);
12810       if( rc!=SQLITE_OK ) break;
12811
12812       if( res<0 ){
12813         raw_printf(stderr, "Error: internal error");
12814         break;
12815       }else{
12816         if( bGroupByParent
12817         && (bVerbose || res==0)
12818         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
12819         ){
12820           raw_printf(out, "-- Parent table %s\n", zParent);
12821           sqlite3_free(zPrev);
12822           zPrev = sqlite3_mprintf("%s", zParent);
12823         }
12824
12825         if( res==0 ){
12826           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
12827         }else if( bVerbose ){
12828           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
12829               zIndent, zFrom, zTarget
12830           );
12831         }
12832       }
12833     }
12834     sqlite3_free(zPrev);
12835
12836     if( rc!=SQLITE_OK ){
12837       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12838     }
12839
12840     rc2 = sqlite3_finalize(pSql);
12841     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
12842       rc = rc2;
12843       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12844     }
12845   }else{
12846     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12847   }
12848
12849   return rc;
12850 }
12851
12852 /*
12853 ** Implementation of ".lint" dot command.
12854 */
12855 static int lintDotCommand(
12856   ShellState *pState,             /* Current shell tool state */
12857   char **azArg,                   /* Array of arguments passed to dot command */
12858   int nArg                        /* Number of entries in azArg[] */
12859 ){
12860   int n;
12861   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
12862   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
12863   return lintFkeyIndexes(pState, azArg, nArg);
12864
12865  usage:
12866   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
12867   raw_printf(stderr, "Where sub-commands are:\n");
12868   raw_printf(stderr, "    fkey-indexes\n");
12869   return SQLITE_ERROR;
12870 }
12871
12872 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12873 /*********************************************************************************
12874 ** The ".archive" or ".ar" command.
12875 */
12876 static void shellPrepare(
12877   sqlite3 *db, 
12878   int *pRc, 
12879   const char *zSql, 
12880   sqlite3_stmt **ppStmt
12881 ){
12882   *ppStmt = 0;
12883   if( *pRc==SQLITE_OK ){
12884     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12885     if( rc!=SQLITE_OK ){
12886       raw_printf(stderr, "sql error: %s (%d)\n", 
12887           sqlite3_errmsg(db), sqlite3_errcode(db)
12888       );
12889       *pRc = rc;
12890     }
12891   }
12892 }
12893
12894 static void shellPreparePrintf(
12895   sqlite3 *db, 
12896   int *pRc, 
12897   sqlite3_stmt **ppStmt,
12898   const char *zFmt, 
12899   ...
12900 ){
12901   *ppStmt = 0;
12902   if( *pRc==SQLITE_OK ){
12903     va_list ap;
12904     char *z;
12905     va_start(ap, zFmt);
12906     z = sqlite3_vmprintf(zFmt, ap);
12907     va_end(ap);
12908     if( z==0 ){
12909       *pRc = SQLITE_NOMEM;
12910     }else{
12911       shellPrepare(db, pRc, z, ppStmt);
12912       sqlite3_free(z);
12913     }
12914   }
12915 }
12916
12917 static void shellFinalize(
12918   int *pRc, 
12919   sqlite3_stmt *pStmt
12920 ){
12921   if( pStmt ){
12922     sqlite3 *db = sqlite3_db_handle(pStmt);
12923     int rc = sqlite3_finalize(pStmt);
12924     if( *pRc==SQLITE_OK ){
12925       if( rc!=SQLITE_OK ){
12926         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12927       }
12928       *pRc = rc;
12929     }
12930   }
12931 }
12932
12933 static void shellReset(
12934   int *pRc, 
12935   sqlite3_stmt *pStmt
12936 ){
12937   int rc = sqlite3_reset(pStmt);
12938   if( *pRc==SQLITE_OK ){
12939     if( rc!=SQLITE_OK ){
12940       sqlite3 *db = sqlite3_db_handle(pStmt);
12941       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12942     }
12943     *pRc = rc;
12944   }
12945 }
12946 /*
12947 ** Structure representing a single ".ar" command.
12948 */
12949 typedef struct ArCommand ArCommand;
12950 struct ArCommand {
12951   u8 eCmd;                        /* An AR_CMD_* value */
12952   u8 bVerbose;                    /* True if --verbose */
12953   u8 bZip;                        /* True if the archive is a ZIP */
12954   u8 bDryRun;                     /* True if --dry-run */
12955   u8 bAppend;                     /* True if --append */
12956   u8 fromCmdLine;                 /* Run from -A instead of .archive */
12957   int nArg;                       /* Number of command arguments */
12958   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
12959   const char *zFile;              /* --file argument, or NULL */
12960   const char *zDir;               /* --directory argument, or NULL */
12961   char **azArg;                   /* Array of command arguments */
12962   ShellState *p;                  /* Shell state */
12963   sqlite3 *db;                    /* Database containing the archive */
12964 };
12965
12966 /*
12967 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
12968 */
12969 static int arUsage(FILE *f){
12970   showHelp(f,"archive");
12971   return SQLITE_ERROR;
12972 }
12973
12974 /*
12975 ** Print an error message for the .ar command to stderr and return 
12976 ** SQLITE_ERROR.
12977 */
12978 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
12979   va_list ap;
12980   char *z;
12981   va_start(ap, zFmt);
12982   z = sqlite3_vmprintf(zFmt, ap);
12983   va_end(ap);
12984   utf8_printf(stderr, "Error: %s\n", z);
12985   if( pAr->fromCmdLine ){
12986     utf8_printf(stderr, "Use \"-A\" for more help\n");
12987   }else{
12988     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
12989   }
12990   sqlite3_free(z);
12991   return SQLITE_ERROR;
12992 }
12993
12994 /*
12995 ** Values for ArCommand.eCmd.
12996 */
12997 #define AR_CMD_CREATE       1
12998 #define AR_CMD_EXTRACT      2
12999 #define AR_CMD_LIST         3
13000 #define AR_CMD_UPDATE       4
13001 #define AR_CMD_HELP         5
13002
13003 /*
13004 ** Other (non-command) switches.
13005 */
13006 #define AR_SWITCH_VERBOSE     6
13007 #define AR_SWITCH_FILE        7
13008 #define AR_SWITCH_DIRECTORY   8
13009 #define AR_SWITCH_APPEND      9
13010 #define AR_SWITCH_DRYRUN     10
13011
13012 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
13013   switch( eSwitch ){
13014     case AR_CMD_CREATE:
13015     case AR_CMD_EXTRACT:
13016     case AR_CMD_LIST:
13017     case AR_CMD_UPDATE:
13018     case AR_CMD_HELP:
13019       if( pAr->eCmd ){
13020         return arErrorMsg(pAr, "multiple command options");
13021       }
13022       pAr->eCmd = eSwitch;
13023       break;
13024
13025     case AR_SWITCH_DRYRUN:
13026       pAr->bDryRun = 1;
13027       break;
13028     case AR_SWITCH_VERBOSE:
13029       pAr->bVerbose = 1;
13030       break;
13031     case AR_SWITCH_APPEND:
13032       pAr->bAppend = 1;
13033       /* Fall thru into --file */
13034     case AR_SWITCH_FILE:
13035       pAr->zFile = zArg;
13036       break;
13037     case AR_SWITCH_DIRECTORY:
13038       pAr->zDir = zArg;
13039       break;
13040   }
13041
13042   return SQLITE_OK;
13043 }
13044
13045 /*
13046 ** Parse the command line for an ".ar" command. The results are written into
13047 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
13048 ** successfully, otherwise an error message is written to stderr and 
13049 ** SQLITE_ERROR returned.
13050 */
13051 static int arParseCommand(
13052   char **azArg,                   /* Array of arguments passed to dot command */
13053   int nArg,                       /* Number of entries in azArg[] */
13054   ArCommand *pAr                  /* Populate this object */
13055 ){
13056   struct ArSwitch {
13057     const char *zLong;
13058     char cShort;
13059     u8 eSwitch;
13060     u8 bArg;
13061   } aSwitch[] = {
13062     { "create",    'c', AR_CMD_CREATE,       0 },
13063     { "extract",   'x', AR_CMD_EXTRACT,      0 },
13064     { "list",      't', AR_CMD_LIST,         0 },
13065     { "update",    'u', AR_CMD_UPDATE,       0 },
13066     { "help",      'h', AR_CMD_HELP,         0 },
13067     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
13068     { "file",      'f', AR_SWITCH_FILE,      1 },
13069     { "append",    'a', AR_SWITCH_APPEND,    1 },
13070     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
13071     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
13072   };
13073   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
13074   struct ArSwitch *pEnd = &aSwitch[nSwitch];
13075
13076   if( nArg<=1 ){
13077     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
13078     return arUsage(stderr);
13079   }else{
13080     char *z = azArg[1];
13081     if( z[0]!='-' ){
13082       /* Traditional style [tar] invocation */
13083       int i;
13084       int iArg = 2;
13085       for(i=0; z[i]; i++){
13086         const char *zArg = 0;
13087         struct ArSwitch *pOpt;
13088         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13089           if( z[i]==pOpt->cShort ) break;
13090         }
13091         if( pOpt==pEnd ){
13092           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13093         }
13094         if( pOpt->bArg ){
13095           if( iArg>=nArg ){
13096             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13097           }
13098           zArg = azArg[iArg++];
13099         }
13100         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13101       }
13102       pAr->nArg = nArg-iArg;
13103       if( pAr->nArg>0 ){
13104         pAr->azArg = &azArg[iArg];
13105       }
13106     }else{
13107       /* Non-traditional invocation */
13108       int iArg;
13109       for(iArg=1; iArg<nArg; iArg++){
13110         int n;
13111         z = azArg[iArg];
13112         if( z[0]!='-' ){
13113           /* All remaining command line words are command arguments. */
13114           pAr->azArg = &azArg[iArg];
13115           pAr->nArg = nArg-iArg;
13116           break;
13117         }
13118         n = strlen30(z);
13119
13120         if( z[1]!='-' ){
13121           int i;
13122           /* One or more short options */
13123           for(i=1; i<n; i++){
13124             const char *zArg = 0;
13125             struct ArSwitch *pOpt;
13126             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13127               if( z[i]==pOpt->cShort ) break;
13128             }
13129             if( pOpt==pEnd ){
13130               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13131             }
13132             if( pOpt->bArg ){
13133               if( i<(n-1) ){
13134                 zArg = &z[i+1];
13135                 i = n;
13136               }else{
13137                 if( iArg>=(nArg-1) ){
13138                   return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13139                 }
13140                 zArg = azArg[++iArg];
13141               }
13142             }
13143             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13144           }
13145         }else if( z[2]=='\0' ){
13146           /* A -- option, indicating that all remaining command line words
13147           ** are command arguments.  */
13148           pAr->azArg = &azArg[iArg+1];
13149           pAr->nArg = nArg-iArg-1;
13150           break;
13151         }else{
13152           /* A long option */
13153           const char *zArg = 0;             /* Argument for option, if any */
13154           struct ArSwitch *pMatch = 0;      /* Matching option */
13155           struct ArSwitch *pOpt;            /* Iterator */
13156           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13157             const char *zLong = pOpt->zLong;
13158             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
13159               if( pMatch ){
13160                 return arErrorMsg(pAr, "ambiguous option: %s",z);
13161               }else{
13162                 pMatch = pOpt;
13163               }
13164             }
13165           }
13166
13167           if( pMatch==0 ){
13168             return arErrorMsg(pAr, "unrecognized option: %s", z);
13169           }
13170           if( pMatch->bArg ){
13171             if( iArg>=(nArg-1) ){
13172               return arErrorMsg(pAr, "option requires an argument: %s", z);
13173             }
13174             zArg = azArg[++iArg];
13175           }
13176           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
13177         }
13178       }
13179     }
13180   }
13181
13182   return SQLITE_OK;
13183 }
13184
13185 /*
13186 ** This function assumes that all arguments within the ArCommand.azArg[]
13187 ** array refer to archive members, as for the --extract or --list commands. 
13188 ** It checks that each of them are present. If any specified file is not
13189 ** present in the archive, an error is printed to stderr and an error
13190 ** code returned. Otherwise, if all specified arguments are present in
13191 ** the archive, SQLITE_OK is returned.
13192 **
13193 ** This function strips any trailing '/' characters from each argument.
13194 ** This is consistent with the way the [tar] command seems to work on
13195 ** Linux.
13196 */
13197 static int arCheckEntries(ArCommand *pAr){
13198   int rc = SQLITE_OK;
13199   if( pAr->nArg ){
13200     int i, j;
13201     sqlite3_stmt *pTest = 0;
13202
13203     shellPreparePrintf(pAr->db, &rc, &pTest,
13204         "SELECT name FROM %s WHERE name=$name", 
13205         pAr->zSrcTable
13206     );
13207     j = sqlite3_bind_parameter_index(pTest, "$name");
13208     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13209       char *z = pAr->azArg[i];
13210       int n = strlen30(z);
13211       int bOk = 0;
13212       while( n>0 && z[n-1]=='/' ) n--;
13213       z[n] = '\0';
13214       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
13215       if( SQLITE_ROW==sqlite3_step(pTest) ){
13216         bOk = 1;
13217       }
13218       shellReset(&rc, pTest);
13219       if( rc==SQLITE_OK && bOk==0 ){
13220         utf8_printf(stderr, "not found in archive: %s\n", z);
13221         rc = SQLITE_ERROR;
13222       }
13223     }
13224     shellFinalize(&rc, pTest);
13225   }
13226   return rc;
13227 }
13228
13229 /*
13230 ** Format a WHERE clause that can be used against the "sqlar" table to
13231 ** identify all archive members that match the command arguments held
13232 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
13233 ** The caller is responsible for eventually calling sqlite3_free() on
13234 ** any non-NULL (*pzWhere) value.
13235 */
13236 static void arWhereClause(
13237   int *pRc, 
13238   ArCommand *pAr, 
13239   char **pzWhere                  /* OUT: New WHERE clause */
13240 ){
13241   char *zWhere = 0;
13242   if( *pRc==SQLITE_OK ){
13243     if( pAr->nArg==0 ){
13244       zWhere = sqlite3_mprintf("1");
13245     }else{
13246       int i;
13247       const char *zSep = "";
13248       for(i=0; i<pAr->nArg; i++){
13249         const char *z = pAr->azArg[i];
13250         zWhere = sqlite3_mprintf(
13251           "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", 
13252           zWhere, zSep, z, strlen30(z)+1, z
13253         );
13254         if( zWhere==0 ){
13255           *pRc = SQLITE_NOMEM;
13256           break;
13257         }
13258         zSep = " OR ";
13259       }
13260     }
13261   }
13262   *pzWhere = zWhere;
13263 }
13264
13265 /*
13266 ** Implementation of .ar "lisT" command. 
13267 */
13268 static int arListCommand(ArCommand *pAr){
13269   const char *zSql = "SELECT %s FROM %s WHERE %s"; 
13270   const char *azCols[] = {
13271     "name",
13272     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
13273   };
13274
13275   char *zWhere = 0;
13276   sqlite3_stmt *pSql = 0;
13277   int rc;
13278
13279   rc = arCheckEntries(pAr);
13280   arWhereClause(&rc, pAr, &zWhere);
13281
13282   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
13283                      pAr->zSrcTable, zWhere);
13284   if( pAr->bDryRun ){
13285     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13286   }else{
13287     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13288       if( pAr->bVerbose ){
13289         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
13290             sqlite3_column_text(pSql, 0),
13291             sqlite3_column_int(pSql, 1), 
13292             sqlite3_column_text(pSql, 2),
13293             sqlite3_column_text(pSql, 3)
13294         );
13295       }else{
13296         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13297       }
13298     }
13299   }
13300   shellFinalize(&rc, pSql);
13301   sqlite3_free(zWhere);
13302   return rc;
13303 }
13304
13305
13306 /*
13307 ** Implementation of .ar "eXtract" command. 
13308 */
13309 static int arExtractCommand(ArCommand *pAr){
13310   const char *zSql1 = 
13311     "SELECT "
13312     " ($dir || name),"
13313     " writefile(($dir || name), %s, mode, mtime) "
13314     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
13315     " AND name NOT GLOB '*..[/\\]*'";
13316
13317   const char *azExtraArg[] = { 
13318     "sqlar_uncompress(data, sz)",
13319     "data"
13320   };
13321
13322   sqlite3_stmt *pSql = 0;
13323   int rc = SQLITE_OK;
13324   char *zDir = 0;
13325   char *zWhere = 0;
13326   int i, j;
13327
13328   /* If arguments are specified, check that they actually exist within
13329   ** the archive before proceeding. And formulate a WHERE clause to
13330   ** match them.  */
13331   rc = arCheckEntries(pAr);
13332   arWhereClause(&rc, pAr, &zWhere);
13333
13334   if( rc==SQLITE_OK ){
13335     if( pAr->zDir ){
13336       zDir = sqlite3_mprintf("%s/", pAr->zDir);
13337     }else{
13338       zDir = sqlite3_mprintf("");
13339     }
13340     if( zDir==0 ) rc = SQLITE_NOMEM;
13341   }
13342
13343   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 
13344       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
13345   );
13346
13347   if( rc==SQLITE_OK ){
13348     j = sqlite3_bind_parameter_index(pSql, "$dir");
13349     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
13350
13351     /* Run the SELECT statement twice. The first time, writefile() is called
13352     ** for all archive members that should be extracted. The second time,
13353     ** only for the directories. This is because the timestamps for
13354     ** extracted directories must be reset after they are populated (as
13355     ** populating them changes the timestamp).  */
13356     for(i=0; i<2; i++){
13357       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
13358       sqlite3_bind_int(pSql, j, i);
13359       if( pAr->bDryRun ){
13360         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13361       }else{
13362         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13363           if( i==0 && pAr->bVerbose ){
13364             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13365           }
13366         }
13367       }
13368       shellReset(&rc, pSql);
13369     }
13370     shellFinalize(&rc, pSql);
13371   }
13372
13373   sqlite3_free(zDir);
13374   sqlite3_free(zWhere);
13375   return rc;
13376 }
13377
13378 /*
13379 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
13380 */
13381 static int arExecSql(ArCommand *pAr, const char *zSql){
13382   int rc;
13383   if( pAr->bDryRun ){
13384     utf8_printf(pAr->p->out, "%s\n", zSql);
13385     rc = SQLITE_OK;
13386   }else{
13387     char *zErr = 0;
13388     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
13389     if( zErr ){
13390       utf8_printf(stdout, "ERROR: %s\n", zErr);
13391       sqlite3_free(zErr);
13392     }
13393   }
13394   return rc;
13395 }
13396
13397
13398 /*
13399 ** Implementation of .ar "create" and "update" commands.
13400 **
13401 ** Create the "sqlar" table in the database if it does not already exist.
13402 ** Then add each file in the azFile[] array to the archive. Directories
13403 ** are added recursively. If argument bVerbose is non-zero, a message is
13404 ** printed on stdout for each file archived.
13405 **
13406 ** The create command is the same as update, except that it drops
13407 ** any existing "sqlar" table before beginning.
13408 */
13409 static int arCreateOrUpdateCommand(
13410   ArCommand *pAr,                 /* Command arguments and options */
13411   int bUpdate                     /* true for a --create.  false for --update */
13412 ){
13413   const char *zCreate = 
13414       "CREATE TABLE IF NOT EXISTS sqlar(\n"
13415       "  name TEXT PRIMARY KEY,  -- name of the file\n"
13416       "  mode INT,               -- access permissions\n"
13417       "  mtime INT,              -- last modification time\n"
13418       "  sz INT,                 -- original file size\n"
13419       "  data BLOB               -- compressed content\n"
13420       ")";
13421   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
13422   const char *zInsertFmt[2] = {
13423      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
13424      "  SELECT\n"
13425      "    %s,\n"
13426      "    mode,\n"
13427      "    mtime,\n"
13428      "    CASE substr(lsmode(mode),1,1)\n"
13429      "      WHEN '-' THEN length(data)\n"
13430      "      WHEN 'd' THEN 0\n"
13431      "      ELSE -1 END,\n"
13432      "    sqlar_compress(data)\n"
13433      "  FROM fsdir(%Q,%Q)\n"
13434      "  WHERE lsmode(mode) NOT LIKE '?%%';",
13435      "REPLACE INTO %s(name,mode,mtime,data)\n"
13436      "  SELECT\n"
13437      "    %s,\n"
13438      "    mode,\n"
13439      "    mtime,\n"
13440      "    data\n"
13441      "  FROM fsdir(%Q,%Q)\n"
13442      "  WHERE lsmode(mode) NOT LIKE '?%%';"
13443   };
13444   int i;                          /* For iterating through azFile[] */
13445   int rc;                         /* Return code */
13446   const char *zTab = 0;           /* SQL table into which to insert */
13447   char *zSql;
13448   char zTemp[50];
13449
13450   arExecSql(pAr, "PRAGMA page_size=512");
13451   rc = arExecSql(pAr, "SAVEPOINT ar;");
13452   if( rc!=SQLITE_OK ) return rc;
13453   zTemp[0] = 0; 
13454   if( pAr->bZip ){
13455     /* Initialize the zipfile virtual table, if necessary */
13456     if( pAr->zFile ){
13457       sqlite3_uint64 r;
13458       sqlite3_randomness(sizeof(r),&r);
13459       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
13460       zTab = zTemp;
13461       zSql = sqlite3_mprintf(
13462          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
13463          zTab, pAr->zFile
13464       );
13465       rc = arExecSql(pAr, zSql);
13466       sqlite3_free(zSql);
13467     }else{
13468       zTab = "zip";
13469     }
13470   }else{
13471     /* Initialize the table for an SQLAR */
13472     zTab = "sqlar";
13473     if( bUpdate==0 ){
13474       rc = arExecSql(pAr, zDrop);
13475       if( rc!=SQLITE_OK ) goto end_ar_transaction;
13476     }
13477     rc = arExecSql(pAr, zCreate);
13478   }
13479   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13480     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
13481         pAr->bVerbose ? "shell_putsnl(name)" : "name",
13482         pAr->azArg[i], pAr->zDir);
13483     rc = arExecSql(pAr, zSql2);
13484     sqlite3_free(zSql2);
13485   }
13486 end_ar_transaction:
13487   if( rc!=SQLITE_OK ){
13488     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
13489   }else{
13490     rc = arExecSql(pAr, "RELEASE ar;");
13491     if( pAr->bZip && pAr->zFile ){
13492       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
13493       arExecSql(pAr, zSql);
13494       sqlite3_free(zSql);
13495     }
13496   }
13497   return rc;
13498 }
13499
13500 /*
13501 ** Implementation of ".ar" dot command.
13502 */
13503 static int arDotCommand(
13504   ShellState *pState,             /* Current shell tool state */
13505   int fromCmdLine,                /* True if -A command-line option, not .ar cmd */
13506   char **azArg,                   /* Array of arguments passed to dot command */
13507   int nArg                        /* Number of entries in azArg[] */
13508 ){
13509   ArCommand cmd;
13510   int rc;
13511   memset(&cmd, 0, sizeof(cmd));
13512   cmd.fromCmdLine = fromCmdLine;
13513   rc = arParseCommand(azArg, nArg, &cmd);
13514   if( rc==SQLITE_OK ){
13515     int eDbType = SHELL_OPEN_UNSPEC;
13516     cmd.p = pState;
13517     cmd.db = pState->db;
13518     if( cmd.zFile ){
13519       eDbType = deduceDatabaseType(cmd.zFile, 1);
13520     }else{
13521       eDbType = pState->openMode;
13522     }
13523     if( eDbType==SHELL_OPEN_ZIPFILE ){
13524       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
13525         if( cmd.zFile==0 ){
13526           cmd.zSrcTable = sqlite3_mprintf("zip");
13527         }else{
13528           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
13529         }
13530       }
13531       cmd.bZip = 1;
13532     }else if( cmd.zFile ){
13533       int flags;
13534       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13535       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
13536         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13537       }else{
13538         flags = SQLITE_OPEN_READONLY;
13539       }
13540       cmd.db = 0;
13541       if( cmd.bDryRun ){
13542         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
13543              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
13544       }
13545       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 
13546              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
13547       if( rc!=SQLITE_OK ){
13548         utf8_printf(stderr, "cannot open file: %s (%s)\n", 
13549             cmd.zFile, sqlite3_errmsg(cmd.db)
13550         );
13551         goto end_ar_command;
13552       }
13553       sqlite3_fileio_init(cmd.db, 0, 0);
13554       sqlite3_sqlar_init(cmd.db, 0, 0);
13555       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13556                               shellPutsFunc, 0, 0);
13557
13558     }
13559     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13560       if( cmd.eCmd!=AR_CMD_CREATE
13561        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13562       ){
13563         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13564         rc = SQLITE_ERROR;
13565         goto end_ar_command;
13566       }
13567       cmd.zSrcTable = sqlite3_mprintf("sqlar");
13568     }
13569
13570     switch( cmd.eCmd ){
13571       case AR_CMD_CREATE:
13572         rc = arCreateOrUpdateCommand(&cmd, 0);
13573         break;
13574
13575       case AR_CMD_EXTRACT:
13576         rc = arExtractCommand(&cmd);
13577         break;
13578
13579       case AR_CMD_LIST:
13580         rc = arListCommand(&cmd);
13581         break;
13582
13583       case AR_CMD_HELP:
13584         arUsage(pState->out);
13585         break;
13586
13587       default:
13588         assert( cmd.eCmd==AR_CMD_UPDATE );
13589         rc = arCreateOrUpdateCommand(&cmd, 1);
13590         break;
13591     }
13592   }
13593 end_ar_command:
13594   if( cmd.db!=pState->db ){
13595     close_db(cmd.db);
13596   }
13597   sqlite3_free(cmd.zSrcTable);
13598
13599   return rc;
13600 }
13601 /* End of the ".archive" or ".ar" command logic
13602 **********************************************************************************/
13603 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
13604
13605
13606 /*
13607 ** If an input line begins with "." then invoke this routine to
13608 ** process that line.
13609 **
13610 ** Return 1 on error, 2 to exit, and 0 otherwise.
13611 */
13612 static int do_meta_command(char *zLine, ShellState *p){
13613   int h = 1;
13614   int nArg = 0;
13615   int n, c;
13616   int rc = 0;
13617   char *azArg[50];
13618
13619 #ifndef SQLITE_OMIT_VIRTUALTABLE
13620   if( p->expert.pExpert ){
13621     expertFinish(p, 1, 0);
13622   }
13623 #endif
13624
13625   /* Parse the input line into tokens.
13626   */
13627   while( zLine[h] && nArg<ArraySize(azArg) ){
13628     while( IsSpace(zLine[h]) ){ h++; }
13629     if( zLine[h]==0 ) break;
13630     if( zLine[h]=='\'' || zLine[h]=='"' ){
13631       int delim = zLine[h++];
13632       azArg[nArg++] = &zLine[h];
13633       while( zLine[h] && zLine[h]!=delim ){
13634         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
13635         h++;
13636       }
13637       if( zLine[h]==delim ){
13638         zLine[h++] = 0;
13639       }
13640       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
13641     }else{
13642       azArg[nArg++] = &zLine[h];
13643       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
13644       if( zLine[h] ) zLine[h++] = 0;
13645       resolve_backslashes(azArg[nArg-1]);
13646     }
13647   }
13648
13649   /* Process the input line.
13650   */
13651   if( nArg==0 ) return 0; /* no tokens, no error */
13652   n = strlen30(azArg[0]);
13653   c = azArg[0][0];
13654   clearTempFile(p);
13655
13656 #ifndef SQLITE_OMIT_AUTHORIZATION
13657   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
13658     if( nArg!=2 ){
13659       raw_printf(stderr, "Usage: .auth ON|OFF\n");
13660       rc = 1;
13661       goto meta_command_exit;
13662     }
13663     open_db(p, 0);
13664     if( booleanValue(azArg[1]) ){
13665       sqlite3_set_authorizer(p->db, shellAuth, p);
13666     }else{
13667       sqlite3_set_authorizer(p->db, 0, 0);
13668     }
13669   }else
13670 #endif
13671
13672 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13673   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13674     open_db(p, 0);
13675     rc = arDotCommand(p, 0, azArg, nArg);
13676   }else
13677 #endif
13678
13679   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13680    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
13681   ){
13682     const char *zDestFile = 0;
13683     const char *zDb = 0;
13684     sqlite3 *pDest;
13685     sqlite3_backup *pBackup;
13686     int j;
13687     int bAsync = 0;
13688     const char *zVfs = 0;
13689     for(j=1; j<nArg; j++){
13690       const char *z = azArg[j];
13691       if( z[0]=='-' ){
13692         if( z[1]=='-' ) z++;
13693         if( strcmp(z, "-append")==0 ){
13694           zVfs = "apndvfs";
13695         }else
13696         if( strcmp(z, "-async")==0 ){
13697           bAsync = 1;
13698         }else
13699         {
13700           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
13701           return 1;
13702         }
13703       }else if( zDestFile==0 ){
13704         zDestFile = azArg[j];
13705       }else if( zDb==0 ){
13706         zDb = zDestFile;
13707         zDestFile = azArg[j];
13708       }else{
13709         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
13710         return 1;
13711       }
13712     }
13713     if( zDestFile==0 ){
13714       raw_printf(stderr, "missing FILENAME argument on .backup\n");
13715       return 1;
13716     }
13717     if( zDb==0 ) zDb = "main";
13718     rc = sqlite3_open_v2(zDestFile, &pDest, 
13719                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13720     if( rc!=SQLITE_OK ){
13721       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13722       close_db(pDest);
13723       return 1;
13724     }
13725     if( bAsync ){
13726       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
13727                    0, 0, 0);
13728     }
13729     open_db(p, 0);
13730     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13731     if( pBackup==0 ){
13732       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13733       close_db(pDest);
13734       return 1;
13735     }
13736     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13737     sqlite3_backup_finish(pBackup);
13738     if( rc==SQLITE_DONE ){
13739       rc = 0;
13740     }else{
13741       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13742       rc = 1;
13743     }
13744     close_db(pDest);
13745   }else
13746
13747   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13748     if( nArg==2 ){
13749       bail_on_error = booleanValue(azArg[1]);
13750     }else{
13751       raw_printf(stderr, "Usage: .bail on|off\n");
13752       rc = 1;
13753     }
13754   }else
13755
13756   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
13757     if( nArg==2 ){
13758       if( booleanValue(azArg[1]) ){
13759         setBinaryMode(p->out, 1);
13760       }else{
13761         setTextMode(p->out, 1);
13762       }
13763     }else{
13764       raw_printf(stderr, "Usage: .binary on|off\n");
13765       rc = 1;
13766     }
13767   }else
13768
13769   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
13770     if( nArg==2 ){
13771 #if defined(_WIN32) || defined(WIN32)
13772       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
13773       rc = !SetCurrentDirectoryW(z);
13774       sqlite3_free(z);
13775 #else
13776       rc = chdir(azArg[1]);
13777 #endif
13778       if( rc ){
13779         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
13780         rc = 1;
13781       }
13782     }else{
13783       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
13784       rc = 1;
13785     }
13786   }else
13787
13788   /* The undocumented ".breakpoint" command causes a call to the no-op
13789   ** routine named test_breakpoint().
13790   */
13791   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
13792     test_breakpoint();
13793   }else
13794
13795   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
13796     if( nArg==2 ){
13797       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
13798     }else{
13799       raw_printf(stderr, "Usage: .changes on|off\n");
13800       rc = 1;
13801     }
13802   }else
13803
13804   /* Cancel output redirection, if it is currently set (by .testcase)
13805   ** Then read the content of the testcase-out.txt file and compare against
13806   ** azArg[1].  If there are differences, report an error and exit.
13807   */
13808   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
13809     char *zRes = 0;
13810     output_reset(p);
13811     if( nArg!=2 ){
13812       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
13813       rc = 2;
13814     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
13815       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
13816       rc = 2;
13817     }else if( testcase_glob(azArg[1],zRes)==0 ){
13818       utf8_printf(stderr,
13819                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
13820                  p->zTestcase, azArg[1], zRes);
13821       rc = 1;
13822     }else{
13823       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
13824       p->nCheck++;
13825     }
13826     sqlite3_free(zRes);
13827   }else
13828
13829   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
13830     if( nArg==2 ){
13831       tryToClone(p, azArg[1]);
13832     }else{
13833       raw_printf(stderr, "Usage: .clone FILENAME\n");
13834       rc = 1;
13835     }
13836   }else
13837
13838   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
13839     ShellState data;
13840     char *zErrMsg = 0;
13841     open_db(p, 0);
13842     memcpy(&data, p, sizeof(data));
13843     data.showHeader = 0;
13844     data.cMode = data.mode = MODE_List;
13845     sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
13846     data.cnt = 0;
13847     sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
13848                  callback, &data, &zErrMsg);
13849     if( zErrMsg ){
13850       utf8_printf(stderr,"Error: %s\n", zErrMsg);
13851       sqlite3_free(zErrMsg);
13852       rc = 1;
13853     }
13854   }else
13855
13856   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13857     static const struct DbConfigChoices {
13858       const char *zName;
13859       int op;
13860     } aDbConfig[] = {
13861         { "enable_fkey",      SQLITE_DBCONFIG_ENABLE_FKEY            },
13862         { "enable_trigger",   SQLITE_DBCONFIG_ENABLE_TRIGGER         },
13863         { "fts3_tokenizer",   SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER  },
13864         { "load_extension",   SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION  },
13865         { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE       },
13866         { "enable_qpsg",      SQLITE_DBCONFIG_ENABLE_QPSG            },
13867         { "trigger_eqp",      SQLITE_DBCONFIG_TRIGGER_EQP            },
13868         { "reset_database",   SQLITE_DBCONFIG_RESET_DATABASE         },
13869         { "defensive",        SQLITE_DBCONFIG_DEFENSIVE              },
13870     };
13871     int ii, v;
13872     open_db(p, 0);
13873     for(ii=0; ii<ArraySize(aDbConfig); ii++){
13874       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13875       if( nArg>=3 ){
13876         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13877       }
13878       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13879       utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13880       if( nArg>1 ) break;
13881     }
13882     if( nArg>1 && ii==ArraySize(aDbConfig) ){
13883       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13884       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13885     }   
13886   }else
13887
13888   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13889     rc = shell_dbinfo_command(p, nArg, azArg);
13890   }else
13891
13892   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
13893     const char *zLike = 0;
13894     int i;
13895     int savedShowHeader = p->showHeader;
13896     int savedShellFlags = p->shellFlgs;
13897     ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
13898     for(i=1; i<nArg; i++){
13899       if( azArg[i][0]=='-' ){
13900         const char *z = azArg[i]+1;
13901         if( z[0]=='-' ) z++;
13902         if( strcmp(z,"preserve-rowids")==0 ){
13903 #ifdef SQLITE_OMIT_VIRTUALTABLE
13904           raw_printf(stderr, "The --preserve-rowids option is not compatible"
13905                              " with SQLITE_OMIT_VIRTUALTABLE\n");
13906           rc = 1;
13907           goto meta_command_exit;
13908 #else
13909           ShellSetFlag(p, SHFLG_PreserveRowid);
13910 #endif
13911         }else
13912         if( strcmp(z,"newlines")==0 ){
13913           ShellSetFlag(p, SHFLG_Newlines);
13914         }else
13915         {
13916           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
13917           rc = 1;
13918           goto meta_command_exit;
13919         }
13920       }else if( zLike ){
13921         raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
13922                            "?--newlines? ?LIKE-PATTERN?\n");
13923         rc = 1;
13924         goto meta_command_exit;
13925       }else{
13926         zLike = azArg[i];
13927       }
13928     }
13929     open_db(p, 0);
13930     /* When playing back a "dump", the content might appear in an order
13931     ** which causes immediate foreign key constraints to be violated.
13932     ** So disable foreign-key constraint enforcement to prevent problems. */
13933     raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
13934     raw_printf(p->out, "BEGIN TRANSACTION;\n");
13935     p->writableSchema = 0;
13936     p->showHeader = 0;
13937     /* Set writable_schema=ON since doing so forces SQLite to initialize
13938     ** as much of the schema as it can even if the sqlite_master table is
13939     ** corrupt. */
13940     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
13941     p->nErr = 0;
13942     if( zLike==0 ){
13943       run_schema_dump_query(p,
13944         "SELECT name, type, sql FROM sqlite_master "
13945         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
13946       );
13947       run_schema_dump_query(p,
13948         "SELECT name, type, sql FROM sqlite_master "
13949         "WHERE name=='sqlite_sequence'"
13950       );
13951       run_table_dump_query(p,
13952         "SELECT sql FROM sqlite_master "
13953         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
13954       );
13955     }else{
13956       char *zSql;
13957       zSql = sqlite3_mprintf(
13958         "SELECT name, type, sql FROM sqlite_master "
13959         "WHERE tbl_name LIKE %Q AND type=='table'"
13960         "  AND sql NOT NULL", zLike);
13961       run_schema_dump_query(p,zSql);
13962       sqlite3_free(zSql);
13963       zSql = sqlite3_mprintf(
13964         "SELECT sql FROM sqlite_master "
13965         "WHERE sql NOT NULL"
13966         "  AND type IN ('index','trigger','view')"
13967         "  AND tbl_name LIKE %Q", zLike);
13968       run_table_dump_query(p, zSql, 0);
13969       sqlite3_free(zSql);
13970     }
13971     if( p->writableSchema ){
13972       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
13973       p->writableSchema = 0;
13974     }
13975     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13976     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
13977     raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
13978     p->showHeader = savedShowHeader;
13979     p->shellFlgs = savedShellFlags;
13980   }else
13981
13982   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
13983     if( nArg==2 ){
13984       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
13985     }else{
13986       raw_printf(stderr, "Usage: .echo on|off\n");
13987       rc = 1;
13988     }
13989   }else
13990
13991   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
13992     if( nArg==2 ){
13993       p->autoEQPtest = 0;
13994       if( p->autoEQPtrace ){
13995         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
13996         p->autoEQPtrace = 0;
13997       }
13998       if( strcmp(azArg[1],"full")==0 ){
13999         p->autoEQP = AUTOEQP_full;
14000       }else if( strcmp(azArg[1],"trigger")==0 ){
14001         p->autoEQP = AUTOEQP_trigger;
14002 #ifdef SQLITE_DEBUG
14003       }else if( strcmp(azArg[1],"test")==0 ){
14004         p->autoEQP = AUTOEQP_on;
14005         p->autoEQPtest = 1;
14006       }else if( strcmp(azArg[1],"trace")==0 ){
14007         p->autoEQP = AUTOEQP_full;
14008         p->autoEQPtrace = 1;
14009         open_db(p, 0);
14010         sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
14011         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
14012 #endif
14013       }else{
14014         p->autoEQP = (u8)booleanValue(azArg[1]);
14015       }
14016     }else{
14017       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
14018       rc = 1;
14019     }
14020   }else
14021
14022   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
14023     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
14024     rc = 2;
14025   }else
14026
14027   /* The ".explain" command is automatic now.  It is largely pointless.  It
14028   ** retained purely for backwards compatibility */
14029   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
14030     int val = 1;
14031     if( nArg>=2 ){
14032       if( strcmp(azArg[1],"auto")==0 ){
14033         val = 99;
14034       }else{
14035         val =  booleanValue(azArg[1]);
14036       }
14037     }
14038     if( val==1 && p->mode!=MODE_Explain ){
14039       p->normalMode = p->mode;
14040       p->mode = MODE_Explain;
14041       p->autoExplain = 0;
14042     }else if( val==0 ){
14043       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14044       p->autoExplain = 0;
14045     }else if( val==99 ){
14046       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14047       p->autoExplain = 1;
14048     }
14049   }else
14050
14051 #ifndef SQLITE_OMIT_VIRTUALTABLE
14052   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
14053     open_db(p, 0);
14054     expertDotCommand(p, azArg, nArg);
14055   }else
14056 #endif
14057
14058   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
14059     ShellState data;
14060     char *zErrMsg = 0;
14061     int doStats = 0;
14062     memcpy(&data, p, sizeof(data));
14063     data.showHeader = 0;
14064     data.cMode = data.mode = MODE_Semi;
14065     if( nArg==2 && optionMatch(azArg[1], "indent") ){
14066       data.cMode = data.mode = MODE_Pretty;
14067       nArg = 1;
14068     }
14069     if( nArg!=1 ){
14070       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
14071       rc = 1;
14072       goto meta_command_exit;
14073     }
14074     open_db(p, 0);
14075     rc = sqlite3_exec(p->db,
14076        "SELECT sql FROM"
14077        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
14078        "     FROM sqlite_master UNION ALL"
14079        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
14080        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
14081        "ORDER BY rowid",
14082        callback, &data, &zErrMsg
14083     );
14084     if( rc==SQLITE_OK ){
14085       sqlite3_stmt *pStmt;
14086       rc = sqlite3_prepare_v2(p->db,
14087                "SELECT rowid FROM sqlite_master"
14088                " WHERE name GLOB 'sqlite_stat[134]'",
14089                -1, &pStmt, 0);
14090       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
14091       sqlite3_finalize(pStmt);
14092     }
14093     if( doStats==0 ){
14094       raw_printf(p->out, "/* No STAT tables available */\n");
14095     }else{
14096       raw_printf(p->out, "ANALYZE sqlite_master;\n");
14097       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
14098                    callback, &data, &zErrMsg);
14099       data.cMode = data.mode = MODE_Insert;
14100       data.zDestTable = "sqlite_stat1";
14101       shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
14102       data.zDestTable = "sqlite_stat3";
14103       shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
14104       data.zDestTable = "sqlite_stat4";
14105       shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
14106       raw_printf(p->out, "ANALYZE sqlite_master;\n");
14107     }
14108   }else
14109
14110   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
14111     if( nArg==2 ){
14112       p->showHeader = booleanValue(azArg[1]);
14113     }else{
14114       raw_printf(stderr, "Usage: .headers on|off\n");
14115       rc = 1;
14116     }
14117   }else
14118
14119   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
14120     if( nArg>=2 ){
14121       n = showHelp(p->out, azArg[1]);
14122       if( n==0 ){
14123         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
14124       }
14125     }else{
14126       showHelp(p->out, 0);
14127     }
14128   }else
14129
14130   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
14131     char *zTable;               /* Insert data into this table */
14132     char *zFile;                /* Name of file to extra content from */
14133     sqlite3_stmt *pStmt = NULL; /* A statement */
14134     int nCol;                   /* Number of columns in the table */
14135     int nByte;                  /* Number of bytes in an SQL string */
14136     int i, j;                   /* Loop counters */
14137     int needCommit;             /* True to COMMIT or ROLLBACK at end */
14138     int nSep;                   /* Number of bytes in p->colSeparator[] */
14139     char *zSql;                 /* An SQL statement */
14140     ImportCtx sCtx;             /* Reader context */
14141     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
14142     int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
14143
14144     if( nArg!=3 ){
14145       raw_printf(stderr, "Usage: .import FILE TABLE\n");
14146       goto meta_command_exit;
14147     }
14148     zFile = azArg[1];
14149     zTable = azArg[2];
14150     seenInterrupt = 0;
14151     memset(&sCtx, 0, sizeof(sCtx));
14152     open_db(p, 0);
14153     nSep = strlen30(p->colSeparator);
14154     if( nSep==0 ){
14155       raw_printf(stderr,
14156                  "Error: non-null column separator required for import\n");
14157       return 1;
14158     }
14159     if( nSep>1 ){
14160       raw_printf(stderr, "Error: multi-character column separators not allowed"
14161                       " for import\n");
14162       return 1;
14163     }
14164     nSep = strlen30(p->rowSeparator);
14165     if( nSep==0 ){
14166       raw_printf(stderr, "Error: non-null row separator required for import\n");
14167       return 1;
14168     }
14169     if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
14170       /* When importing CSV (only), if the row separator is set to the
14171       ** default output row separator, change it to the default input
14172       ** row separator.  This avoids having to maintain different input
14173       ** and output row separators. */
14174       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14175       nSep = strlen30(p->rowSeparator);
14176     }
14177     if( nSep>1 ){
14178       raw_printf(stderr, "Error: multi-character row separators not allowed"
14179                       " for import\n");
14180       return 1;
14181     }
14182     sCtx.zFile = zFile;
14183     sCtx.nLine = 1;
14184     if( sCtx.zFile[0]=='|' ){
14185 #ifdef SQLITE_OMIT_POPEN
14186       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14187       return 1;
14188 #else
14189       sCtx.in = popen(sCtx.zFile+1, "r");
14190       sCtx.zFile = "<pipe>";
14191       xCloser = pclose;
14192 #endif
14193     }else{
14194       sCtx.in = fopen(sCtx.zFile, "rb");
14195       xCloser = fclose;
14196     }
14197     if( p->mode==MODE_Ascii ){
14198       xRead = ascii_read_one_field;
14199     }else{
14200       xRead = csv_read_one_field;
14201     }
14202     if( sCtx.in==0 ){
14203       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
14204       return 1;
14205     }
14206     sCtx.cColSep = p->colSeparator[0];
14207     sCtx.cRowSep = p->rowSeparator[0];
14208     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
14209     if( zSql==0 ){
14210       xCloser(sCtx.in);
14211       shell_out_of_memory();
14212     }
14213     nByte = strlen30(zSql);
14214     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14215     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
14216     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
14217       char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
14218       char cSep = '(';
14219       while( xRead(&sCtx) ){
14220         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
14221         cSep = ',';
14222         if( sCtx.cTerm!=sCtx.cColSep ) break;
14223       }
14224       if( cSep=='(' ){
14225         sqlite3_free(zCreate);
14226         sqlite3_free(sCtx.z);
14227         xCloser(sCtx.in);
14228         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
14229         return 1;
14230       }
14231       zCreate = sqlite3_mprintf("%z\n)", zCreate);
14232       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
14233       sqlite3_free(zCreate);
14234       if( rc ){
14235         utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
14236                 sqlite3_errmsg(p->db));
14237         sqlite3_free(sCtx.z);
14238         xCloser(sCtx.in);
14239         return 1;
14240       }
14241       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14242     }
14243     sqlite3_free(zSql);
14244     if( rc ){
14245       if (pStmt) sqlite3_finalize(pStmt);
14246       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
14247       xCloser(sCtx.in);
14248       return 1;
14249     }
14250     nCol = sqlite3_column_count(pStmt);
14251     sqlite3_finalize(pStmt);
14252     pStmt = 0;
14253     if( nCol==0 ) return 0; /* no columns, no error */
14254     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
14255     if( zSql==0 ){
14256       xCloser(sCtx.in);
14257       shell_out_of_memory();
14258     }
14259     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
14260     j = strlen30(zSql);
14261     for(i=1; i<nCol; i++){
14262       zSql[j++] = ',';
14263       zSql[j++] = '?';
14264     }
14265     zSql[j++] = ')';
14266     zSql[j] = 0;
14267     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14268     sqlite3_free(zSql);
14269     if( rc ){
14270       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14271       if (pStmt) sqlite3_finalize(pStmt);
14272       xCloser(sCtx.in);
14273       return 1;
14274     }
14275     needCommit = sqlite3_get_autocommit(p->db);
14276     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
14277     do{
14278       int startLine = sCtx.nLine;
14279       for(i=0; i<nCol; i++){
14280         char *z = xRead(&sCtx);
14281         /*
14282         ** Did we reach end-of-file before finding any columns?
14283         ** If so, stop instead of NULL filling the remaining columns.
14284         */
14285         if( z==0 && i==0 ) break;
14286         /*
14287         ** Did we reach end-of-file OR end-of-line before finding any
14288         ** columns in ASCII mode?  If so, stop instead of NULL filling
14289         ** the remaining columns.
14290         */
14291         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
14292         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
14293         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
14294           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14295                           "filling the rest with NULL\n",
14296                           sCtx.zFile, startLine, nCol, i+1);
14297           i += 2;
14298           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
14299         }
14300       }
14301       if( sCtx.cTerm==sCtx.cColSep ){
14302         do{
14303           xRead(&sCtx);
14304           i++;
14305         }while( sCtx.cTerm==sCtx.cColSep );
14306         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14307                         "extras ignored\n",
14308                         sCtx.zFile, startLine, nCol, i);
14309       }
14310       if( i>=nCol ){
14311         sqlite3_step(pStmt);
14312         rc = sqlite3_reset(pStmt);
14313         if( rc!=SQLITE_OK ){
14314           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
14315                       startLine, sqlite3_errmsg(p->db));
14316         }
14317       }
14318     }while( sCtx.cTerm!=EOF );
14319
14320     xCloser(sCtx.in);
14321     sqlite3_free(sCtx.z);
14322     sqlite3_finalize(pStmt);
14323     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
14324   }else
14325
14326 #ifndef SQLITE_UNTESTABLE
14327   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
14328     char *zSql;
14329     char *zCollist = 0;
14330     sqlite3_stmt *pStmt;
14331     int tnum = 0;
14332     int i;
14333     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
14334       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
14335                           "       .imposter off\n");
14336       rc = 1;
14337       goto meta_command_exit;
14338     }
14339     open_db(p, 0);
14340     if( nArg==2 ){
14341       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
14342       goto meta_command_exit;
14343     }
14344     zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
14345                            " WHERE name='%q' AND type='index'", azArg[1]);
14346     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14347     sqlite3_free(zSql);
14348     if( sqlite3_step(pStmt)==SQLITE_ROW ){
14349       tnum = sqlite3_column_int(pStmt, 0);
14350     }
14351     sqlite3_finalize(pStmt);
14352     if( tnum==0 ){
14353       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
14354       rc = 1;
14355       goto meta_command_exit;
14356     }
14357     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
14358     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14359     sqlite3_free(zSql);
14360     i = 0;
14361     while( sqlite3_step(pStmt)==SQLITE_ROW ){
14362       char zLabel[20];
14363       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
14364       i++;
14365       if( zCol==0 ){
14366         if( sqlite3_column_int(pStmt,1)==-1 ){
14367           zCol = "_ROWID_";
14368         }else{
14369           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
14370           zCol = zLabel;
14371         }
14372       }
14373       if( zCollist==0 ){
14374         zCollist = sqlite3_mprintf("\"%w\"", zCol);
14375       }else{
14376         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
14377       }
14378     }
14379     sqlite3_finalize(pStmt);
14380     zSql = sqlite3_mprintf(
14381           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
14382           azArg[2], zCollist, zCollist);
14383     sqlite3_free(zCollist);
14384     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
14385     if( rc==SQLITE_OK ){
14386       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
14387       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
14388       if( rc ){
14389         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
14390       }else{
14391         utf8_printf(stdout, "%s;\n", zSql);
14392         raw_printf(stdout,
14393            "WARNING: writing to an imposter table will corrupt the index!\n"
14394         );
14395       }
14396     }else{
14397       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
14398       rc = 1;
14399     }
14400     sqlite3_free(zSql);
14401   }else
14402 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
14403
14404 #ifdef SQLITE_ENABLE_IOTRACE
14405   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
14406     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
14407     if( iotrace && iotrace!=stdout ) fclose(iotrace);
14408     iotrace = 0;
14409     if( nArg<2 ){
14410       sqlite3IoTrace = 0;
14411     }else if( strcmp(azArg[1], "-")==0 ){
14412       sqlite3IoTrace = iotracePrintf;
14413       iotrace = stdout;
14414     }else{
14415       iotrace = fopen(azArg[1], "w");
14416       if( iotrace==0 ){
14417         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
14418         sqlite3IoTrace = 0;
14419         rc = 1;
14420       }else{
14421         sqlite3IoTrace = iotracePrintf;
14422       }
14423     }
14424   }else
14425 #endif
14426
14427   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
14428     static const struct {
14429        const char *zLimitName;   /* Name of a limit */
14430        int limitCode;            /* Integer code for that limit */
14431     } aLimit[] = {
14432       { "length",                SQLITE_LIMIT_LENGTH                    },
14433       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
14434       { "column",                SQLITE_LIMIT_COLUMN                    },
14435       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
14436       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
14437       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
14438       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
14439       { "attached",              SQLITE_LIMIT_ATTACHED                  },
14440       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
14441       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
14442       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
14443       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
14444     };
14445     int i, n2;
14446     open_db(p, 0);
14447     if( nArg==1 ){
14448       for(i=0; i<ArraySize(aLimit); i++){
14449         printf("%20s %d\n", aLimit[i].zLimitName,
14450                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
14451       }
14452     }else if( nArg>3 ){
14453       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
14454       rc = 1;
14455       goto meta_command_exit;
14456     }else{
14457       int iLimit = -1;
14458       n2 = strlen30(azArg[1]);
14459       for(i=0; i<ArraySize(aLimit); i++){
14460         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
14461           if( iLimit<0 ){
14462             iLimit = i;
14463           }else{
14464             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
14465             rc = 1;
14466             goto meta_command_exit;
14467           }
14468         }
14469       }
14470       if( iLimit<0 ){
14471         utf8_printf(stderr, "unknown limit: \"%s\"\n"
14472                         "enter \".limits\" with no arguments for a list.\n",
14473                          azArg[1]);
14474         rc = 1;
14475         goto meta_command_exit;
14476       }
14477       if( nArg==3 ){
14478         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
14479                       (int)integerValue(azArg[2]));
14480       }
14481       printf("%20s %d\n", aLimit[iLimit].zLimitName,
14482              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
14483     }
14484   }else
14485
14486   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
14487     open_db(p, 0);
14488     lintDotCommand(p, azArg, nArg);
14489   }else
14490
14491 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14492   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
14493     const char *zFile, *zProc;
14494     char *zErrMsg = 0;
14495     if( nArg<2 ){
14496       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
14497       rc = 1;
14498       goto meta_command_exit;
14499     }
14500     zFile = azArg[1];
14501     zProc = nArg>=3 ? azArg[2] : 0;
14502     open_db(p, 0);
14503     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
14504     if( rc!=SQLITE_OK ){
14505       utf8_printf(stderr, "Error: %s\n", zErrMsg);
14506       sqlite3_free(zErrMsg);
14507       rc = 1;
14508     }
14509   }else
14510 #endif
14511
14512   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
14513     if( nArg!=2 ){
14514       raw_printf(stderr, "Usage: .log FILENAME\n");
14515       rc = 1;
14516     }else{
14517       const char *zFile = azArg[1];
14518       output_file_close(p->pLog);
14519       p->pLog = output_file_open(zFile, 0);
14520     }
14521   }else
14522
14523   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
14524     const char *zMode = nArg>=2 ? azArg[1] : "";
14525     int n2 = strlen30(zMode);
14526     int c2 = zMode[0];
14527     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
14528       p->mode = MODE_Line;
14529       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14530     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
14531       p->mode = MODE_Column;
14532       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14533     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
14534       p->mode = MODE_List;
14535       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
14536       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14537     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
14538       p->mode = MODE_Html;
14539     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
14540       p->mode = MODE_Tcl;
14541       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
14542       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14543     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
14544       p->mode = MODE_Csv;
14545       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14546       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14547     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
14548       p->mode = MODE_List;
14549       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
14550     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
14551       p->mode = MODE_Insert;
14552       set_table_name(p, nArg>=3 ? azArg[2] : "table");
14553     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
14554       p->mode = MODE_Quote;
14555     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
14556       p->mode = MODE_Ascii;
14557       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
14558       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
14559     }else if( nArg==1 ){
14560       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
14561     }else{
14562       raw_printf(stderr, "Error: mode should be one of: "
14563          "ascii column csv html insert line list quote tabs tcl\n");
14564       rc = 1;
14565     }
14566     p->cMode = p->mode;
14567   }else
14568
14569   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
14570     if( nArg==2 ){
14571       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
14572                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
14573     }else{
14574       raw_printf(stderr, "Usage: .nullvalue STRING\n");
14575       rc = 1;
14576     }
14577   }else
14578
14579   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
14580     char *zNewFilename;  /* Name of the database file to open */
14581     int iName = 1;       /* Index in azArg[] of the filename */
14582     int newFlag = 0;     /* True to delete file before opening */
14583     /* Close the existing database */
14584     session_close_all(p);
14585     close_db(p->db);
14586     p->db = 0;
14587     p->zDbFilename = 0;
14588     sqlite3_free(p->zFreeOnClose);
14589     p->zFreeOnClose = 0;
14590     p->openMode = SHELL_OPEN_UNSPEC;
14591     p->szMax = 0;
14592     /* Check for command-line arguments */
14593     for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
14594       const char *z = azArg[iName];
14595       if( optionMatch(z,"new") ){
14596         newFlag = 1;
14597 #ifdef SQLITE_HAVE_ZLIB
14598       }else if( optionMatch(z, "zip") ){
14599         p->openMode = SHELL_OPEN_ZIPFILE;
14600 #endif
14601       }else if( optionMatch(z, "append") ){
14602         p->openMode = SHELL_OPEN_APPENDVFS;
14603       }else if( optionMatch(z, "readonly") ){
14604         p->openMode = SHELL_OPEN_READONLY;
14605 #ifdef SQLITE_ENABLE_DESERIALIZE
14606       }else if( optionMatch(z, "deserialize") ){
14607         p->openMode = SHELL_OPEN_DESERIALIZE;
14608       }else if( optionMatch(z, "hexdb") ){
14609         p->openMode = SHELL_OPEN_HEXDB;
14610       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
14611         p->szMax = integerValue(azArg[++iName]);
14612 #endif /* SQLITE_ENABLE_DESERIALIZE */
14613       }else if( z[0]=='-' ){
14614         utf8_printf(stderr, "unknown option: %s\n", z);
14615         rc = 1;
14616         goto meta_command_exit;
14617       }
14618     }
14619     /* If a filename is specified, try to open it first */
14620     zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14621     if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
14622       if( newFlag ) shellDeleteFile(zNewFilename);
14623       p->zDbFilename = zNewFilename;
14624       open_db(p, OPEN_DB_KEEPALIVE);
14625       if( p->db==0 ){
14626         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14627         sqlite3_free(zNewFilename);
14628       }else{
14629         p->zFreeOnClose = zNewFilename;
14630       }
14631     }
14632     if( p->db==0 ){
14633       /* As a fall-back open a TEMP database */
14634       p->zDbFilename = 0;
14635       open_db(p, 0);
14636     }
14637   }else
14638
14639   if( (c=='o'
14640         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
14641    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
14642   ){
14643     const char *zFile = nArg>=2 ? azArg[1] : "stdout";
14644     int bTxtMode = 0;
14645     if( azArg[0][0]=='e' ){
14646       /* Transform the ".excel" command into ".once -x" */
14647       nArg = 2;
14648       azArg[0] = "once";
14649       zFile = azArg[1] = "-x";
14650       n = 4;
14651     }
14652     if( nArg>2 ){
14653       utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
14654       rc = 1;
14655       goto meta_command_exit;
14656     }
14657     if( n>1 && strncmp(azArg[0], "once", n)==0 ){
14658       if( nArg<2 ){
14659         raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
14660         rc = 1;
14661         goto meta_command_exit;
14662       }
14663       p->outCount = 2;
14664     }else{
14665       p->outCount = 0;
14666     }
14667     output_reset(p);
14668     if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
14669 #ifndef SQLITE_NOHAVE_SYSTEM
14670     if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
14671       p->doXdgOpen = 1;
14672       outputModePush(p);
14673       if( zFile[1]=='x' ){
14674         newTempFile(p, "csv");
14675         p->mode = MODE_Csv;
14676         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14677         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14678       }else{
14679         newTempFile(p, "txt");
14680         bTxtMode = 1;
14681       }
14682       zFile = p->zTempFile;
14683     }
14684 #endif /* SQLITE_NOHAVE_SYSTEM */
14685     if( zFile[0]=='|' ){
14686 #ifdef SQLITE_OMIT_POPEN
14687       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14688       rc = 1;
14689       p->out = stdout;
14690 #else
14691       p->out = popen(zFile + 1, "w");
14692       if( p->out==0 ){
14693         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
14694         p->out = stdout;
14695         rc = 1;
14696       }else{
14697         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14698       }
14699 #endif
14700     }else{
14701       p->out = output_file_open(zFile, bTxtMode);
14702       if( p->out==0 ){
14703         if( strcmp(zFile,"off")!=0 ){
14704           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
14705         }
14706         p->out = stdout;
14707         rc = 1;
14708       } else {
14709         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14710       }
14711     }
14712   }else
14713
14714   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14715     int i;
14716     for(i=1; i<nArg; i++){
14717       if( i>1 ) raw_printf(p->out, " ");
14718       utf8_printf(p->out, "%s", azArg[i]);
14719     }
14720     raw_printf(p->out, "\n");
14721   }else
14722
14723 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14724   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
14725     int i;
14726     int nn = 0;
14727     p->flgProgress = 0;
14728     p->mxProgress = 0;
14729     p->nProgress = 0;
14730     for(i=1; i<nArg; i++){
14731       const char *z = azArg[i];
14732       if( z[0]=='-' ){
14733         z++;
14734         if( z[0]=='-' ) z++;
14735         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
14736           p->flgProgress |= SHELL_PROGRESS_QUIET;
14737           continue;
14738         }
14739         if( strcmp(z,"reset")==0 ){
14740           p->flgProgress |= SHELL_PROGRESS_RESET;
14741           continue;
14742         }
14743         if( strcmp(z,"once")==0 ){
14744           p->flgProgress |= SHELL_PROGRESS_ONCE;
14745           continue;
14746         }
14747         if( strcmp(z,"limit")==0 ){
14748           if( i+1>=nArg ){
14749             utf8_printf(stderr, "Error: missing argument on --limit\n");
14750             rc = 1;
14751             goto meta_command_exit;
14752           }else{
14753             p->mxProgress = (int)integerValue(azArg[++i]);
14754           }
14755           continue;
14756         }
14757         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
14758         rc = 1;
14759         goto meta_command_exit;
14760       }else{
14761         nn = (int)integerValue(z);
14762       }
14763     }
14764     open_db(p, 0);
14765     sqlite3_progress_handler(p->db, nn, progress_handler, p);
14766   }else
14767 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
14768
14769   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
14770     if( nArg >= 2) {
14771       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
14772     }
14773     if( nArg >= 3) {
14774       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
14775     }
14776   }else
14777
14778   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
14779     rc = 2;
14780   }else
14781
14782   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
14783     FILE *inSaved = p->in;
14784     int savedLineno = p->lineno;
14785     if( nArg!=2 ){
14786       raw_printf(stderr, "Usage: .read FILE\n");
14787       rc = 1;
14788       goto meta_command_exit;
14789     }
14790     p->in = fopen(azArg[1], "rb");
14791     if( p->in==0 ){
14792       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
14793       rc = 1;
14794     }else{
14795       rc = process_input(p);
14796       fclose(p->in);
14797     }
14798     p->in = inSaved;
14799     p->lineno = savedLineno;
14800   }else
14801
14802   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
14803     const char *zSrcFile;
14804     const char *zDb;
14805     sqlite3 *pSrc;
14806     sqlite3_backup *pBackup;
14807     int nTimeout = 0;
14808
14809     if( nArg==2 ){
14810       zSrcFile = azArg[1];
14811       zDb = "main";
14812     }else if( nArg==3 ){
14813       zSrcFile = azArg[2];
14814       zDb = azArg[1];
14815     }else{
14816       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
14817       rc = 1;
14818       goto meta_command_exit;
14819     }
14820     rc = sqlite3_open(zSrcFile, &pSrc);
14821     if( rc!=SQLITE_OK ){
14822       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14823       close_db(pSrc);
14824       return 1;
14825     }
14826     open_db(p, 0);
14827     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
14828     if( pBackup==0 ){
14829       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14830       close_db(pSrc);
14831       return 1;
14832     }
14833     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
14834           || rc==SQLITE_BUSY  ){
14835       if( rc==SQLITE_BUSY ){
14836         if( nTimeout++ >= 3 ) break;
14837         sqlite3_sleep(100);
14838       }
14839     }
14840     sqlite3_backup_finish(pBackup);
14841     if( rc==SQLITE_DONE ){
14842       rc = 0;
14843     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
14844       raw_printf(stderr, "Error: source database is busy\n");
14845       rc = 1;
14846     }else{
14847       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14848       rc = 1;
14849     }
14850     close_db(pSrc);
14851   }else
14852
14853   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
14854     if( nArg==2 ){
14855       p->scanstatsOn = (u8)booleanValue(azArg[1]);
14856 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
14857       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
14858 #endif
14859     }else{
14860       raw_printf(stderr, "Usage: .scanstats on|off\n");
14861       rc = 1;
14862     }
14863   }else
14864
14865   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
14866     ShellText sSelect;
14867     ShellState data;
14868     char *zErrMsg = 0;
14869     const char *zDiv = "(";
14870     const char *zName = 0;
14871     int iSchema = 0;
14872     int bDebug = 0;
14873     int ii;
14874
14875     open_db(p, 0);
14876     memcpy(&data, p, sizeof(data));
14877     data.showHeader = 0;
14878     data.cMode = data.mode = MODE_Semi;
14879     initText(&sSelect);
14880     for(ii=1; ii<nArg; ii++){
14881       if( optionMatch(azArg[ii],"indent") ){
14882         data.cMode = data.mode = MODE_Pretty;
14883       }else if( optionMatch(azArg[ii],"debug") ){
14884         bDebug = 1;
14885       }else if( zName==0 ){
14886         zName = azArg[ii];
14887       }else{
14888         raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
14889         rc = 1;
14890         goto meta_command_exit;
14891       }
14892     }
14893     if( zName!=0 ){
14894       int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
14895       if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
14896         char *new_argv[2], *new_colv[2];
14897         new_argv[0] = sqlite3_mprintf(
14898                       "CREATE TABLE %s (\n"
14899                       "  type text,\n"
14900                       "  name text,\n"
14901                       "  tbl_name text,\n"
14902                       "  rootpage integer,\n"
14903                       "  sql text\n"
14904                       ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
14905         new_argv[1] = 0;
14906         new_colv[0] = "sql";
14907         new_colv[1] = 0;
14908         callback(&data, 1, new_argv, new_colv);
14909         sqlite3_free(new_argv[0]);
14910       }
14911     }
14912     if( zDiv ){
14913       sqlite3_stmt *pStmt = 0;
14914       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
14915                               -1, &pStmt, 0);
14916       if( rc ){
14917         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14918         sqlite3_finalize(pStmt);
14919         rc = 1;
14920         goto meta_command_exit;
14921       }
14922       appendText(&sSelect, "SELECT sql FROM", 0);
14923       iSchema = 0;
14924       while( sqlite3_step(pStmt)==SQLITE_ROW ){
14925         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
14926         char zScNum[30];
14927         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
14928         appendText(&sSelect, zDiv, 0);
14929         zDiv = " UNION ALL ";
14930         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
14931         if( sqlite3_stricmp(zDb, "main")!=0 ){
14932           appendText(&sSelect, zDb, '"');
14933         }else{
14934           appendText(&sSelect, "NULL", 0);
14935         }
14936         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
14937         appendText(&sSelect, zScNum, 0);
14938         appendText(&sSelect, " AS snum, ", 0);
14939         appendText(&sSelect, zDb, '\'');
14940         appendText(&sSelect, " AS sname FROM ", 0);
14941         appendText(&sSelect, zDb, '"');
14942         appendText(&sSelect, ".sqlite_master", 0);
14943       }
14944       sqlite3_finalize(pStmt);
14945 #ifdef SQLITE_INTROSPECTION_PRAGMAS
14946       if( zName ){
14947         appendText(&sSelect,
14948            " UNION ALL SELECT shell_module_schema(name),"
14949            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
14950       }
14951 #endif
14952       appendText(&sSelect, ") WHERE ", 0);
14953       if( zName ){
14954         char *zQarg = sqlite3_mprintf("%Q", zName);
14955         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
14956                     strchr(zName, '[') != 0;
14957         if( strchr(zName, '.') ){
14958           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
14959         }else{
14960           appendText(&sSelect, "lower(tbl_name)", 0);
14961         }
14962         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
14963         appendText(&sSelect, zQarg, 0);
14964         if( !bGlob ){
14965           appendText(&sSelect, " ESCAPE '\\' ", 0);
14966         }
14967         appendText(&sSelect, " AND ", 0);
14968         sqlite3_free(zQarg);
14969       }
14970       appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
14971                            " ORDER BY snum, rowid", 0);
14972       if( bDebug ){
14973         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
14974       }else{
14975         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
14976       }
14977       freeText(&sSelect);
14978     }
14979     if( zErrMsg ){
14980       utf8_printf(stderr,"Error: %s\n", zErrMsg);
14981       sqlite3_free(zErrMsg);
14982       rc = 1;
14983     }else if( rc != SQLITE_OK ){
14984       raw_printf(stderr,"Error: querying schema information\n");
14985       rc = 1;
14986     }else{
14987       rc = 0;
14988     }
14989   }else
14990
14991 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
14992   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
14993     sqlite3SelectTrace = (int)integerValue(azArg[1]);
14994   }else
14995 #endif
14996
14997 #if defined(SQLITE_ENABLE_SESSION)
14998   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
14999     OpenSession *pSession = &p->aSession[0];
15000     char **azCmd = &azArg[1];
15001     int iSes = 0;
15002     int nCmd = nArg - 1;
15003     int i;
15004     if( nArg<=1 ) goto session_syntax_error;
15005     open_db(p, 0);
15006     if( nArg>=3 ){
15007       for(iSes=0; iSes<p->nSession; iSes++){
15008         if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
15009       }
15010       if( iSes<p->nSession ){
15011         pSession = &p->aSession[iSes];
15012         azCmd++;
15013         nCmd--;
15014       }else{
15015         pSession = &p->aSession[0];
15016         iSes = 0;
15017       }
15018     }
15019
15020     /* .session attach TABLE
15021     ** Invoke the sqlite3session_attach() interface to attach a particular
15022     ** table so that it is never filtered.
15023     */
15024     if( strcmp(azCmd[0],"attach")==0 ){
15025       if( nCmd!=2 ) goto session_syntax_error;
15026       if( pSession->p==0 ){
15027         session_not_open:
15028         raw_printf(stderr, "ERROR: No sessions are open\n");
15029       }else{
15030         rc = sqlite3session_attach(pSession->p, azCmd[1]);
15031         if( rc ){
15032           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
15033           rc = 0;
15034         }
15035       }
15036     }else
15037
15038     /* .session changeset FILE
15039     ** .session patchset FILE
15040     ** Write a changeset or patchset into a file.  The file is overwritten.
15041     */
15042     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
15043       FILE *out = 0;
15044       if( nCmd!=2 ) goto session_syntax_error;
15045       if( pSession->p==0 ) goto session_not_open;
15046       out = fopen(azCmd[1], "wb");
15047       if( out==0 ){
15048         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
15049       }else{
15050         int szChng;
15051         void *pChng;
15052         if( azCmd[0][0]=='c' ){
15053           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
15054         }else{
15055           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
15056         }
15057         if( rc ){
15058           printf("Error: error code %d\n", rc);
15059           rc = 0;
15060         }
15061         if( pChng
15062           && fwrite(pChng, szChng, 1, out)!=1 ){
15063           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
15064                   szChng);
15065         }
15066         sqlite3_free(pChng);
15067         fclose(out);
15068       }
15069     }else
15070
15071     /* .session close
15072     ** Close the identified session
15073     */
15074     if( strcmp(azCmd[0], "close")==0 ){
15075       if( nCmd!=1 ) goto session_syntax_error;
15076       if( p->nSession ){
15077         session_close(pSession);
15078         p->aSession[iSes] = p->aSession[--p->nSession];
15079       }
15080     }else
15081
15082     /* .session enable ?BOOLEAN?
15083     ** Query or set the enable flag
15084     */
15085     if( strcmp(azCmd[0], "enable")==0 ){
15086       int ii;
15087       if( nCmd>2 ) goto session_syntax_error;
15088       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15089       if( p->nSession ){
15090         ii = sqlite3session_enable(pSession->p, ii);
15091         utf8_printf(p->out, "session %s enable flag = %d\n",
15092                     pSession->zName, ii);
15093       }
15094     }else
15095
15096     /* .session filter GLOB ....
15097     ** Set a list of GLOB patterns of table names to be excluded.
15098     */
15099     if( strcmp(azCmd[0], "filter")==0 ){
15100       int ii, nByte;
15101       if( nCmd<2 ) goto session_syntax_error;
15102       if( p->nSession ){
15103         for(ii=0; ii<pSession->nFilter; ii++){
15104           sqlite3_free(pSession->azFilter[ii]);
15105         }
15106         sqlite3_free(pSession->azFilter);
15107         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
15108         pSession->azFilter = sqlite3_malloc( nByte );
15109         if( pSession->azFilter==0 ){
15110           raw_printf(stderr, "Error: out or memory\n");
15111           exit(1);
15112         }
15113         for(ii=1; ii<nCmd; ii++){
15114           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
15115         }
15116         pSession->nFilter = ii-1;
15117       }
15118     }else
15119
15120     /* .session indirect ?BOOLEAN?
15121     ** Query or set the indirect flag
15122     */
15123     if( strcmp(azCmd[0], "indirect")==0 ){
15124       int ii;
15125       if( nCmd>2 ) goto session_syntax_error;
15126       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15127       if( p->nSession ){
15128         ii = sqlite3session_indirect(pSession->p, ii);
15129         utf8_printf(p->out, "session %s indirect flag = %d\n",
15130                     pSession->zName, ii);
15131       }
15132     }else
15133
15134     /* .session isempty
15135     ** Determine if the session is empty
15136     */
15137     if( strcmp(azCmd[0], "isempty")==0 ){
15138       int ii;
15139       if( nCmd!=1 ) goto session_syntax_error;
15140       if( p->nSession ){
15141         ii = sqlite3session_isempty(pSession->p);
15142         utf8_printf(p->out, "session %s isempty flag = %d\n",
15143                     pSession->zName, ii);
15144       }
15145     }else
15146
15147     /* .session list
15148     ** List all currently open sessions
15149     */
15150     if( strcmp(azCmd[0],"list")==0 ){
15151       for(i=0; i<p->nSession; i++){
15152         utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
15153       }
15154     }else
15155
15156     /* .session open DB NAME
15157     ** Open a new session called NAME on the attached database DB.
15158     ** DB is normally "main".
15159     */
15160     if( strcmp(azCmd[0],"open")==0 ){
15161       char *zName;
15162       if( nCmd!=3 ) goto session_syntax_error;
15163       zName = azCmd[2];
15164       if( zName[0]==0 ) goto session_syntax_error;
15165       for(i=0; i<p->nSession; i++){
15166         if( strcmp(p->aSession[i].zName,zName)==0 ){
15167           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
15168           goto meta_command_exit;
15169         }
15170       }
15171       if( p->nSession>=ArraySize(p->aSession) ){
15172         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
15173         goto meta_command_exit;
15174       }
15175       pSession = &p->aSession[p->nSession];
15176       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
15177       if( rc ){
15178         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
15179         rc = 0;
15180         goto meta_command_exit;
15181       }
15182       pSession->nFilter = 0;
15183       sqlite3session_table_filter(pSession->p, session_filter, pSession);
15184       p->nSession++;
15185       pSession->zName = sqlite3_mprintf("%s", zName);
15186     }else
15187     /* If no command name matches, show a syntax error */
15188     session_syntax_error:
15189     showHelp(p->out, "session");
15190   }else
15191 #endif
15192
15193 #ifdef SQLITE_DEBUG
15194   /* Undocumented commands for internal testing.  Subject to change
15195   ** without notice. */
15196   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
15197     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
15198       int i, v;
15199       for(i=1; i<nArg; i++){
15200         v = booleanValue(azArg[i]);
15201         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
15202       }
15203     }
15204     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
15205       int i; sqlite3_int64 v;
15206       for(i=1; i<nArg; i++){
15207         char zBuf[200];
15208         v = integerValue(azArg[i]);
15209         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
15210         utf8_printf(p->out, "%s", zBuf);
15211       }
15212     }
15213   }else
15214 #endif
15215
15216   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
15217     int bIsInit = 0;         /* True to initialize the SELFTEST table */
15218     int bVerbose = 0;        /* Verbose output */
15219     int bSelftestExists;     /* True if SELFTEST already exists */
15220     int i, k;                /* Loop counters */
15221     int nTest = 0;           /* Number of tests runs */
15222     int nErr = 0;            /* Number of errors seen */
15223     ShellText str;           /* Answer for a query */
15224     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
15225
15226     open_db(p,0);
15227     for(i=1; i<nArg; i++){
15228       const char *z = azArg[i];
15229       if( z[0]=='-' && z[1]=='-' ) z++;
15230       if( strcmp(z,"-init")==0 ){
15231         bIsInit = 1;
15232       }else
15233       if( strcmp(z,"-v")==0 ){
15234         bVerbose++;
15235       }else
15236       {
15237         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15238                     azArg[i], azArg[0]);
15239         raw_printf(stderr, "Should be one of: --init -v\n");
15240         rc = 1;
15241         goto meta_command_exit;
15242       }
15243     }
15244     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
15245            != SQLITE_OK ){
15246       bSelftestExists = 0;
15247     }else{
15248       bSelftestExists = 1;
15249     }
15250     if( bIsInit ){
15251       createSelftestTable(p);
15252       bSelftestExists = 1;
15253     }
15254     initText(&str);
15255     appendText(&str, "x", 0);
15256     for(k=bSelftestExists; k>=0; k--){
15257       if( k==1 ){
15258         rc = sqlite3_prepare_v2(p->db,
15259             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
15260             -1, &pStmt, 0);
15261       }else{
15262         rc = sqlite3_prepare_v2(p->db,
15263           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
15264           "      (1,'run','PRAGMA integrity_check','ok')",
15265           -1, &pStmt, 0);
15266       }
15267       if( rc ){
15268         raw_printf(stderr, "Error querying the selftest table\n");
15269         rc = 1;
15270         sqlite3_finalize(pStmt);
15271         goto meta_command_exit;
15272       }
15273       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
15274         int tno = sqlite3_column_int(pStmt, 0);
15275         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
15276         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
15277         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
15278
15279         k = 0;
15280         if( bVerbose>0 ){
15281           char *zQuote = sqlite3_mprintf("%q", zSql);
15282           printf("%d: %s %s\n", tno, zOp, zSql);
15283           sqlite3_free(zQuote);
15284         }
15285         if( strcmp(zOp,"memo")==0 ){
15286           utf8_printf(p->out, "%s\n", zSql);
15287         }else
15288         if( strcmp(zOp,"run")==0 ){
15289           char *zErrMsg = 0;
15290           str.n = 0;
15291           str.z[0] = 0;
15292           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
15293           nTest++;
15294           if( bVerbose ){
15295             utf8_printf(p->out, "Result: %s\n", str.z);
15296           }
15297           if( rc || zErrMsg ){
15298             nErr++;
15299             rc = 1;
15300             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
15301             sqlite3_free(zErrMsg);
15302           }else if( strcmp(zAns,str.z)!=0 ){
15303             nErr++;
15304             rc = 1;
15305             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
15306             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
15307           }
15308         }else
15309         {
15310           utf8_printf(stderr,
15311             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
15312           rc = 1;
15313           break;
15314         }
15315       } /* End loop over rows of content from SELFTEST */
15316       sqlite3_finalize(pStmt);
15317     } /* End loop over k */
15318     freeText(&str);
15319     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
15320   }else
15321
15322   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
15323     if( nArg<2 || nArg>3 ){
15324       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
15325       rc = 1;
15326     }
15327     if( nArg>=2 ){
15328       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
15329                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
15330     }
15331     if( nArg>=3 ){
15332       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
15333                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
15334     }
15335   }else
15336
15337   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
15338     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
15339     int i;                   /* Loop counter */
15340     int bSchema = 0;         /* Also hash the schema */
15341     int bSeparate = 0;       /* Hash each table separately */
15342     int iSize = 224;         /* Hash algorithm to use */
15343     int bDebug = 0;          /* Only show the query that would have run */
15344     sqlite3_stmt *pStmt;     /* For querying tables names */
15345     char *zSql;              /* SQL to be run */
15346     char *zSep;              /* Separator */
15347     ShellText sSql;          /* Complete SQL for the query to run the hash */
15348     ShellText sQuery;        /* Set of queries used to read all content */
15349     open_db(p, 0);
15350     for(i=1; i<nArg; i++){
15351       const char *z = azArg[i];
15352       if( z[0]=='-' ){
15353         z++;
15354         if( z[0]=='-' ) z++;
15355         if( strcmp(z,"schema")==0 ){
15356           bSchema = 1;
15357         }else
15358         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
15359          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
15360         ){
15361           iSize = atoi(&z[5]);
15362         }else
15363         if( strcmp(z,"debug")==0 ){
15364           bDebug = 1;
15365         }else
15366         {
15367           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15368                       azArg[i], azArg[0]);
15369           raw_printf(stderr, "Should be one of: --schema"
15370                              " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
15371           rc = 1;
15372           goto meta_command_exit;
15373         }
15374       }else if( zLike ){
15375         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
15376         rc = 1;
15377         goto meta_command_exit;
15378       }else{
15379         zLike = z;
15380         bSeparate = 1;
15381         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
15382       }
15383     }
15384     if( bSchema ){
15385       zSql = "SELECT lower(name) FROM sqlite_master"
15386              " WHERE type='table' AND coalesce(rootpage,0)>1"
15387              " UNION ALL SELECT 'sqlite_master'"
15388              " ORDER BY 1 collate nocase";
15389     }else{
15390       zSql = "SELECT lower(name) FROM sqlite_master"
15391              " WHERE type='table' AND coalesce(rootpage,0)>1"
15392              " AND name NOT LIKE 'sqlite_%'"
15393              " ORDER BY 1 collate nocase";
15394     }
15395     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15396     initText(&sQuery);
15397     initText(&sSql);
15398     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
15399     zSep = "VALUES(";
15400     while( SQLITE_ROW==sqlite3_step(pStmt) ){
15401       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
15402       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
15403       if( strncmp(zTab, "sqlite_",7)!=0 ){
15404         appendText(&sQuery,"SELECT * FROM ", 0);
15405         appendText(&sQuery,zTab,'"');
15406         appendText(&sQuery," NOT INDEXED;", 0);
15407       }else if( strcmp(zTab, "sqlite_master")==0 ){
15408         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
15409                            " ORDER BY name;", 0);
15410       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
15411         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
15412                            " ORDER BY name;", 0);
15413       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
15414         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
15415                            " ORDER BY tbl,idx;", 0);
15416       }else if( strcmp(zTab, "sqlite_stat3")==0
15417              || strcmp(zTab, "sqlite_stat4")==0 ){
15418         appendText(&sQuery, "SELECT * FROM ", 0);
15419         appendText(&sQuery, zTab, 0);
15420         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
15421       }
15422       appendText(&sSql, zSep, 0);
15423       appendText(&sSql, sQuery.z, '\'');
15424       sQuery.n = 0;
15425       appendText(&sSql, ",", 0);
15426       appendText(&sSql, zTab, '\'');
15427       zSep = "),(";
15428     }
15429     sqlite3_finalize(pStmt);
15430     if( bSeparate ){
15431       zSql = sqlite3_mprintf(
15432           "%s))"
15433           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
15434           "   FROM [sha3sum$query]",
15435           sSql.z, iSize);
15436     }else{
15437       zSql = sqlite3_mprintf(
15438           "%s))"
15439           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
15440           "   FROM [sha3sum$query]",
15441           sSql.z, iSize);
15442     }
15443     freeText(&sQuery);
15444     freeText(&sSql);
15445     if( bDebug ){
15446       utf8_printf(p->out, "%s\n", zSql);
15447     }else{
15448       shell_exec(p, zSql, 0);
15449     }
15450     sqlite3_free(zSql);
15451   }else
15452
15453 #ifndef SQLITE_NOHAVE_SYSTEM
15454   if( c=='s'
15455    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
15456   ){
15457     char *zCmd;
15458     int i, x;
15459     if( nArg<2 ){
15460       raw_printf(stderr, "Usage: .system COMMAND\n");
15461       rc = 1;
15462       goto meta_command_exit;
15463     }
15464     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
15465     for(i=2; i<nArg; i++){
15466       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
15467                              zCmd, azArg[i]);
15468     }
15469     x = system(zCmd);
15470     sqlite3_free(zCmd);
15471     if( x ) raw_printf(stderr, "System command returns %d\n", x);
15472   }else
15473 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15474
15475   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
15476     static const char *azBool[] = { "off", "on", "trigger", "full"};
15477     int i;
15478     if( nArg!=1 ){
15479       raw_printf(stderr, "Usage: .show\n");
15480       rc = 1;
15481       goto meta_command_exit;
15482     }
15483     utf8_printf(p->out, "%12.12s: %s\n","echo",
15484                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
15485     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
15486     utf8_printf(p->out, "%12.12s: %s\n","explain",
15487          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
15488     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
15489     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
15490     utf8_printf(p->out, "%12.12s: ", "nullvalue");
15491       output_c_string(p->out, p->nullValue);
15492       raw_printf(p->out, "\n");
15493     utf8_printf(p->out,"%12.12s: %s\n","output",
15494             strlen30(p->outfile) ? p->outfile : "stdout");
15495     utf8_printf(p->out,"%12.12s: ", "colseparator");
15496       output_c_string(p->out, p->colSeparator);
15497       raw_printf(p->out, "\n");
15498     utf8_printf(p->out,"%12.12s: ", "rowseparator");
15499       output_c_string(p->out, p->rowSeparator);
15500       raw_printf(p->out, "\n");
15501     utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
15502     utf8_printf(p->out, "%12.12s: ", "width");
15503     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
15504       raw_printf(p->out, "%d ", p->colWidth[i]);
15505     }
15506     raw_printf(p->out, "\n");
15507     utf8_printf(p->out, "%12.12s: %s\n", "filename",
15508                 p->zDbFilename ? p->zDbFilename : "");
15509   }else
15510
15511   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
15512     if( nArg==2 ){
15513       p->statsOn = (u8)booleanValue(azArg[1]);
15514     }else if( nArg==1 ){
15515       display_stats(p->db, p, 0);
15516     }else{
15517       raw_printf(stderr, "Usage: .stats ?on|off?\n");
15518       rc = 1;
15519     }
15520   }else
15521
15522   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
15523    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
15524                  || strncmp(azArg[0], "indexes", n)==0) )
15525   ){
15526     sqlite3_stmt *pStmt;
15527     char **azResult;
15528     int nRow, nAlloc;
15529     int ii;
15530     ShellText s;
15531     initText(&s);
15532     open_db(p, 0);
15533     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
15534     if( rc ){
15535       sqlite3_finalize(pStmt);
15536       return shellDatabaseError(p->db);
15537     }
15538
15539     if( nArg>2 && c=='i' ){
15540       /* It is an historical accident that the .indexes command shows an error
15541       ** when called with the wrong number of arguments whereas the .tables
15542       ** command does not. */
15543       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
15544       rc = 1;
15545       sqlite3_finalize(pStmt);
15546       goto meta_command_exit;
15547     }
15548     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
15549       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
15550       if( zDbName==0 ) continue;
15551       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
15552       if( sqlite3_stricmp(zDbName, "main")==0 ){
15553         appendText(&s, "SELECT name FROM ", 0);
15554       }else{
15555         appendText(&s, "SELECT ", 0);
15556         appendText(&s, zDbName, '\'');
15557         appendText(&s, "||'.'||name FROM ", 0);
15558       }
15559       appendText(&s, zDbName, '"');
15560       appendText(&s, ".sqlite_master ", 0);
15561       if( c=='t' ){
15562         appendText(&s," WHERE type IN ('table','view')"
15563                       "   AND name NOT LIKE 'sqlite_%'"
15564                       "   AND name LIKE ?1", 0);
15565       }else{
15566         appendText(&s," WHERE type='index'"
15567                       "   AND tbl_name LIKE ?1", 0);
15568       }
15569     }
15570     rc = sqlite3_finalize(pStmt);
15571     appendText(&s, " ORDER BY 1", 0);
15572     rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
15573     freeText(&s);
15574     if( rc ) return shellDatabaseError(p->db);
15575
15576     /* Run the SQL statement prepared by the above block. Store the results
15577     ** as an array of nul-terminated strings in azResult[].  */
15578     nRow = nAlloc = 0;
15579     azResult = 0;
15580     if( nArg>1 ){
15581       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
15582     }else{
15583       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
15584     }
15585     while( sqlite3_step(pStmt)==SQLITE_ROW ){
15586       if( nRow>=nAlloc ){
15587         char **azNew;
15588         int n2 = nAlloc*2 + 10;
15589         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
15590         if( azNew==0 ) shell_out_of_memory();
15591         nAlloc = n2;
15592         azResult = azNew;
15593       }
15594       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
15595       if( 0==azResult[nRow] ) shell_out_of_memory();
15596       nRow++;
15597     }
15598     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
15599       rc = shellDatabaseError(p->db);
15600     }
15601
15602     /* Pretty-print the contents of array azResult[] to the output */
15603     if( rc==0 && nRow>0 ){
15604       int len, maxlen = 0;
15605       int i, j;
15606       int nPrintCol, nPrintRow;
15607       for(i=0; i<nRow; i++){
15608         len = strlen30(azResult[i]);
15609         if( len>maxlen ) maxlen = len;
15610       }
15611       nPrintCol = 80/(maxlen+2);
15612       if( nPrintCol<1 ) nPrintCol = 1;
15613       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
15614       for(i=0; i<nPrintRow; i++){
15615         for(j=i; j<nRow; j+=nPrintRow){
15616           char *zSp = j<nPrintRow ? "" : "  ";
15617           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
15618                       azResult[j] ? azResult[j]:"");
15619         }
15620         raw_printf(p->out, "\n");
15621       }
15622     }
15623
15624     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
15625     sqlite3_free(azResult);
15626   }else
15627
15628   /* Begin redirecting output to the file "testcase-out.txt" */
15629   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
15630     output_reset(p);
15631     p->out = output_file_open("testcase-out.txt", 0);
15632     if( p->out==0 ){
15633       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
15634     }
15635     if( nArg>=2 ){
15636       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
15637     }else{
15638       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
15639     }
15640   }else
15641
15642 #ifndef SQLITE_UNTESTABLE
15643   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
15644     static const struct {
15645        const char *zCtrlName;   /* Name of a test-control option */
15646        int ctrlCode;            /* Integer code for that option */
15647        const char *zUsage;      /* Usage notes */
15648     } aCtrl[] = {
15649       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
15650       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
15651     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
15652     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
15653       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
15654     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
15655       { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
15656       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"       },
15657       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
15658       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
15659       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
15660 #ifdef YYCOVERAGE
15661       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
15662 #endif
15663       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
15664       { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
15665       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
15666       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
15667       { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
15668     };
15669     int testctrl = -1;
15670     int iCtrl = -1;
15671     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
15672     int isOk = 0;
15673     int i, n2;
15674     const char *zCmd = 0;
15675
15676     open_db(p, 0);
15677     zCmd = nArg>=2 ? azArg[1] : "help";
15678
15679     /* The argument can optionally begin with "-" or "--" */
15680     if( zCmd[0]=='-' && zCmd[1] ){
15681       zCmd++;
15682       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15683     }
15684
15685     /* --help lists all test-controls */
15686     if( strcmp(zCmd,"help")==0 ){
15687       utf8_printf(p->out, "Available test-controls:\n");
15688       for(i=0; i<ArraySize(aCtrl); i++){
15689         utf8_printf(p->out, "  .testctrl %s %s\n",
15690                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15691       }
15692       rc = 1;
15693       goto meta_command_exit;
15694     }
15695
15696     /* convert testctrl text option to value. allow any unique prefix
15697     ** of the option name, or a numerical value. */
15698     n2 = strlen30(zCmd);
15699     for(i=0; i<ArraySize(aCtrl); i++){
15700       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15701         if( testctrl<0 ){
15702           testctrl = aCtrl[i].ctrlCode;
15703           iCtrl = i;
15704         }else{
15705           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
15706                               "Use \".testctrl --help\" for help\n", zCmd);
15707           rc = 1;
15708           goto meta_command_exit;
15709         }
15710       }
15711     }
15712     if( testctrl<0 ){
15713       utf8_printf(stderr,"Error: unknown test-control: %s\n"
15714                          "Use \".testctrl --help\" for help\n", zCmd);
15715     }else{
15716       switch(testctrl){
15717
15718         /* sqlite3_test_control(int, db, int) */
15719         case SQLITE_TESTCTRL_OPTIMIZATIONS:
15720         case SQLITE_TESTCTRL_RESERVE:
15721           if( nArg==3 ){
15722             int opt = (int)strtol(azArg[2], 0, 0);
15723             rc2 = sqlite3_test_control(testctrl, p->db, opt);
15724             isOk = 3;
15725           }
15726           break;
15727
15728         /* sqlite3_test_control(int) */
15729         case SQLITE_TESTCTRL_PRNG_SAVE:
15730         case SQLITE_TESTCTRL_PRNG_RESTORE:
15731         case SQLITE_TESTCTRL_PRNG_RESET:
15732         case SQLITE_TESTCTRL_BYTEORDER:
15733           if( nArg==2 ){
15734             rc2 = sqlite3_test_control(testctrl);
15735             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
15736           }
15737           break;
15738
15739         /* sqlite3_test_control(int, uint) */
15740         case SQLITE_TESTCTRL_PENDING_BYTE:
15741           if( nArg==3 ){
15742             unsigned int opt = (unsigned int)integerValue(azArg[2]);
15743             rc2 = sqlite3_test_control(testctrl, opt);
15744             isOk = 3;
15745           }
15746           break;
15747
15748         /* sqlite3_test_control(int, int) */
15749         case SQLITE_TESTCTRL_ASSERT:
15750         case SQLITE_TESTCTRL_ALWAYS:
15751         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
15752           if( nArg==3 ){
15753             int opt = booleanValue(azArg[2]);
15754             rc2 = sqlite3_test_control(testctrl, opt);
15755             isOk = 1;
15756           }
15757           break;
15758
15759         /* sqlite3_test_control(int, int) */
15760         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
15761         case SQLITE_TESTCTRL_NEVER_CORRUPT:
15762           if( nArg==3 ){
15763             int opt = booleanValue(azArg[2]);
15764             rc2 = sqlite3_test_control(testctrl, opt);
15765             isOk = 3;
15766           }
15767           break;
15768
15769         case SQLITE_TESTCTRL_IMPOSTER:
15770           if( nArg==5 ){
15771             rc2 = sqlite3_test_control(testctrl, p->db,
15772                           azArg[2],
15773                           integerValue(azArg[3]),
15774                           integerValue(azArg[4]));
15775             isOk = 3;
15776           }
15777           break;
15778
15779 #ifdef YYCOVERAGE
15780         case SQLITE_TESTCTRL_PARSER_COVERAGE:
15781           if( nArg==2 ){
15782             sqlite3_test_control(testctrl, p->out);
15783             isOk = 3;
15784           }
15785 #endif
15786       }
15787     }
15788     if( isOk==0 && iCtrl>=0 ){
15789       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
15790       rc = 1;
15791     }else if( isOk==1 ){
15792       raw_printf(p->out, "%d\n", rc2);
15793     }else if( isOk==2 ){
15794       raw_printf(p->out, "0x%08x\n", rc2);
15795     }
15796   }else
15797 #endif /* !defined(SQLITE_UNTESTABLE) */
15798
15799   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
15800     open_db(p, 0);
15801     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
15802   }else
15803
15804   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
15805     if( nArg==2 ){
15806       enableTimer = booleanValue(azArg[1]);
15807       if( enableTimer && !HAS_TIMER ){
15808         raw_printf(stderr, "Error: timer not available on this system.\n");
15809         enableTimer = 0;
15810       }
15811     }else{
15812       raw_printf(stderr, "Usage: .timer on|off\n");
15813       rc = 1;
15814     }
15815   }else
15816
15817 #ifndef SQLITE_OMIT_TRACE
15818   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
15819     int mType = 0;
15820     int jj;
15821     open_db(p, 0);
15822     for(jj=1; jj<nArg; jj++){
15823       const char *z = azArg[jj];
15824       if( z[0]=='-' ){
15825         if( optionMatch(z, "expanded") ){
15826           p->eTraceType = SHELL_TRACE_EXPANDED;
15827         }
15828 #ifdef SQLITE_ENABLE_NORMALIZE
15829         else if( optionMatch(z, "normalized") ){
15830           p->eTraceType = SHELL_TRACE_NORMALIZED;
15831         }
15832 #endif
15833         else if( optionMatch(z, "plain") ){
15834           p->eTraceType = SHELL_TRACE_PLAIN;
15835         }
15836         else if( optionMatch(z, "profile") ){
15837           mType |= SQLITE_TRACE_PROFILE;
15838         }
15839         else if( optionMatch(z, "row") ){
15840           mType |= SQLITE_TRACE_ROW;
15841         }
15842         else if( optionMatch(z, "stmt") ){
15843           mType |= SQLITE_TRACE_STMT;
15844         }
15845         else if( optionMatch(z, "close") ){
15846           mType |= SQLITE_TRACE_CLOSE;
15847         }
15848         else {
15849           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
15850           rc = 1;
15851           goto meta_command_exit;
15852         }
15853       }else{
15854         output_file_close(p->traceOut);
15855         p->traceOut = output_file_open(azArg[1], 0);
15856       }
15857     }
15858     if( p->traceOut==0 ){
15859       sqlite3_trace_v2(p->db, 0, 0, 0);
15860     }else{
15861       if( mType==0 ) mType = SQLITE_TRACE_STMT;
15862       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
15863     }
15864   }else
15865 #endif /* !defined(SQLITE_OMIT_TRACE) */
15866
15867 #if SQLITE_USER_AUTHENTICATION
15868   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
15869     if( nArg<2 ){
15870       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
15871       rc = 1;
15872       goto meta_command_exit;
15873     }
15874     open_db(p, 0);
15875     if( strcmp(azArg[1],"login")==0 ){
15876       if( nArg!=4 ){
15877         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
15878         rc = 1;
15879         goto meta_command_exit;
15880       }
15881       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
15882       if( rc ){
15883         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
15884         rc = 1;
15885       }
15886     }else if( strcmp(azArg[1],"add")==0 ){
15887       if( nArg!=5 ){
15888         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
15889         rc = 1;
15890         goto meta_command_exit;
15891       }
15892       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15893                             booleanValue(azArg[4]));
15894       if( rc ){
15895         raw_printf(stderr, "User-Add failed: %d\n", rc);
15896         rc = 1;
15897       }
15898     }else if( strcmp(azArg[1],"edit")==0 ){
15899       if( nArg!=5 ){
15900         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
15901         rc = 1;
15902         goto meta_command_exit;
15903       }
15904       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15905                               booleanValue(azArg[4]));
15906       if( rc ){
15907         raw_printf(stderr, "User-Edit failed: %d\n", rc);
15908         rc = 1;
15909       }
15910     }else if( strcmp(azArg[1],"delete")==0 ){
15911       if( nArg!=3 ){
15912         raw_printf(stderr, "Usage: .user delete USER\n");
15913         rc = 1;
15914         goto meta_command_exit;
15915       }
15916       rc = sqlite3_user_delete(p->db, azArg[2]);
15917       if( rc ){
15918         raw_printf(stderr, "User-Delete failed: %d\n", rc);
15919         rc = 1;
15920       }
15921     }else{
15922       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
15923       rc = 1;
15924       goto meta_command_exit;
15925     }
15926   }else
15927 #endif /* SQLITE_USER_AUTHENTICATION */
15928
15929   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
15930     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
15931         sqlite3_libversion(), sqlite3_sourceid());
15932 #if SQLITE_HAVE_ZLIB
15933     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
15934 #endif
15935 #define CTIMEOPT_VAL_(opt) #opt
15936 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
15937 #if defined(__clang__) && defined(__clang_major__)
15938     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
15939                     CTIMEOPT_VAL(__clang_minor__) "."
15940                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
15941 #elif defined(_MSC_VER)
15942     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
15943 #elif defined(__GNUC__) && defined(__VERSION__)
15944     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
15945 #endif
15946   }else
15947
15948   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
15949     const char *zDbName = nArg==2 ? azArg[1] : "main";
15950     sqlite3_vfs *pVfs = 0;
15951     if( p->db ){
15952       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
15953       if( pVfs ){
15954         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
15955         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
15956         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
15957         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15958       }
15959     }
15960   }else
15961
15962   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
15963     sqlite3_vfs *pVfs;
15964     sqlite3_vfs *pCurrent = 0;
15965     if( p->db ){
15966       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
15967     }
15968     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
15969       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
15970            pVfs==pCurrent ? "  <--- CURRENT" : "");
15971       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
15972       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
15973       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15974       if( pVfs->pNext ){
15975         raw_printf(p->out, "-----------------------------------\n");
15976       }
15977     }
15978   }else
15979
15980   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
15981     const char *zDbName = nArg==2 ? azArg[1] : "main";
15982     char *zVfsName = 0;
15983     if( p->db ){
15984       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
15985       if( zVfsName ){
15986         utf8_printf(p->out, "%s\n", zVfsName);
15987         sqlite3_free(zVfsName);
15988       }
15989     }
15990   }else
15991
15992 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
15993   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
15994     sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
15995   }else
15996 #endif
15997
15998   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
15999     int j;
16000     assert( nArg<=ArraySize(azArg) );
16001     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
16002       p->colWidth[j-1] = (int)integerValue(azArg[j]);
16003     }
16004   }else
16005
16006   {
16007     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
16008       " \"%s\". Enter \".help\" for help\n", azArg[0]);
16009     rc = 1;
16010   }
16011
16012 meta_command_exit:
16013   if( p->outCount ){
16014     p->outCount--;
16015     if( p->outCount==0 ) output_reset(p);
16016   }
16017   return rc;
16018 }
16019
16020 /*
16021 ** Return TRUE if a semicolon occurs anywhere in the first N characters
16022 ** of string z[].
16023 */
16024 static int line_contains_semicolon(const char *z, int N){
16025   int i;
16026   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
16027   return 0;
16028 }
16029
16030 /*
16031 ** Test to see if a line consists entirely of whitespace.
16032 */
16033 static int _all_whitespace(const char *z){
16034   for(; *z; z++){
16035     if( IsSpace(z[0]) ) continue;
16036     if( *z=='/' && z[1]=='*' ){
16037       z += 2;
16038       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
16039       if( *z==0 ) return 0;
16040       z++;
16041       continue;
16042     }
16043     if( *z=='-' && z[1]=='-' ){
16044       z += 2;
16045       while( *z && *z!='\n' ){ z++; }
16046       if( *z==0 ) return 1;
16047       continue;
16048     }
16049     return 0;
16050   }
16051   return 1;
16052 }
16053
16054 /*
16055 ** Return TRUE if the line typed in is an SQL command terminator other
16056 ** than a semi-colon.  The SQL Server style "go" command is understood
16057 ** as is the Oracle "/".
16058 */
16059 static int line_is_command_terminator(const char *zLine){
16060   while( IsSpace(zLine[0]) ){ zLine++; };
16061   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
16062     return 1;  /* Oracle */
16063   }
16064   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
16065          && _all_whitespace(&zLine[2]) ){
16066     return 1;  /* SQL Server */
16067   }
16068   return 0;
16069 }
16070
16071 /*
16072 ** We need a default sqlite3_complete() implementation to use in case
16073 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
16074 ** any arbitrary text is a complete SQL statement.  This is not very
16075 ** user-friendly, but it does seem to work.
16076 */
16077 #ifdef SQLITE_OMIT_COMPLETE
16078 #define sqlite3_complete(x) 1
16079 #endif
16080
16081 /*
16082 ** Return true if zSql is a complete SQL statement.  Return false if it
16083 ** ends in the middle of a string literal or C-style comment.
16084 */
16085 static int line_is_complete(char *zSql, int nSql){
16086   int rc;
16087   if( zSql==0 ) return 1;
16088   zSql[nSql] = ';';
16089   zSql[nSql+1] = 0;
16090   rc = sqlite3_complete(zSql);
16091   zSql[nSql] = 0;
16092   return rc;
16093 }
16094
16095 /*
16096 ** Run a single line of SQL.  Return the number of errors.
16097 */
16098 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
16099   int rc;
16100   char *zErrMsg = 0;
16101
16102   open_db(p, 0);
16103   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
16104   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
16105   BEGIN_TIMER;
16106   rc = shell_exec(p, zSql, &zErrMsg);
16107   END_TIMER;
16108   if( rc || zErrMsg ){
16109     char zPrefix[100];
16110     if( in!=0 || !stdin_is_interactive ){
16111       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
16112                        "Error: near line %d:", startline);
16113     }else{
16114       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
16115     }
16116     if( zErrMsg!=0 ){
16117       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
16118       sqlite3_free(zErrMsg);
16119       zErrMsg = 0;
16120     }else{
16121       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
16122     }
16123     return 1;
16124   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
16125     raw_printf(p->out, "changes: %3d   total_changes: %d\n",
16126             sqlite3_changes(p->db), sqlite3_total_changes(p->db));
16127   }
16128   return 0;
16129 }
16130
16131
16132 /*
16133 ** Read input from *in and process it.  If *in==0 then input
16134 ** is interactive - the user is typing it it.  Otherwise, input
16135 ** is coming from a file or device.  A prompt is issued and history
16136 ** is saved only if input is interactive.  An interrupt signal will
16137 ** cause this routine to exit immediately, unless input is interactive.
16138 **
16139 ** Return the number of errors.
16140 */
16141 static int process_input(ShellState *p){
16142   char *zLine = 0;          /* A single input line */
16143   char *zSql = 0;           /* Accumulated SQL text */
16144   int nLine;                /* Length of current line */
16145   int nSql = 0;             /* Bytes of zSql[] used */
16146   int nAlloc = 0;           /* Allocated zSql[] space */
16147   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
16148   int rc;                   /* Error code */
16149   int errCnt = 0;           /* Number of errors seen */
16150   int startline = 0;        /* Line number for start of current input */
16151
16152   p->lineno = 0;
16153   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
16154     fflush(p->out);
16155     zLine = one_input_line(p->in, zLine, nSql>0);
16156     if( zLine==0 ){
16157       /* End of input */
16158       if( p->in==0 && stdin_is_interactive ) printf("\n");
16159       break;
16160     }
16161     if( seenInterrupt ){
16162       if( p->in!=0 ) break;
16163       seenInterrupt = 0;
16164     }
16165     p->lineno++;
16166     if( nSql==0 && _all_whitespace(zLine) ){
16167       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16168       continue;
16169     }
16170     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
16171       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16172       if( zLine[0]=='.' ){
16173         rc = do_meta_command(zLine, p);
16174         if( rc==2 ){ /* exit requested */
16175           break;
16176         }else if( rc ){
16177           errCnt++;
16178         }
16179       }
16180       continue;
16181     }
16182     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
16183       memcpy(zLine,";",2);
16184     }
16185     nLine = strlen30(zLine);
16186     if( nSql+nLine+2>=nAlloc ){
16187       nAlloc = nSql+nLine+100;
16188       zSql = realloc(zSql, nAlloc);
16189       if( zSql==0 ) shell_out_of_memory();
16190     }
16191     nSqlPrior = nSql;
16192     if( nSql==0 ){
16193       int i;
16194       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
16195       assert( nAlloc>0 && zSql!=0 );
16196       memcpy(zSql, zLine+i, nLine+1-i);
16197       startline = p->lineno;
16198       nSql = nLine-i;
16199     }else{
16200       zSql[nSql++] = '\n';
16201       memcpy(zSql+nSql, zLine, nLine+1);
16202       nSql += nLine;
16203     }
16204     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
16205                 && sqlite3_complete(zSql) ){
16206       errCnt += runOneSqlLine(p, zSql, p->in, startline);
16207       nSql = 0;
16208       if( p->outCount ){
16209         output_reset(p);
16210         p->outCount = 0;
16211       }else{
16212         clearTempFile(p);
16213       }
16214     }else if( nSql && _all_whitespace(zSql) ){
16215       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
16216       nSql = 0;
16217     }
16218   }
16219   if( nSql && !_all_whitespace(zSql) ){
16220     errCnt += runOneSqlLine(p, zSql, p->in, startline);
16221   }
16222   free(zSql);
16223   free(zLine);
16224   return errCnt>0;
16225 }
16226
16227 /*
16228 ** Return a pathname which is the user's home directory.  A
16229 ** 0 return indicates an error of some kind.
16230 */
16231 static char *find_home_dir(int clearFlag){
16232   static char *home_dir = NULL;
16233   if( clearFlag ){
16234     free(home_dir);
16235     home_dir = 0;
16236     return 0;
16237   }
16238   if( home_dir ) return home_dir;
16239
16240 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
16241      && !defined(__RTP__) && !defined(_WRS_KERNEL)
16242   {
16243     struct passwd *pwent;
16244     uid_t uid = getuid();
16245     if( (pwent=getpwuid(uid)) != NULL) {
16246       home_dir = pwent->pw_dir;
16247     }
16248   }
16249 #endif
16250
16251 #if defined(_WIN32_WCE)
16252   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
16253    */
16254   home_dir = "/";
16255 #else
16256
16257 #if defined(_WIN32) || defined(WIN32)
16258   if (!home_dir) {
16259     home_dir = getenv("USERPROFILE");
16260   }
16261 #endif
16262
16263   if (!home_dir) {
16264     home_dir = getenv("HOME");
16265   }
16266
16267 #if defined(_WIN32) || defined(WIN32)
16268   if (!home_dir) {
16269     char *zDrive, *zPath;
16270     int n;
16271     zDrive = getenv("HOMEDRIVE");
16272     zPath = getenv("HOMEPATH");
16273     if( zDrive && zPath ){
16274       n = strlen30(zDrive) + strlen30(zPath) + 1;
16275       home_dir = malloc( n );
16276       if( home_dir==0 ) return 0;
16277       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
16278       return home_dir;
16279     }
16280     home_dir = "c:\\";
16281   }
16282 #endif
16283
16284 #endif /* !_WIN32_WCE */
16285
16286   if( home_dir ){
16287     int n = strlen30(home_dir) + 1;
16288     char *z = malloc( n );
16289     if( z ) memcpy(z, home_dir, n);
16290     home_dir = z;
16291   }
16292
16293   return home_dir;
16294 }
16295
16296 /*
16297 ** Read input from the file given by sqliterc_override.  Or if that
16298 ** parameter is NULL, take input from ~/.sqliterc
16299 **
16300 ** Returns the number of errors.
16301 */
16302 static void process_sqliterc(
16303   ShellState *p,                  /* Configuration data */
16304   const char *sqliterc_override   /* Name of config file. NULL to use default */
16305 ){
16306   char *home_dir = NULL;
16307   const char *sqliterc = sqliterc_override;
16308   char *zBuf = 0;
16309   FILE *inSaved = p->in;
16310   int savedLineno = p->lineno;
16311
16312   if (sqliterc == NULL) {
16313     home_dir = find_home_dir(0);
16314     if( home_dir==0 ){
16315       raw_printf(stderr, "-- warning: cannot find home directory;"
16316                       " cannot read ~/.sqliterc\n");
16317       return;
16318     }
16319     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
16320     sqliterc = zBuf;
16321   }
16322   p->in = fopen(sqliterc,"rb");
16323   if( p->in ){
16324     if( stdin_is_interactive ){
16325       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
16326     }
16327     process_input(p);
16328     fclose(p->in);
16329   }
16330   p->in = inSaved;
16331   p->lineno = savedLineno;
16332   sqlite3_free(zBuf);
16333 }
16334
16335 /*
16336 ** Show available command line options
16337 */
16338 static const char zOptions[] =
16339 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
16340   "   -A ARGS...           run \".archive ARGS\" and exit\n"
16341 #endif
16342   "   -append              append the database to the end of the file\n"
16343   "   -ascii               set output mode to 'ascii'\n"
16344   "   -bail                stop after hitting an error\n"
16345   "   -batch               force batch I/O\n"
16346   "   -column              set output mode to 'column'\n"
16347   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
16348   "   -csv                 set output mode to 'csv'\n"
16349 #if defined(SQLITE_ENABLE_DESERIALIZE)
16350   "   -deserialize         open the database using sqlite3_deserialize()\n"
16351 #endif
16352   "   -echo                print commands before execution\n"
16353   "   -init FILENAME       read/process named file\n"
16354   "   -[no]header          turn headers on or off\n"
16355 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16356   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
16357 #endif
16358   "   -help                show this message\n"
16359   "   -html                set output mode to HTML\n"
16360   "   -interactive         force interactive I/O\n"
16361   "   -line                set output mode to 'line'\n"
16362   "   -list                set output mode to 'list'\n"
16363   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
16364 #if defined(SQLITE_ENABLE_DESERIALIZE)
16365   "   -maxsize N           maximum size for a --deserialize database\n"
16366 #endif
16367   "   -memtrace            trace all memory allocations and deallocations\n"
16368   "   -mmap N              default mmap size set to N\n"
16369 #ifdef SQLITE_ENABLE_MULTIPLEX
16370   "   -multiplex           enable the multiplexor VFS\n"
16371 #endif
16372   "   -newline SEP         set output row separator. Default: '\\n'\n"
16373   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
16374   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
16375   "   -quote               set output mode to 'quote'\n"
16376   "   -readonly            open the database read-only\n"
16377   "   -separator SEP       set output column separator. Default: '|'\n"
16378 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16379   "   -sorterref SIZE      sorter references threshold size\n"
16380 #endif
16381   "   -stats               print memory stats before each finalize\n"
16382   "   -version             show SQLite version\n"
16383   "   -vfs NAME            use NAME as the default VFS\n"
16384 #ifdef SQLITE_ENABLE_VFSTRACE
16385   "   -vfstrace            enable tracing of all VFS calls\n"
16386 #endif
16387 #ifdef SQLITE_HAVE_ZLIB
16388   "   -zip                 open the file as a ZIP Archive\n"
16389 #endif
16390 ;
16391 static void usage(int showDetail){
16392   utf8_printf(stderr,
16393       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
16394       "FILENAME is the name of an SQLite database. A new database is created\n"
16395       "if the file does not previously exist.\n", Argv0);
16396   if( showDetail ){
16397     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
16398   }else{
16399     raw_printf(stderr, "Use the -help option for additional information\n");
16400   }
16401   exit(1);
16402 }
16403
16404 /*
16405 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
16406 ** error message if it is initialized.
16407 */
16408 static void verify_uninitialized(void){
16409   if( sqlite3_config(-1)==SQLITE_MISUSE ){
16410     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
16411                         " initialization.\n");
16412   }
16413 }
16414
16415 /*
16416 ** Initialize the state information in data
16417 */
16418 static void main_init(ShellState *data) {
16419   memset(data, 0, sizeof(*data));
16420   data->normalMode = data->cMode = data->mode = MODE_List;
16421   data->autoExplain = 1;
16422   memcpy(data->colSeparator,SEP_Column, 2);
16423   memcpy(data->rowSeparator,SEP_Row, 2);
16424   data->showHeader = 0;
16425   data->shellFlgs = SHFLG_Lookaside;
16426   verify_uninitialized();
16427   sqlite3_config(SQLITE_CONFIG_URI, 1);
16428   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
16429   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
16430   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
16431   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
16432 }
16433
16434 /*
16435 ** Output text to the console in a font that attracts extra attention.
16436 */
16437 #ifdef _WIN32
16438 static void printBold(const char *zText){
16439   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
16440   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
16441   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
16442   SetConsoleTextAttribute(out,
16443          FOREGROUND_RED|FOREGROUND_INTENSITY
16444   );
16445   printf("%s", zText);
16446   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
16447 }
16448 #else
16449 static void printBold(const char *zText){
16450   printf("\033[1m%s\033[0m", zText);
16451 }
16452 #endif
16453
16454 /*
16455 ** Get the argument to an --option.  Throw an error and die if no argument
16456 ** is available.
16457 */
16458 static char *cmdline_option_value(int argc, char **argv, int i){
16459   if( i==argc ){
16460     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
16461             argv[0], argv[argc-1]);
16462     exit(1);
16463   }
16464   return argv[i];
16465 }
16466
16467 #ifndef SQLITE_SHELL_IS_UTF8
16468 #  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
16469 #    define SQLITE_SHELL_IS_UTF8          (0)
16470 #  else
16471 #    define SQLITE_SHELL_IS_UTF8          (1)
16472 #  endif
16473 #endif
16474
16475 #if SQLITE_SHELL_IS_UTF8
16476 int SQLITE_CDECL main(int argc, char **argv){
16477 #else
16478 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
16479   char **argv;
16480 #endif
16481   char *zErrMsg = 0;
16482   ShellState data;
16483   const char *zInitFile = 0;
16484   int i;
16485   int rc = 0;
16486   int warnInmemoryDb = 0;
16487   int readStdin = 1;
16488   int nCmd = 0;
16489   char **azCmd = 0;
16490   const char *zVfs = 0;           /* Value of -vfs command-line option */
16491 #if !SQLITE_SHELL_IS_UTF8
16492   char **argvToFree = 0;
16493   int argcToFree = 0;
16494 #endif
16495
16496   setBinaryMode(stdin, 0);
16497   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
16498   stdin_is_interactive = isatty(0);
16499   stdout_is_console = isatty(1);
16500
16501 #if !defined(_WIN32_WCE)
16502   if( getenv("SQLITE_DEBUG_BREAK") ){
16503     if( isatty(0) && isatty(2) ){
16504       fprintf(stderr,
16505           "attach debugger to process %d and press any key to continue.\n",
16506           GETPID());
16507       fgetc(stdin);
16508     }else{
16509 #if defined(_WIN32) || defined(WIN32)
16510       DebugBreak();
16511 #elif defined(SIGTRAP)
16512       raise(SIGTRAP);
16513 #endif
16514     }
16515   }
16516 #endif
16517
16518 #if USE_SYSTEM_SQLITE+0!=1
16519   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
16520     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
16521             sqlite3_sourceid(), SQLITE_SOURCE_ID);
16522     exit(1);
16523   }
16524 #endif
16525   main_init(&data);
16526
16527   /* On Windows, we must translate command-line arguments into UTF-8.
16528   ** The SQLite memory allocator subsystem has to be enabled in order to
16529   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
16530   ** subsequent sqlite3_config() calls will work.  So copy all results into
16531   ** memory that does not come from the SQLite memory allocator.
16532   */
16533 #if !SQLITE_SHELL_IS_UTF8
16534   sqlite3_initialize();
16535   argvToFree = malloc(sizeof(argv[0])*argc*2);
16536   argcToFree = argc;
16537   argv = argvToFree + argc;
16538   if( argv==0 ) shell_out_of_memory();
16539   for(i=0; i<argc; i++){
16540     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
16541     int n;
16542     if( z==0 ) shell_out_of_memory();
16543     n = (int)strlen(z);
16544     argv[i] = malloc( n+1 );
16545     if( argv[i]==0 ) shell_out_of_memory();
16546     memcpy(argv[i], z, n+1);
16547     argvToFree[i] = argv[i];
16548     sqlite3_free(z);
16549   }
16550   sqlite3_shutdown();
16551 #endif
16552
16553   assert( argc>=1 && argv && argv[0] );
16554   Argv0 = argv[0];
16555
16556   /* Make sure we have a valid signal handler early, before anything
16557   ** else is done.
16558   */
16559 #ifdef SIGINT
16560   signal(SIGINT, interrupt_handler);
16561 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
16562   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
16563 #endif
16564
16565 #ifdef SQLITE_SHELL_DBNAME_PROC
16566   {
16567     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
16568     ** of a C-function that will provide the name of the database file.  Use
16569     ** this compile-time option to embed this shell program in larger
16570     ** applications. */
16571     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
16572     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
16573     warnInmemoryDb = 0;
16574   }
16575 #endif
16576
16577   /* Do an initial pass through the command-line argument to locate
16578   ** the name of the database file, the name of the initialization file,
16579   ** the size of the alternative malloc heap,
16580   ** and the first command to execute.
16581   */
16582   verify_uninitialized();
16583   for(i=1; i<argc; i++){
16584     char *z;
16585     z = argv[i];
16586     if( z[0]!='-' ){
16587       if( data.zDbFilename==0 ){
16588         data.zDbFilename = z;
16589       }else{
16590         /* Excesss arguments are interpreted as SQL (or dot-commands) and
16591         ** mean that nothing is read from stdin */
16592         readStdin = 0;
16593         nCmd++;
16594         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
16595         if( azCmd==0 ) shell_out_of_memory();
16596         azCmd[nCmd-1] = z;
16597       }
16598     }
16599     if( z[1]=='-' ) z++;
16600     if( strcmp(z,"-separator")==0
16601      || strcmp(z,"-nullvalue")==0
16602      || strcmp(z,"-newline")==0
16603      || strcmp(z,"-cmd")==0
16604     ){
16605       (void)cmdline_option_value(argc, argv, ++i);
16606     }else if( strcmp(z,"-init")==0 ){
16607       zInitFile = cmdline_option_value(argc, argv, ++i);
16608     }else if( strcmp(z,"-batch")==0 ){
16609       /* Need to check for batch mode here to so we can avoid printing
16610       ** informational messages (like from process_sqliterc) before
16611       ** we do the actual processing of arguments later in a second pass.
16612       */
16613       stdin_is_interactive = 0;
16614     }else if( strcmp(z,"-heap")==0 ){
16615 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16616       const char *zSize;
16617       sqlite3_int64 szHeap;
16618
16619       zSize = cmdline_option_value(argc, argv, ++i);
16620       szHeap = integerValue(zSize);
16621       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
16622       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
16623 #else
16624       (void)cmdline_option_value(argc, argv, ++i);
16625 #endif
16626     }else if( strcmp(z,"-pagecache")==0 ){
16627       int n, sz;
16628       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16629       if( sz>70000 ) sz = 70000;
16630       if( sz<0 ) sz = 0;
16631       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16632       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
16633                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
16634       data.shellFlgs |= SHFLG_Pagecache;
16635     }else if( strcmp(z,"-lookaside")==0 ){
16636       int n, sz;
16637       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16638       if( sz<0 ) sz = 0;
16639       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16640       if( n<0 ) n = 0;
16641       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
16642       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
16643 #ifdef SQLITE_ENABLE_VFSTRACE
16644     }else if( strcmp(z,"-vfstrace")==0 ){
16645       extern int vfstrace_register(
16646          const char *zTraceName,
16647          const char *zOldVfsName,
16648          int (*xOut)(const char*,void*),
16649          void *pOutArg,
16650          int makeDefault
16651       );
16652       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
16653 #endif
16654 #ifdef SQLITE_ENABLE_MULTIPLEX
16655     }else if( strcmp(z,"-multiplex")==0 ){
16656       extern int sqlite3_multiple_initialize(const char*,int);
16657       sqlite3_multiplex_initialize(0, 1);
16658 #endif
16659     }else if( strcmp(z,"-mmap")==0 ){
16660       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16661       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
16662 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16663     }else if( strcmp(z,"-sorterref")==0 ){
16664       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16665       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
16666 #endif
16667     }else if( strcmp(z,"-vfs")==0 ){
16668       zVfs = cmdline_option_value(argc, argv, ++i);
16669 #ifdef SQLITE_HAVE_ZLIB
16670     }else if( strcmp(z,"-zip")==0 ){
16671       data.openMode = SHELL_OPEN_ZIPFILE;
16672 #endif
16673     }else if( strcmp(z,"-append")==0 ){
16674       data.openMode = SHELL_OPEN_APPENDVFS;
16675 #ifdef SQLITE_ENABLE_DESERIALIZE
16676     }else if( strcmp(z,"-deserialize")==0 ){
16677       data.openMode = SHELL_OPEN_DESERIALIZE;
16678     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16679       data.szMax = integerValue(argv[++i]);
16680 #endif
16681     }else if( strcmp(z,"-readonly")==0 ){
16682       data.openMode = SHELL_OPEN_READONLY;
16683 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16684     }else if( strncmp(z, "-A",2)==0 ){
16685       /* All remaining command-line arguments are passed to the ".archive"
16686       ** command, so ignore them */
16687       break;
16688 #endif
16689     }else if( strcmp(z, "-memtrace")==0 ){
16690       sqlite3MemTraceActivate(stderr);
16691     }
16692   }
16693   verify_uninitialized();
16694
16695
16696 #ifdef SQLITE_SHELL_INIT_PROC
16697   {
16698     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
16699     ** of a C-function that will perform initialization actions on SQLite that
16700     ** occur just before or after sqlite3_initialize(). Use this compile-time
16701     ** option to embed this shell program in larger applications. */
16702     extern void SQLITE_SHELL_INIT_PROC(void);
16703     SQLITE_SHELL_INIT_PROC();
16704   }
16705 #else
16706   /* All the sqlite3_config() calls have now been made. So it is safe
16707   ** to call sqlite3_initialize() and process any command line -vfs option. */
16708   sqlite3_initialize();
16709 #endif
16710
16711   if( zVfs ){
16712     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
16713     if( pVfs ){
16714       sqlite3_vfs_register(pVfs, 1);
16715     }else{
16716       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
16717       exit(1);
16718     }
16719   }
16720
16721   if( data.zDbFilename==0 ){
16722 #ifndef SQLITE_OMIT_MEMORYDB
16723     data.zDbFilename = ":memory:";
16724     warnInmemoryDb = argc==1;
16725 #else
16726     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
16727     return 1;
16728 #endif
16729   }
16730   data.out = stdout;
16731   sqlite3_appendvfs_init(0,0,0);
16732
16733   /* Go ahead and open the database file if it already exists.  If the
16734   ** file does not exist, delay opening it.  This prevents empty database
16735   ** files from being created if a user mistypes the database name argument
16736   ** to the sqlite command-line tool.
16737   */
16738   if( access(data.zDbFilename, 0)==0 ){
16739     open_db(&data, 0);
16740   }
16741
16742   /* Process the initialization file if there is one.  If no -init option
16743   ** is given on the command line, look for a file named ~/.sqliterc and
16744   ** try to process it.
16745   */
16746   process_sqliterc(&data,zInitFile);
16747
16748   /* Make a second pass through the command-line argument and set
16749   ** options.  This second pass is delayed until after the initialization
16750   ** file is processed so that the command-line arguments will override
16751   ** settings in the initialization file.
16752   */
16753   for(i=1; i<argc; i++){
16754     char *z = argv[i];
16755     if( z[0]!='-' ) continue;
16756     if( z[1]=='-' ){ z++; }
16757     if( strcmp(z,"-init")==0 ){
16758       i++;
16759     }else if( strcmp(z,"-html")==0 ){
16760       data.mode = MODE_Html;
16761     }else if( strcmp(z,"-list")==0 ){
16762       data.mode = MODE_List;
16763     }else if( strcmp(z,"-quote")==0 ){
16764       data.mode = MODE_Quote;
16765     }else if( strcmp(z,"-line")==0 ){
16766       data.mode = MODE_Line;
16767     }else if( strcmp(z,"-column")==0 ){
16768       data.mode = MODE_Column;
16769     }else if( strcmp(z,"-csv")==0 ){
16770       data.mode = MODE_Csv;
16771       memcpy(data.colSeparator,",",2);
16772 #ifdef SQLITE_HAVE_ZLIB
16773     }else if( strcmp(z,"-zip")==0 ){
16774       data.openMode = SHELL_OPEN_ZIPFILE;
16775 #endif
16776     }else if( strcmp(z,"-append")==0 ){
16777       data.openMode = SHELL_OPEN_APPENDVFS;
16778 #ifdef SQLITE_ENABLE_DESERIALIZE
16779     }else if( strcmp(z,"-deserialize")==0 ){
16780       data.openMode = SHELL_OPEN_DESERIALIZE;
16781     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16782       data.szMax = integerValue(argv[++i]);
16783 #endif
16784     }else if( strcmp(z,"-readonly")==0 ){
16785       data.openMode = SHELL_OPEN_READONLY;
16786     }else if( strcmp(z,"-ascii")==0 ){
16787       data.mode = MODE_Ascii;
16788       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16789                        SEP_Unit);
16790       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16791                        SEP_Record);
16792     }else if( strcmp(z,"-separator")==0 ){
16793       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16794                        "%s",cmdline_option_value(argc,argv,++i));
16795     }else if( strcmp(z,"-newline")==0 ){
16796       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16797                        "%s",cmdline_option_value(argc,argv,++i));
16798     }else if( strcmp(z,"-nullvalue")==0 ){
16799       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
16800                        "%s",cmdline_option_value(argc,argv,++i));
16801     }else if( strcmp(z,"-header")==0 ){
16802       data.showHeader = 1;
16803     }else if( strcmp(z,"-noheader")==0 ){
16804       data.showHeader = 0;
16805     }else if( strcmp(z,"-echo")==0 ){
16806       ShellSetFlag(&data, SHFLG_Echo);
16807     }else if( strcmp(z,"-eqp")==0 ){
16808       data.autoEQP = AUTOEQP_on;
16809     }else if( strcmp(z,"-eqpfull")==0 ){
16810       data.autoEQP = AUTOEQP_full;
16811     }else if( strcmp(z,"-stats")==0 ){
16812       data.statsOn = 1;
16813     }else if( strcmp(z,"-scanstats")==0 ){
16814       data.scanstatsOn = 1;
16815     }else if( strcmp(z,"-backslash")==0 ){
16816       /* Undocumented command-line option: -backslash
16817       ** Causes C-style backslash escapes to be evaluated in SQL statements
16818       ** prior to sending the SQL into SQLite.  Useful for injecting
16819       ** crazy bytes in the middle of SQL statements for testing and debugging.
16820       */
16821       ShellSetFlag(&data, SHFLG_Backslash);
16822     }else if( strcmp(z,"-bail")==0 ){
16823       bail_on_error = 1;
16824     }else if( strcmp(z,"-version")==0 ){
16825       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
16826       return 0;
16827     }else if( strcmp(z,"-interactive")==0 ){
16828       stdin_is_interactive = 1;
16829     }else if( strcmp(z,"-batch")==0 ){
16830       stdin_is_interactive = 0;
16831     }else if( strcmp(z,"-heap")==0 ){
16832       i++;
16833     }else if( strcmp(z,"-pagecache")==0 ){
16834       i+=2;
16835     }else if( strcmp(z,"-lookaside")==0 ){
16836       i+=2;
16837     }else if( strcmp(z,"-mmap")==0 ){
16838       i++;
16839     }else if( strcmp(z,"-memtrace")==0 ){
16840       i++;
16841 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16842     }else if( strcmp(z,"-sorterref")==0 ){
16843       i++;
16844 #endif
16845     }else if( strcmp(z,"-vfs")==0 ){
16846       i++;
16847 #ifdef SQLITE_ENABLE_VFSTRACE
16848     }else if( strcmp(z,"-vfstrace")==0 ){
16849       i++;
16850 #endif
16851 #ifdef SQLITE_ENABLE_MULTIPLEX
16852     }else if( strcmp(z,"-multiplex")==0 ){
16853       i++;
16854 #endif
16855     }else if( strcmp(z,"-help")==0 ){
16856       usage(1);
16857     }else if( strcmp(z,"-cmd")==0 ){
16858       /* Run commands that follow -cmd first and separately from commands
16859       ** that simply appear on the command-line.  This seems goofy.  It would
16860       ** be better if all commands ran in the order that they appear.  But
16861       ** we retain the goofy behavior for historical compatibility. */
16862       if( i==argc-1 ) break;
16863       z = cmdline_option_value(argc,argv,++i);
16864       if( z[0]=='.' ){
16865         rc = do_meta_command(z, &data);
16866         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
16867       }else{
16868         open_db(&data, 0);
16869         rc = shell_exec(&data, z, &zErrMsg);
16870         if( zErrMsg!=0 ){
16871           utf8_printf(stderr,"Error: %s\n", zErrMsg);
16872           if( bail_on_error ) return rc!=0 ? rc : 1;
16873         }else if( rc!=0 ){
16874           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
16875           if( bail_on_error ) return rc;
16876         }
16877       }
16878 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16879     }else if( strncmp(z, "-A", 2)==0 ){
16880       if( nCmd>0 ){
16881         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
16882                             " with \"%s\"\n", z);
16883         return 1;
16884       }
16885       open_db(&data, OPEN_DB_ZIPFILE);
16886       if( z[2] ){
16887         argv[i] = &z[2];
16888         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
16889       }else{
16890         arDotCommand(&data, 1, argv+i, argc-i);
16891       }
16892       readStdin = 0;
16893       break;
16894 #endif
16895     }else{
16896       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
16897       raw_printf(stderr,"Use -help for a list of options.\n");
16898       return 1;
16899     }
16900     data.cMode = data.mode;
16901   }
16902
16903   if( !readStdin ){
16904     /* Run all arguments that do not begin with '-' as if they were separate
16905     ** command-line inputs, except for the argToSkip argument which contains
16906     ** the database filename.
16907     */
16908     for(i=0; i<nCmd; i++){
16909       if( azCmd[i][0]=='.' ){
16910         rc = do_meta_command(azCmd[i], &data);
16911         if( rc ) return rc==2 ? 0 : rc;
16912       }else{
16913         open_db(&data, 0);
16914         rc = shell_exec(&data, azCmd[i], &zErrMsg);
16915         if( zErrMsg!=0 ){
16916           utf8_printf(stderr,"Error: %s\n", zErrMsg);
16917           return rc!=0 ? rc : 1;
16918         }else if( rc!=0 ){
16919           utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
16920           return rc;
16921         }
16922       }
16923     }
16924     free(azCmd);
16925   }else{
16926     /* Run commands received from standard input
16927     */
16928     if( stdin_is_interactive ){
16929       char *zHome;
16930       char *zHistory;
16931       int nHistory;
16932       printf(
16933         "SQLite version %s %.19s\n" /*extra-version-info*/
16934         "Enter \".help\" for usage hints.\n",
16935         sqlite3_libversion(), sqlite3_sourceid()
16936       );
16937       if( warnInmemoryDb ){
16938         printf("Connected to a ");
16939         printBold("transient in-memory database");
16940         printf(".\nUse \".open FILENAME\" to reopen on a "
16941                "persistent database.\n");
16942       }
16943       zHistory = getenv("SQLITE_HISTORY");
16944       if( zHistory ){
16945         zHistory = strdup(zHistory);
16946       }else if( (zHome = find_home_dir(0))!=0 ){
16947         nHistory = strlen30(zHome) + 20;
16948         if( (zHistory = malloc(nHistory))!=0 ){
16949           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
16950         }
16951       }
16952       if( zHistory ){ shell_read_history(zHistory); }
16953 #if HAVE_READLINE || HAVE_EDITLINE
16954       rl_attempted_completion_function = readline_completion;
16955 #elif HAVE_LINENOISE
16956       linenoiseSetCompletionCallback(linenoise_completion);
16957 #endif
16958       data.in = 0;
16959       rc = process_input(&data);
16960       if( zHistory ){
16961         shell_stifle_history(2000);
16962         shell_write_history(zHistory);
16963         free(zHistory);
16964       }
16965     }else{
16966       data.in = stdin;
16967       rc = process_input(&data);
16968     }
16969   }
16970   set_table_name(&data, 0);
16971   if( data.db ){
16972     session_close_all(&data);
16973     close_db(data.db);
16974   }
16975   sqlite3_free(data.zFreeOnClose);
16976   find_home_dir(1);
16977   output_reset(&data);
16978   data.doXdgOpen = 0;
16979   clearTempFile(&data);
16980 #if !SQLITE_SHELL_IS_UTF8
16981   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
16982   free(argvToFree);
16983 #endif
16984   /* Clear the global data structure so that valgrind will detect memory
16985   ** leaks */
16986   memset(&data, 0, sizeof(data));
16987   return rc;
16988 }