]> pd.if.org Git - zpackage/blob - sqlite/shell.c
bump sqlite to version 3.24.0
[zpackage] / sqlite / shell.c
1 /* DO NOT EDIT!
2 ** This file is automatically generated by the script in the canonical
3 ** SQLite source tree at tool/mkshellc.tcl.  That script combines source
4 ** code from various constituent source files of SQLite into this single
5 ** "shell.c" file used to implement the SQLite command-line shell.
6 **
7 ** Most of the code found below comes from the "src/shell.c.in" file in
8 ** the canonical SQLite source tree.  That main file contains "INCLUDE"
9 ** lines that specify other files in the canonical source tree that are
10 ** inserted to getnerate this complete program source file.
11 **
12 ** The code from multiple files is combined into this single "shell.c"
13 ** source file to help make the command-line program easier to compile.
14 **
15 ** To modify this program, get a copy of the canonical SQLite source tree,
16 ** edit the src/shell.c.in" and/or some of the other files that are included
17 ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
18 */
19 /*
20 ** 2001 September 15
21 **
22 ** The author disclaims copyright to this source code.  In place of
23 ** a legal notice, here is a blessing:
24 **
25 **    May you do good and not evil.
26 **    May you find forgiveness for yourself and forgive others.
27 **    May you share freely, never taking more than you give.
28 **
29 *************************************************************************
30 ** This file contains code to implement the "sqlite" command line
31 ** utility for accessing SQLite databases.
32 */
33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34 /* This needs to come before any includes for MSVC compiler */
35 #define _CRT_SECURE_NO_WARNINGS
36 #endif
37
38 /*
39 ** Warning pragmas copied from msvc.h in the core.
40 */
41 #if defined(_MSC_VER)
42 #pragma warning(disable : 4054)
43 #pragma warning(disable : 4055)
44 #pragma warning(disable : 4100)
45 #pragma warning(disable : 4127)
46 #pragma warning(disable : 4130)
47 #pragma warning(disable : 4152)
48 #pragma warning(disable : 4189)
49 #pragma warning(disable : 4206)
50 #pragma warning(disable : 4210)
51 #pragma warning(disable : 4232)
52 #pragma warning(disable : 4244)
53 #pragma warning(disable : 4305)
54 #pragma warning(disable : 4306)
55 #pragma warning(disable : 4702)
56 #pragma warning(disable : 4706)
57 #endif /* defined(_MSC_VER) */
58
59 /*
60 ** No support for loadable extensions in VxWorks.
61 */
62 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
63 # define SQLITE_OMIT_LOAD_EXTENSION 1
64 #endif
65
66 /*
67 ** Enable large-file support for fopen() and friends on unix.
68 */
69 #ifndef SQLITE_DISABLE_LFS
70 # define _LARGE_FILE       1
71 # ifndef _FILE_OFFSET_BITS
72 #   define _FILE_OFFSET_BITS 64
73 # endif
74 # define _LARGEFILE_SOURCE 1
75 #endif
76
77 #include <stdlib.h>
78 #include <string.h>
79 #include <stdio.h>
80 #include <assert.h>
81 #include "sqlite3.h"
82 typedef sqlite3_int64 i64;
83 typedef sqlite3_uint64 u64;
84 typedef unsigned char u8;
85 #if SQLITE_USER_AUTHENTICATION
86 # include "sqlite3userauth.h"
87 #endif
88 #include <ctype.h>
89 #include <stdarg.h>
90
91 #if !defined(_WIN32) && !defined(WIN32)
92 # include <signal.h>
93 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
94 #  include <pwd.h>
95 # endif
96 #endif
97 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
98 # include <unistd.h>
99 # include <dirent.h>
100 # if defined(__MINGW32__)
101 #  define DIRENT dirent
102 #  ifndef S_ISLNK
103 #   define S_ISLNK(mode) (0)
104 #  endif
105 # endif
106 #endif
107 #include <sys/types.h>
108 #include <sys/stat.h>
109
110 #if HAVE_READLINE
111 # include <readline/readline.h>
112 # include <readline/history.h>
113 #endif
114
115 #if HAVE_EDITLINE
116 # include <editline/readline.h>
117 #endif
118
119 #if HAVE_EDITLINE || HAVE_READLINE
120
121 # define shell_add_history(X) add_history(X)
122 # define shell_read_history(X) read_history(X)
123 # define shell_write_history(X) write_history(X)
124 # define shell_stifle_history(X) stifle_history(X)
125 # define shell_readline(X) readline(X)
126
127 #elif HAVE_LINENOISE
128
129 # include "linenoise.h"
130 # define shell_add_history(X) linenoiseHistoryAdd(X)
131 # define shell_read_history(X) linenoiseHistoryLoad(X)
132 # define shell_write_history(X) linenoiseHistorySave(X)
133 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
134 # define shell_readline(X) linenoise(X)
135
136 #else
137
138 # define shell_read_history(X)
139 # define shell_write_history(X)
140 # define shell_stifle_history(X)
141
142 # define SHELL_USE_LOCAL_GETLINE 1
143 #endif
144
145
146 #if defined(_WIN32) || defined(WIN32)
147 # include <io.h>
148 # include <fcntl.h>
149 # define isatty(h) _isatty(h)
150 # ifndef access
151 #  define access(f,m) _access((f),(m))
152 # endif
153 # ifndef unlink
154 #  define unlink _unlink
155 # endif
156 # undef popen
157 # define popen _popen
158 # undef pclose
159 # define pclose _pclose
160 #else
161  /* Make sure isatty() has a prototype. */
162  extern int isatty(int);
163
164 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
165   /* popen and pclose are not C89 functions and so are
166   ** sometimes omitted from the <stdio.h> header */
167    extern FILE *popen(const char*,const char*);
168    extern int pclose(FILE*);
169 # else
170 #  define SQLITE_OMIT_POPEN 1
171 # endif
172 #endif
173
174 #if defined(_WIN32_WCE)
175 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176  * thus we always assume that we have a console. That can be
177  * overridden with the -batch command line option.
178  */
179 #define isatty(x) 1
180 #endif
181
182 /* ctype macros that work with signed characters */
183 #define IsSpace(X)  isspace((unsigned char)X)
184 #define IsDigit(X)  isdigit((unsigned char)X)
185 #define ToLower(X)  (char)tolower((unsigned char)X)
186
187 #if defined(_WIN32) || defined(WIN32)
188 #include <windows.h>
189
190 /* string conversion routines only needed on Win32 */
191 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
192 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
193 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
194 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
195 #endif
196
197 /* On Windows, we normally run with output mode of TEXT so that \n characters
198 ** are automatically translated into \r\n.  However, this behavior needs
199 ** to be disabled in some cases (ex: when generating CSV output and when
200 ** rendering quoted strings that contain \n characters).  The following
201 ** routines take care of that.
202 */
203 #if defined(_WIN32) || defined(WIN32)
204 static void setBinaryMode(FILE *file, int isOutput){
205   if( isOutput ) fflush(file);
206   _setmode(_fileno(file), _O_BINARY);
207 }
208 static void setTextMode(FILE *file, int isOutput){
209   if( isOutput ) fflush(file);
210   _setmode(_fileno(file), _O_TEXT);
211 }
212 #else
213 # define setBinaryMode(X,Y)
214 # define setTextMode(X,Y)
215 #endif
216
217
218 /* True if the timer is enabled */
219 static int enableTimer = 0;
220
221 /* Return the current wall-clock time */
222 static sqlite3_int64 timeOfDay(void){
223   static sqlite3_vfs *clockVfs = 0;
224   sqlite3_int64 t;
225   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
226   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
227     clockVfs->xCurrentTimeInt64(clockVfs, &t);
228   }else{
229     double r;
230     clockVfs->xCurrentTime(clockVfs, &r);
231     t = (sqlite3_int64)(r*86400000.0);
232   }
233   return t;
234 }
235
236 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
237 #include <sys/time.h>
238 #include <sys/resource.h>
239
240 /* VxWorks does not support getrusage() as far as we can determine */
241 #if defined(_WRS_KERNEL) || defined(__RTP__)
242 struct rusage {
243   struct timeval ru_utime; /* user CPU time used */
244   struct timeval ru_stime; /* system CPU time used */
245 };
246 #define getrusage(A,B) memset(B,0,sizeof(*B))
247 #endif
248
249 /* Saved resource information for the beginning of an operation */
250 static struct rusage sBegin;  /* CPU time at start */
251 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
252
253 /*
254 ** Begin timing an operation
255 */
256 static void beginTimer(void){
257   if( enableTimer ){
258     getrusage(RUSAGE_SELF, &sBegin);
259     iBegin = timeOfDay();
260   }
261 }
262
263 /* Return the difference of two time_structs in seconds */
264 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
265   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
266          (double)(pEnd->tv_sec - pStart->tv_sec);
267 }
268
269 /*
270 ** Print the timing results.
271 */
272 static void endTimer(void){
273   if( enableTimer ){
274     sqlite3_int64 iEnd = timeOfDay();
275     struct rusage sEnd;
276     getrusage(RUSAGE_SELF, &sEnd);
277     printf("Run Time: real %.3f user %f sys %f\n",
278        (iEnd - iBegin)*0.001,
279        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
280        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
281   }
282 }
283
284 #define BEGIN_TIMER beginTimer()
285 #define END_TIMER endTimer()
286 #define HAS_TIMER 1
287
288 #elif (defined(_WIN32) || defined(WIN32))
289
290 /* Saved resource information for the beginning of an operation */
291 static HANDLE hProcess;
292 static FILETIME ftKernelBegin;
293 static FILETIME ftUserBegin;
294 static sqlite3_int64 ftWallBegin;
295 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
296                                     LPFILETIME, LPFILETIME);
297 static GETPROCTIMES getProcessTimesAddr = NULL;
298
299 /*
300 ** Check to see if we have timer support.  Return 1 if necessary
301 ** support found (or found previously).
302 */
303 static int hasTimer(void){
304   if( getProcessTimesAddr ){
305     return 1;
306   } else {
307     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
308     ** versions. See if the version we are running on has it, and if it
309     ** does, save off a pointer to it and the current process handle.
310     */
311     hProcess = GetCurrentProcess();
312     if( hProcess ){
313       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
314       if( NULL != hinstLib ){
315         getProcessTimesAddr =
316             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
317         if( NULL != getProcessTimesAddr ){
318           return 1;
319         }
320         FreeLibrary(hinstLib);
321       }
322     }
323   }
324   return 0;
325 }
326
327 /*
328 ** Begin timing an operation
329 */
330 static void beginTimer(void){
331   if( enableTimer && getProcessTimesAddr ){
332     FILETIME ftCreation, ftExit;
333     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
334                         &ftKernelBegin,&ftUserBegin);
335     ftWallBegin = timeOfDay();
336   }
337 }
338
339 /* Return the difference of two FILETIME structs in seconds */
340 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
341   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
342   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
343   return (double) ((i64End - i64Start) / 10000000.0);
344 }
345
346 /*
347 ** Print the timing results.
348 */
349 static void endTimer(void){
350   if( enableTimer && getProcessTimesAddr){
351     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
352     sqlite3_int64 ftWallEnd = timeOfDay();
353     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
354     printf("Run Time: real %.3f user %f sys %f\n",
355        (ftWallEnd - ftWallBegin)*0.001,
356        timeDiff(&ftUserBegin, &ftUserEnd),
357        timeDiff(&ftKernelBegin, &ftKernelEnd));
358   }
359 }
360
361 #define BEGIN_TIMER beginTimer()
362 #define END_TIMER endTimer()
363 #define HAS_TIMER hasTimer()
364
365 #else
366 #define BEGIN_TIMER
367 #define END_TIMER
368 #define HAS_TIMER 0
369 #endif
370
371 /*
372 ** Used to prevent warnings about unused parameters
373 */
374 #define UNUSED_PARAMETER(x) (void)(x)
375
376 /*
377 ** Number of elements in an array
378 */
379 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
380
381 /*
382 ** If the following flag is set, then command execution stops
383 ** at an error if we are not interactive.
384 */
385 static int bail_on_error = 0;
386
387 /*
388 ** Threat stdin as an interactive input if the following variable
389 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
390 */
391 static int stdin_is_interactive = 1;
392
393 /*
394 ** On Windows systems we have to know if standard output is a console
395 ** in order to translate UTF-8 into MBCS.  The following variable is
396 ** true if translation is required.
397 */
398 static int stdout_is_console = 1;
399
400 /*
401 ** The following is the open SQLite database.  We make a pointer
402 ** to this database a static variable so that it can be accessed
403 ** by the SIGINT handler to interrupt database processing.
404 */
405 static sqlite3 *globalDb = 0;
406
407 /*
408 ** True if an interrupt (Control-C) has been received.
409 */
410 static volatile int seenInterrupt = 0;
411
412 /*
413 ** This is the name of our program. It is set in main(), used
414 ** in a number of other places, mostly for error messages.
415 */
416 static char *Argv0;
417
418 /*
419 ** Prompt strings. Initialized in main. Settable with
420 **   .prompt main continue
421 */
422 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
423 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
424
425 /*
426 ** Render output like fprintf().  Except, if the output is going to the
427 ** console and if this is running on a Windows machine, translate the
428 ** output from UTF-8 into MBCS.
429 */
430 #if defined(_WIN32) || defined(WIN32)
431 void utf8_printf(FILE *out, const char *zFormat, ...){
432   va_list ap;
433   va_start(ap, zFormat);
434   if( stdout_is_console && (out==stdout || out==stderr) ){
435     char *z1 = sqlite3_vmprintf(zFormat, ap);
436     char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
437     sqlite3_free(z1);
438     fputs(z2, out);
439     sqlite3_free(z2);
440   }else{
441     vfprintf(out, zFormat, ap);
442   }
443   va_end(ap);
444 }
445 #elif !defined(utf8_printf)
446 # define utf8_printf fprintf
447 #endif
448
449 /*
450 ** Render output like fprintf().  This should not be used on anything that
451 ** includes string formatting (e.g. "%s").
452 */
453 #if !defined(raw_printf)
454 # define raw_printf fprintf
455 #endif
456
457 /* Indicate out-of-memory and exit. */
458 static void shell_out_of_memory(void){
459   raw_printf(stderr,"Error: out of memory\n");
460   exit(1);
461 }
462
463 /*
464 ** Write I/O traces to the following stream.
465 */
466 #ifdef SQLITE_ENABLE_IOTRACE
467 static FILE *iotrace = 0;
468 #endif
469
470 /*
471 ** This routine works like printf in that its first argument is a
472 ** format string and subsequent arguments are values to be substituted
473 ** in place of % fields.  The result of formatting this string
474 ** is written to iotrace.
475 */
476 #ifdef SQLITE_ENABLE_IOTRACE
477 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
478   va_list ap;
479   char *z;
480   if( iotrace==0 ) return;
481   va_start(ap, zFormat);
482   z = sqlite3_vmprintf(zFormat, ap);
483   va_end(ap);
484   utf8_printf(iotrace, "%s", z);
485   sqlite3_free(z);
486 }
487 #endif
488
489 /*
490 ** Output string zUtf to stream pOut as w characters.  If w is negative,
491 ** then right-justify the text.  W is the width in UTF-8 characters, not
492 ** in bytes.  This is different from the %*.*s specification in printf
493 ** since with %*.*s the width is measured in bytes, not characters.
494 */
495 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
496   int i;
497   int n;
498   int aw = w<0 ? -w : w;
499   char zBuf[1000];
500   if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
501   for(i=n=0; zUtf[i]; i++){
502     if( (zUtf[i]&0xc0)!=0x80 ){
503       n++;
504       if( n==aw ){
505         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
506         break;
507       }
508     }
509   }
510   if( n>=aw ){
511     utf8_printf(pOut, "%.*s", i, zUtf);
512   }else if( w<0 ){
513     utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
514   }else{
515     utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
516   }
517 }
518
519
520 /*
521 ** Determines if a string is a number of not.
522 */
523 static int isNumber(const char *z, int *realnum){
524   if( *z=='-' || *z=='+' ) z++;
525   if( !IsDigit(*z) ){
526     return 0;
527   }
528   z++;
529   if( realnum ) *realnum = 0;
530   while( IsDigit(*z) ){ z++; }
531   if( *z=='.' ){
532     z++;
533     if( !IsDigit(*z) ) return 0;
534     while( IsDigit(*z) ){ z++; }
535     if( realnum ) *realnum = 1;
536   }
537   if( *z=='e' || *z=='E' ){
538     z++;
539     if( *z=='+' || *z=='-' ) z++;
540     if( !IsDigit(*z) ) return 0;
541     while( IsDigit(*z) ){ z++; }
542     if( realnum ) *realnum = 1;
543   }
544   return *z==0;
545 }
546
547 /*
548 ** Compute a string length that is limited to what can be stored in
549 ** lower 30 bits of a 32-bit signed integer.
550 */
551 static int strlen30(const char *z){
552   const char *z2 = z;
553   while( *z2 ){ z2++; }
554   return 0x3fffffff & (int)(z2 - z);
555 }
556
557 /*
558 ** Return the length of a string in characters.  Multibyte UTF8 characters
559 ** count as a single character.
560 */
561 static int strlenChar(const char *z){
562   int n = 0;
563   while( *z ){
564     if( (0xc0&*(z++))!=0x80 ) n++;
565   }
566   return n;
567 }
568
569 /*
570 ** This routine reads a line of text from FILE in, stores
571 ** the text in memory obtained from malloc() and returns a pointer
572 ** to the text.  NULL is returned at end of file, or if malloc()
573 ** fails.
574 **
575 ** If zLine is not NULL then it is a malloced buffer returned from
576 ** a previous call to this routine that may be reused.
577 */
578 static char *local_getline(char *zLine, FILE *in){
579   int nLine = zLine==0 ? 0 : 100;
580   int n = 0;
581
582   while( 1 ){
583     if( n+100>nLine ){
584       nLine = nLine*2 + 100;
585       zLine = realloc(zLine, nLine);
586       if( zLine==0 ) return 0;
587     }
588     if( fgets(&zLine[n], nLine - n, in)==0 ){
589       if( n==0 ){
590         free(zLine);
591         return 0;
592       }
593       zLine[n] = 0;
594       break;
595     }
596     while( zLine[n] ) n++;
597     if( n>0 && zLine[n-1]=='\n' ){
598       n--;
599       if( n>0 && zLine[n-1]=='\r' ) n--;
600       zLine[n] = 0;
601       break;
602     }
603   }
604 #if defined(_WIN32) || defined(WIN32)
605   /* For interactive input on Windows systems, translate the
606   ** multi-byte characterset characters into UTF-8. */
607   if( stdin_is_interactive && in==stdin ){
608     char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
609     if( zTrans ){
610       int nTrans = strlen30(zTrans)+1;
611       if( nTrans>nLine ){
612         zLine = realloc(zLine, nTrans);
613         if( zLine==0 ){
614           sqlite3_free(zTrans);
615           return 0;
616         }
617       }
618       memcpy(zLine, zTrans, nTrans);
619       sqlite3_free(zTrans);
620     }
621   }
622 #endif /* defined(_WIN32) || defined(WIN32) */
623   return zLine;
624 }
625
626 /*
627 ** Retrieve a single line of input text.
628 **
629 ** If in==0 then read from standard input and prompt before each line.
630 ** If isContinuation is true, then a continuation prompt is appropriate.
631 ** If isContinuation is zero, then the main prompt should be used.
632 **
633 ** If zPrior is not NULL then it is a buffer from a prior call to this
634 ** routine that can be reused.
635 **
636 ** The result is stored in space obtained from malloc() and must either
637 ** be freed by the caller or else passed back into this routine via the
638 ** zPrior argument for reuse.
639 */
640 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
641   char *zPrompt;
642   char *zResult;
643   if( in!=0 ){
644     zResult = local_getline(zPrior, in);
645   }else{
646     zPrompt = isContinuation ? continuePrompt : mainPrompt;
647 #if SHELL_USE_LOCAL_GETLINE
648     printf("%s", zPrompt);
649     fflush(stdout);
650     zResult = local_getline(zPrior, stdin);
651 #else
652     free(zPrior);
653     zResult = shell_readline(zPrompt);
654     if( zResult && *zResult ) shell_add_history(zResult);
655 #endif
656   }
657   return zResult;
658 }
659
660
661 /*
662 ** Return the value of a hexadecimal digit.  Return -1 if the input
663 ** is not a hex digit.
664 */
665 static int hexDigitValue(char c){
666   if( c>='0' && c<='9' ) return c - '0';
667   if( c>='a' && c<='f' ) return c - 'a' + 10;
668   if( c>='A' && c<='F' ) return c - 'A' + 10;
669   return -1;
670 }
671
672 /*
673 ** Interpret zArg as an integer value, possibly with suffixes.
674 */
675 static sqlite3_int64 integerValue(const char *zArg){
676   sqlite3_int64 v = 0;
677   static const struct { char *zSuffix; int iMult; } aMult[] = {
678     { "KiB", 1024 },
679     { "MiB", 1024*1024 },
680     { "GiB", 1024*1024*1024 },
681     { "KB",  1000 },
682     { "MB",  1000000 },
683     { "GB",  1000000000 },
684     { "K",   1000 },
685     { "M",   1000000 },
686     { "G",   1000000000 },
687   };
688   int i;
689   int isNeg = 0;
690   if( zArg[0]=='-' ){
691     isNeg = 1;
692     zArg++;
693   }else if( zArg[0]=='+' ){
694     zArg++;
695   }
696   if( zArg[0]=='0' && zArg[1]=='x' ){
697     int x;
698     zArg += 2;
699     while( (x = hexDigitValue(zArg[0]))>=0 ){
700       v = (v<<4) + x;
701       zArg++;
702     }
703   }else{
704     while( IsDigit(zArg[0]) ){
705       v = v*10 + zArg[0] - '0';
706       zArg++;
707     }
708   }
709   for(i=0; i<ArraySize(aMult); i++){
710     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
711       v *= aMult[i].iMult;
712       break;
713     }
714   }
715   return isNeg? -v : v;
716 }
717
718 /*
719 ** A variable length string to which one can append text.
720 */
721 typedef struct ShellText ShellText;
722 struct ShellText {
723   char *z;
724   int n;
725   int nAlloc;
726 };
727
728 /*
729 ** Initialize and destroy a ShellText object
730 */
731 static void initText(ShellText *p){
732   memset(p, 0, sizeof(*p));
733 }
734 static void freeText(ShellText *p){
735   free(p->z);
736   initText(p);
737 }
738
739 /* zIn is either a pointer to a NULL-terminated string in memory obtained
740 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
741 ** added to zIn, and the result returned in memory obtained from malloc().
742 ** zIn, if it was not NULL, is freed.
743 **
744 ** If the third argument, quote, is not '\0', then it is used as a
745 ** quote character for zAppend.
746 */
747 static void appendText(ShellText *p, char const *zAppend, char quote){
748   int len;
749   int i;
750   int nAppend = strlen30(zAppend);
751
752   len = nAppend+p->n+1;
753   if( quote ){
754     len += 2;
755     for(i=0; i<nAppend; i++){
756       if( zAppend[i]==quote ) len++;
757     }
758   }
759
760   if( p->n+len>=p->nAlloc ){
761     p->nAlloc = p->nAlloc*2 + len + 20;
762     p->z = realloc(p->z, p->nAlloc);
763     if( p->z==0 ){
764       memset(p, 0, sizeof(*p));
765       return;
766     }
767   }
768
769   if( quote ){
770     char *zCsr = p->z+p->n;
771     *zCsr++ = quote;
772     for(i=0; i<nAppend; i++){
773       *zCsr++ = zAppend[i];
774       if( zAppend[i]==quote ) *zCsr++ = quote;
775     }
776     *zCsr++ = quote;
777     p->n = (int)(zCsr - p->z);
778     *zCsr = '\0';
779   }else{
780     memcpy(p->z+p->n, zAppend, nAppend);
781     p->n += nAppend;
782     p->z[p->n] = '\0';
783   }
784 }
785
786 /*
787 ** Attempt to determine if identifier zName needs to be quoted, either
788 ** because it contains non-alphanumeric characters, or because it is an
789 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
790 ** that quoting is required.
791 **
792 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
793 */
794 static char quoteChar(const char *zName){
795   int i;
796   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
797   for(i=0; zName[i]; i++){
798     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
799   }
800   return sqlite3_keyword_check(zName, i) ? '"' : 0;
801 }
802
803 /*
804 ** Construct a fake object name and column list to describe the structure
805 ** of the view, virtual table, or table valued function zSchema.zName.
806 */
807 static char *shellFakeSchema(
808   sqlite3 *db,            /* The database connection containing the vtab */
809   const char *zSchema,    /* Schema of the database holding the vtab */
810   const char *zName       /* The name of the virtual table */
811 ){
812   sqlite3_stmt *pStmt = 0;
813   char *zSql;
814   ShellText s;
815   char cQuote;
816   char *zDiv = "(";
817   int nRow = 0;
818
819   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
820                          zSchema ? zSchema : "main", zName);
821   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
822   sqlite3_free(zSql);
823   initText(&s);
824   if( zSchema ){
825     cQuote = quoteChar(zSchema);
826     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
827     appendText(&s, zSchema, cQuote);
828     appendText(&s, ".", 0);
829   }
830   cQuote = quoteChar(zName);
831   appendText(&s, zName, cQuote);
832   while( sqlite3_step(pStmt)==SQLITE_ROW ){
833     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
834     nRow++;
835     appendText(&s, zDiv, 0);
836     zDiv = ",";
837     cQuote = quoteChar(zCol);
838     appendText(&s, zCol, cQuote);
839   }
840   appendText(&s, ")", 0);
841   sqlite3_finalize(pStmt);
842   if( nRow==0 ){
843     freeText(&s);
844     s.z = 0;
845   }
846   return s.z;
847 }
848
849 /*
850 ** SQL function:  shell_module_schema(X)
851 **
852 ** Return a fake schema for the table-valued function or eponymous virtual
853 ** table X.
854 */
855 static void shellModuleSchema(
856   sqlite3_context *pCtx,
857   int nVal,
858   sqlite3_value **apVal
859 ){
860   const char *zName = (const char*)sqlite3_value_text(apVal[0]);
861   char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
862   UNUSED_PARAMETER(nVal);
863   if( zFake ){
864     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
865                         -1, sqlite3_free);
866     free(zFake);
867   }
868 }
869
870 /*
871 ** SQL function:  shell_add_schema(S,X)
872 **
873 ** Add the schema name X to the CREATE statement in S and return the result.
874 ** Examples:
875 **
876 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
877 **
878 ** Also works on
879 **
880 **    CREATE INDEX
881 **    CREATE UNIQUE INDEX
882 **    CREATE VIEW
883 **    CREATE TRIGGER
884 **    CREATE VIRTUAL TABLE
885 **
886 ** This UDF is used by the .schema command to insert the schema name of
887 ** attached databases into the middle of the sqlite_master.sql field.
888 */
889 static void shellAddSchemaName(
890   sqlite3_context *pCtx,
891   int nVal,
892   sqlite3_value **apVal
893 ){
894   static const char *aPrefix[] = {
895      "TABLE",
896      "INDEX",
897      "UNIQUE INDEX",
898      "VIEW",
899      "TRIGGER",
900      "VIRTUAL TABLE"
901   };
902   int i = 0;
903   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
904   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
905   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
906   sqlite3 *db = sqlite3_context_db_handle(pCtx);
907   UNUSED_PARAMETER(nVal);
908   if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
909     for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
910       int n = strlen30(aPrefix[i]);
911       if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
912         char *z = 0;
913         char *zFake = 0;
914         if( zSchema ){
915           char cQuote = quoteChar(zSchema);
916           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
917             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
918           }else{
919             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
920           }
921         }
922         if( zName
923          && aPrefix[i][0]=='V'
924          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
925         ){
926           if( z==0 ){
927             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
928           }else{
929             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
930           }
931           free(zFake);
932         }
933         if( z ){
934           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
935           return;
936         }
937       }
938     }
939   }
940   sqlite3_result_value(pCtx, apVal[0]);
941 }
942
943 /*
944 ** The source code for several run-time loadable extensions is inserted
945 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
946 ** code, we need to override some macros to make the included program code
947 ** work here in the middle of this regular program.
948 */
949 #define SQLITE_EXTENSION_INIT1
950 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
951
952 #if defined(_WIN32) && defined(_MSC_VER)
953 /************************* Begin test_windirent.h ******************/
954 /*
955 ** 2015 November 30
956 **
957 ** The author disclaims copyright to this source code.  In place of
958 ** a legal notice, here is a blessing:
959 **
960 **    May you do good and not evil.
961 **    May you find forgiveness for yourself and forgive others.
962 **    May you share freely, never taking more than you give.
963 **
964 *************************************************************************
965 ** This file contains declarations for most of the opendir() family of
966 ** POSIX functions on Win32 using the MSVCRT.
967 */
968
969 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
970 #define SQLITE_WINDIRENT_H
971
972 /*
973 ** We need several data types from the Windows SDK header.
974 */
975
976 #ifndef WIN32_LEAN_AND_MEAN
977 #define WIN32_LEAN_AND_MEAN
978 #endif
979
980 #include "windows.h"
981
982 /*
983 ** We need several support functions from the SQLite core.
984 */
985
986
987 /*
988 ** We need several things from the ANSI and MSVCRT headers.
989 */
990
991 #include <stdio.h>
992 #include <stdlib.h>
993 #include <errno.h>
994 #include <io.h>
995 #include <limits.h>
996 #include <sys/types.h>
997 #include <sys/stat.h>
998
999 /*
1000 ** We may need several defines that should have been in "sys/stat.h".
1001 */
1002
1003 #ifndef S_ISREG
1004 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1005 #endif
1006
1007 #ifndef S_ISDIR
1008 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1009 #endif
1010
1011 #ifndef S_ISLNK
1012 #define S_ISLNK(mode) (0)
1013 #endif
1014
1015 /*
1016 ** We may need to provide the "mode_t" type.
1017 */
1018
1019 #ifndef MODE_T_DEFINED
1020   #define MODE_T_DEFINED
1021   typedef unsigned short mode_t;
1022 #endif
1023
1024 /*
1025 ** We may need to provide the "ino_t" type.
1026 */
1027
1028 #ifndef INO_T_DEFINED
1029   #define INO_T_DEFINED
1030   typedef unsigned short ino_t;
1031 #endif
1032
1033 /*
1034 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1035 */
1036
1037 #ifndef NAME_MAX
1038 #  ifdef FILENAME_MAX
1039 #    define NAME_MAX (FILENAME_MAX)
1040 #  else
1041 #    define NAME_MAX (260)
1042 #  endif
1043 #endif
1044
1045 /*
1046 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1047 */
1048
1049 #ifndef NULL_INTPTR_T
1050 #  define NULL_INTPTR_T ((intptr_t)(0))
1051 #endif
1052
1053 #ifndef BAD_INTPTR_T
1054 #  define BAD_INTPTR_T ((intptr_t)(-1))
1055 #endif
1056
1057 /*
1058 ** We need to provide the necessary structures and related types.
1059 */
1060
1061 #ifndef DIRENT_DEFINED
1062 #define DIRENT_DEFINED
1063 typedef struct DIRENT DIRENT;
1064 typedef DIRENT *LPDIRENT;
1065 struct DIRENT {
1066   ino_t d_ino;               /* Sequence number, do not use. */
1067   unsigned d_attributes;     /* Win32 file attributes. */
1068   char d_name[NAME_MAX + 1]; /* Name within the directory. */
1069 };
1070 #endif
1071
1072 #ifndef DIR_DEFINED
1073 #define DIR_DEFINED
1074 typedef struct DIR DIR;
1075 typedef DIR *LPDIR;
1076 struct DIR {
1077   intptr_t d_handle; /* Value returned by "_findfirst". */
1078   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1079   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1080 };
1081 #endif
1082
1083 /*
1084 ** Provide a macro, for use by the implementation, to determine if a
1085 ** particular directory entry should be skipped over when searching for
1086 ** the next directory entry that should be returned by the readdir() or
1087 ** readdir_r() functions.
1088 */
1089
1090 #ifndef is_filtered
1091 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1092 #endif
1093
1094 /*
1095 ** Provide the function prototype for the POSIX compatiable getenv()
1096 ** function.  This function is not thread-safe.
1097 */
1098
1099 extern const char *windirent_getenv(const char *name);
1100
1101 /*
1102 ** Finally, we can provide the function prototypes for the opendir(),
1103 ** readdir(), readdir_r(), and closedir() POSIX functions.
1104 */
1105
1106 extern LPDIR opendir(const char *dirname);
1107 extern LPDIRENT readdir(LPDIR dirp);
1108 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1109 extern INT closedir(LPDIR dirp);
1110
1111 #endif /* defined(WIN32) && defined(_MSC_VER) */
1112
1113 /************************* End test_windirent.h ********************/
1114 /************************* Begin test_windirent.c ******************/
1115 /*
1116 ** 2015 November 30
1117 **
1118 ** The author disclaims copyright to this source code.  In place of
1119 ** a legal notice, here is a blessing:
1120 **
1121 **    May you do good and not evil.
1122 **    May you find forgiveness for yourself and forgive others.
1123 **    May you share freely, never taking more than you give.
1124 **
1125 *************************************************************************
1126 ** This file contains code to implement most of the opendir() family of
1127 ** POSIX functions on Win32 using the MSVCRT.
1128 */
1129
1130 #if defined(_WIN32) && defined(_MSC_VER)
1131 /* #include "test_windirent.h" */
1132
1133 /*
1134 ** Implementation of the POSIX getenv() function using the Win32 API.
1135 ** This function is not thread-safe.
1136 */
1137 const char *windirent_getenv(
1138   const char *name
1139 ){
1140   static char value[32768]; /* Maximum length, per MSDN */
1141   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1142   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1143
1144   memset(value, 0, sizeof(value));
1145   dwRet = GetEnvironmentVariableA(name, value, dwSize);
1146   if( dwRet==0 || dwRet>dwSize ){
1147     /*
1148     ** The function call to GetEnvironmentVariableA() failed -OR-
1149     ** the buffer is not large enough.  Either way, return NULL.
1150     */
1151     return 0;
1152   }else{
1153     /*
1154     ** The function call to GetEnvironmentVariableA() succeeded
1155     ** -AND- the buffer contains the entire value.
1156     */
1157     return value;
1158   }
1159 }
1160
1161 /*
1162 ** Implementation of the POSIX opendir() function using the MSVCRT.
1163 */
1164 LPDIR opendir(
1165   const char *dirname
1166 ){
1167   struct _finddata_t data;
1168   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1169   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1170
1171   if( dirp==NULL ) return NULL;
1172   memset(dirp, 0, sizeof(DIR));
1173
1174   /* TODO: Remove this if Unix-style root paths are not used. */
1175   if( sqlite3_stricmp(dirname, "/")==0 ){
1176     dirname = windirent_getenv("SystemDrive");
1177   }
1178
1179   memset(&data, 0, sizeof(struct _finddata_t));
1180   _snprintf(data.name, namesize, "%s\\*", dirname);
1181   dirp->d_handle = _findfirst(data.name, &data);
1182
1183   if( dirp->d_handle==BAD_INTPTR_T ){
1184     closedir(dirp);
1185     return NULL;
1186   }
1187
1188   /* TODO: Remove this block to allow hidden and/or system files. */
1189   if( is_filtered(data) ){
1190 next:
1191
1192     memset(&data, 0, sizeof(struct _finddata_t));
1193     if( _findnext(dirp->d_handle, &data)==-1 ){
1194       closedir(dirp);
1195       return NULL;
1196     }
1197
1198     /* TODO: Remove this block to allow hidden and/or system files. */
1199     if( is_filtered(data) ) goto next;
1200   }
1201
1202   dirp->d_first.d_attributes = data.attrib;
1203   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1204   dirp->d_first.d_name[NAME_MAX] = '\0';
1205
1206   return dirp;
1207 }
1208
1209 /*
1210 ** Implementation of the POSIX readdir() function using the MSVCRT.
1211 */
1212 LPDIRENT readdir(
1213   LPDIR dirp
1214 ){
1215   struct _finddata_t data;
1216
1217   if( dirp==NULL ) return NULL;
1218
1219   if( dirp->d_first.d_ino==0 ){
1220     dirp->d_first.d_ino++;
1221     dirp->d_next.d_ino++;
1222
1223     return &dirp->d_first;
1224   }
1225
1226 next:
1227
1228   memset(&data, 0, sizeof(struct _finddata_t));
1229   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1230
1231   /* TODO: Remove this block to allow hidden and/or system files. */
1232   if( is_filtered(data) ) goto next;
1233
1234   dirp->d_next.d_ino++;
1235   dirp->d_next.d_attributes = data.attrib;
1236   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1237   dirp->d_next.d_name[NAME_MAX] = '\0';
1238
1239   return &dirp->d_next;
1240 }
1241
1242 /*
1243 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1244 */
1245 INT readdir_r(
1246   LPDIR dirp,
1247   LPDIRENT entry,
1248   LPDIRENT *result
1249 ){
1250   struct _finddata_t data;
1251
1252   if( dirp==NULL ) return EBADF;
1253
1254   if( dirp->d_first.d_ino==0 ){
1255     dirp->d_first.d_ino++;
1256     dirp->d_next.d_ino++;
1257
1258     entry->d_ino = dirp->d_first.d_ino;
1259     entry->d_attributes = dirp->d_first.d_attributes;
1260     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1261     entry->d_name[NAME_MAX] = '\0';
1262
1263     *result = entry;
1264     return 0;
1265   }
1266
1267 next:
1268
1269   memset(&data, 0, sizeof(struct _finddata_t));
1270   if( _findnext(dirp->d_handle, &data)==-1 ){
1271     *result = NULL;
1272     return ENOENT;
1273   }
1274
1275   /* TODO: Remove this block to allow hidden and/or system files. */
1276   if( is_filtered(data) ) goto next;
1277
1278   entry->d_ino = (ino_t)-1; /* not available */
1279   entry->d_attributes = data.attrib;
1280   strncpy(entry->d_name, data.name, NAME_MAX);
1281   entry->d_name[NAME_MAX] = '\0';
1282
1283   *result = entry;
1284   return 0;
1285 }
1286
1287 /*
1288 ** Implementation of the POSIX closedir() function using the MSVCRT.
1289 */
1290 INT closedir(
1291   LPDIR dirp
1292 ){
1293   INT result = 0;
1294
1295   if( dirp==NULL ) return EINVAL;
1296
1297   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1298     result = _findclose(dirp->d_handle);
1299   }
1300
1301   sqlite3_free(dirp);
1302   return result;
1303 }
1304
1305 #endif /* defined(WIN32) && defined(_MSC_VER) */
1306
1307 /************************* End test_windirent.c ********************/
1308 #define dirent DIRENT
1309 #endif
1310 /************************* Begin ../ext/misc/shathree.c ******************/
1311 /*
1312 ** 2017-03-08
1313 **
1314 ** The author disclaims copyright to this source code.  In place of
1315 ** a legal notice, here is a blessing:
1316 **
1317 **    May you do good and not evil.
1318 **    May you find forgiveness for yourself and forgive others.
1319 **    May you share freely, never taking more than you give.
1320 **
1321 ******************************************************************************
1322 **
1323 ** This SQLite extension implements a functions that compute SHA1 hashes.
1324 ** Two SQL functions are implemented:
1325 **
1326 **     sha3(X,SIZE)
1327 **     sha3_query(Y,SIZE)
1328 **
1329 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1330 ** X is NULL.
1331 **
1332 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1333 ** and returns a hash of their results.
1334 **
1335 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1336 ** is used.  If SIZE is included it must be one of the integers 224, 256,
1337 ** 384, or 512, to determine SHA3 hash variant that is computed.
1338 */
1339 SQLITE_EXTENSION_INIT1
1340 #include <assert.h>
1341 #include <string.h>
1342 #include <stdarg.h>
1343 /* typedef sqlite3_uint64 u64; */
1344
1345 /******************************************************************************
1346 ** The Hash Engine
1347 */
1348 /*
1349 ** Macros to determine whether the machine is big or little endian,
1350 ** and whether or not that determination is run-time or compile-time.
1351 **
1352 ** For best performance, an attempt is made to guess at the byte-order
1353 ** using C-preprocessor macros.  If that is unsuccessful, or if
1354 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1355 ** at run-time.
1356 */
1357 #ifndef SHA3_BYTEORDER
1358 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1359      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1360      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1361      defined(__arm__)
1362 #   define SHA3_BYTEORDER    1234
1363 # elif defined(sparc)    || defined(__ppc__)
1364 #   define SHA3_BYTEORDER    4321
1365 # else
1366 #   define SHA3_BYTEORDER 0
1367 # endif
1368 #endif
1369
1370
1371 /*
1372 ** State structure for a SHA3 hash in progress
1373 */
1374 typedef struct SHA3Context SHA3Context;
1375 struct SHA3Context {
1376   union {
1377     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1378     unsigned char x[1600];    /* ... or 1600 bytes */
1379   } u;
1380   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1381   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1382   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1383 };
1384
1385 /*
1386 ** A single step of the Keccak mixing function for a 1600-bit state
1387 */
1388 static void KeccakF1600Step(SHA3Context *p){
1389   int i;
1390   u64 b0, b1, b2, b3, b4;
1391   u64 c0, c1, c2, c3, c4;
1392   u64 d0, d1, d2, d3, d4;
1393   static const u64 RC[] = {
1394     0x0000000000000001ULL,  0x0000000000008082ULL,
1395     0x800000000000808aULL,  0x8000000080008000ULL,
1396     0x000000000000808bULL,  0x0000000080000001ULL,
1397     0x8000000080008081ULL,  0x8000000000008009ULL,
1398     0x000000000000008aULL,  0x0000000000000088ULL,
1399     0x0000000080008009ULL,  0x000000008000000aULL,
1400     0x000000008000808bULL,  0x800000000000008bULL,
1401     0x8000000000008089ULL,  0x8000000000008003ULL,
1402     0x8000000000008002ULL,  0x8000000000000080ULL,
1403     0x000000000000800aULL,  0x800000008000000aULL,
1404     0x8000000080008081ULL,  0x8000000000008080ULL,
1405     0x0000000080000001ULL,  0x8000000080008008ULL
1406   };
1407 # define a00 (p->u.s[0])
1408 # define a01 (p->u.s[1])
1409 # define a02 (p->u.s[2])
1410 # define a03 (p->u.s[3])
1411 # define a04 (p->u.s[4])
1412 # define a10 (p->u.s[5])
1413 # define a11 (p->u.s[6])
1414 # define a12 (p->u.s[7])
1415 # define a13 (p->u.s[8])
1416 # define a14 (p->u.s[9])
1417 # define a20 (p->u.s[10])
1418 # define a21 (p->u.s[11])
1419 # define a22 (p->u.s[12])
1420 # define a23 (p->u.s[13])
1421 # define a24 (p->u.s[14])
1422 # define a30 (p->u.s[15])
1423 # define a31 (p->u.s[16])
1424 # define a32 (p->u.s[17])
1425 # define a33 (p->u.s[18])
1426 # define a34 (p->u.s[19])
1427 # define a40 (p->u.s[20])
1428 # define a41 (p->u.s[21])
1429 # define a42 (p->u.s[22])
1430 # define a43 (p->u.s[23])
1431 # define a44 (p->u.s[24])
1432 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1433
1434   for(i=0; i<24; i+=4){
1435     c0 = a00^a10^a20^a30^a40;
1436     c1 = a01^a11^a21^a31^a41;
1437     c2 = a02^a12^a22^a32^a42;
1438     c3 = a03^a13^a23^a33^a43;
1439     c4 = a04^a14^a24^a34^a44;
1440     d0 = c4^ROL64(c1, 1);
1441     d1 = c0^ROL64(c2, 1);
1442     d2 = c1^ROL64(c3, 1);
1443     d3 = c2^ROL64(c4, 1);
1444     d4 = c3^ROL64(c0, 1);
1445
1446     b0 = (a00^d0);
1447     b1 = ROL64((a11^d1), 44);
1448     b2 = ROL64((a22^d2), 43);
1449     b3 = ROL64((a33^d3), 21);
1450     b4 = ROL64((a44^d4), 14);
1451     a00 =   b0 ^((~b1)&  b2 );
1452     a00 ^= RC[i];
1453     a11 =   b1 ^((~b2)&  b3 );
1454     a22 =   b2 ^((~b3)&  b4 );
1455     a33 =   b3 ^((~b4)&  b0 );
1456     a44 =   b4 ^((~b0)&  b1 );
1457
1458     b2 = ROL64((a20^d0), 3);
1459     b3 = ROL64((a31^d1), 45);
1460     b4 = ROL64((a42^d2), 61);
1461     b0 = ROL64((a03^d3), 28);
1462     b1 = ROL64((a14^d4), 20);
1463     a20 =   b0 ^((~b1)&  b2 );
1464     a31 =   b1 ^((~b2)&  b3 );
1465     a42 =   b2 ^((~b3)&  b4 );
1466     a03 =   b3 ^((~b4)&  b0 );
1467     a14 =   b4 ^((~b0)&  b1 );
1468
1469     b4 = ROL64((a40^d0), 18);
1470     b0 = ROL64((a01^d1), 1);
1471     b1 = ROL64((a12^d2), 6);
1472     b2 = ROL64((a23^d3), 25);
1473     b3 = ROL64((a34^d4), 8);
1474     a40 =   b0 ^((~b1)&  b2 );
1475     a01 =   b1 ^((~b2)&  b3 );
1476     a12 =   b2 ^((~b3)&  b4 );
1477     a23 =   b3 ^((~b4)&  b0 );
1478     a34 =   b4 ^((~b0)&  b1 );
1479
1480     b1 = ROL64((a10^d0), 36);
1481     b2 = ROL64((a21^d1), 10);
1482     b3 = ROL64((a32^d2), 15);
1483     b4 = ROL64((a43^d3), 56);
1484     b0 = ROL64((a04^d4), 27);
1485     a10 =   b0 ^((~b1)&  b2 );
1486     a21 =   b1 ^((~b2)&  b3 );
1487     a32 =   b2 ^((~b3)&  b4 );
1488     a43 =   b3 ^((~b4)&  b0 );
1489     a04 =   b4 ^((~b0)&  b1 );
1490
1491     b3 = ROL64((a30^d0), 41);
1492     b4 = ROL64((a41^d1), 2);
1493     b0 = ROL64((a02^d2), 62);
1494     b1 = ROL64((a13^d3), 55);
1495     b2 = ROL64((a24^d4), 39);
1496     a30 =   b0 ^((~b1)&  b2 );
1497     a41 =   b1 ^((~b2)&  b3 );
1498     a02 =   b2 ^((~b3)&  b4 );
1499     a13 =   b3 ^((~b4)&  b0 );
1500     a24 =   b4 ^((~b0)&  b1 );
1501
1502     c0 = a00^a20^a40^a10^a30;
1503     c1 = a11^a31^a01^a21^a41;
1504     c2 = a22^a42^a12^a32^a02;
1505     c3 = a33^a03^a23^a43^a13;
1506     c4 = a44^a14^a34^a04^a24;
1507     d0 = c4^ROL64(c1, 1);
1508     d1 = c0^ROL64(c2, 1);
1509     d2 = c1^ROL64(c3, 1);
1510     d3 = c2^ROL64(c4, 1);
1511     d4 = c3^ROL64(c0, 1);
1512
1513     b0 = (a00^d0);
1514     b1 = ROL64((a31^d1), 44);
1515     b2 = ROL64((a12^d2), 43);
1516     b3 = ROL64((a43^d3), 21);
1517     b4 = ROL64((a24^d4), 14);
1518     a00 =   b0 ^((~b1)&  b2 );
1519     a00 ^= RC[i+1];
1520     a31 =   b1 ^((~b2)&  b3 );
1521     a12 =   b2 ^((~b3)&  b4 );
1522     a43 =   b3 ^((~b4)&  b0 );
1523     a24 =   b4 ^((~b0)&  b1 );
1524
1525     b2 = ROL64((a40^d0), 3);
1526     b3 = ROL64((a21^d1), 45);
1527     b4 = ROL64((a02^d2), 61);
1528     b0 = ROL64((a33^d3), 28);
1529     b1 = ROL64((a14^d4), 20);
1530     a40 =   b0 ^((~b1)&  b2 );
1531     a21 =   b1 ^((~b2)&  b3 );
1532     a02 =   b2 ^((~b3)&  b4 );
1533     a33 =   b3 ^((~b4)&  b0 );
1534     a14 =   b4 ^((~b0)&  b1 );
1535
1536     b4 = ROL64((a30^d0), 18);
1537     b0 = ROL64((a11^d1), 1);
1538     b1 = ROL64((a42^d2), 6);
1539     b2 = ROL64((a23^d3), 25);
1540     b3 = ROL64((a04^d4), 8);
1541     a30 =   b0 ^((~b1)&  b2 );
1542     a11 =   b1 ^((~b2)&  b3 );
1543     a42 =   b2 ^((~b3)&  b4 );
1544     a23 =   b3 ^((~b4)&  b0 );
1545     a04 =   b4 ^((~b0)&  b1 );
1546
1547     b1 = ROL64((a20^d0), 36);
1548     b2 = ROL64((a01^d1), 10);
1549     b3 = ROL64((a32^d2), 15);
1550     b4 = ROL64((a13^d3), 56);
1551     b0 = ROL64((a44^d4), 27);
1552     a20 =   b0 ^((~b1)&  b2 );
1553     a01 =   b1 ^((~b2)&  b3 );
1554     a32 =   b2 ^((~b3)&  b4 );
1555     a13 =   b3 ^((~b4)&  b0 );
1556     a44 =   b4 ^((~b0)&  b1 );
1557
1558     b3 = ROL64((a10^d0), 41);
1559     b4 = ROL64((a41^d1), 2);
1560     b0 = ROL64((a22^d2), 62);
1561     b1 = ROL64((a03^d3), 55);
1562     b2 = ROL64((a34^d4), 39);
1563     a10 =   b0 ^((~b1)&  b2 );
1564     a41 =   b1 ^((~b2)&  b3 );
1565     a22 =   b2 ^((~b3)&  b4 );
1566     a03 =   b3 ^((~b4)&  b0 );
1567     a34 =   b4 ^((~b0)&  b1 );
1568
1569     c0 = a00^a40^a30^a20^a10;
1570     c1 = a31^a21^a11^a01^a41;
1571     c2 = a12^a02^a42^a32^a22;
1572     c3 = a43^a33^a23^a13^a03;
1573     c4 = a24^a14^a04^a44^a34;
1574     d0 = c4^ROL64(c1, 1);
1575     d1 = c0^ROL64(c2, 1);
1576     d2 = c1^ROL64(c3, 1);
1577     d3 = c2^ROL64(c4, 1);
1578     d4 = c3^ROL64(c0, 1);
1579
1580     b0 = (a00^d0);
1581     b1 = ROL64((a21^d1), 44);
1582     b2 = ROL64((a42^d2), 43);
1583     b3 = ROL64((a13^d3), 21);
1584     b4 = ROL64((a34^d4), 14);
1585     a00 =   b0 ^((~b1)&  b2 );
1586     a00 ^= RC[i+2];
1587     a21 =   b1 ^((~b2)&  b3 );
1588     a42 =   b2 ^((~b3)&  b4 );
1589     a13 =   b3 ^((~b4)&  b0 );
1590     a34 =   b4 ^((~b0)&  b1 );
1591
1592     b2 = ROL64((a30^d0), 3);
1593     b3 = ROL64((a01^d1), 45);
1594     b4 = ROL64((a22^d2), 61);
1595     b0 = ROL64((a43^d3), 28);
1596     b1 = ROL64((a14^d4), 20);
1597     a30 =   b0 ^((~b1)&  b2 );
1598     a01 =   b1 ^((~b2)&  b3 );
1599     a22 =   b2 ^((~b3)&  b4 );
1600     a43 =   b3 ^((~b4)&  b0 );
1601     a14 =   b4 ^((~b0)&  b1 );
1602
1603     b4 = ROL64((a10^d0), 18);
1604     b0 = ROL64((a31^d1), 1);
1605     b1 = ROL64((a02^d2), 6);
1606     b2 = ROL64((a23^d3), 25);
1607     b3 = ROL64((a44^d4), 8);
1608     a10 =   b0 ^((~b1)&  b2 );
1609     a31 =   b1 ^((~b2)&  b3 );
1610     a02 =   b2 ^((~b3)&  b4 );
1611     a23 =   b3 ^((~b4)&  b0 );
1612     a44 =   b4 ^((~b0)&  b1 );
1613
1614     b1 = ROL64((a40^d0), 36);
1615     b2 = ROL64((a11^d1), 10);
1616     b3 = ROL64((a32^d2), 15);
1617     b4 = ROL64((a03^d3), 56);
1618     b0 = ROL64((a24^d4), 27);
1619     a40 =   b0 ^((~b1)&  b2 );
1620     a11 =   b1 ^((~b2)&  b3 );
1621     a32 =   b2 ^((~b3)&  b4 );
1622     a03 =   b3 ^((~b4)&  b0 );
1623     a24 =   b4 ^((~b0)&  b1 );
1624
1625     b3 = ROL64((a20^d0), 41);
1626     b4 = ROL64((a41^d1), 2);
1627     b0 = ROL64((a12^d2), 62);
1628     b1 = ROL64((a33^d3), 55);
1629     b2 = ROL64((a04^d4), 39);
1630     a20 =   b0 ^((~b1)&  b2 );
1631     a41 =   b1 ^((~b2)&  b3 );
1632     a12 =   b2 ^((~b3)&  b4 );
1633     a33 =   b3 ^((~b4)&  b0 );
1634     a04 =   b4 ^((~b0)&  b1 );
1635
1636     c0 = a00^a30^a10^a40^a20;
1637     c1 = a21^a01^a31^a11^a41;
1638     c2 = a42^a22^a02^a32^a12;
1639     c3 = a13^a43^a23^a03^a33;
1640     c4 = a34^a14^a44^a24^a04;
1641     d0 = c4^ROL64(c1, 1);
1642     d1 = c0^ROL64(c2, 1);
1643     d2 = c1^ROL64(c3, 1);
1644     d3 = c2^ROL64(c4, 1);
1645     d4 = c3^ROL64(c0, 1);
1646
1647     b0 = (a00^d0);
1648     b1 = ROL64((a01^d1), 44);
1649     b2 = ROL64((a02^d2), 43);
1650     b3 = ROL64((a03^d3), 21);
1651     b4 = ROL64((a04^d4), 14);
1652     a00 =   b0 ^((~b1)&  b2 );
1653     a00 ^= RC[i+3];
1654     a01 =   b1 ^((~b2)&  b3 );
1655     a02 =   b2 ^((~b3)&  b4 );
1656     a03 =   b3 ^((~b4)&  b0 );
1657     a04 =   b4 ^((~b0)&  b1 );
1658
1659     b2 = ROL64((a10^d0), 3);
1660     b3 = ROL64((a11^d1), 45);
1661     b4 = ROL64((a12^d2), 61);
1662     b0 = ROL64((a13^d3), 28);
1663     b1 = ROL64((a14^d4), 20);
1664     a10 =   b0 ^((~b1)&  b2 );
1665     a11 =   b1 ^((~b2)&  b3 );
1666     a12 =   b2 ^((~b3)&  b4 );
1667     a13 =   b3 ^((~b4)&  b0 );
1668     a14 =   b4 ^((~b0)&  b1 );
1669
1670     b4 = ROL64((a20^d0), 18);
1671     b0 = ROL64((a21^d1), 1);
1672     b1 = ROL64((a22^d2), 6);
1673     b2 = ROL64((a23^d3), 25);
1674     b3 = ROL64((a24^d4), 8);
1675     a20 =   b0 ^((~b1)&  b2 );
1676     a21 =   b1 ^((~b2)&  b3 );
1677     a22 =   b2 ^((~b3)&  b4 );
1678     a23 =   b3 ^((~b4)&  b0 );
1679     a24 =   b4 ^((~b0)&  b1 );
1680
1681     b1 = ROL64((a30^d0), 36);
1682     b2 = ROL64((a31^d1), 10);
1683     b3 = ROL64((a32^d2), 15);
1684     b4 = ROL64((a33^d3), 56);
1685     b0 = ROL64((a34^d4), 27);
1686     a30 =   b0 ^((~b1)&  b2 );
1687     a31 =   b1 ^((~b2)&  b3 );
1688     a32 =   b2 ^((~b3)&  b4 );
1689     a33 =   b3 ^((~b4)&  b0 );
1690     a34 =   b4 ^((~b0)&  b1 );
1691
1692     b3 = ROL64((a40^d0), 41);
1693     b4 = ROL64((a41^d1), 2);
1694     b0 = ROL64((a42^d2), 62);
1695     b1 = ROL64((a43^d3), 55);
1696     b2 = ROL64((a44^d4), 39);
1697     a40 =   b0 ^((~b1)&  b2 );
1698     a41 =   b1 ^((~b2)&  b3 );
1699     a42 =   b2 ^((~b3)&  b4 );
1700     a43 =   b3 ^((~b4)&  b0 );
1701     a44 =   b4 ^((~b0)&  b1 );
1702   }
1703 }
1704
1705 /*
1706 ** Initialize a new hash.  iSize determines the size of the hash
1707 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1708 ** can be zero to use the default hash size of 256 bits.
1709 */
1710 static void SHA3Init(SHA3Context *p, int iSize){
1711   memset(p, 0, sizeof(*p));
1712   if( iSize>=128 && iSize<=512 ){
1713     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1714   }else{
1715     p->nRate = (1600 - 2*256)/8;
1716   }
1717 #if SHA3_BYTEORDER==1234
1718   /* Known to be little-endian at compile-time. No-op */
1719 #elif SHA3_BYTEORDER==4321
1720   p->ixMask = 7;  /* Big-endian */
1721 #else
1722   {
1723     static unsigned int one = 1;
1724     if( 1==*(unsigned char*)&one ){
1725       /* Little endian.  No byte swapping. */
1726       p->ixMask = 0;
1727     }else{
1728       /* Big endian.  Byte swap. */
1729       p->ixMask = 7;
1730     }
1731   }
1732 #endif
1733 }
1734
1735 /*
1736 ** Make consecutive calls to the SHA3Update function to add new content
1737 ** to the hash
1738 */
1739 static void SHA3Update(
1740   SHA3Context *p,
1741   const unsigned char *aData,
1742   unsigned int nData
1743 ){
1744   unsigned int i = 0;
1745 #if SHA3_BYTEORDER==1234
1746   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1747     for(; i+7<nData; i+=8){
1748       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1749       p->nLoaded += 8;
1750       if( p->nLoaded>=p->nRate ){
1751         KeccakF1600Step(p);
1752         p->nLoaded = 0;
1753       }
1754     }
1755   }
1756 #endif
1757   for(; i<nData; i++){
1758 #if SHA3_BYTEORDER==1234
1759     p->u.x[p->nLoaded] ^= aData[i];
1760 #elif SHA3_BYTEORDER==4321
1761     p->u.x[p->nLoaded^0x07] ^= aData[i];
1762 #else
1763     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1764 #endif
1765     p->nLoaded++;
1766     if( p->nLoaded==p->nRate ){
1767       KeccakF1600Step(p);
1768       p->nLoaded = 0;
1769     }
1770   }
1771 }
1772
1773 /*
1774 ** After all content has been added, invoke SHA3Final() to compute
1775 ** the final hash.  The function returns a pointer to the binary
1776 ** hash value.
1777 */
1778 static unsigned char *SHA3Final(SHA3Context *p){
1779   unsigned int i;
1780   if( p->nLoaded==p->nRate-1 ){
1781     const unsigned char c1 = 0x86;
1782     SHA3Update(p, &c1, 1);
1783   }else{
1784     const unsigned char c2 = 0x06;
1785     const unsigned char c3 = 0x80;
1786     SHA3Update(p, &c2, 1);
1787     p->nLoaded = p->nRate - 1;
1788     SHA3Update(p, &c3, 1);
1789   }
1790   for(i=0; i<p->nRate; i++){
1791     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1792   }
1793   return &p->u.x[p->nRate];
1794 }
1795 /* End of the hashing logic
1796 *****************************************************************************/
1797
1798 /*
1799 ** Implementation of the sha3(X,SIZE) function.
1800 **
1801 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1802 ** size is 256.  If X is a BLOB, it is hashed as is.  
1803 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1804 ** and the string is hashed without the trailing 0x00 terminator.  The hash
1805 ** of a NULL value is NULL.
1806 */
1807 static void sha3Func(
1808   sqlite3_context *context,
1809   int argc,
1810   sqlite3_value **argv
1811 ){
1812   SHA3Context cx;
1813   int eType = sqlite3_value_type(argv[0]);
1814   int nByte = sqlite3_value_bytes(argv[0]);
1815   int iSize;
1816   if( argc==1 ){
1817     iSize = 256;
1818   }else{
1819     iSize = sqlite3_value_int(argv[1]);
1820     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1821       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1822                                     "384 512", -1);
1823       return;
1824     }
1825   }
1826   if( eType==SQLITE_NULL ) return;
1827   SHA3Init(&cx, iSize);
1828   if( eType==SQLITE_BLOB ){
1829     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1830   }else{
1831     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1832   }
1833   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1834 }
1835
1836 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1837 ** of 50 bytes and add it to the hash.
1838 */
1839 static void hash_step_vformat(
1840   SHA3Context *p,                 /* Add content to this context */
1841   const char *zFormat,
1842   ...
1843 ){
1844   va_list ap;
1845   int n;
1846   char zBuf[50];
1847   va_start(ap, zFormat);
1848   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1849   va_end(ap);
1850   n = (int)strlen(zBuf);
1851   SHA3Update(p, (unsigned char*)zBuf, n);
1852 }
1853
1854 /*
1855 ** Implementation of the sha3_query(SQL,SIZE) function.
1856 **
1857 ** This function compiles and runs the SQL statement(s) given in the
1858 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
1859 ** size is 256.
1860 **
1861 ** The format of the byte stream that is hashed is summarized as follows:
1862 **
1863 **       S<n>:<sql>
1864 **       R
1865 **       N
1866 **       I<int>
1867 **       F<ieee-float>
1868 **       B<size>:<bytes>
1869 **       T<size>:<text>
1870 **
1871 ** <sql> is the original SQL text for each statement run and <n> is
1872 ** the size of that text.  The SQL text is UTF-8.  A single R character
1873 ** occurs before the start of each row.  N means a NULL value.
1874 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
1875 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1876 ** B means blobs of <size> bytes.  T means text rendered as <size>
1877 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1878 ** text integers.
1879 **
1880 ** For each SQL statement in the X input, there is one S segment.  Each
1881 ** S segment is followed by zero or more R segments, one for each row in the
1882 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
1883 ** one for each column in the result set.  Segments are concatentated directly
1884 ** with no delimiters of any kind.
1885 */
1886 static void sha3QueryFunc(
1887   sqlite3_context *context,
1888   int argc,
1889   sqlite3_value **argv
1890 ){
1891   sqlite3 *db = sqlite3_context_db_handle(context);
1892   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1893   sqlite3_stmt *pStmt = 0;
1894   int nCol;                   /* Number of columns in the result set */
1895   int i;                      /* Loop counter */
1896   int rc;
1897   int n;
1898   const char *z;
1899   SHA3Context cx;
1900   int iSize;
1901
1902   if( argc==1 ){
1903     iSize = 256;
1904   }else{
1905     iSize = sqlite3_value_int(argv[1]);
1906     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1907       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1908                                     "384 512", -1);
1909       return;
1910     }
1911   }
1912   if( zSql==0 ) return;
1913   SHA3Init(&cx, iSize);
1914   while( zSql[0] ){
1915     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1916     if( rc ){
1917       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1918                                    zSql, sqlite3_errmsg(db));
1919       sqlite3_finalize(pStmt);
1920       sqlite3_result_error(context, zMsg, -1);
1921       sqlite3_free(zMsg);
1922       return;
1923     }
1924     if( !sqlite3_stmt_readonly(pStmt) ){
1925       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1926       sqlite3_finalize(pStmt);
1927       sqlite3_result_error(context, zMsg, -1);
1928       sqlite3_free(zMsg);
1929       return;
1930     }
1931     nCol = sqlite3_column_count(pStmt);
1932     z = sqlite3_sql(pStmt);
1933     n = (int)strlen(z);
1934     hash_step_vformat(&cx,"S%d:",n);
1935     SHA3Update(&cx,(unsigned char*)z,n);
1936
1937     /* Compute a hash over the result of the query */
1938     while( SQLITE_ROW==sqlite3_step(pStmt) ){
1939       SHA3Update(&cx,(const unsigned char*)"R",1);
1940       for(i=0; i<nCol; i++){
1941         switch( sqlite3_column_type(pStmt,i) ){
1942           case SQLITE_NULL: {
1943             SHA3Update(&cx, (const unsigned char*)"N",1);
1944             break;
1945           }
1946           case SQLITE_INTEGER: {
1947             sqlite3_uint64 u;
1948             int j;
1949             unsigned char x[9];
1950             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1951             memcpy(&u, &v, 8);
1952             for(j=8; j>=1; j--){
1953               x[j] = u & 0xff;
1954               u >>= 8;
1955             }
1956             x[0] = 'I';
1957             SHA3Update(&cx, x, 9);
1958             break;
1959           }
1960           case SQLITE_FLOAT: {
1961             sqlite3_uint64 u;
1962             int j;
1963             unsigned char x[9];
1964             double r = sqlite3_column_double(pStmt,i);
1965             memcpy(&u, &r, 8);
1966             for(j=8; j>=1; j--){
1967               x[j] = u & 0xff;
1968               u >>= 8;
1969             }
1970             x[0] = 'F';
1971             SHA3Update(&cx,x,9);
1972             break;
1973           }
1974           case SQLITE_TEXT: {
1975             int n2 = sqlite3_column_bytes(pStmt, i);
1976             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1977             hash_step_vformat(&cx,"T%d:",n2);
1978             SHA3Update(&cx, z2, n2);
1979             break;
1980           }
1981           case SQLITE_BLOB: {
1982             int n2 = sqlite3_column_bytes(pStmt, i);
1983             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1984             hash_step_vformat(&cx,"B%d:",n2);
1985             SHA3Update(&cx, z2, n2);
1986             break;
1987           }
1988         }
1989       }
1990     }
1991     sqlite3_finalize(pStmt);
1992   }
1993   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1994 }
1995
1996
1997 #ifdef _WIN32
1998
1999 #endif
2000 int sqlite3_shathree_init(
2001   sqlite3 *db,
2002   char **pzErrMsg,
2003   const sqlite3_api_routines *pApi
2004 ){
2005   int rc = SQLITE_OK;
2006   SQLITE_EXTENSION_INIT2(pApi);
2007   (void)pzErrMsg;  /* Unused parameter */
2008   rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2009                                sha3Func, 0, 0);
2010   if( rc==SQLITE_OK ){
2011     rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2012                                  sha3Func, 0, 0);
2013   }
2014   if( rc==SQLITE_OK ){
2015     rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2016                                  sha3QueryFunc, 0, 0);
2017   }
2018   if( rc==SQLITE_OK ){
2019     rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2020                                  sha3QueryFunc, 0, 0);
2021   }
2022   return rc;
2023 }
2024
2025 /************************* End ../ext/misc/shathree.c ********************/
2026 /************************* Begin ../ext/misc/fileio.c ******************/
2027 /*
2028 ** 2014-06-13
2029 **
2030 ** The author disclaims copyright to this source code.  In place of
2031 ** a legal notice, here is a blessing:
2032 **
2033 **    May you do good and not evil.
2034 **    May you find forgiveness for yourself and forgive others.
2035 **    May you share freely, never taking more than you give.
2036 **
2037 ******************************************************************************
2038 **
2039 ** This SQLite extension implements SQL functions readfile() and
2040 ** writefile(), and eponymous virtual type "fsdir".
2041 **
2042 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2043 **
2044 **   If neither of the optional arguments is present, then this UDF
2045 **   function writes blob DATA to file FILE. If successful, the number
2046 **   of bytes written is returned. If an error occurs, NULL is returned.
2047 **
2048 **   If the first option argument - MODE - is present, then it must
2049 **   be passed an integer value that corresponds to a POSIX mode
2050 **   value (file type + permissions, as returned in the stat.st_mode
2051 **   field by the stat() system call). Three types of files may
2052 **   be written/created:
2053 **
2054 **     regular files:  (mode & 0170000)==0100000
2055 **     symbolic links: (mode & 0170000)==0120000
2056 **     directories:    (mode & 0170000)==0040000
2057 **
2058 **   For a directory, the DATA is ignored. For a symbolic link, it is
2059 **   interpreted as text and used as the target of the link. For a
2060 **   regular file, it is interpreted as a blob and written into the
2061 **   named file. Regardless of the type of file, its permissions are
2062 **   set to (mode & 0777) before returning.
2063 **
2064 **   If the optional MTIME argument is present, then it is interpreted
2065 **   as an integer - the number of seconds since the unix epoch. The
2066 **   modification-time of the target file is set to this value before
2067 **   returning.
2068 **
2069 **   If three or more arguments are passed to this function and an
2070 **   error is encountered, an exception is raised.
2071 **
2072 ** READFILE(FILE):
2073 **
2074 **   Read and return the contents of file FILE (type blob) from disk.
2075 **
2076 ** FSDIR:
2077 **
2078 **   Used as follows:
2079 **
2080 **     SELECT * FROM fsdir($path [, $dir]);
2081 **
2082 **   Parameter $path is an absolute or relative pathname. If the file that it
2083 **   refers to does not exist, it is an error. If the path refers to a regular
2084 **   file or symbolic link, it returns a single row. Or, if the path refers
2085 **   to a directory, it returns one row for the directory, and one row for each
2086 **   file within the hierarchy rooted at $path.
2087 **
2088 **   Each row has the following columns:
2089 **
2090 **     name:  Path to file or directory (text value).
2091 **     mode:  Value of stat.st_mode for directory entry (an integer).
2092 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2093 **     data:  For a regular file, a blob containing the file data. For a
2094 **            symlink, a text value containing the text of the link. For a
2095 **            directory, NULL.
2096 **
2097 **   If a non-NULL value is specified for the optional $dir parameter and
2098 **   $path is a relative path, then $path is interpreted relative to $dir. 
2099 **   And the paths returned in the "name" column of the table are also 
2100 **   relative to directory $dir.
2101 */
2102 SQLITE_EXTENSION_INIT1
2103 #include <stdio.h>
2104 #include <string.h>
2105 #include <assert.h>
2106
2107 #include <sys/types.h>
2108 #include <sys/stat.h>
2109 #include <fcntl.h>
2110 #if !defined(_WIN32) && !defined(WIN32)
2111 #  include <unistd.h>
2112 #  include <dirent.h>
2113 #  include <utime.h>
2114 #  include <sys/time.h>
2115 #else
2116 #  include "windows.h"
2117 #  include <io.h>
2118 #  include <direct.h>
2119 /* #  include "test_windirent.h" */
2120 #  define dirent DIRENT
2121 #  ifndef chmod
2122 #    define chmod _chmod
2123 #  endif
2124 #  ifndef stat
2125 #    define stat _stat
2126 #  endif
2127 #  define mkdir(path,mode) _mkdir(path)
2128 #  define lstat(path,buf) stat(path,buf)
2129 #endif
2130 #include <time.h>
2131 #include <errno.h>
2132
2133
2134 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2135
2136 /*
2137 ** Set the result stored by context ctx to a blob containing the 
2138 ** contents of file zName.
2139 */
2140 static void readFileContents(sqlite3_context *ctx, const char *zName){
2141   FILE *in;
2142   long nIn;
2143   void *pBuf;
2144
2145   in = fopen(zName, "rb");
2146   if( in==0 ) return;
2147   fseek(in, 0, SEEK_END);
2148   nIn = ftell(in);
2149   rewind(in);
2150   pBuf = sqlite3_malloc( nIn );
2151   if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
2152     sqlite3_result_blob(ctx, pBuf, nIn, sqlite3_free);
2153   }else{
2154     sqlite3_free(pBuf);
2155   }
2156   fclose(in);
2157 }
2158
2159 /*
2160 ** Implementation of the "readfile(X)" SQL function.  The entire content
2161 ** of the file named X is read and returned as a BLOB.  NULL is returned
2162 ** if the file does not exist or is unreadable.
2163 */
2164 static void readfileFunc(
2165   sqlite3_context *context,
2166   int argc,
2167   sqlite3_value **argv
2168 ){
2169   const char *zName;
2170   (void)(argc);  /* Unused parameter */
2171   zName = (const char*)sqlite3_value_text(argv[0]);
2172   if( zName==0 ) return;
2173   readFileContents(context, zName);
2174 }
2175
2176 /*
2177 ** Set the error message contained in context ctx to the results of
2178 ** vprintf(zFmt, ...).
2179 */
2180 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2181   char *zMsg = 0;
2182   va_list ap;
2183   va_start(ap, zFmt);
2184   zMsg = sqlite3_vmprintf(zFmt, ap);
2185   sqlite3_result_error(ctx, zMsg, -1);
2186   sqlite3_free(zMsg);
2187   va_end(ap);
2188 }
2189
2190 #if defined(_WIN32)
2191 /*
2192 ** This function is designed to convert a Win32 FILETIME structure into the
2193 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2194 */
2195 static sqlite3_uint64 fileTimeToUnixTime(
2196   LPFILETIME pFileTime
2197 ){
2198   SYSTEMTIME epochSystemTime;
2199   ULARGE_INTEGER epochIntervals;
2200   FILETIME epochFileTime;
2201   ULARGE_INTEGER fileIntervals;
2202
2203   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2204   epochSystemTime.wYear = 1970;
2205   epochSystemTime.wMonth = 1;
2206   epochSystemTime.wDay = 1;
2207   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2208   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2209   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2210
2211   fileIntervals.LowPart = pFileTime->dwLowDateTime;
2212   fileIntervals.HighPart = pFileTime->dwHighDateTime;
2213
2214   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2215 }
2216
2217 /*
2218 ** This function attempts to normalize the time values found in the stat()
2219 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2220 ** appears to return these values as local times.
2221 */
2222 static void statTimesToUtc(
2223   const char *zPath,
2224   struct stat *pStatBuf
2225 ){
2226   HANDLE hFindFile;
2227   WIN32_FIND_DATAW fd;
2228   LPWSTR zUnicodeName;
2229   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2230   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2231   if( zUnicodeName ){
2232     memset(&fd, 0, sizeof(WIN32_FIND_DATA));
2233     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2234     if( hFindFile!=NULL ){
2235       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2236       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2237       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2238       FindClose(hFindFile);
2239     }
2240     sqlite3_free(zUnicodeName);
2241   }
2242 }
2243 #endif
2244
2245 /*
2246 ** This function is used in place of stat().  On Windows, special handling
2247 ** is required in order for the included time to be returned as UTC.  On all
2248 ** other systems, this function simply calls stat().
2249 */
2250 static int fileStat(
2251   const char *zPath,
2252   struct stat *pStatBuf
2253 ){
2254 #if defined(_WIN32)
2255   int rc = stat(zPath, pStatBuf);
2256   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2257   return rc;
2258 #else
2259   return stat(zPath, pStatBuf);
2260 #endif
2261 }
2262
2263 /*
2264 ** This function is used in place of lstat().  On Windows, special handling
2265 ** is required in order for the included time to be returned as UTC.  On all
2266 ** other systems, this function simply calls lstat().
2267 */
2268 static int fileLinkStat(
2269   const char *zPath,
2270   struct stat *pStatBuf
2271 ){
2272 #if defined(_WIN32)
2273   int rc = lstat(zPath, pStatBuf);
2274   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2275   return rc;
2276 #else
2277   return lstat(zPath, pStatBuf);
2278 #endif
2279 }
2280
2281 /*
2282 ** Argument zFile is the name of a file that will be created and/or written
2283 ** by SQL function writefile(). This function ensures that the directory
2284 ** zFile will be written to exists, creating it if required. The permissions
2285 ** for any path components created by this function are set to (mode&0777).
2286 **
2287 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2288 ** SQLITE_OK is returned if the directory is successfully created, or
2289 ** SQLITE_ERROR otherwise.
2290 */
2291 static int makeDirectory(
2292   const char *zFile,
2293   mode_t mode
2294 ){
2295   char *zCopy = sqlite3_mprintf("%s", zFile);
2296   int rc = SQLITE_OK;
2297
2298   if( zCopy==0 ){
2299     rc = SQLITE_NOMEM;
2300   }else{
2301     int nCopy = (int)strlen(zCopy);
2302     int i = 1;
2303
2304     while( rc==SQLITE_OK ){
2305       struct stat sStat;
2306       int rc2;
2307
2308       for(; zCopy[i]!='/' && i<nCopy; i++);
2309       if( i==nCopy ) break;
2310       zCopy[i] = '\0';
2311
2312       rc2 = fileStat(zCopy, &sStat);
2313       if( rc2!=0 ){
2314         if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
2315       }else{
2316         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2317       }
2318       zCopy[i] = '/';
2319       i++;
2320     }
2321
2322     sqlite3_free(zCopy);
2323   }
2324
2325   return rc;
2326 }
2327
2328 /*
2329 ** This function does the work for the writefile() UDF. Refer to 
2330 ** header comments at the top of this file for details.
2331 */
2332 static int writeFile(
2333   sqlite3_context *pCtx,          /* Context to return bytes written in */
2334   const char *zFile,              /* File to write */
2335   sqlite3_value *pData,           /* Data to write */
2336   mode_t mode,                    /* MODE parameter passed to writefile() */
2337   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2338 ){
2339 #if !defined(_WIN32) && !defined(WIN32)
2340   if( S_ISLNK(mode) ){
2341     const char *zTo = (const char*)sqlite3_value_text(pData);
2342     if( symlink(zTo, zFile)<0 ) return 1;
2343   }else
2344 #endif
2345   {
2346     if( S_ISDIR(mode) ){
2347       if( mkdir(zFile, mode) ){
2348         /* The mkdir() call to create the directory failed. This might not
2349         ** be an error though - if there is already a directory at the same
2350         ** path and either the permissions already match or can be changed
2351         ** to do so using chmod(), it is not an error.  */
2352         struct stat sStat;
2353         if( errno!=EEXIST
2354          || 0!=fileStat(zFile, &sStat)
2355          || !S_ISDIR(sStat.st_mode)
2356          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2357         ){
2358           return 1;
2359         }
2360       }
2361     }else{
2362       sqlite3_int64 nWrite = 0;
2363       const char *z;
2364       int rc = 0;
2365       FILE *out = fopen(zFile, "wb");
2366       if( out==0 ) return 1;
2367       z = (const char*)sqlite3_value_blob(pData);
2368       if( z ){
2369         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2370         nWrite = sqlite3_value_bytes(pData);
2371         if( nWrite!=n ){
2372           rc = 1;
2373         }
2374       }
2375       fclose(out);
2376       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2377         rc = 1;
2378       }
2379       if( rc ) return 2;
2380       sqlite3_result_int64(pCtx, nWrite);
2381     }
2382   }
2383
2384   if( mtime>=0 ){
2385 #if defined(_WIN32)
2386     /* Windows */
2387     FILETIME lastAccess;
2388     FILETIME lastWrite;
2389     SYSTEMTIME currentTime;
2390     LONGLONG intervals;
2391     HANDLE hFile;
2392     LPWSTR zUnicodeName;
2393     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2394
2395     GetSystemTime(&currentTime);
2396     SystemTimeToFileTime(&currentTime, &lastAccess);
2397     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2398     lastWrite.dwLowDateTime = (DWORD)intervals;
2399     lastWrite.dwHighDateTime = intervals >> 32;
2400     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2401     if( zUnicodeName==0 ){
2402       return 1;
2403     }
2404     hFile = CreateFileW(
2405       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2406       FILE_FLAG_BACKUP_SEMANTICS, NULL
2407     );
2408     sqlite3_free(zUnicodeName);
2409     if( hFile!=INVALID_HANDLE_VALUE ){
2410       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2411       CloseHandle(hFile);
2412       return !bResult;
2413     }else{
2414       return 1;
2415     }
2416 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2417     /* Recent unix */
2418     struct timespec times[2];
2419     times[0].tv_nsec = times[1].tv_nsec = 0;
2420     times[0].tv_sec = time(0);
2421     times[1].tv_sec = mtime;
2422     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2423       return 1;
2424     }
2425 #else
2426     /* Legacy unix */
2427     struct timeval times[2];
2428     times[0].tv_usec = times[1].tv_usec = 0;
2429     times[0].tv_sec = time(0);
2430     times[1].tv_sec = mtime;
2431     if( utimes(zFile, times) ){
2432       return 1;
2433     }
2434 #endif
2435   }
2436
2437   return 0;
2438 }
2439
2440 /*
2441 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.  
2442 ** Refer to header comments at the top of this file for details.
2443 */
2444 static void writefileFunc(
2445   sqlite3_context *context,
2446   int argc,
2447   sqlite3_value **argv
2448 ){
2449   const char *zFile;
2450   mode_t mode = 0;
2451   int res;
2452   sqlite3_int64 mtime = -1;
2453
2454   if( argc<2 || argc>4 ){
2455     sqlite3_result_error(context, 
2456         "wrong number of arguments to function writefile()", -1
2457     );
2458     return;
2459   }
2460
2461   zFile = (const char*)sqlite3_value_text(argv[0]);
2462   if( zFile==0 ) return;
2463   if( argc>=3 ){
2464     mode = (mode_t)sqlite3_value_int(argv[2]);
2465   }
2466   if( argc==4 ){
2467     mtime = sqlite3_value_int64(argv[3]);
2468   }
2469
2470   res = writeFile(context, zFile, argv[1], mode, mtime);
2471   if( res==1 && errno==ENOENT ){
2472     if( makeDirectory(zFile, mode)==SQLITE_OK ){
2473       res = writeFile(context, zFile, argv[1], mode, mtime);
2474     }
2475   }
2476
2477   if( argc>2 && res!=0 ){
2478     if( S_ISLNK(mode) ){
2479       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2480     }else if( S_ISDIR(mode) ){
2481       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2482     }else{
2483       ctxErrorMsg(context, "failed to write file: %s", zFile);
2484     }
2485   }
2486 }
2487
2488 /*
2489 ** SQL function:   lsmode(MODE)
2490 **
2491 ** Given a numberic st_mode from stat(), convert it into a human-readable
2492 ** text string in the style of "ls -l".
2493 */
2494 static void lsModeFunc(
2495   sqlite3_context *context,
2496   int argc,
2497   sqlite3_value **argv
2498 ){
2499   int i;
2500   int iMode = sqlite3_value_int(argv[0]);
2501   char z[16];
2502   (void)argc;
2503   if( S_ISLNK(iMode) ){
2504     z[0] = 'l';
2505   }else if( S_ISREG(iMode) ){
2506     z[0] = '-';
2507   }else if( S_ISDIR(iMode) ){
2508     z[0] = 'd';
2509   }else{
2510     z[0] = '?';
2511   }
2512   for(i=0; i<3; i++){
2513     int m = (iMode >> ((2-i)*3));
2514     char *a = &z[1 + i*3];
2515     a[0] = (m & 0x4) ? 'r' : '-';
2516     a[1] = (m & 0x2) ? 'w' : '-';
2517     a[2] = (m & 0x1) ? 'x' : '-';
2518   }
2519   z[10] = '\0';
2520   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2521 }
2522
2523 #ifndef SQLITE_OMIT_VIRTUALTABLE
2524
2525 /* 
2526 ** Cursor type for recursively iterating through a directory structure.
2527 */
2528 typedef struct fsdir_cursor fsdir_cursor;
2529 typedef struct FsdirLevel FsdirLevel;
2530
2531 struct FsdirLevel {
2532   DIR *pDir;                 /* From opendir() */
2533   char *zDir;                /* Name of directory (nul-terminated) */
2534 };
2535
2536 struct fsdir_cursor {
2537   sqlite3_vtab_cursor base;  /* Base class - must be first */
2538
2539   int nLvl;                  /* Number of entries in aLvl[] array */
2540   int iLvl;                  /* Index of current entry */
2541   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2542
2543   const char *zBase;
2544   int nBase;
2545
2546   struct stat sStat;         /* Current lstat() results */
2547   char *zPath;               /* Path to current entry */
2548   sqlite3_int64 iRowid;      /* Current rowid */
2549 };
2550
2551 typedef struct fsdir_tab fsdir_tab;
2552 struct fsdir_tab {
2553   sqlite3_vtab base;         /* Base class - must be first */
2554 };
2555
2556 /*
2557 ** Construct a new fsdir virtual table object.
2558 */
2559 static int fsdirConnect(
2560   sqlite3 *db,
2561   void *pAux,
2562   int argc, const char *const*argv,
2563   sqlite3_vtab **ppVtab,
2564   char **pzErr
2565 ){
2566   fsdir_tab *pNew = 0;
2567   int rc;
2568   (void)pAux;
2569   (void)argc;
2570   (void)argv;
2571   (void)pzErr;
2572   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2573   if( rc==SQLITE_OK ){
2574     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2575     if( pNew==0 ) return SQLITE_NOMEM;
2576     memset(pNew, 0, sizeof(*pNew));
2577   }
2578   *ppVtab = (sqlite3_vtab*)pNew;
2579   return rc;
2580 }
2581
2582 /*
2583 ** This method is the destructor for fsdir vtab objects.
2584 */
2585 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2586   sqlite3_free(pVtab);
2587   return SQLITE_OK;
2588 }
2589
2590 /*
2591 ** Constructor for a new fsdir_cursor object.
2592 */
2593 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2594   fsdir_cursor *pCur;
2595   (void)p;
2596   pCur = sqlite3_malloc( sizeof(*pCur) );
2597   if( pCur==0 ) return SQLITE_NOMEM;
2598   memset(pCur, 0, sizeof(*pCur));
2599   pCur->iLvl = -1;
2600   *ppCursor = &pCur->base;
2601   return SQLITE_OK;
2602 }
2603
2604 /*
2605 ** Reset a cursor back to the state it was in when first returned
2606 ** by fsdirOpen().
2607 */
2608 static void fsdirResetCursor(fsdir_cursor *pCur){
2609   int i;
2610   for(i=0; i<=pCur->iLvl; i++){
2611     FsdirLevel *pLvl = &pCur->aLvl[i];
2612     if( pLvl->pDir ) closedir(pLvl->pDir);
2613     sqlite3_free(pLvl->zDir);
2614   }
2615   sqlite3_free(pCur->zPath);
2616   sqlite3_free(pCur->aLvl);
2617   pCur->aLvl = 0;
2618   pCur->zPath = 0;
2619   pCur->zBase = 0;
2620   pCur->nBase = 0;
2621   pCur->nLvl = 0;
2622   pCur->iLvl = -1;
2623   pCur->iRowid = 1;
2624 }
2625
2626 /*
2627 ** Destructor for an fsdir_cursor.
2628 */
2629 static int fsdirClose(sqlite3_vtab_cursor *cur){
2630   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2631
2632   fsdirResetCursor(pCur);
2633   sqlite3_free(pCur);
2634   return SQLITE_OK;
2635 }
2636
2637 /*
2638 ** Set the error message for the virtual table associated with cursor
2639 ** pCur to the results of vprintf(zFmt, ...).
2640 */
2641 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2642   va_list ap;
2643   va_start(ap, zFmt);
2644   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2645   va_end(ap);
2646 }
2647
2648
2649 /*
2650 ** Advance an fsdir_cursor to its next row of output.
2651 */
2652 static int fsdirNext(sqlite3_vtab_cursor *cur){
2653   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2654   mode_t m = pCur->sStat.st_mode;
2655
2656   pCur->iRowid++;
2657   if( S_ISDIR(m) ){
2658     /* Descend into this directory */
2659     int iNew = pCur->iLvl + 1;
2660     FsdirLevel *pLvl;
2661     if( iNew>=pCur->nLvl ){
2662       int nNew = iNew+1;
2663       int nByte = nNew*sizeof(FsdirLevel);
2664       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc(pCur->aLvl, nByte);
2665       if( aNew==0 ) return SQLITE_NOMEM;
2666       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2667       pCur->aLvl = aNew;
2668       pCur->nLvl = nNew;
2669     }
2670     pCur->iLvl = iNew;
2671     pLvl = &pCur->aLvl[iNew];
2672     
2673     pLvl->zDir = pCur->zPath;
2674     pCur->zPath = 0;
2675     pLvl->pDir = opendir(pLvl->zDir);
2676     if( pLvl->pDir==0 ){
2677       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2678       return SQLITE_ERROR;
2679     }
2680   }
2681
2682   while( pCur->iLvl>=0 ){
2683     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2684     struct dirent *pEntry = readdir(pLvl->pDir);
2685     if( pEntry ){
2686       if( pEntry->d_name[0]=='.' ){
2687        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2688        if( pEntry->d_name[1]=='\0' ) continue;
2689       }
2690       sqlite3_free(pCur->zPath);
2691       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2692       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2693       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2694         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2695         return SQLITE_ERROR;
2696       }
2697       return SQLITE_OK;
2698     }
2699     closedir(pLvl->pDir);
2700     sqlite3_free(pLvl->zDir);
2701     pLvl->pDir = 0;
2702     pLvl->zDir = 0;
2703     pCur->iLvl--;
2704   }
2705
2706   /* EOF */
2707   sqlite3_free(pCur->zPath);
2708   pCur->zPath = 0;
2709   return SQLITE_OK;
2710 }
2711
2712 /*
2713 ** Return values of columns for the row at which the series_cursor
2714 ** is currently pointing.
2715 */
2716 static int fsdirColumn(
2717   sqlite3_vtab_cursor *cur,   /* The cursor */
2718   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2719   int i                       /* Which column to return */
2720 ){
2721   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2722   switch( i ){
2723     case 0: { /* name */
2724       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2725       break;
2726     }
2727
2728     case 1: /* mode */
2729       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2730       break;
2731
2732     case 2: /* mtime */
2733       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2734       break;
2735
2736     case 3: { /* data */
2737       mode_t m = pCur->sStat.st_mode;
2738       if( S_ISDIR(m) ){
2739         sqlite3_result_null(ctx);
2740 #if !defined(_WIN32) && !defined(WIN32)
2741       }else if( S_ISLNK(m) ){
2742         char aStatic[64];
2743         char *aBuf = aStatic;
2744         int nBuf = 64;
2745         int n;
2746
2747         while( 1 ){
2748           n = readlink(pCur->zPath, aBuf, nBuf);
2749           if( n<nBuf ) break;
2750           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2751           nBuf = nBuf*2;
2752           aBuf = sqlite3_malloc(nBuf);
2753           if( aBuf==0 ){
2754             sqlite3_result_error_nomem(ctx);
2755             return SQLITE_NOMEM;
2756           }
2757         }
2758
2759         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2760         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2761 #endif
2762       }else{
2763         readFileContents(ctx, pCur->zPath);
2764       }
2765     }
2766   }
2767   return SQLITE_OK;
2768 }
2769
2770 /*
2771 ** Return the rowid for the current row. In this implementation, the
2772 ** first row returned is assigned rowid value 1, and each subsequent
2773 ** row a value 1 more than that of the previous.
2774 */
2775 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2776   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2777   *pRowid = pCur->iRowid;
2778   return SQLITE_OK;
2779 }
2780
2781 /*
2782 ** Return TRUE if the cursor has been moved off of the last
2783 ** row of output.
2784 */
2785 static int fsdirEof(sqlite3_vtab_cursor *cur){
2786   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2787   return (pCur->zPath==0);
2788 }
2789
2790 /*
2791 ** xFilter callback.
2792 */
2793 static int fsdirFilter(
2794   sqlite3_vtab_cursor *cur, 
2795   int idxNum, const char *idxStr,
2796   int argc, sqlite3_value **argv
2797 ){
2798   const char *zDir = 0;
2799   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2800   (void)idxStr;
2801   fsdirResetCursor(pCur);
2802
2803   if( idxNum==0 ){
2804     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2805     return SQLITE_ERROR;
2806   }
2807
2808   assert( argc==idxNum && (argc==1 || argc==2) );
2809   zDir = (const char*)sqlite3_value_text(argv[0]);
2810   if( zDir==0 ){
2811     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2812     return SQLITE_ERROR;
2813   }
2814   if( argc==2 ){
2815     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2816   }
2817   if( pCur->zBase ){
2818     pCur->nBase = (int)strlen(pCur->zBase)+1;
2819     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2820   }else{
2821     pCur->zPath = sqlite3_mprintf("%s", zDir);
2822   }
2823
2824   if( pCur->zPath==0 ){
2825     return SQLITE_NOMEM;
2826   }
2827   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2828     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2829     return SQLITE_ERROR;
2830   }
2831
2832   return SQLITE_OK;
2833 }
2834
2835 /*
2836 ** SQLite will invoke this method one or more times while planning a query
2837 ** that uses the generate_series virtual table.  This routine needs to create
2838 ** a query plan for each invocation and compute an estimated cost for that
2839 ** plan.
2840 **
2841 ** In this implementation idxNum is used to represent the
2842 ** query plan.  idxStr is unused.
2843 **
2844 ** The query plan is represented by bits in idxNum:
2845 **
2846 **  (1)  start = $value  -- constraint exists
2847 **  (2)  stop = $value   -- constraint exists
2848 **  (4)  step = $value   -- constraint exists
2849 **  (8)  output in descending order
2850 */
2851 static int fsdirBestIndex(
2852   sqlite3_vtab *tab,
2853   sqlite3_index_info *pIdxInfo
2854 ){
2855   int i;                 /* Loop over constraints */
2856   int idx4 = -1;
2857   int idx5 = -1;
2858   const struct sqlite3_index_constraint *pConstraint;
2859
2860   (void)tab;
2861   pConstraint = pIdxInfo->aConstraint;
2862   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2863     if( pConstraint->usable==0 ) continue;
2864     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2865     if( pConstraint->iColumn==4 ) idx4 = i;
2866     if( pConstraint->iColumn==5 ) idx5 = i;
2867   }
2868
2869   if( idx4<0 ){
2870     pIdxInfo->idxNum = 0;
2871     pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
2872   }else{
2873     pIdxInfo->aConstraintUsage[idx4].omit = 1;
2874     pIdxInfo->aConstraintUsage[idx4].argvIndex = 1;
2875     if( idx5>=0 ){
2876       pIdxInfo->aConstraintUsage[idx5].omit = 1;
2877       pIdxInfo->aConstraintUsage[idx5].argvIndex = 2;
2878       pIdxInfo->idxNum = 2;
2879       pIdxInfo->estimatedCost = 10.0;
2880     }else{
2881       pIdxInfo->idxNum = 1;
2882       pIdxInfo->estimatedCost = 100.0;
2883     }
2884   }
2885
2886   return SQLITE_OK;
2887 }
2888
2889 /*
2890 ** Register the "fsdir" virtual table.
2891 */
2892 static int fsdirRegister(sqlite3 *db){
2893   static sqlite3_module fsdirModule = {
2894     0,                         /* iVersion */
2895     0,                         /* xCreate */
2896     fsdirConnect,              /* xConnect */
2897     fsdirBestIndex,            /* xBestIndex */
2898     fsdirDisconnect,           /* xDisconnect */
2899     0,                         /* xDestroy */
2900     fsdirOpen,                 /* xOpen - open a cursor */
2901     fsdirClose,                /* xClose - close a cursor */
2902     fsdirFilter,               /* xFilter - configure scan constraints */
2903     fsdirNext,                 /* xNext - advance a cursor */
2904     fsdirEof,                  /* xEof - check for end of scan */
2905     fsdirColumn,               /* xColumn - read data */
2906     fsdirRowid,                /* xRowid - read data */
2907     0,                         /* xUpdate */
2908     0,                         /* xBegin */
2909     0,                         /* xSync */
2910     0,                         /* xCommit */
2911     0,                         /* xRollback */
2912     0,                         /* xFindMethod */
2913     0,                         /* xRename */
2914     0,                         /* xSavepoint */
2915     0,                         /* xRelease */
2916     0                          /* xRollbackTo */
2917   };
2918
2919   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2920   return rc;
2921 }
2922 #else         /* SQLITE_OMIT_VIRTUALTABLE */
2923 # define fsdirRegister(x) SQLITE_OK
2924 #endif
2925
2926 #ifdef _WIN32
2927
2928 #endif
2929 int sqlite3_fileio_init(
2930   sqlite3 *db, 
2931   char **pzErrMsg, 
2932   const sqlite3_api_routines *pApi
2933 ){
2934   int rc = SQLITE_OK;
2935   SQLITE_EXTENSION_INIT2(pApi);
2936   (void)pzErrMsg;  /* Unused parameter */
2937   rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
2938                                readfileFunc, 0, 0);
2939   if( rc==SQLITE_OK ){
2940     rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
2941                                  writefileFunc, 0, 0);
2942   }
2943   if( rc==SQLITE_OK ){
2944     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
2945                                  lsModeFunc, 0, 0);
2946   }
2947   if( rc==SQLITE_OK ){
2948     rc = fsdirRegister(db);
2949   }
2950   return rc;
2951 }
2952
2953 /************************* End ../ext/misc/fileio.c ********************/
2954 /************************* Begin ../ext/misc/completion.c ******************/
2955 /*
2956 ** 2017-07-10
2957 **
2958 ** The author disclaims copyright to this source code.  In place of
2959 ** a legal notice, here is a blessing:
2960 **
2961 **    May you do good and not evil.
2962 **    May you find forgiveness for yourself and forgive others.
2963 **    May you share freely, never taking more than you give.
2964 **
2965 *************************************************************************
2966 **
2967 ** This file implements an eponymous virtual table that returns suggested
2968 ** completions for a partial SQL input.
2969 **
2970 ** Suggested usage:
2971 **
2972 **     SELECT DISTINCT candidate COLLATE nocase
2973 **       FROM completion($prefix,$wholeline)
2974 **      ORDER BY 1;
2975 **
2976 ** The two query parameters are optional.  $prefix is the text of the
2977 ** current word being typed and that is to be completed.  $wholeline is
2978 ** the complete input line, used for context.
2979 **
2980 ** The raw completion() table might return the same candidate multiple
2981 ** times, for example if the same column name is used to two or more
2982 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
2983 ** the DISTINCT and ORDER BY are recommended.
2984 **
2985 ** This virtual table operates at the speed of human typing, and so there
2986 ** is no attempt to make it fast.  Even a slow implementation will be much
2987 ** faster than any human can type.
2988 **
2989 */
2990 SQLITE_EXTENSION_INIT1
2991 #include <assert.h>
2992 #include <string.h>
2993 #include <ctype.h>
2994
2995 #ifndef SQLITE_OMIT_VIRTUALTABLE
2996
2997 /* completion_vtab is a subclass of sqlite3_vtab which will
2998 ** serve as the underlying representation of a completion virtual table
2999 */
3000 typedef struct completion_vtab completion_vtab;
3001 struct completion_vtab {
3002   sqlite3_vtab base;  /* Base class - must be first */
3003   sqlite3 *db;        /* Database connection for this completion vtab */
3004 };
3005
3006 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3007 ** serve as the underlying representation of a cursor that scans
3008 ** over rows of the result
3009 */
3010 typedef struct completion_cursor completion_cursor;
3011 struct completion_cursor {
3012   sqlite3_vtab_cursor base;  /* Base class - must be first */
3013   sqlite3 *db;               /* Database connection for this cursor */
3014   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3015   char *zPrefix;             /* The prefix for the word we want to complete */
3016   char *zLine;               /* The whole that we want to complete */
3017   const char *zCurrentRow;   /* Current output row */
3018   int szRow;                 /* Length of the zCurrentRow string */
3019   sqlite3_stmt *pStmt;       /* Current statement */
3020   sqlite3_int64 iRowid;      /* The rowid */
3021   int ePhase;                /* Current phase */
3022   int j;                     /* inter-phase counter */
3023 };
3024
3025 /* Values for ePhase:
3026 */
3027 #define COMPLETION_FIRST_PHASE   1
3028 #define COMPLETION_KEYWORDS      1
3029 #define COMPLETION_PRAGMAS       2
3030 #define COMPLETION_FUNCTIONS     3
3031 #define COMPLETION_COLLATIONS    4
3032 #define COMPLETION_INDEXES       5
3033 #define COMPLETION_TRIGGERS      6
3034 #define COMPLETION_DATABASES     7
3035 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3036 #define COMPLETION_COLUMNS       9
3037 #define COMPLETION_MODULES       10
3038 #define COMPLETION_EOF           11
3039
3040 /*
3041 ** The completionConnect() method is invoked to create a new
3042 ** completion_vtab that describes the completion virtual table.
3043 **
3044 ** Think of this routine as the constructor for completion_vtab objects.
3045 **
3046 ** All this routine needs to do is:
3047 **
3048 **    (1) Allocate the completion_vtab object and initialize all fields.
3049 **
3050 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3051 **        result set of queries against completion will look like.
3052 */
3053 static int completionConnect(
3054   sqlite3 *db,
3055   void *pAux,
3056   int argc, const char *const*argv,
3057   sqlite3_vtab **ppVtab,
3058   char **pzErr
3059 ){
3060   completion_vtab *pNew;
3061   int rc;
3062
3063   (void)(pAux);    /* Unused parameter */
3064   (void)(argc);    /* Unused parameter */
3065   (void)(argv);    /* Unused parameter */
3066   (void)(pzErr);   /* Unused parameter */
3067
3068 /* Column numbers */
3069 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3070 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3071 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3072 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3073
3074   rc = sqlite3_declare_vtab(db,
3075       "CREATE TABLE x("
3076       "  candidate TEXT,"
3077       "  prefix TEXT HIDDEN,"
3078       "  wholeline TEXT HIDDEN,"
3079       "  phase INT HIDDEN"        /* Used for debugging only */
3080       ")");
3081   if( rc==SQLITE_OK ){
3082     pNew = sqlite3_malloc( sizeof(*pNew) );
3083     *ppVtab = (sqlite3_vtab*)pNew;
3084     if( pNew==0 ) return SQLITE_NOMEM;
3085     memset(pNew, 0, sizeof(*pNew));
3086     pNew->db = db;
3087   }
3088   return rc;
3089 }
3090
3091 /*
3092 ** This method is the destructor for completion_cursor objects.
3093 */
3094 static int completionDisconnect(sqlite3_vtab *pVtab){
3095   sqlite3_free(pVtab);
3096   return SQLITE_OK;
3097 }
3098
3099 /*
3100 ** Constructor for a new completion_cursor object.
3101 */
3102 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3103   completion_cursor *pCur;
3104   pCur = sqlite3_malloc( sizeof(*pCur) );
3105   if( pCur==0 ) return SQLITE_NOMEM;
3106   memset(pCur, 0, sizeof(*pCur));
3107   pCur->db = ((completion_vtab*)p)->db;
3108   *ppCursor = &pCur->base;
3109   return SQLITE_OK;
3110 }
3111
3112 /*
3113 ** Reset the completion_cursor.
3114 */
3115 static void completionCursorReset(completion_cursor *pCur){
3116   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3117   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3118   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3119   pCur->j = 0;
3120 }
3121
3122 /*
3123 ** Destructor for a completion_cursor.
3124 */
3125 static int completionClose(sqlite3_vtab_cursor *cur){
3126   completionCursorReset((completion_cursor*)cur);
3127   sqlite3_free(cur);
3128   return SQLITE_OK;
3129 }
3130
3131 /*
3132 ** Advance a completion_cursor to its next row of output.
3133 **
3134 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3135 ** record the current state of the scan.  This routine sets ->zCurrentRow
3136 ** to the current row of output and then returns.  If no more rows remain,
3137 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3138 ** table that has reached the end of its scan.
3139 **
3140 ** The current implementation just lists potential identifiers and
3141 ** keywords and filters them by zPrefix.  Future enhancements should
3142 ** take zLine into account to try to restrict the set of identifiers and
3143 ** keywords based on what would be legal at the current point of input.
3144 */
3145 static int completionNext(sqlite3_vtab_cursor *cur){
3146   completion_cursor *pCur = (completion_cursor*)cur;
3147   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3148   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3149   pCur->iRowid++;
3150   while( pCur->ePhase!=COMPLETION_EOF ){
3151     switch( pCur->ePhase ){
3152       case COMPLETION_KEYWORDS: {
3153         if( pCur->j >= sqlite3_keyword_count() ){
3154           pCur->zCurrentRow = 0;
3155           pCur->ePhase = COMPLETION_DATABASES;
3156         }else{
3157           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3158         }
3159         iCol = -1;
3160         break;
3161       }
3162       case COMPLETION_DATABASES: {
3163         if( pCur->pStmt==0 ){
3164           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3165                              &pCur->pStmt, 0);
3166         }
3167         iCol = 1;
3168         eNextPhase = COMPLETION_TABLES;
3169         break;
3170       }
3171       case COMPLETION_TABLES: {
3172         if( pCur->pStmt==0 ){
3173           sqlite3_stmt *pS2;
3174           char *zSql = 0;
3175           const char *zSep = "";
3176           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3177           while( sqlite3_step(pS2)==SQLITE_ROW ){
3178             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3179             zSql = sqlite3_mprintf(
3180                "%z%s"
3181                "SELECT name FROM \"%w\".sqlite_master",
3182                zSql, zSep, zDb
3183             );
3184             if( zSql==0 ) return SQLITE_NOMEM;
3185             zSep = " UNION ";
3186           }
3187           sqlite3_finalize(pS2);
3188           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3189           sqlite3_free(zSql);
3190         }
3191         iCol = 0;
3192         eNextPhase = COMPLETION_COLUMNS;
3193         break;
3194       }
3195       case COMPLETION_COLUMNS: {
3196         if( pCur->pStmt==0 ){
3197           sqlite3_stmt *pS2;
3198           char *zSql = 0;
3199           const char *zSep = "";
3200           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3201           while( sqlite3_step(pS2)==SQLITE_ROW ){
3202             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3203             zSql = sqlite3_mprintf(
3204                "%z%s"
3205                "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3206                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3207                " WHERE sm.type='table'",
3208                zSql, zSep, zDb, zDb
3209             );
3210             if( zSql==0 ) return SQLITE_NOMEM;
3211             zSep = " UNION ";
3212           }
3213           sqlite3_finalize(pS2);
3214           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3215           sqlite3_free(zSql);
3216         }
3217         iCol = 0;
3218         eNextPhase = COMPLETION_EOF;
3219         break;
3220       }
3221     }
3222     if( iCol<0 ){
3223       /* This case is when the phase presets zCurrentRow */
3224       if( pCur->zCurrentRow==0 ) continue;
3225     }else{
3226       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3227         /* Extract the next row of content */
3228         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3229         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3230       }else{
3231         /* When all rows are finished, advance to the next phase */
3232         sqlite3_finalize(pCur->pStmt);
3233         pCur->pStmt = 0;
3234         pCur->ePhase = eNextPhase;
3235         continue;
3236       }
3237     }
3238     if( pCur->nPrefix==0 ) break;
3239     if( pCur->nPrefix<=pCur->szRow
3240      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3241     ){
3242       break;
3243     }
3244   }
3245
3246   return SQLITE_OK;
3247 }
3248
3249 /*
3250 ** Return values of columns for the row at which the completion_cursor
3251 ** is currently pointing.
3252 */
3253 static int completionColumn(
3254   sqlite3_vtab_cursor *cur,   /* The cursor */
3255   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3256   int i                       /* Which column to return */
3257 ){
3258   completion_cursor *pCur = (completion_cursor*)cur;
3259   switch( i ){
3260     case COMPLETION_COLUMN_CANDIDATE: {
3261       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3262       break;
3263     }
3264     case COMPLETION_COLUMN_PREFIX: {
3265       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3266       break;
3267     }
3268     case COMPLETION_COLUMN_WHOLELINE: {
3269       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3270       break;
3271     }
3272     case COMPLETION_COLUMN_PHASE: {
3273       sqlite3_result_int(ctx, pCur->ePhase);
3274       break;
3275     }
3276   }
3277   return SQLITE_OK;
3278 }
3279
3280 /*
3281 ** Return the rowid for the current row.  In this implementation, the
3282 ** rowid is the same as the output value.
3283 */
3284 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3285   completion_cursor *pCur = (completion_cursor*)cur;
3286   *pRowid = pCur->iRowid;
3287   return SQLITE_OK;
3288 }
3289
3290 /*
3291 ** Return TRUE if the cursor has been moved off of the last
3292 ** row of output.
3293 */
3294 static int completionEof(sqlite3_vtab_cursor *cur){
3295   completion_cursor *pCur = (completion_cursor*)cur;
3296   return pCur->ePhase >= COMPLETION_EOF;
3297 }
3298
3299 /*
3300 ** This method is called to "rewind" the completion_cursor object back
3301 ** to the first row of output.  This method is always called at least
3302 ** once prior to any call to completionColumn() or completionRowid() or 
3303 ** completionEof().
3304 */
3305 static int completionFilter(
3306   sqlite3_vtab_cursor *pVtabCursor, 
3307   int idxNum, const char *idxStr,
3308   int argc, sqlite3_value **argv
3309 ){
3310   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3311   int iArg = 0;
3312   (void)(idxStr);   /* Unused parameter */
3313   (void)(argc);     /* Unused parameter */
3314   completionCursorReset(pCur);
3315   if( idxNum & 1 ){
3316     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3317     if( pCur->nPrefix>0 ){
3318       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3319       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3320     }
3321     iArg++;
3322   }
3323   if( idxNum & 2 ){
3324     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3325     if( pCur->nLine>0 ){
3326       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3327       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3328     }
3329     iArg++;
3330   }
3331   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3332     int i = pCur->nLine;
3333     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3334       i--;
3335     }
3336     pCur->nPrefix = pCur->nLine - i;
3337     if( pCur->nPrefix>0 ){
3338       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3339       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3340     }
3341   }
3342   pCur->iRowid = 0;
3343   pCur->ePhase = COMPLETION_FIRST_PHASE;
3344   return completionNext(pVtabCursor);
3345 }
3346
3347 /*
3348 ** SQLite will invoke this method one or more times while planning a query
3349 ** that uses the completion virtual table.  This routine needs to create
3350 ** a query plan for each invocation and compute an estimated cost for that
3351 ** plan.
3352 **
3353 ** There are two hidden parameters that act as arguments to the table-valued
3354 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3355 ** is available and bit 1 is set if "wholeline" is available.
3356 */
3357 static int completionBestIndex(
3358   sqlite3_vtab *tab,
3359   sqlite3_index_info *pIdxInfo
3360 ){
3361   int i;                 /* Loop over constraints */
3362   int idxNum = 0;        /* The query plan bitmask */
3363   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3364   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3365   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3366   const struct sqlite3_index_constraint *pConstraint;
3367
3368   (void)(tab);    /* Unused parameter */
3369   pConstraint = pIdxInfo->aConstraint;
3370   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3371     if( pConstraint->usable==0 ) continue;
3372     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3373     switch( pConstraint->iColumn ){
3374       case COMPLETION_COLUMN_PREFIX:
3375         prefixIdx = i;
3376         idxNum |= 1;
3377         break;
3378       case COMPLETION_COLUMN_WHOLELINE:
3379         wholelineIdx = i;
3380         idxNum |= 2;
3381         break;
3382     }
3383   }
3384   if( prefixIdx>=0 ){
3385     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3386     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3387   }
3388   if( wholelineIdx>=0 ){
3389     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3390     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3391   }
3392   pIdxInfo->idxNum = idxNum;
3393   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3394   pIdxInfo->estimatedRows = 500 - 100*nArg;
3395   return SQLITE_OK;
3396 }
3397
3398 /*
3399 ** This following structure defines all the methods for the 
3400 ** completion virtual table.
3401 */
3402 static sqlite3_module completionModule = {
3403   0,                         /* iVersion */
3404   0,                         /* xCreate */
3405   completionConnect,         /* xConnect */
3406   completionBestIndex,       /* xBestIndex */
3407   completionDisconnect,      /* xDisconnect */
3408   0,                         /* xDestroy */
3409   completionOpen,            /* xOpen - open a cursor */
3410   completionClose,           /* xClose - close a cursor */
3411   completionFilter,          /* xFilter - configure scan constraints */
3412   completionNext,            /* xNext - advance a cursor */
3413   completionEof,             /* xEof - check for end of scan */
3414   completionColumn,          /* xColumn - read data */
3415   completionRowid,           /* xRowid - read data */
3416   0,                         /* xUpdate */
3417   0,                         /* xBegin */
3418   0,                         /* xSync */
3419   0,                         /* xCommit */
3420   0,                         /* xRollback */
3421   0,                         /* xFindMethod */
3422   0,                         /* xRename */
3423   0,                         /* xSavepoint */
3424   0,                         /* xRelease */
3425   0                          /* xRollbackTo */
3426 };
3427
3428 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3429
3430 int sqlite3CompletionVtabInit(sqlite3 *db){
3431   int rc = SQLITE_OK;
3432 #ifndef SQLITE_OMIT_VIRTUALTABLE
3433   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3434 #endif
3435   return rc;
3436 }
3437
3438 #ifdef _WIN32
3439
3440 #endif
3441 int sqlite3_completion_init(
3442   sqlite3 *db, 
3443   char **pzErrMsg, 
3444   const sqlite3_api_routines *pApi
3445 ){
3446   int rc = SQLITE_OK;
3447   SQLITE_EXTENSION_INIT2(pApi);
3448   (void)(pzErrMsg);  /* Unused parameter */
3449 #ifndef SQLITE_OMIT_VIRTUALTABLE
3450   rc = sqlite3CompletionVtabInit(db);
3451 #endif
3452   return rc;
3453 }
3454
3455 /************************* End ../ext/misc/completion.c ********************/
3456 /************************* Begin ../ext/misc/appendvfs.c ******************/
3457 /*
3458 ** 2017-10-20
3459 **
3460 ** The author disclaims copyright to this source code.  In place of
3461 ** a legal notice, here is a blessing:
3462 **
3463 **    May you do good and not evil.
3464 **    May you find forgiveness for yourself and forgive others.
3465 **    May you share freely, never taking more than you give.
3466 **
3467 ******************************************************************************
3468 **
3469 ** This file implements a VFS shim that allows an SQLite database to be
3470 ** appended onto the end of some other file, such as an executable.
3471 **
3472 ** A special record must appear at the end of the file that identifies the
3473 ** file as an appended database and provides an offset to page 1.  For
3474 ** best performance page 1 should be located at a disk page boundary, though
3475 ** that is not required.
3476 **
3477 ** When opening a database using this VFS, the connection might treat
3478 ** the file as an ordinary SQLite database, or it might treat is as a
3479 ** database appended onto some other file.  Here are the rules:
3480 **
3481 **  (1)  When opening a new empty file, that file is treated as an ordinary
3482 **       database.
3483 **
3484 **  (2)  When opening a file that begins with the standard SQLite prefix
3485 **       string "SQLite format 3", that file is treated as an ordinary
3486 **       database.
3487 **
3488 **  (3)  When opening a file that ends with the appendvfs trailer string
3489 **       "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3490 **       database.
3491 **
3492 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3493 **       set, then a new database is appended to the already existing file.
3494 **
3495 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3496 **
3497 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3498 ** the file containing the database is limited to 1GB.  This VFS will refuse
3499 ** to read or write past the 1GB mark.  This restriction might be lifted in
3500 ** future versions.  For now, if you need a large database, then keep the
3501 ** database in a separate file.
3502 **
3503 ** If the file being opened is not an appended database, then this shim is
3504 ** a pass-through into the default underlying VFS.
3505 **/
3506 SQLITE_EXTENSION_INIT1
3507 #include <string.h>
3508 #include <assert.h>
3509
3510 /* The append mark at the end of the database is:
3511 **
3512 **     Start-Of-SQLite3-NNNNNNNN
3513 **     123456789 123456789 12345
3514 **
3515 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3516 ** the offset to page 1.
3517 */
3518 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3519 #define APND_MARK_PREFIX_SZ  17
3520 #define APND_MARK_SIZE       25
3521
3522 /*
3523 ** Maximum size of the combined prefix + database + append-mark.  This
3524 ** must be less than 0x40000000 to avoid locking issues on Windows.
3525 */
3526 #define APND_MAX_SIZE  (65536*15259)
3527
3528 /*
3529 ** Forward declaration of objects used by this utility
3530 */
3531 typedef struct sqlite3_vfs ApndVfs;
3532 typedef struct ApndFile ApndFile;
3533
3534 /* Access to a lower-level VFS that (might) implement dynamic loading,
3535 ** access to randomness, etc.
3536 */
3537 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3538 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3539
3540 /* An open file */
3541 struct ApndFile {
3542   sqlite3_file base;              /* IO methods */
3543   sqlite3_int64 iPgOne;           /* File offset to page 1 */
3544   sqlite3_int64 iMark;            /* Start of the append-mark */
3545 };
3546
3547 /*
3548 ** Methods for ApndFile
3549 */
3550 static int apndClose(sqlite3_file*);
3551 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3552 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3553 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3554 static int apndSync(sqlite3_file*, int flags);
3555 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3556 static int apndLock(sqlite3_file*, int);
3557 static int apndUnlock(sqlite3_file*, int);
3558 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3559 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3560 static int apndSectorSize(sqlite3_file*);
3561 static int apndDeviceCharacteristics(sqlite3_file*);
3562 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3563 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3564 static void apndShmBarrier(sqlite3_file*);
3565 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3566 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3567 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3568
3569 /*
3570 ** Methods for ApndVfs
3571 */
3572 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3573 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3574 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3575 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3576 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3577 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3578 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3579 static void apndDlClose(sqlite3_vfs*, void*);
3580 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3581 static int apndSleep(sqlite3_vfs*, int microseconds);
3582 static int apndCurrentTime(sqlite3_vfs*, double*);
3583 static int apndGetLastError(sqlite3_vfs*, int, char *);
3584 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3585 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3586 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3587 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3588
3589 static sqlite3_vfs apnd_vfs = {
3590   3,                            /* iVersion (set when registered) */
3591   0,                            /* szOsFile (set when registered) */
3592   1024,                         /* mxPathname */
3593   0,                            /* pNext */
3594   "apndvfs",                    /* zName */
3595   0,                            /* pAppData (set when registered) */ 
3596   apndOpen,                     /* xOpen */
3597   apndDelete,                   /* xDelete */
3598   apndAccess,                   /* xAccess */
3599   apndFullPathname,             /* xFullPathname */
3600   apndDlOpen,                   /* xDlOpen */
3601   apndDlError,                  /* xDlError */
3602   apndDlSym,                    /* xDlSym */
3603   apndDlClose,                  /* xDlClose */
3604   apndRandomness,               /* xRandomness */
3605   apndSleep,                    /* xSleep */
3606   apndCurrentTime,              /* xCurrentTime */
3607   apndGetLastError,             /* xGetLastError */
3608   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3609   apndSetSystemCall,            /* xSetSystemCall */
3610   apndGetSystemCall,            /* xGetSystemCall */
3611   apndNextSystemCall            /* xNextSystemCall */
3612 };
3613
3614 static const sqlite3_io_methods apnd_io_methods = {
3615   3,                              /* iVersion */
3616   apndClose,                      /* xClose */
3617   apndRead,                       /* xRead */
3618   apndWrite,                      /* xWrite */
3619   apndTruncate,                   /* xTruncate */
3620   apndSync,                       /* xSync */
3621   apndFileSize,                   /* xFileSize */
3622   apndLock,                       /* xLock */
3623   apndUnlock,                     /* xUnlock */
3624   apndCheckReservedLock,          /* xCheckReservedLock */
3625   apndFileControl,                /* xFileControl */
3626   apndSectorSize,                 /* xSectorSize */
3627   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3628   apndShmMap,                     /* xShmMap */
3629   apndShmLock,                    /* xShmLock */
3630   apndShmBarrier,                 /* xShmBarrier */
3631   apndShmUnmap,                   /* xShmUnmap */
3632   apndFetch,                      /* xFetch */
3633   apndUnfetch                     /* xUnfetch */
3634 };
3635
3636
3637
3638 /*
3639 ** Close an apnd-file.
3640 */
3641 static int apndClose(sqlite3_file *pFile){
3642   pFile = ORIGFILE(pFile);
3643   return pFile->pMethods->xClose(pFile);
3644 }
3645
3646 /*
3647 ** Read data from an apnd-file.
3648 */
3649 static int apndRead(
3650   sqlite3_file *pFile, 
3651   void *zBuf, 
3652   int iAmt, 
3653   sqlite_int64 iOfst
3654 ){
3655   ApndFile *p = (ApndFile *)pFile;
3656   pFile = ORIGFILE(pFile);
3657   return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3658 }
3659
3660 /*
3661 ** Add the append-mark onto the end of the file.
3662 */
3663 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3664   int i;
3665   unsigned char a[APND_MARK_SIZE];
3666   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3667   for(i=0; i<8; i++){
3668     a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3669   }
3670   return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3671 }
3672
3673 /*
3674 ** Write data to an apnd-file.
3675 */
3676 static int apndWrite(
3677   sqlite3_file *pFile,
3678   const void *zBuf,
3679   int iAmt,
3680   sqlite_int64 iOfst
3681 ){
3682   int rc;
3683   ApndFile *p = (ApndFile *)pFile;
3684   pFile = ORIGFILE(pFile);
3685   if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3686   rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3687   if( rc==SQLITE_OK &&  iOfst + iAmt + p->iPgOne > p->iMark ){
3688     sqlite3_int64 sz = 0;
3689     rc = pFile->pMethods->xFileSize(pFile, &sz);
3690     if( rc==SQLITE_OK ){
3691       p->iMark = sz - APND_MARK_SIZE;
3692       if( iOfst + iAmt + p->iPgOne > p->iMark ){
3693         p->iMark = p->iPgOne + iOfst + iAmt;
3694         rc = apndWriteMark(p, pFile);
3695       }
3696     }
3697   }
3698   return rc;
3699 }
3700
3701 /*
3702 ** Truncate an apnd-file.
3703 */
3704 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3705   int rc;
3706   ApndFile *p = (ApndFile *)pFile;
3707   pFile = ORIGFILE(pFile);
3708   rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3709   if( rc==SQLITE_OK ){
3710     p->iMark = p->iPgOne+size;
3711     rc = apndWriteMark(p, pFile);
3712   }
3713   return rc;
3714 }
3715
3716 /*
3717 ** Sync an apnd-file.
3718 */
3719 static int apndSync(sqlite3_file *pFile, int flags){
3720   pFile = ORIGFILE(pFile);
3721   return pFile->pMethods->xSync(pFile, flags);
3722 }
3723
3724 /*
3725 ** Return the current file-size of an apnd-file.
3726 */
3727 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3728   ApndFile *p = (ApndFile *)pFile;
3729   int rc;
3730   pFile = ORIGFILE(p);
3731   rc = pFile->pMethods->xFileSize(pFile, pSize);
3732   if( rc==SQLITE_OK && p->iPgOne ){
3733     *pSize -= p->iPgOne + APND_MARK_SIZE;
3734   }
3735   return rc;
3736 }
3737
3738 /*
3739 ** Lock an apnd-file.
3740 */
3741 static int apndLock(sqlite3_file *pFile, int eLock){
3742   pFile = ORIGFILE(pFile);
3743   return pFile->pMethods->xLock(pFile, eLock);
3744 }
3745
3746 /*
3747 ** Unlock an apnd-file.
3748 */
3749 static int apndUnlock(sqlite3_file *pFile, int eLock){
3750   pFile = ORIGFILE(pFile);
3751   return pFile->pMethods->xUnlock(pFile, eLock);
3752 }
3753
3754 /*
3755 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3756 */
3757 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3758   pFile = ORIGFILE(pFile);
3759   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3760 }
3761
3762 /*
3763 ** File control method. For custom operations on an apnd-file.
3764 */
3765 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3766   ApndFile *p = (ApndFile *)pFile;
3767   int rc;
3768   pFile = ORIGFILE(pFile);
3769   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3770   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3771     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3772   }
3773   return rc;
3774 }
3775
3776 /*
3777 ** Return the sector-size in bytes for an apnd-file.
3778 */
3779 static int apndSectorSize(sqlite3_file *pFile){
3780   pFile = ORIGFILE(pFile);
3781   return pFile->pMethods->xSectorSize(pFile);
3782 }
3783
3784 /*
3785 ** Return the device characteristic flags supported by an apnd-file.
3786 */
3787 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3788   pFile = ORIGFILE(pFile);
3789   return pFile->pMethods->xDeviceCharacteristics(pFile);
3790 }
3791
3792 /* Create a shared memory file mapping */
3793 static int apndShmMap(
3794   sqlite3_file *pFile,
3795   int iPg,
3796   int pgsz,
3797   int bExtend,
3798   void volatile **pp
3799 ){
3800   pFile = ORIGFILE(pFile);
3801   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3802 }
3803
3804 /* Perform locking on a shared-memory segment */
3805 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3806   pFile = ORIGFILE(pFile);
3807   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3808 }
3809
3810 /* Memory barrier operation on shared memory */
3811 static void apndShmBarrier(sqlite3_file *pFile){
3812   pFile = ORIGFILE(pFile);
3813   pFile->pMethods->xShmBarrier(pFile);
3814 }
3815
3816 /* Unmap a shared memory segment */
3817 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3818   pFile = ORIGFILE(pFile);
3819   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3820 }
3821
3822 /* Fetch a page of a memory-mapped file */
3823 static int apndFetch(
3824   sqlite3_file *pFile,
3825   sqlite3_int64 iOfst,
3826   int iAmt,
3827   void **pp
3828 ){
3829   ApndFile *p = (ApndFile *)pFile;
3830   pFile = ORIGFILE(pFile);
3831   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3832 }
3833
3834 /* Release a memory-mapped page */
3835 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3836   ApndFile *p = (ApndFile *)pFile;
3837   pFile = ORIGFILE(pFile);
3838   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3839 }
3840
3841 /*
3842 ** Check to see if the file is an ordinary SQLite database file.
3843 */
3844 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3845   int rc;
3846   char zHdr[16];
3847   static const char aSqliteHdr[] = "SQLite format 3";
3848   if( sz<512 ) return 0;
3849   rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3850   if( rc ) return 0;
3851   return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3852 }
3853
3854 /*
3855 ** Try to read the append-mark off the end of a file.  Return the
3856 ** start of the appended database if the append-mark is present.  If
3857 ** there is no append-mark, return -1;
3858 */
3859 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3860   int rc, i;
3861   sqlite3_int64 iMark;
3862   unsigned char a[APND_MARK_SIZE];
3863
3864   if( sz<=APND_MARK_SIZE ) return -1;
3865   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3866   if( rc ) return -1;
3867   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3868   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3869   for(i=1; i<8; i++){    
3870     iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3871   }
3872   return iMark;
3873 }
3874
3875 /*
3876 ** Open an apnd file handle.
3877 */
3878 static int apndOpen(
3879   sqlite3_vfs *pVfs,
3880   const char *zName,
3881   sqlite3_file *pFile,
3882   int flags,
3883   int *pOutFlags
3884 ){
3885   ApndFile *p;
3886   sqlite3_file *pSubFile;
3887   sqlite3_vfs *pSubVfs;
3888   int rc;
3889   sqlite3_int64 sz;
3890   pSubVfs = ORIGVFS(pVfs);
3891   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3892     return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3893   }
3894   p = (ApndFile*)pFile;
3895   memset(p, 0, sizeof(*p));
3896   pSubFile = ORIGFILE(pFile);
3897   p->base.pMethods = &apnd_io_methods;
3898   rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3899   if( rc ) goto apnd_open_done;
3900   rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3901   if( rc ){
3902     pSubFile->pMethods->xClose(pSubFile);
3903     goto apnd_open_done;
3904   }
3905   if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3906     memmove(pFile, pSubFile, pSubVfs->szOsFile);
3907     return SQLITE_OK;
3908   }
3909   p->iMark = 0;
3910   p->iPgOne = apndReadMark(sz, pFile);
3911   if( p->iPgOne>0 ){
3912     return SQLITE_OK;
3913   }
3914   if( (flags & SQLITE_OPEN_CREATE)==0 ){
3915     pSubFile->pMethods->xClose(pSubFile);
3916     rc = SQLITE_CANTOPEN;
3917   }
3918   p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3919 apnd_open_done:
3920   if( rc ) pFile->pMethods = 0;
3921   return rc;
3922 }
3923
3924 /*
3925 ** All other VFS methods are pass-thrus.
3926 */
3927 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3928   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
3929 }
3930 static int apndAccess(
3931   sqlite3_vfs *pVfs, 
3932   const char *zPath, 
3933   int flags, 
3934   int *pResOut
3935 ){
3936   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
3937 }
3938 static int apndFullPathname(
3939   sqlite3_vfs *pVfs, 
3940   const char *zPath, 
3941   int nOut, 
3942   char *zOut
3943 ){
3944   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
3945 }
3946 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
3947   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
3948 }
3949 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
3950   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
3951 }
3952 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
3953   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
3954 }
3955 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
3956   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
3957 }
3958 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
3959   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
3960 }
3961 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
3962   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
3963 }
3964 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
3965   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
3966 }
3967 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
3968   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
3969 }
3970 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
3971   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
3972 }
3973 static int apndSetSystemCall(
3974   sqlite3_vfs *pVfs,
3975   const char *zName,
3976   sqlite3_syscall_ptr pCall
3977 ){
3978   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
3979 }
3980 static sqlite3_syscall_ptr apndGetSystemCall(
3981   sqlite3_vfs *pVfs,
3982   const char *zName
3983 ){
3984   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
3985 }
3986 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
3987   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
3988 }
3989
3990   
3991 #ifdef _WIN32
3992
3993 #endif
3994 /* 
3995 ** This routine is called when the extension is loaded.
3996 ** Register the new VFS.
3997 */
3998 int sqlite3_appendvfs_init(
3999   sqlite3 *db, 
4000   char **pzErrMsg, 
4001   const sqlite3_api_routines *pApi
4002 ){
4003   int rc = SQLITE_OK;
4004   sqlite3_vfs *pOrig;
4005   SQLITE_EXTENSION_INIT2(pApi);
4006   (void)pzErrMsg;
4007   (void)db;
4008   pOrig = sqlite3_vfs_find(0);
4009   apnd_vfs.iVersion = pOrig->iVersion;
4010   apnd_vfs.pAppData = pOrig;
4011   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4012   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4013 #ifdef APPENDVFS_TEST
4014   if( rc==SQLITE_OK ){
4015     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4016   }
4017 #endif
4018   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4019   return rc;
4020 }
4021
4022 /************************* End ../ext/misc/appendvfs.c ********************/
4023 #ifdef SQLITE_HAVE_ZLIB
4024 /************************* Begin ../ext/misc/zipfile.c ******************/
4025 /*
4026 ** 2017-12-26
4027 **
4028 ** The author disclaims copyright to this source code.  In place of
4029 ** a legal notice, here is a blessing:
4030 **
4031 **    May you do good and not evil.
4032 **    May you find forgiveness for yourself and forgive others.
4033 **    May you share freely, never taking more than you give.
4034 **
4035 ******************************************************************************
4036 **
4037 ** This file implements a virtual table for reading and writing ZIP archive
4038 ** files.
4039 **
4040 ** Usage example:
4041 **
4042 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4043 **
4044 ** Current limitations:
4045 **
4046 **    *  No support for encryption
4047 **    *  No support for ZIP archives spanning multiple files
4048 **    *  No support for zip64 extensions
4049 **    *  Only the "inflate/deflate" (zlib) compression method is supported
4050 */
4051 SQLITE_EXTENSION_INIT1
4052 #include <stdio.h>
4053 #include <string.h>
4054 #include <assert.h>
4055
4056 #include <zlib.h>
4057
4058 #ifndef SQLITE_OMIT_VIRTUALTABLE
4059
4060 #ifndef SQLITE_AMALGAMATION
4061
4062 /* typedef sqlite3_int64 i64; */
4063 /* typedef unsigned char u8; */
4064 typedef unsigned short u16;
4065 typedef unsigned long u32;
4066 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4067
4068 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4069 # define ALWAYS(X)      (1)
4070 # define NEVER(X)       (0)
4071 #elif !defined(NDEBUG)
4072 # define ALWAYS(X)      ((X)?1:(assert(0),0))
4073 # define NEVER(X)       ((X)?(assert(0),1):0)
4074 #else
4075 # define ALWAYS(X)      (X)
4076 # define NEVER(X)       (X)
4077 #endif
4078
4079 #endif   /* SQLITE_AMALGAMATION */
4080
4081 /*
4082 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4083 **
4084 ** In some ways it would be better to obtain these values from system 
4085 ** header files. But, the dependency is undesirable and (a) these
4086 ** have been stable for decades, (b) the values are part of POSIX and
4087 ** are also made explicit in [man stat], and (c) are part of the 
4088 ** file format for zip archives.
4089 */
4090 #ifndef S_IFDIR
4091 # define S_IFDIR 0040000
4092 #endif
4093 #ifndef S_IFREG
4094 # define S_IFREG 0100000
4095 #endif
4096 #ifndef S_IFLNK
4097 # define S_IFLNK 0120000
4098 #endif
4099
4100 static const char ZIPFILE_SCHEMA[] = 
4101   "CREATE TABLE y("
4102     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
4103     "mode,"              /* 1: POSIX mode for file */
4104     "mtime,"             /* 2: Last modification time (secs since 1970)*/
4105     "sz,"                /* 3: Size of object */
4106     "rawdata,"           /* 4: Raw data */
4107     "data,"              /* 5: Uncompressed data */
4108     "method,"            /* 6: Compression method (integer) */
4109     "z HIDDEN"           /* 7: Name of zip file */
4110   ") WITHOUT ROWID;";
4111
4112 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
4113 #define ZIPFILE_BUFFER_SIZE (64*1024)
4114
4115
4116 /*
4117 ** Magic numbers used to read and write zip files.
4118 **
4119 ** ZIPFILE_NEWENTRY_MADEBY:
4120 **   Use this value for the "version-made-by" field in new zip file
4121 **   entries. The upper byte indicates "unix", and the lower byte 
4122 **   indicates that the zip file matches pkzip specification 3.0. 
4123 **   This is what info-zip seems to do.
4124 **
4125 ** ZIPFILE_NEWENTRY_REQUIRED:
4126 **   Value for "version-required-to-extract" field of new entries.
4127 **   Version 2.0 is required to support folders and deflate compression.
4128 **
4129 ** ZIPFILE_NEWENTRY_FLAGS:
4130 **   Value for "general-purpose-bit-flags" field of new entries. Bit
4131 **   11 means "utf-8 filename and comment".
4132 **
4133 ** ZIPFILE_SIGNATURE_CDS:
4134 **   First 4 bytes of a valid CDS record.
4135 **
4136 ** ZIPFILE_SIGNATURE_LFH:
4137 **   First 4 bytes of a valid LFH record.
4138 **
4139 ** ZIPFILE_SIGNATURE_EOCD
4140 **   First 4 bytes of a valid EOCD record.
4141 */
4142 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
4143 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
4144 #define ZIPFILE_NEWENTRY_REQUIRED 20
4145 #define ZIPFILE_NEWENTRY_FLAGS    0x800
4146 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
4147 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
4148 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
4149
4150 /*
4151 ** The sizes of the fixed-size part of each of the three main data 
4152 ** structures in a zip archive.
4153 */
4154 #define ZIPFILE_LFH_FIXED_SZ      30
4155 #define ZIPFILE_EOCD_FIXED_SZ     22
4156 #define ZIPFILE_CDS_FIXED_SZ      46
4157
4158 /*
4159 *** 4.3.16  End of central directory record:
4160 ***
4161 ***   end of central dir signature    4 bytes  (0x06054b50)
4162 ***   number of this disk             2 bytes
4163 ***   number of the disk with the
4164 ***   start of the central directory  2 bytes
4165 ***   total number of entries in the
4166 ***   central directory on this disk  2 bytes
4167 ***   total number of entries in
4168 ***   the central directory           2 bytes
4169 ***   size of the central directory   4 bytes
4170 ***   offset of start of central
4171 ***   directory with respect to
4172 ***   the starting disk number        4 bytes
4173 ***   .ZIP file comment length        2 bytes
4174 ***   .ZIP file comment       (variable size)
4175 */
4176 typedef struct ZipfileEOCD ZipfileEOCD;
4177 struct ZipfileEOCD {
4178   u16 iDisk;
4179   u16 iFirstDisk;
4180   u16 nEntry;
4181   u16 nEntryTotal;
4182   u32 nSize;
4183   u32 iOffset;
4184 };
4185
4186 /*
4187 *** 4.3.12  Central directory structure:
4188 ***
4189 *** ...
4190 ***
4191 ***   central file header signature   4 bytes  (0x02014b50)
4192 ***   version made by                 2 bytes
4193 ***   version needed to extract       2 bytes
4194 ***   general purpose bit flag        2 bytes
4195 ***   compression method              2 bytes
4196 ***   last mod file time              2 bytes
4197 ***   last mod file date              2 bytes
4198 ***   crc-32                          4 bytes
4199 ***   compressed size                 4 bytes
4200 ***   uncompressed size               4 bytes
4201 ***   file name length                2 bytes
4202 ***   extra field length              2 bytes
4203 ***   file comment length             2 bytes
4204 ***   disk number start               2 bytes
4205 ***   internal file attributes        2 bytes
4206 ***   external file attributes        4 bytes
4207 ***   relative offset of local header 4 bytes
4208 */
4209 typedef struct ZipfileCDS ZipfileCDS;
4210 struct ZipfileCDS {
4211   u16 iVersionMadeBy;
4212   u16 iVersionExtract;
4213   u16 flags;
4214   u16 iCompression;
4215   u16 mTime;
4216   u16 mDate;
4217   u32 crc32;
4218   u32 szCompressed;
4219   u32 szUncompressed;
4220   u16 nFile;
4221   u16 nExtra;
4222   u16 nComment;
4223   u16 iDiskStart;
4224   u16 iInternalAttr;
4225   u32 iExternalAttr;
4226   u32 iOffset;
4227   char *zFile;                    /* Filename (sqlite3_malloc()) */
4228 };
4229
4230 /*
4231 *** 4.3.7  Local file header:
4232 ***
4233 ***   local file header signature     4 bytes  (0x04034b50)
4234 ***   version needed to extract       2 bytes
4235 ***   general purpose bit flag        2 bytes
4236 ***   compression method              2 bytes
4237 ***   last mod file time              2 bytes
4238 ***   last mod file date              2 bytes
4239 ***   crc-32                          4 bytes
4240 ***   compressed size                 4 bytes
4241 ***   uncompressed size               4 bytes
4242 ***   file name length                2 bytes
4243 ***   extra field length              2 bytes
4244 ***   
4245 */
4246 typedef struct ZipfileLFH ZipfileLFH;
4247 struct ZipfileLFH {
4248   u16 iVersionExtract;
4249   u16 flags;
4250   u16 iCompression;
4251   u16 mTime;
4252   u16 mDate;
4253   u32 crc32;
4254   u32 szCompressed;
4255   u32 szUncompressed;
4256   u16 nFile;
4257   u16 nExtra;
4258 };
4259
4260 typedef struct ZipfileEntry ZipfileEntry;
4261 struct ZipfileEntry {
4262   ZipfileCDS cds;            /* Parsed CDS record */
4263   u32 mUnixTime;             /* Modification time, in UNIX format */
4264   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
4265   i64 iDataOff;              /* Offset to data in file (if aData==0) */
4266   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
4267   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
4268 };
4269
4270 /* 
4271 ** Cursor type for zipfile tables.
4272 */
4273 typedef struct ZipfileCsr ZipfileCsr;
4274 struct ZipfileCsr {
4275   sqlite3_vtab_cursor base;  /* Base class - must be first */
4276   i64 iId;                   /* Cursor ID */
4277   u8 bEof;                   /* True when at EOF */
4278   u8 bNoop;                  /* If next xNext() call is no-op */
4279
4280   /* Used outside of write transactions */
4281   FILE *pFile;               /* Zip file */
4282   i64 iNextOff;              /* Offset of next record in central directory */
4283   ZipfileEOCD eocd;          /* Parse of central directory record */
4284
4285   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
4286   ZipfileEntry *pCurrent;    /* Current entry */
4287   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
4288 };
4289
4290 typedef struct ZipfileTab ZipfileTab;
4291 struct ZipfileTab {
4292   sqlite3_vtab base;         /* Base class - must be first */
4293   char *zFile;               /* Zip file this table accesses (may be NULL) */
4294   sqlite3 *db;               /* Host database connection */
4295   u8 *aBuffer;               /* Temporary buffer used for various tasks */
4296
4297   ZipfileCsr *pCsrList;      /* List of cursors */
4298   i64 iNextCsrid;
4299
4300   /* The following are used by write transactions only */
4301   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4302   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
4303   FILE *pWriteFd;            /* File handle open on zip archive */
4304   i64 szCurrent;             /* Current size of zip archive */
4305   i64 szOrig;                /* Size of archive at start of transaction */
4306 };
4307
4308 /*
4309 ** Set the error message contained in context ctx to the results of
4310 ** vprintf(zFmt, ...).
4311 */
4312 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4313   char *zMsg = 0;
4314   va_list ap;
4315   va_start(ap, zFmt);
4316   zMsg = sqlite3_vmprintf(zFmt, ap);
4317   sqlite3_result_error(ctx, zMsg, -1);
4318   sqlite3_free(zMsg);
4319   va_end(ap);
4320 }
4321
4322 /*
4323 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4324 ** is not quoted, do nothing.
4325 */
4326 static void zipfileDequote(char *zIn){
4327   char q = zIn[0];
4328   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4329     int iIn = 1;
4330     int iOut = 0;
4331     if( q=='[' ) q = ']';
4332     while( ALWAYS(zIn[iIn]) ){
4333       char c = zIn[iIn++];
4334       if( c==q && zIn[iIn++]!=q ) break;
4335       zIn[iOut++] = c;
4336     }
4337     zIn[iOut] = '\0';
4338   }
4339 }
4340
4341 /*
4342 ** Construct a new ZipfileTab virtual table object.
4343 ** 
4344 **   argv[0]   -> module name  ("zipfile")
4345 **   argv[1]   -> database name
4346 **   argv[2]   -> table name
4347 **   argv[...] -> "column name" and other module argument fields.
4348 */
4349 static int zipfileConnect(
4350   sqlite3 *db,
4351   void *pAux,
4352   int argc, const char *const*argv,
4353   sqlite3_vtab **ppVtab,
4354   char **pzErr
4355 ){
4356   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4357   int nFile = 0;
4358   const char *zFile = 0;
4359   ZipfileTab *pNew = 0;
4360   int rc;
4361
4362   /* If the table name is not "zipfile", require that the argument be
4363   ** specified. This stops zipfile tables from being created as:
4364   **
4365   **   CREATE VIRTUAL TABLE zzz USING zipfile();
4366   **
4367   ** It does not prevent:
4368   **
4369   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
4370   */
4371   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4372   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4373     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4374     return SQLITE_ERROR;
4375   }
4376
4377   if( argc>3 ){
4378     zFile = argv[3];
4379     nFile = (int)strlen(zFile)+1;
4380   }
4381
4382   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4383   if( rc==SQLITE_OK ){
4384     pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
4385     if( pNew==0 ) return SQLITE_NOMEM;
4386     memset(pNew, 0, nByte+nFile);
4387     pNew->db = db;
4388     pNew->aBuffer = (u8*)&pNew[1];
4389     if( zFile ){
4390       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4391       memcpy(pNew->zFile, zFile, nFile);
4392       zipfileDequote(pNew->zFile);
4393     }
4394   }
4395   *ppVtab = (sqlite3_vtab*)pNew;
4396   return rc;
4397 }
4398
4399 /*
4400 ** Free the ZipfileEntry structure indicated by the only argument.
4401 */
4402 static void zipfileEntryFree(ZipfileEntry *p){
4403   if( p ){
4404     sqlite3_free(p->cds.zFile);
4405     sqlite3_free(p);
4406   }
4407 }
4408
4409 /*
4410 ** Release resources that should be freed at the end of a write 
4411 ** transaction.
4412 */
4413 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4414   ZipfileEntry *pEntry;
4415   ZipfileEntry *pNext;
4416
4417   if( pTab->pWriteFd ){
4418     fclose(pTab->pWriteFd);
4419     pTab->pWriteFd = 0;
4420   }
4421   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4422     pNext = pEntry->pNext;
4423     zipfileEntryFree(pEntry);
4424   }
4425   pTab->pFirstEntry = 0;
4426   pTab->pLastEntry = 0;
4427   pTab->szCurrent = 0;
4428   pTab->szOrig = 0;
4429 }
4430
4431 /*
4432 ** This method is the destructor for zipfile vtab objects.
4433 */
4434 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4435   zipfileCleanupTransaction((ZipfileTab*)pVtab);
4436   sqlite3_free(pVtab);
4437   return SQLITE_OK;
4438 }
4439
4440 /*
4441 ** Constructor for a new ZipfileCsr object.
4442 */
4443 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4444   ZipfileTab *pTab = (ZipfileTab*)p;
4445   ZipfileCsr *pCsr;
4446   pCsr = sqlite3_malloc(sizeof(*pCsr));
4447   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4448   if( pCsr==0 ){
4449     return SQLITE_NOMEM;
4450   }
4451   memset(pCsr, 0, sizeof(*pCsr));
4452   pCsr->iId = ++pTab->iNextCsrid;
4453   pCsr->pCsrNext = pTab->pCsrList;
4454   pTab->pCsrList = pCsr;
4455   return SQLITE_OK;
4456 }
4457
4458 /*
4459 ** Reset a cursor back to the state it was in when first returned
4460 ** by zipfileOpen().
4461 */
4462 static void zipfileResetCursor(ZipfileCsr *pCsr){
4463   ZipfileEntry *p;
4464   ZipfileEntry *pNext;
4465
4466   pCsr->bEof = 0;
4467   if( pCsr->pFile ){
4468     fclose(pCsr->pFile);
4469     pCsr->pFile = 0;
4470     zipfileEntryFree(pCsr->pCurrent);
4471     pCsr->pCurrent = 0;
4472   }
4473
4474   for(p=pCsr->pFreeEntry; p; p=pNext){
4475     pNext = p->pNext;
4476     zipfileEntryFree(p);
4477   }
4478 }
4479
4480 /*
4481 ** Destructor for an ZipfileCsr.
4482 */
4483 static int zipfileClose(sqlite3_vtab_cursor *cur){
4484   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4485   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4486   ZipfileCsr **pp;
4487   zipfileResetCursor(pCsr);
4488
4489   /* Remove this cursor from the ZipfileTab.pCsrList list. */
4490   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4491   *pp = pCsr->pCsrNext;
4492
4493   sqlite3_free(pCsr);
4494   return SQLITE_OK;
4495 }
4496
4497 /*
4498 ** Set the error message for the virtual table associated with cursor
4499 ** pCsr to the results of vprintf(zFmt, ...).
4500 */
4501 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4502   va_list ap;
4503   va_start(ap, zFmt);
4504   sqlite3_free(pTab->base.zErrMsg);
4505   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4506   va_end(ap);
4507 }
4508 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4509   va_list ap;
4510   va_start(ap, zFmt);
4511   sqlite3_free(pCsr->base.pVtab->zErrMsg);
4512   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4513   va_end(ap);
4514 }
4515
4516 /*
4517 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4518 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4519 ** otherwise. 
4520 **
4521 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4522 ** to an English language error message. It is the responsibility of the
4523 ** caller to eventually free this buffer using
4524 ** sqlite3_free().
4525 */
4526 static int zipfileReadData(
4527   FILE *pFile,                    /* Read from this file */
4528   u8 *aRead,                      /* Read into this buffer */
4529   int nRead,                      /* Number of bytes to read */
4530   i64 iOff,                       /* Offset to read from */
4531   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
4532 ){
4533   size_t n;
4534   fseek(pFile, (long)iOff, SEEK_SET);
4535   n = fread(aRead, 1, nRead, pFile);
4536   if( (int)n!=nRead ){
4537     *pzErrmsg = sqlite3_mprintf("error in fread()");
4538     return SQLITE_ERROR;
4539   }
4540   return SQLITE_OK;
4541 }
4542
4543 static int zipfileAppendData(
4544   ZipfileTab *pTab,
4545   const u8 *aWrite,
4546   int nWrite
4547 ){
4548   size_t n;
4549   fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4550   n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4551   if( (int)n!=nWrite ){
4552     pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4553     return SQLITE_ERROR;
4554   }
4555   pTab->szCurrent += nWrite;
4556   return SQLITE_OK;
4557 }
4558
4559 /*
4560 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4561 */
4562 static u16 zipfileGetU16(const u8 *aBuf){
4563   return (aBuf[1] << 8) + aBuf[0];
4564 }
4565
4566 /*
4567 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4568 */
4569 static u32 zipfileGetU32(const u8 *aBuf){
4570   return ((u32)(aBuf[3]) << 24)
4571        + ((u32)(aBuf[2]) << 16)
4572        + ((u32)(aBuf[1]) <<  8)
4573        + ((u32)(aBuf[0]) <<  0);
4574 }
4575
4576 /*
4577 ** Write a 16-bit little endiate integer into buffer aBuf.
4578 */
4579 static void zipfilePutU16(u8 *aBuf, u16 val){
4580   aBuf[0] = val & 0xFF;
4581   aBuf[1] = (val>>8) & 0xFF;
4582 }
4583
4584 /*
4585 ** Write a 32-bit little endiate integer into buffer aBuf.
4586 */
4587 static void zipfilePutU32(u8 *aBuf, u32 val){
4588   aBuf[0] = val & 0xFF;
4589   aBuf[1] = (val>>8) & 0xFF;
4590   aBuf[2] = (val>>16) & 0xFF;
4591   aBuf[3] = (val>>24) & 0xFF;
4592 }
4593
4594 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4595 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4596
4597 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4598 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4599
4600 /*
4601 ** Magic numbers used to read CDS records.
4602 */
4603 #define ZIPFILE_CDS_NFILE_OFF        28
4604 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4605
4606 /*
4607 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4608 ** if the record is not well-formed, or SQLITE_OK otherwise.
4609 */
4610 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4611   u8 *aRead = aBuf;
4612   u32 sig = zipfileRead32(aRead);
4613   int rc = SQLITE_OK;
4614   if( sig!=ZIPFILE_SIGNATURE_CDS ){
4615     rc = SQLITE_ERROR;
4616   }else{
4617     pCDS->iVersionMadeBy = zipfileRead16(aRead);
4618     pCDS->iVersionExtract = zipfileRead16(aRead);
4619     pCDS->flags = zipfileRead16(aRead);
4620     pCDS->iCompression = zipfileRead16(aRead);
4621     pCDS->mTime = zipfileRead16(aRead);
4622     pCDS->mDate = zipfileRead16(aRead);
4623     pCDS->crc32 = zipfileRead32(aRead);
4624     pCDS->szCompressed = zipfileRead32(aRead);
4625     pCDS->szUncompressed = zipfileRead32(aRead);
4626     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4627     pCDS->nFile = zipfileRead16(aRead);
4628     pCDS->nExtra = zipfileRead16(aRead);
4629     pCDS->nComment = zipfileRead16(aRead);
4630     pCDS->iDiskStart = zipfileRead16(aRead);
4631     pCDS->iInternalAttr = zipfileRead16(aRead);
4632     pCDS->iExternalAttr = zipfileRead32(aRead);
4633     pCDS->iOffset = zipfileRead32(aRead);
4634     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4635   }
4636
4637   return rc;
4638 }
4639
4640 /*
4641 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4642 ** if the record is not well-formed, or SQLITE_OK otherwise.
4643 */
4644 static int zipfileReadLFH(
4645   u8 *aBuffer,
4646   ZipfileLFH *pLFH
4647 ){
4648   u8 *aRead = aBuffer;
4649   int rc = SQLITE_OK;
4650
4651   u32 sig = zipfileRead32(aRead);
4652   if( sig!=ZIPFILE_SIGNATURE_LFH ){
4653     rc = SQLITE_ERROR;
4654   }else{
4655     pLFH->iVersionExtract = zipfileRead16(aRead);
4656     pLFH->flags = zipfileRead16(aRead);
4657     pLFH->iCompression = zipfileRead16(aRead);
4658     pLFH->mTime = zipfileRead16(aRead);
4659     pLFH->mDate = zipfileRead16(aRead);
4660     pLFH->crc32 = zipfileRead32(aRead);
4661     pLFH->szCompressed = zipfileRead32(aRead);
4662     pLFH->szUncompressed = zipfileRead32(aRead);
4663     pLFH->nFile = zipfileRead16(aRead);
4664     pLFH->nExtra = zipfileRead16(aRead);
4665   }
4666   return rc;
4667 }
4668
4669
4670 /*
4671 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4672 ** Scan through this buffer to find an "extra-timestamp" field. If one
4673 ** exists, extract the 32-bit modification-timestamp from it and store
4674 ** the value in output parameter *pmTime.
4675 **
4676 ** Zero is returned if no extra-timestamp record could be found (and so
4677 ** *pmTime is left unchanged), or non-zero otherwise.
4678 **
4679 ** The general format of an extra field is:
4680 **
4681 **   Header ID    2 bytes
4682 **   Data Size    2 bytes
4683 **   Data         N bytes
4684 */
4685 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4686   int ret = 0;
4687   u8 *p = aExtra;
4688   u8 *pEnd = &aExtra[nExtra];
4689
4690   while( p<pEnd ){
4691     u16 id = zipfileRead16(p);
4692     u16 nByte = zipfileRead16(p);
4693
4694     switch( id ){
4695       case ZIPFILE_EXTRA_TIMESTAMP: {
4696         u8 b = p[0];
4697         if( b & 0x01 ){     /* 0x01 -> modtime is present */
4698           *pmTime = zipfileGetU32(&p[1]);
4699           ret = 1;
4700         }
4701         break;
4702       }
4703     }
4704
4705     p += nByte;
4706   }
4707   return ret;
4708 }
4709
4710 /*
4711 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4712 ** fields of the CDS structure passed as the only argument to a 32-bit
4713 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4714 **
4715 ** "Standard" MS-DOS time format:
4716 **
4717 **   File modification time:
4718 **     Bits 00-04: seconds divided by 2
4719 **     Bits 05-10: minute
4720 **     Bits 11-15: hour
4721 **   File modification date:
4722 **     Bits 00-04: day
4723 **     Bits 05-08: month (1-12)
4724 **     Bits 09-15: years from 1980 
4725 **
4726 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4727 */
4728 static u32 zipfileMtime(ZipfileCDS *pCDS){
4729   int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4730   int M = ((pCDS->mDate >> 5) & 0x0F);
4731   int D = (pCDS->mDate & 0x1F);
4732   int B = -13;
4733
4734   int sec = (pCDS->mTime & 0x1F)*2;
4735   int min = (pCDS->mTime >> 5) & 0x3F;
4736   int hr = (pCDS->mTime >> 11) & 0x1F;
4737   i64 JD;
4738
4739   /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4740
4741   /* Calculate the JD in seconds for noon on the day in question */
4742   if( M<3 ){
4743     Y = Y-1;
4744     M = M+12;
4745   }
4746   JD = (i64)(24*60*60) * (
4747       (int)(365.25 * (Y + 4716))
4748     + (int)(30.6001 * (M + 1))
4749     + D + B - 1524
4750   );
4751
4752   /* Correct the JD for the time within the day */
4753   JD += (hr-12) * 3600 + min * 60 + sec;
4754
4755   /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4756   return (u32)(JD - (i64)(24405875) * 24*60*6);
4757 }
4758
4759 /*
4760 ** The opposite of zipfileMtime(). This function populates the mTime and
4761 ** mDate fields of the CDS structure passed as the first argument according
4762 ** to the UNIX timestamp value passed as the second.
4763 */
4764 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4765   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4766   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4767
4768   int A, B, C, D, E;
4769   int yr, mon, day;
4770   int hr, min, sec;
4771
4772   A = (int)((JD - 1867216.25)/36524.25);
4773   A = (int)(JD + 1 + A - (A/4));
4774   B = A + 1524;
4775   C = (int)((B - 122.1)/365.25);
4776   D = (36525*(C&32767))/100;
4777   E = (int)((B-D)/30.6001);
4778
4779   day = B - D - (int)(30.6001*E);
4780   mon = (E<14 ? E-1 : E-13);
4781   yr = mon>2 ? C-4716 : C-4715;
4782
4783   hr = (mUnixTime % (24*60*60)) / (60*60);
4784   min = (mUnixTime % (60*60)) / 60;
4785   sec = (mUnixTime % 60);
4786
4787   if( yr>=1980 ){
4788     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4789     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4790   }else{
4791     pCds->mDate = pCds->mTime = 0;
4792   }
4793
4794   assert( mUnixTime<315507600 
4795        || mUnixTime==zipfileMtime(pCds) 
4796        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
4797        /* || (mUnixTime % 2) */
4798   );
4799 }
4800
4801 /*
4802 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4803 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4804 ** then pFile is a file-handle open on a zip file. In either case, this
4805 ** function creates a ZipfileEntry object based on the zip archive entry
4806 ** for which the CDS record is at offset iOff.
4807 **
4808 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4809 ** the new object. Otherwise, an SQLite error code is returned and the
4810 ** final value of (*ppEntry) undefined.
4811 */
4812 static int zipfileGetEntry(
4813   ZipfileTab *pTab,               /* Store any error message here */
4814   const u8 *aBlob,                /* Pointer to in-memory file image */
4815   int nBlob,                      /* Size of aBlob[] in bytes */
4816   FILE *pFile,                    /* If aBlob==0, read from this file */
4817   i64 iOff,                       /* Offset of CDS record */
4818   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
4819 ){
4820   u8 *aRead;
4821   char **pzErr = &pTab->base.zErrMsg;
4822   int rc = SQLITE_OK;
4823
4824   if( aBlob==0 ){
4825     aRead = pTab->aBuffer;
4826     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
4827   }else{
4828     aRead = (u8*)&aBlob[iOff];
4829   }
4830
4831   if( rc==SQLITE_OK ){
4832     int nAlloc;
4833     ZipfileEntry *pNew;
4834
4835     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
4836     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
4837     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
4838
4839     nAlloc = sizeof(ZipfileEntry) + nExtra;
4840     if( aBlob ){
4841       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
4842     }
4843
4844     pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
4845     if( pNew==0 ){
4846       rc = SQLITE_NOMEM;
4847     }else{
4848       memset(pNew, 0, sizeof(ZipfileEntry));
4849       rc = zipfileReadCDS(aRead, &pNew->cds);
4850       if( rc!=SQLITE_OK ){
4851         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
4852       }else if( aBlob==0 ){
4853         rc = zipfileReadData(
4854             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
4855         );
4856       }else{
4857         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
4858       }
4859     }
4860
4861     if( rc==SQLITE_OK ){
4862       u32 *pt = &pNew->mUnixTime;
4863       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
4864       pNew->aExtra = (u8*)&pNew[1];
4865       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
4866       if( pNew->cds.zFile==0 ){
4867         rc = SQLITE_NOMEM;
4868       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
4869         pNew->mUnixTime = zipfileMtime(&pNew->cds);
4870       }
4871     }
4872
4873     if( rc==SQLITE_OK ){
4874       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4875       ZipfileLFH lfh;
4876       if( pFile ){
4877         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
4878       }else{
4879         aRead = (u8*)&aBlob[pNew->cds.iOffset];
4880       }
4881
4882       rc = zipfileReadLFH(aRead, &lfh);
4883       if( rc==SQLITE_OK ){
4884         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4885         pNew->iDataOff += lfh.nFile + lfh.nExtra;
4886         if( aBlob && pNew->cds.szCompressed ){
4887           pNew->aData = &pNew->aExtra[nExtra];
4888           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
4889         }
4890       }else{
4891         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
4892             (int)pNew->cds.iOffset
4893         );
4894       }
4895     }
4896
4897     if( rc!=SQLITE_OK ){
4898       zipfileEntryFree(pNew);
4899     }else{
4900       *ppEntry = pNew;
4901     }
4902   }
4903
4904   return rc;
4905 }
4906
4907 /*
4908 ** Advance an ZipfileCsr to its next row of output.
4909 */
4910 static int zipfileNext(sqlite3_vtab_cursor *cur){
4911   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4912   int rc = SQLITE_OK;
4913
4914   if( pCsr->pFile ){
4915     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4916     zipfileEntryFree(pCsr->pCurrent);
4917     pCsr->pCurrent = 0;
4918     if( pCsr->iNextOff>=iEof ){
4919       pCsr->bEof = 1;
4920     }else{
4921       ZipfileEntry *p = 0;
4922       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
4923       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
4924       if( rc==SQLITE_OK ){
4925         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4926         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
4927       }
4928       pCsr->pCurrent = p;
4929     }
4930   }else{
4931     if( !pCsr->bNoop ){
4932       pCsr->pCurrent = pCsr->pCurrent->pNext;
4933     }
4934     if( pCsr->pCurrent==0 ){
4935       pCsr->bEof = 1;
4936     }
4937   }
4938
4939   pCsr->bNoop = 0;
4940   return rc;
4941 }
4942
4943 static void zipfileFree(void *p) { 
4944   sqlite3_free(p); 
4945 }
4946
4947 /*
4948 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
4949 ** size is nOut bytes. This function uncompresses the data and sets the
4950 ** return value in context pCtx to the result (a blob).
4951 **
4952 ** If an error occurs, an error code is left in pCtx instead.
4953 */
4954 static void zipfileInflate(
4955   sqlite3_context *pCtx,          /* Store result here */
4956   const u8 *aIn,                  /* Compressed data */
4957   int nIn,                        /* Size of buffer aIn[] in bytes */
4958   int nOut                        /* Expected output size */
4959 ){
4960   u8 *aRes = sqlite3_malloc(nOut);
4961   if( aRes==0 ){
4962     sqlite3_result_error_nomem(pCtx);
4963   }else{
4964     int err;
4965     z_stream str;
4966     memset(&str, 0, sizeof(str));
4967
4968     str.next_in = (Byte*)aIn;
4969     str.avail_in = nIn;
4970     str.next_out = (Byte*)aRes;
4971     str.avail_out = nOut;
4972
4973     err = inflateInit2(&str, -15);
4974     if( err!=Z_OK ){
4975       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
4976     }else{
4977       err = inflate(&str, Z_NO_FLUSH);
4978       if( err!=Z_STREAM_END ){
4979         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
4980       }else{
4981         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
4982         aRes = 0;
4983       }
4984     }
4985     sqlite3_free(aRes);
4986     inflateEnd(&str);
4987   }
4988 }
4989
4990 /*
4991 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
4992 ** compresses it and sets (*ppOut) to point to a buffer containing the
4993 ** compressed data. The caller is responsible for eventually calling
4994 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
4995 ** is set to the size of buffer (*ppOut) in bytes.
4996 **
4997 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
4998 ** code is returned and an error message left in virtual-table handle
4999 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5000 ** case.
5001 */
5002 static int zipfileDeflate(
5003   const u8 *aIn, int nIn,         /* Input */
5004   u8 **ppOut, int *pnOut,         /* Output */
5005   char **pzErr                    /* OUT: Error message */
5006 ){
5007   int nAlloc = (int)compressBound(nIn);
5008   u8 *aOut;
5009   int rc = SQLITE_OK;
5010
5011   aOut = (u8*)sqlite3_malloc(nAlloc);
5012   if( aOut==0 ){
5013     rc = SQLITE_NOMEM;
5014   }else{
5015     int res;
5016     z_stream str;
5017     memset(&str, 0, sizeof(str));
5018     str.next_in = (Bytef*)aIn;
5019     str.avail_in = nIn;
5020     str.next_out = aOut;
5021     str.avail_out = nAlloc;
5022
5023     deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5024     res = deflate(&str, Z_FINISH);
5025
5026     if( res==Z_STREAM_END ){
5027       *ppOut = aOut;
5028       *pnOut = (int)str.total_out;
5029     }else{
5030       sqlite3_free(aOut);
5031       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5032       rc = SQLITE_ERROR;
5033     }
5034     deflateEnd(&str);
5035   }
5036
5037   return rc;
5038 }
5039
5040
5041 /*
5042 ** Return values of columns for the row at which the series_cursor
5043 ** is currently pointing.
5044 */
5045 static int zipfileColumn(
5046   sqlite3_vtab_cursor *cur,   /* The cursor */
5047   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5048   int i                       /* Which column to return */
5049 ){
5050   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5051   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5052   int rc = SQLITE_OK;
5053   switch( i ){
5054     case 0:   /* name */
5055       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5056       break;
5057     case 1:   /* mode */
5058       /* TODO: Whether or not the following is correct surely depends on
5059       ** the platform on which the archive was created.  */
5060       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5061       break;
5062     case 2: { /* mtime */
5063       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5064       break;
5065     }
5066     case 3: { /* sz */
5067       if( sqlite3_vtab_nochange(ctx)==0 ){
5068         sqlite3_result_int64(ctx, pCDS->szUncompressed);
5069       }
5070       break;
5071     }
5072     case 4:   /* rawdata */
5073       if( sqlite3_vtab_nochange(ctx) ) break;
5074     case 5: { /* data */
5075       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5076         int sz = pCDS->szCompressed;
5077         int szFinal = pCDS->szUncompressed;
5078         if( szFinal>0 ){
5079           u8 *aBuf;
5080           u8 *aFree = 0;
5081           if( pCsr->pCurrent->aData ){
5082             aBuf = pCsr->pCurrent->aData;
5083           }else{
5084             aBuf = aFree = sqlite3_malloc(sz);
5085             if( aBuf==0 ){
5086               rc = SQLITE_NOMEM;
5087             }else{
5088               FILE *pFile = pCsr->pFile;
5089               if( pFile==0 ){
5090                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5091               }
5092               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5093                   &pCsr->base.pVtab->zErrMsg
5094               );
5095             }
5096           }
5097           if( rc==SQLITE_OK ){
5098             if( i==5 && pCDS->iCompression ){
5099               zipfileInflate(ctx, aBuf, sz, szFinal);
5100             }else{
5101               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5102             }
5103           }
5104           sqlite3_free(aFree);
5105         }else{
5106           /* Figure out if this is a directory or a zero-sized file. Consider
5107           ** it to be a directory either if the mode suggests so, or if
5108           ** the final character in the name is '/'.  */
5109           u32 mode = pCDS->iExternalAttr >> 16;
5110           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5111             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5112           }
5113         }
5114       }
5115       break;
5116     }
5117     case 6:   /* method */
5118       sqlite3_result_int(ctx, pCDS->iCompression);
5119       break;
5120     default:  /* z */
5121       assert( i==7 );
5122       sqlite3_result_int64(ctx, pCsr->iId);
5123       break;
5124   }
5125
5126   return rc;
5127 }
5128
5129 /*
5130 ** Return TRUE if the cursor is at EOF.
5131 */
5132 static int zipfileEof(sqlite3_vtab_cursor *cur){
5133   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5134   return pCsr->bEof;
5135 }
5136
5137 /*
5138 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5139 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5140 ** is guaranteed to be a file-handle open on a zip file.
5141 **
5142 ** This function attempts to locate the EOCD record within the zip archive
5143 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5144 ** returned if successful. Otherwise, an SQLite error code is returned and
5145 ** an English language error message may be left in virtual-table pTab.
5146 */
5147 static int zipfileReadEOCD(
5148   ZipfileTab *pTab,               /* Return errors here */
5149   const u8 *aBlob,                /* Pointer to in-memory file image */
5150   int nBlob,                      /* Size of aBlob[] in bytes */
5151   FILE *pFile,                    /* Read from this file if aBlob==0 */
5152   ZipfileEOCD *pEOCD              /* Object to populate */
5153 ){
5154   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
5155   int nRead;                      /* Bytes to read from file */
5156   int rc = SQLITE_OK;
5157
5158   if( aBlob==0 ){
5159     i64 iOff;                     /* Offset to read from */
5160     i64 szFile;                   /* Total size of file in bytes */
5161     fseek(pFile, 0, SEEK_END);
5162     szFile = (i64)ftell(pFile);
5163     if( szFile==0 ){
5164       memset(pEOCD, 0, sizeof(ZipfileEOCD));
5165       return SQLITE_OK;
5166     }
5167     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5168     iOff = szFile - nRead;
5169     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5170   }else{
5171     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5172     aRead = (u8*)&aBlob[nBlob-nRead];
5173   }
5174
5175   if( rc==SQLITE_OK ){
5176     int i;
5177
5178     /* Scan backwards looking for the signature bytes */
5179     for(i=nRead-20; i>=0; i--){
5180       if( aRead[i]==0x50 && aRead[i+1]==0x4b 
5181        && aRead[i+2]==0x05 && aRead[i+3]==0x06 
5182       ){
5183         break;
5184       }
5185     }
5186     if( i<0 ){
5187       pTab->base.zErrMsg = sqlite3_mprintf(
5188           "cannot find end of central directory record"
5189       );
5190       return SQLITE_ERROR;
5191     }
5192
5193     aRead += i+4;
5194     pEOCD->iDisk = zipfileRead16(aRead);
5195     pEOCD->iFirstDisk = zipfileRead16(aRead);
5196     pEOCD->nEntry = zipfileRead16(aRead);
5197     pEOCD->nEntryTotal = zipfileRead16(aRead);
5198     pEOCD->nSize = zipfileRead32(aRead);
5199     pEOCD->iOffset = zipfileRead32(aRead);
5200   }
5201
5202   return rc;
5203 }
5204
5205 /*
5206 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
5207 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5208 ** to the end of the list. Otherwise, it is added to the list immediately
5209 ** before pBefore (which is guaranteed to be a part of said list).
5210 */
5211 static void zipfileAddEntry(
5212   ZipfileTab *pTab, 
5213   ZipfileEntry *pBefore, 
5214   ZipfileEntry *pNew
5215 ){
5216   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5217   assert( pNew->pNext==0 );
5218   if( pBefore==0 ){
5219     if( pTab->pFirstEntry==0 ){
5220       pTab->pFirstEntry = pTab->pLastEntry = pNew;
5221     }else{
5222       assert( pTab->pLastEntry->pNext==0 );
5223       pTab->pLastEntry->pNext = pNew;
5224       pTab->pLastEntry = pNew;
5225     }
5226   }else{
5227     ZipfileEntry **pp;
5228     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5229     pNew->pNext = pBefore;
5230     *pp = pNew;
5231   }
5232 }
5233
5234 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5235   ZipfileEOCD eocd;
5236   int rc;
5237   int i;
5238   i64 iOff;
5239
5240   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5241   iOff = eocd.iOffset;
5242   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5243     ZipfileEntry *pNew = 0;
5244     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5245
5246     if( rc==SQLITE_OK ){
5247       zipfileAddEntry(pTab, 0, pNew);
5248       iOff += ZIPFILE_CDS_FIXED_SZ;
5249       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5250     }
5251   }
5252   return rc;
5253 }
5254
5255 /*
5256 ** xFilter callback.
5257 */
5258 static int zipfileFilter(
5259   sqlite3_vtab_cursor *cur, 
5260   int idxNum, const char *idxStr,
5261   int argc, sqlite3_value **argv
5262 ){
5263   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5264   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5265   const char *zFile = 0;          /* Zip file to scan */
5266   int rc = SQLITE_OK;             /* Return Code */
5267   int bInMemory = 0;              /* True for an in-memory zipfile */
5268
5269   zipfileResetCursor(pCsr);
5270
5271   if( pTab->zFile ){
5272     zFile = pTab->zFile;
5273   }else if( idxNum==0 ){
5274     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5275     return SQLITE_ERROR;
5276   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5277     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5278     int nBlob = sqlite3_value_bytes(argv[0]);
5279     assert( pTab->pFirstEntry==0 );
5280     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5281     pCsr->pFreeEntry = pTab->pFirstEntry;
5282     pTab->pFirstEntry = pTab->pLastEntry = 0;
5283     if( rc!=SQLITE_OK ) return rc;
5284     bInMemory = 1;
5285   }else{
5286     zFile = (const char*)sqlite3_value_text(argv[0]);
5287   }
5288
5289   if( 0==pTab->pWriteFd && 0==bInMemory ){
5290     pCsr->pFile = fopen(zFile, "rb");
5291     if( pCsr->pFile==0 ){
5292       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5293       rc = SQLITE_ERROR;
5294     }else{
5295       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5296       if( rc==SQLITE_OK ){
5297         if( pCsr->eocd.nEntry==0 ){
5298           pCsr->bEof = 1;
5299         }else{
5300           pCsr->iNextOff = pCsr->eocd.iOffset;
5301           rc = zipfileNext(cur);
5302         }
5303       }
5304     }
5305   }else{
5306     pCsr->bNoop = 1;
5307     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5308     rc = zipfileNext(cur);
5309   }
5310
5311   return rc;
5312 }
5313
5314 /*
5315 ** xBestIndex callback.
5316 */
5317 static int zipfileBestIndex(
5318   sqlite3_vtab *tab,
5319   sqlite3_index_info *pIdxInfo
5320 ){
5321   int i;
5322
5323   for(i=0; i<pIdxInfo->nConstraint; i++){
5324     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5325     if( pCons->usable==0 ) continue;
5326     if( pCons->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
5327     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5328     break;
5329   }
5330
5331   if( i<pIdxInfo->nConstraint ){
5332     pIdxInfo->aConstraintUsage[i].argvIndex = 1;
5333     pIdxInfo->aConstraintUsage[i].omit = 1;
5334     pIdxInfo->estimatedCost = 1000.0;
5335     pIdxInfo->idxNum = 1;
5336   }else{
5337     pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
5338     pIdxInfo->idxNum = 0;
5339   }
5340
5341   return SQLITE_OK;
5342 }
5343
5344 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5345   ZipfileEntry *pNew;
5346   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5347   if( pNew ){
5348     memset(pNew, 0, sizeof(ZipfileEntry));
5349     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5350     if( pNew->cds.zFile==0 ){
5351       sqlite3_free(pNew);
5352       pNew = 0;
5353     }
5354   }
5355   return pNew;
5356 }
5357
5358 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5359   ZipfileCDS *pCds = &pEntry->cds;
5360   u8 *a = aBuf;
5361
5362   pCds->nExtra = 9;
5363
5364   /* Write the LFH itself */
5365   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5366   zipfileWrite16(a, pCds->iVersionExtract);
5367   zipfileWrite16(a, pCds->flags);
5368   zipfileWrite16(a, pCds->iCompression);
5369   zipfileWrite16(a, pCds->mTime);
5370   zipfileWrite16(a, pCds->mDate);
5371   zipfileWrite32(a, pCds->crc32);
5372   zipfileWrite32(a, pCds->szCompressed);
5373   zipfileWrite32(a, pCds->szUncompressed);
5374   zipfileWrite16(a, (u16)pCds->nFile);
5375   zipfileWrite16(a, pCds->nExtra);
5376   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5377
5378   /* Add the file name */
5379   memcpy(a, pCds->zFile, (int)pCds->nFile);
5380   a += (int)pCds->nFile;
5381
5382   /* The "extra" data */
5383   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5384   zipfileWrite16(a, 5);
5385   *a++ = 0x01;
5386   zipfileWrite32(a, pEntry->mUnixTime);
5387
5388   return a-aBuf;
5389 }
5390
5391 static int zipfileAppendEntry(
5392   ZipfileTab *pTab,
5393   ZipfileEntry *pEntry,
5394   const u8 *pData,
5395   int nData
5396 ){
5397   u8 *aBuf = pTab->aBuffer;
5398   int nBuf;
5399   int rc;
5400
5401   nBuf = zipfileSerializeLFH(pEntry, aBuf);
5402   rc = zipfileAppendData(pTab, aBuf, nBuf);
5403   if( rc==SQLITE_OK ){
5404     pEntry->iDataOff = pTab->szCurrent;
5405     rc = zipfileAppendData(pTab, pData, nData);
5406   }
5407
5408   return rc;
5409 }
5410
5411 static int zipfileGetMode(
5412   sqlite3_value *pVal, 
5413   int bIsDir,                     /* If true, default to directory */
5414   u32 *pMode,                     /* OUT: Mode value */
5415   char **pzErr                    /* OUT: Error message */
5416 ){
5417   const char *z = (const char*)sqlite3_value_text(pVal);
5418   u32 mode = 0;
5419   if( z==0 ){
5420     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5421   }else if( z[0]>='0' && z[0]<='9' ){
5422     mode = (unsigned int)sqlite3_value_int(pVal);
5423   }else{
5424     const char zTemplate[11] = "-rwxrwxrwx";
5425     int i;
5426     if( strlen(z)!=10 ) goto parse_error;
5427     switch( z[0] ){
5428       case '-': mode |= S_IFREG; break;
5429       case 'd': mode |= S_IFDIR; break;
5430       case 'l': mode |= S_IFLNK; break;
5431       default: goto parse_error;
5432     }
5433     for(i=1; i<10; i++){
5434       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5435       else if( z[i]!='-' ) goto parse_error;
5436     }
5437   }
5438   if( ((mode & S_IFDIR)==0)==bIsDir ){
5439     /* The "mode" attribute is a directory, but data has been specified.
5440     ** Or vice-versa - no data but "mode" is a file or symlink.  */
5441     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5442     return SQLITE_CONSTRAINT;
5443   }
5444   *pMode = mode;
5445   return SQLITE_OK;
5446
5447  parse_error:
5448   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5449   return SQLITE_ERROR;
5450 }
5451
5452 /*
5453 ** Both (const char*) arguments point to nul-terminated strings. Argument
5454 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5455 ** identical, ignoring any trailing '/' character in either path.  */
5456 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5457   int nA = (int)strlen(zA);
5458   if( zA[nA-1]=='/' ) nA--;
5459   if( zB[nB-1]=='/' ) nB--;
5460   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5461   return 1;
5462 }
5463
5464 static int zipfileBegin(sqlite3_vtab *pVtab){
5465   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5466   int rc = SQLITE_OK;
5467
5468   assert( pTab->pWriteFd==0 );
5469
5470   /* Open a write fd on the file. Also load the entire central directory
5471   ** structure into memory. During the transaction any new file data is 
5472   ** appended to the archive file, but the central directory is accumulated
5473   ** in main-memory until the transaction is committed.  */
5474   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5475   if( pTab->pWriteFd==0 ){
5476     pTab->base.zErrMsg = sqlite3_mprintf(
5477         "zipfile: failed to open file %s for writing", pTab->zFile
5478         );
5479     rc = SQLITE_ERROR;
5480   }else{
5481     fseek(pTab->pWriteFd, 0, SEEK_END);
5482     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5483     rc = zipfileLoadDirectory(pTab, 0, 0);
5484   }
5485
5486   if( rc!=SQLITE_OK ){
5487     zipfileCleanupTransaction(pTab);
5488   }
5489
5490   return rc;
5491 }
5492
5493 /*
5494 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5495 ** time(2)).
5496 */
5497 static u32 zipfileTime(void){
5498   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5499   u32 ret;
5500   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5501     i64 ms;
5502     pVfs->xCurrentTimeInt64(pVfs, &ms);
5503     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5504   }else{
5505     double day;
5506     pVfs->xCurrentTime(pVfs, &day);
5507     ret = (u32)((day - 2440587.5) * 86400);
5508   }
5509   return ret;
5510 }
5511
5512 /*
5513 ** Return a 32-bit timestamp in UNIX epoch format.
5514 **
5515 ** If the value passed as the only argument is either NULL or an SQL NULL,
5516 ** return the current time. Otherwise, return the value stored in (*pVal)
5517 ** cast to a 32-bit unsigned integer.
5518 */
5519 static u32 zipfileGetTime(sqlite3_value *pVal){
5520   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5521     return zipfileTime();
5522   }
5523   return (u32)sqlite3_value_int64(pVal);
5524 }
5525
5526 /*
5527 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5528 ** linked list.  Remove it from the list and free the object.
5529 */
5530 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5531   if( pOld ){
5532     ZipfileEntry **pp;
5533     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5534     *pp = (*pp)->pNext;
5535     zipfileEntryFree(pOld);
5536   }
5537 }
5538
5539 /*
5540 ** xUpdate method.
5541 */
5542 static int zipfileUpdate(
5543   sqlite3_vtab *pVtab, 
5544   int nVal, 
5545   sqlite3_value **apVal, 
5546   sqlite_int64 *pRowid
5547 ){
5548   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5549   int rc = SQLITE_OK;             /* Return Code */
5550   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
5551
5552   u32 mode = 0;                   /* Mode for new entry */
5553   u32 mTime = 0;                  /* Modification time for new entry */
5554   i64 sz = 0;                     /* Uncompressed size */
5555   const char *zPath = 0;          /* Path for new entry */
5556   int nPath = 0;                  /* strlen(zPath) */
5557   const u8 *pData = 0;            /* Pointer to buffer containing content */
5558   int nData = 0;                  /* Size of pData buffer in bytes */
5559   int iMethod = 0;                /* Compression method for new entry */
5560   u8 *pFree = 0;                  /* Free this */
5561   char *zFree = 0;                /* Also free this */
5562   ZipfileEntry *pOld = 0;
5563   ZipfileEntry *pOld2 = 0;
5564   int bUpdate = 0;                /* True for an update that modifies "name" */
5565   int bIsDir = 0;
5566   u32 iCrc32 = 0;
5567
5568   if( pTab->pWriteFd==0 ){
5569     rc = zipfileBegin(pVtab);
5570     if( rc!=SQLITE_OK ) return rc;
5571   }
5572
5573   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5574   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5575     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5576     int nDelete = (int)strlen(zDelete);
5577     if( nVal>1 ){
5578       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5579       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5580         bUpdate = 1;
5581       }
5582     }
5583     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5584       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5585         break;
5586       }
5587       assert( pOld->pNext );
5588     }
5589   }
5590
5591   if( nVal>1 ){
5592     /* Check that "sz" and "rawdata" are both NULL: */
5593     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5594       zipfileTableErr(pTab, "sz must be NULL");
5595       rc = SQLITE_CONSTRAINT;
5596     }
5597     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5598       zipfileTableErr(pTab, "rawdata must be NULL"); 
5599       rc = SQLITE_CONSTRAINT;
5600     }
5601
5602     if( rc==SQLITE_OK ){
5603       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5604         /* data=NULL. A directory */
5605         bIsDir = 1;
5606       }else{
5607         /* Value specified for "data", and possibly "method". This must be
5608         ** a regular file or a symlink. */
5609         const u8 *aIn = sqlite3_value_blob(apVal[7]);
5610         int nIn = sqlite3_value_bytes(apVal[7]);
5611         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5612
5613         iMethod = sqlite3_value_int(apVal[8]);
5614         sz = nIn;
5615         pData = aIn;
5616         nData = nIn;
5617         if( iMethod!=0 && iMethod!=8 ){
5618           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5619           rc = SQLITE_CONSTRAINT;
5620         }else{
5621           if( bAuto || iMethod ){
5622             int nCmp;
5623             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5624             if( rc==SQLITE_OK ){
5625               if( iMethod || nCmp<nIn ){
5626                 iMethod = 8;
5627                 pData = pFree;
5628                 nData = nCmp;
5629               }
5630             }
5631           }
5632           iCrc32 = crc32(0, aIn, nIn);
5633         }
5634       }
5635     }
5636
5637     if( rc==SQLITE_OK ){
5638       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5639     }
5640
5641     if( rc==SQLITE_OK ){
5642       zPath = (const char*)sqlite3_value_text(apVal[2]);
5643       nPath = (int)strlen(zPath);
5644       mTime = zipfileGetTime(apVal[4]);
5645     }
5646
5647     if( rc==SQLITE_OK && bIsDir ){
5648       /* For a directory, check that the last character in the path is a
5649       ** '/'. This appears to be required for compatibility with info-zip
5650       ** (the unzip command on unix). It does not create directories
5651       ** otherwise.  */
5652       if( zPath[nPath-1]!='/' ){
5653         zFree = sqlite3_mprintf("%s/", zPath);
5654         if( zFree==0 ){ rc = SQLITE_NOMEM; }
5655         zPath = (const char*)zFree;
5656         nPath++;
5657       }
5658     }
5659
5660     /* Check that we're not inserting a duplicate entry -OR- updating an
5661     ** entry with a path, thereby making it into a duplicate. */
5662     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
5663       ZipfileEntry *p;
5664       for(p=pTab->pFirstEntry; p; p=p->pNext){
5665         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5666           switch( sqlite3_vtab_on_conflict(pTab->db) ){
5667             case SQLITE_IGNORE: {
5668               goto zipfile_update_done;
5669             }
5670             case SQLITE_REPLACE: {
5671               pOld2 = p;
5672               break;
5673             }
5674             default: {
5675               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5676               rc = SQLITE_CONSTRAINT;
5677               break;
5678             }
5679           }
5680           break;
5681         }
5682       }
5683     }
5684
5685     if( rc==SQLITE_OK ){
5686       /* Create the new CDS record. */
5687       pNew = zipfileNewEntry(zPath);
5688       if( pNew==0 ){
5689         rc = SQLITE_NOMEM;
5690       }else{
5691         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5692         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5693         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5694         pNew->cds.iCompression = (u16)iMethod;
5695         zipfileMtimeToDos(&pNew->cds, mTime);
5696         pNew->cds.crc32 = iCrc32;
5697         pNew->cds.szCompressed = nData;
5698         pNew->cds.szUncompressed = (u32)sz;
5699         pNew->cds.iExternalAttr = (mode<<16);
5700         pNew->cds.iOffset = (u32)pTab->szCurrent;
5701         pNew->cds.nFile = (u16)nPath;
5702         pNew->mUnixTime = (u32)mTime;
5703         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5704         zipfileAddEntry(pTab, pOld, pNew);
5705       }
5706     }
5707   }
5708
5709   if( rc==SQLITE_OK && (pOld || pOld2) ){
5710     ZipfileCsr *pCsr;
5711     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5712       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
5713         pCsr->pCurrent = pCsr->pCurrent->pNext;
5714         pCsr->bNoop = 1;
5715       }
5716     }
5717
5718     zipfileRemoveEntryFromList(pTab, pOld);
5719     zipfileRemoveEntryFromList(pTab, pOld2);
5720   }
5721
5722 zipfile_update_done:
5723   sqlite3_free(pFree);
5724   sqlite3_free(zFree);
5725   return rc;
5726 }
5727
5728 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5729   u8 *a = aBuf;
5730   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5731   zipfileWrite16(a, p->iDisk);
5732   zipfileWrite16(a, p->iFirstDisk);
5733   zipfileWrite16(a, p->nEntry);
5734   zipfileWrite16(a, p->nEntryTotal);
5735   zipfileWrite32(a, p->nSize);
5736   zipfileWrite32(a, p->iOffset);
5737   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
5738
5739   return a-aBuf;
5740 }
5741
5742 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5743   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5744   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5745   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5746 }
5747
5748 /*
5749 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5750 ** of bytes written.
5751 */
5752 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5753   u8 *a = aBuf;
5754   ZipfileCDS *pCDS = &pEntry->cds;
5755
5756   if( pEntry->aExtra==0 ){
5757     pCDS->nExtra = 9;
5758   }
5759
5760   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5761   zipfileWrite16(a, pCDS->iVersionMadeBy);
5762   zipfileWrite16(a, pCDS->iVersionExtract);
5763   zipfileWrite16(a, pCDS->flags);
5764   zipfileWrite16(a, pCDS->iCompression);
5765   zipfileWrite16(a, pCDS->mTime);
5766   zipfileWrite16(a, pCDS->mDate);
5767   zipfileWrite32(a, pCDS->crc32);
5768   zipfileWrite32(a, pCDS->szCompressed);
5769   zipfileWrite32(a, pCDS->szUncompressed);
5770   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5771   zipfileWrite16(a, pCDS->nFile);
5772   zipfileWrite16(a, pCDS->nExtra);
5773   zipfileWrite16(a, pCDS->nComment);
5774   zipfileWrite16(a, pCDS->iDiskStart);
5775   zipfileWrite16(a, pCDS->iInternalAttr);
5776   zipfileWrite32(a, pCDS->iExternalAttr);
5777   zipfileWrite32(a, pCDS->iOffset);
5778
5779   memcpy(a, pCDS->zFile, pCDS->nFile);
5780   a += pCDS->nFile;
5781
5782   if( pEntry->aExtra ){
5783     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5784     memcpy(a, pEntry->aExtra, n);
5785     a += n;
5786   }else{
5787     assert( pCDS->nExtra==9 );
5788     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5789     zipfileWrite16(a, 5);
5790     *a++ = 0x01;
5791     zipfileWrite32(a, pEntry->mUnixTime);
5792   }
5793
5794   return a-aBuf;
5795 }
5796
5797 static int zipfileCommit(sqlite3_vtab *pVtab){
5798   ZipfileTab *pTab = (ZipfileTab*)pVtab;
5799   int rc = SQLITE_OK;
5800   if( pTab->pWriteFd ){
5801     i64 iOffset = pTab->szCurrent;
5802     ZipfileEntry *p;
5803     ZipfileEOCD eocd;
5804     int nEntry = 0;
5805
5806     /* Write out all entries */
5807     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5808       int n = zipfileSerializeCDS(p, pTab->aBuffer);
5809       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
5810       nEntry++;
5811     }
5812
5813     /* Write out the EOCD record */
5814     eocd.iDisk = 0;
5815     eocd.iFirstDisk = 0;
5816     eocd.nEntry = (u16)nEntry;
5817     eocd.nEntryTotal = (u16)nEntry;
5818     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
5819     eocd.iOffset = (u32)iOffset;
5820     rc = zipfileAppendEOCD(pTab, &eocd);
5821
5822     zipfileCleanupTransaction(pTab);
5823   }
5824   return rc;
5825 }
5826
5827 static int zipfileRollback(sqlite3_vtab *pVtab){
5828   return zipfileCommit(pVtab);
5829 }
5830
5831 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
5832   ZipfileCsr *pCsr;
5833   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5834     if( iId==pCsr->iId ) break;
5835   }
5836   return pCsr;
5837 }
5838
5839 static void zipfileFunctionCds(
5840   sqlite3_context *context,
5841   int argc,
5842   sqlite3_value **argv
5843 ){
5844   ZipfileCsr *pCsr;
5845   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
5846   assert( argc>0 );
5847
5848   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
5849   if( pCsr ){
5850     ZipfileCDS *p = &pCsr->pCurrent->cds;
5851     char *zRes = sqlite3_mprintf("{"
5852         "\"version-made-by\" : %u, "
5853         "\"version-to-extract\" : %u, "
5854         "\"flags\" : %u, "
5855         "\"compression\" : %u, "
5856         "\"time\" : %u, "
5857         "\"date\" : %u, "
5858         "\"crc32\" : %u, "
5859         "\"compressed-size\" : %u, "
5860         "\"uncompressed-size\" : %u, "
5861         "\"file-name-length\" : %u, "
5862         "\"extra-field-length\" : %u, "
5863         "\"file-comment-length\" : %u, "
5864         "\"disk-number-start\" : %u, "
5865         "\"internal-attr\" : %u, "
5866         "\"external-attr\" : %u, "
5867         "\"offset\" : %u }",
5868         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
5869         (u32)p->flags, (u32)p->iCompression,
5870         (u32)p->mTime, (u32)p->mDate,
5871         (u32)p->crc32, (u32)p->szCompressed,
5872         (u32)p->szUncompressed, (u32)p->nFile,
5873         (u32)p->nExtra, (u32)p->nComment,
5874         (u32)p->iDiskStart, (u32)p->iInternalAttr,
5875         (u32)p->iExternalAttr, (u32)p->iOffset
5876     );
5877
5878     if( zRes==0 ){
5879       sqlite3_result_error_nomem(context);
5880     }else{
5881       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
5882       sqlite3_free(zRes);
5883     }
5884   }
5885 }
5886
5887 /*
5888 ** xFindFunction method.
5889 */
5890 static int zipfileFindFunction(
5891   sqlite3_vtab *pVtab,            /* Virtual table handle */
5892   int nArg,                       /* Number of SQL function arguments */
5893   const char *zName,              /* Name of SQL function */
5894   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
5895   void **ppArg                    /* OUT: User data for *pxFunc */
5896 ){
5897   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5898     *pxFunc = zipfileFunctionCds;
5899     *ppArg = (void*)pVtab;
5900     return 1;
5901   }
5902   return 0;
5903 }
5904
5905 typedef struct ZipfileBuffer ZipfileBuffer;
5906 struct ZipfileBuffer {
5907   u8 *a;                          /* Pointer to buffer */
5908   int n;                          /* Size of buffer in bytes */
5909   int nAlloc;                     /* Byte allocated at a[] */
5910 };
5911
5912 typedef struct ZipfileCtx ZipfileCtx;
5913 struct ZipfileCtx {
5914   int nEntry;
5915   ZipfileBuffer body;
5916   ZipfileBuffer cds;
5917 };
5918
5919 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
5920   if( pBuf->n+nByte>pBuf->nAlloc ){
5921     u8 *aNew;
5922     int nNew = pBuf->n ? pBuf->n*2 : 512;
5923     int nReq = pBuf->n + nByte;
5924
5925     while( nNew<nReq ) nNew = nNew*2;
5926     aNew = sqlite3_realloc(pBuf->a, nNew);
5927     if( aNew==0 ) return SQLITE_NOMEM;
5928     pBuf->a = aNew;
5929     pBuf->nAlloc = nNew;
5930   }
5931   return SQLITE_OK;
5932 }
5933
5934 /*
5935 ** xStep() callback for the zipfile() aggregate. This can be called in
5936 ** any of the following ways:
5937 **
5938 **   SELECT zipfile(name,data) ...
5939 **   SELECT zipfile(name,mode,mtime,data) ...
5940 **   SELECT zipfile(name,mode,mtime,data,method) ...
5941 */
5942 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
5943   ZipfileCtx *p;                  /* Aggregate function context */
5944   ZipfileEntry e;                 /* New entry to add to zip archive */
5945
5946   sqlite3_value *pName = 0;
5947   sqlite3_value *pMode = 0;
5948   sqlite3_value *pMtime = 0;
5949   sqlite3_value *pData = 0;
5950   sqlite3_value *pMethod = 0;
5951
5952   int bIsDir = 0;
5953   u32 mode;
5954   int rc = SQLITE_OK;
5955   char *zErr = 0;
5956
5957   int iMethod = -1;               /* Compression method to use (0 or 8) */
5958
5959   const u8 *aData = 0;            /* Possibly compressed data for new entry */
5960   int nData = 0;                  /* Size of aData[] in bytes */
5961   int szUncompressed = 0;         /* Size of data before compression */
5962   u8 *aFree = 0;                  /* Free this before returning */
5963   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
5964
5965   char *zName = 0;                /* Path (name) of new entry */
5966   int nName = 0;                  /* Size of zName in bytes */
5967   char *zFree = 0;                /* Free this before returning */
5968   int nByte;
5969
5970   memset(&e, 0, sizeof(e));
5971   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
5972   if( p==0 ) return;
5973
5974   /* Martial the arguments into stack variables */
5975   if( nVal!=2 && nVal!=4 && nVal!=5 ){
5976     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
5977     rc = SQLITE_ERROR;
5978     goto zipfile_step_out;
5979   }
5980   pName = apVal[0];
5981   if( nVal==2 ){
5982     pData = apVal[1];
5983   }else{
5984     pMode = apVal[1];
5985     pMtime = apVal[2];
5986     pData = apVal[3];
5987     if( nVal==5 ){
5988       pMethod = apVal[4];
5989     }
5990   }
5991
5992   /* Check that the 'name' parameter looks ok. */
5993   zName = (char*)sqlite3_value_text(pName);
5994   nName = sqlite3_value_bytes(pName);
5995   if( zName==0 ){
5996     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
5997     rc = SQLITE_ERROR;
5998     goto zipfile_step_out;
5999   }
6000
6001   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6002   ** deflate compression) or NULL (choose automatically).  */
6003   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6004     iMethod = (int)sqlite3_value_int64(pMethod);
6005     if( iMethod!=0 && iMethod!=8 ){
6006       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6007       rc = SQLITE_ERROR;
6008       goto zipfile_step_out;
6009     }
6010   }
6011
6012   /* Now inspect the data. If this is NULL, then the new entry must be a
6013   ** directory.  Otherwise, figure out whether or not the data should
6014   ** be deflated or simply stored in the zip archive. */
6015   if( sqlite3_value_type(pData)==SQLITE_NULL ){
6016     bIsDir = 1;
6017     iMethod = 0;
6018   }else{
6019     aData = sqlite3_value_blob(pData);
6020     szUncompressed = nData = sqlite3_value_bytes(pData);
6021     iCrc32 = crc32(0, aData, nData);
6022     if( iMethod<0 || iMethod==8 ){
6023       int nOut = 0;
6024       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6025       if( rc!=SQLITE_OK ){
6026         goto zipfile_step_out;
6027       }
6028       if( iMethod==8 || nOut<nData ){
6029         aData = aFree;
6030         nData = nOut;
6031         iMethod = 8;
6032       }else{
6033         iMethod = 0;
6034       }
6035     }
6036   }
6037
6038   /* Decode the "mode" argument. */
6039   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6040   if( rc ) goto zipfile_step_out;
6041
6042   /* Decode the "mtime" argument. */
6043   e.mUnixTime = zipfileGetTime(pMtime);
6044
6045   /* If this is a directory entry, ensure that there is exactly one '/'
6046   ** at the end of the path. Or, if this is not a directory and the path
6047   ** ends in '/' it is an error. */
6048   if( bIsDir==0 ){
6049     if( zName[nName-1]=='/' ){
6050       zErr = sqlite3_mprintf("non-directory name must not end with /");
6051       rc = SQLITE_ERROR;
6052       goto zipfile_step_out;
6053     }
6054   }else{
6055     if( zName[nName-1]!='/' ){
6056       zName = zFree = sqlite3_mprintf("%s/", zName);
6057       nName++;
6058       if( zName==0 ){
6059         rc = SQLITE_NOMEM;
6060         goto zipfile_step_out;
6061       }
6062     }else{
6063       while( nName>1 && zName[nName-2]=='/' ) nName--;
6064     }
6065   }
6066
6067   /* Assemble the ZipfileEntry object for the new zip archive entry */
6068   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6069   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6070   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6071   e.cds.iCompression = (u16)iMethod;
6072   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6073   e.cds.crc32 = iCrc32;
6074   e.cds.szCompressed = nData;
6075   e.cds.szUncompressed = szUncompressed;
6076   e.cds.iExternalAttr = (mode<<16);
6077   e.cds.iOffset = p->body.n;
6078   e.cds.nFile = (u16)nName;
6079   e.cds.zFile = zName;
6080
6081   /* Append the LFH to the body of the new archive */
6082   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6083   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6084   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6085
6086   /* Append the data to the body of the new archive */
6087   if( nData>0 ){
6088     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6089     memcpy(&p->body.a[p->body.n], aData, nData);
6090     p->body.n += nData;
6091   }
6092
6093   /* Append the CDS record to the directory of the new archive */
6094   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6095   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6096   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6097
6098   /* Increment the count of entries in the archive */
6099   p->nEntry++;
6100
6101  zipfile_step_out:
6102   sqlite3_free(aFree);
6103   sqlite3_free(zFree);
6104   if( rc ){
6105     if( zErr ){
6106       sqlite3_result_error(pCtx, zErr, -1);
6107     }else{
6108       sqlite3_result_error_code(pCtx, rc);
6109     }
6110   }
6111   sqlite3_free(zErr);
6112 }
6113
6114 /*
6115 ** xFinalize() callback for zipfile aggregate function.
6116 */
6117 void zipfileFinal(sqlite3_context *pCtx){
6118   ZipfileCtx *p;
6119   ZipfileEOCD eocd;
6120   int nZip;
6121   u8 *aZip;
6122
6123   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6124   if( p==0 ) return;
6125   if( p->nEntry>0 ){
6126     memset(&eocd, 0, sizeof(eocd));
6127     eocd.nEntry = (u16)p->nEntry;
6128     eocd.nEntryTotal = (u16)p->nEntry;
6129     eocd.nSize = p->cds.n;
6130     eocd.iOffset = p->body.n;
6131
6132     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6133     aZip = (u8*)sqlite3_malloc(nZip);
6134     if( aZip==0 ){
6135       sqlite3_result_error_nomem(pCtx);
6136     }else{
6137       memcpy(aZip, p->body.a, p->body.n);
6138       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6139       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6140       sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
6141     }
6142   }
6143
6144   sqlite3_free(p->body.a);
6145   sqlite3_free(p->cds.a);
6146 }
6147
6148
6149 /*
6150 ** Register the "zipfile" virtual table.
6151 */
6152 static int zipfileRegister(sqlite3 *db){
6153   static sqlite3_module zipfileModule = {
6154     1,                         /* iVersion */
6155     zipfileConnect,            /* xCreate */
6156     zipfileConnect,            /* xConnect */
6157     zipfileBestIndex,          /* xBestIndex */
6158     zipfileDisconnect,         /* xDisconnect */
6159     zipfileDisconnect,         /* xDestroy */
6160     zipfileOpen,               /* xOpen - open a cursor */
6161     zipfileClose,              /* xClose - close a cursor */
6162     zipfileFilter,             /* xFilter - configure scan constraints */
6163     zipfileNext,               /* xNext - advance a cursor */
6164     zipfileEof,                /* xEof - check for end of scan */
6165     zipfileColumn,             /* xColumn - read data */
6166     0,                         /* xRowid - read data */
6167     zipfileUpdate,             /* xUpdate */
6168     zipfileBegin,              /* xBegin */
6169     0,                         /* xSync */
6170     zipfileCommit,             /* xCommit */
6171     zipfileRollback,           /* xRollback */
6172     zipfileFindFunction,       /* xFindMethod */
6173     0,                         /* xRename */
6174   };
6175
6176   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
6177   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6178   if( rc==SQLITE_OK ){
6179     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
6180         zipfileStep, zipfileFinal
6181     );
6182   }
6183   return rc;
6184 }
6185 #else         /* SQLITE_OMIT_VIRTUALTABLE */
6186 # define zipfileRegister(x) SQLITE_OK
6187 #endif
6188
6189 #ifdef _WIN32
6190
6191 #endif
6192 int sqlite3_zipfile_init(
6193   sqlite3 *db, 
6194   char **pzErrMsg, 
6195   const sqlite3_api_routines *pApi
6196 ){
6197   SQLITE_EXTENSION_INIT2(pApi);
6198   (void)pzErrMsg;  /* Unused parameter */
6199   return zipfileRegister(db);
6200 }
6201
6202 /************************* End ../ext/misc/zipfile.c ********************/
6203 /************************* Begin ../ext/misc/sqlar.c ******************/
6204 /*
6205 ** 2017-12-17
6206 **
6207 ** The author disclaims copyright to this source code.  In place of
6208 ** a legal notice, here is a blessing:
6209 **
6210 **    May you do good and not evil.
6211 **    May you find forgiveness for yourself and forgive others.
6212 **    May you share freely, never taking more than you give.
6213 **
6214 ******************************************************************************
6215 **
6216 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6217 ** for working with sqlar archives and used by the shell tool's built-in
6218 ** sqlar support.
6219 */
6220 SQLITE_EXTENSION_INIT1
6221 #include <zlib.h>
6222
6223 /*
6224 ** Implementation of the "sqlar_compress(X)" SQL function.
6225 **
6226 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6227 ** zlib utility function compress() yields a smaller blob, return the
6228 ** compressed blob. Otherwise, return a copy of X.
6229 **
6230 ** SQLar uses the "zlib format" for compressed content.  The zlib format
6231 ** contains a two-byte identification header and a four-byte checksum at
6232 ** the end.  This is different from ZIP which uses the raw deflate format.
6233 **
6234 ** Future enhancements to SQLar might add support for new compression formats.
6235 ** If so, those new formats will be identified by alternative headers in the
6236 ** compressed data.
6237 */
6238 static void sqlarCompressFunc(
6239   sqlite3_context *context,
6240   int argc,
6241   sqlite3_value **argv
6242 ){
6243   assert( argc==1 );
6244   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6245     const Bytef *pData = sqlite3_value_blob(argv[0]);
6246     uLong nData = sqlite3_value_bytes(argv[0]);
6247     uLongf nOut = compressBound(nData);
6248     Bytef *pOut;
6249
6250     pOut = (Bytef*)sqlite3_malloc(nOut);
6251     if( pOut==0 ){
6252       sqlite3_result_error_nomem(context);
6253       return;
6254     }else{
6255       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6256         sqlite3_result_error(context, "error in compress()", -1);
6257       }else if( nOut<nData ){
6258         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6259       }else{
6260         sqlite3_result_value(context, argv[0]);
6261       }
6262       sqlite3_free(pOut);
6263     }
6264   }else{
6265     sqlite3_result_value(context, argv[0]);
6266   }
6267 }
6268
6269 /*
6270 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6271 **
6272 ** Parameter SZ is interpreted as an integer. If it is less than or
6273 ** equal to zero, then this function returns a copy of X. Or, if
6274 ** SZ is equal to the size of X when interpreted as a blob, also
6275 ** return a copy of X. Otherwise, decompress blob X using zlib
6276 ** utility function uncompress() and return the results (another
6277 ** blob).
6278 */
6279 static void sqlarUncompressFunc(
6280   sqlite3_context *context,
6281   int argc,
6282   sqlite3_value **argv
6283 ){
6284   uLong nData;
6285   uLongf sz;
6286
6287   assert( argc==2 );
6288   sz = sqlite3_value_int(argv[1]);
6289
6290   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6291     sqlite3_result_value(context, argv[0]);
6292   }else{
6293     const Bytef *pData= sqlite3_value_blob(argv[0]);
6294     Bytef *pOut = sqlite3_malloc(sz);
6295     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6296       sqlite3_result_error(context, "error in uncompress()", -1);
6297     }else{
6298       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6299     }
6300     sqlite3_free(pOut);
6301   }
6302 }
6303
6304
6305 #ifdef _WIN32
6306
6307 #endif
6308 int sqlite3_sqlar_init(
6309   sqlite3 *db, 
6310   char **pzErrMsg, 
6311   const sqlite3_api_routines *pApi
6312 ){
6313   int rc = SQLITE_OK;
6314   SQLITE_EXTENSION_INIT2(pApi);
6315   (void)pzErrMsg;  /* Unused parameter */
6316   rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
6317                                sqlarCompressFunc, 0, 0);
6318   if( rc==SQLITE_OK ){
6319     rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
6320                                  sqlarUncompressFunc, 0, 0);
6321   }
6322   return rc;
6323 }
6324
6325 /************************* End ../ext/misc/sqlar.c ********************/
6326 #endif
6327 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6328 /*
6329 ** 2017 April 07
6330 **
6331 ** The author disclaims copyright to this source code.  In place of
6332 ** a legal notice, here is a blessing:
6333 **
6334 **    May you do good and not evil.
6335 **    May you find forgiveness for yourself and forgive others.
6336 **    May you share freely, never taking more than you give.
6337 **
6338 *************************************************************************
6339 */
6340
6341
6342
6343 typedef struct sqlite3expert sqlite3expert;
6344
6345 /*
6346 ** Create a new sqlite3expert object.
6347 **
6348 ** If successful, a pointer to the new object is returned and (*pzErr) set
6349 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6350 ** an English-language error message. In this case it is the responsibility
6351 ** of the caller to eventually free the error message buffer using
6352 ** sqlite3_free().
6353 */
6354 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6355
6356 /*
6357 ** Configure an sqlite3expert object.
6358 **
6359 ** EXPERT_CONFIG_SAMPLE:
6360 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6361 **   each candidate index. This involves scanning and sorting the entire
6362 **   contents of each user database table once for each candidate index
6363 **   associated with the table. For large databases, this can be 
6364 **   prohibitively slow. This option allows the sqlite3expert object to
6365 **   be configured so that sqlite_stat1 data is instead generated based on a
6366 **   subset of each table, or so that no sqlite_stat1 data is used at all.
6367 **
6368 **   A single integer argument is passed to this option. If the value is less
6369 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
6370 **   the analysis - indexes are recommended based on the database schema only.
6371 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
6372 **   generated for each candidate index (this is the default). Finally, if the
6373 **   value falls between 0 and 100, then it represents the percentage of user
6374 **   table rows that should be considered when generating sqlite_stat1 data.
6375 **
6376 **   Examples:
6377 **
6378 **     // Do not generate any sqlite_stat1 data
6379 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6380 **
6381 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
6382 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6383 */
6384 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6385
6386 #define EXPERT_CONFIG_SAMPLE 1    /* int */
6387
6388 /*
6389 ** Specify zero or more SQL statements to be included in the analysis.
6390 **
6391 ** Buffer zSql must contain zero or more complete SQL statements. This
6392 ** function parses all statements contained in the buffer and adds them
6393 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6394 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6395 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6396 ** may be set to point to an English language error message. In this case
6397 ** the caller is responsible for eventually freeing the error message buffer
6398 ** using sqlite3_free().
6399 **
6400 ** If an error does occur while processing one of the statements in the
6401 ** buffer passed as the second argument, none of the statements in the
6402 ** buffer are added to the analysis.
6403 **
6404 ** This function must be called before sqlite3_expert_analyze(). If a call
6405 ** to this function is made on an sqlite3expert object that has already
6406 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6407 ** immediately and no statements are added to the analysis.
6408 */
6409 int sqlite3_expert_sql(
6410   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
6411   const char *zSql,               /* SQL statement(s) to add */
6412   char **pzErr                    /* OUT: Error message (if any) */
6413 );
6414
6415
6416 /*
6417 ** This function is called after the sqlite3expert object has been configured
6418 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6419 ** the analysis. Once this function has been called, it is not possible to
6420 ** add further SQL statements to the analysis.
6421 **
6422 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6423 ** an error occurs, an SQLite error code is returned and (*pzErr) set to 
6424 ** point to a buffer containing an English language error message. In this
6425 ** case it is the responsibility of the caller to eventually free the buffer
6426 ** using sqlite3_free().
6427 **
6428 ** If an error does occur within this function, the sqlite3expert object
6429 ** is no longer useful for any purpose. At that point it is no longer
6430 ** possible to add further SQL statements to the object or to re-attempt
6431 ** the analysis. The sqlite3expert object must still be freed using a call
6432 ** sqlite3_expert_destroy().
6433 */
6434 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6435
6436 /*
6437 ** Return the total number of statements loaded using sqlite3_expert_sql().
6438 ** The total number of SQL statements may be different from the total number
6439 ** to calls to sqlite3_expert_sql().
6440 */
6441 int sqlite3_expert_count(sqlite3expert*);
6442
6443 /*
6444 ** Return a component of the report.
6445 **
6446 ** This function is called after sqlite3_expert_analyze() to extract the
6447 ** results of the analysis. Each call to this function returns either a
6448 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6449 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6450 ** #define constants defined below.
6451 **
6452 ** For some EXPERT_REPORT_* parameters, the buffer returned contains 
6453 ** information relating to a specific SQL statement. In these cases that
6454 ** SQL statement is identified by the value passed as the second argument.
6455 ** SQL statements are numbered from 0 in the order in which they are parsed.
6456 ** If an out-of-range value (less than zero or equal to or greater than the
6457 ** value returned by sqlite3_expert_count()) is passed as the second argument
6458 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6459 **
6460 ** EXPERT_REPORT_SQL:
6461 **   Return the text of SQL statement iStmt.
6462 **
6463 ** EXPERT_REPORT_INDEXES:
6464 **   Return a buffer containing the CREATE INDEX statements for all recommended
6465 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL 
6466 **   is returned.
6467 **
6468 ** EXPERT_REPORT_PLAN:
6469 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6470 **   iStmt after the proposed indexes have been added to the database schema.
6471 **
6472 ** EXPERT_REPORT_CANDIDATES:
6473 **   Return a pointer to a buffer containing the CREATE INDEX statements 
6474 **   for all indexes that were tested (for all SQL statements). The iStmt
6475 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6476 */
6477 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6478
6479 /*
6480 ** Values for the third argument passed to sqlite3_expert_report().
6481 */
6482 #define EXPERT_REPORT_SQL        1
6483 #define EXPERT_REPORT_INDEXES    2
6484 #define EXPERT_REPORT_PLAN       3
6485 #define EXPERT_REPORT_CANDIDATES 4
6486
6487 /*
6488 ** Free an (sqlite3expert*) handle and all associated resources. There 
6489 ** should be one call to this function for each successful call to 
6490 ** sqlite3-expert_new().
6491 */
6492 void sqlite3_expert_destroy(sqlite3expert*);
6493
6494
6495
6496 /************************* End ../ext/expert/sqlite3expert.h ********************/
6497 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6498 /*
6499 ** 2017 April 09
6500 **
6501 ** The author disclaims copyright to this source code.  In place of
6502 ** a legal notice, here is a blessing:
6503 **
6504 **    May you do good and not evil.
6505 **    May you find forgiveness for yourself and forgive others.
6506 **    May you share freely, never taking more than you give.
6507 **
6508 *************************************************************************
6509 */
6510 #include <assert.h>
6511 #include <string.h>
6512 #include <stdio.h>
6513
6514 #ifndef SQLITE_OMIT_VIRTUALTABLE 
6515
6516 /* typedef sqlite3_int64 i64; */
6517 /* typedef sqlite3_uint64 u64; */
6518
6519 typedef struct IdxColumn IdxColumn;
6520 typedef struct IdxConstraint IdxConstraint;
6521 typedef struct IdxScan IdxScan;
6522 typedef struct IdxStatement IdxStatement;
6523 typedef struct IdxTable IdxTable;
6524 typedef struct IdxWrite IdxWrite;
6525
6526 #define STRLEN  (int)strlen
6527
6528 /*
6529 ** A temp table name that we assume no user database will actually use.
6530 ** If this assumption proves incorrect triggers on the table with the
6531 ** conflicting name will be ignored.
6532 */
6533 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6534
6535 /*
6536 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6537 ** any other type of single-ended range constraint on a column).
6538 **
6539 ** pLink:
6540 **   Used to temporarily link IdxConstraint objects into lists while
6541 **   creating candidate indexes.
6542 */
6543 struct IdxConstraint {
6544   char *zColl;                    /* Collation sequence */
6545   int bRange;                     /* True for range, false for eq */
6546   int iCol;                       /* Constrained table column */
6547   int bFlag;                      /* Used by idxFindCompatible() */
6548   int bDesc;                      /* True if ORDER BY <expr> DESC */
6549   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
6550   IdxConstraint *pLink;           /* See above */
6551 };
6552
6553 /*
6554 ** A single scan of a single table.
6555 */
6556 struct IdxScan {
6557   IdxTable *pTab;                 /* Associated table object */
6558   int iDb;                        /* Database containing table zTable */
6559   i64 covering;                   /* Mask of columns required for cov. index */
6560   IdxConstraint *pOrder;          /* ORDER BY columns */
6561   IdxConstraint *pEq;             /* List of == constraints */
6562   IdxConstraint *pRange;          /* List of < constraints */
6563   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
6564 };
6565
6566 /*
6567 ** Information regarding a single database table. Extracted from 
6568 ** "PRAGMA table_info" by function idxGetTableInfo().
6569 */
6570 struct IdxColumn {
6571   char *zName;
6572   char *zColl;
6573   int iPk;
6574 };
6575 struct IdxTable {
6576   int nCol;
6577   char *zName;                    /* Table name */
6578   IdxColumn *aCol;
6579   IdxTable *pNext;                /* Next table in linked list of all tables */
6580 };
6581
6582 /*
6583 ** An object of the following type is created for each unique table/write-op
6584 ** seen. The objects are stored in a singly-linked list beginning at
6585 ** sqlite3expert.pWrite.
6586 */
6587 struct IdxWrite {
6588   IdxTable *pTab;
6589   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
6590   IdxWrite *pNext;
6591 };
6592
6593 /*
6594 ** Each statement being analyzed is represented by an instance of this
6595 ** structure.
6596 */
6597 struct IdxStatement {
6598   int iId;                        /* Statement number */
6599   char *zSql;                     /* SQL statement */
6600   char *zIdx;                     /* Indexes */
6601   char *zEQP;                     /* Plan */
6602   IdxStatement *pNext;
6603 };
6604
6605
6606 /*
6607 ** A hash table for storing strings. With space for a payload string
6608 ** with each entry. Methods are:
6609 **
6610 **   idxHashInit()
6611 **   idxHashClear()
6612 **   idxHashAdd()
6613 **   idxHashSearch()
6614 */
6615 #define IDX_HASH_SIZE 1023
6616 typedef struct IdxHashEntry IdxHashEntry;
6617 typedef struct IdxHash IdxHash;
6618 struct IdxHashEntry {
6619   char *zKey;                     /* nul-terminated key */
6620   char *zVal;                     /* nul-terminated value string */
6621   char *zVal2;                    /* nul-terminated value string 2 */
6622   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
6623   IdxHashEntry *pNext;            /* Next entry in hash */
6624 };
6625 struct IdxHash {
6626   IdxHashEntry *pFirst;
6627   IdxHashEntry *aHash[IDX_HASH_SIZE];
6628 };
6629
6630 /*
6631 ** sqlite3expert object.
6632 */
6633 struct sqlite3expert {
6634   int iSample;                    /* Percentage of tables to sample for stat1 */
6635   sqlite3 *db;                    /* User database */
6636   sqlite3 *dbm;                   /* In-memory db for this analysis */
6637   sqlite3 *dbv;                   /* Vtab schema for this analysis */
6638   IdxTable *pTable;               /* List of all IdxTable objects */
6639   IdxScan *pScan;                 /* List of scan objects */
6640   IdxWrite *pWrite;               /* List of write objects */
6641   IdxStatement *pStatement;       /* List of IdxStatement objects */
6642   int bRun;                       /* True once analysis has run */
6643   char **pzErrmsg;
6644   int rc;                         /* Error code from whereinfo hook */
6645   IdxHash hIdx;                   /* Hash containing all candidate indexes */
6646   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
6647 };
6648
6649
6650 /*
6651 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). 
6652 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6653 */
6654 static void *idxMalloc(int *pRc, int nByte){
6655   void *pRet;
6656   assert( *pRc==SQLITE_OK );
6657   assert( nByte>0 );
6658   pRet = sqlite3_malloc(nByte);
6659   if( pRet ){
6660     memset(pRet, 0, nByte);
6661   }else{
6662     *pRc = SQLITE_NOMEM;
6663   }
6664   return pRet;
6665 }
6666
6667 /*
6668 ** Initialize an IdxHash hash table.
6669 */
6670 static void idxHashInit(IdxHash *pHash){
6671   memset(pHash, 0, sizeof(IdxHash));
6672 }
6673
6674 /*
6675 ** Reset an IdxHash hash table.
6676 */
6677 static void idxHashClear(IdxHash *pHash){
6678   int i;
6679   for(i=0; i<IDX_HASH_SIZE; i++){
6680     IdxHashEntry *pEntry;
6681     IdxHashEntry *pNext;
6682     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6683       pNext = pEntry->pHashNext;
6684       sqlite3_free(pEntry->zVal2);
6685       sqlite3_free(pEntry);
6686     }
6687   }
6688   memset(pHash, 0, sizeof(IdxHash));
6689 }
6690
6691 /*
6692 ** Return the index of the hash bucket that the string specified by the
6693 ** arguments to this function belongs.
6694 */
6695 static int idxHashString(const char *z, int n){
6696   unsigned int ret = 0;
6697   int i;
6698   for(i=0; i<n; i++){
6699     ret += (ret<<3) + (unsigned char)(z[i]);
6700   }
6701   return (int)(ret % IDX_HASH_SIZE);
6702 }
6703
6704 /*
6705 ** If zKey is already present in the hash table, return non-zero and do
6706 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6707 ** the hash table passed as the second argument. 
6708 */
6709 static int idxHashAdd(
6710   int *pRc, 
6711   IdxHash *pHash, 
6712   const char *zKey,
6713   const char *zVal
6714 ){
6715   int nKey = STRLEN(zKey);
6716   int iHash = idxHashString(zKey, nKey);
6717   int nVal = (zVal ? STRLEN(zVal) : 0);
6718   IdxHashEntry *pEntry;
6719   assert( iHash>=0 );
6720   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6721     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6722       return 1;
6723     }
6724   }
6725   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6726   if( pEntry ){
6727     pEntry->zKey = (char*)&pEntry[1];
6728     memcpy(pEntry->zKey, zKey, nKey);
6729     if( zVal ){
6730       pEntry->zVal = &pEntry->zKey[nKey+1];
6731       memcpy(pEntry->zVal, zVal, nVal);
6732     }
6733     pEntry->pHashNext = pHash->aHash[iHash];
6734     pHash->aHash[iHash] = pEntry;
6735
6736     pEntry->pNext = pHash->pFirst;
6737     pHash->pFirst = pEntry;
6738   }
6739   return 0;
6740 }
6741
6742 /*
6743 ** If zKey/nKey is present in the hash table, return a pointer to the 
6744 ** hash-entry object.
6745 */
6746 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6747   int iHash;
6748   IdxHashEntry *pEntry;
6749   if( nKey<0 ) nKey = STRLEN(zKey);
6750   iHash = idxHashString(zKey, nKey);
6751   assert( iHash>=0 );
6752   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6753     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6754       return pEntry;
6755     }
6756   }
6757   return 0;
6758 }
6759
6760 /*
6761 ** If the hash table contains an entry with a key equal to the string
6762 ** passed as the final two arguments to this function, return a pointer
6763 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6764 ** hash table, return NULL.
6765 */
6766 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6767   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6768   if( pEntry ) return pEntry->zVal;
6769   return 0;
6770 }
6771
6772 /*
6773 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6774 ** variable to point to a copy of nul-terminated string zColl.
6775 */
6776 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6777   IdxConstraint *pNew;
6778   int nColl = STRLEN(zColl);
6779
6780   assert( *pRc==SQLITE_OK );
6781   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6782   if( pNew ){
6783     pNew->zColl = (char*)&pNew[1];
6784     memcpy(pNew->zColl, zColl, nColl+1);
6785   }
6786   return pNew;
6787 }
6788
6789 /*
6790 ** An error associated with database handle db has just occurred. Pass
6791 ** the error message to callback function xOut.
6792 */
6793 static void idxDatabaseError(
6794   sqlite3 *db,                    /* Database handle */
6795   char **pzErrmsg                 /* Write error here */
6796 ){
6797   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6798 }
6799
6800 /*
6801 ** Prepare an SQL statement.
6802 */
6803 static int idxPrepareStmt(
6804   sqlite3 *db,                    /* Database handle to compile against */
6805   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
6806   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
6807   const char *zSql                /* SQL statement to compile */
6808 ){
6809   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6810   if( rc!=SQLITE_OK ){
6811     *ppStmt = 0;
6812     idxDatabaseError(db, pzErrmsg);
6813   }
6814   return rc;
6815 }
6816
6817 /*
6818 ** Prepare an SQL statement using the results of a printf() formatting.
6819 */
6820 static int idxPrintfPrepareStmt(
6821   sqlite3 *db,                    /* Database handle to compile against */
6822   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
6823   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
6824   const char *zFmt,               /* printf() format of SQL statement */
6825   ...                             /* Trailing printf() arguments */
6826 ){
6827   va_list ap;
6828   int rc;
6829   char *zSql;
6830   va_start(ap, zFmt);
6831   zSql = sqlite3_vmprintf(zFmt, ap);
6832   if( zSql==0 ){
6833     rc = SQLITE_NOMEM;
6834   }else{
6835     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
6836     sqlite3_free(zSql);
6837   }
6838   va_end(ap);
6839   return rc;
6840 }
6841
6842
6843 /*************************************************************************
6844 ** Beginning of virtual table implementation.
6845 */
6846 typedef struct ExpertVtab ExpertVtab;
6847 struct ExpertVtab {
6848   sqlite3_vtab base;
6849   IdxTable *pTab;
6850   sqlite3expert *pExpert;
6851 };
6852
6853 typedef struct ExpertCsr ExpertCsr;
6854 struct ExpertCsr {
6855   sqlite3_vtab_cursor base;
6856   sqlite3_stmt *pData;
6857 };
6858
6859 static char *expertDequote(const char *zIn){
6860   int n = STRLEN(zIn);
6861   char *zRet = sqlite3_malloc(n);
6862
6863   assert( zIn[0]=='\'' );
6864   assert( zIn[n-1]=='\'' );
6865
6866   if( zRet ){
6867     int iOut = 0;
6868     int iIn = 0;
6869     for(iIn=1; iIn<(n-1); iIn++){
6870       if( zIn[iIn]=='\'' ){
6871         assert( zIn[iIn+1]=='\'' );
6872         iIn++;
6873       }
6874       zRet[iOut++] = zIn[iIn];
6875     }
6876     zRet[iOut] = '\0';
6877   }
6878
6879   return zRet;
6880 }
6881
6882 /* 
6883 ** This function is the implementation of both the xConnect and xCreate
6884 ** methods of the r-tree virtual table.
6885 **
6886 **   argv[0]   -> module name
6887 **   argv[1]   -> database name
6888 **   argv[2]   -> table name
6889 **   argv[...] -> column names...
6890 */
6891 static int expertConnect(
6892   sqlite3 *db,
6893   void *pAux,
6894   int argc, const char *const*argv,
6895   sqlite3_vtab **ppVtab,
6896   char **pzErr
6897 ){
6898   sqlite3expert *pExpert = (sqlite3expert*)pAux;
6899   ExpertVtab *p = 0;
6900   int rc;
6901
6902   if( argc!=4 ){
6903     *pzErr = sqlite3_mprintf("internal error!");
6904     rc = SQLITE_ERROR;
6905   }else{
6906     char *zCreateTable = expertDequote(argv[3]);
6907     if( zCreateTable ){
6908       rc = sqlite3_declare_vtab(db, zCreateTable);
6909       if( rc==SQLITE_OK ){
6910         p = idxMalloc(&rc, sizeof(ExpertVtab));
6911       }
6912       if( rc==SQLITE_OK ){
6913         p->pExpert = pExpert;
6914         p->pTab = pExpert->pTable;
6915         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
6916       }
6917       sqlite3_free(zCreateTable);
6918     }else{
6919       rc = SQLITE_NOMEM;
6920     }
6921   }
6922
6923   *ppVtab = (sqlite3_vtab*)p;
6924   return rc;
6925 }
6926
6927 static int expertDisconnect(sqlite3_vtab *pVtab){
6928   ExpertVtab *p = (ExpertVtab*)pVtab;
6929   sqlite3_free(p);
6930   return SQLITE_OK;
6931 }
6932
6933 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
6934   ExpertVtab *p = (ExpertVtab*)pVtab;
6935   int rc = SQLITE_OK;
6936   int n = 0;
6937   IdxScan *pScan;
6938   const int opmask = 
6939     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
6940     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
6941     SQLITE_INDEX_CONSTRAINT_LE;
6942
6943   pScan = idxMalloc(&rc, sizeof(IdxScan));
6944   if( pScan ){
6945     int i;
6946
6947     /* Link the new scan object into the list */
6948     pScan->pTab = p->pTab;
6949     pScan->pNextScan = p->pExpert->pScan;
6950     p->pExpert->pScan = pScan;
6951
6952     /* Add the constraints to the IdxScan object */
6953     for(i=0; i<pIdxInfo->nConstraint; i++){
6954       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
6955       if( pCons->usable 
6956        && pCons->iColumn>=0 
6957        && p->pTab->aCol[pCons->iColumn].iPk==0
6958        && (pCons->op & opmask) 
6959       ){
6960         IdxConstraint *pNew;
6961         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
6962         pNew = idxNewConstraint(&rc, zColl);
6963         if( pNew ){
6964           pNew->iCol = pCons->iColumn;
6965           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6966             pNew->pNext = pScan->pEq;
6967             pScan->pEq = pNew;
6968           }else{
6969             pNew->bRange = 1;
6970             pNew->pNext = pScan->pRange;
6971             pScan->pRange = pNew;
6972           }
6973         }
6974         n++;
6975         pIdxInfo->aConstraintUsage[i].argvIndex = n;
6976       }
6977     }
6978
6979     /* Add the ORDER BY to the IdxScan object */
6980     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
6981       int iCol = pIdxInfo->aOrderBy[i].iColumn;
6982       if( iCol>=0 ){
6983         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
6984         if( pNew ){
6985           pNew->iCol = iCol;
6986           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
6987           pNew->pNext = pScan->pOrder;
6988           pNew->pLink = pScan->pOrder;
6989           pScan->pOrder = pNew;
6990           n++;
6991         }
6992       }
6993     }
6994   }
6995
6996   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
6997   return rc;
6998 }
6999
7000 static int expertUpdate(
7001   sqlite3_vtab *pVtab, 
7002   int nData, 
7003   sqlite3_value **azData, 
7004   sqlite_int64 *pRowid
7005 ){
7006   (void)pVtab;
7007   (void)nData;
7008   (void)azData;
7009   (void)pRowid;
7010   return SQLITE_OK;
7011 }
7012
7013 /* 
7014 ** Virtual table module xOpen method.
7015 */
7016 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7017   int rc = SQLITE_OK;
7018   ExpertCsr *pCsr;
7019   (void)pVTab;
7020   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7021   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7022   return rc;
7023 }
7024
7025 /* 
7026 ** Virtual table module xClose method.
7027 */
7028 static int expertClose(sqlite3_vtab_cursor *cur){
7029   ExpertCsr *pCsr = (ExpertCsr*)cur;
7030   sqlite3_finalize(pCsr->pData);
7031   sqlite3_free(pCsr);
7032   return SQLITE_OK;
7033 }
7034
7035 /*
7036 ** Virtual table module xEof method.
7037 **
7038 ** Return non-zero if the cursor does not currently point to a valid 
7039 ** record (i.e if the scan has finished), or zero otherwise.
7040 */
7041 static int expertEof(sqlite3_vtab_cursor *cur){
7042   ExpertCsr *pCsr = (ExpertCsr*)cur;
7043   return pCsr->pData==0;
7044 }
7045
7046 /* 
7047 ** Virtual table module xNext method.
7048 */
7049 static int expertNext(sqlite3_vtab_cursor *cur){
7050   ExpertCsr *pCsr = (ExpertCsr*)cur;
7051   int rc = SQLITE_OK;
7052
7053   assert( pCsr->pData );
7054   rc = sqlite3_step(pCsr->pData);
7055   if( rc!=SQLITE_ROW ){
7056     rc = sqlite3_finalize(pCsr->pData);
7057     pCsr->pData = 0;
7058   }else{
7059     rc = SQLITE_OK;
7060   }
7061
7062   return rc;
7063 }
7064
7065 /* 
7066 ** Virtual table module xRowid method.
7067 */
7068 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7069   (void)cur;
7070   *pRowid = 0;
7071   return SQLITE_OK;
7072 }
7073
7074 /* 
7075 ** Virtual table module xColumn method.
7076 */
7077 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7078   ExpertCsr *pCsr = (ExpertCsr*)cur;
7079   sqlite3_value *pVal;
7080   pVal = sqlite3_column_value(pCsr->pData, i);
7081   if( pVal ){
7082     sqlite3_result_value(ctx, pVal);
7083   }
7084   return SQLITE_OK;
7085 }
7086
7087 /* 
7088 ** Virtual table module xFilter method.
7089 */
7090 static int expertFilter(
7091   sqlite3_vtab_cursor *cur, 
7092   int idxNum, const char *idxStr,
7093   int argc, sqlite3_value **argv
7094 ){
7095   ExpertCsr *pCsr = (ExpertCsr*)cur;
7096   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7097   sqlite3expert *pExpert = pVtab->pExpert;
7098   int rc;
7099
7100   (void)idxNum;
7101   (void)idxStr;
7102   (void)argc;
7103   (void)argv;
7104   rc = sqlite3_finalize(pCsr->pData);
7105   pCsr->pData = 0;
7106   if( rc==SQLITE_OK ){
7107     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7108         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7109     );
7110   }
7111
7112   if( rc==SQLITE_OK ){
7113     rc = expertNext(cur);
7114   }
7115   return rc;
7116 }
7117
7118 static int idxRegisterVtab(sqlite3expert *p){
7119   static sqlite3_module expertModule = {
7120     2,                            /* iVersion */
7121     expertConnect,                /* xCreate - create a table */
7122     expertConnect,                /* xConnect - connect to an existing table */
7123     expertBestIndex,              /* xBestIndex - Determine search strategy */
7124     expertDisconnect,             /* xDisconnect - Disconnect from a table */
7125     expertDisconnect,             /* xDestroy - Drop a table */
7126     expertOpen,                   /* xOpen - open a cursor */
7127     expertClose,                  /* xClose - close a cursor */
7128     expertFilter,                 /* xFilter - configure scan constraints */
7129     expertNext,                   /* xNext - advance a cursor */
7130     expertEof,                    /* xEof */
7131     expertColumn,                 /* xColumn - read data */
7132     expertRowid,                  /* xRowid - read data */
7133     expertUpdate,                 /* xUpdate - write data */
7134     0,                            /* xBegin - begin transaction */
7135     0,                            /* xSync - sync transaction */
7136     0,                            /* xCommit - commit transaction */
7137     0,                            /* xRollback - rollback transaction */
7138     0,                            /* xFindFunction - function overloading */
7139     0,                            /* xRename - rename the table */
7140     0,                            /* xSavepoint */
7141     0,                            /* xRelease */
7142     0,                            /* xRollbackTo */
7143   };
7144
7145   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7146 }
7147 /*
7148 ** End of virtual table implementation.
7149 *************************************************************************/
7150 /*
7151 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7152 ** is called, set it to the return value of sqlite3_finalize() before
7153 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7154 */
7155 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7156   int rc = sqlite3_finalize(pStmt);
7157   if( *pRc==SQLITE_OK ) *pRc = rc;
7158 }
7159
7160 /*
7161 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7162 ** in the main database of connection db. If successful, set (*ppOut) to
7163 ** point to the new object and return SQLITE_OK. Otherwise, return an
7164 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7165 ** set to point to an error string.
7166 **
7167 ** It is the responsibility of the caller to eventually free either the
7168 ** IdxTable object or error message using sqlite3_free().
7169 */
7170 static int idxGetTableInfo(
7171   sqlite3 *db,                    /* Database connection to read details from */
7172   const char *zTab,               /* Table name */
7173   IdxTable **ppOut,               /* OUT: New object (if successful) */
7174   char **pzErrmsg                 /* OUT: Error message (if not) */
7175 ){
7176   sqlite3_stmt *p1 = 0;
7177   int nCol = 0;
7178   int nTab = STRLEN(zTab);
7179   int nByte = sizeof(IdxTable) + nTab + 1;
7180   IdxTable *pNew = 0;
7181   int rc, rc2;
7182   char *pCsr = 0;
7183
7184   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7185   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7186     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7187     nByte += 1 + STRLEN(zCol);
7188     rc = sqlite3_table_column_metadata(
7189         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7190     );
7191     nByte += 1 + STRLEN(zCol);
7192     nCol++;
7193   }
7194   rc2 = sqlite3_reset(p1);
7195   if( rc==SQLITE_OK ) rc = rc2;
7196
7197   nByte += sizeof(IdxColumn) * nCol;
7198   if( rc==SQLITE_OK ){
7199     pNew = idxMalloc(&rc, nByte);
7200   }
7201   if( rc==SQLITE_OK ){
7202     pNew->aCol = (IdxColumn*)&pNew[1];
7203     pNew->nCol = nCol;
7204     pCsr = (char*)&pNew->aCol[nCol];
7205   }
7206
7207   nCol = 0;
7208   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7209     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7210     int nCopy = STRLEN(zCol) + 1;
7211     pNew->aCol[nCol].zName = pCsr;
7212     pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7213     memcpy(pCsr, zCol, nCopy);
7214     pCsr += nCopy;
7215
7216     rc = sqlite3_table_column_metadata(
7217         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7218     );
7219     if( rc==SQLITE_OK ){
7220       nCopy = STRLEN(zCol) + 1;
7221       pNew->aCol[nCol].zColl = pCsr;
7222       memcpy(pCsr, zCol, nCopy);
7223       pCsr += nCopy;
7224     }
7225
7226     nCol++;
7227   }
7228   idxFinalize(&rc, p1);
7229
7230   if( rc!=SQLITE_OK ){
7231     sqlite3_free(pNew);
7232     pNew = 0;
7233   }else{
7234     pNew->zName = pCsr;
7235     memcpy(pNew->zName, zTab, nTab+1);
7236   }
7237
7238   *ppOut = pNew;
7239   return rc;
7240 }
7241
7242 /*
7243 ** This function is a no-op if *pRc is set to anything other than 
7244 ** SQLITE_OK when it is called.
7245 **
7246 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7247 ** the printf() style arguments is appended to zIn and the result returned
7248 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7249 ** zIn before returning.
7250 */
7251 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7252   va_list ap;
7253   char *zAppend = 0;
7254   char *zRet = 0;
7255   int nIn = zIn ? STRLEN(zIn) : 0;
7256   int nAppend = 0;
7257   va_start(ap, zFmt);
7258   if( *pRc==SQLITE_OK ){
7259     zAppend = sqlite3_vmprintf(zFmt, ap);
7260     if( zAppend ){
7261       nAppend = STRLEN(zAppend);
7262       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7263     }
7264     if( zAppend && zRet ){
7265       if( nIn ) memcpy(zRet, zIn, nIn);
7266       memcpy(&zRet[nIn], zAppend, nAppend+1);
7267     }else{
7268       sqlite3_free(zRet);
7269       zRet = 0;
7270       *pRc = SQLITE_NOMEM;
7271     }
7272     sqlite3_free(zAppend);
7273     sqlite3_free(zIn);
7274   }
7275   va_end(ap);
7276   return zRet;
7277 }
7278
7279 /*
7280 ** Return true if zId must be quoted in order to use it as an SQL
7281 ** identifier, or false otherwise.
7282 */
7283 static int idxIdentifierRequiresQuotes(const char *zId){
7284   int i;
7285   for(i=0; zId[i]; i++){
7286     if( !(zId[i]=='_')
7287      && !(zId[i]>='0' && zId[i]<='9')
7288      && !(zId[i]>='a' && zId[i]<='z')
7289      && !(zId[i]>='A' && zId[i]<='Z')
7290     ){
7291       return 1;
7292     }
7293   }
7294   return 0;
7295 }
7296
7297 /*
7298 ** This function appends an index column definition suitable for constraint
7299 ** pCons to the string passed as zIn and returns the result.
7300 */
7301 static char *idxAppendColDefn(
7302   int *pRc,                       /* IN/OUT: Error code */
7303   char *zIn,                      /* Column defn accumulated so far */
7304   IdxTable *pTab,                 /* Table index will be created on */
7305   IdxConstraint *pCons
7306 ){
7307   char *zRet = zIn;
7308   IdxColumn *p = &pTab->aCol[pCons->iCol];
7309   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7310
7311   if( idxIdentifierRequiresQuotes(p->zName) ){
7312     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7313   }else{
7314     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7315   }
7316
7317   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7318     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7319       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7320     }else{
7321       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7322     }
7323   }
7324
7325   if( pCons->bDesc ){
7326     zRet = idxAppendText(pRc, zRet, " DESC");
7327   }
7328   return zRet;
7329 }
7330
7331 /*
7332 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7333 ** would create from arguments pScan, pEq and pTail. If no error occurs and 
7334 ** such an index is found, return non-zero. Or, if no such index is found,
7335 ** return zero.
7336 **
7337 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7338 */
7339 static int idxFindCompatible(
7340   int *pRc,                       /* OUT: Error code */
7341   sqlite3* dbm,                   /* Database to search */
7342   IdxScan *pScan,                 /* Scan for table to search for index on */
7343   IdxConstraint *pEq,             /* List of == constraints */
7344   IdxConstraint *pTail            /* List of range constraints */
7345 ){
7346   const char *zTbl = pScan->pTab->zName;
7347   sqlite3_stmt *pIdxList = 0;
7348   IdxConstraint *pIter;
7349   int nEq = 0;                    /* Number of elements in pEq */
7350   int rc;
7351
7352   /* Count the elements in list pEq */
7353   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7354
7355   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7356   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7357     int bMatch = 1;
7358     IdxConstraint *pT = pTail;
7359     sqlite3_stmt *pInfo = 0;
7360     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7361
7362     /* Zero the IdxConstraint.bFlag values in the pEq list */
7363     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7364
7365     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7366     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7367       int iIdx = sqlite3_column_int(pInfo, 0);
7368       int iCol = sqlite3_column_int(pInfo, 1);
7369       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7370
7371       if( iIdx<nEq ){
7372         for(pIter=pEq; pIter; pIter=pIter->pLink){
7373           if( pIter->bFlag ) continue;
7374           if( pIter->iCol!=iCol ) continue;
7375           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7376           pIter->bFlag = 1;
7377           break;
7378         }
7379         if( pIter==0 ){
7380           bMatch = 0;
7381           break;
7382         }
7383       }else{
7384         if( pT ){
7385           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7386             bMatch = 0;
7387             break;
7388           }
7389           pT = pT->pLink;
7390         }
7391       }
7392     }
7393     idxFinalize(&rc, pInfo);
7394
7395     if( rc==SQLITE_OK && bMatch ){
7396       sqlite3_finalize(pIdxList);
7397       return 1;
7398     }
7399   }
7400   idxFinalize(&rc, pIdxList);
7401
7402   *pRc = rc;
7403   return 0;
7404 }
7405
7406 static int idxCreateFromCons(
7407   sqlite3expert *p,
7408   IdxScan *pScan,
7409   IdxConstraint *pEq, 
7410   IdxConstraint *pTail
7411 ){
7412   sqlite3 *dbm = p->dbm;
7413   int rc = SQLITE_OK;
7414   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7415     IdxTable *pTab = pScan->pTab;
7416     char *zCols = 0;
7417     char *zIdx = 0;
7418     IdxConstraint *pCons;
7419     unsigned int h = 0;
7420     const char *zFmt;
7421
7422     for(pCons=pEq; pCons; pCons=pCons->pLink){
7423       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7424     }
7425     for(pCons=pTail; pCons; pCons=pCons->pLink){
7426       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7427     }
7428
7429     if( rc==SQLITE_OK ){
7430       /* Hash the list of columns to come up with a name for the index */
7431       const char *zTable = pScan->pTab->zName;
7432       char *zName;                /* Index name */
7433       int i;
7434       for(i=0; zCols[i]; i++){
7435         h += ((h<<3) + zCols[i]);
7436       }
7437       zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7438       if( zName==0 ){ 
7439         rc = SQLITE_NOMEM;
7440       }else{
7441         if( idxIdentifierRequiresQuotes(zTable) ){
7442           zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7443         }else{
7444           zFmt = "CREATE INDEX %s ON %s(%s)";
7445         }
7446         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7447         if( !zIdx ){
7448           rc = SQLITE_NOMEM;
7449         }else{
7450           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7451           idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7452         }
7453         sqlite3_free(zName);
7454         sqlite3_free(zIdx);
7455       }
7456     }
7457
7458     sqlite3_free(zCols);
7459   }
7460   return rc;
7461 }
7462
7463 /*
7464 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7465 ** a constraint compatible with *p. Otherwise return false.
7466 */
7467 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7468   IdxConstraint *pCmp;
7469   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7470     if( p->iCol==pCmp->iCol ) return 1;
7471   }
7472   return 0;
7473 }
7474
7475 static int idxCreateFromWhere(
7476   sqlite3expert *p, 
7477   IdxScan *pScan,                 /* Create indexes for this scan */
7478   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
7479 ){
7480   IdxConstraint *p1 = 0;
7481   IdxConstraint *pCon;
7482   int rc;
7483
7484   /* Gather up all the == constraints. */
7485   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7486     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7487       pCon->pLink = p1;
7488       p1 = pCon;
7489     }
7490   }
7491
7492   /* Create an index using the == constraints collected above. And the
7493   ** range constraint/ORDER BY terms passed in by the caller, if any. */
7494   rc = idxCreateFromCons(p, pScan, p1, pTail);
7495
7496   /* If no range/ORDER BY passed by the caller, create a version of the
7497   ** index for each range constraint.  */
7498   if( pTail==0 ){
7499     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7500       assert( pCon->pLink==0 );
7501       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7502         rc = idxCreateFromCons(p, pScan, p1, pCon);
7503       }
7504     }
7505   }
7506
7507   return rc;
7508 }
7509
7510 /*
7511 ** Create candidate indexes in database [dbm] based on the data in 
7512 ** linked-list pScan.
7513 */
7514 static int idxCreateCandidates(sqlite3expert *p){
7515   int rc = SQLITE_OK;
7516   IdxScan *pIter;
7517
7518   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7519     rc = idxCreateFromWhere(p, pIter, 0);
7520     if( rc==SQLITE_OK && pIter->pOrder ){
7521       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7522     }
7523   }
7524
7525   return rc;
7526 }
7527
7528 /*
7529 ** Free all elements of the linked list starting at pConstraint.
7530 */
7531 static void idxConstraintFree(IdxConstraint *pConstraint){
7532   IdxConstraint *pNext;
7533   IdxConstraint *p;
7534
7535   for(p=pConstraint; p; p=pNext){
7536     pNext = p->pNext;
7537     sqlite3_free(p);
7538   }
7539 }
7540
7541 /*
7542 ** Free all elements of the linked list starting from pScan up until pLast
7543 ** (pLast is not freed).
7544 */
7545 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7546   IdxScan *p;
7547   IdxScan *pNext;
7548   for(p=pScan; p!=pLast; p=pNext){
7549     pNext = p->pNextScan;
7550     idxConstraintFree(p->pOrder);
7551     idxConstraintFree(p->pEq);
7552     idxConstraintFree(p->pRange);
7553     sqlite3_free(p);
7554   }
7555 }
7556
7557 /*
7558 ** Free all elements of the linked list starting from pStatement up 
7559 ** until pLast (pLast is not freed).
7560 */
7561 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7562   IdxStatement *p;
7563   IdxStatement *pNext;
7564   for(p=pStatement; p!=pLast; p=pNext){
7565     pNext = p->pNext;
7566     sqlite3_free(p->zEQP);
7567     sqlite3_free(p->zIdx);
7568     sqlite3_free(p);
7569   }
7570 }
7571
7572 /*
7573 ** Free the linked list of IdxTable objects starting at pTab.
7574 */
7575 static void idxTableFree(IdxTable *pTab){
7576   IdxTable *pIter;
7577   IdxTable *pNext;
7578   for(pIter=pTab; pIter; pIter=pNext){
7579     pNext = pIter->pNext;
7580     sqlite3_free(pIter);
7581   }
7582 }
7583
7584 /*
7585 ** Free the linked list of IdxWrite objects starting at pTab.
7586 */
7587 static void idxWriteFree(IdxWrite *pTab){
7588   IdxWrite *pIter;
7589   IdxWrite *pNext;
7590   for(pIter=pTab; pIter; pIter=pNext){
7591     pNext = pIter->pNext;
7592     sqlite3_free(pIter);
7593   }
7594 }
7595
7596
7597
7598 /*
7599 ** This function is called after candidate indexes have been created. It
7600 ** runs all the queries to see which indexes they prefer, and populates
7601 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7602 */
7603 int idxFindIndexes(
7604   sqlite3expert *p,
7605   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
7606 ){
7607   IdxStatement *pStmt;
7608   sqlite3 *dbm = p->dbm;
7609   int rc = SQLITE_OK;
7610
7611   IdxHash hIdx;
7612   idxHashInit(&hIdx);
7613
7614   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7615     IdxHashEntry *pEntry;
7616     sqlite3_stmt *pExplain = 0;
7617     idxHashClear(&hIdx);
7618     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7619         "EXPLAIN QUERY PLAN %s", pStmt->zSql
7620     );
7621     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7622       /* int iId = sqlite3_column_int(pExplain, 0); */
7623       /* int iParent = sqlite3_column_int(pExplain, 1); */
7624       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7625       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7626       int nDetail = STRLEN(zDetail);
7627       int i;
7628
7629       for(i=0; i<nDetail; i++){
7630         const char *zIdx = 0;
7631         if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7632           zIdx = &zDetail[i+13];
7633         }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7634           zIdx = &zDetail[i+22];
7635         }
7636         if( zIdx ){
7637           const char *zSql;
7638           int nIdx = 0;
7639           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7640             nIdx++;
7641           }
7642           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7643           if( zSql ){
7644             idxHashAdd(&rc, &hIdx, zSql, 0);
7645             if( rc ) goto find_indexes_out;
7646           }
7647           break;
7648         }
7649       }
7650
7651       if( zDetail[0]!='-' ){
7652         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7653       }
7654     }
7655
7656     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7657       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7658     }
7659
7660     idxFinalize(&rc, pExplain);
7661   }
7662
7663  find_indexes_out:
7664   idxHashClear(&hIdx);
7665   return rc;
7666 }
7667
7668 static int idxAuthCallback(
7669   void *pCtx,
7670   int eOp,
7671   const char *z3,
7672   const char *z4,
7673   const char *zDb,
7674   const char *zTrigger
7675 ){
7676   int rc = SQLITE_OK;
7677   (void)z4;
7678   (void)zTrigger;
7679   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7680     if( sqlite3_stricmp(zDb, "main")==0 ){
7681       sqlite3expert *p = (sqlite3expert*)pCtx;
7682       IdxTable *pTab;
7683       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7684         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7685       }
7686       if( pTab ){
7687         IdxWrite *pWrite;
7688         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7689           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7690         }
7691         if( pWrite==0 ){
7692           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7693           if( rc==SQLITE_OK ){
7694             pWrite->pTab = pTab;
7695             pWrite->eOp = eOp;
7696             pWrite->pNext = p->pWrite;
7697             p->pWrite = pWrite;
7698           }
7699         }
7700       }
7701     }
7702   }
7703   return rc;
7704 }
7705
7706 static int idxProcessOneTrigger(
7707   sqlite3expert *p, 
7708   IdxWrite *pWrite, 
7709   char **pzErr
7710 ){
7711   static const char *zInt = UNIQUE_TABLE_NAME;
7712   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7713   IdxTable *pTab = pWrite->pTab;
7714   const char *zTab = pTab->zName;
7715   const char *zSql = 
7716     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7717     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7718     "ORDER BY type;";
7719   sqlite3_stmt *pSelect = 0;
7720   int rc = SQLITE_OK;
7721   char *zWrite = 0;
7722
7723   /* Create the table and its triggers in the temp schema */
7724   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7725   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7726     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7727     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7728   }
7729   idxFinalize(&rc, pSelect);
7730
7731   /* Rename the table in the temp schema to zInt */
7732   if( rc==SQLITE_OK ){
7733     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7734     if( z==0 ){
7735       rc = SQLITE_NOMEM;
7736     }else{
7737       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7738       sqlite3_free(z);
7739     }
7740   }
7741
7742   switch( pWrite->eOp ){
7743     case SQLITE_INSERT: {
7744       int i;
7745       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7746       for(i=0; i<pTab->nCol; i++){
7747         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7748       }
7749       zWrite = idxAppendText(&rc, zWrite, ")");
7750       break;
7751     }
7752     case SQLITE_UPDATE: {
7753       int i;
7754       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7755       for(i=0; i<pTab->nCol; i++){
7756         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", 
7757             pTab->aCol[i].zName
7758         );
7759       }
7760       break;
7761     }
7762     default: {
7763       assert( pWrite->eOp==SQLITE_DELETE );
7764       if( rc==SQLITE_OK ){
7765         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7766         if( zWrite==0 ) rc = SQLITE_NOMEM;
7767       }
7768     }
7769   }
7770
7771   if( rc==SQLITE_OK ){
7772     sqlite3_stmt *pX = 0;
7773     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7774     idxFinalize(&rc, pX);
7775     if( rc!=SQLITE_OK ){
7776       idxDatabaseError(p->dbv, pzErr);
7777     }
7778   }
7779   sqlite3_free(zWrite);
7780
7781   if( rc==SQLITE_OK ){
7782     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7783   }
7784
7785   return rc;
7786 }
7787
7788 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7789   int rc = SQLITE_OK;
7790   IdxWrite *pEnd = 0;
7791   IdxWrite *pFirst = p->pWrite;
7792
7793   while( rc==SQLITE_OK && pFirst!=pEnd ){
7794     IdxWrite *pIter;
7795     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7796       rc = idxProcessOneTrigger(p, pIter, pzErr);
7797     }
7798     pEnd = pFirst;
7799     pFirst = p->pWrite;
7800   }
7801
7802   return rc;
7803 }
7804
7805
7806 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7807   int rc = idxRegisterVtab(p);
7808   sqlite3_stmt *pSchema = 0;
7809
7810   /* For each table in the main db schema:
7811   **
7812   **   1) Add an entry to the p->pTable list, and
7813   **   2) Create the equivalent virtual table in dbv.
7814   */
7815   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
7816       "SELECT type, name, sql, 1 FROM sqlite_master "
7817       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
7818       " UNION ALL "
7819       "SELECT type, name, sql, 2 FROM sqlite_master "
7820       "WHERE type = 'trigger'"
7821       "  AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
7822       "ORDER BY 4, 1"
7823   );
7824   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
7825     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
7826     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
7827     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
7828
7829     if( zType[0]=='v' || zType[1]=='r' ){
7830       rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
7831     }else{
7832       IdxTable *pTab;
7833       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
7834       if( rc==SQLITE_OK ){
7835         int i;
7836         char *zInner = 0;
7837         char *zOuter = 0;
7838         pTab->pNext = p->pTable;
7839         p->pTable = pTab;
7840
7841         /* The statement the vtab will pass to sqlite3_declare_vtab() */
7842         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
7843         for(i=0; i<pTab->nCol; i++){
7844           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", 
7845               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
7846           );
7847         }
7848         zInner = idxAppendText(&rc, zInner, ")");
7849
7850         /* The CVT statement to create the vtab */
7851         zOuter = idxAppendText(&rc, 0, 
7852             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
7853         );
7854         if( rc==SQLITE_OK ){
7855           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
7856         }
7857         sqlite3_free(zInner);
7858         sqlite3_free(zOuter);
7859       }
7860     }
7861   }
7862   idxFinalize(&rc, pSchema);
7863   return rc;
7864 }
7865
7866 struct IdxSampleCtx {
7867   int iTarget;
7868   double target;                  /* Target nRet/nRow value */
7869   double nRow;                    /* Number of rows seen */
7870   double nRet;                    /* Number of rows returned */
7871 };
7872
7873 static void idxSampleFunc(
7874   sqlite3_context *pCtx,
7875   int argc,
7876   sqlite3_value **argv
7877 ){
7878   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
7879   int bRet;
7880
7881   (void)argv;
7882   assert( argc==0 );
7883   if( p->nRow==0.0 ){
7884     bRet = 1;
7885   }else{
7886     bRet = (p->nRet / p->nRow) <= p->target;
7887     if( bRet==0 ){
7888       unsigned short rnd;
7889       sqlite3_randomness(2, (void*)&rnd);
7890       bRet = ((int)rnd % 100) <= p->iTarget;
7891     }
7892   }
7893
7894   sqlite3_result_int(pCtx, bRet);
7895   p->nRow += 1.0;
7896   p->nRet += (double)bRet;
7897 }
7898
7899 struct IdxRemCtx {
7900   int nSlot;
7901   struct IdxRemSlot {
7902     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
7903     i64 iVal;                     /* SQLITE_INTEGER value */
7904     double rVal;                  /* SQLITE_FLOAT value */
7905     int nByte;                    /* Bytes of space allocated at z */
7906     int n;                        /* Size of buffer z */
7907     char *z;                      /* SQLITE_TEXT/BLOB value */
7908   } aSlot[1];
7909 };
7910
7911 /*
7912 ** Implementation of scalar function rem().
7913 */
7914 static void idxRemFunc(
7915   sqlite3_context *pCtx,
7916   int argc,
7917   sqlite3_value **argv
7918 ){
7919   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
7920   struct IdxRemSlot *pSlot;
7921   int iSlot;
7922   assert( argc==2 );
7923
7924   iSlot = sqlite3_value_int(argv[0]);
7925   assert( iSlot<=p->nSlot );
7926   pSlot = &p->aSlot[iSlot];
7927
7928   switch( pSlot->eType ){
7929     case SQLITE_NULL:
7930       /* no-op */
7931       break;
7932
7933     case SQLITE_INTEGER:
7934       sqlite3_result_int64(pCtx, pSlot->iVal);
7935       break;
7936
7937     case SQLITE_FLOAT:
7938       sqlite3_result_double(pCtx, pSlot->rVal);
7939       break;
7940
7941     case SQLITE_BLOB:
7942       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7943       break;
7944
7945     case SQLITE_TEXT:
7946       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7947       break;
7948   }
7949
7950   pSlot->eType = sqlite3_value_type(argv[1]);
7951   switch( pSlot->eType ){
7952     case SQLITE_NULL:
7953       /* no-op */
7954       break;
7955
7956     case SQLITE_INTEGER:
7957       pSlot->iVal = sqlite3_value_int64(argv[1]);
7958       break;
7959
7960     case SQLITE_FLOAT:
7961       pSlot->rVal = sqlite3_value_double(argv[1]);
7962       break;
7963
7964     case SQLITE_BLOB:
7965     case SQLITE_TEXT: {
7966       int nByte = sqlite3_value_bytes(argv[1]);
7967       if( nByte>pSlot->nByte ){
7968         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
7969         if( zNew==0 ){
7970           sqlite3_result_error_nomem(pCtx);
7971           return;
7972         }
7973         pSlot->nByte = nByte*2;
7974         pSlot->z = zNew;
7975       }
7976       pSlot->n = nByte;
7977       if( pSlot->eType==SQLITE_BLOB ){
7978         memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
7979       }else{
7980         memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
7981       }
7982       break;
7983     }
7984   }
7985 }
7986
7987 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
7988   int rc = SQLITE_OK;
7989   const char *zMax = 
7990     "SELECT max(i.seqno) FROM "
7991     "  sqlite_master AS s, "
7992     "  pragma_index_list(s.name) AS l, "
7993     "  pragma_index_info(l.name) AS i "
7994     "WHERE s.type = 'table'";
7995   sqlite3_stmt *pMax = 0;
7996
7997   *pnMax = 0;
7998   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
7999   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8000     *pnMax = sqlite3_column_int(pMax, 0) + 1;
8001   }
8002   idxFinalize(&rc, pMax);
8003
8004   return rc;
8005 }
8006
8007 static int idxPopulateOneStat1(
8008   sqlite3expert *p,
8009   sqlite3_stmt *pIndexXInfo,
8010   sqlite3_stmt *pWriteStat,
8011   const char *zTab,
8012   const char *zIdx,
8013   char **pzErr
8014 ){
8015   char *zCols = 0;
8016   char *zOrder = 0;
8017   char *zQuery = 0;
8018   int nCol = 0;
8019   int i;
8020   sqlite3_stmt *pQuery = 0;
8021   int *aStat = 0;
8022   int rc = SQLITE_OK;
8023
8024   assert( p->iSample>0 );
8025
8026   /* Formulate the query text */
8027   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8028   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8029     const char *zComma = zCols==0 ? "" : ", ";
8030     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8031     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8032     zCols = idxAppendText(&rc, zCols, 
8033         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8034     );
8035     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8036   }
8037   sqlite3_reset(pIndexXInfo);
8038   if( rc==SQLITE_OK ){
8039     if( p->iSample==100 ){
8040       zQuery = sqlite3_mprintf(
8041           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8042       );
8043     }else{
8044       zQuery = sqlite3_mprintf(
8045           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8046       );
8047     }
8048   }
8049   sqlite3_free(zCols);
8050   sqlite3_free(zOrder);
8051
8052   /* Formulate the query text */
8053   if( rc==SQLITE_OK ){
8054     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8055     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8056   }
8057   sqlite3_free(zQuery);
8058
8059   if( rc==SQLITE_OK ){
8060     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8061   }
8062   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8063     IdxHashEntry *pEntry;
8064     char *zStat = 0;
8065     for(i=0; i<=nCol; i++) aStat[i] = 1;
8066     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8067       aStat[0]++;
8068       for(i=0; i<nCol; i++){
8069         if( sqlite3_column_int(pQuery, i)==0 ) break;
8070       }
8071       for(/*no-op*/; i<nCol; i++){
8072         aStat[i+1]++;
8073       }
8074     }
8075
8076     if( rc==SQLITE_OK ){
8077       int s0 = aStat[0];
8078       zStat = sqlite3_mprintf("%d", s0);
8079       if( zStat==0 ) rc = SQLITE_NOMEM;
8080       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8081         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8082       }
8083     }
8084
8085     if( rc==SQLITE_OK ){
8086       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8087       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8088       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8089       sqlite3_step(pWriteStat);
8090       rc = sqlite3_reset(pWriteStat);
8091     }
8092
8093     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8094     if( pEntry ){
8095       assert( pEntry->zVal2==0 );
8096       pEntry->zVal2 = zStat;
8097     }else{
8098       sqlite3_free(zStat);
8099     }
8100   }
8101   sqlite3_free(aStat);
8102   idxFinalize(&rc, pQuery);
8103
8104   return rc;
8105 }
8106
8107 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8108   int rc;
8109   char *zSql;
8110
8111   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8112   if( rc!=SQLITE_OK ) return rc;
8113
8114   zSql = sqlite3_mprintf(
8115       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8116   );
8117   if( zSql==0 ) return SQLITE_NOMEM;
8118   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8119   sqlite3_free(zSql);
8120
8121   return rc;
8122 }
8123
8124 /*
8125 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8126 ** indexes have already been created in database sqlite3expert.dbm, this
8127 ** function populates sqlite_stat1 table in the same database.
8128 **
8129 ** The stat1 data is generated by querying the 
8130 */
8131 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8132   int rc = SQLITE_OK;
8133   int nMax =0;
8134   struct IdxRemCtx *pCtx = 0;
8135   struct IdxSampleCtx samplectx; 
8136   int i;
8137   i64 iPrev = -100000;
8138   sqlite3_stmt *pAllIndex = 0;
8139   sqlite3_stmt *pIndexXInfo = 0;
8140   sqlite3_stmt *pWrite = 0;
8141
8142   const char *zAllIndex =
8143     "SELECT s.rowid, s.name, l.name FROM "
8144     "  sqlite_master AS s, "
8145     "  pragma_index_list(s.name) AS l "
8146     "WHERE s.type = 'table'";
8147   const char *zIndexXInfo = 
8148     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8149   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8150
8151   /* If iSample==0, no sqlite_stat1 data is required. */
8152   if( p->iSample==0 ) return SQLITE_OK;
8153
8154   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8155   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8156
8157   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8158
8159   if( rc==SQLITE_OK ){
8160     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8161     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8162   }
8163
8164   if( rc==SQLITE_OK ){
8165     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8166     rc = sqlite3_create_function(
8167         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8168     );
8169   }
8170   if( rc==SQLITE_OK ){
8171     rc = sqlite3_create_function(
8172         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8173     );
8174   }
8175
8176   if( rc==SQLITE_OK ){
8177     pCtx->nSlot = nMax+1;
8178     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8179   }
8180   if( rc==SQLITE_OK ){
8181     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8182   }
8183   if( rc==SQLITE_OK ){
8184     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8185   }
8186
8187   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8188     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8189     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8190     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8191     if( p->iSample<100 && iPrev!=iRowid ){
8192       samplectx.target = (double)p->iSample / 100.0;
8193       samplectx.iTarget = p->iSample;
8194       samplectx.nRow = 0.0;
8195       samplectx.nRet = 0.0;
8196       rc = idxBuildSampleTable(p, zTab);
8197       if( rc!=SQLITE_OK ) break;
8198     }
8199     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8200     iPrev = iRowid;
8201   }
8202   if( rc==SQLITE_OK && p->iSample<100 ){
8203     rc = sqlite3_exec(p->dbv, 
8204         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8205     );
8206   }
8207
8208   idxFinalize(&rc, pAllIndex);
8209   idxFinalize(&rc, pIndexXInfo);
8210   idxFinalize(&rc, pWrite);
8211
8212   for(i=0; i<pCtx->nSlot; i++){
8213     sqlite3_free(pCtx->aSlot[i].z);
8214   }
8215   sqlite3_free(pCtx);
8216
8217   if( rc==SQLITE_OK ){
8218     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8219   }
8220
8221   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8222   return rc;
8223 }
8224
8225 /*
8226 ** Allocate a new sqlite3expert object.
8227 */
8228 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8229   int rc = SQLITE_OK;
8230   sqlite3expert *pNew;
8231
8232   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8233
8234   /* Open two in-memory databases to work with. The "vtab database" (dbv)
8235   ** will contain a virtual table corresponding to each real table in
8236   ** the user database schema, and a copy of each view. It is used to
8237   ** collect information regarding the WHERE, ORDER BY and other clauses
8238   ** of the user's query.
8239   */
8240   if( rc==SQLITE_OK ){
8241     pNew->db = db;
8242     pNew->iSample = 100;
8243     rc = sqlite3_open(":memory:", &pNew->dbv);
8244   }
8245   if( rc==SQLITE_OK ){
8246     rc = sqlite3_open(":memory:", &pNew->dbm);
8247     if( rc==SQLITE_OK ){
8248       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8249     }
8250   }
8251   
8252
8253   /* Copy the entire schema of database [db] into [dbm]. */
8254   if( rc==SQLITE_OK ){
8255     sqlite3_stmt *pSql;
8256     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, 
8257         "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8258         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8259     );
8260     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8261       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8262       rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8263     }
8264     idxFinalize(&rc, pSql);
8265   }
8266
8267   /* Create the vtab schema */
8268   if( rc==SQLITE_OK ){
8269     rc = idxCreateVtabSchema(pNew, pzErrmsg);
8270   }
8271
8272   /* Register the auth callback with dbv */
8273   if( rc==SQLITE_OK ){
8274     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8275   }
8276
8277   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8278   ** return the new sqlite3expert handle.  */
8279   if( rc!=SQLITE_OK ){
8280     sqlite3_expert_destroy(pNew);
8281     pNew = 0;
8282   }
8283   return pNew;
8284 }
8285
8286 /*
8287 ** Configure an sqlite3expert object.
8288 */
8289 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8290   int rc = SQLITE_OK;
8291   va_list ap;
8292   va_start(ap, op);
8293   switch( op ){
8294     case EXPERT_CONFIG_SAMPLE: {
8295       int iVal = va_arg(ap, int);
8296       if( iVal<0 ) iVal = 0;
8297       if( iVal>100 ) iVal = 100;
8298       p->iSample = iVal;
8299       break;
8300     }
8301     default:
8302       rc = SQLITE_NOTFOUND;
8303       break;
8304   }
8305
8306   va_end(ap);
8307   return rc;
8308 }
8309
8310 /*
8311 ** Add an SQL statement to the analysis.
8312 */
8313 int sqlite3_expert_sql(
8314   sqlite3expert *p,               /* From sqlite3_expert_new() */
8315   const char *zSql,               /* SQL statement to add */
8316   char **pzErr                    /* OUT: Error message (if any) */
8317 ){
8318   IdxScan *pScanOrig = p->pScan;
8319   IdxStatement *pStmtOrig = p->pStatement;
8320   int rc = SQLITE_OK;
8321   const char *zStmt = zSql;
8322
8323   if( p->bRun ) return SQLITE_MISUSE;
8324
8325   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8326     sqlite3_stmt *pStmt = 0;
8327     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8328     if( rc==SQLITE_OK ){
8329       if( pStmt ){
8330         IdxStatement *pNew;
8331         const char *z = sqlite3_sql(pStmt);
8332         int n = STRLEN(z);
8333         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8334         if( rc==SQLITE_OK ){
8335           pNew->zSql = (char*)&pNew[1];
8336           memcpy(pNew->zSql, z, n+1);
8337           pNew->pNext = p->pStatement;
8338           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8339           p->pStatement = pNew;
8340         }
8341         sqlite3_finalize(pStmt);
8342       }
8343     }else{
8344       idxDatabaseError(p->dbv, pzErr);
8345     }
8346   }
8347
8348   if( rc!=SQLITE_OK ){
8349     idxScanFree(p->pScan, pScanOrig);
8350     idxStatementFree(p->pStatement, pStmtOrig);
8351     p->pScan = pScanOrig;
8352     p->pStatement = pStmtOrig;
8353   }
8354
8355   return rc;
8356 }
8357
8358 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8359   int rc;
8360   IdxHashEntry *pEntry;
8361
8362   /* Do trigger processing to collect any extra IdxScan structures */
8363   rc = idxProcessTriggers(p, pzErr);
8364
8365   /* Create candidate indexes within the in-memory database file */
8366   if( rc==SQLITE_OK ){
8367     rc = idxCreateCandidates(p);
8368   }
8369
8370   /* Generate the stat1 data */
8371   if( rc==SQLITE_OK ){
8372     rc = idxPopulateStat1(p, pzErr);
8373   }
8374
8375   /* Formulate the EXPERT_REPORT_CANDIDATES text */
8376   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8377     p->zCandidates = idxAppendText(&rc, p->zCandidates, 
8378         "%s;%s%s\n", pEntry->zVal, 
8379         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8380     );
8381   }
8382
8383   /* Figure out which of the candidate indexes are preferred by the query
8384   ** planner and report the results to the user.  */
8385   if( rc==SQLITE_OK ){
8386     rc = idxFindIndexes(p, pzErr);
8387   }
8388
8389   if( rc==SQLITE_OK ){
8390     p->bRun = 1;
8391   }
8392   return rc;
8393 }
8394
8395 /*
8396 ** Return the total number of statements that have been added to this
8397 ** sqlite3expert using sqlite3_expert_sql().
8398 */
8399 int sqlite3_expert_count(sqlite3expert *p){
8400   int nRet = 0;
8401   if( p->pStatement ) nRet = p->pStatement->iId+1;
8402   return nRet;
8403 }
8404
8405 /*
8406 ** Return a component of the report.
8407 */
8408 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8409   const char *zRet = 0;
8410   IdxStatement *pStmt;
8411
8412   if( p->bRun==0 ) return 0;
8413   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8414   switch( eReport ){
8415     case EXPERT_REPORT_SQL:
8416       if( pStmt ) zRet = pStmt->zSql;
8417       break;
8418     case EXPERT_REPORT_INDEXES:
8419       if( pStmt ) zRet = pStmt->zIdx;
8420       break;
8421     case EXPERT_REPORT_PLAN:
8422       if( pStmt ) zRet = pStmt->zEQP;
8423       break;
8424     case EXPERT_REPORT_CANDIDATES:
8425       zRet = p->zCandidates;
8426       break;
8427   }
8428   return zRet;
8429 }
8430
8431 /*
8432 ** Free an sqlite3expert object.
8433 */
8434 void sqlite3_expert_destroy(sqlite3expert *p){
8435   if( p ){
8436     sqlite3_close(p->dbm);
8437     sqlite3_close(p->dbv);
8438     idxScanFree(p->pScan, 0);
8439     idxStatementFree(p->pStatement, 0);
8440     idxTableFree(p->pTable);
8441     idxWriteFree(p->pWrite);
8442     idxHashClear(&p->hIdx);
8443     sqlite3_free(p->zCandidates);
8444     sqlite3_free(p);
8445   }
8446 }
8447
8448 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
8449
8450 /************************* End ../ext/expert/sqlite3expert.c ********************/
8451
8452 #if defined(SQLITE_ENABLE_SESSION)
8453 /*
8454 ** State information for a single open session
8455 */
8456 typedef struct OpenSession OpenSession;
8457 struct OpenSession {
8458   char *zName;             /* Symbolic name for this session */
8459   int nFilter;             /* Number of xFilter rejection GLOB patterns */
8460   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
8461   sqlite3_session *p;      /* The open session */
8462 };
8463 #endif
8464
8465 /*
8466 ** Shell output mode information from before ".explain on",
8467 ** saved so that it can be restored by ".explain off"
8468 */
8469 typedef struct SavedModeInfo SavedModeInfo;
8470 struct SavedModeInfo {
8471   int valid;          /* Is there legit data in here? */
8472   int mode;           /* Mode prior to ".explain on" */
8473   int showHeader;     /* The ".header" setting prior to ".explain on" */
8474   int colWidth[100];  /* Column widths prior to ".explain on" */
8475 };
8476
8477 typedef struct ExpertInfo ExpertInfo;
8478 struct ExpertInfo {
8479   sqlite3expert *pExpert;
8480   int bVerbose;
8481 };
8482
8483 /* A single line in the EQP output */
8484 typedef struct EQPGraphRow EQPGraphRow;
8485 struct EQPGraphRow {
8486   int iEqpId;           /* ID for this row */
8487   int iParentId;        /* ID of the parent row */
8488   EQPGraphRow *pNext;   /* Next row in sequence */
8489   char zText[1];        /* Text to display for this row */
8490 };
8491
8492 /* All EQP output is collected into an instance of the following */
8493 typedef struct EQPGraph EQPGraph;
8494 struct EQPGraph {
8495   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
8496   EQPGraphRow *pLast;   /* Last element of the pRow list */
8497   char zPrefix[100];    /* Graph prefix */
8498 };
8499
8500 /*
8501 ** State information about the database connection is contained in an
8502 ** instance of the following structure.
8503 */
8504 typedef struct ShellState ShellState;
8505 struct ShellState {
8506   sqlite3 *db;           /* The database */
8507   u8 autoExplain;        /* Automatically turn on .explain mode */
8508   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
8509   u8 autoEQPtest;        /* autoEQP is in test mode */
8510   u8 statsOn;            /* True to display memory stats before each finalize */
8511   u8 scanstatsOn;        /* True to display scan stats before each finalize */
8512   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
8513   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
8514   u8 nEqpLevel;          /* Depth of the EQP output graph */
8515   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
8516   int outCount;          /* Revert to stdout when reaching zero */
8517   int cnt;               /* Number of records displayed so far */
8518   FILE *out;             /* Write results here */
8519   FILE *traceOut;        /* Output for sqlite3_trace() */
8520   int nErr;              /* Number of errors seen */
8521   int mode;              /* An output mode setting */
8522   int modePrior;         /* Saved mode */
8523   int cMode;             /* temporary output mode for the current query */
8524   int normalMode;        /* Output mode before ".explain on" */
8525   int writableSchema;    /* True if PRAGMA writable_schema=ON */
8526   int showHeader;        /* True to show column names in List or Column mode */
8527   int nCheck;            /* Number of ".check" commands run */
8528   unsigned shellFlgs;    /* Various flags */
8529   char *zDestTable;      /* Name of destination table when MODE_Insert */
8530   char *zTempFile;       /* Temporary file that might need deleting */
8531   char zTestcase[30];    /* Name of current test case */
8532   char colSeparator[20]; /* Column separator character for several modes */
8533   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
8534   char colSepPrior[20];  /* Saved column separator */
8535   char rowSepPrior[20];  /* Saved row separator */
8536   int colWidth[100];     /* Requested width of each column when in column mode*/
8537   int actualWidth[100];  /* Actual width of each column */
8538   char nullValue[20];    /* The text to print when a NULL comes back from
8539                          ** the database */
8540   char outfile[FILENAME_MAX]; /* Filename for *out */
8541   const char *zDbFilename;    /* name of the database file */
8542   char *zFreeOnClose;         /* Filename to free when closing */
8543   const char *zVfs;           /* Name of VFS to use */
8544   sqlite3_stmt *pStmt;   /* Current statement if any. */
8545   FILE *pLog;            /* Write log output here */
8546   int *aiIndent;         /* Array of indents used in MODE_Explain */
8547   int nIndent;           /* Size of array aiIndent[] */
8548   int iIndent;           /* Index of current op in aiIndent[] */
8549   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
8550 #if defined(SQLITE_ENABLE_SESSION)
8551   int nSession;             /* Number of active sessions */
8552   OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
8553 #endif
8554   ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
8555 };
8556
8557
8558 /* Allowed values for ShellState.autoEQP
8559 */
8560 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
8561 #define AUTOEQP_on       1           /* Automatic EQP is on */
8562 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
8563 #define AUTOEQP_full     3           /* Show full EXPLAIN */
8564
8565 /* Allowed values for ShellState.openMode
8566 */
8567 #define SHELL_OPEN_UNSPEC     0      /* No open-mode specified */
8568 #define SHELL_OPEN_NORMAL     1      /* Normal database file */
8569 #define SHELL_OPEN_APPENDVFS  2      /* Use appendvfs */
8570 #define SHELL_OPEN_ZIPFILE    3      /* Use the zipfile virtual table */
8571 #define SHELL_OPEN_READONLY   4      /* Open a normal database read-only */
8572
8573 /*
8574 ** These are the allowed shellFlgs values
8575 */
8576 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
8577 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
8578 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
8579 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
8580 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
8581 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
8582 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
8583
8584 /*
8585 ** Macros for testing and setting shellFlgs
8586 */
8587 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
8588 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
8589 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
8590
8591 /*
8592 ** These are the allowed modes.
8593 */
8594 #define MODE_Line     0  /* One column per line.  Blank line between records */
8595 #define MODE_Column   1  /* One record per line in neat columns */
8596 #define MODE_List     2  /* One record per line with a separator */
8597 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
8598 #define MODE_Html     4  /* Generate an XHTML table */
8599 #define MODE_Insert   5  /* Generate SQL "insert" statements */
8600 #define MODE_Quote    6  /* Quote values as for SQL */
8601 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
8602 #define MODE_Csv      8  /* Quote strings, numbers are plain */
8603 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
8604 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
8605 #define MODE_Pretty  11  /* Pretty-print schemas */
8606 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
8607
8608 static const char *modeDescr[] = {
8609   "line",
8610   "column",
8611   "list",
8612   "semi",
8613   "html",
8614   "insert",
8615   "quote",
8616   "tcl",
8617   "csv",
8618   "explain",
8619   "ascii",
8620   "prettyprint",
8621   "eqp"
8622 };
8623
8624 /*
8625 ** These are the column/row/line separators used by the various
8626 ** import/export modes.
8627 */
8628 #define SEP_Column    "|"
8629 #define SEP_Row       "\n"
8630 #define SEP_Tab       "\t"
8631 #define SEP_Space     " "
8632 #define SEP_Comma     ","
8633 #define SEP_CrLf      "\r\n"
8634 #define SEP_Unit      "\x1F"
8635 #define SEP_Record    "\x1E"
8636
8637 /*
8638 ** A callback for the sqlite3_log() interface.
8639 */
8640 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
8641   ShellState *p = (ShellState*)pArg;
8642   if( p->pLog==0 ) return;
8643   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
8644   fflush(p->pLog);
8645 }
8646
8647 /*
8648 ** SQL function:  shell_putsnl(X)
8649 **
8650 ** Write the text X to the screen (or whatever output is being directed)
8651 ** adding a newline at the end, and then return X.
8652 */
8653 static void shellPutsFunc(
8654   sqlite3_context *pCtx,
8655   int nVal,
8656   sqlite3_value **apVal
8657 ){
8658   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
8659   (void)nVal;
8660   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
8661   sqlite3_result_value(pCtx, apVal[0]);
8662 }
8663
8664 /*
8665 ** SQL function:   edit(VALUE)
8666 **                 edit(VALUE,EDITOR)
8667 **
8668 ** These steps:
8669 **
8670 **     (1) Write VALUE into a temporary file.
8671 **     (2) Run program EDITOR on that temporary file.
8672 **     (3) Read the temporary file back and return its content as the result.
8673 **     (4) Delete the temporary file
8674 **
8675 ** If the EDITOR argument is omitted, use the value in the VISUAL
8676 ** environment variable.  If still there is no EDITOR, through an error.
8677 **
8678 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8679 */
8680 #ifndef SQLITE_NOHAVE_SYSTEM
8681 static void editFunc(
8682   sqlite3_context *context,
8683   int argc,
8684   sqlite3_value **argv
8685 ){
8686   const char *zEditor;
8687   char *zTempFile = 0;
8688   sqlite3 *db;
8689   char *zCmd = 0;
8690   int bBin;
8691   int rc;
8692   FILE *f = 0;
8693   sqlite3_int64 sz;
8694   sqlite3_int64 x;
8695   unsigned char *p = 0;
8696
8697   if( argc==2 ){
8698     zEditor = (const char*)sqlite3_value_text(argv[1]);
8699   }else{
8700     zEditor = getenv("VISUAL");
8701   }
8702   if( zEditor==0 ){
8703     sqlite3_result_error(context, "no editor for edit()", -1);
8704     return;
8705   }
8706   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
8707     sqlite3_result_error(context, "NULL input to edit()", -1);
8708     return;
8709   }
8710   db = sqlite3_context_db_handle(context);
8711   zTempFile = 0;
8712   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
8713   if( zTempFile==0 ){
8714     sqlite3_uint64 r = 0;
8715     sqlite3_randomness(sizeof(r), &r);
8716     zTempFile = sqlite3_mprintf("temp%llx", r);
8717     if( zTempFile==0 ){
8718       sqlite3_result_error_nomem(context);
8719       return;
8720     }
8721   }
8722   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
8723   f = fopen(zTempFile, bBin ? "wb" : "w");
8724   if( f==0 ){
8725     sqlite3_result_error(context, "edit() cannot open temp file", -1);
8726     goto edit_func_end;
8727   }
8728   sz = sqlite3_value_bytes(argv[0]);
8729   if( bBin ){
8730     x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
8731   }else{
8732     x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
8733   }
8734   fclose(f);
8735   f = 0;
8736   if( x!=sz ){
8737     sqlite3_result_error(context, "edit() could not write the whole file", -1);
8738     goto edit_func_end;
8739   }
8740   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
8741   if( zCmd==0 ){
8742     sqlite3_result_error_nomem(context);
8743     goto edit_func_end;
8744   }
8745   rc = system(zCmd);
8746   sqlite3_free(zCmd);
8747   if( rc ){
8748     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
8749     goto edit_func_end;
8750   }
8751   f = fopen(zTempFile, bBin ? "rb" : "r");
8752   if( f==0 ){
8753     sqlite3_result_error(context,
8754       "edit() cannot reopen temp file after edit", -1);
8755     goto edit_func_end;
8756   }
8757   fseek(f, 0, SEEK_END);
8758   sz = ftell(f);
8759   rewind(f);
8760   p = sqlite3_malloc64( sz+(bBin==0) );
8761   if( p==0 ){
8762     sqlite3_result_error_nomem(context);
8763     goto edit_func_end;
8764   }
8765   if( bBin ){
8766     x = fread(p, 1, sz, f);
8767   }else{
8768     x = fread(p, 1, sz, f);
8769     p[sz] = 0;
8770   }
8771   fclose(f);
8772   f = 0;
8773   if( x!=sz ){
8774     sqlite3_result_error(context, "could not read back the whole file", -1);
8775     goto edit_func_end;
8776   }
8777   if( bBin ){
8778     sqlite3_result_blob64(context, p, sz, sqlite3_free);
8779   }else{
8780     sqlite3_result_text64(context, (const char*)p, sz,
8781                           sqlite3_free, SQLITE_UTF8);
8782   }
8783   p = 0;
8784
8785 edit_func_end:
8786   if( f ) fclose(f);
8787   unlink(zTempFile);
8788   sqlite3_free(zTempFile);
8789   sqlite3_free(p);
8790 }
8791 #endif /* SQLITE_NOHAVE_SYSTEM */
8792
8793 /*
8794 ** Save or restore the current output mode
8795 */
8796 static void outputModePush(ShellState *p){
8797   p->modePrior = p->mode;
8798   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
8799   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
8800 }
8801 static void outputModePop(ShellState *p){
8802   p->mode = p->modePrior;
8803   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
8804   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
8805 }
8806
8807 /*
8808 ** Output the given string as a hex-encoded blob (eg. X'1234' )
8809 */
8810 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
8811   int i;
8812   char *zBlob = (char *)pBlob;
8813   raw_printf(out,"X'");
8814   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
8815   raw_printf(out,"'");
8816 }
8817
8818 /*
8819 ** Find a string that is not found anywhere in z[].  Return a pointer
8820 ** to that string.
8821 **
8822 ** Try to use zA and zB first.  If both of those are already found in z[]
8823 ** then make up some string and store it in the buffer zBuf.
8824 */
8825 static const char *unused_string(
8826   const char *z,                    /* Result must not appear anywhere in z */
8827   const char *zA, const char *zB,   /* Try these first */
8828   char *zBuf                        /* Space to store a generated string */
8829 ){
8830   unsigned i = 0;
8831   if( strstr(z, zA)==0 ) return zA;
8832   if( strstr(z, zB)==0 ) return zB;
8833   do{
8834     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
8835   }while( strstr(z,zBuf)!=0 );
8836   return zBuf;
8837 }
8838
8839 /*
8840 ** Output the given string as a quoted string using SQL quoting conventions.
8841 **
8842 ** See also: output_quoted_escaped_string()
8843 */
8844 static void output_quoted_string(FILE *out, const char *z){
8845   int i;
8846   char c;
8847   setBinaryMode(out, 1);
8848   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8849   if( c==0 ){
8850     utf8_printf(out,"'%s'",z);
8851   }else{
8852     raw_printf(out, "'");
8853     while( *z ){
8854       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8855       if( c=='\'' ) i++;
8856       if( i ){
8857         utf8_printf(out, "%.*s", i, z);
8858         z += i;
8859       }
8860       if( c=='\'' ){
8861         raw_printf(out, "'");
8862         continue;
8863       }
8864       if( c==0 ){
8865         break;
8866       }
8867       z++;
8868     }
8869     raw_printf(out, "'");
8870   }
8871   setTextMode(out, 1);
8872 }
8873
8874 /*
8875 ** Output the given string as a quoted string using SQL quoting conventions.
8876 ** Additionallly , escape the "\n" and "\r" characters so that they do not
8877 ** get corrupted by end-of-line translation facilities in some operating
8878 ** systems.
8879 **
8880 ** This is like output_quoted_string() but with the addition of the \r\n
8881 ** escape mechanism.
8882 */
8883 static void output_quoted_escaped_string(FILE *out, const char *z){
8884   int i;
8885   char c;
8886   setBinaryMode(out, 1);
8887   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
8888   if( c==0 ){
8889     utf8_printf(out,"'%s'",z);
8890   }else{
8891     const char *zNL = 0;
8892     const char *zCR = 0;
8893     int nNL = 0;
8894     int nCR = 0;
8895     char zBuf1[20], zBuf2[20];
8896     for(i=0; z[i]; i++){
8897       if( z[i]=='\n' ) nNL++;
8898       if( z[i]=='\r' ) nCR++;
8899     }
8900     if( nNL ){
8901       raw_printf(out, "replace(");
8902       zNL = unused_string(z, "\\n", "\\012", zBuf1);
8903     }
8904     if( nCR ){
8905       raw_printf(out, "replace(");
8906       zCR = unused_string(z, "\\r", "\\015", zBuf2);
8907     }
8908     raw_printf(out, "'");
8909     while( *z ){
8910       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
8911       if( c=='\'' ) i++;
8912       if( i ){
8913         utf8_printf(out, "%.*s", i, z);
8914         z += i;
8915       }
8916       if( c=='\'' ){
8917         raw_printf(out, "'");
8918         continue;
8919       }
8920       if( c==0 ){
8921         break;
8922       }
8923       z++;
8924       if( c=='\n' ){
8925         raw_printf(out, "%s", zNL);
8926         continue;
8927       }
8928       raw_printf(out, "%s", zCR);
8929     }
8930     raw_printf(out, "'");
8931     if( nCR ){
8932       raw_printf(out, ",'%s',char(13))", zCR);
8933     }
8934     if( nNL ){
8935       raw_printf(out, ",'%s',char(10))", zNL);
8936     }
8937   }
8938   setTextMode(out, 1);
8939 }
8940
8941 /*
8942 ** Output the given string as a quoted according to C or TCL quoting rules.
8943 */
8944 static void output_c_string(FILE *out, const char *z){
8945   unsigned int c;
8946   fputc('"', out);
8947   while( (c = *(z++))!=0 ){
8948     if( c=='\\' ){
8949       fputc(c, out);
8950       fputc(c, out);
8951     }else if( c=='"' ){
8952       fputc('\\', out);
8953       fputc('"', out);
8954     }else if( c=='\t' ){
8955       fputc('\\', out);
8956       fputc('t', out);
8957     }else if( c=='\n' ){
8958       fputc('\\', out);
8959       fputc('n', out);
8960     }else if( c=='\r' ){
8961       fputc('\\', out);
8962       fputc('r', out);
8963     }else if( !isprint(c&0xff) ){
8964       raw_printf(out, "\\%03o", c&0xff);
8965     }else{
8966       fputc(c, out);
8967     }
8968   }
8969   fputc('"', out);
8970 }
8971
8972 /*
8973 ** Output the given string with characters that are special to
8974 ** HTML escaped.
8975 */
8976 static void output_html_string(FILE *out, const char *z){
8977   int i;
8978   if( z==0 ) z = "";
8979   while( *z ){
8980     for(i=0;   z[i]
8981             && z[i]!='<'
8982             && z[i]!='&'
8983             && z[i]!='>'
8984             && z[i]!='\"'
8985             && z[i]!='\'';
8986         i++){}
8987     if( i>0 ){
8988       utf8_printf(out,"%.*s",i,z);
8989     }
8990     if( z[i]=='<' ){
8991       raw_printf(out,"&lt;");
8992     }else if( z[i]=='&' ){
8993       raw_printf(out,"&amp;");
8994     }else if( z[i]=='>' ){
8995       raw_printf(out,"&gt;");
8996     }else if( z[i]=='\"' ){
8997       raw_printf(out,"&quot;");
8998     }else if( z[i]=='\'' ){
8999       raw_printf(out,"&#39;");
9000     }else{
9001       break;
9002     }
9003     z += i + 1;
9004   }
9005 }
9006
9007 /*
9008 ** If a field contains any character identified by a 1 in the following
9009 ** array, then the string must be quoted for CSV.
9010 */
9011 static const char needCsvQuote[] = {
9012   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9013   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9014   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
9015   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9016   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9017   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9018   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
9019   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
9020   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9021   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9022   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9023   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9024   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9025   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9026   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9027   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
9028 };
9029
9030 /*
9031 ** Output a single term of CSV.  Actually, p->colSeparator is used for
9032 ** the separator, which may or may not be a comma.  p->nullValue is
9033 ** the null value.  Strings are quoted if necessary.  The separator
9034 ** is only issued if bSep is true.
9035 */
9036 static void output_csv(ShellState *p, const char *z, int bSep){
9037   FILE *out = p->out;
9038   if( z==0 ){
9039     utf8_printf(out,"%s",p->nullValue);
9040   }else{
9041     int i;
9042     int nSep = strlen30(p->colSeparator);
9043     for(i=0; z[i]; i++){
9044       if( needCsvQuote[((unsigned char*)z)[i]]
9045          || (z[i]==p->colSeparator[0] &&
9046              (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
9047         i = 0;
9048         break;
9049       }
9050     }
9051     if( i==0 ){
9052       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
9053       utf8_printf(out, "%s", zQuoted);
9054       sqlite3_free(zQuoted);
9055     }else{
9056       utf8_printf(out, "%s", z);
9057     }
9058   }
9059   if( bSep ){
9060     utf8_printf(p->out, "%s", p->colSeparator);
9061   }
9062 }
9063
9064 /*
9065 ** This routine runs when the user presses Ctrl-C
9066 */
9067 static void interrupt_handler(int NotUsed){
9068   UNUSED_PARAMETER(NotUsed);
9069   seenInterrupt++;
9070   if( seenInterrupt>2 ) exit(1);
9071   if( globalDb ) sqlite3_interrupt(globalDb);
9072 }
9073
9074 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
9075 /*
9076 ** This routine runs for console events (e.g. Ctrl-C) on Win32
9077 */
9078 static BOOL WINAPI ConsoleCtrlHandler(
9079   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
9080 ){
9081   if( dwCtrlType==CTRL_C_EVENT ){
9082     interrupt_handler(0);
9083     return TRUE;
9084   }
9085   return FALSE;
9086 }
9087 #endif
9088
9089 #ifndef SQLITE_OMIT_AUTHORIZATION
9090 /*
9091 ** When the ".auth ON" is set, the following authorizer callback is
9092 ** invoked.  It always returns SQLITE_OK.
9093 */
9094 static int shellAuth(
9095   void *pClientData,
9096   int op,
9097   const char *zA1,
9098   const char *zA2,
9099   const char *zA3,
9100   const char *zA4
9101 ){
9102   ShellState *p = (ShellState*)pClientData;
9103   static const char *azAction[] = { 0,
9104      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
9105      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
9106      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
9107      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
9108      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
9109      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
9110      "PRAGMA",               "READ",                 "SELECT",
9111      "TRANSACTION",          "UPDATE",               "ATTACH",
9112      "DETACH",               "ALTER_TABLE",          "REINDEX",
9113      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
9114      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
9115   };
9116   int i;
9117   const char *az[4];
9118   az[0] = zA1;
9119   az[1] = zA2;
9120   az[2] = zA3;
9121   az[3] = zA4;
9122   utf8_printf(p->out, "authorizer: %s", azAction[op]);
9123   for(i=0; i<4; i++){
9124     raw_printf(p->out, " ");
9125     if( az[i] ){
9126       output_c_string(p->out, az[i]);
9127     }else{
9128       raw_printf(p->out, "NULL");
9129     }
9130   }
9131   raw_printf(p->out, "\n");
9132   return SQLITE_OK;
9133 }
9134 #endif
9135
9136 /*
9137 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
9138 **
9139 ** This routine converts some CREATE TABLE statements for shadow tables
9140 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
9141 */
9142 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
9143   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
9144     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
9145   }else{
9146     utf8_printf(out, "%s%s", z, zTail);
9147   }
9148 }
9149 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
9150   char c = z[n];
9151   z[n] = 0;
9152   printSchemaLine(out, z, zTail);
9153   z[n] = c;
9154 }
9155
9156 /*
9157 ** Return true if string z[] has nothing but whitespace and comments to the
9158 ** end of the first line.
9159 */
9160 static int wsToEol(const char *z){
9161   int i;
9162   for(i=0; z[i]; i++){
9163     if( z[i]=='\n' ) return 1;
9164     if( IsSpace(z[i]) ) continue;
9165     if( z[i]=='-' && z[i+1]=='-' ) return 1;
9166     return 0;
9167   }
9168   return 1;
9169 }
9170
9171 /*
9172 ** Add a new entry to the EXPLAIN QUERY PLAN data
9173 */
9174 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
9175   EQPGraphRow *pNew;
9176   int nText = strlen30(zText);
9177   if( p->autoEQPtest ){
9178     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
9179   }
9180   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
9181   if( pNew==0 ) shell_out_of_memory();
9182   pNew->iEqpId = iEqpId;
9183   pNew->iParentId = p2;
9184   memcpy(pNew->zText, zText, nText+1);
9185   pNew->pNext = 0;
9186   if( p->sGraph.pLast ){
9187     p->sGraph.pLast->pNext = pNew;
9188   }else{
9189     p->sGraph.pRow = pNew;
9190   }
9191   p->sGraph.pLast = pNew;
9192 }
9193
9194 /*
9195 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
9196 ** in p->sGraph.
9197 */
9198 static void eqp_reset(ShellState *p){
9199   EQPGraphRow *pRow, *pNext;
9200   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
9201     pNext = pRow->pNext;
9202     sqlite3_free(pRow);
9203   }
9204   memset(&p->sGraph, 0, sizeof(p->sGraph));
9205 }
9206
9207 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
9208 ** pOld, or return the first such line if pOld is NULL
9209 */
9210 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
9211   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9212   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
9213   return pRow;
9214 }
9215
9216 /* Render a single level of the graph that has iEqpId as its parent.  Called
9217 ** recursively to render sublevels.
9218 */
9219 static void eqp_render_level(ShellState *p, int iEqpId){
9220   EQPGraphRow *pRow, *pNext;
9221   int n = strlen30(p->sGraph.zPrefix);
9222   char *z;
9223   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
9224     pNext = eqp_next_row(p, iEqpId, pRow);
9225     z = pRow->zText;
9226     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9227     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
9228       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
9229       eqp_render_level(p, pRow->iEqpId);
9230       p->sGraph.zPrefix[n] = 0;
9231     }
9232   }
9233 }
9234
9235 /*
9236 ** Display and reset the EXPLAIN QUERY PLAN data
9237 */
9238 static void eqp_render(ShellState *p){
9239   EQPGraphRow *pRow = p->sGraph.pRow;
9240   if( pRow ){
9241     if( pRow->zText[0]=='-' ){
9242       if( pRow->pNext==0 ){
9243         eqp_reset(p);
9244         return;
9245       }
9246       utf8_printf(p->out, "%s\n", pRow->zText+3);
9247       p->sGraph.pRow = pRow->pNext;
9248       sqlite3_free(pRow);
9249     }else{
9250       utf8_printf(p->out, "QUERY PLAN\n");
9251     }
9252     p->sGraph.zPrefix[0] = 0;
9253     eqp_render_level(p, 0);
9254     eqp_reset(p);
9255   }
9256 }
9257
9258 /*
9259 ** This is the callback routine that the shell
9260 ** invokes for each row of a query result.
9261 */
9262 static int shell_callback(
9263   void *pArg,
9264   int nArg,        /* Number of result columns */
9265   char **azArg,    /* Text of each result column */
9266   char **azCol,    /* Column names */
9267   int *aiType      /* Column types */
9268 ){
9269   int i;
9270   ShellState *p = (ShellState*)pArg;
9271
9272   if( azArg==0 ) return 0;
9273   switch( p->cMode ){
9274     case MODE_Line: {
9275       int w = 5;
9276       if( azArg==0 ) break;
9277       for(i=0; i<nArg; i++){
9278         int len = strlen30(azCol[i] ? azCol[i] : "");
9279         if( len>w ) w = len;
9280       }
9281       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
9282       for(i=0; i<nArg; i++){
9283         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
9284                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
9285       }
9286       break;
9287     }
9288     case MODE_Explain:
9289     case MODE_Column: {
9290       static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
9291       const int *colWidth;
9292       int showHdr;
9293       char *rowSep;
9294       if( p->cMode==MODE_Column ){
9295         colWidth = p->colWidth;
9296         showHdr = p->showHeader;
9297         rowSep = p->rowSeparator;
9298       }else{
9299         colWidth = aExplainWidths;
9300         showHdr = 1;
9301         rowSep = SEP_Row;
9302       }
9303       if( p->cnt++==0 ){
9304         for(i=0; i<nArg; i++){
9305           int w, n;
9306           if( i<ArraySize(p->colWidth) ){
9307             w = colWidth[i];
9308           }else{
9309             w = 0;
9310           }
9311           if( w==0 ){
9312             w = strlenChar(azCol[i] ? azCol[i] : "");
9313             if( w<10 ) w = 10;
9314             n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
9315             if( w<n ) w = n;
9316           }
9317           if( i<ArraySize(p->actualWidth) ){
9318             p->actualWidth[i] = w;
9319           }
9320           if( showHdr ){
9321             utf8_width_print(p->out, w, azCol[i]);
9322             utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
9323           }
9324         }
9325         if( showHdr ){
9326           for(i=0; i<nArg; i++){
9327             int w;
9328             if( i<ArraySize(p->actualWidth) ){
9329                w = p->actualWidth[i];
9330                if( w<0 ) w = -w;
9331             }else{
9332                w = 10;
9333             }
9334             utf8_printf(p->out,"%-*.*s%s",w,w,
9335                    "----------------------------------------------------------"
9336                    "----------------------------------------------------------",
9337                     i==nArg-1 ? rowSep : "  ");
9338           }
9339         }
9340       }
9341       if( azArg==0 ) break;
9342       for(i=0; i<nArg; i++){
9343         int w;
9344         if( i<ArraySize(p->actualWidth) ){
9345            w = p->actualWidth[i];
9346         }else{
9347            w = 10;
9348         }
9349         if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
9350           w = strlenChar(azArg[i]);
9351         }
9352         if( i==1 && p->aiIndent && p->pStmt ){
9353           if( p->iIndent<p->nIndent ){
9354             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
9355           }
9356           p->iIndent++;
9357         }
9358         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
9359         utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
9360       }
9361       break;
9362     }
9363     case MODE_Semi: {   /* .schema and .fullschema output */
9364       printSchemaLine(p->out, azArg[0], ";\n");
9365       break;
9366     }
9367     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
9368       char *z;
9369       int j;
9370       int nParen = 0;
9371       char cEnd = 0;
9372       char c;
9373       int nLine = 0;
9374       assert( nArg==1 );
9375       if( azArg[0]==0 ) break;
9376       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
9377        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
9378       ){
9379         utf8_printf(p->out, "%s;\n", azArg[0]);
9380         break;
9381       }
9382       z = sqlite3_mprintf("%s", azArg[0]);
9383       j = 0;
9384       for(i=0; IsSpace(z[i]); i++){}
9385       for(; (c = z[i])!=0; i++){
9386         if( IsSpace(c) ){
9387           if( z[j-1]=='\r' ) z[j-1] = '\n';
9388           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
9389         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
9390           j--;
9391         }
9392         z[j++] = c;
9393       }
9394       while( j>0 && IsSpace(z[j-1]) ){ j--; }
9395       z[j] = 0;
9396       if( strlen30(z)>=79 ){
9397         for(i=j=0; (c = z[i])!=0; i++){  /* Copy changes from z[i] back to z[j] */
9398           if( c==cEnd ){
9399             cEnd = 0;
9400           }else if( c=='"' || c=='\'' || c=='`' ){
9401             cEnd = c;
9402           }else if( c=='[' ){
9403             cEnd = ']';
9404           }else if( c=='-' && z[i+1]=='-' ){
9405             cEnd = '\n';
9406           }else if( c=='(' ){
9407             nParen++;
9408           }else if( c==')' ){
9409             nParen--;
9410             if( nLine>0 && nParen==0 && j>0 ){
9411               printSchemaLineN(p->out, z, j, "\n");
9412               j = 0;
9413             }
9414           }
9415           z[j++] = c;
9416           if( nParen==1 && cEnd==0
9417            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
9418           ){
9419             if( c=='\n' ) j--;
9420             printSchemaLineN(p->out, z, j, "\n  ");
9421             j = 0;
9422             nLine++;
9423             while( IsSpace(z[i+1]) ){ i++; }
9424           }
9425         }
9426         z[j] = 0;
9427       }
9428       printSchemaLine(p->out, z, ";\n");
9429       sqlite3_free(z);
9430       break;
9431     }
9432     case MODE_List: {
9433       if( p->cnt++==0 && p->showHeader ){
9434         for(i=0; i<nArg; i++){
9435           utf8_printf(p->out,"%s%s",azCol[i],
9436                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
9437         }
9438       }
9439       if( azArg==0 ) break;
9440       for(i=0; i<nArg; i++){
9441         char *z = azArg[i];
9442         if( z==0 ) z = p->nullValue;
9443         utf8_printf(p->out, "%s", z);
9444         if( i<nArg-1 ){
9445           utf8_printf(p->out, "%s", p->colSeparator);
9446         }else{
9447           utf8_printf(p->out, "%s", p->rowSeparator);
9448         }
9449       }
9450       break;
9451     }
9452     case MODE_Html: {
9453       if( p->cnt++==0 && p->showHeader ){
9454         raw_printf(p->out,"<TR>");
9455         for(i=0; i<nArg; i++){
9456           raw_printf(p->out,"<TH>");
9457           output_html_string(p->out, azCol[i]);
9458           raw_printf(p->out,"</TH>\n");
9459         }
9460         raw_printf(p->out,"</TR>\n");
9461       }
9462       if( azArg==0 ) break;
9463       raw_printf(p->out,"<TR>");
9464       for(i=0; i<nArg; i++){
9465         raw_printf(p->out,"<TD>");
9466         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9467         raw_printf(p->out,"</TD>\n");
9468       }
9469       raw_printf(p->out,"</TR>\n");
9470       break;
9471     }
9472     case MODE_Tcl: {
9473       if( p->cnt++==0 && p->showHeader ){
9474         for(i=0; i<nArg; i++){
9475           output_c_string(p->out,azCol[i] ? azCol[i] : "");
9476           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9477         }
9478         utf8_printf(p->out, "%s", p->rowSeparator);
9479       }
9480       if( azArg==0 ) break;
9481       for(i=0; i<nArg; i++){
9482         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9483         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9484       }
9485       utf8_printf(p->out, "%s", p->rowSeparator);
9486       break;
9487     }
9488     case MODE_Csv: {
9489       setBinaryMode(p->out, 1);
9490       if( p->cnt++==0 && p->showHeader ){
9491         for(i=0; i<nArg; i++){
9492           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
9493         }
9494         utf8_printf(p->out, "%s", p->rowSeparator);
9495       }
9496       if( nArg>0 ){
9497         for(i=0; i<nArg; i++){
9498           output_csv(p, azArg[i], i<nArg-1);
9499         }
9500         utf8_printf(p->out, "%s", p->rowSeparator);
9501       }
9502       setTextMode(p->out, 1);
9503       break;
9504     }
9505     case MODE_Insert: {
9506       if( azArg==0 ) break;
9507       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
9508       if( p->showHeader ){
9509         raw_printf(p->out,"(");
9510         for(i=0; i<nArg; i++){
9511           if( i>0 ) raw_printf(p->out, ",");
9512           if( quoteChar(azCol[i]) ){
9513             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
9514             utf8_printf(p->out, "%s", z);
9515             sqlite3_free(z);
9516           }else{
9517             raw_printf(p->out, "%s", azCol[i]);
9518           }
9519         }
9520         raw_printf(p->out,")");
9521       }
9522       p->cnt++;
9523       for(i=0; i<nArg; i++){
9524         raw_printf(p->out, i>0 ? "," : " VALUES(");
9525         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9526           utf8_printf(p->out,"NULL");
9527         }else if( aiType && aiType[i]==SQLITE_TEXT ){
9528           if( ShellHasFlag(p, SHFLG_Newlines) ){
9529             output_quoted_string(p->out, azArg[i]);
9530           }else{
9531             output_quoted_escaped_string(p->out, azArg[i]);
9532           }
9533         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9534           utf8_printf(p->out,"%s", azArg[i]);
9535         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9536           char z[50];
9537           double r = sqlite3_column_double(p->pStmt, i);
9538           sqlite3_snprintf(50,z,"%!.20g", r);
9539           raw_printf(p->out, "%s", z);
9540         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9541           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9542           int nBlob = sqlite3_column_bytes(p->pStmt, i);
9543           output_hex_blob(p->out, pBlob, nBlob);
9544         }else if( isNumber(azArg[i], 0) ){
9545           utf8_printf(p->out,"%s", azArg[i]);
9546         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
9547           output_quoted_string(p->out, azArg[i]);
9548         }else{
9549           output_quoted_escaped_string(p->out, azArg[i]);
9550         }
9551       }
9552       raw_printf(p->out,");\n");
9553       break;
9554     }
9555     case MODE_Quote: {
9556       if( azArg==0 ) break;
9557       if( p->cnt==0 && p->showHeader ){
9558         for(i=0; i<nArg; i++){
9559           if( i>0 ) raw_printf(p->out, ",");
9560           output_quoted_string(p->out, azCol[i]);
9561         }
9562         raw_printf(p->out,"\n");
9563       }
9564       p->cnt++;
9565       for(i=0; i<nArg; i++){
9566         if( i>0 ) raw_printf(p->out, ",");
9567         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9568           utf8_printf(p->out,"NULL");
9569         }else if( aiType && aiType[i]==SQLITE_TEXT ){
9570           output_quoted_string(p->out, azArg[i]);
9571         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9572           utf8_printf(p->out,"%s", azArg[i]);
9573         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9574           char z[50];
9575           double r = sqlite3_column_double(p->pStmt, i);
9576           sqlite3_snprintf(50,z,"%!.20g", r);
9577           raw_printf(p->out, "%s", z);
9578         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9579           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9580           int nBlob = sqlite3_column_bytes(p->pStmt, i);
9581           output_hex_blob(p->out, pBlob, nBlob);
9582         }else if( isNumber(azArg[i], 0) ){
9583           utf8_printf(p->out,"%s", azArg[i]);
9584         }else{
9585           output_quoted_string(p->out, azArg[i]);
9586         }
9587       }
9588       raw_printf(p->out,"\n");
9589       break;
9590     }
9591     case MODE_Ascii: {
9592       if( p->cnt++==0 && p->showHeader ){
9593         for(i=0; i<nArg; i++){
9594           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9595           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
9596         }
9597         utf8_printf(p->out, "%s", p->rowSeparator);
9598       }
9599       if( azArg==0 ) break;
9600       for(i=0; i<nArg; i++){
9601         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9602         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
9603       }
9604       utf8_printf(p->out, "%s", p->rowSeparator);
9605       break;
9606     }
9607     case MODE_EQP: {
9608       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
9609       break;
9610     }
9611   }
9612   return 0;
9613 }
9614
9615 /*
9616 ** This is the callback routine that the SQLite library
9617 ** invokes for each row of a query result.
9618 */
9619 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
9620   /* since we don't have type info, call the shell_callback with a NULL value */
9621   return shell_callback(pArg, nArg, azArg, azCol, NULL);
9622 }
9623
9624 /*
9625 ** This is the callback routine from sqlite3_exec() that appends all
9626 ** output onto the end of a ShellText object.
9627 */
9628 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
9629   ShellText *p = (ShellText*)pArg;
9630   int i;
9631   UNUSED_PARAMETER(az);
9632   if( azArg==0 ) return 0;
9633   if( p->n ) appendText(p, "|", 0);
9634   for(i=0; i<nArg; i++){
9635     if( i ) appendText(p, ",", 0);
9636     if( azArg[i] ) appendText(p, azArg[i], 0);
9637   }
9638   return 0;
9639 }
9640
9641 /*
9642 ** Generate an appropriate SELFTEST table in the main database.
9643 */
9644 static void createSelftestTable(ShellState *p){
9645   char *zErrMsg = 0;
9646   sqlite3_exec(p->db,
9647     "SAVEPOINT selftest_init;\n"
9648     "CREATE TABLE IF NOT EXISTS selftest(\n"
9649     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
9650     "  op TEXT,\n"                   /* Operator:  memo run */
9651     "  cmd TEXT,\n"                  /* Command text */
9652     "  ans TEXT\n"                   /* Desired answer */
9653     ");"
9654     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
9655     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
9656     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
9657     "         'memo','Tests generated by --init');\n"
9658     "INSERT INTO [_shell$self]\n"
9659     "  SELECT 'run',\n"
9660     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
9661                                  "FROM sqlite_master ORDER BY 2'',224))',\n"
9662     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
9663                           "FROM sqlite_master ORDER BY 2',224));\n"
9664     "INSERT INTO [_shell$self]\n"
9665     "  SELECT 'run',"
9666     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
9667     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
9668     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
9669     "  FROM (\n"
9670     "    SELECT name FROM sqlite_master\n"
9671     "     WHERE type='table'\n"
9672     "       AND name<>'selftest'\n"
9673     "       AND coalesce(rootpage,0)>0\n"
9674     "  )\n"
9675     " ORDER BY name;\n"
9676     "INSERT INTO [_shell$self]\n"
9677     "  VALUES('run','PRAGMA integrity_check','ok');\n"
9678     "INSERT INTO selftest(tno,op,cmd,ans)"
9679     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
9680     "DROP TABLE [_shell$self];"
9681     ,0,0,&zErrMsg);
9682   if( zErrMsg ){
9683     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
9684     sqlite3_free(zErrMsg);
9685   }
9686   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
9687 }
9688
9689
9690 /*
9691 ** Set the destination table field of the ShellState structure to
9692 ** the name of the table given.  Escape any quote characters in the
9693 ** table name.
9694 */
9695 static void set_table_name(ShellState *p, const char *zName){
9696   int i, n;
9697   char cQuote;
9698   char *z;
9699
9700   if( p->zDestTable ){
9701     free(p->zDestTable);
9702     p->zDestTable = 0;
9703   }
9704   if( zName==0 ) return;
9705   cQuote = quoteChar(zName);
9706   n = strlen30(zName);
9707   if( cQuote ) n += n+2;
9708   z = p->zDestTable = malloc( n+1 );
9709   if( z==0 ) shell_out_of_memory();
9710   n = 0;
9711   if( cQuote ) z[n++] = cQuote;
9712   for(i=0; zName[i]; i++){
9713     z[n++] = zName[i];
9714     if( zName[i]==cQuote ) z[n++] = cQuote;
9715   }
9716   if( cQuote ) z[n++] = cQuote;
9717   z[n] = 0;
9718 }
9719
9720
9721 /*
9722 ** Execute a query statement that will generate SQL output.  Print
9723 ** the result columns, comma-separated, on a line and then add a
9724 ** semicolon terminator to the end of that line.
9725 **
9726 ** If the number of columns is 1 and that column contains text "--"
9727 ** then write the semicolon on a separate line.  That way, if a
9728 ** "--" comment occurs at the end of the statement, the comment
9729 ** won't consume the semicolon terminator.
9730 */
9731 static int run_table_dump_query(
9732   ShellState *p,           /* Query context */
9733   const char *zSelect,     /* SELECT statement to extract content */
9734   const char *zFirstRow    /* Print before first row, if not NULL */
9735 ){
9736   sqlite3_stmt *pSelect;
9737   int rc;
9738   int nResult;
9739   int i;
9740   const char *z;
9741   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
9742   if( rc!=SQLITE_OK || !pSelect ){
9743     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9744                 sqlite3_errmsg(p->db));
9745     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9746     return rc;
9747   }
9748   rc = sqlite3_step(pSelect);
9749   nResult = sqlite3_column_count(pSelect);
9750   while( rc==SQLITE_ROW ){
9751     if( zFirstRow ){
9752       utf8_printf(p->out, "%s", zFirstRow);
9753       zFirstRow = 0;
9754     }
9755     z = (const char*)sqlite3_column_text(pSelect, 0);
9756     utf8_printf(p->out, "%s", z);
9757     for(i=1; i<nResult; i++){
9758       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
9759     }
9760     if( z==0 ) z = "";
9761     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
9762     if( z[0] ){
9763       raw_printf(p->out, "\n;\n");
9764     }else{
9765       raw_printf(p->out, ";\n");
9766     }
9767     rc = sqlite3_step(pSelect);
9768   }
9769   rc = sqlite3_finalize(pSelect);
9770   if( rc!=SQLITE_OK ){
9771     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9772                 sqlite3_errmsg(p->db));
9773     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9774   }
9775   return rc;
9776 }
9777
9778 /*
9779 ** Allocate space and save off current error string.
9780 */
9781 static char *save_err_msg(
9782   sqlite3 *db            /* Database to query */
9783 ){
9784   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
9785   char *zErrMsg = sqlite3_malloc64(nErrMsg);
9786   if( zErrMsg ){
9787     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
9788   }
9789   return zErrMsg;
9790 }
9791
9792 #ifdef __linux__
9793 /*
9794 ** Attempt to display I/O stats on Linux using /proc/PID/io
9795 */
9796 static void displayLinuxIoStats(FILE *out){
9797   FILE *in;
9798   char z[200];
9799   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
9800   in = fopen(z, "rb");
9801   if( in==0 ) return;
9802   while( fgets(z, sizeof(z), in)!=0 ){
9803     static const struct {
9804       const char *zPattern;
9805       const char *zDesc;
9806     } aTrans[] = {
9807       { "rchar: ",                  "Bytes received by read():" },
9808       { "wchar: ",                  "Bytes sent to write():"    },
9809       { "syscr: ",                  "Read() system calls:"      },
9810       { "syscw: ",                  "Write() system calls:"     },
9811       { "read_bytes: ",             "Bytes read from storage:"  },
9812       { "write_bytes: ",            "Bytes written to storage:" },
9813       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
9814     };
9815     int i;
9816     for(i=0; i<ArraySize(aTrans); i++){
9817       int n = strlen30(aTrans[i].zPattern);
9818       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
9819         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
9820         break;
9821       }
9822     }
9823   }
9824   fclose(in);
9825 }
9826 #endif
9827
9828 /*
9829 ** Display a single line of status using 64-bit values.
9830 */
9831 static void displayStatLine(
9832   ShellState *p,            /* The shell context */
9833   char *zLabel,             /* Label for this one line */
9834   char *zFormat,            /* Format for the result */
9835   int iStatusCtrl,          /* Which status to display */
9836   int bReset                /* True to reset the stats */
9837 ){
9838   sqlite3_int64 iCur = -1;
9839   sqlite3_int64 iHiwtr = -1;
9840   int i, nPercent;
9841   char zLine[200];
9842   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
9843   for(i=0, nPercent=0; zFormat[i]; i++){
9844     if( zFormat[i]=='%' ) nPercent++;
9845   }
9846   if( nPercent>1 ){
9847     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
9848   }else{
9849     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
9850   }
9851   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
9852 }
9853
9854 /*
9855 ** Display memory stats.
9856 */
9857 static int display_stats(
9858   sqlite3 *db,                /* Database to query */
9859   ShellState *pArg,           /* Pointer to ShellState */
9860   int bReset                  /* True to reset the stats */
9861 ){
9862   int iCur;
9863   int iHiwtr;
9864   FILE *out;
9865   if( pArg==0 || pArg->out==0 ) return 0;
9866   out = pArg->out;
9867
9868   if( pArg->pStmt && (pArg->statsOn & 2) ){
9869     int nCol, i, x;
9870     sqlite3_stmt *pStmt = pArg->pStmt;
9871     char z[100];
9872     nCol = sqlite3_column_count(pStmt);
9873     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
9874     for(i=0; i<nCol; i++){
9875       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
9876       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
9877 #ifndef SQLITE_OMIT_DECLTYPE
9878       sqlite3_snprintf(30, z+x, "declared type:");
9879       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
9880 #endif
9881 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9882       sqlite3_snprintf(30, z+x, "database name:");
9883       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
9884       sqlite3_snprintf(30, z+x, "table name:");
9885       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
9886       sqlite3_snprintf(30, z+x, "origin name:");
9887       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
9888 #endif
9889     }
9890   }
9891
9892   displayStatLine(pArg, "Memory Used:",
9893      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9894   displayStatLine(pArg, "Number of Outstanding Allocations:",
9895      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9896   if( pArg->shellFlgs & SHFLG_Pagecache ){
9897     displayStatLine(pArg, "Number of Pcache Pages Used:",
9898        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9899   }
9900   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9901      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9902   displayStatLine(pArg, "Largest Allocation:",
9903      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9904   displayStatLine(pArg, "Largest Pcache Allocation:",
9905      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
9906 #ifdef YYTRACKMAXSTACKDEPTH
9907   displayStatLine(pArg, "Deepest Parser Stack:",
9908      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9909 #endif
9910
9911   if( db ){
9912     if( pArg->shellFlgs & SHFLG_Lookaside ){
9913       iHiwtr = iCur = -1;
9914       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
9915                         &iCur, &iHiwtr, bReset);
9916       raw_printf(pArg->out,
9917               "Lookaside Slots Used:                %d (max %d)\n",
9918               iCur, iHiwtr);
9919       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
9920                         &iCur, &iHiwtr, bReset);
9921       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
9922               iHiwtr);
9923       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
9924                         &iCur, &iHiwtr, bReset);
9925       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
9926               iHiwtr);
9927       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
9928                         &iCur, &iHiwtr, bReset);
9929       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
9930               iHiwtr);
9931     }
9932     iHiwtr = iCur = -1;
9933     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
9934     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
9935             iCur);
9936     iHiwtr = iCur = -1;
9937     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
9938     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
9939     iHiwtr = iCur = -1;
9940     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
9941     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
9942     iHiwtr = iCur = -1;
9943     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
9944     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
9945     iHiwtr = iCur = -1;
9946     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
9947     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
9948     iHiwtr = iCur = -1;
9949     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
9950     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
9951             iCur);
9952     iHiwtr = iCur = -1;
9953     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
9954     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
9955             iCur);
9956   }
9957
9958   if( pArg->pStmt ){
9959     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
9960                                bReset);
9961     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
9962     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
9963     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
9964     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
9965     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
9966     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
9967     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
9968     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
9969     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
9970     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
9971     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
9972     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
9973     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
9974   }
9975
9976 #ifdef __linux__
9977   displayLinuxIoStats(pArg->out);
9978 #endif
9979
9980   /* Do not remove this machine readable comment: extra-stats-output-here */
9981
9982   return 0;
9983 }
9984
9985 /*
9986 ** Display scan stats.
9987 */
9988 static void display_scanstats(
9989   sqlite3 *db,                    /* Database to query */
9990   ShellState *pArg                /* Pointer to ShellState */
9991 ){
9992 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9993   UNUSED_PARAMETER(db);
9994   UNUSED_PARAMETER(pArg);
9995 #else
9996   int i, k, n, mx;
9997   raw_printf(pArg->out, "-------- scanstats --------\n");
9998   mx = 0;
9999   for(k=0; k<=mx; k++){
10000     double rEstLoop = 1.0;
10001     for(i=n=0; 1; i++){
10002       sqlite3_stmt *p = pArg->pStmt;
10003       sqlite3_int64 nLoop, nVisit;
10004       double rEst;
10005       int iSid;
10006       const char *zExplain;
10007       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
10008         break;
10009       }
10010       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
10011       if( iSid>mx ) mx = iSid;
10012       if( iSid!=k ) continue;
10013       if( n==0 ){
10014         rEstLoop = (double)nLoop;
10015         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
10016       }
10017       n++;
10018       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
10019       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
10020       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
10021       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
10022       rEstLoop *= rEst;
10023       raw_printf(pArg->out,
10024           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
10025           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
10026       );
10027     }
10028   }
10029   raw_printf(pArg->out, "---------------------------\n");
10030 #endif
10031 }
10032
10033 /*
10034 ** Parameter azArray points to a zero-terminated array of strings. zStr
10035 ** points to a single nul-terminated string. Return non-zero if zStr
10036 ** is equal, according to strcmp(), to any of the strings in the array.
10037 ** Otherwise, return zero.
10038 */
10039 static int str_in_array(const char *zStr, const char **azArray){
10040   int i;
10041   for(i=0; azArray[i]; i++){
10042     if( 0==strcmp(zStr, azArray[i]) ) return 1;
10043   }
10044   return 0;
10045 }
10046
10047 /*
10048 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
10049 ** and populate the ShellState.aiIndent[] array with the number of
10050 ** spaces each opcode should be indented before it is output.
10051 **
10052 ** The indenting rules are:
10053 **
10054 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
10055 **       all opcodes that occur between the p2 jump destination and the opcode
10056 **       itself by 2 spaces.
10057 **
10058 **     * For each "Goto", if the jump destination is earlier in the program
10059 **       and ends on one of:
10060 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
10061 **       or if the P1 parameter is one instead of zero,
10062 **       then indent all opcodes between the earlier instruction
10063 **       and "Goto" by 2 spaces.
10064 */
10065 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
10066   const char *zSql;               /* The text of the SQL statement */
10067   const char *z;                  /* Used to check if this is an EXPLAIN */
10068   int *abYield = 0;               /* True if op is an OP_Yield */
10069   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
10070   int iOp;                        /* Index of operation in p->aiIndent[] */
10071
10072   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
10073                            "NextIfOpen", "PrevIfOpen", 0 };
10074   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
10075                             "Rewind", 0 };
10076   const char *azGoto[] = { "Goto", 0 };
10077
10078   /* Try to figure out if this is really an EXPLAIN statement. If this
10079   ** cannot be verified, return early.  */
10080   if( sqlite3_column_count(pSql)!=8 ){
10081     p->cMode = p->mode;
10082     return;
10083   }
10084   zSql = sqlite3_sql(pSql);
10085   if( zSql==0 ) return;
10086   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
10087   if( sqlite3_strnicmp(z, "explain", 7) ){
10088     p->cMode = p->mode;
10089     return;
10090   }
10091
10092   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
10093     int i;
10094     int iAddr = sqlite3_column_int(pSql, 0);
10095     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
10096
10097     /* Set p2 to the P2 field of the current opcode. Then, assuming that
10098     ** p2 is an instruction address, set variable p2op to the index of that
10099     ** instruction in the aiIndent[] array. p2 and p2op may be different if
10100     ** the current instruction is part of a sub-program generated by an
10101     ** SQL trigger or foreign key.  */
10102     int p2 = sqlite3_column_int(pSql, 3);
10103     int p2op = (p2 + (iOp-iAddr));
10104
10105     /* Grow the p->aiIndent array as required */
10106     if( iOp>=nAlloc ){
10107       if( iOp==0 ){
10108         /* Do further verfication that this is explain output.  Abort if
10109         ** it is not */
10110         static const char *explainCols[] = {
10111            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
10112         int jj;
10113         for(jj=0; jj<ArraySize(explainCols); jj++){
10114           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
10115             p->cMode = p->mode;
10116             sqlite3_reset(pSql);
10117             return;
10118           }
10119         }
10120       }
10121       nAlloc += 100;
10122       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
10123       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
10124     }
10125     abYield[iOp] = str_in_array(zOp, azYield);
10126     p->aiIndent[iOp] = 0;
10127     p->nIndent = iOp+1;
10128
10129     if( str_in_array(zOp, azNext) ){
10130       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10131     }
10132     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
10133      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
10134     ){
10135       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10136     }
10137   }
10138
10139   p->iIndent = 0;
10140   sqlite3_free(abYield);
10141   sqlite3_reset(pSql);
10142 }
10143
10144 /*
10145 ** Free the array allocated by explain_data_prepare().
10146 */
10147 static void explain_data_delete(ShellState *p){
10148   sqlite3_free(p->aiIndent);
10149   p->aiIndent = 0;
10150   p->nIndent = 0;
10151   p->iIndent = 0;
10152 }
10153
10154 /*
10155 ** Disable and restore .wheretrace and .selecttrace settings.
10156 */
10157 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10158 extern int sqlite3SelectTrace;
10159 static int savedSelectTrace;
10160 #endif
10161 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10162 extern int sqlite3WhereTrace;
10163 static int savedWhereTrace;
10164 #endif
10165 static void disable_debug_trace_modes(void){
10166 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10167   savedSelectTrace = sqlite3SelectTrace;
10168   sqlite3SelectTrace = 0;
10169 #endif
10170 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10171   savedWhereTrace = sqlite3WhereTrace;
10172   sqlite3WhereTrace = 0;
10173 #endif
10174 }
10175 static void restore_debug_trace_modes(void){
10176 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10177   sqlite3SelectTrace = savedSelectTrace;
10178 #endif
10179 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10180   sqlite3WhereTrace = savedWhereTrace;
10181 #endif
10182 }
10183
10184 /*
10185 ** Run a prepared statement
10186 */
10187 static void exec_prepared_stmt(
10188   ShellState *pArg,                                /* Pointer to ShellState */
10189   sqlite3_stmt *pStmt                              /* Statment to run */
10190 ){
10191   int rc;
10192
10193   /* perform the first step.  this will tell us if we
10194   ** have a result set or not and how wide it is.
10195   */
10196   rc = sqlite3_step(pStmt);
10197   /* if we have a result set... */
10198   if( SQLITE_ROW == rc ){
10199     /* allocate space for col name ptr, value ptr, and type */
10200     int nCol = sqlite3_column_count(pStmt);
10201     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10202     if( !pData ){
10203       rc = SQLITE_NOMEM;
10204     }else{
10205       char **azCols = (char **)pData;      /* Names of result columns */
10206       char **azVals = &azCols[nCol];       /* Results */
10207       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10208       int i, x;
10209       assert(sizeof(int) <= sizeof(char *));
10210       /* save off ptrs to column names */
10211       for(i=0; i<nCol; i++){
10212         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10213       }
10214       do{
10215         /* extract the data and data types */
10216         for(i=0; i<nCol; i++){
10217           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10218           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10219             azVals[i] = "";
10220           }else{
10221             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10222           }
10223           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10224             rc = SQLITE_NOMEM;
10225             break; /* from for */
10226           }
10227         } /* end for */
10228
10229         /* if data and types extracted successfully... */
10230         if( SQLITE_ROW == rc ){
10231           /* call the supplied callback with the result row data */
10232           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10233             rc = SQLITE_ABORT;
10234           }else{
10235             rc = sqlite3_step(pStmt);
10236           }
10237         }
10238       } while( SQLITE_ROW == rc );
10239       sqlite3_free(pData);
10240     }
10241   }
10242 }
10243
10244 #ifndef SQLITE_OMIT_VIRTUALTABLE
10245 /*
10246 ** This function is called to process SQL if the previous shell command
10247 ** was ".expert". It passes the SQL in the second argument directly to
10248 ** the sqlite3expert object.
10249 **
10250 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10251 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10252 ** an English language error message. It is the responsibility of the
10253 ** caller to eventually free this buffer using sqlite3_free().
10254 */
10255 static int expertHandleSQL(
10256   ShellState *pState, 
10257   const char *zSql, 
10258   char **pzErr
10259 ){
10260   assert( pState->expert.pExpert );
10261   assert( pzErr==0 || *pzErr==0 );
10262   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
10263 }
10264
10265 /*
10266 ** This function is called either to silently clean up the object
10267 ** created by the ".expert" command (if bCancel==1), or to generate a 
10268 ** report from it and then clean it up (if bCancel==0).
10269 **
10270 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10271 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10272 ** an English language error message. It is the responsibility of the
10273 ** caller to eventually free this buffer using sqlite3_free().
10274 */
10275 static int expertFinish(
10276   ShellState *pState,
10277   int bCancel,
10278   char **pzErr
10279 ){
10280   int rc = SQLITE_OK;
10281   sqlite3expert *p = pState->expert.pExpert;
10282   assert( p );
10283   assert( bCancel || pzErr==0 || *pzErr==0 );
10284   if( bCancel==0 ){
10285     FILE *out = pState->out;
10286     int bVerbose = pState->expert.bVerbose;
10287
10288     rc = sqlite3_expert_analyze(p, pzErr);
10289     if( rc==SQLITE_OK ){
10290       int nQuery = sqlite3_expert_count(p);
10291       int i;
10292
10293       if( bVerbose ){
10294         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
10295         raw_printf(out, "-- Candidates -----------------------------\n");
10296         raw_printf(out, "%s\n", zCand);
10297       }
10298       for(i=0; i<nQuery; i++){
10299         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
10300         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
10301         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
10302         if( zIdx==0 ) zIdx = "(no new indexes)\n";
10303         if( bVerbose ){
10304           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
10305           raw_printf(out, "%s\n\n", zSql);
10306         }
10307         raw_printf(out, "%s\n", zIdx);
10308         raw_printf(out, "%s\n", zEQP);
10309       }
10310     }
10311   }
10312   sqlite3_expert_destroy(p);
10313   pState->expert.pExpert = 0;
10314   return rc;
10315 }
10316
10317 /*
10318 ** Implementation of ".expert" dot command.
10319 */
10320 static int expertDotCommand(
10321   ShellState *pState,             /* Current shell tool state */
10322   char **azArg,                   /* Array of arguments passed to dot command */
10323   int nArg                        /* Number of entries in azArg[] */
10324 ){
10325   int rc = SQLITE_OK;
10326   char *zErr = 0;
10327   int i;
10328   int iSample = 0;
10329
10330   assert( pState->expert.pExpert==0 );
10331   memset(&pState->expert, 0, sizeof(ExpertInfo));
10332
10333   for(i=1; rc==SQLITE_OK && i<nArg; i++){
10334     char *z = azArg[i];
10335     int n;
10336     if( z[0]=='-' && z[1]=='-' ) z++;
10337     n = strlen30(z);
10338     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
10339       pState->expert.bVerbose = 1;
10340     }
10341     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
10342       if( i==(nArg-1) ){
10343         raw_printf(stderr, "option requires an argument: %s\n", z);
10344         rc = SQLITE_ERROR;
10345       }else{
10346         iSample = (int)integerValue(azArg[++i]);
10347         if( iSample<0 || iSample>100 ){
10348           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
10349           rc = SQLITE_ERROR;
10350         }
10351       }
10352     }
10353     else{
10354       raw_printf(stderr, "unknown option: %s\n", z);
10355       rc = SQLITE_ERROR;
10356     }
10357   }
10358
10359   if( rc==SQLITE_OK ){
10360     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
10361     if( pState->expert.pExpert==0 ){
10362       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
10363       rc = SQLITE_ERROR;
10364     }else{
10365       sqlite3_expert_config(
10366           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
10367       );
10368     }
10369   }
10370
10371   return rc;
10372 }
10373 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10374
10375 /*
10376 ** Execute a statement or set of statements.  Print
10377 ** any result rows/columns depending on the current mode
10378 ** set via the supplied callback.
10379 **
10380 ** This is very similar to SQLite's built-in sqlite3_exec()
10381 ** function except it takes a slightly different callback
10382 ** and callback data argument.
10383 */
10384 static int shell_exec(
10385   ShellState *pArg,                         /* Pointer to ShellState */
10386   const char *zSql,                         /* SQL to be evaluated */
10387   char **pzErrMsg                           /* Error msg written here */
10388 ){
10389   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
10390   int rc = SQLITE_OK;             /* Return Code */
10391   int rc2;
10392   const char *zLeftover;          /* Tail of unprocessed SQL */
10393   sqlite3 *db = pArg->db;
10394
10395   if( pzErrMsg ){
10396     *pzErrMsg = NULL;
10397   }
10398
10399 #ifndef SQLITE_OMIT_VIRTUALTABLE
10400   if( pArg->expert.pExpert ){
10401     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
10402     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
10403   }
10404 #endif
10405
10406   while( zSql[0] && (SQLITE_OK == rc) ){
10407     static const char *zStmtSql;
10408     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
10409     if( SQLITE_OK != rc ){
10410       if( pzErrMsg ){
10411         *pzErrMsg = save_err_msg(db);
10412       }
10413     }else{
10414       if( !pStmt ){
10415         /* this happens for a comment or white-space */
10416         zSql = zLeftover;
10417         while( IsSpace(zSql[0]) ) zSql++;
10418         continue;
10419       }
10420       zStmtSql = sqlite3_sql(pStmt);
10421       if( zStmtSql==0 ) zStmtSql = "";
10422       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
10423
10424       /* save off the prepared statment handle and reset row count */
10425       if( pArg ){
10426         pArg->pStmt = pStmt;
10427         pArg->cnt = 0;
10428       }
10429
10430       /* echo the sql statement if echo on */
10431       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10432         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10433       }
10434
10435       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10436       if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
10437         sqlite3_stmt *pExplain;
10438         char *zEQP;
10439         int triggerEQP = 0;
10440         disable_debug_trace_modes();
10441         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
10442         if( pArg->autoEQP>=AUTOEQP_trigger ){
10443           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
10444         }
10445         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10446         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10447         if( rc==SQLITE_OK ){
10448           while( sqlite3_step(pExplain)==SQLITE_ROW ){
10449             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10450             int iEqpId = sqlite3_column_int(pExplain, 0);
10451             int iParentId = sqlite3_column_int(pExplain, 1);
10452             if( zEQPLine[0]=='-' ) eqp_render(pArg);
10453             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10454           }
10455           eqp_render(pArg);
10456         }
10457         sqlite3_finalize(pExplain);
10458         sqlite3_free(zEQP);
10459         if( pArg->autoEQP>=AUTOEQP_full ){
10460           /* Also do an EXPLAIN for ".eqp full" mode */
10461           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10462           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10463           if( rc==SQLITE_OK ){
10464             pArg->cMode = MODE_Explain;
10465             explain_data_prepare(pArg, pExplain);
10466             exec_prepared_stmt(pArg, pExplain);
10467             explain_data_delete(pArg);
10468           }
10469           sqlite3_finalize(pExplain);
10470           sqlite3_free(zEQP);
10471         }
10472         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
10473           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
10474           /* Reprepare pStmt before reactiving trace modes */
10475           sqlite3_finalize(pStmt);
10476           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
10477         }
10478         restore_debug_trace_modes();
10479       }
10480
10481       if( pArg ){
10482         pArg->cMode = pArg->mode;
10483         if( pArg->autoExplain ){
10484           if( sqlite3_column_count(pStmt)==8
10485            && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
10486           ){
10487             pArg->cMode = MODE_Explain;
10488           }
10489           if( sqlite3_column_count(pStmt)==4
10490            && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){
10491             pArg->cMode = MODE_EQP;
10492           }
10493         }
10494
10495         /* If the shell is currently in ".explain" mode, gather the extra
10496         ** data required to add indents to the output.*/
10497         if( pArg->cMode==MODE_Explain ){
10498           explain_data_prepare(pArg, pStmt);
10499         }
10500       }
10501
10502       exec_prepared_stmt(pArg, pStmt);
10503       explain_data_delete(pArg);
10504       eqp_render(pArg);
10505
10506       /* print usage stats if stats on */
10507       if( pArg && pArg->statsOn ){
10508         display_stats(db, pArg, 0);
10509       }
10510
10511       /* print loop-counters if required */
10512       if( pArg && pArg->scanstatsOn ){
10513         display_scanstats(db, pArg);
10514       }
10515
10516       /* Finalize the statement just executed. If this fails, save a
10517       ** copy of the error message. Otherwise, set zSql to point to the
10518       ** next statement to execute. */
10519       rc2 = sqlite3_finalize(pStmt);
10520       if( rc!=SQLITE_NOMEM ) rc = rc2;
10521       if( rc==SQLITE_OK ){
10522         zSql = zLeftover;
10523         while( IsSpace(zSql[0]) ) zSql++;
10524       }else if( pzErrMsg ){
10525         *pzErrMsg = save_err_msg(db);
10526       }
10527
10528       /* clear saved stmt handle */
10529       if( pArg ){
10530         pArg->pStmt = NULL;
10531       }
10532     }
10533   } /* end while */
10534
10535   return rc;
10536 }
10537
10538 /*
10539 ** Release memory previously allocated by tableColumnList().
10540 */
10541 static void freeColumnList(char **azCol){
10542   int i;
10543   for(i=1; azCol[i]; i++){
10544     sqlite3_free(azCol[i]);
10545   }
10546   /* azCol[0] is a static string */
10547   sqlite3_free(azCol);
10548 }
10549
10550 /*
10551 ** Return a list of pointers to strings which are the names of all
10552 ** columns in table zTab.   The memory to hold the names is dynamically
10553 ** allocated and must be released by the caller using a subsequent call
10554 ** to freeColumnList().
10555 **
10556 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
10557 ** value that needs to be preserved, then azCol[0] is filled in with the
10558 ** name of the rowid column.
10559 **
10560 ** The first regular column in the table is azCol[1].  The list is terminated
10561 ** by an entry with azCol[i]==0.
10562 */
10563 static char **tableColumnList(ShellState *p, const char *zTab){
10564   char **azCol = 0;
10565   sqlite3_stmt *pStmt;
10566   char *zSql;
10567   int nCol = 0;
10568   int nAlloc = 0;
10569   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
10570   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
10571   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
10572   int rc;
10573
10574   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
10575   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10576   sqlite3_free(zSql);
10577   if( rc ) return 0;
10578   while( sqlite3_step(pStmt)==SQLITE_ROW ){
10579     if( nCol>=nAlloc-2 ){
10580       nAlloc = nAlloc*2 + nCol + 10;
10581       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
10582       if( azCol==0 ) shell_out_of_memory();
10583     }
10584     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
10585     if( sqlite3_column_int(pStmt, 5) ){
10586       nPK++;
10587       if( nPK==1
10588        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
10589                           "INTEGER")==0
10590       ){
10591         isIPK = 1;
10592       }else{
10593         isIPK = 0;
10594       }
10595     }
10596   }
10597   sqlite3_finalize(pStmt);
10598   if( azCol==0 ) return 0;
10599   azCol[0] = 0;
10600   azCol[nCol+1] = 0;
10601
10602   /* The decision of whether or not a rowid really needs to be preserved
10603   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
10604   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
10605   ** rowids on tables where the rowid is inaccessible because there are other
10606   ** columns in the table named "rowid", "_rowid_", and "oid".
10607   */
10608   if( preserveRowid && isIPK ){
10609     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
10610     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
10611     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
10612     ** ROWID aliases.  To distinguish these cases, check to see if
10613     ** there is a "pk" entry in "PRAGMA index_list".  There will be
10614     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
10615     */
10616     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
10617                            " WHERE origin='pk'", zTab);
10618     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10619     sqlite3_free(zSql);
10620     if( rc ){
10621       freeColumnList(azCol);
10622       return 0;
10623     }
10624     rc = sqlite3_step(pStmt);
10625     sqlite3_finalize(pStmt);
10626     preserveRowid = rc==SQLITE_ROW;
10627   }
10628   if( preserveRowid ){
10629     /* Only preserve the rowid if we can find a name to use for the
10630     ** rowid */
10631     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
10632     int i, j;
10633     for(j=0; j<3; j++){
10634       for(i=1; i<=nCol; i++){
10635         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
10636       }
10637       if( i>nCol ){
10638         /* At this point, we know that azRowid[j] is not the name of any
10639         ** ordinary column in the table.  Verify that azRowid[j] is a valid
10640         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
10641         ** tables will fail this last check */
10642         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
10643         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
10644         break;
10645       }
10646     }
10647   }
10648   return azCol;
10649 }
10650
10651 /*
10652 ** Toggle the reverse_unordered_selects setting.
10653 */
10654 static void toggleSelectOrder(sqlite3 *db){
10655   sqlite3_stmt *pStmt = 0;
10656   int iSetting = 0;
10657   char zStmt[100];
10658   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
10659   if( sqlite3_step(pStmt)==SQLITE_ROW ){
10660     iSetting = sqlite3_column_int(pStmt, 0);
10661   }
10662   sqlite3_finalize(pStmt);
10663   sqlite3_snprintf(sizeof(zStmt), zStmt,
10664        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
10665   sqlite3_exec(db, zStmt, 0, 0, 0);
10666 }
10667
10668 /*
10669 ** This is a different callback routine used for dumping the database.
10670 ** Each row received by this callback consists of a table name,
10671 ** the table type ("index" or "table") and SQL to create the table.
10672 ** This routine should print text sufficient to recreate the table.
10673 */
10674 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
10675   int rc;
10676   const char *zTable;
10677   const char *zType;
10678   const char *zSql;
10679   ShellState *p = (ShellState *)pArg;
10680
10681   UNUSED_PARAMETER(azNotUsed);
10682   if( nArg!=3 || azArg==0 ) return 0;
10683   zTable = azArg[0];
10684   zType = azArg[1];
10685   zSql = azArg[2];
10686
10687   if( strcmp(zTable, "sqlite_sequence")==0 ){
10688     raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
10689   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
10690     raw_printf(p->out, "ANALYZE sqlite_master;\n");
10691   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
10692     return 0;
10693   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
10694     char *zIns;
10695     if( !p->writableSchema ){
10696       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
10697       p->writableSchema = 1;
10698     }
10699     zIns = sqlite3_mprintf(
10700        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
10701        "VALUES('table','%q','%q',0,'%q');",
10702        zTable, zTable, zSql);
10703     utf8_printf(p->out, "%s\n", zIns);
10704     sqlite3_free(zIns);
10705     return 0;
10706   }else{
10707     printSchemaLine(p->out, zSql, ";\n");
10708   }
10709
10710   if( strcmp(zType, "table")==0 ){
10711     ShellText sSelect;
10712     ShellText sTable;
10713     char **azCol;
10714     int i;
10715     char *savedDestTable;
10716     int savedMode;
10717
10718     azCol = tableColumnList(p, zTable);
10719     if( azCol==0 ){
10720       p->nErr++;
10721       return 0;
10722     }
10723
10724     /* Always quote the table name, even if it appears to be pure ascii,
10725     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
10726     initText(&sTable);
10727     appendText(&sTable, zTable, quoteChar(zTable));
10728     /* If preserving the rowid, add a column list after the table name.
10729     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
10730     ** instead of the usual "INSERT INTO tab VALUES(...)".
10731     */
10732     if( azCol[0] ){
10733       appendText(&sTable, "(", 0);
10734       appendText(&sTable, azCol[0], 0);
10735       for(i=1; azCol[i]; i++){
10736         appendText(&sTable, ",", 0);
10737         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
10738       }
10739       appendText(&sTable, ")", 0);
10740     }
10741
10742     /* Build an appropriate SELECT statement */
10743     initText(&sSelect);
10744     appendText(&sSelect, "SELECT ", 0);
10745     if( azCol[0] ){
10746       appendText(&sSelect, azCol[0], 0);
10747       appendText(&sSelect, ",", 0);
10748     }
10749     for(i=1; azCol[i]; i++){
10750       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
10751       if( azCol[i+1] ){
10752         appendText(&sSelect, ",", 0);
10753       }
10754     }
10755     freeColumnList(azCol);
10756     appendText(&sSelect, " FROM ", 0);
10757     appendText(&sSelect, zTable, quoteChar(zTable));
10758
10759     savedDestTable = p->zDestTable;
10760     savedMode = p->mode;
10761     p->zDestTable = sTable.z;
10762     p->mode = p->cMode = MODE_Insert;
10763     rc = shell_exec(p, sSelect.z, 0);
10764     if( (rc&0xff)==SQLITE_CORRUPT ){
10765       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10766       toggleSelectOrder(p->db);
10767       shell_exec(p, sSelect.z, 0);
10768       toggleSelectOrder(p->db);
10769     }
10770     p->zDestTable = savedDestTable;
10771     p->mode = savedMode;
10772     freeText(&sTable);
10773     freeText(&sSelect);
10774     if( rc ) p->nErr++;
10775   }
10776   return 0;
10777 }
10778
10779 /*
10780 ** Run zQuery.  Use dump_callback() as the callback routine so that
10781 ** the contents of the query are output as SQL statements.
10782 **
10783 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
10784 ** "ORDER BY rowid DESC" to the end.
10785 */
10786 static int run_schema_dump_query(
10787   ShellState *p,
10788   const char *zQuery
10789 ){
10790   int rc;
10791   char *zErr = 0;
10792   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
10793   if( rc==SQLITE_CORRUPT ){
10794     char *zQ2;
10795     int len = strlen30(zQuery);
10796     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10797     if( zErr ){
10798       utf8_printf(p->out, "/****** %s ******/\n", zErr);
10799       sqlite3_free(zErr);
10800       zErr = 0;
10801     }
10802     zQ2 = malloc( len+100 );
10803     if( zQ2==0 ) return rc;
10804     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
10805     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
10806     if( rc ){
10807       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
10808     }else{
10809       rc = SQLITE_CORRUPT;
10810     }
10811     sqlite3_free(zErr);
10812     free(zQ2);
10813   }
10814   return rc;
10815 }
10816
10817 /*
10818 ** Text of a help message
10819 */
10820 static char zHelp[] =
10821 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10822   ".archive ...           Manage SQL archives: \".archive --help\" for details\n"
10823 #endif
10824 #ifndef SQLITE_OMIT_AUTHORIZATION
10825   ".auth ON|OFF           Show authorizer callbacks\n"
10826 #endif
10827   ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
10828   "                         Add \"--append\" to open using appendvfs.\n"
10829   ".bail on|off           Stop after hitting an error.  Default OFF\n"
10830   ".binary on|off         Turn binary output on or off.  Default OFF\n"
10831   ".cd DIRECTORY          Change the working directory to DIRECTORY\n"
10832   ".changes on|off        Show number of rows changed by SQL\n"
10833   ".check GLOB            Fail if output since .testcase does not match\n"
10834   ".clone NEWDB           Clone data into NEWDB from the existing database\n"
10835   ".databases             List names and files of attached databases\n"
10836   ".dbconfig ?op? ?val?   List or change sqlite3_db_config() options\n"
10837   ".dbinfo ?DB?           Show status information about the database\n"
10838   ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
10839   "                         If TABLE specified, only dump tables matching\n"
10840   "                         LIKE pattern TABLE.\n"
10841   ".echo on|off           Turn command echo on or off\n"
10842   ".eqp on|off|full       Enable or disable automatic EXPLAIN QUERY PLAN\n"
10843   ".excel                 Display the output of next command in a spreadsheet\n"
10844   ".exit                  Exit this program\n"
10845   ".expert                EXPERIMENTAL. Suggest indexes for specified queries\n"
10846 /* Because explain mode comes on automatically now, the ".explain" mode
10847 ** is removed from the help screen.  It is still supported for legacy, however */
10848 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
10849   ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
10850   ".headers on|off        Turn display of headers on or off\n"
10851   ".help                  Show this message\n"
10852   ".import FILE TABLE     Import data from FILE into TABLE\n"
10853 #ifndef SQLITE_OMIT_TEST_CONTROL
10854   ".imposter INDEX TABLE  Create imposter table TABLE on index INDEX\n"
10855 #endif
10856   ".indexes ?TABLE?       Show names of all indexes\n"
10857   "                         If TABLE specified, only show indexes for tables\n"
10858   "                         matching LIKE pattern TABLE.\n"
10859 #ifdef SQLITE_ENABLE_IOTRACE
10860   ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
10861 #endif
10862   ".limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT\n"
10863   ".lint OPTIONS          Report potential schema issues. Options:\n"
10864   "                         fkey-indexes     Find missing foreign key indexes\n"
10865 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10866   ".load FILE ?ENTRY?     Load an extension library\n"
10867 #endif
10868   ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
10869   ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
10870   "                         ascii    Columns/rows delimited by 0x1F and 0x1E\n"
10871   "                         csv      Comma-separated values\n"
10872   "                         column   Left-aligned columns.  (See .width)\n"
10873   "                         html     HTML <table> code\n"
10874   "                         insert   SQL insert statements for TABLE\n"
10875   "                         line     One value per line\n"
10876   "                         list     Values delimited by \"|\"\n"
10877   "                         quote    Escape answers as for SQL\n"
10878   "                         tabs     Tab-separated values\n"
10879   "                         tcl      TCL list elements\n"
10880   ".nullvalue STRING      Use STRING in place of NULL values\n"
10881   ".once (-e|-x|FILE)     Output for the next SQL command only to FILE\n"
10882   "                         or invoke system text editor (-e) or spreadsheet (-x)\n"
10883   "                         on the output.\n"
10884   ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
10885   "                         The --new option starts with an empty file\n"
10886   "                         Other options: --readonly --append --zip\n"
10887   ".output ?FILE?         Send output to FILE or stdout\n"
10888   ".print STRING...       Print literal STRING\n"
10889   ".prompt MAIN CONTINUE  Replace the standard prompts\n"
10890   ".quit                  Exit this program\n"
10891   ".read FILENAME         Execute SQL in FILENAME\n"
10892   ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
10893   ".save FILE             Write in-memory database into FILE\n"
10894   ".scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off\n"
10895   ".schema ?PATTERN?      Show the CREATE statements matching PATTERN\n"
10896   "                          Add --indent for pretty-printing\n"
10897   ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
10898   ".separator COL ?ROW?   Change the column separator and optionally the row\n"
10899   "                         separator for both the output mode and .import\n"
10900 #if defined(SQLITE_ENABLE_SESSION)
10901   ".session CMD ...       Create or control sessions\n"
10902 #endif
10903   ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
10904 #ifndef SQLITE_NOHAVE_SYSTEM
10905   ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
10906 #endif
10907   ".show                  Show the current values for various settings\n"
10908   ".stats ?on|off?        Show stats or turn stats on or off\n"
10909 #ifndef SQLITE_NOHAVE_SYSTEM
10910   ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
10911 #endif
10912   ".tables ?TABLE?        List names of tables\n"
10913   "                         If TABLE specified, only list tables matching\n"
10914   "                         LIKE pattern TABLE.\n"
10915   ".testcase NAME         Begin redirecting output to 'testcase-out.txt'\n"
10916   ".timeout MS            Try opening locked tables for MS milliseconds\n"
10917   ".timer on|off          Turn SQL timer on or off\n"
10918   ".trace FILE|off        Output each SQL statement as it is run\n"
10919   ".vfsinfo ?AUX?         Information about the top-level VFS\n"
10920   ".vfslist               List all available VFSes\n"
10921   ".vfsname ?AUX?         Print the name of the VFS stack\n"
10922   ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
10923   "                         Negative values right-justify\n"
10924 ;
10925
10926 #if defined(SQLITE_ENABLE_SESSION)
10927 /*
10928 ** Print help information for the ".sessions" command
10929 */
10930 void session_help(ShellState *p){
10931   raw_printf(p->out,
10932     ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
10933     "If ?NAME? is omitted, the first defined session is used.\n"
10934     "Subcommands:\n"
10935     "   attach TABLE             Attach TABLE\n"
10936     "   changeset FILE           Write a changeset into FILE\n"
10937     "   close                    Close one session\n"
10938     "   enable ?BOOLEAN?         Set or query the enable bit\n"
10939     "   filter GLOB...           Reject tables matching GLOBs\n"
10940     "   indirect ?BOOLEAN?       Mark or query the indirect status\n"
10941     "   isempty                  Query whether the session is empty\n"
10942     "   list                     List currently open session names\n"
10943     "   open DB NAME             Open a new session on DB\n"
10944     "   patchset FILE            Write a patchset into FILE\n"
10945   );
10946 }
10947 #endif
10948
10949
10950 /* Forward reference */
10951 static int process_input(ShellState *p, FILE *in);
10952
10953 /*
10954 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
10955 ** and return a pointer to the buffer. The caller is responsible for freeing
10956 ** the memory.
10957 **
10958 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
10959 ** read.
10960 **
10961 ** For convenience, a nul-terminator byte is always appended to the data read
10962 ** from the file before the buffer is returned. This byte is not included in
10963 ** the final value of (*pnByte), if applicable.
10964 **
10965 ** NULL is returned if any error is encountered. The final value of *pnByte
10966 ** is undefined in this case.
10967 */
10968 static char *readFile(const char *zName, int *pnByte){
10969   FILE *in = fopen(zName, "rb");
10970   long nIn;
10971   size_t nRead;
10972   char *pBuf;
10973   if( in==0 ) return 0;
10974   fseek(in, 0, SEEK_END);
10975   nIn = ftell(in);
10976   rewind(in);
10977   pBuf = sqlite3_malloc64( nIn+1 );
10978   if( pBuf==0 ) return 0;
10979   nRead = fread(pBuf, nIn, 1, in);
10980   fclose(in);
10981   if( nRead!=1 ){
10982     sqlite3_free(pBuf);
10983     return 0;
10984   }
10985   pBuf[nIn] = 0;
10986   if( pnByte ) *pnByte = nIn;
10987   return pBuf;
10988 }
10989
10990 #if defined(SQLITE_ENABLE_SESSION)
10991 /*
10992 ** Close a single OpenSession object and release all of its associated
10993 ** resources.
10994 */
10995 static void session_close(OpenSession *pSession){
10996   int i;
10997   sqlite3session_delete(pSession->p);
10998   sqlite3_free(pSession->zName);
10999   for(i=0; i<pSession->nFilter; i++){
11000     sqlite3_free(pSession->azFilter[i]);
11001   }
11002   sqlite3_free(pSession->azFilter);
11003   memset(pSession, 0, sizeof(OpenSession));
11004 }
11005 #endif
11006
11007 /*
11008 ** Close all OpenSession objects and release all associated resources.
11009 */
11010 #if defined(SQLITE_ENABLE_SESSION)
11011 static void session_close_all(ShellState *p){
11012   int i;
11013   for(i=0; i<p->nSession; i++){
11014     session_close(&p->aSession[i]);
11015   }
11016   p->nSession = 0;
11017 }
11018 #else
11019 # define session_close_all(X)
11020 #endif
11021
11022 /*
11023 ** Implementation of the xFilter function for an open session.  Omit
11024 ** any tables named by ".session filter" but let all other table through.
11025 */
11026 #if defined(SQLITE_ENABLE_SESSION)
11027 static int session_filter(void *pCtx, const char *zTab){
11028   OpenSession *pSession = (OpenSession*)pCtx;
11029   int i;
11030   for(i=0; i<pSession->nFilter; i++){
11031     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
11032   }
11033   return 1;
11034 }
11035 #endif
11036
11037 /*
11038 ** Try to deduce the type of file for zName based on its content.  Return
11039 ** one of the SHELL_OPEN_* constants.
11040 **
11041 ** If the file does not exist or is empty but its name looks like a ZIP
11042 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
11043 ** Otherwise, assume an ordinary database regardless of the filename if
11044 ** the type cannot be determined from content.
11045 */
11046 int deduceDatabaseType(const char *zName, int dfltZip){
11047   FILE *f = fopen(zName, "rb");
11048   size_t n;
11049   int rc = SHELL_OPEN_UNSPEC;
11050   char zBuf[100];
11051   if( f==0 ){
11052     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11053        return SHELL_OPEN_ZIPFILE;
11054     }else{
11055        return SHELL_OPEN_NORMAL;
11056     }
11057   }
11058   fseek(f, -25, SEEK_END);
11059   n = fread(zBuf, 25, 1, f);
11060   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11061     rc = SHELL_OPEN_APPENDVFS;
11062   }else{
11063     fseek(f, -22, SEEK_END);
11064     n = fread(zBuf, 22, 1, f);
11065     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11066        && zBuf[3]==0x06 ){
11067       rc = SHELL_OPEN_ZIPFILE;
11068     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11069       rc = SHELL_OPEN_ZIPFILE;
11070     }
11071   }
11072   fclose(f);
11073   return rc;  
11074 }
11075
11076 /* Flags for open_db().
11077 **
11078 ** The default behavior of open_db() is to exit(1) if the database fails to
11079 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11080 ** but still returns without calling exit.
11081 **
11082 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11083 ** ZIP archive if the file does not exist or is empty and its name matches
11084 ** the *.zip pattern.
11085 */
11086 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
11087 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
11088
11089 /*
11090 ** Make sure the database is open.  If it is not, then open it.  If
11091 ** the database fails to open, print an error message and exit.
11092 */
11093 static void open_db(ShellState *p, int openFlags){
11094   if( p->db==0 ){
11095     if( p->openMode==SHELL_OPEN_UNSPEC ){
11096       if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11097         p->openMode = SHELL_OPEN_NORMAL;
11098       }else{
11099         p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 
11100                              (openFlags & OPEN_DB_ZIPFILE)!=0);
11101       }
11102     }
11103     switch( p->openMode ){
11104       case SHELL_OPEN_APPENDVFS: {
11105         sqlite3_open_v2(p->zDbFilename, &p->db, 
11106            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
11107         break;
11108       }
11109       case SHELL_OPEN_ZIPFILE: {
11110         sqlite3_open(":memory:", &p->db);
11111         break;
11112       }
11113       case SHELL_OPEN_READONLY: {
11114         sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
11115         break;
11116       }
11117       case SHELL_OPEN_UNSPEC:
11118       case SHELL_OPEN_NORMAL: {
11119         sqlite3_open(p->zDbFilename, &p->db);
11120         break;
11121       }
11122     }
11123     globalDb = p->db;
11124     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11125       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11126           p->zDbFilename, sqlite3_errmsg(p->db));
11127       if( openFlags & OPEN_DB_KEEPALIVE ) return;
11128       exit(1);
11129     }
11130 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11131     sqlite3_enable_load_extension(p->db, 1);
11132 #endif
11133     sqlite3_fileio_init(p->db, 0, 0);
11134     sqlite3_shathree_init(p->db, 0, 0);
11135     sqlite3_completion_init(p->db, 0, 0);
11136 #ifdef SQLITE_HAVE_ZLIB
11137     sqlite3_zipfile_init(p->db, 0, 0);
11138     sqlite3_sqlar_init(p->db, 0, 0);
11139 #endif
11140     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
11141                             shellAddSchemaName, 0, 0);
11142     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
11143                             shellModuleSchema, 0, 0);
11144     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
11145                             shellPutsFunc, 0, 0);
11146 #ifndef SQLITE_NOHAVE_SYSTEM
11147     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
11148                             editFunc, 0, 0);
11149     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
11150                             editFunc, 0, 0);
11151 #endif
11152     if( p->openMode==SHELL_OPEN_ZIPFILE ){
11153       char *zSql = sqlite3_mprintf(
11154          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
11155       sqlite3_exec(p->db, zSql, 0, 0, 0);
11156       sqlite3_free(zSql);
11157     }
11158   }
11159 }
11160
11161 /*
11162 ** Attempt to close the databaes connection.  Report errors.
11163 */
11164 void close_db(sqlite3 *db){
11165   int rc = sqlite3_close(db);
11166   if( rc ){
11167     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11168         rc, sqlite3_errmsg(db));
11169   } 
11170 }
11171
11172 #if HAVE_READLINE || HAVE_EDITLINE
11173 /*
11174 ** Readline completion callbacks
11175 */
11176 static char *readline_completion_generator(const char *text, int state){
11177   static sqlite3_stmt *pStmt = 0;
11178   char *zRet;
11179   if( state==0 ){
11180     char *zSql;
11181     sqlite3_finalize(pStmt);
11182     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11183                            "  FROM completion(%Q) ORDER BY 1", text);
11184     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11185     sqlite3_free(zSql);
11186   }
11187   if( sqlite3_step(pStmt)==SQLITE_ROW ){
11188     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
11189   }else{
11190     sqlite3_finalize(pStmt);
11191     pStmt = 0;
11192     zRet = 0;
11193   }
11194   return zRet;
11195 }
11196 static char **readline_completion(const char *zText, int iStart, int iEnd){
11197   rl_attempted_completion_over = 1;
11198   return rl_completion_matches(zText, readline_completion_generator);
11199 }
11200
11201 #elif HAVE_LINENOISE
11202 /*
11203 ** Linenoise completion callback
11204 */
11205 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
11206   int nLine = strlen30(zLine);
11207   int i, iStart;
11208   sqlite3_stmt *pStmt = 0;
11209   char *zSql;
11210   char zBuf[1000];
11211
11212   if( nLine>sizeof(zBuf)-30 ) return;
11213   if( zLine[0]=='.' || zLine[0]=='#') return;
11214   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
11215   if( i==nLine-1 ) return;
11216   iStart = i+1;
11217   memcpy(zBuf, zLine, iStart);
11218   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11219                          "  FROM completion(%Q,%Q) ORDER BY 1",
11220                          &zLine[iStart], zLine);
11221   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11222   sqlite3_free(zSql);
11223   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
11224   while( sqlite3_step(pStmt)==SQLITE_ROW ){
11225     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
11226     int nCompletion = sqlite3_column_bytes(pStmt, 0);
11227     if( iStart+nCompletion < sizeof(zBuf)-1 ){
11228       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
11229       linenoiseAddCompletion(lc, zBuf);
11230     }
11231   }
11232   sqlite3_finalize(pStmt);
11233 }
11234 #endif
11235
11236 /*
11237 ** Do C-language style dequoting.
11238 **
11239 **    \a    -> alarm
11240 **    \b    -> backspace
11241 **    \t    -> tab
11242 **    \n    -> newline
11243 **    \v    -> vertical tab
11244 **    \f    -> form feed
11245 **    \r    -> carriage return
11246 **    \s    -> space
11247 **    \"    -> "
11248 **    \'    -> '
11249 **    \\    -> backslash
11250 **    \NNN  -> ascii character NNN in octal
11251 */
11252 static void resolve_backslashes(char *z){
11253   int i, j;
11254   char c;
11255   while( *z && *z!='\\' ) z++;
11256   for(i=j=0; (c = z[i])!=0; i++, j++){
11257     if( c=='\\' && z[i+1]!=0 ){
11258       c = z[++i];
11259       if( c=='a' ){
11260         c = '\a';
11261       }else if( c=='b' ){
11262         c = '\b';
11263       }else if( c=='t' ){
11264         c = '\t';
11265       }else if( c=='n' ){
11266         c = '\n';
11267       }else if( c=='v' ){
11268         c = '\v';
11269       }else if( c=='f' ){
11270         c = '\f';
11271       }else if( c=='r' ){
11272         c = '\r';
11273       }else if( c=='"' ){
11274         c = '"';
11275       }else if( c=='\'' ){
11276         c = '\'';
11277       }else if( c=='\\' ){
11278         c = '\\';
11279       }else if( c>='0' && c<='7' ){
11280         c -= '0';
11281         if( z[i+1]>='0' && z[i+1]<='7' ){
11282           i++;
11283           c = (c<<3) + z[i] - '0';
11284           if( z[i+1]>='0' && z[i+1]<='7' ){
11285             i++;
11286             c = (c<<3) + z[i] - '0';
11287           }
11288         }
11289       }
11290     }
11291     z[j] = c;
11292   }
11293   if( j<i ) z[j] = 0;
11294 }
11295
11296 /*
11297 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
11298 ** for TRUE and FALSE.  Return the integer value if appropriate.
11299 */
11300 static int booleanValue(const char *zArg){
11301   int i;
11302   if( zArg[0]=='0' && zArg[1]=='x' ){
11303     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
11304   }else{
11305     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
11306   }
11307   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
11308   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
11309     return 1;
11310   }
11311   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
11312     return 0;
11313   }
11314   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
11315           zArg);
11316   return 0;
11317 }
11318
11319 /*
11320 ** Set or clear a shell flag according to a boolean value.
11321 */
11322 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
11323   if( booleanValue(zArg) ){
11324     ShellSetFlag(p, mFlag);
11325   }else{
11326     ShellClearFlag(p, mFlag);
11327   }
11328 }
11329
11330 /*
11331 ** Close an output file, assuming it is not stderr or stdout
11332 */
11333 static void output_file_close(FILE *f){
11334   if( f && f!=stdout && f!=stderr ) fclose(f);
11335 }
11336
11337 /*
11338 ** Try to open an output file.   The names "stdout" and "stderr" are
11339 ** recognized and do the right thing.  NULL is returned if the output
11340 ** filename is "off".
11341 */
11342 static FILE *output_file_open(const char *zFile, int bTextMode){
11343   FILE *f;
11344   if( strcmp(zFile,"stdout")==0 ){
11345     f = stdout;
11346   }else if( strcmp(zFile, "stderr")==0 ){
11347     f = stderr;
11348   }else if( strcmp(zFile, "off")==0 ){
11349     f = 0;
11350   }else{
11351     f = fopen(zFile, bTextMode ? "w" : "wb");
11352     if( f==0 ){
11353       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
11354     }
11355   }
11356   return f;
11357 }
11358
11359 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
11360 /*
11361 ** A routine for handling output from sqlite3_trace().
11362 */
11363 static int sql_trace_callback(
11364   unsigned mType,
11365   void *pArg,
11366   void *pP,
11367   void *pX
11368 ){
11369   FILE *f = (FILE*)pArg;
11370   UNUSED_PARAMETER(mType);
11371   UNUSED_PARAMETER(pP);
11372   if( f ){
11373     const char *z = (const char*)pX;
11374     int i = strlen30(z);
11375     while( i>0 && z[i-1]==';' ){ i--; }
11376     utf8_printf(f, "%.*s;\n", i, z);
11377   }
11378   return 0;
11379 }
11380 #endif
11381
11382 /*
11383 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
11384 ** a useful spot to set a debugger breakpoint.
11385 */
11386 static void test_breakpoint(void){
11387   static int nCall = 0;
11388   nCall++;
11389 }
11390
11391 /*
11392 ** An object used to read a CSV and other files for import.
11393 */
11394 typedef struct ImportCtx ImportCtx;
11395 struct ImportCtx {
11396   const char *zFile;  /* Name of the input file */
11397   FILE *in;           /* Read the CSV text from this input stream */
11398   char *z;            /* Accumulated text for a field */
11399   int n;              /* Number of bytes in z */
11400   int nAlloc;         /* Space allocated for z[] */
11401   int nLine;          /* Current line number */
11402   int bNotFirst;      /* True if one or more bytes already read */
11403   int cTerm;          /* Character that terminated the most recent field */
11404   int cColSep;        /* The column separator character.  (Usually ",") */
11405   int cRowSep;        /* The row separator character.  (Usually "\n") */
11406 };
11407
11408 /* Append a single byte to z[] */
11409 static void import_append_char(ImportCtx *p, int c){
11410   if( p->n+1>=p->nAlloc ){
11411     p->nAlloc += p->nAlloc + 100;
11412     p->z = sqlite3_realloc64(p->z, p->nAlloc);
11413     if( p->z==0 ) shell_out_of_memory();
11414   }
11415   p->z[p->n++] = (char)c;
11416 }
11417
11418 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
11419 ** with the option of having a separator other than ",".
11420 **
11421 **   +  Input comes from p->in.
11422 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
11423 **      from sqlite3_malloc64().
11424 **   +  Use p->cSep as the column separator.  The default is ",".
11425 **   +  Use p->rSep as the row separator.  The default is "\n".
11426 **   +  Keep track of the line number in p->nLine.
11427 **   +  Store the character that terminates the field in p->cTerm.  Store
11428 **      EOF on end-of-file.
11429 **   +  Report syntax errors on stderr
11430 */
11431 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
11432   int c;
11433   int cSep = p->cColSep;
11434   int rSep = p->cRowSep;
11435   p->n = 0;
11436   c = fgetc(p->in);
11437   if( c==EOF || seenInterrupt ){
11438     p->cTerm = EOF;
11439     return 0;
11440   }
11441   if( c=='"' ){
11442     int pc, ppc;
11443     int startLine = p->nLine;
11444     int cQuote = c;
11445     pc = ppc = 0;
11446     while( 1 ){
11447       c = fgetc(p->in);
11448       if( c==rSep ) p->nLine++;
11449       if( c==cQuote ){
11450         if( pc==cQuote ){
11451           pc = 0;
11452           continue;
11453         }
11454       }
11455       if( (c==cSep && pc==cQuote)
11456        || (c==rSep && pc==cQuote)
11457        || (c==rSep && pc=='\r' && ppc==cQuote)
11458        || (c==EOF && pc==cQuote)
11459       ){
11460         do{ p->n--; }while( p->z[p->n]!=cQuote );
11461         p->cTerm = c;
11462         break;
11463       }
11464       if( pc==cQuote && c!='\r' ){
11465         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
11466                 p->zFile, p->nLine, cQuote);
11467       }
11468       if( c==EOF ){
11469         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
11470                 p->zFile, startLine, cQuote);
11471         p->cTerm = c;
11472         break;
11473       }
11474       import_append_char(p, c);
11475       ppc = pc;
11476       pc = c;
11477     }
11478   }else{
11479     /* If this is the first field being parsed and it begins with the
11480     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
11481     if( (c&0xff)==0xef && p->bNotFirst==0 ){
11482       import_append_char(p, c);
11483       c = fgetc(p->in);
11484       if( (c&0xff)==0xbb ){
11485         import_append_char(p, c);
11486         c = fgetc(p->in);
11487         if( (c&0xff)==0xbf ){
11488           p->bNotFirst = 1;
11489           p->n = 0;
11490           return csv_read_one_field(p);
11491         }
11492       }
11493     }
11494     while( c!=EOF && c!=cSep && c!=rSep ){
11495       import_append_char(p, c);
11496       c = fgetc(p->in);
11497     }
11498     if( c==rSep ){
11499       p->nLine++;
11500       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
11501     }
11502     p->cTerm = c;
11503   }
11504   if( p->z ) p->z[p->n] = 0;
11505   p->bNotFirst = 1;
11506   return p->z;
11507 }
11508
11509 /* Read a single field of ASCII delimited text.
11510 **
11511 **   +  Input comes from p->in.
11512 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
11513 **      from sqlite3_malloc64().
11514 **   +  Use p->cSep as the column separator.  The default is "\x1F".
11515 **   +  Use p->rSep as the row separator.  The default is "\x1E".
11516 **   +  Keep track of the row number in p->nLine.
11517 **   +  Store the character that terminates the field in p->cTerm.  Store
11518 **      EOF on end-of-file.
11519 **   +  Report syntax errors on stderr
11520 */
11521 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
11522   int c;
11523   int cSep = p->cColSep;
11524   int rSep = p->cRowSep;
11525   p->n = 0;
11526   c = fgetc(p->in);
11527   if( c==EOF || seenInterrupt ){
11528     p->cTerm = EOF;
11529     return 0;
11530   }
11531   while( c!=EOF && c!=cSep && c!=rSep ){
11532     import_append_char(p, c);
11533     c = fgetc(p->in);
11534   }
11535   if( c==rSep ){
11536     p->nLine++;
11537   }
11538   p->cTerm = c;
11539   if( p->z ) p->z[p->n] = 0;
11540   return p->z;
11541 }
11542
11543 /*
11544 ** Try to transfer data for table zTable.  If an error is seen while
11545 ** moving forward, try to go backwards.  The backwards movement won't
11546 ** work for WITHOUT ROWID tables.
11547 */
11548 static void tryToCloneData(
11549   ShellState *p,
11550   sqlite3 *newDb,
11551   const char *zTable
11552 ){
11553   sqlite3_stmt *pQuery = 0;
11554   sqlite3_stmt *pInsert = 0;
11555   char *zQuery = 0;
11556   char *zInsert = 0;
11557   int rc;
11558   int i, j, n;
11559   int nTable = strlen30(zTable);
11560   int k = 0;
11561   int cnt = 0;
11562   const int spinRate = 10000;
11563
11564   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
11565   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11566   if( rc ){
11567     utf8_printf(stderr, "Error %d: %s on [%s]\n",
11568             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11569             zQuery);
11570     goto end_data_xfer;
11571   }
11572   n = sqlite3_column_count(pQuery);
11573   zInsert = sqlite3_malloc64(200 + nTable + n*3);
11574   if( zInsert==0 ) shell_out_of_memory();
11575   sqlite3_snprintf(200+nTable,zInsert,
11576                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
11577   i = strlen30(zInsert);
11578   for(j=1; j<n; j++){
11579     memcpy(zInsert+i, ",?", 2);
11580     i += 2;
11581   }
11582   memcpy(zInsert+i, ");", 3);
11583   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
11584   if( rc ){
11585     utf8_printf(stderr, "Error %d: %s on [%s]\n",
11586             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
11587             zQuery);
11588     goto end_data_xfer;
11589   }
11590   for(k=0; k<2; k++){
11591     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11592       for(i=0; i<n; i++){
11593         switch( sqlite3_column_type(pQuery, i) ){
11594           case SQLITE_NULL: {
11595             sqlite3_bind_null(pInsert, i+1);
11596             break;
11597           }
11598           case SQLITE_INTEGER: {
11599             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
11600             break;
11601           }
11602           case SQLITE_FLOAT: {
11603             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
11604             break;
11605           }
11606           case SQLITE_TEXT: {
11607             sqlite3_bind_text(pInsert, i+1,
11608                              (const char*)sqlite3_column_text(pQuery,i),
11609                              -1, SQLITE_STATIC);
11610             break;
11611           }
11612           case SQLITE_BLOB: {
11613             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
11614                                             sqlite3_column_bytes(pQuery,i),
11615                                             SQLITE_STATIC);
11616             break;
11617           }
11618         }
11619       } /* End for */
11620       rc = sqlite3_step(pInsert);
11621       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
11622         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
11623                         sqlite3_errmsg(newDb));
11624       }
11625       sqlite3_reset(pInsert);
11626       cnt++;
11627       if( (cnt%spinRate)==0 ){
11628         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
11629         fflush(stdout);
11630       }
11631     } /* End while */
11632     if( rc==SQLITE_DONE ) break;
11633     sqlite3_finalize(pQuery);
11634     sqlite3_free(zQuery);
11635     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
11636                              zTable);
11637     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11638     if( rc ){
11639       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
11640       break;
11641     }
11642   } /* End for(k=0...) */
11643
11644 end_data_xfer:
11645   sqlite3_finalize(pQuery);
11646   sqlite3_finalize(pInsert);
11647   sqlite3_free(zQuery);
11648   sqlite3_free(zInsert);
11649 }
11650
11651
11652 /*
11653 ** Try to transfer all rows of the schema that match zWhere.  For
11654 ** each row, invoke xForEach() on the object defined by that row.
11655 ** If an error is encountered while moving forward through the
11656 ** sqlite_master table, try again moving backwards.
11657 */
11658 static void tryToCloneSchema(
11659   ShellState *p,
11660   sqlite3 *newDb,
11661   const char *zWhere,
11662   void (*xForEach)(ShellState*,sqlite3*,const char*)
11663 ){
11664   sqlite3_stmt *pQuery = 0;
11665   char *zQuery = 0;
11666   int rc;
11667   const unsigned char *zName;
11668   const unsigned char *zSql;
11669   char *zErrMsg = 0;
11670
11671   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
11672                            " WHERE %s", zWhere);
11673   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11674   if( rc ){
11675     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
11676                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11677                     zQuery);
11678     goto end_schema_xfer;
11679   }
11680   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11681     zName = sqlite3_column_text(pQuery, 0);
11682     zSql = sqlite3_column_text(pQuery, 1);
11683     printf("%s... ", zName); fflush(stdout);
11684     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
11685     if( zErrMsg ){
11686       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
11687       sqlite3_free(zErrMsg);
11688       zErrMsg = 0;
11689     }
11690     if( xForEach ){
11691       xForEach(p, newDb, (const char*)zName);
11692     }
11693     printf("done\n");
11694   }
11695   if( rc!=SQLITE_DONE ){
11696     sqlite3_finalize(pQuery);
11697     sqlite3_free(zQuery);
11698     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
11699                              " WHERE %s ORDER BY rowid DESC", zWhere);
11700     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11701     if( rc ){
11702       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
11703                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11704                       zQuery);
11705       goto end_schema_xfer;
11706     }
11707     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11708       zName = sqlite3_column_text(pQuery, 0);
11709       zSql = sqlite3_column_text(pQuery, 1);
11710       printf("%s... ", zName); fflush(stdout);
11711       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
11712       if( zErrMsg ){
11713         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
11714         sqlite3_free(zErrMsg);
11715         zErrMsg = 0;
11716       }
11717       if( xForEach ){
11718         xForEach(p, newDb, (const char*)zName);
11719       }
11720       printf("done\n");
11721     }
11722   }
11723 end_schema_xfer:
11724   sqlite3_finalize(pQuery);
11725   sqlite3_free(zQuery);
11726 }
11727
11728 /*
11729 ** Open a new database file named "zNewDb".  Try to recover as much information
11730 ** as possible out of the main database (which might be corrupt) and write it
11731 ** into zNewDb.
11732 */
11733 static void tryToClone(ShellState *p, const char *zNewDb){
11734   int rc;
11735   sqlite3 *newDb = 0;
11736   if( access(zNewDb,0)==0 ){
11737     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
11738     return;
11739   }
11740   rc = sqlite3_open(zNewDb, &newDb);
11741   if( rc ){
11742     utf8_printf(stderr, "Cannot create output database: %s\n",
11743             sqlite3_errmsg(newDb));
11744   }else{
11745     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
11746     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
11747     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
11748     tryToCloneSchema(p, newDb, "type!='table'", 0);
11749     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
11750     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
11751   }
11752   close_db(newDb);
11753 }
11754
11755 /*
11756 ** Change the output file back to stdout.
11757 **
11758 ** If the p->doXdgOpen flag is set, that means the output was being
11759 ** redirected to a temporary file named by p->zTempFile.  In that case,
11760 ** launch start/open/xdg-open on that temporary file.
11761 */
11762 static void output_reset(ShellState *p){
11763   if( p->outfile[0]=='|' ){
11764 #ifndef SQLITE_OMIT_POPEN
11765     pclose(p->out);
11766 #endif
11767   }else{
11768     output_file_close(p->out);
11769 #ifndef SQLITE_NOHAVE_SYSTEM
11770     if( p->doXdgOpen ){
11771       const char *zXdgOpenCmd =
11772 #if defined(_WIN32)
11773       "start";
11774 #elif defined(__APPLE__)
11775       "open";
11776 #else
11777       "xdg-open";
11778 #endif
11779       char *zCmd;
11780       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
11781       if( system(zCmd) ){
11782         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
11783       }
11784       sqlite3_free(zCmd);
11785       outputModePop(p);
11786       p->doXdgOpen = 0;
11787     }
11788 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
11789   }
11790   p->outfile[0] = 0;
11791   p->out = stdout;
11792 }
11793
11794 /*
11795 ** Run an SQL command and return the single integer result.
11796 */
11797 static int db_int(ShellState *p, const char *zSql){
11798   sqlite3_stmt *pStmt;
11799   int res = 0;
11800   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11801   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
11802     res = sqlite3_column_int(pStmt,0);
11803   }
11804   sqlite3_finalize(pStmt);
11805   return res;
11806 }
11807
11808 /*
11809 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
11810 */
11811 static unsigned int get2byteInt(unsigned char *a){
11812   return (a[0]<<8) + a[1];
11813 }
11814 static unsigned int get4byteInt(unsigned char *a){
11815   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
11816 }
11817
11818 /*
11819 ** Implementation of the ".info" command.
11820 **
11821 ** Return 1 on error, 2 to exit, and 0 otherwise.
11822 */
11823 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
11824   static const struct { const char *zName; int ofst; } aField[] = {
11825      { "file change counter:",  24  },
11826      { "database page count:",  28  },
11827      { "freelist page count:",  36  },
11828      { "schema cookie:",        40  },
11829      { "schema format:",        44  },
11830      { "default cache size:",   48  },
11831      { "autovacuum top root:",  52  },
11832      { "incremental vacuum:",   64  },
11833      { "text encoding:",        56  },
11834      { "user version:",         60  },
11835      { "application id:",       68  },
11836      { "software version:",     96  },
11837   };
11838   static const struct { const char *zName; const char *zSql; } aQuery[] = {
11839      { "number of tables:",
11840        "SELECT count(*) FROM %s WHERE type='table'" },
11841      { "number of indexes:",
11842        "SELECT count(*) FROM %s WHERE type='index'" },
11843      { "number of triggers:",
11844        "SELECT count(*) FROM %s WHERE type='trigger'" },
11845      { "number of views:",
11846        "SELECT count(*) FROM %s WHERE type='view'" },
11847      { "schema size:",
11848        "SELECT total(length(sql)) FROM %s" },
11849   };
11850   int i;
11851   char *zSchemaTab;
11852   char *zDb = nArg>=2 ? azArg[1] : "main";
11853   sqlite3_stmt *pStmt = 0;
11854   unsigned char aHdr[100];
11855   open_db(p, 0);
11856   if( p->db==0 ) return 1;
11857   sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
11858                      -1, &pStmt, 0);
11859   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
11860   if( sqlite3_step(pStmt)==SQLITE_ROW
11861    && sqlite3_column_bytes(pStmt,0)>100
11862   ){
11863     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
11864     sqlite3_finalize(pStmt);
11865   }else{
11866     raw_printf(stderr, "unable to read database header\n");
11867     sqlite3_finalize(pStmt);
11868     return 1;
11869   }
11870   i = get2byteInt(aHdr+16);
11871   if( i==1 ) i = 65536;
11872   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
11873   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
11874   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
11875   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
11876   for(i=0; i<ArraySize(aField); i++){
11877     int ofst = aField[i].ofst;
11878     unsigned int val = get4byteInt(aHdr + ofst);
11879     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
11880     switch( ofst ){
11881       case 56: {
11882         if( val==1 ) raw_printf(p->out, " (utf8)");
11883         if( val==2 ) raw_printf(p->out, " (utf16le)");
11884         if( val==3 ) raw_printf(p->out, " (utf16be)");
11885       }
11886     }
11887     raw_printf(p->out, "\n");
11888   }
11889   if( zDb==0 ){
11890     zSchemaTab = sqlite3_mprintf("main.sqlite_master");
11891   }else if( strcmp(zDb,"temp")==0 ){
11892     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
11893   }else{
11894     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
11895   }
11896   for(i=0; i<ArraySize(aQuery); i++){
11897     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
11898     int val = db_int(p, zSql);
11899     sqlite3_free(zSql);
11900     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
11901   }
11902   sqlite3_free(zSchemaTab);
11903   return 0;
11904 }
11905
11906 /*
11907 ** Print the current sqlite3_errmsg() value to stderr and return 1.
11908 */
11909 static int shellDatabaseError(sqlite3 *db){
11910   const char *zErr = sqlite3_errmsg(db);
11911   utf8_printf(stderr, "Error: %s\n", zErr);
11912   return 1;
11913 }
11914
11915 /*
11916 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
11917 ** if they match and FALSE (0) if they do not match.
11918 **
11919 ** Globbing rules:
11920 **
11921 **      '*'       Matches any sequence of zero or more characters.
11922 **
11923 **      '?'       Matches exactly one character.
11924 **
11925 **     [...]      Matches one character from the enclosed list of
11926 **                characters.
11927 **
11928 **     [^...]     Matches one character not in the enclosed list.
11929 **
11930 **      '#'       Matches any sequence of one or more digits with an
11931 **                optional + or - sign in front
11932 **
11933 **      ' '       Any span of whitespace matches any other span of
11934 **                whitespace.
11935 **
11936 ** Extra whitespace at the end of z[] is ignored.
11937 */
11938 static int testcase_glob(const char *zGlob, const char *z){
11939   int c, c2;
11940   int invert;
11941   int seen;
11942
11943   while( (c = (*(zGlob++)))!=0 ){
11944     if( IsSpace(c) ){
11945       if( !IsSpace(*z) ) return 0;
11946       while( IsSpace(*zGlob) ) zGlob++;
11947       while( IsSpace(*z) ) z++;
11948     }else if( c=='*' ){
11949       while( (c=(*(zGlob++))) == '*' || c=='?' ){
11950         if( c=='?' && (*(z++))==0 ) return 0;
11951       }
11952       if( c==0 ){
11953         return 1;
11954       }else if( c=='[' ){
11955         while( *z && testcase_glob(zGlob-1,z)==0 ){
11956           z++;
11957         }
11958         return (*z)!=0;
11959       }
11960       while( (c2 = (*(z++)))!=0 ){
11961         while( c2!=c ){
11962           c2 = *(z++);
11963           if( c2==0 ) return 0;
11964         }
11965         if( testcase_glob(zGlob,z) ) return 1;
11966       }
11967       return 0;
11968     }else if( c=='?' ){
11969       if( (*(z++))==0 ) return 0;
11970     }else if( c=='[' ){
11971       int prior_c = 0;
11972       seen = 0;
11973       invert = 0;
11974       c = *(z++);
11975       if( c==0 ) return 0;
11976       c2 = *(zGlob++);
11977       if( c2=='^' ){
11978         invert = 1;
11979         c2 = *(zGlob++);
11980       }
11981       if( c2==']' ){
11982         if( c==']' ) seen = 1;
11983         c2 = *(zGlob++);
11984       }
11985       while( c2 && c2!=']' ){
11986         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
11987           c2 = *(zGlob++);
11988           if( c>=prior_c && c<=c2 ) seen = 1;
11989           prior_c = 0;
11990         }else{
11991           if( c==c2 ){
11992             seen = 1;
11993           }
11994           prior_c = c2;
11995         }
11996         c2 = *(zGlob++);
11997       }
11998       if( c2==0 || (seen ^ invert)==0 ) return 0;
11999     }else if( c=='#' ){
12000       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
12001       if( !IsDigit(z[0]) ) return 0;
12002       z++;
12003       while( IsDigit(z[0]) ){ z++; }
12004     }else{
12005       if( c!=(*(z++)) ) return 0;
12006     }
12007   }
12008   while( IsSpace(*z) ){ z++; }
12009   return *z==0;
12010 }
12011
12012
12013 /*
12014 ** Compare the string as a command-line option with either one or two
12015 ** initial "-" characters.
12016 */
12017 static int optionMatch(const char *zStr, const char *zOpt){
12018   if( zStr[0]!='-' ) return 0;
12019   zStr++;
12020   if( zStr[0]=='-' ) zStr++;
12021   return strcmp(zStr, zOpt)==0;
12022 }
12023
12024 /*
12025 ** Delete a file.
12026 */
12027 int shellDeleteFile(const char *zFilename){
12028   int rc;
12029 #ifdef _WIN32
12030   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
12031   rc = _wunlink(z);
12032   sqlite3_free(z);
12033 #else
12034   rc = unlink(zFilename);
12035 #endif
12036   return rc;
12037 }
12038
12039 /*
12040 ** Try to delete the temporary file (if there is one) and free the
12041 ** memory used to hold the name of the temp file.
12042 */
12043 static void clearTempFile(ShellState *p){
12044   if( p->zTempFile==0 ) return;
12045   if( p->doXdgOpen ) return;
12046   if( shellDeleteFile(p->zTempFile) ) return;
12047   sqlite3_free(p->zTempFile);
12048   p->zTempFile = 0;
12049 }
12050
12051 /*
12052 ** Create a new temp file name with the given suffix.
12053 */
12054 static void newTempFile(ShellState *p, const char *zSuffix){
12055   clearTempFile(p);
12056   sqlite3_free(p->zTempFile);
12057   p->zTempFile = 0;
12058   if( p->db ){
12059     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
12060   }
12061   if( p->zTempFile==0 ){
12062     sqlite3_uint64 r;
12063     sqlite3_randomness(sizeof(r), &r);
12064     p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
12065   }else{
12066     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
12067   }
12068   if( p->zTempFile==0 ){
12069     raw_printf(stderr, "out of memory\n");
12070     exit(1);
12071   }
12072 }
12073
12074
12075 /*
12076 ** The implementation of SQL scalar function fkey_collate_clause(), used
12077 ** by the ".lint fkey-indexes" command. This scalar function is always
12078 ** called with four arguments - the parent table name, the parent column name,
12079 ** the child table name and the child column name.
12080 **
12081 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
12082 **
12083 ** If either of the named tables or columns do not exist, this function
12084 ** returns an empty string. An empty string is also returned if both tables
12085 ** and columns exist but have the same default collation sequence. Or,
12086 ** if both exist but the default collation sequences are different, this
12087 ** function returns the string " COLLATE <parent-collation>", where
12088 ** <parent-collation> is the default collation sequence of the parent column.
12089 */
12090 static void shellFkeyCollateClause(
12091   sqlite3_context *pCtx,
12092   int nVal,
12093   sqlite3_value **apVal
12094 ){
12095   sqlite3 *db = sqlite3_context_db_handle(pCtx);
12096   const char *zParent;
12097   const char *zParentCol;
12098   const char *zParentSeq;
12099   const char *zChild;
12100   const char *zChildCol;
12101   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
12102   int rc;
12103
12104   assert( nVal==4 );
12105   zParent = (const char*)sqlite3_value_text(apVal[0]);
12106   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
12107   zChild = (const char*)sqlite3_value_text(apVal[2]);
12108   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
12109
12110   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
12111   rc = sqlite3_table_column_metadata(
12112       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
12113   );
12114   if( rc==SQLITE_OK ){
12115     rc = sqlite3_table_column_metadata(
12116         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
12117     );
12118   }
12119
12120   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
12121     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
12122     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
12123     sqlite3_free(z);
12124   }
12125 }
12126
12127
12128 /*
12129 ** The implementation of dot-command ".lint fkey-indexes".
12130 */
12131 static int lintFkeyIndexes(
12132   ShellState *pState,             /* Current shell tool state */
12133   char **azArg,                   /* Array of arguments passed to dot command */
12134   int nArg                        /* Number of entries in azArg[] */
12135 ){
12136   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
12137   FILE *out = pState->out;        /* Stream to write non-error output to */
12138   int bVerbose = 0;               /* If -verbose is present */
12139   int bGroupByParent = 0;         /* If -groupbyparent is present */
12140   int i;                          /* To iterate through azArg[] */
12141   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
12142   int rc;                         /* Return code */
12143   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
12144
12145   /*
12146   ** This SELECT statement returns one row for each foreign key constraint
12147   ** in the schema of the main database. The column values are:
12148   **
12149   ** 0. The text of an SQL statement similar to:
12150   **
12151   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
12152   **
12153   **    This SELECT is similar to the one that the foreign keys implementation
12154   **    needs to run internally on child tables. If there is an index that can
12155   **    be used to optimize this query, then it can also be used by the FK
12156   **    implementation to optimize DELETE or UPDATE statements on the parent
12157   **    table.
12158   **
12159   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
12160   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
12161   **    contains an index that can be used to optimize the query.
12162   **
12163   ** 2. Human readable text that describes the child table and columns. e.g.
12164   **
12165   **       "child_table(child_key1, child_key2)"
12166   **
12167   ** 3. Human readable text that describes the parent table and columns. e.g.
12168   **
12169   **       "parent_table(parent_key1, parent_key2)"
12170   **
12171   ** 4. A full CREATE INDEX statement for an index that could be used to
12172   **    optimize DELETE or UPDATE statements on the parent table. e.g.
12173   **
12174   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
12175   **
12176   ** 5. The name of the parent table.
12177   **
12178   ** These six values are used by the C logic below to generate the report.
12179   */
12180   const char *zSql =
12181   "SELECT "
12182     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
12183     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
12184     "  || fkey_collate_clause("
12185     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
12186     ", "
12187     "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
12188     "  || group_concat('*=?', ' AND ') || ')'"
12189     ", "
12190     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
12191     ", "
12192     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
12193     ", "
12194     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
12195     "  || ' ON ' || quote(s.name) || '('"
12196     "  || group_concat(quote(f.[from]) ||"
12197     "        fkey_collate_clause("
12198     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
12199     "  || ');'"
12200     ", "
12201     "     f.[table] "
12202     "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
12203     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
12204     "GROUP BY s.name, f.id "
12205     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
12206   ;
12207   const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
12208
12209   for(i=2; i<nArg; i++){
12210     int n = strlen30(azArg[i]);
12211     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
12212       bVerbose = 1;
12213     }
12214     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
12215       bGroupByParent = 1;
12216       zIndent = "    ";
12217     }
12218     else{
12219       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
12220           azArg[0], azArg[1]
12221       );
12222       return SQLITE_ERROR;
12223     }
12224   }
12225
12226   /* Register the fkey_collate_clause() SQL function */
12227   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
12228       0, shellFkeyCollateClause, 0, 0
12229   );
12230
12231
12232   if( rc==SQLITE_OK ){
12233     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
12234   }
12235   if( rc==SQLITE_OK ){
12236     sqlite3_bind_int(pSql, 1, bGroupByParent);
12237   }
12238
12239   if( rc==SQLITE_OK ){
12240     int rc2;
12241     char *zPrev = 0;
12242     while( SQLITE_ROW==sqlite3_step(pSql) ){
12243       int res = -1;
12244       sqlite3_stmt *pExplain = 0;
12245       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
12246       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
12247       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
12248       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
12249       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
12250       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
12251
12252       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
12253       if( rc!=SQLITE_OK ) break;
12254       if( SQLITE_ROW==sqlite3_step(pExplain) ){
12255         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
12256         res = (
12257               0==sqlite3_strglob(zGlob, zPlan)
12258            || 0==sqlite3_strglob(zGlobIPK, zPlan)
12259         );
12260       }
12261       rc = sqlite3_finalize(pExplain);
12262       if( rc!=SQLITE_OK ) break;
12263
12264       if( res<0 ){
12265         raw_printf(stderr, "Error: internal error");
12266         break;
12267       }else{
12268         if( bGroupByParent
12269         && (bVerbose || res==0)
12270         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
12271         ){
12272           raw_printf(out, "-- Parent table %s\n", zParent);
12273           sqlite3_free(zPrev);
12274           zPrev = sqlite3_mprintf("%s", zParent);
12275         }
12276
12277         if( res==0 ){
12278           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
12279         }else if( bVerbose ){
12280           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
12281               zIndent, zFrom, zTarget
12282           );
12283         }
12284       }
12285     }
12286     sqlite3_free(zPrev);
12287
12288     if( rc!=SQLITE_OK ){
12289       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12290     }
12291
12292     rc2 = sqlite3_finalize(pSql);
12293     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
12294       rc = rc2;
12295       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12296     }
12297   }else{
12298     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12299   }
12300
12301   return rc;
12302 }
12303
12304 /*
12305 ** Implementation of ".lint" dot command.
12306 */
12307 static int lintDotCommand(
12308   ShellState *pState,             /* Current shell tool state */
12309   char **azArg,                   /* Array of arguments passed to dot command */
12310   int nArg                        /* Number of entries in azArg[] */
12311 ){
12312   int n;
12313   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
12314   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
12315   return lintFkeyIndexes(pState, azArg, nArg);
12316
12317  usage:
12318   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
12319   raw_printf(stderr, "Where sub-commands are:\n");
12320   raw_printf(stderr, "    fkey-indexes\n");
12321   return SQLITE_ERROR;
12322 }
12323
12324 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12325 /*********************************************************************************
12326 ** The ".archive" or ".ar" command.
12327 */
12328 static void shellPrepare(
12329   sqlite3 *db, 
12330   int *pRc, 
12331   const char *zSql, 
12332   sqlite3_stmt **ppStmt
12333 ){
12334   *ppStmt = 0;
12335   if( *pRc==SQLITE_OK ){
12336     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12337     if( rc!=SQLITE_OK ){
12338       raw_printf(stderr, "sql error: %s (%d)\n", 
12339           sqlite3_errmsg(db), sqlite3_errcode(db)
12340       );
12341       *pRc = rc;
12342     }
12343   }
12344 }
12345
12346 static void shellPreparePrintf(
12347   sqlite3 *db, 
12348   int *pRc, 
12349   sqlite3_stmt **ppStmt,
12350   const char *zFmt, 
12351   ...
12352 ){
12353   *ppStmt = 0;
12354   if( *pRc==SQLITE_OK ){
12355     va_list ap;
12356     char *z;
12357     va_start(ap, zFmt);
12358     z = sqlite3_vmprintf(zFmt, ap);
12359     if( z==0 ){
12360       *pRc = SQLITE_NOMEM;
12361     }else{
12362       shellPrepare(db, pRc, z, ppStmt);
12363       sqlite3_free(z);
12364     }
12365   }
12366 }
12367
12368 static void shellFinalize(
12369   int *pRc, 
12370   sqlite3_stmt *pStmt
12371 ){
12372   if( pStmt ){
12373     sqlite3 *db = sqlite3_db_handle(pStmt);
12374     int rc = sqlite3_finalize(pStmt);
12375     if( *pRc==SQLITE_OK ){
12376       if( rc!=SQLITE_OK ){
12377         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12378       }
12379       *pRc = rc;
12380     }
12381   }
12382 }
12383
12384 static void shellReset(
12385   int *pRc, 
12386   sqlite3_stmt *pStmt
12387 ){
12388   int rc = sqlite3_reset(pStmt);
12389   if( *pRc==SQLITE_OK ){
12390     if( rc!=SQLITE_OK ){
12391       sqlite3 *db = sqlite3_db_handle(pStmt);
12392       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12393     }
12394     *pRc = rc;
12395   }
12396 }
12397 /*
12398 ** Structure representing a single ".ar" command.
12399 */
12400 typedef struct ArCommand ArCommand;
12401 struct ArCommand {
12402   u8 eCmd;                        /* An AR_CMD_* value */
12403   u8 bVerbose;                    /* True if --verbose */
12404   u8 bZip;                        /* True if the archive is a ZIP */
12405   u8 bDryRun;                     /* True if --dry-run */
12406   u8 bAppend;                     /* True if --append */
12407   u8 fromCmdLine;                 /* Run from -A instead of .archive */
12408   int nArg;                       /* Number of command arguments */
12409   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
12410   const char *zFile;              /* --file argument, or NULL */
12411   const char *zDir;               /* --directory argument, or NULL */
12412   char **azArg;                   /* Array of command arguments */
12413   ShellState *p;                  /* Shell state */
12414   sqlite3 *db;                    /* Database containing the archive */
12415 };
12416
12417 /*
12418 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
12419 */
12420 static int arUsage(FILE *f){
12421   raw_printf(f,
12422 "\n"
12423 "Usage: .ar [OPTION...] [FILE...]\n"
12424 "The .ar command manages sqlar archives.\n"
12425 "\n"
12426 "Examples:\n"
12427 "  .ar -cf archive.sar foo bar    # Create archive.sar from files foo and bar\n"
12428 "  .ar -tf archive.sar            # List members of archive.sar\n"
12429 "  .ar -xvf archive.sar           # Verbosely extract files from archive.sar\n"
12430 "\n"
12431 "Each command line must feature exactly one command option:\n"
12432 "  -c, --create               Create a new archive\n"
12433 "  -u, --update               Update or add files to an existing archive\n"
12434 "  -t, --list                 List contents of archive\n"
12435 "  -x, --extract              Extract files from archive\n"
12436 "\n"
12437 "And zero or more optional options:\n"
12438 "  -v, --verbose              Print each filename as it is processed\n"
12439 "  -f FILE, --file FILE       Operate on archive FILE (default is current db)\n"
12440 "  -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS\n"
12441 "  -C DIR, --directory DIR    Change to directory DIR to read/extract files\n"
12442 "  -n, --dryrun               Show the SQL that would have occurred\n"
12443 "\n"
12444 "See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
12445 "\n"
12446 );
12447   return SQLITE_ERROR;
12448 }
12449
12450 /*
12451 ** Print an error message for the .ar command to stderr and return 
12452 ** SQLITE_ERROR.
12453 */
12454 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
12455   va_list ap;
12456   char *z;
12457   va_start(ap, zFmt);
12458   z = sqlite3_vmprintf(zFmt, ap);
12459   va_end(ap);
12460   utf8_printf(stderr, "Error: %s\n", z);
12461   if( pAr->fromCmdLine ){
12462     utf8_printf(stderr, "Use \"-A\" for more help\n");
12463   }else{
12464     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
12465   }
12466   sqlite3_free(z);
12467   return SQLITE_ERROR;
12468 }
12469
12470 /*
12471 ** Values for ArCommand.eCmd.
12472 */
12473 #define AR_CMD_CREATE       1
12474 #define AR_CMD_EXTRACT      2
12475 #define AR_CMD_LIST         3
12476 #define AR_CMD_UPDATE       4
12477 #define AR_CMD_HELP         5
12478
12479 /*
12480 ** Other (non-command) switches.
12481 */
12482 #define AR_SWITCH_VERBOSE     6
12483 #define AR_SWITCH_FILE        7
12484 #define AR_SWITCH_DIRECTORY   8
12485 #define AR_SWITCH_APPEND      9
12486 #define AR_SWITCH_DRYRUN     10
12487
12488 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
12489   switch( eSwitch ){
12490     case AR_CMD_CREATE:
12491     case AR_CMD_EXTRACT:
12492     case AR_CMD_LIST:
12493     case AR_CMD_UPDATE:
12494     case AR_CMD_HELP:
12495       if( pAr->eCmd ){
12496         return arErrorMsg(pAr, "multiple command options");
12497       }
12498       pAr->eCmd = eSwitch;
12499       break;
12500
12501     case AR_SWITCH_DRYRUN:
12502       pAr->bDryRun = 1;
12503       break;
12504     case AR_SWITCH_VERBOSE:
12505       pAr->bVerbose = 1;
12506       break;
12507     case AR_SWITCH_APPEND:
12508       pAr->bAppend = 1;
12509       /* Fall thru into --file */
12510     case AR_SWITCH_FILE:
12511       pAr->zFile = zArg;
12512       break;
12513     case AR_SWITCH_DIRECTORY:
12514       pAr->zDir = zArg;
12515       break;
12516   }
12517
12518   return SQLITE_OK;
12519 }
12520
12521 /*
12522 ** Parse the command line for an ".ar" command. The results are written into
12523 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
12524 ** successfully, otherwise an error message is written to stderr and 
12525 ** SQLITE_ERROR returned.
12526 */
12527 static int arParseCommand(
12528   char **azArg,                   /* Array of arguments passed to dot command */
12529   int nArg,                       /* Number of entries in azArg[] */
12530   ArCommand *pAr                  /* Populate this object */
12531 ){
12532   struct ArSwitch {
12533     const char *zLong;
12534     char cShort;
12535     u8 eSwitch;
12536     u8 bArg;
12537   } aSwitch[] = {
12538     { "create",    'c', AR_CMD_CREATE,       0 },
12539     { "extract",   'x', AR_CMD_EXTRACT,      0 },
12540     { "list",      't', AR_CMD_LIST,         0 },
12541     { "update",    'u', AR_CMD_UPDATE,       0 },
12542     { "help",      'h', AR_CMD_HELP,         0 },
12543     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
12544     { "file",      'f', AR_SWITCH_FILE,      1 },
12545     { "append",    'a', AR_SWITCH_APPEND,    1 },
12546     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
12547     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
12548   };
12549   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
12550   struct ArSwitch *pEnd = &aSwitch[nSwitch];
12551
12552   if( nArg<=1 ){
12553     return arUsage(stderr);
12554   }else{
12555     char *z = azArg[1];
12556     if( z[0]!='-' ){
12557       /* Traditional style [tar] invocation */
12558       int i;
12559       int iArg = 2;
12560       for(i=0; z[i]; i++){
12561         const char *zArg = 0;
12562         struct ArSwitch *pOpt;
12563         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12564           if( z[i]==pOpt->cShort ) break;
12565         }
12566         if( pOpt==pEnd ){
12567           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12568         }
12569         if( pOpt->bArg ){
12570           if( iArg>=nArg ){
12571             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12572           }
12573           zArg = azArg[iArg++];
12574         }
12575         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12576       }
12577       pAr->nArg = nArg-iArg;
12578       if( pAr->nArg>0 ){
12579         pAr->azArg = &azArg[iArg];
12580       }
12581     }else{
12582       /* Non-traditional invocation */
12583       int iArg;
12584       for(iArg=1; iArg<nArg; iArg++){
12585         int n;
12586         z = azArg[iArg];
12587         if( z[0]!='-' ){
12588           /* All remaining command line words are command arguments. */
12589           pAr->azArg = &azArg[iArg];
12590           pAr->nArg = nArg-iArg;
12591           break;
12592         }
12593         n = strlen30(z);
12594
12595         if( z[1]!='-' ){
12596           int i;
12597           /* One or more short options */
12598           for(i=1; i<n; i++){
12599             const char *zArg = 0;
12600             struct ArSwitch *pOpt;
12601             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12602               if( z[i]==pOpt->cShort ) break;
12603             }
12604             if( pOpt==pEnd ){
12605               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12606             }
12607             if( pOpt->bArg ){
12608               if( i<(n-1) ){
12609                 zArg = &z[i+1];
12610                 i = n;
12611               }else{
12612                 if( iArg>=(nArg-1) ){
12613                   return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12614                 }
12615                 zArg = azArg[++iArg];
12616               }
12617             }
12618             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12619           }
12620         }else if( z[2]=='\0' ){
12621           /* A -- option, indicating that all remaining command line words
12622           ** are command arguments.  */
12623           pAr->azArg = &azArg[iArg+1];
12624           pAr->nArg = nArg-iArg-1;
12625           break;
12626         }else{
12627           /* A long option */
12628           const char *zArg = 0;             /* Argument for option, if any */
12629           struct ArSwitch *pMatch = 0;      /* Matching option */
12630           struct ArSwitch *pOpt;            /* Iterator */
12631           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12632             const char *zLong = pOpt->zLong;
12633             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
12634               if( pMatch ){
12635                 return arErrorMsg(pAr, "ambiguous option: %s",z);
12636               }else{
12637                 pMatch = pOpt;
12638               }
12639             }
12640           }
12641
12642           if( pMatch==0 ){
12643             return arErrorMsg(pAr, "unrecognized option: %s", z);
12644           }
12645           if( pMatch->bArg ){
12646             if( iArg>=(nArg-1) ){
12647               return arErrorMsg(pAr, "option requires an argument: %s", z);
12648             }
12649             zArg = azArg[++iArg];
12650           }
12651           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
12652         }
12653       }
12654     }
12655   }
12656
12657   return SQLITE_OK;
12658 }
12659
12660 /*
12661 ** This function assumes that all arguments within the ArCommand.azArg[]
12662 ** array refer to archive members, as for the --extract or --list commands. 
12663 ** It checks that each of them are present. If any specified file is not
12664 ** present in the archive, an error is printed to stderr and an error
12665 ** code returned. Otherwise, if all specified arguments are present in
12666 ** the archive, SQLITE_OK is returned.
12667 **
12668 ** This function strips any trailing '/' characters from each argument.
12669 ** This is consistent with the way the [tar] command seems to work on
12670 ** Linux.
12671 */
12672 static int arCheckEntries(ArCommand *pAr){
12673   int rc = SQLITE_OK;
12674   if( pAr->nArg ){
12675     int i, j;
12676     sqlite3_stmt *pTest = 0;
12677
12678     shellPreparePrintf(pAr->db, &rc, &pTest,
12679         "SELECT name FROM %s WHERE name=$name", 
12680         pAr->zSrcTable
12681     );
12682     j = sqlite3_bind_parameter_index(pTest, "$name");
12683     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12684       char *z = pAr->azArg[i];
12685       int n = strlen30(z);
12686       int bOk = 0;
12687       while( n>0 && z[n-1]=='/' ) n--;
12688       z[n] = '\0';
12689       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
12690       if( SQLITE_ROW==sqlite3_step(pTest) ){
12691         bOk = 1;
12692       }
12693       shellReset(&rc, pTest);
12694       if( rc==SQLITE_OK && bOk==0 ){
12695         utf8_printf(stderr, "not found in archive: %s\n", z);
12696         rc = SQLITE_ERROR;
12697       }
12698     }
12699     shellFinalize(&rc, pTest);
12700   }
12701   return rc;
12702 }
12703
12704 /*
12705 ** Format a WHERE clause that can be used against the "sqlar" table to
12706 ** identify all archive members that match the command arguments held
12707 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
12708 ** The caller is responsible for eventually calling sqlite3_free() on
12709 ** any non-NULL (*pzWhere) value.
12710 */
12711 static void arWhereClause(
12712   int *pRc, 
12713   ArCommand *pAr, 
12714   char **pzWhere                  /* OUT: New WHERE clause */
12715 ){
12716   char *zWhere = 0;
12717   if( *pRc==SQLITE_OK ){
12718     if( pAr->nArg==0 ){
12719       zWhere = sqlite3_mprintf("1");
12720     }else{
12721       int i;
12722       const char *zSep = "";
12723       for(i=0; i<pAr->nArg; i++){
12724         const char *z = pAr->azArg[i];
12725         zWhere = sqlite3_mprintf(
12726           "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", 
12727           zWhere, zSep, z, strlen30(z)+1, z
12728         );
12729         if( zWhere==0 ){
12730           *pRc = SQLITE_NOMEM;
12731           break;
12732         }
12733         zSep = " OR ";
12734       }
12735     }
12736   }
12737   *pzWhere = zWhere;
12738 }
12739
12740 /*
12741 ** Implementation of .ar "lisT" command. 
12742 */
12743 static int arListCommand(ArCommand *pAr){
12744   const char *zSql = "SELECT %s FROM %s WHERE %s"; 
12745   const char *azCols[] = {
12746     "name",
12747     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
12748   };
12749
12750   char *zWhere = 0;
12751   sqlite3_stmt *pSql = 0;
12752   int rc;
12753
12754   rc = arCheckEntries(pAr);
12755   arWhereClause(&rc, pAr, &zWhere);
12756
12757   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
12758                      pAr->zSrcTable, zWhere);
12759   if( pAr->bDryRun ){
12760     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12761   }else{
12762     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12763       if( pAr->bVerbose ){
12764         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
12765             sqlite3_column_text(pSql, 0),
12766             sqlite3_column_int(pSql, 1), 
12767             sqlite3_column_text(pSql, 2),
12768             sqlite3_column_text(pSql, 3)
12769         );
12770       }else{
12771         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12772       }
12773     }
12774   }
12775   shellFinalize(&rc, pSql);
12776   sqlite3_free(zWhere);
12777   return rc;
12778 }
12779
12780
12781 /*
12782 ** Implementation of .ar "eXtract" command. 
12783 */
12784 static int arExtractCommand(ArCommand *pAr){
12785   const char *zSql1 = 
12786     "SELECT "
12787     " ($dir || name),"
12788     " writefile(($dir || name), %s, mode, mtime) "
12789     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
12790
12791   const char *azExtraArg[] = { 
12792     "sqlar_uncompress(data, sz)",
12793     "data"
12794   };
12795
12796   sqlite3_stmt *pSql = 0;
12797   int rc = SQLITE_OK;
12798   char *zDir = 0;
12799   char *zWhere = 0;
12800   int i, j;
12801
12802   /* If arguments are specified, check that they actually exist within
12803   ** the archive before proceeding. And formulate a WHERE clause to
12804   ** match them.  */
12805   rc = arCheckEntries(pAr);
12806   arWhereClause(&rc, pAr, &zWhere);
12807
12808   if( rc==SQLITE_OK ){
12809     if( pAr->zDir ){
12810       zDir = sqlite3_mprintf("%s/", pAr->zDir);
12811     }else{
12812       zDir = sqlite3_mprintf("");
12813     }
12814     if( zDir==0 ) rc = SQLITE_NOMEM;
12815   }
12816
12817   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 
12818       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
12819   );
12820
12821   if( rc==SQLITE_OK ){
12822     j = sqlite3_bind_parameter_index(pSql, "$dir");
12823     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
12824
12825     /* Run the SELECT statement twice. The first time, writefile() is called
12826     ** for all archive members that should be extracted. The second time,
12827     ** only for the directories. This is because the timestamps for
12828     ** extracted directories must be reset after they are populated (as
12829     ** populating them changes the timestamp).  */
12830     for(i=0; i<2; i++){
12831       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
12832       sqlite3_bind_int(pSql, j, i);
12833       if( pAr->bDryRun ){
12834         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12835       }else{
12836         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12837           if( i==0 && pAr->bVerbose ){
12838             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12839           }
12840         }
12841       }
12842       shellReset(&rc, pSql);
12843     }
12844     shellFinalize(&rc, pSql);
12845   }
12846
12847   sqlite3_free(zDir);
12848   sqlite3_free(zWhere);
12849   return rc;
12850 }
12851
12852 /*
12853 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
12854 */
12855 static int arExecSql(ArCommand *pAr, const char *zSql){
12856   int rc;
12857   if( pAr->bDryRun ){
12858     utf8_printf(pAr->p->out, "%s\n", zSql);
12859     rc = SQLITE_OK;
12860   }else{
12861     char *zErr = 0;
12862     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
12863     if( zErr ){
12864       utf8_printf(stdout, "ERROR: %s\n", zErr);
12865       sqlite3_free(zErr);
12866     }
12867   }
12868   return rc;
12869 }
12870
12871
12872 /*
12873 ** Implementation of .ar "create" and "update" commands.
12874 **
12875 ** Create the "sqlar" table in the database if it does not already exist.
12876 ** Then add each file in the azFile[] array to the archive. Directories
12877 ** are added recursively. If argument bVerbose is non-zero, a message is
12878 ** printed on stdout for each file archived.
12879 **
12880 ** The create command is the same as update, except that it drops
12881 ** any existing "sqlar" table before beginning.
12882 */
12883 static int arCreateOrUpdateCommand(
12884   ArCommand *pAr,                 /* Command arguments and options */
12885   int bUpdate                     /* true for a --create.  false for --update */
12886 ){
12887   const char *zCreate = 
12888       "CREATE TABLE IF NOT EXISTS sqlar(\n"
12889       "  name TEXT PRIMARY KEY,  -- name of the file\n"
12890       "  mode INT,               -- access permissions\n"
12891       "  mtime INT,              -- last modification time\n"
12892       "  sz INT,                 -- original file size\n"
12893       "  data BLOB               -- compressed content\n"
12894       ")";
12895   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
12896   const char *zInsertFmt[2] = {
12897      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
12898      "  SELECT\n"
12899      "    %s,\n"
12900      "    mode,\n"
12901      "    mtime,\n"
12902      "    CASE substr(lsmode(mode),1,1)\n"
12903      "      WHEN '-' THEN length(data)\n"
12904      "      WHEN 'd' THEN 0\n"
12905      "      ELSE -1 END,\n"
12906      "    sqlar_compress(data)\n"
12907      "  FROM fsdir(%Q,%Q)\n"
12908      "  WHERE lsmode(mode) NOT LIKE '?%%';",
12909      "REPLACE INTO %s(name,mode,mtime,data)\n"
12910      "  SELECT\n"
12911      "    %s,\n"
12912      "    mode,\n"
12913      "    mtime,\n"
12914      "    data\n"
12915      "  FROM fsdir(%Q,%Q)\n"
12916      "  WHERE lsmode(mode) NOT LIKE '?%%';"
12917   };
12918   int i;                          /* For iterating through azFile[] */
12919   int rc;                         /* Return code */
12920   const char *zTab = 0;           /* SQL table into which to insert */
12921   char *zSql;
12922   char zTemp[50];
12923
12924   arExecSql(pAr, "PRAGMA page_size=512");
12925   rc = arExecSql(pAr, "SAVEPOINT ar;");
12926   if( rc!=SQLITE_OK ) return rc;
12927   zTemp[0] = 0; 
12928   if( pAr->bZip ){
12929     /* Initialize the zipfile virtual table, if necessary */
12930     if( pAr->zFile ){
12931       sqlite3_uint64 r;
12932       sqlite3_randomness(sizeof(r),&r);
12933       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
12934       zTab = zTemp;
12935       zSql = sqlite3_mprintf(
12936          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
12937          zTab, pAr->zFile
12938       );
12939       rc = arExecSql(pAr, zSql);
12940       sqlite3_free(zSql);
12941     }else{
12942       zTab = "zip";
12943     }
12944   }else{
12945     /* Initialize the table for an SQLAR */
12946     zTab = "sqlar";
12947     if( bUpdate==0 ){
12948       rc = arExecSql(pAr, zDrop);
12949       if( rc!=SQLITE_OK ) goto end_ar_transaction;
12950     }
12951     rc = arExecSql(pAr, zCreate);
12952   }
12953   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12954     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
12955         pAr->bVerbose ? "shell_putsnl(name)" : "name",
12956         pAr->azArg[i], pAr->zDir);
12957     rc = arExecSql(pAr, zSql2);
12958     sqlite3_free(zSql2);
12959   }
12960 end_ar_transaction:
12961   if( rc!=SQLITE_OK ){
12962     arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12963   }else{
12964     rc = arExecSql(pAr, "RELEASE ar;");
12965     if( pAr->bZip && pAr->zFile ){
12966       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
12967       arExecSql(pAr, zSql);
12968       sqlite3_free(zSql);
12969     }
12970   }
12971   return rc;
12972 }
12973
12974 /*
12975 ** Implementation of ".ar" dot command.
12976 */
12977 static int arDotCommand(
12978   ShellState *pState,             /* Current shell tool state */
12979   int fromCmdLine,                /* True if -A command-line option, not .ar cmd */
12980   char **azArg,                   /* Array of arguments passed to dot command */
12981   int nArg                        /* Number of entries in azArg[] */
12982 ){
12983   ArCommand cmd;
12984   int rc;
12985   memset(&cmd, 0, sizeof(cmd));
12986   cmd.fromCmdLine = fromCmdLine;
12987   rc = arParseCommand(azArg, nArg, &cmd);
12988   if( rc==SQLITE_OK ){
12989     int eDbType = SHELL_OPEN_UNSPEC;
12990     cmd.p = pState;
12991     cmd.db = pState->db;
12992     if( cmd.zFile ){
12993       eDbType = deduceDatabaseType(cmd.zFile, 1);
12994     }else{
12995       eDbType = pState->openMode;
12996     }
12997     if( eDbType==SHELL_OPEN_ZIPFILE ){
12998       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
12999         if( cmd.zFile==0 ){
13000           cmd.zSrcTable = sqlite3_mprintf("zip");
13001         }else{
13002           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
13003         }
13004       }
13005       cmd.bZip = 1;
13006     }else if( cmd.zFile ){
13007       int flags;
13008       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13009       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
13010         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13011       }else{
13012         flags = SQLITE_OPEN_READONLY;
13013       }
13014       cmd.db = 0;
13015       if( cmd.bDryRun ){
13016         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
13017              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
13018       }
13019       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 
13020              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
13021       if( rc!=SQLITE_OK ){
13022         utf8_printf(stderr, "cannot open file: %s (%s)\n", 
13023             cmd.zFile, sqlite3_errmsg(cmd.db)
13024         );
13025         goto end_ar_command;
13026       }
13027       sqlite3_fileio_init(cmd.db, 0, 0);
13028       sqlite3_sqlar_init(cmd.db, 0, 0);
13029       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13030                               shellPutsFunc, 0, 0);
13031
13032     }
13033     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13034       if( cmd.eCmd!=AR_CMD_CREATE
13035        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13036       ){
13037         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13038         rc = SQLITE_ERROR;
13039         goto end_ar_command;
13040       }
13041       cmd.zSrcTable = sqlite3_mprintf("sqlar");
13042     }
13043
13044     switch( cmd.eCmd ){
13045       case AR_CMD_CREATE:
13046         rc = arCreateOrUpdateCommand(&cmd, 0);
13047         break;
13048
13049       case AR_CMD_EXTRACT:
13050         rc = arExtractCommand(&cmd);
13051         break;
13052
13053       case AR_CMD_LIST:
13054         rc = arListCommand(&cmd);
13055         break;
13056
13057       case AR_CMD_HELP:
13058         arUsage(pState->out);
13059         break;
13060
13061       default:
13062         assert( cmd.eCmd==AR_CMD_UPDATE );
13063         rc = arCreateOrUpdateCommand(&cmd, 1);
13064         break;
13065     }
13066   }
13067 end_ar_command:
13068   if( cmd.db!=pState->db ){
13069     close_db(cmd.db);
13070   }
13071   sqlite3_free(cmd.zSrcTable);
13072
13073   return rc;
13074 }
13075 /* End of the ".archive" or ".ar" command logic
13076 **********************************************************************************/
13077 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
13078
13079
13080 /*
13081 ** If an input line begins with "." then invoke this routine to
13082 ** process that line.
13083 **
13084 ** Return 1 on error, 2 to exit, and 0 otherwise.
13085 */
13086 static int do_meta_command(char *zLine, ShellState *p){
13087   int h = 1;
13088   int nArg = 0;
13089   int n, c;
13090   int rc = 0;
13091   char *azArg[50];
13092
13093 #ifndef SQLITE_OMIT_VIRTUALTABLE
13094   if( p->expert.pExpert ){
13095     expertFinish(p, 1, 0);
13096   }
13097 #endif
13098
13099   /* Parse the input line into tokens.
13100   */
13101   while( zLine[h] && nArg<ArraySize(azArg) ){
13102     while( IsSpace(zLine[h]) ){ h++; }
13103     if( zLine[h]==0 ) break;
13104     if( zLine[h]=='\'' || zLine[h]=='"' ){
13105       int delim = zLine[h++];
13106       azArg[nArg++] = &zLine[h];
13107       while( zLine[h] && zLine[h]!=delim ){
13108         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
13109         h++;
13110       }
13111       if( zLine[h]==delim ){
13112         zLine[h++] = 0;
13113       }
13114       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
13115     }else{
13116       azArg[nArg++] = &zLine[h];
13117       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
13118       if( zLine[h] ) zLine[h++] = 0;
13119       resolve_backslashes(azArg[nArg-1]);
13120     }
13121   }
13122
13123   /* Process the input line.
13124   */
13125   if( nArg==0 ) return 0; /* no tokens, no error */
13126   n = strlen30(azArg[0]);
13127   c = azArg[0][0];
13128   clearTempFile(p);
13129
13130 #ifndef SQLITE_OMIT_AUTHORIZATION
13131   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
13132     if( nArg!=2 ){
13133       raw_printf(stderr, "Usage: .auth ON|OFF\n");
13134       rc = 1;
13135       goto meta_command_exit;
13136     }
13137     open_db(p, 0);
13138     if( booleanValue(azArg[1]) ){
13139       sqlite3_set_authorizer(p->db, shellAuth, p);
13140     }else{
13141       sqlite3_set_authorizer(p->db, 0, 0);
13142     }
13143   }else
13144 #endif
13145
13146 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13147   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13148     open_db(p, 0);
13149     rc = arDotCommand(p, 0, azArg, nArg);
13150   }else
13151 #endif
13152
13153   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13154    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
13155   ){
13156     const char *zDestFile = 0;
13157     const char *zDb = 0;
13158     sqlite3 *pDest;
13159     sqlite3_backup *pBackup;
13160     int j;
13161     const char *zVfs = 0;
13162     for(j=1; j<nArg; j++){
13163       const char *z = azArg[j];
13164       if( z[0]=='-' ){
13165         if( z[1]=='-' ) z++;
13166         if( strcmp(z, "-append")==0 ){
13167           zVfs = "apndvfs";
13168         }else
13169         {
13170           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
13171           return 1;
13172         }
13173       }else if( zDestFile==0 ){
13174         zDestFile = azArg[j];
13175       }else if( zDb==0 ){
13176         zDb = zDestFile;
13177         zDestFile = azArg[j];
13178       }else{
13179         raw_printf(stderr, "Usage: .backup ?DB? ?--append? FILENAME\n");
13180         return 1;
13181       }
13182     }
13183     if( zDestFile==0 ){
13184       raw_printf(stderr, "missing FILENAME argument on .backup\n");
13185       return 1;
13186     }
13187     if( zDb==0 ) zDb = "main";
13188     rc = sqlite3_open_v2(zDestFile, &pDest, 
13189                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13190     if( rc!=SQLITE_OK ){
13191       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13192       close_db(pDest);
13193       return 1;
13194     }
13195     open_db(p, 0);
13196     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13197     if( pBackup==0 ){
13198       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13199       close_db(pDest);
13200       return 1;
13201     }
13202     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13203     sqlite3_backup_finish(pBackup);
13204     if( rc==SQLITE_DONE ){
13205       rc = 0;
13206     }else{
13207       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13208       rc = 1;
13209     }
13210     close_db(pDest);
13211   }else
13212
13213   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13214     if( nArg==2 ){
13215       bail_on_error = booleanValue(azArg[1]);
13216     }else{
13217       raw_printf(stderr, "Usage: .bail on|off\n");
13218       rc = 1;
13219     }
13220   }else
13221
13222   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
13223     if( nArg==2 ){
13224       if( booleanValue(azArg[1]) ){
13225         setBinaryMode(p->out, 1);
13226       }else{
13227         setTextMode(p->out, 1);
13228       }
13229     }else{
13230       raw_printf(stderr, "Usage: .binary on|off\n");
13231       rc = 1;
13232     }
13233   }else
13234
13235   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
13236     if( nArg==2 ){
13237 #if defined(_WIN32) || defined(WIN32)
13238       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
13239       rc = !SetCurrentDirectoryW(z);
13240       sqlite3_free(z);
13241 #else
13242       rc = chdir(azArg[1]);
13243 #endif
13244       if( rc ){
13245         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
13246         rc = 1;
13247       }
13248     }else{
13249       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
13250       rc = 1;
13251     }
13252   }else
13253
13254   /* The undocumented ".breakpoint" command causes a call to the no-op
13255   ** routine named test_breakpoint().
13256   */
13257   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
13258     test_breakpoint();
13259   }else
13260
13261   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
13262     if( nArg==2 ){
13263       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
13264     }else{
13265       raw_printf(stderr, "Usage: .changes on|off\n");
13266       rc = 1;
13267     }
13268   }else
13269
13270   /* Cancel output redirection, if it is currently set (by .testcase)
13271   ** Then read the content of the testcase-out.txt file and compare against
13272   ** azArg[1].  If there are differences, report an error and exit.
13273   */
13274   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
13275     char *zRes = 0;
13276     output_reset(p);
13277     if( nArg!=2 ){
13278       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
13279       rc = 2;
13280     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
13281       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
13282       rc = 2;
13283     }else if( testcase_glob(azArg[1],zRes)==0 ){
13284       utf8_printf(stderr,
13285                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
13286                  p->zTestcase, azArg[1], zRes);
13287       rc = 1;
13288     }else{
13289       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
13290       p->nCheck++;
13291     }
13292     sqlite3_free(zRes);
13293   }else
13294
13295   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
13296     if( nArg==2 ){
13297       tryToClone(p, azArg[1]);
13298     }else{
13299       raw_printf(stderr, "Usage: .clone FILENAME\n");
13300       rc = 1;
13301     }
13302   }else
13303
13304   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
13305     ShellState data;
13306     char *zErrMsg = 0;
13307     open_db(p, 0);
13308     memcpy(&data, p, sizeof(data));
13309     data.showHeader = 0;
13310     data.cMode = data.mode = MODE_List;
13311     sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
13312     data.cnt = 0;
13313     sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
13314                  callback, &data, &zErrMsg);
13315     if( zErrMsg ){
13316       utf8_printf(stderr,"Error: %s\n", zErrMsg);
13317       sqlite3_free(zErrMsg);
13318       rc = 1;
13319     }
13320   }else
13321
13322   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13323     static const struct DbConfigChoices {const char *zName; int op;} aDbConfig[] = {
13324         { "enable_fkey",      SQLITE_DBCONFIG_ENABLE_FKEY            },
13325         { "enable_trigger",   SQLITE_DBCONFIG_ENABLE_TRIGGER         },
13326         { "fts3_tokenizer",   SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER  },
13327         { "load_extension",   SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION  },
13328         { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE       },
13329         { "enable_qpsg",      SQLITE_DBCONFIG_ENABLE_QPSG            },
13330         { "trigger_eqp",      SQLITE_DBCONFIG_TRIGGER_EQP            },
13331         { "reset_database",   SQLITE_DBCONFIG_RESET_DATABASE         },
13332     };
13333     int ii, v;
13334     open_db(p, 0);
13335     for(ii=0; ii<ArraySize(aDbConfig); ii++){
13336       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13337       if( nArg>=3 ){
13338         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13339       }
13340       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13341       utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13342       if( nArg>1 ) break;
13343     }
13344     if( nArg>1 && ii==ArraySize(aDbConfig) ){
13345       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13346       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13347     }   
13348   }else
13349
13350   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13351     rc = shell_dbinfo_command(p, nArg, azArg);
13352   }else
13353
13354   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
13355     const char *zLike = 0;
13356     int i;
13357     int savedShowHeader = p->showHeader;
13358     ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
13359     for(i=1; i<nArg; i++){
13360       if( azArg[i][0]=='-' ){
13361         const char *z = azArg[i]+1;
13362         if( z[0]=='-' ) z++;
13363         if( strcmp(z,"preserve-rowids")==0 ){
13364 #ifdef SQLITE_OMIT_VIRTUALTABLE
13365           raw_printf(stderr, "The --preserve-rowids option is not compatible"
13366                              " with SQLITE_OMIT_VIRTUALTABLE\n");
13367           rc = 1;
13368           goto meta_command_exit;
13369 #else
13370           ShellSetFlag(p, SHFLG_PreserveRowid);
13371 #endif
13372         }else
13373         if( strcmp(z,"newlines")==0 ){
13374           ShellSetFlag(p, SHFLG_Newlines);
13375         }else
13376         {
13377           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
13378           rc = 1;
13379           goto meta_command_exit;
13380         }
13381       }else if( zLike ){
13382         raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
13383                            "?--newlines? ?LIKE-PATTERN?\n");
13384         rc = 1;
13385         goto meta_command_exit;
13386       }else{
13387         zLike = azArg[i];
13388       }
13389     }
13390     open_db(p, 0);
13391     /* When playing back a "dump", the content might appear in an order
13392     ** which causes immediate foreign key constraints to be violated.
13393     ** So disable foreign-key constraint enforcement to prevent problems. */
13394     raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
13395     raw_printf(p->out, "BEGIN TRANSACTION;\n");
13396     p->writableSchema = 0;
13397     p->showHeader = 0;
13398     /* Set writable_schema=ON since doing so forces SQLite to initialize
13399     ** as much of the schema as it can even if the sqlite_master table is
13400     ** corrupt. */
13401     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
13402     p->nErr = 0;
13403     if( zLike==0 ){
13404       run_schema_dump_query(p,
13405         "SELECT name, type, sql FROM sqlite_master "
13406         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
13407       );
13408       run_schema_dump_query(p,
13409         "SELECT name, type, sql FROM sqlite_master "
13410         "WHERE name=='sqlite_sequence'"
13411       );
13412       run_table_dump_query(p,
13413         "SELECT sql FROM sqlite_master "
13414         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
13415       );
13416     }else{
13417       char *zSql;
13418       zSql = sqlite3_mprintf(
13419         "SELECT name, type, sql FROM sqlite_master "
13420         "WHERE tbl_name LIKE %Q AND type=='table'"
13421         "  AND sql NOT NULL", zLike);
13422       run_schema_dump_query(p,zSql);
13423       sqlite3_free(zSql);
13424       zSql = sqlite3_mprintf(
13425         "SELECT sql FROM sqlite_master "
13426         "WHERE sql NOT NULL"
13427         "  AND type IN ('index','trigger','view')"
13428         "  AND tbl_name LIKE %Q", zLike);
13429       run_table_dump_query(p, zSql, 0);
13430       sqlite3_free(zSql);
13431     }
13432     if( p->writableSchema ){
13433       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
13434       p->writableSchema = 0;
13435     }
13436     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13437     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
13438     raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
13439     p->showHeader = savedShowHeader;
13440   }else
13441
13442   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
13443     if( nArg==2 ){
13444       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
13445     }else{
13446       raw_printf(stderr, "Usage: .echo on|off\n");
13447       rc = 1;
13448     }
13449   }else
13450
13451   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
13452     if( nArg==2 ){
13453       p->autoEQPtest = 0;
13454       if( strcmp(azArg[1],"full")==0 ){
13455         p->autoEQP = AUTOEQP_full;
13456       }else if( strcmp(azArg[1],"trigger")==0 ){
13457         p->autoEQP = AUTOEQP_trigger;
13458       }else if( strcmp(azArg[1],"test")==0 ){
13459         p->autoEQP = AUTOEQP_on;
13460         p->autoEQPtest = 1;
13461       }else{
13462         p->autoEQP = (u8)booleanValue(azArg[1]);
13463       }
13464     }else{
13465       raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
13466       rc = 1;
13467     }
13468   }else
13469
13470   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
13471     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
13472     rc = 2;
13473   }else
13474
13475   /* The ".explain" command is automatic now.  It is largely pointless.  It
13476   ** retained purely for backwards compatibility */
13477   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
13478     int val = 1;
13479     if( nArg>=2 ){
13480       if( strcmp(azArg[1],"auto")==0 ){
13481         val = 99;
13482       }else{
13483         val =  booleanValue(azArg[1]);
13484       }
13485     }
13486     if( val==1 && p->mode!=MODE_Explain ){
13487       p->normalMode = p->mode;
13488       p->mode = MODE_Explain;
13489       p->autoExplain = 0;
13490     }else if( val==0 ){
13491       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
13492       p->autoExplain = 0;
13493     }else if( val==99 ){
13494       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
13495       p->autoExplain = 1;
13496     }
13497   }else
13498
13499 #ifndef SQLITE_OMIT_VIRTUALTABLE
13500   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
13501     open_db(p, 0);
13502     expertDotCommand(p, azArg, nArg);
13503   }else
13504 #endif
13505
13506   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
13507     ShellState data;
13508     char *zErrMsg = 0;
13509     int doStats = 0;
13510     memcpy(&data, p, sizeof(data));
13511     data.showHeader = 0;
13512     data.cMode = data.mode = MODE_Semi;
13513     if( nArg==2 && optionMatch(azArg[1], "indent") ){
13514       data.cMode = data.mode = MODE_Pretty;
13515       nArg = 1;
13516     }
13517     if( nArg!=1 ){
13518       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
13519       rc = 1;
13520       goto meta_command_exit;
13521     }
13522     open_db(p, 0);
13523     rc = sqlite3_exec(p->db,
13524        "SELECT sql FROM"
13525        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
13526        "     FROM sqlite_master UNION ALL"
13527        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
13528        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
13529        "ORDER BY rowid",
13530        callback, &data, &zErrMsg
13531     );
13532     if( rc==SQLITE_OK ){
13533       sqlite3_stmt *pStmt;
13534       rc = sqlite3_prepare_v2(p->db,
13535                "SELECT rowid FROM sqlite_master"
13536                " WHERE name GLOB 'sqlite_stat[134]'",
13537                -1, &pStmt, 0);
13538       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
13539       sqlite3_finalize(pStmt);
13540     }
13541     if( doStats==0 ){
13542       raw_printf(p->out, "/* No STAT tables available */\n");
13543     }else{
13544       raw_printf(p->out, "ANALYZE sqlite_master;\n");
13545       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
13546                    callback, &data, &zErrMsg);
13547       data.cMode = data.mode = MODE_Insert;
13548       data.zDestTable = "sqlite_stat1";
13549       shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
13550       data.zDestTable = "sqlite_stat3";
13551       shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
13552       data.zDestTable = "sqlite_stat4";
13553       shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
13554       raw_printf(p->out, "ANALYZE sqlite_master;\n");
13555     }
13556   }else
13557
13558   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
13559     if( nArg==2 ){
13560       p->showHeader = booleanValue(azArg[1]);
13561     }else{
13562       raw_printf(stderr, "Usage: .headers on|off\n");
13563       rc = 1;
13564     }
13565   }else
13566
13567   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
13568     utf8_printf(p->out, "%s", zHelp);
13569   }else
13570
13571   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
13572     char *zTable;               /* Insert data into this table */
13573     char *zFile;                /* Name of file to extra content from */
13574     sqlite3_stmt *pStmt = NULL; /* A statement */
13575     int nCol;                   /* Number of columns in the table */
13576     int nByte;                  /* Number of bytes in an SQL string */
13577     int i, j;                   /* Loop counters */
13578     int needCommit;             /* True to COMMIT or ROLLBACK at end */
13579     int nSep;                   /* Number of bytes in p->colSeparator[] */
13580     char *zSql;                 /* An SQL statement */
13581     ImportCtx sCtx;             /* Reader context */
13582     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
13583     int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
13584
13585     if( nArg!=3 ){
13586       raw_printf(stderr, "Usage: .import FILE TABLE\n");
13587       goto meta_command_exit;
13588     }
13589     zFile = azArg[1];
13590     zTable = azArg[2];
13591     seenInterrupt = 0;
13592     memset(&sCtx, 0, sizeof(sCtx));
13593     open_db(p, 0);
13594     nSep = strlen30(p->colSeparator);
13595     if( nSep==0 ){
13596       raw_printf(stderr,
13597                  "Error: non-null column separator required for import\n");
13598       return 1;
13599     }
13600     if( nSep>1 ){
13601       raw_printf(stderr, "Error: multi-character column separators not allowed"
13602                       " for import\n");
13603       return 1;
13604     }
13605     nSep = strlen30(p->rowSeparator);
13606     if( nSep==0 ){
13607       raw_printf(stderr, "Error: non-null row separator required for import\n");
13608       return 1;
13609     }
13610     if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
13611       /* When importing CSV (only), if the row separator is set to the
13612       ** default output row separator, change it to the default input
13613       ** row separator.  This avoids having to maintain different input
13614       ** and output row separators. */
13615       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13616       nSep = strlen30(p->rowSeparator);
13617     }
13618     if( nSep>1 ){
13619       raw_printf(stderr, "Error: multi-character row separators not allowed"
13620                       " for import\n");
13621       return 1;
13622     }
13623     sCtx.zFile = zFile;
13624     sCtx.nLine = 1;
13625     if( sCtx.zFile[0]=='|' ){
13626 #ifdef SQLITE_OMIT_POPEN
13627       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13628       return 1;
13629 #else
13630       sCtx.in = popen(sCtx.zFile+1, "r");
13631       sCtx.zFile = "<pipe>";
13632       xCloser = pclose;
13633 #endif
13634     }else{
13635       sCtx.in = fopen(sCtx.zFile, "rb");
13636       xCloser = fclose;
13637     }
13638     if( p->mode==MODE_Ascii ){
13639       xRead = ascii_read_one_field;
13640     }else{
13641       xRead = csv_read_one_field;
13642     }
13643     if( sCtx.in==0 ){
13644       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
13645       return 1;
13646     }
13647     sCtx.cColSep = p->colSeparator[0];
13648     sCtx.cRowSep = p->rowSeparator[0];
13649     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
13650     if( zSql==0 ){
13651       xCloser(sCtx.in);
13652       shell_out_of_memory();
13653     }
13654     nByte = strlen30(zSql);
13655     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13656     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
13657     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
13658       char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
13659       char cSep = '(';
13660       while( xRead(&sCtx) ){
13661         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
13662         cSep = ',';
13663         if( sCtx.cTerm!=sCtx.cColSep ) break;
13664       }
13665       if( cSep=='(' ){
13666         sqlite3_free(zCreate);
13667         sqlite3_free(sCtx.z);
13668         xCloser(sCtx.in);
13669         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
13670         return 1;
13671       }
13672       zCreate = sqlite3_mprintf("%z\n)", zCreate);
13673       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
13674       sqlite3_free(zCreate);
13675       if( rc ){
13676         utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
13677                 sqlite3_errmsg(p->db));
13678         sqlite3_free(sCtx.z);
13679         xCloser(sCtx.in);
13680         return 1;
13681       }
13682       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13683     }
13684     sqlite3_free(zSql);
13685     if( rc ){
13686       if (pStmt) sqlite3_finalize(pStmt);
13687       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
13688       xCloser(sCtx.in);
13689       return 1;
13690     }
13691     nCol = sqlite3_column_count(pStmt);
13692     sqlite3_finalize(pStmt);
13693     pStmt = 0;
13694     if( nCol==0 ) return 0; /* no columns, no error */
13695     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
13696     if( zSql==0 ){
13697       xCloser(sCtx.in);
13698       shell_out_of_memory();
13699     }
13700     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
13701     j = strlen30(zSql);
13702     for(i=1; i<nCol; i++){
13703       zSql[j++] = ',';
13704       zSql[j++] = '?';
13705     }
13706     zSql[j++] = ')';
13707     zSql[j] = 0;
13708     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13709     sqlite3_free(zSql);
13710     if( rc ){
13711       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13712       if (pStmt) sqlite3_finalize(pStmt);
13713       xCloser(sCtx.in);
13714       return 1;
13715     }
13716     needCommit = sqlite3_get_autocommit(p->db);
13717     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
13718     do{
13719       int startLine = sCtx.nLine;
13720       for(i=0; i<nCol; i++){
13721         char *z = xRead(&sCtx);
13722         /*
13723         ** Did we reach end-of-file before finding any columns?
13724         ** If so, stop instead of NULL filling the remaining columns.
13725         */
13726         if( z==0 && i==0 ) break;
13727         /*
13728         ** Did we reach end-of-file OR end-of-line before finding any
13729         ** columns in ASCII mode?  If so, stop instead of NULL filling
13730         ** the remaining columns.
13731         */
13732         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
13733         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
13734         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
13735           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
13736                           "filling the rest with NULL\n",
13737                           sCtx.zFile, startLine, nCol, i+1);
13738           i += 2;
13739           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
13740         }
13741       }
13742       if( sCtx.cTerm==sCtx.cColSep ){
13743         do{
13744           xRead(&sCtx);
13745           i++;
13746         }while( sCtx.cTerm==sCtx.cColSep );
13747         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
13748                         "extras ignored\n",
13749                         sCtx.zFile, startLine, nCol, i);
13750       }
13751       if( i>=nCol ){
13752         sqlite3_step(pStmt);
13753         rc = sqlite3_reset(pStmt);
13754         if( rc!=SQLITE_OK ){
13755           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
13756                       startLine, sqlite3_errmsg(p->db));
13757         }
13758       }
13759     }while( sCtx.cTerm!=EOF );
13760
13761     xCloser(sCtx.in);
13762     sqlite3_free(sCtx.z);
13763     sqlite3_finalize(pStmt);
13764     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
13765   }else
13766
13767 #ifndef SQLITE_UNTESTABLE
13768   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
13769     char *zSql;
13770     char *zCollist = 0;
13771     sqlite3_stmt *pStmt;
13772     int tnum = 0;
13773     int i;
13774     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
13775       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
13776                           "       .imposter off\n");
13777       rc = 1;
13778       goto meta_command_exit;
13779     }
13780     open_db(p, 0);
13781     if( nArg==2 ){
13782       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
13783       goto meta_command_exit;
13784     }
13785     zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
13786                            " WHERE name='%q' AND type='index'", azArg[1]);
13787     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13788     sqlite3_free(zSql);
13789     if( sqlite3_step(pStmt)==SQLITE_ROW ){
13790       tnum = sqlite3_column_int(pStmt, 0);
13791     }
13792     sqlite3_finalize(pStmt);
13793     if( tnum==0 ){
13794       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
13795       rc = 1;
13796       goto meta_command_exit;
13797     }
13798     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
13799     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13800     sqlite3_free(zSql);
13801     i = 0;
13802     while( sqlite3_step(pStmt)==SQLITE_ROW ){
13803       char zLabel[20];
13804       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
13805       i++;
13806       if( zCol==0 ){
13807         if( sqlite3_column_int(pStmt,1)==-1 ){
13808           zCol = "_ROWID_";
13809         }else{
13810           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
13811           zCol = zLabel;
13812         }
13813       }
13814       if( zCollist==0 ){
13815         zCollist = sqlite3_mprintf("\"%w\"", zCol);
13816       }else{
13817         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
13818       }
13819     }
13820     sqlite3_finalize(pStmt);
13821     zSql = sqlite3_mprintf(
13822           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
13823           azArg[2], zCollist, zCollist);
13824     sqlite3_free(zCollist);
13825     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
13826     if( rc==SQLITE_OK ){
13827       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
13828       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
13829       if( rc ){
13830         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
13831       }else{
13832         utf8_printf(stdout, "%s;\n", zSql);
13833         raw_printf(stdout,
13834            "WARNING: writing to an imposter table will corrupt the index!\n"
13835         );
13836       }
13837     }else{
13838       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
13839       rc = 1;
13840     }
13841     sqlite3_free(zSql);
13842   }else
13843 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
13844
13845 #ifdef SQLITE_ENABLE_IOTRACE
13846   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
13847     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
13848     if( iotrace && iotrace!=stdout ) fclose(iotrace);
13849     iotrace = 0;
13850     if( nArg<2 ){
13851       sqlite3IoTrace = 0;
13852     }else if( strcmp(azArg[1], "-")==0 ){
13853       sqlite3IoTrace = iotracePrintf;
13854       iotrace = stdout;
13855     }else{
13856       iotrace = fopen(azArg[1], "w");
13857       if( iotrace==0 ){
13858         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
13859         sqlite3IoTrace = 0;
13860         rc = 1;
13861       }else{
13862         sqlite3IoTrace = iotracePrintf;
13863       }
13864     }
13865   }else
13866 #endif
13867
13868   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
13869     static const struct {
13870        const char *zLimitName;   /* Name of a limit */
13871        int limitCode;            /* Integer code for that limit */
13872     } aLimit[] = {
13873       { "length",                SQLITE_LIMIT_LENGTH                    },
13874       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
13875       { "column",                SQLITE_LIMIT_COLUMN                    },
13876       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
13877       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
13878       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
13879       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
13880       { "attached",              SQLITE_LIMIT_ATTACHED                  },
13881       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
13882       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
13883       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
13884       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
13885     };
13886     int i, n2;
13887     open_db(p, 0);
13888     if( nArg==1 ){
13889       for(i=0; i<ArraySize(aLimit); i++){
13890         printf("%20s %d\n", aLimit[i].zLimitName,
13891                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
13892       }
13893     }else if( nArg>3 ){
13894       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
13895       rc = 1;
13896       goto meta_command_exit;
13897     }else{
13898       int iLimit = -1;
13899       n2 = strlen30(azArg[1]);
13900       for(i=0; i<ArraySize(aLimit); i++){
13901         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
13902           if( iLimit<0 ){
13903             iLimit = i;
13904           }else{
13905             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
13906             rc = 1;
13907             goto meta_command_exit;
13908           }
13909         }
13910       }
13911       if( iLimit<0 ){
13912         utf8_printf(stderr, "unknown limit: \"%s\"\n"
13913                         "enter \".limits\" with no arguments for a list.\n",
13914                          azArg[1]);
13915         rc = 1;
13916         goto meta_command_exit;
13917       }
13918       if( nArg==3 ){
13919         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
13920                       (int)integerValue(azArg[2]));
13921       }
13922       printf("%20s %d\n", aLimit[iLimit].zLimitName,
13923              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
13924     }
13925   }else
13926
13927   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
13928     open_db(p, 0);
13929     lintDotCommand(p, azArg, nArg);
13930   }else
13931
13932 #ifndef SQLITE_OMIT_LOAD_EXTENSION
13933   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
13934     const char *zFile, *zProc;
13935     char *zErrMsg = 0;
13936     if( nArg<2 ){
13937       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
13938       rc = 1;
13939       goto meta_command_exit;
13940     }
13941     zFile = azArg[1];
13942     zProc = nArg>=3 ? azArg[2] : 0;
13943     open_db(p, 0);
13944     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
13945     if( rc!=SQLITE_OK ){
13946       utf8_printf(stderr, "Error: %s\n", zErrMsg);
13947       sqlite3_free(zErrMsg);
13948       rc = 1;
13949     }
13950   }else
13951 #endif
13952
13953   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
13954     if( nArg!=2 ){
13955       raw_printf(stderr, "Usage: .log FILENAME\n");
13956       rc = 1;
13957     }else{
13958       const char *zFile = azArg[1];
13959       output_file_close(p->pLog);
13960       p->pLog = output_file_open(zFile, 0);
13961     }
13962   }else
13963
13964   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
13965     const char *zMode = nArg>=2 ? azArg[1] : "";
13966     int n2 = strlen30(zMode);
13967     int c2 = zMode[0];
13968     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
13969       p->mode = MODE_Line;
13970       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13971     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
13972       p->mode = MODE_Column;
13973       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13974     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
13975       p->mode = MODE_List;
13976       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
13977       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13978     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
13979       p->mode = MODE_Html;
13980     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
13981       p->mode = MODE_Tcl;
13982       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
13983       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13984     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
13985       p->mode = MODE_Csv;
13986       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13987       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13988     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
13989       p->mode = MODE_List;
13990       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
13991     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
13992       p->mode = MODE_Insert;
13993       set_table_name(p, nArg>=3 ? azArg[2] : "table");
13994     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
13995       p->mode = MODE_Quote;
13996     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
13997       p->mode = MODE_Ascii;
13998       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
13999       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
14000     }else if( nArg==1 ){
14001       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
14002     }else{
14003       raw_printf(stderr, "Error: mode should be one of: "
14004          "ascii column csv html insert line list quote tabs tcl\n");
14005       rc = 1;
14006     }
14007     p->cMode = p->mode;
14008   }else
14009
14010   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
14011     if( nArg==2 ){
14012       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
14013                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
14014     }else{
14015       raw_printf(stderr, "Usage: .nullvalue STRING\n");
14016       rc = 1;
14017     }
14018   }else
14019
14020   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
14021     char *zNewFilename;  /* Name of the database file to open */
14022     int iName = 1;       /* Index in azArg[] of the filename */
14023     int newFlag = 0;     /* True to delete file before opening */
14024     /* Close the existing database */
14025     session_close_all(p);
14026     close_db(p->db);
14027     p->db = 0;
14028     p->zDbFilename = 0;
14029     sqlite3_free(p->zFreeOnClose);
14030     p->zFreeOnClose = 0;
14031     p->openMode = SHELL_OPEN_UNSPEC;
14032     /* Check for command-line arguments */
14033     for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
14034       const char *z = azArg[iName];
14035       if( optionMatch(z,"new") ){
14036         newFlag = 1;
14037 #ifdef SQLITE_HAVE_ZLIB
14038       }else if( optionMatch(z, "zip") ){
14039         p->openMode = SHELL_OPEN_ZIPFILE;
14040 #endif
14041       }else if( optionMatch(z, "append") ){
14042         p->openMode = SHELL_OPEN_APPENDVFS;
14043       }else if( optionMatch(z, "readonly") ){
14044         p->openMode = SHELL_OPEN_READONLY;
14045       }else if( z[0]=='-' ){
14046         utf8_printf(stderr, "unknown option: %s\n", z);
14047         rc = 1;
14048         goto meta_command_exit;
14049       }
14050     }
14051     /* If a filename is specified, try to open it first */
14052     zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14053     if( zNewFilename ){
14054       if( newFlag ) shellDeleteFile(zNewFilename);
14055       p->zDbFilename = zNewFilename;
14056       open_db(p, OPEN_DB_KEEPALIVE);
14057       if( p->db==0 ){
14058         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14059         sqlite3_free(zNewFilename);
14060       }else{
14061         p->zFreeOnClose = zNewFilename;
14062       }
14063     }
14064     if( p->db==0 ){
14065       /* As a fall-back open a TEMP database */
14066       p->zDbFilename = 0;
14067       open_db(p, 0);
14068     }
14069   }else
14070
14071   if( (c=='o'
14072         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
14073    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
14074   ){
14075     const char *zFile = nArg>=2 ? azArg[1] : "stdout";
14076     int bTxtMode = 0;
14077     if( azArg[0][0]=='e' ){
14078       /* Transform the ".excel" command into ".once -x" */
14079       nArg = 2;
14080       azArg[0] = "once";
14081       zFile = azArg[1] = "-x";
14082       n = 4;
14083     }
14084     if( nArg>2 ){
14085       utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
14086       rc = 1;
14087       goto meta_command_exit;
14088     }
14089     if( n>1 && strncmp(azArg[0], "once", n)==0 ){
14090       if( nArg<2 ){
14091         raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
14092         rc = 1;
14093         goto meta_command_exit;
14094       }
14095       p->outCount = 2;
14096     }else{
14097       p->outCount = 0;
14098     }
14099     output_reset(p);
14100     if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
14101 #ifndef SQLITE_NOHAVE_SYSTEM
14102     if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
14103       p->doXdgOpen = 1;
14104       outputModePush(p);
14105       if( zFile[1]=='x' ){
14106         newTempFile(p, "csv");
14107         p->mode = MODE_Csv;
14108         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14109         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14110       }else{
14111         newTempFile(p, "txt");
14112         bTxtMode = 1;
14113       }
14114       zFile = p->zTempFile;
14115     }
14116 #endif /* SQLITE_NOHAVE_SYSTEM */
14117     if( zFile[0]=='|' ){
14118 #ifdef SQLITE_OMIT_POPEN
14119       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14120       rc = 1;
14121       p->out = stdout;
14122 #else
14123       p->out = popen(zFile + 1, "w");
14124       if( p->out==0 ){
14125         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
14126         p->out = stdout;
14127         rc = 1;
14128       }else{
14129         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14130       }
14131 #endif
14132     }else{
14133       p->out = output_file_open(zFile, bTxtMode);
14134       if( p->out==0 ){
14135         if( strcmp(zFile,"off")!=0 ){
14136           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
14137         }
14138         p->out = stdout;
14139         rc = 1;
14140       } else {
14141         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14142       }
14143     }
14144   }else
14145
14146   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14147     int i;
14148     for(i=1; i<nArg; i++){
14149       if( i>1 ) raw_printf(p->out, " ");
14150       utf8_printf(p->out, "%s", azArg[i]);
14151     }
14152     raw_printf(p->out, "\n");
14153   }else
14154
14155   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
14156     if( nArg >= 2) {
14157       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
14158     }
14159     if( nArg >= 3) {
14160       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
14161     }
14162   }else
14163
14164   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
14165     rc = 2;
14166   }else
14167
14168   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
14169     FILE *alt;
14170     if( nArg!=2 ){
14171       raw_printf(stderr, "Usage: .read FILE\n");
14172       rc = 1;
14173       goto meta_command_exit;
14174     }
14175     alt = fopen(azArg[1], "rb");
14176     if( alt==0 ){
14177       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
14178       rc = 1;
14179     }else{
14180       rc = process_input(p, alt);
14181       fclose(alt);
14182     }
14183   }else
14184
14185   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
14186     const char *zSrcFile;
14187     const char *zDb;
14188     sqlite3 *pSrc;
14189     sqlite3_backup *pBackup;
14190     int nTimeout = 0;
14191
14192     if( nArg==2 ){
14193       zSrcFile = azArg[1];
14194       zDb = "main";
14195     }else if( nArg==3 ){
14196       zSrcFile = azArg[2];
14197       zDb = azArg[1];
14198     }else{
14199       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
14200       rc = 1;
14201       goto meta_command_exit;
14202     }
14203     rc = sqlite3_open(zSrcFile, &pSrc);
14204     if( rc!=SQLITE_OK ){
14205       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14206       close_db(pSrc);
14207       return 1;
14208     }
14209     open_db(p, 0);
14210     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
14211     if( pBackup==0 ){
14212       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14213       close_db(pSrc);
14214       return 1;
14215     }
14216     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
14217           || rc==SQLITE_BUSY  ){
14218       if( rc==SQLITE_BUSY ){
14219         if( nTimeout++ >= 3 ) break;
14220         sqlite3_sleep(100);
14221       }
14222     }
14223     sqlite3_backup_finish(pBackup);
14224     if( rc==SQLITE_DONE ){
14225       rc = 0;
14226     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
14227       raw_printf(stderr, "Error: source database is busy\n");
14228       rc = 1;
14229     }else{
14230       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14231       rc = 1;
14232     }
14233     close_db(pSrc);
14234   }else
14235
14236   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
14237     if( nArg==2 ){
14238       p->scanstatsOn = (u8)booleanValue(azArg[1]);
14239 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
14240       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
14241 #endif
14242     }else{
14243       raw_printf(stderr, "Usage: .scanstats on|off\n");
14244       rc = 1;
14245     }
14246   }else
14247
14248   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
14249     ShellText sSelect;
14250     ShellState data;
14251     char *zErrMsg = 0;
14252     const char *zDiv = "(";
14253     const char *zName = 0;
14254     int iSchema = 0;
14255     int bDebug = 0;
14256     int ii;
14257
14258     open_db(p, 0);
14259     memcpy(&data, p, sizeof(data));
14260     data.showHeader = 0;
14261     data.cMode = data.mode = MODE_Semi;
14262     initText(&sSelect);
14263     for(ii=1; ii<nArg; ii++){
14264       if( optionMatch(azArg[ii],"indent") ){
14265         data.cMode = data.mode = MODE_Pretty;
14266       }else if( optionMatch(azArg[ii],"debug") ){
14267         bDebug = 1;
14268       }else if( zName==0 ){
14269         zName = azArg[ii];
14270       }else{
14271         raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
14272         rc = 1;
14273         goto meta_command_exit;
14274       }
14275     }
14276     if( zName!=0 ){
14277       int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
14278       if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
14279         char *new_argv[2], *new_colv[2];
14280         new_argv[0] = sqlite3_mprintf(
14281                       "CREATE TABLE %s (\n"
14282                       "  type text,\n"
14283                       "  name text,\n"
14284                       "  tbl_name text,\n"
14285                       "  rootpage integer,\n"
14286                       "  sql text\n"
14287                       ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
14288         new_argv[1] = 0;
14289         new_colv[0] = "sql";
14290         new_colv[1] = 0;
14291         callback(&data, 1, new_argv, new_colv);
14292         sqlite3_free(new_argv[0]);
14293       }
14294     }
14295     if( zDiv ){
14296       sqlite3_stmt *pStmt = 0;
14297       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
14298                               -1, &pStmt, 0);
14299       if( rc ){
14300         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14301         sqlite3_finalize(pStmt);
14302         rc = 1;
14303         goto meta_command_exit;
14304       }
14305       appendText(&sSelect, "SELECT sql FROM", 0);
14306       iSchema = 0;
14307       while( sqlite3_step(pStmt)==SQLITE_ROW ){
14308         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
14309         char zScNum[30];
14310         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
14311         appendText(&sSelect, zDiv, 0);
14312         zDiv = " UNION ALL ";
14313         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
14314         if( sqlite3_stricmp(zDb, "main")!=0 ){
14315           appendText(&sSelect, zDb, '"');
14316         }else{
14317           appendText(&sSelect, "NULL", 0);
14318         }
14319         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
14320         appendText(&sSelect, zScNum, 0);
14321         appendText(&sSelect, " AS snum, ", 0);
14322         appendText(&sSelect, zDb, '\'');
14323         appendText(&sSelect, " AS sname FROM ", 0);
14324         appendText(&sSelect, zDb, '"');
14325         appendText(&sSelect, ".sqlite_master", 0);
14326       }
14327       sqlite3_finalize(pStmt);
14328 #ifdef SQLITE_INTROSPECTION_PRAGMAS
14329       if( zName ){
14330         appendText(&sSelect,
14331            " UNION ALL SELECT shell_module_schema(name),"
14332            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
14333       }
14334 #endif
14335       appendText(&sSelect, ") WHERE ", 0);
14336       if( zName ){
14337         char *zQarg = sqlite3_mprintf("%Q", zName);
14338         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
14339                     strchr(zName, '[') != 0;
14340         if( strchr(zName, '.') ){
14341           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
14342         }else{
14343           appendText(&sSelect, "lower(tbl_name)", 0);
14344         }
14345         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
14346         appendText(&sSelect, zQarg, 0);
14347         if( !bGlob ){
14348           appendText(&sSelect, " ESCAPE '\\' ", 0);
14349         }
14350         appendText(&sSelect, " AND ", 0);
14351         sqlite3_free(zQarg);
14352       }
14353       appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
14354                            " ORDER BY snum, rowid", 0);
14355       if( bDebug ){
14356         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
14357       }else{
14358         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
14359       }
14360       freeText(&sSelect);
14361     }
14362     if( zErrMsg ){
14363       utf8_printf(stderr,"Error: %s\n", zErrMsg);
14364       sqlite3_free(zErrMsg);
14365       rc = 1;
14366     }else if( rc != SQLITE_OK ){
14367       raw_printf(stderr,"Error: querying schema information\n");
14368       rc = 1;
14369     }else{
14370       rc = 0;
14371     }
14372   }else
14373
14374 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
14375   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
14376     sqlite3SelectTrace = (int)integerValue(azArg[1]);
14377   }else
14378 #endif
14379
14380 #if defined(SQLITE_ENABLE_SESSION)
14381   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
14382     OpenSession *pSession = &p->aSession[0];
14383     char **azCmd = &azArg[1];
14384     int iSes = 0;
14385     int nCmd = nArg - 1;
14386     int i;
14387     if( nArg<=1 ) goto session_syntax_error;
14388     open_db(p, 0);
14389     if( nArg>=3 ){
14390       for(iSes=0; iSes<p->nSession; iSes++){
14391         if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
14392       }
14393       if( iSes<p->nSession ){
14394         pSession = &p->aSession[iSes];
14395         azCmd++;
14396         nCmd--;
14397       }else{
14398         pSession = &p->aSession[0];
14399         iSes = 0;
14400       }
14401     }
14402
14403     /* .session attach TABLE
14404     ** Invoke the sqlite3session_attach() interface to attach a particular
14405     ** table so that it is never filtered.
14406     */
14407     if( strcmp(azCmd[0],"attach")==0 ){
14408       if( nCmd!=2 ) goto session_syntax_error;
14409       if( pSession->p==0 ){
14410         session_not_open:
14411         raw_printf(stderr, "ERROR: No sessions are open\n");
14412       }else{
14413         rc = sqlite3session_attach(pSession->p, azCmd[1]);
14414         if( rc ){
14415           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
14416           rc = 0;
14417         }
14418       }
14419     }else
14420
14421     /* .session changeset FILE
14422     ** .session patchset FILE
14423     ** Write a changeset or patchset into a file.  The file is overwritten.
14424     */
14425     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
14426       FILE *out = 0;
14427       if( nCmd!=2 ) goto session_syntax_error;
14428       if( pSession->p==0 ) goto session_not_open;
14429       out = fopen(azCmd[1], "wb");
14430       if( out==0 ){
14431         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
14432       }else{
14433         int szChng;
14434         void *pChng;
14435         if( azCmd[0][0]=='c' ){
14436           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
14437         }else{
14438           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
14439         }
14440         if( rc ){
14441           printf("Error: error code %d\n", rc);
14442           rc = 0;
14443         }
14444         if( pChng
14445           && fwrite(pChng, szChng, 1, out)!=1 ){
14446           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
14447                   szChng);
14448         }
14449         sqlite3_free(pChng);
14450         fclose(out);
14451       }
14452     }else
14453
14454     /* .session close
14455     ** Close the identified session
14456     */
14457     if( strcmp(azCmd[0], "close")==0 ){
14458       if( nCmd!=1 ) goto session_syntax_error;
14459       if( p->nSession ){
14460         session_close(pSession);
14461         p->aSession[iSes] = p->aSession[--p->nSession];
14462       }
14463     }else
14464
14465     /* .session enable ?BOOLEAN?
14466     ** Query or set the enable flag
14467     */
14468     if( strcmp(azCmd[0], "enable")==0 ){
14469       int ii;
14470       if( nCmd>2 ) goto session_syntax_error;
14471       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
14472       if( p->nSession ){
14473         ii = sqlite3session_enable(pSession->p, ii);
14474         utf8_printf(p->out, "session %s enable flag = %d\n",
14475                     pSession->zName, ii);
14476       }
14477     }else
14478
14479     /* .session filter GLOB ....
14480     ** Set a list of GLOB patterns of table names to be excluded.
14481     */
14482     if( strcmp(azCmd[0], "filter")==0 ){
14483       int ii, nByte;
14484       if( nCmd<2 ) goto session_syntax_error;
14485       if( p->nSession ){
14486         for(ii=0; ii<pSession->nFilter; ii++){
14487           sqlite3_free(pSession->azFilter[ii]);
14488         }
14489         sqlite3_free(pSession->azFilter);
14490         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
14491         pSession->azFilter = sqlite3_malloc( nByte );
14492         if( pSession->azFilter==0 ){
14493           raw_printf(stderr, "Error: out or memory\n");
14494           exit(1);
14495         }
14496         for(ii=1; ii<nCmd; ii++){
14497           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
14498         }
14499         pSession->nFilter = ii-1;
14500       }
14501     }else
14502
14503     /* .session indirect ?BOOLEAN?
14504     ** Query or set the indirect flag
14505     */
14506     if( strcmp(azCmd[0], "indirect")==0 ){
14507       int ii;
14508       if( nCmd>2 ) goto session_syntax_error;
14509       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
14510       if( p->nSession ){
14511         ii = sqlite3session_indirect(pSession->p, ii);
14512         utf8_printf(p->out, "session %s indirect flag = %d\n",
14513                     pSession->zName, ii);
14514       }
14515     }else
14516
14517     /* .session isempty
14518     ** Determine if the session is empty
14519     */
14520     if( strcmp(azCmd[0], "isempty")==0 ){
14521       int ii;
14522       if( nCmd!=1 ) goto session_syntax_error;
14523       if( p->nSession ){
14524         ii = sqlite3session_isempty(pSession->p);
14525         utf8_printf(p->out, "session %s isempty flag = %d\n",
14526                     pSession->zName, ii);
14527       }
14528     }else
14529
14530     /* .session list
14531     ** List all currently open sessions
14532     */
14533     if( strcmp(azCmd[0],"list")==0 ){
14534       for(i=0; i<p->nSession; i++){
14535         utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
14536       }
14537     }else
14538
14539     /* .session open DB NAME
14540     ** Open a new session called NAME on the attached database DB.
14541     ** DB is normally "main".
14542     */
14543     if( strcmp(azCmd[0],"open")==0 ){
14544       char *zName;
14545       if( nCmd!=3 ) goto session_syntax_error;
14546       zName = azCmd[2];
14547       if( zName[0]==0 ) goto session_syntax_error;
14548       for(i=0; i<p->nSession; i++){
14549         if( strcmp(p->aSession[i].zName,zName)==0 ){
14550           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
14551           goto meta_command_exit;
14552         }
14553       }
14554       if( p->nSession>=ArraySize(p->aSession) ){
14555         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
14556         goto meta_command_exit;
14557       }
14558       pSession = &p->aSession[p->nSession];
14559       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
14560       if( rc ){
14561         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
14562         rc = 0;
14563         goto meta_command_exit;
14564       }
14565       pSession->nFilter = 0;
14566       sqlite3session_table_filter(pSession->p, session_filter, pSession);
14567       p->nSession++;
14568       pSession->zName = sqlite3_mprintf("%s", zName);
14569     }else
14570     /* If no command name matches, show a syntax error */
14571     session_syntax_error:
14572     session_help(p);
14573   }else
14574 #endif
14575
14576 #ifdef SQLITE_DEBUG
14577   /* Undocumented commands for internal testing.  Subject to change
14578   ** without notice. */
14579   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
14580     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
14581       int i, v;
14582       for(i=1; i<nArg; i++){
14583         v = booleanValue(azArg[i]);
14584         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
14585       }
14586     }
14587     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
14588       int i; sqlite3_int64 v;
14589       for(i=1; i<nArg; i++){
14590         char zBuf[200];
14591         v = integerValue(azArg[i]);
14592         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
14593         utf8_printf(p->out, "%s", zBuf);
14594       }
14595     }
14596   }else
14597 #endif
14598
14599   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
14600     int bIsInit = 0;         /* True to initialize the SELFTEST table */
14601     int bVerbose = 0;        /* Verbose output */
14602     int bSelftestExists;     /* True if SELFTEST already exists */
14603     int i, k;                /* Loop counters */
14604     int nTest = 0;           /* Number of tests runs */
14605     int nErr = 0;            /* Number of errors seen */
14606     ShellText str;           /* Answer for a query */
14607     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
14608
14609     open_db(p,0);
14610     for(i=1; i<nArg; i++){
14611       const char *z = azArg[i];
14612       if( z[0]=='-' && z[1]=='-' ) z++;
14613       if( strcmp(z,"-init")==0 ){
14614         bIsInit = 1;
14615       }else
14616       if( strcmp(z,"-v")==0 ){
14617         bVerbose++;
14618       }else
14619       {
14620         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
14621                     azArg[i], azArg[0]);
14622         raw_printf(stderr, "Should be one of: --init -v\n");
14623         rc = 1;
14624         goto meta_command_exit;
14625       }
14626     }
14627     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
14628            != SQLITE_OK ){
14629       bSelftestExists = 0;
14630     }else{
14631       bSelftestExists = 1;
14632     }
14633     if( bIsInit ){
14634       createSelftestTable(p);
14635       bSelftestExists = 1;
14636     }
14637     initText(&str);
14638     appendText(&str, "x", 0);
14639     for(k=bSelftestExists; k>=0; k--){
14640       if( k==1 ){
14641         rc = sqlite3_prepare_v2(p->db,
14642             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
14643             -1, &pStmt, 0);
14644       }else{
14645         rc = sqlite3_prepare_v2(p->db,
14646           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
14647           "      (1,'run','PRAGMA integrity_check','ok')",
14648           -1, &pStmt, 0);
14649       }
14650       if( rc ){
14651         raw_printf(stderr, "Error querying the selftest table\n");
14652         rc = 1;
14653         sqlite3_finalize(pStmt);
14654         goto meta_command_exit;
14655       }
14656       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
14657         int tno = sqlite3_column_int(pStmt, 0);
14658         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
14659         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
14660         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
14661
14662         k = 0;
14663         if( bVerbose>0 ){
14664           char *zQuote = sqlite3_mprintf("%q", zSql);
14665           printf("%d: %s %s\n", tno, zOp, zSql);
14666           sqlite3_free(zQuote);
14667         }
14668         if( strcmp(zOp,"memo")==0 ){
14669           utf8_printf(p->out, "%s\n", zSql);
14670         }else
14671         if( strcmp(zOp,"run")==0 ){
14672           char *zErrMsg = 0;
14673           str.n = 0;
14674           str.z[0] = 0;
14675           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
14676           nTest++;
14677           if( bVerbose ){
14678             utf8_printf(p->out, "Result: %s\n", str.z);
14679           }
14680           if( rc || zErrMsg ){
14681             nErr++;
14682             rc = 1;
14683             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
14684             sqlite3_free(zErrMsg);
14685           }else if( strcmp(zAns,str.z)!=0 ){
14686             nErr++;
14687             rc = 1;
14688             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
14689             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
14690           }
14691         }else
14692         {
14693           utf8_printf(stderr,
14694             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
14695           rc = 1;
14696           break;
14697         }
14698       } /* End loop over rows of content from SELFTEST */
14699       sqlite3_finalize(pStmt);
14700     } /* End loop over k */
14701     freeText(&str);
14702     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
14703   }else
14704
14705   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
14706     if( nArg<2 || nArg>3 ){
14707       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
14708       rc = 1;
14709     }
14710     if( nArg>=2 ){
14711       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
14712                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
14713     }
14714     if( nArg>=3 ){
14715       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
14716                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
14717     }
14718   }else
14719
14720   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
14721     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
14722     int i;                   /* Loop counter */
14723     int bSchema = 0;         /* Also hash the schema */
14724     int bSeparate = 0;       /* Hash each table separately */
14725     int iSize = 224;         /* Hash algorithm to use */
14726     int bDebug = 0;          /* Only show the query that would have run */
14727     sqlite3_stmt *pStmt;     /* For querying tables names */
14728     char *zSql;              /* SQL to be run */
14729     char *zSep;              /* Separator */
14730     ShellText sSql;          /* Complete SQL for the query to run the hash */
14731     ShellText sQuery;        /* Set of queries used to read all content */
14732     open_db(p, 0);
14733     for(i=1; i<nArg; i++){
14734       const char *z = azArg[i];
14735       if( z[0]=='-' ){
14736         z++;
14737         if( z[0]=='-' ) z++;
14738         if( strcmp(z,"schema")==0 ){
14739           bSchema = 1;
14740         }else
14741         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
14742          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
14743         ){
14744           iSize = atoi(&z[5]);
14745         }else
14746         if( strcmp(z,"debug")==0 ){
14747           bDebug = 1;
14748         }else
14749         {
14750           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
14751                       azArg[i], azArg[0]);
14752           raw_printf(stderr, "Should be one of: --schema"
14753                              " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
14754           rc = 1;
14755           goto meta_command_exit;
14756         }
14757       }else if( zLike ){
14758         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
14759         rc = 1;
14760         goto meta_command_exit;
14761       }else{
14762         zLike = z;
14763         bSeparate = 1;
14764         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
14765       }
14766     }
14767     if( bSchema ){
14768       zSql = "SELECT lower(name) FROM sqlite_master"
14769              " WHERE type='table' AND coalesce(rootpage,0)>1"
14770              " UNION ALL SELECT 'sqlite_master'"
14771              " ORDER BY 1 collate nocase";
14772     }else{
14773       zSql = "SELECT lower(name) FROM sqlite_master"
14774              " WHERE type='table' AND coalesce(rootpage,0)>1"
14775              " AND name NOT LIKE 'sqlite_%'"
14776              " ORDER BY 1 collate nocase";
14777     }
14778     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14779     initText(&sQuery);
14780     initText(&sSql);
14781     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
14782     zSep = "VALUES(";
14783     while( SQLITE_ROW==sqlite3_step(pStmt) ){
14784       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
14785       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
14786       if( strncmp(zTab, "sqlite_",7)!=0 ){
14787         appendText(&sQuery,"SELECT * FROM ", 0);
14788         appendText(&sQuery,zTab,'"');
14789         appendText(&sQuery," NOT INDEXED;", 0);
14790       }else if( strcmp(zTab, "sqlite_master")==0 ){
14791         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
14792                            " ORDER BY name;", 0);
14793       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
14794         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
14795                            " ORDER BY name;", 0);
14796       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
14797         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
14798                            " ORDER BY tbl,idx;", 0);
14799       }else if( strcmp(zTab, "sqlite_stat3")==0
14800              || strcmp(zTab, "sqlite_stat4")==0 ){
14801         appendText(&sQuery, "SELECT * FROM ", 0);
14802         appendText(&sQuery, zTab, 0);
14803         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
14804       }
14805       appendText(&sSql, zSep, 0);
14806       appendText(&sSql, sQuery.z, '\'');
14807       sQuery.n = 0;
14808       appendText(&sSql, ",", 0);
14809       appendText(&sSql, zTab, '\'');
14810       zSep = "),(";
14811     }
14812     sqlite3_finalize(pStmt);
14813     if( bSeparate ){
14814       zSql = sqlite3_mprintf(
14815           "%s))"
14816           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
14817           "   FROM [sha3sum$query]",
14818           sSql.z, iSize);
14819     }else{
14820       zSql = sqlite3_mprintf(
14821           "%s))"
14822           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
14823           "   FROM [sha3sum$query]",
14824           sSql.z, iSize);
14825     }
14826     freeText(&sQuery);
14827     freeText(&sSql);
14828     if( bDebug ){
14829       utf8_printf(p->out, "%s\n", zSql);
14830     }else{
14831       shell_exec(p, zSql, 0);
14832     }
14833     sqlite3_free(zSql);
14834   }else
14835
14836 #ifndef SQLITE_NOHAVE_SYSTEM
14837   if( c=='s'
14838    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
14839   ){
14840     char *zCmd;
14841     int i, x;
14842     if( nArg<2 ){
14843       raw_printf(stderr, "Usage: .system COMMAND\n");
14844       rc = 1;
14845       goto meta_command_exit;
14846     }
14847     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
14848     for(i=2; i<nArg; i++){
14849       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
14850                              zCmd, azArg[i]);
14851     }
14852     x = system(zCmd);
14853     sqlite3_free(zCmd);
14854     if( x ) raw_printf(stderr, "System command returns %d\n", x);
14855   }else
14856 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
14857
14858   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
14859     static const char *azBool[] = { "off", "on", "trigger", "full"};
14860     int i;
14861     if( nArg!=1 ){
14862       raw_printf(stderr, "Usage: .show\n");
14863       rc = 1;
14864       goto meta_command_exit;
14865     }
14866     utf8_printf(p->out, "%12.12s: %s\n","echo",
14867                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
14868     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
14869     utf8_printf(p->out, "%12.12s: %s\n","explain",
14870          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
14871     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
14872     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
14873     utf8_printf(p->out, "%12.12s: ", "nullvalue");
14874       output_c_string(p->out, p->nullValue);
14875       raw_printf(p->out, "\n");
14876     utf8_printf(p->out,"%12.12s: %s\n","output",
14877             strlen30(p->outfile) ? p->outfile : "stdout");
14878     utf8_printf(p->out,"%12.12s: ", "colseparator");
14879       output_c_string(p->out, p->colSeparator);
14880       raw_printf(p->out, "\n");
14881     utf8_printf(p->out,"%12.12s: ", "rowseparator");
14882       output_c_string(p->out, p->rowSeparator);
14883       raw_printf(p->out, "\n");
14884     utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
14885     utf8_printf(p->out, "%12.12s: ", "width");
14886     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
14887       raw_printf(p->out, "%d ", p->colWidth[i]);
14888     }
14889     raw_printf(p->out, "\n");
14890     utf8_printf(p->out, "%12.12s: %s\n", "filename",
14891                 p->zDbFilename ? p->zDbFilename : "");
14892   }else
14893
14894   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
14895     if( nArg==2 ){
14896       p->statsOn = (u8)booleanValue(azArg[1]);
14897     }else if( nArg==1 ){
14898       display_stats(p->db, p, 0);
14899     }else{
14900       raw_printf(stderr, "Usage: .stats ?on|off?\n");
14901       rc = 1;
14902     }
14903   }else
14904
14905   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
14906    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
14907                  || strncmp(azArg[0], "indexes", n)==0) )
14908   ){
14909     sqlite3_stmt *pStmt;
14910     char **azResult;
14911     int nRow, nAlloc;
14912     int ii;
14913     ShellText s;
14914     initText(&s);
14915     open_db(p, 0);
14916     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
14917     if( rc ){
14918       sqlite3_finalize(pStmt);
14919       return shellDatabaseError(p->db);
14920     }
14921
14922     if( nArg>2 && c=='i' ){
14923       /* It is an historical accident that the .indexes command shows an error
14924       ** when called with the wrong number of arguments whereas the .tables
14925       ** command does not. */
14926       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
14927       rc = 1;
14928       sqlite3_finalize(pStmt);
14929       goto meta_command_exit;
14930     }
14931     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
14932       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
14933       if( zDbName==0 ) continue;
14934       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
14935       if( sqlite3_stricmp(zDbName, "main")==0 ){
14936         appendText(&s, "SELECT name FROM ", 0);
14937       }else{
14938         appendText(&s, "SELECT ", 0);
14939         appendText(&s, zDbName, '\'');
14940         appendText(&s, "||'.'||name FROM ", 0);
14941       }
14942       appendText(&s, zDbName, '"');
14943       appendText(&s, ".sqlite_master ", 0);
14944       if( c=='t' ){
14945         appendText(&s," WHERE type IN ('table','view')"
14946                       "   AND name NOT LIKE 'sqlite_%'"
14947                       "   AND name LIKE ?1", 0);
14948       }else{
14949         appendText(&s," WHERE type='index'"
14950                       "   AND tbl_name LIKE ?1", 0);
14951       }
14952     }
14953     rc = sqlite3_finalize(pStmt);
14954     appendText(&s, " ORDER BY 1", 0);
14955     rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
14956     freeText(&s);
14957     if( rc ) return shellDatabaseError(p->db);
14958
14959     /* Run the SQL statement prepared by the above block. Store the results
14960     ** as an array of nul-terminated strings in azResult[].  */
14961     nRow = nAlloc = 0;
14962     azResult = 0;
14963     if( nArg>1 ){
14964       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
14965     }else{
14966       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
14967     }
14968     while( sqlite3_step(pStmt)==SQLITE_ROW ){
14969       if( nRow>=nAlloc ){
14970         char **azNew;
14971         int n2 = nAlloc*2 + 10;
14972         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
14973         if( azNew==0 ) shell_out_of_memory();
14974         nAlloc = n2;
14975         azResult = azNew;
14976       }
14977       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
14978       if( 0==azResult[nRow] ) shell_out_of_memory();
14979       nRow++;
14980     }
14981     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
14982       rc = shellDatabaseError(p->db);
14983     }
14984
14985     /* Pretty-print the contents of array azResult[] to the output */
14986     if( rc==0 && nRow>0 ){
14987       int len, maxlen = 0;
14988       int i, j;
14989       int nPrintCol, nPrintRow;
14990       for(i=0; i<nRow; i++){
14991         len = strlen30(azResult[i]);
14992         if( len>maxlen ) maxlen = len;
14993       }
14994       nPrintCol = 80/(maxlen+2);
14995       if( nPrintCol<1 ) nPrintCol = 1;
14996       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
14997       for(i=0; i<nPrintRow; i++){
14998         for(j=i; j<nRow; j+=nPrintRow){
14999           char *zSp = j<nPrintRow ? "" : "  ";
15000           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
15001                       azResult[j] ? azResult[j]:"");
15002         }
15003         raw_printf(p->out, "\n");
15004       }
15005     }
15006
15007     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
15008     sqlite3_free(azResult);
15009   }else
15010
15011   /* Begin redirecting output to the file "testcase-out.txt" */
15012   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
15013     output_reset(p);
15014     p->out = output_file_open("testcase-out.txt", 0);
15015     if( p->out==0 ){
15016       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
15017     }
15018     if( nArg>=2 ){
15019       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
15020     }else{
15021       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
15022     }
15023   }else
15024
15025 #ifndef SQLITE_UNTESTABLE
15026   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
15027     static const struct {
15028        const char *zCtrlName;   /* Name of a test-control option */
15029        int ctrlCode;            /* Integer code for that option */
15030        const char *zUsage;      /* Usage notes */
15031     } aCtrl[] = {
15032       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
15033       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
15034     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
15035     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
15036       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
15037     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
15038       { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
15039       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
15040       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
15041       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
15042 #ifdef YYCOVERAGE
15043       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
15044 #endif
15045       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
15046       { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
15047       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
15048       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
15049       { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
15050     };
15051     int testctrl = -1;
15052     int iCtrl = -1;
15053     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
15054     int isOk = 0;
15055     int i, n2;
15056     const char *zCmd = 0;
15057
15058     open_db(p, 0);
15059     zCmd = nArg>=2 ? azArg[1] : "help";
15060
15061     /* The argument can optionally begin with "-" or "--" */
15062     if( zCmd[0]=='-' && zCmd[1] ){
15063       zCmd++;
15064       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15065     }
15066
15067     /* --help lists all test-controls */
15068     if( strcmp(zCmd,"help")==0 ){
15069       utf8_printf(p->out, "Available test-controls:\n");
15070       for(i=0; i<ArraySize(aCtrl); i++){
15071         utf8_printf(p->out, "  .testctrl %s %s\n",
15072                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15073       }
15074       rc = 1;
15075       goto meta_command_exit;
15076     }
15077
15078     /* convert testctrl text option to value. allow any unique prefix
15079     ** of the option name, or a numerical value. */
15080     n2 = strlen30(zCmd);
15081     for(i=0; i<ArraySize(aCtrl); i++){
15082       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15083         if( testctrl<0 ){
15084           testctrl = aCtrl[i].ctrlCode;
15085           iCtrl = i;
15086         }else{
15087           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
15088                               "Use \".testctrl --help\" for help\n", zCmd);
15089           rc = 1;
15090           goto meta_command_exit;
15091         }
15092       }
15093     }
15094     if( testctrl<0 ){
15095       utf8_printf(stderr,"Error: unknown test-control: %s\n"
15096                          "Use \".testctrl --help\" for help\n", zCmd);
15097     }else{
15098       switch(testctrl){
15099
15100         /* sqlite3_test_control(int, db, int) */
15101         case SQLITE_TESTCTRL_OPTIMIZATIONS:
15102         case SQLITE_TESTCTRL_RESERVE:
15103           if( nArg==3 ){
15104             int opt = (int)strtol(azArg[2], 0, 0);
15105             rc2 = sqlite3_test_control(testctrl, p->db, opt);
15106             isOk = 3;
15107           }
15108           break;
15109
15110         /* sqlite3_test_control(int) */
15111         case SQLITE_TESTCTRL_PRNG_SAVE:
15112         case SQLITE_TESTCTRL_PRNG_RESTORE:
15113         case SQLITE_TESTCTRL_PRNG_RESET:
15114         case SQLITE_TESTCTRL_BYTEORDER:
15115           if( nArg==2 ){
15116             rc2 = sqlite3_test_control(testctrl);
15117             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
15118           }
15119           break;
15120
15121         /* sqlite3_test_control(int, uint) */
15122         case SQLITE_TESTCTRL_PENDING_BYTE:
15123           if( nArg==3 ){
15124             unsigned int opt = (unsigned int)integerValue(azArg[2]);
15125             rc2 = sqlite3_test_control(testctrl, opt);
15126             isOk = 3;
15127           }
15128           break;
15129
15130         /* sqlite3_test_control(int, int) */
15131         case SQLITE_TESTCTRL_ASSERT:
15132         case SQLITE_TESTCTRL_ALWAYS:
15133           if( nArg==3 ){
15134             int opt = booleanValue(azArg[2]);
15135             rc2 = sqlite3_test_control(testctrl, opt);
15136             isOk = 1;
15137           }
15138           break;
15139
15140         /* sqlite3_test_control(int, int) */
15141         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
15142         case SQLITE_TESTCTRL_NEVER_CORRUPT:
15143           if( nArg==3 ){
15144             int opt = booleanValue(azArg[2]);
15145             rc2 = sqlite3_test_control(testctrl, opt);
15146             isOk = 3;
15147           }
15148           break;
15149
15150         case SQLITE_TESTCTRL_IMPOSTER:
15151           if( nArg==5 ){
15152             rc2 = sqlite3_test_control(testctrl, p->db,
15153                           azArg[2],
15154                           integerValue(azArg[3]),
15155                           integerValue(azArg[4]));
15156             isOk = 3;
15157           }
15158           break;
15159
15160 #ifdef YYCOVERAGE
15161         case SQLITE_TESTCTRL_PARSER_COVERAGE:
15162           if( nArg==2 ){
15163             sqlite3_test_control(testctrl, p->out);
15164             isOk = 3;
15165           }
15166 #endif
15167       }
15168     }
15169     if( isOk==0 && iCtrl>=0 ){
15170       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
15171       rc = 1;
15172     }else if( isOk==1 ){
15173       raw_printf(p->out, "%d\n", rc2);
15174     }else if( isOk==2 ){
15175       raw_printf(p->out, "0x%08x\n", rc2);
15176     }
15177   }else
15178 #endif /* !defined(SQLITE_UNTESTABLE) */
15179
15180   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
15181     open_db(p, 0);
15182     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
15183   }else
15184
15185   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
15186     if( nArg==2 ){
15187       enableTimer = booleanValue(azArg[1]);
15188       if( enableTimer && !HAS_TIMER ){
15189         raw_printf(stderr, "Error: timer not available on this system.\n");
15190         enableTimer = 0;
15191       }
15192     }else{
15193       raw_printf(stderr, "Usage: .timer on|off\n");
15194       rc = 1;
15195     }
15196   }else
15197
15198   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
15199     open_db(p, 0);
15200     if( nArg!=2 ){
15201       raw_printf(stderr, "Usage: .trace FILE|off\n");
15202       rc = 1;
15203       goto meta_command_exit;
15204     }
15205     output_file_close(p->traceOut);
15206     p->traceOut = output_file_open(azArg[1], 0);
15207 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
15208     if( p->traceOut==0 ){
15209       sqlite3_trace_v2(p->db, 0, 0, 0);
15210     }else{
15211       sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
15212     }
15213 #endif
15214   }else
15215
15216 #if SQLITE_USER_AUTHENTICATION
15217   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
15218     if( nArg<2 ){
15219       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
15220       rc = 1;
15221       goto meta_command_exit;
15222     }
15223     open_db(p, 0);
15224     if( strcmp(azArg[1],"login")==0 ){
15225       if( nArg!=4 ){
15226         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
15227         rc = 1;
15228         goto meta_command_exit;
15229       }
15230       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
15231       if( rc ){
15232         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
15233         rc = 1;
15234       }
15235     }else if( strcmp(azArg[1],"add")==0 ){
15236       if( nArg!=5 ){
15237         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
15238         rc = 1;
15239         goto meta_command_exit;
15240       }
15241       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15242                             booleanValue(azArg[4]));
15243       if( rc ){
15244         raw_printf(stderr, "User-Add failed: %d\n", rc);
15245         rc = 1;
15246       }
15247     }else if( strcmp(azArg[1],"edit")==0 ){
15248       if( nArg!=5 ){
15249         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
15250         rc = 1;
15251         goto meta_command_exit;
15252       }
15253       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15254                               booleanValue(azArg[4]));
15255       if( rc ){
15256         raw_printf(stderr, "User-Edit failed: %d\n", rc);
15257         rc = 1;
15258       }
15259     }else if( strcmp(azArg[1],"delete")==0 ){
15260       if( nArg!=3 ){
15261         raw_printf(stderr, "Usage: .user delete USER\n");
15262         rc = 1;
15263         goto meta_command_exit;
15264       }
15265       rc = sqlite3_user_delete(p->db, azArg[2]);
15266       if( rc ){
15267         raw_printf(stderr, "User-Delete failed: %d\n", rc);
15268         rc = 1;
15269       }
15270     }else{
15271       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
15272       rc = 1;
15273       goto meta_command_exit;
15274     }
15275   }else
15276 #endif /* SQLITE_USER_AUTHENTICATION */
15277
15278   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
15279     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
15280         sqlite3_libversion(), sqlite3_sourceid());
15281 #if SQLITE_HAVE_ZLIB
15282     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
15283 #endif
15284 #define CTIMEOPT_VAL_(opt) #opt
15285 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
15286 #if defined(__clang__) && defined(__clang_major__)
15287     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
15288                     CTIMEOPT_VAL(__clang_minor__) "."
15289                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
15290 #elif defined(_MSC_VER)
15291     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
15292 #elif defined(__GNUC__) && defined(__VERSION__)
15293     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
15294 #endif
15295   }else
15296
15297   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
15298     const char *zDbName = nArg==2 ? azArg[1] : "main";
15299     sqlite3_vfs *pVfs = 0;
15300     if( p->db ){
15301       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
15302       if( pVfs ){
15303         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
15304         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
15305         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
15306         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15307       }
15308     }
15309   }else
15310
15311   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
15312     sqlite3_vfs *pVfs;
15313     sqlite3_vfs *pCurrent = 0;
15314     if( p->db ){
15315       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
15316     }
15317     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
15318       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
15319            pVfs==pCurrent ? "  <--- CURRENT" : "");
15320       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
15321       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
15322       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15323       if( pVfs->pNext ){
15324         raw_printf(p->out, "-----------------------------------\n");
15325       }
15326     }
15327   }else
15328
15329   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
15330     const char *zDbName = nArg==2 ? azArg[1] : "main";
15331     char *zVfsName = 0;
15332     if( p->db ){
15333       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
15334       if( zVfsName ){
15335         utf8_printf(p->out, "%s\n", zVfsName);
15336         sqlite3_free(zVfsName);
15337       }
15338     }
15339   }else
15340
15341 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
15342   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
15343     sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
15344   }else
15345 #endif
15346
15347   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
15348     int j;
15349     assert( nArg<=ArraySize(azArg) );
15350     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
15351       p->colWidth[j-1] = (int)integerValue(azArg[j]);
15352     }
15353   }else
15354
15355   {
15356     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
15357       " \"%s\". Enter \".help\" for help\n", azArg[0]);
15358     rc = 1;
15359   }
15360
15361 meta_command_exit:
15362   if( p->outCount ){
15363     p->outCount--;
15364     if( p->outCount==0 ) output_reset(p);
15365   }
15366   return rc;
15367 }
15368
15369 /*
15370 ** Return TRUE if a semicolon occurs anywhere in the first N characters
15371 ** of string z[].
15372 */
15373 static int line_contains_semicolon(const char *z, int N){
15374   int i;
15375   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
15376   return 0;
15377 }
15378
15379 /*
15380 ** Test to see if a line consists entirely of whitespace.
15381 */
15382 static int _all_whitespace(const char *z){
15383   for(; *z; z++){
15384     if( IsSpace(z[0]) ) continue;
15385     if( *z=='/' && z[1]=='*' ){
15386       z += 2;
15387       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
15388       if( *z==0 ) return 0;
15389       z++;
15390       continue;
15391     }
15392     if( *z=='-' && z[1]=='-' ){
15393       z += 2;
15394       while( *z && *z!='\n' ){ z++; }
15395       if( *z==0 ) return 1;
15396       continue;
15397     }
15398     return 0;
15399   }
15400   return 1;
15401 }
15402
15403 /*
15404 ** Return TRUE if the line typed in is an SQL command terminator other
15405 ** than a semi-colon.  The SQL Server style "go" command is understood
15406 ** as is the Oracle "/".
15407 */
15408 static int line_is_command_terminator(const char *zLine){
15409   while( IsSpace(zLine[0]) ){ zLine++; };
15410   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
15411     return 1;  /* Oracle */
15412   }
15413   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
15414          && _all_whitespace(&zLine[2]) ){
15415     return 1;  /* SQL Server */
15416   }
15417   return 0;
15418 }
15419
15420 /*
15421 ** We need a default sqlite3_complete() implementation to use in case
15422 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
15423 ** any arbitrary text is a complete SQL statement.  This is not very
15424 ** user-friendly, but it does seem to work.
15425 */
15426 #ifdef SQLITE_OMIT_COMPLETE
15427 int sqlite3_complete(const char *zSql){ return 1; }
15428 #endif
15429
15430 /*
15431 ** Return true if zSql is a complete SQL statement.  Return false if it
15432 ** ends in the middle of a string literal or C-style comment.
15433 */
15434 static int line_is_complete(char *zSql, int nSql){
15435   int rc;
15436   if( zSql==0 ) return 1;
15437   zSql[nSql] = ';';
15438   zSql[nSql+1] = 0;
15439   rc = sqlite3_complete(zSql);
15440   zSql[nSql] = 0;
15441   return rc;
15442 }
15443
15444 /*
15445 ** Run a single line of SQL.  Return the number of errors.
15446 */
15447 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
15448   int rc;
15449   char *zErrMsg = 0;
15450
15451   open_db(p, 0);
15452   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
15453   BEGIN_TIMER;
15454   rc = shell_exec(p, zSql, &zErrMsg);
15455   END_TIMER;
15456   if( rc || zErrMsg ){
15457     char zPrefix[100];
15458     if( in!=0 || !stdin_is_interactive ){
15459       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
15460                        "Error: near line %d:", startline);
15461     }else{
15462       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
15463     }
15464     if( zErrMsg!=0 ){
15465       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
15466       sqlite3_free(zErrMsg);
15467       zErrMsg = 0;
15468     }else{
15469       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
15470     }
15471     return 1;
15472   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
15473     raw_printf(p->out, "changes: %3d   total_changes: %d\n",
15474             sqlite3_changes(p->db), sqlite3_total_changes(p->db));
15475   }
15476   return 0;
15477 }
15478
15479
15480 /*
15481 ** Read input from *in and process it.  If *in==0 then input
15482 ** is interactive - the user is typing it it.  Otherwise, input
15483 ** is coming from a file or device.  A prompt is issued and history
15484 ** is saved only if input is interactive.  An interrupt signal will
15485 ** cause this routine to exit immediately, unless input is interactive.
15486 **
15487 ** Return the number of errors.
15488 */
15489 static int process_input(ShellState *p, FILE *in){
15490   char *zLine = 0;          /* A single input line */
15491   char *zSql = 0;           /* Accumulated SQL text */
15492   int nLine;                /* Length of current line */
15493   int nSql = 0;             /* Bytes of zSql[] used */
15494   int nAlloc = 0;           /* Allocated zSql[] space */
15495   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
15496   int rc;                   /* Error code */
15497   int errCnt = 0;           /* Number of errors seen */
15498   int lineno = 0;           /* Current line number */
15499   int startline = 0;        /* Line number for start of current input */
15500
15501   while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
15502     fflush(p->out);
15503     zLine = one_input_line(in, zLine, nSql>0);
15504     if( zLine==0 ){
15505       /* End of input */
15506       if( in==0 && stdin_is_interactive ) printf("\n");
15507       break;
15508     }
15509     if( seenInterrupt ){
15510       if( in!=0 ) break;
15511       seenInterrupt = 0;
15512     }
15513     lineno++;
15514     if( nSql==0 && _all_whitespace(zLine) ){
15515       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
15516       continue;
15517     }
15518     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
15519       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
15520       if( zLine[0]=='.' ){
15521         rc = do_meta_command(zLine, p);
15522         if( rc==2 ){ /* exit requested */
15523           break;
15524         }else if( rc ){
15525           errCnt++;
15526         }
15527       }
15528       continue;
15529     }
15530     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
15531       memcpy(zLine,";",2);
15532     }
15533     nLine = strlen30(zLine);
15534     if( nSql+nLine+2>=nAlloc ){
15535       nAlloc = nSql+nLine+100;
15536       zSql = realloc(zSql, nAlloc);
15537       if( zSql==0 ) shell_out_of_memory();
15538     }
15539     nSqlPrior = nSql;
15540     if( nSql==0 ){
15541       int i;
15542       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
15543       assert( nAlloc>0 && zSql!=0 );
15544       memcpy(zSql, zLine+i, nLine+1-i);
15545       startline = lineno;
15546       nSql = nLine-i;
15547     }else{
15548       zSql[nSql++] = '\n';
15549       memcpy(zSql+nSql, zLine, nLine+1);
15550       nSql += nLine;
15551     }
15552     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
15553                 && sqlite3_complete(zSql) ){
15554       errCnt += runOneSqlLine(p, zSql, in, startline);
15555       nSql = 0;
15556       if( p->outCount ){
15557         output_reset(p);
15558         p->outCount = 0;
15559       }else{
15560         clearTempFile(p);
15561       }
15562     }else if( nSql && _all_whitespace(zSql) ){
15563       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
15564       nSql = 0;
15565     }
15566   }
15567   if( nSql && !_all_whitespace(zSql) ){
15568     errCnt += runOneSqlLine(p, zSql, in, startline);
15569   }
15570   free(zSql);
15571   free(zLine);
15572   return errCnt>0;
15573 }
15574
15575 /*
15576 ** Return a pathname which is the user's home directory.  A
15577 ** 0 return indicates an error of some kind.
15578 */
15579 static char *find_home_dir(int clearFlag){
15580   static char *home_dir = NULL;
15581   if( clearFlag ){
15582     free(home_dir);
15583     home_dir = 0;
15584     return 0;
15585   }
15586   if( home_dir ) return home_dir;
15587
15588 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
15589      && !defined(__RTP__) && !defined(_WRS_KERNEL)
15590   {
15591     struct passwd *pwent;
15592     uid_t uid = getuid();
15593     if( (pwent=getpwuid(uid)) != NULL) {
15594       home_dir = pwent->pw_dir;
15595     }
15596   }
15597 #endif
15598
15599 #if defined(_WIN32_WCE)
15600   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
15601    */
15602   home_dir = "/";
15603 #else
15604
15605 #if defined(_WIN32) || defined(WIN32)
15606   if (!home_dir) {
15607     home_dir = getenv("USERPROFILE");
15608   }
15609 #endif
15610
15611   if (!home_dir) {
15612     home_dir = getenv("HOME");
15613   }
15614
15615 #if defined(_WIN32) || defined(WIN32)
15616   if (!home_dir) {
15617     char *zDrive, *zPath;
15618     int n;
15619     zDrive = getenv("HOMEDRIVE");
15620     zPath = getenv("HOMEPATH");
15621     if( zDrive && zPath ){
15622       n = strlen30(zDrive) + strlen30(zPath) + 1;
15623       home_dir = malloc( n );
15624       if( home_dir==0 ) return 0;
15625       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
15626       return home_dir;
15627     }
15628     home_dir = "c:\\";
15629   }
15630 #endif
15631
15632 #endif /* !_WIN32_WCE */
15633
15634   if( home_dir ){
15635     int n = strlen30(home_dir) + 1;
15636     char *z = malloc( n );
15637     if( z ) memcpy(z, home_dir, n);
15638     home_dir = z;
15639   }
15640
15641   return home_dir;
15642 }
15643
15644 /*
15645 ** Read input from the file given by sqliterc_override.  Or if that
15646 ** parameter is NULL, take input from ~/.sqliterc
15647 **
15648 ** Returns the number of errors.
15649 */
15650 static void process_sqliterc(
15651   ShellState *p,                  /* Configuration data */
15652   const char *sqliterc_override   /* Name of config file. NULL to use default */
15653 ){
15654   char *home_dir = NULL;
15655   const char *sqliterc = sqliterc_override;
15656   char *zBuf = 0;
15657   FILE *in = NULL;
15658
15659   if (sqliterc == NULL) {
15660     home_dir = find_home_dir(0);
15661     if( home_dir==0 ){
15662       raw_printf(stderr, "-- warning: cannot find home directory;"
15663                       " cannot read ~/.sqliterc\n");
15664       return;
15665     }
15666     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
15667     sqliterc = zBuf;
15668   }
15669   in = fopen(sqliterc,"rb");
15670   if( in ){
15671     if( stdin_is_interactive ){
15672       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
15673     }
15674     process_input(p,in);
15675     fclose(in);
15676   }
15677   sqlite3_free(zBuf);
15678 }
15679
15680 /*
15681 ** Show available command line options
15682 */
15683 static const char zOptions[] =
15684 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15685   "   -A ARGS...           run \".archive ARGS\" and exit\n"
15686 #endif
15687   "   -append              append the database to the end of the file\n"
15688   "   -ascii               set output mode to 'ascii'\n"
15689   "   -bail                stop after hitting an error\n"
15690   "   -batch               force batch I/O\n"
15691   "   -column              set output mode to 'column'\n"
15692   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
15693   "   -csv                 set output mode to 'csv'\n"
15694   "   -echo                print commands before execution\n"
15695   "   -init FILENAME       read/process named file\n"
15696   "   -[no]header          turn headers on or off\n"
15697 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
15698   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
15699 #endif
15700   "   -help                show this message\n"
15701   "   -html                set output mode to HTML\n"
15702   "   -interactive         force interactive I/O\n"
15703   "   -line                set output mode to 'line'\n"
15704   "   -list                set output mode to 'list'\n"
15705   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
15706   "   -mmap N              default mmap size set to N\n"
15707 #ifdef SQLITE_ENABLE_MULTIPLEX
15708   "   -multiplex           enable the multiplexor VFS\n"
15709 #endif
15710   "   -newline SEP         set output row separator. Default: '\\n'\n"
15711   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
15712   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
15713   "   -quote               set output mode to 'quote'\n"
15714   "   -readonly            open the database read-only\n"
15715   "   -separator SEP       set output column separator. Default: '|'\n"
15716 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
15717   "   -sorterref SIZE      sorter references threshold size\n"
15718 #endif
15719   "   -stats               print memory stats before each finalize\n"
15720   "   -version             show SQLite version\n"
15721   "   -vfs NAME            use NAME as the default VFS\n"
15722 #ifdef SQLITE_ENABLE_VFSTRACE
15723   "   -vfstrace            enable tracing of all VFS calls\n"
15724 #endif
15725 #ifdef SQLITE_HAVE_ZLIB
15726   "   -zip                 open the file as a ZIP Archive\n"
15727 #endif
15728 ;
15729 static void usage(int showDetail){
15730   utf8_printf(stderr,
15731       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
15732       "FILENAME is the name of an SQLite database. A new database is created\n"
15733       "if the file does not previously exist.\n", Argv0);
15734   if( showDetail ){
15735     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
15736   }else{
15737     raw_printf(stderr, "Use the -help option for additional information\n");
15738   }
15739   exit(1);
15740 }
15741
15742 /*
15743 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
15744 ** error message if it is initialized.
15745 */
15746 static void verify_uninitialized(void){
15747   if( sqlite3_config(-1)==SQLITE_MISUSE ){
15748     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
15749                         " initialization.\n");
15750   }
15751 }
15752
15753 /*
15754 ** Initialize the state information in data
15755 */
15756 static void main_init(ShellState *data) {
15757   memset(data, 0, sizeof(*data));
15758   data->normalMode = data->cMode = data->mode = MODE_List;
15759   data->autoExplain = 1;
15760   memcpy(data->colSeparator,SEP_Column, 2);
15761   memcpy(data->rowSeparator,SEP_Row, 2);
15762   data->showHeader = 0;
15763   data->shellFlgs = SHFLG_Lookaside;
15764   verify_uninitialized();
15765   sqlite3_config(SQLITE_CONFIG_URI, 1);
15766   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
15767   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
15768   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
15769   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
15770 }
15771
15772 /*
15773 ** Output text to the console in a font that attracts extra attention.
15774 */
15775 #ifdef _WIN32
15776 static void printBold(const char *zText){
15777   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
15778   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
15779   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
15780   SetConsoleTextAttribute(out,
15781          FOREGROUND_RED|FOREGROUND_INTENSITY
15782   );
15783   printf("%s", zText);
15784   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
15785 }
15786 #else
15787 static void printBold(const char *zText){
15788   printf("\033[1m%s\033[0m", zText);
15789 }
15790 #endif
15791
15792 /*
15793 ** Get the argument to an --option.  Throw an error and die if no argument
15794 ** is available.
15795 */
15796 static char *cmdline_option_value(int argc, char **argv, int i){
15797   if( i==argc ){
15798     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
15799             argv[0], argv[argc-1]);
15800     exit(1);
15801   }
15802   return argv[i];
15803 }
15804
15805 #ifndef SQLITE_SHELL_IS_UTF8
15806 #  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
15807 #    define SQLITE_SHELL_IS_UTF8          (0)
15808 #  else
15809 #    define SQLITE_SHELL_IS_UTF8          (1)
15810 #  endif
15811 #endif
15812
15813 #if SQLITE_SHELL_IS_UTF8
15814 int SQLITE_CDECL main(int argc, char **argv){
15815 #else
15816 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
15817   char **argv;
15818 #endif
15819   char *zErrMsg = 0;
15820   ShellState data;
15821   const char *zInitFile = 0;
15822   int i;
15823   int rc = 0;
15824   int warnInmemoryDb = 0;
15825   int readStdin = 1;
15826   int nCmd = 0;
15827   char **azCmd = 0;
15828   const char *zVfs = 0;           /* Value of -vfs command-line option */
15829 #if !SQLITE_SHELL_IS_UTF8
15830   char **argvToFree = 0;
15831   int argcToFree = 0;
15832 #endif
15833
15834   setBinaryMode(stdin, 0);
15835   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
15836   stdin_is_interactive = isatty(0);
15837   stdout_is_console = isatty(1);
15838
15839 #if USE_SYSTEM_SQLITE+0!=1
15840   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
15841     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
15842             sqlite3_sourceid(), SQLITE_SOURCE_ID);
15843     exit(1);
15844   }
15845 #endif
15846   main_init(&data);
15847
15848   /* On Windows, we must translate command-line arguments into UTF-8.
15849   ** The SQLite memory allocator subsystem has to be enabled in order to
15850   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
15851   ** subsequent sqlite3_config() calls will work.  So copy all results into
15852   ** memory that does not come from the SQLite memory allocator.
15853   */
15854 #if !SQLITE_SHELL_IS_UTF8
15855   sqlite3_initialize();
15856   argvToFree = malloc(sizeof(argv[0])*argc*2);
15857   argcToFree = argc;
15858   argv = argvToFree + argc;
15859   if( argv==0 ) shell_out_of_memory();
15860   for(i=0; i<argc; i++){
15861     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
15862     int n;
15863     if( z==0 ) shell_out_of_memory();
15864     n = (int)strlen(z);
15865     argv[i] = malloc( n+1 );
15866     if( argv[i]==0 ) shell_out_of_memory();
15867     memcpy(argv[i], z, n+1);
15868     argvToFree[i] = argv[i];
15869     sqlite3_free(z);
15870   }
15871   sqlite3_shutdown();
15872 #endif
15873
15874   assert( argc>=1 && argv && argv[0] );
15875   Argv0 = argv[0];
15876
15877   /* Make sure we have a valid signal handler early, before anything
15878   ** else is done.
15879   */
15880 #ifdef SIGINT
15881   signal(SIGINT, interrupt_handler);
15882 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
15883   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
15884 #endif
15885
15886 #ifdef SQLITE_SHELL_DBNAME_PROC
15887   {
15888     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
15889     ** of a C-function that will provide the name of the database file.  Use
15890     ** this compile-time option to embed this shell program in larger
15891     ** applications. */
15892     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
15893     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
15894     warnInmemoryDb = 0;
15895   }
15896 #endif
15897
15898   /* Do an initial pass through the command-line argument to locate
15899   ** the name of the database file, the name of the initialization file,
15900   ** the size of the alternative malloc heap,
15901   ** and the first command to execute.
15902   */
15903   verify_uninitialized();
15904   for(i=1; i<argc; i++){
15905     char *z;
15906     z = argv[i];
15907     if( z[0]!='-' ){
15908       if( data.zDbFilename==0 ){
15909         data.zDbFilename = z;
15910       }else{
15911         /* Excesss arguments are interpreted as SQL (or dot-commands) and
15912         ** mean that nothing is read from stdin */
15913         readStdin = 0;
15914         nCmd++;
15915         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
15916         if( azCmd==0 ) shell_out_of_memory();
15917         azCmd[nCmd-1] = z;
15918       }
15919     }
15920     if( z[1]=='-' ) z++;
15921     if( strcmp(z,"-separator")==0
15922      || strcmp(z,"-nullvalue")==0
15923      || strcmp(z,"-newline")==0
15924      || strcmp(z,"-cmd")==0
15925     ){
15926       (void)cmdline_option_value(argc, argv, ++i);
15927     }else if( strcmp(z,"-init")==0 ){
15928       zInitFile = cmdline_option_value(argc, argv, ++i);
15929     }else if( strcmp(z,"-batch")==0 ){
15930       /* Need to check for batch mode here to so we can avoid printing
15931       ** informational messages (like from process_sqliterc) before
15932       ** we do the actual processing of arguments later in a second pass.
15933       */
15934       stdin_is_interactive = 0;
15935     }else if( strcmp(z,"-heap")==0 ){
15936 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
15937       const char *zSize;
15938       sqlite3_int64 szHeap;
15939
15940       zSize = cmdline_option_value(argc, argv, ++i);
15941       szHeap = integerValue(zSize);
15942       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
15943       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
15944 #else
15945       (void)cmdline_option_value(argc, argv, ++i);
15946 #endif
15947     }else if( strcmp(z,"-pagecache")==0 ){
15948       int n, sz;
15949       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15950       if( sz>70000 ) sz = 70000;
15951       if( sz<0 ) sz = 0;
15952       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15953       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
15954                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
15955       data.shellFlgs |= SHFLG_Pagecache;
15956     }else if( strcmp(z,"-lookaside")==0 ){
15957       int n, sz;
15958       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15959       if( sz<0 ) sz = 0;
15960       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15961       if( n<0 ) n = 0;
15962       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
15963       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
15964 #ifdef SQLITE_ENABLE_VFSTRACE
15965     }else if( strcmp(z,"-vfstrace")==0 ){
15966       extern int vfstrace_register(
15967          const char *zTraceName,
15968          const char *zOldVfsName,
15969          int (*xOut)(const char*,void*),
15970          void *pOutArg,
15971          int makeDefault
15972       );
15973       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
15974 #endif
15975 #ifdef SQLITE_ENABLE_MULTIPLEX
15976     }else if( strcmp(z,"-multiplex")==0 ){
15977       extern int sqlite3_multiple_initialize(const char*,int);
15978       sqlite3_multiplex_initialize(0, 1);
15979 #endif
15980     }else if( strcmp(z,"-mmap")==0 ){
15981       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
15982       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
15983 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
15984     }else if( strcmp(z,"-sorterref")==0 ){
15985       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
15986       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
15987 #endif
15988     }else if( strcmp(z,"-vfs")==0 ){
15989       zVfs = cmdline_option_value(argc, argv, ++i);
15990 #ifdef SQLITE_HAVE_ZLIB
15991     }else if( strcmp(z,"-zip")==0 ){
15992       data.openMode = SHELL_OPEN_ZIPFILE;
15993 #endif
15994     }else if( strcmp(z,"-append")==0 ){
15995       data.openMode = SHELL_OPEN_APPENDVFS;
15996     }else if( strcmp(z,"-readonly")==0 ){
15997       data.openMode = SHELL_OPEN_READONLY;
15998 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15999     }else if( strncmp(z, "-A",2)==0 ){
16000       /* All remaining command-line arguments are passed to the ".archive"
16001       ** command, so ignore them */
16002       break;
16003 #endif
16004     }
16005   }
16006   verify_uninitialized();
16007
16008
16009 #ifdef SQLITE_SHELL_INIT_PROC
16010   {
16011     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
16012     ** of a C-function that will perform initialization actions on SQLite that
16013     ** occur just before or after sqlite3_initialize(). Use this compile-time
16014     ** option to embed this shell program in larger applications. */
16015     extern void SQLITE_SHELL_INIT_PROC(void);
16016     SQLITE_SHELL_INIT_PROC();
16017   }
16018 #else
16019   /* All the sqlite3_config() calls have now been made. So it is safe
16020   ** to call sqlite3_initialize() and process any command line -vfs option. */
16021   sqlite3_initialize();
16022 #endif
16023
16024   if( zVfs ){
16025     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
16026     if( pVfs ){
16027       sqlite3_vfs_register(pVfs, 1);
16028     }else{
16029       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
16030       exit(1);
16031     }
16032   }
16033
16034   if( data.zDbFilename==0 ){
16035 #ifndef SQLITE_OMIT_MEMORYDB
16036     data.zDbFilename = ":memory:";
16037     warnInmemoryDb = argc==1;
16038 #else
16039     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
16040     return 1;
16041 #endif
16042   }
16043   data.out = stdout;
16044   sqlite3_appendvfs_init(0,0,0);
16045
16046   /* Go ahead and open the database file if it already exists.  If the
16047   ** file does not exist, delay opening it.  This prevents empty database
16048   ** files from being created if a user mistypes the database name argument
16049   ** to the sqlite command-line tool.
16050   */
16051   if( access(data.zDbFilename, 0)==0 ){
16052     open_db(&data, 0);
16053   }
16054
16055   /* Process the initialization file if there is one.  If no -init option
16056   ** is given on the command line, look for a file named ~/.sqliterc and
16057   ** try to process it.
16058   */
16059   process_sqliterc(&data,zInitFile);
16060
16061   /* Make a second pass through the command-line argument and set
16062   ** options.  This second pass is delayed until after the initialization
16063   ** file is processed so that the command-line arguments will override
16064   ** settings in the initialization file.
16065   */
16066   for(i=1; i<argc; i++){
16067     char *z = argv[i];
16068     if( z[0]!='-' ) continue;
16069     if( z[1]=='-' ){ z++; }
16070     if( strcmp(z,"-init")==0 ){
16071       i++;
16072     }else if( strcmp(z,"-html")==0 ){
16073       data.mode = MODE_Html;
16074     }else if( strcmp(z,"-list")==0 ){
16075       data.mode = MODE_List;
16076     }else if( strcmp(z,"-quote")==0 ){
16077       data.mode = MODE_Quote;
16078     }else if( strcmp(z,"-line")==0 ){
16079       data.mode = MODE_Line;
16080     }else if( strcmp(z,"-column")==0 ){
16081       data.mode = MODE_Column;
16082     }else if( strcmp(z,"-csv")==0 ){
16083       data.mode = MODE_Csv;
16084       memcpy(data.colSeparator,",",2);
16085 #ifdef SQLITE_HAVE_ZLIB
16086     }else if( strcmp(z,"-zip")==0 ){
16087       data.openMode = SHELL_OPEN_ZIPFILE;
16088 #endif
16089     }else if( strcmp(z,"-append")==0 ){
16090       data.openMode = SHELL_OPEN_APPENDVFS;
16091     }else if( strcmp(z,"-readonly")==0 ){
16092       data.openMode = SHELL_OPEN_READONLY;
16093     }else if( strcmp(z,"-ascii")==0 ){
16094       data.mode = MODE_Ascii;
16095       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16096                        SEP_Unit);
16097       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16098                        SEP_Record);
16099     }else if( strcmp(z,"-separator")==0 ){
16100       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16101                        "%s",cmdline_option_value(argc,argv,++i));
16102     }else if( strcmp(z,"-newline")==0 ){
16103       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16104                        "%s",cmdline_option_value(argc,argv,++i));
16105     }else if( strcmp(z,"-nullvalue")==0 ){
16106       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
16107                        "%s",cmdline_option_value(argc,argv,++i));
16108     }else if( strcmp(z,"-header")==0 ){
16109       data.showHeader = 1;
16110     }else if( strcmp(z,"-noheader")==0 ){
16111       data.showHeader = 0;
16112     }else if( strcmp(z,"-echo")==0 ){
16113       ShellSetFlag(&data, SHFLG_Echo);
16114     }else if( strcmp(z,"-eqp")==0 ){
16115       data.autoEQP = AUTOEQP_on;
16116     }else if( strcmp(z,"-eqpfull")==0 ){
16117       data.autoEQP = AUTOEQP_full;
16118     }else if( strcmp(z,"-stats")==0 ){
16119       data.statsOn = 1;
16120     }else if( strcmp(z,"-scanstats")==0 ){
16121       data.scanstatsOn = 1;
16122     }else if( strcmp(z,"-backslash")==0 ){
16123       /* Undocumented command-line option: -backslash
16124       ** Causes C-style backslash escapes to be evaluated in SQL statements
16125       ** prior to sending the SQL into SQLite.  Useful for injecting
16126       ** crazy bytes in the middle of SQL statements for testing and debugging.
16127       */
16128       ShellSetFlag(&data, SHFLG_Backslash);
16129     }else if( strcmp(z,"-bail")==0 ){
16130       bail_on_error = 1;
16131     }else if( strcmp(z,"-version")==0 ){
16132       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
16133       return 0;
16134     }else if( strcmp(z,"-interactive")==0 ){
16135       stdin_is_interactive = 1;
16136     }else if( strcmp(z,"-batch")==0 ){
16137       stdin_is_interactive = 0;
16138     }else if( strcmp(z,"-heap")==0 ){
16139       i++;
16140     }else if( strcmp(z,"-pagecache")==0 ){
16141       i+=2;
16142     }else if( strcmp(z,"-lookaside")==0 ){
16143       i+=2;
16144     }else if( strcmp(z,"-mmap")==0 ){
16145       i++;
16146 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16147     }else if( strcmp(z,"-sorterref")==0 ){
16148       i++;
16149 #endif
16150     }else if( strcmp(z,"-vfs")==0 ){
16151       i++;
16152 #ifdef SQLITE_ENABLE_VFSTRACE
16153     }else if( strcmp(z,"-vfstrace")==0 ){
16154       i++;
16155 #endif
16156 #ifdef SQLITE_ENABLE_MULTIPLEX
16157     }else if( strcmp(z,"-multiplex")==0 ){
16158       i++;
16159 #endif
16160     }else if( strcmp(z,"-help")==0 ){
16161       usage(1);
16162     }else if( strcmp(z,"-cmd")==0 ){
16163       /* Run commands that follow -cmd first and separately from commands
16164       ** that simply appear on the command-line.  This seems goofy.  It would
16165       ** be better if all commands ran in the order that they appear.  But
16166       ** we retain the goofy behavior for historical compatibility. */
16167       if( i==argc-1 ) break;
16168       z = cmdline_option_value(argc,argv,++i);
16169       if( z[0]=='.' ){
16170         rc = do_meta_command(z, &data);
16171         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
16172       }else{
16173         open_db(&data, 0);
16174         rc = shell_exec(&data, z, &zErrMsg);
16175         if( zErrMsg!=0 ){
16176           utf8_printf(stderr,"Error: %s\n", zErrMsg);
16177           if( bail_on_error ) return rc!=0 ? rc : 1;
16178         }else if( rc!=0 ){
16179           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
16180           if( bail_on_error ) return rc;
16181         }
16182       }
16183 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16184     }else if( strncmp(z, "-A", 2)==0 ){
16185       if( nCmd>0 ){
16186         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
16187                             " with \"%s\"\n", z);
16188         return 1;
16189       }
16190       open_db(&data, OPEN_DB_ZIPFILE);
16191       if( z[2] ){
16192         argv[i] = &z[2];
16193         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
16194       }else{
16195         arDotCommand(&data, 1, argv+i, argc-i);
16196       }
16197       readStdin = 0;
16198       break;
16199 #endif
16200     }else{
16201       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
16202       raw_printf(stderr,"Use -help for a list of options.\n");
16203       return 1;
16204     }
16205     data.cMode = data.mode;
16206   }
16207
16208   if( !readStdin ){
16209     /* Run all arguments that do not begin with '-' as if they were separate
16210     ** command-line inputs, except for the argToSkip argument which contains
16211     ** the database filename.
16212     */
16213     for(i=0; i<nCmd; i++){
16214       if( azCmd[i][0]=='.' ){
16215         rc = do_meta_command(azCmd[i], &data);
16216         if( rc ) return rc==2 ? 0 : rc;
16217       }else{
16218         open_db(&data, 0);
16219         rc = shell_exec(&data, azCmd[i], &zErrMsg);
16220         if( zErrMsg!=0 ){
16221           utf8_printf(stderr,"Error: %s\n", zErrMsg);
16222           return rc!=0 ? rc : 1;
16223         }else if( rc!=0 ){
16224           utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
16225           return rc;
16226         }
16227       }
16228     }
16229     free(azCmd);
16230   }else{
16231     /* Run commands received from standard input
16232     */
16233     if( stdin_is_interactive ){
16234       char *zHome;
16235       char *zHistory = 0;
16236       int nHistory;
16237       printf(
16238         "SQLite version %s %.19s\n" /*extra-version-info*/
16239         "Enter \".help\" for usage hints.\n",
16240         sqlite3_libversion(), sqlite3_sourceid()
16241       );
16242       if( warnInmemoryDb ){
16243         printf("Connected to a ");
16244         printBold("transient in-memory database");
16245         printf(".\nUse \".open FILENAME\" to reopen on a "
16246                "persistent database.\n");
16247       }
16248       zHome = find_home_dir(0);
16249       if( zHome ){
16250         nHistory = strlen30(zHome) + 20;
16251         if( (zHistory = malloc(nHistory))!=0 ){
16252           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
16253         }
16254       }
16255       if( zHistory ){ shell_read_history(zHistory); }
16256 #if HAVE_READLINE || HAVE_EDITLINE
16257       rl_attempted_completion_function = readline_completion;
16258 #elif HAVE_LINENOISE
16259       linenoiseSetCompletionCallback(linenoise_completion);
16260 #endif
16261       rc = process_input(&data, 0);
16262       if( zHistory ){
16263         shell_stifle_history(2000);
16264         shell_write_history(zHistory);
16265         free(zHistory);
16266       }
16267     }else{
16268       rc = process_input(&data, stdin);
16269     }
16270   }
16271   set_table_name(&data, 0);
16272   if( data.db ){
16273     session_close_all(&data);
16274     close_db(data.db);
16275   }
16276   sqlite3_free(data.zFreeOnClose);
16277   find_home_dir(1);
16278   output_reset(&data);
16279   data.doXdgOpen = 0;
16280   clearTempFile(&data);
16281 #if !SQLITE_SHELL_IS_UTF8
16282   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
16283   free(argvToFree);
16284 #endif
16285   /* Clear the global data structure so that valgrind will detect memory
16286   ** leaks */
16287   memset(&data, 0, sizeof(data));
16288   return rc;
16289 }