xref: /freebsd/contrib/sqlite3/shell.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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,
2011                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2012                       0, sha3Func, 0, 0);
2013   if( rc==SQLITE_OK ){
2014     rc = sqlite3_create_function(db, "sha3", 2,
2015                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2016                       0, sha3Func, 0, 0);
2017   }
2018   if( rc==SQLITE_OK ){
2019     rc = sqlite3_create_function(db, "sha3_query", 1,
2020                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2021                       0, sha3QueryFunc, 0, 0);
2022   }
2023   if( rc==SQLITE_OK ){
2024     rc = sqlite3_create_function(db, "sha3_query", 2,
2025                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2026                       0, sha3QueryFunc, 0, 0);
2027   }
2028   return rc;
2029 }
2030 
2031 /************************* End ../ext/misc/shathree.c ********************/
2032 /************************* Begin ../ext/misc/fileio.c ******************/
2033 /*
2034 ** 2014-06-13
2035 **
2036 ** The author disclaims copyright to this source code.  In place of
2037 ** a legal notice, here is a blessing:
2038 **
2039 **    May you do good and not evil.
2040 **    May you find forgiveness for yourself and forgive others.
2041 **    May you share freely, never taking more than you give.
2042 **
2043 ******************************************************************************
2044 **
2045 ** This SQLite extension implements SQL functions readfile() and
2046 ** writefile(), and eponymous virtual type "fsdir".
2047 **
2048 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2049 **
2050 **   If neither of the optional arguments is present, then this UDF
2051 **   function writes blob DATA to file FILE. If successful, the number
2052 **   of bytes written is returned. If an error occurs, NULL is returned.
2053 **
2054 **   If the first option argument - MODE - is present, then it must
2055 **   be passed an integer value that corresponds to a POSIX mode
2056 **   value (file type + permissions, as returned in the stat.st_mode
2057 **   field by the stat() system call). Three types of files may
2058 **   be written/created:
2059 **
2060 **     regular files:  (mode & 0170000)==0100000
2061 **     symbolic links: (mode & 0170000)==0120000
2062 **     directories:    (mode & 0170000)==0040000
2063 **
2064 **   For a directory, the DATA is ignored. For a symbolic link, it is
2065 **   interpreted as text and used as the target of the link. For a
2066 **   regular file, it is interpreted as a blob and written into the
2067 **   named file. Regardless of the type of file, its permissions are
2068 **   set to (mode & 0777) before returning.
2069 **
2070 **   If the optional MTIME argument is present, then it is interpreted
2071 **   as an integer - the number of seconds since the unix epoch. The
2072 **   modification-time of the target file is set to this value before
2073 **   returning.
2074 **
2075 **   If three or more arguments are passed to this function and an
2076 **   error is encountered, an exception is raised.
2077 **
2078 ** READFILE(FILE):
2079 **
2080 **   Read and return the contents of file FILE (type blob) from disk.
2081 **
2082 ** FSDIR:
2083 **
2084 **   Used as follows:
2085 **
2086 **     SELECT * FROM fsdir($path [, $dir]);
2087 **
2088 **   Parameter $path is an absolute or relative pathname. If the file that it
2089 **   refers to does not exist, it is an error. If the path refers to a regular
2090 **   file or symbolic link, it returns a single row. Or, if the path refers
2091 **   to a directory, it returns one row for the directory, and one row for each
2092 **   file within the hierarchy rooted at $path.
2093 **
2094 **   Each row has the following columns:
2095 **
2096 **     name:  Path to file or directory (text value).
2097 **     mode:  Value of stat.st_mode for directory entry (an integer).
2098 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2099 **     data:  For a regular file, a blob containing the file data. For a
2100 **            symlink, a text value containing the text of the link. For a
2101 **            directory, NULL.
2102 **
2103 **   If a non-NULL value is specified for the optional $dir parameter and
2104 **   $path is a relative path, then $path is interpreted relative to $dir.
2105 **   And the paths returned in the "name" column of the table are also
2106 **   relative to directory $dir.
2107 */
2108 /* #include "sqlite3ext.h" */
2109 SQLITE_EXTENSION_INIT1
2110 #include <stdio.h>
2111 #include <string.h>
2112 #include <assert.h>
2113 
2114 #include <sys/types.h>
2115 #include <sys/stat.h>
2116 #include <fcntl.h>
2117 #if !defined(_WIN32) && !defined(WIN32)
2118 #  include <unistd.h>
2119 #  include <dirent.h>
2120 #  include <utime.h>
2121 #  include <sys/time.h>
2122 #else
2123 #  include "windows.h"
2124 #  include <io.h>
2125 #  include <direct.h>
2126 /* #  include "test_windirent.h" */
2127 #  define dirent DIRENT
2128 #  ifndef chmod
2129 #    define chmod _chmod
2130 #  endif
2131 #  ifndef stat
2132 #    define stat _stat
2133 #  endif
2134 #  define mkdir(path,mode) _mkdir(path)
2135 #  define lstat(path,buf) stat(path,buf)
2136 #endif
2137 #include <time.h>
2138 #include <errno.h>
2139 
2140 
2141 /*
2142 ** Structure of the fsdir() table-valued function
2143 */
2144                  /*    0    1    2     3    4           5             */
2145 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2146 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
2147 #define FSDIR_COLUMN_MODE     1     /* Access mode */
2148 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2149 #define FSDIR_COLUMN_DATA     3     /* File content */
2150 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2151 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2152 
2153 
2154 /*
2155 ** Set the result stored by context ctx to a blob containing the
2156 ** contents of file zName.  Or, leave the result unchanged (NULL)
2157 ** if the file does not exist or is unreadable.
2158 **
2159 ** If the file exceeds the SQLite blob size limit, through an
2160 ** SQLITE_TOOBIG error.
2161 **
2162 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2163 ** off of disk.
2164 */
2165 static void readFileContents(sqlite3_context *ctx, const char *zName){
2166   FILE *in;
2167   sqlite3_int64 nIn;
2168   void *pBuf;
2169   sqlite3 *db;
2170   int mxBlob;
2171 
2172   in = fopen(zName, "rb");
2173   if( in==0 ){
2174     /* File does not exist or is unreadable. Leave the result set to NULL. */
2175     return;
2176   }
2177   fseek(in, 0, SEEK_END);
2178   nIn = ftell(in);
2179   rewind(in);
2180   db = sqlite3_context_db_handle(ctx);
2181   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2182   if( nIn>mxBlob ){
2183     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2184     fclose(in);
2185     return;
2186   }
2187   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2188   if( pBuf==0 ){
2189     sqlite3_result_error_nomem(ctx);
2190     fclose(in);
2191     return;
2192   }
2193   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2194     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2195   }else{
2196     sqlite3_result_error_code(ctx, SQLITE_IOERR);
2197     sqlite3_free(pBuf);
2198   }
2199   fclose(in);
2200 }
2201 
2202 /*
2203 ** Implementation of the "readfile(X)" SQL function.  The entire content
2204 ** of the file named X is read and returned as a BLOB.  NULL is returned
2205 ** if the file does not exist or is unreadable.
2206 */
2207 static void readfileFunc(
2208   sqlite3_context *context,
2209   int argc,
2210   sqlite3_value **argv
2211 ){
2212   const char *zName;
2213   (void)(argc);  /* Unused parameter */
2214   zName = (const char*)sqlite3_value_text(argv[0]);
2215   if( zName==0 ) return;
2216   readFileContents(context, zName);
2217 }
2218 
2219 /*
2220 ** Set the error message contained in context ctx to the results of
2221 ** vprintf(zFmt, ...).
2222 */
2223 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2224   char *zMsg = 0;
2225   va_list ap;
2226   va_start(ap, zFmt);
2227   zMsg = sqlite3_vmprintf(zFmt, ap);
2228   sqlite3_result_error(ctx, zMsg, -1);
2229   sqlite3_free(zMsg);
2230   va_end(ap);
2231 }
2232 
2233 #if defined(_WIN32)
2234 /*
2235 ** This function is designed to convert a Win32 FILETIME structure into the
2236 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2237 */
2238 static sqlite3_uint64 fileTimeToUnixTime(
2239   LPFILETIME pFileTime
2240 ){
2241   SYSTEMTIME epochSystemTime;
2242   ULARGE_INTEGER epochIntervals;
2243   FILETIME epochFileTime;
2244   ULARGE_INTEGER fileIntervals;
2245 
2246   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2247   epochSystemTime.wYear = 1970;
2248   epochSystemTime.wMonth = 1;
2249   epochSystemTime.wDay = 1;
2250   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2251   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2252   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2253 
2254   fileIntervals.LowPart = pFileTime->dwLowDateTime;
2255   fileIntervals.HighPart = pFileTime->dwHighDateTime;
2256 
2257   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2258 }
2259 
2260 /*
2261 ** This function attempts to normalize the time values found in the stat()
2262 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2263 ** appears to return these values as local times.
2264 */
2265 static void statTimesToUtc(
2266   const char *zPath,
2267   struct stat *pStatBuf
2268 ){
2269   HANDLE hFindFile;
2270   WIN32_FIND_DATAW fd;
2271   LPWSTR zUnicodeName;
2272   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2273   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2274   if( zUnicodeName ){
2275     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2276     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2277     if( hFindFile!=NULL ){
2278       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2279       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2280       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2281       FindClose(hFindFile);
2282     }
2283     sqlite3_free(zUnicodeName);
2284   }
2285 }
2286 #endif
2287 
2288 /*
2289 ** This function is used in place of stat().  On Windows, special handling
2290 ** is required in order for the included time to be returned as UTC.  On all
2291 ** other systems, this function simply calls stat().
2292 */
2293 static int fileStat(
2294   const char *zPath,
2295   struct stat *pStatBuf
2296 ){
2297 #if defined(_WIN32)
2298   int rc = stat(zPath, pStatBuf);
2299   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2300   return rc;
2301 #else
2302   return stat(zPath, pStatBuf);
2303 #endif
2304 }
2305 
2306 /*
2307 ** This function is used in place of lstat().  On Windows, special handling
2308 ** is required in order for the included time to be returned as UTC.  On all
2309 ** other systems, this function simply calls lstat().
2310 */
2311 static int fileLinkStat(
2312   const char *zPath,
2313   struct stat *pStatBuf
2314 ){
2315 #if defined(_WIN32)
2316   int rc = lstat(zPath, pStatBuf);
2317   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2318   return rc;
2319 #else
2320   return lstat(zPath, pStatBuf);
2321 #endif
2322 }
2323 
2324 /*
2325 ** Argument zFile is the name of a file that will be created and/or written
2326 ** by SQL function writefile(). This function ensures that the directory
2327 ** zFile will be written to exists, creating it if required. The permissions
2328 ** for any path components created by this function are set in accordance
2329 ** with the current umask.
2330 **
2331 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2332 ** SQLITE_OK is returned if the directory is successfully created, or
2333 ** SQLITE_ERROR otherwise.
2334 */
2335 static int makeDirectory(
2336   const char *zFile
2337 ){
2338   char *zCopy = sqlite3_mprintf("%s", zFile);
2339   int rc = SQLITE_OK;
2340 
2341   if( zCopy==0 ){
2342     rc = SQLITE_NOMEM;
2343   }else{
2344     int nCopy = (int)strlen(zCopy);
2345     int i = 1;
2346 
2347     while( rc==SQLITE_OK ){
2348       struct stat sStat;
2349       int rc2;
2350 
2351       for(; zCopy[i]!='/' && i<nCopy; i++);
2352       if( i==nCopy ) break;
2353       zCopy[i] = '\0';
2354 
2355       rc2 = fileStat(zCopy, &sStat);
2356       if( rc2!=0 ){
2357         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2358       }else{
2359         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2360       }
2361       zCopy[i] = '/';
2362       i++;
2363     }
2364 
2365     sqlite3_free(zCopy);
2366   }
2367 
2368   return rc;
2369 }
2370 
2371 /*
2372 ** This function does the work for the writefile() UDF. Refer to
2373 ** header comments at the top of this file for details.
2374 */
2375 static int writeFile(
2376   sqlite3_context *pCtx,          /* Context to return bytes written in */
2377   const char *zFile,              /* File to write */
2378   sqlite3_value *pData,           /* Data to write */
2379   mode_t mode,                    /* MODE parameter passed to writefile() */
2380   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2381 ){
2382 #if !defined(_WIN32) && !defined(WIN32)
2383   if( S_ISLNK(mode) ){
2384     const char *zTo = (const char*)sqlite3_value_text(pData);
2385     if( symlink(zTo, zFile)<0 ) return 1;
2386   }else
2387 #endif
2388   {
2389     if( S_ISDIR(mode) ){
2390       if( mkdir(zFile, mode) ){
2391         /* The mkdir() call to create the directory failed. This might not
2392         ** be an error though - if there is already a directory at the same
2393         ** path and either the permissions already match or can be changed
2394         ** to do so using chmod(), it is not an error.  */
2395         struct stat sStat;
2396         if( errno!=EEXIST
2397          || 0!=fileStat(zFile, &sStat)
2398          || !S_ISDIR(sStat.st_mode)
2399          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2400         ){
2401           return 1;
2402         }
2403       }
2404     }else{
2405       sqlite3_int64 nWrite = 0;
2406       const char *z;
2407       int rc = 0;
2408       FILE *out = fopen(zFile, "wb");
2409       if( out==0 ) return 1;
2410       z = (const char*)sqlite3_value_blob(pData);
2411       if( z ){
2412         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2413         nWrite = sqlite3_value_bytes(pData);
2414         if( nWrite!=n ){
2415           rc = 1;
2416         }
2417       }
2418       fclose(out);
2419       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2420         rc = 1;
2421       }
2422       if( rc ) return 2;
2423       sqlite3_result_int64(pCtx, nWrite);
2424     }
2425   }
2426 
2427   if( mtime>=0 ){
2428 #if defined(_WIN32)
2429     /* Windows */
2430     FILETIME lastAccess;
2431     FILETIME lastWrite;
2432     SYSTEMTIME currentTime;
2433     LONGLONG intervals;
2434     HANDLE hFile;
2435     LPWSTR zUnicodeName;
2436     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2437 
2438     GetSystemTime(&currentTime);
2439     SystemTimeToFileTime(&currentTime, &lastAccess);
2440     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2441     lastWrite.dwLowDateTime = (DWORD)intervals;
2442     lastWrite.dwHighDateTime = intervals >> 32;
2443     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2444     if( zUnicodeName==0 ){
2445       return 1;
2446     }
2447     hFile = CreateFileW(
2448       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2449       FILE_FLAG_BACKUP_SEMANTICS, NULL
2450     );
2451     sqlite3_free(zUnicodeName);
2452     if( hFile!=INVALID_HANDLE_VALUE ){
2453       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2454       CloseHandle(hFile);
2455       return !bResult;
2456     }else{
2457       return 1;
2458     }
2459 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2460     /* Recent unix */
2461     struct timespec times[2];
2462     times[0].tv_nsec = times[1].tv_nsec = 0;
2463     times[0].tv_sec = time(0);
2464     times[1].tv_sec = mtime;
2465     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2466       return 1;
2467     }
2468 #else
2469     /* Legacy unix */
2470     struct timeval times[2];
2471     times[0].tv_usec = times[1].tv_usec = 0;
2472     times[0].tv_sec = time(0);
2473     times[1].tv_sec = mtime;
2474     if( utimes(zFile, times) ){
2475       return 1;
2476     }
2477 #endif
2478   }
2479 
2480   return 0;
2481 }
2482 
2483 /*
2484 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2485 ** Refer to header comments at the top of this file for details.
2486 */
2487 static void writefileFunc(
2488   sqlite3_context *context,
2489   int argc,
2490   sqlite3_value **argv
2491 ){
2492   const char *zFile;
2493   mode_t mode = 0;
2494   int res;
2495   sqlite3_int64 mtime = -1;
2496 
2497   if( argc<2 || argc>4 ){
2498     sqlite3_result_error(context,
2499         "wrong number of arguments to function writefile()", -1
2500     );
2501     return;
2502   }
2503 
2504   zFile = (const char*)sqlite3_value_text(argv[0]);
2505   if( zFile==0 ) return;
2506   if( argc>=3 ){
2507     mode = (mode_t)sqlite3_value_int(argv[2]);
2508   }
2509   if( argc==4 ){
2510     mtime = sqlite3_value_int64(argv[3]);
2511   }
2512 
2513   res = writeFile(context, zFile, argv[1], mode, mtime);
2514   if( res==1 && errno==ENOENT ){
2515     if( makeDirectory(zFile)==SQLITE_OK ){
2516       res = writeFile(context, zFile, argv[1], mode, mtime);
2517     }
2518   }
2519 
2520   if( argc>2 && res!=0 ){
2521     if( S_ISLNK(mode) ){
2522       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2523     }else if( S_ISDIR(mode) ){
2524       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2525     }else{
2526       ctxErrorMsg(context, "failed to write file: %s", zFile);
2527     }
2528   }
2529 }
2530 
2531 /*
2532 ** SQL function:   lsmode(MODE)
2533 **
2534 ** Given a numberic st_mode from stat(), convert it into a human-readable
2535 ** text string in the style of "ls -l".
2536 */
2537 static void lsModeFunc(
2538   sqlite3_context *context,
2539   int argc,
2540   sqlite3_value **argv
2541 ){
2542   int i;
2543   int iMode = sqlite3_value_int(argv[0]);
2544   char z[16];
2545   (void)argc;
2546   if( S_ISLNK(iMode) ){
2547     z[0] = 'l';
2548   }else if( S_ISREG(iMode) ){
2549     z[0] = '-';
2550   }else if( S_ISDIR(iMode) ){
2551     z[0] = 'd';
2552   }else{
2553     z[0] = '?';
2554   }
2555   for(i=0; i<3; i++){
2556     int m = (iMode >> ((2-i)*3));
2557     char *a = &z[1 + i*3];
2558     a[0] = (m & 0x4) ? 'r' : '-';
2559     a[1] = (m & 0x2) ? 'w' : '-';
2560     a[2] = (m & 0x1) ? 'x' : '-';
2561   }
2562   z[10] = '\0';
2563   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2564 }
2565 
2566 #ifndef SQLITE_OMIT_VIRTUALTABLE
2567 
2568 /*
2569 ** Cursor type for recursively iterating through a directory structure.
2570 */
2571 typedef struct fsdir_cursor fsdir_cursor;
2572 typedef struct FsdirLevel FsdirLevel;
2573 
2574 struct FsdirLevel {
2575   DIR *pDir;                 /* From opendir() */
2576   char *zDir;                /* Name of directory (nul-terminated) */
2577 };
2578 
2579 struct fsdir_cursor {
2580   sqlite3_vtab_cursor base;  /* Base class - must be first */
2581 
2582   int nLvl;                  /* Number of entries in aLvl[] array */
2583   int iLvl;                  /* Index of current entry */
2584   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2585 
2586   const char *zBase;
2587   int nBase;
2588 
2589   struct stat sStat;         /* Current lstat() results */
2590   char *zPath;               /* Path to current entry */
2591   sqlite3_int64 iRowid;      /* Current rowid */
2592 };
2593 
2594 typedef struct fsdir_tab fsdir_tab;
2595 struct fsdir_tab {
2596   sqlite3_vtab base;         /* Base class - must be first */
2597 };
2598 
2599 /*
2600 ** Construct a new fsdir virtual table object.
2601 */
2602 static int fsdirConnect(
2603   sqlite3 *db,
2604   void *pAux,
2605   int argc, const char *const*argv,
2606   sqlite3_vtab **ppVtab,
2607   char **pzErr
2608 ){
2609   fsdir_tab *pNew = 0;
2610   int rc;
2611   (void)pAux;
2612   (void)argc;
2613   (void)argv;
2614   (void)pzErr;
2615   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2616   if( rc==SQLITE_OK ){
2617     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2618     if( pNew==0 ) return SQLITE_NOMEM;
2619     memset(pNew, 0, sizeof(*pNew));
2620     sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2621   }
2622   *ppVtab = (sqlite3_vtab*)pNew;
2623   return rc;
2624 }
2625 
2626 /*
2627 ** This method is the destructor for fsdir vtab objects.
2628 */
2629 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2630   sqlite3_free(pVtab);
2631   return SQLITE_OK;
2632 }
2633 
2634 /*
2635 ** Constructor for a new fsdir_cursor object.
2636 */
2637 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2638   fsdir_cursor *pCur;
2639   (void)p;
2640   pCur = sqlite3_malloc( sizeof(*pCur) );
2641   if( pCur==0 ) return SQLITE_NOMEM;
2642   memset(pCur, 0, sizeof(*pCur));
2643   pCur->iLvl = -1;
2644   *ppCursor = &pCur->base;
2645   return SQLITE_OK;
2646 }
2647 
2648 /*
2649 ** Reset a cursor back to the state it was in when first returned
2650 ** by fsdirOpen().
2651 */
2652 static void fsdirResetCursor(fsdir_cursor *pCur){
2653   int i;
2654   for(i=0; i<=pCur->iLvl; i++){
2655     FsdirLevel *pLvl = &pCur->aLvl[i];
2656     if( pLvl->pDir ) closedir(pLvl->pDir);
2657     sqlite3_free(pLvl->zDir);
2658   }
2659   sqlite3_free(pCur->zPath);
2660   sqlite3_free(pCur->aLvl);
2661   pCur->aLvl = 0;
2662   pCur->zPath = 0;
2663   pCur->zBase = 0;
2664   pCur->nBase = 0;
2665   pCur->nLvl = 0;
2666   pCur->iLvl = -1;
2667   pCur->iRowid = 1;
2668 }
2669 
2670 /*
2671 ** Destructor for an fsdir_cursor.
2672 */
2673 static int fsdirClose(sqlite3_vtab_cursor *cur){
2674   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2675 
2676   fsdirResetCursor(pCur);
2677   sqlite3_free(pCur);
2678   return SQLITE_OK;
2679 }
2680 
2681 /*
2682 ** Set the error message for the virtual table associated with cursor
2683 ** pCur to the results of vprintf(zFmt, ...).
2684 */
2685 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2686   va_list ap;
2687   va_start(ap, zFmt);
2688   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2689   va_end(ap);
2690 }
2691 
2692 
2693 /*
2694 ** Advance an fsdir_cursor to its next row of output.
2695 */
2696 static int fsdirNext(sqlite3_vtab_cursor *cur){
2697   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2698   mode_t m = pCur->sStat.st_mode;
2699 
2700   pCur->iRowid++;
2701   if( S_ISDIR(m) ){
2702     /* Descend into this directory */
2703     int iNew = pCur->iLvl + 1;
2704     FsdirLevel *pLvl;
2705     if( iNew>=pCur->nLvl ){
2706       int nNew = iNew+1;
2707       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2708       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2709       if( aNew==0 ) return SQLITE_NOMEM;
2710       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2711       pCur->aLvl = aNew;
2712       pCur->nLvl = nNew;
2713     }
2714     pCur->iLvl = iNew;
2715     pLvl = &pCur->aLvl[iNew];
2716 
2717     pLvl->zDir = pCur->zPath;
2718     pCur->zPath = 0;
2719     pLvl->pDir = opendir(pLvl->zDir);
2720     if( pLvl->pDir==0 ){
2721       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2722       return SQLITE_ERROR;
2723     }
2724   }
2725 
2726   while( pCur->iLvl>=0 ){
2727     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2728     struct dirent *pEntry = readdir(pLvl->pDir);
2729     if( pEntry ){
2730       if( pEntry->d_name[0]=='.' ){
2731        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2732        if( pEntry->d_name[1]=='\0' ) continue;
2733       }
2734       sqlite3_free(pCur->zPath);
2735       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2736       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2737       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2738         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2739         return SQLITE_ERROR;
2740       }
2741       return SQLITE_OK;
2742     }
2743     closedir(pLvl->pDir);
2744     sqlite3_free(pLvl->zDir);
2745     pLvl->pDir = 0;
2746     pLvl->zDir = 0;
2747     pCur->iLvl--;
2748   }
2749 
2750   /* EOF */
2751   sqlite3_free(pCur->zPath);
2752   pCur->zPath = 0;
2753   return SQLITE_OK;
2754 }
2755 
2756 /*
2757 ** Return values of columns for the row at which the series_cursor
2758 ** is currently pointing.
2759 */
2760 static int fsdirColumn(
2761   sqlite3_vtab_cursor *cur,   /* The cursor */
2762   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2763   int i                       /* Which column to return */
2764 ){
2765   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2766   switch( i ){
2767     case FSDIR_COLUMN_NAME: {
2768       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2769       break;
2770     }
2771 
2772     case FSDIR_COLUMN_MODE:
2773       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2774       break;
2775 
2776     case FSDIR_COLUMN_MTIME:
2777       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2778       break;
2779 
2780     case FSDIR_COLUMN_DATA: {
2781       mode_t m = pCur->sStat.st_mode;
2782       if( S_ISDIR(m) ){
2783         sqlite3_result_null(ctx);
2784 #if !defined(_WIN32) && !defined(WIN32)
2785       }else if( S_ISLNK(m) ){
2786         char aStatic[64];
2787         char *aBuf = aStatic;
2788         sqlite3_int64 nBuf = 64;
2789         int n;
2790 
2791         while( 1 ){
2792           n = readlink(pCur->zPath, aBuf, nBuf);
2793           if( n<nBuf ) break;
2794           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2795           nBuf = nBuf*2;
2796           aBuf = sqlite3_malloc64(nBuf);
2797           if( aBuf==0 ){
2798             sqlite3_result_error_nomem(ctx);
2799             return SQLITE_NOMEM;
2800           }
2801         }
2802 
2803         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2804         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2805 #endif
2806       }else{
2807         readFileContents(ctx, pCur->zPath);
2808       }
2809     }
2810     case FSDIR_COLUMN_PATH:
2811     default: {
2812       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2813       ** always return their values as NULL */
2814       break;
2815     }
2816   }
2817   return SQLITE_OK;
2818 }
2819 
2820 /*
2821 ** Return the rowid for the current row. In this implementation, the
2822 ** first row returned is assigned rowid value 1, and each subsequent
2823 ** row a value 1 more than that of the previous.
2824 */
2825 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2826   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2827   *pRowid = pCur->iRowid;
2828   return SQLITE_OK;
2829 }
2830 
2831 /*
2832 ** Return TRUE if the cursor has been moved off of the last
2833 ** row of output.
2834 */
2835 static int fsdirEof(sqlite3_vtab_cursor *cur){
2836   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2837   return (pCur->zPath==0);
2838 }
2839 
2840 /*
2841 ** xFilter callback.
2842 **
2843 ** idxNum==1   PATH parameter only
2844 ** idxNum==2   Both PATH and DIR supplied
2845 */
2846 static int fsdirFilter(
2847   sqlite3_vtab_cursor *cur,
2848   int idxNum, const char *idxStr,
2849   int argc, sqlite3_value **argv
2850 ){
2851   const char *zDir = 0;
2852   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2853   (void)idxStr;
2854   fsdirResetCursor(pCur);
2855 
2856   if( idxNum==0 ){
2857     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2858     return SQLITE_ERROR;
2859   }
2860 
2861   assert( argc==idxNum && (argc==1 || argc==2) );
2862   zDir = (const char*)sqlite3_value_text(argv[0]);
2863   if( zDir==0 ){
2864     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2865     return SQLITE_ERROR;
2866   }
2867   if( argc==2 ){
2868     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2869   }
2870   if( pCur->zBase ){
2871     pCur->nBase = (int)strlen(pCur->zBase)+1;
2872     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2873   }else{
2874     pCur->zPath = sqlite3_mprintf("%s", zDir);
2875   }
2876 
2877   if( pCur->zPath==0 ){
2878     return SQLITE_NOMEM;
2879   }
2880   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2881     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2882     return SQLITE_ERROR;
2883   }
2884 
2885   return SQLITE_OK;
2886 }
2887 
2888 /*
2889 ** SQLite will invoke this method one or more times while planning a query
2890 ** that uses the generate_series virtual table.  This routine needs to create
2891 ** a query plan for each invocation and compute an estimated cost for that
2892 ** plan.
2893 **
2894 ** In this implementation idxNum is used to represent the
2895 ** query plan.  idxStr is unused.
2896 **
2897 ** The query plan is represented by values of idxNum:
2898 **
2899 **  (1)  The path value is supplied by argv[0]
2900 **  (2)  Path is in argv[0] and dir is in argv[1]
2901 */
2902 static int fsdirBestIndex(
2903   sqlite3_vtab *tab,
2904   sqlite3_index_info *pIdxInfo
2905 ){
2906   int i;                 /* Loop over constraints */
2907   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
2908   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
2909   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
2910   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
2911   const struct sqlite3_index_constraint *pConstraint;
2912 
2913   (void)tab;
2914   pConstraint = pIdxInfo->aConstraint;
2915   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2916     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2917     switch( pConstraint->iColumn ){
2918       case FSDIR_COLUMN_PATH: {
2919         if( pConstraint->usable ){
2920           idxPath = i;
2921           seenPath = 0;
2922         }else if( idxPath<0 ){
2923           seenPath = 1;
2924         }
2925         break;
2926       }
2927       case FSDIR_COLUMN_DIR: {
2928         if( pConstraint->usable ){
2929           idxDir = i;
2930           seenDir = 0;
2931         }else if( idxDir<0 ){
2932           seenDir = 1;
2933         }
2934         break;
2935       }
2936     }
2937   }
2938   if( seenPath || seenDir ){
2939     /* If input parameters are unusable, disallow this plan */
2940     return SQLITE_CONSTRAINT;
2941   }
2942 
2943   if( idxPath<0 ){
2944     pIdxInfo->idxNum = 0;
2945     /* The pIdxInfo->estimatedCost should have been initialized to a huge
2946     ** number.  Leave it unchanged. */
2947     pIdxInfo->estimatedRows = 0x7fffffff;
2948   }else{
2949     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
2950     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
2951     if( idxDir>=0 ){
2952       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
2953       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
2954       pIdxInfo->idxNum = 2;
2955       pIdxInfo->estimatedCost = 10.0;
2956     }else{
2957       pIdxInfo->idxNum = 1;
2958       pIdxInfo->estimatedCost = 100.0;
2959     }
2960   }
2961 
2962   return SQLITE_OK;
2963 }
2964 
2965 /*
2966 ** Register the "fsdir" virtual table.
2967 */
2968 static int fsdirRegister(sqlite3 *db){
2969   static sqlite3_module fsdirModule = {
2970     0,                         /* iVersion */
2971     0,                         /* xCreate */
2972     fsdirConnect,              /* xConnect */
2973     fsdirBestIndex,            /* xBestIndex */
2974     fsdirDisconnect,           /* xDisconnect */
2975     0,                         /* xDestroy */
2976     fsdirOpen,                 /* xOpen - open a cursor */
2977     fsdirClose,                /* xClose - close a cursor */
2978     fsdirFilter,               /* xFilter - configure scan constraints */
2979     fsdirNext,                 /* xNext - advance a cursor */
2980     fsdirEof,                  /* xEof - check for end of scan */
2981     fsdirColumn,               /* xColumn - read data */
2982     fsdirRowid,                /* xRowid - read data */
2983     0,                         /* xUpdate */
2984     0,                         /* xBegin */
2985     0,                         /* xSync */
2986     0,                         /* xCommit */
2987     0,                         /* xRollback */
2988     0,                         /* xFindMethod */
2989     0,                         /* xRename */
2990     0,                         /* xSavepoint */
2991     0,                         /* xRelease */
2992     0,                         /* xRollbackTo */
2993     0,                         /* xShadowName */
2994   };
2995 
2996   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2997   return rc;
2998 }
2999 #else         /* SQLITE_OMIT_VIRTUALTABLE */
3000 # define fsdirRegister(x) SQLITE_OK
3001 #endif
3002 
3003 #ifdef _WIN32
3004 
3005 #endif
3006 int sqlite3_fileio_init(
3007   sqlite3 *db,
3008   char **pzErrMsg,
3009   const sqlite3_api_routines *pApi
3010 ){
3011   int rc = SQLITE_OK;
3012   SQLITE_EXTENSION_INIT2(pApi);
3013   (void)pzErrMsg;  /* Unused parameter */
3014   rc = sqlite3_create_function(db, "readfile", 1,
3015                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3016                                readfileFunc, 0, 0);
3017   if( rc==SQLITE_OK ){
3018     rc = sqlite3_create_function(db, "writefile", -1,
3019                                  SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3020                                  writefileFunc, 0, 0);
3021   }
3022   if( rc==SQLITE_OK ){
3023     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3024                                  lsModeFunc, 0, 0);
3025   }
3026   if( rc==SQLITE_OK ){
3027     rc = fsdirRegister(db);
3028   }
3029   return rc;
3030 }
3031 
3032 /************************* End ../ext/misc/fileio.c ********************/
3033 /************************* Begin ../ext/misc/completion.c ******************/
3034 /*
3035 ** 2017-07-10
3036 **
3037 ** The author disclaims copyright to this source code.  In place of
3038 ** a legal notice, here is a blessing:
3039 **
3040 **    May you do good and not evil.
3041 **    May you find forgiveness for yourself and forgive others.
3042 **    May you share freely, never taking more than you give.
3043 **
3044 *************************************************************************
3045 **
3046 ** This file implements an eponymous virtual table that returns suggested
3047 ** completions for a partial SQL input.
3048 **
3049 ** Suggested usage:
3050 **
3051 **     SELECT DISTINCT candidate COLLATE nocase
3052 **       FROM completion($prefix,$wholeline)
3053 **      ORDER BY 1;
3054 **
3055 ** The two query parameters are optional.  $prefix is the text of the
3056 ** current word being typed and that is to be completed.  $wholeline is
3057 ** the complete input line, used for context.
3058 **
3059 ** The raw completion() table might return the same candidate multiple
3060 ** times, for example if the same column name is used to two or more
3061 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
3062 ** the DISTINCT and ORDER BY are recommended.
3063 **
3064 ** This virtual table operates at the speed of human typing, and so there
3065 ** is no attempt to make it fast.  Even a slow implementation will be much
3066 ** faster than any human can type.
3067 **
3068 */
3069 /* #include "sqlite3ext.h" */
3070 SQLITE_EXTENSION_INIT1
3071 #include <assert.h>
3072 #include <string.h>
3073 #include <ctype.h>
3074 
3075 #ifndef SQLITE_OMIT_VIRTUALTABLE
3076 
3077 /* completion_vtab is a subclass of sqlite3_vtab which will
3078 ** serve as the underlying representation of a completion virtual table
3079 */
3080 typedef struct completion_vtab completion_vtab;
3081 struct completion_vtab {
3082   sqlite3_vtab base;  /* Base class - must be first */
3083   sqlite3 *db;        /* Database connection for this completion vtab */
3084 };
3085 
3086 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3087 ** serve as the underlying representation of a cursor that scans
3088 ** over rows of the result
3089 */
3090 typedef struct completion_cursor completion_cursor;
3091 struct completion_cursor {
3092   sqlite3_vtab_cursor base;  /* Base class - must be first */
3093   sqlite3 *db;               /* Database connection for this cursor */
3094   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3095   char *zPrefix;             /* The prefix for the word we want to complete */
3096   char *zLine;               /* The whole that we want to complete */
3097   const char *zCurrentRow;   /* Current output row */
3098   int szRow;                 /* Length of the zCurrentRow string */
3099   sqlite3_stmt *pStmt;       /* Current statement */
3100   sqlite3_int64 iRowid;      /* The rowid */
3101   int ePhase;                /* Current phase */
3102   int j;                     /* inter-phase counter */
3103 };
3104 
3105 /* Values for ePhase:
3106 */
3107 #define COMPLETION_FIRST_PHASE   1
3108 #define COMPLETION_KEYWORDS      1
3109 #define COMPLETION_PRAGMAS       2
3110 #define COMPLETION_FUNCTIONS     3
3111 #define COMPLETION_COLLATIONS    4
3112 #define COMPLETION_INDEXES       5
3113 #define COMPLETION_TRIGGERS      6
3114 #define COMPLETION_DATABASES     7
3115 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3116 #define COMPLETION_COLUMNS       9
3117 #define COMPLETION_MODULES       10
3118 #define COMPLETION_EOF           11
3119 
3120 /*
3121 ** The completionConnect() method is invoked to create a new
3122 ** completion_vtab that describes the completion virtual table.
3123 **
3124 ** Think of this routine as the constructor for completion_vtab objects.
3125 **
3126 ** All this routine needs to do is:
3127 **
3128 **    (1) Allocate the completion_vtab object and initialize all fields.
3129 **
3130 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3131 **        result set of queries against completion will look like.
3132 */
3133 static int completionConnect(
3134   sqlite3 *db,
3135   void *pAux,
3136   int argc, const char *const*argv,
3137   sqlite3_vtab **ppVtab,
3138   char **pzErr
3139 ){
3140   completion_vtab *pNew;
3141   int rc;
3142 
3143   (void)(pAux);    /* Unused parameter */
3144   (void)(argc);    /* Unused parameter */
3145   (void)(argv);    /* Unused parameter */
3146   (void)(pzErr);   /* Unused parameter */
3147 
3148 /* Column numbers */
3149 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3150 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3151 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3152 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3153 
3154   sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3155   rc = sqlite3_declare_vtab(db,
3156       "CREATE TABLE x("
3157       "  candidate TEXT,"
3158       "  prefix TEXT HIDDEN,"
3159       "  wholeline TEXT HIDDEN,"
3160       "  phase INT HIDDEN"        /* Used for debugging only */
3161       ")");
3162   if( rc==SQLITE_OK ){
3163     pNew = sqlite3_malloc( sizeof(*pNew) );
3164     *ppVtab = (sqlite3_vtab*)pNew;
3165     if( pNew==0 ) return SQLITE_NOMEM;
3166     memset(pNew, 0, sizeof(*pNew));
3167     pNew->db = db;
3168   }
3169   return rc;
3170 }
3171 
3172 /*
3173 ** This method is the destructor for completion_cursor objects.
3174 */
3175 static int completionDisconnect(sqlite3_vtab *pVtab){
3176   sqlite3_free(pVtab);
3177   return SQLITE_OK;
3178 }
3179 
3180 /*
3181 ** Constructor for a new completion_cursor object.
3182 */
3183 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3184   completion_cursor *pCur;
3185   pCur = sqlite3_malloc( sizeof(*pCur) );
3186   if( pCur==0 ) return SQLITE_NOMEM;
3187   memset(pCur, 0, sizeof(*pCur));
3188   pCur->db = ((completion_vtab*)p)->db;
3189   *ppCursor = &pCur->base;
3190   return SQLITE_OK;
3191 }
3192 
3193 /*
3194 ** Reset the completion_cursor.
3195 */
3196 static void completionCursorReset(completion_cursor *pCur){
3197   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3198   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3199   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3200   pCur->j = 0;
3201 }
3202 
3203 /*
3204 ** Destructor for a completion_cursor.
3205 */
3206 static int completionClose(sqlite3_vtab_cursor *cur){
3207   completionCursorReset((completion_cursor*)cur);
3208   sqlite3_free(cur);
3209   return SQLITE_OK;
3210 }
3211 
3212 /*
3213 ** Advance a completion_cursor to its next row of output.
3214 **
3215 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3216 ** record the current state of the scan.  This routine sets ->zCurrentRow
3217 ** to the current row of output and then returns.  If no more rows remain,
3218 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3219 ** table that has reached the end of its scan.
3220 **
3221 ** The current implementation just lists potential identifiers and
3222 ** keywords and filters them by zPrefix.  Future enhancements should
3223 ** take zLine into account to try to restrict the set of identifiers and
3224 ** keywords based on what would be legal at the current point of input.
3225 */
3226 static int completionNext(sqlite3_vtab_cursor *cur){
3227   completion_cursor *pCur = (completion_cursor*)cur;
3228   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3229   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3230   pCur->iRowid++;
3231   while( pCur->ePhase!=COMPLETION_EOF ){
3232     switch( pCur->ePhase ){
3233       case COMPLETION_KEYWORDS: {
3234         if( pCur->j >= sqlite3_keyword_count() ){
3235           pCur->zCurrentRow = 0;
3236           pCur->ePhase = COMPLETION_DATABASES;
3237         }else{
3238           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3239         }
3240         iCol = -1;
3241         break;
3242       }
3243       case COMPLETION_DATABASES: {
3244         if( pCur->pStmt==0 ){
3245           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3246                              &pCur->pStmt, 0);
3247         }
3248         iCol = 1;
3249         eNextPhase = COMPLETION_TABLES;
3250         break;
3251       }
3252       case COMPLETION_TABLES: {
3253         if( pCur->pStmt==0 ){
3254           sqlite3_stmt *pS2;
3255           char *zSql = 0;
3256           const char *zSep = "";
3257           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3258           while( sqlite3_step(pS2)==SQLITE_ROW ){
3259             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3260             zSql = sqlite3_mprintf(
3261                "%z%s"
3262                "SELECT name FROM \"%w\".sqlite_master",
3263                zSql, zSep, zDb
3264             );
3265             if( zSql==0 ) return SQLITE_NOMEM;
3266             zSep = " UNION ";
3267           }
3268           sqlite3_finalize(pS2);
3269           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3270           sqlite3_free(zSql);
3271         }
3272         iCol = 0;
3273         eNextPhase = COMPLETION_COLUMNS;
3274         break;
3275       }
3276       case COMPLETION_COLUMNS: {
3277         if( pCur->pStmt==0 ){
3278           sqlite3_stmt *pS2;
3279           char *zSql = 0;
3280           const char *zSep = "";
3281           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3282           while( sqlite3_step(pS2)==SQLITE_ROW ){
3283             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3284             zSql = sqlite3_mprintf(
3285                "%z%s"
3286                "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3287                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3288                " WHERE sm.type='table'",
3289                zSql, zSep, zDb, zDb
3290             );
3291             if( zSql==0 ) return SQLITE_NOMEM;
3292             zSep = " UNION ";
3293           }
3294           sqlite3_finalize(pS2);
3295           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3296           sqlite3_free(zSql);
3297         }
3298         iCol = 0;
3299         eNextPhase = COMPLETION_EOF;
3300         break;
3301       }
3302     }
3303     if( iCol<0 ){
3304       /* This case is when the phase presets zCurrentRow */
3305       if( pCur->zCurrentRow==0 ) continue;
3306     }else{
3307       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3308         /* Extract the next row of content */
3309         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3310         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3311       }else{
3312         /* When all rows are finished, advance to the next phase */
3313         sqlite3_finalize(pCur->pStmt);
3314         pCur->pStmt = 0;
3315         pCur->ePhase = eNextPhase;
3316         continue;
3317       }
3318     }
3319     if( pCur->nPrefix==0 ) break;
3320     if( pCur->nPrefix<=pCur->szRow
3321      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3322     ){
3323       break;
3324     }
3325   }
3326 
3327   return SQLITE_OK;
3328 }
3329 
3330 /*
3331 ** Return values of columns for the row at which the completion_cursor
3332 ** is currently pointing.
3333 */
3334 static int completionColumn(
3335   sqlite3_vtab_cursor *cur,   /* The cursor */
3336   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3337   int i                       /* Which column to return */
3338 ){
3339   completion_cursor *pCur = (completion_cursor*)cur;
3340   switch( i ){
3341     case COMPLETION_COLUMN_CANDIDATE: {
3342       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3343       break;
3344     }
3345     case COMPLETION_COLUMN_PREFIX: {
3346       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3347       break;
3348     }
3349     case COMPLETION_COLUMN_WHOLELINE: {
3350       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3351       break;
3352     }
3353     case COMPLETION_COLUMN_PHASE: {
3354       sqlite3_result_int(ctx, pCur->ePhase);
3355       break;
3356     }
3357   }
3358   return SQLITE_OK;
3359 }
3360 
3361 /*
3362 ** Return the rowid for the current row.  In this implementation, the
3363 ** rowid is the same as the output value.
3364 */
3365 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3366   completion_cursor *pCur = (completion_cursor*)cur;
3367   *pRowid = pCur->iRowid;
3368   return SQLITE_OK;
3369 }
3370 
3371 /*
3372 ** Return TRUE if the cursor has been moved off of the last
3373 ** row of output.
3374 */
3375 static int completionEof(sqlite3_vtab_cursor *cur){
3376   completion_cursor *pCur = (completion_cursor*)cur;
3377   return pCur->ePhase >= COMPLETION_EOF;
3378 }
3379 
3380 /*
3381 ** This method is called to "rewind" the completion_cursor object back
3382 ** to the first row of output.  This method is always called at least
3383 ** once prior to any call to completionColumn() or completionRowid() or
3384 ** completionEof().
3385 */
3386 static int completionFilter(
3387   sqlite3_vtab_cursor *pVtabCursor,
3388   int idxNum, const char *idxStr,
3389   int argc, sqlite3_value **argv
3390 ){
3391   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3392   int iArg = 0;
3393   (void)(idxStr);   /* Unused parameter */
3394   (void)(argc);     /* Unused parameter */
3395   completionCursorReset(pCur);
3396   if( idxNum & 1 ){
3397     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3398     if( pCur->nPrefix>0 ){
3399       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3400       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3401     }
3402     iArg = 1;
3403   }
3404   if( idxNum & 2 ){
3405     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3406     if( pCur->nLine>0 ){
3407       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3408       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3409     }
3410   }
3411   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3412     int i = pCur->nLine;
3413     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3414       i--;
3415     }
3416     pCur->nPrefix = pCur->nLine - i;
3417     if( pCur->nPrefix>0 ){
3418       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3419       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3420     }
3421   }
3422   pCur->iRowid = 0;
3423   pCur->ePhase = COMPLETION_FIRST_PHASE;
3424   return completionNext(pVtabCursor);
3425 }
3426 
3427 /*
3428 ** SQLite will invoke this method one or more times while planning a query
3429 ** that uses the completion virtual table.  This routine needs to create
3430 ** a query plan for each invocation and compute an estimated cost for that
3431 ** plan.
3432 **
3433 ** There are two hidden parameters that act as arguments to the table-valued
3434 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3435 ** is available and bit 1 is set if "wholeline" is available.
3436 */
3437 static int completionBestIndex(
3438   sqlite3_vtab *tab,
3439   sqlite3_index_info *pIdxInfo
3440 ){
3441   int i;                 /* Loop over constraints */
3442   int idxNum = 0;        /* The query plan bitmask */
3443   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3444   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3445   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3446   const struct sqlite3_index_constraint *pConstraint;
3447 
3448   (void)(tab);    /* Unused parameter */
3449   pConstraint = pIdxInfo->aConstraint;
3450   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3451     if( pConstraint->usable==0 ) continue;
3452     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3453     switch( pConstraint->iColumn ){
3454       case COMPLETION_COLUMN_PREFIX:
3455         prefixIdx = i;
3456         idxNum |= 1;
3457         break;
3458       case COMPLETION_COLUMN_WHOLELINE:
3459         wholelineIdx = i;
3460         idxNum |= 2;
3461         break;
3462     }
3463   }
3464   if( prefixIdx>=0 ){
3465     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3466     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3467   }
3468   if( wholelineIdx>=0 ){
3469     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3470     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3471   }
3472   pIdxInfo->idxNum = idxNum;
3473   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3474   pIdxInfo->estimatedRows = 500 - 100*nArg;
3475   return SQLITE_OK;
3476 }
3477 
3478 /*
3479 ** This following structure defines all the methods for the
3480 ** completion virtual table.
3481 */
3482 static sqlite3_module completionModule = {
3483   0,                         /* iVersion */
3484   0,                         /* xCreate */
3485   completionConnect,         /* xConnect */
3486   completionBestIndex,       /* xBestIndex */
3487   completionDisconnect,      /* xDisconnect */
3488   0,                         /* xDestroy */
3489   completionOpen,            /* xOpen - open a cursor */
3490   completionClose,           /* xClose - close a cursor */
3491   completionFilter,          /* xFilter - configure scan constraints */
3492   completionNext,            /* xNext - advance a cursor */
3493   completionEof,             /* xEof - check for end of scan */
3494   completionColumn,          /* xColumn - read data */
3495   completionRowid,           /* xRowid - read data */
3496   0,                         /* xUpdate */
3497   0,                         /* xBegin */
3498   0,                         /* xSync */
3499   0,                         /* xCommit */
3500   0,                         /* xRollback */
3501   0,                         /* xFindMethod */
3502   0,                         /* xRename */
3503   0,                         /* xSavepoint */
3504   0,                         /* xRelease */
3505   0,                         /* xRollbackTo */
3506   0                          /* xShadowName */
3507 };
3508 
3509 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3510 
3511 int sqlite3CompletionVtabInit(sqlite3 *db){
3512   int rc = SQLITE_OK;
3513 #ifndef SQLITE_OMIT_VIRTUALTABLE
3514   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3515 #endif
3516   return rc;
3517 }
3518 
3519 #ifdef _WIN32
3520 
3521 #endif
3522 int sqlite3_completion_init(
3523   sqlite3 *db,
3524   char **pzErrMsg,
3525   const sqlite3_api_routines *pApi
3526 ){
3527   int rc = SQLITE_OK;
3528   SQLITE_EXTENSION_INIT2(pApi);
3529   (void)(pzErrMsg);  /* Unused parameter */
3530 #ifndef SQLITE_OMIT_VIRTUALTABLE
3531   rc = sqlite3CompletionVtabInit(db);
3532 #endif
3533   return rc;
3534 }
3535 
3536 /************************* End ../ext/misc/completion.c ********************/
3537 /************************* Begin ../ext/misc/appendvfs.c ******************/
3538 /*
3539 ** 2017-10-20
3540 **
3541 ** The author disclaims copyright to this source code.  In place of
3542 ** a legal notice, here is a blessing:
3543 **
3544 **    May you do good and not evil.
3545 **    May you find forgiveness for yourself and forgive others.
3546 **    May you share freely, never taking more than you give.
3547 **
3548 ******************************************************************************
3549 **
3550 ** This file implements a VFS shim that allows an SQLite database to be
3551 ** appended onto the end of some other file, such as an executable.
3552 **
3553 ** A special record must appear at the end of the file that identifies the
3554 ** file as an appended database and provides an offset to page 1.  For
3555 ** best performance page 1 should be located at a disk page boundary, though
3556 ** that is not required.
3557 **
3558 ** When opening a database using this VFS, the connection might treat
3559 ** the file as an ordinary SQLite database, or it might treat is as a
3560 ** database appended onto some other file.  Here are the rules:
3561 **
3562 **  (1)  When opening a new empty file, that file is treated as an ordinary
3563 **       database.
3564 **
3565 **  (2)  When opening a file that begins with the standard SQLite prefix
3566 **       string "SQLite format 3", that file is treated as an ordinary
3567 **       database.
3568 **
3569 **  (3)  When opening a file that ends with the appendvfs trailer string
3570 **       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3571 **       database.
3572 **
3573 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3574 **       set, then a new database is appended to the already existing file.
3575 **
3576 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3577 **
3578 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3579 ** the file containing the database is limited to 1GB.  This VFS will refuse
3580 ** to read or write past the 1GB mark.  This restriction might be lifted in
3581 ** future versions.  For now, if you need a large database, then keep the
3582 ** database in a separate file.
3583 **
3584 ** If the file being opened is not an appended database, then this shim is
3585 ** a pass-through into the default underlying VFS.
3586 **/
3587 /* #include "sqlite3ext.h" */
3588 SQLITE_EXTENSION_INIT1
3589 #include <string.h>
3590 #include <assert.h>
3591 
3592 /* The append mark at the end of the database is:
3593 **
3594 **     Start-Of-SQLite3-NNNNNNNN
3595 **     123456789 123456789 12345
3596 **
3597 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3598 ** the offset to page 1.
3599 */
3600 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3601 #define APND_MARK_PREFIX_SZ  17
3602 #define APND_MARK_SIZE       25
3603 
3604 /*
3605 ** Maximum size of the combined prefix + database + append-mark.  This
3606 ** must be less than 0x40000000 to avoid locking issues on Windows.
3607 */
3608 #define APND_MAX_SIZE  (65536*15259)
3609 
3610 /*
3611 ** Forward declaration of objects used by this utility
3612 */
3613 typedef struct sqlite3_vfs ApndVfs;
3614 typedef struct ApndFile ApndFile;
3615 
3616 /* Access to a lower-level VFS that (might) implement dynamic loading,
3617 ** access to randomness, etc.
3618 */
3619 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3620 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3621 
3622 /* An open file */
3623 struct ApndFile {
3624   sqlite3_file base;              /* IO methods */
3625   sqlite3_int64 iPgOne;           /* File offset to page 1 */
3626   sqlite3_int64 iMark;            /* Start of the append-mark */
3627 };
3628 
3629 /*
3630 ** Methods for ApndFile
3631 */
3632 static int apndClose(sqlite3_file*);
3633 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3634 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3635 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3636 static int apndSync(sqlite3_file*, int flags);
3637 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3638 static int apndLock(sqlite3_file*, int);
3639 static int apndUnlock(sqlite3_file*, int);
3640 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3641 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3642 static int apndSectorSize(sqlite3_file*);
3643 static int apndDeviceCharacteristics(sqlite3_file*);
3644 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3645 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3646 static void apndShmBarrier(sqlite3_file*);
3647 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3648 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3649 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3650 
3651 /*
3652 ** Methods for ApndVfs
3653 */
3654 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3655 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3656 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3657 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3658 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3659 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3660 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3661 static void apndDlClose(sqlite3_vfs*, void*);
3662 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3663 static int apndSleep(sqlite3_vfs*, int microseconds);
3664 static int apndCurrentTime(sqlite3_vfs*, double*);
3665 static int apndGetLastError(sqlite3_vfs*, int, char *);
3666 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3667 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3668 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3669 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3670 
3671 static sqlite3_vfs apnd_vfs = {
3672   3,                            /* iVersion (set when registered) */
3673   0,                            /* szOsFile (set when registered) */
3674   1024,                         /* mxPathname */
3675   0,                            /* pNext */
3676   "apndvfs",                    /* zName */
3677   0,                            /* pAppData (set when registered) */
3678   apndOpen,                     /* xOpen */
3679   apndDelete,                   /* xDelete */
3680   apndAccess,                   /* xAccess */
3681   apndFullPathname,             /* xFullPathname */
3682   apndDlOpen,                   /* xDlOpen */
3683   apndDlError,                  /* xDlError */
3684   apndDlSym,                    /* xDlSym */
3685   apndDlClose,                  /* xDlClose */
3686   apndRandomness,               /* xRandomness */
3687   apndSleep,                    /* xSleep */
3688   apndCurrentTime,              /* xCurrentTime */
3689   apndGetLastError,             /* xGetLastError */
3690   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3691   apndSetSystemCall,            /* xSetSystemCall */
3692   apndGetSystemCall,            /* xGetSystemCall */
3693   apndNextSystemCall            /* xNextSystemCall */
3694 };
3695 
3696 static const sqlite3_io_methods apnd_io_methods = {
3697   3,                              /* iVersion */
3698   apndClose,                      /* xClose */
3699   apndRead,                       /* xRead */
3700   apndWrite,                      /* xWrite */
3701   apndTruncate,                   /* xTruncate */
3702   apndSync,                       /* xSync */
3703   apndFileSize,                   /* xFileSize */
3704   apndLock,                       /* xLock */
3705   apndUnlock,                     /* xUnlock */
3706   apndCheckReservedLock,          /* xCheckReservedLock */
3707   apndFileControl,                /* xFileControl */
3708   apndSectorSize,                 /* xSectorSize */
3709   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3710   apndShmMap,                     /* xShmMap */
3711   apndShmLock,                    /* xShmLock */
3712   apndShmBarrier,                 /* xShmBarrier */
3713   apndShmUnmap,                   /* xShmUnmap */
3714   apndFetch,                      /* xFetch */
3715   apndUnfetch                     /* xUnfetch */
3716 };
3717 
3718 
3719 
3720 /*
3721 ** Close an apnd-file.
3722 */
3723 static int apndClose(sqlite3_file *pFile){
3724   pFile = ORIGFILE(pFile);
3725   return pFile->pMethods->xClose(pFile);
3726 }
3727 
3728 /*
3729 ** Read data from an apnd-file.
3730 */
3731 static int apndRead(
3732   sqlite3_file *pFile,
3733   void *zBuf,
3734   int iAmt,
3735   sqlite_int64 iOfst
3736 ){
3737   ApndFile *p = (ApndFile *)pFile;
3738   pFile = ORIGFILE(pFile);
3739   return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3740 }
3741 
3742 /*
3743 ** Add the append-mark onto the end of the file.
3744 */
3745 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3746   int i;
3747   unsigned char a[APND_MARK_SIZE];
3748   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3749   for(i=0; i<8; i++){
3750     a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3751   }
3752   return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3753 }
3754 
3755 /*
3756 ** Write data to an apnd-file.
3757 */
3758 static int apndWrite(
3759   sqlite3_file *pFile,
3760   const void *zBuf,
3761   int iAmt,
3762   sqlite_int64 iOfst
3763 ){
3764   int rc;
3765   ApndFile *p = (ApndFile *)pFile;
3766   pFile = ORIGFILE(pFile);
3767   if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3768   rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3769   if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
3770     sqlite3_int64 sz = 0;
3771     rc = pFile->pMethods->xFileSize(pFile, &sz);
3772     if( rc==SQLITE_OK ){
3773       p->iMark = sz - APND_MARK_SIZE;
3774       if( iOfst + iAmt + p->iPgOne > p->iMark ){
3775         p->iMark = p->iPgOne + iOfst + iAmt;
3776         rc = apndWriteMark(p, pFile);
3777       }
3778     }
3779   }
3780   return rc;
3781 }
3782 
3783 /*
3784 ** Truncate an apnd-file.
3785 */
3786 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3787   int rc;
3788   ApndFile *p = (ApndFile *)pFile;
3789   pFile = ORIGFILE(pFile);
3790   rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3791   if( rc==SQLITE_OK ){
3792     p->iMark = p->iPgOne+size;
3793     rc = apndWriteMark(p, pFile);
3794   }
3795   return rc;
3796 }
3797 
3798 /*
3799 ** Sync an apnd-file.
3800 */
3801 static int apndSync(sqlite3_file *pFile, int flags){
3802   pFile = ORIGFILE(pFile);
3803   return pFile->pMethods->xSync(pFile, flags);
3804 }
3805 
3806 /*
3807 ** Return the current file-size of an apnd-file.
3808 */
3809 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3810   ApndFile *p = (ApndFile *)pFile;
3811   int rc;
3812   pFile = ORIGFILE(p);
3813   rc = pFile->pMethods->xFileSize(pFile, pSize);
3814   if( rc==SQLITE_OK && p->iPgOne ){
3815     *pSize -= p->iPgOne + APND_MARK_SIZE;
3816   }
3817   return rc;
3818 }
3819 
3820 /*
3821 ** Lock an apnd-file.
3822 */
3823 static int apndLock(sqlite3_file *pFile, int eLock){
3824   pFile = ORIGFILE(pFile);
3825   return pFile->pMethods->xLock(pFile, eLock);
3826 }
3827 
3828 /*
3829 ** Unlock an apnd-file.
3830 */
3831 static int apndUnlock(sqlite3_file *pFile, int eLock){
3832   pFile = ORIGFILE(pFile);
3833   return pFile->pMethods->xUnlock(pFile, eLock);
3834 }
3835 
3836 /*
3837 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3838 */
3839 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3840   pFile = ORIGFILE(pFile);
3841   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3842 }
3843 
3844 /*
3845 ** File control method. For custom operations on an apnd-file.
3846 */
3847 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3848   ApndFile *p = (ApndFile *)pFile;
3849   int rc;
3850   pFile = ORIGFILE(pFile);
3851   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3852   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3853     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3854   }
3855   return rc;
3856 }
3857 
3858 /*
3859 ** Return the sector-size in bytes for an apnd-file.
3860 */
3861 static int apndSectorSize(sqlite3_file *pFile){
3862   pFile = ORIGFILE(pFile);
3863   return pFile->pMethods->xSectorSize(pFile);
3864 }
3865 
3866 /*
3867 ** Return the device characteristic flags supported by an apnd-file.
3868 */
3869 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3870   pFile = ORIGFILE(pFile);
3871   return pFile->pMethods->xDeviceCharacteristics(pFile);
3872 }
3873 
3874 /* Create a shared memory file mapping */
3875 static int apndShmMap(
3876   sqlite3_file *pFile,
3877   int iPg,
3878   int pgsz,
3879   int bExtend,
3880   void volatile **pp
3881 ){
3882   pFile = ORIGFILE(pFile);
3883   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3884 }
3885 
3886 /* Perform locking on a shared-memory segment */
3887 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3888   pFile = ORIGFILE(pFile);
3889   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3890 }
3891 
3892 /* Memory barrier operation on shared memory */
3893 static void apndShmBarrier(sqlite3_file *pFile){
3894   pFile = ORIGFILE(pFile);
3895   pFile->pMethods->xShmBarrier(pFile);
3896 }
3897 
3898 /* Unmap a shared memory segment */
3899 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3900   pFile = ORIGFILE(pFile);
3901   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3902 }
3903 
3904 /* Fetch a page of a memory-mapped file */
3905 static int apndFetch(
3906   sqlite3_file *pFile,
3907   sqlite3_int64 iOfst,
3908   int iAmt,
3909   void **pp
3910 ){
3911   ApndFile *p = (ApndFile *)pFile;
3912   pFile = ORIGFILE(pFile);
3913   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3914 }
3915 
3916 /* Release a memory-mapped page */
3917 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3918   ApndFile *p = (ApndFile *)pFile;
3919   pFile = ORIGFILE(pFile);
3920   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3921 }
3922 
3923 /*
3924 ** Check to see if the file is an ordinary SQLite database file.
3925 */
3926 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3927   int rc;
3928   char zHdr[16];
3929   static const char aSqliteHdr[] = "SQLite format 3";
3930   if( sz<512 ) return 0;
3931   rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3932   if( rc ) return 0;
3933   return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3934 }
3935 
3936 /*
3937 ** Try to read the append-mark off the end of a file.  Return the
3938 ** start of the appended database if the append-mark is present.  If
3939 ** there is no append-mark, return -1;
3940 */
3941 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3942   int rc, i;
3943   sqlite3_int64 iMark;
3944   unsigned char a[APND_MARK_SIZE];
3945 
3946   if( sz<=APND_MARK_SIZE ) return -1;
3947   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3948   if( rc ) return -1;
3949   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3950   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3951   for(i=1; i<8; i++){
3952     iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3953   }
3954   return iMark;
3955 }
3956 
3957 /*
3958 ** Open an apnd file handle.
3959 */
3960 static int apndOpen(
3961   sqlite3_vfs *pVfs,
3962   const char *zName,
3963   sqlite3_file *pFile,
3964   int flags,
3965   int *pOutFlags
3966 ){
3967   ApndFile *p;
3968   sqlite3_file *pSubFile;
3969   sqlite3_vfs *pSubVfs;
3970   int rc;
3971   sqlite3_int64 sz;
3972   pSubVfs = ORIGVFS(pVfs);
3973   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3974     return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3975   }
3976   p = (ApndFile*)pFile;
3977   memset(p, 0, sizeof(*p));
3978   pSubFile = ORIGFILE(pFile);
3979   p->base.pMethods = &apnd_io_methods;
3980   rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3981   if( rc ) goto apnd_open_done;
3982   rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3983   if( rc ){
3984     pSubFile->pMethods->xClose(pSubFile);
3985     goto apnd_open_done;
3986   }
3987   if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3988     memmove(pFile, pSubFile, pSubVfs->szOsFile);
3989     return SQLITE_OK;
3990   }
3991   p->iMark = 0;
3992   p->iPgOne = apndReadMark(sz, pFile);
3993   if( p->iPgOne>0 ){
3994     return SQLITE_OK;
3995   }
3996   if( (flags & SQLITE_OPEN_CREATE)==0 ){
3997     pSubFile->pMethods->xClose(pSubFile);
3998     rc = SQLITE_CANTOPEN;
3999   }
4000   p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
4001 apnd_open_done:
4002   if( rc ) pFile->pMethods = 0;
4003   return rc;
4004 }
4005 
4006 /*
4007 ** All other VFS methods are pass-thrus.
4008 */
4009 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4010   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4011 }
4012 static int apndAccess(
4013   sqlite3_vfs *pVfs,
4014   const char *zPath,
4015   int flags,
4016   int *pResOut
4017 ){
4018   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4019 }
4020 static int apndFullPathname(
4021   sqlite3_vfs *pVfs,
4022   const char *zPath,
4023   int nOut,
4024   char *zOut
4025 ){
4026   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4027 }
4028 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4029   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4030 }
4031 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4032   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4033 }
4034 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4035   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4036 }
4037 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4038   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4039 }
4040 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4041   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4042 }
4043 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4044   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4045 }
4046 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4047   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4048 }
4049 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4050   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4051 }
4052 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4053   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4054 }
4055 static int apndSetSystemCall(
4056   sqlite3_vfs *pVfs,
4057   const char *zName,
4058   sqlite3_syscall_ptr pCall
4059 ){
4060   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4061 }
4062 static sqlite3_syscall_ptr apndGetSystemCall(
4063   sqlite3_vfs *pVfs,
4064   const char *zName
4065 ){
4066   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4067 }
4068 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4069   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4070 }
4071 
4072 
4073 #ifdef _WIN32
4074 
4075 #endif
4076 /*
4077 ** This routine is called when the extension is loaded.
4078 ** Register the new VFS.
4079 */
4080 int sqlite3_appendvfs_init(
4081   sqlite3 *db,
4082   char **pzErrMsg,
4083   const sqlite3_api_routines *pApi
4084 ){
4085   int rc = SQLITE_OK;
4086   sqlite3_vfs *pOrig;
4087   SQLITE_EXTENSION_INIT2(pApi);
4088   (void)pzErrMsg;
4089   (void)db;
4090   pOrig = sqlite3_vfs_find(0);
4091   apnd_vfs.iVersion = pOrig->iVersion;
4092   apnd_vfs.pAppData = pOrig;
4093   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4094   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4095 #ifdef APPENDVFS_TEST
4096   if( rc==SQLITE_OK ){
4097     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4098   }
4099 #endif
4100   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4101   return rc;
4102 }
4103 
4104 /************************* End ../ext/misc/appendvfs.c ********************/
4105 /************************* Begin ../ext/misc/memtrace.c ******************/
4106 /*
4107 ** 2019-01-21
4108 **
4109 ** The author disclaims copyright to this source code.  In place of
4110 ** a legal notice, here is a blessing:
4111 **
4112 **    May you do good and not evil.
4113 **    May you find forgiveness for yourself and forgive others.
4114 **    May you share freely, never taking more than you give.
4115 **
4116 *************************************************************************
4117 **
4118 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4119 ** mechanism to add a tracing layer on top of SQLite.  If this extension
4120 ** is registered prior to sqlite3_initialize(), it will cause all memory
4121 ** allocation activities to be logged on standard output, or to some other
4122 ** FILE specified by the initializer.
4123 **
4124 ** This file needs to be compiled into the application that uses it.
4125 **
4126 ** This extension is used to implement the --memtrace option of the
4127 ** command-line shell.
4128 */
4129 #include <assert.h>
4130 #include <string.h>
4131 #include <stdio.h>
4132 
4133 /* The original memory allocation routines */
4134 static sqlite3_mem_methods memtraceBase;
4135 static FILE *memtraceOut;
4136 
4137 /* Methods that trace memory allocations */
4138 static void *memtraceMalloc(int n){
4139   if( memtraceOut ){
4140     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4141             memtraceBase.xRoundup(n));
4142   }
4143   return memtraceBase.xMalloc(n);
4144 }
4145 static void memtraceFree(void *p){
4146   if( p==0 ) return;
4147   if( memtraceOut ){
4148     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4149   }
4150   memtraceBase.xFree(p);
4151 }
4152 static void *memtraceRealloc(void *p, int n){
4153   if( p==0 ) return memtraceMalloc(n);
4154   if( n==0 ){
4155     memtraceFree(p);
4156     return 0;
4157   }
4158   if( memtraceOut ){
4159     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4160             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4161   }
4162   return memtraceBase.xRealloc(p, n);
4163 }
4164 static int memtraceSize(void *p){
4165   return memtraceBase.xSize(p);
4166 }
4167 static int memtraceRoundup(int n){
4168   return memtraceBase.xRoundup(n);
4169 }
4170 static int memtraceInit(void *p){
4171   return memtraceBase.xInit(p);
4172 }
4173 static void memtraceShutdown(void *p){
4174   memtraceBase.xShutdown(p);
4175 }
4176 
4177 /* The substitute memory allocator */
4178 static sqlite3_mem_methods ersaztMethods = {
4179   memtraceMalloc,
4180   memtraceFree,
4181   memtraceRealloc,
4182   memtraceSize,
4183   memtraceRoundup,
4184   memtraceInit,
4185   memtraceShutdown,
4186   0
4187 };
4188 
4189 /* Begin tracing memory allocations to out. */
4190 int sqlite3MemTraceActivate(FILE *out){
4191   int rc = SQLITE_OK;
4192   if( memtraceBase.xMalloc==0 ){
4193     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4194     if( rc==SQLITE_OK ){
4195       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4196     }
4197   }
4198   memtraceOut = out;
4199   return rc;
4200 }
4201 
4202 /* Deactivate memory tracing */
4203 int sqlite3MemTraceDeactivate(void){
4204   int rc = SQLITE_OK;
4205   if( memtraceBase.xMalloc!=0 ){
4206     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4207     if( rc==SQLITE_OK ){
4208       memset(&memtraceBase, 0, sizeof(memtraceBase));
4209     }
4210   }
4211   memtraceOut = 0;
4212   return rc;
4213 }
4214 
4215 /************************* End ../ext/misc/memtrace.c ********************/
4216 #ifdef SQLITE_HAVE_ZLIB
4217 /************************* Begin ../ext/misc/zipfile.c ******************/
4218 /*
4219 ** 2017-12-26
4220 **
4221 ** The author disclaims copyright to this source code.  In place of
4222 ** a legal notice, here is a blessing:
4223 **
4224 **    May you do good and not evil.
4225 **    May you find forgiveness for yourself and forgive others.
4226 **    May you share freely, never taking more than you give.
4227 **
4228 ******************************************************************************
4229 **
4230 ** This file implements a virtual table for reading and writing ZIP archive
4231 ** files.
4232 **
4233 ** Usage example:
4234 **
4235 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4236 **
4237 ** Current limitations:
4238 **
4239 **    *  No support for encryption
4240 **    *  No support for ZIP archives spanning multiple files
4241 **    *  No support for zip64 extensions
4242 **    *  Only the "inflate/deflate" (zlib) compression method is supported
4243 */
4244 /* #include "sqlite3ext.h" */
4245 SQLITE_EXTENSION_INIT1
4246 #include <stdio.h>
4247 #include <string.h>
4248 #include <assert.h>
4249 
4250 #include <zlib.h>
4251 
4252 #ifndef SQLITE_OMIT_VIRTUALTABLE
4253 
4254 #ifndef SQLITE_AMALGAMATION
4255 
4256 /* typedef sqlite3_int64 i64; */
4257 /* typedef unsigned char u8; */
4258 typedef unsigned short u16;
4259 typedef unsigned long u32;
4260 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4261 
4262 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4263 # define ALWAYS(X)      (1)
4264 # define NEVER(X)       (0)
4265 #elif !defined(NDEBUG)
4266 # define ALWAYS(X)      ((X)?1:(assert(0),0))
4267 # define NEVER(X)       ((X)?(assert(0),1):0)
4268 #else
4269 # define ALWAYS(X)      (X)
4270 # define NEVER(X)       (X)
4271 #endif
4272 
4273 #endif   /* SQLITE_AMALGAMATION */
4274 
4275 /*
4276 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4277 **
4278 ** In some ways it would be better to obtain these values from system
4279 ** header files. But, the dependency is undesirable and (a) these
4280 ** have been stable for decades, (b) the values are part of POSIX and
4281 ** are also made explicit in [man stat], and (c) are part of the
4282 ** file format for zip archives.
4283 */
4284 #ifndef S_IFDIR
4285 # define S_IFDIR 0040000
4286 #endif
4287 #ifndef S_IFREG
4288 # define S_IFREG 0100000
4289 #endif
4290 #ifndef S_IFLNK
4291 # define S_IFLNK 0120000
4292 #endif
4293 
4294 static const char ZIPFILE_SCHEMA[] =
4295   "CREATE TABLE y("
4296     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
4297     "mode,"              /* 1: POSIX mode for file */
4298     "mtime,"             /* 2: Last modification time (secs since 1970)*/
4299     "sz,"                /* 3: Size of object */
4300     "rawdata,"           /* 4: Raw data */
4301     "data,"              /* 5: Uncompressed data */
4302     "method,"            /* 6: Compression method (integer) */
4303     "z HIDDEN"           /* 7: Name of zip file */
4304   ") WITHOUT ROWID;";
4305 
4306 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
4307 #define ZIPFILE_BUFFER_SIZE (64*1024)
4308 
4309 
4310 /*
4311 ** Magic numbers used to read and write zip files.
4312 **
4313 ** ZIPFILE_NEWENTRY_MADEBY:
4314 **   Use this value for the "version-made-by" field in new zip file
4315 **   entries. The upper byte indicates "unix", and the lower byte
4316 **   indicates that the zip file matches pkzip specification 3.0.
4317 **   This is what info-zip seems to do.
4318 **
4319 ** ZIPFILE_NEWENTRY_REQUIRED:
4320 **   Value for "version-required-to-extract" field of new entries.
4321 **   Version 2.0 is required to support folders and deflate compression.
4322 **
4323 ** ZIPFILE_NEWENTRY_FLAGS:
4324 **   Value for "general-purpose-bit-flags" field of new entries. Bit
4325 **   11 means "utf-8 filename and comment".
4326 **
4327 ** ZIPFILE_SIGNATURE_CDS:
4328 **   First 4 bytes of a valid CDS record.
4329 **
4330 ** ZIPFILE_SIGNATURE_LFH:
4331 **   First 4 bytes of a valid LFH record.
4332 **
4333 ** ZIPFILE_SIGNATURE_EOCD
4334 **   First 4 bytes of a valid EOCD record.
4335 */
4336 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
4337 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
4338 #define ZIPFILE_NEWENTRY_REQUIRED 20
4339 #define ZIPFILE_NEWENTRY_FLAGS    0x800
4340 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
4341 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
4342 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
4343 
4344 /*
4345 ** The sizes of the fixed-size part of each of the three main data
4346 ** structures in a zip archive.
4347 */
4348 #define ZIPFILE_LFH_FIXED_SZ      30
4349 #define ZIPFILE_EOCD_FIXED_SZ     22
4350 #define ZIPFILE_CDS_FIXED_SZ      46
4351 
4352 /*
4353 *** 4.3.16  End of central directory record:
4354 ***
4355 ***   end of central dir signature    4 bytes  (0x06054b50)
4356 ***   number of this disk             2 bytes
4357 ***   number of the disk with the
4358 ***   start of the central directory  2 bytes
4359 ***   total number of entries in the
4360 ***   central directory on this disk  2 bytes
4361 ***   total number of entries in
4362 ***   the central directory           2 bytes
4363 ***   size of the central directory   4 bytes
4364 ***   offset of start of central
4365 ***   directory with respect to
4366 ***   the starting disk number        4 bytes
4367 ***   .ZIP file comment length        2 bytes
4368 ***   .ZIP file comment       (variable size)
4369 */
4370 typedef struct ZipfileEOCD ZipfileEOCD;
4371 struct ZipfileEOCD {
4372   u16 iDisk;
4373   u16 iFirstDisk;
4374   u16 nEntry;
4375   u16 nEntryTotal;
4376   u32 nSize;
4377   u32 iOffset;
4378 };
4379 
4380 /*
4381 *** 4.3.12  Central directory structure:
4382 ***
4383 *** ...
4384 ***
4385 ***   central file header signature   4 bytes  (0x02014b50)
4386 ***   version made by                 2 bytes
4387 ***   version needed to extract       2 bytes
4388 ***   general purpose bit flag        2 bytes
4389 ***   compression method              2 bytes
4390 ***   last mod file time              2 bytes
4391 ***   last mod file date              2 bytes
4392 ***   crc-32                          4 bytes
4393 ***   compressed size                 4 bytes
4394 ***   uncompressed size               4 bytes
4395 ***   file name length                2 bytes
4396 ***   extra field length              2 bytes
4397 ***   file comment length             2 bytes
4398 ***   disk number start               2 bytes
4399 ***   internal file attributes        2 bytes
4400 ***   external file attributes        4 bytes
4401 ***   relative offset of local header 4 bytes
4402 */
4403 typedef struct ZipfileCDS ZipfileCDS;
4404 struct ZipfileCDS {
4405   u16 iVersionMadeBy;
4406   u16 iVersionExtract;
4407   u16 flags;
4408   u16 iCompression;
4409   u16 mTime;
4410   u16 mDate;
4411   u32 crc32;
4412   u32 szCompressed;
4413   u32 szUncompressed;
4414   u16 nFile;
4415   u16 nExtra;
4416   u16 nComment;
4417   u16 iDiskStart;
4418   u16 iInternalAttr;
4419   u32 iExternalAttr;
4420   u32 iOffset;
4421   char *zFile;                    /* Filename (sqlite3_malloc()) */
4422 };
4423 
4424 /*
4425 *** 4.3.7  Local file header:
4426 ***
4427 ***   local file header signature     4 bytes  (0x04034b50)
4428 ***   version needed to extract       2 bytes
4429 ***   general purpose bit flag        2 bytes
4430 ***   compression method              2 bytes
4431 ***   last mod file time              2 bytes
4432 ***   last mod file date              2 bytes
4433 ***   crc-32                          4 bytes
4434 ***   compressed size                 4 bytes
4435 ***   uncompressed size               4 bytes
4436 ***   file name length                2 bytes
4437 ***   extra field length              2 bytes
4438 ***
4439 */
4440 typedef struct ZipfileLFH ZipfileLFH;
4441 struct ZipfileLFH {
4442   u16 iVersionExtract;
4443   u16 flags;
4444   u16 iCompression;
4445   u16 mTime;
4446   u16 mDate;
4447   u32 crc32;
4448   u32 szCompressed;
4449   u32 szUncompressed;
4450   u16 nFile;
4451   u16 nExtra;
4452 };
4453 
4454 typedef struct ZipfileEntry ZipfileEntry;
4455 struct ZipfileEntry {
4456   ZipfileCDS cds;            /* Parsed CDS record */
4457   u32 mUnixTime;             /* Modification time, in UNIX format */
4458   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
4459   i64 iDataOff;              /* Offset to data in file (if aData==0) */
4460   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
4461   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
4462 };
4463 
4464 /*
4465 ** Cursor type for zipfile tables.
4466 */
4467 typedef struct ZipfileCsr ZipfileCsr;
4468 struct ZipfileCsr {
4469   sqlite3_vtab_cursor base;  /* Base class - must be first */
4470   i64 iId;                   /* Cursor ID */
4471   u8 bEof;                   /* True when at EOF */
4472   u8 bNoop;                  /* If next xNext() call is no-op */
4473 
4474   /* Used outside of write transactions */
4475   FILE *pFile;               /* Zip file */
4476   i64 iNextOff;              /* Offset of next record in central directory */
4477   ZipfileEOCD eocd;          /* Parse of central directory record */
4478 
4479   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
4480   ZipfileEntry *pCurrent;    /* Current entry */
4481   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
4482 };
4483 
4484 typedef struct ZipfileTab ZipfileTab;
4485 struct ZipfileTab {
4486   sqlite3_vtab base;         /* Base class - must be first */
4487   char *zFile;               /* Zip file this table accesses (may be NULL) */
4488   sqlite3 *db;               /* Host database connection */
4489   u8 *aBuffer;               /* Temporary buffer used for various tasks */
4490 
4491   ZipfileCsr *pCsrList;      /* List of cursors */
4492   i64 iNextCsrid;
4493 
4494   /* The following are used by write transactions only */
4495   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4496   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
4497   FILE *pWriteFd;            /* File handle open on zip archive */
4498   i64 szCurrent;             /* Current size of zip archive */
4499   i64 szOrig;                /* Size of archive at start of transaction */
4500 };
4501 
4502 /*
4503 ** Set the error message contained in context ctx to the results of
4504 ** vprintf(zFmt, ...).
4505 */
4506 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4507   char *zMsg = 0;
4508   va_list ap;
4509   va_start(ap, zFmt);
4510   zMsg = sqlite3_vmprintf(zFmt, ap);
4511   sqlite3_result_error(ctx, zMsg, -1);
4512   sqlite3_free(zMsg);
4513   va_end(ap);
4514 }
4515 
4516 /*
4517 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4518 ** is not quoted, do nothing.
4519 */
4520 static void zipfileDequote(char *zIn){
4521   char q = zIn[0];
4522   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4523     int iIn = 1;
4524     int iOut = 0;
4525     if( q=='[' ) q = ']';
4526     while( ALWAYS(zIn[iIn]) ){
4527       char c = zIn[iIn++];
4528       if( c==q && zIn[iIn++]!=q ) break;
4529       zIn[iOut++] = c;
4530     }
4531     zIn[iOut] = '\0';
4532   }
4533 }
4534 
4535 /*
4536 ** Construct a new ZipfileTab virtual table object.
4537 **
4538 **   argv[0]   -> module name  ("zipfile")
4539 **   argv[1]   -> database name
4540 **   argv[2]   -> table name
4541 **   argv[...] -> "column name" and other module argument fields.
4542 */
4543 static int zipfileConnect(
4544   sqlite3 *db,
4545   void *pAux,
4546   int argc, const char *const*argv,
4547   sqlite3_vtab **ppVtab,
4548   char **pzErr
4549 ){
4550   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4551   int nFile = 0;
4552   const char *zFile = 0;
4553   ZipfileTab *pNew = 0;
4554   int rc;
4555 
4556   /* If the table name is not "zipfile", require that the argument be
4557   ** specified. This stops zipfile tables from being created as:
4558   **
4559   **   CREATE VIRTUAL TABLE zzz USING zipfile();
4560   **
4561   ** It does not prevent:
4562   **
4563   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
4564   */
4565   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4566   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4567     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4568     return SQLITE_ERROR;
4569   }
4570 
4571   if( argc>3 ){
4572     zFile = argv[3];
4573     nFile = (int)strlen(zFile)+1;
4574   }
4575 
4576   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4577   if( rc==SQLITE_OK ){
4578     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
4579     if( pNew==0 ) return SQLITE_NOMEM;
4580     memset(pNew, 0, nByte+nFile);
4581     pNew->db = db;
4582     pNew->aBuffer = (u8*)&pNew[1];
4583     if( zFile ){
4584       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4585       memcpy(pNew->zFile, zFile, nFile);
4586       zipfileDequote(pNew->zFile);
4587     }
4588   }
4589   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
4590   *ppVtab = (sqlite3_vtab*)pNew;
4591   return rc;
4592 }
4593 
4594 /*
4595 ** Free the ZipfileEntry structure indicated by the only argument.
4596 */
4597 static void zipfileEntryFree(ZipfileEntry *p){
4598   if( p ){
4599     sqlite3_free(p->cds.zFile);
4600     sqlite3_free(p);
4601   }
4602 }
4603 
4604 /*
4605 ** Release resources that should be freed at the end of a write
4606 ** transaction.
4607 */
4608 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4609   ZipfileEntry *pEntry;
4610   ZipfileEntry *pNext;
4611 
4612   if( pTab->pWriteFd ){
4613     fclose(pTab->pWriteFd);
4614     pTab->pWriteFd = 0;
4615   }
4616   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4617     pNext = pEntry->pNext;
4618     zipfileEntryFree(pEntry);
4619   }
4620   pTab->pFirstEntry = 0;
4621   pTab->pLastEntry = 0;
4622   pTab->szCurrent = 0;
4623   pTab->szOrig = 0;
4624 }
4625 
4626 /*
4627 ** This method is the destructor for zipfile vtab objects.
4628 */
4629 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4630   zipfileCleanupTransaction((ZipfileTab*)pVtab);
4631   sqlite3_free(pVtab);
4632   return SQLITE_OK;
4633 }
4634 
4635 /*
4636 ** Constructor for a new ZipfileCsr object.
4637 */
4638 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4639   ZipfileTab *pTab = (ZipfileTab*)p;
4640   ZipfileCsr *pCsr;
4641   pCsr = sqlite3_malloc(sizeof(*pCsr));
4642   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4643   if( pCsr==0 ){
4644     return SQLITE_NOMEM;
4645   }
4646   memset(pCsr, 0, sizeof(*pCsr));
4647   pCsr->iId = ++pTab->iNextCsrid;
4648   pCsr->pCsrNext = pTab->pCsrList;
4649   pTab->pCsrList = pCsr;
4650   return SQLITE_OK;
4651 }
4652 
4653 /*
4654 ** Reset a cursor back to the state it was in when first returned
4655 ** by zipfileOpen().
4656 */
4657 static void zipfileResetCursor(ZipfileCsr *pCsr){
4658   ZipfileEntry *p;
4659   ZipfileEntry *pNext;
4660 
4661   pCsr->bEof = 0;
4662   if( pCsr->pFile ){
4663     fclose(pCsr->pFile);
4664     pCsr->pFile = 0;
4665     zipfileEntryFree(pCsr->pCurrent);
4666     pCsr->pCurrent = 0;
4667   }
4668 
4669   for(p=pCsr->pFreeEntry; p; p=pNext){
4670     pNext = p->pNext;
4671     zipfileEntryFree(p);
4672   }
4673 }
4674 
4675 /*
4676 ** Destructor for an ZipfileCsr.
4677 */
4678 static int zipfileClose(sqlite3_vtab_cursor *cur){
4679   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4680   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4681   ZipfileCsr **pp;
4682   zipfileResetCursor(pCsr);
4683 
4684   /* Remove this cursor from the ZipfileTab.pCsrList list. */
4685   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4686   *pp = pCsr->pCsrNext;
4687 
4688   sqlite3_free(pCsr);
4689   return SQLITE_OK;
4690 }
4691 
4692 /*
4693 ** Set the error message for the virtual table associated with cursor
4694 ** pCsr to the results of vprintf(zFmt, ...).
4695 */
4696 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4697   va_list ap;
4698   va_start(ap, zFmt);
4699   sqlite3_free(pTab->base.zErrMsg);
4700   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4701   va_end(ap);
4702 }
4703 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4704   va_list ap;
4705   va_start(ap, zFmt);
4706   sqlite3_free(pCsr->base.pVtab->zErrMsg);
4707   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4708   va_end(ap);
4709 }
4710 
4711 /*
4712 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4713 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4714 ** otherwise.
4715 **
4716 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4717 ** to an English language error message. It is the responsibility of the
4718 ** caller to eventually free this buffer using
4719 ** sqlite3_free().
4720 */
4721 static int zipfileReadData(
4722   FILE *pFile,                    /* Read from this file */
4723   u8 *aRead,                      /* Read into this buffer */
4724   int nRead,                      /* Number of bytes to read */
4725   i64 iOff,                       /* Offset to read from */
4726   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
4727 ){
4728   size_t n;
4729   fseek(pFile, (long)iOff, SEEK_SET);
4730   n = fread(aRead, 1, nRead, pFile);
4731   if( (int)n!=nRead ){
4732     *pzErrmsg = sqlite3_mprintf("error in fread()");
4733     return SQLITE_ERROR;
4734   }
4735   return SQLITE_OK;
4736 }
4737 
4738 static int zipfileAppendData(
4739   ZipfileTab *pTab,
4740   const u8 *aWrite,
4741   int nWrite
4742 ){
4743   size_t n;
4744   fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4745   n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4746   if( (int)n!=nWrite ){
4747     pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4748     return SQLITE_ERROR;
4749   }
4750   pTab->szCurrent += nWrite;
4751   return SQLITE_OK;
4752 }
4753 
4754 /*
4755 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4756 */
4757 static u16 zipfileGetU16(const u8 *aBuf){
4758   return (aBuf[1] << 8) + aBuf[0];
4759 }
4760 
4761 /*
4762 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4763 */
4764 static u32 zipfileGetU32(const u8 *aBuf){
4765   return ((u32)(aBuf[3]) << 24)
4766        + ((u32)(aBuf[2]) << 16)
4767        + ((u32)(aBuf[1]) <<  8)
4768        + ((u32)(aBuf[0]) <<  0);
4769 }
4770 
4771 /*
4772 ** Write a 16-bit little endiate integer into buffer aBuf.
4773 */
4774 static void zipfilePutU16(u8 *aBuf, u16 val){
4775   aBuf[0] = val & 0xFF;
4776   aBuf[1] = (val>>8) & 0xFF;
4777 }
4778 
4779 /*
4780 ** Write a 32-bit little endiate integer into buffer aBuf.
4781 */
4782 static void zipfilePutU32(u8 *aBuf, u32 val){
4783   aBuf[0] = val & 0xFF;
4784   aBuf[1] = (val>>8) & 0xFF;
4785   aBuf[2] = (val>>16) & 0xFF;
4786   aBuf[3] = (val>>24) & 0xFF;
4787 }
4788 
4789 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4790 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4791 
4792 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4793 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4794 
4795 /*
4796 ** Magic numbers used to read CDS records.
4797 */
4798 #define ZIPFILE_CDS_NFILE_OFF        28
4799 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4800 
4801 /*
4802 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4803 ** if the record is not well-formed, or SQLITE_OK otherwise.
4804 */
4805 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4806   u8 *aRead = aBuf;
4807   u32 sig = zipfileRead32(aRead);
4808   int rc = SQLITE_OK;
4809   if( sig!=ZIPFILE_SIGNATURE_CDS ){
4810     rc = SQLITE_ERROR;
4811   }else{
4812     pCDS->iVersionMadeBy = zipfileRead16(aRead);
4813     pCDS->iVersionExtract = zipfileRead16(aRead);
4814     pCDS->flags = zipfileRead16(aRead);
4815     pCDS->iCompression = zipfileRead16(aRead);
4816     pCDS->mTime = zipfileRead16(aRead);
4817     pCDS->mDate = zipfileRead16(aRead);
4818     pCDS->crc32 = zipfileRead32(aRead);
4819     pCDS->szCompressed = zipfileRead32(aRead);
4820     pCDS->szUncompressed = zipfileRead32(aRead);
4821     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4822     pCDS->nFile = zipfileRead16(aRead);
4823     pCDS->nExtra = zipfileRead16(aRead);
4824     pCDS->nComment = zipfileRead16(aRead);
4825     pCDS->iDiskStart = zipfileRead16(aRead);
4826     pCDS->iInternalAttr = zipfileRead16(aRead);
4827     pCDS->iExternalAttr = zipfileRead32(aRead);
4828     pCDS->iOffset = zipfileRead32(aRead);
4829     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4830   }
4831 
4832   return rc;
4833 }
4834 
4835 /*
4836 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4837 ** if the record is not well-formed, or SQLITE_OK otherwise.
4838 */
4839 static int zipfileReadLFH(
4840   u8 *aBuffer,
4841   ZipfileLFH *pLFH
4842 ){
4843   u8 *aRead = aBuffer;
4844   int rc = SQLITE_OK;
4845 
4846   u32 sig = zipfileRead32(aRead);
4847   if( sig!=ZIPFILE_SIGNATURE_LFH ){
4848     rc = SQLITE_ERROR;
4849   }else{
4850     pLFH->iVersionExtract = zipfileRead16(aRead);
4851     pLFH->flags = zipfileRead16(aRead);
4852     pLFH->iCompression = zipfileRead16(aRead);
4853     pLFH->mTime = zipfileRead16(aRead);
4854     pLFH->mDate = zipfileRead16(aRead);
4855     pLFH->crc32 = zipfileRead32(aRead);
4856     pLFH->szCompressed = zipfileRead32(aRead);
4857     pLFH->szUncompressed = zipfileRead32(aRead);
4858     pLFH->nFile = zipfileRead16(aRead);
4859     pLFH->nExtra = zipfileRead16(aRead);
4860   }
4861   return rc;
4862 }
4863 
4864 
4865 /*
4866 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4867 ** Scan through this buffer to find an "extra-timestamp" field. If one
4868 ** exists, extract the 32-bit modification-timestamp from it and store
4869 ** the value in output parameter *pmTime.
4870 **
4871 ** Zero is returned if no extra-timestamp record could be found (and so
4872 ** *pmTime is left unchanged), or non-zero otherwise.
4873 **
4874 ** The general format of an extra field is:
4875 **
4876 **   Header ID    2 bytes
4877 **   Data Size    2 bytes
4878 **   Data         N bytes
4879 */
4880 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4881   int ret = 0;
4882   u8 *p = aExtra;
4883   u8 *pEnd = &aExtra[nExtra];
4884 
4885   while( p<pEnd ){
4886     u16 id = zipfileRead16(p);
4887     u16 nByte = zipfileRead16(p);
4888 
4889     switch( id ){
4890       case ZIPFILE_EXTRA_TIMESTAMP: {
4891         u8 b = p[0];
4892         if( b & 0x01 ){     /* 0x01 -> modtime is present */
4893           *pmTime = zipfileGetU32(&p[1]);
4894           ret = 1;
4895         }
4896         break;
4897       }
4898     }
4899 
4900     p += nByte;
4901   }
4902   return ret;
4903 }
4904 
4905 /*
4906 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4907 ** fields of the CDS structure passed as the only argument to a 32-bit
4908 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4909 **
4910 ** "Standard" MS-DOS time format:
4911 **
4912 **   File modification time:
4913 **     Bits 00-04: seconds divided by 2
4914 **     Bits 05-10: minute
4915 **     Bits 11-15: hour
4916 **   File modification date:
4917 **     Bits 00-04: day
4918 **     Bits 05-08: month (1-12)
4919 **     Bits 09-15: years from 1980
4920 **
4921 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4922 */
4923 static u32 zipfileMtime(ZipfileCDS *pCDS){
4924   int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4925   int M = ((pCDS->mDate >> 5) & 0x0F);
4926   int D = (pCDS->mDate & 0x1F);
4927   int B = -13;
4928 
4929   int sec = (pCDS->mTime & 0x1F)*2;
4930   int min = (pCDS->mTime >> 5) & 0x3F;
4931   int hr = (pCDS->mTime >> 11) & 0x1F;
4932   i64 JD;
4933 
4934   /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4935 
4936   /* Calculate the JD in seconds for noon on the day in question */
4937   if( M<3 ){
4938     Y = Y-1;
4939     M = M+12;
4940   }
4941   JD = (i64)(24*60*60) * (
4942       (int)(365.25 * (Y + 4716))
4943     + (int)(30.6001 * (M + 1))
4944     + D + B - 1524
4945   );
4946 
4947   /* Correct the JD for the time within the day */
4948   JD += (hr-12) * 3600 + min * 60 + sec;
4949 
4950   /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4951   return (u32)(JD - (i64)(24405875) * 24*60*6);
4952 }
4953 
4954 /*
4955 ** The opposite of zipfileMtime(). This function populates the mTime and
4956 ** mDate fields of the CDS structure passed as the first argument according
4957 ** to the UNIX timestamp value passed as the second.
4958 */
4959 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4960   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4961   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4962 
4963   int A, B, C, D, E;
4964   int yr, mon, day;
4965   int hr, min, sec;
4966 
4967   A = (int)((JD - 1867216.25)/36524.25);
4968   A = (int)(JD + 1 + A - (A/4));
4969   B = A + 1524;
4970   C = (int)((B - 122.1)/365.25);
4971   D = (36525*(C&32767))/100;
4972   E = (int)((B-D)/30.6001);
4973 
4974   day = B - D - (int)(30.6001*E);
4975   mon = (E<14 ? E-1 : E-13);
4976   yr = mon>2 ? C-4716 : C-4715;
4977 
4978   hr = (mUnixTime % (24*60*60)) / (60*60);
4979   min = (mUnixTime % (60*60)) / 60;
4980   sec = (mUnixTime % 60);
4981 
4982   if( yr>=1980 ){
4983     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4984     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4985   }else{
4986     pCds->mDate = pCds->mTime = 0;
4987   }
4988 
4989   assert( mUnixTime<315507600
4990        || mUnixTime==zipfileMtime(pCds)
4991        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
4992        /* || (mUnixTime % 2) */
4993   );
4994 }
4995 
4996 /*
4997 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4998 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4999 ** then pFile is a file-handle open on a zip file. In either case, this
5000 ** function creates a ZipfileEntry object based on the zip archive entry
5001 ** for which the CDS record is at offset iOff.
5002 **
5003 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
5004 ** the new object. Otherwise, an SQLite error code is returned and the
5005 ** final value of (*ppEntry) undefined.
5006 */
5007 static int zipfileGetEntry(
5008   ZipfileTab *pTab,               /* Store any error message here */
5009   const u8 *aBlob,                /* Pointer to in-memory file image */
5010   int nBlob,                      /* Size of aBlob[] in bytes */
5011   FILE *pFile,                    /* If aBlob==0, read from this file */
5012   i64 iOff,                       /* Offset of CDS record */
5013   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
5014 ){
5015   u8 *aRead;
5016   char **pzErr = &pTab->base.zErrMsg;
5017   int rc = SQLITE_OK;
5018 
5019   if( aBlob==0 ){
5020     aRead = pTab->aBuffer;
5021     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
5022   }else{
5023     aRead = (u8*)&aBlob[iOff];
5024   }
5025 
5026   if( rc==SQLITE_OK ){
5027     sqlite3_int64 nAlloc;
5028     ZipfileEntry *pNew;
5029 
5030     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
5031     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
5032     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
5033 
5034     nAlloc = sizeof(ZipfileEntry) + nExtra;
5035     if( aBlob ){
5036       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
5037     }
5038 
5039     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
5040     if( pNew==0 ){
5041       rc = SQLITE_NOMEM;
5042     }else{
5043       memset(pNew, 0, sizeof(ZipfileEntry));
5044       rc = zipfileReadCDS(aRead, &pNew->cds);
5045       if( rc!=SQLITE_OK ){
5046         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
5047       }else if( aBlob==0 ){
5048         rc = zipfileReadData(
5049             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
5050         );
5051       }else{
5052         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
5053       }
5054     }
5055 
5056     if( rc==SQLITE_OK ){
5057       u32 *pt = &pNew->mUnixTime;
5058       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
5059       pNew->aExtra = (u8*)&pNew[1];
5060       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
5061       if( pNew->cds.zFile==0 ){
5062         rc = SQLITE_NOMEM;
5063       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
5064         pNew->mUnixTime = zipfileMtime(&pNew->cds);
5065       }
5066     }
5067 
5068     if( rc==SQLITE_OK ){
5069       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
5070       ZipfileLFH lfh;
5071       if( pFile ){
5072         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
5073       }else{
5074         aRead = (u8*)&aBlob[pNew->cds.iOffset];
5075       }
5076 
5077       rc = zipfileReadLFH(aRead, &lfh);
5078       if( rc==SQLITE_OK ){
5079         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
5080         pNew->iDataOff += lfh.nFile + lfh.nExtra;
5081         if( aBlob && pNew->cds.szCompressed ){
5082           pNew->aData = &pNew->aExtra[nExtra];
5083           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
5084         }
5085       }else{
5086         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
5087             (int)pNew->cds.iOffset
5088         );
5089       }
5090     }
5091 
5092     if( rc!=SQLITE_OK ){
5093       zipfileEntryFree(pNew);
5094     }else{
5095       *ppEntry = pNew;
5096     }
5097   }
5098 
5099   return rc;
5100 }
5101 
5102 /*
5103 ** Advance an ZipfileCsr to its next row of output.
5104 */
5105 static int zipfileNext(sqlite3_vtab_cursor *cur){
5106   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5107   int rc = SQLITE_OK;
5108 
5109   if( pCsr->pFile ){
5110     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
5111     zipfileEntryFree(pCsr->pCurrent);
5112     pCsr->pCurrent = 0;
5113     if( pCsr->iNextOff>=iEof ){
5114       pCsr->bEof = 1;
5115     }else{
5116       ZipfileEntry *p = 0;
5117       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
5118       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
5119       if( rc==SQLITE_OK ){
5120         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
5121         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
5122       }
5123       pCsr->pCurrent = p;
5124     }
5125   }else{
5126     if( !pCsr->bNoop ){
5127       pCsr->pCurrent = pCsr->pCurrent->pNext;
5128     }
5129     if( pCsr->pCurrent==0 ){
5130       pCsr->bEof = 1;
5131     }
5132   }
5133 
5134   pCsr->bNoop = 0;
5135   return rc;
5136 }
5137 
5138 static void zipfileFree(void *p) {
5139   sqlite3_free(p);
5140 }
5141 
5142 /*
5143 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
5144 ** size is nOut bytes. This function uncompresses the data and sets the
5145 ** return value in context pCtx to the result (a blob).
5146 **
5147 ** If an error occurs, an error code is left in pCtx instead.
5148 */
5149 static void zipfileInflate(
5150   sqlite3_context *pCtx,          /* Store result here */
5151   const u8 *aIn,                  /* Compressed data */
5152   int nIn,                        /* Size of buffer aIn[] in bytes */
5153   int nOut                        /* Expected output size */
5154 ){
5155   u8 *aRes = sqlite3_malloc(nOut);
5156   if( aRes==0 ){
5157     sqlite3_result_error_nomem(pCtx);
5158   }else{
5159     int err;
5160     z_stream str;
5161     memset(&str, 0, sizeof(str));
5162 
5163     str.next_in = (Byte*)aIn;
5164     str.avail_in = nIn;
5165     str.next_out = (Byte*)aRes;
5166     str.avail_out = nOut;
5167 
5168     err = inflateInit2(&str, -15);
5169     if( err!=Z_OK ){
5170       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
5171     }else{
5172       err = inflate(&str, Z_NO_FLUSH);
5173       if( err!=Z_STREAM_END ){
5174         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
5175       }else{
5176         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
5177         aRes = 0;
5178       }
5179     }
5180     sqlite3_free(aRes);
5181     inflateEnd(&str);
5182   }
5183 }
5184 
5185 /*
5186 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
5187 ** compresses it and sets (*ppOut) to point to a buffer containing the
5188 ** compressed data. The caller is responsible for eventually calling
5189 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
5190 ** is set to the size of buffer (*ppOut) in bytes.
5191 **
5192 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
5193 ** code is returned and an error message left in virtual-table handle
5194 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5195 ** case.
5196 */
5197 static int zipfileDeflate(
5198   const u8 *aIn, int nIn,         /* Input */
5199   u8 **ppOut, int *pnOut,         /* Output */
5200   char **pzErr                    /* OUT: Error message */
5201 ){
5202   int rc = SQLITE_OK;
5203   sqlite3_int64 nAlloc;
5204   z_stream str;
5205   u8 *aOut;
5206 
5207   memset(&str, 0, sizeof(str));
5208   str.next_in = (Bytef*)aIn;
5209   str.avail_in = nIn;
5210   deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5211 
5212   nAlloc = deflateBound(&str, nIn);
5213   aOut = (u8*)sqlite3_malloc64(nAlloc);
5214   if( aOut==0 ){
5215     rc = SQLITE_NOMEM;
5216   }else{
5217     int res;
5218     str.next_out = aOut;
5219     str.avail_out = nAlloc;
5220     res = deflate(&str, Z_FINISH);
5221     if( res==Z_STREAM_END ){
5222       *ppOut = aOut;
5223       *pnOut = (int)str.total_out;
5224     }else{
5225       sqlite3_free(aOut);
5226       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5227       rc = SQLITE_ERROR;
5228     }
5229     deflateEnd(&str);
5230   }
5231 
5232   return rc;
5233 }
5234 
5235 
5236 /*
5237 ** Return values of columns for the row at which the series_cursor
5238 ** is currently pointing.
5239 */
5240 static int zipfileColumn(
5241   sqlite3_vtab_cursor *cur,   /* The cursor */
5242   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5243   int i                       /* Which column to return */
5244 ){
5245   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5246   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5247   int rc = SQLITE_OK;
5248   switch( i ){
5249     case 0:   /* name */
5250       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5251       break;
5252     case 1:   /* mode */
5253       /* TODO: Whether or not the following is correct surely depends on
5254       ** the platform on which the archive was created.  */
5255       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5256       break;
5257     case 2: { /* mtime */
5258       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5259       break;
5260     }
5261     case 3: { /* sz */
5262       if( sqlite3_vtab_nochange(ctx)==0 ){
5263         sqlite3_result_int64(ctx, pCDS->szUncompressed);
5264       }
5265       break;
5266     }
5267     case 4:   /* rawdata */
5268       if( sqlite3_vtab_nochange(ctx) ) break;
5269     case 5: { /* data */
5270       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5271         int sz = pCDS->szCompressed;
5272         int szFinal = pCDS->szUncompressed;
5273         if( szFinal>0 ){
5274           u8 *aBuf;
5275           u8 *aFree = 0;
5276           if( pCsr->pCurrent->aData ){
5277             aBuf = pCsr->pCurrent->aData;
5278           }else{
5279             aBuf = aFree = sqlite3_malloc64(sz);
5280             if( aBuf==0 ){
5281               rc = SQLITE_NOMEM;
5282             }else{
5283               FILE *pFile = pCsr->pFile;
5284               if( pFile==0 ){
5285                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5286               }
5287               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5288                   &pCsr->base.pVtab->zErrMsg
5289               );
5290             }
5291           }
5292           if( rc==SQLITE_OK ){
5293             if( i==5 && pCDS->iCompression ){
5294               zipfileInflate(ctx, aBuf, sz, szFinal);
5295             }else{
5296               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5297             }
5298           }
5299           sqlite3_free(aFree);
5300         }else{
5301           /* Figure out if this is a directory or a zero-sized file. Consider
5302           ** it to be a directory either if the mode suggests so, or if
5303           ** the final character in the name is '/'.  */
5304           u32 mode = pCDS->iExternalAttr >> 16;
5305           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5306             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5307           }
5308         }
5309       }
5310       break;
5311     }
5312     case 6:   /* method */
5313       sqlite3_result_int(ctx, pCDS->iCompression);
5314       break;
5315     default:  /* z */
5316       assert( i==7 );
5317       sqlite3_result_int64(ctx, pCsr->iId);
5318       break;
5319   }
5320 
5321   return rc;
5322 }
5323 
5324 /*
5325 ** Return TRUE if the cursor is at EOF.
5326 */
5327 static int zipfileEof(sqlite3_vtab_cursor *cur){
5328   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5329   return pCsr->bEof;
5330 }
5331 
5332 /*
5333 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5334 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5335 ** is guaranteed to be a file-handle open on a zip file.
5336 **
5337 ** This function attempts to locate the EOCD record within the zip archive
5338 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5339 ** returned if successful. Otherwise, an SQLite error code is returned and
5340 ** an English language error message may be left in virtual-table pTab.
5341 */
5342 static int zipfileReadEOCD(
5343   ZipfileTab *pTab,               /* Return errors here */
5344   const u8 *aBlob,                /* Pointer to in-memory file image */
5345   int nBlob,                      /* Size of aBlob[] in bytes */
5346   FILE *pFile,                    /* Read from this file if aBlob==0 */
5347   ZipfileEOCD *pEOCD              /* Object to populate */
5348 ){
5349   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
5350   int nRead;                      /* Bytes to read from file */
5351   int rc = SQLITE_OK;
5352 
5353   if( aBlob==0 ){
5354     i64 iOff;                     /* Offset to read from */
5355     i64 szFile;                   /* Total size of file in bytes */
5356     fseek(pFile, 0, SEEK_END);
5357     szFile = (i64)ftell(pFile);
5358     if( szFile==0 ){
5359       memset(pEOCD, 0, sizeof(ZipfileEOCD));
5360       return SQLITE_OK;
5361     }
5362     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5363     iOff = szFile - nRead;
5364     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5365   }else{
5366     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5367     aRead = (u8*)&aBlob[nBlob-nRead];
5368   }
5369 
5370   if( rc==SQLITE_OK ){
5371     int i;
5372 
5373     /* Scan backwards looking for the signature bytes */
5374     for(i=nRead-20; i>=0; i--){
5375       if( aRead[i]==0x50 && aRead[i+1]==0x4b
5376        && aRead[i+2]==0x05 && aRead[i+3]==0x06
5377       ){
5378         break;
5379       }
5380     }
5381     if( i<0 ){
5382       pTab->base.zErrMsg = sqlite3_mprintf(
5383           "cannot find end of central directory record"
5384       );
5385       return SQLITE_ERROR;
5386     }
5387 
5388     aRead += i+4;
5389     pEOCD->iDisk = zipfileRead16(aRead);
5390     pEOCD->iFirstDisk = zipfileRead16(aRead);
5391     pEOCD->nEntry = zipfileRead16(aRead);
5392     pEOCD->nEntryTotal = zipfileRead16(aRead);
5393     pEOCD->nSize = zipfileRead32(aRead);
5394     pEOCD->iOffset = zipfileRead32(aRead);
5395   }
5396 
5397   return rc;
5398 }
5399 
5400 /*
5401 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5402 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5403 ** to the end of the list. Otherwise, it is added to the list immediately
5404 ** before pBefore (which is guaranteed to be a part of said list).
5405 */
5406 static void zipfileAddEntry(
5407   ZipfileTab *pTab,
5408   ZipfileEntry *pBefore,
5409   ZipfileEntry *pNew
5410 ){
5411   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5412   assert( pNew->pNext==0 );
5413   if( pBefore==0 ){
5414     if( pTab->pFirstEntry==0 ){
5415       pTab->pFirstEntry = pTab->pLastEntry = pNew;
5416     }else{
5417       assert( pTab->pLastEntry->pNext==0 );
5418       pTab->pLastEntry->pNext = pNew;
5419       pTab->pLastEntry = pNew;
5420     }
5421   }else{
5422     ZipfileEntry **pp;
5423     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5424     pNew->pNext = pBefore;
5425     *pp = pNew;
5426   }
5427 }
5428 
5429 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5430   ZipfileEOCD eocd;
5431   int rc;
5432   int i;
5433   i64 iOff;
5434 
5435   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5436   iOff = eocd.iOffset;
5437   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5438     ZipfileEntry *pNew = 0;
5439     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5440 
5441     if( rc==SQLITE_OK ){
5442       zipfileAddEntry(pTab, 0, pNew);
5443       iOff += ZIPFILE_CDS_FIXED_SZ;
5444       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5445     }
5446   }
5447   return rc;
5448 }
5449 
5450 /*
5451 ** xFilter callback.
5452 */
5453 static int zipfileFilter(
5454   sqlite3_vtab_cursor *cur,
5455   int idxNum, const char *idxStr,
5456   int argc, sqlite3_value **argv
5457 ){
5458   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5459   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5460   const char *zFile = 0;          /* Zip file to scan */
5461   int rc = SQLITE_OK;             /* Return Code */
5462   int bInMemory = 0;              /* True for an in-memory zipfile */
5463 
5464   zipfileResetCursor(pCsr);
5465 
5466   if( pTab->zFile ){
5467     zFile = pTab->zFile;
5468   }else if( idxNum==0 ){
5469     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5470     return SQLITE_ERROR;
5471   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5472     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5473     int nBlob = sqlite3_value_bytes(argv[0]);
5474     assert( pTab->pFirstEntry==0 );
5475     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5476     pCsr->pFreeEntry = pTab->pFirstEntry;
5477     pTab->pFirstEntry = pTab->pLastEntry = 0;
5478     if( rc!=SQLITE_OK ) return rc;
5479     bInMemory = 1;
5480   }else{
5481     zFile = (const char*)sqlite3_value_text(argv[0]);
5482   }
5483 
5484   if( 0==pTab->pWriteFd && 0==bInMemory ){
5485     pCsr->pFile = fopen(zFile, "rb");
5486     if( pCsr->pFile==0 ){
5487       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5488       rc = SQLITE_ERROR;
5489     }else{
5490       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5491       if( rc==SQLITE_OK ){
5492         if( pCsr->eocd.nEntry==0 ){
5493           pCsr->bEof = 1;
5494         }else{
5495           pCsr->iNextOff = pCsr->eocd.iOffset;
5496           rc = zipfileNext(cur);
5497         }
5498       }
5499     }
5500   }else{
5501     pCsr->bNoop = 1;
5502     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5503     rc = zipfileNext(cur);
5504   }
5505 
5506   return rc;
5507 }
5508 
5509 /*
5510 ** xBestIndex callback.
5511 */
5512 static int zipfileBestIndex(
5513   sqlite3_vtab *tab,
5514   sqlite3_index_info *pIdxInfo
5515 ){
5516   int i;
5517   int idx = -1;
5518   int unusable = 0;
5519 
5520   for(i=0; i<pIdxInfo->nConstraint; i++){
5521     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5522     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5523     if( pCons->usable==0 ){
5524       unusable = 1;
5525     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5526       idx = i;
5527     }
5528   }
5529   pIdxInfo->estimatedCost = 1000.0;
5530   if( idx>=0 ){
5531     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
5532     pIdxInfo->aConstraintUsage[idx].omit = 1;
5533     pIdxInfo->idxNum = 1;
5534   }else if( unusable ){
5535     return SQLITE_CONSTRAINT;
5536   }
5537   return SQLITE_OK;
5538 }
5539 
5540 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5541   ZipfileEntry *pNew;
5542   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5543   if( pNew ){
5544     memset(pNew, 0, sizeof(ZipfileEntry));
5545     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5546     if( pNew->cds.zFile==0 ){
5547       sqlite3_free(pNew);
5548       pNew = 0;
5549     }
5550   }
5551   return pNew;
5552 }
5553 
5554 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5555   ZipfileCDS *pCds = &pEntry->cds;
5556   u8 *a = aBuf;
5557 
5558   pCds->nExtra = 9;
5559 
5560   /* Write the LFH itself */
5561   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5562   zipfileWrite16(a, pCds->iVersionExtract);
5563   zipfileWrite16(a, pCds->flags);
5564   zipfileWrite16(a, pCds->iCompression);
5565   zipfileWrite16(a, pCds->mTime);
5566   zipfileWrite16(a, pCds->mDate);
5567   zipfileWrite32(a, pCds->crc32);
5568   zipfileWrite32(a, pCds->szCompressed);
5569   zipfileWrite32(a, pCds->szUncompressed);
5570   zipfileWrite16(a, (u16)pCds->nFile);
5571   zipfileWrite16(a, pCds->nExtra);
5572   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5573 
5574   /* Add the file name */
5575   memcpy(a, pCds->zFile, (int)pCds->nFile);
5576   a += (int)pCds->nFile;
5577 
5578   /* The "extra" data */
5579   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5580   zipfileWrite16(a, 5);
5581   *a++ = 0x01;
5582   zipfileWrite32(a, pEntry->mUnixTime);
5583 
5584   return a-aBuf;
5585 }
5586 
5587 static int zipfileAppendEntry(
5588   ZipfileTab *pTab,
5589   ZipfileEntry *pEntry,
5590   const u8 *pData,
5591   int nData
5592 ){
5593   u8 *aBuf = pTab->aBuffer;
5594   int nBuf;
5595   int rc;
5596 
5597   nBuf = zipfileSerializeLFH(pEntry, aBuf);
5598   rc = zipfileAppendData(pTab, aBuf, nBuf);
5599   if( rc==SQLITE_OK ){
5600     pEntry->iDataOff = pTab->szCurrent;
5601     rc = zipfileAppendData(pTab, pData, nData);
5602   }
5603 
5604   return rc;
5605 }
5606 
5607 static int zipfileGetMode(
5608   sqlite3_value *pVal,
5609   int bIsDir,                     /* If true, default to directory */
5610   u32 *pMode,                     /* OUT: Mode value */
5611   char **pzErr                    /* OUT: Error message */
5612 ){
5613   const char *z = (const char*)sqlite3_value_text(pVal);
5614   u32 mode = 0;
5615   if( z==0 ){
5616     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5617   }else if( z[0]>='0' && z[0]<='9' ){
5618     mode = (unsigned int)sqlite3_value_int(pVal);
5619   }else{
5620     const char zTemplate[11] = "-rwxrwxrwx";
5621     int i;
5622     if( strlen(z)!=10 ) goto parse_error;
5623     switch( z[0] ){
5624       case '-': mode |= S_IFREG; break;
5625       case 'd': mode |= S_IFDIR; break;
5626       case 'l': mode |= S_IFLNK; break;
5627       default: goto parse_error;
5628     }
5629     for(i=1; i<10; i++){
5630       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5631       else if( z[i]!='-' ) goto parse_error;
5632     }
5633   }
5634   if( ((mode & S_IFDIR)==0)==bIsDir ){
5635     /* The "mode" attribute is a directory, but data has been specified.
5636     ** Or vice-versa - no data but "mode" is a file or symlink.  */
5637     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5638     return SQLITE_CONSTRAINT;
5639   }
5640   *pMode = mode;
5641   return SQLITE_OK;
5642 
5643  parse_error:
5644   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5645   return SQLITE_ERROR;
5646 }
5647 
5648 /*
5649 ** Both (const char*) arguments point to nul-terminated strings. Argument
5650 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5651 ** identical, ignoring any trailing '/' character in either path.  */
5652 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5653   int nA = (int)strlen(zA);
5654   if( nA>0 && zA[nA-1]=='/' ) nA--;
5655   if( nB>0 && zB[nB-1]=='/' ) nB--;
5656   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5657   return 1;
5658 }
5659 
5660 static int zipfileBegin(sqlite3_vtab *pVtab){
5661   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5662   int rc = SQLITE_OK;
5663 
5664   assert( pTab->pWriteFd==0 );
5665   if( pTab->zFile==0 || pTab->zFile[0]==0 ){
5666     pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
5667     return SQLITE_ERROR;
5668   }
5669 
5670   /* Open a write fd on the file. Also load the entire central directory
5671   ** structure into memory. During the transaction any new file data is
5672   ** appended to the archive file, but the central directory is accumulated
5673   ** in main-memory until the transaction is committed.  */
5674   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5675   if( pTab->pWriteFd==0 ){
5676     pTab->base.zErrMsg = sqlite3_mprintf(
5677         "zipfile: failed to open file %s for writing", pTab->zFile
5678         );
5679     rc = SQLITE_ERROR;
5680   }else{
5681     fseek(pTab->pWriteFd, 0, SEEK_END);
5682     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5683     rc = zipfileLoadDirectory(pTab, 0, 0);
5684   }
5685 
5686   if( rc!=SQLITE_OK ){
5687     zipfileCleanupTransaction(pTab);
5688   }
5689 
5690   return rc;
5691 }
5692 
5693 /*
5694 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5695 ** time(2)).
5696 */
5697 static u32 zipfileTime(void){
5698   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5699   u32 ret;
5700   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5701     i64 ms;
5702     pVfs->xCurrentTimeInt64(pVfs, &ms);
5703     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5704   }else{
5705     double day;
5706     pVfs->xCurrentTime(pVfs, &day);
5707     ret = (u32)((day - 2440587.5) * 86400);
5708   }
5709   return ret;
5710 }
5711 
5712 /*
5713 ** Return a 32-bit timestamp in UNIX epoch format.
5714 **
5715 ** If the value passed as the only argument is either NULL or an SQL NULL,
5716 ** return the current time. Otherwise, return the value stored in (*pVal)
5717 ** cast to a 32-bit unsigned integer.
5718 */
5719 static u32 zipfileGetTime(sqlite3_value *pVal){
5720   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5721     return zipfileTime();
5722   }
5723   return (u32)sqlite3_value_int64(pVal);
5724 }
5725 
5726 /*
5727 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5728 ** linked list.  Remove it from the list and free the object.
5729 */
5730 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5731   if( pOld ){
5732     ZipfileEntry **pp;
5733     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5734     *pp = (*pp)->pNext;
5735     zipfileEntryFree(pOld);
5736   }
5737 }
5738 
5739 /*
5740 ** xUpdate method.
5741 */
5742 static int zipfileUpdate(
5743   sqlite3_vtab *pVtab,
5744   int nVal,
5745   sqlite3_value **apVal,
5746   sqlite_int64 *pRowid
5747 ){
5748   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5749   int rc = SQLITE_OK;             /* Return Code */
5750   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
5751 
5752   u32 mode = 0;                   /* Mode for new entry */
5753   u32 mTime = 0;                  /* Modification time for new entry */
5754   i64 sz = 0;                     /* Uncompressed size */
5755   const char *zPath = 0;          /* Path for new entry */
5756   int nPath = 0;                  /* strlen(zPath) */
5757   const u8 *pData = 0;            /* Pointer to buffer containing content */
5758   int nData = 0;                  /* Size of pData buffer in bytes */
5759   int iMethod = 0;                /* Compression method for new entry */
5760   u8 *pFree = 0;                  /* Free this */
5761   char *zFree = 0;                /* Also free this */
5762   ZipfileEntry *pOld = 0;
5763   ZipfileEntry *pOld2 = 0;
5764   int bUpdate = 0;                /* True for an update that modifies "name" */
5765   int bIsDir = 0;
5766   u32 iCrc32 = 0;
5767 
5768   if( pTab->pWriteFd==0 ){
5769     rc = zipfileBegin(pVtab);
5770     if( rc!=SQLITE_OK ) return rc;
5771   }
5772 
5773   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5774   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5775     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5776     int nDelete = (int)strlen(zDelete);
5777     if( nVal>1 ){
5778       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5779       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5780         bUpdate = 1;
5781       }
5782     }
5783     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5784       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5785         break;
5786       }
5787       assert( pOld->pNext );
5788     }
5789   }
5790 
5791   if( nVal>1 ){
5792     /* Check that "sz" and "rawdata" are both NULL: */
5793     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5794       zipfileTableErr(pTab, "sz must be NULL");
5795       rc = SQLITE_CONSTRAINT;
5796     }
5797     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5798       zipfileTableErr(pTab, "rawdata must be NULL");
5799       rc = SQLITE_CONSTRAINT;
5800     }
5801 
5802     if( rc==SQLITE_OK ){
5803       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5804         /* data=NULL. A directory */
5805         bIsDir = 1;
5806       }else{
5807         /* Value specified for "data", and possibly "method". This must be
5808         ** a regular file or a symlink. */
5809         const u8 *aIn = sqlite3_value_blob(apVal[7]);
5810         int nIn = sqlite3_value_bytes(apVal[7]);
5811         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5812 
5813         iMethod = sqlite3_value_int(apVal[8]);
5814         sz = nIn;
5815         pData = aIn;
5816         nData = nIn;
5817         if( iMethod!=0 && iMethod!=8 ){
5818           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5819           rc = SQLITE_CONSTRAINT;
5820         }else{
5821           if( bAuto || iMethod ){
5822             int nCmp;
5823             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5824             if( rc==SQLITE_OK ){
5825               if( iMethod || nCmp<nIn ){
5826                 iMethod = 8;
5827                 pData = pFree;
5828                 nData = nCmp;
5829               }
5830             }
5831           }
5832           iCrc32 = crc32(0, aIn, nIn);
5833         }
5834       }
5835     }
5836 
5837     if( rc==SQLITE_OK ){
5838       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5839     }
5840 
5841     if( rc==SQLITE_OK ){
5842       zPath = (const char*)sqlite3_value_text(apVal[2]);
5843       if( zPath==0 ) zPath = "";
5844       nPath = (int)strlen(zPath);
5845       mTime = zipfileGetTime(apVal[4]);
5846     }
5847 
5848     if( rc==SQLITE_OK && bIsDir ){
5849       /* For a directory, check that the last character in the path is a
5850       ** '/'. This appears to be required for compatibility with info-zip
5851       ** (the unzip command on unix). It does not create directories
5852       ** otherwise.  */
5853       if( nPath<=0 || zPath[nPath-1]!='/' ){
5854         zFree = sqlite3_mprintf("%s/", zPath);
5855         zPath = (const char*)zFree;
5856         if( zFree==0 ){
5857           rc = SQLITE_NOMEM;
5858           nPath = 0;
5859         }else{
5860           nPath = (int)strlen(zPath);
5861         }
5862       }
5863     }
5864 
5865     /* Check that we're not inserting a duplicate entry -OR- updating an
5866     ** entry with a path, thereby making it into a duplicate. */
5867     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
5868       ZipfileEntry *p;
5869       for(p=pTab->pFirstEntry; p; p=p->pNext){
5870         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5871           switch( sqlite3_vtab_on_conflict(pTab->db) ){
5872             case SQLITE_IGNORE: {
5873               goto zipfile_update_done;
5874             }
5875             case SQLITE_REPLACE: {
5876               pOld2 = p;
5877               break;
5878             }
5879             default: {
5880               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5881               rc = SQLITE_CONSTRAINT;
5882               break;
5883             }
5884           }
5885           break;
5886         }
5887       }
5888     }
5889 
5890     if( rc==SQLITE_OK ){
5891       /* Create the new CDS record. */
5892       pNew = zipfileNewEntry(zPath);
5893       if( pNew==0 ){
5894         rc = SQLITE_NOMEM;
5895       }else{
5896         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5897         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5898         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5899         pNew->cds.iCompression = (u16)iMethod;
5900         zipfileMtimeToDos(&pNew->cds, mTime);
5901         pNew->cds.crc32 = iCrc32;
5902         pNew->cds.szCompressed = nData;
5903         pNew->cds.szUncompressed = (u32)sz;
5904         pNew->cds.iExternalAttr = (mode<<16);
5905         pNew->cds.iOffset = (u32)pTab->szCurrent;
5906         pNew->cds.nFile = (u16)nPath;
5907         pNew->mUnixTime = (u32)mTime;
5908         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5909         zipfileAddEntry(pTab, pOld, pNew);
5910       }
5911     }
5912   }
5913 
5914   if( rc==SQLITE_OK && (pOld || pOld2) ){
5915     ZipfileCsr *pCsr;
5916     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5917       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
5918         pCsr->pCurrent = pCsr->pCurrent->pNext;
5919         pCsr->bNoop = 1;
5920       }
5921     }
5922 
5923     zipfileRemoveEntryFromList(pTab, pOld);
5924     zipfileRemoveEntryFromList(pTab, pOld2);
5925   }
5926 
5927 zipfile_update_done:
5928   sqlite3_free(pFree);
5929   sqlite3_free(zFree);
5930   return rc;
5931 }
5932 
5933 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5934   u8 *a = aBuf;
5935   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5936   zipfileWrite16(a, p->iDisk);
5937   zipfileWrite16(a, p->iFirstDisk);
5938   zipfileWrite16(a, p->nEntry);
5939   zipfileWrite16(a, p->nEntryTotal);
5940   zipfileWrite32(a, p->nSize);
5941   zipfileWrite32(a, p->iOffset);
5942   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
5943 
5944   return a-aBuf;
5945 }
5946 
5947 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5948   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5949   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5950   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5951 }
5952 
5953 /*
5954 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5955 ** of bytes written.
5956 */
5957 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5958   u8 *a = aBuf;
5959   ZipfileCDS *pCDS = &pEntry->cds;
5960 
5961   if( pEntry->aExtra==0 ){
5962     pCDS->nExtra = 9;
5963   }
5964 
5965   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5966   zipfileWrite16(a, pCDS->iVersionMadeBy);
5967   zipfileWrite16(a, pCDS->iVersionExtract);
5968   zipfileWrite16(a, pCDS->flags);
5969   zipfileWrite16(a, pCDS->iCompression);
5970   zipfileWrite16(a, pCDS->mTime);
5971   zipfileWrite16(a, pCDS->mDate);
5972   zipfileWrite32(a, pCDS->crc32);
5973   zipfileWrite32(a, pCDS->szCompressed);
5974   zipfileWrite32(a, pCDS->szUncompressed);
5975   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5976   zipfileWrite16(a, pCDS->nFile);
5977   zipfileWrite16(a, pCDS->nExtra);
5978   zipfileWrite16(a, pCDS->nComment);
5979   zipfileWrite16(a, pCDS->iDiskStart);
5980   zipfileWrite16(a, pCDS->iInternalAttr);
5981   zipfileWrite32(a, pCDS->iExternalAttr);
5982   zipfileWrite32(a, pCDS->iOffset);
5983 
5984   memcpy(a, pCDS->zFile, pCDS->nFile);
5985   a += pCDS->nFile;
5986 
5987   if( pEntry->aExtra ){
5988     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5989     memcpy(a, pEntry->aExtra, n);
5990     a += n;
5991   }else{
5992     assert( pCDS->nExtra==9 );
5993     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5994     zipfileWrite16(a, 5);
5995     *a++ = 0x01;
5996     zipfileWrite32(a, pEntry->mUnixTime);
5997   }
5998 
5999   return a-aBuf;
6000 }
6001 
6002 static int zipfileCommit(sqlite3_vtab *pVtab){
6003   ZipfileTab *pTab = (ZipfileTab*)pVtab;
6004   int rc = SQLITE_OK;
6005   if( pTab->pWriteFd ){
6006     i64 iOffset = pTab->szCurrent;
6007     ZipfileEntry *p;
6008     ZipfileEOCD eocd;
6009     int nEntry = 0;
6010 
6011     /* Write out all entries */
6012     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
6013       int n = zipfileSerializeCDS(p, pTab->aBuffer);
6014       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
6015       nEntry++;
6016     }
6017 
6018     /* Write out the EOCD record */
6019     eocd.iDisk = 0;
6020     eocd.iFirstDisk = 0;
6021     eocd.nEntry = (u16)nEntry;
6022     eocd.nEntryTotal = (u16)nEntry;
6023     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
6024     eocd.iOffset = (u32)iOffset;
6025     rc = zipfileAppendEOCD(pTab, &eocd);
6026 
6027     zipfileCleanupTransaction(pTab);
6028   }
6029   return rc;
6030 }
6031 
6032 static int zipfileRollback(sqlite3_vtab *pVtab){
6033   return zipfileCommit(pVtab);
6034 }
6035 
6036 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
6037   ZipfileCsr *pCsr;
6038   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6039     if( iId==pCsr->iId ) break;
6040   }
6041   return pCsr;
6042 }
6043 
6044 static void zipfileFunctionCds(
6045   sqlite3_context *context,
6046   int argc,
6047   sqlite3_value **argv
6048 ){
6049   ZipfileCsr *pCsr;
6050   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
6051   assert( argc>0 );
6052 
6053   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
6054   if( pCsr ){
6055     ZipfileCDS *p = &pCsr->pCurrent->cds;
6056     char *zRes = sqlite3_mprintf("{"
6057         "\"version-made-by\" : %u, "
6058         "\"version-to-extract\" : %u, "
6059         "\"flags\" : %u, "
6060         "\"compression\" : %u, "
6061         "\"time\" : %u, "
6062         "\"date\" : %u, "
6063         "\"crc32\" : %u, "
6064         "\"compressed-size\" : %u, "
6065         "\"uncompressed-size\" : %u, "
6066         "\"file-name-length\" : %u, "
6067         "\"extra-field-length\" : %u, "
6068         "\"file-comment-length\" : %u, "
6069         "\"disk-number-start\" : %u, "
6070         "\"internal-attr\" : %u, "
6071         "\"external-attr\" : %u, "
6072         "\"offset\" : %u }",
6073         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
6074         (u32)p->flags, (u32)p->iCompression,
6075         (u32)p->mTime, (u32)p->mDate,
6076         (u32)p->crc32, (u32)p->szCompressed,
6077         (u32)p->szUncompressed, (u32)p->nFile,
6078         (u32)p->nExtra, (u32)p->nComment,
6079         (u32)p->iDiskStart, (u32)p->iInternalAttr,
6080         (u32)p->iExternalAttr, (u32)p->iOffset
6081     );
6082 
6083     if( zRes==0 ){
6084       sqlite3_result_error_nomem(context);
6085     }else{
6086       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
6087       sqlite3_free(zRes);
6088     }
6089   }
6090 }
6091 
6092 /*
6093 ** xFindFunction method.
6094 */
6095 static int zipfileFindFunction(
6096   sqlite3_vtab *pVtab,            /* Virtual table handle */
6097   int nArg,                       /* Number of SQL function arguments */
6098   const char *zName,              /* Name of SQL function */
6099   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
6100   void **ppArg                    /* OUT: User data for *pxFunc */
6101 ){
6102   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
6103     *pxFunc = zipfileFunctionCds;
6104     *ppArg = (void*)pVtab;
6105     return 1;
6106   }
6107   return 0;
6108 }
6109 
6110 typedef struct ZipfileBuffer ZipfileBuffer;
6111 struct ZipfileBuffer {
6112   u8 *a;                          /* Pointer to buffer */
6113   int n;                          /* Size of buffer in bytes */
6114   int nAlloc;                     /* Byte allocated at a[] */
6115 };
6116 
6117 typedef struct ZipfileCtx ZipfileCtx;
6118 struct ZipfileCtx {
6119   int nEntry;
6120   ZipfileBuffer body;
6121   ZipfileBuffer cds;
6122 };
6123 
6124 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
6125   if( pBuf->n+nByte>pBuf->nAlloc ){
6126     u8 *aNew;
6127     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
6128     int nReq = pBuf->n + nByte;
6129 
6130     while( nNew<nReq ) nNew = nNew*2;
6131     aNew = sqlite3_realloc64(pBuf->a, nNew);
6132     if( aNew==0 ) return SQLITE_NOMEM;
6133     pBuf->a = aNew;
6134     pBuf->nAlloc = (int)nNew;
6135   }
6136   return SQLITE_OK;
6137 }
6138 
6139 /*
6140 ** xStep() callback for the zipfile() aggregate. This can be called in
6141 ** any of the following ways:
6142 **
6143 **   SELECT zipfile(name,data) ...
6144 **   SELECT zipfile(name,mode,mtime,data) ...
6145 **   SELECT zipfile(name,mode,mtime,data,method) ...
6146 */
6147 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
6148   ZipfileCtx *p;                  /* Aggregate function context */
6149   ZipfileEntry e;                 /* New entry to add to zip archive */
6150 
6151   sqlite3_value *pName = 0;
6152   sqlite3_value *pMode = 0;
6153   sqlite3_value *pMtime = 0;
6154   sqlite3_value *pData = 0;
6155   sqlite3_value *pMethod = 0;
6156 
6157   int bIsDir = 0;
6158   u32 mode;
6159   int rc = SQLITE_OK;
6160   char *zErr = 0;
6161 
6162   int iMethod = -1;               /* Compression method to use (0 or 8) */
6163 
6164   const u8 *aData = 0;            /* Possibly compressed data for new entry */
6165   int nData = 0;                  /* Size of aData[] in bytes */
6166   int szUncompressed = 0;         /* Size of data before compression */
6167   u8 *aFree = 0;                  /* Free this before returning */
6168   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
6169 
6170   char *zName = 0;                /* Path (name) of new entry */
6171   int nName = 0;                  /* Size of zName in bytes */
6172   char *zFree = 0;                /* Free this before returning */
6173   int nByte;
6174 
6175   memset(&e, 0, sizeof(e));
6176   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6177   if( p==0 ) return;
6178 
6179   /* Martial the arguments into stack variables */
6180   if( nVal!=2 && nVal!=4 && nVal!=5 ){
6181     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
6182     rc = SQLITE_ERROR;
6183     goto zipfile_step_out;
6184   }
6185   pName = apVal[0];
6186   if( nVal==2 ){
6187     pData = apVal[1];
6188   }else{
6189     pMode = apVal[1];
6190     pMtime = apVal[2];
6191     pData = apVal[3];
6192     if( nVal==5 ){
6193       pMethod = apVal[4];
6194     }
6195   }
6196 
6197   /* Check that the 'name' parameter looks ok. */
6198   zName = (char*)sqlite3_value_text(pName);
6199   nName = sqlite3_value_bytes(pName);
6200   if( zName==0 ){
6201     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
6202     rc = SQLITE_ERROR;
6203     goto zipfile_step_out;
6204   }
6205 
6206   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6207   ** deflate compression) or NULL (choose automatically).  */
6208   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6209     iMethod = (int)sqlite3_value_int64(pMethod);
6210     if( iMethod!=0 && iMethod!=8 ){
6211       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6212       rc = SQLITE_ERROR;
6213       goto zipfile_step_out;
6214     }
6215   }
6216 
6217   /* Now inspect the data. If this is NULL, then the new entry must be a
6218   ** directory.  Otherwise, figure out whether or not the data should
6219   ** be deflated or simply stored in the zip archive. */
6220   if( sqlite3_value_type(pData)==SQLITE_NULL ){
6221     bIsDir = 1;
6222     iMethod = 0;
6223   }else{
6224     aData = sqlite3_value_blob(pData);
6225     szUncompressed = nData = sqlite3_value_bytes(pData);
6226     iCrc32 = crc32(0, aData, nData);
6227     if( iMethod<0 || iMethod==8 ){
6228       int nOut = 0;
6229       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6230       if( rc!=SQLITE_OK ){
6231         goto zipfile_step_out;
6232       }
6233       if( iMethod==8 || nOut<nData ){
6234         aData = aFree;
6235         nData = nOut;
6236         iMethod = 8;
6237       }else{
6238         iMethod = 0;
6239       }
6240     }
6241   }
6242 
6243   /* Decode the "mode" argument. */
6244   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6245   if( rc ) goto zipfile_step_out;
6246 
6247   /* Decode the "mtime" argument. */
6248   e.mUnixTime = zipfileGetTime(pMtime);
6249 
6250   /* If this is a directory entry, ensure that there is exactly one '/'
6251   ** at the end of the path. Or, if this is not a directory and the path
6252   ** ends in '/' it is an error. */
6253   if( bIsDir==0 ){
6254     if( nName>0 && zName[nName-1]=='/' ){
6255       zErr = sqlite3_mprintf("non-directory name must not end with /");
6256       rc = SQLITE_ERROR;
6257       goto zipfile_step_out;
6258     }
6259   }else{
6260     if( nName==0 || zName[nName-1]!='/' ){
6261       zName = zFree = sqlite3_mprintf("%s/", zName);
6262       if( zName==0 ){
6263         rc = SQLITE_NOMEM;
6264         goto zipfile_step_out;
6265       }
6266       nName = (int)strlen(zName);
6267     }else{
6268       while( nName>1 && zName[nName-2]=='/' ) nName--;
6269     }
6270   }
6271 
6272   /* Assemble the ZipfileEntry object for the new zip archive entry */
6273   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6274   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6275   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6276   e.cds.iCompression = (u16)iMethod;
6277   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6278   e.cds.crc32 = iCrc32;
6279   e.cds.szCompressed = nData;
6280   e.cds.szUncompressed = szUncompressed;
6281   e.cds.iExternalAttr = (mode<<16);
6282   e.cds.iOffset = p->body.n;
6283   e.cds.nFile = (u16)nName;
6284   e.cds.zFile = zName;
6285 
6286   /* Append the LFH to the body of the new archive */
6287   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6288   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6289   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6290 
6291   /* Append the data to the body of the new archive */
6292   if( nData>0 ){
6293     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6294     memcpy(&p->body.a[p->body.n], aData, nData);
6295     p->body.n += nData;
6296   }
6297 
6298   /* Append the CDS record to the directory of the new archive */
6299   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6300   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6301   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6302 
6303   /* Increment the count of entries in the archive */
6304   p->nEntry++;
6305 
6306  zipfile_step_out:
6307   sqlite3_free(aFree);
6308   sqlite3_free(zFree);
6309   if( rc ){
6310     if( zErr ){
6311       sqlite3_result_error(pCtx, zErr, -1);
6312     }else{
6313       sqlite3_result_error_code(pCtx, rc);
6314     }
6315   }
6316   sqlite3_free(zErr);
6317 }
6318 
6319 /*
6320 ** xFinalize() callback for zipfile aggregate function.
6321 */
6322 void zipfileFinal(sqlite3_context *pCtx){
6323   ZipfileCtx *p;
6324   ZipfileEOCD eocd;
6325   sqlite3_int64 nZip;
6326   u8 *aZip;
6327 
6328   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6329   if( p==0 ) return;
6330   if( p->nEntry>0 ){
6331     memset(&eocd, 0, sizeof(eocd));
6332     eocd.nEntry = (u16)p->nEntry;
6333     eocd.nEntryTotal = (u16)p->nEntry;
6334     eocd.nSize = p->cds.n;
6335     eocd.iOffset = p->body.n;
6336 
6337     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6338     aZip = (u8*)sqlite3_malloc64(nZip);
6339     if( aZip==0 ){
6340       sqlite3_result_error_nomem(pCtx);
6341     }else{
6342       memcpy(aZip, p->body.a, p->body.n);
6343       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6344       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6345       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
6346     }
6347   }
6348 
6349   sqlite3_free(p->body.a);
6350   sqlite3_free(p->cds.a);
6351 }
6352 
6353 
6354 /*
6355 ** Register the "zipfile" virtual table.
6356 */
6357 static int zipfileRegister(sqlite3 *db){
6358   static sqlite3_module zipfileModule = {
6359     1,                         /* iVersion */
6360     zipfileConnect,            /* xCreate */
6361     zipfileConnect,            /* xConnect */
6362     zipfileBestIndex,          /* xBestIndex */
6363     zipfileDisconnect,         /* xDisconnect */
6364     zipfileDisconnect,         /* xDestroy */
6365     zipfileOpen,               /* xOpen - open a cursor */
6366     zipfileClose,              /* xClose - close a cursor */
6367     zipfileFilter,             /* xFilter - configure scan constraints */
6368     zipfileNext,               /* xNext - advance a cursor */
6369     zipfileEof,                /* xEof - check for end of scan */
6370     zipfileColumn,             /* xColumn - read data */
6371     0,                         /* xRowid - read data */
6372     zipfileUpdate,             /* xUpdate */
6373     zipfileBegin,              /* xBegin */
6374     0,                         /* xSync */
6375     zipfileCommit,             /* xCommit */
6376     zipfileRollback,           /* xRollback */
6377     zipfileFindFunction,       /* xFindMethod */
6378     0,                         /* xRename */
6379   };
6380 
6381   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
6382   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6383   if( rc==SQLITE_OK ){
6384     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6385         zipfileStep, zipfileFinal
6386     );
6387   }
6388   return rc;
6389 }
6390 #else         /* SQLITE_OMIT_VIRTUALTABLE */
6391 # define zipfileRegister(x) SQLITE_OK
6392 #endif
6393 
6394 #ifdef _WIN32
6395 
6396 #endif
6397 int sqlite3_zipfile_init(
6398   sqlite3 *db,
6399   char **pzErrMsg,
6400   const sqlite3_api_routines *pApi
6401 ){
6402   SQLITE_EXTENSION_INIT2(pApi);
6403   (void)pzErrMsg;  /* Unused parameter */
6404   return zipfileRegister(db);
6405 }
6406 
6407 /************************* End ../ext/misc/zipfile.c ********************/
6408 /************************* Begin ../ext/misc/sqlar.c ******************/
6409 /*
6410 ** 2017-12-17
6411 **
6412 ** The author disclaims copyright to this source code.  In place of
6413 ** a legal notice, here is a blessing:
6414 **
6415 **    May you do good and not evil.
6416 **    May you find forgiveness for yourself and forgive others.
6417 **    May you share freely, never taking more than you give.
6418 **
6419 ******************************************************************************
6420 **
6421 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6422 ** for working with sqlar archives and used by the shell tool's built-in
6423 ** sqlar support.
6424 */
6425 /* #include "sqlite3ext.h" */
6426 SQLITE_EXTENSION_INIT1
6427 #include <zlib.h>
6428 
6429 /*
6430 ** Implementation of the "sqlar_compress(X)" SQL function.
6431 **
6432 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6433 ** zlib utility function compress() yields a smaller blob, return the
6434 ** compressed blob. Otherwise, return a copy of X.
6435 **
6436 ** SQLar uses the "zlib format" for compressed content.  The zlib format
6437 ** contains a two-byte identification header and a four-byte checksum at
6438 ** the end.  This is different from ZIP which uses the raw deflate format.
6439 **
6440 ** Future enhancements to SQLar might add support for new compression formats.
6441 ** If so, those new formats will be identified by alternative headers in the
6442 ** compressed data.
6443 */
6444 static void sqlarCompressFunc(
6445   sqlite3_context *context,
6446   int argc,
6447   sqlite3_value **argv
6448 ){
6449   assert( argc==1 );
6450   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6451     const Bytef *pData = sqlite3_value_blob(argv[0]);
6452     uLong nData = sqlite3_value_bytes(argv[0]);
6453     uLongf nOut = compressBound(nData);
6454     Bytef *pOut;
6455 
6456     pOut = (Bytef*)sqlite3_malloc(nOut);
6457     if( pOut==0 ){
6458       sqlite3_result_error_nomem(context);
6459       return;
6460     }else{
6461       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6462         sqlite3_result_error(context, "error in compress()", -1);
6463       }else if( nOut<nData ){
6464         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6465       }else{
6466         sqlite3_result_value(context, argv[0]);
6467       }
6468       sqlite3_free(pOut);
6469     }
6470   }else{
6471     sqlite3_result_value(context, argv[0]);
6472   }
6473 }
6474 
6475 /*
6476 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6477 **
6478 ** Parameter SZ is interpreted as an integer. If it is less than or
6479 ** equal to zero, then this function returns a copy of X. Or, if
6480 ** SZ is equal to the size of X when interpreted as a blob, also
6481 ** return a copy of X. Otherwise, decompress blob X using zlib
6482 ** utility function uncompress() and return the results (another
6483 ** blob).
6484 */
6485 static void sqlarUncompressFunc(
6486   sqlite3_context *context,
6487   int argc,
6488   sqlite3_value **argv
6489 ){
6490   uLong nData;
6491   uLongf sz;
6492 
6493   assert( argc==2 );
6494   sz = sqlite3_value_int(argv[1]);
6495 
6496   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6497     sqlite3_result_value(context, argv[0]);
6498   }else{
6499     const Bytef *pData= sqlite3_value_blob(argv[0]);
6500     Bytef *pOut = sqlite3_malloc(sz);
6501     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6502       sqlite3_result_error(context, "error in uncompress()", -1);
6503     }else{
6504       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6505     }
6506     sqlite3_free(pOut);
6507   }
6508 }
6509 
6510 
6511 #ifdef _WIN32
6512 
6513 #endif
6514 int sqlite3_sqlar_init(
6515   sqlite3 *db,
6516   char **pzErrMsg,
6517   const sqlite3_api_routines *pApi
6518 ){
6519   int rc = SQLITE_OK;
6520   SQLITE_EXTENSION_INIT2(pApi);
6521   (void)pzErrMsg;  /* Unused parameter */
6522   rc = sqlite3_create_function(db, "sqlar_compress", 1,
6523                                SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
6524                                sqlarCompressFunc, 0, 0);
6525   if( rc==SQLITE_OK ){
6526     rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
6527                                  SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
6528                                  sqlarUncompressFunc, 0, 0);
6529   }
6530   return rc;
6531 }
6532 
6533 /************************* End ../ext/misc/sqlar.c ********************/
6534 #endif
6535 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6536 /*
6537 ** 2017 April 07
6538 **
6539 ** The author disclaims copyright to this source code.  In place of
6540 ** a legal notice, here is a blessing:
6541 **
6542 **    May you do good and not evil.
6543 **    May you find forgiveness for yourself and forgive others.
6544 **    May you share freely, never taking more than you give.
6545 **
6546 *************************************************************************
6547 */
6548 #if !defined(SQLITEEXPERT_H)
6549 #define SQLITEEXPERT_H 1
6550 /* #include "sqlite3.h" */
6551 
6552 typedef struct sqlite3expert sqlite3expert;
6553 
6554 /*
6555 ** Create a new sqlite3expert object.
6556 **
6557 ** If successful, a pointer to the new object is returned and (*pzErr) set
6558 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6559 ** an English-language error message. In this case it is the responsibility
6560 ** of the caller to eventually free the error message buffer using
6561 ** sqlite3_free().
6562 */
6563 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6564 
6565 /*
6566 ** Configure an sqlite3expert object.
6567 **
6568 ** EXPERT_CONFIG_SAMPLE:
6569 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6570 **   each candidate index. This involves scanning and sorting the entire
6571 **   contents of each user database table once for each candidate index
6572 **   associated with the table. For large databases, this can be
6573 **   prohibitively slow. This option allows the sqlite3expert object to
6574 **   be configured so that sqlite_stat1 data is instead generated based on a
6575 **   subset of each table, or so that no sqlite_stat1 data is used at all.
6576 **
6577 **   A single integer argument is passed to this option. If the value is less
6578 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
6579 **   the analysis - indexes are recommended based on the database schema only.
6580 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
6581 **   generated for each candidate index (this is the default). Finally, if the
6582 **   value falls between 0 and 100, then it represents the percentage of user
6583 **   table rows that should be considered when generating sqlite_stat1 data.
6584 **
6585 **   Examples:
6586 **
6587 **     // Do not generate any sqlite_stat1 data
6588 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6589 **
6590 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
6591 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6592 */
6593 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6594 
6595 #define EXPERT_CONFIG_SAMPLE 1    /* int */
6596 
6597 /*
6598 ** Specify zero or more SQL statements to be included in the analysis.
6599 **
6600 ** Buffer zSql must contain zero or more complete SQL statements. This
6601 ** function parses all statements contained in the buffer and adds them
6602 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6603 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6604 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6605 ** may be set to point to an English language error message. In this case
6606 ** the caller is responsible for eventually freeing the error message buffer
6607 ** using sqlite3_free().
6608 **
6609 ** If an error does occur while processing one of the statements in the
6610 ** buffer passed as the second argument, none of the statements in the
6611 ** buffer are added to the analysis.
6612 **
6613 ** This function must be called before sqlite3_expert_analyze(). If a call
6614 ** to this function is made on an sqlite3expert object that has already
6615 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6616 ** immediately and no statements are added to the analysis.
6617 */
6618 int sqlite3_expert_sql(
6619   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
6620   const char *zSql,               /* SQL statement(s) to add */
6621   char **pzErr                    /* OUT: Error message (if any) */
6622 );
6623 
6624 
6625 /*
6626 ** This function is called after the sqlite3expert object has been configured
6627 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6628 ** the analysis. Once this function has been called, it is not possible to
6629 ** add further SQL statements to the analysis.
6630 **
6631 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6632 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
6633 ** point to a buffer containing an English language error message. In this
6634 ** case it is the responsibility of the caller to eventually free the buffer
6635 ** using sqlite3_free().
6636 **
6637 ** If an error does occur within this function, the sqlite3expert object
6638 ** is no longer useful for any purpose. At that point it is no longer
6639 ** possible to add further SQL statements to the object or to re-attempt
6640 ** the analysis. The sqlite3expert object must still be freed using a call
6641 ** sqlite3_expert_destroy().
6642 */
6643 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6644 
6645 /*
6646 ** Return the total number of statements loaded using sqlite3_expert_sql().
6647 ** The total number of SQL statements may be different from the total number
6648 ** to calls to sqlite3_expert_sql().
6649 */
6650 int sqlite3_expert_count(sqlite3expert*);
6651 
6652 /*
6653 ** Return a component of the report.
6654 **
6655 ** This function is called after sqlite3_expert_analyze() to extract the
6656 ** results of the analysis. Each call to this function returns either a
6657 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6658 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6659 ** #define constants defined below.
6660 **
6661 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
6662 ** information relating to a specific SQL statement. In these cases that
6663 ** SQL statement is identified by the value passed as the second argument.
6664 ** SQL statements are numbered from 0 in the order in which they are parsed.
6665 ** If an out-of-range value (less than zero or equal to or greater than the
6666 ** value returned by sqlite3_expert_count()) is passed as the second argument
6667 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6668 **
6669 ** EXPERT_REPORT_SQL:
6670 **   Return the text of SQL statement iStmt.
6671 **
6672 ** EXPERT_REPORT_INDEXES:
6673 **   Return a buffer containing the CREATE INDEX statements for all recommended
6674 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL
6675 **   is returned.
6676 **
6677 ** EXPERT_REPORT_PLAN:
6678 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6679 **   iStmt after the proposed indexes have been added to the database schema.
6680 **
6681 ** EXPERT_REPORT_CANDIDATES:
6682 **   Return a pointer to a buffer containing the CREATE INDEX statements
6683 **   for all indexes that were tested (for all SQL statements). The iStmt
6684 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6685 */
6686 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6687 
6688 /*
6689 ** Values for the third argument passed to sqlite3_expert_report().
6690 */
6691 #define EXPERT_REPORT_SQL        1
6692 #define EXPERT_REPORT_INDEXES    2
6693 #define EXPERT_REPORT_PLAN       3
6694 #define EXPERT_REPORT_CANDIDATES 4
6695 
6696 /*
6697 ** Free an (sqlite3expert*) handle and all associated resources. There
6698 ** should be one call to this function for each successful call to
6699 ** sqlite3-expert_new().
6700 */
6701 void sqlite3_expert_destroy(sqlite3expert*);
6702 
6703 #endif  /* !defined(SQLITEEXPERT_H) */
6704 
6705 /************************* End ../ext/expert/sqlite3expert.h ********************/
6706 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6707 /*
6708 ** 2017 April 09
6709 **
6710 ** The author disclaims copyright to this source code.  In place of
6711 ** a legal notice, here is a blessing:
6712 **
6713 **    May you do good and not evil.
6714 **    May you find forgiveness for yourself and forgive others.
6715 **    May you share freely, never taking more than you give.
6716 **
6717 *************************************************************************
6718 */
6719 /* #include "sqlite3expert.h" */
6720 #include <assert.h>
6721 #include <string.h>
6722 #include <stdio.h>
6723 
6724 #ifndef SQLITE_OMIT_VIRTUALTABLE
6725 
6726 /* typedef sqlite3_int64 i64; */
6727 /* typedef sqlite3_uint64 u64; */
6728 
6729 typedef struct IdxColumn IdxColumn;
6730 typedef struct IdxConstraint IdxConstraint;
6731 typedef struct IdxScan IdxScan;
6732 typedef struct IdxStatement IdxStatement;
6733 typedef struct IdxTable IdxTable;
6734 typedef struct IdxWrite IdxWrite;
6735 
6736 #define STRLEN  (int)strlen
6737 
6738 /*
6739 ** A temp table name that we assume no user database will actually use.
6740 ** If this assumption proves incorrect triggers on the table with the
6741 ** conflicting name will be ignored.
6742 */
6743 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6744 
6745 /*
6746 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6747 ** any other type of single-ended range constraint on a column).
6748 **
6749 ** pLink:
6750 **   Used to temporarily link IdxConstraint objects into lists while
6751 **   creating candidate indexes.
6752 */
6753 struct IdxConstraint {
6754   char *zColl;                    /* Collation sequence */
6755   int bRange;                     /* True for range, false for eq */
6756   int iCol;                       /* Constrained table column */
6757   int bFlag;                      /* Used by idxFindCompatible() */
6758   int bDesc;                      /* True if ORDER BY <expr> DESC */
6759   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
6760   IdxConstraint *pLink;           /* See above */
6761 };
6762 
6763 /*
6764 ** A single scan of a single table.
6765 */
6766 struct IdxScan {
6767   IdxTable *pTab;                 /* Associated table object */
6768   int iDb;                        /* Database containing table zTable */
6769   i64 covering;                   /* Mask of columns required for cov. index */
6770   IdxConstraint *pOrder;          /* ORDER BY columns */
6771   IdxConstraint *pEq;             /* List of == constraints */
6772   IdxConstraint *pRange;          /* List of < constraints */
6773   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
6774 };
6775 
6776 /*
6777 ** Information regarding a single database table. Extracted from
6778 ** "PRAGMA table_info" by function idxGetTableInfo().
6779 */
6780 struct IdxColumn {
6781   char *zName;
6782   char *zColl;
6783   int iPk;
6784 };
6785 struct IdxTable {
6786   int nCol;
6787   char *zName;                    /* Table name */
6788   IdxColumn *aCol;
6789   IdxTable *pNext;                /* Next table in linked list of all tables */
6790 };
6791 
6792 /*
6793 ** An object of the following type is created for each unique table/write-op
6794 ** seen. The objects are stored in a singly-linked list beginning at
6795 ** sqlite3expert.pWrite.
6796 */
6797 struct IdxWrite {
6798   IdxTable *pTab;
6799   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
6800   IdxWrite *pNext;
6801 };
6802 
6803 /*
6804 ** Each statement being analyzed is represented by an instance of this
6805 ** structure.
6806 */
6807 struct IdxStatement {
6808   int iId;                        /* Statement number */
6809   char *zSql;                     /* SQL statement */
6810   char *zIdx;                     /* Indexes */
6811   char *zEQP;                     /* Plan */
6812   IdxStatement *pNext;
6813 };
6814 
6815 
6816 /*
6817 ** A hash table for storing strings. With space for a payload string
6818 ** with each entry. Methods are:
6819 **
6820 **   idxHashInit()
6821 **   idxHashClear()
6822 **   idxHashAdd()
6823 **   idxHashSearch()
6824 */
6825 #define IDX_HASH_SIZE 1023
6826 typedef struct IdxHashEntry IdxHashEntry;
6827 typedef struct IdxHash IdxHash;
6828 struct IdxHashEntry {
6829   char *zKey;                     /* nul-terminated key */
6830   char *zVal;                     /* nul-terminated value string */
6831   char *zVal2;                    /* nul-terminated value string 2 */
6832   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
6833   IdxHashEntry *pNext;            /* Next entry in hash */
6834 };
6835 struct IdxHash {
6836   IdxHashEntry *pFirst;
6837   IdxHashEntry *aHash[IDX_HASH_SIZE];
6838 };
6839 
6840 /*
6841 ** sqlite3expert object.
6842 */
6843 struct sqlite3expert {
6844   int iSample;                    /* Percentage of tables to sample for stat1 */
6845   sqlite3 *db;                    /* User database */
6846   sqlite3 *dbm;                   /* In-memory db for this analysis */
6847   sqlite3 *dbv;                   /* Vtab schema for this analysis */
6848   IdxTable *pTable;               /* List of all IdxTable objects */
6849   IdxScan *pScan;                 /* List of scan objects */
6850   IdxWrite *pWrite;               /* List of write objects */
6851   IdxStatement *pStatement;       /* List of IdxStatement objects */
6852   int bRun;                       /* True once analysis has run */
6853   char **pzErrmsg;
6854   int rc;                         /* Error code from whereinfo hook */
6855   IdxHash hIdx;                   /* Hash containing all candidate indexes */
6856   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
6857 };
6858 
6859 
6860 /*
6861 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
6862 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6863 */
6864 static void *idxMalloc(int *pRc, int nByte){
6865   void *pRet;
6866   assert( *pRc==SQLITE_OK );
6867   assert( nByte>0 );
6868   pRet = sqlite3_malloc(nByte);
6869   if( pRet ){
6870     memset(pRet, 0, nByte);
6871   }else{
6872     *pRc = SQLITE_NOMEM;
6873   }
6874   return pRet;
6875 }
6876 
6877 /*
6878 ** Initialize an IdxHash hash table.
6879 */
6880 static void idxHashInit(IdxHash *pHash){
6881   memset(pHash, 0, sizeof(IdxHash));
6882 }
6883 
6884 /*
6885 ** Reset an IdxHash hash table.
6886 */
6887 static void idxHashClear(IdxHash *pHash){
6888   int i;
6889   for(i=0; i<IDX_HASH_SIZE; i++){
6890     IdxHashEntry *pEntry;
6891     IdxHashEntry *pNext;
6892     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6893       pNext = pEntry->pHashNext;
6894       sqlite3_free(pEntry->zVal2);
6895       sqlite3_free(pEntry);
6896     }
6897   }
6898   memset(pHash, 0, sizeof(IdxHash));
6899 }
6900 
6901 /*
6902 ** Return the index of the hash bucket that the string specified by the
6903 ** arguments to this function belongs.
6904 */
6905 static int idxHashString(const char *z, int n){
6906   unsigned int ret = 0;
6907   int i;
6908   for(i=0; i<n; i++){
6909     ret += (ret<<3) + (unsigned char)(z[i]);
6910   }
6911   return (int)(ret % IDX_HASH_SIZE);
6912 }
6913 
6914 /*
6915 ** If zKey is already present in the hash table, return non-zero and do
6916 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6917 ** the hash table passed as the second argument.
6918 */
6919 static int idxHashAdd(
6920   int *pRc,
6921   IdxHash *pHash,
6922   const char *zKey,
6923   const char *zVal
6924 ){
6925   int nKey = STRLEN(zKey);
6926   int iHash = idxHashString(zKey, nKey);
6927   int nVal = (zVal ? STRLEN(zVal) : 0);
6928   IdxHashEntry *pEntry;
6929   assert( iHash>=0 );
6930   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6931     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6932       return 1;
6933     }
6934   }
6935   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6936   if( pEntry ){
6937     pEntry->zKey = (char*)&pEntry[1];
6938     memcpy(pEntry->zKey, zKey, nKey);
6939     if( zVal ){
6940       pEntry->zVal = &pEntry->zKey[nKey+1];
6941       memcpy(pEntry->zVal, zVal, nVal);
6942     }
6943     pEntry->pHashNext = pHash->aHash[iHash];
6944     pHash->aHash[iHash] = pEntry;
6945 
6946     pEntry->pNext = pHash->pFirst;
6947     pHash->pFirst = pEntry;
6948   }
6949   return 0;
6950 }
6951 
6952 /*
6953 ** If zKey/nKey is present in the hash table, return a pointer to the
6954 ** hash-entry object.
6955 */
6956 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6957   int iHash;
6958   IdxHashEntry *pEntry;
6959   if( nKey<0 ) nKey = STRLEN(zKey);
6960   iHash = idxHashString(zKey, nKey);
6961   assert( iHash>=0 );
6962   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6963     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6964       return pEntry;
6965     }
6966   }
6967   return 0;
6968 }
6969 
6970 /*
6971 ** If the hash table contains an entry with a key equal to the string
6972 ** passed as the final two arguments to this function, return a pointer
6973 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6974 ** hash table, return NULL.
6975 */
6976 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6977   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6978   if( pEntry ) return pEntry->zVal;
6979   return 0;
6980 }
6981 
6982 /*
6983 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6984 ** variable to point to a copy of nul-terminated string zColl.
6985 */
6986 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6987   IdxConstraint *pNew;
6988   int nColl = STRLEN(zColl);
6989 
6990   assert( *pRc==SQLITE_OK );
6991   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6992   if( pNew ){
6993     pNew->zColl = (char*)&pNew[1];
6994     memcpy(pNew->zColl, zColl, nColl+1);
6995   }
6996   return pNew;
6997 }
6998 
6999 /*
7000 ** An error associated with database handle db has just occurred. Pass
7001 ** the error message to callback function xOut.
7002 */
7003 static void idxDatabaseError(
7004   sqlite3 *db,                    /* Database handle */
7005   char **pzErrmsg                 /* Write error here */
7006 ){
7007   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
7008 }
7009 
7010 /*
7011 ** Prepare an SQL statement.
7012 */
7013 static int idxPrepareStmt(
7014   sqlite3 *db,                    /* Database handle to compile against */
7015   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
7016   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
7017   const char *zSql                /* SQL statement to compile */
7018 ){
7019   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
7020   if( rc!=SQLITE_OK ){
7021     *ppStmt = 0;
7022     idxDatabaseError(db, pzErrmsg);
7023   }
7024   return rc;
7025 }
7026 
7027 /*
7028 ** Prepare an SQL statement using the results of a printf() formatting.
7029 */
7030 static int idxPrintfPrepareStmt(
7031   sqlite3 *db,                    /* Database handle to compile against */
7032   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
7033   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
7034   const char *zFmt,               /* printf() format of SQL statement */
7035   ...                             /* Trailing printf() arguments */
7036 ){
7037   va_list ap;
7038   int rc;
7039   char *zSql;
7040   va_start(ap, zFmt);
7041   zSql = sqlite3_vmprintf(zFmt, ap);
7042   if( zSql==0 ){
7043     rc = SQLITE_NOMEM;
7044   }else{
7045     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
7046     sqlite3_free(zSql);
7047   }
7048   va_end(ap);
7049   return rc;
7050 }
7051 
7052 
7053 /*************************************************************************
7054 ** Beginning of virtual table implementation.
7055 */
7056 typedef struct ExpertVtab ExpertVtab;
7057 struct ExpertVtab {
7058   sqlite3_vtab base;
7059   IdxTable *pTab;
7060   sqlite3expert *pExpert;
7061 };
7062 
7063 typedef struct ExpertCsr ExpertCsr;
7064 struct ExpertCsr {
7065   sqlite3_vtab_cursor base;
7066   sqlite3_stmt *pData;
7067 };
7068 
7069 static char *expertDequote(const char *zIn){
7070   int n = STRLEN(zIn);
7071   char *zRet = sqlite3_malloc(n);
7072 
7073   assert( zIn[0]=='\'' );
7074   assert( zIn[n-1]=='\'' );
7075 
7076   if( zRet ){
7077     int iOut = 0;
7078     int iIn = 0;
7079     for(iIn=1; iIn<(n-1); iIn++){
7080       if( zIn[iIn]=='\'' ){
7081         assert( zIn[iIn+1]=='\'' );
7082         iIn++;
7083       }
7084       zRet[iOut++] = zIn[iIn];
7085     }
7086     zRet[iOut] = '\0';
7087   }
7088 
7089   return zRet;
7090 }
7091 
7092 /*
7093 ** This function is the implementation of both the xConnect and xCreate
7094 ** methods of the r-tree virtual table.
7095 **
7096 **   argv[0]   -> module name
7097 **   argv[1]   -> database name
7098 **   argv[2]   -> table name
7099 **   argv[...] -> column names...
7100 */
7101 static int expertConnect(
7102   sqlite3 *db,
7103   void *pAux,
7104   int argc, const char *const*argv,
7105   sqlite3_vtab **ppVtab,
7106   char **pzErr
7107 ){
7108   sqlite3expert *pExpert = (sqlite3expert*)pAux;
7109   ExpertVtab *p = 0;
7110   int rc;
7111 
7112   if( argc!=4 ){
7113     *pzErr = sqlite3_mprintf("internal error!");
7114     rc = SQLITE_ERROR;
7115   }else{
7116     char *zCreateTable = expertDequote(argv[3]);
7117     if( zCreateTable ){
7118       rc = sqlite3_declare_vtab(db, zCreateTable);
7119       if( rc==SQLITE_OK ){
7120         p = idxMalloc(&rc, sizeof(ExpertVtab));
7121       }
7122       if( rc==SQLITE_OK ){
7123         p->pExpert = pExpert;
7124         p->pTab = pExpert->pTable;
7125         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
7126       }
7127       sqlite3_free(zCreateTable);
7128     }else{
7129       rc = SQLITE_NOMEM;
7130     }
7131   }
7132 
7133   *ppVtab = (sqlite3_vtab*)p;
7134   return rc;
7135 }
7136 
7137 static int expertDisconnect(sqlite3_vtab *pVtab){
7138   ExpertVtab *p = (ExpertVtab*)pVtab;
7139   sqlite3_free(p);
7140   return SQLITE_OK;
7141 }
7142 
7143 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
7144   ExpertVtab *p = (ExpertVtab*)pVtab;
7145   int rc = SQLITE_OK;
7146   int n = 0;
7147   IdxScan *pScan;
7148   const int opmask =
7149     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
7150     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
7151     SQLITE_INDEX_CONSTRAINT_LE;
7152 
7153   pScan = idxMalloc(&rc, sizeof(IdxScan));
7154   if( pScan ){
7155     int i;
7156 
7157     /* Link the new scan object into the list */
7158     pScan->pTab = p->pTab;
7159     pScan->pNextScan = p->pExpert->pScan;
7160     p->pExpert->pScan = pScan;
7161 
7162     /* Add the constraints to the IdxScan object */
7163     for(i=0; i<pIdxInfo->nConstraint; i++){
7164       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7165       if( pCons->usable
7166        && pCons->iColumn>=0
7167        && p->pTab->aCol[pCons->iColumn].iPk==0
7168        && (pCons->op & opmask)
7169       ){
7170         IdxConstraint *pNew;
7171         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
7172         pNew = idxNewConstraint(&rc, zColl);
7173         if( pNew ){
7174           pNew->iCol = pCons->iColumn;
7175           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7176             pNew->pNext = pScan->pEq;
7177             pScan->pEq = pNew;
7178           }else{
7179             pNew->bRange = 1;
7180             pNew->pNext = pScan->pRange;
7181             pScan->pRange = pNew;
7182           }
7183         }
7184         n++;
7185         pIdxInfo->aConstraintUsage[i].argvIndex = n;
7186       }
7187     }
7188 
7189     /* Add the ORDER BY to the IdxScan object */
7190     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
7191       int iCol = pIdxInfo->aOrderBy[i].iColumn;
7192       if( iCol>=0 ){
7193         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
7194         if( pNew ){
7195           pNew->iCol = iCol;
7196           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
7197           pNew->pNext = pScan->pOrder;
7198           pNew->pLink = pScan->pOrder;
7199           pScan->pOrder = pNew;
7200           n++;
7201         }
7202       }
7203     }
7204   }
7205 
7206   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7207   return rc;
7208 }
7209 
7210 static int expertUpdate(
7211   sqlite3_vtab *pVtab,
7212   int nData,
7213   sqlite3_value **azData,
7214   sqlite_int64 *pRowid
7215 ){
7216   (void)pVtab;
7217   (void)nData;
7218   (void)azData;
7219   (void)pRowid;
7220   return SQLITE_OK;
7221 }
7222 
7223 /*
7224 ** Virtual table module xOpen method.
7225 */
7226 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7227   int rc = SQLITE_OK;
7228   ExpertCsr *pCsr;
7229   (void)pVTab;
7230   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7231   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7232   return rc;
7233 }
7234 
7235 /*
7236 ** Virtual table module xClose method.
7237 */
7238 static int expertClose(sqlite3_vtab_cursor *cur){
7239   ExpertCsr *pCsr = (ExpertCsr*)cur;
7240   sqlite3_finalize(pCsr->pData);
7241   sqlite3_free(pCsr);
7242   return SQLITE_OK;
7243 }
7244 
7245 /*
7246 ** Virtual table module xEof method.
7247 **
7248 ** Return non-zero if the cursor does not currently point to a valid
7249 ** record (i.e if the scan has finished), or zero otherwise.
7250 */
7251 static int expertEof(sqlite3_vtab_cursor *cur){
7252   ExpertCsr *pCsr = (ExpertCsr*)cur;
7253   return pCsr->pData==0;
7254 }
7255 
7256 /*
7257 ** Virtual table module xNext method.
7258 */
7259 static int expertNext(sqlite3_vtab_cursor *cur){
7260   ExpertCsr *pCsr = (ExpertCsr*)cur;
7261   int rc = SQLITE_OK;
7262 
7263   assert( pCsr->pData );
7264   rc = sqlite3_step(pCsr->pData);
7265   if( rc!=SQLITE_ROW ){
7266     rc = sqlite3_finalize(pCsr->pData);
7267     pCsr->pData = 0;
7268   }else{
7269     rc = SQLITE_OK;
7270   }
7271 
7272   return rc;
7273 }
7274 
7275 /*
7276 ** Virtual table module xRowid method.
7277 */
7278 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7279   (void)cur;
7280   *pRowid = 0;
7281   return SQLITE_OK;
7282 }
7283 
7284 /*
7285 ** Virtual table module xColumn method.
7286 */
7287 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7288   ExpertCsr *pCsr = (ExpertCsr*)cur;
7289   sqlite3_value *pVal;
7290   pVal = sqlite3_column_value(pCsr->pData, i);
7291   if( pVal ){
7292     sqlite3_result_value(ctx, pVal);
7293   }
7294   return SQLITE_OK;
7295 }
7296 
7297 /*
7298 ** Virtual table module xFilter method.
7299 */
7300 static int expertFilter(
7301   sqlite3_vtab_cursor *cur,
7302   int idxNum, const char *idxStr,
7303   int argc, sqlite3_value **argv
7304 ){
7305   ExpertCsr *pCsr = (ExpertCsr*)cur;
7306   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7307   sqlite3expert *pExpert = pVtab->pExpert;
7308   int rc;
7309 
7310   (void)idxNum;
7311   (void)idxStr;
7312   (void)argc;
7313   (void)argv;
7314   rc = sqlite3_finalize(pCsr->pData);
7315   pCsr->pData = 0;
7316   if( rc==SQLITE_OK ){
7317     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7318         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7319     );
7320   }
7321 
7322   if( rc==SQLITE_OK ){
7323     rc = expertNext(cur);
7324   }
7325   return rc;
7326 }
7327 
7328 static int idxRegisterVtab(sqlite3expert *p){
7329   static sqlite3_module expertModule = {
7330     2,                            /* iVersion */
7331     expertConnect,                /* xCreate - create a table */
7332     expertConnect,                /* xConnect - connect to an existing table */
7333     expertBestIndex,              /* xBestIndex - Determine search strategy */
7334     expertDisconnect,             /* xDisconnect - Disconnect from a table */
7335     expertDisconnect,             /* xDestroy - Drop a table */
7336     expertOpen,                   /* xOpen - open a cursor */
7337     expertClose,                  /* xClose - close a cursor */
7338     expertFilter,                 /* xFilter - configure scan constraints */
7339     expertNext,                   /* xNext - advance a cursor */
7340     expertEof,                    /* xEof */
7341     expertColumn,                 /* xColumn - read data */
7342     expertRowid,                  /* xRowid - read data */
7343     expertUpdate,                 /* xUpdate - write data */
7344     0,                            /* xBegin - begin transaction */
7345     0,                            /* xSync - sync transaction */
7346     0,                            /* xCommit - commit transaction */
7347     0,                            /* xRollback - rollback transaction */
7348     0,                            /* xFindFunction - function overloading */
7349     0,                            /* xRename - rename the table */
7350     0,                            /* xSavepoint */
7351     0,                            /* xRelease */
7352     0,                            /* xRollbackTo */
7353     0,                            /* xShadowName */
7354   };
7355 
7356   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7357 }
7358 /*
7359 ** End of virtual table implementation.
7360 *************************************************************************/
7361 /*
7362 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7363 ** is called, set it to the return value of sqlite3_finalize() before
7364 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7365 */
7366 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7367   int rc = sqlite3_finalize(pStmt);
7368   if( *pRc==SQLITE_OK ) *pRc = rc;
7369 }
7370 
7371 /*
7372 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7373 ** in the main database of connection db. If successful, set (*ppOut) to
7374 ** point to the new object and return SQLITE_OK. Otherwise, return an
7375 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7376 ** set to point to an error string.
7377 **
7378 ** It is the responsibility of the caller to eventually free either the
7379 ** IdxTable object or error message using sqlite3_free().
7380 */
7381 static int idxGetTableInfo(
7382   sqlite3 *db,                    /* Database connection to read details from */
7383   const char *zTab,               /* Table name */
7384   IdxTable **ppOut,               /* OUT: New object (if successful) */
7385   char **pzErrmsg                 /* OUT: Error message (if not) */
7386 ){
7387   sqlite3_stmt *p1 = 0;
7388   int nCol = 0;
7389   int nTab = STRLEN(zTab);
7390   int nByte = sizeof(IdxTable) + nTab + 1;
7391   IdxTable *pNew = 0;
7392   int rc, rc2;
7393   char *pCsr = 0;
7394 
7395   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7396   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7397     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7398     nByte += 1 + STRLEN(zCol);
7399     rc = sqlite3_table_column_metadata(
7400         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7401     );
7402     nByte += 1 + STRLEN(zCol);
7403     nCol++;
7404   }
7405   rc2 = sqlite3_reset(p1);
7406   if( rc==SQLITE_OK ) rc = rc2;
7407 
7408   nByte += sizeof(IdxColumn) * nCol;
7409   if( rc==SQLITE_OK ){
7410     pNew = idxMalloc(&rc, nByte);
7411   }
7412   if( rc==SQLITE_OK ){
7413     pNew->aCol = (IdxColumn*)&pNew[1];
7414     pNew->nCol = nCol;
7415     pCsr = (char*)&pNew->aCol[nCol];
7416   }
7417 
7418   nCol = 0;
7419   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7420     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7421     int nCopy = STRLEN(zCol) + 1;
7422     pNew->aCol[nCol].zName = pCsr;
7423     pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7424     memcpy(pCsr, zCol, nCopy);
7425     pCsr += nCopy;
7426 
7427     rc = sqlite3_table_column_metadata(
7428         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7429     );
7430     if( rc==SQLITE_OK ){
7431       nCopy = STRLEN(zCol) + 1;
7432       pNew->aCol[nCol].zColl = pCsr;
7433       memcpy(pCsr, zCol, nCopy);
7434       pCsr += nCopy;
7435     }
7436 
7437     nCol++;
7438   }
7439   idxFinalize(&rc, p1);
7440 
7441   if( rc!=SQLITE_OK ){
7442     sqlite3_free(pNew);
7443     pNew = 0;
7444   }else{
7445     pNew->zName = pCsr;
7446     memcpy(pNew->zName, zTab, nTab+1);
7447   }
7448 
7449   *ppOut = pNew;
7450   return rc;
7451 }
7452 
7453 /*
7454 ** This function is a no-op if *pRc is set to anything other than
7455 ** SQLITE_OK when it is called.
7456 **
7457 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7458 ** the printf() style arguments is appended to zIn and the result returned
7459 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7460 ** zIn before returning.
7461 */
7462 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7463   va_list ap;
7464   char *zAppend = 0;
7465   char *zRet = 0;
7466   int nIn = zIn ? STRLEN(zIn) : 0;
7467   int nAppend = 0;
7468   va_start(ap, zFmt);
7469   if( *pRc==SQLITE_OK ){
7470     zAppend = sqlite3_vmprintf(zFmt, ap);
7471     if( zAppend ){
7472       nAppend = STRLEN(zAppend);
7473       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7474     }
7475     if( zAppend && zRet ){
7476       if( nIn ) memcpy(zRet, zIn, nIn);
7477       memcpy(&zRet[nIn], zAppend, nAppend+1);
7478     }else{
7479       sqlite3_free(zRet);
7480       zRet = 0;
7481       *pRc = SQLITE_NOMEM;
7482     }
7483     sqlite3_free(zAppend);
7484     sqlite3_free(zIn);
7485   }
7486   va_end(ap);
7487   return zRet;
7488 }
7489 
7490 /*
7491 ** Return true if zId must be quoted in order to use it as an SQL
7492 ** identifier, or false otherwise.
7493 */
7494 static int idxIdentifierRequiresQuotes(const char *zId){
7495   int i;
7496   for(i=0; zId[i]; i++){
7497     if( !(zId[i]=='_')
7498      && !(zId[i]>='0' && zId[i]<='9')
7499      && !(zId[i]>='a' && zId[i]<='z')
7500      && !(zId[i]>='A' && zId[i]<='Z')
7501     ){
7502       return 1;
7503     }
7504   }
7505   return 0;
7506 }
7507 
7508 /*
7509 ** This function appends an index column definition suitable for constraint
7510 ** pCons to the string passed as zIn and returns the result.
7511 */
7512 static char *idxAppendColDefn(
7513   int *pRc,                       /* IN/OUT: Error code */
7514   char *zIn,                      /* Column defn accumulated so far */
7515   IdxTable *pTab,                 /* Table index will be created on */
7516   IdxConstraint *pCons
7517 ){
7518   char *zRet = zIn;
7519   IdxColumn *p = &pTab->aCol[pCons->iCol];
7520   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7521 
7522   if( idxIdentifierRequiresQuotes(p->zName) ){
7523     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7524   }else{
7525     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7526   }
7527 
7528   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7529     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7530       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7531     }else{
7532       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7533     }
7534   }
7535 
7536   if( pCons->bDesc ){
7537     zRet = idxAppendText(pRc, zRet, " DESC");
7538   }
7539   return zRet;
7540 }
7541 
7542 /*
7543 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7544 ** would create from arguments pScan, pEq and pTail. If no error occurs and
7545 ** such an index is found, return non-zero. Or, if no such index is found,
7546 ** return zero.
7547 **
7548 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7549 */
7550 static int idxFindCompatible(
7551   int *pRc,                       /* OUT: Error code */
7552   sqlite3* dbm,                   /* Database to search */
7553   IdxScan *pScan,                 /* Scan for table to search for index on */
7554   IdxConstraint *pEq,             /* List of == constraints */
7555   IdxConstraint *pTail            /* List of range constraints */
7556 ){
7557   const char *zTbl = pScan->pTab->zName;
7558   sqlite3_stmt *pIdxList = 0;
7559   IdxConstraint *pIter;
7560   int nEq = 0;                    /* Number of elements in pEq */
7561   int rc;
7562 
7563   /* Count the elements in list pEq */
7564   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7565 
7566   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7567   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7568     int bMatch = 1;
7569     IdxConstraint *pT = pTail;
7570     sqlite3_stmt *pInfo = 0;
7571     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7572 
7573     /* Zero the IdxConstraint.bFlag values in the pEq list */
7574     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7575 
7576     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7577     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7578       int iIdx = sqlite3_column_int(pInfo, 0);
7579       int iCol = sqlite3_column_int(pInfo, 1);
7580       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7581 
7582       if( iIdx<nEq ){
7583         for(pIter=pEq; pIter; pIter=pIter->pLink){
7584           if( pIter->bFlag ) continue;
7585           if( pIter->iCol!=iCol ) continue;
7586           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7587           pIter->bFlag = 1;
7588           break;
7589         }
7590         if( pIter==0 ){
7591           bMatch = 0;
7592           break;
7593         }
7594       }else{
7595         if( pT ){
7596           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7597             bMatch = 0;
7598             break;
7599           }
7600           pT = pT->pLink;
7601         }
7602       }
7603     }
7604     idxFinalize(&rc, pInfo);
7605 
7606     if( rc==SQLITE_OK && bMatch ){
7607       sqlite3_finalize(pIdxList);
7608       return 1;
7609     }
7610   }
7611   idxFinalize(&rc, pIdxList);
7612 
7613   *pRc = rc;
7614   return 0;
7615 }
7616 
7617 static int idxCreateFromCons(
7618   sqlite3expert *p,
7619   IdxScan *pScan,
7620   IdxConstraint *pEq,
7621   IdxConstraint *pTail
7622 ){
7623   sqlite3 *dbm = p->dbm;
7624   int rc = SQLITE_OK;
7625   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7626     IdxTable *pTab = pScan->pTab;
7627     char *zCols = 0;
7628     char *zIdx = 0;
7629     IdxConstraint *pCons;
7630     unsigned int h = 0;
7631     const char *zFmt;
7632 
7633     for(pCons=pEq; pCons; pCons=pCons->pLink){
7634       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7635     }
7636     for(pCons=pTail; pCons; pCons=pCons->pLink){
7637       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7638     }
7639 
7640     if( rc==SQLITE_OK ){
7641       /* Hash the list of columns to come up with a name for the index */
7642       const char *zTable = pScan->pTab->zName;
7643       char *zName;                /* Index name */
7644       int i;
7645       for(i=0; zCols[i]; i++){
7646         h += ((h<<3) + zCols[i]);
7647       }
7648       zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7649       if( zName==0 ){
7650         rc = SQLITE_NOMEM;
7651       }else{
7652         if( idxIdentifierRequiresQuotes(zTable) ){
7653           zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7654         }else{
7655           zFmt = "CREATE INDEX %s ON %s(%s)";
7656         }
7657         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7658         if( !zIdx ){
7659           rc = SQLITE_NOMEM;
7660         }else{
7661           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7662           idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7663         }
7664         sqlite3_free(zName);
7665         sqlite3_free(zIdx);
7666       }
7667     }
7668 
7669     sqlite3_free(zCols);
7670   }
7671   return rc;
7672 }
7673 
7674 /*
7675 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7676 ** a constraint compatible with *p. Otherwise return false.
7677 */
7678 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7679   IdxConstraint *pCmp;
7680   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7681     if( p->iCol==pCmp->iCol ) return 1;
7682   }
7683   return 0;
7684 }
7685 
7686 static int idxCreateFromWhere(
7687   sqlite3expert *p,
7688   IdxScan *pScan,                 /* Create indexes for this scan */
7689   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
7690 ){
7691   IdxConstraint *p1 = 0;
7692   IdxConstraint *pCon;
7693   int rc;
7694 
7695   /* Gather up all the == constraints. */
7696   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7697     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7698       pCon->pLink = p1;
7699       p1 = pCon;
7700     }
7701   }
7702 
7703   /* Create an index using the == constraints collected above. And the
7704   ** range constraint/ORDER BY terms passed in by the caller, if any. */
7705   rc = idxCreateFromCons(p, pScan, p1, pTail);
7706 
7707   /* If no range/ORDER BY passed by the caller, create a version of the
7708   ** index for each range constraint.  */
7709   if( pTail==0 ){
7710     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7711       assert( pCon->pLink==0 );
7712       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7713         rc = idxCreateFromCons(p, pScan, p1, pCon);
7714       }
7715     }
7716   }
7717 
7718   return rc;
7719 }
7720 
7721 /*
7722 ** Create candidate indexes in database [dbm] based on the data in
7723 ** linked-list pScan.
7724 */
7725 static int idxCreateCandidates(sqlite3expert *p){
7726   int rc = SQLITE_OK;
7727   IdxScan *pIter;
7728 
7729   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7730     rc = idxCreateFromWhere(p, pIter, 0);
7731     if( rc==SQLITE_OK && pIter->pOrder ){
7732       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7733     }
7734   }
7735 
7736   return rc;
7737 }
7738 
7739 /*
7740 ** Free all elements of the linked list starting at pConstraint.
7741 */
7742 static void idxConstraintFree(IdxConstraint *pConstraint){
7743   IdxConstraint *pNext;
7744   IdxConstraint *p;
7745 
7746   for(p=pConstraint; p; p=pNext){
7747     pNext = p->pNext;
7748     sqlite3_free(p);
7749   }
7750 }
7751 
7752 /*
7753 ** Free all elements of the linked list starting from pScan up until pLast
7754 ** (pLast is not freed).
7755 */
7756 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7757   IdxScan *p;
7758   IdxScan *pNext;
7759   for(p=pScan; p!=pLast; p=pNext){
7760     pNext = p->pNextScan;
7761     idxConstraintFree(p->pOrder);
7762     idxConstraintFree(p->pEq);
7763     idxConstraintFree(p->pRange);
7764     sqlite3_free(p);
7765   }
7766 }
7767 
7768 /*
7769 ** Free all elements of the linked list starting from pStatement up
7770 ** until pLast (pLast is not freed).
7771 */
7772 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7773   IdxStatement *p;
7774   IdxStatement *pNext;
7775   for(p=pStatement; p!=pLast; p=pNext){
7776     pNext = p->pNext;
7777     sqlite3_free(p->zEQP);
7778     sqlite3_free(p->zIdx);
7779     sqlite3_free(p);
7780   }
7781 }
7782 
7783 /*
7784 ** Free the linked list of IdxTable objects starting at pTab.
7785 */
7786 static void idxTableFree(IdxTable *pTab){
7787   IdxTable *pIter;
7788   IdxTable *pNext;
7789   for(pIter=pTab; pIter; pIter=pNext){
7790     pNext = pIter->pNext;
7791     sqlite3_free(pIter);
7792   }
7793 }
7794 
7795 /*
7796 ** Free the linked list of IdxWrite objects starting at pTab.
7797 */
7798 static void idxWriteFree(IdxWrite *pTab){
7799   IdxWrite *pIter;
7800   IdxWrite *pNext;
7801   for(pIter=pTab; pIter; pIter=pNext){
7802     pNext = pIter->pNext;
7803     sqlite3_free(pIter);
7804   }
7805 }
7806 
7807 
7808 
7809 /*
7810 ** This function is called after candidate indexes have been created. It
7811 ** runs all the queries to see which indexes they prefer, and populates
7812 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7813 */
7814 int idxFindIndexes(
7815   sqlite3expert *p,
7816   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
7817 ){
7818   IdxStatement *pStmt;
7819   sqlite3 *dbm = p->dbm;
7820   int rc = SQLITE_OK;
7821 
7822   IdxHash hIdx;
7823   idxHashInit(&hIdx);
7824 
7825   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7826     IdxHashEntry *pEntry;
7827     sqlite3_stmt *pExplain = 0;
7828     idxHashClear(&hIdx);
7829     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7830         "EXPLAIN QUERY PLAN %s", pStmt->zSql
7831     );
7832     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7833       /* int iId = sqlite3_column_int(pExplain, 0); */
7834       /* int iParent = sqlite3_column_int(pExplain, 1); */
7835       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7836       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7837       int nDetail = STRLEN(zDetail);
7838       int i;
7839 
7840       for(i=0; i<nDetail; i++){
7841         const char *zIdx = 0;
7842         if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7843           zIdx = &zDetail[i+13];
7844         }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7845           zIdx = &zDetail[i+22];
7846         }
7847         if( zIdx ){
7848           const char *zSql;
7849           int nIdx = 0;
7850           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7851             nIdx++;
7852           }
7853           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7854           if( zSql ){
7855             idxHashAdd(&rc, &hIdx, zSql, 0);
7856             if( rc ) goto find_indexes_out;
7857           }
7858           break;
7859         }
7860       }
7861 
7862       if( zDetail[0]!='-' ){
7863         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7864       }
7865     }
7866 
7867     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7868       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7869     }
7870 
7871     idxFinalize(&rc, pExplain);
7872   }
7873 
7874  find_indexes_out:
7875   idxHashClear(&hIdx);
7876   return rc;
7877 }
7878 
7879 static int idxAuthCallback(
7880   void *pCtx,
7881   int eOp,
7882   const char *z3,
7883   const char *z4,
7884   const char *zDb,
7885   const char *zTrigger
7886 ){
7887   int rc = SQLITE_OK;
7888   (void)z4;
7889   (void)zTrigger;
7890   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7891     if( sqlite3_stricmp(zDb, "main")==0 ){
7892       sqlite3expert *p = (sqlite3expert*)pCtx;
7893       IdxTable *pTab;
7894       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7895         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7896       }
7897       if( pTab ){
7898         IdxWrite *pWrite;
7899         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7900           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7901         }
7902         if( pWrite==0 ){
7903           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7904           if( rc==SQLITE_OK ){
7905             pWrite->pTab = pTab;
7906             pWrite->eOp = eOp;
7907             pWrite->pNext = p->pWrite;
7908             p->pWrite = pWrite;
7909           }
7910         }
7911       }
7912     }
7913   }
7914   return rc;
7915 }
7916 
7917 static int idxProcessOneTrigger(
7918   sqlite3expert *p,
7919   IdxWrite *pWrite,
7920   char **pzErr
7921 ){
7922   static const char *zInt = UNIQUE_TABLE_NAME;
7923   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7924   IdxTable *pTab = pWrite->pTab;
7925   const char *zTab = pTab->zName;
7926   const char *zSql =
7927     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7928     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7929     "ORDER BY type;";
7930   sqlite3_stmt *pSelect = 0;
7931   int rc = SQLITE_OK;
7932   char *zWrite = 0;
7933 
7934   /* Create the table and its triggers in the temp schema */
7935   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7936   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7937     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7938     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7939   }
7940   idxFinalize(&rc, pSelect);
7941 
7942   /* Rename the table in the temp schema to zInt */
7943   if( rc==SQLITE_OK ){
7944     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7945     if( z==0 ){
7946       rc = SQLITE_NOMEM;
7947     }else{
7948       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7949       sqlite3_free(z);
7950     }
7951   }
7952 
7953   switch( pWrite->eOp ){
7954     case SQLITE_INSERT: {
7955       int i;
7956       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7957       for(i=0; i<pTab->nCol; i++){
7958         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7959       }
7960       zWrite = idxAppendText(&rc, zWrite, ")");
7961       break;
7962     }
7963     case SQLITE_UPDATE: {
7964       int i;
7965       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7966       for(i=0; i<pTab->nCol; i++){
7967         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
7968             pTab->aCol[i].zName
7969         );
7970       }
7971       break;
7972     }
7973     default: {
7974       assert( pWrite->eOp==SQLITE_DELETE );
7975       if( rc==SQLITE_OK ){
7976         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7977         if( zWrite==0 ) rc = SQLITE_NOMEM;
7978       }
7979     }
7980   }
7981 
7982   if( rc==SQLITE_OK ){
7983     sqlite3_stmt *pX = 0;
7984     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7985     idxFinalize(&rc, pX);
7986     if( rc!=SQLITE_OK ){
7987       idxDatabaseError(p->dbv, pzErr);
7988     }
7989   }
7990   sqlite3_free(zWrite);
7991 
7992   if( rc==SQLITE_OK ){
7993     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7994   }
7995 
7996   return rc;
7997 }
7998 
7999 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
8000   int rc = SQLITE_OK;
8001   IdxWrite *pEnd = 0;
8002   IdxWrite *pFirst = p->pWrite;
8003 
8004   while( rc==SQLITE_OK && pFirst!=pEnd ){
8005     IdxWrite *pIter;
8006     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
8007       rc = idxProcessOneTrigger(p, pIter, pzErr);
8008     }
8009     pEnd = pFirst;
8010     pFirst = p->pWrite;
8011   }
8012 
8013   return rc;
8014 }
8015 
8016 
8017 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
8018   int rc = idxRegisterVtab(p);
8019   sqlite3_stmt *pSchema = 0;
8020 
8021   /* For each table in the main db schema:
8022   **
8023   **   1) Add an entry to the p->pTable list, and
8024   **   2) Create the equivalent virtual table in dbv.
8025   */
8026   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
8027       "SELECT type, name, sql, 1 FROM sqlite_master "
8028       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
8029       " UNION ALL "
8030       "SELECT type, name, sql, 2 FROM sqlite_master "
8031       "WHERE type = 'trigger'"
8032       "  AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
8033       "ORDER BY 4, 1"
8034   );
8035   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
8036     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
8037     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
8038     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
8039 
8040     if( zType[0]=='v' || zType[1]=='r' ){
8041       rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
8042     }else{
8043       IdxTable *pTab;
8044       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
8045       if( rc==SQLITE_OK ){
8046         int i;
8047         char *zInner = 0;
8048         char *zOuter = 0;
8049         pTab->pNext = p->pTable;
8050         p->pTable = pTab;
8051 
8052         /* The statement the vtab will pass to sqlite3_declare_vtab() */
8053         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
8054         for(i=0; i<pTab->nCol; i++){
8055           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
8056               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
8057           );
8058         }
8059         zInner = idxAppendText(&rc, zInner, ")");
8060 
8061         /* The CVT statement to create the vtab */
8062         zOuter = idxAppendText(&rc, 0,
8063             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
8064         );
8065         if( rc==SQLITE_OK ){
8066           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
8067         }
8068         sqlite3_free(zInner);
8069         sqlite3_free(zOuter);
8070       }
8071     }
8072   }
8073   idxFinalize(&rc, pSchema);
8074   return rc;
8075 }
8076 
8077 struct IdxSampleCtx {
8078   int iTarget;
8079   double target;                  /* Target nRet/nRow value */
8080   double nRow;                    /* Number of rows seen */
8081   double nRet;                    /* Number of rows returned */
8082 };
8083 
8084 static void idxSampleFunc(
8085   sqlite3_context *pCtx,
8086   int argc,
8087   sqlite3_value **argv
8088 ){
8089   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
8090   int bRet;
8091 
8092   (void)argv;
8093   assert( argc==0 );
8094   if( p->nRow==0.0 ){
8095     bRet = 1;
8096   }else{
8097     bRet = (p->nRet / p->nRow) <= p->target;
8098     if( bRet==0 ){
8099       unsigned short rnd;
8100       sqlite3_randomness(2, (void*)&rnd);
8101       bRet = ((int)rnd % 100) <= p->iTarget;
8102     }
8103   }
8104 
8105   sqlite3_result_int(pCtx, bRet);
8106   p->nRow += 1.0;
8107   p->nRet += (double)bRet;
8108 }
8109 
8110 struct IdxRemCtx {
8111   int nSlot;
8112   struct IdxRemSlot {
8113     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
8114     i64 iVal;                     /* SQLITE_INTEGER value */
8115     double rVal;                  /* SQLITE_FLOAT value */
8116     int nByte;                    /* Bytes of space allocated at z */
8117     int n;                        /* Size of buffer z */
8118     char *z;                      /* SQLITE_TEXT/BLOB value */
8119   } aSlot[1];
8120 };
8121 
8122 /*
8123 ** Implementation of scalar function rem().
8124 */
8125 static void idxRemFunc(
8126   sqlite3_context *pCtx,
8127   int argc,
8128   sqlite3_value **argv
8129 ){
8130   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
8131   struct IdxRemSlot *pSlot;
8132   int iSlot;
8133   assert( argc==2 );
8134 
8135   iSlot = sqlite3_value_int(argv[0]);
8136   assert( iSlot<=p->nSlot );
8137   pSlot = &p->aSlot[iSlot];
8138 
8139   switch( pSlot->eType ){
8140     case SQLITE_NULL:
8141       /* no-op */
8142       break;
8143 
8144     case SQLITE_INTEGER:
8145       sqlite3_result_int64(pCtx, pSlot->iVal);
8146       break;
8147 
8148     case SQLITE_FLOAT:
8149       sqlite3_result_double(pCtx, pSlot->rVal);
8150       break;
8151 
8152     case SQLITE_BLOB:
8153       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8154       break;
8155 
8156     case SQLITE_TEXT:
8157       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8158       break;
8159   }
8160 
8161   pSlot->eType = sqlite3_value_type(argv[1]);
8162   switch( pSlot->eType ){
8163     case SQLITE_NULL:
8164       /* no-op */
8165       break;
8166 
8167     case SQLITE_INTEGER:
8168       pSlot->iVal = sqlite3_value_int64(argv[1]);
8169       break;
8170 
8171     case SQLITE_FLOAT:
8172       pSlot->rVal = sqlite3_value_double(argv[1]);
8173       break;
8174 
8175     case SQLITE_BLOB:
8176     case SQLITE_TEXT: {
8177       int nByte = sqlite3_value_bytes(argv[1]);
8178       if( nByte>pSlot->nByte ){
8179         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
8180         if( zNew==0 ){
8181           sqlite3_result_error_nomem(pCtx);
8182           return;
8183         }
8184         pSlot->nByte = nByte*2;
8185         pSlot->z = zNew;
8186       }
8187       pSlot->n = nByte;
8188       if( pSlot->eType==SQLITE_BLOB ){
8189         memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
8190       }else{
8191         memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
8192       }
8193       break;
8194     }
8195   }
8196 }
8197 
8198 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
8199   int rc = SQLITE_OK;
8200   const char *zMax =
8201     "SELECT max(i.seqno) FROM "
8202     "  sqlite_master AS s, "
8203     "  pragma_index_list(s.name) AS l, "
8204     "  pragma_index_info(l.name) AS i "
8205     "WHERE s.type = 'table'";
8206   sqlite3_stmt *pMax = 0;
8207 
8208   *pnMax = 0;
8209   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
8210   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8211     *pnMax = sqlite3_column_int(pMax, 0) + 1;
8212   }
8213   idxFinalize(&rc, pMax);
8214 
8215   return rc;
8216 }
8217 
8218 static int idxPopulateOneStat1(
8219   sqlite3expert *p,
8220   sqlite3_stmt *pIndexXInfo,
8221   sqlite3_stmt *pWriteStat,
8222   const char *zTab,
8223   const char *zIdx,
8224   char **pzErr
8225 ){
8226   char *zCols = 0;
8227   char *zOrder = 0;
8228   char *zQuery = 0;
8229   int nCol = 0;
8230   int i;
8231   sqlite3_stmt *pQuery = 0;
8232   int *aStat = 0;
8233   int rc = SQLITE_OK;
8234 
8235   assert( p->iSample>0 );
8236 
8237   /* Formulate the query text */
8238   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8239   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8240     const char *zComma = zCols==0 ? "" : ", ";
8241     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8242     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8243     zCols = idxAppendText(&rc, zCols,
8244         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8245     );
8246     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8247   }
8248   sqlite3_reset(pIndexXInfo);
8249   if( rc==SQLITE_OK ){
8250     if( p->iSample==100 ){
8251       zQuery = sqlite3_mprintf(
8252           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8253       );
8254     }else{
8255       zQuery = sqlite3_mprintf(
8256           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8257       );
8258     }
8259   }
8260   sqlite3_free(zCols);
8261   sqlite3_free(zOrder);
8262 
8263   /* Formulate the query text */
8264   if( rc==SQLITE_OK ){
8265     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8266     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8267   }
8268   sqlite3_free(zQuery);
8269 
8270   if( rc==SQLITE_OK ){
8271     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8272   }
8273   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8274     IdxHashEntry *pEntry;
8275     char *zStat = 0;
8276     for(i=0; i<=nCol; i++) aStat[i] = 1;
8277     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8278       aStat[0]++;
8279       for(i=0; i<nCol; i++){
8280         if( sqlite3_column_int(pQuery, i)==0 ) break;
8281       }
8282       for(/*no-op*/; i<nCol; i++){
8283         aStat[i+1]++;
8284       }
8285     }
8286 
8287     if( rc==SQLITE_OK ){
8288       int s0 = aStat[0];
8289       zStat = sqlite3_mprintf("%d", s0);
8290       if( zStat==0 ) rc = SQLITE_NOMEM;
8291       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8292         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8293       }
8294     }
8295 
8296     if( rc==SQLITE_OK ){
8297       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8298       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8299       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8300       sqlite3_step(pWriteStat);
8301       rc = sqlite3_reset(pWriteStat);
8302     }
8303 
8304     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8305     if( pEntry ){
8306       assert( pEntry->zVal2==0 );
8307       pEntry->zVal2 = zStat;
8308     }else{
8309       sqlite3_free(zStat);
8310     }
8311   }
8312   sqlite3_free(aStat);
8313   idxFinalize(&rc, pQuery);
8314 
8315   return rc;
8316 }
8317 
8318 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8319   int rc;
8320   char *zSql;
8321 
8322   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8323   if( rc!=SQLITE_OK ) return rc;
8324 
8325   zSql = sqlite3_mprintf(
8326       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8327   );
8328   if( zSql==0 ) return SQLITE_NOMEM;
8329   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8330   sqlite3_free(zSql);
8331 
8332   return rc;
8333 }
8334 
8335 /*
8336 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8337 ** indexes have already been created in database sqlite3expert.dbm, this
8338 ** function populates sqlite_stat1 table in the same database.
8339 **
8340 ** The stat1 data is generated by querying the
8341 */
8342 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8343   int rc = SQLITE_OK;
8344   int nMax =0;
8345   struct IdxRemCtx *pCtx = 0;
8346   struct IdxSampleCtx samplectx;
8347   int i;
8348   i64 iPrev = -100000;
8349   sqlite3_stmt *pAllIndex = 0;
8350   sqlite3_stmt *pIndexXInfo = 0;
8351   sqlite3_stmt *pWrite = 0;
8352 
8353   const char *zAllIndex =
8354     "SELECT s.rowid, s.name, l.name FROM "
8355     "  sqlite_master AS s, "
8356     "  pragma_index_list(s.name) AS l "
8357     "WHERE s.type = 'table'";
8358   const char *zIndexXInfo =
8359     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8360   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8361 
8362   /* If iSample==0, no sqlite_stat1 data is required. */
8363   if( p->iSample==0 ) return SQLITE_OK;
8364 
8365   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8366   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8367 
8368   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8369 
8370   if( rc==SQLITE_OK ){
8371     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8372     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8373   }
8374 
8375   if( rc==SQLITE_OK ){
8376     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8377     rc = sqlite3_create_function(
8378         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8379     );
8380   }
8381   if( rc==SQLITE_OK ){
8382     rc = sqlite3_create_function(
8383         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8384     );
8385   }
8386 
8387   if( rc==SQLITE_OK ){
8388     pCtx->nSlot = nMax+1;
8389     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8390   }
8391   if( rc==SQLITE_OK ){
8392     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8393   }
8394   if( rc==SQLITE_OK ){
8395     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8396   }
8397 
8398   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8399     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8400     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8401     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8402     if( p->iSample<100 && iPrev!=iRowid ){
8403       samplectx.target = (double)p->iSample / 100.0;
8404       samplectx.iTarget = p->iSample;
8405       samplectx.nRow = 0.0;
8406       samplectx.nRet = 0.0;
8407       rc = idxBuildSampleTable(p, zTab);
8408       if( rc!=SQLITE_OK ) break;
8409     }
8410     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8411     iPrev = iRowid;
8412   }
8413   if( rc==SQLITE_OK && p->iSample<100 ){
8414     rc = sqlite3_exec(p->dbv,
8415         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8416     );
8417   }
8418 
8419   idxFinalize(&rc, pAllIndex);
8420   idxFinalize(&rc, pIndexXInfo);
8421   idxFinalize(&rc, pWrite);
8422 
8423   for(i=0; i<pCtx->nSlot; i++){
8424     sqlite3_free(pCtx->aSlot[i].z);
8425   }
8426   sqlite3_free(pCtx);
8427 
8428   if( rc==SQLITE_OK ){
8429     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8430   }
8431 
8432   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8433   return rc;
8434 }
8435 
8436 /*
8437 ** Allocate a new sqlite3expert object.
8438 */
8439 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8440   int rc = SQLITE_OK;
8441   sqlite3expert *pNew;
8442 
8443   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8444 
8445   /* Open two in-memory databases to work with. The "vtab database" (dbv)
8446   ** will contain a virtual table corresponding to each real table in
8447   ** the user database schema, and a copy of each view. It is used to
8448   ** collect information regarding the WHERE, ORDER BY and other clauses
8449   ** of the user's query.
8450   */
8451   if( rc==SQLITE_OK ){
8452     pNew->db = db;
8453     pNew->iSample = 100;
8454     rc = sqlite3_open(":memory:", &pNew->dbv);
8455   }
8456   if( rc==SQLITE_OK ){
8457     rc = sqlite3_open(":memory:", &pNew->dbm);
8458     if( rc==SQLITE_OK ){
8459       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8460     }
8461   }
8462 
8463 
8464   /* Copy the entire schema of database [db] into [dbm]. */
8465   if( rc==SQLITE_OK ){
8466     sqlite3_stmt *pSql;
8467     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
8468         "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8469         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8470     );
8471     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8472       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8473       rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8474     }
8475     idxFinalize(&rc, pSql);
8476   }
8477 
8478   /* Create the vtab schema */
8479   if( rc==SQLITE_OK ){
8480     rc = idxCreateVtabSchema(pNew, pzErrmsg);
8481   }
8482 
8483   /* Register the auth callback with dbv */
8484   if( rc==SQLITE_OK ){
8485     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8486   }
8487 
8488   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8489   ** return the new sqlite3expert handle.  */
8490   if( rc!=SQLITE_OK ){
8491     sqlite3_expert_destroy(pNew);
8492     pNew = 0;
8493   }
8494   return pNew;
8495 }
8496 
8497 /*
8498 ** Configure an sqlite3expert object.
8499 */
8500 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8501   int rc = SQLITE_OK;
8502   va_list ap;
8503   va_start(ap, op);
8504   switch( op ){
8505     case EXPERT_CONFIG_SAMPLE: {
8506       int iVal = va_arg(ap, int);
8507       if( iVal<0 ) iVal = 0;
8508       if( iVal>100 ) iVal = 100;
8509       p->iSample = iVal;
8510       break;
8511     }
8512     default:
8513       rc = SQLITE_NOTFOUND;
8514       break;
8515   }
8516 
8517   va_end(ap);
8518   return rc;
8519 }
8520 
8521 /*
8522 ** Add an SQL statement to the analysis.
8523 */
8524 int sqlite3_expert_sql(
8525   sqlite3expert *p,               /* From sqlite3_expert_new() */
8526   const char *zSql,               /* SQL statement to add */
8527   char **pzErr                    /* OUT: Error message (if any) */
8528 ){
8529   IdxScan *pScanOrig = p->pScan;
8530   IdxStatement *pStmtOrig = p->pStatement;
8531   int rc = SQLITE_OK;
8532   const char *zStmt = zSql;
8533 
8534   if( p->bRun ) return SQLITE_MISUSE;
8535 
8536   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8537     sqlite3_stmt *pStmt = 0;
8538     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8539     if( rc==SQLITE_OK ){
8540       if( pStmt ){
8541         IdxStatement *pNew;
8542         const char *z = sqlite3_sql(pStmt);
8543         int n = STRLEN(z);
8544         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8545         if( rc==SQLITE_OK ){
8546           pNew->zSql = (char*)&pNew[1];
8547           memcpy(pNew->zSql, z, n+1);
8548           pNew->pNext = p->pStatement;
8549           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8550           p->pStatement = pNew;
8551         }
8552         sqlite3_finalize(pStmt);
8553       }
8554     }else{
8555       idxDatabaseError(p->dbv, pzErr);
8556     }
8557   }
8558 
8559   if( rc!=SQLITE_OK ){
8560     idxScanFree(p->pScan, pScanOrig);
8561     idxStatementFree(p->pStatement, pStmtOrig);
8562     p->pScan = pScanOrig;
8563     p->pStatement = pStmtOrig;
8564   }
8565 
8566   return rc;
8567 }
8568 
8569 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8570   int rc;
8571   IdxHashEntry *pEntry;
8572 
8573   /* Do trigger processing to collect any extra IdxScan structures */
8574   rc = idxProcessTriggers(p, pzErr);
8575 
8576   /* Create candidate indexes within the in-memory database file */
8577   if( rc==SQLITE_OK ){
8578     rc = idxCreateCandidates(p);
8579   }
8580 
8581   /* Generate the stat1 data */
8582   if( rc==SQLITE_OK ){
8583     rc = idxPopulateStat1(p, pzErr);
8584   }
8585 
8586   /* Formulate the EXPERT_REPORT_CANDIDATES text */
8587   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8588     p->zCandidates = idxAppendText(&rc, p->zCandidates,
8589         "%s;%s%s\n", pEntry->zVal,
8590         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8591     );
8592   }
8593 
8594   /* Figure out which of the candidate indexes are preferred by the query
8595   ** planner and report the results to the user.  */
8596   if( rc==SQLITE_OK ){
8597     rc = idxFindIndexes(p, pzErr);
8598   }
8599 
8600   if( rc==SQLITE_OK ){
8601     p->bRun = 1;
8602   }
8603   return rc;
8604 }
8605 
8606 /*
8607 ** Return the total number of statements that have been added to this
8608 ** sqlite3expert using sqlite3_expert_sql().
8609 */
8610 int sqlite3_expert_count(sqlite3expert *p){
8611   int nRet = 0;
8612   if( p->pStatement ) nRet = p->pStatement->iId+1;
8613   return nRet;
8614 }
8615 
8616 /*
8617 ** Return a component of the report.
8618 */
8619 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8620   const char *zRet = 0;
8621   IdxStatement *pStmt;
8622 
8623   if( p->bRun==0 ) return 0;
8624   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8625   switch( eReport ){
8626     case EXPERT_REPORT_SQL:
8627       if( pStmt ) zRet = pStmt->zSql;
8628       break;
8629     case EXPERT_REPORT_INDEXES:
8630       if( pStmt ) zRet = pStmt->zIdx;
8631       break;
8632     case EXPERT_REPORT_PLAN:
8633       if( pStmt ) zRet = pStmt->zEQP;
8634       break;
8635     case EXPERT_REPORT_CANDIDATES:
8636       zRet = p->zCandidates;
8637       break;
8638   }
8639   return zRet;
8640 }
8641 
8642 /*
8643 ** Free an sqlite3expert object.
8644 */
8645 void sqlite3_expert_destroy(sqlite3expert *p){
8646   if( p ){
8647     sqlite3_close(p->dbm);
8648     sqlite3_close(p->dbv);
8649     idxScanFree(p->pScan, 0);
8650     idxStatementFree(p->pStatement, 0);
8651     idxTableFree(p->pTable);
8652     idxWriteFree(p->pWrite);
8653     idxHashClear(&p->hIdx);
8654     sqlite3_free(p->zCandidates);
8655     sqlite3_free(p);
8656   }
8657 }
8658 
8659 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
8660 
8661 /************************* End ../ext/expert/sqlite3expert.c ********************/
8662 
8663 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8664 /************************* Begin ../ext/misc/dbdata.c ******************/
8665 /*
8666 ** 2019-04-17
8667 **
8668 ** The author disclaims copyright to this source code.  In place of
8669 ** a legal notice, here is a blessing:
8670 **
8671 **    May you do good and not evil.
8672 **    May you find forgiveness for yourself and forgive others.
8673 **    May you share freely, never taking more than you give.
8674 **
8675 ******************************************************************************
8676 **
8677 ** This file contains an implementation of two eponymous virtual tables,
8678 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
8679 ** "sqlite_dbpage" eponymous virtual table be available.
8680 **
8681 ** SQLITE_DBDATA:
8682 **   sqlite_dbdata is used to extract data directly from a database b-tree
8683 **   page and its associated overflow pages, bypassing the b-tree layer.
8684 **   The table schema is equivalent to:
8685 **
8686 **     CREATE TABLE sqlite_dbdata(
8687 **       pgno INTEGER,
8688 **       cell INTEGER,
8689 **       field INTEGER,
8690 **       value ANY,
8691 **       schema TEXT HIDDEN
8692 **     );
8693 **
8694 **   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
8695 **   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
8696 **   "schema".
8697 **
8698 **   Each page of the database is inspected. If it cannot be interpreted as
8699 **   a b-tree page, or if it is a b-tree page containing 0 entries, the
8700 **   sqlite_dbdata table contains no rows for that page.  Otherwise, the
8701 **   table contains one row for each field in the record associated with
8702 **   each cell on the page. For intkey b-trees, the key value is stored in
8703 **   field -1.
8704 **
8705 **   For example, for the database:
8706 **
8707 **     CREATE TABLE t1(a, b);     -- root page is page 2
8708 **     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
8709 **     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
8710 **
8711 **   the sqlite_dbdata table contains, as well as from entries related to
8712 **   page 1, content equivalent to:
8713 **
8714 **     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
8715 **         (2, 0, -1, 5     ),
8716 **         (2, 0,  0, 'v'   ),
8717 **         (2, 0,  1, 'five'),
8718 **         (2, 1, -1, 10    ),
8719 **         (2, 1,  0, 'x'   ),
8720 **         (2, 1,  1, 'ten' );
8721 **
8722 **   If database corruption is encountered, this module does not report an
8723 **   error. Instead, it attempts to extract as much data as possible and
8724 **   ignores the corruption.
8725 **
8726 ** SQLITE_DBPTR:
8727 **   The sqlite_dbptr table has the following schema:
8728 **
8729 **     CREATE TABLE sqlite_dbptr(
8730 **       pgno INTEGER,
8731 **       child INTEGER,
8732 **       schema TEXT HIDDEN
8733 **     );
8734 **
8735 **   It contains one entry for each b-tree pointer between a parent and
8736 **   child page in the database.
8737 */
8738 #if !defined(SQLITEINT_H)
8739 /* #include "sqlite3ext.h" */
8740 
8741 /* typedef unsigned char u8; */
8742 
8743 #endif
8744 SQLITE_EXTENSION_INIT1
8745 #include <string.h>
8746 #include <assert.h>
8747 
8748 #define DBDATA_PADDING_BYTES 100
8749 
8750 typedef struct DbdataTable DbdataTable;
8751 typedef struct DbdataCursor DbdataCursor;
8752 
8753 /* Cursor object */
8754 struct DbdataCursor {
8755   sqlite3_vtab_cursor base;       /* Base class.  Must be first */
8756   sqlite3_stmt *pStmt;            /* For fetching database pages */
8757 
8758   int iPgno;                      /* Current page number */
8759   u8 *aPage;                      /* Buffer containing page */
8760   int nPage;                      /* Size of aPage[] in bytes */
8761   int nCell;                      /* Number of cells on aPage[] */
8762   int iCell;                      /* Current cell number */
8763   int bOnePage;                   /* True to stop after one page */
8764   int szDb;
8765   sqlite3_int64 iRowid;
8766 
8767   /* Only for the sqlite_dbdata table */
8768   u8 *pRec;                       /* Buffer containing current record */
8769   int nRec;                       /* Size of pRec[] in bytes */
8770   int nHdr;                       /* Size of header in bytes */
8771   int iField;                     /* Current field number */
8772   u8 *pHdrPtr;
8773   u8 *pPtr;
8774 
8775   sqlite3_int64 iIntkey;          /* Integer key value */
8776 };
8777 
8778 /* Table object */
8779 struct DbdataTable {
8780   sqlite3_vtab base;              /* Base class.  Must be first */
8781   sqlite3 *db;                    /* The database connection */
8782   sqlite3_stmt *pStmt;            /* For fetching database pages */
8783   int bPtr;                       /* True for sqlite3_dbptr table */
8784 };
8785 
8786 /* Column and schema definitions for sqlite_dbdata */
8787 #define DBDATA_COLUMN_PGNO        0
8788 #define DBDATA_COLUMN_CELL        1
8789 #define DBDATA_COLUMN_FIELD       2
8790 #define DBDATA_COLUMN_VALUE       3
8791 #define DBDATA_COLUMN_SCHEMA      4
8792 #define DBDATA_SCHEMA             \
8793       "CREATE TABLE x("           \
8794       "  pgno INTEGER,"           \
8795       "  cell INTEGER,"           \
8796       "  field INTEGER,"          \
8797       "  value ANY,"              \
8798       "  schema TEXT HIDDEN"      \
8799       ")"
8800 
8801 /* Column and schema definitions for sqlite_dbptr */
8802 #define DBPTR_COLUMN_PGNO         0
8803 #define DBPTR_COLUMN_CHILD        1
8804 #define DBPTR_COLUMN_SCHEMA       2
8805 #define DBPTR_SCHEMA              \
8806       "CREATE TABLE x("           \
8807       "  pgno INTEGER,"           \
8808       "  child INTEGER,"          \
8809       "  schema TEXT HIDDEN"      \
8810       ")"
8811 
8812 /*
8813 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
8814 ** table.
8815 */
8816 static int dbdataConnect(
8817   sqlite3 *db,
8818   void *pAux,
8819   int argc, const char *const*argv,
8820   sqlite3_vtab **ppVtab,
8821   char **pzErr
8822 ){
8823   DbdataTable *pTab = 0;
8824   int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
8825 
8826   if( rc==SQLITE_OK ){
8827     pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
8828     if( pTab==0 ){
8829       rc = SQLITE_NOMEM;
8830     }else{
8831       memset(pTab, 0, sizeof(DbdataTable));
8832       pTab->db = db;
8833       pTab->bPtr = (pAux!=0);
8834     }
8835   }
8836 
8837   *ppVtab = (sqlite3_vtab*)pTab;
8838   return rc;
8839 }
8840 
8841 /*
8842 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
8843 */
8844 static int dbdataDisconnect(sqlite3_vtab *pVtab){
8845   DbdataTable *pTab = (DbdataTable*)pVtab;
8846   if( pTab ){
8847     sqlite3_finalize(pTab->pStmt);
8848     sqlite3_free(pVtab);
8849   }
8850   return SQLITE_OK;
8851 }
8852 
8853 /*
8854 ** This function interprets two types of constraints:
8855 **
8856 **       schema=?
8857 **       pgno=?
8858 **
8859 ** If neither are present, idxNum is set to 0. If schema=? is present,
8860 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
8861 ** in idxNum is set.
8862 **
8863 ** If both parameters are present, schema is in position 0 and pgno in
8864 ** position 1.
8865 */
8866 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
8867   DbdataTable *pTab = (DbdataTable*)tab;
8868   int i;
8869   int iSchema = -1;
8870   int iPgno = -1;
8871   int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
8872 
8873   for(i=0; i<pIdx->nConstraint; i++){
8874     struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
8875     if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8876       if( p->iColumn==colSchema ){
8877         if( p->usable==0 ) return SQLITE_CONSTRAINT;
8878         iSchema = i;
8879       }
8880       if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
8881         iPgno = i;
8882       }
8883     }
8884   }
8885 
8886   if( iSchema>=0 ){
8887     pIdx->aConstraintUsage[iSchema].argvIndex = 1;
8888     pIdx->aConstraintUsage[iSchema].omit = 1;
8889   }
8890   if( iPgno>=0 ){
8891     pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
8892     pIdx->aConstraintUsage[iPgno].omit = 1;
8893     pIdx->estimatedCost = 100;
8894     pIdx->estimatedRows =  50;
8895 
8896     if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
8897       int iCol = pIdx->aOrderBy[0].iColumn;
8898       if( pIdx->nOrderBy==1 ){
8899         pIdx->orderByConsumed = (iCol==0 || iCol==1);
8900       }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
8901         pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
8902       }
8903     }
8904 
8905   }else{
8906     pIdx->estimatedCost = 100000000;
8907     pIdx->estimatedRows = 1000000000;
8908   }
8909   pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
8910   return SQLITE_OK;
8911 }
8912 
8913 /*
8914 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
8915 */
8916 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
8917   DbdataCursor *pCsr;
8918 
8919   pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
8920   if( pCsr==0 ){
8921     return SQLITE_NOMEM;
8922   }else{
8923     memset(pCsr, 0, sizeof(DbdataCursor));
8924     pCsr->base.pVtab = pVTab;
8925   }
8926 
8927   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
8928   return SQLITE_OK;
8929 }
8930 
8931 /*
8932 ** Restore a cursor object to the state it was in when first allocated
8933 ** by dbdataOpen().
8934 */
8935 static void dbdataResetCursor(DbdataCursor *pCsr){
8936   DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
8937   if( pTab->pStmt==0 ){
8938     pTab->pStmt = pCsr->pStmt;
8939   }else{
8940     sqlite3_finalize(pCsr->pStmt);
8941   }
8942   pCsr->pStmt = 0;
8943   pCsr->iPgno = 1;
8944   pCsr->iCell = 0;
8945   pCsr->iField = 0;
8946   pCsr->bOnePage = 0;
8947   sqlite3_free(pCsr->aPage);
8948   sqlite3_free(pCsr->pRec);
8949   pCsr->pRec = 0;
8950   pCsr->aPage = 0;
8951 }
8952 
8953 /*
8954 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
8955 */
8956 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
8957   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
8958   dbdataResetCursor(pCsr);
8959   sqlite3_free(pCsr);
8960   return SQLITE_OK;
8961 }
8962 
8963 /*
8964 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
8965 */
8966 static unsigned int get_uint16(unsigned char *a){
8967   return (a[0]<<8)|a[1];
8968 }
8969 static unsigned int get_uint32(unsigned char *a){
8970   return ((unsigned int)a[0]<<24)
8971        | ((unsigned int)a[1]<<16)
8972        | ((unsigned int)a[2]<<8)
8973        | ((unsigned int)a[3]);
8974 }
8975 
8976 /*
8977 ** Load page pgno from the database via the sqlite_dbpage virtual table.
8978 ** If successful, set (*ppPage) to point to a buffer containing the page
8979 ** data, (*pnPage) to the size of that buffer in bytes and return
8980 ** SQLITE_OK. In this case it is the responsibility of the caller to
8981 ** eventually free the buffer using sqlite3_free().
8982 **
8983 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
8984 ** return an SQLite error code.
8985 */
8986 static int dbdataLoadPage(
8987   DbdataCursor *pCsr,             /* Cursor object */
8988   unsigned int pgno,              /* Page number of page to load */
8989   u8 **ppPage,                    /* OUT: pointer to page buffer */
8990   int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
8991 ){
8992   int rc2;
8993   int rc = SQLITE_OK;
8994   sqlite3_stmt *pStmt = pCsr->pStmt;
8995 
8996   *ppPage = 0;
8997   *pnPage = 0;
8998   sqlite3_bind_int64(pStmt, 2, pgno);
8999   if( SQLITE_ROW==sqlite3_step(pStmt) ){
9000     int nCopy = sqlite3_column_bytes(pStmt, 0);
9001     if( nCopy>0 ){
9002       u8 *pPage;
9003       pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
9004       if( pPage==0 ){
9005         rc = SQLITE_NOMEM;
9006       }else{
9007         const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
9008         memcpy(pPage, pCopy, nCopy);
9009         memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
9010       }
9011       *ppPage = pPage;
9012       *pnPage = nCopy;
9013     }
9014   }
9015   rc2 = sqlite3_reset(pStmt);
9016   if( rc==SQLITE_OK ) rc = rc2;
9017 
9018   return rc;
9019 }
9020 
9021 /*
9022 ** Read a varint.  Put the value in *pVal and return the number of bytes.
9023 */
9024 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
9025   sqlite3_int64 v = 0;
9026   int i;
9027   for(i=0; i<8; i++){
9028     v = (v<<7) + (z[i]&0x7f);
9029     if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
9030   }
9031   v = (v<<8) + (z[i]&0xff);
9032   *pVal = v;
9033   return 9;
9034 }
9035 
9036 /*
9037 ** Return the number of bytes of space used by an SQLite value of type
9038 ** eType.
9039 */
9040 static int dbdataValueBytes(int eType){
9041   switch( eType ){
9042     case 0: case 8: case 9:
9043     case 10: case 11:
9044       return 0;
9045     case 1:
9046       return 1;
9047     case 2:
9048       return 2;
9049     case 3:
9050       return 3;
9051     case 4:
9052       return 4;
9053     case 5:
9054       return 6;
9055     case 6:
9056     case 7:
9057       return 8;
9058     default:
9059       if( eType>0 ){
9060         return ((eType-12) / 2);
9061       }
9062       return 0;
9063   }
9064 }
9065 
9066 /*
9067 ** Load a value of type eType from buffer pData and use it to set the
9068 ** result of context object pCtx.
9069 */
9070 static void dbdataValue(
9071   sqlite3_context *pCtx,
9072   int eType,
9073   u8 *pData,
9074   int nData
9075 ){
9076   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
9077     switch( eType ){
9078       case 0:
9079       case 10:
9080       case 11:
9081         sqlite3_result_null(pCtx);
9082         break;
9083 
9084       case 8:
9085         sqlite3_result_int(pCtx, 0);
9086         break;
9087       case 9:
9088         sqlite3_result_int(pCtx, 1);
9089         break;
9090 
9091       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
9092         sqlite3_uint64 v = (signed char)pData[0];
9093         pData++;
9094         switch( eType ){
9095           case 7:
9096           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
9097           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
9098           case 4:  v = (v<<8) + pData[0];  pData++;
9099           case 3:  v = (v<<8) + pData[0];  pData++;
9100           case 2:  v = (v<<8) + pData[0];  pData++;
9101         }
9102 
9103         if( eType==7 ){
9104           double r;
9105           memcpy(&r, &v, sizeof(r));
9106           sqlite3_result_double(pCtx, r);
9107         }else{
9108           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
9109         }
9110         break;
9111       }
9112 
9113       default: {
9114         int n = ((eType-12) / 2);
9115         if( eType % 2 ){
9116           sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
9117         }else{
9118           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
9119         }
9120       }
9121     }
9122   }
9123 }
9124 
9125 /*
9126 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
9127 */
9128 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
9129   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9130   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9131 
9132   pCsr->iRowid++;
9133   while( 1 ){
9134     int rc;
9135     int iOff = (pCsr->iPgno==1 ? 100 : 0);
9136     int bNextPage = 0;
9137 
9138     if( pCsr->aPage==0 ){
9139       while( 1 ){
9140         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
9141         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
9142         if( rc!=SQLITE_OK ) return rc;
9143         if( pCsr->aPage ) break;
9144         pCsr->iPgno++;
9145       }
9146       pCsr->iCell = pTab->bPtr ? -2 : 0;
9147       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
9148     }
9149 
9150     if( pTab->bPtr ){
9151       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
9152         pCsr->iCell = pCsr->nCell;
9153       }
9154       pCsr->iCell++;
9155       if( pCsr->iCell>=pCsr->nCell ){
9156         sqlite3_free(pCsr->aPage);
9157         pCsr->aPage = 0;
9158         if( pCsr->bOnePage ) return SQLITE_OK;
9159         pCsr->iPgno++;
9160       }else{
9161         return SQLITE_OK;
9162       }
9163     }else{
9164       /* If there is no record loaded, load it now. */
9165       if( pCsr->pRec==0 ){
9166         int bHasRowid = 0;
9167         int nPointer = 0;
9168         sqlite3_int64 nPayload = 0;
9169         sqlite3_int64 nHdr = 0;
9170         int iHdr;
9171         int U, X;
9172         int nLocal;
9173 
9174         switch( pCsr->aPage[iOff] ){
9175           case 0x02:
9176             nPointer = 4;
9177             break;
9178           case 0x0a:
9179             break;
9180           case 0x0d:
9181             bHasRowid = 1;
9182             break;
9183           default:
9184             /* This is not a b-tree page with records on it. Continue. */
9185             pCsr->iCell = pCsr->nCell;
9186             break;
9187         }
9188 
9189         if( pCsr->iCell>=pCsr->nCell ){
9190           bNextPage = 1;
9191         }else{
9192 
9193           iOff += 8 + nPointer + pCsr->iCell*2;
9194           if( iOff>pCsr->nPage ){
9195             bNextPage = 1;
9196           }else{
9197             iOff = get_uint16(&pCsr->aPage[iOff]);
9198           }
9199 
9200           /* For an interior node cell, skip past the child-page number */
9201           iOff += nPointer;
9202 
9203           /* Load the "byte of payload including overflow" field */
9204           if( bNextPage || iOff>pCsr->nPage ){
9205             bNextPage = 1;
9206           }else{
9207             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
9208           }
9209 
9210           /* If this is a leaf intkey cell, load the rowid */
9211           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
9212             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
9213           }
9214 
9215           /* Figure out how much data to read from the local page */
9216           U = pCsr->nPage;
9217           if( bHasRowid ){
9218             X = U-35;
9219           }else{
9220             X = ((U-12)*64/255)-23;
9221           }
9222           if( nPayload<=X ){
9223             nLocal = nPayload;
9224           }else{
9225             int M, K;
9226             M = ((U-12)*32/255)-23;
9227             K = M+((nPayload-M)%(U-4));
9228             if( K<=X ){
9229               nLocal = K;
9230             }else{
9231               nLocal = M;
9232             }
9233           }
9234 
9235           if( bNextPage || nLocal+iOff>pCsr->nPage ){
9236             bNextPage = 1;
9237           }else{
9238 
9239             /* Allocate space for payload. And a bit more to catch small buffer
9240             ** overruns caused by attempting to read a varint or similar from
9241             ** near the end of a corrupt record.  */
9242             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
9243             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
9244             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
9245             pCsr->nRec = nPayload;
9246 
9247             /* Load the nLocal bytes of payload */
9248             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
9249             iOff += nLocal;
9250 
9251             /* Load content from overflow pages */
9252             if( nPayload>nLocal ){
9253               sqlite3_int64 nRem = nPayload - nLocal;
9254               unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
9255               while( nRem>0 ){
9256                 u8 *aOvfl = 0;
9257                 int nOvfl = 0;
9258                 int nCopy;
9259                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
9260                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
9261                 if( rc!=SQLITE_OK ) return rc;
9262                 if( aOvfl==0 ) break;
9263 
9264                 nCopy = U-4;
9265                 if( nCopy>nRem ) nCopy = nRem;
9266                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
9267                 nRem -= nCopy;
9268 
9269                 pgnoOvfl = get_uint32(aOvfl);
9270                 sqlite3_free(aOvfl);
9271               }
9272             }
9273 
9274             iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
9275             pCsr->nHdr = nHdr;
9276             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
9277             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
9278             pCsr->iField = (bHasRowid ? -1 : 0);
9279           }
9280         }
9281       }else{
9282         pCsr->iField++;
9283         if( pCsr->iField>0 ){
9284           sqlite3_int64 iType;
9285           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
9286             bNextPage = 1;
9287           }else{
9288             pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
9289             pCsr->pPtr += dbdataValueBytes(iType);
9290           }
9291         }
9292       }
9293 
9294       if( bNextPage ){
9295         sqlite3_free(pCsr->aPage);
9296         sqlite3_free(pCsr->pRec);
9297         pCsr->aPage = 0;
9298         pCsr->pRec = 0;
9299         if( pCsr->bOnePage ) return SQLITE_OK;
9300         pCsr->iPgno++;
9301       }else{
9302         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
9303           return SQLITE_OK;
9304         }
9305 
9306         /* Advance to the next cell. The next iteration of the loop will load
9307         ** the record and so on. */
9308         sqlite3_free(pCsr->pRec);
9309         pCsr->pRec = 0;
9310         pCsr->iCell++;
9311       }
9312     }
9313   }
9314 
9315   assert( !"can't get here" );
9316   return SQLITE_OK;
9317 }
9318 
9319 /*
9320 ** Return true if the cursor is at EOF.
9321 */
9322 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
9323   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9324   return pCsr->aPage==0;
9325 }
9326 
9327 /*
9328 ** Determine the size in pages of database zSchema (where zSchema is
9329 ** "main", "temp" or the name of an attached database) and set
9330 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
9331 ** an SQLite error code.
9332 */
9333 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
9334   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
9335   char *zSql = 0;
9336   int rc, rc2;
9337   sqlite3_stmt *pStmt = 0;
9338 
9339   zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
9340   if( zSql==0 ) return SQLITE_NOMEM;
9341   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
9342   sqlite3_free(zSql);
9343   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9344     pCsr->szDb = sqlite3_column_int(pStmt, 0);
9345   }
9346   rc2 = sqlite3_finalize(pStmt);
9347   if( rc==SQLITE_OK ) rc = rc2;
9348   return rc;
9349 }
9350 
9351 /*
9352 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
9353 */
9354 static int dbdataFilter(
9355   sqlite3_vtab_cursor *pCursor,
9356   int idxNum, const char *idxStr,
9357   int argc, sqlite3_value **argv
9358 ){
9359   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9360   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9361   int rc = SQLITE_OK;
9362   const char *zSchema = "main";
9363 
9364   dbdataResetCursor(pCsr);
9365   assert( pCsr->iPgno==1 );
9366   if( idxNum & 0x01 ){
9367     zSchema = (const char*)sqlite3_value_text(argv[0]);
9368   }
9369   if( idxNum & 0x02 ){
9370     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
9371     pCsr->bOnePage = 1;
9372   }else{
9373     pCsr->nPage = dbdataDbsize(pCsr, zSchema);
9374     rc = dbdataDbsize(pCsr, zSchema);
9375   }
9376 
9377   if( rc==SQLITE_OK ){
9378     if( pTab->pStmt ){
9379       pCsr->pStmt = pTab->pStmt;
9380       pTab->pStmt = 0;
9381     }else{
9382       rc = sqlite3_prepare_v2(pTab->db,
9383           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
9384           &pCsr->pStmt, 0
9385       );
9386     }
9387   }
9388   if( rc==SQLITE_OK ){
9389     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
9390   }else{
9391     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
9392   }
9393   if( rc==SQLITE_OK ){
9394     rc = dbdataNext(pCursor);
9395   }
9396   return rc;
9397 }
9398 
9399 /*
9400 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
9401 */
9402 static int dbdataColumn(
9403   sqlite3_vtab_cursor *pCursor,
9404   sqlite3_context *ctx,
9405   int i
9406 ){
9407   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9408   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9409   if( pTab->bPtr ){
9410     switch( i ){
9411       case DBPTR_COLUMN_PGNO:
9412         sqlite3_result_int64(ctx, pCsr->iPgno);
9413         break;
9414       case DBPTR_COLUMN_CHILD: {
9415         int iOff = pCsr->iPgno==1 ? 100 : 0;
9416         if( pCsr->iCell<0 ){
9417           iOff += 8;
9418         }else{
9419           iOff += 12 + pCsr->iCell*2;
9420           if( iOff>pCsr->nPage ) return SQLITE_OK;
9421           iOff = get_uint16(&pCsr->aPage[iOff]);
9422         }
9423         if( iOff<=pCsr->nPage ){
9424           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
9425         }
9426         break;
9427       }
9428     }
9429   }else{
9430     switch( i ){
9431       case DBDATA_COLUMN_PGNO:
9432         sqlite3_result_int64(ctx, pCsr->iPgno);
9433         break;
9434       case DBDATA_COLUMN_CELL:
9435         sqlite3_result_int(ctx, pCsr->iCell);
9436         break;
9437       case DBDATA_COLUMN_FIELD:
9438         sqlite3_result_int(ctx, pCsr->iField);
9439         break;
9440       case DBDATA_COLUMN_VALUE: {
9441         if( pCsr->iField<0 ){
9442           sqlite3_result_int64(ctx, pCsr->iIntkey);
9443         }else{
9444           sqlite3_int64 iType;
9445           dbdataGetVarint(pCsr->pHdrPtr, &iType);
9446           dbdataValue(
9447               ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
9448           );
9449         }
9450         break;
9451       }
9452     }
9453   }
9454   return SQLITE_OK;
9455 }
9456 
9457 /*
9458 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
9459 */
9460 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
9461   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9462   *pRowid = pCsr->iRowid;
9463   return SQLITE_OK;
9464 }
9465 
9466 
9467 /*
9468 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
9469 */
9470 static int sqlite3DbdataRegister(sqlite3 *db){
9471   static sqlite3_module dbdata_module = {
9472     0,                            /* iVersion */
9473     0,                            /* xCreate */
9474     dbdataConnect,                /* xConnect */
9475     dbdataBestIndex,              /* xBestIndex */
9476     dbdataDisconnect,             /* xDisconnect */
9477     0,                            /* xDestroy */
9478     dbdataOpen,                   /* xOpen - open a cursor */
9479     dbdataClose,                  /* xClose - close a cursor */
9480     dbdataFilter,                 /* xFilter - configure scan constraints */
9481     dbdataNext,                   /* xNext - advance a cursor */
9482     dbdataEof,                    /* xEof - check for end of scan */
9483     dbdataColumn,                 /* xColumn - read data */
9484     dbdataRowid,                  /* xRowid - read data */
9485     0,                            /* xUpdate */
9486     0,                            /* xBegin */
9487     0,                            /* xSync */
9488     0,                            /* xCommit */
9489     0,                            /* xRollback */
9490     0,                            /* xFindMethod */
9491     0,                            /* xRename */
9492     0,                            /* xSavepoint */
9493     0,                            /* xRelease */
9494     0,                            /* xRollbackTo */
9495     0                             /* xShadowName */
9496   };
9497 
9498   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
9499   if( rc==SQLITE_OK ){
9500     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
9501   }
9502   return rc;
9503 }
9504 
9505 #ifdef _WIN32
9506 
9507 #endif
9508 int sqlite3_dbdata_init(
9509   sqlite3 *db,
9510   char **pzErrMsg,
9511   const sqlite3_api_routines *pApi
9512 ){
9513   SQLITE_EXTENSION_INIT2(pApi);
9514   return sqlite3DbdataRegister(db);
9515 }
9516 
9517 /************************* End ../ext/misc/dbdata.c ********************/
9518 #endif
9519 
9520 #if defined(SQLITE_ENABLE_SESSION)
9521 /*
9522 ** State information for a single open session
9523 */
9524 typedef struct OpenSession OpenSession;
9525 struct OpenSession {
9526   char *zName;             /* Symbolic name for this session */
9527   int nFilter;             /* Number of xFilter rejection GLOB patterns */
9528   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
9529   sqlite3_session *p;      /* The open session */
9530 };
9531 #endif
9532 
9533 /*
9534 ** Shell output mode information from before ".explain on",
9535 ** saved so that it can be restored by ".explain off"
9536 */
9537 typedef struct SavedModeInfo SavedModeInfo;
9538 struct SavedModeInfo {
9539   int valid;          /* Is there legit data in here? */
9540   int mode;           /* Mode prior to ".explain on" */
9541   int showHeader;     /* The ".header" setting prior to ".explain on" */
9542   int colWidth[100];  /* Column widths prior to ".explain on" */
9543 };
9544 
9545 typedef struct ExpertInfo ExpertInfo;
9546 struct ExpertInfo {
9547   sqlite3expert *pExpert;
9548   int bVerbose;
9549 };
9550 
9551 /* A single line in the EQP output */
9552 typedef struct EQPGraphRow EQPGraphRow;
9553 struct EQPGraphRow {
9554   int iEqpId;           /* ID for this row */
9555   int iParentId;        /* ID of the parent row */
9556   EQPGraphRow *pNext;   /* Next row in sequence */
9557   char zText[1];        /* Text to display for this row */
9558 };
9559 
9560 /* All EQP output is collected into an instance of the following */
9561 typedef struct EQPGraph EQPGraph;
9562 struct EQPGraph {
9563   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
9564   EQPGraphRow *pLast;   /* Last element of the pRow list */
9565   char zPrefix[100];    /* Graph prefix */
9566 };
9567 
9568 /*
9569 ** State information about the database connection is contained in an
9570 ** instance of the following structure.
9571 */
9572 typedef struct ShellState ShellState;
9573 struct ShellState {
9574   sqlite3 *db;           /* The database */
9575   u8 autoExplain;        /* Automatically turn on .explain mode */
9576   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
9577   u8 autoEQPtest;        /* autoEQP is in test mode */
9578   u8 autoEQPtrace;       /* autoEQP is in trace mode */
9579   u8 statsOn;            /* True to display memory stats before each finalize */
9580   u8 scanstatsOn;        /* True to display scan stats before each finalize */
9581   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
9582   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
9583   u8 nEqpLevel;          /* Depth of the EQP output graph */
9584   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
9585   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
9586   int outCount;          /* Revert to stdout when reaching zero */
9587   int cnt;               /* Number of records displayed so far */
9588   int lineno;            /* Line number of last line read from in */
9589   int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
9590   FILE *in;              /* Read commands from this stream */
9591   FILE *out;             /* Write results here */
9592   FILE *traceOut;        /* Output for sqlite3_trace() */
9593   int nErr;              /* Number of errors seen */
9594   int mode;              /* An output mode setting */
9595   int modePrior;         /* Saved mode */
9596   int cMode;             /* temporary output mode for the current query */
9597   int normalMode;        /* Output mode before ".explain on" */
9598   int writableSchema;    /* True if PRAGMA writable_schema=ON */
9599   int showHeader;        /* True to show column names in List or Column mode */
9600   int nCheck;            /* Number of ".check" commands run */
9601   unsigned nProgress;    /* Number of progress callbacks encountered */
9602   unsigned mxProgress;   /* Maximum progress callbacks before failing */
9603   unsigned flgProgress;  /* Flags for the progress callback */
9604   unsigned shellFlgs;    /* Various flags */
9605   sqlite3_int64 szMax;   /* --maxsize argument to .open */
9606   char *zDestTable;      /* Name of destination table when MODE_Insert */
9607   char *zTempFile;       /* Temporary file that might need deleting */
9608   char zTestcase[30];    /* Name of current test case */
9609   char colSeparator[20]; /* Column separator character for several modes */
9610   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
9611   char colSepPrior[20];  /* Saved column separator */
9612   char rowSepPrior[20];  /* Saved row separator */
9613   int colWidth[100];     /* Requested width of each column when in column mode*/
9614   int actualWidth[100];  /* Actual width of each column */
9615   char nullValue[20];    /* The text to print when a NULL comes back from
9616                          ** the database */
9617   char outfile[FILENAME_MAX]; /* Filename for *out */
9618   const char *zDbFilename;    /* name of the database file */
9619   char *zFreeOnClose;         /* Filename to free when closing */
9620   const char *zVfs;           /* Name of VFS to use */
9621   sqlite3_stmt *pStmt;   /* Current statement if any. */
9622   FILE *pLog;            /* Write log output here */
9623   int *aiIndent;         /* Array of indents used in MODE_Explain */
9624   int nIndent;           /* Size of array aiIndent[] */
9625   int iIndent;           /* Index of current op in aiIndent[] */
9626   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
9627 #if defined(SQLITE_ENABLE_SESSION)
9628   int nSession;             /* Number of active sessions */
9629   OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
9630 #endif
9631   ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
9632 };
9633 
9634 
9635 /* Allowed values for ShellState.autoEQP
9636 */
9637 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
9638 #define AUTOEQP_on       1           /* Automatic EQP is on */
9639 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
9640 #define AUTOEQP_full     3           /* Show full EXPLAIN */
9641 
9642 /* Allowed values for ShellState.openMode
9643 */
9644 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
9645 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
9646 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
9647 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
9648 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
9649 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
9650 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
9651 
9652 /* Allowed values for ShellState.eTraceType
9653 */
9654 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
9655 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
9656 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
9657 
9658 /* Bits in the ShellState.flgProgress variable */
9659 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
9660 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
9661                                    ** callback limit is reached, and for each
9662                                    ** top-level SQL statement */
9663 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
9664 
9665 /*
9666 ** These are the allowed shellFlgs values
9667 */
9668 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
9669 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
9670 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
9671 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
9672 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
9673 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
9674 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
9675 
9676 /*
9677 ** Macros for testing and setting shellFlgs
9678 */
9679 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
9680 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
9681 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
9682 
9683 /*
9684 ** These are the allowed modes.
9685 */
9686 #define MODE_Line     0  /* One column per line.  Blank line between records */
9687 #define MODE_Column   1  /* One record per line in neat columns */
9688 #define MODE_List     2  /* One record per line with a separator */
9689 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
9690 #define MODE_Html     4  /* Generate an XHTML table */
9691 #define MODE_Insert   5  /* Generate SQL "insert" statements */
9692 #define MODE_Quote    6  /* Quote values as for SQL */
9693 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
9694 #define MODE_Csv      8  /* Quote strings, numbers are plain */
9695 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
9696 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
9697 #define MODE_Pretty  11  /* Pretty-print schemas */
9698 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
9699 
9700 static const char *modeDescr[] = {
9701   "line",
9702   "column",
9703   "list",
9704   "semi",
9705   "html",
9706   "insert",
9707   "quote",
9708   "tcl",
9709   "csv",
9710   "explain",
9711   "ascii",
9712   "prettyprint",
9713   "eqp"
9714 };
9715 
9716 /*
9717 ** These are the column/row/line separators used by the various
9718 ** import/export modes.
9719 */
9720 #define SEP_Column    "|"
9721 #define SEP_Row       "\n"
9722 #define SEP_Tab       "\t"
9723 #define SEP_Space     " "
9724 #define SEP_Comma     ","
9725 #define SEP_CrLf      "\r\n"
9726 #define SEP_Unit      "\x1F"
9727 #define SEP_Record    "\x1E"
9728 
9729 /*
9730 ** A callback for the sqlite3_log() interface.
9731 */
9732 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
9733   ShellState *p = (ShellState*)pArg;
9734   if( p->pLog==0 ) return;
9735   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
9736   fflush(p->pLog);
9737 }
9738 
9739 /*
9740 ** SQL function:  shell_putsnl(X)
9741 **
9742 ** Write the text X to the screen (or whatever output is being directed)
9743 ** adding a newline at the end, and then return X.
9744 */
9745 static void shellPutsFunc(
9746   sqlite3_context *pCtx,
9747   int nVal,
9748   sqlite3_value **apVal
9749 ){
9750   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
9751   (void)nVal;
9752   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
9753   sqlite3_result_value(pCtx, apVal[0]);
9754 }
9755 
9756 /*
9757 ** SQL function:   edit(VALUE)
9758 **                 edit(VALUE,EDITOR)
9759 **
9760 ** These steps:
9761 **
9762 **     (1) Write VALUE into a temporary file.
9763 **     (2) Run program EDITOR on that temporary file.
9764 **     (3) Read the temporary file back and return its content as the result.
9765 **     (4) Delete the temporary file
9766 **
9767 ** If the EDITOR argument is omitted, use the value in the VISUAL
9768 ** environment variable.  If still there is no EDITOR, through an error.
9769 **
9770 ** Also throw an error if the EDITOR program returns a non-zero exit code.
9771 */
9772 #ifndef SQLITE_NOHAVE_SYSTEM
9773 static void editFunc(
9774   sqlite3_context *context,
9775   int argc,
9776   sqlite3_value **argv
9777 ){
9778   const char *zEditor;
9779   char *zTempFile = 0;
9780   sqlite3 *db;
9781   char *zCmd = 0;
9782   int bBin;
9783   int rc;
9784   int hasCRNL = 0;
9785   FILE *f = 0;
9786   sqlite3_int64 sz;
9787   sqlite3_int64 x;
9788   unsigned char *p = 0;
9789 
9790   if( argc==2 ){
9791     zEditor = (const char*)sqlite3_value_text(argv[1]);
9792   }else{
9793     zEditor = getenv("VISUAL");
9794   }
9795   if( zEditor==0 ){
9796     sqlite3_result_error(context, "no editor for edit()", -1);
9797     return;
9798   }
9799   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
9800     sqlite3_result_error(context, "NULL input to edit()", -1);
9801     return;
9802   }
9803   db = sqlite3_context_db_handle(context);
9804   zTempFile = 0;
9805   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
9806   if( zTempFile==0 ){
9807     sqlite3_uint64 r = 0;
9808     sqlite3_randomness(sizeof(r), &r);
9809     zTempFile = sqlite3_mprintf("temp%llx", r);
9810     if( zTempFile==0 ){
9811       sqlite3_result_error_nomem(context);
9812       return;
9813     }
9814   }
9815   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
9816   /* When writing the file to be edited, do \n to \r\n conversions on systems
9817   ** that want \r\n line endings */
9818   f = fopen(zTempFile, bBin ? "wb" : "w");
9819   if( f==0 ){
9820     sqlite3_result_error(context, "edit() cannot open temp file", -1);
9821     goto edit_func_end;
9822   }
9823   sz = sqlite3_value_bytes(argv[0]);
9824   if( bBin ){
9825     x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
9826   }else{
9827     const char *z = (const char*)sqlite3_value_text(argv[0]);
9828     /* Remember whether or not the value originally contained \r\n */
9829     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
9830     x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
9831   }
9832   fclose(f);
9833   f = 0;
9834   if( x!=sz ){
9835     sqlite3_result_error(context, "edit() could not write the whole file", -1);
9836     goto edit_func_end;
9837   }
9838   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
9839   if( zCmd==0 ){
9840     sqlite3_result_error_nomem(context);
9841     goto edit_func_end;
9842   }
9843   rc = system(zCmd);
9844   sqlite3_free(zCmd);
9845   if( rc ){
9846     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
9847     goto edit_func_end;
9848   }
9849   f = fopen(zTempFile, "rb");
9850   if( f==0 ){
9851     sqlite3_result_error(context,
9852       "edit() cannot reopen temp file after edit", -1);
9853     goto edit_func_end;
9854   }
9855   fseek(f, 0, SEEK_END);
9856   sz = ftell(f);
9857   rewind(f);
9858   p = sqlite3_malloc64( sz+1 );
9859   if( p==0 ){
9860     sqlite3_result_error_nomem(context);
9861     goto edit_func_end;
9862   }
9863   x = fread(p, 1, (size_t)sz, f);
9864   fclose(f);
9865   f = 0;
9866   if( x!=sz ){
9867     sqlite3_result_error(context, "could not read back the whole file", -1);
9868     goto edit_func_end;
9869   }
9870   if( bBin ){
9871     sqlite3_result_blob64(context, p, sz, sqlite3_free);
9872   }else{
9873     sqlite3_int64 i, j;
9874     if( hasCRNL ){
9875       /* If the original contains \r\n then do no conversions back to \n */
9876       j = sz;
9877     }else{
9878       /* If the file did not originally contain \r\n then convert any new
9879       ** \r\n back into \n */
9880       for(i=j=0; i<sz; i++){
9881         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
9882         p[j++] = p[i];
9883       }
9884       sz = j;
9885       p[sz] = 0;
9886     }
9887     sqlite3_result_text64(context, (const char*)p, sz,
9888                           sqlite3_free, SQLITE_UTF8);
9889   }
9890   p = 0;
9891 
9892 edit_func_end:
9893   if( f ) fclose(f);
9894   unlink(zTempFile);
9895   sqlite3_free(zTempFile);
9896   sqlite3_free(p);
9897 }
9898 #endif /* SQLITE_NOHAVE_SYSTEM */
9899 
9900 /*
9901 ** Save or restore the current output mode
9902 */
9903 static void outputModePush(ShellState *p){
9904   p->modePrior = p->mode;
9905   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
9906   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
9907 }
9908 static void outputModePop(ShellState *p){
9909   p->mode = p->modePrior;
9910   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
9911   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
9912 }
9913 
9914 /*
9915 ** Output the given string as a hex-encoded blob (eg. X'1234' )
9916 */
9917 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
9918   int i;
9919   char *zBlob = (char *)pBlob;
9920   raw_printf(out,"X'");
9921   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
9922   raw_printf(out,"'");
9923 }
9924 
9925 /*
9926 ** Find a string that is not found anywhere in z[].  Return a pointer
9927 ** to that string.
9928 **
9929 ** Try to use zA and zB first.  If both of those are already found in z[]
9930 ** then make up some string and store it in the buffer zBuf.
9931 */
9932 static const char *unused_string(
9933   const char *z,                    /* Result must not appear anywhere in z */
9934   const char *zA, const char *zB,   /* Try these first */
9935   char *zBuf                        /* Space to store a generated string */
9936 ){
9937   unsigned i = 0;
9938   if( strstr(z, zA)==0 ) return zA;
9939   if( strstr(z, zB)==0 ) return zB;
9940   do{
9941     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
9942   }while( strstr(z,zBuf)!=0 );
9943   return zBuf;
9944 }
9945 
9946 /*
9947 ** Output the given string as a quoted string using SQL quoting conventions.
9948 **
9949 ** See also: output_quoted_escaped_string()
9950 */
9951 static void output_quoted_string(FILE *out, const char *z){
9952   int i;
9953   char c;
9954   setBinaryMode(out, 1);
9955   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9956   if( c==0 ){
9957     utf8_printf(out,"'%s'",z);
9958   }else{
9959     raw_printf(out, "'");
9960     while( *z ){
9961       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9962       if( c=='\'' ) i++;
9963       if( i ){
9964         utf8_printf(out, "%.*s", i, z);
9965         z += i;
9966       }
9967       if( c=='\'' ){
9968         raw_printf(out, "'");
9969         continue;
9970       }
9971       if( c==0 ){
9972         break;
9973       }
9974       z++;
9975     }
9976     raw_printf(out, "'");
9977   }
9978   setTextMode(out, 1);
9979 }
9980 
9981 /*
9982 ** Output the given string as a quoted string using SQL quoting conventions.
9983 ** Additionallly , escape the "\n" and "\r" characters so that they do not
9984 ** get corrupted by end-of-line translation facilities in some operating
9985 ** systems.
9986 **
9987 ** This is like output_quoted_string() but with the addition of the \r\n
9988 ** escape mechanism.
9989 */
9990 static void output_quoted_escaped_string(FILE *out, const char *z){
9991   int i;
9992   char c;
9993   setBinaryMode(out, 1);
9994   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
9995   if( c==0 ){
9996     utf8_printf(out,"'%s'",z);
9997   }else{
9998     const char *zNL = 0;
9999     const char *zCR = 0;
10000     int nNL = 0;
10001     int nCR = 0;
10002     char zBuf1[20], zBuf2[20];
10003     for(i=0; z[i]; i++){
10004       if( z[i]=='\n' ) nNL++;
10005       if( z[i]=='\r' ) nCR++;
10006     }
10007     if( nNL ){
10008       raw_printf(out, "replace(");
10009       zNL = unused_string(z, "\\n", "\\012", zBuf1);
10010     }
10011     if( nCR ){
10012       raw_printf(out, "replace(");
10013       zCR = unused_string(z, "\\r", "\\015", zBuf2);
10014     }
10015     raw_printf(out, "'");
10016     while( *z ){
10017       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
10018       if( c=='\'' ) i++;
10019       if( i ){
10020         utf8_printf(out, "%.*s", i, z);
10021         z += i;
10022       }
10023       if( c=='\'' ){
10024         raw_printf(out, "'");
10025         continue;
10026       }
10027       if( c==0 ){
10028         break;
10029       }
10030       z++;
10031       if( c=='\n' ){
10032         raw_printf(out, "%s", zNL);
10033         continue;
10034       }
10035       raw_printf(out, "%s", zCR);
10036     }
10037     raw_printf(out, "'");
10038     if( nCR ){
10039       raw_printf(out, ",'%s',char(13))", zCR);
10040     }
10041     if( nNL ){
10042       raw_printf(out, ",'%s',char(10))", zNL);
10043     }
10044   }
10045   setTextMode(out, 1);
10046 }
10047 
10048 /*
10049 ** Output the given string as a quoted according to C or TCL quoting rules.
10050 */
10051 static void output_c_string(FILE *out, const char *z){
10052   unsigned int c;
10053   fputc('"', out);
10054   while( (c = *(z++))!=0 ){
10055     if( c=='\\' ){
10056       fputc(c, out);
10057       fputc(c, out);
10058     }else if( c=='"' ){
10059       fputc('\\', out);
10060       fputc('"', out);
10061     }else if( c=='\t' ){
10062       fputc('\\', out);
10063       fputc('t', out);
10064     }else if( c=='\n' ){
10065       fputc('\\', out);
10066       fputc('n', out);
10067     }else if( c=='\r' ){
10068       fputc('\\', out);
10069       fputc('r', out);
10070     }else if( !isprint(c&0xff) ){
10071       raw_printf(out, "\\%03o", c&0xff);
10072     }else{
10073       fputc(c, out);
10074     }
10075   }
10076   fputc('"', out);
10077 }
10078 
10079 /*
10080 ** Output the given string with characters that are special to
10081 ** HTML escaped.
10082 */
10083 static void output_html_string(FILE *out, const char *z){
10084   int i;
10085   if( z==0 ) z = "";
10086   while( *z ){
10087     for(i=0;   z[i]
10088             && z[i]!='<'
10089             && z[i]!='&'
10090             && z[i]!='>'
10091             && z[i]!='\"'
10092             && z[i]!='\'';
10093         i++){}
10094     if( i>0 ){
10095       utf8_printf(out,"%.*s",i,z);
10096     }
10097     if( z[i]=='<' ){
10098       raw_printf(out,"&lt;");
10099     }else if( z[i]=='&' ){
10100       raw_printf(out,"&amp;");
10101     }else if( z[i]=='>' ){
10102       raw_printf(out,"&gt;");
10103     }else if( z[i]=='\"' ){
10104       raw_printf(out,"&quot;");
10105     }else if( z[i]=='\'' ){
10106       raw_printf(out,"&#39;");
10107     }else{
10108       break;
10109     }
10110     z += i + 1;
10111   }
10112 }
10113 
10114 /*
10115 ** If a field contains any character identified by a 1 in the following
10116 ** array, then the string must be quoted for CSV.
10117 */
10118 static const char needCsvQuote[] = {
10119   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10120   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10121   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
10122   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10123   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10124   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10125   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
10126   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
10127   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10128   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10129   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10130   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10131   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10132   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10133   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10134   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
10135 };
10136 
10137 /*
10138 ** Output a single term of CSV.  Actually, p->colSeparator is used for
10139 ** the separator, which may or may not be a comma.  p->nullValue is
10140 ** the null value.  Strings are quoted if necessary.  The separator
10141 ** is only issued if bSep is true.
10142 */
10143 static void output_csv(ShellState *p, const char *z, int bSep){
10144   FILE *out = p->out;
10145   if( z==0 ){
10146     utf8_printf(out,"%s",p->nullValue);
10147   }else{
10148     int i;
10149     int nSep = strlen30(p->colSeparator);
10150     for(i=0; z[i]; i++){
10151       if( needCsvQuote[((unsigned char*)z)[i]]
10152          || (z[i]==p->colSeparator[0] &&
10153              (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
10154         i = 0;
10155         break;
10156       }
10157     }
10158     if( i==0 ){
10159       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
10160       utf8_printf(out, "%s", zQuoted);
10161       sqlite3_free(zQuoted);
10162     }else{
10163       utf8_printf(out, "%s", z);
10164     }
10165   }
10166   if( bSep ){
10167     utf8_printf(p->out, "%s", p->colSeparator);
10168   }
10169 }
10170 
10171 /*
10172 ** This routine runs when the user presses Ctrl-C
10173 */
10174 static void interrupt_handler(int NotUsed){
10175   UNUSED_PARAMETER(NotUsed);
10176   seenInterrupt++;
10177   if( seenInterrupt>2 ) exit(1);
10178   if( globalDb ) sqlite3_interrupt(globalDb);
10179 }
10180 
10181 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10182 /*
10183 ** This routine runs for console events (e.g. Ctrl-C) on Win32
10184 */
10185 static BOOL WINAPI ConsoleCtrlHandler(
10186   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
10187 ){
10188   if( dwCtrlType==CTRL_C_EVENT ){
10189     interrupt_handler(0);
10190     return TRUE;
10191   }
10192   return FALSE;
10193 }
10194 #endif
10195 
10196 #ifndef SQLITE_OMIT_AUTHORIZATION
10197 /*
10198 ** When the ".auth ON" is set, the following authorizer callback is
10199 ** invoked.  It always returns SQLITE_OK.
10200 */
10201 static int shellAuth(
10202   void *pClientData,
10203   int op,
10204   const char *zA1,
10205   const char *zA2,
10206   const char *zA3,
10207   const char *zA4
10208 ){
10209   ShellState *p = (ShellState*)pClientData;
10210   static const char *azAction[] = { 0,
10211      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
10212      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
10213      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
10214      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
10215      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
10216      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
10217      "PRAGMA",               "READ",                 "SELECT",
10218      "TRANSACTION",          "UPDATE",               "ATTACH",
10219      "DETACH",               "ALTER_TABLE",          "REINDEX",
10220      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
10221      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
10222   };
10223   int i;
10224   const char *az[4];
10225   az[0] = zA1;
10226   az[1] = zA2;
10227   az[2] = zA3;
10228   az[3] = zA4;
10229   utf8_printf(p->out, "authorizer: %s", azAction[op]);
10230   for(i=0; i<4; i++){
10231     raw_printf(p->out, " ");
10232     if( az[i] ){
10233       output_c_string(p->out, az[i]);
10234     }else{
10235       raw_printf(p->out, "NULL");
10236     }
10237   }
10238   raw_printf(p->out, "\n");
10239   return SQLITE_OK;
10240 }
10241 #endif
10242 
10243 /*
10244 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
10245 **
10246 ** This routine converts some CREATE TABLE statements for shadow tables
10247 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
10248 */
10249 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
10250   if( z==0 ) return;
10251   if( zTail==0 ) return;
10252   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
10253     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
10254   }else{
10255     utf8_printf(out, "%s%s", z, zTail);
10256   }
10257 }
10258 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
10259   char c = z[n];
10260   z[n] = 0;
10261   printSchemaLine(out, z, zTail);
10262   z[n] = c;
10263 }
10264 
10265 /*
10266 ** Return true if string z[] has nothing but whitespace and comments to the
10267 ** end of the first line.
10268 */
10269 static int wsToEol(const char *z){
10270   int i;
10271   for(i=0; z[i]; i++){
10272     if( z[i]=='\n' ) return 1;
10273     if( IsSpace(z[i]) ) continue;
10274     if( z[i]=='-' && z[i+1]=='-' ) return 1;
10275     return 0;
10276   }
10277   return 1;
10278 }
10279 
10280 /*
10281 ** Add a new entry to the EXPLAIN QUERY PLAN data
10282 */
10283 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
10284   EQPGraphRow *pNew;
10285   int nText = strlen30(zText);
10286   if( p->autoEQPtest ){
10287     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
10288   }
10289   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
10290   if( pNew==0 ) shell_out_of_memory();
10291   pNew->iEqpId = iEqpId;
10292   pNew->iParentId = p2;
10293   memcpy(pNew->zText, zText, nText+1);
10294   pNew->pNext = 0;
10295   if( p->sGraph.pLast ){
10296     p->sGraph.pLast->pNext = pNew;
10297   }else{
10298     p->sGraph.pRow = pNew;
10299   }
10300   p->sGraph.pLast = pNew;
10301 }
10302 
10303 /*
10304 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
10305 ** in p->sGraph.
10306 */
10307 static void eqp_reset(ShellState *p){
10308   EQPGraphRow *pRow, *pNext;
10309   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
10310     pNext = pRow->pNext;
10311     sqlite3_free(pRow);
10312   }
10313   memset(&p->sGraph, 0, sizeof(p->sGraph));
10314 }
10315 
10316 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
10317 ** pOld, or return the first such line if pOld is NULL
10318 */
10319 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
10320   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
10321   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
10322   return pRow;
10323 }
10324 
10325 /* Render a single level of the graph that has iEqpId as its parent.  Called
10326 ** recursively to render sublevels.
10327 */
10328 static void eqp_render_level(ShellState *p, int iEqpId){
10329   EQPGraphRow *pRow, *pNext;
10330   int n = strlen30(p->sGraph.zPrefix);
10331   char *z;
10332   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
10333     pNext = eqp_next_row(p, iEqpId, pRow);
10334     z = pRow->zText;
10335     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
10336                 pNext ? "|--" : "`--", z);
10337     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
10338       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
10339       eqp_render_level(p, pRow->iEqpId);
10340       p->sGraph.zPrefix[n] = 0;
10341     }
10342   }
10343 }
10344 
10345 /*
10346 ** Display and reset the EXPLAIN QUERY PLAN data
10347 */
10348 static void eqp_render(ShellState *p){
10349   EQPGraphRow *pRow = p->sGraph.pRow;
10350   if( pRow ){
10351     if( pRow->zText[0]=='-' ){
10352       if( pRow->pNext==0 ){
10353         eqp_reset(p);
10354         return;
10355       }
10356       utf8_printf(p->out, "%s\n", pRow->zText+3);
10357       p->sGraph.pRow = pRow->pNext;
10358       sqlite3_free(pRow);
10359     }else{
10360       utf8_printf(p->out, "QUERY PLAN\n");
10361     }
10362     p->sGraph.zPrefix[0] = 0;
10363     eqp_render_level(p, 0);
10364     eqp_reset(p);
10365   }
10366 }
10367 
10368 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10369 /*
10370 ** Progress handler callback.
10371 */
10372 static int progress_handler(void *pClientData) {
10373   ShellState *p = (ShellState*)pClientData;
10374   p->nProgress++;
10375   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
10376     raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
10377     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10378     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
10379     return 1;
10380   }
10381   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
10382     raw_printf(p->out, "Progress %u\n", p->nProgress);
10383   }
10384   return 0;
10385 }
10386 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10387 
10388 /*
10389 ** This is the callback routine that the shell
10390 ** invokes for each row of a query result.
10391 */
10392 static int shell_callback(
10393   void *pArg,
10394   int nArg,        /* Number of result columns */
10395   char **azArg,    /* Text of each result column */
10396   char **azCol,    /* Column names */
10397   int *aiType      /* Column types */
10398 ){
10399   int i;
10400   ShellState *p = (ShellState*)pArg;
10401 
10402   if( azArg==0 ) return 0;
10403   switch( p->cMode ){
10404     case MODE_Line: {
10405       int w = 5;
10406       if( azArg==0 ) break;
10407       for(i=0; i<nArg; i++){
10408         int len = strlen30(azCol[i] ? azCol[i] : "");
10409         if( len>w ) w = len;
10410       }
10411       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
10412       for(i=0; i<nArg; i++){
10413         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
10414                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
10415       }
10416       break;
10417     }
10418     case MODE_Explain:
10419     case MODE_Column: {
10420       static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
10421       const int *colWidth;
10422       int showHdr;
10423       char *rowSep;
10424       int nWidth;
10425       if( p->cMode==MODE_Column ){
10426         colWidth = p->colWidth;
10427         nWidth = ArraySize(p->colWidth);
10428         showHdr = p->showHeader;
10429         rowSep = p->rowSeparator;
10430       }else{
10431         colWidth = aExplainWidths;
10432         nWidth = ArraySize(aExplainWidths);
10433         showHdr = 1;
10434         rowSep = SEP_Row;
10435       }
10436       if( p->cnt++==0 ){
10437         for(i=0; i<nArg; i++){
10438           int w, n;
10439           if( i<nWidth ){
10440             w = colWidth[i];
10441           }else{
10442             w = 0;
10443           }
10444           if( w==0 ){
10445             w = strlenChar(azCol[i] ? azCol[i] : "");
10446             if( w<10 ) w = 10;
10447             n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
10448             if( w<n ) w = n;
10449           }
10450           if( i<ArraySize(p->actualWidth) ){
10451             p->actualWidth[i] = w;
10452           }
10453           if( showHdr ){
10454             utf8_width_print(p->out, w, azCol[i]);
10455             utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
10456           }
10457         }
10458         if( showHdr ){
10459           for(i=0; i<nArg; i++){
10460             int w;
10461             if( i<ArraySize(p->actualWidth) ){
10462                w = p->actualWidth[i];
10463                if( w<0 ) w = -w;
10464             }else{
10465                w = 10;
10466             }
10467             utf8_printf(p->out,"%-*.*s%s",w,w,
10468                    "----------------------------------------------------------"
10469                    "----------------------------------------------------------",
10470                     i==nArg-1 ? rowSep : "  ");
10471           }
10472         }
10473       }
10474       if( azArg==0 ) break;
10475       for(i=0; i<nArg; i++){
10476         int w;
10477         if( i<ArraySize(p->actualWidth) ){
10478            w = p->actualWidth[i];
10479         }else{
10480            w = 10;
10481         }
10482         if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
10483           w = strlenChar(azArg[i]);
10484         }
10485         if( i==1 && p->aiIndent && p->pStmt ){
10486           if( p->iIndent<p->nIndent ){
10487             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
10488           }
10489           p->iIndent++;
10490         }
10491         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
10492         utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
10493       }
10494       break;
10495     }
10496     case MODE_Semi: {   /* .schema and .fullschema output */
10497       printSchemaLine(p->out, azArg[0], ";\n");
10498       break;
10499     }
10500     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
10501       char *z;
10502       int j;
10503       int nParen = 0;
10504       char cEnd = 0;
10505       char c;
10506       int nLine = 0;
10507       assert( nArg==1 );
10508       if( azArg[0]==0 ) break;
10509       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
10510        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
10511       ){
10512         utf8_printf(p->out, "%s;\n", azArg[0]);
10513         break;
10514       }
10515       z = sqlite3_mprintf("%s", azArg[0]);
10516       j = 0;
10517       for(i=0; IsSpace(z[i]); i++){}
10518       for(; (c = z[i])!=0; i++){
10519         if( IsSpace(c) ){
10520           if( z[j-1]=='\r' ) z[j-1] = '\n';
10521           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
10522         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
10523           j--;
10524         }
10525         z[j++] = c;
10526       }
10527       while( j>0 && IsSpace(z[j-1]) ){ j--; }
10528       z[j] = 0;
10529       if( strlen30(z)>=79 ){
10530         for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
10531           if( c==cEnd ){
10532             cEnd = 0;
10533           }else if( c=='"' || c=='\'' || c=='`' ){
10534             cEnd = c;
10535           }else if( c=='[' ){
10536             cEnd = ']';
10537           }else if( c=='-' && z[i+1]=='-' ){
10538             cEnd = '\n';
10539           }else if( c=='(' ){
10540             nParen++;
10541           }else if( c==')' ){
10542             nParen--;
10543             if( nLine>0 && nParen==0 && j>0 ){
10544               printSchemaLineN(p->out, z, j, "\n");
10545               j = 0;
10546             }
10547           }
10548           z[j++] = c;
10549           if( nParen==1 && cEnd==0
10550            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
10551           ){
10552             if( c=='\n' ) j--;
10553             printSchemaLineN(p->out, z, j, "\n  ");
10554             j = 0;
10555             nLine++;
10556             while( IsSpace(z[i+1]) ){ i++; }
10557           }
10558         }
10559         z[j] = 0;
10560       }
10561       printSchemaLine(p->out, z, ";\n");
10562       sqlite3_free(z);
10563       break;
10564     }
10565     case MODE_List: {
10566       if( p->cnt++==0 && p->showHeader ){
10567         for(i=0; i<nArg; i++){
10568           utf8_printf(p->out,"%s%s",azCol[i],
10569                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
10570         }
10571       }
10572       if( azArg==0 ) break;
10573       for(i=0; i<nArg; i++){
10574         char *z = azArg[i];
10575         if( z==0 ) z = p->nullValue;
10576         utf8_printf(p->out, "%s", z);
10577         if( i<nArg-1 ){
10578           utf8_printf(p->out, "%s", p->colSeparator);
10579         }else{
10580           utf8_printf(p->out, "%s", p->rowSeparator);
10581         }
10582       }
10583       break;
10584     }
10585     case MODE_Html: {
10586       if( p->cnt++==0 && p->showHeader ){
10587         raw_printf(p->out,"<TR>");
10588         for(i=0; i<nArg; i++){
10589           raw_printf(p->out,"<TH>");
10590           output_html_string(p->out, azCol[i]);
10591           raw_printf(p->out,"</TH>\n");
10592         }
10593         raw_printf(p->out,"</TR>\n");
10594       }
10595       if( azArg==0 ) break;
10596       raw_printf(p->out,"<TR>");
10597       for(i=0; i<nArg; i++){
10598         raw_printf(p->out,"<TD>");
10599         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10600         raw_printf(p->out,"</TD>\n");
10601       }
10602       raw_printf(p->out,"</TR>\n");
10603       break;
10604     }
10605     case MODE_Tcl: {
10606       if( p->cnt++==0 && p->showHeader ){
10607         for(i=0; i<nArg; i++){
10608           output_c_string(p->out,azCol[i] ? azCol[i] : "");
10609           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10610         }
10611         utf8_printf(p->out, "%s", p->rowSeparator);
10612       }
10613       if( azArg==0 ) break;
10614       for(i=0; i<nArg; i++){
10615         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10616         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10617       }
10618       utf8_printf(p->out, "%s", p->rowSeparator);
10619       break;
10620     }
10621     case MODE_Csv: {
10622       setBinaryMode(p->out, 1);
10623       if( p->cnt++==0 && p->showHeader ){
10624         for(i=0; i<nArg; i++){
10625           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
10626         }
10627         utf8_printf(p->out, "%s", p->rowSeparator);
10628       }
10629       if( nArg>0 ){
10630         for(i=0; i<nArg; i++){
10631           output_csv(p, azArg[i], i<nArg-1);
10632         }
10633         utf8_printf(p->out, "%s", p->rowSeparator);
10634       }
10635       setTextMode(p->out, 1);
10636       break;
10637     }
10638     case MODE_Insert: {
10639       if( azArg==0 ) break;
10640       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
10641       if( p->showHeader ){
10642         raw_printf(p->out,"(");
10643         for(i=0; i<nArg; i++){
10644           if( i>0 ) raw_printf(p->out, ",");
10645           if( quoteChar(azCol[i]) ){
10646             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
10647             utf8_printf(p->out, "%s", z);
10648             sqlite3_free(z);
10649           }else{
10650             raw_printf(p->out, "%s", azCol[i]);
10651           }
10652         }
10653         raw_printf(p->out,")");
10654       }
10655       p->cnt++;
10656       for(i=0; i<nArg; i++){
10657         raw_printf(p->out, i>0 ? "," : " VALUES(");
10658         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10659           utf8_printf(p->out,"NULL");
10660         }else if( aiType && aiType[i]==SQLITE_TEXT ){
10661           if( ShellHasFlag(p, SHFLG_Newlines) ){
10662             output_quoted_string(p->out, azArg[i]);
10663           }else{
10664             output_quoted_escaped_string(p->out, azArg[i]);
10665           }
10666         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10667           utf8_printf(p->out,"%s", azArg[i]);
10668         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10669           char z[50];
10670           double r = sqlite3_column_double(p->pStmt, i);
10671           sqlite3_uint64 ur;
10672           memcpy(&ur,&r,sizeof(r));
10673           if( ur==0x7ff0000000000000LL ){
10674             raw_printf(p->out, "1e999");
10675           }else if( ur==0xfff0000000000000LL ){
10676             raw_printf(p->out, "-1e999");
10677           }else{
10678             sqlite3_snprintf(50,z,"%!.20g", r);
10679             raw_printf(p->out, "%s", z);
10680           }
10681         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10682           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10683           int nBlob = sqlite3_column_bytes(p->pStmt, i);
10684           output_hex_blob(p->out, pBlob, nBlob);
10685         }else if( isNumber(azArg[i], 0) ){
10686           utf8_printf(p->out,"%s", azArg[i]);
10687         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
10688           output_quoted_string(p->out, azArg[i]);
10689         }else{
10690           output_quoted_escaped_string(p->out, azArg[i]);
10691         }
10692       }
10693       raw_printf(p->out,");\n");
10694       break;
10695     }
10696     case MODE_Quote: {
10697       if( azArg==0 ) break;
10698       if( p->cnt==0 && p->showHeader ){
10699         for(i=0; i<nArg; i++){
10700           if( i>0 ) raw_printf(p->out, ",");
10701           output_quoted_string(p->out, azCol[i]);
10702         }
10703         raw_printf(p->out,"\n");
10704       }
10705       p->cnt++;
10706       for(i=0; i<nArg; i++){
10707         if( i>0 ) raw_printf(p->out, ",");
10708         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10709           utf8_printf(p->out,"NULL");
10710         }else if( aiType && aiType[i]==SQLITE_TEXT ){
10711           output_quoted_string(p->out, azArg[i]);
10712         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10713           utf8_printf(p->out,"%s", azArg[i]);
10714         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10715           char z[50];
10716           double r = sqlite3_column_double(p->pStmt, i);
10717           sqlite3_snprintf(50,z,"%!.20g", r);
10718           raw_printf(p->out, "%s", z);
10719         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10720           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10721           int nBlob = sqlite3_column_bytes(p->pStmt, i);
10722           output_hex_blob(p->out, pBlob, nBlob);
10723         }else if( isNumber(azArg[i], 0) ){
10724           utf8_printf(p->out,"%s", azArg[i]);
10725         }else{
10726           output_quoted_string(p->out, azArg[i]);
10727         }
10728       }
10729       raw_printf(p->out,"\n");
10730       break;
10731     }
10732     case MODE_Ascii: {
10733       if( p->cnt++==0 && p->showHeader ){
10734         for(i=0; i<nArg; i++){
10735           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10736           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
10737         }
10738         utf8_printf(p->out, "%s", p->rowSeparator);
10739       }
10740       if( azArg==0 ) break;
10741       for(i=0; i<nArg; i++){
10742         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10743         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
10744       }
10745       utf8_printf(p->out, "%s", p->rowSeparator);
10746       break;
10747     }
10748     case MODE_EQP: {
10749       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
10750       break;
10751     }
10752   }
10753   return 0;
10754 }
10755 
10756 /*
10757 ** This is the callback routine that the SQLite library
10758 ** invokes for each row of a query result.
10759 */
10760 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
10761   /* since we don't have type info, call the shell_callback with a NULL value */
10762   return shell_callback(pArg, nArg, azArg, azCol, NULL);
10763 }
10764 
10765 /*
10766 ** This is the callback routine from sqlite3_exec() that appends all
10767 ** output onto the end of a ShellText object.
10768 */
10769 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
10770   ShellText *p = (ShellText*)pArg;
10771   int i;
10772   UNUSED_PARAMETER(az);
10773   if( azArg==0 ) return 0;
10774   if( p->n ) appendText(p, "|", 0);
10775   for(i=0; i<nArg; i++){
10776     if( i ) appendText(p, ",", 0);
10777     if( azArg[i] ) appendText(p, azArg[i], 0);
10778   }
10779   return 0;
10780 }
10781 
10782 /*
10783 ** Generate an appropriate SELFTEST table in the main database.
10784 */
10785 static void createSelftestTable(ShellState *p){
10786   char *zErrMsg = 0;
10787   sqlite3_exec(p->db,
10788     "SAVEPOINT selftest_init;\n"
10789     "CREATE TABLE IF NOT EXISTS selftest(\n"
10790     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
10791     "  op TEXT,\n"                   /* Operator:  memo run */
10792     "  cmd TEXT,\n"                  /* Command text */
10793     "  ans TEXT\n"                   /* Desired answer */
10794     ");"
10795     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
10796     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
10797     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
10798     "         'memo','Tests generated by --init');\n"
10799     "INSERT INTO [_shell$self]\n"
10800     "  SELECT 'run',\n"
10801     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
10802                                  "FROM sqlite_master ORDER BY 2'',224))',\n"
10803     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
10804                           "FROM sqlite_master ORDER BY 2',224));\n"
10805     "INSERT INTO [_shell$self]\n"
10806     "  SELECT 'run',"
10807     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
10808     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
10809     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
10810     "  FROM (\n"
10811     "    SELECT name FROM sqlite_master\n"
10812     "     WHERE type='table'\n"
10813     "       AND name<>'selftest'\n"
10814     "       AND coalesce(rootpage,0)>0\n"
10815     "  )\n"
10816     " ORDER BY name;\n"
10817     "INSERT INTO [_shell$self]\n"
10818     "  VALUES('run','PRAGMA integrity_check','ok');\n"
10819     "INSERT INTO selftest(tno,op,cmd,ans)"
10820     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
10821     "DROP TABLE [_shell$self];"
10822     ,0,0,&zErrMsg);
10823   if( zErrMsg ){
10824     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
10825     sqlite3_free(zErrMsg);
10826   }
10827   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
10828 }
10829 
10830 
10831 /*
10832 ** Set the destination table field of the ShellState structure to
10833 ** the name of the table given.  Escape any quote characters in the
10834 ** table name.
10835 */
10836 static void set_table_name(ShellState *p, const char *zName){
10837   int i, n;
10838   char cQuote;
10839   char *z;
10840 
10841   if( p->zDestTable ){
10842     free(p->zDestTable);
10843     p->zDestTable = 0;
10844   }
10845   if( zName==0 ) return;
10846   cQuote = quoteChar(zName);
10847   n = strlen30(zName);
10848   if( cQuote ) n += n+2;
10849   z = p->zDestTable = malloc( n+1 );
10850   if( z==0 ) shell_out_of_memory();
10851   n = 0;
10852   if( cQuote ) z[n++] = cQuote;
10853   for(i=0; zName[i]; i++){
10854     z[n++] = zName[i];
10855     if( zName[i]==cQuote ) z[n++] = cQuote;
10856   }
10857   if( cQuote ) z[n++] = cQuote;
10858   z[n] = 0;
10859 }
10860 
10861 
10862 /*
10863 ** Execute a query statement that will generate SQL output.  Print
10864 ** the result columns, comma-separated, on a line and then add a
10865 ** semicolon terminator to the end of that line.
10866 **
10867 ** If the number of columns is 1 and that column contains text "--"
10868 ** then write the semicolon on a separate line.  That way, if a
10869 ** "--" comment occurs at the end of the statement, the comment
10870 ** won't consume the semicolon terminator.
10871 */
10872 static int run_table_dump_query(
10873   ShellState *p,           /* Query context */
10874   const char *zSelect,     /* SELECT statement to extract content */
10875   const char *zFirstRow    /* Print before first row, if not NULL */
10876 ){
10877   sqlite3_stmt *pSelect;
10878   int rc;
10879   int nResult;
10880   int i;
10881   const char *z;
10882   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
10883   if( rc!=SQLITE_OK || !pSelect ){
10884     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
10885                 sqlite3_errmsg(p->db));
10886     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
10887     return rc;
10888   }
10889   rc = sqlite3_step(pSelect);
10890   nResult = sqlite3_column_count(pSelect);
10891   while( rc==SQLITE_ROW ){
10892     if( zFirstRow ){
10893       utf8_printf(p->out, "%s", zFirstRow);
10894       zFirstRow = 0;
10895     }
10896     z = (const char*)sqlite3_column_text(pSelect, 0);
10897     utf8_printf(p->out, "%s", z);
10898     for(i=1; i<nResult; i++){
10899       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
10900     }
10901     if( z==0 ) z = "";
10902     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
10903     if( z[0] ){
10904       raw_printf(p->out, "\n;\n");
10905     }else{
10906       raw_printf(p->out, ";\n");
10907     }
10908     rc = sqlite3_step(pSelect);
10909   }
10910   rc = sqlite3_finalize(pSelect);
10911   if( rc!=SQLITE_OK ){
10912     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
10913                 sqlite3_errmsg(p->db));
10914     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
10915   }
10916   return rc;
10917 }
10918 
10919 /*
10920 ** Allocate space and save off current error string.
10921 */
10922 static char *save_err_msg(
10923   sqlite3 *db            /* Database to query */
10924 ){
10925   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
10926   char *zErrMsg = sqlite3_malloc64(nErrMsg);
10927   if( zErrMsg ){
10928     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
10929   }
10930   return zErrMsg;
10931 }
10932 
10933 #ifdef __linux__
10934 /*
10935 ** Attempt to display I/O stats on Linux using /proc/PID/io
10936 */
10937 static void displayLinuxIoStats(FILE *out){
10938   FILE *in;
10939   char z[200];
10940   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
10941   in = fopen(z, "rb");
10942   if( in==0 ) return;
10943   while( fgets(z, sizeof(z), in)!=0 ){
10944     static const struct {
10945       const char *zPattern;
10946       const char *zDesc;
10947     } aTrans[] = {
10948       { "rchar: ",                  "Bytes received by read():" },
10949       { "wchar: ",                  "Bytes sent to write():"    },
10950       { "syscr: ",                  "Read() system calls:"      },
10951       { "syscw: ",                  "Write() system calls:"     },
10952       { "read_bytes: ",             "Bytes read from storage:"  },
10953       { "write_bytes: ",            "Bytes written to storage:" },
10954       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
10955     };
10956     int i;
10957     for(i=0; i<ArraySize(aTrans); i++){
10958       int n = strlen30(aTrans[i].zPattern);
10959       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
10960         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
10961         break;
10962       }
10963     }
10964   }
10965   fclose(in);
10966 }
10967 #endif
10968 
10969 /*
10970 ** Display a single line of status using 64-bit values.
10971 */
10972 static void displayStatLine(
10973   ShellState *p,            /* The shell context */
10974   char *zLabel,             /* Label for this one line */
10975   char *zFormat,            /* Format for the result */
10976   int iStatusCtrl,          /* Which status to display */
10977   int bReset                /* True to reset the stats */
10978 ){
10979   sqlite3_int64 iCur = -1;
10980   sqlite3_int64 iHiwtr = -1;
10981   int i, nPercent;
10982   char zLine[200];
10983   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
10984   for(i=0, nPercent=0; zFormat[i]; i++){
10985     if( zFormat[i]=='%' ) nPercent++;
10986   }
10987   if( nPercent>1 ){
10988     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
10989   }else{
10990     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
10991   }
10992   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
10993 }
10994 
10995 /*
10996 ** Display memory stats.
10997 */
10998 static int display_stats(
10999   sqlite3 *db,                /* Database to query */
11000   ShellState *pArg,           /* Pointer to ShellState */
11001   int bReset                  /* True to reset the stats */
11002 ){
11003   int iCur;
11004   int iHiwtr;
11005   FILE *out;
11006   if( pArg==0 || pArg->out==0 ) return 0;
11007   out = pArg->out;
11008 
11009   if( pArg->pStmt && (pArg->statsOn & 2) ){
11010     int nCol, i, x;
11011     sqlite3_stmt *pStmt = pArg->pStmt;
11012     char z[100];
11013     nCol = sqlite3_column_count(pStmt);
11014     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
11015     for(i=0; i<nCol; i++){
11016       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
11017       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
11018 #ifndef SQLITE_OMIT_DECLTYPE
11019       sqlite3_snprintf(30, z+x, "declared type:");
11020       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
11021 #endif
11022 #ifdef SQLITE_ENABLE_COLUMN_METADATA
11023       sqlite3_snprintf(30, z+x, "database name:");
11024       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
11025       sqlite3_snprintf(30, z+x, "table name:");
11026       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
11027       sqlite3_snprintf(30, z+x, "origin name:");
11028       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
11029 #endif
11030     }
11031   }
11032 
11033   displayStatLine(pArg, "Memory Used:",
11034      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
11035   displayStatLine(pArg, "Number of Outstanding Allocations:",
11036      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
11037   if( pArg->shellFlgs & SHFLG_Pagecache ){
11038     displayStatLine(pArg, "Number of Pcache Pages Used:",
11039        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
11040   }
11041   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
11042      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
11043   displayStatLine(pArg, "Largest Allocation:",
11044      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
11045   displayStatLine(pArg, "Largest Pcache Allocation:",
11046      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
11047 #ifdef YYTRACKMAXSTACKDEPTH
11048   displayStatLine(pArg, "Deepest Parser Stack:",
11049      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
11050 #endif
11051 
11052   if( db ){
11053     if( pArg->shellFlgs & SHFLG_Lookaside ){
11054       iHiwtr = iCur = -1;
11055       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
11056                         &iCur, &iHiwtr, bReset);
11057       raw_printf(pArg->out,
11058               "Lookaside Slots Used:                %d (max %d)\n",
11059               iCur, iHiwtr);
11060       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
11061                         &iCur, &iHiwtr, bReset);
11062       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
11063               iHiwtr);
11064       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
11065                         &iCur, &iHiwtr, bReset);
11066       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
11067               iHiwtr);
11068       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
11069                         &iCur, &iHiwtr, bReset);
11070       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
11071               iHiwtr);
11072     }
11073     iHiwtr = iCur = -1;
11074     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
11075     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
11076             iCur);
11077     iHiwtr = iCur = -1;
11078     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
11079     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
11080     iHiwtr = iCur = -1;
11081     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
11082     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
11083     iHiwtr = iCur = -1;
11084     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
11085     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
11086     iHiwtr = iCur = -1;
11087     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
11088     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
11089     iHiwtr = iCur = -1;
11090     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
11091     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
11092             iCur);
11093     iHiwtr = iCur = -1;
11094     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
11095     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
11096             iCur);
11097   }
11098 
11099   if( pArg->pStmt ){
11100     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
11101                                bReset);
11102     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
11103     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
11104     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
11105     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
11106     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
11107     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
11108     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
11109     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
11110     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
11111     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
11112     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
11113     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
11114     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
11115   }
11116 
11117 #ifdef __linux__
11118   displayLinuxIoStats(pArg->out);
11119 #endif
11120 
11121   /* Do not remove this machine readable comment: extra-stats-output-here */
11122 
11123   return 0;
11124 }
11125 
11126 /*
11127 ** Display scan stats.
11128 */
11129 static void display_scanstats(
11130   sqlite3 *db,                    /* Database to query */
11131   ShellState *pArg                /* Pointer to ShellState */
11132 ){
11133 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
11134   UNUSED_PARAMETER(db);
11135   UNUSED_PARAMETER(pArg);
11136 #else
11137   int i, k, n, mx;
11138   raw_printf(pArg->out, "-------- scanstats --------\n");
11139   mx = 0;
11140   for(k=0; k<=mx; k++){
11141     double rEstLoop = 1.0;
11142     for(i=n=0; 1; i++){
11143       sqlite3_stmt *p = pArg->pStmt;
11144       sqlite3_int64 nLoop, nVisit;
11145       double rEst;
11146       int iSid;
11147       const char *zExplain;
11148       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
11149         break;
11150       }
11151       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
11152       if( iSid>mx ) mx = iSid;
11153       if( iSid!=k ) continue;
11154       if( n==0 ){
11155         rEstLoop = (double)nLoop;
11156         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
11157       }
11158       n++;
11159       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
11160       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
11161       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
11162       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
11163       rEstLoop *= rEst;
11164       raw_printf(pArg->out,
11165           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
11166           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
11167       );
11168     }
11169   }
11170   raw_printf(pArg->out, "---------------------------\n");
11171 #endif
11172 }
11173 
11174 /*
11175 ** Parameter azArray points to a zero-terminated array of strings. zStr
11176 ** points to a single nul-terminated string. Return non-zero if zStr
11177 ** is equal, according to strcmp(), to any of the strings in the array.
11178 ** Otherwise, return zero.
11179 */
11180 static int str_in_array(const char *zStr, const char **azArray){
11181   int i;
11182   for(i=0; azArray[i]; i++){
11183     if( 0==strcmp(zStr, azArray[i]) ) return 1;
11184   }
11185   return 0;
11186 }
11187 
11188 /*
11189 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
11190 ** and populate the ShellState.aiIndent[] array with the number of
11191 ** spaces each opcode should be indented before it is output.
11192 **
11193 ** The indenting rules are:
11194 **
11195 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
11196 **       all opcodes that occur between the p2 jump destination and the opcode
11197 **       itself by 2 spaces.
11198 **
11199 **     * For each "Goto", if the jump destination is earlier in the program
11200 **       and ends on one of:
11201 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
11202 **       or if the P1 parameter is one instead of zero,
11203 **       then indent all opcodes between the earlier instruction
11204 **       and "Goto" by 2 spaces.
11205 */
11206 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
11207   const char *zSql;               /* The text of the SQL statement */
11208   const char *z;                  /* Used to check if this is an EXPLAIN */
11209   int *abYield = 0;               /* True if op is an OP_Yield */
11210   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
11211   int iOp;                        /* Index of operation in p->aiIndent[] */
11212 
11213   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
11214   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
11215                             "Rewind", 0 };
11216   const char *azGoto[] = { "Goto", 0 };
11217 
11218   /* Try to figure out if this is really an EXPLAIN statement. If this
11219   ** cannot be verified, return early.  */
11220   if( sqlite3_column_count(pSql)!=8 ){
11221     p->cMode = p->mode;
11222     return;
11223   }
11224   zSql = sqlite3_sql(pSql);
11225   if( zSql==0 ) return;
11226   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
11227   if( sqlite3_strnicmp(z, "explain", 7) ){
11228     p->cMode = p->mode;
11229     return;
11230   }
11231 
11232   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
11233     int i;
11234     int iAddr = sqlite3_column_int(pSql, 0);
11235     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
11236 
11237     /* Set p2 to the P2 field of the current opcode. Then, assuming that
11238     ** p2 is an instruction address, set variable p2op to the index of that
11239     ** instruction in the aiIndent[] array. p2 and p2op may be different if
11240     ** the current instruction is part of a sub-program generated by an
11241     ** SQL trigger or foreign key.  */
11242     int p2 = sqlite3_column_int(pSql, 3);
11243     int p2op = (p2 + (iOp-iAddr));
11244 
11245     /* Grow the p->aiIndent array as required */
11246     if( iOp>=nAlloc ){
11247       if( iOp==0 ){
11248         /* Do further verfication that this is explain output.  Abort if
11249         ** it is not */
11250         static const char *explainCols[] = {
11251            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
11252         int jj;
11253         for(jj=0; jj<ArraySize(explainCols); jj++){
11254           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
11255             p->cMode = p->mode;
11256             sqlite3_reset(pSql);
11257             return;
11258           }
11259         }
11260       }
11261       nAlloc += 100;
11262       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
11263       if( p->aiIndent==0 ) shell_out_of_memory();
11264       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
11265       if( abYield==0 ) shell_out_of_memory();
11266     }
11267     abYield[iOp] = str_in_array(zOp, azYield);
11268     p->aiIndent[iOp] = 0;
11269     p->nIndent = iOp+1;
11270 
11271     if( str_in_array(zOp, azNext) ){
11272       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11273     }
11274     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
11275      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
11276     ){
11277       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11278     }
11279   }
11280 
11281   p->iIndent = 0;
11282   sqlite3_free(abYield);
11283   sqlite3_reset(pSql);
11284 }
11285 
11286 /*
11287 ** Free the array allocated by explain_data_prepare().
11288 */
11289 static void explain_data_delete(ShellState *p){
11290   sqlite3_free(p->aiIndent);
11291   p->aiIndent = 0;
11292   p->nIndent = 0;
11293   p->iIndent = 0;
11294 }
11295 
11296 /*
11297 ** Disable and restore .wheretrace and .selecttrace settings.
11298 */
11299 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11300 extern int sqlite3SelectTrace;
11301 static int savedSelectTrace;
11302 #endif
11303 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11304 extern int sqlite3WhereTrace;
11305 static int savedWhereTrace;
11306 #endif
11307 static void disable_debug_trace_modes(void){
11308 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11309   savedSelectTrace = sqlite3SelectTrace;
11310   sqlite3SelectTrace = 0;
11311 #endif
11312 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11313   savedWhereTrace = sqlite3WhereTrace;
11314   sqlite3WhereTrace = 0;
11315 #endif
11316 }
11317 static void restore_debug_trace_modes(void){
11318 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11319   sqlite3SelectTrace = savedSelectTrace;
11320 #endif
11321 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11322   sqlite3WhereTrace = savedWhereTrace;
11323 #endif
11324 }
11325 
11326 /* Create the TEMP table used to store parameter bindings */
11327 static void bind_table_init(ShellState *p){
11328   int wrSchema = 0;
11329   int defensiveMode = 0;
11330   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
11331   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
11332   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
11333   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
11334   sqlite3_exec(p->db,
11335     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
11336     "  key TEXT PRIMARY KEY,\n"
11337     "  value ANY\n"
11338     ") WITHOUT ROWID;",
11339     0, 0, 0);
11340   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
11341   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
11342 }
11343 
11344 /*
11345 ** Bind parameters on a prepared statement.
11346 **
11347 ** Parameter bindings are taken from a TEMP table of the form:
11348 **
11349 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
11350 **    WITHOUT ROWID;
11351 **
11352 ** No bindings occur if this table does not exist.  The special character '$'
11353 ** is included in the table name to help prevent collisions with actual tables.
11354 ** The table must be in the TEMP schema.
11355 */
11356 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
11357   int nVar;
11358   int i;
11359   int rc;
11360   sqlite3_stmt *pQ = 0;
11361 
11362   nVar = sqlite3_bind_parameter_count(pStmt);
11363   if( nVar==0 ) return;  /* Nothing to do */
11364   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
11365                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
11366     return; /* Parameter table does not exist */
11367   }
11368   rc = sqlite3_prepare_v2(pArg->db,
11369           "SELECT value FROM temp.sqlite_parameters"
11370           " WHERE key=?1", -1, &pQ, 0);
11371   if( rc || pQ==0 ) return;
11372   for(i=1; i<=nVar; i++){
11373     char zNum[30];
11374     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
11375     if( zVar==0 ){
11376       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
11377       zVar = zNum;
11378     }
11379     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
11380     if( sqlite3_step(pQ)==SQLITE_ROW ){
11381       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
11382     }else{
11383       sqlite3_bind_null(pStmt, i);
11384     }
11385     sqlite3_reset(pQ);
11386   }
11387   sqlite3_finalize(pQ);
11388 }
11389 
11390 /*
11391 ** Run a prepared statement
11392 */
11393 static void exec_prepared_stmt(
11394   ShellState *pArg,                                /* Pointer to ShellState */
11395   sqlite3_stmt *pStmt                              /* Statment to run */
11396 ){
11397   int rc;
11398 
11399   /* perform the first step.  this will tell us if we
11400   ** have a result set or not and how wide it is.
11401   */
11402   rc = sqlite3_step(pStmt);
11403   /* if we have a result set... */
11404   if( SQLITE_ROW == rc ){
11405     /* allocate space for col name ptr, value ptr, and type */
11406     int nCol = sqlite3_column_count(pStmt);
11407     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
11408     if( !pData ){
11409       rc = SQLITE_NOMEM;
11410     }else{
11411       char **azCols = (char **)pData;      /* Names of result columns */
11412       char **azVals = &azCols[nCol];       /* Results */
11413       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
11414       int i, x;
11415       assert(sizeof(int) <= sizeof(char *));
11416       /* save off ptrs to column names */
11417       for(i=0; i<nCol; i++){
11418         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
11419       }
11420       do{
11421         /* extract the data and data types */
11422         for(i=0; i<nCol; i++){
11423           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
11424           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
11425             azVals[i] = "";
11426           }else{
11427             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
11428           }
11429           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
11430             rc = SQLITE_NOMEM;
11431             break; /* from for */
11432           }
11433         } /* end for */
11434 
11435         /* if data and types extracted successfully... */
11436         if( SQLITE_ROW == rc ){
11437           /* call the supplied callback with the result row data */
11438           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
11439             rc = SQLITE_ABORT;
11440           }else{
11441             rc = sqlite3_step(pStmt);
11442           }
11443         }
11444       } while( SQLITE_ROW == rc );
11445       sqlite3_free(pData);
11446     }
11447   }
11448 }
11449 
11450 #ifndef SQLITE_OMIT_VIRTUALTABLE
11451 /*
11452 ** This function is called to process SQL if the previous shell command
11453 ** was ".expert". It passes the SQL in the second argument directly to
11454 ** the sqlite3expert object.
11455 **
11456 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11457 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11458 ** an English language error message. It is the responsibility of the
11459 ** caller to eventually free this buffer using sqlite3_free().
11460 */
11461 static int expertHandleSQL(
11462   ShellState *pState,
11463   const char *zSql,
11464   char **pzErr
11465 ){
11466   assert( pState->expert.pExpert );
11467   assert( pzErr==0 || *pzErr==0 );
11468   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
11469 }
11470 
11471 /*
11472 ** This function is called either to silently clean up the object
11473 ** created by the ".expert" command (if bCancel==1), or to generate a
11474 ** report from it and then clean it up (if bCancel==0).
11475 **
11476 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11477 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11478 ** an English language error message. It is the responsibility of the
11479 ** caller to eventually free this buffer using sqlite3_free().
11480 */
11481 static int expertFinish(
11482   ShellState *pState,
11483   int bCancel,
11484   char **pzErr
11485 ){
11486   int rc = SQLITE_OK;
11487   sqlite3expert *p = pState->expert.pExpert;
11488   assert( p );
11489   assert( bCancel || pzErr==0 || *pzErr==0 );
11490   if( bCancel==0 ){
11491     FILE *out = pState->out;
11492     int bVerbose = pState->expert.bVerbose;
11493 
11494     rc = sqlite3_expert_analyze(p, pzErr);
11495     if( rc==SQLITE_OK ){
11496       int nQuery = sqlite3_expert_count(p);
11497       int i;
11498 
11499       if( bVerbose ){
11500         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
11501         raw_printf(out, "-- Candidates -----------------------------\n");
11502         raw_printf(out, "%s\n", zCand);
11503       }
11504       for(i=0; i<nQuery; i++){
11505         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
11506         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
11507         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
11508         if( zIdx==0 ) zIdx = "(no new indexes)\n";
11509         if( bVerbose ){
11510           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
11511           raw_printf(out, "%s\n\n", zSql);
11512         }
11513         raw_printf(out, "%s\n", zIdx);
11514         raw_printf(out, "%s\n", zEQP);
11515       }
11516     }
11517   }
11518   sqlite3_expert_destroy(p);
11519   pState->expert.pExpert = 0;
11520   return rc;
11521 }
11522 
11523 /*
11524 ** Implementation of ".expert" dot command.
11525 */
11526 static int expertDotCommand(
11527   ShellState *pState,             /* Current shell tool state */
11528   char **azArg,                   /* Array of arguments passed to dot command */
11529   int nArg                        /* Number of entries in azArg[] */
11530 ){
11531   int rc = SQLITE_OK;
11532   char *zErr = 0;
11533   int i;
11534   int iSample = 0;
11535 
11536   assert( pState->expert.pExpert==0 );
11537   memset(&pState->expert, 0, sizeof(ExpertInfo));
11538 
11539   for(i=1; rc==SQLITE_OK && i<nArg; i++){
11540     char *z = azArg[i];
11541     int n;
11542     if( z[0]=='-' && z[1]=='-' ) z++;
11543     n = strlen30(z);
11544     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
11545       pState->expert.bVerbose = 1;
11546     }
11547     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
11548       if( i==(nArg-1) ){
11549         raw_printf(stderr, "option requires an argument: %s\n", z);
11550         rc = SQLITE_ERROR;
11551       }else{
11552         iSample = (int)integerValue(azArg[++i]);
11553         if( iSample<0 || iSample>100 ){
11554           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
11555           rc = SQLITE_ERROR;
11556         }
11557       }
11558     }
11559     else{
11560       raw_printf(stderr, "unknown option: %s\n", z);
11561       rc = SQLITE_ERROR;
11562     }
11563   }
11564 
11565   if( rc==SQLITE_OK ){
11566     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
11567     if( pState->expert.pExpert==0 ){
11568       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
11569       rc = SQLITE_ERROR;
11570     }else{
11571       sqlite3_expert_config(
11572           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
11573       );
11574     }
11575   }
11576 
11577   return rc;
11578 }
11579 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11580 
11581 /*
11582 ** Execute a statement or set of statements.  Print
11583 ** any result rows/columns depending on the current mode
11584 ** set via the supplied callback.
11585 **
11586 ** This is very similar to SQLite's built-in sqlite3_exec()
11587 ** function except it takes a slightly different callback
11588 ** and callback data argument.
11589 */
11590 static int shell_exec(
11591   ShellState *pArg,                         /* Pointer to ShellState */
11592   const char *zSql,                         /* SQL to be evaluated */
11593   char **pzErrMsg                           /* Error msg written here */
11594 ){
11595   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
11596   int rc = SQLITE_OK;             /* Return Code */
11597   int rc2;
11598   const char *zLeftover;          /* Tail of unprocessed SQL */
11599   sqlite3 *db = pArg->db;
11600 
11601   if( pzErrMsg ){
11602     *pzErrMsg = NULL;
11603   }
11604 
11605 #ifndef SQLITE_OMIT_VIRTUALTABLE
11606   if( pArg->expert.pExpert ){
11607     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
11608     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
11609   }
11610 #endif
11611 
11612   while( zSql[0] && (SQLITE_OK == rc) ){
11613     static const char *zStmtSql;
11614     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
11615     if( SQLITE_OK != rc ){
11616       if( pzErrMsg ){
11617         *pzErrMsg = save_err_msg(db);
11618       }
11619     }else{
11620       if( !pStmt ){
11621         /* this happens for a comment or white-space */
11622         zSql = zLeftover;
11623         while( IsSpace(zSql[0]) ) zSql++;
11624         continue;
11625       }
11626       zStmtSql = sqlite3_sql(pStmt);
11627       if( zStmtSql==0 ) zStmtSql = "";
11628       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
11629 
11630       /* save off the prepared statment handle and reset row count */
11631       if( pArg ){
11632         pArg->pStmt = pStmt;
11633         pArg->cnt = 0;
11634       }
11635 
11636       /* echo the sql statement if echo on */
11637       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
11638         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
11639       }
11640 
11641       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
11642       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
11643         sqlite3_stmt *pExplain;
11644         char *zEQP;
11645         int triggerEQP = 0;
11646         disable_debug_trace_modes();
11647         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
11648         if( pArg->autoEQP>=AUTOEQP_trigger ){
11649           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
11650         }
11651         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
11652         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11653         if( rc==SQLITE_OK ){
11654           while( sqlite3_step(pExplain)==SQLITE_ROW ){
11655             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
11656             int iEqpId = sqlite3_column_int(pExplain, 0);
11657             int iParentId = sqlite3_column_int(pExplain, 1);
11658             if( zEQPLine[0]=='-' ) eqp_render(pArg);
11659             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
11660           }
11661           eqp_render(pArg);
11662         }
11663         sqlite3_finalize(pExplain);
11664         sqlite3_free(zEQP);
11665         if( pArg->autoEQP>=AUTOEQP_full ){
11666           /* Also do an EXPLAIN for ".eqp full" mode */
11667           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
11668           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11669           if( rc==SQLITE_OK ){
11670             pArg->cMode = MODE_Explain;
11671             explain_data_prepare(pArg, pExplain);
11672             exec_prepared_stmt(pArg, pExplain);
11673             explain_data_delete(pArg);
11674           }
11675           sqlite3_finalize(pExplain);
11676           sqlite3_free(zEQP);
11677         }
11678         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
11679           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
11680           /* Reprepare pStmt before reactiving trace modes */
11681           sqlite3_finalize(pStmt);
11682           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
11683           if( pArg ) pArg->pStmt = pStmt;
11684         }
11685         restore_debug_trace_modes();
11686       }
11687 
11688       if( pArg ){
11689         pArg->cMode = pArg->mode;
11690         if( pArg->autoExplain ){
11691           if( sqlite3_stmt_isexplain(pStmt)==1 ){
11692             pArg->cMode = MODE_Explain;
11693           }
11694           if( sqlite3_stmt_isexplain(pStmt)==2 ){
11695             pArg->cMode = MODE_EQP;
11696           }
11697         }
11698 
11699         /* If the shell is currently in ".explain" mode, gather the extra
11700         ** data required to add indents to the output.*/
11701         if( pArg->cMode==MODE_Explain ){
11702           explain_data_prepare(pArg, pStmt);
11703         }
11704       }
11705 
11706       bind_prepared_stmt(pArg, pStmt);
11707       exec_prepared_stmt(pArg, pStmt);
11708       explain_data_delete(pArg);
11709       eqp_render(pArg);
11710 
11711       /* print usage stats if stats on */
11712       if( pArg && pArg->statsOn ){
11713         display_stats(db, pArg, 0);
11714       }
11715 
11716       /* print loop-counters if required */
11717       if( pArg && pArg->scanstatsOn ){
11718         display_scanstats(db, pArg);
11719       }
11720 
11721       /* Finalize the statement just executed. If this fails, save a
11722       ** copy of the error message. Otherwise, set zSql to point to the
11723       ** next statement to execute. */
11724       rc2 = sqlite3_finalize(pStmt);
11725       if( rc!=SQLITE_NOMEM ) rc = rc2;
11726       if( rc==SQLITE_OK ){
11727         zSql = zLeftover;
11728         while( IsSpace(zSql[0]) ) zSql++;
11729       }else if( pzErrMsg ){
11730         *pzErrMsg = save_err_msg(db);
11731       }
11732 
11733       /* clear saved stmt handle */
11734       if( pArg ){
11735         pArg->pStmt = NULL;
11736       }
11737     }
11738   } /* end while */
11739 
11740   return rc;
11741 }
11742 
11743 /*
11744 ** Release memory previously allocated by tableColumnList().
11745 */
11746 static void freeColumnList(char **azCol){
11747   int i;
11748   for(i=1; azCol[i]; i++){
11749     sqlite3_free(azCol[i]);
11750   }
11751   /* azCol[0] is a static string */
11752   sqlite3_free(azCol);
11753 }
11754 
11755 /*
11756 ** Return a list of pointers to strings which are the names of all
11757 ** columns in table zTab.   The memory to hold the names is dynamically
11758 ** allocated and must be released by the caller using a subsequent call
11759 ** to freeColumnList().
11760 **
11761 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
11762 ** value that needs to be preserved, then azCol[0] is filled in with the
11763 ** name of the rowid column.
11764 **
11765 ** The first regular column in the table is azCol[1].  The list is terminated
11766 ** by an entry with azCol[i]==0.
11767 */
11768 static char **tableColumnList(ShellState *p, const char *zTab){
11769   char **azCol = 0;
11770   sqlite3_stmt *pStmt;
11771   char *zSql;
11772   int nCol = 0;
11773   int nAlloc = 0;
11774   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
11775   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
11776   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
11777   int rc;
11778 
11779   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
11780   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11781   sqlite3_free(zSql);
11782   if( rc ) return 0;
11783   while( sqlite3_step(pStmt)==SQLITE_ROW ){
11784     if( nCol>=nAlloc-2 ){
11785       nAlloc = nAlloc*2 + nCol + 10;
11786       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
11787       if( azCol==0 ) shell_out_of_memory();
11788     }
11789     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
11790     if( sqlite3_column_int(pStmt, 5) ){
11791       nPK++;
11792       if( nPK==1
11793        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
11794                           "INTEGER")==0
11795       ){
11796         isIPK = 1;
11797       }else{
11798         isIPK = 0;
11799       }
11800     }
11801   }
11802   sqlite3_finalize(pStmt);
11803   if( azCol==0 ) return 0;
11804   azCol[0] = 0;
11805   azCol[nCol+1] = 0;
11806 
11807   /* The decision of whether or not a rowid really needs to be preserved
11808   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
11809   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
11810   ** rowids on tables where the rowid is inaccessible because there are other
11811   ** columns in the table named "rowid", "_rowid_", and "oid".
11812   */
11813   if( preserveRowid && isIPK ){
11814     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
11815     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
11816     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
11817     ** ROWID aliases.  To distinguish these cases, check to see if
11818     ** there is a "pk" entry in "PRAGMA index_list".  There will be
11819     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
11820     */
11821     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
11822                            " WHERE origin='pk'", zTab);
11823     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11824     sqlite3_free(zSql);
11825     if( rc ){
11826       freeColumnList(azCol);
11827       return 0;
11828     }
11829     rc = sqlite3_step(pStmt);
11830     sqlite3_finalize(pStmt);
11831     preserveRowid = rc==SQLITE_ROW;
11832   }
11833   if( preserveRowid ){
11834     /* Only preserve the rowid if we can find a name to use for the
11835     ** rowid */
11836     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
11837     int i, j;
11838     for(j=0; j<3; j++){
11839       for(i=1; i<=nCol; i++){
11840         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
11841       }
11842       if( i>nCol ){
11843         /* At this point, we know that azRowid[j] is not the name of any
11844         ** ordinary column in the table.  Verify that azRowid[j] is a valid
11845         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
11846         ** tables will fail this last check */
11847         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
11848         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
11849         break;
11850       }
11851     }
11852   }
11853   return azCol;
11854 }
11855 
11856 /*
11857 ** Toggle the reverse_unordered_selects setting.
11858 */
11859 static void toggleSelectOrder(sqlite3 *db){
11860   sqlite3_stmt *pStmt = 0;
11861   int iSetting = 0;
11862   char zStmt[100];
11863   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
11864   if( sqlite3_step(pStmt)==SQLITE_ROW ){
11865     iSetting = sqlite3_column_int(pStmt, 0);
11866   }
11867   sqlite3_finalize(pStmt);
11868   sqlite3_snprintf(sizeof(zStmt), zStmt,
11869        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
11870   sqlite3_exec(db, zStmt, 0, 0, 0);
11871 }
11872 
11873 /*
11874 ** This is a different callback routine used for dumping the database.
11875 ** Each row received by this callback consists of a table name,
11876 ** the table type ("index" or "table") and SQL to create the table.
11877 ** This routine should print text sufficient to recreate the table.
11878 */
11879 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
11880   int rc;
11881   const char *zTable;
11882   const char *zType;
11883   const char *zSql;
11884   ShellState *p = (ShellState *)pArg;
11885 
11886   UNUSED_PARAMETER(azNotUsed);
11887   if( nArg!=3 || azArg==0 ) return 0;
11888   zTable = azArg[0];
11889   zType = azArg[1];
11890   zSql = azArg[2];
11891 
11892   if( strcmp(zTable, "sqlite_sequence")==0 ){
11893     raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
11894   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
11895     raw_printf(p->out, "ANALYZE sqlite_master;\n");
11896   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
11897     return 0;
11898   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
11899     char *zIns;
11900     if( !p->writableSchema ){
11901       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
11902       p->writableSchema = 1;
11903     }
11904     zIns = sqlite3_mprintf(
11905        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
11906        "VALUES('table','%q','%q',0,'%q');",
11907        zTable, zTable, zSql);
11908     utf8_printf(p->out, "%s\n", zIns);
11909     sqlite3_free(zIns);
11910     return 0;
11911   }else{
11912     printSchemaLine(p->out, zSql, ";\n");
11913   }
11914 
11915   if( strcmp(zType, "table")==0 ){
11916     ShellText sSelect;
11917     ShellText sTable;
11918     char **azCol;
11919     int i;
11920     char *savedDestTable;
11921     int savedMode;
11922 
11923     azCol = tableColumnList(p, zTable);
11924     if( azCol==0 ){
11925       p->nErr++;
11926       return 0;
11927     }
11928 
11929     /* Always quote the table name, even if it appears to be pure ascii,
11930     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
11931     initText(&sTable);
11932     appendText(&sTable, zTable, quoteChar(zTable));
11933     /* If preserving the rowid, add a column list after the table name.
11934     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
11935     ** instead of the usual "INSERT INTO tab VALUES(...)".
11936     */
11937     if( azCol[0] ){
11938       appendText(&sTable, "(", 0);
11939       appendText(&sTable, azCol[0], 0);
11940       for(i=1; azCol[i]; i++){
11941         appendText(&sTable, ",", 0);
11942         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
11943       }
11944       appendText(&sTable, ")", 0);
11945     }
11946 
11947     /* Build an appropriate SELECT statement */
11948     initText(&sSelect);
11949     appendText(&sSelect, "SELECT ", 0);
11950     if( azCol[0] ){
11951       appendText(&sSelect, azCol[0], 0);
11952       appendText(&sSelect, ",", 0);
11953     }
11954     for(i=1; azCol[i]; i++){
11955       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
11956       if( azCol[i+1] ){
11957         appendText(&sSelect, ",", 0);
11958       }
11959     }
11960     freeColumnList(azCol);
11961     appendText(&sSelect, " FROM ", 0);
11962     appendText(&sSelect, zTable, quoteChar(zTable));
11963 
11964     savedDestTable = p->zDestTable;
11965     savedMode = p->mode;
11966     p->zDestTable = sTable.z;
11967     p->mode = p->cMode = MODE_Insert;
11968     rc = shell_exec(p, sSelect.z, 0);
11969     if( (rc&0xff)==SQLITE_CORRUPT ){
11970       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11971       toggleSelectOrder(p->db);
11972       shell_exec(p, sSelect.z, 0);
11973       toggleSelectOrder(p->db);
11974     }
11975     p->zDestTable = savedDestTable;
11976     p->mode = savedMode;
11977     freeText(&sTable);
11978     freeText(&sSelect);
11979     if( rc ) p->nErr++;
11980   }
11981   return 0;
11982 }
11983 
11984 /*
11985 ** Run zQuery.  Use dump_callback() as the callback routine so that
11986 ** the contents of the query are output as SQL statements.
11987 **
11988 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
11989 ** "ORDER BY rowid DESC" to the end.
11990 */
11991 static int run_schema_dump_query(
11992   ShellState *p,
11993   const char *zQuery
11994 ){
11995   int rc;
11996   char *zErr = 0;
11997   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
11998   if( rc==SQLITE_CORRUPT ){
11999     char *zQ2;
12000     int len = strlen30(zQuery);
12001     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
12002     if( zErr ){
12003       utf8_printf(p->out, "/****** %s ******/\n", zErr);
12004       sqlite3_free(zErr);
12005       zErr = 0;
12006     }
12007     zQ2 = malloc( len+100 );
12008     if( zQ2==0 ) return rc;
12009     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
12010     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
12011     if( rc ){
12012       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
12013     }else{
12014       rc = SQLITE_CORRUPT;
12015     }
12016     sqlite3_free(zErr);
12017     free(zQ2);
12018   }
12019   return rc;
12020 }
12021 
12022 /*
12023 ** Text of help messages.
12024 **
12025 ** The help text for each individual command begins with a line that starts
12026 ** with ".".  Subsequent lines are supplimental information.
12027 **
12028 ** There must be two or more spaces between the end of the command and the
12029 ** start of the description of what that command does.
12030 */
12031 static const char *(azHelp[]) = {
12032 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
12033   ".archive ...             Manage SQL archives",
12034   "   Each command must have exactly one of the following options:",
12035   "     -c, --create               Create a new archive",
12036   "     -u, --update               Add or update files with changed mtime",
12037   "     -i, --insert               Like -u but always add even if unchanged",
12038   "     -t, --list                 List contents of archive",
12039   "     -x, --extract              Extract files from archive",
12040   "   Optional arguments:",
12041   "     -v, --verbose              Print each filename as it is processed",
12042   "     -f FILE, --file FILE       Use archive FILE (default is current db)",
12043   "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
12044   "     -C DIR, --directory DIR    Read/extract files from directory DIR",
12045   "     -n, --dryrun               Show the SQL that would have occurred",
12046   "   Examples:",
12047   "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
12048   "     .ar -tf ARCHIVE          # List members of ARCHIVE",
12049   "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
12050   "   See also:",
12051   "      http://sqlite.org/cli.html#sqlar_archive_support",
12052 #endif
12053 #ifndef SQLITE_OMIT_AUTHORIZATION
12054   ".auth ON|OFF             Show authorizer callbacks",
12055 #endif
12056   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
12057   "       --append            Use the appendvfs",
12058   "       --async             Write to FILE without journal and fsync()",
12059   ".bail on|off             Stop after hitting an error.  Default OFF",
12060   ".binary on|off           Turn binary output on or off.  Default OFF",
12061   ".cd DIRECTORY            Change the working directory to DIRECTORY",
12062   ".changes on|off          Show number of rows changed by SQL",
12063   ".check GLOB              Fail if output since .testcase does not match",
12064   ".clone NEWDB             Clone data into NEWDB from the existing database",
12065   ".databases               List names and files of attached databases",
12066   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
12067   ".dbinfo ?DB?             Show status information about the database",
12068   ".dump ?TABLE? ...        Render all database content as SQL",
12069   "   Options:",
12070   "     --preserve-rowids      Include ROWID values in the output",
12071   "     --newlines             Allow unescaped newline characters in output",
12072   "   TABLE is a LIKE pattern for the tables to dump",
12073   ".echo on|off             Turn command echo on or off",
12074   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
12075   "   Other Modes:",
12076 #ifdef SQLITE_DEBUG
12077   "      test                  Show raw EXPLAIN QUERY PLAN output",
12078   "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
12079 #endif
12080   "      trigger               Like \"full\" but also show trigger bytecode",
12081   ".excel                   Display the output of next command in spreadsheet",
12082   ".exit ?CODE?             Exit this program with return-code CODE",
12083   ".expert                  EXPERIMENTAL. Suggest indexes for queries",
12084   ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
12085   ".filectrl CMD ...        Run various sqlite3_file_control() operations",
12086   "                           Run \".filectrl\" with no arguments for details",
12087   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
12088   ".headers on|off          Turn display of headers on or off",
12089   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
12090   ".import FILE TABLE       Import data from FILE into TABLE",
12091 #ifndef SQLITE_OMIT_TEST_CONTROL
12092   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
12093 #endif
12094   ".indexes ?TABLE?         Show names of indexes",
12095   "                           If TABLE is specified, only show indexes for",
12096   "                           tables matching TABLE using the LIKE operator.",
12097 #ifdef SQLITE_ENABLE_IOTRACE
12098   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
12099 #endif
12100   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
12101   ".lint OPTIONS            Report potential schema issues.",
12102   "     Options:",
12103   "        fkey-indexes     Find missing foreign key indexes",
12104 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12105   ".load FILE ?ENTRY?       Load an extension library",
12106 #endif
12107   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
12108   ".mode MODE ?TABLE?       Set output mode",
12109   "   MODE is one of:",
12110   "     ascii    Columns/rows delimited by 0x1F and 0x1E",
12111   "     csv      Comma-separated values",
12112   "     column   Left-aligned columns.  (See .width)",
12113   "     html     HTML <table> code",
12114   "     insert   SQL insert statements for TABLE",
12115   "     line     One value per line",
12116   "     list     Values delimited by \"|\"",
12117   "     quote    Escape answers as for SQL",
12118   "     tabs     Tab-separated values",
12119   "     tcl      TCL list elements",
12120   ".nullvalue STRING        Use STRING in place of NULL values",
12121   ".once (-e|-x|FILE)       Output for the next SQL command only to FILE",
12122   "     If FILE begins with '|' then open as a pipe",
12123   "     Other options:",
12124   "       -e    Invoke system text editor",
12125   "       -x    Open in a spreadsheet",
12126   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
12127   "     Options:",
12128   "        --append        Use appendvfs to append database to the end of FILE",
12129 #ifdef SQLITE_ENABLE_DESERIALIZE
12130   "        --deserialize   Load into memory useing sqlite3_deserialize()",
12131   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
12132   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
12133 #endif
12134   "        --new           Initialize FILE to an empty database",
12135   "        --nofollow      Do not follow symbolic links",
12136   "        --readonly      Open FILE readonly",
12137   "        --zip           FILE is a ZIP archive",
12138   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
12139   "     If FILE begins with '|' then open it as a pipe.",
12140   ".parameter CMD ...       Manage SQL parameter bindings",
12141   "   clear                   Erase all bindings",
12142   "   init                    Initialize the TEMP table that holds bindings",
12143   "   list                    List the current parameter bindings",
12144   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
12145   "                           PARAMETER should start with one of: $ : @ ?",
12146   "   unset PARAMETER         Remove PARAMETER from the binding table",
12147   ".print STRING...         Print literal STRING",
12148 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12149   ".progress N              Invoke progress handler after every N opcodes",
12150   "   --limit N                 Interrupt after N progress callbacks",
12151   "   --once                    Do no more than one progress interrupt",
12152   "   --quiet|-q                No output except at interrupts",
12153   "   --reset                   Reset the count for each input and interrupt",
12154 #endif
12155   ".prompt MAIN CONTINUE    Replace the standard prompts",
12156   ".quit                    Exit this program",
12157   ".read FILE               Read input from FILE",
12158 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12159   ".recover                 Recover as much data as possible from corrupt db.",
12160   "   --freelist-corrupt       Assume the freelist is corrupt",
12161   "   --recovery-db NAME       Store recovery metadata in database file NAME",
12162   "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
12163   "   --no-rowids              Do not attempt to recover rowid values",
12164   "                            that are not also INTEGER PRIMARY KEYs",
12165 #endif
12166   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
12167   ".save FILE               Write in-memory database into FILE",
12168   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
12169   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
12170   "     Options:",
12171   "         --indent            Try to pretty-print the schema",
12172   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
12173   "    Options:",
12174   "       --init               Create a new SELFTEST table",
12175   "       -v                   Verbose output",
12176   ".separator COL ?ROW?     Change the column and row separators",
12177 #if defined(SQLITE_ENABLE_SESSION)
12178   ".session ?NAME? CMD ...  Create or control sessions",
12179   "   Subcommands:",
12180   "     attach TABLE             Attach TABLE",
12181   "     changeset FILE           Write a changeset into FILE",
12182   "     close                    Close one session",
12183   "     enable ?BOOLEAN?         Set or query the enable bit",
12184   "     filter GLOB...           Reject tables matching GLOBs",
12185   "     indirect ?BOOLEAN?       Mark or query the indirect status",
12186   "     isempty                  Query whether the session is empty",
12187   "     list                     List currently open session names",
12188   "     open DB NAME             Open a new session on DB",
12189   "     patchset FILE            Write a patchset into FILE",
12190   "   If ?NAME? is omitted, the first defined session is used.",
12191 #endif
12192   ".sha3sum ...             Compute a SHA3 hash of database content",
12193   "    Options:",
12194   "      --schema              Also hash the sqlite_master table",
12195   "      --sha3-224            Use the sha3-224 algorithm",
12196   "      --sha3-256            Use the sha3-256 algorithm (default)",
12197   "      --sha3-384            Use the sha3-384 algorithm",
12198   "      --sha3-512            Use the sha3-512 algorithm",
12199   "    Any other argument is a LIKE pattern for tables to hash",
12200 #ifndef SQLITE_NOHAVE_SYSTEM
12201   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
12202 #endif
12203   ".show                    Show the current values for various settings",
12204   ".stats ?on|off?          Show stats or turn stats on or off",
12205 #ifndef SQLITE_NOHAVE_SYSTEM
12206   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
12207 #endif
12208   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
12209   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
12210   ".testctrl CMD ...        Run various sqlite3_test_control() operations",
12211   "                           Run \".testctrl\" with no arguments for details",
12212   ".timeout MS              Try opening locked tables for MS milliseconds",
12213   ".timer on|off            Turn SQL timer on or off",
12214 #ifndef SQLITE_OMIT_TRACE
12215   ".trace ?OPTIONS?         Output each SQL statement as it is run",
12216   "    FILE                    Send output to FILE",
12217   "    stdout                  Send output to stdout",
12218   "    stderr                  Send output to stderr",
12219   "    off                     Disable tracing",
12220   "    --expanded              Expand query parameters",
12221 #ifdef SQLITE_ENABLE_NORMALIZE
12222   "    --normalized            Normal the SQL statements",
12223 #endif
12224   "    --plain                 Show SQL as it is input",
12225   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
12226   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
12227   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
12228   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
12229 #endif /* SQLITE_OMIT_TRACE */
12230 #ifdef SQLITE_DEBUG
12231   ".unmodule NAME ...       Unregister virtual table modules",
12232   "    --allexcept             Unregister everything except those named",
12233 #endif
12234   ".vfsinfo ?AUX?           Information about the top-level VFS",
12235   ".vfslist                 List all available VFSes",
12236   ".vfsname ?AUX?           Print the name of the VFS stack",
12237   ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
12238   "     Negative values right-justify",
12239 };
12240 
12241 /*
12242 ** Output help text.
12243 **
12244 ** zPattern describes the set of commands for which help text is provided.
12245 ** If zPattern is NULL, then show all commands, but only give a one-line
12246 ** description of each.
12247 **
12248 ** Return the number of matches.
12249 */
12250 static int showHelp(FILE *out, const char *zPattern){
12251   int i = 0;
12252   int j = 0;
12253   int n = 0;
12254   char *zPat;
12255   if( zPattern==0
12256    || zPattern[0]=='0'
12257    || strcmp(zPattern,"-a")==0
12258    || strcmp(zPattern,"-all")==0
12259   ){
12260     /* Show all commands, but only one line per command */
12261     if( zPattern==0 ) zPattern = "";
12262     for(i=0; i<ArraySize(azHelp); i++){
12263       if( azHelp[i][0]=='.' || zPattern[0] ){
12264         utf8_printf(out, "%s\n", azHelp[i]);
12265         n++;
12266       }
12267     }
12268   }else{
12269     /* Look for commands that for which zPattern is an exact prefix */
12270     zPat = sqlite3_mprintf(".%s*", zPattern);
12271     for(i=0; i<ArraySize(azHelp); i++){
12272       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
12273         utf8_printf(out, "%s\n", azHelp[i]);
12274         j = i+1;
12275         n++;
12276       }
12277     }
12278     sqlite3_free(zPat);
12279     if( n ){
12280       if( n==1 ){
12281         /* when zPattern is a prefix of exactly one command, then include the
12282         ** details of that command, which should begin at offset j */
12283         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
12284           utf8_printf(out, "%s\n", azHelp[j]);
12285           j++;
12286         }
12287       }
12288       return n;
12289     }
12290     /* Look for commands that contain zPattern anywhere.  Show the complete
12291     ** text of all commands that match. */
12292     zPat = sqlite3_mprintf("%%%s%%", zPattern);
12293     for(i=0; i<ArraySize(azHelp); i++){
12294       if( azHelp[i][0]=='.' ) j = i;
12295       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
12296         utf8_printf(out, "%s\n", azHelp[j]);
12297         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
12298           j++;
12299           utf8_printf(out, "%s\n", azHelp[j]);
12300         }
12301         i = j;
12302         n++;
12303       }
12304     }
12305     sqlite3_free(zPat);
12306   }
12307   return n;
12308 }
12309 
12310 /* Forward reference */
12311 static int process_input(ShellState *p);
12312 
12313 /*
12314 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
12315 ** and return a pointer to the buffer. The caller is responsible for freeing
12316 ** the memory.
12317 **
12318 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
12319 ** read.
12320 **
12321 ** For convenience, a nul-terminator byte is always appended to the data read
12322 ** from the file before the buffer is returned. This byte is not included in
12323 ** the final value of (*pnByte), if applicable.
12324 **
12325 ** NULL is returned if any error is encountered. The final value of *pnByte
12326 ** is undefined in this case.
12327 */
12328 static char *readFile(const char *zName, int *pnByte){
12329   FILE *in = fopen(zName, "rb");
12330   long nIn;
12331   size_t nRead;
12332   char *pBuf;
12333   if( in==0 ) return 0;
12334   fseek(in, 0, SEEK_END);
12335   nIn = ftell(in);
12336   rewind(in);
12337   pBuf = sqlite3_malloc64( nIn+1 );
12338   if( pBuf==0 ){ fclose(in); return 0; }
12339   nRead = fread(pBuf, nIn, 1, in);
12340   fclose(in);
12341   if( nRead!=1 ){
12342     sqlite3_free(pBuf);
12343     return 0;
12344   }
12345   pBuf[nIn] = 0;
12346   if( pnByte ) *pnByte = nIn;
12347   return pBuf;
12348 }
12349 
12350 #if defined(SQLITE_ENABLE_SESSION)
12351 /*
12352 ** Close a single OpenSession object and release all of its associated
12353 ** resources.
12354 */
12355 static void session_close(OpenSession *pSession){
12356   int i;
12357   sqlite3session_delete(pSession->p);
12358   sqlite3_free(pSession->zName);
12359   for(i=0; i<pSession->nFilter; i++){
12360     sqlite3_free(pSession->azFilter[i]);
12361   }
12362   sqlite3_free(pSession->azFilter);
12363   memset(pSession, 0, sizeof(OpenSession));
12364 }
12365 #endif
12366 
12367 /*
12368 ** Close all OpenSession objects and release all associated resources.
12369 */
12370 #if defined(SQLITE_ENABLE_SESSION)
12371 static void session_close_all(ShellState *p){
12372   int i;
12373   for(i=0; i<p->nSession; i++){
12374     session_close(&p->aSession[i]);
12375   }
12376   p->nSession = 0;
12377 }
12378 #else
12379 # define session_close_all(X)
12380 #endif
12381 
12382 /*
12383 ** Implementation of the xFilter function for an open session.  Omit
12384 ** any tables named by ".session filter" but let all other table through.
12385 */
12386 #if defined(SQLITE_ENABLE_SESSION)
12387 static int session_filter(void *pCtx, const char *zTab){
12388   OpenSession *pSession = (OpenSession*)pCtx;
12389   int i;
12390   for(i=0; i<pSession->nFilter; i++){
12391     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
12392   }
12393   return 1;
12394 }
12395 #endif
12396 
12397 /*
12398 ** Try to deduce the type of file for zName based on its content.  Return
12399 ** one of the SHELL_OPEN_* constants.
12400 **
12401 ** If the file does not exist or is empty but its name looks like a ZIP
12402 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
12403 ** Otherwise, assume an ordinary database regardless of the filename if
12404 ** the type cannot be determined from content.
12405 */
12406 int deduceDatabaseType(const char *zName, int dfltZip){
12407   FILE *f = fopen(zName, "rb");
12408   size_t n;
12409   int rc = SHELL_OPEN_UNSPEC;
12410   char zBuf[100];
12411   if( f==0 ){
12412     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12413        return SHELL_OPEN_ZIPFILE;
12414     }else{
12415        return SHELL_OPEN_NORMAL;
12416     }
12417   }
12418   n = fread(zBuf, 16, 1, f);
12419   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
12420     fclose(f);
12421     return SHELL_OPEN_NORMAL;
12422   }
12423   fseek(f, -25, SEEK_END);
12424   n = fread(zBuf, 25, 1, f);
12425   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
12426     rc = SHELL_OPEN_APPENDVFS;
12427   }else{
12428     fseek(f, -22, SEEK_END);
12429     n = fread(zBuf, 22, 1, f);
12430     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
12431        && zBuf[3]==0x06 ){
12432       rc = SHELL_OPEN_ZIPFILE;
12433     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12434       rc = SHELL_OPEN_ZIPFILE;
12435     }
12436   }
12437   fclose(f);
12438   return rc;
12439 }
12440 
12441 #ifdef SQLITE_ENABLE_DESERIALIZE
12442 /*
12443 ** Reconstruct an in-memory database using the output from the "dbtotxt"
12444 ** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
12445 ** is 0, then read from standard input.
12446 */
12447 static unsigned char *readHexDb(ShellState *p, int *pnData){
12448   unsigned char *a = 0;
12449   int nLine;
12450   int n = 0;
12451   int pgsz = 0;
12452   int iOffset = 0;
12453   int j, k;
12454   int rc;
12455   FILE *in;
12456   unsigned int x[16];
12457   char zLine[1000];
12458   if( p->zDbFilename ){
12459     in = fopen(p->zDbFilename, "r");
12460     if( in==0 ){
12461       utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
12462       return 0;
12463     }
12464     nLine = 0;
12465   }else{
12466     in = p->in;
12467     nLine = p->lineno;
12468     if( in==0 ) in = stdin;
12469   }
12470   *pnData = 0;
12471   nLine++;
12472   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
12473   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
12474   if( rc!=2 ) goto readHexDb_error;
12475   if( n<0 ) goto readHexDb_error;
12476   if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
12477   n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
12478   a = sqlite3_malloc( n ? n : 1 );
12479   if( a==0 ){
12480     utf8_printf(stderr, "Out of memory!\n");
12481     goto readHexDb_error;
12482   }
12483   memset(a, 0, n);
12484   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
12485     utf8_printf(stderr, "invalid pagesize\n");
12486     goto readHexDb_error;
12487   }
12488   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
12489     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
12490     if( rc==2 ){
12491       iOffset = k;
12492       continue;
12493     }
12494     if( strncmp(zLine, "| end ", 6)==0 ){
12495       break;
12496     }
12497     rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
12498                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
12499                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
12500     if( rc==17 ){
12501       k = iOffset+j;
12502       if( k+16<=n ){
12503         int ii;
12504         for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
12505       }
12506     }
12507   }
12508   *pnData = n;
12509   if( in!=p->in ){
12510     fclose(in);
12511   }else{
12512     p->lineno = nLine;
12513   }
12514   return a;
12515 
12516 readHexDb_error:
12517   if( in!=p->in ){
12518     fclose(in);
12519   }else{
12520     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
12521       nLine++;
12522       if(strncmp(zLine, "| end ", 6)==0 ) break;
12523     }
12524     p->lineno = nLine;
12525   }
12526   sqlite3_free(a);
12527   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
12528   return 0;
12529 }
12530 #endif /* SQLITE_ENABLE_DESERIALIZE */
12531 
12532 /*
12533 ** Scalar function "shell_int32". The first argument to this function
12534 ** must be a blob. The second a non-negative integer. This function
12535 ** reads and returns a 32-bit big-endian integer from byte
12536 ** offset (4*<arg2>) of the blob.
12537 */
12538 static void shellInt32(
12539   sqlite3_context *context,
12540   int argc,
12541   sqlite3_value **argv
12542 ){
12543   const unsigned char *pBlob;
12544   int nBlob;
12545   int iInt;
12546 
12547   UNUSED_PARAMETER(argc);
12548   nBlob = sqlite3_value_bytes(argv[0]);
12549   pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
12550   iInt = sqlite3_value_int(argv[1]);
12551 
12552   if( iInt>=0 && (iInt+1)*4<=nBlob ){
12553     const unsigned char *a = &pBlob[iInt*4];
12554     sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
12555                        + ((sqlite3_int64)a[1]<<16)
12556                        + ((sqlite3_int64)a[2]<< 8)
12557                        + ((sqlite3_int64)a[3]<< 0);
12558     sqlite3_result_int64(context, iVal);
12559   }
12560 }
12561 
12562 /*
12563 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
12564 ** using "..." with internal double-quote characters doubled.
12565 */
12566 static void shellIdQuote(
12567   sqlite3_context *context,
12568   int argc,
12569   sqlite3_value **argv
12570 ){
12571   const char *zName = (const char*)sqlite3_value_text(argv[0]);
12572   UNUSED_PARAMETER(argc);
12573   if( zName ){
12574     char *z = sqlite3_mprintf("\"%w\"", zName);
12575     sqlite3_result_text(context, z, -1, sqlite3_free);
12576   }
12577 }
12578 
12579 /*
12580 ** Scalar function "shell_escape_crnl" used by the .recover command.
12581 ** The argument passed to this function is the output of built-in
12582 ** function quote(). If the first character of the input is "'",
12583 ** indicating that the value passed to quote() was a text value,
12584 ** then this function searches the input for "\n" and "\r" characters
12585 ** and adds a wrapper similar to the following:
12586 **
12587 **   replace(replace(<input>, '\n', char(10), '\r', char(13));
12588 **
12589 ** Or, if the first character of the input is not "'", then a copy
12590 ** of the input is returned.
12591 */
12592 static void shellEscapeCrnl(
12593   sqlite3_context *context,
12594   int argc,
12595   sqlite3_value **argv
12596 ){
12597   const char *zText = (const char*)sqlite3_value_text(argv[0]);
12598   UNUSED_PARAMETER(argc);
12599   if( zText[0]=='\'' ){
12600     int nText = sqlite3_value_bytes(argv[0]);
12601     int i;
12602     char zBuf1[20];
12603     char zBuf2[20];
12604     const char *zNL = 0;
12605     const char *zCR = 0;
12606     int nCR = 0;
12607     int nNL = 0;
12608 
12609     for(i=0; zText[i]; i++){
12610       if( zNL==0 && zText[i]=='\n' ){
12611         zNL = unused_string(zText, "\\n", "\\012", zBuf1);
12612         nNL = (int)strlen(zNL);
12613       }
12614       if( zCR==0 && zText[i]=='\r' ){
12615         zCR = unused_string(zText, "\\r", "\\015", zBuf2);
12616         nCR = (int)strlen(zCR);
12617       }
12618     }
12619 
12620     if( zNL || zCR ){
12621       int iOut = 0;
12622       i64 nMax = (nNL > nCR) ? nNL : nCR;
12623       i64 nAlloc = nMax * nText + (nMax+64)*2;
12624       char *zOut = (char*)sqlite3_malloc64(nAlloc);
12625       if( zOut==0 ){
12626         sqlite3_result_error_nomem(context);
12627         return;
12628       }
12629 
12630       if( zNL && zCR ){
12631         memcpy(&zOut[iOut], "replace(replace(", 16);
12632         iOut += 16;
12633       }else{
12634         memcpy(&zOut[iOut], "replace(", 8);
12635         iOut += 8;
12636       }
12637       for(i=0; zText[i]; i++){
12638         if( zText[i]=='\n' ){
12639           memcpy(&zOut[iOut], zNL, nNL);
12640           iOut += nNL;
12641         }else if( zText[i]=='\r' ){
12642           memcpy(&zOut[iOut], zCR, nCR);
12643           iOut += nCR;
12644         }else{
12645           zOut[iOut] = zText[i];
12646           iOut++;
12647         }
12648       }
12649 
12650       if( zNL ){
12651         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12652         memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
12653         memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
12654       }
12655       if( zCR ){
12656         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12657         memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
12658         memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
12659       }
12660 
12661       sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
12662       sqlite3_free(zOut);
12663       return;
12664     }
12665   }
12666 
12667   sqlite3_result_value(context, argv[0]);
12668 }
12669 
12670 /* Flags for open_db().
12671 **
12672 ** The default behavior of open_db() is to exit(1) if the database fails to
12673 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
12674 ** but still returns without calling exit.
12675 **
12676 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
12677 ** ZIP archive if the file does not exist or is empty and its name matches
12678 ** the *.zip pattern.
12679 */
12680 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
12681 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
12682 
12683 /*
12684 ** Make sure the database is open.  If it is not, then open it.  If
12685 ** the database fails to open, print an error message and exit.
12686 */
12687 static void open_db(ShellState *p, int openFlags){
12688   if( p->db==0 ){
12689     if( p->openMode==SHELL_OPEN_UNSPEC ){
12690       if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
12691         p->openMode = SHELL_OPEN_NORMAL;
12692       }else{
12693         p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
12694                              (openFlags & OPEN_DB_ZIPFILE)!=0);
12695       }
12696     }
12697     switch( p->openMode ){
12698       case SHELL_OPEN_APPENDVFS: {
12699         sqlite3_open_v2(p->zDbFilename, &p->db,
12700            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
12701         break;
12702       }
12703       case SHELL_OPEN_HEXDB:
12704       case SHELL_OPEN_DESERIALIZE: {
12705         sqlite3_open(0, &p->db);
12706         break;
12707       }
12708       case SHELL_OPEN_ZIPFILE: {
12709         sqlite3_open(":memory:", &p->db);
12710         break;
12711       }
12712       case SHELL_OPEN_READONLY: {
12713         sqlite3_open_v2(p->zDbFilename, &p->db,
12714             SQLITE_OPEN_READONLY|p->openFlags, 0);
12715         break;
12716       }
12717       case SHELL_OPEN_UNSPEC:
12718       case SHELL_OPEN_NORMAL: {
12719         sqlite3_open_v2(p->zDbFilename, &p->db,
12720            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
12721         break;
12722       }
12723     }
12724     globalDb = p->db;
12725     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
12726       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
12727           p->zDbFilename, sqlite3_errmsg(p->db));
12728       if( openFlags & OPEN_DB_KEEPALIVE ){
12729         sqlite3_open(":memory:", &p->db);
12730         return;
12731       }
12732       exit(1);
12733     }
12734 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12735     sqlite3_enable_load_extension(p->db, 1);
12736 #endif
12737     sqlite3_fileio_init(p->db, 0, 0);
12738     sqlite3_shathree_init(p->db, 0, 0);
12739     sqlite3_completion_init(p->db, 0, 0);
12740 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12741     sqlite3_dbdata_init(p->db, 0, 0);
12742 #endif
12743 #ifdef SQLITE_HAVE_ZLIB
12744     sqlite3_zipfile_init(p->db, 0, 0);
12745     sqlite3_sqlar_init(p->db, 0, 0);
12746 #endif
12747     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
12748                             shellAddSchemaName, 0, 0);
12749     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
12750                             shellModuleSchema, 0, 0);
12751     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
12752                             shellPutsFunc, 0, 0);
12753     sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
12754                             shellEscapeCrnl, 0, 0);
12755     sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
12756                             shellInt32, 0, 0);
12757     sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
12758                             shellIdQuote, 0, 0);
12759 #ifndef SQLITE_NOHAVE_SYSTEM
12760     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
12761                             editFunc, 0, 0);
12762     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
12763                             editFunc, 0, 0);
12764 #endif
12765     if( p->openMode==SHELL_OPEN_ZIPFILE ){
12766       char *zSql = sqlite3_mprintf(
12767          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
12768       sqlite3_exec(p->db, zSql, 0, 0, 0);
12769       sqlite3_free(zSql);
12770     }
12771 #ifdef SQLITE_ENABLE_DESERIALIZE
12772     else
12773     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
12774       int rc;
12775       int nData = 0;
12776       unsigned char *aData;
12777       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
12778         aData = (unsigned char*)readFile(p->zDbFilename, &nData);
12779       }else{
12780         aData = readHexDb(p, &nData);
12781         if( aData==0 ){
12782           return;
12783         }
12784       }
12785       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
12786                    SQLITE_DESERIALIZE_RESIZEABLE |
12787                    SQLITE_DESERIALIZE_FREEONCLOSE);
12788       if( rc ){
12789         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
12790       }
12791       if( p->szMax>0 ){
12792         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
12793       }
12794     }
12795 #endif
12796   }
12797 }
12798 
12799 /*
12800 ** Attempt to close the databaes connection.  Report errors.
12801 */
12802 void close_db(sqlite3 *db){
12803   int rc = sqlite3_close(db);
12804   if( rc ){
12805     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
12806         rc, sqlite3_errmsg(db));
12807   }
12808 }
12809 
12810 #if HAVE_READLINE || HAVE_EDITLINE
12811 /*
12812 ** Readline completion callbacks
12813 */
12814 static char *readline_completion_generator(const char *text, int state){
12815   static sqlite3_stmt *pStmt = 0;
12816   char *zRet;
12817   if( state==0 ){
12818     char *zSql;
12819     sqlite3_finalize(pStmt);
12820     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
12821                            "  FROM completion(%Q) ORDER BY 1", text);
12822     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
12823     sqlite3_free(zSql);
12824   }
12825   if( sqlite3_step(pStmt)==SQLITE_ROW ){
12826     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
12827   }else{
12828     sqlite3_finalize(pStmt);
12829     pStmt = 0;
12830     zRet = 0;
12831   }
12832   return zRet;
12833 }
12834 static char **readline_completion(const char *zText, int iStart, int iEnd){
12835   rl_attempted_completion_over = 1;
12836   return rl_completion_matches(zText, readline_completion_generator);
12837 }
12838 
12839 #elif HAVE_LINENOISE
12840 /*
12841 ** Linenoise completion callback
12842 */
12843 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
12844   int nLine = strlen30(zLine);
12845   int i, iStart;
12846   sqlite3_stmt *pStmt = 0;
12847   char *zSql;
12848   char zBuf[1000];
12849 
12850   if( nLine>sizeof(zBuf)-30 ) return;
12851   if( zLine[0]=='.' || zLine[0]=='#') return;
12852   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
12853   if( i==nLine-1 ) return;
12854   iStart = i+1;
12855   memcpy(zBuf, zLine, iStart);
12856   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
12857                          "  FROM completion(%Q,%Q) ORDER BY 1",
12858                          &zLine[iStart], zLine);
12859   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
12860   sqlite3_free(zSql);
12861   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
12862   while( sqlite3_step(pStmt)==SQLITE_ROW ){
12863     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
12864     int nCompletion = sqlite3_column_bytes(pStmt, 0);
12865     if( iStart+nCompletion < sizeof(zBuf)-1 ){
12866       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
12867       linenoiseAddCompletion(lc, zBuf);
12868     }
12869   }
12870   sqlite3_finalize(pStmt);
12871 }
12872 #endif
12873 
12874 /*
12875 ** Do C-language style dequoting.
12876 **
12877 **    \a    -> alarm
12878 **    \b    -> backspace
12879 **    \t    -> tab
12880 **    \n    -> newline
12881 **    \v    -> vertical tab
12882 **    \f    -> form feed
12883 **    \r    -> carriage return
12884 **    \s    -> space
12885 **    \"    -> "
12886 **    \'    -> '
12887 **    \\    -> backslash
12888 **    \NNN  -> ascii character NNN in octal
12889 */
12890 static void resolve_backslashes(char *z){
12891   int i, j;
12892   char c;
12893   while( *z && *z!='\\' ) z++;
12894   for(i=j=0; (c = z[i])!=0; i++, j++){
12895     if( c=='\\' && z[i+1]!=0 ){
12896       c = z[++i];
12897       if( c=='a' ){
12898         c = '\a';
12899       }else if( c=='b' ){
12900         c = '\b';
12901       }else if( c=='t' ){
12902         c = '\t';
12903       }else if( c=='n' ){
12904         c = '\n';
12905       }else if( c=='v' ){
12906         c = '\v';
12907       }else if( c=='f' ){
12908         c = '\f';
12909       }else if( c=='r' ){
12910         c = '\r';
12911       }else if( c=='"' ){
12912         c = '"';
12913       }else if( c=='\'' ){
12914         c = '\'';
12915       }else if( c=='\\' ){
12916         c = '\\';
12917       }else if( c>='0' && c<='7' ){
12918         c -= '0';
12919         if( z[i+1]>='0' && z[i+1]<='7' ){
12920           i++;
12921           c = (c<<3) + z[i] - '0';
12922           if( z[i+1]>='0' && z[i+1]<='7' ){
12923             i++;
12924             c = (c<<3) + z[i] - '0';
12925           }
12926         }
12927       }
12928     }
12929     z[j] = c;
12930   }
12931   if( j<i ) z[j] = 0;
12932 }
12933 
12934 /*
12935 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
12936 ** for TRUE and FALSE.  Return the integer value if appropriate.
12937 */
12938 static int booleanValue(const char *zArg){
12939   int i;
12940   if( zArg[0]=='0' && zArg[1]=='x' ){
12941     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
12942   }else{
12943     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
12944   }
12945   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
12946   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
12947     return 1;
12948   }
12949   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
12950     return 0;
12951   }
12952   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
12953           zArg);
12954   return 0;
12955 }
12956 
12957 /*
12958 ** Set or clear a shell flag according to a boolean value.
12959 */
12960 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
12961   if( booleanValue(zArg) ){
12962     ShellSetFlag(p, mFlag);
12963   }else{
12964     ShellClearFlag(p, mFlag);
12965   }
12966 }
12967 
12968 /*
12969 ** Close an output file, assuming it is not stderr or stdout
12970 */
12971 static void output_file_close(FILE *f){
12972   if( f && f!=stdout && f!=stderr ) fclose(f);
12973 }
12974 
12975 /*
12976 ** Try to open an output file.   The names "stdout" and "stderr" are
12977 ** recognized and do the right thing.  NULL is returned if the output
12978 ** filename is "off".
12979 */
12980 static FILE *output_file_open(const char *zFile, int bTextMode){
12981   FILE *f;
12982   if( strcmp(zFile,"stdout")==0 ){
12983     f = stdout;
12984   }else if( strcmp(zFile, "stderr")==0 ){
12985     f = stderr;
12986   }else if( strcmp(zFile, "off")==0 ){
12987     f = 0;
12988   }else{
12989     f = fopen(zFile, bTextMode ? "w" : "wb");
12990     if( f==0 ){
12991       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
12992     }
12993   }
12994   return f;
12995 }
12996 
12997 #ifndef SQLITE_OMIT_TRACE
12998 /*
12999 ** A routine for handling output from sqlite3_trace().
13000 */
13001 static int sql_trace_callback(
13002   unsigned mType,         /* The trace type */
13003   void *pArg,             /* The ShellState pointer */
13004   void *pP,               /* Usually a pointer to sqlite_stmt */
13005   void *pX                /* Auxiliary output */
13006 ){
13007   ShellState *p = (ShellState*)pArg;
13008   sqlite3_stmt *pStmt;
13009   const char *zSql;
13010   int nSql;
13011   if( p->traceOut==0 ) return 0;
13012   if( mType==SQLITE_TRACE_CLOSE ){
13013     utf8_printf(p->traceOut, "-- closing database connection\n");
13014     return 0;
13015   }
13016   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
13017     zSql = (const char*)pX;
13018   }else{
13019     pStmt = (sqlite3_stmt*)pP;
13020     switch( p->eTraceType ){
13021       case SHELL_TRACE_EXPANDED: {
13022         zSql = sqlite3_expanded_sql(pStmt);
13023         break;
13024       }
13025 #ifdef SQLITE_ENABLE_NORMALIZE
13026       case SHELL_TRACE_NORMALIZED: {
13027         zSql = sqlite3_normalized_sql(pStmt);
13028         break;
13029       }
13030 #endif
13031       default: {
13032         zSql = sqlite3_sql(pStmt);
13033         break;
13034       }
13035     }
13036   }
13037   if( zSql==0 ) return 0;
13038   nSql = strlen30(zSql);
13039   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
13040   switch( mType ){
13041     case SQLITE_TRACE_ROW:
13042     case SQLITE_TRACE_STMT: {
13043       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
13044       break;
13045     }
13046     case SQLITE_TRACE_PROFILE: {
13047       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
13048       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
13049       break;
13050     }
13051   }
13052   return 0;
13053 }
13054 #endif
13055 
13056 /*
13057 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
13058 ** a useful spot to set a debugger breakpoint.
13059 */
13060 static void test_breakpoint(void){
13061   static int nCall = 0;
13062   nCall++;
13063 }
13064 
13065 /*
13066 ** An object used to read a CSV and other files for import.
13067 */
13068 typedef struct ImportCtx ImportCtx;
13069 struct ImportCtx {
13070   const char *zFile;  /* Name of the input file */
13071   FILE *in;           /* Read the CSV text from this input stream */
13072   char *z;            /* Accumulated text for a field */
13073   int n;              /* Number of bytes in z */
13074   int nAlloc;         /* Space allocated for z[] */
13075   int nLine;          /* Current line number */
13076   int bNotFirst;      /* True if one or more bytes already read */
13077   int cTerm;          /* Character that terminated the most recent field */
13078   int cColSep;        /* The column separator character.  (Usually ",") */
13079   int cRowSep;        /* The row separator character.  (Usually "\n") */
13080 };
13081 
13082 /* Append a single byte to z[] */
13083 static void import_append_char(ImportCtx *p, int c){
13084   if( p->n+1>=p->nAlloc ){
13085     p->nAlloc += p->nAlloc + 100;
13086     p->z = sqlite3_realloc64(p->z, p->nAlloc);
13087     if( p->z==0 ) shell_out_of_memory();
13088   }
13089   p->z[p->n++] = (char)c;
13090 }
13091 
13092 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
13093 ** with the option of having a separator other than ",".
13094 **
13095 **   +  Input comes from p->in.
13096 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
13097 **      from sqlite3_malloc64().
13098 **   +  Use p->cSep as the column separator.  The default is ",".
13099 **   +  Use p->rSep as the row separator.  The default is "\n".
13100 **   +  Keep track of the line number in p->nLine.
13101 **   +  Store the character that terminates the field in p->cTerm.  Store
13102 **      EOF on end-of-file.
13103 **   +  Report syntax errors on stderr
13104 */
13105 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
13106   int c;
13107   int cSep = p->cColSep;
13108   int rSep = p->cRowSep;
13109   p->n = 0;
13110   c = fgetc(p->in);
13111   if( c==EOF || seenInterrupt ){
13112     p->cTerm = EOF;
13113     return 0;
13114   }
13115   if( c=='"' ){
13116     int pc, ppc;
13117     int startLine = p->nLine;
13118     int cQuote = c;
13119     pc = ppc = 0;
13120     while( 1 ){
13121       c = fgetc(p->in);
13122       if( c==rSep ) p->nLine++;
13123       if( c==cQuote ){
13124         if( pc==cQuote ){
13125           pc = 0;
13126           continue;
13127         }
13128       }
13129       if( (c==cSep && pc==cQuote)
13130        || (c==rSep && pc==cQuote)
13131        || (c==rSep && pc=='\r' && ppc==cQuote)
13132        || (c==EOF && pc==cQuote)
13133       ){
13134         do{ p->n--; }while( p->z[p->n]!=cQuote );
13135         p->cTerm = c;
13136         break;
13137       }
13138       if( pc==cQuote && c!='\r' ){
13139         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
13140                 p->zFile, p->nLine, cQuote);
13141       }
13142       if( c==EOF ){
13143         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
13144                 p->zFile, startLine, cQuote);
13145         p->cTerm = c;
13146         break;
13147       }
13148       import_append_char(p, c);
13149       ppc = pc;
13150       pc = c;
13151     }
13152   }else{
13153     /* If this is the first field being parsed and it begins with the
13154     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
13155     if( (c&0xff)==0xef && p->bNotFirst==0 ){
13156       import_append_char(p, c);
13157       c = fgetc(p->in);
13158       if( (c&0xff)==0xbb ){
13159         import_append_char(p, c);
13160         c = fgetc(p->in);
13161         if( (c&0xff)==0xbf ){
13162           p->bNotFirst = 1;
13163           p->n = 0;
13164           return csv_read_one_field(p);
13165         }
13166       }
13167     }
13168     while( c!=EOF && c!=cSep && c!=rSep ){
13169       import_append_char(p, c);
13170       c = fgetc(p->in);
13171     }
13172     if( c==rSep ){
13173       p->nLine++;
13174       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
13175     }
13176     p->cTerm = c;
13177   }
13178   if( p->z ) p->z[p->n] = 0;
13179   p->bNotFirst = 1;
13180   return p->z;
13181 }
13182 
13183 /* Read a single field of ASCII delimited text.
13184 **
13185 **   +  Input comes from p->in.
13186 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
13187 **      from sqlite3_malloc64().
13188 **   +  Use p->cSep as the column separator.  The default is "\x1F".
13189 **   +  Use p->rSep as the row separator.  The default is "\x1E".
13190 **   +  Keep track of the row number in p->nLine.
13191 **   +  Store the character that terminates the field in p->cTerm.  Store
13192 **      EOF on end-of-file.
13193 **   +  Report syntax errors on stderr
13194 */
13195 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
13196   int c;
13197   int cSep = p->cColSep;
13198   int rSep = p->cRowSep;
13199   p->n = 0;
13200   c = fgetc(p->in);
13201   if( c==EOF || seenInterrupt ){
13202     p->cTerm = EOF;
13203     return 0;
13204   }
13205   while( c!=EOF && c!=cSep && c!=rSep ){
13206     import_append_char(p, c);
13207     c = fgetc(p->in);
13208   }
13209   if( c==rSep ){
13210     p->nLine++;
13211   }
13212   p->cTerm = c;
13213   if( p->z ) p->z[p->n] = 0;
13214   return p->z;
13215 }
13216 
13217 /*
13218 ** Try to transfer data for table zTable.  If an error is seen while
13219 ** moving forward, try to go backwards.  The backwards movement won't
13220 ** work for WITHOUT ROWID tables.
13221 */
13222 static void tryToCloneData(
13223   ShellState *p,
13224   sqlite3 *newDb,
13225   const char *zTable
13226 ){
13227   sqlite3_stmt *pQuery = 0;
13228   sqlite3_stmt *pInsert = 0;
13229   char *zQuery = 0;
13230   char *zInsert = 0;
13231   int rc;
13232   int i, j, n;
13233   int nTable = strlen30(zTable);
13234   int k = 0;
13235   int cnt = 0;
13236   const int spinRate = 10000;
13237 
13238   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
13239   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13240   if( rc ){
13241     utf8_printf(stderr, "Error %d: %s on [%s]\n",
13242             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13243             zQuery);
13244     goto end_data_xfer;
13245   }
13246   n = sqlite3_column_count(pQuery);
13247   zInsert = sqlite3_malloc64(200 + nTable + n*3);
13248   if( zInsert==0 ) shell_out_of_memory();
13249   sqlite3_snprintf(200+nTable,zInsert,
13250                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
13251   i = strlen30(zInsert);
13252   for(j=1; j<n; j++){
13253     memcpy(zInsert+i, ",?", 2);
13254     i += 2;
13255   }
13256   memcpy(zInsert+i, ");", 3);
13257   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
13258   if( rc ){
13259     utf8_printf(stderr, "Error %d: %s on [%s]\n",
13260             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
13261             zQuery);
13262     goto end_data_xfer;
13263   }
13264   for(k=0; k<2; k++){
13265     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13266       for(i=0; i<n; i++){
13267         switch( sqlite3_column_type(pQuery, i) ){
13268           case SQLITE_NULL: {
13269             sqlite3_bind_null(pInsert, i+1);
13270             break;
13271           }
13272           case SQLITE_INTEGER: {
13273             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
13274             break;
13275           }
13276           case SQLITE_FLOAT: {
13277             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
13278             break;
13279           }
13280           case SQLITE_TEXT: {
13281             sqlite3_bind_text(pInsert, i+1,
13282                              (const char*)sqlite3_column_text(pQuery,i),
13283                              -1, SQLITE_STATIC);
13284             break;
13285           }
13286           case SQLITE_BLOB: {
13287             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
13288                                             sqlite3_column_bytes(pQuery,i),
13289                                             SQLITE_STATIC);
13290             break;
13291           }
13292         }
13293       } /* End for */
13294       rc = sqlite3_step(pInsert);
13295       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
13296         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
13297                         sqlite3_errmsg(newDb));
13298       }
13299       sqlite3_reset(pInsert);
13300       cnt++;
13301       if( (cnt%spinRate)==0 ){
13302         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
13303         fflush(stdout);
13304       }
13305     } /* End while */
13306     if( rc==SQLITE_DONE ) break;
13307     sqlite3_finalize(pQuery);
13308     sqlite3_free(zQuery);
13309     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
13310                              zTable);
13311     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13312     if( rc ){
13313       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
13314       break;
13315     }
13316   } /* End for(k=0...) */
13317 
13318 end_data_xfer:
13319   sqlite3_finalize(pQuery);
13320   sqlite3_finalize(pInsert);
13321   sqlite3_free(zQuery);
13322   sqlite3_free(zInsert);
13323 }
13324 
13325 
13326 /*
13327 ** Try to transfer all rows of the schema that match zWhere.  For
13328 ** each row, invoke xForEach() on the object defined by that row.
13329 ** If an error is encountered while moving forward through the
13330 ** sqlite_master table, try again moving backwards.
13331 */
13332 static void tryToCloneSchema(
13333   ShellState *p,
13334   sqlite3 *newDb,
13335   const char *zWhere,
13336   void (*xForEach)(ShellState*,sqlite3*,const char*)
13337 ){
13338   sqlite3_stmt *pQuery = 0;
13339   char *zQuery = 0;
13340   int rc;
13341   const unsigned char *zName;
13342   const unsigned char *zSql;
13343   char *zErrMsg = 0;
13344 
13345   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13346                            " WHERE %s", zWhere);
13347   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13348   if( rc ){
13349     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13350                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13351                     zQuery);
13352     goto end_schema_xfer;
13353   }
13354   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13355     zName = sqlite3_column_text(pQuery, 0);
13356     zSql = sqlite3_column_text(pQuery, 1);
13357     printf("%s... ", zName); fflush(stdout);
13358     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13359     if( zErrMsg ){
13360       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13361       sqlite3_free(zErrMsg);
13362       zErrMsg = 0;
13363     }
13364     if( xForEach ){
13365       xForEach(p, newDb, (const char*)zName);
13366     }
13367     printf("done\n");
13368   }
13369   if( rc!=SQLITE_DONE ){
13370     sqlite3_finalize(pQuery);
13371     sqlite3_free(zQuery);
13372     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13373                              " WHERE %s ORDER BY rowid DESC", zWhere);
13374     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13375     if( rc ){
13376       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13377                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13378                       zQuery);
13379       goto end_schema_xfer;
13380     }
13381     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13382       zName = sqlite3_column_text(pQuery, 0);
13383       zSql = sqlite3_column_text(pQuery, 1);
13384       printf("%s... ", zName); fflush(stdout);
13385       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13386       if( zErrMsg ){
13387         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13388         sqlite3_free(zErrMsg);
13389         zErrMsg = 0;
13390       }
13391       if( xForEach ){
13392         xForEach(p, newDb, (const char*)zName);
13393       }
13394       printf("done\n");
13395     }
13396   }
13397 end_schema_xfer:
13398   sqlite3_finalize(pQuery);
13399   sqlite3_free(zQuery);
13400 }
13401 
13402 /*
13403 ** Open a new database file named "zNewDb".  Try to recover as much information
13404 ** as possible out of the main database (which might be corrupt) and write it
13405 ** into zNewDb.
13406 */
13407 static void tryToClone(ShellState *p, const char *zNewDb){
13408   int rc;
13409   sqlite3 *newDb = 0;
13410   if( access(zNewDb,0)==0 ){
13411     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
13412     return;
13413   }
13414   rc = sqlite3_open(zNewDb, &newDb);
13415   if( rc ){
13416     utf8_printf(stderr, "Cannot create output database: %s\n",
13417             sqlite3_errmsg(newDb));
13418   }else{
13419     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
13420     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
13421     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
13422     tryToCloneSchema(p, newDb, "type!='table'", 0);
13423     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
13424     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13425   }
13426   close_db(newDb);
13427 }
13428 
13429 /*
13430 ** Change the output file back to stdout.
13431 **
13432 ** If the p->doXdgOpen flag is set, that means the output was being
13433 ** redirected to a temporary file named by p->zTempFile.  In that case,
13434 ** launch start/open/xdg-open on that temporary file.
13435 */
13436 static void output_reset(ShellState *p){
13437   if( p->outfile[0]=='|' ){
13438 #ifndef SQLITE_OMIT_POPEN
13439     pclose(p->out);
13440 #endif
13441   }else{
13442     output_file_close(p->out);
13443 #ifndef SQLITE_NOHAVE_SYSTEM
13444     if( p->doXdgOpen ){
13445       const char *zXdgOpenCmd =
13446 #if defined(_WIN32)
13447       "start";
13448 #elif defined(__APPLE__)
13449       "open";
13450 #else
13451       "xdg-open";
13452 #endif
13453       char *zCmd;
13454       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
13455       if( system(zCmd) ){
13456         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
13457       }
13458       sqlite3_free(zCmd);
13459       outputModePop(p);
13460       p->doXdgOpen = 0;
13461       sqlite3_sleep(100);
13462     }
13463 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
13464   }
13465   p->outfile[0] = 0;
13466   p->out = stdout;
13467 }
13468 
13469 /*
13470 ** Run an SQL command and return the single integer result.
13471 */
13472 static int db_int(ShellState *p, const char *zSql){
13473   sqlite3_stmt *pStmt;
13474   int res = 0;
13475   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13476   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
13477     res = sqlite3_column_int(pStmt,0);
13478   }
13479   sqlite3_finalize(pStmt);
13480   return res;
13481 }
13482 
13483 /*
13484 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
13485 */
13486 static unsigned int get2byteInt(unsigned char *a){
13487   return (a[0]<<8) + a[1];
13488 }
13489 static unsigned int get4byteInt(unsigned char *a){
13490   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
13491 }
13492 
13493 /*
13494 ** Implementation of the ".dbinfo" command.
13495 **
13496 ** Return 1 on error, 2 to exit, and 0 otherwise.
13497 */
13498 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
13499   static const struct { const char *zName; int ofst; } aField[] = {
13500      { "file change counter:",  24  },
13501      { "database page count:",  28  },
13502      { "freelist page count:",  36  },
13503      { "schema cookie:",        40  },
13504      { "schema format:",        44  },
13505      { "default cache size:",   48  },
13506      { "autovacuum top root:",  52  },
13507      { "incremental vacuum:",   64  },
13508      { "text encoding:",        56  },
13509      { "user version:",         60  },
13510      { "application id:",       68  },
13511      { "software version:",     96  },
13512   };
13513   static const struct { const char *zName; const char *zSql; } aQuery[] = {
13514      { "number of tables:",
13515        "SELECT count(*) FROM %s WHERE type='table'" },
13516      { "number of indexes:",
13517        "SELECT count(*) FROM %s WHERE type='index'" },
13518      { "number of triggers:",
13519        "SELECT count(*) FROM %s WHERE type='trigger'" },
13520      { "number of views:",
13521        "SELECT count(*) FROM %s WHERE type='view'" },
13522      { "schema size:",
13523        "SELECT total(length(sql)) FROM %s" },
13524   };
13525   int i, rc;
13526   unsigned iDataVersion;
13527   char *zSchemaTab;
13528   char *zDb = nArg>=2 ? azArg[1] : "main";
13529   sqlite3_stmt *pStmt = 0;
13530   unsigned char aHdr[100];
13531   open_db(p, 0);
13532   if( p->db==0 ) return 1;
13533   rc = sqlite3_prepare_v2(p->db,
13534              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
13535              -1, &pStmt, 0);
13536   if( rc ){
13537     if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
13538       utf8_printf(stderr, "the \".dbinfo\" command requires the "
13539                           "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
13540     }else{
13541       utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
13542     }
13543     sqlite3_finalize(pStmt);
13544     return 1;
13545   }
13546   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
13547   if( sqlite3_step(pStmt)==SQLITE_ROW
13548    && sqlite3_column_bytes(pStmt,0)>100
13549   ){
13550     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
13551     sqlite3_finalize(pStmt);
13552   }else{
13553     raw_printf(stderr, "unable to read database header\n");
13554     sqlite3_finalize(pStmt);
13555     return 1;
13556   }
13557   i = get2byteInt(aHdr+16);
13558   if( i==1 ) i = 65536;
13559   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
13560   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
13561   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
13562   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
13563   for(i=0; i<ArraySize(aField); i++){
13564     int ofst = aField[i].ofst;
13565     unsigned int val = get4byteInt(aHdr + ofst);
13566     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
13567     switch( ofst ){
13568       case 56: {
13569         if( val==1 ) raw_printf(p->out, " (utf8)");
13570         if( val==2 ) raw_printf(p->out, " (utf16le)");
13571         if( val==3 ) raw_printf(p->out, " (utf16be)");
13572       }
13573     }
13574     raw_printf(p->out, "\n");
13575   }
13576   if( zDb==0 ){
13577     zSchemaTab = sqlite3_mprintf("main.sqlite_master");
13578   }else if( strcmp(zDb,"temp")==0 ){
13579     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
13580   }else{
13581     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
13582   }
13583   for(i=0; i<ArraySize(aQuery); i++){
13584     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
13585     int val = db_int(p, zSql);
13586     sqlite3_free(zSql);
13587     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
13588   }
13589   sqlite3_free(zSchemaTab);
13590   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
13591   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
13592   return 0;
13593 }
13594 
13595 /*
13596 ** Print the current sqlite3_errmsg() value to stderr and return 1.
13597 */
13598 static int shellDatabaseError(sqlite3 *db){
13599   const char *zErr = sqlite3_errmsg(db);
13600   utf8_printf(stderr, "Error: %s\n", zErr);
13601   return 1;
13602 }
13603 
13604 /*
13605 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
13606 ** if they match and FALSE (0) if they do not match.
13607 **
13608 ** Globbing rules:
13609 **
13610 **      '*'       Matches any sequence of zero or more characters.
13611 **
13612 **      '?'       Matches exactly one character.
13613 **
13614 **     [...]      Matches one character from the enclosed list of
13615 **                characters.
13616 **
13617 **     [^...]     Matches one character not in the enclosed list.
13618 **
13619 **      '#'       Matches any sequence of one or more digits with an
13620 **                optional + or - sign in front
13621 **
13622 **      ' '       Any span of whitespace matches any other span of
13623 **                whitespace.
13624 **
13625 ** Extra whitespace at the end of z[] is ignored.
13626 */
13627 static int testcase_glob(const char *zGlob, const char *z){
13628   int c, c2;
13629   int invert;
13630   int seen;
13631 
13632   while( (c = (*(zGlob++)))!=0 ){
13633     if( IsSpace(c) ){
13634       if( !IsSpace(*z) ) return 0;
13635       while( IsSpace(*zGlob) ) zGlob++;
13636       while( IsSpace(*z) ) z++;
13637     }else if( c=='*' ){
13638       while( (c=(*(zGlob++))) == '*' || c=='?' ){
13639         if( c=='?' && (*(z++))==0 ) return 0;
13640       }
13641       if( c==0 ){
13642         return 1;
13643       }else if( c=='[' ){
13644         while( *z && testcase_glob(zGlob-1,z)==0 ){
13645           z++;
13646         }
13647         return (*z)!=0;
13648       }
13649       while( (c2 = (*(z++)))!=0 ){
13650         while( c2!=c ){
13651           c2 = *(z++);
13652           if( c2==0 ) return 0;
13653         }
13654         if( testcase_glob(zGlob,z) ) return 1;
13655       }
13656       return 0;
13657     }else if( c=='?' ){
13658       if( (*(z++))==0 ) return 0;
13659     }else if( c=='[' ){
13660       int prior_c = 0;
13661       seen = 0;
13662       invert = 0;
13663       c = *(z++);
13664       if( c==0 ) return 0;
13665       c2 = *(zGlob++);
13666       if( c2=='^' ){
13667         invert = 1;
13668         c2 = *(zGlob++);
13669       }
13670       if( c2==']' ){
13671         if( c==']' ) seen = 1;
13672         c2 = *(zGlob++);
13673       }
13674       while( c2 && c2!=']' ){
13675         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
13676           c2 = *(zGlob++);
13677           if( c>=prior_c && c<=c2 ) seen = 1;
13678           prior_c = 0;
13679         }else{
13680           if( c==c2 ){
13681             seen = 1;
13682           }
13683           prior_c = c2;
13684         }
13685         c2 = *(zGlob++);
13686       }
13687       if( c2==0 || (seen ^ invert)==0 ) return 0;
13688     }else if( c=='#' ){
13689       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
13690       if( !IsDigit(z[0]) ) return 0;
13691       z++;
13692       while( IsDigit(z[0]) ){ z++; }
13693     }else{
13694       if( c!=(*(z++)) ) return 0;
13695     }
13696   }
13697   while( IsSpace(*z) ){ z++; }
13698   return *z==0;
13699 }
13700 
13701 
13702 /*
13703 ** Compare the string as a command-line option with either one or two
13704 ** initial "-" characters.
13705 */
13706 static int optionMatch(const char *zStr, const char *zOpt){
13707   if( zStr[0]!='-' ) return 0;
13708   zStr++;
13709   if( zStr[0]=='-' ) zStr++;
13710   return strcmp(zStr, zOpt)==0;
13711 }
13712 
13713 /*
13714 ** Delete a file.
13715 */
13716 int shellDeleteFile(const char *zFilename){
13717   int rc;
13718 #ifdef _WIN32
13719   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
13720   rc = _wunlink(z);
13721   sqlite3_free(z);
13722 #else
13723   rc = unlink(zFilename);
13724 #endif
13725   return rc;
13726 }
13727 
13728 /*
13729 ** Try to delete the temporary file (if there is one) and free the
13730 ** memory used to hold the name of the temp file.
13731 */
13732 static void clearTempFile(ShellState *p){
13733   if( p->zTempFile==0 ) return;
13734   if( p->doXdgOpen ) return;
13735   if( shellDeleteFile(p->zTempFile) ) return;
13736   sqlite3_free(p->zTempFile);
13737   p->zTempFile = 0;
13738 }
13739 
13740 /*
13741 ** Create a new temp file name with the given suffix.
13742 */
13743 static void newTempFile(ShellState *p, const char *zSuffix){
13744   clearTempFile(p);
13745   sqlite3_free(p->zTempFile);
13746   p->zTempFile = 0;
13747   if( p->db ){
13748     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
13749   }
13750   if( p->zTempFile==0 ){
13751     sqlite3_uint64 r;
13752     sqlite3_randomness(sizeof(r), &r);
13753     p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
13754   }else{
13755     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
13756   }
13757   if( p->zTempFile==0 ){
13758     raw_printf(stderr, "out of memory\n");
13759     exit(1);
13760   }
13761 }
13762 
13763 
13764 /*
13765 ** The implementation of SQL scalar function fkey_collate_clause(), used
13766 ** by the ".lint fkey-indexes" command. This scalar function is always
13767 ** called with four arguments - the parent table name, the parent column name,
13768 ** the child table name and the child column name.
13769 **
13770 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
13771 **
13772 ** If either of the named tables or columns do not exist, this function
13773 ** returns an empty string. An empty string is also returned if both tables
13774 ** and columns exist but have the same default collation sequence. Or,
13775 ** if both exist but the default collation sequences are different, this
13776 ** function returns the string " COLLATE <parent-collation>", where
13777 ** <parent-collation> is the default collation sequence of the parent column.
13778 */
13779 static void shellFkeyCollateClause(
13780   sqlite3_context *pCtx,
13781   int nVal,
13782   sqlite3_value **apVal
13783 ){
13784   sqlite3 *db = sqlite3_context_db_handle(pCtx);
13785   const char *zParent;
13786   const char *zParentCol;
13787   const char *zParentSeq;
13788   const char *zChild;
13789   const char *zChildCol;
13790   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
13791   int rc;
13792 
13793   assert( nVal==4 );
13794   zParent = (const char*)sqlite3_value_text(apVal[0]);
13795   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
13796   zChild = (const char*)sqlite3_value_text(apVal[2]);
13797   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
13798 
13799   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
13800   rc = sqlite3_table_column_metadata(
13801       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
13802   );
13803   if( rc==SQLITE_OK ){
13804     rc = sqlite3_table_column_metadata(
13805         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
13806     );
13807   }
13808 
13809   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
13810     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
13811     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
13812     sqlite3_free(z);
13813   }
13814 }
13815 
13816 
13817 /*
13818 ** The implementation of dot-command ".lint fkey-indexes".
13819 */
13820 static int lintFkeyIndexes(
13821   ShellState *pState,             /* Current shell tool state */
13822   char **azArg,                   /* Array of arguments passed to dot command */
13823   int nArg                        /* Number of entries in azArg[] */
13824 ){
13825   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
13826   FILE *out = pState->out;        /* Stream to write non-error output to */
13827   int bVerbose = 0;               /* If -verbose is present */
13828   int bGroupByParent = 0;         /* If -groupbyparent is present */
13829   int i;                          /* To iterate through azArg[] */
13830   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
13831   int rc;                         /* Return code */
13832   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
13833 
13834   /*
13835   ** This SELECT statement returns one row for each foreign key constraint
13836   ** in the schema of the main database. The column values are:
13837   **
13838   ** 0. The text of an SQL statement similar to:
13839   **
13840   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
13841   **
13842   **    This SELECT is similar to the one that the foreign keys implementation
13843   **    needs to run internally on child tables. If there is an index that can
13844   **    be used to optimize this query, then it can also be used by the FK
13845   **    implementation to optimize DELETE or UPDATE statements on the parent
13846   **    table.
13847   **
13848   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
13849   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
13850   **    contains an index that can be used to optimize the query.
13851   **
13852   ** 2. Human readable text that describes the child table and columns. e.g.
13853   **
13854   **       "child_table(child_key1, child_key2)"
13855   **
13856   ** 3. Human readable text that describes the parent table and columns. e.g.
13857   **
13858   **       "parent_table(parent_key1, parent_key2)"
13859   **
13860   ** 4. A full CREATE INDEX statement for an index that could be used to
13861   **    optimize DELETE or UPDATE statements on the parent table. e.g.
13862   **
13863   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
13864   **
13865   ** 5. The name of the parent table.
13866   **
13867   ** These six values are used by the C logic below to generate the report.
13868   */
13869   const char *zSql =
13870   "SELECT "
13871     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
13872     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
13873     "  || fkey_collate_clause("
13874     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
13875     ", "
13876     "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
13877     "  || group_concat('*=?', ' AND ') || ')'"
13878     ", "
13879     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
13880     ", "
13881     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
13882     ", "
13883     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
13884     "  || ' ON ' || quote(s.name) || '('"
13885     "  || group_concat(quote(f.[from]) ||"
13886     "        fkey_collate_clause("
13887     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
13888     "  || ');'"
13889     ", "
13890     "     f.[table] "
13891     "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
13892     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
13893     "GROUP BY s.name, f.id "
13894     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
13895   ;
13896   const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
13897 
13898   for(i=2; i<nArg; i++){
13899     int n = strlen30(azArg[i]);
13900     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
13901       bVerbose = 1;
13902     }
13903     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
13904       bGroupByParent = 1;
13905       zIndent = "    ";
13906     }
13907     else{
13908       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
13909           azArg[0], azArg[1]
13910       );
13911       return SQLITE_ERROR;
13912     }
13913   }
13914 
13915   /* Register the fkey_collate_clause() SQL function */
13916   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
13917       0, shellFkeyCollateClause, 0, 0
13918   );
13919 
13920 
13921   if( rc==SQLITE_OK ){
13922     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
13923   }
13924   if( rc==SQLITE_OK ){
13925     sqlite3_bind_int(pSql, 1, bGroupByParent);
13926   }
13927 
13928   if( rc==SQLITE_OK ){
13929     int rc2;
13930     char *zPrev = 0;
13931     while( SQLITE_ROW==sqlite3_step(pSql) ){
13932       int res = -1;
13933       sqlite3_stmt *pExplain = 0;
13934       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
13935       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
13936       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
13937       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
13938       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
13939       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
13940 
13941       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13942       if( rc!=SQLITE_OK ) break;
13943       if( SQLITE_ROW==sqlite3_step(pExplain) ){
13944         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
13945         res = (
13946               0==sqlite3_strglob(zGlob, zPlan)
13947            || 0==sqlite3_strglob(zGlobIPK, zPlan)
13948         );
13949       }
13950       rc = sqlite3_finalize(pExplain);
13951       if( rc!=SQLITE_OK ) break;
13952 
13953       if( res<0 ){
13954         raw_printf(stderr, "Error: internal error");
13955         break;
13956       }else{
13957         if( bGroupByParent
13958         && (bVerbose || res==0)
13959         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
13960         ){
13961           raw_printf(out, "-- Parent table %s\n", zParent);
13962           sqlite3_free(zPrev);
13963           zPrev = sqlite3_mprintf("%s", zParent);
13964         }
13965 
13966         if( res==0 ){
13967           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
13968         }else if( bVerbose ){
13969           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
13970               zIndent, zFrom, zTarget
13971           );
13972         }
13973       }
13974     }
13975     sqlite3_free(zPrev);
13976 
13977     if( rc!=SQLITE_OK ){
13978       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
13979     }
13980 
13981     rc2 = sqlite3_finalize(pSql);
13982     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
13983       rc = rc2;
13984       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
13985     }
13986   }else{
13987     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
13988   }
13989 
13990   return rc;
13991 }
13992 
13993 /*
13994 ** Implementation of ".lint" dot command.
13995 */
13996 static int lintDotCommand(
13997   ShellState *pState,             /* Current shell tool state */
13998   char **azArg,                   /* Array of arguments passed to dot command */
13999   int nArg                        /* Number of entries in azArg[] */
14000 ){
14001   int n;
14002   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
14003   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
14004   return lintFkeyIndexes(pState, azArg, nArg);
14005 
14006  usage:
14007   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
14008   raw_printf(stderr, "Where sub-commands are:\n");
14009   raw_printf(stderr, "    fkey-indexes\n");
14010   return SQLITE_ERROR;
14011 }
14012 
14013 #if !defined SQLITE_OMIT_VIRTUALTABLE
14014 static void shellPrepare(
14015   sqlite3 *db,
14016   int *pRc,
14017   const char *zSql,
14018   sqlite3_stmt **ppStmt
14019 ){
14020   *ppStmt = 0;
14021   if( *pRc==SQLITE_OK ){
14022     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
14023     if( rc!=SQLITE_OK ){
14024       raw_printf(stderr, "sql error: %s (%d)\n",
14025           sqlite3_errmsg(db), sqlite3_errcode(db)
14026       );
14027       *pRc = rc;
14028     }
14029   }
14030 }
14031 
14032 /*
14033 ** Create a prepared statement using printf-style arguments for the SQL.
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 shellPreparePrintf(
14040   sqlite3 *db,
14041   int *pRc,
14042   sqlite3_stmt **ppStmt,
14043   const char *zFmt,
14044   ...
14045 ){
14046   *ppStmt = 0;
14047   if( *pRc==SQLITE_OK ){
14048     va_list ap;
14049     char *z;
14050     va_start(ap, zFmt);
14051     z = sqlite3_vmprintf(zFmt, ap);
14052     va_end(ap);
14053     if( z==0 ){
14054       *pRc = SQLITE_NOMEM;
14055     }else{
14056       shellPrepare(db, pRc, z, ppStmt);
14057       sqlite3_free(z);
14058     }
14059   }
14060 }
14061 
14062 /* Finalize the prepared statement created using shellPreparePrintf().
14063 **
14064 ** This routine is could be marked "static".  But it is not always used,
14065 ** depending on compile-time options.  By omitting the "static", we avoid
14066 ** nuisance compiler warnings about "defined but not used".
14067 */
14068 void shellFinalize(
14069   int *pRc,
14070   sqlite3_stmt *pStmt
14071 ){
14072   if( pStmt ){
14073     sqlite3 *db = sqlite3_db_handle(pStmt);
14074     int rc = sqlite3_finalize(pStmt);
14075     if( *pRc==SQLITE_OK ){
14076       if( rc!=SQLITE_OK ){
14077         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14078       }
14079       *pRc = rc;
14080     }
14081   }
14082 }
14083 
14084 /* Reset the prepared statement created using shellPreparePrintf().
14085 **
14086 ** This routine is could be marked "static".  But it is not always used,
14087 ** depending on compile-time options.  By omitting the "static", we avoid
14088 ** nuisance compiler warnings about "defined but not used".
14089 */
14090 void shellReset(
14091   int *pRc,
14092   sqlite3_stmt *pStmt
14093 ){
14094   int rc = sqlite3_reset(pStmt);
14095   if( *pRc==SQLITE_OK ){
14096     if( rc!=SQLITE_OK ){
14097       sqlite3 *db = sqlite3_db_handle(pStmt);
14098       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14099     }
14100     *pRc = rc;
14101   }
14102 }
14103 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
14104 
14105 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
14106 /******************************************************************************
14107 ** The ".archive" or ".ar" command.
14108 */
14109 /*
14110 ** Structure representing a single ".ar" command.
14111 */
14112 typedef struct ArCommand ArCommand;
14113 struct ArCommand {
14114   u8 eCmd;                        /* An AR_CMD_* value */
14115   u8 bVerbose;                    /* True if --verbose */
14116   u8 bZip;                        /* True if the archive is a ZIP */
14117   u8 bDryRun;                     /* True if --dry-run */
14118   u8 bAppend;                     /* True if --append */
14119   u8 fromCmdLine;                 /* Run from -A instead of .archive */
14120   int nArg;                       /* Number of command arguments */
14121   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
14122   const char *zFile;              /* --file argument, or NULL */
14123   const char *zDir;               /* --directory argument, or NULL */
14124   char **azArg;                   /* Array of command arguments */
14125   ShellState *p;                  /* Shell state */
14126   sqlite3 *db;                    /* Database containing the archive */
14127 };
14128 
14129 /*
14130 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
14131 */
14132 static int arUsage(FILE *f){
14133   showHelp(f,"archive");
14134   return SQLITE_ERROR;
14135 }
14136 
14137 /*
14138 ** Print an error message for the .ar command to stderr and return
14139 ** SQLITE_ERROR.
14140 */
14141 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
14142   va_list ap;
14143   char *z;
14144   va_start(ap, zFmt);
14145   z = sqlite3_vmprintf(zFmt, ap);
14146   va_end(ap);
14147   utf8_printf(stderr, "Error: %s\n", z);
14148   if( pAr->fromCmdLine ){
14149     utf8_printf(stderr, "Use \"-A\" for more help\n");
14150   }else{
14151     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
14152   }
14153   sqlite3_free(z);
14154   return SQLITE_ERROR;
14155 }
14156 
14157 /*
14158 ** Values for ArCommand.eCmd.
14159 */
14160 #define AR_CMD_CREATE       1
14161 #define AR_CMD_UPDATE       2
14162 #define AR_CMD_INSERT       3
14163 #define AR_CMD_EXTRACT      4
14164 #define AR_CMD_LIST         5
14165 #define AR_CMD_HELP         6
14166 
14167 /*
14168 ** Other (non-command) switches.
14169 */
14170 #define AR_SWITCH_VERBOSE     7
14171 #define AR_SWITCH_FILE        8
14172 #define AR_SWITCH_DIRECTORY   9
14173 #define AR_SWITCH_APPEND     10
14174 #define AR_SWITCH_DRYRUN     11
14175 
14176 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
14177   switch( eSwitch ){
14178     case AR_CMD_CREATE:
14179     case AR_CMD_EXTRACT:
14180     case AR_CMD_LIST:
14181     case AR_CMD_UPDATE:
14182     case AR_CMD_INSERT:
14183     case AR_CMD_HELP:
14184       if( pAr->eCmd ){
14185         return arErrorMsg(pAr, "multiple command options");
14186       }
14187       pAr->eCmd = eSwitch;
14188       break;
14189 
14190     case AR_SWITCH_DRYRUN:
14191       pAr->bDryRun = 1;
14192       break;
14193     case AR_SWITCH_VERBOSE:
14194       pAr->bVerbose = 1;
14195       break;
14196     case AR_SWITCH_APPEND:
14197       pAr->bAppend = 1;
14198       /* Fall thru into --file */
14199     case AR_SWITCH_FILE:
14200       pAr->zFile = zArg;
14201       break;
14202     case AR_SWITCH_DIRECTORY:
14203       pAr->zDir = zArg;
14204       break;
14205   }
14206 
14207   return SQLITE_OK;
14208 }
14209 
14210 /*
14211 ** Parse the command line for an ".ar" command. The results are written into
14212 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
14213 ** successfully, otherwise an error message is written to stderr and
14214 ** SQLITE_ERROR returned.
14215 */
14216 static int arParseCommand(
14217   char **azArg,                   /* Array of arguments passed to dot command */
14218   int nArg,                       /* Number of entries in azArg[] */
14219   ArCommand *pAr                  /* Populate this object */
14220 ){
14221   struct ArSwitch {
14222     const char *zLong;
14223     char cShort;
14224     u8 eSwitch;
14225     u8 bArg;
14226   } aSwitch[] = {
14227     { "create",    'c', AR_CMD_CREATE,       0 },
14228     { "extract",   'x', AR_CMD_EXTRACT,      0 },
14229     { "insert",    'i', AR_CMD_INSERT,       0 },
14230     { "list",      't', AR_CMD_LIST,         0 },
14231     { "update",    'u', AR_CMD_UPDATE,       0 },
14232     { "help",      'h', AR_CMD_HELP,         0 },
14233     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
14234     { "file",      'f', AR_SWITCH_FILE,      1 },
14235     { "append",    'a', AR_SWITCH_APPEND,    1 },
14236     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
14237     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
14238   };
14239   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
14240   struct ArSwitch *pEnd = &aSwitch[nSwitch];
14241 
14242   if( nArg<=1 ){
14243     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
14244     return arUsage(stderr);
14245   }else{
14246     char *z = azArg[1];
14247     if( z[0]!='-' ){
14248       /* Traditional style [tar] invocation */
14249       int i;
14250       int iArg = 2;
14251       for(i=0; z[i]; i++){
14252         const char *zArg = 0;
14253         struct ArSwitch *pOpt;
14254         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14255           if( z[i]==pOpt->cShort ) break;
14256         }
14257         if( pOpt==pEnd ){
14258           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14259         }
14260         if( pOpt->bArg ){
14261           if( iArg>=nArg ){
14262             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
14263           }
14264           zArg = azArg[iArg++];
14265         }
14266         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14267       }
14268       pAr->nArg = nArg-iArg;
14269       if( pAr->nArg>0 ){
14270         pAr->azArg = &azArg[iArg];
14271       }
14272     }else{
14273       /* Non-traditional invocation */
14274       int iArg;
14275       for(iArg=1; iArg<nArg; iArg++){
14276         int n;
14277         z = azArg[iArg];
14278         if( z[0]!='-' ){
14279           /* All remaining command line words are command arguments. */
14280           pAr->azArg = &azArg[iArg];
14281           pAr->nArg = nArg-iArg;
14282           break;
14283         }
14284         n = strlen30(z);
14285 
14286         if( z[1]!='-' ){
14287           int i;
14288           /* One or more short options */
14289           for(i=1; i<n; i++){
14290             const char *zArg = 0;
14291             struct ArSwitch *pOpt;
14292             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14293               if( z[i]==pOpt->cShort ) break;
14294             }
14295             if( pOpt==pEnd ){
14296               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14297             }
14298             if( pOpt->bArg ){
14299               if( i<(n-1) ){
14300                 zArg = &z[i+1];
14301                 i = n;
14302               }else{
14303                 if( iArg>=(nArg-1) ){
14304                   return arErrorMsg(pAr, "option requires an argument: %c",
14305                                     z[i]);
14306                 }
14307                 zArg = azArg[++iArg];
14308               }
14309             }
14310             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14311           }
14312         }else if( z[2]=='\0' ){
14313           /* A -- option, indicating that all remaining command line words
14314           ** are command arguments.  */
14315           pAr->azArg = &azArg[iArg+1];
14316           pAr->nArg = nArg-iArg-1;
14317           break;
14318         }else{
14319           /* A long option */
14320           const char *zArg = 0;             /* Argument for option, if any */
14321           struct ArSwitch *pMatch = 0;      /* Matching option */
14322           struct ArSwitch *pOpt;            /* Iterator */
14323           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14324             const char *zLong = pOpt->zLong;
14325             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
14326               if( pMatch ){
14327                 return arErrorMsg(pAr, "ambiguous option: %s",z);
14328               }else{
14329                 pMatch = pOpt;
14330               }
14331             }
14332           }
14333 
14334           if( pMatch==0 ){
14335             return arErrorMsg(pAr, "unrecognized option: %s", z);
14336           }
14337           if( pMatch->bArg ){
14338             if( iArg>=(nArg-1) ){
14339               return arErrorMsg(pAr, "option requires an argument: %s", z);
14340             }
14341             zArg = azArg[++iArg];
14342           }
14343           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
14344         }
14345       }
14346     }
14347   }
14348 
14349   return SQLITE_OK;
14350 }
14351 
14352 /*
14353 ** This function assumes that all arguments within the ArCommand.azArg[]
14354 ** array refer to archive members, as for the --extract or --list commands.
14355 ** It checks that each of them are present. If any specified file is not
14356 ** present in the archive, an error is printed to stderr and an error
14357 ** code returned. Otherwise, if all specified arguments are present in
14358 ** the archive, SQLITE_OK is returned.
14359 **
14360 ** This function strips any trailing '/' characters from each argument.
14361 ** This is consistent with the way the [tar] command seems to work on
14362 ** Linux.
14363 */
14364 static int arCheckEntries(ArCommand *pAr){
14365   int rc = SQLITE_OK;
14366   if( pAr->nArg ){
14367     int i, j;
14368     sqlite3_stmt *pTest = 0;
14369 
14370     shellPreparePrintf(pAr->db, &rc, &pTest,
14371         "SELECT name FROM %s WHERE name=$name",
14372         pAr->zSrcTable
14373     );
14374     j = sqlite3_bind_parameter_index(pTest, "$name");
14375     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14376       char *z = pAr->azArg[i];
14377       int n = strlen30(z);
14378       int bOk = 0;
14379       while( n>0 && z[n-1]=='/' ) n--;
14380       z[n] = '\0';
14381       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
14382       if( SQLITE_ROW==sqlite3_step(pTest) ){
14383         bOk = 1;
14384       }
14385       shellReset(&rc, pTest);
14386       if( rc==SQLITE_OK && bOk==0 ){
14387         utf8_printf(stderr, "not found in archive: %s\n", z);
14388         rc = SQLITE_ERROR;
14389       }
14390     }
14391     shellFinalize(&rc, pTest);
14392   }
14393   return rc;
14394 }
14395 
14396 /*
14397 ** Format a WHERE clause that can be used against the "sqlar" table to
14398 ** identify all archive members that match the command arguments held
14399 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
14400 ** The caller is responsible for eventually calling sqlite3_free() on
14401 ** any non-NULL (*pzWhere) value.
14402 */
14403 static void arWhereClause(
14404   int *pRc,
14405   ArCommand *pAr,
14406   char **pzWhere                  /* OUT: New WHERE clause */
14407 ){
14408   char *zWhere = 0;
14409   if( *pRc==SQLITE_OK ){
14410     if( pAr->nArg==0 ){
14411       zWhere = sqlite3_mprintf("1");
14412     }else{
14413       int i;
14414       const char *zSep = "";
14415       for(i=0; i<pAr->nArg; i++){
14416         const char *z = pAr->azArg[i];
14417         zWhere = sqlite3_mprintf(
14418           "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
14419           zWhere, zSep, z, strlen30(z)+1, z
14420         );
14421         if( zWhere==0 ){
14422           *pRc = SQLITE_NOMEM;
14423           break;
14424         }
14425         zSep = " OR ";
14426       }
14427     }
14428   }
14429   *pzWhere = zWhere;
14430 }
14431 
14432 /*
14433 ** Implementation of .ar "lisT" command.
14434 */
14435 static int arListCommand(ArCommand *pAr){
14436   const char *zSql = "SELECT %s FROM %s WHERE %s";
14437   const char *azCols[] = {
14438     "name",
14439     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
14440   };
14441 
14442   char *zWhere = 0;
14443   sqlite3_stmt *pSql = 0;
14444   int rc;
14445 
14446   rc = arCheckEntries(pAr);
14447   arWhereClause(&rc, pAr, &zWhere);
14448 
14449   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
14450                      pAr->zSrcTable, zWhere);
14451   if( pAr->bDryRun ){
14452     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14453   }else{
14454     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14455       if( pAr->bVerbose ){
14456         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
14457             sqlite3_column_text(pSql, 0),
14458             sqlite3_column_int(pSql, 1),
14459             sqlite3_column_text(pSql, 2),
14460             sqlite3_column_text(pSql, 3)
14461         );
14462       }else{
14463         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14464       }
14465     }
14466   }
14467   shellFinalize(&rc, pSql);
14468   sqlite3_free(zWhere);
14469   return rc;
14470 }
14471 
14472 
14473 /*
14474 ** Implementation of .ar "eXtract" command.
14475 */
14476 static int arExtractCommand(ArCommand *pAr){
14477   const char *zSql1 =
14478     "SELECT "
14479     " ($dir || name),"
14480     " writefile(($dir || name), %s, mode, mtime) "
14481     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
14482     " AND name NOT GLOB '*..[/\\]*'";
14483 
14484   const char *azExtraArg[] = {
14485     "sqlar_uncompress(data, sz)",
14486     "data"
14487   };
14488 
14489   sqlite3_stmt *pSql = 0;
14490   int rc = SQLITE_OK;
14491   char *zDir = 0;
14492   char *zWhere = 0;
14493   int i, j;
14494 
14495   /* If arguments are specified, check that they actually exist within
14496   ** the archive before proceeding. And formulate a WHERE clause to
14497   ** match them.  */
14498   rc = arCheckEntries(pAr);
14499   arWhereClause(&rc, pAr, &zWhere);
14500 
14501   if( rc==SQLITE_OK ){
14502     if( pAr->zDir ){
14503       zDir = sqlite3_mprintf("%s/", pAr->zDir);
14504     }else{
14505       zDir = sqlite3_mprintf("");
14506     }
14507     if( zDir==0 ) rc = SQLITE_NOMEM;
14508   }
14509 
14510   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
14511       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
14512   );
14513 
14514   if( rc==SQLITE_OK ){
14515     j = sqlite3_bind_parameter_index(pSql, "$dir");
14516     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
14517 
14518     /* Run the SELECT statement twice. The first time, writefile() is called
14519     ** for all archive members that should be extracted. The second time,
14520     ** only for the directories. This is because the timestamps for
14521     ** extracted directories must be reset after they are populated (as
14522     ** populating them changes the timestamp).  */
14523     for(i=0; i<2; i++){
14524       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
14525       sqlite3_bind_int(pSql, j, i);
14526       if( pAr->bDryRun ){
14527         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14528       }else{
14529         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14530           if( i==0 && pAr->bVerbose ){
14531             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14532           }
14533         }
14534       }
14535       shellReset(&rc, pSql);
14536     }
14537     shellFinalize(&rc, pSql);
14538   }
14539 
14540   sqlite3_free(zDir);
14541   sqlite3_free(zWhere);
14542   return rc;
14543 }
14544 
14545 /*
14546 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
14547 */
14548 static int arExecSql(ArCommand *pAr, const char *zSql){
14549   int rc;
14550   if( pAr->bDryRun ){
14551     utf8_printf(pAr->p->out, "%s\n", zSql);
14552     rc = SQLITE_OK;
14553   }else{
14554     char *zErr = 0;
14555     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
14556     if( zErr ){
14557       utf8_printf(stdout, "ERROR: %s\n", zErr);
14558       sqlite3_free(zErr);
14559     }
14560   }
14561   return rc;
14562 }
14563 
14564 
14565 /*
14566 ** Implementation of .ar "create", "insert", and "update" commands.
14567 **
14568 **     create    ->     Create a new SQL archive
14569 **     insert    ->     Insert or reinsert all files listed
14570 **     update    ->     Insert files that have changed or that were not
14571 **                      previously in the archive
14572 **
14573 ** Create the "sqlar" table in the database if it does not already exist.
14574 ** Then add each file in the azFile[] array to the archive. Directories
14575 ** are added recursively. If argument bVerbose is non-zero, a message is
14576 ** printed on stdout for each file archived.
14577 **
14578 ** The create command is the same as update, except that it drops
14579 ** any existing "sqlar" table before beginning.  The "insert" command
14580 ** always overwrites every file named on the command-line, where as
14581 ** "update" only overwrites if the size or mtime or mode has changed.
14582 */
14583 static int arCreateOrUpdateCommand(
14584   ArCommand *pAr,                 /* Command arguments and options */
14585   int bUpdate,                    /* true for a --create. */
14586   int bOnlyIfChanged              /* Only update if file has changed */
14587 ){
14588   const char *zCreate =
14589       "CREATE TABLE IF NOT EXISTS sqlar(\n"
14590       "  name TEXT PRIMARY KEY,  -- name of the file\n"
14591       "  mode INT,               -- access permissions\n"
14592       "  mtime INT,              -- last modification time\n"
14593       "  sz INT,                 -- original file size\n"
14594       "  data BLOB               -- compressed content\n"
14595       ")";
14596   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
14597   const char *zInsertFmt[2] = {
14598      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
14599      "  SELECT\n"
14600      "    %s,\n"
14601      "    mode,\n"
14602      "    mtime,\n"
14603      "    CASE substr(lsmode(mode),1,1)\n"
14604      "      WHEN '-' THEN length(data)\n"
14605      "      WHEN 'd' THEN 0\n"
14606      "      ELSE -1 END,\n"
14607      "    sqlar_compress(data)\n"
14608      "  FROM fsdir(%Q,%Q) AS disk\n"
14609      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14610      ,
14611      "REPLACE INTO %s(name,mode,mtime,data)\n"
14612      "  SELECT\n"
14613      "    %s,\n"
14614      "    mode,\n"
14615      "    mtime,\n"
14616      "    data\n"
14617      "  FROM fsdir(%Q,%Q) AS disk\n"
14618      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14619   };
14620   int i;                          /* For iterating through azFile[] */
14621   int rc;                         /* Return code */
14622   const char *zTab = 0;           /* SQL table into which to insert */
14623   char *zSql;
14624   char zTemp[50];
14625   char *zExists = 0;
14626 
14627   arExecSql(pAr, "PRAGMA page_size=512");
14628   rc = arExecSql(pAr, "SAVEPOINT ar;");
14629   if( rc!=SQLITE_OK ) return rc;
14630   zTemp[0] = 0;
14631   if( pAr->bZip ){
14632     /* Initialize the zipfile virtual table, if necessary */
14633     if( pAr->zFile ){
14634       sqlite3_uint64 r;
14635       sqlite3_randomness(sizeof(r),&r);
14636       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
14637       zTab = zTemp;
14638       zSql = sqlite3_mprintf(
14639          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
14640          zTab, pAr->zFile
14641       );
14642       rc = arExecSql(pAr, zSql);
14643       sqlite3_free(zSql);
14644     }else{
14645       zTab = "zip";
14646     }
14647   }else{
14648     /* Initialize the table for an SQLAR */
14649     zTab = "sqlar";
14650     if( bUpdate==0 ){
14651       rc = arExecSql(pAr, zDrop);
14652       if( rc!=SQLITE_OK ) goto end_ar_transaction;
14653     }
14654     rc = arExecSql(pAr, zCreate);
14655   }
14656   if( bOnlyIfChanged ){
14657     zExists = sqlite3_mprintf(
14658       " AND NOT EXISTS("
14659           "SELECT 1 FROM %s AS mem"
14660           " WHERE mem.name=disk.name"
14661           " AND mem.mtime=disk.mtime"
14662           " AND mem.mode=disk.mode)", zTab);
14663   }else{
14664     zExists = sqlite3_mprintf("");
14665   }
14666   if( zExists==0 ) rc = SQLITE_NOMEM;
14667   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14668     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
14669         pAr->bVerbose ? "shell_putsnl(name)" : "name",
14670         pAr->azArg[i], pAr->zDir, zExists);
14671     rc = arExecSql(pAr, zSql2);
14672     sqlite3_free(zSql2);
14673   }
14674 end_ar_transaction:
14675   if( rc!=SQLITE_OK ){
14676     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
14677   }else{
14678     rc = arExecSql(pAr, "RELEASE ar;");
14679     if( pAr->bZip && pAr->zFile ){
14680       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
14681       arExecSql(pAr, zSql);
14682       sqlite3_free(zSql);
14683     }
14684   }
14685   sqlite3_free(zExists);
14686   return rc;
14687 }
14688 
14689 /*
14690 ** Implementation of ".ar" dot command.
14691 */
14692 static int arDotCommand(
14693   ShellState *pState,          /* Current shell tool state */
14694   int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
14695   char **azArg,                /* Array of arguments passed to dot command */
14696   int nArg                     /* Number of entries in azArg[] */
14697 ){
14698   ArCommand cmd;
14699   int rc;
14700   memset(&cmd, 0, sizeof(cmd));
14701   cmd.fromCmdLine = fromCmdLine;
14702   rc = arParseCommand(azArg, nArg, &cmd);
14703   if( rc==SQLITE_OK ){
14704     int eDbType = SHELL_OPEN_UNSPEC;
14705     cmd.p = pState;
14706     cmd.db = pState->db;
14707     if( cmd.zFile ){
14708       eDbType = deduceDatabaseType(cmd.zFile, 1);
14709     }else{
14710       eDbType = pState->openMode;
14711     }
14712     if( eDbType==SHELL_OPEN_ZIPFILE ){
14713       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
14714         if( cmd.zFile==0 ){
14715           cmd.zSrcTable = sqlite3_mprintf("zip");
14716         }else{
14717           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
14718         }
14719       }
14720       cmd.bZip = 1;
14721     }else if( cmd.zFile ){
14722       int flags;
14723       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
14724       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
14725            || cmd.eCmd==AR_CMD_UPDATE ){
14726         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
14727       }else{
14728         flags = SQLITE_OPEN_READONLY;
14729       }
14730       cmd.db = 0;
14731       if( cmd.bDryRun ){
14732         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
14733              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
14734       }
14735       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
14736              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
14737       if( rc!=SQLITE_OK ){
14738         utf8_printf(stderr, "cannot open file: %s (%s)\n",
14739             cmd.zFile, sqlite3_errmsg(cmd.db)
14740         );
14741         goto end_ar_command;
14742       }
14743       sqlite3_fileio_init(cmd.db, 0, 0);
14744       sqlite3_sqlar_init(cmd.db, 0, 0);
14745       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
14746                               shellPutsFunc, 0, 0);
14747 
14748     }
14749     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
14750       if( cmd.eCmd!=AR_CMD_CREATE
14751        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
14752       ){
14753         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
14754         rc = SQLITE_ERROR;
14755         goto end_ar_command;
14756       }
14757       cmd.zSrcTable = sqlite3_mprintf("sqlar");
14758     }
14759 
14760     switch( cmd.eCmd ){
14761       case AR_CMD_CREATE:
14762         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
14763         break;
14764 
14765       case AR_CMD_EXTRACT:
14766         rc = arExtractCommand(&cmd);
14767         break;
14768 
14769       case AR_CMD_LIST:
14770         rc = arListCommand(&cmd);
14771         break;
14772 
14773       case AR_CMD_HELP:
14774         arUsage(pState->out);
14775         break;
14776 
14777       case AR_CMD_INSERT:
14778         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
14779         break;
14780 
14781       default:
14782         assert( cmd.eCmd==AR_CMD_UPDATE );
14783         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
14784         break;
14785     }
14786   }
14787 end_ar_command:
14788   if( cmd.db!=pState->db ){
14789     close_db(cmd.db);
14790   }
14791   sqlite3_free(cmd.zSrcTable);
14792 
14793   return rc;
14794 }
14795 /* End of the ".archive" or ".ar" command logic
14796 *******************************************************************************/
14797 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
14798 
14799 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14800 /*
14801 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
14802 ** Otherwise, the SQL statement or statements in zSql are executed using
14803 ** database connection db and the error code written to *pRc before
14804 ** this function returns.
14805 */
14806 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
14807   int rc = *pRc;
14808   if( rc==SQLITE_OK ){
14809     char *zErr = 0;
14810     rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
14811     if( rc!=SQLITE_OK ){
14812       raw_printf(stderr, "SQL error: %s\n", zErr);
14813     }
14814     *pRc = rc;
14815   }
14816 }
14817 
14818 /*
14819 ** Like shellExec(), except that zFmt is a printf() style format string.
14820 */
14821 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
14822   char *z = 0;
14823   if( *pRc==SQLITE_OK ){
14824     va_list ap;
14825     va_start(ap, zFmt);
14826     z = sqlite3_vmprintf(zFmt, ap);
14827     va_end(ap);
14828     if( z==0 ){
14829       *pRc = SQLITE_NOMEM;
14830     }else{
14831       shellExec(db, pRc, z);
14832     }
14833     sqlite3_free(z);
14834   }
14835 }
14836 
14837 /*
14838 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
14839 ** Otherwise, an attempt is made to allocate, zero and return a pointer
14840 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
14841 ** to SQLITE_NOMEM and NULL returned.
14842 */
14843 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
14844   void *pRet = 0;
14845   if( *pRc==SQLITE_OK ){
14846     pRet = sqlite3_malloc64(nByte);
14847     if( pRet==0 ){
14848       *pRc = SQLITE_NOMEM;
14849     }else{
14850       memset(pRet, 0, nByte);
14851     }
14852   }
14853   return pRet;
14854 }
14855 
14856 /*
14857 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
14858 ** Otherwise, zFmt is treated as a printf() style string. The result of
14859 ** formatting it along with any trailing arguments is written into a
14860 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
14861 ** It is the responsibility of the caller to eventually free this buffer
14862 ** using a call to sqlite3_free().
14863 **
14864 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
14865 ** pointer returned.
14866 */
14867 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
14868   char *z = 0;
14869   if( *pRc==SQLITE_OK ){
14870     va_list ap;
14871     va_start(ap, zFmt);
14872     z = sqlite3_vmprintf(zFmt, ap);
14873     va_end(ap);
14874     if( z==0 ){
14875       *pRc = SQLITE_NOMEM;
14876     }
14877   }
14878   return z;
14879 }
14880 
14881 /*
14882 ** When running the ".recover" command, each output table, and the special
14883 ** orphaned row table if it is required, is represented by an instance
14884 ** of the following struct.
14885 */
14886 typedef struct RecoverTable RecoverTable;
14887 struct RecoverTable {
14888   char *zQuoted;                  /* Quoted version of table name */
14889   int nCol;                       /* Number of columns in table */
14890   char **azlCol;                  /* Array of column lists */
14891   int iPk;                        /* Index of IPK column */
14892 };
14893 
14894 /*
14895 ** Free a RecoverTable object allocated by recoverFindTable() or
14896 ** recoverOrphanTable().
14897 */
14898 static void recoverFreeTable(RecoverTable *pTab){
14899   if( pTab ){
14900     sqlite3_free(pTab->zQuoted);
14901     if( pTab->azlCol ){
14902       int i;
14903       for(i=0; i<=pTab->nCol; i++){
14904         sqlite3_free(pTab->azlCol[i]);
14905       }
14906       sqlite3_free(pTab->azlCol);
14907     }
14908     sqlite3_free(pTab);
14909   }
14910 }
14911 
14912 /*
14913 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
14914 ** Otherwise, it allocates and returns a RecoverTable object based on the
14915 ** final four arguments passed to this function. It is the responsibility
14916 ** of the caller to eventually free the returned object using
14917 ** recoverFreeTable().
14918 */
14919 static RecoverTable *recoverNewTable(
14920   int *pRc,                       /* IN/OUT: Error code */
14921   const char *zName,              /* Name of table */
14922   const char *zSql,               /* CREATE TABLE statement */
14923   int bIntkey,
14924   int nCol
14925 ){
14926   sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
14927   int rc = *pRc;
14928   RecoverTable *pTab = 0;
14929 
14930   pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
14931   if( rc==SQLITE_OK ){
14932     int nSqlCol = 0;
14933     int bSqlIntkey = 0;
14934     sqlite3_stmt *pStmt = 0;
14935 
14936     rc = sqlite3_open("", &dbtmp);
14937     if( rc==SQLITE_OK ){
14938       sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
14939                               shellIdQuote, 0, 0);
14940     }
14941     if( rc==SQLITE_OK ){
14942       rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
14943     }
14944     if( rc==SQLITE_OK ){
14945       rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
14946       if( rc==SQLITE_ERROR ){
14947         rc = SQLITE_OK;
14948         goto finished;
14949       }
14950     }
14951     shellPreparePrintf(dbtmp, &rc, &pStmt,
14952         "SELECT count(*) FROM pragma_table_info(%Q)", zName
14953     );
14954     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
14955       nSqlCol = sqlite3_column_int(pStmt, 0);
14956     }
14957     shellFinalize(&rc, pStmt);
14958 
14959     if( rc!=SQLITE_OK || nSqlCol<nCol ){
14960       goto finished;
14961     }
14962 
14963     shellPreparePrintf(dbtmp, &rc, &pStmt,
14964       "SELECT ("
14965       "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
14966       ") FROM sqlite_master WHERE name = %Q", zName
14967     );
14968     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
14969       bSqlIntkey = sqlite3_column_int(pStmt, 0);
14970     }
14971     shellFinalize(&rc, pStmt);
14972 
14973     if( bIntkey==bSqlIntkey ){
14974       int i;
14975       const char *zPk = "_rowid_";
14976       sqlite3_stmt *pPkFinder = 0;
14977 
14978       /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
14979       ** set zPk to the name of the PK column, and pTab->iPk to the index
14980       ** of the column, where columns are 0-numbered from left to right.
14981       ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
14982       ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
14983       pTab->iPk = -2;
14984       if( bIntkey ){
14985         shellPreparePrintf(dbtmp, &rc, &pPkFinder,
14986           "SELECT cid, name FROM pragma_table_info(%Q) "
14987           "  WHERE pk=1 AND type='integer' COLLATE nocase"
14988           "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
14989           , zName, zName
14990         );
14991         if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
14992           pTab->iPk = sqlite3_column_int(pPkFinder, 0);
14993           zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
14994         }
14995       }
14996 
14997       pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
14998       pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
14999       pTab->nCol = nSqlCol;
15000 
15001       if( bIntkey ){
15002         pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
15003       }else{
15004         pTab->azlCol[0] = shellMPrintf(&rc, "");
15005       }
15006       i = 1;
15007       shellPreparePrintf(dbtmp, &rc, &pStmt,
15008           "SELECT %Q || group_concat(shell_idquote(name), ', ') "
15009           "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
15010           "FROM pragma_table_info(%Q)",
15011           bIntkey ? ", " : "", pTab->iPk,
15012           bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
15013           zName
15014       );
15015       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15016         const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
15017         pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
15018         i++;
15019       }
15020       shellFinalize(&rc, pStmt);
15021 
15022       shellFinalize(&rc, pPkFinder);
15023     }
15024   }
15025 
15026  finished:
15027   sqlite3_close(dbtmp);
15028   *pRc = rc;
15029   if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
15030     recoverFreeTable(pTab);
15031     pTab = 0;
15032   }
15033   return pTab;
15034 }
15035 
15036 /*
15037 ** This function is called to search the schema recovered from the
15038 ** sqlite_master table of the (possibly) corrupt database as part
15039 ** of a ".recover" command. Specifically, for a table with root page
15040 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
15041 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
15042 ** those.
15043 **
15044 ** If a table is found, a (RecoverTable*) object is returned. Or, if
15045 ** no such table is found, but bIntkey is false and iRoot is the
15046 ** root page of an index in the recovered schema, then (*pbNoop) is
15047 ** set to true and NULL returned. Or, if there is no such table or
15048 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
15049 ** the caller should write data to the orphans table.
15050 */
15051 static RecoverTable *recoverFindTable(
15052   ShellState *pState,             /* Shell state object */
15053   int *pRc,                       /* IN/OUT: Error code */
15054   int iRoot,                      /* Root page of table */
15055   int bIntkey,                    /* True for an intkey table */
15056   int nCol,                       /* Number of columns in table */
15057   int *pbNoop                     /* OUT: True if iRoot is root of index */
15058 ){
15059   sqlite3_stmt *pStmt = 0;
15060   RecoverTable *pRet = 0;
15061   int bNoop = 0;
15062   const char *zSql = 0;
15063   const char *zName = 0;
15064 
15065   /* Search the recovered schema for an object with root page iRoot. */
15066   shellPreparePrintf(pState->db, pRc, &pStmt,
15067       "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
15068   );
15069   while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15070     const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
15071     if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
15072       bNoop = 1;
15073       break;
15074     }
15075     if( sqlite3_stricmp(zType, "table")==0 ){
15076       zName = (const char*)sqlite3_column_text(pStmt, 1);
15077       zSql = (const char*)sqlite3_column_text(pStmt, 2);
15078       pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
15079       break;
15080     }
15081   }
15082 
15083   shellFinalize(pRc, pStmt);
15084   *pbNoop = bNoop;
15085   return pRet;
15086 }
15087 
15088 /*
15089 ** Return a RecoverTable object representing the orphans table.
15090 */
15091 static RecoverTable *recoverOrphanTable(
15092   ShellState *pState,             /* Shell state object */
15093   int *pRc,                       /* IN/OUT: Error code */
15094   const char *zLostAndFound,      /* Base name for orphans table */
15095   int nCol                        /* Number of user data columns */
15096 ){
15097   RecoverTable *pTab = 0;
15098   if( nCol>=0 && *pRc==SQLITE_OK ){
15099     int i;
15100 
15101     /* This block determines the name of the orphan table. The prefered
15102     ** name is zLostAndFound. But if that clashes with another name
15103     ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
15104     ** and so on until a non-clashing name is found.  */
15105     int iTab = 0;
15106     char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
15107     sqlite3_stmt *pTest = 0;
15108     shellPrepare(pState->db, pRc,
15109         "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
15110     );
15111     if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15112     while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
15113       shellReset(pRc, pTest);
15114       sqlite3_free(zTab);
15115       zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
15116       sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15117     }
15118     shellFinalize(pRc, pTest);
15119 
15120     pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
15121     if( pTab ){
15122       pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
15123       pTab->nCol = nCol;
15124       pTab->iPk = -2;
15125       if( nCol>0 ){
15126         pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
15127         if( pTab->azlCol ){
15128           pTab->azlCol[nCol] = shellMPrintf(pRc, "");
15129           for(i=nCol-1; i>=0; i--){
15130             pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
15131           }
15132         }
15133       }
15134 
15135       if( *pRc!=SQLITE_OK ){
15136         recoverFreeTable(pTab);
15137         pTab = 0;
15138       }else{
15139         raw_printf(pState->out,
15140             "CREATE TABLE %s(rootpgno INTEGER, "
15141             "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
15142         );
15143         for(i=0; i<nCol; i++){
15144           raw_printf(pState->out, ", c%d", i);
15145         }
15146         raw_printf(pState->out, ");\n");
15147       }
15148     }
15149     sqlite3_free(zTab);
15150   }
15151   return pTab;
15152 }
15153 
15154 /*
15155 ** This function is called to recover data from the database. A script
15156 ** to construct a new database containing all recovered data is output
15157 ** on stream pState->out.
15158 */
15159 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
15160   int rc = SQLITE_OK;
15161   sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
15162   sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
15163   sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
15164   const char *zRecoveryDb = "";   /* Name of "recovery" database */
15165   const char *zLostAndFound = "lost_and_found";
15166   int i;
15167   int nOrphan = -1;
15168   RecoverTable *pOrphan = 0;
15169 
15170   int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
15171   int bRowids = 1;                /* 0 if --no-rowids */
15172   for(i=1; i<nArg; i++){
15173     char *z = azArg[i];
15174     int n;
15175     if( z[0]=='-' && z[1]=='-' ) z++;
15176     n = strlen30(z);
15177     if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
15178       bFreelist = 0;
15179     }else
15180     if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
15181       i++;
15182       zRecoveryDb = azArg[i];
15183     }else
15184     if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
15185       i++;
15186       zLostAndFound = azArg[i];
15187     }else
15188     if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
15189       bRowids = 0;
15190     }
15191     else{
15192       utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
15193       showHelp(pState->out, azArg[0]);
15194       return 1;
15195     }
15196   }
15197 
15198   shellExecPrintf(pState->db, &rc,
15199     /* Attach an in-memory database named 'recovery'. Create an indexed
15200     ** cache of the sqlite_dbptr virtual table. */
15201     "PRAGMA writable_schema = on;"
15202     "ATTACH %Q AS recovery;"
15203     "DROP TABLE IF EXISTS recovery.dbptr;"
15204     "DROP TABLE IF EXISTS recovery.freelist;"
15205     "DROP TABLE IF EXISTS recovery.map;"
15206     "DROP TABLE IF EXISTS recovery.schema;"
15207     "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
15208   );
15209 
15210   if( bFreelist ){
15211     shellExec(pState->db, &rc,
15212       "WITH trunk(pgno) AS ("
15213       "  SELECT shell_int32("
15214       "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
15215       "      WHERE x>0"
15216       "    UNION"
15217       "  SELECT shell_int32("
15218       "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
15219       "      FROM trunk WHERE x>0"
15220       "),"
15221       "freelist(data, n, freepgno) AS ("
15222       "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
15223       "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
15224       "    UNION ALL"
15225       "  SELECT data, n-1, shell_int32(data, 2+n) "
15226       "      FROM freelist WHERE n>=0"
15227       ")"
15228       "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
15229     );
15230   }
15231 
15232   /* If this is an auto-vacuum database, add all pointer-map pages to
15233   ** the freelist table. Do this regardless of whether or not
15234   ** --freelist-corrupt was specified.  */
15235   shellExec(pState->db, &rc,
15236     "WITH ptrmap(pgno) AS ("
15237     "  SELECT 2 WHERE shell_int32("
15238     "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
15239     "  )"
15240     "    UNION ALL "
15241     "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
15242     "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
15243     ")"
15244     "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
15245   );
15246 
15247   shellExec(pState->db, &rc,
15248     "CREATE TABLE recovery.dbptr("
15249     "      pgno, child, PRIMARY KEY(child, pgno)"
15250     ") WITHOUT ROWID;"
15251     "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
15252     "    SELECT * FROM sqlite_dbptr"
15253     "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
15254 
15255     /* Delete any pointer to page 1. This ensures that page 1 is considered
15256     ** a root page, regardless of how corrupt the db is. */
15257     "DELETE FROM recovery.dbptr WHERE child = 1;"
15258 
15259     /* Delete all pointers to any pages that have more than one pointer
15260     ** to them. Such pages will be treated as root pages when recovering
15261     ** data.  */
15262     "DELETE FROM recovery.dbptr WHERE child IN ("
15263     "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
15264     ");"
15265 
15266     /* Create the "map" table that will (eventually) contain instructions
15267     ** for dealing with each page in the db that contains one or more
15268     ** records. */
15269     "CREATE TABLE recovery.map("
15270       "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
15271     ");"
15272 
15273     /* Populate table [map]. If there are circular loops of pages in the
15274     ** database, the following adds all pages in such a loop to the map
15275     ** as individual root pages. This could be handled better.  */
15276     "WITH pages(i, maxlen) AS ("
15277     "  SELECT page_count, ("
15278     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
15279     "  ) FROM pragma_page_count WHERE page_count>0"
15280     "    UNION ALL"
15281     "  SELECT i-1, ("
15282     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
15283     "  ) FROM pages WHERE i>=2"
15284     ")"
15285     "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
15286     "  SELECT i, maxlen, NULL, ("
15287     "    WITH p(orig, pgno, parent) AS ("
15288     "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
15289     "        UNION "
15290     "      SELECT i, p.parent, "
15291     "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
15292     "    )"
15293     "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
15294     ") "
15295     "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
15296     "UPDATE recovery.map AS o SET intkey = ("
15297     "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
15298     ");"
15299 
15300     /* Extract data from page 1 and any linked pages into table
15301     ** recovery.schema. With the same schema as an sqlite_master table.  */
15302     "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
15303     "INSERT INTO recovery.schema SELECT "
15304     "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
15305     "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
15306     "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
15307     "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
15308     "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
15309     "FROM sqlite_dbdata WHERE pgno IN ("
15310     "  SELECT pgno FROM recovery.map WHERE root=1"
15311     ")"
15312     "GROUP BY pgno, cell;"
15313     "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
15314   );
15315 
15316   /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
15317   ** CREATE TABLE statements that extracted from the existing schema.  */
15318   if( rc==SQLITE_OK ){
15319     sqlite3_stmt *pStmt = 0;
15320     /* ".recover" might output content in an order which causes immediate
15321     ** foreign key constraints to be violated. So disable foreign-key
15322     ** constraint enforcement to prevent problems when running the output
15323     ** script. */
15324     raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
15325     raw_printf(pState->out, "BEGIN;\n");
15326     raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
15327     shellPrepare(pState->db, &rc,
15328         "SELECT sql FROM recovery.schema "
15329         "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
15330     );
15331     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15332       const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
15333       raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
15334           &zCreateTable[12]
15335       );
15336     }
15337     shellFinalize(&rc, pStmt);
15338   }
15339 
15340   /* Figure out if an orphan table will be required. And if so, how many
15341   ** user columns it should contain */
15342   shellPrepare(pState->db, &rc,
15343       "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
15344       , &pLoop
15345   );
15346   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15347     nOrphan = sqlite3_column_int(pLoop, 0);
15348   }
15349   shellFinalize(&rc, pLoop);
15350   pLoop = 0;
15351 
15352   shellPrepare(pState->db, &rc,
15353       "SELECT pgno FROM recovery.map WHERE root=?", &pPages
15354   );
15355 
15356   shellPrepare(pState->db, &rc,
15357       "SELECT max(field), group_concat(shell_escape_crnl(quote"
15358       "(case when (? AND field<0) then NULL else value end)"
15359       "), ', ')"
15360       ", min(field) "
15361       "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
15362       "GROUP BY cell", &pCells
15363   );
15364 
15365   /* Loop through each root page. */
15366   shellPrepare(pState->db, &rc,
15367       "SELECT root, intkey, max(maxlen) FROM recovery.map"
15368       " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
15369       "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
15370       ")", &pLoop
15371   );
15372   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15373     int iRoot = sqlite3_column_int(pLoop, 0);
15374     int bIntkey = sqlite3_column_int(pLoop, 1);
15375     int nCol = sqlite3_column_int(pLoop, 2);
15376     int bNoop = 0;
15377     RecoverTable *pTab;
15378 
15379     assert( bIntkey==0 || bIntkey==1 );
15380     pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
15381     if( bNoop || rc ) continue;
15382     if( pTab==0 ){
15383       if( pOrphan==0 ){
15384         pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15385       }
15386       pTab = pOrphan;
15387       if( pTab==0 ) break;
15388     }
15389 
15390     if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
15391       raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
15392     }
15393     sqlite3_bind_int(pPages, 1, iRoot);
15394     if( bRowids==0 && pTab->iPk<0 ){
15395       sqlite3_bind_int(pCells, 1, 1);
15396     }else{
15397       sqlite3_bind_int(pCells, 1, 0);
15398     }
15399     sqlite3_bind_int(pCells, 3, pTab->iPk);
15400 
15401     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
15402       int iPgno = sqlite3_column_int(pPages, 0);
15403       sqlite3_bind_int(pCells, 2, iPgno);
15404       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
15405         int nField = sqlite3_column_int(pCells, 0);
15406         int iMin = sqlite3_column_int(pCells, 2);
15407         const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
15408 
15409         RecoverTable *pTab2 = pTab;
15410         if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
15411           if( pOrphan==0 ){
15412             pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15413           }
15414           pTab2 = pOrphan;
15415           if( pTab2==0 ) break;
15416         }
15417 
15418         nField = nField+1;
15419         if( pTab2==pOrphan ){
15420           raw_printf(pState->out,
15421               "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
15422               pTab2->zQuoted, iRoot, iPgno, nField,
15423               iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
15424           );
15425         }else{
15426           raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
15427               pTab2->zQuoted, pTab2->azlCol[nField], zVal
15428           );
15429         }
15430       }
15431       shellReset(&rc, pCells);
15432     }
15433     shellReset(&rc, pPages);
15434     if( pTab!=pOrphan ) recoverFreeTable(pTab);
15435   }
15436   shellFinalize(&rc, pLoop);
15437   shellFinalize(&rc, pPages);
15438   shellFinalize(&rc, pCells);
15439   recoverFreeTable(pOrphan);
15440 
15441   /* The rest of the schema */
15442   if( rc==SQLITE_OK ){
15443     sqlite3_stmt *pStmt = 0;
15444     shellPrepare(pState->db, &rc,
15445         "SELECT sql, name FROM recovery.schema "
15446         "WHERE sql NOT LIKE 'create table%'", &pStmt
15447     );
15448     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15449       const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
15450       if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
15451         const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
15452         char *zPrint = shellMPrintf(&rc,
15453           "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
15454           zName, zName, zSql
15455         );
15456         raw_printf(pState->out, "%s;\n", zPrint);
15457         sqlite3_free(zPrint);
15458       }else{
15459         raw_printf(pState->out, "%s;\n", zSql);
15460       }
15461     }
15462     shellFinalize(&rc, pStmt);
15463   }
15464 
15465   if( rc==SQLITE_OK ){
15466     raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
15467     raw_printf(pState->out, "COMMIT;\n");
15468   }
15469   sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
15470   return rc;
15471 }
15472 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15473 
15474 
15475 /*
15476 ** If an input line begins with "." then invoke this routine to
15477 ** process that line.
15478 **
15479 ** Return 1 on error, 2 to exit, and 0 otherwise.
15480 */
15481 static int do_meta_command(char *zLine, ShellState *p){
15482   int h = 1;
15483   int nArg = 0;
15484   int n, c;
15485   int rc = 0;
15486   char *azArg[52];
15487 
15488 #ifndef SQLITE_OMIT_VIRTUALTABLE
15489   if( p->expert.pExpert ){
15490     expertFinish(p, 1, 0);
15491   }
15492 #endif
15493 
15494   /* Parse the input line into tokens.
15495   */
15496   while( zLine[h] && nArg<ArraySize(azArg)-1 ){
15497     while( IsSpace(zLine[h]) ){ h++; }
15498     if( zLine[h]==0 ) break;
15499     if( zLine[h]=='\'' || zLine[h]=='"' ){
15500       int delim = zLine[h++];
15501       azArg[nArg++] = &zLine[h];
15502       while( zLine[h] && zLine[h]!=delim ){
15503         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
15504         h++;
15505       }
15506       if( zLine[h]==delim ){
15507         zLine[h++] = 0;
15508       }
15509       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
15510     }else{
15511       azArg[nArg++] = &zLine[h];
15512       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
15513       if( zLine[h] ) zLine[h++] = 0;
15514       resolve_backslashes(azArg[nArg-1]);
15515     }
15516   }
15517   azArg[nArg] = 0;
15518 
15519   /* Process the input line.
15520   */
15521   if( nArg==0 ) return 0; /* no tokens, no error */
15522   n = strlen30(azArg[0]);
15523   c = azArg[0][0];
15524   clearTempFile(p);
15525 
15526 #ifndef SQLITE_OMIT_AUTHORIZATION
15527   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
15528     if( nArg!=2 ){
15529       raw_printf(stderr, "Usage: .auth ON|OFF\n");
15530       rc = 1;
15531       goto meta_command_exit;
15532     }
15533     open_db(p, 0);
15534     if( booleanValue(azArg[1]) ){
15535       sqlite3_set_authorizer(p->db, shellAuth, p);
15536     }else{
15537       sqlite3_set_authorizer(p->db, 0, 0);
15538     }
15539   }else
15540 #endif
15541 
15542 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15543   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
15544     open_db(p, 0);
15545     rc = arDotCommand(p, 0, azArg, nArg);
15546   }else
15547 #endif
15548 
15549   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
15550    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
15551   ){
15552     const char *zDestFile = 0;
15553     const char *zDb = 0;
15554     sqlite3 *pDest;
15555     sqlite3_backup *pBackup;
15556     int j;
15557     int bAsync = 0;
15558     const char *zVfs = 0;
15559     for(j=1; j<nArg; j++){
15560       const char *z = azArg[j];
15561       if( z[0]=='-' ){
15562         if( z[1]=='-' ) z++;
15563         if( strcmp(z, "-append")==0 ){
15564           zVfs = "apndvfs";
15565         }else
15566         if( strcmp(z, "-async")==0 ){
15567           bAsync = 1;
15568         }else
15569         {
15570           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
15571           return 1;
15572         }
15573       }else if( zDestFile==0 ){
15574         zDestFile = azArg[j];
15575       }else if( zDb==0 ){
15576         zDb = zDestFile;
15577         zDestFile = azArg[j];
15578       }else{
15579         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
15580         return 1;
15581       }
15582     }
15583     if( zDestFile==0 ){
15584       raw_printf(stderr, "missing FILENAME argument on .backup\n");
15585       return 1;
15586     }
15587     if( zDb==0 ) zDb = "main";
15588     rc = sqlite3_open_v2(zDestFile, &pDest,
15589                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
15590     if( rc!=SQLITE_OK ){
15591       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
15592       close_db(pDest);
15593       return 1;
15594     }
15595     if( bAsync ){
15596       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
15597                    0, 0, 0);
15598     }
15599     open_db(p, 0);
15600     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
15601     if( pBackup==0 ){
15602       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15603       close_db(pDest);
15604       return 1;
15605     }
15606     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
15607     sqlite3_backup_finish(pBackup);
15608     if( rc==SQLITE_DONE ){
15609       rc = 0;
15610     }else{
15611       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15612       rc = 1;
15613     }
15614     close_db(pDest);
15615   }else
15616 
15617   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
15618     if( nArg==2 ){
15619       bail_on_error = booleanValue(azArg[1]);
15620     }else{
15621       raw_printf(stderr, "Usage: .bail on|off\n");
15622       rc = 1;
15623     }
15624   }else
15625 
15626   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
15627     if( nArg==2 ){
15628       if( booleanValue(azArg[1]) ){
15629         setBinaryMode(p->out, 1);
15630       }else{
15631         setTextMode(p->out, 1);
15632       }
15633     }else{
15634       raw_printf(stderr, "Usage: .binary on|off\n");
15635       rc = 1;
15636     }
15637   }else
15638 
15639   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
15640     if( nArg==2 ){
15641 #if defined(_WIN32) || defined(WIN32)
15642       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
15643       rc = !SetCurrentDirectoryW(z);
15644       sqlite3_free(z);
15645 #else
15646       rc = chdir(azArg[1]);
15647 #endif
15648       if( rc ){
15649         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
15650         rc = 1;
15651       }
15652     }else{
15653       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
15654       rc = 1;
15655     }
15656   }else
15657 
15658   /* The undocumented ".breakpoint" command causes a call to the no-op
15659   ** routine named test_breakpoint().
15660   */
15661   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
15662     test_breakpoint();
15663   }else
15664 
15665   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
15666     if( nArg==2 ){
15667       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
15668     }else{
15669       raw_printf(stderr, "Usage: .changes on|off\n");
15670       rc = 1;
15671     }
15672   }else
15673 
15674   /* Cancel output redirection, if it is currently set (by .testcase)
15675   ** Then read the content of the testcase-out.txt file and compare against
15676   ** azArg[1].  If there are differences, report an error and exit.
15677   */
15678   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
15679     char *zRes = 0;
15680     output_reset(p);
15681     if( nArg!=2 ){
15682       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
15683       rc = 2;
15684     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
15685       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
15686       rc = 2;
15687     }else if( testcase_glob(azArg[1],zRes)==0 ){
15688       utf8_printf(stderr,
15689                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
15690                  p->zTestcase, azArg[1], zRes);
15691       rc = 1;
15692     }else{
15693       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
15694       p->nCheck++;
15695     }
15696     sqlite3_free(zRes);
15697   }else
15698 
15699   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
15700     if( nArg==2 ){
15701       tryToClone(p, azArg[1]);
15702     }else{
15703       raw_printf(stderr, "Usage: .clone FILENAME\n");
15704       rc = 1;
15705     }
15706   }else
15707 
15708   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
15709     ShellState data;
15710     char *zErrMsg = 0;
15711     open_db(p, 0);
15712     memcpy(&data, p, sizeof(data));
15713     data.showHeader = 0;
15714     data.cMode = data.mode = MODE_List;
15715     sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
15716     data.cnt = 0;
15717     sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
15718                  callback, &data, &zErrMsg);
15719     if( zErrMsg ){
15720       utf8_printf(stderr,"Error: %s\n", zErrMsg);
15721       sqlite3_free(zErrMsg);
15722       rc = 1;
15723     }
15724   }else
15725 
15726   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
15727     static const struct DbConfigChoices {
15728       const char *zName;
15729       int op;
15730     } aDbConfig[] = {
15731         { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
15732         { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
15733         { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
15734         { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
15735         { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
15736         { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
15737         { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
15738         { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
15739         { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
15740         { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
15741         { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
15742         { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
15743         { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
15744         { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
15745         { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
15746         { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
15747     };
15748     int ii, v;
15749     open_db(p, 0);
15750     for(ii=0; ii<ArraySize(aDbConfig); ii++){
15751       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
15752       if( nArg>=3 ){
15753         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
15754       }
15755       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
15756       utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
15757       if( nArg>1 ) break;
15758     }
15759     if( nArg>1 && ii==ArraySize(aDbConfig) ){
15760       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
15761       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
15762     }
15763   }else
15764 
15765   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
15766     rc = shell_dbinfo_command(p, nArg, azArg);
15767   }else
15768 
15769 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15770   if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
15771     open_db(p, 0);
15772     rc = recoverDatabaseCmd(p, nArg, azArg);
15773   }else
15774 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15775 
15776   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
15777     const char *zLike = 0;
15778     int i;
15779     int savedShowHeader = p->showHeader;
15780     int savedShellFlags = p->shellFlgs;
15781     ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
15782     for(i=1; i<nArg; i++){
15783       if( azArg[i][0]=='-' ){
15784         const char *z = azArg[i]+1;
15785         if( z[0]=='-' ) z++;
15786         if( strcmp(z,"preserve-rowids")==0 ){
15787 #ifdef SQLITE_OMIT_VIRTUALTABLE
15788           raw_printf(stderr, "The --preserve-rowids option is not compatible"
15789                              " with SQLITE_OMIT_VIRTUALTABLE\n");
15790           rc = 1;
15791           goto meta_command_exit;
15792 #else
15793           ShellSetFlag(p, SHFLG_PreserveRowid);
15794 #endif
15795         }else
15796         if( strcmp(z,"newlines")==0 ){
15797           ShellSetFlag(p, SHFLG_Newlines);
15798         }else
15799         {
15800           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
15801           rc = 1;
15802           goto meta_command_exit;
15803         }
15804       }else if( zLike ){
15805         raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
15806                            "?--newlines? ?LIKE-PATTERN?\n");
15807         rc = 1;
15808         goto meta_command_exit;
15809       }else{
15810         zLike = azArg[i];
15811       }
15812     }
15813 
15814     open_db(p, 0);
15815 
15816     /* When playing back a "dump", the content might appear in an order
15817     ** which causes immediate foreign key constraints to be violated.
15818     ** So disable foreign-key constraint enforcement to prevent problems. */
15819     raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
15820     raw_printf(p->out, "BEGIN TRANSACTION;\n");
15821     p->writableSchema = 0;
15822     p->showHeader = 0;
15823     /* Set writable_schema=ON since doing so forces SQLite to initialize
15824     ** as much of the schema as it can even if the sqlite_master table is
15825     ** corrupt. */
15826     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
15827     p->nErr = 0;
15828     if( zLike==0 ){
15829       run_schema_dump_query(p,
15830         "SELECT name, type, sql FROM sqlite_master "
15831         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
15832       );
15833       run_schema_dump_query(p,
15834         "SELECT name, type, sql FROM sqlite_master "
15835         "WHERE name=='sqlite_sequence'"
15836       );
15837       run_table_dump_query(p,
15838         "SELECT sql FROM sqlite_master "
15839         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
15840       );
15841     }else{
15842       char *zSql;
15843       zSql = sqlite3_mprintf(
15844         "SELECT name, type, sql FROM sqlite_master "
15845         "WHERE tbl_name LIKE %Q AND type=='table'"
15846         "  AND sql NOT NULL", zLike);
15847       run_schema_dump_query(p,zSql);
15848       sqlite3_free(zSql);
15849       zSql = sqlite3_mprintf(
15850         "SELECT sql FROM sqlite_master "
15851         "WHERE sql NOT NULL"
15852         "  AND type IN ('index','trigger','view')"
15853         "  AND tbl_name LIKE %Q", zLike);
15854       run_table_dump_query(p, zSql, 0);
15855       sqlite3_free(zSql);
15856     }
15857     if( p->writableSchema ){
15858       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
15859       p->writableSchema = 0;
15860     }
15861     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
15862     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
15863     raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
15864     p->showHeader = savedShowHeader;
15865     p->shellFlgs = savedShellFlags;
15866   }else
15867 
15868   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
15869     if( nArg==2 ){
15870       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
15871     }else{
15872       raw_printf(stderr, "Usage: .echo on|off\n");
15873       rc = 1;
15874     }
15875   }else
15876 
15877   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
15878     if( nArg==2 ){
15879       p->autoEQPtest = 0;
15880       if( p->autoEQPtrace ){
15881         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
15882         p->autoEQPtrace = 0;
15883       }
15884       if( strcmp(azArg[1],"full")==0 ){
15885         p->autoEQP = AUTOEQP_full;
15886       }else if( strcmp(azArg[1],"trigger")==0 ){
15887         p->autoEQP = AUTOEQP_trigger;
15888 #ifdef SQLITE_DEBUG
15889       }else if( strcmp(azArg[1],"test")==0 ){
15890         p->autoEQP = AUTOEQP_on;
15891         p->autoEQPtest = 1;
15892       }else if( strcmp(azArg[1],"trace")==0 ){
15893         p->autoEQP = AUTOEQP_full;
15894         p->autoEQPtrace = 1;
15895         open_db(p, 0);
15896         sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
15897         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
15898 #endif
15899       }else{
15900         p->autoEQP = (u8)booleanValue(azArg[1]);
15901       }
15902     }else{
15903       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
15904       rc = 1;
15905     }
15906   }else
15907 
15908   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
15909     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
15910     rc = 2;
15911   }else
15912 
15913   /* The ".explain" command is automatic now.  It is largely pointless.  It
15914   ** retained purely for backwards compatibility */
15915   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
15916     int val = 1;
15917     if( nArg>=2 ){
15918       if( strcmp(azArg[1],"auto")==0 ){
15919         val = 99;
15920       }else{
15921         val =  booleanValue(azArg[1]);
15922       }
15923     }
15924     if( val==1 && p->mode!=MODE_Explain ){
15925       p->normalMode = p->mode;
15926       p->mode = MODE_Explain;
15927       p->autoExplain = 0;
15928     }else if( val==0 ){
15929       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
15930       p->autoExplain = 0;
15931     }else if( val==99 ){
15932       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
15933       p->autoExplain = 1;
15934     }
15935   }else
15936 
15937 #ifndef SQLITE_OMIT_VIRTUALTABLE
15938   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
15939     open_db(p, 0);
15940     expertDotCommand(p, azArg, nArg);
15941   }else
15942 #endif
15943 
15944   if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
15945     static const struct {
15946        const char *zCtrlName;   /* Name of a test-control option */
15947        int ctrlCode;            /* Integer code for that option */
15948        const char *zUsage;      /* Usage notes */
15949     } aCtrl[] = {
15950       { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
15951       { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
15952    /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
15953       { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
15954       { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
15955    /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
15956       { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
15957       { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
15958       { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
15959     };
15960     int filectrl = -1;
15961     int iCtrl = -1;
15962     sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
15963     int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
15964     int n2, i;
15965     const char *zCmd = 0;
15966 
15967     open_db(p, 0);
15968     zCmd = nArg>=2 ? azArg[1] : "help";
15969 
15970     /* The argument can optionally begin with "-" or "--" */
15971     if( zCmd[0]=='-' && zCmd[1] ){
15972       zCmd++;
15973       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15974     }
15975 
15976     /* --help lists all file-controls */
15977     if( strcmp(zCmd,"help")==0 ){
15978       utf8_printf(p->out, "Available file-controls:\n");
15979       for(i=0; i<ArraySize(aCtrl); i++){
15980         utf8_printf(p->out, "  .filectrl %s %s\n",
15981                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15982       }
15983       rc = 1;
15984       goto meta_command_exit;
15985     }
15986 
15987     /* convert filectrl text option to value. allow any unique prefix
15988     ** of the option name, or a numerical value. */
15989     n2 = strlen30(zCmd);
15990     for(i=0; i<ArraySize(aCtrl); i++){
15991       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15992         if( filectrl<0 ){
15993           filectrl = aCtrl[i].ctrlCode;
15994           iCtrl = i;
15995         }else{
15996           utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
15997                               "Use \".filectrl --help\" for help\n", zCmd);
15998           rc = 1;
15999           goto meta_command_exit;
16000         }
16001       }
16002     }
16003     if( filectrl<0 ){
16004       utf8_printf(stderr,"Error: unknown file-control: %s\n"
16005                          "Use \".filectrl --help\" for help\n", zCmd);
16006     }else{
16007       switch(filectrl){
16008         case SQLITE_FCNTL_SIZE_LIMIT: {
16009           if( nArg!=2 && nArg!=3 ) break;
16010           iRes = nArg==3 ? integerValue(azArg[2]) : -1;
16011           sqlite3_file_control(p->db, 0, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
16012           isOk = 1;
16013           break;
16014         }
16015         case SQLITE_FCNTL_LOCK_TIMEOUT:
16016         case SQLITE_FCNTL_CHUNK_SIZE: {
16017           int x;
16018           if( nArg!=3 ) break;
16019           x = (int)integerValue(azArg[2]);
16020           sqlite3_file_control(p->db, 0, filectrl, &x);
16021           isOk = 2;
16022           break;
16023         }
16024         case SQLITE_FCNTL_PERSIST_WAL:
16025         case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
16026           int x;
16027           if( nArg!=2 && nArg!=3 ) break;
16028           x = nArg==3 ? booleanValue(azArg[2]) : -1;
16029           sqlite3_file_control(p->db, 0, filectrl, &x);
16030           iRes = x;
16031           isOk = 1;
16032           break;
16033         }
16034         case SQLITE_FCNTL_HAS_MOVED: {
16035           int x;
16036           if( nArg!=2 ) break;
16037           sqlite3_file_control(p->db, 0, filectrl, &x);
16038           iRes = x;
16039           isOk = 1;
16040           break;
16041         }
16042         case SQLITE_FCNTL_TEMPFILENAME: {
16043           char *z = 0;
16044           if( nArg!=2 ) break;
16045           sqlite3_file_control(p->db, 0, filectrl, &z);
16046           if( z ){
16047             utf8_printf(p->out, "%s\n", z);
16048             sqlite3_free(z);
16049           }
16050           isOk = 2;
16051           break;
16052         }
16053       }
16054     }
16055     if( isOk==0 && iCtrl>=0 ){
16056       utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
16057       rc = 1;
16058     }else if( isOk==1 ){
16059       char zBuf[100];
16060       sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
16061       raw_printf(p->out, "%s\n", zBuf);
16062     }
16063   }else
16064 
16065   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
16066     ShellState data;
16067     char *zErrMsg = 0;
16068     int doStats = 0;
16069     memcpy(&data, p, sizeof(data));
16070     data.showHeader = 0;
16071     data.cMode = data.mode = MODE_Semi;
16072     if( nArg==2 && optionMatch(azArg[1], "indent") ){
16073       data.cMode = data.mode = MODE_Pretty;
16074       nArg = 1;
16075     }
16076     if( nArg!=1 ){
16077       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
16078       rc = 1;
16079       goto meta_command_exit;
16080     }
16081     open_db(p, 0);
16082     rc = sqlite3_exec(p->db,
16083        "SELECT sql FROM"
16084        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
16085        "     FROM sqlite_master UNION ALL"
16086        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
16087        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
16088        "ORDER BY rowid",
16089        callback, &data, &zErrMsg
16090     );
16091     if( rc==SQLITE_OK ){
16092       sqlite3_stmt *pStmt;
16093       rc = sqlite3_prepare_v2(p->db,
16094                "SELECT rowid FROM sqlite_master"
16095                " WHERE name GLOB 'sqlite_stat[134]'",
16096                -1, &pStmt, 0);
16097       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
16098       sqlite3_finalize(pStmt);
16099     }
16100     if( doStats==0 ){
16101       raw_printf(p->out, "/* No STAT tables available */\n");
16102     }else{
16103       raw_printf(p->out, "ANALYZE sqlite_master;\n");
16104       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
16105                    callback, &data, &zErrMsg);
16106       data.cMode = data.mode = MODE_Insert;
16107       data.zDestTable = "sqlite_stat1";
16108       shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
16109       data.zDestTable = "sqlite_stat4";
16110       shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
16111       raw_printf(p->out, "ANALYZE sqlite_master;\n");
16112     }
16113   }else
16114 
16115   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
16116     if( nArg==2 ){
16117       p->showHeader = booleanValue(azArg[1]);
16118     }else{
16119       raw_printf(stderr, "Usage: .headers on|off\n");
16120       rc = 1;
16121     }
16122   }else
16123 
16124   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
16125     if( nArg>=2 ){
16126       n = showHelp(p->out, azArg[1]);
16127       if( n==0 ){
16128         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
16129       }
16130     }else{
16131       showHelp(p->out, 0);
16132     }
16133   }else
16134 
16135   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
16136     char *zTable;               /* Insert data into this table */
16137     char *zFile;                /* Name of file to extra content from */
16138     sqlite3_stmt *pStmt = NULL; /* A statement */
16139     int nCol;                   /* Number of columns in the table */
16140     int nByte;                  /* Number of bytes in an SQL string */
16141     int i, j;                   /* Loop counters */
16142     int needCommit;             /* True to COMMIT or ROLLBACK at end */
16143     int nSep;                   /* Number of bytes in p->colSeparator[] */
16144     char *zSql;                 /* An SQL statement */
16145     ImportCtx sCtx;             /* Reader context */
16146     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
16147     int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
16148 
16149     if( nArg!=3 ){
16150       raw_printf(stderr, "Usage: .import FILE TABLE\n");
16151       goto meta_command_exit;
16152     }
16153     zFile = azArg[1];
16154     zTable = azArg[2];
16155     seenInterrupt = 0;
16156     memset(&sCtx, 0, sizeof(sCtx));
16157     open_db(p, 0);
16158     nSep = strlen30(p->colSeparator);
16159     if( nSep==0 ){
16160       raw_printf(stderr,
16161                  "Error: non-null column separator required for import\n");
16162       return 1;
16163     }
16164     if( nSep>1 ){
16165       raw_printf(stderr, "Error: multi-character column separators not allowed"
16166                       " for import\n");
16167       return 1;
16168     }
16169     nSep = strlen30(p->rowSeparator);
16170     if( nSep==0 ){
16171       raw_printf(stderr, "Error: non-null row separator required for import\n");
16172       return 1;
16173     }
16174     if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
16175       /* When importing CSV (only), if the row separator is set to the
16176       ** default output row separator, change it to the default input
16177       ** row separator.  This avoids having to maintain different input
16178       ** and output row separators. */
16179       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16180       nSep = strlen30(p->rowSeparator);
16181     }
16182     if( nSep>1 ){
16183       raw_printf(stderr, "Error: multi-character row separators not allowed"
16184                       " for import\n");
16185       return 1;
16186     }
16187     sCtx.zFile = zFile;
16188     sCtx.nLine = 1;
16189     if( sCtx.zFile[0]=='|' ){
16190 #ifdef SQLITE_OMIT_POPEN
16191       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16192       return 1;
16193 #else
16194       sCtx.in = popen(sCtx.zFile+1, "r");
16195       sCtx.zFile = "<pipe>";
16196       xCloser = pclose;
16197 #endif
16198     }else{
16199       sCtx.in = fopen(sCtx.zFile, "rb");
16200       xCloser = fclose;
16201     }
16202     if( p->mode==MODE_Ascii ){
16203       xRead = ascii_read_one_field;
16204     }else{
16205       xRead = csv_read_one_field;
16206     }
16207     if( sCtx.in==0 ){
16208       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16209       return 1;
16210     }
16211     sCtx.cColSep = p->colSeparator[0];
16212     sCtx.cRowSep = p->rowSeparator[0];
16213     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
16214     if( zSql==0 ){
16215       xCloser(sCtx.in);
16216       shell_out_of_memory();
16217     }
16218     nByte = strlen30(zSql);
16219     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16220     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
16221     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
16222       char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
16223       char cSep = '(';
16224       while( xRead(&sCtx) ){
16225         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
16226         cSep = ',';
16227         if( sCtx.cTerm!=sCtx.cColSep ) break;
16228       }
16229       if( cSep=='(' ){
16230         sqlite3_free(zCreate);
16231         sqlite3_free(sCtx.z);
16232         xCloser(sCtx.in);
16233         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
16234         return 1;
16235       }
16236       zCreate = sqlite3_mprintf("%z\n)", zCreate);
16237       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
16238       sqlite3_free(zCreate);
16239       if( rc ){
16240         utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
16241                 sqlite3_errmsg(p->db));
16242         sqlite3_free(sCtx.z);
16243         xCloser(sCtx.in);
16244         return 1;
16245       }
16246       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16247     }
16248     sqlite3_free(zSql);
16249     if( rc ){
16250       if (pStmt) sqlite3_finalize(pStmt);
16251       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
16252       xCloser(sCtx.in);
16253       return 1;
16254     }
16255     nCol = sqlite3_column_count(pStmt);
16256     sqlite3_finalize(pStmt);
16257     pStmt = 0;
16258     if( nCol==0 ) return 0; /* no columns, no error */
16259     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
16260     if( zSql==0 ){
16261       xCloser(sCtx.in);
16262       shell_out_of_memory();
16263     }
16264     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
16265     j = strlen30(zSql);
16266     for(i=1; i<nCol; i++){
16267       zSql[j++] = ',';
16268       zSql[j++] = '?';
16269     }
16270     zSql[j++] = ')';
16271     zSql[j] = 0;
16272     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16273     sqlite3_free(zSql);
16274     if( rc ){
16275       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16276       if (pStmt) sqlite3_finalize(pStmt);
16277       xCloser(sCtx.in);
16278       return 1;
16279     }
16280     needCommit = sqlite3_get_autocommit(p->db);
16281     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
16282     do{
16283       int startLine = sCtx.nLine;
16284       for(i=0; i<nCol; i++){
16285         char *z = xRead(&sCtx);
16286         /*
16287         ** Did we reach end-of-file before finding any columns?
16288         ** If so, stop instead of NULL filling the remaining columns.
16289         */
16290         if( z==0 && i==0 ) break;
16291         /*
16292         ** Did we reach end-of-file OR end-of-line before finding any
16293         ** columns in ASCII mode?  If so, stop instead of NULL filling
16294         ** the remaining columns.
16295         */
16296         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
16297         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
16298         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
16299           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16300                           "filling the rest with NULL\n",
16301                           sCtx.zFile, startLine, nCol, i+1);
16302           i += 2;
16303           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
16304         }
16305       }
16306       if( sCtx.cTerm==sCtx.cColSep ){
16307         do{
16308           xRead(&sCtx);
16309           i++;
16310         }while( sCtx.cTerm==sCtx.cColSep );
16311         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16312                         "extras ignored\n",
16313                         sCtx.zFile, startLine, nCol, i);
16314       }
16315       if( i>=nCol ){
16316         sqlite3_step(pStmt);
16317         rc = sqlite3_reset(pStmt);
16318         if( rc!=SQLITE_OK ){
16319           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
16320                       startLine, sqlite3_errmsg(p->db));
16321         }
16322       }
16323     }while( sCtx.cTerm!=EOF );
16324 
16325     xCloser(sCtx.in);
16326     sqlite3_free(sCtx.z);
16327     sqlite3_finalize(pStmt);
16328     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
16329   }else
16330 
16331 #ifndef SQLITE_UNTESTABLE
16332   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
16333     char *zSql;
16334     char *zCollist = 0;
16335     sqlite3_stmt *pStmt;
16336     int tnum = 0;
16337     int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
16338     int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
16339     int i;
16340     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
16341       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
16342                           "       .imposter off\n");
16343       /* Also allowed, but not documented:
16344       **
16345       **    .imposter TABLE IMPOSTER
16346       **
16347       ** where TABLE is a WITHOUT ROWID table.  In that case, the
16348       ** imposter is another WITHOUT ROWID table with the columns in
16349       ** storage order. */
16350       rc = 1;
16351       goto meta_command_exit;
16352     }
16353     open_db(p, 0);
16354     if( nArg==2 ){
16355       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
16356       goto meta_command_exit;
16357     }
16358     zSql = sqlite3_mprintf(
16359       "SELECT rootpage, 0 FROM sqlite_master"
16360       " WHERE name='%q' AND type='index'"
16361       "UNION ALL "
16362       "SELECT rootpage, 1 FROM sqlite_master"
16363       " WHERE name='%q' AND type='table'"
16364       "   AND sql LIKE '%%without%%rowid%%'",
16365       azArg[1], azArg[1]
16366     );
16367     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16368     sqlite3_free(zSql);
16369     if( sqlite3_step(pStmt)==SQLITE_ROW ){
16370       tnum = sqlite3_column_int(pStmt, 0);
16371       isWO = sqlite3_column_int(pStmt, 1);
16372     }
16373     sqlite3_finalize(pStmt);
16374     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
16375     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16376     sqlite3_free(zSql);
16377     i = 0;
16378     while( sqlite3_step(pStmt)==SQLITE_ROW ){
16379       char zLabel[20];
16380       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
16381       i++;
16382       if( zCol==0 ){
16383         if( sqlite3_column_int(pStmt,1)==-1 ){
16384           zCol = "_ROWID_";
16385         }else{
16386           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
16387           zCol = zLabel;
16388         }
16389       }
16390       if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
16391         lenPK = (int)strlen(zCollist);
16392       }
16393       if( zCollist==0 ){
16394         zCollist = sqlite3_mprintf("\"%w\"", zCol);
16395       }else{
16396         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
16397       }
16398     }
16399     sqlite3_finalize(pStmt);
16400     if( i==0 || tnum==0 ){
16401       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
16402       rc = 1;
16403       sqlite3_free(zCollist);
16404       goto meta_command_exit;
16405     }
16406     if( lenPK==0 ) lenPK = 100000;
16407     zSql = sqlite3_mprintf(
16408           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
16409           azArg[2], zCollist, lenPK, zCollist);
16410     sqlite3_free(zCollist);
16411     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
16412     if( rc==SQLITE_OK ){
16413       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
16414       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
16415       if( rc ){
16416         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
16417       }else{
16418         utf8_printf(stdout, "%s;\n", zSql);
16419         raw_printf(stdout,
16420           "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
16421           azArg[1], isWO ? "table" : "index"
16422         );
16423       }
16424     }else{
16425       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
16426       rc = 1;
16427     }
16428     sqlite3_free(zSql);
16429   }else
16430 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
16431 
16432 #ifdef SQLITE_ENABLE_IOTRACE
16433   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
16434     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
16435     if( iotrace && iotrace!=stdout ) fclose(iotrace);
16436     iotrace = 0;
16437     if( nArg<2 ){
16438       sqlite3IoTrace = 0;
16439     }else if( strcmp(azArg[1], "-")==0 ){
16440       sqlite3IoTrace = iotracePrintf;
16441       iotrace = stdout;
16442     }else{
16443       iotrace = fopen(azArg[1], "w");
16444       if( iotrace==0 ){
16445         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
16446         sqlite3IoTrace = 0;
16447         rc = 1;
16448       }else{
16449         sqlite3IoTrace = iotracePrintf;
16450       }
16451     }
16452   }else
16453 #endif
16454 
16455   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
16456     static const struct {
16457        const char *zLimitName;   /* Name of a limit */
16458        int limitCode;            /* Integer code for that limit */
16459     } aLimit[] = {
16460       { "length",                SQLITE_LIMIT_LENGTH                    },
16461       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
16462       { "column",                SQLITE_LIMIT_COLUMN                    },
16463       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
16464       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
16465       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
16466       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
16467       { "attached",              SQLITE_LIMIT_ATTACHED                  },
16468       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
16469       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
16470       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
16471       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
16472     };
16473     int i, n2;
16474     open_db(p, 0);
16475     if( nArg==1 ){
16476       for(i=0; i<ArraySize(aLimit); i++){
16477         printf("%20s %d\n", aLimit[i].zLimitName,
16478                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
16479       }
16480     }else if( nArg>3 ){
16481       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
16482       rc = 1;
16483       goto meta_command_exit;
16484     }else{
16485       int iLimit = -1;
16486       n2 = strlen30(azArg[1]);
16487       for(i=0; i<ArraySize(aLimit); i++){
16488         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
16489           if( iLimit<0 ){
16490             iLimit = i;
16491           }else{
16492             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
16493             rc = 1;
16494             goto meta_command_exit;
16495           }
16496         }
16497       }
16498       if( iLimit<0 ){
16499         utf8_printf(stderr, "unknown limit: \"%s\"\n"
16500                         "enter \".limits\" with no arguments for a list.\n",
16501                          azArg[1]);
16502         rc = 1;
16503         goto meta_command_exit;
16504       }
16505       if( nArg==3 ){
16506         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
16507                       (int)integerValue(azArg[2]));
16508       }
16509       printf("%20s %d\n", aLimit[iLimit].zLimitName,
16510              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
16511     }
16512   }else
16513 
16514   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
16515     open_db(p, 0);
16516     lintDotCommand(p, azArg, nArg);
16517   }else
16518 
16519 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16520   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
16521     const char *zFile, *zProc;
16522     char *zErrMsg = 0;
16523     if( nArg<2 ){
16524       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
16525       rc = 1;
16526       goto meta_command_exit;
16527     }
16528     zFile = azArg[1];
16529     zProc = nArg>=3 ? azArg[2] : 0;
16530     open_db(p, 0);
16531     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
16532     if( rc!=SQLITE_OK ){
16533       utf8_printf(stderr, "Error: %s\n", zErrMsg);
16534       sqlite3_free(zErrMsg);
16535       rc = 1;
16536     }
16537   }else
16538 #endif
16539 
16540   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
16541     if( nArg!=2 ){
16542       raw_printf(stderr, "Usage: .log FILENAME\n");
16543       rc = 1;
16544     }else{
16545       const char *zFile = azArg[1];
16546       output_file_close(p->pLog);
16547       p->pLog = output_file_open(zFile, 0);
16548     }
16549   }else
16550 
16551   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
16552     const char *zMode = nArg>=2 ? azArg[1] : "";
16553     int n2 = strlen30(zMode);
16554     int c2 = zMode[0];
16555     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
16556       p->mode = MODE_Line;
16557       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16558     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
16559       p->mode = MODE_Column;
16560       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16561     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
16562       p->mode = MODE_List;
16563       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
16564       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16565     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
16566       p->mode = MODE_Html;
16567     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
16568       p->mode = MODE_Tcl;
16569       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
16570       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16571     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
16572       p->mode = MODE_Csv;
16573       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
16574       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
16575     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
16576       p->mode = MODE_List;
16577       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
16578     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
16579       p->mode = MODE_Insert;
16580       set_table_name(p, nArg>=3 ? azArg[2] : "table");
16581     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
16582       p->mode = MODE_Quote;
16583     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
16584       p->mode = MODE_Ascii;
16585       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
16586       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
16587     }else if( nArg==1 ){
16588       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
16589     }else{
16590       raw_printf(stderr, "Error: mode should be one of: "
16591          "ascii column csv html insert line list quote tabs tcl\n");
16592       rc = 1;
16593     }
16594     p->cMode = p->mode;
16595   }else
16596 
16597   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
16598     if( nArg==2 ){
16599       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
16600                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
16601     }else{
16602       raw_printf(stderr, "Usage: .nullvalue STRING\n");
16603       rc = 1;
16604     }
16605   }else
16606 
16607   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
16608     char *zNewFilename;  /* Name of the database file to open */
16609     int iName = 1;       /* Index in azArg[] of the filename */
16610     int newFlag = 0;     /* True to delete file before opening */
16611     /* Close the existing database */
16612     session_close_all(p);
16613     close_db(p->db);
16614     p->db = 0;
16615     p->zDbFilename = 0;
16616     sqlite3_free(p->zFreeOnClose);
16617     p->zFreeOnClose = 0;
16618     p->openMode = SHELL_OPEN_UNSPEC;
16619     p->openFlags = 0;
16620     p->szMax = 0;
16621     /* Check for command-line arguments */
16622     for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
16623       const char *z = azArg[iName];
16624       if( optionMatch(z,"new") ){
16625         newFlag = 1;
16626 #ifdef SQLITE_HAVE_ZLIB
16627       }else if( optionMatch(z, "zip") ){
16628         p->openMode = SHELL_OPEN_ZIPFILE;
16629 #endif
16630       }else if( optionMatch(z, "append") ){
16631         p->openMode = SHELL_OPEN_APPENDVFS;
16632       }else if( optionMatch(z, "readonly") ){
16633         p->openMode = SHELL_OPEN_READONLY;
16634       }else if( optionMatch(z, "nofollow") ){
16635         p->openFlags |= SQLITE_OPEN_NOFOLLOW;
16636 #ifdef SQLITE_ENABLE_DESERIALIZE
16637       }else if( optionMatch(z, "deserialize") ){
16638         p->openMode = SHELL_OPEN_DESERIALIZE;
16639       }else if( optionMatch(z, "hexdb") ){
16640         p->openMode = SHELL_OPEN_HEXDB;
16641       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
16642         p->szMax = integerValue(azArg[++iName]);
16643 #endif /* SQLITE_ENABLE_DESERIALIZE */
16644       }else if( z[0]=='-' ){
16645         utf8_printf(stderr, "unknown option: %s\n", z);
16646         rc = 1;
16647         goto meta_command_exit;
16648       }
16649     }
16650     /* If a filename is specified, try to open it first */
16651     zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
16652     if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
16653       if( newFlag ) shellDeleteFile(zNewFilename);
16654       p->zDbFilename = zNewFilename;
16655       open_db(p, OPEN_DB_KEEPALIVE);
16656       if( p->db==0 ){
16657         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
16658         sqlite3_free(zNewFilename);
16659       }else{
16660         p->zFreeOnClose = zNewFilename;
16661       }
16662     }
16663     if( p->db==0 ){
16664       /* As a fall-back open a TEMP database */
16665       p->zDbFilename = 0;
16666       open_db(p, 0);
16667     }
16668   }else
16669 
16670   if( (c=='o'
16671         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
16672    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
16673   ){
16674     const char *zFile = nArg>=2 ? azArg[1] : "stdout";
16675     int bTxtMode = 0;
16676     if( azArg[0][0]=='e' ){
16677       /* Transform the ".excel" command into ".once -x" */
16678       nArg = 2;
16679       azArg[0] = "once";
16680       zFile = azArg[1] = "-x";
16681       n = 4;
16682     }
16683     if( nArg>2 ){
16684       utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
16685       rc = 1;
16686       goto meta_command_exit;
16687     }
16688     if( n>1 && strncmp(azArg[0], "once", n)==0 ){
16689       if( nArg<2 ){
16690         raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
16691         rc = 1;
16692         goto meta_command_exit;
16693       }
16694       p->outCount = 2;
16695     }else{
16696       p->outCount = 0;
16697     }
16698     output_reset(p);
16699     if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
16700 #ifndef SQLITE_NOHAVE_SYSTEM
16701     if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
16702       p->doXdgOpen = 1;
16703       outputModePush(p);
16704       if( zFile[1]=='x' ){
16705         newTempFile(p, "csv");
16706         p->mode = MODE_Csv;
16707         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
16708         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
16709       }else{
16710         newTempFile(p, "txt");
16711         bTxtMode = 1;
16712       }
16713       zFile = p->zTempFile;
16714     }
16715 #endif /* SQLITE_NOHAVE_SYSTEM */
16716     if( zFile[0]=='|' ){
16717 #ifdef SQLITE_OMIT_POPEN
16718       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16719       rc = 1;
16720       p->out = stdout;
16721 #else
16722       p->out = popen(zFile + 1, "w");
16723       if( p->out==0 ){
16724         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
16725         p->out = stdout;
16726         rc = 1;
16727       }else{
16728         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
16729       }
16730 #endif
16731     }else{
16732       p->out = output_file_open(zFile, bTxtMode);
16733       if( p->out==0 ){
16734         if( strcmp(zFile,"off")!=0 ){
16735           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
16736         }
16737         p->out = stdout;
16738         rc = 1;
16739       } else {
16740         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
16741       }
16742     }
16743   }else
16744 
16745   if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
16746     open_db(p,0);
16747     if( nArg<=1 ) goto parameter_syntax_error;
16748 
16749     /* .parameter clear
16750     ** Clear all bind parameters by dropping the TEMP table that holds them.
16751     */
16752     if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
16753       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
16754                    0, 0, 0);
16755     }else
16756 
16757     /* .parameter list
16758     ** List all bind parameters.
16759     */
16760     if( nArg==2 && strcmp(azArg[1],"list")==0 ){
16761       sqlite3_stmt *pStmt = 0;
16762       int rx;
16763       int len = 0;
16764       rx = sqlite3_prepare_v2(p->db,
16765              "SELECT max(length(key)) "
16766              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
16767       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
16768         len = sqlite3_column_int(pStmt, 0);
16769         if( len>40 ) len = 40;
16770       }
16771       sqlite3_finalize(pStmt);
16772       pStmt = 0;
16773       if( len ){
16774         rx = sqlite3_prepare_v2(p->db,
16775              "SELECT key, quote(value) "
16776              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
16777         while( sqlite3_step(pStmt)==SQLITE_ROW ){
16778           utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
16779                       sqlite3_column_text(pStmt,1));
16780         }
16781         sqlite3_finalize(pStmt);
16782       }
16783     }else
16784 
16785     /* .parameter init
16786     ** Make sure the TEMP table used to hold bind parameters exists.
16787     ** Create it if necessary.
16788     */
16789     if( nArg==2 && strcmp(azArg[1],"init")==0 ){
16790       bind_table_init(p);
16791     }else
16792 
16793     /* .parameter set NAME VALUE
16794     ** Set or reset a bind parameter.  NAME should be the full parameter
16795     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
16796     ** VALUE can be in either SQL literal notation, or if not it will be
16797     ** understood to be a text string.
16798     */
16799     if( nArg==4 && strcmp(azArg[1],"set")==0 ){
16800       int rx;
16801       char *zSql;
16802       sqlite3_stmt *pStmt;
16803       const char *zKey = azArg[2];
16804       const char *zValue = azArg[3];
16805       bind_table_init(p);
16806       zSql = sqlite3_mprintf(
16807                   "REPLACE INTO temp.sqlite_parameters(key,value)"
16808                   "VALUES(%Q,%s);", zKey, zValue);
16809       if( zSql==0 ) shell_out_of_memory();
16810       pStmt = 0;
16811       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16812       sqlite3_free(zSql);
16813       if( rx!=SQLITE_OK ){
16814         sqlite3_finalize(pStmt);
16815         pStmt = 0;
16816         zSql = sqlite3_mprintf(
16817                    "REPLACE INTO temp.sqlite_parameters(key,value)"
16818                    "VALUES(%Q,%Q);", zKey, zValue);
16819         if( zSql==0 ) shell_out_of_memory();
16820         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16821         sqlite3_free(zSql);
16822         if( rx!=SQLITE_OK ){
16823           utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
16824           sqlite3_finalize(pStmt);
16825           pStmt = 0;
16826           rc = 1;
16827         }
16828       }
16829       sqlite3_step(pStmt);
16830       sqlite3_finalize(pStmt);
16831     }else
16832 
16833     /* .parameter unset NAME
16834     ** Remove the NAME binding from the parameter binding table, if it
16835     ** exists.
16836     */
16837     if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
16838       char *zSql = sqlite3_mprintf(
16839           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
16840       if( zSql==0 ) shell_out_of_memory();
16841       sqlite3_exec(p->db, zSql, 0, 0, 0);
16842       sqlite3_free(zSql);
16843     }else
16844     /* If no command name matches, show a syntax error */
16845     parameter_syntax_error:
16846     showHelp(p->out, "parameter");
16847   }else
16848 
16849   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
16850     int i;
16851     for(i=1; i<nArg; i++){
16852       if( i>1 ) raw_printf(p->out, " ");
16853       utf8_printf(p->out, "%s", azArg[i]);
16854     }
16855     raw_printf(p->out, "\n");
16856   }else
16857 
16858 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16859   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
16860     int i;
16861     int nn = 0;
16862     p->flgProgress = 0;
16863     p->mxProgress = 0;
16864     p->nProgress = 0;
16865     for(i=1; i<nArg; i++){
16866       const char *z = azArg[i];
16867       if( z[0]=='-' ){
16868         z++;
16869         if( z[0]=='-' ) z++;
16870         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
16871           p->flgProgress |= SHELL_PROGRESS_QUIET;
16872           continue;
16873         }
16874         if( strcmp(z,"reset")==0 ){
16875           p->flgProgress |= SHELL_PROGRESS_RESET;
16876           continue;
16877         }
16878         if( strcmp(z,"once")==0 ){
16879           p->flgProgress |= SHELL_PROGRESS_ONCE;
16880           continue;
16881         }
16882         if( strcmp(z,"limit")==0 ){
16883           if( i+1>=nArg ){
16884             utf8_printf(stderr, "Error: missing argument on --limit\n");
16885             rc = 1;
16886             goto meta_command_exit;
16887           }else{
16888             p->mxProgress = (int)integerValue(azArg[++i]);
16889           }
16890           continue;
16891         }
16892         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
16893         rc = 1;
16894         goto meta_command_exit;
16895       }else{
16896         nn = (int)integerValue(z);
16897       }
16898     }
16899     open_db(p, 0);
16900     sqlite3_progress_handler(p->db, nn, progress_handler, p);
16901   }else
16902 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
16903 
16904   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
16905     if( nArg >= 2) {
16906       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
16907     }
16908     if( nArg >= 3) {
16909       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
16910     }
16911   }else
16912 
16913   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
16914     rc = 2;
16915   }else
16916 
16917   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
16918     FILE *inSaved = p->in;
16919     int savedLineno = p->lineno;
16920     if( nArg!=2 ){
16921       raw_printf(stderr, "Usage: .read FILE\n");
16922       rc = 1;
16923       goto meta_command_exit;
16924     }
16925     p->in = fopen(azArg[1], "rb");
16926     if( p->in==0 ){
16927       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
16928       rc = 1;
16929     }else{
16930       rc = process_input(p);
16931       fclose(p->in);
16932     }
16933     p->in = inSaved;
16934     p->lineno = savedLineno;
16935   }else
16936 
16937   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
16938     const char *zSrcFile;
16939     const char *zDb;
16940     sqlite3 *pSrc;
16941     sqlite3_backup *pBackup;
16942     int nTimeout = 0;
16943 
16944     if( nArg==2 ){
16945       zSrcFile = azArg[1];
16946       zDb = "main";
16947     }else if( nArg==3 ){
16948       zSrcFile = azArg[2];
16949       zDb = azArg[1];
16950     }else{
16951       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
16952       rc = 1;
16953       goto meta_command_exit;
16954     }
16955     rc = sqlite3_open(zSrcFile, &pSrc);
16956     if( rc!=SQLITE_OK ){
16957       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
16958       close_db(pSrc);
16959       return 1;
16960     }
16961     open_db(p, 0);
16962     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
16963     if( pBackup==0 ){
16964       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16965       close_db(pSrc);
16966       return 1;
16967     }
16968     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
16969           || rc==SQLITE_BUSY  ){
16970       if( rc==SQLITE_BUSY ){
16971         if( nTimeout++ >= 3 ) break;
16972         sqlite3_sleep(100);
16973       }
16974     }
16975     sqlite3_backup_finish(pBackup);
16976     if( rc==SQLITE_DONE ){
16977       rc = 0;
16978     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
16979       raw_printf(stderr, "Error: source database is busy\n");
16980       rc = 1;
16981     }else{
16982       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16983       rc = 1;
16984     }
16985     close_db(pSrc);
16986   }else
16987 
16988   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
16989     if( nArg==2 ){
16990       p->scanstatsOn = (u8)booleanValue(azArg[1]);
16991 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
16992       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
16993 #endif
16994     }else{
16995       raw_printf(stderr, "Usage: .scanstats on|off\n");
16996       rc = 1;
16997     }
16998   }else
16999 
17000   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
17001     ShellText sSelect;
17002     ShellState data;
17003     char *zErrMsg = 0;
17004     const char *zDiv = "(";
17005     const char *zName = 0;
17006     int iSchema = 0;
17007     int bDebug = 0;
17008     int ii;
17009 
17010     open_db(p, 0);
17011     memcpy(&data, p, sizeof(data));
17012     data.showHeader = 0;
17013     data.cMode = data.mode = MODE_Semi;
17014     initText(&sSelect);
17015     for(ii=1; ii<nArg; ii++){
17016       if( optionMatch(azArg[ii],"indent") ){
17017         data.cMode = data.mode = MODE_Pretty;
17018       }else if( optionMatch(azArg[ii],"debug") ){
17019         bDebug = 1;
17020       }else if( zName==0 ){
17021         zName = azArg[ii];
17022       }else{
17023         raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
17024         rc = 1;
17025         goto meta_command_exit;
17026       }
17027     }
17028     if( zName!=0 ){
17029       int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
17030       if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
17031         char *new_argv[2], *new_colv[2];
17032         new_argv[0] = sqlite3_mprintf(
17033                       "CREATE TABLE %s (\n"
17034                       "  type text,\n"
17035                       "  name text,\n"
17036                       "  tbl_name text,\n"
17037                       "  rootpage integer,\n"
17038                       "  sql text\n"
17039                       ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
17040         new_argv[1] = 0;
17041         new_colv[0] = "sql";
17042         new_colv[1] = 0;
17043         callback(&data, 1, new_argv, new_colv);
17044         sqlite3_free(new_argv[0]);
17045       }
17046     }
17047     if( zDiv ){
17048       sqlite3_stmt *pStmt = 0;
17049       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
17050                               -1, &pStmt, 0);
17051       if( rc ){
17052         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17053         sqlite3_finalize(pStmt);
17054         rc = 1;
17055         goto meta_command_exit;
17056       }
17057       appendText(&sSelect, "SELECT sql FROM", 0);
17058       iSchema = 0;
17059       while( sqlite3_step(pStmt)==SQLITE_ROW ){
17060         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
17061         char zScNum[30];
17062         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
17063         appendText(&sSelect, zDiv, 0);
17064         zDiv = " UNION ALL ";
17065         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
17066         if( sqlite3_stricmp(zDb, "main")!=0 ){
17067           appendText(&sSelect, zDb, '\'');
17068         }else{
17069           appendText(&sSelect, "NULL", 0);
17070         }
17071         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
17072         appendText(&sSelect, zScNum, 0);
17073         appendText(&sSelect, " AS snum, ", 0);
17074         appendText(&sSelect, zDb, '\'');
17075         appendText(&sSelect, " AS sname FROM ", 0);
17076         appendText(&sSelect, zDb, quoteChar(zDb));
17077         appendText(&sSelect, ".sqlite_master", 0);
17078       }
17079       sqlite3_finalize(pStmt);
17080 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
17081       if( zName ){
17082         appendText(&sSelect,
17083            " UNION ALL SELECT shell_module_schema(name),"
17084            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
17085         0);
17086       }
17087 #endif
17088       appendText(&sSelect, ") WHERE ", 0);
17089       if( zName ){
17090         char *zQarg = sqlite3_mprintf("%Q", zName);
17091         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
17092                     strchr(zName, '[') != 0;
17093         if( strchr(zName, '.') ){
17094           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
17095         }else{
17096           appendText(&sSelect, "lower(tbl_name)", 0);
17097         }
17098         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
17099         appendText(&sSelect, zQarg, 0);
17100         if( !bGlob ){
17101           appendText(&sSelect, " ESCAPE '\\' ", 0);
17102         }
17103         appendText(&sSelect, " AND ", 0);
17104         sqlite3_free(zQarg);
17105       }
17106       appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
17107                            " ORDER BY snum, rowid", 0);
17108       if( bDebug ){
17109         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
17110       }else{
17111         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
17112       }
17113       freeText(&sSelect);
17114     }
17115     if( zErrMsg ){
17116       utf8_printf(stderr,"Error: %s\n", zErrMsg);
17117       sqlite3_free(zErrMsg);
17118       rc = 1;
17119     }else if( rc != SQLITE_OK ){
17120       raw_printf(stderr,"Error: querying schema information\n");
17121       rc = 1;
17122     }else{
17123       rc = 0;
17124     }
17125   }else
17126 
17127 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
17128   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
17129     sqlite3SelectTrace = (int)integerValue(azArg[1]);
17130   }else
17131 #endif
17132 
17133 #if defined(SQLITE_ENABLE_SESSION)
17134   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
17135     OpenSession *pSession = &p->aSession[0];
17136     char **azCmd = &azArg[1];
17137     int iSes = 0;
17138     int nCmd = nArg - 1;
17139     int i;
17140     if( nArg<=1 ) goto session_syntax_error;
17141     open_db(p, 0);
17142     if( nArg>=3 ){
17143       for(iSes=0; iSes<p->nSession; iSes++){
17144         if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
17145       }
17146       if( iSes<p->nSession ){
17147         pSession = &p->aSession[iSes];
17148         azCmd++;
17149         nCmd--;
17150       }else{
17151         pSession = &p->aSession[0];
17152         iSes = 0;
17153       }
17154     }
17155 
17156     /* .session attach TABLE
17157     ** Invoke the sqlite3session_attach() interface to attach a particular
17158     ** table so that it is never filtered.
17159     */
17160     if( strcmp(azCmd[0],"attach")==0 ){
17161       if( nCmd!=2 ) goto session_syntax_error;
17162       if( pSession->p==0 ){
17163         session_not_open:
17164         raw_printf(stderr, "ERROR: No sessions are open\n");
17165       }else{
17166         rc = sqlite3session_attach(pSession->p, azCmd[1]);
17167         if( rc ){
17168           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
17169           rc = 0;
17170         }
17171       }
17172     }else
17173 
17174     /* .session changeset FILE
17175     ** .session patchset FILE
17176     ** Write a changeset or patchset into a file.  The file is overwritten.
17177     */
17178     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
17179       FILE *out = 0;
17180       if( nCmd!=2 ) goto session_syntax_error;
17181       if( pSession->p==0 ) goto session_not_open;
17182       out = fopen(azCmd[1], "wb");
17183       if( out==0 ){
17184         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
17185                     azCmd[1]);
17186       }else{
17187         int szChng;
17188         void *pChng;
17189         if( azCmd[0][0]=='c' ){
17190           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
17191         }else{
17192           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
17193         }
17194         if( rc ){
17195           printf("Error: error code %d\n", rc);
17196           rc = 0;
17197         }
17198         if( pChng
17199           && fwrite(pChng, szChng, 1, out)!=1 ){
17200           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
17201                   szChng);
17202         }
17203         sqlite3_free(pChng);
17204         fclose(out);
17205       }
17206     }else
17207 
17208     /* .session close
17209     ** Close the identified session
17210     */
17211     if( strcmp(azCmd[0], "close")==0 ){
17212       if( nCmd!=1 ) goto session_syntax_error;
17213       if( p->nSession ){
17214         session_close(pSession);
17215         p->aSession[iSes] = p->aSession[--p->nSession];
17216       }
17217     }else
17218 
17219     /* .session enable ?BOOLEAN?
17220     ** Query or set the enable flag
17221     */
17222     if( strcmp(azCmd[0], "enable")==0 ){
17223       int ii;
17224       if( nCmd>2 ) goto session_syntax_error;
17225       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17226       if( p->nSession ){
17227         ii = sqlite3session_enable(pSession->p, ii);
17228         utf8_printf(p->out, "session %s enable flag = %d\n",
17229                     pSession->zName, ii);
17230       }
17231     }else
17232 
17233     /* .session filter GLOB ....
17234     ** Set a list of GLOB patterns of table names to be excluded.
17235     */
17236     if( strcmp(azCmd[0], "filter")==0 ){
17237       int ii, nByte;
17238       if( nCmd<2 ) goto session_syntax_error;
17239       if( p->nSession ){
17240         for(ii=0; ii<pSession->nFilter; ii++){
17241           sqlite3_free(pSession->azFilter[ii]);
17242         }
17243         sqlite3_free(pSession->azFilter);
17244         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
17245         pSession->azFilter = sqlite3_malloc( nByte );
17246         if( pSession->azFilter==0 ){
17247           raw_printf(stderr, "Error: out or memory\n");
17248           exit(1);
17249         }
17250         for(ii=1; ii<nCmd; ii++){
17251           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
17252         }
17253         pSession->nFilter = ii-1;
17254       }
17255     }else
17256 
17257     /* .session indirect ?BOOLEAN?
17258     ** Query or set the indirect flag
17259     */
17260     if( strcmp(azCmd[0], "indirect")==0 ){
17261       int ii;
17262       if( nCmd>2 ) goto session_syntax_error;
17263       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17264       if( p->nSession ){
17265         ii = sqlite3session_indirect(pSession->p, ii);
17266         utf8_printf(p->out, "session %s indirect flag = %d\n",
17267                     pSession->zName, ii);
17268       }
17269     }else
17270 
17271     /* .session isempty
17272     ** Determine if the session is empty
17273     */
17274     if( strcmp(azCmd[0], "isempty")==0 ){
17275       int ii;
17276       if( nCmd!=1 ) goto session_syntax_error;
17277       if( p->nSession ){
17278         ii = sqlite3session_isempty(pSession->p);
17279         utf8_printf(p->out, "session %s isempty flag = %d\n",
17280                     pSession->zName, ii);
17281       }
17282     }else
17283 
17284     /* .session list
17285     ** List all currently open sessions
17286     */
17287     if( strcmp(azCmd[0],"list")==0 ){
17288       for(i=0; i<p->nSession; i++){
17289         utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
17290       }
17291     }else
17292 
17293     /* .session open DB NAME
17294     ** Open a new session called NAME on the attached database DB.
17295     ** DB is normally "main".
17296     */
17297     if( strcmp(azCmd[0],"open")==0 ){
17298       char *zName;
17299       if( nCmd!=3 ) goto session_syntax_error;
17300       zName = azCmd[2];
17301       if( zName[0]==0 ) goto session_syntax_error;
17302       for(i=0; i<p->nSession; i++){
17303         if( strcmp(p->aSession[i].zName,zName)==0 ){
17304           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
17305           goto meta_command_exit;
17306         }
17307       }
17308       if( p->nSession>=ArraySize(p->aSession) ){
17309         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
17310         goto meta_command_exit;
17311       }
17312       pSession = &p->aSession[p->nSession];
17313       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
17314       if( rc ){
17315         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
17316         rc = 0;
17317         goto meta_command_exit;
17318       }
17319       pSession->nFilter = 0;
17320       sqlite3session_table_filter(pSession->p, session_filter, pSession);
17321       p->nSession++;
17322       pSession->zName = sqlite3_mprintf("%s", zName);
17323     }else
17324     /* If no command name matches, show a syntax error */
17325     session_syntax_error:
17326     showHelp(p->out, "session");
17327   }else
17328 #endif
17329 
17330 #ifdef SQLITE_DEBUG
17331   /* Undocumented commands for internal testing.  Subject to change
17332   ** without notice. */
17333   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
17334     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
17335       int i, v;
17336       for(i=1; i<nArg; i++){
17337         v = booleanValue(azArg[i]);
17338         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
17339       }
17340     }
17341     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
17342       int i; sqlite3_int64 v;
17343       for(i=1; i<nArg; i++){
17344         char zBuf[200];
17345         v = integerValue(azArg[i]);
17346         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
17347         utf8_printf(p->out, "%s", zBuf);
17348       }
17349     }
17350   }else
17351 #endif
17352 
17353   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
17354     int bIsInit = 0;         /* True to initialize the SELFTEST table */
17355     int bVerbose = 0;        /* Verbose output */
17356     int bSelftestExists;     /* True if SELFTEST already exists */
17357     int i, k;                /* Loop counters */
17358     int nTest = 0;           /* Number of tests runs */
17359     int nErr = 0;            /* Number of errors seen */
17360     ShellText str;           /* Answer for a query */
17361     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
17362 
17363     open_db(p,0);
17364     for(i=1; i<nArg; i++){
17365       const char *z = azArg[i];
17366       if( z[0]=='-' && z[1]=='-' ) z++;
17367       if( strcmp(z,"-init")==0 ){
17368         bIsInit = 1;
17369       }else
17370       if( strcmp(z,"-v")==0 ){
17371         bVerbose++;
17372       }else
17373       {
17374         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17375                     azArg[i], azArg[0]);
17376         raw_printf(stderr, "Should be one of: --init -v\n");
17377         rc = 1;
17378         goto meta_command_exit;
17379       }
17380     }
17381     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
17382            != SQLITE_OK ){
17383       bSelftestExists = 0;
17384     }else{
17385       bSelftestExists = 1;
17386     }
17387     if( bIsInit ){
17388       createSelftestTable(p);
17389       bSelftestExists = 1;
17390     }
17391     initText(&str);
17392     appendText(&str, "x", 0);
17393     for(k=bSelftestExists; k>=0; k--){
17394       if( k==1 ){
17395         rc = sqlite3_prepare_v2(p->db,
17396             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
17397             -1, &pStmt, 0);
17398       }else{
17399         rc = sqlite3_prepare_v2(p->db,
17400           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
17401           "      (1,'run','PRAGMA integrity_check','ok')",
17402           -1, &pStmt, 0);
17403       }
17404       if( rc ){
17405         raw_printf(stderr, "Error querying the selftest table\n");
17406         rc = 1;
17407         sqlite3_finalize(pStmt);
17408         goto meta_command_exit;
17409       }
17410       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
17411         int tno = sqlite3_column_int(pStmt, 0);
17412         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
17413         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
17414         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
17415 
17416         k = 0;
17417         if( bVerbose>0 ){
17418           char *zQuote = sqlite3_mprintf("%q", zSql);
17419           printf("%d: %s %s\n", tno, zOp, zSql);
17420           sqlite3_free(zQuote);
17421         }
17422         if( strcmp(zOp,"memo")==0 ){
17423           utf8_printf(p->out, "%s\n", zSql);
17424         }else
17425         if( strcmp(zOp,"run")==0 ){
17426           char *zErrMsg = 0;
17427           str.n = 0;
17428           str.z[0] = 0;
17429           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
17430           nTest++;
17431           if( bVerbose ){
17432             utf8_printf(p->out, "Result: %s\n", str.z);
17433           }
17434           if( rc || zErrMsg ){
17435             nErr++;
17436             rc = 1;
17437             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
17438             sqlite3_free(zErrMsg);
17439           }else if( strcmp(zAns,str.z)!=0 ){
17440             nErr++;
17441             rc = 1;
17442             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
17443             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
17444           }
17445         }else
17446         {
17447           utf8_printf(stderr,
17448             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
17449           rc = 1;
17450           break;
17451         }
17452       } /* End loop over rows of content from SELFTEST */
17453       sqlite3_finalize(pStmt);
17454     } /* End loop over k */
17455     freeText(&str);
17456     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
17457   }else
17458 
17459   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
17460     if( nArg<2 || nArg>3 ){
17461       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
17462       rc = 1;
17463     }
17464     if( nArg>=2 ){
17465       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
17466                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
17467     }
17468     if( nArg>=3 ){
17469       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
17470                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
17471     }
17472   }else
17473 
17474   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
17475     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
17476     int i;                   /* Loop counter */
17477     int bSchema = 0;         /* Also hash the schema */
17478     int bSeparate = 0;       /* Hash each table separately */
17479     int iSize = 224;         /* Hash algorithm to use */
17480     int bDebug = 0;          /* Only show the query that would have run */
17481     sqlite3_stmt *pStmt;     /* For querying tables names */
17482     char *zSql;              /* SQL to be run */
17483     char *zSep;              /* Separator */
17484     ShellText sSql;          /* Complete SQL for the query to run the hash */
17485     ShellText sQuery;        /* Set of queries used to read all content */
17486     open_db(p, 0);
17487     for(i=1; i<nArg; i++){
17488       const char *z = azArg[i];
17489       if( z[0]=='-' ){
17490         z++;
17491         if( z[0]=='-' ) z++;
17492         if( strcmp(z,"schema")==0 ){
17493           bSchema = 1;
17494         }else
17495         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
17496          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
17497         ){
17498           iSize = atoi(&z[5]);
17499         }else
17500         if( strcmp(z,"debug")==0 ){
17501           bDebug = 1;
17502         }else
17503         {
17504           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17505                       azArg[i], azArg[0]);
17506           showHelp(p->out, azArg[0]);
17507           rc = 1;
17508           goto meta_command_exit;
17509         }
17510       }else if( zLike ){
17511         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
17512         rc = 1;
17513         goto meta_command_exit;
17514       }else{
17515         zLike = z;
17516         bSeparate = 1;
17517         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
17518       }
17519     }
17520     if( bSchema ){
17521       zSql = "SELECT lower(name) FROM sqlite_master"
17522              " WHERE type='table' AND coalesce(rootpage,0)>1"
17523              " UNION ALL SELECT 'sqlite_master'"
17524              " ORDER BY 1 collate nocase";
17525     }else{
17526       zSql = "SELECT lower(name) FROM sqlite_master"
17527              " WHERE type='table' AND coalesce(rootpage,0)>1"
17528              " AND name NOT LIKE 'sqlite_%'"
17529              " ORDER BY 1 collate nocase";
17530     }
17531     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17532     initText(&sQuery);
17533     initText(&sSql);
17534     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
17535     zSep = "VALUES(";
17536     while( SQLITE_ROW==sqlite3_step(pStmt) ){
17537       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
17538       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
17539       if( strncmp(zTab, "sqlite_",7)!=0 ){
17540         appendText(&sQuery,"SELECT * FROM ", 0);
17541         appendText(&sQuery,zTab,'"');
17542         appendText(&sQuery," NOT INDEXED;", 0);
17543       }else if( strcmp(zTab, "sqlite_master")==0 ){
17544         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
17545                            " ORDER BY name;", 0);
17546       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
17547         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
17548                            " ORDER BY name;", 0);
17549       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
17550         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
17551                            " ORDER BY tbl,idx;", 0);
17552       }else if( strcmp(zTab, "sqlite_stat4")==0 ){
17553         appendText(&sQuery, "SELECT * FROM ", 0);
17554         appendText(&sQuery, zTab, 0);
17555         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
17556       }
17557       appendText(&sSql, zSep, 0);
17558       appendText(&sSql, sQuery.z, '\'');
17559       sQuery.n = 0;
17560       appendText(&sSql, ",", 0);
17561       appendText(&sSql, zTab, '\'');
17562       zSep = "),(";
17563     }
17564     sqlite3_finalize(pStmt);
17565     if( bSeparate ){
17566       zSql = sqlite3_mprintf(
17567           "%s))"
17568           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
17569           "   FROM [sha3sum$query]",
17570           sSql.z, iSize);
17571     }else{
17572       zSql = sqlite3_mprintf(
17573           "%s))"
17574           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
17575           "   FROM [sha3sum$query]",
17576           sSql.z, iSize);
17577     }
17578     freeText(&sQuery);
17579     freeText(&sSql);
17580     if( bDebug ){
17581       utf8_printf(p->out, "%s\n", zSql);
17582     }else{
17583       shell_exec(p, zSql, 0);
17584     }
17585     sqlite3_free(zSql);
17586   }else
17587 
17588 #ifndef SQLITE_NOHAVE_SYSTEM
17589   if( c=='s'
17590    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
17591   ){
17592     char *zCmd;
17593     int i, x;
17594     if( nArg<2 ){
17595       raw_printf(stderr, "Usage: .system COMMAND\n");
17596       rc = 1;
17597       goto meta_command_exit;
17598     }
17599     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
17600     for(i=2; i<nArg; i++){
17601       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
17602                              zCmd, azArg[i]);
17603     }
17604     x = system(zCmd);
17605     sqlite3_free(zCmd);
17606     if( x ) raw_printf(stderr, "System command returns %d\n", x);
17607   }else
17608 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
17609 
17610   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
17611     static const char *azBool[] = { "off", "on", "trigger", "full"};
17612     int i;
17613     if( nArg!=1 ){
17614       raw_printf(stderr, "Usage: .show\n");
17615       rc = 1;
17616       goto meta_command_exit;
17617     }
17618     utf8_printf(p->out, "%12.12s: %s\n","echo",
17619                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
17620     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
17621     utf8_printf(p->out, "%12.12s: %s\n","explain",
17622          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
17623     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
17624     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
17625     utf8_printf(p->out, "%12.12s: ", "nullvalue");
17626       output_c_string(p->out, p->nullValue);
17627       raw_printf(p->out, "\n");
17628     utf8_printf(p->out,"%12.12s: %s\n","output",
17629             strlen30(p->outfile) ? p->outfile : "stdout");
17630     utf8_printf(p->out,"%12.12s: ", "colseparator");
17631       output_c_string(p->out, p->colSeparator);
17632       raw_printf(p->out, "\n");
17633     utf8_printf(p->out,"%12.12s: ", "rowseparator");
17634       output_c_string(p->out, p->rowSeparator);
17635       raw_printf(p->out, "\n");
17636     utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
17637     utf8_printf(p->out, "%12.12s: ", "width");
17638     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
17639       raw_printf(p->out, "%d ", p->colWidth[i]);
17640     }
17641     raw_printf(p->out, "\n");
17642     utf8_printf(p->out, "%12.12s: %s\n", "filename",
17643                 p->zDbFilename ? p->zDbFilename : "");
17644   }else
17645 
17646   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
17647     if( nArg==2 ){
17648       p->statsOn = (u8)booleanValue(azArg[1]);
17649     }else if( nArg==1 ){
17650       display_stats(p->db, p, 0);
17651     }else{
17652       raw_printf(stderr, "Usage: .stats ?on|off?\n");
17653       rc = 1;
17654     }
17655   }else
17656 
17657   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
17658    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
17659                  || strncmp(azArg[0], "indexes", n)==0) )
17660   ){
17661     sqlite3_stmt *pStmt;
17662     char **azResult;
17663     int nRow, nAlloc;
17664     int ii;
17665     ShellText s;
17666     initText(&s);
17667     open_db(p, 0);
17668     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
17669     if( rc ){
17670       sqlite3_finalize(pStmt);
17671       return shellDatabaseError(p->db);
17672     }
17673 
17674     if( nArg>2 && c=='i' ){
17675       /* It is an historical accident that the .indexes command shows an error
17676       ** when called with the wrong number of arguments whereas the .tables
17677       ** command does not. */
17678       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
17679       rc = 1;
17680       sqlite3_finalize(pStmt);
17681       goto meta_command_exit;
17682     }
17683     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
17684       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
17685       if( zDbName==0 ) continue;
17686       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
17687       if( sqlite3_stricmp(zDbName, "main")==0 ){
17688         appendText(&s, "SELECT name FROM ", 0);
17689       }else{
17690         appendText(&s, "SELECT ", 0);
17691         appendText(&s, zDbName, '\'');
17692         appendText(&s, "||'.'||name FROM ", 0);
17693       }
17694       appendText(&s, zDbName, '"');
17695       appendText(&s, ".sqlite_master ", 0);
17696       if( c=='t' ){
17697         appendText(&s," WHERE type IN ('table','view')"
17698                       "   AND name NOT LIKE 'sqlite_%'"
17699                       "   AND name LIKE ?1", 0);
17700       }else{
17701         appendText(&s," WHERE type='index'"
17702                       "   AND tbl_name LIKE ?1", 0);
17703       }
17704     }
17705     rc = sqlite3_finalize(pStmt);
17706     appendText(&s, " ORDER BY 1", 0);
17707     rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
17708     freeText(&s);
17709     if( rc ) return shellDatabaseError(p->db);
17710 
17711     /* Run the SQL statement prepared by the above block. Store the results
17712     ** as an array of nul-terminated strings in azResult[].  */
17713     nRow = nAlloc = 0;
17714     azResult = 0;
17715     if( nArg>1 ){
17716       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
17717     }else{
17718       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
17719     }
17720     while( sqlite3_step(pStmt)==SQLITE_ROW ){
17721       if( nRow>=nAlloc ){
17722         char **azNew;
17723         int n2 = nAlloc*2 + 10;
17724         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
17725         if( azNew==0 ) shell_out_of_memory();
17726         nAlloc = n2;
17727         azResult = azNew;
17728       }
17729       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
17730       if( 0==azResult[nRow] ) shell_out_of_memory();
17731       nRow++;
17732     }
17733     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
17734       rc = shellDatabaseError(p->db);
17735     }
17736 
17737     /* Pretty-print the contents of array azResult[] to the output */
17738     if( rc==0 && nRow>0 ){
17739       int len, maxlen = 0;
17740       int i, j;
17741       int nPrintCol, nPrintRow;
17742       for(i=0; i<nRow; i++){
17743         len = strlen30(azResult[i]);
17744         if( len>maxlen ) maxlen = len;
17745       }
17746       nPrintCol = 80/(maxlen+2);
17747       if( nPrintCol<1 ) nPrintCol = 1;
17748       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
17749       for(i=0; i<nPrintRow; i++){
17750         for(j=i; j<nRow; j+=nPrintRow){
17751           char *zSp = j<nPrintRow ? "" : "  ";
17752           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
17753                       azResult[j] ? azResult[j]:"");
17754         }
17755         raw_printf(p->out, "\n");
17756       }
17757     }
17758 
17759     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
17760     sqlite3_free(azResult);
17761   }else
17762 
17763   /* Begin redirecting output to the file "testcase-out.txt" */
17764   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
17765     output_reset(p);
17766     p->out = output_file_open("testcase-out.txt", 0);
17767     if( p->out==0 ){
17768       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
17769     }
17770     if( nArg>=2 ){
17771       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
17772     }else{
17773       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
17774     }
17775   }else
17776 
17777 #ifndef SQLITE_UNTESTABLE
17778   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
17779     static const struct {
17780        const char *zCtrlName;   /* Name of a test-control option */
17781        int ctrlCode;            /* Integer code for that option */
17782        const char *zUsage;      /* Usage notes */
17783     } aCtrl[] = {
17784       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
17785       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
17786     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
17787     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
17788       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
17789       { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
17790     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
17791       { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
17792       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
17793       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
17794       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
17795       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
17796 #ifdef YYCOVERAGE
17797       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
17798 #endif
17799       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
17800       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
17801       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
17802       { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
17803       { "reserve",            SQLITE_TESTCTRL_RESERVE,      "BYTES-OF-RESERVE"},
17804     };
17805     int testctrl = -1;
17806     int iCtrl = -1;
17807     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
17808     int isOk = 0;
17809     int i, n2;
17810     const char *zCmd = 0;
17811 
17812     open_db(p, 0);
17813     zCmd = nArg>=2 ? azArg[1] : "help";
17814 
17815     /* The argument can optionally begin with "-" or "--" */
17816     if( zCmd[0]=='-' && zCmd[1] ){
17817       zCmd++;
17818       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
17819     }
17820 
17821     /* --help lists all test-controls */
17822     if( strcmp(zCmd,"help")==0 ){
17823       utf8_printf(p->out, "Available test-controls:\n");
17824       for(i=0; i<ArraySize(aCtrl); i++){
17825         utf8_printf(p->out, "  .testctrl %s %s\n",
17826                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
17827       }
17828       rc = 1;
17829       goto meta_command_exit;
17830     }
17831 
17832     /* convert testctrl text option to value. allow any unique prefix
17833     ** of the option name, or a numerical value. */
17834     n2 = strlen30(zCmd);
17835     for(i=0; i<ArraySize(aCtrl); i++){
17836       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
17837         if( testctrl<0 ){
17838           testctrl = aCtrl[i].ctrlCode;
17839           iCtrl = i;
17840         }else{
17841           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
17842                               "Use \".testctrl --help\" for help\n", zCmd);
17843           rc = 1;
17844           goto meta_command_exit;
17845         }
17846       }
17847     }
17848     if( testctrl<0 ){
17849       utf8_printf(stderr,"Error: unknown test-control: %s\n"
17850                          "Use \".testctrl --help\" for help\n", zCmd);
17851     }else{
17852       switch(testctrl){
17853 
17854         /* sqlite3_test_control(int, db, int) */
17855         case SQLITE_TESTCTRL_OPTIMIZATIONS:
17856         case SQLITE_TESTCTRL_RESERVE:
17857           if( nArg==3 ){
17858             int opt = (int)strtol(azArg[2], 0, 0);
17859             rc2 = sqlite3_test_control(testctrl, p->db, opt);
17860             isOk = 3;
17861           }
17862           break;
17863 
17864         /* sqlite3_test_control(int) */
17865         case SQLITE_TESTCTRL_PRNG_SAVE:
17866         case SQLITE_TESTCTRL_PRNG_RESTORE:
17867         case SQLITE_TESTCTRL_PRNG_RESET:
17868         case SQLITE_TESTCTRL_BYTEORDER:
17869           if( nArg==2 ){
17870             rc2 = sqlite3_test_control(testctrl);
17871             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
17872           }
17873           break;
17874 
17875         /* sqlite3_test_control(int, uint) */
17876         case SQLITE_TESTCTRL_PENDING_BYTE:
17877           if( nArg==3 ){
17878             unsigned int opt = (unsigned int)integerValue(azArg[2]);
17879             rc2 = sqlite3_test_control(testctrl, opt);
17880             isOk = 3;
17881           }
17882           break;
17883 
17884         /* sqlite3_test_control(int, int, sqlite3*) */
17885         case SQLITE_TESTCTRL_PRNG_SEED:
17886           if( nArg==3 || nArg==4 ){
17887             int ii = (int)integerValue(azArg[2]);
17888             sqlite3 *db;
17889             if( ii==0 && strcmp(azArg[2],"random")==0 ){
17890               sqlite3_randomness(sizeof(ii),&ii);
17891               printf("-- random seed: %d\n", ii);
17892             }
17893             if( nArg==3 ){
17894               db = 0;
17895             }else{
17896               db = p->db;
17897               /* Make sure the schema has been loaded */
17898               sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
17899             }
17900             rc2 = sqlite3_test_control(testctrl, ii, db);
17901             isOk = 3;
17902           }
17903           break;
17904 
17905         /* sqlite3_test_control(int, int) */
17906         case SQLITE_TESTCTRL_ASSERT:
17907         case SQLITE_TESTCTRL_ALWAYS:
17908           if( nArg==3 ){
17909             int opt = booleanValue(azArg[2]);
17910             rc2 = sqlite3_test_control(testctrl, opt);
17911             isOk = 1;
17912           }
17913           break;
17914 
17915         /* sqlite3_test_control(int, int) */
17916         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
17917         case SQLITE_TESTCTRL_NEVER_CORRUPT:
17918           if( nArg==3 ){
17919             int opt = booleanValue(azArg[2]);
17920             rc2 = sqlite3_test_control(testctrl, opt);
17921             isOk = 3;
17922           }
17923           break;
17924 
17925         /* sqlite3_test_control(sqlite3*) */
17926         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
17927           rc2 = sqlite3_test_control(testctrl, p->db);
17928           isOk = 3;
17929           break;
17930 
17931         case SQLITE_TESTCTRL_IMPOSTER:
17932           if( nArg==5 ){
17933             rc2 = sqlite3_test_control(testctrl, p->db,
17934                           azArg[2],
17935                           integerValue(azArg[3]),
17936                           integerValue(azArg[4]));
17937             isOk = 3;
17938           }
17939           break;
17940 
17941 #ifdef YYCOVERAGE
17942         case SQLITE_TESTCTRL_PARSER_COVERAGE:
17943           if( nArg==2 ){
17944             sqlite3_test_control(testctrl, p->out);
17945             isOk = 3;
17946           }
17947 #endif
17948       }
17949     }
17950     if( isOk==0 && iCtrl>=0 ){
17951       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
17952       rc = 1;
17953     }else if( isOk==1 ){
17954       raw_printf(p->out, "%d\n", rc2);
17955     }else if( isOk==2 ){
17956       raw_printf(p->out, "0x%08x\n", rc2);
17957     }
17958   }else
17959 #endif /* !defined(SQLITE_UNTESTABLE) */
17960 
17961   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
17962     open_db(p, 0);
17963     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
17964   }else
17965 
17966   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
17967     if( nArg==2 ){
17968       enableTimer = booleanValue(azArg[1]);
17969       if( enableTimer && !HAS_TIMER ){
17970         raw_printf(stderr, "Error: timer not available on this system.\n");
17971         enableTimer = 0;
17972       }
17973     }else{
17974       raw_printf(stderr, "Usage: .timer on|off\n");
17975       rc = 1;
17976     }
17977   }else
17978 
17979 #ifndef SQLITE_OMIT_TRACE
17980   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
17981     int mType = 0;
17982     int jj;
17983     open_db(p, 0);
17984     for(jj=1; jj<nArg; jj++){
17985       const char *z = azArg[jj];
17986       if( z[0]=='-' ){
17987         if( optionMatch(z, "expanded") ){
17988           p->eTraceType = SHELL_TRACE_EXPANDED;
17989         }
17990 #ifdef SQLITE_ENABLE_NORMALIZE
17991         else if( optionMatch(z, "normalized") ){
17992           p->eTraceType = SHELL_TRACE_NORMALIZED;
17993         }
17994 #endif
17995         else if( optionMatch(z, "plain") ){
17996           p->eTraceType = SHELL_TRACE_PLAIN;
17997         }
17998         else if( optionMatch(z, "profile") ){
17999           mType |= SQLITE_TRACE_PROFILE;
18000         }
18001         else if( optionMatch(z, "row") ){
18002           mType |= SQLITE_TRACE_ROW;
18003         }
18004         else if( optionMatch(z, "stmt") ){
18005           mType |= SQLITE_TRACE_STMT;
18006         }
18007         else if( optionMatch(z, "close") ){
18008           mType |= SQLITE_TRACE_CLOSE;
18009         }
18010         else {
18011           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
18012           rc = 1;
18013           goto meta_command_exit;
18014         }
18015       }else{
18016         output_file_close(p->traceOut);
18017         p->traceOut = output_file_open(azArg[1], 0);
18018       }
18019     }
18020     if( p->traceOut==0 ){
18021       sqlite3_trace_v2(p->db, 0, 0, 0);
18022     }else{
18023       if( mType==0 ) mType = SQLITE_TRACE_STMT;
18024       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
18025     }
18026   }else
18027 #endif /* !defined(SQLITE_OMIT_TRACE) */
18028 
18029 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18030   if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
18031     int ii;
18032     int lenOpt;
18033     char *zOpt;
18034     if( nArg<2 ){
18035       raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
18036       rc = 1;
18037       goto meta_command_exit;
18038     }
18039     open_db(p, 0);
18040     zOpt = azArg[1];
18041     if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
18042     lenOpt = (int)strlen(zOpt);
18043     if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
18044       assert( azArg[nArg]==0 );
18045       sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
18046     }else{
18047       for(ii=1; ii<nArg; ii++){
18048         sqlite3_create_module(p->db, azArg[ii], 0, 0);
18049       }
18050     }
18051   }else
18052 #endif
18053 
18054 #if SQLITE_USER_AUTHENTICATION
18055   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
18056     if( nArg<2 ){
18057       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
18058       rc = 1;
18059       goto meta_command_exit;
18060     }
18061     open_db(p, 0);
18062     if( strcmp(azArg[1],"login")==0 ){
18063       if( nArg!=4 ){
18064         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
18065         rc = 1;
18066         goto meta_command_exit;
18067       }
18068       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
18069                                      strlen30(azArg[3]));
18070       if( rc ){
18071         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
18072         rc = 1;
18073       }
18074     }else if( strcmp(azArg[1],"add")==0 ){
18075       if( nArg!=5 ){
18076         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
18077         rc = 1;
18078         goto meta_command_exit;
18079       }
18080       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18081                             booleanValue(azArg[4]));
18082       if( rc ){
18083         raw_printf(stderr, "User-Add failed: %d\n", rc);
18084         rc = 1;
18085       }
18086     }else if( strcmp(azArg[1],"edit")==0 ){
18087       if( nArg!=5 ){
18088         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
18089         rc = 1;
18090         goto meta_command_exit;
18091       }
18092       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18093                               booleanValue(azArg[4]));
18094       if( rc ){
18095         raw_printf(stderr, "User-Edit failed: %d\n", rc);
18096         rc = 1;
18097       }
18098     }else if( strcmp(azArg[1],"delete")==0 ){
18099       if( nArg!=3 ){
18100         raw_printf(stderr, "Usage: .user delete USER\n");
18101         rc = 1;
18102         goto meta_command_exit;
18103       }
18104       rc = sqlite3_user_delete(p->db, azArg[2]);
18105       if( rc ){
18106         raw_printf(stderr, "User-Delete failed: %d\n", rc);
18107         rc = 1;
18108       }
18109     }else{
18110       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
18111       rc = 1;
18112       goto meta_command_exit;
18113     }
18114   }else
18115 #endif /* SQLITE_USER_AUTHENTICATION */
18116 
18117   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
18118     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
18119         sqlite3_libversion(), sqlite3_sourceid());
18120 #if SQLITE_HAVE_ZLIB
18121     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
18122 #endif
18123 #define CTIMEOPT_VAL_(opt) #opt
18124 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
18125 #if defined(__clang__) && defined(__clang_major__)
18126     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
18127                     CTIMEOPT_VAL(__clang_minor__) "."
18128                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
18129 #elif defined(_MSC_VER)
18130     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
18131 #elif defined(__GNUC__) && defined(__VERSION__)
18132     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
18133 #endif
18134   }else
18135 
18136   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
18137     const char *zDbName = nArg==2 ? azArg[1] : "main";
18138     sqlite3_vfs *pVfs = 0;
18139     if( p->db ){
18140       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
18141       if( pVfs ){
18142         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
18143         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
18144         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
18145         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18146       }
18147     }
18148   }else
18149 
18150   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
18151     sqlite3_vfs *pVfs;
18152     sqlite3_vfs *pCurrent = 0;
18153     if( p->db ){
18154       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
18155     }
18156     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
18157       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
18158            pVfs==pCurrent ? "  <--- CURRENT" : "");
18159       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
18160       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
18161       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18162       if( pVfs->pNext ){
18163         raw_printf(p->out, "-----------------------------------\n");
18164       }
18165     }
18166   }else
18167 
18168   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
18169     const char *zDbName = nArg==2 ? azArg[1] : "main";
18170     char *zVfsName = 0;
18171     if( p->db ){
18172       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
18173       if( zVfsName ){
18174         utf8_printf(p->out, "%s\n", zVfsName);
18175         sqlite3_free(zVfsName);
18176       }
18177     }
18178   }else
18179 
18180 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
18181   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
18182     sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
18183   }else
18184 #endif
18185 
18186   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
18187     int j;
18188     assert( nArg<=ArraySize(azArg) );
18189     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
18190       p->colWidth[j-1] = (int)integerValue(azArg[j]);
18191     }
18192   }else
18193 
18194   {
18195     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
18196       " \"%s\". Enter \".help\" for help\n", azArg[0]);
18197     rc = 1;
18198   }
18199 
18200 meta_command_exit:
18201   if( p->outCount ){
18202     p->outCount--;
18203     if( p->outCount==0 ) output_reset(p);
18204   }
18205   return rc;
18206 }
18207 
18208 /*
18209 ** Return TRUE if a semicolon occurs anywhere in the first N characters
18210 ** of string z[].
18211 */
18212 static int line_contains_semicolon(const char *z, int N){
18213   int i;
18214   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
18215   return 0;
18216 }
18217 
18218 /*
18219 ** Test to see if a line consists entirely of whitespace.
18220 */
18221 static int _all_whitespace(const char *z){
18222   for(; *z; z++){
18223     if( IsSpace(z[0]) ) continue;
18224     if( *z=='/' && z[1]=='*' ){
18225       z += 2;
18226       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
18227       if( *z==0 ) return 0;
18228       z++;
18229       continue;
18230     }
18231     if( *z=='-' && z[1]=='-' ){
18232       z += 2;
18233       while( *z && *z!='\n' ){ z++; }
18234       if( *z==0 ) return 1;
18235       continue;
18236     }
18237     return 0;
18238   }
18239   return 1;
18240 }
18241 
18242 /*
18243 ** Return TRUE if the line typed in is an SQL command terminator other
18244 ** than a semi-colon.  The SQL Server style "go" command is understood
18245 ** as is the Oracle "/".
18246 */
18247 static int line_is_command_terminator(const char *zLine){
18248   while( IsSpace(zLine[0]) ){ zLine++; };
18249   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
18250     return 1;  /* Oracle */
18251   }
18252   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
18253          && _all_whitespace(&zLine[2]) ){
18254     return 1;  /* SQL Server */
18255   }
18256   return 0;
18257 }
18258 
18259 /*
18260 ** We need a default sqlite3_complete() implementation to use in case
18261 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
18262 ** any arbitrary text is a complete SQL statement.  This is not very
18263 ** user-friendly, but it does seem to work.
18264 */
18265 #ifdef SQLITE_OMIT_COMPLETE
18266 #define sqlite3_complete(x) 1
18267 #endif
18268 
18269 /*
18270 ** Return true if zSql is a complete SQL statement.  Return false if it
18271 ** ends in the middle of a string literal or C-style comment.
18272 */
18273 static int line_is_complete(char *zSql, int nSql){
18274   int rc;
18275   if( zSql==0 ) return 1;
18276   zSql[nSql] = ';';
18277   zSql[nSql+1] = 0;
18278   rc = sqlite3_complete(zSql);
18279   zSql[nSql] = 0;
18280   return rc;
18281 }
18282 
18283 /*
18284 ** Run a single line of SQL.  Return the number of errors.
18285 */
18286 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
18287   int rc;
18288   char *zErrMsg = 0;
18289 
18290   open_db(p, 0);
18291   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
18292   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
18293   BEGIN_TIMER;
18294   rc = shell_exec(p, zSql, &zErrMsg);
18295   END_TIMER;
18296   if( rc || zErrMsg ){
18297     char zPrefix[100];
18298     if( in!=0 || !stdin_is_interactive ){
18299       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
18300                        "Error: near line %d:", startline);
18301     }else{
18302       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
18303     }
18304     if( zErrMsg!=0 ){
18305       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
18306       sqlite3_free(zErrMsg);
18307       zErrMsg = 0;
18308     }else{
18309       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
18310     }
18311     return 1;
18312   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
18313     raw_printf(p->out, "changes: %3d   total_changes: %d\n",
18314             sqlite3_changes(p->db), sqlite3_total_changes(p->db));
18315   }
18316   return 0;
18317 }
18318 
18319 
18320 /*
18321 ** Read input from *in and process it.  If *in==0 then input
18322 ** is interactive - the user is typing it it.  Otherwise, input
18323 ** is coming from a file or device.  A prompt is issued and history
18324 ** is saved only if input is interactive.  An interrupt signal will
18325 ** cause this routine to exit immediately, unless input is interactive.
18326 **
18327 ** Return the number of errors.
18328 */
18329 static int process_input(ShellState *p){
18330   char *zLine = 0;          /* A single input line */
18331   char *zSql = 0;           /* Accumulated SQL text */
18332   int nLine;                /* Length of current line */
18333   int nSql = 0;             /* Bytes of zSql[] used */
18334   int nAlloc = 0;           /* Allocated zSql[] space */
18335   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
18336   int rc;                   /* Error code */
18337   int errCnt = 0;           /* Number of errors seen */
18338   int startline = 0;        /* Line number for start of current input */
18339 
18340   p->lineno = 0;
18341   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
18342     fflush(p->out);
18343     zLine = one_input_line(p->in, zLine, nSql>0);
18344     if( zLine==0 ){
18345       /* End of input */
18346       if( p->in==0 && stdin_is_interactive ) printf("\n");
18347       break;
18348     }
18349     if( seenInterrupt ){
18350       if( p->in!=0 ) break;
18351       seenInterrupt = 0;
18352     }
18353     p->lineno++;
18354     if( nSql==0 && _all_whitespace(zLine) ){
18355       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18356       continue;
18357     }
18358     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
18359       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18360       if( zLine[0]=='.' ){
18361         rc = do_meta_command(zLine, p);
18362         if( rc==2 ){ /* exit requested */
18363           break;
18364         }else if( rc ){
18365           errCnt++;
18366         }
18367       }
18368       continue;
18369     }
18370     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
18371       memcpy(zLine,";",2);
18372     }
18373     nLine = strlen30(zLine);
18374     if( nSql+nLine+2>=nAlloc ){
18375       nAlloc = nSql+nLine+100;
18376       zSql = realloc(zSql, nAlloc);
18377       if( zSql==0 ) shell_out_of_memory();
18378     }
18379     nSqlPrior = nSql;
18380     if( nSql==0 ){
18381       int i;
18382       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
18383       assert( nAlloc>0 && zSql!=0 );
18384       memcpy(zSql, zLine+i, nLine+1-i);
18385       startline = p->lineno;
18386       nSql = nLine-i;
18387     }else{
18388       zSql[nSql++] = '\n';
18389       memcpy(zSql+nSql, zLine, nLine+1);
18390       nSql += nLine;
18391     }
18392     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
18393                 && sqlite3_complete(zSql) ){
18394       errCnt += runOneSqlLine(p, zSql, p->in, startline);
18395       nSql = 0;
18396       if( p->outCount ){
18397         output_reset(p);
18398         p->outCount = 0;
18399       }else{
18400         clearTempFile(p);
18401       }
18402     }else if( nSql && _all_whitespace(zSql) ){
18403       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
18404       nSql = 0;
18405     }
18406   }
18407   if( nSql && !_all_whitespace(zSql) ){
18408     errCnt += runOneSqlLine(p, zSql, p->in, startline);
18409   }
18410   free(zSql);
18411   free(zLine);
18412   return errCnt>0;
18413 }
18414 
18415 /*
18416 ** Return a pathname which is the user's home directory.  A
18417 ** 0 return indicates an error of some kind.
18418 */
18419 static char *find_home_dir(int clearFlag){
18420   static char *home_dir = NULL;
18421   if( clearFlag ){
18422     free(home_dir);
18423     home_dir = 0;
18424     return 0;
18425   }
18426   if( home_dir ) return home_dir;
18427 
18428 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
18429      && !defined(__RTP__) && !defined(_WRS_KERNEL)
18430   {
18431     struct passwd *pwent;
18432     uid_t uid = getuid();
18433     if( (pwent=getpwuid(uid)) != NULL) {
18434       home_dir = pwent->pw_dir;
18435     }
18436   }
18437 #endif
18438 
18439 #if defined(_WIN32_WCE)
18440   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
18441    */
18442   home_dir = "/";
18443 #else
18444 
18445 #if defined(_WIN32) || defined(WIN32)
18446   if (!home_dir) {
18447     home_dir = getenv("USERPROFILE");
18448   }
18449 #endif
18450 
18451   if (!home_dir) {
18452     home_dir = getenv("HOME");
18453   }
18454 
18455 #if defined(_WIN32) || defined(WIN32)
18456   if (!home_dir) {
18457     char *zDrive, *zPath;
18458     int n;
18459     zDrive = getenv("HOMEDRIVE");
18460     zPath = getenv("HOMEPATH");
18461     if( zDrive && zPath ){
18462       n = strlen30(zDrive) + strlen30(zPath) + 1;
18463       home_dir = malloc( n );
18464       if( home_dir==0 ) return 0;
18465       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
18466       return home_dir;
18467     }
18468     home_dir = "c:\\";
18469   }
18470 #endif
18471 
18472 #endif /* !_WIN32_WCE */
18473 
18474   if( home_dir ){
18475     int n = strlen30(home_dir) + 1;
18476     char *z = malloc( n );
18477     if( z ) memcpy(z, home_dir, n);
18478     home_dir = z;
18479   }
18480 
18481   return home_dir;
18482 }
18483 
18484 /*
18485 ** Read input from the file given by sqliterc_override.  Or if that
18486 ** parameter is NULL, take input from ~/.sqliterc
18487 **
18488 ** Returns the number of errors.
18489 */
18490 static void process_sqliterc(
18491   ShellState *p,                  /* Configuration data */
18492   const char *sqliterc_override   /* Name of config file. NULL to use default */
18493 ){
18494   char *home_dir = NULL;
18495   const char *sqliterc = sqliterc_override;
18496   char *zBuf = 0;
18497   FILE *inSaved = p->in;
18498   int savedLineno = p->lineno;
18499 
18500   if (sqliterc == NULL) {
18501     home_dir = find_home_dir(0);
18502     if( home_dir==0 ){
18503       raw_printf(stderr, "-- warning: cannot find home directory;"
18504                       " cannot read ~/.sqliterc\n");
18505       return;
18506     }
18507     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
18508     sqliterc = zBuf;
18509   }
18510   p->in = fopen(sqliterc,"rb");
18511   if( p->in ){
18512     if( stdin_is_interactive ){
18513       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
18514     }
18515     process_input(p);
18516     fclose(p->in);
18517   }
18518   p->in = inSaved;
18519   p->lineno = savedLineno;
18520   sqlite3_free(zBuf);
18521 }
18522 
18523 /*
18524 ** Show available command line options
18525 */
18526 static const char zOptions[] =
18527 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18528   "   -A ARGS...           run \".archive ARGS\" and exit\n"
18529 #endif
18530   "   -append              append the database to the end of the file\n"
18531   "   -ascii               set output mode to 'ascii'\n"
18532   "   -bail                stop after hitting an error\n"
18533   "   -batch               force batch I/O\n"
18534   "   -column              set output mode to 'column'\n"
18535   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
18536   "   -csv                 set output mode to 'csv'\n"
18537 #if defined(SQLITE_ENABLE_DESERIALIZE)
18538   "   -deserialize         open the database using sqlite3_deserialize()\n"
18539 #endif
18540   "   -echo                print commands before execution\n"
18541   "   -init FILENAME       read/process named file\n"
18542   "   -[no]header          turn headers on or off\n"
18543 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
18544   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
18545 #endif
18546   "   -help                show this message\n"
18547   "   -html                set output mode to HTML\n"
18548   "   -interactive         force interactive I/O\n"
18549   "   -line                set output mode to 'line'\n"
18550   "   -list                set output mode to 'list'\n"
18551   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
18552 #if defined(SQLITE_ENABLE_DESERIALIZE)
18553   "   -maxsize N           maximum size for a --deserialize database\n"
18554 #endif
18555   "   -memtrace            trace all memory allocations and deallocations\n"
18556   "   -mmap N              default mmap size set to N\n"
18557 #ifdef SQLITE_ENABLE_MULTIPLEX
18558   "   -multiplex           enable the multiplexor VFS\n"
18559 #endif
18560   "   -newline SEP         set output row separator. Default: '\\n'\n"
18561   "   -nofollow            refuse to open symbolic links to database files\n"
18562   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
18563   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
18564   "   -quote               set output mode to 'quote'\n"
18565   "   -readonly            open the database read-only\n"
18566   "   -separator SEP       set output column separator. Default: '|'\n"
18567 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
18568   "   -sorterref SIZE      sorter references threshold size\n"
18569 #endif
18570   "   -stats               print memory stats before each finalize\n"
18571   "   -version             show SQLite version\n"
18572   "   -vfs NAME            use NAME as the default VFS\n"
18573 #ifdef SQLITE_ENABLE_VFSTRACE
18574   "   -vfstrace            enable tracing of all VFS calls\n"
18575 #endif
18576 #ifdef SQLITE_HAVE_ZLIB
18577   "   -zip                 open the file as a ZIP Archive\n"
18578 #endif
18579 ;
18580 static void usage(int showDetail){
18581   utf8_printf(stderr,
18582       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
18583       "FILENAME is the name of an SQLite database. A new database is created\n"
18584       "if the file does not previously exist.\n", Argv0);
18585   if( showDetail ){
18586     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
18587   }else{
18588     raw_printf(stderr, "Use the -help option for additional information\n");
18589   }
18590   exit(1);
18591 }
18592 
18593 /*
18594 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
18595 ** error message if it is initialized.
18596 */
18597 static void verify_uninitialized(void){
18598   if( sqlite3_config(-1)==SQLITE_MISUSE ){
18599     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
18600                         " initialization.\n");
18601   }
18602 }
18603 
18604 /*
18605 ** Initialize the state information in data
18606 */
18607 static void main_init(ShellState *data) {
18608   memset(data, 0, sizeof(*data));
18609   data->normalMode = data->cMode = data->mode = MODE_List;
18610   data->autoExplain = 1;
18611   memcpy(data->colSeparator,SEP_Column, 2);
18612   memcpy(data->rowSeparator,SEP_Row, 2);
18613   data->showHeader = 0;
18614   data->shellFlgs = SHFLG_Lookaside;
18615   verify_uninitialized();
18616   sqlite3_config(SQLITE_CONFIG_URI, 1);
18617   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
18618   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
18619   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
18620   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
18621 }
18622 
18623 /*
18624 ** Output text to the console in a font that attracts extra attention.
18625 */
18626 #ifdef _WIN32
18627 static void printBold(const char *zText){
18628   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
18629   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
18630   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
18631   SetConsoleTextAttribute(out,
18632          FOREGROUND_RED|FOREGROUND_INTENSITY
18633   );
18634   printf("%s", zText);
18635   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
18636 }
18637 #else
18638 static void printBold(const char *zText){
18639   printf("\033[1m%s\033[0m", zText);
18640 }
18641 #endif
18642 
18643 /*
18644 ** Get the argument to an --option.  Throw an error and die if no argument
18645 ** is available.
18646 */
18647 static char *cmdline_option_value(int argc, char **argv, int i){
18648   if( i==argc ){
18649     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
18650             argv[0], argv[argc-1]);
18651     exit(1);
18652   }
18653   return argv[i];
18654 }
18655 
18656 #ifndef SQLITE_SHELL_IS_UTF8
18657 #  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
18658 #    define SQLITE_SHELL_IS_UTF8          (0)
18659 #  else
18660 #    define SQLITE_SHELL_IS_UTF8          (1)
18661 #  endif
18662 #endif
18663 
18664 #if SQLITE_SHELL_IS_UTF8
18665 int SQLITE_CDECL main(int argc, char **argv){
18666 #else
18667 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
18668   char **argv;
18669 #endif
18670   char *zErrMsg = 0;
18671   ShellState data;
18672   const char *zInitFile = 0;
18673   int i;
18674   int rc = 0;
18675   int warnInmemoryDb = 0;
18676   int readStdin = 1;
18677   int nCmd = 0;
18678   char **azCmd = 0;
18679   const char *zVfs = 0;           /* Value of -vfs command-line option */
18680 #if !SQLITE_SHELL_IS_UTF8
18681   char **argvToFree = 0;
18682   int argcToFree = 0;
18683 #endif
18684 
18685   setBinaryMode(stdin, 0);
18686   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
18687   stdin_is_interactive = isatty(0);
18688   stdout_is_console = isatty(1);
18689 
18690 #if !defined(_WIN32_WCE)
18691   if( getenv("SQLITE_DEBUG_BREAK") ){
18692     if( isatty(0) && isatty(2) ){
18693       fprintf(stderr,
18694           "attach debugger to process %d and press any key to continue.\n",
18695           GETPID());
18696       fgetc(stdin);
18697     }else{
18698 #if defined(_WIN32) || defined(WIN32)
18699       DebugBreak();
18700 #elif defined(SIGTRAP)
18701       raise(SIGTRAP);
18702 #endif
18703     }
18704   }
18705 #endif
18706 
18707 #if USE_SYSTEM_SQLITE+0!=1
18708   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
18709     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
18710             sqlite3_sourceid(), SQLITE_SOURCE_ID);
18711     exit(1);
18712   }
18713 #endif
18714   main_init(&data);
18715 
18716   /* On Windows, we must translate command-line arguments into UTF-8.
18717   ** The SQLite memory allocator subsystem has to be enabled in order to
18718   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
18719   ** subsequent sqlite3_config() calls will work.  So copy all results into
18720   ** memory that does not come from the SQLite memory allocator.
18721   */
18722 #if !SQLITE_SHELL_IS_UTF8
18723   sqlite3_initialize();
18724   argvToFree = malloc(sizeof(argv[0])*argc*2);
18725   argcToFree = argc;
18726   argv = argvToFree + argc;
18727   if( argv==0 ) shell_out_of_memory();
18728   for(i=0; i<argc; i++){
18729     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
18730     int n;
18731     if( z==0 ) shell_out_of_memory();
18732     n = (int)strlen(z);
18733     argv[i] = malloc( n+1 );
18734     if( argv[i]==0 ) shell_out_of_memory();
18735     memcpy(argv[i], z, n+1);
18736     argvToFree[i] = argv[i];
18737     sqlite3_free(z);
18738   }
18739   sqlite3_shutdown();
18740 #endif
18741 
18742   assert( argc>=1 && argv && argv[0] );
18743   Argv0 = argv[0];
18744 
18745   /* Make sure we have a valid signal handler early, before anything
18746   ** else is done.
18747   */
18748 #ifdef SIGINT
18749   signal(SIGINT, interrupt_handler);
18750 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
18751   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
18752 #endif
18753 
18754 #ifdef SQLITE_SHELL_DBNAME_PROC
18755   {
18756     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
18757     ** of a C-function that will provide the name of the database file.  Use
18758     ** this compile-time option to embed this shell program in larger
18759     ** applications. */
18760     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
18761     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
18762     warnInmemoryDb = 0;
18763   }
18764 #endif
18765 
18766   /* Do an initial pass through the command-line argument to locate
18767   ** the name of the database file, the name of the initialization file,
18768   ** the size of the alternative malloc heap,
18769   ** and the first command to execute.
18770   */
18771   verify_uninitialized();
18772   for(i=1; i<argc; i++){
18773     char *z;
18774     z = argv[i];
18775     if( z[0]!='-' ){
18776       if( data.zDbFilename==0 ){
18777         data.zDbFilename = z;
18778       }else{
18779         /* Excesss arguments are interpreted as SQL (or dot-commands) and
18780         ** mean that nothing is read from stdin */
18781         readStdin = 0;
18782         nCmd++;
18783         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
18784         if( azCmd==0 ) shell_out_of_memory();
18785         azCmd[nCmd-1] = z;
18786       }
18787     }
18788     if( z[1]=='-' ) z++;
18789     if( strcmp(z,"-separator")==0
18790      || strcmp(z,"-nullvalue")==0
18791      || strcmp(z,"-newline")==0
18792      || strcmp(z,"-cmd")==0
18793     ){
18794       (void)cmdline_option_value(argc, argv, ++i);
18795     }else if( strcmp(z,"-init")==0 ){
18796       zInitFile = cmdline_option_value(argc, argv, ++i);
18797     }else if( strcmp(z,"-batch")==0 ){
18798       /* Need to check for batch mode here to so we can avoid printing
18799       ** informational messages (like from process_sqliterc) before
18800       ** we do the actual processing of arguments later in a second pass.
18801       */
18802       stdin_is_interactive = 0;
18803     }else if( strcmp(z,"-heap")==0 ){
18804 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
18805       const char *zSize;
18806       sqlite3_int64 szHeap;
18807 
18808       zSize = cmdline_option_value(argc, argv, ++i);
18809       szHeap = integerValue(zSize);
18810       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
18811       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
18812 #else
18813       (void)cmdline_option_value(argc, argv, ++i);
18814 #endif
18815     }else if( strcmp(z,"-pagecache")==0 ){
18816       int n, sz;
18817       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
18818       if( sz>70000 ) sz = 70000;
18819       if( sz<0 ) sz = 0;
18820       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
18821       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
18822                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
18823       data.shellFlgs |= SHFLG_Pagecache;
18824     }else if( strcmp(z,"-lookaside")==0 ){
18825       int n, sz;
18826       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
18827       if( sz<0 ) sz = 0;
18828       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
18829       if( n<0 ) n = 0;
18830       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
18831       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
18832 #ifdef SQLITE_ENABLE_VFSTRACE
18833     }else if( strcmp(z,"-vfstrace")==0 ){
18834       extern int vfstrace_register(
18835          const char *zTraceName,
18836          const char *zOldVfsName,
18837          int (*xOut)(const char*,void*),
18838          void *pOutArg,
18839          int makeDefault
18840       );
18841       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
18842 #endif
18843 #ifdef SQLITE_ENABLE_MULTIPLEX
18844     }else if( strcmp(z,"-multiplex")==0 ){
18845       extern int sqlite3_multiple_initialize(const char*,int);
18846       sqlite3_multiplex_initialize(0, 1);
18847 #endif
18848     }else if( strcmp(z,"-mmap")==0 ){
18849       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
18850       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
18851 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
18852     }else if( strcmp(z,"-sorterref")==0 ){
18853       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
18854       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
18855 #endif
18856     }else if( strcmp(z,"-vfs")==0 ){
18857       zVfs = cmdline_option_value(argc, argv, ++i);
18858 #ifdef SQLITE_HAVE_ZLIB
18859     }else if( strcmp(z,"-zip")==0 ){
18860       data.openMode = SHELL_OPEN_ZIPFILE;
18861 #endif
18862     }else if( strcmp(z,"-append")==0 ){
18863       data.openMode = SHELL_OPEN_APPENDVFS;
18864 #ifdef SQLITE_ENABLE_DESERIALIZE
18865     }else if( strcmp(z,"-deserialize")==0 ){
18866       data.openMode = SHELL_OPEN_DESERIALIZE;
18867     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
18868       data.szMax = integerValue(argv[++i]);
18869 #endif
18870     }else if( strcmp(z,"-readonly")==0 ){
18871       data.openMode = SHELL_OPEN_READONLY;
18872     }else if( strcmp(z,"-nofollow")==0 ){
18873       data.openFlags = SQLITE_OPEN_NOFOLLOW;
18874 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
18875     }else if( strncmp(z, "-A",2)==0 ){
18876       /* All remaining command-line arguments are passed to the ".archive"
18877       ** command, so ignore them */
18878       break;
18879 #endif
18880     }else if( strcmp(z, "-memtrace")==0 ){
18881       sqlite3MemTraceActivate(stderr);
18882     }
18883   }
18884   verify_uninitialized();
18885 
18886 
18887 #ifdef SQLITE_SHELL_INIT_PROC
18888   {
18889     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
18890     ** of a C-function that will perform initialization actions on SQLite that
18891     ** occur just before or after sqlite3_initialize(). Use this compile-time
18892     ** option to embed this shell program in larger applications. */
18893     extern void SQLITE_SHELL_INIT_PROC(void);
18894     SQLITE_SHELL_INIT_PROC();
18895   }
18896 #else
18897   /* All the sqlite3_config() calls have now been made. So it is safe
18898   ** to call sqlite3_initialize() and process any command line -vfs option. */
18899   sqlite3_initialize();
18900 #endif
18901 
18902   if( zVfs ){
18903     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
18904     if( pVfs ){
18905       sqlite3_vfs_register(pVfs, 1);
18906     }else{
18907       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
18908       exit(1);
18909     }
18910   }
18911 
18912   if( data.zDbFilename==0 ){
18913 #ifndef SQLITE_OMIT_MEMORYDB
18914     data.zDbFilename = ":memory:";
18915     warnInmemoryDb = argc==1;
18916 #else
18917     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
18918     return 1;
18919 #endif
18920   }
18921   data.out = stdout;
18922   sqlite3_appendvfs_init(0,0,0);
18923 
18924   /* Go ahead and open the database file if it already exists.  If the
18925   ** file does not exist, delay opening it.  This prevents empty database
18926   ** files from being created if a user mistypes the database name argument
18927   ** to the sqlite command-line tool.
18928   */
18929   if( access(data.zDbFilename, 0)==0 ){
18930     open_db(&data, 0);
18931   }
18932 
18933   /* Process the initialization file if there is one.  If no -init option
18934   ** is given on the command line, look for a file named ~/.sqliterc and
18935   ** try to process it.
18936   */
18937   process_sqliterc(&data,zInitFile);
18938 
18939   /* Make a second pass through the command-line argument and set
18940   ** options.  This second pass is delayed until after the initialization
18941   ** file is processed so that the command-line arguments will override
18942   ** settings in the initialization file.
18943   */
18944   for(i=1; i<argc; i++){
18945     char *z = argv[i];
18946     if( z[0]!='-' ) continue;
18947     if( z[1]=='-' ){ z++; }
18948     if( strcmp(z,"-init")==0 ){
18949       i++;
18950     }else if( strcmp(z,"-html")==0 ){
18951       data.mode = MODE_Html;
18952     }else if( strcmp(z,"-list")==0 ){
18953       data.mode = MODE_List;
18954     }else if( strcmp(z,"-quote")==0 ){
18955       data.mode = MODE_Quote;
18956     }else if( strcmp(z,"-line")==0 ){
18957       data.mode = MODE_Line;
18958     }else if( strcmp(z,"-column")==0 ){
18959       data.mode = MODE_Column;
18960     }else if( strcmp(z,"-csv")==0 ){
18961       data.mode = MODE_Csv;
18962       memcpy(data.colSeparator,",",2);
18963 #ifdef SQLITE_HAVE_ZLIB
18964     }else if( strcmp(z,"-zip")==0 ){
18965       data.openMode = SHELL_OPEN_ZIPFILE;
18966 #endif
18967     }else if( strcmp(z,"-append")==0 ){
18968       data.openMode = SHELL_OPEN_APPENDVFS;
18969 #ifdef SQLITE_ENABLE_DESERIALIZE
18970     }else if( strcmp(z,"-deserialize")==0 ){
18971       data.openMode = SHELL_OPEN_DESERIALIZE;
18972     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
18973       data.szMax = integerValue(argv[++i]);
18974 #endif
18975     }else if( strcmp(z,"-readonly")==0 ){
18976       data.openMode = SHELL_OPEN_READONLY;
18977     }else if( strcmp(z,"-nofollow")==0 ){
18978       data.openFlags |= SQLITE_OPEN_NOFOLLOW;
18979     }else if( strcmp(z,"-ascii")==0 ){
18980       data.mode = MODE_Ascii;
18981       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
18982                        SEP_Unit);
18983       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
18984                        SEP_Record);
18985     }else if( strcmp(z,"-separator")==0 ){
18986       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
18987                        "%s",cmdline_option_value(argc,argv,++i));
18988     }else if( strcmp(z,"-newline")==0 ){
18989       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
18990                        "%s",cmdline_option_value(argc,argv,++i));
18991     }else if( strcmp(z,"-nullvalue")==0 ){
18992       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
18993                        "%s",cmdline_option_value(argc,argv,++i));
18994     }else if( strcmp(z,"-header")==0 ){
18995       data.showHeader = 1;
18996     }else if( strcmp(z,"-noheader")==0 ){
18997       data.showHeader = 0;
18998     }else if( strcmp(z,"-echo")==0 ){
18999       ShellSetFlag(&data, SHFLG_Echo);
19000     }else if( strcmp(z,"-eqp")==0 ){
19001       data.autoEQP = AUTOEQP_on;
19002     }else if( strcmp(z,"-eqpfull")==0 ){
19003       data.autoEQP = AUTOEQP_full;
19004     }else if( strcmp(z,"-stats")==0 ){
19005       data.statsOn = 1;
19006     }else if( strcmp(z,"-scanstats")==0 ){
19007       data.scanstatsOn = 1;
19008     }else if( strcmp(z,"-backslash")==0 ){
19009       /* Undocumented command-line option: -backslash
19010       ** Causes C-style backslash escapes to be evaluated in SQL statements
19011       ** prior to sending the SQL into SQLite.  Useful for injecting
19012       ** crazy bytes in the middle of SQL statements for testing and debugging.
19013       */
19014       ShellSetFlag(&data, SHFLG_Backslash);
19015     }else if( strcmp(z,"-bail")==0 ){
19016       bail_on_error = 1;
19017     }else if( strcmp(z,"-version")==0 ){
19018       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
19019       return 0;
19020     }else if( strcmp(z,"-interactive")==0 ){
19021       stdin_is_interactive = 1;
19022     }else if( strcmp(z,"-batch")==0 ){
19023       stdin_is_interactive = 0;
19024     }else if( strcmp(z,"-heap")==0 ){
19025       i++;
19026     }else if( strcmp(z,"-pagecache")==0 ){
19027       i+=2;
19028     }else if( strcmp(z,"-lookaside")==0 ){
19029       i+=2;
19030     }else if( strcmp(z,"-mmap")==0 ){
19031       i++;
19032     }else if( strcmp(z,"-memtrace")==0 ){
19033       i++;
19034 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
19035     }else if( strcmp(z,"-sorterref")==0 ){
19036       i++;
19037 #endif
19038     }else if( strcmp(z,"-vfs")==0 ){
19039       i++;
19040 #ifdef SQLITE_ENABLE_VFSTRACE
19041     }else if( strcmp(z,"-vfstrace")==0 ){
19042       i++;
19043 #endif
19044 #ifdef SQLITE_ENABLE_MULTIPLEX
19045     }else if( strcmp(z,"-multiplex")==0 ){
19046       i++;
19047 #endif
19048     }else if( strcmp(z,"-help")==0 ){
19049       usage(1);
19050     }else if( strcmp(z,"-cmd")==0 ){
19051       /* Run commands that follow -cmd first and separately from commands
19052       ** that simply appear on the command-line.  This seems goofy.  It would
19053       ** be better if all commands ran in the order that they appear.  But
19054       ** we retain the goofy behavior for historical compatibility. */
19055       if( i==argc-1 ) break;
19056       z = cmdline_option_value(argc,argv,++i);
19057       if( z[0]=='.' ){
19058         rc = do_meta_command(z, &data);
19059         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
19060       }else{
19061         open_db(&data, 0);
19062         rc = shell_exec(&data, z, &zErrMsg);
19063         if( zErrMsg!=0 ){
19064           utf8_printf(stderr,"Error: %s\n", zErrMsg);
19065           if( bail_on_error ) return rc!=0 ? rc : 1;
19066         }else if( rc!=0 ){
19067           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
19068           if( bail_on_error ) return rc;
19069         }
19070       }
19071 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19072     }else if( strncmp(z, "-A", 2)==0 ){
19073       if( nCmd>0 ){
19074         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
19075                             " with \"%s\"\n", z);
19076         return 1;
19077       }
19078       open_db(&data, OPEN_DB_ZIPFILE);
19079       if( z[2] ){
19080         argv[i] = &z[2];
19081         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
19082       }else{
19083         arDotCommand(&data, 1, argv+i, argc-i);
19084       }
19085       readStdin = 0;
19086       break;
19087 #endif
19088     }else{
19089       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
19090       raw_printf(stderr,"Use -help for a list of options.\n");
19091       return 1;
19092     }
19093     data.cMode = data.mode;
19094   }
19095 
19096   if( !readStdin ){
19097     /* Run all arguments that do not begin with '-' as if they were separate
19098     ** command-line inputs, except for the argToSkip argument which contains
19099     ** the database filename.
19100     */
19101     for(i=0; i<nCmd; i++){
19102       if( azCmd[i][0]=='.' ){
19103         rc = do_meta_command(azCmd[i], &data);
19104         if( rc ) return rc==2 ? 0 : rc;
19105       }else{
19106         open_db(&data, 0);
19107         rc = shell_exec(&data, azCmd[i], &zErrMsg);
19108         if( zErrMsg!=0 ){
19109           utf8_printf(stderr,"Error: %s\n", zErrMsg);
19110           return rc!=0 ? rc : 1;
19111         }else if( rc!=0 ){
19112           utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
19113           return rc;
19114         }
19115       }
19116     }
19117     free(azCmd);
19118   }else{
19119     /* Run commands received from standard input
19120     */
19121     if( stdin_is_interactive ){
19122       char *zHome;
19123       char *zHistory;
19124       int nHistory;
19125       printf(
19126         "SQLite version %s %.19s\n" /*extra-version-info*/
19127         "Enter \".help\" for usage hints.\n",
19128         sqlite3_libversion(), sqlite3_sourceid()
19129       );
19130       if( warnInmemoryDb ){
19131         printf("Connected to a ");
19132         printBold("transient in-memory database");
19133         printf(".\nUse \".open FILENAME\" to reopen on a "
19134                "persistent database.\n");
19135       }
19136       zHistory = getenv("SQLITE_HISTORY");
19137       if( zHistory ){
19138         zHistory = strdup(zHistory);
19139       }else if( (zHome = find_home_dir(0))!=0 ){
19140         nHistory = strlen30(zHome) + 20;
19141         if( (zHistory = malloc(nHistory))!=0 ){
19142           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
19143         }
19144       }
19145       if( zHistory ){ shell_read_history(zHistory); }
19146 #if HAVE_READLINE || HAVE_EDITLINE
19147       rl_attempted_completion_function = readline_completion;
19148 #elif HAVE_LINENOISE
19149       linenoiseSetCompletionCallback(linenoise_completion);
19150 #endif
19151       data.in = 0;
19152       rc = process_input(&data);
19153       if( zHistory ){
19154         shell_stifle_history(2000);
19155         shell_write_history(zHistory);
19156         free(zHistory);
19157       }
19158     }else{
19159       data.in = stdin;
19160       rc = process_input(&data);
19161     }
19162   }
19163   set_table_name(&data, 0);
19164   if( data.db ){
19165     session_close_all(&data);
19166     close_db(data.db);
19167   }
19168   sqlite3_free(data.zFreeOnClose);
19169   find_home_dir(1);
19170   output_reset(&data);
19171   data.doXdgOpen = 0;
19172   clearTempFile(&data);
19173 #if !SQLITE_SHELL_IS_UTF8
19174   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
19175   free(argvToFree);
19176 #endif
19177   /* Clear the global data structure so that valgrind will detect memory
19178   ** leaks */
19179   memset(&data, 0, sizeof(data));
19180   return rc;
19181 }
19182