xref: /freebsd/contrib/sqlite3/shell.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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 /* #include "sqlite3.h" */
987 
988 /*
989 ** We need several things from the ANSI and MSVCRT headers.
990 */
991 
992 #include <stdio.h>
993 #include <stdlib.h>
994 #include <errno.h>
995 #include <io.h>
996 #include <limits.h>
997 #include <sys/types.h>
998 #include <sys/stat.h>
999 
1000 /*
1001 ** We may need several defines that should have been in "sys/stat.h".
1002 */
1003 
1004 #ifndef S_ISREG
1005 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1006 #endif
1007 
1008 #ifndef S_ISDIR
1009 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1010 #endif
1011 
1012 #ifndef S_ISLNK
1013 #define S_ISLNK(mode) (0)
1014 #endif
1015 
1016 /*
1017 ** We may need to provide the "mode_t" type.
1018 */
1019 
1020 #ifndef MODE_T_DEFINED
1021   #define MODE_T_DEFINED
1022   typedef unsigned short mode_t;
1023 #endif
1024 
1025 /*
1026 ** We may need to provide the "ino_t" type.
1027 */
1028 
1029 #ifndef INO_T_DEFINED
1030   #define INO_T_DEFINED
1031   typedef unsigned short ino_t;
1032 #endif
1033 
1034 /*
1035 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1036 */
1037 
1038 #ifndef NAME_MAX
1039 #  ifdef FILENAME_MAX
1040 #    define NAME_MAX (FILENAME_MAX)
1041 #  else
1042 #    define NAME_MAX (260)
1043 #  endif
1044 #endif
1045 
1046 /*
1047 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1048 */
1049 
1050 #ifndef NULL_INTPTR_T
1051 #  define NULL_INTPTR_T ((intptr_t)(0))
1052 #endif
1053 
1054 #ifndef BAD_INTPTR_T
1055 #  define BAD_INTPTR_T ((intptr_t)(-1))
1056 #endif
1057 
1058 /*
1059 ** We need to provide the necessary structures and related types.
1060 */
1061 
1062 #ifndef DIRENT_DEFINED
1063 #define DIRENT_DEFINED
1064 typedef struct DIRENT DIRENT;
1065 typedef DIRENT *LPDIRENT;
1066 struct DIRENT {
1067   ino_t d_ino;               /* Sequence number, do not use. */
1068   unsigned d_attributes;     /* Win32 file attributes. */
1069   char d_name[NAME_MAX + 1]; /* Name within the directory. */
1070 };
1071 #endif
1072 
1073 #ifndef DIR_DEFINED
1074 #define DIR_DEFINED
1075 typedef struct DIR DIR;
1076 typedef DIR *LPDIR;
1077 struct DIR {
1078   intptr_t d_handle; /* Value returned by "_findfirst". */
1079   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1080   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1081 };
1082 #endif
1083 
1084 /*
1085 ** Provide a macro, for use by the implementation, to determine if a
1086 ** particular directory entry should be skipped over when searching for
1087 ** the next directory entry that should be returned by the readdir() or
1088 ** readdir_r() functions.
1089 */
1090 
1091 #ifndef is_filtered
1092 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1093 #endif
1094 
1095 /*
1096 ** Provide the function prototype for the POSIX compatiable getenv()
1097 ** function.  This function is not thread-safe.
1098 */
1099 
1100 extern const char *windirent_getenv(const char *name);
1101 
1102 /*
1103 ** Finally, we can provide the function prototypes for the opendir(),
1104 ** readdir(), readdir_r(), and closedir() POSIX functions.
1105 */
1106 
1107 extern LPDIR opendir(const char *dirname);
1108 extern LPDIRENT readdir(LPDIR dirp);
1109 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1110 extern INT closedir(LPDIR dirp);
1111 
1112 #endif /* defined(WIN32) && defined(_MSC_VER) */
1113 
1114 /************************* End test_windirent.h ********************/
1115 /************************* Begin test_windirent.c ******************/
1116 /*
1117 ** 2015 November 30
1118 **
1119 ** The author disclaims copyright to this source code.  In place of
1120 ** a legal notice, here is a blessing:
1121 **
1122 **    May you do good and not evil.
1123 **    May you find forgiveness for yourself and forgive others.
1124 **    May you share freely, never taking more than you give.
1125 **
1126 *************************************************************************
1127 ** This file contains code to implement most of the opendir() family of
1128 ** POSIX functions on Win32 using the MSVCRT.
1129 */
1130 
1131 #if defined(_WIN32) && defined(_MSC_VER)
1132 /* #include "test_windirent.h" */
1133 
1134 /*
1135 ** Implementation of the POSIX getenv() function using the Win32 API.
1136 ** This function is not thread-safe.
1137 */
1138 const char *windirent_getenv(
1139   const char *name
1140 ){
1141   static char value[32768]; /* Maximum length, per MSDN */
1142   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1143   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1144 
1145   memset(value, 0, sizeof(value));
1146   dwRet = GetEnvironmentVariableA(name, value, dwSize);
1147   if( dwRet==0 || dwRet>dwSize ){
1148     /*
1149     ** The function call to GetEnvironmentVariableA() failed -OR-
1150     ** the buffer is not large enough.  Either way, return NULL.
1151     */
1152     return 0;
1153   }else{
1154     /*
1155     ** The function call to GetEnvironmentVariableA() succeeded
1156     ** -AND- the buffer contains the entire value.
1157     */
1158     return value;
1159   }
1160 }
1161 
1162 /*
1163 ** Implementation of the POSIX opendir() function using the MSVCRT.
1164 */
1165 LPDIR opendir(
1166   const char *dirname
1167 ){
1168   struct _finddata_t data;
1169   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1170   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1171 
1172   if( dirp==NULL ) return NULL;
1173   memset(dirp, 0, sizeof(DIR));
1174 
1175   /* TODO: Remove this if Unix-style root paths are not used. */
1176   if( sqlite3_stricmp(dirname, "/")==0 ){
1177     dirname = windirent_getenv("SystemDrive");
1178   }
1179 
1180   memset(&data, 0, sizeof(struct _finddata_t));
1181   _snprintf(data.name, namesize, "%s\\*", dirname);
1182   dirp->d_handle = _findfirst(data.name, &data);
1183 
1184   if( dirp->d_handle==BAD_INTPTR_T ){
1185     closedir(dirp);
1186     return NULL;
1187   }
1188 
1189   /* TODO: Remove this block to allow hidden and/or system files. */
1190   if( is_filtered(data) ){
1191 next:
1192 
1193     memset(&data, 0, sizeof(struct _finddata_t));
1194     if( _findnext(dirp->d_handle, &data)==-1 ){
1195       closedir(dirp);
1196       return NULL;
1197     }
1198 
1199     /* TODO: Remove this block to allow hidden and/or system files. */
1200     if( is_filtered(data) ) goto next;
1201   }
1202 
1203   dirp->d_first.d_attributes = data.attrib;
1204   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1205   dirp->d_first.d_name[NAME_MAX] = '\0';
1206 
1207   return dirp;
1208 }
1209 
1210 /*
1211 ** Implementation of the POSIX readdir() function using the MSVCRT.
1212 */
1213 LPDIRENT readdir(
1214   LPDIR dirp
1215 ){
1216   struct _finddata_t data;
1217 
1218   if( dirp==NULL ) return NULL;
1219 
1220   if( dirp->d_first.d_ino==0 ){
1221     dirp->d_first.d_ino++;
1222     dirp->d_next.d_ino++;
1223 
1224     return &dirp->d_first;
1225   }
1226 
1227 next:
1228 
1229   memset(&data, 0, sizeof(struct _finddata_t));
1230   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1231 
1232   /* TODO: Remove this block to allow hidden and/or system files. */
1233   if( is_filtered(data) ) goto next;
1234 
1235   dirp->d_next.d_ino++;
1236   dirp->d_next.d_attributes = data.attrib;
1237   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1238   dirp->d_next.d_name[NAME_MAX] = '\0';
1239 
1240   return &dirp->d_next;
1241 }
1242 
1243 /*
1244 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1245 */
1246 INT readdir_r(
1247   LPDIR dirp,
1248   LPDIRENT entry,
1249   LPDIRENT *result
1250 ){
1251   struct _finddata_t data;
1252 
1253   if( dirp==NULL ) return EBADF;
1254 
1255   if( dirp->d_first.d_ino==0 ){
1256     dirp->d_first.d_ino++;
1257     dirp->d_next.d_ino++;
1258 
1259     entry->d_ino = dirp->d_first.d_ino;
1260     entry->d_attributes = dirp->d_first.d_attributes;
1261     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1262     entry->d_name[NAME_MAX] = '\0';
1263 
1264     *result = entry;
1265     return 0;
1266   }
1267 
1268 next:
1269 
1270   memset(&data, 0, sizeof(struct _finddata_t));
1271   if( _findnext(dirp->d_handle, &data)==-1 ){
1272     *result = NULL;
1273     return ENOENT;
1274   }
1275 
1276   /* TODO: Remove this block to allow hidden and/or system files. */
1277   if( is_filtered(data) ) goto next;
1278 
1279   entry->d_ino = (ino_t)-1; /* not available */
1280   entry->d_attributes = data.attrib;
1281   strncpy(entry->d_name, data.name, NAME_MAX);
1282   entry->d_name[NAME_MAX] = '\0';
1283 
1284   *result = entry;
1285   return 0;
1286 }
1287 
1288 /*
1289 ** Implementation of the POSIX closedir() function using the MSVCRT.
1290 */
1291 INT closedir(
1292   LPDIR dirp
1293 ){
1294   INT result = 0;
1295 
1296   if( dirp==NULL ) return EINVAL;
1297 
1298   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1299     result = _findclose(dirp->d_handle);
1300   }
1301 
1302   sqlite3_free(dirp);
1303   return result;
1304 }
1305 
1306 #endif /* defined(WIN32) && defined(_MSC_VER) */
1307 
1308 /************************* End test_windirent.c ********************/
1309 #define dirent DIRENT
1310 #endif
1311 /************************* Begin ../ext/misc/shathree.c ******************/
1312 /*
1313 ** 2017-03-08
1314 **
1315 ** The author disclaims copyright to this source code.  In place of
1316 ** a legal notice, here is a blessing:
1317 **
1318 **    May you do good and not evil.
1319 **    May you find forgiveness for yourself and forgive others.
1320 **    May you share freely, never taking more than you give.
1321 **
1322 ******************************************************************************
1323 **
1324 ** This SQLite extension implements functions that compute SHA3 hashes.
1325 ** Two SQL functions are implemented:
1326 **
1327 **     sha3(X,SIZE)
1328 **     sha3_query(Y,SIZE)
1329 **
1330 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1331 ** X is NULL.
1332 **
1333 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1334 ** and returns a hash of their results.
1335 **
1336 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1337 ** is used.  If SIZE is included it must be one of the integers 224, 256,
1338 ** 384, or 512, to determine SHA3 hash variant that is computed.
1339 */
1340 /* #include "sqlite3ext.h" */
1341 SQLITE_EXTENSION_INIT1
1342 #include <assert.h>
1343 #include <string.h>
1344 #include <stdarg.h>
1345 /* typedef sqlite3_uint64 u64; */
1346 
1347 /******************************************************************************
1348 ** The Hash Engine
1349 */
1350 /*
1351 ** Macros to determine whether the machine is big or little endian,
1352 ** and whether or not that determination is run-time or compile-time.
1353 **
1354 ** For best performance, an attempt is made to guess at the byte-order
1355 ** using C-preprocessor macros.  If that is unsuccessful, or if
1356 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1357 ** at run-time.
1358 */
1359 #ifndef SHA3_BYTEORDER
1360 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1361      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1362      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1363      defined(__arm__)
1364 #   define SHA3_BYTEORDER    1234
1365 # elif defined(sparc)    || defined(__ppc__)
1366 #   define SHA3_BYTEORDER    4321
1367 # else
1368 #   define SHA3_BYTEORDER 0
1369 # endif
1370 #endif
1371 
1372 
1373 /*
1374 ** State structure for a SHA3 hash in progress
1375 */
1376 typedef struct SHA3Context SHA3Context;
1377 struct SHA3Context {
1378   union {
1379     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1380     unsigned char x[1600];    /* ... or 1600 bytes */
1381   } u;
1382   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1383   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1384   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1385 };
1386 
1387 /*
1388 ** A single step of the Keccak mixing function for a 1600-bit state
1389 */
1390 static void KeccakF1600Step(SHA3Context *p){
1391   int i;
1392   u64 b0, b1, b2, b3, b4;
1393   u64 c0, c1, c2, c3, c4;
1394   u64 d0, d1, d2, d3, d4;
1395   static const u64 RC[] = {
1396     0x0000000000000001ULL,  0x0000000000008082ULL,
1397     0x800000000000808aULL,  0x8000000080008000ULL,
1398     0x000000000000808bULL,  0x0000000080000001ULL,
1399     0x8000000080008081ULL,  0x8000000000008009ULL,
1400     0x000000000000008aULL,  0x0000000000000088ULL,
1401     0x0000000080008009ULL,  0x000000008000000aULL,
1402     0x000000008000808bULL,  0x800000000000008bULL,
1403     0x8000000000008089ULL,  0x8000000000008003ULL,
1404     0x8000000000008002ULL,  0x8000000000000080ULL,
1405     0x000000000000800aULL,  0x800000008000000aULL,
1406     0x8000000080008081ULL,  0x8000000000008080ULL,
1407     0x0000000080000001ULL,  0x8000000080008008ULL
1408   };
1409 # define a00 (p->u.s[0])
1410 # define a01 (p->u.s[1])
1411 # define a02 (p->u.s[2])
1412 # define a03 (p->u.s[3])
1413 # define a04 (p->u.s[4])
1414 # define a10 (p->u.s[5])
1415 # define a11 (p->u.s[6])
1416 # define a12 (p->u.s[7])
1417 # define a13 (p->u.s[8])
1418 # define a14 (p->u.s[9])
1419 # define a20 (p->u.s[10])
1420 # define a21 (p->u.s[11])
1421 # define a22 (p->u.s[12])
1422 # define a23 (p->u.s[13])
1423 # define a24 (p->u.s[14])
1424 # define a30 (p->u.s[15])
1425 # define a31 (p->u.s[16])
1426 # define a32 (p->u.s[17])
1427 # define a33 (p->u.s[18])
1428 # define a34 (p->u.s[19])
1429 # define a40 (p->u.s[20])
1430 # define a41 (p->u.s[21])
1431 # define a42 (p->u.s[22])
1432 # define a43 (p->u.s[23])
1433 # define a44 (p->u.s[24])
1434 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1435 
1436   for(i=0; i<24; i+=4){
1437     c0 = a00^a10^a20^a30^a40;
1438     c1 = a01^a11^a21^a31^a41;
1439     c2 = a02^a12^a22^a32^a42;
1440     c3 = a03^a13^a23^a33^a43;
1441     c4 = a04^a14^a24^a34^a44;
1442     d0 = c4^ROL64(c1, 1);
1443     d1 = c0^ROL64(c2, 1);
1444     d2 = c1^ROL64(c3, 1);
1445     d3 = c2^ROL64(c4, 1);
1446     d4 = c3^ROL64(c0, 1);
1447 
1448     b0 = (a00^d0);
1449     b1 = ROL64((a11^d1), 44);
1450     b2 = ROL64((a22^d2), 43);
1451     b3 = ROL64((a33^d3), 21);
1452     b4 = ROL64((a44^d4), 14);
1453     a00 =   b0 ^((~b1)&  b2 );
1454     a00 ^= RC[i];
1455     a11 =   b1 ^((~b2)&  b3 );
1456     a22 =   b2 ^((~b3)&  b4 );
1457     a33 =   b3 ^((~b4)&  b0 );
1458     a44 =   b4 ^((~b0)&  b1 );
1459 
1460     b2 = ROL64((a20^d0), 3);
1461     b3 = ROL64((a31^d1), 45);
1462     b4 = ROL64((a42^d2), 61);
1463     b0 = ROL64((a03^d3), 28);
1464     b1 = ROL64((a14^d4), 20);
1465     a20 =   b0 ^((~b1)&  b2 );
1466     a31 =   b1 ^((~b2)&  b3 );
1467     a42 =   b2 ^((~b3)&  b4 );
1468     a03 =   b3 ^((~b4)&  b0 );
1469     a14 =   b4 ^((~b0)&  b1 );
1470 
1471     b4 = ROL64((a40^d0), 18);
1472     b0 = ROL64((a01^d1), 1);
1473     b1 = ROL64((a12^d2), 6);
1474     b2 = ROL64((a23^d3), 25);
1475     b3 = ROL64((a34^d4), 8);
1476     a40 =   b0 ^((~b1)&  b2 );
1477     a01 =   b1 ^((~b2)&  b3 );
1478     a12 =   b2 ^((~b3)&  b4 );
1479     a23 =   b3 ^((~b4)&  b0 );
1480     a34 =   b4 ^((~b0)&  b1 );
1481 
1482     b1 = ROL64((a10^d0), 36);
1483     b2 = ROL64((a21^d1), 10);
1484     b3 = ROL64((a32^d2), 15);
1485     b4 = ROL64((a43^d3), 56);
1486     b0 = ROL64((a04^d4), 27);
1487     a10 =   b0 ^((~b1)&  b2 );
1488     a21 =   b1 ^((~b2)&  b3 );
1489     a32 =   b2 ^((~b3)&  b4 );
1490     a43 =   b3 ^((~b4)&  b0 );
1491     a04 =   b4 ^((~b0)&  b1 );
1492 
1493     b3 = ROL64((a30^d0), 41);
1494     b4 = ROL64((a41^d1), 2);
1495     b0 = ROL64((a02^d2), 62);
1496     b1 = ROL64((a13^d3), 55);
1497     b2 = ROL64((a24^d4), 39);
1498     a30 =   b0 ^((~b1)&  b2 );
1499     a41 =   b1 ^((~b2)&  b3 );
1500     a02 =   b2 ^((~b3)&  b4 );
1501     a13 =   b3 ^((~b4)&  b0 );
1502     a24 =   b4 ^((~b0)&  b1 );
1503 
1504     c0 = a00^a20^a40^a10^a30;
1505     c1 = a11^a31^a01^a21^a41;
1506     c2 = a22^a42^a12^a32^a02;
1507     c3 = a33^a03^a23^a43^a13;
1508     c4 = a44^a14^a34^a04^a24;
1509     d0 = c4^ROL64(c1, 1);
1510     d1 = c0^ROL64(c2, 1);
1511     d2 = c1^ROL64(c3, 1);
1512     d3 = c2^ROL64(c4, 1);
1513     d4 = c3^ROL64(c0, 1);
1514 
1515     b0 = (a00^d0);
1516     b1 = ROL64((a31^d1), 44);
1517     b2 = ROL64((a12^d2), 43);
1518     b3 = ROL64((a43^d3), 21);
1519     b4 = ROL64((a24^d4), 14);
1520     a00 =   b0 ^((~b1)&  b2 );
1521     a00 ^= RC[i+1];
1522     a31 =   b1 ^((~b2)&  b3 );
1523     a12 =   b2 ^((~b3)&  b4 );
1524     a43 =   b3 ^((~b4)&  b0 );
1525     a24 =   b4 ^((~b0)&  b1 );
1526 
1527     b2 = ROL64((a40^d0), 3);
1528     b3 = ROL64((a21^d1), 45);
1529     b4 = ROL64((a02^d2), 61);
1530     b0 = ROL64((a33^d3), 28);
1531     b1 = ROL64((a14^d4), 20);
1532     a40 =   b0 ^((~b1)&  b2 );
1533     a21 =   b1 ^((~b2)&  b3 );
1534     a02 =   b2 ^((~b3)&  b4 );
1535     a33 =   b3 ^((~b4)&  b0 );
1536     a14 =   b4 ^((~b0)&  b1 );
1537 
1538     b4 = ROL64((a30^d0), 18);
1539     b0 = ROL64((a11^d1), 1);
1540     b1 = ROL64((a42^d2), 6);
1541     b2 = ROL64((a23^d3), 25);
1542     b3 = ROL64((a04^d4), 8);
1543     a30 =   b0 ^((~b1)&  b2 );
1544     a11 =   b1 ^((~b2)&  b3 );
1545     a42 =   b2 ^((~b3)&  b4 );
1546     a23 =   b3 ^((~b4)&  b0 );
1547     a04 =   b4 ^((~b0)&  b1 );
1548 
1549     b1 = ROL64((a20^d0), 36);
1550     b2 = ROL64((a01^d1), 10);
1551     b3 = ROL64((a32^d2), 15);
1552     b4 = ROL64((a13^d3), 56);
1553     b0 = ROL64((a44^d4), 27);
1554     a20 =   b0 ^((~b1)&  b2 );
1555     a01 =   b1 ^((~b2)&  b3 );
1556     a32 =   b2 ^((~b3)&  b4 );
1557     a13 =   b3 ^((~b4)&  b0 );
1558     a44 =   b4 ^((~b0)&  b1 );
1559 
1560     b3 = ROL64((a10^d0), 41);
1561     b4 = ROL64((a41^d1), 2);
1562     b0 = ROL64((a22^d2), 62);
1563     b1 = ROL64((a03^d3), 55);
1564     b2 = ROL64((a34^d4), 39);
1565     a10 =   b0 ^((~b1)&  b2 );
1566     a41 =   b1 ^((~b2)&  b3 );
1567     a22 =   b2 ^((~b3)&  b4 );
1568     a03 =   b3 ^((~b4)&  b0 );
1569     a34 =   b4 ^((~b0)&  b1 );
1570 
1571     c0 = a00^a40^a30^a20^a10;
1572     c1 = a31^a21^a11^a01^a41;
1573     c2 = a12^a02^a42^a32^a22;
1574     c3 = a43^a33^a23^a13^a03;
1575     c4 = a24^a14^a04^a44^a34;
1576     d0 = c4^ROL64(c1, 1);
1577     d1 = c0^ROL64(c2, 1);
1578     d2 = c1^ROL64(c3, 1);
1579     d3 = c2^ROL64(c4, 1);
1580     d4 = c3^ROL64(c0, 1);
1581 
1582     b0 = (a00^d0);
1583     b1 = ROL64((a21^d1), 44);
1584     b2 = ROL64((a42^d2), 43);
1585     b3 = ROL64((a13^d3), 21);
1586     b4 = ROL64((a34^d4), 14);
1587     a00 =   b0 ^((~b1)&  b2 );
1588     a00 ^= RC[i+2];
1589     a21 =   b1 ^((~b2)&  b3 );
1590     a42 =   b2 ^((~b3)&  b4 );
1591     a13 =   b3 ^((~b4)&  b0 );
1592     a34 =   b4 ^((~b0)&  b1 );
1593 
1594     b2 = ROL64((a30^d0), 3);
1595     b3 = ROL64((a01^d1), 45);
1596     b4 = ROL64((a22^d2), 61);
1597     b0 = ROL64((a43^d3), 28);
1598     b1 = ROL64((a14^d4), 20);
1599     a30 =   b0 ^((~b1)&  b2 );
1600     a01 =   b1 ^((~b2)&  b3 );
1601     a22 =   b2 ^((~b3)&  b4 );
1602     a43 =   b3 ^((~b4)&  b0 );
1603     a14 =   b4 ^((~b0)&  b1 );
1604 
1605     b4 = ROL64((a10^d0), 18);
1606     b0 = ROL64((a31^d1), 1);
1607     b1 = ROL64((a02^d2), 6);
1608     b2 = ROL64((a23^d3), 25);
1609     b3 = ROL64((a44^d4), 8);
1610     a10 =   b0 ^((~b1)&  b2 );
1611     a31 =   b1 ^((~b2)&  b3 );
1612     a02 =   b2 ^((~b3)&  b4 );
1613     a23 =   b3 ^((~b4)&  b0 );
1614     a44 =   b4 ^((~b0)&  b1 );
1615 
1616     b1 = ROL64((a40^d0), 36);
1617     b2 = ROL64((a11^d1), 10);
1618     b3 = ROL64((a32^d2), 15);
1619     b4 = ROL64((a03^d3), 56);
1620     b0 = ROL64((a24^d4), 27);
1621     a40 =   b0 ^((~b1)&  b2 );
1622     a11 =   b1 ^((~b2)&  b3 );
1623     a32 =   b2 ^((~b3)&  b4 );
1624     a03 =   b3 ^((~b4)&  b0 );
1625     a24 =   b4 ^((~b0)&  b1 );
1626 
1627     b3 = ROL64((a20^d0), 41);
1628     b4 = ROL64((a41^d1), 2);
1629     b0 = ROL64((a12^d2), 62);
1630     b1 = ROL64((a33^d3), 55);
1631     b2 = ROL64((a04^d4), 39);
1632     a20 =   b0 ^((~b1)&  b2 );
1633     a41 =   b1 ^((~b2)&  b3 );
1634     a12 =   b2 ^((~b3)&  b4 );
1635     a33 =   b3 ^((~b4)&  b0 );
1636     a04 =   b4 ^((~b0)&  b1 );
1637 
1638     c0 = a00^a30^a10^a40^a20;
1639     c1 = a21^a01^a31^a11^a41;
1640     c2 = a42^a22^a02^a32^a12;
1641     c3 = a13^a43^a23^a03^a33;
1642     c4 = a34^a14^a44^a24^a04;
1643     d0 = c4^ROL64(c1, 1);
1644     d1 = c0^ROL64(c2, 1);
1645     d2 = c1^ROL64(c3, 1);
1646     d3 = c2^ROL64(c4, 1);
1647     d4 = c3^ROL64(c0, 1);
1648 
1649     b0 = (a00^d0);
1650     b1 = ROL64((a01^d1), 44);
1651     b2 = ROL64((a02^d2), 43);
1652     b3 = ROL64((a03^d3), 21);
1653     b4 = ROL64((a04^d4), 14);
1654     a00 =   b0 ^((~b1)&  b2 );
1655     a00 ^= RC[i+3];
1656     a01 =   b1 ^((~b2)&  b3 );
1657     a02 =   b2 ^((~b3)&  b4 );
1658     a03 =   b3 ^((~b4)&  b0 );
1659     a04 =   b4 ^((~b0)&  b1 );
1660 
1661     b2 = ROL64((a10^d0), 3);
1662     b3 = ROL64((a11^d1), 45);
1663     b4 = ROL64((a12^d2), 61);
1664     b0 = ROL64((a13^d3), 28);
1665     b1 = ROL64((a14^d4), 20);
1666     a10 =   b0 ^((~b1)&  b2 );
1667     a11 =   b1 ^((~b2)&  b3 );
1668     a12 =   b2 ^((~b3)&  b4 );
1669     a13 =   b3 ^((~b4)&  b0 );
1670     a14 =   b4 ^((~b0)&  b1 );
1671 
1672     b4 = ROL64((a20^d0), 18);
1673     b0 = ROL64((a21^d1), 1);
1674     b1 = ROL64((a22^d2), 6);
1675     b2 = ROL64((a23^d3), 25);
1676     b3 = ROL64((a24^d4), 8);
1677     a20 =   b0 ^((~b1)&  b2 );
1678     a21 =   b1 ^((~b2)&  b3 );
1679     a22 =   b2 ^((~b3)&  b4 );
1680     a23 =   b3 ^((~b4)&  b0 );
1681     a24 =   b4 ^((~b0)&  b1 );
1682 
1683     b1 = ROL64((a30^d0), 36);
1684     b2 = ROL64((a31^d1), 10);
1685     b3 = ROL64((a32^d2), 15);
1686     b4 = ROL64((a33^d3), 56);
1687     b0 = ROL64((a34^d4), 27);
1688     a30 =   b0 ^((~b1)&  b2 );
1689     a31 =   b1 ^((~b2)&  b3 );
1690     a32 =   b2 ^((~b3)&  b4 );
1691     a33 =   b3 ^((~b4)&  b0 );
1692     a34 =   b4 ^((~b0)&  b1 );
1693 
1694     b3 = ROL64((a40^d0), 41);
1695     b4 = ROL64((a41^d1), 2);
1696     b0 = ROL64((a42^d2), 62);
1697     b1 = ROL64((a43^d3), 55);
1698     b2 = ROL64((a44^d4), 39);
1699     a40 =   b0 ^((~b1)&  b2 );
1700     a41 =   b1 ^((~b2)&  b3 );
1701     a42 =   b2 ^((~b3)&  b4 );
1702     a43 =   b3 ^((~b4)&  b0 );
1703     a44 =   b4 ^((~b0)&  b1 );
1704   }
1705 }
1706 
1707 /*
1708 ** Initialize a new hash.  iSize determines the size of the hash
1709 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1710 ** can be zero to use the default hash size of 256 bits.
1711 */
1712 static void SHA3Init(SHA3Context *p, int iSize){
1713   memset(p, 0, sizeof(*p));
1714   if( iSize>=128 && iSize<=512 ){
1715     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1716   }else{
1717     p->nRate = (1600 - 2*256)/8;
1718   }
1719 #if SHA3_BYTEORDER==1234
1720   /* Known to be little-endian at compile-time. No-op */
1721 #elif SHA3_BYTEORDER==4321
1722   p->ixMask = 7;  /* Big-endian */
1723 #else
1724   {
1725     static unsigned int one = 1;
1726     if( 1==*(unsigned char*)&one ){
1727       /* Little endian.  No byte swapping. */
1728       p->ixMask = 0;
1729     }else{
1730       /* Big endian.  Byte swap. */
1731       p->ixMask = 7;
1732     }
1733   }
1734 #endif
1735 }
1736 
1737 /*
1738 ** Make consecutive calls to the SHA3Update function to add new content
1739 ** to the hash
1740 */
1741 static void SHA3Update(
1742   SHA3Context *p,
1743   const unsigned char *aData,
1744   unsigned int nData
1745 ){
1746   unsigned int i = 0;
1747 #if SHA3_BYTEORDER==1234
1748   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1749     for(; i+7<nData; i+=8){
1750       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1751       p->nLoaded += 8;
1752       if( p->nLoaded>=p->nRate ){
1753         KeccakF1600Step(p);
1754         p->nLoaded = 0;
1755       }
1756     }
1757   }
1758 #endif
1759   for(; i<nData; i++){
1760 #if SHA3_BYTEORDER==1234
1761     p->u.x[p->nLoaded] ^= aData[i];
1762 #elif SHA3_BYTEORDER==4321
1763     p->u.x[p->nLoaded^0x07] ^= aData[i];
1764 #else
1765     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1766 #endif
1767     p->nLoaded++;
1768     if( p->nLoaded==p->nRate ){
1769       KeccakF1600Step(p);
1770       p->nLoaded = 0;
1771     }
1772   }
1773 }
1774 
1775 /*
1776 ** After all content has been added, invoke SHA3Final() to compute
1777 ** the final hash.  The function returns a pointer to the binary
1778 ** hash value.
1779 */
1780 static unsigned char *SHA3Final(SHA3Context *p){
1781   unsigned int i;
1782   if( p->nLoaded==p->nRate-1 ){
1783     const unsigned char c1 = 0x86;
1784     SHA3Update(p, &c1, 1);
1785   }else{
1786     const unsigned char c2 = 0x06;
1787     const unsigned char c3 = 0x80;
1788     SHA3Update(p, &c2, 1);
1789     p->nLoaded = p->nRate - 1;
1790     SHA3Update(p, &c3, 1);
1791   }
1792   for(i=0; i<p->nRate; i++){
1793     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1794   }
1795   return &p->u.x[p->nRate];
1796 }
1797 /* End of the hashing logic
1798 *****************************************************************************/
1799 
1800 /*
1801 ** Implementation of the sha3(X,SIZE) function.
1802 **
1803 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1804 ** size is 256.  If X is a BLOB, it is hashed as is.
1805 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1806 ** and the string is hashed without the trailing 0x00 terminator.  The hash
1807 ** of a NULL value is NULL.
1808 */
1809 static void sha3Func(
1810   sqlite3_context *context,
1811   int argc,
1812   sqlite3_value **argv
1813 ){
1814   SHA3Context cx;
1815   int eType = sqlite3_value_type(argv[0]);
1816   int nByte = sqlite3_value_bytes(argv[0]);
1817   int iSize;
1818   if( argc==1 ){
1819     iSize = 256;
1820   }else{
1821     iSize = sqlite3_value_int(argv[1]);
1822     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1823       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1824                                     "384 512", -1);
1825       return;
1826     }
1827   }
1828   if( eType==SQLITE_NULL ) return;
1829   SHA3Init(&cx, iSize);
1830   if( eType==SQLITE_BLOB ){
1831     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1832   }else{
1833     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1834   }
1835   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1836 }
1837 
1838 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1839 ** of 50 bytes and add it to the hash.
1840 */
1841 static void hash_step_vformat(
1842   SHA3Context *p,                 /* Add content to this context */
1843   const char *zFormat,
1844   ...
1845 ){
1846   va_list ap;
1847   int n;
1848   char zBuf[50];
1849   va_start(ap, zFormat);
1850   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1851   va_end(ap);
1852   n = (int)strlen(zBuf);
1853   SHA3Update(p, (unsigned char*)zBuf, n);
1854 }
1855 
1856 /*
1857 ** Implementation of the sha3_query(SQL,SIZE) function.
1858 **
1859 ** This function compiles and runs the SQL statement(s) given in the
1860 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
1861 ** size is 256.
1862 **
1863 ** The format of the byte stream that is hashed is summarized as follows:
1864 **
1865 **       S<n>:<sql>
1866 **       R
1867 **       N
1868 **       I<int>
1869 **       F<ieee-float>
1870 **       B<size>:<bytes>
1871 **       T<size>:<text>
1872 **
1873 ** <sql> is the original SQL text for each statement run and <n> is
1874 ** the size of that text.  The SQL text is UTF-8.  A single R character
1875 ** occurs before the start of each row.  N means a NULL value.
1876 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
1877 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1878 ** B means blobs of <size> bytes.  T means text rendered as <size>
1879 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1880 ** text integers.
1881 **
1882 ** For each SQL statement in the X input, there is one S segment.  Each
1883 ** S segment is followed by zero or more R segments, one for each row in the
1884 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
1885 ** one for each column in the result set.  Segments are concatentated directly
1886 ** with no delimiters of any kind.
1887 */
1888 static void sha3QueryFunc(
1889   sqlite3_context *context,
1890   int argc,
1891   sqlite3_value **argv
1892 ){
1893   sqlite3 *db = sqlite3_context_db_handle(context);
1894   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1895   sqlite3_stmt *pStmt = 0;
1896   int nCol;                   /* Number of columns in the result set */
1897   int i;                      /* Loop counter */
1898   int rc;
1899   int n;
1900   const char *z;
1901   SHA3Context cx;
1902   int iSize;
1903 
1904   if( argc==1 ){
1905     iSize = 256;
1906   }else{
1907     iSize = sqlite3_value_int(argv[1]);
1908     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1909       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1910                                     "384 512", -1);
1911       return;
1912     }
1913   }
1914   if( zSql==0 ) return;
1915   SHA3Init(&cx, iSize);
1916   while( zSql[0] ){
1917     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1918     if( rc ){
1919       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1920                                    zSql, sqlite3_errmsg(db));
1921       sqlite3_finalize(pStmt);
1922       sqlite3_result_error(context, zMsg, -1);
1923       sqlite3_free(zMsg);
1924       return;
1925     }
1926     if( !sqlite3_stmt_readonly(pStmt) ){
1927       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1928       sqlite3_finalize(pStmt);
1929       sqlite3_result_error(context, zMsg, -1);
1930       sqlite3_free(zMsg);
1931       return;
1932     }
1933     nCol = sqlite3_column_count(pStmt);
1934     z = sqlite3_sql(pStmt);
1935     n = (int)strlen(z);
1936     hash_step_vformat(&cx,"S%d:",n);
1937     SHA3Update(&cx,(unsigned char*)z,n);
1938 
1939     /* Compute a hash over the result of the query */
1940     while( SQLITE_ROW==sqlite3_step(pStmt) ){
1941       SHA3Update(&cx,(const unsigned char*)"R",1);
1942       for(i=0; i<nCol; i++){
1943         switch( sqlite3_column_type(pStmt,i) ){
1944           case SQLITE_NULL: {
1945             SHA3Update(&cx, (const unsigned char*)"N",1);
1946             break;
1947           }
1948           case SQLITE_INTEGER: {
1949             sqlite3_uint64 u;
1950             int j;
1951             unsigned char x[9];
1952             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1953             memcpy(&u, &v, 8);
1954             for(j=8; j>=1; j--){
1955               x[j] = u & 0xff;
1956               u >>= 8;
1957             }
1958             x[0] = 'I';
1959             SHA3Update(&cx, x, 9);
1960             break;
1961           }
1962           case SQLITE_FLOAT: {
1963             sqlite3_uint64 u;
1964             int j;
1965             unsigned char x[9];
1966             double r = sqlite3_column_double(pStmt,i);
1967             memcpy(&u, &r, 8);
1968             for(j=8; j>=1; j--){
1969               x[j] = u & 0xff;
1970               u >>= 8;
1971             }
1972             x[0] = 'F';
1973             SHA3Update(&cx,x,9);
1974             break;
1975           }
1976           case SQLITE_TEXT: {
1977             int n2 = sqlite3_column_bytes(pStmt, i);
1978             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1979             hash_step_vformat(&cx,"T%d:",n2);
1980             SHA3Update(&cx, z2, n2);
1981             break;
1982           }
1983           case SQLITE_BLOB: {
1984             int n2 = sqlite3_column_bytes(pStmt, i);
1985             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1986             hash_step_vformat(&cx,"B%d:",n2);
1987             SHA3Update(&cx, z2, n2);
1988             break;
1989           }
1990         }
1991       }
1992     }
1993     sqlite3_finalize(pStmt);
1994   }
1995   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1996 }
1997 
1998 
1999 #ifdef _WIN32
2000 
2001 #endif
2002 int sqlite3_shathree_init(
2003   sqlite3 *db,
2004   char **pzErrMsg,
2005   const sqlite3_api_routines *pApi
2006 ){
2007   int rc = SQLITE_OK;
2008   SQLITE_EXTENSION_INIT2(pApi);
2009   (void)pzErrMsg;  /* Unused parameter */
2010   rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2011                                sha3Func, 0, 0);
2012   if( rc==SQLITE_OK ){
2013     rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2014                                  sha3Func, 0, 0);
2015   }
2016   if( rc==SQLITE_OK ){
2017     rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2018                                  sha3QueryFunc, 0, 0);
2019   }
2020   if( rc==SQLITE_OK ){
2021     rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2022                                  sha3QueryFunc, 0, 0);
2023   }
2024   return rc;
2025 }
2026 
2027 /************************* End ../ext/misc/shathree.c ********************/
2028 /************************* Begin ../ext/misc/fileio.c ******************/
2029 /*
2030 ** 2014-06-13
2031 **
2032 ** The author disclaims copyright to this source code.  In place of
2033 ** a legal notice, here is a blessing:
2034 **
2035 **    May you do good and not evil.
2036 **    May you find forgiveness for yourself and forgive others.
2037 **    May you share freely, never taking more than you give.
2038 **
2039 ******************************************************************************
2040 **
2041 ** This SQLite extension implements SQL functions readfile() and
2042 ** writefile(), and eponymous virtual type "fsdir".
2043 **
2044 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2045 **
2046 **   If neither of the optional arguments is present, then this UDF
2047 **   function writes blob DATA to file FILE. If successful, the number
2048 **   of bytes written is returned. If an error occurs, NULL is returned.
2049 **
2050 **   If the first option argument - MODE - is present, then it must
2051 **   be passed an integer value that corresponds to a POSIX mode
2052 **   value (file type + permissions, as returned in the stat.st_mode
2053 **   field by the stat() system call). Three types of files may
2054 **   be written/created:
2055 **
2056 **     regular files:  (mode & 0170000)==0100000
2057 **     symbolic links: (mode & 0170000)==0120000
2058 **     directories:    (mode & 0170000)==0040000
2059 **
2060 **   For a directory, the DATA is ignored. For a symbolic link, it is
2061 **   interpreted as text and used as the target of the link. For a
2062 **   regular file, it is interpreted as a blob and written into the
2063 **   named file. Regardless of the type of file, its permissions are
2064 **   set to (mode & 0777) before returning.
2065 **
2066 **   If the optional MTIME argument is present, then it is interpreted
2067 **   as an integer - the number of seconds since the unix epoch. The
2068 **   modification-time of the target file is set to this value before
2069 **   returning.
2070 **
2071 **   If three or more arguments are passed to this function and an
2072 **   error is encountered, an exception is raised.
2073 **
2074 ** READFILE(FILE):
2075 **
2076 **   Read and return the contents of file FILE (type blob) from disk.
2077 **
2078 ** FSDIR:
2079 **
2080 **   Used as follows:
2081 **
2082 **     SELECT * FROM fsdir($path [, $dir]);
2083 **
2084 **   Parameter $path is an absolute or relative pathname. If the file that it
2085 **   refers to does not exist, it is an error. If the path refers to a regular
2086 **   file or symbolic link, it returns a single row. Or, if the path refers
2087 **   to a directory, it returns one row for the directory, and one row for each
2088 **   file within the hierarchy rooted at $path.
2089 **
2090 **   Each row has the following columns:
2091 **
2092 **     name:  Path to file or directory (text value).
2093 **     mode:  Value of stat.st_mode for directory entry (an integer).
2094 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2095 **     data:  For a regular file, a blob containing the file data. For a
2096 **            symlink, a text value containing the text of the link. For a
2097 **            directory, NULL.
2098 **
2099 **   If a non-NULL value is specified for the optional $dir parameter and
2100 **   $path is a relative path, then $path is interpreted relative to $dir.
2101 **   And the paths returned in the "name" column of the table are also
2102 **   relative to directory $dir.
2103 */
2104 /* #include "sqlite3ext.h" */
2105 SQLITE_EXTENSION_INIT1
2106 #include <stdio.h>
2107 #include <string.h>
2108 #include <assert.h>
2109 
2110 #include <sys/types.h>
2111 #include <sys/stat.h>
2112 #include <fcntl.h>
2113 #if !defined(_WIN32) && !defined(WIN32)
2114 #  include <unistd.h>
2115 #  include <dirent.h>
2116 #  include <utime.h>
2117 #  include <sys/time.h>
2118 #else
2119 #  include "windows.h"
2120 #  include <io.h>
2121 #  include <direct.h>
2122 /* #  include "test_windirent.h" */
2123 #  define dirent DIRENT
2124 #  ifndef chmod
2125 #    define chmod _chmod
2126 #  endif
2127 #  ifndef stat
2128 #    define stat _stat
2129 #  endif
2130 #  define mkdir(path,mode) _mkdir(path)
2131 #  define lstat(path,buf) stat(path,buf)
2132 #endif
2133 #include <time.h>
2134 #include <errno.h>
2135 
2136 
2137 /*
2138 ** Structure of the fsdir() table-valued function
2139 */
2140                  /*    0    1    2     3    4           5             */
2141 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2142 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
2143 #define FSDIR_COLUMN_MODE     1     /* Access mode */
2144 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2145 #define FSDIR_COLUMN_DATA     3     /* File content */
2146 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2147 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2148 
2149 
2150 /*
2151 ** Set the result stored by context ctx to a blob containing the
2152 ** contents of file zName.  Or, leave the result unchanged (NULL)
2153 ** if the file does not exist or is unreadable.
2154 **
2155 ** If the file exceeds the SQLite blob size limit, through an
2156 ** SQLITE_TOOBIG error.
2157 **
2158 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2159 ** off of disk.
2160 */
2161 static void readFileContents(sqlite3_context *ctx, const char *zName){
2162   FILE *in;
2163   sqlite3_int64 nIn;
2164   void *pBuf;
2165   sqlite3 *db;
2166   int mxBlob;
2167 
2168   in = fopen(zName, "rb");
2169   if( in==0 ){
2170     /* File does not exist or is unreadable. Leave the result set to NULL. */
2171     return;
2172   }
2173   fseek(in, 0, SEEK_END);
2174   nIn = ftell(in);
2175   rewind(in);
2176   db = sqlite3_context_db_handle(ctx);
2177   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2178   if( nIn>mxBlob ){
2179     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2180     fclose(in);
2181     return;
2182   }
2183   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2184   if( pBuf==0 ){
2185     sqlite3_result_error_nomem(ctx);
2186     fclose(in);
2187     return;
2188   }
2189   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2190     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2191   }else{
2192     sqlite3_result_error_code(ctx, SQLITE_IOERR);
2193     sqlite3_free(pBuf);
2194   }
2195   fclose(in);
2196 }
2197 
2198 /*
2199 ** Implementation of the "readfile(X)" SQL function.  The entire content
2200 ** of the file named X is read and returned as a BLOB.  NULL is returned
2201 ** if the file does not exist or is unreadable.
2202 */
2203 static void readfileFunc(
2204   sqlite3_context *context,
2205   int argc,
2206   sqlite3_value **argv
2207 ){
2208   const char *zName;
2209   (void)(argc);  /* Unused parameter */
2210   zName = (const char*)sqlite3_value_text(argv[0]);
2211   if( zName==0 ) return;
2212   readFileContents(context, zName);
2213 }
2214 
2215 /*
2216 ** Set the error message contained in context ctx to the results of
2217 ** vprintf(zFmt, ...).
2218 */
2219 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2220   char *zMsg = 0;
2221   va_list ap;
2222   va_start(ap, zFmt);
2223   zMsg = sqlite3_vmprintf(zFmt, ap);
2224   sqlite3_result_error(ctx, zMsg, -1);
2225   sqlite3_free(zMsg);
2226   va_end(ap);
2227 }
2228 
2229 #if defined(_WIN32)
2230 /*
2231 ** This function is designed to convert a Win32 FILETIME structure into the
2232 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2233 */
2234 static sqlite3_uint64 fileTimeToUnixTime(
2235   LPFILETIME pFileTime
2236 ){
2237   SYSTEMTIME epochSystemTime;
2238   ULARGE_INTEGER epochIntervals;
2239   FILETIME epochFileTime;
2240   ULARGE_INTEGER fileIntervals;
2241 
2242   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2243   epochSystemTime.wYear = 1970;
2244   epochSystemTime.wMonth = 1;
2245   epochSystemTime.wDay = 1;
2246   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2247   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2248   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2249 
2250   fileIntervals.LowPart = pFileTime->dwLowDateTime;
2251   fileIntervals.HighPart = pFileTime->dwHighDateTime;
2252 
2253   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2254 }
2255 
2256 /*
2257 ** This function attempts to normalize the time values found in the stat()
2258 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2259 ** appears to return these values as local times.
2260 */
2261 static void statTimesToUtc(
2262   const char *zPath,
2263   struct stat *pStatBuf
2264 ){
2265   HANDLE hFindFile;
2266   WIN32_FIND_DATAW fd;
2267   LPWSTR zUnicodeName;
2268   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2269   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2270   if( zUnicodeName ){
2271     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2272     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2273     if( hFindFile!=NULL ){
2274       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2275       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2276       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2277       FindClose(hFindFile);
2278     }
2279     sqlite3_free(zUnicodeName);
2280   }
2281 }
2282 #endif
2283 
2284 /*
2285 ** This function is used in place of stat().  On Windows, special handling
2286 ** is required in order for the included time to be returned as UTC.  On all
2287 ** other systems, this function simply calls stat().
2288 */
2289 static int fileStat(
2290   const char *zPath,
2291   struct stat *pStatBuf
2292 ){
2293 #if defined(_WIN32)
2294   int rc = stat(zPath, pStatBuf);
2295   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2296   return rc;
2297 #else
2298   return stat(zPath, pStatBuf);
2299 #endif
2300 }
2301 
2302 /*
2303 ** This function is used in place of lstat().  On Windows, special handling
2304 ** is required in order for the included time to be returned as UTC.  On all
2305 ** other systems, this function simply calls lstat().
2306 */
2307 static int fileLinkStat(
2308   const char *zPath,
2309   struct stat *pStatBuf
2310 ){
2311 #if defined(_WIN32)
2312   int rc = lstat(zPath, pStatBuf);
2313   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2314   return rc;
2315 #else
2316   return lstat(zPath, pStatBuf);
2317 #endif
2318 }
2319 
2320 /*
2321 ** Argument zFile is the name of a file that will be created and/or written
2322 ** by SQL function writefile(). This function ensures that the directory
2323 ** zFile will be written to exists, creating it if required. The permissions
2324 ** for any path components created by this function are set in accordance
2325 ** with the current umask.
2326 **
2327 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2328 ** SQLITE_OK is returned if the directory is successfully created, or
2329 ** SQLITE_ERROR otherwise.
2330 */
2331 static int makeDirectory(
2332   const char *zFile
2333 ){
2334   char *zCopy = sqlite3_mprintf("%s", zFile);
2335   int rc = SQLITE_OK;
2336 
2337   if( zCopy==0 ){
2338     rc = SQLITE_NOMEM;
2339   }else{
2340     int nCopy = (int)strlen(zCopy);
2341     int i = 1;
2342 
2343     while( rc==SQLITE_OK ){
2344       struct stat sStat;
2345       int rc2;
2346 
2347       for(; zCopy[i]!='/' && i<nCopy; i++);
2348       if( i==nCopy ) break;
2349       zCopy[i] = '\0';
2350 
2351       rc2 = fileStat(zCopy, &sStat);
2352       if( rc2!=0 ){
2353         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2354       }else{
2355         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2356       }
2357       zCopy[i] = '/';
2358       i++;
2359     }
2360 
2361     sqlite3_free(zCopy);
2362   }
2363 
2364   return rc;
2365 }
2366 
2367 /*
2368 ** This function does the work for the writefile() UDF. Refer to
2369 ** header comments at the top of this file for details.
2370 */
2371 static int writeFile(
2372   sqlite3_context *pCtx,          /* Context to return bytes written in */
2373   const char *zFile,              /* File to write */
2374   sqlite3_value *pData,           /* Data to write */
2375   mode_t mode,                    /* MODE parameter passed to writefile() */
2376   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2377 ){
2378 #if !defined(_WIN32) && !defined(WIN32)
2379   if( S_ISLNK(mode) ){
2380     const char *zTo = (const char*)sqlite3_value_text(pData);
2381     if( symlink(zTo, zFile)<0 ) return 1;
2382   }else
2383 #endif
2384   {
2385     if( S_ISDIR(mode) ){
2386       if( mkdir(zFile, mode) ){
2387         /* The mkdir() call to create the directory failed. This might not
2388         ** be an error though - if there is already a directory at the same
2389         ** path and either the permissions already match or can be changed
2390         ** to do so using chmod(), it is not an error.  */
2391         struct stat sStat;
2392         if( errno!=EEXIST
2393          || 0!=fileStat(zFile, &sStat)
2394          || !S_ISDIR(sStat.st_mode)
2395          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2396         ){
2397           return 1;
2398         }
2399       }
2400     }else{
2401       sqlite3_int64 nWrite = 0;
2402       const char *z;
2403       int rc = 0;
2404       FILE *out = fopen(zFile, "wb");
2405       if( out==0 ) return 1;
2406       z = (const char*)sqlite3_value_blob(pData);
2407       if( z ){
2408         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2409         nWrite = sqlite3_value_bytes(pData);
2410         if( nWrite!=n ){
2411           rc = 1;
2412         }
2413       }
2414       fclose(out);
2415       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2416         rc = 1;
2417       }
2418       if( rc ) return 2;
2419       sqlite3_result_int64(pCtx, nWrite);
2420     }
2421   }
2422 
2423   if( mtime>=0 ){
2424 #if defined(_WIN32)
2425     /* Windows */
2426     FILETIME lastAccess;
2427     FILETIME lastWrite;
2428     SYSTEMTIME currentTime;
2429     LONGLONG intervals;
2430     HANDLE hFile;
2431     LPWSTR zUnicodeName;
2432     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2433 
2434     GetSystemTime(&currentTime);
2435     SystemTimeToFileTime(&currentTime, &lastAccess);
2436     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2437     lastWrite.dwLowDateTime = (DWORD)intervals;
2438     lastWrite.dwHighDateTime = intervals >> 32;
2439     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2440     if( zUnicodeName==0 ){
2441       return 1;
2442     }
2443     hFile = CreateFileW(
2444       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2445       FILE_FLAG_BACKUP_SEMANTICS, NULL
2446     );
2447     sqlite3_free(zUnicodeName);
2448     if( hFile!=INVALID_HANDLE_VALUE ){
2449       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2450       CloseHandle(hFile);
2451       return !bResult;
2452     }else{
2453       return 1;
2454     }
2455 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2456     /* Recent unix */
2457     struct timespec times[2];
2458     times[0].tv_nsec = times[1].tv_nsec = 0;
2459     times[0].tv_sec = time(0);
2460     times[1].tv_sec = mtime;
2461     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2462       return 1;
2463     }
2464 #else
2465     /* Legacy unix */
2466     struct timeval times[2];
2467     times[0].tv_usec = times[1].tv_usec = 0;
2468     times[0].tv_sec = time(0);
2469     times[1].tv_sec = mtime;
2470     if( utimes(zFile, times) ){
2471       return 1;
2472     }
2473 #endif
2474   }
2475 
2476   return 0;
2477 }
2478 
2479 /*
2480 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2481 ** Refer to header comments at the top of this file for details.
2482 */
2483 static void writefileFunc(
2484   sqlite3_context *context,
2485   int argc,
2486   sqlite3_value **argv
2487 ){
2488   const char *zFile;
2489   mode_t mode = 0;
2490   int res;
2491   sqlite3_int64 mtime = -1;
2492 
2493   if( argc<2 || argc>4 ){
2494     sqlite3_result_error(context,
2495         "wrong number of arguments to function writefile()", -1
2496     );
2497     return;
2498   }
2499 
2500   zFile = (const char*)sqlite3_value_text(argv[0]);
2501   if( zFile==0 ) return;
2502   if( argc>=3 ){
2503     mode = (mode_t)sqlite3_value_int(argv[2]);
2504   }
2505   if( argc==4 ){
2506     mtime = sqlite3_value_int64(argv[3]);
2507   }
2508 
2509   res = writeFile(context, zFile, argv[1], mode, mtime);
2510   if( res==1 && errno==ENOENT ){
2511     if( makeDirectory(zFile)==SQLITE_OK ){
2512       res = writeFile(context, zFile, argv[1], mode, mtime);
2513     }
2514   }
2515 
2516   if( argc>2 && res!=0 ){
2517     if( S_ISLNK(mode) ){
2518       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2519     }else if( S_ISDIR(mode) ){
2520       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2521     }else{
2522       ctxErrorMsg(context, "failed to write file: %s", zFile);
2523     }
2524   }
2525 }
2526 
2527 /*
2528 ** SQL function:   lsmode(MODE)
2529 **
2530 ** Given a numberic st_mode from stat(), convert it into a human-readable
2531 ** text string in the style of "ls -l".
2532 */
2533 static void lsModeFunc(
2534   sqlite3_context *context,
2535   int argc,
2536   sqlite3_value **argv
2537 ){
2538   int i;
2539   int iMode = sqlite3_value_int(argv[0]);
2540   char z[16];
2541   (void)argc;
2542   if( S_ISLNK(iMode) ){
2543     z[0] = 'l';
2544   }else if( S_ISREG(iMode) ){
2545     z[0] = '-';
2546   }else if( S_ISDIR(iMode) ){
2547     z[0] = 'd';
2548   }else{
2549     z[0] = '?';
2550   }
2551   for(i=0; i<3; i++){
2552     int m = (iMode >> ((2-i)*3));
2553     char *a = &z[1 + i*3];
2554     a[0] = (m & 0x4) ? 'r' : '-';
2555     a[1] = (m & 0x2) ? 'w' : '-';
2556     a[2] = (m & 0x1) ? 'x' : '-';
2557   }
2558   z[10] = '\0';
2559   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2560 }
2561 
2562 #ifndef SQLITE_OMIT_VIRTUALTABLE
2563 
2564 /*
2565 ** Cursor type for recursively iterating through a directory structure.
2566 */
2567 typedef struct fsdir_cursor fsdir_cursor;
2568 typedef struct FsdirLevel FsdirLevel;
2569 
2570 struct FsdirLevel {
2571   DIR *pDir;                 /* From opendir() */
2572   char *zDir;                /* Name of directory (nul-terminated) */
2573 };
2574 
2575 struct fsdir_cursor {
2576   sqlite3_vtab_cursor base;  /* Base class - must be first */
2577 
2578   int nLvl;                  /* Number of entries in aLvl[] array */
2579   int iLvl;                  /* Index of current entry */
2580   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2581 
2582   const char *zBase;
2583   int nBase;
2584 
2585   struct stat sStat;         /* Current lstat() results */
2586   char *zPath;               /* Path to current entry */
2587   sqlite3_int64 iRowid;      /* Current rowid */
2588 };
2589 
2590 typedef struct fsdir_tab fsdir_tab;
2591 struct fsdir_tab {
2592   sqlite3_vtab base;         /* Base class - must be first */
2593 };
2594 
2595 /*
2596 ** Construct a new fsdir virtual table object.
2597 */
2598 static int fsdirConnect(
2599   sqlite3 *db,
2600   void *pAux,
2601   int argc, const char *const*argv,
2602   sqlite3_vtab **ppVtab,
2603   char **pzErr
2604 ){
2605   fsdir_tab *pNew = 0;
2606   int rc;
2607   (void)pAux;
2608   (void)argc;
2609   (void)argv;
2610   (void)pzErr;
2611   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2612   if( rc==SQLITE_OK ){
2613     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2614     if( pNew==0 ) return SQLITE_NOMEM;
2615     memset(pNew, 0, sizeof(*pNew));
2616   }
2617   *ppVtab = (sqlite3_vtab*)pNew;
2618   return rc;
2619 }
2620 
2621 /*
2622 ** This method is the destructor for fsdir vtab objects.
2623 */
2624 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2625   sqlite3_free(pVtab);
2626   return SQLITE_OK;
2627 }
2628 
2629 /*
2630 ** Constructor for a new fsdir_cursor object.
2631 */
2632 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2633   fsdir_cursor *pCur;
2634   (void)p;
2635   pCur = sqlite3_malloc( sizeof(*pCur) );
2636   if( pCur==0 ) return SQLITE_NOMEM;
2637   memset(pCur, 0, sizeof(*pCur));
2638   pCur->iLvl = -1;
2639   *ppCursor = &pCur->base;
2640   return SQLITE_OK;
2641 }
2642 
2643 /*
2644 ** Reset a cursor back to the state it was in when first returned
2645 ** by fsdirOpen().
2646 */
2647 static void fsdirResetCursor(fsdir_cursor *pCur){
2648   int i;
2649   for(i=0; i<=pCur->iLvl; i++){
2650     FsdirLevel *pLvl = &pCur->aLvl[i];
2651     if( pLvl->pDir ) closedir(pLvl->pDir);
2652     sqlite3_free(pLvl->zDir);
2653   }
2654   sqlite3_free(pCur->zPath);
2655   sqlite3_free(pCur->aLvl);
2656   pCur->aLvl = 0;
2657   pCur->zPath = 0;
2658   pCur->zBase = 0;
2659   pCur->nBase = 0;
2660   pCur->nLvl = 0;
2661   pCur->iLvl = -1;
2662   pCur->iRowid = 1;
2663 }
2664 
2665 /*
2666 ** Destructor for an fsdir_cursor.
2667 */
2668 static int fsdirClose(sqlite3_vtab_cursor *cur){
2669   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2670 
2671   fsdirResetCursor(pCur);
2672   sqlite3_free(pCur);
2673   return SQLITE_OK;
2674 }
2675 
2676 /*
2677 ** Set the error message for the virtual table associated with cursor
2678 ** pCur to the results of vprintf(zFmt, ...).
2679 */
2680 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2681   va_list ap;
2682   va_start(ap, zFmt);
2683   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2684   va_end(ap);
2685 }
2686 
2687 
2688 /*
2689 ** Advance an fsdir_cursor to its next row of output.
2690 */
2691 static int fsdirNext(sqlite3_vtab_cursor *cur){
2692   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2693   mode_t m = pCur->sStat.st_mode;
2694 
2695   pCur->iRowid++;
2696   if( S_ISDIR(m) ){
2697     /* Descend into this directory */
2698     int iNew = pCur->iLvl + 1;
2699     FsdirLevel *pLvl;
2700     if( iNew>=pCur->nLvl ){
2701       int nNew = iNew+1;
2702       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2703       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2704       if( aNew==0 ) return SQLITE_NOMEM;
2705       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2706       pCur->aLvl = aNew;
2707       pCur->nLvl = nNew;
2708     }
2709     pCur->iLvl = iNew;
2710     pLvl = &pCur->aLvl[iNew];
2711 
2712     pLvl->zDir = pCur->zPath;
2713     pCur->zPath = 0;
2714     pLvl->pDir = opendir(pLvl->zDir);
2715     if( pLvl->pDir==0 ){
2716       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2717       return SQLITE_ERROR;
2718     }
2719   }
2720 
2721   while( pCur->iLvl>=0 ){
2722     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2723     struct dirent *pEntry = readdir(pLvl->pDir);
2724     if( pEntry ){
2725       if( pEntry->d_name[0]=='.' ){
2726        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2727        if( pEntry->d_name[1]=='\0' ) continue;
2728       }
2729       sqlite3_free(pCur->zPath);
2730       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2731       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2732       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2733         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2734         return SQLITE_ERROR;
2735       }
2736       return SQLITE_OK;
2737     }
2738     closedir(pLvl->pDir);
2739     sqlite3_free(pLvl->zDir);
2740     pLvl->pDir = 0;
2741     pLvl->zDir = 0;
2742     pCur->iLvl--;
2743   }
2744 
2745   /* EOF */
2746   sqlite3_free(pCur->zPath);
2747   pCur->zPath = 0;
2748   return SQLITE_OK;
2749 }
2750 
2751 /*
2752 ** Return values of columns for the row at which the series_cursor
2753 ** is currently pointing.
2754 */
2755 static int fsdirColumn(
2756   sqlite3_vtab_cursor *cur,   /* The cursor */
2757   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2758   int i                       /* Which column to return */
2759 ){
2760   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2761   switch( i ){
2762     case FSDIR_COLUMN_NAME: {
2763       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2764       break;
2765     }
2766 
2767     case FSDIR_COLUMN_MODE:
2768       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2769       break;
2770 
2771     case FSDIR_COLUMN_MTIME:
2772       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2773       break;
2774 
2775     case FSDIR_COLUMN_DATA: {
2776       mode_t m = pCur->sStat.st_mode;
2777       if( S_ISDIR(m) ){
2778         sqlite3_result_null(ctx);
2779 #if !defined(_WIN32) && !defined(WIN32)
2780       }else if( S_ISLNK(m) ){
2781         char aStatic[64];
2782         char *aBuf = aStatic;
2783         sqlite3_int64 nBuf = 64;
2784         int n;
2785 
2786         while( 1 ){
2787           n = readlink(pCur->zPath, aBuf, nBuf);
2788           if( n<nBuf ) break;
2789           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2790           nBuf = nBuf*2;
2791           aBuf = sqlite3_malloc64(nBuf);
2792           if( aBuf==0 ){
2793             sqlite3_result_error_nomem(ctx);
2794             return SQLITE_NOMEM;
2795           }
2796         }
2797 
2798         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2799         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2800 #endif
2801       }else{
2802         readFileContents(ctx, pCur->zPath);
2803       }
2804     }
2805     case FSDIR_COLUMN_PATH:
2806     default: {
2807       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2808       ** always return their values as NULL */
2809       break;
2810     }
2811   }
2812   return SQLITE_OK;
2813 }
2814 
2815 /*
2816 ** Return the rowid for the current row. In this implementation, the
2817 ** first row returned is assigned rowid value 1, and each subsequent
2818 ** row a value 1 more than that of the previous.
2819 */
2820 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2821   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2822   *pRowid = pCur->iRowid;
2823   return SQLITE_OK;
2824 }
2825 
2826 /*
2827 ** Return TRUE if the cursor has been moved off of the last
2828 ** row of output.
2829 */
2830 static int fsdirEof(sqlite3_vtab_cursor *cur){
2831   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2832   return (pCur->zPath==0);
2833 }
2834 
2835 /*
2836 ** xFilter callback.
2837 **
2838 ** idxNum==1   PATH parameter only
2839 ** idxNum==2   Both PATH and DIR supplied
2840 */
2841 static int fsdirFilter(
2842   sqlite3_vtab_cursor *cur,
2843   int idxNum, const char *idxStr,
2844   int argc, sqlite3_value **argv
2845 ){
2846   const char *zDir = 0;
2847   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2848   (void)idxStr;
2849   fsdirResetCursor(pCur);
2850 
2851   if( idxNum==0 ){
2852     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2853     return SQLITE_ERROR;
2854   }
2855 
2856   assert( argc==idxNum && (argc==1 || argc==2) );
2857   zDir = (const char*)sqlite3_value_text(argv[0]);
2858   if( zDir==0 ){
2859     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2860     return SQLITE_ERROR;
2861   }
2862   if( argc==2 ){
2863     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2864   }
2865   if( pCur->zBase ){
2866     pCur->nBase = (int)strlen(pCur->zBase)+1;
2867     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2868   }else{
2869     pCur->zPath = sqlite3_mprintf("%s", zDir);
2870   }
2871 
2872   if( pCur->zPath==0 ){
2873     return SQLITE_NOMEM;
2874   }
2875   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2876     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2877     return SQLITE_ERROR;
2878   }
2879 
2880   return SQLITE_OK;
2881 }
2882 
2883 /*
2884 ** SQLite will invoke this method one or more times while planning a query
2885 ** that uses the generate_series virtual table.  This routine needs to create
2886 ** a query plan for each invocation and compute an estimated cost for that
2887 ** plan.
2888 **
2889 ** In this implementation idxNum is used to represent the
2890 ** query plan.  idxStr is unused.
2891 **
2892 ** The query plan is represented by values of idxNum:
2893 **
2894 **  (1)  The path value is supplied by argv[0]
2895 **  (2)  Path is in argv[0] and dir is in argv[1]
2896 */
2897 static int fsdirBestIndex(
2898   sqlite3_vtab *tab,
2899   sqlite3_index_info *pIdxInfo
2900 ){
2901   int i;                 /* Loop over constraints */
2902   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
2903   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
2904   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
2905   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
2906   const struct sqlite3_index_constraint *pConstraint;
2907 
2908   (void)tab;
2909   pConstraint = pIdxInfo->aConstraint;
2910   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2911     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2912     switch( pConstraint->iColumn ){
2913       case FSDIR_COLUMN_PATH: {
2914         if( pConstraint->usable ){
2915           idxPath = i;
2916           seenPath = 0;
2917         }else if( idxPath<0 ){
2918           seenPath = 1;
2919         }
2920         break;
2921       }
2922       case FSDIR_COLUMN_DIR: {
2923         if( pConstraint->usable ){
2924           idxDir = i;
2925           seenDir = 0;
2926         }else if( idxDir<0 ){
2927           seenDir = 1;
2928         }
2929         break;
2930       }
2931     }
2932   }
2933   if( seenPath || seenDir ){
2934     /* If input parameters are unusable, disallow this plan */
2935     return SQLITE_CONSTRAINT;
2936   }
2937 
2938   if( idxPath<0 ){
2939     pIdxInfo->idxNum = 0;
2940     /* The pIdxInfo->estimatedCost should have been initialized to a huge
2941     ** number.  Leave it unchanged. */
2942     pIdxInfo->estimatedRows = 0x7fffffff;
2943   }else{
2944     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
2945     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
2946     if( idxDir>=0 ){
2947       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
2948       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
2949       pIdxInfo->idxNum = 2;
2950       pIdxInfo->estimatedCost = 10.0;
2951     }else{
2952       pIdxInfo->idxNum = 1;
2953       pIdxInfo->estimatedCost = 100.0;
2954     }
2955   }
2956 
2957   return SQLITE_OK;
2958 }
2959 
2960 /*
2961 ** Register the "fsdir" virtual table.
2962 */
2963 static int fsdirRegister(sqlite3 *db){
2964   static sqlite3_module fsdirModule = {
2965     0,                         /* iVersion */
2966     0,                         /* xCreate */
2967     fsdirConnect,              /* xConnect */
2968     fsdirBestIndex,            /* xBestIndex */
2969     fsdirDisconnect,           /* xDisconnect */
2970     0,                         /* xDestroy */
2971     fsdirOpen,                 /* xOpen - open a cursor */
2972     fsdirClose,                /* xClose - close a cursor */
2973     fsdirFilter,               /* xFilter - configure scan constraints */
2974     fsdirNext,                 /* xNext - advance a cursor */
2975     fsdirEof,                  /* xEof - check for end of scan */
2976     fsdirColumn,               /* xColumn - read data */
2977     fsdirRowid,                /* xRowid - read data */
2978     0,                         /* xUpdate */
2979     0,                         /* xBegin */
2980     0,                         /* xSync */
2981     0,                         /* xCommit */
2982     0,                         /* xRollback */
2983     0,                         /* xFindMethod */
2984     0,                         /* xRename */
2985     0,                         /* xSavepoint */
2986     0,                         /* xRelease */
2987     0,                         /* xRollbackTo */
2988     0,                         /* xShadowName */
2989   };
2990 
2991   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2992   return rc;
2993 }
2994 #else         /* SQLITE_OMIT_VIRTUALTABLE */
2995 # define fsdirRegister(x) SQLITE_OK
2996 #endif
2997 
2998 #ifdef _WIN32
2999 
3000 #endif
3001 int sqlite3_fileio_init(
3002   sqlite3 *db,
3003   char **pzErrMsg,
3004   const sqlite3_api_routines *pApi
3005 ){
3006   int rc = SQLITE_OK;
3007   SQLITE_EXTENSION_INIT2(pApi);
3008   (void)pzErrMsg;  /* Unused parameter */
3009   rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
3010                                readfileFunc, 0, 0);
3011   if( rc==SQLITE_OK ){
3012     rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
3013                                  writefileFunc, 0, 0);
3014   }
3015   if( rc==SQLITE_OK ){
3016     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3017                                  lsModeFunc, 0, 0);
3018   }
3019   if( rc==SQLITE_OK ){
3020     rc = fsdirRegister(db);
3021   }
3022   return rc;
3023 }
3024 
3025 /************************* End ../ext/misc/fileio.c ********************/
3026 /************************* Begin ../ext/misc/completion.c ******************/
3027 /*
3028 ** 2017-07-10
3029 **
3030 ** The author disclaims copyright to this source code.  In place of
3031 ** a legal notice, here is a blessing:
3032 **
3033 **    May you do good and not evil.
3034 **    May you find forgiveness for yourself and forgive others.
3035 **    May you share freely, never taking more than you give.
3036 **
3037 *************************************************************************
3038 **
3039 ** This file implements an eponymous virtual table that returns suggested
3040 ** completions for a partial SQL input.
3041 **
3042 ** Suggested usage:
3043 **
3044 **     SELECT DISTINCT candidate COLLATE nocase
3045 **       FROM completion($prefix,$wholeline)
3046 **      ORDER BY 1;
3047 **
3048 ** The two query parameters are optional.  $prefix is the text of the
3049 ** current word being typed and that is to be completed.  $wholeline is
3050 ** the complete input line, used for context.
3051 **
3052 ** The raw completion() table might return the same candidate multiple
3053 ** times, for example if the same column name is used to two or more
3054 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
3055 ** the DISTINCT and ORDER BY are recommended.
3056 **
3057 ** This virtual table operates at the speed of human typing, and so there
3058 ** is no attempt to make it fast.  Even a slow implementation will be much
3059 ** faster than any human can type.
3060 **
3061 */
3062 /* #include "sqlite3ext.h" */
3063 SQLITE_EXTENSION_INIT1
3064 #include <assert.h>
3065 #include <string.h>
3066 #include <ctype.h>
3067 
3068 #ifndef SQLITE_OMIT_VIRTUALTABLE
3069 
3070 /* completion_vtab is a subclass of sqlite3_vtab which will
3071 ** serve as the underlying representation of a completion virtual table
3072 */
3073 typedef struct completion_vtab completion_vtab;
3074 struct completion_vtab {
3075   sqlite3_vtab base;  /* Base class - must be first */
3076   sqlite3 *db;        /* Database connection for this completion vtab */
3077 };
3078 
3079 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3080 ** serve as the underlying representation of a cursor that scans
3081 ** over rows of the result
3082 */
3083 typedef struct completion_cursor completion_cursor;
3084 struct completion_cursor {
3085   sqlite3_vtab_cursor base;  /* Base class - must be first */
3086   sqlite3 *db;               /* Database connection for this cursor */
3087   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3088   char *zPrefix;             /* The prefix for the word we want to complete */
3089   char *zLine;               /* The whole that we want to complete */
3090   const char *zCurrentRow;   /* Current output row */
3091   int szRow;                 /* Length of the zCurrentRow string */
3092   sqlite3_stmt *pStmt;       /* Current statement */
3093   sqlite3_int64 iRowid;      /* The rowid */
3094   int ePhase;                /* Current phase */
3095   int j;                     /* inter-phase counter */
3096 };
3097 
3098 /* Values for ePhase:
3099 */
3100 #define COMPLETION_FIRST_PHASE   1
3101 #define COMPLETION_KEYWORDS      1
3102 #define COMPLETION_PRAGMAS       2
3103 #define COMPLETION_FUNCTIONS     3
3104 #define COMPLETION_COLLATIONS    4
3105 #define COMPLETION_INDEXES       5
3106 #define COMPLETION_TRIGGERS      6
3107 #define COMPLETION_DATABASES     7
3108 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3109 #define COMPLETION_COLUMNS       9
3110 #define COMPLETION_MODULES       10
3111 #define COMPLETION_EOF           11
3112 
3113 /*
3114 ** The completionConnect() method is invoked to create a new
3115 ** completion_vtab that describes the completion virtual table.
3116 **
3117 ** Think of this routine as the constructor for completion_vtab objects.
3118 **
3119 ** All this routine needs to do is:
3120 **
3121 **    (1) Allocate the completion_vtab object and initialize all fields.
3122 **
3123 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3124 **        result set of queries against completion will look like.
3125 */
3126 static int completionConnect(
3127   sqlite3 *db,
3128   void *pAux,
3129   int argc, const char *const*argv,
3130   sqlite3_vtab **ppVtab,
3131   char **pzErr
3132 ){
3133   completion_vtab *pNew;
3134   int rc;
3135 
3136   (void)(pAux);    /* Unused parameter */
3137   (void)(argc);    /* Unused parameter */
3138   (void)(argv);    /* Unused parameter */
3139   (void)(pzErr);   /* Unused parameter */
3140 
3141 /* Column numbers */
3142 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3143 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3144 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3145 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3146 
3147   rc = sqlite3_declare_vtab(db,
3148       "CREATE TABLE x("
3149       "  candidate TEXT,"
3150       "  prefix TEXT HIDDEN,"
3151       "  wholeline TEXT HIDDEN,"
3152       "  phase INT HIDDEN"        /* Used for debugging only */
3153       ")");
3154   if( rc==SQLITE_OK ){
3155     pNew = sqlite3_malloc( sizeof(*pNew) );
3156     *ppVtab = (sqlite3_vtab*)pNew;
3157     if( pNew==0 ) return SQLITE_NOMEM;
3158     memset(pNew, 0, sizeof(*pNew));
3159     pNew->db = db;
3160   }
3161   return rc;
3162 }
3163 
3164 /*
3165 ** This method is the destructor for completion_cursor objects.
3166 */
3167 static int completionDisconnect(sqlite3_vtab *pVtab){
3168   sqlite3_free(pVtab);
3169   return SQLITE_OK;
3170 }
3171 
3172 /*
3173 ** Constructor for a new completion_cursor object.
3174 */
3175 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3176   completion_cursor *pCur;
3177   pCur = sqlite3_malloc( sizeof(*pCur) );
3178   if( pCur==0 ) return SQLITE_NOMEM;
3179   memset(pCur, 0, sizeof(*pCur));
3180   pCur->db = ((completion_vtab*)p)->db;
3181   *ppCursor = &pCur->base;
3182   return SQLITE_OK;
3183 }
3184 
3185 /*
3186 ** Reset the completion_cursor.
3187 */
3188 static void completionCursorReset(completion_cursor *pCur){
3189   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3190   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3191   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3192   pCur->j = 0;
3193 }
3194 
3195 /*
3196 ** Destructor for a completion_cursor.
3197 */
3198 static int completionClose(sqlite3_vtab_cursor *cur){
3199   completionCursorReset((completion_cursor*)cur);
3200   sqlite3_free(cur);
3201   return SQLITE_OK;
3202 }
3203 
3204 /*
3205 ** Advance a completion_cursor to its next row of output.
3206 **
3207 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3208 ** record the current state of the scan.  This routine sets ->zCurrentRow
3209 ** to the current row of output and then returns.  If no more rows remain,
3210 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3211 ** table that has reached the end of its scan.
3212 **
3213 ** The current implementation just lists potential identifiers and
3214 ** keywords and filters them by zPrefix.  Future enhancements should
3215 ** take zLine into account to try to restrict the set of identifiers and
3216 ** keywords based on what would be legal at the current point of input.
3217 */
3218 static int completionNext(sqlite3_vtab_cursor *cur){
3219   completion_cursor *pCur = (completion_cursor*)cur;
3220   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3221   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3222   pCur->iRowid++;
3223   while( pCur->ePhase!=COMPLETION_EOF ){
3224     switch( pCur->ePhase ){
3225       case COMPLETION_KEYWORDS: {
3226         if( pCur->j >= sqlite3_keyword_count() ){
3227           pCur->zCurrentRow = 0;
3228           pCur->ePhase = COMPLETION_DATABASES;
3229         }else{
3230           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3231         }
3232         iCol = -1;
3233         break;
3234       }
3235       case COMPLETION_DATABASES: {
3236         if( pCur->pStmt==0 ){
3237           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3238                              &pCur->pStmt, 0);
3239         }
3240         iCol = 1;
3241         eNextPhase = COMPLETION_TABLES;
3242         break;
3243       }
3244       case COMPLETION_TABLES: {
3245         if( pCur->pStmt==0 ){
3246           sqlite3_stmt *pS2;
3247           char *zSql = 0;
3248           const char *zSep = "";
3249           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3250           while( sqlite3_step(pS2)==SQLITE_ROW ){
3251             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3252             zSql = sqlite3_mprintf(
3253                "%z%s"
3254                "SELECT name FROM \"%w\".sqlite_master",
3255                zSql, zSep, zDb
3256             );
3257             if( zSql==0 ) return SQLITE_NOMEM;
3258             zSep = " UNION ";
3259           }
3260           sqlite3_finalize(pS2);
3261           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3262           sqlite3_free(zSql);
3263         }
3264         iCol = 0;
3265         eNextPhase = COMPLETION_COLUMNS;
3266         break;
3267       }
3268       case COMPLETION_COLUMNS: {
3269         if( pCur->pStmt==0 ){
3270           sqlite3_stmt *pS2;
3271           char *zSql = 0;
3272           const char *zSep = "";
3273           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3274           while( sqlite3_step(pS2)==SQLITE_ROW ){
3275             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3276             zSql = sqlite3_mprintf(
3277                "%z%s"
3278                "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3279                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3280                " WHERE sm.type='table'",
3281                zSql, zSep, zDb, zDb
3282             );
3283             if( zSql==0 ) return SQLITE_NOMEM;
3284             zSep = " UNION ";
3285           }
3286           sqlite3_finalize(pS2);
3287           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3288           sqlite3_free(zSql);
3289         }
3290         iCol = 0;
3291         eNextPhase = COMPLETION_EOF;
3292         break;
3293       }
3294     }
3295     if( iCol<0 ){
3296       /* This case is when the phase presets zCurrentRow */
3297       if( pCur->zCurrentRow==0 ) continue;
3298     }else{
3299       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3300         /* Extract the next row of content */
3301         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3302         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3303       }else{
3304         /* When all rows are finished, advance to the next phase */
3305         sqlite3_finalize(pCur->pStmt);
3306         pCur->pStmt = 0;
3307         pCur->ePhase = eNextPhase;
3308         continue;
3309       }
3310     }
3311     if( pCur->nPrefix==0 ) break;
3312     if( pCur->nPrefix<=pCur->szRow
3313      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3314     ){
3315       break;
3316     }
3317   }
3318 
3319   return SQLITE_OK;
3320 }
3321 
3322 /*
3323 ** Return values of columns for the row at which the completion_cursor
3324 ** is currently pointing.
3325 */
3326 static int completionColumn(
3327   sqlite3_vtab_cursor *cur,   /* The cursor */
3328   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3329   int i                       /* Which column to return */
3330 ){
3331   completion_cursor *pCur = (completion_cursor*)cur;
3332   switch( i ){
3333     case COMPLETION_COLUMN_CANDIDATE: {
3334       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3335       break;
3336     }
3337     case COMPLETION_COLUMN_PREFIX: {
3338       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3339       break;
3340     }
3341     case COMPLETION_COLUMN_WHOLELINE: {
3342       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3343       break;
3344     }
3345     case COMPLETION_COLUMN_PHASE: {
3346       sqlite3_result_int(ctx, pCur->ePhase);
3347       break;
3348     }
3349   }
3350   return SQLITE_OK;
3351 }
3352 
3353 /*
3354 ** Return the rowid for the current row.  In this implementation, the
3355 ** rowid is the same as the output value.
3356 */
3357 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3358   completion_cursor *pCur = (completion_cursor*)cur;
3359   *pRowid = pCur->iRowid;
3360   return SQLITE_OK;
3361 }
3362 
3363 /*
3364 ** Return TRUE if the cursor has been moved off of the last
3365 ** row of output.
3366 */
3367 static int completionEof(sqlite3_vtab_cursor *cur){
3368   completion_cursor *pCur = (completion_cursor*)cur;
3369   return pCur->ePhase >= COMPLETION_EOF;
3370 }
3371 
3372 /*
3373 ** This method is called to "rewind" the completion_cursor object back
3374 ** to the first row of output.  This method is always called at least
3375 ** once prior to any call to completionColumn() or completionRowid() or
3376 ** completionEof().
3377 */
3378 static int completionFilter(
3379   sqlite3_vtab_cursor *pVtabCursor,
3380   int idxNum, const char *idxStr,
3381   int argc, sqlite3_value **argv
3382 ){
3383   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3384   int iArg = 0;
3385   (void)(idxStr);   /* Unused parameter */
3386   (void)(argc);     /* Unused parameter */
3387   completionCursorReset(pCur);
3388   if( idxNum & 1 ){
3389     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3390     if( pCur->nPrefix>0 ){
3391       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3392       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3393     }
3394     iArg = 1;
3395   }
3396   if( idxNum & 2 ){
3397     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3398     if( pCur->nLine>0 ){
3399       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3400       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3401     }
3402   }
3403   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3404     int i = pCur->nLine;
3405     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3406       i--;
3407     }
3408     pCur->nPrefix = pCur->nLine - i;
3409     if( pCur->nPrefix>0 ){
3410       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3411       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3412     }
3413   }
3414   pCur->iRowid = 0;
3415   pCur->ePhase = COMPLETION_FIRST_PHASE;
3416   return completionNext(pVtabCursor);
3417 }
3418 
3419 /*
3420 ** SQLite will invoke this method one or more times while planning a query
3421 ** that uses the completion virtual table.  This routine needs to create
3422 ** a query plan for each invocation and compute an estimated cost for that
3423 ** plan.
3424 **
3425 ** There are two hidden parameters that act as arguments to the table-valued
3426 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3427 ** is available and bit 1 is set if "wholeline" is available.
3428 */
3429 static int completionBestIndex(
3430   sqlite3_vtab *tab,
3431   sqlite3_index_info *pIdxInfo
3432 ){
3433   int i;                 /* Loop over constraints */
3434   int idxNum = 0;        /* The query plan bitmask */
3435   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3436   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3437   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3438   const struct sqlite3_index_constraint *pConstraint;
3439 
3440   (void)(tab);    /* Unused parameter */
3441   pConstraint = pIdxInfo->aConstraint;
3442   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3443     if( pConstraint->usable==0 ) continue;
3444     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3445     switch( pConstraint->iColumn ){
3446       case COMPLETION_COLUMN_PREFIX:
3447         prefixIdx = i;
3448         idxNum |= 1;
3449         break;
3450       case COMPLETION_COLUMN_WHOLELINE:
3451         wholelineIdx = i;
3452         idxNum |= 2;
3453         break;
3454     }
3455   }
3456   if( prefixIdx>=0 ){
3457     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3458     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3459   }
3460   if( wholelineIdx>=0 ){
3461     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3462     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3463   }
3464   pIdxInfo->idxNum = idxNum;
3465   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3466   pIdxInfo->estimatedRows = 500 - 100*nArg;
3467   return SQLITE_OK;
3468 }
3469 
3470 /*
3471 ** This following structure defines all the methods for the
3472 ** completion virtual table.
3473 */
3474 static sqlite3_module completionModule = {
3475   0,                         /* iVersion */
3476   0,                         /* xCreate */
3477   completionConnect,         /* xConnect */
3478   completionBestIndex,       /* xBestIndex */
3479   completionDisconnect,      /* xDisconnect */
3480   0,                         /* xDestroy */
3481   completionOpen,            /* xOpen - open a cursor */
3482   completionClose,           /* xClose - close a cursor */
3483   completionFilter,          /* xFilter - configure scan constraints */
3484   completionNext,            /* xNext - advance a cursor */
3485   completionEof,             /* xEof - check for end of scan */
3486   completionColumn,          /* xColumn - read data */
3487   completionRowid,           /* xRowid - read data */
3488   0,                         /* xUpdate */
3489   0,                         /* xBegin */
3490   0,                         /* xSync */
3491   0,                         /* xCommit */
3492   0,                         /* xRollback */
3493   0,                         /* xFindMethod */
3494   0,                         /* xRename */
3495   0,                         /* xSavepoint */
3496   0,                         /* xRelease */
3497   0,                         /* xRollbackTo */
3498   0                          /* xShadowName */
3499 };
3500 
3501 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3502 
3503 int sqlite3CompletionVtabInit(sqlite3 *db){
3504   int rc = SQLITE_OK;
3505 #ifndef SQLITE_OMIT_VIRTUALTABLE
3506   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3507 #endif
3508   return rc;
3509 }
3510 
3511 #ifdef _WIN32
3512 
3513 #endif
3514 int sqlite3_completion_init(
3515   sqlite3 *db,
3516   char **pzErrMsg,
3517   const sqlite3_api_routines *pApi
3518 ){
3519   int rc = SQLITE_OK;
3520   SQLITE_EXTENSION_INIT2(pApi);
3521   (void)(pzErrMsg);  /* Unused parameter */
3522 #ifndef SQLITE_OMIT_VIRTUALTABLE
3523   rc = sqlite3CompletionVtabInit(db);
3524 #endif
3525   return rc;
3526 }
3527 
3528 /************************* End ../ext/misc/completion.c ********************/
3529 /************************* Begin ../ext/misc/appendvfs.c ******************/
3530 /*
3531 ** 2017-10-20
3532 **
3533 ** The author disclaims copyright to this source code.  In place of
3534 ** a legal notice, here is a blessing:
3535 **
3536 **    May you do good and not evil.
3537 **    May you find forgiveness for yourself and forgive others.
3538 **    May you share freely, never taking more than you give.
3539 **
3540 ******************************************************************************
3541 **
3542 ** This file implements a VFS shim that allows an SQLite database to be
3543 ** appended onto the end of some other file, such as an executable.
3544 **
3545 ** A special record must appear at the end of the file that identifies the
3546 ** file as an appended database and provides an offset to page 1.  For
3547 ** best performance page 1 should be located at a disk page boundary, though
3548 ** that is not required.
3549 **
3550 ** When opening a database using this VFS, the connection might treat
3551 ** the file as an ordinary SQLite database, or it might treat is as a
3552 ** database appended onto some other file.  Here are the rules:
3553 **
3554 **  (1)  When opening a new empty file, that file is treated as an ordinary
3555 **       database.
3556 **
3557 **  (2)  When opening a file that begins with the standard SQLite prefix
3558 **       string "SQLite format 3", that file is treated as an ordinary
3559 **       database.
3560 **
3561 **  (3)  When opening a file that ends with the appendvfs trailer string
3562 **       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3563 **       database.
3564 **
3565 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3566 **       set, then a new database is appended to the already existing file.
3567 **
3568 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3569 **
3570 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3571 ** the file containing the database is limited to 1GB.  This VFS will refuse
3572 ** to read or write past the 1GB mark.  This restriction might be lifted in
3573 ** future versions.  For now, if you need a large database, then keep the
3574 ** database in a separate file.
3575 **
3576 ** If the file being opened is not an appended database, then this shim is
3577 ** a pass-through into the default underlying VFS.
3578 **/
3579 /* #include "sqlite3ext.h" */
3580 SQLITE_EXTENSION_INIT1
3581 #include <string.h>
3582 #include <assert.h>
3583 
3584 /* The append mark at the end of the database is:
3585 **
3586 **     Start-Of-SQLite3-NNNNNNNN
3587 **     123456789 123456789 12345
3588 **
3589 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3590 ** the offset to page 1.
3591 */
3592 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3593 #define APND_MARK_PREFIX_SZ  17
3594 #define APND_MARK_SIZE       25
3595 
3596 /*
3597 ** Maximum size of the combined prefix + database + append-mark.  This
3598 ** must be less than 0x40000000 to avoid locking issues on Windows.
3599 */
3600 #define APND_MAX_SIZE  (65536*15259)
3601 
3602 /*
3603 ** Forward declaration of objects used by this utility
3604 */
3605 typedef struct sqlite3_vfs ApndVfs;
3606 typedef struct ApndFile ApndFile;
3607 
3608 /* Access to a lower-level VFS that (might) implement dynamic loading,
3609 ** access to randomness, etc.
3610 */
3611 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3612 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3613 
3614 /* An open file */
3615 struct ApndFile {
3616   sqlite3_file base;              /* IO methods */
3617   sqlite3_int64 iPgOne;           /* File offset to page 1 */
3618   sqlite3_int64 iMark;            /* Start of the append-mark */
3619 };
3620 
3621 /*
3622 ** Methods for ApndFile
3623 */
3624 static int apndClose(sqlite3_file*);
3625 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3626 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3627 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3628 static int apndSync(sqlite3_file*, int flags);
3629 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3630 static int apndLock(sqlite3_file*, int);
3631 static int apndUnlock(sqlite3_file*, int);
3632 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3633 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3634 static int apndSectorSize(sqlite3_file*);
3635 static int apndDeviceCharacteristics(sqlite3_file*);
3636 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3637 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3638 static void apndShmBarrier(sqlite3_file*);
3639 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3640 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3641 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3642 
3643 /*
3644 ** Methods for ApndVfs
3645 */
3646 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3647 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3648 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3649 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3650 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3651 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3652 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3653 static void apndDlClose(sqlite3_vfs*, void*);
3654 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3655 static int apndSleep(sqlite3_vfs*, int microseconds);
3656 static int apndCurrentTime(sqlite3_vfs*, double*);
3657 static int apndGetLastError(sqlite3_vfs*, int, char *);
3658 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3659 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3660 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3661 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3662 
3663 static sqlite3_vfs apnd_vfs = {
3664   3,                            /* iVersion (set when registered) */
3665   0,                            /* szOsFile (set when registered) */
3666   1024,                         /* mxPathname */
3667   0,                            /* pNext */
3668   "apndvfs",                    /* zName */
3669   0,                            /* pAppData (set when registered) */
3670   apndOpen,                     /* xOpen */
3671   apndDelete,                   /* xDelete */
3672   apndAccess,                   /* xAccess */
3673   apndFullPathname,             /* xFullPathname */
3674   apndDlOpen,                   /* xDlOpen */
3675   apndDlError,                  /* xDlError */
3676   apndDlSym,                    /* xDlSym */
3677   apndDlClose,                  /* xDlClose */
3678   apndRandomness,               /* xRandomness */
3679   apndSleep,                    /* xSleep */
3680   apndCurrentTime,              /* xCurrentTime */
3681   apndGetLastError,             /* xGetLastError */
3682   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3683   apndSetSystemCall,            /* xSetSystemCall */
3684   apndGetSystemCall,            /* xGetSystemCall */
3685   apndNextSystemCall            /* xNextSystemCall */
3686 };
3687 
3688 static const sqlite3_io_methods apnd_io_methods = {
3689   3,                              /* iVersion */
3690   apndClose,                      /* xClose */
3691   apndRead,                       /* xRead */
3692   apndWrite,                      /* xWrite */
3693   apndTruncate,                   /* xTruncate */
3694   apndSync,                       /* xSync */
3695   apndFileSize,                   /* xFileSize */
3696   apndLock,                       /* xLock */
3697   apndUnlock,                     /* xUnlock */
3698   apndCheckReservedLock,          /* xCheckReservedLock */
3699   apndFileControl,                /* xFileControl */
3700   apndSectorSize,                 /* xSectorSize */
3701   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3702   apndShmMap,                     /* xShmMap */
3703   apndShmLock,                    /* xShmLock */
3704   apndShmBarrier,                 /* xShmBarrier */
3705   apndShmUnmap,                   /* xShmUnmap */
3706   apndFetch,                      /* xFetch */
3707   apndUnfetch                     /* xUnfetch */
3708 };
3709 
3710 
3711 
3712 /*
3713 ** Close an apnd-file.
3714 */
3715 static int apndClose(sqlite3_file *pFile){
3716   pFile = ORIGFILE(pFile);
3717   return pFile->pMethods->xClose(pFile);
3718 }
3719 
3720 /*
3721 ** Read data from an apnd-file.
3722 */
3723 static int apndRead(
3724   sqlite3_file *pFile,
3725   void *zBuf,
3726   int iAmt,
3727   sqlite_int64 iOfst
3728 ){
3729   ApndFile *p = (ApndFile *)pFile;
3730   pFile = ORIGFILE(pFile);
3731   return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3732 }
3733 
3734 /*
3735 ** Add the append-mark onto the end of the file.
3736 */
3737 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3738   int i;
3739   unsigned char a[APND_MARK_SIZE];
3740   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3741   for(i=0; i<8; i++){
3742     a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3743   }
3744   return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3745 }
3746 
3747 /*
3748 ** Write data to an apnd-file.
3749 */
3750 static int apndWrite(
3751   sqlite3_file *pFile,
3752   const void *zBuf,
3753   int iAmt,
3754   sqlite_int64 iOfst
3755 ){
3756   int rc;
3757   ApndFile *p = (ApndFile *)pFile;
3758   pFile = ORIGFILE(pFile);
3759   if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3760   rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3761   if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
3762     sqlite3_int64 sz = 0;
3763     rc = pFile->pMethods->xFileSize(pFile, &sz);
3764     if( rc==SQLITE_OK ){
3765       p->iMark = sz - APND_MARK_SIZE;
3766       if( iOfst + iAmt + p->iPgOne > p->iMark ){
3767         p->iMark = p->iPgOne + iOfst + iAmt;
3768         rc = apndWriteMark(p, pFile);
3769       }
3770     }
3771   }
3772   return rc;
3773 }
3774 
3775 /*
3776 ** Truncate an apnd-file.
3777 */
3778 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3779   int rc;
3780   ApndFile *p = (ApndFile *)pFile;
3781   pFile = ORIGFILE(pFile);
3782   rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3783   if( rc==SQLITE_OK ){
3784     p->iMark = p->iPgOne+size;
3785     rc = apndWriteMark(p, pFile);
3786   }
3787   return rc;
3788 }
3789 
3790 /*
3791 ** Sync an apnd-file.
3792 */
3793 static int apndSync(sqlite3_file *pFile, int flags){
3794   pFile = ORIGFILE(pFile);
3795   return pFile->pMethods->xSync(pFile, flags);
3796 }
3797 
3798 /*
3799 ** Return the current file-size of an apnd-file.
3800 */
3801 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3802   ApndFile *p = (ApndFile *)pFile;
3803   int rc;
3804   pFile = ORIGFILE(p);
3805   rc = pFile->pMethods->xFileSize(pFile, pSize);
3806   if( rc==SQLITE_OK && p->iPgOne ){
3807     *pSize -= p->iPgOne + APND_MARK_SIZE;
3808   }
3809   return rc;
3810 }
3811 
3812 /*
3813 ** Lock an apnd-file.
3814 */
3815 static int apndLock(sqlite3_file *pFile, int eLock){
3816   pFile = ORIGFILE(pFile);
3817   return pFile->pMethods->xLock(pFile, eLock);
3818 }
3819 
3820 /*
3821 ** Unlock an apnd-file.
3822 */
3823 static int apndUnlock(sqlite3_file *pFile, int eLock){
3824   pFile = ORIGFILE(pFile);
3825   return pFile->pMethods->xUnlock(pFile, eLock);
3826 }
3827 
3828 /*
3829 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3830 */
3831 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3832   pFile = ORIGFILE(pFile);
3833   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3834 }
3835 
3836 /*
3837 ** File control method. For custom operations on an apnd-file.
3838 */
3839 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3840   ApndFile *p = (ApndFile *)pFile;
3841   int rc;
3842   pFile = ORIGFILE(pFile);
3843   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3844   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3845     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3846   }
3847   return rc;
3848 }
3849 
3850 /*
3851 ** Return the sector-size in bytes for an apnd-file.
3852 */
3853 static int apndSectorSize(sqlite3_file *pFile){
3854   pFile = ORIGFILE(pFile);
3855   return pFile->pMethods->xSectorSize(pFile);
3856 }
3857 
3858 /*
3859 ** Return the device characteristic flags supported by an apnd-file.
3860 */
3861 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3862   pFile = ORIGFILE(pFile);
3863   return pFile->pMethods->xDeviceCharacteristics(pFile);
3864 }
3865 
3866 /* Create a shared memory file mapping */
3867 static int apndShmMap(
3868   sqlite3_file *pFile,
3869   int iPg,
3870   int pgsz,
3871   int bExtend,
3872   void volatile **pp
3873 ){
3874   pFile = ORIGFILE(pFile);
3875   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3876 }
3877 
3878 /* Perform locking on a shared-memory segment */
3879 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3880   pFile = ORIGFILE(pFile);
3881   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3882 }
3883 
3884 /* Memory barrier operation on shared memory */
3885 static void apndShmBarrier(sqlite3_file *pFile){
3886   pFile = ORIGFILE(pFile);
3887   pFile->pMethods->xShmBarrier(pFile);
3888 }
3889 
3890 /* Unmap a shared memory segment */
3891 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3892   pFile = ORIGFILE(pFile);
3893   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3894 }
3895 
3896 /* Fetch a page of a memory-mapped file */
3897 static int apndFetch(
3898   sqlite3_file *pFile,
3899   sqlite3_int64 iOfst,
3900   int iAmt,
3901   void **pp
3902 ){
3903   ApndFile *p = (ApndFile *)pFile;
3904   pFile = ORIGFILE(pFile);
3905   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3906 }
3907 
3908 /* Release a memory-mapped page */
3909 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3910   ApndFile *p = (ApndFile *)pFile;
3911   pFile = ORIGFILE(pFile);
3912   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3913 }
3914 
3915 /*
3916 ** Check to see if the file is an ordinary SQLite database file.
3917 */
3918 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3919   int rc;
3920   char zHdr[16];
3921   static const char aSqliteHdr[] = "SQLite format 3";
3922   if( sz<512 ) return 0;
3923   rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3924   if( rc ) return 0;
3925   return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3926 }
3927 
3928 /*
3929 ** Try to read the append-mark off the end of a file.  Return the
3930 ** start of the appended database if the append-mark is present.  If
3931 ** there is no append-mark, return -1;
3932 */
3933 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3934   int rc, i;
3935   sqlite3_int64 iMark;
3936   unsigned char a[APND_MARK_SIZE];
3937 
3938   if( sz<=APND_MARK_SIZE ) return -1;
3939   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3940   if( rc ) return -1;
3941   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3942   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3943   for(i=1; i<8; i++){
3944     iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3945   }
3946   return iMark;
3947 }
3948 
3949 /*
3950 ** Open an apnd file handle.
3951 */
3952 static int apndOpen(
3953   sqlite3_vfs *pVfs,
3954   const char *zName,
3955   sqlite3_file *pFile,
3956   int flags,
3957   int *pOutFlags
3958 ){
3959   ApndFile *p;
3960   sqlite3_file *pSubFile;
3961   sqlite3_vfs *pSubVfs;
3962   int rc;
3963   sqlite3_int64 sz;
3964   pSubVfs = ORIGVFS(pVfs);
3965   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3966     return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3967   }
3968   p = (ApndFile*)pFile;
3969   memset(p, 0, sizeof(*p));
3970   pSubFile = ORIGFILE(pFile);
3971   p->base.pMethods = &apnd_io_methods;
3972   rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3973   if( rc ) goto apnd_open_done;
3974   rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3975   if( rc ){
3976     pSubFile->pMethods->xClose(pSubFile);
3977     goto apnd_open_done;
3978   }
3979   if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3980     memmove(pFile, pSubFile, pSubVfs->szOsFile);
3981     return SQLITE_OK;
3982   }
3983   p->iMark = 0;
3984   p->iPgOne = apndReadMark(sz, pFile);
3985   if( p->iPgOne>0 ){
3986     return SQLITE_OK;
3987   }
3988   if( (flags & SQLITE_OPEN_CREATE)==0 ){
3989     pSubFile->pMethods->xClose(pSubFile);
3990     rc = SQLITE_CANTOPEN;
3991   }
3992   p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3993 apnd_open_done:
3994   if( rc ) pFile->pMethods = 0;
3995   return rc;
3996 }
3997 
3998 /*
3999 ** All other VFS methods are pass-thrus.
4000 */
4001 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4002   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4003 }
4004 static int apndAccess(
4005   sqlite3_vfs *pVfs,
4006   const char *zPath,
4007   int flags,
4008   int *pResOut
4009 ){
4010   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4011 }
4012 static int apndFullPathname(
4013   sqlite3_vfs *pVfs,
4014   const char *zPath,
4015   int nOut,
4016   char *zOut
4017 ){
4018   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4019 }
4020 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4021   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4022 }
4023 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4024   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4025 }
4026 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4027   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4028 }
4029 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4030   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4031 }
4032 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4033   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4034 }
4035 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4036   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4037 }
4038 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4039   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4040 }
4041 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4042   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4043 }
4044 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4045   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4046 }
4047 static int apndSetSystemCall(
4048   sqlite3_vfs *pVfs,
4049   const char *zName,
4050   sqlite3_syscall_ptr pCall
4051 ){
4052   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4053 }
4054 static sqlite3_syscall_ptr apndGetSystemCall(
4055   sqlite3_vfs *pVfs,
4056   const char *zName
4057 ){
4058   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4059 }
4060 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4061   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4062 }
4063 
4064 
4065 #ifdef _WIN32
4066 
4067 #endif
4068 /*
4069 ** This routine is called when the extension is loaded.
4070 ** Register the new VFS.
4071 */
4072 int sqlite3_appendvfs_init(
4073   sqlite3 *db,
4074   char **pzErrMsg,
4075   const sqlite3_api_routines *pApi
4076 ){
4077   int rc = SQLITE_OK;
4078   sqlite3_vfs *pOrig;
4079   SQLITE_EXTENSION_INIT2(pApi);
4080   (void)pzErrMsg;
4081   (void)db;
4082   pOrig = sqlite3_vfs_find(0);
4083   apnd_vfs.iVersion = pOrig->iVersion;
4084   apnd_vfs.pAppData = pOrig;
4085   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4086   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4087 #ifdef APPENDVFS_TEST
4088   if( rc==SQLITE_OK ){
4089     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4090   }
4091 #endif
4092   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4093   return rc;
4094 }
4095 
4096 /************************* End ../ext/misc/appendvfs.c ********************/
4097 /************************* Begin ../ext/misc/memtrace.c ******************/
4098 /*
4099 ** 2019-01-21
4100 **
4101 ** The author disclaims copyright to this source code.  In place of
4102 ** a legal notice, here is a blessing:
4103 **
4104 **    May you do good and not evil.
4105 **    May you find forgiveness for yourself and forgive others.
4106 **    May you share freely, never taking more than you give.
4107 **
4108 *************************************************************************
4109 **
4110 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4111 ** mechanism to add a tracing layer on top of SQLite.  If this extension
4112 ** is registered prior to sqlite3_initialize(), it will cause all memory
4113 ** allocation activities to be logged on standard output, or to some other
4114 ** FILE specified by the initializer.
4115 **
4116 ** This file needs to be compiled into the application that uses it.
4117 **
4118 ** This extension is used to implement the --memtrace option of the
4119 ** command-line shell.
4120 */
4121 #include <assert.h>
4122 #include <string.h>
4123 #include <stdio.h>
4124 
4125 /* The original memory allocation routines */
4126 static sqlite3_mem_methods memtraceBase;
4127 static FILE *memtraceOut;
4128 
4129 /* Methods that trace memory allocations */
4130 static void *memtraceMalloc(int n){
4131   if( memtraceOut ){
4132     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4133             memtraceBase.xRoundup(n));
4134   }
4135   return memtraceBase.xMalloc(n);
4136 }
4137 static void memtraceFree(void *p){
4138   if( p==0 ) return;
4139   if( memtraceOut ){
4140     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4141   }
4142   memtraceBase.xFree(p);
4143 }
4144 static void *memtraceRealloc(void *p, int n){
4145   if( p==0 ) return memtraceMalloc(n);
4146   if( n==0 ){
4147     memtraceFree(p);
4148     return 0;
4149   }
4150   if( memtraceOut ){
4151     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4152             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4153   }
4154   return memtraceBase.xRealloc(p, n);
4155 }
4156 static int memtraceSize(void *p){
4157   return memtraceBase.xSize(p);
4158 }
4159 static int memtraceRoundup(int n){
4160   return memtraceBase.xRoundup(n);
4161 }
4162 static int memtraceInit(void *p){
4163   return memtraceBase.xInit(p);
4164 }
4165 static void memtraceShutdown(void *p){
4166   memtraceBase.xShutdown(p);
4167 }
4168 
4169 /* The substitute memory allocator */
4170 static sqlite3_mem_methods ersaztMethods = {
4171   memtraceMalloc,
4172   memtraceFree,
4173   memtraceRealloc,
4174   memtraceSize,
4175   memtraceRoundup,
4176   memtraceInit,
4177   memtraceShutdown,
4178   0
4179 };
4180 
4181 /* Begin tracing memory allocations to out. */
4182 int sqlite3MemTraceActivate(FILE *out){
4183   int rc = SQLITE_OK;
4184   if( memtraceBase.xMalloc==0 ){
4185     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4186     if( rc==SQLITE_OK ){
4187       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4188     }
4189   }
4190   memtraceOut = out;
4191   return rc;
4192 }
4193 
4194 /* Deactivate memory tracing */
4195 int sqlite3MemTraceDeactivate(void){
4196   int rc = SQLITE_OK;
4197   if( memtraceBase.xMalloc!=0 ){
4198     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4199     if( rc==SQLITE_OK ){
4200       memset(&memtraceBase, 0, sizeof(memtraceBase));
4201     }
4202   }
4203   memtraceOut = 0;
4204   return rc;
4205 }
4206 
4207 /************************* End ../ext/misc/memtrace.c ********************/
4208 #ifdef SQLITE_HAVE_ZLIB
4209 /************************* Begin ../ext/misc/zipfile.c ******************/
4210 /*
4211 ** 2017-12-26
4212 **
4213 ** The author disclaims copyright to this source code.  In place of
4214 ** a legal notice, here is a blessing:
4215 **
4216 **    May you do good and not evil.
4217 **    May you find forgiveness for yourself and forgive others.
4218 **    May you share freely, never taking more than you give.
4219 **
4220 ******************************************************************************
4221 **
4222 ** This file implements a virtual table for reading and writing ZIP archive
4223 ** files.
4224 **
4225 ** Usage example:
4226 **
4227 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4228 **
4229 ** Current limitations:
4230 **
4231 **    *  No support for encryption
4232 **    *  No support for ZIP archives spanning multiple files
4233 **    *  No support for zip64 extensions
4234 **    *  Only the "inflate/deflate" (zlib) compression method is supported
4235 */
4236 /* #include "sqlite3ext.h" */
4237 SQLITE_EXTENSION_INIT1
4238 #include <stdio.h>
4239 #include <string.h>
4240 #include <assert.h>
4241 
4242 #include <zlib.h>
4243 
4244 #ifndef SQLITE_OMIT_VIRTUALTABLE
4245 
4246 #ifndef SQLITE_AMALGAMATION
4247 
4248 /* typedef sqlite3_int64 i64; */
4249 /* typedef unsigned char u8; */
4250 typedef unsigned short u16;
4251 typedef unsigned long u32;
4252 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4253 
4254 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4255 # define ALWAYS(X)      (1)
4256 # define NEVER(X)       (0)
4257 #elif !defined(NDEBUG)
4258 # define ALWAYS(X)      ((X)?1:(assert(0),0))
4259 # define NEVER(X)       ((X)?(assert(0),1):0)
4260 #else
4261 # define ALWAYS(X)      (X)
4262 # define NEVER(X)       (X)
4263 #endif
4264 
4265 #endif   /* SQLITE_AMALGAMATION */
4266 
4267 /*
4268 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4269 **
4270 ** In some ways it would be better to obtain these values from system
4271 ** header files. But, the dependency is undesirable and (a) these
4272 ** have been stable for decades, (b) the values are part of POSIX and
4273 ** are also made explicit in [man stat], and (c) are part of the
4274 ** file format for zip archives.
4275 */
4276 #ifndef S_IFDIR
4277 # define S_IFDIR 0040000
4278 #endif
4279 #ifndef S_IFREG
4280 # define S_IFREG 0100000
4281 #endif
4282 #ifndef S_IFLNK
4283 # define S_IFLNK 0120000
4284 #endif
4285 
4286 static const char ZIPFILE_SCHEMA[] =
4287   "CREATE TABLE y("
4288     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
4289     "mode,"              /* 1: POSIX mode for file */
4290     "mtime,"             /* 2: Last modification time (secs since 1970)*/
4291     "sz,"                /* 3: Size of object */
4292     "rawdata,"           /* 4: Raw data */
4293     "data,"              /* 5: Uncompressed data */
4294     "method,"            /* 6: Compression method (integer) */
4295     "z HIDDEN"           /* 7: Name of zip file */
4296   ") WITHOUT ROWID;";
4297 
4298 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
4299 #define ZIPFILE_BUFFER_SIZE (64*1024)
4300 
4301 
4302 /*
4303 ** Magic numbers used to read and write zip files.
4304 **
4305 ** ZIPFILE_NEWENTRY_MADEBY:
4306 **   Use this value for the "version-made-by" field in new zip file
4307 **   entries. The upper byte indicates "unix", and the lower byte
4308 **   indicates that the zip file matches pkzip specification 3.0.
4309 **   This is what info-zip seems to do.
4310 **
4311 ** ZIPFILE_NEWENTRY_REQUIRED:
4312 **   Value for "version-required-to-extract" field of new entries.
4313 **   Version 2.0 is required to support folders and deflate compression.
4314 **
4315 ** ZIPFILE_NEWENTRY_FLAGS:
4316 **   Value for "general-purpose-bit-flags" field of new entries. Bit
4317 **   11 means "utf-8 filename and comment".
4318 **
4319 ** ZIPFILE_SIGNATURE_CDS:
4320 **   First 4 bytes of a valid CDS record.
4321 **
4322 ** ZIPFILE_SIGNATURE_LFH:
4323 **   First 4 bytes of a valid LFH record.
4324 **
4325 ** ZIPFILE_SIGNATURE_EOCD
4326 **   First 4 bytes of a valid EOCD record.
4327 */
4328 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
4329 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
4330 #define ZIPFILE_NEWENTRY_REQUIRED 20
4331 #define ZIPFILE_NEWENTRY_FLAGS    0x800
4332 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
4333 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
4334 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
4335 
4336 /*
4337 ** The sizes of the fixed-size part of each of the three main data
4338 ** structures in a zip archive.
4339 */
4340 #define ZIPFILE_LFH_FIXED_SZ      30
4341 #define ZIPFILE_EOCD_FIXED_SZ     22
4342 #define ZIPFILE_CDS_FIXED_SZ      46
4343 
4344 /*
4345 *** 4.3.16  End of central directory record:
4346 ***
4347 ***   end of central dir signature    4 bytes  (0x06054b50)
4348 ***   number of this disk             2 bytes
4349 ***   number of the disk with the
4350 ***   start of the central directory  2 bytes
4351 ***   total number of entries in the
4352 ***   central directory on this disk  2 bytes
4353 ***   total number of entries in
4354 ***   the central directory           2 bytes
4355 ***   size of the central directory   4 bytes
4356 ***   offset of start of central
4357 ***   directory with respect to
4358 ***   the starting disk number        4 bytes
4359 ***   .ZIP file comment length        2 bytes
4360 ***   .ZIP file comment       (variable size)
4361 */
4362 typedef struct ZipfileEOCD ZipfileEOCD;
4363 struct ZipfileEOCD {
4364   u16 iDisk;
4365   u16 iFirstDisk;
4366   u16 nEntry;
4367   u16 nEntryTotal;
4368   u32 nSize;
4369   u32 iOffset;
4370 };
4371 
4372 /*
4373 *** 4.3.12  Central directory structure:
4374 ***
4375 *** ...
4376 ***
4377 ***   central file header signature   4 bytes  (0x02014b50)
4378 ***   version made by                 2 bytes
4379 ***   version needed to extract       2 bytes
4380 ***   general purpose bit flag        2 bytes
4381 ***   compression method              2 bytes
4382 ***   last mod file time              2 bytes
4383 ***   last mod file date              2 bytes
4384 ***   crc-32                          4 bytes
4385 ***   compressed size                 4 bytes
4386 ***   uncompressed size               4 bytes
4387 ***   file name length                2 bytes
4388 ***   extra field length              2 bytes
4389 ***   file comment length             2 bytes
4390 ***   disk number start               2 bytes
4391 ***   internal file attributes        2 bytes
4392 ***   external file attributes        4 bytes
4393 ***   relative offset of local header 4 bytes
4394 */
4395 typedef struct ZipfileCDS ZipfileCDS;
4396 struct ZipfileCDS {
4397   u16 iVersionMadeBy;
4398   u16 iVersionExtract;
4399   u16 flags;
4400   u16 iCompression;
4401   u16 mTime;
4402   u16 mDate;
4403   u32 crc32;
4404   u32 szCompressed;
4405   u32 szUncompressed;
4406   u16 nFile;
4407   u16 nExtra;
4408   u16 nComment;
4409   u16 iDiskStart;
4410   u16 iInternalAttr;
4411   u32 iExternalAttr;
4412   u32 iOffset;
4413   char *zFile;                    /* Filename (sqlite3_malloc()) */
4414 };
4415 
4416 /*
4417 *** 4.3.7  Local file header:
4418 ***
4419 ***   local file header signature     4 bytes  (0x04034b50)
4420 ***   version needed to extract       2 bytes
4421 ***   general purpose bit flag        2 bytes
4422 ***   compression method              2 bytes
4423 ***   last mod file time              2 bytes
4424 ***   last mod file date              2 bytes
4425 ***   crc-32                          4 bytes
4426 ***   compressed size                 4 bytes
4427 ***   uncompressed size               4 bytes
4428 ***   file name length                2 bytes
4429 ***   extra field length              2 bytes
4430 ***
4431 */
4432 typedef struct ZipfileLFH ZipfileLFH;
4433 struct ZipfileLFH {
4434   u16 iVersionExtract;
4435   u16 flags;
4436   u16 iCompression;
4437   u16 mTime;
4438   u16 mDate;
4439   u32 crc32;
4440   u32 szCompressed;
4441   u32 szUncompressed;
4442   u16 nFile;
4443   u16 nExtra;
4444 };
4445 
4446 typedef struct ZipfileEntry ZipfileEntry;
4447 struct ZipfileEntry {
4448   ZipfileCDS cds;            /* Parsed CDS record */
4449   u32 mUnixTime;             /* Modification time, in UNIX format */
4450   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
4451   i64 iDataOff;              /* Offset to data in file (if aData==0) */
4452   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
4453   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
4454 };
4455 
4456 /*
4457 ** Cursor type for zipfile tables.
4458 */
4459 typedef struct ZipfileCsr ZipfileCsr;
4460 struct ZipfileCsr {
4461   sqlite3_vtab_cursor base;  /* Base class - must be first */
4462   i64 iId;                   /* Cursor ID */
4463   u8 bEof;                   /* True when at EOF */
4464   u8 bNoop;                  /* If next xNext() call is no-op */
4465 
4466   /* Used outside of write transactions */
4467   FILE *pFile;               /* Zip file */
4468   i64 iNextOff;              /* Offset of next record in central directory */
4469   ZipfileEOCD eocd;          /* Parse of central directory record */
4470 
4471   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
4472   ZipfileEntry *pCurrent;    /* Current entry */
4473   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
4474 };
4475 
4476 typedef struct ZipfileTab ZipfileTab;
4477 struct ZipfileTab {
4478   sqlite3_vtab base;         /* Base class - must be first */
4479   char *zFile;               /* Zip file this table accesses (may be NULL) */
4480   sqlite3 *db;               /* Host database connection */
4481   u8 *aBuffer;               /* Temporary buffer used for various tasks */
4482 
4483   ZipfileCsr *pCsrList;      /* List of cursors */
4484   i64 iNextCsrid;
4485 
4486   /* The following are used by write transactions only */
4487   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4488   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
4489   FILE *pWriteFd;            /* File handle open on zip archive */
4490   i64 szCurrent;             /* Current size of zip archive */
4491   i64 szOrig;                /* Size of archive at start of transaction */
4492 };
4493 
4494 /*
4495 ** Set the error message contained in context ctx to the results of
4496 ** vprintf(zFmt, ...).
4497 */
4498 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4499   char *zMsg = 0;
4500   va_list ap;
4501   va_start(ap, zFmt);
4502   zMsg = sqlite3_vmprintf(zFmt, ap);
4503   sqlite3_result_error(ctx, zMsg, -1);
4504   sqlite3_free(zMsg);
4505   va_end(ap);
4506 }
4507 
4508 /*
4509 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4510 ** is not quoted, do nothing.
4511 */
4512 static void zipfileDequote(char *zIn){
4513   char q = zIn[0];
4514   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4515     int iIn = 1;
4516     int iOut = 0;
4517     if( q=='[' ) q = ']';
4518     while( ALWAYS(zIn[iIn]) ){
4519       char c = zIn[iIn++];
4520       if( c==q && zIn[iIn++]!=q ) break;
4521       zIn[iOut++] = c;
4522     }
4523     zIn[iOut] = '\0';
4524   }
4525 }
4526 
4527 /*
4528 ** Construct a new ZipfileTab virtual table object.
4529 **
4530 **   argv[0]   -> module name  ("zipfile")
4531 **   argv[1]   -> database name
4532 **   argv[2]   -> table name
4533 **   argv[...] -> "column name" and other module argument fields.
4534 */
4535 static int zipfileConnect(
4536   sqlite3 *db,
4537   void *pAux,
4538   int argc, const char *const*argv,
4539   sqlite3_vtab **ppVtab,
4540   char **pzErr
4541 ){
4542   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4543   int nFile = 0;
4544   const char *zFile = 0;
4545   ZipfileTab *pNew = 0;
4546   int rc;
4547 
4548   /* If the table name is not "zipfile", require that the argument be
4549   ** specified. This stops zipfile tables from being created as:
4550   **
4551   **   CREATE VIRTUAL TABLE zzz USING zipfile();
4552   **
4553   ** It does not prevent:
4554   **
4555   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
4556   */
4557   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4558   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4559     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4560     return SQLITE_ERROR;
4561   }
4562 
4563   if( argc>3 ){
4564     zFile = argv[3];
4565     nFile = (int)strlen(zFile)+1;
4566   }
4567 
4568   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4569   if( rc==SQLITE_OK ){
4570     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
4571     if( pNew==0 ) return SQLITE_NOMEM;
4572     memset(pNew, 0, nByte+nFile);
4573     pNew->db = db;
4574     pNew->aBuffer = (u8*)&pNew[1];
4575     if( zFile ){
4576       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4577       memcpy(pNew->zFile, zFile, nFile);
4578       zipfileDequote(pNew->zFile);
4579     }
4580   }
4581   *ppVtab = (sqlite3_vtab*)pNew;
4582   return rc;
4583 }
4584 
4585 /*
4586 ** Free the ZipfileEntry structure indicated by the only argument.
4587 */
4588 static void zipfileEntryFree(ZipfileEntry *p){
4589   if( p ){
4590     sqlite3_free(p->cds.zFile);
4591     sqlite3_free(p);
4592   }
4593 }
4594 
4595 /*
4596 ** Release resources that should be freed at the end of a write
4597 ** transaction.
4598 */
4599 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4600   ZipfileEntry *pEntry;
4601   ZipfileEntry *pNext;
4602 
4603   if( pTab->pWriteFd ){
4604     fclose(pTab->pWriteFd);
4605     pTab->pWriteFd = 0;
4606   }
4607   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4608     pNext = pEntry->pNext;
4609     zipfileEntryFree(pEntry);
4610   }
4611   pTab->pFirstEntry = 0;
4612   pTab->pLastEntry = 0;
4613   pTab->szCurrent = 0;
4614   pTab->szOrig = 0;
4615 }
4616 
4617 /*
4618 ** This method is the destructor for zipfile vtab objects.
4619 */
4620 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4621   zipfileCleanupTransaction((ZipfileTab*)pVtab);
4622   sqlite3_free(pVtab);
4623   return SQLITE_OK;
4624 }
4625 
4626 /*
4627 ** Constructor for a new ZipfileCsr object.
4628 */
4629 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4630   ZipfileTab *pTab = (ZipfileTab*)p;
4631   ZipfileCsr *pCsr;
4632   pCsr = sqlite3_malloc(sizeof(*pCsr));
4633   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4634   if( pCsr==0 ){
4635     return SQLITE_NOMEM;
4636   }
4637   memset(pCsr, 0, sizeof(*pCsr));
4638   pCsr->iId = ++pTab->iNextCsrid;
4639   pCsr->pCsrNext = pTab->pCsrList;
4640   pTab->pCsrList = pCsr;
4641   return SQLITE_OK;
4642 }
4643 
4644 /*
4645 ** Reset a cursor back to the state it was in when first returned
4646 ** by zipfileOpen().
4647 */
4648 static void zipfileResetCursor(ZipfileCsr *pCsr){
4649   ZipfileEntry *p;
4650   ZipfileEntry *pNext;
4651 
4652   pCsr->bEof = 0;
4653   if( pCsr->pFile ){
4654     fclose(pCsr->pFile);
4655     pCsr->pFile = 0;
4656     zipfileEntryFree(pCsr->pCurrent);
4657     pCsr->pCurrent = 0;
4658   }
4659 
4660   for(p=pCsr->pFreeEntry; p; p=pNext){
4661     pNext = p->pNext;
4662     zipfileEntryFree(p);
4663   }
4664 }
4665 
4666 /*
4667 ** Destructor for an ZipfileCsr.
4668 */
4669 static int zipfileClose(sqlite3_vtab_cursor *cur){
4670   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4671   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4672   ZipfileCsr **pp;
4673   zipfileResetCursor(pCsr);
4674 
4675   /* Remove this cursor from the ZipfileTab.pCsrList list. */
4676   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4677   *pp = pCsr->pCsrNext;
4678 
4679   sqlite3_free(pCsr);
4680   return SQLITE_OK;
4681 }
4682 
4683 /*
4684 ** Set the error message for the virtual table associated with cursor
4685 ** pCsr to the results of vprintf(zFmt, ...).
4686 */
4687 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4688   va_list ap;
4689   va_start(ap, zFmt);
4690   sqlite3_free(pTab->base.zErrMsg);
4691   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4692   va_end(ap);
4693 }
4694 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4695   va_list ap;
4696   va_start(ap, zFmt);
4697   sqlite3_free(pCsr->base.pVtab->zErrMsg);
4698   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4699   va_end(ap);
4700 }
4701 
4702 /*
4703 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4704 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4705 ** otherwise.
4706 **
4707 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4708 ** to an English language error message. It is the responsibility of the
4709 ** caller to eventually free this buffer using
4710 ** sqlite3_free().
4711 */
4712 static int zipfileReadData(
4713   FILE *pFile,                    /* Read from this file */
4714   u8 *aRead,                      /* Read into this buffer */
4715   int nRead,                      /* Number of bytes to read */
4716   i64 iOff,                       /* Offset to read from */
4717   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
4718 ){
4719   size_t n;
4720   fseek(pFile, (long)iOff, SEEK_SET);
4721   n = fread(aRead, 1, nRead, pFile);
4722   if( (int)n!=nRead ){
4723     *pzErrmsg = sqlite3_mprintf("error in fread()");
4724     return SQLITE_ERROR;
4725   }
4726   return SQLITE_OK;
4727 }
4728 
4729 static int zipfileAppendData(
4730   ZipfileTab *pTab,
4731   const u8 *aWrite,
4732   int nWrite
4733 ){
4734   size_t n;
4735   fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4736   n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4737   if( (int)n!=nWrite ){
4738     pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4739     return SQLITE_ERROR;
4740   }
4741   pTab->szCurrent += nWrite;
4742   return SQLITE_OK;
4743 }
4744 
4745 /*
4746 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4747 */
4748 static u16 zipfileGetU16(const u8 *aBuf){
4749   return (aBuf[1] << 8) + aBuf[0];
4750 }
4751 
4752 /*
4753 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4754 */
4755 static u32 zipfileGetU32(const u8 *aBuf){
4756   return ((u32)(aBuf[3]) << 24)
4757        + ((u32)(aBuf[2]) << 16)
4758        + ((u32)(aBuf[1]) <<  8)
4759        + ((u32)(aBuf[0]) <<  0);
4760 }
4761 
4762 /*
4763 ** Write a 16-bit little endiate integer into buffer aBuf.
4764 */
4765 static void zipfilePutU16(u8 *aBuf, u16 val){
4766   aBuf[0] = val & 0xFF;
4767   aBuf[1] = (val>>8) & 0xFF;
4768 }
4769 
4770 /*
4771 ** Write a 32-bit little endiate integer into buffer aBuf.
4772 */
4773 static void zipfilePutU32(u8 *aBuf, u32 val){
4774   aBuf[0] = val & 0xFF;
4775   aBuf[1] = (val>>8) & 0xFF;
4776   aBuf[2] = (val>>16) & 0xFF;
4777   aBuf[3] = (val>>24) & 0xFF;
4778 }
4779 
4780 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4781 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4782 
4783 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4784 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4785 
4786 /*
4787 ** Magic numbers used to read CDS records.
4788 */
4789 #define ZIPFILE_CDS_NFILE_OFF        28
4790 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4791 
4792 /*
4793 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4794 ** if the record is not well-formed, or SQLITE_OK otherwise.
4795 */
4796 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4797   u8 *aRead = aBuf;
4798   u32 sig = zipfileRead32(aRead);
4799   int rc = SQLITE_OK;
4800   if( sig!=ZIPFILE_SIGNATURE_CDS ){
4801     rc = SQLITE_ERROR;
4802   }else{
4803     pCDS->iVersionMadeBy = zipfileRead16(aRead);
4804     pCDS->iVersionExtract = zipfileRead16(aRead);
4805     pCDS->flags = zipfileRead16(aRead);
4806     pCDS->iCompression = zipfileRead16(aRead);
4807     pCDS->mTime = zipfileRead16(aRead);
4808     pCDS->mDate = zipfileRead16(aRead);
4809     pCDS->crc32 = zipfileRead32(aRead);
4810     pCDS->szCompressed = zipfileRead32(aRead);
4811     pCDS->szUncompressed = zipfileRead32(aRead);
4812     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4813     pCDS->nFile = zipfileRead16(aRead);
4814     pCDS->nExtra = zipfileRead16(aRead);
4815     pCDS->nComment = zipfileRead16(aRead);
4816     pCDS->iDiskStart = zipfileRead16(aRead);
4817     pCDS->iInternalAttr = zipfileRead16(aRead);
4818     pCDS->iExternalAttr = zipfileRead32(aRead);
4819     pCDS->iOffset = zipfileRead32(aRead);
4820     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4821   }
4822 
4823   return rc;
4824 }
4825 
4826 /*
4827 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4828 ** if the record is not well-formed, or SQLITE_OK otherwise.
4829 */
4830 static int zipfileReadLFH(
4831   u8 *aBuffer,
4832   ZipfileLFH *pLFH
4833 ){
4834   u8 *aRead = aBuffer;
4835   int rc = SQLITE_OK;
4836 
4837   u32 sig = zipfileRead32(aRead);
4838   if( sig!=ZIPFILE_SIGNATURE_LFH ){
4839     rc = SQLITE_ERROR;
4840   }else{
4841     pLFH->iVersionExtract = zipfileRead16(aRead);
4842     pLFH->flags = zipfileRead16(aRead);
4843     pLFH->iCompression = zipfileRead16(aRead);
4844     pLFH->mTime = zipfileRead16(aRead);
4845     pLFH->mDate = zipfileRead16(aRead);
4846     pLFH->crc32 = zipfileRead32(aRead);
4847     pLFH->szCompressed = zipfileRead32(aRead);
4848     pLFH->szUncompressed = zipfileRead32(aRead);
4849     pLFH->nFile = zipfileRead16(aRead);
4850     pLFH->nExtra = zipfileRead16(aRead);
4851   }
4852   return rc;
4853 }
4854 
4855 
4856 /*
4857 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4858 ** Scan through this buffer to find an "extra-timestamp" field. If one
4859 ** exists, extract the 32-bit modification-timestamp from it and store
4860 ** the value in output parameter *pmTime.
4861 **
4862 ** Zero is returned if no extra-timestamp record could be found (and so
4863 ** *pmTime is left unchanged), or non-zero otherwise.
4864 **
4865 ** The general format of an extra field is:
4866 **
4867 **   Header ID    2 bytes
4868 **   Data Size    2 bytes
4869 **   Data         N bytes
4870 */
4871 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4872   int ret = 0;
4873   u8 *p = aExtra;
4874   u8 *pEnd = &aExtra[nExtra];
4875 
4876   while( p<pEnd ){
4877     u16 id = zipfileRead16(p);
4878     u16 nByte = zipfileRead16(p);
4879 
4880     switch( id ){
4881       case ZIPFILE_EXTRA_TIMESTAMP: {
4882         u8 b = p[0];
4883         if( b & 0x01 ){     /* 0x01 -> modtime is present */
4884           *pmTime = zipfileGetU32(&p[1]);
4885           ret = 1;
4886         }
4887         break;
4888       }
4889     }
4890 
4891     p += nByte;
4892   }
4893   return ret;
4894 }
4895 
4896 /*
4897 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4898 ** fields of the CDS structure passed as the only argument to a 32-bit
4899 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4900 **
4901 ** "Standard" MS-DOS time format:
4902 **
4903 **   File modification time:
4904 **     Bits 00-04: seconds divided by 2
4905 **     Bits 05-10: minute
4906 **     Bits 11-15: hour
4907 **   File modification date:
4908 **     Bits 00-04: day
4909 **     Bits 05-08: month (1-12)
4910 **     Bits 09-15: years from 1980
4911 **
4912 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4913 */
4914 static u32 zipfileMtime(ZipfileCDS *pCDS){
4915   int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4916   int M = ((pCDS->mDate >> 5) & 0x0F);
4917   int D = (pCDS->mDate & 0x1F);
4918   int B = -13;
4919 
4920   int sec = (pCDS->mTime & 0x1F)*2;
4921   int min = (pCDS->mTime >> 5) & 0x3F;
4922   int hr = (pCDS->mTime >> 11) & 0x1F;
4923   i64 JD;
4924 
4925   /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4926 
4927   /* Calculate the JD in seconds for noon on the day in question */
4928   if( M<3 ){
4929     Y = Y-1;
4930     M = M+12;
4931   }
4932   JD = (i64)(24*60*60) * (
4933       (int)(365.25 * (Y + 4716))
4934     + (int)(30.6001 * (M + 1))
4935     + D + B - 1524
4936   );
4937 
4938   /* Correct the JD for the time within the day */
4939   JD += (hr-12) * 3600 + min * 60 + sec;
4940 
4941   /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4942   return (u32)(JD - (i64)(24405875) * 24*60*6);
4943 }
4944 
4945 /*
4946 ** The opposite of zipfileMtime(). This function populates the mTime and
4947 ** mDate fields of the CDS structure passed as the first argument according
4948 ** to the UNIX timestamp value passed as the second.
4949 */
4950 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4951   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4952   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4953 
4954   int A, B, C, D, E;
4955   int yr, mon, day;
4956   int hr, min, sec;
4957 
4958   A = (int)((JD - 1867216.25)/36524.25);
4959   A = (int)(JD + 1 + A - (A/4));
4960   B = A + 1524;
4961   C = (int)((B - 122.1)/365.25);
4962   D = (36525*(C&32767))/100;
4963   E = (int)((B-D)/30.6001);
4964 
4965   day = B - D - (int)(30.6001*E);
4966   mon = (E<14 ? E-1 : E-13);
4967   yr = mon>2 ? C-4716 : C-4715;
4968 
4969   hr = (mUnixTime % (24*60*60)) / (60*60);
4970   min = (mUnixTime % (60*60)) / 60;
4971   sec = (mUnixTime % 60);
4972 
4973   if( yr>=1980 ){
4974     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4975     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4976   }else{
4977     pCds->mDate = pCds->mTime = 0;
4978   }
4979 
4980   assert( mUnixTime<315507600
4981        || mUnixTime==zipfileMtime(pCds)
4982        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
4983        /* || (mUnixTime % 2) */
4984   );
4985 }
4986 
4987 /*
4988 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4989 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4990 ** then pFile is a file-handle open on a zip file. In either case, this
4991 ** function creates a ZipfileEntry object based on the zip archive entry
4992 ** for which the CDS record is at offset iOff.
4993 **
4994 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4995 ** the new object. Otherwise, an SQLite error code is returned and the
4996 ** final value of (*ppEntry) undefined.
4997 */
4998 static int zipfileGetEntry(
4999   ZipfileTab *pTab,               /* Store any error message here */
5000   const u8 *aBlob,                /* Pointer to in-memory file image */
5001   int nBlob,                      /* Size of aBlob[] in bytes */
5002   FILE *pFile,                    /* If aBlob==0, read from this file */
5003   i64 iOff,                       /* Offset of CDS record */
5004   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
5005 ){
5006   u8 *aRead;
5007   char **pzErr = &pTab->base.zErrMsg;
5008   int rc = SQLITE_OK;
5009 
5010   if( aBlob==0 ){
5011     aRead = pTab->aBuffer;
5012     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
5013   }else{
5014     aRead = (u8*)&aBlob[iOff];
5015   }
5016 
5017   if( rc==SQLITE_OK ){
5018     sqlite3_int64 nAlloc;
5019     ZipfileEntry *pNew;
5020 
5021     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
5022     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
5023     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
5024 
5025     nAlloc = sizeof(ZipfileEntry) + nExtra;
5026     if( aBlob ){
5027       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
5028     }
5029 
5030     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
5031     if( pNew==0 ){
5032       rc = SQLITE_NOMEM;
5033     }else{
5034       memset(pNew, 0, sizeof(ZipfileEntry));
5035       rc = zipfileReadCDS(aRead, &pNew->cds);
5036       if( rc!=SQLITE_OK ){
5037         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
5038       }else if( aBlob==0 ){
5039         rc = zipfileReadData(
5040             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
5041         );
5042       }else{
5043         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
5044       }
5045     }
5046 
5047     if( rc==SQLITE_OK ){
5048       u32 *pt = &pNew->mUnixTime;
5049       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
5050       pNew->aExtra = (u8*)&pNew[1];
5051       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
5052       if( pNew->cds.zFile==0 ){
5053         rc = SQLITE_NOMEM;
5054       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
5055         pNew->mUnixTime = zipfileMtime(&pNew->cds);
5056       }
5057     }
5058 
5059     if( rc==SQLITE_OK ){
5060       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
5061       ZipfileLFH lfh;
5062       if( pFile ){
5063         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
5064       }else{
5065         aRead = (u8*)&aBlob[pNew->cds.iOffset];
5066       }
5067 
5068       rc = zipfileReadLFH(aRead, &lfh);
5069       if( rc==SQLITE_OK ){
5070         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
5071         pNew->iDataOff += lfh.nFile + lfh.nExtra;
5072         if( aBlob && pNew->cds.szCompressed ){
5073           pNew->aData = &pNew->aExtra[nExtra];
5074           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
5075         }
5076       }else{
5077         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
5078             (int)pNew->cds.iOffset
5079         );
5080       }
5081     }
5082 
5083     if( rc!=SQLITE_OK ){
5084       zipfileEntryFree(pNew);
5085     }else{
5086       *ppEntry = pNew;
5087     }
5088   }
5089 
5090   return rc;
5091 }
5092 
5093 /*
5094 ** Advance an ZipfileCsr to its next row of output.
5095 */
5096 static int zipfileNext(sqlite3_vtab_cursor *cur){
5097   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5098   int rc = SQLITE_OK;
5099 
5100   if( pCsr->pFile ){
5101     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
5102     zipfileEntryFree(pCsr->pCurrent);
5103     pCsr->pCurrent = 0;
5104     if( pCsr->iNextOff>=iEof ){
5105       pCsr->bEof = 1;
5106     }else{
5107       ZipfileEntry *p = 0;
5108       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
5109       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
5110       if( rc==SQLITE_OK ){
5111         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
5112         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
5113       }
5114       pCsr->pCurrent = p;
5115     }
5116   }else{
5117     if( !pCsr->bNoop ){
5118       pCsr->pCurrent = pCsr->pCurrent->pNext;
5119     }
5120     if( pCsr->pCurrent==0 ){
5121       pCsr->bEof = 1;
5122     }
5123   }
5124 
5125   pCsr->bNoop = 0;
5126   return rc;
5127 }
5128 
5129 static void zipfileFree(void *p) {
5130   sqlite3_free(p);
5131 }
5132 
5133 /*
5134 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
5135 ** size is nOut bytes. This function uncompresses the data and sets the
5136 ** return value in context pCtx to the result (a blob).
5137 **
5138 ** If an error occurs, an error code is left in pCtx instead.
5139 */
5140 static void zipfileInflate(
5141   sqlite3_context *pCtx,          /* Store result here */
5142   const u8 *aIn,                  /* Compressed data */
5143   int nIn,                        /* Size of buffer aIn[] in bytes */
5144   int nOut                        /* Expected output size */
5145 ){
5146   u8 *aRes = sqlite3_malloc(nOut);
5147   if( aRes==0 ){
5148     sqlite3_result_error_nomem(pCtx);
5149   }else{
5150     int err;
5151     z_stream str;
5152     memset(&str, 0, sizeof(str));
5153 
5154     str.next_in = (Byte*)aIn;
5155     str.avail_in = nIn;
5156     str.next_out = (Byte*)aRes;
5157     str.avail_out = nOut;
5158 
5159     err = inflateInit2(&str, -15);
5160     if( err!=Z_OK ){
5161       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
5162     }else{
5163       err = inflate(&str, Z_NO_FLUSH);
5164       if( err!=Z_STREAM_END ){
5165         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
5166       }else{
5167         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
5168         aRes = 0;
5169       }
5170     }
5171     sqlite3_free(aRes);
5172     inflateEnd(&str);
5173   }
5174 }
5175 
5176 /*
5177 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
5178 ** compresses it and sets (*ppOut) to point to a buffer containing the
5179 ** compressed data. The caller is responsible for eventually calling
5180 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
5181 ** is set to the size of buffer (*ppOut) in bytes.
5182 **
5183 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
5184 ** code is returned and an error message left in virtual-table handle
5185 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5186 ** case.
5187 */
5188 static int zipfileDeflate(
5189   const u8 *aIn, int nIn,         /* Input */
5190   u8 **ppOut, int *pnOut,         /* Output */
5191   char **pzErr                    /* OUT: Error message */
5192 ){
5193   sqlite3_int64 nAlloc = compressBound(nIn);
5194   u8 *aOut;
5195   int rc = SQLITE_OK;
5196 
5197   aOut = (u8*)sqlite3_malloc64(nAlloc);
5198   if( aOut==0 ){
5199     rc = SQLITE_NOMEM;
5200   }else{
5201     int res;
5202     z_stream str;
5203     memset(&str, 0, sizeof(str));
5204     str.next_in = (Bytef*)aIn;
5205     str.avail_in = nIn;
5206     str.next_out = aOut;
5207     str.avail_out = nAlloc;
5208 
5209     deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5210     res = deflate(&str, Z_FINISH);
5211 
5212     if( res==Z_STREAM_END ){
5213       *ppOut = aOut;
5214       *pnOut = (int)str.total_out;
5215     }else{
5216       sqlite3_free(aOut);
5217       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5218       rc = SQLITE_ERROR;
5219     }
5220     deflateEnd(&str);
5221   }
5222 
5223   return rc;
5224 }
5225 
5226 
5227 /*
5228 ** Return values of columns for the row at which the series_cursor
5229 ** is currently pointing.
5230 */
5231 static int zipfileColumn(
5232   sqlite3_vtab_cursor *cur,   /* The cursor */
5233   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5234   int i                       /* Which column to return */
5235 ){
5236   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5237   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5238   int rc = SQLITE_OK;
5239   switch( i ){
5240     case 0:   /* name */
5241       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5242       break;
5243     case 1:   /* mode */
5244       /* TODO: Whether or not the following is correct surely depends on
5245       ** the platform on which the archive was created.  */
5246       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5247       break;
5248     case 2: { /* mtime */
5249       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5250       break;
5251     }
5252     case 3: { /* sz */
5253       if( sqlite3_vtab_nochange(ctx)==0 ){
5254         sqlite3_result_int64(ctx, pCDS->szUncompressed);
5255       }
5256       break;
5257     }
5258     case 4:   /* rawdata */
5259       if( sqlite3_vtab_nochange(ctx) ) break;
5260     case 5: { /* data */
5261       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5262         int sz = pCDS->szCompressed;
5263         int szFinal = pCDS->szUncompressed;
5264         if( szFinal>0 ){
5265           u8 *aBuf;
5266           u8 *aFree = 0;
5267           if( pCsr->pCurrent->aData ){
5268             aBuf = pCsr->pCurrent->aData;
5269           }else{
5270             aBuf = aFree = sqlite3_malloc64(sz);
5271             if( aBuf==0 ){
5272               rc = SQLITE_NOMEM;
5273             }else{
5274               FILE *pFile = pCsr->pFile;
5275               if( pFile==0 ){
5276                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5277               }
5278               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5279                   &pCsr->base.pVtab->zErrMsg
5280               );
5281             }
5282           }
5283           if( rc==SQLITE_OK ){
5284             if( i==5 && pCDS->iCompression ){
5285               zipfileInflate(ctx, aBuf, sz, szFinal);
5286             }else{
5287               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5288             }
5289           }
5290           sqlite3_free(aFree);
5291         }else{
5292           /* Figure out if this is a directory or a zero-sized file. Consider
5293           ** it to be a directory either if the mode suggests so, or if
5294           ** the final character in the name is '/'.  */
5295           u32 mode = pCDS->iExternalAttr >> 16;
5296           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5297             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5298           }
5299         }
5300       }
5301       break;
5302     }
5303     case 6:   /* method */
5304       sqlite3_result_int(ctx, pCDS->iCompression);
5305       break;
5306     default:  /* z */
5307       assert( i==7 );
5308       sqlite3_result_int64(ctx, pCsr->iId);
5309       break;
5310   }
5311 
5312   return rc;
5313 }
5314 
5315 /*
5316 ** Return TRUE if the cursor is at EOF.
5317 */
5318 static int zipfileEof(sqlite3_vtab_cursor *cur){
5319   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5320   return pCsr->bEof;
5321 }
5322 
5323 /*
5324 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5325 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5326 ** is guaranteed to be a file-handle open on a zip file.
5327 **
5328 ** This function attempts to locate the EOCD record within the zip archive
5329 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5330 ** returned if successful. Otherwise, an SQLite error code is returned and
5331 ** an English language error message may be left in virtual-table pTab.
5332 */
5333 static int zipfileReadEOCD(
5334   ZipfileTab *pTab,               /* Return errors here */
5335   const u8 *aBlob,                /* Pointer to in-memory file image */
5336   int nBlob,                      /* Size of aBlob[] in bytes */
5337   FILE *pFile,                    /* Read from this file if aBlob==0 */
5338   ZipfileEOCD *pEOCD              /* Object to populate */
5339 ){
5340   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
5341   int nRead;                      /* Bytes to read from file */
5342   int rc = SQLITE_OK;
5343 
5344   if( aBlob==0 ){
5345     i64 iOff;                     /* Offset to read from */
5346     i64 szFile;                   /* Total size of file in bytes */
5347     fseek(pFile, 0, SEEK_END);
5348     szFile = (i64)ftell(pFile);
5349     if( szFile==0 ){
5350       memset(pEOCD, 0, sizeof(ZipfileEOCD));
5351       return SQLITE_OK;
5352     }
5353     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5354     iOff = szFile - nRead;
5355     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5356   }else{
5357     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5358     aRead = (u8*)&aBlob[nBlob-nRead];
5359   }
5360 
5361   if( rc==SQLITE_OK ){
5362     int i;
5363 
5364     /* Scan backwards looking for the signature bytes */
5365     for(i=nRead-20; i>=0; i--){
5366       if( aRead[i]==0x50 && aRead[i+1]==0x4b
5367        && aRead[i+2]==0x05 && aRead[i+3]==0x06
5368       ){
5369         break;
5370       }
5371     }
5372     if( i<0 ){
5373       pTab->base.zErrMsg = sqlite3_mprintf(
5374           "cannot find end of central directory record"
5375       );
5376       return SQLITE_ERROR;
5377     }
5378 
5379     aRead += i+4;
5380     pEOCD->iDisk = zipfileRead16(aRead);
5381     pEOCD->iFirstDisk = zipfileRead16(aRead);
5382     pEOCD->nEntry = zipfileRead16(aRead);
5383     pEOCD->nEntryTotal = zipfileRead16(aRead);
5384     pEOCD->nSize = zipfileRead32(aRead);
5385     pEOCD->iOffset = zipfileRead32(aRead);
5386   }
5387 
5388   return rc;
5389 }
5390 
5391 /*
5392 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5393 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5394 ** to the end of the list. Otherwise, it is added to the list immediately
5395 ** before pBefore (which is guaranteed to be a part of said list).
5396 */
5397 static void zipfileAddEntry(
5398   ZipfileTab *pTab,
5399   ZipfileEntry *pBefore,
5400   ZipfileEntry *pNew
5401 ){
5402   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5403   assert( pNew->pNext==0 );
5404   if( pBefore==0 ){
5405     if( pTab->pFirstEntry==0 ){
5406       pTab->pFirstEntry = pTab->pLastEntry = pNew;
5407     }else{
5408       assert( pTab->pLastEntry->pNext==0 );
5409       pTab->pLastEntry->pNext = pNew;
5410       pTab->pLastEntry = pNew;
5411     }
5412   }else{
5413     ZipfileEntry **pp;
5414     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5415     pNew->pNext = pBefore;
5416     *pp = pNew;
5417   }
5418 }
5419 
5420 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5421   ZipfileEOCD eocd;
5422   int rc;
5423   int i;
5424   i64 iOff;
5425 
5426   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5427   iOff = eocd.iOffset;
5428   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5429     ZipfileEntry *pNew = 0;
5430     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5431 
5432     if( rc==SQLITE_OK ){
5433       zipfileAddEntry(pTab, 0, pNew);
5434       iOff += ZIPFILE_CDS_FIXED_SZ;
5435       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5436     }
5437   }
5438   return rc;
5439 }
5440 
5441 /*
5442 ** xFilter callback.
5443 */
5444 static int zipfileFilter(
5445   sqlite3_vtab_cursor *cur,
5446   int idxNum, const char *idxStr,
5447   int argc, sqlite3_value **argv
5448 ){
5449   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5450   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5451   const char *zFile = 0;          /* Zip file to scan */
5452   int rc = SQLITE_OK;             /* Return Code */
5453   int bInMemory = 0;              /* True for an in-memory zipfile */
5454 
5455   zipfileResetCursor(pCsr);
5456 
5457   if( pTab->zFile ){
5458     zFile = pTab->zFile;
5459   }else if( idxNum==0 ){
5460     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5461     return SQLITE_ERROR;
5462   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5463     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5464     int nBlob = sqlite3_value_bytes(argv[0]);
5465     assert( pTab->pFirstEntry==0 );
5466     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5467     pCsr->pFreeEntry = pTab->pFirstEntry;
5468     pTab->pFirstEntry = pTab->pLastEntry = 0;
5469     if( rc!=SQLITE_OK ) return rc;
5470     bInMemory = 1;
5471   }else{
5472     zFile = (const char*)sqlite3_value_text(argv[0]);
5473   }
5474 
5475   if( 0==pTab->pWriteFd && 0==bInMemory ){
5476     pCsr->pFile = fopen(zFile, "rb");
5477     if( pCsr->pFile==0 ){
5478       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5479       rc = SQLITE_ERROR;
5480     }else{
5481       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5482       if( rc==SQLITE_OK ){
5483         if( pCsr->eocd.nEntry==0 ){
5484           pCsr->bEof = 1;
5485         }else{
5486           pCsr->iNextOff = pCsr->eocd.iOffset;
5487           rc = zipfileNext(cur);
5488         }
5489       }
5490     }
5491   }else{
5492     pCsr->bNoop = 1;
5493     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5494     rc = zipfileNext(cur);
5495   }
5496 
5497   return rc;
5498 }
5499 
5500 /*
5501 ** xBestIndex callback.
5502 */
5503 static int zipfileBestIndex(
5504   sqlite3_vtab *tab,
5505   sqlite3_index_info *pIdxInfo
5506 ){
5507   int i;
5508   int idx = -1;
5509   int unusable = 0;
5510 
5511   for(i=0; i<pIdxInfo->nConstraint; i++){
5512     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5513     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5514     if( pCons->usable==0 ){
5515       unusable = 1;
5516     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5517       idx = i;
5518     }
5519   }
5520   if( idx>=0 ){
5521     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
5522     pIdxInfo->aConstraintUsage[idx].omit = 1;
5523     pIdxInfo->estimatedCost = 1000.0;
5524     pIdxInfo->idxNum = 1;
5525   }else if( unusable ){
5526     return SQLITE_CONSTRAINT;
5527   }
5528   return SQLITE_OK;
5529 }
5530 
5531 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5532   ZipfileEntry *pNew;
5533   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5534   if( pNew ){
5535     memset(pNew, 0, sizeof(ZipfileEntry));
5536     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5537     if( pNew->cds.zFile==0 ){
5538       sqlite3_free(pNew);
5539       pNew = 0;
5540     }
5541   }
5542   return pNew;
5543 }
5544 
5545 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5546   ZipfileCDS *pCds = &pEntry->cds;
5547   u8 *a = aBuf;
5548 
5549   pCds->nExtra = 9;
5550 
5551   /* Write the LFH itself */
5552   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5553   zipfileWrite16(a, pCds->iVersionExtract);
5554   zipfileWrite16(a, pCds->flags);
5555   zipfileWrite16(a, pCds->iCompression);
5556   zipfileWrite16(a, pCds->mTime);
5557   zipfileWrite16(a, pCds->mDate);
5558   zipfileWrite32(a, pCds->crc32);
5559   zipfileWrite32(a, pCds->szCompressed);
5560   zipfileWrite32(a, pCds->szUncompressed);
5561   zipfileWrite16(a, (u16)pCds->nFile);
5562   zipfileWrite16(a, pCds->nExtra);
5563   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5564 
5565   /* Add the file name */
5566   memcpy(a, pCds->zFile, (int)pCds->nFile);
5567   a += (int)pCds->nFile;
5568 
5569   /* The "extra" data */
5570   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5571   zipfileWrite16(a, 5);
5572   *a++ = 0x01;
5573   zipfileWrite32(a, pEntry->mUnixTime);
5574 
5575   return a-aBuf;
5576 }
5577 
5578 static int zipfileAppendEntry(
5579   ZipfileTab *pTab,
5580   ZipfileEntry *pEntry,
5581   const u8 *pData,
5582   int nData
5583 ){
5584   u8 *aBuf = pTab->aBuffer;
5585   int nBuf;
5586   int rc;
5587 
5588   nBuf = zipfileSerializeLFH(pEntry, aBuf);
5589   rc = zipfileAppendData(pTab, aBuf, nBuf);
5590   if( rc==SQLITE_OK ){
5591     pEntry->iDataOff = pTab->szCurrent;
5592     rc = zipfileAppendData(pTab, pData, nData);
5593   }
5594 
5595   return rc;
5596 }
5597 
5598 static int zipfileGetMode(
5599   sqlite3_value *pVal,
5600   int bIsDir,                     /* If true, default to directory */
5601   u32 *pMode,                     /* OUT: Mode value */
5602   char **pzErr                    /* OUT: Error message */
5603 ){
5604   const char *z = (const char*)sqlite3_value_text(pVal);
5605   u32 mode = 0;
5606   if( z==0 ){
5607     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5608   }else if( z[0]>='0' && z[0]<='9' ){
5609     mode = (unsigned int)sqlite3_value_int(pVal);
5610   }else{
5611     const char zTemplate[11] = "-rwxrwxrwx";
5612     int i;
5613     if( strlen(z)!=10 ) goto parse_error;
5614     switch( z[0] ){
5615       case '-': mode |= S_IFREG; break;
5616       case 'd': mode |= S_IFDIR; break;
5617       case 'l': mode |= S_IFLNK; break;
5618       default: goto parse_error;
5619     }
5620     for(i=1; i<10; i++){
5621       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5622       else if( z[i]!='-' ) goto parse_error;
5623     }
5624   }
5625   if( ((mode & S_IFDIR)==0)==bIsDir ){
5626     /* The "mode" attribute is a directory, but data has been specified.
5627     ** Or vice-versa - no data but "mode" is a file or symlink.  */
5628     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5629     return SQLITE_CONSTRAINT;
5630   }
5631   *pMode = mode;
5632   return SQLITE_OK;
5633 
5634  parse_error:
5635   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5636   return SQLITE_ERROR;
5637 }
5638 
5639 /*
5640 ** Both (const char*) arguments point to nul-terminated strings. Argument
5641 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5642 ** identical, ignoring any trailing '/' character in either path.  */
5643 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5644   int nA = (int)strlen(zA);
5645   if( zA[nA-1]=='/' ) nA--;
5646   if( zB[nB-1]=='/' ) nB--;
5647   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5648   return 1;
5649 }
5650 
5651 static int zipfileBegin(sqlite3_vtab *pVtab){
5652   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5653   int rc = SQLITE_OK;
5654 
5655   assert( pTab->pWriteFd==0 );
5656 
5657   /* Open a write fd on the file. Also load the entire central directory
5658   ** structure into memory. During the transaction any new file data is
5659   ** appended to the archive file, but the central directory is accumulated
5660   ** in main-memory until the transaction is committed.  */
5661   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5662   if( pTab->pWriteFd==0 ){
5663     pTab->base.zErrMsg = sqlite3_mprintf(
5664         "zipfile: failed to open file %s for writing", pTab->zFile
5665         );
5666     rc = SQLITE_ERROR;
5667   }else{
5668     fseek(pTab->pWriteFd, 0, SEEK_END);
5669     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5670     rc = zipfileLoadDirectory(pTab, 0, 0);
5671   }
5672 
5673   if( rc!=SQLITE_OK ){
5674     zipfileCleanupTransaction(pTab);
5675   }
5676 
5677   return rc;
5678 }
5679 
5680 /*
5681 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5682 ** time(2)).
5683 */
5684 static u32 zipfileTime(void){
5685   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5686   u32 ret;
5687   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5688     i64 ms;
5689     pVfs->xCurrentTimeInt64(pVfs, &ms);
5690     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5691   }else{
5692     double day;
5693     pVfs->xCurrentTime(pVfs, &day);
5694     ret = (u32)((day - 2440587.5) * 86400);
5695   }
5696   return ret;
5697 }
5698 
5699 /*
5700 ** Return a 32-bit timestamp in UNIX epoch format.
5701 **
5702 ** If the value passed as the only argument is either NULL or an SQL NULL,
5703 ** return the current time. Otherwise, return the value stored in (*pVal)
5704 ** cast to a 32-bit unsigned integer.
5705 */
5706 static u32 zipfileGetTime(sqlite3_value *pVal){
5707   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5708     return zipfileTime();
5709   }
5710   return (u32)sqlite3_value_int64(pVal);
5711 }
5712 
5713 /*
5714 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5715 ** linked list.  Remove it from the list and free the object.
5716 */
5717 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5718   if( pOld ){
5719     ZipfileEntry **pp;
5720     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5721     *pp = (*pp)->pNext;
5722     zipfileEntryFree(pOld);
5723   }
5724 }
5725 
5726 /*
5727 ** xUpdate method.
5728 */
5729 static int zipfileUpdate(
5730   sqlite3_vtab *pVtab,
5731   int nVal,
5732   sqlite3_value **apVal,
5733   sqlite_int64 *pRowid
5734 ){
5735   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5736   int rc = SQLITE_OK;             /* Return Code */
5737   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
5738 
5739   u32 mode = 0;                   /* Mode for new entry */
5740   u32 mTime = 0;                  /* Modification time for new entry */
5741   i64 sz = 0;                     /* Uncompressed size */
5742   const char *zPath = 0;          /* Path for new entry */
5743   int nPath = 0;                  /* strlen(zPath) */
5744   const u8 *pData = 0;            /* Pointer to buffer containing content */
5745   int nData = 0;                  /* Size of pData buffer in bytes */
5746   int iMethod = 0;                /* Compression method for new entry */
5747   u8 *pFree = 0;                  /* Free this */
5748   char *zFree = 0;                /* Also free this */
5749   ZipfileEntry *pOld = 0;
5750   ZipfileEntry *pOld2 = 0;
5751   int bUpdate = 0;                /* True for an update that modifies "name" */
5752   int bIsDir = 0;
5753   u32 iCrc32 = 0;
5754 
5755   if( pTab->pWriteFd==0 ){
5756     rc = zipfileBegin(pVtab);
5757     if( rc!=SQLITE_OK ) return rc;
5758   }
5759 
5760   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5761   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5762     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5763     int nDelete = (int)strlen(zDelete);
5764     if( nVal>1 ){
5765       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5766       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5767         bUpdate = 1;
5768       }
5769     }
5770     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5771       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5772         break;
5773       }
5774       assert( pOld->pNext );
5775     }
5776   }
5777 
5778   if( nVal>1 ){
5779     /* Check that "sz" and "rawdata" are both NULL: */
5780     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5781       zipfileTableErr(pTab, "sz must be NULL");
5782       rc = SQLITE_CONSTRAINT;
5783     }
5784     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5785       zipfileTableErr(pTab, "rawdata must be NULL");
5786       rc = SQLITE_CONSTRAINT;
5787     }
5788 
5789     if( rc==SQLITE_OK ){
5790       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5791         /* data=NULL. A directory */
5792         bIsDir = 1;
5793       }else{
5794         /* Value specified for "data", and possibly "method". This must be
5795         ** a regular file or a symlink. */
5796         const u8 *aIn = sqlite3_value_blob(apVal[7]);
5797         int nIn = sqlite3_value_bytes(apVal[7]);
5798         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5799 
5800         iMethod = sqlite3_value_int(apVal[8]);
5801         sz = nIn;
5802         pData = aIn;
5803         nData = nIn;
5804         if( iMethod!=0 && iMethod!=8 ){
5805           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5806           rc = SQLITE_CONSTRAINT;
5807         }else{
5808           if( bAuto || iMethod ){
5809             int nCmp;
5810             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5811             if( rc==SQLITE_OK ){
5812               if( iMethod || nCmp<nIn ){
5813                 iMethod = 8;
5814                 pData = pFree;
5815                 nData = nCmp;
5816               }
5817             }
5818           }
5819           iCrc32 = crc32(0, aIn, nIn);
5820         }
5821       }
5822     }
5823 
5824     if( rc==SQLITE_OK ){
5825       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5826     }
5827 
5828     if( rc==SQLITE_OK ){
5829       zPath = (const char*)sqlite3_value_text(apVal[2]);
5830       nPath = (int)strlen(zPath);
5831       mTime = zipfileGetTime(apVal[4]);
5832     }
5833 
5834     if( rc==SQLITE_OK && bIsDir ){
5835       /* For a directory, check that the last character in the path is a
5836       ** '/'. This appears to be required for compatibility with info-zip
5837       ** (the unzip command on unix). It does not create directories
5838       ** otherwise.  */
5839       if( zPath[nPath-1]!='/' ){
5840         zFree = sqlite3_mprintf("%s/", zPath);
5841         if( zFree==0 ){ rc = SQLITE_NOMEM; }
5842         zPath = (const char*)zFree;
5843         nPath++;
5844       }
5845     }
5846 
5847     /* Check that we're not inserting a duplicate entry -OR- updating an
5848     ** entry with a path, thereby making it into a duplicate. */
5849     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
5850       ZipfileEntry *p;
5851       for(p=pTab->pFirstEntry; p; p=p->pNext){
5852         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5853           switch( sqlite3_vtab_on_conflict(pTab->db) ){
5854             case SQLITE_IGNORE: {
5855               goto zipfile_update_done;
5856             }
5857             case SQLITE_REPLACE: {
5858               pOld2 = p;
5859               break;
5860             }
5861             default: {
5862               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5863               rc = SQLITE_CONSTRAINT;
5864               break;
5865             }
5866           }
5867           break;
5868         }
5869       }
5870     }
5871 
5872     if( rc==SQLITE_OK ){
5873       /* Create the new CDS record. */
5874       pNew = zipfileNewEntry(zPath);
5875       if( pNew==0 ){
5876         rc = SQLITE_NOMEM;
5877       }else{
5878         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5879         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5880         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5881         pNew->cds.iCompression = (u16)iMethod;
5882         zipfileMtimeToDos(&pNew->cds, mTime);
5883         pNew->cds.crc32 = iCrc32;
5884         pNew->cds.szCompressed = nData;
5885         pNew->cds.szUncompressed = (u32)sz;
5886         pNew->cds.iExternalAttr = (mode<<16);
5887         pNew->cds.iOffset = (u32)pTab->szCurrent;
5888         pNew->cds.nFile = (u16)nPath;
5889         pNew->mUnixTime = (u32)mTime;
5890         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5891         zipfileAddEntry(pTab, pOld, pNew);
5892       }
5893     }
5894   }
5895 
5896   if( rc==SQLITE_OK && (pOld || pOld2) ){
5897     ZipfileCsr *pCsr;
5898     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5899       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
5900         pCsr->pCurrent = pCsr->pCurrent->pNext;
5901         pCsr->bNoop = 1;
5902       }
5903     }
5904 
5905     zipfileRemoveEntryFromList(pTab, pOld);
5906     zipfileRemoveEntryFromList(pTab, pOld2);
5907   }
5908 
5909 zipfile_update_done:
5910   sqlite3_free(pFree);
5911   sqlite3_free(zFree);
5912   return rc;
5913 }
5914 
5915 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5916   u8 *a = aBuf;
5917   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5918   zipfileWrite16(a, p->iDisk);
5919   zipfileWrite16(a, p->iFirstDisk);
5920   zipfileWrite16(a, p->nEntry);
5921   zipfileWrite16(a, p->nEntryTotal);
5922   zipfileWrite32(a, p->nSize);
5923   zipfileWrite32(a, p->iOffset);
5924   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
5925 
5926   return a-aBuf;
5927 }
5928 
5929 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5930   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5931   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5932   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5933 }
5934 
5935 /*
5936 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5937 ** of bytes written.
5938 */
5939 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5940   u8 *a = aBuf;
5941   ZipfileCDS *pCDS = &pEntry->cds;
5942 
5943   if( pEntry->aExtra==0 ){
5944     pCDS->nExtra = 9;
5945   }
5946 
5947   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5948   zipfileWrite16(a, pCDS->iVersionMadeBy);
5949   zipfileWrite16(a, pCDS->iVersionExtract);
5950   zipfileWrite16(a, pCDS->flags);
5951   zipfileWrite16(a, pCDS->iCompression);
5952   zipfileWrite16(a, pCDS->mTime);
5953   zipfileWrite16(a, pCDS->mDate);
5954   zipfileWrite32(a, pCDS->crc32);
5955   zipfileWrite32(a, pCDS->szCompressed);
5956   zipfileWrite32(a, pCDS->szUncompressed);
5957   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5958   zipfileWrite16(a, pCDS->nFile);
5959   zipfileWrite16(a, pCDS->nExtra);
5960   zipfileWrite16(a, pCDS->nComment);
5961   zipfileWrite16(a, pCDS->iDiskStart);
5962   zipfileWrite16(a, pCDS->iInternalAttr);
5963   zipfileWrite32(a, pCDS->iExternalAttr);
5964   zipfileWrite32(a, pCDS->iOffset);
5965 
5966   memcpy(a, pCDS->zFile, pCDS->nFile);
5967   a += pCDS->nFile;
5968 
5969   if( pEntry->aExtra ){
5970     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5971     memcpy(a, pEntry->aExtra, n);
5972     a += n;
5973   }else{
5974     assert( pCDS->nExtra==9 );
5975     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5976     zipfileWrite16(a, 5);
5977     *a++ = 0x01;
5978     zipfileWrite32(a, pEntry->mUnixTime);
5979   }
5980 
5981   return a-aBuf;
5982 }
5983 
5984 static int zipfileCommit(sqlite3_vtab *pVtab){
5985   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5986   int rc = SQLITE_OK;
5987   if( pTab->pWriteFd ){
5988     i64 iOffset = pTab->szCurrent;
5989     ZipfileEntry *p;
5990     ZipfileEOCD eocd;
5991     int nEntry = 0;
5992 
5993     /* Write out all entries */
5994     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5995       int n = zipfileSerializeCDS(p, pTab->aBuffer);
5996       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
5997       nEntry++;
5998     }
5999 
6000     /* Write out the EOCD record */
6001     eocd.iDisk = 0;
6002     eocd.iFirstDisk = 0;
6003     eocd.nEntry = (u16)nEntry;
6004     eocd.nEntryTotal = (u16)nEntry;
6005     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
6006     eocd.iOffset = (u32)iOffset;
6007     rc = zipfileAppendEOCD(pTab, &eocd);
6008 
6009     zipfileCleanupTransaction(pTab);
6010   }
6011   return rc;
6012 }
6013 
6014 static int zipfileRollback(sqlite3_vtab *pVtab){
6015   return zipfileCommit(pVtab);
6016 }
6017 
6018 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
6019   ZipfileCsr *pCsr;
6020   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6021     if( iId==pCsr->iId ) break;
6022   }
6023   return pCsr;
6024 }
6025 
6026 static void zipfileFunctionCds(
6027   sqlite3_context *context,
6028   int argc,
6029   sqlite3_value **argv
6030 ){
6031   ZipfileCsr *pCsr;
6032   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
6033   assert( argc>0 );
6034 
6035   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
6036   if( pCsr ){
6037     ZipfileCDS *p = &pCsr->pCurrent->cds;
6038     char *zRes = sqlite3_mprintf("{"
6039         "\"version-made-by\" : %u, "
6040         "\"version-to-extract\" : %u, "
6041         "\"flags\" : %u, "
6042         "\"compression\" : %u, "
6043         "\"time\" : %u, "
6044         "\"date\" : %u, "
6045         "\"crc32\" : %u, "
6046         "\"compressed-size\" : %u, "
6047         "\"uncompressed-size\" : %u, "
6048         "\"file-name-length\" : %u, "
6049         "\"extra-field-length\" : %u, "
6050         "\"file-comment-length\" : %u, "
6051         "\"disk-number-start\" : %u, "
6052         "\"internal-attr\" : %u, "
6053         "\"external-attr\" : %u, "
6054         "\"offset\" : %u }",
6055         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
6056         (u32)p->flags, (u32)p->iCompression,
6057         (u32)p->mTime, (u32)p->mDate,
6058         (u32)p->crc32, (u32)p->szCompressed,
6059         (u32)p->szUncompressed, (u32)p->nFile,
6060         (u32)p->nExtra, (u32)p->nComment,
6061         (u32)p->iDiskStart, (u32)p->iInternalAttr,
6062         (u32)p->iExternalAttr, (u32)p->iOffset
6063     );
6064 
6065     if( zRes==0 ){
6066       sqlite3_result_error_nomem(context);
6067     }else{
6068       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
6069       sqlite3_free(zRes);
6070     }
6071   }
6072 }
6073 
6074 /*
6075 ** xFindFunction method.
6076 */
6077 static int zipfileFindFunction(
6078   sqlite3_vtab *pVtab,            /* Virtual table handle */
6079   int nArg,                       /* Number of SQL function arguments */
6080   const char *zName,              /* Name of SQL function */
6081   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
6082   void **ppArg                    /* OUT: User data for *pxFunc */
6083 ){
6084   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
6085     *pxFunc = zipfileFunctionCds;
6086     *ppArg = (void*)pVtab;
6087     return 1;
6088   }
6089   return 0;
6090 }
6091 
6092 typedef struct ZipfileBuffer ZipfileBuffer;
6093 struct ZipfileBuffer {
6094   u8 *a;                          /* Pointer to buffer */
6095   int n;                          /* Size of buffer in bytes */
6096   int nAlloc;                     /* Byte allocated at a[] */
6097 };
6098 
6099 typedef struct ZipfileCtx ZipfileCtx;
6100 struct ZipfileCtx {
6101   int nEntry;
6102   ZipfileBuffer body;
6103   ZipfileBuffer cds;
6104 };
6105 
6106 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
6107   if( pBuf->n+nByte>pBuf->nAlloc ){
6108     u8 *aNew;
6109     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
6110     int nReq = pBuf->n + nByte;
6111 
6112     while( nNew<nReq ) nNew = nNew*2;
6113     aNew = sqlite3_realloc64(pBuf->a, nNew);
6114     if( aNew==0 ) return SQLITE_NOMEM;
6115     pBuf->a = aNew;
6116     pBuf->nAlloc = (int)nNew;
6117   }
6118   return SQLITE_OK;
6119 }
6120 
6121 /*
6122 ** xStep() callback for the zipfile() aggregate. This can be called in
6123 ** any of the following ways:
6124 **
6125 **   SELECT zipfile(name,data) ...
6126 **   SELECT zipfile(name,mode,mtime,data) ...
6127 **   SELECT zipfile(name,mode,mtime,data,method) ...
6128 */
6129 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
6130   ZipfileCtx *p;                  /* Aggregate function context */
6131   ZipfileEntry e;                 /* New entry to add to zip archive */
6132 
6133   sqlite3_value *pName = 0;
6134   sqlite3_value *pMode = 0;
6135   sqlite3_value *pMtime = 0;
6136   sqlite3_value *pData = 0;
6137   sqlite3_value *pMethod = 0;
6138 
6139   int bIsDir = 0;
6140   u32 mode;
6141   int rc = SQLITE_OK;
6142   char *zErr = 0;
6143 
6144   int iMethod = -1;               /* Compression method to use (0 or 8) */
6145 
6146   const u8 *aData = 0;            /* Possibly compressed data for new entry */
6147   int nData = 0;                  /* Size of aData[] in bytes */
6148   int szUncompressed = 0;         /* Size of data before compression */
6149   u8 *aFree = 0;                  /* Free this before returning */
6150   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
6151 
6152   char *zName = 0;                /* Path (name) of new entry */
6153   int nName = 0;                  /* Size of zName in bytes */
6154   char *zFree = 0;                /* Free this before returning */
6155   int nByte;
6156 
6157   memset(&e, 0, sizeof(e));
6158   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6159   if( p==0 ) return;
6160 
6161   /* Martial the arguments into stack variables */
6162   if( nVal!=2 && nVal!=4 && nVal!=5 ){
6163     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
6164     rc = SQLITE_ERROR;
6165     goto zipfile_step_out;
6166   }
6167   pName = apVal[0];
6168   if( nVal==2 ){
6169     pData = apVal[1];
6170   }else{
6171     pMode = apVal[1];
6172     pMtime = apVal[2];
6173     pData = apVal[3];
6174     if( nVal==5 ){
6175       pMethod = apVal[4];
6176     }
6177   }
6178 
6179   /* Check that the 'name' parameter looks ok. */
6180   zName = (char*)sqlite3_value_text(pName);
6181   nName = sqlite3_value_bytes(pName);
6182   if( zName==0 ){
6183     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
6184     rc = SQLITE_ERROR;
6185     goto zipfile_step_out;
6186   }
6187 
6188   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6189   ** deflate compression) or NULL (choose automatically).  */
6190   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6191     iMethod = (int)sqlite3_value_int64(pMethod);
6192     if( iMethod!=0 && iMethod!=8 ){
6193       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6194       rc = SQLITE_ERROR;
6195       goto zipfile_step_out;
6196     }
6197   }
6198 
6199   /* Now inspect the data. If this is NULL, then the new entry must be a
6200   ** directory.  Otherwise, figure out whether or not the data should
6201   ** be deflated or simply stored in the zip archive. */
6202   if( sqlite3_value_type(pData)==SQLITE_NULL ){
6203     bIsDir = 1;
6204     iMethod = 0;
6205   }else{
6206     aData = sqlite3_value_blob(pData);
6207     szUncompressed = nData = sqlite3_value_bytes(pData);
6208     iCrc32 = crc32(0, aData, nData);
6209     if( iMethod<0 || iMethod==8 ){
6210       int nOut = 0;
6211       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6212       if( rc!=SQLITE_OK ){
6213         goto zipfile_step_out;
6214       }
6215       if( iMethod==8 || nOut<nData ){
6216         aData = aFree;
6217         nData = nOut;
6218         iMethod = 8;
6219       }else{
6220         iMethod = 0;
6221       }
6222     }
6223   }
6224 
6225   /* Decode the "mode" argument. */
6226   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6227   if( rc ) goto zipfile_step_out;
6228 
6229   /* Decode the "mtime" argument. */
6230   e.mUnixTime = zipfileGetTime(pMtime);
6231 
6232   /* If this is a directory entry, ensure that there is exactly one '/'
6233   ** at the end of the path. Or, if this is not a directory and the path
6234   ** ends in '/' it is an error. */
6235   if( bIsDir==0 ){
6236     if( zName[nName-1]=='/' ){
6237       zErr = sqlite3_mprintf("non-directory name must not end with /");
6238       rc = SQLITE_ERROR;
6239       goto zipfile_step_out;
6240     }
6241   }else{
6242     if( zName[nName-1]!='/' ){
6243       zName = zFree = sqlite3_mprintf("%s/", zName);
6244       nName++;
6245       if( zName==0 ){
6246         rc = SQLITE_NOMEM;
6247         goto zipfile_step_out;
6248       }
6249     }else{
6250       while( nName>1 && zName[nName-2]=='/' ) nName--;
6251     }
6252   }
6253 
6254   /* Assemble the ZipfileEntry object for the new zip archive entry */
6255   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6256   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6257   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6258   e.cds.iCompression = (u16)iMethod;
6259   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6260   e.cds.crc32 = iCrc32;
6261   e.cds.szCompressed = nData;
6262   e.cds.szUncompressed = szUncompressed;
6263   e.cds.iExternalAttr = (mode<<16);
6264   e.cds.iOffset = p->body.n;
6265   e.cds.nFile = (u16)nName;
6266   e.cds.zFile = zName;
6267 
6268   /* Append the LFH to the body of the new archive */
6269   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6270   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6271   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6272 
6273   /* Append the data to the body of the new archive */
6274   if( nData>0 ){
6275     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6276     memcpy(&p->body.a[p->body.n], aData, nData);
6277     p->body.n += nData;
6278   }
6279 
6280   /* Append the CDS record to the directory of the new archive */
6281   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6282   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6283   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6284 
6285   /* Increment the count of entries in the archive */
6286   p->nEntry++;
6287 
6288  zipfile_step_out:
6289   sqlite3_free(aFree);
6290   sqlite3_free(zFree);
6291   if( rc ){
6292     if( zErr ){
6293       sqlite3_result_error(pCtx, zErr, -1);
6294     }else{
6295       sqlite3_result_error_code(pCtx, rc);
6296     }
6297   }
6298   sqlite3_free(zErr);
6299 }
6300 
6301 /*
6302 ** xFinalize() callback for zipfile aggregate function.
6303 */
6304 void zipfileFinal(sqlite3_context *pCtx){
6305   ZipfileCtx *p;
6306   ZipfileEOCD eocd;
6307   sqlite3_int64 nZip;
6308   u8 *aZip;
6309 
6310   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6311   if( p==0 ) return;
6312   if( p->nEntry>0 ){
6313     memset(&eocd, 0, sizeof(eocd));
6314     eocd.nEntry = (u16)p->nEntry;
6315     eocd.nEntryTotal = (u16)p->nEntry;
6316     eocd.nSize = p->cds.n;
6317     eocd.iOffset = p->body.n;
6318 
6319     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6320     aZip = (u8*)sqlite3_malloc64(nZip);
6321     if( aZip==0 ){
6322       sqlite3_result_error_nomem(pCtx);
6323     }else{
6324       memcpy(aZip, p->body.a, p->body.n);
6325       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6326       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6327       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
6328     }
6329   }
6330 
6331   sqlite3_free(p->body.a);
6332   sqlite3_free(p->cds.a);
6333 }
6334 
6335 
6336 /*
6337 ** Register the "zipfile" virtual table.
6338 */
6339 static int zipfileRegister(sqlite3 *db){
6340   static sqlite3_module zipfileModule = {
6341     1,                         /* iVersion */
6342     zipfileConnect,            /* xCreate */
6343     zipfileConnect,            /* xConnect */
6344     zipfileBestIndex,          /* xBestIndex */
6345     zipfileDisconnect,         /* xDisconnect */
6346     zipfileDisconnect,         /* xDestroy */
6347     zipfileOpen,               /* xOpen - open a cursor */
6348     zipfileClose,              /* xClose - close a cursor */
6349     zipfileFilter,             /* xFilter - configure scan constraints */
6350     zipfileNext,               /* xNext - advance a cursor */
6351     zipfileEof,                /* xEof - check for end of scan */
6352     zipfileColumn,             /* xColumn - read data */
6353     0,                         /* xRowid - read data */
6354     zipfileUpdate,             /* xUpdate */
6355     zipfileBegin,              /* xBegin */
6356     0,                         /* xSync */
6357     zipfileCommit,             /* xCommit */
6358     zipfileRollback,           /* xRollback */
6359     zipfileFindFunction,       /* xFindMethod */
6360     0,                         /* xRename */
6361   };
6362 
6363   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
6364   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6365   if( rc==SQLITE_OK ){
6366     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6367         zipfileStep, zipfileFinal
6368     );
6369   }
6370   return rc;
6371 }
6372 #else         /* SQLITE_OMIT_VIRTUALTABLE */
6373 # define zipfileRegister(x) SQLITE_OK
6374 #endif
6375 
6376 #ifdef _WIN32
6377 
6378 #endif
6379 int sqlite3_zipfile_init(
6380   sqlite3 *db,
6381   char **pzErrMsg,
6382   const sqlite3_api_routines *pApi
6383 ){
6384   SQLITE_EXTENSION_INIT2(pApi);
6385   (void)pzErrMsg;  /* Unused parameter */
6386   return zipfileRegister(db);
6387 }
6388 
6389 /************************* End ../ext/misc/zipfile.c ********************/
6390 /************************* Begin ../ext/misc/sqlar.c ******************/
6391 /*
6392 ** 2017-12-17
6393 **
6394 ** The author disclaims copyright to this source code.  In place of
6395 ** a legal notice, here is a blessing:
6396 **
6397 **    May you do good and not evil.
6398 **    May you find forgiveness for yourself and forgive others.
6399 **    May you share freely, never taking more than you give.
6400 **
6401 ******************************************************************************
6402 **
6403 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6404 ** for working with sqlar archives and used by the shell tool's built-in
6405 ** sqlar support.
6406 */
6407 /* #include "sqlite3ext.h" */
6408 SQLITE_EXTENSION_INIT1
6409 #include <zlib.h>
6410 
6411 /*
6412 ** Implementation of the "sqlar_compress(X)" SQL function.
6413 **
6414 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6415 ** zlib utility function compress() yields a smaller blob, return the
6416 ** compressed blob. Otherwise, return a copy of X.
6417 **
6418 ** SQLar uses the "zlib format" for compressed content.  The zlib format
6419 ** contains a two-byte identification header and a four-byte checksum at
6420 ** the end.  This is different from ZIP which uses the raw deflate format.
6421 **
6422 ** Future enhancements to SQLar might add support for new compression formats.
6423 ** If so, those new formats will be identified by alternative headers in the
6424 ** compressed data.
6425 */
6426 static void sqlarCompressFunc(
6427   sqlite3_context *context,
6428   int argc,
6429   sqlite3_value **argv
6430 ){
6431   assert( argc==1 );
6432   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6433     const Bytef *pData = sqlite3_value_blob(argv[0]);
6434     uLong nData = sqlite3_value_bytes(argv[0]);
6435     uLongf nOut = compressBound(nData);
6436     Bytef *pOut;
6437 
6438     pOut = (Bytef*)sqlite3_malloc(nOut);
6439     if( pOut==0 ){
6440       sqlite3_result_error_nomem(context);
6441       return;
6442     }else{
6443       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6444         sqlite3_result_error(context, "error in compress()", -1);
6445       }else if( nOut<nData ){
6446         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6447       }else{
6448         sqlite3_result_value(context, argv[0]);
6449       }
6450       sqlite3_free(pOut);
6451     }
6452   }else{
6453     sqlite3_result_value(context, argv[0]);
6454   }
6455 }
6456 
6457 /*
6458 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6459 **
6460 ** Parameter SZ is interpreted as an integer. If it is less than or
6461 ** equal to zero, then this function returns a copy of X. Or, if
6462 ** SZ is equal to the size of X when interpreted as a blob, also
6463 ** return a copy of X. Otherwise, decompress blob X using zlib
6464 ** utility function uncompress() and return the results (another
6465 ** blob).
6466 */
6467 static void sqlarUncompressFunc(
6468   sqlite3_context *context,
6469   int argc,
6470   sqlite3_value **argv
6471 ){
6472   uLong nData;
6473   uLongf sz;
6474 
6475   assert( argc==2 );
6476   sz = sqlite3_value_int(argv[1]);
6477 
6478   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6479     sqlite3_result_value(context, argv[0]);
6480   }else{
6481     const Bytef *pData= sqlite3_value_blob(argv[0]);
6482     Bytef *pOut = sqlite3_malloc(sz);
6483     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6484       sqlite3_result_error(context, "error in uncompress()", -1);
6485     }else{
6486       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6487     }
6488     sqlite3_free(pOut);
6489   }
6490 }
6491 
6492 
6493 #ifdef _WIN32
6494 
6495 #endif
6496 int sqlite3_sqlar_init(
6497   sqlite3 *db,
6498   char **pzErrMsg,
6499   const sqlite3_api_routines *pApi
6500 ){
6501   int rc = SQLITE_OK;
6502   SQLITE_EXTENSION_INIT2(pApi);
6503   (void)pzErrMsg;  /* Unused parameter */
6504   rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
6505                                sqlarCompressFunc, 0, 0);
6506   if( rc==SQLITE_OK ){
6507     rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
6508                                  sqlarUncompressFunc, 0, 0);
6509   }
6510   return rc;
6511 }
6512 
6513 /************************* End ../ext/misc/sqlar.c ********************/
6514 #endif
6515 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6516 /*
6517 ** 2017 April 07
6518 **
6519 ** The author disclaims copyright to this source code.  In place of
6520 ** a legal notice, here is a blessing:
6521 **
6522 **    May you do good and not evil.
6523 **    May you find forgiveness for yourself and forgive others.
6524 **    May you share freely, never taking more than you give.
6525 **
6526 *************************************************************************
6527 */
6528 
6529 
6530 /* #include "sqlite3.h" */
6531 
6532 typedef struct sqlite3expert sqlite3expert;
6533 
6534 /*
6535 ** Create a new sqlite3expert object.
6536 **
6537 ** If successful, a pointer to the new object is returned and (*pzErr) set
6538 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6539 ** an English-language error message. In this case it is the responsibility
6540 ** of the caller to eventually free the error message buffer using
6541 ** sqlite3_free().
6542 */
6543 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6544 
6545 /*
6546 ** Configure an sqlite3expert object.
6547 **
6548 ** EXPERT_CONFIG_SAMPLE:
6549 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6550 **   each candidate index. This involves scanning and sorting the entire
6551 **   contents of each user database table once for each candidate index
6552 **   associated with the table. For large databases, this can be
6553 **   prohibitively slow. This option allows the sqlite3expert object to
6554 **   be configured so that sqlite_stat1 data is instead generated based on a
6555 **   subset of each table, or so that no sqlite_stat1 data is used at all.
6556 **
6557 **   A single integer argument is passed to this option. If the value is less
6558 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
6559 **   the analysis - indexes are recommended based on the database schema only.
6560 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
6561 **   generated for each candidate index (this is the default). Finally, if the
6562 **   value falls between 0 and 100, then it represents the percentage of user
6563 **   table rows that should be considered when generating sqlite_stat1 data.
6564 **
6565 **   Examples:
6566 **
6567 **     // Do not generate any sqlite_stat1 data
6568 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6569 **
6570 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
6571 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6572 */
6573 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6574 
6575 #define EXPERT_CONFIG_SAMPLE 1    /* int */
6576 
6577 /*
6578 ** Specify zero or more SQL statements to be included in the analysis.
6579 **
6580 ** Buffer zSql must contain zero or more complete SQL statements. This
6581 ** function parses all statements contained in the buffer and adds them
6582 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6583 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6584 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6585 ** may be set to point to an English language error message. In this case
6586 ** the caller is responsible for eventually freeing the error message buffer
6587 ** using sqlite3_free().
6588 **
6589 ** If an error does occur while processing one of the statements in the
6590 ** buffer passed as the second argument, none of the statements in the
6591 ** buffer are added to the analysis.
6592 **
6593 ** This function must be called before sqlite3_expert_analyze(). If a call
6594 ** to this function is made on an sqlite3expert object that has already
6595 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6596 ** immediately and no statements are added to the analysis.
6597 */
6598 int sqlite3_expert_sql(
6599   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
6600   const char *zSql,               /* SQL statement(s) to add */
6601   char **pzErr                    /* OUT: Error message (if any) */
6602 );
6603 
6604 
6605 /*
6606 ** This function is called after the sqlite3expert object has been configured
6607 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6608 ** the analysis. Once this function has been called, it is not possible to
6609 ** add further SQL statements to the analysis.
6610 **
6611 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6612 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
6613 ** point to a buffer containing an English language error message. In this
6614 ** case it is the responsibility of the caller to eventually free the buffer
6615 ** using sqlite3_free().
6616 **
6617 ** If an error does occur within this function, the sqlite3expert object
6618 ** is no longer useful for any purpose. At that point it is no longer
6619 ** possible to add further SQL statements to the object or to re-attempt
6620 ** the analysis. The sqlite3expert object must still be freed using a call
6621 ** sqlite3_expert_destroy().
6622 */
6623 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6624 
6625 /*
6626 ** Return the total number of statements loaded using sqlite3_expert_sql().
6627 ** The total number of SQL statements may be different from the total number
6628 ** to calls to sqlite3_expert_sql().
6629 */
6630 int sqlite3_expert_count(sqlite3expert*);
6631 
6632 /*
6633 ** Return a component of the report.
6634 **
6635 ** This function is called after sqlite3_expert_analyze() to extract the
6636 ** results of the analysis. Each call to this function returns either a
6637 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6638 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6639 ** #define constants defined below.
6640 **
6641 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
6642 ** information relating to a specific SQL statement. In these cases that
6643 ** SQL statement is identified by the value passed as the second argument.
6644 ** SQL statements are numbered from 0 in the order in which they are parsed.
6645 ** If an out-of-range value (less than zero or equal to or greater than the
6646 ** value returned by sqlite3_expert_count()) is passed as the second argument
6647 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6648 **
6649 ** EXPERT_REPORT_SQL:
6650 **   Return the text of SQL statement iStmt.
6651 **
6652 ** EXPERT_REPORT_INDEXES:
6653 **   Return a buffer containing the CREATE INDEX statements for all recommended
6654 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL
6655 **   is returned.
6656 **
6657 ** EXPERT_REPORT_PLAN:
6658 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6659 **   iStmt after the proposed indexes have been added to the database schema.
6660 **
6661 ** EXPERT_REPORT_CANDIDATES:
6662 **   Return a pointer to a buffer containing the CREATE INDEX statements
6663 **   for all indexes that were tested (for all SQL statements). The iStmt
6664 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6665 */
6666 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6667 
6668 /*
6669 ** Values for the third argument passed to sqlite3_expert_report().
6670 */
6671 #define EXPERT_REPORT_SQL        1
6672 #define EXPERT_REPORT_INDEXES    2
6673 #define EXPERT_REPORT_PLAN       3
6674 #define EXPERT_REPORT_CANDIDATES 4
6675 
6676 /*
6677 ** Free an (sqlite3expert*) handle and all associated resources. There
6678 ** should be one call to this function for each successful call to
6679 ** sqlite3-expert_new().
6680 */
6681 void sqlite3_expert_destroy(sqlite3expert*);
6682 
6683 
6684 
6685 /************************* End ../ext/expert/sqlite3expert.h ********************/
6686 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6687 /*
6688 ** 2017 April 09
6689 **
6690 ** The author disclaims copyright to this source code.  In place of
6691 ** a legal notice, here is a blessing:
6692 **
6693 **    May you do good and not evil.
6694 **    May you find forgiveness for yourself and forgive others.
6695 **    May you share freely, never taking more than you give.
6696 **
6697 *************************************************************************
6698 */
6699 /* #include "sqlite3expert.h" */
6700 #include <assert.h>
6701 #include <string.h>
6702 #include <stdio.h>
6703 
6704 #ifndef SQLITE_OMIT_VIRTUALTABLE
6705 
6706 /* typedef sqlite3_int64 i64; */
6707 /* typedef sqlite3_uint64 u64; */
6708 
6709 typedef struct IdxColumn IdxColumn;
6710 typedef struct IdxConstraint IdxConstraint;
6711 typedef struct IdxScan IdxScan;
6712 typedef struct IdxStatement IdxStatement;
6713 typedef struct IdxTable IdxTable;
6714 typedef struct IdxWrite IdxWrite;
6715 
6716 #define STRLEN  (int)strlen
6717 
6718 /*
6719 ** A temp table name that we assume no user database will actually use.
6720 ** If this assumption proves incorrect triggers on the table with the
6721 ** conflicting name will be ignored.
6722 */
6723 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6724 
6725 /*
6726 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6727 ** any other type of single-ended range constraint on a column).
6728 **
6729 ** pLink:
6730 **   Used to temporarily link IdxConstraint objects into lists while
6731 **   creating candidate indexes.
6732 */
6733 struct IdxConstraint {
6734   char *zColl;                    /* Collation sequence */
6735   int bRange;                     /* True for range, false for eq */
6736   int iCol;                       /* Constrained table column */
6737   int bFlag;                      /* Used by idxFindCompatible() */
6738   int bDesc;                      /* True if ORDER BY <expr> DESC */
6739   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
6740   IdxConstraint *pLink;           /* See above */
6741 };
6742 
6743 /*
6744 ** A single scan of a single table.
6745 */
6746 struct IdxScan {
6747   IdxTable *pTab;                 /* Associated table object */
6748   int iDb;                        /* Database containing table zTable */
6749   i64 covering;                   /* Mask of columns required for cov. index */
6750   IdxConstraint *pOrder;          /* ORDER BY columns */
6751   IdxConstraint *pEq;             /* List of == constraints */
6752   IdxConstraint *pRange;          /* List of < constraints */
6753   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
6754 };
6755 
6756 /*
6757 ** Information regarding a single database table. Extracted from
6758 ** "PRAGMA table_info" by function idxGetTableInfo().
6759 */
6760 struct IdxColumn {
6761   char *zName;
6762   char *zColl;
6763   int iPk;
6764 };
6765 struct IdxTable {
6766   int nCol;
6767   char *zName;                    /* Table name */
6768   IdxColumn *aCol;
6769   IdxTable *pNext;                /* Next table in linked list of all tables */
6770 };
6771 
6772 /*
6773 ** An object of the following type is created for each unique table/write-op
6774 ** seen. The objects are stored in a singly-linked list beginning at
6775 ** sqlite3expert.pWrite.
6776 */
6777 struct IdxWrite {
6778   IdxTable *pTab;
6779   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
6780   IdxWrite *pNext;
6781 };
6782 
6783 /*
6784 ** Each statement being analyzed is represented by an instance of this
6785 ** structure.
6786 */
6787 struct IdxStatement {
6788   int iId;                        /* Statement number */
6789   char *zSql;                     /* SQL statement */
6790   char *zIdx;                     /* Indexes */
6791   char *zEQP;                     /* Plan */
6792   IdxStatement *pNext;
6793 };
6794 
6795 
6796 /*
6797 ** A hash table for storing strings. With space for a payload string
6798 ** with each entry. Methods are:
6799 **
6800 **   idxHashInit()
6801 **   idxHashClear()
6802 **   idxHashAdd()
6803 **   idxHashSearch()
6804 */
6805 #define IDX_HASH_SIZE 1023
6806 typedef struct IdxHashEntry IdxHashEntry;
6807 typedef struct IdxHash IdxHash;
6808 struct IdxHashEntry {
6809   char *zKey;                     /* nul-terminated key */
6810   char *zVal;                     /* nul-terminated value string */
6811   char *zVal2;                    /* nul-terminated value string 2 */
6812   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
6813   IdxHashEntry *pNext;            /* Next entry in hash */
6814 };
6815 struct IdxHash {
6816   IdxHashEntry *pFirst;
6817   IdxHashEntry *aHash[IDX_HASH_SIZE];
6818 };
6819 
6820 /*
6821 ** sqlite3expert object.
6822 */
6823 struct sqlite3expert {
6824   int iSample;                    /* Percentage of tables to sample for stat1 */
6825   sqlite3 *db;                    /* User database */
6826   sqlite3 *dbm;                   /* In-memory db for this analysis */
6827   sqlite3 *dbv;                   /* Vtab schema for this analysis */
6828   IdxTable *pTable;               /* List of all IdxTable objects */
6829   IdxScan *pScan;                 /* List of scan objects */
6830   IdxWrite *pWrite;               /* List of write objects */
6831   IdxStatement *pStatement;       /* List of IdxStatement objects */
6832   int bRun;                       /* True once analysis has run */
6833   char **pzErrmsg;
6834   int rc;                         /* Error code from whereinfo hook */
6835   IdxHash hIdx;                   /* Hash containing all candidate indexes */
6836   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
6837 };
6838 
6839 
6840 /*
6841 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
6842 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6843 */
6844 static void *idxMalloc(int *pRc, int nByte){
6845   void *pRet;
6846   assert( *pRc==SQLITE_OK );
6847   assert( nByte>0 );
6848   pRet = sqlite3_malloc(nByte);
6849   if( pRet ){
6850     memset(pRet, 0, nByte);
6851   }else{
6852     *pRc = SQLITE_NOMEM;
6853   }
6854   return pRet;
6855 }
6856 
6857 /*
6858 ** Initialize an IdxHash hash table.
6859 */
6860 static void idxHashInit(IdxHash *pHash){
6861   memset(pHash, 0, sizeof(IdxHash));
6862 }
6863 
6864 /*
6865 ** Reset an IdxHash hash table.
6866 */
6867 static void idxHashClear(IdxHash *pHash){
6868   int i;
6869   for(i=0; i<IDX_HASH_SIZE; i++){
6870     IdxHashEntry *pEntry;
6871     IdxHashEntry *pNext;
6872     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6873       pNext = pEntry->pHashNext;
6874       sqlite3_free(pEntry->zVal2);
6875       sqlite3_free(pEntry);
6876     }
6877   }
6878   memset(pHash, 0, sizeof(IdxHash));
6879 }
6880 
6881 /*
6882 ** Return the index of the hash bucket that the string specified by the
6883 ** arguments to this function belongs.
6884 */
6885 static int idxHashString(const char *z, int n){
6886   unsigned int ret = 0;
6887   int i;
6888   for(i=0; i<n; i++){
6889     ret += (ret<<3) + (unsigned char)(z[i]);
6890   }
6891   return (int)(ret % IDX_HASH_SIZE);
6892 }
6893 
6894 /*
6895 ** If zKey is already present in the hash table, return non-zero and do
6896 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6897 ** the hash table passed as the second argument.
6898 */
6899 static int idxHashAdd(
6900   int *pRc,
6901   IdxHash *pHash,
6902   const char *zKey,
6903   const char *zVal
6904 ){
6905   int nKey = STRLEN(zKey);
6906   int iHash = idxHashString(zKey, nKey);
6907   int nVal = (zVal ? STRLEN(zVal) : 0);
6908   IdxHashEntry *pEntry;
6909   assert( iHash>=0 );
6910   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6911     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6912       return 1;
6913     }
6914   }
6915   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6916   if( pEntry ){
6917     pEntry->zKey = (char*)&pEntry[1];
6918     memcpy(pEntry->zKey, zKey, nKey);
6919     if( zVal ){
6920       pEntry->zVal = &pEntry->zKey[nKey+1];
6921       memcpy(pEntry->zVal, zVal, nVal);
6922     }
6923     pEntry->pHashNext = pHash->aHash[iHash];
6924     pHash->aHash[iHash] = pEntry;
6925 
6926     pEntry->pNext = pHash->pFirst;
6927     pHash->pFirst = pEntry;
6928   }
6929   return 0;
6930 }
6931 
6932 /*
6933 ** If zKey/nKey is present in the hash table, return a pointer to the
6934 ** hash-entry object.
6935 */
6936 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6937   int iHash;
6938   IdxHashEntry *pEntry;
6939   if( nKey<0 ) nKey = STRLEN(zKey);
6940   iHash = idxHashString(zKey, nKey);
6941   assert( iHash>=0 );
6942   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6943     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6944       return pEntry;
6945     }
6946   }
6947   return 0;
6948 }
6949 
6950 /*
6951 ** If the hash table contains an entry with a key equal to the string
6952 ** passed as the final two arguments to this function, return a pointer
6953 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6954 ** hash table, return NULL.
6955 */
6956 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6957   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6958   if( pEntry ) return pEntry->zVal;
6959   return 0;
6960 }
6961 
6962 /*
6963 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6964 ** variable to point to a copy of nul-terminated string zColl.
6965 */
6966 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6967   IdxConstraint *pNew;
6968   int nColl = STRLEN(zColl);
6969 
6970   assert( *pRc==SQLITE_OK );
6971   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6972   if( pNew ){
6973     pNew->zColl = (char*)&pNew[1];
6974     memcpy(pNew->zColl, zColl, nColl+1);
6975   }
6976   return pNew;
6977 }
6978 
6979 /*
6980 ** An error associated with database handle db has just occurred. Pass
6981 ** the error message to callback function xOut.
6982 */
6983 static void idxDatabaseError(
6984   sqlite3 *db,                    /* Database handle */
6985   char **pzErrmsg                 /* Write error here */
6986 ){
6987   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6988 }
6989 
6990 /*
6991 ** Prepare an SQL statement.
6992 */
6993 static int idxPrepareStmt(
6994   sqlite3 *db,                    /* Database handle to compile against */
6995   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
6996   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
6997   const char *zSql                /* SQL statement to compile */
6998 ){
6999   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
7000   if( rc!=SQLITE_OK ){
7001     *ppStmt = 0;
7002     idxDatabaseError(db, pzErrmsg);
7003   }
7004   return rc;
7005 }
7006 
7007 /*
7008 ** Prepare an SQL statement using the results of a printf() formatting.
7009 */
7010 static int idxPrintfPrepareStmt(
7011   sqlite3 *db,                    /* Database handle to compile against */
7012   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
7013   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
7014   const char *zFmt,               /* printf() format of SQL statement */
7015   ...                             /* Trailing printf() arguments */
7016 ){
7017   va_list ap;
7018   int rc;
7019   char *zSql;
7020   va_start(ap, zFmt);
7021   zSql = sqlite3_vmprintf(zFmt, ap);
7022   if( zSql==0 ){
7023     rc = SQLITE_NOMEM;
7024   }else{
7025     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
7026     sqlite3_free(zSql);
7027   }
7028   va_end(ap);
7029   return rc;
7030 }
7031 
7032 
7033 /*************************************************************************
7034 ** Beginning of virtual table implementation.
7035 */
7036 typedef struct ExpertVtab ExpertVtab;
7037 struct ExpertVtab {
7038   sqlite3_vtab base;
7039   IdxTable *pTab;
7040   sqlite3expert *pExpert;
7041 };
7042 
7043 typedef struct ExpertCsr ExpertCsr;
7044 struct ExpertCsr {
7045   sqlite3_vtab_cursor base;
7046   sqlite3_stmt *pData;
7047 };
7048 
7049 static char *expertDequote(const char *zIn){
7050   int n = STRLEN(zIn);
7051   char *zRet = sqlite3_malloc(n);
7052 
7053   assert( zIn[0]=='\'' );
7054   assert( zIn[n-1]=='\'' );
7055 
7056   if( zRet ){
7057     int iOut = 0;
7058     int iIn = 0;
7059     for(iIn=1; iIn<(n-1); iIn++){
7060       if( zIn[iIn]=='\'' ){
7061         assert( zIn[iIn+1]=='\'' );
7062         iIn++;
7063       }
7064       zRet[iOut++] = zIn[iIn];
7065     }
7066     zRet[iOut] = '\0';
7067   }
7068 
7069   return zRet;
7070 }
7071 
7072 /*
7073 ** This function is the implementation of both the xConnect and xCreate
7074 ** methods of the r-tree virtual table.
7075 **
7076 **   argv[0]   -> module name
7077 **   argv[1]   -> database name
7078 **   argv[2]   -> table name
7079 **   argv[...] -> column names...
7080 */
7081 static int expertConnect(
7082   sqlite3 *db,
7083   void *pAux,
7084   int argc, const char *const*argv,
7085   sqlite3_vtab **ppVtab,
7086   char **pzErr
7087 ){
7088   sqlite3expert *pExpert = (sqlite3expert*)pAux;
7089   ExpertVtab *p = 0;
7090   int rc;
7091 
7092   if( argc!=4 ){
7093     *pzErr = sqlite3_mprintf("internal error!");
7094     rc = SQLITE_ERROR;
7095   }else{
7096     char *zCreateTable = expertDequote(argv[3]);
7097     if( zCreateTable ){
7098       rc = sqlite3_declare_vtab(db, zCreateTable);
7099       if( rc==SQLITE_OK ){
7100         p = idxMalloc(&rc, sizeof(ExpertVtab));
7101       }
7102       if( rc==SQLITE_OK ){
7103         p->pExpert = pExpert;
7104         p->pTab = pExpert->pTable;
7105         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
7106       }
7107       sqlite3_free(zCreateTable);
7108     }else{
7109       rc = SQLITE_NOMEM;
7110     }
7111   }
7112 
7113   *ppVtab = (sqlite3_vtab*)p;
7114   return rc;
7115 }
7116 
7117 static int expertDisconnect(sqlite3_vtab *pVtab){
7118   ExpertVtab *p = (ExpertVtab*)pVtab;
7119   sqlite3_free(p);
7120   return SQLITE_OK;
7121 }
7122 
7123 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
7124   ExpertVtab *p = (ExpertVtab*)pVtab;
7125   int rc = SQLITE_OK;
7126   int n = 0;
7127   IdxScan *pScan;
7128   const int opmask =
7129     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
7130     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
7131     SQLITE_INDEX_CONSTRAINT_LE;
7132 
7133   pScan = idxMalloc(&rc, sizeof(IdxScan));
7134   if( pScan ){
7135     int i;
7136 
7137     /* Link the new scan object into the list */
7138     pScan->pTab = p->pTab;
7139     pScan->pNextScan = p->pExpert->pScan;
7140     p->pExpert->pScan = pScan;
7141 
7142     /* Add the constraints to the IdxScan object */
7143     for(i=0; i<pIdxInfo->nConstraint; i++){
7144       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7145       if( pCons->usable
7146        && pCons->iColumn>=0
7147        && p->pTab->aCol[pCons->iColumn].iPk==0
7148        && (pCons->op & opmask)
7149       ){
7150         IdxConstraint *pNew;
7151         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
7152         pNew = idxNewConstraint(&rc, zColl);
7153         if( pNew ){
7154           pNew->iCol = pCons->iColumn;
7155           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7156             pNew->pNext = pScan->pEq;
7157             pScan->pEq = pNew;
7158           }else{
7159             pNew->bRange = 1;
7160             pNew->pNext = pScan->pRange;
7161             pScan->pRange = pNew;
7162           }
7163         }
7164         n++;
7165         pIdxInfo->aConstraintUsage[i].argvIndex = n;
7166       }
7167     }
7168 
7169     /* Add the ORDER BY to the IdxScan object */
7170     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
7171       int iCol = pIdxInfo->aOrderBy[i].iColumn;
7172       if( iCol>=0 ){
7173         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
7174         if( pNew ){
7175           pNew->iCol = iCol;
7176           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
7177           pNew->pNext = pScan->pOrder;
7178           pNew->pLink = pScan->pOrder;
7179           pScan->pOrder = pNew;
7180           n++;
7181         }
7182       }
7183     }
7184   }
7185 
7186   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7187   return rc;
7188 }
7189 
7190 static int expertUpdate(
7191   sqlite3_vtab *pVtab,
7192   int nData,
7193   sqlite3_value **azData,
7194   sqlite_int64 *pRowid
7195 ){
7196   (void)pVtab;
7197   (void)nData;
7198   (void)azData;
7199   (void)pRowid;
7200   return SQLITE_OK;
7201 }
7202 
7203 /*
7204 ** Virtual table module xOpen method.
7205 */
7206 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7207   int rc = SQLITE_OK;
7208   ExpertCsr *pCsr;
7209   (void)pVTab;
7210   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7211   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7212   return rc;
7213 }
7214 
7215 /*
7216 ** Virtual table module xClose method.
7217 */
7218 static int expertClose(sqlite3_vtab_cursor *cur){
7219   ExpertCsr *pCsr = (ExpertCsr*)cur;
7220   sqlite3_finalize(pCsr->pData);
7221   sqlite3_free(pCsr);
7222   return SQLITE_OK;
7223 }
7224 
7225 /*
7226 ** Virtual table module xEof method.
7227 **
7228 ** Return non-zero if the cursor does not currently point to a valid
7229 ** record (i.e if the scan has finished), or zero otherwise.
7230 */
7231 static int expertEof(sqlite3_vtab_cursor *cur){
7232   ExpertCsr *pCsr = (ExpertCsr*)cur;
7233   return pCsr->pData==0;
7234 }
7235 
7236 /*
7237 ** Virtual table module xNext method.
7238 */
7239 static int expertNext(sqlite3_vtab_cursor *cur){
7240   ExpertCsr *pCsr = (ExpertCsr*)cur;
7241   int rc = SQLITE_OK;
7242 
7243   assert( pCsr->pData );
7244   rc = sqlite3_step(pCsr->pData);
7245   if( rc!=SQLITE_ROW ){
7246     rc = sqlite3_finalize(pCsr->pData);
7247     pCsr->pData = 0;
7248   }else{
7249     rc = SQLITE_OK;
7250   }
7251 
7252   return rc;
7253 }
7254 
7255 /*
7256 ** Virtual table module xRowid method.
7257 */
7258 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7259   (void)cur;
7260   *pRowid = 0;
7261   return SQLITE_OK;
7262 }
7263 
7264 /*
7265 ** Virtual table module xColumn method.
7266 */
7267 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7268   ExpertCsr *pCsr = (ExpertCsr*)cur;
7269   sqlite3_value *pVal;
7270   pVal = sqlite3_column_value(pCsr->pData, i);
7271   if( pVal ){
7272     sqlite3_result_value(ctx, pVal);
7273   }
7274   return SQLITE_OK;
7275 }
7276 
7277 /*
7278 ** Virtual table module xFilter method.
7279 */
7280 static int expertFilter(
7281   sqlite3_vtab_cursor *cur,
7282   int idxNum, const char *idxStr,
7283   int argc, sqlite3_value **argv
7284 ){
7285   ExpertCsr *pCsr = (ExpertCsr*)cur;
7286   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7287   sqlite3expert *pExpert = pVtab->pExpert;
7288   int rc;
7289 
7290   (void)idxNum;
7291   (void)idxStr;
7292   (void)argc;
7293   (void)argv;
7294   rc = sqlite3_finalize(pCsr->pData);
7295   pCsr->pData = 0;
7296   if( rc==SQLITE_OK ){
7297     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7298         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7299     );
7300   }
7301 
7302   if( rc==SQLITE_OK ){
7303     rc = expertNext(cur);
7304   }
7305   return rc;
7306 }
7307 
7308 static int idxRegisterVtab(sqlite3expert *p){
7309   static sqlite3_module expertModule = {
7310     2,                            /* iVersion */
7311     expertConnect,                /* xCreate - create a table */
7312     expertConnect,                /* xConnect - connect to an existing table */
7313     expertBestIndex,              /* xBestIndex - Determine search strategy */
7314     expertDisconnect,             /* xDisconnect - Disconnect from a table */
7315     expertDisconnect,             /* xDestroy - Drop a table */
7316     expertOpen,                   /* xOpen - open a cursor */
7317     expertClose,                  /* xClose - close a cursor */
7318     expertFilter,                 /* xFilter - configure scan constraints */
7319     expertNext,                   /* xNext - advance a cursor */
7320     expertEof,                    /* xEof */
7321     expertColumn,                 /* xColumn - read data */
7322     expertRowid,                  /* xRowid - read data */
7323     expertUpdate,                 /* xUpdate - write data */
7324     0,                            /* xBegin - begin transaction */
7325     0,                            /* xSync - sync transaction */
7326     0,                            /* xCommit - commit transaction */
7327     0,                            /* xRollback - rollback transaction */
7328     0,                            /* xFindFunction - function overloading */
7329     0,                            /* xRename - rename the table */
7330     0,                            /* xSavepoint */
7331     0,                            /* xRelease */
7332     0,                            /* xRollbackTo */
7333     0,                            /* xShadowName */
7334   };
7335 
7336   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7337 }
7338 /*
7339 ** End of virtual table implementation.
7340 *************************************************************************/
7341 /*
7342 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7343 ** is called, set it to the return value of sqlite3_finalize() before
7344 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7345 */
7346 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7347   int rc = sqlite3_finalize(pStmt);
7348   if( *pRc==SQLITE_OK ) *pRc = rc;
7349 }
7350 
7351 /*
7352 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7353 ** in the main database of connection db. If successful, set (*ppOut) to
7354 ** point to the new object and return SQLITE_OK. Otherwise, return an
7355 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7356 ** set to point to an error string.
7357 **
7358 ** It is the responsibility of the caller to eventually free either the
7359 ** IdxTable object or error message using sqlite3_free().
7360 */
7361 static int idxGetTableInfo(
7362   sqlite3 *db,                    /* Database connection to read details from */
7363   const char *zTab,               /* Table name */
7364   IdxTable **ppOut,               /* OUT: New object (if successful) */
7365   char **pzErrmsg                 /* OUT: Error message (if not) */
7366 ){
7367   sqlite3_stmt *p1 = 0;
7368   int nCol = 0;
7369   int nTab = STRLEN(zTab);
7370   int nByte = sizeof(IdxTable) + nTab + 1;
7371   IdxTable *pNew = 0;
7372   int rc, rc2;
7373   char *pCsr = 0;
7374 
7375   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7376   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7377     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7378     nByte += 1 + STRLEN(zCol);
7379     rc = sqlite3_table_column_metadata(
7380         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7381     );
7382     nByte += 1 + STRLEN(zCol);
7383     nCol++;
7384   }
7385   rc2 = sqlite3_reset(p1);
7386   if( rc==SQLITE_OK ) rc = rc2;
7387 
7388   nByte += sizeof(IdxColumn) * nCol;
7389   if( rc==SQLITE_OK ){
7390     pNew = idxMalloc(&rc, nByte);
7391   }
7392   if( rc==SQLITE_OK ){
7393     pNew->aCol = (IdxColumn*)&pNew[1];
7394     pNew->nCol = nCol;
7395     pCsr = (char*)&pNew->aCol[nCol];
7396   }
7397 
7398   nCol = 0;
7399   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7400     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7401     int nCopy = STRLEN(zCol) + 1;
7402     pNew->aCol[nCol].zName = pCsr;
7403     pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7404     memcpy(pCsr, zCol, nCopy);
7405     pCsr += nCopy;
7406 
7407     rc = sqlite3_table_column_metadata(
7408         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7409     );
7410     if( rc==SQLITE_OK ){
7411       nCopy = STRLEN(zCol) + 1;
7412       pNew->aCol[nCol].zColl = pCsr;
7413       memcpy(pCsr, zCol, nCopy);
7414       pCsr += nCopy;
7415     }
7416 
7417     nCol++;
7418   }
7419   idxFinalize(&rc, p1);
7420 
7421   if( rc!=SQLITE_OK ){
7422     sqlite3_free(pNew);
7423     pNew = 0;
7424   }else{
7425     pNew->zName = pCsr;
7426     memcpy(pNew->zName, zTab, nTab+1);
7427   }
7428 
7429   *ppOut = pNew;
7430   return rc;
7431 }
7432 
7433 /*
7434 ** This function is a no-op if *pRc is set to anything other than
7435 ** SQLITE_OK when it is called.
7436 **
7437 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7438 ** the printf() style arguments is appended to zIn and the result returned
7439 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7440 ** zIn before returning.
7441 */
7442 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7443   va_list ap;
7444   char *zAppend = 0;
7445   char *zRet = 0;
7446   int nIn = zIn ? STRLEN(zIn) : 0;
7447   int nAppend = 0;
7448   va_start(ap, zFmt);
7449   if( *pRc==SQLITE_OK ){
7450     zAppend = sqlite3_vmprintf(zFmt, ap);
7451     if( zAppend ){
7452       nAppend = STRLEN(zAppend);
7453       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7454     }
7455     if( zAppend && zRet ){
7456       if( nIn ) memcpy(zRet, zIn, nIn);
7457       memcpy(&zRet[nIn], zAppend, nAppend+1);
7458     }else{
7459       sqlite3_free(zRet);
7460       zRet = 0;
7461       *pRc = SQLITE_NOMEM;
7462     }
7463     sqlite3_free(zAppend);
7464     sqlite3_free(zIn);
7465   }
7466   va_end(ap);
7467   return zRet;
7468 }
7469 
7470 /*
7471 ** Return true if zId must be quoted in order to use it as an SQL
7472 ** identifier, or false otherwise.
7473 */
7474 static int idxIdentifierRequiresQuotes(const char *zId){
7475   int i;
7476   for(i=0; zId[i]; i++){
7477     if( !(zId[i]=='_')
7478      && !(zId[i]>='0' && zId[i]<='9')
7479      && !(zId[i]>='a' && zId[i]<='z')
7480      && !(zId[i]>='A' && zId[i]<='Z')
7481     ){
7482       return 1;
7483     }
7484   }
7485   return 0;
7486 }
7487 
7488 /*
7489 ** This function appends an index column definition suitable for constraint
7490 ** pCons to the string passed as zIn and returns the result.
7491 */
7492 static char *idxAppendColDefn(
7493   int *pRc,                       /* IN/OUT: Error code */
7494   char *zIn,                      /* Column defn accumulated so far */
7495   IdxTable *pTab,                 /* Table index will be created on */
7496   IdxConstraint *pCons
7497 ){
7498   char *zRet = zIn;
7499   IdxColumn *p = &pTab->aCol[pCons->iCol];
7500   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7501 
7502   if( idxIdentifierRequiresQuotes(p->zName) ){
7503     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7504   }else{
7505     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7506   }
7507 
7508   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7509     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7510       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7511     }else{
7512       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7513     }
7514   }
7515 
7516   if( pCons->bDesc ){
7517     zRet = idxAppendText(pRc, zRet, " DESC");
7518   }
7519   return zRet;
7520 }
7521 
7522 /*
7523 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7524 ** would create from arguments pScan, pEq and pTail. If no error occurs and
7525 ** such an index is found, return non-zero. Or, if no such index is found,
7526 ** return zero.
7527 **
7528 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7529 */
7530 static int idxFindCompatible(
7531   int *pRc,                       /* OUT: Error code */
7532   sqlite3* dbm,                   /* Database to search */
7533   IdxScan *pScan,                 /* Scan for table to search for index on */
7534   IdxConstraint *pEq,             /* List of == constraints */
7535   IdxConstraint *pTail            /* List of range constraints */
7536 ){
7537   const char *zTbl = pScan->pTab->zName;
7538   sqlite3_stmt *pIdxList = 0;
7539   IdxConstraint *pIter;
7540   int nEq = 0;                    /* Number of elements in pEq */
7541   int rc;
7542 
7543   /* Count the elements in list pEq */
7544   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7545 
7546   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7547   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7548     int bMatch = 1;
7549     IdxConstraint *pT = pTail;
7550     sqlite3_stmt *pInfo = 0;
7551     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7552 
7553     /* Zero the IdxConstraint.bFlag values in the pEq list */
7554     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7555 
7556     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7557     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7558       int iIdx = sqlite3_column_int(pInfo, 0);
7559       int iCol = sqlite3_column_int(pInfo, 1);
7560       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7561 
7562       if( iIdx<nEq ){
7563         for(pIter=pEq; pIter; pIter=pIter->pLink){
7564           if( pIter->bFlag ) continue;
7565           if( pIter->iCol!=iCol ) continue;
7566           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7567           pIter->bFlag = 1;
7568           break;
7569         }
7570         if( pIter==0 ){
7571           bMatch = 0;
7572           break;
7573         }
7574       }else{
7575         if( pT ){
7576           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7577             bMatch = 0;
7578             break;
7579           }
7580           pT = pT->pLink;
7581         }
7582       }
7583     }
7584     idxFinalize(&rc, pInfo);
7585 
7586     if( rc==SQLITE_OK && bMatch ){
7587       sqlite3_finalize(pIdxList);
7588       return 1;
7589     }
7590   }
7591   idxFinalize(&rc, pIdxList);
7592 
7593   *pRc = rc;
7594   return 0;
7595 }
7596 
7597 static int idxCreateFromCons(
7598   sqlite3expert *p,
7599   IdxScan *pScan,
7600   IdxConstraint *pEq,
7601   IdxConstraint *pTail
7602 ){
7603   sqlite3 *dbm = p->dbm;
7604   int rc = SQLITE_OK;
7605   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7606     IdxTable *pTab = pScan->pTab;
7607     char *zCols = 0;
7608     char *zIdx = 0;
7609     IdxConstraint *pCons;
7610     unsigned int h = 0;
7611     const char *zFmt;
7612 
7613     for(pCons=pEq; pCons; pCons=pCons->pLink){
7614       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7615     }
7616     for(pCons=pTail; pCons; pCons=pCons->pLink){
7617       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7618     }
7619 
7620     if( rc==SQLITE_OK ){
7621       /* Hash the list of columns to come up with a name for the index */
7622       const char *zTable = pScan->pTab->zName;
7623       char *zName;                /* Index name */
7624       int i;
7625       for(i=0; zCols[i]; i++){
7626         h += ((h<<3) + zCols[i]);
7627       }
7628       zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7629       if( zName==0 ){
7630         rc = SQLITE_NOMEM;
7631       }else{
7632         if( idxIdentifierRequiresQuotes(zTable) ){
7633           zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7634         }else{
7635           zFmt = "CREATE INDEX %s ON %s(%s)";
7636         }
7637         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7638         if( !zIdx ){
7639           rc = SQLITE_NOMEM;
7640         }else{
7641           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7642           idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7643         }
7644         sqlite3_free(zName);
7645         sqlite3_free(zIdx);
7646       }
7647     }
7648 
7649     sqlite3_free(zCols);
7650   }
7651   return rc;
7652 }
7653 
7654 /*
7655 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7656 ** a constraint compatible with *p. Otherwise return false.
7657 */
7658 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7659   IdxConstraint *pCmp;
7660   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7661     if( p->iCol==pCmp->iCol ) return 1;
7662   }
7663   return 0;
7664 }
7665 
7666 static int idxCreateFromWhere(
7667   sqlite3expert *p,
7668   IdxScan *pScan,                 /* Create indexes for this scan */
7669   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
7670 ){
7671   IdxConstraint *p1 = 0;
7672   IdxConstraint *pCon;
7673   int rc;
7674 
7675   /* Gather up all the == constraints. */
7676   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7677     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7678       pCon->pLink = p1;
7679       p1 = pCon;
7680     }
7681   }
7682 
7683   /* Create an index using the == constraints collected above. And the
7684   ** range constraint/ORDER BY terms passed in by the caller, if any. */
7685   rc = idxCreateFromCons(p, pScan, p1, pTail);
7686 
7687   /* If no range/ORDER BY passed by the caller, create a version of the
7688   ** index for each range constraint.  */
7689   if( pTail==0 ){
7690     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7691       assert( pCon->pLink==0 );
7692       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7693         rc = idxCreateFromCons(p, pScan, p1, pCon);
7694       }
7695     }
7696   }
7697 
7698   return rc;
7699 }
7700 
7701 /*
7702 ** Create candidate indexes in database [dbm] based on the data in
7703 ** linked-list pScan.
7704 */
7705 static int idxCreateCandidates(sqlite3expert *p){
7706   int rc = SQLITE_OK;
7707   IdxScan *pIter;
7708 
7709   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7710     rc = idxCreateFromWhere(p, pIter, 0);
7711     if( rc==SQLITE_OK && pIter->pOrder ){
7712       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7713     }
7714   }
7715 
7716   return rc;
7717 }
7718 
7719 /*
7720 ** Free all elements of the linked list starting at pConstraint.
7721 */
7722 static void idxConstraintFree(IdxConstraint *pConstraint){
7723   IdxConstraint *pNext;
7724   IdxConstraint *p;
7725 
7726   for(p=pConstraint; p; p=pNext){
7727     pNext = p->pNext;
7728     sqlite3_free(p);
7729   }
7730 }
7731 
7732 /*
7733 ** Free all elements of the linked list starting from pScan up until pLast
7734 ** (pLast is not freed).
7735 */
7736 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7737   IdxScan *p;
7738   IdxScan *pNext;
7739   for(p=pScan; p!=pLast; p=pNext){
7740     pNext = p->pNextScan;
7741     idxConstraintFree(p->pOrder);
7742     idxConstraintFree(p->pEq);
7743     idxConstraintFree(p->pRange);
7744     sqlite3_free(p);
7745   }
7746 }
7747 
7748 /*
7749 ** Free all elements of the linked list starting from pStatement up
7750 ** until pLast (pLast is not freed).
7751 */
7752 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7753   IdxStatement *p;
7754   IdxStatement *pNext;
7755   for(p=pStatement; p!=pLast; p=pNext){
7756     pNext = p->pNext;
7757     sqlite3_free(p->zEQP);
7758     sqlite3_free(p->zIdx);
7759     sqlite3_free(p);
7760   }
7761 }
7762 
7763 /*
7764 ** Free the linked list of IdxTable objects starting at pTab.
7765 */
7766 static void idxTableFree(IdxTable *pTab){
7767   IdxTable *pIter;
7768   IdxTable *pNext;
7769   for(pIter=pTab; pIter; pIter=pNext){
7770     pNext = pIter->pNext;
7771     sqlite3_free(pIter);
7772   }
7773 }
7774 
7775 /*
7776 ** Free the linked list of IdxWrite objects starting at pTab.
7777 */
7778 static void idxWriteFree(IdxWrite *pTab){
7779   IdxWrite *pIter;
7780   IdxWrite *pNext;
7781   for(pIter=pTab; pIter; pIter=pNext){
7782     pNext = pIter->pNext;
7783     sqlite3_free(pIter);
7784   }
7785 }
7786 
7787 
7788 
7789 /*
7790 ** This function is called after candidate indexes have been created. It
7791 ** runs all the queries to see which indexes they prefer, and populates
7792 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7793 */
7794 int idxFindIndexes(
7795   sqlite3expert *p,
7796   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
7797 ){
7798   IdxStatement *pStmt;
7799   sqlite3 *dbm = p->dbm;
7800   int rc = SQLITE_OK;
7801 
7802   IdxHash hIdx;
7803   idxHashInit(&hIdx);
7804 
7805   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7806     IdxHashEntry *pEntry;
7807     sqlite3_stmt *pExplain = 0;
7808     idxHashClear(&hIdx);
7809     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7810         "EXPLAIN QUERY PLAN %s", pStmt->zSql
7811     );
7812     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7813       /* int iId = sqlite3_column_int(pExplain, 0); */
7814       /* int iParent = sqlite3_column_int(pExplain, 1); */
7815       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7816       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7817       int nDetail = STRLEN(zDetail);
7818       int i;
7819 
7820       for(i=0; i<nDetail; i++){
7821         const char *zIdx = 0;
7822         if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7823           zIdx = &zDetail[i+13];
7824         }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7825           zIdx = &zDetail[i+22];
7826         }
7827         if( zIdx ){
7828           const char *zSql;
7829           int nIdx = 0;
7830           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7831             nIdx++;
7832           }
7833           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7834           if( zSql ){
7835             idxHashAdd(&rc, &hIdx, zSql, 0);
7836             if( rc ) goto find_indexes_out;
7837           }
7838           break;
7839         }
7840       }
7841 
7842       if( zDetail[0]!='-' ){
7843         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7844       }
7845     }
7846 
7847     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7848       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7849     }
7850 
7851     idxFinalize(&rc, pExplain);
7852   }
7853 
7854  find_indexes_out:
7855   idxHashClear(&hIdx);
7856   return rc;
7857 }
7858 
7859 static int idxAuthCallback(
7860   void *pCtx,
7861   int eOp,
7862   const char *z3,
7863   const char *z4,
7864   const char *zDb,
7865   const char *zTrigger
7866 ){
7867   int rc = SQLITE_OK;
7868   (void)z4;
7869   (void)zTrigger;
7870   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7871     if( sqlite3_stricmp(zDb, "main")==0 ){
7872       sqlite3expert *p = (sqlite3expert*)pCtx;
7873       IdxTable *pTab;
7874       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7875         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7876       }
7877       if( pTab ){
7878         IdxWrite *pWrite;
7879         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7880           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7881         }
7882         if( pWrite==0 ){
7883           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7884           if( rc==SQLITE_OK ){
7885             pWrite->pTab = pTab;
7886             pWrite->eOp = eOp;
7887             pWrite->pNext = p->pWrite;
7888             p->pWrite = pWrite;
7889           }
7890         }
7891       }
7892     }
7893   }
7894   return rc;
7895 }
7896 
7897 static int idxProcessOneTrigger(
7898   sqlite3expert *p,
7899   IdxWrite *pWrite,
7900   char **pzErr
7901 ){
7902   static const char *zInt = UNIQUE_TABLE_NAME;
7903   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7904   IdxTable *pTab = pWrite->pTab;
7905   const char *zTab = pTab->zName;
7906   const char *zSql =
7907     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7908     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7909     "ORDER BY type;";
7910   sqlite3_stmt *pSelect = 0;
7911   int rc = SQLITE_OK;
7912   char *zWrite = 0;
7913 
7914   /* Create the table and its triggers in the temp schema */
7915   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7916   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7917     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7918     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7919   }
7920   idxFinalize(&rc, pSelect);
7921 
7922   /* Rename the table in the temp schema to zInt */
7923   if( rc==SQLITE_OK ){
7924     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7925     if( z==0 ){
7926       rc = SQLITE_NOMEM;
7927     }else{
7928       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7929       sqlite3_free(z);
7930     }
7931   }
7932 
7933   switch( pWrite->eOp ){
7934     case SQLITE_INSERT: {
7935       int i;
7936       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7937       for(i=0; i<pTab->nCol; i++){
7938         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7939       }
7940       zWrite = idxAppendText(&rc, zWrite, ")");
7941       break;
7942     }
7943     case SQLITE_UPDATE: {
7944       int i;
7945       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7946       for(i=0; i<pTab->nCol; i++){
7947         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
7948             pTab->aCol[i].zName
7949         );
7950       }
7951       break;
7952     }
7953     default: {
7954       assert( pWrite->eOp==SQLITE_DELETE );
7955       if( rc==SQLITE_OK ){
7956         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7957         if( zWrite==0 ) rc = SQLITE_NOMEM;
7958       }
7959     }
7960   }
7961 
7962   if( rc==SQLITE_OK ){
7963     sqlite3_stmt *pX = 0;
7964     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7965     idxFinalize(&rc, pX);
7966     if( rc!=SQLITE_OK ){
7967       idxDatabaseError(p->dbv, pzErr);
7968     }
7969   }
7970   sqlite3_free(zWrite);
7971 
7972   if( rc==SQLITE_OK ){
7973     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7974   }
7975 
7976   return rc;
7977 }
7978 
7979 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7980   int rc = SQLITE_OK;
7981   IdxWrite *pEnd = 0;
7982   IdxWrite *pFirst = p->pWrite;
7983 
7984   while( rc==SQLITE_OK && pFirst!=pEnd ){
7985     IdxWrite *pIter;
7986     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7987       rc = idxProcessOneTrigger(p, pIter, pzErr);
7988     }
7989     pEnd = pFirst;
7990     pFirst = p->pWrite;
7991   }
7992 
7993   return rc;
7994 }
7995 
7996 
7997 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7998   int rc = idxRegisterVtab(p);
7999   sqlite3_stmt *pSchema = 0;
8000 
8001   /* For each table in the main db schema:
8002   **
8003   **   1) Add an entry to the p->pTable list, and
8004   **   2) Create the equivalent virtual table in dbv.
8005   */
8006   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
8007       "SELECT type, name, sql, 1 FROM sqlite_master "
8008       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
8009       " UNION ALL "
8010       "SELECT type, name, sql, 2 FROM sqlite_master "
8011       "WHERE type = 'trigger'"
8012       "  AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
8013       "ORDER BY 4, 1"
8014   );
8015   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
8016     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
8017     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
8018     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
8019 
8020     if( zType[0]=='v' || zType[1]=='r' ){
8021       rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
8022     }else{
8023       IdxTable *pTab;
8024       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
8025       if( rc==SQLITE_OK ){
8026         int i;
8027         char *zInner = 0;
8028         char *zOuter = 0;
8029         pTab->pNext = p->pTable;
8030         p->pTable = pTab;
8031 
8032         /* The statement the vtab will pass to sqlite3_declare_vtab() */
8033         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
8034         for(i=0; i<pTab->nCol; i++){
8035           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
8036               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
8037           );
8038         }
8039         zInner = idxAppendText(&rc, zInner, ")");
8040 
8041         /* The CVT statement to create the vtab */
8042         zOuter = idxAppendText(&rc, 0,
8043             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
8044         );
8045         if( rc==SQLITE_OK ){
8046           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
8047         }
8048         sqlite3_free(zInner);
8049         sqlite3_free(zOuter);
8050       }
8051     }
8052   }
8053   idxFinalize(&rc, pSchema);
8054   return rc;
8055 }
8056 
8057 struct IdxSampleCtx {
8058   int iTarget;
8059   double target;                  /* Target nRet/nRow value */
8060   double nRow;                    /* Number of rows seen */
8061   double nRet;                    /* Number of rows returned */
8062 };
8063 
8064 static void idxSampleFunc(
8065   sqlite3_context *pCtx,
8066   int argc,
8067   sqlite3_value **argv
8068 ){
8069   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
8070   int bRet;
8071 
8072   (void)argv;
8073   assert( argc==0 );
8074   if( p->nRow==0.0 ){
8075     bRet = 1;
8076   }else{
8077     bRet = (p->nRet / p->nRow) <= p->target;
8078     if( bRet==0 ){
8079       unsigned short rnd;
8080       sqlite3_randomness(2, (void*)&rnd);
8081       bRet = ((int)rnd % 100) <= p->iTarget;
8082     }
8083   }
8084 
8085   sqlite3_result_int(pCtx, bRet);
8086   p->nRow += 1.0;
8087   p->nRet += (double)bRet;
8088 }
8089 
8090 struct IdxRemCtx {
8091   int nSlot;
8092   struct IdxRemSlot {
8093     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
8094     i64 iVal;                     /* SQLITE_INTEGER value */
8095     double rVal;                  /* SQLITE_FLOAT value */
8096     int nByte;                    /* Bytes of space allocated at z */
8097     int n;                        /* Size of buffer z */
8098     char *z;                      /* SQLITE_TEXT/BLOB value */
8099   } aSlot[1];
8100 };
8101 
8102 /*
8103 ** Implementation of scalar function rem().
8104 */
8105 static void idxRemFunc(
8106   sqlite3_context *pCtx,
8107   int argc,
8108   sqlite3_value **argv
8109 ){
8110   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
8111   struct IdxRemSlot *pSlot;
8112   int iSlot;
8113   assert( argc==2 );
8114 
8115   iSlot = sqlite3_value_int(argv[0]);
8116   assert( iSlot<=p->nSlot );
8117   pSlot = &p->aSlot[iSlot];
8118 
8119   switch( pSlot->eType ){
8120     case SQLITE_NULL:
8121       /* no-op */
8122       break;
8123 
8124     case SQLITE_INTEGER:
8125       sqlite3_result_int64(pCtx, pSlot->iVal);
8126       break;
8127 
8128     case SQLITE_FLOAT:
8129       sqlite3_result_double(pCtx, pSlot->rVal);
8130       break;
8131 
8132     case SQLITE_BLOB:
8133       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8134       break;
8135 
8136     case SQLITE_TEXT:
8137       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8138       break;
8139   }
8140 
8141   pSlot->eType = sqlite3_value_type(argv[1]);
8142   switch( pSlot->eType ){
8143     case SQLITE_NULL:
8144       /* no-op */
8145       break;
8146 
8147     case SQLITE_INTEGER:
8148       pSlot->iVal = sqlite3_value_int64(argv[1]);
8149       break;
8150 
8151     case SQLITE_FLOAT:
8152       pSlot->rVal = sqlite3_value_double(argv[1]);
8153       break;
8154 
8155     case SQLITE_BLOB:
8156     case SQLITE_TEXT: {
8157       int nByte = sqlite3_value_bytes(argv[1]);
8158       if( nByte>pSlot->nByte ){
8159         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
8160         if( zNew==0 ){
8161           sqlite3_result_error_nomem(pCtx);
8162           return;
8163         }
8164         pSlot->nByte = nByte*2;
8165         pSlot->z = zNew;
8166       }
8167       pSlot->n = nByte;
8168       if( pSlot->eType==SQLITE_BLOB ){
8169         memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
8170       }else{
8171         memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
8172       }
8173       break;
8174     }
8175   }
8176 }
8177 
8178 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
8179   int rc = SQLITE_OK;
8180   const char *zMax =
8181     "SELECT max(i.seqno) FROM "
8182     "  sqlite_master AS s, "
8183     "  pragma_index_list(s.name) AS l, "
8184     "  pragma_index_info(l.name) AS i "
8185     "WHERE s.type = 'table'";
8186   sqlite3_stmt *pMax = 0;
8187 
8188   *pnMax = 0;
8189   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
8190   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8191     *pnMax = sqlite3_column_int(pMax, 0) + 1;
8192   }
8193   idxFinalize(&rc, pMax);
8194 
8195   return rc;
8196 }
8197 
8198 static int idxPopulateOneStat1(
8199   sqlite3expert *p,
8200   sqlite3_stmt *pIndexXInfo,
8201   sqlite3_stmt *pWriteStat,
8202   const char *zTab,
8203   const char *zIdx,
8204   char **pzErr
8205 ){
8206   char *zCols = 0;
8207   char *zOrder = 0;
8208   char *zQuery = 0;
8209   int nCol = 0;
8210   int i;
8211   sqlite3_stmt *pQuery = 0;
8212   int *aStat = 0;
8213   int rc = SQLITE_OK;
8214 
8215   assert( p->iSample>0 );
8216 
8217   /* Formulate the query text */
8218   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8219   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8220     const char *zComma = zCols==0 ? "" : ", ";
8221     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8222     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8223     zCols = idxAppendText(&rc, zCols,
8224         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8225     );
8226     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8227   }
8228   sqlite3_reset(pIndexXInfo);
8229   if( rc==SQLITE_OK ){
8230     if( p->iSample==100 ){
8231       zQuery = sqlite3_mprintf(
8232           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8233       );
8234     }else{
8235       zQuery = sqlite3_mprintf(
8236           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8237       );
8238     }
8239   }
8240   sqlite3_free(zCols);
8241   sqlite3_free(zOrder);
8242 
8243   /* Formulate the query text */
8244   if( rc==SQLITE_OK ){
8245     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8246     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8247   }
8248   sqlite3_free(zQuery);
8249 
8250   if( rc==SQLITE_OK ){
8251     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8252   }
8253   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8254     IdxHashEntry *pEntry;
8255     char *zStat = 0;
8256     for(i=0; i<=nCol; i++) aStat[i] = 1;
8257     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8258       aStat[0]++;
8259       for(i=0; i<nCol; i++){
8260         if( sqlite3_column_int(pQuery, i)==0 ) break;
8261       }
8262       for(/*no-op*/; i<nCol; i++){
8263         aStat[i+1]++;
8264       }
8265     }
8266 
8267     if( rc==SQLITE_OK ){
8268       int s0 = aStat[0];
8269       zStat = sqlite3_mprintf("%d", s0);
8270       if( zStat==0 ) rc = SQLITE_NOMEM;
8271       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8272         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8273       }
8274     }
8275 
8276     if( rc==SQLITE_OK ){
8277       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8278       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8279       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8280       sqlite3_step(pWriteStat);
8281       rc = sqlite3_reset(pWriteStat);
8282     }
8283 
8284     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8285     if( pEntry ){
8286       assert( pEntry->zVal2==0 );
8287       pEntry->zVal2 = zStat;
8288     }else{
8289       sqlite3_free(zStat);
8290     }
8291   }
8292   sqlite3_free(aStat);
8293   idxFinalize(&rc, pQuery);
8294 
8295   return rc;
8296 }
8297 
8298 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8299   int rc;
8300   char *zSql;
8301 
8302   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8303   if( rc!=SQLITE_OK ) return rc;
8304 
8305   zSql = sqlite3_mprintf(
8306       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8307   );
8308   if( zSql==0 ) return SQLITE_NOMEM;
8309   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8310   sqlite3_free(zSql);
8311 
8312   return rc;
8313 }
8314 
8315 /*
8316 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8317 ** indexes have already been created in database sqlite3expert.dbm, this
8318 ** function populates sqlite_stat1 table in the same database.
8319 **
8320 ** The stat1 data is generated by querying the
8321 */
8322 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8323   int rc = SQLITE_OK;
8324   int nMax =0;
8325   struct IdxRemCtx *pCtx = 0;
8326   struct IdxSampleCtx samplectx;
8327   int i;
8328   i64 iPrev = -100000;
8329   sqlite3_stmt *pAllIndex = 0;
8330   sqlite3_stmt *pIndexXInfo = 0;
8331   sqlite3_stmt *pWrite = 0;
8332 
8333   const char *zAllIndex =
8334     "SELECT s.rowid, s.name, l.name FROM "
8335     "  sqlite_master AS s, "
8336     "  pragma_index_list(s.name) AS l "
8337     "WHERE s.type = 'table'";
8338   const char *zIndexXInfo =
8339     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8340   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8341 
8342   /* If iSample==0, no sqlite_stat1 data is required. */
8343   if( p->iSample==0 ) return SQLITE_OK;
8344 
8345   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8346   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8347 
8348   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8349 
8350   if( rc==SQLITE_OK ){
8351     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8352     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8353   }
8354 
8355   if( rc==SQLITE_OK ){
8356     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8357     rc = sqlite3_create_function(
8358         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8359     );
8360   }
8361   if( rc==SQLITE_OK ){
8362     rc = sqlite3_create_function(
8363         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8364     );
8365   }
8366 
8367   if( rc==SQLITE_OK ){
8368     pCtx->nSlot = nMax+1;
8369     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8370   }
8371   if( rc==SQLITE_OK ){
8372     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8373   }
8374   if( rc==SQLITE_OK ){
8375     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8376   }
8377 
8378   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8379     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8380     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8381     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8382     if( p->iSample<100 && iPrev!=iRowid ){
8383       samplectx.target = (double)p->iSample / 100.0;
8384       samplectx.iTarget = p->iSample;
8385       samplectx.nRow = 0.0;
8386       samplectx.nRet = 0.0;
8387       rc = idxBuildSampleTable(p, zTab);
8388       if( rc!=SQLITE_OK ) break;
8389     }
8390     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8391     iPrev = iRowid;
8392   }
8393   if( rc==SQLITE_OK && p->iSample<100 ){
8394     rc = sqlite3_exec(p->dbv,
8395         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8396     );
8397   }
8398 
8399   idxFinalize(&rc, pAllIndex);
8400   idxFinalize(&rc, pIndexXInfo);
8401   idxFinalize(&rc, pWrite);
8402 
8403   for(i=0; i<pCtx->nSlot; i++){
8404     sqlite3_free(pCtx->aSlot[i].z);
8405   }
8406   sqlite3_free(pCtx);
8407 
8408   if( rc==SQLITE_OK ){
8409     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8410   }
8411 
8412   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8413   return rc;
8414 }
8415 
8416 /*
8417 ** Allocate a new sqlite3expert object.
8418 */
8419 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8420   int rc = SQLITE_OK;
8421   sqlite3expert *pNew;
8422 
8423   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8424 
8425   /* Open two in-memory databases to work with. The "vtab database" (dbv)
8426   ** will contain a virtual table corresponding to each real table in
8427   ** the user database schema, and a copy of each view. It is used to
8428   ** collect information regarding the WHERE, ORDER BY and other clauses
8429   ** of the user's query.
8430   */
8431   if( rc==SQLITE_OK ){
8432     pNew->db = db;
8433     pNew->iSample = 100;
8434     rc = sqlite3_open(":memory:", &pNew->dbv);
8435   }
8436   if( rc==SQLITE_OK ){
8437     rc = sqlite3_open(":memory:", &pNew->dbm);
8438     if( rc==SQLITE_OK ){
8439       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8440     }
8441   }
8442 
8443 
8444   /* Copy the entire schema of database [db] into [dbm]. */
8445   if( rc==SQLITE_OK ){
8446     sqlite3_stmt *pSql;
8447     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
8448         "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8449         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8450     );
8451     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8452       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8453       rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8454     }
8455     idxFinalize(&rc, pSql);
8456   }
8457 
8458   /* Create the vtab schema */
8459   if( rc==SQLITE_OK ){
8460     rc = idxCreateVtabSchema(pNew, pzErrmsg);
8461   }
8462 
8463   /* Register the auth callback with dbv */
8464   if( rc==SQLITE_OK ){
8465     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8466   }
8467 
8468   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8469   ** return the new sqlite3expert handle.  */
8470   if( rc!=SQLITE_OK ){
8471     sqlite3_expert_destroy(pNew);
8472     pNew = 0;
8473   }
8474   return pNew;
8475 }
8476 
8477 /*
8478 ** Configure an sqlite3expert object.
8479 */
8480 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8481   int rc = SQLITE_OK;
8482   va_list ap;
8483   va_start(ap, op);
8484   switch( op ){
8485     case EXPERT_CONFIG_SAMPLE: {
8486       int iVal = va_arg(ap, int);
8487       if( iVal<0 ) iVal = 0;
8488       if( iVal>100 ) iVal = 100;
8489       p->iSample = iVal;
8490       break;
8491     }
8492     default:
8493       rc = SQLITE_NOTFOUND;
8494       break;
8495   }
8496 
8497   va_end(ap);
8498   return rc;
8499 }
8500 
8501 /*
8502 ** Add an SQL statement to the analysis.
8503 */
8504 int sqlite3_expert_sql(
8505   sqlite3expert *p,               /* From sqlite3_expert_new() */
8506   const char *zSql,               /* SQL statement to add */
8507   char **pzErr                    /* OUT: Error message (if any) */
8508 ){
8509   IdxScan *pScanOrig = p->pScan;
8510   IdxStatement *pStmtOrig = p->pStatement;
8511   int rc = SQLITE_OK;
8512   const char *zStmt = zSql;
8513 
8514   if( p->bRun ) return SQLITE_MISUSE;
8515 
8516   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8517     sqlite3_stmt *pStmt = 0;
8518     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8519     if( rc==SQLITE_OK ){
8520       if( pStmt ){
8521         IdxStatement *pNew;
8522         const char *z = sqlite3_sql(pStmt);
8523         int n = STRLEN(z);
8524         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8525         if( rc==SQLITE_OK ){
8526           pNew->zSql = (char*)&pNew[1];
8527           memcpy(pNew->zSql, z, n+1);
8528           pNew->pNext = p->pStatement;
8529           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8530           p->pStatement = pNew;
8531         }
8532         sqlite3_finalize(pStmt);
8533       }
8534     }else{
8535       idxDatabaseError(p->dbv, pzErr);
8536     }
8537   }
8538 
8539   if( rc!=SQLITE_OK ){
8540     idxScanFree(p->pScan, pScanOrig);
8541     idxStatementFree(p->pStatement, pStmtOrig);
8542     p->pScan = pScanOrig;
8543     p->pStatement = pStmtOrig;
8544   }
8545 
8546   return rc;
8547 }
8548 
8549 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8550   int rc;
8551   IdxHashEntry *pEntry;
8552 
8553   /* Do trigger processing to collect any extra IdxScan structures */
8554   rc = idxProcessTriggers(p, pzErr);
8555 
8556   /* Create candidate indexes within the in-memory database file */
8557   if( rc==SQLITE_OK ){
8558     rc = idxCreateCandidates(p);
8559   }
8560 
8561   /* Generate the stat1 data */
8562   if( rc==SQLITE_OK ){
8563     rc = idxPopulateStat1(p, pzErr);
8564   }
8565 
8566   /* Formulate the EXPERT_REPORT_CANDIDATES text */
8567   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8568     p->zCandidates = idxAppendText(&rc, p->zCandidates,
8569         "%s;%s%s\n", pEntry->zVal,
8570         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8571     );
8572   }
8573 
8574   /* Figure out which of the candidate indexes are preferred by the query
8575   ** planner and report the results to the user.  */
8576   if( rc==SQLITE_OK ){
8577     rc = idxFindIndexes(p, pzErr);
8578   }
8579 
8580   if( rc==SQLITE_OK ){
8581     p->bRun = 1;
8582   }
8583   return rc;
8584 }
8585 
8586 /*
8587 ** Return the total number of statements that have been added to this
8588 ** sqlite3expert using sqlite3_expert_sql().
8589 */
8590 int sqlite3_expert_count(sqlite3expert *p){
8591   int nRet = 0;
8592   if( p->pStatement ) nRet = p->pStatement->iId+1;
8593   return nRet;
8594 }
8595 
8596 /*
8597 ** Return a component of the report.
8598 */
8599 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8600   const char *zRet = 0;
8601   IdxStatement *pStmt;
8602 
8603   if( p->bRun==0 ) return 0;
8604   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8605   switch( eReport ){
8606     case EXPERT_REPORT_SQL:
8607       if( pStmt ) zRet = pStmt->zSql;
8608       break;
8609     case EXPERT_REPORT_INDEXES:
8610       if( pStmt ) zRet = pStmt->zIdx;
8611       break;
8612     case EXPERT_REPORT_PLAN:
8613       if( pStmt ) zRet = pStmt->zEQP;
8614       break;
8615     case EXPERT_REPORT_CANDIDATES:
8616       zRet = p->zCandidates;
8617       break;
8618   }
8619   return zRet;
8620 }
8621 
8622 /*
8623 ** Free an sqlite3expert object.
8624 */
8625 void sqlite3_expert_destroy(sqlite3expert *p){
8626   if( p ){
8627     sqlite3_close(p->dbm);
8628     sqlite3_close(p->dbv);
8629     idxScanFree(p->pScan, 0);
8630     idxStatementFree(p->pStatement, 0);
8631     idxTableFree(p->pTable);
8632     idxWriteFree(p->pWrite);
8633     idxHashClear(&p->hIdx);
8634     sqlite3_free(p->zCandidates);
8635     sqlite3_free(p);
8636   }
8637 }
8638 
8639 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
8640 
8641 /************************* End ../ext/expert/sqlite3expert.c ********************/
8642 
8643 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8644 /************************* Begin ../ext/misc/dbdata.c ******************/
8645 /*
8646 ** 2019-04-17
8647 **
8648 ** The author disclaims copyright to this source code.  In place of
8649 ** a legal notice, here is a blessing:
8650 **
8651 **    May you do good and not evil.
8652 **    May you find forgiveness for yourself and forgive others.
8653 **    May you share freely, never taking more than you give.
8654 **
8655 ******************************************************************************
8656 **
8657 ** This file contains an implementation of two eponymous virtual tables,
8658 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
8659 ** "sqlite_dbpage" eponymous virtual table be available.
8660 **
8661 ** SQLITE_DBDATA:
8662 **   sqlite_dbdata is used to extract data directly from a database b-tree
8663 **   page and its associated overflow pages, bypassing the b-tree layer.
8664 **   The table schema is equivalent to:
8665 **
8666 **     CREATE TABLE sqlite_dbdata(
8667 **       pgno INTEGER,
8668 **       cell INTEGER,
8669 **       field INTEGER,
8670 **       value ANY,
8671 **       schema TEXT HIDDEN
8672 **     );
8673 **
8674 **   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
8675 **   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
8676 **   "schema".
8677 **
8678 **   Each page of the database is inspected. If it cannot be interpreted as
8679 **   a b-tree page, or if it is a b-tree page containing 0 entries, the
8680 **   sqlite_dbdata table contains no rows for that page.  Otherwise, the
8681 **   table contains one row for each field in the record associated with
8682 **   each cell on the page. For intkey b-trees, the key value is stored in
8683 **   field -1.
8684 **
8685 **   For example, for the database:
8686 **
8687 **     CREATE TABLE t1(a, b);     -- root page is page 2
8688 **     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
8689 **     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
8690 **
8691 **   the sqlite_dbdata table contains, as well as from entries related to
8692 **   page 1, content equivalent to:
8693 **
8694 **     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
8695 **         (2, 0, -1, 5     ),
8696 **         (2, 0,  0, 'v'   ),
8697 **         (2, 0,  1, 'five'),
8698 **         (2, 1, -1, 10    ),
8699 **         (2, 1,  0, 'x'   ),
8700 **         (2, 1,  1, 'ten' );
8701 **
8702 **   If database corruption is encountered, this module does not report an
8703 **   error. Instead, it attempts to extract as much data as possible and
8704 **   ignores the corruption.
8705 **
8706 ** SQLITE_DBPTR:
8707 **   The sqlite_dbptr table has the following schema:
8708 **
8709 **     CREATE TABLE sqlite_dbptr(
8710 **       pgno INTEGER,
8711 **       child INTEGER,
8712 **       schema TEXT HIDDEN
8713 **     );
8714 **
8715 **   It contains one entry for each b-tree pointer between a parent and
8716 **   child page in the database.
8717 */
8718 #if !defined(SQLITEINT_H)
8719 /* #include "sqlite3ext.h" */
8720 
8721 /* typedef unsigned char u8; */
8722 
8723 #endif
8724 SQLITE_EXTENSION_INIT1
8725 #include <string.h>
8726 #include <assert.h>
8727 
8728 #define DBDATA_PADDING_BYTES 100
8729 
8730 typedef struct DbdataTable DbdataTable;
8731 typedef struct DbdataCursor DbdataCursor;
8732 
8733 /* Cursor object */
8734 struct DbdataCursor {
8735   sqlite3_vtab_cursor base;       /* Base class.  Must be first */
8736   sqlite3_stmt *pStmt;            /* For fetching database pages */
8737 
8738   int iPgno;                      /* Current page number */
8739   u8 *aPage;                      /* Buffer containing page */
8740   int nPage;                      /* Size of aPage[] in bytes */
8741   int nCell;                      /* Number of cells on aPage[] */
8742   int iCell;                      /* Current cell number */
8743   int bOnePage;                   /* True to stop after one page */
8744   int szDb;
8745   sqlite3_int64 iRowid;
8746 
8747   /* Only for the sqlite_dbdata table */
8748   u8 *pRec;                       /* Buffer containing current record */
8749   int nRec;                       /* Size of pRec[] in bytes */
8750   int nHdr;                       /* Size of header in bytes */
8751   int iField;                     /* Current field number */
8752   u8 *pHdrPtr;
8753   u8 *pPtr;
8754 
8755   sqlite3_int64 iIntkey;          /* Integer key value */
8756 };
8757 
8758 /* Table object */
8759 struct DbdataTable {
8760   sqlite3_vtab base;              /* Base class.  Must be first */
8761   sqlite3 *db;                    /* The database connection */
8762   sqlite3_stmt *pStmt;            /* For fetching database pages */
8763   int bPtr;                       /* True for sqlite3_dbptr table */
8764 };
8765 
8766 /* Column and schema definitions for sqlite_dbdata */
8767 #define DBDATA_COLUMN_PGNO        0
8768 #define DBDATA_COLUMN_CELL        1
8769 #define DBDATA_COLUMN_FIELD       2
8770 #define DBDATA_COLUMN_VALUE       3
8771 #define DBDATA_COLUMN_SCHEMA      4
8772 #define DBDATA_SCHEMA             \
8773       "CREATE TABLE x("           \
8774       "  pgno INTEGER,"           \
8775       "  cell INTEGER,"           \
8776       "  field INTEGER,"          \
8777       "  value ANY,"              \
8778       "  schema TEXT HIDDEN"      \
8779       ")"
8780 
8781 /* Column and schema definitions for sqlite_dbptr */
8782 #define DBPTR_COLUMN_PGNO         0
8783 #define DBPTR_COLUMN_CHILD        1
8784 #define DBPTR_COLUMN_SCHEMA       2
8785 #define DBPTR_SCHEMA              \
8786       "CREATE TABLE x("           \
8787       "  pgno INTEGER,"           \
8788       "  child INTEGER,"          \
8789       "  schema TEXT HIDDEN"      \
8790       ")"
8791 
8792 /*
8793 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
8794 ** table.
8795 */
8796 static int dbdataConnect(
8797   sqlite3 *db,
8798   void *pAux,
8799   int argc, const char *const*argv,
8800   sqlite3_vtab **ppVtab,
8801   char **pzErr
8802 ){
8803   DbdataTable *pTab = 0;
8804   int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
8805 
8806   if( rc==SQLITE_OK ){
8807     pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
8808     if( pTab==0 ){
8809       rc = SQLITE_NOMEM;
8810     }else{
8811       memset(pTab, 0, sizeof(DbdataTable));
8812       pTab->db = db;
8813       pTab->bPtr = (pAux!=0);
8814     }
8815   }
8816 
8817   *ppVtab = (sqlite3_vtab*)pTab;
8818   return rc;
8819 }
8820 
8821 /*
8822 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
8823 */
8824 static int dbdataDisconnect(sqlite3_vtab *pVtab){
8825   DbdataTable *pTab = (DbdataTable*)pVtab;
8826   if( pTab ){
8827     sqlite3_finalize(pTab->pStmt);
8828     sqlite3_free(pVtab);
8829   }
8830   return SQLITE_OK;
8831 }
8832 
8833 /*
8834 ** This function interprets two types of constraints:
8835 **
8836 **       schema=?
8837 **       pgno=?
8838 **
8839 ** If neither are present, idxNum is set to 0. If schema=? is present,
8840 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
8841 ** in idxNum is set.
8842 **
8843 ** If both parameters are present, schema is in position 0 and pgno in
8844 ** position 1.
8845 */
8846 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
8847   DbdataTable *pTab = (DbdataTable*)tab;
8848   int i;
8849   int iSchema = -1;
8850   int iPgno = -1;
8851   int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
8852 
8853   for(i=0; i<pIdx->nConstraint; i++){
8854     struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
8855     if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8856       if( p->iColumn==colSchema ){
8857         if( p->usable==0 ) return SQLITE_CONSTRAINT;
8858         iSchema = i;
8859       }
8860       if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
8861         iPgno = i;
8862       }
8863     }
8864   }
8865 
8866   if( iSchema>=0 ){
8867     pIdx->aConstraintUsage[iSchema].argvIndex = 1;
8868     pIdx->aConstraintUsage[iSchema].omit = 1;
8869   }
8870   if( iPgno>=0 ){
8871     pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
8872     pIdx->aConstraintUsage[iPgno].omit = 1;
8873     pIdx->estimatedCost = 100;
8874     pIdx->estimatedRows =  50;
8875 
8876     if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
8877       int iCol = pIdx->aOrderBy[0].iColumn;
8878       if( pIdx->nOrderBy==1 ){
8879         pIdx->orderByConsumed = (iCol==0 || iCol==1);
8880       }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
8881         pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
8882       }
8883     }
8884 
8885   }else{
8886     pIdx->estimatedCost = 100000000;
8887     pIdx->estimatedRows = 1000000000;
8888   }
8889   pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
8890   return SQLITE_OK;
8891 }
8892 
8893 /*
8894 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
8895 */
8896 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
8897   DbdataCursor *pCsr;
8898 
8899   pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
8900   if( pCsr==0 ){
8901     return SQLITE_NOMEM;
8902   }else{
8903     memset(pCsr, 0, sizeof(DbdataCursor));
8904     pCsr->base.pVtab = pVTab;
8905   }
8906 
8907   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
8908   return SQLITE_OK;
8909 }
8910 
8911 /*
8912 ** Restore a cursor object to the state it was in when first allocated
8913 ** by dbdataOpen().
8914 */
8915 static void dbdataResetCursor(DbdataCursor *pCsr){
8916   DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
8917   if( pTab->pStmt==0 ){
8918     pTab->pStmt = pCsr->pStmt;
8919   }else{
8920     sqlite3_finalize(pCsr->pStmt);
8921   }
8922   pCsr->pStmt = 0;
8923   pCsr->iPgno = 1;
8924   pCsr->iCell = 0;
8925   pCsr->iField = 0;
8926   pCsr->bOnePage = 0;
8927   sqlite3_free(pCsr->aPage);
8928   sqlite3_free(pCsr->pRec);
8929   pCsr->pRec = 0;
8930   pCsr->aPage = 0;
8931 }
8932 
8933 /*
8934 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
8935 */
8936 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
8937   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
8938   dbdataResetCursor(pCsr);
8939   sqlite3_free(pCsr);
8940   return SQLITE_OK;
8941 }
8942 
8943 /*
8944 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
8945 */
8946 static unsigned int get_uint16(unsigned char *a){
8947   return (a[0]<<8)|a[1];
8948 }
8949 static unsigned int get_uint32(unsigned char *a){
8950   return ((unsigned int)a[0]<<24)
8951        | ((unsigned int)a[1]<<16)
8952        | ((unsigned int)a[2]<<8)
8953        | ((unsigned int)a[3]);
8954 }
8955 
8956 /*
8957 ** Load page pgno from the database via the sqlite_dbpage virtual table.
8958 ** If successful, set (*ppPage) to point to a buffer containing the page
8959 ** data, (*pnPage) to the size of that buffer in bytes and return
8960 ** SQLITE_OK. In this case it is the responsibility of the caller to
8961 ** eventually free the buffer using sqlite3_free().
8962 **
8963 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
8964 ** return an SQLite error code.
8965 */
8966 static int dbdataLoadPage(
8967   DbdataCursor *pCsr,             /* Cursor object */
8968   unsigned int pgno,              /* Page number of page to load */
8969   u8 **ppPage,                    /* OUT: pointer to page buffer */
8970   int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
8971 ){
8972   int rc2;
8973   int rc = SQLITE_OK;
8974   sqlite3_stmt *pStmt = pCsr->pStmt;
8975 
8976   *ppPage = 0;
8977   *pnPage = 0;
8978   sqlite3_bind_int64(pStmt, 2, pgno);
8979   if( SQLITE_ROW==sqlite3_step(pStmt) ){
8980     int nCopy = sqlite3_column_bytes(pStmt, 0);
8981     if( nCopy>0 ){
8982       u8 *pPage;
8983       pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
8984       if( pPage==0 ){
8985         rc = SQLITE_NOMEM;
8986       }else{
8987         const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
8988         memcpy(pPage, pCopy, nCopy);
8989         memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
8990       }
8991       *ppPage = pPage;
8992       *pnPage = nCopy;
8993     }
8994   }
8995   rc2 = sqlite3_reset(pStmt);
8996   if( rc==SQLITE_OK ) rc = rc2;
8997 
8998   return rc;
8999 }
9000 
9001 /*
9002 ** Read a varint.  Put the value in *pVal and return the number of bytes.
9003 */
9004 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
9005   sqlite3_int64 v = 0;
9006   int i;
9007   for(i=0; i<8; i++){
9008     v = (v<<7) + (z[i]&0x7f);
9009     if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
9010   }
9011   v = (v<<8) + (z[i]&0xff);
9012   *pVal = v;
9013   return 9;
9014 }
9015 
9016 /*
9017 ** Return the number of bytes of space used by an SQLite value of type
9018 ** eType.
9019 */
9020 static int dbdataValueBytes(int eType){
9021   switch( eType ){
9022     case 0: case 8: case 9:
9023     case 10: case 11:
9024       return 0;
9025     case 1:
9026       return 1;
9027     case 2:
9028       return 2;
9029     case 3:
9030       return 3;
9031     case 4:
9032       return 4;
9033     case 5:
9034       return 6;
9035     case 6:
9036     case 7:
9037       return 8;
9038     default:
9039       if( eType>0 ){
9040         return ((eType-12) / 2);
9041       }
9042       return 0;
9043   }
9044 }
9045 
9046 /*
9047 ** Load a value of type eType from buffer pData and use it to set the
9048 ** result of context object pCtx.
9049 */
9050 static void dbdataValue(
9051   sqlite3_context *pCtx,
9052   int eType,
9053   u8 *pData,
9054   int nData
9055 ){
9056   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
9057     switch( eType ){
9058       case 0:
9059       case 10:
9060       case 11:
9061         sqlite3_result_null(pCtx);
9062         break;
9063 
9064       case 8:
9065         sqlite3_result_int(pCtx, 0);
9066         break;
9067       case 9:
9068         sqlite3_result_int(pCtx, 1);
9069         break;
9070 
9071       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
9072         sqlite3_uint64 v = (signed char)pData[0];
9073         pData++;
9074         switch( eType ){
9075           case 7:
9076           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
9077           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
9078           case 4:  v = (v<<8) + pData[0];  pData++;
9079           case 3:  v = (v<<8) + pData[0];  pData++;
9080           case 2:  v = (v<<8) + pData[0];  pData++;
9081         }
9082 
9083         if( eType==7 ){
9084           double r;
9085           memcpy(&r, &v, sizeof(r));
9086           sqlite3_result_double(pCtx, r);
9087         }else{
9088           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
9089         }
9090         break;
9091       }
9092 
9093       default: {
9094         int n = ((eType-12) / 2);
9095         if( eType % 2 ){
9096           sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
9097         }else{
9098           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
9099         }
9100       }
9101     }
9102   }
9103 }
9104 
9105 /*
9106 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
9107 */
9108 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
9109   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9110   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9111 
9112   pCsr->iRowid++;
9113   while( 1 ){
9114     int rc;
9115     int iOff = (pCsr->iPgno==1 ? 100 : 0);
9116     int bNextPage = 0;
9117 
9118     if( pCsr->aPage==0 ){
9119       while( 1 ){
9120         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
9121         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
9122         if( rc!=SQLITE_OK ) return rc;
9123         if( pCsr->aPage ) break;
9124         pCsr->iPgno++;
9125       }
9126       pCsr->iCell = pTab->bPtr ? -2 : 0;
9127       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
9128     }
9129 
9130     if( pTab->bPtr ){
9131       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
9132         pCsr->iCell = pCsr->nCell;
9133       }
9134       pCsr->iCell++;
9135       if( pCsr->iCell>=pCsr->nCell ){
9136         sqlite3_free(pCsr->aPage);
9137         pCsr->aPage = 0;
9138         if( pCsr->bOnePage ) return SQLITE_OK;
9139         pCsr->iPgno++;
9140       }else{
9141         return SQLITE_OK;
9142       }
9143     }else{
9144       /* If there is no record loaded, load it now. */
9145       if( pCsr->pRec==0 ){
9146         int bHasRowid = 0;
9147         int nPointer = 0;
9148         sqlite3_int64 nPayload = 0;
9149         sqlite3_int64 nHdr = 0;
9150         int iHdr;
9151         int U, X;
9152         int nLocal;
9153 
9154         switch( pCsr->aPage[iOff] ){
9155           case 0x02:
9156             nPointer = 4;
9157             break;
9158           case 0x0a:
9159             break;
9160           case 0x0d:
9161             bHasRowid = 1;
9162             break;
9163           default:
9164             /* This is not a b-tree page with records on it. Continue. */
9165             pCsr->iCell = pCsr->nCell;
9166             break;
9167         }
9168 
9169         if( pCsr->iCell>=pCsr->nCell ){
9170           bNextPage = 1;
9171         }else{
9172 
9173           iOff += 8 + nPointer + pCsr->iCell*2;
9174           if( iOff>pCsr->nPage ){
9175             bNextPage = 1;
9176           }else{
9177             iOff = get_uint16(&pCsr->aPage[iOff]);
9178           }
9179 
9180           /* For an interior node cell, skip past the child-page number */
9181           iOff += nPointer;
9182 
9183           /* Load the "byte of payload including overflow" field */
9184           if( bNextPage || iOff>pCsr->nPage ){
9185             bNextPage = 1;
9186           }else{
9187             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
9188           }
9189 
9190           /* If this is a leaf intkey cell, load the rowid */
9191           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
9192             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
9193           }
9194 
9195           /* Figure out how much data to read from the local page */
9196           U = pCsr->nPage;
9197           if( bHasRowid ){
9198             X = U-35;
9199           }else{
9200             X = ((U-12)*64/255)-23;
9201           }
9202           if( nPayload<=X ){
9203             nLocal = nPayload;
9204           }else{
9205             int M, K;
9206             M = ((U-12)*32/255)-23;
9207             K = M+((nPayload-M)%(U-4));
9208             if( K<=X ){
9209               nLocal = K;
9210             }else{
9211               nLocal = M;
9212             }
9213           }
9214 
9215           if( bNextPage || nLocal+iOff>pCsr->nPage ){
9216             bNextPage = 1;
9217           }else{
9218 
9219             /* Allocate space for payload. And a bit more to catch small buffer
9220             ** overruns caused by attempting to read a varint or similar from
9221             ** near the end of a corrupt record.  */
9222             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
9223             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
9224             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
9225             pCsr->nRec = nPayload;
9226 
9227             /* Load the nLocal bytes of payload */
9228             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
9229             iOff += nLocal;
9230 
9231             /* Load content from overflow pages */
9232             if( nPayload>nLocal ){
9233               sqlite3_int64 nRem = nPayload - nLocal;
9234               unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
9235               while( nRem>0 ){
9236                 u8 *aOvfl = 0;
9237                 int nOvfl = 0;
9238                 int nCopy;
9239                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
9240                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
9241                 if( rc!=SQLITE_OK ) return rc;
9242                 if( aOvfl==0 ) break;
9243 
9244                 nCopy = U-4;
9245                 if( nCopy>nRem ) nCopy = nRem;
9246                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
9247                 nRem -= nCopy;
9248 
9249                 pgnoOvfl = get_uint32(aOvfl);
9250                 sqlite3_free(aOvfl);
9251               }
9252             }
9253 
9254             iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
9255             pCsr->nHdr = nHdr;
9256             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
9257             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
9258             pCsr->iField = (bHasRowid ? -1 : 0);
9259           }
9260         }
9261       }else{
9262         pCsr->iField++;
9263         if( pCsr->iField>0 ){
9264           sqlite3_int64 iType;
9265           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
9266             bNextPage = 1;
9267           }else{
9268             pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
9269             pCsr->pPtr += dbdataValueBytes(iType);
9270           }
9271         }
9272       }
9273 
9274       if( bNextPage ){
9275         sqlite3_free(pCsr->aPage);
9276         sqlite3_free(pCsr->pRec);
9277         pCsr->aPage = 0;
9278         pCsr->pRec = 0;
9279         if( pCsr->bOnePage ) return SQLITE_OK;
9280         pCsr->iPgno++;
9281       }else{
9282         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
9283           return SQLITE_OK;
9284         }
9285 
9286         /* Advance to the next cell. The next iteration of the loop will load
9287         ** the record and so on. */
9288         sqlite3_free(pCsr->pRec);
9289         pCsr->pRec = 0;
9290         pCsr->iCell++;
9291       }
9292     }
9293   }
9294 
9295   assert( !"can't get here" );
9296   return SQLITE_OK;
9297 }
9298 
9299 /*
9300 ** Return true if the cursor is at EOF.
9301 */
9302 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
9303   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9304   return pCsr->aPage==0;
9305 }
9306 
9307 /*
9308 ** Determine the size in pages of database zSchema (where zSchema is
9309 ** "main", "temp" or the name of an attached database) and set
9310 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
9311 ** an SQLite error code.
9312 */
9313 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
9314   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
9315   char *zSql = 0;
9316   int rc, rc2;
9317   sqlite3_stmt *pStmt = 0;
9318 
9319   zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
9320   if( zSql==0 ) return SQLITE_NOMEM;
9321   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
9322   sqlite3_free(zSql);
9323   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9324     pCsr->szDb = sqlite3_column_int(pStmt, 0);
9325   }
9326   rc2 = sqlite3_finalize(pStmt);
9327   if( rc==SQLITE_OK ) rc = rc2;
9328   return rc;
9329 }
9330 
9331 /*
9332 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
9333 */
9334 static int dbdataFilter(
9335   sqlite3_vtab_cursor *pCursor,
9336   int idxNum, const char *idxStr,
9337   int argc, sqlite3_value **argv
9338 ){
9339   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9340   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9341   int rc = SQLITE_OK;
9342   const char *zSchema = "main";
9343 
9344   dbdataResetCursor(pCsr);
9345   assert( pCsr->iPgno==1 );
9346   if( idxNum & 0x01 ){
9347     zSchema = (const char*)sqlite3_value_text(argv[0]);
9348   }
9349   if( idxNum & 0x02 ){
9350     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
9351     pCsr->bOnePage = 1;
9352   }else{
9353     pCsr->nPage = dbdataDbsize(pCsr, zSchema);
9354     rc = dbdataDbsize(pCsr, zSchema);
9355   }
9356 
9357   if( rc==SQLITE_OK ){
9358     if( pTab->pStmt ){
9359       pCsr->pStmt = pTab->pStmt;
9360       pTab->pStmt = 0;
9361     }else{
9362       rc = sqlite3_prepare_v2(pTab->db,
9363           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
9364           &pCsr->pStmt, 0
9365       );
9366     }
9367   }
9368   if( rc==SQLITE_OK ){
9369     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
9370   }else{
9371     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
9372   }
9373   if( rc==SQLITE_OK ){
9374     rc = dbdataNext(pCursor);
9375   }
9376   return rc;
9377 }
9378 
9379 /*
9380 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
9381 */
9382 static int dbdataColumn(
9383   sqlite3_vtab_cursor *pCursor,
9384   sqlite3_context *ctx,
9385   int i
9386 ){
9387   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9388   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9389   if( pTab->bPtr ){
9390     switch( i ){
9391       case DBPTR_COLUMN_PGNO:
9392         sqlite3_result_int64(ctx, pCsr->iPgno);
9393         break;
9394       case DBPTR_COLUMN_CHILD: {
9395         int iOff = pCsr->iPgno==1 ? 100 : 0;
9396         if( pCsr->iCell<0 ){
9397           iOff += 8;
9398         }else{
9399           iOff += 12 + pCsr->iCell*2;
9400           if( iOff>pCsr->nPage ) return SQLITE_OK;
9401           iOff = get_uint16(&pCsr->aPage[iOff]);
9402         }
9403         if( iOff<=pCsr->nPage ){
9404           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
9405         }
9406         break;
9407       }
9408     }
9409   }else{
9410     switch( i ){
9411       case DBDATA_COLUMN_PGNO:
9412         sqlite3_result_int64(ctx, pCsr->iPgno);
9413         break;
9414       case DBDATA_COLUMN_CELL:
9415         sqlite3_result_int(ctx, pCsr->iCell);
9416         break;
9417       case DBDATA_COLUMN_FIELD:
9418         sqlite3_result_int(ctx, pCsr->iField);
9419         break;
9420       case DBDATA_COLUMN_VALUE: {
9421         if( pCsr->iField<0 ){
9422           sqlite3_result_int64(ctx, pCsr->iIntkey);
9423         }else{
9424           sqlite3_int64 iType;
9425           dbdataGetVarint(pCsr->pHdrPtr, &iType);
9426           dbdataValue(
9427               ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
9428           );
9429         }
9430         break;
9431       }
9432     }
9433   }
9434   return SQLITE_OK;
9435 }
9436 
9437 /*
9438 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
9439 */
9440 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
9441   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9442   *pRowid = pCsr->iRowid;
9443   return SQLITE_OK;
9444 }
9445 
9446 
9447 /*
9448 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
9449 */
9450 static int sqlite3DbdataRegister(sqlite3 *db){
9451   static sqlite3_module dbdata_module = {
9452     0,                            /* iVersion */
9453     0,                            /* xCreate */
9454     dbdataConnect,                /* xConnect */
9455     dbdataBestIndex,              /* xBestIndex */
9456     dbdataDisconnect,             /* xDisconnect */
9457     0,                            /* xDestroy */
9458     dbdataOpen,                   /* xOpen - open a cursor */
9459     dbdataClose,                  /* xClose - close a cursor */
9460     dbdataFilter,                 /* xFilter - configure scan constraints */
9461     dbdataNext,                   /* xNext - advance a cursor */
9462     dbdataEof,                    /* xEof - check for end of scan */
9463     dbdataColumn,                 /* xColumn - read data */
9464     dbdataRowid,                  /* xRowid - read data */
9465     0,                            /* xUpdate */
9466     0,                            /* xBegin */
9467     0,                            /* xSync */
9468     0,                            /* xCommit */
9469     0,                            /* xRollback */
9470     0,                            /* xFindMethod */
9471     0,                            /* xRename */
9472     0,                            /* xSavepoint */
9473     0,                            /* xRelease */
9474     0,                            /* xRollbackTo */
9475     0                             /* xShadowName */
9476   };
9477 
9478   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
9479   if( rc==SQLITE_OK ){
9480     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
9481   }
9482   return rc;
9483 }
9484 
9485 #ifdef _WIN32
9486 
9487 #endif
9488 int sqlite3_dbdata_init(
9489   sqlite3 *db,
9490   char **pzErrMsg,
9491   const sqlite3_api_routines *pApi
9492 ){
9493   SQLITE_EXTENSION_INIT2(pApi);
9494   return sqlite3DbdataRegister(db);
9495 }
9496 
9497 /************************* End ../ext/misc/dbdata.c ********************/
9498 #endif
9499 
9500 #if defined(SQLITE_ENABLE_SESSION)
9501 /*
9502 ** State information for a single open session
9503 */
9504 typedef struct OpenSession OpenSession;
9505 struct OpenSession {
9506   char *zName;             /* Symbolic name for this session */
9507   int nFilter;             /* Number of xFilter rejection GLOB patterns */
9508   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
9509   sqlite3_session *p;      /* The open session */
9510 };
9511 #endif
9512 
9513 /*
9514 ** Shell output mode information from before ".explain on",
9515 ** saved so that it can be restored by ".explain off"
9516 */
9517 typedef struct SavedModeInfo SavedModeInfo;
9518 struct SavedModeInfo {
9519   int valid;          /* Is there legit data in here? */
9520   int mode;           /* Mode prior to ".explain on" */
9521   int showHeader;     /* The ".header" setting prior to ".explain on" */
9522   int colWidth[100];  /* Column widths prior to ".explain on" */
9523 };
9524 
9525 typedef struct ExpertInfo ExpertInfo;
9526 struct ExpertInfo {
9527   sqlite3expert *pExpert;
9528   int bVerbose;
9529 };
9530 
9531 /* A single line in the EQP output */
9532 typedef struct EQPGraphRow EQPGraphRow;
9533 struct EQPGraphRow {
9534   int iEqpId;           /* ID for this row */
9535   int iParentId;        /* ID of the parent row */
9536   EQPGraphRow *pNext;   /* Next row in sequence */
9537   char zText[1];        /* Text to display for this row */
9538 };
9539 
9540 /* All EQP output is collected into an instance of the following */
9541 typedef struct EQPGraph EQPGraph;
9542 struct EQPGraph {
9543   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
9544   EQPGraphRow *pLast;   /* Last element of the pRow list */
9545   char zPrefix[100];    /* Graph prefix */
9546 };
9547 
9548 /*
9549 ** State information about the database connection is contained in an
9550 ** instance of the following structure.
9551 */
9552 typedef struct ShellState ShellState;
9553 struct ShellState {
9554   sqlite3 *db;           /* The database */
9555   u8 autoExplain;        /* Automatically turn on .explain mode */
9556   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
9557   u8 autoEQPtest;        /* autoEQP is in test mode */
9558   u8 autoEQPtrace;       /* autoEQP is in trace mode */
9559   u8 statsOn;            /* True to display memory stats before each finalize */
9560   u8 scanstatsOn;        /* True to display scan stats before each finalize */
9561   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
9562   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
9563   u8 nEqpLevel;          /* Depth of the EQP output graph */
9564   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
9565   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
9566   int outCount;          /* Revert to stdout when reaching zero */
9567   int cnt;               /* Number of records displayed so far */
9568   int lineno;            /* Line number of last line read from in */
9569   FILE *in;              /* Read commands from this stream */
9570   FILE *out;             /* Write results here */
9571   FILE *traceOut;        /* Output for sqlite3_trace() */
9572   int nErr;              /* Number of errors seen */
9573   int mode;              /* An output mode setting */
9574   int modePrior;         /* Saved mode */
9575   int cMode;             /* temporary output mode for the current query */
9576   int normalMode;        /* Output mode before ".explain on" */
9577   int writableSchema;    /* True if PRAGMA writable_schema=ON */
9578   int showHeader;        /* True to show column names in List or Column mode */
9579   int nCheck;            /* Number of ".check" commands run */
9580   unsigned nProgress;    /* Number of progress callbacks encountered */
9581   unsigned mxProgress;   /* Maximum progress callbacks before failing */
9582   unsigned flgProgress;  /* Flags for the progress callback */
9583   unsigned shellFlgs;    /* Various flags */
9584   sqlite3_int64 szMax;   /* --maxsize argument to .open */
9585   char *zDestTable;      /* Name of destination table when MODE_Insert */
9586   char *zTempFile;       /* Temporary file that might need deleting */
9587   char zTestcase[30];    /* Name of current test case */
9588   char colSeparator[20]; /* Column separator character for several modes */
9589   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
9590   char colSepPrior[20];  /* Saved column separator */
9591   char rowSepPrior[20];  /* Saved row separator */
9592   int colWidth[100];     /* Requested width of each column when in column mode*/
9593   int actualWidth[100];  /* Actual width of each column */
9594   char nullValue[20];    /* The text to print when a NULL comes back from
9595                          ** the database */
9596   char outfile[FILENAME_MAX]; /* Filename for *out */
9597   const char *zDbFilename;    /* name of the database file */
9598   char *zFreeOnClose;         /* Filename to free when closing */
9599   const char *zVfs;           /* Name of VFS to use */
9600   sqlite3_stmt *pStmt;   /* Current statement if any. */
9601   FILE *pLog;            /* Write log output here */
9602   int *aiIndent;         /* Array of indents used in MODE_Explain */
9603   int nIndent;           /* Size of array aiIndent[] */
9604   int iIndent;           /* Index of current op in aiIndent[] */
9605   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
9606 #if defined(SQLITE_ENABLE_SESSION)
9607   int nSession;             /* Number of active sessions */
9608   OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
9609 #endif
9610   ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
9611 };
9612 
9613 
9614 /* Allowed values for ShellState.autoEQP
9615 */
9616 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
9617 #define AUTOEQP_on       1           /* Automatic EQP is on */
9618 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
9619 #define AUTOEQP_full     3           /* Show full EXPLAIN */
9620 
9621 /* Allowed values for ShellState.openMode
9622 */
9623 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
9624 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
9625 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
9626 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
9627 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
9628 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
9629 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
9630 
9631 /* Allowed values for ShellState.eTraceType
9632 */
9633 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
9634 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
9635 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
9636 
9637 /* Bits in the ShellState.flgProgress variable */
9638 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
9639 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
9640                                    ** callback limit is reached, and for each
9641                                    ** top-level SQL statement */
9642 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
9643 
9644 /*
9645 ** These are the allowed shellFlgs values
9646 */
9647 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
9648 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
9649 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
9650 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
9651 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
9652 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
9653 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
9654 
9655 /*
9656 ** Macros for testing and setting shellFlgs
9657 */
9658 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
9659 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
9660 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
9661 
9662 /*
9663 ** These are the allowed modes.
9664 */
9665 #define MODE_Line     0  /* One column per line.  Blank line between records */
9666 #define MODE_Column   1  /* One record per line in neat columns */
9667 #define MODE_List     2  /* One record per line with a separator */
9668 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
9669 #define MODE_Html     4  /* Generate an XHTML table */
9670 #define MODE_Insert   5  /* Generate SQL "insert" statements */
9671 #define MODE_Quote    6  /* Quote values as for SQL */
9672 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
9673 #define MODE_Csv      8  /* Quote strings, numbers are plain */
9674 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
9675 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
9676 #define MODE_Pretty  11  /* Pretty-print schemas */
9677 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
9678 
9679 static const char *modeDescr[] = {
9680   "line",
9681   "column",
9682   "list",
9683   "semi",
9684   "html",
9685   "insert",
9686   "quote",
9687   "tcl",
9688   "csv",
9689   "explain",
9690   "ascii",
9691   "prettyprint",
9692   "eqp"
9693 };
9694 
9695 /*
9696 ** These are the column/row/line separators used by the various
9697 ** import/export modes.
9698 */
9699 #define SEP_Column    "|"
9700 #define SEP_Row       "\n"
9701 #define SEP_Tab       "\t"
9702 #define SEP_Space     " "
9703 #define SEP_Comma     ","
9704 #define SEP_CrLf      "\r\n"
9705 #define SEP_Unit      "\x1F"
9706 #define SEP_Record    "\x1E"
9707 
9708 /*
9709 ** A callback for the sqlite3_log() interface.
9710 */
9711 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
9712   ShellState *p = (ShellState*)pArg;
9713   if( p->pLog==0 ) return;
9714   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
9715   fflush(p->pLog);
9716 }
9717 
9718 /*
9719 ** SQL function:  shell_putsnl(X)
9720 **
9721 ** Write the text X to the screen (or whatever output is being directed)
9722 ** adding a newline at the end, and then return X.
9723 */
9724 static void shellPutsFunc(
9725   sqlite3_context *pCtx,
9726   int nVal,
9727   sqlite3_value **apVal
9728 ){
9729   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
9730   (void)nVal;
9731   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
9732   sqlite3_result_value(pCtx, apVal[0]);
9733 }
9734 
9735 /*
9736 ** SQL function:   edit(VALUE)
9737 **                 edit(VALUE,EDITOR)
9738 **
9739 ** These steps:
9740 **
9741 **     (1) Write VALUE into a temporary file.
9742 **     (2) Run program EDITOR on that temporary file.
9743 **     (3) Read the temporary file back and return its content as the result.
9744 **     (4) Delete the temporary file
9745 **
9746 ** If the EDITOR argument is omitted, use the value in the VISUAL
9747 ** environment variable.  If still there is no EDITOR, through an error.
9748 **
9749 ** Also throw an error if the EDITOR program returns a non-zero exit code.
9750 */
9751 #ifndef SQLITE_NOHAVE_SYSTEM
9752 static void editFunc(
9753   sqlite3_context *context,
9754   int argc,
9755   sqlite3_value **argv
9756 ){
9757   const char *zEditor;
9758   char *zTempFile = 0;
9759   sqlite3 *db;
9760   char *zCmd = 0;
9761   int bBin;
9762   int rc;
9763   int hasCRNL = 0;
9764   FILE *f = 0;
9765   sqlite3_int64 sz;
9766   sqlite3_int64 x;
9767   unsigned char *p = 0;
9768 
9769   if( argc==2 ){
9770     zEditor = (const char*)sqlite3_value_text(argv[1]);
9771   }else{
9772     zEditor = getenv("VISUAL");
9773   }
9774   if( zEditor==0 ){
9775     sqlite3_result_error(context, "no editor for edit()", -1);
9776     return;
9777   }
9778   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
9779     sqlite3_result_error(context, "NULL input to edit()", -1);
9780     return;
9781   }
9782   db = sqlite3_context_db_handle(context);
9783   zTempFile = 0;
9784   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
9785   if( zTempFile==0 ){
9786     sqlite3_uint64 r = 0;
9787     sqlite3_randomness(sizeof(r), &r);
9788     zTempFile = sqlite3_mprintf("temp%llx", r);
9789     if( zTempFile==0 ){
9790       sqlite3_result_error_nomem(context);
9791       return;
9792     }
9793   }
9794   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
9795   /* When writing the file to be edited, do \n to \r\n conversions on systems
9796   ** that want \r\n line endings */
9797   f = fopen(zTempFile, bBin ? "wb" : "w");
9798   if( f==0 ){
9799     sqlite3_result_error(context, "edit() cannot open temp file", -1);
9800     goto edit_func_end;
9801   }
9802   sz = sqlite3_value_bytes(argv[0]);
9803   if( bBin ){
9804     x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
9805   }else{
9806     const char *z = (const char*)sqlite3_value_text(argv[0]);
9807     /* Remember whether or not the value originally contained \r\n */
9808     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
9809     x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
9810   }
9811   fclose(f);
9812   f = 0;
9813   if( x!=sz ){
9814     sqlite3_result_error(context, "edit() could not write the whole file", -1);
9815     goto edit_func_end;
9816   }
9817   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
9818   if( zCmd==0 ){
9819     sqlite3_result_error_nomem(context);
9820     goto edit_func_end;
9821   }
9822   rc = system(zCmd);
9823   sqlite3_free(zCmd);
9824   if( rc ){
9825     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
9826     goto edit_func_end;
9827   }
9828   f = fopen(zTempFile, "rb");
9829   if( f==0 ){
9830     sqlite3_result_error(context,
9831       "edit() cannot reopen temp file after edit", -1);
9832     goto edit_func_end;
9833   }
9834   fseek(f, 0, SEEK_END);
9835   sz = ftell(f);
9836   rewind(f);
9837   p = sqlite3_malloc64( sz+1 );
9838   if( p==0 ){
9839     sqlite3_result_error_nomem(context);
9840     goto edit_func_end;
9841   }
9842   x = fread(p, 1, (size_t)sz, f);
9843   fclose(f);
9844   f = 0;
9845   if( x!=sz ){
9846     sqlite3_result_error(context, "could not read back the whole file", -1);
9847     goto edit_func_end;
9848   }
9849   if( bBin ){
9850     sqlite3_result_blob64(context, p, sz, sqlite3_free);
9851   }else{
9852     sqlite3_int64 i, j;
9853     if( hasCRNL ){
9854       /* If the original contains \r\n then do no conversions back to \n */
9855       j = sz;
9856     }else{
9857       /* If the file did not originally contain \r\n then convert any new
9858       ** \r\n back into \n */
9859       for(i=j=0; i<sz; i++){
9860         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
9861         p[j++] = p[i];
9862       }
9863       sz = j;
9864       p[sz] = 0;
9865     }
9866     sqlite3_result_text64(context, (const char*)p, sz,
9867                           sqlite3_free, SQLITE_UTF8);
9868   }
9869   p = 0;
9870 
9871 edit_func_end:
9872   if( f ) fclose(f);
9873   unlink(zTempFile);
9874   sqlite3_free(zTempFile);
9875   sqlite3_free(p);
9876 }
9877 #endif /* SQLITE_NOHAVE_SYSTEM */
9878 
9879 /*
9880 ** Save or restore the current output mode
9881 */
9882 static void outputModePush(ShellState *p){
9883   p->modePrior = p->mode;
9884   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
9885   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
9886 }
9887 static void outputModePop(ShellState *p){
9888   p->mode = p->modePrior;
9889   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
9890   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
9891 }
9892 
9893 /*
9894 ** Output the given string as a hex-encoded blob (eg. X'1234' )
9895 */
9896 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
9897   int i;
9898   char *zBlob = (char *)pBlob;
9899   raw_printf(out,"X'");
9900   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
9901   raw_printf(out,"'");
9902 }
9903 
9904 /*
9905 ** Find a string that is not found anywhere in z[].  Return a pointer
9906 ** to that string.
9907 **
9908 ** Try to use zA and zB first.  If both of those are already found in z[]
9909 ** then make up some string and store it in the buffer zBuf.
9910 */
9911 static const char *unused_string(
9912   const char *z,                    /* Result must not appear anywhere in z */
9913   const char *zA, const char *zB,   /* Try these first */
9914   char *zBuf                        /* Space to store a generated string */
9915 ){
9916   unsigned i = 0;
9917   if( strstr(z, zA)==0 ) return zA;
9918   if( strstr(z, zB)==0 ) return zB;
9919   do{
9920     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
9921   }while( strstr(z,zBuf)!=0 );
9922   return zBuf;
9923 }
9924 
9925 /*
9926 ** Output the given string as a quoted string using SQL quoting conventions.
9927 **
9928 ** See also: output_quoted_escaped_string()
9929 */
9930 static void output_quoted_string(FILE *out, const char *z){
9931   int i;
9932   char c;
9933   setBinaryMode(out, 1);
9934   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9935   if( c==0 ){
9936     utf8_printf(out,"'%s'",z);
9937   }else{
9938     raw_printf(out, "'");
9939     while( *z ){
9940       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9941       if( c=='\'' ) i++;
9942       if( i ){
9943         utf8_printf(out, "%.*s", i, z);
9944         z += i;
9945       }
9946       if( c=='\'' ){
9947         raw_printf(out, "'");
9948         continue;
9949       }
9950       if( c==0 ){
9951         break;
9952       }
9953       z++;
9954     }
9955     raw_printf(out, "'");
9956   }
9957   setTextMode(out, 1);
9958 }
9959 
9960 /*
9961 ** Output the given string as a quoted string using SQL quoting conventions.
9962 ** Additionallly , escape the "\n" and "\r" characters so that they do not
9963 ** get corrupted by end-of-line translation facilities in some operating
9964 ** systems.
9965 **
9966 ** This is like output_quoted_string() but with the addition of the \r\n
9967 ** escape mechanism.
9968 */
9969 static void output_quoted_escaped_string(FILE *out, const char *z){
9970   int i;
9971   char c;
9972   setBinaryMode(out, 1);
9973   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
9974   if( c==0 ){
9975     utf8_printf(out,"'%s'",z);
9976   }else{
9977     const char *zNL = 0;
9978     const char *zCR = 0;
9979     int nNL = 0;
9980     int nCR = 0;
9981     char zBuf1[20], zBuf2[20];
9982     for(i=0; z[i]; i++){
9983       if( z[i]=='\n' ) nNL++;
9984       if( z[i]=='\r' ) nCR++;
9985     }
9986     if( nNL ){
9987       raw_printf(out, "replace(");
9988       zNL = unused_string(z, "\\n", "\\012", zBuf1);
9989     }
9990     if( nCR ){
9991       raw_printf(out, "replace(");
9992       zCR = unused_string(z, "\\r", "\\015", zBuf2);
9993     }
9994     raw_printf(out, "'");
9995     while( *z ){
9996       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
9997       if( c=='\'' ) i++;
9998       if( i ){
9999         utf8_printf(out, "%.*s", i, z);
10000         z += i;
10001       }
10002       if( c=='\'' ){
10003         raw_printf(out, "'");
10004         continue;
10005       }
10006       if( c==0 ){
10007         break;
10008       }
10009       z++;
10010       if( c=='\n' ){
10011         raw_printf(out, "%s", zNL);
10012         continue;
10013       }
10014       raw_printf(out, "%s", zCR);
10015     }
10016     raw_printf(out, "'");
10017     if( nCR ){
10018       raw_printf(out, ",'%s',char(13))", zCR);
10019     }
10020     if( nNL ){
10021       raw_printf(out, ",'%s',char(10))", zNL);
10022     }
10023   }
10024   setTextMode(out, 1);
10025 }
10026 
10027 /*
10028 ** Output the given string as a quoted according to C or TCL quoting rules.
10029 */
10030 static void output_c_string(FILE *out, const char *z){
10031   unsigned int c;
10032   fputc('"', out);
10033   while( (c = *(z++))!=0 ){
10034     if( c=='\\' ){
10035       fputc(c, out);
10036       fputc(c, out);
10037     }else if( c=='"' ){
10038       fputc('\\', out);
10039       fputc('"', out);
10040     }else if( c=='\t' ){
10041       fputc('\\', out);
10042       fputc('t', out);
10043     }else if( c=='\n' ){
10044       fputc('\\', out);
10045       fputc('n', out);
10046     }else if( c=='\r' ){
10047       fputc('\\', out);
10048       fputc('r', out);
10049     }else if( !isprint(c&0xff) ){
10050       raw_printf(out, "\\%03o", c&0xff);
10051     }else{
10052       fputc(c, out);
10053     }
10054   }
10055   fputc('"', out);
10056 }
10057 
10058 /*
10059 ** Output the given string with characters that are special to
10060 ** HTML escaped.
10061 */
10062 static void output_html_string(FILE *out, const char *z){
10063   int i;
10064   if( z==0 ) z = "";
10065   while( *z ){
10066     for(i=0;   z[i]
10067             && z[i]!='<'
10068             && z[i]!='&'
10069             && z[i]!='>'
10070             && z[i]!='\"'
10071             && z[i]!='\'';
10072         i++){}
10073     if( i>0 ){
10074       utf8_printf(out,"%.*s",i,z);
10075     }
10076     if( z[i]=='<' ){
10077       raw_printf(out,"&lt;");
10078     }else if( z[i]=='&' ){
10079       raw_printf(out,"&amp;");
10080     }else if( z[i]=='>' ){
10081       raw_printf(out,"&gt;");
10082     }else if( z[i]=='\"' ){
10083       raw_printf(out,"&quot;");
10084     }else if( z[i]=='\'' ){
10085       raw_printf(out,"&#39;");
10086     }else{
10087       break;
10088     }
10089     z += i + 1;
10090   }
10091 }
10092 
10093 /*
10094 ** If a field contains any character identified by a 1 in the following
10095 ** array, then the string must be quoted for CSV.
10096 */
10097 static const char needCsvQuote[] = {
10098   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10099   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10100   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
10101   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10102   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10103   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10104   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10105   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
10106   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10107   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10108   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10109   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10110   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10111   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10112   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10113   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10114 };
10115 
10116 /*
10117 ** Output a single term of CSV.  Actually, p->colSeparator is used for
10118 ** the separator, which may or may not be a comma.  p->nullValue is
10119 ** the null value.  Strings are quoted if necessary.  The separator
10120 ** is only issued if bSep is true.
10121 */
10122 static void output_csv(ShellState *p, const char *z, int bSep){
10123   FILE *out = p->out;
10124   if( z==0 ){
10125     utf8_printf(out,"%s",p->nullValue);
10126   }else{
10127     int i;
10128     int nSep = strlen30(p->colSeparator);
10129     for(i=0; z[i]; i++){
10130       if( needCsvQuote[((unsigned char*)z)[i]]
10131          || (z[i]==p->colSeparator[0] &&
10132              (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
10133         i = 0;
10134         break;
10135       }
10136     }
10137     if( i==0 ){
10138       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
10139       utf8_printf(out, "%s", zQuoted);
10140       sqlite3_free(zQuoted);
10141     }else{
10142       utf8_printf(out, "%s", z);
10143     }
10144   }
10145   if( bSep ){
10146     utf8_printf(p->out, "%s", p->colSeparator);
10147   }
10148 }
10149 
10150 /*
10151 ** This routine runs when the user presses Ctrl-C
10152 */
10153 static void interrupt_handler(int NotUsed){
10154   UNUSED_PARAMETER(NotUsed);
10155   seenInterrupt++;
10156   if( seenInterrupt>2 ) exit(1);
10157   if( globalDb ) sqlite3_interrupt(globalDb);
10158 }
10159 
10160 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10161 /*
10162 ** This routine runs for console events (e.g. Ctrl-C) on Win32
10163 */
10164 static BOOL WINAPI ConsoleCtrlHandler(
10165   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
10166 ){
10167   if( dwCtrlType==CTRL_C_EVENT ){
10168     interrupt_handler(0);
10169     return TRUE;
10170   }
10171   return FALSE;
10172 }
10173 #endif
10174 
10175 #ifndef SQLITE_OMIT_AUTHORIZATION
10176 /*
10177 ** When the ".auth ON" is set, the following authorizer callback is
10178 ** invoked.  It always returns SQLITE_OK.
10179 */
10180 static int shellAuth(
10181   void *pClientData,
10182   int op,
10183   const char *zA1,
10184   const char *zA2,
10185   const char *zA3,
10186   const char *zA4
10187 ){
10188   ShellState *p = (ShellState*)pClientData;
10189   static const char *azAction[] = { 0,
10190      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
10191      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
10192      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
10193      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
10194      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
10195      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
10196      "PRAGMA",               "READ",                 "SELECT",
10197      "TRANSACTION",          "UPDATE",               "ATTACH",
10198      "DETACH",               "ALTER_TABLE",          "REINDEX",
10199      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
10200      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
10201   };
10202   int i;
10203   const char *az[4];
10204   az[0] = zA1;
10205   az[1] = zA2;
10206   az[2] = zA3;
10207   az[3] = zA4;
10208   utf8_printf(p->out, "authorizer: %s", azAction[op]);
10209   for(i=0; i<4; i++){
10210     raw_printf(p->out, " ");
10211     if( az[i] ){
10212       output_c_string(p->out, az[i]);
10213     }else{
10214       raw_printf(p->out, "NULL");
10215     }
10216   }
10217   raw_printf(p->out, "\n");
10218   return SQLITE_OK;
10219 }
10220 #endif
10221 
10222 /*
10223 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
10224 **
10225 ** This routine converts some CREATE TABLE statements for shadow tables
10226 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
10227 */
10228 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
10229   if( z==0 ) return;
10230   if( zTail==0 ) return;
10231   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
10232     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
10233   }else{
10234     utf8_printf(out, "%s%s", z, zTail);
10235   }
10236 }
10237 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
10238   char c = z[n];
10239   z[n] = 0;
10240   printSchemaLine(out, z, zTail);
10241   z[n] = c;
10242 }
10243 
10244 /*
10245 ** Return true if string z[] has nothing but whitespace and comments to the
10246 ** end of the first line.
10247 */
10248 static int wsToEol(const char *z){
10249   int i;
10250   for(i=0; z[i]; i++){
10251     if( z[i]=='\n' ) return 1;
10252     if( IsSpace(z[i]) ) continue;
10253     if( z[i]=='-' && z[i+1]=='-' ) return 1;
10254     return 0;
10255   }
10256   return 1;
10257 }
10258 
10259 /*
10260 ** Add a new entry to the EXPLAIN QUERY PLAN data
10261 */
10262 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
10263   EQPGraphRow *pNew;
10264   int nText = strlen30(zText);
10265   if( p->autoEQPtest ){
10266     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
10267   }
10268   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
10269   if( pNew==0 ) shell_out_of_memory();
10270   pNew->iEqpId = iEqpId;
10271   pNew->iParentId = p2;
10272   memcpy(pNew->zText, zText, nText+1);
10273   pNew->pNext = 0;
10274   if( p->sGraph.pLast ){
10275     p->sGraph.pLast->pNext = pNew;
10276   }else{
10277     p->sGraph.pRow = pNew;
10278   }
10279   p->sGraph.pLast = pNew;
10280 }
10281 
10282 /*
10283 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
10284 ** in p->sGraph.
10285 */
10286 static void eqp_reset(ShellState *p){
10287   EQPGraphRow *pRow, *pNext;
10288   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
10289     pNext = pRow->pNext;
10290     sqlite3_free(pRow);
10291   }
10292   memset(&p->sGraph, 0, sizeof(p->sGraph));
10293 }
10294 
10295 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
10296 ** pOld, or return the first such line if pOld is NULL
10297 */
10298 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
10299   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
10300   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
10301   return pRow;
10302 }
10303 
10304 /* Render a single level of the graph that has iEqpId as its parent.  Called
10305 ** recursively to render sublevels.
10306 */
10307 static void eqp_render_level(ShellState *p, int iEqpId){
10308   EQPGraphRow *pRow, *pNext;
10309   int n = strlen30(p->sGraph.zPrefix);
10310   char *z;
10311   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
10312     pNext = eqp_next_row(p, iEqpId, pRow);
10313     z = pRow->zText;
10314     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
10315                 pNext ? "|--" : "`--", z);
10316     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
10317       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
10318       eqp_render_level(p, pRow->iEqpId);
10319       p->sGraph.zPrefix[n] = 0;
10320     }
10321   }
10322 }
10323 
10324 /*
10325 ** Display and reset the EXPLAIN QUERY PLAN data
10326 */
10327 static void eqp_render(ShellState *p){
10328   EQPGraphRow *pRow = p->sGraph.pRow;
10329   if( pRow ){
10330     if( pRow->zText[0]=='-' ){
10331       if( pRow->pNext==0 ){
10332         eqp_reset(p);
10333         return;
10334       }
10335       utf8_printf(p->out, "%s\n", pRow->zText+3);
10336       p->sGraph.pRow = pRow->pNext;
10337       sqlite3_free(pRow);
10338     }else{
10339       utf8_printf(p->out, "QUERY PLAN\n");
10340     }
10341     p->sGraph.zPrefix[0] = 0;
10342     eqp_render_level(p, 0);
10343     eqp_reset(p);
10344   }
10345 }
10346 
10347 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10348 /*
10349 ** Progress handler callback.
10350 */
10351 static int progress_handler(void *pClientData) {
10352   ShellState *p = (ShellState*)pClientData;
10353   p->nProgress++;
10354   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
10355     raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
10356     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10357     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
10358     return 1;
10359   }
10360   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
10361     raw_printf(p->out, "Progress %u\n", p->nProgress);
10362   }
10363   return 0;
10364 }
10365 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10366 
10367 /*
10368 ** This is the callback routine that the shell
10369 ** invokes for each row of a query result.
10370 */
10371 static int shell_callback(
10372   void *pArg,
10373   int nArg,        /* Number of result columns */
10374   char **azArg,    /* Text of each result column */
10375   char **azCol,    /* Column names */
10376   int *aiType      /* Column types */
10377 ){
10378   int i;
10379   ShellState *p = (ShellState*)pArg;
10380 
10381   if( azArg==0 ) return 0;
10382   switch( p->cMode ){
10383     case MODE_Line: {
10384       int w = 5;
10385       if( azArg==0 ) break;
10386       for(i=0; i<nArg; i++){
10387         int len = strlen30(azCol[i] ? azCol[i] : "");
10388         if( len>w ) w = len;
10389       }
10390       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
10391       for(i=0; i<nArg; i++){
10392         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
10393                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
10394       }
10395       break;
10396     }
10397     case MODE_Explain:
10398     case MODE_Column: {
10399       static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
10400       const int *colWidth;
10401       int showHdr;
10402       char *rowSep;
10403       if( p->cMode==MODE_Column ){
10404         colWidth = p->colWidth;
10405         showHdr = p->showHeader;
10406         rowSep = p->rowSeparator;
10407       }else{
10408         colWidth = aExplainWidths;
10409         showHdr = 1;
10410         rowSep = SEP_Row;
10411       }
10412       if( p->cnt++==0 ){
10413         for(i=0; i<nArg; i++){
10414           int w, n;
10415           if( i<ArraySize(p->colWidth) ){
10416             w = colWidth[i];
10417           }else{
10418             w = 0;
10419           }
10420           if( w==0 ){
10421             w = strlenChar(azCol[i] ? azCol[i] : "");
10422             if( w<10 ) w = 10;
10423             n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
10424             if( w<n ) w = n;
10425           }
10426           if( i<ArraySize(p->actualWidth) ){
10427             p->actualWidth[i] = w;
10428           }
10429           if( showHdr ){
10430             utf8_width_print(p->out, w, azCol[i]);
10431             utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
10432           }
10433         }
10434         if( showHdr ){
10435           for(i=0; i<nArg; i++){
10436             int w;
10437             if( i<ArraySize(p->actualWidth) ){
10438                w = p->actualWidth[i];
10439                if( w<0 ) w = -w;
10440             }else{
10441                w = 10;
10442             }
10443             utf8_printf(p->out,"%-*.*s%s",w,w,
10444                    "----------------------------------------------------------"
10445                    "----------------------------------------------------------",
10446                     i==nArg-1 ? rowSep : "  ");
10447           }
10448         }
10449       }
10450       if( azArg==0 ) break;
10451       for(i=0; i<nArg; i++){
10452         int w;
10453         if( i<ArraySize(p->actualWidth) ){
10454            w = p->actualWidth[i];
10455         }else{
10456            w = 10;
10457         }
10458         if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
10459           w = strlenChar(azArg[i]);
10460         }
10461         if( i==1 && p->aiIndent && p->pStmt ){
10462           if( p->iIndent<p->nIndent ){
10463             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
10464           }
10465           p->iIndent++;
10466         }
10467         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
10468         utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
10469       }
10470       break;
10471     }
10472     case MODE_Semi: {   /* .schema and .fullschema output */
10473       printSchemaLine(p->out, azArg[0], ";\n");
10474       break;
10475     }
10476     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
10477       char *z;
10478       int j;
10479       int nParen = 0;
10480       char cEnd = 0;
10481       char c;
10482       int nLine = 0;
10483       assert( nArg==1 );
10484       if( azArg[0]==0 ) break;
10485       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
10486        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
10487       ){
10488         utf8_printf(p->out, "%s;\n", azArg[0]);
10489         break;
10490       }
10491       z = sqlite3_mprintf("%s", azArg[0]);
10492       j = 0;
10493       for(i=0; IsSpace(z[i]); i++){}
10494       for(; (c = z[i])!=0; i++){
10495         if( IsSpace(c) ){
10496           if( z[j-1]=='\r' ) z[j-1] = '\n';
10497           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
10498         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
10499           j--;
10500         }
10501         z[j++] = c;
10502       }
10503       while( j>0 && IsSpace(z[j-1]) ){ j--; }
10504       z[j] = 0;
10505       if( strlen30(z)>=79 ){
10506         for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
10507           if( c==cEnd ){
10508             cEnd = 0;
10509           }else if( c=='"' || c=='\'' || c=='`' ){
10510             cEnd = c;
10511           }else if( c=='[' ){
10512             cEnd = ']';
10513           }else if( c=='-' && z[i+1]=='-' ){
10514             cEnd = '\n';
10515           }else if( c=='(' ){
10516             nParen++;
10517           }else if( c==')' ){
10518             nParen--;
10519             if( nLine>0 && nParen==0 && j>0 ){
10520               printSchemaLineN(p->out, z, j, "\n");
10521               j = 0;
10522             }
10523           }
10524           z[j++] = c;
10525           if( nParen==1 && cEnd==0
10526            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
10527           ){
10528             if( c=='\n' ) j--;
10529             printSchemaLineN(p->out, z, j, "\n  ");
10530             j = 0;
10531             nLine++;
10532             while( IsSpace(z[i+1]) ){ i++; }
10533           }
10534         }
10535         z[j] = 0;
10536       }
10537       printSchemaLine(p->out, z, ";\n");
10538       sqlite3_free(z);
10539       break;
10540     }
10541     case MODE_List: {
10542       if( p->cnt++==0 && p->showHeader ){
10543         for(i=0; i<nArg; i++){
10544           utf8_printf(p->out,"%s%s",azCol[i],
10545                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
10546         }
10547       }
10548       if( azArg==0 ) break;
10549       for(i=0; i<nArg; i++){
10550         char *z = azArg[i];
10551         if( z==0 ) z = p->nullValue;
10552         utf8_printf(p->out, "%s", z);
10553         if( i<nArg-1 ){
10554           utf8_printf(p->out, "%s", p->colSeparator);
10555         }else{
10556           utf8_printf(p->out, "%s", p->rowSeparator);
10557         }
10558       }
10559       break;
10560     }
10561     case MODE_Html: {
10562       if( p->cnt++==0 && p->showHeader ){
10563         raw_printf(p->out,"<TR>");
10564         for(i=0; i<nArg; i++){
10565           raw_printf(p->out,"<TH>");
10566           output_html_string(p->out, azCol[i]);
10567           raw_printf(p->out,"</TH>\n");
10568         }
10569         raw_printf(p->out,"</TR>\n");
10570       }
10571       if( azArg==0 ) break;
10572       raw_printf(p->out,"<TR>");
10573       for(i=0; i<nArg; i++){
10574         raw_printf(p->out,"<TD>");
10575         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10576         raw_printf(p->out,"</TD>\n");
10577       }
10578       raw_printf(p->out,"</TR>\n");
10579       break;
10580     }
10581     case MODE_Tcl: {
10582       if( p->cnt++==0 && p->showHeader ){
10583         for(i=0; i<nArg; i++){
10584           output_c_string(p->out,azCol[i] ? azCol[i] : "");
10585           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10586         }
10587         utf8_printf(p->out, "%s", p->rowSeparator);
10588       }
10589       if( azArg==0 ) break;
10590       for(i=0; i<nArg; i++){
10591         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10592         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10593       }
10594       utf8_printf(p->out, "%s", p->rowSeparator);
10595       break;
10596     }
10597     case MODE_Csv: {
10598       setBinaryMode(p->out, 1);
10599       if( p->cnt++==0 && p->showHeader ){
10600         for(i=0; i<nArg; i++){
10601           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
10602         }
10603         utf8_printf(p->out, "%s", p->rowSeparator);
10604       }
10605       if( nArg>0 ){
10606         for(i=0; i<nArg; i++){
10607           output_csv(p, azArg[i], i<nArg-1);
10608         }
10609         utf8_printf(p->out, "%s", p->rowSeparator);
10610       }
10611       setTextMode(p->out, 1);
10612       break;
10613     }
10614     case MODE_Insert: {
10615       if( azArg==0 ) break;
10616       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
10617       if( p->showHeader ){
10618         raw_printf(p->out,"(");
10619         for(i=0; i<nArg; i++){
10620           if( i>0 ) raw_printf(p->out, ",");
10621           if( quoteChar(azCol[i]) ){
10622             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
10623             utf8_printf(p->out, "%s", z);
10624             sqlite3_free(z);
10625           }else{
10626             raw_printf(p->out, "%s", azCol[i]);
10627           }
10628         }
10629         raw_printf(p->out,")");
10630       }
10631       p->cnt++;
10632       for(i=0; i<nArg; i++){
10633         raw_printf(p->out, i>0 ? "," : " VALUES(");
10634         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10635           utf8_printf(p->out,"NULL");
10636         }else if( aiType && aiType[i]==SQLITE_TEXT ){
10637           if( ShellHasFlag(p, SHFLG_Newlines) ){
10638             output_quoted_string(p->out, azArg[i]);
10639           }else{
10640             output_quoted_escaped_string(p->out, azArg[i]);
10641           }
10642         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10643           utf8_printf(p->out,"%s", azArg[i]);
10644         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10645           char z[50];
10646           double r = sqlite3_column_double(p->pStmt, i);
10647           sqlite3_uint64 ur;
10648           memcpy(&ur,&r,sizeof(r));
10649           if( ur==0x7ff0000000000000LL ){
10650             raw_printf(p->out, "1e999");
10651           }else if( ur==0xfff0000000000000LL ){
10652             raw_printf(p->out, "-1e999");
10653           }else{
10654             sqlite3_snprintf(50,z,"%!.20g", r);
10655             raw_printf(p->out, "%s", z);
10656           }
10657         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10658           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10659           int nBlob = sqlite3_column_bytes(p->pStmt, i);
10660           output_hex_blob(p->out, pBlob, nBlob);
10661         }else if( isNumber(azArg[i], 0) ){
10662           utf8_printf(p->out,"%s", azArg[i]);
10663         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
10664           output_quoted_string(p->out, azArg[i]);
10665         }else{
10666           output_quoted_escaped_string(p->out, azArg[i]);
10667         }
10668       }
10669       raw_printf(p->out,");\n");
10670       break;
10671     }
10672     case MODE_Quote: {
10673       if( azArg==0 ) break;
10674       if( p->cnt==0 && p->showHeader ){
10675         for(i=0; i<nArg; i++){
10676           if( i>0 ) raw_printf(p->out, ",");
10677           output_quoted_string(p->out, azCol[i]);
10678         }
10679         raw_printf(p->out,"\n");
10680       }
10681       p->cnt++;
10682       for(i=0; i<nArg; i++){
10683         if( i>0 ) raw_printf(p->out, ",");
10684         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10685           utf8_printf(p->out,"NULL");
10686         }else if( aiType && aiType[i]==SQLITE_TEXT ){
10687           output_quoted_string(p->out, azArg[i]);
10688         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10689           utf8_printf(p->out,"%s", azArg[i]);
10690         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10691           char z[50];
10692           double r = sqlite3_column_double(p->pStmt, i);
10693           sqlite3_snprintf(50,z,"%!.20g", r);
10694           raw_printf(p->out, "%s", z);
10695         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10696           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10697           int nBlob = sqlite3_column_bytes(p->pStmt, i);
10698           output_hex_blob(p->out, pBlob, nBlob);
10699         }else if( isNumber(azArg[i], 0) ){
10700           utf8_printf(p->out,"%s", azArg[i]);
10701         }else{
10702           output_quoted_string(p->out, azArg[i]);
10703         }
10704       }
10705       raw_printf(p->out,"\n");
10706       break;
10707     }
10708     case MODE_Ascii: {
10709       if( p->cnt++==0 && p->showHeader ){
10710         for(i=0; i<nArg; i++){
10711           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10712           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
10713         }
10714         utf8_printf(p->out, "%s", p->rowSeparator);
10715       }
10716       if( azArg==0 ) break;
10717       for(i=0; i<nArg; i++){
10718         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10719         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
10720       }
10721       utf8_printf(p->out, "%s", p->rowSeparator);
10722       break;
10723     }
10724     case MODE_EQP: {
10725       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
10726       break;
10727     }
10728   }
10729   return 0;
10730 }
10731 
10732 /*
10733 ** This is the callback routine that the SQLite library
10734 ** invokes for each row of a query result.
10735 */
10736 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
10737   /* since we don't have type info, call the shell_callback with a NULL value */
10738   return shell_callback(pArg, nArg, azArg, azCol, NULL);
10739 }
10740 
10741 /*
10742 ** This is the callback routine from sqlite3_exec() that appends all
10743 ** output onto the end of a ShellText object.
10744 */
10745 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
10746   ShellText *p = (ShellText*)pArg;
10747   int i;
10748   UNUSED_PARAMETER(az);
10749   if( azArg==0 ) return 0;
10750   if( p->n ) appendText(p, "|", 0);
10751   for(i=0; i<nArg; i++){
10752     if( i ) appendText(p, ",", 0);
10753     if( azArg[i] ) appendText(p, azArg[i], 0);
10754   }
10755   return 0;
10756 }
10757 
10758 /*
10759 ** Generate an appropriate SELFTEST table in the main database.
10760 */
10761 static void createSelftestTable(ShellState *p){
10762   char *zErrMsg = 0;
10763   sqlite3_exec(p->db,
10764     "SAVEPOINT selftest_init;\n"
10765     "CREATE TABLE IF NOT EXISTS selftest(\n"
10766     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
10767     "  op TEXT,\n"                   /* Operator:  memo run */
10768     "  cmd TEXT,\n"                  /* Command text */
10769     "  ans TEXT\n"                   /* Desired answer */
10770     ");"
10771     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
10772     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
10773     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
10774     "         'memo','Tests generated by --init');\n"
10775     "INSERT INTO [_shell$self]\n"
10776     "  SELECT 'run',\n"
10777     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
10778                                  "FROM sqlite_master ORDER BY 2'',224))',\n"
10779     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
10780                           "FROM sqlite_master ORDER BY 2',224));\n"
10781     "INSERT INTO [_shell$self]\n"
10782     "  SELECT 'run',"
10783     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
10784     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
10785     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
10786     "  FROM (\n"
10787     "    SELECT name FROM sqlite_master\n"
10788     "     WHERE type='table'\n"
10789     "       AND name<>'selftest'\n"
10790     "       AND coalesce(rootpage,0)>0\n"
10791     "  )\n"
10792     " ORDER BY name;\n"
10793     "INSERT INTO [_shell$self]\n"
10794     "  VALUES('run','PRAGMA integrity_check','ok');\n"
10795     "INSERT INTO selftest(tno,op,cmd,ans)"
10796     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
10797     "DROP TABLE [_shell$self];"
10798     ,0,0,&zErrMsg);
10799   if( zErrMsg ){
10800     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
10801     sqlite3_free(zErrMsg);
10802   }
10803   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
10804 }
10805 
10806 
10807 /*
10808 ** Set the destination table field of the ShellState structure to
10809 ** the name of the table given.  Escape any quote characters in the
10810 ** table name.
10811 */
10812 static void set_table_name(ShellState *p, const char *zName){
10813   int i, n;
10814   char cQuote;
10815   char *z;
10816 
10817   if( p->zDestTable ){
10818     free(p->zDestTable);
10819     p->zDestTable = 0;
10820   }
10821   if( zName==0 ) return;
10822   cQuote = quoteChar(zName);
10823   n = strlen30(zName);
10824   if( cQuote ) n += n+2;
10825   z = p->zDestTable = malloc( n+1 );
10826   if( z==0 ) shell_out_of_memory();
10827   n = 0;
10828   if( cQuote ) z[n++] = cQuote;
10829   for(i=0; zName[i]; i++){
10830     z[n++] = zName[i];
10831     if( zName[i]==cQuote ) z[n++] = cQuote;
10832   }
10833   if( cQuote ) z[n++] = cQuote;
10834   z[n] = 0;
10835 }
10836 
10837 
10838 /*
10839 ** Execute a query statement that will generate SQL output.  Print
10840 ** the result columns, comma-separated, on a line and then add a
10841 ** semicolon terminator to the end of that line.
10842 **
10843 ** If the number of columns is 1 and that column contains text "--"
10844 ** then write the semicolon on a separate line.  That way, if a
10845 ** "--" comment occurs at the end of the statement, the comment
10846 ** won't consume the semicolon terminator.
10847 */
10848 static int run_table_dump_query(
10849   ShellState *p,           /* Query context */
10850   const char *zSelect,     /* SELECT statement to extract content */
10851   const char *zFirstRow    /* Print before first row, if not NULL */
10852 ){
10853   sqlite3_stmt *pSelect;
10854   int rc;
10855   int nResult;
10856   int i;
10857   const char *z;
10858   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
10859   if( rc!=SQLITE_OK || !pSelect ){
10860     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
10861                 sqlite3_errmsg(p->db));
10862     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
10863     return rc;
10864   }
10865   rc = sqlite3_step(pSelect);
10866   nResult = sqlite3_column_count(pSelect);
10867   while( rc==SQLITE_ROW ){
10868     if( zFirstRow ){
10869       utf8_printf(p->out, "%s", zFirstRow);
10870       zFirstRow = 0;
10871     }
10872     z = (const char*)sqlite3_column_text(pSelect, 0);
10873     utf8_printf(p->out, "%s", z);
10874     for(i=1; i<nResult; i++){
10875       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
10876     }
10877     if( z==0 ) z = "";
10878     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
10879     if( z[0] ){
10880       raw_printf(p->out, "\n;\n");
10881     }else{
10882       raw_printf(p->out, ";\n");
10883     }
10884     rc = sqlite3_step(pSelect);
10885   }
10886   rc = sqlite3_finalize(pSelect);
10887   if( rc!=SQLITE_OK ){
10888     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
10889                 sqlite3_errmsg(p->db));
10890     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
10891   }
10892   return rc;
10893 }
10894 
10895 /*
10896 ** Allocate space and save off current error string.
10897 */
10898 static char *save_err_msg(
10899   sqlite3 *db            /* Database to query */
10900 ){
10901   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
10902   char *zErrMsg = sqlite3_malloc64(nErrMsg);
10903   if( zErrMsg ){
10904     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
10905   }
10906   return zErrMsg;
10907 }
10908 
10909 #ifdef __linux__
10910 /*
10911 ** Attempt to display I/O stats on Linux using /proc/PID/io
10912 */
10913 static void displayLinuxIoStats(FILE *out){
10914   FILE *in;
10915   char z[200];
10916   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
10917   in = fopen(z, "rb");
10918   if( in==0 ) return;
10919   while( fgets(z, sizeof(z), in)!=0 ){
10920     static const struct {
10921       const char *zPattern;
10922       const char *zDesc;
10923     } aTrans[] = {
10924       { "rchar: ",                  "Bytes received by read():" },
10925       { "wchar: ",                  "Bytes sent to write():"    },
10926       { "syscr: ",                  "Read() system calls:"      },
10927       { "syscw: ",                  "Write() system calls:"     },
10928       { "read_bytes: ",             "Bytes read from storage:"  },
10929       { "write_bytes: ",            "Bytes written to storage:" },
10930       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
10931     };
10932     int i;
10933     for(i=0; i<ArraySize(aTrans); i++){
10934       int n = strlen30(aTrans[i].zPattern);
10935       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
10936         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
10937         break;
10938       }
10939     }
10940   }
10941   fclose(in);
10942 }
10943 #endif
10944 
10945 /*
10946 ** Display a single line of status using 64-bit values.
10947 */
10948 static void displayStatLine(
10949   ShellState *p,            /* The shell context */
10950   char *zLabel,             /* Label for this one line */
10951   char *zFormat,            /* Format for the result */
10952   int iStatusCtrl,          /* Which status to display */
10953   int bReset                /* True to reset the stats */
10954 ){
10955   sqlite3_int64 iCur = -1;
10956   sqlite3_int64 iHiwtr = -1;
10957   int i, nPercent;
10958   char zLine[200];
10959   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
10960   for(i=0, nPercent=0; zFormat[i]; i++){
10961     if( zFormat[i]=='%' ) nPercent++;
10962   }
10963   if( nPercent>1 ){
10964     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
10965   }else{
10966     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
10967   }
10968   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
10969 }
10970 
10971 /*
10972 ** Display memory stats.
10973 */
10974 static int display_stats(
10975   sqlite3 *db,                /* Database to query */
10976   ShellState *pArg,           /* Pointer to ShellState */
10977   int bReset                  /* True to reset the stats */
10978 ){
10979   int iCur;
10980   int iHiwtr;
10981   FILE *out;
10982   if( pArg==0 || pArg->out==0 ) return 0;
10983   out = pArg->out;
10984 
10985   if( pArg->pStmt && (pArg->statsOn & 2) ){
10986     int nCol, i, x;
10987     sqlite3_stmt *pStmt = pArg->pStmt;
10988     char z[100];
10989     nCol = sqlite3_column_count(pStmt);
10990     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
10991     for(i=0; i<nCol; i++){
10992       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
10993       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
10994 #ifndef SQLITE_OMIT_DECLTYPE
10995       sqlite3_snprintf(30, z+x, "declared type:");
10996       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
10997 #endif
10998 #ifdef SQLITE_ENABLE_COLUMN_METADATA
10999       sqlite3_snprintf(30, z+x, "database name:");
11000       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
11001       sqlite3_snprintf(30, z+x, "table name:");
11002       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
11003       sqlite3_snprintf(30, z+x, "origin name:");
11004       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
11005 #endif
11006     }
11007   }
11008 
11009   displayStatLine(pArg, "Memory Used:",
11010      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
11011   displayStatLine(pArg, "Number of Outstanding Allocations:",
11012      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
11013   if( pArg->shellFlgs & SHFLG_Pagecache ){
11014     displayStatLine(pArg, "Number of Pcache Pages Used:",
11015        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
11016   }
11017   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
11018      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
11019   displayStatLine(pArg, "Largest Allocation:",
11020      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
11021   displayStatLine(pArg, "Largest Pcache Allocation:",
11022      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
11023 #ifdef YYTRACKMAXSTACKDEPTH
11024   displayStatLine(pArg, "Deepest Parser Stack:",
11025      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
11026 #endif
11027 
11028   if( db ){
11029     if( pArg->shellFlgs & SHFLG_Lookaside ){
11030       iHiwtr = iCur = -1;
11031       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
11032                         &iCur, &iHiwtr, bReset);
11033       raw_printf(pArg->out,
11034               "Lookaside Slots Used:                %d (max %d)\n",
11035               iCur, iHiwtr);
11036       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
11037                         &iCur, &iHiwtr, bReset);
11038       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
11039               iHiwtr);
11040       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
11041                         &iCur, &iHiwtr, bReset);
11042       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
11043               iHiwtr);
11044       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
11045                         &iCur, &iHiwtr, bReset);
11046       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
11047               iHiwtr);
11048     }
11049     iHiwtr = iCur = -1;
11050     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
11051     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
11052             iCur);
11053     iHiwtr = iCur = -1;
11054     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
11055     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
11056     iHiwtr = iCur = -1;
11057     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
11058     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
11059     iHiwtr = iCur = -1;
11060     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
11061     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
11062     iHiwtr = iCur = -1;
11063     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
11064     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
11065     iHiwtr = iCur = -1;
11066     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
11067     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
11068             iCur);
11069     iHiwtr = iCur = -1;
11070     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
11071     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
11072             iCur);
11073   }
11074 
11075   if( pArg->pStmt ){
11076     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
11077                                bReset);
11078     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
11079     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
11080     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
11081     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
11082     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
11083     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
11084     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
11085     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
11086     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
11087     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
11088     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
11089     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
11090     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
11091   }
11092 
11093 #ifdef __linux__
11094   displayLinuxIoStats(pArg->out);
11095 #endif
11096 
11097   /* Do not remove this machine readable comment: extra-stats-output-here */
11098 
11099   return 0;
11100 }
11101 
11102 /*
11103 ** Display scan stats.
11104 */
11105 static void display_scanstats(
11106   sqlite3 *db,                    /* Database to query */
11107   ShellState *pArg                /* Pointer to ShellState */
11108 ){
11109 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
11110   UNUSED_PARAMETER(db);
11111   UNUSED_PARAMETER(pArg);
11112 #else
11113   int i, k, n, mx;
11114   raw_printf(pArg->out, "-------- scanstats --------\n");
11115   mx = 0;
11116   for(k=0; k<=mx; k++){
11117     double rEstLoop = 1.0;
11118     for(i=n=0; 1; i++){
11119       sqlite3_stmt *p = pArg->pStmt;
11120       sqlite3_int64 nLoop, nVisit;
11121       double rEst;
11122       int iSid;
11123       const char *zExplain;
11124       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
11125         break;
11126       }
11127       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
11128       if( iSid>mx ) mx = iSid;
11129       if( iSid!=k ) continue;
11130       if( n==0 ){
11131         rEstLoop = (double)nLoop;
11132         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
11133       }
11134       n++;
11135       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
11136       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
11137       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
11138       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
11139       rEstLoop *= rEst;
11140       raw_printf(pArg->out,
11141           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
11142           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
11143       );
11144     }
11145   }
11146   raw_printf(pArg->out, "---------------------------\n");
11147 #endif
11148 }
11149 
11150 /*
11151 ** Parameter azArray points to a zero-terminated array of strings. zStr
11152 ** points to a single nul-terminated string. Return non-zero if zStr
11153 ** is equal, according to strcmp(), to any of the strings in the array.
11154 ** Otherwise, return zero.
11155 */
11156 static int str_in_array(const char *zStr, const char **azArray){
11157   int i;
11158   for(i=0; azArray[i]; i++){
11159     if( 0==strcmp(zStr, azArray[i]) ) return 1;
11160   }
11161   return 0;
11162 }
11163 
11164 /*
11165 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
11166 ** and populate the ShellState.aiIndent[] array with the number of
11167 ** spaces each opcode should be indented before it is output.
11168 **
11169 ** The indenting rules are:
11170 **
11171 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
11172 **       all opcodes that occur between the p2 jump destination and the opcode
11173 **       itself by 2 spaces.
11174 **
11175 **     * For each "Goto", if the jump destination is earlier in the program
11176 **       and ends on one of:
11177 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
11178 **       or if the P1 parameter is one instead of zero,
11179 **       then indent all opcodes between the earlier instruction
11180 **       and "Goto" by 2 spaces.
11181 */
11182 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
11183   const char *zSql;               /* The text of the SQL statement */
11184   const char *z;                  /* Used to check if this is an EXPLAIN */
11185   int *abYield = 0;               /* True if op is an OP_Yield */
11186   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
11187   int iOp;                        /* Index of operation in p->aiIndent[] */
11188 
11189   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
11190   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
11191                             "Rewind", 0 };
11192   const char *azGoto[] = { "Goto", 0 };
11193 
11194   /* Try to figure out if this is really an EXPLAIN statement. If this
11195   ** cannot be verified, return early.  */
11196   if( sqlite3_column_count(pSql)!=8 ){
11197     p->cMode = p->mode;
11198     return;
11199   }
11200   zSql = sqlite3_sql(pSql);
11201   if( zSql==0 ) return;
11202   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
11203   if( sqlite3_strnicmp(z, "explain", 7) ){
11204     p->cMode = p->mode;
11205     return;
11206   }
11207 
11208   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
11209     int i;
11210     int iAddr = sqlite3_column_int(pSql, 0);
11211     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
11212 
11213     /* Set p2 to the P2 field of the current opcode. Then, assuming that
11214     ** p2 is an instruction address, set variable p2op to the index of that
11215     ** instruction in the aiIndent[] array. p2 and p2op may be different if
11216     ** the current instruction is part of a sub-program generated by an
11217     ** SQL trigger or foreign key.  */
11218     int p2 = sqlite3_column_int(pSql, 3);
11219     int p2op = (p2 + (iOp-iAddr));
11220 
11221     /* Grow the p->aiIndent array as required */
11222     if( iOp>=nAlloc ){
11223       if( iOp==0 ){
11224         /* Do further verfication that this is explain output.  Abort if
11225         ** it is not */
11226         static const char *explainCols[] = {
11227            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
11228         int jj;
11229         for(jj=0; jj<ArraySize(explainCols); jj++){
11230           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
11231             p->cMode = p->mode;
11232             sqlite3_reset(pSql);
11233             return;
11234           }
11235         }
11236       }
11237       nAlloc += 100;
11238       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
11239       if( p->aiIndent==0 ) shell_out_of_memory();
11240       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
11241       if( abYield==0 ) shell_out_of_memory();
11242     }
11243     abYield[iOp] = str_in_array(zOp, azYield);
11244     p->aiIndent[iOp] = 0;
11245     p->nIndent = iOp+1;
11246 
11247     if( str_in_array(zOp, azNext) ){
11248       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11249     }
11250     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
11251      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
11252     ){
11253       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11254     }
11255   }
11256 
11257   p->iIndent = 0;
11258   sqlite3_free(abYield);
11259   sqlite3_reset(pSql);
11260 }
11261 
11262 /*
11263 ** Free the array allocated by explain_data_prepare().
11264 */
11265 static void explain_data_delete(ShellState *p){
11266   sqlite3_free(p->aiIndent);
11267   p->aiIndent = 0;
11268   p->nIndent = 0;
11269   p->iIndent = 0;
11270 }
11271 
11272 /*
11273 ** Disable and restore .wheretrace and .selecttrace settings.
11274 */
11275 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11276 extern int sqlite3SelectTrace;
11277 static int savedSelectTrace;
11278 #endif
11279 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11280 extern int sqlite3WhereTrace;
11281 static int savedWhereTrace;
11282 #endif
11283 static void disable_debug_trace_modes(void){
11284 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11285   savedSelectTrace = sqlite3SelectTrace;
11286   sqlite3SelectTrace = 0;
11287 #endif
11288 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11289   savedWhereTrace = sqlite3WhereTrace;
11290   sqlite3WhereTrace = 0;
11291 #endif
11292 }
11293 static void restore_debug_trace_modes(void){
11294 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11295   sqlite3SelectTrace = savedSelectTrace;
11296 #endif
11297 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11298   sqlite3WhereTrace = savedWhereTrace;
11299 #endif
11300 }
11301 
11302 /* Create the TEMP table used to store parameter bindings */
11303 static void bind_table_init(ShellState *p){
11304   int wrSchema = 0;
11305   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
11306   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
11307   sqlite3_exec(p->db,
11308     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
11309     "  key TEXT PRIMARY KEY,\n"
11310     "  value ANY\n"
11311     ") WITHOUT ROWID;",
11312     0, 0, 0);
11313   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
11314 }
11315 
11316 /*
11317 ** Bind parameters on a prepared statement.
11318 **
11319 ** Parameter bindings are taken from a TEMP table of the form:
11320 **
11321 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
11322 **    WITHOUT ROWID;
11323 **
11324 ** No bindings occur if this table does not exist.  The special character '$'
11325 ** is included in the table name to help prevent collisions with actual tables.
11326 ** The table must be in the TEMP schema.
11327 */
11328 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
11329   int nVar;
11330   int i;
11331   int rc;
11332   sqlite3_stmt *pQ = 0;
11333 
11334   nVar = sqlite3_bind_parameter_count(pStmt);
11335   if( nVar==0 ) return;  /* Nothing to do */
11336   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
11337                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
11338     return; /* Parameter table does not exist */
11339   }
11340   rc = sqlite3_prepare_v2(pArg->db,
11341           "SELECT value FROM temp.sqlite_parameters"
11342           " WHERE key=?1", -1, &pQ, 0);
11343   if( rc || pQ==0 ) return;
11344   for(i=1; i<=nVar; i++){
11345     char zNum[30];
11346     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
11347     if( zVar==0 ){
11348       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
11349       zVar = zNum;
11350     }
11351     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
11352     if( sqlite3_step(pQ)==SQLITE_ROW ){
11353       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
11354     }else{
11355       sqlite3_bind_null(pStmt, i);
11356     }
11357     sqlite3_reset(pQ);
11358   }
11359   sqlite3_finalize(pQ);
11360 }
11361 
11362 /*
11363 ** Run a prepared statement
11364 */
11365 static void exec_prepared_stmt(
11366   ShellState *pArg,                                /* Pointer to ShellState */
11367   sqlite3_stmt *pStmt                              /* Statment to run */
11368 ){
11369   int rc;
11370 
11371   /* perform the first step.  this will tell us if we
11372   ** have a result set or not and how wide it is.
11373   */
11374   rc = sqlite3_step(pStmt);
11375   /* if we have a result set... */
11376   if( SQLITE_ROW == rc ){
11377     /* allocate space for col name ptr, value ptr, and type */
11378     int nCol = sqlite3_column_count(pStmt);
11379     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
11380     if( !pData ){
11381       rc = SQLITE_NOMEM;
11382     }else{
11383       char **azCols = (char **)pData;      /* Names of result columns */
11384       char **azVals = &azCols[nCol];       /* Results */
11385       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
11386       int i, x;
11387       assert(sizeof(int) <= sizeof(char *));
11388       /* save off ptrs to column names */
11389       for(i=0; i<nCol; i++){
11390         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
11391       }
11392       do{
11393         /* extract the data and data types */
11394         for(i=0; i<nCol; i++){
11395           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
11396           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
11397             azVals[i] = "";
11398           }else{
11399             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
11400           }
11401           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
11402             rc = SQLITE_NOMEM;
11403             break; /* from for */
11404           }
11405         } /* end for */
11406 
11407         /* if data and types extracted successfully... */
11408         if( SQLITE_ROW == rc ){
11409           /* call the supplied callback with the result row data */
11410           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
11411             rc = SQLITE_ABORT;
11412           }else{
11413             rc = sqlite3_step(pStmt);
11414           }
11415         }
11416       } while( SQLITE_ROW == rc );
11417       sqlite3_free(pData);
11418     }
11419   }
11420 }
11421 
11422 #ifndef SQLITE_OMIT_VIRTUALTABLE
11423 /*
11424 ** This function is called to process SQL if the previous shell command
11425 ** was ".expert". It passes the SQL in the second argument directly to
11426 ** the sqlite3expert object.
11427 **
11428 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11429 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11430 ** an English language error message. It is the responsibility of the
11431 ** caller to eventually free this buffer using sqlite3_free().
11432 */
11433 static int expertHandleSQL(
11434   ShellState *pState,
11435   const char *zSql,
11436   char **pzErr
11437 ){
11438   assert( pState->expert.pExpert );
11439   assert( pzErr==0 || *pzErr==0 );
11440   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
11441 }
11442 
11443 /*
11444 ** This function is called either to silently clean up the object
11445 ** created by the ".expert" command (if bCancel==1), or to generate a
11446 ** report from it and then clean it up (if bCancel==0).
11447 **
11448 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11449 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11450 ** an English language error message. It is the responsibility of the
11451 ** caller to eventually free this buffer using sqlite3_free().
11452 */
11453 static int expertFinish(
11454   ShellState *pState,
11455   int bCancel,
11456   char **pzErr
11457 ){
11458   int rc = SQLITE_OK;
11459   sqlite3expert *p = pState->expert.pExpert;
11460   assert( p );
11461   assert( bCancel || pzErr==0 || *pzErr==0 );
11462   if( bCancel==0 ){
11463     FILE *out = pState->out;
11464     int bVerbose = pState->expert.bVerbose;
11465 
11466     rc = sqlite3_expert_analyze(p, pzErr);
11467     if( rc==SQLITE_OK ){
11468       int nQuery = sqlite3_expert_count(p);
11469       int i;
11470 
11471       if( bVerbose ){
11472         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
11473         raw_printf(out, "-- Candidates -----------------------------\n");
11474         raw_printf(out, "%s\n", zCand);
11475       }
11476       for(i=0; i<nQuery; i++){
11477         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
11478         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
11479         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
11480         if( zIdx==0 ) zIdx = "(no new indexes)\n";
11481         if( bVerbose ){
11482           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
11483           raw_printf(out, "%s\n\n", zSql);
11484         }
11485         raw_printf(out, "%s\n", zIdx);
11486         raw_printf(out, "%s\n", zEQP);
11487       }
11488     }
11489   }
11490   sqlite3_expert_destroy(p);
11491   pState->expert.pExpert = 0;
11492   return rc;
11493 }
11494 
11495 /*
11496 ** Implementation of ".expert" dot command.
11497 */
11498 static int expertDotCommand(
11499   ShellState *pState,             /* Current shell tool state */
11500   char **azArg,                   /* Array of arguments passed to dot command */
11501   int nArg                        /* Number of entries in azArg[] */
11502 ){
11503   int rc = SQLITE_OK;
11504   char *zErr = 0;
11505   int i;
11506   int iSample = 0;
11507 
11508   assert( pState->expert.pExpert==0 );
11509   memset(&pState->expert, 0, sizeof(ExpertInfo));
11510 
11511   for(i=1; rc==SQLITE_OK && i<nArg; i++){
11512     char *z = azArg[i];
11513     int n;
11514     if( z[0]=='-' && z[1]=='-' ) z++;
11515     n = strlen30(z);
11516     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
11517       pState->expert.bVerbose = 1;
11518     }
11519     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
11520       if( i==(nArg-1) ){
11521         raw_printf(stderr, "option requires an argument: %s\n", z);
11522         rc = SQLITE_ERROR;
11523       }else{
11524         iSample = (int)integerValue(azArg[++i]);
11525         if( iSample<0 || iSample>100 ){
11526           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
11527           rc = SQLITE_ERROR;
11528         }
11529       }
11530     }
11531     else{
11532       raw_printf(stderr, "unknown option: %s\n", z);
11533       rc = SQLITE_ERROR;
11534     }
11535   }
11536 
11537   if( rc==SQLITE_OK ){
11538     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
11539     if( pState->expert.pExpert==0 ){
11540       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
11541       rc = SQLITE_ERROR;
11542     }else{
11543       sqlite3_expert_config(
11544           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
11545       );
11546     }
11547   }
11548 
11549   return rc;
11550 }
11551 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11552 
11553 /*
11554 ** Execute a statement or set of statements.  Print
11555 ** any result rows/columns depending on the current mode
11556 ** set via the supplied callback.
11557 **
11558 ** This is very similar to SQLite's built-in sqlite3_exec()
11559 ** function except it takes a slightly different callback
11560 ** and callback data argument.
11561 */
11562 static int shell_exec(
11563   ShellState *pArg,                         /* Pointer to ShellState */
11564   const char *zSql,                         /* SQL to be evaluated */
11565   char **pzErrMsg                           /* Error msg written here */
11566 ){
11567   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
11568   int rc = SQLITE_OK;             /* Return Code */
11569   int rc2;
11570   const char *zLeftover;          /* Tail of unprocessed SQL */
11571   sqlite3 *db = pArg->db;
11572 
11573   if( pzErrMsg ){
11574     *pzErrMsg = NULL;
11575   }
11576 
11577 #ifndef SQLITE_OMIT_VIRTUALTABLE
11578   if( pArg->expert.pExpert ){
11579     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
11580     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
11581   }
11582 #endif
11583 
11584   while( zSql[0] && (SQLITE_OK == rc) ){
11585     static const char *zStmtSql;
11586     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
11587     if( SQLITE_OK != rc ){
11588       if( pzErrMsg ){
11589         *pzErrMsg = save_err_msg(db);
11590       }
11591     }else{
11592       if( !pStmt ){
11593         /* this happens for a comment or white-space */
11594         zSql = zLeftover;
11595         while( IsSpace(zSql[0]) ) zSql++;
11596         continue;
11597       }
11598       zStmtSql = sqlite3_sql(pStmt);
11599       if( zStmtSql==0 ) zStmtSql = "";
11600       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
11601 
11602       /* save off the prepared statment handle and reset row count */
11603       if( pArg ){
11604         pArg->pStmt = pStmt;
11605         pArg->cnt = 0;
11606       }
11607 
11608       /* echo the sql statement if echo on */
11609       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
11610         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
11611       }
11612 
11613       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
11614       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
11615         sqlite3_stmt *pExplain;
11616         char *zEQP;
11617         int triggerEQP = 0;
11618         disable_debug_trace_modes();
11619         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
11620         if( pArg->autoEQP>=AUTOEQP_trigger ){
11621           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
11622         }
11623         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
11624         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11625         if( rc==SQLITE_OK ){
11626           while( sqlite3_step(pExplain)==SQLITE_ROW ){
11627             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
11628             int iEqpId = sqlite3_column_int(pExplain, 0);
11629             int iParentId = sqlite3_column_int(pExplain, 1);
11630             if( zEQPLine[0]=='-' ) eqp_render(pArg);
11631             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
11632           }
11633           eqp_render(pArg);
11634         }
11635         sqlite3_finalize(pExplain);
11636         sqlite3_free(zEQP);
11637         if( pArg->autoEQP>=AUTOEQP_full ){
11638           /* Also do an EXPLAIN for ".eqp full" mode */
11639           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
11640           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11641           if( rc==SQLITE_OK ){
11642             pArg->cMode = MODE_Explain;
11643             explain_data_prepare(pArg, pExplain);
11644             exec_prepared_stmt(pArg, pExplain);
11645             explain_data_delete(pArg);
11646           }
11647           sqlite3_finalize(pExplain);
11648           sqlite3_free(zEQP);
11649         }
11650         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
11651           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
11652           /* Reprepare pStmt before reactiving trace modes */
11653           sqlite3_finalize(pStmt);
11654           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
11655           if( pArg ) pArg->pStmt = pStmt;
11656         }
11657         restore_debug_trace_modes();
11658       }
11659 
11660       if( pArg ){
11661         pArg->cMode = pArg->mode;
11662         if( pArg->autoExplain ){
11663           if( sqlite3_stmt_isexplain(pStmt)==1 ){
11664             pArg->cMode = MODE_Explain;
11665           }
11666           if( sqlite3_stmt_isexplain(pStmt)==2 ){
11667             pArg->cMode = MODE_EQP;
11668           }
11669         }
11670 
11671         /* If the shell is currently in ".explain" mode, gather the extra
11672         ** data required to add indents to the output.*/
11673         if( pArg->cMode==MODE_Explain ){
11674           explain_data_prepare(pArg, pStmt);
11675         }
11676       }
11677 
11678       bind_prepared_stmt(pArg, pStmt);
11679       exec_prepared_stmt(pArg, pStmt);
11680       explain_data_delete(pArg);
11681       eqp_render(pArg);
11682 
11683       /* print usage stats if stats on */
11684       if( pArg && pArg->statsOn ){
11685         display_stats(db, pArg, 0);
11686       }
11687 
11688       /* print loop-counters if required */
11689       if( pArg && pArg->scanstatsOn ){
11690         display_scanstats(db, pArg);
11691       }
11692 
11693       /* Finalize the statement just executed. If this fails, save a
11694       ** copy of the error message. Otherwise, set zSql to point to the
11695       ** next statement to execute. */
11696       rc2 = sqlite3_finalize(pStmt);
11697       if( rc!=SQLITE_NOMEM ) rc = rc2;
11698       if( rc==SQLITE_OK ){
11699         zSql = zLeftover;
11700         while( IsSpace(zSql[0]) ) zSql++;
11701       }else if( pzErrMsg ){
11702         *pzErrMsg = save_err_msg(db);
11703       }
11704 
11705       /* clear saved stmt handle */
11706       if( pArg ){
11707         pArg->pStmt = NULL;
11708       }
11709     }
11710   } /* end while */
11711 
11712   return rc;
11713 }
11714 
11715 /*
11716 ** Release memory previously allocated by tableColumnList().
11717 */
11718 static void freeColumnList(char **azCol){
11719   int i;
11720   for(i=1; azCol[i]; i++){
11721     sqlite3_free(azCol[i]);
11722   }
11723   /* azCol[0] is a static string */
11724   sqlite3_free(azCol);
11725 }
11726 
11727 /*
11728 ** Return a list of pointers to strings which are the names of all
11729 ** columns in table zTab.   The memory to hold the names is dynamically
11730 ** allocated and must be released by the caller using a subsequent call
11731 ** to freeColumnList().
11732 **
11733 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
11734 ** value that needs to be preserved, then azCol[0] is filled in with the
11735 ** name of the rowid column.
11736 **
11737 ** The first regular column in the table is azCol[1].  The list is terminated
11738 ** by an entry with azCol[i]==0.
11739 */
11740 static char **tableColumnList(ShellState *p, const char *zTab){
11741   char **azCol = 0;
11742   sqlite3_stmt *pStmt;
11743   char *zSql;
11744   int nCol = 0;
11745   int nAlloc = 0;
11746   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
11747   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
11748   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
11749   int rc;
11750 
11751   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
11752   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11753   sqlite3_free(zSql);
11754   if( rc ) return 0;
11755   while( sqlite3_step(pStmt)==SQLITE_ROW ){
11756     if( nCol>=nAlloc-2 ){
11757       nAlloc = nAlloc*2 + nCol + 10;
11758       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
11759       if( azCol==0 ) shell_out_of_memory();
11760     }
11761     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
11762     if( sqlite3_column_int(pStmt, 5) ){
11763       nPK++;
11764       if( nPK==1
11765        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
11766                           "INTEGER")==0
11767       ){
11768         isIPK = 1;
11769       }else{
11770         isIPK = 0;
11771       }
11772     }
11773   }
11774   sqlite3_finalize(pStmt);
11775   if( azCol==0 ) return 0;
11776   azCol[0] = 0;
11777   azCol[nCol+1] = 0;
11778 
11779   /* The decision of whether or not a rowid really needs to be preserved
11780   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
11781   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
11782   ** rowids on tables where the rowid is inaccessible because there are other
11783   ** columns in the table named "rowid", "_rowid_", and "oid".
11784   */
11785   if( preserveRowid && isIPK ){
11786     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
11787     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
11788     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
11789     ** ROWID aliases.  To distinguish these cases, check to see if
11790     ** there is a "pk" entry in "PRAGMA index_list".  There will be
11791     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
11792     */
11793     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
11794                            " WHERE origin='pk'", zTab);
11795     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11796     sqlite3_free(zSql);
11797     if( rc ){
11798       freeColumnList(azCol);
11799       return 0;
11800     }
11801     rc = sqlite3_step(pStmt);
11802     sqlite3_finalize(pStmt);
11803     preserveRowid = rc==SQLITE_ROW;
11804   }
11805   if( preserveRowid ){
11806     /* Only preserve the rowid if we can find a name to use for the
11807     ** rowid */
11808     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
11809     int i, j;
11810     for(j=0; j<3; j++){
11811       for(i=1; i<=nCol; i++){
11812         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
11813       }
11814       if( i>nCol ){
11815         /* At this point, we know that azRowid[j] is not the name of any
11816         ** ordinary column in the table.  Verify that azRowid[j] is a valid
11817         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
11818         ** tables will fail this last check */
11819         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
11820         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
11821         break;
11822       }
11823     }
11824   }
11825   return azCol;
11826 }
11827 
11828 /*
11829 ** Toggle the reverse_unordered_selects setting.
11830 */
11831 static void toggleSelectOrder(sqlite3 *db){
11832   sqlite3_stmt *pStmt = 0;
11833   int iSetting = 0;
11834   char zStmt[100];
11835   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
11836   if( sqlite3_step(pStmt)==SQLITE_ROW ){
11837     iSetting = sqlite3_column_int(pStmt, 0);
11838   }
11839   sqlite3_finalize(pStmt);
11840   sqlite3_snprintf(sizeof(zStmt), zStmt,
11841        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
11842   sqlite3_exec(db, zStmt, 0, 0, 0);
11843 }
11844 
11845 /*
11846 ** This is a different callback routine used for dumping the database.
11847 ** Each row received by this callback consists of a table name,
11848 ** the table type ("index" or "table") and SQL to create the table.
11849 ** This routine should print text sufficient to recreate the table.
11850 */
11851 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
11852   int rc;
11853   const char *zTable;
11854   const char *zType;
11855   const char *zSql;
11856   ShellState *p = (ShellState *)pArg;
11857 
11858   UNUSED_PARAMETER(azNotUsed);
11859   if( nArg!=3 || azArg==0 ) return 0;
11860   zTable = azArg[0];
11861   zType = azArg[1];
11862   zSql = azArg[2];
11863 
11864   if( strcmp(zTable, "sqlite_sequence")==0 ){
11865     raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
11866   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
11867     raw_printf(p->out, "ANALYZE sqlite_master;\n");
11868   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
11869     return 0;
11870   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
11871     char *zIns;
11872     if( !p->writableSchema ){
11873       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
11874       p->writableSchema = 1;
11875     }
11876     zIns = sqlite3_mprintf(
11877        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
11878        "VALUES('table','%q','%q',0,'%q');",
11879        zTable, zTable, zSql);
11880     utf8_printf(p->out, "%s\n", zIns);
11881     sqlite3_free(zIns);
11882     return 0;
11883   }else{
11884     printSchemaLine(p->out, zSql, ";\n");
11885   }
11886 
11887   if( strcmp(zType, "table")==0 ){
11888     ShellText sSelect;
11889     ShellText sTable;
11890     char **azCol;
11891     int i;
11892     char *savedDestTable;
11893     int savedMode;
11894 
11895     azCol = tableColumnList(p, zTable);
11896     if( azCol==0 ){
11897       p->nErr++;
11898       return 0;
11899     }
11900 
11901     /* Always quote the table name, even if it appears to be pure ascii,
11902     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
11903     initText(&sTable);
11904     appendText(&sTable, zTable, quoteChar(zTable));
11905     /* If preserving the rowid, add a column list after the table name.
11906     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
11907     ** instead of the usual "INSERT INTO tab VALUES(...)".
11908     */
11909     if( azCol[0] ){
11910       appendText(&sTable, "(", 0);
11911       appendText(&sTable, azCol[0], 0);
11912       for(i=1; azCol[i]; i++){
11913         appendText(&sTable, ",", 0);
11914         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
11915       }
11916       appendText(&sTable, ")", 0);
11917     }
11918 
11919     /* Build an appropriate SELECT statement */
11920     initText(&sSelect);
11921     appendText(&sSelect, "SELECT ", 0);
11922     if( azCol[0] ){
11923       appendText(&sSelect, azCol[0], 0);
11924       appendText(&sSelect, ",", 0);
11925     }
11926     for(i=1; azCol[i]; i++){
11927       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
11928       if( azCol[i+1] ){
11929         appendText(&sSelect, ",", 0);
11930       }
11931     }
11932     freeColumnList(azCol);
11933     appendText(&sSelect, " FROM ", 0);
11934     appendText(&sSelect, zTable, quoteChar(zTable));
11935 
11936     savedDestTable = p->zDestTable;
11937     savedMode = p->mode;
11938     p->zDestTable = sTable.z;
11939     p->mode = p->cMode = MODE_Insert;
11940     rc = shell_exec(p, sSelect.z, 0);
11941     if( (rc&0xff)==SQLITE_CORRUPT ){
11942       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11943       toggleSelectOrder(p->db);
11944       shell_exec(p, sSelect.z, 0);
11945       toggleSelectOrder(p->db);
11946     }
11947     p->zDestTable = savedDestTable;
11948     p->mode = savedMode;
11949     freeText(&sTable);
11950     freeText(&sSelect);
11951     if( rc ) p->nErr++;
11952   }
11953   return 0;
11954 }
11955 
11956 /*
11957 ** Run zQuery.  Use dump_callback() as the callback routine so that
11958 ** the contents of the query are output as SQL statements.
11959 **
11960 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
11961 ** "ORDER BY rowid DESC" to the end.
11962 */
11963 static int run_schema_dump_query(
11964   ShellState *p,
11965   const char *zQuery
11966 ){
11967   int rc;
11968   char *zErr = 0;
11969   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
11970   if( rc==SQLITE_CORRUPT ){
11971     char *zQ2;
11972     int len = strlen30(zQuery);
11973     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11974     if( zErr ){
11975       utf8_printf(p->out, "/****** %s ******/\n", zErr);
11976       sqlite3_free(zErr);
11977       zErr = 0;
11978     }
11979     zQ2 = malloc( len+100 );
11980     if( zQ2==0 ) return rc;
11981     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
11982     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
11983     if( rc ){
11984       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
11985     }else{
11986       rc = SQLITE_CORRUPT;
11987     }
11988     sqlite3_free(zErr);
11989     free(zQ2);
11990   }
11991   return rc;
11992 }
11993 
11994 /*
11995 ** Text of help messages.
11996 **
11997 ** The help text for each individual command begins with a line that starts
11998 ** with ".".  Subsequent lines are supplimental information.
11999 **
12000 ** There must be two or more spaces between the end of the command and the
12001 ** start of the description of what that command does.
12002 */
12003 static const char *(azHelp[]) = {
12004 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
12005   ".archive ...             Manage SQL archives",
12006   "   Each command must have exactly one of the following options:",
12007   "     -c, --create               Create a new archive",
12008   "     -u, --update               Add or update files with changed mtime",
12009   "     -i, --insert               Like -u but always add even if unchanged",
12010   "     -t, --list                 List contents of archive",
12011   "     -x, --extract              Extract files from archive",
12012   "   Optional arguments:",
12013   "     -v, --verbose              Print each filename as it is processed",
12014   "     -f FILE, --file FILE       Use archive FILE (default is current db)",
12015   "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
12016   "     -C DIR, --directory DIR    Read/extract files from directory DIR",
12017   "     -n, --dryrun               Show the SQL that would have occurred",
12018   "   Examples:",
12019   "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
12020   "     .ar -tf ARCHIVE          # List members of ARCHIVE",
12021   "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
12022   "   See also:",
12023   "      http://sqlite.org/cli.html#sqlar_archive_support",
12024 #endif
12025 #ifndef SQLITE_OMIT_AUTHORIZATION
12026   ".auth ON|OFF             Show authorizer callbacks",
12027 #endif
12028   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
12029   "       --append            Use the appendvfs",
12030   "       --async             Write to FILE without journal and fsync()",
12031   ".bail on|off             Stop after hitting an error.  Default OFF",
12032   ".binary on|off           Turn binary output on or off.  Default OFF",
12033   ".cd DIRECTORY            Change the working directory to DIRECTORY",
12034   ".changes on|off          Show number of rows changed by SQL",
12035   ".check GLOB              Fail if output since .testcase does not match",
12036   ".clone NEWDB             Clone data into NEWDB from the existing database",
12037   ".databases               List names and files of attached databases",
12038   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
12039   ".dbinfo ?DB?             Show status information about the database",
12040   ".dump ?TABLE? ...        Render all database content as SQL",
12041   "   Options:",
12042   "     --preserve-rowids      Include ROWID values in the output",
12043   "     --newlines             Allow unescaped newline characters in output",
12044   "   TABLE is a LIKE pattern for the tables to dump",
12045   ".echo on|off             Turn command echo on or off",
12046   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
12047   "   Other Modes:",
12048 #ifdef SQLITE_DEBUG
12049   "      test                  Show raw EXPLAIN QUERY PLAN output",
12050   "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
12051 #endif
12052   "      trigger               Like \"full\" but also show trigger bytecode",
12053   ".excel                   Display the output of next command in spreadsheet",
12054   ".exit ?CODE?             Exit this program with return-code CODE",
12055   ".expert                  EXPERIMENTAL. Suggest indexes for queries",
12056 /* Because explain mode comes on automatically now, the ".explain" mode
12057 ** is removed from the help screen. It is still supported for legacy, however */
12058 /*".explain ?on|off|auto?   Turn EXPLAIN output mode on or off",*/
12059   ".filectrl CMD ...        Run various sqlite3_file_control() operations",
12060   "                           Run \".filectrl\" with no arguments for details",
12061   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
12062   ".headers on|off          Turn display of headers on or off",
12063   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
12064   ".import FILE TABLE       Import data from FILE into TABLE",
12065 #ifndef SQLITE_OMIT_TEST_CONTROL
12066   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
12067 #endif
12068   ".indexes ?TABLE?         Show names of indexes",
12069   "                           If TABLE is specified, only show indexes for",
12070   "                           tables matching TABLE using the LIKE operator.",
12071 #ifdef SQLITE_ENABLE_IOTRACE
12072   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
12073 #endif
12074   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
12075   ".lint OPTIONS            Report potential schema issues.",
12076   "     Options:",
12077   "        fkey-indexes     Find missing foreign key indexes",
12078 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12079   ".load FILE ?ENTRY?       Load an extension library",
12080 #endif
12081   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
12082   ".mode MODE ?TABLE?       Set output mode",
12083   "   MODE is one of:",
12084   "     ascii    Columns/rows delimited by 0x1F and 0x1E",
12085   "     csv      Comma-separated values",
12086   "     column   Left-aligned columns.  (See .width)",
12087   "     html     HTML <table> code",
12088   "     insert   SQL insert statements for TABLE",
12089   "     line     One value per line",
12090   "     list     Values delimited by \"|\"",
12091   "     quote    Escape answers as for SQL",
12092   "     tabs     Tab-separated values",
12093   "     tcl      TCL list elements",
12094   ".nullvalue STRING        Use STRING in place of NULL values",
12095   ".once (-e|-x|FILE)       Output for the next SQL command only to FILE",
12096   "     If FILE begins with '|' then open as a pipe",
12097   "     Other options:",
12098   "       -e    Invoke system text editor",
12099   "       -x    Open in a spreadsheet",
12100   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
12101   "     Options:",
12102   "        --append        Use appendvfs to append database to the end of FILE",
12103 #ifdef SQLITE_ENABLE_DESERIALIZE
12104   "        --deserialize   Load into memory useing sqlite3_deserialize()",
12105   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
12106   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
12107 #endif
12108   "        --new           Initialize FILE to an empty database",
12109   "        --readonly      Open FILE readonly",
12110   "        --zip           FILE is a ZIP archive",
12111   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
12112   "     If FILE begins with '|' then open it as a pipe.",
12113   ".parameter CMD ...       Manage SQL parameter bindings",
12114   "   clear                   Erase all bindings",
12115   "   init                    Initialize the TEMP table that holds bindings",
12116   "   list                    List the current parameter bindings",
12117   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
12118   "                           PARAMETER should start with one of: $ : @ ?",
12119   "   unset PARAMETER         Remove PARAMETER from the binding table",
12120   ".print STRING...         Print literal STRING",
12121 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12122   ".progress N              Invoke progress handler after every N opcodes",
12123   "   --limit N                 Interrupt after N progress callbacks",
12124   "   --once                    Do no more than one progress interrupt",
12125   "   --quiet|-q                No output except at interrupts",
12126   "   --reset                   Reset the count for each input and interrupt",
12127 #endif
12128   ".prompt MAIN CONTINUE    Replace the standard prompts",
12129   ".quit                    Exit this program",
12130   ".read FILE               Read input from FILE",
12131 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12132   ".recover                 Recover as much data as possible from corrupt db.",
12133   "   --freelist-corrupt       Assume the freelist is corrupt",
12134   "   --recovery-db NAME       Store recovery metadata in database file NAME",
12135   "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
12136   "   --no-rowids              Do not attempt to recover rowid values",
12137   "                            that are not also INTEGER PRIMARY KEYs",
12138 #endif
12139   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
12140   ".save FILE               Write in-memory database into FILE",
12141   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
12142   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
12143   "     Options:",
12144   "         --indent            Try to pretty-print the schema",
12145   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
12146   "    Options:",
12147   "       --init               Create a new SELFTEST table",
12148   "       -v                   Verbose output",
12149   ".separator COL ?ROW?     Change the column and row separators",
12150 #if defined(SQLITE_ENABLE_SESSION)
12151   ".session ?NAME? CMD ...  Create or control sessions",
12152   "   Subcommands:",
12153   "     attach TABLE             Attach TABLE",
12154   "     changeset FILE           Write a changeset into FILE",
12155   "     close                    Close one session",
12156   "     enable ?BOOLEAN?         Set or query the enable bit",
12157   "     filter GLOB...           Reject tables matching GLOBs",
12158   "     indirect ?BOOLEAN?       Mark or query the indirect status",
12159   "     isempty                  Query whether the session is empty",
12160   "     list                     List currently open session names",
12161   "     open DB NAME             Open a new session on DB",
12162   "     patchset FILE            Write a patchset into FILE",
12163   "   If ?NAME? is omitted, the first defined session is used.",
12164 #endif
12165   ".sha3sum ...             Compute a SHA3 hash of database content",
12166   "    Options:",
12167   "      --schema              Also hash the sqlite_master table",
12168   "      --sha3-224            Use the sha3-224 algorithm",
12169   "      --sha3-256            Use the sha3-256 algorithm (default)",
12170   "      --sha3-384            Use the sha3-384 algorithm",
12171   "      --sha3-512            Use the sha3-512 algorithm",
12172   "    Any other argument is a LIKE pattern for tables to hash",
12173 #ifndef SQLITE_NOHAVE_SYSTEM
12174   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
12175 #endif
12176   ".show                    Show the current values for various settings",
12177   ".stats ?on|off?          Show stats or turn stats on or off",
12178 #ifndef SQLITE_NOHAVE_SYSTEM
12179   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
12180 #endif
12181   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
12182   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
12183   ".testctrl CMD ...        Run various sqlite3_test_control() operations",
12184   "                           Run \".testctrl\" with no arguments for details",
12185   ".timeout MS              Try opening locked tables for MS milliseconds",
12186   ".timer on|off            Turn SQL timer on or off",
12187 #ifndef SQLITE_OMIT_TRACE
12188   ".trace ?OPTIONS?         Output each SQL statement as it is run",
12189   "    FILE                    Send output to FILE",
12190   "    stdout                  Send output to stdout",
12191   "    stderr                  Send output to stderr",
12192   "    off                     Disable tracing",
12193   "    --expanded              Expand query parameters",
12194 #ifdef SQLITE_ENABLE_NORMALIZE
12195   "    --normalized            Normal the SQL statements",
12196 #endif
12197   "    --plain                 Show SQL as it is input",
12198   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
12199   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
12200   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
12201   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
12202 #endif /* SQLITE_OMIT_TRACE */
12203 #ifdef SQLITE_DEBUG
12204   ".unmodule NAME ...       Unregister virtual table modules",
12205   "    --allexcept             Unregister everything except those named",
12206 #endif
12207   ".vfsinfo ?AUX?           Information about the top-level VFS",
12208   ".vfslist                 List all available VFSes",
12209   ".vfsname ?AUX?           Print the name of the VFS stack",
12210   ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
12211   "     Negative values right-justify",
12212 };
12213 
12214 /*
12215 ** Output help text.
12216 **
12217 ** zPattern describes the set of commands for which help text is provided.
12218 ** If zPattern is NULL, then show all commands, but only give a one-line
12219 ** description of each.
12220 **
12221 ** Return the number of matches.
12222 */
12223 static int showHelp(FILE *out, const char *zPattern){
12224   int i = 0;
12225   int j = 0;
12226   int n = 0;
12227   char *zPat;
12228   if( zPattern==0
12229    || zPattern[0]=='0'
12230    || strcmp(zPattern,"-a")==0
12231    || strcmp(zPattern,"-all")==0
12232   ){
12233     /* Show all commands, but only one line per command */
12234     if( zPattern==0 ) zPattern = "";
12235     for(i=0; i<ArraySize(azHelp); i++){
12236       if( azHelp[i][0]=='.' || zPattern[0] ){
12237         utf8_printf(out, "%s\n", azHelp[i]);
12238         n++;
12239       }
12240     }
12241   }else{
12242     /* Look for commands that for which zPattern is an exact prefix */
12243     zPat = sqlite3_mprintf(".%s*", zPattern);
12244     for(i=0; i<ArraySize(azHelp); i++){
12245       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
12246         utf8_printf(out, "%s\n", azHelp[i]);
12247         j = i+1;
12248         n++;
12249       }
12250     }
12251     sqlite3_free(zPat);
12252     if( n ){
12253       if( n==1 ){
12254         /* when zPattern is a prefix of exactly one command, then include the
12255         ** details of that command, which should begin at offset j */
12256         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
12257           utf8_printf(out, "%s\n", azHelp[j]);
12258           j++;
12259         }
12260       }
12261       return n;
12262     }
12263     /* Look for commands that contain zPattern anywhere.  Show the complete
12264     ** text of all commands that match. */
12265     zPat = sqlite3_mprintf("%%%s%%", zPattern);
12266     for(i=0; i<ArraySize(azHelp); i++){
12267       if( azHelp[i][0]=='.' ) j = i;
12268       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
12269         utf8_printf(out, "%s\n", azHelp[j]);
12270         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
12271           j++;
12272           utf8_printf(out, "%s\n", azHelp[j]);
12273         }
12274         i = j;
12275         n++;
12276       }
12277     }
12278     sqlite3_free(zPat);
12279   }
12280   return n;
12281 }
12282 
12283 /* Forward reference */
12284 static int process_input(ShellState *p);
12285 
12286 /*
12287 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
12288 ** and return a pointer to the buffer. The caller is responsible for freeing
12289 ** the memory.
12290 **
12291 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
12292 ** read.
12293 **
12294 ** For convenience, a nul-terminator byte is always appended to the data read
12295 ** from the file before the buffer is returned. This byte is not included in
12296 ** the final value of (*pnByte), if applicable.
12297 **
12298 ** NULL is returned if any error is encountered. The final value of *pnByte
12299 ** is undefined in this case.
12300 */
12301 static char *readFile(const char *zName, int *pnByte){
12302   FILE *in = fopen(zName, "rb");
12303   long nIn;
12304   size_t nRead;
12305   char *pBuf;
12306   if( in==0 ) return 0;
12307   fseek(in, 0, SEEK_END);
12308   nIn = ftell(in);
12309   rewind(in);
12310   pBuf = sqlite3_malloc64( nIn+1 );
12311   if( pBuf==0 ){ fclose(in); return 0; }
12312   nRead = fread(pBuf, nIn, 1, in);
12313   fclose(in);
12314   if( nRead!=1 ){
12315     sqlite3_free(pBuf);
12316     return 0;
12317   }
12318   pBuf[nIn] = 0;
12319   if( pnByte ) *pnByte = nIn;
12320   return pBuf;
12321 }
12322 
12323 #if defined(SQLITE_ENABLE_SESSION)
12324 /*
12325 ** Close a single OpenSession object and release all of its associated
12326 ** resources.
12327 */
12328 static void session_close(OpenSession *pSession){
12329   int i;
12330   sqlite3session_delete(pSession->p);
12331   sqlite3_free(pSession->zName);
12332   for(i=0; i<pSession->nFilter; i++){
12333     sqlite3_free(pSession->azFilter[i]);
12334   }
12335   sqlite3_free(pSession->azFilter);
12336   memset(pSession, 0, sizeof(OpenSession));
12337 }
12338 #endif
12339 
12340 /*
12341 ** Close all OpenSession objects and release all associated resources.
12342 */
12343 #if defined(SQLITE_ENABLE_SESSION)
12344 static void session_close_all(ShellState *p){
12345   int i;
12346   for(i=0; i<p->nSession; i++){
12347     session_close(&p->aSession[i]);
12348   }
12349   p->nSession = 0;
12350 }
12351 #else
12352 # define session_close_all(X)
12353 #endif
12354 
12355 /*
12356 ** Implementation of the xFilter function for an open session.  Omit
12357 ** any tables named by ".session filter" but let all other table through.
12358 */
12359 #if defined(SQLITE_ENABLE_SESSION)
12360 static int session_filter(void *pCtx, const char *zTab){
12361   OpenSession *pSession = (OpenSession*)pCtx;
12362   int i;
12363   for(i=0; i<pSession->nFilter; i++){
12364     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
12365   }
12366   return 1;
12367 }
12368 #endif
12369 
12370 /*
12371 ** Try to deduce the type of file for zName based on its content.  Return
12372 ** one of the SHELL_OPEN_* constants.
12373 **
12374 ** If the file does not exist or is empty but its name looks like a ZIP
12375 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
12376 ** Otherwise, assume an ordinary database regardless of the filename if
12377 ** the type cannot be determined from content.
12378 */
12379 int deduceDatabaseType(const char *zName, int dfltZip){
12380   FILE *f = fopen(zName, "rb");
12381   size_t n;
12382   int rc = SHELL_OPEN_UNSPEC;
12383   char zBuf[100];
12384   if( f==0 ){
12385     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12386        return SHELL_OPEN_ZIPFILE;
12387     }else{
12388        return SHELL_OPEN_NORMAL;
12389     }
12390   }
12391   n = fread(zBuf, 16, 1, f);
12392   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
12393     fclose(f);
12394     return SHELL_OPEN_NORMAL;
12395   }
12396   fseek(f, -25, SEEK_END);
12397   n = fread(zBuf, 25, 1, f);
12398   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
12399     rc = SHELL_OPEN_APPENDVFS;
12400   }else{
12401     fseek(f, -22, SEEK_END);
12402     n = fread(zBuf, 22, 1, f);
12403     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
12404        && zBuf[3]==0x06 ){
12405       rc = SHELL_OPEN_ZIPFILE;
12406     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12407       rc = SHELL_OPEN_ZIPFILE;
12408     }
12409   }
12410   fclose(f);
12411   return rc;
12412 }
12413 
12414 #ifdef SQLITE_ENABLE_DESERIALIZE
12415 /*
12416 ** Reconstruct an in-memory database using the output from the "dbtotxt"
12417 ** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
12418 ** is 0, then read from standard input.
12419 */
12420 static unsigned char *readHexDb(ShellState *p, int *pnData){
12421   unsigned char *a = 0;
12422   int nLine;
12423   int n = 0;
12424   int pgsz = 0;
12425   int iOffset = 0;
12426   int j, k;
12427   int rc;
12428   FILE *in;
12429   unsigned int x[16];
12430   char zLine[1000];
12431   if( p->zDbFilename ){
12432     in = fopen(p->zDbFilename, "r");
12433     if( in==0 ){
12434       utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
12435       return 0;
12436     }
12437     nLine = 0;
12438   }else{
12439     in = p->in;
12440     nLine = p->lineno;
12441     if( in==0 ) in = stdin;
12442   }
12443   *pnData = 0;
12444   nLine++;
12445   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
12446   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
12447   if( rc!=2 ) goto readHexDb_error;
12448   if( n<0 ) goto readHexDb_error;
12449   if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
12450   n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
12451   a = sqlite3_malloc( n ? n : 1 );
12452   if( a==0 ){
12453     utf8_printf(stderr, "Out of memory!\n");
12454     goto readHexDb_error;
12455   }
12456   memset(a, 0, n);
12457   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
12458     utf8_printf(stderr, "invalid pagesize\n");
12459     goto readHexDb_error;
12460   }
12461   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
12462     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
12463     if( rc==2 ){
12464       iOffset = k;
12465       continue;
12466     }
12467     if( strncmp(zLine, "| end ", 6)==0 ){
12468       break;
12469     }
12470     rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
12471                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
12472                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
12473     if( rc==17 ){
12474       k = iOffset+j;
12475       if( k+16<=n ){
12476         int ii;
12477         for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
12478       }
12479     }
12480   }
12481   *pnData = n;
12482   if( in!=p->in ){
12483     fclose(in);
12484   }else{
12485     p->lineno = nLine;
12486   }
12487   return a;
12488 
12489 readHexDb_error:
12490   if( in!=p->in ){
12491     fclose(in);
12492   }else{
12493     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
12494       nLine++;
12495       if(strncmp(zLine, "| end ", 6)==0 ) break;
12496     }
12497     p->lineno = nLine;
12498   }
12499   sqlite3_free(a);
12500   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
12501   return 0;
12502 }
12503 #endif /* SQLITE_ENABLE_DESERIALIZE */
12504 
12505 /*
12506 ** Scalar function "shell_int32". The first argument to this function
12507 ** must be a blob. The second a non-negative integer. This function
12508 ** reads and returns a 32-bit big-endian integer from byte
12509 ** offset (4*<arg2>) of the blob.
12510 */
12511 static void shellInt32(
12512   sqlite3_context *context,
12513   int argc,
12514   sqlite3_value **argv
12515 ){
12516   const unsigned char *pBlob;
12517   int nBlob;
12518   int iInt;
12519 
12520   UNUSED_PARAMETER(argc);
12521   nBlob = sqlite3_value_bytes(argv[0]);
12522   pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
12523   iInt = sqlite3_value_int(argv[1]);
12524 
12525   if( iInt>=0 && (iInt+1)*4<=nBlob ){
12526     const unsigned char *a = &pBlob[iInt*4];
12527     sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
12528                        + ((sqlite3_int64)a[1]<<16)
12529                        + ((sqlite3_int64)a[2]<< 8)
12530                        + ((sqlite3_int64)a[3]<< 0);
12531     sqlite3_result_int64(context, iVal);
12532   }
12533 }
12534 
12535 /*
12536 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
12537 ** using "..." with internal double-quote characters doubled.
12538 */
12539 static void shellIdQuote(
12540   sqlite3_context *context,
12541   int argc,
12542   sqlite3_value **argv
12543 ){
12544   const char *zName = (const char*)sqlite3_value_text(argv[0]);
12545   UNUSED_PARAMETER(argc);
12546   if( zName ){
12547     char *z = sqlite3_mprintf("\"%w\"", zName);
12548     sqlite3_result_text(context, z, -1, sqlite3_free);
12549   }
12550 }
12551 
12552 /*
12553 ** Scalar function "shell_escape_crnl" used by the .recover command.
12554 ** The argument passed to this function is the output of built-in
12555 ** function quote(). If the first character of the input is "'",
12556 ** indicating that the value passed to quote() was a text value,
12557 ** then this function searches the input for "\n" and "\r" characters
12558 ** and adds a wrapper similar to the following:
12559 **
12560 **   replace(replace(<input>, '\n', char(10), '\r', char(13));
12561 **
12562 ** Or, if the first character of the input is not "'", then a copy
12563 ** of the input is returned.
12564 */
12565 static void shellEscapeCrnl(
12566   sqlite3_context *context,
12567   int argc,
12568   sqlite3_value **argv
12569 ){
12570   const char *zText = (const char*)sqlite3_value_text(argv[0]);
12571   UNUSED_PARAMETER(argc);
12572   if( zText[0]=='\'' ){
12573     int nText = sqlite3_value_bytes(argv[0]);
12574     int i;
12575     char zBuf1[20];
12576     char zBuf2[20];
12577     const char *zNL = 0;
12578     const char *zCR = 0;
12579     int nCR = 0;
12580     int nNL = 0;
12581 
12582     for(i=0; zText[i]; i++){
12583       if( zNL==0 && zText[i]=='\n' ){
12584         zNL = unused_string(zText, "\\n", "\\012", zBuf1);
12585         nNL = (int)strlen(zNL);
12586       }
12587       if( zCR==0 && zText[i]=='\r' ){
12588         zCR = unused_string(zText, "\\r", "\\015", zBuf2);
12589         nCR = (int)strlen(zCR);
12590       }
12591     }
12592 
12593     if( zNL || zCR ){
12594       int iOut = 0;
12595       i64 nMax = (nNL > nCR) ? nNL : nCR;
12596       i64 nAlloc = nMax * nText + (nMax+64)*2;
12597       char *zOut = (char*)sqlite3_malloc64(nAlloc);
12598       if( zOut==0 ){
12599         sqlite3_result_error_nomem(context);
12600         return;
12601       }
12602 
12603       if( zNL && zCR ){
12604         memcpy(&zOut[iOut], "replace(replace(", 16);
12605         iOut += 16;
12606       }else{
12607         memcpy(&zOut[iOut], "replace(", 8);
12608         iOut += 8;
12609       }
12610       for(i=0; zText[i]; i++){
12611         if( zText[i]=='\n' ){
12612           memcpy(&zOut[iOut], zNL, nNL);
12613           iOut += nNL;
12614         }else if( zText[i]=='\r' ){
12615           memcpy(&zOut[iOut], zCR, nCR);
12616           iOut += nCR;
12617         }else{
12618           zOut[iOut] = zText[i];
12619           iOut++;
12620         }
12621       }
12622 
12623       if( zNL ){
12624         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12625         memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
12626         memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
12627       }
12628       if( zCR ){
12629         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12630         memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
12631         memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
12632       }
12633 
12634       sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
12635       sqlite3_free(zOut);
12636       return;
12637     }
12638   }
12639 
12640   sqlite3_result_value(context, argv[0]);
12641 }
12642 
12643 /* Flags for open_db().
12644 **
12645 ** The default behavior of open_db() is to exit(1) if the database fails to
12646 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
12647 ** but still returns without calling exit.
12648 **
12649 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
12650 ** ZIP archive if the file does not exist or is empty and its name matches
12651 ** the *.zip pattern.
12652 */
12653 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
12654 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
12655 
12656 /*
12657 ** Make sure the database is open.  If it is not, then open it.  If
12658 ** the database fails to open, print an error message and exit.
12659 */
12660 static void open_db(ShellState *p, int openFlags){
12661   if( p->db==0 ){
12662     if( p->openMode==SHELL_OPEN_UNSPEC ){
12663       if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
12664         p->openMode = SHELL_OPEN_NORMAL;
12665       }else{
12666         p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
12667                              (openFlags & OPEN_DB_ZIPFILE)!=0);
12668       }
12669     }
12670     switch( p->openMode ){
12671       case SHELL_OPEN_APPENDVFS: {
12672         sqlite3_open_v2(p->zDbFilename, &p->db,
12673            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
12674         break;
12675       }
12676       case SHELL_OPEN_HEXDB:
12677       case SHELL_OPEN_DESERIALIZE: {
12678         sqlite3_open(0, &p->db);
12679         break;
12680       }
12681       case SHELL_OPEN_ZIPFILE: {
12682         sqlite3_open(":memory:", &p->db);
12683         break;
12684       }
12685       case SHELL_OPEN_READONLY: {
12686         sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
12687         break;
12688       }
12689       case SHELL_OPEN_UNSPEC:
12690       case SHELL_OPEN_NORMAL: {
12691         sqlite3_open(p->zDbFilename, &p->db);
12692         break;
12693       }
12694     }
12695     globalDb = p->db;
12696     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
12697       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
12698           p->zDbFilename, sqlite3_errmsg(p->db));
12699       if( openFlags & OPEN_DB_KEEPALIVE ){
12700         sqlite3_open(":memory:", &p->db);
12701         return;
12702       }
12703       exit(1);
12704     }
12705 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12706     sqlite3_enable_load_extension(p->db, 1);
12707 #endif
12708     sqlite3_fileio_init(p->db, 0, 0);
12709     sqlite3_shathree_init(p->db, 0, 0);
12710     sqlite3_completion_init(p->db, 0, 0);
12711 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12712     sqlite3_dbdata_init(p->db, 0, 0);
12713 #endif
12714 #ifdef SQLITE_HAVE_ZLIB
12715     sqlite3_zipfile_init(p->db, 0, 0);
12716     sqlite3_sqlar_init(p->db, 0, 0);
12717 #endif
12718     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
12719                             shellAddSchemaName, 0, 0);
12720     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
12721                             shellModuleSchema, 0, 0);
12722     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
12723                             shellPutsFunc, 0, 0);
12724     sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
12725                             shellEscapeCrnl, 0, 0);
12726     sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
12727                             shellInt32, 0, 0);
12728     sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
12729                             shellIdQuote, 0, 0);
12730 #ifndef SQLITE_NOHAVE_SYSTEM
12731     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
12732                             editFunc, 0, 0);
12733     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
12734                             editFunc, 0, 0);
12735 #endif
12736     if( p->openMode==SHELL_OPEN_ZIPFILE ){
12737       char *zSql = sqlite3_mprintf(
12738          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
12739       sqlite3_exec(p->db, zSql, 0, 0, 0);
12740       sqlite3_free(zSql);
12741     }
12742 #ifdef SQLITE_ENABLE_DESERIALIZE
12743     else
12744     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
12745       int rc;
12746       int nData = 0;
12747       unsigned char *aData;
12748       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
12749         aData = (unsigned char*)readFile(p->zDbFilename, &nData);
12750       }else{
12751         aData = readHexDb(p, &nData);
12752         if( aData==0 ){
12753           return;
12754         }
12755       }
12756       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
12757                    SQLITE_DESERIALIZE_RESIZEABLE |
12758                    SQLITE_DESERIALIZE_FREEONCLOSE);
12759       if( rc ){
12760         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
12761       }
12762       if( p->szMax>0 ){
12763         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
12764       }
12765     }
12766 #endif
12767   }
12768 }
12769 
12770 /*
12771 ** Attempt to close the databaes connection.  Report errors.
12772 */
12773 void close_db(sqlite3 *db){
12774   int rc = sqlite3_close(db);
12775   if( rc ){
12776     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
12777         rc, sqlite3_errmsg(db));
12778   }
12779 }
12780 
12781 #if HAVE_READLINE || HAVE_EDITLINE
12782 /*
12783 ** Readline completion callbacks
12784 */
12785 static char *readline_completion_generator(const char *text, int state){
12786   static sqlite3_stmt *pStmt = 0;
12787   char *zRet;
12788   if( state==0 ){
12789     char *zSql;
12790     sqlite3_finalize(pStmt);
12791     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
12792                            "  FROM completion(%Q) ORDER BY 1", text);
12793     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
12794     sqlite3_free(zSql);
12795   }
12796   if( sqlite3_step(pStmt)==SQLITE_ROW ){
12797     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
12798   }else{
12799     sqlite3_finalize(pStmt);
12800     pStmt = 0;
12801     zRet = 0;
12802   }
12803   return zRet;
12804 }
12805 static char **readline_completion(const char *zText, int iStart, int iEnd){
12806   rl_attempted_completion_over = 1;
12807   return rl_completion_matches(zText, readline_completion_generator);
12808 }
12809 
12810 #elif HAVE_LINENOISE
12811 /*
12812 ** Linenoise completion callback
12813 */
12814 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
12815   int nLine = strlen30(zLine);
12816   int i, iStart;
12817   sqlite3_stmt *pStmt = 0;
12818   char *zSql;
12819   char zBuf[1000];
12820 
12821   if( nLine>sizeof(zBuf)-30 ) return;
12822   if( zLine[0]=='.' || zLine[0]=='#') return;
12823   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
12824   if( i==nLine-1 ) return;
12825   iStart = i+1;
12826   memcpy(zBuf, zLine, iStart);
12827   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
12828                          "  FROM completion(%Q,%Q) ORDER BY 1",
12829                          &zLine[iStart], zLine);
12830   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
12831   sqlite3_free(zSql);
12832   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
12833   while( sqlite3_step(pStmt)==SQLITE_ROW ){
12834     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
12835     int nCompletion = sqlite3_column_bytes(pStmt, 0);
12836     if( iStart+nCompletion < sizeof(zBuf)-1 ){
12837       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
12838       linenoiseAddCompletion(lc, zBuf);
12839     }
12840   }
12841   sqlite3_finalize(pStmt);
12842 }
12843 #endif
12844 
12845 /*
12846 ** Do C-language style dequoting.
12847 **
12848 **    \a    -> alarm
12849 **    \b    -> backspace
12850 **    \t    -> tab
12851 **    \n    -> newline
12852 **    \v    -> vertical tab
12853 **    \f    -> form feed
12854 **    \r    -> carriage return
12855 **    \s    -> space
12856 **    \"    -> "
12857 **    \'    -> '
12858 **    \\    -> backslash
12859 **    \NNN  -> ascii character NNN in octal
12860 */
12861 static void resolve_backslashes(char *z){
12862   int i, j;
12863   char c;
12864   while( *z && *z!='\\' ) z++;
12865   for(i=j=0; (c = z[i])!=0; i++, j++){
12866     if( c=='\\' && z[i+1]!=0 ){
12867       c = z[++i];
12868       if( c=='a' ){
12869         c = '\a';
12870       }else if( c=='b' ){
12871         c = '\b';
12872       }else if( c=='t' ){
12873         c = '\t';
12874       }else if( c=='n' ){
12875         c = '\n';
12876       }else if( c=='v' ){
12877         c = '\v';
12878       }else if( c=='f' ){
12879         c = '\f';
12880       }else if( c=='r' ){
12881         c = '\r';
12882       }else if( c=='"' ){
12883         c = '"';
12884       }else if( c=='\'' ){
12885         c = '\'';
12886       }else if( c=='\\' ){
12887         c = '\\';
12888       }else if( c>='0' && c<='7' ){
12889         c -= '0';
12890         if( z[i+1]>='0' && z[i+1]<='7' ){
12891           i++;
12892           c = (c<<3) + z[i] - '0';
12893           if( z[i+1]>='0' && z[i+1]<='7' ){
12894             i++;
12895             c = (c<<3) + z[i] - '0';
12896           }
12897         }
12898       }
12899     }
12900     z[j] = c;
12901   }
12902   if( j<i ) z[j] = 0;
12903 }
12904 
12905 /*
12906 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
12907 ** for TRUE and FALSE.  Return the integer value if appropriate.
12908 */
12909 static int booleanValue(const char *zArg){
12910   int i;
12911   if( zArg[0]=='0' && zArg[1]=='x' ){
12912     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
12913   }else{
12914     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
12915   }
12916   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
12917   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
12918     return 1;
12919   }
12920   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
12921     return 0;
12922   }
12923   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
12924           zArg);
12925   return 0;
12926 }
12927 
12928 /*
12929 ** Set or clear a shell flag according to a boolean value.
12930 */
12931 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
12932   if( booleanValue(zArg) ){
12933     ShellSetFlag(p, mFlag);
12934   }else{
12935     ShellClearFlag(p, mFlag);
12936   }
12937 }
12938 
12939 /*
12940 ** Close an output file, assuming it is not stderr or stdout
12941 */
12942 static void output_file_close(FILE *f){
12943   if( f && f!=stdout && f!=stderr ) fclose(f);
12944 }
12945 
12946 /*
12947 ** Try to open an output file.   The names "stdout" and "stderr" are
12948 ** recognized and do the right thing.  NULL is returned if the output
12949 ** filename is "off".
12950 */
12951 static FILE *output_file_open(const char *zFile, int bTextMode){
12952   FILE *f;
12953   if( strcmp(zFile,"stdout")==0 ){
12954     f = stdout;
12955   }else if( strcmp(zFile, "stderr")==0 ){
12956     f = stderr;
12957   }else if( strcmp(zFile, "off")==0 ){
12958     f = 0;
12959   }else{
12960     f = fopen(zFile, bTextMode ? "w" : "wb");
12961     if( f==0 ){
12962       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
12963     }
12964   }
12965   return f;
12966 }
12967 
12968 #ifndef SQLITE_OMIT_TRACE
12969 /*
12970 ** A routine for handling output from sqlite3_trace().
12971 */
12972 static int sql_trace_callback(
12973   unsigned mType,         /* The trace type */
12974   void *pArg,             /* The ShellState pointer */
12975   void *pP,               /* Usually a pointer to sqlite_stmt */
12976   void *pX                /* Auxiliary output */
12977 ){
12978   ShellState *p = (ShellState*)pArg;
12979   sqlite3_stmt *pStmt;
12980   const char *zSql;
12981   int nSql;
12982   if( p->traceOut==0 ) return 0;
12983   if( mType==SQLITE_TRACE_CLOSE ){
12984     utf8_printf(p->traceOut, "-- closing database connection\n");
12985     return 0;
12986   }
12987   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
12988     zSql = (const char*)pX;
12989   }else{
12990     pStmt = (sqlite3_stmt*)pP;
12991     switch( p->eTraceType ){
12992       case SHELL_TRACE_EXPANDED: {
12993         zSql = sqlite3_expanded_sql(pStmt);
12994         break;
12995       }
12996 #ifdef SQLITE_ENABLE_NORMALIZE
12997       case SHELL_TRACE_NORMALIZED: {
12998         zSql = sqlite3_normalized_sql(pStmt);
12999         break;
13000       }
13001 #endif
13002       default: {
13003         zSql = sqlite3_sql(pStmt);
13004         break;
13005       }
13006     }
13007   }
13008   if( zSql==0 ) return 0;
13009   nSql = strlen30(zSql);
13010   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
13011   switch( mType ){
13012     case SQLITE_TRACE_ROW:
13013     case SQLITE_TRACE_STMT: {
13014       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
13015       break;
13016     }
13017     case SQLITE_TRACE_PROFILE: {
13018       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
13019       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
13020       break;
13021     }
13022   }
13023   return 0;
13024 }
13025 #endif
13026 
13027 /*
13028 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
13029 ** a useful spot to set a debugger breakpoint.
13030 */
13031 static void test_breakpoint(void){
13032   static int nCall = 0;
13033   nCall++;
13034 }
13035 
13036 /*
13037 ** An object used to read a CSV and other files for import.
13038 */
13039 typedef struct ImportCtx ImportCtx;
13040 struct ImportCtx {
13041   const char *zFile;  /* Name of the input file */
13042   FILE *in;           /* Read the CSV text from this input stream */
13043   char *z;            /* Accumulated text for a field */
13044   int n;              /* Number of bytes in z */
13045   int nAlloc;         /* Space allocated for z[] */
13046   int nLine;          /* Current line number */
13047   int bNotFirst;      /* True if one or more bytes already read */
13048   int cTerm;          /* Character that terminated the most recent field */
13049   int cColSep;        /* The column separator character.  (Usually ",") */
13050   int cRowSep;        /* The row separator character.  (Usually "\n") */
13051 };
13052 
13053 /* Append a single byte to z[] */
13054 static void import_append_char(ImportCtx *p, int c){
13055   if( p->n+1>=p->nAlloc ){
13056     p->nAlloc += p->nAlloc + 100;
13057     p->z = sqlite3_realloc64(p->z, p->nAlloc);
13058     if( p->z==0 ) shell_out_of_memory();
13059   }
13060   p->z[p->n++] = (char)c;
13061 }
13062 
13063 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
13064 ** with the option of having a separator other than ",".
13065 **
13066 **   +  Input comes from p->in.
13067 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
13068 **      from sqlite3_malloc64().
13069 **   +  Use p->cSep as the column separator.  The default is ",".
13070 **   +  Use p->rSep as the row separator.  The default is "\n".
13071 **   +  Keep track of the line number in p->nLine.
13072 **   +  Store the character that terminates the field in p->cTerm.  Store
13073 **      EOF on end-of-file.
13074 **   +  Report syntax errors on stderr
13075 */
13076 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
13077   int c;
13078   int cSep = p->cColSep;
13079   int rSep = p->cRowSep;
13080   p->n = 0;
13081   c = fgetc(p->in);
13082   if( c==EOF || seenInterrupt ){
13083     p->cTerm = EOF;
13084     return 0;
13085   }
13086   if( c=='"' ){
13087     int pc, ppc;
13088     int startLine = p->nLine;
13089     int cQuote = c;
13090     pc = ppc = 0;
13091     while( 1 ){
13092       c = fgetc(p->in);
13093       if( c==rSep ) p->nLine++;
13094       if( c==cQuote ){
13095         if( pc==cQuote ){
13096           pc = 0;
13097           continue;
13098         }
13099       }
13100       if( (c==cSep && pc==cQuote)
13101        || (c==rSep && pc==cQuote)
13102        || (c==rSep && pc=='\r' && ppc==cQuote)
13103        || (c==EOF && pc==cQuote)
13104       ){
13105         do{ p->n--; }while( p->z[p->n]!=cQuote );
13106         p->cTerm = c;
13107         break;
13108       }
13109       if( pc==cQuote && c!='\r' ){
13110         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
13111                 p->zFile, p->nLine, cQuote);
13112       }
13113       if( c==EOF ){
13114         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
13115                 p->zFile, startLine, cQuote);
13116         p->cTerm = c;
13117         break;
13118       }
13119       import_append_char(p, c);
13120       ppc = pc;
13121       pc = c;
13122     }
13123   }else{
13124     /* If this is the first field being parsed and it begins with the
13125     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
13126     if( (c&0xff)==0xef && p->bNotFirst==0 ){
13127       import_append_char(p, c);
13128       c = fgetc(p->in);
13129       if( (c&0xff)==0xbb ){
13130         import_append_char(p, c);
13131         c = fgetc(p->in);
13132         if( (c&0xff)==0xbf ){
13133           p->bNotFirst = 1;
13134           p->n = 0;
13135           return csv_read_one_field(p);
13136         }
13137       }
13138     }
13139     while( c!=EOF && c!=cSep && c!=rSep ){
13140       import_append_char(p, c);
13141       c = fgetc(p->in);
13142     }
13143     if( c==rSep ){
13144       p->nLine++;
13145       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
13146     }
13147     p->cTerm = c;
13148   }
13149   if( p->z ) p->z[p->n] = 0;
13150   p->bNotFirst = 1;
13151   return p->z;
13152 }
13153 
13154 /* Read a single field of ASCII delimited text.
13155 **
13156 **   +  Input comes from p->in.
13157 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
13158 **      from sqlite3_malloc64().
13159 **   +  Use p->cSep as the column separator.  The default is "\x1F".
13160 **   +  Use p->rSep as the row separator.  The default is "\x1E".
13161 **   +  Keep track of the row number in p->nLine.
13162 **   +  Store the character that terminates the field in p->cTerm.  Store
13163 **      EOF on end-of-file.
13164 **   +  Report syntax errors on stderr
13165 */
13166 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
13167   int c;
13168   int cSep = p->cColSep;
13169   int rSep = p->cRowSep;
13170   p->n = 0;
13171   c = fgetc(p->in);
13172   if( c==EOF || seenInterrupt ){
13173     p->cTerm = EOF;
13174     return 0;
13175   }
13176   while( c!=EOF && c!=cSep && c!=rSep ){
13177     import_append_char(p, c);
13178     c = fgetc(p->in);
13179   }
13180   if( c==rSep ){
13181     p->nLine++;
13182   }
13183   p->cTerm = c;
13184   if( p->z ) p->z[p->n] = 0;
13185   return p->z;
13186 }
13187 
13188 /*
13189 ** Try to transfer data for table zTable.  If an error is seen while
13190 ** moving forward, try to go backwards.  The backwards movement won't
13191 ** work for WITHOUT ROWID tables.
13192 */
13193 static void tryToCloneData(
13194   ShellState *p,
13195   sqlite3 *newDb,
13196   const char *zTable
13197 ){
13198   sqlite3_stmt *pQuery = 0;
13199   sqlite3_stmt *pInsert = 0;
13200   char *zQuery = 0;
13201   char *zInsert = 0;
13202   int rc;
13203   int i, j, n;
13204   int nTable = strlen30(zTable);
13205   int k = 0;
13206   int cnt = 0;
13207   const int spinRate = 10000;
13208 
13209   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
13210   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13211   if( rc ){
13212     utf8_printf(stderr, "Error %d: %s on [%s]\n",
13213             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13214             zQuery);
13215     goto end_data_xfer;
13216   }
13217   n = sqlite3_column_count(pQuery);
13218   zInsert = sqlite3_malloc64(200 + nTable + n*3);
13219   if( zInsert==0 ) shell_out_of_memory();
13220   sqlite3_snprintf(200+nTable,zInsert,
13221                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
13222   i = strlen30(zInsert);
13223   for(j=1; j<n; j++){
13224     memcpy(zInsert+i, ",?", 2);
13225     i += 2;
13226   }
13227   memcpy(zInsert+i, ");", 3);
13228   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
13229   if( rc ){
13230     utf8_printf(stderr, "Error %d: %s on [%s]\n",
13231             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
13232             zQuery);
13233     goto end_data_xfer;
13234   }
13235   for(k=0; k<2; k++){
13236     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13237       for(i=0; i<n; i++){
13238         switch( sqlite3_column_type(pQuery, i) ){
13239           case SQLITE_NULL: {
13240             sqlite3_bind_null(pInsert, i+1);
13241             break;
13242           }
13243           case SQLITE_INTEGER: {
13244             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
13245             break;
13246           }
13247           case SQLITE_FLOAT: {
13248             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
13249             break;
13250           }
13251           case SQLITE_TEXT: {
13252             sqlite3_bind_text(pInsert, i+1,
13253                              (const char*)sqlite3_column_text(pQuery,i),
13254                              -1, SQLITE_STATIC);
13255             break;
13256           }
13257           case SQLITE_BLOB: {
13258             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
13259                                             sqlite3_column_bytes(pQuery,i),
13260                                             SQLITE_STATIC);
13261             break;
13262           }
13263         }
13264       } /* End for */
13265       rc = sqlite3_step(pInsert);
13266       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
13267         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
13268                         sqlite3_errmsg(newDb));
13269       }
13270       sqlite3_reset(pInsert);
13271       cnt++;
13272       if( (cnt%spinRate)==0 ){
13273         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
13274         fflush(stdout);
13275       }
13276     } /* End while */
13277     if( rc==SQLITE_DONE ) break;
13278     sqlite3_finalize(pQuery);
13279     sqlite3_free(zQuery);
13280     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
13281                              zTable);
13282     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13283     if( rc ){
13284       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
13285       break;
13286     }
13287   } /* End for(k=0...) */
13288 
13289 end_data_xfer:
13290   sqlite3_finalize(pQuery);
13291   sqlite3_finalize(pInsert);
13292   sqlite3_free(zQuery);
13293   sqlite3_free(zInsert);
13294 }
13295 
13296 
13297 /*
13298 ** Try to transfer all rows of the schema that match zWhere.  For
13299 ** each row, invoke xForEach() on the object defined by that row.
13300 ** If an error is encountered while moving forward through the
13301 ** sqlite_master table, try again moving backwards.
13302 */
13303 static void tryToCloneSchema(
13304   ShellState *p,
13305   sqlite3 *newDb,
13306   const char *zWhere,
13307   void (*xForEach)(ShellState*,sqlite3*,const char*)
13308 ){
13309   sqlite3_stmt *pQuery = 0;
13310   char *zQuery = 0;
13311   int rc;
13312   const unsigned char *zName;
13313   const unsigned char *zSql;
13314   char *zErrMsg = 0;
13315 
13316   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13317                            " WHERE %s", zWhere);
13318   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13319   if( rc ){
13320     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13321                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13322                     zQuery);
13323     goto end_schema_xfer;
13324   }
13325   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13326     zName = sqlite3_column_text(pQuery, 0);
13327     zSql = sqlite3_column_text(pQuery, 1);
13328     printf("%s... ", zName); fflush(stdout);
13329     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13330     if( zErrMsg ){
13331       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13332       sqlite3_free(zErrMsg);
13333       zErrMsg = 0;
13334     }
13335     if( xForEach ){
13336       xForEach(p, newDb, (const char*)zName);
13337     }
13338     printf("done\n");
13339   }
13340   if( rc!=SQLITE_DONE ){
13341     sqlite3_finalize(pQuery);
13342     sqlite3_free(zQuery);
13343     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13344                              " WHERE %s ORDER BY rowid DESC", zWhere);
13345     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13346     if( rc ){
13347       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13348                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13349                       zQuery);
13350       goto end_schema_xfer;
13351     }
13352     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13353       zName = sqlite3_column_text(pQuery, 0);
13354       zSql = sqlite3_column_text(pQuery, 1);
13355       printf("%s... ", zName); fflush(stdout);
13356       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13357       if( zErrMsg ){
13358         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13359         sqlite3_free(zErrMsg);
13360         zErrMsg = 0;
13361       }
13362       if( xForEach ){
13363         xForEach(p, newDb, (const char*)zName);
13364       }
13365       printf("done\n");
13366     }
13367   }
13368 end_schema_xfer:
13369   sqlite3_finalize(pQuery);
13370   sqlite3_free(zQuery);
13371 }
13372 
13373 /*
13374 ** Open a new database file named "zNewDb".  Try to recover as much information
13375 ** as possible out of the main database (which might be corrupt) and write it
13376 ** into zNewDb.
13377 */
13378 static void tryToClone(ShellState *p, const char *zNewDb){
13379   int rc;
13380   sqlite3 *newDb = 0;
13381   if( access(zNewDb,0)==0 ){
13382     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
13383     return;
13384   }
13385   rc = sqlite3_open(zNewDb, &newDb);
13386   if( rc ){
13387     utf8_printf(stderr, "Cannot create output database: %s\n",
13388             sqlite3_errmsg(newDb));
13389   }else{
13390     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
13391     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
13392     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
13393     tryToCloneSchema(p, newDb, "type!='table'", 0);
13394     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
13395     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13396   }
13397   close_db(newDb);
13398 }
13399 
13400 /*
13401 ** Change the output file back to stdout.
13402 **
13403 ** If the p->doXdgOpen flag is set, that means the output was being
13404 ** redirected to a temporary file named by p->zTempFile.  In that case,
13405 ** launch start/open/xdg-open on that temporary file.
13406 */
13407 static void output_reset(ShellState *p){
13408   if( p->outfile[0]=='|' ){
13409 #ifndef SQLITE_OMIT_POPEN
13410     pclose(p->out);
13411 #endif
13412   }else{
13413     output_file_close(p->out);
13414 #ifndef SQLITE_NOHAVE_SYSTEM
13415     if( p->doXdgOpen ){
13416       const char *zXdgOpenCmd =
13417 #if defined(_WIN32)
13418       "start";
13419 #elif defined(__APPLE__)
13420       "open";
13421 #else
13422       "xdg-open";
13423 #endif
13424       char *zCmd;
13425       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
13426       if( system(zCmd) ){
13427         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
13428       }
13429       sqlite3_free(zCmd);
13430       outputModePop(p);
13431       p->doXdgOpen = 0;
13432       sqlite3_sleep(100);
13433     }
13434 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
13435   }
13436   p->outfile[0] = 0;
13437   p->out = stdout;
13438 }
13439 
13440 /*
13441 ** Run an SQL command and return the single integer result.
13442 */
13443 static int db_int(ShellState *p, const char *zSql){
13444   sqlite3_stmt *pStmt;
13445   int res = 0;
13446   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13447   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
13448     res = sqlite3_column_int(pStmt,0);
13449   }
13450   sqlite3_finalize(pStmt);
13451   return res;
13452 }
13453 
13454 /*
13455 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
13456 */
13457 static unsigned int get2byteInt(unsigned char *a){
13458   return (a[0]<<8) + a[1];
13459 }
13460 static unsigned int get4byteInt(unsigned char *a){
13461   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
13462 }
13463 
13464 /*
13465 ** Implementation of the ".info" command.
13466 **
13467 ** Return 1 on error, 2 to exit, and 0 otherwise.
13468 */
13469 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
13470   static const struct { const char *zName; int ofst; } aField[] = {
13471      { "file change counter:",  24  },
13472      { "database page count:",  28  },
13473      { "freelist page count:",  36  },
13474      { "schema cookie:",        40  },
13475      { "schema format:",        44  },
13476      { "default cache size:",   48  },
13477      { "autovacuum top root:",  52  },
13478      { "incremental vacuum:",   64  },
13479      { "text encoding:",        56  },
13480      { "user version:",         60  },
13481      { "application id:",       68  },
13482      { "software version:",     96  },
13483   };
13484   static const struct { const char *zName; const char *zSql; } aQuery[] = {
13485      { "number of tables:",
13486        "SELECT count(*) FROM %s WHERE type='table'" },
13487      { "number of indexes:",
13488        "SELECT count(*) FROM %s WHERE type='index'" },
13489      { "number of triggers:",
13490        "SELECT count(*) FROM %s WHERE type='trigger'" },
13491      { "number of views:",
13492        "SELECT count(*) FROM %s WHERE type='view'" },
13493      { "schema size:",
13494        "SELECT total(length(sql)) FROM %s" },
13495   };
13496   int i, rc;
13497   unsigned iDataVersion;
13498   char *zSchemaTab;
13499   char *zDb = nArg>=2 ? azArg[1] : "main";
13500   sqlite3_stmt *pStmt = 0;
13501   unsigned char aHdr[100];
13502   open_db(p, 0);
13503   if( p->db==0 ) return 1;
13504   rc = sqlite3_prepare_v2(p->db,
13505              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
13506              -1, &pStmt, 0);
13507   if( rc ){
13508     if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
13509       utf8_printf(stderr, "the \".dbinfo\" command requires the "
13510                           "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
13511     }else{
13512       utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
13513     }
13514     sqlite3_finalize(pStmt);
13515     return 1;
13516   }
13517   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
13518   if( sqlite3_step(pStmt)==SQLITE_ROW
13519    && sqlite3_column_bytes(pStmt,0)>100
13520   ){
13521     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
13522     sqlite3_finalize(pStmt);
13523   }else{
13524     raw_printf(stderr, "unable to read database header\n");
13525     sqlite3_finalize(pStmt);
13526     return 1;
13527   }
13528   i = get2byteInt(aHdr+16);
13529   if( i==1 ) i = 65536;
13530   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
13531   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
13532   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
13533   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
13534   for(i=0; i<ArraySize(aField); i++){
13535     int ofst = aField[i].ofst;
13536     unsigned int val = get4byteInt(aHdr + ofst);
13537     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
13538     switch( ofst ){
13539       case 56: {
13540         if( val==1 ) raw_printf(p->out, " (utf8)");
13541         if( val==2 ) raw_printf(p->out, " (utf16le)");
13542         if( val==3 ) raw_printf(p->out, " (utf16be)");
13543       }
13544     }
13545     raw_printf(p->out, "\n");
13546   }
13547   if( zDb==0 ){
13548     zSchemaTab = sqlite3_mprintf("main.sqlite_master");
13549   }else if( strcmp(zDb,"temp")==0 ){
13550     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
13551   }else{
13552     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
13553   }
13554   for(i=0; i<ArraySize(aQuery); i++){
13555     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
13556     int val = db_int(p, zSql);
13557     sqlite3_free(zSql);
13558     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
13559   }
13560   sqlite3_free(zSchemaTab);
13561   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
13562   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
13563   return 0;
13564 }
13565 
13566 /*
13567 ** Print the current sqlite3_errmsg() value to stderr and return 1.
13568 */
13569 static int shellDatabaseError(sqlite3 *db){
13570   const char *zErr = sqlite3_errmsg(db);
13571   utf8_printf(stderr, "Error: %s\n", zErr);
13572   return 1;
13573 }
13574 
13575 /*
13576 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
13577 ** if they match and FALSE (0) if they do not match.
13578 **
13579 ** Globbing rules:
13580 **
13581 **      '*'       Matches any sequence of zero or more characters.
13582 **
13583 **      '?'       Matches exactly one character.
13584 **
13585 **     [...]      Matches one character from the enclosed list of
13586 **                characters.
13587 **
13588 **     [^...]     Matches one character not in the enclosed list.
13589 **
13590 **      '#'       Matches any sequence of one or more digits with an
13591 **                optional + or - sign in front
13592 **
13593 **      ' '       Any span of whitespace matches any other span of
13594 **                whitespace.
13595 **
13596 ** Extra whitespace at the end of z[] is ignored.
13597 */
13598 static int testcase_glob(const char *zGlob, const char *z){
13599   int c, c2;
13600   int invert;
13601   int seen;
13602 
13603   while( (c = (*(zGlob++)))!=0 ){
13604     if( IsSpace(c) ){
13605       if( !IsSpace(*z) ) return 0;
13606       while( IsSpace(*zGlob) ) zGlob++;
13607       while( IsSpace(*z) ) z++;
13608     }else if( c=='*' ){
13609       while( (c=(*(zGlob++))) == '*' || c=='?' ){
13610         if( c=='?' && (*(z++))==0 ) return 0;
13611       }
13612       if( c==0 ){
13613         return 1;
13614       }else if( c=='[' ){
13615         while( *z && testcase_glob(zGlob-1,z)==0 ){
13616           z++;
13617         }
13618         return (*z)!=0;
13619       }
13620       while( (c2 = (*(z++)))!=0 ){
13621         while( c2!=c ){
13622           c2 = *(z++);
13623           if( c2==0 ) return 0;
13624         }
13625         if( testcase_glob(zGlob,z) ) return 1;
13626       }
13627       return 0;
13628     }else if( c=='?' ){
13629       if( (*(z++))==0 ) return 0;
13630     }else if( c=='[' ){
13631       int prior_c = 0;
13632       seen = 0;
13633       invert = 0;
13634       c = *(z++);
13635       if( c==0 ) return 0;
13636       c2 = *(zGlob++);
13637       if( c2=='^' ){
13638         invert = 1;
13639         c2 = *(zGlob++);
13640       }
13641       if( c2==']' ){
13642         if( c==']' ) seen = 1;
13643         c2 = *(zGlob++);
13644       }
13645       while( c2 && c2!=']' ){
13646         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
13647           c2 = *(zGlob++);
13648           if( c>=prior_c && c<=c2 ) seen = 1;
13649           prior_c = 0;
13650         }else{
13651           if( c==c2 ){
13652             seen = 1;
13653           }
13654           prior_c = c2;
13655         }
13656         c2 = *(zGlob++);
13657       }
13658       if( c2==0 || (seen ^ invert)==0 ) return 0;
13659     }else if( c=='#' ){
13660       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
13661       if( !IsDigit(z[0]) ) return 0;
13662       z++;
13663       while( IsDigit(z[0]) ){ z++; }
13664     }else{
13665       if( c!=(*(z++)) ) return 0;
13666     }
13667   }
13668   while( IsSpace(*z) ){ z++; }
13669   return *z==0;
13670 }
13671 
13672 
13673 /*
13674 ** Compare the string as a command-line option with either one or two
13675 ** initial "-" characters.
13676 */
13677 static int optionMatch(const char *zStr, const char *zOpt){
13678   if( zStr[0]!='-' ) return 0;
13679   zStr++;
13680   if( zStr[0]=='-' ) zStr++;
13681   return strcmp(zStr, zOpt)==0;
13682 }
13683 
13684 /*
13685 ** Delete a file.
13686 */
13687 int shellDeleteFile(const char *zFilename){
13688   int rc;
13689 #ifdef _WIN32
13690   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
13691   rc = _wunlink(z);
13692   sqlite3_free(z);
13693 #else
13694   rc = unlink(zFilename);
13695 #endif
13696   return rc;
13697 }
13698 
13699 /*
13700 ** Try to delete the temporary file (if there is one) and free the
13701 ** memory used to hold the name of the temp file.
13702 */
13703 static void clearTempFile(ShellState *p){
13704   if( p->zTempFile==0 ) return;
13705   if( p->doXdgOpen ) return;
13706   if( shellDeleteFile(p->zTempFile) ) return;
13707   sqlite3_free(p->zTempFile);
13708   p->zTempFile = 0;
13709 }
13710 
13711 /*
13712 ** Create a new temp file name with the given suffix.
13713 */
13714 static void newTempFile(ShellState *p, const char *zSuffix){
13715   clearTempFile(p);
13716   sqlite3_free(p->zTempFile);
13717   p->zTempFile = 0;
13718   if( p->db ){
13719     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
13720   }
13721   if( p->zTempFile==0 ){
13722     sqlite3_uint64 r;
13723     sqlite3_randomness(sizeof(r), &r);
13724     p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
13725   }else{
13726     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
13727   }
13728   if( p->zTempFile==0 ){
13729     raw_printf(stderr, "out of memory\n");
13730     exit(1);
13731   }
13732 }
13733 
13734 
13735 /*
13736 ** The implementation of SQL scalar function fkey_collate_clause(), used
13737 ** by the ".lint fkey-indexes" command. This scalar function is always
13738 ** called with four arguments - the parent table name, the parent column name,
13739 ** the child table name and the child column name.
13740 **
13741 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
13742 **
13743 ** If either of the named tables or columns do not exist, this function
13744 ** returns an empty string. An empty string is also returned if both tables
13745 ** and columns exist but have the same default collation sequence. Or,
13746 ** if both exist but the default collation sequences are different, this
13747 ** function returns the string " COLLATE <parent-collation>", where
13748 ** <parent-collation> is the default collation sequence of the parent column.
13749 */
13750 static void shellFkeyCollateClause(
13751   sqlite3_context *pCtx,
13752   int nVal,
13753   sqlite3_value **apVal
13754 ){
13755   sqlite3 *db = sqlite3_context_db_handle(pCtx);
13756   const char *zParent;
13757   const char *zParentCol;
13758   const char *zParentSeq;
13759   const char *zChild;
13760   const char *zChildCol;
13761   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
13762   int rc;
13763 
13764   assert( nVal==4 );
13765   zParent = (const char*)sqlite3_value_text(apVal[0]);
13766   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
13767   zChild = (const char*)sqlite3_value_text(apVal[2]);
13768   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
13769 
13770   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
13771   rc = sqlite3_table_column_metadata(
13772       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
13773   );
13774   if( rc==SQLITE_OK ){
13775     rc = sqlite3_table_column_metadata(
13776         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
13777     );
13778   }
13779 
13780   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
13781     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
13782     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
13783     sqlite3_free(z);
13784   }
13785 }
13786 
13787 
13788 /*
13789 ** The implementation of dot-command ".lint fkey-indexes".
13790 */
13791 static int lintFkeyIndexes(
13792   ShellState *pState,             /* Current shell tool state */
13793   char **azArg,                   /* Array of arguments passed to dot command */
13794   int nArg                        /* Number of entries in azArg[] */
13795 ){
13796   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
13797   FILE *out = pState->out;        /* Stream to write non-error output to */
13798   int bVerbose = 0;               /* If -verbose is present */
13799   int bGroupByParent = 0;         /* If -groupbyparent is present */
13800   int i;                          /* To iterate through azArg[] */
13801   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
13802   int rc;                         /* Return code */
13803   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
13804 
13805   /*
13806   ** This SELECT statement returns one row for each foreign key constraint
13807   ** in the schema of the main database. The column values are:
13808   **
13809   ** 0. The text of an SQL statement similar to:
13810   **
13811   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
13812   **
13813   **    This SELECT is similar to the one that the foreign keys implementation
13814   **    needs to run internally on child tables. If there is an index that can
13815   **    be used to optimize this query, then it can also be used by the FK
13816   **    implementation to optimize DELETE or UPDATE statements on the parent
13817   **    table.
13818   **
13819   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
13820   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
13821   **    contains an index that can be used to optimize the query.
13822   **
13823   ** 2. Human readable text that describes the child table and columns. e.g.
13824   **
13825   **       "child_table(child_key1, child_key2)"
13826   **
13827   ** 3. Human readable text that describes the parent table and columns. e.g.
13828   **
13829   **       "parent_table(parent_key1, parent_key2)"
13830   **
13831   ** 4. A full CREATE INDEX statement for an index that could be used to
13832   **    optimize DELETE or UPDATE statements on the parent table. e.g.
13833   **
13834   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
13835   **
13836   ** 5. The name of the parent table.
13837   **
13838   ** These six values are used by the C logic below to generate the report.
13839   */
13840   const char *zSql =
13841   "SELECT "
13842     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
13843     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
13844     "  || fkey_collate_clause("
13845     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
13846     ", "
13847     "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
13848     "  || group_concat('*=?', ' AND ') || ')'"
13849     ", "
13850     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
13851     ", "
13852     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
13853     ", "
13854     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
13855     "  || ' ON ' || quote(s.name) || '('"
13856     "  || group_concat(quote(f.[from]) ||"
13857     "        fkey_collate_clause("
13858     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
13859     "  || ');'"
13860     ", "
13861     "     f.[table] "
13862     "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
13863     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
13864     "GROUP BY s.name, f.id "
13865     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
13866   ;
13867   const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
13868 
13869   for(i=2; i<nArg; i++){
13870     int n = strlen30(azArg[i]);
13871     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
13872       bVerbose = 1;
13873     }
13874     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
13875       bGroupByParent = 1;
13876       zIndent = "    ";
13877     }
13878     else{
13879       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
13880           azArg[0], azArg[1]
13881       );
13882       return SQLITE_ERROR;
13883     }
13884   }
13885 
13886   /* Register the fkey_collate_clause() SQL function */
13887   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
13888       0, shellFkeyCollateClause, 0, 0
13889   );
13890 
13891 
13892   if( rc==SQLITE_OK ){
13893     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
13894   }
13895   if( rc==SQLITE_OK ){
13896     sqlite3_bind_int(pSql, 1, bGroupByParent);
13897   }
13898 
13899   if( rc==SQLITE_OK ){
13900     int rc2;
13901     char *zPrev = 0;
13902     while( SQLITE_ROW==sqlite3_step(pSql) ){
13903       int res = -1;
13904       sqlite3_stmt *pExplain = 0;
13905       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
13906       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
13907       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
13908       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
13909       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
13910       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
13911 
13912       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13913       if( rc!=SQLITE_OK ) break;
13914       if( SQLITE_ROW==sqlite3_step(pExplain) ){
13915         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
13916         res = (
13917               0==sqlite3_strglob(zGlob, zPlan)
13918            || 0==sqlite3_strglob(zGlobIPK, zPlan)
13919         );
13920       }
13921       rc = sqlite3_finalize(pExplain);
13922       if( rc!=SQLITE_OK ) break;
13923 
13924       if( res<0 ){
13925         raw_printf(stderr, "Error: internal error");
13926         break;
13927       }else{
13928         if( bGroupByParent
13929         && (bVerbose || res==0)
13930         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
13931         ){
13932           raw_printf(out, "-- Parent table %s\n", zParent);
13933           sqlite3_free(zPrev);
13934           zPrev = sqlite3_mprintf("%s", zParent);
13935         }
13936 
13937         if( res==0 ){
13938           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
13939         }else if( bVerbose ){
13940           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
13941               zIndent, zFrom, zTarget
13942           );
13943         }
13944       }
13945     }
13946     sqlite3_free(zPrev);
13947 
13948     if( rc!=SQLITE_OK ){
13949       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
13950     }
13951 
13952     rc2 = sqlite3_finalize(pSql);
13953     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
13954       rc = rc2;
13955       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
13956     }
13957   }else{
13958     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
13959   }
13960 
13961   return rc;
13962 }
13963 
13964 /*
13965 ** Implementation of ".lint" dot command.
13966 */
13967 static int lintDotCommand(
13968   ShellState *pState,             /* Current shell tool state */
13969   char **azArg,                   /* Array of arguments passed to dot command */
13970   int nArg                        /* Number of entries in azArg[] */
13971 ){
13972   int n;
13973   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
13974   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
13975   return lintFkeyIndexes(pState, azArg, nArg);
13976 
13977  usage:
13978   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
13979   raw_printf(stderr, "Where sub-commands are:\n");
13980   raw_printf(stderr, "    fkey-indexes\n");
13981   return SQLITE_ERROR;
13982 }
13983 
13984 #if !defined SQLITE_OMIT_VIRTUALTABLE
13985 static void shellPrepare(
13986   sqlite3 *db,
13987   int *pRc,
13988   const char *zSql,
13989   sqlite3_stmt **ppStmt
13990 ){
13991   *ppStmt = 0;
13992   if( *pRc==SQLITE_OK ){
13993     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
13994     if( rc!=SQLITE_OK ){
13995       raw_printf(stderr, "sql error: %s (%d)\n",
13996           sqlite3_errmsg(db), sqlite3_errcode(db)
13997       );
13998       *pRc = rc;
13999     }
14000   }
14001 }
14002 
14003 /*
14004 ** Create a prepared statement using printf-style arguments for the SQL.
14005 **
14006 ** This routine is could be marked "static".  But it is not always used,
14007 ** depending on compile-time options.  By omitting the "static", we avoid
14008 ** nuisance compiler warnings about "defined but not used".
14009 */
14010 void shellPreparePrintf(
14011   sqlite3 *db,
14012   int *pRc,
14013   sqlite3_stmt **ppStmt,
14014   const char *zFmt,
14015   ...
14016 ){
14017   *ppStmt = 0;
14018   if( *pRc==SQLITE_OK ){
14019     va_list ap;
14020     char *z;
14021     va_start(ap, zFmt);
14022     z = sqlite3_vmprintf(zFmt, ap);
14023     va_end(ap);
14024     if( z==0 ){
14025       *pRc = SQLITE_NOMEM;
14026     }else{
14027       shellPrepare(db, pRc, z, ppStmt);
14028       sqlite3_free(z);
14029     }
14030   }
14031 }
14032 
14033 /* Finalize the prepared statement created using shellPreparePrintf().
14034 **
14035 ** This routine is could be marked "static".  But it is not always used,
14036 ** depending on compile-time options.  By omitting the "static", we avoid
14037 ** nuisance compiler warnings about "defined but not used".
14038 */
14039 void shellFinalize(
14040   int *pRc,
14041   sqlite3_stmt *pStmt
14042 ){
14043   if( pStmt ){
14044     sqlite3 *db = sqlite3_db_handle(pStmt);
14045     int rc = sqlite3_finalize(pStmt);
14046     if( *pRc==SQLITE_OK ){
14047       if( rc!=SQLITE_OK ){
14048         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14049       }
14050       *pRc = rc;
14051     }
14052   }
14053 }
14054 
14055 /* Reset the prepared statement created using shellPreparePrintf().
14056 **
14057 ** This routine is could be marked "static".  But it is not always used,
14058 ** depending on compile-time options.  By omitting the "static", we avoid
14059 ** nuisance compiler warnings about "defined but not used".
14060 */
14061 void shellReset(
14062   int *pRc,
14063   sqlite3_stmt *pStmt
14064 ){
14065   int rc = sqlite3_reset(pStmt);
14066   if( *pRc==SQLITE_OK ){
14067     if( rc!=SQLITE_OK ){
14068       sqlite3 *db = sqlite3_db_handle(pStmt);
14069       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14070     }
14071     *pRc = rc;
14072   }
14073 }
14074 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
14075 
14076 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
14077 /******************************************************************************
14078 ** The ".archive" or ".ar" command.
14079 */
14080 /*
14081 ** Structure representing a single ".ar" command.
14082 */
14083 typedef struct ArCommand ArCommand;
14084 struct ArCommand {
14085   u8 eCmd;                        /* An AR_CMD_* value */
14086   u8 bVerbose;                    /* True if --verbose */
14087   u8 bZip;                        /* True if the archive is a ZIP */
14088   u8 bDryRun;                     /* True if --dry-run */
14089   u8 bAppend;                     /* True if --append */
14090   u8 fromCmdLine;                 /* Run from -A instead of .archive */
14091   int nArg;                       /* Number of command arguments */
14092   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
14093   const char *zFile;              /* --file argument, or NULL */
14094   const char *zDir;               /* --directory argument, or NULL */
14095   char **azArg;                   /* Array of command arguments */
14096   ShellState *p;                  /* Shell state */
14097   sqlite3 *db;                    /* Database containing the archive */
14098 };
14099 
14100 /*
14101 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
14102 */
14103 static int arUsage(FILE *f){
14104   showHelp(f,"archive");
14105   return SQLITE_ERROR;
14106 }
14107 
14108 /*
14109 ** Print an error message for the .ar command to stderr and return
14110 ** SQLITE_ERROR.
14111 */
14112 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
14113   va_list ap;
14114   char *z;
14115   va_start(ap, zFmt);
14116   z = sqlite3_vmprintf(zFmt, ap);
14117   va_end(ap);
14118   utf8_printf(stderr, "Error: %s\n", z);
14119   if( pAr->fromCmdLine ){
14120     utf8_printf(stderr, "Use \"-A\" for more help\n");
14121   }else{
14122     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
14123   }
14124   sqlite3_free(z);
14125   return SQLITE_ERROR;
14126 }
14127 
14128 /*
14129 ** Values for ArCommand.eCmd.
14130 */
14131 #define AR_CMD_CREATE       1
14132 #define AR_CMD_UPDATE       2
14133 #define AR_CMD_INSERT       3
14134 #define AR_CMD_EXTRACT      4
14135 #define AR_CMD_LIST         5
14136 #define AR_CMD_HELP         6
14137 
14138 /*
14139 ** Other (non-command) switches.
14140 */
14141 #define AR_SWITCH_VERBOSE     7
14142 #define AR_SWITCH_FILE        8
14143 #define AR_SWITCH_DIRECTORY   9
14144 #define AR_SWITCH_APPEND     10
14145 #define AR_SWITCH_DRYRUN     11
14146 
14147 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
14148   switch( eSwitch ){
14149     case AR_CMD_CREATE:
14150     case AR_CMD_EXTRACT:
14151     case AR_CMD_LIST:
14152     case AR_CMD_UPDATE:
14153     case AR_CMD_INSERT:
14154     case AR_CMD_HELP:
14155       if( pAr->eCmd ){
14156         return arErrorMsg(pAr, "multiple command options");
14157       }
14158       pAr->eCmd = eSwitch;
14159       break;
14160 
14161     case AR_SWITCH_DRYRUN:
14162       pAr->bDryRun = 1;
14163       break;
14164     case AR_SWITCH_VERBOSE:
14165       pAr->bVerbose = 1;
14166       break;
14167     case AR_SWITCH_APPEND:
14168       pAr->bAppend = 1;
14169       /* Fall thru into --file */
14170     case AR_SWITCH_FILE:
14171       pAr->zFile = zArg;
14172       break;
14173     case AR_SWITCH_DIRECTORY:
14174       pAr->zDir = zArg;
14175       break;
14176   }
14177 
14178   return SQLITE_OK;
14179 }
14180 
14181 /*
14182 ** Parse the command line for an ".ar" command. The results are written into
14183 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
14184 ** successfully, otherwise an error message is written to stderr and
14185 ** SQLITE_ERROR returned.
14186 */
14187 static int arParseCommand(
14188   char **azArg,                   /* Array of arguments passed to dot command */
14189   int nArg,                       /* Number of entries in azArg[] */
14190   ArCommand *pAr                  /* Populate this object */
14191 ){
14192   struct ArSwitch {
14193     const char *zLong;
14194     char cShort;
14195     u8 eSwitch;
14196     u8 bArg;
14197   } aSwitch[] = {
14198     { "create",    'c', AR_CMD_CREATE,       0 },
14199     { "extract",   'x', AR_CMD_EXTRACT,      0 },
14200     { "insert",    'i', AR_CMD_INSERT,       0 },
14201     { "list",      't', AR_CMD_LIST,         0 },
14202     { "update",    'u', AR_CMD_UPDATE,       0 },
14203     { "help",      'h', AR_CMD_HELP,         0 },
14204     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
14205     { "file",      'f', AR_SWITCH_FILE,      1 },
14206     { "append",    'a', AR_SWITCH_APPEND,    1 },
14207     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
14208     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
14209   };
14210   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
14211   struct ArSwitch *pEnd = &aSwitch[nSwitch];
14212 
14213   if( nArg<=1 ){
14214     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
14215     return arUsage(stderr);
14216   }else{
14217     char *z = azArg[1];
14218     if( z[0]!='-' ){
14219       /* Traditional style [tar] invocation */
14220       int i;
14221       int iArg = 2;
14222       for(i=0; z[i]; i++){
14223         const char *zArg = 0;
14224         struct ArSwitch *pOpt;
14225         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14226           if( z[i]==pOpt->cShort ) break;
14227         }
14228         if( pOpt==pEnd ){
14229           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14230         }
14231         if( pOpt->bArg ){
14232           if( iArg>=nArg ){
14233             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
14234           }
14235           zArg = azArg[iArg++];
14236         }
14237         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14238       }
14239       pAr->nArg = nArg-iArg;
14240       if( pAr->nArg>0 ){
14241         pAr->azArg = &azArg[iArg];
14242       }
14243     }else{
14244       /* Non-traditional invocation */
14245       int iArg;
14246       for(iArg=1; iArg<nArg; iArg++){
14247         int n;
14248         z = azArg[iArg];
14249         if( z[0]!='-' ){
14250           /* All remaining command line words are command arguments. */
14251           pAr->azArg = &azArg[iArg];
14252           pAr->nArg = nArg-iArg;
14253           break;
14254         }
14255         n = strlen30(z);
14256 
14257         if( z[1]!='-' ){
14258           int i;
14259           /* One or more short options */
14260           for(i=1; i<n; i++){
14261             const char *zArg = 0;
14262             struct ArSwitch *pOpt;
14263             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14264               if( z[i]==pOpt->cShort ) break;
14265             }
14266             if( pOpt==pEnd ){
14267               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14268             }
14269             if( pOpt->bArg ){
14270               if( i<(n-1) ){
14271                 zArg = &z[i+1];
14272                 i = n;
14273               }else{
14274                 if( iArg>=(nArg-1) ){
14275                   return arErrorMsg(pAr, "option requires an argument: %c",
14276                                     z[i]);
14277                 }
14278                 zArg = azArg[++iArg];
14279               }
14280             }
14281             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14282           }
14283         }else if( z[2]=='\0' ){
14284           /* A -- option, indicating that all remaining command line words
14285           ** are command arguments.  */
14286           pAr->azArg = &azArg[iArg+1];
14287           pAr->nArg = nArg-iArg-1;
14288           break;
14289         }else{
14290           /* A long option */
14291           const char *zArg = 0;             /* Argument for option, if any */
14292           struct ArSwitch *pMatch = 0;      /* Matching option */
14293           struct ArSwitch *pOpt;            /* Iterator */
14294           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14295             const char *zLong = pOpt->zLong;
14296             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
14297               if( pMatch ){
14298                 return arErrorMsg(pAr, "ambiguous option: %s",z);
14299               }else{
14300                 pMatch = pOpt;
14301               }
14302             }
14303           }
14304 
14305           if( pMatch==0 ){
14306             return arErrorMsg(pAr, "unrecognized option: %s", z);
14307           }
14308           if( pMatch->bArg ){
14309             if( iArg>=(nArg-1) ){
14310               return arErrorMsg(pAr, "option requires an argument: %s", z);
14311             }
14312             zArg = azArg[++iArg];
14313           }
14314           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
14315         }
14316       }
14317     }
14318   }
14319 
14320   return SQLITE_OK;
14321 }
14322 
14323 /*
14324 ** This function assumes that all arguments within the ArCommand.azArg[]
14325 ** array refer to archive members, as for the --extract or --list commands.
14326 ** It checks that each of them are present. If any specified file is not
14327 ** present in the archive, an error is printed to stderr and an error
14328 ** code returned. Otherwise, if all specified arguments are present in
14329 ** the archive, SQLITE_OK is returned.
14330 **
14331 ** This function strips any trailing '/' characters from each argument.
14332 ** This is consistent with the way the [tar] command seems to work on
14333 ** Linux.
14334 */
14335 static int arCheckEntries(ArCommand *pAr){
14336   int rc = SQLITE_OK;
14337   if( pAr->nArg ){
14338     int i, j;
14339     sqlite3_stmt *pTest = 0;
14340 
14341     shellPreparePrintf(pAr->db, &rc, &pTest,
14342         "SELECT name FROM %s WHERE name=$name",
14343         pAr->zSrcTable
14344     );
14345     j = sqlite3_bind_parameter_index(pTest, "$name");
14346     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14347       char *z = pAr->azArg[i];
14348       int n = strlen30(z);
14349       int bOk = 0;
14350       while( n>0 && z[n-1]=='/' ) n--;
14351       z[n] = '\0';
14352       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
14353       if( SQLITE_ROW==sqlite3_step(pTest) ){
14354         bOk = 1;
14355       }
14356       shellReset(&rc, pTest);
14357       if( rc==SQLITE_OK && bOk==0 ){
14358         utf8_printf(stderr, "not found in archive: %s\n", z);
14359         rc = SQLITE_ERROR;
14360       }
14361     }
14362     shellFinalize(&rc, pTest);
14363   }
14364   return rc;
14365 }
14366 
14367 /*
14368 ** Format a WHERE clause that can be used against the "sqlar" table to
14369 ** identify all archive members that match the command arguments held
14370 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
14371 ** The caller is responsible for eventually calling sqlite3_free() on
14372 ** any non-NULL (*pzWhere) value.
14373 */
14374 static void arWhereClause(
14375   int *pRc,
14376   ArCommand *pAr,
14377   char **pzWhere                  /* OUT: New WHERE clause */
14378 ){
14379   char *zWhere = 0;
14380   if( *pRc==SQLITE_OK ){
14381     if( pAr->nArg==0 ){
14382       zWhere = sqlite3_mprintf("1");
14383     }else{
14384       int i;
14385       const char *zSep = "";
14386       for(i=0; i<pAr->nArg; i++){
14387         const char *z = pAr->azArg[i];
14388         zWhere = sqlite3_mprintf(
14389           "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
14390           zWhere, zSep, z, strlen30(z)+1, z
14391         );
14392         if( zWhere==0 ){
14393           *pRc = SQLITE_NOMEM;
14394           break;
14395         }
14396         zSep = " OR ";
14397       }
14398     }
14399   }
14400   *pzWhere = zWhere;
14401 }
14402 
14403 /*
14404 ** Implementation of .ar "lisT" command.
14405 */
14406 static int arListCommand(ArCommand *pAr){
14407   const char *zSql = "SELECT %s FROM %s WHERE %s";
14408   const char *azCols[] = {
14409     "name",
14410     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
14411   };
14412 
14413   char *zWhere = 0;
14414   sqlite3_stmt *pSql = 0;
14415   int rc;
14416 
14417   rc = arCheckEntries(pAr);
14418   arWhereClause(&rc, pAr, &zWhere);
14419 
14420   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
14421                      pAr->zSrcTable, zWhere);
14422   if( pAr->bDryRun ){
14423     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14424   }else{
14425     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14426       if( pAr->bVerbose ){
14427         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
14428             sqlite3_column_text(pSql, 0),
14429             sqlite3_column_int(pSql, 1),
14430             sqlite3_column_text(pSql, 2),
14431             sqlite3_column_text(pSql, 3)
14432         );
14433       }else{
14434         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14435       }
14436     }
14437   }
14438   shellFinalize(&rc, pSql);
14439   sqlite3_free(zWhere);
14440   return rc;
14441 }
14442 
14443 
14444 /*
14445 ** Implementation of .ar "eXtract" command.
14446 */
14447 static int arExtractCommand(ArCommand *pAr){
14448   const char *zSql1 =
14449     "SELECT "
14450     " ($dir || name),"
14451     " writefile(($dir || name), %s, mode, mtime) "
14452     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
14453     " AND name NOT GLOB '*..[/\\]*'";
14454 
14455   const char *azExtraArg[] = {
14456     "sqlar_uncompress(data, sz)",
14457     "data"
14458   };
14459 
14460   sqlite3_stmt *pSql = 0;
14461   int rc = SQLITE_OK;
14462   char *zDir = 0;
14463   char *zWhere = 0;
14464   int i, j;
14465 
14466   /* If arguments are specified, check that they actually exist within
14467   ** the archive before proceeding. And formulate a WHERE clause to
14468   ** match them.  */
14469   rc = arCheckEntries(pAr);
14470   arWhereClause(&rc, pAr, &zWhere);
14471 
14472   if( rc==SQLITE_OK ){
14473     if( pAr->zDir ){
14474       zDir = sqlite3_mprintf("%s/", pAr->zDir);
14475     }else{
14476       zDir = sqlite3_mprintf("");
14477     }
14478     if( zDir==0 ) rc = SQLITE_NOMEM;
14479   }
14480 
14481   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
14482       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
14483   );
14484 
14485   if( rc==SQLITE_OK ){
14486     j = sqlite3_bind_parameter_index(pSql, "$dir");
14487     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
14488 
14489     /* Run the SELECT statement twice. The first time, writefile() is called
14490     ** for all archive members that should be extracted. The second time,
14491     ** only for the directories. This is because the timestamps for
14492     ** extracted directories must be reset after they are populated (as
14493     ** populating them changes the timestamp).  */
14494     for(i=0; i<2; i++){
14495       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
14496       sqlite3_bind_int(pSql, j, i);
14497       if( pAr->bDryRun ){
14498         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14499       }else{
14500         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14501           if( i==0 && pAr->bVerbose ){
14502             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14503           }
14504         }
14505       }
14506       shellReset(&rc, pSql);
14507     }
14508     shellFinalize(&rc, pSql);
14509   }
14510 
14511   sqlite3_free(zDir);
14512   sqlite3_free(zWhere);
14513   return rc;
14514 }
14515 
14516 /*
14517 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
14518 */
14519 static int arExecSql(ArCommand *pAr, const char *zSql){
14520   int rc;
14521   if( pAr->bDryRun ){
14522     utf8_printf(pAr->p->out, "%s\n", zSql);
14523     rc = SQLITE_OK;
14524   }else{
14525     char *zErr = 0;
14526     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
14527     if( zErr ){
14528       utf8_printf(stdout, "ERROR: %s\n", zErr);
14529       sqlite3_free(zErr);
14530     }
14531   }
14532   return rc;
14533 }
14534 
14535 
14536 /*
14537 ** Implementation of .ar "create", "insert", and "update" commands.
14538 **
14539 **     create    ->     Create a new SQL archive
14540 **     insert    ->     Insert or reinsert all files listed
14541 **     update    ->     Insert files that have changed or that were not
14542 **                      previously in the archive
14543 **
14544 ** Create the "sqlar" table in the database if it does not already exist.
14545 ** Then add each file in the azFile[] array to the archive. Directories
14546 ** are added recursively. If argument bVerbose is non-zero, a message is
14547 ** printed on stdout for each file archived.
14548 **
14549 ** The create command is the same as update, except that it drops
14550 ** any existing "sqlar" table before beginning.  The "insert" command
14551 ** always overwrites every file named on the command-line, where as
14552 ** "update" only overwrites if the size or mtime or mode has changed.
14553 */
14554 static int arCreateOrUpdateCommand(
14555   ArCommand *pAr,                 /* Command arguments and options */
14556   int bUpdate,                    /* true for a --create. */
14557   int bOnlyIfChanged              /* Only update if file has changed */
14558 ){
14559   const char *zCreate =
14560       "CREATE TABLE IF NOT EXISTS sqlar(\n"
14561       "  name TEXT PRIMARY KEY,  -- name of the file\n"
14562       "  mode INT,               -- access permissions\n"
14563       "  mtime INT,              -- last modification time\n"
14564       "  sz INT,                 -- original file size\n"
14565       "  data BLOB               -- compressed content\n"
14566       ")";
14567   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
14568   const char *zInsertFmt[2] = {
14569      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
14570      "  SELECT\n"
14571      "    %s,\n"
14572      "    mode,\n"
14573      "    mtime,\n"
14574      "    CASE substr(lsmode(mode),1,1)\n"
14575      "      WHEN '-' THEN length(data)\n"
14576      "      WHEN 'd' THEN 0\n"
14577      "      ELSE -1 END,\n"
14578      "    sqlar_compress(data)\n"
14579      "  FROM fsdir(%Q,%Q) AS disk\n"
14580      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14581      ,
14582      "REPLACE INTO %s(name,mode,mtime,data)\n"
14583      "  SELECT\n"
14584      "    %s,\n"
14585      "    mode,\n"
14586      "    mtime,\n"
14587      "    data\n"
14588      "  FROM fsdir(%Q,%Q) AS disk\n"
14589      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14590   };
14591   int i;                          /* For iterating through azFile[] */
14592   int rc;                         /* Return code */
14593   const char *zTab = 0;           /* SQL table into which to insert */
14594   char *zSql;
14595   char zTemp[50];
14596   char *zExists = 0;
14597 
14598   arExecSql(pAr, "PRAGMA page_size=512");
14599   rc = arExecSql(pAr, "SAVEPOINT ar;");
14600   if( rc!=SQLITE_OK ) return rc;
14601   zTemp[0] = 0;
14602   if( pAr->bZip ){
14603     /* Initialize the zipfile virtual table, if necessary */
14604     if( pAr->zFile ){
14605       sqlite3_uint64 r;
14606       sqlite3_randomness(sizeof(r),&r);
14607       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
14608       zTab = zTemp;
14609       zSql = sqlite3_mprintf(
14610          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
14611          zTab, pAr->zFile
14612       );
14613       rc = arExecSql(pAr, zSql);
14614       sqlite3_free(zSql);
14615     }else{
14616       zTab = "zip";
14617     }
14618   }else{
14619     /* Initialize the table for an SQLAR */
14620     zTab = "sqlar";
14621     if( bUpdate==0 ){
14622       rc = arExecSql(pAr, zDrop);
14623       if( rc!=SQLITE_OK ) goto end_ar_transaction;
14624     }
14625     rc = arExecSql(pAr, zCreate);
14626   }
14627   if( bOnlyIfChanged ){
14628     zExists = sqlite3_mprintf(
14629       " AND NOT EXISTS("
14630           "SELECT 1 FROM %s AS mem"
14631           " WHERE mem.name=disk.name"
14632           " AND mem.mtime=disk.mtime"
14633           " AND mem.mode=disk.mode)", zTab);
14634   }else{
14635     zExists = sqlite3_mprintf("");
14636   }
14637   if( zExists==0 ) rc = SQLITE_NOMEM;
14638   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14639     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
14640         pAr->bVerbose ? "shell_putsnl(name)" : "name",
14641         pAr->azArg[i], pAr->zDir, zExists);
14642     rc = arExecSql(pAr, zSql2);
14643     sqlite3_free(zSql2);
14644   }
14645 end_ar_transaction:
14646   if( rc!=SQLITE_OK ){
14647     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
14648   }else{
14649     rc = arExecSql(pAr, "RELEASE ar;");
14650     if( pAr->bZip && pAr->zFile ){
14651       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
14652       arExecSql(pAr, zSql);
14653       sqlite3_free(zSql);
14654     }
14655   }
14656   sqlite3_free(zExists);
14657   return rc;
14658 }
14659 
14660 /*
14661 ** Implementation of ".ar" dot command.
14662 */
14663 static int arDotCommand(
14664   ShellState *pState,          /* Current shell tool state */
14665   int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
14666   char **azArg,                /* Array of arguments passed to dot command */
14667   int nArg                     /* Number of entries in azArg[] */
14668 ){
14669   ArCommand cmd;
14670   int rc;
14671   memset(&cmd, 0, sizeof(cmd));
14672   cmd.fromCmdLine = fromCmdLine;
14673   rc = arParseCommand(azArg, nArg, &cmd);
14674   if( rc==SQLITE_OK ){
14675     int eDbType = SHELL_OPEN_UNSPEC;
14676     cmd.p = pState;
14677     cmd.db = pState->db;
14678     if( cmd.zFile ){
14679       eDbType = deduceDatabaseType(cmd.zFile, 1);
14680     }else{
14681       eDbType = pState->openMode;
14682     }
14683     if( eDbType==SHELL_OPEN_ZIPFILE ){
14684       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
14685         if( cmd.zFile==0 ){
14686           cmd.zSrcTable = sqlite3_mprintf("zip");
14687         }else{
14688           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
14689         }
14690       }
14691       cmd.bZip = 1;
14692     }else if( cmd.zFile ){
14693       int flags;
14694       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
14695       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
14696            || cmd.eCmd==AR_CMD_UPDATE ){
14697         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
14698       }else{
14699         flags = SQLITE_OPEN_READONLY;
14700       }
14701       cmd.db = 0;
14702       if( cmd.bDryRun ){
14703         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
14704              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
14705       }
14706       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
14707              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
14708       if( rc!=SQLITE_OK ){
14709         utf8_printf(stderr, "cannot open file: %s (%s)\n",
14710             cmd.zFile, sqlite3_errmsg(cmd.db)
14711         );
14712         goto end_ar_command;
14713       }
14714       sqlite3_fileio_init(cmd.db, 0, 0);
14715       sqlite3_sqlar_init(cmd.db, 0, 0);
14716       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
14717                               shellPutsFunc, 0, 0);
14718 
14719     }
14720     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
14721       if( cmd.eCmd!=AR_CMD_CREATE
14722        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
14723       ){
14724         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
14725         rc = SQLITE_ERROR;
14726         goto end_ar_command;
14727       }
14728       cmd.zSrcTable = sqlite3_mprintf("sqlar");
14729     }
14730 
14731     switch( cmd.eCmd ){
14732       case AR_CMD_CREATE:
14733         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
14734         break;
14735 
14736       case AR_CMD_EXTRACT:
14737         rc = arExtractCommand(&cmd);
14738         break;
14739 
14740       case AR_CMD_LIST:
14741         rc = arListCommand(&cmd);
14742         break;
14743 
14744       case AR_CMD_HELP:
14745         arUsage(pState->out);
14746         break;
14747 
14748       case AR_CMD_INSERT:
14749         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
14750         break;
14751 
14752       default:
14753         assert( cmd.eCmd==AR_CMD_UPDATE );
14754         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
14755         break;
14756     }
14757   }
14758 end_ar_command:
14759   if( cmd.db!=pState->db ){
14760     close_db(cmd.db);
14761   }
14762   sqlite3_free(cmd.zSrcTable);
14763 
14764   return rc;
14765 }
14766 /* End of the ".archive" or ".ar" command logic
14767 *******************************************************************************/
14768 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
14769 
14770 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14771 /*
14772 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
14773 ** Otherwise, the SQL statement or statements in zSql are executed using
14774 ** database connection db and the error code written to *pRc before
14775 ** this function returns.
14776 */
14777 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
14778   int rc = *pRc;
14779   if( rc==SQLITE_OK ){
14780     char *zErr = 0;
14781     rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
14782     if( rc!=SQLITE_OK ){
14783       raw_printf(stderr, "SQL error: %s\n", zErr);
14784     }
14785     *pRc = rc;
14786   }
14787 }
14788 
14789 /*
14790 ** Like shellExec(), except that zFmt is a printf() style format string.
14791 */
14792 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
14793   char *z = 0;
14794   if( *pRc==SQLITE_OK ){
14795     va_list ap;
14796     va_start(ap, zFmt);
14797     z = sqlite3_vmprintf(zFmt, ap);
14798     va_end(ap);
14799     if( z==0 ){
14800       *pRc = SQLITE_NOMEM;
14801     }else{
14802       shellExec(db, pRc, z);
14803     }
14804     sqlite3_free(z);
14805   }
14806 }
14807 
14808 /*
14809 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
14810 ** Otherwise, an attempt is made to allocate, zero and return a pointer
14811 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
14812 ** to SQLITE_NOMEM and NULL returned.
14813 */
14814 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
14815   void *pRet = 0;
14816   if( *pRc==SQLITE_OK ){
14817     pRet = sqlite3_malloc64(nByte);
14818     if( pRet==0 ){
14819       *pRc = SQLITE_NOMEM;
14820     }else{
14821       memset(pRet, 0, nByte);
14822     }
14823   }
14824   return pRet;
14825 }
14826 
14827 /*
14828 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
14829 ** Otherwise, zFmt is treated as a printf() style string. The result of
14830 ** formatting it along with any trailing arguments is written into a
14831 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
14832 ** It is the responsibility of the caller to eventually free this buffer
14833 ** using a call to sqlite3_free().
14834 **
14835 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
14836 ** pointer returned.
14837 */
14838 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
14839   char *z = 0;
14840   if( *pRc==SQLITE_OK ){
14841     va_list ap;
14842     va_start(ap, zFmt);
14843     z = sqlite3_vmprintf(zFmt, ap);
14844     va_end(ap);
14845     if( z==0 ){
14846       *pRc = SQLITE_NOMEM;
14847     }
14848   }
14849   return z;
14850 }
14851 
14852 /*
14853 ** When running the ".recover" command, each output table, and the special
14854 ** orphaned row table if it is required, is represented by an instance
14855 ** of the following struct.
14856 */
14857 typedef struct RecoverTable RecoverTable;
14858 struct RecoverTable {
14859   char *zQuoted;                  /* Quoted version of table name */
14860   int nCol;                       /* Number of columns in table */
14861   char **azlCol;                  /* Array of column lists */
14862   int iPk;                        /* Index of IPK column */
14863 };
14864 
14865 /*
14866 ** Free a RecoverTable object allocated by recoverFindTable() or
14867 ** recoverOrphanTable().
14868 */
14869 static void recoverFreeTable(RecoverTable *pTab){
14870   if( pTab ){
14871     sqlite3_free(pTab->zQuoted);
14872     if( pTab->azlCol ){
14873       int i;
14874       for(i=0; i<=pTab->nCol; i++){
14875         sqlite3_free(pTab->azlCol[i]);
14876       }
14877       sqlite3_free(pTab->azlCol);
14878     }
14879     sqlite3_free(pTab);
14880   }
14881 }
14882 
14883 /*
14884 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
14885 ** Otherwise, it allocates and returns a RecoverTable object based on the
14886 ** final four arguments passed to this function. It is the responsibility
14887 ** of the caller to eventually free the returned object using
14888 ** recoverFreeTable().
14889 */
14890 static RecoverTable *recoverNewTable(
14891   int *pRc,                       /* IN/OUT: Error code */
14892   const char *zName,              /* Name of table */
14893   const char *zSql,               /* CREATE TABLE statement */
14894   int bIntkey,
14895   int nCol
14896 ){
14897   sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
14898   int rc = *pRc;
14899   RecoverTable *pTab = 0;
14900 
14901   pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
14902   if( rc==SQLITE_OK ){
14903     int nSqlCol = 0;
14904     int bSqlIntkey = 0;
14905     sqlite3_stmt *pStmt = 0;
14906 
14907     rc = sqlite3_open("", &dbtmp);
14908     if( rc==SQLITE_OK ){
14909       sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
14910                               shellIdQuote, 0, 0);
14911     }
14912     if( rc==SQLITE_OK ){
14913       rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
14914     }
14915     if( rc==SQLITE_OK ){
14916       rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
14917       if( rc==SQLITE_ERROR ){
14918         rc = SQLITE_OK;
14919         goto finished;
14920       }
14921     }
14922     shellPreparePrintf(dbtmp, &rc, &pStmt,
14923         "SELECT count(*) FROM pragma_table_info(%Q)", zName
14924     );
14925     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
14926       nSqlCol = sqlite3_column_int(pStmt, 0);
14927     }
14928     shellFinalize(&rc, pStmt);
14929 
14930     if( rc!=SQLITE_OK || nSqlCol<nCol ){
14931       goto finished;
14932     }
14933 
14934     shellPreparePrintf(dbtmp, &rc, &pStmt,
14935       "SELECT ("
14936       "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
14937       ") FROM sqlite_master WHERE name = %Q", zName
14938     );
14939     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
14940       bSqlIntkey = sqlite3_column_int(pStmt, 0);
14941     }
14942     shellFinalize(&rc, pStmt);
14943 
14944     if( bIntkey==bSqlIntkey ){
14945       int i;
14946       const char *zPk = "_rowid_";
14947       sqlite3_stmt *pPkFinder = 0;
14948 
14949       /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
14950       ** set zPk to the name of the PK column, and pTab->iPk to the index
14951       ** of the column, where columns are 0-numbered from left to right.
14952       ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
14953       ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
14954       pTab->iPk = -2;
14955       if( bIntkey ){
14956         shellPreparePrintf(dbtmp, &rc, &pPkFinder,
14957           "SELECT cid, name FROM pragma_table_info(%Q) "
14958           "  WHERE pk=1 AND type='integer' COLLATE nocase"
14959           "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
14960           , zName, zName
14961         );
14962         if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
14963           pTab->iPk = sqlite3_column_int(pPkFinder, 0);
14964           zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
14965         }
14966       }
14967 
14968       pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
14969       pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
14970       pTab->nCol = nSqlCol;
14971 
14972       if( bIntkey ){
14973         pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
14974       }else{
14975         pTab->azlCol[0] = shellMPrintf(&rc, "");
14976       }
14977       i = 1;
14978       shellPreparePrintf(dbtmp, &rc, &pStmt,
14979           "SELECT %Q || group_concat(shell_idquote(name), ', ') "
14980           "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
14981           "FROM pragma_table_info(%Q)",
14982           bIntkey ? ", " : "", pTab->iPk,
14983           bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
14984           zName
14985       );
14986       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
14987         const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
14988         pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
14989         i++;
14990       }
14991       shellFinalize(&rc, pStmt);
14992 
14993       shellFinalize(&rc, pPkFinder);
14994     }
14995   }
14996 
14997  finished:
14998   sqlite3_close(dbtmp);
14999   *pRc = rc;
15000   if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
15001     recoverFreeTable(pTab);
15002     pTab = 0;
15003   }
15004   return pTab;
15005 }
15006 
15007 /*
15008 ** This function is called to search the schema recovered from the
15009 ** sqlite_master table of the (possibly) corrupt database as part
15010 ** of a ".recover" command. Specifically, for a table with root page
15011 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
15012 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
15013 ** those.
15014 **
15015 ** If a table is found, a (RecoverTable*) object is returned. Or, if
15016 ** no such table is found, but bIntkey is false and iRoot is the
15017 ** root page of an index in the recovered schema, then (*pbNoop) is
15018 ** set to true and NULL returned. Or, if there is no such table or
15019 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
15020 ** the caller should write data to the orphans table.
15021 */
15022 static RecoverTable *recoverFindTable(
15023   ShellState *pState,             /* Shell state object */
15024   int *pRc,                       /* IN/OUT: Error code */
15025   int iRoot,                      /* Root page of table */
15026   int bIntkey,                    /* True for an intkey table */
15027   int nCol,                       /* Number of columns in table */
15028   int *pbNoop                     /* OUT: True if iRoot is root of index */
15029 ){
15030   sqlite3_stmt *pStmt = 0;
15031   RecoverTable *pRet = 0;
15032   int bNoop = 0;
15033   const char *zSql = 0;
15034   const char *zName = 0;
15035 
15036   /* Search the recovered schema for an object with root page iRoot. */
15037   shellPreparePrintf(pState->db, pRc, &pStmt,
15038       "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
15039   );
15040   while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15041     const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
15042     if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
15043       bNoop = 1;
15044       break;
15045     }
15046     if( sqlite3_stricmp(zType, "table")==0 ){
15047       zName = (const char*)sqlite3_column_text(pStmt, 1);
15048       zSql = (const char*)sqlite3_column_text(pStmt, 2);
15049       pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
15050       break;
15051     }
15052   }
15053 
15054   shellFinalize(pRc, pStmt);
15055   *pbNoop = bNoop;
15056   return pRet;
15057 }
15058 
15059 /*
15060 ** Return a RecoverTable object representing the orphans table.
15061 */
15062 static RecoverTable *recoverOrphanTable(
15063   ShellState *pState,             /* Shell state object */
15064   int *pRc,                       /* IN/OUT: Error code */
15065   const char *zLostAndFound,      /* Base name for orphans table */
15066   int nCol                        /* Number of user data columns */
15067 ){
15068   RecoverTable *pTab = 0;
15069   if( nCol>=0 && *pRc==SQLITE_OK ){
15070     int i;
15071 
15072     /* This block determines the name of the orphan table. The prefered
15073     ** name is zLostAndFound. But if that clashes with another name
15074     ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
15075     ** and so on until a non-clashing name is found.  */
15076     int iTab = 0;
15077     char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
15078     sqlite3_stmt *pTest = 0;
15079     shellPrepare(pState->db, pRc,
15080         "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
15081     );
15082     if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15083     while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
15084       shellReset(pRc, pTest);
15085       sqlite3_free(zTab);
15086       zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
15087       sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15088     }
15089     shellFinalize(pRc, pTest);
15090 
15091     pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
15092     if( pTab ){
15093       pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
15094       pTab->nCol = nCol;
15095       pTab->iPk = -2;
15096       if( nCol>0 ){
15097         pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
15098         if( pTab->azlCol ){
15099           pTab->azlCol[nCol] = shellMPrintf(pRc, "");
15100           for(i=nCol-1; i>=0; i--){
15101             pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
15102           }
15103         }
15104       }
15105 
15106       if( *pRc!=SQLITE_OK ){
15107         recoverFreeTable(pTab);
15108         pTab = 0;
15109       }else{
15110         raw_printf(pState->out,
15111             "CREATE TABLE %s(rootpgno INTEGER, "
15112             "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
15113         );
15114         for(i=0; i<nCol; i++){
15115           raw_printf(pState->out, ", c%d", i);
15116         }
15117         raw_printf(pState->out, ");\n");
15118       }
15119     }
15120     sqlite3_free(zTab);
15121   }
15122   return pTab;
15123 }
15124 
15125 /*
15126 ** This function is called to recover data from the database. A script
15127 ** to construct a new database containing all recovered data is output
15128 ** on stream pState->out.
15129 */
15130 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
15131   int rc = SQLITE_OK;
15132   sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
15133   sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
15134   sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
15135   const char *zRecoveryDb = "";   /* Name of "recovery" database */
15136   const char *zLostAndFound = "lost_and_found";
15137   int i;
15138   int nOrphan = -1;
15139   RecoverTable *pOrphan = 0;
15140 
15141   int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
15142   int bRowids = 1;                /* 0 if --no-rowids */
15143   for(i=1; i<nArg; i++){
15144     char *z = azArg[i];
15145     int n;
15146     if( z[0]=='-' && z[1]=='-' ) z++;
15147     n = strlen30(z);
15148     if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
15149       bFreelist = 0;
15150     }else
15151     if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
15152       i++;
15153       zRecoveryDb = azArg[i];
15154     }else
15155     if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
15156       i++;
15157       zLostAndFound = azArg[i];
15158     }else
15159     if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
15160       bRowids = 0;
15161     }
15162     else{
15163       utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
15164       showHelp(pState->out, azArg[0]);
15165       return 1;
15166     }
15167   }
15168 
15169   shellExecPrintf(pState->db, &rc,
15170     /* Attach an in-memory database named 'recovery'. Create an indexed
15171     ** cache of the sqlite_dbptr virtual table. */
15172     "PRAGMA writable_schema = on;"
15173     "ATTACH %Q AS recovery;"
15174     "DROP TABLE IF EXISTS recovery.dbptr;"
15175     "DROP TABLE IF EXISTS recovery.freelist;"
15176     "DROP TABLE IF EXISTS recovery.map;"
15177     "DROP TABLE IF EXISTS recovery.schema;"
15178     "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
15179   );
15180 
15181   if( bFreelist ){
15182     shellExec(pState->db, &rc,
15183       "WITH trunk(pgno) AS ("
15184       "  SELECT shell_int32("
15185       "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
15186       "      WHERE x>0"
15187       "    UNION"
15188       "  SELECT shell_int32("
15189       "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
15190       "      FROM trunk WHERE x>0"
15191       "),"
15192       "freelist(data, n, freepgno) AS ("
15193       "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
15194       "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
15195       "    UNION ALL"
15196       "  SELECT data, n-1, shell_int32(data, 2+n) "
15197       "      FROM freelist WHERE n>=0"
15198       ")"
15199       "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
15200     );
15201   }
15202 
15203   /* If this is an auto-vacuum database, add all pointer-map pages to
15204   ** the freelist table. Do this regardless of whether or not
15205   ** --freelist-corrupt was specified.  */
15206   shellExec(pState->db, &rc,
15207     "WITH ptrmap(pgno) AS ("
15208     "  SELECT 2 WHERE shell_int32("
15209     "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
15210     "  )"
15211     "    UNION ALL "
15212     "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
15213     "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
15214     ")"
15215     "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
15216   );
15217 
15218   shellExec(pState->db, &rc,
15219     "CREATE TABLE recovery.dbptr("
15220     "      pgno, child, PRIMARY KEY(child, pgno)"
15221     ") WITHOUT ROWID;"
15222     "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
15223     "    SELECT * FROM sqlite_dbptr"
15224     "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
15225 
15226     /* Delete any pointer to page 1. This ensures that page 1 is considered
15227     ** a root page, regardless of how corrupt the db is. */
15228     "DELETE FROM recovery.dbptr WHERE child = 1;"
15229 
15230     /* Delete all pointers to any pages that have more than one pointer
15231     ** to them. Such pages will be treated as root pages when recovering
15232     ** data.  */
15233     "DELETE FROM recovery.dbptr WHERE child IN ("
15234     "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
15235     ");"
15236 
15237     /* Create the "map" table that will (eventually) contain instructions
15238     ** for dealing with each page in the db that contains one or more
15239     ** records. */
15240     "CREATE TABLE recovery.map("
15241       "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
15242     ");"
15243 
15244     /* Populate table [map]. If there are circular loops of pages in the
15245     ** database, the following adds all pages in such a loop to the map
15246     ** as individual root pages. This could be handled better.  */
15247     "WITH pages(i, maxlen) AS ("
15248     "  SELECT page_count, ("
15249     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
15250     "  ) FROM pragma_page_count WHERE page_count>0"
15251     "    UNION ALL"
15252     "  SELECT i-1, ("
15253     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
15254     "  ) FROM pages WHERE i>=2"
15255     ")"
15256     "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
15257     "  SELECT i, maxlen, NULL, ("
15258     "    WITH p(orig, pgno, parent) AS ("
15259     "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
15260     "        UNION "
15261     "      SELECT i, p.parent, "
15262     "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
15263     "    )"
15264     "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
15265     ") "
15266     "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
15267     "UPDATE recovery.map AS o SET intkey = ("
15268     "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
15269     ");"
15270 
15271     /* Extract data from page 1 and any linked pages into table
15272     ** recovery.schema. With the same schema as an sqlite_master table.  */
15273     "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
15274     "INSERT INTO recovery.schema SELECT "
15275     "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
15276     "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
15277     "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
15278     "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
15279     "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
15280     "FROM sqlite_dbdata WHERE pgno IN ("
15281     "  SELECT pgno FROM recovery.map WHERE root=1"
15282     ")"
15283     "GROUP BY pgno, cell;"
15284     "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
15285   );
15286 
15287   /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
15288   ** CREATE TABLE statements that extracted from the existing schema.  */
15289   if( rc==SQLITE_OK ){
15290     sqlite3_stmt *pStmt = 0;
15291     /* ".recover" might output content in an order which causes immediate
15292     ** foreign key constraints to be violated. So disable foreign-key
15293     ** constraint enforcement to prevent problems when running the output
15294     ** script. */
15295     raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
15296     raw_printf(pState->out, "BEGIN;\n");
15297     raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
15298     shellPrepare(pState->db, &rc,
15299         "SELECT sql FROM recovery.schema "
15300         "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
15301     );
15302     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15303       const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
15304       raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
15305           &zCreateTable[12]
15306       );
15307     }
15308     shellFinalize(&rc, pStmt);
15309   }
15310 
15311   /* Figure out if an orphan table will be required. And if so, how many
15312   ** user columns it should contain */
15313   shellPrepare(pState->db, &rc,
15314       "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
15315       , &pLoop
15316   );
15317   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15318     nOrphan = sqlite3_column_int(pLoop, 0);
15319   }
15320   shellFinalize(&rc, pLoop);
15321   pLoop = 0;
15322 
15323   shellPrepare(pState->db, &rc,
15324       "SELECT pgno FROM recovery.map WHERE root=?", &pPages
15325   );
15326 
15327   shellPrepare(pState->db, &rc,
15328       "SELECT max(field), group_concat(shell_escape_crnl(quote"
15329       "(case when (? AND field<0) then NULL else value end)"
15330       "), ', ')"
15331       ", min(field) "
15332       "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
15333       "GROUP BY cell", &pCells
15334   );
15335 
15336   /* Loop through each root page. */
15337   shellPrepare(pState->db, &rc,
15338       "SELECT root, intkey, max(maxlen) FROM recovery.map"
15339       " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
15340       "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
15341       ")", &pLoop
15342   );
15343   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15344     int iRoot = sqlite3_column_int(pLoop, 0);
15345     int bIntkey = sqlite3_column_int(pLoop, 1);
15346     int nCol = sqlite3_column_int(pLoop, 2);
15347     int bNoop = 0;
15348     RecoverTable *pTab;
15349 
15350     assert( bIntkey==0 || bIntkey==1 );
15351     pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
15352     if( bNoop || rc ) continue;
15353     if( pTab==0 ){
15354       if( pOrphan==0 ){
15355         pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15356       }
15357       pTab = pOrphan;
15358       if( pTab==0 ) break;
15359     }
15360 
15361     if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
15362       raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
15363     }
15364     sqlite3_bind_int(pPages, 1, iRoot);
15365     if( bRowids==0 && pTab->iPk<0 ){
15366       sqlite3_bind_int(pCells, 1, 1);
15367     }else{
15368       sqlite3_bind_int(pCells, 1, 0);
15369     }
15370     sqlite3_bind_int(pCells, 3, pTab->iPk);
15371 
15372     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
15373       int iPgno = sqlite3_column_int(pPages, 0);
15374       sqlite3_bind_int(pCells, 2, iPgno);
15375       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
15376         int nField = sqlite3_column_int(pCells, 0);
15377         int iMin = sqlite3_column_int(pCells, 2);
15378         const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
15379 
15380         RecoverTable *pTab2 = pTab;
15381         if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
15382           if( pOrphan==0 ){
15383             pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15384           }
15385           pTab2 = pOrphan;
15386           if( pTab2==0 ) break;
15387         }
15388 
15389         nField = nField+1;
15390         if( pTab2==pOrphan ){
15391           raw_printf(pState->out,
15392               "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
15393               pTab2->zQuoted, iRoot, iPgno, nField,
15394               iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
15395           );
15396         }else{
15397           raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
15398               pTab2->zQuoted, pTab2->azlCol[nField], zVal
15399           );
15400         }
15401       }
15402       shellReset(&rc, pCells);
15403     }
15404     shellReset(&rc, pPages);
15405     if( pTab!=pOrphan ) recoverFreeTable(pTab);
15406   }
15407   shellFinalize(&rc, pLoop);
15408   shellFinalize(&rc, pPages);
15409   shellFinalize(&rc, pCells);
15410   recoverFreeTable(pOrphan);
15411 
15412   /* The rest of the schema */
15413   if( rc==SQLITE_OK ){
15414     sqlite3_stmt *pStmt = 0;
15415     shellPrepare(pState->db, &rc,
15416         "SELECT sql, name FROM recovery.schema "
15417         "WHERE sql NOT LIKE 'create table%'", &pStmt
15418     );
15419     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15420       const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
15421       if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
15422         const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
15423         char *zPrint = shellMPrintf(&rc,
15424           "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
15425           zName, zName, zSql
15426         );
15427         raw_printf(pState->out, "%s;\n", zPrint);
15428         sqlite3_free(zPrint);
15429       }else{
15430         raw_printf(pState->out, "%s;\n", zSql);
15431       }
15432     }
15433     shellFinalize(&rc, pStmt);
15434   }
15435 
15436   if( rc==SQLITE_OK ){
15437     raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
15438     raw_printf(pState->out, "COMMIT;\n");
15439   }
15440   sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
15441   return rc;
15442 }
15443 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15444 
15445 
15446 /*
15447 ** If an input line begins with "." then invoke this routine to
15448 ** process that line.
15449 **
15450 ** Return 1 on error, 2 to exit, and 0 otherwise.
15451 */
15452 static int do_meta_command(char *zLine, ShellState *p){
15453   int h = 1;
15454   int nArg = 0;
15455   int n, c;
15456   int rc = 0;
15457   char *azArg[52];
15458 
15459 #ifndef SQLITE_OMIT_VIRTUALTABLE
15460   if( p->expert.pExpert ){
15461     expertFinish(p, 1, 0);
15462   }
15463 #endif
15464 
15465   /* Parse the input line into tokens.
15466   */
15467   while( zLine[h] && nArg<ArraySize(azArg)-1 ){
15468     while( IsSpace(zLine[h]) ){ h++; }
15469     if( zLine[h]==0 ) break;
15470     if( zLine[h]=='\'' || zLine[h]=='"' ){
15471       int delim = zLine[h++];
15472       azArg[nArg++] = &zLine[h];
15473       while( zLine[h] && zLine[h]!=delim ){
15474         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
15475         h++;
15476       }
15477       if( zLine[h]==delim ){
15478         zLine[h++] = 0;
15479       }
15480       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
15481     }else{
15482       azArg[nArg++] = &zLine[h];
15483       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
15484       if( zLine[h] ) zLine[h++] = 0;
15485       resolve_backslashes(azArg[nArg-1]);
15486     }
15487   }
15488   azArg[nArg] = 0;
15489 
15490   /* Process the input line.
15491   */
15492   if( nArg==0 ) return 0; /* no tokens, no error */
15493   n = strlen30(azArg[0]);
15494   c = azArg[0][0];
15495   clearTempFile(p);
15496 
15497 #ifndef SQLITE_OMIT_AUTHORIZATION
15498   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
15499     if( nArg!=2 ){
15500       raw_printf(stderr, "Usage: .auth ON|OFF\n");
15501       rc = 1;
15502       goto meta_command_exit;
15503     }
15504     open_db(p, 0);
15505     if( booleanValue(azArg[1]) ){
15506       sqlite3_set_authorizer(p->db, shellAuth, p);
15507     }else{
15508       sqlite3_set_authorizer(p->db, 0, 0);
15509     }
15510   }else
15511 #endif
15512 
15513 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15514   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
15515     open_db(p, 0);
15516     rc = arDotCommand(p, 0, azArg, nArg);
15517   }else
15518 #endif
15519 
15520   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
15521    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
15522   ){
15523     const char *zDestFile = 0;
15524     const char *zDb = 0;
15525     sqlite3 *pDest;
15526     sqlite3_backup *pBackup;
15527     int j;
15528     int bAsync = 0;
15529     const char *zVfs = 0;
15530     for(j=1; j<nArg; j++){
15531       const char *z = azArg[j];
15532       if( z[0]=='-' ){
15533         if( z[1]=='-' ) z++;
15534         if( strcmp(z, "-append")==0 ){
15535           zVfs = "apndvfs";
15536         }else
15537         if( strcmp(z, "-async")==0 ){
15538           bAsync = 1;
15539         }else
15540         {
15541           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
15542           return 1;
15543         }
15544       }else if( zDestFile==0 ){
15545         zDestFile = azArg[j];
15546       }else if( zDb==0 ){
15547         zDb = zDestFile;
15548         zDestFile = azArg[j];
15549       }else{
15550         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
15551         return 1;
15552       }
15553     }
15554     if( zDestFile==0 ){
15555       raw_printf(stderr, "missing FILENAME argument on .backup\n");
15556       return 1;
15557     }
15558     if( zDb==0 ) zDb = "main";
15559     rc = sqlite3_open_v2(zDestFile, &pDest,
15560                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
15561     if( rc!=SQLITE_OK ){
15562       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
15563       close_db(pDest);
15564       return 1;
15565     }
15566     if( bAsync ){
15567       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
15568                    0, 0, 0);
15569     }
15570     open_db(p, 0);
15571     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
15572     if( pBackup==0 ){
15573       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15574       close_db(pDest);
15575       return 1;
15576     }
15577     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
15578     sqlite3_backup_finish(pBackup);
15579     if( rc==SQLITE_DONE ){
15580       rc = 0;
15581     }else{
15582       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15583       rc = 1;
15584     }
15585     close_db(pDest);
15586   }else
15587 
15588   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
15589     if( nArg==2 ){
15590       bail_on_error = booleanValue(azArg[1]);
15591     }else{
15592       raw_printf(stderr, "Usage: .bail on|off\n");
15593       rc = 1;
15594     }
15595   }else
15596 
15597   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
15598     if( nArg==2 ){
15599       if( booleanValue(azArg[1]) ){
15600         setBinaryMode(p->out, 1);
15601       }else{
15602         setTextMode(p->out, 1);
15603       }
15604     }else{
15605       raw_printf(stderr, "Usage: .binary on|off\n");
15606       rc = 1;
15607     }
15608   }else
15609 
15610   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
15611     if( nArg==2 ){
15612 #if defined(_WIN32) || defined(WIN32)
15613       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
15614       rc = !SetCurrentDirectoryW(z);
15615       sqlite3_free(z);
15616 #else
15617       rc = chdir(azArg[1]);
15618 #endif
15619       if( rc ){
15620         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
15621         rc = 1;
15622       }
15623     }else{
15624       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
15625       rc = 1;
15626     }
15627   }else
15628 
15629   /* The undocumented ".breakpoint" command causes a call to the no-op
15630   ** routine named test_breakpoint().
15631   */
15632   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
15633     test_breakpoint();
15634   }else
15635 
15636   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
15637     if( nArg==2 ){
15638       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
15639     }else{
15640       raw_printf(stderr, "Usage: .changes on|off\n");
15641       rc = 1;
15642     }
15643   }else
15644 
15645   /* Cancel output redirection, if it is currently set (by .testcase)
15646   ** Then read the content of the testcase-out.txt file and compare against
15647   ** azArg[1].  If there are differences, report an error and exit.
15648   */
15649   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
15650     char *zRes = 0;
15651     output_reset(p);
15652     if( nArg!=2 ){
15653       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
15654       rc = 2;
15655     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
15656       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
15657       rc = 2;
15658     }else if( testcase_glob(azArg[1],zRes)==0 ){
15659       utf8_printf(stderr,
15660                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
15661                  p->zTestcase, azArg[1], zRes);
15662       rc = 1;
15663     }else{
15664       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
15665       p->nCheck++;
15666     }
15667     sqlite3_free(zRes);
15668   }else
15669 
15670   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
15671     if( nArg==2 ){
15672       tryToClone(p, azArg[1]);
15673     }else{
15674       raw_printf(stderr, "Usage: .clone FILENAME\n");
15675       rc = 1;
15676     }
15677   }else
15678 
15679   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
15680     ShellState data;
15681     char *zErrMsg = 0;
15682     open_db(p, 0);
15683     memcpy(&data, p, sizeof(data));
15684     data.showHeader = 0;
15685     data.cMode = data.mode = MODE_List;
15686     sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
15687     data.cnt = 0;
15688     sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
15689                  callback, &data, &zErrMsg);
15690     if( zErrMsg ){
15691       utf8_printf(stderr,"Error: %s\n", zErrMsg);
15692       sqlite3_free(zErrMsg);
15693       rc = 1;
15694     }
15695   }else
15696 
15697   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
15698     static const struct DbConfigChoices {
15699       const char *zName;
15700       int op;
15701     } aDbConfig[] = {
15702         { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
15703         { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
15704         { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
15705         { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
15706         { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
15707         { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
15708         { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
15709         { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
15710         { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
15711         { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
15712         { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
15713         { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
15714         { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
15715         { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
15716     };
15717     int ii, v;
15718     open_db(p, 0);
15719     for(ii=0; ii<ArraySize(aDbConfig); ii++){
15720       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
15721       if( nArg>=3 ){
15722         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
15723       }
15724       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
15725       utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
15726       if( nArg>1 ) break;
15727     }
15728     if( nArg>1 && ii==ArraySize(aDbConfig) ){
15729       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
15730       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
15731     }
15732   }else
15733 
15734   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
15735     rc = shell_dbinfo_command(p, nArg, azArg);
15736   }else
15737 
15738 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15739   if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
15740     open_db(p, 0);
15741     rc = recoverDatabaseCmd(p, nArg, azArg);
15742   }else
15743 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15744 
15745   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
15746     const char *zLike = 0;
15747     int i;
15748     int savedShowHeader = p->showHeader;
15749     int savedShellFlags = p->shellFlgs;
15750     ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
15751     for(i=1; i<nArg; i++){
15752       if( azArg[i][0]=='-' ){
15753         const char *z = azArg[i]+1;
15754         if( z[0]=='-' ) z++;
15755         if( strcmp(z,"preserve-rowids")==0 ){
15756 #ifdef SQLITE_OMIT_VIRTUALTABLE
15757           raw_printf(stderr, "The --preserve-rowids option is not compatible"
15758                              " with SQLITE_OMIT_VIRTUALTABLE\n");
15759           rc = 1;
15760           goto meta_command_exit;
15761 #else
15762           ShellSetFlag(p, SHFLG_PreserveRowid);
15763 #endif
15764         }else
15765         if( strcmp(z,"newlines")==0 ){
15766           ShellSetFlag(p, SHFLG_Newlines);
15767         }else
15768         {
15769           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
15770           rc = 1;
15771           goto meta_command_exit;
15772         }
15773       }else if( zLike ){
15774         raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
15775                            "?--newlines? ?LIKE-PATTERN?\n");
15776         rc = 1;
15777         goto meta_command_exit;
15778       }else{
15779         zLike = azArg[i];
15780       }
15781     }
15782 
15783     open_db(p, 0);
15784 
15785     /* When playing back a "dump", the content might appear in an order
15786     ** which causes immediate foreign key constraints to be violated.
15787     ** So disable foreign-key constraint enforcement to prevent problems. */
15788     raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
15789     raw_printf(p->out, "BEGIN TRANSACTION;\n");
15790     p->writableSchema = 0;
15791     p->showHeader = 0;
15792     /* Set writable_schema=ON since doing so forces SQLite to initialize
15793     ** as much of the schema as it can even if the sqlite_master table is
15794     ** corrupt. */
15795     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
15796     p->nErr = 0;
15797     if( zLike==0 ){
15798       run_schema_dump_query(p,
15799         "SELECT name, type, sql FROM sqlite_master "
15800         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
15801       );
15802       run_schema_dump_query(p,
15803         "SELECT name, type, sql FROM sqlite_master "
15804         "WHERE name=='sqlite_sequence'"
15805       );
15806       run_table_dump_query(p,
15807         "SELECT sql FROM sqlite_master "
15808         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
15809       );
15810     }else{
15811       char *zSql;
15812       zSql = sqlite3_mprintf(
15813         "SELECT name, type, sql FROM sqlite_master "
15814         "WHERE tbl_name LIKE %Q AND type=='table'"
15815         "  AND sql NOT NULL", zLike);
15816       run_schema_dump_query(p,zSql);
15817       sqlite3_free(zSql);
15818       zSql = sqlite3_mprintf(
15819         "SELECT sql FROM sqlite_master "
15820         "WHERE sql NOT NULL"
15821         "  AND type IN ('index','trigger','view')"
15822         "  AND tbl_name LIKE %Q", zLike);
15823       run_table_dump_query(p, zSql, 0);
15824       sqlite3_free(zSql);
15825     }
15826     if( p->writableSchema ){
15827       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
15828       p->writableSchema = 0;
15829     }
15830     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
15831     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
15832     raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
15833     p->showHeader = savedShowHeader;
15834     p->shellFlgs = savedShellFlags;
15835   }else
15836 
15837   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
15838     if( nArg==2 ){
15839       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
15840     }else{
15841       raw_printf(stderr, "Usage: .echo on|off\n");
15842       rc = 1;
15843     }
15844   }else
15845 
15846   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
15847     if( nArg==2 ){
15848       p->autoEQPtest = 0;
15849       if( p->autoEQPtrace ){
15850         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
15851         p->autoEQPtrace = 0;
15852       }
15853       if( strcmp(azArg[1],"full")==0 ){
15854         p->autoEQP = AUTOEQP_full;
15855       }else if( strcmp(azArg[1],"trigger")==0 ){
15856         p->autoEQP = AUTOEQP_trigger;
15857 #ifdef SQLITE_DEBUG
15858       }else if( strcmp(azArg[1],"test")==0 ){
15859         p->autoEQP = AUTOEQP_on;
15860         p->autoEQPtest = 1;
15861       }else if( strcmp(azArg[1],"trace")==0 ){
15862         p->autoEQP = AUTOEQP_full;
15863         p->autoEQPtrace = 1;
15864         open_db(p, 0);
15865         sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
15866         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
15867 #endif
15868       }else{
15869         p->autoEQP = (u8)booleanValue(azArg[1]);
15870       }
15871     }else{
15872       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
15873       rc = 1;
15874     }
15875   }else
15876 
15877   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
15878     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
15879     rc = 2;
15880   }else
15881 
15882   /* The ".explain" command is automatic now.  It is largely pointless.  It
15883   ** retained purely for backwards compatibility */
15884   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
15885     int val = 1;
15886     if( nArg>=2 ){
15887       if( strcmp(azArg[1],"auto")==0 ){
15888         val = 99;
15889       }else{
15890         val =  booleanValue(azArg[1]);
15891       }
15892     }
15893     if( val==1 && p->mode!=MODE_Explain ){
15894       p->normalMode = p->mode;
15895       p->mode = MODE_Explain;
15896       p->autoExplain = 0;
15897     }else if( val==0 ){
15898       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
15899       p->autoExplain = 0;
15900     }else if( val==99 ){
15901       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
15902       p->autoExplain = 1;
15903     }
15904   }else
15905 
15906 #ifndef SQLITE_OMIT_VIRTUALTABLE
15907   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
15908     open_db(p, 0);
15909     expertDotCommand(p, azArg, nArg);
15910   }else
15911 #endif
15912 
15913   if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
15914     static const struct {
15915        const char *zCtrlName;   /* Name of a test-control option */
15916        int ctrlCode;            /* Integer code for that option */
15917        const char *zUsage;      /* Usage notes */
15918     } aCtrl[] = {
15919       { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
15920       { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
15921    /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
15922       { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
15923       { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
15924    /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
15925       { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
15926       { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
15927       { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
15928     };
15929     int filectrl = -1;
15930     int iCtrl = -1;
15931     sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
15932     int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
15933     int n2, i;
15934     const char *zCmd = 0;
15935 
15936     open_db(p, 0);
15937     zCmd = nArg>=2 ? azArg[1] : "help";
15938 
15939     /* The argument can optionally begin with "-" or "--" */
15940     if( zCmd[0]=='-' && zCmd[1] ){
15941       zCmd++;
15942       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15943     }
15944 
15945     /* --help lists all file-controls */
15946     if( strcmp(zCmd,"help")==0 ){
15947       utf8_printf(p->out, "Available file-controls:\n");
15948       for(i=0; i<ArraySize(aCtrl); i++){
15949         utf8_printf(p->out, "  .filectrl %s %s\n",
15950                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15951       }
15952       rc = 1;
15953       goto meta_command_exit;
15954     }
15955 
15956     /* convert filectrl text option to value. allow any unique prefix
15957     ** of the option name, or a numerical value. */
15958     n2 = strlen30(zCmd);
15959     for(i=0; i<ArraySize(aCtrl); i++){
15960       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15961         if( filectrl<0 ){
15962           filectrl = aCtrl[i].ctrlCode;
15963           iCtrl = i;
15964         }else{
15965           utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
15966                               "Use \".filectrl --help\" for help\n", zCmd);
15967           rc = 1;
15968           goto meta_command_exit;
15969         }
15970       }
15971     }
15972     if( filectrl<0 ){
15973       utf8_printf(stderr,"Error: unknown file-control: %s\n"
15974                          "Use \".filectrl --help\" for help\n", zCmd);
15975     }else{
15976       switch(filectrl){
15977         case SQLITE_FCNTL_SIZE_LIMIT: {
15978           if( nArg!=2 && nArg!=3 ) break;
15979           iRes = nArg==3 ? integerValue(azArg[2]) : -1;
15980           sqlite3_file_control(p->db, 0, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
15981           isOk = 1;
15982           break;
15983         }
15984         case SQLITE_FCNTL_LOCK_TIMEOUT:
15985         case SQLITE_FCNTL_CHUNK_SIZE: {
15986           int x;
15987           if( nArg!=3 ) break;
15988           x = (int)integerValue(azArg[2]);
15989           sqlite3_file_control(p->db, 0, filectrl, &x);
15990           isOk = 2;
15991           break;
15992         }
15993         case SQLITE_FCNTL_PERSIST_WAL:
15994         case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
15995           int x;
15996           if( nArg!=2 && nArg!=3 ) break;
15997           x = nArg==3 ? booleanValue(azArg[2]) : -1;
15998           sqlite3_file_control(p->db, 0, filectrl, &x);
15999           iRes = x;
16000           isOk = 1;
16001           break;
16002         }
16003         case SQLITE_FCNTL_HAS_MOVED: {
16004           int x;
16005           if( nArg!=2 ) break;
16006           sqlite3_file_control(p->db, 0, filectrl, &x);
16007           iRes = x;
16008           isOk = 1;
16009           break;
16010         }
16011         case SQLITE_FCNTL_TEMPFILENAME: {
16012           char *z = 0;
16013           if( nArg!=2 ) break;
16014           sqlite3_file_control(p->db, 0, filectrl, &z);
16015           if( z ){
16016             utf8_printf(p->out, "%s\n", z);
16017             sqlite3_free(z);
16018           }
16019           isOk = 2;
16020           break;
16021         }
16022       }
16023     }
16024     if( isOk==0 && iCtrl>=0 ){
16025       utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
16026       rc = 1;
16027     }else if( isOk==1 ){
16028       char zBuf[100];
16029       sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
16030       raw_printf(p->out, "%s\n", zBuf);
16031     }
16032   }else
16033 
16034   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
16035     ShellState data;
16036     char *zErrMsg = 0;
16037     int doStats = 0;
16038     memcpy(&data, p, sizeof(data));
16039     data.showHeader = 0;
16040     data.cMode = data.mode = MODE_Semi;
16041     if( nArg==2 && optionMatch(azArg[1], "indent") ){
16042       data.cMode = data.mode = MODE_Pretty;
16043       nArg = 1;
16044     }
16045     if( nArg!=1 ){
16046       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
16047       rc = 1;
16048       goto meta_command_exit;
16049     }
16050     open_db(p, 0);
16051     rc = sqlite3_exec(p->db,
16052        "SELECT sql FROM"
16053        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
16054        "     FROM sqlite_master UNION ALL"
16055        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
16056        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
16057        "ORDER BY rowid",
16058        callback, &data, &zErrMsg
16059     );
16060     if( rc==SQLITE_OK ){
16061       sqlite3_stmt *pStmt;
16062       rc = sqlite3_prepare_v2(p->db,
16063                "SELECT rowid FROM sqlite_master"
16064                " WHERE name GLOB 'sqlite_stat[134]'",
16065                -1, &pStmt, 0);
16066       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
16067       sqlite3_finalize(pStmt);
16068     }
16069     if( doStats==0 ){
16070       raw_printf(p->out, "/* No STAT tables available */\n");
16071     }else{
16072       raw_printf(p->out, "ANALYZE sqlite_master;\n");
16073       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
16074                    callback, &data, &zErrMsg);
16075       data.cMode = data.mode = MODE_Insert;
16076       data.zDestTable = "sqlite_stat1";
16077       shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
16078       data.zDestTable = "sqlite_stat4";
16079       shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
16080       raw_printf(p->out, "ANALYZE sqlite_master;\n");
16081     }
16082   }else
16083 
16084   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
16085     if( nArg==2 ){
16086       p->showHeader = booleanValue(azArg[1]);
16087     }else{
16088       raw_printf(stderr, "Usage: .headers on|off\n");
16089       rc = 1;
16090     }
16091   }else
16092 
16093   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
16094     if( nArg>=2 ){
16095       n = showHelp(p->out, azArg[1]);
16096       if( n==0 ){
16097         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
16098       }
16099     }else{
16100       showHelp(p->out, 0);
16101     }
16102   }else
16103 
16104   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
16105     char *zTable;               /* Insert data into this table */
16106     char *zFile;                /* Name of file to extra content from */
16107     sqlite3_stmt *pStmt = NULL; /* A statement */
16108     int nCol;                   /* Number of columns in the table */
16109     int nByte;                  /* Number of bytes in an SQL string */
16110     int i, j;                   /* Loop counters */
16111     int needCommit;             /* True to COMMIT or ROLLBACK at end */
16112     int nSep;                   /* Number of bytes in p->colSeparator[] */
16113     char *zSql;                 /* An SQL statement */
16114     ImportCtx sCtx;             /* Reader context */
16115     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
16116     int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
16117 
16118     if( nArg!=3 ){
16119       raw_printf(stderr, "Usage: .import FILE TABLE\n");
16120       goto meta_command_exit;
16121     }
16122     zFile = azArg[1];
16123     zTable = azArg[2];
16124     seenInterrupt = 0;
16125     memset(&sCtx, 0, sizeof(sCtx));
16126     open_db(p, 0);
16127     nSep = strlen30(p->colSeparator);
16128     if( nSep==0 ){
16129       raw_printf(stderr,
16130                  "Error: non-null column separator required for import\n");
16131       return 1;
16132     }
16133     if( nSep>1 ){
16134       raw_printf(stderr, "Error: multi-character column separators not allowed"
16135                       " for import\n");
16136       return 1;
16137     }
16138     nSep = strlen30(p->rowSeparator);
16139     if( nSep==0 ){
16140       raw_printf(stderr, "Error: non-null row separator required for import\n");
16141       return 1;
16142     }
16143     if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
16144       /* When importing CSV (only), if the row separator is set to the
16145       ** default output row separator, change it to the default input
16146       ** row separator.  This avoids having to maintain different input
16147       ** and output row separators. */
16148       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16149       nSep = strlen30(p->rowSeparator);
16150     }
16151     if( nSep>1 ){
16152       raw_printf(stderr, "Error: multi-character row separators not allowed"
16153                       " for import\n");
16154       return 1;
16155     }
16156     sCtx.zFile = zFile;
16157     sCtx.nLine = 1;
16158     if( sCtx.zFile[0]=='|' ){
16159 #ifdef SQLITE_OMIT_POPEN
16160       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16161       return 1;
16162 #else
16163       sCtx.in = popen(sCtx.zFile+1, "r");
16164       sCtx.zFile = "<pipe>";
16165       xCloser = pclose;
16166 #endif
16167     }else{
16168       sCtx.in = fopen(sCtx.zFile, "rb");
16169       xCloser = fclose;
16170     }
16171     if( p->mode==MODE_Ascii ){
16172       xRead = ascii_read_one_field;
16173     }else{
16174       xRead = csv_read_one_field;
16175     }
16176     if( sCtx.in==0 ){
16177       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16178       return 1;
16179     }
16180     sCtx.cColSep = p->colSeparator[0];
16181     sCtx.cRowSep = p->rowSeparator[0];
16182     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
16183     if( zSql==0 ){
16184       xCloser(sCtx.in);
16185       shell_out_of_memory();
16186     }
16187     nByte = strlen30(zSql);
16188     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16189     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
16190     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
16191       char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
16192       char cSep = '(';
16193       while( xRead(&sCtx) ){
16194         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
16195         cSep = ',';
16196         if( sCtx.cTerm!=sCtx.cColSep ) break;
16197       }
16198       if( cSep=='(' ){
16199         sqlite3_free(zCreate);
16200         sqlite3_free(sCtx.z);
16201         xCloser(sCtx.in);
16202         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
16203         return 1;
16204       }
16205       zCreate = sqlite3_mprintf("%z\n)", zCreate);
16206       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
16207       sqlite3_free(zCreate);
16208       if( rc ){
16209         utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
16210                 sqlite3_errmsg(p->db));
16211         sqlite3_free(sCtx.z);
16212         xCloser(sCtx.in);
16213         return 1;
16214       }
16215       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16216     }
16217     sqlite3_free(zSql);
16218     if( rc ){
16219       if (pStmt) sqlite3_finalize(pStmt);
16220       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
16221       xCloser(sCtx.in);
16222       return 1;
16223     }
16224     nCol = sqlite3_column_count(pStmt);
16225     sqlite3_finalize(pStmt);
16226     pStmt = 0;
16227     if( nCol==0 ) return 0; /* no columns, no error */
16228     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
16229     if( zSql==0 ){
16230       xCloser(sCtx.in);
16231       shell_out_of_memory();
16232     }
16233     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
16234     j = strlen30(zSql);
16235     for(i=1; i<nCol; i++){
16236       zSql[j++] = ',';
16237       zSql[j++] = '?';
16238     }
16239     zSql[j++] = ')';
16240     zSql[j] = 0;
16241     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16242     sqlite3_free(zSql);
16243     if( rc ){
16244       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16245       if (pStmt) sqlite3_finalize(pStmt);
16246       xCloser(sCtx.in);
16247       return 1;
16248     }
16249     needCommit = sqlite3_get_autocommit(p->db);
16250     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
16251     do{
16252       int startLine = sCtx.nLine;
16253       for(i=0; i<nCol; i++){
16254         char *z = xRead(&sCtx);
16255         /*
16256         ** Did we reach end-of-file before finding any columns?
16257         ** If so, stop instead of NULL filling the remaining columns.
16258         */
16259         if( z==0 && i==0 ) break;
16260         /*
16261         ** Did we reach end-of-file OR end-of-line before finding any
16262         ** columns in ASCII mode?  If so, stop instead of NULL filling
16263         ** the remaining columns.
16264         */
16265         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
16266         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
16267         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
16268           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16269                           "filling the rest with NULL\n",
16270                           sCtx.zFile, startLine, nCol, i+1);
16271           i += 2;
16272           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
16273         }
16274       }
16275       if( sCtx.cTerm==sCtx.cColSep ){
16276         do{
16277           xRead(&sCtx);
16278           i++;
16279         }while( sCtx.cTerm==sCtx.cColSep );
16280         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16281                         "extras ignored\n",
16282                         sCtx.zFile, startLine, nCol, i);
16283       }
16284       if( i>=nCol ){
16285         sqlite3_step(pStmt);
16286         rc = sqlite3_reset(pStmt);
16287         if( rc!=SQLITE_OK ){
16288           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
16289                       startLine, sqlite3_errmsg(p->db));
16290         }
16291       }
16292     }while( sCtx.cTerm!=EOF );
16293 
16294     xCloser(sCtx.in);
16295     sqlite3_free(sCtx.z);
16296     sqlite3_finalize(pStmt);
16297     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
16298   }else
16299 
16300 #ifndef SQLITE_UNTESTABLE
16301   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
16302     char *zSql;
16303     char *zCollist = 0;
16304     sqlite3_stmt *pStmt;
16305     int tnum = 0;
16306     int i;
16307     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
16308       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
16309                           "       .imposter off\n");
16310       rc = 1;
16311       goto meta_command_exit;
16312     }
16313     open_db(p, 0);
16314     if( nArg==2 ){
16315       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
16316       goto meta_command_exit;
16317     }
16318     zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
16319                            " WHERE name='%q' AND type='index'", azArg[1]);
16320     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16321     sqlite3_free(zSql);
16322     if( sqlite3_step(pStmt)==SQLITE_ROW ){
16323       tnum = sqlite3_column_int(pStmt, 0);
16324     }
16325     sqlite3_finalize(pStmt);
16326     if( tnum==0 ){
16327       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
16328       rc = 1;
16329       goto meta_command_exit;
16330     }
16331     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
16332     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16333     sqlite3_free(zSql);
16334     i = 0;
16335     while( sqlite3_step(pStmt)==SQLITE_ROW ){
16336       char zLabel[20];
16337       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
16338       i++;
16339       if( zCol==0 ){
16340         if( sqlite3_column_int(pStmt,1)==-1 ){
16341           zCol = "_ROWID_";
16342         }else{
16343           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
16344           zCol = zLabel;
16345         }
16346       }
16347       if( zCollist==0 ){
16348         zCollist = sqlite3_mprintf("\"%w\"", zCol);
16349       }else{
16350         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
16351       }
16352     }
16353     sqlite3_finalize(pStmt);
16354     zSql = sqlite3_mprintf(
16355           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
16356           azArg[2], zCollist, zCollist);
16357     sqlite3_free(zCollist);
16358     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
16359     if( rc==SQLITE_OK ){
16360       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
16361       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
16362       if( rc ){
16363         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
16364       }else{
16365         utf8_printf(stdout, "%s;\n", zSql);
16366         raw_printf(stdout,
16367            "WARNING: writing to an imposter table will corrupt the index!\n"
16368         );
16369       }
16370     }else{
16371       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
16372       rc = 1;
16373     }
16374     sqlite3_free(zSql);
16375   }else
16376 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
16377 
16378 #ifdef SQLITE_ENABLE_IOTRACE
16379   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
16380     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
16381     if( iotrace && iotrace!=stdout ) fclose(iotrace);
16382     iotrace = 0;
16383     if( nArg<2 ){
16384       sqlite3IoTrace = 0;
16385     }else if( strcmp(azArg[1], "-")==0 ){
16386       sqlite3IoTrace = iotracePrintf;
16387       iotrace = stdout;
16388     }else{
16389       iotrace = fopen(azArg[1], "w");
16390       if( iotrace==0 ){
16391         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
16392         sqlite3IoTrace = 0;
16393         rc = 1;
16394       }else{
16395         sqlite3IoTrace = iotracePrintf;
16396       }
16397     }
16398   }else
16399 #endif
16400 
16401   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
16402     static const struct {
16403        const char *zLimitName;   /* Name of a limit */
16404        int limitCode;            /* Integer code for that limit */
16405     } aLimit[] = {
16406       { "length",                SQLITE_LIMIT_LENGTH                    },
16407       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
16408       { "column",                SQLITE_LIMIT_COLUMN                    },
16409       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
16410       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
16411       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
16412       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
16413       { "attached",              SQLITE_LIMIT_ATTACHED                  },
16414       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
16415       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
16416       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
16417       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
16418     };
16419     int i, n2;
16420     open_db(p, 0);
16421     if( nArg==1 ){
16422       for(i=0; i<ArraySize(aLimit); i++){
16423         printf("%20s %d\n", aLimit[i].zLimitName,
16424                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
16425       }
16426     }else if( nArg>3 ){
16427       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
16428       rc = 1;
16429       goto meta_command_exit;
16430     }else{
16431       int iLimit = -1;
16432       n2 = strlen30(azArg[1]);
16433       for(i=0; i<ArraySize(aLimit); i++){
16434         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
16435           if( iLimit<0 ){
16436             iLimit = i;
16437           }else{
16438             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
16439             rc = 1;
16440             goto meta_command_exit;
16441           }
16442         }
16443       }
16444       if( iLimit<0 ){
16445         utf8_printf(stderr, "unknown limit: \"%s\"\n"
16446                         "enter \".limits\" with no arguments for a list.\n",
16447                          azArg[1]);
16448         rc = 1;
16449         goto meta_command_exit;
16450       }
16451       if( nArg==3 ){
16452         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
16453                       (int)integerValue(azArg[2]));
16454       }
16455       printf("%20s %d\n", aLimit[iLimit].zLimitName,
16456              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
16457     }
16458   }else
16459 
16460   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
16461     open_db(p, 0);
16462     lintDotCommand(p, azArg, nArg);
16463   }else
16464 
16465 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16466   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
16467     const char *zFile, *zProc;
16468     char *zErrMsg = 0;
16469     if( nArg<2 ){
16470       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
16471       rc = 1;
16472       goto meta_command_exit;
16473     }
16474     zFile = azArg[1];
16475     zProc = nArg>=3 ? azArg[2] : 0;
16476     open_db(p, 0);
16477     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
16478     if( rc!=SQLITE_OK ){
16479       utf8_printf(stderr, "Error: %s\n", zErrMsg);
16480       sqlite3_free(zErrMsg);
16481       rc = 1;
16482     }
16483   }else
16484 #endif
16485 
16486   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
16487     if( nArg!=2 ){
16488       raw_printf(stderr, "Usage: .log FILENAME\n");
16489       rc = 1;
16490     }else{
16491       const char *zFile = azArg[1];
16492       output_file_close(p->pLog);
16493       p->pLog = output_file_open(zFile, 0);
16494     }
16495   }else
16496 
16497   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
16498     const char *zMode = nArg>=2 ? azArg[1] : "";
16499     int n2 = strlen30(zMode);
16500     int c2 = zMode[0];
16501     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
16502       p->mode = MODE_Line;
16503       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16504     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
16505       p->mode = MODE_Column;
16506       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16507     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
16508       p->mode = MODE_List;
16509       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
16510       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16511     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
16512       p->mode = MODE_Html;
16513     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
16514       p->mode = MODE_Tcl;
16515       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
16516       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16517     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
16518       p->mode = MODE_Csv;
16519       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
16520       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
16521     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
16522       p->mode = MODE_List;
16523       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
16524     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
16525       p->mode = MODE_Insert;
16526       set_table_name(p, nArg>=3 ? azArg[2] : "table");
16527     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
16528       p->mode = MODE_Quote;
16529     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
16530       p->mode = MODE_Ascii;
16531       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
16532       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
16533     }else if( nArg==1 ){
16534       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
16535     }else{
16536       raw_printf(stderr, "Error: mode should be one of: "
16537          "ascii column csv html insert line list quote tabs tcl\n");
16538       rc = 1;
16539     }
16540     p->cMode = p->mode;
16541   }else
16542 
16543   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
16544     if( nArg==2 ){
16545       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
16546                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
16547     }else{
16548       raw_printf(stderr, "Usage: .nullvalue STRING\n");
16549       rc = 1;
16550     }
16551   }else
16552 
16553   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
16554     char *zNewFilename;  /* Name of the database file to open */
16555     int iName = 1;       /* Index in azArg[] of the filename */
16556     int newFlag = 0;     /* True to delete file before opening */
16557     /* Close the existing database */
16558     session_close_all(p);
16559     close_db(p->db);
16560     p->db = 0;
16561     p->zDbFilename = 0;
16562     sqlite3_free(p->zFreeOnClose);
16563     p->zFreeOnClose = 0;
16564     p->openMode = SHELL_OPEN_UNSPEC;
16565     p->szMax = 0;
16566     /* Check for command-line arguments */
16567     for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
16568       const char *z = azArg[iName];
16569       if( optionMatch(z,"new") ){
16570         newFlag = 1;
16571 #ifdef SQLITE_HAVE_ZLIB
16572       }else if( optionMatch(z, "zip") ){
16573         p->openMode = SHELL_OPEN_ZIPFILE;
16574 #endif
16575       }else if( optionMatch(z, "append") ){
16576         p->openMode = SHELL_OPEN_APPENDVFS;
16577       }else if( optionMatch(z, "readonly") ){
16578         p->openMode = SHELL_OPEN_READONLY;
16579 #ifdef SQLITE_ENABLE_DESERIALIZE
16580       }else if( optionMatch(z, "deserialize") ){
16581         p->openMode = SHELL_OPEN_DESERIALIZE;
16582       }else if( optionMatch(z, "hexdb") ){
16583         p->openMode = SHELL_OPEN_HEXDB;
16584       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
16585         p->szMax = integerValue(azArg[++iName]);
16586 #endif /* SQLITE_ENABLE_DESERIALIZE */
16587       }else if( z[0]=='-' ){
16588         utf8_printf(stderr, "unknown option: %s\n", z);
16589         rc = 1;
16590         goto meta_command_exit;
16591       }
16592     }
16593     /* If a filename is specified, try to open it first */
16594     zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
16595     if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
16596       if( newFlag ) shellDeleteFile(zNewFilename);
16597       p->zDbFilename = zNewFilename;
16598       open_db(p, OPEN_DB_KEEPALIVE);
16599       if( p->db==0 ){
16600         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
16601         sqlite3_free(zNewFilename);
16602       }else{
16603         p->zFreeOnClose = zNewFilename;
16604       }
16605     }
16606     if( p->db==0 ){
16607       /* As a fall-back open a TEMP database */
16608       p->zDbFilename = 0;
16609       open_db(p, 0);
16610     }
16611   }else
16612 
16613   if( (c=='o'
16614         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
16615    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
16616   ){
16617     const char *zFile = nArg>=2 ? azArg[1] : "stdout";
16618     int bTxtMode = 0;
16619     if( azArg[0][0]=='e' ){
16620       /* Transform the ".excel" command into ".once -x" */
16621       nArg = 2;
16622       azArg[0] = "once";
16623       zFile = azArg[1] = "-x";
16624       n = 4;
16625     }
16626     if( nArg>2 ){
16627       utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
16628       rc = 1;
16629       goto meta_command_exit;
16630     }
16631     if( n>1 && strncmp(azArg[0], "once", n)==0 ){
16632       if( nArg<2 ){
16633         raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
16634         rc = 1;
16635         goto meta_command_exit;
16636       }
16637       p->outCount = 2;
16638     }else{
16639       p->outCount = 0;
16640     }
16641     output_reset(p);
16642     if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
16643 #ifndef SQLITE_NOHAVE_SYSTEM
16644     if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
16645       p->doXdgOpen = 1;
16646       outputModePush(p);
16647       if( zFile[1]=='x' ){
16648         newTempFile(p, "csv");
16649         p->mode = MODE_Csv;
16650         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
16651         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
16652       }else{
16653         newTempFile(p, "txt");
16654         bTxtMode = 1;
16655       }
16656       zFile = p->zTempFile;
16657     }
16658 #endif /* SQLITE_NOHAVE_SYSTEM */
16659     if( zFile[0]=='|' ){
16660 #ifdef SQLITE_OMIT_POPEN
16661       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16662       rc = 1;
16663       p->out = stdout;
16664 #else
16665       p->out = popen(zFile + 1, "w");
16666       if( p->out==0 ){
16667         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
16668         p->out = stdout;
16669         rc = 1;
16670       }else{
16671         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
16672       }
16673 #endif
16674     }else{
16675       p->out = output_file_open(zFile, bTxtMode);
16676       if( p->out==0 ){
16677         if( strcmp(zFile,"off")!=0 ){
16678           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
16679         }
16680         p->out = stdout;
16681         rc = 1;
16682       } else {
16683         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
16684       }
16685     }
16686   }else
16687 
16688   if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
16689     open_db(p,0);
16690     if( nArg<=1 ) goto parameter_syntax_error;
16691 
16692     /* .parameter clear
16693     ** Clear all bind parameters by dropping the TEMP table that holds them.
16694     */
16695     if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
16696       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
16697                    0, 0, 0);
16698     }else
16699 
16700     /* .parameter list
16701     ** List all bind parameters.
16702     */
16703     if( nArg==2 && strcmp(azArg[1],"list")==0 ){
16704       sqlite3_stmt *pStmt = 0;
16705       int rx;
16706       int len = 0;
16707       rx = sqlite3_prepare_v2(p->db,
16708              "SELECT max(length(key)) "
16709              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
16710       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
16711         len = sqlite3_column_int(pStmt, 0);
16712         if( len>40 ) len = 40;
16713       }
16714       sqlite3_finalize(pStmt);
16715       pStmt = 0;
16716       if( len ){
16717         rx = sqlite3_prepare_v2(p->db,
16718              "SELECT key, quote(value) "
16719              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
16720         while( sqlite3_step(pStmt)==SQLITE_ROW ){
16721           utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
16722                       sqlite3_column_text(pStmt,1));
16723         }
16724         sqlite3_finalize(pStmt);
16725       }
16726     }else
16727 
16728     /* .parameter init
16729     ** Make sure the TEMP table used to hold bind parameters exists.
16730     ** Create it if necessary.
16731     */
16732     if( nArg==2 && strcmp(azArg[1],"init")==0 ){
16733       bind_table_init(p);
16734     }else
16735 
16736     /* .parameter set NAME VALUE
16737     ** Set or reset a bind parameter.  NAME should be the full parameter
16738     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
16739     ** VALUE can be in either SQL literal notation, or if not it will be
16740     ** understood to be a text string.
16741     */
16742     if( nArg==4 && strcmp(azArg[1],"set")==0 ){
16743       int rx;
16744       char *zSql;
16745       sqlite3_stmt *pStmt;
16746       const char *zKey = azArg[2];
16747       const char *zValue = azArg[3];
16748       bind_table_init(p);
16749       zSql = sqlite3_mprintf(
16750                   "REPLACE INTO temp.sqlite_parameters(key,value)"
16751                   "VALUES(%Q,%s);", zKey, zValue);
16752       if( zSql==0 ) shell_out_of_memory();
16753       pStmt = 0;
16754       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16755       sqlite3_free(zSql);
16756       if( rx!=SQLITE_OK ){
16757         sqlite3_finalize(pStmt);
16758         pStmt = 0;
16759         zSql = sqlite3_mprintf(
16760                    "REPLACE INTO temp.sqlite_parameters(key,value)"
16761                    "VALUES(%Q,%Q);", zKey, zValue);
16762         if( zSql==0 ) shell_out_of_memory();
16763         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16764         sqlite3_free(zSql);
16765         if( rx!=SQLITE_OK ){
16766           utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
16767           sqlite3_finalize(pStmt);
16768           pStmt = 0;
16769           rc = 1;
16770         }
16771       }
16772       sqlite3_step(pStmt);
16773       sqlite3_finalize(pStmt);
16774     }else
16775 
16776     /* .parameter unset NAME
16777     ** Remove the NAME binding from the parameter binding table, if it
16778     ** exists.
16779     */
16780     if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
16781       char *zSql = sqlite3_mprintf(
16782           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
16783       if( zSql==0 ) shell_out_of_memory();
16784       sqlite3_exec(p->db, zSql, 0, 0, 0);
16785       sqlite3_free(zSql);
16786     }else
16787     /* If no command name matches, show a syntax error */
16788     parameter_syntax_error:
16789     showHelp(p->out, "parameter");
16790   }else
16791 
16792   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
16793     int i;
16794     for(i=1; i<nArg; i++){
16795       if( i>1 ) raw_printf(p->out, " ");
16796       utf8_printf(p->out, "%s", azArg[i]);
16797     }
16798     raw_printf(p->out, "\n");
16799   }else
16800 
16801 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16802   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
16803     int i;
16804     int nn = 0;
16805     p->flgProgress = 0;
16806     p->mxProgress = 0;
16807     p->nProgress = 0;
16808     for(i=1; i<nArg; i++){
16809       const char *z = azArg[i];
16810       if( z[0]=='-' ){
16811         z++;
16812         if( z[0]=='-' ) z++;
16813         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
16814           p->flgProgress |= SHELL_PROGRESS_QUIET;
16815           continue;
16816         }
16817         if( strcmp(z,"reset")==0 ){
16818           p->flgProgress |= SHELL_PROGRESS_RESET;
16819           continue;
16820         }
16821         if( strcmp(z,"once")==0 ){
16822           p->flgProgress |= SHELL_PROGRESS_ONCE;
16823           continue;
16824         }
16825         if( strcmp(z,"limit")==0 ){
16826           if( i+1>=nArg ){
16827             utf8_printf(stderr, "Error: missing argument on --limit\n");
16828             rc = 1;
16829             goto meta_command_exit;
16830           }else{
16831             p->mxProgress = (int)integerValue(azArg[++i]);
16832           }
16833           continue;
16834         }
16835         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
16836         rc = 1;
16837         goto meta_command_exit;
16838       }else{
16839         nn = (int)integerValue(z);
16840       }
16841     }
16842     open_db(p, 0);
16843     sqlite3_progress_handler(p->db, nn, progress_handler, p);
16844   }else
16845 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
16846 
16847   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
16848     if( nArg >= 2) {
16849       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
16850     }
16851     if( nArg >= 3) {
16852       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
16853     }
16854   }else
16855 
16856   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
16857     rc = 2;
16858   }else
16859 
16860   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
16861     FILE *inSaved = p->in;
16862     int savedLineno = p->lineno;
16863     if( nArg!=2 ){
16864       raw_printf(stderr, "Usage: .read FILE\n");
16865       rc = 1;
16866       goto meta_command_exit;
16867     }
16868     p->in = fopen(azArg[1], "rb");
16869     if( p->in==0 ){
16870       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
16871       rc = 1;
16872     }else{
16873       rc = process_input(p);
16874       fclose(p->in);
16875     }
16876     p->in = inSaved;
16877     p->lineno = savedLineno;
16878   }else
16879 
16880   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
16881     const char *zSrcFile;
16882     const char *zDb;
16883     sqlite3 *pSrc;
16884     sqlite3_backup *pBackup;
16885     int nTimeout = 0;
16886 
16887     if( nArg==2 ){
16888       zSrcFile = azArg[1];
16889       zDb = "main";
16890     }else if( nArg==3 ){
16891       zSrcFile = azArg[2];
16892       zDb = azArg[1];
16893     }else{
16894       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
16895       rc = 1;
16896       goto meta_command_exit;
16897     }
16898     rc = sqlite3_open(zSrcFile, &pSrc);
16899     if( rc!=SQLITE_OK ){
16900       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
16901       close_db(pSrc);
16902       return 1;
16903     }
16904     open_db(p, 0);
16905     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
16906     if( pBackup==0 ){
16907       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16908       close_db(pSrc);
16909       return 1;
16910     }
16911     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
16912           || rc==SQLITE_BUSY  ){
16913       if( rc==SQLITE_BUSY ){
16914         if( nTimeout++ >= 3 ) break;
16915         sqlite3_sleep(100);
16916       }
16917     }
16918     sqlite3_backup_finish(pBackup);
16919     if( rc==SQLITE_DONE ){
16920       rc = 0;
16921     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
16922       raw_printf(stderr, "Error: source database is busy\n");
16923       rc = 1;
16924     }else{
16925       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16926       rc = 1;
16927     }
16928     close_db(pSrc);
16929   }else
16930 
16931   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
16932     if( nArg==2 ){
16933       p->scanstatsOn = (u8)booleanValue(azArg[1]);
16934 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
16935       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
16936 #endif
16937     }else{
16938       raw_printf(stderr, "Usage: .scanstats on|off\n");
16939       rc = 1;
16940     }
16941   }else
16942 
16943   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
16944     ShellText sSelect;
16945     ShellState data;
16946     char *zErrMsg = 0;
16947     const char *zDiv = "(";
16948     const char *zName = 0;
16949     int iSchema = 0;
16950     int bDebug = 0;
16951     int ii;
16952 
16953     open_db(p, 0);
16954     memcpy(&data, p, sizeof(data));
16955     data.showHeader = 0;
16956     data.cMode = data.mode = MODE_Semi;
16957     initText(&sSelect);
16958     for(ii=1; ii<nArg; ii++){
16959       if( optionMatch(azArg[ii],"indent") ){
16960         data.cMode = data.mode = MODE_Pretty;
16961       }else if( optionMatch(azArg[ii],"debug") ){
16962         bDebug = 1;
16963       }else if( zName==0 ){
16964         zName = azArg[ii];
16965       }else{
16966         raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
16967         rc = 1;
16968         goto meta_command_exit;
16969       }
16970     }
16971     if( zName!=0 ){
16972       int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
16973       if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
16974         char *new_argv[2], *new_colv[2];
16975         new_argv[0] = sqlite3_mprintf(
16976                       "CREATE TABLE %s (\n"
16977                       "  type text,\n"
16978                       "  name text,\n"
16979                       "  tbl_name text,\n"
16980                       "  rootpage integer,\n"
16981                       "  sql text\n"
16982                       ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
16983         new_argv[1] = 0;
16984         new_colv[0] = "sql";
16985         new_colv[1] = 0;
16986         callback(&data, 1, new_argv, new_colv);
16987         sqlite3_free(new_argv[0]);
16988       }
16989     }
16990     if( zDiv ){
16991       sqlite3_stmt *pStmt = 0;
16992       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
16993                               -1, &pStmt, 0);
16994       if( rc ){
16995         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16996         sqlite3_finalize(pStmt);
16997         rc = 1;
16998         goto meta_command_exit;
16999       }
17000       appendText(&sSelect, "SELECT sql FROM", 0);
17001       iSchema = 0;
17002       while( sqlite3_step(pStmt)==SQLITE_ROW ){
17003         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
17004         char zScNum[30];
17005         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
17006         appendText(&sSelect, zDiv, 0);
17007         zDiv = " UNION ALL ";
17008         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
17009         if( sqlite3_stricmp(zDb, "main")!=0 ){
17010           appendText(&sSelect, zDb, '\'');
17011         }else{
17012           appendText(&sSelect, "NULL", 0);
17013         }
17014         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
17015         appendText(&sSelect, zScNum, 0);
17016         appendText(&sSelect, " AS snum, ", 0);
17017         appendText(&sSelect, zDb, '\'');
17018         appendText(&sSelect, " AS sname FROM ", 0);
17019         appendText(&sSelect, zDb, quoteChar(zDb));
17020         appendText(&sSelect, ".sqlite_master", 0);
17021       }
17022       sqlite3_finalize(pStmt);
17023 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
17024       if( zName ){
17025         appendText(&sSelect,
17026            " UNION ALL SELECT shell_module_schema(name),"
17027            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
17028         0);
17029       }
17030 #endif
17031       appendText(&sSelect, ") WHERE ", 0);
17032       if( zName ){
17033         char *zQarg = sqlite3_mprintf("%Q", zName);
17034         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
17035                     strchr(zName, '[') != 0;
17036         if( strchr(zName, '.') ){
17037           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
17038         }else{
17039           appendText(&sSelect, "lower(tbl_name)", 0);
17040         }
17041         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
17042         appendText(&sSelect, zQarg, 0);
17043         if( !bGlob ){
17044           appendText(&sSelect, " ESCAPE '\\' ", 0);
17045         }
17046         appendText(&sSelect, " AND ", 0);
17047         sqlite3_free(zQarg);
17048       }
17049       appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
17050                            " ORDER BY snum, rowid", 0);
17051       if( bDebug ){
17052         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
17053       }else{
17054         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
17055       }
17056       freeText(&sSelect);
17057     }
17058     if( zErrMsg ){
17059       utf8_printf(stderr,"Error: %s\n", zErrMsg);
17060       sqlite3_free(zErrMsg);
17061       rc = 1;
17062     }else if( rc != SQLITE_OK ){
17063       raw_printf(stderr,"Error: querying schema information\n");
17064       rc = 1;
17065     }else{
17066       rc = 0;
17067     }
17068   }else
17069 
17070 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
17071   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
17072     sqlite3SelectTrace = (int)integerValue(azArg[1]);
17073   }else
17074 #endif
17075 
17076 #if defined(SQLITE_ENABLE_SESSION)
17077   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
17078     OpenSession *pSession = &p->aSession[0];
17079     char **azCmd = &azArg[1];
17080     int iSes = 0;
17081     int nCmd = nArg - 1;
17082     int i;
17083     if( nArg<=1 ) goto session_syntax_error;
17084     open_db(p, 0);
17085     if( nArg>=3 ){
17086       for(iSes=0; iSes<p->nSession; iSes++){
17087         if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
17088       }
17089       if( iSes<p->nSession ){
17090         pSession = &p->aSession[iSes];
17091         azCmd++;
17092         nCmd--;
17093       }else{
17094         pSession = &p->aSession[0];
17095         iSes = 0;
17096       }
17097     }
17098 
17099     /* .session attach TABLE
17100     ** Invoke the sqlite3session_attach() interface to attach a particular
17101     ** table so that it is never filtered.
17102     */
17103     if( strcmp(azCmd[0],"attach")==0 ){
17104       if( nCmd!=2 ) goto session_syntax_error;
17105       if( pSession->p==0 ){
17106         session_not_open:
17107         raw_printf(stderr, "ERROR: No sessions are open\n");
17108       }else{
17109         rc = sqlite3session_attach(pSession->p, azCmd[1]);
17110         if( rc ){
17111           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
17112           rc = 0;
17113         }
17114       }
17115     }else
17116 
17117     /* .session changeset FILE
17118     ** .session patchset FILE
17119     ** Write a changeset or patchset into a file.  The file is overwritten.
17120     */
17121     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
17122       FILE *out = 0;
17123       if( nCmd!=2 ) goto session_syntax_error;
17124       if( pSession->p==0 ) goto session_not_open;
17125       out = fopen(azCmd[1], "wb");
17126       if( out==0 ){
17127         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
17128                     azCmd[1]);
17129       }else{
17130         int szChng;
17131         void *pChng;
17132         if( azCmd[0][0]=='c' ){
17133           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
17134         }else{
17135           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
17136         }
17137         if( rc ){
17138           printf("Error: error code %d\n", rc);
17139           rc = 0;
17140         }
17141         if( pChng
17142           && fwrite(pChng, szChng, 1, out)!=1 ){
17143           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
17144                   szChng);
17145         }
17146         sqlite3_free(pChng);
17147         fclose(out);
17148       }
17149     }else
17150 
17151     /* .session close
17152     ** Close the identified session
17153     */
17154     if( strcmp(azCmd[0], "close")==0 ){
17155       if( nCmd!=1 ) goto session_syntax_error;
17156       if( p->nSession ){
17157         session_close(pSession);
17158         p->aSession[iSes] = p->aSession[--p->nSession];
17159       }
17160     }else
17161 
17162     /* .session enable ?BOOLEAN?
17163     ** Query or set the enable flag
17164     */
17165     if( strcmp(azCmd[0], "enable")==0 ){
17166       int ii;
17167       if( nCmd>2 ) goto session_syntax_error;
17168       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17169       if( p->nSession ){
17170         ii = sqlite3session_enable(pSession->p, ii);
17171         utf8_printf(p->out, "session %s enable flag = %d\n",
17172                     pSession->zName, ii);
17173       }
17174     }else
17175 
17176     /* .session filter GLOB ....
17177     ** Set a list of GLOB patterns of table names to be excluded.
17178     */
17179     if( strcmp(azCmd[0], "filter")==0 ){
17180       int ii, nByte;
17181       if( nCmd<2 ) goto session_syntax_error;
17182       if( p->nSession ){
17183         for(ii=0; ii<pSession->nFilter; ii++){
17184           sqlite3_free(pSession->azFilter[ii]);
17185         }
17186         sqlite3_free(pSession->azFilter);
17187         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
17188         pSession->azFilter = sqlite3_malloc( nByte );
17189         if( pSession->azFilter==0 ){
17190           raw_printf(stderr, "Error: out or memory\n");
17191           exit(1);
17192         }
17193         for(ii=1; ii<nCmd; ii++){
17194           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
17195         }
17196         pSession->nFilter = ii-1;
17197       }
17198     }else
17199 
17200     /* .session indirect ?BOOLEAN?
17201     ** Query or set the indirect flag
17202     */
17203     if( strcmp(azCmd[0], "indirect")==0 ){
17204       int ii;
17205       if( nCmd>2 ) goto session_syntax_error;
17206       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17207       if( p->nSession ){
17208         ii = sqlite3session_indirect(pSession->p, ii);
17209         utf8_printf(p->out, "session %s indirect flag = %d\n",
17210                     pSession->zName, ii);
17211       }
17212     }else
17213 
17214     /* .session isempty
17215     ** Determine if the session is empty
17216     */
17217     if( strcmp(azCmd[0], "isempty")==0 ){
17218       int ii;
17219       if( nCmd!=1 ) goto session_syntax_error;
17220       if( p->nSession ){
17221         ii = sqlite3session_isempty(pSession->p);
17222         utf8_printf(p->out, "session %s isempty flag = %d\n",
17223                     pSession->zName, ii);
17224       }
17225     }else
17226 
17227     /* .session list
17228     ** List all currently open sessions
17229     */
17230     if( strcmp(azCmd[0],"list")==0 ){
17231       for(i=0; i<p->nSession; i++){
17232         utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
17233       }
17234     }else
17235 
17236     /* .session open DB NAME
17237     ** Open a new session called NAME on the attached database DB.
17238     ** DB is normally "main".
17239     */
17240     if( strcmp(azCmd[0],"open")==0 ){
17241       char *zName;
17242       if( nCmd!=3 ) goto session_syntax_error;
17243       zName = azCmd[2];
17244       if( zName[0]==0 ) goto session_syntax_error;
17245       for(i=0; i<p->nSession; i++){
17246         if( strcmp(p->aSession[i].zName,zName)==0 ){
17247           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
17248           goto meta_command_exit;
17249         }
17250       }
17251       if( p->nSession>=ArraySize(p->aSession) ){
17252         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
17253         goto meta_command_exit;
17254       }
17255       pSession = &p->aSession[p->nSession];
17256       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
17257       if( rc ){
17258         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
17259         rc = 0;
17260         goto meta_command_exit;
17261       }
17262       pSession->nFilter = 0;
17263       sqlite3session_table_filter(pSession->p, session_filter, pSession);
17264       p->nSession++;
17265       pSession->zName = sqlite3_mprintf("%s", zName);
17266     }else
17267     /* If no command name matches, show a syntax error */
17268     session_syntax_error:
17269     showHelp(p->out, "session");
17270   }else
17271 #endif
17272 
17273 #ifdef SQLITE_DEBUG
17274   /* Undocumented commands for internal testing.  Subject to change
17275   ** without notice. */
17276   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
17277     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
17278       int i, v;
17279       for(i=1; i<nArg; i++){
17280         v = booleanValue(azArg[i]);
17281         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
17282       }
17283     }
17284     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
17285       int i; sqlite3_int64 v;
17286       for(i=1; i<nArg; i++){
17287         char zBuf[200];
17288         v = integerValue(azArg[i]);
17289         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
17290         utf8_printf(p->out, "%s", zBuf);
17291       }
17292     }
17293   }else
17294 #endif
17295 
17296   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
17297     int bIsInit = 0;         /* True to initialize the SELFTEST table */
17298     int bVerbose = 0;        /* Verbose output */
17299     int bSelftestExists;     /* True if SELFTEST already exists */
17300     int i, k;                /* Loop counters */
17301     int nTest = 0;           /* Number of tests runs */
17302     int nErr = 0;            /* Number of errors seen */
17303     ShellText str;           /* Answer for a query */
17304     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
17305 
17306     open_db(p,0);
17307     for(i=1; i<nArg; i++){
17308       const char *z = azArg[i];
17309       if( z[0]=='-' && z[1]=='-' ) z++;
17310       if( strcmp(z,"-init")==0 ){
17311         bIsInit = 1;
17312       }else
17313       if( strcmp(z,"-v")==0 ){
17314         bVerbose++;
17315       }else
17316       {
17317         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17318                     azArg[i], azArg[0]);
17319         raw_printf(stderr, "Should be one of: --init -v\n");
17320         rc = 1;
17321         goto meta_command_exit;
17322       }
17323     }
17324     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
17325            != SQLITE_OK ){
17326       bSelftestExists = 0;
17327     }else{
17328       bSelftestExists = 1;
17329     }
17330     if( bIsInit ){
17331       createSelftestTable(p);
17332       bSelftestExists = 1;
17333     }
17334     initText(&str);
17335     appendText(&str, "x", 0);
17336     for(k=bSelftestExists; k>=0; k--){
17337       if( k==1 ){
17338         rc = sqlite3_prepare_v2(p->db,
17339             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
17340             -1, &pStmt, 0);
17341       }else{
17342         rc = sqlite3_prepare_v2(p->db,
17343           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
17344           "      (1,'run','PRAGMA integrity_check','ok')",
17345           -1, &pStmt, 0);
17346       }
17347       if( rc ){
17348         raw_printf(stderr, "Error querying the selftest table\n");
17349         rc = 1;
17350         sqlite3_finalize(pStmt);
17351         goto meta_command_exit;
17352       }
17353       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
17354         int tno = sqlite3_column_int(pStmt, 0);
17355         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
17356         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
17357         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
17358 
17359         k = 0;
17360         if( bVerbose>0 ){
17361           char *zQuote = sqlite3_mprintf("%q", zSql);
17362           printf("%d: %s %s\n", tno, zOp, zSql);
17363           sqlite3_free(zQuote);
17364         }
17365         if( strcmp(zOp,"memo")==0 ){
17366           utf8_printf(p->out, "%s\n", zSql);
17367         }else
17368         if( strcmp(zOp,"run")==0 ){
17369           char *zErrMsg = 0;
17370           str.n = 0;
17371           str.z[0] = 0;
17372           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
17373           nTest++;
17374           if( bVerbose ){
17375             utf8_printf(p->out, "Result: %s\n", str.z);
17376           }
17377           if( rc || zErrMsg ){
17378             nErr++;
17379             rc = 1;
17380             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
17381             sqlite3_free(zErrMsg);
17382           }else if( strcmp(zAns,str.z)!=0 ){
17383             nErr++;
17384             rc = 1;
17385             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
17386             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
17387           }
17388         }else
17389         {
17390           utf8_printf(stderr,
17391             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
17392           rc = 1;
17393           break;
17394         }
17395       } /* End loop over rows of content from SELFTEST */
17396       sqlite3_finalize(pStmt);
17397     } /* End loop over k */
17398     freeText(&str);
17399     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
17400   }else
17401 
17402   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
17403     if( nArg<2 || nArg>3 ){
17404       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
17405       rc = 1;
17406     }
17407     if( nArg>=2 ){
17408       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
17409                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
17410     }
17411     if( nArg>=3 ){
17412       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
17413                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
17414     }
17415   }else
17416 
17417   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
17418     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
17419     int i;                   /* Loop counter */
17420     int bSchema = 0;         /* Also hash the schema */
17421     int bSeparate = 0;       /* Hash each table separately */
17422     int iSize = 224;         /* Hash algorithm to use */
17423     int bDebug = 0;          /* Only show the query that would have run */
17424     sqlite3_stmt *pStmt;     /* For querying tables names */
17425     char *zSql;              /* SQL to be run */
17426     char *zSep;              /* Separator */
17427     ShellText sSql;          /* Complete SQL for the query to run the hash */
17428     ShellText sQuery;        /* Set of queries used to read all content */
17429     open_db(p, 0);
17430     for(i=1; i<nArg; i++){
17431       const char *z = azArg[i];
17432       if( z[0]=='-' ){
17433         z++;
17434         if( z[0]=='-' ) z++;
17435         if( strcmp(z,"schema")==0 ){
17436           bSchema = 1;
17437         }else
17438         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
17439          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
17440         ){
17441           iSize = atoi(&z[5]);
17442         }else
17443         if( strcmp(z,"debug")==0 ){
17444           bDebug = 1;
17445         }else
17446         {
17447           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17448                       azArg[i], azArg[0]);
17449           showHelp(p->out, azArg[0]);
17450           rc = 1;
17451           goto meta_command_exit;
17452         }
17453       }else if( zLike ){
17454         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
17455         rc = 1;
17456         goto meta_command_exit;
17457       }else{
17458         zLike = z;
17459         bSeparate = 1;
17460         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
17461       }
17462     }
17463     if( bSchema ){
17464       zSql = "SELECT lower(name) FROM sqlite_master"
17465              " WHERE type='table' AND coalesce(rootpage,0)>1"
17466              " UNION ALL SELECT 'sqlite_master'"
17467              " ORDER BY 1 collate nocase";
17468     }else{
17469       zSql = "SELECT lower(name) FROM sqlite_master"
17470              " WHERE type='table' AND coalesce(rootpage,0)>1"
17471              " AND name NOT LIKE 'sqlite_%'"
17472              " ORDER BY 1 collate nocase";
17473     }
17474     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17475     initText(&sQuery);
17476     initText(&sSql);
17477     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
17478     zSep = "VALUES(";
17479     while( SQLITE_ROW==sqlite3_step(pStmt) ){
17480       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
17481       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
17482       if( strncmp(zTab, "sqlite_",7)!=0 ){
17483         appendText(&sQuery,"SELECT * FROM ", 0);
17484         appendText(&sQuery,zTab,'"');
17485         appendText(&sQuery," NOT INDEXED;", 0);
17486       }else if( strcmp(zTab, "sqlite_master")==0 ){
17487         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
17488                            " ORDER BY name;", 0);
17489       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
17490         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
17491                            " ORDER BY name;", 0);
17492       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
17493         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
17494                            " ORDER BY tbl,idx;", 0);
17495       }else if( strcmp(zTab, "sqlite_stat4")==0 ){
17496         appendText(&sQuery, "SELECT * FROM ", 0);
17497         appendText(&sQuery, zTab, 0);
17498         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
17499       }
17500       appendText(&sSql, zSep, 0);
17501       appendText(&sSql, sQuery.z, '\'');
17502       sQuery.n = 0;
17503       appendText(&sSql, ",", 0);
17504       appendText(&sSql, zTab, '\'');
17505       zSep = "),(";
17506     }
17507     sqlite3_finalize(pStmt);
17508     if( bSeparate ){
17509       zSql = sqlite3_mprintf(
17510           "%s))"
17511           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
17512           "   FROM [sha3sum$query]",
17513           sSql.z, iSize);
17514     }else{
17515       zSql = sqlite3_mprintf(
17516           "%s))"
17517           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
17518           "   FROM [sha3sum$query]",
17519           sSql.z, iSize);
17520     }
17521     freeText(&sQuery);
17522     freeText(&sSql);
17523     if( bDebug ){
17524       utf8_printf(p->out, "%s\n", zSql);
17525     }else{
17526       shell_exec(p, zSql, 0);
17527     }
17528     sqlite3_free(zSql);
17529   }else
17530 
17531 #ifndef SQLITE_NOHAVE_SYSTEM
17532   if( c=='s'
17533    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
17534   ){
17535     char *zCmd;
17536     int i, x;
17537     if( nArg<2 ){
17538       raw_printf(stderr, "Usage: .system COMMAND\n");
17539       rc = 1;
17540       goto meta_command_exit;
17541     }
17542     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
17543     for(i=2; i<nArg; i++){
17544       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
17545                              zCmd, azArg[i]);
17546     }
17547     x = system(zCmd);
17548     sqlite3_free(zCmd);
17549     if( x ) raw_printf(stderr, "System command returns %d\n", x);
17550   }else
17551 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
17552 
17553   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
17554     static const char *azBool[] = { "off", "on", "trigger", "full"};
17555     int i;
17556     if( nArg!=1 ){
17557       raw_printf(stderr, "Usage: .show\n");
17558       rc = 1;
17559       goto meta_command_exit;
17560     }
17561     utf8_printf(p->out, "%12.12s: %s\n","echo",
17562                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
17563     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
17564     utf8_printf(p->out, "%12.12s: %s\n","explain",
17565          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
17566     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
17567     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
17568     utf8_printf(p->out, "%12.12s: ", "nullvalue");
17569       output_c_string(p->out, p->nullValue);
17570       raw_printf(p->out, "\n");
17571     utf8_printf(p->out,"%12.12s: %s\n","output",
17572             strlen30(p->outfile) ? p->outfile : "stdout");
17573     utf8_printf(p->out,"%12.12s: ", "colseparator");
17574       output_c_string(p->out, p->colSeparator);
17575       raw_printf(p->out, "\n");
17576     utf8_printf(p->out,"%12.12s: ", "rowseparator");
17577       output_c_string(p->out, p->rowSeparator);
17578       raw_printf(p->out, "\n");
17579     utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
17580     utf8_printf(p->out, "%12.12s: ", "width");
17581     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
17582       raw_printf(p->out, "%d ", p->colWidth[i]);
17583     }
17584     raw_printf(p->out, "\n");
17585     utf8_printf(p->out, "%12.12s: %s\n", "filename",
17586                 p->zDbFilename ? p->zDbFilename : "");
17587   }else
17588 
17589   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
17590     if( nArg==2 ){
17591       p->statsOn = (u8)booleanValue(azArg[1]);
17592     }else if( nArg==1 ){
17593       display_stats(p->db, p, 0);
17594     }else{
17595       raw_printf(stderr, "Usage: .stats ?on|off?\n");
17596       rc = 1;
17597     }
17598   }else
17599 
17600   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
17601    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
17602                  || strncmp(azArg[0], "indexes", n)==0) )
17603   ){
17604     sqlite3_stmt *pStmt;
17605     char **azResult;
17606     int nRow, nAlloc;
17607     int ii;
17608     ShellText s;
17609     initText(&s);
17610     open_db(p, 0);
17611     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
17612     if( rc ){
17613       sqlite3_finalize(pStmt);
17614       return shellDatabaseError(p->db);
17615     }
17616 
17617     if( nArg>2 && c=='i' ){
17618       /* It is an historical accident that the .indexes command shows an error
17619       ** when called with the wrong number of arguments whereas the .tables
17620       ** command does not. */
17621       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
17622       rc = 1;
17623       sqlite3_finalize(pStmt);
17624       goto meta_command_exit;
17625     }
17626     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
17627       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
17628       if( zDbName==0 ) continue;
17629       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
17630       if( sqlite3_stricmp(zDbName, "main")==0 ){
17631         appendText(&s, "SELECT name FROM ", 0);
17632       }else{
17633         appendText(&s, "SELECT ", 0);
17634         appendText(&s, zDbName, '\'');
17635         appendText(&s, "||'.'||name FROM ", 0);
17636       }
17637       appendText(&s, zDbName, '"');
17638       appendText(&s, ".sqlite_master ", 0);
17639       if( c=='t' ){
17640         appendText(&s," WHERE type IN ('table','view')"
17641                       "   AND name NOT LIKE 'sqlite_%'"
17642                       "   AND name LIKE ?1", 0);
17643       }else{
17644         appendText(&s," WHERE type='index'"
17645                       "   AND tbl_name LIKE ?1", 0);
17646       }
17647     }
17648     rc = sqlite3_finalize(pStmt);
17649     appendText(&s, " ORDER BY 1", 0);
17650     rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
17651     freeText(&s);
17652     if( rc ) return shellDatabaseError(p->db);
17653 
17654     /* Run the SQL statement prepared by the above block. Store the results
17655     ** as an array of nul-terminated strings in azResult[].  */
17656     nRow = nAlloc = 0;
17657     azResult = 0;
17658     if( nArg>1 ){
17659       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
17660     }else{
17661       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
17662     }
17663     while( sqlite3_step(pStmt)==SQLITE_ROW ){
17664       if( nRow>=nAlloc ){
17665         char **azNew;
17666         int n2 = nAlloc*2 + 10;
17667         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
17668         if( azNew==0 ) shell_out_of_memory();
17669         nAlloc = n2;
17670         azResult = azNew;
17671       }
17672       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
17673       if( 0==azResult[nRow] ) shell_out_of_memory();
17674       nRow++;
17675     }
17676     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
17677       rc = shellDatabaseError(p->db);
17678     }
17679 
17680     /* Pretty-print the contents of array azResult[] to the output */
17681     if( rc==0 && nRow>0 ){
17682       int len, maxlen = 0;
17683       int i, j;
17684       int nPrintCol, nPrintRow;
17685       for(i=0; i<nRow; i++){
17686         len = strlen30(azResult[i]);
17687         if( len>maxlen ) maxlen = len;
17688       }
17689       nPrintCol = 80/(maxlen+2);
17690       if( nPrintCol<1 ) nPrintCol = 1;
17691       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
17692       for(i=0; i<nPrintRow; i++){
17693         for(j=i; j<nRow; j+=nPrintRow){
17694           char *zSp = j<nPrintRow ? "" : "  ";
17695           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
17696                       azResult[j] ? azResult[j]:"");
17697         }
17698         raw_printf(p->out, "\n");
17699       }
17700     }
17701 
17702     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
17703     sqlite3_free(azResult);
17704   }else
17705 
17706   /* Begin redirecting output to the file "testcase-out.txt" */
17707   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
17708     output_reset(p);
17709     p->out = output_file_open("testcase-out.txt", 0);
17710     if( p->out==0 ){
17711       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
17712     }
17713     if( nArg>=2 ){
17714       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
17715     }else{
17716       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
17717     }
17718   }else
17719 
17720 #ifndef SQLITE_UNTESTABLE
17721   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
17722     static const struct {
17723        const char *zCtrlName;   /* Name of a test-control option */
17724        int ctrlCode;            /* Integer code for that option */
17725        const char *zUsage;      /* Usage notes */
17726     } aCtrl[] = {
17727       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
17728       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
17729     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
17730     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
17731       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
17732       { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
17733     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
17734       { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
17735       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"   },
17736       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
17737       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
17738       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
17739 #ifdef YYCOVERAGE
17740       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
17741 #endif
17742       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
17743       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
17744       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
17745       { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
17746       { "reserve",            SQLITE_TESTCTRL_RESERVE,      "BYTES-OF-RESERVE"},
17747     };
17748     int testctrl = -1;
17749     int iCtrl = -1;
17750     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
17751     int isOk = 0;
17752     int i, n2;
17753     const char *zCmd = 0;
17754 
17755     open_db(p, 0);
17756     zCmd = nArg>=2 ? azArg[1] : "help";
17757 
17758     /* The argument can optionally begin with "-" or "--" */
17759     if( zCmd[0]=='-' && zCmd[1] ){
17760       zCmd++;
17761       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
17762     }
17763 
17764     /* --help lists all test-controls */
17765     if( strcmp(zCmd,"help")==0 ){
17766       utf8_printf(p->out, "Available test-controls:\n");
17767       for(i=0; i<ArraySize(aCtrl); i++){
17768         utf8_printf(p->out, "  .testctrl %s %s\n",
17769                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
17770       }
17771       rc = 1;
17772       goto meta_command_exit;
17773     }
17774 
17775     /* convert testctrl text option to value. allow any unique prefix
17776     ** of the option name, or a numerical value. */
17777     n2 = strlen30(zCmd);
17778     for(i=0; i<ArraySize(aCtrl); i++){
17779       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
17780         if( testctrl<0 ){
17781           testctrl = aCtrl[i].ctrlCode;
17782           iCtrl = i;
17783         }else{
17784           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
17785                               "Use \".testctrl --help\" for help\n", zCmd);
17786           rc = 1;
17787           goto meta_command_exit;
17788         }
17789       }
17790     }
17791     if( testctrl<0 ){
17792       utf8_printf(stderr,"Error: unknown test-control: %s\n"
17793                          "Use \".testctrl --help\" for help\n", zCmd);
17794     }else{
17795       switch(testctrl){
17796 
17797         /* sqlite3_test_control(int, db, int) */
17798         case SQLITE_TESTCTRL_OPTIMIZATIONS:
17799         case SQLITE_TESTCTRL_RESERVE:
17800           if( nArg==3 ){
17801             int opt = (int)strtol(azArg[2], 0, 0);
17802             rc2 = sqlite3_test_control(testctrl, p->db, opt);
17803             isOk = 3;
17804           }
17805           break;
17806 
17807         /* sqlite3_test_control(int) */
17808         case SQLITE_TESTCTRL_PRNG_SAVE:
17809         case SQLITE_TESTCTRL_PRNG_RESTORE:
17810         case SQLITE_TESTCTRL_PRNG_RESET:
17811         case SQLITE_TESTCTRL_BYTEORDER:
17812           if( nArg==2 ){
17813             rc2 = sqlite3_test_control(testctrl);
17814             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
17815           }
17816           break;
17817 
17818         /* sqlite3_test_control(int, uint) */
17819         case SQLITE_TESTCTRL_PENDING_BYTE:
17820           if( nArg==3 ){
17821             unsigned int opt = (unsigned int)integerValue(azArg[2]);
17822             rc2 = sqlite3_test_control(testctrl, opt);
17823             isOk = 3;
17824           }
17825           break;
17826 
17827         /* sqlite3_test_control(int, int, sqlite3*) */
17828         case SQLITE_TESTCTRL_PRNG_SEED:
17829           if( nArg==3 || nArg==4 ){
17830             int ii = (int)integerValue(azArg[2]);
17831             sqlite3 *db;
17832             if( ii==0 && strcmp(azArg[2],"random")==0 ){
17833               sqlite3_randomness(sizeof(ii),&ii);
17834               printf("-- random seed: %d\n", ii);
17835             }
17836             if( nArg==3 ){
17837               db = 0;
17838             }else{
17839               db = p->db;
17840               /* Make sure the schema has been loaded */
17841               sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
17842             }
17843             rc2 = sqlite3_test_control(testctrl, ii, db);
17844             isOk = 3;
17845           }
17846           break;
17847 
17848         /* sqlite3_test_control(int, int) */
17849         case SQLITE_TESTCTRL_ASSERT:
17850         case SQLITE_TESTCTRL_ALWAYS:
17851         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
17852           if( nArg==3 ){
17853             int opt = booleanValue(azArg[2]);
17854             rc2 = sqlite3_test_control(testctrl, opt);
17855             isOk = 1;
17856           }
17857           break;
17858 
17859         /* sqlite3_test_control(int, int) */
17860         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
17861         case SQLITE_TESTCTRL_NEVER_CORRUPT:
17862           if( nArg==3 ){
17863             int opt = booleanValue(azArg[2]);
17864             rc2 = sqlite3_test_control(testctrl, opt);
17865             isOk = 3;
17866           }
17867           break;
17868 
17869         case SQLITE_TESTCTRL_IMPOSTER:
17870           if( nArg==5 ){
17871             rc2 = sqlite3_test_control(testctrl, p->db,
17872                           azArg[2],
17873                           integerValue(azArg[3]),
17874                           integerValue(azArg[4]));
17875             isOk = 3;
17876           }
17877           break;
17878 
17879 #ifdef YYCOVERAGE
17880         case SQLITE_TESTCTRL_PARSER_COVERAGE:
17881           if( nArg==2 ){
17882             sqlite3_test_control(testctrl, p->out);
17883             isOk = 3;
17884           }
17885 #endif
17886       }
17887     }
17888     if( isOk==0 && iCtrl>=0 ){
17889       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
17890       rc = 1;
17891     }else if( isOk==1 ){
17892       raw_printf(p->out, "%d\n", rc2);
17893     }else if( isOk==2 ){
17894       raw_printf(p->out, "0x%08x\n", rc2);
17895     }
17896   }else
17897 #endif /* !defined(SQLITE_UNTESTABLE) */
17898 
17899   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
17900     open_db(p, 0);
17901     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
17902   }else
17903 
17904   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
17905     if( nArg==2 ){
17906       enableTimer = booleanValue(azArg[1]);
17907       if( enableTimer && !HAS_TIMER ){
17908         raw_printf(stderr, "Error: timer not available on this system.\n");
17909         enableTimer = 0;
17910       }
17911     }else{
17912       raw_printf(stderr, "Usage: .timer on|off\n");
17913       rc = 1;
17914     }
17915   }else
17916 
17917 #ifndef SQLITE_OMIT_TRACE
17918   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
17919     int mType = 0;
17920     int jj;
17921     open_db(p, 0);
17922     for(jj=1; jj<nArg; jj++){
17923       const char *z = azArg[jj];
17924       if( z[0]=='-' ){
17925         if( optionMatch(z, "expanded") ){
17926           p->eTraceType = SHELL_TRACE_EXPANDED;
17927         }
17928 #ifdef SQLITE_ENABLE_NORMALIZE
17929         else if( optionMatch(z, "normalized") ){
17930           p->eTraceType = SHELL_TRACE_NORMALIZED;
17931         }
17932 #endif
17933         else if( optionMatch(z, "plain") ){
17934           p->eTraceType = SHELL_TRACE_PLAIN;
17935         }
17936         else if( optionMatch(z, "profile") ){
17937           mType |= SQLITE_TRACE_PROFILE;
17938         }
17939         else if( optionMatch(z, "row") ){
17940           mType |= SQLITE_TRACE_ROW;
17941         }
17942         else if( optionMatch(z, "stmt") ){
17943           mType |= SQLITE_TRACE_STMT;
17944         }
17945         else if( optionMatch(z, "close") ){
17946           mType |= SQLITE_TRACE_CLOSE;
17947         }
17948         else {
17949           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
17950           rc = 1;
17951           goto meta_command_exit;
17952         }
17953       }else{
17954         output_file_close(p->traceOut);
17955         p->traceOut = output_file_open(azArg[1], 0);
17956       }
17957     }
17958     if( p->traceOut==0 ){
17959       sqlite3_trace_v2(p->db, 0, 0, 0);
17960     }else{
17961       if( mType==0 ) mType = SQLITE_TRACE_STMT;
17962       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
17963     }
17964   }else
17965 #endif /* !defined(SQLITE_OMIT_TRACE) */
17966 
17967 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
17968   if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
17969     int ii;
17970     int lenOpt;
17971     char *zOpt;
17972     if( nArg<2 ){
17973       raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
17974       rc = 1;
17975       goto meta_command_exit;
17976     }
17977     open_db(p, 0);
17978     zOpt = azArg[1];
17979     if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
17980     lenOpt = (int)strlen(zOpt);
17981     if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
17982       assert( azArg[nArg]==0 );
17983       sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
17984     }else{
17985       for(ii=1; ii<nArg; ii++){
17986         sqlite3_create_module(p->db, azArg[ii], 0, 0);
17987       }
17988     }
17989   }else
17990 #endif
17991 
17992 #if SQLITE_USER_AUTHENTICATION
17993   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
17994     if( nArg<2 ){
17995       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
17996       rc = 1;
17997       goto meta_command_exit;
17998     }
17999     open_db(p, 0);
18000     if( strcmp(azArg[1],"login")==0 ){
18001       if( nArg!=4 ){
18002         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
18003         rc = 1;
18004         goto meta_command_exit;
18005       }
18006       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
18007                                      strlen30(azArg[3]));
18008       if( rc ){
18009         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
18010         rc = 1;
18011       }
18012     }else if( strcmp(azArg[1],"add")==0 ){
18013       if( nArg!=5 ){
18014         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
18015         rc = 1;
18016         goto meta_command_exit;
18017       }
18018       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18019                             booleanValue(azArg[4]));
18020       if( rc ){
18021         raw_printf(stderr, "User-Add failed: %d\n", rc);
18022         rc = 1;
18023       }
18024     }else if( strcmp(azArg[1],"edit")==0 ){
18025       if( nArg!=5 ){
18026         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
18027         rc = 1;
18028         goto meta_command_exit;
18029       }
18030       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18031                               booleanValue(azArg[4]));
18032       if( rc ){
18033         raw_printf(stderr, "User-Edit failed: %d\n", rc);
18034         rc = 1;
18035       }
18036     }else if( strcmp(azArg[1],"delete")==0 ){
18037       if( nArg!=3 ){
18038         raw_printf(stderr, "Usage: .user delete USER\n");
18039         rc = 1;
18040         goto meta_command_exit;
18041       }
18042       rc = sqlite3_user_delete(p->db, azArg[2]);
18043       if( rc ){
18044         raw_printf(stderr, "User-Delete failed: %d\n", rc);
18045         rc = 1;
18046       }
18047     }else{
18048       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
18049       rc = 1;
18050       goto meta_command_exit;
18051     }
18052   }else
18053 #endif /* SQLITE_USER_AUTHENTICATION */
18054 
18055   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
18056     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
18057         sqlite3_libversion(), sqlite3_sourceid());
18058 #if SQLITE_HAVE_ZLIB
18059     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
18060 #endif
18061 #define CTIMEOPT_VAL_(opt) #opt
18062 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
18063 #if defined(__clang__) && defined(__clang_major__)
18064     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
18065                     CTIMEOPT_VAL(__clang_minor__) "."
18066                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
18067 #elif defined(_MSC_VER)
18068     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
18069 #elif defined(__GNUC__) && defined(__VERSION__)
18070     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
18071 #endif
18072   }else
18073 
18074   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
18075     const char *zDbName = nArg==2 ? azArg[1] : "main";
18076     sqlite3_vfs *pVfs = 0;
18077     if( p->db ){
18078       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
18079       if( pVfs ){
18080         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
18081         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
18082         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
18083         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18084       }
18085     }
18086   }else
18087 
18088   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
18089     sqlite3_vfs *pVfs;
18090     sqlite3_vfs *pCurrent = 0;
18091     if( p->db ){
18092       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
18093     }
18094     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
18095       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
18096            pVfs==pCurrent ? "  <--- CURRENT" : "");
18097       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
18098       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
18099       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18100       if( pVfs->pNext ){
18101         raw_printf(p->out, "-----------------------------------\n");
18102       }
18103     }
18104   }else
18105 
18106   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
18107     const char *zDbName = nArg==2 ? azArg[1] : "main";
18108     char *zVfsName = 0;
18109     if( p->db ){
18110       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
18111       if( zVfsName ){
18112         utf8_printf(p->out, "%s\n", zVfsName);
18113         sqlite3_free(zVfsName);
18114       }
18115     }
18116   }else
18117 
18118 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
18119   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
18120     sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
18121   }else
18122 #endif
18123 
18124   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
18125     int j;
18126     assert( nArg<=ArraySize(azArg) );
18127     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
18128       p->colWidth[j-1] = (int)integerValue(azArg[j]);
18129     }
18130   }else
18131 
18132   {
18133     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
18134       " \"%s\". Enter \".help\" for help\n", azArg[0]);
18135     rc = 1;
18136   }
18137 
18138 meta_command_exit:
18139   if( p->outCount ){
18140     p->outCount--;
18141     if( p->outCount==0 ) output_reset(p);
18142   }
18143   return rc;
18144 }
18145 
18146 /*
18147 ** Return TRUE if a semicolon occurs anywhere in the first N characters
18148 ** of string z[].
18149 */
18150 static int line_contains_semicolon(const char *z, int N){
18151   int i;
18152   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
18153   return 0;
18154 }
18155 
18156 /*
18157 ** Test to see if a line consists entirely of whitespace.
18158 */
18159 static int _all_whitespace(const char *z){
18160   for(; *z; z++){
18161     if( IsSpace(z[0]) ) continue;
18162     if( *z=='/' && z[1]=='*' ){
18163       z += 2;
18164       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
18165       if( *z==0 ) return 0;
18166       z++;
18167       continue;
18168     }
18169     if( *z=='-' && z[1]=='-' ){
18170       z += 2;
18171       while( *z && *z!='\n' ){ z++; }
18172       if( *z==0 ) return 1;
18173       continue;
18174     }
18175     return 0;
18176   }
18177   return 1;
18178 }
18179 
18180 /*
18181 ** Return TRUE if the line typed in is an SQL command terminator other
18182 ** than a semi-colon.  The SQL Server style "go" command is understood
18183 ** as is the Oracle "/".
18184 */
18185 static int line_is_command_terminator(const char *zLine){
18186   while( IsSpace(zLine[0]) ){ zLine++; };
18187   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
18188     return 1;  /* Oracle */
18189   }
18190   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
18191          && _all_whitespace(&zLine[2]) ){
18192     return 1;  /* SQL Server */
18193   }
18194   return 0;
18195 }
18196 
18197 /*
18198 ** We need a default sqlite3_complete() implementation to use in case
18199 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
18200 ** any arbitrary text is a complete SQL statement.  This is not very
18201 ** user-friendly, but it does seem to work.
18202 */
18203 #ifdef SQLITE_OMIT_COMPLETE
18204 #define sqlite3_complete(x) 1
18205 #endif
18206 
18207 /*
18208 ** Return true if zSql is a complete SQL statement.  Return false if it
18209 ** ends in the middle of a string literal or C-style comment.
18210 */
18211 static int line_is_complete(char *zSql, int nSql){
18212   int rc;
18213   if( zSql==0 ) return 1;
18214   zSql[nSql] = ';';
18215   zSql[nSql+1] = 0;
18216   rc = sqlite3_complete(zSql);
18217   zSql[nSql] = 0;
18218   return rc;
18219 }
18220 
18221 /*
18222 ** Run a single line of SQL.  Return the number of errors.
18223 */
18224 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
18225   int rc;
18226   char *zErrMsg = 0;
18227 
18228   open_db(p, 0);
18229   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
18230   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
18231   BEGIN_TIMER;
18232   rc = shell_exec(p, zSql, &zErrMsg);
18233   END_TIMER;
18234   if( rc || zErrMsg ){
18235     char zPrefix[100];
18236     if( in!=0 || !stdin_is_interactive ){
18237       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
18238                        "Error: near line %d:", startline);
18239     }else{
18240       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
18241     }
18242     if( zErrMsg!=0 ){
18243       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
18244       sqlite3_free(zErrMsg);
18245       zErrMsg = 0;
18246     }else{
18247       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
18248     }
18249     return 1;
18250   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
18251     raw_printf(p->out, "changes: %3d   total_changes: %d\n",
18252             sqlite3_changes(p->db), sqlite3_total_changes(p->db));
18253   }
18254   return 0;
18255 }
18256 
18257 
18258 /*
18259 ** Read input from *in and process it.  If *in==0 then input
18260 ** is interactive - the user is typing it it.  Otherwise, input
18261 ** is coming from a file or device.  A prompt is issued and history
18262 ** is saved only if input is interactive.  An interrupt signal will
18263 ** cause this routine to exit immediately, unless input is interactive.
18264 **
18265 ** Return the number of errors.
18266 */
18267 static int process_input(ShellState *p){
18268   char *zLine = 0;          /* A single input line */
18269   char *zSql = 0;           /* Accumulated SQL text */
18270   int nLine;                /* Length of current line */
18271   int nSql = 0;             /* Bytes of zSql[] used */
18272   int nAlloc = 0;           /* Allocated zSql[] space */
18273   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
18274   int rc;                   /* Error code */
18275   int errCnt = 0;           /* Number of errors seen */
18276   int startline = 0;        /* Line number for start of current input */
18277 
18278   p->lineno = 0;
18279   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
18280     fflush(p->out);
18281     zLine = one_input_line(p->in, zLine, nSql>0);
18282     if( zLine==0 ){
18283       /* End of input */
18284       if( p->in==0 && stdin_is_interactive ) printf("\n");
18285       break;
18286     }
18287     if( seenInterrupt ){
18288       if( p->in!=0 ) break;
18289       seenInterrupt = 0;
18290     }
18291     p->lineno++;
18292     if( nSql==0 && _all_whitespace(zLine) ){
18293       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18294       continue;
18295     }
18296     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
18297       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18298       if( zLine[0]=='.' ){
18299         rc = do_meta_command(zLine, p);
18300         if( rc==2 ){ /* exit requested */
18301           break;
18302         }else if( rc ){
18303           errCnt++;
18304         }
18305       }
18306       continue;
18307     }
18308     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
18309       memcpy(zLine,";",2);
18310     }
18311     nLine = strlen30(zLine);
18312     if( nSql+nLine+2>=nAlloc ){
18313       nAlloc = nSql+nLine+100;
18314       zSql = realloc(zSql, nAlloc);
18315       if( zSql==0 ) shell_out_of_memory();
18316     }
18317     nSqlPrior = nSql;
18318     if( nSql==0 ){
18319       int i;
18320       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
18321       assert( nAlloc>0 && zSql!=0 );
18322       memcpy(zSql, zLine+i, nLine+1-i);
18323       startline = p->lineno;
18324       nSql = nLine-i;
18325     }else{
18326       zSql[nSql++] = '\n';
18327       memcpy(zSql+nSql, zLine, nLine+1);
18328       nSql += nLine;
18329     }
18330     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
18331                 && sqlite3_complete(zSql) ){
18332       errCnt += runOneSqlLine(p, zSql, p->in, startline);
18333       nSql = 0;
18334       if( p->outCount ){
18335         output_reset(p);
18336         p->outCount = 0;
18337       }else{
18338         clearTempFile(p);
18339       }
18340     }else if( nSql && _all_whitespace(zSql) ){
18341       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
18342       nSql = 0;
18343     }
18344   }
18345   if( nSql && !_all_whitespace(zSql) ){
18346     errCnt += runOneSqlLine(p, zSql, p->in, startline);
18347   }
18348   free(zSql);
18349   free(zLine);
18350   return errCnt>0;
18351 }
18352 
18353 /*
18354 ** Return a pathname which is the user's home directory.  A
18355 ** 0 return indicates an error of some kind.
18356 */
18357 static char *find_home_dir(int clearFlag){
18358   static char *home_dir = NULL;
18359   if( clearFlag ){
18360     free(home_dir);
18361     home_dir = 0;
18362     return 0;
18363   }
18364   if( home_dir ) return home_dir;
18365 
18366 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
18367      && !defined(__RTP__) && !defined(_WRS_KERNEL)
18368   {
18369     struct passwd *pwent;
18370     uid_t uid = getuid();
18371     if( (pwent=getpwuid(uid)) != NULL) {
18372       home_dir = pwent->pw_dir;
18373     }
18374   }
18375 #endif
18376 
18377 #if defined(_WIN32_WCE)
18378   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
18379    */
18380   home_dir = "/";
18381 #else
18382 
18383 #if defined(_WIN32) || defined(WIN32)
18384   if (!home_dir) {
18385     home_dir = getenv("USERPROFILE");
18386   }
18387 #endif
18388 
18389   if (!home_dir) {
18390     home_dir = getenv("HOME");
18391   }
18392 
18393 #if defined(_WIN32) || defined(WIN32)
18394   if (!home_dir) {
18395     char *zDrive, *zPath;
18396     int n;
18397     zDrive = getenv("HOMEDRIVE");
18398     zPath = getenv("HOMEPATH");
18399     if( zDrive && zPath ){
18400       n = strlen30(zDrive) + strlen30(zPath) + 1;
18401       home_dir = malloc( n );
18402       if( home_dir==0 ) return 0;
18403       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
18404       return home_dir;
18405     }
18406     home_dir = "c:\\";
18407   }
18408 #endif
18409 
18410 #endif /* !_WIN32_WCE */
18411 
18412   if( home_dir ){
18413     int n = strlen30(home_dir) + 1;
18414     char *z = malloc( n );
18415     if( z ) memcpy(z, home_dir, n);
18416     home_dir = z;
18417   }
18418 
18419   return home_dir;
18420 }
18421 
18422 /*
18423 ** Read input from the file given by sqliterc_override.  Or if that
18424 ** parameter is NULL, take input from ~/.sqliterc
18425 **
18426 ** Returns the number of errors.
18427 */
18428 static void process_sqliterc(
18429   ShellState *p,                  /* Configuration data */
18430   const char *sqliterc_override   /* Name of config file. NULL to use default */
18431 ){
18432   char *home_dir = NULL;
18433   const char *sqliterc = sqliterc_override;
18434   char *zBuf = 0;
18435   FILE *inSaved = p->in;
18436   int savedLineno = p->lineno;
18437 
18438   if (sqliterc == NULL) {
18439     home_dir = find_home_dir(0);
18440     if( home_dir==0 ){
18441       raw_printf(stderr, "-- warning: cannot find home directory;"
18442                       " cannot read ~/.sqliterc\n");
18443       return;
18444     }
18445     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
18446     sqliterc = zBuf;
18447   }
18448   p->in = fopen(sqliterc,"rb");
18449   if( p->in ){
18450     if( stdin_is_interactive ){
18451       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
18452     }
18453     process_input(p);
18454     fclose(p->in);
18455   }
18456   p->in = inSaved;
18457   p->lineno = savedLineno;
18458   sqlite3_free(zBuf);
18459 }
18460 
18461 /*
18462 ** Show available command line options
18463 */
18464 static const char zOptions[] =
18465 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18466   "   -A ARGS...           run \".archive ARGS\" and exit\n"
18467 #endif
18468   "   -append              append the database to the end of the file\n"
18469   "   -ascii               set output mode to 'ascii'\n"
18470   "   -bail                stop after hitting an error\n"
18471   "   -batch               force batch I/O\n"
18472   "   -column              set output mode to 'column'\n"
18473   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
18474   "   -csv                 set output mode to 'csv'\n"
18475 #if defined(SQLITE_ENABLE_DESERIALIZE)
18476   "   -deserialize         open the database using sqlite3_deserialize()\n"
18477 #endif
18478   "   -echo                print commands before execution\n"
18479   "   -init FILENAME       read/process named file\n"
18480   "   -[no]header          turn headers on or off\n"
18481 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
18482   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
18483 #endif
18484   "   -help                show this message\n"
18485   "   -html                set output mode to HTML\n"
18486   "   -interactive         force interactive I/O\n"
18487   "   -line                set output mode to 'line'\n"
18488   "   -list                set output mode to 'list'\n"
18489   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
18490 #if defined(SQLITE_ENABLE_DESERIALIZE)
18491   "   -maxsize N           maximum size for a --deserialize database\n"
18492 #endif
18493   "   -memtrace            trace all memory allocations and deallocations\n"
18494   "   -mmap N              default mmap size set to N\n"
18495 #ifdef SQLITE_ENABLE_MULTIPLEX
18496   "   -multiplex           enable the multiplexor VFS\n"
18497 #endif
18498   "   -newline SEP         set output row separator. Default: '\\n'\n"
18499   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
18500   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
18501   "   -quote               set output mode to 'quote'\n"
18502   "   -readonly            open the database read-only\n"
18503   "   -separator SEP       set output column separator. Default: '|'\n"
18504 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
18505   "   -sorterref SIZE      sorter references threshold size\n"
18506 #endif
18507   "   -stats               print memory stats before each finalize\n"
18508   "   -version             show SQLite version\n"
18509   "   -vfs NAME            use NAME as the default VFS\n"
18510 #ifdef SQLITE_ENABLE_VFSTRACE
18511   "   -vfstrace            enable tracing of all VFS calls\n"
18512 #endif
18513 #ifdef SQLITE_HAVE_ZLIB
18514   "   -zip                 open the file as a ZIP Archive\n"
18515 #endif
18516 ;
18517 static void usage(int showDetail){
18518   utf8_printf(stderr,
18519       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
18520       "FILENAME is the name of an SQLite database. A new database is created\n"
18521       "if the file does not previously exist.\n", Argv0);
18522   if( showDetail ){
18523     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
18524   }else{
18525     raw_printf(stderr, "Use the -help option for additional information\n");
18526   }
18527   exit(1);
18528 }
18529 
18530 /*
18531 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
18532 ** error message if it is initialized.
18533 */
18534 static void verify_uninitialized(void){
18535   if( sqlite3_config(-1)==SQLITE_MISUSE ){
18536     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
18537                         " initialization.\n");
18538   }
18539 }
18540 
18541 /*
18542 ** Initialize the state information in data
18543 */
18544 static void main_init(ShellState *data) {
18545   memset(data, 0, sizeof(*data));
18546   data->normalMode = data->cMode = data->mode = MODE_List;
18547   data->autoExplain = 1;
18548   memcpy(data->colSeparator,SEP_Column, 2);
18549   memcpy(data->rowSeparator,SEP_Row, 2);
18550   data->showHeader = 0;
18551   data->shellFlgs = SHFLG_Lookaside;
18552   verify_uninitialized();
18553   sqlite3_config(SQLITE_CONFIG_URI, 1);
18554   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
18555   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
18556   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
18557   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
18558 }
18559 
18560 /*
18561 ** Output text to the console in a font that attracts extra attention.
18562 */
18563 #ifdef _WIN32
18564 static void printBold(const char *zText){
18565   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
18566   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
18567   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
18568   SetConsoleTextAttribute(out,
18569          FOREGROUND_RED|FOREGROUND_INTENSITY
18570   );
18571   printf("%s", zText);
18572   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
18573 }
18574 #else
18575 static void printBold(const char *zText){
18576   printf("\033[1m%s\033[0m", zText);
18577 }
18578 #endif
18579 
18580 /*
18581 ** Get the argument to an --option.  Throw an error and die if no argument
18582 ** is available.
18583 */
18584 static char *cmdline_option_value(int argc, char **argv, int i){
18585   if( i==argc ){
18586     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
18587             argv[0], argv[argc-1]);
18588     exit(1);
18589   }
18590   return argv[i];
18591 }
18592 
18593 #ifndef SQLITE_SHELL_IS_UTF8
18594 #  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
18595 #    define SQLITE_SHELL_IS_UTF8          (0)
18596 #  else
18597 #    define SQLITE_SHELL_IS_UTF8          (1)
18598 #  endif
18599 #endif
18600 
18601 #if SQLITE_SHELL_IS_UTF8
18602 int SQLITE_CDECL main(int argc, char **argv){
18603 #else
18604 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
18605   char **argv;
18606 #endif
18607   char *zErrMsg = 0;
18608   ShellState data;
18609   const char *zInitFile = 0;
18610   int i;
18611   int rc = 0;
18612   int warnInmemoryDb = 0;
18613   int readStdin = 1;
18614   int nCmd = 0;
18615   char **azCmd = 0;
18616   const char *zVfs = 0;           /* Value of -vfs command-line option */
18617 #if !SQLITE_SHELL_IS_UTF8
18618   char **argvToFree = 0;
18619   int argcToFree = 0;
18620 #endif
18621 
18622   setBinaryMode(stdin, 0);
18623   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
18624   stdin_is_interactive = isatty(0);
18625   stdout_is_console = isatty(1);
18626 
18627 #if !defined(_WIN32_WCE)
18628   if( getenv("SQLITE_DEBUG_BREAK") ){
18629     if( isatty(0) && isatty(2) ){
18630       fprintf(stderr,
18631           "attach debugger to process %d and press any key to continue.\n",
18632           GETPID());
18633       fgetc(stdin);
18634     }else{
18635 #if defined(_WIN32) || defined(WIN32)
18636       DebugBreak();
18637 #elif defined(SIGTRAP)
18638       raise(SIGTRAP);
18639 #endif
18640     }
18641   }
18642 #endif
18643 
18644 #if USE_SYSTEM_SQLITE+0!=1
18645   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
18646     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
18647             sqlite3_sourceid(), SQLITE_SOURCE_ID);
18648     exit(1);
18649   }
18650 #endif
18651   main_init(&data);
18652 
18653   /* On Windows, we must translate command-line arguments into UTF-8.
18654   ** The SQLite memory allocator subsystem has to be enabled in order to
18655   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
18656   ** subsequent sqlite3_config() calls will work.  So copy all results into
18657   ** memory that does not come from the SQLite memory allocator.
18658   */
18659 #if !SQLITE_SHELL_IS_UTF8
18660   sqlite3_initialize();
18661   argvToFree = malloc(sizeof(argv[0])*argc*2);
18662   argcToFree = argc;
18663   argv = argvToFree + argc;
18664   if( argv==0 ) shell_out_of_memory();
18665   for(i=0; i<argc; i++){
18666     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
18667     int n;
18668     if( z==0 ) shell_out_of_memory();
18669     n = (int)strlen(z);
18670     argv[i] = malloc( n+1 );
18671     if( argv[i]==0 ) shell_out_of_memory();
18672     memcpy(argv[i], z, n+1);
18673     argvToFree[i] = argv[i];
18674     sqlite3_free(z);
18675   }
18676   sqlite3_shutdown();
18677 #endif
18678 
18679   assert( argc>=1 && argv && argv[0] );
18680   Argv0 = argv[0];
18681 
18682   /* Make sure we have a valid signal handler early, before anything
18683   ** else is done.
18684   */
18685 #ifdef SIGINT
18686   signal(SIGINT, interrupt_handler);
18687 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
18688   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
18689 #endif
18690 
18691 #ifdef SQLITE_SHELL_DBNAME_PROC
18692   {
18693     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
18694     ** of a C-function that will provide the name of the database file.  Use
18695     ** this compile-time option to embed this shell program in larger
18696     ** applications. */
18697     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
18698     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
18699     warnInmemoryDb = 0;
18700   }
18701 #endif
18702 
18703   /* Do an initial pass through the command-line argument to locate
18704   ** the name of the database file, the name of the initialization file,
18705   ** the size of the alternative malloc heap,
18706   ** and the first command to execute.
18707   */
18708   verify_uninitialized();
18709   for(i=1; i<argc; i++){
18710     char *z;
18711     z = argv[i];
18712     if( z[0]!='-' ){
18713       if( data.zDbFilename==0 ){
18714         data.zDbFilename = z;
18715       }else{
18716         /* Excesss arguments are interpreted as SQL (or dot-commands) and
18717         ** mean that nothing is read from stdin */
18718         readStdin = 0;
18719         nCmd++;
18720         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
18721         if( azCmd==0 ) shell_out_of_memory();
18722         azCmd[nCmd-1] = z;
18723       }
18724     }
18725     if( z[1]=='-' ) z++;
18726     if( strcmp(z,"-separator")==0
18727      || strcmp(z,"-nullvalue")==0
18728      || strcmp(z,"-newline")==0
18729      || strcmp(z,"-cmd")==0
18730     ){
18731       (void)cmdline_option_value(argc, argv, ++i);
18732     }else if( strcmp(z,"-init")==0 ){
18733       zInitFile = cmdline_option_value(argc, argv, ++i);
18734     }else if( strcmp(z,"-batch")==0 ){
18735       /* Need to check for batch mode here to so we can avoid printing
18736       ** informational messages (like from process_sqliterc) before
18737       ** we do the actual processing of arguments later in a second pass.
18738       */
18739       stdin_is_interactive = 0;
18740     }else if( strcmp(z,"-heap")==0 ){
18741 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
18742       const char *zSize;
18743       sqlite3_int64 szHeap;
18744 
18745       zSize = cmdline_option_value(argc, argv, ++i);
18746       szHeap = integerValue(zSize);
18747       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
18748       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
18749 #else
18750       (void)cmdline_option_value(argc, argv, ++i);
18751 #endif
18752     }else if( strcmp(z,"-pagecache")==0 ){
18753       int n, sz;
18754       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
18755       if( sz>70000 ) sz = 70000;
18756       if( sz<0 ) sz = 0;
18757       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
18758       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
18759                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
18760       data.shellFlgs |= SHFLG_Pagecache;
18761     }else if( strcmp(z,"-lookaside")==0 ){
18762       int n, sz;
18763       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
18764       if( sz<0 ) sz = 0;
18765       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
18766       if( n<0 ) n = 0;
18767       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
18768       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
18769 #ifdef SQLITE_ENABLE_VFSTRACE
18770     }else if( strcmp(z,"-vfstrace")==0 ){
18771       extern int vfstrace_register(
18772          const char *zTraceName,
18773          const char *zOldVfsName,
18774          int (*xOut)(const char*,void*),
18775          void *pOutArg,
18776          int makeDefault
18777       );
18778       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
18779 #endif
18780 #ifdef SQLITE_ENABLE_MULTIPLEX
18781     }else if( strcmp(z,"-multiplex")==0 ){
18782       extern int sqlite3_multiple_initialize(const char*,int);
18783       sqlite3_multiplex_initialize(0, 1);
18784 #endif
18785     }else if( strcmp(z,"-mmap")==0 ){
18786       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
18787       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
18788 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
18789     }else if( strcmp(z,"-sorterref")==0 ){
18790       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
18791       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
18792 #endif
18793     }else if( strcmp(z,"-vfs")==0 ){
18794       zVfs = cmdline_option_value(argc, argv, ++i);
18795 #ifdef SQLITE_HAVE_ZLIB
18796     }else if( strcmp(z,"-zip")==0 ){
18797       data.openMode = SHELL_OPEN_ZIPFILE;
18798 #endif
18799     }else if( strcmp(z,"-append")==0 ){
18800       data.openMode = SHELL_OPEN_APPENDVFS;
18801 #ifdef SQLITE_ENABLE_DESERIALIZE
18802     }else if( strcmp(z,"-deserialize")==0 ){
18803       data.openMode = SHELL_OPEN_DESERIALIZE;
18804     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
18805       data.szMax = integerValue(argv[++i]);
18806 #endif
18807     }else if( strcmp(z,"-readonly")==0 ){
18808       data.openMode = SHELL_OPEN_READONLY;
18809 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
18810     }else if( strncmp(z, "-A",2)==0 ){
18811       /* All remaining command-line arguments are passed to the ".archive"
18812       ** command, so ignore them */
18813       break;
18814 #endif
18815     }else if( strcmp(z, "-memtrace")==0 ){
18816       sqlite3MemTraceActivate(stderr);
18817     }
18818   }
18819   verify_uninitialized();
18820 
18821 
18822 #ifdef SQLITE_SHELL_INIT_PROC
18823   {
18824     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
18825     ** of a C-function that will perform initialization actions on SQLite that
18826     ** occur just before or after sqlite3_initialize(). Use this compile-time
18827     ** option to embed this shell program in larger applications. */
18828     extern void SQLITE_SHELL_INIT_PROC(void);
18829     SQLITE_SHELL_INIT_PROC();
18830   }
18831 #else
18832   /* All the sqlite3_config() calls have now been made. So it is safe
18833   ** to call sqlite3_initialize() and process any command line -vfs option. */
18834   sqlite3_initialize();
18835 #endif
18836 
18837   if( zVfs ){
18838     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
18839     if( pVfs ){
18840       sqlite3_vfs_register(pVfs, 1);
18841     }else{
18842       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
18843       exit(1);
18844     }
18845   }
18846 
18847   if( data.zDbFilename==0 ){
18848 #ifndef SQLITE_OMIT_MEMORYDB
18849     data.zDbFilename = ":memory:";
18850     warnInmemoryDb = argc==1;
18851 #else
18852     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
18853     return 1;
18854 #endif
18855   }
18856   data.out = stdout;
18857   sqlite3_appendvfs_init(0,0,0);
18858 
18859   /* Go ahead and open the database file if it already exists.  If the
18860   ** file does not exist, delay opening it.  This prevents empty database
18861   ** files from being created if a user mistypes the database name argument
18862   ** to the sqlite command-line tool.
18863   */
18864   if( access(data.zDbFilename, 0)==0 ){
18865     open_db(&data, 0);
18866   }
18867 
18868   /* Process the initialization file if there is one.  If no -init option
18869   ** is given on the command line, look for a file named ~/.sqliterc and
18870   ** try to process it.
18871   */
18872   process_sqliterc(&data,zInitFile);
18873 
18874   /* Make a second pass through the command-line argument and set
18875   ** options.  This second pass is delayed until after the initialization
18876   ** file is processed so that the command-line arguments will override
18877   ** settings in the initialization file.
18878   */
18879   for(i=1; i<argc; i++){
18880     char *z = argv[i];
18881     if( z[0]!='-' ) continue;
18882     if( z[1]=='-' ){ z++; }
18883     if( strcmp(z,"-init")==0 ){
18884       i++;
18885     }else if( strcmp(z,"-html")==0 ){
18886       data.mode = MODE_Html;
18887     }else if( strcmp(z,"-list")==0 ){
18888       data.mode = MODE_List;
18889     }else if( strcmp(z,"-quote")==0 ){
18890       data.mode = MODE_Quote;
18891     }else if( strcmp(z,"-line")==0 ){
18892       data.mode = MODE_Line;
18893     }else if( strcmp(z,"-column")==0 ){
18894       data.mode = MODE_Column;
18895     }else if( strcmp(z,"-csv")==0 ){
18896       data.mode = MODE_Csv;
18897       memcpy(data.colSeparator,",",2);
18898 #ifdef SQLITE_HAVE_ZLIB
18899     }else if( strcmp(z,"-zip")==0 ){
18900       data.openMode = SHELL_OPEN_ZIPFILE;
18901 #endif
18902     }else if( strcmp(z,"-append")==0 ){
18903       data.openMode = SHELL_OPEN_APPENDVFS;
18904 #ifdef SQLITE_ENABLE_DESERIALIZE
18905     }else if( strcmp(z,"-deserialize")==0 ){
18906       data.openMode = SHELL_OPEN_DESERIALIZE;
18907     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
18908       data.szMax = integerValue(argv[++i]);
18909 #endif
18910     }else if( strcmp(z,"-readonly")==0 ){
18911       data.openMode = SHELL_OPEN_READONLY;
18912     }else if( strcmp(z,"-ascii")==0 ){
18913       data.mode = MODE_Ascii;
18914       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
18915                        SEP_Unit);
18916       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
18917                        SEP_Record);
18918     }else if( strcmp(z,"-separator")==0 ){
18919       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
18920                        "%s",cmdline_option_value(argc,argv,++i));
18921     }else if( strcmp(z,"-newline")==0 ){
18922       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
18923                        "%s",cmdline_option_value(argc,argv,++i));
18924     }else if( strcmp(z,"-nullvalue")==0 ){
18925       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
18926                        "%s",cmdline_option_value(argc,argv,++i));
18927     }else if( strcmp(z,"-header")==0 ){
18928       data.showHeader = 1;
18929     }else if( strcmp(z,"-noheader")==0 ){
18930       data.showHeader = 0;
18931     }else if( strcmp(z,"-echo")==0 ){
18932       ShellSetFlag(&data, SHFLG_Echo);
18933     }else if( strcmp(z,"-eqp")==0 ){
18934       data.autoEQP = AUTOEQP_on;
18935     }else if( strcmp(z,"-eqpfull")==0 ){
18936       data.autoEQP = AUTOEQP_full;
18937     }else if( strcmp(z,"-stats")==0 ){
18938       data.statsOn = 1;
18939     }else if( strcmp(z,"-scanstats")==0 ){
18940       data.scanstatsOn = 1;
18941     }else if( strcmp(z,"-backslash")==0 ){
18942       /* Undocumented command-line option: -backslash
18943       ** Causes C-style backslash escapes to be evaluated in SQL statements
18944       ** prior to sending the SQL into SQLite.  Useful for injecting
18945       ** crazy bytes in the middle of SQL statements for testing and debugging.
18946       */
18947       ShellSetFlag(&data, SHFLG_Backslash);
18948     }else if( strcmp(z,"-bail")==0 ){
18949       bail_on_error = 1;
18950     }else if( strcmp(z,"-version")==0 ){
18951       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
18952       return 0;
18953     }else if( strcmp(z,"-interactive")==0 ){
18954       stdin_is_interactive = 1;
18955     }else if( strcmp(z,"-batch")==0 ){
18956       stdin_is_interactive = 0;
18957     }else if( strcmp(z,"-heap")==0 ){
18958       i++;
18959     }else if( strcmp(z,"-pagecache")==0 ){
18960       i+=2;
18961     }else if( strcmp(z,"-lookaside")==0 ){
18962       i+=2;
18963     }else if( strcmp(z,"-mmap")==0 ){
18964       i++;
18965     }else if( strcmp(z,"-memtrace")==0 ){
18966       i++;
18967 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
18968     }else if( strcmp(z,"-sorterref")==0 ){
18969       i++;
18970 #endif
18971     }else if( strcmp(z,"-vfs")==0 ){
18972       i++;
18973 #ifdef SQLITE_ENABLE_VFSTRACE
18974     }else if( strcmp(z,"-vfstrace")==0 ){
18975       i++;
18976 #endif
18977 #ifdef SQLITE_ENABLE_MULTIPLEX
18978     }else if( strcmp(z,"-multiplex")==0 ){
18979       i++;
18980 #endif
18981     }else if( strcmp(z,"-help")==0 ){
18982       usage(1);
18983     }else if( strcmp(z,"-cmd")==0 ){
18984       /* Run commands that follow -cmd first and separately from commands
18985       ** that simply appear on the command-line.  This seems goofy.  It would
18986       ** be better if all commands ran in the order that they appear.  But
18987       ** we retain the goofy behavior for historical compatibility. */
18988       if( i==argc-1 ) break;
18989       z = cmdline_option_value(argc,argv,++i);
18990       if( z[0]=='.' ){
18991         rc = do_meta_command(z, &data);
18992         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
18993       }else{
18994         open_db(&data, 0);
18995         rc = shell_exec(&data, z, &zErrMsg);
18996         if( zErrMsg!=0 ){
18997           utf8_printf(stderr,"Error: %s\n", zErrMsg);
18998           if( bail_on_error ) return rc!=0 ? rc : 1;
18999         }else if( rc!=0 ){
19000           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
19001           if( bail_on_error ) return rc;
19002         }
19003       }
19004 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19005     }else if( strncmp(z, "-A", 2)==0 ){
19006       if( nCmd>0 ){
19007         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
19008                             " with \"%s\"\n", z);
19009         return 1;
19010       }
19011       open_db(&data, OPEN_DB_ZIPFILE);
19012       if( z[2] ){
19013         argv[i] = &z[2];
19014         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
19015       }else{
19016         arDotCommand(&data, 1, argv+i, argc-i);
19017       }
19018       readStdin = 0;
19019       break;
19020 #endif
19021     }else{
19022       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
19023       raw_printf(stderr,"Use -help for a list of options.\n");
19024       return 1;
19025     }
19026     data.cMode = data.mode;
19027   }
19028 
19029   if( !readStdin ){
19030     /* Run all arguments that do not begin with '-' as if they were separate
19031     ** command-line inputs, except for the argToSkip argument which contains
19032     ** the database filename.
19033     */
19034     for(i=0; i<nCmd; i++){
19035       if( azCmd[i][0]=='.' ){
19036         rc = do_meta_command(azCmd[i], &data);
19037         if( rc ) return rc==2 ? 0 : rc;
19038       }else{
19039         open_db(&data, 0);
19040         rc = shell_exec(&data, azCmd[i], &zErrMsg);
19041         if( zErrMsg!=0 ){
19042           utf8_printf(stderr,"Error: %s\n", zErrMsg);
19043           return rc!=0 ? rc : 1;
19044         }else if( rc!=0 ){
19045           utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
19046           return rc;
19047         }
19048       }
19049     }
19050     free(azCmd);
19051   }else{
19052     /* Run commands received from standard input
19053     */
19054     if( stdin_is_interactive ){
19055       char *zHome;
19056       char *zHistory;
19057       int nHistory;
19058       printf(
19059         "SQLite version %s %.19s\n" /*extra-version-info*/
19060         "Enter \".help\" for usage hints.\n",
19061         sqlite3_libversion(), sqlite3_sourceid()
19062       );
19063       if( warnInmemoryDb ){
19064         printf("Connected to a ");
19065         printBold("transient in-memory database");
19066         printf(".\nUse \".open FILENAME\" to reopen on a "
19067                "persistent database.\n");
19068       }
19069       zHistory = getenv("SQLITE_HISTORY");
19070       if( zHistory ){
19071         zHistory = strdup(zHistory);
19072       }else if( (zHome = find_home_dir(0))!=0 ){
19073         nHistory = strlen30(zHome) + 20;
19074         if( (zHistory = malloc(nHistory))!=0 ){
19075           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
19076         }
19077       }
19078       if( zHistory ){ shell_read_history(zHistory); }
19079 #if HAVE_READLINE || HAVE_EDITLINE
19080       rl_attempted_completion_function = readline_completion;
19081 #elif HAVE_LINENOISE
19082       linenoiseSetCompletionCallback(linenoise_completion);
19083 #endif
19084       data.in = 0;
19085       rc = process_input(&data);
19086       if( zHistory ){
19087         shell_stifle_history(2000);
19088         shell_write_history(zHistory);
19089         free(zHistory);
19090       }
19091     }else{
19092       data.in = stdin;
19093       rc = process_input(&data);
19094     }
19095   }
19096   set_table_name(&data, 0);
19097   if( data.db ){
19098     session_close_all(&data);
19099     close_db(data.db);
19100   }
19101   sqlite3_free(data.zFreeOnClose);
19102   find_home_dir(1);
19103   output_reset(&data);
19104   data.doXdgOpen = 0;
19105   clearTempFile(&data);
19106 #if !SQLITE_SHELL_IS_UTF8
19107   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
19108   free(argvToFree);
19109 #endif
19110   /* Clear the global data structure so that valgrind will detect memory
19111   ** leaks */
19112   memset(&data, 0, sizeof(data));
19113   return rc;
19114 }
19115