]> git.lizzy.rs Git - sqlite3-cmake.git/blob - src/shell.c
bf5be5c3677ba6fa5cda0450a79781192b596c00
[sqlite3-cmake.git] / src / 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 ** Determine if we are dealing with WinRT, which provides only a subset of
40 ** the full Win32 API.
41 */
42 #if !defined(SQLITE_OS_WINRT)
43 # define SQLITE_OS_WINRT 0
44 #endif
45
46 /*
47 ** Warning pragmas copied from msvc.h in the core.
48 */
49 #if defined(_MSC_VER)
50 #pragma warning(disable : 4054)
51 #pragma warning(disable : 4055)
52 #pragma warning(disable : 4100)
53 #pragma warning(disable : 4127)
54 #pragma warning(disable : 4130)
55 #pragma warning(disable : 4152)
56 #pragma warning(disable : 4189)
57 #pragma warning(disable : 4206)
58 #pragma warning(disable : 4210)
59 #pragma warning(disable : 4232)
60 #pragma warning(disable : 4244)
61 #pragma warning(disable : 4305)
62 #pragma warning(disable : 4306)
63 #pragma warning(disable : 4702)
64 #pragma warning(disable : 4706)
65 #endif /* defined(_MSC_VER) */
66
67 /*
68 ** No support for loadable extensions in VxWorks.
69 */
70 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
71 # define SQLITE_OMIT_LOAD_EXTENSION 1
72 #endif
73
74 /*
75 ** Enable large-file support for fopen() and friends on unix.
76 */
77 #ifndef SQLITE_DISABLE_LFS
78 # define _LARGE_FILE       1
79 # ifndef _FILE_OFFSET_BITS
80 #   define _FILE_OFFSET_BITS 64
81 # endif
82 # define _LARGEFILE_SOURCE 1
83 #endif
84
85 #include <stdlib.h>
86 #include <string.h>
87 #include <stdio.h>
88 #include <assert.h>
89 #include "sqlite3.h"
90 typedef sqlite3_int64 i64;
91 typedef sqlite3_uint64 u64;
92 typedef unsigned char u8;
93 #if SQLITE_USER_AUTHENTICATION
94 # include "sqlite3userauth.h"
95 #endif
96 #include <ctype.h>
97 #include <stdarg.h>
98
99 #if !defined(_WIN32) && !defined(WIN32)
100 # include <signal.h>
101 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
102 #  include <pwd.h>
103 # endif
104 #endif
105 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
106 # include <unistd.h>
107 # include <dirent.h>
108 # define GETPID getpid
109 # if defined(__MINGW32__)
110 #  define DIRENT dirent
111 #  ifndef S_ISLNK
112 #   define S_ISLNK(mode) (0)
113 #  endif
114 # endif
115 #else
116 # define GETPID (int)GetCurrentProcessId
117 #endif
118 #include <sys/types.h>
119 #include <sys/stat.h>
120
121 #if HAVE_READLINE
122 # include <readline/readline.h>
123 # include <readline/history.h>
124 #endif
125
126 #if HAVE_EDITLINE
127 # include <editline/readline.h>
128 #endif
129
130 #if HAVE_EDITLINE || HAVE_READLINE
131
132 # define shell_add_history(X) add_history(X)
133 # define shell_read_history(X) read_history(X)
134 # define shell_write_history(X) write_history(X)
135 # define shell_stifle_history(X) stifle_history(X)
136 # define shell_readline(X) readline(X)
137
138 #elif HAVE_LINENOISE
139
140 # include "linenoise.h"
141 # define shell_add_history(X) linenoiseHistoryAdd(X)
142 # define shell_read_history(X) linenoiseHistoryLoad(X)
143 # define shell_write_history(X) linenoiseHistorySave(X)
144 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
145 # define shell_readline(X) linenoise(X)
146
147 #else
148
149 # define shell_read_history(X)
150 # define shell_write_history(X)
151 # define shell_stifle_history(X)
152
153 # define SHELL_USE_LOCAL_GETLINE 1
154 #endif
155
156
157 #if defined(_WIN32) || defined(WIN32)
158 # if SQLITE_OS_WINRT
159 #  define SQLITE_OMIT_POPEN 1
160 # else
161 #  include <io.h>
162 #  include <fcntl.h>
163 #  define isatty(h) _isatty(h)
164 #  ifndef access
165 #   define access(f,m) _access((f),(m))
166 #  endif
167 #  ifndef unlink
168 #   define unlink _unlink
169 #  endif
170 #  ifndef strdup
171 #   define strdup _strdup
172 #  endif
173 #  undef popen
174 #  define popen _popen
175 #  undef pclose
176 #  define pclose _pclose
177 # endif
178 #else
179  /* Make sure isatty() has a prototype. */
180  extern int isatty(int);
181
182 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
183   /* popen and pclose are not C89 functions and so are
184   ** sometimes omitted from the <stdio.h> header */
185    extern FILE *popen(const char*,const char*);
186    extern int pclose(FILE*);
187 # else
188 #  define SQLITE_OMIT_POPEN 1
189 # endif
190 #endif
191
192 #if defined(_WIN32_WCE)
193 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
194  * thus we always assume that we have a console. That can be
195  * overridden with the -batch command line option.
196  */
197 #define isatty(x) 1
198 #endif
199
200 /* ctype macros that work with signed characters */
201 #define IsSpace(X)  isspace((unsigned char)X)
202 #define IsDigit(X)  isdigit((unsigned char)X)
203 #define ToLower(X)  (char)tolower((unsigned char)X)
204
205 #if defined(_WIN32) || defined(WIN32)
206 #if SQLITE_OS_WINRT
207 #include <intrin.h>
208 #endif
209 #include <windows.h>
210
211 /* string conversion routines only needed on Win32 */
212 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
213 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
214 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
215 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
216 #endif
217
218 /* On Windows, we normally run with output mode of TEXT so that \n characters
219 ** are automatically translated into \r\n.  However, this behavior needs
220 ** to be disabled in some cases (ex: when generating CSV output and when
221 ** rendering quoted strings that contain \n characters).  The following
222 ** routines take care of that.
223 */
224 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
225 static void setBinaryMode(FILE *file, int isOutput){
226   if( isOutput ) fflush(file);
227   _setmode(_fileno(file), _O_BINARY);
228 }
229 static void setTextMode(FILE *file, int isOutput){
230   if( isOutput ) fflush(file);
231   _setmode(_fileno(file), _O_TEXT);
232 }
233 #else
234 # define setBinaryMode(X,Y)
235 # define setTextMode(X,Y)
236 #endif
237
238
239 /* True if the timer is enabled */
240 static int enableTimer = 0;
241
242 /* Return the current wall-clock time */
243 static sqlite3_int64 timeOfDay(void){
244   static sqlite3_vfs *clockVfs = 0;
245   sqlite3_int64 t;
246   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
247   if( clockVfs==0 ) return 0;  /* Never actually happens */
248   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
249     clockVfs->xCurrentTimeInt64(clockVfs, &t);
250   }else{
251     double r;
252     clockVfs->xCurrentTime(clockVfs, &r);
253     t = (sqlite3_int64)(r*86400000.0);
254   }
255   return t;
256 }
257
258 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
259 #include <sys/time.h>
260 #include <sys/resource.h>
261
262 /* VxWorks does not support getrusage() as far as we can determine */
263 #if defined(_WRS_KERNEL) || defined(__RTP__)
264 struct rusage {
265   struct timeval ru_utime; /* user CPU time used */
266   struct timeval ru_stime; /* system CPU time used */
267 };
268 #define getrusage(A,B) memset(B,0,sizeof(*B))
269 #endif
270
271 /* Saved resource information for the beginning of an operation */
272 static struct rusage sBegin;  /* CPU time at start */
273 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
274
275 /*
276 ** Begin timing an operation
277 */
278 static void beginTimer(void){
279   if( enableTimer ){
280     getrusage(RUSAGE_SELF, &sBegin);
281     iBegin = timeOfDay();
282   }
283 }
284
285 /* Return the difference of two time_structs in seconds */
286 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
287   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
288          (double)(pEnd->tv_sec - pStart->tv_sec);
289 }
290
291 /*
292 ** Print the timing results.
293 */
294 static void endTimer(void){
295   if( enableTimer ){
296     sqlite3_int64 iEnd = timeOfDay();
297     struct rusage sEnd;
298     getrusage(RUSAGE_SELF, &sEnd);
299     printf("Run Time: real %.3f user %f sys %f\n",
300        (iEnd - iBegin)*0.001,
301        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
302        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
303   }
304 }
305
306 #define BEGIN_TIMER beginTimer()
307 #define END_TIMER endTimer()
308 #define HAS_TIMER 1
309
310 #elif (defined(_WIN32) || defined(WIN32))
311
312 /* Saved resource information for the beginning of an operation */
313 static HANDLE hProcess;
314 static FILETIME ftKernelBegin;
315 static FILETIME ftUserBegin;
316 static sqlite3_int64 ftWallBegin;
317 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
318                                     LPFILETIME, LPFILETIME);
319 static GETPROCTIMES getProcessTimesAddr = NULL;
320
321 /*
322 ** Check to see if we have timer support.  Return 1 if necessary
323 ** support found (or found previously).
324 */
325 static int hasTimer(void){
326   if( getProcessTimesAddr ){
327     return 1;
328   } else {
329 #if !SQLITE_OS_WINRT
330     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
331     ** versions. See if the version we are running on has it, and if it
332     ** does, save off a pointer to it and the current process handle.
333     */
334     hProcess = GetCurrentProcess();
335     if( hProcess ){
336       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
337       if( NULL != hinstLib ){
338         getProcessTimesAddr =
339             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
340         if( NULL != getProcessTimesAddr ){
341           return 1;
342         }
343         FreeLibrary(hinstLib);
344       }
345     }
346 #endif
347   }
348   return 0;
349 }
350
351 /*
352 ** Begin timing an operation
353 */
354 static void beginTimer(void){
355   if( enableTimer && getProcessTimesAddr ){
356     FILETIME ftCreation, ftExit;
357     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
358                         &ftKernelBegin,&ftUserBegin);
359     ftWallBegin = timeOfDay();
360   }
361 }
362
363 /* Return the difference of two FILETIME structs in seconds */
364 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
365   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
366   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
367   return (double) ((i64End - i64Start) / 10000000.0);
368 }
369
370 /*
371 ** Print the timing results.
372 */
373 static void endTimer(void){
374   if( enableTimer && getProcessTimesAddr){
375     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
376     sqlite3_int64 ftWallEnd = timeOfDay();
377     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
378     printf("Run Time: real %.3f user %f sys %f\n",
379        (ftWallEnd - ftWallBegin)*0.001,
380        timeDiff(&ftUserBegin, &ftUserEnd),
381        timeDiff(&ftKernelBegin, &ftKernelEnd));
382   }
383 }
384
385 #define BEGIN_TIMER beginTimer()
386 #define END_TIMER endTimer()
387 #define HAS_TIMER hasTimer()
388
389 #else
390 #define BEGIN_TIMER
391 #define END_TIMER
392 #define HAS_TIMER 0
393 #endif
394
395 /*
396 ** Used to prevent warnings about unused parameters
397 */
398 #define UNUSED_PARAMETER(x) (void)(x)
399
400 /*
401 ** Number of elements in an array
402 */
403 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
404
405 /*
406 ** If the following flag is set, then command execution stops
407 ** at an error if we are not interactive.
408 */
409 static int bail_on_error = 0;
410
411 /*
412 ** Threat stdin as an interactive input if the following variable
413 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
414 */
415 static int stdin_is_interactive = 1;
416
417 /*
418 ** On Windows systems we have to know if standard output is a console
419 ** in order to translate UTF-8 into MBCS.  The following variable is
420 ** true if translation is required.
421 */
422 static int stdout_is_console = 1;
423
424 /*
425 ** The following is the open SQLite database.  We make a pointer
426 ** to this database a static variable so that it can be accessed
427 ** by the SIGINT handler to interrupt database processing.
428 */
429 static sqlite3 *globalDb = 0;
430
431 /*
432 ** True if an interrupt (Control-C) has been received.
433 */
434 static volatile int seenInterrupt = 0;
435
436 #ifdef SQLITE_DEBUG
437 /*
438 ** Out-of-memory simulator variables
439 */
440 static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
441 static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
442 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
443 #endif /* SQLITE_DEBUG */
444
445 /*
446 ** This is the name of our program. It is set in main(), used
447 ** in a number of other places, mostly for error messages.
448 */
449 static char *Argv0;
450
451 /*
452 ** Prompt strings. Initialized in main. Settable with
453 **   .prompt main continue
454 */
455 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
456 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
457
458 /*
459 ** Render output like fprintf().  Except, if the output is going to the
460 ** console and if this is running on a Windows machine, translate the
461 ** output from UTF-8 into MBCS.
462 */
463 #if defined(_WIN32) || defined(WIN32)
464 void utf8_printf(FILE *out, const char *zFormat, ...){
465   va_list ap;
466   va_start(ap, zFormat);
467   if( stdout_is_console && (out==stdout || out==stderr) ){
468     char *z1 = sqlite3_vmprintf(zFormat, ap);
469     char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
470     sqlite3_free(z1);
471     fputs(z2, out);
472     sqlite3_free(z2);
473   }else{
474     vfprintf(out, zFormat, ap);
475   }
476   va_end(ap);
477 }
478 #elif !defined(utf8_printf)
479 # define utf8_printf fprintf
480 #endif
481
482 /*
483 ** Render output like fprintf().  This should not be used on anything that
484 ** includes string formatting (e.g. "%s").
485 */
486 #if !defined(raw_printf)
487 # define raw_printf fprintf
488 #endif
489
490 /* Indicate out-of-memory and exit. */
491 static void shell_out_of_memory(void){
492   raw_printf(stderr,"Error: out of memory\n");
493   exit(1);
494 }
495
496 #ifdef SQLITE_DEBUG
497 /* This routine is called when a simulated OOM occurs.  It is broken
498 ** out as a separate routine to make it easy to set a breakpoint on
499 ** the OOM
500 */
501 void shellOomFault(void){
502   if( oomRepeat>0 ){
503     oomRepeat--;
504   }else{
505     oomCounter--;
506   }
507 }
508 #endif /* SQLITE_DEBUG */
509
510 #ifdef SQLITE_DEBUG
511 /* This routine is a replacement malloc() that is used to simulate
512 ** Out-Of-Memory (OOM) errors for testing purposes.
513 */
514 static void *oomMalloc(int nByte){
515   if( oomCounter ){
516     if( oomCounter==1 ){
517       shellOomFault();
518       return 0;
519     }else{
520       oomCounter--;
521     }
522   }
523   return defaultMalloc(nByte);
524 }
525 #endif /* SQLITE_DEBUG */
526
527 #ifdef SQLITE_DEBUG
528 /* Register the OOM simulator.  This must occur before any memory
529 ** allocations */
530 static void registerOomSimulator(void){
531   sqlite3_mem_methods mem;
532   sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
533   defaultMalloc = mem.xMalloc;
534   mem.xMalloc = oomMalloc;
535   sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
536 }
537 #endif
538
539 /*
540 ** Write I/O traces to the following stream.
541 */
542 #ifdef SQLITE_ENABLE_IOTRACE
543 static FILE *iotrace = 0;
544 #endif
545
546 /*
547 ** This routine works like printf in that its first argument is a
548 ** format string and subsequent arguments are values to be substituted
549 ** in place of % fields.  The result of formatting this string
550 ** is written to iotrace.
551 */
552 #ifdef SQLITE_ENABLE_IOTRACE
553 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
554   va_list ap;
555   char *z;
556   if( iotrace==0 ) return;
557   va_start(ap, zFormat);
558   z = sqlite3_vmprintf(zFormat, ap);
559   va_end(ap);
560   utf8_printf(iotrace, "%s", z);
561   sqlite3_free(z);
562 }
563 #endif
564
565 /*
566 ** Output string zUtf to stream pOut as w characters.  If w is negative,
567 ** then right-justify the text.  W is the width in UTF-8 characters, not
568 ** in bytes.  This is different from the %*.*s specification in printf
569 ** since with %*.*s the width is measured in bytes, not characters.
570 */
571 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
572   int i;
573   int n;
574   int aw = w<0 ? -w : w;
575   for(i=n=0; zUtf[i]; i++){
576     if( (zUtf[i]&0xc0)!=0x80 ){
577       n++;
578       if( n==aw ){
579         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
580         break;
581       }
582     }
583   }
584   if( n>=aw ){
585     utf8_printf(pOut, "%.*s", i, zUtf);
586   }else if( w<0 ){
587     utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
588   }else{
589     utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
590   }
591 }
592
593
594 /*
595 ** Determines if a string is a number of not.
596 */
597 static int isNumber(const char *z, int *realnum){
598   if( *z=='-' || *z=='+' ) z++;
599   if( !IsDigit(*z) ){
600     return 0;
601   }
602   z++;
603   if( realnum ) *realnum = 0;
604   while( IsDigit(*z) ){ z++; }
605   if( *z=='.' ){
606     z++;
607     if( !IsDigit(*z) ) return 0;
608     while( IsDigit(*z) ){ z++; }
609     if( realnum ) *realnum = 1;
610   }
611   if( *z=='e' || *z=='E' ){
612     z++;
613     if( *z=='+' || *z=='-' ) z++;
614     if( !IsDigit(*z) ) return 0;
615     while( IsDigit(*z) ){ z++; }
616     if( realnum ) *realnum = 1;
617   }
618   return *z==0;
619 }
620
621 /*
622 ** Compute a string length that is limited to what can be stored in
623 ** lower 30 bits of a 32-bit signed integer.
624 */
625 static int strlen30(const char *z){
626   const char *z2 = z;
627   while( *z2 ){ z2++; }
628   return 0x3fffffff & (int)(z2 - z);
629 }
630
631 /*
632 ** Return the length of a string in characters.  Multibyte UTF8 characters
633 ** count as a single character.
634 */
635 static int strlenChar(const char *z){
636   int n = 0;
637   while( *z ){
638     if( (0xc0&*(z++))!=0x80 ) n++;
639   }
640   return n;
641 }
642
643 /*
644 ** Return true if zFile does not exist or if it is not an ordinary file.
645 */
646 #ifdef _WIN32
647 # define notNormalFile(X) 0
648 #else
649 static int notNormalFile(const char *zFile){
650   struct stat x;
651   int rc;
652   memset(&x, 0, sizeof(x));
653   rc = stat(zFile, &x);
654   return rc || !S_ISREG(x.st_mode);
655 }
656 #endif
657
658 /*
659 ** This routine reads a line of text from FILE in, stores
660 ** the text in memory obtained from malloc() and returns a pointer
661 ** to the text.  NULL is returned at end of file, or if malloc()
662 ** fails.
663 **
664 ** If zLine is not NULL then it is a malloced buffer returned from
665 ** a previous call to this routine that may be reused.
666 */
667 static char *local_getline(char *zLine, FILE *in){
668   int nLine = zLine==0 ? 0 : 100;
669   int n = 0;
670
671   while( 1 ){
672     if( n+100>nLine ){
673       nLine = nLine*2 + 100;
674       zLine = realloc(zLine, nLine);
675       if( zLine==0 ) shell_out_of_memory();
676     }
677     if( fgets(&zLine[n], nLine - n, in)==0 ){
678       if( n==0 ){
679         free(zLine);
680         return 0;
681       }
682       zLine[n] = 0;
683       break;
684     }
685     while( zLine[n] ) n++;
686     if( n>0 && zLine[n-1]=='\n' ){
687       n--;
688       if( n>0 && zLine[n-1]=='\r' ) n--;
689       zLine[n] = 0;
690       break;
691     }
692   }
693 #if defined(_WIN32) || defined(WIN32)
694   /* For interactive input on Windows systems, translate the
695   ** multi-byte characterset characters into UTF-8. */
696   if( stdin_is_interactive && in==stdin ){
697     char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
698     if( zTrans ){
699       int nTrans = strlen30(zTrans)+1;
700       if( nTrans>nLine ){
701         zLine = realloc(zLine, nTrans);
702         if( zLine==0 ) shell_out_of_memory();
703       }
704       memcpy(zLine, zTrans, nTrans);
705       sqlite3_free(zTrans);
706     }
707   }
708 #endif /* defined(_WIN32) || defined(WIN32) */
709   return zLine;
710 }
711
712 /*
713 ** Retrieve a single line of input text.
714 **
715 ** If in==0 then read from standard input and prompt before each line.
716 ** If isContinuation is true, then a continuation prompt is appropriate.
717 ** If isContinuation is zero, then the main prompt should be used.
718 **
719 ** If zPrior is not NULL then it is a buffer from a prior call to this
720 ** routine that can be reused.
721 **
722 ** The result is stored in space obtained from malloc() and must either
723 ** be freed by the caller or else passed back into this routine via the
724 ** zPrior argument for reuse.
725 */
726 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
727   char *zPrompt;
728   char *zResult;
729   if( in!=0 ){
730     zResult = local_getline(zPrior, in);
731   }else{
732     zPrompt = isContinuation ? continuePrompt : mainPrompt;
733 #if SHELL_USE_LOCAL_GETLINE
734     printf("%s", zPrompt);
735     fflush(stdout);
736     zResult = local_getline(zPrior, stdin);
737 #else
738     free(zPrior);
739     zResult = shell_readline(zPrompt);
740     if( zResult && *zResult ) shell_add_history(zResult);
741 #endif
742   }
743   return zResult;
744 }
745
746
747 /*
748 ** Return the value of a hexadecimal digit.  Return -1 if the input
749 ** is not a hex digit.
750 */
751 static int hexDigitValue(char c){
752   if( c>='0' && c<='9' ) return c - '0';
753   if( c>='a' && c<='f' ) return c - 'a' + 10;
754   if( c>='A' && c<='F' ) return c - 'A' + 10;
755   return -1;
756 }
757
758 /*
759 ** Interpret zArg as an integer value, possibly with suffixes.
760 */
761 static sqlite3_int64 integerValue(const char *zArg){
762   sqlite3_int64 v = 0;
763   static const struct { char *zSuffix; int iMult; } aMult[] = {
764     { "KiB", 1024 },
765     { "MiB", 1024*1024 },
766     { "GiB", 1024*1024*1024 },
767     { "KB",  1000 },
768     { "MB",  1000000 },
769     { "GB",  1000000000 },
770     { "K",   1000 },
771     { "M",   1000000 },
772     { "G",   1000000000 },
773   };
774   int i;
775   int isNeg = 0;
776   if( zArg[0]=='-' ){
777     isNeg = 1;
778     zArg++;
779   }else if( zArg[0]=='+' ){
780     zArg++;
781   }
782   if( zArg[0]=='0' && zArg[1]=='x' ){
783     int x;
784     zArg += 2;
785     while( (x = hexDigitValue(zArg[0]))>=0 ){
786       v = (v<<4) + x;
787       zArg++;
788     }
789   }else{
790     while( IsDigit(zArg[0]) ){
791       v = v*10 + zArg[0] - '0';
792       zArg++;
793     }
794   }
795   for(i=0; i<ArraySize(aMult); i++){
796     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
797       v *= aMult[i].iMult;
798       break;
799     }
800   }
801   return isNeg? -v : v;
802 }
803
804 /*
805 ** A variable length string to which one can append text.
806 */
807 typedef struct ShellText ShellText;
808 struct ShellText {
809   char *z;
810   int n;
811   int nAlloc;
812 };
813
814 /*
815 ** Initialize and destroy a ShellText object
816 */
817 static void initText(ShellText *p){
818   memset(p, 0, sizeof(*p));
819 }
820 static void freeText(ShellText *p){
821   free(p->z);
822   initText(p);
823 }
824
825 /* zIn is either a pointer to a NULL-terminated string in memory obtained
826 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
827 ** added to zIn, and the result returned in memory obtained from malloc().
828 ** zIn, if it was not NULL, is freed.
829 **
830 ** If the third argument, quote, is not '\0', then it is used as a
831 ** quote character for zAppend.
832 */
833 static void appendText(ShellText *p, char const *zAppend, char quote){
834   int len;
835   int i;
836   int nAppend = strlen30(zAppend);
837
838   len = nAppend+p->n+1;
839   if( quote ){
840     len += 2;
841     for(i=0; i<nAppend; i++){
842       if( zAppend[i]==quote ) len++;
843     }
844   }
845
846   if( p->n+len>=p->nAlloc ){
847     p->nAlloc = p->nAlloc*2 + len + 20;
848     p->z = realloc(p->z, p->nAlloc);
849     if( p->z==0 ) shell_out_of_memory();
850   }
851
852   if( quote ){
853     char *zCsr = p->z+p->n;
854     *zCsr++ = quote;
855     for(i=0; i<nAppend; i++){
856       *zCsr++ = zAppend[i];
857       if( zAppend[i]==quote ) *zCsr++ = quote;
858     }
859     *zCsr++ = quote;
860     p->n = (int)(zCsr - p->z);
861     *zCsr = '\0';
862   }else{
863     memcpy(p->z+p->n, zAppend, nAppend);
864     p->n += nAppend;
865     p->z[p->n] = '\0';
866   }
867 }
868
869 /*
870 ** Attempt to determine if identifier zName needs to be quoted, either
871 ** because it contains non-alphanumeric characters, or because it is an
872 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
873 ** that quoting is required.
874 **
875 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
876 */
877 static char quoteChar(const char *zName){
878   int i;
879   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
880   for(i=0; zName[i]; i++){
881     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
882   }
883   return sqlite3_keyword_check(zName, i) ? '"' : 0;
884 }
885
886 /*
887 ** Construct a fake object name and column list to describe the structure
888 ** of the view, virtual table, or table valued function zSchema.zName.
889 */
890 static char *shellFakeSchema(
891   sqlite3 *db,            /* The database connection containing the vtab */
892   const char *zSchema,    /* Schema of the database holding the vtab */
893   const char *zName       /* The name of the virtual table */
894 ){
895   sqlite3_stmt *pStmt = 0;
896   char *zSql;
897   ShellText s;
898   char cQuote;
899   char *zDiv = "(";
900   int nRow = 0;
901
902   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
903                          zSchema ? zSchema : "main", zName);
904   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
905   sqlite3_free(zSql);
906   initText(&s);
907   if( zSchema ){
908     cQuote = quoteChar(zSchema);
909     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
910     appendText(&s, zSchema, cQuote);
911     appendText(&s, ".", 0);
912   }
913   cQuote = quoteChar(zName);
914   appendText(&s, zName, cQuote);
915   while( sqlite3_step(pStmt)==SQLITE_ROW ){
916     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
917     nRow++;
918     appendText(&s, zDiv, 0);
919     zDiv = ",";
920     cQuote = quoteChar(zCol);
921     appendText(&s, zCol, cQuote);
922   }
923   appendText(&s, ")", 0);
924   sqlite3_finalize(pStmt);
925   if( nRow==0 ){
926     freeText(&s);
927     s.z = 0;
928   }
929   return s.z;
930 }
931
932 /*
933 ** SQL function:  shell_module_schema(X)
934 **
935 ** Return a fake schema for the table-valued function or eponymous virtual
936 ** table X.
937 */
938 static void shellModuleSchema(
939   sqlite3_context *pCtx,
940   int nVal,
941   sqlite3_value **apVal
942 ){
943   const char *zName = (const char*)sqlite3_value_text(apVal[0]);
944   char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
945   UNUSED_PARAMETER(nVal);
946   if( zFake ){
947     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
948                         -1, sqlite3_free);
949     free(zFake);
950   }
951 }
952
953 /*
954 ** SQL function:  shell_add_schema(S,X)
955 **
956 ** Add the schema name X to the CREATE statement in S and return the result.
957 ** Examples:
958 **
959 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
960 **
961 ** Also works on
962 **
963 **    CREATE INDEX
964 **    CREATE UNIQUE INDEX
965 **    CREATE VIEW
966 **    CREATE TRIGGER
967 **    CREATE VIRTUAL TABLE
968 **
969 ** This UDF is used by the .schema command to insert the schema name of
970 ** attached databases into the middle of the sqlite_schema.sql field.
971 */
972 static void shellAddSchemaName(
973   sqlite3_context *pCtx,
974   int nVal,
975   sqlite3_value **apVal
976 ){
977   static const char *aPrefix[] = {
978      "TABLE",
979      "INDEX",
980      "UNIQUE INDEX",
981      "VIEW",
982      "TRIGGER",
983      "VIRTUAL TABLE"
984   };
985   int i = 0;
986   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
987   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
988   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
989   sqlite3 *db = sqlite3_context_db_handle(pCtx);
990   UNUSED_PARAMETER(nVal);
991   if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
992     for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
993       int n = strlen30(aPrefix[i]);
994       if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
995         char *z = 0;
996         char *zFake = 0;
997         if( zSchema ){
998           char cQuote = quoteChar(zSchema);
999           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1000             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1001           }else{
1002             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1003           }
1004         }
1005         if( zName
1006          && aPrefix[i][0]=='V'
1007          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1008         ){
1009           if( z==0 ){
1010             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1011           }else{
1012             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1013           }
1014           free(zFake);
1015         }
1016         if( z ){
1017           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1018           return;
1019         }
1020       }
1021     }
1022   }
1023   sqlite3_result_value(pCtx, apVal[0]);
1024 }
1025
1026 /*
1027 ** The source code for several run-time loadable extensions is inserted
1028 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
1029 ** code, we need to override some macros to make the included program code
1030 ** work here in the middle of this regular program.
1031 */
1032 #define SQLITE_EXTENSION_INIT1
1033 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1034
1035 #if defined(_WIN32) && defined(_MSC_VER)
1036 /************************* Begin test_windirent.h ******************/
1037 /*
1038 ** 2015 November 30
1039 **
1040 ** The author disclaims copyright to this source code.  In place of
1041 ** a legal notice, here is a blessing:
1042 **
1043 **    May you do good and not evil.
1044 **    May you find forgiveness for yourself and forgive others.
1045 **    May you share freely, never taking more than you give.
1046 **
1047 *************************************************************************
1048 ** This file contains declarations for most of the opendir() family of
1049 ** POSIX functions on Win32 using the MSVCRT.
1050 */
1051
1052 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1053 #define SQLITE_WINDIRENT_H
1054
1055 /*
1056 ** We need several data types from the Windows SDK header.
1057 */
1058
1059 #ifndef WIN32_LEAN_AND_MEAN
1060 #define WIN32_LEAN_AND_MEAN
1061 #endif
1062
1063 #include "windows.h"
1064
1065 /*
1066 ** We need several support functions from the SQLite core.
1067 */
1068
1069 /* #include "sqlite3.h" */
1070
1071 /*
1072 ** We need several things from the ANSI and MSVCRT headers.
1073 */
1074
1075 #include <stdio.h>
1076 #include <stdlib.h>
1077 #include <errno.h>
1078 #include <io.h>
1079 #include <limits.h>
1080 #include <sys/types.h>
1081 #include <sys/stat.h>
1082
1083 /*
1084 ** We may need several defines that should have been in "sys/stat.h".
1085 */
1086
1087 #ifndef S_ISREG
1088 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1089 #endif
1090
1091 #ifndef S_ISDIR
1092 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1093 #endif
1094
1095 #ifndef S_ISLNK
1096 #define S_ISLNK(mode) (0)
1097 #endif
1098
1099 /*
1100 ** We may need to provide the "mode_t" type.
1101 */
1102
1103 #ifndef MODE_T_DEFINED
1104   #define MODE_T_DEFINED
1105   typedef unsigned short mode_t;
1106 #endif
1107
1108 /*
1109 ** We may need to provide the "ino_t" type.
1110 */
1111
1112 #ifndef INO_T_DEFINED
1113   #define INO_T_DEFINED
1114   typedef unsigned short ino_t;
1115 #endif
1116
1117 /*
1118 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1119 */
1120
1121 #ifndef NAME_MAX
1122 #  ifdef FILENAME_MAX
1123 #    define NAME_MAX (FILENAME_MAX)
1124 #  else
1125 #    define NAME_MAX (260)
1126 #  endif
1127 #endif
1128
1129 /*
1130 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1131 */
1132
1133 #ifndef NULL_INTPTR_T
1134 #  define NULL_INTPTR_T ((intptr_t)(0))
1135 #endif
1136
1137 #ifndef BAD_INTPTR_T
1138 #  define BAD_INTPTR_T ((intptr_t)(-1))
1139 #endif
1140
1141 /*
1142 ** We need to provide the necessary structures and related types.
1143 */
1144
1145 #ifndef DIRENT_DEFINED
1146 #define DIRENT_DEFINED
1147 typedef struct DIRENT DIRENT;
1148 typedef DIRENT *LPDIRENT;
1149 struct DIRENT {
1150   ino_t d_ino;               /* Sequence number, do not use. */
1151   unsigned d_attributes;     /* Win32 file attributes. */
1152   char d_name[NAME_MAX + 1]; /* Name within the directory. */
1153 };
1154 #endif
1155
1156 #ifndef DIR_DEFINED
1157 #define DIR_DEFINED
1158 typedef struct DIR DIR;
1159 typedef DIR *LPDIR;
1160 struct DIR {
1161   intptr_t d_handle; /* Value returned by "_findfirst". */
1162   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1163   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1164 };
1165 #endif
1166
1167 /*
1168 ** Provide a macro, for use by the implementation, to determine if a
1169 ** particular directory entry should be skipped over when searching for
1170 ** the next directory entry that should be returned by the readdir() or
1171 ** readdir_r() functions.
1172 */
1173
1174 #ifndef is_filtered
1175 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1176 #endif
1177
1178 /*
1179 ** Provide the function prototype for the POSIX compatiable getenv()
1180 ** function.  This function is not thread-safe.
1181 */
1182
1183 extern const char *windirent_getenv(const char *name);
1184
1185 /*
1186 ** Finally, we can provide the function prototypes for the opendir(),
1187 ** readdir(), readdir_r(), and closedir() POSIX functions.
1188 */
1189
1190 extern LPDIR opendir(const char *dirname);
1191 extern LPDIRENT readdir(LPDIR dirp);
1192 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1193 extern INT closedir(LPDIR dirp);
1194
1195 #endif /* defined(WIN32) && defined(_MSC_VER) */
1196
1197 /************************* End test_windirent.h ********************/
1198 /************************* Begin test_windirent.c ******************/
1199 /*
1200 ** 2015 November 30
1201 **
1202 ** The author disclaims copyright to this source code.  In place of
1203 ** a legal notice, here is a blessing:
1204 **
1205 **    May you do good and not evil.
1206 **    May you find forgiveness for yourself and forgive others.
1207 **    May you share freely, never taking more than you give.
1208 **
1209 *************************************************************************
1210 ** This file contains code to implement most of the opendir() family of
1211 ** POSIX functions on Win32 using the MSVCRT.
1212 */
1213
1214 #if defined(_WIN32) && defined(_MSC_VER)
1215 /* #include "test_windirent.h" */
1216
1217 /*
1218 ** Implementation of the POSIX getenv() function using the Win32 API.
1219 ** This function is not thread-safe.
1220 */
1221 const char *windirent_getenv(
1222   const char *name
1223 ){
1224   static char value[32768]; /* Maximum length, per MSDN */
1225   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1226   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1227
1228   memset(value, 0, sizeof(value));
1229   dwRet = GetEnvironmentVariableA(name, value, dwSize);
1230   if( dwRet==0 || dwRet>dwSize ){
1231     /*
1232     ** The function call to GetEnvironmentVariableA() failed -OR-
1233     ** the buffer is not large enough.  Either way, return NULL.
1234     */
1235     return 0;
1236   }else{
1237     /*
1238     ** The function call to GetEnvironmentVariableA() succeeded
1239     ** -AND- the buffer contains the entire value.
1240     */
1241     return value;
1242   }
1243 }
1244
1245 /*
1246 ** Implementation of the POSIX opendir() function using the MSVCRT.
1247 */
1248 LPDIR opendir(
1249   const char *dirname
1250 ){
1251   struct _finddata_t data;
1252   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1253   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1254
1255   if( dirp==NULL ) return NULL;
1256   memset(dirp, 0, sizeof(DIR));
1257
1258   /* TODO: Remove this if Unix-style root paths are not used. */
1259   if( sqlite3_stricmp(dirname, "/")==0 ){
1260     dirname = windirent_getenv("SystemDrive");
1261   }
1262
1263   memset(&data, 0, sizeof(struct _finddata_t));
1264   _snprintf(data.name, namesize, "%s\\*", dirname);
1265   dirp->d_handle = _findfirst(data.name, &data);
1266
1267   if( dirp->d_handle==BAD_INTPTR_T ){
1268     closedir(dirp);
1269     return NULL;
1270   }
1271
1272   /* TODO: Remove this block to allow hidden and/or system files. */
1273   if( is_filtered(data) ){
1274 next:
1275
1276     memset(&data, 0, sizeof(struct _finddata_t));
1277     if( _findnext(dirp->d_handle, &data)==-1 ){
1278       closedir(dirp);
1279       return NULL;
1280     }
1281
1282     /* TODO: Remove this block to allow hidden and/or system files. */
1283     if( is_filtered(data) ) goto next;
1284   }
1285
1286   dirp->d_first.d_attributes = data.attrib;
1287   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1288   dirp->d_first.d_name[NAME_MAX] = '\0';
1289
1290   return dirp;
1291 }
1292
1293 /*
1294 ** Implementation of the POSIX readdir() function using the MSVCRT.
1295 */
1296 LPDIRENT readdir(
1297   LPDIR dirp
1298 ){
1299   struct _finddata_t data;
1300
1301   if( dirp==NULL ) return NULL;
1302
1303   if( dirp->d_first.d_ino==0 ){
1304     dirp->d_first.d_ino++;
1305     dirp->d_next.d_ino++;
1306
1307     return &dirp->d_first;
1308   }
1309
1310 next:
1311
1312   memset(&data, 0, sizeof(struct _finddata_t));
1313   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1314
1315   /* TODO: Remove this block to allow hidden and/or system files. */
1316   if( is_filtered(data) ) goto next;
1317
1318   dirp->d_next.d_ino++;
1319   dirp->d_next.d_attributes = data.attrib;
1320   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1321   dirp->d_next.d_name[NAME_MAX] = '\0';
1322
1323   return &dirp->d_next;
1324 }
1325
1326 /*
1327 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1328 */
1329 INT readdir_r(
1330   LPDIR dirp,
1331   LPDIRENT entry,
1332   LPDIRENT *result
1333 ){
1334   struct _finddata_t data;
1335
1336   if( dirp==NULL ) return EBADF;
1337
1338   if( dirp->d_first.d_ino==0 ){
1339     dirp->d_first.d_ino++;
1340     dirp->d_next.d_ino++;
1341
1342     entry->d_ino = dirp->d_first.d_ino;
1343     entry->d_attributes = dirp->d_first.d_attributes;
1344     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1345     entry->d_name[NAME_MAX] = '\0';
1346
1347     *result = entry;
1348     return 0;
1349   }
1350
1351 next:
1352
1353   memset(&data, 0, sizeof(struct _finddata_t));
1354   if( _findnext(dirp->d_handle, &data)==-1 ){
1355     *result = NULL;
1356     return ENOENT;
1357   }
1358
1359   /* TODO: Remove this block to allow hidden and/or system files. */
1360   if( is_filtered(data) ) goto next;
1361
1362   entry->d_ino = (ino_t)-1; /* not available */
1363   entry->d_attributes = data.attrib;
1364   strncpy(entry->d_name, data.name, NAME_MAX);
1365   entry->d_name[NAME_MAX] = '\0';
1366
1367   *result = entry;
1368   return 0;
1369 }
1370
1371 /*
1372 ** Implementation of the POSIX closedir() function using the MSVCRT.
1373 */
1374 INT closedir(
1375   LPDIR dirp
1376 ){
1377   INT result = 0;
1378
1379   if( dirp==NULL ) return EINVAL;
1380
1381   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1382     result = _findclose(dirp->d_handle);
1383   }
1384
1385   sqlite3_free(dirp);
1386   return result;
1387 }
1388
1389 #endif /* defined(WIN32) && defined(_MSC_VER) */
1390
1391 /************************* End test_windirent.c ********************/
1392 #define dirent DIRENT
1393 #endif
1394 /************************* Begin ../ext/misc/shathree.c ******************/
1395 /*
1396 ** 2017-03-08
1397 **
1398 ** The author disclaims copyright to this source code.  In place of
1399 ** a legal notice, here is a blessing:
1400 **
1401 **    May you do good and not evil.
1402 **    May you find forgiveness for yourself and forgive others.
1403 **    May you share freely, never taking more than you give.
1404 **
1405 ******************************************************************************
1406 **
1407 ** This SQLite extension implements functions that compute SHA3 hashes.
1408 ** Two SQL functions are implemented:
1409 **
1410 **     sha3(X,SIZE)
1411 **     sha3_query(Y,SIZE)
1412 **
1413 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1414 ** X is NULL.
1415 **
1416 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1417 ** and returns a hash of their results.
1418 **
1419 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1420 ** is used.  If SIZE is included it must be one of the integers 224, 256,
1421 ** 384, or 512, to determine SHA3 hash variant that is computed.
1422 */
1423 /* #include "sqlite3ext.h" */
1424 SQLITE_EXTENSION_INIT1
1425 #include <assert.h>
1426 #include <string.h>
1427 #include <stdarg.h>
1428
1429 #ifndef SQLITE_AMALGAMATION
1430 /* typedef sqlite3_uint64 u64; */
1431 #endif /* SQLITE_AMALGAMATION */
1432
1433 /******************************************************************************
1434 ** The Hash Engine
1435 */
1436 /*
1437 ** Macros to determine whether the machine is big or little endian,
1438 ** and whether or not that determination is run-time or compile-time.
1439 **
1440 ** For best performance, an attempt is made to guess at the byte-order
1441 ** using C-preprocessor macros.  If that is unsuccessful, or if
1442 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1443 ** at run-time.
1444 */
1445 #ifndef SHA3_BYTEORDER
1446 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1447      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1448      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1449      defined(__arm__)
1450 #   define SHA3_BYTEORDER    1234
1451 # elif defined(sparc)    || defined(__ppc__)
1452 #   define SHA3_BYTEORDER    4321
1453 # else
1454 #   define SHA3_BYTEORDER 0
1455 # endif
1456 #endif
1457
1458
1459 /*
1460 ** State structure for a SHA3 hash in progress
1461 */
1462 typedef struct SHA3Context SHA3Context;
1463 struct SHA3Context {
1464   union {
1465     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1466     unsigned char x[1600];    /* ... or 1600 bytes */
1467   } u;
1468   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1469   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1470   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1471 };
1472
1473 /*
1474 ** A single step of the Keccak mixing function for a 1600-bit state
1475 */
1476 static void KeccakF1600Step(SHA3Context *p){
1477   int i;
1478   u64 b0, b1, b2, b3, b4;
1479   u64 c0, c1, c2, c3, c4;
1480   u64 d0, d1, d2, d3, d4;
1481   static const u64 RC[] = {
1482     0x0000000000000001ULL,  0x0000000000008082ULL,
1483     0x800000000000808aULL,  0x8000000080008000ULL,
1484     0x000000000000808bULL,  0x0000000080000001ULL,
1485     0x8000000080008081ULL,  0x8000000000008009ULL,
1486     0x000000000000008aULL,  0x0000000000000088ULL,
1487     0x0000000080008009ULL,  0x000000008000000aULL,
1488     0x000000008000808bULL,  0x800000000000008bULL,
1489     0x8000000000008089ULL,  0x8000000000008003ULL,
1490     0x8000000000008002ULL,  0x8000000000000080ULL,
1491     0x000000000000800aULL,  0x800000008000000aULL,
1492     0x8000000080008081ULL,  0x8000000000008080ULL,
1493     0x0000000080000001ULL,  0x8000000080008008ULL
1494   };
1495 # define a00 (p->u.s[0])
1496 # define a01 (p->u.s[1])
1497 # define a02 (p->u.s[2])
1498 # define a03 (p->u.s[3])
1499 # define a04 (p->u.s[4])
1500 # define a10 (p->u.s[5])
1501 # define a11 (p->u.s[6])
1502 # define a12 (p->u.s[7])
1503 # define a13 (p->u.s[8])
1504 # define a14 (p->u.s[9])
1505 # define a20 (p->u.s[10])
1506 # define a21 (p->u.s[11])
1507 # define a22 (p->u.s[12])
1508 # define a23 (p->u.s[13])
1509 # define a24 (p->u.s[14])
1510 # define a30 (p->u.s[15])
1511 # define a31 (p->u.s[16])
1512 # define a32 (p->u.s[17])
1513 # define a33 (p->u.s[18])
1514 # define a34 (p->u.s[19])
1515 # define a40 (p->u.s[20])
1516 # define a41 (p->u.s[21])
1517 # define a42 (p->u.s[22])
1518 # define a43 (p->u.s[23])
1519 # define a44 (p->u.s[24])
1520 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1521
1522   for(i=0; i<24; i+=4){
1523     c0 = a00^a10^a20^a30^a40;
1524     c1 = a01^a11^a21^a31^a41;
1525     c2 = a02^a12^a22^a32^a42;
1526     c3 = a03^a13^a23^a33^a43;
1527     c4 = a04^a14^a24^a34^a44;
1528     d0 = c4^ROL64(c1, 1);
1529     d1 = c0^ROL64(c2, 1);
1530     d2 = c1^ROL64(c3, 1);
1531     d3 = c2^ROL64(c4, 1);
1532     d4 = c3^ROL64(c0, 1);
1533
1534     b0 = (a00^d0);
1535     b1 = ROL64((a11^d1), 44);
1536     b2 = ROL64((a22^d2), 43);
1537     b3 = ROL64((a33^d3), 21);
1538     b4 = ROL64((a44^d4), 14);
1539     a00 =   b0 ^((~b1)&  b2 );
1540     a00 ^= RC[i];
1541     a11 =   b1 ^((~b2)&  b3 );
1542     a22 =   b2 ^((~b3)&  b4 );
1543     a33 =   b3 ^((~b4)&  b0 );
1544     a44 =   b4 ^((~b0)&  b1 );
1545
1546     b2 = ROL64((a20^d0), 3);
1547     b3 = ROL64((a31^d1), 45);
1548     b4 = ROL64((a42^d2), 61);
1549     b0 = ROL64((a03^d3), 28);
1550     b1 = ROL64((a14^d4), 20);
1551     a20 =   b0 ^((~b1)&  b2 );
1552     a31 =   b1 ^((~b2)&  b3 );
1553     a42 =   b2 ^((~b3)&  b4 );
1554     a03 =   b3 ^((~b4)&  b0 );
1555     a14 =   b4 ^((~b0)&  b1 );
1556
1557     b4 = ROL64((a40^d0), 18);
1558     b0 = ROL64((a01^d1), 1);
1559     b1 = ROL64((a12^d2), 6);
1560     b2 = ROL64((a23^d3), 25);
1561     b3 = ROL64((a34^d4), 8);
1562     a40 =   b0 ^((~b1)&  b2 );
1563     a01 =   b1 ^((~b2)&  b3 );
1564     a12 =   b2 ^((~b3)&  b4 );
1565     a23 =   b3 ^((~b4)&  b0 );
1566     a34 =   b4 ^((~b0)&  b1 );
1567
1568     b1 = ROL64((a10^d0), 36);
1569     b2 = ROL64((a21^d1), 10);
1570     b3 = ROL64((a32^d2), 15);
1571     b4 = ROL64((a43^d3), 56);
1572     b0 = ROL64((a04^d4), 27);
1573     a10 =   b0 ^((~b1)&  b2 );
1574     a21 =   b1 ^((~b2)&  b3 );
1575     a32 =   b2 ^((~b3)&  b4 );
1576     a43 =   b3 ^((~b4)&  b0 );
1577     a04 =   b4 ^((~b0)&  b1 );
1578
1579     b3 = ROL64((a30^d0), 41);
1580     b4 = ROL64((a41^d1), 2);
1581     b0 = ROL64((a02^d2), 62);
1582     b1 = ROL64((a13^d3), 55);
1583     b2 = ROL64((a24^d4), 39);
1584     a30 =   b0 ^((~b1)&  b2 );
1585     a41 =   b1 ^((~b2)&  b3 );
1586     a02 =   b2 ^((~b3)&  b4 );
1587     a13 =   b3 ^((~b4)&  b0 );
1588     a24 =   b4 ^((~b0)&  b1 );
1589
1590     c0 = a00^a20^a40^a10^a30;
1591     c1 = a11^a31^a01^a21^a41;
1592     c2 = a22^a42^a12^a32^a02;
1593     c3 = a33^a03^a23^a43^a13;
1594     c4 = a44^a14^a34^a04^a24;
1595     d0 = c4^ROL64(c1, 1);
1596     d1 = c0^ROL64(c2, 1);
1597     d2 = c1^ROL64(c3, 1);
1598     d3 = c2^ROL64(c4, 1);
1599     d4 = c3^ROL64(c0, 1);
1600
1601     b0 = (a00^d0);
1602     b1 = ROL64((a31^d1), 44);
1603     b2 = ROL64((a12^d2), 43);
1604     b3 = ROL64((a43^d3), 21);
1605     b4 = ROL64((a24^d4), 14);
1606     a00 =   b0 ^((~b1)&  b2 );
1607     a00 ^= RC[i+1];
1608     a31 =   b1 ^((~b2)&  b3 );
1609     a12 =   b2 ^((~b3)&  b4 );
1610     a43 =   b3 ^((~b4)&  b0 );
1611     a24 =   b4 ^((~b0)&  b1 );
1612
1613     b2 = ROL64((a40^d0), 3);
1614     b3 = ROL64((a21^d1), 45);
1615     b4 = ROL64((a02^d2), 61);
1616     b0 = ROL64((a33^d3), 28);
1617     b1 = ROL64((a14^d4), 20);
1618     a40 =   b0 ^((~b1)&  b2 );
1619     a21 =   b1 ^((~b2)&  b3 );
1620     a02 =   b2 ^((~b3)&  b4 );
1621     a33 =   b3 ^((~b4)&  b0 );
1622     a14 =   b4 ^((~b0)&  b1 );
1623
1624     b4 = ROL64((a30^d0), 18);
1625     b0 = ROL64((a11^d1), 1);
1626     b1 = ROL64((a42^d2), 6);
1627     b2 = ROL64((a23^d3), 25);
1628     b3 = ROL64((a04^d4), 8);
1629     a30 =   b0 ^((~b1)&  b2 );
1630     a11 =   b1 ^((~b2)&  b3 );
1631     a42 =   b2 ^((~b3)&  b4 );
1632     a23 =   b3 ^((~b4)&  b0 );
1633     a04 =   b4 ^((~b0)&  b1 );
1634
1635     b1 = ROL64((a20^d0), 36);
1636     b2 = ROL64((a01^d1), 10);
1637     b3 = ROL64((a32^d2), 15);
1638     b4 = ROL64((a13^d3), 56);
1639     b0 = ROL64((a44^d4), 27);
1640     a20 =   b0 ^((~b1)&  b2 );
1641     a01 =   b1 ^((~b2)&  b3 );
1642     a32 =   b2 ^((~b3)&  b4 );
1643     a13 =   b3 ^((~b4)&  b0 );
1644     a44 =   b4 ^((~b0)&  b1 );
1645
1646     b3 = ROL64((a10^d0), 41);
1647     b4 = ROL64((a41^d1), 2);
1648     b0 = ROL64((a22^d2), 62);
1649     b1 = ROL64((a03^d3), 55);
1650     b2 = ROL64((a34^d4), 39);
1651     a10 =   b0 ^((~b1)&  b2 );
1652     a41 =   b1 ^((~b2)&  b3 );
1653     a22 =   b2 ^((~b3)&  b4 );
1654     a03 =   b3 ^((~b4)&  b0 );
1655     a34 =   b4 ^((~b0)&  b1 );
1656
1657     c0 = a00^a40^a30^a20^a10;
1658     c1 = a31^a21^a11^a01^a41;
1659     c2 = a12^a02^a42^a32^a22;
1660     c3 = a43^a33^a23^a13^a03;
1661     c4 = a24^a14^a04^a44^a34;
1662     d0 = c4^ROL64(c1, 1);
1663     d1 = c0^ROL64(c2, 1);
1664     d2 = c1^ROL64(c3, 1);
1665     d3 = c2^ROL64(c4, 1);
1666     d4 = c3^ROL64(c0, 1);
1667
1668     b0 = (a00^d0);
1669     b1 = ROL64((a21^d1), 44);
1670     b2 = ROL64((a42^d2), 43);
1671     b3 = ROL64((a13^d3), 21);
1672     b4 = ROL64((a34^d4), 14);
1673     a00 =   b0 ^((~b1)&  b2 );
1674     a00 ^= RC[i+2];
1675     a21 =   b1 ^((~b2)&  b3 );
1676     a42 =   b2 ^((~b3)&  b4 );
1677     a13 =   b3 ^((~b4)&  b0 );
1678     a34 =   b4 ^((~b0)&  b1 );
1679
1680     b2 = ROL64((a30^d0), 3);
1681     b3 = ROL64((a01^d1), 45);
1682     b4 = ROL64((a22^d2), 61);
1683     b0 = ROL64((a43^d3), 28);
1684     b1 = ROL64((a14^d4), 20);
1685     a30 =   b0 ^((~b1)&  b2 );
1686     a01 =   b1 ^((~b2)&  b3 );
1687     a22 =   b2 ^((~b3)&  b4 );
1688     a43 =   b3 ^((~b4)&  b0 );
1689     a14 =   b4 ^((~b0)&  b1 );
1690
1691     b4 = ROL64((a10^d0), 18);
1692     b0 = ROL64((a31^d1), 1);
1693     b1 = ROL64((a02^d2), 6);
1694     b2 = ROL64((a23^d3), 25);
1695     b3 = ROL64((a44^d4), 8);
1696     a10 =   b0 ^((~b1)&  b2 );
1697     a31 =   b1 ^((~b2)&  b3 );
1698     a02 =   b2 ^((~b3)&  b4 );
1699     a23 =   b3 ^((~b4)&  b0 );
1700     a44 =   b4 ^((~b0)&  b1 );
1701
1702     b1 = ROL64((a40^d0), 36);
1703     b2 = ROL64((a11^d1), 10);
1704     b3 = ROL64((a32^d2), 15);
1705     b4 = ROL64((a03^d3), 56);
1706     b0 = ROL64((a24^d4), 27);
1707     a40 =   b0 ^((~b1)&  b2 );
1708     a11 =   b1 ^((~b2)&  b3 );
1709     a32 =   b2 ^((~b3)&  b4 );
1710     a03 =   b3 ^((~b4)&  b0 );
1711     a24 =   b4 ^((~b0)&  b1 );
1712
1713     b3 = ROL64((a20^d0), 41);
1714     b4 = ROL64((a41^d1), 2);
1715     b0 = ROL64((a12^d2), 62);
1716     b1 = ROL64((a33^d3), 55);
1717     b2 = ROL64((a04^d4), 39);
1718     a20 =   b0 ^((~b1)&  b2 );
1719     a41 =   b1 ^((~b2)&  b3 );
1720     a12 =   b2 ^((~b3)&  b4 );
1721     a33 =   b3 ^((~b4)&  b0 );
1722     a04 =   b4 ^((~b0)&  b1 );
1723
1724     c0 = a00^a30^a10^a40^a20;
1725     c1 = a21^a01^a31^a11^a41;
1726     c2 = a42^a22^a02^a32^a12;
1727     c3 = a13^a43^a23^a03^a33;
1728     c4 = a34^a14^a44^a24^a04;
1729     d0 = c4^ROL64(c1, 1);
1730     d1 = c0^ROL64(c2, 1);
1731     d2 = c1^ROL64(c3, 1);
1732     d3 = c2^ROL64(c4, 1);
1733     d4 = c3^ROL64(c0, 1);
1734
1735     b0 = (a00^d0);
1736     b1 = ROL64((a01^d1), 44);
1737     b2 = ROL64((a02^d2), 43);
1738     b3 = ROL64((a03^d3), 21);
1739     b4 = ROL64((a04^d4), 14);
1740     a00 =   b0 ^((~b1)&  b2 );
1741     a00 ^= RC[i+3];
1742     a01 =   b1 ^((~b2)&  b3 );
1743     a02 =   b2 ^((~b3)&  b4 );
1744     a03 =   b3 ^((~b4)&  b0 );
1745     a04 =   b4 ^((~b0)&  b1 );
1746
1747     b2 = ROL64((a10^d0), 3);
1748     b3 = ROL64((a11^d1), 45);
1749     b4 = ROL64((a12^d2), 61);
1750     b0 = ROL64((a13^d3), 28);
1751     b1 = ROL64((a14^d4), 20);
1752     a10 =   b0 ^((~b1)&  b2 );
1753     a11 =   b1 ^((~b2)&  b3 );
1754     a12 =   b2 ^((~b3)&  b4 );
1755     a13 =   b3 ^((~b4)&  b0 );
1756     a14 =   b4 ^((~b0)&  b1 );
1757
1758     b4 = ROL64((a20^d0), 18);
1759     b0 = ROL64((a21^d1), 1);
1760     b1 = ROL64((a22^d2), 6);
1761     b2 = ROL64((a23^d3), 25);
1762     b3 = ROL64((a24^d4), 8);
1763     a20 =   b0 ^((~b1)&  b2 );
1764     a21 =   b1 ^((~b2)&  b3 );
1765     a22 =   b2 ^((~b3)&  b4 );
1766     a23 =   b3 ^((~b4)&  b0 );
1767     a24 =   b4 ^((~b0)&  b1 );
1768
1769     b1 = ROL64((a30^d0), 36);
1770     b2 = ROL64((a31^d1), 10);
1771     b3 = ROL64((a32^d2), 15);
1772     b4 = ROL64((a33^d3), 56);
1773     b0 = ROL64((a34^d4), 27);
1774     a30 =   b0 ^((~b1)&  b2 );
1775     a31 =   b1 ^((~b2)&  b3 );
1776     a32 =   b2 ^((~b3)&  b4 );
1777     a33 =   b3 ^((~b4)&  b0 );
1778     a34 =   b4 ^((~b0)&  b1 );
1779
1780     b3 = ROL64((a40^d0), 41);
1781     b4 = ROL64((a41^d1), 2);
1782     b0 = ROL64((a42^d2), 62);
1783     b1 = ROL64((a43^d3), 55);
1784     b2 = ROL64((a44^d4), 39);
1785     a40 =   b0 ^((~b1)&  b2 );
1786     a41 =   b1 ^((~b2)&  b3 );
1787     a42 =   b2 ^((~b3)&  b4 );
1788     a43 =   b3 ^((~b4)&  b0 );
1789     a44 =   b4 ^((~b0)&  b1 );
1790   }
1791 }
1792
1793 /*
1794 ** Initialize a new hash.  iSize determines the size of the hash
1795 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1796 ** can be zero to use the default hash size of 256 bits.
1797 */
1798 static void SHA3Init(SHA3Context *p, int iSize){
1799   memset(p, 0, sizeof(*p));
1800   if( iSize>=128 && iSize<=512 ){
1801     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1802   }else{
1803     p->nRate = (1600 - 2*256)/8;
1804   }
1805 #if SHA3_BYTEORDER==1234
1806   /* Known to be little-endian at compile-time. No-op */
1807 #elif SHA3_BYTEORDER==4321
1808   p->ixMask = 7;  /* Big-endian */
1809 #else
1810   {
1811     static unsigned int one = 1;
1812     if( 1==*(unsigned char*)&one ){
1813       /* Little endian.  No byte swapping. */
1814       p->ixMask = 0;
1815     }else{
1816       /* Big endian.  Byte swap. */
1817       p->ixMask = 7;
1818     }
1819   }
1820 #endif
1821 }
1822
1823 /*
1824 ** Make consecutive calls to the SHA3Update function to add new content
1825 ** to the hash
1826 */
1827 static void SHA3Update(
1828   SHA3Context *p,
1829   const unsigned char *aData,
1830   unsigned int nData
1831 ){
1832   unsigned int i = 0;
1833 #if SHA3_BYTEORDER==1234
1834   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1835     for(; i+7<nData; i+=8){
1836       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1837       p->nLoaded += 8;
1838       if( p->nLoaded>=p->nRate ){
1839         KeccakF1600Step(p);
1840         p->nLoaded = 0;
1841       }
1842     }
1843   }
1844 #endif
1845   for(; i<nData; i++){
1846 #if SHA3_BYTEORDER==1234
1847     p->u.x[p->nLoaded] ^= aData[i];
1848 #elif SHA3_BYTEORDER==4321
1849     p->u.x[p->nLoaded^0x07] ^= aData[i];
1850 #else
1851     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1852 #endif
1853     p->nLoaded++;
1854     if( p->nLoaded==p->nRate ){
1855       KeccakF1600Step(p);
1856       p->nLoaded = 0;
1857     }
1858   }
1859 }
1860
1861 /*
1862 ** After all content has been added, invoke SHA3Final() to compute
1863 ** the final hash.  The function returns a pointer to the binary
1864 ** hash value.
1865 */
1866 static unsigned char *SHA3Final(SHA3Context *p){
1867   unsigned int i;
1868   if( p->nLoaded==p->nRate-1 ){
1869     const unsigned char c1 = 0x86;
1870     SHA3Update(p, &c1, 1);
1871   }else{
1872     const unsigned char c2 = 0x06;
1873     const unsigned char c3 = 0x80;
1874     SHA3Update(p, &c2, 1);
1875     p->nLoaded = p->nRate - 1;
1876     SHA3Update(p, &c3, 1);
1877   }
1878   for(i=0; i<p->nRate; i++){
1879     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1880   }
1881   return &p->u.x[p->nRate];
1882 }
1883 /* End of the hashing logic
1884 *****************************************************************************/
1885
1886 /*
1887 ** Implementation of the sha3(X,SIZE) function.
1888 **
1889 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1890 ** size is 256.  If X is a BLOB, it is hashed as is.  
1891 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1892 ** and the string is hashed without the trailing 0x00 terminator.  The hash
1893 ** of a NULL value is NULL.
1894 */
1895 static void sha3Func(
1896   sqlite3_context *context,
1897   int argc,
1898   sqlite3_value **argv
1899 ){
1900   SHA3Context cx;
1901   int eType = sqlite3_value_type(argv[0]);
1902   int nByte = sqlite3_value_bytes(argv[0]);
1903   int iSize;
1904   if( argc==1 ){
1905     iSize = 256;
1906   }else{
1907     iSize = sqlite3_value_int(argv[1]);
1908     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1909       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1910                                     "384 512", -1);
1911       return;
1912     }
1913   }
1914   if( eType==SQLITE_NULL ) return;
1915   SHA3Init(&cx, iSize);
1916   if( eType==SQLITE_BLOB ){
1917     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1918   }else{
1919     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1920   }
1921   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1922 }
1923
1924 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1925 ** of 50 bytes and add it to the hash.
1926 */
1927 static void hash_step_vformat(
1928   SHA3Context *p,                 /* Add content to this context */
1929   const char *zFormat,
1930   ...
1931 ){
1932   va_list ap;
1933   int n;
1934   char zBuf[50];
1935   va_start(ap, zFormat);
1936   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1937   va_end(ap);
1938   n = (int)strlen(zBuf);
1939   SHA3Update(p, (unsigned char*)zBuf, n);
1940 }
1941
1942 /*
1943 ** Implementation of the sha3_query(SQL,SIZE) function.
1944 **
1945 ** This function compiles and runs the SQL statement(s) given in the
1946 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
1947 ** size is 256.
1948 **
1949 ** The format of the byte stream that is hashed is summarized as follows:
1950 **
1951 **       S<n>:<sql>
1952 **       R
1953 **       N
1954 **       I<int>
1955 **       F<ieee-float>
1956 **       B<size>:<bytes>
1957 **       T<size>:<text>
1958 **
1959 ** <sql> is the original SQL text for each statement run and <n> is
1960 ** the size of that text.  The SQL text is UTF-8.  A single R character
1961 ** occurs before the start of each row.  N means a NULL value.
1962 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
1963 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1964 ** B means blobs of <size> bytes.  T means text rendered as <size>
1965 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1966 ** text integers.
1967 **
1968 ** For each SQL statement in the X input, there is one S segment.  Each
1969 ** S segment is followed by zero or more R segments, one for each row in the
1970 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
1971 ** one for each column in the result set.  Segments are concatentated directly
1972 ** with no delimiters of any kind.
1973 */
1974 static void sha3QueryFunc(
1975   sqlite3_context *context,
1976   int argc,
1977   sqlite3_value **argv
1978 ){
1979   sqlite3 *db = sqlite3_context_db_handle(context);
1980   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1981   sqlite3_stmt *pStmt = 0;
1982   int nCol;                   /* Number of columns in the result set */
1983   int i;                      /* Loop counter */
1984   int rc;
1985   int n;
1986   const char *z;
1987   SHA3Context cx;
1988   int iSize;
1989
1990   if( argc==1 ){
1991     iSize = 256;
1992   }else{
1993     iSize = sqlite3_value_int(argv[1]);
1994     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1995       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1996                                     "384 512", -1);
1997       return;
1998     }
1999   }
2000   if( zSql==0 ) return;
2001   SHA3Init(&cx, iSize);
2002   while( zSql[0] ){
2003     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2004     if( rc ){
2005       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2006                                    zSql, sqlite3_errmsg(db));
2007       sqlite3_finalize(pStmt);
2008       sqlite3_result_error(context, zMsg, -1);
2009       sqlite3_free(zMsg);
2010       return;
2011     }
2012     if( !sqlite3_stmt_readonly(pStmt) ){
2013       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2014       sqlite3_finalize(pStmt);
2015       sqlite3_result_error(context, zMsg, -1);
2016       sqlite3_free(zMsg);
2017       return;
2018     }
2019     nCol = sqlite3_column_count(pStmt);
2020     z = sqlite3_sql(pStmt);
2021     if( z ){
2022       n = (int)strlen(z);
2023       hash_step_vformat(&cx,"S%d:",n);
2024       SHA3Update(&cx,(unsigned char*)z,n);
2025     }
2026
2027     /* Compute a hash over the result of the query */
2028     while( SQLITE_ROW==sqlite3_step(pStmt) ){
2029       SHA3Update(&cx,(const unsigned char*)"R",1);
2030       for(i=0; i<nCol; i++){
2031         switch( sqlite3_column_type(pStmt,i) ){
2032           case SQLITE_NULL: {
2033             SHA3Update(&cx, (const unsigned char*)"N",1);
2034             break;
2035           }
2036           case SQLITE_INTEGER: {
2037             sqlite3_uint64 u;
2038             int j;
2039             unsigned char x[9];
2040             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2041             memcpy(&u, &v, 8);
2042             for(j=8; j>=1; j--){
2043               x[j] = u & 0xff;
2044               u >>= 8;
2045             }
2046             x[0] = 'I';
2047             SHA3Update(&cx, x, 9);
2048             break;
2049           }
2050           case SQLITE_FLOAT: {
2051             sqlite3_uint64 u;
2052             int j;
2053             unsigned char x[9];
2054             double r = sqlite3_column_double(pStmt,i);
2055             memcpy(&u, &r, 8);
2056             for(j=8; j>=1; j--){
2057               x[j] = u & 0xff;
2058               u >>= 8;
2059             }
2060             x[0] = 'F';
2061             SHA3Update(&cx,x,9);
2062             break;
2063           }
2064           case SQLITE_TEXT: {
2065             int n2 = sqlite3_column_bytes(pStmt, i);
2066             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2067             hash_step_vformat(&cx,"T%d:",n2);
2068             SHA3Update(&cx, z2, n2);
2069             break;
2070           }
2071           case SQLITE_BLOB: {
2072             int n2 = sqlite3_column_bytes(pStmt, i);
2073             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2074             hash_step_vformat(&cx,"B%d:",n2);
2075             SHA3Update(&cx, z2, n2);
2076             break;
2077           }
2078         }
2079       }
2080     }
2081     sqlite3_finalize(pStmt);
2082   }
2083   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2084 }
2085
2086
2087 #ifdef _WIN32
2088
2089 #endif
2090 int sqlite3_shathree_init(
2091   sqlite3 *db,
2092   char **pzErrMsg,
2093   const sqlite3_api_routines *pApi
2094 ){
2095   int rc = SQLITE_OK;
2096   SQLITE_EXTENSION_INIT2(pApi);
2097   (void)pzErrMsg;  /* Unused parameter */
2098   rc = sqlite3_create_function(db, "sha3", 1,
2099                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2100                       0, sha3Func, 0, 0);
2101   if( rc==SQLITE_OK ){
2102     rc = sqlite3_create_function(db, "sha3", 2,
2103                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2104                       0, sha3Func, 0, 0);
2105   }
2106   if( rc==SQLITE_OK ){
2107     rc = sqlite3_create_function(db, "sha3_query", 1,
2108                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2109                       0, sha3QueryFunc, 0, 0);
2110   }
2111   if( rc==SQLITE_OK ){
2112     rc = sqlite3_create_function(db, "sha3_query", 2,
2113                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2114                       0, sha3QueryFunc, 0, 0);
2115   }
2116   return rc;
2117 }
2118
2119 /************************* End ../ext/misc/shathree.c ********************/
2120 /************************* Begin ../ext/misc/fileio.c ******************/
2121 /*
2122 ** 2014-06-13
2123 **
2124 ** The author disclaims copyright to this source code.  In place of
2125 ** a legal notice, here is a blessing:
2126 **
2127 **    May you do good and not evil.
2128 **    May you find forgiveness for yourself and forgive others.
2129 **    May you share freely, never taking more than you give.
2130 **
2131 ******************************************************************************
2132 **
2133 ** This SQLite extension implements SQL functions readfile() and
2134 ** writefile(), and eponymous virtual type "fsdir".
2135 **
2136 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2137 **
2138 **   If neither of the optional arguments is present, then this UDF
2139 **   function writes blob DATA to file FILE. If successful, the number
2140 **   of bytes written is returned. If an error occurs, NULL is returned.
2141 **
2142 **   If the first option argument - MODE - is present, then it must
2143 **   be passed an integer value that corresponds to a POSIX mode
2144 **   value (file type + permissions, as returned in the stat.st_mode
2145 **   field by the stat() system call). Three types of files may
2146 **   be written/created:
2147 **
2148 **     regular files:  (mode & 0170000)==0100000
2149 **     symbolic links: (mode & 0170000)==0120000
2150 **     directories:    (mode & 0170000)==0040000
2151 **
2152 **   For a directory, the DATA is ignored. For a symbolic link, it is
2153 **   interpreted as text and used as the target of the link. For a
2154 **   regular file, it is interpreted as a blob and written into the
2155 **   named file. Regardless of the type of file, its permissions are
2156 **   set to (mode & 0777) before returning.
2157 **
2158 **   If the optional MTIME argument is present, then it is interpreted
2159 **   as an integer - the number of seconds since the unix epoch. The
2160 **   modification-time of the target file is set to this value before
2161 **   returning.
2162 **
2163 **   If three or more arguments are passed to this function and an
2164 **   error is encountered, an exception is raised.
2165 **
2166 ** READFILE(FILE):
2167 **
2168 **   Read and return the contents of file FILE (type blob) from disk.
2169 **
2170 ** FSDIR:
2171 **
2172 **   Used as follows:
2173 **
2174 **     SELECT * FROM fsdir($path [, $dir]);
2175 **
2176 **   Parameter $path is an absolute or relative pathname. If the file that it
2177 **   refers to does not exist, it is an error. If the path refers to a regular
2178 **   file or symbolic link, it returns a single row. Or, if the path refers
2179 **   to a directory, it returns one row for the directory, and one row for each
2180 **   file within the hierarchy rooted at $path.
2181 **
2182 **   Each row has the following columns:
2183 **
2184 **     name:  Path to file or directory (text value).
2185 **     mode:  Value of stat.st_mode for directory entry (an integer).
2186 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2187 **     data:  For a regular file, a blob containing the file data. For a
2188 **            symlink, a text value containing the text of the link. For a
2189 **            directory, NULL.
2190 **
2191 **   If a non-NULL value is specified for the optional $dir parameter and
2192 **   $path is a relative path, then $path is interpreted relative to $dir. 
2193 **   And the paths returned in the "name" column of the table are also 
2194 **   relative to directory $dir.
2195 */
2196 /* #include "sqlite3ext.h" */
2197 SQLITE_EXTENSION_INIT1
2198 #include <stdio.h>
2199 #include <string.h>
2200 #include <assert.h>
2201
2202 #include <sys/types.h>
2203 #include <sys/stat.h>
2204 #include <fcntl.h>
2205 #if !defined(_WIN32) && !defined(WIN32)
2206 #  include <unistd.h>
2207 #  include <dirent.h>
2208 #  include <utime.h>
2209 #  include <sys/time.h>
2210 #else
2211 #  include "windows.h"
2212 #  include <io.h>
2213 #  include <direct.h>
2214 /* #  include "test_windirent.h" */
2215 #  define dirent DIRENT
2216 #  ifndef chmod
2217 #    define chmod _chmod
2218 #  endif
2219 #  ifndef stat
2220 #    define stat _stat
2221 #  endif
2222 #  define mkdir(path,mode) _mkdir(path)
2223 #  define lstat(path,buf) stat(path,buf)
2224 #endif
2225 #include <time.h>
2226 #include <errno.h>
2227
2228
2229 /*
2230 ** Structure of the fsdir() table-valued function
2231 */
2232                  /*    0    1    2     3    4           5             */
2233 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2234 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
2235 #define FSDIR_COLUMN_MODE     1     /* Access mode */
2236 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2237 #define FSDIR_COLUMN_DATA     3     /* File content */
2238 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2239 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2240
2241
2242 /*
2243 ** Set the result stored by context ctx to a blob containing the 
2244 ** contents of file zName.  Or, leave the result unchanged (NULL)
2245 ** if the file does not exist or is unreadable.
2246 **
2247 ** If the file exceeds the SQLite blob size limit, through an
2248 ** SQLITE_TOOBIG error.
2249 **
2250 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2251 ** off of disk.
2252 */
2253 static void readFileContents(sqlite3_context *ctx, const char *zName){
2254   FILE *in;
2255   sqlite3_int64 nIn;
2256   void *pBuf;
2257   sqlite3 *db;
2258   int mxBlob;
2259
2260   in = fopen(zName, "rb");
2261   if( in==0 ){
2262     /* File does not exist or is unreadable. Leave the result set to NULL. */
2263     return;
2264   }
2265   fseek(in, 0, SEEK_END);
2266   nIn = ftell(in);
2267   rewind(in);
2268   db = sqlite3_context_db_handle(ctx);
2269   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2270   if( nIn>mxBlob ){
2271     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2272     fclose(in);
2273     return;
2274   }
2275   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2276   if( pBuf==0 ){
2277     sqlite3_result_error_nomem(ctx);
2278     fclose(in);
2279     return;
2280   }
2281   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2282     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2283   }else{
2284     sqlite3_result_error_code(ctx, SQLITE_IOERR);
2285     sqlite3_free(pBuf);
2286   }
2287   fclose(in);
2288 }
2289
2290 /*
2291 ** Implementation of the "readfile(X)" SQL function.  The entire content
2292 ** of the file named X is read and returned as a BLOB.  NULL is returned
2293 ** if the file does not exist or is unreadable.
2294 */
2295 static void readfileFunc(
2296   sqlite3_context *context,
2297   int argc,
2298   sqlite3_value **argv
2299 ){
2300   const char *zName;
2301   (void)(argc);  /* Unused parameter */
2302   zName = (const char*)sqlite3_value_text(argv[0]);
2303   if( zName==0 ) return;
2304   readFileContents(context, zName);
2305 }
2306
2307 /*
2308 ** Set the error message contained in context ctx to the results of
2309 ** vprintf(zFmt, ...).
2310 */
2311 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2312   char *zMsg = 0;
2313   va_list ap;
2314   va_start(ap, zFmt);
2315   zMsg = sqlite3_vmprintf(zFmt, ap);
2316   sqlite3_result_error(ctx, zMsg, -1);
2317   sqlite3_free(zMsg);
2318   va_end(ap);
2319 }
2320
2321 #if defined(_WIN32)
2322 /*
2323 ** This function is designed to convert a Win32 FILETIME structure into the
2324 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2325 */
2326 static sqlite3_uint64 fileTimeToUnixTime(
2327   LPFILETIME pFileTime
2328 ){
2329   SYSTEMTIME epochSystemTime;
2330   ULARGE_INTEGER epochIntervals;
2331   FILETIME epochFileTime;
2332   ULARGE_INTEGER fileIntervals;
2333
2334   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2335   epochSystemTime.wYear = 1970;
2336   epochSystemTime.wMonth = 1;
2337   epochSystemTime.wDay = 1;
2338   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2339   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2340   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2341
2342   fileIntervals.LowPart = pFileTime->dwLowDateTime;
2343   fileIntervals.HighPart = pFileTime->dwHighDateTime;
2344
2345   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2346 }
2347
2348 /*
2349 ** This function attempts to normalize the time values found in the stat()
2350 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2351 ** appears to return these values as local times.
2352 */
2353 static void statTimesToUtc(
2354   const char *zPath,
2355   struct stat *pStatBuf
2356 ){
2357   HANDLE hFindFile;
2358   WIN32_FIND_DATAW fd;
2359   LPWSTR zUnicodeName;
2360   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2361   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2362   if( zUnicodeName ){
2363     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2364     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2365     if( hFindFile!=NULL ){
2366       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2367       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2368       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2369       FindClose(hFindFile);
2370     }
2371     sqlite3_free(zUnicodeName);
2372   }
2373 }
2374 #endif
2375
2376 /*
2377 ** This function is used in place of stat().  On Windows, special handling
2378 ** is required in order for the included time to be returned as UTC.  On all
2379 ** other systems, this function simply calls stat().
2380 */
2381 static int fileStat(
2382   const char *zPath,
2383   struct stat *pStatBuf
2384 ){
2385 #if defined(_WIN32)
2386   int rc = stat(zPath, pStatBuf);
2387   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2388   return rc;
2389 #else
2390   return stat(zPath, pStatBuf);
2391 #endif
2392 }
2393
2394 /*
2395 ** This function is used in place of lstat().  On Windows, special handling
2396 ** is required in order for the included time to be returned as UTC.  On all
2397 ** other systems, this function simply calls lstat().
2398 */
2399 static int fileLinkStat(
2400   const char *zPath,
2401   struct stat *pStatBuf
2402 ){
2403 #if defined(_WIN32)
2404   int rc = lstat(zPath, pStatBuf);
2405   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2406   return rc;
2407 #else
2408   return lstat(zPath, pStatBuf);
2409 #endif
2410 }
2411
2412 /*
2413 ** Argument zFile is the name of a file that will be created and/or written
2414 ** by SQL function writefile(). This function ensures that the directory
2415 ** zFile will be written to exists, creating it if required. The permissions
2416 ** for any path components created by this function are set in accordance
2417 ** with the current umask.
2418 **
2419 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2420 ** SQLITE_OK is returned if the directory is successfully created, or
2421 ** SQLITE_ERROR otherwise.
2422 */
2423 static int makeDirectory(
2424   const char *zFile
2425 ){
2426   char *zCopy = sqlite3_mprintf("%s", zFile);
2427   int rc = SQLITE_OK;
2428
2429   if( zCopy==0 ){
2430     rc = SQLITE_NOMEM;
2431   }else{
2432     int nCopy = (int)strlen(zCopy);
2433     int i = 1;
2434
2435     while( rc==SQLITE_OK ){
2436       struct stat sStat;
2437       int rc2;
2438
2439       for(; zCopy[i]!='/' && i<nCopy; i++);
2440       if( i==nCopy ) break;
2441       zCopy[i] = '\0';
2442
2443       rc2 = fileStat(zCopy, &sStat);
2444       if( rc2!=0 ){
2445         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2446       }else{
2447         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2448       }
2449       zCopy[i] = '/';
2450       i++;
2451     }
2452
2453     sqlite3_free(zCopy);
2454   }
2455
2456   return rc;
2457 }
2458
2459 /*
2460 ** This function does the work for the writefile() UDF. Refer to 
2461 ** header comments at the top of this file for details.
2462 */
2463 static int writeFile(
2464   sqlite3_context *pCtx,          /* Context to return bytes written in */
2465   const char *zFile,              /* File to write */
2466   sqlite3_value *pData,           /* Data to write */
2467   mode_t mode,                    /* MODE parameter passed to writefile() */
2468   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2469 ){
2470 #if !defined(_WIN32) && !defined(WIN32)
2471   if( S_ISLNK(mode) ){
2472     const char *zTo = (const char*)sqlite3_value_text(pData);
2473     if( symlink(zTo, zFile)<0 ) return 1;
2474   }else
2475 #endif
2476   {
2477     if( S_ISDIR(mode) ){
2478       if( mkdir(zFile, mode) ){
2479         /* The mkdir() call to create the directory failed. This might not
2480         ** be an error though - if there is already a directory at the same
2481         ** path and either the permissions already match or can be changed
2482         ** to do so using chmod(), it is not an error.  */
2483         struct stat sStat;
2484         if( errno!=EEXIST
2485          || 0!=fileStat(zFile, &sStat)
2486          || !S_ISDIR(sStat.st_mode)
2487          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2488         ){
2489           return 1;
2490         }
2491       }
2492     }else{
2493       sqlite3_int64 nWrite = 0;
2494       const char *z;
2495       int rc = 0;
2496       FILE *out = fopen(zFile, "wb");
2497       if( out==0 ) return 1;
2498       z = (const char*)sqlite3_value_blob(pData);
2499       if( z ){
2500         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2501         nWrite = sqlite3_value_bytes(pData);
2502         if( nWrite!=n ){
2503           rc = 1;
2504         }
2505       }
2506       fclose(out);
2507       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2508         rc = 1;
2509       }
2510       if( rc ) return 2;
2511       sqlite3_result_int64(pCtx, nWrite);
2512     }
2513   }
2514
2515   if( mtime>=0 ){
2516 #if defined(_WIN32)
2517 #if !SQLITE_OS_WINRT
2518     /* Windows */
2519     FILETIME lastAccess;
2520     FILETIME lastWrite;
2521     SYSTEMTIME currentTime;
2522     LONGLONG intervals;
2523     HANDLE hFile;
2524     LPWSTR zUnicodeName;
2525     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2526
2527     GetSystemTime(&currentTime);
2528     SystemTimeToFileTime(&currentTime, &lastAccess);
2529     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2530     lastWrite.dwLowDateTime = (DWORD)intervals;
2531     lastWrite.dwHighDateTime = intervals >> 32;
2532     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2533     if( zUnicodeName==0 ){
2534       return 1;
2535     }
2536     hFile = CreateFileW(
2537       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2538       FILE_FLAG_BACKUP_SEMANTICS, NULL
2539     );
2540     sqlite3_free(zUnicodeName);
2541     if( hFile!=INVALID_HANDLE_VALUE ){
2542       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2543       CloseHandle(hFile);
2544       return !bResult;
2545     }else{
2546       return 1;
2547     }
2548 #endif
2549 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2550     /* Recent unix */
2551     struct timespec times[2];
2552     times[0].tv_nsec = times[1].tv_nsec = 0;
2553     times[0].tv_sec = time(0);
2554     times[1].tv_sec = mtime;
2555     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2556       return 1;
2557     }
2558 #else
2559     /* Legacy unix */
2560     struct timeval times[2];
2561     times[0].tv_usec = times[1].tv_usec = 0;
2562     times[0].tv_sec = time(0);
2563     times[1].tv_sec = mtime;
2564     if( utimes(zFile, times) ){
2565       return 1;
2566     }
2567 #endif
2568   }
2569
2570   return 0;
2571 }
2572
2573 /*
2574 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.  
2575 ** Refer to header comments at the top of this file for details.
2576 */
2577 static void writefileFunc(
2578   sqlite3_context *context,
2579   int argc,
2580   sqlite3_value **argv
2581 ){
2582   const char *zFile;
2583   mode_t mode = 0;
2584   int res;
2585   sqlite3_int64 mtime = -1;
2586
2587   if( argc<2 || argc>4 ){
2588     sqlite3_result_error(context, 
2589         "wrong number of arguments to function writefile()", -1
2590     );
2591     return;
2592   }
2593
2594   zFile = (const char*)sqlite3_value_text(argv[0]);
2595   if( zFile==0 ) return;
2596   if( argc>=3 ){
2597     mode = (mode_t)sqlite3_value_int(argv[2]);
2598   }
2599   if( argc==4 ){
2600     mtime = sqlite3_value_int64(argv[3]);
2601   }
2602
2603   res = writeFile(context, zFile, argv[1], mode, mtime);
2604   if( res==1 && errno==ENOENT ){
2605     if( makeDirectory(zFile)==SQLITE_OK ){
2606       res = writeFile(context, zFile, argv[1], mode, mtime);
2607     }
2608   }
2609
2610   if( argc>2 && res!=0 ){
2611     if( S_ISLNK(mode) ){
2612       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2613     }else if( S_ISDIR(mode) ){
2614       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2615     }else{
2616       ctxErrorMsg(context, "failed to write file: %s", zFile);
2617     }
2618   }
2619 }
2620
2621 /*
2622 ** SQL function:   lsmode(MODE)
2623 **
2624 ** Given a numberic st_mode from stat(), convert it into a human-readable
2625 ** text string in the style of "ls -l".
2626 */
2627 static void lsModeFunc(
2628   sqlite3_context *context,
2629   int argc,
2630   sqlite3_value **argv
2631 ){
2632   int i;
2633   int iMode = sqlite3_value_int(argv[0]);
2634   char z[16];
2635   (void)argc;
2636   if( S_ISLNK(iMode) ){
2637     z[0] = 'l';
2638   }else if( S_ISREG(iMode) ){
2639     z[0] = '-';
2640   }else if( S_ISDIR(iMode) ){
2641     z[0] = 'd';
2642   }else{
2643     z[0] = '?';
2644   }
2645   for(i=0; i<3; i++){
2646     int m = (iMode >> ((2-i)*3));
2647     char *a = &z[1 + i*3];
2648     a[0] = (m & 0x4) ? 'r' : '-';
2649     a[1] = (m & 0x2) ? 'w' : '-';
2650     a[2] = (m & 0x1) ? 'x' : '-';
2651   }
2652   z[10] = '\0';
2653   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2654 }
2655
2656 #ifndef SQLITE_OMIT_VIRTUALTABLE
2657
2658 /* 
2659 ** Cursor type for recursively iterating through a directory structure.
2660 */
2661 typedef struct fsdir_cursor fsdir_cursor;
2662 typedef struct FsdirLevel FsdirLevel;
2663
2664 struct FsdirLevel {
2665   DIR *pDir;                 /* From opendir() */
2666   char *zDir;                /* Name of directory (nul-terminated) */
2667 };
2668
2669 struct fsdir_cursor {
2670   sqlite3_vtab_cursor base;  /* Base class - must be first */
2671
2672   int nLvl;                  /* Number of entries in aLvl[] array */
2673   int iLvl;                  /* Index of current entry */
2674   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2675
2676   const char *zBase;
2677   int nBase;
2678
2679   struct stat sStat;         /* Current lstat() results */
2680   char *zPath;               /* Path to current entry */
2681   sqlite3_int64 iRowid;      /* Current rowid */
2682 };
2683
2684 typedef struct fsdir_tab fsdir_tab;
2685 struct fsdir_tab {
2686   sqlite3_vtab base;         /* Base class - must be first */
2687 };
2688
2689 /*
2690 ** Construct a new fsdir virtual table object.
2691 */
2692 static int fsdirConnect(
2693   sqlite3 *db,
2694   void *pAux,
2695   int argc, const char *const*argv,
2696   sqlite3_vtab **ppVtab,
2697   char **pzErr
2698 ){
2699   fsdir_tab *pNew = 0;
2700   int rc;
2701   (void)pAux;
2702   (void)argc;
2703   (void)argv;
2704   (void)pzErr;
2705   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2706   if( rc==SQLITE_OK ){
2707     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2708     if( pNew==0 ) return SQLITE_NOMEM;
2709     memset(pNew, 0, sizeof(*pNew));
2710     sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2711   }
2712   *ppVtab = (sqlite3_vtab*)pNew;
2713   return rc;
2714 }
2715
2716 /*
2717 ** This method is the destructor for fsdir vtab objects.
2718 */
2719 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2720   sqlite3_free(pVtab);
2721   return SQLITE_OK;
2722 }
2723
2724 /*
2725 ** Constructor for a new fsdir_cursor object.
2726 */
2727 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2728   fsdir_cursor *pCur;
2729   (void)p;
2730   pCur = sqlite3_malloc( sizeof(*pCur) );
2731   if( pCur==0 ) return SQLITE_NOMEM;
2732   memset(pCur, 0, sizeof(*pCur));
2733   pCur->iLvl = -1;
2734   *ppCursor = &pCur->base;
2735   return SQLITE_OK;
2736 }
2737
2738 /*
2739 ** Reset a cursor back to the state it was in when first returned
2740 ** by fsdirOpen().
2741 */
2742 static void fsdirResetCursor(fsdir_cursor *pCur){
2743   int i;
2744   for(i=0; i<=pCur->iLvl; i++){
2745     FsdirLevel *pLvl = &pCur->aLvl[i];
2746     if( pLvl->pDir ) closedir(pLvl->pDir);
2747     sqlite3_free(pLvl->zDir);
2748   }
2749   sqlite3_free(pCur->zPath);
2750   sqlite3_free(pCur->aLvl);
2751   pCur->aLvl = 0;
2752   pCur->zPath = 0;
2753   pCur->zBase = 0;
2754   pCur->nBase = 0;
2755   pCur->nLvl = 0;
2756   pCur->iLvl = -1;
2757   pCur->iRowid = 1;
2758 }
2759
2760 /*
2761 ** Destructor for an fsdir_cursor.
2762 */
2763 static int fsdirClose(sqlite3_vtab_cursor *cur){
2764   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2765
2766   fsdirResetCursor(pCur);
2767   sqlite3_free(pCur);
2768   return SQLITE_OK;
2769 }
2770
2771 /*
2772 ** Set the error message for the virtual table associated with cursor
2773 ** pCur to the results of vprintf(zFmt, ...).
2774 */
2775 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2776   va_list ap;
2777   va_start(ap, zFmt);
2778   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2779   va_end(ap);
2780 }
2781
2782
2783 /*
2784 ** Advance an fsdir_cursor to its next row of output.
2785 */
2786 static int fsdirNext(sqlite3_vtab_cursor *cur){
2787   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2788   mode_t m = pCur->sStat.st_mode;
2789
2790   pCur->iRowid++;
2791   if( S_ISDIR(m) ){
2792     /* Descend into this directory */
2793     int iNew = pCur->iLvl + 1;
2794     FsdirLevel *pLvl;
2795     if( iNew>=pCur->nLvl ){
2796       int nNew = iNew+1;
2797       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2798       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2799       if( aNew==0 ) return SQLITE_NOMEM;
2800       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2801       pCur->aLvl = aNew;
2802       pCur->nLvl = nNew;
2803     }
2804     pCur->iLvl = iNew;
2805     pLvl = &pCur->aLvl[iNew];
2806     
2807     pLvl->zDir = pCur->zPath;
2808     pCur->zPath = 0;
2809     pLvl->pDir = opendir(pLvl->zDir);
2810     if( pLvl->pDir==0 ){
2811       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2812       return SQLITE_ERROR;
2813     }
2814   }
2815
2816   while( pCur->iLvl>=0 ){
2817     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2818     struct dirent *pEntry = readdir(pLvl->pDir);
2819     if( pEntry ){
2820       if( pEntry->d_name[0]=='.' ){
2821        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2822        if( pEntry->d_name[1]=='\0' ) continue;
2823       }
2824       sqlite3_free(pCur->zPath);
2825       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2826       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2827       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2828         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2829         return SQLITE_ERROR;
2830       }
2831       return SQLITE_OK;
2832     }
2833     closedir(pLvl->pDir);
2834     sqlite3_free(pLvl->zDir);
2835     pLvl->pDir = 0;
2836     pLvl->zDir = 0;
2837     pCur->iLvl--;
2838   }
2839
2840   /* EOF */
2841   sqlite3_free(pCur->zPath);
2842   pCur->zPath = 0;
2843   return SQLITE_OK;
2844 }
2845
2846 /*
2847 ** Return values of columns for the row at which the series_cursor
2848 ** is currently pointing.
2849 */
2850 static int fsdirColumn(
2851   sqlite3_vtab_cursor *cur,   /* The cursor */
2852   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2853   int i                       /* Which column to return */
2854 ){
2855   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2856   switch( i ){
2857     case FSDIR_COLUMN_NAME: {
2858       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2859       break;
2860     }
2861
2862     case FSDIR_COLUMN_MODE:
2863       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2864       break;
2865
2866     case FSDIR_COLUMN_MTIME:
2867       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2868       break;
2869
2870     case FSDIR_COLUMN_DATA: {
2871       mode_t m = pCur->sStat.st_mode;
2872       if( S_ISDIR(m) ){
2873         sqlite3_result_null(ctx);
2874 #if !defined(_WIN32) && !defined(WIN32)
2875       }else if( S_ISLNK(m) ){
2876         char aStatic[64];
2877         char *aBuf = aStatic;
2878         sqlite3_int64 nBuf = 64;
2879         int n;
2880
2881         while( 1 ){
2882           n = readlink(pCur->zPath, aBuf, nBuf);
2883           if( n<nBuf ) break;
2884           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2885           nBuf = nBuf*2;
2886           aBuf = sqlite3_malloc64(nBuf);
2887           if( aBuf==0 ){
2888             sqlite3_result_error_nomem(ctx);
2889             return SQLITE_NOMEM;
2890           }
2891         }
2892
2893         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2894         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2895 #endif
2896       }else{
2897         readFileContents(ctx, pCur->zPath);
2898       }
2899     }
2900     case FSDIR_COLUMN_PATH:
2901     default: {
2902       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2903       ** always return their values as NULL */
2904       break;
2905     }
2906   }
2907   return SQLITE_OK;
2908 }
2909
2910 /*
2911 ** Return the rowid for the current row. In this implementation, the
2912 ** first row returned is assigned rowid value 1, and each subsequent
2913 ** row a value 1 more than that of the previous.
2914 */
2915 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2916   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2917   *pRowid = pCur->iRowid;
2918   return SQLITE_OK;
2919 }
2920
2921 /*
2922 ** Return TRUE if the cursor has been moved off of the last
2923 ** row of output.
2924 */
2925 static int fsdirEof(sqlite3_vtab_cursor *cur){
2926   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2927   return (pCur->zPath==0);
2928 }
2929
2930 /*
2931 ** xFilter callback.
2932 **
2933 ** idxNum==1   PATH parameter only
2934 ** idxNum==2   Both PATH and DIR supplied
2935 */
2936 static int fsdirFilter(
2937   sqlite3_vtab_cursor *cur, 
2938   int idxNum, const char *idxStr,
2939   int argc, sqlite3_value **argv
2940 ){
2941   const char *zDir = 0;
2942   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2943   (void)idxStr;
2944   fsdirResetCursor(pCur);
2945
2946   if( idxNum==0 ){
2947     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2948     return SQLITE_ERROR;
2949   }
2950
2951   assert( argc==idxNum && (argc==1 || argc==2) );
2952   zDir = (const char*)sqlite3_value_text(argv[0]);
2953   if( zDir==0 ){
2954     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2955     return SQLITE_ERROR;
2956   }
2957   if( argc==2 ){
2958     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2959   }
2960   if( pCur->zBase ){
2961     pCur->nBase = (int)strlen(pCur->zBase)+1;
2962     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2963   }else{
2964     pCur->zPath = sqlite3_mprintf("%s", zDir);
2965   }
2966
2967   if( pCur->zPath==0 ){
2968     return SQLITE_NOMEM;
2969   }
2970   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2971     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2972     return SQLITE_ERROR;
2973   }
2974
2975   return SQLITE_OK;
2976 }
2977
2978 /*
2979 ** SQLite will invoke this method one or more times while planning a query
2980 ** that uses the generate_series virtual table.  This routine needs to create
2981 ** a query plan for each invocation and compute an estimated cost for that
2982 ** plan.
2983 **
2984 ** In this implementation idxNum is used to represent the
2985 ** query plan.  idxStr is unused.
2986 **
2987 ** The query plan is represented by values of idxNum:
2988 **
2989 **  (1)  The path value is supplied by argv[0]
2990 **  (2)  Path is in argv[0] and dir is in argv[1]
2991 */
2992 static int fsdirBestIndex(
2993   sqlite3_vtab *tab,
2994   sqlite3_index_info *pIdxInfo
2995 ){
2996   int i;                 /* Loop over constraints */
2997   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
2998   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
2999   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
3000   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
3001   const struct sqlite3_index_constraint *pConstraint;
3002
3003   (void)tab;
3004   pConstraint = pIdxInfo->aConstraint;
3005   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3006     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3007     switch( pConstraint->iColumn ){
3008       case FSDIR_COLUMN_PATH: {
3009         if( pConstraint->usable ){
3010           idxPath = i;
3011           seenPath = 0;
3012         }else if( idxPath<0 ){
3013           seenPath = 1;
3014         }
3015         break;
3016       }
3017       case FSDIR_COLUMN_DIR: {
3018         if( pConstraint->usable ){
3019           idxDir = i;
3020           seenDir = 0;
3021         }else if( idxDir<0 ){
3022           seenDir = 1;
3023         }
3024         break;
3025       }
3026     } 
3027   }
3028   if( seenPath || seenDir ){
3029     /* If input parameters are unusable, disallow this plan */
3030     return SQLITE_CONSTRAINT;
3031   }
3032
3033   if( idxPath<0 ){
3034     pIdxInfo->idxNum = 0;
3035     /* The pIdxInfo->estimatedCost should have been initialized to a huge
3036     ** number.  Leave it unchanged. */
3037     pIdxInfo->estimatedRows = 0x7fffffff;
3038   }else{
3039     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3040     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3041     if( idxDir>=0 ){
3042       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3043       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3044       pIdxInfo->idxNum = 2;
3045       pIdxInfo->estimatedCost = 10.0;
3046     }else{
3047       pIdxInfo->idxNum = 1;
3048       pIdxInfo->estimatedCost = 100.0;
3049     }
3050   }
3051
3052   return SQLITE_OK;
3053 }
3054
3055 /*
3056 ** Register the "fsdir" virtual table.
3057 */
3058 static int fsdirRegister(sqlite3 *db){
3059   static sqlite3_module fsdirModule = {
3060     0,                         /* iVersion */
3061     0,                         /* xCreate */
3062     fsdirConnect,              /* xConnect */
3063     fsdirBestIndex,            /* xBestIndex */
3064     fsdirDisconnect,           /* xDisconnect */
3065     0,                         /* xDestroy */
3066     fsdirOpen,                 /* xOpen - open a cursor */
3067     fsdirClose,                /* xClose - close a cursor */
3068     fsdirFilter,               /* xFilter - configure scan constraints */
3069     fsdirNext,                 /* xNext - advance a cursor */
3070     fsdirEof,                  /* xEof - check for end of scan */
3071     fsdirColumn,               /* xColumn - read data */
3072     fsdirRowid,                /* xRowid - read data */
3073     0,                         /* xUpdate */
3074     0,                         /* xBegin */
3075     0,                         /* xSync */
3076     0,                         /* xCommit */
3077     0,                         /* xRollback */
3078     0,                         /* xFindMethod */
3079     0,                         /* xRename */
3080     0,                         /* xSavepoint */
3081     0,                         /* xRelease */
3082     0,                         /* xRollbackTo */
3083     0,                         /* xShadowName */
3084   };
3085
3086   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3087   return rc;
3088 }
3089 #else         /* SQLITE_OMIT_VIRTUALTABLE */
3090 # define fsdirRegister(x) SQLITE_OK
3091 #endif
3092
3093 #ifdef _WIN32
3094
3095 #endif
3096 int sqlite3_fileio_init(
3097   sqlite3 *db, 
3098   char **pzErrMsg, 
3099   const sqlite3_api_routines *pApi
3100 ){
3101   int rc = SQLITE_OK;
3102   SQLITE_EXTENSION_INIT2(pApi);
3103   (void)pzErrMsg;  /* Unused parameter */
3104   rc = sqlite3_create_function(db, "readfile", 1, 
3105                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3106                                readfileFunc, 0, 0);
3107   if( rc==SQLITE_OK ){
3108     rc = sqlite3_create_function(db, "writefile", -1,
3109                                  SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3110                                  writefileFunc, 0, 0);
3111   }
3112   if( rc==SQLITE_OK ){
3113     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3114                                  lsModeFunc, 0, 0);
3115   }
3116   if( rc==SQLITE_OK ){
3117     rc = fsdirRegister(db);
3118   }
3119   return rc;
3120 }
3121
3122 /************************* End ../ext/misc/fileio.c ********************/
3123 /************************* Begin ../ext/misc/completion.c ******************/
3124 /*
3125 ** 2017-07-10
3126 **
3127 ** The author disclaims copyright to this source code.  In place of
3128 ** a legal notice, here is a blessing:
3129 **
3130 **    May you do good and not evil.
3131 **    May you find forgiveness for yourself and forgive others.
3132 **    May you share freely, never taking more than you give.
3133 **
3134 *************************************************************************
3135 **
3136 ** This file implements an eponymous virtual table that returns suggested
3137 ** completions for a partial SQL input.
3138 **
3139 ** Suggested usage:
3140 **
3141 **     SELECT DISTINCT candidate COLLATE nocase
3142 **       FROM completion($prefix,$wholeline)
3143 **      ORDER BY 1;
3144 **
3145 ** The two query parameters are optional.  $prefix is the text of the
3146 ** current word being typed and that is to be completed.  $wholeline is
3147 ** the complete input line, used for context.
3148 **
3149 ** The raw completion() table might return the same candidate multiple
3150 ** times, for example if the same column name is used to two or more
3151 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
3152 ** the DISTINCT and ORDER BY are recommended.
3153 **
3154 ** This virtual table operates at the speed of human typing, and so there
3155 ** is no attempt to make it fast.  Even a slow implementation will be much
3156 ** faster than any human can type.
3157 **
3158 */
3159 /* #include "sqlite3ext.h" */
3160 SQLITE_EXTENSION_INIT1
3161 #include <assert.h>
3162 #include <string.h>
3163 #include <ctype.h>
3164
3165 #ifndef SQLITE_OMIT_VIRTUALTABLE
3166
3167 /* completion_vtab is a subclass of sqlite3_vtab which will
3168 ** serve as the underlying representation of a completion virtual table
3169 */
3170 typedef struct completion_vtab completion_vtab;
3171 struct completion_vtab {
3172   sqlite3_vtab base;  /* Base class - must be first */
3173   sqlite3 *db;        /* Database connection for this completion vtab */
3174 };
3175
3176 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3177 ** serve as the underlying representation of a cursor that scans
3178 ** over rows of the result
3179 */
3180 typedef struct completion_cursor completion_cursor;
3181 struct completion_cursor {
3182   sqlite3_vtab_cursor base;  /* Base class - must be first */
3183   sqlite3 *db;               /* Database connection for this cursor */
3184   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3185   char *zPrefix;             /* The prefix for the word we want to complete */
3186   char *zLine;               /* The whole that we want to complete */
3187   const char *zCurrentRow;   /* Current output row */
3188   int szRow;                 /* Length of the zCurrentRow string */
3189   sqlite3_stmt *pStmt;       /* Current statement */
3190   sqlite3_int64 iRowid;      /* The rowid */
3191   int ePhase;                /* Current phase */
3192   int j;                     /* inter-phase counter */
3193 };
3194
3195 /* Values for ePhase:
3196 */
3197 #define COMPLETION_FIRST_PHASE   1
3198 #define COMPLETION_KEYWORDS      1
3199 #define COMPLETION_PRAGMAS       2
3200 #define COMPLETION_FUNCTIONS     3
3201 #define COMPLETION_COLLATIONS    4
3202 #define COMPLETION_INDEXES       5
3203 #define COMPLETION_TRIGGERS      6
3204 #define COMPLETION_DATABASES     7
3205 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3206 #define COMPLETION_COLUMNS       9
3207 #define COMPLETION_MODULES       10
3208 #define COMPLETION_EOF           11
3209
3210 /*
3211 ** The completionConnect() method is invoked to create a new
3212 ** completion_vtab that describes the completion virtual table.
3213 **
3214 ** Think of this routine as the constructor for completion_vtab objects.
3215 **
3216 ** All this routine needs to do is:
3217 **
3218 **    (1) Allocate the completion_vtab object and initialize all fields.
3219 **
3220 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3221 **        result set of queries against completion will look like.
3222 */
3223 static int completionConnect(
3224   sqlite3 *db,
3225   void *pAux,
3226   int argc, const char *const*argv,
3227   sqlite3_vtab **ppVtab,
3228   char **pzErr
3229 ){
3230   completion_vtab *pNew;
3231   int rc;
3232
3233   (void)(pAux);    /* Unused parameter */
3234   (void)(argc);    /* Unused parameter */
3235   (void)(argv);    /* Unused parameter */
3236   (void)(pzErr);   /* Unused parameter */
3237
3238 /* Column numbers */
3239 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3240 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3241 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3242 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3243
3244   sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3245   rc = sqlite3_declare_vtab(db,
3246       "CREATE TABLE x("
3247       "  candidate TEXT,"
3248       "  prefix TEXT HIDDEN,"
3249       "  wholeline TEXT HIDDEN,"
3250       "  phase INT HIDDEN"        /* Used for debugging only */
3251       ")");
3252   if( rc==SQLITE_OK ){
3253     pNew = sqlite3_malloc( sizeof(*pNew) );
3254     *ppVtab = (sqlite3_vtab*)pNew;
3255     if( pNew==0 ) return SQLITE_NOMEM;
3256     memset(pNew, 0, sizeof(*pNew));
3257     pNew->db = db;
3258   }
3259   return rc;
3260 }
3261
3262 /*
3263 ** This method is the destructor for completion_cursor objects.
3264 */
3265 static int completionDisconnect(sqlite3_vtab *pVtab){
3266   sqlite3_free(pVtab);
3267   return SQLITE_OK;
3268 }
3269
3270 /*
3271 ** Constructor for a new completion_cursor object.
3272 */
3273 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3274   completion_cursor *pCur;
3275   pCur = sqlite3_malloc( sizeof(*pCur) );
3276   if( pCur==0 ) return SQLITE_NOMEM;
3277   memset(pCur, 0, sizeof(*pCur));
3278   pCur->db = ((completion_vtab*)p)->db;
3279   *ppCursor = &pCur->base;
3280   return SQLITE_OK;
3281 }
3282
3283 /*
3284 ** Reset the completion_cursor.
3285 */
3286 static void completionCursorReset(completion_cursor *pCur){
3287   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3288   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3289   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3290   pCur->j = 0;
3291 }
3292
3293 /*
3294 ** Destructor for a completion_cursor.
3295 */
3296 static int completionClose(sqlite3_vtab_cursor *cur){
3297   completionCursorReset((completion_cursor*)cur);
3298   sqlite3_free(cur);
3299   return SQLITE_OK;
3300 }
3301
3302 /*
3303 ** Advance a completion_cursor to its next row of output.
3304 **
3305 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3306 ** record the current state of the scan.  This routine sets ->zCurrentRow
3307 ** to the current row of output and then returns.  If no more rows remain,
3308 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3309 ** table that has reached the end of its scan.
3310 **
3311 ** The current implementation just lists potential identifiers and
3312 ** keywords and filters them by zPrefix.  Future enhancements should
3313 ** take zLine into account to try to restrict the set of identifiers and
3314 ** keywords based on what would be legal at the current point of input.
3315 */
3316 static int completionNext(sqlite3_vtab_cursor *cur){
3317   completion_cursor *pCur = (completion_cursor*)cur;
3318   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3319   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3320   pCur->iRowid++;
3321   while( pCur->ePhase!=COMPLETION_EOF ){
3322     switch( pCur->ePhase ){
3323       case COMPLETION_KEYWORDS: {
3324         if( pCur->j >= sqlite3_keyword_count() ){
3325           pCur->zCurrentRow = 0;
3326           pCur->ePhase = COMPLETION_DATABASES;
3327         }else{
3328           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3329         }
3330         iCol = -1;
3331         break;
3332       }
3333       case COMPLETION_DATABASES: {
3334         if( pCur->pStmt==0 ){
3335           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3336                              &pCur->pStmt, 0);
3337         }
3338         iCol = 1;
3339         eNextPhase = COMPLETION_TABLES;
3340         break;
3341       }
3342       case COMPLETION_TABLES: {
3343         if( pCur->pStmt==0 ){
3344           sqlite3_stmt *pS2;
3345           char *zSql = 0;
3346           const char *zSep = "";
3347           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3348           while( sqlite3_step(pS2)==SQLITE_ROW ){
3349             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3350             zSql = sqlite3_mprintf(
3351                "%z%s"
3352                "SELECT name FROM \"%w\".sqlite_schema",
3353                zSql, zSep, zDb
3354             );
3355             if( zSql==0 ) return SQLITE_NOMEM;
3356             zSep = " UNION ";
3357           }
3358           sqlite3_finalize(pS2);
3359           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3360           sqlite3_free(zSql);
3361         }
3362         iCol = 0;
3363         eNextPhase = COMPLETION_COLUMNS;
3364         break;
3365       }
3366       case COMPLETION_COLUMNS: {
3367         if( pCur->pStmt==0 ){
3368           sqlite3_stmt *pS2;
3369           char *zSql = 0;
3370           const char *zSep = "";
3371           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3372           while( sqlite3_step(pS2)==SQLITE_ROW ){
3373             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3374             zSql = sqlite3_mprintf(
3375                "%z%s"
3376                "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3377                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3378                " WHERE sm.type='table'",
3379                zSql, zSep, zDb, zDb
3380             );
3381             if( zSql==0 ) return SQLITE_NOMEM;
3382             zSep = " UNION ";
3383           }
3384           sqlite3_finalize(pS2);
3385           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3386           sqlite3_free(zSql);
3387         }
3388         iCol = 0;
3389         eNextPhase = COMPLETION_EOF;
3390         break;
3391       }
3392     }
3393     if( iCol<0 ){
3394       /* This case is when the phase presets zCurrentRow */
3395       if( pCur->zCurrentRow==0 ) continue;
3396     }else{
3397       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3398         /* Extract the next row of content */
3399         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3400         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3401       }else{
3402         /* When all rows are finished, advance to the next phase */
3403         sqlite3_finalize(pCur->pStmt);
3404         pCur->pStmt = 0;
3405         pCur->ePhase = eNextPhase;
3406         continue;
3407       }
3408     }
3409     if( pCur->nPrefix==0 ) break;
3410     if( pCur->nPrefix<=pCur->szRow
3411      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3412     ){
3413       break;
3414     }
3415   }
3416
3417   return SQLITE_OK;
3418 }
3419
3420 /*
3421 ** Return values of columns for the row at which the completion_cursor
3422 ** is currently pointing.
3423 */
3424 static int completionColumn(
3425   sqlite3_vtab_cursor *cur,   /* The cursor */
3426   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3427   int i                       /* Which column to return */
3428 ){
3429   completion_cursor *pCur = (completion_cursor*)cur;
3430   switch( i ){
3431     case COMPLETION_COLUMN_CANDIDATE: {
3432       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3433       break;
3434     }
3435     case COMPLETION_COLUMN_PREFIX: {
3436       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3437       break;
3438     }
3439     case COMPLETION_COLUMN_WHOLELINE: {
3440       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3441       break;
3442     }
3443     case COMPLETION_COLUMN_PHASE: {
3444       sqlite3_result_int(ctx, pCur->ePhase);
3445       break;
3446     }
3447   }
3448   return SQLITE_OK;
3449 }
3450
3451 /*
3452 ** Return the rowid for the current row.  In this implementation, the
3453 ** rowid is the same as the output value.
3454 */
3455 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3456   completion_cursor *pCur = (completion_cursor*)cur;
3457   *pRowid = pCur->iRowid;
3458   return SQLITE_OK;
3459 }
3460
3461 /*
3462 ** Return TRUE if the cursor has been moved off of the last
3463 ** row of output.
3464 */
3465 static int completionEof(sqlite3_vtab_cursor *cur){
3466   completion_cursor *pCur = (completion_cursor*)cur;
3467   return pCur->ePhase >= COMPLETION_EOF;
3468 }
3469
3470 /*
3471 ** This method is called to "rewind" the completion_cursor object back
3472 ** to the first row of output.  This method is always called at least
3473 ** once prior to any call to completionColumn() or completionRowid() or 
3474 ** completionEof().
3475 */
3476 static int completionFilter(
3477   sqlite3_vtab_cursor *pVtabCursor, 
3478   int idxNum, const char *idxStr,
3479   int argc, sqlite3_value **argv
3480 ){
3481   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3482   int iArg = 0;
3483   (void)(idxStr);   /* Unused parameter */
3484   (void)(argc);     /* Unused parameter */
3485   completionCursorReset(pCur);
3486   if( idxNum & 1 ){
3487     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3488     if( pCur->nPrefix>0 ){
3489       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3490       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3491     }
3492     iArg = 1;
3493   }
3494   if( idxNum & 2 ){
3495     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3496     if( pCur->nLine>0 ){
3497       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3498       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3499     }
3500   }
3501   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3502     int i = pCur->nLine;
3503     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3504       i--;
3505     }
3506     pCur->nPrefix = pCur->nLine - i;
3507     if( pCur->nPrefix>0 ){
3508       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3509       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3510     }
3511   }
3512   pCur->iRowid = 0;
3513   pCur->ePhase = COMPLETION_FIRST_PHASE;
3514   return completionNext(pVtabCursor);
3515 }
3516
3517 /*
3518 ** SQLite will invoke this method one or more times while planning a query
3519 ** that uses the completion virtual table.  This routine needs to create
3520 ** a query plan for each invocation and compute an estimated cost for that
3521 ** plan.
3522 **
3523 ** There are two hidden parameters that act as arguments to the table-valued
3524 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3525 ** is available and bit 1 is set if "wholeline" is available.
3526 */
3527 static int completionBestIndex(
3528   sqlite3_vtab *tab,
3529   sqlite3_index_info *pIdxInfo
3530 ){
3531   int i;                 /* Loop over constraints */
3532   int idxNum = 0;        /* The query plan bitmask */
3533   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3534   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3535   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3536   const struct sqlite3_index_constraint *pConstraint;
3537
3538   (void)(tab);    /* Unused parameter */
3539   pConstraint = pIdxInfo->aConstraint;
3540   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3541     if( pConstraint->usable==0 ) continue;
3542     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3543     switch( pConstraint->iColumn ){
3544       case COMPLETION_COLUMN_PREFIX:
3545         prefixIdx = i;
3546         idxNum |= 1;
3547         break;
3548       case COMPLETION_COLUMN_WHOLELINE:
3549         wholelineIdx = i;
3550         idxNum |= 2;
3551         break;
3552     }
3553   }
3554   if( prefixIdx>=0 ){
3555     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3556     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3557   }
3558   if( wholelineIdx>=0 ){
3559     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3560     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3561   }
3562   pIdxInfo->idxNum = idxNum;
3563   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3564   pIdxInfo->estimatedRows = 500 - 100*nArg;
3565   return SQLITE_OK;
3566 }
3567
3568 /*
3569 ** This following structure defines all the methods for the 
3570 ** completion virtual table.
3571 */
3572 static sqlite3_module completionModule = {
3573   0,                         /* iVersion */
3574   0,                         /* xCreate */
3575   completionConnect,         /* xConnect */
3576   completionBestIndex,       /* xBestIndex */
3577   completionDisconnect,      /* xDisconnect */
3578   0,                         /* xDestroy */
3579   completionOpen,            /* xOpen - open a cursor */
3580   completionClose,           /* xClose - close a cursor */
3581   completionFilter,          /* xFilter - configure scan constraints */
3582   completionNext,            /* xNext - advance a cursor */
3583   completionEof,             /* xEof - check for end of scan */
3584   completionColumn,          /* xColumn - read data */
3585   completionRowid,           /* xRowid - read data */
3586   0,                         /* xUpdate */
3587   0,                         /* xBegin */
3588   0,                         /* xSync */
3589   0,                         /* xCommit */
3590   0,                         /* xRollback */
3591   0,                         /* xFindMethod */
3592   0,                         /* xRename */
3593   0,                         /* xSavepoint */
3594   0,                         /* xRelease */
3595   0,                         /* xRollbackTo */
3596   0                          /* xShadowName */
3597 };
3598
3599 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3600
3601 int sqlite3CompletionVtabInit(sqlite3 *db){
3602   int rc = SQLITE_OK;
3603 #ifndef SQLITE_OMIT_VIRTUALTABLE
3604   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3605 #endif
3606   return rc;
3607 }
3608
3609 #ifdef _WIN32
3610
3611 #endif
3612 int sqlite3_completion_init(
3613   sqlite3 *db, 
3614   char **pzErrMsg, 
3615   const sqlite3_api_routines *pApi
3616 ){
3617   int rc = SQLITE_OK;
3618   SQLITE_EXTENSION_INIT2(pApi);
3619   (void)(pzErrMsg);  /* Unused parameter */
3620 #ifndef SQLITE_OMIT_VIRTUALTABLE
3621   rc = sqlite3CompletionVtabInit(db);
3622 #endif
3623   return rc;
3624 }
3625
3626 /************************* End ../ext/misc/completion.c ********************/
3627 /************************* Begin ../ext/misc/appendvfs.c ******************/
3628 /*
3629 ** 2017-10-20
3630 **
3631 ** The author disclaims copyright to this source code.  In place of
3632 ** a legal notice, here is a blessing:
3633 **
3634 **    May you do good and not evil.
3635 **    May you find forgiveness for yourself and forgive others.
3636 **    May you share freely, never taking more than you give.
3637 **
3638 ******************************************************************************
3639 **
3640 ** This file implements a VFS shim that allows an SQLite database to be
3641 ** appended onto the end of some other file, such as an executable.
3642 **
3643 ** A special record must appear at the end of the file that identifies the
3644 ** file as an appended database and provides the offset to the first page
3645 ** of the exposed content. (Or, it is the length of the content prefix.)
3646 ** For best performance page 1 should be located at a disk page boundary,
3647 ** though that is not required.
3648 **
3649 ** When opening a database using this VFS, the connection might treat
3650 ** the file as an ordinary SQLite database, or it might treat it as a
3651 ** database appended onto some other file.  The decision is made by
3652 ** applying the following rules in order:
3653 **
3654 **  (1)  An empty file is an ordinary database.
3655 **
3656 **  (2)  If the file ends with the appendvfs trailer string
3657 **       "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3658 **
3659 **  (3)  If the file begins with the standard SQLite prefix string
3660 **       "SQLite format 3", that file is an ordinary database.
3661 **
3662 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3663 **       set, then a new database is appended to the already existing file.
3664 **
3665 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3666 **
3667 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3668 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
3669 ** This VFS will not read or write past the 1GiB mark.  This restriction
3670 ** might be lifted in future versions.  For now, if you need a larger
3671 ** database, then keep it in a separate file.
3672 **
3673 ** If the file being opened is a plain database (not an appended one), then
3674 ** this shim is a pass-through into the default underlying VFS. (rule 3)
3675 **/
3676 /* #include "sqlite3ext.h" */
3677 SQLITE_EXTENSION_INIT1
3678 #include <string.h>
3679 #include <assert.h>
3680
3681 /* The append mark at the end of the database is:
3682 **
3683 **     Start-Of-SQLite3-NNNNNNNN
3684 **     123456789 123456789 12345
3685 **
3686 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3687 ** the offset to page 1, and also the length of the prefix content.
3688 */
3689 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3690 #define APND_MARK_PREFIX_SZ  17
3691 #define APND_MARK_FOS_SZ      8
3692 #define APND_MARK_SIZE       (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3693
3694 /*
3695 ** Maximum size of the combined prefix + database + append-mark.  This
3696 ** must be less than 0x40000000 to avoid locking issues on Windows.
3697 */
3698 #define APND_MAX_SIZE  (0x40000000)
3699
3700 /*
3701 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3702 */
3703 #ifndef APND_ROUNDUP
3704 #define APND_ROUNDUP 4096
3705 #endif
3706 #define APND_ALIGN_MASK         ((sqlite3_int64)(APND_ROUNDUP-1))
3707 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3708
3709 /*
3710 ** Forward declaration of objects used by this utility
3711 */
3712 typedef struct sqlite3_vfs ApndVfs;
3713 typedef struct ApndFile ApndFile;
3714
3715 /* Access to a lower-level VFS that (might) implement dynamic loading,
3716 ** access to randomness, etc.
3717 */
3718 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3719 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3720
3721 /* An open appendvfs file
3722 **
3723 ** An instance of this structure describes the appended database file.
3724 ** A separate sqlite3_file object is always appended. The appended
3725 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3726 ** the entire file, including the prefix, the database, and the
3727 ** append-mark.
3728 **
3729 ** The structure of an AppendVFS database is like this:
3730 **
3731 **   +-------------+---------+----------+-------------+
3732 **   | prefix-file | padding | database | append-mark |
3733 **   +-------------+---------+----------+-------------+
3734 **                           ^          ^
3735 **                           |          |
3736 **                         iPgOne      iMark
3737 **
3738 **
3739 ** "prefix file" -  file onto which the database has been appended.
3740 ** "padding"     -  zero or more bytes inserted so that "database"
3741 **                  starts on an APND_ROUNDUP boundary
3742 ** "database"    -  The SQLite database file
3743 ** "append-mark" -  The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3744 **                  the offset from the start of prefix-file to the start
3745 **                  of "database".
3746 **
3747 ** The size of the database is iMark - iPgOne.
3748 **
3749 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3750 ** of iPgOne stored as a big-ending 64-bit integer.
3751 **
3752 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3753 ** Or, iMark is -1 to indicate that it has not yet been written.
3754 */
3755 struct ApndFile {
3756   sqlite3_file base;        /* Subclass.  MUST BE FIRST! */
3757   sqlite3_int64 iPgOne;     /* Offset to the start of the database */
3758   sqlite3_int64 iMark;      /* Offset of the append mark.  -1 if unwritten */
3759   /* Always followed by another sqlite3_file that describes the whole file */
3760 };
3761
3762 /*
3763 ** Methods for ApndFile
3764 */
3765 static int apndClose(sqlite3_file*);
3766 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3767 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3768 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3769 static int apndSync(sqlite3_file*, int flags);
3770 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3771 static int apndLock(sqlite3_file*, int);
3772 static int apndUnlock(sqlite3_file*, int);
3773 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3774 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3775 static int apndSectorSize(sqlite3_file*);
3776 static int apndDeviceCharacteristics(sqlite3_file*);
3777 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3778 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3779 static void apndShmBarrier(sqlite3_file*);
3780 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3781 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3782 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3783
3784 /*
3785 ** Methods for ApndVfs
3786 */
3787 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3788 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3789 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3790 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3791 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3792 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3793 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3794 static void apndDlClose(sqlite3_vfs*, void*);
3795 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3796 static int apndSleep(sqlite3_vfs*, int microseconds);
3797 static int apndCurrentTime(sqlite3_vfs*, double*);
3798 static int apndGetLastError(sqlite3_vfs*, int, char *);
3799 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3800 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3801 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3802 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3803
3804 static sqlite3_vfs apnd_vfs = {
3805   3,                            /* iVersion (set when registered) */
3806   0,                            /* szOsFile (set when registered) */
3807   1024,                         /* mxPathname */
3808   0,                            /* pNext */
3809   "apndvfs",                    /* zName */
3810   0,                            /* pAppData (set when registered) */ 
3811   apndOpen,                     /* xOpen */
3812   apndDelete,                   /* xDelete */
3813   apndAccess,                   /* xAccess */
3814   apndFullPathname,             /* xFullPathname */
3815   apndDlOpen,                   /* xDlOpen */
3816   apndDlError,                  /* xDlError */
3817   apndDlSym,                    /* xDlSym */
3818   apndDlClose,                  /* xDlClose */
3819   apndRandomness,               /* xRandomness */
3820   apndSleep,                    /* xSleep */
3821   apndCurrentTime,              /* xCurrentTime */
3822   apndGetLastError,             /* xGetLastError */
3823   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3824   apndSetSystemCall,            /* xSetSystemCall */
3825   apndGetSystemCall,            /* xGetSystemCall */
3826   apndNextSystemCall            /* xNextSystemCall */
3827 };
3828
3829 static const sqlite3_io_methods apnd_io_methods = {
3830   3,                              /* iVersion */
3831   apndClose,                      /* xClose */
3832   apndRead,                       /* xRead */
3833   apndWrite,                      /* xWrite */
3834   apndTruncate,                   /* xTruncate */
3835   apndSync,                       /* xSync */
3836   apndFileSize,                   /* xFileSize */
3837   apndLock,                       /* xLock */
3838   apndUnlock,                     /* xUnlock */
3839   apndCheckReservedLock,          /* xCheckReservedLock */
3840   apndFileControl,                /* xFileControl */
3841   apndSectorSize,                 /* xSectorSize */
3842   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3843   apndShmMap,                     /* xShmMap */
3844   apndShmLock,                    /* xShmLock */
3845   apndShmBarrier,                 /* xShmBarrier */
3846   apndShmUnmap,                   /* xShmUnmap */
3847   apndFetch,                      /* xFetch */
3848   apndUnfetch                     /* xUnfetch */
3849 };
3850
3851 /*
3852 ** Close an apnd-file.
3853 */
3854 static int apndClose(sqlite3_file *pFile){
3855   pFile = ORIGFILE(pFile);
3856   return pFile->pMethods->xClose(pFile);
3857 }
3858
3859 /*
3860 ** Read data from an apnd-file.
3861 */
3862 static int apndRead(
3863   sqlite3_file *pFile, 
3864   void *zBuf, 
3865   int iAmt, 
3866   sqlite_int64 iOfst
3867 ){
3868   ApndFile *paf = (ApndFile *)pFile;
3869   pFile = ORIGFILE(pFile);
3870   return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3871 }
3872
3873 /*
3874 ** Add the append-mark onto what should become the end of the file.
3875 *  If and only if this succeeds, internal ApndFile.iMark is updated.
3876 *  Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3877 */
3878 static int apndWriteMark(
3879   ApndFile *paf,
3880   sqlite3_file *pFile,
3881   sqlite_int64 iWriteEnd
3882 ){
3883   sqlite_int64 iPgOne = paf->iPgOne;
3884   unsigned char a[APND_MARK_SIZE];
3885   int i = APND_MARK_FOS_SZ;
3886   int rc;
3887   assert(pFile == ORIGFILE(paf));
3888   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3889   while( --i >= 0 ){
3890     a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3891     iPgOne >>= 8;
3892   }
3893   iWriteEnd += paf->iPgOne;
3894   if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3895                   (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3896     paf->iMark = iWriteEnd;
3897   }
3898   return rc;
3899 }
3900
3901 /*
3902 ** Write data to an apnd-file.
3903 */
3904 static int apndWrite(
3905   sqlite3_file *pFile,
3906   const void *zBuf,
3907   int iAmt,
3908   sqlite_int64 iOfst
3909 ){
3910   ApndFile *paf = (ApndFile *)pFile;
3911   sqlite_int64 iWriteEnd = iOfst + iAmt;
3912   if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3913   pFile = ORIGFILE(pFile);
3914   /* If append-mark is absent or will be overwritten, write it. */
3915   if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3916     int rc = apndWriteMark(paf, pFile, iWriteEnd);
3917     if( SQLITE_OK!=rc ) return rc;
3918   }
3919   return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3920 }
3921
3922 /*
3923 ** Truncate an apnd-file.
3924 */
3925 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3926   ApndFile *paf = (ApndFile *)pFile;
3927   pFile = ORIGFILE(pFile);
3928   /* The append mark goes out first so truncate failure does not lose it. */
3929   if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3930   /* Truncate underlying file just past append mark */
3931   return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3932 }
3933
3934 /*
3935 ** Sync an apnd-file.
3936 */
3937 static int apndSync(sqlite3_file *pFile, int flags){
3938   pFile = ORIGFILE(pFile);
3939   return pFile->pMethods->xSync(pFile, flags);
3940 }
3941
3942 /*
3943 ** Return the current file-size of an apnd-file.
3944 ** If the append mark is not yet there, the file-size is 0.
3945 */
3946 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3947   ApndFile *paf = (ApndFile *)pFile;
3948   *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
3949   return SQLITE_OK;
3950 }
3951
3952 /*
3953 ** Lock an apnd-file.
3954 */
3955 static int apndLock(sqlite3_file *pFile, int eLock){
3956   pFile = ORIGFILE(pFile);
3957   return pFile->pMethods->xLock(pFile, eLock);
3958 }
3959
3960 /*
3961 ** Unlock an apnd-file.
3962 */
3963 static int apndUnlock(sqlite3_file *pFile, int eLock){
3964   pFile = ORIGFILE(pFile);
3965   return pFile->pMethods->xUnlock(pFile, eLock);
3966 }
3967
3968 /*
3969 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3970 */
3971 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3972   pFile = ORIGFILE(pFile);
3973   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3974 }
3975
3976 /*
3977 ** File control method. For custom operations on an apnd-file.
3978 */
3979 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3980   ApndFile *paf = (ApndFile *)pFile;
3981   int rc;
3982   pFile = ORIGFILE(pFile);
3983   if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
3984   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3985   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3986     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
3987   }
3988   return rc;
3989 }
3990
3991 /*
3992 ** Return the sector-size in bytes for an apnd-file.
3993 */
3994 static int apndSectorSize(sqlite3_file *pFile){
3995   pFile = ORIGFILE(pFile);
3996   return pFile->pMethods->xSectorSize(pFile);
3997 }
3998
3999 /*
4000 ** Return the device characteristic flags supported by an apnd-file.
4001 */
4002 static int apndDeviceCharacteristics(sqlite3_file *pFile){
4003   pFile = ORIGFILE(pFile);
4004   return pFile->pMethods->xDeviceCharacteristics(pFile);
4005 }
4006
4007 /* Create a shared memory file mapping */
4008 static int apndShmMap(
4009   sqlite3_file *pFile,
4010   int iPg,
4011   int pgsz,
4012   int bExtend,
4013   void volatile **pp
4014 ){
4015   pFile = ORIGFILE(pFile);
4016   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4017 }
4018
4019 /* Perform locking on a shared-memory segment */
4020 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4021   pFile = ORIGFILE(pFile);
4022   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4023 }
4024
4025 /* Memory barrier operation on shared memory */
4026 static void apndShmBarrier(sqlite3_file *pFile){
4027   pFile = ORIGFILE(pFile);
4028   pFile->pMethods->xShmBarrier(pFile);
4029 }
4030
4031 /* Unmap a shared memory segment */
4032 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4033   pFile = ORIGFILE(pFile);
4034   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4035 }
4036
4037 /* Fetch a page of a memory-mapped file */
4038 static int apndFetch(
4039   sqlite3_file *pFile,
4040   sqlite3_int64 iOfst,
4041   int iAmt,
4042   void **pp
4043 ){
4044   ApndFile *p = (ApndFile *)pFile;
4045   if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4046     return SQLITE_IOERR; /* Cannot read what is not yet there. */
4047   }
4048   pFile = ORIGFILE(pFile);
4049   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4050 }
4051
4052 /* Release a memory-mapped page */
4053 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4054   ApndFile *p = (ApndFile *)pFile;
4055   pFile = ORIGFILE(pFile);
4056   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4057 }
4058
4059 /*
4060 ** Try to read the append-mark off the end of a file.  Return the
4061 ** start of the appended database if the append-mark is present.
4062 ** If there is no valid append-mark, return -1;
4063 **
4064 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4065 ** indicates that the appended database contains at least one page.  The
4066 ** start-of-database value must be a multiple of 512.
4067 */
4068 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4069   int rc, i;
4070   sqlite3_int64 iMark;
4071   int msbs = 8 * (APND_MARK_FOS_SZ-1);
4072   unsigned char a[APND_MARK_SIZE];
4073
4074   if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4075   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4076   if( rc ) return -1;
4077   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4078   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4079   for(i=1; i<8; i++){
4080     msbs -= 8;
4081     iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4082   }
4083   if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4084   if( iMark & 0x1ff ) return -1;
4085   return iMark;
4086 }
4087
4088 static const char apvfsSqliteHdr[] = "SQLite format 3";
4089 /*
4090 ** Check to see if the file is an appendvfs SQLite database file.
4091 ** Return true iff it is such. Parameter sz is the file's size.
4092 */
4093 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4094   int rc;
4095   char zHdr[16];
4096   sqlite3_int64 iMark = apndReadMark(sz, pFile);
4097   if( iMark>=0 ){
4098     /* If file has the correct end-marker, the expected odd size, and the
4099     ** SQLite DB type marker where the end-marker puts it, then it
4100     ** is an appendvfs database.
4101     */
4102     rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4103     if( SQLITE_OK==rc
4104      && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4105      && (sz & 0x1ff) == APND_MARK_SIZE
4106      && sz>=512+APND_MARK_SIZE
4107     ){
4108       return 1; /* It's an appendvfs database */
4109     }
4110   }
4111   return 0;
4112 }
4113
4114 /*
4115 ** Check to see if the file is an ordinary SQLite database file.
4116 ** Return true iff so. Parameter sz is the file's size.
4117 */
4118 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4119   char zHdr[16];
4120   if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4121    || (sz & 0x1ff) != 0
4122    || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4123    || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4124   ){
4125     return 0;
4126   }else{
4127     return 1;
4128   }
4129 }
4130
4131 /*
4132 ** Open an apnd file handle.
4133 */
4134 static int apndOpen(
4135   sqlite3_vfs *pApndVfs,
4136   const char *zName,
4137   sqlite3_file *pFile,
4138   int flags,
4139   int *pOutFlags
4140 ){
4141   ApndFile *pApndFile = (ApndFile*)pFile;
4142   sqlite3_file *pBaseFile = ORIGFILE(pFile);
4143   sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4144   int rc;
4145   sqlite3_int64 sz = 0;
4146   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4147     /* The appendvfs is not to be used for transient or temporary databases.
4148     ** Just use the base VFS open to initialize the given file object and
4149     ** open the underlying file. (Appendvfs is then unused for this file.)
4150     */
4151     return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4152   }
4153   memset(pApndFile, 0, sizeof(ApndFile));
4154   pFile->pMethods = &apnd_io_methods;
4155   pApndFile->iMark = -1;    /* Append mark not yet written */
4156
4157   rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4158   if( rc==SQLITE_OK ){
4159     rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4160     if( rc ){
4161       pBaseFile->pMethods->xClose(pBaseFile);
4162     }
4163   }
4164   if( rc ){
4165     pFile->pMethods = 0;
4166     return rc;
4167   }
4168   if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4169     /* The file being opened appears to be just an ordinary DB. Copy
4170     ** the base dispatch-table so this instance mimics the base VFS. 
4171     */
4172     memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4173     return SQLITE_OK;
4174   }
4175   pApndFile->iPgOne = apndReadMark(sz, pFile);
4176   if( pApndFile->iPgOne>=0 ){
4177     pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4178     return SQLITE_OK;
4179   }
4180   if( (flags & SQLITE_OPEN_CREATE)==0 ){
4181     pBaseFile->pMethods->xClose(pBaseFile);
4182     rc = SQLITE_CANTOPEN;
4183     pFile->pMethods = 0;
4184   }else{
4185     /* Round newly added appendvfs location to #define'd page boundary. 
4186     ** Note that nothing has yet been written to the underlying file.
4187     ** The append mark will be written along with first content write.
4188     ** Until then, paf->iMark value indicates it is not yet written.
4189     */
4190     pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4191   }
4192   return rc;
4193 }
4194
4195 /*
4196 ** Delete an apnd file.
4197 ** For an appendvfs, this could mean delete the appendvfs portion,
4198 ** leaving the appendee as it was before it gained an appendvfs.
4199 ** For now, this code deletes the underlying file too.
4200 */
4201 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4202   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4203 }
4204
4205 /*
4206 ** All other VFS methods are pass-thrus.
4207 */
4208 static int apndAccess(
4209   sqlite3_vfs *pVfs, 
4210   const char *zPath, 
4211   int flags, 
4212   int *pResOut
4213 ){
4214   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4215 }
4216 static int apndFullPathname(
4217   sqlite3_vfs *pVfs, 
4218   const char *zPath, 
4219   int nOut, 
4220   char *zOut
4221 ){
4222   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4223 }
4224 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4225   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4226 }
4227 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4228   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4229 }
4230 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4231   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4232 }
4233 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4234   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4235 }
4236 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4237   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4238 }
4239 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4240   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4241 }
4242 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4243   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4244 }
4245 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4246   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4247 }
4248 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4249   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4250 }
4251 static int apndSetSystemCall(
4252   sqlite3_vfs *pVfs,
4253   const char *zName,
4254   sqlite3_syscall_ptr pCall
4255 ){
4256   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4257 }
4258 static sqlite3_syscall_ptr apndGetSystemCall(
4259   sqlite3_vfs *pVfs,
4260   const char *zName
4261 ){
4262   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4263 }
4264 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4265   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4266 }
4267
4268   
4269 #ifdef _WIN32
4270
4271 #endif
4272 /* 
4273 ** This routine is called when the extension is loaded.
4274 ** Register the new VFS.
4275 */
4276 int sqlite3_appendvfs_init(
4277   sqlite3 *db, 
4278   char **pzErrMsg, 
4279   const sqlite3_api_routines *pApi
4280 ){
4281   int rc = SQLITE_OK;
4282   sqlite3_vfs *pOrig;
4283   SQLITE_EXTENSION_INIT2(pApi);
4284   (void)pzErrMsg;
4285   (void)db;
4286   pOrig = sqlite3_vfs_find(0);
4287   if( pOrig==0 ) return SQLITE_ERROR;
4288   apnd_vfs.iVersion = pOrig->iVersion;
4289   apnd_vfs.pAppData = pOrig;
4290   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4291   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4292 #ifdef APPENDVFS_TEST
4293   if( rc==SQLITE_OK ){
4294     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4295   }
4296 #endif
4297   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4298   return rc;
4299 }
4300
4301 /************************* End ../ext/misc/appendvfs.c ********************/
4302 /************************* Begin ../ext/misc/memtrace.c ******************/
4303 /*
4304 ** 2019-01-21
4305 **
4306 ** The author disclaims copyright to this source code.  In place of
4307 ** a legal notice, here is a blessing:
4308 **
4309 **    May you do good and not evil.
4310 **    May you find forgiveness for yourself and forgive others.
4311 **    May you share freely, never taking more than you give.
4312 **
4313 *************************************************************************
4314 **
4315 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4316 ** mechanism to add a tracing layer on top of SQLite.  If this extension
4317 ** is registered prior to sqlite3_initialize(), it will cause all memory
4318 ** allocation activities to be logged on standard output, or to some other
4319 ** FILE specified by the initializer.
4320 **
4321 ** This file needs to be compiled into the application that uses it.
4322 **
4323 ** This extension is used to implement the --memtrace option of the
4324 ** command-line shell.
4325 */
4326 #include <assert.h>
4327 #include <string.h>
4328 #include <stdio.h>
4329
4330 /* The original memory allocation routines */
4331 static sqlite3_mem_methods memtraceBase;
4332 static FILE *memtraceOut;
4333
4334 /* Methods that trace memory allocations */
4335 static void *memtraceMalloc(int n){
4336   if( memtraceOut ){
4337     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 
4338             memtraceBase.xRoundup(n));
4339   }
4340   return memtraceBase.xMalloc(n);
4341 }
4342 static void memtraceFree(void *p){
4343   if( p==0 ) return;
4344   if( memtraceOut ){
4345     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4346   }
4347   memtraceBase.xFree(p);
4348 }
4349 static void *memtraceRealloc(void *p, int n){
4350   if( p==0 ) return memtraceMalloc(n);
4351   if( n==0 ){
4352     memtraceFree(p);
4353     return 0;
4354   }
4355   if( memtraceOut ){
4356     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4357             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4358   }
4359   return memtraceBase.xRealloc(p, n);
4360 }
4361 static int memtraceSize(void *p){
4362   return memtraceBase.xSize(p);
4363 }
4364 static int memtraceRoundup(int n){
4365   return memtraceBase.xRoundup(n);
4366 }
4367 static int memtraceInit(void *p){
4368   return memtraceBase.xInit(p);
4369 }
4370 static void memtraceShutdown(void *p){
4371   memtraceBase.xShutdown(p);
4372 }
4373
4374 /* The substitute memory allocator */
4375 static sqlite3_mem_methods ersaztMethods = {
4376   memtraceMalloc,
4377   memtraceFree,
4378   memtraceRealloc,
4379   memtraceSize,
4380   memtraceRoundup,
4381   memtraceInit,
4382   memtraceShutdown,
4383   0
4384 };
4385
4386 /* Begin tracing memory allocations to out. */
4387 int sqlite3MemTraceActivate(FILE *out){
4388   int rc = SQLITE_OK;
4389   if( memtraceBase.xMalloc==0 ){
4390     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4391     if( rc==SQLITE_OK ){
4392       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4393     }
4394   }
4395   memtraceOut = out;
4396   return rc;
4397 }
4398
4399 /* Deactivate memory tracing */
4400 int sqlite3MemTraceDeactivate(void){
4401   int rc = SQLITE_OK;
4402   if( memtraceBase.xMalloc!=0 ){
4403     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4404     if( rc==SQLITE_OK ){
4405       memset(&memtraceBase, 0, sizeof(memtraceBase));
4406     }
4407   }
4408   memtraceOut = 0;
4409   return rc;
4410 }
4411
4412 /************************* End ../ext/misc/memtrace.c ********************/
4413 /************************* Begin ../ext/misc/uint.c ******************/
4414 /*
4415 ** 2020-04-14
4416 **
4417 ** The author disclaims copyright to this source code.  In place of
4418 ** a legal notice, here is a blessing:
4419 **
4420 **    May you do good and not evil.
4421 **    May you find forgiveness for yourself and forgive others.
4422 **    May you share freely, never taking more than you give.
4423 **
4424 ******************************************************************************
4425 **
4426 ** This SQLite extension implements the UINT collating sequence.
4427 **
4428 ** UINT works like BINARY for text, except that embedded strings
4429 ** of digits compare in numeric order.
4430 **
4431 **     *   Leading zeros are handled properly, in the sense that
4432 **         they do not mess of the maginitude comparison of embedded
4433 **         strings of digits.  "x00123y" is equal to "x123y".
4434 **
4435 **     *   Only unsigned integers are recognized.  Plus and minus
4436 **         signs are ignored.  Decimal points and exponential notation
4437 **         are ignored.
4438 **
4439 **     *   Embedded integers can be of arbitrary length.  Comparison
4440 **         is *not* limited integers that can be expressed as a
4441 **         64-bit machine integer.
4442 */
4443 /* #include "sqlite3ext.h" */
4444 SQLITE_EXTENSION_INIT1
4445 #include <assert.h>
4446 #include <string.h>
4447 #include <ctype.h>
4448
4449 /*
4450 ** Compare text in lexicographic order, except strings of digits
4451 ** compare in numeric order.
4452 */
4453 static int uintCollFunc(
4454   void *notUsed,
4455   int nKey1, const void *pKey1,
4456   int nKey2, const void *pKey2
4457 ){
4458   const unsigned char *zA = (const unsigned char*)pKey1;
4459   const unsigned char *zB = (const unsigned char*)pKey2;
4460   int i=0, j=0, x;
4461   (void)notUsed;
4462   while( i<nKey1 && j<nKey2 ){
4463     x = zA[i] - zB[j];
4464     if( isdigit(zA[i]) ){
4465       int k;
4466       if( !isdigit(zB[j]) ) return x;
4467       while( i<nKey1 && zA[i]=='0' ){ i++; }
4468       while( j<nKey2 && zB[j]=='0' ){ j++; }
4469       k = 0;
4470       while( i+k<nKey1 && isdigit(zA[i+k])
4471              && j+k<nKey2 && isdigit(zB[j+k]) ){
4472         k++;
4473       }
4474       if( i+k<nKey1 && isdigit(zA[i+k]) ){
4475         return +1;
4476       }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4477         return -1;
4478       }else{
4479         x = memcmp(zA+i, zB+j, k);
4480         if( x ) return x;
4481         i += k;
4482         j += k;
4483       }
4484     }else if( x ){
4485       return x;
4486     }else{
4487       i++;
4488       j++;
4489     }
4490   }
4491   return (nKey1 - i) - (nKey2 - j);
4492 }
4493
4494 #ifdef _WIN32
4495
4496 #endif
4497 int sqlite3_uint_init(
4498   sqlite3 *db, 
4499   char **pzErrMsg, 
4500   const sqlite3_api_routines *pApi
4501 ){
4502   SQLITE_EXTENSION_INIT2(pApi);
4503   (void)pzErrMsg;  /* Unused parameter */
4504   return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4505 }
4506
4507 /************************* End ../ext/misc/uint.c ********************/
4508 /************************* Begin ../ext/misc/decimal.c ******************/
4509 /*
4510 ** 2020-06-22
4511 **
4512 ** The author disclaims copyright to this source code.  In place of
4513 ** a legal notice, here is a blessing:
4514 **
4515 **    May you do good and not evil.
4516 **    May you find forgiveness for yourself and forgive others.
4517 **    May you share freely, never taking more than you give.
4518 **
4519 ******************************************************************************
4520 **
4521 ** Routines to implement arbitrary-precision decimal math.
4522 **
4523 ** The focus here is on simplicity and correctness, not performance.
4524 */
4525 /* #include "sqlite3ext.h" */
4526 SQLITE_EXTENSION_INIT1
4527 #include <assert.h>
4528 #include <string.h>
4529 #include <ctype.h>
4530 #include <stdlib.h>
4531
4532 /* Mark a function parameter as unused, to suppress nuisance compiler
4533 ** warnings. */
4534 #ifndef UNUSED_PARAMETER
4535 # define UNUSED_PARAMETER(X)  (void)(X)
4536 #endif
4537
4538
4539 /* A decimal object */
4540 typedef struct Decimal Decimal;
4541 struct Decimal {
4542   char sign;        /* 0 for positive, 1 for negative */
4543   char oom;         /* True if an OOM is encountered */
4544   char isNull;      /* True if holds a NULL rather than a number */
4545   char isInit;      /* True upon initialization */
4546   int nDigit;       /* Total number of digits */
4547   int nFrac;        /* Number of digits to the right of the decimal point */
4548   signed char *a;   /* Array of digits.  Most significant first. */
4549 };
4550
4551 /*
4552 ** Release memory held by a Decimal, but do not free the object itself.
4553 */
4554 static void decimal_clear(Decimal *p){
4555   sqlite3_free(p->a);
4556 }
4557
4558 /*
4559 ** Destroy a Decimal object
4560 */
4561 static void decimal_free(Decimal *p){
4562   if( p ){
4563     decimal_clear(p);
4564     sqlite3_free(p);
4565   }
4566 }
4567
4568 /*
4569 ** Allocate a new Decimal object.  Initialize it to the number given
4570 ** by the input string.
4571 */
4572 static Decimal *decimal_new(
4573   sqlite3_context *pCtx,
4574   sqlite3_value *pIn,
4575   int nAlt,
4576   const unsigned char *zAlt
4577 ){
4578   Decimal *p;
4579   int n, i;
4580   const unsigned char *zIn;
4581   int iExp = 0;
4582   p = sqlite3_malloc( sizeof(*p) );
4583   if( p==0 ) goto new_no_mem;
4584   p->sign = 0;
4585   p->oom = 0;
4586   p->isInit = 1;
4587   p->isNull = 0;
4588   p->nDigit = 0;
4589   p->nFrac = 0;
4590   if( zAlt ){
4591     n = nAlt,
4592     zIn = zAlt;
4593   }else{
4594     if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4595       p->a = 0;
4596       p->isNull = 1;
4597       return p;
4598     }
4599     n = sqlite3_value_bytes(pIn);
4600     zIn = sqlite3_value_text(pIn);
4601   }
4602   p->a = sqlite3_malloc64( n+1 );
4603   if( p->a==0 ) goto new_no_mem;
4604   for(i=0; isspace(zIn[i]); i++){}
4605   if( zIn[i]=='-' ){
4606     p->sign = 1;
4607     i++;
4608   }else if( zIn[i]=='+' ){
4609     i++;
4610   }
4611   while( i<n && zIn[i]=='0' ) i++;
4612   while( i<n ){
4613     char c = zIn[i];
4614     if( c>='0' && c<='9' ){
4615       p->a[p->nDigit++] = c - '0';
4616     }else if( c=='.' ){
4617       p->nFrac = p->nDigit + 1;
4618     }else if( c=='e' || c=='E' ){
4619       int j = i+1;
4620       int neg = 0;
4621       if( j>=n ) break;
4622       if( zIn[j]=='-' ){
4623         neg = 1;
4624         j++;
4625       }else if( zIn[j]=='+' ){
4626         j++;
4627       }
4628       while( j<n && iExp<1000000 ){
4629         if( zIn[j]>='0' && zIn[j]<='9' ){
4630           iExp = iExp*10 + zIn[j] - '0';
4631         }
4632         j++;
4633       }
4634       if( neg ) iExp = -iExp;
4635       break;
4636     }
4637     i++;
4638   }
4639   if( p->nFrac ){
4640     p->nFrac = p->nDigit - (p->nFrac - 1);
4641   }
4642   if( iExp>0 ){
4643     if( p->nFrac>0 ){
4644       if( iExp<=p->nFrac ){
4645         p->nFrac -= iExp;
4646         iExp = 0;
4647       }else{
4648         iExp -= p->nFrac;
4649         p->nFrac = 0;
4650       }
4651     }
4652     if( iExp>0 ){   
4653       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4654       if( p->a==0 ) goto new_no_mem;
4655       memset(p->a+p->nDigit, 0, iExp);
4656       p->nDigit += iExp;
4657     }
4658   }else if( iExp<0 ){
4659     int nExtra;
4660     iExp = -iExp;
4661     nExtra = p->nDigit - p->nFrac - 1;
4662     if( nExtra ){
4663       if( nExtra>=iExp ){
4664         p->nFrac += iExp;
4665         iExp  = 0;
4666       }else{
4667         iExp -= nExtra;
4668         p->nFrac = p->nDigit - 1;
4669       }
4670     }
4671     if( iExp>0 ){
4672       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4673       if( p->a==0 ) goto new_no_mem;
4674       memmove(p->a+iExp, p->a, p->nDigit);
4675       memset(p->a, 0, iExp);
4676       p->nDigit += iExp;
4677       p->nFrac += iExp;
4678     }
4679   }
4680   return p;
4681
4682 new_no_mem:
4683   if( pCtx ) sqlite3_result_error_nomem(pCtx);
4684   sqlite3_free(p);
4685   return 0;
4686 }
4687
4688 /*
4689 ** Make the given Decimal the result.
4690 */
4691 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4692   char *z;
4693   int i, j;
4694   int n;
4695   if( p==0 || p->oom ){
4696     sqlite3_result_error_nomem(pCtx);
4697     return;
4698   }
4699   if( p->isNull ){
4700     sqlite3_result_null(pCtx);
4701     return;
4702   }
4703   z = sqlite3_malloc( p->nDigit+4 );
4704   if( z==0 ){
4705     sqlite3_result_error_nomem(pCtx);
4706     return;
4707   }
4708   i = 0;
4709   if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4710     p->sign = 0;
4711   }
4712   if( p->sign ){
4713     z[0] = '-';
4714     i = 1;
4715   }
4716   n = p->nDigit - p->nFrac;
4717   if( n<=0 ){
4718     z[i++] = '0';
4719   }
4720   j = 0;
4721   while( n>1 && p->a[j]==0 ){
4722     j++;
4723     n--;
4724   }
4725   while( n>0  ){
4726     z[i++] = p->a[j] + '0';
4727     j++;
4728     n--;
4729   }
4730   if( p->nFrac ){
4731     z[i++] = '.';
4732     do{
4733       z[i++] = p->a[j] + '0';
4734       j++;
4735     }while( j<p->nDigit );
4736   }
4737   z[i] = 0;
4738   sqlite3_result_text(pCtx, z, i, sqlite3_free);
4739 }
4740
4741 /*
4742 ** SQL Function:   decimal(X)
4743 **
4744 ** Convert input X into decimal and then back into text
4745 */
4746 static void decimalFunc(
4747   sqlite3_context *context,
4748   int argc,
4749   sqlite3_value **argv
4750 ){
4751   Decimal *p = decimal_new(context, argv[0], 0, 0);
4752   UNUSED_PARAMETER(argc);
4753   decimal_result(context, p);
4754   decimal_free(p);
4755 }
4756
4757 /*
4758 ** Compare to Decimal objects.  Return negative, 0, or positive if the
4759 ** first object is less than, equal to, or greater than the second.
4760 **
4761 ** Preconditions for this routine:
4762 **
4763 **    pA!=0
4764 **    pA->isNull==0
4765 **    pB!=0
4766 **    pB->isNull==0
4767 */
4768 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4769   int nASig, nBSig, rc, n;
4770   if( pA->sign!=pB->sign ){
4771     return pA->sign ? -1 : +1;
4772   }
4773   if( pA->sign ){
4774     const Decimal *pTemp = pA;
4775     pA = pB;
4776     pB = pTemp;
4777   }
4778   nASig = pA->nDigit - pA->nFrac;
4779   nBSig = pB->nDigit - pB->nFrac;
4780   if( nASig!=nBSig ){
4781     return nASig - nBSig;
4782   }
4783   n = pA->nDigit;
4784   if( n>pB->nDigit ) n = pB->nDigit;
4785   rc = memcmp(pA->a, pB->a, n);
4786   if( rc==0 ){
4787     rc = pA->nDigit - pB->nDigit;
4788   }
4789   return rc;
4790 }
4791
4792 /*
4793 ** SQL Function:   decimal_cmp(X, Y)
4794 **
4795 ** Return negative, zero, or positive if X is less then, equal to, or
4796 ** greater than Y.
4797 */
4798 static void decimalCmpFunc(
4799   sqlite3_context *context,
4800   int argc,
4801   sqlite3_value **argv
4802 ){
4803   Decimal *pA = 0, *pB = 0;
4804   int rc;
4805
4806   UNUSED_PARAMETER(argc);
4807   pA = decimal_new(context, argv[0], 0, 0);
4808   if( pA==0 || pA->isNull ) goto cmp_done;
4809   pB = decimal_new(context, argv[1], 0, 0);
4810   if( pB==0 || pB->isNull ) goto cmp_done;
4811   rc = decimal_cmp(pA, pB);
4812   if( rc<0 ) rc = -1;
4813   else if( rc>0 ) rc = +1;
4814   sqlite3_result_int(context, rc);
4815 cmp_done:
4816   decimal_free(pA);
4817   decimal_free(pB);
4818 }
4819
4820 /*
4821 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4822 ** digits to the right of the decimal point.
4823 */
4824 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4825   int nAddSig;
4826   int nAddFrac;
4827   if( p==0 ) return;
4828   nAddFrac = nFrac - p->nFrac;
4829   nAddSig = (nDigit - p->nDigit) - nAddFrac;
4830   if( nAddFrac==0 && nAddSig==0 ) return;
4831   p->a = sqlite3_realloc64(p->a, nDigit+1);
4832   if( p->a==0 ){
4833     p->oom = 1;
4834     return;
4835   }
4836   if( nAddSig ){
4837     memmove(p->a+nAddSig, p->a, p->nDigit);
4838     memset(p->a, 0, nAddSig);
4839     p->nDigit += nAddSig;
4840   }
4841   if( nAddFrac ){
4842     memset(p->a+p->nDigit, 0, nAddFrac);
4843     p->nDigit += nAddFrac;
4844     p->nFrac += nAddFrac;
4845   }
4846 }
4847
4848 /*
4849 ** Add the value pB into pA.
4850 **
4851 ** Both pA and pB might become denormalized by this routine.
4852 */
4853 static void decimal_add(Decimal *pA, Decimal *pB){
4854   int nSig, nFrac, nDigit;
4855   int i, rc;
4856   if( pA==0 ){
4857     return;
4858   }
4859   if( pA->oom || pB==0 || pB->oom ){
4860     pA->oom = 1;
4861     return;
4862   }
4863   if( pA->isNull || pB->isNull ){
4864     pA->isNull = 1;
4865     return;
4866   }
4867   nSig = pA->nDigit - pA->nFrac;
4868   if( nSig && pA->a[0]==0 ) nSig--;
4869   if( nSig<pB->nDigit-pB->nFrac ){
4870     nSig = pB->nDigit - pB->nFrac;
4871   }
4872   nFrac = pA->nFrac;
4873   if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4874   nDigit = nSig + nFrac + 1;
4875   decimal_expand(pA, nDigit, nFrac);
4876   decimal_expand(pB, nDigit, nFrac);
4877   if( pA->oom || pB->oom ){
4878     pA->oom = 1;
4879   }else{
4880     if( pA->sign==pB->sign ){
4881       int carry = 0;
4882       for(i=nDigit-1; i>=0; i--){
4883         int x = pA->a[i] + pB->a[i] + carry;
4884         if( x>=10 ){
4885           carry = 1;
4886           pA->a[i] = x - 10;
4887         }else{
4888           carry = 0;
4889           pA->a[i] = x;
4890         }
4891       }
4892     }else{
4893       signed char *aA, *aB;
4894       int borrow = 0;
4895       rc = memcmp(pA->a, pB->a, nDigit);
4896       if( rc<0 ){
4897         aA = pB->a;
4898         aB = pA->a;
4899         pA->sign = !pA->sign;
4900       }else{
4901         aA = pA->a;
4902         aB = pB->a;
4903       }
4904       for(i=nDigit-1; i>=0; i--){
4905         int x = aA[i] - aB[i] - borrow;
4906         if( x<0 ){
4907           pA->a[i] = x+10;
4908           borrow = 1;
4909         }else{
4910           pA->a[i] = x;
4911           borrow = 0;
4912         }
4913       }
4914     }
4915   }
4916 }
4917
4918 /*
4919 ** Compare text in decimal order.
4920 */
4921 static int decimalCollFunc(
4922   void *notUsed,
4923   int nKey1, const void *pKey1,
4924   int nKey2, const void *pKey2
4925 ){
4926   const unsigned char *zA = (const unsigned char*)pKey1;
4927   const unsigned char *zB = (const unsigned char*)pKey2;
4928   Decimal *pA = decimal_new(0, 0, nKey1, zA);
4929   Decimal *pB = decimal_new(0, 0, nKey2, zB);
4930   int rc;
4931   UNUSED_PARAMETER(notUsed);
4932   if( pA==0 || pB==0 ){
4933     rc = 0;
4934   }else{
4935     rc = decimal_cmp(pA, pB);
4936   }
4937   decimal_free(pA);
4938   decimal_free(pB);
4939   return rc;
4940 }
4941
4942
4943 /*
4944 ** SQL Function:   decimal_add(X, Y)
4945 **                 decimal_sub(X, Y)
4946 **
4947 ** Return the sum or difference of X and Y.
4948 */
4949 static void decimalAddFunc(
4950   sqlite3_context *context,
4951   int argc,
4952   sqlite3_value **argv
4953 ){
4954   Decimal *pA = decimal_new(context, argv[0], 0, 0);
4955   Decimal *pB = decimal_new(context, argv[1], 0, 0);
4956   UNUSED_PARAMETER(argc);
4957   decimal_add(pA, pB);
4958   decimal_result(context, pA);
4959   decimal_free(pA);
4960   decimal_free(pB);
4961 }
4962 static void decimalSubFunc(
4963   sqlite3_context *context,
4964   int argc,
4965   sqlite3_value **argv
4966 ){
4967   Decimal *pA = decimal_new(context, argv[0], 0, 0);
4968   Decimal *pB = decimal_new(context, argv[1], 0, 0);
4969   UNUSED_PARAMETER(argc);
4970   if( pB ){
4971     pB->sign = !pB->sign;
4972     decimal_add(pA, pB);
4973     decimal_result(context, pA);
4974   }
4975   decimal_free(pA);
4976   decimal_free(pB);
4977 }
4978
4979 /* Aggregate funcion:   decimal_sum(X)
4980 **
4981 ** Works like sum() except that it uses decimal arithmetic for unlimited
4982 ** precision.
4983 */
4984 static void decimalSumStep(
4985   sqlite3_context *context,
4986   int argc,
4987   sqlite3_value **argv
4988 ){
4989   Decimal *p;
4990   Decimal *pArg;
4991   UNUSED_PARAMETER(argc);
4992   p = sqlite3_aggregate_context(context, sizeof(*p));
4993   if( p==0 ) return;
4994   if( !p->isInit ){
4995     p->isInit = 1;
4996     p->a = sqlite3_malloc(2);
4997     if( p->a==0 ){
4998       p->oom = 1;
4999     }else{
5000       p->a[0] = 0;
5001     }
5002     p->nDigit = 1;
5003     p->nFrac = 0;
5004   }
5005   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5006   pArg = decimal_new(context, argv[0], 0, 0);
5007   decimal_add(p, pArg);
5008   decimal_free(pArg);
5009 }
5010 static void decimalSumInverse(
5011   sqlite3_context *context,
5012   int argc,
5013   sqlite3_value **argv
5014 ){
5015   Decimal *p;
5016   Decimal *pArg;
5017   UNUSED_PARAMETER(argc);
5018   p = sqlite3_aggregate_context(context, sizeof(*p));
5019   if( p==0 ) return;
5020   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5021   pArg = decimal_new(context, argv[0], 0, 0);
5022   if( pArg ) pArg->sign = !pArg->sign;
5023   decimal_add(p, pArg);
5024   decimal_free(pArg);
5025 }
5026 static void decimalSumValue(sqlite3_context *context){
5027   Decimal *p = sqlite3_aggregate_context(context, 0);
5028   if( p==0 ) return;
5029   decimal_result(context, p);
5030 }
5031 static void decimalSumFinalize(sqlite3_context *context){
5032   Decimal *p = sqlite3_aggregate_context(context, 0);
5033   if( p==0 ) return;
5034   decimal_result(context, p);
5035   decimal_clear(p);
5036 }
5037
5038 /*
5039 ** SQL Function:   decimal_mul(X, Y)
5040 **
5041 ** Return the product of X and Y.
5042 **
5043 ** All significant digits after the decimal point are retained.
5044 ** Trailing zeros after the decimal point are omitted as long as
5045 ** the number of digits after the decimal point is no less than
5046 ** either the number of digits in either input.
5047 */
5048 static void decimalMulFunc(
5049   sqlite3_context *context,
5050   int argc,
5051   sqlite3_value **argv
5052 ){
5053   Decimal *pA = decimal_new(context, argv[0], 0, 0);
5054   Decimal *pB = decimal_new(context, argv[1], 0, 0);
5055   signed char *acc = 0;
5056   int i, j, k;
5057   int minFrac;
5058   UNUSED_PARAMETER(argc);
5059   if( pA==0 || pA->oom || pA->isNull
5060    || pB==0 || pB->oom || pB->isNull 
5061   ){
5062     goto mul_end;
5063   }
5064   acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
5065   if( acc==0 ){
5066     sqlite3_result_error_nomem(context);
5067     goto mul_end;
5068   }
5069   memset(acc, 0, pA->nDigit + pB->nDigit + 2);
5070   minFrac = pA->nFrac;
5071   if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
5072   for(i=pA->nDigit-1; i>=0; i--){
5073     signed char f = pA->a[i];
5074     int carry = 0, x;
5075     for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
5076       x = acc[k] + f*pB->a[j] + carry;
5077       acc[k] = x%10;
5078       carry = x/10;
5079     }
5080     x = acc[k] + carry;
5081     acc[k] = x%10;
5082     acc[k-1] += x/10;
5083   }
5084   sqlite3_free(pA->a);
5085   pA->a = acc;
5086   acc = 0;
5087   pA->nDigit += pB->nDigit + 2;
5088   pA->nFrac += pB->nFrac;
5089   pA->sign ^= pB->sign;
5090   while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
5091     pA->nFrac--;
5092     pA->nDigit--;
5093   }
5094   decimal_result(context, pA);
5095
5096 mul_end:
5097   sqlite3_free(acc);
5098   decimal_free(pA);
5099   decimal_free(pB);
5100 }
5101
5102 #ifdef _WIN32
5103
5104 #endif
5105 int sqlite3_decimal_init(
5106   sqlite3 *db, 
5107   char **pzErrMsg, 
5108   const sqlite3_api_routines *pApi
5109 ){
5110   int rc = SQLITE_OK;
5111   static const struct {
5112     const char *zFuncName;
5113     int nArg;
5114     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5115   } aFunc[] = {
5116     { "decimal",       1,   decimalFunc        },
5117     { "decimal_cmp",   2,   decimalCmpFunc     },
5118     { "decimal_add",   2,   decimalAddFunc     },
5119     { "decimal_sub",   2,   decimalSubFunc     },
5120     { "decimal_mul",   2,   decimalMulFunc     },
5121   };
5122   unsigned int i;
5123   (void)pzErrMsg;  /* Unused parameter */
5124
5125   SQLITE_EXTENSION_INIT2(pApi);
5126
5127   for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5128     rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5129                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5130                    0, aFunc[i].xFunc, 0, 0);
5131   }
5132   if( rc==SQLITE_OK ){
5133     rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5134                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5135                    decimalSumStep, decimalSumFinalize,
5136                    decimalSumValue, decimalSumInverse, 0);
5137   }
5138   if( rc==SQLITE_OK ){
5139     rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5140                                   0, decimalCollFunc);
5141   }
5142   return rc;
5143 }
5144
5145 /************************* End ../ext/misc/decimal.c ********************/
5146 /************************* Begin ../ext/misc/ieee754.c ******************/
5147 /*
5148 ** 2013-04-17
5149 **
5150 ** The author disclaims copyright to this source code.  In place of
5151 ** a legal notice, here is a blessing:
5152 **
5153 **    May you do good and not evil.
5154 **    May you find forgiveness for yourself and forgive others.
5155 **    May you share freely, never taking more than you give.
5156 **
5157 ******************************************************************************
5158 **
5159 ** This SQLite extension implements functions for the exact display
5160 ** and input of IEEE754 Binary64 floating-point numbers.
5161 **
5162 **   ieee754(X)
5163 **   ieee754(Y,Z)
5164 **
5165 ** In the first form, the value X should be a floating-point number.
5166 ** The function will return a string of the form 'ieee754(Y,Z)' where
5167 ** Y and Z are integers such that X==Y*pow(2,Z).
5168 **
5169 ** In the second form, Y and Z are integers which are the mantissa and
5170 ** base-2 exponent of a new floating point number.  The function returns
5171 ** a floating-point value equal to Y*pow(2,Z).
5172 **
5173 ** Examples:
5174 **
5175 **     ieee754(2.0)             ->     'ieee754(2,0)'
5176 **     ieee754(45.25)           ->     'ieee754(181,-2)'
5177 **     ieee754(2, 0)            ->     2.0
5178 **     ieee754(181, -2)         ->     45.25
5179 **
5180 ** Two additional functions break apart the one-argument ieee754()
5181 ** result into separate integer values:
5182 **
5183 **     ieee754_mantissa(45.25)  ->     181
5184 **     ieee754_exponent(45.25)  ->     -2
5185 **
5186 ** These functions convert binary64 numbers into blobs and back again.
5187 **
5188 **     ieee754_from_blob(x'3ff0000000000000')  ->  1.0
5189 **     ieee754_to_blob(1.0)                    ->  x'3ff0000000000000'
5190 **
5191 ** In all single-argument functions, if the argument is an 8-byte blob
5192 ** then that blob is interpreted as a big-endian binary64 value.
5193 **
5194 **
5195 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5196 ** -----------------------------------------------
5197 **
5198 ** This extension in combination with the separate 'decimal' extension
5199 ** can be used to compute the exact decimal representation of binary64
5200 ** values.  To begin, first compute a table of exponent values:
5201 **
5202 **    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5203 **    WITH RECURSIVE c(x,v) AS (
5204 **      VALUES(0,'1')
5205 **      UNION ALL
5206 **      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5207 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5208 **    WITH RECURSIVE c(x,v) AS (
5209 **      VALUES(-1,'0.5')
5210 **      UNION ALL
5211 **      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5212 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5213 **
5214 ** Then, to compute the exact decimal representation of a floating
5215 ** point value (the value 47.49 is used in the example) do:
5216 **
5217 **    WITH c(n) AS (VALUES(47.49))
5218 **          ---------------^^^^^---- Replace with whatever you want
5219 **    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5220 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5221 **
5222 ** Here is a query to show various boundry values for the binary64
5223 ** number format:
5224 **
5225 **    WITH c(name,bin) AS (VALUES
5226 **       ('minimum positive value',        x'0000000000000001'),
5227 **       ('maximum subnormal value',       x'000fffffffffffff'),
5228 **       ('mininum positive nornal value', x'0010000000000000'),
5229 **       ('maximum value',                 x'7fefffffffffffff'))
5230 **    SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5231 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5232 **
5233 */
5234 /* #include "sqlite3ext.h" */
5235 SQLITE_EXTENSION_INIT1
5236 #include <assert.h>
5237 #include <string.h>
5238
5239 /* Mark a function parameter as unused, to suppress nuisance compiler
5240 ** warnings. */
5241 #ifndef UNUSED_PARAMETER
5242 # define UNUSED_PARAMETER(X)  (void)(X)
5243 #endif
5244
5245 /*
5246 ** Implementation of the ieee754() function
5247 */
5248 static void ieee754func(
5249   sqlite3_context *context,
5250   int argc,
5251   sqlite3_value **argv
5252 ){
5253   if( argc==1 ){
5254     sqlite3_int64 m, a;
5255     double r;
5256     int e;
5257     int isNeg;
5258     char zResult[100];
5259     assert( sizeof(m)==sizeof(r) );
5260     if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5261      && sqlite3_value_bytes(argv[0])==sizeof(r)
5262     ){
5263       const unsigned char *x = sqlite3_value_blob(argv[0]);
5264       unsigned int i;
5265       sqlite3_uint64 v = 0;
5266       for(i=0; i<sizeof(r); i++){
5267         v = (v<<8) | x[i];
5268       }
5269       memcpy(&r, &v, sizeof(r));
5270     }else{
5271       r = sqlite3_value_double(argv[0]);
5272     }
5273     if( r<0.0 ){
5274       isNeg = 1;
5275       r = -r;
5276     }else{
5277       isNeg = 0;
5278     }
5279     memcpy(&a,&r,sizeof(a));
5280     if( a==0 ){
5281       e = 0;
5282       m = 0;
5283     }else{
5284       e = a>>52;
5285       m = a & ((((sqlite3_int64)1)<<52)-1);
5286       if( e==0 ){
5287         m <<= 1;
5288       }else{
5289         m |= ((sqlite3_int64)1)<<52;
5290       }
5291       while( e<1075 && m>0 && (m&1)==0 ){
5292         m >>= 1;
5293         e++;
5294       }
5295       if( isNeg ) m = -m;
5296     }
5297     switch( *(int*)sqlite3_user_data(context) ){
5298       case 0:
5299         sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5300                          m, e-1075);
5301         sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5302         break;
5303       case 1:
5304         sqlite3_result_int64(context, m);
5305         break;
5306       case 2:
5307         sqlite3_result_int(context, e-1075);
5308         break;
5309     }
5310   }else{
5311     sqlite3_int64 m, e, a;
5312     double r;
5313     int isNeg = 0;
5314     m = sqlite3_value_int64(argv[0]);
5315     e = sqlite3_value_int64(argv[1]);
5316
5317     /* Limit the range of e.  Ticket 22dea1cfdb9151e4 2021-03-02 */
5318     if( e>10000 ){
5319       e = 10000;
5320     }else if( e<-10000 ){
5321       e = -10000;
5322     }
5323
5324     if( m<0 ){
5325       isNeg = 1;
5326       m = -m;
5327       if( m<0 ) return;
5328     }else if( m==0 && e>-1000 && e<1000 ){
5329       sqlite3_result_double(context, 0.0);
5330       return;
5331     }
5332     while( (m>>32)&0xffe00000 ){
5333       m >>= 1;
5334       e++;
5335     }
5336     while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5337       m <<= 1;
5338       e--;
5339     }
5340     e += 1075;
5341     if( e<=0 ){
5342       /* Subnormal */
5343       m >>= 1-e;
5344       e = 0;
5345     }else if( e>0x7ff ){
5346       e = 0x7ff;
5347     }
5348     a = m & ((((sqlite3_int64)1)<<52)-1);
5349     a |= e<<52;
5350     if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5351     memcpy(&r, &a, sizeof(r));
5352     sqlite3_result_double(context, r);
5353   }
5354 }
5355
5356 /*
5357 ** Functions to convert between blobs and floats.
5358 */
5359 static void ieee754func_from_blob(
5360   sqlite3_context *context,
5361   int argc,
5362   sqlite3_value **argv
5363 ){
5364   UNUSED_PARAMETER(argc);
5365   if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5366    && sqlite3_value_bytes(argv[0])==sizeof(double)
5367   ){
5368     double r;
5369     const unsigned char *x = sqlite3_value_blob(argv[0]);
5370     unsigned int i;
5371     sqlite3_uint64 v = 0;
5372     for(i=0; i<sizeof(r); i++){
5373       v = (v<<8) | x[i];
5374     }
5375     memcpy(&r, &v, sizeof(r));
5376     sqlite3_result_double(context, r);
5377   }
5378 }
5379 static void ieee754func_to_blob(
5380   sqlite3_context *context,
5381   int argc,
5382   sqlite3_value **argv
5383 ){
5384   UNUSED_PARAMETER(argc);
5385   if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5386    || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5387   ){
5388     double r = sqlite3_value_double(argv[0]);
5389     sqlite3_uint64 v;
5390     unsigned char a[sizeof(r)];
5391     unsigned int i;
5392     memcpy(&v, &r, sizeof(r));
5393     for(i=1; i<=sizeof(r); i++){
5394       a[sizeof(r)-i] = v&0xff;
5395       v >>= 8;
5396     }
5397     sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5398   }
5399 }
5400
5401
5402 #ifdef _WIN32
5403
5404 #endif
5405 int sqlite3_ieee_init(
5406   sqlite3 *db, 
5407   char **pzErrMsg, 
5408   const sqlite3_api_routines *pApi
5409 ){
5410   static const struct {
5411     char *zFName;
5412     int nArg;
5413     int iAux;
5414     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5415   } aFunc[] = {
5416     { "ieee754",           1,   0, ieee754func },
5417     { "ieee754",           2,   0, ieee754func },
5418     { "ieee754_mantissa",  1,   1, ieee754func },
5419     { "ieee754_exponent",  1,   2, ieee754func },
5420     { "ieee754_to_blob",   1,   0, ieee754func_to_blob },
5421     { "ieee754_from_blob", 1,   0, ieee754func_from_blob },
5422
5423   };
5424   unsigned int i;
5425   int rc = SQLITE_OK;
5426   SQLITE_EXTENSION_INIT2(pApi);
5427   (void)pzErrMsg;  /* Unused parameter */
5428   for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5429     rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,    
5430                                SQLITE_UTF8|SQLITE_INNOCUOUS,
5431                                (void*)&aFunc[i].iAux,
5432                                aFunc[i].xFunc, 0, 0);
5433   }
5434   return rc;
5435 }
5436
5437 /************************* End ../ext/misc/ieee754.c ********************/
5438 /************************* Begin ../ext/misc/series.c ******************/
5439 /*
5440 ** 2015-08-18
5441 **
5442 ** The author disclaims copyright to this source code.  In place of
5443 ** a legal notice, here is a blessing:
5444 **
5445 **    May you do good and not evil.
5446 **    May you find forgiveness for yourself and forgive others.
5447 **    May you share freely, never taking more than you give.
5448 **
5449 *************************************************************************
5450 **
5451 ** This file demonstrates how to create a table-valued-function using
5452 ** a virtual table.  This demo implements the generate_series() function
5453 ** which gives similar results to the eponymous function in PostgreSQL.
5454 ** Examples:
5455 **
5456 **      SELECT * FROM generate_series(0,100,5);
5457 **
5458 ** The query above returns integers from 0 through 100 counting by steps
5459 ** of 5.
5460 **
5461 **      SELECT * FROM generate_series(0,100);
5462 **
5463 ** Integers from 0 through 100 with a step size of 1.
5464 **
5465 **      SELECT * FROM generate_series(20) LIMIT 10;
5466 **
5467 ** Integers 20 through 29.
5468 **
5469 ** HOW IT WORKS
5470 **
5471 ** The generate_series "function" is really a virtual table with the
5472 ** following schema:
5473 **
5474 **     CREATE TABLE generate_series(
5475 **       value,
5476 **       start HIDDEN,
5477 **       stop HIDDEN,
5478 **       step HIDDEN
5479 **     );
5480 **
5481 ** Function arguments in queries against this virtual table are translated
5482 ** into equality constraints against successive hidden columns.  In other
5483 ** words, the following pairs of queries are equivalent to each other:
5484 **
5485 **    SELECT * FROM generate_series(0,100,5);
5486 **    SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5487 **
5488 **    SELECT * FROM generate_series(0,100);
5489 **    SELECT * FROM generate_series WHERE start=0 AND stop=100;
5490 **
5491 **    SELECT * FROM generate_series(20) LIMIT 10;
5492 **    SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5493 **
5494 ** The generate_series virtual table implementation leaves the xCreate method
5495 ** set to NULL.  This means that it is not possible to do a CREATE VIRTUAL
5496 ** TABLE command with "generate_series" as the USING argument.  Instead, there
5497 ** is a single generate_series virtual table that is always available without
5498 ** having to be created first.
5499 **
5500 ** The xBestIndex method looks for equality constraints against the hidden
5501 ** start, stop, and step columns, and if present, it uses those constraints
5502 ** to bound the sequence of generated values.  If the equality constraints
5503 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5504 ** xBestIndex returns a small cost when both start and stop are available,
5505 ** and a very large cost if either start or stop are unavailable.  This
5506 ** encourages the query planner to order joins such that the bounds of the
5507 ** series are well-defined.
5508 */
5509 /* #include "sqlite3ext.h" */
5510 SQLITE_EXTENSION_INIT1
5511 #include <assert.h>
5512 #include <string.h>
5513
5514 #ifndef SQLITE_OMIT_VIRTUALTABLE
5515
5516
5517 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5518 ** serve as the underlying representation of a cursor that scans
5519 ** over rows of the result
5520 */
5521 typedef struct series_cursor series_cursor;
5522 struct series_cursor {
5523   sqlite3_vtab_cursor base;  /* Base class - must be first */
5524   int isDesc;                /* True to count down rather than up */
5525   sqlite3_int64 iRowid;      /* The rowid */
5526   sqlite3_int64 iValue;      /* Current value ("value") */
5527   sqlite3_int64 mnValue;     /* Mimimum value ("start") */
5528   sqlite3_int64 mxValue;     /* Maximum value ("stop") */
5529   sqlite3_int64 iStep;       /* Increment ("step") */
5530 };
5531
5532 /*
5533 ** The seriesConnect() method is invoked to create a new
5534 ** series_vtab that describes the generate_series virtual table.
5535 **
5536 ** Think of this routine as the constructor for series_vtab objects.
5537 **
5538 ** All this routine needs to do is:
5539 **
5540 **    (1) Allocate the series_vtab object and initialize all fields.
5541 **
5542 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5543 **        result set of queries against generate_series will look like.
5544 */
5545 static int seriesConnect(
5546   sqlite3 *db,
5547   void *pUnused,
5548   int argcUnused, const char *const*argvUnused,
5549   sqlite3_vtab **ppVtab,
5550   char **pzErrUnused
5551 ){
5552   sqlite3_vtab *pNew;
5553   int rc;
5554
5555 /* Column numbers */
5556 #define SERIES_COLUMN_VALUE 0
5557 #define SERIES_COLUMN_START 1
5558 #define SERIES_COLUMN_STOP  2
5559 #define SERIES_COLUMN_STEP  3
5560
5561   (void)pUnused;
5562   (void)argcUnused;
5563   (void)argvUnused;
5564   (void)pzErrUnused;
5565   rc = sqlite3_declare_vtab(db,
5566      "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5567   if( rc==SQLITE_OK ){
5568     pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5569     if( pNew==0 ) return SQLITE_NOMEM;
5570     memset(pNew, 0, sizeof(*pNew));
5571     sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5572   }
5573   return rc;
5574 }
5575
5576 /*
5577 ** This method is the destructor for series_cursor objects.
5578 */
5579 static int seriesDisconnect(sqlite3_vtab *pVtab){
5580   sqlite3_free(pVtab);
5581   return SQLITE_OK;
5582 }
5583
5584 /*
5585 ** Constructor for a new series_cursor object.
5586 */
5587 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5588   series_cursor *pCur;
5589   (void)pUnused;
5590   pCur = sqlite3_malloc( sizeof(*pCur) );
5591   if( pCur==0 ) return SQLITE_NOMEM;
5592   memset(pCur, 0, sizeof(*pCur));
5593   *ppCursor = &pCur->base;
5594   return SQLITE_OK;
5595 }
5596
5597 /*
5598 ** Destructor for a series_cursor.
5599 */
5600 static int seriesClose(sqlite3_vtab_cursor *cur){
5601   sqlite3_free(cur);
5602   return SQLITE_OK;
5603 }
5604
5605
5606 /*
5607 ** Advance a series_cursor to its next row of output.
5608 */
5609 static int seriesNext(sqlite3_vtab_cursor *cur){
5610   series_cursor *pCur = (series_cursor*)cur;
5611   if( pCur->isDesc ){
5612     pCur->iValue -= pCur->iStep;
5613   }else{
5614     pCur->iValue += pCur->iStep;
5615   }
5616   pCur->iRowid++;
5617   return SQLITE_OK;
5618 }
5619
5620 /*
5621 ** Return values of columns for the row at which the series_cursor
5622 ** is currently pointing.
5623 */
5624 static int seriesColumn(
5625   sqlite3_vtab_cursor *cur,   /* The cursor */
5626   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5627   int i                       /* Which column to return */
5628 ){
5629   series_cursor *pCur = (series_cursor*)cur;
5630   sqlite3_int64 x = 0;
5631   switch( i ){
5632     case SERIES_COLUMN_START:  x = pCur->mnValue; break;
5633     case SERIES_COLUMN_STOP:   x = pCur->mxValue; break;
5634     case SERIES_COLUMN_STEP:   x = pCur->iStep;   break;
5635     default:                   x = pCur->iValue;  break;
5636   }
5637   sqlite3_result_int64(ctx, x);
5638   return SQLITE_OK;
5639 }
5640
5641 /*
5642 ** Return the rowid for the current row. In this implementation, the
5643 ** first row returned is assigned rowid value 1, and each subsequent
5644 ** row a value 1 more than that of the previous.
5645 */
5646 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5647   series_cursor *pCur = (series_cursor*)cur;
5648   *pRowid = pCur->iRowid;
5649   return SQLITE_OK;
5650 }
5651
5652 /*
5653 ** Return TRUE if the cursor has been moved off of the last
5654 ** row of output.
5655 */
5656 static int seriesEof(sqlite3_vtab_cursor *cur){
5657   series_cursor *pCur = (series_cursor*)cur;
5658   if( pCur->isDesc ){
5659     return pCur->iValue < pCur->mnValue;
5660   }else{
5661     return pCur->iValue > pCur->mxValue;
5662   }
5663 }
5664
5665 /* True to cause run-time checking of the start=, stop=, and/or step= 
5666 ** parameters.  The only reason to do this is for testing the
5667 ** constraint checking logic for virtual tables in the SQLite core.
5668 */
5669 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5670 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5671 #endif
5672
5673 /*
5674 ** This method is called to "rewind" the series_cursor object back
5675 ** to the first row of output.  This method is always called at least
5676 ** once prior to any call to seriesColumn() or seriesRowid() or 
5677 ** seriesEof().
5678 **
5679 ** The query plan selected by seriesBestIndex is passed in the idxNum
5680 ** parameter.  (idxStr is not used in this implementation.)  idxNum
5681 ** is a bitmask showing which constraints are available:
5682 **
5683 **    1:    start=VALUE
5684 **    2:    stop=VALUE
5685 **    4:    step=VALUE
5686 **
5687 ** Also, if bit 8 is set, that means that the series should be output
5688 ** in descending order rather than in ascending order.  If bit 16 is
5689 ** set, then output must appear in ascending order.
5690 **
5691 ** This routine should initialize the cursor and position it so that it
5692 ** is pointing at the first row, or pointing off the end of the table
5693 ** (so that seriesEof() will return true) if the table is empty.
5694 */
5695 static int seriesFilter(
5696   sqlite3_vtab_cursor *pVtabCursor, 
5697   int idxNum, const char *idxStrUnused,
5698   int argc, sqlite3_value **argv
5699 ){
5700   series_cursor *pCur = (series_cursor *)pVtabCursor;
5701   int i = 0;
5702   (void)idxStrUnused;
5703   if( idxNum & 1 ){
5704     pCur->mnValue = sqlite3_value_int64(argv[i++]);
5705   }else{
5706     pCur->mnValue = 0;
5707   }
5708   if( idxNum & 2 ){
5709     pCur->mxValue = sqlite3_value_int64(argv[i++]);
5710   }else{
5711     pCur->mxValue = 0xffffffff;
5712   }
5713   if( idxNum & 4 ){
5714     pCur->iStep = sqlite3_value_int64(argv[i++]);
5715     if( pCur->iStep==0 ){
5716       pCur->iStep = 1;
5717     }else if( pCur->iStep<0 ){
5718       pCur->iStep = -pCur->iStep;
5719       if( (idxNum & 16)==0 ) idxNum |= 8;
5720     }
5721   }else{
5722     pCur->iStep = 1;
5723   }
5724   for(i=0; i<argc; i++){
5725     if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5726       /* If any of the constraints have a NULL value, then return no rows.
5727       ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5728       pCur->mnValue = 1;
5729       pCur->mxValue = 0;
5730       break;
5731     }
5732   }
5733   if( idxNum & 8 ){
5734     pCur->isDesc = 1;
5735     pCur->iValue = pCur->mxValue;
5736     if( pCur->iStep>0 ){
5737       pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5738     }
5739   }else{
5740     pCur->isDesc = 0;
5741     pCur->iValue = pCur->mnValue;
5742   }
5743   pCur->iRowid = 1;
5744   return SQLITE_OK;
5745 }
5746
5747 /*
5748 ** SQLite will invoke this method one or more times while planning a query
5749 ** that uses the generate_series virtual table.  This routine needs to create
5750 ** a query plan for each invocation and compute an estimated cost for that
5751 ** plan.
5752 **
5753 ** In this implementation idxNum is used to represent the
5754 ** query plan.  idxStr is unused.
5755 **
5756 ** The query plan is represented by bits in idxNum:
5757 **
5758 **  (1)  start = $value  -- constraint exists
5759 **  (2)  stop = $value   -- constraint exists
5760 **  (4)  step = $value   -- constraint exists
5761 **  (8)  output in descending order
5762 */
5763 static int seriesBestIndex(
5764   sqlite3_vtab *tabUnused,
5765   sqlite3_index_info *pIdxInfo
5766 ){
5767   int i, j;              /* Loop over constraints */
5768   int idxNum = 0;        /* The query plan bitmask */
5769   int unusableMask = 0;  /* Mask of unusable constraints */
5770   int nArg = 0;          /* Number of arguments that seriesFilter() expects */
5771   int aIdx[3];           /* Constraints on start, stop, and step */
5772   const struct sqlite3_index_constraint *pConstraint;
5773
5774   /* This implementation assumes that the start, stop, and step columns
5775   ** are the last three columns in the virtual table. */
5776   assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5777   assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5778   (void)tabUnused;
5779   aIdx[0] = aIdx[1] = aIdx[2] = -1;
5780   pConstraint = pIdxInfo->aConstraint;
5781   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5782     int iCol;    /* 0 for start, 1 for stop, 2 for step */
5783     int iMask;   /* bitmask for those column */
5784     if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5785     iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5786     assert( iCol>=0 && iCol<=2 );
5787     iMask = 1 << iCol;
5788     if( pConstraint->usable==0 ){
5789       unusableMask |=  iMask;
5790       continue;
5791     }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5792       idxNum |= iMask;
5793       aIdx[iCol] = i;
5794     }
5795   }
5796   for(i=0; i<3; i++){
5797     if( (j = aIdx[i])>=0 ){
5798       pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5799       pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5800     }
5801   }
5802   if( (unusableMask & ~idxNum)!=0 ){
5803     /* The start, stop, and step columns are inputs.  Therefore if there
5804     ** are unusable constraints on any of start, stop, or step then
5805     ** this plan is unusable */
5806     return SQLITE_CONSTRAINT;
5807   }
5808   if( (idxNum & 3)==3 ){
5809     /* Both start= and stop= boundaries are available.  This is the 
5810     ** the preferred case */
5811     pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5812     pIdxInfo->estimatedRows = 1000;
5813     if( pIdxInfo->nOrderBy==1 ){
5814       if( pIdxInfo->aOrderBy[0].desc ){
5815         idxNum |= 8;
5816       }else{
5817         idxNum |= 16;
5818       }
5819       pIdxInfo->orderByConsumed = 1;
5820     }
5821   }else{
5822     /* If either boundary is missing, we have to generate a huge span
5823     ** of numbers.  Make this case very expensive so that the query
5824     ** planner will work hard to avoid it. */
5825     pIdxInfo->estimatedRows = 2147483647;
5826   }
5827   pIdxInfo->idxNum = idxNum;
5828   return SQLITE_OK;
5829 }
5830
5831 /*
5832 ** This following structure defines all the methods for the 
5833 ** generate_series virtual table.
5834 */
5835 static sqlite3_module seriesModule = {
5836   0,                         /* iVersion */
5837   0,                         /* xCreate */
5838   seriesConnect,             /* xConnect */
5839   seriesBestIndex,           /* xBestIndex */
5840   seriesDisconnect,          /* xDisconnect */
5841   0,                         /* xDestroy */
5842   seriesOpen,                /* xOpen - open a cursor */
5843   seriesClose,               /* xClose - close a cursor */
5844   seriesFilter,              /* xFilter - configure scan constraints */
5845   seriesNext,                /* xNext - advance a cursor */
5846   seriesEof,                 /* xEof - check for end of scan */
5847   seriesColumn,              /* xColumn - read data */
5848   seriesRowid,               /* xRowid - read data */
5849   0,                         /* xUpdate */
5850   0,                         /* xBegin */
5851   0,                         /* xSync */
5852   0,                         /* xCommit */
5853   0,                         /* xRollback */
5854   0,                         /* xFindMethod */
5855   0,                         /* xRename */
5856   0,                         /* xSavepoint */
5857   0,                         /* xRelease */
5858   0,                         /* xRollbackTo */
5859   0                          /* xShadowName */
5860 };
5861
5862 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5863
5864 #ifdef _WIN32
5865
5866 #endif
5867 int sqlite3_series_init(
5868   sqlite3 *db, 
5869   char **pzErrMsg, 
5870   const sqlite3_api_routines *pApi
5871 ){
5872   int rc = SQLITE_OK;
5873   SQLITE_EXTENSION_INIT2(pApi);
5874 #ifndef SQLITE_OMIT_VIRTUALTABLE
5875   if( sqlite3_libversion_number()<3008012 ){
5876     *pzErrMsg = sqlite3_mprintf(
5877         "generate_series() requires SQLite 3.8.12 or later");
5878     return SQLITE_ERROR;
5879   }
5880   rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5881 #endif
5882   return rc;
5883 }
5884
5885 /************************* End ../ext/misc/series.c ********************/
5886 /************************* Begin ../ext/misc/regexp.c ******************/
5887 /*
5888 ** 2012-11-13
5889 **
5890 ** The author disclaims copyright to this source code.  In place of
5891 ** a legal notice, here is a blessing:
5892 **
5893 **    May you do good and not evil.
5894 **    May you find forgiveness for yourself and forgive others.
5895 **    May you share freely, never taking more than you give.
5896 **
5897 ******************************************************************************
5898 **
5899 ** The code in this file implements a compact but reasonably
5900 ** efficient regular-expression matcher for posix extended regular
5901 ** expressions against UTF8 text.
5902 **
5903 ** This file is an SQLite extension.  It registers a single function
5904 ** named "regexp(A,B)" where A is the regular expression and B is the
5905 ** string to be matched.  By registering this function, SQLite will also
5906 ** then implement the "B regexp A" operator.  Note that with the function
5907 ** the regular expression comes first, but with the operator it comes
5908 ** second.
5909 **
5910 **  The following regular expression syntax is supported:
5911 **
5912 **     X*      zero or more occurrences of X
5913 **     X+      one or more occurrences of X
5914 **     X?      zero or one occurrences of X
5915 **     X{p,q}  between p and q occurrences of X
5916 **     (X)     match X
5917 **     X|Y     X or Y
5918 **     ^X      X occurring at the beginning of the string
5919 **     X$      X occurring at the end of the string
5920 **     .       Match any single character
5921 **     \c      Character c where c is one of \{}()[]|*+?.
5922 **     \c      C-language escapes for c in afnrtv.  ex: \t or \n
5923 **     \uXXXX  Where XXXX is exactly 4 hex digits, unicode value XXXX
5924 **     \xXX    Where XX is exactly 2 hex digits, unicode value XX
5925 **     [abc]   Any single character from the set abc
5926 **     [^abc]  Any single character not in the set abc
5927 **     [a-z]   Any single character in the range a-z
5928 **     [^a-z]  Any single character not in the range a-z
5929 **     \b      Word boundary
5930 **     \w      Word character.  [A-Za-z0-9_]
5931 **     \W      Non-word character
5932 **     \d      Digit
5933 **     \D      Non-digit
5934 **     \s      Whitespace character
5935 **     \S      Non-whitespace character
5936 **
5937 ** A nondeterministic finite automaton (NFA) is used for matching, so the
5938 ** performance is bounded by O(N*M) where N is the size of the regular
5939 ** expression and M is the size of the input string.  The matcher never
5940 ** exhibits exponential behavior.  Note that the X{p,q} operator expands
5941 ** to p copies of X following by q-p copies of X? and that the size of the
5942 ** regular expression in the O(N*M) performance bound is computed after
5943 ** this expansion.
5944 */
5945 #include <string.h>
5946 #include <stdlib.h>
5947 /* #include "sqlite3ext.h" */
5948 SQLITE_EXTENSION_INIT1
5949
5950 /*
5951 ** The following #defines change the names of some functions implemented in
5952 ** this file to prevent name collisions with C-library functions of the
5953 ** same name.
5954 */
5955 #define re_match   sqlite3re_match
5956 #define re_compile sqlite3re_compile
5957 #define re_free    sqlite3re_free
5958
5959 /* The end-of-input character */
5960 #define RE_EOF            0    /* End of input */
5961
5962 /* The NFA is implemented as sequence of opcodes taken from the following
5963 ** set.  Each opcode has a single integer argument.
5964 */
5965 #define RE_OP_MATCH       1    /* Match the one character in the argument */
5966 #define RE_OP_ANY         2    /* Match any one character.  (Implements ".") */
5967 #define RE_OP_ANYSTAR     3    /* Special optimized version of .* */
5968 #define RE_OP_FORK        4    /* Continue to both next and opcode at iArg */
5969 #define RE_OP_GOTO        5    /* Jump to opcode at iArg */
5970 #define RE_OP_ACCEPT      6    /* Halt and indicate a successful match */
5971 #define RE_OP_CC_INC      7    /* Beginning of a [...] character class */
5972 #define RE_OP_CC_EXC      8    /* Beginning of a [^...] character class */
5973 #define RE_OP_CC_VALUE    9    /* Single value in a character class */
5974 #define RE_OP_CC_RANGE   10    /* Range of values in a character class */
5975 #define RE_OP_WORD       11    /* Perl word character [A-Za-z0-9_] */
5976 #define RE_OP_NOTWORD    12    /* Not a perl word character */
5977 #define RE_OP_DIGIT      13    /* digit:  [0-9] */
5978 #define RE_OP_NOTDIGIT   14    /* Not a digit */
5979 #define RE_OP_SPACE      15    /* space:  [ \t\n\r\v\f] */
5980 #define RE_OP_NOTSPACE   16    /* Not a digit */
5981 #define RE_OP_BOUNDARY   17    /* Boundary between word and non-word */
5982
5983 /* Each opcode is a "state" in the NFA */
5984 typedef unsigned short ReStateNumber;
5985
5986 /* Because this is an NFA and not a DFA, multiple states can be active at
5987 ** once.  An instance of the following object records all active states in
5988 ** the NFA.  The implementation is optimized for the common case where the
5989 ** number of actives states is small.
5990 */
5991 typedef struct ReStateSet {
5992   unsigned nState;            /* Number of current states */
5993   ReStateNumber *aState;      /* Current states */
5994 } ReStateSet;
5995
5996 /* An input string read one character at a time.
5997 */
5998 typedef struct ReInput ReInput;
5999 struct ReInput {
6000   const unsigned char *z;  /* All text */
6001   int i;                   /* Next byte to read */
6002   int mx;                  /* EOF when i>=mx */
6003 };
6004
6005 /* A compiled NFA (or an NFA that is in the process of being compiled) is
6006 ** an instance of the following object.
6007 */
6008 typedef struct ReCompiled ReCompiled;
6009 struct ReCompiled {
6010   ReInput sIn;                /* Regular expression text */
6011   const char *zErr;           /* Error message to return */
6012   char *aOp;                  /* Operators for the virtual machine */
6013   int *aArg;                  /* Arguments to each operator */
6014   unsigned (*xNextChar)(ReInput*);  /* Next character function */
6015   unsigned char zInit[12];    /* Initial text to match */
6016   int nInit;                  /* Number of characters in zInit */
6017   unsigned nState;            /* Number of entries in aOp[] and aArg[] */
6018   unsigned nAlloc;            /* Slots allocated for aOp[] and aArg[] */
6019 };
6020
6021 /* Add a state to the given state set if it is not already there */
6022 static void re_add_state(ReStateSet *pSet, int newState){
6023   unsigned i;
6024   for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
6025   pSet->aState[pSet->nState++] = (ReStateNumber)newState;
6026 }
6027
6028 /* Extract the next unicode character from *pzIn and return it.  Advance
6029 ** *pzIn to the first byte past the end of the character returned.  To
6030 ** be clear:  this routine converts utf8 to unicode.  This routine is 
6031 ** optimized for the common case where the next character is a single byte.
6032 */
6033 static unsigned re_next_char(ReInput *p){
6034   unsigned c;
6035   if( p->i>=p->mx ) return 0;
6036   c = p->z[p->i++];
6037   if( c>=0x80 ){
6038     if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
6039       c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
6040       if( c<0x80 ) c = 0xfffd;
6041     }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
6042            && (p->z[p->i+1]&0xc0)==0x80 ){
6043       c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
6044       p->i += 2;
6045       if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
6046     }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
6047            && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
6048       c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
6049                        | (p->z[p->i+2]&0x3f);
6050       p->i += 3;
6051       if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
6052     }else{
6053       c = 0xfffd;
6054     }
6055   }
6056   return c;
6057 }
6058 static unsigned re_next_char_nocase(ReInput *p){
6059   unsigned c = re_next_char(p);
6060   if( c>='A' && c<='Z' ) c += 'a' - 'A';
6061   return c;
6062 }
6063
6064 /* Return true if c is a perl "word" character:  [A-Za-z0-9_] */
6065 static int re_word_char(int c){
6066   return (c>='0' && c<='9') || (c>='a' && c<='z')
6067       || (c>='A' && c<='Z') || c=='_';
6068 }
6069
6070 /* Return true if c is a "digit" character:  [0-9] */
6071 static int re_digit_char(int c){
6072   return (c>='0' && c<='9');
6073 }
6074
6075 /* Return true if c is a perl "space" character:  [ \t\r\n\v\f] */
6076 static int re_space_char(int c){
6077   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
6078 }
6079
6080 /* Run a compiled regular expression on the zero-terminated input
6081 ** string zIn[].  Return true on a match and false if there is no match.
6082 */
6083 static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
6084   ReStateSet aStateSet[2], *pThis, *pNext;
6085   ReStateNumber aSpace[100];
6086   ReStateNumber *pToFree;
6087   unsigned int i = 0;
6088   unsigned int iSwap = 0;
6089   int c = RE_EOF+1;
6090   int cPrev = 0;
6091   int rc = 0;
6092   ReInput in;
6093
6094   in.z = zIn;
6095   in.i = 0;
6096   in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
6097
6098   /* Look for the initial prefix match, if there is one. */
6099   if( pRe->nInit ){
6100     unsigned char x = pRe->zInit[0];
6101     while( in.i+pRe->nInit<=in.mx 
6102      && (zIn[in.i]!=x ||
6103          strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
6104     ){
6105       in.i++;
6106     }
6107     if( in.i+pRe->nInit>in.mx ) return 0;
6108   }
6109
6110   if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
6111     pToFree = 0;
6112     aStateSet[0].aState = aSpace;
6113   }else{
6114     pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
6115     if( pToFree==0 ) return -1;
6116     aStateSet[0].aState = pToFree;
6117   }
6118   aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
6119   pNext = &aStateSet[1];
6120   pNext->nState = 0;
6121   re_add_state(pNext, 0);
6122   while( c!=RE_EOF && pNext->nState>0 ){
6123     cPrev = c;
6124     c = pRe->xNextChar(&in);
6125     pThis = pNext;
6126     pNext = &aStateSet[iSwap];
6127     iSwap = 1 - iSwap;
6128     pNext->nState = 0;
6129     for(i=0; i<pThis->nState; i++){
6130       int x = pThis->aState[i];
6131       switch( pRe->aOp[x] ){
6132         case RE_OP_MATCH: {
6133           if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
6134           break;
6135         }
6136         case RE_OP_ANY: {
6137           if( c!=0 ) re_add_state(pNext, x+1);
6138           break;
6139         }
6140         case RE_OP_WORD: {
6141           if( re_word_char(c) ) re_add_state(pNext, x+1);
6142           break;
6143         }
6144         case RE_OP_NOTWORD: {
6145           if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
6146           break;
6147         }
6148         case RE_OP_DIGIT: {
6149           if( re_digit_char(c) ) re_add_state(pNext, x+1);
6150           break;
6151         }
6152         case RE_OP_NOTDIGIT: {
6153           if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
6154           break;
6155         }
6156         case RE_OP_SPACE: {
6157           if( re_space_char(c) ) re_add_state(pNext, x+1);
6158           break;
6159         }
6160         case RE_OP_NOTSPACE: {
6161           if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
6162           break;
6163         }
6164         case RE_OP_BOUNDARY: {
6165           if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
6166           break;
6167         }
6168         case RE_OP_ANYSTAR: {
6169           re_add_state(pNext, x);
6170           re_add_state(pThis, x+1);
6171           break;
6172         }
6173         case RE_OP_FORK: {
6174           re_add_state(pThis, x+pRe->aArg[x]);
6175           re_add_state(pThis, x+1);
6176           break;
6177         }
6178         case RE_OP_GOTO: {
6179           re_add_state(pThis, x+pRe->aArg[x]);
6180           break;
6181         }
6182         case RE_OP_ACCEPT: {
6183           rc = 1;
6184           goto re_match_end;
6185         }
6186         case RE_OP_CC_EXC: {
6187           if( c==0 ) break;
6188           /* fall-through */
6189         }
6190         case RE_OP_CC_INC: {
6191           int j = 1;
6192           int n = pRe->aArg[x];
6193           int hit = 0;
6194           for(j=1; j>0 && j<n; j++){
6195             if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
6196               if( pRe->aArg[x+j]==c ){
6197                 hit = 1;
6198                 j = -1;
6199               }
6200             }else{
6201               if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
6202                 hit = 1;
6203                 j = -1;
6204               }else{
6205                 j++;
6206               }
6207             }
6208           }
6209           if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
6210           if( hit ) re_add_state(pNext, x+n);
6211           break;
6212         }
6213       }
6214     }
6215   }
6216   for(i=0; i<pNext->nState; i++){
6217     if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
6218   }
6219 re_match_end:
6220   sqlite3_free(pToFree);
6221   return rc;
6222 }
6223
6224 /* Resize the opcode and argument arrays for an RE under construction.
6225 */
6226 static int re_resize(ReCompiled *p, int N){
6227   char *aOp;
6228   int *aArg;
6229   aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6230   if( aOp==0 ) return 1;
6231   p->aOp = aOp;
6232   aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6233   if( aArg==0 ) return 1;
6234   p->aArg = aArg;
6235   p->nAlloc = N;
6236   return 0;
6237 }
6238
6239 /* Insert a new opcode and argument into an RE under construction.  The
6240 ** insertion point is just prior to existing opcode iBefore.
6241 */
6242 static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
6243   int i;
6244   if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
6245   for(i=p->nState; i>iBefore; i--){
6246     p->aOp[i] = p->aOp[i-1];
6247     p->aArg[i] = p->aArg[i-1];
6248   }
6249   p->nState++;
6250   p->aOp[iBefore] = (char)op;
6251   p->aArg[iBefore] = arg;
6252   return iBefore;
6253 }
6254
6255 /* Append a new opcode and argument to the end of the RE under construction.
6256 */
6257 static int re_append(ReCompiled *p, int op, int arg){
6258   return re_insert(p, p->nState, op, arg);
6259 }
6260
6261 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6262 ** under construction.
6263 */
6264 static void re_copy(ReCompiled *p, int iStart, int N){
6265   if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6266   memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6267   memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6268   p->nState += N;
6269 }
6270
6271 /* Return true if c is a hexadecimal digit character:  [0-9a-fA-F]
6272 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c).  If
6273 ** c is not a hex digit *pV is unchanged.
6274 */
6275 static int re_hex(int c, int *pV){
6276   if( c>='0' && c<='9' ){
6277     c -= '0';
6278   }else if( c>='a' && c<='f' ){
6279     c -= 'a' - 10;
6280   }else if( c>='A' && c<='F' ){
6281     c -= 'A' - 10;
6282   }else{
6283     return 0;
6284   }
6285   *pV = (*pV)*16 + (c & 0xff);
6286   return 1;
6287 }
6288
6289 /* A backslash character has been seen, read the next character and
6290 ** return its interpretation.
6291 */
6292 static unsigned re_esc_char(ReCompiled *p){
6293   static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
6294   static const char zTrans[] = "\a\f\n\r\t\v";
6295   int i, v = 0;
6296   char c;
6297   if( p->sIn.i>=p->sIn.mx ) return 0;
6298   c = p->sIn.z[p->sIn.i];
6299   if( c=='u' && p->sIn.i+4<p->sIn.mx ){
6300     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6301     if( re_hex(zIn[1],&v)
6302      && re_hex(zIn[2],&v)
6303      && re_hex(zIn[3],&v)
6304      && re_hex(zIn[4],&v)
6305     ){
6306       p->sIn.i += 5;
6307       return v;
6308     }
6309   }
6310   if( c=='x' && p->sIn.i+2<p->sIn.mx ){
6311     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6312     if( re_hex(zIn[1],&v)
6313      && re_hex(zIn[2],&v)
6314     ){
6315       p->sIn.i += 3;
6316       return v;
6317     }
6318   }
6319   for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
6320   if( zEsc[i] ){
6321     if( i<6 ) c = zTrans[i];
6322     p->sIn.i++;
6323   }else{
6324     p->zErr = "unknown \\ escape";
6325   }
6326   return c;
6327 }
6328
6329 /* Forward declaration */
6330 static const char *re_subcompile_string(ReCompiled*);
6331
6332 /* Peek at the next byte of input */
6333 static unsigned char rePeek(ReCompiled *p){
6334   return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
6335 }
6336
6337 /* Compile RE text into a sequence of opcodes.  Continue up to the
6338 ** first unmatched ")" character, then return.  If an error is found,
6339 ** return a pointer to the error message string.
6340 */
6341 static const char *re_subcompile_re(ReCompiled *p){
6342   const char *zErr;
6343   int iStart, iEnd, iGoto;
6344   iStart = p->nState;
6345   zErr = re_subcompile_string(p);
6346   if( zErr ) return zErr;
6347   while( rePeek(p)=='|' ){
6348     iEnd = p->nState;
6349     re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
6350     iGoto = re_append(p, RE_OP_GOTO, 0);
6351     p->sIn.i++;
6352     zErr = re_subcompile_string(p);
6353     if( zErr ) return zErr;
6354     p->aArg[iGoto] = p->nState - iGoto;
6355   }
6356   return 0;
6357 }
6358
6359 /* Compile an element of regular expression text (anything that can be
6360 ** an operand to the "|" operator).  Return NULL on success or a pointer
6361 ** to the error message if there is a problem.
6362 */
6363 static const char *re_subcompile_string(ReCompiled *p){
6364   int iPrev = -1;
6365   int iStart;
6366   unsigned c;
6367   const char *zErr;
6368   while( (c = p->xNextChar(&p->sIn))!=0 ){
6369     iStart = p->nState;
6370     switch( c ){
6371       case '|':
6372       case '$':
6373       case ')': {
6374         p->sIn.i--;
6375         return 0;
6376       }
6377       case '(': {
6378         zErr = re_subcompile_re(p);
6379         if( zErr ) return zErr;
6380         if( rePeek(p)!=')' ) return "unmatched '('";
6381         p->sIn.i++;
6382         break;
6383       }
6384       case '.': {
6385         if( rePeek(p)=='*' ){
6386           re_append(p, RE_OP_ANYSTAR, 0);
6387           p->sIn.i++;
6388         }else{
6389           re_append(p, RE_OP_ANY, 0);
6390         }
6391         break;
6392       }
6393       case '*': {
6394         if( iPrev<0 ) return "'*' without operand";
6395         re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
6396         re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
6397         break;
6398       }
6399       case '+': {
6400         if( iPrev<0 ) return "'+' without operand";
6401         re_append(p, RE_OP_FORK, iPrev - p->nState);
6402         break;
6403       }
6404       case '?': {
6405         if( iPrev<0 ) return "'?' without operand";
6406         re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
6407         break;
6408       }
6409       case '{': {
6410         int m = 0, n = 0;
6411         int sz, j;
6412         if( iPrev<0 ) return "'{m,n}' without operand";
6413         while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
6414         n = m;
6415         if( c==',' ){
6416           p->sIn.i++;
6417           n = 0;
6418           while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
6419         }
6420         if( c!='}' ) return "unmatched '{'";
6421         if( n>0 && n<m ) return "n less than m in '{m,n}'";
6422         p->sIn.i++;
6423         sz = p->nState - iPrev;
6424         if( m==0 ){
6425           if( n==0 ) return "both m and n are zero in '{m,n}'";
6426           re_insert(p, iPrev, RE_OP_FORK, sz+1);
6427           n--;
6428         }else{
6429           for(j=1; j<m; j++) re_copy(p, iPrev, sz);
6430         }
6431         for(j=m; j<n; j++){
6432           re_append(p, RE_OP_FORK, sz+1);
6433           re_copy(p, iPrev, sz);
6434         }
6435         if( n==0 && m>0 ){
6436           re_append(p, RE_OP_FORK, -sz);
6437         }
6438         break;
6439       }
6440       case '[': {
6441         int iFirst = p->nState;
6442         if( rePeek(p)=='^' ){
6443           re_append(p, RE_OP_CC_EXC, 0);
6444           p->sIn.i++;
6445         }else{
6446           re_append(p, RE_OP_CC_INC, 0);
6447         }
6448         while( (c = p->xNextChar(&p->sIn))!=0 ){
6449           if( c=='[' && rePeek(p)==':' ){
6450             return "POSIX character classes not supported";
6451           }
6452           if( c=='\\' ) c = re_esc_char(p);
6453           if( rePeek(p)=='-' ){
6454             re_append(p, RE_OP_CC_RANGE, c);
6455             p->sIn.i++;
6456             c = p->xNextChar(&p->sIn);
6457             if( c=='\\' ) c = re_esc_char(p);
6458             re_append(p, RE_OP_CC_RANGE, c);
6459           }else{
6460             re_append(p, RE_OP_CC_VALUE, c);
6461           }
6462           if( rePeek(p)==']' ){ p->sIn.i++; break; }
6463         }
6464         if( c==0 ) return "unclosed '['";
6465         p->aArg[iFirst] = p->nState - iFirst;
6466         break;
6467       }
6468       case '\\': {
6469         int specialOp = 0;
6470         switch( rePeek(p) ){
6471           case 'b': specialOp = RE_OP_BOUNDARY;   break;
6472           case 'd': specialOp = RE_OP_DIGIT;      break;
6473           case 'D': specialOp = RE_OP_NOTDIGIT;   break;
6474           case 's': specialOp = RE_OP_SPACE;      break;
6475           case 'S': specialOp = RE_OP_NOTSPACE;   break;
6476           case 'w': specialOp = RE_OP_WORD;       break;
6477           case 'W': specialOp = RE_OP_NOTWORD;    break;
6478         }
6479         if( specialOp ){
6480           p->sIn.i++;
6481           re_append(p, specialOp, 0);
6482         }else{
6483           c = re_esc_char(p);
6484           re_append(p, RE_OP_MATCH, c);
6485         }
6486         break;
6487       }
6488       default: {
6489         re_append(p, RE_OP_MATCH, c);
6490         break;
6491       }
6492     }
6493     iPrev = iStart;
6494   }
6495   return 0;
6496 }
6497
6498 /* Free and reclaim all the memory used by a previously compiled
6499 ** regular expression.  Applications should invoke this routine once
6500 ** for every call to re_compile() to avoid memory leaks.
6501 */
6502 static void re_free(ReCompiled *pRe){
6503   if( pRe ){
6504     sqlite3_free(pRe->aOp);
6505     sqlite3_free(pRe->aArg);
6506     sqlite3_free(pRe);
6507   }
6508 }
6509
6510 /*
6511 ** Compile a textual regular expression in zIn[] into a compiled regular
6512 ** expression suitable for us by re_match() and return a pointer to the
6513 ** compiled regular expression in *ppRe.  Return NULL on success or an
6514 ** error message if something goes wrong.
6515 */
6516 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
6517   ReCompiled *pRe;
6518   const char *zErr;
6519   int i, j;
6520
6521   *ppRe = 0;
6522   pRe = sqlite3_malloc( sizeof(*pRe) );
6523   if( pRe==0 ){
6524     return "out of memory";
6525   }
6526   memset(pRe, 0, sizeof(*pRe));
6527   pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
6528   if( re_resize(pRe, 30) ){
6529     re_free(pRe);
6530     return "out of memory";
6531   }
6532   if( zIn[0]=='^' ){
6533     zIn++;
6534   }else{
6535     re_append(pRe, RE_OP_ANYSTAR, 0);
6536   }
6537   pRe->sIn.z = (unsigned char*)zIn;
6538   pRe->sIn.i = 0;
6539   pRe->sIn.mx = (int)strlen(zIn);
6540   zErr = re_subcompile_re(pRe);
6541   if( zErr ){
6542     re_free(pRe);
6543     return zErr;
6544   }
6545   if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
6546     re_append(pRe, RE_OP_MATCH, RE_EOF);
6547     re_append(pRe, RE_OP_ACCEPT, 0);
6548     *ppRe = pRe;
6549   }else if( pRe->sIn.i>=pRe->sIn.mx ){
6550     re_append(pRe, RE_OP_ACCEPT, 0);
6551     *ppRe = pRe;
6552   }else{
6553     re_free(pRe);
6554     return "unrecognized character";
6555   }
6556
6557   /* The following is a performance optimization.  If the regex begins with
6558   ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6559   ** one or more matching characters, enter those matching characters into
6560   ** zInit[].  The re_match() routine can then search ahead in the input 
6561   ** string looking for the initial match without having to run the whole
6562   ** regex engine over the string.  Do not worry able trying to match
6563   ** unicode characters beyond plane 0 - those are very rare and this is
6564   ** just an optimization. */
6565   if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
6566     for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
6567       unsigned x = pRe->aArg[i];
6568       if( x<=127 ){
6569         pRe->zInit[j++] = (unsigned char)x;
6570       }else if( x<=0xfff ){
6571         pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
6572         pRe->zInit[j++] = 0x80 | (x&0x3f);
6573       }else if( x<=0xffff ){
6574         pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12));
6575         pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
6576         pRe->zInit[j++] = 0x80 | (x&0x3f);
6577       }else{
6578         break;
6579       }
6580     }
6581     if( j>0 && pRe->zInit[j-1]==0 ) j--;
6582     pRe->nInit = j;
6583   }
6584   return pRe->zErr;
6585 }
6586
6587 /*
6588 ** Implementation of the regexp() SQL function.  This function implements
6589 ** the build-in REGEXP operator.  The first argument to the function is the
6590 ** pattern and the second argument is the string.  So, the SQL statements:
6591 **
6592 **       A REGEXP B
6593 **
6594 ** is implemented as regexp(B,A).
6595 */
6596 static void re_sql_func(
6597   sqlite3_context *context,
6598   int argc,
6599   sqlite3_value **argv
6600 ){
6601   ReCompiled *pRe;          /* Compiled regular expression */
6602   const char *zPattern;     /* The regular expression */
6603   const unsigned char *zStr;/* String being searched */
6604   const char *zErr;         /* Compile error message */
6605   int setAux = 0;           /* True to invoke sqlite3_set_auxdata() */
6606
6607   (void)argc;  /* Unused */
6608   pRe = sqlite3_get_auxdata(context, 0);
6609   if( pRe==0 ){
6610     zPattern = (const char*)sqlite3_value_text(argv[0]);
6611     if( zPattern==0 ) return;
6612     zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
6613     if( zErr ){
6614       re_free(pRe);
6615       sqlite3_result_error(context, zErr, -1);
6616       return;
6617     }
6618     if( pRe==0 ){
6619       sqlite3_result_error_nomem(context);
6620       return;
6621     }
6622     setAux = 1;
6623   }
6624   zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
6625   if( zStr!=0 ){
6626     sqlite3_result_int(context, re_match(pRe, zStr, -1));
6627   }
6628   if( setAux ){
6629     sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
6630   }
6631 }
6632
6633 /*
6634 ** Invoke this routine to register the regexp() function with the
6635 ** SQLite database connection.
6636 */
6637 #ifdef _WIN32
6638
6639 #endif
6640 int sqlite3_regexp_init(
6641   sqlite3 *db, 
6642   char **pzErrMsg, 
6643   const sqlite3_api_routines *pApi
6644 ){
6645   int rc = SQLITE_OK;
6646   SQLITE_EXTENSION_INIT2(pApi);
6647   (void)pzErrMsg;  /* Unused */
6648   rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6649                                0, re_sql_func, 0, 0);
6650   if( rc==SQLITE_OK ){
6651     /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6652     ** of regexp(PATTERN,STRING). */
6653     rc = sqlite3_create_function(db, "regexpi", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6654                                  (void*)db, re_sql_func, 0, 0);
6655   }
6656   return rc;
6657 }
6658
6659 /************************* End ../ext/misc/regexp.c ********************/
6660 #ifdef SQLITE_HAVE_ZLIB
6661 /************************* Begin ../ext/misc/zipfile.c ******************/
6662 /*
6663 ** 2017-12-26
6664 **
6665 ** The author disclaims copyright to this source code.  In place of
6666 ** a legal notice, here is a blessing:
6667 **
6668 **    May you do good and not evil.
6669 **    May you find forgiveness for yourself and forgive others.
6670 **    May you share freely, never taking more than you give.
6671 **
6672 ******************************************************************************
6673 **
6674 ** This file implements a virtual table for reading and writing ZIP archive
6675 ** files.
6676 **
6677 ** Usage example:
6678 **
6679 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
6680 **
6681 ** Current limitations:
6682 **
6683 **    *  No support for encryption
6684 **    *  No support for ZIP archives spanning multiple files
6685 **    *  No support for zip64 extensions
6686 **    *  Only the "inflate/deflate" (zlib) compression method is supported
6687 */
6688 /* #include "sqlite3ext.h" */
6689 SQLITE_EXTENSION_INIT1
6690 #include <stdio.h>
6691 #include <string.h>
6692 #include <assert.h>
6693
6694 #include <zlib.h>
6695
6696 #ifndef SQLITE_OMIT_VIRTUALTABLE
6697
6698 #ifndef SQLITE_AMALGAMATION
6699
6700 #ifndef UINT32_TYPE
6701 # ifdef HAVE_UINT32_T
6702 #  define UINT32_TYPE uint32_t
6703 # else
6704 #  define UINT32_TYPE unsigned int
6705 # endif
6706 #endif
6707 #ifndef UINT16_TYPE
6708 # ifdef HAVE_UINT16_T
6709 #  define UINT16_TYPE uint16_t
6710 # else
6711 #  define UINT16_TYPE unsigned short int
6712 # endif
6713 #endif
6714 /* typedef sqlite3_int64 i64; */
6715 /* typedef unsigned char u8; */
6716 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
6717 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
6718 #define MIN(a,b) ((a)<(b) ? (a) : (b))
6719
6720 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
6721 # define ALWAYS(X)      (1)
6722 # define NEVER(X)       (0)
6723 #elif !defined(NDEBUG)
6724 # define ALWAYS(X)      ((X)?1:(assert(0),0))
6725 # define NEVER(X)       ((X)?(assert(0),1):0)
6726 #else
6727 # define ALWAYS(X)      (X)
6728 # define NEVER(X)       (X)
6729 #endif
6730
6731 #endif   /* SQLITE_AMALGAMATION */
6732
6733 /*
6734 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
6735 **
6736 ** In some ways it would be better to obtain these values from system 
6737 ** header files. But, the dependency is undesirable and (a) these
6738 ** have been stable for decades, (b) the values are part of POSIX and
6739 ** are also made explicit in [man stat], and (c) are part of the 
6740 ** file format for zip archives.
6741 */
6742 #ifndef S_IFDIR
6743 # define S_IFDIR 0040000
6744 #endif
6745 #ifndef S_IFREG
6746 # define S_IFREG 0100000
6747 #endif
6748 #ifndef S_IFLNK
6749 # define S_IFLNK 0120000
6750 #endif
6751
6752 static const char ZIPFILE_SCHEMA[] = 
6753   "CREATE TABLE y("
6754     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
6755     "mode,"              /* 1: POSIX mode for file */
6756     "mtime,"             /* 2: Last modification time (secs since 1970)*/
6757     "sz,"                /* 3: Size of object */
6758     "rawdata,"           /* 4: Raw data */
6759     "data,"              /* 5: Uncompressed data */
6760     "method,"            /* 6: Compression method (integer) */
6761     "z HIDDEN"           /* 7: Name of zip file */
6762   ") WITHOUT ROWID;";
6763
6764 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
6765 #define ZIPFILE_BUFFER_SIZE (64*1024)
6766
6767
6768 /*
6769 ** Magic numbers used to read and write zip files.
6770 **
6771 ** ZIPFILE_NEWENTRY_MADEBY:
6772 **   Use this value for the "version-made-by" field in new zip file
6773 **   entries. The upper byte indicates "unix", and the lower byte 
6774 **   indicates that the zip file matches pkzip specification 3.0. 
6775 **   This is what info-zip seems to do.
6776 **
6777 ** ZIPFILE_NEWENTRY_REQUIRED:
6778 **   Value for "version-required-to-extract" field of new entries.
6779 **   Version 2.0 is required to support folders and deflate compression.
6780 **
6781 ** ZIPFILE_NEWENTRY_FLAGS:
6782 **   Value for "general-purpose-bit-flags" field of new entries. Bit
6783 **   11 means "utf-8 filename and comment".
6784 **
6785 ** ZIPFILE_SIGNATURE_CDS:
6786 **   First 4 bytes of a valid CDS record.
6787 **
6788 ** ZIPFILE_SIGNATURE_LFH:
6789 **   First 4 bytes of a valid LFH record.
6790 **
6791 ** ZIPFILE_SIGNATURE_EOCD
6792 **   First 4 bytes of a valid EOCD record.
6793 */
6794 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
6795 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
6796 #define ZIPFILE_NEWENTRY_REQUIRED 20
6797 #define ZIPFILE_NEWENTRY_FLAGS    0x800
6798 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
6799 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
6800 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
6801
6802 /*
6803 ** The sizes of the fixed-size part of each of the three main data 
6804 ** structures in a zip archive.
6805 */
6806 #define ZIPFILE_LFH_FIXED_SZ      30
6807 #define ZIPFILE_EOCD_FIXED_SZ     22
6808 #define ZIPFILE_CDS_FIXED_SZ      46
6809
6810 /*
6811 *** 4.3.16  End of central directory record:
6812 ***
6813 ***   end of central dir signature    4 bytes  (0x06054b50)
6814 ***   number of this disk             2 bytes
6815 ***   number of the disk with the
6816 ***   start of the central directory  2 bytes
6817 ***   total number of entries in the
6818 ***   central directory on this disk  2 bytes
6819 ***   total number of entries in
6820 ***   the central directory           2 bytes
6821 ***   size of the central directory   4 bytes
6822 ***   offset of start of central
6823 ***   directory with respect to
6824 ***   the starting disk number        4 bytes
6825 ***   .ZIP file comment length        2 bytes
6826 ***   .ZIP file comment       (variable size)
6827 */
6828 typedef struct ZipfileEOCD ZipfileEOCD;
6829 struct ZipfileEOCD {
6830   u16 iDisk;
6831   u16 iFirstDisk;
6832   u16 nEntry;
6833   u16 nEntryTotal;
6834   u32 nSize;
6835   u32 iOffset;
6836 };
6837
6838 /*
6839 *** 4.3.12  Central directory structure:
6840 ***
6841 *** ...
6842 ***
6843 ***   central file header signature   4 bytes  (0x02014b50)
6844 ***   version made by                 2 bytes
6845 ***   version needed to extract       2 bytes
6846 ***   general purpose bit flag        2 bytes
6847 ***   compression method              2 bytes
6848 ***   last mod file time              2 bytes
6849 ***   last mod file date              2 bytes
6850 ***   crc-32                          4 bytes
6851 ***   compressed size                 4 bytes
6852 ***   uncompressed size               4 bytes
6853 ***   file name length                2 bytes
6854 ***   extra field length              2 bytes
6855 ***   file comment length             2 bytes
6856 ***   disk number start               2 bytes
6857 ***   internal file attributes        2 bytes
6858 ***   external file attributes        4 bytes
6859 ***   relative offset of local header 4 bytes
6860 */
6861 typedef struct ZipfileCDS ZipfileCDS;
6862 struct ZipfileCDS {
6863   u16 iVersionMadeBy;
6864   u16 iVersionExtract;
6865   u16 flags;
6866   u16 iCompression;
6867   u16 mTime;
6868   u16 mDate;
6869   u32 crc32;
6870   u32 szCompressed;
6871   u32 szUncompressed;
6872   u16 nFile;
6873   u16 nExtra;
6874   u16 nComment;
6875   u16 iDiskStart;
6876   u16 iInternalAttr;
6877   u32 iExternalAttr;
6878   u32 iOffset;
6879   char *zFile;                    /* Filename (sqlite3_malloc()) */
6880 };
6881
6882 /*
6883 *** 4.3.7  Local file header:
6884 ***
6885 ***   local file header signature     4 bytes  (0x04034b50)
6886 ***   version needed to extract       2 bytes
6887 ***   general purpose bit flag        2 bytes
6888 ***   compression method              2 bytes
6889 ***   last mod file time              2 bytes
6890 ***   last mod file date              2 bytes
6891 ***   crc-32                          4 bytes
6892 ***   compressed size                 4 bytes
6893 ***   uncompressed size               4 bytes
6894 ***   file name length                2 bytes
6895 ***   extra field length              2 bytes
6896 ***   
6897 */
6898 typedef struct ZipfileLFH ZipfileLFH;
6899 struct ZipfileLFH {
6900   u16 iVersionExtract;
6901   u16 flags;
6902   u16 iCompression;
6903   u16 mTime;
6904   u16 mDate;
6905   u32 crc32;
6906   u32 szCompressed;
6907   u32 szUncompressed;
6908   u16 nFile;
6909   u16 nExtra;
6910 };
6911
6912 typedef struct ZipfileEntry ZipfileEntry;
6913 struct ZipfileEntry {
6914   ZipfileCDS cds;            /* Parsed CDS record */
6915   u32 mUnixTime;             /* Modification time, in UNIX format */
6916   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
6917   i64 iDataOff;              /* Offset to data in file (if aData==0) */
6918   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
6919   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
6920 };
6921
6922 /* 
6923 ** Cursor type for zipfile tables.
6924 */
6925 typedef struct ZipfileCsr ZipfileCsr;
6926 struct ZipfileCsr {
6927   sqlite3_vtab_cursor base;  /* Base class - must be first */
6928   i64 iId;                   /* Cursor ID */
6929   u8 bEof;                   /* True when at EOF */
6930   u8 bNoop;                  /* If next xNext() call is no-op */
6931
6932   /* Used outside of write transactions */
6933   FILE *pFile;               /* Zip file */
6934   i64 iNextOff;              /* Offset of next record in central directory */
6935   ZipfileEOCD eocd;          /* Parse of central directory record */
6936
6937   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
6938   ZipfileEntry *pCurrent;    /* Current entry */
6939   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
6940 };
6941
6942 typedef struct ZipfileTab ZipfileTab;
6943 struct ZipfileTab {
6944   sqlite3_vtab base;         /* Base class - must be first */
6945   char *zFile;               /* Zip file this table accesses (may be NULL) */
6946   sqlite3 *db;               /* Host database connection */
6947   u8 *aBuffer;               /* Temporary buffer used for various tasks */
6948
6949   ZipfileCsr *pCsrList;      /* List of cursors */
6950   i64 iNextCsrid;
6951
6952   /* The following are used by write transactions only */
6953   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6954   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
6955   FILE *pWriteFd;            /* File handle open on zip archive */
6956   i64 szCurrent;             /* Current size of zip archive */
6957   i64 szOrig;                /* Size of archive at start of transaction */
6958 };
6959
6960 /*
6961 ** Set the error message contained in context ctx to the results of
6962 ** vprintf(zFmt, ...).
6963 */
6964 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
6965   char *zMsg = 0;
6966   va_list ap;
6967   va_start(ap, zFmt);
6968   zMsg = sqlite3_vmprintf(zFmt, ap);
6969   sqlite3_result_error(ctx, zMsg, -1);
6970   sqlite3_free(zMsg);
6971   va_end(ap);
6972 }
6973
6974 /*
6975 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
6976 ** is not quoted, do nothing.
6977 */
6978 static void zipfileDequote(char *zIn){
6979   char q = zIn[0];
6980   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
6981     int iIn = 1;
6982     int iOut = 0;
6983     if( q=='[' ) q = ']';
6984     while( ALWAYS(zIn[iIn]) ){
6985       char c = zIn[iIn++];
6986       if( c==q && zIn[iIn++]!=q ) break;
6987       zIn[iOut++] = c;
6988     }
6989     zIn[iOut] = '\0';
6990   }
6991 }
6992
6993 /*
6994 ** Construct a new ZipfileTab virtual table object.
6995 ** 
6996 **   argv[0]   -> module name  ("zipfile")
6997 **   argv[1]   -> database name
6998 **   argv[2]   -> table name
6999 **   argv[...] -> "column name" and other module argument fields.
7000 */
7001 static int zipfileConnect(
7002   sqlite3 *db,
7003   void *pAux,
7004   int argc, const char *const*argv,
7005   sqlite3_vtab **ppVtab,
7006   char **pzErr
7007 ){
7008   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
7009   int nFile = 0;
7010   const char *zFile = 0;
7011   ZipfileTab *pNew = 0;
7012   int rc;
7013
7014   /* If the table name is not "zipfile", require that the argument be
7015   ** specified. This stops zipfile tables from being created as:
7016   **
7017   **   CREATE VIRTUAL TABLE zzz USING zipfile();
7018   **
7019   ** It does not prevent:
7020   **
7021   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
7022   */
7023   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
7024   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
7025     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
7026     return SQLITE_ERROR;
7027   }
7028
7029   if( argc>3 ){
7030     zFile = argv[3];
7031     nFile = (int)strlen(zFile)+1;
7032   }
7033
7034   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
7035   if( rc==SQLITE_OK ){
7036     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
7037     if( pNew==0 ) return SQLITE_NOMEM;
7038     memset(pNew, 0, nByte+nFile);
7039     pNew->db = db;
7040     pNew->aBuffer = (u8*)&pNew[1];
7041     if( zFile ){
7042       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
7043       memcpy(pNew->zFile, zFile, nFile);
7044       zipfileDequote(pNew->zFile);
7045     }
7046   }
7047   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
7048   *ppVtab = (sqlite3_vtab*)pNew;
7049   return rc;
7050 }
7051
7052 /*
7053 ** Free the ZipfileEntry structure indicated by the only argument.
7054 */
7055 static void zipfileEntryFree(ZipfileEntry *p){
7056   if( p ){
7057     sqlite3_free(p->cds.zFile);
7058     sqlite3_free(p);
7059   }
7060 }
7061
7062 /*
7063 ** Release resources that should be freed at the end of a write 
7064 ** transaction.
7065 */
7066 static void zipfileCleanupTransaction(ZipfileTab *pTab){
7067   ZipfileEntry *pEntry;
7068   ZipfileEntry *pNext;
7069
7070   if( pTab->pWriteFd ){
7071     fclose(pTab->pWriteFd);
7072     pTab->pWriteFd = 0;
7073   }
7074   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
7075     pNext = pEntry->pNext;
7076     zipfileEntryFree(pEntry);
7077   }
7078   pTab->pFirstEntry = 0;
7079   pTab->pLastEntry = 0;
7080   pTab->szCurrent = 0;
7081   pTab->szOrig = 0;
7082 }
7083
7084 /*
7085 ** This method is the destructor for zipfile vtab objects.
7086 */
7087 static int zipfileDisconnect(sqlite3_vtab *pVtab){
7088   zipfileCleanupTransaction((ZipfileTab*)pVtab);
7089   sqlite3_free(pVtab);
7090   return SQLITE_OK;
7091 }
7092
7093 /*
7094 ** Constructor for a new ZipfileCsr object.
7095 */
7096 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
7097   ZipfileTab *pTab = (ZipfileTab*)p;
7098   ZipfileCsr *pCsr;
7099   pCsr = sqlite3_malloc(sizeof(*pCsr));
7100   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
7101   if( pCsr==0 ){
7102     return SQLITE_NOMEM;
7103   }
7104   memset(pCsr, 0, sizeof(*pCsr));
7105   pCsr->iId = ++pTab->iNextCsrid;
7106   pCsr->pCsrNext = pTab->pCsrList;
7107   pTab->pCsrList = pCsr;
7108   return SQLITE_OK;
7109 }
7110
7111 /*
7112 ** Reset a cursor back to the state it was in when first returned
7113 ** by zipfileOpen().
7114 */
7115 static void zipfileResetCursor(ZipfileCsr *pCsr){
7116   ZipfileEntry *p;
7117   ZipfileEntry *pNext;
7118
7119   pCsr->bEof = 0;
7120   if( pCsr->pFile ){
7121     fclose(pCsr->pFile);
7122     pCsr->pFile = 0;
7123     zipfileEntryFree(pCsr->pCurrent);
7124     pCsr->pCurrent = 0;
7125   }
7126
7127   for(p=pCsr->pFreeEntry; p; p=pNext){
7128     pNext = p->pNext;
7129     zipfileEntryFree(p);
7130   }
7131 }
7132
7133 /*
7134 ** Destructor for an ZipfileCsr.
7135 */
7136 static int zipfileClose(sqlite3_vtab_cursor *cur){
7137   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7138   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
7139   ZipfileCsr **pp;
7140   zipfileResetCursor(pCsr);
7141
7142   /* Remove this cursor from the ZipfileTab.pCsrList list. */
7143   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
7144   *pp = pCsr->pCsrNext;
7145
7146   sqlite3_free(pCsr);
7147   return SQLITE_OK;
7148 }
7149
7150 /*
7151 ** Set the error message for the virtual table associated with cursor
7152 ** pCsr to the results of vprintf(zFmt, ...).
7153 */
7154 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
7155   va_list ap;
7156   va_start(ap, zFmt);
7157   sqlite3_free(pTab->base.zErrMsg);
7158   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
7159   va_end(ap);
7160 }
7161 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
7162   va_list ap;
7163   va_start(ap, zFmt);
7164   sqlite3_free(pCsr->base.pVtab->zErrMsg);
7165   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
7166   va_end(ap);
7167 }
7168
7169 /*
7170 ** Read nRead bytes of data from offset iOff of file pFile into buffer
7171 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
7172 ** otherwise. 
7173 **
7174 ** If an error does occur, output variable (*pzErrmsg) may be set to point
7175 ** to an English language error message. It is the responsibility of the
7176 ** caller to eventually free this buffer using
7177 ** sqlite3_free().
7178 */
7179 static int zipfileReadData(
7180   FILE *pFile,                    /* Read from this file */
7181   u8 *aRead,                      /* Read into this buffer */
7182   int nRead,                      /* Number of bytes to read */
7183   i64 iOff,                       /* Offset to read from */
7184   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
7185 ){
7186   size_t n;
7187   fseek(pFile, (long)iOff, SEEK_SET);
7188   n = fread(aRead, 1, nRead, pFile);
7189   if( (int)n!=nRead ){
7190     *pzErrmsg = sqlite3_mprintf("error in fread()");
7191     return SQLITE_ERROR;
7192   }
7193   return SQLITE_OK;
7194 }
7195
7196 static int zipfileAppendData(
7197   ZipfileTab *pTab,
7198   const u8 *aWrite,
7199   int nWrite
7200 ){
7201   if( nWrite>0 ){
7202     size_t n = nWrite;
7203     fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
7204     n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
7205     if( (int)n!=nWrite ){
7206       pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
7207       return SQLITE_ERROR;
7208     }
7209     pTab->szCurrent += nWrite;
7210   }
7211   return SQLITE_OK;
7212 }
7213
7214 /*
7215 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
7216 */
7217 static u16 zipfileGetU16(const u8 *aBuf){
7218   return (aBuf[1] << 8) + aBuf[0];
7219 }
7220
7221 /*
7222 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
7223 */
7224 static u32 zipfileGetU32(const u8 *aBuf){
7225   return ((u32)(aBuf[3]) << 24)
7226        + ((u32)(aBuf[2]) << 16)
7227        + ((u32)(aBuf[1]) <<  8)
7228        + ((u32)(aBuf[0]) <<  0);
7229 }
7230
7231 /*
7232 ** Write a 16-bit little endiate integer into buffer aBuf.
7233 */
7234 static void zipfilePutU16(u8 *aBuf, u16 val){
7235   aBuf[0] = val & 0xFF;
7236   aBuf[1] = (val>>8) & 0xFF;
7237 }
7238
7239 /*
7240 ** Write a 32-bit little endiate integer into buffer aBuf.
7241 */
7242 static void zipfilePutU32(u8 *aBuf, u32 val){
7243   aBuf[0] = val & 0xFF;
7244   aBuf[1] = (val>>8) & 0xFF;
7245   aBuf[2] = (val>>16) & 0xFF;
7246   aBuf[3] = (val>>24) & 0xFF;
7247 }
7248
7249 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
7250 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
7251
7252 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
7253 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
7254
7255 /*
7256 ** Magic numbers used to read CDS records.
7257 */
7258 #define ZIPFILE_CDS_NFILE_OFF        28
7259 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
7260
7261 /*
7262 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
7263 ** if the record is not well-formed, or SQLITE_OK otherwise.
7264 */
7265 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
7266   u8 *aRead = aBuf;
7267   u32 sig = zipfileRead32(aRead);
7268   int rc = SQLITE_OK;
7269   if( sig!=ZIPFILE_SIGNATURE_CDS ){
7270     rc = SQLITE_ERROR;
7271   }else{
7272     pCDS->iVersionMadeBy = zipfileRead16(aRead);
7273     pCDS->iVersionExtract = zipfileRead16(aRead);
7274     pCDS->flags = zipfileRead16(aRead);
7275     pCDS->iCompression = zipfileRead16(aRead);
7276     pCDS->mTime = zipfileRead16(aRead);
7277     pCDS->mDate = zipfileRead16(aRead);
7278     pCDS->crc32 = zipfileRead32(aRead);
7279     pCDS->szCompressed = zipfileRead32(aRead);
7280     pCDS->szUncompressed = zipfileRead32(aRead);
7281     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7282     pCDS->nFile = zipfileRead16(aRead);
7283     pCDS->nExtra = zipfileRead16(aRead);
7284     pCDS->nComment = zipfileRead16(aRead);
7285     pCDS->iDiskStart = zipfileRead16(aRead);
7286     pCDS->iInternalAttr = zipfileRead16(aRead);
7287     pCDS->iExternalAttr = zipfileRead32(aRead);
7288     pCDS->iOffset = zipfileRead32(aRead);
7289     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
7290   }
7291
7292   return rc;
7293 }
7294
7295 /*
7296 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
7297 ** if the record is not well-formed, or SQLITE_OK otherwise.
7298 */
7299 static int zipfileReadLFH(
7300   u8 *aBuffer,
7301   ZipfileLFH *pLFH
7302 ){
7303   u8 *aRead = aBuffer;
7304   int rc = SQLITE_OK;
7305
7306   u32 sig = zipfileRead32(aRead);
7307   if( sig!=ZIPFILE_SIGNATURE_LFH ){
7308     rc = SQLITE_ERROR;
7309   }else{
7310     pLFH->iVersionExtract = zipfileRead16(aRead);
7311     pLFH->flags = zipfileRead16(aRead);
7312     pLFH->iCompression = zipfileRead16(aRead);
7313     pLFH->mTime = zipfileRead16(aRead);
7314     pLFH->mDate = zipfileRead16(aRead);
7315     pLFH->crc32 = zipfileRead32(aRead);
7316     pLFH->szCompressed = zipfileRead32(aRead);
7317     pLFH->szUncompressed = zipfileRead32(aRead);
7318     pLFH->nFile = zipfileRead16(aRead);
7319     pLFH->nExtra = zipfileRead16(aRead);
7320   }
7321   return rc;
7322 }
7323
7324
7325 /*
7326 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
7327 ** Scan through this buffer to find an "extra-timestamp" field. If one
7328 ** exists, extract the 32-bit modification-timestamp from it and store
7329 ** the value in output parameter *pmTime.
7330 **
7331 ** Zero is returned if no extra-timestamp record could be found (and so
7332 ** *pmTime is left unchanged), or non-zero otherwise.
7333 **
7334 ** The general format of an extra field is:
7335 **
7336 **   Header ID    2 bytes
7337 **   Data Size    2 bytes
7338 **   Data         N bytes
7339 */
7340 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
7341   int ret = 0;
7342   u8 *p = aExtra;
7343   u8 *pEnd = &aExtra[nExtra];
7344
7345   while( p<pEnd ){
7346     u16 id = zipfileRead16(p);
7347     u16 nByte = zipfileRead16(p);
7348
7349     switch( id ){
7350       case ZIPFILE_EXTRA_TIMESTAMP: {
7351         u8 b = p[0];
7352         if( b & 0x01 ){     /* 0x01 -> modtime is present */
7353           *pmTime = zipfileGetU32(&p[1]);
7354           ret = 1;
7355         }
7356         break;
7357       }
7358     }
7359
7360     p += nByte;
7361   }
7362   return ret;
7363 }
7364
7365 /*
7366 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
7367 ** fields of the CDS structure passed as the only argument to a 32-bit
7368 ** UNIX seconds-since-the-epoch timestamp. Return the result.
7369 **
7370 ** "Standard" MS-DOS time format:
7371 **
7372 **   File modification time:
7373 **     Bits 00-04: seconds divided by 2
7374 **     Bits 05-10: minute
7375 **     Bits 11-15: hour
7376 **   File modification date:
7377 **     Bits 00-04: day
7378 **     Bits 05-08: month (1-12)
7379 **     Bits 09-15: years from 1980 
7380 **
7381 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
7382 */
7383 static u32 zipfileMtime(ZipfileCDS *pCDS){
7384   int Y,M,D,X1,X2,A,B,sec,min,hr;
7385   i64 JDsec;
7386   Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
7387   M = ((pCDS->mDate >> 5) & 0x0F);
7388   D = (pCDS->mDate & 0x1F);
7389   sec = (pCDS->mTime & 0x1F)*2;
7390   min = (pCDS->mTime >> 5) & 0x3F;
7391   hr = (pCDS->mTime >> 11) & 0x1F;
7392   if( M<=2 ){
7393     Y--;
7394     M += 12;
7395   }
7396   X1 = 36525*(Y+4716)/100;
7397   X2 = 306001*(M+1)/10000;
7398   A = Y/100;
7399   B = 2 - A + (A/4);
7400   JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
7401   return (u32)(JDsec - (i64)24405875*(i64)8640);
7402 }
7403
7404 /*
7405 ** The opposite of zipfileMtime(). This function populates the mTime and
7406 ** mDate fields of the CDS structure passed as the first argument according
7407 ** to the UNIX timestamp value passed as the second.
7408 */
7409 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
7410   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
7411   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
7412
7413   int A, B, C, D, E;
7414   int yr, mon, day;
7415   int hr, min, sec;
7416
7417   A = (int)((JD - 1867216.25)/36524.25);
7418   A = (int)(JD + 1 + A - (A/4));
7419   B = A + 1524;
7420   C = (int)((B - 122.1)/365.25);
7421   D = (36525*(C&32767))/100;
7422   E = (int)((B-D)/30.6001);
7423
7424   day = B - D - (int)(30.6001*E);
7425   mon = (E<14 ? E-1 : E-13);
7426   yr = mon>2 ? C-4716 : C-4715;
7427
7428   hr = (mUnixTime % (24*60*60)) / (60*60);
7429   min = (mUnixTime % (60*60)) / 60;
7430   sec = (mUnixTime % 60);
7431
7432   if( yr>=1980 ){
7433     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
7434     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
7435   }else{
7436     pCds->mDate = pCds->mTime = 0;
7437   }
7438
7439   assert( mUnixTime<315507600 
7440        || mUnixTime==zipfileMtime(pCds) 
7441        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
7442        /* || (mUnixTime % 2) */
7443   );
7444 }
7445
7446 /*
7447 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
7448 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
7449 ** then pFile is a file-handle open on a zip file. In either case, this
7450 ** function creates a ZipfileEntry object based on the zip archive entry
7451 ** for which the CDS record is at offset iOff.
7452 **
7453 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
7454 ** the new object. Otherwise, an SQLite error code is returned and the
7455 ** final value of (*ppEntry) undefined.
7456 */
7457 static int zipfileGetEntry(
7458   ZipfileTab *pTab,               /* Store any error message here */
7459   const u8 *aBlob,                /* Pointer to in-memory file image */
7460   int nBlob,                      /* Size of aBlob[] in bytes */
7461   FILE *pFile,                    /* If aBlob==0, read from this file */
7462   i64 iOff,                       /* Offset of CDS record */
7463   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
7464 ){
7465   u8 *aRead;
7466   char **pzErr = &pTab->base.zErrMsg;
7467   int rc = SQLITE_OK;
7468
7469   if( aBlob==0 ){
7470     aRead = pTab->aBuffer;
7471     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
7472   }else{
7473     aRead = (u8*)&aBlob[iOff];
7474   }
7475
7476   if( rc==SQLITE_OK ){
7477     sqlite3_int64 nAlloc;
7478     ZipfileEntry *pNew;
7479
7480     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
7481     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
7482     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
7483
7484     nAlloc = sizeof(ZipfileEntry) + nExtra;
7485     if( aBlob ){
7486       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
7487     }
7488
7489     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
7490     if( pNew==0 ){
7491       rc = SQLITE_NOMEM;
7492     }else{
7493       memset(pNew, 0, sizeof(ZipfileEntry));
7494       rc = zipfileReadCDS(aRead, &pNew->cds);
7495       if( rc!=SQLITE_OK ){
7496         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
7497       }else if( aBlob==0 ){
7498         rc = zipfileReadData(
7499             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
7500         );
7501       }else{
7502         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
7503       }
7504     }
7505
7506     if( rc==SQLITE_OK ){
7507       u32 *pt = &pNew->mUnixTime;
7508       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
7509       pNew->aExtra = (u8*)&pNew[1];
7510       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
7511       if( pNew->cds.zFile==0 ){
7512         rc = SQLITE_NOMEM;
7513       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
7514         pNew->mUnixTime = zipfileMtime(&pNew->cds);
7515       }
7516     }
7517
7518     if( rc==SQLITE_OK ){
7519       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
7520       ZipfileLFH lfh;
7521       if( pFile ){
7522         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
7523       }else{
7524         aRead = (u8*)&aBlob[pNew->cds.iOffset];
7525       }
7526
7527       rc = zipfileReadLFH(aRead, &lfh);
7528       if( rc==SQLITE_OK ){
7529         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
7530         pNew->iDataOff += lfh.nFile + lfh.nExtra;
7531         if( aBlob && pNew->cds.szCompressed ){
7532           pNew->aData = &pNew->aExtra[nExtra];
7533           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
7534         }
7535       }else{
7536         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
7537             (int)pNew->cds.iOffset
7538         );
7539       }
7540     }
7541
7542     if( rc!=SQLITE_OK ){
7543       zipfileEntryFree(pNew);
7544     }else{
7545       *ppEntry = pNew;
7546     }
7547   }
7548
7549   return rc;
7550 }
7551
7552 /*
7553 ** Advance an ZipfileCsr to its next row of output.
7554 */
7555 static int zipfileNext(sqlite3_vtab_cursor *cur){
7556   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7557   int rc = SQLITE_OK;
7558
7559   if( pCsr->pFile ){
7560     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
7561     zipfileEntryFree(pCsr->pCurrent);
7562     pCsr->pCurrent = 0;
7563     if( pCsr->iNextOff>=iEof ){
7564       pCsr->bEof = 1;
7565     }else{
7566       ZipfileEntry *p = 0;
7567       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
7568       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
7569       if( rc==SQLITE_OK ){
7570         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
7571         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
7572       }
7573       pCsr->pCurrent = p;
7574     }
7575   }else{
7576     if( !pCsr->bNoop ){
7577       pCsr->pCurrent = pCsr->pCurrent->pNext;
7578     }
7579     if( pCsr->pCurrent==0 ){
7580       pCsr->bEof = 1;
7581     }
7582   }
7583
7584   pCsr->bNoop = 0;
7585   return rc;
7586 }
7587
7588 static void zipfileFree(void *p) { 
7589   sqlite3_free(p); 
7590 }
7591
7592 /*
7593 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
7594 ** size is nOut bytes. This function uncompresses the data and sets the
7595 ** return value in context pCtx to the result (a blob).
7596 **
7597 ** If an error occurs, an error code is left in pCtx instead.
7598 */
7599 static void zipfileInflate(
7600   sqlite3_context *pCtx,          /* Store result here */
7601   const u8 *aIn,                  /* Compressed data */
7602   int nIn,                        /* Size of buffer aIn[] in bytes */
7603   int nOut                        /* Expected output size */
7604 ){
7605   u8 *aRes = sqlite3_malloc(nOut);
7606   if( aRes==0 ){
7607     sqlite3_result_error_nomem(pCtx);
7608   }else{
7609     int err;
7610     z_stream str;
7611     memset(&str, 0, sizeof(str));
7612
7613     str.next_in = (Byte*)aIn;
7614     str.avail_in = nIn;
7615     str.next_out = (Byte*)aRes;
7616     str.avail_out = nOut;
7617
7618     err = inflateInit2(&str, -15);
7619     if( err!=Z_OK ){
7620       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
7621     }else{
7622       err = inflate(&str, Z_NO_FLUSH);
7623       if( err!=Z_STREAM_END ){
7624         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
7625       }else{
7626         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
7627         aRes = 0;
7628       }
7629     }
7630     sqlite3_free(aRes);
7631     inflateEnd(&str);
7632   }
7633 }
7634
7635 /*
7636 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
7637 ** compresses it and sets (*ppOut) to point to a buffer containing the
7638 ** compressed data. The caller is responsible for eventually calling
7639 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
7640 ** is set to the size of buffer (*ppOut) in bytes.
7641 **
7642 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
7643 ** code is returned and an error message left in virtual-table handle
7644 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
7645 ** case.
7646 */
7647 static int zipfileDeflate(
7648   const u8 *aIn, int nIn,         /* Input */
7649   u8 **ppOut, int *pnOut,         /* Output */
7650   char **pzErr                    /* OUT: Error message */
7651 ){
7652   int rc = SQLITE_OK;
7653   sqlite3_int64 nAlloc;
7654   z_stream str;
7655   u8 *aOut;
7656
7657   memset(&str, 0, sizeof(str));
7658   str.next_in = (Bytef*)aIn;
7659   str.avail_in = nIn;
7660   deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
7661
7662   nAlloc = deflateBound(&str, nIn);
7663   aOut = (u8*)sqlite3_malloc64(nAlloc);
7664   if( aOut==0 ){
7665     rc = SQLITE_NOMEM;
7666   }else{
7667     int res;
7668     str.next_out = aOut;
7669     str.avail_out = nAlloc;
7670     res = deflate(&str, Z_FINISH);
7671     if( res==Z_STREAM_END ){
7672       *ppOut = aOut;
7673       *pnOut = (int)str.total_out;
7674     }else{
7675       sqlite3_free(aOut);
7676       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
7677       rc = SQLITE_ERROR;
7678     }
7679     deflateEnd(&str);
7680   }
7681
7682   return rc;
7683 }
7684
7685
7686 /*
7687 ** Return values of columns for the row at which the series_cursor
7688 ** is currently pointing.
7689 */
7690 static int zipfileColumn(
7691   sqlite3_vtab_cursor *cur,   /* The cursor */
7692   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
7693   int i                       /* Which column to return */
7694 ){
7695   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7696   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
7697   int rc = SQLITE_OK;
7698   switch( i ){
7699     case 0:   /* name */
7700       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
7701       break;
7702     case 1:   /* mode */
7703       /* TODO: Whether or not the following is correct surely depends on
7704       ** the platform on which the archive was created.  */
7705       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
7706       break;
7707     case 2: { /* mtime */
7708       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
7709       break;
7710     }
7711     case 3: { /* sz */
7712       if( sqlite3_vtab_nochange(ctx)==0 ){
7713         sqlite3_result_int64(ctx, pCDS->szUncompressed);
7714       }
7715       break;
7716     }
7717     case 4:   /* rawdata */
7718       if( sqlite3_vtab_nochange(ctx) ) break;
7719     case 5: { /* data */
7720       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
7721         int sz = pCDS->szCompressed;
7722         int szFinal = pCDS->szUncompressed;
7723         if( szFinal>0 ){
7724           u8 *aBuf;
7725           u8 *aFree = 0;
7726           if( pCsr->pCurrent->aData ){
7727             aBuf = pCsr->pCurrent->aData;
7728           }else{
7729             aBuf = aFree = sqlite3_malloc64(sz);
7730             if( aBuf==0 ){
7731               rc = SQLITE_NOMEM;
7732             }else{
7733               FILE *pFile = pCsr->pFile;
7734               if( pFile==0 ){
7735                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
7736               }
7737               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
7738                   &pCsr->base.pVtab->zErrMsg
7739               );
7740             }
7741           }
7742           if( rc==SQLITE_OK ){
7743             if( i==5 && pCDS->iCompression ){
7744               zipfileInflate(ctx, aBuf, sz, szFinal);
7745             }else{
7746               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
7747             }
7748           }
7749           sqlite3_free(aFree);
7750         }else{
7751           /* Figure out if this is a directory or a zero-sized file. Consider
7752           ** it to be a directory either if the mode suggests so, or if
7753           ** the final character in the name is '/'.  */
7754           u32 mode = pCDS->iExternalAttr >> 16;
7755           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
7756             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
7757           }
7758         }
7759       }
7760       break;
7761     }
7762     case 6:   /* method */
7763       sqlite3_result_int(ctx, pCDS->iCompression);
7764       break;
7765     default:  /* z */
7766       assert( i==7 );
7767       sqlite3_result_int64(ctx, pCsr->iId);
7768       break;
7769   }
7770
7771   return rc;
7772 }
7773
7774 /*
7775 ** Return TRUE if the cursor is at EOF.
7776 */
7777 static int zipfileEof(sqlite3_vtab_cursor *cur){
7778   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7779   return pCsr->bEof;
7780 }
7781
7782 /*
7783 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
7784 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
7785 ** is guaranteed to be a file-handle open on a zip file.
7786 **
7787 ** This function attempts to locate the EOCD record within the zip archive
7788 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
7789 ** returned if successful. Otherwise, an SQLite error code is returned and
7790 ** an English language error message may be left in virtual-table pTab.
7791 */
7792 static int zipfileReadEOCD(
7793   ZipfileTab *pTab,               /* Return errors here */
7794   const u8 *aBlob,                /* Pointer to in-memory file image */
7795   int nBlob,                      /* Size of aBlob[] in bytes */
7796   FILE *pFile,                    /* Read from this file if aBlob==0 */
7797   ZipfileEOCD *pEOCD              /* Object to populate */
7798 ){
7799   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
7800   int nRead;                      /* Bytes to read from file */
7801   int rc = SQLITE_OK;
7802
7803   if( aBlob==0 ){
7804     i64 iOff;                     /* Offset to read from */
7805     i64 szFile;                   /* Total size of file in bytes */
7806     fseek(pFile, 0, SEEK_END);
7807     szFile = (i64)ftell(pFile);
7808     if( szFile==0 ){
7809       memset(pEOCD, 0, sizeof(ZipfileEOCD));
7810       return SQLITE_OK;
7811     }
7812     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
7813     iOff = szFile - nRead;
7814     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
7815   }else{
7816     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
7817     aRead = (u8*)&aBlob[nBlob-nRead];
7818   }
7819
7820   if( rc==SQLITE_OK ){
7821     int i;
7822
7823     /* Scan backwards looking for the signature bytes */
7824     for(i=nRead-20; i>=0; i--){
7825       if( aRead[i]==0x50 && aRead[i+1]==0x4b 
7826        && aRead[i+2]==0x05 && aRead[i+3]==0x06 
7827       ){
7828         break;
7829       }
7830     }
7831     if( i<0 ){
7832       pTab->base.zErrMsg = sqlite3_mprintf(
7833           "cannot find end of central directory record"
7834       );
7835       return SQLITE_ERROR;
7836     }
7837
7838     aRead += i+4;
7839     pEOCD->iDisk = zipfileRead16(aRead);
7840     pEOCD->iFirstDisk = zipfileRead16(aRead);
7841     pEOCD->nEntry = zipfileRead16(aRead);
7842     pEOCD->nEntryTotal = zipfileRead16(aRead);
7843     pEOCD->nSize = zipfileRead32(aRead);
7844     pEOCD->iOffset = zipfileRead32(aRead);
7845   }
7846
7847   return rc;
7848 }
7849
7850 /*
7851 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
7852 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
7853 ** to the end of the list. Otherwise, it is added to the list immediately
7854 ** before pBefore (which is guaranteed to be a part of said list).
7855 */
7856 static void zipfileAddEntry(
7857   ZipfileTab *pTab, 
7858   ZipfileEntry *pBefore, 
7859   ZipfileEntry *pNew
7860 ){
7861   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
7862   assert( pNew->pNext==0 );
7863   if( pBefore==0 ){
7864     if( pTab->pFirstEntry==0 ){
7865       pTab->pFirstEntry = pTab->pLastEntry = pNew;
7866     }else{
7867       assert( pTab->pLastEntry->pNext==0 );
7868       pTab->pLastEntry->pNext = pNew;
7869       pTab->pLastEntry = pNew;
7870     }
7871   }else{
7872     ZipfileEntry **pp;
7873     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
7874     pNew->pNext = pBefore;
7875     *pp = pNew;
7876   }
7877 }
7878
7879 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
7880   ZipfileEOCD eocd;
7881   int rc;
7882   int i;
7883   i64 iOff;
7884
7885   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
7886   iOff = eocd.iOffset;
7887   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
7888     ZipfileEntry *pNew = 0;
7889     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
7890
7891     if( rc==SQLITE_OK ){
7892       zipfileAddEntry(pTab, 0, pNew);
7893       iOff += ZIPFILE_CDS_FIXED_SZ;
7894       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
7895     }
7896   }
7897   return rc;
7898 }
7899
7900 /*
7901 ** xFilter callback.
7902 */
7903 static int zipfileFilter(
7904   sqlite3_vtab_cursor *cur, 
7905   int idxNum, const char *idxStr,
7906   int argc, sqlite3_value **argv
7907 ){
7908   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7909   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7910   const char *zFile = 0;          /* Zip file to scan */
7911   int rc = SQLITE_OK;             /* Return Code */
7912   int bInMemory = 0;              /* True for an in-memory zipfile */
7913
7914   zipfileResetCursor(pCsr);
7915
7916   if( pTab->zFile ){
7917     zFile = pTab->zFile;
7918   }else if( idxNum==0 ){
7919     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7920     return SQLITE_ERROR;
7921   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7922     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7923     int nBlob = sqlite3_value_bytes(argv[0]);
7924     assert( pTab->pFirstEntry==0 );
7925     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7926     pCsr->pFreeEntry = pTab->pFirstEntry;
7927     pTab->pFirstEntry = pTab->pLastEntry = 0;
7928     if( rc!=SQLITE_OK ) return rc;
7929     bInMemory = 1;
7930   }else{
7931     zFile = (const char*)sqlite3_value_text(argv[0]);
7932   }
7933
7934   if( 0==pTab->pWriteFd && 0==bInMemory ){
7935     pCsr->pFile = fopen(zFile, "rb");
7936     if( pCsr->pFile==0 ){
7937       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7938       rc = SQLITE_ERROR;
7939     }else{
7940       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7941       if( rc==SQLITE_OK ){
7942         if( pCsr->eocd.nEntry==0 ){
7943           pCsr->bEof = 1;
7944         }else{
7945           pCsr->iNextOff = pCsr->eocd.iOffset;
7946           rc = zipfileNext(cur);
7947         }
7948       }
7949     }
7950   }else{
7951     pCsr->bNoop = 1;
7952     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
7953     rc = zipfileNext(cur);
7954   }
7955
7956   return rc;
7957 }
7958
7959 /*
7960 ** xBestIndex callback.
7961 */
7962 static int zipfileBestIndex(
7963   sqlite3_vtab *tab,
7964   sqlite3_index_info *pIdxInfo
7965 ){
7966   int i;
7967   int idx = -1;
7968   int unusable = 0;
7969
7970   for(i=0; i<pIdxInfo->nConstraint; i++){
7971     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7972     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
7973     if( pCons->usable==0 ){
7974       unusable = 1;
7975     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7976       idx = i;
7977     }
7978   }
7979   pIdxInfo->estimatedCost = 1000.0;
7980   if( idx>=0 ){
7981     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
7982     pIdxInfo->aConstraintUsage[idx].omit = 1;
7983     pIdxInfo->idxNum = 1;
7984   }else if( unusable ){
7985     return SQLITE_CONSTRAINT;
7986   }
7987   return SQLITE_OK;
7988 }
7989
7990 static ZipfileEntry *zipfileNewEntry(const char *zPath){
7991   ZipfileEntry *pNew;
7992   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
7993   if( pNew ){
7994     memset(pNew, 0, sizeof(ZipfileEntry));
7995     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
7996     if( pNew->cds.zFile==0 ){
7997       sqlite3_free(pNew);
7998       pNew = 0;
7999     }
8000   }
8001   return pNew;
8002 }
8003
8004 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
8005   ZipfileCDS *pCds = &pEntry->cds;
8006   u8 *a = aBuf;
8007
8008   pCds->nExtra = 9;
8009
8010   /* Write the LFH itself */
8011   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
8012   zipfileWrite16(a, pCds->iVersionExtract);
8013   zipfileWrite16(a, pCds->flags);
8014   zipfileWrite16(a, pCds->iCompression);
8015   zipfileWrite16(a, pCds->mTime);
8016   zipfileWrite16(a, pCds->mDate);
8017   zipfileWrite32(a, pCds->crc32);
8018   zipfileWrite32(a, pCds->szCompressed);
8019   zipfileWrite32(a, pCds->szUncompressed);
8020   zipfileWrite16(a, (u16)pCds->nFile);
8021   zipfileWrite16(a, pCds->nExtra);
8022   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
8023
8024   /* Add the file name */
8025   memcpy(a, pCds->zFile, (int)pCds->nFile);
8026   a += (int)pCds->nFile;
8027
8028   /* The "extra" data */
8029   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8030   zipfileWrite16(a, 5);
8031   *a++ = 0x01;
8032   zipfileWrite32(a, pEntry->mUnixTime);
8033
8034   return a-aBuf;
8035 }
8036
8037 static int zipfileAppendEntry(
8038   ZipfileTab *pTab,
8039   ZipfileEntry *pEntry,
8040   const u8 *pData,
8041   int nData
8042 ){
8043   u8 *aBuf = pTab->aBuffer;
8044   int nBuf;
8045   int rc;
8046
8047   nBuf = zipfileSerializeLFH(pEntry, aBuf);
8048   rc = zipfileAppendData(pTab, aBuf, nBuf);
8049   if( rc==SQLITE_OK ){
8050     pEntry->iDataOff = pTab->szCurrent;
8051     rc = zipfileAppendData(pTab, pData, nData);
8052   }
8053
8054   return rc;
8055 }
8056
8057 static int zipfileGetMode(
8058   sqlite3_value *pVal, 
8059   int bIsDir,                     /* If true, default to directory */
8060   u32 *pMode,                     /* OUT: Mode value */
8061   char **pzErr                    /* OUT: Error message */
8062 ){
8063   const char *z = (const char*)sqlite3_value_text(pVal);
8064   u32 mode = 0;
8065   if( z==0 ){
8066     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
8067   }else if( z[0]>='0' && z[0]<='9' ){
8068     mode = (unsigned int)sqlite3_value_int(pVal);
8069   }else{
8070     const char zTemplate[11] = "-rwxrwxrwx";
8071     int i;
8072     if( strlen(z)!=10 ) goto parse_error;
8073     switch( z[0] ){
8074       case '-': mode |= S_IFREG; break;
8075       case 'd': mode |= S_IFDIR; break;
8076       case 'l': mode |= S_IFLNK; break;
8077       default: goto parse_error;
8078     }
8079     for(i=1; i<10; i++){
8080       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
8081       else if( z[i]!='-' ) goto parse_error;
8082     }
8083   }
8084   if( ((mode & S_IFDIR)==0)==bIsDir ){
8085     /* The "mode" attribute is a directory, but data has been specified.
8086     ** Or vice-versa - no data but "mode" is a file or symlink.  */
8087     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
8088     return SQLITE_CONSTRAINT;
8089   }
8090   *pMode = mode;
8091   return SQLITE_OK;
8092
8093  parse_error:
8094   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
8095   return SQLITE_ERROR;
8096 }
8097
8098 /*
8099 ** Both (const char*) arguments point to nul-terminated strings. Argument
8100 ** nB is the value of strlen(zB). This function returns 0 if the strings are
8101 ** identical, ignoring any trailing '/' character in either path.  */
8102 static int zipfileComparePath(const char *zA, const char *zB, int nB){
8103   int nA = (int)strlen(zA);
8104   if( nA>0 && zA[nA-1]=='/' ) nA--;
8105   if( nB>0 && zB[nB-1]=='/' ) nB--;
8106   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
8107   return 1;
8108 }
8109
8110 static int zipfileBegin(sqlite3_vtab *pVtab){
8111   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8112   int rc = SQLITE_OK;
8113
8114   assert( pTab->pWriteFd==0 );
8115   if( pTab->zFile==0 || pTab->zFile[0]==0 ){
8116     pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
8117     return SQLITE_ERROR;
8118   }
8119
8120   /* Open a write fd on the file. Also load the entire central directory
8121   ** structure into memory. During the transaction any new file data is 
8122   ** appended to the archive file, but the central directory is accumulated
8123   ** in main-memory until the transaction is committed.  */
8124   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
8125   if( pTab->pWriteFd==0 ){
8126     pTab->base.zErrMsg = sqlite3_mprintf(
8127         "zipfile: failed to open file %s for writing", pTab->zFile
8128         );
8129     rc = SQLITE_ERROR;
8130   }else{
8131     fseek(pTab->pWriteFd, 0, SEEK_END);
8132     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
8133     rc = zipfileLoadDirectory(pTab, 0, 0);
8134   }
8135
8136   if( rc!=SQLITE_OK ){
8137     zipfileCleanupTransaction(pTab);
8138   }
8139
8140   return rc;
8141 }
8142
8143 /*
8144 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
8145 ** time(2)).
8146 */
8147 static u32 zipfileTime(void){
8148   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
8149   u32 ret;
8150   if( pVfs==0 ) return 0;
8151   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
8152     i64 ms;
8153     pVfs->xCurrentTimeInt64(pVfs, &ms);
8154     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
8155   }else{
8156     double day;
8157     pVfs->xCurrentTime(pVfs, &day);
8158     ret = (u32)((day - 2440587.5) * 86400);
8159   }
8160   return ret;
8161 }
8162
8163 /*
8164 ** Return a 32-bit timestamp in UNIX epoch format.
8165 **
8166 ** If the value passed as the only argument is either NULL or an SQL NULL,
8167 ** return the current time. Otherwise, return the value stored in (*pVal)
8168 ** cast to a 32-bit unsigned integer.
8169 */
8170 static u32 zipfileGetTime(sqlite3_value *pVal){
8171   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
8172     return zipfileTime();
8173   }
8174   return (u32)sqlite3_value_int64(pVal);
8175 }
8176
8177 /*
8178 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
8179 ** linked list.  Remove it from the list and free the object.
8180 */
8181 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
8182   if( pOld ){
8183     ZipfileEntry **pp;
8184     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
8185     *pp = (*pp)->pNext;
8186     zipfileEntryFree(pOld);
8187   }
8188 }
8189
8190 /*
8191 ** xUpdate method.
8192 */
8193 static int zipfileUpdate(
8194   sqlite3_vtab *pVtab, 
8195   int nVal, 
8196   sqlite3_value **apVal, 
8197   sqlite_int64 *pRowid
8198 ){
8199   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8200   int rc = SQLITE_OK;             /* Return Code */
8201   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
8202
8203   u32 mode = 0;                   /* Mode for new entry */
8204   u32 mTime = 0;                  /* Modification time for new entry */
8205   i64 sz = 0;                     /* Uncompressed size */
8206   const char *zPath = 0;          /* Path for new entry */
8207   int nPath = 0;                  /* strlen(zPath) */
8208   const u8 *pData = 0;            /* Pointer to buffer containing content */
8209   int nData = 0;                  /* Size of pData buffer in bytes */
8210   int iMethod = 0;                /* Compression method for new entry */
8211   u8 *pFree = 0;                  /* Free this */
8212   char *zFree = 0;                /* Also free this */
8213   ZipfileEntry *pOld = 0;
8214   ZipfileEntry *pOld2 = 0;
8215   int bUpdate = 0;                /* True for an update that modifies "name" */
8216   int bIsDir = 0;
8217   u32 iCrc32 = 0;
8218
8219   if( pTab->pWriteFd==0 ){
8220     rc = zipfileBegin(pVtab);
8221     if( rc!=SQLITE_OK ) return rc;
8222   }
8223
8224   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
8225   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
8226     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
8227     int nDelete = (int)strlen(zDelete);
8228     if( nVal>1 ){
8229       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
8230       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
8231         bUpdate = 1;
8232       }
8233     }
8234     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
8235       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
8236         break;
8237       }
8238       assert( pOld->pNext );
8239     }
8240   }
8241
8242   if( nVal>1 ){
8243     /* Check that "sz" and "rawdata" are both NULL: */
8244     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
8245       zipfileTableErr(pTab, "sz must be NULL");
8246       rc = SQLITE_CONSTRAINT;
8247     }
8248     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
8249       zipfileTableErr(pTab, "rawdata must be NULL"); 
8250       rc = SQLITE_CONSTRAINT;
8251     }
8252
8253     if( rc==SQLITE_OK ){
8254       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
8255         /* data=NULL. A directory */
8256         bIsDir = 1;
8257       }else{
8258         /* Value specified for "data", and possibly "method". This must be
8259         ** a regular file or a symlink. */
8260         const u8 *aIn = sqlite3_value_blob(apVal[7]);
8261         int nIn = sqlite3_value_bytes(apVal[7]);
8262         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
8263
8264         iMethod = sqlite3_value_int(apVal[8]);
8265         sz = nIn;
8266         pData = aIn;
8267         nData = nIn;
8268         if( iMethod!=0 && iMethod!=8 ){
8269           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
8270           rc = SQLITE_CONSTRAINT;
8271         }else{
8272           if( bAuto || iMethod ){
8273             int nCmp;
8274             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
8275             if( rc==SQLITE_OK ){
8276               if( iMethod || nCmp<nIn ){
8277                 iMethod = 8;
8278                 pData = pFree;
8279                 nData = nCmp;
8280               }
8281             }
8282           }
8283           iCrc32 = crc32(0, aIn, nIn);
8284         }
8285       }
8286     }
8287
8288     if( rc==SQLITE_OK ){
8289       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
8290     }
8291
8292     if( rc==SQLITE_OK ){
8293       zPath = (const char*)sqlite3_value_text(apVal[2]);
8294       if( zPath==0 ) zPath = "";
8295       nPath = (int)strlen(zPath);
8296       mTime = zipfileGetTime(apVal[4]);
8297     }
8298
8299     if( rc==SQLITE_OK && bIsDir ){
8300       /* For a directory, check that the last character in the path is a
8301       ** '/'. This appears to be required for compatibility with info-zip
8302       ** (the unzip command on unix). It does not create directories
8303       ** otherwise.  */
8304       if( nPath<=0 || zPath[nPath-1]!='/' ){
8305         zFree = sqlite3_mprintf("%s/", zPath);
8306         zPath = (const char*)zFree;
8307         if( zFree==0 ){
8308           rc = SQLITE_NOMEM;
8309           nPath = 0;
8310         }else{
8311           nPath = (int)strlen(zPath);
8312         }
8313       }
8314     }
8315
8316     /* Check that we're not inserting a duplicate entry -OR- updating an
8317     ** entry with a path, thereby making it into a duplicate. */
8318     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
8319       ZipfileEntry *p;
8320       for(p=pTab->pFirstEntry; p; p=p->pNext){
8321         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
8322           switch( sqlite3_vtab_on_conflict(pTab->db) ){
8323             case SQLITE_IGNORE: {
8324               goto zipfile_update_done;
8325             }
8326             case SQLITE_REPLACE: {
8327               pOld2 = p;
8328               break;
8329             }
8330             default: {
8331               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
8332               rc = SQLITE_CONSTRAINT;
8333               break;
8334             }
8335           }
8336           break;
8337         }
8338       }
8339     }
8340
8341     if( rc==SQLITE_OK ){
8342       /* Create the new CDS record. */
8343       pNew = zipfileNewEntry(zPath);
8344       if( pNew==0 ){
8345         rc = SQLITE_NOMEM;
8346       }else{
8347         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8348         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8349         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8350         pNew->cds.iCompression = (u16)iMethod;
8351         zipfileMtimeToDos(&pNew->cds, mTime);
8352         pNew->cds.crc32 = iCrc32;
8353         pNew->cds.szCompressed = nData;
8354         pNew->cds.szUncompressed = (u32)sz;
8355         pNew->cds.iExternalAttr = (mode<<16);
8356         pNew->cds.iOffset = (u32)pTab->szCurrent;
8357         pNew->cds.nFile = (u16)nPath;
8358         pNew->mUnixTime = (u32)mTime;
8359         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
8360         zipfileAddEntry(pTab, pOld, pNew);
8361       }
8362     }
8363   }
8364
8365   if( rc==SQLITE_OK && (pOld || pOld2) ){
8366     ZipfileCsr *pCsr;
8367     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8368       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
8369         pCsr->pCurrent = pCsr->pCurrent->pNext;
8370         pCsr->bNoop = 1;
8371       }
8372     }
8373
8374     zipfileRemoveEntryFromList(pTab, pOld);
8375     zipfileRemoveEntryFromList(pTab, pOld2);
8376   }
8377
8378 zipfile_update_done:
8379   sqlite3_free(pFree);
8380   sqlite3_free(zFree);
8381   return rc;
8382 }
8383
8384 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
8385   u8 *a = aBuf;
8386   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
8387   zipfileWrite16(a, p->iDisk);
8388   zipfileWrite16(a, p->iFirstDisk);
8389   zipfileWrite16(a, p->nEntry);
8390   zipfileWrite16(a, p->nEntryTotal);
8391   zipfileWrite32(a, p->nSize);
8392   zipfileWrite32(a, p->iOffset);
8393   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
8394
8395   return a-aBuf;
8396 }
8397
8398 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
8399   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
8400   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
8401   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
8402 }
8403
8404 /*
8405 ** Serialize the CDS structure into buffer aBuf[]. Return the number
8406 ** of bytes written.
8407 */
8408 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
8409   u8 *a = aBuf;
8410   ZipfileCDS *pCDS = &pEntry->cds;
8411
8412   if( pEntry->aExtra==0 ){
8413     pCDS->nExtra = 9;
8414   }
8415
8416   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
8417   zipfileWrite16(a, pCDS->iVersionMadeBy);
8418   zipfileWrite16(a, pCDS->iVersionExtract);
8419   zipfileWrite16(a, pCDS->flags);
8420   zipfileWrite16(a, pCDS->iCompression);
8421   zipfileWrite16(a, pCDS->mTime);
8422   zipfileWrite16(a, pCDS->mDate);
8423   zipfileWrite32(a, pCDS->crc32);
8424   zipfileWrite32(a, pCDS->szCompressed);
8425   zipfileWrite32(a, pCDS->szUncompressed);
8426   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
8427   zipfileWrite16(a, pCDS->nFile);
8428   zipfileWrite16(a, pCDS->nExtra);
8429   zipfileWrite16(a, pCDS->nComment);
8430   zipfileWrite16(a, pCDS->iDiskStart);
8431   zipfileWrite16(a, pCDS->iInternalAttr);
8432   zipfileWrite32(a, pCDS->iExternalAttr);
8433   zipfileWrite32(a, pCDS->iOffset);
8434
8435   memcpy(a, pCDS->zFile, pCDS->nFile);
8436   a += pCDS->nFile;
8437
8438   if( pEntry->aExtra ){
8439     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
8440     memcpy(a, pEntry->aExtra, n);
8441     a += n;
8442   }else{
8443     assert( pCDS->nExtra==9 );
8444     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8445     zipfileWrite16(a, 5);
8446     *a++ = 0x01;
8447     zipfileWrite32(a, pEntry->mUnixTime);
8448   }
8449
8450   return a-aBuf;
8451 }
8452
8453 static int zipfileCommit(sqlite3_vtab *pVtab){
8454   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8455   int rc = SQLITE_OK;
8456   if( pTab->pWriteFd ){
8457     i64 iOffset = pTab->szCurrent;
8458     ZipfileEntry *p;
8459     ZipfileEOCD eocd;
8460     int nEntry = 0;
8461
8462     /* Write out all entries */
8463     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
8464       int n = zipfileSerializeCDS(p, pTab->aBuffer);
8465       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
8466       nEntry++;
8467     }
8468
8469     /* Write out the EOCD record */
8470     eocd.iDisk = 0;
8471     eocd.iFirstDisk = 0;
8472     eocd.nEntry = (u16)nEntry;
8473     eocd.nEntryTotal = (u16)nEntry;
8474     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
8475     eocd.iOffset = (u32)iOffset;
8476     rc = zipfileAppendEOCD(pTab, &eocd);
8477
8478     zipfileCleanupTransaction(pTab);
8479   }
8480   return rc;
8481 }
8482
8483 static int zipfileRollback(sqlite3_vtab *pVtab){
8484   return zipfileCommit(pVtab);
8485 }
8486
8487 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
8488   ZipfileCsr *pCsr;
8489   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8490     if( iId==pCsr->iId ) break;
8491   }
8492   return pCsr;
8493 }
8494
8495 static void zipfileFunctionCds(
8496   sqlite3_context *context,
8497   int argc,
8498   sqlite3_value **argv
8499 ){
8500   ZipfileCsr *pCsr;
8501   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
8502   assert( argc>0 );
8503
8504   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
8505   if( pCsr ){
8506     ZipfileCDS *p = &pCsr->pCurrent->cds;
8507     char *zRes = sqlite3_mprintf("{"
8508         "\"version-made-by\" : %u, "
8509         "\"version-to-extract\" : %u, "
8510         "\"flags\" : %u, "
8511         "\"compression\" : %u, "
8512         "\"time\" : %u, "
8513         "\"date\" : %u, "
8514         "\"crc32\" : %u, "
8515         "\"compressed-size\" : %u, "
8516         "\"uncompressed-size\" : %u, "
8517         "\"file-name-length\" : %u, "
8518         "\"extra-field-length\" : %u, "
8519         "\"file-comment-length\" : %u, "
8520         "\"disk-number-start\" : %u, "
8521         "\"internal-attr\" : %u, "
8522         "\"external-attr\" : %u, "
8523         "\"offset\" : %u }",
8524         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
8525         (u32)p->flags, (u32)p->iCompression,
8526         (u32)p->mTime, (u32)p->mDate,
8527         (u32)p->crc32, (u32)p->szCompressed,
8528         (u32)p->szUncompressed, (u32)p->nFile,
8529         (u32)p->nExtra, (u32)p->nComment,
8530         (u32)p->iDiskStart, (u32)p->iInternalAttr,
8531         (u32)p->iExternalAttr, (u32)p->iOffset
8532     );
8533
8534     if( zRes==0 ){
8535       sqlite3_result_error_nomem(context);
8536     }else{
8537       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
8538       sqlite3_free(zRes);
8539     }
8540   }
8541 }
8542
8543 /*
8544 ** xFindFunction method.
8545 */
8546 static int zipfileFindFunction(
8547   sqlite3_vtab *pVtab,            /* Virtual table handle */
8548   int nArg,                       /* Number of SQL function arguments */
8549   const char *zName,              /* Name of SQL function */
8550   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
8551   void **ppArg                    /* OUT: User data for *pxFunc */
8552 ){
8553   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
8554     *pxFunc = zipfileFunctionCds;
8555     *ppArg = (void*)pVtab;
8556     return 1;
8557   }
8558   return 0;
8559 }
8560
8561 typedef struct ZipfileBuffer ZipfileBuffer;
8562 struct ZipfileBuffer {
8563   u8 *a;                          /* Pointer to buffer */
8564   int n;                          /* Size of buffer in bytes */
8565   int nAlloc;                     /* Byte allocated at a[] */
8566 };
8567
8568 typedef struct ZipfileCtx ZipfileCtx;
8569 struct ZipfileCtx {
8570   int nEntry;
8571   ZipfileBuffer body;
8572   ZipfileBuffer cds;
8573 };
8574
8575 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
8576   if( pBuf->n+nByte>pBuf->nAlloc ){
8577     u8 *aNew;
8578     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
8579     int nReq = pBuf->n + nByte;
8580
8581     while( nNew<nReq ) nNew = nNew*2;
8582     aNew = sqlite3_realloc64(pBuf->a, nNew);
8583     if( aNew==0 ) return SQLITE_NOMEM;
8584     pBuf->a = aNew;
8585     pBuf->nAlloc = (int)nNew;
8586   }
8587   return SQLITE_OK;
8588 }
8589
8590 /*
8591 ** xStep() callback for the zipfile() aggregate. This can be called in
8592 ** any of the following ways:
8593 **
8594 **   SELECT zipfile(name,data) ...
8595 **   SELECT zipfile(name,mode,mtime,data) ...
8596 **   SELECT zipfile(name,mode,mtime,data,method) ...
8597 */
8598 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
8599   ZipfileCtx *p;                  /* Aggregate function context */
8600   ZipfileEntry e;                 /* New entry to add to zip archive */
8601
8602   sqlite3_value *pName = 0;
8603   sqlite3_value *pMode = 0;
8604   sqlite3_value *pMtime = 0;
8605   sqlite3_value *pData = 0;
8606   sqlite3_value *pMethod = 0;
8607
8608   int bIsDir = 0;
8609   u32 mode;
8610   int rc = SQLITE_OK;
8611   char *zErr = 0;
8612
8613   int iMethod = -1;               /* Compression method to use (0 or 8) */
8614
8615   const u8 *aData = 0;            /* Possibly compressed data for new entry */
8616   int nData = 0;                  /* Size of aData[] in bytes */
8617   int szUncompressed = 0;         /* Size of data before compression */
8618   u8 *aFree = 0;                  /* Free this before returning */
8619   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
8620
8621   char *zName = 0;                /* Path (name) of new entry */
8622   int nName = 0;                  /* Size of zName in bytes */
8623   char *zFree = 0;                /* Free this before returning */
8624   int nByte;
8625
8626   memset(&e, 0, sizeof(e));
8627   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8628   if( p==0 ) return;
8629
8630   /* Martial the arguments into stack variables */
8631   if( nVal!=2 && nVal!=4 && nVal!=5 ){
8632     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
8633     rc = SQLITE_ERROR;
8634     goto zipfile_step_out;
8635   }
8636   pName = apVal[0];
8637   if( nVal==2 ){
8638     pData = apVal[1];
8639   }else{
8640     pMode = apVal[1];
8641     pMtime = apVal[2];
8642     pData = apVal[3];
8643     if( nVal==5 ){
8644       pMethod = apVal[4];
8645     }
8646   }
8647
8648   /* Check that the 'name' parameter looks ok. */
8649   zName = (char*)sqlite3_value_text(pName);
8650   nName = sqlite3_value_bytes(pName);
8651   if( zName==0 ){
8652     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
8653     rc = SQLITE_ERROR;
8654     goto zipfile_step_out;
8655   }
8656
8657   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
8658   ** deflate compression) or NULL (choose automatically).  */
8659   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
8660     iMethod = (int)sqlite3_value_int64(pMethod);
8661     if( iMethod!=0 && iMethod!=8 ){
8662       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
8663       rc = SQLITE_ERROR;
8664       goto zipfile_step_out;
8665     }
8666   }
8667
8668   /* Now inspect the data. If this is NULL, then the new entry must be a
8669   ** directory.  Otherwise, figure out whether or not the data should
8670   ** be deflated or simply stored in the zip archive. */
8671   if( sqlite3_value_type(pData)==SQLITE_NULL ){
8672     bIsDir = 1;
8673     iMethod = 0;
8674   }else{
8675     aData = sqlite3_value_blob(pData);
8676     szUncompressed = nData = sqlite3_value_bytes(pData);
8677     iCrc32 = crc32(0, aData, nData);
8678     if( iMethod<0 || iMethod==8 ){
8679       int nOut = 0;
8680       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
8681       if( rc!=SQLITE_OK ){
8682         goto zipfile_step_out;
8683       }
8684       if( iMethod==8 || nOut<nData ){
8685         aData = aFree;
8686         nData = nOut;
8687         iMethod = 8;
8688       }else{
8689         iMethod = 0;
8690       }
8691     }
8692   }
8693
8694   /* Decode the "mode" argument. */
8695   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
8696   if( rc ) goto zipfile_step_out;
8697
8698   /* Decode the "mtime" argument. */
8699   e.mUnixTime = zipfileGetTime(pMtime);
8700
8701   /* If this is a directory entry, ensure that there is exactly one '/'
8702   ** at the end of the path. Or, if this is not a directory and the path
8703   ** ends in '/' it is an error. */
8704   if( bIsDir==0 ){
8705     if( nName>0 && zName[nName-1]=='/' ){
8706       zErr = sqlite3_mprintf("non-directory name must not end with /");
8707       rc = SQLITE_ERROR;
8708       goto zipfile_step_out;
8709     }
8710   }else{
8711     if( nName==0 || zName[nName-1]!='/' ){
8712       zName = zFree = sqlite3_mprintf("%s/", zName);
8713       if( zName==0 ){
8714         rc = SQLITE_NOMEM;
8715         goto zipfile_step_out;
8716       }
8717       nName = (int)strlen(zName);
8718     }else{
8719       while( nName>1 && zName[nName-2]=='/' ) nName--;
8720     }
8721   }
8722
8723   /* Assemble the ZipfileEntry object for the new zip archive entry */
8724   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8725   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8726   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8727   e.cds.iCompression = (u16)iMethod;
8728   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
8729   e.cds.crc32 = iCrc32;
8730   e.cds.szCompressed = nData;
8731   e.cds.szUncompressed = szUncompressed;
8732   e.cds.iExternalAttr = (mode<<16);
8733   e.cds.iOffset = p->body.n;
8734   e.cds.nFile = (u16)nName;
8735   e.cds.zFile = zName;
8736
8737   /* Append the LFH to the body of the new archive */
8738   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
8739   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
8740   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
8741
8742   /* Append the data to the body of the new archive */
8743   if( nData>0 ){
8744     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
8745     memcpy(&p->body.a[p->body.n], aData, nData);
8746     p->body.n += nData;
8747   }
8748
8749   /* Append the CDS record to the directory of the new archive */
8750   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
8751   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
8752   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
8753
8754   /* Increment the count of entries in the archive */
8755   p->nEntry++;
8756
8757  zipfile_step_out:
8758   sqlite3_free(aFree);
8759   sqlite3_free(zFree);
8760   if( rc ){
8761     if( zErr ){
8762       sqlite3_result_error(pCtx, zErr, -1);
8763     }else{
8764       sqlite3_result_error_code(pCtx, rc);
8765     }
8766   }
8767   sqlite3_free(zErr);
8768 }
8769
8770 /*
8771 ** xFinalize() callback for zipfile aggregate function.
8772 */
8773 void zipfileFinal(sqlite3_context *pCtx){
8774   ZipfileCtx *p;
8775   ZipfileEOCD eocd;
8776   sqlite3_int64 nZip;
8777   u8 *aZip;
8778
8779   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8780   if( p==0 ) return;
8781   if( p->nEntry>0 ){
8782     memset(&eocd, 0, sizeof(eocd));
8783     eocd.nEntry = (u16)p->nEntry;
8784     eocd.nEntryTotal = (u16)p->nEntry;
8785     eocd.nSize = p->cds.n;
8786     eocd.iOffset = p->body.n;
8787
8788     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
8789     aZip = (u8*)sqlite3_malloc64(nZip);
8790     if( aZip==0 ){
8791       sqlite3_result_error_nomem(pCtx);
8792     }else{
8793       memcpy(aZip, p->body.a, p->body.n);
8794       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
8795       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
8796       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
8797     }
8798   }
8799
8800   sqlite3_free(p->body.a);
8801   sqlite3_free(p->cds.a);
8802 }
8803
8804
8805 /*
8806 ** Register the "zipfile" virtual table.
8807 */
8808 static int zipfileRegister(sqlite3 *db){
8809   static sqlite3_module zipfileModule = {
8810     1,                         /* iVersion */
8811     zipfileConnect,            /* xCreate */
8812     zipfileConnect,            /* xConnect */
8813     zipfileBestIndex,          /* xBestIndex */
8814     zipfileDisconnect,         /* xDisconnect */
8815     zipfileDisconnect,         /* xDestroy */
8816     zipfileOpen,               /* xOpen - open a cursor */
8817     zipfileClose,              /* xClose - close a cursor */
8818     zipfileFilter,             /* xFilter - configure scan constraints */
8819     zipfileNext,               /* xNext - advance a cursor */
8820     zipfileEof,                /* xEof - check for end of scan */
8821     zipfileColumn,             /* xColumn - read data */
8822     0,                         /* xRowid - read data */
8823     zipfileUpdate,             /* xUpdate */
8824     zipfileBegin,              /* xBegin */
8825     0,                         /* xSync */
8826     zipfileCommit,             /* xCommit */
8827     zipfileRollback,           /* xRollback */
8828     zipfileFindFunction,       /* xFindMethod */
8829     0,                         /* xRename */
8830   };
8831
8832   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
8833   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
8834   if( rc==SQLITE_OK ){
8835     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
8836         zipfileStep, zipfileFinal
8837     );
8838   }
8839   assert( sizeof(i64)==8 );
8840   assert( sizeof(u32)==4 );
8841   assert( sizeof(u16)==2 );
8842   assert( sizeof(u8)==1 );
8843   return rc;
8844 }
8845 #else         /* SQLITE_OMIT_VIRTUALTABLE */
8846 # define zipfileRegister(x) SQLITE_OK
8847 #endif
8848
8849 #ifdef _WIN32
8850
8851 #endif
8852 int sqlite3_zipfile_init(
8853   sqlite3 *db, 
8854   char **pzErrMsg, 
8855   const sqlite3_api_routines *pApi
8856 ){
8857   SQLITE_EXTENSION_INIT2(pApi);
8858   (void)pzErrMsg;  /* Unused parameter */
8859   return zipfileRegister(db);
8860 }
8861
8862 /************************* End ../ext/misc/zipfile.c ********************/
8863 /************************* Begin ../ext/misc/sqlar.c ******************/
8864 /*
8865 ** 2017-12-17
8866 **
8867 ** The author disclaims copyright to this source code.  In place of
8868 ** a legal notice, here is a blessing:
8869 **
8870 **    May you do good and not evil.
8871 **    May you find forgiveness for yourself and forgive others.
8872 **    May you share freely, never taking more than you give.
8873 **
8874 ******************************************************************************
8875 **
8876 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
8877 ** for working with sqlar archives and used by the shell tool's built-in
8878 ** sqlar support.
8879 */
8880 /* #include "sqlite3ext.h" */
8881 SQLITE_EXTENSION_INIT1
8882 #include <zlib.h>
8883 #include <assert.h>
8884
8885 /*
8886 ** Implementation of the "sqlar_compress(X)" SQL function.
8887 **
8888 ** If the type of X is SQLITE_BLOB, and compressing that blob using
8889 ** zlib utility function compress() yields a smaller blob, return the
8890 ** compressed blob. Otherwise, return a copy of X.
8891 **
8892 ** SQLar uses the "zlib format" for compressed content.  The zlib format
8893 ** contains a two-byte identification header and a four-byte checksum at
8894 ** the end.  This is different from ZIP which uses the raw deflate format.
8895 **
8896 ** Future enhancements to SQLar might add support for new compression formats.
8897 ** If so, those new formats will be identified by alternative headers in the
8898 ** compressed data.
8899 */
8900 static void sqlarCompressFunc(
8901   sqlite3_context *context,
8902   int argc,
8903   sqlite3_value **argv
8904 ){
8905   assert( argc==1 );
8906   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8907     const Bytef *pData = sqlite3_value_blob(argv[0]);
8908     uLong nData = sqlite3_value_bytes(argv[0]);
8909     uLongf nOut = compressBound(nData);
8910     Bytef *pOut;
8911
8912     pOut = (Bytef*)sqlite3_malloc(nOut);
8913     if( pOut==0 ){
8914       sqlite3_result_error_nomem(context);
8915       return;
8916     }else{
8917       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8918         sqlite3_result_error(context, "error in compress()", -1);
8919       }else if( nOut<nData ){
8920         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8921       }else{
8922         sqlite3_result_value(context, argv[0]);
8923       }
8924       sqlite3_free(pOut);
8925     }
8926   }else{
8927     sqlite3_result_value(context, argv[0]);
8928   }
8929 }
8930
8931 /*
8932 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8933 **
8934 ** Parameter SZ is interpreted as an integer. If it is less than or
8935 ** equal to zero, then this function returns a copy of X. Or, if
8936 ** SZ is equal to the size of X when interpreted as a blob, also
8937 ** return a copy of X. Otherwise, decompress blob X using zlib
8938 ** utility function uncompress() and return the results (another
8939 ** blob).
8940 */
8941 static void sqlarUncompressFunc(
8942   sqlite3_context *context,
8943   int argc,
8944   sqlite3_value **argv
8945 ){
8946   uLong nData;
8947   uLongf sz;
8948
8949   assert( argc==2 );
8950   sz = sqlite3_value_int(argv[1]);
8951
8952   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
8953     sqlite3_result_value(context, argv[0]);
8954   }else{
8955     const Bytef *pData= sqlite3_value_blob(argv[0]);
8956     Bytef *pOut = sqlite3_malloc(sz);
8957     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
8958       sqlite3_result_error(context, "error in uncompress()", -1);
8959     }else{
8960       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
8961     }
8962     sqlite3_free(pOut);
8963   }
8964 }
8965
8966
8967 #ifdef _WIN32
8968
8969 #endif
8970 int sqlite3_sqlar_init(
8971   sqlite3 *db, 
8972   char **pzErrMsg, 
8973   const sqlite3_api_routines *pApi
8974 ){
8975   int rc = SQLITE_OK;
8976   SQLITE_EXTENSION_INIT2(pApi);
8977   (void)pzErrMsg;  /* Unused parameter */
8978   rc = sqlite3_create_function(db, "sqlar_compress", 1, 
8979                                SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8980                                sqlarCompressFunc, 0, 0);
8981   if( rc==SQLITE_OK ){
8982     rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
8983                                  SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8984                                  sqlarUncompressFunc, 0, 0);
8985   }
8986   return rc;
8987 }
8988
8989 /************************* End ../ext/misc/sqlar.c ********************/
8990 #endif
8991 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
8992 /*
8993 ** 2017 April 07
8994 **
8995 ** The author disclaims copyright to this source code.  In place of
8996 ** a legal notice, here is a blessing:
8997 **
8998 **    May you do good and not evil.
8999 **    May you find forgiveness for yourself and forgive others.
9000 **    May you share freely, never taking more than you give.
9001 **
9002 *************************************************************************
9003 */
9004 #if !defined(SQLITEEXPERT_H)
9005 #define SQLITEEXPERT_H 1
9006 /* #include "sqlite3.h" */
9007
9008 typedef struct sqlite3expert sqlite3expert;
9009
9010 /*
9011 ** Create a new sqlite3expert object.
9012 **
9013 ** If successful, a pointer to the new object is returned and (*pzErr) set
9014 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
9015 ** an English-language error message. In this case it is the responsibility
9016 ** of the caller to eventually free the error message buffer using
9017 ** sqlite3_free().
9018 */
9019 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
9020
9021 /*
9022 ** Configure an sqlite3expert object.
9023 **
9024 ** EXPERT_CONFIG_SAMPLE:
9025 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
9026 **   each candidate index. This involves scanning and sorting the entire
9027 **   contents of each user database table once for each candidate index
9028 **   associated with the table. For large databases, this can be 
9029 **   prohibitively slow. This option allows the sqlite3expert object to
9030 **   be configured so that sqlite_stat1 data is instead generated based on a
9031 **   subset of each table, or so that no sqlite_stat1 data is used at all.
9032 **
9033 **   A single integer argument is passed to this option. If the value is less
9034 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
9035 **   the analysis - indexes are recommended based on the database schema only.
9036 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
9037 **   generated for each candidate index (this is the default). Finally, if the
9038 **   value falls between 0 and 100, then it represents the percentage of user
9039 **   table rows that should be considered when generating sqlite_stat1 data.
9040 **
9041 **   Examples:
9042 **
9043 **     // Do not generate any sqlite_stat1 data
9044 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
9045 **
9046 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
9047 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
9048 */
9049 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
9050
9051 #define EXPERT_CONFIG_SAMPLE 1    /* int */
9052
9053 /*
9054 ** Specify zero or more SQL statements to be included in the analysis.
9055 **
9056 ** Buffer zSql must contain zero or more complete SQL statements. This
9057 ** function parses all statements contained in the buffer and adds them
9058 ** to the internal list of statements to analyze. If successful, SQLITE_OK
9059 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
9060 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
9061 ** may be set to point to an English language error message. In this case
9062 ** the caller is responsible for eventually freeing the error message buffer
9063 ** using sqlite3_free().
9064 **
9065 ** If an error does occur while processing one of the statements in the
9066 ** buffer passed as the second argument, none of the statements in the
9067 ** buffer are added to the analysis.
9068 **
9069 ** This function must be called before sqlite3_expert_analyze(). If a call
9070 ** to this function is made on an sqlite3expert object that has already
9071 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
9072 ** immediately and no statements are added to the analysis.
9073 */
9074 int sqlite3_expert_sql(
9075   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
9076   const char *zSql,               /* SQL statement(s) to add */
9077   char **pzErr                    /* OUT: Error message (if any) */
9078 );
9079
9080
9081 /*
9082 ** This function is called after the sqlite3expert object has been configured
9083 ** with all SQL statements using sqlite3_expert_sql() to actually perform
9084 ** the analysis. Once this function has been called, it is not possible to
9085 ** add further SQL statements to the analysis.
9086 **
9087 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
9088 ** an error occurs, an SQLite error code is returned and (*pzErr) set to 
9089 ** point to a buffer containing an English language error message. In this
9090 ** case it is the responsibility of the caller to eventually free the buffer
9091 ** using sqlite3_free().
9092 **
9093 ** If an error does occur within this function, the sqlite3expert object
9094 ** is no longer useful for any purpose. At that point it is no longer
9095 ** possible to add further SQL statements to the object or to re-attempt
9096 ** the analysis. The sqlite3expert object must still be freed using a call
9097 ** sqlite3_expert_destroy().
9098 */
9099 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
9100
9101 /*
9102 ** Return the total number of statements loaded using sqlite3_expert_sql().
9103 ** The total number of SQL statements may be different from the total number
9104 ** to calls to sqlite3_expert_sql().
9105 */
9106 int sqlite3_expert_count(sqlite3expert*);
9107
9108 /*
9109 ** Return a component of the report.
9110 **
9111 ** This function is called after sqlite3_expert_analyze() to extract the
9112 ** results of the analysis. Each call to this function returns either a
9113 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
9114 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
9115 ** #define constants defined below.
9116 **
9117 ** For some EXPERT_REPORT_* parameters, the buffer returned contains 
9118 ** information relating to a specific SQL statement. In these cases that
9119 ** SQL statement is identified by the value passed as the second argument.
9120 ** SQL statements are numbered from 0 in the order in which they are parsed.
9121 ** If an out-of-range value (less than zero or equal to or greater than the
9122 ** value returned by sqlite3_expert_count()) is passed as the second argument
9123 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
9124 **
9125 ** EXPERT_REPORT_SQL:
9126 **   Return the text of SQL statement iStmt.
9127 **
9128 ** EXPERT_REPORT_INDEXES:
9129 **   Return a buffer containing the CREATE INDEX statements for all recommended
9130 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL 
9131 **   is returned.
9132 **
9133 ** EXPERT_REPORT_PLAN:
9134 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
9135 **   iStmt after the proposed indexes have been added to the database schema.
9136 **
9137 ** EXPERT_REPORT_CANDIDATES:
9138 **   Return a pointer to a buffer containing the CREATE INDEX statements 
9139 **   for all indexes that were tested (for all SQL statements). The iStmt
9140 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
9141 */
9142 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
9143
9144 /*
9145 ** Values for the third argument passed to sqlite3_expert_report().
9146 */
9147 #define EXPERT_REPORT_SQL        1
9148 #define EXPERT_REPORT_INDEXES    2
9149 #define EXPERT_REPORT_PLAN       3
9150 #define EXPERT_REPORT_CANDIDATES 4
9151
9152 /*
9153 ** Free an (sqlite3expert*) handle and all associated resources. There 
9154 ** should be one call to this function for each successful call to 
9155 ** sqlite3-expert_new().
9156 */
9157 void sqlite3_expert_destroy(sqlite3expert*);
9158
9159 #endif  /* !defined(SQLITEEXPERT_H) */
9160
9161 /************************* End ../ext/expert/sqlite3expert.h ********************/
9162 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
9163 /*
9164 ** 2017 April 09
9165 **
9166 ** The author disclaims copyright to this source code.  In place of
9167 ** a legal notice, here is a blessing:
9168 **
9169 **    May you do good and not evil.
9170 **    May you find forgiveness for yourself and forgive others.
9171 **    May you share freely, never taking more than you give.
9172 **
9173 *************************************************************************
9174 */
9175 /* #include "sqlite3expert.h" */
9176 #include <assert.h>
9177 #include <string.h>
9178 #include <stdio.h>
9179
9180 #ifndef SQLITE_OMIT_VIRTUALTABLE 
9181
9182 /* typedef sqlite3_int64 i64; */
9183 /* typedef sqlite3_uint64 u64; */
9184
9185 typedef struct IdxColumn IdxColumn;
9186 typedef struct IdxConstraint IdxConstraint;
9187 typedef struct IdxScan IdxScan;
9188 typedef struct IdxStatement IdxStatement;
9189 typedef struct IdxTable IdxTable;
9190 typedef struct IdxWrite IdxWrite;
9191
9192 #define STRLEN  (int)strlen
9193
9194 /*
9195 ** A temp table name that we assume no user database will actually use.
9196 ** If this assumption proves incorrect triggers on the table with the
9197 ** conflicting name will be ignored.
9198 */
9199 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
9200
9201 /*
9202 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
9203 ** any other type of single-ended range constraint on a column).
9204 **
9205 ** pLink:
9206 **   Used to temporarily link IdxConstraint objects into lists while
9207 **   creating candidate indexes.
9208 */
9209 struct IdxConstraint {
9210   char *zColl;                    /* Collation sequence */
9211   int bRange;                     /* True for range, false for eq */
9212   int iCol;                       /* Constrained table column */
9213   int bFlag;                      /* Used by idxFindCompatible() */
9214   int bDesc;                      /* True if ORDER BY <expr> DESC */
9215   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
9216   IdxConstraint *pLink;           /* See above */
9217 };
9218
9219 /*
9220 ** A single scan of a single table.
9221 */
9222 struct IdxScan {
9223   IdxTable *pTab;                 /* Associated table object */
9224   int iDb;                        /* Database containing table zTable */
9225   i64 covering;                   /* Mask of columns required for cov. index */
9226   IdxConstraint *pOrder;          /* ORDER BY columns */
9227   IdxConstraint *pEq;             /* List of == constraints */
9228   IdxConstraint *pRange;          /* List of < constraints */
9229   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
9230 };
9231
9232 /*
9233 ** Information regarding a single database table. Extracted from 
9234 ** "PRAGMA table_info" by function idxGetTableInfo().
9235 */
9236 struct IdxColumn {
9237   char *zName;
9238   char *zColl;
9239   int iPk;
9240 };
9241 struct IdxTable {
9242   int nCol;
9243   char *zName;                    /* Table name */
9244   IdxColumn *aCol;
9245   IdxTable *pNext;                /* Next table in linked list of all tables */
9246 };
9247
9248 /*
9249 ** An object of the following type is created for each unique table/write-op
9250 ** seen. The objects are stored in a singly-linked list beginning at
9251 ** sqlite3expert.pWrite.
9252 */
9253 struct IdxWrite {
9254   IdxTable *pTab;
9255   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
9256   IdxWrite *pNext;
9257 };
9258
9259 /*
9260 ** Each statement being analyzed is represented by an instance of this
9261 ** structure.
9262 */
9263 struct IdxStatement {
9264   int iId;                        /* Statement number */
9265   char *zSql;                     /* SQL statement */
9266   char *zIdx;                     /* Indexes */
9267   char *zEQP;                     /* Plan */
9268   IdxStatement *pNext;
9269 };
9270
9271
9272 /*
9273 ** A hash table for storing strings. With space for a payload string
9274 ** with each entry. Methods are:
9275 **
9276 **   idxHashInit()
9277 **   idxHashClear()
9278 **   idxHashAdd()
9279 **   idxHashSearch()
9280 */
9281 #define IDX_HASH_SIZE 1023
9282 typedef struct IdxHashEntry IdxHashEntry;
9283 typedef struct IdxHash IdxHash;
9284 struct IdxHashEntry {
9285   char *zKey;                     /* nul-terminated key */
9286   char *zVal;                     /* nul-terminated value string */
9287   char *zVal2;                    /* nul-terminated value string 2 */
9288   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
9289   IdxHashEntry *pNext;            /* Next entry in hash */
9290 };
9291 struct IdxHash {
9292   IdxHashEntry *pFirst;
9293   IdxHashEntry *aHash[IDX_HASH_SIZE];
9294 };
9295
9296 /*
9297 ** sqlite3expert object.
9298 */
9299 struct sqlite3expert {
9300   int iSample;                    /* Percentage of tables to sample for stat1 */
9301   sqlite3 *db;                    /* User database */
9302   sqlite3 *dbm;                   /* In-memory db for this analysis */
9303   sqlite3 *dbv;                   /* Vtab schema for this analysis */
9304   IdxTable *pTable;               /* List of all IdxTable objects */
9305   IdxScan *pScan;                 /* List of scan objects */
9306   IdxWrite *pWrite;               /* List of write objects */
9307   IdxStatement *pStatement;       /* List of IdxStatement objects */
9308   int bRun;                       /* True once analysis has run */
9309   char **pzErrmsg;
9310   int rc;                         /* Error code from whereinfo hook */
9311   IdxHash hIdx;                   /* Hash containing all candidate indexes */
9312   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
9313 };
9314
9315
9316 /*
9317 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). 
9318 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
9319 */
9320 static void *idxMalloc(int *pRc, int nByte){
9321   void *pRet;
9322   assert( *pRc==SQLITE_OK );
9323   assert( nByte>0 );
9324   pRet = sqlite3_malloc(nByte);
9325   if( pRet ){
9326     memset(pRet, 0, nByte);
9327   }else{
9328     *pRc = SQLITE_NOMEM;
9329   }
9330   return pRet;
9331 }
9332
9333 /*
9334 ** Initialize an IdxHash hash table.
9335 */
9336 static void idxHashInit(IdxHash *pHash){
9337   memset(pHash, 0, sizeof(IdxHash));
9338 }
9339
9340 /*
9341 ** Reset an IdxHash hash table.
9342 */
9343 static void idxHashClear(IdxHash *pHash){
9344   int i;
9345   for(i=0; i<IDX_HASH_SIZE; i++){
9346     IdxHashEntry *pEntry;
9347     IdxHashEntry *pNext;
9348     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
9349       pNext = pEntry->pHashNext;
9350       sqlite3_free(pEntry->zVal2);
9351       sqlite3_free(pEntry);
9352     }
9353   }
9354   memset(pHash, 0, sizeof(IdxHash));
9355 }
9356
9357 /*
9358 ** Return the index of the hash bucket that the string specified by the
9359 ** arguments to this function belongs.
9360 */
9361 static int idxHashString(const char *z, int n){
9362   unsigned int ret = 0;
9363   int i;
9364   for(i=0; i<n; i++){
9365     ret += (ret<<3) + (unsigned char)(z[i]);
9366   }
9367   return (int)(ret % IDX_HASH_SIZE);
9368 }
9369
9370 /*
9371 ** If zKey is already present in the hash table, return non-zero and do
9372 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
9373 ** the hash table passed as the second argument. 
9374 */
9375 static int idxHashAdd(
9376   int *pRc, 
9377   IdxHash *pHash, 
9378   const char *zKey,
9379   const char *zVal
9380 ){
9381   int nKey = STRLEN(zKey);
9382   int iHash = idxHashString(zKey, nKey);
9383   int nVal = (zVal ? STRLEN(zVal) : 0);
9384   IdxHashEntry *pEntry;
9385   assert( iHash>=0 );
9386   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9387     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9388       return 1;
9389     }
9390   }
9391   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
9392   if( pEntry ){
9393     pEntry->zKey = (char*)&pEntry[1];
9394     memcpy(pEntry->zKey, zKey, nKey);
9395     if( zVal ){
9396       pEntry->zVal = &pEntry->zKey[nKey+1];
9397       memcpy(pEntry->zVal, zVal, nVal);
9398     }
9399     pEntry->pHashNext = pHash->aHash[iHash];
9400     pHash->aHash[iHash] = pEntry;
9401
9402     pEntry->pNext = pHash->pFirst;
9403     pHash->pFirst = pEntry;
9404   }
9405   return 0;
9406 }
9407
9408 /*
9409 ** If zKey/nKey is present in the hash table, return a pointer to the 
9410 ** hash-entry object.
9411 */
9412 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
9413   int iHash;
9414   IdxHashEntry *pEntry;
9415   if( nKey<0 ) nKey = STRLEN(zKey);
9416   iHash = idxHashString(zKey, nKey);
9417   assert( iHash>=0 );
9418   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9419     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9420       return pEntry;
9421     }
9422   }
9423   return 0;
9424 }
9425
9426 /*
9427 ** If the hash table contains an entry with a key equal to the string
9428 ** passed as the final two arguments to this function, return a pointer
9429 ** to the payload string. Otherwise, if zKey/nKey is not present in the
9430 ** hash table, return NULL.
9431 */
9432 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
9433   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
9434   if( pEntry ) return pEntry->zVal;
9435   return 0;
9436 }
9437
9438 /*
9439 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
9440 ** variable to point to a copy of nul-terminated string zColl.
9441 */
9442 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
9443   IdxConstraint *pNew;
9444   int nColl = STRLEN(zColl);
9445
9446   assert( *pRc==SQLITE_OK );
9447   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
9448   if( pNew ){
9449     pNew->zColl = (char*)&pNew[1];
9450     memcpy(pNew->zColl, zColl, nColl+1);
9451   }
9452   return pNew;
9453 }
9454
9455 /*
9456 ** An error associated with database handle db has just occurred. Pass
9457 ** the error message to callback function xOut.
9458 */
9459 static void idxDatabaseError(
9460   sqlite3 *db,                    /* Database handle */
9461   char **pzErrmsg                 /* Write error here */
9462 ){
9463   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
9464 }
9465
9466 /*
9467 ** Prepare an SQL statement.
9468 */
9469 static int idxPrepareStmt(
9470   sqlite3 *db,                    /* Database handle to compile against */
9471   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
9472   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
9473   const char *zSql                /* SQL statement to compile */
9474 ){
9475   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
9476   if( rc!=SQLITE_OK ){
9477     *ppStmt = 0;
9478     idxDatabaseError(db, pzErrmsg);
9479   }
9480   return rc;
9481 }
9482
9483 /*
9484 ** Prepare an SQL statement using the results of a printf() formatting.
9485 */
9486 static int idxPrintfPrepareStmt(
9487   sqlite3 *db,                    /* Database handle to compile against */
9488   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
9489   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
9490   const char *zFmt,               /* printf() format of SQL statement */
9491   ...                             /* Trailing printf() arguments */
9492 ){
9493   va_list ap;
9494   int rc;
9495   char *zSql;
9496   va_start(ap, zFmt);
9497   zSql = sqlite3_vmprintf(zFmt, ap);
9498   if( zSql==0 ){
9499     rc = SQLITE_NOMEM;
9500   }else{
9501     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
9502     sqlite3_free(zSql);
9503   }
9504   va_end(ap);
9505   return rc;
9506 }
9507
9508
9509 /*************************************************************************
9510 ** Beginning of virtual table implementation.
9511 */
9512 typedef struct ExpertVtab ExpertVtab;
9513 struct ExpertVtab {
9514   sqlite3_vtab base;
9515   IdxTable *pTab;
9516   sqlite3expert *pExpert;
9517 };
9518
9519 typedef struct ExpertCsr ExpertCsr;
9520 struct ExpertCsr {
9521   sqlite3_vtab_cursor base;
9522   sqlite3_stmt *pData;
9523 };
9524
9525 static char *expertDequote(const char *zIn){
9526   int n = STRLEN(zIn);
9527   char *zRet = sqlite3_malloc(n);
9528
9529   assert( zIn[0]=='\'' );
9530   assert( zIn[n-1]=='\'' );
9531
9532   if( zRet ){
9533     int iOut = 0;
9534     int iIn = 0;
9535     for(iIn=1; iIn<(n-1); iIn++){
9536       if( zIn[iIn]=='\'' ){
9537         assert( zIn[iIn+1]=='\'' );
9538         iIn++;
9539       }
9540       zRet[iOut++] = zIn[iIn];
9541     }
9542     zRet[iOut] = '\0';
9543   }
9544
9545   return zRet;
9546 }
9547
9548 /* 
9549 ** This function is the implementation of both the xConnect and xCreate
9550 ** methods of the r-tree virtual table.
9551 **
9552 **   argv[0]   -> module name
9553 **   argv[1]   -> database name
9554 **   argv[2]   -> table name
9555 **   argv[...] -> column names...
9556 */
9557 static int expertConnect(
9558   sqlite3 *db,
9559   void *pAux,
9560   int argc, const char *const*argv,
9561   sqlite3_vtab **ppVtab,
9562   char **pzErr
9563 ){
9564   sqlite3expert *pExpert = (sqlite3expert*)pAux;
9565   ExpertVtab *p = 0;
9566   int rc;
9567
9568   if( argc!=4 ){
9569     *pzErr = sqlite3_mprintf("internal error!");
9570     rc = SQLITE_ERROR;
9571   }else{
9572     char *zCreateTable = expertDequote(argv[3]);
9573     if( zCreateTable ){
9574       rc = sqlite3_declare_vtab(db, zCreateTable);
9575       if( rc==SQLITE_OK ){
9576         p = idxMalloc(&rc, sizeof(ExpertVtab));
9577       }
9578       if( rc==SQLITE_OK ){
9579         p->pExpert = pExpert;
9580         p->pTab = pExpert->pTable;
9581         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
9582       }
9583       sqlite3_free(zCreateTable);
9584     }else{
9585       rc = SQLITE_NOMEM;
9586     }
9587   }
9588
9589   *ppVtab = (sqlite3_vtab*)p;
9590   return rc;
9591 }
9592
9593 static int expertDisconnect(sqlite3_vtab *pVtab){
9594   ExpertVtab *p = (ExpertVtab*)pVtab;
9595   sqlite3_free(p);
9596   return SQLITE_OK;
9597 }
9598
9599 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
9600   ExpertVtab *p = (ExpertVtab*)pVtab;
9601   int rc = SQLITE_OK;
9602   int n = 0;
9603   IdxScan *pScan;
9604   const int opmask = 
9605     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
9606     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
9607     SQLITE_INDEX_CONSTRAINT_LE;
9608
9609   pScan = idxMalloc(&rc, sizeof(IdxScan));
9610   if( pScan ){
9611     int i;
9612
9613     /* Link the new scan object into the list */
9614     pScan->pTab = p->pTab;
9615     pScan->pNextScan = p->pExpert->pScan;
9616     p->pExpert->pScan = pScan;
9617
9618     /* Add the constraints to the IdxScan object */
9619     for(i=0; i<pIdxInfo->nConstraint; i++){
9620       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
9621       if( pCons->usable 
9622        && pCons->iColumn>=0 
9623        && p->pTab->aCol[pCons->iColumn].iPk==0
9624        && (pCons->op & opmask) 
9625       ){
9626         IdxConstraint *pNew;
9627         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
9628         pNew = idxNewConstraint(&rc, zColl);
9629         if( pNew ){
9630           pNew->iCol = pCons->iColumn;
9631           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
9632             pNew->pNext = pScan->pEq;
9633             pScan->pEq = pNew;
9634           }else{
9635             pNew->bRange = 1;
9636             pNew->pNext = pScan->pRange;
9637             pScan->pRange = pNew;
9638           }
9639         }
9640         n++;
9641         pIdxInfo->aConstraintUsage[i].argvIndex = n;
9642       }
9643     }
9644
9645     /* Add the ORDER BY to the IdxScan object */
9646     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
9647       int iCol = pIdxInfo->aOrderBy[i].iColumn;
9648       if( iCol>=0 ){
9649         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
9650         if( pNew ){
9651           pNew->iCol = iCol;
9652           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
9653           pNew->pNext = pScan->pOrder;
9654           pNew->pLink = pScan->pOrder;
9655           pScan->pOrder = pNew;
9656           n++;
9657         }
9658       }
9659     }
9660   }
9661
9662   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
9663   return rc;
9664 }
9665
9666 static int expertUpdate(
9667   sqlite3_vtab *pVtab, 
9668   int nData, 
9669   sqlite3_value **azData, 
9670   sqlite_int64 *pRowid
9671 ){
9672   (void)pVtab;
9673   (void)nData;
9674   (void)azData;
9675   (void)pRowid;
9676   return SQLITE_OK;
9677 }
9678
9679 /* 
9680 ** Virtual table module xOpen method.
9681 */
9682 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
9683   int rc = SQLITE_OK;
9684   ExpertCsr *pCsr;
9685   (void)pVTab;
9686   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
9687   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
9688   return rc;
9689 }
9690
9691 /* 
9692 ** Virtual table module xClose method.
9693 */
9694 static int expertClose(sqlite3_vtab_cursor *cur){
9695   ExpertCsr *pCsr = (ExpertCsr*)cur;
9696   sqlite3_finalize(pCsr->pData);
9697   sqlite3_free(pCsr);
9698   return SQLITE_OK;
9699 }
9700
9701 /*
9702 ** Virtual table module xEof method.
9703 **
9704 ** Return non-zero if the cursor does not currently point to a valid 
9705 ** record (i.e if the scan has finished), or zero otherwise.
9706 */
9707 static int expertEof(sqlite3_vtab_cursor *cur){
9708   ExpertCsr *pCsr = (ExpertCsr*)cur;
9709   return pCsr->pData==0;
9710 }
9711
9712 /* 
9713 ** Virtual table module xNext method.
9714 */
9715 static int expertNext(sqlite3_vtab_cursor *cur){
9716   ExpertCsr *pCsr = (ExpertCsr*)cur;
9717   int rc = SQLITE_OK;
9718
9719   assert( pCsr->pData );
9720   rc = sqlite3_step(pCsr->pData);
9721   if( rc!=SQLITE_ROW ){
9722     rc = sqlite3_finalize(pCsr->pData);
9723     pCsr->pData = 0;
9724   }else{
9725     rc = SQLITE_OK;
9726   }
9727
9728   return rc;
9729 }
9730
9731 /* 
9732 ** Virtual table module xRowid method.
9733 */
9734 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
9735   (void)cur;
9736   *pRowid = 0;
9737   return SQLITE_OK;
9738 }
9739
9740 /* 
9741 ** Virtual table module xColumn method.
9742 */
9743 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
9744   ExpertCsr *pCsr = (ExpertCsr*)cur;
9745   sqlite3_value *pVal;
9746   pVal = sqlite3_column_value(pCsr->pData, i);
9747   if( pVal ){
9748     sqlite3_result_value(ctx, pVal);
9749   }
9750   return SQLITE_OK;
9751 }
9752
9753 /* 
9754 ** Virtual table module xFilter method.
9755 */
9756 static int expertFilter(
9757   sqlite3_vtab_cursor *cur, 
9758   int idxNum, const char *idxStr,
9759   int argc, sqlite3_value **argv
9760 ){
9761   ExpertCsr *pCsr = (ExpertCsr*)cur;
9762   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
9763   sqlite3expert *pExpert = pVtab->pExpert;
9764   int rc;
9765
9766   (void)idxNum;
9767   (void)idxStr;
9768   (void)argc;
9769   (void)argv;
9770   rc = sqlite3_finalize(pCsr->pData);
9771   pCsr->pData = 0;
9772   if( rc==SQLITE_OK ){
9773     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
9774         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
9775     );
9776   }
9777
9778   if( rc==SQLITE_OK ){
9779     rc = expertNext(cur);
9780   }
9781   return rc;
9782 }
9783
9784 static int idxRegisterVtab(sqlite3expert *p){
9785   static sqlite3_module expertModule = {
9786     2,                            /* iVersion */
9787     expertConnect,                /* xCreate - create a table */
9788     expertConnect,                /* xConnect - connect to an existing table */
9789     expertBestIndex,              /* xBestIndex - Determine search strategy */
9790     expertDisconnect,             /* xDisconnect - Disconnect from a table */
9791     expertDisconnect,             /* xDestroy - Drop a table */
9792     expertOpen,                   /* xOpen - open a cursor */
9793     expertClose,                  /* xClose - close a cursor */
9794     expertFilter,                 /* xFilter - configure scan constraints */
9795     expertNext,                   /* xNext - advance a cursor */
9796     expertEof,                    /* xEof */
9797     expertColumn,                 /* xColumn - read data */
9798     expertRowid,                  /* xRowid - read data */
9799     expertUpdate,                 /* xUpdate - write data */
9800     0,                            /* xBegin - begin transaction */
9801     0,                            /* xSync - sync transaction */
9802     0,                            /* xCommit - commit transaction */
9803     0,                            /* xRollback - rollback transaction */
9804     0,                            /* xFindFunction - function overloading */
9805     0,                            /* xRename - rename the table */
9806     0,                            /* xSavepoint */
9807     0,                            /* xRelease */
9808     0,                            /* xRollbackTo */
9809     0,                            /* xShadowName */
9810   };
9811
9812   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
9813 }
9814 /*
9815 ** End of virtual table implementation.
9816 *************************************************************************/
9817 /*
9818 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
9819 ** is called, set it to the return value of sqlite3_finalize() before
9820 ** returning. Otherwise, discard the sqlite3_finalize() return value.
9821 */
9822 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
9823   int rc = sqlite3_finalize(pStmt);
9824   if( *pRc==SQLITE_OK ) *pRc = rc;
9825 }
9826
9827 /*
9828 ** Attempt to allocate an IdxTable structure corresponding to table zTab
9829 ** in the main database of connection db. If successful, set (*ppOut) to
9830 ** point to the new object and return SQLITE_OK. Otherwise, return an
9831 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
9832 ** set to point to an error string.
9833 **
9834 ** It is the responsibility of the caller to eventually free either the
9835 ** IdxTable object or error message using sqlite3_free().
9836 */
9837 static int idxGetTableInfo(
9838   sqlite3 *db,                    /* Database connection to read details from */
9839   const char *zTab,               /* Table name */
9840   IdxTable **ppOut,               /* OUT: New object (if successful) */
9841   char **pzErrmsg                 /* OUT: Error message (if not) */
9842 ){
9843   sqlite3_stmt *p1 = 0;
9844   int nCol = 0;
9845   int nTab = STRLEN(zTab);
9846   int nByte = sizeof(IdxTable) + nTab + 1;
9847   IdxTable *pNew = 0;
9848   int rc, rc2;
9849   char *pCsr = 0;
9850   int nPk = 0;
9851
9852   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9853   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9854     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9855     nByte += 1 + STRLEN(zCol);
9856     rc = sqlite3_table_column_metadata(
9857         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9858     );
9859     nByte += 1 + STRLEN(zCol);
9860     nCol++;
9861     nPk += (sqlite3_column_int(p1, 5)>0);
9862   }
9863   rc2 = sqlite3_reset(p1);
9864   if( rc==SQLITE_OK ) rc = rc2;
9865
9866   nByte += sizeof(IdxColumn) * nCol;
9867   if( rc==SQLITE_OK ){
9868     pNew = idxMalloc(&rc, nByte);
9869   }
9870   if( rc==SQLITE_OK ){
9871     pNew->aCol = (IdxColumn*)&pNew[1];
9872     pNew->nCol = nCol;
9873     pCsr = (char*)&pNew->aCol[nCol];
9874   }
9875
9876   nCol = 0;
9877   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9878     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9879     int nCopy = STRLEN(zCol) + 1;
9880     pNew->aCol[nCol].zName = pCsr;
9881     pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9882     memcpy(pCsr, zCol, nCopy);
9883     pCsr += nCopy;
9884
9885     rc = sqlite3_table_column_metadata(
9886         db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9887     );
9888     if( rc==SQLITE_OK ){
9889       nCopy = STRLEN(zCol) + 1;
9890       pNew->aCol[nCol].zColl = pCsr;
9891       memcpy(pCsr, zCol, nCopy);
9892       pCsr += nCopy;
9893     }
9894
9895     nCol++;
9896   }
9897   idxFinalize(&rc, p1);
9898
9899   if( rc!=SQLITE_OK ){
9900     sqlite3_free(pNew);
9901     pNew = 0;
9902   }else{
9903     pNew->zName = pCsr;
9904     memcpy(pNew->zName, zTab, nTab+1);
9905   }
9906
9907   *ppOut = pNew;
9908   return rc;
9909 }
9910
9911 /*
9912 ** This function is a no-op if *pRc is set to anything other than 
9913 ** SQLITE_OK when it is called.
9914 **
9915 ** If *pRc is initially set to SQLITE_OK, then the text specified by
9916 ** the printf() style arguments is appended to zIn and the result returned
9917 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9918 ** zIn before returning.
9919 */
9920 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
9921   va_list ap;
9922   char *zAppend = 0;
9923   char *zRet = 0;
9924   int nIn = zIn ? STRLEN(zIn) : 0;
9925   int nAppend = 0;
9926   va_start(ap, zFmt);
9927   if( *pRc==SQLITE_OK ){
9928     zAppend = sqlite3_vmprintf(zFmt, ap);
9929     if( zAppend ){
9930       nAppend = STRLEN(zAppend);
9931       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
9932     }
9933     if( zAppend && zRet ){
9934       if( nIn ) memcpy(zRet, zIn, nIn);
9935       memcpy(&zRet[nIn], zAppend, nAppend+1);
9936     }else{
9937       sqlite3_free(zRet);
9938       zRet = 0;
9939       *pRc = SQLITE_NOMEM;
9940     }
9941     sqlite3_free(zAppend);
9942     sqlite3_free(zIn);
9943   }
9944   va_end(ap);
9945   return zRet;
9946 }
9947
9948 /*
9949 ** Return true if zId must be quoted in order to use it as an SQL
9950 ** identifier, or false otherwise.
9951 */
9952 static int idxIdentifierRequiresQuotes(const char *zId){
9953   int i;
9954   for(i=0; zId[i]; i++){
9955     if( !(zId[i]=='_')
9956      && !(zId[i]>='0' && zId[i]<='9')
9957      && !(zId[i]>='a' && zId[i]<='z')
9958      && !(zId[i]>='A' && zId[i]<='Z')
9959     ){
9960       return 1;
9961     }
9962   }
9963   return 0;
9964 }
9965
9966 /*
9967 ** This function appends an index column definition suitable for constraint
9968 ** pCons to the string passed as zIn and returns the result.
9969 */
9970 static char *idxAppendColDefn(
9971   int *pRc,                       /* IN/OUT: Error code */
9972   char *zIn,                      /* Column defn accumulated so far */
9973   IdxTable *pTab,                 /* Table index will be created on */
9974   IdxConstraint *pCons
9975 ){
9976   char *zRet = zIn;
9977   IdxColumn *p = &pTab->aCol[pCons->iCol];
9978   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
9979
9980   if( idxIdentifierRequiresQuotes(p->zName) ){
9981     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
9982   }else{
9983     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
9984   }
9985
9986   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
9987     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
9988       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
9989     }else{
9990       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
9991     }
9992   }
9993
9994   if( pCons->bDesc ){
9995     zRet = idxAppendText(pRc, zRet, " DESC");
9996   }
9997   return zRet;
9998 }
9999
10000 /*
10001 ** Search database dbm for an index compatible with the one idxCreateFromCons()
10002 ** would create from arguments pScan, pEq and pTail. If no error occurs and 
10003 ** such an index is found, return non-zero. Or, if no such index is found,
10004 ** return zero.
10005 **
10006 ** If an error occurs, set *pRc to an SQLite error code and return zero.
10007 */
10008 static int idxFindCompatible(
10009   int *pRc,                       /* OUT: Error code */
10010   sqlite3* dbm,                   /* Database to search */
10011   IdxScan *pScan,                 /* Scan for table to search for index on */
10012   IdxConstraint *pEq,             /* List of == constraints */
10013   IdxConstraint *pTail            /* List of range constraints */
10014 ){
10015   const char *zTbl = pScan->pTab->zName;
10016   sqlite3_stmt *pIdxList = 0;
10017   IdxConstraint *pIter;
10018   int nEq = 0;                    /* Number of elements in pEq */
10019   int rc;
10020
10021   /* Count the elements in list pEq */
10022   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
10023
10024   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
10025   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
10026     int bMatch = 1;
10027     IdxConstraint *pT = pTail;
10028     sqlite3_stmt *pInfo = 0;
10029     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
10030
10031     /* Zero the IdxConstraint.bFlag values in the pEq list */
10032     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
10033
10034     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
10035     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
10036       int iIdx = sqlite3_column_int(pInfo, 0);
10037       int iCol = sqlite3_column_int(pInfo, 1);
10038       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
10039
10040       if( iIdx<nEq ){
10041         for(pIter=pEq; pIter; pIter=pIter->pLink){
10042           if( pIter->bFlag ) continue;
10043           if( pIter->iCol!=iCol ) continue;
10044           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
10045           pIter->bFlag = 1;
10046           break;
10047         }
10048         if( pIter==0 ){
10049           bMatch = 0;
10050           break;
10051         }
10052       }else{
10053         if( pT ){
10054           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
10055             bMatch = 0;
10056             break;
10057           }
10058           pT = pT->pLink;
10059         }
10060       }
10061     }
10062     idxFinalize(&rc, pInfo);
10063
10064     if( rc==SQLITE_OK && bMatch ){
10065       sqlite3_finalize(pIdxList);
10066       return 1;
10067     }
10068   }
10069   idxFinalize(&rc, pIdxList);
10070
10071   *pRc = rc;
10072   return 0;
10073 }
10074
10075 static int idxCreateFromCons(
10076   sqlite3expert *p,
10077   IdxScan *pScan,
10078   IdxConstraint *pEq, 
10079   IdxConstraint *pTail
10080 ){
10081   sqlite3 *dbm = p->dbm;
10082   int rc = SQLITE_OK;
10083   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
10084     IdxTable *pTab = pScan->pTab;
10085     char *zCols = 0;
10086     char *zIdx = 0;
10087     IdxConstraint *pCons;
10088     unsigned int h = 0;
10089     const char *zFmt;
10090
10091     for(pCons=pEq; pCons; pCons=pCons->pLink){
10092       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10093     }
10094     for(pCons=pTail; pCons; pCons=pCons->pLink){
10095       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10096     }
10097
10098     if( rc==SQLITE_OK ){
10099       /* Hash the list of columns to come up with a name for the index */
10100       const char *zTable = pScan->pTab->zName;
10101       char *zName;                /* Index name */
10102       int i;
10103       for(i=0; zCols[i]; i++){
10104         h += ((h<<3) + zCols[i]);
10105       }
10106       zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
10107       if( zName==0 ){ 
10108         rc = SQLITE_NOMEM;
10109       }else{
10110         if( idxIdentifierRequiresQuotes(zTable) ){
10111           zFmt = "CREATE INDEX '%q' ON %Q(%s)";
10112         }else{
10113           zFmt = "CREATE INDEX %s ON %s(%s)";
10114         }
10115         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
10116         if( !zIdx ){
10117           rc = SQLITE_NOMEM;
10118         }else{
10119           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
10120           idxHashAdd(&rc, &p->hIdx, zName, zIdx);
10121         }
10122         sqlite3_free(zName);
10123         sqlite3_free(zIdx);
10124       }
10125     }
10126
10127     sqlite3_free(zCols);
10128   }
10129   return rc;
10130 }
10131
10132 /*
10133 ** Return true if list pList (linked by IdxConstraint.pLink) contains
10134 ** a constraint compatible with *p. Otherwise return false.
10135 */
10136 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
10137   IdxConstraint *pCmp;
10138   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
10139     if( p->iCol==pCmp->iCol ) return 1;
10140   }
10141   return 0;
10142 }
10143
10144 static int idxCreateFromWhere(
10145   sqlite3expert *p, 
10146   IdxScan *pScan,                 /* Create indexes for this scan */
10147   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
10148 ){
10149   IdxConstraint *p1 = 0;
10150   IdxConstraint *pCon;
10151   int rc;
10152
10153   /* Gather up all the == constraints. */
10154   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
10155     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10156       pCon->pLink = p1;
10157       p1 = pCon;
10158     }
10159   }
10160
10161   /* Create an index using the == constraints collected above. And the
10162   ** range constraint/ORDER BY terms passed in by the caller, if any. */
10163   rc = idxCreateFromCons(p, pScan, p1, pTail);
10164
10165   /* If no range/ORDER BY passed by the caller, create a version of the
10166   ** index for each range constraint.  */
10167   if( pTail==0 ){
10168     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
10169       assert( pCon->pLink==0 );
10170       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10171         rc = idxCreateFromCons(p, pScan, p1, pCon);
10172       }
10173     }
10174   }
10175
10176   return rc;
10177 }
10178
10179 /*
10180 ** Create candidate indexes in database [dbm] based on the data in 
10181 ** linked-list pScan.
10182 */
10183 static int idxCreateCandidates(sqlite3expert *p){
10184   int rc = SQLITE_OK;
10185   IdxScan *pIter;
10186
10187   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
10188     rc = idxCreateFromWhere(p, pIter, 0);
10189     if( rc==SQLITE_OK && pIter->pOrder ){
10190       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
10191     }
10192   }
10193
10194   return rc;
10195 }
10196
10197 /*
10198 ** Free all elements of the linked list starting at pConstraint.
10199 */
10200 static void idxConstraintFree(IdxConstraint *pConstraint){
10201   IdxConstraint *pNext;
10202   IdxConstraint *p;
10203
10204   for(p=pConstraint; p; p=pNext){
10205     pNext = p->pNext;
10206     sqlite3_free(p);
10207   }
10208 }
10209
10210 /*
10211 ** Free all elements of the linked list starting from pScan up until pLast
10212 ** (pLast is not freed).
10213 */
10214 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
10215   IdxScan *p;
10216   IdxScan *pNext;
10217   for(p=pScan; p!=pLast; p=pNext){
10218     pNext = p->pNextScan;
10219     idxConstraintFree(p->pOrder);
10220     idxConstraintFree(p->pEq);
10221     idxConstraintFree(p->pRange);
10222     sqlite3_free(p);
10223   }
10224 }
10225
10226 /*
10227 ** Free all elements of the linked list starting from pStatement up 
10228 ** until pLast (pLast is not freed).
10229 */
10230 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
10231   IdxStatement *p;
10232   IdxStatement *pNext;
10233   for(p=pStatement; p!=pLast; p=pNext){
10234     pNext = p->pNext;
10235     sqlite3_free(p->zEQP);
10236     sqlite3_free(p->zIdx);
10237     sqlite3_free(p);
10238   }
10239 }
10240
10241 /*
10242 ** Free the linked list of IdxTable objects starting at pTab.
10243 */
10244 static void idxTableFree(IdxTable *pTab){
10245   IdxTable *pIter;
10246   IdxTable *pNext;
10247   for(pIter=pTab; pIter; pIter=pNext){
10248     pNext = pIter->pNext;
10249     sqlite3_free(pIter);
10250   }
10251 }
10252
10253 /*
10254 ** Free the linked list of IdxWrite objects starting at pTab.
10255 */
10256 static void idxWriteFree(IdxWrite *pTab){
10257   IdxWrite *pIter;
10258   IdxWrite *pNext;
10259   for(pIter=pTab; pIter; pIter=pNext){
10260     pNext = pIter->pNext;
10261     sqlite3_free(pIter);
10262   }
10263 }
10264
10265
10266
10267 /*
10268 ** This function is called after candidate indexes have been created. It
10269 ** runs all the queries to see which indexes they prefer, and populates
10270 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
10271 */
10272 int idxFindIndexes(
10273   sqlite3expert *p,
10274   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
10275 ){
10276   IdxStatement *pStmt;
10277   sqlite3 *dbm = p->dbm;
10278   int rc = SQLITE_OK;
10279
10280   IdxHash hIdx;
10281   idxHashInit(&hIdx);
10282
10283   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
10284     IdxHashEntry *pEntry;
10285     sqlite3_stmt *pExplain = 0;
10286     idxHashClear(&hIdx);
10287     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
10288         "EXPLAIN QUERY PLAN %s", pStmt->zSql
10289     );
10290     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
10291       /* int iId = sqlite3_column_int(pExplain, 0); */
10292       /* int iParent = sqlite3_column_int(pExplain, 1); */
10293       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
10294       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
10295       int nDetail;
10296       int i;
10297
10298       if( !zDetail ) continue;
10299       nDetail = STRLEN(zDetail);
10300
10301       for(i=0; i<nDetail; i++){
10302         const char *zIdx = 0;
10303         if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
10304           zIdx = &zDetail[i+13];
10305         }else if( i+22<nDetail 
10306             && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 
10307         ){
10308           zIdx = &zDetail[i+22];
10309         }
10310         if( zIdx ){
10311           const char *zSql;
10312           int nIdx = 0;
10313           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
10314             nIdx++;
10315           }
10316           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
10317           if( zSql ){
10318             idxHashAdd(&rc, &hIdx, zSql, 0);
10319             if( rc ) goto find_indexes_out;
10320           }
10321           break;
10322         }
10323       }
10324
10325       if( zDetail[0]!='-' ){
10326         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
10327       }
10328     }
10329
10330     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10331       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
10332     }
10333
10334     idxFinalize(&rc, pExplain);
10335   }
10336
10337  find_indexes_out:
10338   idxHashClear(&hIdx);
10339   return rc;
10340 }
10341
10342 static int idxAuthCallback(
10343   void *pCtx,
10344   int eOp,
10345   const char *z3,
10346   const char *z4,
10347   const char *zDb,
10348   const char *zTrigger
10349 ){
10350   int rc = SQLITE_OK;
10351   (void)z4;
10352   (void)zTrigger;
10353   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
10354     if( sqlite3_stricmp(zDb, "main")==0 ){
10355       sqlite3expert *p = (sqlite3expert*)pCtx;
10356       IdxTable *pTab;
10357       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
10358         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
10359       }
10360       if( pTab ){
10361         IdxWrite *pWrite;
10362         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
10363           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
10364         }
10365         if( pWrite==0 ){
10366           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
10367           if( rc==SQLITE_OK ){
10368             pWrite->pTab = pTab;
10369             pWrite->eOp = eOp;
10370             pWrite->pNext = p->pWrite;
10371             p->pWrite = pWrite;
10372           }
10373         }
10374       }
10375     }
10376   }
10377   return rc;
10378 }
10379
10380 static int idxProcessOneTrigger(
10381   sqlite3expert *p, 
10382   IdxWrite *pWrite, 
10383   char **pzErr
10384 ){
10385   static const char *zInt = UNIQUE_TABLE_NAME;
10386   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
10387   IdxTable *pTab = pWrite->pTab;
10388   const char *zTab = pTab->zName;
10389   const char *zSql = 
10390     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
10391     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
10392     "ORDER BY type;";
10393   sqlite3_stmt *pSelect = 0;
10394   int rc = SQLITE_OK;
10395   char *zWrite = 0;
10396
10397   /* Create the table and its triggers in the temp schema */
10398   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
10399   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
10400     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
10401     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
10402   }
10403   idxFinalize(&rc, pSelect);
10404
10405   /* Rename the table in the temp schema to zInt */
10406   if( rc==SQLITE_OK ){
10407     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
10408     if( z==0 ){
10409       rc = SQLITE_NOMEM;
10410     }else{
10411       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
10412       sqlite3_free(z);
10413     }
10414   }
10415
10416   switch( pWrite->eOp ){
10417     case SQLITE_INSERT: {
10418       int i;
10419       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
10420       for(i=0; i<pTab->nCol; i++){
10421         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
10422       }
10423       zWrite = idxAppendText(&rc, zWrite, ")");
10424       break;
10425     }
10426     case SQLITE_UPDATE: {
10427       int i;
10428       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
10429       for(i=0; i<pTab->nCol; i++){
10430         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", 
10431             pTab->aCol[i].zName
10432         );
10433       }
10434       break;
10435     }
10436     default: {
10437       assert( pWrite->eOp==SQLITE_DELETE );
10438       if( rc==SQLITE_OK ){
10439         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
10440         if( zWrite==0 ) rc = SQLITE_NOMEM;
10441       }
10442     }
10443   }
10444
10445   if( rc==SQLITE_OK ){
10446     sqlite3_stmt *pX = 0;
10447     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
10448     idxFinalize(&rc, pX);
10449     if( rc!=SQLITE_OK ){
10450       idxDatabaseError(p->dbv, pzErr);
10451     }
10452   }
10453   sqlite3_free(zWrite);
10454
10455   if( rc==SQLITE_OK ){
10456     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
10457   }
10458
10459   return rc;
10460 }
10461
10462 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
10463   int rc = SQLITE_OK;
10464   IdxWrite *pEnd = 0;
10465   IdxWrite *pFirst = p->pWrite;
10466
10467   while( rc==SQLITE_OK && pFirst!=pEnd ){
10468     IdxWrite *pIter;
10469     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
10470       rc = idxProcessOneTrigger(p, pIter, pzErr);
10471     }
10472     pEnd = pFirst;
10473     pFirst = p->pWrite;
10474   }
10475
10476   return rc;
10477 }
10478
10479
10480 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
10481   int rc = idxRegisterVtab(p);
10482   sqlite3_stmt *pSchema = 0;
10483
10484   /* For each table in the main db schema:
10485   **
10486   **   1) Add an entry to the p->pTable list, and
10487   **   2) Create the equivalent virtual table in dbv.
10488   */
10489   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
10490       "SELECT type, name, sql, 1 FROM sqlite_schema "
10491       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
10492       " UNION ALL "
10493       "SELECT type, name, sql, 2 FROM sqlite_schema "
10494       "WHERE type = 'trigger'"
10495       "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
10496       "ORDER BY 4, 1"
10497   );
10498   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
10499     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
10500     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
10501     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
10502
10503     if( zType[0]=='v' || zType[1]=='r' ){
10504       rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10505     }else{
10506       IdxTable *pTab;
10507       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
10508       if( rc==SQLITE_OK ){
10509         int i;
10510         char *zInner = 0;
10511         char *zOuter = 0;
10512         pTab->pNext = p->pTable;
10513         p->pTable = pTab;
10514
10515         /* The statement the vtab will pass to sqlite3_declare_vtab() */
10516         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
10517         for(i=0; i<pTab->nCol; i++){
10518           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", 
10519               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
10520           );
10521         }
10522         zInner = idxAppendText(&rc, zInner, ")");
10523
10524         /* The CVT statement to create the vtab */
10525         zOuter = idxAppendText(&rc, 0, 
10526             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
10527         );
10528         if( rc==SQLITE_OK ){
10529           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
10530         }
10531         sqlite3_free(zInner);
10532         sqlite3_free(zOuter);
10533       }
10534     }
10535   }
10536   idxFinalize(&rc, pSchema);
10537   return rc;
10538 }
10539
10540 struct IdxSampleCtx {
10541   int iTarget;
10542   double target;                  /* Target nRet/nRow value */
10543   double nRow;                    /* Number of rows seen */
10544   double nRet;                    /* Number of rows returned */
10545 };
10546
10547 static void idxSampleFunc(
10548   sqlite3_context *pCtx,
10549   int argc,
10550   sqlite3_value **argv
10551 ){
10552   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
10553   int bRet;
10554
10555   (void)argv;
10556   assert( argc==0 );
10557   if( p->nRow==0.0 ){
10558     bRet = 1;
10559   }else{
10560     bRet = (p->nRet / p->nRow) <= p->target;
10561     if( bRet==0 ){
10562       unsigned short rnd;
10563       sqlite3_randomness(2, (void*)&rnd);
10564       bRet = ((int)rnd % 100) <= p->iTarget;
10565     }
10566   }
10567
10568   sqlite3_result_int(pCtx, bRet);
10569   p->nRow += 1.0;
10570   p->nRet += (double)bRet;
10571 }
10572
10573 struct IdxRemCtx {
10574   int nSlot;
10575   struct IdxRemSlot {
10576     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
10577     i64 iVal;                     /* SQLITE_INTEGER value */
10578     double rVal;                  /* SQLITE_FLOAT value */
10579     int nByte;                    /* Bytes of space allocated at z */
10580     int n;                        /* Size of buffer z */
10581     char *z;                      /* SQLITE_TEXT/BLOB value */
10582   } aSlot[1];
10583 };
10584
10585 /*
10586 ** Implementation of scalar function rem().
10587 */
10588 static void idxRemFunc(
10589   sqlite3_context *pCtx,
10590   int argc,
10591   sqlite3_value **argv
10592 ){
10593   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
10594   struct IdxRemSlot *pSlot;
10595   int iSlot;
10596   assert( argc==2 );
10597
10598   iSlot = sqlite3_value_int(argv[0]);
10599   assert( iSlot<=p->nSlot );
10600   pSlot = &p->aSlot[iSlot];
10601
10602   switch( pSlot->eType ){
10603     case SQLITE_NULL:
10604       /* no-op */
10605       break;
10606
10607     case SQLITE_INTEGER:
10608       sqlite3_result_int64(pCtx, pSlot->iVal);
10609       break;
10610
10611     case SQLITE_FLOAT:
10612       sqlite3_result_double(pCtx, pSlot->rVal);
10613       break;
10614
10615     case SQLITE_BLOB:
10616       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10617       break;
10618
10619     case SQLITE_TEXT:
10620       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10621       break;
10622   }
10623
10624   pSlot->eType = sqlite3_value_type(argv[1]);
10625   switch( pSlot->eType ){
10626     case SQLITE_NULL:
10627       /* no-op */
10628       break;
10629
10630     case SQLITE_INTEGER:
10631       pSlot->iVal = sqlite3_value_int64(argv[1]);
10632       break;
10633
10634     case SQLITE_FLOAT:
10635       pSlot->rVal = sqlite3_value_double(argv[1]);
10636       break;
10637
10638     case SQLITE_BLOB:
10639     case SQLITE_TEXT: {
10640       int nByte = sqlite3_value_bytes(argv[1]);
10641       if( nByte>pSlot->nByte ){
10642         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
10643         if( zNew==0 ){
10644           sqlite3_result_error_nomem(pCtx);
10645           return;
10646         }
10647         pSlot->nByte = nByte*2;
10648         pSlot->z = zNew;
10649       }
10650       pSlot->n = nByte;
10651       if( pSlot->eType==SQLITE_BLOB ){
10652         memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
10653       }else{
10654         memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
10655       }
10656       break;
10657     }
10658   }
10659 }
10660
10661 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
10662   int rc = SQLITE_OK;
10663   const char *zMax = 
10664     "SELECT max(i.seqno) FROM "
10665     "  sqlite_schema AS s, "
10666     "  pragma_index_list(s.name) AS l, "
10667     "  pragma_index_info(l.name) AS i "
10668     "WHERE s.type = 'table'";
10669   sqlite3_stmt *pMax = 0;
10670
10671   *pnMax = 0;
10672   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
10673   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
10674     *pnMax = sqlite3_column_int(pMax, 0) + 1;
10675   }
10676   idxFinalize(&rc, pMax);
10677
10678   return rc;
10679 }
10680
10681 static int idxPopulateOneStat1(
10682   sqlite3expert *p,
10683   sqlite3_stmt *pIndexXInfo,
10684   sqlite3_stmt *pWriteStat,
10685   const char *zTab,
10686   const char *zIdx,
10687   char **pzErr
10688 ){
10689   char *zCols = 0;
10690   char *zOrder = 0;
10691   char *zQuery = 0;
10692   int nCol = 0;
10693   int i;
10694   sqlite3_stmt *pQuery = 0;
10695   int *aStat = 0;
10696   int rc = SQLITE_OK;
10697
10698   assert( p->iSample>0 );
10699
10700   /* Formulate the query text */
10701   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
10702   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
10703     const char *zComma = zCols==0 ? "" : ", ";
10704     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
10705     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
10706     zCols = idxAppendText(&rc, zCols, 
10707         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
10708     );
10709     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
10710   }
10711   sqlite3_reset(pIndexXInfo);
10712   if( rc==SQLITE_OK ){
10713     if( p->iSample==100 ){
10714       zQuery = sqlite3_mprintf(
10715           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
10716       );
10717     }else{
10718       zQuery = sqlite3_mprintf(
10719           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
10720       );
10721     }
10722   }
10723   sqlite3_free(zCols);
10724   sqlite3_free(zOrder);
10725
10726   /* Formulate the query text */
10727   if( rc==SQLITE_OK ){
10728     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10729     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
10730   }
10731   sqlite3_free(zQuery);
10732
10733   if( rc==SQLITE_OK ){
10734     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
10735   }
10736   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10737     IdxHashEntry *pEntry;
10738     char *zStat = 0;
10739     for(i=0; i<=nCol; i++) aStat[i] = 1;
10740     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10741       aStat[0]++;
10742       for(i=0; i<nCol; i++){
10743         if( sqlite3_column_int(pQuery, i)==0 ) break;
10744       }
10745       for(/*no-op*/; i<nCol; i++){
10746         aStat[i+1]++;
10747       }
10748     }
10749
10750     if( rc==SQLITE_OK ){
10751       int s0 = aStat[0];
10752       zStat = sqlite3_mprintf("%d", s0);
10753       if( zStat==0 ) rc = SQLITE_NOMEM;
10754       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
10755         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
10756       }
10757     }
10758
10759     if( rc==SQLITE_OK ){
10760       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
10761       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
10762       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
10763       sqlite3_step(pWriteStat);
10764       rc = sqlite3_reset(pWriteStat);
10765     }
10766
10767     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
10768     if( pEntry ){
10769       assert( pEntry->zVal2==0 );
10770       pEntry->zVal2 = zStat;
10771     }else{
10772       sqlite3_free(zStat);
10773     }
10774   }
10775   sqlite3_free(aStat);
10776   idxFinalize(&rc, pQuery);
10777
10778   return rc;
10779 }
10780
10781 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
10782   int rc;
10783   char *zSql;
10784
10785   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10786   if( rc!=SQLITE_OK ) return rc;
10787
10788   zSql = sqlite3_mprintf(
10789       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
10790   );
10791   if( zSql==0 ) return SQLITE_NOMEM;
10792   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
10793   sqlite3_free(zSql);
10794
10795   return rc;
10796 }
10797
10798 /*
10799 ** This function is called as part of sqlite3_expert_analyze(). Candidate
10800 ** indexes have already been created in database sqlite3expert.dbm, this
10801 ** function populates sqlite_stat1 table in the same database.
10802 **
10803 ** The stat1 data is generated by querying the 
10804 */
10805 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
10806   int rc = SQLITE_OK;
10807   int nMax =0;
10808   struct IdxRemCtx *pCtx = 0;
10809   struct IdxSampleCtx samplectx; 
10810   int i;
10811   i64 iPrev = -100000;
10812   sqlite3_stmt *pAllIndex = 0;
10813   sqlite3_stmt *pIndexXInfo = 0;
10814   sqlite3_stmt *pWrite = 0;
10815
10816   const char *zAllIndex =
10817     "SELECT s.rowid, s.name, l.name FROM "
10818     "  sqlite_schema AS s, "
10819     "  pragma_index_list(s.name) AS l "
10820     "WHERE s.type = 'table'";
10821   const char *zIndexXInfo = 
10822     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
10823   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
10824
10825   /* If iSample==0, no sqlite_stat1 data is required. */
10826   if( p->iSample==0 ) return SQLITE_OK;
10827
10828   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
10829   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
10830
10831   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
10832
10833   if( rc==SQLITE_OK ){
10834     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
10835     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
10836   }
10837
10838   if( rc==SQLITE_OK ){
10839     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10840     rc = sqlite3_create_function(
10841         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
10842     );
10843   }
10844   if( rc==SQLITE_OK ){
10845     rc = sqlite3_create_function(
10846         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
10847     );
10848   }
10849
10850   if( rc==SQLITE_OK ){
10851     pCtx->nSlot = nMax+1;
10852     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
10853   }
10854   if( rc==SQLITE_OK ){
10855     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
10856   }
10857   if( rc==SQLITE_OK ){
10858     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
10859   }
10860
10861   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
10862     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
10863     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
10864     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
10865     if( p->iSample<100 && iPrev!=iRowid ){
10866       samplectx.target = (double)p->iSample / 100.0;
10867       samplectx.iTarget = p->iSample;
10868       samplectx.nRow = 0.0;
10869       samplectx.nRet = 0.0;
10870       rc = idxBuildSampleTable(p, zTab);
10871       if( rc!=SQLITE_OK ) break;
10872     }
10873     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
10874     iPrev = iRowid;
10875   }
10876   if( rc==SQLITE_OK && p->iSample<100 ){
10877     rc = sqlite3_exec(p->dbv, 
10878         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
10879     );
10880   }
10881
10882   idxFinalize(&rc, pAllIndex);
10883   idxFinalize(&rc, pIndexXInfo);
10884   idxFinalize(&rc, pWrite);
10885
10886   if( pCtx ){
10887     for(i=0; i<pCtx->nSlot; i++){
10888       sqlite3_free(pCtx->aSlot[i].z);
10889     }
10890     sqlite3_free(pCtx);
10891   }
10892
10893   if( rc==SQLITE_OK ){
10894     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
10895   }
10896
10897   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10898   return rc;
10899 }
10900
10901 /*
10902 ** Allocate a new sqlite3expert object.
10903 */
10904 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
10905   int rc = SQLITE_OK;
10906   sqlite3expert *pNew;
10907
10908   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
10909
10910   /* Open two in-memory databases to work with. The "vtab database" (dbv)
10911   ** will contain a virtual table corresponding to each real table in
10912   ** the user database schema, and a copy of each view. It is used to
10913   ** collect information regarding the WHERE, ORDER BY and other clauses
10914   ** of the user's query.
10915   */
10916   if( rc==SQLITE_OK ){
10917     pNew->db = db;
10918     pNew->iSample = 100;
10919     rc = sqlite3_open(":memory:", &pNew->dbv);
10920   }
10921   if( rc==SQLITE_OK ){
10922     rc = sqlite3_open(":memory:", &pNew->dbm);
10923     if( rc==SQLITE_OK ){
10924       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
10925     }
10926   }
10927   
10928
10929   /* Copy the entire schema of database [db] into [dbm]. */
10930   if( rc==SQLITE_OK ){
10931     sqlite3_stmt *pSql;
10932     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, 
10933         "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
10934         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
10935     );
10936     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
10937       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
10938       rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
10939     }
10940     idxFinalize(&rc, pSql);
10941   }
10942
10943   /* Create the vtab schema */
10944   if( rc==SQLITE_OK ){
10945     rc = idxCreateVtabSchema(pNew, pzErrmsg);
10946   }
10947
10948   /* Register the auth callback with dbv */
10949   if( rc==SQLITE_OK ){
10950     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
10951   }
10952
10953   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
10954   ** return the new sqlite3expert handle.  */
10955   if( rc!=SQLITE_OK ){
10956     sqlite3_expert_destroy(pNew);
10957     pNew = 0;
10958   }
10959   return pNew;
10960 }
10961
10962 /*
10963 ** Configure an sqlite3expert object.
10964 */
10965 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
10966   int rc = SQLITE_OK;
10967   va_list ap;
10968   va_start(ap, op);
10969   switch( op ){
10970     case EXPERT_CONFIG_SAMPLE: {
10971       int iVal = va_arg(ap, int);
10972       if( iVal<0 ) iVal = 0;
10973       if( iVal>100 ) iVal = 100;
10974       p->iSample = iVal;
10975       break;
10976     }
10977     default:
10978       rc = SQLITE_NOTFOUND;
10979       break;
10980   }
10981
10982   va_end(ap);
10983   return rc;
10984 }
10985
10986 /*
10987 ** Add an SQL statement to the analysis.
10988 */
10989 int sqlite3_expert_sql(
10990   sqlite3expert *p,               /* From sqlite3_expert_new() */
10991   const char *zSql,               /* SQL statement to add */
10992   char **pzErr                    /* OUT: Error message (if any) */
10993 ){
10994   IdxScan *pScanOrig = p->pScan;
10995   IdxStatement *pStmtOrig = p->pStatement;
10996   int rc = SQLITE_OK;
10997   const char *zStmt = zSql;
10998
10999   if( p->bRun ) return SQLITE_MISUSE;
11000
11001   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
11002     sqlite3_stmt *pStmt = 0;
11003     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
11004     if( rc==SQLITE_OK ){
11005       if( pStmt ){
11006         IdxStatement *pNew;
11007         const char *z = sqlite3_sql(pStmt);
11008         int n = STRLEN(z);
11009         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
11010         if( rc==SQLITE_OK ){
11011           pNew->zSql = (char*)&pNew[1];
11012           memcpy(pNew->zSql, z, n+1);
11013           pNew->pNext = p->pStatement;
11014           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
11015           p->pStatement = pNew;
11016         }
11017         sqlite3_finalize(pStmt);
11018       }
11019     }else{
11020       idxDatabaseError(p->dbv, pzErr);
11021     }
11022   }
11023
11024   if( rc!=SQLITE_OK ){
11025     idxScanFree(p->pScan, pScanOrig);
11026     idxStatementFree(p->pStatement, pStmtOrig);
11027     p->pScan = pScanOrig;
11028     p->pStatement = pStmtOrig;
11029   }
11030
11031   return rc;
11032 }
11033
11034 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
11035   int rc;
11036   IdxHashEntry *pEntry;
11037
11038   /* Do trigger processing to collect any extra IdxScan structures */
11039   rc = idxProcessTriggers(p, pzErr);
11040
11041   /* Create candidate indexes within the in-memory database file */
11042   if( rc==SQLITE_OK ){
11043     rc = idxCreateCandidates(p);
11044   }
11045
11046   /* Generate the stat1 data */
11047   if( rc==SQLITE_OK ){
11048     rc = idxPopulateStat1(p, pzErr);
11049   }
11050
11051   /* Formulate the EXPERT_REPORT_CANDIDATES text */
11052   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
11053     p->zCandidates = idxAppendText(&rc, p->zCandidates, 
11054         "%s;%s%s\n", pEntry->zVal, 
11055         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
11056     );
11057   }
11058
11059   /* Figure out which of the candidate indexes are preferred by the query
11060   ** planner and report the results to the user.  */
11061   if( rc==SQLITE_OK ){
11062     rc = idxFindIndexes(p, pzErr);
11063   }
11064
11065   if( rc==SQLITE_OK ){
11066     p->bRun = 1;
11067   }
11068   return rc;
11069 }
11070
11071 /*
11072 ** Return the total number of statements that have been added to this
11073 ** sqlite3expert using sqlite3_expert_sql().
11074 */
11075 int sqlite3_expert_count(sqlite3expert *p){
11076   int nRet = 0;
11077   if( p->pStatement ) nRet = p->pStatement->iId+1;
11078   return nRet;
11079 }
11080
11081 /*
11082 ** Return a component of the report.
11083 */
11084 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
11085   const char *zRet = 0;
11086   IdxStatement *pStmt;
11087
11088   if( p->bRun==0 ) return 0;
11089   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
11090   switch( eReport ){
11091     case EXPERT_REPORT_SQL:
11092       if( pStmt ) zRet = pStmt->zSql;
11093       break;
11094     case EXPERT_REPORT_INDEXES:
11095       if( pStmt ) zRet = pStmt->zIdx;
11096       break;
11097     case EXPERT_REPORT_PLAN:
11098       if( pStmt ) zRet = pStmt->zEQP;
11099       break;
11100     case EXPERT_REPORT_CANDIDATES:
11101       zRet = p->zCandidates;
11102       break;
11103   }
11104   return zRet;
11105 }
11106
11107 /*
11108 ** Free an sqlite3expert object.
11109 */
11110 void sqlite3_expert_destroy(sqlite3expert *p){
11111   if( p ){
11112     sqlite3_close(p->dbm);
11113     sqlite3_close(p->dbv);
11114     idxScanFree(p->pScan, 0);
11115     idxStatementFree(p->pStatement, 0);
11116     idxTableFree(p->pTable);
11117     idxWriteFree(p->pWrite);
11118     idxHashClear(&p->hIdx);
11119     sqlite3_free(p->zCandidates);
11120     sqlite3_free(p);
11121   }
11122 }
11123
11124 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11125
11126 /************************* End ../ext/expert/sqlite3expert.c ********************/
11127
11128 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11129 /************************* Begin ../ext/misc/dbdata.c ******************/
11130 /*
11131 ** 2019-04-17
11132 **
11133 ** The author disclaims copyright to this source code.  In place of
11134 ** a legal notice, here is a blessing:
11135 **
11136 **    May you do good and not evil.
11137 **    May you find forgiveness for yourself and forgive others.
11138 **    May you share freely, never taking more than you give.
11139 **
11140 ******************************************************************************
11141 **
11142 ** This file contains an implementation of two eponymous virtual tables,
11143 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
11144 ** "sqlite_dbpage" eponymous virtual table be available.
11145 **
11146 ** SQLITE_DBDATA:
11147 **   sqlite_dbdata is used to extract data directly from a database b-tree
11148 **   page and its associated overflow pages, bypassing the b-tree layer.
11149 **   The table schema is equivalent to:
11150 **
11151 **     CREATE TABLE sqlite_dbdata(
11152 **       pgno INTEGER,
11153 **       cell INTEGER,
11154 **       field INTEGER,
11155 **       value ANY,
11156 **       schema TEXT HIDDEN
11157 **     );
11158 **
11159 **   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
11160 **   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
11161 **   "schema".
11162 **
11163 **   Each page of the database is inspected. If it cannot be interpreted as
11164 **   a b-tree page, or if it is a b-tree page containing 0 entries, the
11165 **   sqlite_dbdata table contains no rows for that page.  Otherwise, the
11166 **   table contains one row for each field in the record associated with
11167 **   each cell on the page. For intkey b-trees, the key value is stored in
11168 **   field -1.
11169 **
11170 **   For example, for the database:
11171 **
11172 **     CREATE TABLE t1(a, b);     -- root page is page 2
11173 **     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
11174 **     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
11175 **
11176 **   the sqlite_dbdata table contains, as well as from entries related to 
11177 **   page 1, content equivalent to:
11178 **
11179 **     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
11180 **         (2, 0, -1, 5     ),
11181 **         (2, 0,  0, 'v'   ),
11182 **         (2, 0,  1, 'five'),
11183 **         (2, 1, -1, 10    ),
11184 **         (2, 1,  0, 'x'   ),
11185 **         (2, 1,  1, 'ten' );
11186 **
11187 **   If database corruption is encountered, this module does not report an
11188 **   error. Instead, it attempts to extract as much data as possible and
11189 **   ignores the corruption.
11190 **
11191 ** SQLITE_DBPTR:
11192 **   The sqlite_dbptr table has the following schema:
11193 **
11194 **     CREATE TABLE sqlite_dbptr(
11195 **       pgno INTEGER,
11196 **       child INTEGER,
11197 **       schema TEXT HIDDEN
11198 **     );
11199 **
11200 **   It contains one entry for each b-tree pointer between a parent and
11201 **   child page in the database.
11202 */
11203 #if !defined(SQLITEINT_H) 
11204 /* #include "sqlite3ext.h" */
11205
11206 /* typedef unsigned char u8; */
11207
11208 #endif
11209 SQLITE_EXTENSION_INIT1
11210 #include <string.h>
11211 #include <assert.h>
11212
11213 #define DBDATA_PADDING_BYTES 100 
11214
11215 typedef struct DbdataTable DbdataTable;
11216 typedef struct DbdataCursor DbdataCursor;
11217
11218 /* Cursor object */
11219 struct DbdataCursor {
11220   sqlite3_vtab_cursor base;       /* Base class.  Must be first */
11221   sqlite3_stmt *pStmt;            /* For fetching database pages */
11222
11223   int iPgno;                      /* Current page number */
11224   u8 *aPage;                      /* Buffer containing page */
11225   int nPage;                      /* Size of aPage[] in bytes */
11226   int nCell;                      /* Number of cells on aPage[] */
11227   int iCell;                      /* Current cell number */
11228   int bOnePage;                   /* True to stop after one page */
11229   int szDb;
11230   sqlite3_int64 iRowid;
11231
11232   /* Only for the sqlite_dbdata table */
11233   u8 *pRec;                       /* Buffer containing current record */
11234   int nRec;                       /* Size of pRec[] in bytes */
11235   int nHdr;                       /* Size of header in bytes */
11236   int iField;                     /* Current field number */
11237   u8 *pHdrPtr;
11238   u8 *pPtr;
11239   
11240   sqlite3_int64 iIntkey;          /* Integer key value */
11241 };
11242
11243 /* Table object */
11244 struct DbdataTable {
11245   sqlite3_vtab base;              /* Base class.  Must be first */
11246   sqlite3 *db;                    /* The database connection */
11247   sqlite3_stmt *pStmt;            /* For fetching database pages */
11248   int bPtr;                       /* True for sqlite3_dbptr table */
11249 };
11250
11251 /* Column and schema definitions for sqlite_dbdata */
11252 #define DBDATA_COLUMN_PGNO        0
11253 #define DBDATA_COLUMN_CELL        1
11254 #define DBDATA_COLUMN_FIELD       2
11255 #define DBDATA_COLUMN_VALUE       3
11256 #define DBDATA_COLUMN_SCHEMA      4
11257 #define DBDATA_SCHEMA             \
11258       "CREATE TABLE x("           \
11259       "  pgno INTEGER,"           \
11260       "  cell INTEGER,"           \
11261       "  field INTEGER,"          \
11262       "  value ANY,"              \
11263       "  schema TEXT HIDDEN"      \
11264       ")"
11265
11266 /* Column and schema definitions for sqlite_dbptr */
11267 #define DBPTR_COLUMN_PGNO         0
11268 #define DBPTR_COLUMN_CHILD        1
11269 #define DBPTR_COLUMN_SCHEMA       2
11270 #define DBPTR_SCHEMA              \
11271       "CREATE TABLE x("           \
11272       "  pgno INTEGER,"           \
11273       "  child INTEGER,"          \
11274       "  schema TEXT HIDDEN"      \
11275       ")"
11276
11277 /*
11278 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual 
11279 ** table.
11280 */
11281 static int dbdataConnect(
11282   sqlite3 *db,
11283   void *pAux,
11284   int argc, const char *const*argv,
11285   sqlite3_vtab **ppVtab,
11286   char **pzErr
11287 ){
11288   DbdataTable *pTab = 0;
11289   int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
11290
11291   if( rc==SQLITE_OK ){
11292     pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
11293     if( pTab==0 ){
11294       rc = SQLITE_NOMEM;
11295     }else{
11296       memset(pTab, 0, sizeof(DbdataTable));
11297       pTab->db = db;
11298       pTab->bPtr = (pAux!=0);
11299     }
11300   }
11301
11302   *ppVtab = (sqlite3_vtab*)pTab;
11303   return rc;
11304 }
11305
11306 /*
11307 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
11308 */
11309 static int dbdataDisconnect(sqlite3_vtab *pVtab){
11310   DbdataTable *pTab = (DbdataTable*)pVtab;
11311   if( pTab ){
11312     sqlite3_finalize(pTab->pStmt);
11313     sqlite3_free(pVtab);
11314   }
11315   return SQLITE_OK;
11316 }
11317
11318 /*
11319 ** This function interprets two types of constraints:
11320 **
11321 **       schema=?
11322 **       pgno=?
11323 **
11324 ** If neither are present, idxNum is set to 0. If schema=? is present,
11325 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
11326 ** in idxNum is set.
11327 **
11328 ** If both parameters are present, schema is in position 0 and pgno in
11329 ** position 1.
11330 */
11331 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
11332   DbdataTable *pTab = (DbdataTable*)tab;
11333   int i;
11334   int iSchema = -1;
11335   int iPgno = -1;
11336   int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
11337
11338   for(i=0; i<pIdx->nConstraint; i++){
11339     struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
11340     if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
11341       if( p->iColumn==colSchema ){
11342         if( p->usable==0 ) return SQLITE_CONSTRAINT;
11343         iSchema = i;
11344       }
11345       if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
11346         iPgno = i;
11347       }
11348     }
11349   }
11350
11351   if( iSchema>=0 ){
11352     pIdx->aConstraintUsage[iSchema].argvIndex = 1;
11353     pIdx->aConstraintUsage[iSchema].omit = 1;
11354   }
11355   if( iPgno>=0 ){
11356     pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
11357     pIdx->aConstraintUsage[iPgno].omit = 1;
11358     pIdx->estimatedCost = 100;
11359     pIdx->estimatedRows =  50;
11360
11361     if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
11362       int iCol = pIdx->aOrderBy[0].iColumn;
11363       if( pIdx->nOrderBy==1 ){
11364         pIdx->orderByConsumed = (iCol==0 || iCol==1);
11365       }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
11366         pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
11367       }
11368     }
11369
11370   }else{
11371     pIdx->estimatedCost = 100000000;
11372     pIdx->estimatedRows = 1000000000;
11373   }
11374   pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
11375   return SQLITE_OK;
11376 }
11377
11378 /*
11379 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
11380 */
11381 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
11382   DbdataCursor *pCsr;
11383
11384   pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
11385   if( pCsr==0 ){
11386     return SQLITE_NOMEM;
11387   }else{
11388     memset(pCsr, 0, sizeof(DbdataCursor));
11389     pCsr->base.pVtab = pVTab;
11390   }
11391
11392   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
11393   return SQLITE_OK;
11394 }
11395
11396 /*
11397 ** Restore a cursor object to the state it was in when first allocated 
11398 ** by dbdataOpen().
11399 */
11400 static void dbdataResetCursor(DbdataCursor *pCsr){
11401   DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
11402   if( pTab->pStmt==0 ){
11403     pTab->pStmt = pCsr->pStmt;
11404   }else{
11405     sqlite3_finalize(pCsr->pStmt);
11406   }
11407   pCsr->pStmt = 0;
11408   pCsr->iPgno = 1;
11409   pCsr->iCell = 0;
11410   pCsr->iField = 0;
11411   pCsr->bOnePage = 0;
11412   sqlite3_free(pCsr->aPage);
11413   sqlite3_free(pCsr->pRec);
11414   pCsr->pRec = 0;
11415   pCsr->aPage = 0;
11416 }
11417
11418 /*
11419 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
11420 */
11421 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
11422   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11423   dbdataResetCursor(pCsr);
11424   sqlite3_free(pCsr);
11425   return SQLITE_OK;
11426 }
11427
11428 /* 
11429 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers. 
11430 */
11431 static unsigned int get_uint16(unsigned char *a){
11432   return (a[0]<<8)|a[1];
11433 }
11434 static unsigned int get_uint32(unsigned char *a){
11435   return ((unsigned int)a[0]<<24)
11436        | ((unsigned int)a[1]<<16)
11437        | ((unsigned int)a[2]<<8)
11438        | ((unsigned int)a[3]);
11439 }
11440
11441 /*
11442 ** Load page pgno from the database via the sqlite_dbpage virtual table.
11443 ** If successful, set (*ppPage) to point to a buffer containing the page
11444 ** data, (*pnPage) to the size of that buffer in bytes and return
11445 ** SQLITE_OK. In this case it is the responsibility of the caller to
11446 ** eventually free the buffer using sqlite3_free().
11447 **
11448 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
11449 ** return an SQLite error code.
11450 */
11451 static int dbdataLoadPage(
11452   DbdataCursor *pCsr,             /* Cursor object */
11453   unsigned int pgno,              /* Page number of page to load */
11454   u8 **ppPage,                    /* OUT: pointer to page buffer */
11455   int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
11456 ){
11457   int rc2;
11458   int rc = SQLITE_OK;
11459   sqlite3_stmt *pStmt = pCsr->pStmt;
11460
11461   *ppPage = 0;
11462   *pnPage = 0;
11463   sqlite3_bind_int64(pStmt, 2, pgno);
11464   if( SQLITE_ROW==sqlite3_step(pStmt) ){
11465     int nCopy = sqlite3_column_bytes(pStmt, 0);
11466     if( nCopy>0 ){
11467       u8 *pPage;
11468       pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11469       if( pPage==0 ){
11470         rc = SQLITE_NOMEM;
11471       }else{
11472         const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11473         memcpy(pPage, pCopy, nCopy);
11474         memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11475       }
11476       *ppPage = pPage;
11477       *pnPage = nCopy;
11478     }
11479   }
11480   rc2 = sqlite3_reset(pStmt);
11481   if( rc==SQLITE_OK ) rc = rc2;
11482
11483   return rc;
11484 }
11485
11486 /*
11487 ** Read a varint.  Put the value in *pVal and return the number of bytes.
11488 */
11489 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11490   sqlite3_int64 v = 0;
11491   int i;
11492   for(i=0; i<8; i++){
11493     v = (v<<7) + (z[i]&0x7f);
11494     if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
11495   }
11496   v = (v<<8) + (z[i]&0xff);
11497   *pVal = v;
11498   return 9;
11499 }
11500
11501 /*
11502 ** Return the number of bytes of space used by an SQLite value of type
11503 ** eType.
11504 */
11505 static int dbdataValueBytes(int eType){
11506   switch( eType ){
11507     case 0: case 8: case 9:
11508     case 10: case 11:
11509       return 0;
11510     case 1:
11511       return 1;
11512     case 2:
11513       return 2;
11514     case 3:
11515       return 3;
11516     case 4:
11517       return 4;
11518     case 5:
11519       return 6;
11520     case 6:
11521     case 7:
11522       return 8;
11523     default:
11524       if( eType>0 ){
11525         return ((eType-12) / 2);
11526       }
11527       return 0;
11528   }
11529 }
11530
11531 /*
11532 ** Load a value of type eType from buffer pData and use it to set the
11533 ** result of context object pCtx.
11534 */
11535 static void dbdataValue(
11536   sqlite3_context *pCtx, 
11537   int eType, 
11538   u8 *pData,
11539   int nData
11540 ){
11541   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
11542     switch( eType ){
11543       case 0: 
11544       case 10: 
11545       case 11: 
11546         sqlite3_result_null(pCtx);
11547         break;
11548       
11549       case 8: 
11550         sqlite3_result_int(pCtx, 0);
11551         break;
11552       case 9:
11553         sqlite3_result_int(pCtx, 1);
11554         break;
11555   
11556       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
11557         sqlite3_uint64 v = (signed char)pData[0];
11558         pData++;
11559         switch( eType ){
11560           case 7:
11561           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
11562           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
11563           case 4:  v = (v<<8) + pData[0];  pData++;
11564           case 3:  v = (v<<8) + pData[0];  pData++;
11565           case 2:  v = (v<<8) + pData[0];  pData++;
11566         }
11567   
11568         if( eType==7 ){
11569           double r;
11570           memcpy(&r, &v, sizeof(r));
11571           sqlite3_result_double(pCtx, r);
11572         }else{
11573           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
11574         }
11575         break;
11576       }
11577   
11578       default: {
11579         int n = ((eType-12) / 2);
11580         if( eType % 2 ){
11581           sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
11582         }else{
11583           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
11584         }
11585       }
11586     }
11587   }
11588 }
11589
11590 /*
11591 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
11592 */
11593 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
11594   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11595   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11596
11597   pCsr->iRowid++;
11598   while( 1 ){
11599     int rc;
11600     int iOff = (pCsr->iPgno==1 ? 100 : 0);
11601     int bNextPage = 0;
11602
11603     if( pCsr->aPage==0 ){
11604       while( 1 ){
11605         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
11606         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
11607         if( rc!=SQLITE_OK ) return rc;
11608         if( pCsr->aPage ) break;
11609         pCsr->iPgno++;
11610       }
11611       pCsr->iCell = pTab->bPtr ? -2 : 0;
11612       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
11613     }
11614
11615     if( pTab->bPtr ){
11616       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
11617         pCsr->iCell = pCsr->nCell;
11618       }
11619       pCsr->iCell++;
11620       if( pCsr->iCell>=pCsr->nCell ){
11621         sqlite3_free(pCsr->aPage);
11622         pCsr->aPage = 0;
11623         if( pCsr->bOnePage ) return SQLITE_OK;
11624         pCsr->iPgno++;
11625       }else{
11626         return SQLITE_OK;
11627       }
11628     }else{
11629       /* If there is no record loaded, load it now. */
11630       if( pCsr->pRec==0 ){
11631         int bHasRowid = 0;
11632         int nPointer = 0;
11633         sqlite3_int64 nPayload = 0;
11634         sqlite3_int64 nHdr = 0;
11635         int iHdr;
11636         int U, X;
11637         int nLocal;
11638   
11639         switch( pCsr->aPage[iOff] ){
11640           case 0x02:
11641             nPointer = 4;
11642             break;
11643           case 0x0a:
11644             break;
11645           case 0x0d:
11646             bHasRowid = 1;
11647             break;
11648           default:
11649             /* This is not a b-tree page with records on it. Continue. */
11650             pCsr->iCell = pCsr->nCell;
11651             break;
11652         }
11653
11654         if( pCsr->iCell>=pCsr->nCell ){
11655           bNextPage = 1;
11656         }else{
11657   
11658           iOff += 8 + nPointer + pCsr->iCell*2;
11659           if( iOff>pCsr->nPage ){
11660             bNextPage = 1;
11661           }else{
11662             iOff = get_uint16(&pCsr->aPage[iOff]);
11663           }
11664     
11665           /* For an interior node cell, skip past the child-page number */
11666           iOff += nPointer;
11667     
11668           /* Load the "byte of payload including overflow" field */
11669           if( bNextPage || iOff>pCsr->nPage ){
11670             bNextPage = 1;
11671           }else{
11672             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
11673           }
11674     
11675           /* If this is a leaf intkey cell, load the rowid */
11676           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
11677             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
11678           }
11679     
11680           /* Figure out how much data to read from the local page */
11681           U = pCsr->nPage;
11682           if( bHasRowid ){
11683             X = U-35;
11684           }else{
11685             X = ((U-12)*64/255)-23;
11686           }
11687           if( nPayload<=X ){
11688             nLocal = nPayload;
11689           }else{
11690             int M, K;
11691             M = ((U-12)*32/255)-23;
11692             K = M+((nPayload-M)%(U-4));
11693             if( K<=X ){
11694               nLocal = K;
11695             }else{
11696               nLocal = M;
11697             }
11698           }
11699
11700           if( bNextPage || nLocal+iOff>pCsr->nPage ){
11701             bNextPage = 1;
11702           }else{
11703
11704             /* Allocate space for payload. And a bit more to catch small buffer
11705             ** overruns caused by attempting to read a varint or similar from 
11706             ** near the end of a corrupt record.  */
11707             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
11708             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
11709             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
11710             pCsr->nRec = nPayload;
11711
11712             /* Load the nLocal bytes of payload */
11713             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
11714             iOff += nLocal;
11715
11716             /* Load content from overflow pages */
11717             if( nPayload>nLocal ){
11718               sqlite3_int64 nRem = nPayload - nLocal;
11719               unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
11720               while( nRem>0 ){
11721                 u8 *aOvfl = 0;
11722                 int nOvfl = 0;
11723                 int nCopy;
11724                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
11725                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
11726                 if( rc!=SQLITE_OK ) return rc;
11727                 if( aOvfl==0 ) break;
11728
11729                 nCopy = U-4;
11730                 if( nCopy>nRem ) nCopy = nRem;
11731                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
11732                 nRem -= nCopy;
11733
11734                 pgnoOvfl = get_uint32(aOvfl);
11735                 sqlite3_free(aOvfl);
11736               }
11737             }
11738     
11739             iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
11740             pCsr->nHdr = nHdr;
11741             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
11742             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
11743             pCsr->iField = (bHasRowid ? -1 : 0);
11744           }
11745         }
11746       }else{
11747         pCsr->iField++;
11748         if( pCsr->iField>0 ){
11749           sqlite3_int64 iType;
11750           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
11751             bNextPage = 1;
11752           }else{
11753             pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
11754             pCsr->pPtr += dbdataValueBytes(iType);
11755           }
11756         }
11757       }
11758
11759       if( bNextPage ){
11760         sqlite3_free(pCsr->aPage);
11761         sqlite3_free(pCsr->pRec);
11762         pCsr->aPage = 0;
11763         pCsr->pRec = 0;
11764         if( pCsr->bOnePage ) return SQLITE_OK;
11765         pCsr->iPgno++;
11766       }else{
11767         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
11768           return SQLITE_OK;
11769         }
11770
11771         /* Advance to the next cell. The next iteration of the loop will load
11772         ** the record and so on. */
11773         sqlite3_free(pCsr->pRec);
11774         pCsr->pRec = 0;
11775         pCsr->iCell++;
11776       }
11777     }
11778   }
11779
11780   assert( !"can't get here" );
11781   return SQLITE_OK;
11782 }
11783
11784 /* 
11785 ** Return true if the cursor is at EOF.
11786 */
11787 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
11788   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11789   return pCsr->aPage==0;
11790 }
11791
11792 /* 
11793 ** Determine the size in pages of database zSchema (where zSchema is
11794 ** "main", "temp" or the name of an attached database) and set 
11795 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
11796 ** an SQLite error code.
11797 */
11798 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
11799   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
11800   char *zSql = 0;
11801   int rc, rc2;
11802   sqlite3_stmt *pStmt = 0;
11803
11804   zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
11805   if( zSql==0 ) return SQLITE_NOMEM;
11806   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
11807   sqlite3_free(zSql);
11808   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11809     pCsr->szDb = sqlite3_column_int(pStmt, 0);
11810   }
11811   rc2 = sqlite3_finalize(pStmt);
11812   if( rc==SQLITE_OK ) rc = rc2;
11813   return rc;
11814 }
11815
11816 /* 
11817 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
11818 */
11819 static int dbdataFilter(
11820   sqlite3_vtab_cursor *pCursor, 
11821   int idxNum, const char *idxStr,
11822   int argc, sqlite3_value **argv
11823 ){
11824   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11825   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11826   int rc = SQLITE_OK;
11827   const char *zSchema = "main";
11828
11829   dbdataResetCursor(pCsr);
11830   assert( pCsr->iPgno==1 );
11831   if( idxNum & 0x01 ){
11832     zSchema = (const char*)sqlite3_value_text(argv[0]);
11833   }
11834   if( idxNum & 0x02 ){
11835     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
11836     pCsr->bOnePage = 1;
11837   }else{
11838     pCsr->nPage = dbdataDbsize(pCsr, zSchema);
11839     rc = dbdataDbsize(pCsr, zSchema);
11840   }
11841
11842   if( rc==SQLITE_OK ){
11843     if( pTab->pStmt ){
11844       pCsr->pStmt = pTab->pStmt;
11845       pTab->pStmt = 0;
11846     }else{
11847       rc = sqlite3_prepare_v2(pTab->db, 
11848           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
11849           &pCsr->pStmt, 0
11850       );
11851     }
11852   }
11853   if( rc==SQLITE_OK ){
11854     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
11855   }else{
11856     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
11857   }
11858   if( rc==SQLITE_OK ){
11859     rc = dbdataNext(pCursor);
11860   }
11861   return rc;
11862 }
11863
11864 /* 
11865 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
11866 */
11867 static int dbdataColumn(
11868   sqlite3_vtab_cursor *pCursor, 
11869   sqlite3_context *ctx, 
11870   int i
11871 ){
11872   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11873   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11874   if( pTab->bPtr ){
11875     switch( i ){
11876       case DBPTR_COLUMN_PGNO:
11877         sqlite3_result_int64(ctx, pCsr->iPgno);
11878         break;
11879       case DBPTR_COLUMN_CHILD: {
11880         int iOff = pCsr->iPgno==1 ? 100 : 0;
11881         if( pCsr->iCell<0 ){
11882           iOff += 8;
11883         }else{
11884           iOff += 12 + pCsr->iCell*2;
11885           if( iOff>pCsr->nPage ) return SQLITE_OK;
11886           iOff = get_uint16(&pCsr->aPage[iOff]);
11887         }
11888         if( iOff<=pCsr->nPage ){
11889           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
11890         }
11891         break;
11892       }
11893     }
11894   }else{
11895     switch( i ){
11896       case DBDATA_COLUMN_PGNO:
11897         sqlite3_result_int64(ctx, pCsr->iPgno);
11898         break;
11899       case DBDATA_COLUMN_CELL:
11900         sqlite3_result_int(ctx, pCsr->iCell);
11901         break;
11902       case DBDATA_COLUMN_FIELD:
11903         sqlite3_result_int(ctx, pCsr->iField);
11904         break;
11905       case DBDATA_COLUMN_VALUE: {
11906         if( pCsr->iField<0 ){
11907           sqlite3_result_int64(ctx, pCsr->iIntkey);
11908         }else{
11909           sqlite3_int64 iType;
11910           dbdataGetVarint(pCsr->pHdrPtr, &iType);
11911           dbdataValue(
11912               ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
11913           );
11914         }
11915         break;
11916       }
11917     }
11918   }
11919   return SQLITE_OK;
11920 }
11921
11922 /* 
11923 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
11924 */
11925 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
11926   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11927   *pRowid = pCsr->iRowid;
11928   return SQLITE_OK;
11929 }
11930
11931
11932 /*
11933 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
11934 */
11935 static int sqlite3DbdataRegister(sqlite3 *db){
11936   static sqlite3_module dbdata_module = {
11937     0,                            /* iVersion */
11938     0,                            /* xCreate */
11939     dbdataConnect,                /* xConnect */
11940     dbdataBestIndex,              /* xBestIndex */
11941     dbdataDisconnect,             /* xDisconnect */
11942     0,                            /* xDestroy */
11943     dbdataOpen,                   /* xOpen - open a cursor */
11944     dbdataClose,                  /* xClose - close a cursor */
11945     dbdataFilter,                 /* xFilter - configure scan constraints */
11946     dbdataNext,                   /* xNext - advance a cursor */
11947     dbdataEof,                    /* xEof - check for end of scan */
11948     dbdataColumn,                 /* xColumn - read data */
11949     dbdataRowid,                  /* xRowid - read data */
11950     0,                            /* xUpdate */
11951     0,                            /* xBegin */
11952     0,                            /* xSync */
11953     0,                            /* xCommit */
11954     0,                            /* xRollback */
11955     0,                            /* xFindMethod */
11956     0,                            /* xRename */
11957     0,                            /* xSavepoint */
11958     0,                            /* xRelease */
11959     0,                            /* xRollbackTo */
11960     0                             /* xShadowName */
11961   };
11962
11963   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
11964   if( rc==SQLITE_OK ){
11965     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
11966   }
11967   return rc;
11968 }
11969
11970 #ifdef _WIN32
11971
11972 #endif
11973 int sqlite3_dbdata_init(
11974   sqlite3 *db, 
11975   char **pzErrMsg, 
11976   const sqlite3_api_routines *pApi
11977 ){
11978   SQLITE_EXTENSION_INIT2(pApi);
11979   return sqlite3DbdataRegister(db);
11980 }
11981
11982 /************************* End ../ext/misc/dbdata.c ********************/
11983 #endif
11984
11985 #if defined(SQLITE_ENABLE_SESSION)
11986 /*
11987 ** State information for a single open session
11988 */
11989 typedef struct OpenSession OpenSession;
11990 struct OpenSession {
11991   char *zName;             /* Symbolic name for this session */
11992   int nFilter;             /* Number of xFilter rejection GLOB patterns */
11993   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
11994   sqlite3_session *p;      /* The open session */
11995 };
11996 #endif
11997
11998 typedef struct ExpertInfo ExpertInfo;
11999 struct ExpertInfo {
12000   sqlite3expert *pExpert;
12001   int bVerbose;
12002 };
12003
12004 /* A single line in the EQP output */
12005 typedef struct EQPGraphRow EQPGraphRow;
12006 struct EQPGraphRow {
12007   int iEqpId;           /* ID for this row */
12008   int iParentId;        /* ID of the parent row */
12009   EQPGraphRow *pNext;   /* Next row in sequence */
12010   char zText[1];        /* Text to display for this row */
12011 };
12012
12013 /* All EQP output is collected into an instance of the following */
12014 typedef struct EQPGraph EQPGraph;
12015 struct EQPGraph {
12016   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
12017   EQPGraphRow *pLast;   /* Last element of the pRow list */
12018   char zPrefix[100];    /* Graph prefix */
12019 };
12020
12021 /*
12022 ** State information about the database connection is contained in an
12023 ** instance of the following structure.
12024 */
12025 typedef struct ShellState ShellState;
12026 struct ShellState {
12027   sqlite3 *db;           /* The database */
12028   u8 autoExplain;        /* Automatically turn on .explain mode */
12029   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
12030   u8 autoEQPtest;        /* autoEQP is in test mode */
12031   u8 autoEQPtrace;       /* autoEQP is in trace mode */
12032   u8 scanstatsOn;        /* True to display scan stats before each finalize */
12033   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
12034   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
12035   u8 nEqpLevel;          /* Depth of the EQP output graph */
12036   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
12037   unsigned statsOn;      /* True to display memory stats before each finalize */
12038   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
12039   int outCount;          /* Revert to stdout when reaching zero */
12040   int cnt;               /* Number of records displayed so far */
12041   int lineno;            /* Line number of last line read from in */
12042   int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
12043   FILE *in;              /* Read commands from this stream */
12044   FILE *out;             /* Write results here */
12045   FILE *traceOut;        /* Output for sqlite3_trace() */
12046   int nErr;              /* Number of errors seen */
12047   int mode;              /* An output mode setting */
12048   int modePrior;         /* Saved mode */
12049   int cMode;             /* temporary output mode for the current query */
12050   int normalMode;        /* Output mode before ".explain on" */
12051   int writableSchema;    /* True if PRAGMA writable_schema=ON */
12052   int showHeader;        /* True to show column names in List or Column mode */
12053   int nCheck;            /* Number of ".check" commands run */
12054   unsigned nProgress;    /* Number of progress callbacks encountered */
12055   unsigned mxProgress;   /* Maximum progress callbacks before failing */
12056   unsigned flgProgress;  /* Flags for the progress callback */
12057   unsigned shellFlgs;    /* Various flags */
12058   unsigned priorShFlgs;  /* Saved copy of flags */
12059   sqlite3_int64 szMax;   /* --maxsize argument to .open */
12060   char *zDestTable;      /* Name of destination table when MODE_Insert */
12061   char *zTempFile;       /* Temporary file that might need deleting */
12062   char zTestcase[30];    /* Name of current test case */
12063   char colSeparator[20]; /* Column separator character for several modes */
12064   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
12065   char colSepPrior[20];  /* Saved column separator */
12066   char rowSepPrior[20];  /* Saved row separator */
12067   int *colWidth;         /* Requested width of each column in columnar modes */
12068   int *actualWidth;      /* Actual width of each column */
12069   int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
12070   char nullValue[20];    /* The text to print when a NULL comes back from
12071                          ** the database */
12072   char outfile[FILENAME_MAX]; /* Filename for *out */
12073   const char *zDbFilename;    /* name of the database file */
12074   char *zFreeOnClose;         /* Filename to free when closing */
12075   const char *zVfs;           /* Name of VFS to use */
12076   sqlite3_stmt *pStmt;   /* Current statement if any. */
12077   FILE *pLog;            /* Write log output here */
12078   int *aiIndent;         /* Array of indents used in MODE_Explain */
12079   int nIndent;           /* Size of array aiIndent[] */
12080   int iIndent;           /* Index of current op in aiIndent[] */
12081   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
12082 #if defined(SQLITE_ENABLE_SESSION)
12083   int nSession;             /* Number of active sessions */
12084   OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
12085 #endif
12086   ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
12087 };
12088
12089
12090 /* Allowed values for ShellState.autoEQP
12091 */
12092 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
12093 #define AUTOEQP_on       1           /* Automatic EQP is on */
12094 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
12095 #define AUTOEQP_full     3           /* Show full EXPLAIN */
12096
12097 /* Allowed values for ShellState.openMode
12098 */
12099 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
12100 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
12101 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
12102 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
12103 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
12104 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
12105 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
12106
12107 /* Allowed values for ShellState.eTraceType
12108 */
12109 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
12110 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
12111 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
12112
12113 /* Bits in the ShellState.flgProgress variable */
12114 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
12115 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
12116                                    ** callback limit is reached, and for each
12117                                    ** top-level SQL statement */
12118 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
12119
12120 /*
12121 ** These are the allowed shellFlgs values
12122 */
12123 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
12124 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
12125 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
12126 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
12127 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
12128 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
12129 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
12130 #define SHFLG_HeaderSet      0x00000080 /* .header has been used */
12131 #define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
12132 #define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
12133
12134 /*
12135 ** Macros for testing and setting shellFlgs
12136 */
12137 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
12138 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
12139 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
12140
12141 /*
12142 ** These are the allowed modes.
12143 */
12144 #define MODE_Line     0  /* One column per line.  Blank line between records */
12145 #define MODE_Column   1  /* One record per line in neat columns */
12146 #define MODE_List     2  /* One record per line with a separator */
12147 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
12148 #define MODE_Html     4  /* Generate an XHTML table */
12149 #define MODE_Insert   5  /* Generate SQL "insert" statements */
12150 #define MODE_Quote    6  /* Quote values as for SQL */
12151 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
12152 #define MODE_Csv      8  /* Quote strings, numbers are plain */
12153 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
12154 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
12155 #define MODE_Pretty  11  /* Pretty-print schemas */
12156 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
12157 #define MODE_Json    13  /* Output JSON */
12158 #define MODE_Markdown 14 /* Markdown formatting */
12159 #define MODE_Table   15  /* MySQL-style table formatting */
12160 #define MODE_Box     16  /* Unicode box-drawing characters */
12161
12162 static const char *modeDescr[] = {
12163   "line",
12164   "column",
12165   "list",
12166   "semi",
12167   "html",
12168   "insert",
12169   "quote",
12170   "tcl",
12171   "csv",
12172   "explain",
12173   "ascii",
12174   "prettyprint",
12175   "eqp",
12176   "json",
12177   "markdown",
12178   "table",
12179   "box"
12180 };
12181
12182 /*
12183 ** These are the column/row/line separators used by the various
12184 ** import/export modes.
12185 */
12186 #define SEP_Column    "|"
12187 #define SEP_Row       "\n"
12188 #define SEP_Tab       "\t"
12189 #define SEP_Space     " "
12190 #define SEP_Comma     ","
12191 #define SEP_CrLf      "\r\n"
12192 #define SEP_Unit      "\x1F"
12193 #define SEP_Record    "\x1E"
12194
12195 /*
12196 ** A callback for the sqlite3_log() interface.
12197 */
12198 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
12199   ShellState *p = (ShellState*)pArg;
12200   if( p->pLog==0 ) return;
12201   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
12202   fflush(p->pLog);
12203 }
12204
12205 /*
12206 ** SQL function:  shell_putsnl(X)
12207 **
12208 ** Write the text X to the screen (or whatever output is being directed)
12209 ** adding a newline at the end, and then return X.
12210 */
12211 static void shellPutsFunc(
12212   sqlite3_context *pCtx,
12213   int nVal,
12214   sqlite3_value **apVal
12215 ){
12216   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
12217   (void)nVal;
12218   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
12219   sqlite3_result_value(pCtx, apVal[0]);
12220 }
12221
12222 /*
12223 ** SQL function:   edit(VALUE)
12224 **                 edit(VALUE,EDITOR)
12225 **
12226 ** These steps:
12227 **
12228 **     (1) Write VALUE into a temporary file.
12229 **     (2) Run program EDITOR on that temporary file.
12230 **     (3) Read the temporary file back and return its content as the result.
12231 **     (4) Delete the temporary file
12232 **
12233 ** If the EDITOR argument is omitted, use the value in the VISUAL
12234 ** environment variable.  If still there is no EDITOR, through an error.
12235 **
12236 ** Also throw an error if the EDITOR program returns a non-zero exit code.
12237 */
12238 #ifndef SQLITE_NOHAVE_SYSTEM
12239 static void editFunc(
12240   sqlite3_context *context,
12241   int argc,
12242   sqlite3_value **argv
12243 ){
12244   const char *zEditor;
12245   char *zTempFile = 0;
12246   sqlite3 *db;
12247   char *zCmd = 0;
12248   int bBin;
12249   int rc;
12250   int hasCRNL = 0;
12251   FILE *f = 0;
12252   sqlite3_int64 sz;
12253   sqlite3_int64 x;
12254   unsigned char *p = 0;
12255
12256   if( argc==2 ){
12257     zEditor = (const char*)sqlite3_value_text(argv[1]);
12258   }else{
12259     zEditor = getenv("VISUAL");
12260   }
12261   if( zEditor==0 ){
12262     sqlite3_result_error(context, "no editor for edit()", -1);
12263     return;
12264   }
12265   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
12266     sqlite3_result_error(context, "NULL input to edit()", -1);
12267     return;
12268   }
12269   db = sqlite3_context_db_handle(context);
12270   zTempFile = 0;
12271   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
12272   if( zTempFile==0 ){
12273     sqlite3_uint64 r = 0;
12274     sqlite3_randomness(sizeof(r), &r);
12275     zTempFile = sqlite3_mprintf("temp%llx", r);
12276     if( zTempFile==0 ){
12277       sqlite3_result_error_nomem(context);
12278       return;
12279     }
12280   }
12281   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
12282   /* When writing the file to be edited, do \n to \r\n conversions on systems
12283   ** that want \r\n line endings */
12284   f = fopen(zTempFile, bBin ? "wb" : "w");
12285   if( f==0 ){
12286     sqlite3_result_error(context, "edit() cannot open temp file", -1);
12287     goto edit_func_end;
12288   }
12289   sz = sqlite3_value_bytes(argv[0]);
12290   if( bBin ){
12291     x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
12292   }else{
12293     const char *z = (const char*)sqlite3_value_text(argv[0]);
12294     /* Remember whether or not the value originally contained \r\n */
12295     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
12296     x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
12297   }
12298   fclose(f);
12299   f = 0;
12300   if( x!=sz ){
12301     sqlite3_result_error(context, "edit() could not write the whole file", -1);
12302     goto edit_func_end;
12303   }
12304   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
12305   if( zCmd==0 ){
12306     sqlite3_result_error_nomem(context);
12307     goto edit_func_end;
12308   }
12309   rc = system(zCmd);
12310   sqlite3_free(zCmd);
12311   if( rc ){
12312     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
12313     goto edit_func_end;
12314   }
12315   f = fopen(zTempFile, "rb");
12316   if( f==0 ){
12317     sqlite3_result_error(context,
12318       "edit() cannot reopen temp file after edit", -1);
12319     goto edit_func_end;
12320   }
12321   fseek(f, 0, SEEK_END);
12322   sz = ftell(f);
12323   rewind(f);
12324   p = sqlite3_malloc64( sz+1 );
12325   if( p==0 ){
12326     sqlite3_result_error_nomem(context);
12327     goto edit_func_end;
12328   }
12329   x = fread(p, 1, (size_t)sz, f);
12330   fclose(f);
12331   f = 0;
12332   if( x!=sz ){
12333     sqlite3_result_error(context, "could not read back the whole file", -1);
12334     goto edit_func_end;
12335   }
12336   if( bBin ){
12337     sqlite3_result_blob64(context, p, sz, sqlite3_free);
12338   }else{
12339     sqlite3_int64 i, j;
12340     if( hasCRNL ){
12341       /* If the original contains \r\n then do no conversions back to \n */
12342       j = sz;
12343     }else{
12344       /* If the file did not originally contain \r\n then convert any new
12345       ** \r\n back into \n */
12346       for(i=j=0; i<sz; i++){
12347         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
12348         p[j++] = p[i];
12349       }
12350       sz = j;
12351       p[sz] = 0;
12352     } 
12353     sqlite3_result_text64(context, (const char*)p, sz,
12354                           sqlite3_free, SQLITE_UTF8);
12355   }
12356   p = 0;
12357
12358 edit_func_end:
12359   if( f ) fclose(f);
12360   unlink(zTempFile);
12361   sqlite3_free(zTempFile);
12362   sqlite3_free(p);
12363 }
12364 #endif /* SQLITE_NOHAVE_SYSTEM */
12365
12366 /*
12367 ** Save or restore the current output mode
12368 */
12369 static void outputModePush(ShellState *p){
12370   p->modePrior = p->mode;
12371   p->priorShFlgs = p->shellFlgs;
12372   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
12373   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
12374 }
12375 static void outputModePop(ShellState *p){
12376   p->mode = p->modePrior;
12377   p->shellFlgs = p->priorShFlgs;
12378   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
12379   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
12380 }
12381
12382 /*
12383 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12384 */
12385 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
12386   int i;
12387   char *zBlob = (char *)pBlob;
12388   raw_printf(out,"X'");
12389   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
12390   raw_printf(out,"'");
12391 }
12392
12393 /*
12394 ** Find a string that is not found anywhere in z[].  Return a pointer
12395 ** to that string.
12396 **
12397 ** Try to use zA and zB first.  If both of those are already found in z[]
12398 ** then make up some string and store it in the buffer zBuf.
12399 */
12400 static const char *unused_string(
12401   const char *z,                    /* Result must not appear anywhere in z */
12402   const char *zA, const char *zB,   /* Try these first */
12403   char *zBuf                        /* Space to store a generated string */
12404 ){
12405   unsigned i = 0;
12406   if( strstr(z, zA)==0 ) return zA;
12407   if( strstr(z, zB)==0 ) return zB;
12408   do{
12409     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
12410   }while( strstr(z,zBuf)!=0 );
12411   return zBuf;
12412 }
12413
12414 /*
12415 ** Output the given string as a quoted string using SQL quoting conventions.
12416 **
12417 ** See also: output_quoted_escaped_string()
12418 */
12419 static void output_quoted_string(FILE *out, const char *z){
12420   int i;
12421   char c;
12422   setBinaryMode(out, 1);
12423   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12424   if( c==0 ){
12425     utf8_printf(out,"'%s'",z);
12426   }else{
12427     raw_printf(out, "'");
12428     while( *z ){
12429       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12430       if( c=='\'' ) i++;
12431       if( i ){
12432         utf8_printf(out, "%.*s", i, z);
12433         z += i;
12434       }
12435       if( c=='\'' ){
12436         raw_printf(out, "'");
12437         continue;
12438       }
12439       if( c==0 ){
12440         break;
12441       }
12442       z++;
12443     }
12444     raw_printf(out, "'");
12445   }
12446   setTextMode(out, 1);
12447 }
12448
12449 /*
12450 ** Output the given string as a quoted string using SQL quoting conventions.
12451 ** Additionallly , escape the "\n" and "\r" characters so that they do not
12452 ** get corrupted by end-of-line translation facilities in some operating
12453 ** systems.
12454 **
12455 ** This is like output_quoted_string() but with the addition of the \r\n
12456 ** escape mechanism.
12457 */
12458 static void output_quoted_escaped_string(FILE *out, const char *z){
12459   int i;
12460   char c;
12461   setBinaryMode(out, 1);
12462   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
12463   if( c==0 ){
12464     utf8_printf(out,"'%s'",z);
12465   }else{
12466     const char *zNL = 0;
12467     const char *zCR = 0;
12468     int nNL = 0;
12469     int nCR = 0;
12470     char zBuf1[20], zBuf2[20];
12471     for(i=0; z[i]; i++){
12472       if( z[i]=='\n' ) nNL++;
12473       if( z[i]=='\r' ) nCR++;
12474     }
12475     if( nNL ){
12476       raw_printf(out, "replace(");
12477       zNL = unused_string(z, "\\n", "\\012", zBuf1);
12478     }
12479     if( nCR ){
12480       raw_printf(out, "replace(");
12481       zCR = unused_string(z, "\\r", "\\015", zBuf2);
12482     }
12483     raw_printf(out, "'");
12484     while( *z ){
12485       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
12486       if( c=='\'' ) i++;
12487       if( i ){
12488         utf8_printf(out, "%.*s", i, z);
12489         z += i;
12490       }
12491       if( c=='\'' ){
12492         raw_printf(out, "'");
12493         continue;
12494       }
12495       if( c==0 ){
12496         break;
12497       }
12498       z++;
12499       if( c=='\n' ){
12500         raw_printf(out, "%s", zNL);
12501         continue;
12502       }
12503       raw_printf(out, "%s", zCR);
12504     }
12505     raw_printf(out, "'");
12506     if( nCR ){
12507       raw_printf(out, ",'%s',char(13))", zCR);
12508     }
12509     if( nNL ){
12510       raw_printf(out, ",'%s',char(10))", zNL);
12511     }
12512   }
12513   setTextMode(out, 1);
12514 }
12515
12516 /*
12517 ** Output the given string as a quoted according to C or TCL quoting rules.
12518 */
12519 static void output_c_string(FILE *out, const char *z){
12520   unsigned int c;
12521   fputc('"', out);
12522   while( (c = *(z++))!=0 ){
12523     if( c=='\\' ){
12524       fputc(c, out);
12525       fputc(c, out);
12526     }else if( c=='"' ){
12527       fputc('\\', out);
12528       fputc('"', out);
12529     }else if( c=='\t' ){
12530       fputc('\\', out);
12531       fputc('t', out);
12532     }else if( c=='\n' ){
12533       fputc('\\', out);
12534       fputc('n', out);
12535     }else if( c=='\r' ){
12536       fputc('\\', out);
12537       fputc('r', out);
12538     }else if( !isprint(c&0xff) ){
12539       raw_printf(out, "\\%03o", c&0xff);
12540     }else{
12541       fputc(c, out);
12542     }
12543   }
12544   fputc('"', out);
12545 }
12546
12547 /*
12548 ** Output the given string as a quoted according to JSON quoting rules.
12549 */
12550 static void output_json_string(FILE *out, const char *z, int n){
12551   unsigned int c;
12552   if( n<0 ) n = (int)strlen(z);
12553   fputc('"', out);
12554   while( n-- ){
12555     c = *(z++);
12556     if( c=='\\' || c=='"' ){
12557       fputc('\\', out);
12558       fputc(c, out);
12559     }else if( c<=0x1f ){
12560       fputc('\\', out);
12561       if( c=='\b' ){
12562         fputc('b', out);
12563       }else if( c=='\f' ){
12564         fputc('f', out);
12565       }else if( c=='\n' ){
12566         fputc('n', out);
12567       }else if( c=='\r' ){
12568         fputc('r', out);
12569       }else if( c=='\t' ){
12570         fputc('t', out);
12571       }else{
12572          raw_printf(out, "u%04x",c);
12573       }
12574     }else{
12575       fputc(c, out);
12576     }
12577   }
12578   fputc('"', out);
12579 }
12580
12581 /*
12582 ** Output the given string with characters that are special to
12583 ** HTML escaped.
12584 */
12585 static void output_html_string(FILE *out, const char *z){
12586   int i;
12587   if( z==0 ) z = "";
12588   while( *z ){
12589     for(i=0;   z[i]
12590             && z[i]!='<'
12591             && z[i]!='&'
12592             && z[i]!='>'
12593             && z[i]!='\"'
12594             && z[i]!='\'';
12595         i++){}
12596     if( i>0 ){
12597       utf8_printf(out,"%.*s",i,z);
12598     }
12599     if( z[i]=='<' ){
12600       raw_printf(out,"&lt;");
12601     }else if( z[i]=='&' ){
12602       raw_printf(out,"&amp;");
12603     }else if( z[i]=='>' ){
12604       raw_printf(out,"&gt;");
12605     }else if( z[i]=='\"' ){
12606       raw_printf(out,"&quot;");
12607     }else if( z[i]=='\'' ){
12608       raw_printf(out,"&#39;");
12609     }else{
12610       break;
12611     }
12612     z += i + 1;
12613   }
12614 }
12615
12616 /*
12617 ** If a field contains any character identified by a 1 in the following
12618 ** array, then the string must be quoted for CSV.
12619 */
12620 static const char needCsvQuote[] = {
12621   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12622   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12623   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
12624   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12625   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12626   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12627   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12628   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
12629   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12630   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12631   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12632   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12633   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12634   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12635   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12636   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12637 };
12638
12639 /*
12640 ** Output a single term of CSV.  Actually, p->colSeparator is used for
12641 ** the separator, which may or may not be a comma.  p->nullValue is
12642 ** the null value.  Strings are quoted if necessary.  The separator
12643 ** is only issued if bSep is true.
12644 */
12645 static void output_csv(ShellState *p, const char *z, int bSep){
12646   FILE *out = p->out;
12647   if( z==0 ){
12648     utf8_printf(out,"%s",p->nullValue);
12649   }else{
12650     int i;
12651     int nSep = strlen30(p->colSeparator);
12652     for(i=0; z[i]; i++){
12653       if( needCsvQuote[((unsigned char*)z)[i]]
12654          || (z[i]==p->colSeparator[0] &&
12655              (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
12656         i = 0;
12657         break;
12658       }
12659     }
12660     if( i==0 ){
12661       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
12662       utf8_printf(out, "%s", zQuoted);
12663       sqlite3_free(zQuoted);
12664     }else{
12665       utf8_printf(out, "%s", z);
12666     }
12667   }
12668   if( bSep ){
12669     utf8_printf(p->out, "%s", p->colSeparator);
12670   }
12671 }
12672
12673 /*
12674 ** This routine runs when the user presses Ctrl-C
12675 */
12676 static void interrupt_handler(int NotUsed){
12677   UNUSED_PARAMETER(NotUsed);
12678   seenInterrupt++;
12679   if( seenInterrupt>2 ) exit(1);
12680   if( globalDb ) sqlite3_interrupt(globalDb);
12681 }
12682
12683 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12684 /*
12685 ** This routine runs for console events (e.g. Ctrl-C) on Win32
12686 */
12687 static BOOL WINAPI ConsoleCtrlHandler(
12688   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
12689 ){
12690   if( dwCtrlType==CTRL_C_EVENT ){
12691     interrupt_handler(0);
12692     return TRUE;
12693   }
12694   return FALSE;
12695 }
12696 #endif
12697
12698 #ifndef SQLITE_OMIT_AUTHORIZATION
12699 /*
12700 ** When the ".auth ON" is set, the following authorizer callback is
12701 ** invoked.  It always returns SQLITE_OK.
12702 */
12703 static int shellAuth(
12704   void *pClientData,
12705   int op,
12706   const char *zA1,
12707   const char *zA2,
12708   const char *zA3,
12709   const char *zA4
12710 ){
12711   ShellState *p = (ShellState*)pClientData;
12712   static const char *azAction[] = { 0,
12713      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
12714      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
12715      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
12716      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
12717      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
12718      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
12719      "PRAGMA",               "READ",                 "SELECT",
12720      "TRANSACTION",          "UPDATE",               "ATTACH",
12721      "DETACH",               "ALTER_TABLE",          "REINDEX",
12722      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
12723      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
12724   };
12725   int i;
12726   const char *az[4];
12727   az[0] = zA1;
12728   az[1] = zA2;
12729   az[2] = zA3;
12730   az[3] = zA4;
12731   utf8_printf(p->out, "authorizer: %s", azAction[op]);
12732   for(i=0; i<4; i++){
12733     raw_printf(p->out, " ");
12734     if( az[i] ){
12735       output_c_string(p->out, az[i]);
12736     }else{
12737       raw_printf(p->out, "NULL");
12738     }
12739   }
12740   raw_printf(p->out, "\n");
12741   return SQLITE_OK;
12742 }
12743 #endif
12744
12745 /*
12746 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
12747 **
12748 ** This routine converts some CREATE TABLE statements for shadow tables
12749 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
12750 */
12751 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
12752   if( z==0 ) return;
12753   if( zTail==0 ) return;
12754   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
12755     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
12756   }else{
12757     utf8_printf(out, "%s%s", z, zTail);
12758   }
12759 }
12760 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
12761   char c = z[n];
12762   z[n] = 0;
12763   printSchemaLine(out, z, zTail);
12764   z[n] = c;
12765 }
12766
12767 /*
12768 ** Return true if string z[] has nothing but whitespace and comments to the
12769 ** end of the first line.
12770 */
12771 static int wsToEol(const char *z){
12772   int i;
12773   for(i=0; z[i]; i++){
12774     if( z[i]=='\n' ) return 1;
12775     if( IsSpace(z[i]) ) continue;
12776     if( z[i]=='-' && z[i+1]=='-' ) return 1;
12777     return 0;
12778   }
12779   return 1;
12780 }
12781
12782 /*
12783 ** Add a new entry to the EXPLAIN QUERY PLAN data
12784 */
12785 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
12786   EQPGraphRow *pNew;
12787   int nText = strlen30(zText);
12788   if( p->autoEQPtest ){
12789     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
12790   }
12791   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
12792   if( pNew==0 ) shell_out_of_memory();
12793   pNew->iEqpId = iEqpId;
12794   pNew->iParentId = p2;
12795   memcpy(pNew->zText, zText, nText+1);
12796   pNew->pNext = 0;
12797   if( p->sGraph.pLast ){
12798     p->sGraph.pLast->pNext = pNew;
12799   }else{
12800     p->sGraph.pRow = pNew;
12801   }
12802   p->sGraph.pLast = pNew;
12803 }
12804
12805 /*
12806 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
12807 ** in p->sGraph.
12808 */
12809 static void eqp_reset(ShellState *p){
12810   EQPGraphRow *pRow, *pNext;
12811   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
12812     pNext = pRow->pNext;
12813     sqlite3_free(pRow);
12814   }
12815   memset(&p->sGraph, 0, sizeof(p->sGraph));
12816 }
12817
12818 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
12819 ** pOld, or return the first such line if pOld is NULL
12820 */
12821 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
12822   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
12823   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
12824   return pRow;
12825 }
12826
12827 /* Render a single level of the graph that has iEqpId as its parent.  Called
12828 ** recursively to render sublevels.
12829 */
12830 static void eqp_render_level(ShellState *p, int iEqpId){
12831   EQPGraphRow *pRow, *pNext;
12832   int n = strlen30(p->sGraph.zPrefix);
12833   char *z;
12834   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
12835     pNext = eqp_next_row(p, iEqpId, pRow);
12836     z = pRow->zText;
12837     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
12838                 pNext ? "|--" : "`--", z);
12839     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
12840       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
12841       eqp_render_level(p, pRow->iEqpId);
12842       p->sGraph.zPrefix[n] = 0;
12843     }
12844   }
12845 }
12846
12847 /*
12848 ** Display and reset the EXPLAIN QUERY PLAN data
12849 */
12850 static void eqp_render(ShellState *p){
12851   EQPGraphRow *pRow = p->sGraph.pRow;
12852   if( pRow ){
12853     if( pRow->zText[0]=='-' ){
12854       if( pRow->pNext==0 ){
12855         eqp_reset(p);
12856         return;
12857       }
12858       utf8_printf(p->out, "%s\n", pRow->zText+3);
12859       p->sGraph.pRow = pRow->pNext;
12860       sqlite3_free(pRow);
12861     }else{
12862       utf8_printf(p->out, "QUERY PLAN\n");
12863     }
12864     p->sGraph.zPrefix[0] = 0;
12865     eqp_render_level(p, 0);
12866     eqp_reset(p);
12867   }
12868 }
12869
12870 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12871 /*
12872 ** Progress handler callback.
12873 */
12874 static int progress_handler(void *pClientData) {
12875   ShellState *p = (ShellState*)pClientData;
12876   p->nProgress++;
12877   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
12878     raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
12879     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
12880     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
12881     return 1;
12882   }
12883   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
12884     raw_printf(p->out, "Progress %u\n", p->nProgress);
12885   }
12886   return 0;
12887 }
12888 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
12889
12890 /*
12891 ** Print N dashes
12892 */
12893 static void print_dashes(FILE *out, int N){
12894   const char zDash[] = "--------------------------------------------------";
12895   const int nDash = sizeof(zDash) - 1;
12896   while( N>nDash ){
12897     fputs(zDash, out);
12898     N -= nDash;
12899   }
12900   raw_printf(out, "%.*s", N, zDash);
12901 }
12902
12903 /*
12904 ** Print a markdown or table-style row separator using ascii-art
12905 */
12906 static void print_row_separator(
12907   ShellState *p,
12908   int nArg,
12909   const char *zSep
12910 ){
12911   int i;
12912   if( nArg>0 ){
12913     fputs(zSep, p->out);
12914     print_dashes(p->out, p->actualWidth[0]+2);
12915     for(i=1; i<nArg; i++){
12916       fputs(zSep, p->out);
12917       print_dashes(p->out, p->actualWidth[i]+2);
12918     }
12919     fputs(zSep, p->out);
12920   }
12921   fputs("\n", p->out);
12922 }
12923
12924 /*
12925 ** This is the callback routine that the shell
12926 ** invokes for each row of a query result.
12927 */
12928 static int shell_callback(
12929   void *pArg,
12930   int nArg,        /* Number of result columns */
12931   char **azArg,    /* Text of each result column */
12932   char **azCol,    /* Column names */
12933   int *aiType      /* Column types.  Might be NULL */
12934 ){
12935   int i;
12936   ShellState *p = (ShellState*)pArg;
12937
12938   if( azArg==0 ) return 0;
12939   switch( p->cMode ){
12940     case MODE_Line: {
12941       int w = 5;
12942       if( azArg==0 ) break;
12943       for(i=0; i<nArg; i++){
12944         int len = strlen30(azCol[i] ? azCol[i] : "");
12945         if( len>w ) w = len;
12946       }
12947       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
12948       for(i=0; i<nArg; i++){
12949         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
12950                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
12951       }
12952       break;
12953     }
12954     case MODE_Explain: {
12955       static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
12956       if( nArg>ArraySize(aExplainWidth) ){
12957         nArg = ArraySize(aExplainWidth);
12958       }
12959       if( p->cnt++==0 ){
12960         for(i=0; i<nArg; i++){
12961           int w = aExplainWidth[i];
12962           utf8_width_print(p->out, w, azCol[i]);
12963           fputs(i==nArg-1 ? "\n" : "  ", p->out);
12964         }
12965         for(i=0; i<nArg; i++){
12966           int w = aExplainWidth[i];
12967           print_dashes(p->out, w);
12968           fputs(i==nArg-1 ? "\n" : "  ", p->out);
12969         }
12970       }
12971       if( azArg==0 ) break;
12972       for(i=0; i<nArg; i++){
12973         int w = aExplainWidth[i];
12974         if( i==nArg-1 ) w = 0;
12975         if( azArg[i] && strlenChar(azArg[i])>w ){
12976           w = strlenChar(azArg[i]);
12977         }
12978         if( i==1 && p->aiIndent && p->pStmt ){
12979           if( p->iIndent<p->nIndent ){
12980             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
12981           }
12982           p->iIndent++;
12983         }
12984         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
12985         fputs(i==nArg-1 ? "\n" : "  ", p->out);
12986       }
12987       break;
12988     }
12989     case MODE_Semi: {   /* .schema and .fullschema output */
12990       printSchemaLine(p->out, azArg[0], ";\n");
12991       break;
12992     }
12993     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
12994       char *z;
12995       int j;
12996       int nParen = 0;
12997       char cEnd = 0;
12998       char c;
12999       int nLine = 0;
13000       assert( nArg==1 );
13001       if( azArg[0]==0 ) break;
13002       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
13003        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
13004       ){
13005         utf8_printf(p->out, "%s;\n", azArg[0]);
13006         break;
13007       }
13008       z = sqlite3_mprintf("%s", azArg[0]);
13009       j = 0;
13010       for(i=0; IsSpace(z[i]); i++){}
13011       for(; (c = z[i])!=0; i++){
13012         if( IsSpace(c) ){
13013           if( z[j-1]=='\r' ) z[j-1] = '\n';
13014           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
13015         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
13016           j--;
13017         }
13018         z[j++] = c;
13019       }
13020       while( j>0 && IsSpace(z[j-1]) ){ j--; }
13021       z[j] = 0;
13022       if( strlen30(z)>=79 ){
13023         for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
13024           if( c==cEnd ){
13025             cEnd = 0;
13026           }else if( c=='"' || c=='\'' || c=='`' ){
13027             cEnd = c;
13028           }else if( c=='[' ){
13029             cEnd = ']';
13030           }else if( c=='-' && z[i+1]=='-' ){
13031             cEnd = '\n';
13032           }else if( c=='(' ){
13033             nParen++;
13034           }else if( c==')' ){
13035             nParen--;
13036             if( nLine>0 && nParen==0 && j>0 ){
13037               printSchemaLineN(p->out, z, j, "\n");
13038               j = 0;
13039             }
13040           }
13041           z[j++] = c;
13042           if( nParen==1 && cEnd==0
13043            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
13044           ){
13045             if( c=='\n' ) j--;
13046             printSchemaLineN(p->out, z, j, "\n  ");
13047             j = 0;
13048             nLine++;
13049             while( IsSpace(z[i+1]) ){ i++; }
13050           }
13051         }
13052         z[j] = 0;
13053       }
13054       printSchemaLine(p->out, z, ";\n");
13055       sqlite3_free(z);
13056       break;
13057     }
13058     case MODE_List: {
13059       if( p->cnt++==0 && p->showHeader ){
13060         for(i=0; i<nArg; i++){
13061           utf8_printf(p->out,"%s%s",azCol[i],
13062                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
13063         }
13064       }
13065       if( azArg==0 ) break;
13066       for(i=0; i<nArg; i++){
13067         char *z = azArg[i];
13068         if( z==0 ) z = p->nullValue;
13069         utf8_printf(p->out, "%s", z);
13070         if( i<nArg-1 ){
13071           utf8_printf(p->out, "%s", p->colSeparator);
13072         }else{
13073           utf8_printf(p->out, "%s", p->rowSeparator);
13074         }
13075       }
13076       break;
13077     }
13078     case MODE_Html: {
13079       if( p->cnt++==0 && p->showHeader ){
13080         raw_printf(p->out,"<TR>");
13081         for(i=0; i<nArg; i++){
13082           raw_printf(p->out,"<TH>");
13083           output_html_string(p->out, azCol[i]);
13084           raw_printf(p->out,"</TH>\n");
13085         }
13086         raw_printf(p->out,"</TR>\n");
13087       }
13088       if( azArg==0 ) break;
13089       raw_printf(p->out,"<TR>");
13090       for(i=0; i<nArg; i++){
13091         raw_printf(p->out,"<TD>");
13092         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13093         raw_printf(p->out,"</TD>\n");
13094       }
13095       raw_printf(p->out,"</TR>\n");
13096       break;
13097     }
13098     case MODE_Tcl: {
13099       if( p->cnt++==0 && p->showHeader ){
13100         for(i=0; i<nArg; i++){
13101           output_c_string(p->out,azCol[i] ? azCol[i] : "");
13102           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13103         }
13104         utf8_printf(p->out, "%s", p->rowSeparator);
13105       }
13106       if( azArg==0 ) break;
13107       for(i=0; i<nArg; i++){
13108         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13109         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13110       }
13111       utf8_printf(p->out, "%s", p->rowSeparator);
13112       break;
13113     }
13114     case MODE_Csv: {
13115       setBinaryMode(p->out, 1);
13116       if( p->cnt++==0 && p->showHeader ){
13117         for(i=0; i<nArg; i++){
13118           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
13119         }
13120         utf8_printf(p->out, "%s", p->rowSeparator);
13121       }
13122       if( nArg>0 ){
13123         for(i=0; i<nArg; i++){
13124           output_csv(p, azArg[i], i<nArg-1);
13125         }
13126         utf8_printf(p->out, "%s", p->rowSeparator);
13127       }
13128       setTextMode(p->out, 1);
13129       break;
13130     }
13131     case MODE_Insert: {
13132       if( azArg==0 ) break;
13133       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
13134       if( p->showHeader ){
13135         raw_printf(p->out,"(");
13136         for(i=0; i<nArg; i++){
13137           if( i>0 ) raw_printf(p->out, ",");
13138           if( quoteChar(azCol[i]) ){
13139             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
13140             utf8_printf(p->out, "%s", z);
13141             sqlite3_free(z);
13142           }else{
13143             raw_printf(p->out, "%s", azCol[i]);
13144           }
13145         }
13146         raw_printf(p->out,")");
13147       }
13148       p->cnt++;
13149       for(i=0; i<nArg; i++){
13150         raw_printf(p->out, i>0 ? "," : " VALUES(");
13151         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13152           utf8_printf(p->out,"NULL");
13153         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13154           if( ShellHasFlag(p, SHFLG_Newlines) ){
13155             output_quoted_string(p->out, azArg[i]);
13156           }else{
13157             output_quoted_escaped_string(p->out, azArg[i]);
13158           }
13159         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13160           utf8_printf(p->out,"%s", azArg[i]);
13161         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13162           char z[50];
13163           double r = sqlite3_column_double(p->pStmt, i);
13164           sqlite3_uint64 ur;
13165           memcpy(&ur,&r,sizeof(r));
13166           if( ur==0x7ff0000000000000LL ){
13167             raw_printf(p->out, "1e999");
13168           }else if( ur==0xfff0000000000000LL ){
13169             raw_printf(p->out, "-1e999");
13170           }else{
13171             sqlite3_snprintf(50,z,"%!.20g", r);
13172             raw_printf(p->out, "%s", z);
13173           }
13174         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13175           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13176           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13177           output_hex_blob(p->out, pBlob, nBlob);
13178         }else if( isNumber(azArg[i], 0) ){
13179           utf8_printf(p->out,"%s", azArg[i]);
13180         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
13181           output_quoted_string(p->out, azArg[i]);
13182         }else{
13183           output_quoted_escaped_string(p->out, azArg[i]);
13184         }
13185       }
13186       raw_printf(p->out,");\n");
13187       break;
13188     }
13189     case MODE_Json: {
13190       if( azArg==0 ) break;
13191       if( p->cnt==0 ){
13192         fputs("[{", p->out);
13193       }else{
13194         fputs(",\n{", p->out);
13195       }
13196       p->cnt++;
13197       for(i=0; i<nArg; i++){
13198         output_json_string(p->out, azCol[i], -1);
13199         putc(':', p->out);
13200         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13201           fputs("null",p->out);
13202         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13203           char z[50];
13204           double r = sqlite3_column_double(p->pStmt, i);
13205           sqlite3_uint64 ur;
13206           memcpy(&ur,&r,sizeof(r));
13207           if( ur==0x7ff0000000000000LL ){
13208             raw_printf(p->out, "1e999");
13209           }else if( ur==0xfff0000000000000LL ){
13210             raw_printf(p->out, "-1e999");
13211           }else{
13212             sqlite3_snprintf(50,z,"%!.20g", r);
13213             raw_printf(p->out, "%s", z);
13214           }
13215         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13216           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13217           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13218           output_json_string(p->out, pBlob, nBlob);
13219         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13220           output_json_string(p->out, azArg[i], -1);
13221         }else{
13222           utf8_printf(p->out,"%s", azArg[i]);
13223         }
13224         if( i<nArg-1 ){
13225           putc(',', p->out);
13226         }
13227       }
13228       putc('}', p->out);
13229       break;
13230     }
13231     case MODE_Quote: {
13232       if( azArg==0 ) break;
13233       if( p->cnt==0 && p->showHeader ){
13234         for(i=0; i<nArg; i++){
13235           if( i>0 ) fputs(p->colSeparator, p->out);
13236           output_quoted_string(p->out, azCol[i]);
13237         }
13238         fputs(p->rowSeparator, p->out);
13239       }
13240       p->cnt++;
13241       for(i=0; i<nArg; i++){
13242         if( i>0 ) fputs(p->colSeparator, p->out);
13243         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13244           utf8_printf(p->out,"NULL");
13245         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13246           output_quoted_string(p->out, azArg[i]);
13247         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13248           utf8_printf(p->out,"%s", azArg[i]);
13249         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13250           char z[50];
13251           double r = sqlite3_column_double(p->pStmt, i);
13252           sqlite3_snprintf(50,z,"%!.20g", r);
13253           raw_printf(p->out, "%s", z);
13254         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13255           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13256           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13257           output_hex_blob(p->out, pBlob, nBlob);
13258         }else if( isNumber(azArg[i], 0) ){
13259           utf8_printf(p->out,"%s", azArg[i]);
13260         }else{
13261           output_quoted_string(p->out, azArg[i]);
13262         }
13263       }
13264       fputs(p->rowSeparator, p->out);
13265       break;
13266     }
13267     case MODE_Ascii: {
13268       if( p->cnt++==0 && p->showHeader ){
13269         for(i=0; i<nArg; i++){
13270           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13271           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
13272         }
13273         utf8_printf(p->out, "%s", p->rowSeparator);
13274       }
13275       if( azArg==0 ) break;
13276       for(i=0; i<nArg; i++){
13277         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13278         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
13279       }
13280       utf8_printf(p->out, "%s", p->rowSeparator);
13281       break;
13282     }
13283     case MODE_EQP: {
13284       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
13285       break;
13286     }
13287   }
13288   return 0;
13289 }
13290
13291 /*
13292 ** This is the callback routine that the SQLite library
13293 ** invokes for each row of a query result.
13294 */
13295 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
13296   /* since we don't have type info, call the shell_callback with a NULL value */
13297   return shell_callback(pArg, nArg, azArg, azCol, NULL);
13298 }
13299
13300 /*
13301 ** This is the callback routine from sqlite3_exec() that appends all
13302 ** output onto the end of a ShellText object.
13303 */
13304 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
13305   ShellText *p = (ShellText*)pArg;
13306   int i;
13307   UNUSED_PARAMETER(az);
13308   if( azArg==0 ) return 0;
13309   if( p->n ) appendText(p, "|", 0);
13310   for(i=0; i<nArg; i++){
13311     if( i ) appendText(p, ",", 0);
13312     if( azArg[i] ) appendText(p, azArg[i], 0);
13313   }
13314   return 0;
13315 }
13316
13317 /*
13318 ** Generate an appropriate SELFTEST table in the main database.
13319 */
13320 static void createSelftestTable(ShellState *p){
13321   char *zErrMsg = 0;
13322   sqlite3_exec(p->db,
13323     "SAVEPOINT selftest_init;\n"
13324     "CREATE TABLE IF NOT EXISTS selftest(\n"
13325     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
13326     "  op TEXT,\n"                   /* Operator:  memo run */
13327     "  cmd TEXT,\n"                  /* Command text */
13328     "  ans TEXT\n"                   /* Desired answer */
13329     ");"
13330     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
13331     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
13332     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
13333     "         'memo','Tests generated by --init');\n"
13334     "INSERT INTO [_shell$self]\n"
13335     "  SELECT 'run',\n"
13336     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
13337                                  "FROM sqlite_schema ORDER BY 2'',224))',\n"
13338     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
13339                           "FROM sqlite_schema ORDER BY 2',224));\n"
13340     "INSERT INTO [_shell$self]\n"
13341     "  SELECT 'run',"
13342     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
13343     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
13344     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
13345     "  FROM (\n"
13346     "    SELECT name FROM sqlite_schema\n"
13347     "     WHERE type='table'\n"
13348     "       AND name<>'selftest'\n"
13349     "       AND coalesce(rootpage,0)>0\n"
13350     "  )\n"
13351     " ORDER BY name;\n"
13352     "INSERT INTO [_shell$self]\n"
13353     "  VALUES('run','PRAGMA integrity_check','ok');\n"
13354     "INSERT INTO selftest(tno,op,cmd,ans)"
13355     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
13356     "DROP TABLE [_shell$self];"
13357     ,0,0,&zErrMsg);
13358   if( zErrMsg ){
13359     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
13360     sqlite3_free(zErrMsg);
13361   }
13362   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
13363 }
13364
13365
13366 /*
13367 ** Set the destination table field of the ShellState structure to
13368 ** the name of the table given.  Escape any quote characters in the
13369 ** table name.
13370 */
13371 static void set_table_name(ShellState *p, const char *zName){
13372   int i, n;
13373   char cQuote;
13374   char *z;
13375
13376   if( p->zDestTable ){
13377     free(p->zDestTable);
13378     p->zDestTable = 0;
13379   }
13380   if( zName==0 ) return;
13381   cQuote = quoteChar(zName);
13382   n = strlen30(zName);
13383   if( cQuote ) n += n+2;
13384   z = p->zDestTable = malloc( n+1 );
13385   if( z==0 ) shell_out_of_memory();
13386   n = 0;
13387   if( cQuote ) z[n++] = cQuote;
13388   for(i=0; zName[i]; i++){
13389     z[n++] = zName[i];
13390     if( zName[i]==cQuote ) z[n++] = cQuote;
13391   }
13392   if( cQuote ) z[n++] = cQuote;
13393   z[n] = 0;
13394 }
13395
13396
13397 /*
13398 ** Execute a query statement that will generate SQL output.  Print
13399 ** the result columns, comma-separated, on a line and then add a
13400 ** semicolon terminator to the end of that line.
13401 **
13402 ** If the number of columns is 1 and that column contains text "--"
13403 ** then write the semicolon on a separate line.  That way, if a
13404 ** "--" comment occurs at the end of the statement, the comment
13405 ** won't consume the semicolon terminator.
13406 */
13407 static int run_table_dump_query(
13408   ShellState *p,           /* Query context */
13409   const char *zSelect      /* SELECT statement to extract content */
13410 ){
13411   sqlite3_stmt *pSelect;
13412   int rc;
13413   int nResult;
13414   int i;
13415   const char *z;
13416   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
13417   if( rc!=SQLITE_OK || !pSelect ){
13418     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13419                 sqlite3_errmsg(p->db));
13420     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13421     return rc;
13422   }
13423   rc = sqlite3_step(pSelect);
13424   nResult = sqlite3_column_count(pSelect);
13425   while( rc==SQLITE_ROW ){
13426     z = (const char*)sqlite3_column_text(pSelect, 0);
13427     utf8_printf(p->out, "%s", z);
13428     for(i=1; i<nResult; i++){
13429       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
13430     }
13431     if( z==0 ) z = "";
13432     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
13433     if( z[0] ){
13434       raw_printf(p->out, "\n;\n");
13435     }else{
13436       raw_printf(p->out, ";\n");
13437     }
13438     rc = sqlite3_step(pSelect);
13439   }
13440   rc = sqlite3_finalize(pSelect);
13441   if( rc!=SQLITE_OK ){
13442     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13443                 sqlite3_errmsg(p->db));
13444     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13445   }
13446   return rc;
13447 }
13448
13449 /*
13450 ** Allocate space and save off current error string.
13451 */
13452 static char *save_err_msg(
13453   sqlite3 *db            /* Database to query */
13454 ){
13455   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
13456   char *zErrMsg = sqlite3_malloc64(nErrMsg);
13457   if( zErrMsg ){
13458     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
13459   }
13460   return zErrMsg;
13461 }
13462
13463 #ifdef __linux__
13464 /*
13465 ** Attempt to display I/O stats on Linux using /proc/PID/io
13466 */
13467 static void displayLinuxIoStats(FILE *out){
13468   FILE *in;
13469   char z[200];
13470   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
13471   in = fopen(z, "rb");
13472   if( in==0 ) return;
13473   while( fgets(z, sizeof(z), in)!=0 ){
13474     static const struct {
13475       const char *zPattern;
13476       const char *zDesc;
13477     } aTrans[] = {
13478       { "rchar: ",                  "Bytes received by read():" },
13479       { "wchar: ",                  "Bytes sent to write():"    },
13480       { "syscr: ",                  "Read() system calls:"      },
13481       { "syscw: ",                  "Write() system calls:"     },
13482       { "read_bytes: ",             "Bytes read from storage:"  },
13483       { "write_bytes: ",            "Bytes written to storage:" },
13484       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
13485     };
13486     int i;
13487     for(i=0; i<ArraySize(aTrans); i++){
13488       int n = strlen30(aTrans[i].zPattern);
13489       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
13490         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
13491         break;
13492       }
13493     }
13494   }
13495   fclose(in);
13496 }
13497 #endif
13498
13499 /*
13500 ** Display a single line of status using 64-bit values.
13501 */
13502 static void displayStatLine(
13503   ShellState *p,            /* The shell context */
13504   char *zLabel,             /* Label for this one line */
13505   char *zFormat,            /* Format for the result */
13506   int iStatusCtrl,          /* Which status to display */
13507   int bReset                /* True to reset the stats */
13508 ){
13509   sqlite3_int64 iCur = -1;
13510   sqlite3_int64 iHiwtr = -1;
13511   int i, nPercent;
13512   char zLine[200];
13513   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
13514   for(i=0, nPercent=0; zFormat[i]; i++){
13515     if( zFormat[i]=='%' ) nPercent++;
13516   }
13517   if( nPercent>1 ){
13518     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
13519   }else{
13520     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
13521   }
13522   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
13523 }
13524
13525 /*
13526 ** Display memory stats.
13527 */
13528 static int display_stats(
13529   sqlite3 *db,                /* Database to query */
13530   ShellState *pArg,           /* Pointer to ShellState */
13531   int bReset                  /* True to reset the stats */
13532 ){
13533   int iCur;
13534   int iHiwtr;
13535   FILE *out;
13536   if( pArg==0 || pArg->out==0 ) return 0;
13537   out = pArg->out;
13538
13539   if( pArg->pStmt && pArg->statsOn==2 ){
13540     int nCol, i, x;
13541     sqlite3_stmt *pStmt = pArg->pStmt;
13542     char z[100];
13543     nCol = sqlite3_column_count(pStmt);
13544     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
13545     for(i=0; i<nCol; i++){
13546       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
13547       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
13548 #ifndef SQLITE_OMIT_DECLTYPE
13549       sqlite3_snprintf(30, z+x, "declared type:");
13550       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
13551 #endif
13552 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13553       sqlite3_snprintf(30, z+x, "database name:");
13554       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
13555       sqlite3_snprintf(30, z+x, "table name:");
13556       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
13557       sqlite3_snprintf(30, z+x, "origin name:");
13558       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
13559 #endif
13560     }
13561   }
13562
13563   if( pArg->statsOn==3 ){
13564     if( pArg->pStmt ){
13565       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13566       raw_printf(pArg->out, "VM-steps: %d\n", iCur);
13567     }
13568     return 0;
13569   }
13570
13571   displayStatLine(pArg, "Memory Used:",
13572      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
13573   displayStatLine(pArg, "Number of Outstanding Allocations:",
13574      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
13575   if( pArg->shellFlgs & SHFLG_Pagecache ){
13576     displayStatLine(pArg, "Number of Pcache Pages Used:",
13577        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
13578   }
13579   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
13580      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
13581   displayStatLine(pArg, "Largest Allocation:",
13582      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
13583   displayStatLine(pArg, "Largest Pcache Allocation:",
13584      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
13585 #ifdef YYTRACKMAXSTACKDEPTH
13586   displayStatLine(pArg, "Deepest Parser Stack:",
13587      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
13588 #endif
13589
13590   if( db ){
13591     if( pArg->shellFlgs & SHFLG_Lookaside ){
13592       iHiwtr = iCur = -1;
13593       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
13594                         &iCur, &iHiwtr, bReset);
13595       raw_printf(pArg->out,
13596               "Lookaside Slots Used:                %d (max %d)\n",
13597               iCur, iHiwtr);
13598       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
13599                         &iCur, &iHiwtr, bReset);
13600       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
13601               iHiwtr);
13602       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
13603                         &iCur, &iHiwtr, bReset);
13604       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
13605               iHiwtr);
13606       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
13607                         &iCur, &iHiwtr, bReset);
13608       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
13609               iHiwtr);
13610     }
13611     iHiwtr = iCur = -1;
13612     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
13613     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
13614             iCur);
13615     iHiwtr = iCur = -1;
13616     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
13617     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
13618     iHiwtr = iCur = -1;
13619     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
13620     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
13621     iHiwtr = iCur = -1;
13622     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
13623     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
13624     iHiwtr = iCur = -1;
13625     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
13626     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
13627     iHiwtr = iCur = -1;
13628     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
13629     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
13630             iCur);
13631     iHiwtr = iCur = -1;
13632     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
13633     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
13634             iCur);
13635   }
13636
13637   if( pArg->pStmt ){
13638     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
13639                                bReset);
13640     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
13641     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
13642     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
13643     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
13644     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
13645     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13646     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
13647     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
13648     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
13649     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
13650     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
13651     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
13652     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
13653   }
13654
13655 #ifdef __linux__
13656   displayLinuxIoStats(pArg->out);
13657 #endif
13658
13659   /* Do not remove this machine readable comment: extra-stats-output-here */
13660
13661   return 0;
13662 }
13663
13664 /*
13665 ** Display scan stats.
13666 */
13667 static void display_scanstats(
13668   sqlite3 *db,                    /* Database to query */
13669   ShellState *pArg                /* Pointer to ShellState */
13670 ){
13671 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13672   UNUSED_PARAMETER(db);
13673   UNUSED_PARAMETER(pArg);
13674 #else
13675   int i, k, n, mx;
13676   raw_printf(pArg->out, "-------- scanstats --------\n");
13677   mx = 0;
13678   for(k=0; k<=mx; k++){
13679     double rEstLoop = 1.0;
13680     for(i=n=0; 1; i++){
13681       sqlite3_stmt *p = pArg->pStmt;
13682       sqlite3_int64 nLoop, nVisit;
13683       double rEst;
13684       int iSid;
13685       const char *zExplain;
13686       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
13687         break;
13688       }
13689       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
13690       if( iSid>mx ) mx = iSid;
13691       if( iSid!=k ) continue;
13692       if( n==0 ){
13693         rEstLoop = (double)nLoop;
13694         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
13695       }
13696       n++;
13697       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
13698       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
13699       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
13700       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
13701       rEstLoop *= rEst;
13702       raw_printf(pArg->out,
13703           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
13704           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
13705       );
13706     }
13707   }
13708   raw_printf(pArg->out, "---------------------------\n");
13709 #endif
13710 }
13711
13712 /*
13713 ** Parameter azArray points to a zero-terminated array of strings. zStr
13714 ** points to a single nul-terminated string. Return non-zero if zStr
13715 ** is equal, according to strcmp(), to any of the strings in the array.
13716 ** Otherwise, return zero.
13717 */
13718 static int str_in_array(const char *zStr, const char **azArray){
13719   int i;
13720   for(i=0; azArray[i]; i++){
13721     if( 0==strcmp(zStr, azArray[i]) ) return 1;
13722   }
13723   return 0;
13724 }
13725
13726 /*
13727 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
13728 ** and populate the ShellState.aiIndent[] array with the number of
13729 ** spaces each opcode should be indented before it is output.
13730 **
13731 ** The indenting rules are:
13732 **
13733 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
13734 **       all opcodes that occur between the p2 jump destination and the opcode
13735 **       itself by 2 spaces.
13736 **
13737 **     * For each "Goto", if the jump destination is earlier in the program
13738 **       and ends on one of:
13739 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
13740 **       or if the P1 parameter is one instead of zero,
13741 **       then indent all opcodes between the earlier instruction
13742 **       and "Goto" by 2 spaces.
13743 */
13744 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
13745   const char *zSql;               /* The text of the SQL statement */
13746   const char *z;                  /* Used to check if this is an EXPLAIN */
13747   int *abYield = 0;               /* True if op is an OP_Yield */
13748   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
13749   int iOp;                        /* Index of operation in p->aiIndent[] */
13750
13751   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
13752   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
13753                             "Rewind", 0 };
13754   const char *azGoto[] = { "Goto", 0 };
13755
13756   /* Try to figure out if this is really an EXPLAIN statement. If this
13757   ** cannot be verified, return early.  */
13758   if( sqlite3_column_count(pSql)!=8 ){
13759     p->cMode = p->mode;
13760     return;
13761   }
13762   zSql = sqlite3_sql(pSql);
13763   if( zSql==0 ) return;
13764   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
13765   if( sqlite3_strnicmp(z, "explain", 7) ){
13766     p->cMode = p->mode;
13767     return;
13768   }
13769
13770   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
13771     int i;
13772     int iAddr = sqlite3_column_int(pSql, 0);
13773     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
13774
13775     /* Set p2 to the P2 field of the current opcode. Then, assuming that
13776     ** p2 is an instruction address, set variable p2op to the index of that
13777     ** instruction in the aiIndent[] array. p2 and p2op may be different if
13778     ** the current instruction is part of a sub-program generated by an
13779     ** SQL trigger or foreign key.  */
13780     int p2 = sqlite3_column_int(pSql, 3);
13781     int p2op = (p2 + (iOp-iAddr));
13782
13783     /* Grow the p->aiIndent array as required */
13784     if( iOp>=nAlloc ){
13785       if( iOp==0 ){
13786         /* Do further verfication that this is explain output.  Abort if
13787         ** it is not */
13788         static const char *explainCols[] = {
13789            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
13790         int jj;
13791         for(jj=0; jj<ArraySize(explainCols); jj++){
13792           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
13793             p->cMode = p->mode;
13794             sqlite3_reset(pSql);
13795             return;
13796           }
13797         }
13798       }
13799       nAlloc += 100;
13800       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
13801       if( p->aiIndent==0 ) shell_out_of_memory();
13802       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
13803       if( abYield==0 ) shell_out_of_memory();
13804     }
13805     abYield[iOp] = str_in_array(zOp, azYield);
13806     p->aiIndent[iOp] = 0;
13807     p->nIndent = iOp+1;
13808
13809     if( str_in_array(zOp, azNext) ){
13810       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
13811     }
13812     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
13813      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
13814     ){
13815       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
13816     }
13817   }
13818
13819   p->iIndent = 0;
13820   sqlite3_free(abYield);
13821   sqlite3_reset(pSql);
13822 }
13823
13824 /*
13825 ** Free the array allocated by explain_data_prepare().
13826 */
13827 static void explain_data_delete(ShellState *p){
13828   sqlite3_free(p->aiIndent);
13829   p->aiIndent = 0;
13830   p->nIndent = 0;
13831   p->iIndent = 0;
13832 }
13833
13834 /*
13835 ** Disable and restore .wheretrace and .selecttrace settings.
13836 */
13837 static unsigned int savedSelectTrace;
13838 static unsigned int savedWhereTrace;
13839 static void disable_debug_trace_modes(void){
13840   unsigned int zero = 0;
13841   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
13842   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
13843   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
13844   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
13845 }
13846 static void restore_debug_trace_modes(void){
13847   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
13848   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
13849 }
13850
13851 /* Create the TEMP table used to store parameter bindings */
13852 static void bind_table_init(ShellState *p){
13853   int wrSchema = 0;
13854   int defensiveMode = 0;
13855   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
13856   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
13857   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
13858   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
13859   sqlite3_exec(p->db,
13860     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
13861     "  key TEXT PRIMARY KEY,\n"
13862     "  value\n"
13863     ") WITHOUT ROWID;",
13864     0, 0, 0);
13865   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
13866   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
13867 }
13868
13869 /*
13870 ** Bind parameters on a prepared statement.
13871 **
13872 ** Parameter bindings are taken from a TEMP table of the form:
13873 **
13874 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
13875 **    WITHOUT ROWID;
13876 **
13877 ** No bindings occur if this table does not exist.  The name of the table
13878 ** begins with "sqlite_" so that it will not collide with ordinary application
13879 ** tables.  The table must be in the TEMP schema.
13880 */
13881 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
13882   int nVar;
13883   int i;
13884   int rc;
13885   sqlite3_stmt *pQ = 0;
13886
13887   nVar = sqlite3_bind_parameter_count(pStmt);
13888   if( nVar==0 ) return;  /* Nothing to do */
13889   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
13890                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
13891     return; /* Parameter table does not exist */
13892   }
13893   rc = sqlite3_prepare_v2(pArg->db,
13894           "SELECT value FROM temp.sqlite_parameters"
13895           " WHERE key=?1", -1, &pQ, 0);
13896   if( rc || pQ==0 ) return;
13897   for(i=1; i<=nVar; i++){
13898     char zNum[30];
13899     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
13900     if( zVar==0 ){
13901       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
13902       zVar = zNum;
13903     }
13904     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
13905     if( sqlite3_step(pQ)==SQLITE_ROW ){
13906       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
13907     }else{
13908       sqlite3_bind_null(pStmt, i);
13909     }
13910     sqlite3_reset(pQ);
13911   }
13912   sqlite3_finalize(pQ);
13913 }
13914
13915 /*
13916 ** UTF8 box-drawing characters.  Imagine box lines like this:
13917 **
13918 **           1
13919 **           |
13920 **       4 --+-- 2
13921 **           |
13922 **           3
13923 **
13924 ** Each box characters has between 2 and 4 of the lines leading from
13925 ** the center.  The characters are here identified by the numbers of
13926 ** their corresponding lines.
13927 */
13928 #define BOX_24   "\342\224\200"  /* U+2500 --- */
13929 #define BOX_13   "\342\224\202"  /* U+2502  |  */
13930 #define BOX_23   "\342\224\214"  /* U+250c  ,- */
13931 #define BOX_34   "\342\224\220"  /* U+2510 -,  */
13932 #define BOX_12   "\342\224\224"  /* U+2514  '- */
13933 #define BOX_14   "\342\224\230"  /* U+2518 -'  */
13934 #define BOX_123  "\342\224\234"  /* U+251c  |- */
13935 #define BOX_134  "\342\224\244"  /* U+2524 -|  */
13936 #define BOX_234  "\342\224\254"  /* U+252c -,- */
13937 #define BOX_124  "\342\224\264"  /* U+2534 -'- */
13938 #define BOX_1234 "\342\224\274"  /* U+253c -|- */
13939
13940 /* Draw horizontal line N characters long using unicode box
13941 ** characters
13942 */
13943 static void print_box_line(FILE *out, int N){
13944   const char zDash[] = 
13945       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
13946       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
13947   const int nDash = sizeof(zDash) - 1;
13948   N *= 3;
13949   while( N>nDash ){
13950     utf8_printf(out, zDash);
13951     N -= nDash;
13952   }
13953   utf8_printf(out, "%.*s", N, zDash);
13954 }
13955
13956 /*
13957 ** Draw a horizontal separator for a MODE_Box table.
13958 */
13959 static void print_box_row_separator(
13960   ShellState *p,
13961   int nArg,
13962   const char *zSep1,
13963   const char *zSep2,
13964   const char *zSep3
13965 ){
13966   int i;
13967   if( nArg>0 ){
13968     utf8_printf(p->out, "%s", zSep1);
13969     print_box_line(p->out, p->actualWidth[0]+2);
13970     for(i=1; i<nArg; i++){
13971       utf8_printf(p->out, "%s", zSep2);
13972       print_box_line(p->out, p->actualWidth[i]+2);
13973     }
13974     utf8_printf(p->out, "%s", zSep3);
13975   }
13976   fputs("\n", p->out);
13977 }
13978
13979
13980
13981 /*
13982 ** Run a prepared statement and output the result in one of the
13983 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
13984 ** or MODE_Box.
13985 **
13986 ** This is different from ordinary exec_prepared_stmt() in that
13987 ** it has to run the entire query and gather the results into memory
13988 ** first, in order to determine column widths, before providing
13989 ** any output.
13990 */
13991 static void exec_prepared_stmt_columnar(
13992   ShellState *p,                        /* Pointer to ShellState */
13993   sqlite3_stmt *pStmt                   /* Statment to run */
13994 ){
13995   sqlite3_int64 nRow = 0;
13996   int nColumn = 0;
13997   char **azData = 0;
13998   sqlite3_int64 nAlloc = 0;
13999   const char *z;
14000   int rc;
14001   sqlite3_int64 i, nData;
14002   int j, nTotal, w, n;
14003   const char *colSep = 0;
14004   const char *rowSep = 0;
14005
14006   rc = sqlite3_step(pStmt);
14007   if( rc!=SQLITE_ROW ) return;
14008   nColumn = sqlite3_column_count(pStmt);
14009   nAlloc = nColumn*4;
14010   if( nAlloc<=0 ) nAlloc = 1;
14011   azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14012   if( azData==0 ) shell_out_of_memory();
14013   for(i=0; i<nColumn; i++){
14014     azData[i] = strdup(sqlite3_column_name(pStmt,i));
14015   }
14016   do{
14017     if( (nRow+2)*nColumn >= nAlloc ){
14018       nAlloc *= 2;
14019       azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14020       if( azData==0 ) shell_out_of_memory();
14021     }
14022     nRow++;
14023     for(i=0; i<nColumn; i++){
14024       z = (const char*)sqlite3_column_text(pStmt,i);
14025       azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14026     }
14027   }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
14028   if( nColumn>p->nWidth ){
14029     p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
14030     if( p->colWidth==0 ) shell_out_of_memory();
14031     for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14032     p->nWidth = nColumn;
14033     p->actualWidth = &p->colWidth[nColumn];
14034   }
14035   memset(p->actualWidth, 0, nColumn*sizeof(int));
14036   for(i=0; i<nColumn; i++){
14037     w = p->colWidth[i];
14038     if( w<0 ) w = -w;
14039     p->actualWidth[i] = w;
14040   }
14041   nTotal = nColumn*(nRow+1);
14042   for(i=0; i<nTotal; i++){
14043     z = azData[i];
14044     if( z==0 ) z = p->nullValue;
14045     n = strlenChar(z);
14046     j = i%nColumn;
14047     if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
14048   }
14049   if( seenInterrupt ) goto columnar_end;
14050   if( nColumn==0 ) goto columnar_end;
14051   switch( p->cMode ){
14052     case MODE_Column: {
14053       colSep = "  ";
14054       rowSep = "\n";
14055       if( p->showHeader ){
14056         for(i=0; i<nColumn; i++){
14057           w = p->actualWidth[i];
14058           if( p->colWidth[i]<0 ) w = -w;
14059           utf8_width_print(p->out, w, azData[i]);
14060           fputs(i==nColumn-1?"\n":"  ", p->out);
14061         }
14062         for(i=0; i<nColumn; i++){
14063           print_dashes(p->out, p->actualWidth[i]);
14064           fputs(i==nColumn-1?"\n":"  ", p->out);
14065         }
14066       }
14067       break;
14068     }
14069     case MODE_Table: {
14070       colSep = " | ";
14071       rowSep = " |\n";
14072       print_row_separator(p, nColumn, "+");
14073       fputs("| ", p->out);
14074       for(i=0; i<nColumn; i++){
14075         w = p->actualWidth[i];
14076         n = strlenChar(azData[i]);
14077         utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14078         fputs(i==nColumn-1?" |\n":" | ", p->out);
14079       }
14080       print_row_separator(p, nColumn, "+");
14081       break;
14082     }
14083     case MODE_Markdown: {
14084       colSep = " | ";
14085       rowSep = " |\n";
14086       fputs("| ", p->out);
14087       for(i=0; i<nColumn; i++){
14088         w = p->actualWidth[i];
14089         n = strlenChar(azData[i]);
14090         utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14091         fputs(i==nColumn-1?" |\n":" | ", p->out);
14092       }
14093       print_row_separator(p, nColumn, "|");
14094       break;
14095     }
14096     case MODE_Box: {
14097       colSep = " " BOX_13 " ";
14098       rowSep = " " BOX_13 "\n";
14099       print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
14100       utf8_printf(p->out, BOX_13 " ");
14101       for(i=0; i<nColumn; i++){
14102         w = p->actualWidth[i];
14103         n = strlenChar(azData[i]);
14104         utf8_printf(p->out, "%*s%s%*s%s",
14105             (w-n)/2, "", azData[i], (w-n+1)/2, "",
14106             i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
14107       }
14108       print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14109       break;
14110     }
14111   }
14112   for(i=nColumn, j=0; i<nTotal; i++, j++){
14113     if( j==0 && p->cMode!=MODE_Column ){
14114       utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
14115     }
14116     z = azData[i];
14117     if( z==0 ) z = p->nullValue;
14118     w = p->actualWidth[j];
14119     if( p->colWidth[j]<0 ) w = -w;
14120     utf8_width_print(p->out, w, z);
14121     if( j==nColumn-1 ){
14122       utf8_printf(p->out, "%s", rowSep);
14123       j = -1;
14124       if( seenInterrupt ) goto columnar_end;
14125     }else{
14126       utf8_printf(p->out, "%s", colSep);
14127     }
14128   }
14129   if( p->cMode==MODE_Table ){
14130     print_row_separator(p, nColumn, "+");
14131   }else if( p->cMode==MODE_Box ){
14132     print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
14133   }
14134 columnar_end:
14135   if( seenInterrupt ){
14136     utf8_printf(p->out, "Interrupt\n");
14137   }
14138   nData = (nRow+1)*nColumn;
14139   for(i=0; i<nData; i++) free(azData[i]);
14140   sqlite3_free(azData);
14141 }
14142
14143 /*
14144 ** Run a prepared statement
14145 */
14146 static void exec_prepared_stmt(
14147   ShellState *pArg,                                /* Pointer to ShellState */
14148   sqlite3_stmt *pStmt                              /* Statment to run */
14149 ){
14150   int rc;
14151
14152   if( pArg->cMode==MODE_Column
14153    || pArg->cMode==MODE_Table
14154    || pArg->cMode==MODE_Box
14155    || pArg->cMode==MODE_Markdown
14156   ){
14157     exec_prepared_stmt_columnar(pArg, pStmt);
14158     return;
14159   }
14160
14161   /* perform the first step.  this will tell us if we
14162   ** have a result set or not and how wide it is.
14163   */
14164   rc = sqlite3_step(pStmt);
14165   /* if we have a result set... */
14166   if( SQLITE_ROW == rc ){
14167     /* allocate space for col name ptr, value ptr, and type */
14168     int nCol = sqlite3_column_count(pStmt);
14169     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
14170     if( !pData ){
14171       rc = SQLITE_NOMEM;
14172     }else{
14173       char **azCols = (char **)pData;      /* Names of result columns */
14174       char **azVals = &azCols[nCol];       /* Results */
14175       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
14176       int i, x;
14177       assert(sizeof(int) <= sizeof(char *));
14178       /* save off ptrs to column names */
14179       for(i=0; i<nCol; i++){
14180         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
14181       }
14182       do{
14183         /* extract the data and data types */
14184         for(i=0; i<nCol; i++){
14185           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
14186           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
14187             azVals[i] = "";
14188           }else{
14189             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
14190           }
14191           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
14192             rc = SQLITE_NOMEM;
14193             break; /* from for */
14194           }
14195         } /* end for */
14196
14197         /* if data and types extracted successfully... */
14198         if( SQLITE_ROW == rc ){
14199           /* call the supplied callback with the result row data */
14200           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
14201             rc = SQLITE_ABORT;
14202           }else{
14203             rc = sqlite3_step(pStmt);
14204           }
14205         }
14206       } while( SQLITE_ROW == rc );
14207       sqlite3_free(pData);
14208       if( pArg->cMode==MODE_Json ){
14209         fputs("]\n", pArg->out);
14210       }
14211     }
14212   }
14213 }
14214
14215 #ifndef SQLITE_OMIT_VIRTUALTABLE
14216 /*
14217 ** This function is called to process SQL if the previous shell command
14218 ** was ".expert". It passes the SQL in the second argument directly to
14219 ** the sqlite3expert object.
14220 **
14221 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14222 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14223 ** an English language error message. It is the responsibility of the
14224 ** caller to eventually free this buffer using sqlite3_free().
14225 */
14226 static int expertHandleSQL(
14227   ShellState *pState, 
14228   const char *zSql, 
14229   char **pzErr
14230 ){
14231   assert( pState->expert.pExpert );
14232   assert( pzErr==0 || *pzErr==0 );
14233   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
14234 }
14235
14236 /*
14237 ** This function is called either to silently clean up the object
14238 ** created by the ".expert" command (if bCancel==1), or to generate a 
14239 ** report from it and then clean it up (if bCancel==0).
14240 **
14241 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14242 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14243 ** an English language error message. It is the responsibility of the
14244 ** caller to eventually free this buffer using sqlite3_free().
14245 */
14246 static int expertFinish(
14247   ShellState *pState,
14248   int bCancel,
14249   char **pzErr
14250 ){
14251   int rc = SQLITE_OK;
14252   sqlite3expert *p = pState->expert.pExpert;
14253   assert( p );
14254   assert( bCancel || pzErr==0 || *pzErr==0 );
14255   if( bCancel==0 ){
14256     FILE *out = pState->out;
14257     int bVerbose = pState->expert.bVerbose;
14258
14259     rc = sqlite3_expert_analyze(p, pzErr);
14260     if( rc==SQLITE_OK ){
14261       int nQuery = sqlite3_expert_count(p);
14262       int i;
14263
14264       if( bVerbose ){
14265         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
14266         raw_printf(out, "-- Candidates -----------------------------\n");
14267         raw_printf(out, "%s\n", zCand);
14268       }
14269       for(i=0; i<nQuery; i++){
14270         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
14271         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
14272         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
14273         if( zIdx==0 ) zIdx = "(no new indexes)\n";
14274         if( bVerbose ){
14275           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
14276           raw_printf(out, "%s\n\n", zSql);
14277         }
14278         raw_printf(out, "%s\n", zIdx);
14279         raw_printf(out, "%s\n", zEQP);
14280       }
14281     }
14282   }
14283   sqlite3_expert_destroy(p);
14284   pState->expert.pExpert = 0;
14285   return rc;
14286 }
14287
14288 /*
14289 ** Implementation of ".expert" dot command.
14290 */
14291 static int expertDotCommand(
14292   ShellState *pState,             /* Current shell tool state */
14293   char **azArg,                   /* Array of arguments passed to dot command */
14294   int nArg                        /* Number of entries in azArg[] */
14295 ){
14296   int rc = SQLITE_OK;
14297   char *zErr = 0;
14298   int i;
14299   int iSample = 0;
14300
14301   assert( pState->expert.pExpert==0 );
14302   memset(&pState->expert, 0, sizeof(ExpertInfo));
14303
14304   for(i=1; rc==SQLITE_OK && i<nArg; i++){
14305     char *z = azArg[i];
14306     int n;
14307     if( z[0]=='-' && z[1]=='-' ) z++;
14308     n = strlen30(z);
14309     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
14310       pState->expert.bVerbose = 1;
14311     }
14312     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
14313       if( i==(nArg-1) ){
14314         raw_printf(stderr, "option requires an argument: %s\n", z);
14315         rc = SQLITE_ERROR;
14316       }else{
14317         iSample = (int)integerValue(azArg[++i]);
14318         if( iSample<0 || iSample>100 ){
14319           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
14320           rc = SQLITE_ERROR;
14321         }
14322       }
14323     }
14324     else{
14325       raw_printf(stderr, "unknown option: %s\n", z);
14326       rc = SQLITE_ERROR;
14327     }
14328   }
14329
14330   if( rc==SQLITE_OK ){
14331     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
14332     if( pState->expert.pExpert==0 ){
14333       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
14334       rc = SQLITE_ERROR;
14335     }else{
14336       sqlite3_expert_config(
14337           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
14338       );
14339     }
14340   }
14341
14342   return rc;
14343 }
14344 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14345
14346 /*
14347 ** Execute a statement or set of statements.  Print
14348 ** any result rows/columns depending on the current mode
14349 ** set via the supplied callback.
14350 **
14351 ** This is very similar to SQLite's built-in sqlite3_exec()
14352 ** function except it takes a slightly different callback
14353 ** and callback data argument.
14354 */
14355 static int shell_exec(
14356   ShellState *pArg,                         /* Pointer to ShellState */
14357   const char *zSql,                         /* SQL to be evaluated */
14358   char **pzErrMsg                           /* Error msg written here */
14359 ){
14360   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
14361   int rc = SQLITE_OK;             /* Return Code */
14362   int rc2;
14363   const char *zLeftover;          /* Tail of unprocessed SQL */
14364   sqlite3 *db = pArg->db;
14365
14366   if( pzErrMsg ){
14367     *pzErrMsg = NULL;
14368   }
14369
14370 #ifndef SQLITE_OMIT_VIRTUALTABLE
14371   if( pArg->expert.pExpert ){
14372     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
14373     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
14374   }
14375 #endif
14376
14377   while( zSql[0] && (SQLITE_OK == rc) ){
14378     static const char *zStmtSql;
14379     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
14380     if( SQLITE_OK != rc ){
14381       if( pzErrMsg ){
14382         *pzErrMsg = save_err_msg(db);
14383       }
14384     }else{
14385       if( !pStmt ){
14386         /* this happens for a comment or white-space */
14387         zSql = zLeftover;
14388         while( IsSpace(zSql[0]) ) zSql++;
14389         continue;
14390       }
14391       zStmtSql = sqlite3_sql(pStmt);
14392       if( zStmtSql==0 ) zStmtSql = "";
14393       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
14394
14395       /* save off the prepared statment handle and reset row count */
14396       if( pArg ){
14397         pArg->pStmt = pStmt;
14398         pArg->cnt = 0;
14399       }
14400
14401       /* echo the sql statement if echo on */
14402       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
14403         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
14404       }
14405
14406       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
14407       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
14408         sqlite3_stmt *pExplain;
14409         char *zEQP;
14410         int triggerEQP = 0;
14411         disable_debug_trace_modes();
14412         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
14413         if( pArg->autoEQP>=AUTOEQP_trigger ){
14414           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
14415         }
14416         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
14417         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14418         if( rc==SQLITE_OK ){
14419           while( sqlite3_step(pExplain)==SQLITE_ROW ){
14420             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
14421             int iEqpId = sqlite3_column_int(pExplain, 0);
14422             int iParentId = sqlite3_column_int(pExplain, 1);
14423             if( zEQPLine==0 ) zEQPLine = "";
14424             if( zEQPLine[0]=='-' ) eqp_render(pArg);
14425             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
14426           }
14427           eqp_render(pArg);
14428         }
14429         sqlite3_finalize(pExplain);
14430         sqlite3_free(zEQP);
14431         if( pArg->autoEQP>=AUTOEQP_full ){
14432           /* Also do an EXPLAIN for ".eqp full" mode */
14433           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
14434           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14435           if( rc==SQLITE_OK ){
14436             pArg->cMode = MODE_Explain;
14437             explain_data_prepare(pArg, pExplain);
14438             exec_prepared_stmt(pArg, pExplain);
14439             explain_data_delete(pArg);
14440           }
14441           sqlite3_finalize(pExplain);
14442           sqlite3_free(zEQP);
14443         }
14444         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
14445           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
14446           /* Reprepare pStmt before reactiving trace modes */
14447           sqlite3_finalize(pStmt);
14448           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
14449           if( pArg ) pArg->pStmt = pStmt;
14450         }
14451         restore_debug_trace_modes();
14452       }
14453
14454       if( pArg ){
14455         pArg->cMode = pArg->mode;
14456         if( pArg->autoExplain ){
14457           if( sqlite3_stmt_isexplain(pStmt)==1 ){
14458             pArg->cMode = MODE_Explain;
14459           }
14460           if( sqlite3_stmt_isexplain(pStmt)==2 ){
14461             pArg->cMode = MODE_EQP;
14462           }
14463         }
14464
14465         /* If the shell is currently in ".explain" mode, gather the extra
14466         ** data required to add indents to the output.*/
14467         if( pArg->cMode==MODE_Explain ){
14468           explain_data_prepare(pArg, pStmt);
14469         }
14470       }
14471
14472       bind_prepared_stmt(pArg, pStmt);
14473       exec_prepared_stmt(pArg, pStmt);
14474       explain_data_delete(pArg);
14475       eqp_render(pArg);
14476
14477       /* print usage stats if stats on */
14478       if( pArg && pArg->statsOn ){
14479         display_stats(db, pArg, 0);
14480       }
14481
14482       /* print loop-counters if required */
14483       if( pArg && pArg->scanstatsOn ){
14484         display_scanstats(db, pArg);
14485       }
14486
14487       /* Finalize the statement just executed. If this fails, save a
14488       ** copy of the error message. Otherwise, set zSql to point to the
14489       ** next statement to execute. */
14490       rc2 = sqlite3_finalize(pStmt);
14491       if( rc!=SQLITE_NOMEM ) rc = rc2;
14492       if( rc==SQLITE_OK ){
14493         zSql = zLeftover;
14494         while( IsSpace(zSql[0]) ) zSql++;
14495       }else if( pzErrMsg ){
14496         *pzErrMsg = save_err_msg(db);
14497       }
14498
14499       /* clear saved stmt handle */
14500       if( pArg ){
14501         pArg->pStmt = NULL;
14502       }
14503     }
14504   } /* end while */
14505
14506   return rc;
14507 }
14508
14509 /*
14510 ** Release memory previously allocated by tableColumnList().
14511 */
14512 static void freeColumnList(char **azCol){
14513   int i;
14514   for(i=1; azCol[i]; i++){
14515     sqlite3_free(azCol[i]);
14516   }
14517   /* azCol[0] is a static string */
14518   sqlite3_free(azCol);
14519 }
14520
14521 /*
14522 ** Return a list of pointers to strings which are the names of all
14523 ** columns in table zTab.   The memory to hold the names is dynamically
14524 ** allocated and must be released by the caller using a subsequent call
14525 ** to freeColumnList().
14526 **
14527 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
14528 ** value that needs to be preserved, then azCol[0] is filled in with the
14529 ** name of the rowid column.
14530 **
14531 ** The first regular column in the table is azCol[1].  The list is terminated
14532 ** by an entry with azCol[i]==0.
14533 */
14534 static char **tableColumnList(ShellState *p, const char *zTab){
14535   char **azCol = 0;
14536   sqlite3_stmt *pStmt;
14537   char *zSql;
14538   int nCol = 0;
14539   int nAlloc = 0;
14540   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
14541   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
14542   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
14543   int rc;
14544
14545   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
14546   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14547   sqlite3_free(zSql);
14548   if( rc ) return 0;
14549   while( sqlite3_step(pStmt)==SQLITE_ROW ){
14550     if( nCol>=nAlloc-2 ){
14551       nAlloc = nAlloc*2 + nCol + 10;
14552       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
14553       if( azCol==0 ) shell_out_of_memory();
14554     }
14555     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
14556     if( sqlite3_column_int(pStmt, 5) ){
14557       nPK++;
14558       if( nPK==1
14559        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
14560                           "INTEGER")==0
14561       ){
14562         isIPK = 1;
14563       }else{
14564         isIPK = 0;
14565       }
14566     }
14567   }
14568   sqlite3_finalize(pStmt);
14569   if( azCol==0 ) return 0;
14570   azCol[0] = 0;
14571   azCol[nCol+1] = 0;
14572
14573   /* The decision of whether or not a rowid really needs to be preserved
14574   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
14575   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
14576   ** rowids on tables where the rowid is inaccessible because there are other
14577   ** columns in the table named "rowid", "_rowid_", and "oid".
14578   */
14579   if( preserveRowid && isIPK ){
14580     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
14581     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
14582     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
14583     ** ROWID aliases.  To distinguish these cases, check to see if
14584     ** there is a "pk" entry in "PRAGMA index_list".  There will be
14585     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
14586     */
14587     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
14588                            " WHERE origin='pk'", zTab);
14589     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14590     sqlite3_free(zSql);
14591     if( rc ){
14592       freeColumnList(azCol);
14593       return 0;
14594     }
14595     rc = sqlite3_step(pStmt);
14596     sqlite3_finalize(pStmt);
14597     preserveRowid = rc==SQLITE_ROW;
14598   }
14599   if( preserveRowid ){
14600     /* Only preserve the rowid if we can find a name to use for the
14601     ** rowid */
14602     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
14603     int i, j;
14604     for(j=0; j<3; j++){
14605       for(i=1; i<=nCol; i++){
14606         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
14607       }
14608       if( i>nCol ){
14609         /* At this point, we know that azRowid[j] is not the name of any
14610         ** ordinary column in the table.  Verify that azRowid[j] is a valid
14611         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
14612         ** tables will fail this last check */
14613         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
14614         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
14615         break;
14616       }
14617     }
14618   }
14619   return azCol;
14620 }
14621
14622 /*
14623 ** Toggle the reverse_unordered_selects setting.
14624 */
14625 static void toggleSelectOrder(sqlite3 *db){
14626   sqlite3_stmt *pStmt = 0;
14627   int iSetting = 0;
14628   char zStmt[100];
14629   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
14630   if( sqlite3_step(pStmt)==SQLITE_ROW ){
14631     iSetting = sqlite3_column_int(pStmt, 0);
14632   }
14633   sqlite3_finalize(pStmt);
14634   sqlite3_snprintf(sizeof(zStmt), zStmt,
14635        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
14636   sqlite3_exec(db, zStmt, 0, 0, 0);
14637 }
14638
14639 /*
14640 ** This is a different callback routine used for dumping the database.
14641 ** Each row received by this callback consists of a table name,
14642 ** the table type ("index" or "table") and SQL to create the table.
14643 ** This routine should print text sufficient to recreate the table.
14644 */
14645 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
14646   int rc;
14647   const char *zTable;
14648   const char *zType;
14649   const char *zSql;
14650   ShellState *p = (ShellState *)pArg;
14651   int dataOnly;
14652   int noSys;
14653
14654   UNUSED_PARAMETER(azNotUsed);
14655   if( nArg!=3 || azArg==0 ) return 0;
14656   zTable = azArg[0];
14657   zType = azArg[1];
14658   zSql = azArg[2];
14659   dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
14660   noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
14661
14662   if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
14663     if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
14664   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
14665     if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
14666   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
14667     return 0;
14668   }else if( dataOnly ){
14669     /* no-op */
14670   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
14671     char *zIns;
14672     if( !p->writableSchema ){
14673       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
14674       p->writableSchema = 1;
14675     }
14676     zIns = sqlite3_mprintf(
14677        "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
14678        "VALUES('table','%q','%q',0,'%q');",
14679        zTable, zTable, zSql);
14680     utf8_printf(p->out, "%s\n", zIns);
14681     sqlite3_free(zIns);
14682     return 0;
14683   }else{
14684     printSchemaLine(p->out, zSql, ";\n");
14685   }
14686
14687   if( strcmp(zType, "table")==0 ){
14688     ShellText sSelect;
14689     ShellText sTable;
14690     char **azCol;
14691     int i;
14692     char *savedDestTable;
14693     int savedMode;
14694
14695     azCol = tableColumnList(p, zTable);
14696     if( azCol==0 ){
14697       p->nErr++;
14698       return 0;
14699     }
14700
14701     /* Always quote the table name, even if it appears to be pure ascii,
14702     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
14703     initText(&sTable);
14704     appendText(&sTable, zTable, quoteChar(zTable));
14705     /* If preserving the rowid, add a column list after the table name.
14706     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
14707     ** instead of the usual "INSERT INTO tab VALUES(...)".
14708     */
14709     if( azCol[0] ){
14710       appendText(&sTable, "(", 0);
14711       appendText(&sTable, azCol[0], 0);
14712       for(i=1; azCol[i]; i++){
14713         appendText(&sTable, ",", 0);
14714         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
14715       }
14716       appendText(&sTable, ")", 0);
14717     }
14718
14719     /* Build an appropriate SELECT statement */
14720     initText(&sSelect);
14721     appendText(&sSelect, "SELECT ", 0);
14722     if( azCol[0] ){
14723       appendText(&sSelect, azCol[0], 0);
14724       appendText(&sSelect, ",", 0);
14725     }
14726     for(i=1; azCol[i]; i++){
14727       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
14728       if( azCol[i+1] ){
14729         appendText(&sSelect, ",", 0);
14730       }
14731     }
14732     freeColumnList(azCol);
14733     appendText(&sSelect, " FROM ", 0);
14734     appendText(&sSelect, zTable, quoteChar(zTable));
14735
14736     savedDestTable = p->zDestTable;
14737     savedMode = p->mode;
14738     p->zDestTable = sTable.z;
14739     p->mode = p->cMode = MODE_Insert;
14740     rc = shell_exec(p, sSelect.z, 0);
14741     if( (rc&0xff)==SQLITE_CORRUPT ){
14742       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14743       toggleSelectOrder(p->db);
14744       shell_exec(p, sSelect.z, 0);
14745       toggleSelectOrder(p->db);
14746     }
14747     p->zDestTable = savedDestTable;
14748     p->mode = savedMode;
14749     freeText(&sTable);
14750     freeText(&sSelect);
14751     if( rc ) p->nErr++;
14752   }
14753   return 0;
14754 }
14755
14756 /*
14757 ** Run zQuery.  Use dump_callback() as the callback routine so that
14758 ** the contents of the query are output as SQL statements.
14759 **
14760 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
14761 ** "ORDER BY rowid DESC" to the end.
14762 */
14763 static int run_schema_dump_query(
14764   ShellState *p,
14765   const char *zQuery
14766 ){
14767   int rc;
14768   char *zErr = 0;
14769   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
14770   if( rc==SQLITE_CORRUPT ){
14771     char *zQ2;
14772     int len = strlen30(zQuery);
14773     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14774     if( zErr ){
14775       utf8_printf(p->out, "/****** %s ******/\n", zErr);
14776       sqlite3_free(zErr);
14777       zErr = 0;
14778     }
14779     zQ2 = malloc( len+100 );
14780     if( zQ2==0 ) return rc;
14781     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
14782     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
14783     if( rc ){
14784       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
14785     }else{
14786       rc = SQLITE_CORRUPT;
14787     }
14788     sqlite3_free(zErr);
14789     free(zQ2);
14790   }
14791   return rc;
14792 }
14793
14794 /*
14795 ** Text of help messages.
14796 **
14797 ** The help text for each individual command begins with a line that starts
14798 ** with ".".  Subsequent lines are supplimental information.
14799 **
14800 ** There must be two or more spaces between the end of the command and the
14801 ** start of the description of what that command does.
14802 */
14803 static const char *(azHelp[]) = {
14804 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
14805   ".archive ...             Manage SQL archives",
14806   "   Each command must have exactly one of the following options:",
14807   "     -c, --create               Create a new archive",
14808   "     -u, --update               Add or update files with changed mtime",
14809   "     -i, --insert               Like -u but always add even if unchanged",
14810   "     -t, --list                 List contents of archive",
14811   "     -x, --extract              Extract files from archive",
14812   "   Optional arguments:",
14813   "     -v, --verbose              Print each filename as it is processed",
14814   "     -f FILE, --file FILE       Use archive FILE (default is current db)",
14815   "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
14816   "     -C DIR, --directory DIR    Read/extract files from directory DIR",
14817   "     -n, --dryrun               Show the SQL that would have occurred",
14818   "   Examples:",
14819   "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
14820   "     .ar -tf ARCHIVE          # List members of ARCHIVE",
14821   "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
14822   "   See also:",
14823   "      http://sqlite.org/cli.html#sqlite_archive_support",
14824 #endif
14825 #ifndef SQLITE_OMIT_AUTHORIZATION
14826   ".auth ON|OFF             Show authorizer callbacks",
14827 #endif
14828   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
14829   "       --append            Use the appendvfs",
14830   "       --async             Write to FILE without journal and fsync()",
14831   ".bail on|off             Stop after hitting an error.  Default OFF",
14832   ".binary on|off           Turn binary output on or off.  Default OFF",
14833   ".cd DIRECTORY            Change the working directory to DIRECTORY",
14834   ".changes on|off          Show number of rows changed by SQL",
14835   ".check GLOB              Fail if output since .testcase does not match",
14836   ".clone NEWDB             Clone data into NEWDB from the existing database",
14837   ".databases               List names and files of attached databases",
14838   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
14839   ".dbinfo ?DB?             Show status information about the database",
14840   ".dump ?OBJECTS?          Render database content as SQL",
14841   "   Options:",
14842   "     --data-only            Output only INSERT statements",
14843   "     --newlines             Allow unescaped newline characters in output",
14844   "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
14845   "     --preserve-rowids      Include ROWID values in the output",
14846   "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
14847   "   Additional LIKE patterns can be given in subsequent arguments",
14848   ".echo on|off             Turn command echo on or off",
14849   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
14850   "   Other Modes:",
14851 #ifdef SQLITE_DEBUG
14852   "      test                  Show raw EXPLAIN QUERY PLAN output",
14853   "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
14854 #endif
14855   "      trigger               Like \"full\" but also show trigger bytecode",
14856   ".excel                   Display the output of next command in spreadsheet",
14857   "   --bom                   Put a UTF8 byte-order mark on intermediate file",
14858   ".exit ?CODE?             Exit this program with return-code CODE",
14859   ".expert                  EXPERIMENTAL. Suggest indexes for queries",
14860   ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
14861   ".filectrl CMD ...        Run various sqlite3_file_control() operations",
14862   "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
14863   "   --help                  Show CMD details",
14864   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
14865   ".headers on|off          Turn display of headers on or off",
14866   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
14867   ".import FILE TABLE       Import data from FILE into TABLE",
14868   "   Options:",
14869   "     --ascii               Use \\037 and \\036 as column and row separators",
14870   "     --csv                 Use , and \\n as column and row separators",
14871   "     --skip N              Skip the first N rows of input",
14872   "     -v                    \"Verbose\" - increase auxiliary output",
14873   "   Notes:",
14874   "     *  If TABLE does not exist, it is created.  The first row of input",
14875   "        determines the column names.",
14876   "     *  If neither --csv or --ascii are used, the input mode is derived",
14877   "        from the \".mode\" output mode",
14878   "     *  If FILE begins with \"|\" then it is a command that generates the",
14879   "        input text.",
14880 #ifndef SQLITE_OMIT_TEST_CONTROL
14881   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
14882 #endif
14883   ".indexes ?TABLE?         Show names of indexes",
14884   "                           If TABLE is specified, only show indexes for",
14885   "                           tables matching TABLE using the LIKE operator.",
14886 #ifdef SQLITE_ENABLE_IOTRACE
14887   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
14888 #endif
14889   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
14890   ".lint OPTIONS            Report potential schema issues.",
14891   "     Options:",
14892   "        fkey-indexes     Find missing foreign key indexes",
14893 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14894   ".load FILE ?ENTRY?       Load an extension library",
14895 #endif
14896   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
14897   ".mode MODE ?TABLE?       Set output mode",
14898   "   MODE is one of:",
14899   "     ascii     Columns/rows delimited by 0x1F and 0x1E",
14900   "     box       Tables using unicode box-drawing characters",
14901   "     csv       Comma-separated values",
14902   "     column    Output in columns.  (See .width)",
14903   "     html      HTML <table> code",
14904   "     insert    SQL insert statements for TABLE",
14905   "     json      Results in a JSON array",
14906   "     line      One value per line",
14907   "     list      Values delimited by \"|\"",
14908   "     markdown  Markdown table format",
14909   "     quote     Escape answers as for SQL",
14910   "     table     ASCII-art table",
14911   "     tabs      Tab-separated values",
14912   "     tcl       TCL list elements",
14913   ".nullvalue STRING        Use STRING in place of NULL values",
14914   ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
14915   "     If FILE begins with '|' then open as a pipe",
14916   "       --bom  Put a UTF8 byte-order mark at the beginning",
14917   "       -e     Send output to the system text editor",
14918   "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
14919 #ifdef SQLITE_DEBUG
14920   ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
14921 #endif 
14922   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
14923   "     Options:",
14924   "        --append        Use appendvfs to append database to the end of FILE",
14925 #ifndef SQLITE_OMIT_DESERIALIZE
14926   "        --deserialize   Load into memory using sqlite3_deserialize()",
14927   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
14928   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
14929 #endif
14930   "        --new           Initialize FILE to an empty database",
14931   "        --nofollow      Do not follow symbolic links",
14932   "        --readonly      Open FILE readonly",
14933   "        --zip           FILE is a ZIP archive",
14934   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
14935   "   If FILE begins with '|' then open it as a pipe.",
14936   "   Options:",
14937   "     --bom                 Prefix output with a UTF8 byte-order mark",
14938   "     -e                    Send output to the system text editor",
14939   "     -x                    Send output as CSV to a spreadsheet",
14940   ".parameter CMD ...       Manage SQL parameter bindings",
14941   "   clear                   Erase all bindings",
14942   "   init                    Initialize the TEMP table that holds bindings",
14943   "   list                    List the current parameter bindings",
14944   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
14945   "                           PARAMETER should start with one of: $ : @ ?",
14946   "   unset PARAMETER         Remove PARAMETER from the binding table",
14947   ".print STRING...         Print literal STRING",
14948 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14949   ".progress N              Invoke progress handler after every N opcodes",
14950   "   --limit N                 Interrupt after N progress callbacks",
14951   "   --once                    Do no more than one progress interrupt",
14952   "   --quiet|-q                No output except at interrupts",
14953   "   --reset                   Reset the count for each input and interrupt",
14954 #endif
14955   ".prompt MAIN CONTINUE    Replace the standard prompts",
14956   ".quit                    Exit this program",
14957   ".read FILE               Read input from FILE",
14958 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14959   ".recover                 Recover as much data as possible from corrupt db.",
14960   "   --freelist-corrupt       Assume the freelist is corrupt",
14961   "   --recovery-db NAME       Store recovery metadata in database file NAME",
14962   "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
14963   "   --no-rowids              Do not attempt to recover rowid values",
14964   "                            that are not also INTEGER PRIMARY KEYs",
14965 #endif
14966   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
14967   ".save FILE               Write in-memory database into FILE",
14968   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
14969   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
14970   "   Options:",
14971   "      --indent             Try to pretty-print the schema",
14972   "      --nosys              Omit objects whose names start with \"sqlite_\"",
14973   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
14974   "    Options:",
14975   "       --init               Create a new SELFTEST table",
14976   "       -v                   Verbose output",
14977   ".separator COL ?ROW?     Change the column and row separators",
14978 #if defined(SQLITE_ENABLE_SESSION)
14979   ".session ?NAME? CMD ...  Create or control sessions",
14980   "   Subcommands:",
14981   "     attach TABLE             Attach TABLE",
14982   "     changeset FILE           Write a changeset into FILE",
14983   "     close                    Close one session",
14984   "     enable ?BOOLEAN?         Set or query the enable bit",
14985   "     filter GLOB...           Reject tables matching GLOBs",
14986   "     indirect ?BOOLEAN?       Mark or query the indirect status",
14987   "     isempty                  Query whether the session is empty",
14988   "     list                     List currently open session names",
14989   "     open DB NAME             Open a new session on DB",
14990   "     patchset FILE            Write a patchset into FILE",
14991   "   If ?NAME? is omitted, the first defined session is used.",
14992 #endif
14993   ".sha3sum ...             Compute a SHA3 hash of database content",
14994   "    Options:",
14995   "      --schema              Also hash the sqlite_schema table",
14996   "      --sha3-224            Use the sha3-224 algorithm",
14997   "      --sha3-256            Use the sha3-256 algorithm (default)",
14998   "      --sha3-384            Use the sha3-384 algorithm",
14999   "      --sha3-512            Use the sha3-512 algorithm",
15000   "    Any other argument is a LIKE pattern for tables to hash",
15001 #ifndef SQLITE_NOHAVE_SYSTEM
15002   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
15003 #endif
15004   ".show                    Show the current values for various settings",
15005   ".stats ?ARG?             Show stats or turn stats on or off",
15006   "   off                      Turn off automatic stat display",
15007   "   on                       Turn on automatic stat display",
15008   "   stmt                     Show statement stats",
15009   "   vmstep                   Show the virtual machine step count only",
15010 #ifndef SQLITE_NOHAVE_SYSTEM
15011   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
15012 #endif
15013   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
15014   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
15015   ".testctrl CMD ...        Run various sqlite3_test_control() operations",
15016   "                           Run \".testctrl\" with no arguments for details",
15017   ".timeout MS              Try opening locked tables for MS milliseconds",
15018   ".timer on|off            Turn SQL timer on or off",
15019 #ifndef SQLITE_OMIT_TRACE
15020   ".trace ?OPTIONS?         Output each SQL statement as it is run",
15021   "    FILE                    Send output to FILE",
15022   "    stdout                  Send output to stdout",
15023   "    stderr                  Send output to stderr",
15024   "    off                     Disable tracing",
15025   "    --expanded              Expand query parameters",
15026 #ifdef SQLITE_ENABLE_NORMALIZE
15027   "    --normalized            Normal the SQL statements",
15028 #endif
15029   "    --plain                 Show SQL as it is input",
15030   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
15031   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
15032   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
15033   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
15034 #endif /* SQLITE_OMIT_TRACE */
15035 #ifdef SQLITE_DEBUG
15036   ".unmodule NAME ...       Unregister virtual table modules",
15037   "    --allexcept             Unregister everything except those named",
15038 #endif
15039   ".vfsinfo ?AUX?           Information about the top-level VFS",
15040   ".vfslist                 List all available VFSes",
15041   ".vfsname ?AUX?           Print the name of the VFS stack",
15042   ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
15043   "     Negative values right-justify",
15044 };
15045
15046 /*
15047 ** Output help text.
15048 **
15049 ** zPattern describes the set of commands for which help text is provided.
15050 ** If zPattern is NULL, then show all commands, but only give a one-line
15051 ** description of each.
15052 **
15053 ** Return the number of matches.
15054 */
15055 static int showHelp(FILE *out, const char *zPattern){
15056   int i = 0;
15057   int j = 0;
15058   int n = 0;
15059   char *zPat;
15060   if( zPattern==0
15061    || zPattern[0]=='0'
15062    || strcmp(zPattern,"-a")==0
15063    || strcmp(zPattern,"-all")==0
15064    || strcmp(zPattern,"--all")==0
15065   ){
15066     /* Show all commands, but only one line per command */
15067     if( zPattern==0 ) zPattern = "";
15068     for(i=0; i<ArraySize(azHelp); i++){
15069       if( azHelp[i][0]=='.' || zPattern[0] ){
15070         utf8_printf(out, "%s\n", azHelp[i]);
15071         n++;
15072       }
15073     }
15074   }else{
15075     /* Look for commands that for which zPattern is an exact prefix */
15076     zPat = sqlite3_mprintf(".%s*", zPattern);
15077     for(i=0; i<ArraySize(azHelp); i++){
15078       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
15079         utf8_printf(out, "%s\n", azHelp[i]);
15080         j = i+1;
15081         n++;
15082       }
15083     }
15084     sqlite3_free(zPat);
15085     if( n ){
15086       if( n==1 ){
15087         /* when zPattern is a prefix of exactly one command, then include the
15088         ** details of that command, which should begin at offset j */
15089         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
15090           utf8_printf(out, "%s\n", azHelp[j]);
15091           j++;
15092         }
15093       }
15094       return n;
15095     }
15096     /* Look for commands that contain zPattern anywhere.  Show the complete
15097     ** text of all commands that match. */
15098     zPat = sqlite3_mprintf("%%%s%%", zPattern);
15099     for(i=0; i<ArraySize(azHelp); i++){
15100       if( azHelp[i][0]=='.' ) j = i;
15101       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
15102         utf8_printf(out, "%s\n", azHelp[j]);
15103         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
15104           j++;
15105           utf8_printf(out, "%s\n", azHelp[j]);
15106         }
15107         i = j;
15108         n++;
15109       }
15110     }
15111     sqlite3_free(zPat);
15112   }
15113   return n;
15114 }
15115
15116 /* Forward reference */
15117 static int process_input(ShellState *p);
15118
15119 /*
15120 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
15121 ** and return a pointer to the buffer. The caller is responsible for freeing
15122 ** the memory.
15123 **
15124 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
15125 ** read.
15126 **
15127 ** For convenience, a nul-terminator byte is always appended to the data read
15128 ** from the file before the buffer is returned. This byte is not included in
15129 ** the final value of (*pnByte), if applicable.
15130 **
15131 ** NULL is returned if any error is encountered. The final value of *pnByte
15132 ** is undefined in this case.
15133 */
15134 static char *readFile(const char *zName, int *pnByte){
15135   FILE *in = fopen(zName, "rb");
15136   long nIn;
15137   size_t nRead;
15138   char *pBuf;
15139   if( in==0 ) return 0;
15140   fseek(in, 0, SEEK_END);
15141   nIn = ftell(in);
15142   rewind(in);
15143   pBuf = sqlite3_malloc64( nIn+1 );
15144   if( pBuf==0 ){ fclose(in); return 0; }
15145   nRead = fread(pBuf, nIn, 1, in);
15146   fclose(in);
15147   if( nRead!=1 ){
15148     sqlite3_free(pBuf);
15149     return 0;
15150   }
15151   pBuf[nIn] = 0;
15152   if( pnByte ) *pnByte = nIn;
15153   return pBuf;
15154 }
15155
15156 #if defined(SQLITE_ENABLE_SESSION)
15157 /*
15158 ** Close a single OpenSession object and release all of its associated
15159 ** resources.
15160 */
15161 static void session_close(OpenSession *pSession){
15162   int i;
15163   sqlite3session_delete(pSession->p);
15164   sqlite3_free(pSession->zName);
15165   for(i=0; i<pSession->nFilter; i++){
15166     sqlite3_free(pSession->azFilter[i]);
15167   }
15168   sqlite3_free(pSession->azFilter);
15169   memset(pSession, 0, sizeof(OpenSession));
15170 }
15171 #endif
15172
15173 /*
15174 ** Close all OpenSession objects and release all associated resources.
15175 */
15176 #if defined(SQLITE_ENABLE_SESSION)
15177 static void session_close_all(ShellState *p){
15178   int i;
15179   for(i=0; i<p->nSession; i++){
15180     session_close(&p->aSession[i]);
15181   }
15182   p->nSession = 0;
15183 }
15184 #else
15185 # define session_close_all(X)
15186 #endif
15187
15188 /*
15189 ** Implementation of the xFilter function for an open session.  Omit
15190 ** any tables named by ".session filter" but let all other table through.
15191 */
15192 #if defined(SQLITE_ENABLE_SESSION)
15193 static int session_filter(void *pCtx, const char *zTab){
15194   OpenSession *pSession = (OpenSession*)pCtx;
15195   int i;
15196   for(i=0; i<pSession->nFilter; i++){
15197     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
15198   }
15199   return 1;
15200 }
15201 #endif
15202
15203 /*
15204 ** Try to deduce the type of file for zName based on its content.  Return
15205 ** one of the SHELL_OPEN_* constants.
15206 **
15207 ** If the file does not exist or is empty but its name looks like a ZIP
15208 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
15209 ** Otherwise, assume an ordinary database regardless of the filename if
15210 ** the type cannot be determined from content.
15211 */
15212 int deduceDatabaseType(const char *zName, int dfltZip){
15213   FILE *f = fopen(zName, "rb");
15214   size_t n;
15215   int rc = SHELL_OPEN_UNSPEC;
15216   char zBuf[100];
15217   if( f==0 ){
15218     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15219        return SHELL_OPEN_ZIPFILE;
15220     }else{
15221        return SHELL_OPEN_NORMAL;
15222     }
15223   }
15224   n = fread(zBuf, 16, 1, f);
15225   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
15226     fclose(f);
15227     return SHELL_OPEN_NORMAL;
15228   }
15229   fseek(f, -25, SEEK_END);
15230   n = fread(zBuf, 25, 1, f);
15231   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
15232     rc = SHELL_OPEN_APPENDVFS;
15233   }else{
15234     fseek(f, -22, SEEK_END);
15235     n = fread(zBuf, 22, 1, f);
15236     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
15237        && zBuf[3]==0x06 ){
15238       rc = SHELL_OPEN_ZIPFILE;
15239     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15240       rc = SHELL_OPEN_ZIPFILE;
15241     }
15242   }
15243   fclose(f);
15244   return rc;  
15245 }
15246
15247 #ifndef SQLITE_OMIT_DESERIALIZE
15248 /*
15249 ** Reconstruct an in-memory database using the output from the "dbtotxt"
15250 ** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
15251 ** is 0, then read from standard input.
15252 */
15253 static unsigned char *readHexDb(ShellState *p, int *pnData){
15254   unsigned char *a = 0;
15255   int nLine;
15256   int n = 0;
15257   int pgsz = 0;
15258   int iOffset = 0;
15259   int j, k;
15260   int rc;
15261   FILE *in;
15262   unsigned int x[16];
15263   char zLine[1000];
15264   if( p->zDbFilename ){
15265     in = fopen(p->zDbFilename, "r");
15266     if( in==0 ){
15267       utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
15268       return 0;
15269     }
15270     nLine = 0;
15271   }else{
15272     in = p->in;
15273     nLine = p->lineno;
15274     if( in==0 ) in = stdin;
15275   }
15276   *pnData = 0;
15277   nLine++;
15278   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
15279   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
15280   if( rc!=2 ) goto readHexDb_error;
15281   if( n<0 ) goto readHexDb_error;
15282   if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
15283   n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
15284   a = sqlite3_malloc( n ? n : 1 );
15285   if( a==0 ){
15286     utf8_printf(stderr, "Out of memory!\n");
15287     goto readHexDb_error;
15288   }
15289   memset(a, 0, n);
15290   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
15291     utf8_printf(stderr, "invalid pagesize\n");
15292     goto readHexDb_error;
15293   }
15294   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
15295     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
15296     if( rc==2 ){
15297       iOffset = k;
15298       continue;
15299     }
15300     if( strncmp(zLine, "| end ", 6)==0 ){
15301       break;
15302     }
15303     rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
15304                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
15305                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
15306     if( rc==17 ){
15307       k = iOffset+j;
15308       if( k+16<=n ){
15309         int ii;
15310         for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
15311       }
15312     }
15313   }
15314   *pnData = n;
15315   if( in!=p->in ){
15316     fclose(in);
15317   }else{
15318     p->lineno = nLine;
15319   }
15320   return a;
15321
15322 readHexDb_error:
15323   if( in!=p->in ){
15324     fclose(in);
15325   }else{
15326     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
15327       nLine++;
15328       if(strncmp(zLine, "| end ", 6)==0 ) break;
15329     }
15330     p->lineno = nLine;
15331   }
15332   sqlite3_free(a);
15333   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
15334   return 0;
15335 }
15336 #endif /* SQLITE_OMIT_DESERIALIZE */
15337
15338 /*
15339 ** Scalar function "shell_int32". The first argument to this function
15340 ** must be a blob. The second a non-negative integer. This function
15341 ** reads and returns a 32-bit big-endian integer from byte
15342 ** offset (4*<arg2>) of the blob.
15343 */
15344 static void shellInt32(
15345   sqlite3_context *context, 
15346   int argc, 
15347   sqlite3_value **argv
15348 ){
15349   const unsigned char *pBlob;
15350   int nBlob;
15351   int iInt;
15352
15353   UNUSED_PARAMETER(argc);
15354   nBlob = sqlite3_value_bytes(argv[0]);
15355   pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
15356   iInt = sqlite3_value_int(argv[1]);
15357
15358   if( iInt>=0 && (iInt+1)*4<=nBlob ){
15359     const unsigned char *a = &pBlob[iInt*4];
15360     sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
15361                        + ((sqlite3_int64)a[1]<<16)
15362                        + ((sqlite3_int64)a[2]<< 8)
15363                        + ((sqlite3_int64)a[3]<< 0);
15364     sqlite3_result_int64(context, iVal);
15365   }
15366 }
15367
15368 /*
15369 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
15370 ** using "..." with internal double-quote characters doubled.
15371 */
15372 static void shellIdQuote(
15373   sqlite3_context *context, 
15374   int argc, 
15375   sqlite3_value **argv
15376 ){
15377   const char *zName = (const char*)sqlite3_value_text(argv[0]);
15378   UNUSED_PARAMETER(argc);
15379   if( zName ){
15380     char *z = sqlite3_mprintf("\"%w\"", zName);
15381     sqlite3_result_text(context, z, -1, sqlite3_free);
15382   }
15383 }
15384
15385 /*
15386 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
15387 */
15388 static void shellUSleepFunc(
15389   sqlite3_context *context, 
15390   int argcUnused, 
15391   sqlite3_value **argv
15392 ){
15393   int sleep = sqlite3_value_int(argv[0]);
15394   (void)argcUnused;
15395   sqlite3_sleep(sleep/1000);
15396   sqlite3_result_int(context, sleep);
15397 }
15398
15399 /*
15400 ** Scalar function "shell_escape_crnl" used by the .recover command.
15401 ** The argument passed to this function is the output of built-in
15402 ** function quote(). If the first character of the input is "'", 
15403 ** indicating that the value passed to quote() was a text value,
15404 ** then this function searches the input for "\n" and "\r" characters
15405 ** and adds a wrapper similar to the following:
15406 **
15407 **   replace(replace(<input>, '\n', char(10), '\r', char(13));
15408 **
15409 ** Or, if the first character of the input is not "'", then a copy
15410 ** of the input is returned.
15411 */
15412 static void shellEscapeCrnl(
15413   sqlite3_context *context, 
15414   int argc, 
15415   sqlite3_value **argv
15416 ){
15417   const char *zText = (const char*)sqlite3_value_text(argv[0]);
15418   UNUSED_PARAMETER(argc);
15419   if( zText[0]=='\'' ){
15420     int nText = sqlite3_value_bytes(argv[0]);
15421     int i;
15422     char zBuf1[20];
15423     char zBuf2[20];
15424     const char *zNL = 0;
15425     const char *zCR = 0;
15426     int nCR = 0;
15427     int nNL = 0;
15428
15429     for(i=0; zText[i]; i++){
15430       if( zNL==0 && zText[i]=='\n' ){
15431         zNL = unused_string(zText, "\\n", "\\012", zBuf1);
15432         nNL = (int)strlen(zNL);
15433       }
15434       if( zCR==0 && zText[i]=='\r' ){
15435         zCR = unused_string(zText, "\\r", "\\015", zBuf2);
15436         nCR = (int)strlen(zCR);
15437       }
15438     }
15439
15440     if( zNL || zCR ){
15441       int iOut = 0;
15442       i64 nMax = (nNL > nCR) ? nNL : nCR;
15443       i64 nAlloc = nMax * nText + (nMax+64)*2;
15444       char *zOut = (char*)sqlite3_malloc64(nAlloc);
15445       if( zOut==0 ){
15446         sqlite3_result_error_nomem(context);
15447         return;
15448       }
15449
15450       if( zNL && zCR ){
15451         memcpy(&zOut[iOut], "replace(replace(", 16);
15452         iOut += 16;
15453       }else{
15454         memcpy(&zOut[iOut], "replace(", 8);
15455         iOut += 8;
15456       }
15457       for(i=0; zText[i]; i++){
15458         if( zText[i]=='\n' ){
15459           memcpy(&zOut[iOut], zNL, nNL);
15460           iOut += nNL;
15461         }else if( zText[i]=='\r' ){
15462           memcpy(&zOut[iOut], zCR, nCR);
15463           iOut += nCR;
15464         }else{
15465           zOut[iOut] = zText[i];
15466           iOut++;
15467         }
15468       }
15469
15470       if( zNL ){
15471         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15472         memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
15473         memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
15474       }
15475       if( zCR ){
15476         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15477         memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
15478         memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
15479       }
15480
15481       sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
15482       sqlite3_free(zOut);
15483       return;
15484     }
15485   }
15486
15487   sqlite3_result_value(context, argv[0]);
15488 }
15489
15490 /* Flags for open_db().
15491 **
15492 ** The default behavior of open_db() is to exit(1) if the database fails to
15493 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
15494 ** but still returns without calling exit.
15495 **
15496 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
15497 ** ZIP archive if the file does not exist or is empty and its name matches
15498 ** the *.zip pattern.
15499 */
15500 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
15501 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
15502
15503 /*
15504 ** Make sure the database is open.  If it is not, then open it.  If
15505 ** the database fails to open, print an error message and exit.
15506 */
15507 static void open_db(ShellState *p, int openFlags){
15508   if( p->db==0 ){
15509     if( p->openMode==SHELL_OPEN_UNSPEC ){
15510       if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
15511         p->openMode = SHELL_OPEN_NORMAL;
15512       }else{
15513         p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 
15514                              (openFlags & OPEN_DB_ZIPFILE)!=0);
15515       }
15516     }
15517     switch( p->openMode ){
15518       case SHELL_OPEN_APPENDVFS: {
15519         sqlite3_open_v2(p->zDbFilename, &p->db, 
15520            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
15521         break;
15522       }
15523       case SHELL_OPEN_HEXDB:
15524       case SHELL_OPEN_DESERIALIZE: {
15525         sqlite3_open(0, &p->db);
15526         break;
15527       }
15528       case SHELL_OPEN_ZIPFILE: {
15529         sqlite3_open(":memory:", &p->db);
15530         break;
15531       }
15532       case SHELL_OPEN_READONLY: {
15533         sqlite3_open_v2(p->zDbFilename, &p->db,
15534             SQLITE_OPEN_READONLY|p->openFlags, 0);
15535         break;
15536       }
15537       case SHELL_OPEN_UNSPEC:
15538       case SHELL_OPEN_NORMAL: {
15539         sqlite3_open_v2(p->zDbFilename, &p->db,
15540            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
15541         break;
15542       }
15543     }
15544     globalDb = p->db;
15545     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
15546       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
15547           p->zDbFilename, sqlite3_errmsg(p->db));
15548       if( openFlags & OPEN_DB_KEEPALIVE ){
15549         sqlite3_open(":memory:", &p->db);
15550         return;
15551       }
15552       exit(1);
15553     }
15554 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15555     sqlite3_enable_load_extension(p->db, 1);
15556 #endif
15557     sqlite3_fileio_init(p->db, 0, 0);
15558     sqlite3_shathree_init(p->db, 0, 0);
15559     sqlite3_completion_init(p->db, 0, 0);
15560     sqlite3_uint_init(p->db, 0, 0);
15561     sqlite3_decimal_init(p->db, 0, 0);
15562     sqlite3_regexp_init(p->db, 0, 0);
15563     sqlite3_ieee_init(p->db, 0, 0);
15564     sqlite3_series_init(p->db, 0, 0);
15565 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15566     sqlite3_dbdata_init(p->db, 0, 0);
15567 #endif
15568 #ifdef SQLITE_HAVE_ZLIB
15569     sqlite3_zipfile_init(p->db, 0, 0);
15570     sqlite3_sqlar_init(p->db, 0, 0);
15571 #endif
15572     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
15573                             shellAddSchemaName, 0, 0);
15574     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
15575                             shellModuleSchema, 0, 0);
15576     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
15577                             shellPutsFunc, 0, 0);
15578     sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
15579                             shellEscapeCrnl, 0, 0);
15580     sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
15581                             shellInt32, 0, 0);
15582     sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
15583                             shellIdQuote, 0, 0);
15584     sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
15585                             shellUSleepFunc, 0, 0);
15586 #ifndef SQLITE_NOHAVE_SYSTEM
15587     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
15588                             editFunc, 0, 0);
15589     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
15590                             editFunc, 0, 0);
15591 #endif
15592     if( p->openMode==SHELL_OPEN_ZIPFILE ){
15593       char *zSql = sqlite3_mprintf(
15594          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
15595       sqlite3_exec(p->db, zSql, 0, 0, 0);
15596       sqlite3_free(zSql);
15597     }
15598 #ifndef SQLITE_OMIT_DESERIALIZE
15599     else
15600     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
15601       int rc;
15602       int nData = 0;
15603       unsigned char *aData;
15604       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
15605         aData = (unsigned char*)readFile(p->zDbFilename, &nData);
15606       }else{
15607         aData = readHexDb(p, &nData);
15608         if( aData==0 ){
15609           return;
15610         }
15611       }
15612       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
15613                    SQLITE_DESERIALIZE_RESIZEABLE |
15614                    SQLITE_DESERIALIZE_FREEONCLOSE);
15615       if( rc ){
15616         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
15617       }
15618       if( p->szMax>0 ){
15619         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
15620       }
15621     }
15622 #endif
15623   }
15624 }
15625
15626 /*
15627 ** Attempt to close the databaes connection.  Report errors.
15628 */
15629 void close_db(sqlite3 *db){
15630   int rc = sqlite3_close(db);
15631   if( rc ){
15632     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
15633         rc, sqlite3_errmsg(db));
15634   } 
15635 }
15636
15637 #if HAVE_READLINE || HAVE_EDITLINE
15638 /*
15639 ** Readline completion callbacks
15640 */
15641 static char *readline_completion_generator(const char *text, int state){
15642   static sqlite3_stmt *pStmt = 0;
15643   char *zRet;
15644   if( state==0 ){
15645     char *zSql;
15646     sqlite3_finalize(pStmt);
15647     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15648                            "  FROM completion(%Q) ORDER BY 1", text);
15649     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15650     sqlite3_free(zSql);
15651   }
15652   if( sqlite3_step(pStmt)==SQLITE_ROW ){
15653     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
15654   }else{
15655     sqlite3_finalize(pStmt);
15656     pStmt = 0;
15657     zRet = 0;
15658   }
15659   return zRet;
15660 }
15661 static char **readline_completion(const char *zText, int iStart, int iEnd){
15662   rl_attempted_completion_over = 1;
15663   return rl_completion_matches(zText, readline_completion_generator);
15664 }
15665
15666 #elif HAVE_LINENOISE
15667 /*
15668 ** Linenoise completion callback
15669 */
15670 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
15671   int nLine = strlen30(zLine);
15672   int i, iStart;
15673   sqlite3_stmt *pStmt = 0;
15674   char *zSql;
15675   char zBuf[1000];
15676
15677   if( nLine>sizeof(zBuf)-30 ) return;
15678   if( zLine[0]=='.' || zLine[0]=='#') return;
15679   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
15680   if( i==nLine-1 ) return;
15681   iStart = i+1;
15682   memcpy(zBuf, zLine, iStart);
15683   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15684                          "  FROM completion(%Q,%Q) ORDER BY 1",
15685                          &zLine[iStart], zLine);
15686   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15687   sqlite3_free(zSql);
15688   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
15689   while( sqlite3_step(pStmt)==SQLITE_ROW ){
15690     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
15691     int nCompletion = sqlite3_column_bytes(pStmt, 0);
15692     if( iStart+nCompletion < sizeof(zBuf)-1 ){
15693       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
15694       linenoiseAddCompletion(lc, zBuf);
15695     }
15696   }
15697   sqlite3_finalize(pStmt);
15698 }
15699 #endif
15700
15701 /*
15702 ** Do C-language style dequoting.
15703 **
15704 **    \a    -> alarm
15705 **    \b    -> backspace
15706 **    \t    -> tab
15707 **    \n    -> newline
15708 **    \v    -> vertical tab
15709 **    \f    -> form feed
15710 **    \r    -> carriage return
15711 **    \s    -> space
15712 **    \"    -> "
15713 **    \'    -> '
15714 **    \\    -> backslash
15715 **    \NNN  -> ascii character NNN in octal
15716 */
15717 static void resolve_backslashes(char *z){
15718   int i, j;
15719   char c;
15720   while( *z && *z!='\\' ) z++;
15721   for(i=j=0; (c = z[i])!=0; i++, j++){
15722     if( c=='\\' && z[i+1]!=0 ){
15723       c = z[++i];
15724       if( c=='a' ){
15725         c = '\a';
15726       }else if( c=='b' ){
15727         c = '\b';
15728       }else if( c=='t' ){
15729         c = '\t';
15730       }else if( c=='n' ){
15731         c = '\n';
15732       }else if( c=='v' ){
15733         c = '\v';
15734       }else if( c=='f' ){
15735         c = '\f';
15736       }else if( c=='r' ){
15737         c = '\r';
15738       }else if( c=='"' ){
15739         c = '"';
15740       }else if( c=='\'' ){
15741         c = '\'';
15742       }else if( c=='\\' ){
15743         c = '\\';
15744       }else if( c>='0' && c<='7' ){
15745         c -= '0';
15746         if( z[i+1]>='0' && z[i+1]<='7' ){
15747           i++;
15748           c = (c<<3) + z[i] - '0';
15749           if( z[i+1]>='0' && z[i+1]<='7' ){
15750             i++;
15751             c = (c<<3) + z[i] - '0';
15752           }
15753         }
15754       }
15755     }
15756     z[j] = c;
15757   }
15758   if( j<i ) z[j] = 0;
15759 }
15760
15761 /*
15762 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
15763 ** for TRUE and FALSE.  Return the integer value if appropriate.
15764 */
15765 static int booleanValue(const char *zArg){
15766   int i;
15767   if( zArg[0]=='0' && zArg[1]=='x' ){
15768     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
15769   }else{
15770     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
15771   }
15772   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
15773   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
15774     return 1;
15775   }
15776   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
15777     return 0;
15778   }
15779   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
15780           zArg);
15781   return 0;
15782 }
15783
15784 /*
15785 ** Set or clear a shell flag according to a boolean value.
15786 */
15787 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
15788   if( booleanValue(zArg) ){
15789     ShellSetFlag(p, mFlag);
15790   }else{
15791     ShellClearFlag(p, mFlag);
15792   }
15793 }
15794
15795 /*
15796 ** Close an output file, assuming it is not stderr or stdout
15797 */
15798 static void output_file_close(FILE *f){
15799   if( f && f!=stdout && f!=stderr ) fclose(f);
15800 }
15801
15802 /*
15803 ** Try to open an output file.   The names "stdout" and "stderr" are
15804 ** recognized and do the right thing.  NULL is returned if the output
15805 ** filename is "off".
15806 */
15807 static FILE *output_file_open(const char *zFile, int bTextMode){
15808   FILE *f;
15809   if( strcmp(zFile,"stdout")==0 ){
15810     f = stdout;
15811   }else if( strcmp(zFile, "stderr")==0 ){
15812     f = stderr;
15813   }else if( strcmp(zFile, "off")==0 ){
15814     f = 0;
15815   }else{
15816     f = fopen(zFile, bTextMode ? "w" : "wb");
15817     if( f==0 ){
15818       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
15819     }
15820   }
15821   return f;
15822 }
15823
15824 #ifndef SQLITE_OMIT_TRACE
15825 /*
15826 ** A routine for handling output from sqlite3_trace().
15827 */
15828 static int sql_trace_callback(
15829   unsigned mType,         /* The trace type */
15830   void *pArg,             /* The ShellState pointer */
15831   void *pP,               /* Usually a pointer to sqlite_stmt */
15832   void *pX                /* Auxiliary output */
15833 ){
15834   ShellState *p = (ShellState*)pArg;
15835   sqlite3_stmt *pStmt;
15836   const char *zSql;
15837   int nSql;
15838   if( p->traceOut==0 ) return 0;
15839   if( mType==SQLITE_TRACE_CLOSE ){
15840     utf8_printf(p->traceOut, "-- closing database connection\n");
15841     return 0;
15842   }
15843   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
15844     zSql = (const char*)pX;
15845   }else{
15846     pStmt = (sqlite3_stmt*)pP;
15847     switch( p->eTraceType ){
15848       case SHELL_TRACE_EXPANDED: {
15849         zSql = sqlite3_expanded_sql(pStmt);
15850         break;
15851       }
15852 #ifdef SQLITE_ENABLE_NORMALIZE
15853       case SHELL_TRACE_NORMALIZED: {
15854         zSql = sqlite3_normalized_sql(pStmt);
15855         break;
15856       }
15857 #endif
15858       default: {
15859         zSql = sqlite3_sql(pStmt);
15860         break;
15861       }
15862     }
15863   }
15864   if( zSql==0 ) return 0;
15865   nSql = strlen30(zSql);
15866   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
15867   switch( mType ){
15868     case SQLITE_TRACE_ROW:
15869     case SQLITE_TRACE_STMT: {
15870       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
15871       break;
15872     }
15873     case SQLITE_TRACE_PROFILE: {
15874       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
15875       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
15876       break;
15877     }
15878   }
15879   return 0;
15880 }
15881 #endif
15882
15883 /*
15884 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
15885 ** a useful spot to set a debugger breakpoint.
15886 */
15887 static void test_breakpoint(void){
15888   static int nCall = 0;
15889   nCall++;
15890 }
15891
15892 /*
15893 ** An object used to read a CSV and other files for import.
15894 */
15895 typedef struct ImportCtx ImportCtx;
15896 struct ImportCtx {
15897   const char *zFile;  /* Name of the input file */
15898   FILE *in;           /* Read the CSV text from this input stream */
15899   int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
15900   char *z;            /* Accumulated text for a field */
15901   int n;              /* Number of bytes in z */
15902   int nAlloc;         /* Space allocated for z[] */
15903   int nLine;          /* Current line number */
15904   int nRow;           /* Number of rows imported */
15905   int nErr;           /* Number of errors encountered */
15906   int bNotFirst;      /* True if one or more bytes already read */
15907   int cTerm;          /* Character that terminated the most recent field */
15908   int cColSep;        /* The column separator character.  (Usually ",") */
15909   int cRowSep;        /* The row separator character.  (Usually "\n") */
15910 };
15911
15912 /* Clean up resourced used by an ImportCtx */
15913 static void import_cleanup(ImportCtx *p){
15914   if( p->in!=0 && p->xCloser!=0 ){
15915     p->xCloser(p->in);
15916     p->in = 0;
15917   }
15918   sqlite3_free(p->z);
15919   p->z = 0;
15920 }
15921
15922 /* Append a single byte to z[] */
15923 static void import_append_char(ImportCtx *p, int c){
15924   if( p->n+1>=p->nAlloc ){
15925     p->nAlloc += p->nAlloc + 100;
15926     p->z = sqlite3_realloc64(p->z, p->nAlloc);
15927     if( p->z==0 ) shell_out_of_memory();
15928   }
15929   p->z[p->n++] = (char)c;
15930 }
15931
15932 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
15933 ** with the option of having a separator other than ",".
15934 **
15935 **   +  Input comes from p->in.
15936 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
15937 **      from sqlite3_malloc64().
15938 **   +  Use p->cSep as the column separator.  The default is ",".
15939 **   +  Use p->rSep as the row separator.  The default is "\n".
15940 **   +  Keep track of the line number in p->nLine.
15941 **   +  Store the character that terminates the field in p->cTerm.  Store
15942 **      EOF on end-of-file.
15943 **   +  Report syntax errors on stderr
15944 */
15945 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
15946   int c;
15947   int cSep = p->cColSep;
15948   int rSep = p->cRowSep;
15949   p->n = 0;
15950   c = fgetc(p->in);
15951   if( c==EOF || seenInterrupt ){
15952     p->cTerm = EOF;
15953     return 0;
15954   }
15955   if( c=='"' ){
15956     int pc, ppc;
15957     int startLine = p->nLine;
15958     int cQuote = c;
15959     pc = ppc = 0;
15960     while( 1 ){
15961       c = fgetc(p->in);
15962       if( c==rSep ) p->nLine++;
15963       if( c==cQuote ){
15964         if( pc==cQuote ){
15965           pc = 0;
15966           continue;
15967         }
15968       }
15969       if( (c==cSep && pc==cQuote)
15970        || (c==rSep && pc==cQuote)
15971        || (c==rSep && pc=='\r' && ppc==cQuote)
15972        || (c==EOF && pc==cQuote)
15973       ){
15974         do{ p->n--; }while( p->z[p->n]!=cQuote );
15975         p->cTerm = c;
15976         break;
15977       }
15978       if( pc==cQuote && c!='\r' ){
15979         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
15980                 p->zFile, p->nLine, cQuote);
15981       }
15982       if( c==EOF ){
15983         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
15984                 p->zFile, startLine, cQuote);
15985         p->cTerm = c;
15986         break;
15987       }
15988       import_append_char(p, c);
15989       ppc = pc;
15990       pc = c;
15991     }
15992   }else{
15993     /* If this is the first field being parsed and it begins with the
15994     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
15995     if( (c&0xff)==0xef && p->bNotFirst==0 ){
15996       import_append_char(p, c);
15997       c = fgetc(p->in);
15998       if( (c&0xff)==0xbb ){
15999         import_append_char(p, c);
16000         c = fgetc(p->in);
16001         if( (c&0xff)==0xbf ){
16002           p->bNotFirst = 1;
16003           p->n = 0;
16004           return csv_read_one_field(p);
16005         }
16006       }
16007     }
16008     while( c!=EOF && c!=cSep && c!=rSep ){
16009       import_append_char(p, c);
16010       c = fgetc(p->in);
16011     }
16012     if( c==rSep ){
16013       p->nLine++;
16014       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
16015     }
16016     p->cTerm = c;
16017   }
16018   if( p->z ) p->z[p->n] = 0;
16019   p->bNotFirst = 1;
16020   return p->z;
16021 }
16022
16023 /* Read a single field of ASCII delimited text.
16024 **
16025 **   +  Input comes from p->in.
16026 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
16027 **      from sqlite3_malloc64().
16028 **   +  Use p->cSep as the column separator.  The default is "\x1F".
16029 **   +  Use p->rSep as the row separator.  The default is "\x1E".
16030 **   +  Keep track of the row number in p->nLine.
16031 **   +  Store the character that terminates the field in p->cTerm.  Store
16032 **      EOF on end-of-file.
16033 **   +  Report syntax errors on stderr
16034 */
16035 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
16036   int c;
16037   int cSep = p->cColSep;
16038   int rSep = p->cRowSep;
16039   p->n = 0;
16040   c = fgetc(p->in);
16041   if( c==EOF || seenInterrupt ){
16042     p->cTerm = EOF;
16043     return 0;
16044   }
16045   while( c!=EOF && c!=cSep && c!=rSep ){
16046     import_append_char(p, c);
16047     c = fgetc(p->in);
16048   }
16049   if( c==rSep ){
16050     p->nLine++;
16051   }
16052   p->cTerm = c;
16053   if( p->z ) p->z[p->n] = 0;
16054   return p->z;
16055 }
16056
16057 /*
16058 ** Try to transfer data for table zTable.  If an error is seen while
16059 ** moving forward, try to go backwards.  The backwards movement won't
16060 ** work for WITHOUT ROWID tables.
16061 */
16062 static void tryToCloneData(
16063   ShellState *p,
16064   sqlite3 *newDb,
16065   const char *zTable
16066 ){
16067   sqlite3_stmt *pQuery = 0;
16068   sqlite3_stmt *pInsert = 0;
16069   char *zQuery = 0;
16070   char *zInsert = 0;
16071   int rc;
16072   int i, j, n;
16073   int nTable = strlen30(zTable);
16074   int k = 0;
16075   int cnt = 0;
16076   const int spinRate = 10000;
16077
16078   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
16079   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16080   if( rc ){
16081     utf8_printf(stderr, "Error %d: %s on [%s]\n",
16082             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16083             zQuery);
16084     goto end_data_xfer;
16085   }
16086   n = sqlite3_column_count(pQuery);
16087   zInsert = sqlite3_malloc64(200 + nTable + n*3);
16088   if( zInsert==0 ) shell_out_of_memory();
16089   sqlite3_snprintf(200+nTable,zInsert,
16090                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
16091   i = strlen30(zInsert);
16092   for(j=1; j<n; j++){
16093     memcpy(zInsert+i, ",?", 2);
16094     i += 2;
16095   }
16096   memcpy(zInsert+i, ");", 3);
16097   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
16098   if( rc ){
16099     utf8_printf(stderr, "Error %d: %s on [%s]\n",
16100             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
16101             zQuery);
16102     goto end_data_xfer;
16103   }
16104   for(k=0; k<2; k++){
16105     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16106       for(i=0; i<n; i++){
16107         switch( sqlite3_column_type(pQuery, i) ){
16108           case SQLITE_NULL: {
16109             sqlite3_bind_null(pInsert, i+1);
16110             break;
16111           }
16112           case SQLITE_INTEGER: {
16113             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
16114             break;
16115           }
16116           case SQLITE_FLOAT: {
16117             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
16118             break;
16119           }
16120           case SQLITE_TEXT: {
16121             sqlite3_bind_text(pInsert, i+1,
16122                              (const char*)sqlite3_column_text(pQuery,i),
16123                              -1, SQLITE_STATIC);
16124             break;
16125           }
16126           case SQLITE_BLOB: {
16127             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
16128                                             sqlite3_column_bytes(pQuery,i),
16129                                             SQLITE_STATIC);
16130             break;
16131           }
16132         }
16133       } /* End for */
16134       rc = sqlite3_step(pInsert);
16135       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
16136         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
16137                         sqlite3_errmsg(newDb));
16138       }
16139       sqlite3_reset(pInsert);
16140       cnt++;
16141       if( (cnt%spinRate)==0 ){
16142         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
16143         fflush(stdout);
16144       }
16145     } /* End while */
16146     if( rc==SQLITE_DONE ) break;
16147     sqlite3_finalize(pQuery);
16148     sqlite3_free(zQuery);
16149     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
16150                              zTable);
16151     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16152     if( rc ){
16153       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
16154       break;
16155     }
16156   } /* End for(k=0...) */
16157
16158 end_data_xfer:
16159   sqlite3_finalize(pQuery);
16160   sqlite3_finalize(pInsert);
16161   sqlite3_free(zQuery);
16162   sqlite3_free(zInsert);
16163 }
16164
16165
16166 /*
16167 ** Try to transfer all rows of the schema that match zWhere.  For
16168 ** each row, invoke xForEach() on the object defined by that row.
16169 ** If an error is encountered while moving forward through the
16170 ** sqlite_schema table, try again moving backwards.
16171 */
16172 static void tryToCloneSchema(
16173   ShellState *p,
16174   sqlite3 *newDb,
16175   const char *zWhere,
16176   void (*xForEach)(ShellState*,sqlite3*,const char*)
16177 ){
16178   sqlite3_stmt *pQuery = 0;
16179   char *zQuery = 0;
16180   int rc;
16181   const unsigned char *zName;
16182   const unsigned char *zSql;
16183   char *zErrMsg = 0;
16184
16185   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16186                            " WHERE %s", zWhere);
16187   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16188   if( rc ){
16189     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16190                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16191                     zQuery);
16192     goto end_schema_xfer;
16193   }
16194   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16195     zName = sqlite3_column_text(pQuery, 0);
16196     zSql = sqlite3_column_text(pQuery, 1);
16197     printf("%s... ", zName); fflush(stdout);
16198     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16199     if( zErrMsg ){
16200       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16201       sqlite3_free(zErrMsg);
16202       zErrMsg = 0;
16203     }
16204     if( xForEach ){
16205       xForEach(p, newDb, (const char*)zName);
16206     }
16207     printf("done\n");
16208   }
16209   if( rc!=SQLITE_DONE ){
16210     sqlite3_finalize(pQuery);
16211     sqlite3_free(zQuery);
16212     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16213                              " WHERE %s ORDER BY rowid DESC", zWhere);
16214     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16215     if( rc ){
16216       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16217                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16218                       zQuery);
16219       goto end_schema_xfer;
16220     }
16221     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16222       zName = sqlite3_column_text(pQuery, 0);
16223       zSql = sqlite3_column_text(pQuery, 1);
16224       printf("%s... ", zName); fflush(stdout);
16225       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16226       if( zErrMsg ){
16227         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16228         sqlite3_free(zErrMsg);
16229         zErrMsg = 0;
16230       }
16231       if( xForEach ){
16232         xForEach(p, newDb, (const char*)zName);
16233       }
16234       printf("done\n");
16235     }
16236   }
16237 end_schema_xfer:
16238   sqlite3_finalize(pQuery);
16239   sqlite3_free(zQuery);
16240 }
16241
16242 /*
16243 ** Open a new database file named "zNewDb".  Try to recover as much information
16244 ** as possible out of the main database (which might be corrupt) and write it
16245 ** into zNewDb.
16246 */
16247 static void tryToClone(ShellState *p, const char *zNewDb){
16248   int rc;
16249   sqlite3 *newDb = 0;
16250   if( access(zNewDb,0)==0 ){
16251     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
16252     return;
16253   }
16254   rc = sqlite3_open(zNewDb, &newDb);
16255   if( rc ){
16256     utf8_printf(stderr, "Cannot create output database: %s\n",
16257             sqlite3_errmsg(newDb));
16258   }else{
16259     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
16260     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
16261     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
16262     tryToCloneSchema(p, newDb, "type!='table'", 0);
16263     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
16264     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
16265   }
16266   close_db(newDb);
16267 }
16268
16269 /*
16270 ** Change the output file back to stdout.
16271 **
16272 ** If the p->doXdgOpen flag is set, that means the output was being
16273 ** redirected to a temporary file named by p->zTempFile.  In that case,
16274 ** launch start/open/xdg-open on that temporary file.
16275 */
16276 static void output_reset(ShellState *p){
16277   if( p->outfile[0]=='|' ){
16278 #ifndef SQLITE_OMIT_POPEN
16279     pclose(p->out);
16280 #endif
16281   }else{
16282     output_file_close(p->out);
16283 #ifndef SQLITE_NOHAVE_SYSTEM
16284     if( p->doXdgOpen ){
16285       const char *zXdgOpenCmd =
16286 #if defined(_WIN32)
16287       "start";
16288 #elif defined(__APPLE__)
16289       "open";
16290 #else
16291       "xdg-open";
16292 #endif
16293       char *zCmd;
16294       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
16295       if( system(zCmd) ){
16296         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
16297       }else{
16298         /* Give the start/open/xdg-open command some time to get
16299         ** going before we continue, and potential delete the
16300         ** p->zTempFile data file out from under it */
16301         sqlite3_sleep(2000);
16302       }
16303       sqlite3_free(zCmd);
16304       outputModePop(p);
16305       p->doXdgOpen = 0;
16306     }
16307 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
16308   }
16309   p->outfile[0] = 0;
16310   p->out = stdout;
16311 }
16312
16313 /*
16314 ** Run an SQL command and return the single integer result.
16315 */
16316 static int db_int(ShellState *p, const char *zSql){
16317   sqlite3_stmt *pStmt;
16318   int res = 0;
16319   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16320   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
16321     res = sqlite3_column_int(pStmt,0);
16322   }
16323   sqlite3_finalize(pStmt);
16324   return res;
16325 }
16326
16327 /*
16328 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
16329 */
16330 static unsigned int get2byteInt(unsigned char *a){
16331   return (a[0]<<8) + a[1];
16332 }
16333 static unsigned int get4byteInt(unsigned char *a){
16334   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
16335 }
16336
16337 /*
16338 ** Implementation of the ".dbinfo" command.
16339 **
16340 ** Return 1 on error, 2 to exit, and 0 otherwise.
16341 */
16342 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
16343   static const struct { const char *zName; int ofst; } aField[] = {
16344      { "file change counter:",  24  },
16345      { "database page count:",  28  },
16346      { "freelist page count:",  36  },
16347      { "schema cookie:",        40  },
16348      { "schema format:",        44  },
16349      { "default cache size:",   48  },
16350      { "autovacuum top root:",  52  },
16351      { "incremental vacuum:",   64  },
16352      { "text encoding:",        56  },
16353      { "user version:",         60  },
16354      { "application id:",       68  },
16355      { "software version:",     96  },
16356   };
16357   static const struct { const char *zName; const char *zSql; } aQuery[] = {
16358      { "number of tables:",
16359        "SELECT count(*) FROM %s WHERE type='table'" },
16360      { "number of indexes:",
16361        "SELECT count(*) FROM %s WHERE type='index'" },
16362      { "number of triggers:",
16363        "SELECT count(*) FROM %s WHERE type='trigger'" },
16364      { "number of views:",
16365        "SELECT count(*) FROM %s WHERE type='view'" },
16366      { "schema size:",
16367        "SELECT total(length(sql)) FROM %s" },
16368   };
16369   int i, rc;
16370   unsigned iDataVersion;
16371   char *zSchemaTab;
16372   char *zDb = nArg>=2 ? azArg[1] : "main";
16373   sqlite3_stmt *pStmt = 0;
16374   unsigned char aHdr[100];
16375   open_db(p, 0);
16376   if( p->db==0 ) return 1;
16377   rc = sqlite3_prepare_v2(p->db,
16378              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
16379              -1, &pStmt, 0);
16380   if( rc ){
16381     utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
16382     sqlite3_finalize(pStmt);
16383     return 1;
16384   }
16385   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
16386   if( sqlite3_step(pStmt)==SQLITE_ROW
16387    && sqlite3_column_bytes(pStmt,0)>100
16388   ){
16389     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
16390     sqlite3_finalize(pStmt);
16391   }else{
16392     raw_printf(stderr, "unable to read database header\n");
16393     sqlite3_finalize(pStmt);
16394     return 1;
16395   }
16396   i = get2byteInt(aHdr+16);
16397   if( i==1 ) i = 65536;
16398   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
16399   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
16400   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
16401   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
16402   for(i=0; i<ArraySize(aField); i++){
16403     int ofst = aField[i].ofst;
16404     unsigned int val = get4byteInt(aHdr + ofst);
16405     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
16406     switch( ofst ){
16407       case 56: {
16408         if( val==1 ) raw_printf(p->out, " (utf8)");
16409         if( val==2 ) raw_printf(p->out, " (utf16le)");
16410         if( val==3 ) raw_printf(p->out, " (utf16be)");
16411       }
16412     }
16413     raw_printf(p->out, "\n");
16414   }
16415   if( zDb==0 ){
16416     zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
16417   }else if( strcmp(zDb,"temp")==0 ){
16418     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
16419   }else{
16420     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
16421   }
16422   for(i=0; i<ArraySize(aQuery); i++){
16423     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
16424     int val = db_int(p, zSql);
16425     sqlite3_free(zSql);
16426     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
16427   }
16428   sqlite3_free(zSchemaTab);
16429   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
16430   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
16431   return 0;
16432 }
16433
16434 /*
16435 ** Print the current sqlite3_errmsg() value to stderr and return 1.
16436 */
16437 static int shellDatabaseError(sqlite3 *db){
16438   const char *zErr = sqlite3_errmsg(db);
16439   utf8_printf(stderr, "Error: %s\n", zErr);
16440   return 1;
16441 }
16442
16443 /*
16444 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
16445 ** if they match and FALSE (0) if they do not match.
16446 **
16447 ** Globbing rules:
16448 **
16449 **      '*'       Matches any sequence of zero or more characters.
16450 **
16451 **      '?'       Matches exactly one character.
16452 **
16453 **     [...]      Matches one character from the enclosed list of
16454 **                characters.
16455 **
16456 **     [^...]     Matches one character not in the enclosed list.
16457 **
16458 **      '#'       Matches any sequence of one or more digits with an
16459 **                optional + or - sign in front
16460 **
16461 **      ' '       Any span of whitespace matches any other span of
16462 **                whitespace.
16463 **
16464 ** Extra whitespace at the end of z[] is ignored.
16465 */
16466 static int testcase_glob(const char *zGlob, const char *z){
16467   int c, c2;
16468   int invert;
16469   int seen;
16470
16471   while( (c = (*(zGlob++)))!=0 ){
16472     if( IsSpace(c) ){
16473       if( !IsSpace(*z) ) return 0;
16474       while( IsSpace(*zGlob) ) zGlob++;
16475       while( IsSpace(*z) ) z++;
16476     }else if( c=='*' ){
16477       while( (c=(*(zGlob++))) == '*' || c=='?' ){
16478         if( c=='?' && (*(z++))==0 ) return 0;
16479       }
16480       if( c==0 ){
16481         return 1;
16482       }else if( c=='[' ){
16483         while( *z && testcase_glob(zGlob-1,z)==0 ){
16484           z++;
16485         }
16486         return (*z)!=0;
16487       }
16488       while( (c2 = (*(z++)))!=0 ){
16489         while( c2!=c ){
16490           c2 = *(z++);
16491           if( c2==0 ) return 0;
16492         }
16493         if( testcase_glob(zGlob,z) ) return 1;
16494       }
16495       return 0;
16496     }else if( c=='?' ){
16497       if( (*(z++))==0 ) return 0;
16498     }else if( c=='[' ){
16499       int prior_c = 0;
16500       seen = 0;
16501       invert = 0;
16502       c = *(z++);
16503       if( c==0 ) return 0;
16504       c2 = *(zGlob++);
16505       if( c2=='^' ){
16506         invert = 1;
16507         c2 = *(zGlob++);
16508       }
16509       if( c2==']' ){
16510         if( c==']' ) seen = 1;
16511         c2 = *(zGlob++);
16512       }
16513       while( c2 && c2!=']' ){
16514         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
16515           c2 = *(zGlob++);
16516           if( c>=prior_c && c<=c2 ) seen = 1;
16517           prior_c = 0;
16518         }else{
16519           if( c==c2 ){
16520             seen = 1;
16521           }
16522           prior_c = c2;
16523         }
16524         c2 = *(zGlob++);
16525       }
16526       if( c2==0 || (seen ^ invert)==0 ) return 0;
16527     }else if( c=='#' ){
16528       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
16529       if( !IsDigit(z[0]) ) return 0;
16530       z++;
16531       while( IsDigit(z[0]) ){ z++; }
16532     }else{
16533       if( c!=(*(z++)) ) return 0;
16534     }
16535   }
16536   while( IsSpace(*z) ){ z++; }
16537   return *z==0;
16538 }
16539
16540
16541 /*
16542 ** Compare the string as a command-line option with either one or two
16543 ** initial "-" characters.
16544 */
16545 static int optionMatch(const char *zStr, const char *zOpt){
16546   if( zStr[0]!='-' ) return 0;
16547   zStr++;
16548   if( zStr[0]=='-' ) zStr++;
16549   return strcmp(zStr, zOpt)==0;
16550 }
16551
16552 /*
16553 ** Delete a file.
16554 */
16555 int shellDeleteFile(const char *zFilename){
16556   int rc;
16557 #ifdef _WIN32
16558   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
16559   rc = _wunlink(z);
16560   sqlite3_free(z);
16561 #else
16562   rc = unlink(zFilename);
16563 #endif
16564   return rc;
16565 }
16566
16567 /*
16568 ** Try to delete the temporary file (if there is one) and free the
16569 ** memory used to hold the name of the temp file.
16570 */
16571 static void clearTempFile(ShellState *p){
16572   if( p->zTempFile==0 ) return;
16573   if( p->doXdgOpen ) return;
16574   if( shellDeleteFile(p->zTempFile) ) return;
16575   sqlite3_free(p->zTempFile);
16576   p->zTempFile = 0;
16577 }
16578
16579 /*
16580 ** Create a new temp file name with the given suffix.
16581 */
16582 static void newTempFile(ShellState *p, const char *zSuffix){
16583   clearTempFile(p);
16584   sqlite3_free(p->zTempFile);
16585   p->zTempFile = 0;
16586   if( p->db ){
16587     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
16588   }
16589   if( p->zTempFile==0 ){
16590     /* If p->db is an in-memory database then the TEMPFILENAME file-control
16591     ** will not work and we will need to fallback to guessing */
16592     char *zTemp;
16593     sqlite3_uint64 r;
16594     sqlite3_randomness(sizeof(r), &r);
16595     zTemp = getenv("TEMP");
16596     if( zTemp==0 ) zTemp = getenv("TMP");
16597     if( zTemp==0 ){
16598 #ifdef _WIN32
16599       zTemp = "\\tmp";
16600 #else
16601       zTemp = "/tmp";
16602 #endif
16603     }
16604     p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
16605   }else{
16606     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
16607   }
16608   if( p->zTempFile==0 ){
16609     raw_printf(stderr, "out of memory\n");
16610     exit(1);
16611   }
16612 }
16613
16614
16615 /*
16616 ** The implementation of SQL scalar function fkey_collate_clause(), used
16617 ** by the ".lint fkey-indexes" command. This scalar function is always
16618 ** called with four arguments - the parent table name, the parent column name,
16619 ** the child table name and the child column name.
16620 **
16621 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
16622 **
16623 ** If either of the named tables or columns do not exist, this function
16624 ** returns an empty string. An empty string is also returned if both tables
16625 ** and columns exist but have the same default collation sequence. Or,
16626 ** if both exist but the default collation sequences are different, this
16627 ** function returns the string " COLLATE <parent-collation>", where
16628 ** <parent-collation> is the default collation sequence of the parent column.
16629 */
16630 static void shellFkeyCollateClause(
16631   sqlite3_context *pCtx,
16632   int nVal,
16633   sqlite3_value **apVal
16634 ){
16635   sqlite3 *db = sqlite3_context_db_handle(pCtx);
16636   const char *zParent;
16637   const char *zParentCol;
16638   const char *zParentSeq;
16639   const char *zChild;
16640   const char *zChildCol;
16641   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
16642   int rc;
16643
16644   assert( nVal==4 );
16645   zParent = (const char*)sqlite3_value_text(apVal[0]);
16646   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
16647   zChild = (const char*)sqlite3_value_text(apVal[2]);
16648   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
16649
16650   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
16651   rc = sqlite3_table_column_metadata(
16652       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
16653   );
16654   if( rc==SQLITE_OK ){
16655     rc = sqlite3_table_column_metadata(
16656         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
16657     );
16658   }
16659
16660   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
16661     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
16662     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
16663     sqlite3_free(z);
16664   }
16665 }
16666
16667
16668 /*
16669 ** The implementation of dot-command ".lint fkey-indexes".
16670 */
16671 static int lintFkeyIndexes(
16672   ShellState *pState,             /* Current shell tool state */
16673   char **azArg,                   /* Array of arguments passed to dot command */
16674   int nArg                        /* Number of entries in azArg[] */
16675 ){
16676   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
16677   FILE *out = pState->out;        /* Stream to write non-error output to */
16678   int bVerbose = 0;               /* If -verbose is present */
16679   int bGroupByParent = 0;         /* If -groupbyparent is present */
16680   int i;                          /* To iterate through azArg[] */
16681   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
16682   int rc;                         /* Return code */
16683   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
16684
16685   /*
16686   ** This SELECT statement returns one row for each foreign key constraint
16687   ** in the schema of the main database. The column values are:
16688   **
16689   ** 0. The text of an SQL statement similar to:
16690   **
16691   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
16692   **
16693   **    This SELECT is similar to the one that the foreign keys implementation
16694   **    needs to run internally on child tables. If there is an index that can
16695   **    be used to optimize this query, then it can also be used by the FK
16696   **    implementation to optimize DELETE or UPDATE statements on the parent
16697   **    table.
16698   **
16699   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
16700   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
16701   **    contains an index that can be used to optimize the query.
16702   **
16703   ** 2. Human readable text that describes the child table and columns. e.g.
16704   **
16705   **       "child_table(child_key1, child_key2)"
16706   **
16707   ** 3. Human readable text that describes the parent table and columns. e.g.
16708   **
16709   **       "parent_table(parent_key1, parent_key2)"
16710   **
16711   ** 4. A full CREATE INDEX statement for an index that could be used to
16712   **    optimize DELETE or UPDATE statements on the parent table. e.g.
16713   **
16714   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
16715   **
16716   ** 5. The name of the parent table.
16717   **
16718   ** These six values are used by the C logic below to generate the report.
16719   */
16720   const char *zSql =
16721   "SELECT "
16722     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
16723     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
16724     "  || fkey_collate_clause("
16725     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
16726     ", "
16727     "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
16728     "  || group_concat('*=?', ' AND ') || ')'"
16729     ", "
16730     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
16731     ", "
16732     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
16733     ", "
16734     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
16735     "  || ' ON ' || quote(s.name) || '('"
16736     "  || group_concat(quote(f.[from]) ||"
16737     "        fkey_collate_clause("
16738     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
16739     "  || ');'"
16740     ", "
16741     "     f.[table] "
16742     "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
16743     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
16744     "GROUP BY s.name, f.id "
16745     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
16746   ;
16747   const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
16748
16749   for(i=2; i<nArg; i++){
16750     int n = strlen30(azArg[i]);
16751     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
16752       bVerbose = 1;
16753     }
16754     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
16755       bGroupByParent = 1;
16756       zIndent = "    ";
16757     }
16758     else{
16759       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
16760           azArg[0], azArg[1]
16761       );
16762       return SQLITE_ERROR;
16763     }
16764   }
16765
16766   /* Register the fkey_collate_clause() SQL function */
16767   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
16768       0, shellFkeyCollateClause, 0, 0
16769   );
16770
16771
16772   if( rc==SQLITE_OK ){
16773     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
16774   }
16775   if( rc==SQLITE_OK ){
16776     sqlite3_bind_int(pSql, 1, bGroupByParent);
16777   }
16778
16779   if( rc==SQLITE_OK ){
16780     int rc2;
16781     char *zPrev = 0;
16782     while( SQLITE_ROW==sqlite3_step(pSql) ){
16783       int res = -1;
16784       sqlite3_stmt *pExplain = 0;
16785       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
16786       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
16787       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
16788       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
16789       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
16790       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
16791
16792       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
16793       if( rc!=SQLITE_OK ) break;
16794       if( SQLITE_ROW==sqlite3_step(pExplain) ){
16795         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
16796         res = (
16797               0==sqlite3_strglob(zGlob, zPlan)
16798            || 0==sqlite3_strglob(zGlobIPK, zPlan)
16799         );
16800       }
16801       rc = sqlite3_finalize(pExplain);
16802       if( rc!=SQLITE_OK ) break;
16803
16804       if( res<0 ){
16805         raw_printf(stderr, "Error: internal error");
16806         break;
16807       }else{
16808         if( bGroupByParent
16809         && (bVerbose || res==0)
16810         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
16811         ){
16812           raw_printf(out, "-- Parent table %s\n", zParent);
16813           sqlite3_free(zPrev);
16814           zPrev = sqlite3_mprintf("%s", zParent);
16815         }
16816
16817         if( res==0 ){
16818           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
16819         }else if( bVerbose ){
16820           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
16821               zIndent, zFrom, zTarget
16822           );
16823         }
16824       }
16825     }
16826     sqlite3_free(zPrev);
16827
16828     if( rc!=SQLITE_OK ){
16829       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16830     }
16831
16832     rc2 = sqlite3_finalize(pSql);
16833     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
16834       rc = rc2;
16835       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16836     }
16837   }else{
16838     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16839   }
16840
16841   return rc;
16842 }
16843
16844 /*
16845 ** Implementation of ".lint" dot command.
16846 */
16847 static int lintDotCommand(
16848   ShellState *pState,             /* Current shell tool state */
16849   char **azArg,                   /* Array of arguments passed to dot command */
16850   int nArg                        /* Number of entries in azArg[] */
16851 ){
16852   int n;
16853   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
16854   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
16855   return lintFkeyIndexes(pState, azArg, nArg);
16856
16857  usage:
16858   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
16859   raw_printf(stderr, "Where sub-commands are:\n");
16860   raw_printf(stderr, "    fkey-indexes\n");
16861   return SQLITE_ERROR;
16862 }
16863
16864 #if !defined SQLITE_OMIT_VIRTUALTABLE
16865 static void shellPrepare(
16866   sqlite3 *db, 
16867   int *pRc, 
16868   const char *zSql, 
16869   sqlite3_stmt **ppStmt
16870 ){
16871   *ppStmt = 0;
16872   if( *pRc==SQLITE_OK ){
16873     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
16874     if( rc!=SQLITE_OK ){
16875       raw_printf(stderr, "sql error: %s (%d)\n", 
16876           sqlite3_errmsg(db), sqlite3_errcode(db)
16877       );
16878       *pRc = rc;
16879     }
16880   }
16881 }
16882
16883 /*
16884 ** Create a prepared statement using printf-style arguments for the SQL.
16885 **
16886 ** This routine is could be marked "static".  But it is not always used,
16887 ** depending on compile-time options.  By omitting the "static", we avoid
16888 ** nuisance compiler warnings about "defined but not used".
16889 */
16890 void shellPreparePrintf(
16891   sqlite3 *db, 
16892   int *pRc, 
16893   sqlite3_stmt **ppStmt,
16894   const char *zFmt, 
16895   ...
16896 ){
16897   *ppStmt = 0;
16898   if( *pRc==SQLITE_OK ){
16899     va_list ap;
16900     char *z;
16901     va_start(ap, zFmt);
16902     z = sqlite3_vmprintf(zFmt, ap);
16903     va_end(ap);
16904     if( z==0 ){
16905       *pRc = SQLITE_NOMEM;
16906     }else{
16907       shellPrepare(db, pRc, z, ppStmt);
16908       sqlite3_free(z);
16909     }
16910   }
16911 }
16912
16913 /* Finalize the prepared statement created using shellPreparePrintf().
16914 **
16915 ** This routine is could be marked "static".  But it is not always used,
16916 ** depending on compile-time options.  By omitting the "static", we avoid
16917 ** nuisance compiler warnings about "defined but not used".
16918 */
16919 void shellFinalize(
16920   int *pRc, 
16921   sqlite3_stmt *pStmt
16922 ){
16923   if( pStmt ){
16924     sqlite3 *db = sqlite3_db_handle(pStmt);
16925     int rc = sqlite3_finalize(pStmt);
16926     if( *pRc==SQLITE_OK ){
16927       if( rc!=SQLITE_OK ){
16928         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16929       }
16930       *pRc = rc;
16931     }
16932   }
16933 }
16934
16935 /* Reset the prepared statement created using shellPreparePrintf().
16936 **
16937 ** This routine is could be marked "static".  But it is not always used,
16938 ** depending on compile-time options.  By omitting the "static", we avoid
16939 ** nuisance compiler warnings about "defined but not used".
16940 */
16941 void shellReset(
16942   int *pRc, 
16943   sqlite3_stmt *pStmt
16944 ){
16945   int rc = sqlite3_reset(pStmt);
16946   if( *pRc==SQLITE_OK ){
16947     if( rc!=SQLITE_OK ){
16948       sqlite3 *db = sqlite3_db_handle(pStmt);
16949       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16950     }
16951     *pRc = rc;
16952   }
16953 }
16954 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
16955
16956 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16957 /******************************************************************************
16958 ** The ".archive" or ".ar" command.
16959 */
16960 /*
16961 ** Structure representing a single ".ar" command.
16962 */
16963 typedef struct ArCommand ArCommand;
16964 struct ArCommand {
16965   u8 eCmd;                        /* An AR_CMD_* value */
16966   u8 bVerbose;                    /* True if --verbose */
16967   u8 bZip;                        /* True if the archive is a ZIP */
16968   u8 bDryRun;                     /* True if --dry-run */
16969   u8 bAppend;                     /* True if --append */
16970   u8 fromCmdLine;                 /* Run from -A instead of .archive */
16971   int nArg;                       /* Number of command arguments */
16972   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
16973   const char *zFile;              /* --file argument, or NULL */
16974   const char *zDir;               /* --directory argument, or NULL */
16975   char **azArg;                   /* Array of command arguments */
16976   ShellState *p;                  /* Shell state */
16977   sqlite3 *db;                    /* Database containing the archive */
16978 };
16979
16980 /*
16981 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
16982 */
16983 static int arUsage(FILE *f){
16984   showHelp(f,"archive");
16985   return SQLITE_ERROR;
16986 }
16987
16988 /*
16989 ** Print an error message for the .ar command to stderr and return 
16990 ** SQLITE_ERROR.
16991 */
16992 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
16993   va_list ap;
16994   char *z;
16995   va_start(ap, zFmt);
16996   z = sqlite3_vmprintf(zFmt, ap);
16997   va_end(ap);
16998   utf8_printf(stderr, "Error: %s\n", z);
16999   if( pAr->fromCmdLine ){
17000     utf8_printf(stderr, "Use \"-A\" for more help\n");
17001   }else{
17002     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
17003   }
17004   sqlite3_free(z);
17005   return SQLITE_ERROR;
17006 }
17007
17008 /*
17009 ** Values for ArCommand.eCmd.
17010 */
17011 #define AR_CMD_CREATE       1
17012 #define AR_CMD_UPDATE       2
17013 #define AR_CMD_INSERT       3
17014 #define AR_CMD_EXTRACT      4
17015 #define AR_CMD_LIST         5
17016 #define AR_CMD_HELP         6
17017
17018 /*
17019 ** Other (non-command) switches.
17020 */
17021 #define AR_SWITCH_VERBOSE     7
17022 #define AR_SWITCH_FILE        8
17023 #define AR_SWITCH_DIRECTORY   9
17024 #define AR_SWITCH_APPEND     10
17025 #define AR_SWITCH_DRYRUN     11
17026
17027 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
17028   switch( eSwitch ){
17029     case AR_CMD_CREATE:
17030     case AR_CMD_EXTRACT:
17031     case AR_CMD_LIST:
17032     case AR_CMD_UPDATE:
17033     case AR_CMD_INSERT:
17034     case AR_CMD_HELP:
17035       if( pAr->eCmd ){
17036         return arErrorMsg(pAr, "multiple command options");
17037       }
17038       pAr->eCmd = eSwitch;
17039       break;
17040
17041     case AR_SWITCH_DRYRUN:
17042       pAr->bDryRun = 1;
17043       break;
17044     case AR_SWITCH_VERBOSE:
17045       pAr->bVerbose = 1;
17046       break;
17047     case AR_SWITCH_APPEND:
17048       pAr->bAppend = 1;
17049       /* Fall thru into --file */
17050     case AR_SWITCH_FILE:
17051       pAr->zFile = zArg;
17052       break;
17053     case AR_SWITCH_DIRECTORY:
17054       pAr->zDir = zArg;
17055       break;
17056   }
17057
17058   return SQLITE_OK;
17059 }
17060
17061 /*
17062 ** Parse the command line for an ".ar" command. The results are written into
17063 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
17064 ** successfully, otherwise an error message is written to stderr and 
17065 ** SQLITE_ERROR returned.
17066 */
17067 static int arParseCommand(
17068   char **azArg,                   /* Array of arguments passed to dot command */
17069   int nArg,                       /* Number of entries in azArg[] */
17070   ArCommand *pAr                  /* Populate this object */
17071 ){
17072   struct ArSwitch {
17073     const char *zLong;
17074     char cShort;
17075     u8 eSwitch;
17076     u8 bArg;
17077   } aSwitch[] = {
17078     { "create",    'c', AR_CMD_CREATE,       0 },
17079     { "extract",   'x', AR_CMD_EXTRACT,      0 },
17080     { "insert",    'i', AR_CMD_INSERT,       0 },
17081     { "list",      't', AR_CMD_LIST,         0 },
17082     { "update",    'u', AR_CMD_UPDATE,       0 },
17083     { "help",      'h', AR_CMD_HELP,         0 },
17084     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
17085     { "file",      'f', AR_SWITCH_FILE,      1 },
17086     { "append",    'a', AR_SWITCH_APPEND,    1 },
17087     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
17088     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
17089   };
17090   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
17091   struct ArSwitch *pEnd = &aSwitch[nSwitch];
17092
17093   if( nArg<=1 ){
17094     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
17095     return arUsage(stderr);
17096   }else{
17097     char *z = azArg[1];
17098     if( z[0]!='-' ){
17099       /* Traditional style [tar] invocation */
17100       int i;
17101       int iArg = 2;
17102       for(i=0; z[i]; i++){
17103         const char *zArg = 0;
17104         struct ArSwitch *pOpt;
17105         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17106           if( z[i]==pOpt->cShort ) break;
17107         }
17108         if( pOpt==pEnd ){
17109           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17110         }
17111         if( pOpt->bArg ){
17112           if( iArg>=nArg ){
17113             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
17114           }
17115           zArg = azArg[iArg++];
17116         }
17117         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17118       }
17119       pAr->nArg = nArg-iArg;
17120       if( pAr->nArg>0 ){
17121         pAr->azArg = &azArg[iArg];
17122       }
17123     }else{
17124       /* Non-traditional invocation */
17125       int iArg;
17126       for(iArg=1; iArg<nArg; iArg++){
17127         int n;
17128         z = azArg[iArg];
17129         if( z[0]!='-' ){
17130           /* All remaining command line words are command arguments. */
17131           pAr->azArg = &azArg[iArg];
17132           pAr->nArg = nArg-iArg;
17133           break;
17134         }
17135         n = strlen30(z);
17136
17137         if( z[1]!='-' ){
17138           int i;
17139           /* One or more short options */
17140           for(i=1; i<n; i++){
17141             const char *zArg = 0;
17142             struct ArSwitch *pOpt;
17143             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17144               if( z[i]==pOpt->cShort ) break;
17145             }
17146             if( pOpt==pEnd ){
17147               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17148             }
17149             if( pOpt->bArg ){
17150               if( i<(n-1) ){
17151                 zArg = &z[i+1];
17152                 i = n;
17153               }else{
17154                 if( iArg>=(nArg-1) ){
17155                   return arErrorMsg(pAr, "option requires an argument: %c",
17156                                     z[i]);
17157                 }
17158                 zArg = azArg[++iArg];
17159               }
17160             }
17161             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17162           }
17163         }else if( z[2]=='\0' ){
17164           /* A -- option, indicating that all remaining command line words
17165           ** are command arguments.  */
17166           pAr->azArg = &azArg[iArg+1];
17167           pAr->nArg = nArg-iArg-1;
17168           break;
17169         }else{
17170           /* A long option */
17171           const char *zArg = 0;             /* Argument for option, if any */
17172           struct ArSwitch *pMatch = 0;      /* Matching option */
17173           struct ArSwitch *pOpt;            /* Iterator */
17174           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17175             const char *zLong = pOpt->zLong;
17176             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
17177               if( pMatch ){
17178                 return arErrorMsg(pAr, "ambiguous option: %s",z);
17179               }else{
17180                 pMatch = pOpt;
17181               }
17182             }
17183           }
17184
17185           if( pMatch==0 ){
17186             return arErrorMsg(pAr, "unrecognized option: %s", z);
17187           }
17188           if( pMatch->bArg ){
17189             if( iArg>=(nArg-1) ){
17190               return arErrorMsg(pAr, "option requires an argument: %s", z);
17191             }
17192             zArg = azArg[++iArg];
17193           }
17194           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
17195         }
17196       }
17197     }
17198   }
17199
17200   return SQLITE_OK;
17201 }
17202
17203 /*
17204 ** This function assumes that all arguments within the ArCommand.azArg[]
17205 ** array refer to archive members, as for the --extract or --list commands. 
17206 ** It checks that each of them are present. If any specified file is not
17207 ** present in the archive, an error is printed to stderr and an error
17208 ** code returned. Otherwise, if all specified arguments are present in
17209 ** the archive, SQLITE_OK is returned.
17210 **
17211 ** This function strips any trailing '/' characters from each argument.
17212 ** This is consistent with the way the [tar] command seems to work on
17213 ** Linux.
17214 */
17215 static int arCheckEntries(ArCommand *pAr){
17216   int rc = SQLITE_OK;
17217   if( pAr->nArg ){
17218     int i, j;
17219     sqlite3_stmt *pTest = 0;
17220
17221     shellPreparePrintf(pAr->db, &rc, &pTest,
17222         "SELECT name FROM %s WHERE name=$name", 
17223         pAr->zSrcTable
17224     );
17225     j = sqlite3_bind_parameter_index(pTest, "$name");
17226     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17227       char *z = pAr->azArg[i];
17228       int n = strlen30(z);
17229       int bOk = 0;
17230       while( n>0 && z[n-1]=='/' ) n--;
17231       z[n] = '\0';
17232       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
17233       if( SQLITE_ROW==sqlite3_step(pTest) ){
17234         bOk = 1;
17235       }
17236       shellReset(&rc, pTest);
17237       if( rc==SQLITE_OK && bOk==0 ){
17238         utf8_printf(stderr, "not found in archive: %s\n", z);
17239         rc = SQLITE_ERROR;
17240       }
17241     }
17242     shellFinalize(&rc, pTest);
17243   }
17244   return rc;
17245 }
17246
17247 /*
17248 ** Format a WHERE clause that can be used against the "sqlar" table to
17249 ** identify all archive members that match the command arguments held
17250 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
17251 ** The caller is responsible for eventually calling sqlite3_free() on
17252 ** any non-NULL (*pzWhere) value.
17253 */
17254 static void arWhereClause(
17255   int *pRc, 
17256   ArCommand *pAr, 
17257   char **pzWhere                  /* OUT: New WHERE clause */
17258 ){
17259   char *zWhere = 0;
17260   if( *pRc==SQLITE_OK ){
17261     if( pAr->nArg==0 ){
17262       zWhere = sqlite3_mprintf("1");
17263     }else{
17264       int i;
17265       const char *zSep = "";
17266       for(i=0; i<pAr->nArg; i++){
17267         const char *z = pAr->azArg[i];
17268         zWhere = sqlite3_mprintf(
17269           "%z%s name = '%q' OR substr(name,1,%d) = '%q/'", 
17270           zWhere, zSep, z, strlen30(z)+1, z
17271         );
17272         if( zWhere==0 ){
17273           *pRc = SQLITE_NOMEM;
17274           break;
17275         }
17276         zSep = " OR ";
17277       }
17278     }
17279   }
17280   *pzWhere = zWhere;
17281 }
17282
17283 /*
17284 ** Implementation of .ar "lisT" command. 
17285 */
17286 static int arListCommand(ArCommand *pAr){
17287   const char *zSql = "SELECT %s FROM %s WHERE %s"; 
17288   const char *azCols[] = {
17289     "name",
17290     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
17291   };
17292
17293   char *zWhere = 0;
17294   sqlite3_stmt *pSql = 0;
17295   int rc;
17296
17297   rc = arCheckEntries(pAr);
17298   arWhereClause(&rc, pAr, &zWhere);
17299
17300   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
17301                      pAr->zSrcTable, zWhere);
17302   if( pAr->bDryRun ){
17303     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17304   }else{
17305     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17306       if( pAr->bVerbose ){
17307         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
17308             sqlite3_column_text(pSql, 0),
17309             sqlite3_column_int(pSql, 1), 
17310             sqlite3_column_text(pSql, 2),
17311             sqlite3_column_text(pSql, 3)
17312         );
17313       }else{
17314         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17315       }
17316     }
17317   }
17318   shellFinalize(&rc, pSql);
17319   sqlite3_free(zWhere);
17320   return rc;
17321 }
17322
17323
17324 /*
17325 ** Implementation of .ar "eXtract" command. 
17326 */
17327 static int arExtractCommand(ArCommand *pAr){
17328   const char *zSql1 = 
17329     "SELECT "
17330     " ($dir || name),"
17331     " writefile(($dir || name), %s, mode, mtime) "
17332     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
17333     " AND name NOT GLOB '*..[/\\]*'";
17334
17335   const char *azExtraArg[] = { 
17336     "sqlar_uncompress(data, sz)",
17337     "data"
17338   };
17339
17340   sqlite3_stmt *pSql = 0;
17341   int rc = SQLITE_OK;
17342   char *zDir = 0;
17343   char *zWhere = 0;
17344   int i, j;
17345
17346   /* If arguments are specified, check that they actually exist within
17347   ** the archive before proceeding. And formulate a WHERE clause to
17348   ** match them.  */
17349   rc = arCheckEntries(pAr);
17350   arWhereClause(&rc, pAr, &zWhere);
17351
17352   if( rc==SQLITE_OK ){
17353     if( pAr->zDir ){
17354       zDir = sqlite3_mprintf("%s/", pAr->zDir);
17355     }else{
17356       zDir = sqlite3_mprintf("");
17357     }
17358     if( zDir==0 ) rc = SQLITE_NOMEM;
17359   }
17360
17361   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 
17362       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
17363   );
17364
17365   if( rc==SQLITE_OK ){
17366     j = sqlite3_bind_parameter_index(pSql, "$dir");
17367     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
17368
17369     /* Run the SELECT statement twice. The first time, writefile() is called
17370     ** for all archive members that should be extracted. The second time,
17371     ** only for the directories. This is because the timestamps for
17372     ** extracted directories must be reset after they are populated (as
17373     ** populating them changes the timestamp).  */
17374     for(i=0; i<2; i++){
17375       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
17376       sqlite3_bind_int(pSql, j, i);
17377       if( pAr->bDryRun ){
17378         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17379       }else{
17380         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17381           if( i==0 && pAr->bVerbose ){
17382             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17383           }
17384         }
17385       }
17386       shellReset(&rc, pSql);
17387     }
17388     shellFinalize(&rc, pSql);
17389   }
17390
17391   sqlite3_free(zDir);
17392   sqlite3_free(zWhere);
17393   return rc;
17394 }
17395
17396 /*
17397 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
17398 */
17399 static int arExecSql(ArCommand *pAr, const char *zSql){
17400   int rc;
17401   if( pAr->bDryRun ){
17402     utf8_printf(pAr->p->out, "%s\n", zSql);
17403     rc = SQLITE_OK;
17404   }else{
17405     char *zErr = 0;
17406     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17407     if( zErr ){
17408       utf8_printf(stdout, "ERROR: %s\n", zErr);
17409       sqlite3_free(zErr);
17410     }
17411   }
17412   return rc;
17413 }
17414
17415
17416 /*
17417 ** Implementation of .ar "create", "insert", and "update" commands.
17418 **
17419 **     create    ->     Create a new SQL archive
17420 **     insert    ->     Insert or reinsert all files listed
17421 **     update    ->     Insert files that have changed or that were not
17422 **                      previously in the archive
17423 **
17424 ** Create the "sqlar" table in the database if it does not already exist.
17425 ** Then add each file in the azFile[] array to the archive. Directories
17426 ** are added recursively. If argument bVerbose is non-zero, a message is
17427 ** printed on stdout for each file archived.
17428 **
17429 ** The create command is the same as update, except that it drops
17430 ** any existing "sqlar" table before beginning.  The "insert" command
17431 ** always overwrites every file named on the command-line, where as
17432 ** "update" only overwrites if the size or mtime or mode has changed.
17433 */
17434 static int arCreateOrUpdateCommand(
17435   ArCommand *pAr,                 /* Command arguments and options */
17436   int bUpdate,                    /* true for a --create. */
17437   int bOnlyIfChanged              /* Only update if file has changed */
17438 ){
17439   const char *zCreate = 
17440       "CREATE TABLE IF NOT EXISTS sqlar(\n"
17441       "  name TEXT PRIMARY KEY,  -- name of the file\n"
17442       "  mode INT,               -- access permissions\n"
17443       "  mtime INT,              -- last modification time\n"
17444       "  sz INT,                 -- original file size\n"
17445       "  data BLOB               -- compressed content\n"
17446       ")";
17447   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
17448   const char *zInsertFmt[2] = {
17449      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
17450      "  SELECT\n"
17451      "    %s,\n"
17452      "    mode,\n"
17453      "    mtime,\n"
17454      "    CASE substr(lsmode(mode),1,1)\n"
17455      "      WHEN '-' THEN length(data)\n"
17456      "      WHEN 'd' THEN 0\n"
17457      "      ELSE -1 END,\n"
17458      "    sqlar_compress(data)\n"
17459      "  FROM fsdir(%Q,%Q) AS disk\n"
17460      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17461      ,
17462      "REPLACE INTO %s(name,mode,mtime,data)\n"
17463      "  SELECT\n"
17464      "    %s,\n"
17465      "    mode,\n"
17466      "    mtime,\n"
17467      "    data\n"
17468      "  FROM fsdir(%Q,%Q) AS disk\n"
17469      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17470   };
17471   int i;                          /* For iterating through azFile[] */
17472   int rc;                         /* Return code */
17473   const char *zTab = 0;           /* SQL table into which to insert */
17474   char *zSql;
17475   char zTemp[50];
17476   char *zExists = 0;
17477
17478   arExecSql(pAr, "PRAGMA page_size=512");
17479   rc = arExecSql(pAr, "SAVEPOINT ar;");
17480   if( rc!=SQLITE_OK ) return rc;
17481   zTemp[0] = 0; 
17482   if( pAr->bZip ){
17483     /* Initialize the zipfile virtual table, if necessary */
17484     if( pAr->zFile ){
17485       sqlite3_uint64 r;
17486       sqlite3_randomness(sizeof(r),&r);
17487       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
17488       zTab = zTemp;
17489       zSql = sqlite3_mprintf(
17490          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
17491          zTab, pAr->zFile
17492       );
17493       rc = arExecSql(pAr, zSql);
17494       sqlite3_free(zSql);
17495     }else{
17496       zTab = "zip";
17497     }
17498   }else{
17499     /* Initialize the table for an SQLAR */
17500     zTab = "sqlar";
17501     if( bUpdate==0 ){
17502       rc = arExecSql(pAr, zDrop);
17503       if( rc!=SQLITE_OK ) goto end_ar_transaction;
17504     }
17505     rc = arExecSql(pAr, zCreate);
17506   }
17507   if( bOnlyIfChanged ){
17508     zExists = sqlite3_mprintf(
17509       " AND NOT EXISTS("
17510           "SELECT 1 FROM %s AS mem"
17511           " WHERE mem.name=disk.name"
17512           " AND mem.mtime=disk.mtime"
17513           " AND mem.mode=disk.mode)", zTab);
17514   }else{
17515     zExists = sqlite3_mprintf("");
17516   }
17517   if( zExists==0 ) rc = SQLITE_NOMEM;
17518   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17519     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
17520         pAr->bVerbose ? "shell_putsnl(name)" : "name",
17521         pAr->azArg[i], pAr->zDir, zExists);
17522     rc = arExecSql(pAr, zSql2);
17523     sqlite3_free(zSql2);
17524   }
17525 end_ar_transaction:
17526   if( rc!=SQLITE_OK ){
17527     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
17528   }else{
17529     rc = arExecSql(pAr, "RELEASE ar;");
17530     if( pAr->bZip && pAr->zFile ){
17531       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
17532       arExecSql(pAr, zSql);
17533       sqlite3_free(zSql);
17534     }
17535   }
17536   sqlite3_free(zExists);
17537   return rc;
17538 }
17539
17540 /*
17541 ** Implementation of ".ar" dot command.
17542 */
17543 static int arDotCommand(
17544   ShellState *pState,          /* Current shell tool state */
17545   int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
17546   char **azArg,                /* Array of arguments passed to dot command */
17547   int nArg                     /* Number of entries in azArg[] */
17548 ){
17549   ArCommand cmd;
17550   int rc;
17551   memset(&cmd, 0, sizeof(cmd));
17552   cmd.fromCmdLine = fromCmdLine;
17553   rc = arParseCommand(azArg, nArg, &cmd);
17554   if( rc==SQLITE_OK ){
17555     int eDbType = SHELL_OPEN_UNSPEC;
17556     cmd.p = pState;
17557     cmd.db = pState->db;
17558     if( cmd.zFile ){
17559       eDbType = deduceDatabaseType(cmd.zFile, 1);
17560     }else{
17561       eDbType = pState->openMode;
17562     }
17563     if( eDbType==SHELL_OPEN_ZIPFILE ){
17564       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
17565         if( cmd.zFile==0 ){
17566           cmd.zSrcTable = sqlite3_mprintf("zip");
17567         }else{
17568           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
17569         }
17570       }
17571       cmd.bZip = 1;
17572     }else if( cmd.zFile ){
17573       int flags;
17574       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
17575       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 
17576            || cmd.eCmd==AR_CMD_UPDATE ){
17577         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
17578       }else{
17579         flags = SQLITE_OPEN_READONLY;
17580       }
17581       cmd.db = 0;
17582       if( cmd.bDryRun ){
17583         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
17584              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
17585       }
17586       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 
17587              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
17588       if( rc!=SQLITE_OK ){
17589         utf8_printf(stderr, "cannot open file: %s (%s)\n", 
17590             cmd.zFile, sqlite3_errmsg(cmd.db)
17591         );
17592         goto end_ar_command;
17593       }
17594       sqlite3_fileio_init(cmd.db, 0, 0);
17595       sqlite3_sqlar_init(cmd.db, 0, 0);
17596       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
17597                               shellPutsFunc, 0, 0);
17598
17599     }
17600     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
17601       if( cmd.eCmd!=AR_CMD_CREATE
17602        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
17603       ){
17604         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
17605         rc = SQLITE_ERROR;
17606         goto end_ar_command;
17607       }
17608       cmd.zSrcTable = sqlite3_mprintf("sqlar");
17609     }
17610
17611     switch( cmd.eCmd ){
17612       case AR_CMD_CREATE:
17613         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
17614         break;
17615
17616       case AR_CMD_EXTRACT:
17617         rc = arExtractCommand(&cmd);
17618         break;
17619
17620       case AR_CMD_LIST:
17621         rc = arListCommand(&cmd);
17622         break;
17623
17624       case AR_CMD_HELP:
17625         arUsage(pState->out);
17626         break;
17627
17628       case AR_CMD_INSERT:
17629         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
17630         break;
17631
17632       default:
17633         assert( cmd.eCmd==AR_CMD_UPDATE );
17634         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
17635         break;
17636     }
17637   }
17638 end_ar_command:
17639   if( cmd.db!=pState->db ){
17640     close_db(cmd.db);
17641   }
17642   sqlite3_free(cmd.zSrcTable);
17643
17644   return rc;
17645 }
17646 /* End of the ".archive" or ".ar" command logic
17647 *******************************************************************************/
17648 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
17649
17650 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17651 /*
17652 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
17653 ** Otherwise, the SQL statement or statements in zSql are executed using
17654 ** database connection db and the error code written to *pRc before
17655 ** this function returns.
17656 */
17657 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
17658   int rc = *pRc;
17659   if( rc==SQLITE_OK ){
17660     char *zErr = 0;
17661     rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
17662     if( rc!=SQLITE_OK ){
17663       raw_printf(stderr, "SQL error: %s\n", zErr);
17664     }
17665     sqlite3_free(zErr);
17666     *pRc = rc;
17667   }
17668 }
17669
17670 /*
17671 ** Like shellExec(), except that zFmt is a printf() style format string.
17672 */
17673 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
17674   char *z = 0;
17675   if( *pRc==SQLITE_OK ){
17676     va_list ap;
17677     va_start(ap, zFmt);
17678     z = sqlite3_vmprintf(zFmt, ap);
17679     va_end(ap);
17680     if( z==0 ){
17681       *pRc = SQLITE_NOMEM;
17682     }else{
17683       shellExec(db, pRc, z);
17684     }
17685     sqlite3_free(z);
17686   }
17687 }
17688
17689 /*
17690 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17691 ** Otherwise, an attempt is made to allocate, zero and return a pointer
17692 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
17693 ** to SQLITE_NOMEM and NULL returned.
17694 */
17695 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
17696   void *pRet = 0;
17697   if( *pRc==SQLITE_OK ){
17698     pRet = sqlite3_malloc64(nByte);
17699     if( pRet==0 ){
17700       *pRc = SQLITE_NOMEM;
17701     }else{
17702       memset(pRet, 0, nByte);
17703     }
17704   }
17705   return pRet;
17706 }
17707
17708 /*
17709 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17710 ** Otherwise, zFmt is treated as a printf() style string. The result of
17711 ** formatting it along with any trailing arguments is written into a 
17712 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
17713 ** It is the responsibility of the caller to eventually free this buffer
17714 ** using a call to sqlite3_free().
17715 ** 
17716 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL 
17717 ** pointer returned.
17718 */
17719 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
17720   char *z = 0;
17721   if( *pRc==SQLITE_OK ){
17722     va_list ap;
17723     va_start(ap, zFmt);
17724     z = sqlite3_vmprintf(zFmt, ap);
17725     va_end(ap);
17726     if( z==0 ){
17727       *pRc = SQLITE_NOMEM;
17728     }
17729   }
17730   return z;
17731 }
17732
17733 /*
17734 ** When running the ".recover" command, each output table, and the special
17735 ** orphaned row table if it is required, is represented by an instance
17736 ** of the following struct.
17737 */
17738 typedef struct RecoverTable RecoverTable;
17739 struct RecoverTable {
17740   char *zQuoted;                  /* Quoted version of table name */
17741   int nCol;                       /* Number of columns in table */
17742   char **azlCol;                  /* Array of column lists */
17743   int iPk;                        /* Index of IPK column */
17744 };
17745
17746 /*
17747 ** Free a RecoverTable object allocated by recoverFindTable() or
17748 ** recoverOrphanTable().
17749 */
17750 static void recoverFreeTable(RecoverTable *pTab){
17751   if( pTab ){
17752     sqlite3_free(pTab->zQuoted);
17753     if( pTab->azlCol ){
17754       int i;
17755       for(i=0; i<=pTab->nCol; i++){
17756         sqlite3_free(pTab->azlCol[i]);
17757       }
17758       sqlite3_free(pTab->azlCol);
17759     }
17760     sqlite3_free(pTab);
17761   }
17762 }
17763
17764 /*
17765 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
17766 ** Otherwise, it allocates and returns a RecoverTable object based on the
17767 ** final four arguments passed to this function. It is the responsibility
17768 ** of the caller to eventually free the returned object using
17769 ** recoverFreeTable().
17770 */
17771 static RecoverTable *recoverNewTable(
17772   int *pRc,                       /* IN/OUT: Error code */
17773   const char *zName,              /* Name of table */
17774   const char *zSql,               /* CREATE TABLE statement */
17775   int bIntkey, 
17776   int nCol
17777 ){
17778   sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
17779   int rc = *pRc;
17780   RecoverTable *pTab = 0;
17781
17782   pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
17783   if( rc==SQLITE_OK ){
17784     int nSqlCol = 0;
17785     int bSqlIntkey = 0;
17786     sqlite3_stmt *pStmt = 0;
17787     
17788     rc = sqlite3_open("", &dbtmp);
17789     if( rc==SQLITE_OK ){
17790       sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
17791                               shellIdQuote, 0, 0);
17792     }
17793     if( rc==SQLITE_OK ){
17794       rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
17795     }
17796     if( rc==SQLITE_OK ){
17797       rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
17798       if( rc==SQLITE_ERROR ){
17799         rc = SQLITE_OK;
17800         goto finished;
17801       }
17802     }
17803     shellPreparePrintf(dbtmp, &rc, &pStmt, 
17804         "SELECT count(*) FROM pragma_table_info(%Q)", zName
17805     );
17806     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17807       nSqlCol = sqlite3_column_int(pStmt, 0);
17808     }
17809     shellFinalize(&rc, pStmt);
17810
17811     if( rc!=SQLITE_OK || nSqlCol<nCol ){
17812       goto finished;
17813     }
17814
17815     shellPreparePrintf(dbtmp, &rc, &pStmt, 
17816       "SELECT ("
17817       "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
17818       ") FROM sqlite_schema WHERE name = %Q", zName
17819     );
17820     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17821       bSqlIntkey = sqlite3_column_int(pStmt, 0);
17822     }
17823     shellFinalize(&rc, pStmt);
17824
17825     if( bIntkey==bSqlIntkey ){
17826       int i;
17827       const char *zPk = "_rowid_";
17828       sqlite3_stmt *pPkFinder = 0;
17829
17830       /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
17831       ** set zPk to the name of the PK column, and pTab->iPk to the index
17832       ** of the column, where columns are 0-numbered from left to right.
17833       ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
17834       ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
17835       pTab->iPk = -2;
17836       if( bIntkey ){
17837         shellPreparePrintf(dbtmp, &rc, &pPkFinder, 
17838           "SELECT cid, name FROM pragma_table_info(%Q) "
17839           "  WHERE pk=1 AND type='integer' COLLATE nocase"
17840           "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
17841           , zName, zName
17842         );
17843         if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
17844           pTab->iPk = sqlite3_column_int(pPkFinder, 0);
17845           zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
17846         }
17847       }
17848
17849       pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
17850       pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
17851       pTab->nCol = nSqlCol;
17852
17853       if( bIntkey ){
17854         pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
17855       }else{
17856         pTab->azlCol[0] = shellMPrintf(&rc, "");
17857       }
17858       i = 1;
17859       shellPreparePrintf(dbtmp, &rc, &pStmt, 
17860           "SELECT %Q || group_concat(shell_idquote(name), ', ') "
17861           "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
17862           "FROM pragma_table_info(%Q)", 
17863           bIntkey ? ", " : "", pTab->iPk, 
17864           bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
17865           zName
17866       );
17867       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17868         const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
17869         pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
17870         i++;
17871       }
17872       shellFinalize(&rc, pStmt);
17873
17874       shellFinalize(&rc, pPkFinder);
17875     }
17876   }
17877
17878  finished:
17879   sqlite3_close(dbtmp);
17880   *pRc = rc;
17881   if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
17882     recoverFreeTable(pTab);
17883     pTab = 0;
17884   }
17885   return pTab;
17886 }
17887
17888 /*
17889 ** This function is called to search the schema recovered from the
17890 ** sqlite_schema table of the (possibly) corrupt database as part
17891 ** of a ".recover" command. Specifically, for a table with root page
17892 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
17893 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
17894 ** those.
17895 **
17896 ** If a table is found, a (RecoverTable*) object is returned. Or, if
17897 ** no such table is found, but bIntkey is false and iRoot is the 
17898 ** root page of an index in the recovered schema, then (*pbNoop) is
17899 ** set to true and NULL returned. Or, if there is no such table or
17900 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
17901 ** the caller should write data to the orphans table.
17902 */
17903 static RecoverTable *recoverFindTable(
17904   ShellState *pState,             /* Shell state object */
17905   int *pRc,                       /* IN/OUT: Error code */
17906   int iRoot,                      /* Root page of table */
17907   int bIntkey,                    /* True for an intkey table */
17908   int nCol,                       /* Number of columns in table */
17909   int *pbNoop                     /* OUT: True if iRoot is root of index */
17910 ){
17911   sqlite3_stmt *pStmt = 0;
17912   RecoverTable *pRet = 0;
17913   int bNoop = 0;
17914   const char *zSql = 0;
17915   const char *zName = 0;
17916
17917   /* Search the recovered schema for an object with root page iRoot. */
17918   shellPreparePrintf(pState->db, pRc, &pStmt,
17919       "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
17920   );
17921   while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17922     const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
17923     if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
17924       bNoop = 1;
17925       break;
17926     }
17927     if( sqlite3_stricmp(zType, "table")==0 ){
17928       zName = (const char*)sqlite3_column_text(pStmt, 1);
17929       zSql = (const char*)sqlite3_column_text(pStmt, 2);
17930       pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
17931       break;
17932     }
17933   }
17934
17935   shellFinalize(pRc, pStmt);
17936   *pbNoop = bNoop;
17937   return pRet;
17938 }
17939
17940 /*
17941 ** Return a RecoverTable object representing the orphans table.
17942 */
17943 static RecoverTable *recoverOrphanTable(
17944   ShellState *pState,             /* Shell state object */
17945   int *pRc,                       /* IN/OUT: Error code */
17946   const char *zLostAndFound,      /* Base name for orphans table */
17947   int nCol                        /* Number of user data columns */
17948 ){
17949   RecoverTable *pTab = 0;
17950   if( nCol>=0 && *pRc==SQLITE_OK ){
17951     int i;
17952
17953     /* This block determines the name of the orphan table. The prefered
17954     ** name is zLostAndFound. But if that clashes with another name
17955     ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
17956     ** and so on until a non-clashing name is found.  */
17957     int iTab = 0;
17958     char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
17959     sqlite3_stmt *pTest = 0;
17960     shellPrepare(pState->db, pRc,
17961         "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
17962     );
17963     if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17964     while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
17965       shellReset(pRc, pTest);
17966       sqlite3_free(zTab);
17967       zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
17968       sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17969     }
17970     shellFinalize(pRc, pTest);
17971
17972     pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
17973     if( pTab ){
17974       pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
17975       pTab->nCol = nCol;
17976       pTab->iPk = -2;
17977       if( nCol>0 ){
17978         pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
17979         if( pTab->azlCol ){
17980           pTab->azlCol[nCol] = shellMPrintf(pRc, "");
17981           for(i=nCol-1; i>=0; i--){
17982             pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
17983           }
17984         }
17985       }
17986
17987       if( *pRc!=SQLITE_OK ){
17988         recoverFreeTable(pTab);
17989         pTab = 0;
17990       }else{
17991         raw_printf(pState->out, 
17992             "CREATE TABLE %s(rootpgno INTEGER, "
17993             "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
17994         );
17995         for(i=0; i<nCol; i++){
17996           raw_printf(pState->out, ", c%d", i);
17997         }
17998         raw_printf(pState->out, ");\n");
17999       }
18000     }
18001     sqlite3_free(zTab);
18002   }
18003   return pTab;
18004 }
18005
18006 /*
18007 ** This function is called to recover data from the database. A script
18008 ** to construct a new database containing all recovered data is output
18009 ** on stream pState->out.
18010 */
18011 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
18012   int rc = SQLITE_OK;
18013   sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
18014   sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
18015   sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
18016   const char *zRecoveryDb = "";   /* Name of "recovery" database */
18017   const char *zLostAndFound = "lost_and_found";
18018   int i;
18019   int nOrphan = -1;
18020   RecoverTable *pOrphan = 0;
18021
18022   int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
18023   int bRowids = 1;                /* 0 if --no-rowids */
18024   for(i=1; i<nArg; i++){
18025     char *z = azArg[i];
18026     int n;
18027     if( z[0]=='-' && z[1]=='-' ) z++;
18028     n = strlen30(z);
18029     if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
18030       bFreelist = 0;
18031     }else
18032     if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
18033       i++;
18034       zRecoveryDb = azArg[i];
18035     }else
18036     if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
18037       i++;
18038       zLostAndFound = azArg[i];
18039     }else
18040     if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
18041       bRowids = 0;
18042     }
18043     else{
18044       utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 
18045       showHelp(pState->out, azArg[0]);
18046       return 1;
18047     }
18048   }
18049
18050   shellExecPrintf(pState->db, &rc,
18051     /* Attach an in-memory database named 'recovery'. Create an indexed 
18052     ** cache of the sqlite_dbptr virtual table. */
18053     "PRAGMA writable_schema = on;"
18054     "ATTACH %Q AS recovery;"
18055     "DROP TABLE IF EXISTS recovery.dbptr;"
18056     "DROP TABLE IF EXISTS recovery.freelist;"
18057     "DROP TABLE IF EXISTS recovery.map;"
18058     "DROP TABLE IF EXISTS recovery.schema;"
18059     "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
18060   );
18061
18062   if( bFreelist ){
18063     shellExec(pState->db, &rc,
18064       "WITH trunk(pgno) AS ("
18065       "  SELECT shell_int32("
18066       "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
18067       "      WHERE x>0"
18068       "    UNION"
18069       "  SELECT shell_int32("
18070       "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
18071       "      FROM trunk WHERE x>0"
18072       "),"
18073       "freelist(data, n, freepgno) AS ("
18074       "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
18075       "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
18076       "    UNION ALL"
18077       "  SELECT data, n-1, shell_int32(data, 2+n) "
18078       "      FROM freelist WHERE n>=0"
18079       ")"
18080       "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
18081     );
18082   }
18083
18084   /* If this is an auto-vacuum database, add all pointer-map pages to
18085   ** the freelist table. Do this regardless of whether or not 
18086   ** --freelist-corrupt was specified.  */
18087   shellExec(pState->db, &rc, 
18088     "WITH ptrmap(pgno) AS ("
18089     "  SELECT 2 WHERE shell_int32("
18090     "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
18091     "  )"
18092     "    UNION ALL "
18093     "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
18094     "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
18095     ")"
18096     "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
18097   );
18098
18099   shellExec(pState->db, &rc, 
18100     "CREATE TABLE recovery.dbptr("
18101     "      pgno, child, PRIMARY KEY(child, pgno)"
18102     ") WITHOUT ROWID;"
18103     "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
18104     "    SELECT * FROM sqlite_dbptr"
18105     "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
18106
18107     /* Delete any pointer to page 1. This ensures that page 1 is considered
18108     ** a root page, regardless of how corrupt the db is. */
18109     "DELETE FROM recovery.dbptr WHERE child = 1;"
18110
18111     /* Delete all pointers to any pages that have more than one pointer
18112     ** to them. Such pages will be treated as root pages when recovering
18113     ** data.  */
18114     "DELETE FROM recovery.dbptr WHERE child IN ("
18115     "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
18116     ");"
18117
18118     /* Create the "map" table that will (eventually) contain instructions
18119     ** for dealing with each page in the db that contains one or more 
18120     ** records. */
18121     "CREATE TABLE recovery.map("
18122       "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
18123     ");"
18124
18125     /* Populate table [map]. If there are circular loops of pages in the
18126     ** database, the following adds all pages in such a loop to the map
18127     ** as individual root pages. This could be handled better.  */
18128     "WITH pages(i, maxlen) AS ("
18129     "  SELECT page_count, ("
18130     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
18131     "  ) FROM pragma_page_count WHERE page_count>0"
18132     "    UNION ALL"
18133     "  SELECT i-1, ("
18134     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
18135     "  ) FROM pages WHERE i>=2"
18136     ")"
18137     "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
18138     "  SELECT i, maxlen, NULL, ("
18139     "    WITH p(orig, pgno, parent) AS ("
18140     "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
18141     "        UNION "
18142     "      SELECT i, p.parent, "
18143     "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
18144     "    )"
18145     "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
18146     ") "
18147     "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
18148     "UPDATE recovery.map AS o SET intkey = ("
18149     "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
18150     ");"
18151
18152     /* Extract data from page 1 and any linked pages into table
18153     ** recovery.schema. With the same schema as an sqlite_schema table.  */
18154     "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
18155     "INSERT INTO recovery.schema SELECT "
18156     "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
18157     "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
18158     "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
18159     "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
18160     "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
18161     "FROM sqlite_dbdata WHERE pgno IN ("
18162     "  SELECT pgno FROM recovery.map WHERE root=1"
18163     ")"
18164     "GROUP BY pgno, cell;"
18165     "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
18166   );
18167
18168   /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 
18169   ** CREATE TABLE statements that extracted from the existing schema.  */
18170   if( rc==SQLITE_OK ){
18171     sqlite3_stmt *pStmt = 0;
18172     /* ".recover" might output content in an order which causes immediate
18173     ** foreign key constraints to be violated. So disable foreign-key
18174     ** constraint enforcement to prevent problems when running the output
18175     ** script. */
18176     raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
18177     raw_printf(pState->out, "BEGIN;\n");
18178     raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
18179     shellPrepare(pState->db, &rc,
18180         "SELECT sql FROM recovery.schema "
18181         "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
18182     );
18183     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18184       const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
18185       raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", 
18186           &zCreateTable[12]
18187       );
18188     }
18189     shellFinalize(&rc, pStmt);
18190   }
18191
18192   /* Figure out if an orphan table will be required. And if so, how many
18193   ** user columns it should contain */
18194   shellPrepare(pState->db, &rc, 
18195       "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
18196       , &pLoop
18197   );
18198   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18199     nOrphan = sqlite3_column_int(pLoop, 0);
18200   }
18201   shellFinalize(&rc, pLoop);
18202   pLoop = 0;
18203
18204   shellPrepare(pState->db, &rc,
18205       "SELECT pgno FROM recovery.map WHERE root=?", &pPages
18206   );
18207
18208   shellPrepare(pState->db, &rc,
18209       "SELECT max(field), group_concat(shell_escape_crnl(quote"
18210       "(case when (? AND field<0) then NULL else value end)"
18211       "), ', ')"
18212       ", min(field) "
18213       "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
18214       "GROUP BY cell", &pCells
18215   );
18216
18217   /* Loop through each root page. */
18218   shellPrepare(pState->db, &rc, 
18219       "SELECT root, intkey, max(maxlen) FROM recovery.map" 
18220       " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
18221       "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
18222       ")", &pLoop
18223   );
18224   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18225     int iRoot = sqlite3_column_int(pLoop, 0);
18226     int bIntkey = sqlite3_column_int(pLoop, 1);
18227     int nCol = sqlite3_column_int(pLoop, 2);
18228     int bNoop = 0;
18229     RecoverTable *pTab;
18230
18231     assert( bIntkey==0 || bIntkey==1 );
18232     pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
18233     if( bNoop || rc ) continue;
18234     if( pTab==0 ){
18235       if( pOrphan==0 ){
18236         pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18237       }
18238       pTab = pOrphan;
18239       if( pTab==0 ) break;
18240     }
18241
18242     if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
18243       raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
18244     }
18245     sqlite3_bind_int(pPages, 1, iRoot);
18246     if( bRowids==0 && pTab->iPk<0 ){
18247       sqlite3_bind_int(pCells, 1, 1);
18248     }else{
18249       sqlite3_bind_int(pCells, 1, 0);
18250     }
18251     sqlite3_bind_int(pCells, 3, pTab->iPk);
18252
18253     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
18254       int iPgno = sqlite3_column_int(pPages, 0);
18255       sqlite3_bind_int(pCells, 2, iPgno);
18256       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
18257         int nField = sqlite3_column_int(pCells, 0);
18258         int iMin = sqlite3_column_int(pCells, 2);
18259         const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
18260
18261         RecoverTable *pTab2 = pTab;
18262         if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
18263           if( pOrphan==0 ){
18264             pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18265           }
18266           pTab2 = pOrphan;
18267           if( pTab2==0 ) break;
18268         }
18269
18270         nField = nField+1;
18271         if( pTab2==pOrphan ){
18272           raw_printf(pState->out, 
18273               "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
18274               pTab2->zQuoted, iRoot, iPgno, nField,
18275               iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
18276           );
18277         }else{
18278           raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 
18279               pTab2->zQuoted, pTab2->azlCol[nField], zVal
18280           );
18281         }
18282       }
18283       shellReset(&rc, pCells);
18284     }
18285     shellReset(&rc, pPages);
18286     if( pTab!=pOrphan ) recoverFreeTable(pTab);
18287   }
18288   shellFinalize(&rc, pLoop);
18289   shellFinalize(&rc, pPages);
18290   shellFinalize(&rc, pCells);
18291   recoverFreeTable(pOrphan);
18292
18293   /* The rest of the schema */
18294   if( rc==SQLITE_OK ){
18295     sqlite3_stmt *pStmt = 0;
18296     shellPrepare(pState->db, &rc, 
18297         "SELECT sql, name FROM recovery.schema "
18298         "WHERE sql NOT LIKE 'create table%'", &pStmt
18299     );
18300     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18301       const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
18302       if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
18303         const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
18304         char *zPrint = shellMPrintf(&rc, 
18305           "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
18306           zName, zName, zSql
18307         );
18308         raw_printf(pState->out, "%s;\n", zPrint);
18309         sqlite3_free(zPrint);
18310       }else{
18311         raw_printf(pState->out, "%s;\n", zSql);
18312       }
18313     }
18314     shellFinalize(&rc, pStmt);
18315   }
18316
18317   if( rc==SQLITE_OK ){
18318     raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
18319     raw_printf(pState->out, "COMMIT;\n");
18320   }
18321   sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
18322   return rc;
18323 }
18324 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18325
18326
18327 /*
18328 ** If an input line begins with "." then invoke this routine to
18329 ** process that line.
18330 **
18331 ** Return 1 on error, 2 to exit, and 0 otherwise.
18332 */
18333 static int do_meta_command(char *zLine, ShellState *p){
18334   int h = 1;
18335   int nArg = 0;
18336   int n, c;
18337   int rc = 0;
18338   char *azArg[52];
18339
18340 #ifndef SQLITE_OMIT_VIRTUALTABLE
18341   if( p->expert.pExpert ){
18342     expertFinish(p, 1, 0);
18343   }
18344 #endif
18345
18346   /* Parse the input line into tokens.
18347   */
18348   while( zLine[h] && nArg<ArraySize(azArg)-1 ){
18349     while( IsSpace(zLine[h]) ){ h++; }
18350     if( zLine[h]==0 ) break;
18351     if( zLine[h]=='\'' || zLine[h]=='"' ){
18352       int delim = zLine[h++];
18353       azArg[nArg++] = &zLine[h];
18354       while( zLine[h] && zLine[h]!=delim ){
18355         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
18356         h++;
18357       }
18358       if( zLine[h]==delim ){
18359         zLine[h++] = 0;
18360       }
18361       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
18362     }else{
18363       azArg[nArg++] = &zLine[h];
18364       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
18365       if( zLine[h] ) zLine[h++] = 0;
18366       resolve_backslashes(azArg[nArg-1]);
18367     }
18368   }
18369   azArg[nArg] = 0;
18370
18371   /* Process the input line.
18372   */
18373   if( nArg==0 ) return 0; /* no tokens, no error */
18374   n = strlen30(azArg[0]);
18375   c = azArg[0][0];
18376   clearTempFile(p);
18377
18378 #ifndef SQLITE_OMIT_AUTHORIZATION
18379   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
18380     if( nArg!=2 ){
18381       raw_printf(stderr, "Usage: .auth ON|OFF\n");
18382       rc = 1;
18383       goto meta_command_exit;
18384     }
18385     open_db(p, 0);
18386     if( booleanValue(azArg[1]) ){
18387       sqlite3_set_authorizer(p->db, shellAuth, p);
18388     }else{
18389       sqlite3_set_authorizer(p->db, 0, 0);
18390     }
18391   }else
18392 #endif
18393
18394 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
18395   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
18396     open_db(p, 0);
18397     rc = arDotCommand(p, 0, azArg, nArg);
18398   }else
18399 #endif
18400
18401   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
18402    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
18403   ){
18404     const char *zDestFile = 0;
18405     const char *zDb = 0;
18406     sqlite3 *pDest;
18407     sqlite3_backup *pBackup;
18408     int j;
18409     int bAsync = 0;
18410     const char *zVfs = 0;
18411     for(j=1; j<nArg; j++){
18412       const char *z = azArg[j];
18413       if( z[0]=='-' ){
18414         if( z[1]=='-' ) z++;
18415         if( strcmp(z, "-append")==0 ){
18416           zVfs = "apndvfs";
18417         }else
18418         if( strcmp(z, "-async")==0 ){
18419           bAsync = 1;
18420         }else
18421         {
18422           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
18423           return 1;
18424         }
18425       }else if( zDestFile==0 ){
18426         zDestFile = azArg[j];
18427       }else if( zDb==0 ){
18428         zDb = zDestFile;
18429         zDestFile = azArg[j];
18430       }else{
18431         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
18432         return 1;
18433       }
18434     }
18435     if( zDestFile==0 ){
18436       raw_printf(stderr, "missing FILENAME argument on .backup\n");
18437       return 1;
18438     }
18439     if( zDb==0 ) zDb = "main";
18440     rc = sqlite3_open_v2(zDestFile, &pDest, 
18441                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
18442     if( rc!=SQLITE_OK ){
18443       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
18444       close_db(pDest);
18445       return 1;
18446     }
18447     if( bAsync ){
18448       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
18449                    0, 0, 0);
18450     }
18451     open_db(p, 0);
18452     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
18453     if( pBackup==0 ){
18454       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18455       close_db(pDest);
18456       return 1;
18457     }
18458     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
18459     sqlite3_backup_finish(pBackup);
18460     if( rc==SQLITE_DONE ){
18461       rc = 0;
18462     }else{
18463       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18464       rc = 1;
18465     }
18466     close_db(pDest);
18467   }else
18468
18469   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
18470     if( nArg==2 ){
18471       bail_on_error = booleanValue(azArg[1]);
18472     }else{
18473       raw_printf(stderr, "Usage: .bail on|off\n");
18474       rc = 1;
18475     }
18476   }else
18477
18478   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
18479     if( nArg==2 ){
18480       if( booleanValue(azArg[1]) ){
18481         setBinaryMode(p->out, 1);
18482       }else{
18483         setTextMode(p->out, 1);
18484       }
18485     }else{
18486       raw_printf(stderr, "Usage: .binary on|off\n");
18487       rc = 1;
18488     }
18489   }else
18490
18491   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
18492     if( nArg==2 ){
18493 #if defined(_WIN32) || defined(WIN32)
18494       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
18495       rc = !SetCurrentDirectoryW(z);
18496       sqlite3_free(z);
18497 #else
18498       rc = chdir(azArg[1]);
18499 #endif
18500       if( rc ){
18501         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
18502         rc = 1;
18503       }
18504     }else{
18505       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
18506       rc = 1;
18507     }
18508   }else
18509
18510   /* The undocumented ".breakpoint" command causes a call to the no-op
18511   ** routine named test_breakpoint().
18512   */
18513   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
18514     test_breakpoint();
18515   }else
18516
18517   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
18518     if( nArg==2 ){
18519       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
18520     }else{
18521       raw_printf(stderr, "Usage: .changes on|off\n");
18522       rc = 1;
18523     }
18524   }else
18525
18526   /* Cancel output redirection, if it is currently set (by .testcase)
18527   ** Then read the content of the testcase-out.txt file and compare against
18528   ** azArg[1].  If there are differences, report an error and exit.
18529   */
18530   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
18531     char *zRes = 0;
18532     output_reset(p);
18533     if( nArg!=2 ){
18534       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
18535       rc = 2;
18536     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
18537       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
18538       rc = 2;
18539     }else if( testcase_glob(azArg[1],zRes)==0 ){
18540       utf8_printf(stderr,
18541                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
18542                  p->zTestcase, azArg[1], zRes);
18543       rc = 1;
18544     }else{
18545       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
18546       p->nCheck++;
18547     }
18548     sqlite3_free(zRes);
18549   }else
18550
18551   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
18552     if( nArg==2 ){
18553       tryToClone(p, azArg[1]);
18554     }else{
18555       raw_printf(stderr, "Usage: .clone FILENAME\n");
18556       rc = 1;
18557     }
18558   }else
18559
18560   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
18561     char **azName = 0;
18562     int nName = 0;
18563     sqlite3_stmt *pStmt;
18564     int i;
18565     open_db(p, 0);
18566     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
18567     if( rc ){
18568       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18569       rc = 1;
18570     }else{
18571       while( sqlite3_step(pStmt)==SQLITE_ROW ){
18572         const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
18573         const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
18574         azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
18575         if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
18576         azName[nName*2] = strdup(zSchema);
18577         azName[nName*2+1] = strdup(zFile);
18578         nName++;
18579       }
18580     }
18581     sqlite3_finalize(pStmt);
18582     for(i=0; i<nName; i++){
18583       int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
18584       int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
18585       const char *z = azName[i*2+1];
18586       utf8_printf(p->out, "%s: %s %s%s\n",
18587          azName[i*2],
18588          z && z[0] ? z : "\"\"",
18589          bRdonly ? "r/o" : "r/w",
18590          eTxn==SQLITE_TXN_NONE ? "" :
18591             eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
18592       free(azName[i*2]);
18593       free(azName[i*2+1]);
18594     }
18595     sqlite3_free(azName);
18596   }else
18597
18598   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
18599     static const struct DbConfigChoices {
18600       const char *zName;
18601       int op;
18602     } aDbConfig[] = {
18603         { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
18604         { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
18605         { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
18606         { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
18607         { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
18608         { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
18609         { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
18610         { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
18611         { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
18612         { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
18613         { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
18614         { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
18615         { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
18616         { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
18617         { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
18618         { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
18619     };
18620     int ii, v;
18621     open_db(p, 0);
18622     for(ii=0; ii<ArraySize(aDbConfig); ii++){
18623       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
18624       if( nArg>=3 ){
18625         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
18626       }
18627       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
18628       utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
18629       if( nArg>1 ) break;
18630     }
18631     if( nArg>1 && ii==ArraySize(aDbConfig) ){
18632       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
18633       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
18634     }   
18635   }else
18636
18637   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
18638     rc = shell_dbinfo_command(p, nArg, azArg);
18639   }else
18640
18641 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18642   if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
18643     open_db(p, 0);
18644     rc = recoverDatabaseCmd(p, nArg, azArg);
18645   }else
18646 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18647
18648   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
18649     char *zLike = 0;
18650     char *zSql;
18651     int i;
18652     int savedShowHeader = p->showHeader;
18653     int savedShellFlags = p->shellFlgs;
18654     ShellClearFlag(p, 
18655        SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
18656        |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
18657     for(i=1; i<nArg; i++){
18658       if( azArg[i][0]=='-' ){
18659         const char *z = azArg[i]+1;
18660         if( z[0]=='-' ) z++;
18661         if( strcmp(z,"preserve-rowids")==0 ){
18662 #ifdef SQLITE_OMIT_VIRTUALTABLE
18663           raw_printf(stderr, "The --preserve-rowids option is not compatible"
18664                              " with SQLITE_OMIT_VIRTUALTABLE\n");
18665           rc = 1;
18666           sqlite3_free(zLike);
18667           goto meta_command_exit;
18668 #else
18669           ShellSetFlag(p, SHFLG_PreserveRowid);
18670 #endif
18671         }else
18672         if( strcmp(z,"newlines")==0 ){
18673           ShellSetFlag(p, SHFLG_Newlines);
18674         }else
18675         if( strcmp(z,"data-only")==0 ){
18676           ShellSetFlag(p, SHFLG_DumpDataOnly);
18677         }else
18678         if( strcmp(z,"nosys")==0 ){
18679           ShellSetFlag(p, SHFLG_DumpNoSys);
18680         }else
18681         {
18682           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
18683           rc = 1;
18684           sqlite3_free(zLike);
18685           goto meta_command_exit;
18686         }
18687       }else{
18688         /* azArg[i] contains a LIKE pattern. This ".dump" request should
18689         ** only dump data for tables for which either the table name matches
18690         ** the LIKE pattern, or the table appears to be a shadow table of
18691         ** a virtual table for which the name matches the LIKE pattern.
18692         */
18693         char *zExpr = sqlite3_mprintf(
18694             "name LIKE %Q ESCAPE '\\' OR EXISTS ("
18695             "  SELECT 1 FROM sqlite_schema WHERE "
18696             "    name LIKE %Q ESCAPE '\\' AND"
18697             "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
18698             "    substr(o.name, 1, length(name)+1) == (name||'_')"
18699             ")", azArg[i], azArg[i]
18700         );
18701       
18702         if( zLike ){
18703           zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
18704         }else{
18705           zLike = zExpr;
18706         }
18707       }
18708     }
18709
18710     open_db(p, 0);
18711
18712     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
18713       /* When playing back a "dump", the content might appear in an order
18714       ** which causes immediate foreign key constraints to be violated.
18715       ** So disable foreign-key constraint enforcement to prevent problems. */
18716       raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
18717       raw_printf(p->out, "BEGIN TRANSACTION;\n");
18718     }
18719     p->writableSchema = 0;
18720     p->showHeader = 0;
18721     /* Set writable_schema=ON since doing so forces SQLite to initialize
18722     ** as much of the schema as it can even if the sqlite_schema table is
18723     ** corrupt. */
18724     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
18725     p->nErr = 0;
18726     if( zLike==0 ) zLike = sqlite3_mprintf("true");
18727     zSql = sqlite3_mprintf(
18728       "SELECT name, type, sql FROM sqlite_schema AS o "
18729       "WHERE (%s) AND type=='table'"
18730       "  AND sql NOT NULL"
18731       " ORDER BY tbl_name='sqlite_sequence', rowid",
18732       zLike
18733     );
18734     run_schema_dump_query(p,zSql);
18735     sqlite3_free(zSql);
18736     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
18737       zSql = sqlite3_mprintf(
18738         "SELECT sql FROM sqlite_schema AS o "
18739         "WHERE (%s) AND sql NOT NULL"
18740         "  AND type IN ('index','trigger','view')",
18741         zLike
18742       );
18743       run_table_dump_query(p, zSql);
18744       sqlite3_free(zSql);
18745     }
18746     sqlite3_free(zLike);
18747     if( p->writableSchema ){
18748       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
18749       p->writableSchema = 0;
18750     }
18751     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
18752     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
18753     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
18754       raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
18755     }
18756     p->showHeader = savedShowHeader;
18757     p->shellFlgs = savedShellFlags;
18758   }else
18759
18760   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
18761     if( nArg==2 ){
18762       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
18763     }else{
18764       raw_printf(stderr, "Usage: .echo on|off\n");
18765       rc = 1;
18766     }
18767   }else
18768
18769   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
18770     if( nArg==2 ){
18771       p->autoEQPtest = 0;
18772       if( p->autoEQPtrace ){
18773         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
18774         p->autoEQPtrace = 0;
18775       }
18776       if( strcmp(azArg[1],"full")==0 ){
18777         p->autoEQP = AUTOEQP_full;
18778       }else if( strcmp(azArg[1],"trigger")==0 ){
18779         p->autoEQP = AUTOEQP_trigger;
18780 #ifdef SQLITE_DEBUG
18781       }else if( strcmp(azArg[1],"test")==0 ){
18782         p->autoEQP = AUTOEQP_on;
18783         p->autoEQPtest = 1;
18784       }else if( strcmp(azArg[1],"trace")==0 ){
18785         p->autoEQP = AUTOEQP_full;
18786         p->autoEQPtrace = 1;
18787         open_db(p, 0);
18788         sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
18789         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
18790 #endif
18791       }else{
18792         p->autoEQP = (u8)booleanValue(azArg[1]);
18793       }
18794     }else{
18795       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
18796       rc = 1;
18797     }
18798   }else
18799
18800   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
18801     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
18802     rc = 2;
18803   }else
18804
18805   /* The ".explain" command is automatic now.  It is largely pointless.  It
18806   ** retained purely for backwards compatibility */
18807   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
18808     int val = 1;
18809     if( nArg>=2 ){
18810       if( strcmp(azArg[1],"auto")==0 ){
18811         val = 99;
18812       }else{
18813         val =  booleanValue(azArg[1]);
18814       }
18815     }
18816     if( val==1 && p->mode!=MODE_Explain ){
18817       p->normalMode = p->mode;
18818       p->mode = MODE_Explain;
18819       p->autoExplain = 0;
18820     }else if( val==0 ){
18821       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18822       p->autoExplain = 0;
18823     }else if( val==99 ){
18824       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18825       p->autoExplain = 1;
18826     }
18827   }else
18828
18829 #ifndef SQLITE_OMIT_VIRTUALTABLE
18830   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
18831     open_db(p, 0);
18832     expertDotCommand(p, azArg, nArg);
18833   }else
18834 #endif
18835
18836   if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
18837     static const struct {
18838        const char *zCtrlName;   /* Name of a test-control option */
18839        int ctrlCode;            /* Integer code for that option */
18840        const char *zUsage;      /* Usage notes */
18841     } aCtrl[] = {
18842       { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
18843       { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
18844       { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },  
18845       { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
18846       { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
18847    /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
18848       { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
18849       { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
18850       { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
18851       { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
18852    /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
18853     };
18854     int filectrl = -1;
18855     int iCtrl = -1;
18856     sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
18857     int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
18858     int n2, i;
18859     const char *zCmd = 0;
18860     const char *zSchema = 0;
18861
18862     open_db(p, 0);
18863     zCmd = nArg>=2 ? azArg[1] : "help";
18864
18865     if( zCmd[0]=='-' 
18866      && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
18867      && nArg>=4
18868     ){
18869       zSchema = azArg[2];
18870       for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
18871       nArg -= 2;
18872       zCmd = azArg[1];
18873     }
18874
18875     /* The argument can optionally begin with "-" or "--" */
18876     if( zCmd[0]=='-' && zCmd[1] ){
18877       zCmd++;
18878       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
18879     }
18880
18881     /* --help lists all file-controls */
18882     if( strcmp(zCmd,"help")==0 ){
18883       utf8_printf(p->out, "Available file-controls:\n");
18884       for(i=0; i<ArraySize(aCtrl); i++){
18885         utf8_printf(p->out, "  .filectrl %s %s\n",
18886                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
18887       }
18888       rc = 1;
18889       goto meta_command_exit;
18890     }
18891
18892     /* convert filectrl text option to value. allow any unique prefix
18893     ** of the option name, or a numerical value. */
18894     n2 = strlen30(zCmd);
18895     for(i=0; i<ArraySize(aCtrl); i++){
18896       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
18897         if( filectrl<0 ){
18898           filectrl = aCtrl[i].ctrlCode;
18899           iCtrl = i;
18900         }else{
18901           utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
18902                               "Use \".filectrl --help\" for help\n", zCmd);
18903           rc = 1;
18904           goto meta_command_exit;
18905         }
18906       }
18907     }
18908     if( filectrl<0 ){
18909       utf8_printf(stderr,"Error: unknown file-control: %s\n"
18910                          "Use \".filectrl --help\" for help\n", zCmd);
18911     }else{
18912       switch(filectrl){
18913         case SQLITE_FCNTL_SIZE_LIMIT: {
18914           if( nArg!=2 && nArg!=3 ) break;
18915           iRes = nArg==3 ? integerValue(azArg[2]) : -1;
18916           sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
18917           isOk = 1;
18918           break;
18919         }
18920         case SQLITE_FCNTL_LOCK_TIMEOUT:
18921         case SQLITE_FCNTL_CHUNK_SIZE: {
18922           int x;
18923           if( nArg!=3 ) break;
18924           x = (int)integerValue(azArg[2]);
18925           sqlite3_file_control(p->db, zSchema, filectrl, &x);
18926           isOk = 2;
18927           break;
18928         }
18929         case SQLITE_FCNTL_PERSIST_WAL:
18930         case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
18931           int x;
18932           if( nArg!=2 && nArg!=3 ) break;
18933           x = nArg==3 ? booleanValue(azArg[2]) : -1;
18934           sqlite3_file_control(p->db, zSchema, filectrl, &x);
18935           iRes = x;
18936           isOk = 1;
18937           break;
18938         }
18939         case SQLITE_FCNTL_DATA_VERSION:
18940         case SQLITE_FCNTL_HAS_MOVED: {
18941           int x;
18942           if( nArg!=2 ) break;
18943           sqlite3_file_control(p->db, zSchema, filectrl, &x);
18944           iRes = x;
18945           isOk = 1;
18946           break;
18947         }
18948         case SQLITE_FCNTL_TEMPFILENAME: {
18949           char *z = 0;
18950           if( nArg!=2 ) break;
18951           sqlite3_file_control(p->db, zSchema, filectrl, &z);
18952           if( z ){
18953             utf8_printf(p->out, "%s\n", z);
18954             sqlite3_free(z);
18955           }
18956           isOk = 2;
18957           break;
18958         }
18959         case SQLITE_FCNTL_RESERVE_BYTES: {
18960           int x;
18961           if( nArg>=3 ){
18962             x = atoi(azArg[2]);
18963             sqlite3_file_control(p->db, zSchema, filectrl, &x);
18964           }
18965           x = -1;
18966           sqlite3_file_control(p->db, zSchema, filectrl, &x);
18967           utf8_printf(p->out,"%d\n", x);
18968           isOk = 2;
18969           break;
18970         }
18971       }
18972     }
18973     if( isOk==0 && iCtrl>=0 ){
18974       utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18975       rc = 1;
18976     }else if( isOk==1 ){
18977       char zBuf[100];
18978       sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
18979       raw_printf(p->out, "%s\n", zBuf);
18980     }
18981   }else
18982
18983   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
18984     ShellState data;
18985     int doStats = 0;
18986     memcpy(&data, p, sizeof(data));
18987     data.showHeader = 0;
18988     data.cMode = data.mode = MODE_Semi;
18989     if( nArg==2 && optionMatch(azArg[1], "indent") ){
18990       data.cMode = data.mode = MODE_Pretty;
18991       nArg = 1;
18992     }
18993     if( nArg!=1 ){
18994       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
18995       rc = 1;
18996       goto meta_command_exit;
18997     }
18998     open_db(p, 0);
18999     rc = sqlite3_exec(p->db,
19000        "SELECT sql FROM"
19001        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
19002        "     FROM sqlite_schema UNION ALL"
19003        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
19004        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
19005        "ORDER BY rowid",
19006        callback, &data, 0
19007     );
19008     if( rc==SQLITE_OK ){
19009       sqlite3_stmt *pStmt;
19010       rc = sqlite3_prepare_v2(p->db,
19011                "SELECT rowid FROM sqlite_schema"
19012                " WHERE name GLOB 'sqlite_stat[134]'",
19013                -1, &pStmt, 0);
19014       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
19015       sqlite3_finalize(pStmt);
19016     }
19017     if( doStats==0 ){
19018       raw_printf(p->out, "/* No STAT tables available */\n");
19019     }else{
19020       raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19021       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
19022                    callback, &data, 0);
19023       data.cMode = data.mode = MODE_Insert;
19024       data.zDestTable = "sqlite_stat1";
19025       shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
19026       data.zDestTable = "sqlite_stat4";
19027       shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
19028       raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19029     }
19030   }else
19031
19032   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
19033     if( nArg==2 ){
19034       p->showHeader = booleanValue(azArg[1]);
19035       p->shellFlgs |= SHFLG_HeaderSet;
19036     }else{
19037       raw_printf(stderr, "Usage: .headers on|off\n");
19038       rc = 1;
19039     }
19040   }else
19041
19042   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
19043     if( nArg>=2 ){
19044       n = showHelp(p->out, azArg[1]);
19045       if( n==0 ){
19046         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
19047       }
19048     }else{
19049       showHelp(p->out, 0);
19050     }
19051   }else
19052
19053   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
19054     char *zTable = 0;           /* Insert data into this table */
19055     char *zFile = 0;            /* Name of file to extra content from */
19056     sqlite3_stmt *pStmt = NULL; /* A statement */
19057     int nCol;                   /* Number of columns in the table */
19058     int nByte;                  /* Number of bytes in an SQL string */
19059     int i, j;                   /* Loop counters */
19060     int needCommit;             /* True to COMMIT or ROLLBACK at end */
19061     int nSep;                   /* Number of bytes in p->colSeparator[] */
19062     char *zSql;                 /* An SQL statement */
19063     ImportCtx sCtx;             /* Reader context */
19064     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
19065     int eVerbose = 0;           /* Larger for more console output */
19066     int nSkip = 0;              /* Initial lines to skip */
19067     int useOutputMode = 1;      /* Use output mode to determine separators */
19068
19069     memset(&sCtx, 0, sizeof(sCtx));
19070     if( p->mode==MODE_Ascii ){
19071       xRead = ascii_read_one_field;
19072     }else{
19073       xRead = csv_read_one_field;
19074     }
19075     for(i=1; i<nArg; i++){
19076       char *z = azArg[i];
19077       if( z[0]=='-' && z[1]=='-' ) z++;
19078       if( z[0]!='-' ){
19079         if( zFile==0 ){
19080           zFile = z;
19081         }else if( zTable==0 ){
19082           zTable = z;
19083         }else{
19084           utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
19085           showHelp(p->out, "import");
19086           rc = 1;
19087           goto meta_command_exit;
19088         }
19089       }else if( strcmp(z,"-v")==0 ){
19090         eVerbose++;
19091       }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
19092         nSkip = integerValue(azArg[++i]);
19093       }else if( strcmp(z,"-ascii")==0 ){
19094         sCtx.cColSep = SEP_Unit[0];
19095         sCtx.cRowSep = SEP_Record[0];
19096         xRead = ascii_read_one_field;
19097         useOutputMode = 0;
19098       }else if( strcmp(z,"-csv")==0 ){
19099         sCtx.cColSep = ',';
19100         sCtx.cRowSep = '\n';
19101         xRead = csv_read_one_field;
19102         useOutputMode = 0;
19103       }else{
19104         utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
19105         showHelp(p->out, "import");
19106         rc = 1;
19107         goto meta_command_exit;
19108       }
19109     }
19110     if( zTable==0 ){
19111       utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
19112                   zFile==0 ? "FILE" : "TABLE");
19113       showHelp(p->out, "import");
19114       rc = 1;
19115       goto meta_command_exit;
19116     }
19117     seenInterrupt = 0;
19118     open_db(p, 0);
19119     if( useOutputMode ){
19120       /* If neither the --csv or --ascii options are specified, then set
19121       ** the column and row separator characters from the output mode. */
19122       nSep = strlen30(p->colSeparator);
19123       if( nSep==0 ){
19124         raw_printf(stderr,
19125                    "Error: non-null column separator required for import\n");
19126         rc = 1;
19127         goto meta_command_exit;
19128       }
19129       if( nSep>1 ){
19130         raw_printf(stderr, 
19131               "Error: multi-character column separators not allowed"
19132               " for import\n");
19133         rc = 1;
19134         goto meta_command_exit;
19135       }
19136       nSep = strlen30(p->rowSeparator);
19137       if( nSep==0 ){
19138         raw_printf(stderr,
19139             "Error: non-null row separator required for import\n");
19140         rc = 1;
19141         goto meta_command_exit;
19142       }
19143       if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
19144         /* When importing CSV (only), if the row separator is set to the
19145         ** default output row separator, change it to the default input
19146         ** row separator.  This avoids having to maintain different input
19147         ** and output row separators. */
19148         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19149         nSep = strlen30(p->rowSeparator);
19150       }
19151       if( nSep>1 ){
19152         raw_printf(stderr, "Error: multi-character row separators not allowed"
19153                            " for import\n");
19154         rc = 1;
19155         goto meta_command_exit;
19156       }
19157       sCtx.cColSep = p->colSeparator[0];
19158       sCtx.cRowSep = p->rowSeparator[0];
19159     }
19160     sCtx.zFile = zFile;
19161     sCtx.nLine = 1;
19162     if( sCtx.zFile[0]=='|' ){
19163 #ifdef SQLITE_OMIT_POPEN
19164       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19165       rc = 1;
19166       goto meta_command_exit;
19167 #else
19168       sCtx.in = popen(sCtx.zFile+1, "r");
19169       sCtx.zFile = "<pipe>";
19170       sCtx.xCloser = pclose;
19171 #endif
19172     }else{
19173       sCtx.in = fopen(sCtx.zFile, "rb");
19174       sCtx.xCloser = fclose;
19175     }
19176     if( sCtx.in==0 ){
19177       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
19178       rc = 1;
19179       goto meta_command_exit;
19180     }
19181     if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
19182       char zSep[2];
19183       zSep[1] = 0;
19184       zSep[0] = sCtx.cColSep;
19185       utf8_printf(p->out, "Column separator ");
19186       output_c_string(p->out, zSep);
19187       utf8_printf(p->out, ", row separator ");
19188       zSep[0] = sCtx.cRowSep;
19189       output_c_string(p->out, zSep);
19190       utf8_printf(p->out, "\n");
19191     }
19192     while( (nSkip--)>0 ){
19193       while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
19194     }
19195     zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
19196     if( zSql==0 ){
19197       import_cleanup(&sCtx);
19198       shell_out_of_memory();
19199     }
19200     nByte = strlen30(zSql);
19201     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19202     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
19203     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
19204       char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
19205       char cSep = '(';
19206       while( xRead(&sCtx) ){
19207         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
19208         cSep = ',';
19209         if( sCtx.cTerm!=sCtx.cColSep ) break;
19210       }
19211       if( cSep=='(' ){
19212         sqlite3_free(zCreate);
19213         import_cleanup(&sCtx);
19214         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
19215         rc = 1;
19216         goto meta_command_exit;
19217       }
19218       zCreate = sqlite3_mprintf("%z\n)", zCreate);
19219       if( eVerbose>=1 ){
19220         utf8_printf(p->out, "%s\n", zCreate);
19221       }
19222       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
19223       sqlite3_free(zCreate);
19224       if( rc ){
19225         utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
19226                 sqlite3_errmsg(p->db));
19227         import_cleanup(&sCtx);
19228         rc = 1;
19229         goto meta_command_exit;
19230       }
19231       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19232     }
19233     sqlite3_free(zSql);
19234     if( rc ){
19235       if (pStmt) sqlite3_finalize(pStmt);
19236       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
19237       import_cleanup(&sCtx);
19238       rc = 1;
19239       goto meta_command_exit;
19240     }
19241     nCol = sqlite3_column_count(pStmt);
19242     sqlite3_finalize(pStmt);
19243     pStmt = 0;
19244     if( nCol==0 ) return 0; /* no columns, no error */
19245     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
19246     if( zSql==0 ){
19247       import_cleanup(&sCtx);
19248       shell_out_of_memory();
19249     }
19250     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
19251     j = strlen30(zSql);
19252     for(i=1; i<nCol; i++){
19253       zSql[j++] = ',';
19254       zSql[j++] = '?';
19255     }
19256     zSql[j++] = ')';
19257     zSql[j] = 0;
19258     if( eVerbose>=2 ){
19259       utf8_printf(p->out, "Insert using: %s\n", zSql);
19260     }
19261     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19262     sqlite3_free(zSql);
19263     if( rc ){
19264       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19265       if (pStmt) sqlite3_finalize(pStmt);
19266       import_cleanup(&sCtx);
19267       rc = 1;
19268       goto meta_command_exit;
19269     }
19270     needCommit = sqlite3_get_autocommit(p->db);
19271     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
19272     do{
19273       int startLine = sCtx.nLine;
19274       for(i=0; i<nCol; i++){
19275         char *z = xRead(&sCtx);
19276         /*
19277         ** Did we reach end-of-file before finding any columns?
19278         ** If so, stop instead of NULL filling the remaining columns.
19279         */
19280         if( z==0 && i==0 ) break;
19281         /*
19282         ** Did we reach end-of-file OR end-of-line before finding any
19283         ** columns in ASCII mode?  If so, stop instead of NULL filling
19284         ** the remaining columns.
19285         */
19286         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
19287         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
19288         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
19289           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19290                           "filling the rest with NULL\n",
19291                           sCtx.zFile, startLine, nCol, i+1);
19292           i += 2;
19293           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
19294         }
19295       }
19296       if( sCtx.cTerm==sCtx.cColSep ){
19297         do{
19298           xRead(&sCtx);
19299           i++;
19300         }while( sCtx.cTerm==sCtx.cColSep );
19301         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19302                         "extras ignored\n",
19303                         sCtx.zFile, startLine, nCol, i);
19304       }
19305       if( i>=nCol ){
19306         sqlite3_step(pStmt);
19307         rc = sqlite3_reset(pStmt);
19308         if( rc!=SQLITE_OK ){
19309           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
19310                       startLine, sqlite3_errmsg(p->db));
19311           sCtx.nErr++;
19312         }else{
19313           sCtx.nRow++;
19314         }
19315       }
19316     }while( sCtx.cTerm!=EOF );
19317
19318     import_cleanup(&sCtx);
19319     sqlite3_finalize(pStmt);
19320     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
19321     if( eVerbose>0 ){
19322       utf8_printf(p->out,
19323           "Added %d rows with %d errors using %d lines of input\n",
19324           sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
19325     }
19326   }else
19327
19328 #ifndef SQLITE_UNTESTABLE
19329   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
19330     char *zSql;
19331     char *zCollist = 0;
19332     sqlite3_stmt *pStmt;
19333     int tnum = 0;
19334     int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
19335     int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
19336     int i;
19337     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
19338       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
19339                           "       .imposter off\n");
19340       /* Also allowed, but not documented:
19341       **
19342       **    .imposter TABLE IMPOSTER
19343       **
19344       ** where TABLE is a WITHOUT ROWID table.  In that case, the
19345       ** imposter is another WITHOUT ROWID table with the columns in
19346       ** storage order. */
19347       rc = 1;
19348       goto meta_command_exit;
19349     }
19350     open_db(p, 0);
19351     if( nArg==2 ){
19352       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
19353       goto meta_command_exit;
19354     }
19355     zSql = sqlite3_mprintf(
19356       "SELECT rootpage, 0 FROM sqlite_schema"
19357       " WHERE name='%q' AND type='index'"
19358       "UNION ALL "
19359       "SELECT rootpage, 1 FROM sqlite_schema"
19360       " WHERE name='%q' AND type='table'"
19361       "   AND sql LIKE '%%without%%rowid%%'",
19362       azArg[1], azArg[1]
19363     );
19364     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19365     sqlite3_free(zSql);
19366     if( sqlite3_step(pStmt)==SQLITE_ROW ){
19367       tnum = sqlite3_column_int(pStmt, 0);
19368       isWO = sqlite3_column_int(pStmt, 1);
19369     }
19370     sqlite3_finalize(pStmt);
19371     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
19372     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19373     sqlite3_free(zSql);
19374     i = 0;
19375     while( sqlite3_step(pStmt)==SQLITE_ROW ){
19376       char zLabel[20];
19377       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
19378       i++;
19379       if( zCol==0 ){
19380         if( sqlite3_column_int(pStmt,1)==-1 ){
19381           zCol = "_ROWID_";
19382         }else{
19383           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
19384           zCol = zLabel;
19385         }
19386       }
19387       if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
19388         lenPK = (int)strlen(zCollist);
19389       }
19390       if( zCollist==0 ){
19391         zCollist = sqlite3_mprintf("\"%w\"", zCol);
19392       }else{
19393         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
19394       }
19395     }
19396     sqlite3_finalize(pStmt);
19397     if( i==0 || tnum==0 ){
19398       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
19399       rc = 1;
19400       sqlite3_free(zCollist);
19401       goto meta_command_exit;
19402     }
19403     if( lenPK==0 ) lenPK = 100000;
19404     zSql = sqlite3_mprintf(
19405           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
19406           azArg[2], zCollist, lenPK, zCollist);
19407     sqlite3_free(zCollist);
19408     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
19409     if( rc==SQLITE_OK ){
19410       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
19411       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
19412       if( rc ){
19413         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
19414       }else{
19415         utf8_printf(stdout, "%s;\n", zSql);
19416         raw_printf(stdout,
19417           "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
19418           azArg[1], isWO ? "table" : "index"
19419         );
19420       }
19421     }else{
19422       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
19423       rc = 1;
19424     }
19425     sqlite3_free(zSql);
19426   }else
19427 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
19428
19429 #ifdef SQLITE_ENABLE_IOTRACE
19430   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
19431     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
19432     if( iotrace && iotrace!=stdout ) fclose(iotrace);
19433     iotrace = 0;
19434     if( nArg<2 ){
19435       sqlite3IoTrace = 0;
19436     }else if( strcmp(azArg[1], "-")==0 ){
19437       sqlite3IoTrace = iotracePrintf;
19438       iotrace = stdout;
19439     }else{
19440       iotrace = fopen(azArg[1], "w");
19441       if( iotrace==0 ){
19442         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19443         sqlite3IoTrace = 0;
19444         rc = 1;
19445       }else{
19446         sqlite3IoTrace = iotracePrintf;
19447       }
19448     }
19449   }else
19450 #endif
19451
19452   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
19453     static const struct {
19454        const char *zLimitName;   /* Name of a limit */
19455        int limitCode;            /* Integer code for that limit */
19456     } aLimit[] = {
19457       { "length",                SQLITE_LIMIT_LENGTH                    },
19458       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
19459       { "column",                SQLITE_LIMIT_COLUMN                    },
19460       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
19461       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
19462       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
19463       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
19464       { "attached",              SQLITE_LIMIT_ATTACHED                  },
19465       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
19466       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
19467       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
19468       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
19469     };
19470     int i, n2;
19471     open_db(p, 0);
19472     if( nArg==1 ){
19473       for(i=0; i<ArraySize(aLimit); i++){
19474         printf("%20s %d\n", aLimit[i].zLimitName,
19475                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
19476       }
19477     }else if( nArg>3 ){
19478       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
19479       rc = 1;
19480       goto meta_command_exit;
19481     }else{
19482       int iLimit = -1;
19483       n2 = strlen30(azArg[1]);
19484       for(i=0; i<ArraySize(aLimit); i++){
19485         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
19486           if( iLimit<0 ){
19487             iLimit = i;
19488           }else{
19489             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
19490             rc = 1;
19491             goto meta_command_exit;
19492           }
19493         }
19494       }
19495       if( iLimit<0 ){
19496         utf8_printf(stderr, "unknown limit: \"%s\"\n"
19497                         "enter \".limits\" with no arguments for a list.\n",
19498                          azArg[1]);
19499         rc = 1;
19500         goto meta_command_exit;
19501       }
19502       if( nArg==3 ){
19503         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
19504                       (int)integerValue(azArg[2]));
19505       }
19506       printf("%20s %d\n", aLimit[iLimit].zLimitName,
19507              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
19508     }
19509   }else
19510
19511   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
19512     open_db(p, 0);
19513     lintDotCommand(p, azArg, nArg);
19514   }else
19515
19516 #ifndef SQLITE_OMIT_LOAD_EXTENSION
19517   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
19518     const char *zFile, *zProc;
19519     char *zErrMsg = 0;
19520     if( nArg<2 ){
19521       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
19522       rc = 1;
19523       goto meta_command_exit;
19524     }
19525     zFile = azArg[1];
19526     zProc = nArg>=3 ? azArg[2] : 0;
19527     open_db(p, 0);
19528     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
19529     if( rc!=SQLITE_OK ){
19530       utf8_printf(stderr, "Error: %s\n", zErrMsg);
19531       sqlite3_free(zErrMsg);
19532       rc = 1;
19533     }
19534   }else
19535 #endif
19536
19537   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
19538     if( nArg!=2 ){
19539       raw_printf(stderr, "Usage: .log FILENAME\n");
19540       rc = 1;
19541     }else{
19542       const char *zFile = azArg[1];
19543       output_file_close(p->pLog);
19544       p->pLog = output_file_open(zFile, 0);
19545     }
19546   }else
19547
19548   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
19549     const char *zMode = nArg>=2 ? azArg[1] : "";
19550     int n2 = strlen30(zMode);
19551     int c2 = zMode[0];
19552     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
19553       p->mode = MODE_Line;
19554       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19555     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
19556       p->mode = MODE_Column;
19557       if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
19558         p->showHeader = 1;
19559       }
19560       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19561     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
19562       p->mode = MODE_List;
19563       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
19564       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19565     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
19566       p->mode = MODE_Html;
19567     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
19568       p->mode = MODE_Tcl;
19569       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
19570       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19571     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
19572       p->mode = MODE_Csv;
19573       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19574       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
19575     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
19576       p->mode = MODE_List;
19577       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
19578     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
19579       p->mode = MODE_Insert;
19580       set_table_name(p, nArg>=3 ? azArg[2] : "table");
19581     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
19582       p->mode = MODE_Quote;
19583       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19584       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19585     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
19586       p->mode = MODE_Ascii;
19587       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
19588       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
19589     }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
19590       p->mode = MODE_Markdown;
19591     }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
19592       p->mode = MODE_Table;
19593     }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
19594       p->mode = MODE_Box;
19595     }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
19596       p->mode = MODE_Json;
19597     }else if( nArg==1 ){
19598       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
19599     }else{
19600       raw_printf(stderr, "Error: mode should be one of: "
19601          "ascii box column csv html insert json line list markdown "
19602          "quote table tabs tcl\n");
19603       rc = 1;
19604     }
19605     p->cMode = p->mode;
19606   }else
19607
19608   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
19609     if( nArg==2 ){
19610       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
19611                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
19612     }else{
19613       raw_printf(stderr, "Usage: .nullvalue STRING\n");
19614       rc = 1;
19615     }
19616   }else
19617
19618 #ifdef SQLITE_DEBUG
19619   if( c=='o' && strcmp(azArg[0],"oom")==0 ){
19620     int i;
19621     for(i=1; i<nArg; i++){
19622       const char *z = azArg[i];
19623       if( z[0]=='-' && z[1]=='-' ) z++;
19624       if( strcmp(z,"-repeat")==0 ){
19625         if( i==nArg-1 ){
19626           raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
19627           rc = 1;
19628         }else{
19629           oomRepeat = (int)integerValue(azArg[++i]);
19630         }
19631       }else if( IsDigit(z[0]) ){
19632         oomCounter = (int)integerValue(azArg[i]);
19633       }else{
19634         raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
19635         raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
19636         rc = 1;
19637       }
19638     }
19639     if( rc==0 ){
19640       raw_printf(p->out, "oomCounter = %d\n", oomCounter);
19641       raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
19642     }
19643   }else
19644 #endif /* SQLITE_DEBUG */
19645
19646   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
19647     char *zNewFilename = 0;  /* Name of the database file to open */
19648     int iName = 1;           /* Index in azArg[] of the filename */
19649     int newFlag = 0;         /* True to delete file before opening */
19650     /* Close the existing database */
19651     session_close_all(p);
19652     close_db(p->db);
19653     p->db = 0;
19654     p->zDbFilename = 0;
19655     sqlite3_free(p->zFreeOnClose);
19656     p->zFreeOnClose = 0;
19657     p->openMode = SHELL_OPEN_UNSPEC;
19658     p->openFlags = 0;
19659     p->szMax = 0;
19660     /* Check for command-line arguments */
19661     for(iName=1; iName<nArg; iName++){
19662       const char *z = azArg[iName];
19663       if( optionMatch(z,"new") ){
19664         newFlag = 1;
19665 #ifdef SQLITE_HAVE_ZLIB
19666       }else if( optionMatch(z, "zip") ){
19667         p->openMode = SHELL_OPEN_ZIPFILE;
19668 #endif
19669       }else if( optionMatch(z, "append") ){
19670         p->openMode = SHELL_OPEN_APPENDVFS;
19671       }else if( optionMatch(z, "readonly") ){
19672         p->openMode = SHELL_OPEN_READONLY;
19673       }else if( optionMatch(z, "nofollow") ){
19674         p->openFlags |= SQLITE_OPEN_NOFOLLOW;
19675 #ifndef SQLITE_OMIT_DESERIALIZE
19676       }else if( optionMatch(z, "deserialize") ){
19677         p->openMode = SHELL_OPEN_DESERIALIZE;
19678       }else if( optionMatch(z, "hexdb") ){
19679         p->openMode = SHELL_OPEN_HEXDB;
19680       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
19681         p->szMax = integerValue(azArg[++iName]);
19682 #endif /* SQLITE_OMIT_DESERIALIZE */
19683       }else if( z[0]=='-' ){
19684         utf8_printf(stderr, "unknown option: %s\n", z);
19685         rc = 1;
19686         goto meta_command_exit;
19687       }else if( zNewFilename ){
19688         utf8_printf(stderr, "extra argument: \"%s\"\n", z);
19689         rc = 1;
19690         goto meta_command_exit;
19691       }else{
19692         zNewFilename = sqlite3_mprintf("%s", z);
19693       }
19694     }
19695     /* If a filename is specified, try to open it first */
19696     if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
19697       if( newFlag ) shellDeleteFile(zNewFilename);
19698       p->zDbFilename = zNewFilename;
19699       open_db(p, OPEN_DB_KEEPALIVE);
19700       if( p->db==0 ){
19701         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
19702         sqlite3_free(zNewFilename);
19703       }else{
19704         p->zFreeOnClose = zNewFilename;
19705       }
19706     }
19707     if( p->db==0 ){
19708       /* As a fall-back open a TEMP database */
19709       p->zDbFilename = 0;
19710       open_db(p, 0);
19711     }
19712   }else
19713
19714   if( (c=='o'
19715         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
19716    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
19717   ){
19718     char *zFile = 0;
19719     int bTxtMode = 0;
19720     int i;
19721     int eMode = 0;
19722     int bBOM = 0;
19723     int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
19724
19725     if( c=='e' ){
19726       eMode = 'x';
19727       bOnce = 2;
19728     }else if( strncmp(azArg[0],"once",n)==0 ){
19729       bOnce = 1;
19730     }
19731     for(i=1; i<nArg; i++){
19732       char *z = azArg[i];
19733       if( z[0]=='-' ){
19734         if( z[1]=='-' ) z++;
19735         if( strcmp(z,"-bom")==0 ){
19736           bBOM = 1;
19737         }else if( c!='e' && strcmp(z,"-x")==0 ){
19738           eMode = 'x';  /* spreadsheet */
19739         }else if( c!='e' && strcmp(z,"-e")==0 ){
19740           eMode = 'e';  /* text editor */
19741         }else{
19742           utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
19743                       azArg[i]);
19744           showHelp(p->out, azArg[0]);
19745           rc = 1;
19746           goto meta_command_exit;
19747         }
19748       }else if( zFile==0 && eMode!='e' && eMode!='x' ){
19749         zFile = sqlite3_mprintf("%s", z);
19750         if( zFile[0]=='|' ){
19751           while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
19752           break;
19753         }
19754       }else{
19755         utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
19756                     azArg[i]);
19757         showHelp(p->out, azArg[0]);
19758         rc = 1;
19759         sqlite3_free(zFile);
19760         goto meta_command_exit;
19761       }
19762     }
19763     if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
19764     if( bOnce ){
19765       p->outCount = 2;
19766     }else{
19767       p->outCount = 0;
19768     }
19769     output_reset(p);
19770 #ifndef SQLITE_NOHAVE_SYSTEM
19771     if( eMode=='e' || eMode=='x' ){
19772       p->doXdgOpen = 1;
19773       outputModePush(p);
19774       if( eMode=='x' ){
19775         /* spreadsheet mode.  Output as CSV. */
19776         newTempFile(p, "csv");
19777         ShellClearFlag(p, SHFLG_Echo);
19778         p->mode = MODE_Csv;
19779         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19780         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
19781       }else{
19782         /* text editor mode */
19783         newTempFile(p, "txt");
19784         bTxtMode = 1;
19785       }
19786       sqlite3_free(zFile);
19787       zFile = sqlite3_mprintf("%s", p->zTempFile);
19788     }
19789 #endif /* SQLITE_NOHAVE_SYSTEM */
19790     if( zFile[0]=='|' ){
19791 #ifdef SQLITE_OMIT_POPEN
19792       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19793       rc = 1;
19794       p->out = stdout;
19795 #else
19796       p->out = popen(zFile + 1, "w");
19797       if( p->out==0 ){
19798         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
19799         p->out = stdout;
19800         rc = 1;
19801       }else{
19802         if( bBOM ) fprintf(p->out,"\357\273\277");
19803         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
19804       }
19805 #endif
19806     }else{
19807       p->out = output_file_open(zFile, bTxtMode);
19808       if( p->out==0 ){
19809         if( strcmp(zFile,"off")!=0 ){
19810           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
19811         }
19812         p->out = stdout;
19813         rc = 1;
19814       } else {
19815         if( bBOM ) fprintf(p->out,"\357\273\277");
19816         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
19817       }
19818     }
19819     sqlite3_free(zFile);
19820   }else
19821
19822   if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
19823     open_db(p,0);
19824     if( nArg<=1 ) goto parameter_syntax_error;
19825
19826     /* .parameter clear
19827     ** Clear all bind parameters by dropping the TEMP table that holds them.
19828     */
19829     if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
19830       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
19831                    0, 0, 0);
19832     }else
19833
19834     /* .parameter list
19835     ** List all bind parameters.
19836     */
19837     if( nArg==2 && strcmp(azArg[1],"list")==0 ){
19838       sqlite3_stmt *pStmt = 0;
19839       int rx;
19840       int len = 0;
19841       rx = sqlite3_prepare_v2(p->db,
19842              "SELECT max(length(key)) "
19843              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19844       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
19845         len = sqlite3_column_int(pStmt, 0);
19846         if( len>40 ) len = 40;
19847       }
19848       sqlite3_finalize(pStmt);
19849       pStmt = 0;
19850       if( len ){
19851         rx = sqlite3_prepare_v2(p->db,
19852              "SELECT key, quote(value) "
19853              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19854         while( sqlite3_step(pStmt)==SQLITE_ROW ){
19855           utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
19856                       sqlite3_column_text(pStmt,1));
19857         }
19858         sqlite3_finalize(pStmt);
19859       }
19860     }else
19861
19862     /* .parameter init
19863     ** Make sure the TEMP table used to hold bind parameters exists.
19864     ** Create it if necessary.
19865     */
19866     if( nArg==2 && strcmp(azArg[1],"init")==0 ){
19867       bind_table_init(p);
19868     }else
19869
19870     /* .parameter set NAME VALUE
19871     ** Set or reset a bind parameter.  NAME should be the full parameter
19872     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
19873     ** VALUE can be in either SQL literal notation, or if not it will be
19874     ** understood to be a text string.
19875     */
19876     if( nArg==4 && strcmp(azArg[1],"set")==0 ){
19877       int rx;
19878       char *zSql;
19879       sqlite3_stmt *pStmt;
19880       const char *zKey = azArg[2];
19881       const char *zValue = azArg[3];
19882       bind_table_init(p);
19883       zSql = sqlite3_mprintf(
19884                   "REPLACE INTO temp.sqlite_parameters(key,value)"
19885                   "VALUES(%Q,%s);", zKey, zValue);
19886       if( zSql==0 ) shell_out_of_memory();
19887       pStmt = 0;
19888       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19889       sqlite3_free(zSql);
19890       if( rx!=SQLITE_OK ){
19891         sqlite3_finalize(pStmt);
19892         pStmt = 0;
19893         zSql = sqlite3_mprintf(
19894                    "REPLACE INTO temp.sqlite_parameters(key,value)"
19895                    "VALUES(%Q,%Q);", zKey, zValue);
19896         if( zSql==0 ) shell_out_of_memory();
19897         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19898         sqlite3_free(zSql);
19899         if( rx!=SQLITE_OK ){
19900           utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
19901           sqlite3_finalize(pStmt);
19902           pStmt = 0;
19903           rc = 1;
19904         }
19905       }
19906       sqlite3_step(pStmt);
19907       sqlite3_finalize(pStmt);
19908     }else
19909
19910     /* .parameter unset NAME
19911     ** Remove the NAME binding from the parameter binding table, if it
19912     ** exists.
19913     */
19914     if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
19915       char *zSql = sqlite3_mprintf(
19916           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
19917       if( zSql==0 ) shell_out_of_memory();
19918       sqlite3_exec(p->db, zSql, 0, 0, 0);
19919       sqlite3_free(zSql);
19920     }else
19921     /* If no command name matches, show a syntax error */
19922     parameter_syntax_error:
19923     showHelp(p->out, "parameter");
19924   }else
19925
19926   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
19927     int i;
19928     for(i=1; i<nArg; i++){
19929       if( i>1 ) raw_printf(p->out, " ");
19930       utf8_printf(p->out, "%s", azArg[i]);
19931     }
19932     raw_printf(p->out, "\n");
19933   }else
19934
19935 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19936   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
19937     int i;
19938     int nn = 0;
19939     p->flgProgress = 0;
19940     p->mxProgress = 0;
19941     p->nProgress = 0;
19942     for(i=1; i<nArg; i++){
19943       const char *z = azArg[i];
19944       if( z[0]=='-' ){
19945         z++;
19946         if( z[0]=='-' ) z++;
19947         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
19948           p->flgProgress |= SHELL_PROGRESS_QUIET;
19949           continue;
19950         }
19951         if( strcmp(z,"reset")==0 ){
19952           p->flgProgress |= SHELL_PROGRESS_RESET;
19953           continue;
19954         }
19955         if( strcmp(z,"once")==0 ){
19956           p->flgProgress |= SHELL_PROGRESS_ONCE;
19957           continue;
19958         }
19959         if( strcmp(z,"limit")==0 ){
19960           if( i+1>=nArg ){
19961             utf8_printf(stderr, "Error: missing argument on --limit\n");
19962             rc = 1;
19963             goto meta_command_exit;
19964           }else{
19965             p->mxProgress = (int)integerValue(azArg[++i]);
19966           }
19967           continue;
19968         }
19969         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
19970         rc = 1;
19971         goto meta_command_exit;
19972       }else{
19973         nn = (int)integerValue(z);
19974       }
19975     }
19976     open_db(p, 0);
19977     sqlite3_progress_handler(p->db, nn, progress_handler, p);
19978   }else
19979 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19980
19981   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
19982     if( nArg >= 2) {
19983       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
19984     }
19985     if( nArg >= 3) {
19986       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
19987     }
19988   }else
19989
19990   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
19991     rc = 2;
19992   }else
19993
19994   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
19995     FILE *inSaved = p->in;
19996     int savedLineno = p->lineno;
19997     if( nArg!=2 ){
19998       raw_printf(stderr, "Usage: .read FILE\n");
19999       rc = 1;
20000       goto meta_command_exit;
20001     }
20002     if( azArg[1][0]=='|' ){
20003 #ifdef SQLITE_OMIT_POPEN
20004       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20005       rc = 1;
20006       p->out = stdout;
20007 #else
20008       p->in = popen(azArg[1]+1, "r");
20009       if( p->in==0 ){
20010         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
20011         rc = 1;
20012       }else{
20013         rc = process_input(p);
20014         pclose(p->in);
20015       }
20016 #endif
20017     }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
20018       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
20019       rc = 1;
20020     }else{
20021       rc = process_input(p);
20022       fclose(p->in);
20023     }
20024     p->in = inSaved;
20025     p->lineno = savedLineno;
20026   }else
20027
20028   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
20029     const char *zSrcFile;
20030     const char *zDb;
20031     sqlite3 *pSrc;
20032     sqlite3_backup *pBackup;
20033     int nTimeout = 0;
20034
20035     if( nArg==2 ){
20036       zSrcFile = azArg[1];
20037       zDb = "main";
20038     }else if( nArg==3 ){
20039       zSrcFile = azArg[2];
20040       zDb = azArg[1];
20041     }else{
20042       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
20043       rc = 1;
20044       goto meta_command_exit;
20045     }
20046     rc = sqlite3_open(zSrcFile, &pSrc);
20047     if( rc!=SQLITE_OK ){
20048       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
20049       close_db(pSrc);
20050       return 1;
20051     }
20052     open_db(p, 0);
20053     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
20054     if( pBackup==0 ){
20055       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20056       close_db(pSrc);
20057       return 1;
20058     }
20059     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
20060           || rc==SQLITE_BUSY  ){
20061       if( rc==SQLITE_BUSY ){
20062         if( nTimeout++ >= 3 ) break;
20063         sqlite3_sleep(100);
20064       }
20065     }
20066     sqlite3_backup_finish(pBackup);
20067     if( rc==SQLITE_DONE ){
20068       rc = 0;
20069     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
20070       raw_printf(stderr, "Error: source database is busy\n");
20071       rc = 1;
20072     }else{
20073       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20074       rc = 1;
20075     }
20076     close_db(pSrc);
20077   }else
20078
20079   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
20080     if( nArg==2 ){
20081       p->scanstatsOn = (u8)booleanValue(azArg[1]);
20082 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
20083       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
20084 #endif
20085     }else{
20086       raw_printf(stderr, "Usage: .scanstats on|off\n");
20087       rc = 1;
20088     }
20089   }else
20090
20091   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
20092     ShellText sSelect;
20093     ShellState data;
20094     char *zErrMsg = 0;
20095     const char *zDiv = "(";
20096     const char *zName = 0;
20097     int iSchema = 0;
20098     int bDebug = 0;
20099     int bNoSystemTabs = 0;
20100     int ii;
20101
20102     open_db(p, 0);
20103     memcpy(&data, p, sizeof(data));
20104     data.showHeader = 0;
20105     data.cMode = data.mode = MODE_Semi;
20106     initText(&sSelect);
20107     for(ii=1; ii<nArg; ii++){
20108       if( optionMatch(azArg[ii],"indent") ){
20109         data.cMode = data.mode = MODE_Pretty;
20110       }else if( optionMatch(azArg[ii],"debug") ){
20111         bDebug = 1;
20112       }else if( optionMatch(azArg[ii],"nosys") ){
20113         bNoSystemTabs = 1;
20114       }else if( azArg[ii][0]=='-' ){
20115         utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
20116         rc = 1;
20117         goto meta_command_exit;
20118       }else if( zName==0 ){
20119         zName = azArg[ii];
20120       }else{
20121         raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
20122         rc = 1;
20123         goto meta_command_exit;
20124       }
20125     }
20126     if( zName!=0 ){
20127       int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
20128                   || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
20129                   || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
20130                   || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
20131       if( isSchema ){
20132         char *new_argv[2], *new_colv[2];
20133         new_argv[0] = sqlite3_mprintf(
20134                       "CREATE TABLE %s (\n"
20135                       "  type text,\n"
20136                       "  name text,\n"
20137                       "  tbl_name text,\n"
20138                       "  rootpage integer,\n"
20139                       "  sql text\n"
20140                       ")", zName);
20141         new_argv[1] = 0;
20142         new_colv[0] = "sql";
20143         new_colv[1] = 0;
20144         callback(&data, 1, new_argv, new_colv);
20145         sqlite3_free(new_argv[0]);
20146       }
20147     }
20148     if( zDiv ){
20149       sqlite3_stmt *pStmt = 0;
20150       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
20151                               -1, &pStmt, 0);
20152       if( rc ){
20153         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20154         sqlite3_finalize(pStmt);
20155         rc = 1;
20156         goto meta_command_exit;
20157       }
20158       appendText(&sSelect, "SELECT sql FROM", 0);
20159       iSchema = 0;
20160       while( sqlite3_step(pStmt)==SQLITE_ROW ){
20161         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
20162         char zScNum[30];
20163         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
20164         appendText(&sSelect, zDiv, 0);
20165         zDiv = " UNION ALL ";
20166         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
20167         if( sqlite3_stricmp(zDb, "main")!=0 ){
20168           appendText(&sSelect, zDb, '\'');
20169         }else{
20170           appendText(&sSelect, "NULL", 0);
20171         }
20172         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
20173         appendText(&sSelect, zScNum, 0);
20174         appendText(&sSelect, " AS snum, ", 0);
20175         appendText(&sSelect, zDb, '\'');
20176         appendText(&sSelect, " AS sname FROM ", 0);
20177         appendText(&sSelect, zDb, quoteChar(zDb));
20178         appendText(&sSelect, ".sqlite_schema", 0);
20179       }
20180       sqlite3_finalize(pStmt);
20181 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
20182       if( zName ){
20183         appendText(&sSelect,
20184            " UNION ALL SELECT shell_module_schema(name),"
20185            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
20186         0);
20187       }
20188 #endif
20189       appendText(&sSelect, ") WHERE ", 0);
20190       if( zName ){
20191         char *zQarg = sqlite3_mprintf("%Q", zName);
20192         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20193                     strchr(zName, '[') != 0;
20194         if( strchr(zName, '.') ){
20195           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
20196         }else{
20197           appendText(&sSelect, "lower(tbl_name)", 0);
20198         }
20199         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
20200         appendText(&sSelect, zQarg, 0);
20201         if( !bGlob ){
20202           appendText(&sSelect, " ESCAPE '\\' ", 0);
20203         }
20204         appendText(&sSelect, " AND ", 0);
20205         sqlite3_free(zQarg);
20206       }
20207       if( bNoSystemTabs ){
20208         appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
20209       }
20210       appendText(&sSelect, "sql IS NOT NULL"
20211                            " ORDER BY snum, rowid", 0);
20212       if( bDebug ){
20213         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
20214       }else{
20215         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
20216       }
20217       freeText(&sSelect);
20218     }
20219     if( zErrMsg ){
20220       utf8_printf(stderr,"Error: %s\n", zErrMsg);
20221       sqlite3_free(zErrMsg);
20222       rc = 1;
20223     }else if( rc != SQLITE_OK ){
20224       raw_printf(stderr,"Error: querying schema information\n");
20225       rc = 1;
20226     }else{
20227       rc = 0;
20228     }
20229   }else
20230
20231   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
20232     unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
20233     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
20234   }else
20235
20236 #if defined(SQLITE_ENABLE_SESSION)
20237   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
20238     OpenSession *pSession = &p->aSession[0];
20239     char **azCmd = &azArg[1];
20240     int iSes = 0;
20241     int nCmd = nArg - 1;
20242     int i;
20243     if( nArg<=1 ) goto session_syntax_error;
20244     open_db(p, 0);
20245     if( nArg>=3 ){
20246       for(iSes=0; iSes<p->nSession; iSes++){
20247         if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
20248       }
20249       if( iSes<p->nSession ){
20250         pSession = &p->aSession[iSes];
20251         azCmd++;
20252         nCmd--;
20253       }else{
20254         pSession = &p->aSession[0];
20255         iSes = 0;
20256       }
20257     }
20258
20259     /* .session attach TABLE
20260     ** Invoke the sqlite3session_attach() interface to attach a particular
20261     ** table so that it is never filtered.
20262     */
20263     if( strcmp(azCmd[0],"attach")==0 ){
20264       if( nCmd!=2 ) goto session_syntax_error;
20265       if( pSession->p==0 ){
20266         session_not_open:
20267         raw_printf(stderr, "ERROR: No sessions are open\n");
20268       }else{
20269         rc = sqlite3session_attach(pSession->p, azCmd[1]);
20270         if( rc ){
20271           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
20272           rc = 0;
20273         }
20274       }
20275     }else
20276
20277     /* .session changeset FILE
20278     ** .session patchset FILE
20279     ** Write a changeset or patchset into a file.  The file is overwritten.
20280     */
20281     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
20282       FILE *out = 0;
20283       if( nCmd!=2 ) goto session_syntax_error;
20284       if( pSession->p==0 ) goto session_not_open;
20285       out = fopen(azCmd[1], "wb");
20286       if( out==0 ){
20287         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
20288                     azCmd[1]);
20289       }else{
20290         int szChng;
20291         void *pChng;
20292         if( azCmd[0][0]=='c' ){
20293           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
20294         }else{
20295           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
20296         }
20297         if( rc ){
20298           printf("Error: error code %d\n", rc);
20299           rc = 0;
20300         }
20301         if( pChng
20302           && fwrite(pChng, szChng, 1, out)!=1 ){
20303           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
20304                   szChng);
20305         }
20306         sqlite3_free(pChng);
20307         fclose(out);
20308       }
20309     }else
20310
20311     /* .session close
20312     ** Close the identified session
20313     */
20314     if( strcmp(azCmd[0], "close")==0 ){
20315       if( nCmd!=1 ) goto session_syntax_error;
20316       if( p->nSession ){
20317         session_close(pSession);
20318         p->aSession[iSes] = p->aSession[--p->nSession];
20319       }
20320     }else
20321
20322     /* .session enable ?BOOLEAN?
20323     ** Query or set the enable flag
20324     */
20325     if( strcmp(azCmd[0], "enable")==0 ){
20326       int ii;
20327       if( nCmd>2 ) goto session_syntax_error;
20328       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20329       if( p->nSession ){
20330         ii = sqlite3session_enable(pSession->p, ii);
20331         utf8_printf(p->out, "session %s enable flag = %d\n",
20332                     pSession->zName, ii);
20333       }
20334     }else
20335
20336     /* .session filter GLOB ....
20337     ** Set a list of GLOB patterns of table names to be excluded.
20338     */
20339     if( strcmp(azCmd[0], "filter")==0 ){
20340       int ii, nByte;
20341       if( nCmd<2 ) goto session_syntax_error;
20342       if( p->nSession ){
20343         for(ii=0; ii<pSession->nFilter; ii++){
20344           sqlite3_free(pSession->azFilter[ii]);
20345         }
20346         sqlite3_free(pSession->azFilter);
20347         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
20348         pSession->azFilter = sqlite3_malloc( nByte );
20349         if( pSession->azFilter==0 ){
20350           raw_printf(stderr, "Error: out or memory\n");
20351           exit(1);
20352         }
20353         for(ii=1; ii<nCmd; ii++){
20354           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
20355         }
20356         pSession->nFilter = ii-1;
20357       }
20358     }else
20359
20360     /* .session indirect ?BOOLEAN?
20361     ** Query or set the indirect flag
20362     */
20363     if( strcmp(azCmd[0], "indirect")==0 ){
20364       int ii;
20365       if( nCmd>2 ) goto session_syntax_error;
20366       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20367       if( p->nSession ){
20368         ii = sqlite3session_indirect(pSession->p, ii);
20369         utf8_printf(p->out, "session %s indirect flag = %d\n",
20370                     pSession->zName, ii);
20371       }
20372     }else
20373
20374     /* .session isempty
20375     ** Determine if the session is empty
20376     */
20377     if( strcmp(azCmd[0], "isempty")==0 ){
20378       int ii;
20379       if( nCmd!=1 ) goto session_syntax_error;
20380       if( p->nSession ){
20381         ii = sqlite3session_isempty(pSession->p);
20382         utf8_printf(p->out, "session %s isempty flag = %d\n",
20383                     pSession->zName, ii);
20384       }
20385     }else
20386
20387     /* .session list
20388     ** List all currently open sessions
20389     */
20390     if( strcmp(azCmd[0],"list")==0 ){
20391       for(i=0; i<p->nSession; i++){
20392         utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
20393       }
20394     }else
20395
20396     /* .session open DB NAME
20397     ** Open a new session called NAME on the attached database DB.
20398     ** DB is normally "main".
20399     */
20400     if( strcmp(azCmd[0],"open")==0 ){
20401       char *zName;
20402       if( nCmd!=3 ) goto session_syntax_error;
20403       zName = azCmd[2];
20404       if( zName[0]==0 ) goto session_syntax_error;
20405       for(i=0; i<p->nSession; i++){
20406         if( strcmp(p->aSession[i].zName,zName)==0 ){
20407           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
20408           goto meta_command_exit;
20409         }
20410       }
20411       if( p->nSession>=ArraySize(p->aSession) ){
20412         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
20413         goto meta_command_exit;
20414       }
20415       pSession = &p->aSession[p->nSession];
20416       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
20417       if( rc ){
20418         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
20419         rc = 0;
20420         goto meta_command_exit;
20421       }
20422       pSession->nFilter = 0;
20423       sqlite3session_table_filter(pSession->p, session_filter, pSession);
20424       p->nSession++;
20425       pSession->zName = sqlite3_mprintf("%s", zName);
20426     }else
20427     /* If no command name matches, show a syntax error */
20428     session_syntax_error:
20429     showHelp(p->out, "session");
20430   }else
20431 #endif
20432
20433 #ifdef SQLITE_DEBUG
20434   /* Undocumented commands for internal testing.  Subject to change
20435   ** without notice. */
20436   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
20437     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
20438       int i, v;
20439       for(i=1; i<nArg; i++){
20440         v = booleanValue(azArg[i]);
20441         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
20442       }
20443     }
20444     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
20445       int i; sqlite3_int64 v;
20446       for(i=1; i<nArg; i++){
20447         char zBuf[200];
20448         v = integerValue(azArg[i]);
20449         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
20450         utf8_printf(p->out, "%s", zBuf);
20451       }
20452     }
20453   }else
20454 #endif
20455
20456   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
20457     int bIsInit = 0;         /* True to initialize the SELFTEST table */
20458     int bVerbose = 0;        /* Verbose output */
20459     int bSelftestExists;     /* True if SELFTEST already exists */
20460     int i, k;                /* Loop counters */
20461     int nTest = 0;           /* Number of tests runs */
20462     int nErr = 0;            /* Number of errors seen */
20463     ShellText str;           /* Answer for a query */
20464     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
20465
20466     open_db(p,0);
20467     for(i=1; i<nArg; i++){
20468       const char *z = azArg[i];
20469       if( z[0]=='-' && z[1]=='-' ) z++;
20470       if( strcmp(z,"-init")==0 ){
20471         bIsInit = 1;
20472       }else
20473       if( strcmp(z,"-v")==0 ){
20474         bVerbose++;
20475       }else
20476       {
20477         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20478                     azArg[i], azArg[0]);
20479         raw_printf(stderr, "Should be one of: --init -v\n");
20480         rc = 1;
20481         goto meta_command_exit;
20482       }
20483     }
20484     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
20485            != SQLITE_OK ){
20486       bSelftestExists = 0;
20487     }else{
20488       bSelftestExists = 1;
20489     }
20490     if( bIsInit ){
20491       createSelftestTable(p);
20492       bSelftestExists = 1;
20493     }
20494     initText(&str);
20495     appendText(&str, "x", 0);
20496     for(k=bSelftestExists; k>=0; k--){
20497       if( k==1 ){
20498         rc = sqlite3_prepare_v2(p->db,
20499             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
20500             -1, &pStmt, 0);
20501       }else{
20502         rc = sqlite3_prepare_v2(p->db,
20503           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
20504           "      (1,'run','PRAGMA integrity_check','ok')",
20505           -1, &pStmt, 0);
20506       }
20507       if( rc ){
20508         raw_printf(stderr, "Error querying the selftest table\n");
20509         rc = 1;
20510         sqlite3_finalize(pStmt);
20511         goto meta_command_exit;
20512       }
20513       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
20514         int tno = sqlite3_column_int(pStmt, 0);
20515         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
20516         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
20517         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
20518
20519         k = 0;
20520         if( bVerbose>0 ){
20521           char *zQuote = sqlite3_mprintf("%q", zSql);
20522           printf("%d: %s %s\n", tno, zOp, zSql);
20523           sqlite3_free(zQuote);
20524         }
20525         if( strcmp(zOp,"memo")==0 ){
20526           utf8_printf(p->out, "%s\n", zSql);
20527         }else
20528         if( strcmp(zOp,"run")==0 ){
20529           char *zErrMsg = 0;
20530           str.n = 0;
20531           str.z[0] = 0;
20532           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
20533           nTest++;
20534           if( bVerbose ){
20535             utf8_printf(p->out, "Result: %s\n", str.z);
20536           }
20537           if( rc || zErrMsg ){
20538             nErr++;
20539             rc = 1;
20540             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
20541             sqlite3_free(zErrMsg);
20542           }else if( strcmp(zAns,str.z)!=0 ){
20543             nErr++;
20544             rc = 1;
20545             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
20546             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
20547           }
20548         }else
20549         {
20550           utf8_printf(stderr,
20551             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
20552           rc = 1;
20553           break;
20554         }
20555       } /* End loop over rows of content from SELFTEST */
20556       sqlite3_finalize(pStmt);
20557     } /* End loop over k */
20558     freeText(&str);
20559     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
20560   }else
20561
20562   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
20563     if( nArg<2 || nArg>3 ){
20564       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
20565       rc = 1;
20566     }
20567     if( nArg>=2 ){
20568       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
20569                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
20570     }
20571     if( nArg>=3 ){
20572       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
20573                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
20574     }
20575   }else
20576
20577   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
20578     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
20579     int i;                   /* Loop counter */
20580     int bSchema = 0;         /* Also hash the schema */
20581     int bSeparate = 0;       /* Hash each table separately */
20582     int iSize = 224;         /* Hash algorithm to use */
20583     int bDebug = 0;          /* Only show the query that would have run */
20584     sqlite3_stmt *pStmt;     /* For querying tables names */
20585     char *zSql;              /* SQL to be run */
20586     char *zSep;              /* Separator */
20587     ShellText sSql;          /* Complete SQL for the query to run the hash */
20588     ShellText sQuery;        /* Set of queries used to read all content */
20589     open_db(p, 0);
20590     for(i=1; i<nArg; i++){
20591       const char *z = azArg[i];
20592       if( z[0]=='-' ){
20593         z++;
20594         if( z[0]=='-' ) z++;
20595         if( strcmp(z,"schema")==0 ){
20596           bSchema = 1;
20597         }else
20598         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
20599          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
20600         ){
20601           iSize = atoi(&z[5]);
20602         }else
20603         if( strcmp(z,"debug")==0 ){
20604           bDebug = 1;
20605         }else
20606         {
20607           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20608                       azArg[i], azArg[0]);
20609           showHelp(p->out, azArg[0]);
20610           rc = 1;
20611           goto meta_command_exit;
20612         }
20613       }else if( zLike ){
20614         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
20615         rc = 1;
20616         goto meta_command_exit;
20617       }else{
20618         zLike = z;
20619         bSeparate = 1;
20620         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
20621       }
20622     }
20623     if( bSchema ){
20624       zSql = "SELECT lower(name) FROM sqlite_schema"
20625              " WHERE type='table' AND coalesce(rootpage,0)>1"
20626              " UNION ALL SELECT 'sqlite_schema'"
20627              " ORDER BY 1 collate nocase";
20628     }else{
20629       zSql = "SELECT lower(name) FROM sqlite_schema"
20630              " WHERE type='table' AND coalesce(rootpage,0)>1"
20631              " AND name NOT LIKE 'sqlite_%'"
20632              " ORDER BY 1 collate nocase";
20633     }
20634     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20635     initText(&sQuery);
20636     initText(&sSql);
20637     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
20638     zSep = "VALUES(";
20639     while( SQLITE_ROW==sqlite3_step(pStmt) ){
20640       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
20641       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
20642       if( strncmp(zTab, "sqlite_",7)!=0 ){
20643         appendText(&sQuery,"SELECT * FROM ", 0);
20644         appendText(&sQuery,zTab,'"');
20645         appendText(&sQuery," NOT INDEXED;", 0);
20646       }else if( strcmp(zTab, "sqlite_schema")==0 ){
20647         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
20648                            " ORDER BY name;", 0);
20649       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
20650         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
20651                            " ORDER BY name;", 0);
20652       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
20653         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
20654                            " ORDER BY tbl,idx;", 0);
20655       }else if( strcmp(zTab, "sqlite_stat4")==0 ){
20656         appendText(&sQuery, "SELECT * FROM ", 0);
20657         appendText(&sQuery, zTab, 0);
20658         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
20659       }
20660       appendText(&sSql, zSep, 0);
20661       appendText(&sSql, sQuery.z, '\'');
20662       sQuery.n = 0;
20663       appendText(&sSql, ",", 0);
20664       appendText(&sSql, zTab, '\'');
20665       zSep = "),(";
20666     }
20667     sqlite3_finalize(pStmt);
20668     if( bSeparate ){
20669       zSql = sqlite3_mprintf(
20670           "%s))"
20671           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
20672           "   FROM [sha3sum$query]",
20673           sSql.z, iSize);
20674     }else{
20675       zSql = sqlite3_mprintf(
20676           "%s))"
20677           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
20678           "   FROM [sha3sum$query]",
20679           sSql.z, iSize);
20680     }
20681     freeText(&sQuery);
20682     freeText(&sSql);
20683     if( bDebug ){
20684       utf8_printf(p->out, "%s\n", zSql);
20685     }else{
20686       shell_exec(p, zSql, 0);
20687     }
20688     sqlite3_free(zSql);
20689   }else
20690
20691 #ifndef SQLITE_NOHAVE_SYSTEM
20692   if( c=='s'
20693    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
20694   ){
20695     char *zCmd;
20696     int i, x;
20697     if( nArg<2 ){
20698       raw_printf(stderr, "Usage: .system COMMAND\n");
20699       rc = 1;
20700       goto meta_command_exit;
20701     }
20702     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
20703     for(i=2; i<nArg; i++){
20704       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
20705                              zCmd, azArg[i]);
20706     }
20707     x = system(zCmd);
20708     sqlite3_free(zCmd);
20709     if( x ) raw_printf(stderr, "System command returns %d\n", x);
20710   }else
20711 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
20712
20713   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
20714     static const char *azBool[] = { "off", "on", "trigger", "full"};
20715     const char *zOut;
20716     int i;
20717     if( nArg!=1 ){
20718       raw_printf(stderr, "Usage: .show\n");
20719       rc = 1;
20720       goto meta_command_exit;
20721     }
20722     utf8_printf(p->out, "%12.12s: %s\n","echo",
20723                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
20724     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
20725     utf8_printf(p->out, "%12.12s: %s\n","explain",
20726          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
20727     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
20728     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
20729     utf8_printf(p->out, "%12.12s: ", "nullvalue");
20730       output_c_string(p->out, p->nullValue);
20731       raw_printf(p->out, "\n");
20732     utf8_printf(p->out,"%12.12s: %s\n","output",
20733             strlen30(p->outfile) ? p->outfile : "stdout");
20734     utf8_printf(p->out,"%12.12s: ", "colseparator");
20735       output_c_string(p->out, p->colSeparator);
20736       raw_printf(p->out, "\n");
20737     utf8_printf(p->out,"%12.12s: ", "rowseparator");
20738       output_c_string(p->out, p->rowSeparator);
20739       raw_printf(p->out, "\n");
20740     switch( p->statsOn ){
20741       case 0:  zOut = "off";     break;
20742       default: zOut = "on";      break;
20743       case 2:  zOut = "stmt";    break;
20744       case 3:  zOut = "vmstep";  break;
20745     }
20746     utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
20747     utf8_printf(p->out, "%12.12s: ", "width");
20748     for (i=0;i<p->nWidth;i++) {
20749       raw_printf(p->out, "%d ", p->colWidth[i]);
20750     }
20751     raw_printf(p->out, "\n");
20752     utf8_printf(p->out, "%12.12s: %s\n", "filename",
20753                 p->zDbFilename ? p->zDbFilename : "");
20754   }else
20755
20756   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
20757     if( nArg==2 ){
20758       if( strcmp(azArg[1],"stmt")==0 ){
20759         p->statsOn = 2;
20760       }else if( strcmp(azArg[1],"vmstep")==0 ){
20761         p->statsOn = 3;
20762       }else{
20763         p->statsOn = (u8)booleanValue(azArg[1]);
20764       }
20765     }else if( nArg==1 ){
20766       display_stats(p->db, p, 0);
20767     }else{
20768       raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
20769       rc = 1;
20770     }
20771   }else
20772
20773   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
20774    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
20775                  || strncmp(azArg[0], "indexes", n)==0) )
20776   ){
20777     sqlite3_stmt *pStmt;
20778     char **azResult;
20779     int nRow, nAlloc;
20780     int ii;
20781     ShellText s;
20782     initText(&s);
20783     open_db(p, 0);
20784     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
20785     if( rc ){
20786       sqlite3_finalize(pStmt);
20787       return shellDatabaseError(p->db);
20788     }
20789
20790     if( nArg>2 && c=='i' ){
20791       /* It is an historical accident that the .indexes command shows an error
20792       ** when called with the wrong number of arguments whereas the .tables
20793       ** command does not. */
20794       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
20795       rc = 1;
20796       sqlite3_finalize(pStmt);
20797       goto meta_command_exit;
20798     }
20799     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
20800       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
20801       if( zDbName==0 ) continue;
20802       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
20803       if( sqlite3_stricmp(zDbName, "main")==0 ){
20804         appendText(&s, "SELECT name FROM ", 0);
20805       }else{
20806         appendText(&s, "SELECT ", 0);
20807         appendText(&s, zDbName, '\'');
20808         appendText(&s, "||'.'||name FROM ", 0);
20809       }
20810       appendText(&s, zDbName, '"');
20811       appendText(&s, ".sqlite_schema ", 0);
20812       if( c=='t' ){
20813         appendText(&s," WHERE type IN ('table','view')"
20814                       "   AND name NOT LIKE 'sqlite_%'"
20815                       "   AND name LIKE ?1", 0);
20816       }else{
20817         appendText(&s," WHERE type='index'"
20818                       "   AND tbl_name LIKE ?1", 0);
20819       }
20820     }
20821     rc = sqlite3_finalize(pStmt);
20822     appendText(&s, " ORDER BY 1", 0);
20823     rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
20824     freeText(&s);
20825     if( rc ) return shellDatabaseError(p->db);
20826
20827     /* Run the SQL statement prepared by the above block. Store the results
20828     ** as an array of nul-terminated strings in azResult[].  */
20829     nRow = nAlloc = 0;
20830     azResult = 0;
20831     if( nArg>1 ){
20832       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
20833     }else{
20834       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
20835     }
20836     while( sqlite3_step(pStmt)==SQLITE_ROW ){
20837       if( nRow>=nAlloc ){
20838         char **azNew;
20839         int n2 = nAlloc*2 + 10;
20840         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
20841         if( azNew==0 ) shell_out_of_memory();
20842         nAlloc = n2;
20843         azResult = azNew;
20844       }
20845       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
20846       if( 0==azResult[nRow] ) shell_out_of_memory();
20847       nRow++;
20848     }
20849     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
20850       rc = shellDatabaseError(p->db);
20851     }
20852
20853     /* Pretty-print the contents of array azResult[] to the output */
20854     if( rc==0 && nRow>0 ){
20855       int len, maxlen = 0;
20856       int i, j;
20857       int nPrintCol, nPrintRow;
20858       for(i=0; i<nRow; i++){
20859         len = strlen30(azResult[i]);
20860         if( len>maxlen ) maxlen = len;
20861       }
20862       nPrintCol = 80/(maxlen+2);
20863       if( nPrintCol<1 ) nPrintCol = 1;
20864       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
20865       for(i=0; i<nPrintRow; i++){
20866         for(j=i; j<nRow; j+=nPrintRow){
20867           char *zSp = j<nPrintRow ? "" : "  ";
20868           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
20869                       azResult[j] ? azResult[j]:"");
20870         }
20871         raw_printf(p->out, "\n");
20872       }
20873     }
20874
20875     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
20876     sqlite3_free(azResult);
20877   }else
20878
20879   /* Begin redirecting output to the file "testcase-out.txt" */
20880   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
20881     output_reset(p);
20882     p->out = output_file_open("testcase-out.txt", 0);
20883     if( p->out==0 ){
20884       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
20885     }
20886     if( nArg>=2 ){
20887       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
20888     }else{
20889       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
20890     }
20891   }else
20892
20893 #ifndef SQLITE_UNTESTABLE
20894   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
20895     static const struct {
20896        const char *zCtrlName;   /* Name of a test-control option */
20897        int ctrlCode;            /* Integer code for that option */
20898        const char *zUsage;      /* Usage notes */
20899     } aCtrl[] = {
20900       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
20901       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
20902     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
20903     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
20904       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
20905       { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
20906     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
20907       { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
20908       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
20909       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
20910       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
20911       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
20912 #ifdef YYCOVERAGE
20913       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
20914 #endif
20915       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
20916       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
20917       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
20918       { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
20919       { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
20920       { "tune",               SQLITE_TESTCTRL_TUNE,          "ID VALUE"       },
20921     };
20922     int testctrl = -1;
20923     int iCtrl = -1;
20924     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
20925     int isOk = 0;
20926     int i, n2;
20927     const char *zCmd = 0;
20928
20929     open_db(p, 0);
20930     zCmd = nArg>=2 ? azArg[1] : "help";
20931
20932     /* The argument can optionally begin with "-" or "--" */
20933     if( zCmd[0]=='-' && zCmd[1] ){
20934       zCmd++;
20935       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
20936     }
20937
20938     /* --help lists all test-controls */
20939     if( strcmp(zCmd,"help")==0 ){
20940       utf8_printf(p->out, "Available test-controls:\n");
20941       for(i=0; i<ArraySize(aCtrl); i++){
20942         utf8_printf(p->out, "  .testctrl %s %s\n",
20943                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
20944       }
20945       rc = 1;
20946       goto meta_command_exit;
20947     }
20948
20949     /* convert testctrl text option to value. allow any unique prefix
20950     ** of the option name, or a numerical value. */
20951     n2 = strlen30(zCmd);
20952     for(i=0; i<ArraySize(aCtrl); i++){
20953       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
20954         if( testctrl<0 ){
20955           testctrl = aCtrl[i].ctrlCode;
20956           iCtrl = i;
20957         }else{
20958           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
20959                               "Use \".testctrl --help\" for help\n", zCmd);
20960           rc = 1;
20961           goto meta_command_exit;
20962         }
20963       }
20964     }
20965     if( testctrl<0 ){
20966       utf8_printf(stderr,"Error: unknown test-control: %s\n"
20967                          "Use \".testctrl --help\" for help\n", zCmd);
20968     }else{
20969       switch(testctrl){
20970
20971         /* sqlite3_test_control(int, db, int) */
20972         case SQLITE_TESTCTRL_OPTIMIZATIONS:
20973           if( nArg==3 ){
20974             unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
20975             rc2 = sqlite3_test_control(testctrl, p->db, opt);
20976             isOk = 3;
20977           }
20978           break;
20979
20980         /* sqlite3_test_control(int) */
20981         case SQLITE_TESTCTRL_PRNG_SAVE:
20982         case SQLITE_TESTCTRL_PRNG_RESTORE:
20983         case SQLITE_TESTCTRL_BYTEORDER:
20984           if( nArg==2 ){
20985             rc2 = sqlite3_test_control(testctrl);
20986             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
20987           }
20988           break;
20989
20990         /* sqlite3_test_control(int, uint) */
20991         case SQLITE_TESTCTRL_PENDING_BYTE:
20992           if( nArg==3 ){
20993             unsigned int opt = (unsigned int)integerValue(azArg[2]);
20994             rc2 = sqlite3_test_control(testctrl, opt);
20995             isOk = 3;
20996           }
20997           break;
20998
20999         /* sqlite3_test_control(int, int, sqlite3*) */
21000         case SQLITE_TESTCTRL_PRNG_SEED:
21001           if( nArg==3 || nArg==4 ){
21002             int ii = (int)integerValue(azArg[2]);
21003             sqlite3 *db;
21004             if( ii==0 && strcmp(azArg[2],"random")==0 ){
21005               sqlite3_randomness(sizeof(ii),&ii);
21006               printf("-- random seed: %d\n", ii);
21007             }
21008             if( nArg==3 ){
21009               db = 0;
21010             }else{
21011               db = p->db;
21012               /* Make sure the schema has been loaded */
21013               sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
21014             }
21015             rc2 = sqlite3_test_control(testctrl, ii, db);
21016             isOk = 3;
21017           }
21018           break;
21019
21020         /* sqlite3_test_control(int, int) */
21021         case SQLITE_TESTCTRL_ASSERT:
21022         case SQLITE_TESTCTRL_ALWAYS:
21023           if( nArg==3 ){
21024             int opt = booleanValue(azArg[2]);
21025             rc2 = sqlite3_test_control(testctrl, opt);
21026             isOk = 1;
21027           }
21028           break;
21029
21030         /* sqlite3_test_control(int, int) */
21031         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
21032         case SQLITE_TESTCTRL_NEVER_CORRUPT:
21033           if( nArg==3 ){
21034             int opt = booleanValue(azArg[2]);
21035             rc2 = sqlite3_test_control(testctrl, opt);
21036             isOk = 3;
21037           }
21038           break;
21039
21040         /* sqlite3_test_control(sqlite3*) */
21041         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
21042           rc2 = sqlite3_test_control(testctrl, p->db);
21043           isOk = 3;
21044           break;
21045
21046         case SQLITE_TESTCTRL_IMPOSTER:
21047           if( nArg==5 ){
21048             rc2 = sqlite3_test_control(testctrl, p->db,
21049                           azArg[2],
21050                           integerValue(azArg[3]),
21051                           integerValue(azArg[4]));
21052             isOk = 3;
21053           }
21054           break;
21055
21056         case SQLITE_TESTCTRL_SEEK_COUNT: {
21057           u64 x = 0;
21058           rc2 = sqlite3_test_control(testctrl, p->db, &x);
21059           utf8_printf(p->out, "%llu\n", x);
21060           isOk = 3;
21061           break;
21062         }
21063
21064 #ifdef YYCOVERAGE
21065         case SQLITE_TESTCTRL_PARSER_COVERAGE: {
21066           if( nArg==2 ){
21067             sqlite3_test_control(testctrl, p->out);
21068             isOk = 3;
21069           }
21070           break;
21071         }
21072 #endif
21073 #ifdef SQLITE_DEBUG
21074         case SQLITE_TESTCTRL_TUNE: {
21075           if( nArg==4 ){
21076             int id = (int)integerValue(azArg[2]);
21077             int val = (int)integerValue(azArg[3]);
21078             sqlite3_test_control(testctrl, id, &val);
21079             isOk = 3;
21080           }else if( nArg==3 ){
21081             int id = (int)integerValue(azArg[2]);
21082             sqlite3_test_control(testctrl, -id, &rc2);
21083             isOk = 1;
21084           }else if( nArg==2 ){
21085             int id = 1;
21086             while(1){
21087               int val = 0;
21088               rc2 = sqlite3_test_control(testctrl, -id, &val);
21089               if( rc2!=SQLITE_OK ) break;
21090               if( id>1 ) utf8_printf(p->out, "  ");
21091               utf8_printf(p->out, "%d: %d", id, val);
21092               id++;
21093             }
21094             if( id>1 ) utf8_printf(p->out, "\n");
21095             isOk = 3;
21096           }
21097           break;
21098         }
21099 #endif
21100       }
21101     }
21102     if( isOk==0 && iCtrl>=0 ){
21103       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
21104       rc = 1;
21105     }else if( isOk==1 ){
21106       raw_printf(p->out, "%d\n", rc2);
21107     }else if( isOk==2 ){
21108       raw_printf(p->out, "0x%08x\n", rc2);
21109     }
21110   }else
21111 #endif /* !defined(SQLITE_UNTESTABLE) */
21112
21113   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
21114     open_db(p, 0);
21115     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
21116   }else
21117
21118   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
21119     if( nArg==2 ){
21120       enableTimer = booleanValue(azArg[1]);
21121       if( enableTimer && !HAS_TIMER ){
21122         raw_printf(stderr, "Error: timer not available on this system.\n");
21123         enableTimer = 0;
21124       }
21125     }else{
21126       raw_printf(stderr, "Usage: .timer on|off\n");
21127       rc = 1;
21128     }
21129   }else
21130
21131 #ifndef SQLITE_OMIT_TRACE
21132   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
21133     int mType = 0;
21134     int jj;
21135     open_db(p, 0);
21136     for(jj=1; jj<nArg; jj++){
21137       const char *z = azArg[jj];
21138       if( z[0]=='-' ){
21139         if( optionMatch(z, "expanded") ){
21140           p->eTraceType = SHELL_TRACE_EXPANDED;
21141         }
21142 #ifdef SQLITE_ENABLE_NORMALIZE
21143         else if( optionMatch(z, "normalized") ){
21144           p->eTraceType = SHELL_TRACE_NORMALIZED;
21145         }
21146 #endif
21147         else if( optionMatch(z, "plain") ){
21148           p->eTraceType = SHELL_TRACE_PLAIN;
21149         }
21150         else if( optionMatch(z, "profile") ){
21151           mType |= SQLITE_TRACE_PROFILE;
21152         }
21153         else if( optionMatch(z, "row") ){
21154           mType |= SQLITE_TRACE_ROW;
21155         }
21156         else if( optionMatch(z, "stmt") ){
21157           mType |= SQLITE_TRACE_STMT;
21158         }
21159         else if( optionMatch(z, "close") ){
21160           mType |= SQLITE_TRACE_CLOSE;
21161         }
21162         else {
21163           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
21164           rc = 1;
21165           goto meta_command_exit;
21166         }
21167       }else{
21168         output_file_close(p->traceOut);
21169         p->traceOut = output_file_open(azArg[1], 0);
21170       }
21171     }
21172     if( p->traceOut==0 ){
21173       sqlite3_trace_v2(p->db, 0, 0, 0);
21174     }else{
21175       if( mType==0 ) mType = SQLITE_TRACE_STMT;
21176       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
21177     }
21178   }else
21179 #endif /* !defined(SQLITE_OMIT_TRACE) */
21180
21181 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
21182   if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
21183     int ii;
21184     int lenOpt;
21185     char *zOpt;
21186     if( nArg<2 ){
21187       raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
21188       rc = 1;
21189       goto meta_command_exit;
21190     }
21191     open_db(p, 0);
21192     zOpt = azArg[1];
21193     if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
21194     lenOpt = (int)strlen(zOpt);
21195     if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
21196       assert( azArg[nArg]==0 );
21197       sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
21198     }else{
21199       for(ii=1; ii<nArg; ii++){
21200         sqlite3_create_module(p->db, azArg[ii], 0, 0);
21201       }
21202     }
21203   }else
21204 #endif
21205
21206 #if SQLITE_USER_AUTHENTICATION
21207   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
21208     if( nArg<2 ){
21209       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
21210       rc = 1;
21211       goto meta_command_exit;
21212     }
21213     open_db(p, 0);
21214     if( strcmp(azArg[1],"login")==0 ){
21215       if( nArg!=4 ){
21216         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
21217         rc = 1;
21218         goto meta_command_exit;
21219       }
21220       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
21221                                      strlen30(azArg[3]));
21222       if( rc ){
21223         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
21224         rc = 1;
21225       }
21226     }else if( strcmp(azArg[1],"add")==0 ){
21227       if( nArg!=5 ){
21228         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
21229         rc = 1;
21230         goto meta_command_exit;
21231       }
21232       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21233                             booleanValue(azArg[4]));
21234       if( rc ){
21235         raw_printf(stderr, "User-Add failed: %d\n", rc);
21236         rc = 1;
21237       }
21238     }else if( strcmp(azArg[1],"edit")==0 ){
21239       if( nArg!=5 ){
21240         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
21241         rc = 1;
21242         goto meta_command_exit;
21243       }
21244       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21245                               booleanValue(azArg[4]));
21246       if( rc ){
21247         raw_printf(stderr, "User-Edit failed: %d\n", rc);
21248         rc = 1;
21249       }
21250     }else if( strcmp(azArg[1],"delete")==0 ){
21251       if( nArg!=3 ){
21252         raw_printf(stderr, "Usage: .user delete USER\n");
21253         rc = 1;
21254         goto meta_command_exit;
21255       }
21256       rc = sqlite3_user_delete(p->db, azArg[2]);
21257       if( rc ){
21258         raw_printf(stderr, "User-Delete failed: %d\n", rc);
21259         rc = 1;
21260       }
21261     }else{
21262       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
21263       rc = 1;
21264       goto meta_command_exit;
21265     }
21266   }else
21267 #endif /* SQLITE_USER_AUTHENTICATION */
21268
21269   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
21270     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
21271         sqlite3_libversion(), sqlite3_sourceid());
21272 #if SQLITE_HAVE_ZLIB
21273     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
21274 #endif
21275 #define CTIMEOPT_VAL_(opt) #opt
21276 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
21277 #if defined(__clang__) && defined(__clang_major__)
21278     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
21279                     CTIMEOPT_VAL(__clang_minor__) "."
21280                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
21281 #elif defined(_MSC_VER)
21282     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
21283 #elif defined(__GNUC__) && defined(__VERSION__)
21284     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
21285 #endif
21286   }else
21287
21288   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
21289     const char *zDbName = nArg==2 ? azArg[1] : "main";
21290     sqlite3_vfs *pVfs = 0;
21291     if( p->db ){
21292       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
21293       if( pVfs ){
21294         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
21295         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
21296         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
21297         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21298       }
21299     }
21300   }else
21301
21302   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
21303     sqlite3_vfs *pVfs;
21304     sqlite3_vfs *pCurrent = 0;
21305     if( p->db ){
21306       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
21307     }
21308     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
21309       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
21310            pVfs==pCurrent ? "  <--- CURRENT" : "");
21311       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
21312       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
21313       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21314       if( pVfs->pNext ){
21315         raw_printf(p->out, "-----------------------------------\n");
21316       }
21317     }
21318   }else
21319
21320   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
21321     const char *zDbName = nArg==2 ? azArg[1] : "main";
21322     char *zVfsName = 0;
21323     if( p->db ){
21324       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
21325       if( zVfsName ){
21326         utf8_printf(p->out, "%s\n", zVfsName);
21327         sqlite3_free(zVfsName);
21328       }
21329     }
21330   }else
21331
21332   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
21333     unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
21334     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
21335   }else
21336
21337   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
21338     int j;
21339     assert( nArg<=ArraySize(azArg) );
21340     p->nWidth = nArg-1;
21341     p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
21342     if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
21343     if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
21344     for(j=1; j<nArg; j++){
21345       p->colWidth[j-1] = (int)integerValue(azArg[j]);
21346     }
21347   }else
21348
21349   {
21350     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
21351       " \"%s\". Enter \".help\" for help\n", azArg[0]);
21352     rc = 1;
21353   }
21354
21355 meta_command_exit:
21356   if( p->outCount ){
21357     p->outCount--;
21358     if( p->outCount==0 ) output_reset(p);
21359   }
21360   return rc;
21361 }
21362
21363 /*
21364 ** Return TRUE if a semicolon occurs anywhere in the first N characters
21365 ** of string z[].
21366 */
21367 static int line_contains_semicolon(const char *z, int N){
21368   int i;
21369   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
21370   return 0;
21371 }
21372
21373 /*
21374 ** Test to see if a line consists entirely of whitespace.
21375 */
21376 static int _all_whitespace(const char *z){
21377   for(; *z; z++){
21378     if( IsSpace(z[0]) ) continue;
21379     if( *z=='/' && z[1]=='*' ){
21380       z += 2;
21381       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
21382       if( *z==0 ) return 0;
21383       z++;
21384       continue;
21385     }
21386     if( *z=='-' && z[1]=='-' ){
21387       z += 2;
21388       while( *z && *z!='\n' ){ z++; }
21389       if( *z==0 ) return 1;
21390       continue;
21391     }
21392     return 0;
21393   }
21394   return 1;
21395 }
21396
21397 /*
21398 ** Return TRUE if the line typed in is an SQL command terminator other
21399 ** than a semi-colon.  The SQL Server style "go" command is understood
21400 ** as is the Oracle "/".
21401 */
21402 static int line_is_command_terminator(const char *zLine){
21403   while( IsSpace(zLine[0]) ){ zLine++; };
21404   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
21405     return 1;  /* Oracle */
21406   }
21407   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
21408          && _all_whitespace(&zLine[2]) ){
21409     return 1;  /* SQL Server */
21410   }
21411   return 0;
21412 }
21413
21414 /*
21415 ** We need a default sqlite3_complete() implementation to use in case
21416 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
21417 ** any arbitrary text is a complete SQL statement.  This is not very
21418 ** user-friendly, but it does seem to work.
21419 */
21420 #ifdef SQLITE_OMIT_COMPLETE
21421 #define sqlite3_complete(x) 1
21422 #endif
21423
21424 /*
21425 ** Return true if zSql is a complete SQL statement.  Return false if it
21426 ** ends in the middle of a string literal or C-style comment.
21427 */
21428 static int line_is_complete(char *zSql, int nSql){
21429   int rc;
21430   if( zSql==0 ) return 1;
21431   zSql[nSql] = ';';
21432   zSql[nSql+1] = 0;
21433   rc = sqlite3_complete(zSql);
21434   zSql[nSql] = 0;
21435   return rc;
21436 }
21437
21438 /*
21439 ** Run a single line of SQL.  Return the number of errors.
21440 */
21441 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
21442   int rc;
21443   char *zErrMsg = 0;
21444
21445   open_db(p, 0);
21446   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
21447   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
21448   BEGIN_TIMER;
21449   rc = shell_exec(p, zSql, &zErrMsg);
21450   END_TIMER;
21451   if( rc || zErrMsg ){
21452     char zPrefix[100];
21453     if( in!=0 || !stdin_is_interactive ){
21454       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
21455                        "Error: near line %d:", startline);
21456     }else{
21457       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
21458     }
21459     if( zErrMsg!=0 ){
21460       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
21461       sqlite3_free(zErrMsg);
21462       zErrMsg = 0;
21463     }else{
21464       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
21465     }
21466     return 1;
21467   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
21468     raw_printf(p->out, "changes: %3d   total_changes: %d\n",
21469             sqlite3_changes(p->db), sqlite3_total_changes(p->db));
21470   }
21471   return 0;
21472 }
21473
21474
21475 /*
21476 ** Read input from *in and process it.  If *in==0 then input
21477 ** is interactive - the user is typing it it.  Otherwise, input
21478 ** is coming from a file or device.  A prompt is issued and history
21479 ** is saved only if input is interactive.  An interrupt signal will
21480 ** cause this routine to exit immediately, unless input is interactive.
21481 **
21482 ** Return the number of errors.
21483 */
21484 static int process_input(ShellState *p){
21485   char *zLine = 0;          /* A single input line */
21486   char *zSql = 0;           /* Accumulated SQL text */
21487   int nLine;                /* Length of current line */
21488   int nSql = 0;             /* Bytes of zSql[] used */
21489   int nAlloc = 0;           /* Allocated zSql[] space */
21490   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
21491   int rc;                   /* Error code */
21492   int errCnt = 0;           /* Number of errors seen */
21493   int startline = 0;        /* Line number for start of current input */
21494
21495   p->lineno = 0;
21496   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
21497     fflush(p->out);
21498     zLine = one_input_line(p->in, zLine, nSql>0);
21499     if( zLine==0 ){
21500       /* End of input */
21501       if( p->in==0 && stdin_is_interactive ) printf("\n");
21502       break;
21503     }
21504     if( seenInterrupt ){
21505       if( p->in!=0 ) break;
21506       seenInterrupt = 0;
21507     }
21508     p->lineno++;
21509     if( nSql==0 && _all_whitespace(zLine) ){
21510       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
21511       continue;
21512     }
21513     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
21514       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
21515       if( zLine[0]=='.' ){
21516         rc = do_meta_command(zLine, p);
21517         if( rc==2 ){ /* exit requested */
21518           break;
21519         }else if( rc ){
21520           errCnt++;
21521         }
21522       }
21523       continue;
21524     }
21525     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
21526       memcpy(zLine,";",2);
21527     }
21528     nLine = strlen30(zLine);
21529     if( nSql+nLine+2>=nAlloc ){
21530       nAlloc = nSql+nLine+100;
21531       zSql = realloc(zSql, nAlloc);
21532       if( zSql==0 ) shell_out_of_memory();
21533     }
21534     nSqlPrior = nSql;
21535     if( nSql==0 ){
21536       int i;
21537       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
21538       assert( nAlloc>0 && zSql!=0 );
21539       memcpy(zSql, zLine+i, nLine+1-i);
21540       startline = p->lineno;
21541       nSql = nLine-i;
21542     }else{
21543       zSql[nSql++] = '\n';
21544       memcpy(zSql+nSql, zLine, nLine+1);
21545       nSql += nLine;
21546     }
21547     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
21548                 && sqlite3_complete(zSql) ){
21549       errCnt += runOneSqlLine(p, zSql, p->in, startline);
21550       nSql = 0;
21551       if( p->outCount ){
21552         output_reset(p);
21553         p->outCount = 0;
21554       }else{
21555         clearTempFile(p);
21556       }
21557     }else if( nSql && _all_whitespace(zSql) ){
21558       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
21559       nSql = 0;
21560     }
21561   }
21562   if( nSql && !_all_whitespace(zSql) ){
21563     errCnt += runOneSqlLine(p, zSql, p->in, startline);
21564   }
21565   free(zSql);
21566   free(zLine);
21567   return errCnt>0;
21568 }
21569
21570 /*
21571 ** Return a pathname which is the user's home directory.  A
21572 ** 0 return indicates an error of some kind.
21573 */
21574 static char *find_home_dir(int clearFlag){
21575   static char *home_dir = NULL;
21576   if( clearFlag ){
21577     free(home_dir);
21578     home_dir = 0;
21579     return 0;
21580   }
21581   if( home_dir ) return home_dir;
21582
21583 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
21584      && !defined(__RTP__) && !defined(_WRS_KERNEL)
21585   {
21586     struct passwd *pwent;
21587     uid_t uid = getuid();
21588     if( (pwent=getpwuid(uid)) != NULL) {
21589       home_dir = pwent->pw_dir;
21590     }
21591   }
21592 #endif
21593
21594 #if defined(_WIN32_WCE)
21595   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
21596    */
21597   home_dir = "/";
21598 #else
21599
21600 #if defined(_WIN32) || defined(WIN32)
21601   if (!home_dir) {
21602     home_dir = getenv("USERPROFILE");
21603   }
21604 #endif
21605
21606   if (!home_dir) {
21607     home_dir = getenv("HOME");
21608   }
21609
21610 #if defined(_WIN32) || defined(WIN32)
21611   if (!home_dir) {
21612     char *zDrive, *zPath;
21613     int n;
21614     zDrive = getenv("HOMEDRIVE");
21615     zPath = getenv("HOMEPATH");
21616     if( zDrive && zPath ){
21617       n = strlen30(zDrive) + strlen30(zPath) + 1;
21618       home_dir = malloc( n );
21619       if( home_dir==0 ) return 0;
21620       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
21621       return home_dir;
21622     }
21623     home_dir = "c:\\";
21624   }
21625 #endif
21626
21627 #endif /* !_WIN32_WCE */
21628
21629   if( home_dir ){
21630     int n = strlen30(home_dir) + 1;
21631     char *z = malloc( n );
21632     if( z ) memcpy(z, home_dir, n);
21633     home_dir = z;
21634   }
21635
21636   return home_dir;
21637 }
21638
21639 /*
21640 ** Read input from the file given by sqliterc_override.  Or if that
21641 ** parameter is NULL, take input from ~/.sqliterc
21642 **
21643 ** Returns the number of errors.
21644 */
21645 static void process_sqliterc(
21646   ShellState *p,                  /* Configuration data */
21647   const char *sqliterc_override   /* Name of config file. NULL to use default */
21648 ){
21649   char *home_dir = NULL;
21650   const char *sqliterc = sqliterc_override;
21651   char *zBuf = 0;
21652   FILE *inSaved = p->in;
21653   int savedLineno = p->lineno;
21654
21655   if (sqliterc == NULL) {
21656     home_dir = find_home_dir(0);
21657     if( home_dir==0 ){
21658       raw_printf(stderr, "-- warning: cannot find home directory;"
21659                       " cannot read ~/.sqliterc\n");
21660       return;
21661     }
21662     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
21663     sqliterc = zBuf;
21664   }
21665   p->in = fopen(sqliterc,"rb");
21666   if( p->in ){
21667     if( stdin_is_interactive ){
21668       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
21669     }
21670     if( process_input(p) && bail_on_error ) exit(1);
21671     fclose(p->in);
21672   }else if( sqliterc_override!=0 ){
21673     utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
21674     if( bail_on_error ) exit(1);
21675   }
21676   p->in = inSaved;
21677   p->lineno = savedLineno;
21678   sqlite3_free(zBuf);
21679 }
21680
21681 /*
21682 ** Show available command line options
21683 */
21684 static const char zOptions[] =
21685 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
21686   "   -A ARGS...           run \".archive ARGS\" and exit\n"
21687 #endif
21688   "   -append              append the database to the end of the file\n"
21689   "   -ascii               set output mode to 'ascii'\n"
21690   "   -bail                stop after hitting an error\n"
21691   "   -batch               force batch I/O\n"
21692   "   -box                 set output mode to 'box'\n"
21693   "   -column              set output mode to 'column'\n"
21694   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
21695   "   -csv                 set output mode to 'csv'\n"
21696 #if !defined(SQLITE_OMIT_DESERIALIZE)
21697   "   -deserialize         open the database using sqlite3_deserialize()\n"
21698 #endif
21699   "   -echo                print commands before execution\n"
21700   "   -init FILENAME       read/process named file\n"
21701   "   -[no]header          turn headers on or off\n"
21702 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
21703   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
21704 #endif
21705   "   -help                show this message\n"
21706   "   -html                set output mode to HTML\n"
21707   "   -interactive         force interactive I/O\n"
21708   "   -json                set output mode to 'json'\n"
21709   "   -line                set output mode to 'line'\n"
21710   "   -list                set output mode to 'list'\n"
21711   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
21712   "   -markdown            set output mode to 'markdown'\n"
21713 #if !defined(SQLITE_OMIT_DESERIALIZE)
21714   "   -maxsize N           maximum size for a --deserialize database\n"
21715 #endif
21716   "   -memtrace            trace all memory allocations and deallocations\n"
21717   "   -mmap N              default mmap size set to N\n"
21718 #ifdef SQLITE_ENABLE_MULTIPLEX
21719   "   -multiplex           enable the multiplexor VFS\n"
21720 #endif
21721   "   -newline SEP         set output row separator. Default: '\\n'\n"
21722   "   -nofollow            refuse to open symbolic links to database files\n"
21723   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
21724   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
21725   "   -quote               set output mode to 'quote'\n"
21726   "   -readonly            open the database read-only\n"
21727   "   -separator SEP       set output column separator. Default: '|'\n"
21728 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21729   "   -sorterref SIZE      sorter references threshold size\n"
21730 #endif
21731   "   -stats               print memory stats before each finalize\n"
21732   "   -table               set output mode to 'table'\n"
21733   "   -tabs                set output mode to 'tabs'\n"
21734   "   -version             show SQLite version\n"
21735   "   -vfs NAME            use NAME as the default VFS\n"
21736 #ifdef SQLITE_ENABLE_VFSTRACE
21737   "   -vfstrace            enable tracing of all VFS calls\n"
21738 #endif
21739 #ifdef SQLITE_HAVE_ZLIB
21740   "   -zip                 open the file as a ZIP Archive\n"
21741 #endif
21742 ;
21743 static void usage(int showDetail){
21744   utf8_printf(stderr,
21745       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
21746       "FILENAME is the name of an SQLite database. A new database is created\n"
21747       "if the file does not previously exist.\n", Argv0);
21748   if( showDetail ){
21749     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
21750   }else{
21751     raw_printf(stderr, "Use the -help option for additional information\n");
21752   }
21753   exit(1);
21754 }
21755
21756 /*
21757 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
21758 ** error message if it is initialized.
21759 */
21760 static void verify_uninitialized(void){
21761   if( sqlite3_config(-1)==SQLITE_MISUSE ){
21762     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
21763                         " initialization.\n");
21764   }
21765 }
21766
21767 /*
21768 ** Initialize the state information in data
21769 */
21770 static void main_init(ShellState *data) {
21771   memset(data, 0, sizeof(*data));
21772   data->normalMode = data->cMode = data->mode = MODE_List;
21773   data->autoExplain = 1;
21774   memcpy(data->colSeparator,SEP_Column, 2);
21775   memcpy(data->rowSeparator,SEP_Row, 2);
21776   data->showHeader = 0;
21777   data->shellFlgs = SHFLG_Lookaside;
21778   verify_uninitialized();
21779   sqlite3_config(SQLITE_CONFIG_URI, 1);
21780   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
21781   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
21782   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
21783   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
21784 }
21785
21786 /*
21787 ** Output text to the console in a font that attracts extra attention.
21788 */
21789 #ifdef _WIN32
21790 static void printBold(const char *zText){
21791 #if !SQLITE_OS_WINRT
21792   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
21793   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
21794   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
21795   SetConsoleTextAttribute(out,
21796          FOREGROUND_RED|FOREGROUND_INTENSITY
21797   );
21798 #endif
21799   printf("%s", zText);
21800 #if !SQLITE_OS_WINRT
21801   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
21802 #endif
21803 }
21804 #else
21805 static void printBold(const char *zText){
21806   printf("\033[1m%s\033[0m", zText);
21807 }
21808 #endif
21809
21810 /*
21811 ** Get the argument to an --option.  Throw an error and die if no argument
21812 ** is available.
21813 */
21814 static char *cmdline_option_value(int argc, char **argv, int i){
21815   if( i==argc ){
21816     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
21817             argv[0], argv[argc-1]);
21818     exit(1);
21819   }
21820   return argv[i];
21821 }
21822
21823 #ifndef SQLITE_SHELL_IS_UTF8
21824 #  if (defined(_WIN32) || defined(WIN32)) \
21825    && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
21826 #    define SQLITE_SHELL_IS_UTF8          (0)
21827 #  else
21828 #    define SQLITE_SHELL_IS_UTF8          (1)
21829 #  endif
21830 #endif
21831
21832 #if SQLITE_SHELL_IS_UTF8
21833 int SQLITE_CDECL main(int argc, char **argv){
21834 #else
21835 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
21836   char **argv;
21837 #endif
21838   char *zErrMsg = 0;
21839   ShellState data;
21840   const char *zInitFile = 0;
21841   int i;
21842   int rc = 0;
21843   int warnInmemoryDb = 0;
21844   int readStdin = 1;
21845   int nCmd = 0;
21846   char **azCmd = 0;
21847   const char *zVfs = 0;           /* Value of -vfs command-line option */
21848 #if !SQLITE_SHELL_IS_UTF8
21849   char **argvToFree = 0;
21850   int argcToFree = 0;
21851 #endif
21852
21853   setBinaryMode(stdin, 0);
21854   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
21855   stdin_is_interactive = isatty(0);
21856   stdout_is_console = isatty(1);
21857
21858 #ifdef SQLITE_DEBUG
21859   registerOomSimulator();
21860 #endif
21861
21862 #if !defined(_WIN32_WCE)
21863   if( getenv("SQLITE_DEBUG_BREAK") ){
21864     if( isatty(0) && isatty(2) ){
21865       fprintf(stderr,
21866           "attach debugger to process %d and press any key to continue.\n",
21867           GETPID());
21868       fgetc(stdin);
21869     }else{
21870 #if defined(_WIN32) || defined(WIN32)
21871 #if SQLITE_OS_WINRT
21872       __debugbreak();
21873 #else
21874       DebugBreak();
21875 #endif
21876 #elif defined(SIGTRAP)
21877       raise(SIGTRAP);
21878 #endif
21879     }
21880   }
21881 #endif
21882
21883 #if USE_SYSTEM_SQLITE+0!=1
21884   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
21885     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
21886             sqlite3_sourceid(), SQLITE_SOURCE_ID);
21887     exit(1);
21888   }
21889 #endif
21890   main_init(&data);
21891
21892   /* On Windows, we must translate command-line arguments into UTF-8.
21893   ** The SQLite memory allocator subsystem has to be enabled in order to
21894   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
21895   ** subsequent sqlite3_config() calls will work.  So copy all results into
21896   ** memory that does not come from the SQLite memory allocator.
21897   */
21898 #if !SQLITE_SHELL_IS_UTF8
21899   sqlite3_initialize();
21900   argvToFree = malloc(sizeof(argv[0])*argc*2);
21901   argcToFree = argc;
21902   argv = argvToFree + argc;
21903   if( argv==0 ) shell_out_of_memory();
21904   for(i=0; i<argc; i++){
21905     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
21906     int n;
21907     if( z==0 ) shell_out_of_memory();
21908     n = (int)strlen(z);
21909     argv[i] = malloc( n+1 );
21910     if( argv[i]==0 ) shell_out_of_memory();
21911     memcpy(argv[i], z, n+1);
21912     argvToFree[i] = argv[i];
21913     sqlite3_free(z);
21914   }
21915   sqlite3_shutdown();
21916 #endif
21917
21918   assert( argc>=1 && argv && argv[0] );
21919   Argv0 = argv[0];
21920
21921   /* Make sure we have a valid signal handler early, before anything
21922   ** else is done.
21923   */
21924 #ifdef SIGINT
21925   signal(SIGINT, interrupt_handler);
21926 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
21927   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
21928 #endif
21929
21930 #ifdef SQLITE_SHELL_DBNAME_PROC
21931   {
21932     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
21933     ** of a C-function that will provide the name of the database file.  Use
21934     ** this compile-time option to embed this shell program in larger
21935     ** applications. */
21936     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
21937     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
21938     warnInmemoryDb = 0;
21939   }
21940 #endif
21941
21942   /* Do an initial pass through the command-line argument to locate
21943   ** the name of the database file, the name of the initialization file,
21944   ** the size of the alternative malloc heap,
21945   ** and the first command to execute.
21946   */
21947   verify_uninitialized();
21948   for(i=1; i<argc; i++){
21949     char *z;
21950     z = argv[i];
21951     if( z[0]!='-' ){
21952       if( data.zDbFilename==0 ){
21953         data.zDbFilename = z;
21954       }else{
21955         /* Excesss arguments are interpreted as SQL (or dot-commands) and
21956         ** mean that nothing is read from stdin */
21957         readStdin = 0;
21958         nCmd++;
21959         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
21960         if( azCmd==0 ) shell_out_of_memory();
21961         azCmd[nCmd-1] = z;
21962       }
21963     }
21964     if( z[1]=='-' ) z++;
21965     if( strcmp(z,"-separator")==0
21966      || strcmp(z,"-nullvalue")==0
21967      || strcmp(z,"-newline")==0
21968      || strcmp(z,"-cmd")==0
21969     ){
21970       (void)cmdline_option_value(argc, argv, ++i);
21971     }else if( strcmp(z,"-init")==0 ){
21972       zInitFile = cmdline_option_value(argc, argv, ++i);
21973     }else if( strcmp(z,"-batch")==0 ){
21974       /* Need to check for batch mode here to so we can avoid printing
21975       ** informational messages (like from process_sqliterc) before
21976       ** we do the actual processing of arguments later in a second pass.
21977       */
21978       stdin_is_interactive = 0;
21979     }else if( strcmp(z,"-heap")==0 ){
21980 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
21981       const char *zSize;
21982       sqlite3_int64 szHeap;
21983
21984       zSize = cmdline_option_value(argc, argv, ++i);
21985       szHeap = integerValue(zSize);
21986       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
21987       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
21988 #else
21989       (void)cmdline_option_value(argc, argv, ++i);
21990 #endif
21991     }else if( strcmp(z,"-pagecache")==0 ){
21992       sqlite3_int64 n, sz;
21993       sz = integerValue(cmdline_option_value(argc,argv,++i));
21994       if( sz>70000 ) sz = 70000;
21995       if( sz<0 ) sz = 0;
21996       n = integerValue(cmdline_option_value(argc,argv,++i));
21997       if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
21998         n = 0xffffffffffffLL/sz;
21999       }
22000       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
22001                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
22002       data.shellFlgs |= SHFLG_Pagecache;
22003     }else if( strcmp(z,"-lookaside")==0 ){
22004       int n, sz;
22005       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
22006       if( sz<0 ) sz = 0;
22007       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
22008       if( n<0 ) n = 0;
22009       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
22010       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
22011 #ifdef SQLITE_ENABLE_VFSTRACE
22012     }else if( strcmp(z,"-vfstrace")==0 ){
22013       extern int vfstrace_register(
22014          const char *zTraceName,
22015          const char *zOldVfsName,
22016          int (*xOut)(const char*,void*),
22017          void *pOutArg,
22018          int makeDefault
22019       );
22020       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
22021 #endif
22022 #ifdef SQLITE_ENABLE_MULTIPLEX
22023     }else if( strcmp(z,"-multiplex")==0 ){
22024       extern int sqlite3_multiple_initialize(const char*,int);
22025       sqlite3_multiplex_initialize(0, 1);
22026 #endif
22027     }else if( strcmp(z,"-mmap")==0 ){
22028       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22029       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
22030 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22031     }else if( strcmp(z,"-sorterref")==0 ){
22032       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22033       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
22034 #endif
22035     }else if( strcmp(z,"-vfs")==0 ){
22036       zVfs = cmdline_option_value(argc, argv, ++i);
22037 #ifdef SQLITE_HAVE_ZLIB
22038     }else if( strcmp(z,"-zip")==0 ){
22039       data.openMode = SHELL_OPEN_ZIPFILE;
22040 #endif
22041     }else if( strcmp(z,"-append")==0 ){
22042       data.openMode = SHELL_OPEN_APPENDVFS;
22043 #ifndef SQLITE_OMIT_DESERIALIZE
22044     }else if( strcmp(z,"-deserialize")==0 ){
22045       data.openMode = SHELL_OPEN_DESERIALIZE;
22046     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22047       data.szMax = integerValue(argv[++i]);
22048 #endif
22049     }else if( strcmp(z,"-readonly")==0 ){
22050       data.openMode = SHELL_OPEN_READONLY;
22051     }else if( strcmp(z,"-nofollow")==0 ){
22052       data.openFlags = SQLITE_OPEN_NOFOLLOW;
22053 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22054     }else if( strncmp(z, "-A",2)==0 ){
22055       /* All remaining command-line arguments are passed to the ".archive"
22056       ** command, so ignore them */
22057       break;
22058 #endif
22059     }else if( strcmp(z, "-memtrace")==0 ){
22060       sqlite3MemTraceActivate(stderr);
22061     }else if( strcmp(z,"-bail")==0 ){
22062       bail_on_error = 1;
22063     }
22064   }
22065   verify_uninitialized();
22066
22067
22068 #ifdef SQLITE_SHELL_INIT_PROC
22069   {
22070     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
22071     ** of a C-function that will perform initialization actions on SQLite that
22072     ** occur just before or after sqlite3_initialize(). Use this compile-time
22073     ** option to embed this shell program in larger applications. */
22074     extern void SQLITE_SHELL_INIT_PROC(void);
22075     SQLITE_SHELL_INIT_PROC();
22076   }
22077 #else
22078   /* All the sqlite3_config() calls have now been made. So it is safe
22079   ** to call sqlite3_initialize() and process any command line -vfs option. */
22080   sqlite3_initialize();
22081 #endif
22082
22083   if( zVfs ){
22084     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
22085     if( pVfs ){
22086       sqlite3_vfs_register(pVfs, 1);
22087     }else{
22088       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
22089       exit(1);
22090     }
22091   }
22092
22093   if( data.zDbFilename==0 ){
22094 #ifndef SQLITE_OMIT_MEMORYDB
22095     data.zDbFilename = ":memory:";
22096     warnInmemoryDb = argc==1;
22097 #else
22098     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
22099     return 1;
22100 #endif
22101   }
22102   data.out = stdout;
22103   sqlite3_appendvfs_init(0,0,0);
22104
22105   /* Go ahead and open the database file if it already exists.  If the
22106   ** file does not exist, delay opening it.  This prevents empty database
22107   ** files from being created if a user mistypes the database name argument
22108   ** to the sqlite command-line tool.
22109   */
22110   if( access(data.zDbFilename, 0)==0 ){
22111     open_db(&data, 0);
22112   }
22113
22114   /* Process the initialization file if there is one.  If no -init option
22115   ** is given on the command line, look for a file named ~/.sqliterc and
22116   ** try to process it.
22117   */
22118   process_sqliterc(&data,zInitFile);
22119
22120   /* Make a second pass through the command-line argument and set
22121   ** options.  This second pass is delayed until after the initialization
22122   ** file is processed so that the command-line arguments will override
22123   ** settings in the initialization file.
22124   */
22125   for(i=1; i<argc; i++){
22126     char *z = argv[i];
22127     if( z[0]!='-' ) continue;
22128     if( z[1]=='-' ){ z++; }
22129     if( strcmp(z,"-init")==0 ){
22130       i++;
22131     }else if( strcmp(z,"-html")==0 ){
22132       data.mode = MODE_Html;
22133     }else if( strcmp(z,"-list")==0 ){
22134       data.mode = MODE_List;
22135     }else if( strcmp(z,"-quote")==0 ){
22136       data.mode = MODE_Quote;
22137       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
22138       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22139     }else if( strcmp(z,"-line")==0 ){
22140       data.mode = MODE_Line;
22141     }else if( strcmp(z,"-column")==0 ){
22142       data.mode = MODE_Column;
22143     }else if( strcmp(z,"-json")==0 ){
22144       data.mode = MODE_Json;
22145     }else if( strcmp(z,"-markdown")==0 ){
22146       data.mode = MODE_Markdown;
22147     }else if( strcmp(z,"-table")==0 ){
22148       data.mode = MODE_Table;
22149     }else if( strcmp(z,"-box")==0 ){
22150       data.mode = MODE_Box;
22151     }else if( strcmp(z,"-csv")==0 ){
22152       data.mode = MODE_Csv;
22153       memcpy(data.colSeparator,",",2);
22154 #ifdef SQLITE_HAVE_ZLIB
22155     }else if( strcmp(z,"-zip")==0 ){
22156       data.openMode = SHELL_OPEN_ZIPFILE;
22157 #endif
22158     }else if( strcmp(z,"-append")==0 ){
22159       data.openMode = SHELL_OPEN_APPENDVFS;
22160 #ifndef SQLITE_OMIT_DESERIALIZE
22161     }else if( strcmp(z,"-deserialize")==0 ){
22162       data.openMode = SHELL_OPEN_DESERIALIZE;
22163     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22164       data.szMax = integerValue(argv[++i]);
22165 #endif
22166     }else if( strcmp(z,"-readonly")==0 ){
22167       data.openMode = SHELL_OPEN_READONLY;
22168     }else if( strcmp(z,"-nofollow")==0 ){
22169       data.openFlags |= SQLITE_OPEN_NOFOLLOW;
22170     }else if( strcmp(z,"-ascii")==0 ){
22171       data.mode = MODE_Ascii;
22172       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
22173       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
22174     }else if( strcmp(z,"-tabs")==0 ){
22175       data.mode = MODE_List;
22176       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
22177       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22178     }else if( strcmp(z,"-separator")==0 ){
22179       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
22180                        "%s",cmdline_option_value(argc,argv,++i));
22181     }else if( strcmp(z,"-newline")==0 ){
22182       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
22183                        "%s",cmdline_option_value(argc,argv,++i));
22184     }else if( strcmp(z,"-nullvalue")==0 ){
22185       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
22186                        "%s",cmdline_option_value(argc,argv,++i));
22187     }else if( strcmp(z,"-header")==0 ){
22188       data.showHeader = 1;
22189     }else if( strcmp(z,"-noheader")==0 ){
22190       data.showHeader = 0;
22191     }else if( strcmp(z,"-echo")==0 ){
22192       ShellSetFlag(&data, SHFLG_Echo);
22193     }else if( strcmp(z,"-eqp")==0 ){
22194       data.autoEQP = AUTOEQP_on;
22195     }else if( strcmp(z,"-eqpfull")==0 ){
22196       data.autoEQP = AUTOEQP_full;
22197     }else if( strcmp(z,"-stats")==0 ){
22198       data.statsOn = 1;
22199     }else if( strcmp(z,"-scanstats")==0 ){
22200       data.scanstatsOn = 1;
22201     }else if( strcmp(z,"-backslash")==0 ){
22202       /* Undocumented command-line option: -backslash
22203       ** Causes C-style backslash escapes to be evaluated in SQL statements
22204       ** prior to sending the SQL into SQLite.  Useful for injecting
22205       ** crazy bytes in the middle of SQL statements for testing and debugging.
22206       */
22207       ShellSetFlag(&data, SHFLG_Backslash);
22208     }else if( strcmp(z,"-bail")==0 ){
22209       /* No-op.  The bail_on_error flag should already be set. */
22210     }else if( strcmp(z,"-version")==0 ){
22211       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
22212       return 0;
22213     }else if( strcmp(z,"-interactive")==0 ){
22214       stdin_is_interactive = 1;
22215     }else if( strcmp(z,"-batch")==0 ){
22216       stdin_is_interactive = 0;
22217     }else if( strcmp(z,"-heap")==0 ){
22218       i++;
22219     }else if( strcmp(z,"-pagecache")==0 ){
22220       i+=2;
22221     }else if( strcmp(z,"-lookaside")==0 ){
22222       i+=2;
22223     }else if( strcmp(z,"-mmap")==0 ){
22224       i++;
22225     }else if( strcmp(z,"-memtrace")==0 ){
22226       i++;
22227 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22228     }else if( strcmp(z,"-sorterref")==0 ){
22229       i++;
22230 #endif
22231     }else if( strcmp(z,"-vfs")==0 ){
22232       i++;
22233 #ifdef SQLITE_ENABLE_VFSTRACE
22234     }else if( strcmp(z,"-vfstrace")==0 ){
22235       i++;
22236 #endif
22237 #ifdef SQLITE_ENABLE_MULTIPLEX
22238     }else if( strcmp(z,"-multiplex")==0 ){
22239       i++;
22240 #endif
22241     }else if( strcmp(z,"-help")==0 ){
22242       usage(1);
22243     }else if( strcmp(z,"-cmd")==0 ){
22244       /* Run commands that follow -cmd first and separately from commands
22245       ** that simply appear on the command-line.  This seems goofy.  It would
22246       ** be better if all commands ran in the order that they appear.  But
22247       ** we retain the goofy behavior for historical compatibility. */
22248       if( i==argc-1 ) break;
22249       z = cmdline_option_value(argc,argv,++i);
22250       if( z[0]=='.' ){
22251         rc = do_meta_command(z, &data);
22252         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
22253       }else{
22254         open_db(&data, 0);
22255         rc = shell_exec(&data, z, &zErrMsg);
22256         if( zErrMsg!=0 ){
22257           utf8_printf(stderr,"Error: %s\n", zErrMsg);
22258           if( bail_on_error ) return rc!=0 ? rc : 1;
22259         }else if( rc!=0 ){
22260           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
22261           if( bail_on_error ) return rc;
22262         }
22263       }
22264 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22265     }else if( strncmp(z, "-A", 2)==0 ){
22266       if( nCmd>0 ){
22267         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
22268                             " with \"%s\"\n", z);
22269         return 1;
22270       }
22271       open_db(&data, OPEN_DB_ZIPFILE);
22272       if( z[2] ){
22273         argv[i] = &z[2];
22274         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
22275       }else{
22276         arDotCommand(&data, 1, argv+i, argc-i);
22277       }
22278       readStdin = 0;
22279       break;
22280 #endif
22281     }else{
22282       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
22283       raw_printf(stderr,"Use -help for a list of options.\n");
22284       return 1;
22285     }
22286     data.cMode = data.mode;
22287   }
22288
22289   if( !readStdin ){
22290     /* Run all arguments that do not begin with '-' as if they were separate
22291     ** command-line inputs, except for the argToSkip argument which contains
22292     ** the database filename.
22293     */
22294     for(i=0; i<nCmd; i++){
22295       if( azCmd[i][0]=='.' ){
22296         rc = do_meta_command(azCmd[i], &data);
22297         if( rc ){
22298           free(azCmd);
22299           return rc==2 ? 0 : rc;
22300         }
22301       }else{
22302         open_db(&data, 0);
22303         rc = shell_exec(&data, azCmd[i], &zErrMsg);
22304         if( zErrMsg || rc ){
22305           if( zErrMsg!=0 ){
22306             utf8_printf(stderr,"Error: %s\n", zErrMsg);
22307           }else{
22308             utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
22309           }
22310           sqlite3_free(zErrMsg);
22311           free(azCmd);
22312           return rc!=0 ? rc : 1;
22313         }
22314       }
22315     }
22316   }else{
22317     /* Run commands received from standard input
22318     */
22319     if( stdin_is_interactive ){
22320       char *zHome;
22321       char *zHistory;
22322       int nHistory;
22323       printf(
22324         "SQLite version %s %.19s\n" /*extra-version-info*/
22325         "Enter \".help\" for usage hints.\n",
22326         sqlite3_libversion(), sqlite3_sourceid()
22327       );
22328       if( warnInmemoryDb ){
22329         printf("Connected to a ");
22330         printBold("transient in-memory database");
22331         printf(".\nUse \".open FILENAME\" to reopen on a "
22332                "persistent database.\n");
22333       }
22334       zHistory = getenv("SQLITE_HISTORY");
22335       if( zHistory ){
22336         zHistory = strdup(zHistory);
22337       }else if( (zHome = find_home_dir(0))!=0 ){
22338         nHistory = strlen30(zHome) + 20;
22339         if( (zHistory = malloc(nHistory))!=0 ){
22340           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
22341         }
22342       }
22343       if( zHistory ){ shell_read_history(zHistory); }
22344 #if HAVE_READLINE || HAVE_EDITLINE
22345       rl_attempted_completion_function = readline_completion;
22346 #elif HAVE_LINENOISE
22347       linenoiseSetCompletionCallback(linenoise_completion);
22348 #endif
22349       data.in = 0;
22350       rc = process_input(&data);
22351       if( zHistory ){
22352         shell_stifle_history(2000);
22353         shell_write_history(zHistory);
22354         free(zHistory);
22355       }
22356     }else{
22357       data.in = stdin;
22358       rc = process_input(&data);
22359     }
22360   }
22361   free(azCmd);
22362   set_table_name(&data, 0);
22363   if( data.db ){
22364     session_close_all(&data);
22365     close_db(data.db);
22366   }
22367   sqlite3_free(data.zFreeOnClose);
22368   find_home_dir(1);
22369   output_reset(&data);
22370   data.doXdgOpen = 0;
22371   clearTempFile(&data);
22372 #if !SQLITE_SHELL_IS_UTF8
22373   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
22374   free(argvToFree);
22375 #endif
22376   free(data.colWidth);
22377   /* Clear the global data structure so that valgrind will detect memory
22378   ** leaks */
22379   memset(&data, 0, sizeof(data));
22380   return rc;
22381 }