]> git.lizzy.rs Git - sqlite3-cmake.git/blob - src/shell.c
3.37.0
[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 ** Optionally #include a user-defined header, whereby compilation options
40 ** may be set prior to where they take effect, but after platform setup. 
41 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
42 ** file. Note that this macro has a like effect on sqlite3.c compilation.
43 */
44 #ifdef SQLITE_CUSTOM_INCLUDE
45 # define INC_STRINGIFY_(f) #f
46 # define INC_STRINGIFY(f) INC_STRINGIFY_(f)
47 # include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
48 #endif
49
50 /*
51 ** Determine if we are dealing with WinRT, which provides only a subset of
52 ** the full Win32 API.
53 */
54 #if !defined(SQLITE_OS_WINRT)
55 # define SQLITE_OS_WINRT 0
56 #endif
57
58 /*
59 ** Warning pragmas copied from msvc.h in the core.
60 */
61 #if defined(_MSC_VER)
62 #pragma warning(disable : 4054)
63 #pragma warning(disable : 4055)
64 #pragma warning(disable : 4100)
65 #pragma warning(disable : 4127)
66 #pragma warning(disable : 4130)
67 #pragma warning(disable : 4152)
68 #pragma warning(disable : 4189)
69 #pragma warning(disable : 4206)
70 #pragma warning(disable : 4210)
71 #pragma warning(disable : 4232)
72 #pragma warning(disable : 4244)
73 #pragma warning(disable : 4305)
74 #pragma warning(disable : 4306)
75 #pragma warning(disable : 4702)
76 #pragma warning(disable : 4706)
77 #endif /* defined(_MSC_VER) */
78
79 /*
80 ** No support for loadable extensions in VxWorks.
81 */
82 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
83 # define SQLITE_OMIT_LOAD_EXTENSION 1
84 #endif
85
86 /*
87 ** Enable large-file support for fopen() and friends on unix.
88 */
89 #ifndef SQLITE_DISABLE_LFS
90 # define _LARGE_FILE       1
91 # ifndef _FILE_OFFSET_BITS
92 #   define _FILE_OFFSET_BITS 64
93 # endif
94 # define _LARGEFILE_SOURCE 1
95 #endif
96
97 #include <stdlib.h>
98 #include <string.h>
99 #include <stdio.h>
100 #include <assert.h>
101 #include "sqlite3.h"
102 typedef sqlite3_int64 i64;
103 typedef sqlite3_uint64 u64;
104 typedef unsigned char u8;
105 #if SQLITE_USER_AUTHENTICATION
106 # include "sqlite3userauth.h"
107 #endif
108 #include <ctype.h>
109 #include <stdarg.h>
110
111 #if !defined(_WIN32) && !defined(WIN32)
112 # include <signal.h>
113 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
114 #  include <pwd.h>
115 # endif
116 #endif
117 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
118 # include <unistd.h>
119 # include <dirent.h>
120 # define GETPID getpid
121 # if defined(__MINGW32__)
122 #  define DIRENT dirent
123 #  ifndef S_ISLNK
124 #   define S_ISLNK(mode) (0)
125 #  endif
126 # endif
127 #else
128 # define GETPID (int)GetCurrentProcessId
129 #endif
130 #include <sys/types.h>
131 #include <sys/stat.h>
132
133 #if HAVE_READLINE
134 # include <readline/readline.h>
135 # include <readline/history.h>
136 #endif
137
138 #if HAVE_EDITLINE
139 # include <editline/readline.h>
140 #endif
141
142 #if HAVE_EDITLINE || HAVE_READLINE
143
144 # define shell_add_history(X) add_history(X)
145 # define shell_read_history(X) read_history(X)
146 # define shell_write_history(X) write_history(X)
147 # define shell_stifle_history(X) stifle_history(X)
148 # define shell_readline(X) readline(X)
149
150 #elif HAVE_LINENOISE
151
152 # include "linenoise.h"
153 # define shell_add_history(X) linenoiseHistoryAdd(X)
154 # define shell_read_history(X) linenoiseHistoryLoad(X)
155 # define shell_write_history(X) linenoiseHistorySave(X)
156 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
157 # define shell_readline(X) linenoise(X)
158
159 #else
160
161 # define shell_read_history(X)
162 # define shell_write_history(X)
163 # define shell_stifle_history(X)
164
165 # define SHELL_USE_LOCAL_GETLINE 1
166 #endif
167
168
169 #if defined(_WIN32) || defined(WIN32)
170 # if SQLITE_OS_WINRT
171 #  define SQLITE_OMIT_POPEN 1
172 # else
173 #  include <io.h>
174 #  include <fcntl.h>
175 #  define isatty(h) _isatty(h)
176 #  ifndef access
177 #   define access(f,m) _access((f),(m))
178 #  endif
179 #  ifndef unlink
180 #   define unlink _unlink
181 #  endif
182 #  ifndef strdup
183 #   define strdup _strdup
184 #  endif
185 #  undef popen
186 #  define popen _popen
187 #  undef pclose
188 #  define pclose _pclose
189 # endif
190 #else
191  /* Make sure isatty() has a prototype. */
192  extern int isatty(int);
193
194 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
195   /* popen and pclose are not C89 functions and so are
196   ** sometimes omitted from the <stdio.h> header */
197    extern FILE *popen(const char*,const char*);
198    extern int pclose(FILE*);
199 # else
200 #  define SQLITE_OMIT_POPEN 1
201 # endif
202 #endif
203
204 #if defined(_WIN32_WCE)
205 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
206  * thus we always assume that we have a console. That can be
207  * overridden with the -batch command line option.
208  */
209 #define isatty(x) 1
210 #endif
211
212 /* ctype macros that work with signed characters */
213 #define IsSpace(X)  isspace((unsigned char)X)
214 #define IsDigit(X)  isdigit((unsigned char)X)
215 #define ToLower(X)  (char)tolower((unsigned char)X)
216
217 #if defined(_WIN32) || defined(WIN32)
218 #if SQLITE_OS_WINRT
219 #include <intrin.h>
220 #endif
221 #include <windows.h>
222
223 /* string conversion routines only needed on Win32 */
224 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
225 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
226 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
227 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
228 #endif
229
230 /* On Windows, we normally run with output mode of TEXT so that \n characters
231 ** are automatically translated into \r\n.  However, this behavior needs
232 ** to be disabled in some cases (ex: when generating CSV output and when
233 ** rendering quoted strings that contain \n characters).  The following
234 ** routines take care of that.
235 */
236 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
237 static void setBinaryMode(FILE *file, int isOutput){
238   if( isOutput ) fflush(file);
239   _setmode(_fileno(file), _O_BINARY);
240 }
241 static void setTextMode(FILE *file, int isOutput){
242   if( isOutput ) fflush(file);
243   _setmode(_fileno(file), _O_TEXT);
244 }
245 #else
246 # define setBinaryMode(X,Y)
247 # define setTextMode(X,Y)
248 #endif
249
250
251 /* True if the timer is enabled */
252 static int enableTimer = 0;
253
254 /* Return the current wall-clock time */
255 static sqlite3_int64 timeOfDay(void){
256   static sqlite3_vfs *clockVfs = 0;
257   sqlite3_int64 t;
258   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
259   if( clockVfs==0 ) return 0;  /* Never actually happens */
260   if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
261     clockVfs->xCurrentTimeInt64(clockVfs, &t);
262   }else{
263     double r;
264     clockVfs->xCurrentTime(clockVfs, &r);
265     t = (sqlite3_int64)(r*86400000.0);
266   }
267   return t;
268 }
269
270 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
271 #include <sys/time.h>
272 #include <sys/resource.h>
273
274 /* VxWorks does not support getrusage() as far as we can determine */
275 #if defined(_WRS_KERNEL) || defined(__RTP__)
276 struct rusage {
277   struct timeval ru_utime; /* user CPU time used */
278   struct timeval ru_stime; /* system CPU time used */
279 };
280 #define getrusage(A,B) memset(B,0,sizeof(*B))
281 #endif
282
283 /* Saved resource information for the beginning of an operation */
284 static struct rusage sBegin;  /* CPU time at start */
285 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
286
287 /*
288 ** Begin timing an operation
289 */
290 static void beginTimer(void){
291   if( enableTimer ){
292     getrusage(RUSAGE_SELF, &sBegin);
293     iBegin = timeOfDay();
294   }
295 }
296
297 /* Return the difference of two time_structs in seconds */
298 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
299   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
300          (double)(pEnd->tv_sec - pStart->tv_sec);
301 }
302
303 /*
304 ** Print the timing results.
305 */
306 static void endTimer(void){
307   if( enableTimer ){
308     sqlite3_int64 iEnd = timeOfDay();
309     struct rusage sEnd;
310     getrusage(RUSAGE_SELF, &sEnd);
311     printf("Run Time: real %.3f user %f sys %f\n",
312        (iEnd - iBegin)*0.001,
313        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
314        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
315   }
316 }
317
318 #define BEGIN_TIMER beginTimer()
319 #define END_TIMER endTimer()
320 #define HAS_TIMER 1
321
322 #elif (defined(_WIN32) || defined(WIN32))
323
324 /* Saved resource information for the beginning of an operation */
325 static HANDLE hProcess;
326 static FILETIME ftKernelBegin;
327 static FILETIME ftUserBegin;
328 static sqlite3_int64 ftWallBegin;
329 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
330                                     LPFILETIME, LPFILETIME);
331 static GETPROCTIMES getProcessTimesAddr = NULL;
332
333 /*
334 ** Check to see if we have timer support.  Return 1 if necessary
335 ** support found (or found previously).
336 */
337 static int hasTimer(void){
338   if( getProcessTimesAddr ){
339     return 1;
340   } else {
341 #if !SQLITE_OS_WINRT
342     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
343     ** versions. See if the version we are running on has it, and if it
344     ** does, save off a pointer to it and the current process handle.
345     */
346     hProcess = GetCurrentProcess();
347     if( hProcess ){
348       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
349       if( NULL != hinstLib ){
350         getProcessTimesAddr =
351             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
352         if( NULL != getProcessTimesAddr ){
353           return 1;
354         }
355         FreeLibrary(hinstLib);
356       }
357     }
358 #endif
359   }
360   return 0;
361 }
362
363 /*
364 ** Begin timing an operation
365 */
366 static void beginTimer(void){
367   if( enableTimer && getProcessTimesAddr ){
368     FILETIME ftCreation, ftExit;
369     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
370                         &ftKernelBegin,&ftUserBegin);
371     ftWallBegin = timeOfDay();
372   }
373 }
374
375 /* Return the difference of two FILETIME structs in seconds */
376 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
377   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
378   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
379   return (double) ((i64End - i64Start) / 10000000.0);
380 }
381
382 /*
383 ** Print the timing results.
384 */
385 static void endTimer(void){
386   if( enableTimer && getProcessTimesAddr){
387     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
388     sqlite3_int64 ftWallEnd = timeOfDay();
389     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
390     printf("Run Time: real %.3f user %f sys %f\n",
391        (ftWallEnd - ftWallBegin)*0.001,
392        timeDiff(&ftUserBegin, &ftUserEnd),
393        timeDiff(&ftKernelBegin, &ftKernelEnd));
394   }
395 }
396
397 #define BEGIN_TIMER beginTimer()
398 #define END_TIMER endTimer()
399 #define HAS_TIMER hasTimer()
400
401 #else
402 #define BEGIN_TIMER
403 #define END_TIMER
404 #define HAS_TIMER 0
405 #endif
406
407 /*
408 ** Used to prevent warnings about unused parameters
409 */
410 #define UNUSED_PARAMETER(x) (void)(x)
411
412 /*
413 ** Number of elements in an array
414 */
415 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
416
417 /*
418 ** If the following flag is set, then command execution stops
419 ** at an error if we are not interactive.
420 */
421 static int bail_on_error = 0;
422
423 /*
424 ** Threat stdin as an interactive input if the following variable
425 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
426 */
427 static int stdin_is_interactive = 1;
428
429 /*
430 ** On Windows systems we have to know if standard output is a console
431 ** in order to translate UTF-8 into MBCS.  The following variable is
432 ** true if translation is required.
433 */
434 static int stdout_is_console = 1;
435
436 /*
437 ** The following is the open SQLite database.  We make a pointer
438 ** to this database a static variable so that it can be accessed
439 ** by the SIGINT handler to interrupt database processing.
440 */
441 static sqlite3 *globalDb = 0;
442
443 /*
444 ** True if an interrupt (Control-C) has been received.
445 */
446 static volatile int seenInterrupt = 0;
447
448 #ifdef SQLITE_DEBUG
449 /*
450 ** Out-of-memory simulator variables
451 */
452 static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
453 static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
454 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
455 #endif /* SQLITE_DEBUG */
456
457 /*
458 ** This is the name of our program. It is set in main(), used
459 ** in a number of other places, mostly for error messages.
460 */
461 static char *Argv0;
462
463 /*
464 ** Prompt strings. Initialized in main. Settable with
465 **   .prompt main continue
466 */
467 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
468 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
469
470 /*
471 ** Render output like fprintf().  Except, if the output is going to the
472 ** console and if this is running on a Windows machine, translate the
473 ** output from UTF-8 into MBCS.
474 */
475 #if defined(_WIN32) || defined(WIN32)
476 void utf8_printf(FILE *out, const char *zFormat, ...){
477   va_list ap;
478   va_start(ap, zFormat);
479   if( stdout_is_console && (out==stdout || out==stderr) ){
480     char *z1 = sqlite3_vmprintf(zFormat, ap);
481     char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
482     sqlite3_free(z1);
483     fputs(z2, out);
484     sqlite3_free(z2);
485   }else{
486     vfprintf(out, zFormat, ap);
487   }
488   va_end(ap);
489 }
490 #elif !defined(utf8_printf)
491 # define utf8_printf fprintf
492 #endif
493
494 /*
495 ** Render output like fprintf().  This should not be used on anything that
496 ** includes string formatting (e.g. "%s").
497 */
498 #if !defined(raw_printf)
499 # define raw_printf fprintf
500 #endif
501
502 /* Indicate out-of-memory and exit. */
503 static void shell_out_of_memory(void){
504   raw_printf(stderr,"Error: out of memory\n");
505   exit(1);
506 }
507
508 #ifdef SQLITE_DEBUG
509 /* This routine is called when a simulated OOM occurs.  It is broken
510 ** out as a separate routine to make it easy to set a breakpoint on
511 ** the OOM
512 */
513 void shellOomFault(void){
514   if( oomRepeat>0 ){
515     oomRepeat--;
516   }else{
517     oomCounter--;
518   }
519 }
520 #endif /* SQLITE_DEBUG */
521
522 #ifdef SQLITE_DEBUG
523 /* This routine is a replacement malloc() that is used to simulate
524 ** Out-Of-Memory (OOM) errors for testing purposes.
525 */
526 static void *oomMalloc(int nByte){
527   if( oomCounter ){
528     if( oomCounter==1 ){
529       shellOomFault();
530       return 0;
531     }else{
532       oomCounter--;
533     }
534   }
535   return defaultMalloc(nByte);
536 }
537 #endif /* SQLITE_DEBUG */
538
539 #ifdef SQLITE_DEBUG
540 /* Register the OOM simulator.  This must occur before any memory
541 ** allocations */
542 static void registerOomSimulator(void){
543   sqlite3_mem_methods mem;
544   sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
545   defaultMalloc = mem.xMalloc;
546   mem.xMalloc = oomMalloc;
547   sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
548 }
549 #endif
550
551 /*
552 ** Write I/O traces to the following stream.
553 */
554 #ifdef SQLITE_ENABLE_IOTRACE
555 static FILE *iotrace = 0;
556 #endif
557
558 /*
559 ** This routine works like printf in that its first argument is a
560 ** format string and subsequent arguments are values to be substituted
561 ** in place of % fields.  The result of formatting this string
562 ** is written to iotrace.
563 */
564 #ifdef SQLITE_ENABLE_IOTRACE
565 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
566   va_list ap;
567   char *z;
568   if( iotrace==0 ) return;
569   va_start(ap, zFormat);
570   z = sqlite3_vmprintf(zFormat, ap);
571   va_end(ap);
572   utf8_printf(iotrace, "%s", z);
573   sqlite3_free(z);
574 }
575 #endif
576
577 /*
578 ** Output string zUtf to stream pOut as w characters.  If w is negative,
579 ** then right-justify the text.  W is the width in UTF-8 characters, not
580 ** in bytes.  This is different from the %*.*s specification in printf
581 ** since with %*.*s the width is measured in bytes, not characters.
582 */
583 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
584   int i;
585   int n;
586   int aw = w<0 ? -w : w;
587   for(i=n=0; zUtf[i]; i++){
588     if( (zUtf[i]&0xc0)!=0x80 ){
589       n++;
590       if( n==aw ){
591         do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
592         break;
593       }
594     }
595   }
596   if( n>=aw ){
597     utf8_printf(pOut, "%.*s", i, zUtf);
598   }else if( w<0 ){
599     utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
600   }else{
601     utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
602   }
603 }
604
605
606 /*
607 ** Determines if a string is a number of not.
608 */
609 static int isNumber(const char *z, int *realnum){
610   if( *z=='-' || *z=='+' ) z++;
611   if( !IsDigit(*z) ){
612     return 0;
613   }
614   z++;
615   if( realnum ) *realnum = 0;
616   while( IsDigit(*z) ){ z++; }
617   if( *z=='.' ){
618     z++;
619     if( !IsDigit(*z) ) return 0;
620     while( IsDigit(*z) ){ z++; }
621     if( realnum ) *realnum = 1;
622   }
623   if( *z=='e' || *z=='E' ){
624     z++;
625     if( *z=='+' || *z=='-' ) z++;
626     if( !IsDigit(*z) ) return 0;
627     while( IsDigit(*z) ){ z++; }
628     if( realnum ) *realnum = 1;
629   }
630   return *z==0;
631 }
632
633 /*
634 ** Compute a string length that is limited to what can be stored in
635 ** lower 30 bits of a 32-bit signed integer.
636 */
637 static int strlen30(const char *z){
638   const char *z2 = z;
639   while( *z2 ){ z2++; }
640   return 0x3fffffff & (int)(z2 - z);
641 }
642
643 /*
644 ** Return the length of a string in characters.  Multibyte UTF8 characters
645 ** count as a single character.
646 */
647 static int strlenChar(const char *z){
648   int n = 0;
649   while( *z ){
650     if( (0xc0&*(z++))!=0x80 ) n++;
651   }
652   return n;
653 }
654
655 /*
656 ** Return open FILE * if zFile exists, can be opened for read
657 ** and is an ordinary file or a character stream source.
658 ** Otherwise return 0.
659 */
660 static FILE * openChrSource(const char *zFile){
661 #ifdef _WIN32
662   struct _stat x = {0};
663 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
664   /* On Windows, open first, then check the stream nature. This order
665   ** is necessary because _stat() and sibs, when checking a named pipe,
666   ** effectively break the pipe as its supplier sees it. */
667   FILE *rv = fopen(zFile, "rb");
668   if( rv==0 ) return 0;
669   if( _fstat(_fileno(rv), &x) != 0
670       || !STAT_CHR_SRC(x.st_mode)){
671     fclose(rv);
672     rv = 0;
673   }
674   return rv;
675 #else
676   struct stat x = {0};
677   int rc = stat(zFile, &x);
678 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
679   if( rc!=0 ) return 0;
680   if( STAT_CHR_SRC(x.st_mode) ){
681     return fopen(zFile, "rb");
682   }else{
683     return 0;
684   }
685 #endif
686 #undef STAT_CHR_SRC
687 }
688
689 /*
690 ** This routine reads a line of text from FILE in, stores
691 ** the text in memory obtained from malloc() and returns a pointer
692 ** to the text.  NULL is returned at end of file, or if malloc()
693 ** fails.
694 **
695 ** If zLine is not NULL then it is a malloced buffer returned from
696 ** a previous call to this routine that may be reused.
697 */
698 static char *local_getline(char *zLine, FILE *in){
699   int nLine = zLine==0 ? 0 : 100;
700   int n = 0;
701
702   while( 1 ){
703     if( n+100>nLine ){
704       nLine = nLine*2 + 100;
705       zLine = realloc(zLine, nLine);
706       if( zLine==0 ) shell_out_of_memory();
707     }
708     if( fgets(&zLine[n], nLine - n, in)==0 ){
709       if( n==0 ){
710         free(zLine);
711         return 0;
712       }
713       zLine[n] = 0;
714       break;
715     }
716     while( zLine[n] ) n++;
717     if( n>0 && zLine[n-1]=='\n' ){
718       n--;
719       if( n>0 && zLine[n-1]=='\r' ) n--;
720       zLine[n] = 0;
721       break;
722     }
723   }
724 #if defined(_WIN32) || defined(WIN32)
725   /* For interactive input on Windows systems, translate the
726   ** multi-byte characterset characters into UTF-8. */
727   if( stdin_is_interactive && in==stdin ){
728     char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
729     if( zTrans ){
730       int nTrans = strlen30(zTrans)+1;
731       if( nTrans>nLine ){
732         zLine = realloc(zLine, nTrans);
733         if( zLine==0 ) shell_out_of_memory();
734       }
735       memcpy(zLine, zTrans, nTrans);
736       sqlite3_free(zTrans);
737     }
738   }
739 #endif /* defined(_WIN32) || defined(WIN32) */
740   return zLine;
741 }
742
743 /*
744 ** Retrieve a single line of input text.
745 **
746 ** If in==0 then read from standard input and prompt before each line.
747 ** If isContinuation is true, then a continuation prompt is appropriate.
748 ** If isContinuation is zero, then the main prompt should be used.
749 **
750 ** If zPrior is not NULL then it is a buffer from a prior call to this
751 ** routine that can be reused.
752 **
753 ** The result is stored in space obtained from malloc() and must either
754 ** be freed by the caller or else passed back into this routine via the
755 ** zPrior argument for reuse.
756 */
757 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
758   char *zPrompt;
759   char *zResult;
760   if( in!=0 ){
761     zResult = local_getline(zPrior, in);
762   }else{
763     zPrompt = isContinuation ? continuePrompt : mainPrompt;
764 #if SHELL_USE_LOCAL_GETLINE
765     printf("%s", zPrompt);
766     fflush(stdout);
767     zResult = local_getline(zPrior, stdin);
768 #else
769     free(zPrior);
770     zResult = shell_readline(zPrompt);
771     if( zResult && *zResult ) shell_add_history(zResult);
772 #endif
773   }
774   return zResult;
775 }
776
777
778 /*
779 ** Return the value of a hexadecimal digit.  Return -1 if the input
780 ** is not a hex digit.
781 */
782 static int hexDigitValue(char c){
783   if( c>='0' && c<='9' ) return c - '0';
784   if( c>='a' && c<='f' ) return c - 'a' + 10;
785   if( c>='A' && c<='F' ) return c - 'A' + 10;
786   return -1;
787 }
788
789 /*
790 ** Interpret zArg as an integer value, possibly with suffixes.
791 */
792 static sqlite3_int64 integerValue(const char *zArg){
793   sqlite3_int64 v = 0;
794   static const struct { char *zSuffix; int iMult; } aMult[] = {
795     { "KiB", 1024 },
796     { "MiB", 1024*1024 },
797     { "GiB", 1024*1024*1024 },
798     { "KB",  1000 },
799     { "MB",  1000000 },
800     { "GB",  1000000000 },
801     { "K",   1000 },
802     { "M",   1000000 },
803     { "G",   1000000000 },
804   };
805   int i;
806   int isNeg = 0;
807   if( zArg[0]=='-' ){
808     isNeg = 1;
809     zArg++;
810   }else if( zArg[0]=='+' ){
811     zArg++;
812   }
813   if( zArg[0]=='0' && zArg[1]=='x' ){
814     int x;
815     zArg += 2;
816     while( (x = hexDigitValue(zArg[0]))>=0 ){
817       v = (v<<4) + x;
818       zArg++;
819     }
820   }else{
821     while( IsDigit(zArg[0]) ){
822       v = v*10 + zArg[0] - '0';
823       zArg++;
824     }
825   }
826   for(i=0; i<ArraySize(aMult); i++){
827     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
828       v *= aMult[i].iMult;
829       break;
830     }
831   }
832   return isNeg? -v : v;
833 }
834
835 /*
836 ** A variable length string to which one can append text.
837 */
838 typedef struct ShellText ShellText;
839 struct ShellText {
840   char *z;
841   int n;
842   int nAlloc;
843 };
844
845 /*
846 ** Initialize and destroy a ShellText object
847 */
848 static void initText(ShellText *p){
849   memset(p, 0, sizeof(*p));
850 }
851 static void freeText(ShellText *p){
852   free(p->z);
853   initText(p);
854 }
855
856 /* zIn is either a pointer to a NULL-terminated string in memory obtained
857 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
858 ** added to zIn, and the result returned in memory obtained from malloc().
859 ** zIn, if it was not NULL, is freed.
860 **
861 ** If the third argument, quote, is not '\0', then it is used as a
862 ** quote character for zAppend.
863 */
864 static void appendText(ShellText *p, char const *zAppend, char quote){
865   int len;
866   int i;
867   int nAppend = strlen30(zAppend);
868
869   len = nAppend+p->n+1;
870   if( quote ){
871     len += 2;
872     for(i=0; i<nAppend; i++){
873       if( zAppend[i]==quote ) len++;
874     }
875   }
876
877   if( p->z==0 || p->n+len>=p->nAlloc ){
878     p->nAlloc = p->nAlloc*2 + len + 20;
879     p->z = realloc(p->z, p->nAlloc);
880     if( p->z==0 ) shell_out_of_memory();
881   }
882
883   if( quote ){
884     char *zCsr = p->z+p->n;
885     *zCsr++ = quote;
886     for(i=0; i<nAppend; i++){
887       *zCsr++ = zAppend[i];
888       if( zAppend[i]==quote ) *zCsr++ = quote;
889     }
890     *zCsr++ = quote;
891     p->n = (int)(zCsr - p->z);
892     *zCsr = '\0';
893   }else{
894     memcpy(p->z+p->n, zAppend, nAppend);
895     p->n += nAppend;
896     p->z[p->n] = '\0';
897   }
898 }
899
900 /*
901 ** Attempt to determine if identifier zName needs to be quoted, either
902 ** because it contains non-alphanumeric characters, or because it is an
903 ** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
904 ** that quoting is required.
905 **
906 ** Return '"' if quoting is required.  Return 0 if no quoting is required.
907 */
908 static char quoteChar(const char *zName){
909   int i;
910   if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
911   for(i=0; zName[i]; i++){
912     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
913   }
914   return sqlite3_keyword_check(zName, i) ? '"' : 0;
915 }
916
917 /*
918 ** Construct a fake object name and column list to describe the structure
919 ** of the view, virtual table, or table valued function zSchema.zName.
920 */
921 static char *shellFakeSchema(
922   sqlite3 *db,            /* The database connection containing the vtab */
923   const char *zSchema,    /* Schema of the database holding the vtab */
924   const char *zName       /* The name of the virtual table */
925 ){
926   sqlite3_stmt *pStmt = 0;
927   char *zSql;
928   ShellText s;
929   char cQuote;
930   char *zDiv = "(";
931   int nRow = 0;
932
933   zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
934                          zSchema ? zSchema : "main", zName);
935   sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
936   sqlite3_free(zSql);
937   initText(&s);
938   if( zSchema ){
939     cQuote = quoteChar(zSchema);
940     if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
941     appendText(&s, zSchema, cQuote);
942     appendText(&s, ".", 0);
943   }
944   cQuote = quoteChar(zName);
945   appendText(&s, zName, cQuote);
946   while( sqlite3_step(pStmt)==SQLITE_ROW ){
947     const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
948     nRow++;
949     appendText(&s, zDiv, 0);
950     zDiv = ",";
951     cQuote = quoteChar(zCol);
952     appendText(&s, zCol, cQuote);
953   }
954   appendText(&s, ")", 0);
955   sqlite3_finalize(pStmt);
956   if( nRow==0 ){
957     freeText(&s);
958     s.z = 0;
959   }
960   return s.z;
961 }
962
963 /*
964 ** SQL function:  shell_module_schema(X)
965 **
966 ** Return a fake schema for the table-valued function or eponymous virtual
967 ** table X.
968 */
969 static void shellModuleSchema(
970   sqlite3_context *pCtx,
971   int nVal,
972   sqlite3_value **apVal
973 ){
974   const char *zName = (const char*)sqlite3_value_text(apVal[0]);
975   char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
976   UNUSED_PARAMETER(nVal);
977   if( zFake ){
978     sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
979                         -1, sqlite3_free);
980     free(zFake);
981   }
982 }
983
984 /*
985 ** SQL function:  shell_add_schema(S,X)
986 **
987 ** Add the schema name X to the CREATE statement in S and return the result.
988 ** Examples:
989 **
990 **    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
991 **
992 ** Also works on
993 **
994 **    CREATE INDEX
995 **    CREATE UNIQUE INDEX
996 **    CREATE VIEW
997 **    CREATE TRIGGER
998 **    CREATE VIRTUAL TABLE
999 **
1000 ** This UDF is used by the .schema command to insert the schema name of
1001 ** attached databases into the middle of the sqlite_schema.sql field.
1002 */
1003 static void shellAddSchemaName(
1004   sqlite3_context *pCtx,
1005   int nVal,
1006   sqlite3_value **apVal
1007 ){
1008   static const char *aPrefix[] = {
1009      "TABLE",
1010      "INDEX",
1011      "UNIQUE INDEX",
1012      "VIEW",
1013      "TRIGGER",
1014      "VIRTUAL TABLE"
1015   };
1016   int i = 0;
1017   const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1018   const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1019   const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1020   sqlite3 *db = sqlite3_context_db_handle(pCtx);
1021   UNUSED_PARAMETER(nVal);
1022   if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
1023     for(i=0; i<ArraySize(aPrefix); i++){
1024       int n = strlen30(aPrefix[i]);
1025       if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1026         char *z = 0;
1027         char *zFake = 0;
1028         if( zSchema ){
1029           char cQuote = quoteChar(zSchema);
1030           if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1031             z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1032           }else{
1033             z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1034           }
1035         }
1036         if( zName
1037          && aPrefix[i][0]=='V'
1038          && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1039         ){
1040           if( z==0 ){
1041             z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1042           }else{
1043             z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1044           }
1045           free(zFake);
1046         }
1047         if( z ){
1048           sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1049           return;
1050         }
1051       }
1052     }
1053   }
1054   sqlite3_result_value(pCtx, apVal[0]);
1055 }
1056
1057 /*
1058 ** The source code for several run-time loadable extensions is inserted
1059 ** below by the ../tool/mkshellc.tcl script.  Before processing that included
1060 ** code, we need to override some macros to make the included program code
1061 ** work here in the middle of this regular program.
1062 */
1063 #define SQLITE_EXTENSION_INIT1
1064 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1065
1066 #if defined(_WIN32) && defined(_MSC_VER)
1067 /************************* Begin test_windirent.h ******************/
1068 /*
1069 ** 2015 November 30
1070 **
1071 ** The author disclaims copyright to this source code.  In place of
1072 ** a legal notice, here is a blessing:
1073 **
1074 **    May you do good and not evil.
1075 **    May you find forgiveness for yourself and forgive others.
1076 **    May you share freely, never taking more than you give.
1077 **
1078 *************************************************************************
1079 ** This file contains declarations for most of the opendir() family of
1080 ** POSIX functions on Win32 using the MSVCRT.
1081 */
1082
1083 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1084 #define SQLITE_WINDIRENT_H
1085
1086 /*
1087 ** We need several data types from the Windows SDK header.
1088 */
1089
1090 #ifndef WIN32_LEAN_AND_MEAN
1091 #define WIN32_LEAN_AND_MEAN
1092 #endif
1093
1094 #include "windows.h"
1095
1096 /*
1097 ** We need several support functions from the SQLite core.
1098 */
1099
1100 /* #include "sqlite3.h" */
1101
1102 /*
1103 ** We need several things from the ANSI and MSVCRT headers.
1104 */
1105
1106 #include <stdio.h>
1107 #include <stdlib.h>
1108 #include <errno.h>
1109 #include <io.h>
1110 #include <limits.h>
1111 #include <sys/types.h>
1112 #include <sys/stat.h>
1113
1114 /*
1115 ** We may need several defines that should have been in "sys/stat.h".
1116 */
1117
1118 #ifndef S_ISREG
1119 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1120 #endif
1121
1122 #ifndef S_ISDIR
1123 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1124 #endif
1125
1126 #ifndef S_ISLNK
1127 #define S_ISLNK(mode) (0)
1128 #endif
1129
1130 /*
1131 ** We may need to provide the "mode_t" type.
1132 */
1133
1134 #ifndef MODE_T_DEFINED
1135   #define MODE_T_DEFINED
1136   typedef unsigned short mode_t;
1137 #endif
1138
1139 /*
1140 ** We may need to provide the "ino_t" type.
1141 */
1142
1143 #ifndef INO_T_DEFINED
1144   #define INO_T_DEFINED
1145   typedef unsigned short ino_t;
1146 #endif
1147
1148 /*
1149 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1150 */
1151
1152 #ifndef NAME_MAX
1153 #  ifdef FILENAME_MAX
1154 #    define NAME_MAX (FILENAME_MAX)
1155 #  else
1156 #    define NAME_MAX (260)
1157 #  endif
1158 #endif
1159
1160 /*
1161 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1162 */
1163
1164 #ifndef NULL_INTPTR_T
1165 #  define NULL_INTPTR_T ((intptr_t)(0))
1166 #endif
1167
1168 #ifndef BAD_INTPTR_T
1169 #  define BAD_INTPTR_T ((intptr_t)(-1))
1170 #endif
1171
1172 /*
1173 ** We need to provide the necessary structures and related types.
1174 */
1175
1176 #ifndef DIRENT_DEFINED
1177 #define DIRENT_DEFINED
1178 typedef struct DIRENT DIRENT;
1179 typedef DIRENT *LPDIRENT;
1180 struct DIRENT {
1181   ino_t d_ino;               /* Sequence number, do not use. */
1182   unsigned d_attributes;     /* Win32 file attributes. */
1183   char d_name[NAME_MAX + 1]; /* Name within the directory. */
1184 };
1185 #endif
1186
1187 #ifndef DIR_DEFINED
1188 #define DIR_DEFINED
1189 typedef struct DIR DIR;
1190 typedef DIR *LPDIR;
1191 struct DIR {
1192   intptr_t d_handle; /* Value returned by "_findfirst". */
1193   DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1194   DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1195 };
1196 #endif
1197
1198 /*
1199 ** Provide a macro, for use by the implementation, to determine if a
1200 ** particular directory entry should be skipped over when searching for
1201 ** the next directory entry that should be returned by the readdir() or
1202 ** readdir_r() functions.
1203 */
1204
1205 #ifndef is_filtered
1206 #  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1207 #endif
1208
1209 /*
1210 ** Provide the function prototype for the POSIX compatiable getenv()
1211 ** function.  This function is not thread-safe.
1212 */
1213
1214 extern const char *windirent_getenv(const char *name);
1215
1216 /*
1217 ** Finally, we can provide the function prototypes for the opendir(),
1218 ** readdir(), readdir_r(), and closedir() POSIX functions.
1219 */
1220
1221 extern LPDIR opendir(const char *dirname);
1222 extern LPDIRENT readdir(LPDIR dirp);
1223 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1224 extern INT closedir(LPDIR dirp);
1225
1226 #endif /* defined(WIN32) && defined(_MSC_VER) */
1227
1228 /************************* End test_windirent.h ********************/
1229 /************************* Begin test_windirent.c ******************/
1230 /*
1231 ** 2015 November 30
1232 **
1233 ** The author disclaims copyright to this source code.  In place of
1234 ** a legal notice, here is a blessing:
1235 **
1236 **    May you do good and not evil.
1237 **    May you find forgiveness for yourself and forgive others.
1238 **    May you share freely, never taking more than you give.
1239 **
1240 *************************************************************************
1241 ** This file contains code to implement most of the opendir() family of
1242 ** POSIX functions on Win32 using the MSVCRT.
1243 */
1244
1245 #if defined(_WIN32) && defined(_MSC_VER)
1246 /* #include "test_windirent.h" */
1247
1248 /*
1249 ** Implementation of the POSIX getenv() function using the Win32 API.
1250 ** This function is not thread-safe.
1251 */
1252 const char *windirent_getenv(
1253   const char *name
1254 ){
1255   static char value[32768]; /* Maximum length, per MSDN */
1256   DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1257   DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1258
1259   memset(value, 0, sizeof(value));
1260   dwRet = GetEnvironmentVariableA(name, value, dwSize);
1261   if( dwRet==0 || dwRet>dwSize ){
1262     /*
1263     ** The function call to GetEnvironmentVariableA() failed -OR-
1264     ** the buffer is not large enough.  Either way, return NULL.
1265     */
1266     return 0;
1267   }else{
1268     /*
1269     ** The function call to GetEnvironmentVariableA() succeeded
1270     ** -AND- the buffer contains the entire value.
1271     */
1272     return value;
1273   }
1274 }
1275
1276 /*
1277 ** Implementation of the POSIX opendir() function using the MSVCRT.
1278 */
1279 LPDIR opendir(
1280   const char *dirname
1281 ){
1282   struct _finddata_t data;
1283   LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1284   SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1285
1286   if( dirp==NULL ) return NULL;
1287   memset(dirp, 0, sizeof(DIR));
1288
1289   /* TODO: Remove this if Unix-style root paths are not used. */
1290   if( sqlite3_stricmp(dirname, "/")==0 ){
1291     dirname = windirent_getenv("SystemDrive");
1292   }
1293
1294   memset(&data, 0, sizeof(struct _finddata_t));
1295   _snprintf(data.name, namesize, "%s\\*", dirname);
1296   dirp->d_handle = _findfirst(data.name, &data);
1297
1298   if( dirp->d_handle==BAD_INTPTR_T ){
1299     closedir(dirp);
1300     return NULL;
1301   }
1302
1303   /* TODO: Remove this block to allow hidden and/or system files. */
1304   if( is_filtered(data) ){
1305 next:
1306
1307     memset(&data, 0, sizeof(struct _finddata_t));
1308     if( _findnext(dirp->d_handle, &data)==-1 ){
1309       closedir(dirp);
1310       return NULL;
1311     }
1312
1313     /* TODO: Remove this block to allow hidden and/or system files. */
1314     if( is_filtered(data) ) goto next;
1315   }
1316
1317   dirp->d_first.d_attributes = data.attrib;
1318   strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1319   dirp->d_first.d_name[NAME_MAX] = '\0';
1320
1321   return dirp;
1322 }
1323
1324 /*
1325 ** Implementation of the POSIX readdir() function using the MSVCRT.
1326 */
1327 LPDIRENT readdir(
1328   LPDIR dirp
1329 ){
1330   struct _finddata_t data;
1331
1332   if( dirp==NULL ) return NULL;
1333
1334   if( dirp->d_first.d_ino==0 ){
1335     dirp->d_first.d_ino++;
1336     dirp->d_next.d_ino++;
1337
1338     return &dirp->d_first;
1339   }
1340
1341 next:
1342
1343   memset(&data, 0, sizeof(struct _finddata_t));
1344   if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1345
1346   /* TODO: Remove this block to allow hidden and/or system files. */
1347   if( is_filtered(data) ) goto next;
1348
1349   dirp->d_next.d_ino++;
1350   dirp->d_next.d_attributes = data.attrib;
1351   strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1352   dirp->d_next.d_name[NAME_MAX] = '\0';
1353
1354   return &dirp->d_next;
1355 }
1356
1357 /*
1358 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1359 */
1360 INT readdir_r(
1361   LPDIR dirp,
1362   LPDIRENT entry,
1363   LPDIRENT *result
1364 ){
1365   struct _finddata_t data;
1366
1367   if( dirp==NULL ) return EBADF;
1368
1369   if( dirp->d_first.d_ino==0 ){
1370     dirp->d_first.d_ino++;
1371     dirp->d_next.d_ino++;
1372
1373     entry->d_ino = dirp->d_first.d_ino;
1374     entry->d_attributes = dirp->d_first.d_attributes;
1375     strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1376     entry->d_name[NAME_MAX] = '\0';
1377
1378     *result = entry;
1379     return 0;
1380   }
1381
1382 next:
1383
1384   memset(&data, 0, sizeof(struct _finddata_t));
1385   if( _findnext(dirp->d_handle, &data)==-1 ){
1386     *result = NULL;
1387     return ENOENT;
1388   }
1389
1390   /* TODO: Remove this block to allow hidden and/or system files. */
1391   if( is_filtered(data) ) goto next;
1392
1393   entry->d_ino = (ino_t)-1; /* not available */
1394   entry->d_attributes = data.attrib;
1395   strncpy(entry->d_name, data.name, NAME_MAX);
1396   entry->d_name[NAME_MAX] = '\0';
1397
1398   *result = entry;
1399   return 0;
1400 }
1401
1402 /*
1403 ** Implementation of the POSIX closedir() function using the MSVCRT.
1404 */
1405 INT closedir(
1406   LPDIR dirp
1407 ){
1408   INT result = 0;
1409
1410   if( dirp==NULL ) return EINVAL;
1411
1412   if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1413     result = _findclose(dirp->d_handle);
1414   }
1415
1416   sqlite3_free(dirp);
1417   return result;
1418 }
1419
1420 #endif /* defined(WIN32) && defined(_MSC_VER) */
1421
1422 /************************* End test_windirent.c ********************/
1423 #define dirent DIRENT
1424 #endif
1425 /************************* Begin ../ext/misc/shathree.c ******************/
1426 /*
1427 ** 2017-03-08
1428 **
1429 ** The author disclaims copyright to this source code.  In place of
1430 ** a legal notice, here is a blessing:
1431 **
1432 **    May you do good and not evil.
1433 **    May you find forgiveness for yourself and forgive others.
1434 **    May you share freely, never taking more than you give.
1435 **
1436 ******************************************************************************
1437 **
1438 ** This SQLite extension implements functions that compute SHA3 hashes.
1439 ** Two SQL functions are implemented:
1440 **
1441 **     sha3(X,SIZE)
1442 **     sha3_query(Y,SIZE)
1443 **
1444 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1445 ** X is NULL.
1446 **
1447 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1448 ** and returns a hash of their results.
1449 **
1450 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1451 ** is used.  If SIZE is included it must be one of the integers 224, 256,
1452 ** 384, or 512, to determine SHA3 hash variant that is computed.
1453 */
1454 /* #include "sqlite3ext.h" */
1455 SQLITE_EXTENSION_INIT1
1456 #include <assert.h>
1457 #include <string.h>
1458 #include <stdarg.h>
1459
1460 #ifndef SQLITE_AMALGAMATION
1461 /* typedef sqlite3_uint64 u64; */
1462 #endif /* SQLITE_AMALGAMATION */
1463
1464 /******************************************************************************
1465 ** The Hash Engine
1466 */
1467 /*
1468 ** Macros to determine whether the machine is big or little endian,
1469 ** and whether or not that determination is run-time or compile-time.
1470 **
1471 ** For best performance, an attempt is made to guess at the byte-order
1472 ** using C-preprocessor macros.  If that is unsuccessful, or if
1473 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1474 ** at run-time.
1475 */
1476 #ifndef SHA3_BYTEORDER
1477 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1478      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1479      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1480      defined(__arm__)
1481 #   define SHA3_BYTEORDER    1234
1482 # elif defined(sparc)    || defined(__ppc__)
1483 #   define SHA3_BYTEORDER    4321
1484 # else
1485 #   define SHA3_BYTEORDER 0
1486 # endif
1487 #endif
1488
1489
1490 /*
1491 ** State structure for a SHA3 hash in progress
1492 */
1493 typedef struct SHA3Context SHA3Context;
1494 struct SHA3Context {
1495   union {
1496     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1497     unsigned char x[1600];    /* ... or 1600 bytes */
1498   } u;
1499   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1500   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1501   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1502 };
1503
1504 /*
1505 ** A single step of the Keccak mixing function for a 1600-bit state
1506 */
1507 static void KeccakF1600Step(SHA3Context *p){
1508   int i;
1509   u64 b0, b1, b2, b3, b4;
1510   u64 c0, c1, c2, c3, c4;
1511   u64 d0, d1, d2, d3, d4;
1512   static const u64 RC[] = {
1513     0x0000000000000001ULL,  0x0000000000008082ULL,
1514     0x800000000000808aULL,  0x8000000080008000ULL,
1515     0x000000000000808bULL,  0x0000000080000001ULL,
1516     0x8000000080008081ULL,  0x8000000000008009ULL,
1517     0x000000000000008aULL,  0x0000000000000088ULL,
1518     0x0000000080008009ULL,  0x000000008000000aULL,
1519     0x000000008000808bULL,  0x800000000000008bULL,
1520     0x8000000000008089ULL,  0x8000000000008003ULL,
1521     0x8000000000008002ULL,  0x8000000000000080ULL,
1522     0x000000000000800aULL,  0x800000008000000aULL,
1523     0x8000000080008081ULL,  0x8000000000008080ULL,
1524     0x0000000080000001ULL,  0x8000000080008008ULL
1525   };
1526 # define a00 (p->u.s[0])
1527 # define a01 (p->u.s[1])
1528 # define a02 (p->u.s[2])
1529 # define a03 (p->u.s[3])
1530 # define a04 (p->u.s[4])
1531 # define a10 (p->u.s[5])
1532 # define a11 (p->u.s[6])
1533 # define a12 (p->u.s[7])
1534 # define a13 (p->u.s[8])
1535 # define a14 (p->u.s[9])
1536 # define a20 (p->u.s[10])
1537 # define a21 (p->u.s[11])
1538 # define a22 (p->u.s[12])
1539 # define a23 (p->u.s[13])
1540 # define a24 (p->u.s[14])
1541 # define a30 (p->u.s[15])
1542 # define a31 (p->u.s[16])
1543 # define a32 (p->u.s[17])
1544 # define a33 (p->u.s[18])
1545 # define a34 (p->u.s[19])
1546 # define a40 (p->u.s[20])
1547 # define a41 (p->u.s[21])
1548 # define a42 (p->u.s[22])
1549 # define a43 (p->u.s[23])
1550 # define a44 (p->u.s[24])
1551 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1552
1553   for(i=0; i<24; i+=4){
1554     c0 = a00^a10^a20^a30^a40;
1555     c1 = a01^a11^a21^a31^a41;
1556     c2 = a02^a12^a22^a32^a42;
1557     c3 = a03^a13^a23^a33^a43;
1558     c4 = a04^a14^a24^a34^a44;
1559     d0 = c4^ROL64(c1, 1);
1560     d1 = c0^ROL64(c2, 1);
1561     d2 = c1^ROL64(c3, 1);
1562     d3 = c2^ROL64(c4, 1);
1563     d4 = c3^ROL64(c0, 1);
1564
1565     b0 = (a00^d0);
1566     b1 = ROL64((a11^d1), 44);
1567     b2 = ROL64((a22^d2), 43);
1568     b3 = ROL64((a33^d3), 21);
1569     b4 = ROL64((a44^d4), 14);
1570     a00 =   b0 ^((~b1)&  b2 );
1571     a00 ^= RC[i];
1572     a11 =   b1 ^((~b2)&  b3 );
1573     a22 =   b2 ^((~b3)&  b4 );
1574     a33 =   b3 ^((~b4)&  b0 );
1575     a44 =   b4 ^((~b0)&  b1 );
1576
1577     b2 = ROL64((a20^d0), 3);
1578     b3 = ROL64((a31^d1), 45);
1579     b4 = ROL64((a42^d2), 61);
1580     b0 = ROL64((a03^d3), 28);
1581     b1 = ROL64((a14^d4), 20);
1582     a20 =   b0 ^((~b1)&  b2 );
1583     a31 =   b1 ^((~b2)&  b3 );
1584     a42 =   b2 ^((~b3)&  b4 );
1585     a03 =   b3 ^((~b4)&  b0 );
1586     a14 =   b4 ^((~b0)&  b1 );
1587
1588     b4 = ROL64((a40^d0), 18);
1589     b0 = ROL64((a01^d1), 1);
1590     b1 = ROL64((a12^d2), 6);
1591     b2 = ROL64((a23^d3), 25);
1592     b3 = ROL64((a34^d4), 8);
1593     a40 =   b0 ^((~b1)&  b2 );
1594     a01 =   b1 ^((~b2)&  b3 );
1595     a12 =   b2 ^((~b3)&  b4 );
1596     a23 =   b3 ^((~b4)&  b0 );
1597     a34 =   b4 ^((~b0)&  b1 );
1598
1599     b1 = ROL64((a10^d0), 36);
1600     b2 = ROL64((a21^d1), 10);
1601     b3 = ROL64((a32^d2), 15);
1602     b4 = ROL64((a43^d3), 56);
1603     b0 = ROL64((a04^d4), 27);
1604     a10 =   b0 ^((~b1)&  b2 );
1605     a21 =   b1 ^((~b2)&  b3 );
1606     a32 =   b2 ^((~b3)&  b4 );
1607     a43 =   b3 ^((~b4)&  b0 );
1608     a04 =   b4 ^((~b0)&  b1 );
1609
1610     b3 = ROL64((a30^d0), 41);
1611     b4 = ROL64((a41^d1), 2);
1612     b0 = ROL64((a02^d2), 62);
1613     b1 = ROL64((a13^d3), 55);
1614     b2 = ROL64((a24^d4), 39);
1615     a30 =   b0 ^((~b1)&  b2 );
1616     a41 =   b1 ^((~b2)&  b3 );
1617     a02 =   b2 ^((~b3)&  b4 );
1618     a13 =   b3 ^((~b4)&  b0 );
1619     a24 =   b4 ^((~b0)&  b1 );
1620
1621     c0 = a00^a20^a40^a10^a30;
1622     c1 = a11^a31^a01^a21^a41;
1623     c2 = a22^a42^a12^a32^a02;
1624     c3 = a33^a03^a23^a43^a13;
1625     c4 = a44^a14^a34^a04^a24;
1626     d0 = c4^ROL64(c1, 1);
1627     d1 = c0^ROL64(c2, 1);
1628     d2 = c1^ROL64(c3, 1);
1629     d3 = c2^ROL64(c4, 1);
1630     d4 = c3^ROL64(c0, 1);
1631
1632     b0 = (a00^d0);
1633     b1 = ROL64((a31^d1), 44);
1634     b2 = ROL64((a12^d2), 43);
1635     b3 = ROL64((a43^d3), 21);
1636     b4 = ROL64((a24^d4), 14);
1637     a00 =   b0 ^((~b1)&  b2 );
1638     a00 ^= RC[i+1];
1639     a31 =   b1 ^((~b2)&  b3 );
1640     a12 =   b2 ^((~b3)&  b4 );
1641     a43 =   b3 ^((~b4)&  b0 );
1642     a24 =   b4 ^((~b0)&  b1 );
1643
1644     b2 = ROL64((a40^d0), 3);
1645     b3 = ROL64((a21^d1), 45);
1646     b4 = ROL64((a02^d2), 61);
1647     b0 = ROL64((a33^d3), 28);
1648     b1 = ROL64((a14^d4), 20);
1649     a40 =   b0 ^((~b1)&  b2 );
1650     a21 =   b1 ^((~b2)&  b3 );
1651     a02 =   b2 ^((~b3)&  b4 );
1652     a33 =   b3 ^((~b4)&  b0 );
1653     a14 =   b4 ^((~b0)&  b1 );
1654
1655     b4 = ROL64((a30^d0), 18);
1656     b0 = ROL64((a11^d1), 1);
1657     b1 = ROL64((a42^d2), 6);
1658     b2 = ROL64((a23^d3), 25);
1659     b3 = ROL64((a04^d4), 8);
1660     a30 =   b0 ^((~b1)&  b2 );
1661     a11 =   b1 ^((~b2)&  b3 );
1662     a42 =   b2 ^((~b3)&  b4 );
1663     a23 =   b3 ^((~b4)&  b0 );
1664     a04 =   b4 ^((~b0)&  b1 );
1665
1666     b1 = ROL64((a20^d0), 36);
1667     b2 = ROL64((a01^d1), 10);
1668     b3 = ROL64((a32^d2), 15);
1669     b4 = ROL64((a13^d3), 56);
1670     b0 = ROL64((a44^d4), 27);
1671     a20 =   b0 ^((~b1)&  b2 );
1672     a01 =   b1 ^((~b2)&  b3 );
1673     a32 =   b2 ^((~b3)&  b4 );
1674     a13 =   b3 ^((~b4)&  b0 );
1675     a44 =   b4 ^((~b0)&  b1 );
1676
1677     b3 = ROL64((a10^d0), 41);
1678     b4 = ROL64((a41^d1), 2);
1679     b0 = ROL64((a22^d2), 62);
1680     b1 = ROL64((a03^d3), 55);
1681     b2 = ROL64((a34^d4), 39);
1682     a10 =   b0 ^((~b1)&  b2 );
1683     a41 =   b1 ^((~b2)&  b3 );
1684     a22 =   b2 ^((~b3)&  b4 );
1685     a03 =   b3 ^((~b4)&  b0 );
1686     a34 =   b4 ^((~b0)&  b1 );
1687
1688     c0 = a00^a40^a30^a20^a10;
1689     c1 = a31^a21^a11^a01^a41;
1690     c2 = a12^a02^a42^a32^a22;
1691     c3 = a43^a33^a23^a13^a03;
1692     c4 = a24^a14^a04^a44^a34;
1693     d0 = c4^ROL64(c1, 1);
1694     d1 = c0^ROL64(c2, 1);
1695     d2 = c1^ROL64(c3, 1);
1696     d3 = c2^ROL64(c4, 1);
1697     d4 = c3^ROL64(c0, 1);
1698
1699     b0 = (a00^d0);
1700     b1 = ROL64((a21^d1), 44);
1701     b2 = ROL64((a42^d2), 43);
1702     b3 = ROL64((a13^d3), 21);
1703     b4 = ROL64((a34^d4), 14);
1704     a00 =   b0 ^((~b1)&  b2 );
1705     a00 ^= RC[i+2];
1706     a21 =   b1 ^((~b2)&  b3 );
1707     a42 =   b2 ^((~b3)&  b4 );
1708     a13 =   b3 ^((~b4)&  b0 );
1709     a34 =   b4 ^((~b0)&  b1 );
1710
1711     b2 = ROL64((a30^d0), 3);
1712     b3 = ROL64((a01^d1), 45);
1713     b4 = ROL64((a22^d2), 61);
1714     b0 = ROL64((a43^d3), 28);
1715     b1 = ROL64((a14^d4), 20);
1716     a30 =   b0 ^((~b1)&  b2 );
1717     a01 =   b1 ^((~b2)&  b3 );
1718     a22 =   b2 ^((~b3)&  b4 );
1719     a43 =   b3 ^((~b4)&  b0 );
1720     a14 =   b4 ^((~b0)&  b1 );
1721
1722     b4 = ROL64((a10^d0), 18);
1723     b0 = ROL64((a31^d1), 1);
1724     b1 = ROL64((a02^d2), 6);
1725     b2 = ROL64((a23^d3), 25);
1726     b3 = ROL64((a44^d4), 8);
1727     a10 =   b0 ^((~b1)&  b2 );
1728     a31 =   b1 ^((~b2)&  b3 );
1729     a02 =   b2 ^((~b3)&  b4 );
1730     a23 =   b3 ^((~b4)&  b0 );
1731     a44 =   b4 ^((~b0)&  b1 );
1732
1733     b1 = ROL64((a40^d0), 36);
1734     b2 = ROL64((a11^d1), 10);
1735     b3 = ROL64((a32^d2), 15);
1736     b4 = ROL64((a03^d3), 56);
1737     b0 = ROL64((a24^d4), 27);
1738     a40 =   b0 ^((~b1)&  b2 );
1739     a11 =   b1 ^((~b2)&  b3 );
1740     a32 =   b2 ^((~b3)&  b4 );
1741     a03 =   b3 ^((~b4)&  b0 );
1742     a24 =   b4 ^((~b0)&  b1 );
1743
1744     b3 = ROL64((a20^d0), 41);
1745     b4 = ROL64((a41^d1), 2);
1746     b0 = ROL64((a12^d2), 62);
1747     b1 = ROL64((a33^d3), 55);
1748     b2 = ROL64((a04^d4), 39);
1749     a20 =   b0 ^((~b1)&  b2 );
1750     a41 =   b1 ^((~b2)&  b3 );
1751     a12 =   b2 ^((~b3)&  b4 );
1752     a33 =   b3 ^((~b4)&  b0 );
1753     a04 =   b4 ^((~b0)&  b1 );
1754
1755     c0 = a00^a30^a10^a40^a20;
1756     c1 = a21^a01^a31^a11^a41;
1757     c2 = a42^a22^a02^a32^a12;
1758     c3 = a13^a43^a23^a03^a33;
1759     c4 = a34^a14^a44^a24^a04;
1760     d0 = c4^ROL64(c1, 1);
1761     d1 = c0^ROL64(c2, 1);
1762     d2 = c1^ROL64(c3, 1);
1763     d3 = c2^ROL64(c4, 1);
1764     d4 = c3^ROL64(c0, 1);
1765
1766     b0 = (a00^d0);
1767     b1 = ROL64((a01^d1), 44);
1768     b2 = ROL64((a02^d2), 43);
1769     b3 = ROL64((a03^d3), 21);
1770     b4 = ROL64((a04^d4), 14);
1771     a00 =   b0 ^((~b1)&  b2 );
1772     a00 ^= RC[i+3];
1773     a01 =   b1 ^((~b2)&  b3 );
1774     a02 =   b2 ^((~b3)&  b4 );
1775     a03 =   b3 ^((~b4)&  b0 );
1776     a04 =   b4 ^((~b0)&  b1 );
1777
1778     b2 = ROL64((a10^d0), 3);
1779     b3 = ROL64((a11^d1), 45);
1780     b4 = ROL64((a12^d2), 61);
1781     b0 = ROL64((a13^d3), 28);
1782     b1 = ROL64((a14^d4), 20);
1783     a10 =   b0 ^((~b1)&  b2 );
1784     a11 =   b1 ^((~b2)&  b3 );
1785     a12 =   b2 ^((~b3)&  b4 );
1786     a13 =   b3 ^((~b4)&  b0 );
1787     a14 =   b4 ^((~b0)&  b1 );
1788
1789     b4 = ROL64((a20^d0), 18);
1790     b0 = ROL64((a21^d1), 1);
1791     b1 = ROL64((a22^d2), 6);
1792     b2 = ROL64((a23^d3), 25);
1793     b3 = ROL64((a24^d4), 8);
1794     a20 =   b0 ^((~b1)&  b2 );
1795     a21 =   b1 ^((~b2)&  b3 );
1796     a22 =   b2 ^((~b3)&  b4 );
1797     a23 =   b3 ^((~b4)&  b0 );
1798     a24 =   b4 ^((~b0)&  b1 );
1799
1800     b1 = ROL64((a30^d0), 36);
1801     b2 = ROL64((a31^d1), 10);
1802     b3 = ROL64((a32^d2), 15);
1803     b4 = ROL64((a33^d3), 56);
1804     b0 = ROL64((a34^d4), 27);
1805     a30 =   b0 ^((~b1)&  b2 );
1806     a31 =   b1 ^((~b2)&  b3 );
1807     a32 =   b2 ^((~b3)&  b4 );
1808     a33 =   b3 ^((~b4)&  b0 );
1809     a34 =   b4 ^((~b0)&  b1 );
1810
1811     b3 = ROL64((a40^d0), 41);
1812     b4 = ROL64((a41^d1), 2);
1813     b0 = ROL64((a42^d2), 62);
1814     b1 = ROL64((a43^d3), 55);
1815     b2 = ROL64((a44^d4), 39);
1816     a40 =   b0 ^((~b1)&  b2 );
1817     a41 =   b1 ^((~b2)&  b3 );
1818     a42 =   b2 ^((~b3)&  b4 );
1819     a43 =   b3 ^((~b4)&  b0 );
1820     a44 =   b4 ^((~b0)&  b1 );
1821   }
1822 }
1823
1824 /*
1825 ** Initialize a new hash.  iSize determines the size of the hash
1826 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1827 ** can be zero to use the default hash size of 256 bits.
1828 */
1829 static void SHA3Init(SHA3Context *p, int iSize){
1830   memset(p, 0, sizeof(*p));
1831   if( iSize>=128 && iSize<=512 ){
1832     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1833   }else{
1834     p->nRate = (1600 - 2*256)/8;
1835   }
1836 #if SHA3_BYTEORDER==1234
1837   /* Known to be little-endian at compile-time. No-op */
1838 #elif SHA3_BYTEORDER==4321
1839   p->ixMask = 7;  /* Big-endian */
1840 #else
1841   {
1842     static unsigned int one = 1;
1843     if( 1==*(unsigned char*)&one ){
1844       /* Little endian.  No byte swapping. */
1845       p->ixMask = 0;
1846     }else{
1847       /* Big endian.  Byte swap. */
1848       p->ixMask = 7;
1849     }
1850   }
1851 #endif
1852 }
1853
1854 /*
1855 ** Make consecutive calls to the SHA3Update function to add new content
1856 ** to the hash
1857 */
1858 static void SHA3Update(
1859   SHA3Context *p,
1860   const unsigned char *aData,
1861   unsigned int nData
1862 ){
1863   unsigned int i = 0;
1864 #if SHA3_BYTEORDER==1234
1865   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1866     for(; i+7<nData; i+=8){
1867       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1868       p->nLoaded += 8;
1869       if( p->nLoaded>=p->nRate ){
1870         KeccakF1600Step(p);
1871         p->nLoaded = 0;
1872       }
1873     }
1874   }
1875 #endif
1876   for(; i<nData; i++){
1877 #if SHA3_BYTEORDER==1234
1878     p->u.x[p->nLoaded] ^= aData[i];
1879 #elif SHA3_BYTEORDER==4321
1880     p->u.x[p->nLoaded^0x07] ^= aData[i];
1881 #else
1882     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1883 #endif
1884     p->nLoaded++;
1885     if( p->nLoaded==p->nRate ){
1886       KeccakF1600Step(p);
1887       p->nLoaded = 0;
1888     }
1889   }
1890 }
1891
1892 /*
1893 ** After all content has been added, invoke SHA3Final() to compute
1894 ** the final hash.  The function returns a pointer to the binary
1895 ** hash value.
1896 */
1897 static unsigned char *SHA3Final(SHA3Context *p){
1898   unsigned int i;
1899   if( p->nLoaded==p->nRate-1 ){
1900     const unsigned char c1 = 0x86;
1901     SHA3Update(p, &c1, 1);
1902   }else{
1903     const unsigned char c2 = 0x06;
1904     const unsigned char c3 = 0x80;
1905     SHA3Update(p, &c2, 1);
1906     p->nLoaded = p->nRate - 1;
1907     SHA3Update(p, &c3, 1);
1908   }
1909   for(i=0; i<p->nRate; i++){
1910     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1911   }
1912   return &p->u.x[p->nRate];
1913 }
1914 /* End of the hashing logic
1915 *****************************************************************************/
1916
1917 /*
1918 ** Implementation of the sha3(X,SIZE) function.
1919 **
1920 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1921 ** size is 256.  If X is a BLOB, it is hashed as is.  
1922 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1923 ** and the string is hashed without the trailing 0x00 terminator.  The hash
1924 ** of a NULL value is NULL.
1925 */
1926 static void sha3Func(
1927   sqlite3_context *context,
1928   int argc,
1929   sqlite3_value **argv
1930 ){
1931   SHA3Context cx;
1932   int eType = sqlite3_value_type(argv[0]);
1933   int nByte = sqlite3_value_bytes(argv[0]);
1934   int iSize;
1935   if( argc==1 ){
1936     iSize = 256;
1937   }else{
1938     iSize = sqlite3_value_int(argv[1]);
1939     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1940       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1941                                     "384 512", -1);
1942       return;
1943     }
1944   }
1945   if( eType==SQLITE_NULL ) return;
1946   SHA3Init(&cx, iSize);
1947   if( eType==SQLITE_BLOB ){
1948     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1949   }else{
1950     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1951   }
1952   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1953 }
1954
1955 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1956 ** of 50 bytes and add it to the hash.
1957 */
1958 static void hash_step_vformat(
1959   SHA3Context *p,                 /* Add content to this context */
1960   const char *zFormat,
1961   ...
1962 ){
1963   va_list ap;
1964   int n;
1965   char zBuf[50];
1966   va_start(ap, zFormat);
1967   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1968   va_end(ap);
1969   n = (int)strlen(zBuf);
1970   SHA3Update(p, (unsigned char*)zBuf, n);
1971 }
1972
1973 /*
1974 ** Implementation of the sha3_query(SQL,SIZE) function.
1975 **
1976 ** This function compiles and runs the SQL statement(s) given in the
1977 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
1978 ** size is 256.
1979 **
1980 ** The format of the byte stream that is hashed is summarized as follows:
1981 **
1982 **       S<n>:<sql>
1983 **       R
1984 **       N
1985 **       I<int>
1986 **       F<ieee-float>
1987 **       B<size>:<bytes>
1988 **       T<size>:<text>
1989 **
1990 ** <sql> is the original SQL text for each statement run and <n> is
1991 ** the size of that text.  The SQL text is UTF-8.  A single R character
1992 ** occurs before the start of each row.  N means a NULL value.
1993 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
1994 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1995 ** B means blobs of <size> bytes.  T means text rendered as <size>
1996 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
1997 ** text integers.
1998 **
1999 ** For each SQL statement in the X input, there is one S segment.  Each
2000 ** S segment is followed by zero or more R segments, one for each row in the
2001 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
2002 ** one for each column in the result set.  Segments are concatentated directly
2003 ** with no delimiters of any kind.
2004 */
2005 static void sha3QueryFunc(
2006   sqlite3_context *context,
2007   int argc,
2008   sqlite3_value **argv
2009 ){
2010   sqlite3 *db = sqlite3_context_db_handle(context);
2011   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
2012   sqlite3_stmt *pStmt = 0;
2013   int nCol;                   /* Number of columns in the result set */
2014   int i;                      /* Loop counter */
2015   int rc;
2016   int n;
2017   const char *z;
2018   SHA3Context cx;
2019   int iSize;
2020
2021   if( argc==1 ){
2022     iSize = 256;
2023   }else{
2024     iSize = sqlite3_value_int(argv[1]);
2025     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
2026       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
2027                                     "384 512", -1);
2028       return;
2029     }
2030   }
2031   if( zSql==0 ) return;
2032   SHA3Init(&cx, iSize);
2033   while( zSql[0] ){
2034     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2035     if( rc ){
2036       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2037                                    zSql, sqlite3_errmsg(db));
2038       sqlite3_finalize(pStmt);
2039       sqlite3_result_error(context, zMsg, -1);
2040       sqlite3_free(zMsg);
2041       return;
2042     }
2043     if( !sqlite3_stmt_readonly(pStmt) ){
2044       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2045       sqlite3_finalize(pStmt);
2046       sqlite3_result_error(context, zMsg, -1);
2047       sqlite3_free(zMsg);
2048       return;
2049     }
2050     nCol = sqlite3_column_count(pStmt);
2051     z = sqlite3_sql(pStmt);
2052     if( z ){
2053       n = (int)strlen(z);
2054       hash_step_vformat(&cx,"S%d:",n);
2055       SHA3Update(&cx,(unsigned char*)z,n);
2056     }
2057
2058     /* Compute a hash over the result of the query */
2059     while( SQLITE_ROW==sqlite3_step(pStmt) ){
2060       SHA3Update(&cx,(const unsigned char*)"R",1);
2061       for(i=0; i<nCol; i++){
2062         switch( sqlite3_column_type(pStmt,i) ){
2063           case SQLITE_NULL: {
2064             SHA3Update(&cx, (const unsigned char*)"N",1);
2065             break;
2066           }
2067           case SQLITE_INTEGER: {
2068             sqlite3_uint64 u;
2069             int j;
2070             unsigned char x[9];
2071             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2072             memcpy(&u, &v, 8);
2073             for(j=8; j>=1; j--){
2074               x[j] = u & 0xff;
2075               u >>= 8;
2076             }
2077             x[0] = 'I';
2078             SHA3Update(&cx, x, 9);
2079             break;
2080           }
2081           case SQLITE_FLOAT: {
2082             sqlite3_uint64 u;
2083             int j;
2084             unsigned char x[9];
2085             double r = sqlite3_column_double(pStmt,i);
2086             memcpy(&u, &r, 8);
2087             for(j=8; j>=1; j--){
2088               x[j] = u & 0xff;
2089               u >>= 8;
2090             }
2091             x[0] = 'F';
2092             SHA3Update(&cx,x,9);
2093             break;
2094           }
2095           case SQLITE_TEXT: {
2096             int n2 = sqlite3_column_bytes(pStmt, i);
2097             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2098             hash_step_vformat(&cx,"T%d:",n2);
2099             SHA3Update(&cx, z2, n2);
2100             break;
2101           }
2102           case SQLITE_BLOB: {
2103             int n2 = sqlite3_column_bytes(pStmt, i);
2104             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2105             hash_step_vformat(&cx,"B%d:",n2);
2106             SHA3Update(&cx, z2, n2);
2107             break;
2108           }
2109         }
2110       }
2111     }
2112     sqlite3_finalize(pStmt);
2113   }
2114   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2115 }
2116
2117
2118 #ifdef _WIN32
2119
2120 #endif
2121 int sqlite3_shathree_init(
2122   sqlite3 *db,
2123   char **pzErrMsg,
2124   const sqlite3_api_routines *pApi
2125 ){
2126   int rc = SQLITE_OK;
2127   SQLITE_EXTENSION_INIT2(pApi);
2128   (void)pzErrMsg;  /* Unused parameter */
2129   rc = sqlite3_create_function(db, "sha3", 1,
2130                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2131                       0, sha3Func, 0, 0);
2132   if( rc==SQLITE_OK ){
2133     rc = sqlite3_create_function(db, "sha3", 2,
2134                       SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2135                       0, sha3Func, 0, 0);
2136   }
2137   if( rc==SQLITE_OK ){
2138     rc = sqlite3_create_function(db, "sha3_query", 1,
2139                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2140                       0, sha3QueryFunc, 0, 0);
2141   }
2142   if( rc==SQLITE_OK ){
2143     rc = sqlite3_create_function(db, "sha3_query", 2,
2144                       SQLITE_UTF8 | SQLITE_DIRECTONLY,
2145                       0, sha3QueryFunc, 0, 0);
2146   }
2147   return rc;
2148 }
2149
2150 /************************* End ../ext/misc/shathree.c ********************/
2151 /************************* Begin ../ext/misc/fileio.c ******************/
2152 /*
2153 ** 2014-06-13
2154 **
2155 ** The author disclaims copyright to this source code.  In place of
2156 ** a legal notice, here is a blessing:
2157 **
2158 **    May you do good and not evil.
2159 **    May you find forgiveness for yourself and forgive others.
2160 **    May you share freely, never taking more than you give.
2161 **
2162 ******************************************************************************
2163 **
2164 ** This SQLite extension implements SQL functions readfile() and
2165 ** writefile(), and eponymous virtual type "fsdir".
2166 **
2167 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2168 **
2169 **   If neither of the optional arguments is present, then this UDF
2170 **   function writes blob DATA to file FILE. If successful, the number
2171 **   of bytes written is returned. If an error occurs, NULL is returned.
2172 **
2173 **   If the first option argument - MODE - is present, then it must
2174 **   be passed an integer value that corresponds to a POSIX mode
2175 **   value (file type + permissions, as returned in the stat.st_mode
2176 **   field by the stat() system call). Three types of files may
2177 **   be written/created:
2178 **
2179 **     regular files:  (mode & 0170000)==0100000
2180 **     symbolic links: (mode & 0170000)==0120000
2181 **     directories:    (mode & 0170000)==0040000
2182 **
2183 **   For a directory, the DATA is ignored. For a symbolic link, it is
2184 **   interpreted as text and used as the target of the link. For a
2185 **   regular file, it is interpreted as a blob and written into the
2186 **   named file. Regardless of the type of file, its permissions are
2187 **   set to (mode & 0777) before returning.
2188 **
2189 **   If the optional MTIME argument is present, then it is interpreted
2190 **   as an integer - the number of seconds since the unix epoch. The
2191 **   modification-time of the target file is set to this value before
2192 **   returning.
2193 **
2194 **   If three or more arguments are passed to this function and an
2195 **   error is encountered, an exception is raised.
2196 **
2197 ** READFILE(FILE):
2198 **
2199 **   Read and return the contents of file FILE (type blob) from disk.
2200 **
2201 ** FSDIR:
2202 **
2203 **   Used as follows:
2204 **
2205 **     SELECT * FROM fsdir($path [, $dir]);
2206 **
2207 **   Parameter $path is an absolute or relative pathname. If the file that it
2208 **   refers to does not exist, it is an error. If the path refers to a regular
2209 **   file or symbolic link, it returns a single row. Or, if the path refers
2210 **   to a directory, it returns one row for the directory, and one row for each
2211 **   file within the hierarchy rooted at $path.
2212 **
2213 **   Each row has the following columns:
2214 **
2215 **     name:  Path to file or directory (text value).
2216 **     mode:  Value of stat.st_mode for directory entry (an integer).
2217 **     mtime: Value of stat.st_mtime for directory entry (an integer).
2218 **     data:  For a regular file, a blob containing the file data. For a
2219 **            symlink, a text value containing the text of the link. For a
2220 **            directory, NULL.
2221 **
2222 **   If a non-NULL value is specified for the optional $dir parameter and
2223 **   $path is a relative path, then $path is interpreted relative to $dir. 
2224 **   And the paths returned in the "name" column of the table are also 
2225 **   relative to directory $dir.
2226 **
2227 ** Notes on building this extension for Windows:
2228 **   Unless linked statically with the SQLite library, a preprocessor
2229 **   symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
2230 **   DLL form of this extension for WIN32. See its use below for details.
2231 */
2232 /* #include "sqlite3ext.h" */
2233 SQLITE_EXTENSION_INIT1
2234 #include <stdio.h>
2235 #include <string.h>
2236 #include <assert.h>
2237
2238 #include <sys/types.h>
2239 #include <sys/stat.h>
2240 #include <fcntl.h>
2241 #if !defined(_WIN32) && !defined(WIN32)
2242 #  include <unistd.h>
2243 #  include <dirent.h>
2244 #  include <utime.h>
2245 #  include <sys/time.h>
2246 #else
2247 #  include "windows.h"
2248 #  include <io.h>
2249 #  include <direct.h>
2250 /* #  include "test_windirent.h" */
2251 #  define dirent DIRENT
2252 #  ifndef chmod
2253 #    define chmod _chmod
2254 #  endif
2255 #  ifndef stat
2256 #    define stat _stat
2257 #  endif
2258 #  define mkdir(path,mode) _mkdir(path)
2259 #  define lstat(path,buf) stat(path,buf)
2260 #endif
2261 #include <time.h>
2262 #include <errno.h>
2263
2264
2265 /*
2266 ** Structure of the fsdir() table-valued function
2267 */
2268                  /*    0    1    2     3    4           5             */
2269 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2270 #define FSDIR_COLUMN_NAME     0     /* Name of the file */
2271 #define FSDIR_COLUMN_MODE     1     /* Access mode */
2272 #define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2273 #define FSDIR_COLUMN_DATA     3     /* File content */
2274 #define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2275 #define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2276
2277
2278 /*
2279 ** Set the result stored by context ctx to a blob containing the 
2280 ** contents of file zName.  Or, leave the result unchanged (NULL)
2281 ** if the file does not exist or is unreadable.
2282 **
2283 ** If the file exceeds the SQLite blob size limit, through an
2284 ** SQLITE_TOOBIG error.
2285 **
2286 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2287 ** off of disk.
2288 */
2289 static void readFileContents(sqlite3_context *ctx, const char *zName){
2290   FILE *in;
2291   sqlite3_int64 nIn;
2292   void *pBuf;
2293   sqlite3 *db;
2294   int mxBlob;
2295
2296   in = fopen(zName, "rb");
2297   if( in==0 ){
2298     /* File does not exist or is unreadable. Leave the result set to NULL. */
2299     return;
2300   }
2301   fseek(in, 0, SEEK_END);
2302   nIn = ftell(in);
2303   rewind(in);
2304   db = sqlite3_context_db_handle(ctx);
2305   mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2306   if( nIn>mxBlob ){
2307     sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2308     fclose(in);
2309     return;
2310   }
2311   pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2312   if( pBuf==0 ){
2313     sqlite3_result_error_nomem(ctx);
2314     fclose(in);
2315     return;
2316   }
2317   if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2318     sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2319   }else{
2320     sqlite3_result_error_code(ctx, SQLITE_IOERR);
2321     sqlite3_free(pBuf);
2322   }
2323   fclose(in);
2324 }
2325
2326 /*
2327 ** Implementation of the "readfile(X)" SQL function.  The entire content
2328 ** of the file named X is read and returned as a BLOB.  NULL is returned
2329 ** if the file does not exist or is unreadable.
2330 */
2331 static void readfileFunc(
2332   sqlite3_context *context,
2333   int argc,
2334   sqlite3_value **argv
2335 ){
2336   const char *zName;
2337   (void)(argc);  /* Unused parameter */
2338   zName = (const char*)sqlite3_value_text(argv[0]);
2339   if( zName==0 ) return;
2340   readFileContents(context, zName);
2341 }
2342
2343 /*
2344 ** Set the error message contained in context ctx to the results of
2345 ** vprintf(zFmt, ...).
2346 */
2347 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2348   char *zMsg = 0;
2349   va_list ap;
2350   va_start(ap, zFmt);
2351   zMsg = sqlite3_vmprintf(zFmt, ap);
2352   sqlite3_result_error(ctx, zMsg, -1);
2353   sqlite3_free(zMsg);
2354   va_end(ap);
2355 }
2356
2357 #if defined(_WIN32)
2358 /*
2359 ** This function is designed to convert a Win32 FILETIME structure into the
2360 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2361 */
2362 static sqlite3_uint64 fileTimeToUnixTime(
2363   LPFILETIME pFileTime
2364 ){
2365   SYSTEMTIME epochSystemTime;
2366   ULARGE_INTEGER epochIntervals;
2367   FILETIME epochFileTime;
2368   ULARGE_INTEGER fileIntervals;
2369
2370   memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2371   epochSystemTime.wYear = 1970;
2372   epochSystemTime.wMonth = 1;
2373   epochSystemTime.wDay = 1;
2374   SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2375   epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2376   epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2377
2378   fileIntervals.LowPart = pFileTime->dwLowDateTime;
2379   fileIntervals.HighPart = pFileTime->dwHighDateTime;
2380
2381   return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2382 }
2383
2384
2385 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
2386 #  /* To allow a standalone DLL, use this next replacement function: */
2387 #  undef sqlite3_win32_utf8_to_unicode
2388 #  define sqlite3_win32_utf8_to_unicode utf8_to_utf16
2389 #
2390 LPWSTR utf8_to_utf16(const char *z){
2391   int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
2392   LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
2393   if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
2394     return rv;
2395   sqlite3_free(rv);
2396   return 0;
2397 }
2398 #endif
2399
2400 /*
2401 ** This function attempts to normalize the time values found in the stat()
2402 ** buffer to UTC.  This is necessary on Win32, where the runtime library
2403 ** appears to return these values as local times.
2404 */
2405 static void statTimesToUtc(
2406   const char *zPath,
2407   struct stat *pStatBuf
2408 ){
2409   HANDLE hFindFile;
2410   WIN32_FIND_DATAW fd;
2411   LPWSTR zUnicodeName;
2412   extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2413   zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2414   if( zUnicodeName ){
2415     memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2416     hFindFile = FindFirstFileW(zUnicodeName, &fd);
2417     if( hFindFile!=NULL ){
2418       pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2419       pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2420       pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2421       FindClose(hFindFile);
2422     }
2423     sqlite3_free(zUnicodeName);
2424   }
2425 }
2426 #endif
2427
2428 /*
2429 ** This function is used in place of stat().  On Windows, special handling
2430 ** is required in order for the included time to be returned as UTC.  On all
2431 ** other systems, this function simply calls stat().
2432 */
2433 static int fileStat(
2434   const char *zPath,
2435   struct stat *pStatBuf
2436 ){
2437 #if defined(_WIN32)
2438   int rc = stat(zPath, pStatBuf);
2439   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2440   return rc;
2441 #else
2442   return stat(zPath, pStatBuf);
2443 #endif
2444 }
2445
2446 /*
2447 ** This function is used in place of lstat().  On Windows, special handling
2448 ** is required in order for the included time to be returned as UTC.  On all
2449 ** other systems, this function simply calls lstat().
2450 */
2451 static int fileLinkStat(
2452   const char *zPath,
2453   struct stat *pStatBuf
2454 ){
2455 #if defined(_WIN32)
2456   int rc = lstat(zPath, pStatBuf);
2457   if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2458   return rc;
2459 #else
2460   return lstat(zPath, pStatBuf);
2461 #endif
2462 }
2463
2464 /*
2465 ** Argument zFile is the name of a file that will be created and/or written
2466 ** by SQL function writefile(). This function ensures that the directory
2467 ** zFile will be written to exists, creating it if required. The permissions
2468 ** for any path components created by this function are set in accordance
2469 ** with the current umask.
2470 **
2471 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2472 ** SQLITE_OK is returned if the directory is successfully created, or
2473 ** SQLITE_ERROR otherwise.
2474 */
2475 static int makeDirectory(
2476   const char *zFile
2477 ){
2478   char *zCopy = sqlite3_mprintf("%s", zFile);
2479   int rc = SQLITE_OK;
2480
2481   if( zCopy==0 ){
2482     rc = SQLITE_NOMEM;
2483   }else{
2484     int nCopy = (int)strlen(zCopy);
2485     int i = 1;
2486
2487     while( rc==SQLITE_OK ){
2488       struct stat sStat;
2489       int rc2;
2490
2491       for(; zCopy[i]!='/' && i<nCopy; i++);
2492       if( i==nCopy ) break;
2493       zCopy[i] = '\0';
2494
2495       rc2 = fileStat(zCopy, &sStat);
2496       if( rc2!=0 ){
2497         if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2498       }else{
2499         if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2500       }
2501       zCopy[i] = '/';
2502       i++;
2503     }
2504
2505     sqlite3_free(zCopy);
2506   }
2507
2508   return rc;
2509 }
2510
2511 /*
2512 ** This function does the work for the writefile() UDF. Refer to 
2513 ** header comments at the top of this file for details.
2514 */
2515 static int writeFile(
2516   sqlite3_context *pCtx,          /* Context to return bytes written in */
2517   const char *zFile,              /* File to write */
2518   sqlite3_value *pData,           /* Data to write */
2519   mode_t mode,                    /* MODE parameter passed to writefile() */
2520   sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2521 ){
2522 #if !defined(_WIN32) && !defined(WIN32)
2523   if( S_ISLNK(mode) ){
2524     const char *zTo = (const char*)sqlite3_value_text(pData);
2525     if( symlink(zTo, zFile)<0 ) return 1;
2526   }else
2527 #endif
2528   {
2529     if( S_ISDIR(mode) ){
2530       if( mkdir(zFile, mode) ){
2531         /* The mkdir() call to create the directory failed. This might not
2532         ** be an error though - if there is already a directory at the same
2533         ** path and either the permissions already match or can be changed
2534         ** to do so using chmod(), it is not an error.  */
2535         struct stat sStat;
2536         if( errno!=EEXIST
2537          || 0!=fileStat(zFile, &sStat)
2538          || !S_ISDIR(sStat.st_mode)
2539          || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2540         ){
2541           return 1;
2542         }
2543       }
2544     }else{
2545       sqlite3_int64 nWrite = 0;
2546       const char *z;
2547       int rc = 0;
2548       FILE *out = fopen(zFile, "wb");
2549       if( out==0 ) return 1;
2550       z = (const char*)sqlite3_value_blob(pData);
2551       if( z ){
2552         sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2553         nWrite = sqlite3_value_bytes(pData);
2554         if( nWrite!=n ){
2555           rc = 1;
2556         }
2557       }
2558       fclose(out);
2559       if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2560         rc = 1;
2561       }
2562       if( rc ) return 2;
2563       sqlite3_result_int64(pCtx, nWrite);
2564     }
2565   }
2566
2567   if( mtime>=0 ){
2568 #if defined(_WIN32)
2569 #if !SQLITE_OS_WINRT
2570     /* Windows */
2571     FILETIME lastAccess;
2572     FILETIME lastWrite;
2573     SYSTEMTIME currentTime;
2574     LONGLONG intervals;
2575     HANDLE hFile;
2576     LPWSTR zUnicodeName;
2577     extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2578
2579     GetSystemTime(&currentTime);
2580     SystemTimeToFileTime(&currentTime, &lastAccess);
2581     intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2582     lastWrite.dwLowDateTime = (DWORD)intervals;
2583     lastWrite.dwHighDateTime = intervals >> 32;
2584     zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2585     if( zUnicodeName==0 ){
2586       return 1;
2587     }
2588     hFile = CreateFileW(
2589       zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2590       FILE_FLAG_BACKUP_SEMANTICS, NULL
2591     );
2592     sqlite3_free(zUnicodeName);
2593     if( hFile!=INVALID_HANDLE_VALUE ){
2594       BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2595       CloseHandle(hFile);
2596       return !bResult;
2597     }else{
2598       return 1;
2599     }
2600 #endif
2601 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2602     /* Recent unix */
2603     struct timespec times[2];
2604     times[0].tv_nsec = times[1].tv_nsec = 0;
2605     times[0].tv_sec = time(0);
2606     times[1].tv_sec = mtime;
2607     if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2608       return 1;
2609     }
2610 #else
2611     /* Legacy unix */
2612     struct timeval times[2];
2613     times[0].tv_usec = times[1].tv_usec = 0;
2614     times[0].tv_sec = time(0);
2615     times[1].tv_sec = mtime;
2616     if( utimes(zFile, times) ){
2617       return 1;
2618     }
2619 #endif
2620   }
2621
2622   return 0;
2623 }
2624
2625 /*
2626 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.  
2627 ** Refer to header comments at the top of this file for details.
2628 */
2629 static void writefileFunc(
2630   sqlite3_context *context,
2631   int argc,
2632   sqlite3_value **argv
2633 ){
2634   const char *zFile;
2635   mode_t mode = 0;
2636   int res;
2637   sqlite3_int64 mtime = -1;
2638
2639   if( argc<2 || argc>4 ){
2640     sqlite3_result_error(context, 
2641         "wrong number of arguments to function writefile()", -1
2642     );
2643     return;
2644   }
2645
2646   zFile = (const char*)sqlite3_value_text(argv[0]);
2647   if( zFile==0 ) return;
2648   if( argc>=3 ){
2649     mode = (mode_t)sqlite3_value_int(argv[2]);
2650   }
2651   if( argc==4 ){
2652     mtime = sqlite3_value_int64(argv[3]);
2653   }
2654
2655   res = writeFile(context, zFile, argv[1], mode, mtime);
2656   if( res==1 && errno==ENOENT ){
2657     if( makeDirectory(zFile)==SQLITE_OK ){
2658       res = writeFile(context, zFile, argv[1], mode, mtime);
2659     }
2660   }
2661
2662   if( argc>2 && res!=0 ){
2663     if( S_ISLNK(mode) ){
2664       ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2665     }else if( S_ISDIR(mode) ){
2666       ctxErrorMsg(context, "failed to create directory: %s", zFile);
2667     }else{
2668       ctxErrorMsg(context, "failed to write file: %s", zFile);
2669     }
2670   }
2671 }
2672
2673 /*
2674 ** SQL function:   lsmode(MODE)
2675 **
2676 ** Given a numberic st_mode from stat(), convert it into a human-readable
2677 ** text string in the style of "ls -l".
2678 */
2679 static void lsModeFunc(
2680   sqlite3_context *context,
2681   int argc,
2682   sqlite3_value **argv
2683 ){
2684   int i;
2685   int iMode = sqlite3_value_int(argv[0]);
2686   char z[16];
2687   (void)argc;
2688   if( S_ISLNK(iMode) ){
2689     z[0] = 'l';
2690   }else if( S_ISREG(iMode) ){
2691     z[0] = '-';
2692   }else if( S_ISDIR(iMode) ){
2693     z[0] = 'd';
2694   }else{
2695     z[0] = '?';
2696   }
2697   for(i=0; i<3; i++){
2698     int m = (iMode >> ((2-i)*3));
2699     char *a = &z[1 + i*3];
2700     a[0] = (m & 0x4) ? 'r' : '-';
2701     a[1] = (m & 0x2) ? 'w' : '-';
2702     a[2] = (m & 0x1) ? 'x' : '-';
2703   }
2704   z[10] = '\0';
2705   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2706 }
2707
2708 #ifndef SQLITE_OMIT_VIRTUALTABLE
2709
2710 /* 
2711 ** Cursor type for recursively iterating through a directory structure.
2712 */
2713 typedef struct fsdir_cursor fsdir_cursor;
2714 typedef struct FsdirLevel FsdirLevel;
2715
2716 struct FsdirLevel {
2717   DIR *pDir;                 /* From opendir() */
2718   char *zDir;                /* Name of directory (nul-terminated) */
2719 };
2720
2721 struct fsdir_cursor {
2722   sqlite3_vtab_cursor base;  /* Base class - must be first */
2723
2724   int nLvl;                  /* Number of entries in aLvl[] array */
2725   int iLvl;                  /* Index of current entry */
2726   FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2727
2728   const char *zBase;
2729   int nBase;
2730
2731   struct stat sStat;         /* Current lstat() results */
2732   char *zPath;               /* Path to current entry */
2733   sqlite3_int64 iRowid;      /* Current rowid */
2734 };
2735
2736 typedef struct fsdir_tab fsdir_tab;
2737 struct fsdir_tab {
2738   sqlite3_vtab base;         /* Base class - must be first */
2739 };
2740
2741 /*
2742 ** Construct a new fsdir virtual table object.
2743 */
2744 static int fsdirConnect(
2745   sqlite3 *db,
2746   void *pAux,
2747   int argc, const char *const*argv,
2748   sqlite3_vtab **ppVtab,
2749   char **pzErr
2750 ){
2751   fsdir_tab *pNew = 0;
2752   int rc;
2753   (void)pAux;
2754   (void)argc;
2755   (void)argv;
2756   (void)pzErr;
2757   rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2758   if( rc==SQLITE_OK ){
2759     pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2760     if( pNew==0 ) return SQLITE_NOMEM;
2761     memset(pNew, 0, sizeof(*pNew));
2762     sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2763   }
2764   *ppVtab = (sqlite3_vtab*)pNew;
2765   return rc;
2766 }
2767
2768 /*
2769 ** This method is the destructor for fsdir vtab objects.
2770 */
2771 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2772   sqlite3_free(pVtab);
2773   return SQLITE_OK;
2774 }
2775
2776 /*
2777 ** Constructor for a new fsdir_cursor object.
2778 */
2779 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2780   fsdir_cursor *pCur;
2781   (void)p;
2782   pCur = sqlite3_malloc( sizeof(*pCur) );
2783   if( pCur==0 ) return SQLITE_NOMEM;
2784   memset(pCur, 0, sizeof(*pCur));
2785   pCur->iLvl = -1;
2786   *ppCursor = &pCur->base;
2787   return SQLITE_OK;
2788 }
2789
2790 /*
2791 ** Reset a cursor back to the state it was in when first returned
2792 ** by fsdirOpen().
2793 */
2794 static void fsdirResetCursor(fsdir_cursor *pCur){
2795   int i;
2796   for(i=0; i<=pCur->iLvl; i++){
2797     FsdirLevel *pLvl = &pCur->aLvl[i];
2798     if( pLvl->pDir ) closedir(pLvl->pDir);
2799     sqlite3_free(pLvl->zDir);
2800   }
2801   sqlite3_free(pCur->zPath);
2802   sqlite3_free(pCur->aLvl);
2803   pCur->aLvl = 0;
2804   pCur->zPath = 0;
2805   pCur->zBase = 0;
2806   pCur->nBase = 0;
2807   pCur->nLvl = 0;
2808   pCur->iLvl = -1;
2809   pCur->iRowid = 1;
2810 }
2811
2812 /*
2813 ** Destructor for an fsdir_cursor.
2814 */
2815 static int fsdirClose(sqlite3_vtab_cursor *cur){
2816   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2817
2818   fsdirResetCursor(pCur);
2819   sqlite3_free(pCur);
2820   return SQLITE_OK;
2821 }
2822
2823 /*
2824 ** Set the error message for the virtual table associated with cursor
2825 ** pCur to the results of vprintf(zFmt, ...).
2826 */
2827 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2828   va_list ap;
2829   va_start(ap, zFmt);
2830   pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2831   va_end(ap);
2832 }
2833
2834
2835 /*
2836 ** Advance an fsdir_cursor to its next row of output.
2837 */
2838 static int fsdirNext(sqlite3_vtab_cursor *cur){
2839   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2840   mode_t m = pCur->sStat.st_mode;
2841
2842   pCur->iRowid++;
2843   if( S_ISDIR(m) ){
2844     /* Descend into this directory */
2845     int iNew = pCur->iLvl + 1;
2846     FsdirLevel *pLvl;
2847     if( iNew>=pCur->nLvl ){
2848       int nNew = iNew+1;
2849       sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2850       FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2851       if( aNew==0 ) return SQLITE_NOMEM;
2852       memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2853       pCur->aLvl = aNew;
2854       pCur->nLvl = nNew;
2855     }
2856     pCur->iLvl = iNew;
2857     pLvl = &pCur->aLvl[iNew];
2858     
2859     pLvl->zDir = pCur->zPath;
2860     pCur->zPath = 0;
2861     pLvl->pDir = opendir(pLvl->zDir);
2862     if( pLvl->pDir==0 ){
2863       fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2864       return SQLITE_ERROR;
2865     }
2866   }
2867
2868   while( pCur->iLvl>=0 ){
2869     FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2870     struct dirent *pEntry = readdir(pLvl->pDir);
2871     if( pEntry ){
2872       if( pEntry->d_name[0]=='.' ){
2873        if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2874        if( pEntry->d_name[1]=='\0' ) continue;
2875       }
2876       sqlite3_free(pCur->zPath);
2877       pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2878       if( pCur->zPath==0 ) return SQLITE_NOMEM;
2879       if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2880         fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2881         return SQLITE_ERROR;
2882       }
2883       return SQLITE_OK;
2884     }
2885     closedir(pLvl->pDir);
2886     sqlite3_free(pLvl->zDir);
2887     pLvl->pDir = 0;
2888     pLvl->zDir = 0;
2889     pCur->iLvl--;
2890   }
2891
2892   /* EOF */
2893   sqlite3_free(pCur->zPath);
2894   pCur->zPath = 0;
2895   return SQLITE_OK;
2896 }
2897
2898 /*
2899 ** Return values of columns for the row at which the series_cursor
2900 ** is currently pointing.
2901 */
2902 static int fsdirColumn(
2903   sqlite3_vtab_cursor *cur,   /* The cursor */
2904   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2905   int i                       /* Which column to return */
2906 ){
2907   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2908   switch( i ){
2909     case FSDIR_COLUMN_NAME: {
2910       sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2911       break;
2912     }
2913
2914     case FSDIR_COLUMN_MODE:
2915       sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2916       break;
2917
2918     case FSDIR_COLUMN_MTIME:
2919       sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2920       break;
2921
2922     case FSDIR_COLUMN_DATA: {
2923       mode_t m = pCur->sStat.st_mode;
2924       if( S_ISDIR(m) ){
2925         sqlite3_result_null(ctx);
2926 #if !defined(_WIN32) && !defined(WIN32)
2927       }else if( S_ISLNK(m) ){
2928         char aStatic[64];
2929         char *aBuf = aStatic;
2930         sqlite3_int64 nBuf = 64;
2931         int n;
2932
2933         while( 1 ){
2934           n = readlink(pCur->zPath, aBuf, nBuf);
2935           if( n<nBuf ) break;
2936           if( aBuf!=aStatic ) sqlite3_free(aBuf);
2937           nBuf = nBuf*2;
2938           aBuf = sqlite3_malloc64(nBuf);
2939           if( aBuf==0 ){
2940             sqlite3_result_error_nomem(ctx);
2941             return SQLITE_NOMEM;
2942           }
2943         }
2944
2945         sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2946         if( aBuf!=aStatic ) sqlite3_free(aBuf);
2947 #endif
2948       }else{
2949         readFileContents(ctx, pCur->zPath);
2950       }
2951     }
2952     case FSDIR_COLUMN_PATH:
2953     default: {
2954       /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2955       ** always return their values as NULL */
2956       break;
2957     }
2958   }
2959   return SQLITE_OK;
2960 }
2961
2962 /*
2963 ** Return the rowid for the current row. In this implementation, the
2964 ** first row returned is assigned rowid value 1, and each subsequent
2965 ** row a value 1 more than that of the previous.
2966 */
2967 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2968   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2969   *pRowid = pCur->iRowid;
2970   return SQLITE_OK;
2971 }
2972
2973 /*
2974 ** Return TRUE if the cursor has been moved off of the last
2975 ** row of output.
2976 */
2977 static int fsdirEof(sqlite3_vtab_cursor *cur){
2978   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2979   return (pCur->zPath==0);
2980 }
2981
2982 /*
2983 ** xFilter callback.
2984 **
2985 ** idxNum==1   PATH parameter only
2986 ** idxNum==2   Both PATH and DIR supplied
2987 */
2988 static int fsdirFilter(
2989   sqlite3_vtab_cursor *cur, 
2990   int idxNum, const char *idxStr,
2991   int argc, sqlite3_value **argv
2992 ){
2993   const char *zDir = 0;
2994   fsdir_cursor *pCur = (fsdir_cursor*)cur;
2995   (void)idxStr;
2996   fsdirResetCursor(pCur);
2997
2998   if( idxNum==0 ){
2999     fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
3000     return SQLITE_ERROR;
3001   }
3002
3003   assert( argc==idxNum && (argc==1 || argc==2) );
3004   zDir = (const char*)sqlite3_value_text(argv[0]);
3005   if( zDir==0 ){
3006     fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
3007     return SQLITE_ERROR;
3008   }
3009   if( argc==2 ){
3010     pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
3011   }
3012   if( pCur->zBase ){
3013     pCur->nBase = (int)strlen(pCur->zBase)+1;
3014     pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
3015   }else{
3016     pCur->zPath = sqlite3_mprintf("%s", zDir);
3017   }
3018
3019   if( pCur->zPath==0 ){
3020     return SQLITE_NOMEM;
3021   }
3022   if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
3023     fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
3024     return SQLITE_ERROR;
3025   }
3026
3027   return SQLITE_OK;
3028 }
3029
3030 /*
3031 ** SQLite will invoke this method one or more times while planning a query
3032 ** that uses the generate_series virtual table.  This routine needs to create
3033 ** a query plan for each invocation and compute an estimated cost for that
3034 ** plan.
3035 **
3036 ** In this implementation idxNum is used to represent the
3037 ** query plan.  idxStr is unused.
3038 **
3039 ** The query plan is represented by values of idxNum:
3040 **
3041 **  (1)  The path value is supplied by argv[0]
3042 **  (2)  Path is in argv[0] and dir is in argv[1]
3043 */
3044 static int fsdirBestIndex(
3045   sqlite3_vtab *tab,
3046   sqlite3_index_info *pIdxInfo
3047 ){
3048   int i;                 /* Loop over constraints */
3049   int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
3050   int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
3051   int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
3052   int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
3053   const struct sqlite3_index_constraint *pConstraint;
3054
3055   (void)tab;
3056   pConstraint = pIdxInfo->aConstraint;
3057   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3058     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3059     switch( pConstraint->iColumn ){
3060       case FSDIR_COLUMN_PATH: {
3061         if( pConstraint->usable ){
3062           idxPath = i;
3063           seenPath = 0;
3064         }else if( idxPath<0 ){
3065           seenPath = 1;
3066         }
3067         break;
3068       }
3069       case FSDIR_COLUMN_DIR: {
3070         if( pConstraint->usable ){
3071           idxDir = i;
3072           seenDir = 0;
3073         }else if( idxDir<0 ){
3074           seenDir = 1;
3075         }
3076         break;
3077       }
3078     } 
3079   }
3080   if( seenPath || seenDir ){
3081     /* If input parameters are unusable, disallow this plan */
3082     return SQLITE_CONSTRAINT;
3083   }
3084
3085   if( idxPath<0 ){
3086     pIdxInfo->idxNum = 0;
3087     /* The pIdxInfo->estimatedCost should have been initialized to a huge
3088     ** number.  Leave it unchanged. */
3089     pIdxInfo->estimatedRows = 0x7fffffff;
3090   }else{
3091     pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3092     pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3093     if( idxDir>=0 ){
3094       pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3095       pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3096       pIdxInfo->idxNum = 2;
3097       pIdxInfo->estimatedCost = 10.0;
3098     }else{
3099       pIdxInfo->idxNum = 1;
3100       pIdxInfo->estimatedCost = 100.0;
3101     }
3102   }
3103
3104   return SQLITE_OK;
3105 }
3106
3107 /*
3108 ** Register the "fsdir" virtual table.
3109 */
3110 static int fsdirRegister(sqlite3 *db){
3111   static sqlite3_module fsdirModule = {
3112     0,                         /* iVersion */
3113     0,                         /* xCreate */
3114     fsdirConnect,              /* xConnect */
3115     fsdirBestIndex,            /* xBestIndex */
3116     fsdirDisconnect,           /* xDisconnect */
3117     0,                         /* xDestroy */
3118     fsdirOpen,                 /* xOpen - open a cursor */
3119     fsdirClose,                /* xClose - close a cursor */
3120     fsdirFilter,               /* xFilter - configure scan constraints */
3121     fsdirNext,                 /* xNext - advance a cursor */
3122     fsdirEof,                  /* xEof - check for end of scan */
3123     fsdirColumn,               /* xColumn - read data */
3124     fsdirRowid,                /* xRowid - read data */
3125     0,                         /* xUpdate */
3126     0,                         /* xBegin */
3127     0,                         /* xSync */
3128     0,                         /* xCommit */
3129     0,                         /* xRollback */
3130     0,                         /* xFindMethod */
3131     0,                         /* xRename */
3132     0,                         /* xSavepoint */
3133     0,                         /* xRelease */
3134     0,                         /* xRollbackTo */
3135     0,                         /* xShadowName */
3136   };
3137
3138   int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3139   return rc;
3140 }
3141 #else         /* SQLITE_OMIT_VIRTUALTABLE */
3142 # define fsdirRegister(x) SQLITE_OK
3143 #endif
3144
3145 #ifdef _WIN32
3146
3147 #endif
3148 int sqlite3_fileio_init(
3149   sqlite3 *db, 
3150   char **pzErrMsg, 
3151   const sqlite3_api_routines *pApi
3152 ){
3153   int rc = SQLITE_OK;
3154   SQLITE_EXTENSION_INIT2(pApi);
3155   (void)pzErrMsg;  /* Unused parameter */
3156   rc = sqlite3_create_function(db, "readfile", 1, 
3157                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3158                                readfileFunc, 0, 0);
3159   if( rc==SQLITE_OK ){
3160     rc = sqlite3_create_function(db, "writefile", -1,
3161                                  SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3162                                  writefileFunc, 0, 0);
3163   }
3164   if( rc==SQLITE_OK ){
3165     rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3166                                  lsModeFunc, 0, 0);
3167   }
3168   if( rc==SQLITE_OK ){
3169     rc = fsdirRegister(db);
3170   }
3171   return rc;
3172 }
3173
3174 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
3175 /* To allow a standalone DLL, make test_windirent.c use the same
3176  * redefined SQLite API calls as the above extension code does.
3177  * Just pull in this .c to accomplish this. As a beneficial side
3178  * effect, this extension becomes a single translation unit. */
3179 #  include "test_windirent.c"
3180 #endif
3181
3182 /************************* End ../ext/misc/fileio.c ********************/
3183 /************************* Begin ../ext/misc/completion.c ******************/
3184 /*
3185 ** 2017-07-10
3186 **
3187 ** The author disclaims copyright to this source code.  In place of
3188 ** a legal notice, here is a blessing:
3189 **
3190 **    May you do good and not evil.
3191 **    May you find forgiveness for yourself and forgive others.
3192 **    May you share freely, never taking more than you give.
3193 **
3194 *************************************************************************
3195 **
3196 ** This file implements an eponymous virtual table that returns suggested
3197 ** completions for a partial SQL input.
3198 **
3199 ** Suggested usage:
3200 **
3201 **     SELECT DISTINCT candidate COLLATE nocase
3202 **       FROM completion($prefix,$wholeline)
3203 **      ORDER BY 1;
3204 **
3205 ** The two query parameters are optional.  $prefix is the text of the
3206 ** current word being typed and that is to be completed.  $wholeline is
3207 ** the complete input line, used for context.
3208 **
3209 ** The raw completion() table might return the same candidate multiple
3210 ** times, for example if the same column name is used to two or more
3211 ** tables.  And the candidates are returned in an arbitrary order.  Hence,
3212 ** the DISTINCT and ORDER BY are recommended.
3213 **
3214 ** This virtual table operates at the speed of human typing, and so there
3215 ** is no attempt to make it fast.  Even a slow implementation will be much
3216 ** faster than any human can type.
3217 **
3218 */
3219 /* #include "sqlite3ext.h" */
3220 SQLITE_EXTENSION_INIT1
3221 #include <assert.h>
3222 #include <string.h>
3223 #include <ctype.h>
3224
3225 #ifndef SQLITE_OMIT_VIRTUALTABLE
3226
3227 /* completion_vtab is a subclass of sqlite3_vtab which will
3228 ** serve as the underlying representation of a completion virtual table
3229 */
3230 typedef struct completion_vtab completion_vtab;
3231 struct completion_vtab {
3232   sqlite3_vtab base;  /* Base class - must be first */
3233   sqlite3 *db;        /* Database connection for this completion vtab */
3234 };
3235
3236 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3237 ** serve as the underlying representation of a cursor that scans
3238 ** over rows of the result
3239 */
3240 typedef struct completion_cursor completion_cursor;
3241 struct completion_cursor {
3242   sqlite3_vtab_cursor base;  /* Base class - must be first */
3243   sqlite3 *db;               /* Database connection for this cursor */
3244   int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3245   char *zPrefix;             /* The prefix for the word we want to complete */
3246   char *zLine;               /* The whole that we want to complete */
3247   const char *zCurrentRow;   /* Current output row */
3248   int szRow;                 /* Length of the zCurrentRow string */
3249   sqlite3_stmt *pStmt;       /* Current statement */
3250   sqlite3_int64 iRowid;      /* The rowid */
3251   int ePhase;                /* Current phase */
3252   int j;                     /* inter-phase counter */
3253 };
3254
3255 /* Values for ePhase:
3256 */
3257 #define COMPLETION_FIRST_PHASE   1
3258 #define COMPLETION_KEYWORDS      1
3259 #define COMPLETION_PRAGMAS       2
3260 #define COMPLETION_FUNCTIONS     3
3261 #define COMPLETION_COLLATIONS    4
3262 #define COMPLETION_INDEXES       5
3263 #define COMPLETION_TRIGGERS      6
3264 #define COMPLETION_DATABASES     7
3265 #define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3266 #define COMPLETION_COLUMNS       9
3267 #define COMPLETION_MODULES       10
3268 #define COMPLETION_EOF           11
3269
3270 /*
3271 ** The completionConnect() method is invoked to create a new
3272 ** completion_vtab that describes the completion virtual table.
3273 **
3274 ** Think of this routine as the constructor for completion_vtab objects.
3275 **
3276 ** All this routine needs to do is:
3277 **
3278 **    (1) Allocate the completion_vtab object and initialize all fields.
3279 **
3280 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3281 **        result set of queries against completion will look like.
3282 */
3283 static int completionConnect(
3284   sqlite3 *db,
3285   void *pAux,
3286   int argc, const char *const*argv,
3287   sqlite3_vtab **ppVtab,
3288   char **pzErr
3289 ){
3290   completion_vtab *pNew;
3291   int rc;
3292
3293   (void)(pAux);    /* Unused parameter */
3294   (void)(argc);    /* Unused parameter */
3295   (void)(argv);    /* Unused parameter */
3296   (void)(pzErr);   /* Unused parameter */
3297
3298 /* Column numbers */
3299 #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3300 #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3301 #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3302 #define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3303
3304   sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3305   rc = sqlite3_declare_vtab(db,
3306       "CREATE TABLE x("
3307       "  candidate TEXT,"
3308       "  prefix TEXT HIDDEN,"
3309       "  wholeline TEXT HIDDEN,"
3310       "  phase INT HIDDEN"        /* Used for debugging only */
3311       ")");
3312   if( rc==SQLITE_OK ){
3313     pNew = sqlite3_malloc( sizeof(*pNew) );
3314     *ppVtab = (sqlite3_vtab*)pNew;
3315     if( pNew==0 ) return SQLITE_NOMEM;
3316     memset(pNew, 0, sizeof(*pNew));
3317     pNew->db = db;
3318   }
3319   return rc;
3320 }
3321
3322 /*
3323 ** This method is the destructor for completion_cursor objects.
3324 */
3325 static int completionDisconnect(sqlite3_vtab *pVtab){
3326   sqlite3_free(pVtab);
3327   return SQLITE_OK;
3328 }
3329
3330 /*
3331 ** Constructor for a new completion_cursor object.
3332 */
3333 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3334   completion_cursor *pCur;
3335   pCur = sqlite3_malloc( sizeof(*pCur) );
3336   if( pCur==0 ) return SQLITE_NOMEM;
3337   memset(pCur, 0, sizeof(*pCur));
3338   pCur->db = ((completion_vtab*)p)->db;
3339   *ppCursor = &pCur->base;
3340   return SQLITE_OK;
3341 }
3342
3343 /*
3344 ** Reset the completion_cursor.
3345 */
3346 static void completionCursorReset(completion_cursor *pCur){
3347   sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3348   sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3349   sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3350   pCur->j = 0;
3351 }
3352
3353 /*
3354 ** Destructor for a completion_cursor.
3355 */
3356 static int completionClose(sqlite3_vtab_cursor *cur){
3357   completionCursorReset((completion_cursor*)cur);
3358   sqlite3_free(cur);
3359   return SQLITE_OK;
3360 }
3361
3362 /*
3363 ** Advance a completion_cursor to its next row of output.
3364 **
3365 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3366 ** record the current state of the scan.  This routine sets ->zCurrentRow
3367 ** to the current row of output and then returns.  If no more rows remain,
3368 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3369 ** table that has reached the end of its scan.
3370 **
3371 ** The current implementation just lists potential identifiers and
3372 ** keywords and filters them by zPrefix.  Future enhancements should
3373 ** take zLine into account to try to restrict the set of identifiers and
3374 ** keywords based on what would be legal at the current point of input.
3375 */
3376 static int completionNext(sqlite3_vtab_cursor *cur){
3377   completion_cursor *pCur = (completion_cursor*)cur;
3378   int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3379   int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3380   pCur->iRowid++;
3381   while( pCur->ePhase!=COMPLETION_EOF ){
3382     switch( pCur->ePhase ){
3383       case COMPLETION_KEYWORDS: {
3384         if( pCur->j >= sqlite3_keyword_count() ){
3385           pCur->zCurrentRow = 0;
3386           pCur->ePhase = COMPLETION_DATABASES;
3387         }else{
3388           sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3389         }
3390         iCol = -1;
3391         break;
3392       }
3393       case COMPLETION_DATABASES: {
3394         if( pCur->pStmt==0 ){
3395           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3396                              &pCur->pStmt, 0);
3397         }
3398         iCol = 1;
3399         eNextPhase = COMPLETION_TABLES;
3400         break;
3401       }
3402       case COMPLETION_TABLES: {
3403         if( pCur->pStmt==0 ){
3404           sqlite3_stmt *pS2;
3405           char *zSql = 0;
3406           const char *zSep = "";
3407           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3408           while( sqlite3_step(pS2)==SQLITE_ROW ){
3409             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3410             zSql = sqlite3_mprintf(
3411                "%z%s"
3412                "SELECT name FROM \"%w\".sqlite_schema",
3413                zSql, zSep, zDb
3414             );
3415             if( zSql==0 ) return SQLITE_NOMEM;
3416             zSep = " UNION ";
3417           }
3418           sqlite3_finalize(pS2);
3419           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3420           sqlite3_free(zSql);
3421         }
3422         iCol = 0;
3423         eNextPhase = COMPLETION_COLUMNS;
3424         break;
3425       }
3426       case COMPLETION_COLUMNS: {
3427         if( pCur->pStmt==0 ){
3428           sqlite3_stmt *pS2;
3429           char *zSql = 0;
3430           const char *zSep = "";
3431           sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3432           while( sqlite3_step(pS2)==SQLITE_ROW ){
3433             const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3434             zSql = sqlite3_mprintf(
3435                "%z%s"
3436                "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3437                        " JOIN pragma_table_info(sm.name,%Q) AS pti"
3438                " WHERE sm.type='table'",
3439                zSql, zSep, zDb, zDb
3440             );
3441             if( zSql==0 ) return SQLITE_NOMEM;
3442             zSep = " UNION ";
3443           }
3444           sqlite3_finalize(pS2);
3445           sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3446           sqlite3_free(zSql);
3447         }
3448         iCol = 0;
3449         eNextPhase = COMPLETION_EOF;
3450         break;
3451       }
3452     }
3453     if( iCol<0 ){
3454       /* This case is when the phase presets zCurrentRow */
3455       if( pCur->zCurrentRow==0 ) continue;
3456     }else{
3457       if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3458         /* Extract the next row of content */
3459         pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3460         pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3461       }else{
3462         /* When all rows are finished, advance to the next phase */
3463         sqlite3_finalize(pCur->pStmt);
3464         pCur->pStmt = 0;
3465         pCur->ePhase = eNextPhase;
3466         continue;
3467       }
3468     }
3469     if( pCur->nPrefix==0 ) break;
3470     if( pCur->nPrefix<=pCur->szRow
3471      && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3472     ){
3473       break;
3474     }
3475   }
3476
3477   return SQLITE_OK;
3478 }
3479
3480 /*
3481 ** Return values of columns for the row at which the completion_cursor
3482 ** is currently pointing.
3483 */
3484 static int completionColumn(
3485   sqlite3_vtab_cursor *cur,   /* The cursor */
3486   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3487   int i                       /* Which column to return */
3488 ){
3489   completion_cursor *pCur = (completion_cursor*)cur;
3490   switch( i ){
3491     case COMPLETION_COLUMN_CANDIDATE: {
3492       sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3493       break;
3494     }
3495     case COMPLETION_COLUMN_PREFIX: {
3496       sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3497       break;
3498     }
3499     case COMPLETION_COLUMN_WHOLELINE: {
3500       sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3501       break;
3502     }
3503     case COMPLETION_COLUMN_PHASE: {
3504       sqlite3_result_int(ctx, pCur->ePhase);
3505       break;
3506     }
3507   }
3508   return SQLITE_OK;
3509 }
3510
3511 /*
3512 ** Return the rowid for the current row.  In this implementation, the
3513 ** rowid is the same as the output value.
3514 */
3515 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3516   completion_cursor *pCur = (completion_cursor*)cur;
3517   *pRowid = pCur->iRowid;
3518   return SQLITE_OK;
3519 }
3520
3521 /*
3522 ** Return TRUE if the cursor has been moved off of the last
3523 ** row of output.
3524 */
3525 static int completionEof(sqlite3_vtab_cursor *cur){
3526   completion_cursor *pCur = (completion_cursor*)cur;
3527   return pCur->ePhase >= COMPLETION_EOF;
3528 }
3529
3530 /*
3531 ** This method is called to "rewind" the completion_cursor object back
3532 ** to the first row of output.  This method is always called at least
3533 ** once prior to any call to completionColumn() or completionRowid() or 
3534 ** completionEof().
3535 */
3536 static int completionFilter(
3537   sqlite3_vtab_cursor *pVtabCursor, 
3538   int idxNum, const char *idxStr,
3539   int argc, sqlite3_value **argv
3540 ){
3541   completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3542   int iArg = 0;
3543   (void)(idxStr);   /* Unused parameter */
3544   (void)(argc);     /* Unused parameter */
3545   completionCursorReset(pCur);
3546   if( idxNum & 1 ){
3547     pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3548     if( pCur->nPrefix>0 ){
3549       pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3550       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3551     }
3552     iArg = 1;
3553   }
3554   if( idxNum & 2 ){
3555     pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3556     if( pCur->nLine>0 ){
3557       pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3558       if( pCur->zLine==0 ) return SQLITE_NOMEM;
3559     }
3560   }
3561   if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3562     int i = pCur->nLine;
3563     while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3564       i--;
3565     }
3566     pCur->nPrefix = pCur->nLine - i;
3567     if( pCur->nPrefix>0 ){
3568       pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3569       if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3570     }
3571   }
3572   pCur->iRowid = 0;
3573   pCur->ePhase = COMPLETION_FIRST_PHASE;
3574   return completionNext(pVtabCursor);
3575 }
3576
3577 /*
3578 ** SQLite will invoke this method one or more times while planning a query
3579 ** that uses the completion virtual table.  This routine needs to create
3580 ** a query plan for each invocation and compute an estimated cost for that
3581 ** plan.
3582 **
3583 ** There are two hidden parameters that act as arguments to the table-valued
3584 ** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3585 ** is available and bit 1 is set if "wholeline" is available.
3586 */
3587 static int completionBestIndex(
3588   sqlite3_vtab *tab,
3589   sqlite3_index_info *pIdxInfo
3590 ){
3591   int i;                 /* Loop over constraints */
3592   int idxNum = 0;        /* The query plan bitmask */
3593   int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3594   int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3595   int nArg = 0;          /* Number of arguments that completeFilter() expects */
3596   const struct sqlite3_index_constraint *pConstraint;
3597
3598   (void)(tab);    /* Unused parameter */
3599   pConstraint = pIdxInfo->aConstraint;
3600   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3601     if( pConstraint->usable==0 ) continue;
3602     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3603     switch( pConstraint->iColumn ){
3604       case COMPLETION_COLUMN_PREFIX:
3605         prefixIdx = i;
3606         idxNum |= 1;
3607         break;
3608       case COMPLETION_COLUMN_WHOLELINE:
3609         wholelineIdx = i;
3610         idxNum |= 2;
3611         break;
3612     }
3613   }
3614   if( prefixIdx>=0 ){
3615     pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3616     pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3617   }
3618   if( wholelineIdx>=0 ){
3619     pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3620     pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3621   }
3622   pIdxInfo->idxNum = idxNum;
3623   pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3624   pIdxInfo->estimatedRows = 500 - 100*nArg;
3625   return SQLITE_OK;
3626 }
3627
3628 /*
3629 ** This following structure defines all the methods for the 
3630 ** completion virtual table.
3631 */
3632 static sqlite3_module completionModule = {
3633   0,                         /* iVersion */
3634   0,                         /* xCreate */
3635   completionConnect,         /* xConnect */
3636   completionBestIndex,       /* xBestIndex */
3637   completionDisconnect,      /* xDisconnect */
3638   0,                         /* xDestroy */
3639   completionOpen,            /* xOpen - open a cursor */
3640   completionClose,           /* xClose - close a cursor */
3641   completionFilter,          /* xFilter - configure scan constraints */
3642   completionNext,            /* xNext - advance a cursor */
3643   completionEof,             /* xEof - check for end of scan */
3644   completionColumn,          /* xColumn - read data */
3645   completionRowid,           /* xRowid - read data */
3646   0,                         /* xUpdate */
3647   0,                         /* xBegin */
3648   0,                         /* xSync */
3649   0,                         /* xCommit */
3650   0,                         /* xRollback */
3651   0,                         /* xFindMethod */
3652   0,                         /* xRename */
3653   0,                         /* xSavepoint */
3654   0,                         /* xRelease */
3655   0,                         /* xRollbackTo */
3656   0                          /* xShadowName */
3657 };
3658
3659 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3660
3661 int sqlite3CompletionVtabInit(sqlite3 *db){
3662   int rc = SQLITE_OK;
3663 #ifndef SQLITE_OMIT_VIRTUALTABLE
3664   rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3665 #endif
3666   return rc;
3667 }
3668
3669 #ifdef _WIN32
3670
3671 #endif
3672 int sqlite3_completion_init(
3673   sqlite3 *db, 
3674   char **pzErrMsg, 
3675   const sqlite3_api_routines *pApi
3676 ){
3677   int rc = SQLITE_OK;
3678   SQLITE_EXTENSION_INIT2(pApi);
3679   (void)(pzErrMsg);  /* Unused parameter */
3680 #ifndef SQLITE_OMIT_VIRTUALTABLE
3681   rc = sqlite3CompletionVtabInit(db);
3682 #endif
3683   return rc;
3684 }
3685
3686 /************************* End ../ext/misc/completion.c ********************/
3687 /************************* Begin ../ext/misc/appendvfs.c ******************/
3688 /*
3689 ** 2017-10-20
3690 **
3691 ** The author disclaims copyright to this source code.  In place of
3692 ** a legal notice, here is a blessing:
3693 **
3694 **    May you do good and not evil.
3695 **    May you find forgiveness for yourself and forgive others.
3696 **    May you share freely, never taking more than you give.
3697 **
3698 ******************************************************************************
3699 **
3700 ** This file implements a VFS shim that allows an SQLite database to be
3701 ** appended onto the end of some other file, such as an executable.
3702 **
3703 ** A special record must appear at the end of the file that identifies the
3704 ** file as an appended database and provides the offset to the first page
3705 ** of the exposed content. (Or, it is the length of the content prefix.)
3706 ** For best performance page 1 should be located at a disk page boundary,
3707 ** though that is not required.
3708 **
3709 ** When opening a database using this VFS, the connection might treat
3710 ** the file as an ordinary SQLite database, or it might treat it as a
3711 ** database appended onto some other file.  The decision is made by
3712 ** applying the following rules in order:
3713 **
3714 **  (1)  An empty file is an ordinary database.
3715 **
3716 **  (2)  If the file ends with the appendvfs trailer string
3717 **       "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3718 **
3719 **  (3)  If the file begins with the standard SQLite prefix string
3720 **       "SQLite format 3", that file is an ordinary database.
3721 **
3722 **  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3723 **       set, then a new database is appended to the already existing file.
3724 **
3725 **  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3726 **
3727 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3728 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
3729 ** This VFS will not read or write past the 1GiB mark.  This restriction
3730 ** might be lifted in future versions.  For now, if you need a larger
3731 ** database, then keep it in a separate file.
3732 **
3733 ** If the file being opened is a plain database (not an appended one), then
3734 ** this shim is a pass-through into the default underlying VFS. (rule 3)
3735 **/
3736 /* #include "sqlite3ext.h" */
3737 SQLITE_EXTENSION_INIT1
3738 #include <string.h>
3739 #include <assert.h>
3740
3741 /* The append mark at the end of the database is:
3742 **
3743 **     Start-Of-SQLite3-NNNNNNNN
3744 **     123456789 123456789 12345
3745 **
3746 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3747 ** the offset to page 1, and also the length of the prefix content.
3748 */
3749 #define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3750 #define APND_MARK_PREFIX_SZ  17
3751 #define APND_MARK_FOS_SZ      8
3752 #define APND_MARK_SIZE       (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3753
3754 /*
3755 ** Maximum size of the combined prefix + database + append-mark.  This
3756 ** must be less than 0x40000000 to avoid locking issues on Windows.
3757 */
3758 #define APND_MAX_SIZE  (0x40000000)
3759
3760 /*
3761 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3762 */
3763 #ifndef APND_ROUNDUP
3764 #define APND_ROUNDUP 4096
3765 #endif
3766 #define APND_ALIGN_MASK         ((sqlite3_int64)(APND_ROUNDUP-1))
3767 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3768
3769 /*
3770 ** Forward declaration of objects used by this utility
3771 */
3772 typedef struct sqlite3_vfs ApndVfs;
3773 typedef struct ApndFile ApndFile;
3774
3775 /* Access to a lower-level VFS that (might) implement dynamic loading,
3776 ** access to randomness, etc.
3777 */
3778 #define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3779 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3780
3781 /* An open appendvfs file
3782 **
3783 ** An instance of this structure describes the appended database file.
3784 ** A separate sqlite3_file object is always appended. The appended
3785 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3786 ** the entire file, including the prefix, the database, and the
3787 ** append-mark.
3788 **
3789 ** The structure of an AppendVFS database is like this:
3790 **
3791 **   +-------------+---------+----------+-------------+
3792 **   | prefix-file | padding | database | append-mark |
3793 **   +-------------+---------+----------+-------------+
3794 **                           ^          ^
3795 **                           |          |
3796 **                         iPgOne      iMark
3797 **
3798 **
3799 ** "prefix file" -  file onto which the database has been appended.
3800 ** "padding"     -  zero or more bytes inserted so that "database"
3801 **                  starts on an APND_ROUNDUP boundary
3802 ** "database"    -  The SQLite database file
3803 ** "append-mark" -  The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3804 **                  the offset from the start of prefix-file to the start
3805 **                  of "database".
3806 **
3807 ** The size of the database is iMark - iPgOne.
3808 **
3809 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3810 ** of iPgOne stored as a big-ending 64-bit integer.
3811 **
3812 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3813 ** Or, iMark is -1 to indicate that it has not yet been written.
3814 */
3815 struct ApndFile {
3816   sqlite3_file base;        /* Subclass.  MUST BE FIRST! */
3817   sqlite3_int64 iPgOne;     /* Offset to the start of the database */
3818   sqlite3_int64 iMark;      /* Offset of the append mark.  -1 if unwritten */
3819   /* Always followed by another sqlite3_file that describes the whole file */
3820 };
3821
3822 /*
3823 ** Methods for ApndFile
3824 */
3825 static int apndClose(sqlite3_file*);
3826 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3827 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3828 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3829 static int apndSync(sqlite3_file*, int flags);
3830 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3831 static int apndLock(sqlite3_file*, int);
3832 static int apndUnlock(sqlite3_file*, int);
3833 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3834 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3835 static int apndSectorSize(sqlite3_file*);
3836 static int apndDeviceCharacteristics(sqlite3_file*);
3837 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3838 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3839 static void apndShmBarrier(sqlite3_file*);
3840 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3841 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3842 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3843
3844 /*
3845 ** Methods for ApndVfs
3846 */
3847 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3848 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3849 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3850 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3851 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3852 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3853 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3854 static void apndDlClose(sqlite3_vfs*, void*);
3855 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3856 static int apndSleep(sqlite3_vfs*, int microseconds);
3857 static int apndCurrentTime(sqlite3_vfs*, double*);
3858 static int apndGetLastError(sqlite3_vfs*, int, char *);
3859 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3860 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3861 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3862 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3863
3864 static sqlite3_vfs apnd_vfs = {
3865   3,                            /* iVersion (set when registered) */
3866   0,                            /* szOsFile (set when registered) */
3867   1024,                         /* mxPathname */
3868   0,                            /* pNext */
3869   "apndvfs",                    /* zName */
3870   0,                            /* pAppData (set when registered) */ 
3871   apndOpen,                     /* xOpen */
3872   apndDelete,                   /* xDelete */
3873   apndAccess,                   /* xAccess */
3874   apndFullPathname,             /* xFullPathname */
3875   apndDlOpen,                   /* xDlOpen */
3876   apndDlError,                  /* xDlError */
3877   apndDlSym,                    /* xDlSym */
3878   apndDlClose,                  /* xDlClose */
3879   apndRandomness,               /* xRandomness */
3880   apndSleep,                    /* xSleep */
3881   apndCurrentTime,              /* xCurrentTime */
3882   apndGetLastError,             /* xGetLastError */
3883   apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3884   apndSetSystemCall,            /* xSetSystemCall */
3885   apndGetSystemCall,            /* xGetSystemCall */
3886   apndNextSystemCall            /* xNextSystemCall */
3887 };
3888
3889 static const sqlite3_io_methods apnd_io_methods = {
3890   3,                              /* iVersion */
3891   apndClose,                      /* xClose */
3892   apndRead,                       /* xRead */
3893   apndWrite,                      /* xWrite */
3894   apndTruncate,                   /* xTruncate */
3895   apndSync,                       /* xSync */
3896   apndFileSize,                   /* xFileSize */
3897   apndLock,                       /* xLock */
3898   apndUnlock,                     /* xUnlock */
3899   apndCheckReservedLock,          /* xCheckReservedLock */
3900   apndFileControl,                /* xFileControl */
3901   apndSectorSize,                 /* xSectorSize */
3902   apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3903   apndShmMap,                     /* xShmMap */
3904   apndShmLock,                    /* xShmLock */
3905   apndShmBarrier,                 /* xShmBarrier */
3906   apndShmUnmap,                   /* xShmUnmap */
3907   apndFetch,                      /* xFetch */
3908   apndUnfetch                     /* xUnfetch */
3909 };
3910
3911 /*
3912 ** Close an apnd-file.
3913 */
3914 static int apndClose(sqlite3_file *pFile){
3915   pFile = ORIGFILE(pFile);
3916   return pFile->pMethods->xClose(pFile);
3917 }
3918
3919 /*
3920 ** Read data from an apnd-file.
3921 */
3922 static int apndRead(
3923   sqlite3_file *pFile, 
3924   void *zBuf, 
3925   int iAmt, 
3926   sqlite_int64 iOfst
3927 ){
3928   ApndFile *paf = (ApndFile *)pFile;
3929   pFile = ORIGFILE(pFile);
3930   return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3931 }
3932
3933 /*
3934 ** Add the append-mark onto what should become the end of the file.
3935 *  If and only if this succeeds, internal ApndFile.iMark is updated.
3936 *  Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3937 */
3938 static int apndWriteMark(
3939   ApndFile *paf,
3940   sqlite3_file *pFile,
3941   sqlite_int64 iWriteEnd
3942 ){
3943   sqlite_int64 iPgOne = paf->iPgOne;
3944   unsigned char a[APND_MARK_SIZE];
3945   int i = APND_MARK_FOS_SZ;
3946   int rc;
3947   assert(pFile == ORIGFILE(paf));
3948   memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3949   while( --i >= 0 ){
3950     a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3951     iPgOne >>= 8;
3952   }
3953   iWriteEnd += paf->iPgOne;
3954   if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3955                   (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3956     paf->iMark = iWriteEnd;
3957   }
3958   return rc;
3959 }
3960
3961 /*
3962 ** Write data to an apnd-file.
3963 */
3964 static int apndWrite(
3965   sqlite3_file *pFile,
3966   const void *zBuf,
3967   int iAmt,
3968   sqlite_int64 iOfst
3969 ){
3970   ApndFile *paf = (ApndFile *)pFile;
3971   sqlite_int64 iWriteEnd = iOfst + iAmt;
3972   if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3973   pFile = ORIGFILE(pFile);
3974   /* If append-mark is absent or will be overwritten, write it. */
3975   if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3976     int rc = apndWriteMark(paf, pFile, iWriteEnd);
3977     if( SQLITE_OK!=rc ) return rc;
3978   }
3979   return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3980 }
3981
3982 /*
3983 ** Truncate an apnd-file.
3984 */
3985 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3986   ApndFile *paf = (ApndFile *)pFile;
3987   pFile = ORIGFILE(pFile);
3988   /* The append mark goes out first so truncate failure does not lose it. */
3989   if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3990   /* Truncate underlying file just past append mark */
3991   return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3992 }
3993
3994 /*
3995 ** Sync an apnd-file.
3996 */
3997 static int apndSync(sqlite3_file *pFile, int flags){
3998   pFile = ORIGFILE(pFile);
3999   return pFile->pMethods->xSync(pFile, flags);
4000 }
4001
4002 /*
4003 ** Return the current file-size of an apnd-file.
4004 ** If the append mark is not yet there, the file-size is 0.
4005 */
4006 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
4007   ApndFile *paf = (ApndFile *)pFile;
4008   *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
4009   return SQLITE_OK;
4010 }
4011
4012 /*
4013 ** Lock an apnd-file.
4014 */
4015 static int apndLock(sqlite3_file *pFile, int eLock){
4016   pFile = ORIGFILE(pFile);
4017   return pFile->pMethods->xLock(pFile, eLock);
4018 }
4019
4020 /*
4021 ** Unlock an apnd-file.
4022 */
4023 static int apndUnlock(sqlite3_file *pFile, int eLock){
4024   pFile = ORIGFILE(pFile);
4025   return pFile->pMethods->xUnlock(pFile, eLock);
4026 }
4027
4028 /*
4029 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
4030 */
4031 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
4032   pFile = ORIGFILE(pFile);
4033   return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
4034 }
4035
4036 /*
4037 ** File control method. For custom operations on an apnd-file.
4038 */
4039 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
4040   ApndFile *paf = (ApndFile *)pFile;
4041   int rc;
4042   pFile = ORIGFILE(pFile);
4043   if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
4044   rc = pFile->pMethods->xFileControl(pFile, op, pArg);
4045   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
4046     *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
4047   }
4048   return rc;
4049 }
4050
4051 /*
4052 ** Return the sector-size in bytes for an apnd-file.
4053 */
4054 static int apndSectorSize(sqlite3_file *pFile){
4055   pFile = ORIGFILE(pFile);
4056   return pFile->pMethods->xSectorSize(pFile);
4057 }
4058
4059 /*
4060 ** Return the device characteristic flags supported by an apnd-file.
4061 */
4062 static int apndDeviceCharacteristics(sqlite3_file *pFile){
4063   pFile = ORIGFILE(pFile);
4064   return pFile->pMethods->xDeviceCharacteristics(pFile);
4065 }
4066
4067 /* Create a shared memory file mapping */
4068 static int apndShmMap(
4069   sqlite3_file *pFile,
4070   int iPg,
4071   int pgsz,
4072   int bExtend,
4073   void volatile **pp
4074 ){
4075   pFile = ORIGFILE(pFile);
4076   return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4077 }
4078
4079 /* Perform locking on a shared-memory segment */
4080 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4081   pFile = ORIGFILE(pFile);
4082   return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4083 }
4084
4085 /* Memory barrier operation on shared memory */
4086 static void apndShmBarrier(sqlite3_file *pFile){
4087   pFile = ORIGFILE(pFile);
4088   pFile->pMethods->xShmBarrier(pFile);
4089 }
4090
4091 /* Unmap a shared memory segment */
4092 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4093   pFile = ORIGFILE(pFile);
4094   return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4095 }
4096
4097 /* Fetch a page of a memory-mapped file */
4098 static int apndFetch(
4099   sqlite3_file *pFile,
4100   sqlite3_int64 iOfst,
4101   int iAmt,
4102   void **pp
4103 ){
4104   ApndFile *p = (ApndFile *)pFile;
4105   if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4106     return SQLITE_IOERR; /* Cannot read what is not yet there. */
4107   }
4108   pFile = ORIGFILE(pFile);
4109   return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4110 }
4111
4112 /* Release a memory-mapped page */
4113 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4114   ApndFile *p = (ApndFile *)pFile;
4115   pFile = ORIGFILE(pFile);
4116   return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4117 }
4118
4119 /*
4120 ** Try to read the append-mark off the end of a file.  Return the
4121 ** start of the appended database if the append-mark is present.
4122 ** If there is no valid append-mark, return -1;
4123 **
4124 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4125 ** indicates that the appended database contains at least one page.  The
4126 ** start-of-database value must be a multiple of 512.
4127 */
4128 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4129   int rc, i;
4130   sqlite3_int64 iMark;
4131   int msbs = 8 * (APND_MARK_FOS_SZ-1);
4132   unsigned char a[APND_MARK_SIZE];
4133
4134   if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4135   rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4136   if( rc ) return -1;
4137   if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4138   iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4139   for(i=1; i<8; i++){
4140     msbs -= 8;
4141     iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4142   }
4143   if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4144   if( iMark & 0x1ff ) return -1;
4145   return iMark;
4146 }
4147
4148 static const char apvfsSqliteHdr[] = "SQLite format 3";
4149 /*
4150 ** Check to see if the file is an appendvfs SQLite database file.
4151 ** Return true iff it is such. Parameter sz is the file's size.
4152 */
4153 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4154   int rc;
4155   char zHdr[16];
4156   sqlite3_int64 iMark = apndReadMark(sz, pFile);
4157   if( iMark>=0 ){
4158     /* If file has the correct end-marker, the expected odd size, and the
4159     ** SQLite DB type marker where the end-marker puts it, then it
4160     ** is an appendvfs database.
4161     */
4162     rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4163     if( SQLITE_OK==rc
4164      && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4165      && (sz & 0x1ff) == APND_MARK_SIZE
4166      && sz>=512+APND_MARK_SIZE
4167     ){
4168       return 1; /* It's an appendvfs database */
4169     }
4170   }
4171   return 0;
4172 }
4173
4174 /*
4175 ** Check to see if the file is an ordinary SQLite database file.
4176 ** Return true iff so. Parameter sz is the file's size.
4177 */
4178 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4179   char zHdr[16];
4180   if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4181    || (sz & 0x1ff) != 0
4182    || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4183    || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4184   ){
4185     return 0;
4186   }else{
4187     return 1;
4188   }
4189 }
4190
4191 /*
4192 ** Open an apnd file handle.
4193 */
4194 static int apndOpen(
4195   sqlite3_vfs *pApndVfs,
4196   const char *zName,
4197   sqlite3_file *pFile,
4198   int flags,
4199   int *pOutFlags
4200 ){
4201   ApndFile *pApndFile = (ApndFile*)pFile;
4202   sqlite3_file *pBaseFile = ORIGFILE(pFile);
4203   sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4204   int rc;
4205   sqlite3_int64 sz = 0;
4206   if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4207     /* The appendvfs is not to be used for transient or temporary databases.
4208     ** Just use the base VFS open to initialize the given file object and
4209     ** open the underlying file. (Appendvfs is then unused for this file.)
4210     */
4211     return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4212   }
4213   memset(pApndFile, 0, sizeof(ApndFile));
4214   pFile->pMethods = &apnd_io_methods;
4215   pApndFile->iMark = -1;    /* Append mark not yet written */
4216
4217   rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4218   if( rc==SQLITE_OK ){
4219     rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4220     if( rc ){
4221       pBaseFile->pMethods->xClose(pBaseFile);
4222     }
4223   }
4224   if( rc ){
4225     pFile->pMethods = 0;
4226     return rc;
4227   }
4228   if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4229     /* The file being opened appears to be just an ordinary DB. Copy
4230     ** the base dispatch-table so this instance mimics the base VFS. 
4231     */
4232     memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4233     return SQLITE_OK;
4234   }
4235   pApndFile->iPgOne = apndReadMark(sz, pFile);
4236   if( pApndFile->iPgOne>=0 ){
4237     pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4238     return SQLITE_OK;
4239   }
4240   if( (flags & SQLITE_OPEN_CREATE)==0 ){
4241     pBaseFile->pMethods->xClose(pBaseFile);
4242     rc = SQLITE_CANTOPEN;
4243     pFile->pMethods = 0;
4244   }else{
4245     /* Round newly added appendvfs location to #define'd page boundary. 
4246     ** Note that nothing has yet been written to the underlying file.
4247     ** The append mark will be written along with first content write.
4248     ** Until then, paf->iMark value indicates it is not yet written.
4249     */
4250     pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4251   }
4252   return rc;
4253 }
4254
4255 /*
4256 ** Delete an apnd file.
4257 ** For an appendvfs, this could mean delete the appendvfs portion,
4258 ** leaving the appendee as it was before it gained an appendvfs.
4259 ** For now, this code deletes the underlying file too.
4260 */
4261 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4262   return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4263 }
4264
4265 /*
4266 ** All other VFS methods are pass-thrus.
4267 */
4268 static int apndAccess(
4269   sqlite3_vfs *pVfs, 
4270   const char *zPath, 
4271   int flags, 
4272   int *pResOut
4273 ){
4274   return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4275 }
4276 static int apndFullPathname(
4277   sqlite3_vfs *pVfs, 
4278   const char *zPath, 
4279   int nOut, 
4280   char *zOut
4281 ){
4282   return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4283 }
4284 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4285   return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4286 }
4287 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4288   ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4289 }
4290 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4291   return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4292 }
4293 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4294   ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4295 }
4296 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4297   return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4298 }
4299 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4300   return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4301 }
4302 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4303   return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4304 }
4305 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4306   return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4307 }
4308 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4309   return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4310 }
4311 static int apndSetSystemCall(
4312   sqlite3_vfs *pVfs,
4313   const char *zName,
4314   sqlite3_syscall_ptr pCall
4315 ){
4316   return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4317 }
4318 static sqlite3_syscall_ptr apndGetSystemCall(
4319   sqlite3_vfs *pVfs,
4320   const char *zName
4321 ){
4322   return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4323 }
4324 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4325   return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4326 }
4327
4328   
4329 #ifdef _WIN32
4330
4331 #endif
4332 /* 
4333 ** This routine is called when the extension is loaded.
4334 ** Register the new VFS.
4335 */
4336 int sqlite3_appendvfs_init(
4337   sqlite3 *db, 
4338   char **pzErrMsg, 
4339   const sqlite3_api_routines *pApi
4340 ){
4341   int rc = SQLITE_OK;
4342   sqlite3_vfs *pOrig;
4343   SQLITE_EXTENSION_INIT2(pApi);
4344   (void)pzErrMsg;
4345   (void)db;
4346   pOrig = sqlite3_vfs_find(0);
4347   if( pOrig==0 ) return SQLITE_ERROR;
4348   apnd_vfs.iVersion = pOrig->iVersion;
4349   apnd_vfs.pAppData = pOrig;
4350   apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4351   rc = sqlite3_vfs_register(&apnd_vfs, 0);
4352 #ifdef APPENDVFS_TEST
4353   if( rc==SQLITE_OK ){
4354     rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4355   }
4356 #endif
4357   if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4358   return rc;
4359 }
4360
4361 /************************* End ../ext/misc/appendvfs.c ********************/
4362 /************************* Begin ../ext/misc/memtrace.c ******************/
4363 /*
4364 ** 2019-01-21
4365 **
4366 ** The author disclaims copyright to this source code.  In place of
4367 ** a legal notice, here is a blessing:
4368 **
4369 **    May you do good and not evil.
4370 **    May you find forgiveness for yourself and forgive others.
4371 **    May you share freely, never taking more than you give.
4372 **
4373 *************************************************************************
4374 **
4375 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4376 ** mechanism to add a tracing layer on top of SQLite.  If this extension
4377 ** is registered prior to sqlite3_initialize(), it will cause all memory
4378 ** allocation activities to be logged on standard output, or to some other
4379 ** FILE specified by the initializer.
4380 **
4381 ** This file needs to be compiled into the application that uses it.
4382 **
4383 ** This extension is used to implement the --memtrace option of the
4384 ** command-line shell.
4385 */
4386 #include <assert.h>
4387 #include <string.h>
4388 #include <stdio.h>
4389
4390 /* The original memory allocation routines */
4391 static sqlite3_mem_methods memtraceBase;
4392 static FILE *memtraceOut;
4393
4394 /* Methods that trace memory allocations */
4395 static void *memtraceMalloc(int n){
4396   if( memtraceOut ){
4397     fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 
4398             memtraceBase.xRoundup(n));
4399   }
4400   return memtraceBase.xMalloc(n);
4401 }
4402 static void memtraceFree(void *p){
4403   if( p==0 ) return;
4404   if( memtraceOut ){
4405     fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4406   }
4407   memtraceBase.xFree(p);
4408 }
4409 static void *memtraceRealloc(void *p, int n){
4410   if( p==0 ) return memtraceMalloc(n);
4411   if( n==0 ){
4412     memtraceFree(p);
4413     return 0;
4414   }
4415   if( memtraceOut ){
4416     fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4417             memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4418   }
4419   return memtraceBase.xRealloc(p, n);
4420 }
4421 static int memtraceSize(void *p){
4422   return memtraceBase.xSize(p);
4423 }
4424 static int memtraceRoundup(int n){
4425   return memtraceBase.xRoundup(n);
4426 }
4427 static int memtraceInit(void *p){
4428   return memtraceBase.xInit(p);
4429 }
4430 static void memtraceShutdown(void *p){
4431   memtraceBase.xShutdown(p);
4432 }
4433
4434 /* The substitute memory allocator */
4435 static sqlite3_mem_methods ersaztMethods = {
4436   memtraceMalloc,
4437   memtraceFree,
4438   memtraceRealloc,
4439   memtraceSize,
4440   memtraceRoundup,
4441   memtraceInit,
4442   memtraceShutdown,
4443   0
4444 };
4445
4446 /* Begin tracing memory allocations to out. */
4447 int sqlite3MemTraceActivate(FILE *out){
4448   int rc = SQLITE_OK;
4449   if( memtraceBase.xMalloc==0 ){
4450     rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4451     if( rc==SQLITE_OK ){
4452       rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4453     }
4454   }
4455   memtraceOut = out;
4456   return rc;
4457 }
4458
4459 /* Deactivate memory tracing */
4460 int sqlite3MemTraceDeactivate(void){
4461   int rc = SQLITE_OK;
4462   if( memtraceBase.xMalloc!=0 ){
4463     rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4464     if( rc==SQLITE_OK ){
4465       memset(&memtraceBase, 0, sizeof(memtraceBase));
4466     }
4467   }
4468   memtraceOut = 0;
4469   return rc;
4470 }
4471
4472 /************************* End ../ext/misc/memtrace.c ********************/
4473 /************************* Begin ../ext/misc/uint.c ******************/
4474 /*
4475 ** 2020-04-14
4476 **
4477 ** The author disclaims copyright to this source code.  In place of
4478 ** a legal notice, here is a blessing:
4479 **
4480 **    May you do good and not evil.
4481 **    May you find forgiveness for yourself and forgive others.
4482 **    May you share freely, never taking more than you give.
4483 **
4484 ******************************************************************************
4485 **
4486 ** This SQLite extension implements the UINT collating sequence.
4487 **
4488 ** UINT works like BINARY for text, except that embedded strings
4489 ** of digits compare in numeric order.
4490 **
4491 **     *   Leading zeros are handled properly, in the sense that
4492 **         they do not mess of the maginitude comparison of embedded
4493 **         strings of digits.  "x00123y" is equal to "x123y".
4494 **
4495 **     *   Only unsigned integers are recognized.  Plus and minus
4496 **         signs are ignored.  Decimal points and exponential notation
4497 **         are ignored.
4498 **
4499 **     *   Embedded integers can be of arbitrary length.  Comparison
4500 **         is *not* limited integers that can be expressed as a
4501 **         64-bit machine integer.
4502 */
4503 /* #include "sqlite3ext.h" */
4504 SQLITE_EXTENSION_INIT1
4505 #include <assert.h>
4506 #include <string.h>
4507 #include <ctype.h>
4508
4509 /*
4510 ** Compare text in lexicographic order, except strings of digits
4511 ** compare in numeric order.
4512 */
4513 static int uintCollFunc(
4514   void *notUsed,
4515   int nKey1, const void *pKey1,
4516   int nKey2, const void *pKey2
4517 ){
4518   const unsigned char *zA = (const unsigned char*)pKey1;
4519   const unsigned char *zB = (const unsigned char*)pKey2;
4520   int i=0, j=0, x;
4521   (void)notUsed;
4522   while( i<nKey1 && j<nKey2 ){
4523     x = zA[i] - zB[j];
4524     if( isdigit(zA[i]) ){
4525       int k;
4526       if( !isdigit(zB[j]) ) return x;
4527       while( i<nKey1 && zA[i]=='0' ){ i++; }
4528       while( j<nKey2 && zB[j]=='0' ){ j++; }
4529       k = 0;
4530       while( i+k<nKey1 && isdigit(zA[i+k])
4531              && j+k<nKey2 && isdigit(zB[j+k]) ){
4532         k++;
4533       }
4534       if( i+k<nKey1 && isdigit(zA[i+k]) ){
4535         return +1;
4536       }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4537         return -1;
4538       }else{
4539         x = memcmp(zA+i, zB+j, k);
4540         if( x ) return x;
4541         i += k;
4542         j += k;
4543       }
4544     }else if( x ){
4545       return x;
4546     }else{
4547       i++;
4548       j++;
4549     }
4550   }
4551   return (nKey1 - i) - (nKey2 - j);
4552 }
4553
4554 #ifdef _WIN32
4555
4556 #endif
4557 int sqlite3_uint_init(
4558   sqlite3 *db, 
4559   char **pzErrMsg, 
4560   const sqlite3_api_routines *pApi
4561 ){
4562   SQLITE_EXTENSION_INIT2(pApi);
4563   (void)pzErrMsg;  /* Unused parameter */
4564   return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4565 }
4566
4567 /************************* End ../ext/misc/uint.c ********************/
4568 /************************* Begin ../ext/misc/decimal.c ******************/
4569 /*
4570 ** 2020-06-22
4571 **
4572 ** The author disclaims copyright to this source code.  In place of
4573 ** a legal notice, here is a blessing:
4574 **
4575 **    May you do good and not evil.
4576 **    May you find forgiveness for yourself and forgive others.
4577 **    May you share freely, never taking more than you give.
4578 **
4579 ******************************************************************************
4580 **
4581 ** Routines to implement arbitrary-precision decimal math.
4582 **
4583 ** The focus here is on simplicity and correctness, not performance.
4584 */
4585 /* #include "sqlite3ext.h" */
4586 SQLITE_EXTENSION_INIT1
4587 #include <assert.h>
4588 #include <string.h>
4589 #include <ctype.h>
4590 #include <stdlib.h>
4591
4592 /* Mark a function parameter as unused, to suppress nuisance compiler
4593 ** warnings. */
4594 #ifndef UNUSED_PARAMETER
4595 # define UNUSED_PARAMETER(X)  (void)(X)
4596 #endif
4597
4598
4599 /* A decimal object */
4600 typedef struct Decimal Decimal;
4601 struct Decimal {
4602   char sign;        /* 0 for positive, 1 for negative */
4603   char oom;         /* True if an OOM is encountered */
4604   char isNull;      /* True if holds a NULL rather than a number */
4605   char isInit;      /* True upon initialization */
4606   int nDigit;       /* Total number of digits */
4607   int nFrac;        /* Number of digits to the right of the decimal point */
4608   signed char *a;   /* Array of digits.  Most significant first. */
4609 };
4610
4611 /*
4612 ** Release memory held by a Decimal, but do not free the object itself.
4613 */
4614 static void decimal_clear(Decimal *p){
4615   sqlite3_free(p->a);
4616 }
4617
4618 /*
4619 ** Destroy a Decimal object
4620 */
4621 static void decimal_free(Decimal *p){
4622   if( p ){
4623     decimal_clear(p);
4624     sqlite3_free(p);
4625   }
4626 }
4627
4628 /*
4629 ** Allocate a new Decimal object.  Initialize it to the number given
4630 ** by the input string.
4631 */
4632 static Decimal *decimal_new(
4633   sqlite3_context *pCtx,
4634   sqlite3_value *pIn,
4635   int nAlt,
4636   const unsigned char *zAlt
4637 ){
4638   Decimal *p;
4639   int n, i;
4640   const unsigned char *zIn;
4641   int iExp = 0;
4642   p = sqlite3_malloc( sizeof(*p) );
4643   if( p==0 ) goto new_no_mem;
4644   p->sign = 0;
4645   p->oom = 0;
4646   p->isInit = 1;
4647   p->isNull = 0;
4648   p->nDigit = 0;
4649   p->nFrac = 0;
4650   if( zAlt ){
4651     n = nAlt,
4652     zIn = zAlt;
4653   }else{
4654     if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4655       p->a = 0;
4656       p->isNull = 1;
4657       return p;
4658     }
4659     n = sqlite3_value_bytes(pIn);
4660     zIn = sqlite3_value_text(pIn);
4661   }
4662   p->a = sqlite3_malloc64( n+1 );
4663   if( p->a==0 ) goto new_no_mem;
4664   for(i=0; isspace(zIn[i]); i++){}
4665   if( zIn[i]=='-' ){
4666     p->sign = 1;
4667     i++;
4668   }else if( zIn[i]=='+' ){
4669     i++;
4670   }
4671   while( i<n && zIn[i]=='0' ) i++;
4672   while( i<n ){
4673     char c = zIn[i];
4674     if( c>='0' && c<='9' ){
4675       p->a[p->nDigit++] = c - '0';
4676     }else if( c=='.' ){
4677       p->nFrac = p->nDigit + 1;
4678     }else if( c=='e' || c=='E' ){
4679       int j = i+1;
4680       int neg = 0;
4681       if( j>=n ) break;
4682       if( zIn[j]=='-' ){
4683         neg = 1;
4684         j++;
4685       }else if( zIn[j]=='+' ){
4686         j++;
4687       }
4688       while( j<n && iExp<1000000 ){
4689         if( zIn[j]>='0' && zIn[j]<='9' ){
4690           iExp = iExp*10 + zIn[j] - '0';
4691         }
4692         j++;
4693       }
4694       if( neg ) iExp = -iExp;
4695       break;
4696     }
4697     i++;
4698   }
4699   if( p->nFrac ){
4700     p->nFrac = p->nDigit - (p->nFrac - 1);
4701   }
4702   if( iExp>0 ){
4703     if( p->nFrac>0 ){
4704       if( iExp<=p->nFrac ){
4705         p->nFrac -= iExp;
4706         iExp = 0;
4707       }else{
4708         iExp -= p->nFrac;
4709         p->nFrac = 0;
4710       }
4711     }
4712     if( iExp>0 ){   
4713       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4714       if( p->a==0 ) goto new_no_mem;
4715       memset(p->a+p->nDigit, 0, iExp);
4716       p->nDigit += iExp;
4717     }
4718   }else if( iExp<0 ){
4719     int nExtra;
4720     iExp = -iExp;
4721     nExtra = p->nDigit - p->nFrac - 1;
4722     if( nExtra ){
4723       if( nExtra>=iExp ){
4724         p->nFrac += iExp;
4725         iExp  = 0;
4726       }else{
4727         iExp -= nExtra;
4728         p->nFrac = p->nDigit - 1;
4729       }
4730     }
4731     if( iExp>0 ){
4732       p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4733       if( p->a==0 ) goto new_no_mem;
4734       memmove(p->a+iExp, p->a, p->nDigit);
4735       memset(p->a, 0, iExp);
4736       p->nDigit += iExp;
4737       p->nFrac += iExp;
4738     }
4739   }
4740   return p;
4741
4742 new_no_mem:
4743   if( pCtx ) sqlite3_result_error_nomem(pCtx);
4744   sqlite3_free(p);
4745   return 0;
4746 }
4747
4748 /*
4749 ** Make the given Decimal the result.
4750 */
4751 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4752   char *z;
4753   int i, j;
4754   int n;
4755   if( p==0 || p->oom ){
4756     sqlite3_result_error_nomem(pCtx);
4757     return;
4758   }
4759   if( p->isNull ){
4760     sqlite3_result_null(pCtx);
4761     return;
4762   }
4763   z = sqlite3_malloc( p->nDigit+4 );
4764   if( z==0 ){
4765     sqlite3_result_error_nomem(pCtx);
4766     return;
4767   }
4768   i = 0;
4769   if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4770     p->sign = 0;
4771   }
4772   if( p->sign ){
4773     z[0] = '-';
4774     i = 1;
4775   }
4776   n = p->nDigit - p->nFrac;
4777   if( n<=0 ){
4778     z[i++] = '0';
4779   }
4780   j = 0;
4781   while( n>1 && p->a[j]==0 ){
4782     j++;
4783     n--;
4784   }
4785   while( n>0  ){
4786     z[i++] = p->a[j] + '0';
4787     j++;
4788     n--;
4789   }
4790   if( p->nFrac ){
4791     z[i++] = '.';
4792     do{
4793       z[i++] = p->a[j] + '0';
4794       j++;
4795     }while( j<p->nDigit );
4796   }
4797   z[i] = 0;
4798   sqlite3_result_text(pCtx, z, i, sqlite3_free);
4799 }
4800
4801 /*
4802 ** SQL Function:   decimal(X)
4803 **
4804 ** Convert input X into decimal and then back into text
4805 */
4806 static void decimalFunc(
4807   sqlite3_context *context,
4808   int argc,
4809   sqlite3_value **argv
4810 ){
4811   Decimal *p = decimal_new(context, argv[0], 0, 0);
4812   UNUSED_PARAMETER(argc);
4813   decimal_result(context, p);
4814   decimal_free(p);
4815 }
4816
4817 /*
4818 ** Compare to Decimal objects.  Return negative, 0, or positive if the
4819 ** first object is less than, equal to, or greater than the second.
4820 **
4821 ** Preconditions for this routine:
4822 **
4823 **    pA!=0
4824 **    pA->isNull==0
4825 **    pB!=0
4826 **    pB->isNull==0
4827 */
4828 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4829   int nASig, nBSig, rc, n;
4830   if( pA->sign!=pB->sign ){
4831     return pA->sign ? -1 : +1;
4832   }
4833   if( pA->sign ){
4834     const Decimal *pTemp = pA;
4835     pA = pB;
4836     pB = pTemp;
4837   }
4838   nASig = pA->nDigit - pA->nFrac;
4839   nBSig = pB->nDigit - pB->nFrac;
4840   if( nASig!=nBSig ){
4841     return nASig - nBSig;
4842   }
4843   n = pA->nDigit;
4844   if( n>pB->nDigit ) n = pB->nDigit;
4845   rc = memcmp(pA->a, pB->a, n);
4846   if( rc==0 ){
4847     rc = pA->nDigit - pB->nDigit;
4848   }
4849   return rc;
4850 }
4851
4852 /*
4853 ** SQL Function:   decimal_cmp(X, Y)
4854 **
4855 ** Return negative, zero, or positive if X is less then, equal to, or
4856 ** greater than Y.
4857 */
4858 static void decimalCmpFunc(
4859   sqlite3_context *context,
4860   int argc,
4861   sqlite3_value **argv
4862 ){
4863   Decimal *pA = 0, *pB = 0;
4864   int rc;
4865
4866   UNUSED_PARAMETER(argc);
4867   pA = decimal_new(context, argv[0], 0, 0);
4868   if( pA==0 || pA->isNull ) goto cmp_done;
4869   pB = decimal_new(context, argv[1], 0, 0);
4870   if( pB==0 || pB->isNull ) goto cmp_done;
4871   rc = decimal_cmp(pA, pB);
4872   if( rc<0 ) rc = -1;
4873   else if( rc>0 ) rc = +1;
4874   sqlite3_result_int(context, rc);
4875 cmp_done:
4876   decimal_free(pA);
4877   decimal_free(pB);
4878 }
4879
4880 /*
4881 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4882 ** digits to the right of the decimal point.
4883 */
4884 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4885   int nAddSig;
4886   int nAddFrac;
4887   if( p==0 ) return;
4888   nAddFrac = nFrac - p->nFrac;
4889   nAddSig = (nDigit - p->nDigit) - nAddFrac;
4890   if( nAddFrac==0 && nAddSig==0 ) return;
4891   p->a = sqlite3_realloc64(p->a, nDigit+1);
4892   if( p->a==0 ){
4893     p->oom = 1;
4894     return;
4895   }
4896   if( nAddSig ){
4897     memmove(p->a+nAddSig, p->a, p->nDigit);
4898     memset(p->a, 0, nAddSig);
4899     p->nDigit += nAddSig;
4900   }
4901   if( nAddFrac ){
4902     memset(p->a+p->nDigit, 0, nAddFrac);
4903     p->nDigit += nAddFrac;
4904     p->nFrac += nAddFrac;
4905   }
4906 }
4907
4908 /*
4909 ** Add the value pB into pA.
4910 **
4911 ** Both pA and pB might become denormalized by this routine.
4912 */
4913 static void decimal_add(Decimal *pA, Decimal *pB){
4914   int nSig, nFrac, nDigit;
4915   int i, rc;
4916   if( pA==0 ){
4917     return;
4918   }
4919   if( pA->oom || pB==0 || pB->oom ){
4920     pA->oom = 1;
4921     return;
4922   }
4923   if( pA->isNull || pB->isNull ){
4924     pA->isNull = 1;
4925     return;
4926   }
4927   nSig = pA->nDigit - pA->nFrac;
4928   if( nSig && pA->a[0]==0 ) nSig--;
4929   if( nSig<pB->nDigit-pB->nFrac ){
4930     nSig = pB->nDigit - pB->nFrac;
4931   }
4932   nFrac = pA->nFrac;
4933   if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4934   nDigit = nSig + nFrac + 1;
4935   decimal_expand(pA, nDigit, nFrac);
4936   decimal_expand(pB, nDigit, nFrac);
4937   if( pA->oom || pB->oom ){
4938     pA->oom = 1;
4939   }else{
4940     if( pA->sign==pB->sign ){
4941       int carry = 0;
4942       for(i=nDigit-1; i>=0; i--){
4943         int x = pA->a[i] + pB->a[i] + carry;
4944         if( x>=10 ){
4945           carry = 1;
4946           pA->a[i] = x - 10;
4947         }else{
4948           carry = 0;
4949           pA->a[i] = x;
4950         }
4951       }
4952     }else{
4953       signed char *aA, *aB;
4954       int borrow = 0;
4955       rc = memcmp(pA->a, pB->a, nDigit);
4956       if( rc<0 ){
4957         aA = pB->a;
4958         aB = pA->a;
4959         pA->sign = !pA->sign;
4960       }else{
4961         aA = pA->a;
4962         aB = pB->a;
4963       }
4964       for(i=nDigit-1; i>=0; i--){
4965         int x = aA[i] - aB[i] - borrow;
4966         if( x<0 ){
4967           pA->a[i] = x+10;
4968           borrow = 1;
4969         }else{
4970           pA->a[i] = x;
4971           borrow = 0;
4972         }
4973       }
4974     }
4975   }
4976 }
4977
4978 /*
4979 ** Compare text in decimal order.
4980 */
4981 static int decimalCollFunc(
4982   void *notUsed,
4983   int nKey1, const void *pKey1,
4984   int nKey2, const void *pKey2
4985 ){
4986   const unsigned char *zA = (const unsigned char*)pKey1;
4987   const unsigned char *zB = (const unsigned char*)pKey2;
4988   Decimal *pA = decimal_new(0, 0, nKey1, zA);
4989   Decimal *pB = decimal_new(0, 0, nKey2, zB);
4990   int rc;
4991   UNUSED_PARAMETER(notUsed);
4992   if( pA==0 || pB==0 ){
4993     rc = 0;
4994   }else{
4995     rc = decimal_cmp(pA, pB);
4996   }
4997   decimal_free(pA);
4998   decimal_free(pB);
4999   return rc;
5000 }
5001
5002
5003 /*
5004 ** SQL Function:   decimal_add(X, Y)
5005 **                 decimal_sub(X, Y)
5006 **
5007 ** Return the sum or difference of X and Y.
5008 */
5009 static void decimalAddFunc(
5010   sqlite3_context *context,
5011   int argc,
5012   sqlite3_value **argv
5013 ){
5014   Decimal *pA = decimal_new(context, argv[0], 0, 0);
5015   Decimal *pB = decimal_new(context, argv[1], 0, 0);
5016   UNUSED_PARAMETER(argc);
5017   decimal_add(pA, pB);
5018   decimal_result(context, pA);
5019   decimal_free(pA);
5020   decimal_free(pB);
5021 }
5022 static void decimalSubFunc(
5023   sqlite3_context *context,
5024   int argc,
5025   sqlite3_value **argv
5026 ){
5027   Decimal *pA = decimal_new(context, argv[0], 0, 0);
5028   Decimal *pB = decimal_new(context, argv[1], 0, 0);
5029   UNUSED_PARAMETER(argc);
5030   if( pB ){
5031     pB->sign = !pB->sign;
5032     decimal_add(pA, pB);
5033     decimal_result(context, pA);
5034   }
5035   decimal_free(pA);
5036   decimal_free(pB);
5037 }
5038
5039 /* Aggregate funcion:   decimal_sum(X)
5040 **
5041 ** Works like sum() except that it uses decimal arithmetic for unlimited
5042 ** precision.
5043 */
5044 static void decimalSumStep(
5045   sqlite3_context *context,
5046   int argc,
5047   sqlite3_value **argv
5048 ){
5049   Decimal *p;
5050   Decimal *pArg;
5051   UNUSED_PARAMETER(argc);
5052   p = sqlite3_aggregate_context(context, sizeof(*p));
5053   if( p==0 ) return;
5054   if( !p->isInit ){
5055     p->isInit = 1;
5056     p->a = sqlite3_malloc(2);
5057     if( p->a==0 ){
5058       p->oom = 1;
5059     }else{
5060       p->a[0] = 0;
5061     }
5062     p->nDigit = 1;
5063     p->nFrac = 0;
5064   }
5065   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5066   pArg = decimal_new(context, argv[0], 0, 0);
5067   decimal_add(p, pArg);
5068   decimal_free(pArg);
5069 }
5070 static void decimalSumInverse(
5071   sqlite3_context *context,
5072   int argc,
5073   sqlite3_value **argv
5074 ){
5075   Decimal *p;
5076   Decimal *pArg;
5077   UNUSED_PARAMETER(argc);
5078   p = sqlite3_aggregate_context(context, sizeof(*p));
5079   if( p==0 ) return;
5080   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5081   pArg = decimal_new(context, argv[0], 0, 0);
5082   if( pArg ) pArg->sign = !pArg->sign;
5083   decimal_add(p, pArg);
5084   decimal_free(pArg);
5085 }
5086 static void decimalSumValue(sqlite3_context *context){
5087   Decimal *p = sqlite3_aggregate_context(context, 0);
5088   if( p==0 ) return;
5089   decimal_result(context, p);
5090 }
5091 static void decimalSumFinalize(sqlite3_context *context){
5092   Decimal *p = sqlite3_aggregate_context(context, 0);
5093   if( p==0 ) return;
5094   decimal_result(context, p);
5095   decimal_clear(p);
5096 }
5097
5098 /*
5099 ** SQL Function:   decimal_mul(X, Y)
5100 **
5101 ** Return the product of X and Y.
5102 **
5103 ** All significant digits after the decimal point are retained.
5104 ** Trailing zeros after the decimal point are omitted as long as
5105 ** the number of digits after the decimal point is no less than
5106 ** either the number of digits in either input.
5107 */
5108 static void decimalMulFunc(
5109   sqlite3_context *context,
5110   int argc,
5111   sqlite3_value **argv
5112 ){
5113   Decimal *pA = decimal_new(context, argv[0], 0, 0);
5114   Decimal *pB = decimal_new(context, argv[1], 0, 0);
5115   signed char *acc = 0;
5116   int i, j, k;
5117   int minFrac;
5118   UNUSED_PARAMETER(argc);
5119   if( pA==0 || pA->oom || pA->isNull
5120    || pB==0 || pB->oom || pB->isNull 
5121   ){
5122     goto mul_end;
5123   }
5124   acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
5125   if( acc==0 ){
5126     sqlite3_result_error_nomem(context);
5127     goto mul_end;
5128   }
5129   memset(acc, 0, pA->nDigit + pB->nDigit + 2);
5130   minFrac = pA->nFrac;
5131   if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
5132   for(i=pA->nDigit-1; i>=0; i--){
5133     signed char f = pA->a[i];
5134     int carry = 0, x;
5135     for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
5136       x = acc[k] + f*pB->a[j] + carry;
5137       acc[k] = x%10;
5138       carry = x/10;
5139     }
5140     x = acc[k] + carry;
5141     acc[k] = x%10;
5142     acc[k-1] += x/10;
5143   }
5144   sqlite3_free(pA->a);
5145   pA->a = acc;
5146   acc = 0;
5147   pA->nDigit += pB->nDigit + 2;
5148   pA->nFrac += pB->nFrac;
5149   pA->sign ^= pB->sign;
5150   while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
5151     pA->nFrac--;
5152     pA->nDigit--;
5153   }
5154   decimal_result(context, pA);
5155
5156 mul_end:
5157   sqlite3_free(acc);
5158   decimal_free(pA);
5159   decimal_free(pB);
5160 }
5161
5162 #ifdef _WIN32
5163
5164 #endif
5165 int sqlite3_decimal_init(
5166   sqlite3 *db, 
5167   char **pzErrMsg, 
5168   const sqlite3_api_routines *pApi
5169 ){
5170   int rc = SQLITE_OK;
5171   static const struct {
5172     const char *zFuncName;
5173     int nArg;
5174     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5175   } aFunc[] = {
5176     { "decimal",       1,   decimalFunc        },
5177     { "decimal_cmp",   2,   decimalCmpFunc     },
5178     { "decimal_add",   2,   decimalAddFunc     },
5179     { "decimal_sub",   2,   decimalSubFunc     },
5180     { "decimal_mul",   2,   decimalMulFunc     },
5181   };
5182   unsigned int i;
5183   (void)pzErrMsg;  /* Unused parameter */
5184
5185   SQLITE_EXTENSION_INIT2(pApi);
5186
5187   for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5188     rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5189                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5190                    0, aFunc[i].xFunc, 0, 0);
5191   }
5192   if( rc==SQLITE_OK ){
5193     rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5194                    SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5195                    decimalSumStep, decimalSumFinalize,
5196                    decimalSumValue, decimalSumInverse, 0);
5197   }
5198   if( rc==SQLITE_OK ){
5199     rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5200                                   0, decimalCollFunc);
5201   }
5202   return rc;
5203 }
5204
5205 /************************* End ../ext/misc/decimal.c ********************/
5206 /************************* Begin ../ext/misc/ieee754.c ******************/
5207 /*
5208 ** 2013-04-17
5209 **
5210 ** The author disclaims copyright to this source code.  In place of
5211 ** a legal notice, here is a blessing:
5212 **
5213 **    May you do good and not evil.
5214 **    May you find forgiveness for yourself and forgive others.
5215 **    May you share freely, never taking more than you give.
5216 **
5217 ******************************************************************************
5218 **
5219 ** This SQLite extension implements functions for the exact display
5220 ** and input of IEEE754 Binary64 floating-point numbers.
5221 **
5222 **   ieee754(X)
5223 **   ieee754(Y,Z)
5224 **
5225 ** In the first form, the value X should be a floating-point number.
5226 ** The function will return a string of the form 'ieee754(Y,Z)' where
5227 ** Y and Z are integers such that X==Y*pow(2,Z).
5228 **
5229 ** In the second form, Y and Z are integers which are the mantissa and
5230 ** base-2 exponent of a new floating point number.  The function returns
5231 ** a floating-point value equal to Y*pow(2,Z).
5232 **
5233 ** Examples:
5234 **
5235 **     ieee754(2.0)             ->     'ieee754(2,0)'
5236 **     ieee754(45.25)           ->     'ieee754(181,-2)'
5237 **     ieee754(2, 0)            ->     2.0
5238 **     ieee754(181, -2)         ->     45.25
5239 **
5240 ** Two additional functions break apart the one-argument ieee754()
5241 ** result into separate integer values:
5242 **
5243 **     ieee754_mantissa(45.25)  ->     181
5244 **     ieee754_exponent(45.25)  ->     -2
5245 **
5246 ** These functions convert binary64 numbers into blobs and back again.
5247 **
5248 **     ieee754_from_blob(x'3ff0000000000000')  ->  1.0
5249 **     ieee754_to_blob(1.0)                    ->  x'3ff0000000000000'
5250 **
5251 ** In all single-argument functions, if the argument is an 8-byte blob
5252 ** then that blob is interpreted as a big-endian binary64 value.
5253 **
5254 **
5255 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5256 ** -----------------------------------------------
5257 **
5258 ** This extension in combination with the separate 'decimal' extension
5259 ** can be used to compute the exact decimal representation of binary64
5260 ** values.  To begin, first compute a table of exponent values:
5261 **
5262 **    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5263 **    WITH RECURSIVE c(x,v) AS (
5264 **      VALUES(0,'1')
5265 **      UNION ALL
5266 **      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5267 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5268 **    WITH RECURSIVE c(x,v) AS (
5269 **      VALUES(-1,'0.5')
5270 **      UNION ALL
5271 **      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5272 **    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5273 **
5274 ** Then, to compute the exact decimal representation of a floating
5275 ** point value (the value 47.49 is used in the example) do:
5276 **
5277 **    WITH c(n) AS (VALUES(47.49))
5278 **          ---------------^^^^^---- Replace with whatever you want
5279 **    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5280 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5281 **
5282 ** Here is a query to show various boundry values for the binary64
5283 ** number format:
5284 **
5285 **    WITH c(name,bin) AS (VALUES
5286 **       ('minimum positive value',        x'0000000000000001'),
5287 **       ('maximum subnormal value',       x'000fffffffffffff'),
5288 **       ('mininum positive nornal value', x'0010000000000000'),
5289 **       ('maximum value',                 x'7fefffffffffffff'))
5290 **    SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5291 **      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5292 **
5293 */
5294 /* #include "sqlite3ext.h" */
5295 SQLITE_EXTENSION_INIT1
5296 #include <assert.h>
5297 #include <string.h>
5298
5299 /* Mark a function parameter as unused, to suppress nuisance compiler
5300 ** warnings. */
5301 #ifndef UNUSED_PARAMETER
5302 # define UNUSED_PARAMETER(X)  (void)(X)
5303 #endif
5304
5305 /*
5306 ** Implementation of the ieee754() function
5307 */
5308 static void ieee754func(
5309   sqlite3_context *context,
5310   int argc,
5311   sqlite3_value **argv
5312 ){
5313   if( argc==1 ){
5314     sqlite3_int64 m, a;
5315     double r;
5316     int e;
5317     int isNeg;
5318     char zResult[100];
5319     assert( sizeof(m)==sizeof(r) );
5320     if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5321      && sqlite3_value_bytes(argv[0])==sizeof(r)
5322     ){
5323       const unsigned char *x = sqlite3_value_blob(argv[0]);
5324       unsigned int i;
5325       sqlite3_uint64 v = 0;
5326       for(i=0; i<sizeof(r); i++){
5327         v = (v<<8) | x[i];
5328       }
5329       memcpy(&r, &v, sizeof(r));
5330     }else{
5331       r = sqlite3_value_double(argv[0]);
5332     }
5333     if( r<0.0 ){
5334       isNeg = 1;
5335       r = -r;
5336     }else{
5337       isNeg = 0;
5338     }
5339     memcpy(&a,&r,sizeof(a));
5340     if( a==0 ){
5341       e = 0;
5342       m = 0;
5343     }else{
5344       e = a>>52;
5345       m = a & ((((sqlite3_int64)1)<<52)-1);
5346       if( e==0 ){
5347         m <<= 1;
5348       }else{
5349         m |= ((sqlite3_int64)1)<<52;
5350       }
5351       while( e<1075 && m>0 && (m&1)==0 ){
5352         m >>= 1;
5353         e++;
5354       }
5355       if( isNeg ) m = -m;
5356     }
5357     switch( *(int*)sqlite3_user_data(context) ){
5358       case 0:
5359         sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5360                          m, e-1075);
5361         sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5362         break;
5363       case 1:
5364         sqlite3_result_int64(context, m);
5365         break;
5366       case 2:
5367         sqlite3_result_int(context, e-1075);
5368         break;
5369     }
5370   }else{
5371     sqlite3_int64 m, e, a;
5372     double r;
5373     int isNeg = 0;
5374     m = sqlite3_value_int64(argv[0]);
5375     e = sqlite3_value_int64(argv[1]);
5376
5377     /* Limit the range of e.  Ticket 22dea1cfdb9151e4 2021-03-02 */
5378     if( e>10000 ){
5379       e = 10000;
5380     }else if( e<-10000 ){
5381       e = -10000;
5382     }
5383
5384     if( m<0 ){
5385       isNeg = 1;
5386       m = -m;
5387       if( m<0 ) return;
5388     }else if( m==0 && e>-1000 && e<1000 ){
5389       sqlite3_result_double(context, 0.0);
5390       return;
5391     }
5392     while( (m>>32)&0xffe00000 ){
5393       m >>= 1;
5394       e++;
5395     }
5396     while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5397       m <<= 1;
5398       e--;
5399     }
5400     e += 1075;
5401     if( e<=0 ){
5402       /* Subnormal */
5403       if( 1-e >= 64 ){
5404         m = 0;
5405       }else{
5406         m >>= 1-e;
5407       }
5408       e = 0;
5409     }else if( e>0x7ff ){
5410       e = 0x7ff;
5411     }
5412     a = m & ((((sqlite3_int64)1)<<52)-1);
5413     a |= e<<52;
5414     if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5415     memcpy(&r, &a, sizeof(r));
5416     sqlite3_result_double(context, r);
5417   }
5418 }
5419
5420 /*
5421 ** Functions to convert between blobs and floats.
5422 */
5423 static void ieee754func_from_blob(
5424   sqlite3_context *context,
5425   int argc,
5426   sqlite3_value **argv
5427 ){
5428   UNUSED_PARAMETER(argc);
5429   if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5430    && sqlite3_value_bytes(argv[0])==sizeof(double)
5431   ){
5432     double r;
5433     const unsigned char *x = sqlite3_value_blob(argv[0]);
5434     unsigned int i;
5435     sqlite3_uint64 v = 0;
5436     for(i=0; i<sizeof(r); i++){
5437       v = (v<<8) | x[i];
5438     }
5439     memcpy(&r, &v, sizeof(r));
5440     sqlite3_result_double(context, r);
5441   }
5442 }
5443 static void ieee754func_to_blob(
5444   sqlite3_context *context,
5445   int argc,
5446   sqlite3_value **argv
5447 ){
5448   UNUSED_PARAMETER(argc);
5449   if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5450    || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5451   ){
5452     double r = sqlite3_value_double(argv[0]);
5453     sqlite3_uint64 v;
5454     unsigned char a[sizeof(r)];
5455     unsigned int i;
5456     memcpy(&v, &r, sizeof(r));
5457     for(i=1; i<=sizeof(r); i++){
5458       a[sizeof(r)-i] = v&0xff;
5459       v >>= 8;
5460     }
5461     sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5462   }
5463 }
5464
5465
5466 #ifdef _WIN32
5467
5468 #endif
5469 int sqlite3_ieee_init(
5470   sqlite3 *db, 
5471   char **pzErrMsg, 
5472   const sqlite3_api_routines *pApi
5473 ){
5474   static const struct {
5475     char *zFName;
5476     int nArg;
5477     int iAux;
5478     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5479   } aFunc[] = {
5480     { "ieee754",           1,   0, ieee754func },
5481     { "ieee754",           2,   0, ieee754func },
5482     { "ieee754_mantissa",  1,   1, ieee754func },
5483     { "ieee754_exponent",  1,   2, ieee754func },
5484     { "ieee754_to_blob",   1,   0, ieee754func_to_blob },
5485     { "ieee754_from_blob", 1,   0, ieee754func_from_blob },
5486
5487   };
5488   unsigned int i;
5489   int rc = SQLITE_OK;
5490   SQLITE_EXTENSION_INIT2(pApi);
5491   (void)pzErrMsg;  /* Unused parameter */
5492   for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5493     rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,    
5494                                SQLITE_UTF8|SQLITE_INNOCUOUS,
5495                                (void*)&aFunc[i].iAux,
5496                                aFunc[i].xFunc, 0, 0);
5497   }
5498   return rc;
5499 }
5500
5501 /************************* End ../ext/misc/ieee754.c ********************/
5502 /************************* Begin ../ext/misc/series.c ******************/
5503 /*
5504 ** 2015-08-18
5505 **
5506 ** The author disclaims copyright to this source code.  In place of
5507 ** a legal notice, here is a blessing:
5508 **
5509 **    May you do good and not evil.
5510 **    May you find forgiveness for yourself and forgive others.
5511 **    May you share freely, never taking more than you give.
5512 **
5513 *************************************************************************
5514 **
5515 ** This file demonstrates how to create a table-valued-function using
5516 ** a virtual table.  This demo implements the generate_series() function
5517 ** which gives similar results to the eponymous function in PostgreSQL.
5518 ** Examples:
5519 **
5520 **      SELECT * FROM generate_series(0,100,5);
5521 **
5522 ** The query above returns integers from 0 through 100 counting by steps
5523 ** of 5.
5524 **
5525 **      SELECT * FROM generate_series(0,100);
5526 **
5527 ** Integers from 0 through 100 with a step size of 1.
5528 **
5529 **      SELECT * FROM generate_series(20) LIMIT 10;
5530 **
5531 ** Integers 20 through 29.
5532 **
5533 ** HOW IT WORKS
5534 **
5535 ** The generate_series "function" is really a virtual table with the
5536 ** following schema:
5537 **
5538 **     CREATE TABLE generate_series(
5539 **       value,
5540 **       start HIDDEN,
5541 **       stop HIDDEN,
5542 **       step HIDDEN
5543 **     );
5544 **
5545 ** Function arguments in queries against this virtual table are translated
5546 ** into equality constraints against successive hidden columns.  In other
5547 ** words, the following pairs of queries are equivalent to each other:
5548 **
5549 **    SELECT * FROM generate_series(0,100,5);
5550 **    SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5551 **
5552 **    SELECT * FROM generate_series(0,100);
5553 **    SELECT * FROM generate_series WHERE start=0 AND stop=100;
5554 **
5555 **    SELECT * FROM generate_series(20) LIMIT 10;
5556 **    SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5557 **
5558 ** The generate_series virtual table implementation leaves the xCreate method
5559 ** set to NULL.  This means that it is not possible to do a CREATE VIRTUAL
5560 ** TABLE command with "generate_series" as the USING argument.  Instead, there
5561 ** is a single generate_series virtual table that is always available without
5562 ** having to be created first.
5563 **
5564 ** The xBestIndex method looks for equality constraints against the hidden
5565 ** start, stop, and step columns, and if present, it uses those constraints
5566 ** to bound the sequence of generated values.  If the equality constraints
5567 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5568 ** xBestIndex returns a small cost when both start and stop are available,
5569 ** and a very large cost if either start or stop are unavailable.  This
5570 ** encourages the query planner to order joins such that the bounds of the
5571 ** series are well-defined.
5572 */
5573 /* #include "sqlite3ext.h" */
5574 SQLITE_EXTENSION_INIT1
5575 #include <assert.h>
5576 #include <string.h>
5577
5578 #ifndef SQLITE_OMIT_VIRTUALTABLE
5579
5580
5581 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5582 ** serve as the underlying representation of a cursor that scans
5583 ** over rows of the result
5584 */
5585 typedef struct series_cursor series_cursor;
5586 struct series_cursor {
5587   sqlite3_vtab_cursor base;  /* Base class - must be first */
5588   int isDesc;                /* True to count down rather than up */
5589   sqlite3_int64 iRowid;      /* The rowid */
5590   sqlite3_int64 iValue;      /* Current value ("value") */
5591   sqlite3_int64 mnValue;     /* Mimimum value ("start") */
5592   sqlite3_int64 mxValue;     /* Maximum value ("stop") */
5593   sqlite3_int64 iStep;       /* Increment ("step") */
5594 };
5595
5596 /*
5597 ** The seriesConnect() method is invoked to create a new
5598 ** series_vtab that describes the generate_series virtual table.
5599 **
5600 ** Think of this routine as the constructor for series_vtab objects.
5601 **
5602 ** All this routine needs to do is:
5603 **
5604 **    (1) Allocate the series_vtab object and initialize all fields.
5605 **
5606 **    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5607 **        result set of queries against generate_series will look like.
5608 */
5609 static int seriesConnect(
5610   sqlite3 *db,
5611   void *pUnused,
5612   int argcUnused, const char *const*argvUnused,
5613   sqlite3_vtab **ppVtab,
5614   char **pzErrUnused
5615 ){
5616   sqlite3_vtab *pNew;
5617   int rc;
5618
5619 /* Column numbers */
5620 #define SERIES_COLUMN_VALUE 0
5621 #define SERIES_COLUMN_START 1
5622 #define SERIES_COLUMN_STOP  2
5623 #define SERIES_COLUMN_STEP  3
5624
5625   (void)pUnused;
5626   (void)argcUnused;
5627   (void)argvUnused;
5628   (void)pzErrUnused;
5629   rc = sqlite3_declare_vtab(db,
5630      "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5631   if( rc==SQLITE_OK ){
5632     pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5633     if( pNew==0 ) return SQLITE_NOMEM;
5634     memset(pNew, 0, sizeof(*pNew));
5635     sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5636   }
5637   return rc;
5638 }
5639
5640 /*
5641 ** This method is the destructor for series_cursor objects.
5642 */
5643 static int seriesDisconnect(sqlite3_vtab *pVtab){
5644   sqlite3_free(pVtab);
5645   return SQLITE_OK;
5646 }
5647
5648 /*
5649 ** Constructor for a new series_cursor object.
5650 */
5651 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5652   series_cursor *pCur;
5653   (void)pUnused;
5654   pCur = sqlite3_malloc( sizeof(*pCur) );
5655   if( pCur==0 ) return SQLITE_NOMEM;
5656   memset(pCur, 0, sizeof(*pCur));
5657   *ppCursor = &pCur->base;
5658   return SQLITE_OK;
5659 }
5660
5661 /*
5662 ** Destructor for a series_cursor.
5663 */
5664 static int seriesClose(sqlite3_vtab_cursor *cur){
5665   sqlite3_free(cur);
5666   return SQLITE_OK;
5667 }
5668
5669
5670 /*
5671 ** Advance a series_cursor to its next row of output.
5672 */
5673 static int seriesNext(sqlite3_vtab_cursor *cur){
5674   series_cursor *pCur = (series_cursor*)cur;
5675   if( pCur->isDesc ){
5676     pCur->iValue -= pCur->iStep;
5677   }else{
5678     pCur->iValue += pCur->iStep;
5679   }
5680   pCur->iRowid++;
5681   return SQLITE_OK;
5682 }
5683
5684 /*
5685 ** Return values of columns for the row at which the series_cursor
5686 ** is currently pointing.
5687 */
5688 static int seriesColumn(
5689   sqlite3_vtab_cursor *cur,   /* The cursor */
5690   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5691   int i                       /* Which column to return */
5692 ){
5693   series_cursor *pCur = (series_cursor*)cur;
5694   sqlite3_int64 x = 0;
5695   switch( i ){
5696     case SERIES_COLUMN_START:  x = pCur->mnValue; break;
5697     case SERIES_COLUMN_STOP:   x = pCur->mxValue; break;
5698     case SERIES_COLUMN_STEP:   x = pCur->iStep;   break;
5699     default:                   x = pCur->iValue;  break;
5700   }
5701   sqlite3_result_int64(ctx, x);
5702   return SQLITE_OK;
5703 }
5704
5705 /*
5706 ** Return the rowid for the current row. In this implementation, the
5707 ** first row returned is assigned rowid value 1, and each subsequent
5708 ** row a value 1 more than that of the previous.
5709 */
5710 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5711   series_cursor *pCur = (series_cursor*)cur;
5712   *pRowid = pCur->iRowid;
5713   return SQLITE_OK;
5714 }
5715
5716 /*
5717 ** Return TRUE if the cursor has been moved off of the last
5718 ** row of output.
5719 */
5720 static int seriesEof(sqlite3_vtab_cursor *cur){
5721   series_cursor *pCur = (series_cursor*)cur;
5722   if( pCur->isDesc ){
5723     return pCur->iValue < pCur->mnValue;
5724   }else{
5725     return pCur->iValue > pCur->mxValue;
5726   }
5727 }
5728
5729 /* True to cause run-time checking of the start=, stop=, and/or step= 
5730 ** parameters.  The only reason to do this is for testing the
5731 ** constraint checking logic for virtual tables in the SQLite core.
5732 */
5733 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5734 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5735 #endif
5736
5737 /*
5738 ** This method is called to "rewind" the series_cursor object back
5739 ** to the first row of output.  This method is always called at least
5740 ** once prior to any call to seriesColumn() or seriesRowid() or 
5741 ** seriesEof().
5742 **
5743 ** The query plan selected by seriesBestIndex is passed in the idxNum
5744 ** parameter.  (idxStr is not used in this implementation.)  idxNum
5745 ** is a bitmask showing which constraints are available:
5746 **
5747 **    1:    start=VALUE
5748 **    2:    stop=VALUE
5749 **    4:    step=VALUE
5750 **
5751 ** Also, if bit 8 is set, that means that the series should be output
5752 ** in descending order rather than in ascending order.  If bit 16 is
5753 ** set, then output must appear in ascending order.
5754 **
5755 ** This routine should initialize the cursor and position it so that it
5756 ** is pointing at the first row, or pointing off the end of the table
5757 ** (so that seriesEof() will return true) if the table is empty.
5758 */
5759 static int seriesFilter(
5760   sqlite3_vtab_cursor *pVtabCursor, 
5761   int idxNum, const char *idxStrUnused,
5762   int argc, sqlite3_value **argv
5763 ){
5764   series_cursor *pCur = (series_cursor *)pVtabCursor;
5765   int i = 0;
5766   (void)idxStrUnused;
5767   if( idxNum & 1 ){
5768     pCur->mnValue = sqlite3_value_int64(argv[i++]);
5769   }else{
5770     pCur->mnValue = 0;
5771   }
5772   if( idxNum & 2 ){
5773     pCur->mxValue = sqlite3_value_int64(argv[i++]);
5774   }else{
5775     pCur->mxValue = 0xffffffff;
5776   }
5777   if( idxNum & 4 ){
5778     pCur->iStep = sqlite3_value_int64(argv[i++]);
5779     if( pCur->iStep==0 ){
5780       pCur->iStep = 1;
5781     }else if( pCur->iStep<0 ){
5782       pCur->iStep = -pCur->iStep;
5783       if( (idxNum & 16)==0 ) idxNum |= 8;
5784     }
5785   }else{
5786     pCur->iStep = 1;
5787   }
5788   for(i=0; i<argc; i++){
5789     if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5790       /* If any of the constraints have a NULL value, then return no rows.
5791       ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5792       pCur->mnValue = 1;
5793       pCur->mxValue = 0;
5794       break;
5795     }
5796   }
5797   if( idxNum & 8 ){
5798     pCur->isDesc = 1;
5799     pCur->iValue = pCur->mxValue;
5800     if( pCur->iStep>0 ){
5801       pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5802     }
5803   }else{
5804     pCur->isDesc = 0;
5805     pCur->iValue = pCur->mnValue;
5806   }
5807   pCur->iRowid = 1;
5808   return SQLITE_OK;
5809 }
5810
5811 /*
5812 ** SQLite will invoke this method one or more times while planning a query
5813 ** that uses the generate_series virtual table.  This routine needs to create
5814 ** a query plan for each invocation and compute an estimated cost for that
5815 ** plan.
5816 **
5817 ** In this implementation idxNum is used to represent the
5818 ** query plan.  idxStr is unused.
5819 **
5820 ** The query plan is represented by bits in idxNum:
5821 **
5822 **  (1)  start = $value  -- constraint exists
5823 **  (2)  stop = $value   -- constraint exists
5824 **  (4)  step = $value   -- constraint exists
5825 **  (8)  output in descending order
5826 */
5827 static int seriesBestIndex(
5828   sqlite3_vtab *pVTab,
5829   sqlite3_index_info *pIdxInfo
5830 ){
5831   int i, j;              /* Loop over constraints */
5832   int idxNum = 0;        /* The query plan bitmask */
5833   int bStartSeen = 0;    /* EQ constraint seen on the START column */
5834   int unusableMask = 0;  /* Mask of unusable constraints */
5835   int nArg = 0;          /* Number of arguments that seriesFilter() expects */
5836   int aIdx[3];           /* Constraints on start, stop, and step */
5837   const struct sqlite3_index_constraint *pConstraint;
5838
5839   /* This implementation assumes that the start, stop, and step columns
5840   ** are the last three columns in the virtual table. */
5841   assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5842   assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5843
5844   aIdx[0] = aIdx[1] = aIdx[2] = -1;
5845   pConstraint = pIdxInfo->aConstraint;
5846   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5847     int iCol;    /* 0 for start, 1 for stop, 2 for step */
5848     int iMask;   /* bitmask for those column */
5849     if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5850     iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5851     assert( iCol>=0 && iCol<=2 );
5852     iMask = 1 << iCol;
5853     if( iCol==0 ) bStartSeen = 1;
5854     if( pConstraint->usable==0 ){
5855       unusableMask |=  iMask;
5856       continue;
5857     }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5858       idxNum |= iMask;
5859       aIdx[iCol] = i;
5860     }
5861   }
5862   for(i=0; i<3; i++){
5863     if( (j = aIdx[i])>=0 ){
5864       pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5865       pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5866     }
5867   }
5868   /* The current generate_column() implementation requires at least one
5869   ** argument (the START value).  Legacy versions assumed START=0 if the
5870   ** first argument was omitted.  Compile with -DZERO_ARGUMENT_GENERATE_SERIES
5871   ** to obtain the legacy behavior */
5872 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
5873   if( !bStartSeen ){
5874     sqlite3_free(pVTab->zErrMsg);
5875     pVTab->zErrMsg = sqlite3_mprintf(
5876         "first argument to \"generate_series()\" missing or unusable");
5877     return SQLITE_ERROR;
5878   }
5879 #endif
5880   if( (unusableMask & ~idxNum)!=0 ){
5881     /* The start, stop, and step columns are inputs.  Therefore if there
5882     ** are unusable constraints on any of start, stop, or step then
5883     ** this plan is unusable */
5884     return SQLITE_CONSTRAINT;
5885   }
5886   if( (idxNum & 3)==3 ){
5887     /* Both start= and stop= boundaries are available.  This is the 
5888     ** the preferred case */
5889     pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5890     pIdxInfo->estimatedRows = 1000;
5891     if( pIdxInfo->nOrderBy==1 ){
5892       if( pIdxInfo->aOrderBy[0].desc ){
5893         idxNum |= 8;
5894       }else{
5895         idxNum |= 16;
5896       }
5897       pIdxInfo->orderByConsumed = 1;
5898     }
5899   }else{
5900     /* If either boundary is missing, we have to generate a huge span
5901     ** of numbers.  Make this case very expensive so that the query
5902     ** planner will work hard to avoid it. */
5903     pIdxInfo->estimatedRows = 2147483647;
5904   }
5905   pIdxInfo->idxNum = idxNum;
5906   return SQLITE_OK;
5907 }
5908
5909 /*
5910 ** This following structure defines all the methods for the 
5911 ** generate_series virtual table.
5912 */
5913 static sqlite3_module seriesModule = {
5914   0,                         /* iVersion */
5915   0,                         /* xCreate */
5916   seriesConnect,             /* xConnect */
5917   seriesBestIndex,           /* xBestIndex */
5918   seriesDisconnect,          /* xDisconnect */
5919   0,                         /* xDestroy */
5920   seriesOpen,                /* xOpen - open a cursor */
5921   seriesClose,               /* xClose - close a cursor */
5922   seriesFilter,              /* xFilter - configure scan constraints */
5923   seriesNext,                /* xNext - advance a cursor */
5924   seriesEof,                 /* xEof - check for end of scan */
5925   seriesColumn,              /* xColumn - read data */
5926   seriesRowid,               /* xRowid - read data */
5927   0,                         /* xUpdate */
5928   0,                         /* xBegin */
5929   0,                         /* xSync */
5930   0,                         /* xCommit */
5931   0,                         /* xRollback */
5932   0,                         /* xFindMethod */
5933   0,                         /* xRename */
5934   0,                         /* xSavepoint */
5935   0,                         /* xRelease */
5936   0,                         /* xRollbackTo */
5937   0                          /* xShadowName */
5938 };
5939
5940 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5941
5942 #ifdef _WIN32
5943
5944 #endif
5945 int sqlite3_series_init(
5946   sqlite3 *db, 
5947   char **pzErrMsg, 
5948   const sqlite3_api_routines *pApi
5949 ){
5950   int rc = SQLITE_OK;
5951   SQLITE_EXTENSION_INIT2(pApi);
5952 #ifndef SQLITE_OMIT_VIRTUALTABLE
5953   if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){
5954     *pzErrMsg = sqlite3_mprintf(
5955         "generate_series() requires SQLite 3.8.12 or later");
5956     return SQLITE_ERROR;
5957   }
5958   rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5959 #endif
5960   return rc;
5961 }
5962
5963 /************************* End ../ext/misc/series.c ********************/
5964 /************************* Begin ../ext/misc/regexp.c ******************/
5965 /*
5966 ** 2012-11-13
5967 **
5968 ** The author disclaims copyright to this source code.  In place of
5969 ** a legal notice, here is a blessing:
5970 **
5971 **    May you do good and not evil.
5972 **    May you find forgiveness for yourself and forgive others.
5973 **    May you share freely, never taking more than you give.
5974 **
5975 ******************************************************************************
5976 **
5977 ** The code in this file implements a compact but reasonably
5978 ** efficient regular-expression matcher for posix extended regular
5979 ** expressions against UTF8 text.
5980 **
5981 ** This file is an SQLite extension.  It registers a single function
5982 ** named "regexp(A,B)" where A is the regular expression and B is the
5983 ** string to be matched.  By registering this function, SQLite will also
5984 ** then implement the "B regexp A" operator.  Note that with the function
5985 ** the regular expression comes first, but with the operator it comes
5986 ** second.
5987 **
5988 **  The following regular expression syntax is supported:
5989 **
5990 **     X*      zero or more occurrences of X
5991 **     X+      one or more occurrences of X
5992 **     X?      zero or one occurrences of X
5993 **     X{p,q}  between p and q occurrences of X
5994 **     (X)     match X
5995 **     X|Y     X or Y
5996 **     ^X      X occurring at the beginning of the string
5997 **     X$      X occurring at the end of the string
5998 **     .       Match any single character
5999 **     \c      Character c where c is one of \{}()[]|*+?.
6000 **     \c      C-language escapes for c in afnrtv.  ex: \t or \n
6001 **     \uXXXX  Where XXXX is exactly 4 hex digits, unicode value XXXX
6002 **     \xXX    Where XX is exactly 2 hex digits, unicode value XX
6003 **     [abc]   Any single character from the set abc
6004 **     [^abc]  Any single character not in the set abc
6005 **     [a-z]   Any single character in the range a-z
6006 **     [^a-z]  Any single character not in the range a-z
6007 **     \b      Word boundary
6008 **     \w      Word character.  [A-Za-z0-9_]
6009 **     \W      Non-word character
6010 **     \d      Digit
6011 **     \D      Non-digit
6012 **     \s      Whitespace character
6013 **     \S      Non-whitespace character
6014 **
6015 ** A nondeterministic finite automaton (NFA) is used for matching, so the
6016 ** performance is bounded by O(N*M) where N is the size of the regular
6017 ** expression and M is the size of the input string.  The matcher never
6018 ** exhibits exponential behavior.  Note that the X{p,q} operator expands
6019 ** to p copies of X following by q-p copies of X? and that the size of the
6020 ** regular expression in the O(N*M) performance bound is computed after
6021 ** this expansion.
6022 */
6023 #include <string.h>
6024 #include <stdlib.h>
6025 /* #include "sqlite3ext.h" */
6026 SQLITE_EXTENSION_INIT1
6027
6028 /*
6029 ** The following #defines change the names of some functions implemented in
6030 ** this file to prevent name collisions with C-library functions of the
6031 ** same name.
6032 */
6033 #define re_match   sqlite3re_match
6034 #define re_compile sqlite3re_compile
6035 #define re_free    sqlite3re_free
6036
6037 /* The end-of-input character */
6038 #define RE_EOF            0    /* End of input */
6039
6040 /* The NFA is implemented as sequence of opcodes taken from the following
6041 ** set.  Each opcode has a single integer argument.
6042 */
6043 #define RE_OP_MATCH       1    /* Match the one character in the argument */
6044 #define RE_OP_ANY         2    /* Match any one character.  (Implements ".") */
6045 #define RE_OP_ANYSTAR     3    /* Special optimized version of .* */
6046 #define RE_OP_FORK        4    /* Continue to both next and opcode at iArg */
6047 #define RE_OP_GOTO        5    /* Jump to opcode at iArg */
6048 #define RE_OP_ACCEPT      6    /* Halt and indicate a successful match */
6049 #define RE_OP_CC_INC      7    /* Beginning of a [...] character class */
6050 #define RE_OP_CC_EXC      8    /* Beginning of a [^...] character class */
6051 #define RE_OP_CC_VALUE    9    /* Single value in a character class */
6052 #define RE_OP_CC_RANGE   10    /* Range of values in a character class */
6053 #define RE_OP_WORD       11    /* Perl word character [A-Za-z0-9_] */
6054 #define RE_OP_NOTWORD    12    /* Not a perl word character */
6055 #define RE_OP_DIGIT      13    /* digit:  [0-9] */
6056 #define RE_OP_NOTDIGIT   14    /* Not a digit */
6057 #define RE_OP_SPACE      15    /* space:  [ \t\n\r\v\f] */
6058 #define RE_OP_NOTSPACE   16    /* Not a digit */
6059 #define RE_OP_BOUNDARY   17    /* Boundary between word and non-word */
6060
6061 /* Each opcode is a "state" in the NFA */
6062 typedef unsigned short ReStateNumber;
6063
6064 /* Because this is an NFA and not a DFA, multiple states can be active at
6065 ** once.  An instance of the following object records all active states in
6066 ** the NFA.  The implementation is optimized for the common case where the
6067 ** number of actives states is small.
6068 */
6069 typedef struct ReStateSet {
6070   unsigned nState;            /* Number of current states */
6071   ReStateNumber *aState;      /* Current states */
6072 } ReStateSet;
6073
6074 /* An input string read one character at a time.
6075 */
6076 typedef struct ReInput ReInput;
6077 struct ReInput {
6078   const unsigned char *z;  /* All text */
6079   int i;                   /* Next byte to read */
6080   int mx;                  /* EOF when i>=mx */
6081 };
6082
6083 /* A compiled NFA (or an NFA that is in the process of being compiled) is
6084 ** an instance of the following object.
6085 */
6086 typedef struct ReCompiled ReCompiled;
6087 struct ReCompiled {
6088   ReInput sIn;                /* Regular expression text */
6089   const char *zErr;           /* Error message to return */
6090   char *aOp;                  /* Operators for the virtual machine */
6091   int *aArg;                  /* Arguments to each operator */
6092   unsigned (*xNextChar)(ReInput*);  /* Next character function */
6093   unsigned char zInit[12];    /* Initial text to match */
6094   int nInit;                  /* Number of characters in zInit */
6095   unsigned nState;            /* Number of entries in aOp[] and aArg[] */
6096   unsigned nAlloc;            /* Slots allocated for aOp[] and aArg[] */
6097 };
6098
6099 /* Add a state to the given state set if it is not already there */
6100 static void re_add_state(ReStateSet *pSet, int newState){
6101   unsigned i;
6102   for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
6103   pSet->aState[pSet->nState++] = (ReStateNumber)newState;
6104 }
6105
6106 /* Extract the next unicode character from *pzIn and return it.  Advance
6107 ** *pzIn to the first byte past the end of the character returned.  To
6108 ** be clear:  this routine converts utf8 to unicode.  This routine is 
6109 ** optimized for the common case where the next character is a single byte.
6110 */
6111 static unsigned re_next_char(ReInput *p){
6112   unsigned c;
6113   if( p->i>=p->mx ) return 0;
6114   c = p->z[p->i++];
6115   if( c>=0x80 ){
6116     if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
6117       c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
6118       if( c<0x80 ) c = 0xfffd;
6119     }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
6120            && (p->z[p->i+1]&0xc0)==0x80 ){
6121       c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
6122       p->i += 2;
6123       if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
6124     }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
6125            && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
6126       c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
6127                        | (p->z[p->i+2]&0x3f);
6128       p->i += 3;
6129       if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
6130     }else{
6131       c = 0xfffd;
6132     }
6133   }
6134   return c;
6135 }
6136 static unsigned re_next_char_nocase(ReInput *p){
6137   unsigned c = re_next_char(p);
6138   if( c>='A' && c<='Z' ) c += 'a' - 'A';
6139   return c;
6140 }
6141
6142 /* Return true if c is a perl "word" character:  [A-Za-z0-9_] */
6143 static int re_word_char(int c){
6144   return (c>='0' && c<='9') || (c>='a' && c<='z')
6145       || (c>='A' && c<='Z') || c=='_';
6146 }
6147
6148 /* Return true if c is a "digit" character:  [0-9] */
6149 static int re_digit_char(int c){
6150   return (c>='0' && c<='9');
6151 }
6152
6153 /* Return true if c is a perl "space" character:  [ \t\r\n\v\f] */
6154 static int re_space_char(int c){
6155   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
6156 }
6157
6158 /* Run a compiled regular expression on the zero-terminated input
6159 ** string zIn[].  Return true on a match and false if there is no match.
6160 */
6161 static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
6162   ReStateSet aStateSet[2], *pThis, *pNext;
6163   ReStateNumber aSpace[100];
6164   ReStateNumber *pToFree;
6165   unsigned int i = 0;
6166   unsigned int iSwap = 0;
6167   int c = RE_EOF+1;
6168   int cPrev = 0;
6169   int rc = 0;
6170   ReInput in;
6171
6172   in.z = zIn;
6173   in.i = 0;
6174   in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
6175
6176   /* Look for the initial prefix match, if there is one. */
6177   if( pRe->nInit ){
6178     unsigned char x = pRe->zInit[0];
6179     while( in.i+pRe->nInit<=in.mx 
6180      && (zIn[in.i]!=x ||
6181          strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
6182     ){
6183       in.i++;
6184     }
6185     if( in.i+pRe->nInit>in.mx ) return 0;
6186   }
6187
6188   if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
6189     pToFree = 0;
6190     aStateSet[0].aState = aSpace;
6191   }else{
6192     pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
6193     if( pToFree==0 ) return -1;
6194     aStateSet[0].aState = pToFree;
6195   }
6196   aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
6197   pNext = &aStateSet[1];
6198   pNext->nState = 0;
6199   re_add_state(pNext, 0);
6200   while( c!=RE_EOF && pNext->nState>0 ){
6201     cPrev = c;
6202     c = pRe->xNextChar(&in);
6203     pThis = pNext;
6204     pNext = &aStateSet[iSwap];
6205     iSwap = 1 - iSwap;
6206     pNext->nState = 0;
6207     for(i=0; i<pThis->nState; i++){
6208       int x = pThis->aState[i];
6209       switch( pRe->aOp[x] ){
6210         case RE_OP_MATCH: {
6211           if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
6212           break;
6213         }
6214         case RE_OP_ANY: {
6215           if( c!=0 ) re_add_state(pNext, x+1);
6216           break;
6217         }
6218         case RE_OP_WORD: {
6219           if( re_word_char(c) ) re_add_state(pNext, x+1);
6220           break;
6221         }
6222         case RE_OP_NOTWORD: {
6223           if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
6224           break;
6225         }
6226         case RE_OP_DIGIT: {
6227           if( re_digit_char(c) ) re_add_state(pNext, x+1);
6228           break;
6229         }
6230         case RE_OP_NOTDIGIT: {
6231           if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
6232           break;
6233         }
6234         case RE_OP_SPACE: {
6235           if( re_space_char(c) ) re_add_state(pNext, x+1);
6236           break;
6237         }
6238         case RE_OP_NOTSPACE: {
6239           if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
6240           break;
6241         }
6242         case RE_OP_BOUNDARY: {
6243           if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
6244           break;
6245         }
6246         case RE_OP_ANYSTAR: {
6247           re_add_state(pNext, x);
6248           re_add_state(pThis, x+1);
6249           break;
6250         }
6251         case RE_OP_FORK: {
6252           re_add_state(pThis, x+pRe->aArg[x]);
6253           re_add_state(pThis, x+1);
6254           break;
6255         }
6256         case RE_OP_GOTO: {
6257           re_add_state(pThis, x+pRe->aArg[x]);
6258           break;
6259         }
6260         case RE_OP_ACCEPT: {
6261           rc = 1;
6262           goto re_match_end;
6263         }
6264         case RE_OP_CC_EXC: {
6265           if( c==0 ) break;
6266           /* fall-through */ goto re_op_cc_inc;
6267         }
6268         case RE_OP_CC_INC: re_op_cc_inc: {
6269           int j = 1;
6270           int n = pRe->aArg[x];
6271           int hit = 0;
6272           for(j=1; j>0 && j<n; j++){
6273             if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
6274               if( pRe->aArg[x+j]==c ){
6275                 hit = 1;
6276                 j = -1;
6277               }
6278             }else{
6279               if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
6280                 hit = 1;
6281                 j = -1;
6282               }else{
6283                 j++;
6284               }
6285             }
6286           }
6287           if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
6288           if( hit ) re_add_state(pNext, x+n);
6289           break;
6290         }
6291       }
6292     }
6293   }
6294   for(i=0; i<pNext->nState; i++){
6295     if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
6296   }
6297 re_match_end:
6298   sqlite3_free(pToFree);
6299   return rc;
6300 }
6301
6302 /* Resize the opcode and argument arrays for an RE under construction.
6303 */
6304 static int re_resize(ReCompiled *p, int N){
6305   char *aOp;
6306   int *aArg;
6307   aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6308   if( aOp==0 ) return 1;
6309   p->aOp = aOp;
6310   aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6311   if( aArg==0 ) return 1;
6312   p->aArg = aArg;
6313   p->nAlloc = N;
6314   return 0;
6315 }
6316
6317 /* Insert a new opcode and argument into an RE under construction.  The
6318 ** insertion point is just prior to existing opcode iBefore.
6319 */
6320 static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
6321   int i;
6322   if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
6323   for(i=p->nState; i>iBefore; i--){
6324     p->aOp[i] = p->aOp[i-1];
6325     p->aArg[i] = p->aArg[i-1];
6326   }
6327   p->nState++;
6328   p->aOp[iBefore] = (char)op;
6329   p->aArg[iBefore] = arg;
6330   return iBefore;
6331 }
6332
6333 /* Append a new opcode and argument to the end of the RE under construction.
6334 */
6335 static int re_append(ReCompiled *p, int op, int arg){
6336   return re_insert(p, p->nState, op, arg);
6337 }
6338
6339 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6340 ** under construction.
6341 */
6342 static void re_copy(ReCompiled *p, int iStart, int N){
6343   if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6344   memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6345   memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6346   p->nState += N;
6347 }
6348
6349 /* Return true if c is a hexadecimal digit character:  [0-9a-fA-F]
6350 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c).  If
6351 ** c is not a hex digit *pV is unchanged.
6352 */
6353 static int re_hex(int c, int *pV){
6354   if( c>='0' && c<='9' ){
6355     c -= '0';
6356   }else if( c>='a' && c<='f' ){
6357     c -= 'a' - 10;
6358   }else if( c>='A' && c<='F' ){
6359     c -= 'A' - 10;
6360   }else{
6361     return 0;
6362   }
6363   *pV = (*pV)*16 + (c & 0xff);
6364   return 1;
6365 }
6366
6367 /* A backslash character has been seen, read the next character and
6368 ** return its interpretation.
6369 */
6370 static unsigned re_esc_char(ReCompiled *p){
6371   static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
6372   static const char zTrans[] = "\a\f\n\r\t\v";
6373   int i, v = 0;
6374   char c;
6375   if( p->sIn.i>=p->sIn.mx ) return 0;
6376   c = p->sIn.z[p->sIn.i];
6377   if( c=='u' && p->sIn.i+4<p->sIn.mx ){
6378     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6379     if( re_hex(zIn[1],&v)
6380      && re_hex(zIn[2],&v)
6381      && re_hex(zIn[3],&v)
6382      && re_hex(zIn[4],&v)
6383     ){
6384       p->sIn.i += 5;
6385       return v;
6386     }
6387   }
6388   if( c=='x' && p->sIn.i+2<p->sIn.mx ){
6389     const unsigned char *zIn = p->sIn.z + p->sIn.i;
6390     if( re_hex(zIn[1],&v)
6391      && re_hex(zIn[2],&v)
6392     ){
6393       p->sIn.i += 3;
6394       return v;
6395     }
6396   }
6397   for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
6398   if( zEsc[i] ){
6399     if( i<6 ) c = zTrans[i];
6400     p->sIn.i++;
6401   }else{
6402     p->zErr = "unknown \\ escape";
6403   }
6404   return c;
6405 }
6406
6407 /* Forward declaration */
6408 static const char *re_subcompile_string(ReCompiled*);
6409
6410 /* Peek at the next byte of input */
6411 static unsigned char rePeek(ReCompiled *p){
6412   return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
6413 }
6414
6415 /* Compile RE text into a sequence of opcodes.  Continue up to the
6416 ** first unmatched ")" character, then return.  If an error is found,
6417 ** return a pointer to the error message string.
6418 */
6419 static const char *re_subcompile_re(ReCompiled *p){
6420   const char *zErr;
6421   int iStart, iEnd, iGoto;
6422   iStart = p->nState;
6423   zErr = re_subcompile_string(p);
6424   if( zErr ) return zErr;
6425   while( rePeek(p)=='|' ){
6426     iEnd = p->nState;
6427     re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
6428     iGoto = re_append(p, RE_OP_GOTO, 0);
6429     p->sIn.i++;
6430     zErr = re_subcompile_string(p);
6431     if( zErr ) return zErr;
6432     p->aArg[iGoto] = p->nState - iGoto;
6433   }
6434   return 0;
6435 }
6436
6437 /* Compile an element of regular expression text (anything that can be
6438 ** an operand to the "|" operator).  Return NULL on success or a pointer
6439 ** to the error message if there is a problem.
6440 */
6441 static const char *re_subcompile_string(ReCompiled *p){
6442   int iPrev = -1;
6443   int iStart;
6444   unsigned c;
6445   const char *zErr;
6446   while( (c = p->xNextChar(&p->sIn))!=0 ){
6447     iStart = p->nState;
6448     switch( c ){
6449       case '|':
6450       case '$':
6451       case ')': {
6452         p->sIn.i--;
6453         return 0;
6454       }
6455       case '(': {
6456         zErr = re_subcompile_re(p);
6457         if( zErr ) return zErr;
6458         if( rePeek(p)!=')' ) return "unmatched '('";
6459         p->sIn.i++;
6460         break;
6461       }
6462       case '.': {
6463         if( rePeek(p)=='*' ){
6464           re_append(p, RE_OP_ANYSTAR, 0);
6465           p->sIn.i++;
6466         }else{
6467           re_append(p, RE_OP_ANY, 0);
6468         }
6469         break;
6470       }
6471       case '*': {
6472         if( iPrev<0 ) return "'*' without operand";
6473         re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
6474         re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
6475         break;
6476       }
6477       case '+': {
6478         if( iPrev<0 ) return "'+' without operand";
6479         re_append(p, RE_OP_FORK, iPrev - p->nState);
6480         break;
6481       }
6482       case '?': {
6483         if( iPrev<0 ) return "'?' without operand";
6484         re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
6485         break;
6486       }
6487       case '{': {
6488         int m = 0, n = 0;
6489         int sz, j;
6490         if( iPrev<0 ) return "'{m,n}' without operand";
6491         while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
6492         n = m;
6493         if( c==',' ){
6494           p->sIn.i++;
6495           n = 0;
6496           while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
6497         }
6498         if( c!='}' ) return "unmatched '{'";
6499         if( n>0 && n<m ) return "n less than m in '{m,n}'";
6500         p->sIn.i++;
6501         sz = p->nState - iPrev;
6502         if( m==0 ){
6503           if( n==0 ) return "both m and n are zero in '{m,n}'";
6504           re_insert(p, iPrev, RE_OP_FORK, sz+1);
6505           n--;
6506         }else{
6507           for(j=1; j<m; j++) re_copy(p, iPrev, sz);
6508         }
6509         for(j=m; j<n; j++){
6510           re_append(p, RE_OP_FORK, sz+1);
6511           re_copy(p, iPrev, sz);
6512         }
6513         if( n==0 && m>0 ){
6514           re_append(p, RE_OP_FORK, -sz);
6515         }
6516         break;
6517       }
6518       case '[': {
6519         int iFirst = p->nState;
6520         if( rePeek(p)=='^' ){
6521           re_append(p, RE_OP_CC_EXC, 0);
6522           p->sIn.i++;
6523         }else{
6524           re_append(p, RE_OP_CC_INC, 0);
6525         }
6526         while( (c = p->xNextChar(&p->sIn))!=0 ){
6527           if( c=='[' && rePeek(p)==':' ){
6528             return "POSIX character classes not supported";
6529           }
6530           if( c=='\\' ) c = re_esc_char(p);
6531           if( rePeek(p)=='-' ){
6532             re_append(p, RE_OP_CC_RANGE, c);
6533             p->sIn.i++;
6534             c = p->xNextChar(&p->sIn);
6535             if( c=='\\' ) c = re_esc_char(p);
6536             re_append(p, RE_OP_CC_RANGE, c);
6537           }else{
6538             re_append(p, RE_OP_CC_VALUE, c);
6539           }
6540           if( rePeek(p)==']' ){ p->sIn.i++; break; }
6541         }
6542         if( c==0 ) return "unclosed '['";
6543         p->aArg[iFirst] = p->nState - iFirst;
6544         break;
6545       }
6546       case '\\': {
6547         int specialOp = 0;
6548         switch( rePeek(p) ){
6549           case 'b': specialOp = RE_OP_BOUNDARY;   break;
6550           case 'd': specialOp = RE_OP_DIGIT;      break;
6551           case 'D': specialOp = RE_OP_NOTDIGIT;   break;
6552           case 's': specialOp = RE_OP_SPACE;      break;
6553           case 'S': specialOp = RE_OP_NOTSPACE;   break;
6554           case 'w': specialOp = RE_OP_WORD;       break;
6555           case 'W': specialOp = RE_OP_NOTWORD;    break;
6556         }
6557         if( specialOp ){
6558           p->sIn.i++;
6559           re_append(p, specialOp, 0);
6560         }else{
6561           c = re_esc_char(p);
6562           re_append(p, RE_OP_MATCH, c);
6563         }
6564         break;
6565       }
6566       default: {
6567         re_append(p, RE_OP_MATCH, c);
6568         break;
6569       }
6570     }
6571     iPrev = iStart;
6572   }
6573   return 0;
6574 }
6575
6576 /* Free and reclaim all the memory used by a previously compiled
6577 ** regular expression.  Applications should invoke this routine once
6578 ** for every call to re_compile() to avoid memory leaks.
6579 */
6580 static void re_free(ReCompiled *pRe){
6581   if( pRe ){
6582     sqlite3_free(pRe->aOp);
6583     sqlite3_free(pRe->aArg);
6584     sqlite3_free(pRe);
6585   }
6586 }
6587
6588 /*
6589 ** Compile a textual regular expression in zIn[] into a compiled regular
6590 ** expression suitable for us by re_match() and return a pointer to the
6591 ** compiled regular expression in *ppRe.  Return NULL on success or an
6592 ** error message if something goes wrong.
6593 */
6594 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
6595   ReCompiled *pRe;
6596   const char *zErr;
6597   int i, j;
6598
6599   *ppRe = 0;
6600   pRe = sqlite3_malloc( sizeof(*pRe) );
6601   if( pRe==0 ){
6602     return "out of memory";
6603   }
6604   memset(pRe, 0, sizeof(*pRe));
6605   pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
6606   if( re_resize(pRe, 30) ){
6607     re_free(pRe);
6608     return "out of memory";
6609   }
6610   if( zIn[0]=='^' ){
6611     zIn++;
6612   }else{
6613     re_append(pRe, RE_OP_ANYSTAR, 0);
6614   }
6615   pRe->sIn.z = (unsigned char*)zIn;
6616   pRe->sIn.i = 0;
6617   pRe->sIn.mx = (int)strlen(zIn);
6618   zErr = re_subcompile_re(pRe);
6619   if( zErr ){
6620     re_free(pRe);
6621     return zErr;
6622   }
6623   if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
6624     re_append(pRe, RE_OP_MATCH, RE_EOF);
6625     re_append(pRe, RE_OP_ACCEPT, 0);
6626     *ppRe = pRe;
6627   }else if( pRe->sIn.i>=pRe->sIn.mx ){
6628     re_append(pRe, RE_OP_ACCEPT, 0);
6629     *ppRe = pRe;
6630   }else{
6631     re_free(pRe);
6632     return "unrecognized character";
6633   }
6634
6635   /* The following is a performance optimization.  If the regex begins with
6636   ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6637   ** one or more matching characters, enter those matching characters into
6638   ** zInit[].  The re_match() routine can then search ahead in the input 
6639   ** string looking for the initial match without having to run the whole
6640   ** regex engine over the string.  Do not worry able trying to match
6641   ** unicode characters beyond plane 0 - those are very rare and this is
6642   ** just an optimization. */
6643   if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
6644     for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
6645       unsigned x = pRe->aArg[i];
6646       if( x<=127 ){
6647         pRe->zInit[j++] = (unsigned char)x;
6648       }else if( x<=0xfff ){
6649         pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
6650         pRe->zInit[j++] = 0x80 | (x&0x3f);
6651       }else if( x<=0xffff ){
6652         pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12));
6653         pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
6654         pRe->zInit[j++] = 0x80 | (x&0x3f);
6655       }else{
6656         break;
6657       }
6658     }
6659     if( j>0 && pRe->zInit[j-1]==0 ) j--;
6660     pRe->nInit = j;
6661   }
6662   return pRe->zErr;
6663 }
6664
6665 /*
6666 ** Implementation of the regexp() SQL function.  This function implements
6667 ** the build-in REGEXP operator.  The first argument to the function is the
6668 ** pattern and the second argument is the string.  So, the SQL statements:
6669 **
6670 **       A REGEXP B
6671 **
6672 ** is implemented as regexp(B,A).
6673 */
6674 static void re_sql_func(
6675   sqlite3_context *context,
6676   int argc,
6677   sqlite3_value **argv
6678 ){
6679   ReCompiled *pRe;          /* Compiled regular expression */
6680   const char *zPattern;     /* The regular expression */
6681   const unsigned char *zStr;/* String being searched */
6682   const char *zErr;         /* Compile error message */
6683   int setAux = 0;           /* True to invoke sqlite3_set_auxdata() */
6684
6685   (void)argc;  /* Unused */
6686   pRe = sqlite3_get_auxdata(context, 0);
6687   if( pRe==0 ){
6688     zPattern = (const char*)sqlite3_value_text(argv[0]);
6689     if( zPattern==0 ) return;
6690     zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
6691     if( zErr ){
6692       re_free(pRe);
6693       sqlite3_result_error(context, zErr, -1);
6694       return;
6695     }
6696     if( pRe==0 ){
6697       sqlite3_result_error_nomem(context);
6698       return;
6699     }
6700     setAux = 1;
6701   }
6702   zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
6703   if( zStr!=0 ){
6704     sqlite3_result_int(context, re_match(pRe, zStr, -1));
6705   }
6706   if( setAux ){
6707     sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
6708   }
6709 }
6710
6711 /*
6712 ** Invoke this routine to register the regexp() function with the
6713 ** SQLite database connection.
6714 */
6715 #ifdef _WIN32
6716
6717 #endif
6718 int sqlite3_regexp_init(
6719   sqlite3 *db, 
6720   char **pzErrMsg, 
6721   const sqlite3_api_routines *pApi
6722 ){
6723   int rc = SQLITE_OK;
6724   SQLITE_EXTENSION_INIT2(pApi);
6725   (void)pzErrMsg;  /* Unused */
6726   rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6727                                0, re_sql_func, 0, 0);
6728   if( rc==SQLITE_OK ){
6729     /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6730     ** of regexp(PATTERN,STRING). */
6731     rc = sqlite3_create_function(db, "regexpi", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6732                                  (void*)db, re_sql_func, 0, 0);
6733   }
6734   return rc;
6735 }
6736
6737 /************************* End ../ext/misc/regexp.c ********************/
6738 #ifdef SQLITE_HAVE_ZLIB
6739 /************************* Begin ../ext/misc/zipfile.c ******************/
6740 /*
6741 ** 2017-12-26
6742 **
6743 ** The author disclaims copyright to this source code.  In place of
6744 ** a legal notice, here is a blessing:
6745 **
6746 **    May you do good and not evil.
6747 **    May you find forgiveness for yourself and forgive others.
6748 **    May you share freely, never taking more than you give.
6749 **
6750 ******************************************************************************
6751 **
6752 ** This file implements a virtual table for reading and writing ZIP archive
6753 ** files.
6754 **
6755 ** Usage example:
6756 **
6757 **     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
6758 **
6759 ** Current limitations:
6760 **
6761 **    *  No support for encryption
6762 **    *  No support for ZIP archives spanning multiple files
6763 **    *  No support for zip64 extensions
6764 **    *  Only the "inflate/deflate" (zlib) compression method is supported
6765 */
6766 /* #include "sqlite3ext.h" */
6767 SQLITE_EXTENSION_INIT1
6768 #include <stdio.h>
6769 #include <string.h>
6770 #include <assert.h>
6771
6772 #include <zlib.h>
6773
6774 #ifndef SQLITE_OMIT_VIRTUALTABLE
6775
6776 #ifndef SQLITE_AMALGAMATION
6777
6778 #ifndef UINT32_TYPE
6779 # ifdef HAVE_UINT32_T
6780 #  define UINT32_TYPE uint32_t
6781 # else
6782 #  define UINT32_TYPE unsigned int
6783 # endif
6784 #endif
6785 #ifndef UINT16_TYPE
6786 # ifdef HAVE_UINT16_T
6787 #  define UINT16_TYPE uint16_t
6788 # else
6789 #  define UINT16_TYPE unsigned short int
6790 # endif
6791 #endif
6792 /* typedef sqlite3_int64 i64; */
6793 /* typedef unsigned char u8; */
6794 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
6795 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
6796 #define MIN(a,b) ((a)<(b) ? (a) : (b))
6797
6798 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
6799 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
6800 #endif
6801 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
6802 # define ALWAYS(X)      (1)
6803 # define NEVER(X)       (0)
6804 #elif !defined(NDEBUG)
6805 # define ALWAYS(X)      ((X)?1:(assert(0),0))
6806 # define NEVER(X)       ((X)?(assert(0),1):0)
6807 #else
6808 # define ALWAYS(X)      (X)
6809 # define NEVER(X)       (X)
6810 #endif
6811
6812 #endif   /* SQLITE_AMALGAMATION */
6813
6814 /*
6815 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
6816 **
6817 ** In some ways it would be better to obtain these values from system 
6818 ** header files. But, the dependency is undesirable and (a) these
6819 ** have been stable for decades, (b) the values are part of POSIX and
6820 ** are also made explicit in [man stat], and (c) are part of the 
6821 ** file format for zip archives.
6822 */
6823 #ifndef S_IFDIR
6824 # define S_IFDIR 0040000
6825 #endif
6826 #ifndef S_IFREG
6827 # define S_IFREG 0100000
6828 #endif
6829 #ifndef S_IFLNK
6830 # define S_IFLNK 0120000
6831 #endif
6832
6833 static const char ZIPFILE_SCHEMA[] = 
6834   "CREATE TABLE y("
6835     "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
6836     "mode,"              /* 1: POSIX mode for file */
6837     "mtime,"             /* 2: Last modification time (secs since 1970)*/
6838     "sz,"                /* 3: Size of object */
6839     "rawdata,"           /* 4: Raw data */
6840     "data,"              /* 5: Uncompressed data */
6841     "method,"            /* 6: Compression method (integer) */
6842     "z HIDDEN"           /* 7: Name of zip file */
6843   ") WITHOUT ROWID;";
6844
6845 #define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
6846 #define ZIPFILE_BUFFER_SIZE (64*1024)
6847
6848
6849 /*
6850 ** Magic numbers used to read and write zip files.
6851 **
6852 ** ZIPFILE_NEWENTRY_MADEBY:
6853 **   Use this value for the "version-made-by" field in new zip file
6854 **   entries. The upper byte indicates "unix", and the lower byte 
6855 **   indicates that the zip file matches pkzip specification 3.0. 
6856 **   This is what info-zip seems to do.
6857 **
6858 ** ZIPFILE_NEWENTRY_REQUIRED:
6859 **   Value for "version-required-to-extract" field of new entries.
6860 **   Version 2.0 is required to support folders and deflate compression.
6861 **
6862 ** ZIPFILE_NEWENTRY_FLAGS:
6863 **   Value for "general-purpose-bit-flags" field of new entries. Bit
6864 **   11 means "utf-8 filename and comment".
6865 **
6866 ** ZIPFILE_SIGNATURE_CDS:
6867 **   First 4 bytes of a valid CDS record.
6868 **
6869 ** ZIPFILE_SIGNATURE_LFH:
6870 **   First 4 bytes of a valid LFH record.
6871 **
6872 ** ZIPFILE_SIGNATURE_EOCD
6873 **   First 4 bytes of a valid EOCD record.
6874 */
6875 #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
6876 #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
6877 #define ZIPFILE_NEWENTRY_REQUIRED 20
6878 #define ZIPFILE_NEWENTRY_FLAGS    0x800
6879 #define ZIPFILE_SIGNATURE_CDS     0x02014b50
6880 #define ZIPFILE_SIGNATURE_LFH     0x04034b50
6881 #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
6882
6883 /*
6884 ** The sizes of the fixed-size part of each of the three main data 
6885 ** structures in a zip archive.
6886 */
6887 #define ZIPFILE_LFH_FIXED_SZ      30
6888 #define ZIPFILE_EOCD_FIXED_SZ     22
6889 #define ZIPFILE_CDS_FIXED_SZ      46
6890
6891 /*
6892 *** 4.3.16  End of central directory record:
6893 ***
6894 ***   end of central dir signature    4 bytes  (0x06054b50)
6895 ***   number of this disk             2 bytes
6896 ***   number of the disk with the
6897 ***   start of the central directory  2 bytes
6898 ***   total number of entries in the
6899 ***   central directory on this disk  2 bytes
6900 ***   total number of entries in
6901 ***   the central directory           2 bytes
6902 ***   size of the central directory   4 bytes
6903 ***   offset of start of central
6904 ***   directory with respect to
6905 ***   the starting disk number        4 bytes
6906 ***   .ZIP file comment length        2 bytes
6907 ***   .ZIP file comment       (variable size)
6908 */
6909 typedef struct ZipfileEOCD ZipfileEOCD;
6910 struct ZipfileEOCD {
6911   u16 iDisk;
6912   u16 iFirstDisk;
6913   u16 nEntry;
6914   u16 nEntryTotal;
6915   u32 nSize;
6916   u32 iOffset;
6917 };
6918
6919 /*
6920 *** 4.3.12  Central directory structure:
6921 ***
6922 *** ...
6923 ***
6924 ***   central file header signature   4 bytes  (0x02014b50)
6925 ***   version made by                 2 bytes
6926 ***   version needed to extract       2 bytes
6927 ***   general purpose bit flag        2 bytes
6928 ***   compression method              2 bytes
6929 ***   last mod file time              2 bytes
6930 ***   last mod file date              2 bytes
6931 ***   crc-32                          4 bytes
6932 ***   compressed size                 4 bytes
6933 ***   uncompressed size               4 bytes
6934 ***   file name length                2 bytes
6935 ***   extra field length              2 bytes
6936 ***   file comment length             2 bytes
6937 ***   disk number start               2 bytes
6938 ***   internal file attributes        2 bytes
6939 ***   external file attributes        4 bytes
6940 ***   relative offset of local header 4 bytes
6941 */
6942 typedef struct ZipfileCDS ZipfileCDS;
6943 struct ZipfileCDS {
6944   u16 iVersionMadeBy;
6945   u16 iVersionExtract;
6946   u16 flags;
6947   u16 iCompression;
6948   u16 mTime;
6949   u16 mDate;
6950   u32 crc32;
6951   u32 szCompressed;
6952   u32 szUncompressed;
6953   u16 nFile;
6954   u16 nExtra;
6955   u16 nComment;
6956   u16 iDiskStart;
6957   u16 iInternalAttr;
6958   u32 iExternalAttr;
6959   u32 iOffset;
6960   char *zFile;                    /* Filename (sqlite3_malloc()) */
6961 };
6962
6963 /*
6964 *** 4.3.7  Local file header:
6965 ***
6966 ***   local file header signature     4 bytes  (0x04034b50)
6967 ***   version needed to extract       2 bytes
6968 ***   general purpose bit flag        2 bytes
6969 ***   compression method              2 bytes
6970 ***   last mod file time              2 bytes
6971 ***   last mod file date              2 bytes
6972 ***   crc-32                          4 bytes
6973 ***   compressed size                 4 bytes
6974 ***   uncompressed size               4 bytes
6975 ***   file name length                2 bytes
6976 ***   extra field length              2 bytes
6977 ***   
6978 */
6979 typedef struct ZipfileLFH ZipfileLFH;
6980 struct ZipfileLFH {
6981   u16 iVersionExtract;
6982   u16 flags;
6983   u16 iCompression;
6984   u16 mTime;
6985   u16 mDate;
6986   u32 crc32;
6987   u32 szCompressed;
6988   u32 szUncompressed;
6989   u16 nFile;
6990   u16 nExtra;
6991 };
6992
6993 typedef struct ZipfileEntry ZipfileEntry;
6994 struct ZipfileEntry {
6995   ZipfileCDS cds;            /* Parsed CDS record */
6996   u32 mUnixTime;             /* Modification time, in UNIX format */
6997   u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
6998   i64 iDataOff;              /* Offset to data in file (if aData==0) */
6999   u8 *aData;                 /* cds.szCompressed bytes of compressed data */
7000   ZipfileEntry *pNext;       /* Next element in in-memory CDS */
7001 };
7002
7003 /* 
7004 ** Cursor type for zipfile tables.
7005 */
7006 typedef struct ZipfileCsr ZipfileCsr;
7007 struct ZipfileCsr {
7008   sqlite3_vtab_cursor base;  /* Base class - must be first */
7009   i64 iId;                   /* Cursor ID */
7010   u8 bEof;                   /* True when at EOF */
7011   u8 bNoop;                  /* If next xNext() call is no-op */
7012
7013   /* Used outside of write transactions */
7014   FILE *pFile;               /* Zip file */
7015   i64 iNextOff;              /* Offset of next record in central directory */
7016   ZipfileEOCD eocd;          /* Parse of central directory record */
7017
7018   ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
7019   ZipfileEntry *pCurrent;    /* Current entry */
7020   ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
7021 };
7022
7023 typedef struct ZipfileTab ZipfileTab;
7024 struct ZipfileTab {
7025   sqlite3_vtab base;         /* Base class - must be first */
7026   char *zFile;               /* Zip file this table accesses (may be NULL) */
7027   sqlite3 *db;               /* Host database connection */
7028   u8 *aBuffer;               /* Temporary buffer used for various tasks */
7029
7030   ZipfileCsr *pCsrList;      /* List of cursors */
7031   i64 iNextCsrid;
7032
7033   /* The following are used by write transactions only */
7034   ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
7035   ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
7036   FILE *pWriteFd;            /* File handle open on zip archive */
7037   i64 szCurrent;             /* Current size of zip archive */
7038   i64 szOrig;                /* Size of archive at start of transaction */
7039 };
7040
7041 /*
7042 ** Set the error message contained in context ctx to the results of
7043 ** vprintf(zFmt, ...).
7044 */
7045 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
7046   char *zMsg = 0;
7047   va_list ap;
7048   va_start(ap, zFmt);
7049   zMsg = sqlite3_vmprintf(zFmt, ap);
7050   sqlite3_result_error(ctx, zMsg, -1);
7051   sqlite3_free(zMsg);
7052   va_end(ap);
7053 }
7054
7055 /*
7056 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
7057 ** is not quoted, do nothing.
7058 */
7059 static void zipfileDequote(char *zIn){
7060   char q = zIn[0];
7061   if( q=='"' || q=='\'' || q=='`' || q=='[' ){
7062     int iIn = 1;
7063     int iOut = 0;
7064     if( q=='[' ) q = ']';
7065     while( ALWAYS(zIn[iIn]) ){
7066       char c = zIn[iIn++];
7067       if( c==q && zIn[iIn++]!=q ) break;
7068       zIn[iOut++] = c;
7069     }
7070     zIn[iOut] = '\0';
7071   }
7072 }
7073
7074 /*
7075 ** Construct a new ZipfileTab virtual table object.
7076 ** 
7077 **   argv[0]   -> module name  ("zipfile")
7078 **   argv[1]   -> database name
7079 **   argv[2]   -> table name
7080 **   argv[...] -> "column name" and other module argument fields.
7081 */
7082 static int zipfileConnect(
7083   sqlite3 *db,
7084   void *pAux,
7085   int argc, const char *const*argv,
7086   sqlite3_vtab **ppVtab,
7087   char **pzErr
7088 ){
7089   int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
7090   int nFile = 0;
7091   const char *zFile = 0;
7092   ZipfileTab *pNew = 0;
7093   int rc;
7094
7095   /* If the table name is not "zipfile", require that the argument be
7096   ** specified. This stops zipfile tables from being created as:
7097   **
7098   **   CREATE VIRTUAL TABLE zzz USING zipfile();
7099   **
7100   ** It does not prevent:
7101   **
7102   **   CREATE VIRTUAL TABLE zipfile USING zipfile();
7103   */
7104   assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
7105   if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
7106     *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
7107     return SQLITE_ERROR;
7108   }
7109
7110   if( argc>3 ){
7111     zFile = argv[3];
7112     nFile = (int)strlen(zFile)+1;
7113   }
7114
7115   rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
7116   if( rc==SQLITE_OK ){
7117     pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
7118     if( pNew==0 ) return SQLITE_NOMEM;
7119     memset(pNew, 0, nByte+nFile);
7120     pNew->db = db;
7121     pNew->aBuffer = (u8*)&pNew[1];
7122     if( zFile ){
7123       pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
7124       memcpy(pNew->zFile, zFile, nFile);
7125       zipfileDequote(pNew->zFile);
7126     }
7127   }
7128   sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
7129   *ppVtab = (sqlite3_vtab*)pNew;
7130   return rc;
7131 }
7132
7133 /*
7134 ** Free the ZipfileEntry structure indicated by the only argument.
7135 */
7136 static void zipfileEntryFree(ZipfileEntry *p){
7137   if( p ){
7138     sqlite3_free(p->cds.zFile);
7139     sqlite3_free(p);
7140   }
7141 }
7142
7143 /*
7144 ** Release resources that should be freed at the end of a write 
7145 ** transaction.
7146 */
7147 static void zipfileCleanupTransaction(ZipfileTab *pTab){
7148   ZipfileEntry *pEntry;
7149   ZipfileEntry *pNext;
7150
7151   if( pTab->pWriteFd ){
7152     fclose(pTab->pWriteFd);
7153     pTab->pWriteFd = 0;
7154   }
7155   for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
7156     pNext = pEntry->pNext;
7157     zipfileEntryFree(pEntry);
7158   }
7159   pTab->pFirstEntry = 0;
7160   pTab->pLastEntry = 0;
7161   pTab->szCurrent = 0;
7162   pTab->szOrig = 0;
7163 }
7164
7165 /*
7166 ** This method is the destructor for zipfile vtab objects.
7167 */
7168 static int zipfileDisconnect(sqlite3_vtab *pVtab){
7169   zipfileCleanupTransaction((ZipfileTab*)pVtab);
7170   sqlite3_free(pVtab);
7171   return SQLITE_OK;
7172 }
7173
7174 /*
7175 ** Constructor for a new ZipfileCsr object.
7176 */
7177 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
7178   ZipfileTab *pTab = (ZipfileTab*)p;
7179   ZipfileCsr *pCsr;
7180   pCsr = sqlite3_malloc(sizeof(*pCsr));
7181   *ppCsr = (sqlite3_vtab_cursor*)pCsr;
7182   if( pCsr==0 ){
7183     return SQLITE_NOMEM;
7184   }
7185   memset(pCsr, 0, sizeof(*pCsr));
7186   pCsr->iId = ++pTab->iNextCsrid;
7187   pCsr->pCsrNext = pTab->pCsrList;
7188   pTab->pCsrList = pCsr;
7189   return SQLITE_OK;
7190 }
7191
7192 /*
7193 ** Reset a cursor back to the state it was in when first returned
7194 ** by zipfileOpen().
7195 */
7196 static void zipfileResetCursor(ZipfileCsr *pCsr){
7197   ZipfileEntry *p;
7198   ZipfileEntry *pNext;
7199
7200   pCsr->bEof = 0;
7201   if( pCsr->pFile ){
7202     fclose(pCsr->pFile);
7203     pCsr->pFile = 0;
7204     zipfileEntryFree(pCsr->pCurrent);
7205     pCsr->pCurrent = 0;
7206   }
7207
7208   for(p=pCsr->pFreeEntry; p; p=pNext){
7209     pNext = p->pNext;
7210     zipfileEntryFree(p);
7211   }
7212 }
7213
7214 /*
7215 ** Destructor for an ZipfileCsr.
7216 */
7217 static int zipfileClose(sqlite3_vtab_cursor *cur){
7218   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7219   ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
7220   ZipfileCsr **pp;
7221   zipfileResetCursor(pCsr);
7222
7223   /* Remove this cursor from the ZipfileTab.pCsrList list. */
7224   for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
7225   *pp = pCsr->pCsrNext;
7226
7227   sqlite3_free(pCsr);
7228   return SQLITE_OK;
7229 }
7230
7231 /*
7232 ** Set the error message for the virtual table associated with cursor
7233 ** pCsr to the results of vprintf(zFmt, ...).
7234 */
7235 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
7236   va_list ap;
7237   va_start(ap, zFmt);
7238   sqlite3_free(pTab->base.zErrMsg);
7239   pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
7240   va_end(ap);
7241 }
7242 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
7243   va_list ap;
7244   va_start(ap, zFmt);
7245   sqlite3_free(pCsr->base.pVtab->zErrMsg);
7246   pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
7247   va_end(ap);
7248 }
7249
7250 /*
7251 ** Read nRead bytes of data from offset iOff of file pFile into buffer
7252 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
7253 ** otherwise. 
7254 **
7255 ** If an error does occur, output variable (*pzErrmsg) may be set to point
7256 ** to an English language error message. It is the responsibility of the
7257 ** caller to eventually free this buffer using
7258 ** sqlite3_free().
7259 */
7260 static int zipfileReadData(
7261   FILE *pFile,                    /* Read from this file */
7262   u8 *aRead,                      /* Read into this buffer */
7263   int nRead,                      /* Number of bytes to read */
7264   i64 iOff,                       /* Offset to read from */
7265   char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
7266 ){
7267   size_t n;
7268   fseek(pFile, (long)iOff, SEEK_SET);
7269   n = fread(aRead, 1, nRead, pFile);
7270   if( (int)n!=nRead ){
7271     *pzErrmsg = sqlite3_mprintf("error in fread()");
7272     return SQLITE_ERROR;
7273   }
7274   return SQLITE_OK;
7275 }
7276
7277 static int zipfileAppendData(
7278   ZipfileTab *pTab,
7279   const u8 *aWrite,
7280   int nWrite
7281 ){
7282   if( nWrite>0 ){
7283     size_t n = nWrite;
7284     fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
7285     n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
7286     if( (int)n!=nWrite ){
7287       pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
7288       return SQLITE_ERROR;
7289     }
7290     pTab->szCurrent += nWrite;
7291   }
7292   return SQLITE_OK;
7293 }
7294
7295 /*
7296 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
7297 */
7298 static u16 zipfileGetU16(const u8 *aBuf){
7299   return (aBuf[1] << 8) + aBuf[0];
7300 }
7301
7302 /*
7303 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
7304 */
7305 static u32 zipfileGetU32(const u8 *aBuf){
7306   if( aBuf==0 ) return 0;
7307   return ((u32)(aBuf[3]) << 24)
7308        + ((u32)(aBuf[2]) << 16)
7309        + ((u32)(aBuf[1]) <<  8)
7310        + ((u32)(aBuf[0]) <<  0);
7311 }
7312
7313 /*
7314 ** Write a 16-bit little endiate integer into buffer aBuf.
7315 */
7316 static void zipfilePutU16(u8 *aBuf, u16 val){
7317   aBuf[0] = val & 0xFF;
7318   aBuf[1] = (val>>8) & 0xFF;
7319 }
7320
7321 /*
7322 ** Write a 32-bit little endiate integer into buffer aBuf.
7323 */
7324 static void zipfilePutU32(u8 *aBuf, u32 val){
7325   aBuf[0] = val & 0xFF;
7326   aBuf[1] = (val>>8) & 0xFF;
7327   aBuf[2] = (val>>16) & 0xFF;
7328   aBuf[3] = (val>>24) & 0xFF;
7329 }
7330
7331 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
7332 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
7333
7334 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
7335 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
7336
7337 /*
7338 ** Magic numbers used to read CDS records.
7339 */
7340 #define ZIPFILE_CDS_NFILE_OFF        28
7341 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
7342
7343 /*
7344 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
7345 ** if the record is not well-formed, or SQLITE_OK otherwise.
7346 */
7347 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
7348   u8 *aRead = aBuf;
7349   u32 sig = zipfileRead32(aRead);
7350   int rc = SQLITE_OK;
7351   if( sig!=ZIPFILE_SIGNATURE_CDS ){
7352     rc = SQLITE_ERROR;
7353   }else{
7354     pCDS->iVersionMadeBy = zipfileRead16(aRead);
7355     pCDS->iVersionExtract = zipfileRead16(aRead);
7356     pCDS->flags = zipfileRead16(aRead);
7357     pCDS->iCompression = zipfileRead16(aRead);
7358     pCDS->mTime = zipfileRead16(aRead);
7359     pCDS->mDate = zipfileRead16(aRead);
7360     pCDS->crc32 = zipfileRead32(aRead);
7361     pCDS->szCompressed = zipfileRead32(aRead);
7362     pCDS->szUncompressed = zipfileRead32(aRead);
7363     assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7364     pCDS->nFile = zipfileRead16(aRead);
7365     pCDS->nExtra = zipfileRead16(aRead);
7366     pCDS->nComment = zipfileRead16(aRead);
7367     pCDS->iDiskStart = zipfileRead16(aRead);
7368     pCDS->iInternalAttr = zipfileRead16(aRead);
7369     pCDS->iExternalAttr = zipfileRead32(aRead);
7370     pCDS->iOffset = zipfileRead32(aRead);
7371     assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
7372   }
7373
7374   return rc;
7375 }
7376
7377 /*
7378 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
7379 ** if the record is not well-formed, or SQLITE_OK otherwise.
7380 */
7381 static int zipfileReadLFH(
7382   u8 *aBuffer,
7383   ZipfileLFH *pLFH
7384 ){
7385   u8 *aRead = aBuffer;
7386   int rc = SQLITE_OK;
7387
7388   u32 sig = zipfileRead32(aRead);
7389   if( sig!=ZIPFILE_SIGNATURE_LFH ){
7390     rc = SQLITE_ERROR;
7391   }else{
7392     pLFH->iVersionExtract = zipfileRead16(aRead);
7393     pLFH->flags = zipfileRead16(aRead);
7394     pLFH->iCompression = zipfileRead16(aRead);
7395     pLFH->mTime = zipfileRead16(aRead);
7396     pLFH->mDate = zipfileRead16(aRead);
7397     pLFH->crc32 = zipfileRead32(aRead);
7398     pLFH->szCompressed = zipfileRead32(aRead);
7399     pLFH->szUncompressed = zipfileRead32(aRead);
7400     pLFH->nFile = zipfileRead16(aRead);
7401     pLFH->nExtra = zipfileRead16(aRead);
7402   }
7403   return rc;
7404 }
7405
7406
7407 /*
7408 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
7409 ** Scan through this buffer to find an "extra-timestamp" field. If one
7410 ** exists, extract the 32-bit modification-timestamp from it and store
7411 ** the value in output parameter *pmTime.
7412 **
7413 ** Zero is returned if no extra-timestamp record could be found (and so
7414 ** *pmTime is left unchanged), or non-zero otherwise.
7415 **
7416 ** The general format of an extra field is:
7417 **
7418 **   Header ID    2 bytes
7419 **   Data Size    2 bytes
7420 **   Data         N bytes
7421 */
7422 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
7423   int ret = 0;
7424   u8 *p = aExtra;
7425   u8 *pEnd = &aExtra[nExtra];
7426
7427   while( p<pEnd ){
7428     u16 id = zipfileRead16(p);
7429     u16 nByte = zipfileRead16(p);
7430
7431     switch( id ){
7432       case ZIPFILE_EXTRA_TIMESTAMP: {
7433         u8 b = p[0];
7434         if( b & 0x01 ){     /* 0x01 -> modtime is present */
7435           *pmTime = zipfileGetU32(&p[1]);
7436           ret = 1;
7437         }
7438         break;
7439       }
7440     }
7441
7442     p += nByte;
7443   }
7444   return ret;
7445 }
7446
7447 /*
7448 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
7449 ** fields of the CDS structure passed as the only argument to a 32-bit
7450 ** UNIX seconds-since-the-epoch timestamp. Return the result.
7451 **
7452 ** "Standard" MS-DOS time format:
7453 **
7454 **   File modification time:
7455 **     Bits 00-04: seconds divided by 2
7456 **     Bits 05-10: minute
7457 **     Bits 11-15: hour
7458 **   File modification date:
7459 **     Bits 00-04: day
7460 **     Bits 05-08: month (1-12)
7461 **     Bits 09-15: years from 1980 
7462 **
7463 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
7464 */
7465 static u32 zipfileMtime(ZipfileCDS *pCDS){
7466   int Y,M,D,X1,X2,A,B,sec,min,hr;
7467   i64 JDsec;
7468   Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
7469   M = ((pCDS->mDate >> 5) & 0x0F);
7470   D = (pCDS->mDate & 0x1F);
7471   sec = (pCDS->mTime & 0x1F)*2;
7472   min = (pCDS->mTime >> 5) & 0x3F;
7473   hr = (pCDS->mTime >> 11) & 0x1F;
7474   if( M<=2 ){
7475     Y--;
7476     M += 12;
7477   }
7478   X1 = 36525*(Y+4716)/100;
7479   X2 = 306001*(M+1)/10000;
7480   A = Y/100;
7481   B = 2 - A + (A/4);
7482   JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
7483   return (u32)(JDsec - (i64)24405875*(i64)8640);
7484 }
7485
7486 /*
7487 ** The opposite of zipfileMtime(). This function populates the mTime and
7488 ** mDate fields of the CDS structure passed as the first argument according
7489 ** to the UNIX timestamp value passed as the second.
7490 */
7491 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
7492   /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
7493   i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
7494
7495   int A, B, C, D, E;
7496   int yr, mon, day;
7497   int hr, min, sec;
7498
7499   A = (int)((JD - 1867216.25)/36524.25);
7500   A = (int)(JD + 1 + A - (A/4));
7501   B = A + 1524;
7502   C = (int)((B - 122.1)/365.25);
7503   D = (36525*(C&32767))/100;
7504   E = (int)((B-D)/30.6001);
7505
7506   day = B - D - (int)(30.6001*E);
7507   mon = (E<14 ? E-1 : E-13);
7508   yr = mon>2 ? C-4716 : C-4715;
7509
7510   hr = (mUnixTime % (24*60*60)) / (60*60);
7511   min = (mUnixTime % (60*60)) / 60;
7512   sec = (mUnixTime % 60);
7513
7514   if( yr>=1980 ){
7515     pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
7516     pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
7517   }else{
7518     pCds->mDate = pCds->mTime = 0;
7519   }
7520
7521   assert( mUnixTime<315507600 
7522        || mUnixTime==zipfileMtime(pCds) 
7523        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
7524        /* || (mUnixTime % 2) */
7525   );
7526 }
7527
7528 /*
7529 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
7530 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
7531 ** then pFile is a file-handle open on a zip file. In either case, this
7532 ** function creates a ZipfileEntry object based on the zip archive entry
7533 ** for which the CDS record is at offset iOff.
7534 **
7535 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
7536 ** the new object. Otherwise, an SQLite error code is returned and the
7537 ** final value of (*ppEntry) undefined.
7538 */
7539 static int zipfileGetEntry(
7540   ZipfileTab *pTab,               /* Store any error message here */
7541   const u8 *aBlob,                /* Pointer to in-memory file image */
7542   int nBlob,                      /* Size of aBlob[] in bytes */
7543   FILE *pFile,                    /* If aBlob==0, read from this file */
7544   i64 iOff,                       /* Offset of CDS record */
7545   ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
7546 ){
7547   u8 *aRead;
7548   char **pzErr = &pTab->base.zErrMsg;
7549   int rc = SQLITE_OK;
7550
7551   if( aBlob==0 ){
7552     aRead = pTab->aBuffer;
7553     rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
7554   }else{
7555     aRead = (u8*)&aBlob[iOff];
7556   }
7557
7558   if( rc==SQLITE_OK ){
7559     sqlite3_int64 nAlloc;
7560     ZipfileEntry *pNew;
7561
7562     int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
7563     int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
7564     nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
7565
7566     nAlloc = sizeof(ZipfileEntry) + nExtra;
7567     if( aBlob ){
7568       nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
7569     }
7570
7571     pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
7572     if( pNew==0 ){
7573       rc = SQLITE_NOMEM;
7574     }else{
7575       memset(pNew, 0, sizeof(ZipfileEntry));
7576       rc = zipfileReadCDS(aRead, &pNew->cds);
7577       if( rc!=SQLITE_OK ){
7578         *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
7579       }else if( aBlob==0 ){
7580         rc = zipfileReadData(
7581             pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
7582         );
7583       }else{
7584         aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
7585       }
7586     }
7587
7588     if( rc==SQLITE_OK ){
7589       u32 *pt = &pNew->mUnixTime;
7590       pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
7591       pNew->aExtra = (u8*)&pNew[1];
7592       memcpy(pNew->aExtra, &aRead[nFile], nExtra);
7593       if( pNew->cds.zFile==0 ){
7594         rc = SQLITE_NOMEM;
7595       }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
7596         pNew->mUnixTime = zipfileMtime(&pNew->cds);
7597       }
7598     }
7599
7600     if( rc==SQLITE_OK ){
7601       static const int szFix = ZIPFILE_LFH_FIXED_SZ;
7602       ZipfileLFH lfh;
7603       if( pFile ){
7604         rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
7605       }else{
7606         aRead = (u8*)&aBlob[pNew->cds.iOffset];
7607       }
7608
7609       if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
7610       if( rc==SQLITE_OK ){
7611         pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
7612         pNew->iDataOff += lfh.nFile + lfh.nExtra;
7613         if( aBlob && pNew->cds.szCompressed ){
7614           pNew->aData = &pNew->aExtra[nExtra];
7615           memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
7616         }
7617       }else{
7618         *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
7619             (int)pNew->cds.iOffset
7620         );
7621       }
7622     }
7623
7624     if( rc!=SQLITE_OK ){
7625       zipfileEntryFree(pNew);
7626     }else{
7627       *ppEntry = pNew;
7628     }
7629   }
7630
7631   return rc;
7632 }
7633
7634 /*
7635 ** Advance an ZipfileCsr to its next row of output.
7636 */
7637 static int zipfileNext(sqlite3_vtab_cursor *cur){
7638   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7639   int rc = SQLITE_OK;
7640
7641   if( pCsr->pFile ){
7642     i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
7643     zipfileEntryFree(pCsr->pCurrent);
7644     pCsr->pCurrent = 0;
7645     if( pCsr->iNextOff>=iEof ){
7646       pCsr->bEof = 1;
7647     }else{
7648       ZipfileEntry *p = 0;
7649       ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
7650       rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
7651       if( rc==SQLITE_OK ){
7652         pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
7653         pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
7654       }
7655       pCsr->pCurrent = p;
7656     }
7657   }else{
7658     if( !pCsr->bNoop ){
7659       pCsr->pCurrent = pCsr->pCurrent->pNext;
7660     }
7661     if( pCsr->pCurrent==0 ){
7662       pCsr->bEof = 1;
7663     }
7664   }
7665
7666   pCsr->bNoop = 0;
7667   return rc;
7668 }
7669
7670 static void zipfileFree(void *p) { 
7671   sqlite3_free(p); 
7672 }
7673
7674 /*
7675 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
7676 ** size is nOut bytes. This function uncompresses the data and sets the
7677 ** return value in context pCtx to the result (a blob).
7678 **
7679 ** If an error occurs, an error code is left in pCtx instead.
7680 */
7681 static void zipfileInflate(
7682   sqlite3_context *pCtx,          /* Store result here */
7683   const u8 *aIn,                  /* Compressed data */
7684   int nIn,                        /* Size of buffer aIn[] in bytes */
7685   int nOut                        /* Expected output size */
7686 ){
7687   u8 *aRes = sqlite3_malloc(nOut);
7688   if( aRes==0 ){
7689     sqlite3_result_error_nomem(pCtx);
7690   }else{
7691     int err;
7692     z_stream str;
7693     memset(&str, 0, sizeof(str));
7694
7695     str.next_in = (Byte*)aIn;
7696     str.avail_in = nIn;
7697     str.next_out = (Byte*)aRes;
7698     str.avail_out = nOut;
7699
7700     err = inflateInit2(&str, -15);
7701     if( err!=Z_OK ){
7702       zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
7703     }else{
7704       err = inflate(&str, Z_NO_FLUSH);
7705       if( err!=Z_STREAM_END ){
7706         zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
7707       }else{
7708         sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
7709         aRes = 0;
7710       }
7711     }
7712     sqlite3_free(aRes);
7713     inflateEnd(&str);
7714   }
7715 }
7716
7717 /*
7718 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
7719 ** compresses it and sets (*ppOut) to point to a buffer containing the
7720 ** compressed data. The caller is responsible for eventually calling
7721 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
7722 ** is set to the size of buffer (*ppOut) in bytes.
7723 **
7724 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
7725 ** code is returned and an error message left in virtual-table handle
7726 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
7727 ** case.
7728 */
7729 static int zipfileDeflate(
7730   const u8 *aIn, int nIn,         /* Input */
7731   u8 **ppOut, int *pnOut,         /* Output */
7732   char **pzErr                    /* OUT: Error message */
7733 ){
7734   int rc = SQLITE_OK;
7735   sqlite3_int64 nAlloc;
7736   z_stream str;
7737   u8 *aOut;
7738
7739   memset(&str, 0, sizeof(str));
7740   str.next_in = (Bytef*)aIn;
7741   str.avail_in = nIn;
7742   deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
7743
7744   nAlloc = deflateBound(&str, nIn);
7745   aOut = (u8*)sqlite3_malloc64(nAlloc);
7746   if( aOut==0 ){
7747     rc = SQLITE_NOMEM;
7748   }else{
7749     int res;
7750     str.next_out = aOut;
7751     str.avail_out = nAlloc;
7752     res = deflate(&str, Z_FINISH);
7753     if( res==Z_STREAM_END ){
7754       *ppOut = aOut;
7755       *pnOut = (int)str.total_out;
7756     }else{
7757       sqlite3_free(aOut);
7758       *pzErr = sqlite3_mprintf("zipfile: deflate() error");
7759       rc = SQLITE_ERROR;
7760     }
7761     deflateEnd(&str);
7762   }
7763
7764   return rc;
7765 }
7766
7767
7768 /*
7769 ** Return values of columns for the row at which the series_cursor
7770 ** is currently pointing.
7771 */
7772 static int zipfileColumn(
7773   sqlite3_vtab_cursor *cur,   /* The cursor */
7774   sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
7775   int i                       /* Which column to return */
7776 ){
7777   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7778   ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
7779   int rc = SQLITE_OK;
7780   switch( i ){
7781     case 0:   /* name */
7782       sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
7783       break;
7784     case 1:   /* mode */
7785       /* TODO: Whether or not the following is correct surely depends on
7786       ** the platform on which the archive was created.  */
7787       sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
7788       break;
7789     case 2: { /* mtime */
7790       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
7791       break;
7792     }
7793     case 3: { /* sz */
7794       if( sqlite3_vtab_nochange(ctx)==0 ){
7795         sqlite3_result_int64(ctx, pCDS->szUncompressed);
7796       }
7797       break;
7798     }
7799     case 4:   /* rawdata */
7800       if( sqlite3_vtab_nochange(ctx) ) break;
7801     case 5: { /* data */
7802       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
7803         int sz = pCDS->szCompressed;
7804         int szFinal = pCDS->szUncompressed;
7805         if( szFinal>0 ){
7806           u8 *aBuf;
7807           u8 *aFree = 0;
7808           if( pCsr->pCurrent->aData ){
7809             aBuf = pCsr->pCurrent->aData;
7810           }else{
7811             aBuf = aFree = sqlite3_malloc64(sz);
7812             if( aBuf==0 ){
7813               rc = SQLITE_NOMEM;
7814             }else{
7815               FILE *pFile = pCsr->pFile;
7816               if( pFile==0 ){
7817                 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
7818               }
7819               rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
7820                   &pCsr->base.pVtab->zErrMsg
7821               );
7822             }
7823           }
7824           if( rc==SQLITE_OK ){
7825             if( i==5 && pCDS->iCompression ){
7826               zipfileInflate(ctx, aBuf, sz, szFinal);
7827             }else{
7828               sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
7829             }
7830           }
7831           sqlite3_free(aFree);
7832         }else{
7833           /* Figure out if this is a directory or a zero-sized file. Consider
7834           ** it to be a directory either if the mode suggests so, or if
7835           ** the final character in the name is '/'.  */
7836           u32 mode = pCDS->iExternalAttr >> 16;
7837           if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
7838             sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
7839           }
7840         }
7841       }
7842       break;
7843     }
7844     case 6:   /* method */
7845       sqlite3_result_int(ctx, pCDS->iCompression);
7846       break;
7847     default:  /* z */
7848       assert( i==7 );
7849       sqlite3_result_int64(ctx, pCsr->iId);
7850       break;
7851   }
7852
7853   return rc;
7854 }
7855
7856 /*
7857 ** Return TRUE if the cursor is at EOF.
7858 */
7859 static int zipfileEof(sqlite3_vtab_cursor *cur){
7860   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7861   return pCsr->bEof;
7862 }
7863
7864 /*
7865 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
7866 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
7867 ** is guaranteed to be a file-handle open on a zip file.
7868 **
7869 ** This function attempts to locate the EOCD record within the zip archive
7870 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
7871 ** returned if successful. Otherwise, an SQLite error code is returned and
7872 ** an English language error message may be left in virtual-table pTab.
7873 */
7874 static int zipfileReadEOCD(
7875   ZipfileTab *pTab,               /* Return errors here */
7876   const u8 *aBlob,                /* Pointer to in-memory file image */
7877   int nBlob,                      /* Size of aBlob[] in bytes */
7878   FILE *pFile,                    /* Read from this file if aBlob==0 */
7879   ZipfileEOCD *pEOCD              /* Object to populate */
7880 ){
7881   u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
7882   int nRead;                      /* Bytes to read from file */
7883   int rc = SQLITE_OK;
7884
7885   memset(pEOCD, 0, sizeof(ZipfileEOCD));
7886   if( aBlob==0 ){
7887     i64 iOff;                     /* Offset to read from */
7888     i64 szFile;                   /* Total size of file in bytes */
7889     fseek(pFile, 0, SEEK_END);
7890     szFile = (i64)ftell(pFile);
7891     if( szFile==0 ){
7892       return SQLITE_OK;
7893     }
7894     nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
7895     iOff = szFile - nRead;
7896     rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
7897   }else{
7898     nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
7899     aRead = (u8*)&aBlob[nBlob-nRead];
7900   }
7901
7902   if( rc==SQLITE_OK ){
7903     int i;
7904
7905     /* Scan backwards looking for the signature bytes */
7906     for(i=nRead-20; i>=0; i--){
7907       if( aRead[i]==0x50 && aRead[i+1]==0x4b 
7908        && aRead[i+2]==0x05 && aRead[i+3]==0x06 
7909       ){
7910         break;
7911       }
7912     }
7913     if( i<0 ){
7914       pTab->base.zErrMsg = sqlite3_mprintf(
7915           "cannot find end of central directory record"
7916       );
7917       return SQLITE_ERROR;
7918     }
7919
7920     aRead += i+4;
7921     pEOCD->iDisk = zipfileRead16(aRead);
7922     pEOCD->iFirstDisk = zipfileRead16(aRead);
7923     pEOCD->nEntry = zipfileRead16(aRead);
7924     pEOCD->nEntryTotal = zipfileRead16(aRead);
7925     pEOCD->nSize = zipfileRead32(aRead);
7926     pEOCD->iOffset = zipfileRead32(aRead);
7927   }
7928
7929   return rc;
7930 }
7931
7932 /*
7933 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
7934 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
7935 ** to the end of the list. Otherwise, it is added to the list immediately
7936 ** before pBefore (which is guaranteed to be a part of said list).
7937 */
7938 static void zipfileAddEntry(
7939   ZipfileTab *pTab, 
7940   ZipfileEntry *pBefore, 
7941   ZipfileEntry *pNew
7942 ){
7943   assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
7944   assert( pNew->pNext==0 );
7945   if( pBefore==0 ){
7946     if( pTab->pFirstEntry==0 ){
7947       pTab->pFirstEntry = pTab->pLastEntry = pNew;
7948     }else{
7949       assert( pTab->pLastEntry->pNext==0 );
7950       pTab->pLastEntry->pNext = pNew;
7951       pTab->pLastEntry = pNew;
7952     }
7953   }else{
7954     ZipfileEntry **pp;
7955     for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
7956     pNew->pNext = pBefore;
7957     *pp = pNew;
7958   }
7959 }
7960
7961 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
7962   ZipfileEOCD eocd;
7963   int rc;
7964   int i;
7965   i64 iOff;
7966
7967   rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
7968   iOff = eocd.iOffset;
7969   for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
7970     ZipfileEntry *pNew = 0;
7971     rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
7972
7973     if( rc==SQLITE_OK ){
7974       zipfileAddEntry(pTab, 0, pNew);
7975       iOff += ZIPFILE_CDS_FIXED_SZ;
7976       iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
7977     }
7978   }
7979   return rc;
7980 }
7981
7982 /*
7983 ** xFilter callback.
7984 */
7985 static int zipfileFilter(
7986   sqlite3_vtab_cursor *cur, 
7987   int idxNum, const char *idxStr,
7988   int argc, sqlite3_value **argv
7989 ){
7990   ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7991   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7992   const char *zFile = 0;          /* Zip file to scan */
7993   int rc = SQLITE_OK;             /* Return Code */
7994   int bInMemory = 0;              /* True for an in-memory zipfile */
7995
7996   zipfileResetCursor(pCsr);
7997
7998   if( pTab->zFile ){
7999     zFile = pTab->zFile;
8000   }else if( idxNum==0 ){
8001     zipfileCursorErr(pCsr, "zipfile() function requires an argument");
8002     return SQLITE_ERROR;
8003   }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8004     const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
8005     int nBlob = sqlite3_value_bytes(argv[0]);
8006     assert( pTab->pFirstEntry==0 );
8007     rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
8008     pCsr->pFreeEntry = pTab->pFirstEntry;
8009     pTab->pFirstEntry = pTab->pLastEntry = 0;
8010     if( rc!=SQLITE_OK ) return rc;
8011     bInMemory = 1;
8012   }else{
8013     zFile = (const char*)sqlite3_value_text(argv[0]);
8014   }
8015
8016   if( 0==pTab->pWriteFd && 0==bInMemory ){
8017     pCsr->pFile = fopen(zFile, "rb");
8018     if( pCsr->pFile==0 ){
8019       zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
8020       rc = SQLITE_ERROR;
8021     }else{
8022       rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
8023       if( rc==SQLITE_OK ){
8024         if( pCsr->eocd.nEntry==0 ){
8025           pCsr->bEof = 1;
8026         }else{
8027           pCsr->iNextOff = pCsr->eocd.iOffset;
8028           rc = zipfileNext(cur);
8029         }
8030       }
8031     }
8032   }else{
8033     pCsr->bNoop = 1;
8034     pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
8035     rc = zipfileNext(cur);
8036   }
8037
8038   return rc;
8039 }
8040
8041 /*
8042 ** xBestIndex callback.
8043 */
8044 static int zipfileBestIndex(
8045   sqlite3_vtab *tab,
8046   sqlite3_index_info *pIdxInfo
8047 ){
8048   int i;
8049   int idx = -1;
8050   int unusable = 0;
8051
8052   for(i=0; i<pIdxInfo->nConstraint; i++){
8053     const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8054     if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
8055     if( pCons->usable==0 ){
8056       unusable = 1;
8057     }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8058       idx = i;
8059     }
8060   }
8061   pIdxInfo->estimatedCost = 1000.0;
8062   if( idx>=0 ){
8063     pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
8064     pIdxInfo->aConstraintUsage[idx].omit = 1;
8065     pIdxInfo->idxNum = 1;
8066   }else if( unusable ){
8067     return SQLITE_CONSTRAINT;
8068   }
8069   return SQLITE_OK;
8070 }
8071
8072 static ZipfileEntry *zipfileNewEntry(const char *zPath){
8073   ZipfileEntry *pNew;
8074   pNew = sqlite3_malloc(sizeof(ZipfileEntry));
8075   if( pNew ){
8076     memset(pNew, 0, sizeof(ZipfileEntry));
8077     pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
8078     if( pNew->cds.zFile==0 ){
8079       sqlite3_free(pNew);
8080       pNew = 0;
8081     }
8082   }
8083   return pNew;
8084 }
8085
8086 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
8087   ZipfileCDS *pCds = &pEntry->cds;
8088   u8 *a = aBuf;
8089
8090   pCds->nExtra = 9;
8091
8092   /* Write the LFH itself */
8093   zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
8094   zipfileWrite16(a, pCds->iVersionExtract);
8095   zipfileWrite16(a, pCds->flags);
8096   zipfileWrite16(a, pCds->iCompression);
8097   zipfileWrite16(a, pCds->mTime);
8098   zipfileWrite16(a, pCds->mDate);
8099   zipfileWrite32(a, pCds->crc32);
8100   zipfileWrite32(a, pCds->szCompressed);
8101   zipfileWrite32(a, pCds->szUncompressed);
8102   zipfileWrite16(a, (u16)pCds->nFile);
8103   zipfileWrite16(a, pCds->nExtra);
8104   assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
8105
8106   /* Add the file name */
8107   memcpy(a, pCds->zFile, (int)pCds->nFile);
8108   a += (int)pCds->nFile;
8109
8110   /* The "extra" data */
8111   zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8112   zipfileWrite16(a, 5);
8113   *a++ = 0x01;
8114   zipfileWrite32(a, pEntry->mUnixTime);
8115
8116   return a-aBuf;
8117 }
8118
8119 static int zipfileAppendEntry(
8120   ZipfileTab *pTab,
8121   ZipfileEntry *pEntry,
8122   const u8 *pData,
8123   int nData
8124 ){
8125   u8 *aBuf = pTab->aBuffer;
8126   int nBuf;
8127   int rc;
8128
8129   nBuf = zipfileSerializeLFH(pEntry, aBuf);
8130   rc = zipfileAppendData(pTab, aBuf, nBuf);
8131   if( rc==SQLITE_OK ){
8132     pEntry->iDataOff = pTab->szCurrent;
8133     rc = zipfileAppendData(pTab, pData, nData);
8134   }
8135
8136   return rc;
8137 }
8138
8139 static int zipfileGetMode(
8140   sqlite3_value *pVal, 
8141   int bIsDir,                     /* If true, default to directory */
8142   u32 *pMode,                     /* OUT: Mode value */
8143   char **pzErr                    /* OUT: Error message */
8144 ){
8145   const char *z = (const char*)sqlite3_value_text(pVal);
8146   u32 mode = 0;
8147   if( z==0 ){
8148     mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
8149   }else if( z[0]>='0' && z[0]<='9' ){
8150     mode = (unsigned int)sqlite3_value_int(pVal);
8151   }else{
8152     const char zTemplate[11] = "-rwxrwxrwx";
8153     int i;
8154     if( strlen(z)!=10 ) goto parse_error;
8155     switch( z[0] ){
8156       case '-': mode |= S_IFREG; break;
8157       case 'd': mode |= S_IFDIR; break;
8158       case 'l': mode |= S_IFLNK; break;
8159       default: goto parse_error;
8160     }
8161     for(i=1; i<10; i++){
8162       if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
8163       else if( z[i]!='-' ) goto parse_error;
8164     }
8165   }
8166   if( ((mode & S_IFDIR)==0)==bIsDir ){
8167     /* The "mode" attribute is a directory, but data has been specified.
8168     ** Or vice-versa - no data but "mode" is a file or symlink.  */
8169     *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
8170     return SQLITE_CONSTRAINT;
8171   }
8172   *pMode = mode;
8173   return SQLITE_OK;
8174
8175  parse_error:
8176   *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
8177   return SQLITE_ERROR;
8178 }
8179
8180 /*
8181 ** Both (const char*) arguments point to nul-terminated strings. Argument
8182 ** nB is the value of strlen(zB). This function returns 0 if the strings are
8183 ** identical, ignoring any trailing '/' character in either path.  */
8184 static int zipfileComparePath(const char *zA, const char *zB, int nB){
8185   int nA = (int)strlen(zA);
8186   if( nA>0 && zA[nA-1]=='/' ) nA--;
8187   if( nB>0 && zB[nB-1]=='/' ) nB--;
8188   if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
8189   return 1;
8190 }
8191
8192 static int zipfileBegin(sqlite3_vtab *pVtab){
8193   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8194   int rc = SQLITE_OK;
8195
8196   assert( pTab->pWriteFd==0 );
8197   if( pTab->zFile==0 || pTab->zFile[0]==0 ){
8198     pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
8199     return SQLITE_ERROR;
8200   }
8201
8202   /* Open a write fd on the file. Also load the entire central directory
8203   ** structure into memory. During the transaction any new file data is 
8204   ** appended to the archive file, but the central directory is accumulated
8205   ** in main-memory until the transaction is committed.  */
8206   pTab->pWriteFd = fopen(pTab->zFile, "ab+");
8207   if( pTab->pWriteFd==0 ){
8208     pTab->base.zErrMsg = sqlite3_mprintf(
8209         "zipfile: failed to open file %s for writing", pTab->zFile
8210         );
8211     rc = SQLITE_ERROR;
8212   }else{
8213     fseek(pTab->pWriteFd, 0, SEEK_END);
8214     pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
8215     rc = zipfileLoadDirectory(pTab, 0, 0);
8216   }
8217
8218   if( rc!=SQLITE_OK ){
8219     zipfileCleanupTransaction(pTab);
8220   }
8221
8222   return rc;
8223 }
8224
8225 /*
8226 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
8227 ** time(2)).
8228 */
8229 static u32 zipfileTime(void){
8230   sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
8231   u32 ret;
8232   if( pVfs==0 ) return 0;
8233   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
8234     i64 ms;
8235     pVfs->xCurrentTimeInt64(pVfs, &ms);
8236     ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
8237   }else{
8238     double day;
8239     pVfs->xCurrentTime(pVfs, &day);
8240     ret = (u32)((day - 2440587.5) * 86400);
8241   }
8242   return ret;
8243 }
8244
8245 /*
8246 ** Return a 32-bit timestamp in UNIX epoch format.
8247 **
8248 ** If the value passed as the only argument is either NULL or an SQL NULL,
8249 ** return the current time. Otherwise, return the value stored in (*pVal)
8250 ** cast to a 32-bit unsigned integer.
8251 */
8252 static u32 zipfileGetTime(sqlite3_value *pVal){
8253   if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
8254     return zipfileTime();
8255   }
8256   return (u32)sqlite3_value_int64(pVal);
8257 }
8258
8259 /*
8260 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
8261 ** linked list.  Remove it from the list and free the object.
8262 */
8263 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
8264   if( pOld ){
8265     ZipfileEntry **pp;
8266     for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
8267     *pp = (*pp)->pNext;
8268     zipfileEntryFree(pOld);
8269   }
8270 }
8271
8272 /*
8273 ** xUpdate method.
8274 */
8275 static int zipfileUpdate(
8276   sqlite3_vtab *pVtab, 
8277   int nVal, 
8278   sqlite3_value **apVal, 
8279   sqlite_int64 *pRowid
8280 ){
8281   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8282   int rc = SQLITE_OK;             /* Return Code */
8283   ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
8284
8285   u32 mode = 0;                   /* Mode for new entry */
8286   u32 mTime = 0;                  /* Modification time for new entry */
8287   i64 sz = 0;                     /* Uncompressed size */
8288   const char *zPath = 0;          /* Path for new entry */
8289   int nPath = 0;                  /* strlen(zPath) */
8290   const u8 *pData = 0;            /* Pointer to buffer containing content */
8291   int nData = 0;                  /* Size of pData buffer in bytes */
8292   int iMethod = 0;                /* Compression method for new entry */
8293   u8 *pFree = 0;                  /* Free this */
8294   char *zFree = 0;                /* Also free this */
8295   ZipfileEntry *pOld = 0;
8296   ZipfileEntry *pOld2 = 0;
8297   int bUpdate = 0;                /* True for an update that modifies "name" */
8298   int bIsDir = 0;
8299   u32 iCrc32 = 0;
8300
8301   if( pTab->pWriteFd==0 ){
8302     rc = zipfileBegin(pVtab);
8303     if( rc!=SQLITE_OK ) return rc;
8304   }
8305
8306   /* If this is a DELETE or UPDATE, find the archive entry to delete. */
8307   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
8308     const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
8309     int nDelete = (int)strlen(zDelete);
8310     if( nVal>1 ){
8311       const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
8312       if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
8313         bUpdate = 1;
8314       }
8315     }
8316     for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
8317       if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
8318         break;
8319       }
8320       assert( pOld->pNext );
8321     }
8322   }
8323
8324   if( nVal>1 ){
8325     /* Check that "sz" and "rawdata" are both NULL: */
8326     if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
8327       zipfileTableErr(pTab, "sz must be NULL");
8328       rc = SQLITE_CONSTRAINT;
8329     }
8330     if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
8331       zipfileTableErr(pTab, "rawdata must be NULL"); 
8332       rc = SQLITE_CONSTRAINT;
8333     }
8334
8335     if( rc==SQLITE_OK ){
8336       if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
8337         /* data=NULL. A directory */
8338         bIsDir = 1;
8339       }else{
8340         /* Value specified for "data", and possibly "method". This must be
8341         ** a regular file or a symlink. */
8342         const u8 *aIn = sqlite3_value_blob(apVal[7]);
8343         int nIn = sqlite3_value_bytes(apVal[7]);
8344         int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
8345
8346         iMethod = sqlite3_value_int(apVal[8]);
8347         sz = nIn;
8348         pData = aIn;
8349         nData = nIn;
8350         if( iMethod!=0 && iMethod!=8 ){
8351           zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
8352           rc = SQLITE_CONSTRAINT;
8353         }else{
8354           if( bAuto || iMethod ){
8355             int nCmp;
8356             rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
8357             if( rc==SQLITE_OK ){
8358               if( iMethod || nCmp<nIn ){
8359                 iMethod = 8;
8360                 pData = pFree;
8361                 nData = nCmp;
8362               }
8363             }
8364           }
8365           iCrc32 = crc32(0, aIn, nIn);
8366         }
8367       }
8368     }
8369
8370     if( rc==SQLITE_OK ){
8371       rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
8372     }
8373
8374     if( rc==SQLITE_OK ){
8375       zPath = (const char*)sqlite3_value_text(apVal[2]);
8376       if( zPath==0 ) zPath = "";
8377       nPath = (int)strlen(zPath);
8378       mTime = zipfileGetTime(apVal[4]);
8379     }
8380
8381     if( rc==SQLITE_OK && bIsDir ){
8382       /* For a directory, check that the last character in the path is a
8383       ** '/'. This appears to be required for compatibility with info-zip
8384       ** (the unzip command on unix). It does not create directories
8385       ** otherwise.  */
8386       if( nPath<=0 || zPath[nPath-1]!='/' ){
8387         zFree = sqlite3_mprintf("%s/", zPath);
8388         zPath = (const char*)zFree;
8389         if( zFree==0 ){
8390           rc = SQLITE_NOMEM;
8391           nPath = 0;
8392         }else{
8393           nPath = (int)strlen(zPath);
8394         }
8395       }
8396     }
8397
8398     /* Check that we're not inserting a duplicate entry -OR- updating an
8399     ** entry with a path, thereby making it into a duplicate. */
8400     if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
8401       ZipfileEntry *p;
8402       for(p=pTab->pFirstEntry; p; p=p->pNext){
8403         if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
8404           switch( sqlite3_vtab_on_conflict(pTab->db) ){
8405             case SQLITE_IGNORE: {
8406               goto zipfile_update_done;
8407             }
8408             case SQLITE_REPLACE: {
8409               pOld2 = p;
8410               break;
8411             }
8412             default: {
8413               zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
8414               rc = SQLITE_CONSTRAINT;
8415               break;
8416             }
8417           }
8418           break;
8419         }
8420       }
8421     }
8422
8423     if( rc==SQLITE_OK ){
8424       /* Create the new CDS record. */
8425       pNew = zipfileNewEntry(zPath);
8426       if( pNew==0 ){
8427         rc = SQLITE_NOMEM;
8428       }else{
8429         pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8430         pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8431         pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8432         pNew->cds.iCompression = (u16)iMethod;
8433         zipfileMtimeToDos(&pNew->cds, mTime);
8434         pNew->cds.crc32 = iCrc32;
8435         pNew->cds.szCompressed = nData;
8436         pNew->cds.szUncompressed = (u32)sz;
8437         pNew->cds.iExternalAttr = (mode<<16);
8438         pNew->cds.iOffset = (u32)pTab->szCurrent;
8439         pNew->cds.nFile = (u16)nPath;
8440         pNew->mUnixTime = (u32)mTime;
8441         rc = zipfileAppendEntry(pTab, pNew, pData, nData);
8442         zipfileAddEntry(pTab, pOld, pNew);
8443       }
8444     }
8445   }
8446
8447   if( rc==SQLITE_OK && (pOld || pOld2) ){
8448     ZipfileCsr *pCsr;
8449     for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8450       if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
8451         pCsr->pCurrent = pCsr->pCurrent->pNext;
8452         pCsr->bNoop = 1;
8453       }
8454     }
8455
8456     zipfileRemoveEntryFromList(pTab, pOld);
8457     zipfileRemoveEntryFromList(pTab, pOld2);
8458   }
8459
8460 zipfile_update_done:
8461   sqlite3_free(pFree);
8462   sqlite3_free(zFree);
8463   return rc;
8464 }
8465
8466 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
8467   u8 *a = aBuf;
8468   zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
8469   zipfileWrite16(a, p->iDisk);
8470   zipfileWrite16(a, p->iFirstDisk);
8471   zipfileWrite16(a, p->nEntry);
8472   zipfileWrite16(a, p->nEntryTotal);
8473   zipfileWrite32(a, p->nSize);
8474   zipfileWrite32(a, p->iOffset);
8475   zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
8476
8477   return a-aBuf;
8478 }
8479
8480 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
8481   int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
8482   assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
8483   return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
8484 }
8485
8486 /*
8487 ** Serialize the CDS structure into buffer aBuf[]. Return the number
8488 ** of bytes written.
8489 */
8490 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
8491   u8 *a = aBuf;
8492   ZipfileCDS *pCDS = &pEntry->cds;
8493
8494   if( pEntry->aExtra==0 ){
8495     pCDS->nExtra = 9;
8496   }
8497
8498   zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
8499   zipfileWrite16(a, pCDS->iVersionMadeBy);
8500   zipfileWrite16(a, pCDS->iVersionExtract);
8501   zipfileWrite16(a, pCDS->flags);
8502   zipfileWrite16(a, pCDS->iCompression);
8503   zipfileWrite16(a, pCDS->mTime);
8504   zipfileWrite16(a, pCDS->mDate);
8505   zipfileWrite32(a, pCDS->crc32);
8506   zipfileWrite32(a, pCDS->szCompressed);
8507   zipfileWrite32(a, pCDS->szUncompressed);
8508   assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
8509   zipfileWrite16(a, pCDS->nFile);
8510   zipfileWrite16(a, pCDS->nExtra);
8511   zipfileWrite16(a, pCDS->nComment);
8512   zipfileWrite16(a, pCDS->iDiskStart);
8513   zipfileWrite16(a, pCDS->iInternalAttr);
8514   zipfileWrite32(a, pCDS->iExternalAttr);
8515   zipfileWrite32(a, pCDS->iOffset);
8516
8517   memcpy(a, pCDS->zFile, pCDS->nFile);
8518   a += pCDS->nFile;
8519
8520   if( pEntry->aExtra ){
8521     int n = (int)pCDS->nExtra + (int)pCDS->nComment;
8522     memcpy(a, pEntry->aExtra, n);
8523     a += n;
8524   }else{
8525     assert( pCDS->nExtra==9 );
8526     zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8527     zipfileWrite16(a, 5);
8528     *a++ = 0x01;
8529     zipfileWrite32(a, pEntry->mUnixTime);
8530   }
8531
8532   return a-aBuf;
8533 }
8534
8535 static int zipfileCommit(sqlite3_vtab *pVtab){
8536   ZipfileTab *pTab = (ZipfileTab*)pVtab;
8537   int rc = SQLITE_OK;
8538   if( pTab->pWriteFd ){
8539     i64 iOffset = pTab->szCurrent;
8540     ZipfileEntry *p;
8541     ZipfileEOCD eocd;
8542     int nEntry = 0;
8543
8544     /* Write out all entries */
8545     for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
8546       int n = zipfileSerializeCDS(p, pTab->aBuffer);
8547       rc = zipfileAppendData(pTab, pTab->aBuffer, n);
8548       nEntry++;
8549     }
8550
8551     /* Write out the EOCD record */
8552     eocd.iDisk = 0;
8553     eocd.iFirstDisk = 0;
8554     eocd.nEntry = (u16)nEntry;
8555     eocd.nEntryTotal = (u16)nEntry;
8556     eocd.nSize = (u32)(pTab->szCurrent - iOffset);
8557     eocd.iOffset = (u32)iOffset;
8558     rc = zipfileAppendEOCD(pTab, &eocd);
8559
8560     zipfileCleanupTransaction(pTab);
8561   }
8562   return rc;
8563 }
8564
8565 static int zipfileRollback(sqlite3_vtab *pVtab){
8566   return zipfileCommit(pVtab);
8567 }
8568
8569 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
8570   ZipfileCsr *pCsr;
8571   for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8572     if( iId==pCsr->iId ) break;
8573   }
8574   return pCsr;
8575 }
8576
8577 static void zipfileFunctionCds(
8578   sqlite3_context *context,
8579   int argc,
8580   sqlite3_value **argv
8581 ){
8582   ZipfileCsr *pCsr;
8583   ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
8584   assert( argc>0 );
8585
8586   pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
8587   if( pCsr ){
8588     ZipfileCDS *p = &pCsr->pCurrent->cds;
8589     char *zRes = sqlite3_mprintf("{"
8590         "\"version-made-by\" : %u, "
8591         "\"version-to-extract\" : %u, "
8592         "\"flags\" : %u, "
8593         "\"compression\" : %u, "
8594         "\"time\" : %u, "
8595         "\"date\" : %u, "
8596         "\"crc32\" : %u, "
8597         "\"compressed-size\" : %u, "
8598         "\"uncompressed-size\" : %u, "
8599         "\"file-name-length\" : %u, "
8600         "\"extra-field-length\" : %u, "
8601         "\"file-comment-length\" : %u, "
8602         "\"disk-number-start\" : %u, "
8603         "\"internal-attr\" : %u, "
8604         "\"external-attr\" : %u, "
8605         "\"offset\" : %u }",
8606         (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
8607         (u32)p->flags, (u32)p->iCompression,
8608         (u32)p->mTime, (u32)p->mDate,
8609         (u32)p->crc32, (u32)p->szCompressed,
8610         (u32)p->szUncompressed, (u32)p->nFile,
8611         (u32)p->nExtra, (u32)p->nComment,
8612         (u32)p->iDiskStart, (u32)p->iInternalAttr,
8613         (u32)p->iExternalAttr, (u32)p->iOffset
8614     );
8615
8616     if( zRes==0 ){
8617       sqlite3_result_error_nomem(context);
8618     }else{
8619       sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
8620       sqlite3_free(zRes);
8621     }
8622   }
8623 }
8624
8625 /*
8626 ** xFindFunction method.
8627 */
8628 static int zipfileFindFunction(
8629   sqlite3_vtab *pVtab,            /* Virtual table handle */
8630   int nArg,                       /* Number of SQL function arguments */
8631   const char *zName,              /* Name of SQL function */
8632   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
8633   void **ppArg                    /* OUT: User data for *pxFunc */
8634 ){
8635   if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
8636     *pxFunc = zipfileFunctionCds;
8637     *ppArg = (void*)pVtab;
8638     return 1;
8639   }
8640   return 0;
8641 }
8642
8643 typedef struct ZipfileBuffer ZipfileBuffer;
8644 struct ZipfileBuffer {
8645   u8 *a;                          /* Pointer to buffer */
8646   int n;                          /* Size of buffer in bytes */
8647   int nAlloc;                     /* Byte allocated at a[] */
8648 };
8649
8650 typedef struct ZipfileCtx ZipfileCtx;
8651 struct ZipfileCtx {
8652   int nEntry;
8653   ZipfileBuffer body;
8654   ZipfileBuffer cds;
8655 };
8656
8657 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
8658   if( pBuf->n+nByte>pBuf->nAlloc ){
8659     u8 *aNew;
8660     sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
8661     int nReq = pBuf->n + nByte;
8662
8663     while( nNew<nReq ) nNew = nNew*2;
8664     aNew = sqlite3_realloc64(pBuf->a, nNew);
8665     if( aNew==0 ) return SQLITE_NOMEM;
8666     pBuf->a = aNew;
8667     pBuf->nAlloc = (int)nNew;
8668   }
8669   return SQLITE_OK;
8670 }
8671
8672 /*
8673 ** xStep() callback for the zipfile() aggregate. This can be called in
8674 ** any of the following ways:
8675 **
8676 **   SELECT zipfile(name,data) ...
8677 **   SELECT zipfile(name,mode,mtime,data) ...
8678 **   SELECT zipfile(name,mode,mtime,data,method) ...
8679 */
8680 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
8681   ZipfileCtx *p;                  /* Aggregate function context */
8682   ZipfileEntry e;                 /* New entry to add to zip archive */
8683
8684   sqlite3_value *pName = 0;
8685   sqlite3_value *pMode = 0;
8686   sqlite3_value *pMtime = 0;
8687   sqlite3_value *pData = 0;
8688   sqlite3_value *pMethod = 0;
8689
8690   int bIsDir = 0;
8691   u32 mode;
8692   int rc = SQLITE_OK;
8693   char *zErr = 0;
8694
8695   int iMethod = -1;               /* Compression method to use (0 or 8) */
8696
8697   const u8 *aData = 0;            /* Possibly compressed data for new entry */
8698   int nData = 0;                  /* Size of aData[] in bytes */
8699   int szUncompressed = 0;         /* Size of data before compression */
8700   u8 *aFree = 0;                  /* Free this before returning */
8701   u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
8702
8703   char *zName = 0;                /* Path (name) of new entry */
8704   int nName = 0;                  /* Size of zName in bytes */
8705   char *zFree = 0;                /* Free this before returning */
8706   int nByte;
8707
8708   memset(&e, 0, sizeof(e));
8709   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8710   if( p==0 ) return;
8711
8712   /* Martial the arguments into stack variables */
8713   if( nVal!=2 && nVal!=4 && nVal!=5 ){
8714     zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
8715     rc = SQLITE_ERROR;
8716     goto zipfile_step_out;
8717   }
8718   pName = apVal[0];
8719   if( nVal==2 ){
8720     pData = apVal[1];
8721   }else{
8722     pMode = apVal[1];
8723     pMtime = apVal[2];
8724     pData = apVal[3];
8725     if( nVal==5 ){
8726       pMethod = apVal[4];
8727     }
8728   }
8729
8730   /* Check that the 'name' parameter looks ok. */
8731   zName = (char*)sqlite3_value_text(pName);
8732   nName = sqlite3_value_bytes(pName);
8733   if( zName==0 ){
8734     zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
8735     rc = SQLITE_ERROR;
8736     goto zipfile_step_out;
8737   }
8738
8739   /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
8740   ** deflate compression) or NULL (choose automatically).  */
8741   if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
8742     iMethod = (int)sqlite3_value_int64(pMethod);
8743     if( iMethod!=0 && iMethod!=8 ){
8744       zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
8745       rc = SQLITE_ERROR;
8746       goto zipfile_step_out;
8747     }
8748   }
8749
8750   /* Now inspect the data. If this is NULL, then the new entry must be a
8751   ** directory.  Otherwise, figure out whether or not the data should
8752   ** be deflated or simply stored in the zip archive. */
8753   if( sqlite3_value_type(pData)==SQLITE_NULL ){
8754     bIsDir = 1;
8755     iMethod = 0;
8756   }else{
8757     aData = sqlite3_value_blob(pData);
8758     szUncompressed = nData = sqlite3_value_bytes(pData);
8759     iCrc32 = crc32(0, aData, nData);
8760     if( iMethod<0 || iMethod==8 ){
8761       int nOut = 0;
8762       rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
8763       if( rc!=SQLITE_OK ){
8764         goto zipfile_step_out;
8765       }
8766       if( iMethod==8 || nOut<nData ){
8767         aData = aFree;
8768         nData = nOut;
8769         iMethod = 8;
8770       }else{
8771         iMethod = 0;
8772       }
8773     }
8774   }
8775
8776   /* Decode the "mode" argument. */
8777   rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
8778   if( rc ) goto zipfile_step_out;
8779
8780   /* Decode the "mtime" argument. */
8781   e.mUnixTime = zipfileGetTime(pMtime);
8782
8783   /* If this is a directory entry, ensure that there is exactly one '/'
8784   ** at the end of the path. Or, if this is not a directory and the path
8785   ** ends in '/' it is an error. */
8786   if( bIsDir==0 ){
8787     if( nName>0 && zName[nName-1]=='/' ){
8788       zErr = sqlite3_mprintf("non-directory name must not end with /");
8789       rc = SQLITE_ERROR;
8790       goto zipfile_step_out;
8791     }
8792   }else{
8793     if( nName==0 || zName[nName-1]!='/' ){
8794       zName = zFree = sqlite3_mprintf("%s/", zName);
8795       if( zName==0 ){
8796         rc = SQLITE_NOMEM;
8797         goto zipfile_step_out;
8798       }
8799       nName = (int)strlen(zName);
8800     }else{
8801       while( nName>1 && zName[nName-2]=='/' ) nName--;
8802     }
8803   }
8804
8805   /* Assemble the ZipfileEntry object for the new zip archive entry */
8806   e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8807   e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8808   e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8809   e.cds.iCompression = (u16)iMethod;
8810   zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
8811   e.cds.crc32 = iCrc32;
8812   e.cds.szCompressed = nData;
8813   e.cds.szUncompressed = szUncompressed;
8814   e.cds.iExternalAttr = (mode<<16);
8815   e.cds.iOffset = p->body.n;
8816   e.cds.nFile = (u16)nName;
8817   e.cds.zFile = zName;
8818
8819   /* Append the LFH to the body of the new archive */
8820   nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
8821   if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
8822   p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
8823
8824   /* Append the data to the body of the new archive */
8825   if( nData>0 ){
8826     if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
8827     memcpy(&p->body.a[p->body.n], aData, nData);
8828     p->body.n += nData;
8829   }
8830
8831   /* Append the CDS record to the directory of the new archive */
8832   nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
8833   if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
8834   p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
8835
8836   /* Increment the count of entries in the archive */
8837   p->nEntry++;
8838
8839  zipfile_step_out:
8840   sqlite3_free(aFree);
8841   sqlite3_free(zFree);
8842   if( rc ){
8843     if( zErr ){
8844       sqlite3_result_error(pCtx, zErr, -1);
8845     }else{
8846       sqlite3_result_error_code(pCtx, rc);
8847     }
8848   }
8849   sqlite3_free(zErr);
8850 }
8851
8852 /*
8853 ** xFinalize() callback for zipfile aggregate function.
8854 */
8855 void zipfileFinal(sqlite3_context *pCtx){
8856   ZipfileCtx *p;
8857   ZipfileEOCD eocd;
8858   sqlite3_int64 nZip;
8859   u8 *aZip;
8860
8861   p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8862   if( p==0 ) return;
8863   if( p->nEntry>0 ){
8864     memset(&eocd, 0, sizeof(eocd));
8865     eocd.nEntry = (u16)p->nEntry;
8866     eocd.nEntryTotal = (u16)p->nEntry;
8867     eocd.nSize = p->cds.n;
8868     eocd.iOffset = p->body.n;
8869
8870     nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
8871     aZip = (u8*)sqlite3_malloc64(nZip);
8872     if( aZip==0 ){
8873       sqlite3_result_error_nomem(pCtx);
8874     }else{
8875       memcpy(aZip, p->body.a, p->body.n);
8876       memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
8877       zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
8878       sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
8879     }
8880   }
8881
8882   sqlite3_free(p->body.a);
8883   sqlite3_free(p->cds.a);
8884 }
8885
8886
8887 /*
8888 ** Register the "zipfile" virtual table.
8889 */
8890 static int zipfileRegister(sqlite3 *db){
8891   static sqlite3_module zipfileModule = {
8892     1,                         /* iVersion */
8893     zipfileConnect,            /* xCreate */
8894     zipfileConnect,            /* xConnect */
8895     zipfileBestIndex,          /* xBestIndex */
8896     zipfileDisconnect,         /* xDisconnect */
8897     zipfileDisconnect,         /* xDestroy */
8898     zipfileOpen,               /* xOpen - open a cursor */
8899     zipfileClose,              /* xClose - close a cursor */
8900     zipfileFilter,             /* xFilter - configure scan constraints */
8901     zipfileNext,               /* xNext - advance a cursor */
8902     zipfileEof,                /* xEof - check for end of scan */
8903     zipfileColumn,             /* xColumn - read data */
8904     0,                         /* xRowid - read data */
8905     zipfileUpdate,             /* xUpdate */
8906     zipfileBegin,              /* xBegin */
8907     0,                         /* xSync */
8908     zipfileCommit,             /* xCommit */
8909     zipfileRollback,           /* xRollback */
8910     zipfileFindFunction,       /* xFindMethod */
8911     0,                         /* xRename */
8912   };
8913
8914   int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
8915   if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
8916   if( rc==SQLITE_OK ){
8917     rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
8918         zipfileStep, zipfileFinal
8919     );
8920   }
8921   assert( sizeof(i64)==8 );
8922   assert( sizeof(u32)==4 );
8923   assert( sizeof(u16)==2 );
8924   assert( sizeof(u8)==1 );
8925   return rc;
8926 }
8927 #else         /* SQLITE_OMIT_VIRTUALTABLE */
8928 # define zipfileRegister(x) SQLITE_OK
8929 #endif
8930
8931 #ifdef _WIN32
8932
8933 #endif
8934 int sqlite3_zipfile_init(
8935   sqlite3 *db, 
8936   char **pzErrMsg, 
8937   const sqlite3_api_routines *pApi
8938 ){
8939   SQLITE_EXTENSION_INIT2(pApi);
8940   (void)pzErrMsg;  /* Unused parameter */
8941   return zipfileRegister(db);
8942 }
8943
8944 /************************* End ../ext/misc/zipfile.c ********************/
8945 /************************* Begin ../ext/misc/sqlar.c ******************/
8946 /*
8947 ** 2017-12-17
8948 **
8949 ** The author disclaims copyright to this source code.  In place of
8950 ** a legal notice, here is a blessing:
8951 **
8952 **    May you do good and not evil.
8953 **    May you find forgiveness for yourself and forgive others.
8954 **    May you share freely, never taking more than you give.
8955 **
8956 ******************************************************************************
8957 **
8958 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
8959 ** for working with sqlar archives and used by the shell tool's built-in
8960 ** sqlar support.
8961 */
8962 /* #include "sqlite3ext.h" */
8963 SQLITE_EXTENSION_INIT1
8964 #include <zlib.h>
8965 #include <assert.h>
8966
8967 /*
8968 ** Implementation of the "sqlar_compress(X)" SQL function.
8969 **
8970 ** If the type of X is SQLITE_BLOB, and compressing that blob using
8971 ** zlib utility function compress() yields a smaller blob, return the
8972 ** compressed blob. Otherwise, return a copy of X.
8973 **
8974 ** SQLar uses the "zlib format" for compressed content.  The zlib format
8975 ** contains a two-byte identification header and a four-byte checksum at
8976 ** the end.  This is different from ZIP which uses the raw deflate format.
8977 **
8978 ** Future enhancements to SQLar might add support for new compression formats.
8979 ** If so, those new formats will be identified by alternative headers in the
8980 ** compressed data.
8981 */
8982 static void sqlarCompressFunc(
8983   sqlite3_context *context,
8984   int argc,
8985   sqlite3_value **argv
8986 ){
8987   assert( argc==1 );
8988   if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8989     const Bytef *pData = sqlite3_value_blob(argv[0]);
8990     uLong nData = sqlite3_value_bytes(argv[0]);
8991     uLongf nOut = compressBound(nData);
8992     Bytef *pOut;
8993
8994     pOut = (Bytef*)sqlite3_malloc(nOut);
8995     if( pOut==0 ){
8996       sqlite3_result_error_nomem(context);
8997       return;
8998     }else{
8999       if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
9000         sqlite3_result_error(context, "error in compress()", -1);
9001       }else if( nOut<nData ){
9002         sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
9003       }else{
9004         sqlite3_result_value(context, argv[0]);
9005       }
9006       sqlite3_free(pOut);
9007     }
9008   }else{
9009     sqlite3_result_value(context, argv[0]);
9010   }
9011 }
9012
9013 /*
9014 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
9015 **
9016 ** Parameter SZ is interpreted as an integer. If it is less than or
9017 ** equal to zero, then this function returns a copy of X. Or, if
9018 ** SZ is equal to the size of X when interpreted as a blob, also
9019 ** return a copy of X. Otherwise, decompress blob X using zlib
9020 ** utility function uncompress() and return the results (another
9021 ** blob).
9022 */
9023 static void sqlarUncompressFunc(
9024   sqlite3_context *context,
9025   int argc,
9026   sqlite3_value **argv
9027 ){
9028   uLong nData;
9029   uLongf sz;
9030
9031   assert( argc==2 );
9032   sz = sqlite3_value_int(argv[1]);
9033
9034   if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
9035     sqlite3_result_value(context, argv[0]);
9036   }else{
9037     const Bytef *pData= sqlite3_value_blob(argv[0]);
9038     Bytef *pOut = sqlite3_malloc(sz);
9039     if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
9040       sqlite3_result_error(context, "error in uncompress()", -1);
9041     }else{
9042       sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
9043     }
9044     sqlite3_free(pOut);
9045   }
9046 }
9047
9048
9049 #ifdef _WIN32
9050
9051 #endif
9052 int sqlite3_sqlar_init(
9053   sqlite3 *db, 
9054   char **pzErrMsg, 
9055   const sqlite3_api_routines *pApi
9056 ){
9057   int rc = SQLITE_OK;
9058   SQLITE_EXTENSION_INIT2(pApi);
9059   (void)pzErrMsg;  /* Unused parameter */
9060   rc = sqlite3_create_function(db, "sqlar_compress", 1, 
9061                                SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
9062                                sqlarCompressFunc, 0, 0);
9063   if( rc==SQLITE_OK ){
9064     rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
9065                                  SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
9066                                  sqlarUncompressFunc, 0, 0);
9067   }
9068   return rc;
9069 }
9070
9071 /************************* End ../ext/misc/sqlar.c ********************/
9072 #endif
9073 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
9074 /*
9075 ** 2017 April 07
9076 **
9077 ** The author disclaims copyright to this source code.  In place of
9078 ** a legal notice, here is a blessing:
9079 **
9080 **    May you do good and not evil.
9081 **    May you find forgiveness for yourself and forgive others.
9082 **    May you share freely, never taking more than you give.
9083 **
9084 *************************************************************************
9085 */
9086 #if !defined(SQLITEEXPERT_H)
9087 #define SQLITEEXPERT_H 1
9088 /* #include "sqlite3.h" */
9089
9090 typedef struct sqlite3expert sqlite3expert;
9091
9092 /*
9093 ** Create a new sqlite3expert object.
9094 **
9095 ** If successful, a pointer to the new object is returned and (*pzErr) set
9096 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
9097 ** an English-language error message. In this case it is the responsibility
9098 ** of the caller to eventually free the error message buffer using
9099 ** sqlite3_free().
9100 */
9101 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
9102
9103 /*
9104 ** Configure an sqlite3expert object.
9105 **
9106 ** EXPERT_CONFIG_SAMPLE:
9107 **   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
9108 **   each candidate index. This involves scanning and sorting the entire
9109 **   contents of each user database table once for each candidate index
9110 **   associated with the table. For large databases, this can be 
9111 **   prohibitively slow. This option allows the sqlite3expert object to
9112 **   be configured so that sqlite_stat1 data is instead generated based on a
9113 **   subset of each table, or so that no sqlite_stat1 data is used at all.
9114 **
9115 **   A single integer argument is passed to this option. If the value is less
9116 **   than or equal to zero, then no sqlite_stat1 data is generated or used by
9117 **   the analysis - indexes are recommended based on the database schema only.
9118 **   Or, if the value is 100 or greater, complete sqlite_stat1 data is
9119 **   generated for each candidate index (this is the default). Finally, if the
9120 **   value falls between 0 and 100, then it represents the percentage of user
9121 **   table rows that should be considered when generating sqlite_stat1 data.
9122 **
9123 **   Examples:
9124 **
9125 **     // Do not generate any sqlite_stat1 data
9126 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
9127 **
9128 **     // Generate sqlite_stat1 data based on 10% of the rows in each table.
9129 **     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
9130 */
9131 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
9132
9133 #define EXPERT_CONFIG_SAMPLE 1    /* int */
9134
9135 /*
9136 ** Specify zero or more SQL statements to be included in the analysis.
9137 **
9138 ** Buffer zSql must contain zero or more complete SQL statements. This
9139 ** function parses all statements contained in the buffer and adds them
9140 ** to the internal list of statements to analyze. If successful, SQLITE_OK
9141 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
9142 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
9143 ** may be set to point to an English language error message. In this case
9144 ** the caller is responsible for eventually freeing the error message buffer
9145 ** using sqlite3_free().
9146 **
9147 ** If an error does occur while processing one of the statements in the
9148 ** buffer passed as the second argument, none of the statements in the
9149 ** buffer are added to the analysis.
9150 **
9151 ** This function must be called before sqlite3_expert_analyze(). If a call
9152 ** to this function is made on an sqlite3expert object that has already
9153 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
9154 ** immediately and no statements are added to the analysis.
9155 */
9156 int sqlite3_expert_sql(
9157   sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
9158   const char *zSql,               /* SQL statement(s) to add */
9159   char **pzErr                    /* OUT: Error message (if any) */
9160 );
9161
9162
9163 /*
9164 ** This function is called after the sqlite3expert object has been configured
9165 ** with all SQL statements using sqlite3_expert_sql() to actually perform
9166 ** the analysis. Once this function has been called, it is not possible to
9167 ** add further SQL statements to the analysis.
9168 **
9169 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
9170 ** an error occurs, an SQLite error code is returned and (*pzErr) set to 
9171 ** point to a buffer containing an English language error message. In this
9172 ** case it is the responsibility of the caller to eventually free the buffer
9173 ** using sqlite3_free().
9174 **
9175 ** If an error does occur within this function, the sqlite3expert object
9176 ** is no longer useful for any purpose. At that point it is no longer
9177 ** possible to add further SQL statements to the object or to re-attempt
9178 ** the analysis. The sqlite3expert object must still be freed using a call
9179 ** sqlite3_expert_destroy().
9180 */
9181 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
9182
9183 /*
9184 ** Return the total number of statements loaded using sqlite3_expert_sql().
9185 ** The total number of SQL statements may be different from the total number
9186 ** to calls to sqlite3_expert_sql().
9187 */
9188 int sqlite3_expert_count(sqlite3expert*);
9189
9190 /*
9191 ** Return a component of the report.
9192 **
9193 ** This function is called after sqlite3_expert_analyze() to extract the
9194 ** results of the analysis. Each call to this function returns either a
9195 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
9196 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
9197 ** #define constants defined below.
9198 **
9199 ** For some EXPERT_REPORT_* parameters, the buffer returned contains 
9200 ** information relating to a specific SQL statement. In these cases that
9201 ** SQL statement is identified by the value passed as the second argument.
9202 ** SQL statements are numbered from 0 in the order in which they are parsed.
9203 ** If an out-of-range value (less than zero or equal to or greater than the
9204 ** value returned by sqlite3_expert_count()) is passed as the second argument
9205 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
9206 **
9207 ** EXPERT_REPORT_SQL:
9208 **   Return the text of SQL statement iStmt.
9209 **
9210 ** EXPERT_REPORT_INDEXES:
9211 **   Return a buffer containing the CREATE INDEX statements for all recommended
9212 **   indexes for statement iStmt. If there are no new recommeded indexes, NULL 
9213 **   is returned.
9214 **
9215 ** EXPERT_REPORT_PLAN:
9216 **   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
9217 **   iStmt after the proposed indexes have been added to the database schema.
9218 **
9219 ** EXPERT_REPORT_CANDIDATES:
9220 **   Return a pointer to a buffer containing the CREATE INDEX statements 
9221 **   for all indexes that were tested (for all SQL statements). The iStmt
9222 **   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
9223 */
9224 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
9225
9226 /*
9227 ** Values for the third argument passed to sqlite3_expert_report().
9228 */
9229 #define EXPERT_REPORT_SQL        1
9230 #define EXPERT_REPORT_INDEXES    2
9231 #define EXPERT_REPORT_PLAN       3
9232 #define EXPERT_REPORT_CANDIDATES 4
9233
9234 /*
9235 ** Free an (sqlite3expert*) handle and all associated resources. There 
9236 ** should be one call to this function for each successful call to 
9237 ** sqlite3-expert_new().
9238 */
9239 void sqlite3_expert_destroy(sqlite3expert*);
9240
9241 #endif  /* !defined(SQLITEEXPERT_H) */
9242
9243 /************************* End ../ext/expert/sqlite3expert.h ********************/
9244 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
9245 /*
9246 ** 2017 April 09
9247 **
9248 ** The author disclaims copyright to this source code.  In place of
9249 ** a legal notice, here is a blessing:
9250 **
9251 **    May you do good and not evil.
9252 **    May you find forgiveness for yourself and forgive others.
9253 **    May you share freely, never taking more than you give.
9254 **
9255 *************************************************************************
9256 */
9257 /* #include "sqlite3expert.h" */
9258 #include <assert.h>
9259 #include <string.h>
9260 #include <stdio.h>
9261
9262 #if !defined(SQLITE_AMALGAMATION)
9263 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
9264 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
9265 #endif
9266 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
9267 # define ALWAYS(X)      (1)
9268 # define NEVER(X)       (0)
9269 #elif !defined(NDEBUG)
9270 # define ALWAYS(X)      ((X)?1:(assert(0),0))
9271 # define NEVER(X)       ((X)?(assert(0),1):0)
9272 #else
9273 # define ALWAYS(X)      (X)
9274 # define NEVER(X)       (X)
9275 #endif
9276 #endif /* !defined(SQLITE_AMALGAMATION) */
9277
9278
9279 #ifndef SQLITE_OMIT_VIRTUALTABLE 
9280
9281 /* typedef sqlite3_int64 i64; */
9282 /* typedef sqlite3_uint64 u64; */
9283
9284 typedef struct IdxColumn IdxColumn;
9285 typedef struct IdxConstraint IdxConstraint;
9286 typedef struct IdxScan IdxScan;
9287 typedef struct IdxStatement IdxStatement;
9288 typedef struct IdxTable IdxTable;
9289 typedef struct IdxWrite IdxWrite;
9290
9291 #define STRLEN  (int)strlen
9292
9293 /*
9294 ** A temp table name that we assume no user database will actually use.
9295 ** If this assumption proves incorrect triggers on the table with the
9296 ** conflicting name will be ignored.
9297 */
9298 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
9299
9300 /*
9301 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
9302 ** any other type of single-ended range constraint on a column).
9303 **
9304 ** pLink:
9305 **   Used to temporarily link IdxConstraint objects into lists while
9306 **   creating candidate indexes.
9307 */
9308 struct IdxConstraint {
9309   char *zColl;                    /* Collation sequence */
9310   int bRange;                     /* True for range, false for eq */
9311   int iCol;                       /* Constrained table column */
9312   int bFlag;                      /* Used by idxFindCompatible() */
9313   int bDesc;                      /* True if ORDER BY <expr> DESC */
9314   IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
9315   IdxConstraint *pLink;           /* See above */
9316 };
9317
9318 /*
9319 ** A single scan of a single table.
9320 */
9321 struct IdxScan {
9322   IdxTable *pTab;                 /* Associated table object */
9323   int iDb;                        /* Database containing table zTable */
9324   i64 covering;                   /* Mask of columns required for cov. index */
9325   IdxConstraint *pOrder;          /* ORDER BY columns */
9326   IdxConstraint *pEq;             /* List of == constraints */
9327   IdxConstraint *pRange;          /* List of < constraints */
9328   IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
9329 };
9330
9331 /*
9332 ** Information regarding a single database table. Extracted from 
9333 ** "PRAGMA table_info" by function idxGetTableInfo().
9334 */
9335 struct IdxColumn {
9336   char *zName;
9337   char *zColl;
9338   int iPk;
9339 };
9340 struct IdxTable {
9341   int nCol;
9342   char *zName;                    /* Table name */
9343   IdxColumn *aCol;
9344   IdxTable *pNext;                /* Next table in linked list of all tables */
9345 };
9346
9347 /*
9348 ** An object of the following type is created for each unique table/write-op
9349 ** seen. The objects are stored in a singly-linked list beginning at
9350 ** sqlite3expert.pWrite.
9351 */
9352 struct IdxWrite {
9353   IdxTable *pTab;
9354   int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
9355   IdxWrite *pNext;
9356 };
9357
9358 /*
9359 ** Each statement being analyzed is represented by an instance of this
9360 ** structure.
9361 */
9362 struct IdxStatement {
9363   int iId;                        /* Statement number */
9364   char *zSql;                     /* SQL statement */
9365   char *zIdx;                     /* Indexes */
9366   char *zEQP;                     /* Plan */
9367   IdxStatement *pNext;
9368 };
9369
9370
9371 /*
9372 ** A hash table for storing strings. With space for a payload string
9373 ** with each entry. Methods are:
9374 **
9375 **   idxHashInit()
9376 **   idxHashClear()
9377 **   idxHashAdd()
9378 **   idxHashSearch()
9379 */
9380 #define IDX_HASH_SIZE 1023
9381 typedef struct IdxHashEntry IdxHashEntry;
9382 typedef struct IdxHash IdxHash;
9383 struct IdxHashEntry {
9384   char *zKey;                     /* nul-terminated key */
9385   char *zVal;                     /* nul-terminated value string */
9386   char *zVal2;                    /* nul-terminated value string 2 */
9387   IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
9388   IdxHashEntry *pNext;            /* Next entry in hash */
9389 };
9390 struct IdxHash {
9391   IdxHashEntry *pFirst;
9392   IdxHashEntry *aHash[IDX_HASH_SIZE];
9393 };
9394
9395 /*
9396 ** sqlite3expert object.
9397 */
9398 struct sqlite3expert {
9399   int iSample;                    /* Percentage of tables to sample for stat1 */
9400   sqlite3 *db;                    /* User database */
9401   sqlite3 *dbm;                   /* In-memory db for this analysis */
9402   sqlite3 *dbv;                   /* Vtab schema for this analysis */
9403   IdxTable *pTable;               /* List of all IdxTable objects */
9404   IdxScan *pScan;                 /* List of scan objects */
9405   IdxWrite *pWrite;               /* List of write objects */
9406   IdxStatement *pStatement;       /* List of IdxStatement objects */
9407   int bRun;                       /* True once analysis has run */
9408   char **pzErrmsg;
9409   int rc;                         /* Error code from whereinfo hook */
9410   IdxHash hIdx;                   /* Hash containing all candidate indexes */
9411   char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
9412 };
9413
9414
9415 /*
9416 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). 
9417 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
9418 */
9419 static void *idxMalloc(int *pRc, int nByte){
9420   void *pRet;
9421   assert( *pRc==SQLITE_OK );
9422   assert( nByte>0 );
9423   pRet = sqlite3_malloc(nByte);
9424   if( pRet ){
9425     memset(pRet, 0, nByte);
9426   }else{
9427     *pRc = SQLITE_NOMEM;
9428   }
9429   return pRet;
9430 }
9431
9432 /*
9433 ** Initialize an IdxHash hash table.
9434 */
9435 static void idxHashInit(IdxHash *pHash){
9436   memset(pHash, 0, sizeof(IdxHash));
9437 }
9438
9439 /*
9440 ** Reset an IdxHash hash table.
9441 */
9442 static void idxHashClear(IdxHash *pHash){
9443   int i;
9444   for(i=0; i<IDX_HASH_SIZE; i++){
9445     IdxHashEntry *pEntry;
9446     IdxHashEntry *pNext;
9447     for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
9448       pNext = pEntry->pHashNext;
9449       sqlite3_free(pEntry->zVal2);
9450       sqlite3_free(pEntry);
9451     }
9452   }
9453   memset(pHash, 0, sizeof(IdxHash));
9454 }
9455
9456 /*
9457 ** Return the index of the hash bucket that the string specified by the
9458 ** arguments to this function belongs.
9459 */
9460 static int idxHashString(const char *z, int n){
9461   unsigned int ret = 0;
9462   int i;
9463   for(i=0; i<n; i++){
9464     ret += (ret<<3) + (unsigned char)(z[i]);
9465   }
9466   return (int)(ret % IDX_HASH_SIZE);
9467 }
9468
9469 /*
9470 ** If zKey is already present in the hash table, return non-zero and do
9471 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
9472 ** the hash table passed as the second argument. 
9473 */
9474 static int idxHashAdd(
9475   int *pRc, 
9476   IdxHash *pHash, 
9477   const char *zKey,
9478   const char *zVal
9479 ){
9480   int nKey = STRLEN(zKey);
9481   int iHash = idxHashString(zKey, nKey);
9482   int nVal = (zVal ? STRLEN(zVal) : 0);
9483   IdxHashEntry *pEntry;
9484   assert( iHash>=0 );
9485   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9486     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9487       return 1;
9488     }
9489   }
9490   pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
9491   if( pEntry ){
9492     pEntry->zKey = (char*)&pEntry[1];
9493     memcpy(pEntry->zKey, zKey, nKey);
9494     if( zVal ){
9495       pEntry->zVal = &pEntry->zKey[nKey+1];
9496       memcpy(pEntry->zVal, zVal, nVal);
9497     }
9498     pEntry->pHashNext = pHash->aHash[iHash];
9499     pHash->aHash[iHash] = pEntry;
9500
9501     pEntry->pNext = pHash->pFirst;
9502     pHash->pFirst = pEntry;
9503   }
9504   return 0;
9505 }
9506
9507 /*
9508 ** If zKey/nKey is present in the hash table, return a pointer to the 
9509 ** hash-entry object.
9510 */
9511 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
9512   int iHash;
9513   IdxHashEntry *pEntry;
9514   if( nKey<0 ) nKey = STRLEN(zKey);
9515   iHash = idxHashString(zKey, nKey);
9516   assert( iHash>=0 );
9517   for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9518     if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9519       return pEntry;
9520     }
9521   }
9522   return 0;
9523 }
9524
9525 /*
9526 ** If the hash table contains an entry with a key equal to the string
9527 ** passed as the final two arguments to this function, return a pointer
9528 ** to the payload string. Otherwise, if zKey/nKey is not present in the
9529 ** hash table, return NULL.
9530 */
9531 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
9532   IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
9533   if( pEntry ) return pEntry->zVal;
9534   return 0;
9535 }
9536
9537 /*
9538 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
9539 ** variable to point to a copy of nul-terminated string zColl.
9540 */
9541 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
9542   IdxConstraint *pNew;
9543   int nColl = STRLEN(zColl);
9544
9545   assert( *pRc==SQLITE_OK );
9546   pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
9547   if( pNew ){
9548     pNew->zColl = (char*)&pNew[1];
9549     memcpy(pNew->zColl, zColl, nColl+1);
9550   }
9551   return pNew;
9552 }
9553
9554 /*
9555 ** An error associated with database handle db has just occurred. Pass
9556 ** the error message to callback function xOut.
9557 */
9558 static void idxDatabaseError(
9559   sqlite3 *db,                    /* Database handle */
9560   char **pzErrmsg                 /* Write error here */
9561 ){
9562   *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
9563 }
9564
9565 /*
9566 ** Prepare an SQL statement.
9567 */
9568 static int idxPrepareStmt(
9569   sqlite3 *db,                    /* Database handle to compile against */
9570   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
9571   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
9572   const char *zSql                /* SQL statement to compile */
9573 ){
9574   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
9575   if( rc!=SQLITE_OK ){
9576     *ppStmt = 0;
9577     idxDatabaseError(db, pzErrmsg);
9578   }
9579   return rc;
9580 }
9581
9582 /*
9583 ** Prepare an SQL statement using the results of a printf() formatting.
9584 */
9585 static int idxPrintfPrepareStmt(
9586   sqlite3 *db,                    /* Database handle to compile against */
9587   sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
9588   char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
9589   const char *zFmt,               /* printf() format of SQL statement */
9590   ...                             /* Trailing printf() arguments */
9591 ){
9592   va_list ap;
9593   int rc;
9594   char *zSql;
9595   va_start(ap, zFmt);
9596   zSql = sqlite3_vmprintf(zFmt, ap);
9597   if( zSql==0 ){
9598     rc = SQLITE_NOMEM;
9599   }else{
9600     rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
9601     sqlite3_free(zSql);
9602   }
9603   va_end(ap);
9604   return rc;
9605 }
9606
9607
9608 /*************************************************************************
9609 ** Beginning of virtual table implementation.
9610 */
9611 typedef struct ExpertVtab ExpertVtab;
9612 struct ExpertVtab {
9613   sqlite3_vtab base;
9614   IdxTable *pTab;
9615   sqlite3expert *pExpert;
9616 };
9617
9618 typedef struct ExpertCsr ExpertCsr;
9619 struct ExpertCsr {
9620   sqlite3_vtab_cursor base;
9621   sqlite3_stmt *pData;
9622 };
9623
9624 static char *expertDequote(const char *zIn){
9625   int n = STRLEN(zIn);
9626   char *zRet = sqlite3_malloc(n);
9627
9628   assert( zIn[0]=='\'' );
9629   assert( zIn[n-1]=='\'' );
9630
9631   if( zRet ){
9632     int iOut = 0;
9633     int iIn = 0;
9634     for(iIn=1; iIn<(n-1); iIn++){
9635       if( zIn[iIn]=='\'' ){
9636         assert( zIn[iIn+1]=='\'' );
9637         iIn++;
9638       }
9639       zRet[iOut++] = zIn[iIn];
9640     }
9641     zRet[iOut] = '\0';
9642   }
9643
9644   return zRet;
9645 }
9646
9647 /* 
9648 ** This function is the implementation of both the xConnect and xCreate
9649 ** methods of the r-tree virtual table.
9650 **
9651 **   argv[0]   -> module name
9652 **   argv[1]   -> database name
9653 **   argv[2]   -> table name
9654 **   argv[...] -> column names...
9655 */
9656 static int expertConnect(
9657   sqlite3 *db,
9658   void *pAux,
9659   int argc, const char *const*argv,
9660   sqlite3_vtab **ppVtab,
9661   char **pzErr
9662 ){
9663   sqlite3expert *pExpert = (sqlite3expert*)pAux;
9664   ExpertVtab *p = 0;
9665   int rc;
9666
9667   if( argc!=4 ){
9668     *pzErr = sqlite3_mprintf("internal error!");
9669     rc = SQLITE_ERROR;
9670   }else{
9671     char *zCreateTable = expertDequote(argv[3]);
9672     if( zCreateTable ){
9673       rc = sqlite3_declare_vtab(db, zCreateTable);
9674       if( rc==SQLITE_OK ){
9675         p = idxMalloc(&rc, sizeof(ExpertVtab));
9676       }
9677       if( rc==SQLITE_OK ){
9678         p->pExpert = pExpert;
9679         p->pTab = pExpert->pTable;
9680         assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
9681       }
9682       sqlite3_free(zCreateTable);
9683     }else{
9684       rc = SQLITE_NOMEM;
9685     }
9686   }
9687
9688   *ppVtab = (sqlite3_vtab*)p;
9689   return rc;
9690 }
9691
9692 static int expertDisconnect(sqlite3_vtab *pVtab){
9693   ExpertVtab *p = (ExpertVtab*)pVtab;
9694   sqlite3_free(p);
9695   return SQLITE_OK;
9696 }
9697
9698 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
9699   ExpertVtab *p = (ExpertVtab*)pVtab;
9700   int rc = SQLITE_OK;
9701   int n = 0;
9702   IdxScan *pScan;
9703   const int opmask = 
9704     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
9705     SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
9706     SQLITE_INDEX_CONSTRAINT_LE;
9707
9708   pScan = idxMalloc(&rc, sizeof(IdxScan));
9709   if( pScan ){
9710     int i;
9711
9712     /* Link the new scan object into the list */
9713     pScan->pTab = p->pTab;
9714     pScan->pNextScan = p->pExpert->pScan;
9715     p->pExpert->pScan = pScan;
9716
9717     /* Add the constraints to the IdxScan object */
9718     for(i=0; i<pIdxInfo->nConstraint; i++){
9719       struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
9720       if( pCons->usable 
9721        && pCons->iColumn>=0 
9722        && p->pTab->aCol[pCons->iColumn].iPk==0
9723        && (pCons->op & opmask) 
9724       ){
9725         IdxConstraint *pNew;
9726         const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
9727         pNew = idxNewConstraint(&rc, zColl);
9728         if( pNew ){
9729           pNew->iCol = pCons->iColumn;
9730           if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
9731             pNew->pNext = pScan->pEq;
9732             pScan->pEq = pNew;
9733           }else{
9734             pNew->bRange = 1;
9735             pNew->pNext = pScan->pRange;
9736             pScan->pRange = pNew;
9737           }
9738         }
9739         n++;
9740         pIdxInfo->aConstraintUsage[i].argvIndex = n;
9741       }
9742     }
9743
9744     /* Add the ORDER BY to the IdxScan object */
9745     for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
9746       int iCol = pIdxInfo->aOrderBy[i].iColumn;
9747       if( iCol>=0 ){
9748         IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
9749         if( pNew ){
9750           pNew->iCol = iCol;
9751           pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
9752           pNew->pNext = pScan->pOrder;
9753           pNew->pLink = pScan->pOrder;
9754           pScan->pOrder = pNew;
9755           n++;
9756         }
9757       }
9758     }
9759   }
9760
9761   pIdxInfo->estimatedCost = 1000000.0 / (n+1);
9762   return rc;
9763 }
9764
9765 static int expertUpdate(
9766   sqlite3_vtab *pVtab, 
9767   int nData, 
9768   sqlite3_value **azData, 
9769   sqlite_int64 *pRowid
9770 ){
9771   (void)pVtab;
9772   (void)nData;
9773   (void)azData;
9774   (void)pRowid;
9775   return SQLITE_OK;
9776 }
9777
9778 /* 
9779 ** Virtual table module xOpen method.
9780 */
9781 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
9782   int rc = SQLITE_OK;
9783   ExpertCsr *pCsr;
9784   (void)pVTab;
9785   pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
9786   *ppCursor = (sqlite3_vtab_cursor*)pCsr;
9787   return rc;
9788 }
9789
9790 /* 
9791 ** Virtual table module xClose method.
9792 */
9793 static int expertClose(sqlite3_vtab_cursor *cur){
9794   ExpertCsr *pCsr = (ExpertCsr*)cur;
9795   sqlite3_finalize(pCsr->pData);
9796   sqlite3_free(pCsr);
9797   return SQLITE_OK;
9798 }
9799
9800 /*
9801 ** Virtual table module xEof method.
9802 **
9803 ** Return non-zero if the cursor does not currently point to a valid 
9804 ** record (i.e if the scan has finished), or zero otherwise.
9805 */
9806 static int expertEof(sqlite3_vtab_cursor *cur){
9807   ExpertCsr *pCsr = (ExpertCsr*)cur;
9808   return pCsr->pData==0;
9809 }
9810
9811 /* 
9812 ** Virtual table module xNext method.
9813 */
9814 static int expertNext(sqlite3_vtab_cursor *cur){
9815   ExpertCsr *pCsr = (ExpertCsr*)cur;
9816   int rc = SQLITE_OK;
9817
9818   assert( pCsr->pData );
9819   rc = sqlite3_step(pCsr->pData);
9820   if( rc!=SQLITE_ROW ){
9821     rc = sqlite3_finalize(pCsr->pData);
9822     pCsr->pData = 0;
9823   }else{
9824     rc = SQLITE_OK;
9825   }
9826
9827   return rc;
9828 }
9829
9830 /* 
9831 ** Virtual table module xRowid method.
9832 */
9833 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
9834   (void)cur;
9835   *pRowid = 0;
9836   return SQLITE_OK;
9837 }
9838
9839 /* 
9840 ** Virtual table module xColumn method.
9841 */
9842 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
9843   ExpertCsr *pCsr = (ExpertCsr*)cur;
9844   sqlite3_value *pVal;
9845   pVal = sqlite3_column_value(pCsr->pData, i);
9846   if( pVal ){
9847     sqlite3_result_value(ctx, pVal);
9848   }
9849   return SQLITE_OK;
9850 }
9851
9852 /* 
9853 ** Virtual table module xFilter method.
9854 */
9855 static int expertFilter(
9856   sqlite3_vtab_cursor *cur, 
9857   int idxNum, const char *idxStr,
9858   int argc, sqlite3_value **argv
9859 ){
9860   ExpertCsr *pCsr = (ExpertCsr*)cur;
9861   ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
9862   sqlite3expert *pExpert = pVtab->pExpert;
9863   int rc;
9864
9865   (void)idxNum;
9866   (void)idxStr;
9867   (void)argc;
9868   (void)argv;
9869   rc = sqlite3_finalize(pCsr->pData);
9870   pCsr->pData = 0;
9871   if( rc==SQLITE_OK ){
9872     rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
9873         "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
9874     );
9875   }
9876
9877   if( rc==SQLITE_OK ){
9878     rc = expertNext(cur);
9879   }
9880   return rc;
9881 }
9882
9883 static int idxRegisterVtab(sqlite3expert *p){
9884   static sqlite3_module expertModule = {
9885     2,                            /* iVersion */
9886     expertConnect,                /* xCreate - create a table */
9887     expertConnect,                /* xConnect - connect to an existing table */
9888     expertBestIndex,              /* xBestIndex - Determine search strategy */
9889     expertDisconnect,             /* xDisconnect - Disconnect from a table */
9890     expertDisconnect,             /* xDestroy - Drop a table */
9891     expertOpen,                   /* xOpen - open a cursor */
9892     expertClose,                  /* xClose - close a cursor */
9893     expertFilter,                 /* xFilter - configure scan constraints */
9894     expertNext,                   /* xNext - advance a cursor */
9895     expertEof,                    /* xEof */
9896     expertColumn,                 /* xColumn - read data */
9897     expertRowid,                  /* xRowid - read data */
9898     expertUpdate,                 /* xUpdate - write data */
9899     0,                            /* xBegin - begin transaction */
9900     0,                            /* xSync - sync transaction */
9901     0,                            /* xCommit - commit transaction */
9902     0,                            /* xRollback - rollback transaction */
9903     0,                            /* xFindFunction - function overloading */
9904     0,                            /* xRename - rename the table */
9905     0,                            /* xSavepoint */
9906     0,                            /* xRelease */
9907     0,                            /* xRollbackTo */
9908     0,                            /* xShadowName */
9909   };
9910
9911   return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
9912 }
9913 /*
9914 ** End of virtual table implementation.
9915 *************************************************************************/
9916 /*
9917 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
9918 ** is called, set it to the return value of sqlite3_finalize() before
9919 ** returning. Otherwise, discard the sqlite3_finalize() return value.
9920 */
9921 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
9922   int rc = sqlite3_finalize(pStmt);
9923   if( *pRc==SQLITE_OK ) *pRc = rc;
9924 }
9925
9926 /*
9927 ** Attempt to allocate an IdxTable structure corresponding to table zTab
9928 ** in the main database of connection db. If successful, set (*ppOut) to
9929 ** point to the new object and return SQLITE_OK. Otherwise, return an
9930 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
9931 ** set to point to an error string.
9932 **
9933 ** It is the responsibility of the caller to eventually free either the
9934 ** IdxTable object or error message using sqlite3_free().
9935 */
9936 static int idxGetTableInfo(
9937   sqlite3 *db,                    /* Database connection to read details from */
9938   const char *zTab,               /* Table name */
9939   IdxTable **ppOut,               /* OUT: New object (if successful) */
9940   char **pzErrmsg                 /* OUT: Error message (if not) */
9941 ){
9942   sqlite3_stmt *p1 = 0;
9943   int nCol = 0;
9944   int nTab = STRLEN(zTab);
9945   int nByte = sizeof(IdxTable) + nTab + 1;
9946   IdxTable *pNew = 0;
9947   int rc, rc2;
9948   char *pCsr = 0;
9949   int nPk = 0;
9950
9951   rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9952   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9953     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9954     const char *zColSeq = 0;
9955     nByte += 1 + STRLEN(zCol);
9956     rc = sqlite3_table_column_metadata(
9957         db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9958     );
9959     if( zColSeq==0 ) zColSeq = "binary";
9960     nByte += 1 + STRLEN(zColSeq);
9961     nCol++;
9962     nPk += (sqlite3_column_int(p1, 5)>0);
9963   }
9964   rc2 = sqlite3_reset(p1);
9965   if( rc==SQLITE_OK ) rc = rc2;
9966
9967   nByte += sizeof(IdxColumn) * nCol;
9968   if( rc==SQLITE_OK ){
9969     pNew = idxMalloc(&rc, nByte);
9970   }
9971   if( rc==SQLITE_OK ){
9972     pNew->aCol = (IdxColumn*)&pNew[1];
9973     pNew->nCol = nCol;
9974     pCsr = (char*)&pNew->aCol[nCol];
9975   }
9976
9977   nCol = 0;
9978   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9979     const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9980     const char *zColSeq = 0;
9981     int nCopy = STRLEN(zCol) + 1;
9982     pNew->aCol[nCol].zName = pCsr;
9983     pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9984     memcpy(pCsr, zCol, nCopy);
9985     pCsr += nCopy;
9986
9987     rc = sqlite3_table_column_metadata(
9988         db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9989     );
9990     if( rc==SQLITE_OK ){
9991       if( zColSeq==0 ) zColSeq = "binary";
9992       nCopy = STRLEN(zColSeq) + 1;
9993       pNew->aCol[nCol].zColl = pCsr;
9994       memcpy(pCsr, zColSeq, nCopy);
9995       pCsr += nCopy;
9996     }
9997
9998     nCol++;
9999   }
10000   idxFinalize(&rc, p1);
10001
10002   if( rc!=SQLITE_OK ){
10003     sqlite3_free(pNew);
10004     pNew = 0;
10005   }else if( ALWAYS(pNew!=0) ){
10006     pNew->zName = pCsr;
10007     if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1);
10008   }
10009
10010   *ppOut = pNew;
10011   return rc;
10012 }
10013
10014 /*
10015 ** This function is a no-op if *pRc is set to anything other than 
10016 ** SQLITE_OK when it is called.
10017 **
10018 ** If *pRc is initially set to SQLITE_OK, then the text specified by
10019 ** the printf() style arguments is appended to zIn and the result returned
10020 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
10021 ** zIn before returning.
10022 */
10023 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
10024   va_list ap;
10025   char *zAppend = 0;
10026   char *zRet = 0;
10027   int nIn = zIn ? STRLEN(zIn) : 0;
10028   int nAppend = 0;
10029   va_start(ap, zFmt);
10030   if( *pRc==SQLITE_OK ){
10031     zAppend = sqlite3_vmprintf(zFmt, ap);
10032     if( zAppend ){
10033       nAppend = STRLEN(zAppend);
10034       zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
10035     }
10036     if( zAppend && zRet ){
10037       if( nIn ) memcpy(zRet, zIn, nIn);
10038       memcpy(&zRet[nIn], zAppend, nAppend+1);
10039     }else{
10040       sqlite3_free(zRet);
10041       zRet = 0;
10042       *pRc = SQLITE_NOMEM;
10043     }
10044     sqlite3_free(zAppend);
10045     sqlite3_free(zIn);
10046   }
10047   va_end(ap);
10048   return zRet;
10049 }
10050
10051 /*
10052 ** Return true if zId must be quoted in order to use it as an SQL
10053 ** identifier, or false otherwise.
10054 */
10055 static int idxIdentifierRequiresQuotes(const char *zId){
10056   int i;
10057   for(i=0; zId[i]; i++){
10058     if( !(zId[i]=='_')
10059      && !(zId[i]>='0' && zId[i]<='9')
10060      && !(zId[i]>='a' && zId[i]<='z')
10061      && !(zId[i]>='A' && zId[i]<='Z')
10062     ){
10063       return 1;
10064     }
10065   }
10066   return 0;
10067 }
10068
10069 /*
10070 ** This function appends an index column definition suitable for constraint
10071 ** pCons to the string passed as zIn and returns the result.
10072 */
10073 static char *idxAppendColDefn(
10074   int *pRc,                       /* IN/OUT: Error code */
10075   char *zIn,                      /* Column defn accumulated so far */
10076   IdxTable *pTab,                 /* Table index will be created on */
10077   IdxConstraint *pCons
10078 ){
10079   char *zRet = zIn;
10080   IdxColumn *p = &pTab->aCol[pCons->iCol];
10081   if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
10082
10083   if( idxIdentifierRequiresQuotes(p->zName) ){
10084     zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
10085   }else{
10086     zRet = idxAppendText(pRc, zRet, "%s", p->zName);
10087   }
10088
10089   if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
10090     if( idxIdentifierRequiresQuotes(pCons->zColl) ){
10091       zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
10092     }else{
10093       zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
10094     }
10095   }
10096
10097   if( pCons->bDesc ){
10098     zRet = idxAppendText(pRc, zRet, " DESC");
10099   }
10100   return zRet;
10101 }
10102
10103 /*
10104 ** Search database dbm for an index compatible with the one idxCreateFromCons()
10105 ** would create from arguments pScan, pEq and pTail. If no error occurs and 
10106 ** such an index is found, return non-zero. Or, if no such index is found,
10107 ** return zero.
10108 **
10109 ** If an error occurs, set *pRc to an SQLite error code and return zero.
10110 */
10111 static int idxFindCompatible(
10112   int *pRc,                       /* OUT: Error code */
10113   sqlite3* dbm,                   /* Database to search */
10114   IdxScan *pScan,                 /* Scan for table to search for index on */
10115   IdxConstraint *pEq,             /* List of == constraints */
10116   IdxConstraint *pTail            /* List of range constraints */
10117 ){
10118   const char *zTbl = pScan->pTab->zName;
10119   sqlite3_stmt *pIdxList = 0;
10120   IdxConstraint *pIter;
10121   int nEq = 0;                    /* Number of elements in pEq */
10122   int rc;
10123
10124   /* Count the elements in list pEq */
10125   for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
10126
10127   rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
10128   while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
10129     int bMatch = 1;
10130     IdxConstraint *pT = pTail;
10131     sqlite3_stmt *pInfo = 0;
10132     const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
10133
10134     /* Zero the IdxConstraint.bFlag values in the pEq list */
10135     for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
10136
10137     rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
10138     while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
10139       int iIdx = sqlite3_column_int(pInfo, 0);
10140       int iCol = sqlite3_column_int(pInfo, 1);
10141       const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
10142
10143       if( iIdx<nEq ){
10144         for(pIter=pEq; pIter; pIter=pIter->pLink){
10145           if( pIter->bFlag ) continue;
10146           if( pIter->iCol!=iCol ) continue;
10147           if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
10148           pIter->bFlag = 1;
10149           break;
10150         }
10151         if( pIter==0 ){
10152           bMatch = 0;
10153           break;
10154         }
10155       }else{
10156         if( pT ){
10157           if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
10158             bMatch = 0;
10159             break;
10160           }
10161           pT = pT->pLink;
10162         }
10163       }
10164     }
10165     idxFinalize(&rc, pInfo);
10166
10167     if( rc==SQLITE_OK && bMatch ){
10168       sqlite3_finalize(pIdxList);
10169       return 1;
10170     }
10171   }
10172   idxFinalize(&rc, pIdxList);
10173
10174   *pRc = rc;
10175   return 0;
10176 }
10177
10178 /* Callback for sqlite3_exec() with query with leading count(*) column.
10179  * The first argument is expected to be an int*, referent to be incremented
10180  * if that leading column is not exactly '0'.
10181  */
10182 static int countNonzeros(void* pCount, int nc,
10183                          char* azResults[], char* azColumns[]){
10184   (void)azColumns;  /* Suppress unused parameter warning */
10185   if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){
10186     *((int *)pCount) += 1;
10187   }
10188   return 0;
10189 }
10190
10191 static int idxCreateFromCons(
10192   sqlite3expert *p,
10193   IdxScan *pScan,
10194   IdxConstraint *pEq, 
10195   IdxConstraint *pTail
10196 ){
10197   sqlite3 *dbm = p->dbm;
10198   int rc = SQLITE_OK;
10199   if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
10200     IdxTable *pTab = pScan->pTab;
10201     char *zCols = 0;
10202     char *zIdx = 0;
10203     IdxConstraint *pCons;
10204     unsigned int h = 0;
10205     const char *zFmt;
10206
10207     for(pCons=pEq; pCons; pCons=pCons->pLink){
10208       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10209     }
10210     for(pCons=pTail; pCons; pCons=pCons->pLink){
10211       zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10212     }
10213
10214     if( rc==SQLITE_OK ){
10215       /* Hash the list of columns to come up with a name for the index */
10216       const char *zTable = pScan->pTab->zName;
10217       int quoteTable = idxIdentifierRequiresQuotes(zTable);
10218       char *zName = 0;          /* Index name */
10219       int collisions = 0;
10220       do{
10221         int i;
10222         char *zFind;
10223         for(i=0; zCols[i]; i++){
10224           h += ((h<<3) + zCols[i]);
10225         }
10226         sqlite3_free(zName);
10227         zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
10228         if( zName==0 ) break;
10229         /* Is is unique among table, view and index names? */
10230         zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
10231           " AND type in ('index','table','view')";
10232         zFind = sqlite3_mprintf(zFmt, zName);
10233         i = 0;
10234         rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0);
10235         assert(rc==SQLITE_OK);
10236         sqlite3_free(zFind);
10237         if( i==0 ){
10238           collisions = 0;
10239           break;
10240         }
10241         ++collisions;
10242       }while( collisions<50 && zName!=0 );
10243       if( collisions ){
10244         /* This return means "Gave up trying to find a unique index name." */
10245         rc = SQLITE_BUSY_TIMEOUT;
10246       }else if( zName==0 ){
10247         rc = SQLITE_NOMEM;
10248       }else{
10249         if( quoteTable ){
10250           zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)";
10251         }else{
10252           zFmt = "CREATE INDEX %s ON %s(%s)";
10253         }
10254         zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
10255         if( !zIdx ){
10256           rc = SQLITE_NOMEM;
10257         }else{
10258           rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
10259           if( rc!=SQLITE_OK ){
10260             rc = SQLITE_BUSY_TIMEOUT;
10261           }else{
10262             idxHashAdd(&rc, &p->hIdx, zName, zIdx);
10263           }
10264         }
10265         sqlite3_free(zName);
10266         sqlite3_free(zIdx);
10267       }
10268     }
10269
10270     sqlite3_free(zCols);
10271   }
10272   return rc;
10273 }
10274
10275 /*
10276 ** Return true if list pList (linked by IdxConstraint.pLink) contains
10277 ** a constraint compatible with *p. Otherwise return false.
10278 */
10279 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
10280   IdxConstraint *pCmp;
10281   for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
10282     if( p->iCol==pCmp->iCol ) return 1;
10283   }
10284   return 0;
10285 }
10286
10287 static int idxCreateFromWhere(
10288   sqlite3expert *p, 
10289   IdxScan *pScan,                 /* Create indexes for this scan */
10290   IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
10291 ){
10292   IdxConstraint *p1 = 0;
10293   IdxConstraint *pCon;
10294   int rc;
10295
10296   /* Gather up all the == constraints. */
10297   for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
10298     if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10299       pCon->pLink = p1;
10300       p1 = pCon;
10301     }
10302   }
10303
10304   /* Create an index using the == constraints collected above. And the
10305   ** range constraint/ORDER BY terms passed in by the caller, if any. */
10306   rc = idxCreateFromCons(p, pScan, p1, pTail);
10307
10308   /* If no range/ORDER BY passed by the caller, create a version of the
10309   ** index for each range constraint.  */
10310   if( pTail==0 ){
10311     for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
10312       assert( pCon->pLink==0 );
10313       if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10314         rc = idxCreateFromCons(p, pScan, p1, pCon);
10315       }
10316     }
10317   }
10318
10319   return rc;
10320 }
10321
10322 /*
10323 ** Create candidate indexes in database [dbm] based on the data in 
10324 ** linked-list pScan.
10325 */
10326 static int idxCreateCandidates(sqlite3expert *p){
10327   int rc = SQLITE_OK;
10328   IdxScan *pIter;
10329
10330   for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
10331     rc = idxCreateFromWhere(p, pIter, 0);
10332     if( rc==SQLITE_OK && pIter->pOrder ){
10333       rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
10334     }
10335   }
10336
10337   return rc;
10338 }
10339
10340 /*
10341 ** Free all elements of the linked list starting at pConstraint.
10342 */
10343 static void idxConstraintFree(IdxConstraint *pConstraint){
10344   IdxConstraint *pNext;
10345   IdxConstraint *p;
10346
10347   for(p=pConstraint; p; p=pNext){
10348     pNext = p->pNext;
10349     sqlite3_free(p);
10350   }
10351 }
10352
10353 /*
10354 ** Free all elements of the linked list starting from pScan up until pLast
10355 ** (pLast is not freed).
10356 */
10357 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
10358   IdxScan *p;
10359   IdxScan *pNext;
10360   for(p=pScan; p!=pLast; p=pNext){
10361     pNext = p->pNextScan;
10362     idxConstraintFree(p->pOrder);
10363     idxConstraintFree(p->pEq);
10364     idxConstraintFree(p->pRange);
10365     sqlite3_free(p);
10366   }
10367 }
10368
10369 /*
10370 ** Free all elements of the linked list starting from pStatement up 
10371 ** until pLast (pLast is not freed).
10372 */
10373 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
10374   IdxStatement *p;
10375   IdxStatement *pNext;
10376   for(p=pStatement; p!=pLast; p=pNext){
10377     pNext = p->pNext;
10378     sqlite3_free(p->zEQP);
10379     sqlite3_free(p->zIdx);
10380     sqlite3_free(p);
10381   }
10382 }
10383
10384 /*
10385 ** Free the linked list of IdxTable objects starting at pTab.
10386 */
10387 static void idxTableFree(IdxTable *pTab){
10388   IdxTable *pIter;
10389   IdxTable *pNext;
10390   for(pIter=pTab; pIter; pIter=pNext){
10391     pNext = pIter->pNext;
10392     sqlite3_free(pIter);
10393   }
10394 }
10395
10396 /*
10397 ** Free the linked list of IdxWrite objects starting at pTab.
10398 */
10399 static void idxWriteFree(IdxWrite *pTab){
10400   IdxWrite *pIter;
10401   IdxWrite *pNext;
10402   for(pIter=pTab; pIter; pIter=pNext){
10403     pNext = pIter->pNext;
10404     sqlite3_free(pIter);
10405   }
10406 }
10407
10408
10409
10410 /*
10411 ** This function is called after candidate indexes have been created. It
10412 ** runs all the queries to see which indexes they prefer, and populates
10413 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
10414 */
10415 int idxFindIndexes(
10416   sqlite3expert *p,
10417   char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
10418 ){
10419   IdxStatement *pStmt;
10420   sqlite3 *dbm = p->dbm;
10421   int rc = SQLITE_OK;
10422
10423   IdxHash hIdx;
10424   idxHashInit(&hIdx);
10425
10426   for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
10427     IdxHashEntry *pEntry;
10428     sqlite3_stmt *pExplain = 0;
10429     idxHashClear(&hIdx);
10430     rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
10431         "EXPLAIN QUERY PLAN %s", pStmt->zSql
10432     );
10433     while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
10434       /* int iId = sqlite3_column_int(pExplain, 0); */
10435       /* int iParent = sqlite3_column_int(pExplain, 1); */
10436       /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
10437       const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
10438       int nDetail;
10439       int i;
10440
10441       if( !zDetail ) continue;
10442       nDetail = STRLEN(zDetail);
10443
10444       for(i=0; i<nDetail; i++){
10445         const char *zIdx = 0;
10446         if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
10447           zIdx = &zDetail[i+13];
10448         }else if( i+22<nDetail 
10449             && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 
10450         ){
10451           zIdx = &zDetail[i+22];
10452         }
10453         if( zIdx ){
10454           const char *zSql;
10455           int nIdx = 0;
10456           while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
10457             nIdx++;
10458           }
10459           zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
10460           if( zSql ){
10461             idxHashAdd(&rc, &hIdx, zSql, 0);
10462             if( rc ) goto find_indexes_out;
10463           }
10464           break;
10465         }
10466       }
10467
10468       if( zDetail[0]!='-' ){
10469         pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
10470       }
10471     }
10472
10473     for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10474       pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
10475     }
10476
10477     idxFinalize(&rc, pExplain);
10478   }
10479
10480  find_indexes_out:
10481   idxHashClear(&hIdx);
10482   return rc;
10483 }
10484
10485 static int idxAuthCallback(
10486   void *pCtx,
10487   int eOp,
10488   const char *z3,
10489   const char *z4,
10490   const char *zDb,
10491   const char *zTrigger
10492 ){
10493   int rc = SQLITE_OK;
10494   (void)z4;
10495   (void)zTrigger;
10496   if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
10497     if( sqlite3_stricmp(zDb, "main")==0 ){
10498       sqlite3expert *p = (sqlite3expert*)pCtx;
10499       IdxTable *pTab;
10500       for(pTab=p->pTable; pTab; pTab=pTab->pNext){
10501         if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
10502       }
10503       if( pTab ){
10504         IdxWrite *pWrite;
10505         for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
10506           if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
10507         }
10508         if( pWrite==0 ){
10509           pWrite = idxMalloc(&rc, sizeof(IdxWrite));
10510           if( rc==SQLITE_OK ){
10511             pWrite->pTab = pTab;
10512             pWrite->eOp = eOp;
10513             pWrite->pNext = p->pWrite;
10514             p->pWrite = pWrite;
10515           }
10516         }
10517       }
10518     }
10519   }
10520   return rc;
10521 }
10522
10523 static int idxProcessOneTrigger(
10524   sqlite3expert *p, 
10525   IdxWrite *pWrite, 
10526   char **pzErr
10527 ){
10528   static const char *zInt = UNIQUE_TABLE_NAME;
10529   static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
10530   IdxTable *pTab = pWrite->pTab;
10531   const char *zTab = pTab->zName;
10532   const char *zSql = 
10533     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
10534     "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
10535     "ORDER BY type;";
10536   sqlite3_stmt *pSelect = 0;
10537   int rc = SQLITE_OK;
10538   char *zWrite = 0;
10539
10540   /* Create the table and its triggers in the temp schema */
10541   rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
10542   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
10543     const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
10544     rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
10545   }
10546   idxFinalize(&rc, pSelect);
10547
10548   /* Rename the table in the temp schema to zInt */
10549   if( rc==SQLITE_OK ){
10550     char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
10551     if( z==0 ){
10552       rc = SQLITE_NOMEM;
10553     }else{
10554       rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
10555       sqlite3_free(z);
10556     }
10557   }
10558
10559   switch( pWrite->eOp ){
10560     case SQLITE_INSERT: {
10561       int i;
10562       zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
10563       for(i=0; i<pTab->nCol; i++){
10564         zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
10565       }
10566       zWrite = idxAppendText(&rc, zWrite, ")");
10567       break;
10568     }
10569     case SQLITE_UPDATE: {
10570       int i;
10571       zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
10572       for(i=0; i<pTab->nCol; i++){
10573         zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", 
10574             pTab->aCol[i].zName
10575         );
10576       }
10577       break;
10578     }
10579     default: {
10580       assert( pWrite->eOp==SQLITE_DELETE );
10581       if( rc==SQLITE_OK ){
10582         zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
10583         if( zWrite==0 ) rc = SQLITE_NOMEM;
10584       }
10585     }
10586   }
10587
10588   if( rc==SQLITE_OK ){
10589     sqlite3_stmt *pX = 0;
10590     rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
10591     idxFinalize(&rc, pX);
10592     if( rc!=SQLITE_OK ){
10593       idxDatabaseError(p->dbv, pzErr);
10594     }
10595   }
10596   sqlite3_free(zWrite);
10597
10598   if( rc==SQLITE_OK ){
10599     rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
10600   }
10601
10602   return rc;
10603 }
10604
10605 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
10606   int rc = SQLITE_OK;
10607   IdxWrite *pEnd = 0;
10608   IdxWrite *pFirst = p->pWrite;
10609
10610   while( rc==SQLITE_OK && pFirst!=pEnd ){
10611     IdxWrite *pIter;
10612     for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
10613       rc = idxProcessOneTrigger(p, pIter, pzErr);
10614     }
10615     pEnd = pFirst;
10616     pFirst = p->pWrite;
10617   }
10618
10619   return rc;
10620 }
10621
10622
10623 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
10624   int rc = idxRegisterVtab(p);
10625   sqlite3_stmt *pSchema = 0;
10626
10627   /* For each table in the main db schema:
10628   **
10629   **   1) Add an entry to the p->pTable list, and
10630   **   2) Create the equivalent virtual table in dbv.
10631   */
10632   rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
10633       "SELECT type, name, sql, 1 FROM sqlite_schema "
10634       "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
10635       " UNION ALL "
10636       "SELECT type, name, sql, 2 FROM sqlite_schema "
10637       "WHERE type = 'trigger'"
10638       "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
10639       "ORDER BY 4, 1"
10640   );
10641   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
10642     const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
10643     const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
10644     const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
10645
10646     if( zType[0]=='v' || zType[1]=='r' ){
10647       rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10648     }else{
10649       IdxTable *pTab;
10650       rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
10651       if( rc==SQLITE_OK ){
10652         int i;
10653         char *zInner = 0;
10654         char *zOuter = 0;
10655         pTab->pNext = p->pTable;
10656         p->pTable = pTab;
10657
10658         /* The statement the vtab will pass to sqlite3_declare_vtab() */
10659         zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
10660         for(i=0; i<pTab->nCol; i++){
10661           zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", 
10662               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
10663           );
10664         }
10665         zInner = idxAppendText(&rc, zInner, ")");
10666
10667         /* The CVT statement to create the vtab */
10668         zOuter = idxAppendText(&rc, 0, 
10669             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
10670         );
10671         if( rc==SQLITE_OK ){
10672           rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
10673         }
10674         sqlite3_free(zInner);
10675         sqlite3_free(zOuter);
10676       }
10677     }
10678   }
10679   idxFinalize(&rc, pSchema);
10680   return rc;
10681 }
10682
10683 struct IdxSampleCtx {
10684   int iTarget;
10685   double target;                  /* Target nRet/nRow value */
10686   double nRow;                    /* Number of rows seen */
10687   double nRet;                    /* Number of rows returned */
10688 };
10689
10690 static void idxSampleFunc(
10691   sqlite3_context *pCtx,
10692   int argc,
10693   sqlite3_value **argv
10694 ){
10695   struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
10696   int bRet;
10697
10698   (void)argv;
10699   assert( argc==0 );
10700   if( p->nRow==0.0 ){
10701     bRet = 1;
10702   }else{
10703     bRet = (p->nRet / p->nRow) <= p->target;
10704     if( bRet==0 ){
10705       unsigned short rnd;
10706       sqlite3_randomness(2, (void*)&rnd);
10707       bRet = ((int)rnd % 100) <= p->iTarget;
10708     }
10709   }
10710
10711   sqlite3_result_int(pCtx, bRet);
10712   p->nRow += 1.0;
10713   p->nRet += (double)bRet;
10714 }
10715
10716 struct IdxRemCtx {
10717   int nSlot;
10718   struct IdxRemSlot {
10719     int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
10720     i64 iVal;                     /* SQLITE_INTEGER value */
10721     double rVal;                  /* SQLITE_FLOAT value */
10722     int nByte;                    /* Bytes of space allocated at z */
10723     int n;                        /* Size of buffer z */
10724     char *z;                      /* SQLITE_TEXT/BLOB value */
10725   } aSlot[1];
10726 };
10727
10728 /*
10729 ** Implementation of scalar function rem().
10730 */
10731 static void idxRemFunc(
10732   sqlite3_context *pCtx,
10733   int argc,
10734   sqlite3_value **argv
10735 ){
10736   struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
10737   struct IdxRemSlot *pSlot;
10738   int iSlot;
10739   assert( argc==2 );
10740
10741   iSlot = sqlite3_value_int(argv[0]);
10742   assert( iSlot<=p->nSlot );
10743   pSlot = &p->aSlot[iSlot];
10744
10745   switch( pSlot->eType ){
10746     case SQLITE_NULL:
10747       /* no-op */
10748       break;
10749
10750     case SQLITE_INTEGER:
10751       sqlite3_result_int64(pCtx, pSlot->iVal);
10752       break;
10753
10754     case SQLITE_FLOAT:
10755       sqlite3_result_double(pCtx, pSlot->rVal);
10756       break;
10757
10758     case SQLITE_BLOB:
10759       sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10760       break;
10761
10762     case SQLITE_TEXT:
10763       sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10764       break;
10765   }
10766
10767   pSlot->eType = sqlite3_value_type(argv[1]);
10768   switch( pSlot->eType ){
10769     case SQLITE_NULL:
10770       /* no-op */
10771       break;
10772
10773     case SQLITE_INTEGER:
10774       pSlot->iVal = sqlite3_value_int64(argv[1]);
10775       break;
10776
10777     case SQLITE_FLOAT:
10778       pSlot->rVal = sqlite3_value_double(argv[1]);
10779       break;
10780
10781     case SQLITE_BLOB:
10782     case SQLITE_TEXT: {
10783       int nByte = sqlite3_value_bytes(argv[1]);
10784       if( nByte>pSlot->nByte ){
10785         char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
10786         if( zNew==0 ){
10787           sqlite3_result_error_nomem(pCtx);
10788           return;
10789         }
10790         pSlot->nByte = nByte*2;
10791         pSlot->z = zNew;
10792       }
10793       pSlot->n = nByte;
10794       if( pSlot->eType==SQLITE_BLOB ){
10795         memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
10796       }else{
10797         memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
10798       }
10799       break;
10800     }
10801   }
10802 }
10803
10804 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
10805   int rc = SQLITE_OK;
10806   const char *zMax = 
10807     "SELECT max(i.seqno) FROM "
10808     "  sqlite_schema AS s, "
10809     "  pragma_index_list(s.name) AS l, "
10810     "  pragma_index_info(l.name) AS i "
10811     "WHERE s.type = 'table'";
10812   sqlite3_stmt *pMax = 0;
10813
10814   *pnMax = 0;
10815   rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
10816   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
10817     *pnMax = sqlite3_column_int(pMax, 0) + 1;
10818   }
10819   idxFinalize(&rc, pMax);
10820
10821   return rc;
10822 }
10823
10824 static int idxPopulateOneStat1(
10825   sqlite3expert *p,
10826   sqlite3_stmt *pIndexXInfo,
10827   sqlite3_stmt *pWriteStat,
10828   const char *zTab,
10829   const char *zIdx,
10830   char **pzErr
10831 ){
10832   char *zCols = 0;
10833   char *zOrder = 0;
10834   char *zQuery = 0;
10835   int nCol = 0;
10836   int i;
10837   sqlite3_stmt *pQuery = 0;
10838   int *aStat = 0;
10839   int rc = SQLITE_OK;
10840
10841   assert( p->iSample>0 );
10842
10843   /* Formulate the query text */
10844   sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
10845   while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
10846     const char *zComma = zCols==0 ? "" : ", ";
10847     const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
10848     const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
10849     zCols = idxAppendText(&rc, zCols, 
10850         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
10851     );
10852     zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
10853   }
10854   sqlite3_reset(pIndexXInfo);
10855   if( rc==SQLITE_OK ){
10856     if( p->iSample==100 ){
10857       zQuery = sqlite3_mprintf(
10858           "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
10859       );
10860     }else{
10861       zQuery = sqlite3_mprintf(
10862           "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
10863       );
10864     }
10865   }
10866   sqlite3_free(zCols);
10867   sqlite3_free(zOrder);
10868
10869   /* Formulate the query text */
10870   if( rc==SQLITE_OK ){
10871     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10872     rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
10873   }
10874   sqlite3_free(zQuery);
10875
10876   if( rc==SQLITE_OK ){
10877     aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
10878   }
10879   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10880     IdxHashEntry *pEntry;
10881     char *zStat = 0;
10882     for(i=0; i<=nCol; i++) aStat[i] = 1;
10883     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10884       aStat[0]++;
10885       for(i=0; i<nCol; i++){
10886         if( sqlite3_column_int(pQuery, i)==0 ) break;
10887       }
10888       for(/*no-op*/; i<nCol; i++){
10889         aStat[i+1]++;
10890       }
10891     }
10892
10893     if( rc==SQLITE_OK ){
10894       int s0 = aStat[0];
10895       zStat = sqlite3_mprintf("%d", s0);
10896       if( zStat==0 ) rc = SQLITE_NOMEM;
10897       for(i=1; rc==SQLITE_OK && i<=nCol; i++){
10898         zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
10899       }
10900     }
10901
10902     if( rc==SQLITE_OK ){
10903       sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
10904       sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
10905       sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
10906       sqlite3_step(pWriteStat);
10907       rc = sqlite3_reset(pWriteStat);
10908     }
10909
10910     pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
10911     if( pEntry ){
10912       assert( pEntry->zVal2==0 );
10913       pEntry->zVal2 = zStat;
10914     }else{
10915       sqlite3_free(zStat);
10916     }
10917   }
10918   sqlite3_free(aStat);
10919   idxFinalize(&rc, pQuery);
10920
10921   return rc;
10922 }
10923
10924 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
10925   int rc;
10926   char *zSql;
10927
10928   rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10929   if( rc!=SQLITE_OK ) return rc;
10930
10931   zSql = sqlite3_mprintf(
10932       "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
10933   );
10934   if( zSql==0 ) return SQLITE_NOMEM;
10935   rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
10936   sqlite3_free(zSql);
10937
10938   return rc;
10939 }
10940
10941 /*
10942 ** This function is called as part of sqlite3_expert_analyze(). Candidate
10943 ** indexes have already been created in database sqlite3expert.dbm, this
10944 ** function populates sqlite_stat1 table in the same database.
10945 **
10946 ** The stat1 data is generated by querying the 
10947 */
10948 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
10949   int rc = SQLITE_OK;
10950   int nMax =0;
10951   struct IdxRemCtx *pCtx = 0;
10952   struct IdxSampleCtx samplectx; 
10953   int i;
10954   i64 iPrev = -100000;
10955   sqlite3_stmt *pAllIndex = 0;
10956   sqlite3_stmt *pIndexXInfo = 0;
10957   sqlite3_stmt *pWrite = 0;
10958
10959   const char *zAllIndex =
10960     "SELECT s.rowid, s.name, l.name FROM "
10961     "  sqlite_schema AS s, "
10962     "  pragma_index_list(s.name) AS l "
10963     "WHERE s.type = 'table'";
10964   const char *zIndexXInfo = 
10965     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
10966   const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
10967
10968   /* If iSample==0, no sqlite_stat1 data is required. */
10969   if( p->iSample==0 ) return SQLITE_OK;
10970
10971   rc = idxLargestIndex(p->dbm, &nMax, pzErr);
10972   if( nMax<=0 || rc!=SQLITE_OK ) return rc;
10973
10974   rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
10975
10976   if( rc==SQLITE_OK ){
10977     int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
10978     pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
10979   }
10980
10981   if( rc==SQLITE_OK ){
10982     sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10983     rc = sqlite3_create_function(
10984         dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
10985     );
10986   }
10987   if( rc==SQLITE_OK ){
10988     rc = sqlite3_create_function(
10989         p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
10990     );
10991   }
10992
10993   if( rc==SQLITE_OK ){
10994     pCtx->nSlot = nMax+1;
10995     rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
10996   }
10997   if( rc==SQLITE_OK ){
10998     rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
10999   }
11000   if( rc==SQLITE_OK ){
11001     rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
11002   }
11003
11004   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
11005     i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
11006     const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
11007     const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
11008     if( p->iSample<100 && iPrev!=iRowid ){
11009       samplectx.target = (double)p->iSample / 100.0;
11010       samplectx.iTarget = p->iSample;
11011       samplectx.nRow = 0.0;
11012       samplectx.nRet = 0.0;
11013       rc = idxBuildSampleTable(p, zTab);
11014       if( rc!=SQLITE_OK ) break;
11015     }
11016     rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
11017     iPrev = iRowid;
11018   }
11019   if( rc==SQLITE_OK && p->iSample<100 ){
11020     rc = sqlite3_exec(p->dbv, 
11021         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
11022     );
11023   }
11024
11025   idxFinalize(&rc, pAllIndex);
11026   idxFinalize(&rc, pIndexXInfo);
11027   idxFinalize(&rc, pWrite);
11028
11029   if( pCtx ){
11030     for(i=0; i<pCtx->nSlot; i++){
11031       sqlite3_free(pCtx->aSlot[i].z);
11032     }
11033     sqlite3_free(pCtx);
11034   }
11035
11036   if( rc==SQLITE_OK ){
11037     rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
11038   }
11039
11040   sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
11041   return rc;
11042 }
11043
11044 /*
11045 ** Allocate a new sqlite3expert object.
11046 */
11047 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
11048   int rc = SQLITE_OK;
11049   sqlite3expert *pNew;
11050
11051   pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
11052
11053   /* Open two in-memory databases to work with. The "vtab database" (dbv)
11054   ** will contain a virtual table corresponding to each real table in
11055   ** the user database schema, and a copy of each view. It is used to
11056   ** collect information regarding the WHERE, ORDER BY and other clauses
11057   ** of the user's query.
11058   */
11059   if( rc==SQLITE_OK ){
11060     pNew->db = db;
11061     pNew->iSample = 100;
11062     rc = sqlite3_open(":memory:", &pNew->dbv);
11063   }
11064   if( rc==SQLITE_OK ){
11065     rc = sqlite3_open(":memory:", &pNew->dbm);
11066     if( rc==SQLITE_OK ){
11067       sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
11068     }
11069   }
11070   
11071
11072   /* Copy the entire schema of database [db] into [dbm]. */
11073   if( rc==SQLITE_OK ){
11074     sqlite3_stmt *pSql;
11075     rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, 
11076         "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
11077         " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
11078     );
11079     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11080       const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
11081       rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
11082     }
11083     idxFinalize(&rc, pSql);
11084   }
11085
11086   /* Create the vtab schema */
11087   if( rc==SQLITE_OK ){
11088     rc = idxCreateVtabSchema(pNew, pzErrmsg);
11089   }
11090
11091   /* Register the auth callback with dbv */
11092   if( rc==SQLITE_OK ){
11093     sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
11094   }
11095
11096   /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
11097   ** return the new sqlite3expert handle.  */
11098   if( rc!=SQLITE_OK ){
11099     sqlite3_expert_destroy(pNew);
11100     pNew = 0;
11101   }
11102   return pNew;
11103 }
11104
11105 /*
11106 ** Configure an sqlite3expert object.
11107 */
11108 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
11109   int rc = SQLITE_OK;
11110   va_list ap;
11111   va_start(ap, op);
11112   switch( op ){
11113     case EXPERT_CONFIG_SAMPLE: {
11114       int iVal = va_arg(ap, int);
11115       if( iVal<0 ) iVal = 0;
11116       if( iVal>100 ) iVal = 100;
11117       p->iSample = iVal;
11118       break;
11119     }
11120     default:
11121       rc = SQLITE_NOTFOUND;
11122       break;
11123   }
11124
11125   va_end(ap);
11126   return rc;
11127 }
11128
11129 /*
11130 ** Add an SQL statement to the analysis.
11131 */
11132 int sqlite3_expert_sql(
11133   sqlite3expert *p,               /* From sqlite3_expert_new() */
11134   const char *zSql,               /* SQL statement to add */
11135   char **pzErr                    /* OUT: Error message (if any) */
11136 ){
11137   IdxScan *pScanOrig = p->pScan;
11138   IdxStatement *pStmtOrig = p->pStatement;
11139   int rc = SQLITE_OK;
11140   const char *zStmt = zSql;
11141
11142   if( p->bRun ) return SQLITE_MISUSE;
11143
11144   while( rc==SQLITE_OK && zStmt && zStmt[0] ){
11145     sqlite3_stmt *pStmt = 0;
11146     rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
11147     if( rc==SQLITE_OK ){
11148       if( pStmt ){
11149         IdxStatement *pNew;
11150         const char *z = sqlite3_sql(pStmt);
11151         int n = STRLEN(z);
11152         pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
11153         if( rc==SQLITE_OK ){
11154           pNew->zSql = (char*)&pNew[1];
11155           memcpy(pNew->zSql, z, n+1);
11156           pNew->pNext = p->pStatement;
11157           if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
11158           p->pStatement = pNew;
11159         }
11160         sqlite3_finalize(pStmt);
11161       }
11162     }else{
11163       idxDatabaseError(p->dbv, pzErr);
11164     }
11165   }
11166
11167   if( rc!=SQLITE_OK ){
11168     idxScanFree(p->pScan, pScanOrig);
11169     idxStatementFree(p->pStatement, pStmtOrig);
11170     p->pScan = pScanOrig;
11171     p->pStatement = pStmtOrig;
11172   }
11173
11174   return rc;
11175 }
11176
11177 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
11178   int rc;
11179   IdxHashEntry *pEntry;
11180
11181   /* Do trigger processing to collect any extra IdxScan structures */
11182   rc = idxProcessTriggers(p, pzErr);
11183
11184   /* Create candidate indexes within the in-memory database file */
11185   if( rc==SQLITE_OK ){
11186     rc = idxCreateCandidates(p);
11187   }else if ( rc==SQLITE_BUSY_TIMEOUT ){
11188     if( pzErr )
11189       *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose.");
11190     return rc;
11191   }
11192
11193   /* Generate the stat1 data */
11194   if( rc==SQLITE_OK ){
11195     rc = idxPopulateStat1(p, pzErr);
11196   }
11197
11198   /* Formulate the EXPERT_REPORT_CANDIDATES text */
11199   for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
11200     p->zCandidates = idxAppendText(&rc, p->zCandidates, 
11201         "%s;%s%s\n", pEntry->zVal, 
11202         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
11203     );
11204   }
11205
11206   /* Figure out which of the candidate indexes are preferred by the query
11207   ** planner and report the results to the user.  */
11208   if( rc==SQLITE_OK ){
11209     rc = idxFindIndexes(p, pzErr);
11210   }
11211
11212   if( rc==SQLITE_OK ){
11213     p->bRun = 1;
11214   }
11215   return rc;
11216 }
11217
11218 /*
11219 ** Return the total number of statements that have been added to this
11220 ** sqlite3expert using sqlite3_expert_sql().
11221 */
11222 int sqlite3_expert_count(sqlite3expert *p){
11223   int nRet = 0;
11224   if( p->pStatement ) nRet = p->pStatement->iId+1;
11225   return nRet;
11226 }
11227
11228 /*
11229 ** Return a component of the report.
11230 */
11231 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
11232   const char *zRet = 0;
11233   IdxStatement *pStmt;
11234
11235   if( p->bRun==0 ) return 0;
11236   for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
11237   switch( eReport ){
11238     case EXPERT_REPORT_SQL:
11239       if( pStmt ) zRet = pStmt->zSql;
11240       break;
11241     case EXPERT_REPORT_INDEXES:
11242       if( pStmt ) zRet = pStmt->zIdx;
11243       break;
11244     case EXPERT_REPORT_PLAN:
11245       if( pStmt ) zRet = pStmt->zEQP;
11246       break;
11247     case EXPERT_REPORT_CANDIDATES:
11248       zRet = p->zCandidates;
11249       break;
11250   }
11251   return zRet;
11252 }
11253
11254 /*
11255 ** Free an sqlite3expert object.
11256 */
11257 void sqlite3_expert_destroy(sqlite3expert *p){
11258   if( p ){
11259     sqlite3_close(p->dbm);
11260     sqlite3_close(p->dbv);
11261     idxScanFree(p->pScan, 0);
11262     idxStatementFree(p->pStatement, 0);
11263     idxTableFree(p->pTable);
11264     idxWriteFree(p->pWrite);
11265     idxHashClear(&p->hIdx);
11266     sqlite3_free(p->zCandidates);
11267     sqlite3_free(p);
11268   }
11269 }
11270
11271 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11272
11273 /************************* End ../ext/expert/sqlite3expert.c ********************/
11274
11275 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11276 /************************* Begin ../ext/misc/dbdata.c ******************/
11277 /*
11278 ** 2019-04-17
11279 **
11280 ** The author disclaims copyright to this source code.  In place of
11281 ** a legal notice, here is a blessing:
11282 **
11283 **    May you do good and not evil.
11284 **    May you find forgiveness for yourself and forgive others.
11285 **    May you share freely, never taking more than you give.
11286 **
11287 ******************************************************************************
11288 **
11289 ** This file contains an implementation of two eponymous virtual tables,
11290 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
11291 ** "sqlite_dbpage" eponymous virtual table be available.
11292 **
11293 ** SQLITE_DBDATA:
11294 **   sqlite_dbdata is used to extract data directly from a database b-tree
11295 **   page and its associated overflow pages, bypassing the b-tree layer.
11296 **   The table schema is equivalent to:
11297 **
11298 **     CREATE TABLE sqlite_dbdata(
11299 **       pgno INTEGER,
11300 **       cell INTEGER,
11301 **       field INTEGER,
11302 **       value ANY,
11303 **       schema TEXT HIDDEN
11304 **     );
11305 **
11306 **   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
11307 **   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
11308 **   "schema".
11309 **
11310 **   Each page of the database is inspected. If it cannot be interpreted as
11311 **   a b-tree page, or if it is a b-tree page containing 0 entries, the
11312 **   sqlite_dbdata table contains no rows for that page.  Otherwise, the
11313 **   table contains one row for each field in the record associated with
11314 **   each cell on the page. For intkey b-trees, the key value is stored in
11315 **   field -1.
11316 **
11317 **   For example, for the database:
11318 **
11319 **     CREATE TABLE t1(a, b);     -- root page is page 2
11320 **     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
11321 **     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
11322 **
11323 **   the sqlite_dbdata table contains, as well as from entries related to 
11324 **   page 1, content equivalent to:
11325 **
11326 **     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
11327 **         (2, 0, -1, 5     ),
11328 **         (2, 0,  0, 'v'   ),
11329 **         (2, 0,  1, 'five'),
11330 **         (2, 1, -1, 10    ),
11331 **         (2, 1,  0, 'x'   ),
11332 **         (2, 1,  1, 'ten' );
11333 **
11334 **   If database corruption is encountered, this module does not report an
11335 **   error. Instead, it attempts to extract as much data as possible and
11336 **   ignores the corruption.
11337 **
11338 ** SQLITE_DBPTR:
11339 **   The sqlite_dbptr table has the following schema:
11340 **
11341 **     CREATE TABLE sqlite_dbptr(
11342 **       pgno INTEGER,
11343 **       child INTEGER,
11344 **       schema TEXT HIDDEN
11345 **     );
11346 **
11347 **   It contains one entry for each b-tree pointer between a parent and
11348 **   child page in the database.
11349 */
11350 #if !defined(SQLITEINT_H) 
11351 /* #include "sqlite3ext.h" */
11352
11353 /* typedef unsigned char u8; */
11354
11355 #endif
11356 SQLITE_EXTENSION_INIT1
11357 #include <string.h>
11358 #include <assert.h>
11359
11360 #define DBDATA_PADDING_BYTES 100 
11361
11362 typedef struct DbdataTable DbdataTable;
11363 typedef struct DbdataCursor DbdataCursor;
11364
11365 /* Cursor object */
11366 struct DbdataCursor {
11367   sqlite3_vtab_cursor base;       /* Base class.  Must be first */
11368   sqlite3_stmt *pStmt;            /* For fetching database pages */
11369
11370   int iPgno;                      /* Current page number */
11371   u8 *aPage;                      /* Buffer containing page */
11372   int nPage;                      /* Size of aPage[] in bytes */
11373   int nCell;                      /* Number of cells on aPage[] */
11374   int iCell;                      /* Current cell number */
11375   int bOnePage;                   /* True to stop after one page */
11376   int szDb;
11377   sqlite3_int64 iRowid;
11378
11379   /* Only for the sqlite_dbdata table */
11380   u8 *pRec;                       /* Buffer containing current record */
11381   int nRec;                       /* Size of pRec[] in bytes */
11382   int nHdr;                       /* Size of header in bytes */
11383   int iField;                     /* Current field number */
11384   u8 *pHdrPtr;
11385   u8 *pPtr;
11386   
11387   sqlite3_int64 iIntkey;          /* Integer key value */
11388 };
11389
11390 /* Table object */
11391 struct DbdataTable {
11392   sqlite3_vtab base;              /* Base class.  Must be first */
11393   sqlite3 *db;                    /* The database connection */
11394   sqlite3_stmt *pStmt;            /* For fetching database pages */
11395   int bPtr;                       /* True for sqlite3_dbptr table */
11396 };
11397
11398 /* Column and schema definitions for sqlite_dbdata */
11399 #define DBDATA_COLUMN_PGNO        0
11400 #define DBDATA_COLUMN_CELL        1
11401 #define DBDATA_COLUMN_FIELD       2
11402 #define DBDATA_COLUMN_VALUE       3
11403 #define DBDATA_COLUMN_SCHEMA      4
11404 #define DBDATA_SCHEMA             \
11405       "CREATE TABLE x("           \
11406       "  pgno INTEGER,"           \
11407       "  cell INTEGER,"           \
11408       "  field INTEGER,"          \
11409       "  value ANY,"              \
11410       "  schema TEXT HIDDEN"      \
11411       ")"
11412
11413 /* Column and schema definitions for sqlite_dbptr */
11414 #define DBPTR_COLUMN_PGNO         0
11415 #define DBPTR_COLUMN_CHILD        1
11416 #define DBPTR_COLUMN_SCHEMA       2
11417 #define DBPTR_SCHEMA              \
11418       "CREATE TABLE x("           \
11419       "  pgno INTEGER,"           \
11420       "  child INTEGER,"          \
11421       "  schema TEXT HIDDEN"      \
11422       ")"
11423
11424 /*
11425 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual 
11426 ** table.
11427 */
11428 static int dbdataConnect(
11429   sqlite3 *db,
11430   void *pAux,
11431   int argc, const char *const*argv,
11432   sqlite3_vtab **ppVtab,
11433   char **pzErr
11434 ){
11435   DbdataTable *pTab = 0;
11436   int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
11437
11438   if( rc==SQLITE_OK ){
11439     pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
11440     if( pTab==0 ){
11441       rc = SQLITE_NOMEM;
11442     }else{
11443       memset(pTab, 0, sizeof(DbdataTable));
11444       pTab->db = db;
11445       pTab->bPtr = (pAux!=0);
11446     }
11447   }
11448
11449   *ppVtab = (sqlite3_vtab*)pTab;
11450   return rc;
11451 }
11452
11453 /*
11454 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
11455 */
11456 static int dbdataDisconnect(sqlite3_vtab *pVtab){
11457   DbdataTable *pTab = (DbdataTable*)pVtab;
11458   if( pTab ){
11459     sqlite3_finalize(pTab->pStmt);
11460     sqlite3_free(pVtab);
11461   }
11462   return SQLITE_OK;
11463 }
11464
11465 /*
11466 ** This function interprets two types of constraints:
11467 **
11468 **       schema=?
11469 **       pgno=?
11470 **
11471 ** If neither are present, idxNum is set to 0. If schema=? is present,
11472 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
11473 ** in idxNum is set.
11474 **
11475 ** If both parameters are present, schema is in position 0 and pgno in
11476 ** position 1.
11477 */
11478 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
11479   DbdataTable *pTab = (DbdataTable*)tab;
11480   int i;
11481   int iSchema = -1;
11482   int iPgno = -1;
11483   int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
11484
11485   for(i=0; i<pIdx->nConstraint; i++){
11486     struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
11487     if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
11488       if( p->iColumn==colSchema ){
11489         if( p->usable==0 ) return SQLITE_CONSTRAINT;
11490         iSchema = i;
11491       }
11492       if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
11493         iPgno = i;
11494       }
11495     }
11496   }
11497
11498   if( iSchema>=0 ){
11499     pIdx->aConstraintUsage[iSchema].argvIndex = 1;
11500     pIdx->aConstraintUsage[iSchema].omit = 1;
11501   }
11502   if( iPgno>=0 ){
11503     pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
11504     pIdx->aConstraintUsage[iPgno].omit = 1;
11505     pIdx->estimatedCost = 100;
11506     pIdx->estimatedRows =  50;
11507
11508     if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
11509       int iCol = pIdx->aOrderBy[0].iColumn;
11510       if( pIdx->nOrderBy==1 ){
11511         pIdx->orderByConsumed = (iCol==0 || iCol==1);
11512       }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
11513         pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
11514       }
11515     }
11516
11517   }else{
11518     pIdx->estimatedCost = 100000000;
11519     pIdx->estimatedRows = 1000000000;
11520   }
11521   pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
11522   return SQLITE_OK;
11523 }
11524
11525 /*
11526 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
11527 */
11528 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
11529   DbdataCursor *pCsr;
11530
11531   pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
11532   if( pCsr==0 ){
11533     return SQLITE_NOMEM;
11534   }else{
11535     memset(pCsr, 0, sizeof(DbdataCursor));
11536     pCsr->base.pVtab = pVTab;
11537   }
11538
11539   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
11540   return SQLITE_OK;
11541 }
11542
11543 /*
11544 ** Restore a cursor object to the state it was in when first allocated 
11545 ** by dbdataOpen().
11546 */
11547 static void dbdataResetCursor(DbdataCursor *pCsr){
11548   DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
11549   if( pTab->pStmt==0 ){
11550     pTab->pStmt = pCsr->pStmt;
11551   }else{
11552     sqlite3_finalize(pCsr->pStmt);
11553   }
11554   pCsr->pStmt = 0;
11555   pCsr->iPgno = 1;
11556   pCsr->iCell = 0;
11557   pCsr->iField = 0;
11558   pCsr->bOnePage = 0;
11559   sqlite3_free(pCsr->aPage);
11560   sqlite3_free(pCsr->pRec);
11561   pCsr->pRec = 0;
11562   pCsr->aPage = 0;
11563 }
11564
11565 /*
11566 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
11567 */
11568 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
11569   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11570   dbdataResetCursor(pCsr);
11571   sqlite3_free(pCsr);
11572   return SQLITE_OK;
11573 }
11574
11575 /* 
11576 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers. 
11577 */
11578 static unsigned int get_uint16(unsigned char *a){
11579   return (a[0]<<8)|a[1];
11580 }
11581 static unsigned int get_uint32(unsigned char *a){
11582   return ((unsigned int)a[0]<<24)
11583        | ((unsigned int)a[1]<<16)
11584        | ((unsigned int)a[2]<<8)
11585        | ((unsigned int)a[3]);
11586 }
11587
11588 /*
11589 ** Load page pgno from the database via the sqlite_dbpage virtual table.
11590 ** If successful, set (*ppPage) to point to a buffer containing the page
11591 ** data, (*pnPage) to the size of that buffer in bytes and return
11592 ** SQLITE_OK. In this case it is the responsibility of the caller to
11593 ** eventually free the buffer using sqlite3_free().
11594 **
11595 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
11596 ** return an SQLite error code.
11597 */
11598 static int dbdataLoadPage(
11599   DbdataCursor *pCsr,             /* Cursor object */
11600   unsigned int pgno,              /* Page number of page to load */
11601   u8 **ppPage,                    /* OUT: pointer to page buffer */
11602   int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
11603 ){
11604   int rc2;
11605   int rc = SQLITE_OK;
11606   sqlite3_stmt *pStmt = pCsr->pStmt;
11607
11608   *ppPage = 0;
11609   *pnPage = 0;
11610   sqlite3_bind_int64(pStmt, 2, pgno);
11611   if( SQLITE_ROW==sqlite3_step(pStmt) ){
11612     int nCopy = sqlite3_column_bytes(pStmt, 0);
11613     if( nCopy>0 ){
11614       u8 *pPage;
11615       pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11616       if( pPage==0 ){
11617         rc = SQLITE_NOMEM;
11618       }else{
11619         const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11620         memcpy(pPage, pCopy, nCopy);
11621         memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11622       }
11623       *ppPage = pPage;
11624       *pnPage = nCopy;
11625     }
11626   }
11627   rc2 = sqlite3_reset(pStmt);
11628   if( rc==SQLITE_OK ) rc = rc2;
11629
11630   return rc;
11631 }
11632
11633 /*
11634 ** Read a varint.  Put the value in *pVal and return the number of bytes.
11635 */
11636 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11637   sqlite3_int64 v = 0;
11638   int i;
11639   for(i=0; i<8; i++){
11640     v = (v<<7) + (z[i]&0x7f);
11641     if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
11642   }
11643   v = (v<<8) + (z[i]&0xff);
11644   *pVal = v;
11645   return 9;
11646 }
11647
11648 /*
11649 ** Return the number of bytes of space used by an SQLite value of type
11650 ** eType.
11651 */
11652 static int dbdataValueBytes(int eType){
11653   switch( eType ){
11654     case 0: case 8: case 9:
11655     case 10: case 11:
11656       return 0;
11657     case 1:
11658       return 1;
11659     case 2:
11660       return 2;
11661     case 3:
11662       return 3;
11663     case 4:
11664       return 4;
11665     case 5:
11666       return 6;
11667     case 6:
11668     case 7:
11669       return 8;
11670     default:
11671       if( eType>0 ){
11672         return ((eType-12) / 2);
11673       }
11674       return 0;
11675   }
11676 }
11677
11678 /*
11679 ** Load a value of type eType from buffer pData and use it to set the
11680 ** result of context object pCtx.
11681 */
11682 static void dbdataValue(
11683   sqlite3_context *pCtx, 
11684   int eType, 
11685   u8 *pData,
11686   int nData
11687 ){
11688   if( eType>=0 && dbdataValueBytes(eType)<=nData ){
11689     switch( eType ){
11690       case 0: 
11691       case 10: 
11692       case 11: 
11693         sqlite3_result_null(pCtx);
11694         break;
11695       
11696       case 8: 
11697         sqlite3_result_int(pCtx, 0);
11698         break;
11699       case 9:
11700         sqlite3_result_int(pCtx, 1);
11701         break;
11702   
11703       case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
11704         sqlite3_uint64 v = (signed char)pData[0];
11705         pData++;
11706         switch( eType ){
11707           case 7:
11708           case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
11709           case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
11710           case 4:  v = (v<<8) + pData[0];  pData++;
11711           case 3:  v = (v<<8) + pData[0];  pData++;
11712           case 2:  v = (v<<8) + pData[0];  pData++;
11713         }
11714   
11715         if( eType==7 ){
11716           double r;
11717           memcpy(&r, &v, sizeof(r));
11718           sqlite3_result_double(pCtx, r);
11719         }else{
11720           sqlite3_result_int64(pCtx, (sqlite3_int64)v);
11721         }
11722         break;
11723       }
11724   
11725       default: {
11726         int n = ((eType-12) / 2);
11727         if( eType % 2 ){
11728           sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
11729         }else{
11730           sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
11731         }
11732       }
11733     }
11734   }
11735 }
11736
11737 /*
11738 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
11739 */
11740 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
11741   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11742   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11743
11744   pCsr->iRowid++;
11745   while( 1 ){
11746     int rc;
11747     int iOff = (pCsr->iPgno==1 ? 100 : 0);
11748     int bNextPage = 0;
11749
11750     if( pCsr->aPage==0 ){
11751       while( 1 ){
11752         if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
11753         rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
11754         if( rc!=SQLITE_OK ) return rc;
11755         if( pCsr->aPage ) break;
11756         pCsr->iPgno++;
11757       }
11758       pCsr->iCell = pTab->bPtr ? -2 : 0;
11759       pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
11760     }
11761
11762     if( pTab->bPtr ){
11763       if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
11764         pCsr->iCell = pCsr->nCell;
11765       }
11766       pCsr->iCell++;
11767       if( pCsr->iCell>=pCsr->nCell ){
11768         sqlite3_free(pCsr->aPage);
11769         pCsr->aPage = 0;
11770         if( pCsr->bOnePage ) return SQLITE_OK;
11771         pCsr->iPgno++;
11772       }else{
11773         return SQLITE_OK;
11774       }
11775     }else{
11776       /* If there is no record loaded, load it now. */
11777       if( pCsr->pRec==0 ){
11778         int bHasRowid = 0;
11779         int nPointer = 0;
11780         sqlite3_int64 nPayload = 0;
11781         sqlite3_int64 nHdr = 0;
11782         int iHdr;
11783         int U, X;
11784         int nLocal;
11785   
11786         switch( pCsr->aPage[iOff] ){
11787           case 0x02:
11788             nPointer = 4;
11789             break;
11790           case 0x0a:
11791             break;
11792           case 0x0d:
11793             bHasRowid = 1;
11794             break;
11795           default:
11796             /* This is not a b-tree page with records on it. Continue. */
11797             pCsr->iCell = pCsr->nCell;
11798             break;
11799         }
11800
11801         if( pCsr->iCell>=pCsr->nCell ){
11802           bNextPage = 1;
11803         }else{
11804   
11805           iOff += 8 + nPointer + pCsr->iCell*2;
11806           if( iOff>pCsr->nPage ){
11807             bNextPage = 1;
11808           }else{
11809             iOff = get_uint16(&pCsr->aPage[iOff]);
11810           }
11811     
11812           /* For an interior node cell, skip past the child-page number */
11813           iOff += nPointer;
11814     
11815           /* Load the "byte of payload including overflow" field */
11816           if( bNextPage || iOff>pCsr->nPage ){
11817             bNextPage = 1;
11818           }else{
11819             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
11820           }
11821     
11822           /* If this is a leaf intkey cell, load the rowid */
11823           if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
11824             iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
11825           }
11826     
11827           /* Figure out how much data to read from the local page */
11828           U = pCsr->nPage;
11829           if( bHasRowid ){
11830             X = U-35;
11831           }else{
11832             X = ((U-12)*64/255)-23;
11833           }
11834           if( nPayload<=X ){
11835             nLocal = nPayload;
11836           }else{
11837             int M, K;
11838             M = ((U-12)*32/255)-23;
11839             K = M+((nPayload-M)%(U-4));
11840             if( K<=X ){
11841               nLocal = K;
11842             }else{
11843               nLocal = M;
11844             }
11845           }
11846
11847           if( bNextPage || nLocal+iOff>pCsr->nPage ){
11848             bNextPage = 1;
11849           }else{
11850
11851             /* Allocate space for payload. And a bit more to catch small buffer
11852             ** overruns caused by attempting to read a varint or similar from 
11853             ** near the end of a corrupt record.  */
11854             pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
11855             if( pCsr->pRec==0 ) return SQLITE_NOMEM;
11856             memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
11857             pCsr->nRec = nPayload;
11858
11859             /* Load the nLocal bytes of payload */
11860             memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
11861             iOff += nLocal;
11862
11863             /* Load content from overflow pages */
11864             if( nPayload>nLocal ){
11865               sqlite3_int64 nRem = nPayload - nLocal;
11866               unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
11867               while( nRem>0 ){
11868                 u8 *aOvfl = 0;
11869                 int nOvfl = 0;
11870                 int nCopy;
11871                 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
11872                 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
11873                 if( rc!=SQLITE_OK ) return rc;
11874                 if( aOvfl==0 ) break;
11875
11876                 nCopy = U-4;
11877                 if( nCopy>nRem ) nCopy = nRem;
11878                 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
11879                 nRem -= nCopy;
11880
11881                 pgnoOvfl = get_uint32(aOvfl);
11882                 sqlite3_free(aOvfl);
11883               }
11884             }
11885     
11886             iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
11887             pCsr->nHdr = nHdr;
11888             pCsr->pHdrPtr = &pCsr->pRec[iHdr];
11889             pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
11890             pCsr->iField = (bHasRowid ? -1 : 0);
11891           }
11892         }
11893       }else{
11894         pCsr->iField++;
11895         if( pCsr->iField>0 ){
11896           sqlite3_int64 iType;
11897           if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
11898             bNextPage = 1;
11899           }else{
11900             pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
11901             pCsr->pPtr += dbdataValueBytes(iType);
11902           }
11903         }
11904       }
11905
11906       if( bNextPage ){
11907         sqlite3_free(pCsr->aPage);
11908         sqlite3_free(pCsr->pRec);
11909         pCsr->aPage = 0;
11910         pCsr->pRec = 0;
11911         if( pCsr->bOnePage ) return SQLITE_OK;
11912         pCsr->iPgno++;
11913       }else{
11914         if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
11915           return SQLITE_OK;
11916         }
11917
11918         /* Advance to the next cell. The next iteration of the loop will load
11919         ** the record and so on. */
11920         sqlite3_free(pCsr->pRec);
11921         pCsr->pRec = 0;
11922         pCsr->iCell++;
11923       }
11924     }
11925   }
11926
11927   assert( !"can't get here" );
11928   return SQLITE_OK;
11929 }
11930
11931 /* 
11932 ** Return true if the cursor is at EOF.
11933 */
11934 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
11935   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11936   return pCsr->aPage==0;
11937 }
11938
11939 /* 
11940 ** Determine the size in pages of database zSchema (where zSchema is
11941 ** "main", "temp" or the name of an attached database) and set 
11942 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
11943 ** an SQLite error code.
11944 */
11945 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
11946   DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
11947   char *zSql = 0;
11948   int rc, rc2;
11949   sqlite3_stmt *pStmt = 0;
11950
11951   zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
11952   if( zSql==0 ) return SQLITE_NOMEM;
11953   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
11954   sqlite3_free(zSql);
11955   if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11956     pCsr->szDb = sqlite3_column_int(pStmt, 0);
11957   }
11958   rc2 = sqlite3_finalize(pStmt);
11959   if( rc==SQLITE_OK ) rc = rc2;
11960   return rc;
11961 }
11962
11963 /* 
11964 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
11965 */
11966 static int dbdataFilter(
11967   sqlite3_vtab_cursor *pCursor, 
11968   int idxNum, const char *idxStr,
11969   int argc, sqlite3_value **argv
11970 ){
11971   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11972   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11973   int rc = SQLITE_OK;
11974   const char *zSchema = "main";
11975
11976   dbdataResetCursor(pCsr);
11977   assert( pCsr->iPgno==1 );
11978   if( idxNum & 0x01 ){
11979     zSchema = (const char*)sqlite3_value_text(argv[0]);
11980   }
11981   if( idxNum & 0x02 ){
11982     pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
11983     pCsr->bOnePage = 1;
11984   }else{
11985     pCsr->nPage = dbdataDbsize(pCsr, zSchema);
11986     rc = dbdataDbsize(pCsr, zSchema);
11987   }
11988
11989   if( rc==SQLITE_OK ){
11990     if( pTab->pStmt ){
11991       pCsr->pStmt = pTab->pStmt;
11992       pTab->pStmt = 0;
11993     }else{
11994       rc = sqlite3_prepare_v2(pTab->db, 
11995           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
11996           &pCsr->pStmt, 0
11997       );
11998     }
11999   }
12000   if( rc==SQLITE_OK ){
12001     rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
12002   }else{
12003     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
12004   }
12005   if( rc==SQLITE_OK ){
12006     rc = dbdataNext(pCursor);
12007   }
12008   return rc;
12009 }
12010
12011 /* 
12012 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
12013 */
12014 static int dbdataColumn(
12015   sqlite3_vtab_cursor *pCursor, 
12016   sqlite3_context *ctx, 
12017   int i
12018 ){
12019   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12020   DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
12021   if( pTab->bPtr ){
12022     switch( i ){
12023       case DBPTR_COLUMN_PGNO:
12024         sqlite3_result_int64(ctx, pCsr->iPgno);
12025         break;
12026       case DBPTR_COLUMN_CHILD: {
12027         int iOff = pCsr->iPgno==1 ? 100 : 0;
12028         if( pCsr->iCell<0 ){
12029           iOff += 8;
12030         }else{
12031           iOff += 12 + pCsr->iCell*2;
12032           if( iOff>pCsr->nPage ) return SQLITE_OK;
12033           iOff = get_uint16(&pCsr->aPage[iOff]);
12034         }
12035         if( iOff<=pCsr->nPage ){
12036           sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
12037         }
12038         break;
12039       }
12040     }
12041   }else{
12042     switch( i ){
12043       case DBDATA_COLUMN_PGNO:
12044         sqlite3_result_int64(ctx, pCsr->iPgno);
12045         break;
12046       case DBDATA_COLUMN_CELL:
12047         sqlite3_result_int(ctx, pCsr->iCell);
12048         break;
12049       case DBDATA_COLUMN_FIELD:
12050         sqlite3_result_int(ctx, pCsr->iField);
12051         break;
12052       case DBDATA_COLUMN_VALUE: {
12053         if( pCsr->iField<0 ){
12054           sqlite3_result_int64(ctx, pCsr->iIntkey);
12055         }else{
12056           sqlite3_int64 iType;
12057           dbdataGetVarint(pCsr->pHdrPtr, &iType);
12058           dbdataValue(
12059               ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
12060           );
12061         }
12062         break;
12063       }
12064     }
12065   }
12066   return SQLITE_OK;
12067 }
12068
12069 /* 
12070 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
12071 */
12072 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
12073   DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12074   *pRowid = pCsr->iRowid;
12075   return SQLITE_OK;
12076 }
12077
12078
12079 /*
12080 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
12081 */
12082 static int sqlite3DbdataRegister(sqlite3 *db){
12083   static sqlite3_module dbdata_module = {
12084     0,                            /* iVersion */
12085     0,                            /* xCreate */
12086     dbdataConnect,                /* xConnect */
12087     dbdataBestIndex,              /* xBestIndex */
12088     dbdataDisconnect,             /* xDisconnect */
12089     0,                            /* xDestroy */
12090     dbdataOpen,                   /* xOpen - open a cursor */
12091     dbdataClose,                  /* xClose - close a cursor */
12092     dbdataFilter,                 /* xFilter - configure scan constraints */
12093     dbdataNext,                   /* xNext - advance a cursor */
12094     dbdataEof,                    /* xEof - check for end of scan */
12095     dbdataColumn,                 /* xColumn - read data */
12096     dbdataRowid,                  /* xRowid - read data */
12097     0,                            /* xUpdate */
12098     0,                            /* xBegin */
12099     0,                            /* xSync */
12100     0,                            /* xCommit */
12101     0,                            /* xRollback */
12102     0,                            /* xFindMethod */
12103     0,                            /* xRename */
12104     0,                            /* xSavepoint */
12105     0,                            /* xRelease */
12106     0,                            /* xRollbackTo */
12107     0                             /* xShadowName */
12108   };
12109
12110   int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
12111   if( rc==SQLITE_OK ){
12112     rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
12113   }
12114   return rc;
12115 }
12116
12117 #ifdef _WIN32
12118
12119 #endif
12120 int sqlite3_dbdata_init(
12121   sqlite3 *db, 
12122   char **pzErrMsg, 
12123   const sqlite3_api_routines *pApi
12124 ){
12125   SQLITE_EXTENSION_INIT2(pApi);
12126   return sqlite3DbdataRegister(db);
12127 }
12128
12129 /************************* End ../ext/misc/dbdata.c ********************/
12130 #endif
12131
12132 #if defined(SQLITE_ENABLE_SESSION)
12133 /*
12134 ** State information for a single open session
12135 */
12136 typedef struct OpenSession OpenSession;
12137 struct OpenSession {
12138   char *zName;             /* Symbolic name for this session */
12139   int nFilter;             /* Number of xFilter rejection GLOB patterns */
12140   char **azFilter;         /* Array of xFilter rejection GLOB patterns */
12141   sqlite3_session *p;      /* The open session */
12142 };
12143 #endif
12144
12145 typedef struct ExpertInfo ExpertInfo;
12146 struct ExpertInfo {
12147   sqlite3expert *pExpert;
12148   int bVerbose;
12149 };
12150
12151 /* A single line in the EQP output */
12152 typedef struct EQPGraphRow EQPGraphRow;
12153 struct EQPGraphRow {
12154   int iEqpId;           /* ID for this row */
12155   int iParentId;        /* ID of the parent row */
12156   EQPGraphRow *pNext;   /* Next row in sequence */
12157   char zText[1];        /* Text to display for this row */
12158 };
12159
12160 /* All EQP output is collected into an instance of the following */
12161 typedef struct EQPGraph EQPGraph;
12162 struct EQPGraph {
12163   EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
12164   EQPGraphRow *pLast;   /* Last element of the pRow list */
12165   char zPrefix[100];    /* Graph prefix */
12166 };
12167
12168 /*
12169 ** State information about the database connection is contained in an
12170 ** instance of the following structure.
12171 */
12172 typedef struct ShellState ShellState;
12173 struct ShellState {
12174   sqlite3 *db;           /* The database */
12175   u8 autoExplain;        /* Automatically turn on .explain mode */
12176   u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
12177   u8 autoEQPtest;        /* autoEQP is in test mode */
12178   u8 autoEQPtrace;       /* autoEQP is in trace mode */
12179   u8 scanstatsOn;        /* True to display scan stats before each finalize */
12180   u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
12181   u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
12182   u8 nEqpLevel;          /* Depth of the EQP output graph */
12183   u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
12184   u8 bSafeMode;          /* True to prohibit unsafe operations */
12185   u8 bSafeModePersist;   /* The long-term value of bSafeMode */
12186   unsigned statsOn;      /* True to display memory stats before each finalize */
12187   unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
12188   int outCount;          /* Revert to stdout when reaching zero */
12189   int cnt;               /* Number of records displayed so far */
12190   int lineno;            /* Line number of last line read from in */
12191   int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
12192   FILE *in;              /* Read commands from this stream */
12193   FILE *out;             /* Write results here */
12194   FILE *traceOut;        /* Output for sqlite3_trace() */
12195   int nErr;              /* Number of errors seen */
12196   int mode;              /* An output mode setting */
12197   int modePrior;         /* Saved mode */
12198   int cMode;             /* temporary output mode for the current query */
12199   int normalMode;        /* Output mode before ".explain on" */
12200   int writableSchema;    /* True if PRAGMA writable_schema=ON */
12201   int showHeader;        /* True to show column names in List or Column mode */
12202   int nCheck;            /* Number of ".check" commands run */
12203   unsigned nProgress;    /* Number of progress callbacks encountered */
12204   unsigned mxProgress;   /* Maximum progress callbacks before failing */
12205   unsigned flgProgress;  /* Flags for the progress callback */
12206   unsigned shellFlgs;    /* Various flags */
12207   unsigned priorShFlgs;  /* Saved copy of flags */
12208   sqlite3_int64 szMax;   /* --maxsize argument to .open */
12209   char *zDestTable;      /* Name of destination table when MODE_Insert */
12210   char *zTempFile;       /* Temporary file that might need deleting */
12211   char zTestcase[30];    /* Name of current test case */
12212   char colSeparator[20]; /* Column separator character for several modes */
12213   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
12214   char colSepPrior[20];  /* Saved column separator */
12215   char rowSepPrior[20];  /* Saved row separator */
12216   int *colWidth;         /* Requested width of each column in columnar modes */
12217   int *actualWidth;      /* Actual width of each column */
12218   int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
12219   char nullValue[20];    /* The text to print when a NULL comes back from
12220                          ** the database */
12221   char outfile[FILENAME_MAX]; /* Filename for *out */
12222   sqlite3_stmt *pStmt;   /* Current statement if any. */
12223   FILE *pLog;            /* Write log output here */
12224   struct AuxDb {         /* Storage space for auxiliary database connections */
12225     sqlite3 *db;               /* Connection pointer */
12226     const char *zDbFilename;   /* Filename used to open the connection */
12227     char *zFreeOnClose;        /* Free this memory allocation on close */
12228 #if defined(SQLITE_ENABLE_SESSION)
12229     int nSession;              /* Number of active sessions */
12230     OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
12231 #endif
12232   } aAuxDb[5],           /* Array of all database connections */
12233     *pAuxDb;             /* Currently active database connection */
12234   int *aiIndent;         /* Array of indents used in MODE_Explain */
12235   int nIndent;           /* Size of array aiIndent[] */
12236   int iIndent;           /* Index of current op in aiIndent[] */
12237   char *zNonce;          /* Nonce for temporary safe-mode excapes */
12238   EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
12239   ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
12240 };
12241
12242
12243 /* Allowed values for ShellState.autoEQP
12244 */
12245 #define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
12246 #define AUTOEQP_on       1           /* Automatic EQP is on */
12247 #define AUTOEQP_trigger  2           /* On and also show plans for triggers */
12248 #define AUTOEQP_full     3           /* Show full EXPLAIN */
12249
12250 /* Allowed values for ShellState.openMode
12251 */
12252 #define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
12253 #define SHELL_OPEN_NORMAL      1      /* Normal database file */
12254 #define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
12255 #define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
12256 #define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
12257 #define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
12258 #define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
12259
12260 /* Allowed values for ShellState.eTraceType
12261 */
12262 #define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
12263 #define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
12264 #define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
12265
12266 /* Bits in the ShellState.flgProgress variable */
12267 #define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
12268 #define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
12269                                    ** callback limit is reached, and for each
12270                                    ** top-level SQL statement */
12271 #define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
12272
12273 /*
12274 ** These are the allowed shellFlgs values
12275 */
12276 #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
12277 #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
12278 #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
12279 #define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
12280 #define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
12281 #define SHFLG_CountChanges   0x00000020 /* .changes setting */
12282 #define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
12283 #define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
12284 #define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
12285 #define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
12286
12287 /*
12288 ** Macros for testing and setting shellFlgs
12289 */
12290 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
12291 #define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
12292 #define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
12293
12294 /*
12295 ** These are the allowed modes.
12296 */
12297 #define MODE_Line     0  /* One column per line.  Blank line between records */
12298 #define MODE_Column   1  /* One record per line in neat columns */
12299 #define MODE_List     2  /* One record per line with a separator */
12300 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
12301 #define MODE_Html     4  /* Generate an XHTML table */
12302 #define MODE_Insert   5  /* Generate SQL "insert" statements */
12303 #define MODE_Quote    6  /* Quote values as for SQL */
12304 #define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
12305 #define MODE_Csv      8  /* Quote strings, numbers are plain */
12306 #define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
12307 #define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
12308 #define MODE_Pretty  11  /* Pretty-print schemas */
12309 #define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
12310 #define MODE_Json    13  /* Output JSON */
12311 #define MODE_Markdown 14 /* Markdown formatting */
12312 #define MODE_Table   15  /* MySQL-style table formatting */
12313 #define MODE_Box     16  /* Unicode box-drawing characters */
12314
12315 static const char *modeDescr[] = {
12316   "line",
12317   "column",
12318   "list",
12319   "semi",
12320   "html",
12321   "insert",
12322   "quote",
12323   "tcl",
12324   "csv",
12325   "explain",
12326   "ascii",
12327   "prettyprint",
12328   "eqp",
12329   "json",
12330   "markdown",
12331   "table",
12332   "box"
12333 };
12334
12335 /*
12336 ** These are the column/row/line separators used by the various
12337 ** import/export modes.
12338 */
12339 #define SEP_Column    "|"
12340 #define SEP_Row       "\n"
12341 #define SEP_Tab       "\t"
12342 #define SEP_Space     " "
12343 #define SEP_Comma     ","
12344 #define SEP_CrLf      "\r\n"
12345 #define SEP_Unit      "\x1F"
12346 #define SEP_Record    "\x1E"
12347
12348 /*
12349 ** A callback for the sqlite3_log() interface.
12350 */
12351 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
12352   ShellState *p = (ShellState*)pArg;
12353   if( p->pLog==0 ) return;
12354   utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
12355   fflush(p->pLog);
12356 }
12357
12358 /*
12359 ** SQL function:  shell_putsnl(X)
12360 **
12361 ** Write the text X to the screen (or whatever output is being directed)
12362 ** adding a newline at the end, and then return X.
12363 */
12364 static void shellPutsFunc(
12365   sqlite3_context *pCtx,
12366   int nVal,
12367   sqlite3_value **apVal
12368 ){
12369   ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
12370   (void)nVal;
12371   utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
12372   sqlite3_result_value(pCtx, apVal[0]);
12373 }
12374
12375 /*
12376 ** If in safe mode, print an error message described by the arguments
12377 ** and exit immediately.
12378 */
12379 static void failIfSafeMode(
12380   ShellState *p,
12381   const char *zErrMsg,
12382   ...
12383 ){
12384   if( p->bSafeMode ){
12385     va_list ap;
12386     char *zMsg;
12387     va_start(ap, zErrMsg);
12388     zMsg = sqlite3_vmprintf(zErrMsg, ap);
12389     va_end(ap);
12390     raw_printf(stderr, "line %d: ", p->lineno);
12391     utf8_printf(stderr, "%s\n", zMsg);
12392     exit(1);
12393   }
12394 }
12395
12396 /*
12397 ** SQL function:   edit(VALUE)
12398 **                 edit(VALUE,EDITOR)
12399 **
12400 ** These steps:
12401 **
12402 **     (1) Write VALUE into a temporary file.
12403 **     (2) Run program EDITOR on that temporary file.
12404 **     (3) Read the temporary file back and return its content as the result.
12405 **     (4) Delete the temporary file
12406 **
12407 ** If the EDITOR argument is omitted, use the value in the VISUAL
12408 ** environment variable.  If still there is no EDITOR, through an error.
12409 **
12410 ** Also throw an error if the EDITOR program returns a non-zero exit code.
12411 */
12412 #ifndef SQLITE_NOHAVE_SYSTEM
12413 static void editFunc(
12414   sqlite3_context *context,
12415   int argc,
12416   sqlite3_value **argv
12417 ){
12418   const char *zEditor;
12419   char *zTempFile = 0;
12420   sqlite3 *db;
12421   char *zCmd = 0;
12422   int bBin;
12423   int rc;
12424   int hasCRNL = 0;
12425   FILE *f = 0;
12426   sqlite3_int64 sz;
12427   sqlite3_int64 x;
12428   unsigned char *p = 0;
12429
12430   if( argc==2 ){
12431     zEditor = (const char*)sqlite3_value_text(argv[1]);
12432   }else{
12433     zEditor = getenv("VISUAL");
12434   }
12435   if( zEditor==0 ){
12436     sqlite3_result_error(context, "no editor for edit()", -1);
12437     return;
12438   }
12439   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
12440     sqlite3_result_error(context, "NULL input to edit()", -1);
12441     return;
12442   }
12443   db = sqlite3_context_db_handle(context);
12444   zTempFile = 0;
12445   sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
12446   if( zTempFile==0 ){
12447     sqlite3_uint64 r = 0;
12448     sqlite3_randomness(sizeof(r), &r);
12449     zTempFile = sqlite3_mprintf("temp%llx", r);
12450     if( zTempFile==0 ){
12451       sqlite3_result_error_nomem(context);
12452       return;
12453     }
12454   }
12455   bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
12456   /* When writing the file to be edited, do \n to \r\n conversions on systems
12457   ** that want \r\n line endings */
12458   f = fopen(zTempFile, bBin ? "wb" : "w");
12459   if( f==0 ){
12460     sqlite3_result_error(context, "edit() cannot open temp file", -1);
12461     goto edit_func_end;
12462   }
12463   sz = sqlite3_value_bytes(argv[0]);
12464   if( bBin ){
12465     x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
12466   }else{
12467     const char *z = (const char*)sqlite3_value_text(argv[0]);
12468     /* Remember whether or not the value originally contained \r\n */
12469     if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
12470     x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
12471   }
12472   fclose(f);
12473   f = 0;
12474   if( x!=sz ){
12475     sqlite3_result_error(context, "edit() could not write the whole file", -1);
12476     goto edit_func_end;
12477   }
12478   zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
12479   if( zCmd==0 ){
12480     sqlite3_result_error_nomem(context);
12481     goto edit_func_end;
12482   }
12483   rc = system(zCmd);
12484   sqlite3_free(zCmd);
12485   if( rc ){
12486     sqlite3_result_error(context, "EDITOR returned non-zero", -1);
12487     goto edit_func_end;
12488   }
12489   f = fopen(zTempFile, "rb");
12490   if( f==0 ){
12491     sqlite3_result_error(context,
12492       "edit() cannot reopen temp file after edit", -1);
12493     goto edit_func_end;
12494   }
12495   fseek(f, 0, SEEK_END);
12496   sz = ftell(f);
12497   rewind(f);
12498   p = sqlite3_malloc64( sz+1 );
12499   if( p==0 ){
12500     sqlite3_result_error_nomem(context);
12501     goto edit_func_end;
12502   }
12503   x = fread(p, 1, (size_t)sz, f);
12504   fclose(f);
12505   f = 0;
12506   if( x!=sz ){
12507     sqlite3_result_error(context, "could not read back the whole file", -1);
12508     goto edit_func_end;
12509   }
12510   if( bBin ){
12511     sqlite3_result_blob64(context, p, sz, sqlite3_free);
12512   }else{
12513     sqlite3_int64 i, j;
12514     if( hasCRNL ){
12515       /* If the original contains \r\n then do no conversions back to \n */
12516     }else{
12517       /* If the file did not originally contain \r\n then convert any new
12518       ** \r\n back into \n */
12519       for(i=j=0; i<sz; i++){
12520         if( p[i]=='\r' && p[i+1]=='\n' ) i++;
12521         p[j++] = p[i];
12522       }
12523       sz = j;
12524       p[sz] = 0;
12525     } 
12526     sqlite3_result_text64(context, (const char*)p, sz,
12527                           sqlite3_free, SQLITE_UTF8);
12528   }
12529   p = 0;
12530
12531 edit_func_end:
12532   if( f ) fclose(f);
12533   unlink(zTempFile);
12534   sqlite3_free(zTempFile);
12535   sqlite3_free(p);
12536 }
12537 #endif /* SQLITE_NOHAVE_SYSTEM */
12538
12539 /*
12540 ** Save or restore the current output mode
12541 */
12542 static void outputModePush(ShellState *p){
12543   p->modePrior = p->mode;
12544   p->priorShFlgs = p->shellFlgs;
12545   memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
12546   memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
12547 }
12548 static void outputModePop(ShellState *p){
12549   p->mode = p->modePrior;
12550   p->shellFlgs = p->priorShFlgs;
12551   memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
12552   memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
12553 }
12554
12555 /*
12556 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12557 */
12558 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
12559   int i;
12560   char *zBlob = (char *)pBlob;
12561   raw_printf(out,"X'");
12562   for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
12563   raw_printf(out,"'");
12564 }
12565
12566 /*
12567 ** Find a string that is not found anywhere in z[].  Return a pointer
12568 ** to that string.
12569 **
12570 ** Try to use zA and zB first.  If both of those are already found in z[]
12571 ** then make up some string and store it in the buffer zBuf.
12572 */
12573 static const char *unused_string(
12574   const char *z,                    /* Result must not appear anywhere in z */
12575   const char *zA, const char *zB,   /* Try these first */
12576   char *zBuf                        /* Space to store a generated string */
12577 ){
12578   unsigned i = 0;
12579   if( strstr(z, zA)==0 ) return zA;
12580   if( strstr(z, zB)==0 ) return zB;
12581   do{
12582     sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
12583   }while( strstr(z,zBuf)!=0 );
12584   return zBuf;
12585 }
12586
12587 /*
12588 ** Output the given string as a quoted string using SQL quoting conventions.
12589 **
12590 ** See also: output_quoted_escaped_string()
12591 */
12592 static void output_quoted_string(FILE *out, const char *z){
12593   int i;
12594   char c;
12595   setBinaryMode(out, 1);
12596   for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12597   if( c==0 ){
12598     utf8_printf(out,"'%s'",z);
12599   }else{
12600     raw_printf(out, "'");
12601     while( *z ){
12602       for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12603       if( c=='\'' ) i++;
12604       if( i ){
12605         utf8_printf(out, "%.*s", i, z);
12606         z += i;
12607       }
12608       if( c=='\'' ){
12609         raw_printf(out, "'");
12610         continue;
12611       }
12612       if( c==0 ){
12613         break;
12614       }
12615       z++;
12616     }
12617     raw_printf(out, "'");
12618   }
12619   setTextMode(out, 1);
12620 }
12621
12622 /*
12623 ** Output the given string as a quoted string using SQL quoting conventions.
12624 ** Additionallly , escape the "\n" and "\r" characters so that they do not
12625 ** get corrupted by end-of-line translation facilities in some operating
12626 ** systems.
12627 **
12628 ** This is like output_quoted_string() but with the addition of the \r\n
12629 ** escape mechanism.
12630 */
12631 static void output_quoted_escaped_string(FILE *out, const char *z){
12632   int i;
12633   char c;
12634   setBinaryMode(out, 1);
12635   for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
12636   if( c==0 ){
12637     utf8_printf(out,"'%s'",z);
12638   }else{
12639     const char *zNL = 0;
12640     const char *zCR = 0;
12641     int nNL = 0;
12642     int nCR = 0;
12643     char zBuf1[20], zBuf2[20];
12644     for(i=0; z[i]; i++){
12645       if( z[i]=='\n' ) nNL++;
12646       if( z[i]=='\r' ) nCR++;
12647     }
12648     if( nNL ){
12649       raw_printf(out, "replace(");
12650       zNL = unused_string(z, "\\n", "\\012", zBuf1);
12651     }
12652     if( nCR ){
12653       raw_printf(out, "replace(");
12654       zCR = unused_string(z, "\\r", "\\015", zBuf2);
12655     }
12656     raw_printf(out, "'");
12657     while( *z ){
12658       for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
12659       if( c=='\'' ) i++;
12660       if( i ){
12661         utf8_printf(out, "%.*s", i, z);
12662         z += i;
12663       }
12664       if( c=='\'' ){
12665         raw_printf(out, "'");
12666         continue;
12667       }
12668       if( c==0 ){
12669         break;
12670       }
12671       z++;
12672       if( c=='\n' ){
12673         raw_printf(out, "%s", zNL);
12674         continue;
12675       }
12676       raw_printf(out, "%s", zCR);
12677     }
12678     raw_printf(out, "'");
12679     if( nCR ){
12680       raw_printf(out, ",'%s',char(13))", zCR);
12681     }
12682     if( nNL ){
12683       raw_printf(out, ",'%s',char(10))", zNL);
12684     }
12685   }
12686   setTextMode(out, 1);
12687 }
12688
12689 /*
12690 ** Output the given string as a quoted according to C or TCL quoting rules.
12691 */
12692 static void output_c_string(FILE *out, const char *z){
12693   unsigned int c;
12694   fputc('"', out);
12695   while( (c = *(z++))!=0 ){
12696     if( c=='\\' ){
12697       fputc(c, out);
12698       fputc(c, out);
12699     }else if( c=='"' ){
12700       fputc('\\', out);
12701       fputc('"', out);
12702     }else if( c=='\t' ){
12703       fputc('\\', out);
12704       fputc('t', out);
12705     }else if( c=='\n' ){
12706       fputc('\\', out);
12707       fputc('n', out);
12708     }else if( c=='\r' ){
12709       fputc('\\', out);
12710       fputc('r', out);
12711     }else if( !isprint(c&0xff) ){
12712       raw_printf(out, "\\%03o", c&0xff);
12713     }else{
12714       fputc(c, out);
12715     }
12716   }
12717   fputc('"', out);
12718 }
12719
12720 /*
12721 ** Output the given string as a quoted according to JSON quoting rules.
12722 */
12723 static void output_json_string(FILE *out, const char *z, int n){
12724   unsigned int c;
12725   if( n<0 ) n = (int)strlen(z);
12726   fputc('"', out);
12727   while( n-- ){
12728     c = *(z++);
12729     if( c=='\\' || c=='"' ){
12730       fputc('\\', out);
12731       fputc(c, out);
12732     }else if( c<=0x1f ){
12733       fputc('\\', out);
12734       if( c=='\b' ){
12735         fputc('b', out);
12736       }else if( c=='\f' ){
12737         fputc('f', out);
12738       }else if( c=='\n' ){
12739         fputc('n', out);
12740       }else if( c=='\r' ){
12741         fputc('r', out);
12742       }else if( c=='\t' ){
12743         fputc('t', out);
12744       }else{
12745          raw_printf(out, "u%04x",c);
12746       }
12747     }else{
12748       fputc(c, out);
12749     }
12750   }
12751   fputc('"', out);
12752 }
12753
12754 /*
12755 ** Output the given string with characters that are special to
12756 ** HTML escaped.
12757 */
12758 static void output_html_string(FILE *out, const char *z){
12759   int i;
12760   if( z==0 ) z = "";
12761   while( *z ){
12762     for(i=0;   z[i]
12763             && z[i]!='<'
12764             && z[i]!='&'
12765             && z[i]!='>'
12766             && z[i]!='\"'
12767             && z[i]!='\'';
12768         i++){}
12769     if( i>0 ){
12770       utf8_printf(out,"%.*s",i,z);
12771     }
12772     if( z[i]=='<' ){
12773       raw_printf(out,"&lt;");
12774     }else if( z[i]=='&' ){
12775       raw_printf(out,"&amp;");
12776     }else if( z[i]=='>' ){
12777       raw_printf(out,"&gt;");
12778     }else if( z[i]=='\"' ){
12779       raw_printf(out,"&quot;");
12780     }else if( z[i]=='\'' ){
12781       raw_printf(out,"&#39;");
12782     }else{
12783       break;
12784     }
12785     z += i + 1;
12786   }
12787 }
12788
12789 /*
12790 ** If a field contains any character identified by a 1 in the following
12791 ** array, then the string must be quoted for CSV.
12792 */
12793 static const char needCsvQuote[] = {
12794   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12795   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12796   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
12797   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12798   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12799   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12800   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
12801   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
12802   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12803   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12804   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12805   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12806   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12807   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12808   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12809   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
12810 };
12811
12812 /*
12813 ** Output a single term of CSV.  Actually, p->colSeparator is used for
12814 ** the separator, which may or may not be a comma.  p->nullValue is
12815 ** the null value.  Strings are quoted if necessary.  The separator
12816 ** is only issued if bSep is true.
12817 */
12818 static void output_csv(ShellState *p, const char *z, int bSep){
12819   FILE *out = p->out;
12820   if( z==0 ){
12821     utf8_printf(out,"%s",p->nullValue);
12822   }else{
12823     unsigned i;
12824     for(i=0; z[i]; i++){
12825       if( needCsvQuote[((unsigned char*)z)[i]] ){
12826         i = 0;
12827         break;
12828       }
12829     }
12830     if( i==0 || strstr(z, p->colSeparator)!=0 ){
12831       char *zQuoted = sqlite3_mprintf("\"%w\"", z);
12832       utf8_printf(out, "%s", zQuoted);
12833       sqlite3_free(zQuoted);
12834     }else{
12835       utf8_printf(out, "%s", z);
12836     }
12837   }
12838   if( bSep ){
12839     utf8_printf(p->out, "%s", p->colSeparator);
12840   }
12841 }
12842
12843 /*
12844 ** This routine runs when the user presses Ctrl-C
12845 */
12846 static void interrupt_handler(int NotUsed){
12847   UNUSED_PARAMETER(NotUsed);
12848   seenInterrupt++;
12849   if( seenInterrupt>2 ) exit(1);
12850   if( globalDb ) sqlite3_interrupt(globalDb);
12851 }
12852
12853 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12854 /*
12855 ** This routine runs for console events (e.g. Ctrl-C) on Win32
12856 */
12857 static BOOL WINAPI ConsoleCtrlHandler(
12858   DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
12859 ){
12860   if( dwCtrlType==CTRL_C_EVENT ){
12861     interrupt_handler(0);
12862     return TRUE;
12863   }
12864   return FALSE;
12865 }
12866 #endif
12867
12868 #ifndef SQLITE_OMIT_AUTHORIZATION
12869 /*
12870 ** This authorizer runs in safe mode.
12871 */
12872 static int safeModeAuth(
12873   void *pClientData,
12874   int op,
12875   const char *zA1,
12876   const char *zA2,
12877   const char *zA3,
12878   const char *zA4
12879 ){
12880   ShellState *p = (ShellState*)pClientData;
12881   static const char *azProhibitedFunctions[] = {
12882     "edit",
12883     "fts3_tokenizer",
12884     "load_extension",
12885     "readfile",
12886     "writefile",
12887     "zipfile",
12888     "zipfile_cds",
12889   };
12890   UNUSED_PARAMETER(zA2);
12891   UNUSED_PARAMETER(zA3);
12892   UNUSED_PARAMETER(zA4);
12893   switch( op ){
12894     case SQLITE_ATTACH: {
12895       failIfSafeMode(p, "cannot run ATTACH in safe mode");
12896       break;
12897     }
12898     case SQLITE_FUNCTION: {
12899       int i;
12900       for(i=0; i<ArraySize(azProhibitedFunctions); i++){
12901         if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
12902           failIfSafeMode(p, "cannot use the %s() function in safe mode",
12903                          azProhibitedFunctions[i]);
12904         }
12905       }
12906       break;
12907     }
12908   }
12909   return SQLITE_OK;
12910 }
12911
12912 /*
12913 ** When the ".auth ON" is set, the following authorizer callback is
12914 ** invoked.  It always returns SQLITE_OK.
12915 */
12916 static int shellAuth(
12917   void *pClientData,
12918   int op,
12919   const char *zA1,
12920   const char *zA2,
12921   const char *zA3,
12922   const char *zA4
12923 ){
12924   ShellState *p = (ShellState*)pClientData;
12925   static const char *azAction[] = { 0,
12926      "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
12927      "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
12928      "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
12929      "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
12930      "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
12931      "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
12932      "PRAGMA",               "READ",                 "SELECT",
12933      "TRANSACTION",          "UPDATE",               "ATTACH",
12934      "DETACH",               "ALTER_TABLE",          "REINDEX",
12935      "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
12936      "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
12937   };
12938   int i;
12939   const char *az[4];
12940   az[0] = zA1;
12941   az[1] = zA2;
12942   az[2] = zA3;
12943   az[3] = zA4;
12944   utf8_printf(p->out, "authorizer: %s", azAction[op]);
12945   for(i=0; i<4; i++){
12946     raw_printf(p->out, " ");
12947     if( az[i] ){
12948       output_c_string(p->out, az[i]);
12949     }else{
12950       raw_printf(p->out, "NULL");
12951     }
12952   }
12953   raw_printf(p->out, "\n");
12954   if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
12955   return SQLITE_OK;
12956 }
12957 #endif
12958
12959 /*
12960 ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
12961 **
12962 ** This routine converts some CREATE TABLE statements for shadow tables
12963 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
12964 */
12965 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
12966   if( z==0 ) return;
12967   if( zTail==0 ) return;
12968   if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
12969     utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
12970   }else{
12971     utf8_printf(out, "%s%s", z, zTail);
12972   }
12973 }
12974 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
12975   char c = z[n];
12976   z[n] = 0;
12977   printSchemaLine(out, z, zTail);
12978   z[n] = c;
12979 }
12980
12981 /*
12982 ** Return true if string z[] has nothing but whitespace and comments to the
12983 ** end of the first line.
12984 */
12985 static int wsToEol(const char *z){
12986   int i;
12987   for(i=0; z[i]; i++){
12988     if( z[i]=='\n' ) return 1;
12989     if( IsSpace(z[i]) ) continue;
12990     if( z[i]=='-' && z[i+1]=='-' ) return 1;
12991     return 0;
12992   }
12993   return 1;
12994 }
12995
12996 /*
12997 ** Add a new entry to the EXPLAIN QUERY PLAN data
12998 */
12999 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
13000   EQPGraphRow *pNew;
13001   int nText = strlen30(zText);
13002   if( p->autoEQPtest ){
13003     utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
13004   }
13005   pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
13006   if( pNew==0 ) shell_out_of_memory();
13007   pNew->iEqpId = iEqpId;
13008   pNew->iParentId = p2;
13009   memcpy(pNew->zText, zText, nText+1);
13010   pNew->pNext = 0;
13011   if( p->sGraph.pLast ){
13012     p->sGraph.pLast->pNext = pNew;
13013   }else{
13014     p->sGraph.pRow = pNew;
13015   }
13016   p->sGraph.pLast = pNew;
13017 }
13018
13019 /*
13020 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
13021 ** in p->sGraph.
13022 */
13023 static void eqp_reset(ShellState *p){
13024   EQPGraphRow *pRow, *pNext;
13025   for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
13026     pNext = pRow->pNext;
13027     sqlite3_free(pRow);
13028   }
13029   memset(&p->sGraph, 0, sizeof(p->sGraph));
13030 }
13031
13032 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
13033 ** pOld, or return the first such line if pOld is NULL
13034 */
13035 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
13036   EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
13037   while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
13038   return pRow;
13039 }
13040
13041 /* Render a single level of the graph that has iEqpId as its parent.  Called
13042 ** recursively to render sublevels.
13043 */
13044 static void eqp_render_level(ShellState *p, int iEqpId){
13045   EQPGraphRow *pRow, *pNext;
13046   int n = strlen30(p->sGraph.zPrefix);
13047   char *z;
13048   for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
13049     pNext = eqp_next_row(p, iEqpId, pRow);
13050     z = pRow->zText;
13051     utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
13052                 pNext ? "|--" : "`--", z);
13053     if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
13054       memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
13055       eqp_render_level(p, pRow->iEqpId);
13056       p->sGraph.zPrefix[n] = 0;
13057     }
13058   }
13059 }
13060
13061 /*
13062 ** Display and reset the EXPLAIN QUERY PLAN data
13063 */
13064 static void eqp_render(ShellState *p){
13065   EQPGraphRow *pRow = p->sGraph.pRow;
13066   if( pRow ){
13067     if( pRow->zText[0]=='-' ){
13068       if( pRow->pNext==0 ){
13069         eqp_reset(p);
13070         return;
13071       }
13072       utf8_printf(p->out, "%s\n", pRow->zText+3);
13073       p->sGraph.pRow = pRow->pNext;
13074       sqlite3_free(pRow);
13075     }else{
13076       utf8_printf(p->out, "QUERY PLAN\n");
13077     }
13078     p->sGraph.zPrefix[0] = 0;
13079     eqp_render_level(p, 0);
13080     eqp_reset(p);
13081   }
13082 }
13083
13084 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
13085 /*
13086 ** Progress handler callback.
13087 */
13088 static int progress_handler(void *pClientData) {
13089   ShellState *p = (ShellState*)pClientData;
13090   p->nProgress++;
13091   if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
13092     raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
13093     if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
13094     if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
13095     return 1;
13096   }
13097   if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
13098     raw_printf(p->out, "Progress %u\n", p->nProgress);
13099   }
13100   return 0;
13101 }
13102 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
13103
13104 /*
13105 ** Print N dashes
13106 */
13107 static void print_dashes(FILE *out, int N){
13108   const char zDash[] = "--------------------------------------------------";
13109   const int nDash = sizeof(zDash) - 1;
13110   while( N>nDash ){
13111     fputs(zDash, out);
13112     N -= nDash;
13113   }
13114   raw_printf(out, "%.*s", N, zDash);
13115 }
13116
13117 /*
13118 ** Print a markdown or table-style row separator using ascii-art
13119 */
13120 static void print_row_separator(
13121   ShellState *p,
13122   int nArg,
13123   const char *zSep
13124 ){
13125   int i;
13126   if( nArg>0 ){
13127     fputs(zSep, p->out);
13128     print_dashes(p->out, p->actualWidth[0]+2);
13129     for(i=1; i<nArg; i++){
13130       fputs(zSep, p->out);
13131       print_dashes(p->out, p->actualWidth[i]+2);
13132     }
13133     fputs(zSep, p->out);
13134   }
13135   fputs("\n", p->out);
13136 }
13137
13138 /*
13139 ** This is the callback routine that the shell
13140 ** invokes for each row of a query result.
13141 */
13142 static int shell_callback(
13143   void *pArg,
13144   int nArg,        /* Number of result columns */
13145   char **azArg,    /* Text of each result column */
13146   char **azCol,    /* Column names */
13147   int *aiType      /* Column types.  Might be NULL */
13148 ){
13149   int i;
13150   ShellState *p = (ShellState*)pArg;
13151
13152   if( azArg==0 ) return 0;
13153   switch( p->cMode ){
13154     case MODE_Line: {
13155       int w = 5;
13156       if( azArg==0 ) break;
13157       for(i=0; i<nArg; i++){
13158         int len = strlen30(azCol[i] ? azCol[i] : "");
13159         if( len>w ) w = len;
13160       }
13161       if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
13162       for(i=0; i<nArg; i++){
13163         utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
13164                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
13165       }
13166       break;
13167     }
13168     case MODE_Explain: {
13169       static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
13170       if( nArg>ArraySize(aExplainWidth) ){
13171         nArg = ArraySize(aExplainWidth);
13172       }
13173       if( p->cnt++==0 ){
13174         for(i=0; i<nArg; i++){
13175           int w = aExplainWidth[i];
13176           utf8_width_print(p->out, w, azCol[i]);
13177           fputs(i==nArg-1 ? "\n" : "  ", p->out);
13178         }
13179         for(i=0; i<nArg; i++){
13180           int w = aExplainWidth[i];
13181           print_dashes(p->out, w);
13182           fputs(i==nArg-1 ? "\n" : "  ", p->out);
13183         }
13184       }
13185       if( azArg==0 ) break;
13186       for(i=0; i<nArg; i++){
13187         int w = aExplainWidth[i];
13188         if( i==nArg-1 ) w = 0;
13189         if( azArg[i] && strlenChar(azArg[i])>w ){
13190           w = strlenChar(azArg[i]);
13191         }
13192         if( i==1 && p->aiIndent && p->pStmt ){
13193           if( p->iIndent<p->nIndent ){
13194             utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
13195           }
13196           p->iIndent++;
13197         }
13198         utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
13199         fputs(i==nArg-1 ? "\n" : "  ", p->out);
13200       }
13201       break;
13202     }
13203     case MODE_Semi: {   /* .schema and .fullschema output */
13204       printSchemaLine(p->out, azArg[0], ";\n");
13205       break;
13206     }
13207     case MODE_Pretty: {  /* .schema and .fullschema with --indent */
13208       char *z;
13209       int j;
13210       int nParen = 0;
13211       char cEnd = 0;
13212       char c;
13213       int nLine = 0;
13214       assert( nArg==1 );
13215       if( azArg[0]==0 ) break;
13216       if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
13217        || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
13218       ){
13219         utf8_printf(p->out, "%s;\n", azArg[0]);
13220         break;
13221       }
13222       z = sqlite3_mprintf("%s", azArg[0]);
13223       j = 0;
13224       for(i=0; IsSpace(z[i]); i++){}
13225       for(; (c = z[i])!=0; i++){
13226         if( IsSpace(c) ){
13227           if( z[j-1]=='\r' ) z[j-1] = '\n';
13228           if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
13229         }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
13230           j--;
13231         }
13232         z[j++] = c;
13233       }
13234       while( j>0 && IsSpace(z[j-1]) ){ j--; }
13235       z[j] = 0;
13236       if( strlen30(z)>=79 ){
13237         for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
13238           if( c==cEnd ){
13239             cEnd = 0;
13240           }else if( c=='"' || c=='\'' || c=='`' ){
13241             cEnd = c;
13242           }else if( c=='[' ){
13243             cEnd = ']';
13244           }else if( c=='-' && z[i+1]=='-' ){
13245             cEnd = '\n';
13246           }else if( c=='(' ){
13247             nParen++;
13248           }else if( c==')' ){
13249             nParen--;
13250             if( nLine>0 && nParen==0 && j>0 ){
13251               printSchemaLineN(p->out, z, j, "\n");
13252               j = 0;
13253             }
13254           }
13255           z[j++] = c;
13256           if( nParen==1 && cEnd==0
13257            && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
13258           ){
13259             if( c=='\n' ) j--;
13260             printSchemaLineN(p->out, z, j, "\n  ");
13261             j = 0;
13262             nLine++;
13263             while( IsSpace(z[i+1]) ){ i++; }
13264           }
13265         }
13266         z[j] = 0;
13267       }
13268       printSchemaLine(p->out, z, ";\n");
13269       sqlite3_free(z);
13270       break;
13271     }
13272     case MODE_List: {
13273       if( p->cnt++==0 && p->showHeader ){
13274         for(i=0; i<nArg; i++){
13275           utf8_printf(p->out,"%s%s",azCol[i],
13276                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
13277         }
13278       }
13279       if( azArg==0 ) break;
13280       for(i=0; i<nArg; i++){
13281         char *z = azArg[i];
13282         if( z==0 ) z = p->nullValue;
13283         utf8_printf(p->out, "%s", z);
13284         if( i<nArg-1 ){
13285           utf8_printf(p->out, "%s", p->colSeparator);
13286         }else{
13287           utf8_printf(p->out, "%s", p->rowSeparator);
13288         }
13289       }
13290       break;
13291     }
13292     case MODE_Html: {
13293       if( p->cnt++==0 && p->showHeader ){
13294         raw_printf(p->out,"<TR>");
13295         for(i=0; i<nArg; i++){
13296           raw_printf(p->out,"<TH>");
13297           output_html_string(p->out, azCol[i]);
13298           raw_printf(p->out,"</TH>\n");
13299         }
13300         raw_printf(p->out,"</TR>\n");
13301       }
13302       if( azArg==0 ) break;
13303       raw_printf(p->out,"<TR>");
13304       for(i=0; i<nArg; i++){
13305         raw_printf(p->out,"<TD>");
13306         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13307         raw_printf(p->out,"</TD>\n");
13308       }
13309       raw_printf(p->out,"</TR>\n");
13310       break;
13311     }
13312     case MODE_Tcl: {
13313       if( p->cnt++==0 && p->showHeader ){
13314         for(i=0; i<nArg; i++){
13315           output_c_string(p->out,azCol[i] ? azCol[i] : "");
13316           if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13317         }
13318         utf8_printf(p->out, "%s", p->rowSeparator);
13319       }
13320       if( azArg==0 ) break;
13321       for(i=0; i<nArg; i++){
13322         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13323         if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13324       }
13325       utf8_printf(p->out, "%s", p->rowSeparator);
13326       break;
13327     }
13328     case MODE_Csv: {
13329       setBinaryMode(p->out, 1);
13330       if( p->cnt++==0 && p->showHeader ){
13331         for(i=0; i<nArg; i++){
13332           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
13333         }
13334         utf8_printf(p->out, "%s", p->rowSeparator);
13335       }
13336       if( nArg>0 ){
13337         for(i=0; i<nArg; i++){
13338           output_csv(p, azArg[i], i<nArg-1);
13339         }
13340         utf8_printf(p->out, "%s", p->rowSeparator);
13341       }
13342       setTextMode(p->out, 1);
13343       break;
13344     }
13345     case MODE_Insert: {
13346       if( azArg==0 ) break;
13347       utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
13348       if( p->showHeader ){
13349         raw_printf(p->out,"(");
13350         for(i=0; i<nArg; i++){
13351           if( i>0 ) raw_printf(p->out, ",");
13352           if( quoteChar(azCol[i]) ){
13353             char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
13354             utf8_printf(p->out, "%s", z);
13355             sqlite3_free(z);
13356           }else{
13357             raw_printf(p->out, "%s", azCol[i]);
13358           }
13359         }
13360         raw_printf(p->out,")");
13361       }
13362       p->cnt++;
13363       for(i=0; i<nArg; i++){
13364         raw_printf(p->out, i>0 ? "," : " VALUES(");
13365         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13366           utf8_printf(p->out,"NULL");
13367         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13368           if( ShellHasFlag(p, SHFLG_Newlines) ){
13369             output_quoted_string(p->out, azArg[i]);
13370           }else{
13371             output_quoted_escaped_string(p->out, azArg[i]);
13372           }
13373         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13374           utf8_printf(p->out,"%s", azArg[i]);
13375         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13376           char z[50];
13377           double r = sqlite3_column_double(p->pStmt, i);
13378           sqlite3_uint64 ur;
13379           memcpy(&ur,&r,sizeof(r));
13380           if( ur==0x7ff0000000000000LL ){
13381             raw_printf(p->out, "1e999");
13382           }else if( ur==0xfff0000000000000LL ){
13383             raw_printf(p->out, "-1e999");
13384           }else{
13385             sqlite3_snprintf(50,z,"%!.20g", r);
13386             raw_printf(p->out, "%s", z);
13387           }
13388         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13389           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13390           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13391           output_hex_blob(p->out, pBlob, nBlob);
13392         }else if( isNumber(azArg[i], 0) ){
13393           utf8_printf(p->out,"%s", azArg[i]);
13394         }else if( ShellHasFlag(p, SHFLG_Newlines) ){
13395           output_quoted_string(p->out, azArg[i]);
13396         }else{
13397           output_quoted_escaped_string(p->out, azArg[i]);
13398         }
13399       }
13400       raw_printf(p->out,");\n");
13401       break;
13402     }
13403     case MODE_Json: {
13404       if( azArg==0 ) break;
13405       if( p->cnt==0 ){
13406         fputs("[{", p->out);
13407       }else{
13408         fputs(",\n{", p->out);
13409       }
13410       p->cnt++;
13411       for(i=0; i<nArg; i++){
13412         output_json_string(p->out, azCol[i], -1);
13413         putc(':', p->out);
13414         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13415           fputs("null",p->out);
13416         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13417           char z[50];
13418           double r = sqlite3_column_double(p->pStmt, i);
13419           sqlite3_uint64 ur;
13420           memcpy(&ur,&r,sizeof(r));
13421           if( ur==0x7ff0000000000000LL ){
13422             raw_printf(p->out, "1e999");
13423           }else if( ur==0xfff0000000000000LL ){
13424             raw_printf(p->out, "-1e999");
13425           }else{
13426             sqlite3_snprintf(50,z,"%!.20g", r);
13427             raw_printf(p->out, "%s", z);
13428           }
13429         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13430           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13431           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13432           output_json_string(p->out, pBlob, nBlob);
13433         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13434           output_json_string(p->out, azArg[i], -1);
13435         }else{
13436           utf8_printf(p->out,"%s", azArg[i]);
13437         }
13438         if( i<nArg-1 ){
13439           putc(',', p->out);
13440         }
13441       }
13442       putc('}', p->out);
13443       break;
13444     }
13445     case MODE_Quote: {
13446       if( azArg==0 ) break;
13447       if( p->cnt==0 && p->showHeader ){
13448         for(i=0; i<nArg; i++){
13449           if( i>0 ) fputs(p->colSeparator, p->out);
13450           output_quoted_string(p->out, azCol[i]);
13451         }
13452         fputs(p->rowSeparator, p->out);
13453       }
13454       p->cnt++;
13455       for(i=0; i<nArg; i++){
13456         if( i>0 ) fputs(p->colSeparator, p->out);
13457         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13458           utf8_printf(p->out,"NULL");
13459         }else if( aiType && aiType[i]==SQLITE_TEXT ){
13460           output_quoted_string(p->out, azArg[i]);
13461         }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13462           utf8_printf(p->out,"%s", azArg[i]);
13463         }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13464           char z[50];
13465           double r = sqlite3_column_double(p->pStmt, i);
13466           sqlite3_snprintf(50,z,"%!.20g", r);
13467           raw_printf(p->out, "%s", z);
13468         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13469           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13470           int nBlob = sqlite3_column_bytes(p->pStmt, i);
13471           output_hex_blob(p->out, pBlob, nBlob);
13472         }else if( isNumber(azArg[i], 0) ){
13473           utf8_printf(p->out,"%s", azArg[i]);
13474         }else{
13475           output_quoted_string(p->out, azArg[i]);
13476         }
13477       }
13478       fputs(p->rowSeparator, p->out);
13479       break;
13480     }
13481     case MODE_Ascii: {
13482       if( p->cnt++==0 && p->showHeader ){
13483         for(i=0; i<nArg; i++){
13484           if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13485           utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
13486         }
13487         utf8_printf(p->out, "%s", p->rowSeparator);
13488       }
13489       if( azArg==0 ) break;
13490       for(i=0; i<nArg; i++){
13491         if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13492         utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
13493       }
13494       utf8_printf(p->out, "%s", p->rowSeparator);
13495       break;
13496     }
13497     case MODE_EQP: {
13498       eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
13499       break;
13500     }
13501   }
13502   return 0;
13503 }
13504
13505 /*
13506 ** This is the callback routine that the SQLite library
13507 ** invokes for each row of a query result.
13508 */
13509 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
13510   /* since we don't have type info, call the shell_callback with a NULL value */
13511   return shell_callback(pArg, nArg, azArg, azCol, NULL);
13512 }
13513
13514 /*
13515 ** This is the callback routine from sqlite3_exec() that appends all
13516 ** output onto the end of a ShellText object.
13517 */
13518 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
13519   ShellText *p = (ShellText*)pArg;
13520   int i;
13521   UNUSED_PARAMETER(az);
13522   if( azArg==0 ) return 0;
13523   if( p->n ) appendText(p, "|", 0);
13524   for(i=0; i<nArg; i++){
13525     if( i ) appendText(p, ",", 0);
13526     if( azArg[i] ) appendText(p, azArg[i], 0);
13527   }
13528   return 0;
13529 }
13530
13531 /*
13532 ** Generate an appropriate SELFTEST table in the main database.
13533 */
13534 static void createSelftestTable(ShellState *p){
13535   char *zErrMsg = 0;
13536   sqlite3_exec(p->db,
13537     "SAVEPOINT selftest_init;\n"
13538     "CREATE TABLE IF NOT EXISTS selftest(\n"
13539     "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
13540     "  op TEXT,\n"                   /* Operator:  memo run */
13541     "  cmd TEXT,\n"                  /* Command text */
13542     "  ans TEXT\n"                   /* Desired answer */
13543     ");"
13544     "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
13545     "INSERT INTO [_shell$self](rowid,op,cmd)\n"
13546     "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
13547     "         'memo','Tests generated by --init');\n"
13548     "INSERT INTO [_shell$self]\n"
13549     "  SELECT 'run',\n"
13550     "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
13551                                  "FROM sqlite_schema ORDER BY 2'',224))',\n"
13552     "    hex(sha3_query('SELECT type,name,tbl_name,sql "
13553                           "FROM sqlite_schema ORDER BY 2',224));\n"
13554     "INSERT INTO [_shell$self]\n"
13555     "  SELECT 'run',"
13556     "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
13557     "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
13558     "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
13559     "  FROM (\n"
13560     "    SELECT name FROM sqlite_schema\n"
13561     "     WHERE type='table'\n"
13562     "       AND name<>'selftest'\n"
13563     "       AND coalesce(rootpage,0)>0\n"
13564     "  )\n"
13565     " ORDER BY name;\n"
13566     "INSERT INTO [_shell$self]\n"
13567     "  VALUES('run','PRAGMA integrity_check','ok');\n"
13568     "INSERT INTO selftest(tno,op,cmd,ans)"
13569     "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
13570     "DROP TABLE [_shell$self];"
13571     ,0,0,&zErrMsg);
13572   if( zErrMsg ){
13573     utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
13574     sqlite3_free(zErrMsg);
13575   }
13576   sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
13577 }
13578
13579
13580 /*
13581 ** Set the destination table field of the ShellState structure to
13582 ** the name of the table given.  Escape any quote characters in the
13583 ** table name.
13584 */
13585 static void set_table_name(ShellState *p, const char *zName){
13586   int i, n;
13587   char cQuote;
13588   char *z;
13589
13590   if( p->zDestTable ){
13591     free(p->zDestTable);
13592     p->zDestTable = 0;
13593   }
13594   if( zName==0 ) return;
13595   cQuote = quoteChar(zName);
13596   n = strlen30(zName);
13597   if( cQuote ) n += n+2;
13598   z = p->zDestTable = malloc( n+1 );
13599   if( z==0 ) shell_out_of_memory();
13600   n = 0;
13601   if( cQuote ) z[n++] = cQuote;
13602   for(i=0; zName[i]; i++){
13603     z[n++] = zName[i];
13604     if( zName[i]==cQuote ) z[n++] = cQuote;
13605   }
13606   if( cQuote ) z[n++] = cQuote;
13607   z[n] = 0;
13608 }
13609
13610
13611 /*
13612 ** Execute a query statement that will generate SQL output.  Print
13613 ** the result columns, comma-separated, on a line and then add a
13614 ** semicolon terminator to the end of that line.
13615 **
13616 ** If the number of columns is 1 and that column contains text "--"
13617 ** then write the semicolon on a separate line.  That way, if a
13618 ** "--" comment occurs at the end of the statement, the comment
13619 ** won't consume the semicolon terminator.
13620 */
13621 static int run_table_dump_query(
13622   ShellState *p,           /* Query context */
13623   const char *zSelect      /* SELECT statement to extract content */
13624 ){
13625   sqlite3_stmt *pSelect;
13626   int rc;
13627   int nResult;
13628   int i;
13629   const char *z;
13630   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
13631   if( rc!=SQLITE_OK || !pSelect ){
13632     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13633                 sqlite3_errmsg(p->db));
13634     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13635     return rc;
13636   }
13637   rc = sqlite3_step(pSelect);
13638   nResult = sqlite3_column_count(pSelect);
13639   while( rc==SQLITE_ROW ){
13640     z = (const char*)sqlite3_column_text(pSelect, 0);
13641     utf8_printf(p->out, "%s", z);
13642     for(i=1; i<nResult; i++){
13643       utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
13644     }
13645     if( z==0 ) z = "";
13646     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
13647     if( z[0] ){
13648       raw_printf(p->out, "\n;\n");
13649     }else{
13650       raw_printf(p->out, ";\n");
13651     }
13652     rc = sqlite3_step(pSelect);
13653   }
13654   rc = sqlite3_finalize(pSelect);
13655   if( rc!=SQLITE_OK ){
13656     utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13657                 sqlite3_errmsg(p->db));
13658     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13659   }
13660   return rc;
13661 }
13662
13663 /*
13664 ** Allocate space and save off string indicating current error.
13665 */
13666 static char *save_err_msg(
13667   sqlite3 *db,           /* Database to query */
13668   const char *zWhen,     /* Qualifier (format) wrapper */
13669   int rc                 /* Error code returned from API */
13670 ){
13671   if( zWhen==0 )
13672     zWhen = "%s (%d)";
13673   return sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc);
13674 }
13675
13676 #ifdef __linux__
13677 /*
13678 ** Attempt to display I/O stats on Linux using /proc/PID/io
13679 */
13680 static void displayLinuxIoStats(FILE *out){
13681   FILE *in;
13682   char z[200];
13683   sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
13684   in = fopen(z, "rb");
13685   if( in==0 ) return;
13686   while( fgets(z, sizeof(z), in)!=0 ){
13687     static const struct {
13688       const char *zPattern;
13689       const char *zDesc;
13690     } aTrans[] = {
13691       { "rchar: ",                  "Bytes received by read():" },
13692       { "wchar: ",                  "Bytes sent to write():"    },
13693       { "syscr: ",                  "Read() system calls:"      },
13694       { "syscw: ",                  "Write() system calls:"     },
13695       { "read_bytes: ",             "Bytes read from storage:"  },
13696       { "write_bytes: ",            "Bytes written to storage:" },
13697       { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
13698     };
13699     int i;
13700     for(i=0; i<ArraySize(aTrans); i++){
13701       int n = strlen30(aTrans[i].zPattern);
13702       if( strncmp(aTrans[i].zPattern, z, n)==0 ){
13703         utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
13704         break;
13705       }
13706     }
13707   }
13708   fclose(in);
13709 }
13710 #endif
13711
13712 /*
13713 ** Display a single line of status using 64-bit values.
13714 */
13715 static void displayStatLine(
13716   ShellState *p,            /* The shell context */
13717   char *zLabel,             /* Label for this one line */
13718   char *zFormat,            /* Format for the result */
13719   int iStatusCtrl,          /* Which status to display */
13720   int bReset                /* True to reset the stats */
13721 ){
13722   sqlite3_int64 iCur = -1;
13723   sqlite3_int64 iHiwtr = -1;
13724   int i, nPercent;
13725   char zLine[200];
13726   sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
13727   for(i=0, nPercent=0; zFormat[i]; i++){
13728     if( zFormat[i]=='%' ) nPercent++;
13729   }
13730   if( nPercent>1 ){
13731     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
13732   }else{
13733     sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
13734   }
13735   raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
13736 }
13737
13738 /*
13739 ** Display memory stats.
13740 */
13741 static int display_stats(
13742   sqlite3 *db,                /* Database to query */
13743   ShellState *pArg,           /* Pointer to ShellState */
13744   int bReset                  /* True to reset the stats */
13745 ){
13746   int iCur;
13747   int iHiwtr;
13748   FILE *out;
13749   if( pArg==0 || pArg->out==0 ) return 0;
13750   out = pArg->out;
13751
13752   if( pArg->pStmt && pArg->statsOn==2 ){
13753     int nCol, i, x;
13754     sqlite3_stmt *pStmt = pArg->pStmt;
13755     char z[100];
13756     nCol = sqlite3_column_count(pStmt);
13757     raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
13758     for(i=0; i<nCol; i++){
13759       sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
13760       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
13761 #ifndef SQLITE_OMIT_DECLTYPE
13762       sqlite3_snprintf(30, z+x, "declared type:");
13763       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
13764 #endif
13765 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13766       sqlite3_snprintf(30, z+x, "database name:");
13767       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
13768       sqlite3_snprintf(30, z+x, "table name:");
13769       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
13770       sqlite3_snprintf(30, z+x, "origin name:");
13771       utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
13772 #endif
13773     }
13774   }
13775
13776   if( pArg->statsOn==3 ){
13777     if( pArg->pStmt ){
13778       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13779       raw_printf(pArg->out, "VM-steps: %d\n", iCur);
13780     }
13781     return 0;
13782   }
13783
13784   displayStatLine(pArg, "Memory Used:",
13785      "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
13786   displayStatLine(pArg, "Number of Outstanding Allocations:",
13787      "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
13788   if( pArg->shellFlgs & SHFLG_Pagecache ){
13789     displayStatLine(pArg, "Number of Pcache Pages Used:",
13790        "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
13791   }
13792   displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
13793      "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
13794   displayStatLine(pArg, "Largest Allocation:",
13795      "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
13796   displayStatLine(pArg, "Largest Pcache Allocation:",
13797      "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
13798 #ifdef YYTRACKMAXSTACKDEPTH
13799   displayStatLine(pArg, "Deepest Parser Stack:",
13800      "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
13801 #endif
13802
13803   if( db ){
13804     if( pArg->shellFlgs & SHFLG_Lookaside ){
13805       iHiwtr = iCur = -1;
13806       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
13807                         &iCur, &iHiwtr, bReset);
13808       raw_printf(pArg->out,
13809               "Lookaside Slots Used:                %d (max %d)\n",
13810               iCur, iHiwtr);
13811       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
13812                         &iCur, &iHiwtr, bReset);
13813       raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
13814               iHiwtr);
13815       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
13816                         &iCur, &iHiwtr, bReset);
13817       raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
13818               iHiwtr);
13819       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
13820                         &iCur, &iHiwtr, bReset);
13821       raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
13822               iHiwtr);
13823     }
13824     iHiwtr = iCur = -1;
13825     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
13826     raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
13827             iCur);
13828     iHiwtr = iCur = -1;
13829     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
13830     raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
13831     iHiwtr = iCur = -1;
13832     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
13833     raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
13834     iHiwtr = iCur = -1;
13835     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
13836     raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
13837     iHiwtr = iCur = -1;
13838     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
13839     raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
13840     iHiwtr = iCur = -1;
13841     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
13842     raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
13843             iCur);
13844     iHiwtr = iCur = -1;
13845     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
13846     raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
13847             iCur);
13848   }
13849
13850   if( pArg->pStmt ){
13851     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
13852                                bReset);
13853     raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
13854     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
13855     raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
13856     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
13857     raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
13858     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13859     raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
13860     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
13861     raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
13862     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
13863     raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
13864     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
13865     raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
13866   }
13867
13868 #ifdef __linux__
13869   displayLinuxIoStats(pArg->out);
13870 #endif
13871
13872   /* Do not remove this machine readable comment: extra-stats-output-here */
13873
13874   return 0;
13875 }
13876
13877 /*
13878 ** Display scan stats.
13879 */
13880 static void display_scanstats(
13881   sqlite3 *db,                    /* Database to query */
13882   ShellState *pArg                /* Pointer to ShellState */
13883 ){
13884 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13885   UNUSED_PARAMETER(db);
13886   UNUSED_PARAMETER(pArg);
13887 #else
13888   int i, k, n, mx;
13889   raw_printf(pArg->out, "-------- scanstats --------\n");
13890   mx = 0;
13891   for(k=0; k<=mx; k++){
13892     double rEstLoop = 1.0;
13893     for(i=n=0; 1; i++){
13894       sqlite3_stmt *p = pArg->pStmt;
13895       sqlite3_int64 nLoop, nVisit;
13896       double rEst;
13897       int iSid;
13898       const char *zExplain;
13899       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
13900         break;
13901       }
13902       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
13903       if( iSid>mx ) mx = iSid;
13904       if( iSid!=k ) continue;
13905       if( n==0 ){
13906         rEstLoop = (double)nLoop;
13907         if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
13908       }
13909       n++;
13910       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
13911       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
13912       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
13913       utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
13914       rEstLoop *= rEst;
13915       raw_printf(pArg->out,
13916           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
13917           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
13918       );
13919     }
13920   }
13921   raw_printf(pArg->out, "---------------------------\n");
13922 #endif
13923 }
13924
13925 /*
13926 ** Parameter azArray points to a zero-terminated array of strings. zStr
13927 ** points to a single nul-terminated string. Return non-zero if zStr
13928 ** is equal, according to strcmp(), to any of the strings in the array.
13929 ** Otherwise, return zero.
13930 */
13931 static int str_in_array(const char *zStr, const char **azArray){
13932   int i;
13933   for(i=0; azArray[i]; i++){
13934     if( 0==strcmp(zStr, azArray[i]) ) return 1;
13935   }
13936   return 0;
13937 }
13938
13939 /*
13940 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
13941 ** and populate the ShellState.aiIndent[] array with the number of
13942 ** spaces each opcode should be indented before it is output.
13943 **
13944 ** The indenting rules are:
13945 **
13946 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
13947 **       all opcodes that occur between the p2 jump destination and the opcode
13948 **       itself by 2 spaces.
13949 **
13950 **     * For each "Goto", if the jump destination is earlier in the program
13951 **       and ends on one of:
13952 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
13953 **       or if the P1 parameter is one instead of zero,
13954 **       then indent all opcodes between the earlier instruction
13955 **       and "Goto" by 2 spaces.
13956 */
13957 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
13958   const char *zSql;               /* The text of the SQL statement */
13959   const char *z;                  /* Used to check if this is an EXPLAIN */
13960   int *abYield = 0;               /* True if op is an OP_Yield */
13961   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
13962   int iOp;                        /* Index of operation in p->aiIndent[] */
13963
13964   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
13965   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
13966                             "Rewind", 0 };
13967   const char *azGoto[] = { "Goto", 0 };
13968
13969   /* Try to figure out if this is really an EXPLAIN statement. If this
13970   ** cannot be verified, return early.  */
13971   if( sqlite3_column_count(pSql)!=8 ){
13972     p->cMode = p->mode;
13973     return;
13974   }
13975   zSql = sqlite3_sql(pSql);
13976   if( zSql==0 ) return;
13977   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
13978   if( sqlite3_strnicmp(z, "explain", 7) ){
13979     p->cMode = p->mode;
13980     return;
13981   }
13982
13983   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
13984     int i;
13985     int iAddr = sqlite3_column_int(pSql, 0);
13986     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
13987
13988     /* Set p2 to the P2 field of the current opcode. Then, assuming that
13989     ** p2 is an instruction address, set variable p2op to the index of that
13990     ** instruction in the aiIndent[] array. p2 and p2op may be different if
13991     ** the current instruction is part of a sub-program generated by an
13992     ** SQL trigger or foreign key.  */
13993     int p2 = sqlite3_column_int(pSql, 3);
13994     int p2op = (p2 + (iOp-iAddr));
13995
13996     /* Grow the p->aiIndent array as required */
13997     if( iOp>=nAlloc ){
13998       if( iOp==0 ){
13999         /* Do further verfication that this is explain output.  Abort if
14000         ** it is not */
14001         static const char *explainCols[] = {
14002            "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
14003         int jj;
14004         for(jj=0; jj<ArraySize(explainCols); jj++){
14005           if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
14006             p->cMode = p->mode;
14007             sqlite3_reset(pSql);
14008             return;
14009           }
14010         }
14011       }
14012       nAlloc += 100;
14013       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
14014       if( p->aiIndent==0 ) shell_out_of_memory();
14015       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
14016       if( abYield==0 ) shell_out_of_memory();
14017     }
14018     abYield[iOp] = str_in_array(zOp, azYield);
14019     p->aiIndent[iOp] = 0;
14020     p->nIndent = iOp+1;
14021
14022     if( str_in_array(zOp, azNext) ){
14023       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
14024     }
14025     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
14026      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
14027     ){
14028       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
14029     }
14030   }
14031
14032   p->iIndent = 0;
14033   sqlite3_free(abYield);
14034   sqlite3_reset(pSql);
14035 }
14036
14037 /*
14038 ** Free the array allocated by explain_data_prepare().
14039 */
14040 static void explain_data_delete(ShellState *p){
14041   sqlite3_free(p->aiIndent);
14042   p->aiIndent = 0;
14043   p->nIndent = 0;
14044   p->iIndent = 0;
14045 }
14046
14047 /*
14048 ** Disable and restore .wheretrace and .selecttrace settings.
14049 */
14050 static unsigned int savedSelectTrace;
14051 static unsigned int savedWhereTrace;
14052 static void disable_debug_trace_modes(void){
14053   unsigned int zero = 0;
14054   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
14055   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
14056   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
14057   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
14058 }
14059 static void restore_debug_trace_modes(void){
14060   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
14061   sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
14062 }
14063
14064 /* Create the TEMP table used to store parameter bindings */
14065 static void bind_table_init(ShellState *p){
14066   int wrSchema = 0;
14067   int defensiveMode = 0;
14068   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
14069   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
14070   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
14071   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
14072   sqlite3_exec(p->db,
14073     "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
14074     "  key TEXT PRIMARY KEY,\n"
14075     "  value\n"
14076     ") WITHOUT ROWID;",
14077     0, 0, 0);
14078   sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
14079   sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
14080 }
14081
14082 /*
14083 ** Bind parameters on a prepared statement.
14084 **
14085 ** Parameter bindings are taken from a TEMP table of the form:
14086 **
14087 **    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
14088 **    WITHOUT ROWID;
14089 **
14090 ** No bindings occur if this table does not exist.  The name of the table
14091 ** begins with "sqlite_" so that it will not collide with ordinary application
14092 ** tables.  The table must be in the TEMP schema.
14093 */
14094 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
14095   int nVar;
14096   int i;
14097   int rc;
14098   sqlite3_stmt *pQ = 0;
14099
14100   nVar = sqlite3_bind_parameter_count(pStmt);
14101   if( nVar==0 ) return;  /* Nothing to do */
14102   if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
14103                                     "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
14104     return; /* Parameter table does not exist */
14105   }
14106   rc = sqlite3_prepare_v2(pArg->db,
14107           "SELECT value FROM temp.sqlite_parameters"
14108           " WHERE key=?1", -1, &pQ, 0);
14109   if( rc || pQ==0 ) return;
14110   for(i=1; i<=nVar; i++){
14111     char zNum[30];
14112     const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
14113     if( zVar==0 ){
14114       sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
14115       zVar = zNum;
14116     }
14117     sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
14118     if( sqlite3_step(pQ)==SQLITE_ROW ){
14119       sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
14120     }else{
14121       sqlite3_bind_null(pStmt, i);
14122     }
14123     sqlite3_reset(pQ);
14124   }
14125   sqlite3_finalize(pQ);
14126 }
14127
14128 /*
14129 ** UTF8 box-drawing characters.  Imagine box lines like this:
14130 **
14131 **           1
14132 **           |
14133 **       4 --+-- 2
14134 **           |
14135 **           3
14136 **
14137 ** Each box characters has between 2 and 4 of the lines leading from
14138 ** the center.  The characters are here identified by the numbers of
14139 ** their corresponding lines.
14140 */
14141 #define BOX_24   "\342\224\200"  /* U+2500 --- */
14142 #define BOX_13   "\342\224\202"  /* U+2502  |  */
14143 #define BOX_23   "\342\224\214"  /* U+250c  ,- */
14144 #define BOX_34   "\342\224\220"  /* U+2510 -,  */
14145 #define BOX_12   "\342\224\224"  /* U+2514  '- */
14146 #define BOX_14   "\342\224\230"  /* U+2518 -'  */
14147 #define BOX_123  "\342\224\234"  /* U+251c  |- */
14148 #define BOX_134  "\342\224\244"  /* U+2524 -|  */
14149 #define BOX_234  "\342\224\254"  /* U+252c -,- */
14150 #define BOX_124  "\342\224\264"  /* U+2534 -'- */
14151 #define BOX_1234 "\342\224\274"  /* U+253c -|- */
14152
14153 /* Draw horizontal line N characters long using unicode box
14154 ** characters
14155 */
14156 static void print_box_line(FILE *out, int N){
14157   const char zDash[] = 
14158       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
14159       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
14160   const int nDash = sizeof(zDash) - 1;
14161   N *= 3;
14162   while( N>nDash ){
14163     utf8_printf(out, zDash);
14164     N -= nDash;
14165   }
14166   utf8_printf(out, "%.*s", N, zDash);
14167 }
14168
14169 /*
14170 ** Draw a horizontal separator for a MODE_Box table.
14171 */
14172 static void print_box_row_separator(
14173   ShellState *p,
14174   int nArg,
14175   const char *zSep1,
14176   const char *zSep2,
14177   const char *zSep3
14178 ){
14179   int i;
14180   if( nArg>0 ){
14181     utf8_printf(p->out, "%s", zSep1);
14182     print_box_line(p->out, p->actualWidth[0]+2);
14183     for(i=1; i<nArg; i++){
14184       utf8_printf(p->out, "%s", zSep2);
14185       print_box_line(p->out, p->actualWidth[i]+2);
14186     }
14187     utf8_printf(p->out, "%s", zSep3);
14188   }
14189   fputs("\n", p->out);
14190 }
14191
14192
14193
14194 /*
14195 ** Run a prepared statement and output the result in one of the
14196 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
14197 ** or MODE_Box.
14198 **
14199 ** This is different from ordinary exec_prepared_stmt() in that
14200 ** it has to run the entire query and gather the results into memory
14201 ** first, in order to determine column widths, before providing
14202 ** any output.
14203 */
14204 static void exec_prepared_stmt_columnar(
14205   ShellState *p,                        /* Pointer to ShellState */
14206   sqlite3_stmt *pStmt                   /* Statment to run */
14207 ){
14208   sqlite3_int64 nRow = 0;
14209   int nColumn = 0;
14210   char **azData = 0;
14211   sqlite3_int64 nAlloc = 0;
14212   const char *z;
14213   int rc;
14214   sqlite3_int64 i, nData;
14215   int j, nTotal, w, n;
14216   const char *colSep = 0;
14217   const char *rowSep = 0;
14218
14219   rc = sqlite3_step(pStmt);
14220   if( rc!=SQLITE_ROW ) return;
14221   nColumn = sqlite3_column_count(pStmt);
14222   nAlloc = nColumn*4;
14223   if( nAlloc<=0 ) nAlloc = 1;
14224   azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14225   if( azData==0 ) shell_out_of_memory();
14226   for(i=0; i<nColumn; i++){
14227     azData[i] = strdup(sqlite3_column_name(pStmt,i));
14228   }
14229   do{
14230     if( (nRow+2)*nColumn >= nAlloc ){
14231       nAlloc *= 2;
14232       azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14233       if( azData==0 ) shell_out_of_memory();
14234     }
14235     nRow++;
14236     for(i=0; i<nColumn; i++){
14237       z = (const char*)sqlite3_column_text(pStmt,i);
14238       azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14239     }
14240   }while( sqlite3_step(pStmt)==SQLITE_ROW );
14241   if( nColumn>p->nWidth ){
14242     p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14243     if( p->colWidth==0 ) shell_out_of_memory();
14244     for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14245     p->nWidth = nColumn;
14246     p->actualWidth = &p->colWidth[nColumn];
14247   }
14248   memset(p->actualWidth, 0, nColumn*sizeof(int));
14249   for(i=0; i<nColumn; i++){
14250     w = p->colWidth[i];
14251     if( w<0 ) w = -w;
14252     p->actualWidth[i] = w;
14253   }
14254   nTotal = nColumn*(nRow+1);
14255   for(i=0; i<nTotal; i++){
14256     z = azData[i];
14257     if( z==0 ) z = p->nullValue;
14258     n = strlenChar(z);
14259     j = i%nColumn;
14260     if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
14261   }
14262   if( seenInterrupt ) goto columnar_end;
14263   if( nColumn==0 ) goto columnar_end;
14264   switch( p->cMode ){
14265     case MODE_Column: {
14266       colSep = "  ";
14267       rowSep = "\n";
14268       if( p->showHeader ){
14269         for(i=0; i<nColumn; i++){
14270           w = p->actualWidth[i];
14271           if( p->colWidth[i]<0 ) w = -w;
14272           utf8_width_print(p->out, w, azData[i]);
14273           fputs(i==nColumn-1?"\n":"  ", p->out);
14274         }
14275         for(i=0; i<nColumn; i++){
14276           print_dashes(p->out, p->actualWidth[i]);
14277           fputs(i==nColumn-1?"\n":"  ", p->out);
14278         }
14279       }
14280       break;
14281     }
14282     case MODE_Table: {
14283       colSep = " | ";
14284       rowSep = " |\n";
14285       print_row_separator(p, nColumn, "+");
14286       fputs("| ", p->out);
14287       for(i=0; i<nColumn; i++){
14288         w = p->actualWidth[i];
14289         n = strlenChar(azData[i]);
14290         utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14291         fputs(i==nColumn-1?" |\n":" | ", p->out);
14292       }
14293       print_row_separator(p, nColumn, "+");
14294       break;
14295     }
14296     case MODE_Markdown: {
14297       colSep = " | ";
14298       rowSep = " |\n";
14299       fputs("| ", p->out);
14300       for(i=0; i<nColumn; i++){
14301         w = p->actualWidth[i];
14302         n = strlenChar(azData[i]);
14303         utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14304         fputs(i==nColumn-1?" |\n":" | ", p->out);
14305       }
14306       print_row_separator(p, nColumn, "|");
14307       break;
14308     }
14309     case MODE_Box: {
14310       colSep = " " BOX_13 " ";
14311       rowSep = " " BOX_13 "\n";
14312       print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
14313       utf8_printf(p->out, BOX_13 " ");
14314       for(i=0; i<nColumn; i++){
14315         w = p->actualWidth[i];
14316         n = strlenChar(azData[i]);
14317         utf8_printf(p->out, "%*s%s%*s%s",
14318             (w-n)/2, "", azData[i], (w-n+1)/2, "",
14319             i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
14320       }
14321       print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14322       break;
14323     }
14324   }
14325   for(i=nColumn, j=0; i<nTotal; i++, j++){
14326     if( j==0 && p->cMode!=MODE_Column ){
14327       utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
14328     }
14329     z = azData[i];
14330     if( z==0 ) z = p->nullValue;
14331     w = p->actualWidth[j];
14332     if( p->colWidth[j]<0 ) w = -w;
14333     utf8_width_print(p->out, w, z);
14334     if( j==nColumn-1 ){
14335       utf8_printf(p->out, "%s", rowSep);
14336       j = -1;
14337       if( seenInterrupt ) goto columnar_end;
14338     }else{
14339       utf8_printf(p->out, "%s", colSep);
14340     }
14341   }
14342   if( p->cMode==MODE_Table ){
14343     print_row_separator(p, nColumn, "+");
14344   }else if( p->cMode==MODE_Box ){
14345     print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
14346   }
14347 columnar_end:
14348   if( seenInterrupt ){
14349     utf8_printf(p->out, "Interrupt\n");
14350   }
14351   nData = (nRow+1)*nColumn;
14352   for(i=0; i<nData; i++) free(azData[i]);
14353   sqlite3_free(azData);
14354 }
14355
14356 /*
14357 ** Run a prepared statement
14358 */
14359 static void exec_prepared_stmt(
14360   ShellState *pArg,                                /* Pointer to ShellState */
14361   sqlite3_stmt *pStmt                              /* Statment to run */
14362 ){
14363   int rc;
14364
14365   if( pArg->cMode==MODE_Column
14366    || pArg->cMode==MODE_Table
14367    || pArg->cMode==MODE_Box
14368    || pArg->cMode==MODE_Markdown
14369   ){
14370     exec_prepared_stmt_columnar(pArg, pStmt);
14371     return;
14372   }
14373
14374   /* perform the first step.  this will tell us if we
14375   ** have a result set or not and how wide it is.
14376   */
14377   rc = sqlite3_step(pStmt);
14378   /* if we have a result set... */
14379   if( SQLITE_ROW == rc ){
14380     /* allocate space for col name ptr, value ptr, and type */
14381     int nCol = sqlite3_column_count(pStmt);
14382     void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
14383     if( !pData ){
14384       shell_out_of_memory();
14385     }else{
14386       char **azCols = (char **)pData;      /* Names of result columns */
14387       char **azVals = &azCols[nCol];       /* Results */
14388       int *aiTypes = (int *)&azVals[nCol]; /* Result types */
14389       int i, x;
14390       assert(sizeof(int) <= sizeof(char *));
14391       /* save off ptrs to column names */
14392       for(i=0; i<nCol; i++){
14393         azCols[i] = (char *)sqlite3_column_name(pStmt, i);
14394       }
14395       do{
14396         /* extract the data and data types */
14397         for(i=0; i<nCol; i++){
14398           aiTypes[i] = x = sqlite3_column_type(pStmt, i);
14399           if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
14400             azVals[i] = "";
14401           }else{
14402             azVals[i] = (char*)sqlite3_column_text(pStmt, i);
14403           }
14404           if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
14405             rc = SQLITE_NOMEM;
14406             break; /* from for */
14407           }
14408         } /* end for */
14409
14410         /* if data and types extracted successfully... */
14411         if( SQLITE_ROW == rc ){
14412           /* call the supplied callback with the result row data */
14413           if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
14414             rc = SQLITE_ABORT;
14415           }else{
14416             rc = sqlite3_step(pStmt);
14417           }
14418         }
14419       } while( SQLITE_ROW == rc );
14420       sqlite3_free(pData);
14421       if( pArg->cMode==MODE_Json ){
14422         fputs("]\n", pArg->out);
14423       }
14424     }
14425   }
14426 }
14427
14428 #ifndef SQLITE_OMIT_VIRTUALTABLE
14429 /*
14430 ** This function is called to process SQL if the previous shell command
14431 ** was ".expert". It passes the SQL in the second argument directly to
14432 ** the sqlite3expert object.
14433 **
14434 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14435 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14436 ** an English language error message. It is the responsibility of the
14437 ** caller to eventually free this buffer using sqlite3_free().
14438 */
14439 static int expertHandleSQL(
14440   ShellState *pState, 
14441   const char *zSql, 
14442   char **pzErr
14443 ){
14444   assert( pState->expert.pExpert );
14445   assert( pzErr==0 || *pzErr==0 );
14446   return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
14447 }
14448
14449 /*
14450 ** This function is called either to silently clean up the object
14451 ** created by the ".expert" command (if bCancel==1), or to generate a 
14452 ** report from it and then clean it up (if bCancel==0).
14453 **
14454 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14455 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14456 ** an English language error message. It is the responsibility of the
14457 ** caller to eventually free this buffer using sqlite3_free().
14458 */
14459 static int expertFinish(
14460   ShellState *pState,
14461   int bCancel,
14462   char **pzErr
14463 ){
14464   int rc = SQLITE_OK;
14465   sqlite3expert *p = pState->expert.pExpert;
14466   assert( p );
14467   assert( bCancel || pzErr==0 || *pzErr==0 );
14468   if( bCancel==0 ){
14469     FILE *out = pState->out;
14470     int bVerbose = pState->expert.bVerbose;
14471
14472     rc = sqlite3_expert_analyze(p, pzErr);
14473     if( rc==SQLITE_OK ){
14474       int nQuery = sqlite3_expert_count(p);
14475       int i;
14476
14477       if( bVerbose ){
14478         const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
14479         raw_printf(out, "-- Candidates -----------------------------\n");
14480         raw_printf(out, "%s\n", zCand);
14481       }
14482       for(i=0; i<nQuery; i++){
14483         const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
14484         const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
14485         const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
14486         if( zIdx==0 ) zIdx = "(no new indexes)\n";
14487         if( bVerbose ){
14488           raw_printf(out, "-- Query %d --------------------------------\n",i+1);
14489           raw_printf(out, "%s\n\n", zSql);
14490         }
14491         raw_printf(out, "%s\n", zIdx);
14492         raw_printf(out, "%s\n", zEQP);
14493       }
14494     }
14495   }
14496   sqlite3_expert_destroy(p);
14497   pState->expert.pExpert = 0;
14498   return rc;
14499 }
14500
14501 /*
14502 ** Implementation of ".expert" dot command.
14503 */
14504 static int expertDotCommand(
14505   ShellState *pState,             /* Current shell tool state */
14506   char **azArg,                   /* Array of arguments passed to dot command */
14507   int nArg                        /* Number of entries in azArg[] */
14508 ){
14509   int rc = SQLITE_OK;
14510   char *zErr = 0;
14511   int i;
14512   int iSample = 0;
14513
14514   assert( pState->expert.pExpert==0 );
14515   memset(&pState->expert, 0, sizeof(ExpertInfo));
14516
14517   for(i=1; rc==SQLITE_OK && i<nArg; i++){
14518     char *z = azArg[i];
14519     int n;
14520     if( z[0]=='-' && z[1]=='-' ) z++;
14521     n = strlen30(z);
14522     if( n>=2 && 0==strncmp(z, "-verbose", n) ){
14523       pState->expert.bVerbose = 1;
14524     }
14525     else if( n>=2 && 0==strncmp(z, "-sample", n) ){
14526       if( i==(nArg-1) ){
14527         raw_printf(stderr, "option requires an argument: %s\n", z);
14528         rc = SQLITE_ERROR;
14529       }else{
14530         iSample = (int)integerValue(azArg[++i]);
14531         if( iSample<0 || iSample>100 ){
14532           raw_printf(stderr, "value out of range: %s\n", azArg[i]);
14533           rc = SQLITE_ERROR;
14534         }
14535       }
14536     }
14537     else{
14538       raw_printf(stderr, "unknown option: %s\n", z);
14539       rc = SQLITE_ERROR;
14540     }
14541   }
14542
14543   if( rc==SQLITE_OK ){
14544     pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
14545     if( pState->expert.pExpert==0 ){
14546       raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
14547       rc = SQLITE_ERROR;
14548     }else{
14549       sqlite3_expert_config(
14550           pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
14551       );
14552     }
14553   }
14554
14555   return rc;
14556 }
14557 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14558
14559 /*
14560 ** Execute a statement or set of statements.  Print
14561 ** any result rows/columns depending on the current mode
14562 ** set via the supplied callback.
14563 **
14564 ** This is very similar to SQLite's built-in sqlite3_exec()
14565 ** function except it takes a slightly different callback
14566 ** and callback data argument.
14567 */
14568 static int shell_exec(
14569   ShellState *pArg,                         /* Pointer to ShellState */
14570   const char *zSql,                         /* SQL to be evaluated */
14571   char **pzErrMsg                           /* Error msg written here */
14572 ){
14573   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
14574   int rc = SQLITE_OK;             /* Return Code */
14575   int rc2;
14576   const char *zLeftover;          /* Tail of unprocessed SQL */
14577   sqlite3 *db = pArg->db;
14578
14579   if( pzErrMsg ){
14580     *pzErrMsg = NULL;
14581   }
14582
14583 #ifndef SQLITE_OMIT_VIRTUALTABLE
14584   if( pArg->expert.pExpert ){
14585     rc = expertHandleSQL(pArg, zSql, pzErrMsg);
14586     return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
14587   }
14588 #endif
14589
14590   while( zSql[0] && (SQLITE_OK == rc) ){
14591     static const char *zStmtSql;
14592     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
14593     if( SQLITE_OK != rc ){
14594       if( pzErrMsg ){
14595         *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)", rc);
14596       }
14597     }else{
14598       if( !pStmt ){
14599         /* this happens for a comment or white-space */
14600         zSql = zLeftover;
14601         while( IsSpace(zSql[0]) ) zSql++;
14602         continue;
14603       }
14604       zStmtSql = sqlite3_sql(pStmt);
14605       if( zStmtSql==0 ) zStmtSql = "";
14606       while( IsSpace(zStmtSql[0]) ) zStmtSql++;
14607
14608       /* save off the prepared statment handle and reset row count */
14609       if( pArg ){
14610         pArg->pStmt = pStmt;
14611         pArg->cnt = 0;
14612       }
14613
14614       /* echo the sql statement if echo on */
14615       if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
14616         utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
14617       }
14618
14619       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
14620       if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
14621         sqlite3_stmt *pExplain;
14622         char *zEQP;
14623         int triggerEQP = 0;
14624         disable_debug_trace_modes();
14625         sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
14626         if( pArg->autoEQP>=AUTOEQP_trigger ){
14627           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
14628         }
14629         zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
14630         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14631         if( rc==SQLITE_OK ){
14632           while( sqlite3_step(pExplain)==SQLITE_ROW ){
14633             const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
14634             int iEqpId = sqlite3_column_int(pExplain, 0);
14635             int iParentId = sqlite3_column_int(pExplain, 1);
14636             if( zEQPLine==0 ) zEQPLine = "";
14637             if( zEQPLine[0]=='-' ) eqp_render(pArg);
14638             eqp_append(pArg, iEqpId, iParentId, zEQPLine);
14639           }
14640           eqp_render(pArg);
14641         }
14642         sqlite3_finalize(pExplain);
14643         sqlite3_free(zEQP);
14644         if( pArg->autoEQP>=AUTOEQP_full ){
14645           /* Also do an EXPLAIN for ".eqp full" mode */
14646           zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
14647           rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14648           if( rc==SQLITE_OK ){
14649             pArg->cMode = MODE_Explain;
14650             explain_data_prepare(pArg, pExplain);
14651             exec_prepared_stmt(pArg, pExplain);
14652             explain_data_delete(pArg);
14653           }
14654           sqlite3_finalize(pExplain);
14655           sqlite3_free(zEQP);
14656         }
14657         if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
14658           sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
14659           /* Reprepare pStmt before reactiving trace modes */
14660           sqlite3_finalize(pStmt);
14661           sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
14662           if( pArg ) pArg->pStmt = pStmt;
14663         }
14664         restore_debug_trace_modes();
14665       }
14666
14667       if( pArg ){
14668         pArg->cMode = pArg->mode;
14669         if( pArg->autoExplain ){
14670           if( sqlite3_stmt_isexplain(pStmt)==1 ){
14671             pArg->cMode = MODE_Explain;
14672           }
14673           if( sqlite3_stmt_isexplain(pStmt)==2 ){
14674             pArg->cMode = MODE_EQP;
14675           }
14676         }
14677
14678         /* If the shell is currently in ".explain" mode, gather the extra
14679         ** data required to add indents to the output.*/
14680         if( pArg->cMode==MODE_Explain ){
14681           explain_data_prepare(pArg, pStmt);
14682         }
14683       }
14684
14685       bind_prepared_stmt(pArg, pStmt);
14686       exec_prepared_stmt(pArg, pStmt);
14687       explain_data_delete(pArg);
14688       eqp_render(pArg);
14689
14690       /* print usage stats if stats on */
14691       if( pArg && pArg->statsOn ){
14692         display_stats(db, pArg, 0);
14693       }
14694
14695       /* print loop-counters if required */
14696       if( pArg && pArg->scanstatsOn ){
14697         display_scanstats(db, pArg);
14698       }
14699
14700       /* Finalize the statement just executed. If this fails, save a
14701       ** copy of the error message. Otherwise, set zSql to point to the
14702       ** next statement to execute. */
14703       rc2 = sqlite3_finalize(pStmt);
14704       if( rc!=SQLITE_NOMEM ) rc = rc2;
14705       if( rc==SQLITE_OK ){
14706         zSql = zLeftover;
14707         while( IsSpace(zSql[0]) ) zSql++;
14708       }else if( pzErrMsg ){
14709         *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc);
14710       }
14711
14712       /* clear saved stmt handle */
14713       if( pArg ){
14714         pArg->pStmt = NULL;
14715       }
14716     }
14717   } /* end while */
14718
14719   return rc;
14720 }
14721
14722 /*
14723 ** Release memory previously allocated by tableColumnList().
14724 */
14725 static void freeColumnList(char **azCol){
14726   int i;
14727   for(i=1; azCol[i]; i++){
14728     sqlite3_free(azCol[i]);
14729   }
14730   /* azCol[0] is a static string */
14731   sqlite3_free(azCol);
14732 }
14733
14734 /*
14735 ** Return a list of pointers to strings which are the names of all
14736 ** columns in table zTab.   The memory to hold the names is dynamically
14737 ** allocated and must be released by the caller using a subsequent call
14738 ** to freeColumnList().
14739 **
14740 ** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
14741 ** value that needs to be preserved, then azCol[0] is filled in with the
14742 ** name of the rowid column.
14743 **
14744 ** The first regular column in the table is azCol[1].  The list is terminated
14745 ** by an entry with azCol[i]==0.
14746 */
14747 static char **tableColumnList(ShellState *p, const char *zTab){
14748   char **azCol = 0;
14749   sqlite3_stmt *pStmt;
14750   char *zSql;
14751   int nCol = 0;
14752   int nAlloc = 0;
14753   int nPK = 0;       /* Number of PRIMARY KEY columns seen */
14754   int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
14755   int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
14756   int rc;
14757
14758   zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
14759   rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14760   sqlite3_free(zSql);
14761   if( rc ) return 0;
14762   while( sqlite3_step(pStmt)==SQLITE_ROW ){
14763     if( nCol>=nAlloc-2 ){
14764       nAlloc = nAlloc*2 + nCol + 10;
14765       azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
14766       if( azCol==0 ) shell_out_of_memory();
14767     }
14768     azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
14769     if( sqlite3_column_int(pStmt, 5) ){
14770       nPK++;
14771       if( nPK==1
14772        && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
14773                           "INTEGER")==0
14774       ){
14775         isIPK = 1;
14776       }else{
14777         isIPK = 0;
14778       }
14779     }
14780   }
14781   sqlite3_finalize(pStmt);
14782   if( azCol==0 ) return 0;
14783   azCol[0] = 0;
14784   azCol[nCol+1] = 0;
14785
14786   /* The decision of whether or not a rowid really needs to be preserved
14787   ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
14788   ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
14789   ** rowids on tables where the rowid is inaccessible because there are other
14790   ** columns in the table named "rowid", "_rowid_", and "oid".
14791   */
14792   if( preserveRowid && isIPK ){
14793     /* If a single PRIMARY KEY column with type INTEGER was seen, then it
14794     ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
14795     ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
14796     ** ROWID aliases.  To distinguish these cases, check to see if
14797     ** there is a "pk" entry in "PRAGMA index_list".  There will be
14798     ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
14799     */
14800     zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
14801                            " WHERE origin='pk'", zTab);
14802     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14803     sqlite3_free(zSql);
14804     if( rc ){
14805       freeColumnList(azCol);
14806       return 0;
14807     }
14808     rc = sqlite3_step(pStmt);
14809     sqlite3_finalize(pStmt);
14810     preserveRowid = rc==SQLITE_ROW;
14811   }
14812   if( preserveRowid ){
14813     /* Only preserve the rowid if we can find a name to use for the
14814     ** rowid */
14815     static char *azRowid[] = { "rowid", "_rowid_", "oid" };
14816     int i, j;
14817     for(j=0; j<3; j++){
14818       for(i=1; i<=nCol; i++){
14819         if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
14820       }
14821       if( i>nCol ){
14822         /* At this point, we know that azRowid[j] is not the name of any
14823         ** ordinary column in the table.  Verify that azRowid[j] is a valid
14824         ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
14825         ** tables will fail this last check */
14826         rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
14827         if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
14828         break;
14829       }
14830     }
14831   }
14832   return azCol;
14833 }
14834
14835 /*
14836 ** Toggle the reverse_unordered_selects setting.
14837 */
14838 static void toggleSelectOrder(sqlite3 *db){
14839   sqlite3_stmt *pStmt = 0;
14840   int iSetting = 0;
14841   char zStmt[100];
14842   sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
14843   if( sqlite3_step(pStmt)==SQLITE_ROW ){
14844     iSetting = sqlite3_column_int(pStmt, 0);
14845   }
14846   sqlite3_finalize(pStmt);
14847   sqlite3_snprintf(sizeof(zStmt), zStmt,
14848        "PRAGMA reverse_unordered_selects(%d)", !iSetting);
14849   sqlite3_exec(db, zStmt, 0, 0, 0);
14850 }
14851
14852 /*
14853 ** This is a different callback routine used for dumping the database.
14854 ** Each row received by this callback consists of a table name,
14855 ** the table type ("index" or "table") and SQL to create the table.
14856 ** This routine should print text sufficient to recreate the table.
14857 */
14858 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
14859   int rc;
14860   const char *zTable;
14861   const char *zType;
14862   const char *zSql;
14863   ShellState *p = (ShellState *)pArg;
14864   int dataOnly;
14865   int noSys;
14866
14867   UNUSED_PARAMETER(azNotUsed);
14868   if( nArg!=3 || azArg==0 ) return 0;
14869   zTable = azArg[0];
14870   zType = azArg[1];
14871   zSql = azArg[2];
14872   dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
14873   noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
14874
14875   if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
14876     if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
14877   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
14878     if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
14879   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
14880     return 0;
14881   }else if( dataOnly ){
14882     /* no-op */
14883   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
14884     char *zIns;
14885     if( !p->writableSchema ){
14886       raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
14887       p->writableSchema = 1;
14888     }
14889     zIns = sqlite3_mprintf(
14890        "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
14891        "VALUES('table','%q','%q',0,'%q');",
14892        zTable, zTable, zSql);
14893     utf8_printf(p->out, "%s\n", zIns);
14894     sqlite3_free(zIns);
14895     return 0;
14896   }else{
14897     printSchemaLine(p->out, zSql, ";\n");
14898   }
14899
14900   if( strcmp(zType, "table")==0 ){
14901     ShellText sSelect;
14902     ShellText sTable;
14903     char **azCol;
14904     int i;
14905     char *savedDestTable;
14906     int savedMode;
14907
14908     azCol = tableColumnList(p, zTable);
14909     if( azCol==0 ){
14910       p->nErr++;
14911       return 0;
14912     }
14913
14914     /* Always quote the table name, even if it appears to be pure ascii,
14915     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
14916     initText(&sTable);
14917     appendText(&sTable, zTable, quoteChar(zTable));
14918     /* If preserving the rowid, add a column list after the table name.
14919     ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
14920     ** instead of the usual "INSERT INTO tab VALUES(...)".
14921     */
14922     if( azCol[0] ){
14923       appendText(&sTable, "(", 0);
14924       appendText(&sTable, azCol[0], 0);
14925       for(i=1; azCol[i]; i++){
14926         appendText(&sTable, ",", 0);
14927         appendText(&sTable, azCol[i], quoteChar(azCol[i]));
14928       }
14929       appendText(&sTable, ")", 0);
14930     }
14931
14932     /* Build an appropriate SELECT statement */
14933     initText(&sSelect);
14934     appendText(&sSelect, "SELECT ", 0);
14935     if( azCol[0] ){
14936       appendText(&sSelect, azCol[0], 0);
14937       appendText(&sSelect, ",", 0);
14938     }
14939     for(i=1; azCol[i]; i++){
14940       appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
14941       if( azCol[i+1] ){
14942         appendText(&sSelect, ",", 0);
14943       }
14944     }
14945     freeColumnList(azCol);
14946     appendText(&sSelect, " FROM ", 0);
14947     appendText(&sSelect, zTable, quoteChar(zTable));
14948
14949     savedDestTable = p->zDestTable;
14950     savedMode = p->mode;
14951     p->zDestTable = sTable.z;
14952     p->mode = p->cMode = MODE_Insert;
14953     rc = shell_exec(p, sSelect.z, 0);
14954     if( (rc&0xff)==SQLITE_CORRUPT ){
14955       raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14956       toggleSelectOrder(p->db);
14957       shell_exec(p, sSelect.z, 0);
14958       toggleSelectOrder(p->db);
14959     }
14960     p->zDestTable = savedDestTable;
14961     p->mode = savedMode;
14962     freeText(&sTable);
14963     freeText(&sSelect);
14964     if( rc ) p->nErr++;
14965   }
14966   return 0;
14967 }
14968
14969 /*
14970 ** Run zQuery.  Use dump_callback() as the callback routine so that
14971 ** the contents of the query are output as SQL statements.
14972 **
14973 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
14974 ** "ORDER BY rowid DESC" to the end.
14975 */
14976 static int run_schema_dump_query(
14977   ShellState *p,
14978   const char *zQuery
14979 ){
14980   int rc;
14981   char *zErr = 0;
14982   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
14983   if( rc==SQLITE_CORRUPT ){
14984     char *zQ2;
14985     int len = strlen30(zQuery);
14986     raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14987     if( zErr ){
14988       utf8_printf(p->out, "/****** %s ******/\n", zErr);
14989       sqlite3_free(zErr);
14990       zErr = 0;
14991     }
14992     zQ2 = malloc( len+100 );
14993     if( zQ2==0 ) return rc;
14994     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
14995     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
14996     if( rc ){
14997       utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
14998     }else{
14999       rc = SQLITE_CORRUPT;
15000     }
15001     sqlite3_free(zErr);
15002     free(zQ2);
15003   }
15004   return rc;
15005 }
15006
15007 /*
15008 ** Text of help messages.
15009 **
15010 ** The help text for each individual command begins with a line that starts
15011 ** with ".".  Subsequent lines are supplimental information.
15012 **
15013 ** There must be two or more spaces between the end of the command and the
15014 ** start of the description of what that command does.
15015 */
15016 static const char *(azHelp[]) = {
15017 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15018   ".archive ...             Manage SQL archives",
15019   "   Each command must have exactly one of the following options:",
15020   "     -c, --create               Create a new archive",
15021   "     -u, --update               Add or update files with changed mtime",
15022   "     -i, --insert               Like -u but always add even if unchanged",
15023   "     -r, --remove               Remove files from archive",
15024   "     -t, --list                 List contents of archive",
15025   "     -x, --extract              Extract files from archive",
15026   "   Optional arguments:",
15027   "     -v, --verbose              Print each filename as it is processed",
15028   "     -f FILE, --file FILE       Use archive FILE (default is current db)",
15029   "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
15030   "     -C DIR, --directory DIR    Read/extract files from directory DIR",
15031   "     -g, --glob                 Use glob matching for names in archive",
15032   "     -n, --dryrun               Show the SQL that would have occurred",
15033   "   Examples:",
15034   "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
15035   "     .ar -tf ARCHIVE          # List members of ARCHIVE",
15036   "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
15037   "   See also:",
15038   "      http://sqlite.org/cli.html#sqlite_archive_support",
15039 #endif
15040 #ifndef SQLITE_OMIT_AUTHORIZATION
15041   ".auth ON|OFF             Show authorizer callbacks",
15042 #endif
15043   ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
15044   "       --append            Use the appendvfs",
15045   "       --async             Write to FILE without journal and fsync()",
15046   ".bail on|off             Stop after hitting an error.  Default OFF",
15047   ".binary on|off           Turn binary output on or off.  Default OFF",
15048   ".cd DIRECTORY            Change the working directory to DIRECTORY",
15049   ".changes on|off          Show number of rows changed by SQL",
15050   ".check GLOB              Fail if output since .testcase does not match",
15051   ".clone NEWDB             Clone data into NEWDB from the existing database",
15052   ".connection [close] [#]  Open or close an auxiliary database connection",
15053   ".databases               List names and files of attached databases",
15054   ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
15055   ".dbinfo ?DB?             Show status information about the database",
15056   ".dump ?OBJECTS?          Render database content as SQL",
15057   "   Options:",
15058   "     --data-only            Output only INSERT statements",
15059   "     --newlines             Allow unescaped newline characters in output",
15060   "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
15061   "     --preserve-rowids      Include ROWID values in the output",
15062   "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
15063   "   Additional LIKE patterns can be given in subsequent arguments",
15064   ".echo on|off             Turn command echo on or off",
15065   ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
15066   "   Other Modes:",
15067 #ifdef SQLITE_DEBUG
15068   "      test                  Show raw EXPLAIN QUERY PLAN output",
15069   "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
15070 #endif
15071   "      trigger               Like \"full\" but also show trigger bytecode",
15072   ".excel                   Display the output of next command in spreadsheet",
15073   "   --bom                   Put a UTF8 byte-order mark on intermediate file",
15074   ".exit ?CODE?             Exit this program with return-code CODE",
15075   ".expert                  EXPERIMENTAL. Suggest indexes for queries",
15076   ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
15077   ".filectrl CMD ...        Run various sqlite3_file_control() operations",
15078   "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
15079   "   --help                  Show CMD details",
15080   ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
15081   ".headers on|off          Turn display of headers on or off",
15082   ".help ?-all? ?PATTERN?   Show help text for PATTERN",
15083   ".import FILE TABLE       Import data from FILE into TABLE",
15084   "   Options:",
15085   "     --ascii               Use \\037 and \\036 as column and row separators",
15086   "     --csv                 Use , and \\n as column and row separators",
15087   "     --skip N              Skip the first N rows of input",
15088   "     -v                    \"Verbose\" - increase auxiliary output",
15089   "   Notes:",
15090   "     *  If TABLE does not exist, it is created.  The first row of input",
15091   "        determines the column names.",
15092   "     *  If neither --csv or --ascii are used, the input mode is derived",
15093   "        from the \".mode\" output mode",
15094   "     *  If FILE begins with \"|\" then it is a command that generates the",
15095   "        input text.",
15096 #ifndef SQLITE_OMIT_TEST_CONTROL
15097   ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
15098 #endif
15099   ".indexes ?TABLE?         Show names of indexes",
15100   "                           If TABLE is specified, only show indexes for",
15101   "                           tables matching TABLE using the LIKE operator.",
15102 #ifdef SQLITE_ENABLE_IOTRACE
15103   ".iotrace FILE            Enable I/O diagnostic logging to FILE",
15104 #endif
15105   ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
15106   ".lint OPTIONS            Report potential schema issues.",
15107   "     Options:",
15108   "        fkey-indexes     Find missing foreign key indexes",
15109 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15110   ".load FILE ?ENTRY?       Load an extension library",
15111 #endif
15112   ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
15113   ".mode MODE ?TABLE?       Set output mode",
15114   "   MODE is one of:",
15115   "     ascii     Columns/rows delimited by 0x1F and 0x1E",
15116   "     box       Tables using unicode box-drawing characters",
15117   "     csv       Comma-separated values",
15118   "     column    Output in columns.  (See .width)",
15119   "     html      HTML <table> code",
15120   "     insert    SQL insert statements for TABLE",
15121   "     json      Results in a JSON array",
15122   "     line      One value per line",
15123   "     list      Values delimited by \"|\"",
15124   "     markdown  Markdown table format",
15125   "     quote     Escape answers as for SQL",
15126   "     table     ASCII-art table",
15127   "     tabs      Tab-separated values",
15128   "     tcl       TCL list elements",
15129   ".nonce STRING            Disable safe mode for one command if the nonce matches",
15130   ".nullvalue STRING        Use STRING in place of NULL values",
15131   ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
15132   "     If FILE begins with '|' then open as a pipe",
15133   "       --bom  Put a UTF8 byte-order mark at the beginning",
15134   "       -e     Send output to the system text editor",
15135   "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
15136 #ifdef SQLITE_DEBUG
15137   ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
15138 #endif 
15139   ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
15140   "     Options:",
15141   "        --append        Use appendvfs to append database to the end of FILE",
15142 #ifndef SQLITE_OMIT_DESERIALIZE
15143   "        --deserialize   Load into memory using sqlite3_deserialize()",
15144   "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
15145   "        --maxsize N     Maximum size for --hexdb or --deserialized database",
15146 #endif
15147   "        --new           Initialize FILE to an empty database",
15148   "        --nofollow      Do not follow symbolic links",
15149   "        --readonly      Open FILE readonly",
15150   "        --zip           FILE is a ZIP archive",
15151   ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
15152   "   If FILE begins with '|' then open it as a pipe.",
15153   "   Options:",
15154   "     --bom                 Prefix output with a UTF8 byte-order mark",
15155   "     -e                    Send output to the system text editor",
15156   "     -x                    Send output as CSV to a spreadsheet",
15157   ".parameter CMD ...       Manage SQL parameter bindings",
15158   "   clear                   Erase all bindings",
15159   "   init                    Initialize the TEMP table that holds bindings",
15160   "   list                    List the current parameter bindings",
15161   "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
15162   "                           PARAMETER should start with one of: $ : @ ?",
15163   "   unset PARAMETER         Remove PARAMETER from the binding table",
15164   ".print STRING...         Print literal STRING",
15165 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
15166   ".progress N              Invoke progress handler after every N opcodes",
15167   "   --limit N                 Interrupt after N progress callbacks",
15168   "   --once                    Do no more than one progress interrupt",
15169   "   --quiet|-q                No output except at interrupts",
15170   "   --reset                   Reset the count for each input and interrupt",
15171 #endif
15172   ".prompt MAIN CONTINUE    Replace the standard prompts",
15173   ".quit                    Exit this program",
15174   ".read FILE               Read input from FILE",
15175 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15176   ".recover                 Recover as much data as possible from corrupt db.",
15177   "   --freelist-corrupt       Assume the freelist is corrupt",
15178   "   --recovery-db NAME       Store recovery metadata in database file NAME",
15179   "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
15180   "   --no-rowids              Do not attempt to recover rowid values",
15181   "                            that are not also INTEGER PRIMARY KEYs",
15182 #endif
15183   ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
15184   ".save FILE               Write in-memory database into FILE",
15185   ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
15186   ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
15187   "   Options:",
15188   "      --indent             Try to pretty-print the schema",
15189   "      --nosys              Omit objects whose names start with \"sqlite_\"",
15190   ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
15191   "    Options:",
15192   "       --init               Create a new SELFTEST table",
15193   "       -v                   Verbose output",
15194   ".separator COL ?ROW?     Change the column and row separators",
15195 #if defined(SQLITE_ENABLE_SESSION)
15196   ".session ?NAME? CMD ...  Create or control sessions",
15197   "   Subcommands:",
15198   "     attach TABLE             Attach TABLE",
15199   "     changeset FILE           Write a changeset into FILE",
15200   "     close                    Close one session",
15201   "     enable ?BOOLEAN?         Set or query the enable bit",
15202   "     filter GLOB...           Reject tables matching GLOBs",
15203   "     indirect ?BOOLEAN?       Mark or query the indirect status",
15204   "     isempty                  Query whether the session is empty",
15205   "     list                     List currently open session names",
15206   "     open DB NAME             Open a new session on DB",
15207   "     patchset FILE            Write a patchset into FILE",
15208   "   If ?NAME? is omitted, the first defined session is used.",
15209 #endif
15210   ".sha3sum ...             Compute a SHA3 hash of database content",
15211   "    Options:",
15212   "      --schema              Also hash the sqlite_schema table",
15213   "      --sha3-224            Use the sha3-224 algorithm",
15214   "      --sha3-256            Use the sha3-256 algorithm (default)",
15215   "      --sha3-384            Use the sha3-384 algorithm",
15216   "      --sha3-512            Use the sha3-512 algorithm",
15217   "    Any other argument is a LIKE pattern for tables to hash",
15218 #ifndef SQLITE_NOHAVE_SYSTEM
15219   ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
15220 #endif
15221   ".show                    Show the current values for various settings",
15222   ".stats ?ARG?             Show stats or turn stats on or off",
15223   "   off                      Turn off automatic stat display",
15224   "   on                       Turn on automatic stat display",
15225   "   stmt                     Show statement stats",
15226   "   vmstep                   Show the virtual machine step count only",
15227 #ifndef SQLITE_NOHAVE_SYSTEM
15228   ".system CMD ARGS...      Run CMD ARGS... in a system shell",
15229 #endif
15230   ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
15231   ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
15232   ".testctrl CMD ...        Run various sqlite3_test_control() operations",
15233   "                           Run \".testctrl\" with no arguments for details",
15234   ".timeout MS              Try opening locked tables for MS milliseconds",
15235   ".timer on|off            Turn SQL timer on or off",
15236 #ifndef SQLITE_OMIT_TRACE
15237   ".trace ?OPTIONS?         Output each SQL statement as it is run",
15238   "    FILE                    Send output to FILE",
15239   "    stdout                  Send output to stdout",
15240   "    stderr                  Send output to stderr",
15241   "    off                     Disable tracing",
15242   "    --expanded              Expand query parameters",
15243 #ifdef SQLITE_ENABLE_NORMALIZE
15244   "    --normalized            Normal the SQL statements",
15245 #endif
15246   "    --plain                 Show SQL as it is input",
15247   "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
15248   "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
15249   "    --row                   Trace each row (SQLITE_TRACE_ROW)",
15250   "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
15251 #endif /* SQLITE_OMIT_TRACE */
15252 #ifdef SQLITE_DEBUG
15253   ".unmodule NAME ...       Unregister virtual table modules",
15254   "    --allexcept             Unregister everything except those named",
15255 #endif
15256   ".vfsinfo ?AUX?           Information about the top-level VFS",
15257   ".vfslist                 List all available VFSes",
15258   ".vfsname ?AUX?           Print the name of the VFS stack",
15259   ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
15260   "     Negative values right-justify",
15261 };
15262
15263 /*
15264 ** Output help text.
15265 **
15266 ** zPattern describes the set of commands for which help text is provided.
15267 ** If zPattern is NULL, then show all commands, but only give a one-line
15268 ** description of each.
15269 **
15270 ** Return the number of matches.
15271 */
15272 static int showHelp(FILE *out, const char *zPattern){
15273   int i = 0;
15274   int j = 0;
15275   int n = 0;
15276   char *zPat;
15277   if( zPattern==0
15278    || zPattern[0]=='0'
15279    || strcmp(zPattern,"-a")==0
15280    || strcmp(zPattern,"-all")==0
15281    || strcmp(zPattern,"--all")==0
15282   ){
15283     /* Show all commands, but only one line per command */
15284     if( zPattern==0 ) zPattern = "";
15285     for(i=0; i<ArraySize(azHelp); i++){
15286       if( azHelp[i][0]=='.' || zPattern[0] ){
15287         utf8_printf(out, "%s\n", azHelp[i]);
15288         n++;
15289       }
15290     }
15291   }else{
15292     /* Look for commands that for which zPattern is an exact prefix */
15293     zPat = sqlite3_mprintf(".%s*", zPattern);
15294     for(i=0; i<ArraySize(azHelp); i++){
15295       if( sqlite3_strglob(zPat, azHelp[i])==0 ){
15296         utf8_printf(out, "%s\n", azHelp[i]);
15297         j = i+1;
15298         n++;
15299       }
15300     }
15301     sqlite3_free(zPat);
15302     if( n ){
15303       if( n==1 ){
15304         /* when zPattern is a prefix of exactly one command, then include the
15305         ** details of that command, which should begin at offset j */
15306         while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
15307           utf8_printf(out, "%s\n", azHelp[j]);
15308           j++;
15309         }
15310       }
15311       return n;
15312     }
15313     /* Look for commands that contain zPattern anywhere.  Show the complete
15314     ** text of all commands that match. */
15315     zPat = sqlite3_mprintf("%%%s%%", zPattern);
15316     for(i=0; i<ArraySize(azHelp); i++){
15317       if( azHelp[i][0]=='.' ) j = i;
15318       if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
15319         utf8_printf(out, "%s\n", azHelp[j]);
15320         while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
15321           j++;
15322           utf8_printf(out, "%s\n", azHelp[j]);
15323         }
15324         i = j;
15325         n++;
15326       }
15327     }
15328     sqlite3_free(zPat);
15329   }
15330   return n;
15331 }
15332
15333 /* Forward reference */
15334 static int process_input(ShellState *p);
15335
15336 /*
15337 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
15338 ** and return a pointer to the buffer. The caller is responsible for freeing
15339 ** the memory.
15340 **
15341 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
15342 ** read.
15343 **
15344 ** For convenience, a nul-terminator byte is always appended to the data read
15345 ** from the file before the buffer is returned. This byte is not included in
15346 ** the final value of (*pnByte), if applicable.
15347 **
15348 ** NULL is returned if any error is encountered. The final value of *pnByte
15349 ** is undefined in this case.
15350 */
15351 static char *readFile(const char *zName, int *pnByte){
15352   FILE *in = fopen(zName, "rb");
15353   long nIn;
15354   size_t nRead;
15355   char *pBuf;
15356   if( in==0 ) return 0;
15357   fseek(in, 0, SEEK_END);
15358   nIn = ftell(in);
15359   rewind(in);
15360   pBuf = sqlite3_malloc64( nIn+1 );
15361   if( pBuf==0 ){ fclose(in); return 0; }
15362   nRead = fread(pBuf, nIn, 1, in);
15363   fclose(in);
15364   if( nRead!=1 ){
15365     sqlite3_free(pBuf);
15366     return 0;
15367   }
15368   pBuf[nIn] = 0;
15369   if( pnByte ) *pnByte = nIn;
15370   return pBuf;
15371 }
15372
15373 #if defined(SQLITE_ENABLE_SESSION)
15374 /*
15375 ** Close a single OpenSession object and release all of its associated
15376 ** resources.
15377 */
15378 static void session_close(OpenSession *pSession){
15379   int i;
15380   sqlite3session_delete(pSession->p);
15381   sqlite3_free(pSession->zName);
15382   for(i=0; i<pSession->nFilter; i++){
15383     sqlite3_free(pSession->azFilter[i]);
15384   }
15385   sqlite3_free(pSession->azFilter);
15386   memset(pSession, 0, sizeof(OpenSession));
15387 }
15388 #endif
15389
15390 /*
15391 ** Close all OpenSession objects and release all associated resources.
15392 */
15393 #if defined(SQLITE_ENABLE_SESSION)
15394 static void session_close_all(ShellState *p, int i){
15395   int j;
15396   struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
15397   for(j=0; j<pAuxDb->nSession; j++){
15398     session_close(&pAuxDb->aSession[j]);
15399   }
15400   pAuxDb->nSession = 0;
15401 }
15402 #else
15403 # define session_close_all(X,Y)
15404 #endif
15405
15406 /*
15407 ** Implementation of the xFilter function for an open session.  Omit
15408 ** any tables named by ".session filter" but let all other table through.
15409 */
15410 #if defined(SQLITE_ENABLE_SESSION)
15411 static int session_filter(void *pCtx, const char *zTab){
15412   OpenSession *pSession = (OpenSession*)pCtx;
15413   int i;
15414   for(i=0; i<pSession->nFilter; i++){
15415     if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
15416   }
15417   return 1;
15418 }
15419 #endif
15420
15421 /*
15422 ** Try to deduce the type of file for zName based on its content.  Return
15423 ** one of the SHELL_OPEN_* constants.
15424 **
15425 ** If the file does not exist or is empty but its name looks like a ZIP
15426 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
15427 ** Otherwise, assume an ordinary database regardless of the filename if
15428 ** the type cannot be determined from content.
15429 */
15430 int deduceDatabaseType(const char *zName, int dfltZip){
15431   FILE *f = fopen(zName, "rb");
15432   size_t n;
15433   int rc = SHELL_OPEN_UNSPEC;
15434   char zBuf[100];
15435   if( f==0 ){
15436     if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15437        return SHELL_OPEN_ZIPFILE;
15438     }else{
15439        return SHELL_OPEN_NORMAL;
15440     }
15441   }
15442   n = fread(zBuf, 16, 1, f);
15443   if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
15444     fclose(f);
15445     return SHELL_OPEN_NORMAL;
15446   }
15447   fseek(f, -25, SEEK_END);
15448   n = fread(zBuf, 25, 1, f);
15449   if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
15450     rc = SHELL_OPEN_APPENDVFS;
15451   }else{
15452     fseek(f, -22, SEEK_END);
15453     n = fread(zBuf, 22, 1, f);
15454     if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
15455        && zBuf[3]==0x06 ){
15456       rc = SHELL_OPEN_ZIPFILE;
15457     }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15458       rc = SHELL_OPEN_ZIPFILE;
15459     }
15460   }
15461   fclose(f);
15462   return rc;  
15463 }
15464
15465 #ifndef SQLITE_OMIT_DESERIALIZE
15466 /*
15467 ** Reconstruct an in-memory database using the output from the "dbtotxt"
15468 ** program.  Read content from the file in p->aAuxDb[].zDbFilename.
15469 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
15470 */
15471 static unsigned char *readHexDb(ShellState *p, int *pnData){
15472   unsigned char *a = 0;
15473   int nLine;
15474   int n = 0;
15475   int pgsz = 0;
15476   int iOffset = 0;
15477   int j, k;
15478   int rc;
15479   FILE *in;
15480   const char *zDbFilename = p->pAuxDb->zDbFilename;
15481   unsigned int x[16];
15482   char zLine[1000];
15483   if( zDbFilename ){
15484     in = fopen(zDbFilename, "r");
15485     if( in==0 ){
15486       utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
15487       return 0;
15488     }
15489     nLine = 0;
15490   }else{
15491     in = p->in;
15492     nLine = p->lineno;
15493     if( in==0 ) in = stdin;
15494   }
15495   *pnData = 0;
15496   nLine++;
15497   if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
15498   rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
15499   if( rc!=2 ) goto readHexDb_error;
15500   if( n<0 ) goto readHexDb_error;
15501   if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
15502   n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
15503   a = sqlite3_malloc( n ? n : 1 );
15504   if( a==0 ){
15505     utf8_printf(stderr, "Out of memory!\n");
15506     goto readHexDb_error;
15507   }
15508   memset(a, 0, n);
15509   if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
15510     utf8_printf(stderr, "invalid pagesize\n");
15511     goto readHexDb_error;
15512   }
15513   for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
15514     rc = sscanf(zLine, "| page %d offset %d", &j, &k);
15515     if( rc==2 ){
15516       iOffset = k;
15517       continue;
15518     }
15519     if( strncmp(zLine, "| end ", 6)==0 ){
15520       break;
15521     }
15522     rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
15523                 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
15524                 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
15525     if( rc==17 ){
15526       k = iOffset+j;
15527       if( k+16<=n && k>=0 ){
15528         int ii;
15529         for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
15530       }
15531     }
15532   }
15533   *pnData = n;
15534   if( in!=p->in ){
15535     fclose(in);
15536   }else{
15537     p->lineno = nLine;
15538   }
15539   return a;
15540
15541 readHexDb_error:
15542   if( in!=p->in ){
15543     fclose(in);
15544   }else{
15545     while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
15546       nLine++;
15547       if(strncmp(zLine, "| end ", 6)==0 ) break;
15548     }
15549     p->lineno = nLine;
15550   }
15551   sqlite3_free(a);
15552   utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
15553   return 0;
15554 }
15555 #endif /* SQLITE_OMIT_DESERIALIZE */
15556
15557 /*
15558 ** Scalar function "shell_int32". The first argument to this function
15559 ** must be a blob. The second a non-negative integer. This function
15560 ** reads and returns a 32-bit big-endian integer from byte
15561 ** offset (4*<arg2>) of the blob.
15562 */
15563 static void shellInt32(
15564   sqlite3_context *context, 
15565   int argc, 
15566   sqlite3_value **argv
15567 ){
15568   const unsigned char *pBlob;
15569   int nBlob;
15570   int iInt;
15571
15572   UNUSED_PARAMETER(argc);
15573   nBlob = sqlite3_value_bytes(argv[0]);
15574   pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
15575   iInt = sqlite3_value_int(argv[1]);
15576
15577   if( iInt>=0 && (iInt+1)*4<=nBlob ){
15578     const unsigned char *a = &pBlob[iInt*4];
15579     sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
15580                        + ((sqlite3_int64)a[1]<<16)
15581                        + ((sqlite3_int64)a[2]<< 8)
15582                        + ((sqlite3_int64)a[3]<< 0);
15583     sqlite3_result_int64(context, iVal);
15584   }
15585 }
15586
15587 /*
15588 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
15589 ** using "..." with internal double-quote characters doubled.
15590 */
15591 static void shellIdQuote(
15592   sqlite3_context *context, 
15593   int argc, 
15594   sqlite3_value **argv
15595 ){
15596   const char *zName = (const char*)sqlite3_value_text(argv[0]);
15597   UNUSED_PARAMETER(argc);
15598   if( zName ){
15599     char *z = sqlite3_mprintf("\"%w\"", zName);
15600     sqlite3_result_text(context, z, -1, sqlite3_free);
15601   }
15602 }
15603
15604 /*
15605 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
15606 */
15607 static void shellUSleepFunc(
15608   sqlite3_context *context, 
15609   int argcUnused, 
15610   sqlite3_value **argv
15611 ){
15612   int sleep = sqlite3_value_int(argv[0]);
15613   (void)argcUnused;
15614   sqlite3_sleep(sleep/1000);
15615   sqlite3_result_int(context, sleep);
15616 }
15617
15618 /*
15619 ** Scalar function "shell_escape_crnl" used by the .recover command.
15620 ** The argument passed to this function is the output of built-in
15621 ** function quote(). If the first character of the input is "'", 
15622 ** indicating that the value passed to quote() was a text value,
15623 ** then this function searches the input for "\n" and "\r" characters
15624 ** and adds a wrapper similar to the following:
15625 **
15626 **   replace(replace(<input>, '\n', char(10), '\r', char(13));
15627 **
15628 ** Or, if the first character of the input is not "'", then a copy
15629 ** of the input is returned.
15630 */
15631 static void shellEscapeCrnl(
15632   sqlite3_context *context, 
15633   int argc, 
15634   sqlite3_value **argv
15635 ){
15636   const char *zText = (const char*)sqlite3_value_text(argv[0]);
15637   UNUSED_PARAMETER(argc);
15638   if( zText[0]=='\'' ){
15639     int nText = sqlite3_value_bytes(argv[0]);
15640     int i;
15641     char zBuf1[20];
15642     char zBuf2[20];
15643     const char *zNL = 0;
15644     const char *zCR = 0;
15645     int nCR = 0;
15646     int nNL = 0;
15647
15648     for(i=0; zText[i]; i++){
15649       if( zNL==0 && zText[i]=='\n' ){
15650         zNL = unused_string(zText, "\\n", "\\012", zBuf1);
15651         nNL = (int)strlen(zNL);
15652       }
15653       if( zCR==0 && zText[i]=='\r' ){
15654         zCR = unused_string(zText, "\\r", "\\015", zBuf2);
15655         nCR = (int)strlen(zCR);
15656       }
15657     }
15658
15659     if( zNL || zCR ){
15660       int iOut = 0;
15661       i64 nMax = (nNL > nCR) ? nNL : nCR;
15662       i64 nAlloc = nMax * nText + (nMax+64)*2;
15663       char *zOut = (char*)sqlite3_malloc64(nAlloc);
15664       if( zOut==0 ){
15665         sqlite3_result_error_nomem(context);
15666         return;
15667       }
15668
15669       if( zNL && zCR ){
15670         memcpy(&zOut[iOut], "replace(replace(", 16);
15671         iOut += 16;
15672       }else{
15673         memcpy(&zOut[iOut], "replace(", 8);
15674         iOut += 8;
15675       }
15676       for(i=0; zText[i]; i++){
15677         if( zText[i]=='\n' ){
15678           memcpy(&zOut[iOut], zNL, nNL);
15679           iOut += nNL;
15680         }else if( zText[i]=='\r' ){
15681           memcpy(&zOut[iOut], zCR, nCR);
15682           iOut += nCR;
15683         }else{
15684           zOut[iOut] = zText[i];
15685           iOut++;
15686         }
15687       }
15688
15689       if( zNL ){
15690         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15691         memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
15692         memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
15693       }
15694       if( zCR ){
15695         memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15696         memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
15697         memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
15698       }
15699
15700       sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
15701       sqlite3_free(zOut);
15702       return;
15703     }
15704   }
15705
15706   sqlite3_result_value(context, argv[0]);
15707 }
15708
15709 /* Flags for open_db().
15710 **
15711 ** The default behavior of open_db() is to exit(1) if the database fails to
15712 ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
15713 ** but still returns without calling exit.
15714 **
15715 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
15716 ** ZIP archive if the file does not exist or is empty and its name matches
15717 ** the *.zip pattern.
15718 */
15719 #define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
15720 #define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
15721
15722 /*
15723 ** Make sure the database is open.  If it is not, then open it.  If
15724 ** the database fails to open, print an error message and exit.
15725 */
15726 static void open_db(ShellState *p, int openFlags){
15727   if( p->db==0 ){
15728     const char *zDbFilename = p->pAuxDb->zDbFilename;
15729     if( p->openMode==SHELL_OPEN_UNSPEC ){
15730       if( zDbFilename==0 || zDbFilename[0]==0 ){
15731         p->openMode = SHELL_OPEN_NORMAL;
15732       }else{
15733         p->openMode = (u8)deduceDatabaseType(zDbFilename, 
15734                              (openFlags & OPEN_DB_ZIPFILE)!=0);
15735       }
15736     }
15737     switch( p->openMode ){
15738       case SHELL_OPEN_APPENDVFS: {
15739         sqlite3_open_v2(zDbFilename, &p->db, 
15740            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
15741         break;
15742       }
15743       case SHELL_OPEN_HEXDB:
15744       case SHELL_OPEN_DESERIALIZE: {
15745         sqlite3_open(0, &p->db);
15746         break;
15747       }
15748       case SHELL_OPEN_ZIPFILE: {
15749         sqlite3_open(":memory:", &p->db);
15750         break;
15751       }
15752       case SHELL_OPEN_READONLY: {
15753         sqlite3_open_v2(zDbFilename, &p->db,
15754             SQLITE_OPEN_READONLY|p->openFlags, 0);
15755         break;
15756       }
15757       case SHELL_OPEN_UNSPEC:
15758       case SHELL_OPEN_NORMAL: {
15759         sqlite3_open_v2(zDbFilename, &p->db,
15760            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
15761         break;
15762       }
15763     }
15764     globalDb = p->db;
15765     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
15766       utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
15767           zDbFilename, sqlite3_errmsg(p->db));
15768       if( openFlags & OPEN_DB_KEEPALIVE ){
15769         sqlite3_open(":memory:", &p->db);
15770         return;
15771       }
15772       exit(1);
15773     }
15774 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15775     sqlite3_enable_load_extension(p->db, 1);
15776 #endif
15777     sqlite3_fileio_init(p->db, 0, 0);
15778     sqlite3_shathree_init(p->db, 0, 0);
15779     sqlite3_completion_init(p->db, 0, 0);
15780     sqlite3_uint_init(p->db, 0, 0);
15781     sqlite3_decimal_init(p->db, 0, 0);
15782     sqlite3_regexp_init(p->db, 0, 0);
15783     sqlite3_ieee_init(p->db, 0, 0);
15784     sqlite3_series_init(p->db, 0, 0);
15785 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15786     sqlite3_dbdata_init(p->db, 0, 0);
15787 #endif
15788 #ifdef SQLITE_HAVE_ZLIB
15789     sqlite3_zipfile_init(p->db, 0, 0);
15790     sqlite3_sqlar_init(p->db, 0, 0);
15791 #endif
15792     sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
15793                             shellAddSchemaName, 0, 0);
15794     sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
15795                             shellModuleSchema, 0, 0);
15796     sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
15797                             shellPutsFunc, 0, 0);
15798     sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
15799                             shellEscapeCrnl, 0, 0);
15800     sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
15801                             shellInt32, 0, 0);
15802     sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
15803                             shellIdQuote, 0, 0);
15804     sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
15805                             shellUSleepFunc, 0, 0);
15806 #ifndef SQLITE_NOHAVE_SYSTEM
15807     sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
15808                             editFunc, 0, 0);
15809     sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
15810                             editFunc, 0, 0);
15811 #endif
15812     if( p->openMode==SHELL_OPEN_ZIPFILE ){
15813       char *zSql = sqlite3_mprintf(
15814          "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
15815       sqlite3_exec(p->db, zSql, 0, 0, 0);
15816       sqlite3_free(zSql);
15817     }
15818 #ifndef SQLITE_OMIT_DESERIALIZE
15819     else
15820     if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
15821       int rc;
15822       int nData = 0;
15823       unsigned char *aData;
15824       if( p->openMode==SHELL_OPEN_DESERIALIZE ){
15825         aData = (unsigned char*)readFile(zDbFilename, &nData);
15826       }else{
15827         aData = readHexDb(p, &nData);
15828         if( aData==0 ){
15829           return;
15830         }
15831       }
15832       rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
15833                    SQLITE_DESERIALIZE_RESIZEABLE |
15834                    SQLITE_DESERIALIZE_FREEONCLOSE);
15835       if( rc ){
15836         utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
15837       }
15838       if( p->szMax>0 ){
15839         sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
15840       }
15841     }
15842 #endif
15843   }
15844   if( p->bSafeModePersist && p->db!=0 ){
15845     sqlite3_set_authorizer(p->db, safeModeAuth, p);
15846   }
15847 }
15848
15849 /*
15850 ** Attempt to close the databaes connection.  Report errors.
15851 */
15852 void close_db(sqlite3 *db){
15853   int rc = sqlite3_close(db);
15854   if( rc ){
15855     utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
15856         rc, sqlite3_errmsg(db));
15857   } 
15858 }
15859
15860 #if HAVE_READLINE || HAVE_EDITLINE
15861 /*
15862 ** Readline completion callbacks
15863 */
15864 static char *readline_completion_generator(const char *text, int state){
15865   static sqlite3_stmt *pStmt = 0;
15866   char *zRet;
15867   if( state==0 ){
15868     char *zSql;
15869     sqlite3_finalize(pStmt);
15870     zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15871                            "  FROM completion(%Q) ORDER BY 1", text);
15872     sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15873     sqlite3_free(zSql);
15874   }
15875   if( sqlite3_step(pStmt)==SQLITE_ROW ){
15876     zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
15877   }else{
15878     sqlite3_finalize(pStmt);
15879     pStmt = 0;
15880     zRet = 0;
15881   }
15882   return zRet;
15883 }
15884 static char **readline_completion(const char *zText, int iStart, int iEnd){
15885   rl_attempted_completion_over = 1;
15886   return rl_completion_matches(zText, readline_completion_generator);
15887 }
15888
15889 #elif HAVE_LINENOISE
15890 /*
15891 ** Linenoise completion callback
15892 */
15893 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
15894   int nLine = strlen30(zLine);
15895   int i, iStart;
15896   sqlite3_stmt *pStmt = 0;
15897   char *zSql;
15898   char zBuf[1000];
15899
15900   if( nLine>sizeof(zBuf)-30 ) return;
15901   if( zLine[0]=='.' || zLine[0]=='#') return;
15902   for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
15903   if( i==nLine-1 ) return;
15904   iStart = i+1;
15905   memcpy(zBuf, zLine, iStart);
15906   zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15907                          "  FROM completion(%Q,%Q) ORDER BY 1",
15908                          &zLine[iStart], zLine);
15909   sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15910   sqlite3_free(zSql);
15911   sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
15912   while( sqlite3_step(pStmt)==SQLITE_ROW ){
15913     const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
15914     int nCompletion = sqlite3_column_bytes(pStmt, 0);
15915     if( iStart+nCompletion < sizeof(zBuf)-1 ){
15916       memcpy(zBuf+iStart, zCompletion, nCompletion+1);
15917       linenoiseAddCompletion(lc, zBuf);
15918     }
15919   }
15920   sqlite3_finalize(pStmt);
15921 }
15922 #endif
15923
15924 /*
15925 ** Do C-language style dequoting.
15926 **
15927 **    \a    -> alarm
15928 **    \b    -> backspace
15929 **    \t    -> tab
15930 **    \n    -> newline
15931 **    \v    -> vertical tab
15932 **    \f    -> form feed
15933 **    \r    -> carriage return
15934 **    \s    -> space
15935 **    \"    -> "
15936 **    \'    -> '
15937 **    \\    -> backslash
15938 **    \NNN  -> ascii character NNN in octal
15939 */
15940 static void resolve_backslashes(char *z){
15941   int i, j;
15942   char c;
15943   while( *z && *z!='\\' ) z++;
15944   for(i=j=0; (c = z[i])!=0; i++, j++){
15945     if( c=='\\' && z[i+1]!=0 ){
15946       c = z[++i];
15947       if( c=='a' ){
15948         c = '\a';
15949       }else if( c=='b' ){
15950         c = '\b';
15951       }else if( c=='t' ){
15952         c = '\t';
15953       }else if( c=='n' ){
15954         c = '\n';
15955       }else if( c=='v' ){
15956         c = '\v';
15957       }else if( c=='f' ){
15958         c = '\f';
15959       }else if( c=='r' ){
15960         c = '\r';
15961       }else if( c=='"' ){
15962         c = '"';
15963       }else if( c=='\'' ){
15964         c = '\'';
15965       }else if( c=='\\' ){
15966         c = '\\';
15967       }else if( c>='0' && c<='7' ){
15968         c -= '0';
15969         if( z[i+1]>='0' && z[i+1]<='7' ){
15970           i++;
15971           c = (c<<3) + z[i] - '0';
15972           if( z[i+1]>='0' && z[i+1]<='7' ){
15973             i++;
15974             c = (c<<3) + z[i] - '0';
15975           }
15976         }
15977       }
15978     }
15979     z[j] = c;
15980   }
15981   if( j<i ) z[j] = 0;
15982 }
15983
15984 /*
15985 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
15986 ** for TRUE and FALSE.  Return the integer value if appropriate.
15987 */
15988 static int booleanValue(const char *zArg){
15989   int i;
15990   if( zArg[0]=='0' && zArg[1]=='x' ){
15991     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
15992   }else{
15993     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
15994   }
15995   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
15996   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
15997     return 1;
15998   }
15999   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
16000     return 0;
16001   }
16002   utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
16003           zArg);
16004   return 0;
16005 }
16006
16007 /*
16008 ** Set or clear a shell flag according to a boolean value.
16009 */
16010 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
16011   if( booleanValue(zArg) ){
16012     ShellSetFlag(p, mFlag);
16013   }else{
16014     ShellClearFlag(p, mFlag);
16015   }
16016 }
16017
16018 /*
16019 ** Close an output file, assuming it is not stderr or stdout
16020 */
16021 static void output_file_close(FILE *f){
16022   if( f && f!=stdout && f!=stderr ) fclose(f);
16023 }
16024
16025 /*
16026 ** Try to open an output file.   The names "stdout" and "stderr" are
16027 ** recognized and do the right thing.  NULL is returned if the output
16028 ** filename is "off".
16029 */
16030 static FILE *output_file_open(const char *zFile, int bTextMode){
16031   FILE *f;
16032   if( strcmp(zFile,"stdout")==0 ){
16033     f = stdout;
16034   }else if( strcmp(zFile, "stderr")==0 ){
16035     f = stderr;
16036   }else if( strcmp(zFile, "off")==0 ){
16037     f = 0;
16038   }else{
16039     f = fopen(zFile, bTextMode ? "w" : "wb");
16040     if( f==0 ){
16041       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16042     }
16043   }
16044   return f;
16045 }
16046
16047 #ifndef SQLITE_OMIT_TRACE
16048 /*
16049 ** A routine for handling output from sqlite3_trace().
16050 */
16051 static int sql_trace_callback(
16052   unsigned mType,         /* The trace type */
16053   void *pArg,             /* The ShellState pointer */
16054   void *pP,               /* Usually a pointer to sqlite_stmt */
16055   void *pX                /* Auxiliary output */
16056 ){
16057   ShellState *p = (ShellState*)pArg;
16058   sqlite3_stmt *pStmt;
16059   const char *zSql;
16060   int nSql;
16061   if( p->traceOut==0 ) return 0;
16062   if( mType==SQLITE_TRACE_CLOSE ){
16063     utf8_printf(p->traceOut, "-- closing database connection\n");
16064     return 0;
16065   }
16066   if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
16067     zSql = (const char*)pX;
16068   }else{
16069     pStmt = (sqlite3_stmt*)pP;
16070     switch( p->eTraceType ){
16071       case SHELL_TRACE_EXPANDED: {
16072         zSql = sqlite3_expanded_sql(pStmt);
16073         break;
16074       }
16075 #ifdef SQLITE_ENABLE_NORMALIZE
16076       case SHELL_TRACE_NORMALIZED: {
16077         zSql = sqlite3_normalized_sql(pStmt);
16078         break;
16079       }
16080 #endif
16081       default: {
16082         zSql = sqlite3_sql(pStmt);
16083         break;
16084       }
16085     }
16086   }
16087   if( zSql==0 ) return 0;
16088   nSql = strlen30(zSql);
16089   while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
16090   switch( mType ){
16091     case SQLITE_TRACE_ROW:
16092     case SQLITE_TRACE_STMT: {
16093       utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
16094       break;
16095     }
16096     case SQLITE_TRACE_PROFILE: {
16097       sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
16098       utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
16099       break;
16100     }
16101   }
16102   return 0;
16103 }
16104 #endif
16105
16106 /*
16107 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
16108 ** a useful spot to set a debugger breakpoint.
16109 */
16110 static void test_breakpoint(void){
16111   static int nCall = 0;
16112   nCall++;
16113 }
16114
16115 /*
16116 ** An object used to read a CSV and other files for import.
16117 */
16118 typedef struct ImportCtx ImportCtx;
16119 struct ImportCtx {
16120   const char *zFile;  /* Name of the input file */
16121   FILE *in;           /* Read the CSV text from this input stream */
16122   int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
16123   char *z;            /* Accumulated text for a field */
16124   int n;              /* Number of bytes in z */
16125   int nAlloc;         /* Space allocated for z[] */
16126   int nLine;          /* Current line number */
16127   int nRow;           /* Number of rows imported */
16128   int nErr;           /* Number of errors encountered */
16129   int bNotFirst;      /* True if one or more bytes already read */
16130   int cTerm;          /* Character that terminated the most recent field */
16131   int cColSep;        /* The column separator character.  (Usually ",") */
16132   int cRowSep;        /* The row separator character.  (Usually "\n") */
16133 };
16134
16135 /* Clean up resourced used by an ImportCtx */
16136 static void import_cleanup(ImportCtx *p){
16137   if( p->in!=0 && p->xCloser!=0 ){
16138     p->xCloser(p->in);
16139     p->in = 0;
16140   }
16141   sqlite3_free(p->z);
16142   p->z = 0;
16143 }
16144
16145 /* Append a single byte to z[] */
16146 static void import_append_char(ImportCtx *p, int c){
16147   if( p->n+1>=p->nAlloc ){
16148     p->nAlloc += p->nAlloc + 100;
16149     p->z = sqlite3_realloc64(p->z, p->nAlloc);
16150     if( p->z==0 ) shell_out_of_memory();
16151   }
16152   p->z[p->n++] = (char)c;
16153 }
16154
16155 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
16156 ** with the option of having a separator other than ",".
16157 **
16158 **   +  Input comes from p->in.
16159 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
16160 **      from sqlite3_malloc64().
16161 **   +  Use p->cSep as the column separator.  The default is ",".
16162 **   +  Use p->rSep as the row separator.  The default is "\n".
16163 **   +  Keep track of the line number in p->nLine.
16164 **   +  Store the character that terminates the field in p->cTerm.  Store
16165 **      EOF on end-of-file.
16166 **   +  Report syntax errors on stderr
16167 */
16168 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
16169   int c;
16170   int cSep = p->cColSep;
16171   int rSep = p->cRowSep;
16172   p->n = 0;
16173   c = fgetc(p->in);
16174   if( c==EOF || seenInterrupt ){
16175     p->cTerm = EOF;
16176     return 0;
16177   }
16178   if( c=='"' ){
16179     int pc, ppc;
16180     int startLine = p->nLine;
16181     int cQuote = c;
16182     pc = ppc = 0;
16183     while( 1 ){
16184       c = fgetc(p->in);
16185       if( c==rSep ) p->nLine++;
16186       if( c==cQuote ){
16187         if( pc==cQuote ){
16188           pc = 0;
16189           continue;
16190         }
16191       }
16192       if( (c==cSep && pc==cQuote)
16193        || (c==rSep && pc==cQuote)
16194        || (c==rSep && pc=='\r' && ppc==cQuote)
16195        || (c==EOF && pc==cQuote)
16196       ){
16197         do{ p->n--; }while( p->z[p->n]!=cQuote );
16198         p->cTerm = c;
16199         break;
16200       }
16201       if( pc==cQuote && c!='\r' ){
16202         utf8_printf(stderr, "%s:%d: unescaped %c character\n",
16203                 p->zFile, p->nLine, cQuote);
16204       }
16205       if( c==EOF ){
16206         utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
16207                 p->zFile, startLine, cQuote);
16208         p->cTerm = c;
16209         break;
16210       }
16211       import_append_char(p, c);
16212       ppc = pc;
16213       pc = c;
16214     }
16215   }else{
16216     /* If this is the first field being parsed and it begins with the
16217     ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
16218     if( (c&0xff)==0xef && p->bNotFirst==0 ){
16219       import_append_char(p, c);
16220       c = fgetc(p->in);
16221       if( (c&0xff)==0xbb ){
16222         import_append_char(p, c);
16223         c = fgetc(p->in);
16224         if( (c&0xff)==0xbf ){
16225           p->bNotFirst = 1;
16226           p->n = 0;
16227           return csv_read_one_field(p);
16228         }
16229       }
16230     }
16231     while( c!=EOF && c!=cSep && c!=rSep ){
16232       import_append_char(p, c);
16233       c = fgetc(p->in);
16234     }
16235     if( c==rSep ){
16236       p->nLine++;
16237       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
16238     }
16239     p->cTerm = c;
16240   }
16241   if( p->z ) p->z[p->n] = 0;
16242   p->bNotFirst = 1;
16243   return p->z;
16244 }
16245
16246 /* Read a single field of ASCII delimited text.
16247 **
16248 **   +  Input comes from p->in.
16249 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
16250 **      from sqlite3_malloc64().
16251 **   +  Use p->cSep as the column separator.  The default is "\x1F".
16252 **   +  Use p->rSep as the row separator.  The default is "\x1E".
16253 **   +  Keep track of the row number in p->nLine.
16254 **   +  Store the character that terminates the field in p->cTerm.  Store
16255 **      EOF on end-of-file.
16256 **   +  Report syntax errors on stderr
16257 */
16258 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
16259   int c;
16260   int cSep = p->cColSep;
16261   int rSep = p->cRowSep;
16262   p->n = 0;
16263   c = fgetc(p->in);
16264   if( c==EOF || seenInterrupt ){
16265     p->cTerm = EOF;
16266     return 0;
16267   }
16268   while( c!=EOF && c!=cSep && c!=rSep ){
16269     import_append_char(p, c);
16270     c = fgetc(p->in);
16271   }
16272   if( c==rSep ){
16273     p->nLine++;
16274   }
16275   p->cTerm = c;
16276   if( p->z ) p->z[p->n] = 0;
16277   return p->z;
16278 }
16279
16280 /*
16281 ** Try to transfer data for table zTable.  If an error is seen while
16282 ** moving forward, try to go backwards.  The backwards movement won't
16283 ** work for WITHOUT ROWID tables.
16284 */
16285 static void tryToCloneData(
16286   ShellState *p,
16287   sqlite3 *newDb,
16288   const char *zTable
16289 ){
16290   sqlite3_stmt *pQuery = 0;
16291   sqlite3_stmt *pInsert = 0;
16292   char *zQuery = 0;
16293   char *zInsert = 0;
16294   int rc;
16295   int i, j, n;
16296   int nTable = strlen30(zTable);
16297   int k = 0;
16298   int cnt = 0;
16299   const int spinRate = 10000;
16300
16301   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
16302   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16303   if( rc ){
16304     utf8_printf(stderr, "Error %d: %s on [%s]\n",
16305             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16306             zQuery);
16307     goto end_data_xfer;
16308   }
16309   n = sqlite3_column_count(pQuery);
16310   zInsert = sqlite3_malloc64(200 + nTable + n*3);
16311   if( zInsert==0 ) shell_out_of_memory();
16312   sqlite3_snprintf(200+nTable,zInsert,
16313                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
16314   i = strlen30(zInsert);
16315   for(j=1; j<n; j++){
16316     memcpy(zInsert+i, ",?", 2);
16317     i += 2;
16318   }
16319   memcpy(zInsert+i, ");", 3);
16320   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
16321   if( rc ){
16322     utf8_printf(stderr, "Error %d: %s on [%s]\n",
16323             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
16324             zQuery);
16325     goto end_data_xfer;
16326   }
16327   for(k=0; k<2; k++){
16328     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16329       for(i=0; i<n; i++){
16330         switch( sqlite3_column_type(pQuery, i) ){
16331           case SQLITE_NULL: {
16332             sqlite3_bind_null(pInsert, i+1);
16333             break;
16334           }
16335           case SQLITE_INTEGER: {
16336             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
16337             break;
16338           }
16339           case SQLITE_FLOAT: {
16340             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
16341             break;
16342           }
16343           case SQLITE_TEXT: {
16344             sqlite3_bind_text(pInsert, i+1,
16345                              (const char*)sqlite3_column_text(pQuery,i),
16346                              -1, SQLITE_STATIC);
16347             break;
16348           }
16349           case SQLITE_BLOB: {
16350             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
16351                                             sqlite3_column_bytes(pQuery,i),
16352                                             SQLITE_STATIC);
16353             break;
16354           }
16355         }
16356       } /* End for */
16357       rc = sqlite3_step(pInsert);
16358       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
16359         utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
16360                         sqlite3_errmsg(newDb));
16361       }
16362       sqlite3_reset(pInsert);
16363       cnt++;
16364       if( (cnt%spinRate)==0 ){
16365         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
16366         fflush(stdout);
16367       }
16368     } /* End while */
16369     if( rc==SQLITE_DONE ) break;
16370     sqlite3_finalize(pQuery);
16371     sqlite3_free(zQuery);
16372     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
16373                              zTable);
16374     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16375     if( rc ){
16376       utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
16377       break;
16378     }
16379   } /* End for(k=0...) */
16380
16381 end_data_xfer:
16382   sqlite3_finalize(pQuery);
16383   sqlite3_finalize(pInsert);
16384   sqlite3_free(zQuery);
16385   sqlite3_free(zInsert);
16386 }
16387
16388
16389 /*
16390 ** Try to transfer all rows of the schema that match zWhere.  For
16391 ** each row, invoke xForEach() on the object defined by that row.
16392 ** If an error is encountered while moving forward through the
16393 ** sqlite_schema table, try again moving backwards.
16394 */
16395 static void tryToCloneSchema(
16396   ShellState *p,
16397   sqlite3 *newDb,
16398   const char *zWhere,
16399   void (*xForEach)(ShellState*,sqlite3*,const char*)
16400 ){
16401   sqlite3_stmt *pQuery = 0;
16402   char *zQuery = 0;
16403   int rc;
16404   const unsigned char *zName;
16405   const unsigned char *zSql;
16406   char *zErrMsg = 0;
16407
16408   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16409                            " WHERE %s", zWhere);
16410   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16411   if( rc ){
16412     utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16413                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16414                     zQuery);
16415     goto end_schema_xfer;
16416   }
16417   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16418     zName = sqlite3_column_text(pQuery, 0);
16419     zSql = sqlite3_column_text(pQuery, 1);
16420     printf("%s... ", zName); fflush(stdout);
16421     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16422     if( zErrMsg ){
16423       utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16424       sqlite3_free(zErrMsg);
16425       zErrMsg = 0;
16426     }
16427     if( xForEach ){
16428       xForEach(p, newDb, (const char*)zName);
16429     }
16430     printf("done\n");
16431   }
16432   if( rc!=SQLITE_DONE ){
16433     sqlite3_finalize(pQuery);
16434     sqlite3_free(zQuery);
16435     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16436                              " WHERE %s ORDER BY rowid DESC", zWhere);
16437     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16438     if( rc ){
16439       utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16440                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16441                       zQuery);
16442       goto end_schema_xfer;
16443     }
16444     while( sqlite3_step(pQuery)==SQLITE_ROW ){
16445       zName = sqlite3_column_text(pQuery, 0);
16446       zSql = sqlite3_column_text(pQuery, 1);
16447       printf("%s... ", zName); fflush(stdout);
16448       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16449       if( zErrMsg ){
16450         utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16451         sqlite3_free(zErrMsg);
16452         zErrMsg = 0;
16453       }
16454       if( xForEach ){
16455         xForEach(p, newDb, (const char*)zName);
16456       }
16457       printf("done\n");
16458     }
16459   }
16460 end_schema_xfer:
16461   sqlite3_finalize(pQuery);
16462   sqlite3_free(zQuery);
16463 }
16464
16465 /*
16466 ** Open a new database file named "zNewDb".  Try to recover as much information
16467 ** as possible out of the main database (which might be corrupt) and write it
16468 ** into zNewDb.
16469 */
16470 static void tryToClone(ShellState *p, const char *zNewDb){
16471   int rc;
16472   sqlite3 *newDb = 0;
16473   if( access(zNewDb,0)==0 ){
16474     utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
16475     return;
16476   }
16477   rc = sqlite3_open(zNewDb, &newDb);
16478   if( rc ){
16479     utf8_printf(stderr, "Cannot create output database: %s\n",
16480             sqlite3_errmsg(newDb));
16481   }else{
16482     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
16483     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
16484     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
16485     tryToCloneSchema(p, newDb, "type!='table'", 0);
16486     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
16487     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
16488   }
16489   close_db(newDb);
16490 }
16491
16492 /*
16493 ** Change the output file back to stdout.
16494 **
16495 ** If the p->doXdgOpen flag is set, that means the output was being
16496 ** redirected to a temporary file named by p->zTempFile.  In that case,
16497 ** launch start/open/xdg-open on that temporary file.
16498 */
16499 static void output_reset(ShellState *p){
16500   if( p->outfile[0]=='|' ){
16501 #ifndef SQLITE_OMIT_POPEN
16502     pclose(p->out);
16503 #endif
16504   }else{
16505     output_file_close(p->out);
16506 #ifndef SQLITE_NOHAVE_SYSTEM
16507     if( p->doXdgOpen ){
16508       const char *zXdgOpenCmd =
16509 #if defined(_WIN32)
16510       "start";
16511 #elif defined(__APPLE__)
16512       "open";
16513 #else
16514       "xdg-open";
16515 #endif
16516       char *zCmd;
16517       zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
16518       if( system(zCmd) ){
16519         utf8_printf(stderr, "Failed: [%s]\n", zCmd);
16520       }else{
16521         /* Give the start/open/xdg-open command some time to get
16522         ** going before we continue, and potential delete the
16523         ** p->zTempFile data file out from under it */
16524         sqlite3_sleep(2000);
16525       }
16526       sqlite3_free(zCmd);
16527       outputModePop(p);
16528       p->doXdgOpen = 0;
16529     }
16530 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
16531   }
16532   p->outfile[0] = 0;
16533   p->out = stdout;
16534 }
16535
16536 /*
16537 ** Run an SQL command and return the single integer result.
16538 */
16539 static int db_int(ShellState *p, const char *zSql){
16540   sqlite3_stmt *pStmt;
16541   int res = 0;
16542   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16543   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
16544     res = sqlite3_column_int(pStmt,0);
16545   }
16546   sqlite3_finalize(pStmt);
16547   return res;
16548 }
16549
16550 /*
16551 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
16552 */
16553 static unsigned int get2byteInt(unsigned char *a){
16554   return (a[0]<<8) + a[1];
16555 }
16556 static unsigned int get4byteInt(unsigned char *a){
16557   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
16558 }
16559
16560 /*
16561 ** Implementation of the ".dbinfo" command.
16562 **
16563 ** Return 1 on error, 2 to exit, and 0 otherwise.
16564 */
16565 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
16566   static const struct { const char *zName; int ofst; } aField[] = {
16567      { "file change counter:",  24  },
16568      { "database page count:",  28  },
16569      { "freelist page count:",  36  },
16570      { "schema cookie:",        40  },
16571      { "schema format:",        44  },
16572      { "default cache size:",   48  },
16573      { "autovacuum top root:",  52  },
16574      { "incremental vacuum:",   64  },
16575      { "text encoding:",        56  },
16576      { "user version:",         60  },
16577      { "application id:",       68  },
16578      { "software version:",     96  },
16579   };
16580   static const struct { const char *zName; const char *zSql; } aQuery[] = {
16581      { "number of tables:",
16582        "SELECT count(*) FROM %s WHERE type='table'" },
16583      { "number of indexes:",
16584        "SELECT count(*) FROM %s WHERE type='index'" },
16585      { "number of triggers:",
16586        "SELECT count(*) FROM %s WHERE type='trigger'" },
16587      { "number of views:",
16588        "SELECT count(*) FROM %s WHERE type='view'" },
16589      { "schema size:",
16590        "SELECT total(length(sql)) FROM %s" },
16591   };
16592   int i, rc;
16593   unsigned iDataVersion;
16594   char *zSchemaTab;
16595   char *zDb = nArg>=2 ? azArg[1] : "main";
16596   sqlite3_stmt *pStmt = 0;
16597   unsigned char aHdr[100];
16598   open_db(p, 0);
16599   if( p->db==0 ) return 1;
16600   rc = sqlite3_prepare_v2(p->db,
16601              "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
16602              -1, &pStmt, 0);
16603   if( rc ){
16604     utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
16605     sqlite3_finalize(pStmt);
16606     return 1;
16607   }
16608   sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
16609   if( sqlite3_step(pStmt)==SQLITE_ROW
16610    && sqlite3_column_bytes(pStmt,0)>100
16611   ){
16612     memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
16613     sqlite3_finalize(pStmt);
16614   }else{
16615     raw_printf(stderr, "unable to read database header\n");
16616     sqlite3_finalize(pStmt);
16617     return 1;
16618   }
16619   i = get2byteInt(aHdr+16);
16620   if( i==1 ) i = 65536;
16621   utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
16622   utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
16623   utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
16624   utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
16625   for(i=0; i<ArraySize(aField); i++){
16626     int ofst = aField[i].ofst;
16627     unsigned int val = get4byteInt(aHdr + ofst);
16628     utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
16629     switch( ofst ){
16630       case 56: {
16631         if( val==1 ) raw_printf(p->out, " (utf8)");
16632         if( val==2 ) raw_printf(p->out, " (utf16le)");
16633         if( val==3 ) raw_printf(p->out, " (utf16be)");
16634       }
16635     }
16636     raw_printf(p->out, "\n");
16637   }
16638   if( zDb==0 ){
16639     zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
16640   }else if( strcmp(zDb,"temp")==0 ){
16641     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
16642   }else{
16643     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
16644   }
16645   for(i=0; i<ArraySize(aQuery); i++){
16646     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
16647     int val = db_int(p, zSql);
16648     sqlite3_free(zSql);
16649     utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
16650   }
16651   sqlite3_free(zSchemaTab);
16652   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
16653   utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
16654   return 0;
16655 }
16656
16657 /*
16658 ** Print the current sqlite3_errmsg() value to stderr and return 1.
16659 */
16660 static int shellDatabaseError(sqlite3 *db){
16661   const char *zErr = sqlite3_errmsg(db);
16662   utf8_printf(stderr, "Error: %s\n", zErr);
16663   return 1;
16664 }
16665
16666 /*
16667 ** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
16668 ** if they match and FALSE (0) if they do not match.
16669 **
16670 ** Globbing rules:
16671 **
16672 **      '*'       Matches any sequence of zero or more characters.
16673 **
16674 **      '?'       Matches exactly one character.
16675 **
16676 **     [...]      Matches one character from the enclosed list of
16677 **                characters.
16678 **
16679 **     [^...]     Matches one character not in the enclosed list.
16680 **
16681 **      '#'       Matches any sequence of one or more digits with an
16682 **                optional + or - sign in front
16683 **
16684 **      ' '       Any span of whitespace matches any other span of
16685 **                whitespace.
16686 **
16687 ** Extra whitespace at the end of z[] is ignored.
16688 */
16689 static int testcase_glob(const char *zGlob, const char *z){
16690   int c, c2;
16691   int invert;
16692   int seen;
16693
16694   while( (c = (*(zGlob++)))!=0 ){
16695     if( IsSpace(c) ){
16696       if( !IsSpace(*z) ) return 0;
16697       while( IsSpace(*zGlob) ) zGlob++;
16698       while( IsSpace(*z) ) z++;
16699     }else if( c=='*' ){
16700       while( (c=(*(zGlob++))) == '*' || c=='?' ){
16701         if( c=='?' && (*(z++))==0 ) return 0;
16702       }
16703       if( c==0 ){
16704         return 1;
16705       }else if( c=='[' ){
16706         while( *z && testcase_glob(zGlob-1,z)==0 ){
16707           z++;
16708         }
16709         return (*z)!=0;
16710       }
16711       while( (c2 = (*(z++)))!=0 ){
16712         while( c2!=c ){
16713           c2 = *(z++);
16714           if( c2==0 ) return 0;
16715         }
16716         if( testcase_glob(zGlob,z) ) return 1;
16717       }
16718       return 0;
16719     }else if( c=='?' ){
16720       if( (*(z++))==0 ) return 0;
16721     }else if( c=='[' ){
16722       int prior_c = 0;
16723       seen = 0;
16724       invert = 0;
16725       c = *(z++);
16726       if( c==0 ) return 0;
16727       c2 = *(zGlob++);
16728       if( c2=='^' ){
16729         invert = 1;
16730         c2 = *(zGlob++);
16731       }
16732       if( c2==']' ){
16733         if( c==']' ) seen = 1;
16734         c2 = *(zGlob++);
16735       }
16736       while( c2 && c2!=']' ){
16737         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
16738           c2 = *(zGlob++);
16739           if( c>=prior_c && c<=c2 ) seen = 1;
16740           prior_c = 0;
16741         }else{
16742           if( c==c2 ){
16743             seen = 1;
16744           }
16745           prior_c = c2;
16746         }
16747         c2 = *(zGlob++);
16748       }
16749       if( c2==0 || (seen ^ invert)==0 ) return 0;
16750     }else if( c=='#' ){
16751       if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
16752       if( !IsDigit(z[0]) ) return 0;
16753       z++;
16754       while( IsDigit(z[0]) ){ z++; }
16755     }else{
16756       if( c!=(*(z++)) ) return 0;
16757     }
16758   }
16759   while( IsSpace(*z) ){ z++; }
16760   return *z==0;
16761 }
16762
16763
16764 /*
16765 ** Compare the string as a command-line option with either one or two
16766 ** initial "-" characters.
16767 */
16768 static int optionMatch(const char *zStr, const char *zOpt){
16769   if( zStr[0]!='-' ) return 0;
16770   zStr++;
16771   if( zStr[0]=='-' ) zStr++;
16772   return strcmp(zStr, zOpt)==0;
16773 }
16774
16775 /*
16776 ** Delete a file.
16777 */
16778 int shellDeleteFile(const char *zFilename){
16779   int rc;
16780 #ifdef _WIN32
16781   wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
16782   rc = _wunlink(z);
16783   sqlite3_free(z);
16784 #else
16785   rc = unlink(zFilename);
16786 #endif
16787   return rc;
16788 }
16789
16790 /*
16791 ** Try to delete the temporary file (if there is one) and free the
16792 ** memory used to hold the name of the temp file.
16793 */
16794 static void clearTempFile(ShellState *p){
16795   if( p->zTempFile==0 ) return;
16796   if( p->doXdgOpen ) return;
16797   if( shellDeleteFile(p->zTempFile) ) return;
16798   sqlite3_free(p->zTempFile);
16799   p->zTempFile = 0;
16800 }
16801
16802 /*
16803 ** Create a new temp file name with the given suffix.
16804 */
16805 static void newTempFile(ShellState *p, const char *zSuffix){
16806   clearTempFile(p);
16807   sqlite3_free(p->zTempFile);
16808   p->zTempFile = 0;
16809   if( p->db ){
16810     sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
16811   }
16812   if( p->zTempFile==0 ){
16813     /* If p->db is an in-memory database then the TEMPFILENAME file-control
16814     ** will not work and we will need to fallback to guessing */
16815     char *zTemp;
16816     sqlite3_uint64 r;
16817     sqlite3_randomness(sizeof(r), &r);
16818     zTemp = getenv("TEMP");
16819     if( zTemp==0 ) zTemp = getenv("TMP");
16820     if( zTemp==0 ){
16821 #ifdef _WIN32
16822       zTemp = "\\tmp";
16823 #else
16824       zTemp = "/tmp";
16825 #endif
16826     }
16827     p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
16828   }else{
16829     p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
16830   }
16831   if( p->zTempFile==0 ){
16832     shell_out_of_memory();
16833   }
16834 }
16835
16836
16837 /*
16838 ** The implementation of SQL scalar function fkey_collate_clause(), used
16839 ** by the ".lint fkey-indexes" command. This scalar function is always
16840 ** called with four arguments - the parent table name, the parent column name,
16841 ** the child table name and the child column name.
16842 **
16843 **   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
16844 **
16845 ** If either of the named tables or columns do not exist, this function
16846 ** returns an empty string. An empty string is also returned if both tables
16847 ** and columns exist but have the same default collation sequence. Or,
16848 ** if both exist but the default collation sequences are different, this
16849 ** function returns the string " COLLATE <parent-collation>", where
16850 ** <parent-collation> is the default collation sequence of the parent column.
16851 */
16852 static void shellFkeyCollateClause(
16853   sqlite3_context *pCtx,
16854   int nVal,
16855   sqlite3_value **apVal
16856 ){
16857   sqlite3 *db = sqlite3_context_db_handle(pCtx);
16858   const char *zParent;
16859   const char *zParentCol;
16860   const char *zParentSeq;
16861   const char *zChild;
16862   const char *zChildCol;
16863   const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
16864   int rc;
16865
16866   assert( nVal==4 );
16867   zParent = (const char*)sqlite3_value_text(apVal[0]);
16868   zParentCol = (const char*)sqlite3_value_text(apVal[1]);
16869   zChild = (const char*)sqlite3_value_text(apVal[2]);
16870   zChildCol = (const char*)sqlite3_value_text(apVal[3]);
16871
16872   sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
16873   rc = sqlite3_table_column_metadata(
16874       db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
16875   );
16876   if( rc==SQLITE_OK ){
16877     rc = sqlite3_table_column_metadata(
16878         db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
16879     );
16880   }
16881
16882   if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
16883     char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
16884     sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
16885     sqlite3_free(z);
16886   }
16887 }
16888
16889
16890 /*
16891 ** The implementation of dot-command ".lint fkey-indexes".
16892 */
16893 static int lintFkeyIndexes(
16894   ShellState *pState,             /* Current shell tool state */
16895   char **azArg,                   /* Array of arguments passed to dot command */
16896   int nArg                        /* Number of entries in azArg[] */
16897 ){
16898   sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
16899   FILE *out = pState->out;        /* Stream to write non-error output to */
16900   int bVerbose = 0;               /* If -verbose is present */
16901   int bGroupByParent = 0;         /* If -groupbyparent is present */
16902   int i;                          /* To iterate through azArg[] */
16903   const char *zIndent = "";       /* How much to indent CREATE INDEX by */
16904   int rc;                         /* Return code */
16905   sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
16906
16907   /*
16908   ** This SELECT statement returns one row for each foreign key constraint
16909   ** in the schema of the main database. The column values are:
16910   **
16911   ** 0. The text of an SQL statement similar to:
16912   **
16913   **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
16914   **
16915   **    This SELECT is similar to the one that the foreign keys implementation
16916   **    needs to run internally on child tables. If there is an index that can
16917   **    be used to optimize this query, then it can also be used by the FK
16918   **    implementation to optimize DELETE or UPDATE statements on the parent
16919   **    table.
16920   **
16921   ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
16922   **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
16923   **    contains an index that can be used to optimize the query.
16924   **
16925   ** 2. Human readable text that describes the child table and columns. e.g.
16926   **
16927   **       "child_table(child_key1, child_key2)"
16928   **
16929   ** 3. Human readable text that describes the parent table and columns. e.g.
16930   **
16931   **       "parent_table(parent_key1, parent_key2)"
16932   **
16933   ** 4. A full CREATE INDEX statement for an index that could be used to
16934   **    optimize DELETE or UPDATE statements on the parent table. e.g.
16935   **
16936   **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
16937   **
16938   ** 5. The name of the parent table.
16939   **
16940   ** These six values are used by the C logic below to generate the report.
16941   */
16942   const char *zSql =
16943   "SELECT "
16944     "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
16945     "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
16946     "  || fkey_collate_clause("
16947     "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
16948     ", "
16949     "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
16950     "  || group_concat('*=?', ' AND ') || ')'"
16951     ", "
16952     "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
16953     ", "
16954     "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
16955     ", "
16956     "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
16957     "  || ' ON ' || quote(s.name) || '('"
16958     "  || group_concat(quote(f.[from]) ||"
16959     "        fkey_collate_clause("
16960     "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
16961     "  || ');'"
16962     ", "
16963     "     f.[table] "
16964     "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
16965     "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
16966     "GROUP BY s.name, f.id "
16967     "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
16968   ;
16969   const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
16970
16971   for(i=2; i<nArg; i++){
16972     int n = strlen30(azArg[i]);
16973     if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
16974       bVerbose = 1;
16975     }
16976     else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
16977       bGroupByParent = 1;
16978       zIndent = "    ";
16979     }
16980     else{
16981       raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
16982           azArg[0], azArg[1]
16983       );
16984       return SQLITE_ERROR;
16985     }
16986   }
16987
16988   /* Register the fkey_collate_clause() SQL function */
16989   rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
16990       0, shellFkeyCollateClause, 0, 0
16991   );
16992
16993
16994   if( rc==SQLITE_OK ){
16995     rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
16996   }
16997   if( rc==SQLITE_OK ){
16998     sqlite3_bind_int(pSql, 1, bGroupByParent);
16999   }
17000
17001   if( rc==SQLITE_OK ){
17002     int rc2;
17003     char *zPrev = 0;
17004     while( SQLITE_ROW==sqlite3_step(pSql) ){
17005       int res = -1;
17006       sqlite3_stmt *pExplain = 0;
17007       const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
17008       const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
17009       const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
17010       const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
17011       const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
17012       const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
17013
17014       rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
17015       if( rc!=SQLITE_OK ) break;
17016       if( SQLITE_ROW==sqlite3_step(pExplain) ){
17017         const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
17018         res = (
17019               0==sqlite3_strglob(zGlob, zPlan)
17020            || 0==sqlite3_strglob(zGlobIPK, zPlan)
17021         );
17022       }
17023       rc = sqlite3_finalize(pExplain);
17024       if( rc!=SQLITE_OK ) break;
17025
17026       if( res<0 ){
17027         raw_printf(stderr, "Error: internal error");
17028         break;
17029       }else{
17030         if( bGroupByParent
17031         && (bVerbose || res==0)
17032         && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
17033         ){
17034           raw_printf(out, "-- Parent table %s\n", zParent);
17035           sqlite3_free(zPrev);
17036           zPrev = sqlite3_mprintf("%s", zParent);
17037         }
17038
17039         if( res==0 ){
17040           raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
17041         }else if( bVerbose ){
17042           raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
17043               zIndent, zFrom, zTarget
17044           );
17045         }
17046       }
17047     }
17048     sqlite3_free(zPrev);
17049
17050     if( rc!=SQLITE_OK ){
17051       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17052     }
17053
17054     rc2 = sqlite3_finalize(pSql);
17055     if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
17056       rc = rc2;
17057       raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17058     }
17059   }else{
17060     raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17061   }
17062
17063   return rc;
17064 }
17065
17066 /*
17067 ** Implementation of ".lint" dot command.
17068 */
17069 static int lintDotCommand(
17070   ShellState *pState,             /* Current shell tool state */
17071   char **azArg,                   /* Array of arguments passed to dot command */
17072   int nArg                        /* Number of entries in azArg[] */
17073 ){
17074   int n;
17075   n = (nArg>=2 ? strlen30(azArg[1]) : 0);
17076   if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
17077   return lintFkeyIndexes(pState, azArg, nArg);
17078
17079  usage:
17080   raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
17081   raw_printf(stderr, "Where sub-commands are:\n");
17082   raw_printf(stderr, "    fkey-indexes\n");
17083   return SQLITE_ERROR;
17084 }
17085
17086 #if !defined SQLITE_OMIT_VIRTUALTABLE
17087 static void shellPrepare(
17088   sqlite3 *db, 
17089   int *pRc, 
17090   const char *zSql, 
17091   sqlite3_stmt **ppStmt
17092 ){
17093   *ppStmt = 0;
17094   if( *pRc==SQLITE_OK ){
17095     int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
17096     if( rc!=SQLITE_OK ){
17097       raw_printf(stderr, "sql error: %s (%d)\n", 
17098           sqlite3_errmsg(db), sqlite3_errcode(db)
17099       );
17100       *pRc = rc;
17101     }
17102   }
17103 }
17104
17105 /*
17106 ** Create a prepared statement using printf-style arguments for the SQL.
17107 **
17108 ** This routine is could be marked "static".  But it is not always used,
17109 ** depending on compile-time options.  By omitting the "static", we avoid
17110 ** nuisance compiler warnings about "defined but not used".
17111 */
17112 void shellPreparePrintf(
17113   sqlite3 *db, 
17114   int *pRc, 
17115   sqlite3_stmt **ppStmt,
17116   const char *zFmt, 
17117   ...
17118 ){
17119   *ppStmt = 0;
17120   if( *pRc==SQLITE_OK ){
17121     va_list ap;
17122     char *z;
17123     va_start(ap, zFmt);
17124     z = sqlite3_vmprintf(zFmt, ap);
17125     va_end(ap);
17126     if( z==0 ){
17127       *pRc = SQLITE_NOMEM;
17128     }else{
17129       shellPrepare(db, pRc, z, ppStmt);
17130       sqlite3_free(z);
17131     }
17132   }
17133 }
17134
17135 /* Finalize the prepared statement created using shellPreparePrintf().
17136 **
17137 ** This routine is could be marked "static".  But it is not always used,
17138 ** depending on compile-time options.  By omitting the "static", we avoid
17139 ** nuisance compiler warnings about "defined but not used".
17140 */
17141 void shellFinalize(
17142   int *pRc, 
17143   sqlite3_stmt *pStmt
17144 ){
17145   if( pStmt ){
17146     sqlite3 *db = sqlite3_db_handle(pStmt);
17147     int rc = sqlite3_finalize(pStmt);
17148     if( *pRc==SQLITE_OK ){
17149       if( rc!=SQLITE_OK ){
17150         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
17151       }
17152       *pRc = rc;
17153     }
17154   }
17155 }
17156
17157 /* Reset the prepared statement created using shellPreparePrintf().
17158 **
17159 ** This routine is could be marked "static".  But it is not always used,
17160 ** depending on compile-time options.  By omitting the "static", we avoid
17161 ** nuisance compiler warnings about "defined but not used".
17162 */
17163 void shellReset(
17164   int *pRc, 
17165   sqlite3_stmt *pStmt
17166 ){
17167   int rc = sqlite3_reset(pStmt);
17168   if( *pRc==SQLITE_OK ){
17169     if( rc!=SQLITE_OK ){
17170       sqlite3 *db = sqlite3_db_handle(pStmt);
17171       raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
17172     }
17173     *pRc = rc;
17174   }
17175 }
17176 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
17177
17178 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17179 /******************************************************************************
17180 ** The ".archive" or ".ar" command.
17181 */
17182 /*
17183 ** Structure representing a single ".ar" command.
17184 */
17185 typedef struct ArCommand ArCommand;
17186 struct ArCommand {
17187   u8 eCmd;                        /* An AR_CMD_* value */
17188   u8 bVerbose;                    /* True if --verbose */
17189   u8 bZip;                        /* True if the archive is a ZIP */
17190   u8 bDryRun;                     /* True if --dry-run */
17191   u8 bAppend;                     /* True if --append */
17192   u8 bGlob;                       /* True if --glob */
17193   u8 fromCmdLine;                 /* Run from -A instead of .archive */
17194   int nArg;                       /* Number of command arguments */
17195   char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
17196   const char *zFile;              /* --file argument, or NULL */
17197   const char *zDir;               /* --directory argument, or NULL */
17198   char **azArg;                   /* Array of command arguments */
17199   ShellState *p;                  /* Shell state */
17200   sqlite3 *db;                    /* Database containing the archive */
17201 };
17202
17203 /*
17204 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
17205 */
17206 static int arUsage(FILE *f){
17207   showHelp(f,"archive");
17208   return SQLITE_ERROR;
17209 }
17210
17211 /*
17212 ** Print an error message for the .ar command to stderr and return 
17213 ** SQLITE_ERROR.
17214 */
17215 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
17216   va_list ap;
17217   char *z;
17218   va_start(ap, zFmt);
17219   z = sqlite3_vmprintf(zFmt, ap);
17220   va_end(ap);
17221   utf8_printf(stderr, "Error: %s\n", z);
17222   if( pAr->fromCmdLine ){
17223     utf8_printf(stderr, "Use \"-A\" for more help\n");
17224   }else{
17225     utf8_printf(stderr, "Use \".archive --help\" for more help\n");
17226   }
17227   sqlite3_free(z);
17228   return SQLITE_ERROR;
17229 }
17230
17231 /*
17232 ** Values for ArCommand.eCmd.
17233 */
17234 #define AR_CMD_CREATE       1
17235 #define AR_CMD_UPDATE       2
17236 #define AR_CMD_INSERT       3
17237 #define AR_CMD_EXTRACT      4
17238 #define AR_CMD_LIST         5
17239 #define AR_CMD_HELP         6
17240 #define AR_CMD_REMOVE       7
17241
17242 /*
17243 ** Other (non-command) switches.
17244 */
17245 #define AR_SWITCH_VERBOSE     8
17246 #define AR_SWITCH_FILE        9
17247 #define AR_SWITCH_DIRECTORY  10
17248 #define AR_SWITCH_APPEND     11
17249 #define AR_SWITCH_DRYRUN     12
17250 #define AR_SWITCH_GLOB       13
17251
17252 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
17253   switch( eSwitch ){
17254     case AR_CMD_CREATE:
17255     case AR_CMD_EXTRACT:
17256     case AR_CMD_LIST:
17257     case AR_CMD_REMOVE:
17258     case AR_CMD_UPDATE:
17259     case AR_CMD_INSERT:
17260     case AR_CMD_HELP:
17261       if( pAr->eCmd ){
17262         return arErrorMsg(pAr, "multiple command options");
17263       }
17264       pAr->eCmd = eSwitch;
17265       break;
17266
17267     case AR_SWITCH_DRYRUN:
17268       pAr->bDryRun = 1;
17269       break;
17270     case AR_SWITCH_GLOB:
17271       pAr->bGlob = 1;
17272       break;
17273     case AR_SWITCH_VERBOSE:
17274       pAr->bVerbose = 1;
17275       break;
17276     case AR_SWITCH_APPEND:
17277       pAr->bAppend = 1;
17278       /* Fall thru into --file */
17279     case AR_SWITCH_FILE:
17280       pAr->zFile = zArg;
17281       break;
17282     case AR_SWITCH_DIRECTORY:
17283       pAr->zDir = zArg;
17284       break;
17285   }
17286
17287   return SQLITE_OK;
17288 }
17289
17290 /*
17291 ** Parse the command line for an ".ar" command. The results are written into
17292 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
17293 ** successfully, otherwise an error message is written to stderr and 
17294 ** SQLITE_ERROR returned.
17295 */
17296 static int arParseCommand(
17297   char **azArg,                   /* Array of arguments passed to dot command */
17298   int nArg,                       /* Number of entries in azArg[] */
17299   ArCommand *pAr                  /* Populate this object */
17300 ){
17301   struct ArSwitch {
17302     const char *zLong;
17303     char cShort;
17304     u8 eSwitch;
17305     u8 bArg;
17306   } aSwitch[] = {
17307     { "create",    'c', AR_CMD_CREATE,       0 },
17308     { "extract",   'x', AR_CMD_EXTRACT,      0 },
17309     { "insert",    'i', AR_CMD_INSERT,       0 },
17310     { "list",      't', AR_CMD_LIST,         0 },
17311     { "remove",    'r', AR_CMD_REMOVE,       0 },
17312     { "update",    'u', AR_CMD_UPDATE,       0 },
17313     { "help",      'h', AR_CMD_HELP,         0 },
17314     { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
17315     { "file",      'f', AR_SWITCH_FILE,      1 },
17316     { "append",    'a', AR_SWITCH_APPEND,    1 },
17317     { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
17318     { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
17319     { "glob",      'g', AR_SWITCH_GLOB,      0 },
17320   };
17321   int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
17322   struct ArSwitch *pEnd = &aSwitch[nSwitch];
17323
17324   if( nArg<=1 ){
17325     utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
17326     return arUsage(stderr);
17327   }else{
17328     char *z = azArg[1];
17329     if( z[0]!='-' ){
17330       /* Traditional style [tar] invocation */
17331       int i;
17332       int iArg = 2;
17333       for(i=0; z[i]; i++){
17334         const char *zArg = 0;
17335         struct ArSwitch *pOpt;
17336         for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17337           if( z[i]==pOpt->cShort ) break;
17338         }
17339         if( pOpt==pEnd ){
17340           return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17341         }
17342         if( pOpt->bArg ){
17343           if( iArg>=nArg ){
17344             return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
17345           }
17346           zArg = azArg[iArg++];
17347         }
17348         if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17349       }
17350       pAr->nArg = nArg-iArg;
17351       if( pAr->nArg>0 ){
17352         pAr->azArg = &azArg[iArg];
17353       }
17354     }else{
17355       /* Non-traditional invocation */
17356       int iArg;
17357       for(iArg=1; iArg<nArg; iArg++){
17358         int n;
17359         z = azArg[iArg];
17360         if( z[0]!='-' ){
17361           /* All remaining command line words are command arguments. */
17362           pAr->azArg = &azArg[iArg];
17363           pAr->nArg = nArg-iArg;
17364           break;
17365         }
17366         n = strlen30(z);
17367
17368         if( z[1]!='-' ){
17369           int i;
17370           /* One or more short options */
17371           for(i=1; i<n; i++){
17372             const char *zArg = 0;
17373             struct ArSwitch *pOpt;
17374             for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17375               if( z[i]==pOpt->cShort ) break;
17376             }
17377             if( pOpt==pEnd ){
17378               return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17379             }
17380             if( pOpt->bArg ){
17381               if( i<(n-1) ){
17382                 zArg = &z[i+1];
17383                 i = n;
17384               }else{
17385                 if( iArg>=(nArg-1) ){
17386                   return arErrorMsg(pAr, "option requires an argument: %c",
17387                                     z[i]);
17388                 }
17389                 zArg = azArg[++iArg];
17390               }
17391             }
17392             if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17393           }
17394         }else if( z[2]=='\0' ){
17395           /* A -- option, indicating that all remaining command line words
17396           ** are command arguments.  */
17397           pAr->azArg = &azArg[iArg+1];
17398           pAr->nArg = nArg-iArg-1;
17399           break;
17400         }else{
17401           /* A long option */
17402           const char *zArg = 0;             /* Argument for option, if any */
17403           struct ArSwitch *pMatch = 0;      /* Matching option */
17404           struct ArSwitch *pOpt;            /* Iterator */
17405           for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17406             const char *zLong = pOpt->zLong;
17407             if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
17408               if( pMatch ){
17409                 return arErrorMsg(pAr, "ambiguous option: %s",z);
17410               }else{
17411                 pMatch = pOpt;
17412               }
17413             }
17414           }
17415
17416           if( pMatch==0 ){
17417             return arErrorMsg(pAr, "unrecognized option: %s", z);
17418           }
17419           if( pMatch->bArg ){
17420             if( iArg>=(nArg-1) ){
17421               return arErrorMsg(pAr, "option requires an argument: %s", z);
17422             }
17423             zArg = azArg[++iArg];
17424           }
17425           if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
17426         }
17427       }
17428     }
17429   }
17430
17431   return SQLITE_OK;
17432 }
17433
17434 /*
17435 ** This function assumes that all arguments within the ArCommand.azArg[]
17436 ** array refer to archive members, as for the --extract, --list or --remove
17437 ** commands. It checks that each of them are "present". If any specified
17438 ** file is not present in the archive, an error is printed to stderr and an
17439 ** error code returned. Otherwise, if all specified arguments are present
17440 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
17441 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
17442 ** when pAr->bGlob is true.
17443 **
17444 ** This function strips any trailing '/' characters from each argument.
17445 ** This is consistent with the way the [tar] command seems to work on
17446 ** Linux.
17447 */
17448 static int arCheckEntries(ArCommand *pAr){
17449   int rc = SQLITE_OK;
17450   if( pAr->nArg ){
17451     int i, j;
17452     sqlite3_stmt *pTest = 0;
17453     const char *zSel = (pAr->bGlob)
17454       ? "SELECT name FROM %s WHERE glob($name,name)"
17455       : "SELECT name FROM %s WHERE name=$name";
17456
17457     shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
17458     j = sqlite3_bind_parameter_index(pTest, "$name");
17459     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17460       char *z = pAr->azArg[i];
17461       int n = strlen30(z);
17462       int bOk = 0;
17463       while( n>0 && z[n-1]=='/' ) n--;
17464       z[n] = '\0';
17465       sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
17466       if( SQLITE_ROW==sqlite3_step(pTest) ){
17467         bOk = 1;
17468       }
17469       shellReset(&rc, pTest);
17470       if( rc==SQLITE_OK && bOk==0 ){
17471         utf8_printf(stderr, "not found in archive: %s\n", z);
17472         rc = SQLITE_ERROR;
17473       }
17474     }
17475     shellFinalize(&rc, pTest);
17476   }
17477   return rc;
17478 }
17479
17480 /*
17481 ** Format a WHERE clause that can be used against the "sqlar" table to
17482 ** identify all archive members that match the command arguments held
17483 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
17484 ** The caller is responsible for eventually calling sqlite3_free() on
17485 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
17486 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
17487 */
17488 static void arWhereClause(
17489   int *pRc, 
17490   ArCommand *pAr,
17491   char **pzWhere                  /* OUT: New WHERE clause */
17492 ){
17493   char *zWhere = 0;
17494   const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
17495   if( *pRc==SQLITE_OK ){
17496     if( pAr->nArg==0 ){
17497       zWhere = sqlite3_mprintf("1");
17498     }else{
17499       int i;
17500       const char *zSep = "";
17501       for(i=0; i<pAr->nArg; i++){
17502         const char *z = pAr->azArg[i];
17503         zWhere = sqlite3_mprintf(
17504           "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'", 
17505           zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
17506         );
17507         if( zWhere==0 ){
17508           *pRc = SQLITE_NOMEM;
17509           break;
17510         }
17511         zSep = " OR ";
17512       }
17513     }
17514   }
17515   *pzWhere = zWhere;
17516 }
17517
17518 /*
17519 ** Implementation of .ar "lisT" command. 
17520 */
17521 static int arListCommand(ArCommand *pAr){
17522   const char *zSql = "SELECT %s FROM %s WHERE %s"; 
17523   const char *azCols[] = {
17524     "name",
17525     "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
17526   };
17527
17528   char *zWhere = 0;
17529   sqlite3_stmt *pSql = 0;
17530   int rc;
17531
17532   rc = arCheckEntries(pAr);
17533   arWhereClause(&rc, pAr, &zWhere);
17534
17535   shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
17536                      pAr->zSrcTable, zWhere);
17537   if( pAr->bDryRun ){
17538     utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17539   }else{
17540     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17541       if( pAr->bVerbose ){
17542         utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
17543             sqlite3_column_text(pSql, 0),
17544             sqlite3_column_int(pSql, 1), 
17545             sqlite3_column_text(pSql, 2),
17546             sqlite3_column_text(pSql, 3)
17547         );
17548       }else{
17549         utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17550       }
17551     }
17552   }
17553   shellFinalize(&rc, pSql);
17554   sqlite3_free(zWhere);
17555   return rc;
17556 }
17557
17558
17559 /*
17560 ** Implementation of .ar "Remove" command.
17561 */
17562 static int arRemoveCommand(ArCommand *pAr){
17563   int rc = 0;
17564   char *zSql = 0;
17565   char *zWhere = 0;
17566
17567   if( pAr->nArg ){
17568     /* Verify that args actually exist within the archive before proceeding.
17569     ** And formulate a WHERE clause to match them.  */
17570     rc = arCheckEntries(pAr);
17571     arWhereClause(&rc, pAr, &zWhere);
17572   }
17573   if( rc==SQLITE_OK ){
17574     zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
17575                            pAr->zSrcTable, zWhere);
17576     if( pAr->bDryRun ){
17577       utf8_printf(pAr->p->out, "%s\n", zSql);
17578     }else{
17579       char *zErr = 0;
17580       rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
17581       if( rc==SQLITE_OK ){
17582         rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17583         if( rc!=SQLITE_OK ){
17584           sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
17585         }else{
17586           rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
17587         }
17588       }
17589       if( zErr ){
17590         utf8_printf(stdout, "ERROR: %s\n", zErr);
17591         sqlite3_free(zErr);
17592       }
17593     }
17594   }
17595   sqlite3_free(zWhere);
17596   sqlite3_free(zSql);
17597   return rc;
17598 }
17599
17600 /*
17601 ** Implementation of .ar "eXtract" command. 
17602 */
17603 static int arExtractCommand(ArCommand *pAr){
17604   const char *zSql1 = 
17605     "SELECT "
17606     " ($dir || name),"
17607     " writefile(($dir || name), %s, mode, mtime) "
17608     "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
17609     " AND name NOT GLOB '*..[/\\]*'";
17610
17611   const char *azExtraArg[] = { 
17612     "sqlar_uncompress(data, sz)",
17613     "data"
17614   };
17615
17616   sqlite3_stmt *pSql = 0;
17617   int rc = SQLITE_OK;
17618   char *zDir = 0;
17619   char *zWhere = 0;
17620   int i, j;
17621
17622   /* If arguments are specified, check that they actually exist within
17623   ** the archive before proceeding. And formulate a WHERE clause to
17624   ** match them.  */
17625   rc = arCheckEntries(pAr);
17626   arWhereClause(&rc, pAr, &zWhere);
17627
17628   if( rc==SQLITE_OK ){
17629     if( pAr->zDir ){
17630       zDir = sqlite3_mprintf("%s/", pAr->zDir);
17631     }else{
17632       zDir = sqlite3_mprintf("");
17633     }
17634     if( zDir==0 ) rc = SQLITE_NOMEM;
17635   }
17636
17637   shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, 
17638       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
17639   );
17640
17641   if( rc==SQLITE_OK ){
17642     j = sqlite3_bind_parameter_index(pSql, "$dir");
17643     sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
17644
17645     /* Run the SELECT statement twice. The first time, writefile() is called
17646     ** for all archive members that should be extracted. The second time,
17647     ** only for the directories. This is because the timestamps for
17648     ** extracted directories must be reset after they are populated (as
17649     ** populating them changes the timestamp).  */
17650     for(i=0; i<2; i++){
17651       j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
17652       sqlite3_bind_int(pSql, j, i);
17653       if( pAr->bDryRun ){
17654         utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17655       }else{
17656         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17657           if( i==0 && pAr->bVerbose ){
17658             utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17659           }
17660         }
17661       }
17662       shellReset(&rc, pSql);
17663     }
17664     shellFinalize(&rc, pSql);
17665   }
17666
17667   sqlite3_free(zDir);
17668   sqlite3_free(zWhere);
17669   return rc;
17670 }
17671
17672 /*
17673 ** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
17674 */
17675 static int arExecSql(ArCommand *pAr, const char *zSql){
17676   int rc;
17677   if( pAr->bDryRun ){
17678     utf8_printf(pAr->p->out, "%s\n", zSql);
17679     rc = SQLITE_OK;
17680   }else{
17681     char *zErr = 0;
17682     rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17683     if( zErr ){
17684       utf8_printf(stdout, "ERROR: %s\n", zErr);
17685       sqlite3_free(zErr);
17686     }
17687   }
17688   return rc;
17689 }
17690
17691
17692 /*
17693 ** Implementation of .ar "create", "insert", and "update" commands.
17694 **
17695 **     create    ->     Create a new SQL archive
17696 **     insert    ->     Insert or reinsert all files listed
17697 **     update    ->     Insert files that have changed or that were not
17698 **                      previously in the archive
17699 **
17700 ** Create the "sqlar" table in the database if it does not already exist.
17701 ** Then add each file in the azFile[] array to the archive. Directories
17702 ** are added recursively. If argument bVerbose is non-zero, a message is
17703 ** printed on stdout for each file archived.
17704 **
17705 ** The create command is the same as update, except that it drops
17706 ** any existing "sqlar" table before beginning.  The "insert" command
17707 ** always overwrites every file named on the command-line, where as
17708 ** "update" only overwrites if the size or mtime or mode has changed.
17709 */
17710 static int arCreateOrUpdateCommand(
17711   ArCommand *pAr,                 /* Command arguments and options */
17712   int bUpdate,                    /* true for a --create. */
17713   int bOnlyIfChanged              /* Only update if file has changed */
17714 ){
17715   const char *zCreate = 
17716       "CREATE TABLE IF NOT EXISTS sqlar(\n"
17717       "  name TEXT PRIMARY KEY,  -- name of the file\n"
17718       "  mode INT,               -- access permissions\n"
17719       "  mtime INT,              -- last modification time\n"
17720       "  sz INT,                 -- original file size\n"
17721       "  data BLOB               -- compressed content\n"
17722       ")";
17723   const char *zDrop = "DROP TABLE IF EXISTS sqlar";
17724   const char *zInsertFmt[2] = {
17725      "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
17726      "  SELECT\n"
17727      "    %s,\n"
17728      "    mode,\n"
17729      "    mtime,\n"
17730      "    CASE substr(lsmode(mode),1,1)\n"
17731      "      WHEN '-' THEN length(data)\n"
17732      "      WHEN 'd' THEN 0\n"
17733      "      ELSE -1 END,\n"
17734      "    sqlar_compress(data)\n"
17735      "  FROM fsdir(%Q,%Q) AS disk\n"
17736      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17737      ,
17738      "REPLACE INTO %s(name,mode,mtime,data)\n"
17739      "  SELECT\n"
17740      "    %s,\n"
17741      "    mode,\n"
17742      "    mtime,\n"
17743      "    data\n"
17744      "  FROM fsdir(%Q,%Q) AS disk\n"
17745      "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17746   };
17747   int i;                          /* For iterating through azFile[] */
17748   int rc;                         /* Return code */
17749   const char *zTab = 0;           /* SQL table into which to insert */
17750   char *zSql;
17751   char zTemp[50];
17752   char *zExists = 0;
17753
17754   arExecSql(pAr, "PRAGMA page_size=512");
17755   rc = arExecSql(pAr, "SAVEPOINT ar;");
17756   if( rc!=SQLITE_OK ) return rc;
17757   zTemp[0] = 0; 
17758   if( pAr->bZip ){
17759     /* Initialize the zipfile virtual table, if necessary */
17760     if( pAr->zFile ){
17761       sqlite3_uint64 r;
17762       sqlite3_randomness(sizeof(r),&r);
17763       sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
17764       zTab = zTemp;
17765       zSql = sqlite3_mprintf(
17766          "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
17767          zTab, pAr->zFile
17768       );
17769       rc = arExecSql(pAr, zSql);
17770       sqlite3_free(zSql);
17771     }else{
17772       zTab = "zip";
17773     }
17774   }else{
17775     /* Initialize the table for an SQLAR */
17776     zTab = "sqlar";
17777     if( bUpdate==0 ){
17778       rc = arExecSql(pAr, zDrop);
17779       if( rc!=SQLITE_OK ) goto end_ar_transaction;
17780     }
17781     rc = arExecSql(pAr, zCreate);
17782   }
17783   if( bOnlyIfChanged ){
17784     zExists = sqlite3_mprintf(
17785       " AND NOT EXISTS("
17786           "SELECT 1 FROM %s AS mem"
17787           " WHERE mem.name=disk.name"
17788           " AND mem.mtime=disk.mtime"
17789           " AND mem.mode=disk.mode)", zTab);
17790   }else{
17791     zExists = sqlite3_mprintf("");
17792   }
17793   if( zExists==0 ) rc = SQLITE_NOMEM;
17794   for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17795     char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
17796         pAr->bVerbose ? "shell_putsnl(name)" : "name",
17797         pAr->azArg[i], pAr->zDir, zExists);
17798     rc = arExecSql(pAr, zSql2);
17799     sqlite3_free(zSql2);
17800   }
17801 end_ar_transaction:
17802   if( rc!=SQLITE_OK ){
17803     sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
17804   }else{
17805     rc = arExecSql(pAr, "RELEASE ar;");
17806     if( pAr->bZip && pAr->zFile ){
17807       zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
17808       arExecSql(pAr, zSql);
17809       sqlite3_free(zSql);
17810     }
17811   }
17812   sqlite3_free(zExists);
17813   return rc;
17814 }
17815
17816 /*
17817 ** Implementation of ".ar" dot command.
17818 */
17819 static int arDotCommand(
17820   ShellState *pState,          /* Current shell tool state */
17821   int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
17822   char **azArg,                /* Array of arguments passed to dot command */
17823   int nArg                     /* Number of entries in azArg[] */
17824 ){
17825   ArCommand cmd;
17826   int rc;
17827   memset(&cmd, 0, sizeof(cmd));
17828   cmd.fromCmdLine = fromCmdLine;
17829   rc = arParseCommand(azArg, nArg, &cmd);
17830   if( rc==SQLITE_OK ){
17831     int eDbType = SHELL_OPEN_UNSPEC;
17832     cmd.p = pState;
17833     cmd.db = pState->db;
17834     if( cmd.zFile ){
17835       eDbType = deduceDatabaseType(cmd.zFile, 1);
17836     }else{
17837       eDbType = pState->openMode;
17838     }
17839     if( eDbType==SHELL_OPEN_ZIPFILE ){
17840       if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
17841         if( cmd.zFile==0 ){
17842           cmd.zSrcTable = sqlite3_mprintf("zip");
17843         }else{
17844           cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
17845         }
17846       }
17847       cmd.bZip = 1;
17848     }else if( cmd.zFile ){
17849       int flags;
17850       if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
17851       if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT 
17852            || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
17853         flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
17854       }else{
17855         flags = SQLITE_OPEN_READONLY;
17856       }
17857       cmd.db = 0;
17858       if( cmd.bDryRun ){
17859         utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
17860              eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
17861       }
17862       rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, 
17863              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
17864       if( rc!=SQLITE_OK ){
17865         utf8_printf(stderr, "cannot open file: %s (%s)\n", 
17866             cmd.zFile, sqlite3_errmsg(cmd.db)
17867         );
17868         goto end_ar_command;
17869       }
17870       sqlite3_fileio_init(cmd.db, 0, 0);
17871       sqlite3_sqlar_init(cmd.db, 0, 0);
17872       sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
17873                               shellPutsFunc, 0, 0);
17874
17875     }
17876     if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
17877       if( cmd.eCmd!=AR_CMD_CREATE
17878        && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
17879       ){
17880         utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
17881         rc = SQLITE_ERROR;
17882         goto end_ar_command;
17883       }
17884       cmd.zSrcTable = sqlite3_mprintf("sqlar");
17885     }
17886
17887     switch( cmd.eCmd ){
17888       case AR_CMD_CREATE:
17889         rc = arCreateOrUpdateCommand(&cmd, 0, 0);
17890         break;
17891
17892       case AR_CMD_EXTRACT:
17893         rc = arExtractCommand(&cmd);
17894         break;
17895
17896       case AR_CMD_LIST:
17897         rc = arListCommand(&cmd);
17898         break;
17899
17900       case AR_CMD_HELP:
17901         arUsage(pState->out);
17902         break;
17903
17904       case AR_CMD_INSERT:
17905         rc = arCreateOrUpdateCommand(&cmd, 1, 0);
17906         break;
17907
17908       case AR_CMD_REMOVE:
17909         rc = arRemoveCommand(&cmd);
17910         break;
17911
17912       default:
17913         assert( cmd.eCmd==AR_CMD_UPDATE );
17914         rc = arCreateOrUpdateCommand(&cmd, 1, 1);
17915         break;
17916     }
17917   }
17918 end_ar_command:
17919   if( cmd.db!=pState->db ){
17920     close_db(cmd.db);
17921   }
17922   sqlite3_free(cmd.zSrcTable);
17923
17924   return rc;
17925 }
17926 /* End of the ".archive" or ".ar" command logic
17927 *******************************************************************************/
17928 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
17929
17930 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17931 /*
17932 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
17933 ** Otherwise, the SQL statement or statements in zSql are executed using
17934 ** database connection db and the error code written to *pRc before
17935 ** this function returns.
17936 */
17937 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
17938   int rc = *pRc;
17939   if( rc==SQLITE_OK ){
17940     char *zErr = 0;
17941     rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
17942     if( rc!=SQLITE_OK ){
17943       raw_printf(stderr, "SQL error: %s\n", zErr);
17944     }
17945     sqlite3_free(zErr);
17946     *pRc = rc;
17947   }
17948 }
17949
17950 /*
17951 ** Like shellExec(), except that zFmt is a printf() style format string.
17952 */
17953 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
17954   char *z = 0;
17955   if( *pRc==SQLITE_OK ){
17956     va_list ap;
17957     va_start(ap, zFmt);
17958     z = sqlite3_vmprintf(zFmt, ap);
17959     va_end(ap);
17960     if( z==0 ){
17961       *pRc = SQLITE_NOMEM;
17962     }else{
17963       shellExec(db, pRc, z);
17964     }
17965     sqlite3_free(z);
17966   }
17967 }
17968
17969 /*
17970 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17971 ** Otherwise, an attempt is made to allocate, zero and return a pointer
17972 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
17973 ** to SQLITE_NOMEM and NULL returned.
17974 */
17975 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
17976   void *pRet = 0;
17977   if( *pRc==SQLITE_OK ){
17978     pRet = sqlite3_malloc64(nByte);
17979     if( pRet==0 ){
17980       *pRc = SQLITE_NOMEM;
17981     }else{
17982       memset(pRet, 0, nByte);
17983     }
17984   }
17985   return pRet;
17986 }
17987
17988 /*
17989 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17990 ** Otherwise, zFmt is treated as a printf() style string. The result of
17991 ** formatting it along with any trailing arguments is written into a 
17992 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
17993 ** It is the responsibility of the caller to eventually free this buffer
17994 ** using a call to sqlite3_free().
17995 ** 
17996 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL 
17997 ** pointer returned.
17998 */
17999 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
18000   char *z = 0;
18001   if( *pRc==SQLITE_OK ){
18002     va_list ap;
18003     va_start(ap, zFmt);
18004     z = sqlite3_vmprintf(zFmt, ap);
18005     va_end(ap);
18006     if( z==0 ){
18007       *pRc = SQLITE_NOMEM;
18008     }
18009   }
18010   return z;
18011 }
18012
18013 /*
18014 ** When running the ".recover" command, each output table, and the special
18015 ** orphaned row table if it is required, is represented by an instance
18016 ** of the following struct.
18017 */
18018 typedef struct RecoverTable RecoverTable;
18019 struct RecoverTable {
18020   char *zQuoted;                  /* Quoted version of table name */
18021   int nCol;                       /* Number of columns in table */
18022   char **azlCol;                  /* Array of column lists */
18023   int iPk;                        /* Index of IPK column */
18024 };
18025
18026 /*
18027 ** Free a RecoverTable object allocated by recoverFindTable() or
18028 ** recoverOrphanTable().
18029 */
18030 static void recoverFreeTable(RecoverTable *pTab){
18031   if( pTab ){
18032     sqlite3_free(pTab->zQuoted);
18033     if( pTab->azlCol ){
18034       int i;
18035       for(i=0; i<=pTab->nCol; i++){
18036         sqlite3_free(pTab->azlCol[i]);
18037       }
18038       sqlite3_free(pTab->azlCol);
18039     }
18040     sqlite3_free(pTab);
18041   }
18042 }
18043
18044 /*
18045 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
18046 ** Otherwise, it allocates and returns a RecoverTable object based on the
18047 ** final four arguments passed to this function. It is the responsibility
18048 ** of the caller to eventually free the returned object using
18049 ** recoverFreeTable().
18050 */
18051 static RecoverTable *recoverNewTable(
18052   int *pRc,                       /* IN/OUT: Error code */
18053   const char *zName,              /* Name of table */
18054   const char *zSql,               /* CREATE TABLE statement */
18055   int bIntkey, 
18056   int nCol
18057 ){
18058   sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
18059   int rc = *pRc;
18060   RecoverTable *pTab = 0;
18061
18062   pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
18063   if( rc==SQLITE_OK ){
18064     int nSqlCol = 0;
18065     int bSqlIntkey = 0;
18066     sqlite3_stmt *pStmt = 0;
18067     
18068     rc = sqlite3_open("", &dbtmp);
18069     if( rc==SQLITE_OK ){
18070       sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
18071                               shellIdQuote, 0, 0);
18072     }
18073     if( rc==SQLITE_OK ){
18074       rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
18075     }
18076     if( rc==SQLITE_OK ){
18077       rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
18078       if( rc==SQLITE_ERROR ){
18079         rc = SQLITE_OK;
18080         goto finished;
18081       }
18082     }
18083     shellPreparePrintf(dbtmp, &rc, &pStmt, 
18084         "SELECT count(*) FROM pragma_table_info(%Q)", zName
18085     );
18086     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18087       nSqlCol = sqlite3_column_int(pStmt, 0);
18088     }
18089     shellFinalize(&rc, pStmt);
18090
18091     if( rc!=SQLITE_OK || nSqlCol<nCol ){
18092       goto finished;
18093     }
18094
18095     shellPreparePrintf(dbtmp, &rc, &pStmt, 
18096       "SELECT ("
18097       "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
18098       ") FROM sqlite_schema WHERE name = %Q", zName
18099     );
18100     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18101       bSqlIntkey = sqlite3_column_int(pStmt, 0);
18102     }
18103     shellFinalize(&rc, pStmt);
18104
18105     if( bIntkey==bSqlIntkey ){
18106       int i;
18107       const char *zPk = "_rowid_";
18108       sqlite3_stmt *pPkFinder = 0;
18109
18110       /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
18111       ** set zPk to the name of the PK column, and pTab->iPk to the index
18112       ** of the column, where columns are 0-numbered from left to right.
18113       ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
18114       ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
18115       pTab->iPk = -2;
18116       if( bIntkey ){
18117         shellPreparePrintf(dbtmp, &rc, &pPkFinder, 
18118           "SELECT cid, name FROM pragma_table_info(%Q) "
18119           "  WHERE pk=1 AND type='integer' COLLATE nocase"
18120           "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
18121           , zName, zName
18122         );
18123         if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
18124           pTab->iPk = sqlite3_column_int(pPkFinder, 0);
18125           zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
18126         }
18127       }
18128
18129       pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
18130       pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
18131       pTab->nCol = nSqlCol;
18132
18133       if( bIntkey ){
18134         pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
18135       }else{
18136         pTab->azlCol[0] = shellMPrintf(&rc, "");
18137       }
18138       i = 1;
18139       shellPreparePrintf(dbtmp, &rc, &pStmt, 
18140           "SELECT %Q || group_concat(shell_idquote(name), ', ') "
18141           "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
18142           "FROM pragma_table_info(%Q)", 
18143           bIntkey ? ", " : "", pTab->iPk, 
18144           bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
18145           zName
18146       );
18147       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18148         const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
18149         pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
18150         i++;
18151       }
18152       shellFinalize(&rc, pStmt);
18153
18154       shellFinalize(&rc, pPkFinder);
18155     }
18156   }
18157
18158  finished:
18159   sqlite3_close(dbtmp);
18160   *pRc = rc;
18161   if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
18162     recoverFreeTable(pTab);
18163     pTab = 0;
18164   }
18165   return pTab;
18166 }
18167
18168 /*
18169 ** This function is called to search the schema recovered from the
18170 ** sqlite_schema table of the (possibly) corrupt database as part
18171 ** of a ".recover" command. Specifically, for a table with root page
18172 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
18173 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
18174 ** those.
18175 **
18176 ** If a table is found, a (RecoverTable*) object is returned. Or, if
18177 ** no such table is found, but bIntkey is false and iRoot is the 
18178 ** root page of an index in the recovered schema, then (*pbNoop) is
18179 ** set to true and NULL returned. Or, if there is no such table or
18180 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
18181 ** the caller should write data to the orphans table.
18182 */
18183 static RecoverTable *recoverFindTable(
18184   ShellState *pState,             /* Shell state object */
18185   int *pRc,                       /* IN/OUT: Error code */
18186   int iRoot,                      /* Root page of table */
18187   int bIntkey,                    /* True for an intkey table */
18188   int nCol,                       /* Number of columns in table */
18189   int *pbNoop                     /* OUT: True if iRoot is root of index */
18190 ){
18191   sqlite3_stmt *pStmt = 0;
18192   RecoverTable *pRet = 0;
18193   int bNoop = 0;
18194   const char *zSql = 0;
18195   const char *zName = 0;
18196
18197   /* Search the recovered schema for an object with root page iRoot. */
18198   shellPreparePrintf(pState->db, pRc, &pStmt,
18199       "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
18200   );
18201   while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18202     const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
18203     if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
18204       bNoop = 1;
18205       break;
18206     }
18207     if( sqlite3_stricmp(zType, "table")==0 ){
18208       zName = (const char*)sqlite3_column_text(pStmt, 1);
18209       zSql = (const char*)sqlite3_column_text(pStmt, 2);
18210       pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18211       break;
18212     }
18213   }
18214
18215   shellFinalize(pRc, pStmt);
18216   *pbNoop = bNoop;
18217   return pRet;
18218 }
18219
18220 /*
18221 ** Return a RecoverTable object representing the orphans table.
18222 */
18223 static RecoverTable *recoverOrphanTable(
18224   ShellState *pState,             /* Shell state object */
18225   int *pRc,                       /* IN/OUT: Error code */
18226   const char *zLostAndFound,      /* Base name for orphans table */
18227   int nCol                        /* Number of user data columns */
18228 ){
18229   RecoverTable *pTab = 0;
18230   if( nCol>=0 && *pRc==SQLITE_OK ){
18231     int i;
18232
18233     /* This block determines the name of the orphan table. The prefered
18234     ** name is zLostAndFound. But if that clashes with another name
18235     ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
18236     ** and so on until a non-clashing name is found.  */
18237     int iTab = 0;
18238     char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
18239     sqlite3_stmt *pTest = 0;
18240     shellPrepare(pState->db, pRc,
18241         "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
18242     );
18243     if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18244     while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
18245       shellReset(pRc, pTest);
18246       sqlite3_free(zTab);
18247       zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
18248       sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18249     }
18250     shellFinalize(pRc, pTest);
18251
18252     pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
18253     if( pTab ){
18254       pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
18255       pTab->nCol = nCol;
18256       pTab->iPk = -2;
18257       if( nCol>0 ){
18258         pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
18259         if( pTab->azlCol ){
18260           pTab->azlCol[nCol] = shellMPrintf(pRc, "");
18261           for(i=nCol-1; i>=0; i--){
18262             pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
18263           }
18264         }
18265       }
18266
18267       if( *pRc!=SQLITE_OK ){
18268         recoverFreeTable(pTab);
18269         pTab = 0;
18270       }else{
18271         raw_printf(pState->out, 
18272             "CREATE TABLE %s(rootpgno INTEGER, "
18273             "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
18274         );
18275         for(i=0; i<nCol; i++){
18276           raw_printf(pState->out, ", c%d", i);
18277         }
18278         raw_printf(pState->out, ");\n");
18279       }
18280     }
18281     sqlite3_free(zTab);
18282   }
18283   return pTab;
18284 }
18285
18286 /*
18287 ** This function is called to recover data from the database. A script
18288 ** to construct a new database containing all recovered data is output
18289 ** on stream pState->out.
18290 */
18291 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
18292   int rc = SQLITE_OK;
18293   sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
18294   sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
18295   sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
18296   const char *zRecoveryDb = "";   /* Name of "recovery" database */
18297   const char *zLostAndFound = "lost_and_found";
18298   int i;
18299   int nOrphan = -1;
18300   RecoverTable *pOrphan = 0;
18301
18302   int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
18303   int bRowids = 1;                /* 0 if --no-rowids */
18304   for(i=1; i<nArg; i++){
18305     char *z = azArg[i];
18306     int n;
18307     if( z[0]=='-' && z[1]=='-' ) z++;
18308     n = strlen30(z);
18309     if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
18310       bFreelist = 0;
18311     }else
18312     if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
18313       i++;
18314       zRecoveryDb = azArg[i];
18315     }else
18316     if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
18317       i++;
18318       zLostAndFound = azArg[i];
18319     }else
18320     if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
18321       bRowids = 0;
18322     }
18323     else{
18324       utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 
18325       showHelp(pState->out, azArg[0]);
18326       return 1;
18327     }
18328   }
18329
18330   shellExecPrintf(pState->db, &rc,
18331     /* Attach an in-memory database named 'recovery'. Create an indexed 
18332     ** cache of the sqlite_dbptr virtual table. */
18333     "PRAGMA writable_schema = on;"
18334     "ATTACH %Q AS recovery;"
18335     "DROP TABLE IF EXISTS recovery.dbptr;"
18336     "DROP TABLE IF EXISTS recovery.freelist;"
18337     "DROP TABLE IF EXISTS recovery.map;"
18338     "DROP TABLE IF EXISTS recovery.schema;"
18339     "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
18340   );
18341
18342   if( bFreelist ){
18343     shellExec(pState->db, &rc,
18344       "WITH trunk(pgno) AS ("
18345       "  SELECT shell_int32("
18346       "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
18347       "      WHERE x>0"
18348       "    UNION"
18349       "  SELECT shell_int32("
18350       "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
18351       "      FROM trunk WHERE x>0"
18352       "),"
18353       "freelist(data, n, freepgno) AS ("
18354       "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
18355       "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
18356       "    UNION ALL"
18357       "  SELECT data, n-1, shell_int32(data, 2+n) "
18358       "      FROM freelist WHERE n>=0"
18359       ")"
18360       "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
18361     );
18362   }
18363
18364   /* If this is an auto-vacuum database, add all pointer-map pages to
18365   ** the freelist table. Do this regardless of whether or not 
18366   ** --freelist-corrupt was specified.  */
18367   shellExec(pState->db, &rc, 
18368     "WITH ptrmap(pgno) AS ("
18369     "  SELECT 2 WHERE shell_int32("
18370     "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
18371     "  )"
18372     "    UNION ALL "
18373     "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
18374     "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
18375     ")"
18376     "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
18377   );
18378
18379   shellExec(pState->db, &rc, 
18380     "CREATE TABLE recovery.dbptr("
18381     "      pgno, child, PRIMARY KEY(child, pgno)"
18382     ") WITHOUT ROWID;"
18383     "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
18384     "    SELECT * FROM sqlite_dbptr"
18385     "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
18386
18387     /* Delete any pointer to page 1. This ensures that page 1 is considered
18388     ** a root page, regardless of how corrupt the db is. */
18389     "DELETE FROM recovery.dbptr WHERE child = 1;"
18390
18391     /* Delete all pointers to any pages that have more than one pointer
18392     ** to them. Such pages will be treated as root pages when recovering
18393     ** data.  */
18394     "DELETE FROM recovery.dbptr WHERE child IN ("
18395     "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
18396     ");"
18397
18398     /* Create the "map" table that will (eventually) contain instructions
18399     ** for dealing with each page in the db that contains one or more 
18400     ** records. */
18401     "CREATE TABLE recovery.map("
18402       "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
18403     ");"
18404
18405     /* Populate table [map]. If there are circular loops of pages in the
18406     ** database, the following adds all pages in such a loop to the map
18407     ** as individual root pages. This could be handled better.  */
18408     "WITH pages(i, maxlen) AS ("
18409     "  SELECT page_count, ("
18410     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
18411     "  ) FROM pragma_page_count WHERE page_count>0"
18412     "    UNION ALL"
18413     "  SELECT i-1, ("
18414     "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
18415     "  ) FROM pages WHERE i>=2"
18416     ")"
18417     "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
18418     "  SELECT i, maxlen, NULL, ("
18419     "    WITH p(orig, pgno, parent) AS ("
18420     "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
18421     "        UNION "
18422     "      SELECT i, p.parent, "
18423     "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
18424     "    )"
18425     "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
18426     ") "
18427     "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
18428     "UPDATE recovery.map AS o SET intkey = ("
18429     "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
18430     ");"
18431
18432     /* Extract data from page 1 and any linked pages into table
18433     ** recovery.schema. With the same schema as an sqlite_schema table.  */
18434     "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
18435     "INSERT INTO recovery.schema SELECT "
18436     "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
18437     "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
18438     "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
18439     "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
18440     "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
18441     "FROM sqlite_dbdata WHERE pgno IN ("
18442     "  SELECT pgno FROM recovery.map WHERE root=1"
18443     ")"
18444     "GROUP BY pgno, cell;"
18445     "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
18446   );
18447
18448   /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 
18449   ** CREATE TABLE statements that extracted from the existing schema.  */
18450   if( rc==SQLITE_OK ){
18451     sqlite3_stmt *pStmt = 0;
18452     /* ".recover" might output content in an order which causes immediate
18453     ** foreign key constraints to be violated. So disable foreign-key
18454     ** constraint enforcement to prevent problems when running the output
18455     ** script. */
18456     raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
18457     raw_printf(pState->out, "BEGIN;\n");
18458     raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
18459     shellPrepare(pState->db, &rc,
18460         "SELECT sql FROM recovery.schema "
18461         "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
18462     );
18463     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18464       const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
18465       raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", 
18466           &zCreateTable[12]
18467       );
18468     }
18469     shellFinalize(&rc, pStmt);
18470   }
18471
18472   /* Figure out if an orphan table will be required. And if so, how many
18473   ** user columns it should contain */
18474   shellPrepare(pState->db, &rc, 
18475       "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
18476       , &pLoop
18477   );
18478   if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18479     nOrphan = sqlite3_column_int(pLoop, 0);
18480   }
18481   shellFinalize(&rc, pLoop);
18482   pLoop = 0;
18483
18484   shellPrepare(pState->db, &rc,
18485       "SELECT pgno FROM recovery.map WHERE root=?", &pPages
18486   );
18487
18488   shellPrepare(pState->db, &rc,
18489       "SELECT max(field), group_concat(shell_escape_crnl(quote"
18490       "(case when (? AND field<0) then NULL else value end)"
18491       "), ', ')"
18492       ", min(field) "
18493       "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
18494       "GROUP BY cell", &pCells
18495   );
18496
18497   /* Loop through each root page. */
18498   shellPrepare(pState->db, &rc, 
18499       "SELECT root, intkey, max(maxlen) FROM recovery.map" 
18500       " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
18501       "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
18502       ")", &pLoop
18503   );
18504   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18505     int iRoot = sqlite3_column_int(pLoop, 0);
18506     int bIntkey = sqlite3_column_int(pLoop, 1);
18507     int nCol = sqlite3_column_int(pLoop, 2);
18508     int bNoop = 0;
18509     RecoverTable *pTab;
18510
18511     assert( bIntkey==0 || bIntkey==1 );
18512     pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
18513     if( bNoop || rc ) continue;
18514     if( pTab==0 ){
18515       if( pOrphan==0 ){
18516         pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18517       }
18518       pTab = pOrphan;
18519       if( pTab==0 ) break;
18520     }
18521
18522     if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
18523       raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
18524     }
18525     sqlite3_bind_int(pPages, 1, iRoot);
18526     if( bRowids==0 && pTab->iPk<0 ){
18527       sqlite3_bind_int(pCells, 1, 1);
18528     }else{
18529       sqlite3_bind_int(pCells, 1, 0);
18530     }
18531     sqlite3_bind_int(pCells, 3, pTab->iPk);
18532
18533     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
18534       int iPgno = sqlite3_column_int(pPages, 0);
18535       sqlite3_bind_int(pCells, 2, iPgno);
18536       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
18537         int nField = sqlite3_column_int(pCells, 0);
18538         int iMin = sqlite3_column_int(pCells, 2);
18539         const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
18540
18541         RecoverTable *pTab2 = pTab;
18542         if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
18543           if( pOrphan==0 ){
18544             pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18545           }
18546           pTab2 = pOrphan;
18547           if( pTab2==0 ) break;
18548         }
18549
18550         nField = nField+1;
18551         if( pTab2==pOrphan ){
18552           raw_printf(pState->out, 
18553               "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
18554               pTab2->zQuoted, iRoot, iPgno, nField,
18555               iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
18556           );
18557         }else{
18558           raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 
18559               pTab2->zQuoted, pTab2->azlCol[nField], zVal
18560           );
18561         }
18562       }
18563       shellReset(&rc, pCells);
18564     }
18565     shellReset(&rc, pPages);
18566     if( pTab!=pOrphan ) recoverFreeTable(pTab);
18567   }
18568   shellFinalize(&rc, pLoop);
18569   shellFinalize(&rc, pPages);
18570   shellFinalize(&rc, pCells);
18571   recoverFreeTable(pOrphan);
18572
18573   /* The rest of the schema */
18574   if( rc==SQLITE_OK ){
18575     sqlite3_stmt *pStmt = 0;
18576     shellPrepare(pState->db, &rc, 
18577         "SELECT sql, name FROM recovery.schema "
18578         "WHERE sql NOT LIKE 'create table%'", &pStmt
18579     );
18580     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18581       const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
18582       if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
18583         const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
18584         char *zPrint = shellMPrintf(&rc, 
18585           "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
18586           zName, zName, zSql
18587         );
18588         raw_printf(pState->out, "%s;\n", zPrint);
18589         sqlite3_free(zPrint);
18590       }else{
18591         raw_printf(pState->out, "%s;\n", zSql);
18592       }
18593     }
18594     shellFinalize(&rc, pStmt);
18595   }
18596
18597   if( rc==SQLITE_OK ){
18598     raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
18599     raw_printf(pState->out, "COMMIT;\n");
18600   }
18601   sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
18602   return rc;
18603 }
18604 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18605
18606 /*
18607 ** If an input line begins with "." then invoke this routine to
18608 ** process that line.
18609 **
18610 ** Return 1 on error, 2 to exit, and 0 otherwise.
18611 */
18612 static int do_meta_command(char *zLine, ShellState *p){
18613   int h = 1;
18614   int nArg = 0;
18615   int n, c;
18616   int rc = 0;
18617   char *azArg[52];
18618
18619 #ifndef SQLITE_OMIT_VIRTUALTABLE
18620   if( p->expert.pExpert ){
18621     expertFinish(p, 1, 0);
18622   }
18623 #endif
18624
18625   /* Parse the input line into tokens.
18626   */
18627   while( zLine[h] && nArg<ArraySize(azArg)-1 ){
18628     while( IsSpace(zLine[h]) ){ h++; }
18629     if( zLine[h]==0 ) break;
18630     if( zLine[h]=='\'' || zLine[h]=='"' ){
18631       int delim = zLine[h++];
18632       azArg[nArg++] = &zLine[h];
18633       while( zLine[h] && zLine[h]!=delim ){
18634         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
18635         h++;
18636       }
18637       if( zLine[h]==delim ){
18638         zLine[h++] = 0;
18639       }
18640       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
18641     }else{
18642       azArg[nArg++] = &zLine[h];
18643       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
18644       if( zLine[h] ) zLine[h++] = 0;
18645       resolve_backslashes(azArg[nArg-1]);
18646     }
18647   }
18648   azArg[nArg] = 0;
18649
18650   /* Process the input line.
18651   */
18652   if( nArg==0 ) return 0; /* no tokens, no error */
18653   n = strlen30(azArg[0]);
18654   c = azArg[0][0];
18655   clearTempFile(p);
18656
18657 #ifndef SQLITE_OMIT_AUTHORIZATION
18658   if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
18659     if( nArg!=2 ){
18660       raw_printf(stderr, "Usage: .auth ON|OFF\n");
18661       rc = 1;
18662       goto meta_command_exit;
18663     }
18664     open_db(p, 0);
18665     if( booleanValue(azArg[1]) ){
18666       sqlite3_set_authorizer(p->db, shellAuth, p);
18667     }else if( p->bSafeModePersist ){
18668       sqlite3_set_authorizer(p->db, safeModeAuth, p);
18669     }else{
18670       sqlite3_set_authorizer(p->db, 0, 0);
18671     }
18672   }else
18673 #endif
18674
18675 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
18676   if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
18677     open_db(p, 0);
18678     failIfSafeMode(p, "cannot run .archive in safe mode");
18679     rc = arDotCommand(p, 0, azArg, nArg);
18680   }else
18681 #endif
18682
18683   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
18684    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
18685   ){
18686     const char *zDestFile = 0;
18687     const char *zDb = 0;
18688     sqlite3 *pDest;
18689     sqlite3_backup *pBackup;
18690     int j;
18691     int bAsync = 0;
18692     const char *zVfs = 0;
18693     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
18694     for(j=1; j<nArg; j++){
18695       const char *z = azArg[j];
18696       if( z[0]=='-' ){
18697         if( z[1]=='-' ) z++;
18698         if( strcmp(z, "-append")==0 ){
18699           zVfs = "apndvfs";
18700         }else
18701         if( strcmp(z, "-async")==0 ){
18702           bAsync = 1;
18703         }else
18704         {
18705           utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
18706           return 1;
18707         }
18708       }else if( zDestFile==0 ){
18709         zDestFile = azArg[j];
18710       }else if( zDb==0 ){
18711         zDb = zDestFile;
18712         zDestFile = azArg[j];
18713       }else{
18714         raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
18715         return 1;
18716       }
18717     }
18718     if( zDestFile==0 ){
18719       raw_printf(stderr, "missing FILENAME argument on .backup\n");
18720       return 1;
18721     }
18722     if( zDb==0 ) zDb = "main";
18723     rc = sqlite3_open_v2(zDestFile, &pDest, 
18724                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
18725     if( rc!=SQLITE_OK ){
18726       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
18727       close_db(pDest);
18728       return 1;
18729     }
18730     if( bAsync ){
18731       sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
18732                    0, 0, 0);
18733     }
18734     open_db(p, 0);
18735     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
18736     if( pBackup==0 ){
18737       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18738       close_db(pDest);
18739       return 1;
18740     }
18741     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
18742     sqlite3_backup_finish(pBackup);
18743     if( rc==SQLITE_DONE ){
18744       rc = 0;
18745     }else{
18746       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18747       rc = 1;
18748     }
18749     close_db(pDest);
18750   }else
18751
18752   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
18753     if( nArg==2 ){
18754       bail_on_error = booleanValue(azArg[1]);
18755     }else{
18756       raw_printf(stderr, "Usage: .bail on|off\n");
18757       rc = 1;
18758     }
18759   }else
18760
18761   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
18762     if( nArg==2 ){
18763       if( booleanValue(azArg[1]) ){
18764         setBinaryMode(p->out, 1);
18765       }else{
18766         setTextMode(p->out, 1);
18767       }
18768     }else{
18769       raw_printf(stderr, "Usage: .binary on|off\n");
18770       rc = 1;
18771     }
18772   }else
18773
18774   /* The undocumented ".breakpoint" command causes a call to the no-op
18775   ** routine named test_breakpoint().
18776   */
18777   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
18778     test_breakpoint();
18779   }else
18780
18781   if( c=='c' && strcmp(azArg[0],"cd")==0 ){
18782     failIfSafeMode(p, "cannot run .cd in safe mode");
18783     if( nArg==2 ){
18784 #if defined(_WIN32) || defined(WIN32)
18785       wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
18786       rc = !SetCurrentDirectoryW(z);
18787       sqlite3_free(z);
18788 #else
18789       rc = chdir(azArg[1]);
18790 #endif
18791       if( rc ){
18792         utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
18793         rc = 1;
18794       }
18795     }else{
18796       raw_printf(stderr, "Usage: .cd DIRECTORY\n");
18797       rc = 1;
18798     }
18799   }else
18800
18801   if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
18802     if( nArg==2 ){
18803       setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
18804     }else{
18805       raw_printf(stderr, "Usage: .changes on|off\n");
18806       rc = 1;
18807     }
18808   }else
18809
18810   /* Cancel output redirection, if it is currently set (by .testcase)
18811   ** Then read the content of the testcase-out.txt file and compare against
18812   ** azArg[1].  If there are differences, report an error and exit.
18813   */
18814   if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
18815     char *zRes = 0;
18816     output_reset(p);
18817     if( nArg!=2 ){
18818       raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
18819       rc = 2;
18820     }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
18821       raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
18822       rc = 2;
18823     }else if( testcase_glob(azArg[1],zRes)==0 ){
18824       utf8_printf(stderr,
18825                  "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
18826                  p->zTestcase, azArg[1], zRes);
18827       rc = 1;
18828     }else{
18829       utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
18830       p->nCheck++;
18831     }
18832     sqlite3_free(zRes);
18833   }else
18834
18835   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
18836     failIfSafeMode(p, "cannot run .clone in safe mode");
18837     if( nArg==2 ){
18838       tryToClone(p, azArg[1]);
18839     }else{
18840       raw_printf(stderr, "Usage: .clone FILENAME\n");
18841       rc = 1;
18842     }
18843   }else
18844
18845   if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
18846     if( nArg==1 ){
18847       /* List available connections */
18848       int i;
18849       for(i=0; i<ArraySize(p->aAuxDb); i++){
18850         const char *zFile = p->aAuxDb[i].zDbFilename;
18851         if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
18852           zFile = "(not open)";
18853         }else if( zFile==0 ){
18854           zFile = "(memory)";
18855         }else if( zFile[0]==0 ){
18856           zFile = "(temporary-file)";
18857         }
18858         if( p->pAuxDb == &p->aAuxDb[i] ){
18859           utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
18860         }else if( p->aAuxDb[i].db!=0 ){
18861           utf8_printf(stdout, "       %d: %s\n", i, zFile);
18862         }
18863       }
18864     }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
18865       int i = azArg[1][0] - '0';
18866       if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
18867         p->pAuxDb->db = p->db;
18868         p->pAuxDb = &p->aAuxDb[i];
18869         globalDb = p->db = p->pAuxDb->db;
18870         p->pAuxDb->db = 0;
18871       }
18872     }else if( nArg==3 && strcmp(azArg[1], "close")==0
18873            && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
18874       int i = azArg[2][0] - '0';
18875       if( i<0 || i>=ArraySize(p->aAuxDb) ){
18876         /* No-op */
18877       }else if( p->pAuxDb == &p->aAuxDb[i] ){
18878         raw_printf(stderr, "cannot close the active database connection\n");
18879         rc = 1;
18880       }else if( p->aAuxDb[i].db ){
18881         session_close_all(p, i);
18882         close_db(p->aAuxDb[i].db);
18883         p->aAuxDb[i].db = 0;
18884       }
18885     }else{
18886       raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
18887       rc = 1;
18888     }
18889   }else
18890
18891   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
18892     char **azName = 0;
18893     int nName = 0;
18894     sqlite3_stmt *pStmt;
18895     int i;
18896     open_db(p, 0);
18897     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
18898     if( rc ){
18899       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18900       rc = 1;
18901     }else{
18902       while( sqlite3_step(pStmt)==SQLITE_ROW ){
18903         const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
18904         const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
18905         azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
18906         if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
18907         azName[nName*2] = strdup(zSchema);
18908         azName[nName*2+1] = strdup(zFile);
18909         nName++;
18910       }
18911     }
18912     sqlite3_finalize(pStmt);
18913     for(i=0; i<nName; i++){
18914       int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
18915       int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
18916       const char *z = azName[i*2+1];
18917       utf8_printf(p->out, "%s: %s %s%s\n",
18918          azName[i*2],
18919          z && z[0] ? z : "\"\"",
18920          bRdonly ? "r/o" : "r/w",
18921          eTxn==SQLITE_TXN_NONE ? "" :
18922             eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
18923       free(azName[i*2]);
18924       free(azName[i*2+1]);
18925     }
18926     sqlite3_free(azName);
18927   }else
18928
18929   if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
18930     static const struct DbConfigChoices {
18931       const char *zName;
18932       int op;
18933     } aDbConfig[] = {
18934         { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
18935         { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
18936         { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
18937         { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
18938         { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
18939         { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
18940         { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
18941         { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
18942         { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
18943         { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
18944         { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
18945         { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
18946         { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
18947         { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
18948         { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
18949         { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
18950     };
18951     int ii, v;
18952     open_db(p, 0);
18953     for(ii=0; ii<ArraySize(aDbConfig); ii++){
18954       if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
18955       if( nArg>=3 ){
18956         sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
18957       }
18958       sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
18959       utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
18960       if( nArg>1 ) break;
18961     }
18962     if( nArg>1 && ii==ArraySize(aDbConfig) ){
18963       utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
18964       utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
18965     }   
18966   }else
18967
18968   if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
18969     rc = shell_dbinfo_command(p, nArg, azArg);
18970   }else
18971
18972 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18973   if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
18974     open_db(p, 0);
18975     rc = recoverDatabaseCmd(p, nArg, azArg);
18976   }else
18977 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18978
18979   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
18980     char *zLike = 0;
18981     char *zSql;
18982     int i;
18983     int savedShowHeader = p->showHeader;
18984     int savedShellFlags = p->shellFlgs;
18985     ShellClearFlag(p, 
18986        SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
18987        |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
18988     for(i=1; i<nArg; i++){
18989       if( azArg[i][0]=='-' ){
18990         const char *z = azArg[i]+1;
18991         if( z[0]=='-' ) z++;
18992         if( strcmp(z,"preserve-rowids")==0 ){
18993 #ifdef SQLITE_OMIT_VIRTUALTABLE
18994           raw_printf(stderr, "The --preserve-rowids option is not compatible"
18995                              " with SQLITE_OMIT_VIRTUALTABLE\n");
18996           rc = 1;
18997           sqlite3_free(zLike);
18998           goto meta_command_exit;
18999 #else
19000           ShellSetFlag(p, SHFLG_PreserveRowid);
19001 #endif
19002         }else
19003         if( strcmp(z,"newlines")==0 ){
19004           ShellSetFlag(p, SHFLG_Newlines);
19005         }else
19006         if( strcmp(z,"data-only")==0 ){
19007           ShellSetFlag(p, SHFLG_DumpDataOnly);
19008         }else
19009         if( strcmp(z,"nosys")==0 ){
19010           ShellSetFlag(p, SHFLG_DumpNoSys);
19011         }else
19012         {
19013           raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
19014           rc = 1;
19015           sqlite3_free(zLike);
19016           goto meta_command_exit;
19017         }
19018       }else{
19019         /* azArg[i] contains a LIKE pattern. This ".dump" request should
19020         ** only dump data for tables for which either the table name matches
19021         ** the LIKE pattern, or the table appears to be a shadow table of
19022         ** a virtual table for which the name matches the LIKE pattern.
19023         */
19024         char *zExpr = sqlite3_mprintf(
19025             "name LIKE %Q ESCAPE '\\' OR EXISTS ("
19026             "  SELECT 1 FROM sqlite_schema WHERE "
19027             "    name LIKE %Q ESCAPE '\\' AND"
19028             "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
19029             "    substr(o.name, 1, length(name)+1) == (name||'_')"
19030             ")", azArg[i], azArg[i]
19031         );
19032       
19033         if( zLike ){
19034           zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
19035         }else{
19036           zLike = zExpr;
19037         }
19038       }
19039     }
19040
19041     open_db(p, 0);
19042
19043     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19044       /* When playing back a "dump", the content might appear in an order
19045       ** which causes immediate foreign key constraints to be violated.
19046       ** So disable foreign-key constraint enforcement to prevent problems. */
19047       raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
19048       raw_printf(p->out, "BEGIN TRANSACTION;\n");
19049     }
19050     p->writableSchema = 0;
19051     p->showHeader = 0;
19052     /* Set writable_schema=ON since doing so forces SQLite to initialize
19053     ** as much of the schema as it can even if the sqlite_schema table is
19054     ** corrupt. */
19055     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
19056     p->nErr = 0;
19057     if( zLike==0 ) zLike = sqlite3_mprintf("true");
19058     zSql = sqlite3_mprintf(
19059       "SELECT name, type, sql FROM sqlite_schema AS o "
19060       "WHERE (%s) AND type=='table'"
19061       "  AND sql NOT NULL"
19062       " ORDER BY tbl_name='sqlite_sequence', rowid",
19063       zLike
19064     );
19065     run_schema_dump_query(p,zSql);
19066     sqlite3_free(zSql);
19067     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19068       zSql = sqlite3_mprintf(
19069         "SELECT sql FROM sqlite_schema AS o "
19070         "WHERE (%s) AND sql NOT NULL"
19071         "  AND type IN ('index','trigger','view')",
19072         zLike
19073       );
19074       run_table_dump_query(p, zSql);
19075       sqlite3_free(zSql);
19076     }
19077     sqlite3_free(zLike);
19078     if( p->writableSchema ){
19079       raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
19080       p->writableSchema = 0;
19081     }
19082     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
19083     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
19084     if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19085       raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
19086     }
19087     p->showHeader = savedShowHeader;
19088     p->shellFlgs = savedShellFlags;
19089   }else
19090
19091   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
19092     if( nArg==2 ){
19093       setOrClearFlag(p, SHFLG_Echo, azArg[1]);
19094     }else{
19095       raw_printf(stderr, "Usage: .echo on|off\n");
19096       rc = 1;
19097     }
19098   }else
19099
19100   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
19101     if( nArg==2 ){
19102       p->autoEQPtest = 0;
19103       if( p->autoEQPtrace ){
19104         if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
19105         p->autoEQPtrace = 0;
19106       }
19107       if( strcmp(azArg[1],"full")==0 ){
19108         p->autoEQP = AUTOEQP_full;
19109       }else if( strcmp(azArg[1],"trigger")==0 ){
19110         p->autoEQP = AUTOEQP_trigger;
19111 #ifdef SQLITE_DEBUG
19112       }else if( strcmp(azArg[1],"test")==0 ){
19113         p->autoEQP = AUTOEQP_on;
19114         p->autoEQPtest = 1;
19115       }else if( strcmp(azArg[1],"trace")==0 ){
19116         p->autoEQP = AUTOEQP_full;
19117         p->autoEQPtrace = 1;
19118         open_db(p, 0);
19119         sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
19120         sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
19121 #endif
19122       }else{
19123         p->autoEQP = (u8)booleanValue(azArg[1]);
19124       }
19125     }else{
19126       raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
19127       rc = 1;
19128     }
19129   }else
19130
19131   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19132     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19133     rc = 2;
19134   }else
19135
19136   /* The ".explain" command is automatic now.  It is largely pointless.  It
19137   ** retained purely for backwards compatibility */
19138   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
19139     int val = 1;
19140     if( nArg>=2 ){
19141       if( strcmp(azArg[1],"auto")==0 ){
19142         val = 99;
19143       }else{
19144         val =  booleanValue(azArg[1]);
19145       }
19146     }
19147     if( val==1 && p->mode!=MODE_Explain ){
19148       p->normalMode = p->mode;
19149       p->mode = MODE_Explain;
19150       p->autoExplain = 0;
19151     }else if( val==0 ){
19152       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
19153       p->autoExplain = 0;
19154     }else if( val==99 ){
19155       if( p->mode==MODE_Explain ) p->mode = p->normalMode;
19156       p->autoExplain = 1;
19157     }
19158   }else
19159
19160 #ifndef SQLITE_OMIT_VIRTUALTABLE
19161   if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19162     open_db(p, 0);
19163     expertDotCommand(p, azArg, nArg);
19164   }else
19165 #endif
19166
19167   if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
19168     static const struct {
19169        const char *zCtrlName;   /* Name of a test-control option */
19170        int ctrlCode;            /* Integer code for that option */
19171        const char *zUsage;      /* Usage notes */
19172     } aCtrl[] = {
19173       { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
19174       { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
19175       { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },  
19176       { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
19177       { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
19178    /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
19179       { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
19180       { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
19181       { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
19182       { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
19183    /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
19184     };
19185     int filectrl = -1;
19186     int iCtrl = -1;
19187     sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
19188     int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
19189     int n2, i;
19190     const char *zCmd = 0;
19191     const char *zSchema = 0;
19192
19193     open_db(p, 0);
19194     zCmd = nArg>=2 ? azArg[1] : "help";
19195
19196     if( zCmd[0]=='-' 
19197      && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
19198      && nArg>=4
19199     ){
19200       zSchema = azArg[2];
19201       for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
19202       nArg -= 2;
19203       zCmd = azArg[1];
19204     }
19205
19206     /* The argument can optionally begin with "-" or "--" */
19207     if( zCmd[0]=='-' && zCmd[1] ){
19208       zCmd++;
19209       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
19210     }
19211
19212     /* --help lists all file-controls */
19213     if( strcmp(zCmd,"help")==0 ){
19214       utf8_printf(p->out, "Available file-controls:\n");
19215       for(i=0; i<ArraySize(aCtrl); i++){
19216         utf8_printf(p->out, "  .filectrl %s %s\n",
19217                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
19218       }
19219       rc = 1;
19220       goto meta_command_exit;
19221     }
19222
19223     /* convert filectrl text option to value. allow any unique prefix
19224     ** of the option name, or a numerical value. */
19225     n2 = strlen30(zCmd);
19226     for(i=0; i<ArraySize(aCtrl); i++){
19227       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
19228         if( filectrl<0 ){
19229           filectrl = aCtrl[i].ctrlCode;
19230           iCtrl = i;
19231         }else{
19232           utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
19233                               "Use \".filectrl --help\" for help\n", zCmd);
19234           rc = 1;
19235           goto meta_command_exit;
19236         }
19237       }
19238     }
19239     if( filectrl<0 ){
19240       utf8_printf(stderr,"Error: unknown file-control: %s\n"
19241                          "Use \".filectrl --help\" for help\n", zCmd);
19242     }else{
19243       switch(filectrl){
19244         case SQLITE_FCNTL_SIZE_LIMIT: {
19245           if( nArg!=2 && nArg!=3 ) break;
19246           iRes = nArg==3 ? integerValue(azArg[2]) : -1;
19247           sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
19248           isOk = 1;
19249           break;
19250         }
19251         case SQLITE_FCNTL_LOCK_TIMEOUT:
19252         case SQLITE_FCNTL_CHUNK_SIZE: {
19253           int x;
19254           if( nArg!=3 ) break;
19255           x = (int)integerValue(azArg[2]);
19256           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19257           isOk = 2;
19258           break;
19259         }
19260         case SQLITE_FCNTL_PERSIST_WAL:
19261         case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
19262           int x;
19263           if( nArg!=2 && nArg!=3 ) break;
19264           x = nArg==3 ? booleanValue(azArg[2]) : -1;
19265           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19266           iRes = x;
19267           isOk = 1;
19268           break;
19269         }
19270         case SQLITE_FCNTL_DATA_VERSION:
19271         case SQLITE_FCNTL_HAS_MOVED: {
19272           int x;
19273           if( nArg!=2 ) break;
19274           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19275           iRes = x;
19276           isOk = 1;
19277           break;
19278         }
19279         case SQLITE_FCNTL_TEMPFILENAME: {
19280           char *z = 0;
19281           if( nArg!=2 ) break;
19282           sqlite3_file_control(p->db, zSchema, filectrl, &z);
19283           if( z ){
19284             utf8_printf(p->out, "%s\n", z);
19285             sqlite3_free(z);
19286           }
19287           isOk = 2;
19288           break;
19289         }
19290         case SQLITE_FCNTL_RESERVE_BYTES: {
19291           int x;
19292           if( nArg>=3 ){
19293             x = atoi(azArg[2]);
19294             sqlite3_file_control(p->db, zSchema, filectrl, &x);
19295           }
19296           x = -1;
19297           sqlite3_file_control(p->db, zSchema, filectrl, &x);
19298           utf8_printf(p->out,"%d\n", x);
19299           isOk = 2;
19300           break;
19301         }
19302       }
19303     }
19304     if( isOk==0 && iCtrl>=0 ){
19305       utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
19306       rc = 1;
19307     }else if( isOk==1 ){
19308       char zBuf[100];
19309       sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
19310       raw_printf(p->out, "%s\n", zBuf);
19311     }
19312   }else
19313
19314   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
19315     ShellState data;
19316     int doStats = 0;
19317     memcpy(&data, p, sizeof(data));
19318     data.showHeader = 0;
19319     data.cMode = data.mode = MODE_Semi;
19320     if( nArg==2 && optionMatch(azArg[1], "indent") ){
19321       data.cMode = data.mode = MODE_Pretty;
19322       nArg = 1;
19323     }
19324     if( nArg!=1 ){
19325       raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
19326       rc = 1;
19327       goto meta_command_exit;
19328     }
19329     open_db(p, 0);
19330     rc = sqlite3_exec(p->db,
19331        "SELECT sql FROM"
19332        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
19333        "     FROM sqlite_schema UNION ALL"
19334        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
19335        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
19336        "ORDER BY x",
19337        callback, &data, 0
19338     );
19339     if( rc==SQLITE_OK ){
19340       sqlite3_stmt *pStmt;
19341       rc = sqlite3_prepare_v2(p->db,
19342                "SELECT rowid FROM sqlite_schema"
19343                " WHERE name GLOB 'sqlite_stat[134]'",
19344                -1, &pStmt, 0);
19345       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
19346       sqlite3_finalize(pStmt);
19347     }
19348     if( doStats==0 ){
19349       raw_printf(p->out, "/* No STAT tables available */\n");
19350     }else{
19351       raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19352       data.cMode = data.mode = MODE_Insert;
19353       data.zDestTable = "sqlite_stat1";
19354       shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
19355       data.zDestTable = "sqlite_stat4";
19356       shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
19357       raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19358     }
19359   }else
19360
19361   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
19362     if( nArg==2 ){
19363       p->showHeader = booleanValue(azArg[1]);
19364       p->shellFlgs |= SHFLG_HeaderSet;
19365     }else{
19366       raw_printf(stderr, "Usage: .headers on|off\n");
19367       rc = 1;
19368     }
19369   }else
19370
19371   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
19372     if( nArg>=2 ){
19373       n = showHelp(p->out, azArg[1]);
19374       if( n==0 ){
19375         utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
19376       }
19377     }else{
19378       showHelp(p->out, 0);
19379     }
19380   }else
19381
19382   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
19383     char *zTable = 0;           /* Insert data into this table */
19384     char *zFile = 0;            /* Name of file to extra content from */
19385     sqlite3_stmt *pStmt = NULL; /* A statement */
19386     int nCol;                   /* Number of columns in the table */
19387     int nByte;                  /* Number of bytes in an SQL string */
19388     int i, j;                   /* Loop counters */
19389     int needCommit;             /* True to COMMIT or ROLLBACK at end */
19390     int nSep;                   /* Number of bytes in p->colSeparator[] */
19391     char *zSql;                 /* An SQL statement */
19392     ImportCtx sCtx;             /* Reader context */
19393     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
19394     int eVerbose = 0;           /* Larger for more console output */
19395     int nSkip = 0;              /* Initial lines to skip */
19396     int useOutputMode = 1;      /* Use output mode to determine separators */
19397
19398     failIfSafeMode(p, "cannot run .import in safe mode");
19399     memset(&sCtx, 0, sizeof(sCtx));
19400     sCtx.z = sqlite3_malloc64(120);
19401     if( sCtx.z==0 ){
19402       import_cleanup(&sCtx);
19403       shell_out_of_memory();
19404     }
19405     if( p->mode==MODE_Ascii ){
19406       xRead = ascii_read_one_field;
19407     }else{
19408       xRead = csv_read_one_field;
19409     }
19410     for(i=1; i<nArg; i++){
19411       char *z = azArg[i];
19412       if( z[0]=='-' && z[1]=='-' ) z++;
19413       if( z[0]!='-' ){
19414         if( zFile==0 ){
19415           zFile = z;
19416         }else if( zTable==0 ){
19417           zTable = z;
19418         }else{
19419           utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
19420           showHelp(p->out, "import");
19421           rc = 1;
19422           goto meta_command_exit;
19423         }
19424       }else if( strcmp(z,"-v")==0 ){
19425         eVerbose++;
19426       }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
19427         nSkip = integerValue(azArg[++i]);
19428       }else if( strcmp(z,"-ascii")==0 ){
19429         sCtx.cColSep = SEP_Unit[0];
19430         sCtx.cRowSep = SEP_Record[0];
19431         xRead = ascii_read_one_field;
19432         useOutputMode = 0;
19433       }else if( strcmp(z,"-csv")==0 ){
19434         sCtx.cColSep = ',';
19435         sCtx.cRowSep = '\n';
19436         xRead = csv_read_one_field;
19437         useOutputMode = 0;
19438       }else{
19439         utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
19440         showHelp(p->out, "import");
19441         rc = 1;
19442         goto meta_command_exit;
19443       }
19444     }
19445     if( zTable==0 ){
19446       utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
19447                   zFile==0 ? "FILE" : "TABLE");
19448       showHelp(p->out, "import");
19449       rc = 1;
19450       goto meta_command_exit;
19451     }
19452     seenInterrupt = 0;
19453     open_db(p, 0);
19454     if( useOutputMode ){
19455       /* If neither the --csv or --ascii options are specified, then set
19456       ** the column and row separator characters from the output mode. */
19457       nSep = strlen30(p->colSeparator);
19458       if( nSep==0 ){
19459         raw_printf(stderr,
19460                    "Error: non-null column separator required for import\n");
19461         rc = 1;
19462         goto meta_command_exit;
19463       }
19464       if( nSep>1 ){
19465         raw_printf(stderr, 
19466               "Error: multi-character column separators not allowed"
19467               " for import\n");
19468         rc = 1;
19469         goto meta_command_exit;
19470       }
19471       nSep = strlen30(p->rowSeparator);
19472       if( nSep==0 ){
19473         raw_printf(stderr,
19474             "Error: non-null row separator required for import\n");
19475         rc = 1;
19476         goto meta_command_exit;
19477       }
19478       if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
19479         /* When importing CSV (only), if the row separator is set to the
19480         ** default output row separator, change it to the default input
19481         ** row separator.  This avoids having to maintain different input
19482         ** and output row separators. */
19483         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19484         nSep = strlen30(p->rowSeparator);
19485       }
19486       if( nSep>1 ){
19487         raw_printf(stderr, "Error: multi-character row separators not allowed"
19488                            " for import\n");
19489         rc = 1;
19490         goto meta_command_exit;
19491       }
19492       sCtx.cColSep = p->colSeparator[0];
19493       sCtx.cRowSep = p->rowSeparator[0];
19494     }
19495     sCtx.zFile = zFile;
19496     sCtx.nLine = 1;
19497     if( sCtx.zFile[0]=='|' ){
19498 #ifdef SQLITE_OMIT_POPEN
19499       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19500       rc = 1;
19501       goto meta_command_exit;
19502 #else
19503       sCtx.in = popen(sCtx.zFile+1, "r");
19504       sCtx.zFile = "<pipe>";
19505       sCtx.xCloser = pclose;
19506 #endif
19507     }else{
19508       sCtx.in = fopen(sCtx.zFile, "rb");
19509       sCtx.xCloser = fclose;
19510     }
19511     if( sCtx.in==0 ){
19512       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
19513       rc = 1;
19514       import_cleanup(&sCtx);
19515       goto meta_command_exit;
19516     }
19517     if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
19518       char zSep[2];
19519       zSep[1] = 0;
19520       zSep[0] = sCtx.cColSep;
19521       utf8_printf(p->out, "Column separator ");
19522       output_c_string(p->out, zSep);
19523       utf8_printf(p->out, ", row separator ");
19524       zSep[0] = sCtx.cRowSep;
19525       output_c_string(p->out, zSep);
19526       utf8_printf(p->out, "\n");
19527     }
19528     while( (nSkip--)>0 ){
19529       while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
19530     }
19531     zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
19532     if( zSql==0 ){
19533       import_cleanup(&sCtx);
19534       shell_out_of_memory();
19535     }
19536     nByte = strlen30(zSql);
19537     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19538     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
19539     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
19540       char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
19541       char cSep = '(';
19542       while( xRead(&sCtx) ){
19543         zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
19544         cSep = ',';
19545         if( sCtx.cTerm!=sCtx.cColSep ) break;
19546       }
19547       if( cSep=='(' ){
19548         sqlite3_free(zCreate);
19549         import_cleanup(&sCtx);
19550         utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
19551         rc = 1;
19552         goto meta_command_exit;
19553       }
19554       zCreate = sqlite3_mprintf("%z\n)", zCreate);
19555       if( eVerbose>=1 ){
19556         utf8_printf(p->out, "%s\n", zCreate);
19557       }
19558       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
19559       sqlite3_free(zCreate);
19560       if( rc ){
19561         utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
19562                 sqlite3_errmsg(p->db));
19563         import_cleanup(&sCtx);
19564         rc = 1;
19565         goto meta_command_exit;
19566       }
19567       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19568     }
19569     sqlite3_free(zSql);
19570     if( rc ){
19571       if (pStmt) sqlite3_finalize(pStmt);
19572       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
19573       import_cleanup(&sCtx);
19574       rc = 1;
19575       goto meta_command_exit;
19576     }
19577     nCol = sqlite3_column_count(pStmt);
19578     sqlite3_finalize(pStmt);
19579     pStmt = 0;
19580     if( nCol==0 ) return 0; /* no columns, no error */
19581     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
19582     if( zSql==0 ){
19583       import_cleanup(&sCtx);
19584       shell_out_of_memory();
19585     }
19586     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
19587     j = strlen30(zSql);
19588     for(i=1; i<nCol; i++){
19589       zSql[j++] = ',';
19590       zSql[j++] = '?';
19591     }
19592     zSql[j++] = ')';
19593     zSql[j] = 0;
19594     if( eVerbose>=2 ){
19595       utf8_printf(p->out, "Insert using: %s\n", zSql);
19596     }
19597     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19598     sqlite3_free(zSql);
19599     if( rc ){
19600       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19601       if (pStmt) sqlite3_finalize(pStmt);
19602       import_cleanup(&sCtx);
19603       rc = 1;
19604       goto meta_command_exit;
19605     }
19606     needCommit = sqlite3_get_autocommit(p->db);
19607     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
19608     do{
19609       int startLine = sCtx.nLine;
19610       for(i=0; i<nCol; i++){
19611         char *z = xRead(&sCtx);
19612         /*
19613         ** Did we reach end-of-file before finding any columns?
19614         ** If so, stop instead of NULL filling the remaining columns.
19615         */
19616         if( z==0 && i==0 ) break;
19617         /*
19618         ** Did we reach end-of-file OR end-of-line before finding any
19619         ** columns in ASCII mode?  If so, stop instead of NULL filling
19620         ** the remaining columns.
19621         */
19622         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
19623         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
19624         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
19625           utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19626                           "filling the rest with NULL\n",
19627                           sCtx.zFile, startLine, nCol, i+1);
19628           i += 2;
19629           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
19630         }
19631       }
19632       if( sCtx.cTerm==sCtx.cColSep ){
19633         do{
19634           xRead(&sCtx);
19635           i++;
19636         }while( sCtx.cTerm==sCtx.cColSep );
19637         utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19638                         "extras ignored\n",
19639                         sCtx.zFile, startLine, nCol, i);
19640       }
19641       if( i>=nCol ){
19642         sqlite3_step(pStmt);
19643         rc = sqlite3_reset(pStmt);
19644         if( rc!=SQLITE_OK ){
19645           utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
19646                       startLine, sqlite3_errmsg(p->db));
19647           sCtx.nErr++;
19648         }else{
19649           sCtx.nRow++;
19650         }
19651       }
19652     }while( sCtx.cTerm!=EOF );
19653
19654     import_cleanup(&sCtx);
19655     sqlite3_finalize(pStmt);
19656     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
19657     if( eVerbose>0 ){
19658       utf8_printf(p->out,
19659           "Added %d rows with %d errors using %d lines of input\n",
19660           sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
19661     }
19662   }else
19663
19664 #ifndef SQLITE_UNTESTABLE
19665   if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
19666     char *zSql;
19667     char *zCollist = 0;
19668     sqlite3_stmt *pStmt;
19669     int tnum = 0;
19670     int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
19671     int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
19672     int i;
19673     if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
19674       utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
19675                           "       .imposter off\n");
19676       /* Also allowed, but not documented:
19677       **
19678       **    .imposter TABLE IMPOSTER
19679       **
19680       ** where TABLE is a WITHOUT ROWID table.  In that case, the
19681       ** imposter is another WITHOUT ROWID table with the columns in
19682       ** storage order. */
19683       rc = 1;
19684       goto meta_command_exit;
19685     }
19686     open_db(p, 0);
19687     if( nArg==2 ){
19688       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
19689       goto meta_command_exit;
19690     }
19691     zSql = sqlite3_mprintf(
19692       "SELECT rootpage, 0 FROM sqlite_schema"
19693       " WHERE name='%q' AND type='index'"
19694       "UNION ALL "
19695       "SELECT rootpage, 1 FROM sqlite_schema"
19696       " WHERE name='%q' AND type='table'"
19697       "   AND sql LIKE '%%without%%rowid%%'",
19698       azArg[1], azArg[1]
19699     );
19700     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19701     sqlite3_free(zSql);
19702     if( sqlite3_step(pStmt)==SQLITE_ROW ){
19703       tnum = sqlite3_column_int(pStmt, 0);
19704       isWO = sqlite3_column_int(pStmt, 1);
19705     }
19706     sqlite3_finalize(pStmt);
19707     zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
19708     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19709     sqlite3_free(zSql);
19710     i = 0;
19711     while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
19712       char zLabel[20];
19713       const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
19714       i++;
19715       if( zCol==0 ){
19716         if( sqlite3_column_int(pStmt,1)==-1 ){
19717           zCol = "_ROWID_";
19718         }else{
19719           sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
19720           zCol = zLabel;
19721         }
19722       }
19723       if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
19724         lenPK = (int)strlen(zCollist);
19725       }
19726       if( zCollist==0 ){
19727         zCollist = sqlite3_mprintf("\"%w\"", zCol);
19728       }else{
19729         zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
19730       }
19731     }
19732     sqlite3_finalize(pStmt);
19733     if( i==0 || tnum==0 ){
19734       utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
19735       rc = 1;
19736       sqlite3_free(zCollist);
19737       goto meta_command_exit;
19738     }
19739     if( lenPK==0 ) lenPK = 100000;
19740     zSql = sqlite3_mprintf(
19741           "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
19742           azArg[2], zCollist, lenPK, zCollist);
19743     sqlite3_free(zCollist);
19744     rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
19745     if( rc==SQLITE_OK ){
19746       rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
19747       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
19748       if( rc ){
19749         utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
19750       }else{
19751         utf8_printf(stdout, "%s;\n", zSql);
19752         raw_printf(stdout,
19753           "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
19754           azArg[1], isWO ? "table" : "index"
19755         );
19756       }
19757     }else{
19758       raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
19759       rc = 1;
19760     }
19761     sqlite3_free(zSql);
19762   }else
19763 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
19764
19765 #ifdef SQLITE_ENABLE_IOTRACE
19766   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
19767     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
19768     if( iotrace && iotrace!=stdout ) fclose(iotrace);
19769     iotrace = 0;
19770     if( nArg<2 ){
19771       sqlite3IoTrace = 0;
19772     }else if( strcmp(azArg[1], "-")==0 ){
19773       sqlite3IoTrace = iotracePrintf;
19774       iotrace = stdout;
19775     }else{
19776       iotrace = fopen(azArg[1], "w");
19777       if( iotrace==0 ){
19778         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19779         sqlite3IoTrace = 0;
19780         rc = 1;
19781       }else{
19782         sqlite3IoTrace = iotracePrintf;
19783       }
19784     }
19785   }else
19786 #endif
19787
19788   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
19789     static const struct {
19790        const char *zLimitName;   /* Name of a limit */
19791        int limitCode;            /* Integer code for that limit */
19792     } aLimit[] = {
19793       { "length",                SQLITE_LIMIT_LENGTH                    },
19794       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
19795       { "column",                SQLITE_LIMIT_COLUMN                    },
19796       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
19797       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
19798       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
19799       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
19800       { "attached",              SQLITE_LIMIT_ATTACHED                  },
19801       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
19802       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
19803       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
19804       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
19805     };
19806     int i, n2;
19807     open_db(p, 0);
19808     if( nArg==1 ){
19809       for(i=0; i<ArraySize(aLimit); i++){
19810         printf("%20s %d\n", aLimit[i].zLimitName,
19811                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
19812       }
19813     }else if( nArg>3 ){
19814       raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
19815       rc = 1;
19816       goto meta_command_exit;
19817     }else{
19818       int iLimit = -1;
19819       n2 = strlen30(azArg[1]);
19820       for(i=0; i<ArraySize(aLimit); i++){
19821         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
19822           if( iLimit<0 ){
19823             iLimit = i;
19824           }else{
19825             utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
19826             rc = 1;
19827             goto meta_command_exit;
19828           }
19829         }
19830       }
19831       if( iLimit<0 ){
19832         utf8_printf(stderr, "unknown limit: \"%s\"\n"
19833                         "enter \".limits\" with no arguments for a list.\n",
19834                          azArg[1]);
19835         rc = 1;
19836         goto meta_command_exit;
19837       }
19838       if( nArg==3 ){
19839         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
19840                       (int)integerValue(azArg[2]));
19841       }
19842       printf("%20s %d\n", aLimit[iLimit].zLimitName,
19843              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
19844     }
19845   }else
19846
19847   if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
19848     open_db(p, 0);
19849     lintDotCommand(p, azArg, nArg);
19850   }else
19851
19852 #ifndef SQLITE_OMIT_LOAD_EXTENSION
19853   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
19854     const char *zFile, *zProc;
19855     char *zErrMsg = 0;
19856     failIfSafeMode(p, "cannot run .load in safe mode");
19857     if( nArg<2 ){
19858       raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
19859       rc = 1;
19860       goto meta_command_exit;
19861     }
19862     zFile = azArg[1];
19863     zProc = nArg>=3 ? azArg[2] : 0;
19864     open_db(p, 0);
19865     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
19866     if( rc!=SQLITE_OK ){
19867       utf8_printf(stderr, "Error: %s\n", zErrMsg);
19868       sqlite3_free(zErrMsg);
19869       rc = 1;
19870     }
19871   }else
19872 #endif
19873
19874   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
19875     failIfSafeMode(p, "cannot run .log in safe mode");
19876     if( nArg!=2 ){
19877       raw_printf(stderr, "Usage: .log FILENAME\n");
19878       rc = 1;
19879     }else{
19880       const char *zFile = azArg[1];
19881       output_file_close(p->pLog);
19882       p->pLog = output_file_open(zFile, 0);
19883     }
19884   }else
19885
19886   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
19887     const char *zMode = nArg>=2 ? azArg[1] : "";
19888     int n2 = strlen30(zMode);
19889     int c2 = zMode[0];
19890     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
19891       p->mode = MODE_Line;
19892       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19893     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
19894       p->mode = MODE_Column;
19895       if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
19896         p->showHeader = 1;
19897       }
19898       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19899     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
19900       p->mode = MODE_List;
19901       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
19902       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19903     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
19904       p->mode = MODE_Html;
19905     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
19906       p->mode = MODE_Tcl;
19907       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
19908       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19909     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
19910       p->mode = MODE_Csv;
19911       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19912       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
19913     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
19914       p->mode = MODE_List;
19915       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
19916     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
19917       p->mode = MODE_Insert;
19918       set_table_name(p, nArg>=3 ? azArg[2] : "table");
19919     }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
19920       p->mode = MODE_Quote;
19921       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19922       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19923     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
19924       p->mode = MODE_Ascii;
19925       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
19926       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
19927     }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
19928       p->mode = MODE_Markdown;
19929     }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
19930       p->mode = MODE_Table;
19931     }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
19932       p->mode = MODE_Box;
19933     }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
19934       p->mode = MODE_Json;
19935     }else if( nArg==1 ){
19936       raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
19937     }else{
19938       raw_printf(stderr, "Error: mode should be one of: "
19939          "ascii box column csv html insert json line list markdown "
19940          "quote table tabs tcl\n");
19941       rc = 1;
19942     }
19943     p->cMode = p->mode;
19944   }else
19945
19946   if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
19947     if( nArg!=2 ){
19948       raw_printf(stderr, "Usage: .nonce NONCE\n");
19949       rc = 1;
19950     }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
19951       raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]);
19952       exit(1);
19953     }else{
19954       p->bSafeMode = 0;
19955       return 0;  /* Return immediately to bypass the safe mode reset
19956                  ** at the end of this procedure */
19957     }
19958   }else
19959
19960   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
19961     if( nArg==2 ){
19962       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
19963                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
19964     }else{
19965       raw_printf(stderr, "Usage: .nullvalue STRING\n");
19966       rc = 1;
19967     }
19968   }else
19969
19970 #ifdef SQLITE_DEBUG
19971   if( c=='o' && strcmp(azArg[0],"oom")==0 ){
19972     int i;
19973     for(i=1; i<nArg; i++){
19974       const char *z = azArg[i];
19975       if( z[0]=='-' && z[1]=='-' ) z++;
19976       if( strcmp(z,"-repeat")==0 ){
19977         if( i==nArg-1 ){
19978           raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
19979           rc = 1;
19980         }else{
19981           oomRepeat = (int)integerValue(azArg[++i]);
19982         }
19983       }else if( IsDigit(z[0]) ){
19984         oomCounter = (int)integerValue(azArg[i]);
19985       }else{
19986         raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
19987         raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
19988         rc = 1;
19989       }
19990     }
19991     if( rc==0 ){
19992       raw_printf(p->out, "oomCounter = %d\n", oomCounter);
19993       raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
19994     }
19995   }else
19996 #endif /* SQLITE_DEBUG */
19997
19998   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
19999     char *zNewFilename = 0;  /* Name of the database file to open */
20000     int iName = 1;           /* Index in azArg[] of the filename */
20001     int newFlag = 0;         /* True to delete file before opening */
20002     /* Close the existing database */
20003     session_close_all(p, -1);
20004     close_db(p->db);
20005     p->db = 0;
20006     p->pAuxDb->zDbFilename = 0;
20007     sqlite3_free(p->pAuxDb->zFreeOnClose);
20008     p->pAuxDb->zFreeOnClose = 0;
20009     p->openMode = SHELL_OPEN_UNSPEC;
20010     p->openFlags = 0;
20011     p->szMax = 0;
20012     /* Check for command-line arguments */
20013     for(iName=1; iName<nArg; iName++){
20014       const char *z = azArg[iName];
20015       if( optionMatch(z,"new") ){
20016         newFlag = 1;
20017 #ifdef SQLITE_HAVE_ZLIB
20018       }else if( optionMatch(z, "zip") ){
20019         p->openMode = SHELL_OPEN_ZIPFILE;
20020 #endif
20021       }else if( optionMatch(z, "append") ){
20022         p->openMode = SHELL_OPEN_APPENDVFS;
20023       }else if( optionMatch(z, "readonly") ){
20024         p->openMode = SHELL_OPEN_READONLY;
20025       }else if( optionMatch(z, "nofollow") ){
20026         p->openFlags |= SQLITE_OPEN_NOFOLLOW;
20027 #ifndef SQLITE_OMIT_DESERIALIZE
20028       }else if( optionMatch(z, "deserialize") ){
20029         p->openMode = SHELL_OPEN_DESERIALIZE;
20030       }else if( optionMatch(z, "hexdb") ){
20031         p->openMode = SHELL_OPEN_HEXDB;
20032       }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
20033         p->szMax = integerValue(azArg[++iName]);
20034 #endif /* SQLITE_OMIT_DESERIALIZE */
20035       }else if( z[0]=='-' ){
20036         utf8_printf(stderr, "unknown option: %s\n", z);
20037         rc = 1;
20038         goto meta_command_exit;
20039       }else if( zNewFilename ){
20040         utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20041         rc = 1;
20042         goto meta_command_exit;
20043       }else{
20044         zNewFilename = sqlite3_mprintf("%s", z);
20045       }
20046     }
20047     /* If a filename is specified, try to open it first */
20048     if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
20049       if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename);
20050       if( p->bSafeMode
20051        && p->openMode!=SHELL_OPEN_HEXDB
20052        && zNewFilename
20053        && strcmp(zNewFilename,":memory:")!=0
20054       ){
20055         failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20056       }
20057       p->pAuxDb->zDbFilename = zNewFilename;
20058       open_db(p, OPEN_DB_KEEPALIVE);
20059       if( p->db==0 ){
20060         utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
20061         sqlite3_free(zNewFilename);
20062       }else{
20063         p->pAuxDb->zFreeOnClose = zNewFilename;
20064       }
20065     }
20066     if( p->db==0 ){
20067       /* As a fall-back open a TEMP database */
20068       p->pAuxDb->zDbFilename = 0;
20069       open_db(p, 0);
20070     }
20071   }else
20072
20073   if( (c=='o'
20074         && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
20075    || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
20076   ){
20077     char *zFile = 0;
20078     int bTxtMode = 0;
20079     int i;
20080     int eMode = 0;
20081     int bBOM = 0;
20082     int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
20083
20084     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
20085     if( c=='e' ){
20086       eMode = 'x';
20087       bOnce = 2;
20088     }else if( strncmp(azArg[0],"once",n)==0 ){
20089       bOnce = 1;
20090     }
20091     for(i=1; i<nArg; i++){
20092       char *z = azArg[i];
20093       if( z[0]=='-' ){
20094         if( z[1]=='-' ) z++;
20095         if( strcmp(z,"-bom")==0 ){
20096           bBOM = 1;
20097         }else if( c!='e' && strcmp(z,"-x")==0 ){
20098           eMode = 'x';  /* spreadsheet */
20099         }else if( c!='e' && strcmp(z,"-e")==0 ){
20100           eMode = 'e';  /* text editor */
20101         }else{
20102           utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
20103                       azArg[i]);
20104           showHelp(p->out, azArg[0]);
20105           rc = 1;
20106           goto meta_command_exit;
20107         }
20108       }else if( zFile==0 && eMode!='e' && eMode!='x' ){
20109         zFile = sqlite3_mprintf("%s", z);
20110         if( zFile[0]=='|' ){
20111           while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
20112           break;
20113         }
20114       }else{
20115         utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
20116                     azArg[i]);
20117         showHelp(p->out, azArg[0]);
20118         rc = 1;
20119         sqlite3_free(zFile);
20120         goto meta_command_exit;
20121       }
20122     }
20123     if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
20124     if( bOnce ){
20125       p->outCount = 2;
20126     }else{
20127       p->outCount = 0;
20128     }
20129     output_reset(p);
20130 #ifndef SQLITE_NOHAVE_SYSTEM
20131     if( eMode=='e' || eMode=='x' ){
20132       p->doXdgOpen = 1;
20133       outputModePush(p);
20134       if( eMode=='x' ){
20135         /* spreadsheet mode.  Output as CSV. */
20136         newTempFile(p, "csv");
20137         ShellClearFlag(p, SHFLG_Echo);
20138         p->mode = MODE_Csv;
20139         sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20140         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20141       }else{
20142         /* text editor mode */
20143         newTempFile(p, "txt");
20144         bTxtMode = 1;
20145       }
20146       sqlite3_free(zFile);
20147       zFile = sqlite3_mprintf("%s", p->zTempFile);
20148     }
20149 #endif /* SQLITE_NOHAVE_SYSTEM */
20150     if( zFile[0]=='|' ){
20151 #ifdef SQLITE_OMIT_POPEN
20152       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20153       rc = 1;
20154       p->out = stdout;
20155 #else
20156       p->out = popen(zFile + 1, "w");
20157       if( p->out==0 ){
20158         utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
20159         p->out = stdout;
20160         rc = 1;
20161       }else{
20162         if( bBOM ) fprintf(p->out,"\357\273\277");
20163         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20164       }
20165 #endif
20166     }else{
20167       p->out = output_file_open(zFile, bTxtMode);
20168       if( p->out==0 ){
20169         if( strcmp(zFile,"off")!=0 ){
20170           utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
20171         }
20172         p->out = stdout;
20173         rc = 1;
20174       } else {
20175         if( bBOM ) fprintf(p->out,"\357\273\277");
20176         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20177       }
20178     }
20179     sqlite3_free(zFile);
20180   }else
20181
20182   if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
20183     open_db(p,0);
20184     if( nArg<=1 ) goto parameter_syntax_error;
20185
20186     /* .parameter clear
20187     ** Clear all bind parameters by dropping the TEMP table that holds them.
20188     */
20189     if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
20190       sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
20191                    0, 0, 0);
20192     }else
20193
20194     /* .parameter list
20195     ** List all bind parameters.
20196     */
20197     if( nArg==2 && strcmp(azArg[1],"list")==0 ){
20198       sqlite3_stmt *pStmt = 0;
20199       int rx;
20200       int len = 0;
20201       rx = sqlite3_prepare_v2(p->db,
20202              "SELECT max(length(key)) "
20203              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
20204       if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20205         len = sqlite3_column_int(pStmt, 0);
20206         if( len>40 ) len = 40;
20207       }
20208       sqlite3_finalize(pStmt);
20209       pStmt = 0;
20210       if( len ){
20211         rx = sqlite3_prepare_v2(p->db,
20212              "SELECT key, quote(value) "
20213              "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
20214         while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20215           utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
20216                       sqlite3_column_text(pStmt,1));
20217         }
20218         sqlite3_finalize(pStmt);
20219       }
20220     }else
20221
20222     /* .parameter init
20223     ** Make sure the TEMP table used to hold bind parameters exists.
20224     ** Create it if necessary.
20225     */
20226     if( nArg==2 && strcmp(azArg[1],"init")==0 ){
20227       bind_table_init(p);
20228     }else
20229
20230     /* .parameter set NAME VALUE
20231     ** Set or reset a bind parameter.  NAME should be the full parameter
20232     ** name exactly as it appears in the query.  (ex: $abc, @def).  The
20233     ** VALUE can be in either SQL literal notation, or if not it will be
20234     ** understood to be a text string.
20235     */
20236     if( nArg==4 && strcmp(azArg[1],"set")==0 ){
20237       int rx;
20238       char *zSql;
20239       sqlite3_stmt *pStmt;
20240       const char *zKey = azArg[2];
20241       const char *zValue = azArg[3];
20242       bind_table_init(p);
20243       zSql = sqlite3_mprintf(
20244                   "REPLACE INTO temp.sqlite_parameters(key,value)"
20245                   "VALUES(%Q,%s);", zKey, zValue);
20246       if( zSql==0 ) shell_out_of_memory();
20247       pStmt = 0;
20248       rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20249       sqlite3_free(zSql);
20250       if( rx!=SQLITE_OK ){
20251         sqlite3_finalize(pStmt);
20252         pStmt = 0;
20253         zSql = sqlite3_mprintf(
20254                    "REPLACE INTO temp.sqlite_parameters(key,value)"
20255                    "VALUES(%Q,%Q);", zKey, zValue);
20256         if( zSql==0 ) shell_out_of_memory();
20257         rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20258         sqlite3_free(zSql);
20259         if( rx!=SQLITE_OK ){
20260           utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
20261           sqlite3_finalize(pStmt);
20262           pStmt = 0;
20263           rc = 1;
20264         }
20265       }
20266       sqlite3_step(pStmt);
20267       sqlite3_finalize(pStmt);
20268     }else
20269
20270     /* .parameter unset NAME
20271     ** Remove the NAME binding from the parameter binding table, if it
20272     ** exists.
20273     */
20274     if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
20275       char *zSql = sqlite3_mprintf(
20276           "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
20277       if( zSql==0 ) shell_out_of_memory();
20278       sqlite3_exec(p->db, zSql, 0, 0, 0);
20279       sqlite3_free(zSql);
20280     }else
20281     /* If no command name matches, show a syntax error */
20282     parameter_syntax_error:
20283     showHelp(p->out, "parameter");
20284   }else
20285
20286   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
20287     int i;
20288     for(i=1; i<nArg; i++){
20289       if( i>1 ) raw_printf(p->out, " ");
20290       utf8_printf(p->out, "%s", azArg[i]);
20291     }
20292     raw_printf(p->out, "\n");
20293   }else
20294
20295 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
20296   if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
20297     int i;
20298     int nn = 0;
20299     p->flgProgress = 0;
20300     p->mxProgress = 0;
20301     p->nProgress = 0;
20302     for(i=1; i<nArg; i++){
20303       const char *z = azArg[i];
20304       if( z[0]=='-' ){
20305         z++;
20306         if( z[0]=='-' ) z++;
20307         if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
20308           p->flgProgress |= SHELL_PROGRESS_QUIET;
20309           continue;
20310         }
20311         if( strcmp(z,"reset")==0 ){
20312           p->flgProgress |= SHELL_PROGRESS_RESET;
20313           continue;
20314         }
20315         if( strcmp(z,"once")==0 ){
20316           p->flgProgress |= SHELL_PROGRESS_ONCE;
20317           continue;
20318         }
20319         if( strcmp(z,"limit")==0 ){
20320           if( i+1>=nArg ){
20321             utf8_printf(stderr, "Error: missing argument on --limit\n");
20322             rc = 1;
20323             goto meta_command_exit;
20324           }else{
20325             p->mxProgress = (int)integerValue(azArg[++i]);
20326           }
20327           continue;
20328         }
20329         utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
20330         rc = 1;
20331         goto meta_command_exit;
20332       }else{
20333         nn = (int)integerValue(z);
20334       }
20335     }
20336     open_db(p, 0);
20337     sqlite3_progress_handler(p->db, nn, progress_handler, p);
20338   }else
20339 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
20340
20341   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
20342     if( nArg >= 2) {
20343       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
20344     }
20345     if( nArg >= 3) {
20346       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
20347     }
20348   }else
20349
20350   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
20351     rc = 2;
20352   }else
20353
20354   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
20355     FILE *inSaved = p->in;
20356     int savedLineno = p->lineno;
20357     failIfSafeMode(p, "cannot run .read in safe mode");
20358     if( nArg!=2 ){
20359       raw_printf(stderr, "Usage: .read FILE\n");
20360       rc = 1;
20361       goto meta_command_exit;
20362     }
20363     if( azArg[1][0]=='|' ){
20364 #ifdef SQLITE_OMIT_POPEN
20365       raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20366       rc = 1;
20367       p->out = stdout;
20368 #else
20369       p->in = popen(azArg[1]+1, "r");
20370       if( p->in==0 ){
20371         utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
20372         rc = 1;
20373       }else{
20374         rc = process_input(p);
20375         pclose(p->in);
20376       }
20377 #endif
20378     }else if( (p->in = openChrSource(azArg[1]))==0 ){
20379       utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
20380       rc = 1;
20381     }else{
20382       rc = process_input(p);
20383       fclose(p->in);
20384     }
20385     p->in = inSaved;
20386     p->lineno = savedLineno;
20387   }else
20388
20389   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
20390     const char *zSrcFile;
20391     const char *zDb;
20392     sqlite3 *pSrc;
20393     sqlite3_backup *pBackup;
20394     int nTimeout = 0;
20395
20396     failIfSafeMode(p, "cannot run .restore in safe mode");
20397     if( nArg==2 ){
20398       zSrcFile = azArg[1];
20399       zDb = "main";
20400     }else if( nArg==3 ){
20401       zSrcFile = azArg[2];
20402       zDb = azArg[1];
20403     }else{
20404       raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
20405       rc = 1;
20406       goto meta_command_exit;
20407     }
20408     rc = sqlite3_open(zSrcFile, &pSrc);
20409     if( rc!=SQLITE_OK ){
20410       utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
20411       close_db(pSrc);
20412       return 1;
20413     }
20414     open_db(p, 0);
20415     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
20416     if( pBackup==0 ){
20417       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20418       close_db(pSrc);
20419       return 1;
20420     }
20421     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
20422           || rc==SQLITE_BUSY  ){
20423       if( rc==SQLITE_BUSY ){
20424         if( nTimeout++ >= 3 ) break;
20425         sqlite3_sleep(100);
20426       }
20427     }
20428     sqlite3_backup_finish(pBackup);
20429     if( rc==SQLITE_DONE ){
20430       rc = 0;
20431     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
20432       raw_printf(stderr, "Error: source database is busy\n");
20433       rc = 1;
20434     }else{
20435       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20436       rc = 1;
20437     }
20438     close_db(pSrc);
20439   }else
20440
20441   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
20442     if( nArg==2 ){
20443       p->scanstatsOn = (u8)booleanValue(azArg[1]);
20444 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
20445       raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
20446 #endif
20447     }else{
20448       raw_printf(stderr, "Usage: .scanstats on|off\n");
20449       rc = 1;
20450     }
20451   }else
20452
20453   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
20454     ShellText sSelect;
20455     ShellState data;
20456     char *zErrMsg = 0;
20457     const char *zDiv = "(";
20458     const char *zName = 0;
20459     int iSchema = 0;
20460     int bDebug = 0;
20461     int bNoSystemTabs = 0;
20462     int ii;
20463
20464     open_db(p, 0);
20465     memcpy(&data, p, sizeof(data));
20466     data.showHeader = 0;
20467     data.cMode = data.mode = MODE_Semi;
20468     initText(&sSelect);
20469     for(ii=1; ii<nArg; ii++){
20470       if( optionMatch(azArg[ii],"indent") ){
20471         data.cMode = data.mode = MODE_Pretty;
20472       }else if( optionMatch(azArg[ii],"debug") ){
20473         bDebug = 1;
20474       }else if( optionMatch(azArg[ii],"nosys") ){
20475         bNoSystemTabs = 1;
20476       }else if( azArg[ii][0]=='-' ){
20477         utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
20478         rc = 1;
20479         goto meta_command_exit;
20480       }else if( zName==0 ){
20481         zName = azArg[ii];
20482       }else{
20483         raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
20484         rc = 1;
20485         goto meta_command_exit;
20486       }
20487     }
20488     if( zName!=0 ){
20489       int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
20490                   || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
20491                   || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
20492                   || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
20493       if( isSchema ){
20494         char *new_argv[2], *new_colv[2];
20495         new_argv[0] = sqlite3_mprintf(
20496                       "CREATE TABLE %s (\n"
20497                       "  type text,\n"
20498                       "  name text,\n"
20499                       "  tbl_name text,\n"
20500                       "  rootpage integer,\n"
20501                       "  sql text\n"
20502                       ")", zName);
20503         new_argv[1] = 0;
20504         new_colv[0] = "sql";
20505         new_colv[1] = 0;
20506         callback(&data, 1, new_argv, new_colv);
20507         sqlite3_free(new_argv[0]);
20508       }
20509     }
20510     if( zDiv ){
20511       sqlite3_stmt *pStmt = 0;
20512       rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
20513                               -1, &pStmt, 0);
20514       if( rc ){
20515         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20516         sqlite3_finalize(pStmt);
20517         rc = 1;
20518         goto meta_command_exit;
20519       }
20520       appendText(&sSelect, "SELECT sql FROM", 0);
20521       iSchema = 0;
20522       while( sqlite3_step(pStmt)==SQLITE_ROW ){
20523         const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
20524         char zScNum[30];
20525         sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
20526         appendText(&sSelect, zDiv, 0);
20527         zDiv = " UNION ALL ";
20528         appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
20529         if( sqlite3_stricmp(zDb, "main")!=0 ){
20530           appendText(&sSelect, zDb, '\'');
20531         }else{
20532           appendText(&sSelect, "NULL", 0);
20533         }
20534         appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
20535         appendText(&sSelect, zScNum, 0);
20536         appendText(&sSelect, " AS snum, ", 0);
20537         appendText(&sSelect, zDb, '\'');
20538         appendText(&sSelect, " AS sname FROM ", 0);
20539         appendText(&sSelect, zDb, quoteChar(zDb));
20540         appendText(&sSelect, ".sqlite_schema", 0);
20541       }
20542       sqlite3_finalize(pStmt);
20543 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
20544       if( zName ){
20545         appendText(&sSelect,
20546            " UNION ALL SELECT shell_module_schema(name),"
20547            " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
20548         0);
20549       }
20550 #endif
20551       appendText(&sSelect, ") WHERE ", 0);
20552       if( zName ){
20553         char *zQarg = sqlite3_mprintf("%Q", zName);
20554         int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20555                     strchr(zName, '[') != 0;
20556         if( strchr(zName, '.') ){
20557           appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
20558         }else{
20559           appendText(&sSelect, "lower(tbl_name)", 0);
20560         }
20561         appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
20562         appendText(&sSelect, zQarg, 0);
20563         if( !bGlob ){
20564           appendText(&sSelect, " ESCAPE '\\' ", 0);
20565         }
20566         appendText(&sSelect, " AND ", 0);
20567         sqlite3_free(zQarg);
20568       }
20569       if( bNoSystemTabs ){
20570         appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
20571       }
20572       appendText(&sSelect, "sql IS NOT NULL"
20573                            " ORDER BY snum, rowid", 0);
20574       if( bDebug ){
20575         utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
20576       }else{
20577         rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
20578       }
20579       freeText(&sSelect);
20580     }
20581     if( zErrMsg ){
20582       utf8_printf(stderr,"Error: %s\n", zErrMsg);
20583       sqlite3_free(zErrMsg);
20584       rc = 1;
20585     }else if( rc != SQLITE_OK ){
20586       raw_printf(stderr,"Error: querying schema information\n");
20587       rc = 1;
20588     }else{
20589       rc = 0;
20590     }
20591   }else
20592
20593   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
20594     unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
20595     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
20596   }else
20597
20598 #if defined(SQLITE_ENABLE_SESSION)
20599   if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
20600     struct AuxDb *pAuxDb = p->pAuxDb;
20601     OpenSession *pSession = &pAuxDb->aSession[0];
20602     char **azCmd = &azArg[1];
20603     int iSes = 0;
20604     int nCmd = nArg - 1;
20605     int i;
20606     if( nArg<=1 ) goto session_syntax_error;
20607     open_db(p, 0);
20608     if( nArg>=3 ){
20609       for(iSes=0; iSes<pAuxDb->nSession; iSes++){
20610         if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
20611       }
20612       if( iSes<pAuxDb->nSession ){
20613         pSession = &pAuxDb->aSession[iSes];
20614         azCmd++;
20615         nCmd--;
20616       }else{
20617         pSession = &pAuxDb->aSession[0];
20618         iSes = 0;
20619       }
20620     }
20621
20622     /* .session attach TABLE
20623     ** Invoke the sqlite3session_attach() interface to attach a particular
20624     ** table so that it is never filtered.
20625     */
20626     if( strcmp(azCmd[0],"attach")==0 ){
20627       if( nCmd!=2 ) goto session_syntax_error;
20628       if( pSession->p==0 ){
20629         session_not_open:
20630         raw_printf(stderr, "ERROR: No sessions are open\n");
20631       }else{
20632         rc = sqlite3session_attach(pSession->p, azCmd[1]);
20633         if( rc ){
20634           raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
20635           rc = 0;
20636         }
20637       }
20638     }else
20639
20640     /* .session changeset FILE
20641     ** .session patchset FILE
20642     ** Write a changeset or patchset into a file.  The file is overwritten.
20643     */
20644     if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
20645       FILE *out = 0;
20646       failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
20647       if( nCmd!=2 ) goto session_syntax_error;
20648       if( pSession->p==0 ) goto session_not_open;
20649       out = fopen(azCmd[1], "wb");
20650       if( out==0 ){
20651         utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
20652                     azCmd[1]);
20653       }else{
20654         int szChng;
20655         void *pChng;
20656         if( azCmd[0][0]=='c' ){
20657           rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
20658         }else{
20659           rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
20660         }
20661         if( rc ){
20662           printf("Error: error code %d\n", rc);
20663           rc = 0;
20664         }
20665         if( pChng
20666           && fwrite(pChng, szChng, 1, out)!=1 ){
20667           raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
20668                   szChng);
20669         }
20670         sqlite3_free(pChng);
20671         fclose(out);
20672       }
20673     }else
20674
20675     /* .session close
20676     ** Close the identified session
20677     */
20678     if( strcmp(azCmd[0], "close")==0 ){
20679       if( nCmd!=1 ) goto session_syntax_error;
20680       if( pAuxDb->nSession ){
20681         session_close(pSession);
20682         pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
20683       }
20684     }else
20685
20686     /* .session enable ?BOOLEAN?
20687     ** Query or set the enable flag
20688     */
20689     if( strcmp(azCmd[0], "enable")==0 ){
20690       int ii;
20691       if( nCmd>2 ) goto session_syntax_error;
20692       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20693       if( pAuxDb->nSession ){
20694         ii = sqlite3session_enable(pSession->p, ii);
20695         utf8_printf(p->out, "session %s enable flag = %d\n",
20696                     pSession->zName, ii);
20697       }
20698     }else
20699
20700     /* .session filter GLOB ....
20701     ** Set a list of GLOB patterns of table names to be excluded.
20702     */
20703     if( strcmp(azCmd[0], "filter")==0 ){
20704       int ii, nByte;
20705       if( nCmd<2 ) goto session_syntax_error;
20706       if( pAuxDb->nSession ){
20707         for(ii=0; ii<pSession->nFilter; ii++){
20708           sqlite3_free(pSession->azFilter[ii]);
20709         }
20710         sqlite3_free(pSession->azFilter);
20711         nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
20712         pSession->azFilter = sqlite3_malloc( nByte );
20713         if( pSession->azFilter==0 ){
20714           raw_printf(stderr, "Error: out or memory\n");
20715           exit(1);
20716         }
20717         for(ii=1; ii<nCmd; ii++){
20718           pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
20719         }
20720         pSession->nFilter = ii-1;
20721       }
20722     }else
20723
20724     /* .session indirect ?BOOLEAN?
20725     ** Query or set the indirect flag
20726     */
20727     if( strcmp(azCmd[0], "indirect")==0 ){
20728       int ii;
20729       if( nCmd>2 ) goto session_syntax_error;
20730       ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20731       if( pAuxDb->nSession ){
20732         ii = sqlite3session_indirect(pSession->p, ii);
20733         utf8_printf(p->out, "session %s indirect flag = %d\n",
20734                     pSession->zName, ii);
20735       }
20736     }else
20737
20738     /* .session isempty
20739     ** Determine if the session is empty
20740     */
20741     if( strcmp(azCmd[0], "isempty")==0 ){
20742       int ii;
20743       if( nCmd!=1 ) goto session_syntax_error;
20744       if( pAuxDb->nSession ){
20745         ii = sqlite3session_isempty(pSession->p);
20746         utf8_printf(p->out, "session %s isempty flag = %d\n",
20747                     pSession->zName, ii);
20748       }
20749     }else
20750
20751     /* .session list
20752     ** List all currently open sessions
20753     */
20754     if( strcmp(azCmd[0],"list")==0 ){
20755       for(i=0; i<pAuxDb->nSession; i++){
20756         utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
20757       }
20758     }else
20759
20760     /* .session open DB NAME
20761     ** Open a new session called NAME on the attached database DB.
20762     ** DB is normally "main".
20763     */
20764     if( strcmp(azCmd[0],"open")==0 ){
20765       char *zName;
20766       if( nCmd!=3 ) goto session_syntax_error;
20767       zName = azCmd[2];
20768       if( zName[0]==0 ) goto session_syntax_error;
20769       for(i=0; i<pAuxDb->nSession; i++){
20770         if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
20771           utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
20772           goto meta_command_exit;
20773         }
20774       }
20775       if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
20776         raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
20777         goto meta_command_exit;
20778       }
20779       pSession = &pAuxDb->aSession[pAuxDb->nSession];
20780       rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
20781       if( rc ){
20782         raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
20783         rc = 0;
20784         goto meta_command_exit;
20785       }
20786       pSession->nFilter = 0;
20787       sqlite3session_table_filter(pSession->p, session_filter, pSession);
20788       pAuxDb->nSession++;
20789       pSession->zName = sqlite3_mprintf("%s", zName);
20790     }else
20791     /* If no command name matches, show a syntax error */
20792     session_syntax_error:
20793     showHelp(p->out, "session");
20794   }else
20795 #endif
20796
20797 #ifdef SQLITE_DEBUG
20798   /* Undocumented commands for internal testing.  Subject to change
20799   ** without notice. */
20800   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
20801     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
20802       int i, v;
20803       for(i=1; i<nArg; i++){
20804         v = booleanValue(azArg[i]);
20805         utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
20806       }
20807     }
20808     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
20809       int i; sqlite3_int64 v;
20810       for(i=1; i<nArg; i++){
20811         char zBuf[200];
20812         v = integerValue(azArg[i]);
20813         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
20814         utf8_printf(p->out, "%s", zBuf);
20815       }
20816     }
20817   }else
20818 #endif
20819
20820   if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
20821     int bIsInit = 0;         /* True to initialize the SELFTEST table */
20822     int bVerbose = 0;        /* Verbose output */
20823     int bSelftestExists;     /* True if SELFTEST already exists */
20824     int i, k;                /* Loop counters */
20825     int nTest = 0;           /* Number of tests runs */
20826     int nErr = 0;            /* Number of errors seen */
20827     ShellText str;           /* Answer for a query */
20828     sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
20829
20830     open_db(p,0);
20831     for(i=1; i<nArg; i++){
20832       const char *z = azArg[i];
20833       if( z[0]=='-' && z[1]=='-' ) z++;
20834       if( strcmp(z,"-init")==0 ){
20835         bIsInit = 1;
20836       }else
20837       if( strcmp(z,"-v")==0 ){
20838         bVerbose++;
20839       }else
20840       {
20841         utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20842                     azArg[i], azArg[0]);
20843         raw_printf(stderr, "Should be one of: --init -v\n");
20844         rc = 1;
20845         goto meta_command_exit;
20846       }
20847     }
20848     if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
20849            != SQLITE_OK ){
20850       bSelftestExists = 0;
20851     }else{
20852       bSelftestExists = 1;
20853     }
20854     if( bIsInit ){
20855       createSelftestTable(p);
20856       bSelftestExists = 1;
20857     }
20858     initText(&str);
20859     appendText(&str, "x", 0);
20860     for(k=bSelftestExists; k>=0; k--){
20861       if( k==1 ){
20862         rc = sqlite3_prepare_v2(p->db,
20863             "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
20864             -1, &pStmt, 0);
20865       }else{
20866         rc = sqlite3_prepare_v2(p->db,
20867           "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
20868           "      (1,'run','PRAGMA integrity_check','ok')",
20869           -1, &pStmt, 0);
20870       }
20871       if( rc ){
20872         raw_printf(stderr, "Error querying the selftest table\n");
20873         rc = 1;
20874         sqlite3_finalize(pStmt);
20875         goto meta_command_exit;
20876       }
20877       for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
20878         int tno = sqlite3_column_int(pStmt, 0);
20879         const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
20880         const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
20881         const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
20882
20883         k = 0;
20884         if( bVerbose>0 ){
20885           char *zQuote = sqlite3_mprintf("%q", zSql);
20886           printf("%d: %s %s\n", tno, zOp, zSql);
20887           sqlite3_free(zQuote);
20888         }
20889         if( strcmp(zOp,"memo")==0 ){
20890           utf8_printf(p->out, "%s\n", zSql);
20891         }else
20892         if( strcmp(zOp,"run")==0 ){
20893           char *zErrMsg = 0;
20894           str.n = 0;
20895           str.z[0] = 0;
20896           rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
20897           nTest++;
20898           if( bVerbose ){
20899             utf8_printf(p->out, "Result: %s\n", str.z);
20900           }
20901           if( rc || zErrMsg ){
20902             nErr++;
20903             rc = 1;
20904             utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
20905             sqlite3_free(zErrMsg);
20906           }else if( strcmp(zAns,str.z)!=0 ){
20907             nErr++;
20908             rc = 1;
20909             utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
20910             utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
20911           }
20912         }else
20913         {
20914           utf8_printf(stderr,
20915             "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
20916           rc = 1;
20917           break;
20918         }
20919       } /* End loop over rows of content from SELFTEST */
20920       sqlite3_finalize(pStmt);
20921     } /* End loop over k */
20922     freeText(&str);
20923     utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
20924   }else
20925
20926   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
20927     if( nArg<2 || nArg>3 ){
20928       raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
20929       rc = 1;
20930     }
20931     if( nArg>=2 ){
20932       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
20933                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
20934     }
20935     if( nArg>=3 ){
20936       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
20937                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
20938     }
20939   }else
20940
20941   if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
20942     const char *zLike = 0;   /* Which table to checksum. 0 means everything */
20943     int i;                   /* Loop counter */
20944     int bSchema = 0;         /* Also hash the schema */
20945     int bSeparate = 0;       /* Hash each table separately */
20946     int iSize = 224;         /* Hash algorithm to use */
20947     int bDebug = 0;          /* Only show the query that would have run */
20948     sqlite3_stmt *pStmt;     /* For querying tables names */
20949     char *zSql;              /* SQL to be run */
20950     char *zSep;              /* Separator */
20951     ShellText sSql;          /* Complete SQL for the query to run the hash */
20952     ShellText sQuery;        /* Set of queries used to read all content */
20953     open_db(p, 0);
20954     for(i=1; i<nArg; i++){
20955       const char *z = azArg[i];
20956       if( z[0]=='-' ){
20957         z++;
20958         if( z[0]=='-' ) z++;
20959         if( strcmp(z,"schema")==0 ){
20960           bSchema = 1;
20961         }else
20962         if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
20963          || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
20964         ){
20965           iSize = atoi(&z[5]);
20966         }else
20967         if( strcmp(z,"debug")==0 ){
20968           bDebug = 1;
20969         }else
20970         {
20971           utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20972                       azArg[i], azArg[0]);
20973           showHelp(p->out, azArg[0]);
20974           rc = 1;
20975           goto meta_command_exit;
20976         }
20977       }else if( zLike ){
20978         raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
20979         rc = 1;
20980         goto meta_command_exit;
20981       }else{
20982         zLike = z;
20983         bSeparate = 1;
20984         if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
20985       }
20986     }
20987     if( bSchema ){
20988       zSql = "SELECT lower(name) FROM sqlite_schema"
20989              " WHERE type='table' AND coalesce(rootpage,0)>1"
20990              " UNION ALL SELECT 'sqlite_schema'"
20991              " ORDER BY 1 collate nocase";
20992     }else{
20993       zSql = "SELECT lower(name) FROM sqlite_schema"
20994              " WHERE type='table' AND coalesce(rootpage,0)>1"
20995              " AND name NOT LIKE 'sqlite_%'"
20996              " ORDER BY 1 collate nocase";
20997     }
20998     sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20999     initText(&sQuery);
21000     initText(&sSql);
21001     appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
21002     zSep = "VALUES(";
21003     while( SQLITE_ROW==sqlite3_step(pStmt) ){
21004       const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
21005       if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21006       if( strncmp(zTab, "sqlite_",7)!=0 ){
21007         appendText(&sQuery,"SELECT * FROM ", 0);
21008         appendText(&sQuery,zTab,'"');
21009         appendText(&sQuery," NOT INDEXED;", 0);
21010       }else if( strcmp(zTab, "sqlite_schema")==0 ){
21011         appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
21012                            " ORDER BY name;", 0);
21013       }else if( strcmp(zTab, "sqlite_sequence")==0 ){
21014         appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
21015                            " ORDER BY name;", 0);
21016       }else if( strcmp(zTab, "sqlite_stat1")==0 ){
21017         appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
21018                            " ORDER BY tbl,idx;", 0);
21019       }else if( strcmp(zTab, "sqlite_stat4")==0 ){
21020         appendText(&sQuery, "SELECT * FROM ", 0);
21021         appendText(&sQuery, zTab, 0);
21022         appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
21023       }
21024       appendText(&sSql, zSep, 0);
21025       appendText(&sSql, sQuery.z, '\'');
21026       sQuery.n = 0;
21027       appendText(&sSql, ",", 0);
21028       appendText(&sSql, zTab, '\'');
21029       zSep = "),(";
21030     }
21031     sqlite3_finalize(pStmt);
21032     if( bSeparate ){
21033       zSql = sqlite3_mprintf(
21034           "%s))"
21035           " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
21036           "   FROM [sha3sum$query]",
21037           sSql.z, iSize);
21038     }else{
21039       zSql = sqlite3_mprintf(
21040           "%s))"
21041           " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
21042           "   FROM [sha3sum$query]",
21043           sSql.z, iSize);
21044     }
21045     freeText(&sQuery);
21046     freeText(&sSql);
21047     if( bDebug ){
21048       utf8_printf(p->out, "%s\n", zSql);
21049     }else{
21050       shell_exec(p, zSql, 0);
21051     }
21052     sqlite3_free(zSql);
21053   }else
21054
21055 #ifndef SQLITE_NOHAVE_SYSTEM
21056   if( c=='s'
21057    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21058   ){
21059     char *zCmd;
21060     int i, x;
21061     failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
21062     if( nArg<2 ){
21063       raw_printf(stderr, "Usage: .system COMMAND\n");
21064       rc = 1;
21065       goto meta_command_exit;
21066     }
21067     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
21068     for(i=2; i<nArg; i++){
21069       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
21070                              zCmd, azArg[i]);
21071     }
21072     x = system(zCmd);
21073     sqlite3_free(zCmd);
21074     if( x ) raw_printf(stderr, "System command returns %d\n", x);
21075   }else
21076 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
21077
21078   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21079     static const char *azBool[] = { "off", "on", "trigger", "full"};
21080     const char *zOut;
21081     int i;
21082     if( nArg!=1 ){
21083       raw_printf(stderr, "Usage: .show\n");
21084       rc = 1;
21085       goto meta_command_exit;
21086     }
21087     utf8_printf(p->out, "%12.12s: %s\n","echo",
21088                                   azBool[ShellHasFlag(p, SHFLG_Echo)]);
21089     utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
21090     utf8_printf(p->out, "%12.12s: %s\n","explain",
21091          p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
21092     utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
21093     utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
21094     utf8_printf(p->out, "%12.12s: ", "nullvalue");
21095       output_c_string(p->out, p->nullValue);
21096       raw_printf(p->out, "\n");
21097     utf8_printf(p->out,"%12.12s: %s\n","output",
21098             strlen30(p->outfile) ? p->outfile : "stdout");
21099     utf8_printf(p->out,"%12.12s: ", "colseparator");
21100       output_c_string(p->out, p->colSeparator);
21101       raw_printf(p->out, "\n");
21102     utf8_printf(p->out,"%12.12s: ", "rowseparator");
21103       output_c_string(p->out, p->rowSeparator);
21104       raw_printf(p->out, "\n");
21105     switch( p->statsOn ){
21106       case 0:  zOut = "off";     break;
21107       default: zOut = "on";      break;
21108       case 2:  zOut = "stmt";    break;
21109       case 3:  zOut = "vmstep";  break;
21110     }
21111     utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
21112     utf8_printf(p->out, "%12.12s: ", "width");
21113     for (i=0;i<p->nWidth;i++) {
21114       raw_printf(p->out, "%d ", p->colWidth[i]);
21115     }
21116     raw_printf(p->out, "\n");
21117     utf8_printf(p->out, "%12.12s: %s\n", "filename",
21118                 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
21119   }else
21120
21121   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
21122     if( nArg==2 ){
21123       if( strcmp(azArg[1],"stmt")==0 ){
21124         p->statsOn = 2;
21125       }else if( strcmp(azArg[1],"vmstep")==0 ){
21126         p->statsOn = 3;
21127       }else{
21128         p->statsOn = (u8)booleanValue(azArg[1]);
21129       }
21130     }else if( nArg==1 ){
21131       display_stats(p->db, p, 0);
21132     }else{
21133       raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
21134       rc = 1;
21135     }
21136   }else
21137
21138   if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
21139    || (c=='i' && (strncmp(azArg[0], "indices", n)==0
21140                  || strncmp(azArg[0], "indexes", n)==0) )
21141   ){
21142     sqlite3_stmt *pStmt;
21143     char **azResult;
21144     int nRow, nAlloc;
21145     int ii;
21146     ShellText s;
21147     initText(&s);
21148     open_db(p, 0);
21149     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
21150     if( rc ){
21151       sqlite3_finalize(pStmt);
21152       return shellDatabaseError(p->db);
21153     }
21154
21155     if( nArg>2 && c=='i' ){
21156       /* It is an historical accident that the .indexes command shows an error
21157       ** when called with the wrong number of arguments whereas the .tables
21158       ** command does not. */
21159       raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
21160       rc = 1;
21161       sqlite3_finalize(pStmt);
21162       goto meta_command_exit;
21163     }
21164     for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
21165       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
21166       if( zDbName==0 ) continue;
21167       if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
21168       if( sqlite3_stricmp(zDbName, "main")==0 ){
21169         appendText(&s, "SELECT name FROM ", 0);
21170       }else{
21171         appendText(&s, "SELECT ", 0);
21172         appendText(&s, zDbName, '\'');
21173         appendText(&s, "||'.'||name FROM ", 0);
21174       }
21175       appendText(&s, zDbName, '"');
21176       appendText(&s, ".sqlite_schema ", 0);
21177       if( c=='t' ){
21178         appendText(&s," WHERE type IN ('table','view')"
21179                       "   AND name NOT LIKE 'sqlite_%'"
21180                       "   AND name LIKE ?1", 0);
21181       }else{
21182         appendText(&s," WHERE type='index'"
21183                       "   AND tbl_name LIKE ?1", 0);
21184       }
21185     }
21186     rc = sqlite3_finalize(pStmt);
21187     if( rc==SQLITE_OK ){
21188       appendText(&s, " ORDER BY 1", 0);
21189       rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
21190     }
21191     freeText(&s);
21192     if( rc ) return shellDatabaseError(p->db);
21193
21194     /* Run the SQL statement prepared by the above block. Store the results
21195     ** as an array of nul-terminated strings in azResult[].  */
21196     nRow = nAlloc = 0;
21197     azResult = 0;
21198     if( nArg>1 ){
21199       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
21200     }else{
21201       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
21202     }
21203     while( sqlite3_step(pStmt)==SQLITE_ROW ){
21204       if( nRow>=nAlloc ){
21205         char **azNew;
21206         int n2 = nAlloc*2 + 10;
21207         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
21208         if( azNew==0 ) shell_out_of_memory();
21209         nAlloc = n2;
21210         azResult = azNew;
21211       }
21212       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
21213       if( 0==azResult[nRow] ) shell_out_of_memory();
21214       nRow++;
21215     }
21216     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
21217       rc = shellDatabaseError(p->db);
21218     }
21219
21220     /* Pretty-print the contents of array azResult[] to the output */
21221     if( rc==0 && nRow>0 ){
21222       int len, maxlen = 0;
21223       int i, j;
21224       int nPrintCol, nPrintRow;
21225       for(i=0; i<nRow; i++){
21226         len = strlen30(azResult[i]);
21227         if( len>maxlen ) maxlen = len;
21228       }
21229       nPrintCol = 80/(maxlen+2);
21230       if( nPrintCol<1 ) nPrintCol = 1;
21231       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
21232       for(i=0; i<nPrintRow; i++){
21233         for(j=i; j<nRow; j+=nPrintRow){
21234           char *zSp = j<nPrintRow ? "" : "  ";
21235           utf8_printf(p->out, "%s%-*s", zSp, maxlen,
21236                       azResult[j] ? azResult[j]:"");
21237         }
21238         raw_printf(p->out, "\n");
21239       }
21240     }
21241
21242     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
21243     sqlite3_free(azResult);
21244   }else
21245
21246   /* Begin redirecting output to the file "testcase-out.txt" */
21247   if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
21248     output_reset(p);
21249     p->out = output_file_open("testcase-out.txt", 0);
21250     if( p->out==0 ){
21251       raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
21252     }
21253     if( nArg>=2 ){
21254       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
21255     }else{
21256       sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
21257     }
21258   }else
21259
21260 #ifndef SQLITE_UNTESTABLE
21261   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
21262     static const struct {
21263        const char *zCtrlName;   /* Name of a test-control option */
21264        int ctrlCode;            /* Integer code for that option */
21265        const char *zUsage;      /* Usage notes */
21266     } aCtrl[] = {
21267       { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
21268       { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
21269     /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
21270     /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
21271       { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
21272       { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
21273     /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
21274       { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
21275       { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
21276       { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
21277       { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
21278       { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
21279 #ifdef YYCOVERAGE
21280       { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
21281 #endif
21282       { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
21283       { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
21284       { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
21285       { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
21286       { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
21287       { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP,   "NMAX"           },
21288       { "tune",               SQLITE_TESTCTRL_TUNE,          "ID VALUE"       },
21289     };
21290     int testctrl = -1;
21291     int iCtrl = -1;
21292     int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
21293     int isOk = 0;
21294     int i, n2;
21295     const char *zCmd = 0;
21296
21297     open_db(p, 0);
21298     zCmd = nArg>=2 ? azArg[1] : "help";
21299
21300     /* The argument can optionally begin with "-" or "--" */
21301     if( zCmd[0]=='-' && zCmd[1] ){
21302       zCmd++;
21303       if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
21304     }
21305
21306     /* --help lists all test-controls */
21307     if( strcmp(zCmd,"help")==0 ){
21308       utf8_printf(p->out, "Available test-controls:\n");
21309       for(i=0; i<ArraySize(aCtrl); i++){
21310         utf8_printf(p->out, "  .testctrl %s %s\n",
21311                     aCtrl[i].zCtrlName, aCtrl[i].zUsage);
21312       }
21313       rc = 1;
21314       goto meta_command_exit;
21315     }
21316
21317     /* convert testctrl text option to value. allow any unique prefix
21318     ** of the option name, or a numerical value. */
21319     n2 = strlen30(zCmd);
21320     for(i=0; i<ArraySize(aCtrl); i++){
21321       if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
21322         if( testctrl<0 ){
21323           testctrl = aCtrl[i].ctrlCode;
21324           iCtrl = i;
21325         }else{
21326           utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
21327                               "Use \".testctrl --help\" for help\n", zCmd);
21328           rc = 1;
21329           goto meta_command_exit;
21330         }
21331       }
21332     }
21333     if( testctrl<0 ){
21334       utf8_printf(stderr,"Error: unknown test-control: %s\n"
21335                          "Use \".testctrl --help\" for help\n", zCmd);
21336     }else{
21337       switch(testctrl){
21338
21339         /* sqlite3_test_control(int, db, int) */
21340         case SQLITE_TESTCTRL_OPTIMIZATIONS:
21341           if( nArg==3 ){
21342             unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
21343             rc2 = sqlite3_test_control(testctrl, p->db, opt);
21344             isOk = 3;
21345           }
21346           break;
21347
21348         /* sqlite3_test_control(int) */
21349         case SQLITE_TESTCTRL_PRNG_SAVE:
21350         case SQLITE_TESTCTRL_PRNG_RESTORE:
21351         case SQLITE_TESTCTRL_BYTEORDER:
21352           if( nArg==2 ){
21353             rc2 = sqlite3_test_control(testctrl);
21354             isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
21355           }
21356           break;
21357
21358         /* sqlite3_test_control(int, uint) */
21359         case SQLITE_TESTCTRL_PENDING_BYTE:
21360           if( nArg==3 ){
21361             unsigned int opt = (unsigned int)integerValue(azArg[2]);
21362             rc2 = sqlite3_test_control(testctrl, opt);
21363             isOk = 3;
21364           }
21365           break;
21366
21367         /* sqlite3_test_control(int, int, sqlite3*) */
21368         case SQLITE_TESTCTRL_PRNG_SEED:
21369           if( nArg==3 || nArg==4 ){
21370             int ii = (int)integerValue(azArg[2]);
21371             sqlite3 *db;
21372             if( ii==0 && strcmp(azArg[2],"random")==0 ){
21373               sqlite3_randomness(sizeof(ii),&ii);
21374               printf("-- random seed: %d\n", ii);
21375             }
21376             if( nArg==3 ){
21377               db = 0;
21378             }else{
21379               db = p->db;
21380               /* Make sure the schema has been loaded */
21381               sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
21382             }
21383             rc2 = sqlite3_test_control(testctrl, ii, db);
21384             isOk = 3;
21385           }
21386           break;
21387
21388         /* sqlite3_test_control(int, int) */
21389         case SQLITE_TESTCTRL_ASSERT:
21390         case SQLITE_TESTCTRL_ALWAYS:
21391           if( nArg==3 ){
21392             int opt = booleanValue(azArg[2]);
21393             rc2 = sqlite3_test_control(testctrl, opt);
21394             isOk = 1;
21395           }
21396           break;
21397
21398         /* sqlite3_test_control(int, int) */
21399         case SQLITE_TESTCTRL_LOCALTIME_FAULT:
21400         case SQLITE_TESTCTRL_NEVER_CORRUPT:
21401           if( nArg==3 ){
21402             int opt = booleanValue(azArg[2]);
21403             rc2 = sqlite3_test_control(testctrl, opt);
21404             isOk = 3;
21405           }
21406           break;
21407
21408         /* sqlite3_test_control(sqlite3*) */
21409         case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
21410           rc2 = sqlite3_test_control(testctrl, p->db);
21411           isOk = 3;
21412           break;
21413
21414         case SQLITE_TESTCTRL_IMPOSTER:
21415           if( nArg==5 ){
21416             rc2 = sqlite3_test_control(testctrl, p->db,
21417                           azArg[2],
21418                           integerValue(azArg[3]),
21419                           integerValue(azArg[4]));
21420             isOk = 3;
21421           }
21422           break;
21423
21424         case SQLITE_TESTCTRL_SEEK_COUNT: {
21425           u64 x = 0;
21426           rc2 = sqlite3_test_control(testctrl, p->db, &x);
21427           utf8_printf(p->out, "%llu\n", x);
21428           isOk = 3;
21429           break;
21430         }
21431
21432 #ifdef YYCOVERAGE
21433         case SQLITE_TESTCTRL_PARSER_COVERAGE: {
21434           if( nArg==2 ){
21435             sqlite3_test_control(testctrl, p->out);
21436             isOk = 3;
21437           }
21438           break;
21439         }
21440 #endif
21441 #ifdef SQLITE_DEBUG
21442         case SQLITE_TESTCTRL_TUNE: {
21443           if( nArg==4 ){
21444             int id = (int)integerValue(azArg[2]);
21445             int val = (int)integerValue(azArg[3]);
21446             sqlite3_test_control(testctrl, id, &val);
21447             isOk = 3;
21448           }else if( nArg==3 ){
21449             int id = (int)integerValue(azArg[2]);
21450             sqlite3_test_control(testctrl, -id, &rc2);
21451             isOk = 1;
21452           }else if( nArg==2 ){
21453             int id = 1;
21454             while(1){
21455               int val = 0;
21456               rc2 = sqlite3_test_control(testctrl, -id, &val);
21457               if( rc2!=SQLITE_OK ) break;
21458               if( id>1 ) utf8_printf(p->out, "  ");
21459               utf8_printf(p->out, "%d: %d", id, val);
21460               id++;
21461             }
21462             if( id>1 ) utf8_printf(p->out, "\n");
21463             isOk = 3;
21464           }
21465           break;
21466         }
21467 #endif
21468         case SQLITE_TESTCTRL_SORTER_MMAP:
21469           if( nArg==3 ){
21470             int opt = (unsigned int)integerValue(azArg[2]);
21471             rc2 = sqlite3_test_control(testctrl, p->db, opt);
21472             isOk = 3;
21473           }
21474           break;
21475       }
21476     }
21477     if( isOk==0 && iCtrl>=0 ){
21478       utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
21479       rc = 1;
21480     }else if( isOk==1 ){
21481       raw_printf(p->out, "%d\n", rc2);
21482     }else if( isOk==2 ){
21483       raw_printf(p->out, "0x%08x\n", rc2);
21484     }
21485   }else
21486 #endif /* !defined(SQLITE_UNTESTABLE) */
21487
21488   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
21489     open_db(p, 0);
21490     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
21491   }else
21492
21493   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
21494     if( nArg==2 ){
21495       enableTimer = booleanValue(azArg[1]);
21496       if( enableTimer && !HAS_TIMER ){
21497         raw_printf(stderr, "Error: timer not available on this system.\n");
21498         enableTimer = 0;
21499       }
21500     }else{
21501       raw_printf(stderr, "Usage: .timer on|off\n");
21502       rc = 1;
21503     }
21504   }else
21505
21506 #ifndef SQLITE_OMIT_TRACE
21507   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
21508     int mType = 0;
21509     int jj;
21510     open_db(p, 0);
21511     for(jj=1; jj<nArg; jj++){
21512       const char *z = azArg[jj];
21513       if( z[0]=='-' ){
21514         if( optionMatch(z, "expanded") ){
21515           p->eTraceType = SHELL_TRACE_EXPANDED;
21516         }
21517 #ifdef SQLITE_ENABLE_NORMALIZE
21518         else if( optionMatch(z, "normalized") ){
21519           p->eTraceType = SHELL_TRACE_NORMALIZED;
21520         }
21521 #endif
21522         else if( optionMatch(z, "plain") ){
21523           p->eTraceType = SHELL_TRACE_PLAIN;
21524         }
21525         else if( optionMatch(z, "profile") ){
21526           mType |= SQLITE_TRACE_PROFILE;
21527         }
21528         else if( optionMatch(z, "row") ){
21529           mType |= SQLITE_TRACE_ROW;
21530         }
21531         else if( optionMatch(z, "stmt") ){
21532           mType |= SQLITE_TRACE_STMT;
21533         }
21534         else if( optionMatch(z, "close") ){
21535           mType |= SQLITE_TRACE_CLOSE;
21536         }
21537         else {
21538           raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
21539           rc = 1;
21540           goto meta_command_exit;
21541         }
21542       }else{
21543         output_file_close(p->traceOut);
21544         p->traceOut = output_file_open(azArg[1], 0);
21545       }
21546     }
21547     if( p->traceOut==0 ){
21548       sqlite3_trace_v2(p->db, 0, 0, 0);
21549     }else{
21550       if( mType==0 ) mType = SQLITE_TRACE_STMT;
21551       sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
21552     }
21553   }else
21554 #endif /* !defined(SQLITE_OMIT_TRACE) */
21555
21556 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
21557   if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
21558     int ii;
21559     int lenOpt;
21560     char *zOpt;
21561     if( nArg<2 ){
21562       raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
21563       rc = 1;
21564       goto meta_command_exit;
21565     }
21566     open_db(p, 0);
21567     zOpt = azArg[1];
21568     if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
21569     lenOpt = (int)strlen(zOpt);
21570     if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
21571       assert( azArg[nArg]==0 );
21572       sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
21573     }else{
21574       for(ii=1; ii<nArg; ii++){
21575         sqlite3_create_module(p->db, azArg[ii], 0, 0);
21576       }
21577     }
21578   }else
21579 #endif
21580
21581 #if SQLITE_USER_AUTHENTICATION
21582   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
21583     if( nArg<2 ){
21584       raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
21585       rc = 1;
21586       goto meta_command_exit;
21587     }
21588     open_db(p, 0);
21589     if( strcmp(azArg[1],"login")==0 ){
21590       if( nArg!=4 ){
21591         raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
21592         rc = 1;
21593         goto meta_command_exit;
21594       }
21595       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
21596                                      strlen30(azArg[3]));
21597       if( rc ){
21598         utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
21599         rc = 1;
21600       }
21601     }else if( strcmp(azArg[1],"add")==0 ){
21602       if( nArg!=5 ){
21603         raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
21604         rc = 1;
21605         goto meta_command_exit;
21606       }
21607       rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21608                             booleanValue(azArg[4]));
21609       if( rc ){
21610         raw_printf(stderr, "User-Add failed: %d\n", rc);
21611         rc = 1;
21612       }
21613     }else if( strcmp(azArg[1],"edit")==0 ){
21614       if( nArg!=5 ){
21615         raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
21616         rc = 1;
21617         goto meta_command_exit;
21618       }
21619       rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21620                               booleanValue(azArg[4]));
21621       if( rc ){
21622         raw_printf(stderr, "User-Edit failed: %d\n", rc);
21623         rc = 1;
21624       }
21625     }else if( strcmp(azArg[1],"delete")==0 ){
21626       if( nArg!=3 ){
21627         raw_printf(stderr, "Usage: .user delete USER\n");
21628         rc = 1;
21629         goto meta_command_exit;
21630       }
21631       rc = sqlite3_user_delete(p->db, azArg[2]);
21632       if( rc ){
21633         raw_printf(stderr, "User-Delete failed: %d\n", rc);
21634         rc = 1;
21635       }
21636     }else{
21637       raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
21638       rc = 1;
21639       goto meta_command_exit;
21640     }
21641   }else
21642 #endif /* SQLITE_USER_AUTHENTICATION */
21643
21644   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
21645     utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
21646         sqlite3_libversion(), sqlite3_sourceid());
21647 #if SQLITE_HAVE_ZLIB
21648     utf8_printf(p->out, "zlib version %s\n", zlibVersion());
21649 #endif
21650 #define CTIMEOPT_VAL_(opt) #opt
21651 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
21652 #if defined(__clang__) && defined(__clang_major__)
21653     utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
21654                     CTIMEOPT_VAL(__clang_minor__) "."
21655                     CTIMEOPT_VAL(__clang_patchlevel__) "\n");
21656 #elif defined(_MSC_VER)
21657     utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
21658 #elif defined(__GNUC__) && defined(__VERSION__)
21659     utf8_printf(p->out, "gcc-" __VERSION__ "\n");
21660 #endif
21661   }else
21662
21663   if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
21664     const char *zDbName = nArg==2 ? azArg[1] : "main";
21665     sqlite3_vfs *pVfs = 0;
21666     if( p->db ){
21667       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
21668       if( pVfs ){
21669         utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
21670         raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
21671         raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
21672         raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21673       }
21674     }
21675   }else
21676
21677   if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
21678     sqlite3_vfs *pVfs;
21679     sqlite3_vfs *pCurrent = 0;
21680     if( p->db ){
21681       sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
21682     }
21683     for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
21684       utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
21685            pVfs==pCurrent ? "  <--- CURRENT" : "");
21686       raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
21687       raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
21688       raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21689       if( pVfs->pNext ){
21690         raw_printf(p->out, "-----------------------------------\n");
21691       }
21692     }
21693   }else
21694
21695   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
21696     const char *zDbName = nArg==2 ? azArg[1] : "main";
21697     char *zVfsName = 0;
21698     if( p->db ){
21699       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
21700       if( zVfsName ){
21701         utf8_printf(p->out, "%s\n", zVfsName);
21702         sqlite3_free(zVfsName);
21703       }
21704     }
21705   }else
21706
21707   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
21708     unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
21709     sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
21710   }else
21711
21712   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
21713     int j;
21714     assert( nArg<=ArraySize(azArg) );
21715     p->nWidth = nArg-1;
21716     p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
21717     if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
21718     if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
21719     for(j=1; j<nArg; j++){
21720       p->colWidth[j-1] = (int)integerValue(azArg[j]);
21721     }
21722   }else
21723
21724   {
21725     utf8_printf(stderr, "Error: unknown command or invalid arguments: "
21726       " \"%s\". Enter \".help\" for help\n", azArg[0]);
21727     rc = 1;
21728   }
21729
21730 meta_command_exit:
21731   if( p->outCount ){
21732     p->outCount--;
21733     if( p->outCount==0 ) output_reset(p);
21734   }
21735   p->bSafeMode = p->bSafeModePersist;
21736   return rc;
21737 }
21738
21739 /* Line scan result and intermediate states (supporting scan resumption)
21740 */
21741 #ifndef CHAR_BIT
21742 # define CHAR_BIT 8
21743 #endif
21744 typedef enum {
21745   QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
21746   QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
21747   QSS_Start = 0
21748 } QuickScanState;
21749 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
21750 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
21751 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
21752 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
21753 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
21754
21755 /*
21756 ** Scan line for classification to guide shell's handling.
21757 ** The scan is resumable for subsequent lines when prior
21758 ** return values are passed as the 2nd argument.
21759 */
21760 static QuickScanState quickscan(char *zLine, QuickScanState qss){
21761   char cin;
21762   char cWait = (char)qss; /* intentional narrowing loss */
21763   if( cWait==0 ){
21764   PlainScan:
21765     assert( cWait==0 );
21766     while( (cin = *zLine++)!=0 ){
21767       if( IsSpace(cin) )
21768         continue;
21769       switch (cin){
21770       case '-':
21771         if( *zLine!='-' )
21772           break;
21773         while((cin = *++zLine)!=0 )
21774           if( cin=='\n')
21775             goto PlainScan;
21776         return qss;
21777       case ';':
21778         qss |= QSS_EndingSemi;
21779         continue;
21780       case '/':
21781         if( *zLine=='*' ){
21782           ++zLine;
21783           cWait = '*';
21784           qss = QSS_SETV(qss, cWait);
21785           goto TermScan;
21786         }
21787         break;
21788       case '[':
21789         cin = ']';
21790         /* fall thru */
21791       case '`': case '\'': case '"':
21792         cWait = cin;
21793         qss = QSS_HasDark | cWait;
21794         goto TermScan;
21795       default:
21796         break;
21797       }
21798       qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
21799     }
21800   }else{
21801   TermScan:
21802     while( (cin = *zLine++)!=0 ){
21803       if( cin==cWait ){
21804         switch( cWait ){
21805         case '*':
21806           if( *zLine != '/' )
21807             continue;
21808           ++zLine;
21809           cWait = 0;
21810           qss = QSS_SETV(qss, 0);
21811           goto PlainScan;
21812         case '`': case '\'': case '"':
21813           if(*zLine==cWait){
21814             ++zLine;
21815             continue;
21816           }
21817           /* fall thru */
21818         case ']':
21819           cWait = 0;
21820           qss = QSS_SETV(qss, 0);
21821           goto PlainScan;
21822         default: assert(0); 
21823         }
21824       }
21825     }
21826   }
21827   return qss;
21828 }
21829
21830 /*
21831 ** Return TRUE if the line typed in is an SQL command terminator other
21832 ** than a semi-colon.  The SQL Server style "go" command is understood
21833 ** as is the Oracle "/".
21834 */
21835 static int line_is_command_terminator(char *zLine){
21836   while( IsSpace(zLine[0]) ){ zLine++; };
21837   if( zLine[0]=='/' )
21838     zLine += 1; /* Oracle */
21839   else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
21840     zLine += 2; /* SQL Server */
21841   else
21842     return 0;
21843   return quickscan(zLine,QSS_Start)==QSS_Start;
21844 }
21845
21846 /*
21847 ** We need a default sqlite3_complete() implementation to use in case
21848 ** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
21849 ** any arbitrary text is a complete SQL statement.  This is not very
21850 ** user-friendly, but it does seem to work.
21851 */
21852 #ifdef SQLITE_OMIT_COMPLETE
21853 #define sqlite3_complete(x) 1
21854 #endif
21855
21856 /*
21857 ** Return true if zSql is a complete SQL statement.  Return false if it
21858 ** ends in the middle of a string literal or C-style comment.
21859 */
21860 static int line_is_complete(char *zSql, int nSql){
21861   int rc;
21862   if( zSql==0 ) return 1;
21863   zSql[nSql] = ';';
21864   zSql[nSql+1] = 0;
21865   rc = sqlite3_complete(zSql);
21866   zSql[nSql] = 0;
21867   return rc;
21868 }
21869
21870 /*
21871 ** Run a single line of SQL.  Return the number of errors.
21872 */
21873 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
21874   int rc;
21875   char *zErrMsg = 0;
21876
21877   open_db(p, 0);
21878   if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
21879   if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
21880   BEGIN_TIMER;
21881   rc = shell_exec(p, zSql, &zErrMsg);
21882   END_TIMER;
21883   if( rc || zErrMsg ){
21884     char zPrefix[100];
21885     if( in!=0 || !stdin_is_interactive ){
21886       sqlite3_snprintf(sizeof(zPrefix), zPrefix,
21887                        "Error: near line %d:", startline);
21888     }else{
21889       sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
21890     }
21891     if( zErrMsg!=0 ){
21892       utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
21893       sqlite3_free(zErrMsg);
21894       zErrMsg = 0;
21895     }else{
21896       utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
21897     }
21898     return 1;
21899   }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
21900     char zLineBuf[2000];
21901     sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
21902             "changes: %lld   total_changes: %lld",
21903             sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
21904     raw_printf(p->out, "%s\n", zLineBuf);
21905   }
21906   return 0;
21907 }
21908
21909
21910 /*
21911 ** Read input from *in and process it.  If *in==0 then input
21912 ** is interactive - the user is typing it it.  Otherwise, input
21913 ** is coming from a file or device.  A prompt is issued and history
21914 ** is saved only if input is interactive.  An interrupt signal will
21915 ** cause this routine to exit immediately, unless input is interactive.
21916 **
21917 ** Return the number of errors.
21918 */
21919 static int process_input(ShellState *p){
21920   char *zLine = 0;          /* A single input line */
21921   char *zSql = 0;           /* Accumulated SQL text */
21922   int nLine;                /* Length of current line */
21923   int nSql = 0;             /* Bytes of zSql[] used */
21924   int nAlloc = 0;           /* Allocated zSql[] space */
21925   int rc;                   /* Error code */
21926   int errCnt = 0;           /* Number of errors seen */
21927   int startline = 0;        /* Line number for start of current input */
21928   QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
21929
21930   p->lineno = 0;
21931   while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
21932     fflush(p->out);
21933     zLine = one_input_line(p->in, zLine, nSql>0);
21934     if( zLine==0 ){
21935       /* End of input */
21936       if( p->in==0 && stdin_is_interactive ) printf("\n");
21937       break;
21938     }
21939     if( seenInterrupt ){
21940       if( p->in!=0 ) break;
21941       seenInterrupt = 0;
21942     }
21943     p->lineno++;
21944     if( QSS_INPLAIN(qss)
21945         && line_is_command_terminator(zLine)
21946         && line_is_complete(zSql, nSql) ){
21947       memcpy(zLine,";",2);
21948     }
21949     qss = quickscan(zLine, qss);
21950     if( QSS_PLAINWHITE(qss) && nSql==0 ){
21951       if( ShellHasFlag(p, SHFLG_Echo) )
21952         printf("%s\n", zLine);
21953       /* Just swallow single-line whitespace */
21954       qss = QSS_Start;
21955       continue;
21956     }
21957     if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
21958       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
21959       if( zLine[0]=='.' ){
21960         rc = do_meta_command(zLine, p);
21961         if( rc==2 ){ /* exit requested */
21962           break;
21963         }else if( rc ){
21964           errCnt++;
21965         }
21966       }
21967       qss = QSS_Start;
21968       continue;
21969     }
21970     /* No single-line dispositions remain; accumulate line(s). */
21971     nLine = strlen30(zLine);
21972     if( nSql+nLine+2>=nAlloc ){
21973       /* Grow buffer by half-again increments when big. */
21974       nAlloc = nSql+(nSql>>1)+nLine+100;
21975       zSql = realloc(zSql, nAlloc);
21976       if( zSql==0 ) shell_out_of_memory();
21977     }
21978     if( nSql==0 ){
21979       int i;
21980       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
21981       assert( nAlloc>0 && zSql!=0 );
21982       memcpy(zSql, zLine+i, nLine+1-i);
21983       startline = p->lineno;
21984       nSql = nLine-i;
21985     }else{
21986       zSql[nSql++] = '\n';
21987       memcpy(zSql+nSql, zLine, nLine+1);
21988       nSql += nLine;
21989     }
21990     if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
21991       errCnt += runOneSqlLine(p, zSql, p->in, startline);
21992       nSql = 0;
21993       if( p->outCount ){
21994         output_reset(p);
21995         p->outCount = 0;
21996       }else{
21997         clearTempFile(p);
21998       }
21999       p->bSafeMode = p->bSafeModePersist;
22000       qss = QSS_Start;
22001     }else if( nSql && QSS_PLAINWHITE(qss) ){
22002       if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
22003       nSql = 0;
22004       qss = QSS_Start;
22005     }
22006   }
22007   if( nSql && QSS_PLAINDARK(qss) ){
22008     errCnt += runOneSqlLine(p, zSql, p->in, startline);
22009   }
22010   free(zSql);
22011   free(zLine);
22012   return errCnt>0;
22013 }
22014
22015 /*
22016 ** Return a pathname which is the user's home directory.  A
22017 ** 0 return indicates an error of some kind.
22018 */
22019 static char *find_home_dir(int clearFlag){
22020   static char *home_dir = NULL;
22021   if( clearFlag ){
22022     free(home_dir);
22023     home_dir = 0;
22024     return 0;
22025   }
22026   if( home_dir ) return home_dir;
22027
22028 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
22029      && !defined(__RTP__) && !defined(_WRS_KERNEL)
22030   {
22031     struct passwd *pwent;
22032     uid_t uid = getuid();
22033     if( (pwent=getpwuid(uid)) != NULL) {
22034       home_dir = pwent->pw_dir;
22035     }
22036   }
22037 #endif
22038
22039 #if defined(_WIN32_WCE)
22040   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
22041    */
22042   home_dir = "/";
22043 #else
22044
22045 #if defined(_WIN32) || defined(WIN32)
22046   if (!home_dir) {
22047     home_dir = getenv("USERPROFILE");
22048   }
22049 #endif
22050
22051   if (!home_dir) {
22052     home_dir = getenv("HOME");
22053   }
22054
22055 #if defined(_WIN32) || defined(WIN32)
22056   if (!home_dir) {
22057     char *zDrive, *zPath;
22058     int n;
22059     zDrive = getenv("HOMEDRIVE");
22060     zPath = getenv("HOMEPATH");
22061     if( zDrive && zPath ){
22062       n = strlen30(zDrive) + strlen30(zPath) + 1;
22063       home_dir = malloc( n );
22064       if( home_dir==0 ) return 0;
22065       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
22066       return home_dir;
22067     }
22068     home_dir = "c:\\";
22069   }
22070 #endif
22071
22072 #endif /* !_WIN32_WCE */
22073
22074   if( home_dir ){
22075     int n = strlen30(home_dir) + 1;
22076     char *z = malloc( n );
22077     if( z ) memcpy(z, home_dir, n);
22078     home_dir = z;
22079   }
22080
22081   return home_dir;
22082 }
22083
22084 /*
22085 ** Read input from the file given by sqliterc_override.  Or if that
22086 ** parameter is NULL, take input from ~/.sqliterc
22087 **
22088 ** Returns the number of errors.
22089 */
22090 static void process_sqliterc(
22091   ShellState *p,                  /* Configuration data */
22092   const char *sqliterc_override   /* Name of config file. NULL to use default */
22093 ){
22094   char *home_dir = NULL;
22095   const char *sqliterc = sqliterc_override;
22096   char *zBuf = 0;
22097   FILE *inSaved = p->in;
22098   int savedLineno = p->lineno;
22099
22100   if (sqliterc == NULL) {
22101     home_dir = find_home_dir(0);
22102     if( home_dir==0 ){
22103       raw_printf(stderr, "-- warning: cannot find home directory;"
22104                       " cannot read ~/.sqliterc\n");
22105       return;
22106     }
22107     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
22108     sqliterc = zBuf;
22109   }
22110   p->in = fopen(sqliterc,"rb");
22111   if( p->in ){
22112     if( stdin_is_interactive ){
22113       utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
22114     }
22115     if( process_input(p) && bail_on_error ) exit(1);
22116     fclose(p->in);
22117   }else if( sqliterc_override!=0 ){
22118     utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
22119     if( bail_on_error ) exit(1);
22120   }
22121   p->in = inSaved;
22122   p->lineno = savedLineno;
22123   sqlite3_free(zBuf);
22124 }
22125
22126 /*
22127 ** Show available command line options
22128 */
22129 static const char zOptions[] =
22130 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
22131   "   -A ARGS...           run \".archive ARGS\" and exit\n"
22132 #endif
22133   "   -append              append the database to the end of the file\n"
22134   "   -ascii               set output mode to 'ascii'\n"
22135   "   -bail                stop after hitting an error\n"
22136   "   -batch               force batch I/O\n"
22137   "   -box                 set output mode to 'box'\n"
22138   "   -column              set output mode to 'column'\n"
22139   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
22140   "   -csv                 set output mode to 'csv'\n"
22141 #if !defined(SQLITE_OMIT_DESERIALIZE)
22142   "   -deserialize         open the database using sqlite3_deserialize()\n"
22143 #endif
22144   "   -echo                print commands before execution\n"
22145   "   -init FILENAME       read/process named file\n"
22146   "   -[no]header          turn headers on or off\n"
22147 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
22148   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
22149 #endif
22150   "   -help                show this message\n"
22151   "   -html                set output mode to HTML\n"
22152   "   -interactive         force interactive I/O\n"
22153   "   -json                set output mode to 'json'\n"
22154   "   -line                set output mode to 'line'\n"
22155   "   -list                set output mode to 'list'\n"
22156   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
22157   "   -markdown            set output mode to 'markdown'\n"
22158 #if !defined(SQLITE_OMIT_DESERIALIZE)
22159   "   -maxsize N           maximum size for a --deserialize database\n"
22160 #endif
22161   "   -memtrace            trace all memory allocations and deallocations\n"
22162   "   -mmap N              default mmap size set to N\n"
22163 #ifdef SQLITE_ENABLE_MULTIPLEX
22164   "   -multiplex           enable the multiplexor VFS\n"
22165 #endif
22166   "   -newline SEP         set output row separator. Default: '\\n'\n"
22167   "   -nofollow            refuse to open symbolic links to database files\n"
22168   "   -nonce STRING        set the safe-mode escape nonce\n"
22169   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
22170   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
22171   "   -quote               set output mode to 'quote'\n"
22172   "   -readonly            open the database read-only\n"
22173   "   -safe                enable safe-mode\n"
22174   "   -separator SEP       set output column separator. Default: '|'\n"
22175 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22176   "   -sorterref SIZE      sorter references threshold size\n"
22177 #endif
22178   "   -stats               print memory stats before each finalize\n"
22179   "   -table               set output mode to 'table'\n"
22180   "   -tabs                set output mode to 'tabs'\n"
22181   "   -version             show SQLite version\n"
22182   "   -vfs NAME            use NAME as the default VFS\n"
22183 #ifdef SQLITE_ENABLE_VFSTRACE
22184   "   -vfstrace            enable tracing of all VFS calls\n"
22185 #endif
22186 #ifdef SQLITE_HAVE_ZLIB
22187   "   -zip                 open the file as a ZIP Archive\n"
22188 #endif
22189 ;
22190 static void usage(int showDetail){
22191   utf8_printf(stderr,
22192       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
22193       "FILENAME is the name of an SQLite database. A new database is created\n"
22194       "if the file does not previously exist.\n", Argv0);
22195   if( showDetail ){
22196     utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
22197   }else{
22198     raw_printf(stderr, "Use the -help option for additional information\n");
22199   }
22200   exit(1);
22201 }
22202
22203 /*
22204 ** Internal check:  Verify that the SQLite is uninitialized.  Print a
22205 ** error message if it is initialized.
22206 */
22207 static void verify_uninitialized(void){
22208   if( sqlite3_config(-1)==SQLITE_MISUSE ){
22209     utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
22210                         " initialization.\n");
22211   }
22212 }
22213
22214 /*
22215 ** Initialize the state information in data
22216 */
22217 static void main_init(ShellState *data) {
22218   memset(data, 0, sizeof(*data));
22219   data->normalMode = data->cMode = data->mode = MODE_List;
22220   data->autoExplain = 1;
22221   data->pAuxDb = &data->aAuxDb[0];
22222   memcpy(data->colSeparator,SEP_Column, 2);
22223   memcpy(data->rowSeparator,SEP_Row, 2);
22224   data->showHeader = 0;
22225   data->shellFlgs = SHFLG_Lookaside;
22226   verify_uninitialized();
22227   sqlite3_config(SQLITE_CONFIG_URI, 1);
22228   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
22229   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
22230   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
22231   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
22232 }
22233
22234 /*
22235 ** Output text to the console in a font that attracts extra attention.
22236 */
22237 #ifdef _WIN32
22238 static void printBold(const char *zText){
22239 #if !SQLITE_OS_WINRT
22240   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
22241   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
22242   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
22243   SetConsoleTextAttribute(out,
22244          FOREGROUND_RED|FOREGROUND_INTENSITY
22245   );
22246 #endif
22247   printf("%s", zText);
22248 #if !SQLITE_OS_WINRT
22249   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
22250 #endif
22251 }
22252 #else
22253 static void printBold(const char *zText){
22254   printf("\033[1m%s\033[0m", zText);
22255 }
22256 #endif
22257
22258 /*
22259 ** Get the argument to an --option.  Throw an error and die if no argument
22260 ** is available.
22261 */
22262 static char *cmdline_option_value(int argc, char **argv, int i){
22263   if( i==argc ){
22264     utf8_printf(stderr, "%s: Error: missing argument to %s\n",
22265             argv[0], argv[argc-1]);
22266     exit(1);
22267   }
22268   return argv[i];
22269 }
22270
22271 #ifndef SQLITE_SHELL_IS_UTF8
22272 #  if (defined(_WIN32) || defined(WIN32)) \
22273    && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
22274 #    define SQLITE_SHELL_IS_UTF8          (0)
22275 #  else
22276 #    define SQLITE_SHELL_IS_UTF8          (1)
22277 #  endif
22278 #endif
22279
22280 #if SQLITE_SHELL_IS_UTF8
22281 int SQLITE_CDECL main(int argc, char **argv){
22282 #else
22283 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
22284   char **argv;
22285 #endif
22286   char *zErrMsg = 0;
22287   ShellState data;
22288   const char *zInitFile = 0;
22289   int i;
22290   int rc = 0;
22291   int warnInmemoryDb = 0;
22292   int readStdin = 1;
22293   int nCmd = 0;
22294   char **azCmd = 0;
22295   const char *zVfs = 0;           /* Value of -vfs command-line option */
22296 #if !SQLITE_SHELL_IS_UTF8
22297   char **argvToFree = 0;
22298   int argcToFree = 0;
22299 #endif
22300
22301   setBinaryMode(stdin, 0);
22302   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
22303   stdin_is_interactive = isatty(0);
22304   stdout_is_console = isatty(1);
22305
22306 #ifdef SQLITE_DEBUG
22307   registerOomSimulator();
22308 #endif
22309
22310 #if !defined(_WIN32_WCE)
22311   if( getenv("SQLITE_DEBUG_BREAK") ){
22312     if( isatty(0) && isatty(2) ){
22313       fprintf(stderr,
22314           "attach debugger to process %d and press any key to continue.\n",
22315           GETPID());
22316       fgetc(stdin);
22317     }else{
22318 #if defined(_WIN32) || defined(WIN32)
22319 #if SQLITE_OS_WINRT
22320       __debugbreak();
22321 #else
22322       DebugBreak();
22323 #endif
22324 #elif defined(SIGTRAP)
22325       raise(SIGTRAP);
22326 #endif
22327     }
22328   }
22329 #endif
22330
22331 #if USE_SYSTEM_SQLITE+0!=1
22332   if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
22333     utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
22334             sqlite3_sourceid(), SQLITE_SOURCE_ID);
22335     exit(1);
22336   }
22337 #endif
22338   main_init(&data);
22339
22340   /* On Windows, we must translate command-line arguments into UTF-8.
22341   ** The SQLite memory allocator subsystem has to be enabled in order to
22342   ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
22343   ** subsequent sqlite3_config() calls will work.  So copy all results into
22344   ** memory that does not come from the SQLite memory allocator.
22345   */
22346 #if !SQLITE_SHELL_IS_UTF8
22347   sqlite3_initialize();
22348   argvToFree = malloc(sizeof(argv[0])*argc*2);
22349   argcToFree = argc;
22350   argv = argvToFree + argc;
22351   if( argv==0 ) shell_out_of_memory();
22352   for(i=0; i<argc; i++){
22353     char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
22354     int n;
22355     if( z==0 ) shell_out_of_memory();
22356     n = (int)strlen(z);
22357     argv[i] = malloc( n+1 );
22358     if( argv[i]==0 ) shell_out_of_memory();
22359     memcpy(argv[i], z, n+1);
22360     argvToFree[i] = argv[i];
22361     sqlite3_free(z);
22362   }
22363   sqlite3_shutdown();
22364 #endif
22365
22366   assert( argc>=1 && argv && argv[0] );
22367   Argv0 = argv[0];
22368
22369   /* Make sure we have a valid signal handler early, before anything
22370   ** else is done.
22371   */
22372 #ifdef SIGINT
22373   signal(SIGINT, interrupt_handler);
22374 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
22375   SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
22376 #endif
22377
22378 #ifdef SQLITE_SHELL_DBNAME_PROC
22379   {
22380     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
22381     ** of a C-function that will provide the name of the database file.  Use
22382     ** this compile-time option to embed this shell program in larger
22383     ** applications. */
22384     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
22385     SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
22386     warnInmemoryDb = 0;
22387   }
22388 #endif
22389
22390   /* Do an initial pass through the command-line argument to locate
22391   ** the name of the database file, the name of the initialization file,
22392   ** the size of the alternative malloc heap,
22393   ** and the first command to execute.
22394   */
22395   verify_uninitialized();
22396   for(i=1; i<argc; i++){
22397     char *z;
22398     z = argv[i];
22399     if( z[0]!='-' ){
22400       if( data.aAuxDb->zDbFilename==0 ){
22401         data.aAuxDb->zDbFilename = z;
22402       }else{
22403         /* Excesss arguments are interpreted as SQL (or dot-commands) and
22404         ** mean that nothing is read from stdin */
22405         readStdin = 0;
22406         nCmd++;
22407         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
22408         if( azCmd==0 ) shell_out_of_memory();
22409         azCmd[nCmd-1] = z;
22410       }
22411     }
22412     if( z[1]=='-' ) z++;
22413     if( strcmp(z,"-separator")==0
22414      || strcmp(z,"-nullvalue")==0
22415      || strcmp(z,"-newline")==0
22416      || strcmp(z,"-cmd")==0
22417     ){
22418       (void)cmdline_option_value(argc, argv, ++i);
22419     }else if( strcmp(z,"-init")==0 ){
22420       zInitFile = cmdline_option_value(argc, argv, ++i);
22421     }else if( strcmp(z,"-batch")==0 ){
22422       /* Need to check for batch mode here to so we can avoid printing
22423       ** informational messages (like from process_sqliterc) before
22424       ** we do the actual processing of arguments later in a second pass.
22425       */
22426       stdin_is_interactive = 0;
22427     }else if( strcmp(z,"-heap")==0 ){
22428 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
22429       const char *zSize;
22430       sqlite3_int64 szHeap;
22431
22432       zSize = cmdline_option_value(argc, argv, ++i);
22433       szHeap = integerValue(zSize);
22434       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
22435       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
22436 #else
22437       (void)cmdline_option_value(argc, argv, ++i);
22438 #endif
22439     }else if( strcmp(z,"-pagecache")==0 ){
22440       sqlite3_int64 n, sz;
22441       sz = integerValue(cmdline_option_value(argc,argv,++i));
22442       if( sz>70000 ) sz = 70000;
22443       if( sz<0 ) sz = 0;
22444       n = integerValue(cmdline_option_value(argc,argv,++i));
22445       if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
22446         n = 0xffffffffffffLL/sz;
22447       }
22448       sqlite3_config(SQLITE_CONFIG_PAGECACHE,
22449                     (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
22450       data.shellFlgs |= SHFLG_Pagecache;
22451     }else if( strcmp(z,"-lookaside")==0 ){
22452       int n, sz;
22453       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
22454       if( sz<0 ) sz = 0;
22455       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
22456       if( n<0 ) n = 0;
22457       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
22458       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
22459     }else if( strcmp(z,"-threadsafe")==0 ){
22460       int n;
22461       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
22462       switch( n ){
22463          case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
22464          case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
22465          default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
22466       }
22467 #ifdef SQLITE_ENABLE_VFSTRACE
22468     }else if( strcmp(z,"-vfstrace")==0 ){
22469       extern int vfstrace_register(
22470          const char *zTraceName,
22471          const char *zOldVfsName,
22472          int (*xOut)(const char*,void*),
22473          void *pOutArg,
22474          int makeDefault
22475       );
22476       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
22477 #endif
22478 #ifdef SQLITE_ENABLE_MULTIPLEX
22479     }else if( strcmp(z,"-multiplex")==0 ){
22480       extern int sqlite3_multiple_initialize(const char*,int);
22481       sqlite3_multiplex_initialize(0, 1);
22482 #endif
22483     }else if( strcmp(z,"-mmap")==0 ){
22484       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22485       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
22486 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22487     }else if( strcmp(z,"-sorterref")==0 ){
22488       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22489       sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
22490 #endif
22491     }else if( strcmp(z,"-vfs")==0 ){
22492       zVfs = cmdline_option_value(argc, argv, ++i);
22493 #ifdef SQLITE_HAVE_ZLIB
22494     }else if( strcmp(z,"-zip")==0 ){
22495       data.openMode = SHELL_OPEN_ZIPFILE;
22496 #endif
22497     }else if( strcmp(z,"-append")==0 ){
22498       data.openMode = SHELL_OPEN_APPENDVFS;
22499 #ifndef SQLITE_OMIT_DESERIALIZE
22500     }else if( strcmp(z,"-deserialize")==0 ){
22501       data.openMode = SHELL_OPEN_DESERIALIZE;
22502     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22503       data.szMax = integerValue(argv[++i]);
22504 #endif
22505     }else if( strcmp(z,"-readonly")==0 ){
22506       data.openMode = SHELL_OPEN_READONLY;
22507     }else if( strcmp(z,"-nofollow")==0 ){
22508       data.openFlags = SQLITE_OPEN_NOFOLLOW;
22509 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22510     }else if( strncmp(z, "-A",2)==0 ){
22511       /* All remaining command-line arguments are passed to the ".archive"
22512       ** command, so ignore them */
22513       break;
22514 #endif
22515     }else if( strcmp(z, "-memtrace")==0 ){
22516       sqlite3MemTraceActivate(stderr);
22517     }else if( strcmp(z,"-bail")==0 ){
22518       bail_on_error = 1;
22519     }else if( strcmp(z,"-nonce")==0 ){
22520       free(data.zNonce);
22521       data.zNonce = strdup(argv[++i]);
22522     }else if( strcmp(z,"-safe")==0 ){
22523       /* no-op - catch this on the second pass */
22524     }
22525   }
22526   verify_uninitialized();
22527
22528
22529 #ifdef SQLITE_SHELL_INIT_PROC
22530   {
22531     /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
22532     ** of a C-function that will perform initialization actions on SQLite that
22533     ** occur just before or after sqlite3_initialize(). Use this compile-time
22534     ** option to embed this shell program in larger applications. */
22535     extern void SQLITE_SHELL_INIT_PROC(void);
22536     SQLITE_SHELL_INIT_PROC();
22537   }
22538 #else
22539   /* All the sqlite3_config() calls have now been made. So it is safe
22540   ** to call sqlite3_initialize() and process any command line -vfs option. */
22541   sqlite3_initialize();
22542 #endif
22543
22544   if( zVfs ){
22545     sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
22546     if( pVfs ){
22547       sqlite3_vfs_register(pVfs, 1);
22548     }else{
22549       utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
22550       exit(1);
22551     }
22552   }
22553
22554   if( data.pAuxDb->zDbFilename==0 ){
22555 #ifndef SQLITE_OMIT_MEMORYDB
22556     data.pAuxDb->zDbFilename = ":memory:";
22557     warnInmemoryDb = argc==1;
22558 #else
22559     utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
22560     return 1;
22561 #endif
22562   }
22563   data.out = stdout;
22564   sqlite3_appendvfs_init(0,0,0);
22565
22566   /* Go ahead and open the database file if it already exists.  If the
22567   ** file does not exist, delay opening it.  This prevents empty database
22568   ** files from being created if a user mistypes the database name argument
22569   ** to the sqlite command-line tool.
22570   */
22571   if( access(data.pAuxDb->zDbFilename, 0)==0 ){
22572     open_db(&data, 0);
22573   }
22574
22575   /* Process the initialization file if there is one.  If no -init option
22576   ** is given on the command line, look for a file named ~/.sqliterc and
22577   ** try to process it.
22578   */
22579   process_sqliterc(&data,zInitFile);
22580
22581   /* Make a second pass through the command-line argument and set
22582   ** options.  This second pass is delayed until after the initialization
22583   ** file is processed so that the command-line arguments will override
22584   ** settings in the initialization file.
22585   */
22586   for(i=1; i<argc; i++){
22587     char *z = argv[i];
22588     if( z[0]!='-' ) continue;
22589     if( z[1]=='-' ){ z++; }
22590     if( strcmp(z,"-init")==0 ){
22591       i++;
22592     }else if( strcmp(z,"-html")==0 ){
22593       data.mode = MODE_Html;
22594     }else if( strcmp(z,"-list")==0 ){
22595       data.mode = MODE_List;
22596     }else if( strcmp(z,"-quote")==0 ){
22597       data.mode = MODE_Quote;
22598       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
22599       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22600     }else if( strcmp(z,"-line")==0 ){
22601       data.mode = MODE_Line;
22602     }else if( strcmp(z,"-column")==0 ){
22603       data.mode = MODE_Column;
22604     }else if( strcmp(z,"-json")==0 ){
22605       data.mode = MODE_Json;
22606     }else if( strcmp(z,"-markdown")==0 ){
22607       data.mode = MODE_Markdown;
22608     }else if( strcmp(z,"-table")==0 ){
22609       data.mode = MODE_Table;
22610     }else if( strcmp(z,"-box")==0 ){
22611       data.mode = MODE_Box;
22612     }else if( strcmp(z,"-csv")==0 ){
22613       data.mode = MODE_Csv;
22614       memcpy(data.colSeparator,",",2);
22615 #ifdef SQLITE_HAVE_ZLIB
22616     }else if( strcmp(z,"-zip")==0 ){
22617       data.openMode = SHELL_OPEN_ZIPFILE;
22618 #endif
22619     }else if( strcmp(z,"-append")==0 ){
22620       data.openMode = SHELL_OPEN_APPENDVFS;
22621 #ifndef SQLITE_OMIT_DESERIALIZE
22622     }else if( strcmp(z,"-deserialize")==0 ){
22623       data.openMode = SHELL_OPEN_DESERIALIZE;
22624     }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22625       data.szMax = integerValue(argv[++i]);
22626 #endif
22627     }else if( strcmp(z,"-readonly")==0 ){
22628       data.openMode = SHELL_OPEN_READONLY;
22629     }else if( strcmp(z,"-nofollow")==0 ){
22630       data.openFlags |= SQLITE_OPEN_NOFOLLOW;
22631     }else if( strcmp(z,"-ascii")==0 ){
22632       data.mode = MODE_Ascii;
22633       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
22634       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
22635     }else if( strcmp(z,"-tabs")==0 ){
22636       data.mode = MODE_List;
22637       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
22638       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22639     }else if( strcmp(z,"-separator")==0 ){
22640       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
22641                        "%s",cmdline_option_value(argc,argv,++i));
22642     }else if( strcmp(z,"-newline")==0 ){
22643       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
22644                        "%s",cmdline_option_value(argc,argv,++i));
22645     }else if( strcmp(z,"-nullvalue")==0 ){
22646       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
22647                        "%s",cmdline_option_value(argc,argv,++i));
22648     }else if( strcmp(z,"-header")==0 ){
22649       data.showHeader = 1;
22650       ShellSetFlag(&data, SHFLG_HeaderSet);
22651      }else if( strcmp(z,"-noheader")==0 ){
22652       data.showHeader = 0;
22653       ShellSetFlag(&data, SHFLG_HeaderSet);
22654     }else if( strcmp(z,"-echo")==0 ){
22655       ShellSetFlag(&data, SHFLG_Echo);
22656     }else if( strcmp(z,"-eqp")==0 ){
22657       data.autoEQP = AUTOEQP_on;
22658     }else if( strcmp(z,"-eqpfull")==0 ){
22659       data.autoEQP = AUTOEQP_full;
22660     }else if( strcmp(z,"-stats")==0 ){
22661       data.statsOn = 1;
22662     }else if( strcmp(z,"-scanstats")==0 ){
22663       data.scanstatsOn = 1;
22664     }else if( strcmp(z,"-backslash")==0 ){
22665       /* Undocumented command-line option: -backslash
22666       ** Causes C-style backslash escapes to be evaluated in SQL statements
22667       ** prior to sending the SQL into SQLite.  Useful for injecting
22668       ** crazy bytes in the middle of SQL statements for testing and debugging.
22669       */
22670       ShellSetFlag(&data, SHFLG_Backslash);
22671     }else if( strcmp(z,"-bail")==0 ){
22672       /* No-op.  The bail_on_error flag should already be set. */
22673     }else if( strcmp(z,"-version")==0 ){
22674       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
22675       return 0;
22676     }else if( strcmp(z,"-interactive")==0 ){
22677       stdin_is_interactive = 1;
22678     }else if( strcmp(z,"-batch")==0 ){
22679       stdin_is_interactive = 0;
22680     }else if( strcmp(z,"-heap")==0 ){
22681       i++;
22682     }else if( strcmp(z,"-pagecache")==0 ){
22683       i+=2;
22684     }else if( strcmp(z,"-lookaside")==0 ){
22685       i+=2;
22686     }else if( strcmp(z,"-threadsafe")==0 ){
22687       i+=2;
22688     }else if( strcmp(z,"-nonce")==0 ){
22689       i += 2;
22690     }else if( strcmp(z,"-mmap")==0 ){
22691       i++;
22692     }else if( strcmp(z,"-memtrace")==0 ){
22693       i++;
22694 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22695     }else if( strcmp(z,"-sorterref")==0 ){
22696       i++;
22697 #endif
22698     }else if( strcmp(z,"-vfs")==0 ){
22699       i++;
22700 #ifdef SQLITE_ENABLE_VFSTRACE
22701     }else if( strcmp(z,"-vfstrace")==0 ){
22702       i++;
22703 #endif
22704 #ifdef SQLITE_ENABLE_MULTIPLEX
22705     }else if( strcmp(z,"-multiplex")==0 ){
22706       i++;
22707 #endif
22708     }else if( strcmp(z,"-help")==0 ){
22709       usage(1);
22710     }else if( strcmp(z,"-cmd")==0 ){
22711       /* Run commands that follow -cmd first and separately from commands
22712       ** that simply appear on the command-line.  This seems goofy.  It would
22713       ** be better if all commands ran in the order that they appear.  But
22714       ** we retain the goofy behavior for historical compatibility. */
22715       if( i==argc-1 ) break;
22716       z = cmdline_option_value(argc,argv,++i);
22717       if( z[0]=='.' ){
22718         rc = do_meta_command(z, &data);
22719         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
22720       }else{
22721         open_db(&data, 0);
22722         rc = shell_exec(&data, z, &zErrMsg);
22723         if( zErrMsg!=0 ){
22724           utf8_printf(stderr,"Error: %s\n", zErrMsg);
22725           if( bail_on_error ) return rc!=0 ? rc : 1;
22726         }else if( rc!=0 ){
22727           utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
22728           if( bail_on_error ) return rc;
22729         }
22730       }
22731 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22732     }else if( strncmp(z, "-A", 2)==0 ){
22733       if( nCmd>0 ){
22734         utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
22735                             " with \"%s\"\n", z);
22736         return 1;
22737       }
22738       open_db(&data, OPEN_DB_ZIPFILE);
22739       if( z[2] ){
22740         argv[i] = &z[2];
22741         arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
22742       }else{
22743         arDotCommand(&data, 1, argv+i, argc-i);
22744       }
22745       readStdin = 0;
22746       break;
22747 #endif
22748     }else if( strcmp(z,"-safe")==0 ){
22749       data.bSafeMode = data.bSafeModePersist = 1;
22750     }else{
22751       utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
22752       raw_printf(stderr,"Use -help for a list of options.\n");
22753       return 1;
22754     }
22755     data.cMode = data.mode;
22756   }
22757
22758   if( !readStdin ){
22759     /* Run all arguments that do not begin with '-' as if they were separate
22760     ** command-line inputs, except for the argToSkip argument which contains
22761     ** the database filename.
22762     */
22763     for(i=0; i<nCmd; i++){
22764       if( azCmd[i][0]=='.' ){
22765         rc = do_meta_command(azCmd[i], &data);
22766         if( rc ){
22767           free(azCmd);
22768           return rc==2 ? 0 : rc;
22769         }
22770       }else{
22771         open_db(&data, 0);
22772         rc = shell_exec(&data, azCmd[i], &zErrMsg);
22773         if( zErrMsg || rc ){
22774           if( zErrMsg!=0 ){
22775             utf8_printf(stderr,"Error: %s\n", zErrMsg);
22776           }else{
22777             utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
22778           }
22779           sqlite3_free(zErrMsg);
22780           free(azCmd);
22781           return rc!=0 ? rc : 1;
22782         }
22783       }
22784     }
22785   }else{
22786     /* Run commands received from standard input
22787     */
22788     if( stdin_is_interactive ){
22789       char *zHome;
22790       char *zHistory;
22791       int nHistory;
22792       printf(
22793         "SQLite version %s %.19s\n" /*extra-version-info*/
22794         "Enter \".help\" for usage hints.\n",
22795         sqlite3_libversion(), sqlite3_sourceid()
22796       );
22797       if( warnInmemoryDb ){
22798         printf("Connected to a ");
22799         printBold("transient in-memory database");
22800         printf(".\nUse \".open FILENAME\" to reopen on a "
22801                "persistent database.\n");
22802       }
22803       zHistory = getenv("SQLITE_HISTORY");
22804       if( zHistory ){
22805         zHistory = strdup(zHistory);
22806       }else if( (zHome = find_home_dir(0))!=0 ){
22807         nHistory = strlen30(zHome) + 20;
22808         if( (zHistory = malloc(nHistory))!=0 ){
22809           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
22810         }
22811       }
22812       if( zHistory ){ shell_read_history(zHistory); }
22813 #if HAVE_READLINE || HAVE_EDITLINE
22814       rl_attempted_completion_function = readline_completion;
22815 #elif HAVE_LINENOISE
22816       linenoiseSetCompletionCallback(linenoise_completion);
22817 #endif
22818       data.in = 0;
22819       rc = process_input(&data);
22820       if( zHistory ){
22821         shell_stifle_history(2000);
22822         shell_write_history(zHistory);
22823         free(zHistory);
22824       }
22825     }else{
22826       data.in = stdin;
22827       rc = process_input(&data);
22828     }
22829   }
22830   free(azCmd);
22831   set_table_name(&data, 0);
22832   if( data.db ){
22833     session_close_all(&data, -1);
22834     close_db(data.db);
22835   }
22836   for(i=0; i<ArraySize(data.aAuxDb); i++){
22837     sqlite3_free(data.aAuxDb[i].zFreeOnClose);
22838     if( data.aAuxDb[i].db ){
22839       session_close_all(&data, i);
22840       close_db(data.aAuxDb[i].db);
22841     }
22842   }
22843   find_home_dir(1);
22844   output_reset(&data);
22845   data.doXdgOpen = 0;
22846   clearTempFile(&data);
22847 #if !SQLITE_SHELL_IS_UTF8
22848   for(i=0; i<argcToFree; i++) free(argvToFree[i]);
22849   free(argvToFree);
22850 #endif
22851   free(data.colWidth);
22852   free(data.zNonce);
22853   /* Clear the global data structure so that valgrind will detect memory
22854   ** leaks */
22855   memset(&data, 0, sizeof(data));
22856   return rc;
22857 }